vmalloc.c 22 KB

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