vmalloc.c 20 KB

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