vmalloc.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /*
  2. * linux/mm/vmalloc.c
  3. *
  4. * Copyright (C) 1993 Linus Torvalds
  5. * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
  6. * SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000
  7. * Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002
  8. * Numa awareness, Christoph Lameter, SGI, June 2005
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/module.h>
  12. #include <linux/highmem.h>
  13. #include <linux/slab.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/debugobjects.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/kallsyms.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/tlbflush.h>
  22. DEFINE_RWLOCK(vmlist_lock);
  23. struct vm_struct *vmlist;
  24. static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  25. int node, void *caller);
  26. static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
  27. {
  28. pte_t *pte;
  29. pte = pte_offset_kernel(pmd, addr);
  30. do {
  31. pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
  32. WARN_ON(!pte_none(ptent) && !pte_present(ptent));
  33. } while (pte++, addr += PAGE_SIZE, addr != end);
  34. }
  35. static inline void vunmap_pmd_range(pud_t *pud, unsigned long addr,
  36. unsigned long end)
  37. {
  38. pmd_t *pmd;
  39. unsigned long next;
  40. pmd = pmd_offset(pud, addr);
  41. do {
  42. next = pmd_addr_end(addr, end);
  43. if (pmd_none_or_clear_bad(pmd))
  44. continue;
  45. vunmap_pte_range(pmd, addr, next);
  46. } while (pmd++, addr = next, addr != end);
  47. }
  48. static inline void vunmap_pud_range(pgd_t *pgd, unsigned long addr,
  49. unsigned long end)
  50. {
  51. pud_t *pud;
  52. unsigned long next;
  53. pud = pud_offset(pgd, addr);
  54. do {
  55. next = pud_addr_end(addr, end);
  56. if (pud_none_or_clear_bad(pud))
  57. continue;
  58. vunmap_pmd_range(pud, addr, next);
  59. } while (pud++, addr = next, addr != end);
  60. }
  61. void unmap_kernel_range(unsigned long addr, unsigned long size)
  62. {
  63. pgd_t *pgd;
  64. unsigned long next;
  65. unsigned long start = addr;
  66. unsigned long end = addr + size;
  67. BUG_ON(addr >= end);
  68. pgd = pgd_offset_k(addr);
  69. flush_cache_vunmap(addr, end);
  70. do {
  71. next = pgd_addr_end(addr, end);
  72. if (pgd_none_or_clear_bad(pgd))
  73. continue;
  74. vunmap_pud_range(pgd, addr, next);
  75. } while (pgd++, addr = next, addr != end);
  76. flush_tlb_kernel_range(start, end);
  77. }
  78. static void unmap_vm_area(struct vm_struct *area)
  79. {
  80. unmap_kernel_range((unsigned long)area->addr, area->size);
  81. }
  82. static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
  83. unsigned long end, pgprot_t prot, struct page ***pages)
  84. {
  85. pte_t *pte;
  86. pte = pte_alloc_kernel(pmd, addr);
  87. if (!pte)
  88. return -ENOMEM;
  89. do {
  90. struct page *page = **pages;
  91. WARN_ON(!pte_none(*pte));
  92. if (!page)
  93. return -ENOMEM;
  94. set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
  95. (*pages)++;
  96. } while (pte++, addr += PAGE_SIZE, addr != end);
  97. return 0;
  98. }
  99. static inline int vmap_pmd_range(pud_t *pud, unsigned long addr,
  100. unsigned long end, pgprot_t prot, struct page ***pages)
  101. {
  102. pmd_t *pmd;
  103. unsigned long next;
  104. pmd = pmd_alloc(&init_mm, pud, addr);
  105. if (!pmd)
  106. return -ENOMEM;
  107. do {
  108. next = pmd_addr_end(addr, end);
  109. if (vmap_pte_range(pmd, addr, next, prot, pages))
  110. return -ENOMEM;
  111. } while (pmd++, addr = next, addr != end);
  112. return 0;
  113. }
  114. static inline int vmap_pud_range(pgd_t *pgd, unsigned long addr,
  115. unsigned long end, pgprot_t prot, struct page ***pages)
  116. {
  117. pud_t *pud;
  118. unsigned long next;
  119. pud = pud_alloc(&init_mm, pgd, addr);
  120. if (!pud)
  121. return -ENOMEM;
  122. do {
  123. next = pud_addr_end(addr, end);
  124. if (vmap_pmd_range(pud, addr, next, prot, pages))
  125. return -ENOMEM;
  126. } while (pud++, addr = next, addr != end);
  127. return 0;
  128. }
  129. int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
  130. {
  131. pgd_t *pgd;
  132. unsigned long next;
  133. unsigned long addr = (unsigned long) area->addr;
  134. unsigned long end = addr + area->size - PAGE_SIZE;
  135. int err;
  136. BUG_ON(addr >= end);
  137. pgd = pgd_offset_k(addr);
  138. do {
  139. next = pgd_addr_end(addr, end);
  140. err = vmap_pud_range(pgd, addr, next, prot, pages);
  141. if (err)
  142. break;
  143. } while (pgd++, addr = next, addr != end);
  144. flush_cache_vmap((unsigned long) area->addr, end);
  145. return err;
  146. }
  147. EXPORT_SYMBOL_GPL(map_vm_area);
  148. /*
  149. * Map a vmalloc()-space virtual address to the physical page.
  150. */
  151. struct page *vmalloc_to_page(const void *vmalloc_addr)
  152. {
  153. unsigned long addr = (unsigned long) vmalloc_addr;
  154. struct page *page = NULL;
  155. pgd_t *pgd = pgd_offset_k(addr);
  156. pud_t *pud;
  157. pmd_t *pmd;
  158. pte_t *ptep, pte;
  159. /*
  160. * XXX we might need to change this if we add VIRTUAL_BUG_ON for
  161. * architectures that do not vmalloc module space
  162. */
  163. VIRTUAL_BUG_ON(!is_vmalloc_addr(vmalloc_addr) &&
  164. !is_module_address(addr));
  165. if (!pgd_none(*pgd)) {
  166. pud = pud_offset(pgd, addr);
  167. if (!pud_none(*pud)) {
  168. pmd = pmd_offset(pud, addr);
  169. if (!pmd_none(*pmd)) {
  170. ptep = pte_offset_map(pmd, addr);
  171. pte = *ptep;
  172. if (pte_present(pte))
  173. page = pte_page(pte);
  174. pte_unmap(ptep);
  175. }
  176. }
  177. }
  178. return page;
  179. }
  180. EXPORT_SYMBOL(vmalloc_to_page);
  181. /*
  182. * Map a vmalloc()-space virtual address to the physical page frame number.
  183. */
  184. unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
  185. {
  186. return page_to_pfn(vmalloc_to_page(vmalloc_addr));
  187. }
  188. EXPORT_SYMBOL(vmalloc_to_pfn);
  189. static struct vm_struct *
  190. __get_vm_area_node(unsigned long size, unsigned long flags, unsigned long start,
  191. unsigned long end, int node, gfp_t gfp_mask, void *caller)
  192. {
  193. struct vm_struct **p, *tmp, *area;
  194. unsigned long align = 1;
  195. unsigned long addr;
  196. BUG_ON(in_interrupt());
  197. if (flags & VM_IOREMAP) {
  198. int bit = fls(size);
  199. if (bit > IOREMAP_MAX_ORDER)
  200. bit = IOREMAP_MAX_ORDER;
  201. else if (bit < PAGE_SHIFT)
  202. bit = PAGE_SHIFT;
  203. align = 1ul << bit;
  204. }
  205. addr = ALIGN(start, align);
  206. size = PAGE_ALIGN(size);
  207. if (unlikely(!size))
  208. return NULL;
  209. area = kmalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
  210. if (unlikely(!area))
  211. return NULL;
  212. /*
  213. * We always allocate a guard page.
  214. */
  215. size += PAGE_SIZE;
  216. write_lock(&vmlist_lock);
  217. for (p = &vmlist; (tmp = *p) != NULL ;p = &tmp->next) {
  218. if ((unsigned long)tmp->addr < addr) {
  219. if((unsigned long)tmp->addr + tmp->size >= addr)
  220. addr = ALIGN(tmp->size +
  221. (unsigned long)tmp->addr, align);
  222. continue;
  223. }
  224. if ((size + addr) < addr)
  225. goto out;
  226. if (size + addr <= (unsigned long)tmp->addr)
  227. goto found;
  228. addr = ALIGN(tmp->size + (unsigned long)tmp->addr, align);
  229. if (addr > end - size)
  230. goto out;
  231. }
  232. if ((size + addr) < addr)
  233. goto out;
  234. if (addr > end - size)
  235. goto out;
  236. found:
  237. area->next = *p;
  238. *p = area;
  239. area->flags = flags;
  240. area->addr = (void *)addr;
  241. area->size = size;
  242. area->pages = NULL;
  243. area->nr_pages = 0;
  244. area->phys_addr = 0;
  245. area->caller = caller;
  246. write_unlock(&vmlist_lock);
  247. return area;
  248. out:
  249. write_unlock(&vmlist_lock);
  250. kfree(area);
  251. if (printk_ratelimit())
  252. printk(KERN_WARNING "allocation failed: out of vmalloc space - use vmalloc=<size> to increase size.\n");
  253. return NULL;
  254. }
  255. struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
  256. unsigned long start, unsigned long end)
  257. {
  258. return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
  259. __builtin_return_address(0));
  260. }
  261. EXPORT_SYMBOL_GPL(__get_vm_area);
  262. /**
  263. * get_vm_area - reserve a contiguous kernel virtual area
  264. * @size: size of the area
  265. * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
  266. *
  267. * Search an area of @size in the kernel virtual mapping area,
  268. * and reserved it for out purposes. Returns the area descriptor
  269. * on success or %NULL on failure.
  270. */
  271. struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
  272. {
  273. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
  274. -1, GFP_KERNEL, __builtin_return_address(0));
  275. }
  276. struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
  277. void *caller)
  278. {
  279. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
  280. -1, GFP_KERNEL, caller);
  281. }
  282. struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
  283. int node, gfp_t gfp_mask)
  284. {
  285. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
  286. gfp_mask, __builtin_return_address(0));
  287. }
  288. /* Caller must hold vmlist_lock */
  289. static struct vm_struct *__find_vm_area(const void *addr)
  290. {
  291. struct vm_struct *tmp;
  292. for (tmp = vmlist; tmp != NULL; tmp = tmp->next) {
  293. if (tmp->addr == addr)
  294. break;
  295. }
  296. return tmp;
  297. }
  298. /* Caller must hold vmlist_lock */
  299. static struct vm_struct *__remove_vm_area(const void *addr)
  300. {
  301. struct vm_struct **p, *tmp;
  302. for (p = &vmlist ; (tmp = *p) != NULL ;p = &tmp->next) {
  303. if (tmp->addr == addr)
  304. goto found;
  305. }
  306. return NULL;
  307. found:
  308. unmap_vm_area(tmp);
  309. *p = tmp->next;
  310. /*
  311. * Remove the guard page.
  312. */
  313. tmp->size -= PAGE_SIZE;
  314. return tmp;
  315. }
  316. /**
  317. * remove_vm_area - find and remove a continuous kernel virtual area
  318. * @addr: base address
  319. *
  320. * Search for the kernel VM area starting at @addr, and remove it.
  321. * This function returns the found VM area, but using it is NOT safe
  322. * on SMP machines, except for its size or flags.
  323. */
  324. struct vm_struct *remove_vm_area(const void *addr)
  325. {
  326. struct vm_struct *v;
  327. write_lock(&vmlist_lock);
  328. v = __remove_vm_area(addr);
  329. write_unlock(&vmlist_lock);
  330. return v;
  331. }
  332. static void __vunmap(const void *addr, int deallocate_pages)
  333. {
  334. struct vm_struct *area;
  335. if (!addr)
  336. return;
  337. if ((PAGE_SIZE-1) & (unsigned long)addr) {
  338. WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
  339. return;
  340. }
  341. area = remove_vm_area(addr);
  342. if (unlikely(!area)) {
  343. WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
  344. addr);
  345. return;
  346. }
  347. debug_check_no_locks_freed(addr, area->size);
  348. debug_check_no_obj_freed(addr, area->size);
  349. if (deallocate_pages) {
  350. int i;
  351. for (i = 0; i < area->nr_pages; i++) {
  352. struct page *page = area->pages[i];
  353. BUG_ON(!page);
  354. __free_page(page);
  355. }
  356. if (area->flags & VM_VPAGES)
  357. vfree(area->pages);
  358. else
  359. kfree(area->pages);
  360. }
  361. kfree(area);
  362. return;
  363. }
  364. /**
  365. * vfree - release memory allocated by vmalloc()
  366. * @addr: memory base address
  367. *
  368. * Free the virtually continuous memory area starting at @addr, as
  369. * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
  370. * NULL, no operation is performed.
  371. *
  372. * Must not be called in interrupt context.
  373. */
  374. void vfree(const void *addr)
  375. {
  376. BUG_ON(in_interrupt());
  377. __vunmap(addr, 1);
  378. }
  379. EXPORT_SYMBOL(vfree);
  380. /**
  381. * vunmap - release virtual mapping obtained by vmap()
  382. * @addr: memory base address
  383. *
  384. * Free the virtually contiguous memory area starting at @addr,
  385. * which was created from the page array passed to vmap().
  386. *
  387. * Must not be called in interrupt context.
  388. */
  389. void vunmap(const void *addr)
  390. {
  391. BUG_ON(in_interrupt());
  392. __vunmap(addr, 0);
  393. }
  394. EXPORT_SYMBOL(vunmap);
  395. /**
  396. * vmap - map an array of pages into virtually contiguous space
  397. * @pages: array of page pointers
  398. * @count: number of pages to map
  399. * @flags: vm_area->flags
  400. * @prot: page protection for the mapping
  401. *
  402. * Maps @count pages from @pages into contiguous kernel virtual
  403. * space.
  404. */
  405. void *vmap(struct page **pages, unsigned int count,
  406. unsigned long flags, pgprot_t prot)
  407. {
  408. struct vm_struct *area;
  409. if (count > num_physpages)
  410. return NULL;
  411. area = get_vm_area_caller((count << PAGE_SHIFT), flags,
  412. __builtin_return_address(0));
  413. if (!area)
  414. return NULL;
  415. if (map_vm_area(area, prot, &pages)) {
  416. vunmap(area->addr);
  417. return NULL;
  418. }
  419. return area->addr;
  420. }
  421. EXPORT_SYMBOL(vmap);
  422. static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  423. pgprot_t prot, int node, void *caller)
  424. {
  425. struct page **pages;
  426. unsigned int nr_pages, array_size, i;
  427. nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
  428. array_size = (nr_pages * sizeof(struct page *));
  429. area->nr_pages = nr_pages;
  430. /* Please note that the recursion is strictly bounded. */
  431. if (array_size > PAGE_SIZE) {
  432. pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
  433. PAGE_KERNEL, node, caller);
  434. area->flags |= VM_VPAGES;
  435. } else {
  436. pages = kmalloc_node(array_size,
  437. (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
  438. node);
  439. }
  440. area->pages = pages;
  441. area->caller = caller;
  442. if (!area->pages) {
  443. remove_vm_area(area->addr);
  444. kfree(area);
  445. return NULL;
  446. }
  447. for (i = 0; i < area->nr_pages; i++) {
  448. struct page *page;
  449. if (node < 0)
  450. page = alloc_page(gfp_mask);
  451. else
  452. page = alloc_pages_node(node, gfp_mask, 0);
  453. if (unlikely(!page)) {
  454. /* Successfully allocated i pages, free them in __vunmap() */
  455. area->nr_pages = i;
  456. goto fail;
  457. }
  458. area->pages[i] = page;
  459. }
  460. if (map_vm_area(area, prot, &pages))
  461. goto fail;
  462. return area->addr;
  463. fail:
  464. vfree(area->addr);
  465. return NULL;
  466. }
  467. void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
  468. {
  469. return __vmalloc_area_node(area, gfp_mask, prot, -1,
  470. __builtin_return_address(0));
  471. }
  472. /**
  473. * __vmalloc_node - allocate virtually contiguous memory
  474. * @size: allocation size
  475. * @gfp_mask: flags for the page level allocator
  476. * @prot: protection mask for the allocated pages
  477. * @node: node to use for allocation or -1
  478. * @caller: caller's return address
  479. *
  480. * Allocate enough pages to cover @size from the page level
  481. * allocator with @gfp_mask flags. Map them into contiguous
  482. * kernel virtual space, using a pagetable protection of @prot.
  483. */
  484. static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  485. int node, void *caller)
  486. {
  487. struct vm_struct *area;
  488. size = PAGE_ALIGN(size);
  489. if (!size || (size >> PAGE_SHIFT) > num_physpages)
  490. return NULL;
  491. area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
  492. node, gfp_mask, caller);
  493. if (!area)
  494. return NULL;
  495. return __vmalloc_area_node(area, gfp_mask, prot, node, caller);
  496. }
  497. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  498. {
  499. return __vmalloc_node(size, gfp_mask, prot, -1,
  500. __builtin_return_address(0));
  501. }
  502. EXPORT_SYMBOL(__vmalloc);
  503. /**
  504. * vmalloc - allocate virtually contiguous memory
  505. * @size: allocation size
  506. * Allocate enough pages to cover @size from the page level
  507. * allocator and map them into contiguous kernel virtual space.
  508. *
  509. * For tight control over page level allocator and protection flags
  510. * use __vmalloc() instead.
  511. */
  512. void *vmalloc(unsigned long size)
  513. {
  514. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
  515. -1, __builtin_return_address(0));
  516. }
  517. EXPORT_SYMBOL(vmalloc);
  518. /**
  519. * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
  520. * @size: allocation size
  521. *
  522. * The resulting memory area is zeroed so it can be mapped to userspace
  523. * without leaking data.
  524. */
  525. void *vmalloc_user(unsigned long size)
  526. {
  527. struct vm_struct *area;
  528. void *ret;
  529. ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
  530. if (ret) {
  531. write_lock(&vmlist_lock);
  532. area = __find_vm_area(ret);
  533. area->flags |= VM_USERMAP;
  534. write_unlock(&vmlist_lock);
  535. }
  536. return ret;
  537. }
  538. EXPORT_SYMBOL(vmalloc_user);
  539. /**
  540. * vmalloc_node - allocate memory on a specific node
  541. * @size: allocation size
  542. * @node: numa node
  543. *
  544. * Allocate enough pages to cover @size from the page level
  545. * allocator and map them into contiguous kernel virtual space.
  546. *
  547. * For tight control over page level allocator and protection flags
  548. * use __vmalloc() instead.
  549. */
  550. void *vmalloc_node(unsigned long size, int node)
  551. {
  552. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
  553. node, __builtin_return_address(0));
  554. }
  555. EXPORT_SYMBOL(vmalloc_node);
  556. #ifndef PAGE_KERNEL_EXEC
  557. # define PAGE_KERNEL_EXEC PAGE_KERNEL
  558. #endif
  559. /**
  560. * vmalloc_exec - allocate virtually contiguous, executable memory
  561. * @size: allocation size
  562. *
  563. * Kernel-internal function to allocate enough pages to cover @size
  564. * the page level allocator and map them into contiguous and
  565. * executable kernel virtual space.
  566. *
  567. * For tight control over page level allocator and protection flags
  568. * use __vmalloc() instead.
  569. */
  570. void *vmalloc_exec(unsigned long size)
  571. {
  572. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
  573. }
  574. #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
  575. #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
  576. #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
  577. #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
  578. #else
  579. #define GFP_VMALLOC32 GFP_KERNEL
  580. #endif
  581. /**
  582. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  583. * @size: allocation size
  584. *
  585. * Allocate enough 32bit PA addressable pages to cover @size from the
  586. * page level allocator and map them into contiguous kernel virtual space.
  587. */
  588. void *vmalloc_32(unsigned long size)
  589. {
  590. return __vmalloc(size, GFP_VMALLOC32, PAGE_KERNEL);
  591. }
  592. EXPORT_SYMBOL(vmalloc_32);
  593. /**
  594. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  595. * @size: allocation size
  596. *
  597. * The resulting memory area is 32bit addressable and zeroed so it can be
  598. * mapped to userspace without leaking data.
  599. */
  600. void *vmalloc_32_user(unsigned long size)
  601. {
  602. struct vm_struct *area;
  603. void *ret;
  604. ret = __vmalloc(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL);
  605. if (ret) {
  606. write_lock(&vmlist_lock);
  607. area = __find_vm_area(ret);
  608. area->flags |= VM_USERMAP;
  609. write_unlock(&vmlist_lock);
  610. }
  611. return ret;
  612. }
  613. EXPORT_SYMBOL(vmalloc_32_user);
  614. long vread(char *buf, char *addr, unsigned long count)
  615. {
  616. struct vm_struct *tmp;
  617. char *vaddr, *buf_start = buf;
  618. unsigned long n;
  619. /* Don't allow overflow */
  620. if ((unsigned long) addr + count < count)
  621. count = -(unsigned long) addr;
  622. read_lock(&vmlist_lock);
  623. for (tmp = vmlist; tmp; tmp = tmp->next) {
  624. vaddr = (char *) tmp->addr;
  625. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  626. continue;
  627. while (addr < vaddr) {
  628. if (count == 0)
  629. goto finished;
  630. *buf = '\0';
  631. buf++;
  632. addr++;
  633. count--;
  634. }
  635. n = vaddr + tmp->size - PAGE_SIZE - addr;
  636. do {
  637. if (count == 0)
  638. goto finished;
  639. *buf = *addr;
  640. buf++;
  641. addr++;
  642. count--;
  643. } while (--n > 0);
  644. }
  645. finished:
  646. read_unlock(&vmlist_lock);
  647. return buf - buf_start;
  648. }
  649. long vwrite(char *buf, char *addr, unsigned long count)
  650. {
  651. struct vm_struct *tmp;
  652. char *vaddr, *buf_start = buf;
  653. unsigned long n;
  654. /* Don't allow overflow */
  655. if ((unsigned long) addr + count < count)
  656. count = -(unsigned long) addr;
  657. read_lock(&vmlist_lock);
  658. for (tmp = vmlist; tmp; tmp = tmp->next) {
  659. vaddr = (char *) tmp->addr;
  660. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  661. continue;
  662. while (addr < vaddr) {
  663. if (count == 0)
  664. goto finished;
  665. buf++;
  666. addr++;
  667. count--;
  668. }
  669. n = vaddr + tmp->size - PAGE_SIZE - addr;
  670. do {
  671. if (count == 0)
  672. goto finished;
  673. *addr = *buf;
  674. buf++;
  675. addr++;
  676. count--;
  677. } while (--n > 0);
  678. }
  679. finished:
  680. read_unlock(&vmlist_lock);
  681. return buf - buf_start;
  682. }
  683. /**
  684. * remap_vmalloc_range - map vmalloc pages to userspace
  685. * @vma: vma to cover (map full range of vma)
  686. * @addr: vmalloc memory
  687. * @pgoff: number of pages into addr before first page to map
  688. *
  689. * Returns: 0 for success, -Exxx on failure
  690. *
  691. * This function checks that addr is a valid vmalloc'ed area, and
  692. * that it is big enough to cover the vma. Will return failure if
  693. * that criteria isn't met.
  694. *
  695. * Similar to remap_pfn_range() (see mm/memory.c)
  696. */
  697. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  698. unsigned long pgoff)
  699. {
  700. struct vm_struct *area;
  701. unsigned long uaddr = vma->vm_start;
  702. unsigned long usize = vma->vm_end - vma->vm_start;
  703. int ret;
  704. if ((PAGE_SIZE-1) & (unsigned long)addr)
  705. return -EINVAL;
  706. read_lock(&vmlist_lock);
  707. area = __find_vm_area(addr);
  708. if (!area)
  709. goto out_einval_locked;
  710. if (!(area->flags & VM_USERMAP))
  711. goto out_einval_locked;
  712. if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
  713. goto out_einval_locked;
  714. read_unlock(&vmlist_lock);
  715. addr += pgoff << PAGE_SHIFT;
  716. do {
  717. struct page *page = vmalloc_to_page(addr);
  718. ret = vm_insert_page(vma, uaddr, page);
  719. if (ret)
  720. return ret;
  721. uaddr += PAGE_SIZE;
  722. addr += PAGE_SIZE;
  723. usize -= PAGE_SIZE;
  724. } while (usize > 0);
  725. /* Prevent "things" like memory migration? VM_flags need a cleanup... */
  726. vma->vm_flags |= VM_RESERVED;
  727. return ret;
  728. out_einval_locked:
  729. read_unlock(&vmlist_lock);
  730. return -EINVAL;
  731. }
  732. EXPORT_SYMBOL(remap_vmalloc_range);
  733. /*
  734. * Implement a stub for vmalloc_sync_all() if the architecture chose not to
  735. * have one.
  736. */
  737. void __attribute__((weak)) vmalloc_sync_all(void)
  738. {
  739. }
  740. static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
  741. {
  742. /* apply_to_page_range() does all the hard work. */
  743. return 0;
  744. }
  745. /**
  746. * alloc_vm_area - allocate a range of kernel address space
  747. * @size: size of the area
  748. *
  749. * Returns: NULL on failure, vm_struct on success
  750. *
  751. * This function reserves a range of kernel address space, and
  752. * allocates pagetables to map that range. No actual mappings
  753. * are created. If the kernel address space is not shared
  754. * between processes, it syncs the pagetable across all
  755. * processes.
  756. */
  757. struct vm_struct *alloc_vm_area(size_t size)
  758. {
  759. struct vm_struct *area;
  760. area = get_vm_area_caller(size, VM_IOREMAP,
  761. __builtin_return_address(0));
  762. if (area == NULL)
  763. return NULL;
  764. /*
  765. * This ensures that page tables are constructed for this region
  766. * of kernel virtual address space and mapped into init_mm.
  767. */
  768. if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
  769. area->size, f, NULL)) {
  770. free_vm_area(area);
  771. return NULL;
  772. }
  773. /* Make sure the pagetables are constructed in process kernel
  774. mappings */
  775. vmalloc_sync_all();
  776. return area;
  777. }
  778. EXPORT_SYMBOL_GPL(alloc_vm_area);
  779. void free_vm_area(struct vm_struct *area)
  780. {
  781. struct vm_struct *ret;
  782. ret = remove_vm_area(area->addr);
  783. BUG_ON(ret != area);
  784. kfree(area);
  785. }
  786. EXPORT_SYMBOL_GPL(free_vm_area);
  787. #ifdef CONFIG_PROC_FS
  788. static void *s_start(struct seq_file *m, loff_t *pos)
  789. {
  790. loff_t n = *pos;
  791. struct vm_struct *v;
  792. read_lock(&vmlist_lock);
  793. v = vmlist;
  794. while (n > 0 && v) {
  795. n--;
  796. v = v->next;
  797. }
  798. if (!n)
  799. return v;
  800. return NULL;
  801. }
  802. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  803. {
  804. struct vm_struct *v = p;
  805. ++*pos;
  806. return v->next;
  807. }
  808. static void s_stop(struct seq_file *m, void *p)
  809. {
  810. read_unlock(&vmlist_lock);
  811. }
  812. static void show_numa_info(struct seq_file *m, struct vm_struct *v)
  813. {
  814. if (NUMA_BUILD) {
  815. unsigned int nr, *counters = m->private;
  816. if (!counters)
  817. return;
  818. memset(counters, 0, nr_node_ids * sizeof(unsigned int));
  819. for (nr = 0; nr < v->nr_pages; nr++)
  820. counters[page_to_nid(v->pages[nr])]++;
  821. for_each_node_state(nr, N_HIGH_MEMORY)
  822. if (counters[nr])
  823. seq_printf(m, " N%u=%u", nr, counters[nr]);
  824. }
  825. }
  826. static int s_show(struct seq_file *m, void *p)
  827. {
  828. struct vm_struct *v = p;
  829. seq_printf(m, "0x%p-0x%p %7ld",
  830. v->addr, v->addr + v->size, v->size);
  831. if (v->caller) {
  832. char buff[2 * KSYM_NAME_LEN];
  833. seq_putc(m, ' ');
  834. sprint_symbol(buff, (unsigned long)v->caller);
  835. seq_puts(m, buff);
  836. }
  837. if (v->nr_pages)
  838. seq_printf(m, " pages=%d", v->nr_pages);
  839. if (v->phys_addr)
  840. seq_printf(m, " phys=%lx", v->phys_addr);
  841. if (v->flags & VM_IOREMAP)
  842. seq_printf(m, " ioremap");
  843. if (v->flags & VM_ALLOC)
  844. seq_printf(m, " vmalloc");
  845. if (v->flags & VM_MAP)
  846. seq_printf(m, " vmap");
  847. if (v->flags & VM_USERMAP)
  848. seq_printf(m, " user");
  849. if (v->flags & VM_VPAGES)
  850. seq_printf(m, " vpages");
  851. show_numa_info(m, v);
  852. seq_putc(m, '\n');
  853. return 0;
  854. }
  855. const struct seq_operations vmalloc_op = {
  856. .start = s_start,
  857. .next = s_next,
  858. .stop = s_stop,
  859. .show = s_show,
  860. };
  861. #endif