xref: /openbmc/linux/mm/vmalloc.c (revision 80e93effce55044c5a7fa96e8b313640a80bd4e9)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/vmalloc.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1993  Linus Torvalds
51da177e4SLinus Torvalds  *  Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
61da177e4SLinus Torvalds  *  SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000
71da177e4SLinus Torvalds  *  Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/mm.h>
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/highmem.h>
131da177e4SLinus Torvalds #include <linux/slab.h>
141da177e4SLinus Torvalds #include <linux/spinlock.h>
151da177e4SLinus Torvalds #include <linux/interrupt.h>
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/vmalloc.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <asm/uaccess.h>
201da177e4SLinus Torvalds #include <asm/tlbflush.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds DEFINE_RWLOCK(vmlist_lock);
241da177e4SLinus Torvalds struct vm_struct *vmlist;
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
271da177e4SLinus Torvalds {
281da177e4SLinus Torvalds 	pte_t *pte;
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds 	pte = pte_offset_kernel(pmd, addr);
311da177e4SLinus Torvalds 	do {
321da177e4SLinus Torvalds 		pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
331da177e4SLinus Torvalds 		WARN_ON(!pte_none(ptent) && !pte_present(ptent));
341da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
351da177e4SLinus Torvalds }
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds static inline void vunmap_pmd_range(pud_t *pud, unsigned long addr,
381da177e4SLinus Torvalds 						unsigned long end)
391da177e4SLinus Torvalds {
401da177e4SLinus Torvalds 	pmd_t *pmd;
411da177e4SLinus Torvalds 	unsigned long next;
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
441da177e4SLinus Torvalds 	do {
451da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
461da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
471da177e4SLinus Torvalds 			continue;
481da177e4SLinus Torvalds 		vunmap_pte_range(pmd, addr, next);
491da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
501da177e4SLinus Torvalds }
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds static inline void vunmap_pud_range(pgd_t *pgd, unsigned long addr,
531da177e4SLinus Torvalds 						unsigned long end)
541da177e4SLinus Torvalds {
551da177e4SLinus Torvalds 	pud_t *pud;
561da177e4SLinus Torvalds 	unsigned long next;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
591da177e4SLinus Torvalds 	do {
601da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
611da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
621da177e4SLinus Torvalds 			continue;
631da177e4SLinus Torvalds 		vunmap_pmd_range(pud, addr, next);
641da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
651da177e4SLinus Torvalds }
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds void unmap_vm_area(struct vm_struct *area)
681da177e4SLinus Torvalds {
691da177e4SLinus Torvalds 	pgd_t *pgd;
701da177e4SLinus Torvalds 	unsigned long next;
711da177e4SLinus Torvalds 	unsigned long addr = (unsigned long) area->addr;
721da177e4SLinus Torvalds 	unsigned long end = addr + area->size;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	BUG_ON(addr >= end);
751da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
761da177e4SLinus Torvalds 	flush_cache_vunmap(addr, end);
771da177e4SLinus Torvalds 	do {
781da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
791da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
801da177e4SLinus Torvalds 			continue;
811da177e4SLinus Torvalds 		vunmap_pud_range(pgd, addr, next);
821da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
831da177e4SLinus Torvalds 	flush_tlb_kernel_range((unsigned long) area->addr, end);
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
871da177e4SLinus Torvalds 			unsigned long end, pgprot_t prot, struct page ***pages)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	pte_t *pte;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	pte = pte_alloc_kernel(&init_mm, pmd, addr);
921da177e4SLinus Torvalds 	if (!pte)
931da177e4SLinus Torvalds 		return -ENOMEM;
941da177e4SLinus Torvalds 	do {
951da177e4SLinus Torvalds 		struct page *page = **pages;
961da177e4SLinus Torvalds 		WARN_ON(!pte_none(*pte));
971da177e4SLinus Torvalds 		if (!page)
981da177e4SLinus Torvalds 			return -ENOMEM;
991da177e4SLinus Torvalds 		set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
1001da177e4SLinus Torvalds 		(*pages)++;
1011da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1021da177e4SLinus Torvalds 	return 0;
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds static inline int vmap_pmd_range(pud_t *pud, unsigned long addr,
1061da177e4SLinus Torvalds 			unsigned long end, pgprot_t prot, struct page ***pages)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	pmd_t *pmd;
1091da177e4SLinus Torvalds 	unsigned long next;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	pmd = pmd_alloc(&init_mm, pud, addr);
1121da177e4SLinus Torvalds 	if (!pmd)
1131da177e4SLinus Torvalds 		return -ENOMEM;
1141da177e4SLinus Torvalds 	do {
1151da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
1161da177e4SLinus Torvalds 		if (vmap_pte_range(pmd, addr, next, prot, pages))
1171da177e4SLinus Torvalds 			return -ENOMEM;
1181da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
1191da177e4SLinus Torvalds 	return 0;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds static inline int vmap_pud_range(pgd_t *pgd, unsigned long addr,
1231da177e4SLinus Torvalds 			unsigned long end, pgprot_t prot, struct page ***pages)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	pud_t *pud;
1261da177e4SLinus Torvalds 	unsigned long next;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	pud = pud_alloc(&init_mm, pgd, addr);
1291da177e4SLinus Torvalds 	if (!pud)
1301da177e4SLinus Torvalds 		return -ENOMEM;
1311da177e4SLinus Torvalds 	do {
1321da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
1331da177e4SLinus Torvalds 		if (vmap_pmd_range(pud, addr, next, prot, pages))
1341da177e4SLinus Torvalds 			return -ENOMEM;
1351da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
1361da177e4SLinus Torvalds 	return 0;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	pgd_t *pgd;
1421da177e4SLinus Torvalds 	unsigned long next;
1431da177e4SLinus Torvalds 	unsigned long addr = (unsigned long) area->addr;
1441da177e4SLinus Torvalds 	unsigned long end = addr + area->size - PAGE_SIZE;
1451da177e4SLinus Torvalds 	int err;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1481da177e4SLinus Torvalds 	pgd = pgd_offset_k(addr);
1491da177e4SLinus Torvalds 	spin_lock(&init_mm.page_table_lock);
1501da177e4SLinus Torvalds 	do {
1511da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
1521da177e4SLinus Torvalds 		err = vmap_pud_range(pgd, addr, next, prot, pages);
1531da177e4SLinus Torvalds 		if (err)
1541da177e4SLinus Torvalds 			break;
1551da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
1561da177e4SLinus Torvalds 	spin_unlock(&init_mm.page_table_lock);
1571da177e4SLinus Torvalds 	flush_cache_vmap((unsigned long) area->addr, end);
1581da177e4SLinus Torvalds 	return err;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1621da177e4SLinus Torvalds 				unsigned long start, unsigned long end)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	struct vm_struct **p, *tmp, *area;
1651da177e4SLinus Torvalds 	unsigned long align = 1;
1661da177e4SLinus Torvalds 	unsigned long addr;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	if (flags & VM_IOREMAP) {
1691da177e4SLinus Torvalds 		int bit = fls(size);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 		if (bit > IOREMAP_MAX_ORDER)
1721da177e4SLinus Torvalds 			bit = IOREMAP_MAX_ORDER;
1731da177e4SLinus Torvalds 		else if (bit < PAGE_SHIFT)
1741da177e4SLinus Torvalds 			bit = PAGE_SHIFT;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 		align = 1ul << bit;
1771da177e4SLinus Torvalds 	}
1781da177e4SLinus Torvalds 	addr = ALIGN(start, align);
1791da177e4SLinus Torvalds 	size = PAGE_ALIGN(size);
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	area = kmalloc(sizeof(*area), GFP_KERNEL);
1821da177e4SLinus Torvalds 	if (unlikely(!area))
1831da177e4SLinus Torvalds 		return NULL;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	if (unlikely(!size)) {
1861da177e4SLinus Torvalds 		kfree (area);
1871da177e4SLinus Torvalds 		return NULL;
1881da177e4SLinus Torvalds 	}
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	/*
1911da177e4SLinus Torvalds 	 * We always allocate a guard page.
1921da177e4SLinus Torvalds 	 */
1931da177e4SLinus Torvalds 	size += PAGE_SIZE;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	write_lock(&vmlist_lock);
1961da177e4SLinus Torvalds 	for (p = &vmlist; (tmp = *p) != NULL ;p = &tmp->next) {
1971da177e4SLinus Torvalds 		if ((unsigned long)tmp->addr < addr) {
1981da177e4SLinus Torvalds 			if((unsigned long)tmp->addr + tmp->size >= addr)
1991da177e4SLinus Torvalds 				addr = ALIGN(tmp->size +
2001da177e4SLinus Torvalds 					     (unsigned long)tmp->addr, align);
2011da177e4SLinus Torvalds 			continue;
2021da177e4SLinus Torvalds 		}
2031da177e4SLinus Torvalds 		if ((size + addr) < addr)
2041da177e4SLinus Torvalds 			goto out;
2051da177e4SLinus Torvalds 		if (size + addr <= (unsigned long)tmp->addr)
2061da177e4SLinus Torvalds 			goto found;
2071da177e4SLinus Torvalds 		addr = ALIGN(tmp->size + (unsigned long)tmp->addr, align);
2081da177e4SLinus Torvalds 		if (addr > end - size)
2091da177e4SLinus Torvalds 			goto out;
2101da177e4SLinus Torvalds 	}
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds found:
2131da177e4SLinus Torvalds 	area->next = *p;
2141da177e4SLinus Torvalds 	*p = area;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	area->flags = flags;
2171da177e4SLinus Torvalds 	area->addr = (void *)addr;
2181da177e4SLinus Torvalds 	area->size = size;
2191da177e4SLinus Torvalds 	area->pages = NULL;
2201da177e4SLinus Torvalds 	area->nr_pages = 0;
2211da177e4SLinus Torvalds 	area->phys_addr = 0;
2221da177e4SLinus Torvalds 	write_unlock(&vmlist_lock);
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	return area;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds out:
2271da177e4SLinus Torvalds 	write_unlock(&vmlist_lock);
2281da177e4SLinus Torvalds 	kfree(area);
2291da177e4SLinus Torvalds 	if (printk_ratelimit())
2301da177e4SLinus Torvalds 		printk(KERN_WARNING "allocation failed: out of vmalloc space - use vmalloc=<size> to increase size.\n");
2311da177e4SLinus Torvalds 	return NULL;
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds /**
2351da177e4SLinus Torvalds  *	get_vm_area  -  reserve a contingous kernel virtual area
2361da177e4SLinus Torvalds  *
2371da177e4SLinus Torvalds  *	@size:		size of the area
2381da177e4SLinus Torvalds  *	@flags:		%VM_IOREMAP for I/O mappings or VM_ALLOC
2391da177e4SLinus Torvalds  *
2401da177e4SLinus Torvalds  *	Search an area of @size in the kernel virtual mapping area,
2411da177e4SLinus Torvalds  *	and reserved it for out purposes.  Returns the area descriptor
2421da177e4SLinus Torvalds  *	on success or %NULL on failure.
2431da177e4SLinus Torvalds  */
2441da177e4SLinus Torvalds struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	return __get_vm_area(size, flags, VMALLOC_START, VMALLOC_END);
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
2497856dfebSAndi Kleen /* Caller must hold vmlist_lock */
2507856dfebSAndi Kleen struct vm_struct *__remove_vm_area(void *addr)
2517856dfebSAndi Kleen {
2527856dfebSAndi Kleen 	struct vm_struct **p, *tmp;
2537856dfebSAndi Kleen 
2547856dfebSAndi Kleen 	for (p = &vmlist ; (tmp = *p) != NULL ;p = &tmp->next) {
2557856dfebSAndi Kleen 		 if (tmp->addr == addr)
2567856dfebSAndi Kleen 			 goto found;
2577856dfebSAndi Kleen 	}
2587856dfebSAndi Kleen 	return NULL;
2597856dfebSAndi Kleen 
2607856dfebSAndi Kleen found:
2617856dfebSAndi Kleen 	unmap_vm_area(tmp);
2627856dfebSAndi Kleen 	*p = tmp->next;
2637856dfebSAndi Kleen 
2647856dfebSAndi Kleen 	/*
2657856dfebSAndi Kleen 	 * Remove the guard page.
2667856dfebSAndi Kleen 	 */
2677856dfebSAndi Kleen 	tmp->size -= PAGE_SIZE;
2687856dfebSAndi Kleen 	return tmp;
2697856dfebSAndi Kleen }
2707856dfebSAndi Kleen 
2711da177e4SLinus Torvalds /**
2721da177e4SLinus Torvalds  *	remove_vm_area  -  find and remove a contingous kernel virtual area
2731da177e4SLinus Torvalds  *
2741da177e4SLinus Torvalds  *	@addr:		base address
2751da177e4SLinus Torvalds  *
2761da177e4SLinus Torvalds  *	Search for the kernel VM area starting at @addr, and remove it.
2771da177e4SLinus Torvalds  *	This function returns the found VM area, but using it is NOT safe
2787856dfebSAndi Kleen  *	on SMP machines, except for its size or flags.
2791da177e4SLinus Torvalds  */
2801da177e4SLinus Torvalds struct vm_struct *remove_vm_area(void *addr)
2811da177e4SLinus Torvalds {
2827856dfebSAndi Kleen 	struct vm_struct *v;
2831da177e4SLinus Torvalds 	write_lock(&vmlist_lock);
2847856dfebSAndi Kleen 	v = __remove_vm_area(addr);
2851da177e4SLinus Torvalds 	write_unlock(&vmlist_lock);
2867856dfebSAndi Kleen 	return v;
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds void __vunmap(void *addr, int deallocate_pages)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	struct vm_struct *area;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	if (!addr)
2941da177e4SLinus Torvalds 		return;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	if ((PAGE_SIZE-1) & (unsigned long)addr) {
2971da177e4SLinus Torvalds 		printk(KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
2981da177e4SLinus Torvalds 		WARN_ON(1);
2991da177e4SLinus Torvalds 		return;
3001da177e4SLinus Torvalds 	}
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	area = remove_vm_area(addr);
3031da177e4SLinus Torvalds 	if (unlikely(!area)) {
3041da177e4SLinus Torvalds 		printk(KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
3051da177e4SLinus Torvalds 				addr);
3061da177e4SLinus Torvalds 		WARN_ON(1);
3071da177e4SLinus Torvalds 		return;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	if (deallocate_pages) {
3111da177e4SLinus Torvalds 		int i;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 		for (i = 0; i < area->nr_pages; i++) {
3141da177e4SLinus Torvalds 			if (unlikely(!area->pages[i]))
3151da177e4SLinus Torvalds 				BUG();
3161da177e4SLinus Torvalds 			__free_page(area->pages[i]);
3171da177e4SLinus Torvalds 		}
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 		if (area->nr_pages > PAGE_SIZE/sizeof(struct page *))
3201da177e4SLinus Torvalds 			vfree(area->pages);
3211da177e4SLinus Torvalds 		else
3221da177e4SLinus Torvalds 			kfree(area->pages);
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	kfree(area);
3261da177e4SLinus Torvalds 	return;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds /**
3301da177e4SLinus Torvalds  *	vfree  -  release memory allocated by vmalloc()
3311da177e4SLinus Torvalds  *
3321da177e4SLinus Torvalds  *	@addr:		memory base address
3331da177e4SLinus Torvalds  *
3341da177e4SLinus Torvalds  *	Free the virtually contiguous memory area starting at @addr, as
335*80e93effSPekka Enberg  *	obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
336*80e93effSPekka Enberg  *	NULL, no operation is performed.
3371da177e4SLinus Torvalds  *
338*80e93effSPekka Enberg  *	Must not be called in interrupt context.
3391da177e4SLinus Torvalds  */
3401da177e4SLinus Torvalds void vfree(void *addr)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
3431da177e4SLinus Torvalds 	__vunmap(addr, 1);
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds EXPORT_SYMBOL(vfree);
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds /**
3491da177e4SLinus Torvalds  *	vunmap  -  release virtual mapping obtained by vmap()
3501da177e4SLinus Torvalds  *
3511da177e4SLinus Torvalds  *	@addr:		memory base address
3521da177e4SLinus Torvalds  *
3531da177e4SLinus Torvalds  *	Free the virtually contiguous memory area starting at @addr,
3541da177e4SLinus Torvalds  *	which was created from the page array passed to vmap().
3551da177e4SLinus Torvalds  *
356*80e93effSPekka Enberg  *	Must not be called in interrupt context.
3571da177e4SLinus Torvalds  */
3581da177e4SLinus Torvalds void vunmap(void *addr)
3591da177e4SLinus Torvalds {
3601da177e4SLinus Torvalds 	BUG_ON(in_interrupt());
3611da177e4SLinus Torvalds 	__vunmap(addr, 0);
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds EXPORT_SYMBOL(vunmap);
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds /**
3671da177e4SLinus Torvalds  *	vmap  -  map an array of pages into virtually contiguous space
3681da177e4SLinus Torvalds  *
3691da177e4SLinus Torvalds  *	@pages:		array of page pointers
3701da177e4SLinus Torvalds  *	@count:		number of pages to map
3711da177e4SLinus Torvalds  *	@flags:		vm_area->flags
3721da177e4SLinus Torvalds  *	@prot:		page protection for the mapping
3731da177e4SLinus Torvalds  *
3741da177e4SLinus Torvalds  *	Maps @count pages from @pages into contiguous kernel virtual
3751da177e4SLinus Torvalds  *	space.
3761da177e4SLinus Torvalds  */
3771da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count,
3781da177e4SLinus Torvalds 		unsigned long flags, pgprot_t prot)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	struct vm_struct *area;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	if (count > num_physpages)
3831da177e4SLinus Torvalds 		return NULL;
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	area = get_vm_area((count << PAGE_SHIFT), flags);
3861da177e4SLinus Torvalds 	if (!area)
3871da177e4SLinus Torvalds 		return NULL;
3881da177e4SLinus Torvalds 	if (map_vm_area(area, prot, &pages)) {
3891da177e4SLinus Torvalds 		vunmap(area->addr);
3901da177e4SLinus Torvalds 		return NULL;
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	return area->addr;
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds EXPORT_SYMBOL(vmap);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds void *__vmalloc_area(struct vm_struct *area, unsigned int __nocast gfp_mask, pgprot_t prot)
3991da177e4SLinus Torvalds {
4001da177e4SLinus Torvalds 	struct page **pages;
4011da177e4SLinus Torvalds 	unsigned int nr_pages, array_size, i;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
4041da177e4SLinus Torvalds 	array_size = (nr_pages * sizeof(struct page *));
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	area->nr_pages = nr_pages;
4071da177e4SLinus Torvalds 	/* Please note that the recursion is strictly bounded. */
4081da177e4SLinus Torvalds 	if (array_size > PAGE_SIZE)
4091da177e4SLinus Torvalds 		pages = __vmalloc(array_size, gfp_mask, PAGE_KERNEL);
4101da177e4SLinus Torvalds 	else
4111da177e4SLinus Torvalds 		pages = kmalloc(array_size, (gfp_mask & ~__GFP_HIGHMEM));
4121da177e4SLinus Torvalds 	area->pages = pages;
4131da177e4SLinus Torvalds 	if (!area->pages) {
4141da177e4SLinus Torvalds 		remove_vm_area(area->addr);
4151da177e4SLinus Torvalds 		kfree(area);
4161da177e4SLinus Torvalds 		return NULL;
4171da177e4SLinus Torvalds 	}
4181da177e4SLinus Torvalds 	memset(area->pages, 0, array_size);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	for (i = 0; i < area->nr_pages; i++) {
4211da177e4SLinus Torvalds 		area->pages[i] = alloc_page(gfp_mask);
4221da177e4SLinus Torvalds 		if (unlikely(!area->pages[i])) {
4231da177e4SLinus Torvalds 			/* Successfully allocated i pages, free them in __vunmap() */
4241da177e4SLinus Torvalds 			area->nr_pages = i;
4251da177e4SLinus Torvalds 			goto fail;
4261da177e4SLinus Torvalds 		}
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	if (map_vm_area(area, prot, &pages))
4301da177e4SLinus Torvalds 		goto fail;
4311da177e4SLinus Torvalds 	return area->addr;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds fail:
4341da177e4SLinus Torvalds 	vfree(area->addr);
4351da177e4SLinus Torvalds 	return NULL;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds /**
4391da177e4SLinus Torvalds  *	__vmalloc  -  allocate virtually contiguous memory
4401da177e4SLinus Torvalds  *
4411da177e4SLinus Torvalds  *	@size:		allocation size
4421da177e4SLinus Torvalds  *	@gfp_mask:	flags for the page level allocator
4431da177e4SLinus Torvalds  *	@prot:		protection mask for the allocated pages
4441da177e4SLinus Torvalds  *
4451da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
4461da177e4SLinus Torvalds  *	allocator with @gfp_mask flags.  Map them into contiguous
4471da177e4SLinus Torvalds  *	kernel virtual space, using a pagetable protection of @prot.
4481da177e4SLinus Torvalds  */
4491da177e4SLinus Torvalds void *__vmalloc(unsigned long size, unsigned int __nocast gfp_mask, pgprot_t prot)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	struct vm_struct *area;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	size = PAGE_ALIGN(size);
4541da177e4SLinus Torvalds 	if (!size || (size >> PAGE_SHIFT) > num_physpages)
4551da177e4SLinus Torvalds 		return NULL;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	area = get_vm_area(size, VM_ALLOC);
4581da177e4SLinus Torvalds 	if (!area)
4591da177e4SLinus Torvalds 		return NULL;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	return __vmalloc_area(area, gfp_mask, prot);
4621da177e4SLinus Torvalds }
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds EXPORT_SYMBOL(__vmalloc);
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds /**
4671da177e4SLinus Torvalds  *	vmalloc  -  allocate virtually contiguous memory
4681da177e4SLinus Torvalds  *
4691da177e4SLinus Torvalds  *	@size:		allocation size
4701da177e4SLinus Torvalds  *
4711da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
4721da177e4SLinus Torvalds  *	allocator and map them into contiguous kernel virtual space.
4731da177e4SLinus Torvalds  *
4741da177e4SLinus Torvalds  *	For tight cotrol over page level allocator and protection flags
4751da177e4SLinus Torvalds  *	use __vmalloc() instead.
4761da177e4SLinus Torvalds  */
4771da177e4SLinus Torvalds void *vmalloc(unsigned long size)
4781da177e4SLinus Torvalds {
4791da177e4SLinus Torvalds        return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc);
4831da177e4SLinus Torvalds 
4844dc3b16bSPavel Pisa #ifndef PAGE_KERNEL_EXEC
4854dc3b16bSPavel Pisa # define PAGE_KERNEL_EXEC PAGE_KERNEL
4864dc3b16bSPavel Pisa #endif
4874dc3b16bSPavel Pisa 
4881da177e4SLinus Torvalds /**
4891da177e4SLinus Torvalds  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
4901da177e4SLinus Torvalds  *
4911da177e4SLinus Torvalds  *	@size:		allocation size
4921da177e4SLinus Torvalds  *
4931da177e4SLinus Torvalds  *	Kernel-internal function to allocate enough pages to cover @size
4941da177e4SLinus Torvalds  *	the page level allocator and map them into contiguous and
4951da177e4SLinus Torvalds  *	executable kernel virtual space.
4961da177e4SLinus Torvalds  *
4971da177e4SLinus Torvalds  *	For tight cotrol over page level allocator and protection flags
4981da177e4SLinus Torvalds  *	use __vmalloc() instead.
4991da177e4SLinus Torvalds  */
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds void *vmalloc_exec(unsigned long size)
5021da177e4SLinus Torvalds {
5031da177e4SLinus Torvalds 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds /**
5071da177e4SLinus Torvalds  *	vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
5081da177e4SLinus Torvalds  *
5091da177e4SLinus Torvalds  *	@size:		allocation size
5101da177e4SLinus Torvalds  *
5111da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
5121da177e4SLinus Torvalds  *	page level allocator and map them into contiguous kernel virtual space.
5131da177e4SLinus Torvalds  */
5141da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
5151da177e4SLinus Torvalds {
5161da177e4SLinus Torvalds 	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds EXPORT_SYMBOL(vmalloc_32);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	struct vm_struct *tmp;
5241da177e4SLinus Torvalds 	char *vaddr, *buf_start = buf;
5251da177e4SLinus Torvalds 	unsigned long n;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	/* Don't allow overflow */
5281da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
5291da177e4SLinus Torvalds 		count = -(unsigned long) addr;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	read_lock(&vmlist_lock);
5321da177e4SLinus Torvalds 	for (tmp = vmlist; tmp; tmp = tmp->next) {
5331da177e4SLinus Torvalds 		vaddr = (char *) tmp->addr;
5341da177e4SLinus Torvalds 		if (addr >= vaddr + tmp->size - PAGE_SIZE)
5351da177e4SLinus Torvalds 			continue;
5361da177e4SLinus Torvalds 		while (addr < vaddr) {
5371da177e4SLinus Torvalds 			if (count == 0)
5381da177e4SLinus Torvalds 				goto finished;
5391da177e4SLinus Torvalds 			*buf = '\0';
5401da177e4SLinus Torvalds 			buf++;
5411da177e4SLinus Torvalds 			addr++;
5421da177e4SLinus Torvalds 			count--;
5431da177e4SLinus Torvalds 		}
5441da177e4SLinus Torvalds 		n = vaddr + tmp->size - PAGE_SIZE - addr;
5451da177e4SLinus Torvalds 		do {
5461da177e4SLinus Torvalds 			if (count == 0)
5471da177e4SLinus Torvalds 				goto finished;
5481da177e4SLinus Torvalds 			*buf = *addr;
5491da177e4SLinus Torvalds 			buf++;
5501da177e4SLinus Torvalds 			addr++;
5511da177e4SLinus Torvalds 			count--;
5521da177e4SLinus Torvalds 		} while (--n > 0);
5531da177e4SLinus Torvalds 	}
5541da177e4SLinus Torvalds finished:
5551da177e4SLinus Torvalds 	read_unlock(&vmlist_lock);
5561da177e4SLinus Torvalds 	return buf - buf_start;
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
5601da177e4SLinus Torvalds {
5611da177e4SLinus Torvalds 	struct vm_struct *tmp;
5621da177e4SLinus Torvalds 	char *vaddr, *buf_start = buf;
5631da177e4SLinus Torvalds 	unsigned long n;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	/* Don't allow overflow */
5661da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
5671da177e4SLinus Torvalds 		count = -(unsigned long) addr;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	read_lock(&vmlist_lock);
5701da177e4SLinus Torvalds 	for (tmp = vmlist; tmp; tmp = tmp->next) {
5711da177e4SLinus Torvalds 		vaddr = (char *) tmp->addr;
5721da177e4SLinus Torvalds 		if (addr >= vaddr + tmp->size - PAGE_SIZE)
5731da177e4SLinus Torvalds 			continue;
5741da177e4SLinus Torvalds 		while (addr < vaddr) {
5751da177e4SLinus Torvalds 			if (count == 0)
5761da177e4SLinus Torvalds 				goto finished;
5771da177e4SLinus Torvalds 			buf++;
5781da177e4SLinus Torvalds 			addr++;
5791da177e4SLinus Torvalds 			count--;
5801da177e4SLinus Torvalds 		}
5811da177e4SLinus Torvalds 		n = vaddr + tmp->size - PAGE_SIZE - addr;
5821da177e4SLinus Torvalds 		do {
5831da177e4SLinus Torvalds 			if (count == 0)
5841da177e4SLinus Torvalds 				goto finished;
5851da177e4SLinus Torvalds 			*addr = *buf;
5861da177e4SLinus Torvalds 			buf++;
5871da177e4SLinus Torvalds 			addr++;
5881da177e4SLinus Torvalds 			count--;
5891da177e4SLinus Torvalds 		} while (--n > 0);
5901da177e4SLinus Torvalds 	}
5911da177e4SLinus Torvalds finished:
5921da177e4SLinus Torvalds 	read_unlock(&vmlist_lock);
5931da177e4SLinus Torvalds 	return buf - buf_start;
5941da177e4SLinus Torvalds }
595