vmalloc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. static struct vm_struct *__get_vm_area_node(unsigned long size, unsigned long flags,
  146. unsigned long start, unsigned long end,
  147. int node, gfp_t gfp_mask)
  148. {
  149. struct vm_struct **p, *tmp, *area;
  150. unsigned long align = 1;
  151. unsigned long addr;
  152. BUG_ON(in_interrupt());
  153. if (flags & VM_IOREMAP) {
  154. int bit = fls(size);
  155. if (bit > IOREMAP_MAX_ORDER)
  156. bit = IOREMAP_MAX_ORDER;
  157. else if (bit < PAGE_SHIFT)
  158. bit = PAGE_SHIFT;
  159. align = 1ul << bit;
  160. }
  161. addr = ALIGN(start, align);
  162. size = PAGE_ALIGN(size);
  163. if (unlikely(!size))
  164. return NULL;
  165. area = kmalloc_node(sizeof(*area), gfp_mask & GFP_LEVEL_MASK, node);
  166. if (unlikely(!area))
  167. return NULL;
  168. /*
  169. * We always allocate a guard page.
  170. */
  171. size += PAGE_SIZE;
  172. write_lock(&vmlist_lock);
  173. for (p = &vmlist; (tmp = *p) != NULL ;p = &tmp->next) {
  174. if ((unsigned long)tmp->addr < addr) {
  175. if((unsigned long)tmp->addr + tmp->size >= addr)
  176. addr = ALIGN(tmp->size +
  177. (unsigned long)tmp->addr, align);
  178. continue;
  179. }
  180. if ((size + addr) < addr)
  181. goto out;
  182. if (size + addr <= (unsigned long)tmp->addr)
  183. goto found;
  184. addr = ALIGN(tmp->size + (unsigned long)tmp->addr, align);
  185. if (addr > end - size)
  186. goto out;
  187. }
  188. found:
  189. area->next = *p;
  190. *p = area;
  191. area->flags = flags;
  192. area->addr = (void *)addr;
  193. area->size = size;
  194. area->pages = NULL;
  195. area->nr_pages = 0;
  196. area->phys_addr = 0;
  197. write_unlock(&vmlist_lock);
  198. return area;
  199. out:
  200. write_unlock(&vmlist_lock);
  201. kfree(area);
  202. if (printk_ratelimit())
  203. printk(KERN_WARNING "allocation failed: out of vmalloc space - use vmalloc=<size> to increase size.\n");
  204. return NULL;
  205. }
  206. struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
  207. unsigned long start, unsigned long end)
  208. {
  209. return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL);
  210. }
  211. EXPORT_SYMBOL_GPL(__get_vm_area);
  212. /**
  213. * get_vm_area - reserve a contingous kernel virtual area
  214. * @size: size of the area
  215. * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
  216. *
  217. * Search an area of @size in the kernel virtual mapping area,
  218. * and reserved it for out purposes. Returns the area descriptor
  219. * on success or %NULL on failure.
  220. */
  221. struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
  222. {
  223. return __get_vm_area(size, flags, VMALLOC_START, VMALLOC_END);
  224. }
  225. struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
  226. int node, gfp_t gfp_mask)
  227. {
  228. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
  229. gfp_mask);
  230. }
  231. /* Caller must hold vmlist_lock */
  232. static struct vm_struct *__find_vm_area(void *addr)
  233. {
  234. struct vm_struct *tmp;
  235. for (tmp = vmlist; tmp != NULL; tmp = tmp->next) {
  236. if (tmp->addr == addr)
  237. break;
  238. }
  239. return tmp;
  240. }
  241. /* Caller must hold vmlist_lock */
  242. static struct vm_struct *__remove_vm_area(void *addr)
  243. {
  244. struct vm_struct **p, *tmp;
  245. for (p = &vmlist ; (tmp = *p) != NULL ;p = &tmp->next) {
  246. if (tmp->addr == addr)
  247. goto found;
  248. }
  249. return NULL;
  250. found:
  251. unmap_vm_area(tmp);
  252. *p = tmp->next;
  253. /*
  254. * Remove the guard page.
  255. */
  256. tmp->size -= PAGE_SIZE;
  257. return tmp;
  258. }
  259. /**
  260. * remove_vm_area - find and remove a contingous kernel virtual area
  261. * @addr: base address
  262. *
  263. * Search for the kernel VM area starting at @addr, and remove it.
  264. * This function returns the found VM area, but using it is NOT safe
  265. * on SMP machines, except for its size or flags.
  266. */
  267. struct vm_struct *remove_vm_area(void *addr)
  268. {
  269. struct vm_struct *v;
  270. write_lock(&vmlist_lock);
  271. v = __remove_vm_area(addr);
  272. write_unlock(&vmlist_lock);
  273. return v;
  274. }
  275. static void __vunmap(void *addr, int deallocate_pages)
  276. {
  277. struct vm_struct *area;
  278. if (!addr)
  279. return;
  280. if ((PAGE_SIZE-1) & (unsigned long)addr) {
  281. printk(KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
  282. WARN_ON(1);
  283. return;
  284. }
  285. area = remove_vm_area(addr);
  286. if (unlikely(!area)) {
  287. printk(KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
  288. addr);
  289. WARN_ON(1);
  290. return;
  291. }
  292. debug_check_no_locks_freed(addr, area->size);
  293. if (deallocate_pages) {
  294. int i;
  295. for (i = 0; i < area->nr_pages; i++) {
  296. BUG_ON(!area->pages[i]);
  297. __free_page(area->pages[i]);
  298. }
  299. if (area->flags & VM_VPAGES)
  300. vfree(area->pages);
  301. else
  302. kfree(area->pages);
  303. }
  304. kfree(area);
  305. return;
  306. }
  307. /**
  308. * vfree - release memory allocated by vmalloc()
  309. * @addr: memory base address
  310. *
  311. * Free the virtually contiguous memory area starting at @addr, as
  312. * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
  313. * NULL, no operation is performed.
  314. *
  315. * Must not be called in interrupt context.
  316. */
  317. void vfree(void *addr)
  318. {
  319. BUG_ON(in_interrupt());
  320. __vunmap(addr, 1);
  321. }
  322. EXPORT_SYMBOL(vfree);
  323. /**
  324. * vunmap - release virtual mapping obtained by vmap()
  325. * @addr: memory base address
  326. *
  327. * Free the virtually contiguous memory area starting at @addr,
  328. * which was created from the page array passed to vmap().
  329. *
  330. * Must not be called in interrupt context.
  331. */
  332. void vunmap(void *addr)
  333. {
  334. BUG_ON(in_interrupt());
  335. __vunmap(addr, 0);
  336. }
  337. EXPORT_SYMBOL(vunmap);
  338. /**
  339. * vmap - map an array of pages into virtually contiguous space
  340. * @pages: array of page pointers
  341. * @count: number of pages to map
  342. * @flags: vm_area->flags
  343. * @prot: page protection for the mapping
  344. *
  345. * Maps @count pages from @pages into contiguous kernel virtual
  346. * space.
  347. */
  348. void *vmap(struct page **pages, unsigned int count,
  349. unsigned long flags, pgprot_t prot)
  350. {
  351. struct vm_struct *area;
  352. if (count > num_physpages)
  353. return NULL;
  354. area = get_vm_area((count << PAGE_SHIFT), flags);
  355. if (!area)
  356. return NULL;
  357. if (map_vm_area(area, prot, &pages)) {
  358. vunmap(area->addr);
  359. return NULL;
  360. }
  361. return area->addr;
  362. }
  363. EXPORT_SYMBOL(vmap);
  364. void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  365. pgprot_t prot, int node)
  366. {
  367. struct page **pages;
  368. unsigned int nr_pages, array_size, i;
  369. nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
  370. array_size = (nr_pages * sizeof(struct page *));
  371. area->nr_pages = nr_pages;
  372. /* Please note that the recursion is strictly bounded. */
  373. if (array_size > PAGE_SIZE) {
  374. pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
  375. PAGE_KERNEL, node);
  376. area->flags |= VM_VPAGES;
  377. } else {
  378. pages = kmalloc_node(array_size,
  379. (gfp_mask & GFP_LEVEL_MASK) | __GFP_ZERO,
  380. node);
  381. }
  382. area->pages = pages;
  383. if (!area->pages) {
  384. remove_vm_area(area->addr);
  385. kfree(area);
  386. return NULL;
  387. }
  388. for (i = 0; i < area->nr_pages; i++) {
  389. if (node < 0)
  390. area->pages[i] = alloc_page(gfp_mask);
  391. else
  392. area->pages[i] = alloc_pages_node(node, gfp_mask, 0);
  393. if (unlikely(!area->pages[i])) {
  394. /* Successfully allocated i pages, free them in __vunmap() */
  395. area->nr_pages = i;
  396. goto fail;
  397. }
  398. }
  399. if (map_vm_area(area, prot, &pages))
  400. goto fail;
  401. return area->addr;
  402. fail:
  403. vfree(area->addr);
  404. return NULL;
  405. }
  406. void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
  407. {
  408. return __vmalloc_area_node(area, gfp_mask, prot, -1);
  409. }
  410. /**
  411. * __vmalloc_node - allocate virtually contiguous memory
  412. * @size: allocation size
  413. * @gfp_mask: flags for the page level allocator
  414. * @prot: protection mask for the allocated pages
  415. * @node: node to use for allocation or -1
  416. *
  417. * Allocate enough pages to cover @size from the page level
  418. * allocator with @gfp_mask flags. Map them into contiguous
  419. * kernel virtual space, using a pagetable protection of @prot.
  420. */
  421. static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  422. int node)
  423. {
  424. struct vm_struct *area;
  425. size = PAGE_ALIGN(size);
  426. if (!size || (size >> PAGE_SHIFT) > num_physpages)
  427. return NULL;
  428. area = get_vm_area_node(size, VM_ALLOC, node, gfp_mask);
  429. if (!area)
  430. return NULL;
  431. return __vmalloc_area_node(area, gfp_mask, prot, node);
  432. }
  433. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  434. {
  435. return __vmalloc_node(size, gfp_mask, prot, -1);
  436. }
  437. EXPORT_SYMBOL(__vmalloc);
  438. /**
  439. * vmalloc - allocate virtually contiguous memory
  440. * @size: allocation size
  441. * Allocate enough pages to cover @size from the page level
  442. * allocator and map them into contiguous kernel virtual space.
  443. *
  444. * For tight control over page level allocator and protection flags
  445. * use __vmalloc() instead.
  446. */
  447. void *vmalloc(unsigned long size)
  448. {
  449. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
  450. }
  451. EXPORT_SYMBOL(vmalloc);
  452. /**
  453. * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
  454. * @size: allocation size
  455. *
  456. * The resulting memory area is zeroed so it can be mapped to userspace
  457. * without leaking data.
  458. */
  459. void *vmalloc_user(unsigned long size)
  460. {
  461. struct vm_struct *area;
  462. void *ret;
  463. ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
  464. if (ret) {
  465. write_lock(&vmlist_lock);
  466. area = __find_vm_area(ret);
  467. area->flags |= VM_USERMAP;
  468. write_unlock(&vmlist_lock);
  469. }
  470. return ret;
  471. }
  472. EXPORT_SYMBOL(vmalloc_user);
  473. /**
  474. * vmalloc_node - allocate memory on a specific node
  475. * @size: allocation size
  476. * @node: numa node
  477. *
  478. * Allocate enough pages to cover @size from the page level
  479. * allocator and map them into contiguous kernel virtual space.
  480. *
  481. * For tight control over page level allocator and protection flags
  482. * use __vmalloc() instead.
  483. */
  484. void *vmalloc_node(unsigned long size, int node)
  485. {
  486. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL, node);
  487. }
  488. EXPORT_SYMBOL(vmalloc_node);
  489. #ifndef PAGE_KERNEL_EXEC
  490. # define PAGE_KERNEL_EXEC PAGE_KERNEL
  491. #endif
  492. /**
  493. * vmalloc_exec - allocate virtually contiguous, executable memory
  494. * @size: allocation size
  495. *
  496. * Kernel-internal function to allocate enough pages to cover @size
  497. * the page level allocator and map them into contiguous and
  498. * executable kernel virtual space.
  499. *
  500. * For tight control over page level allocator and protection flags
  501. * use __vmalloc() instead.
  502. */
  503. void *vmalloc_exec(unsigned long size)
  504. {
  505. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
  506. }
  507. #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
  508. #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
  509. #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
  510. #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
  511. #else
  512. #define GFP_VMALLOC32 GFP_KERNEL
  513. #endif
  514. /**
  515. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  516. * @size: allocation size
  517. *
  518. * Allocate enough 32bit PA addressable pages to cover @size from the
  519. * page level allocator and map them into contiguous kernel virtual space.
  520. */
  521. void *vmalloc_32(unsigned long size)
  522. {
  523. return __vmalloc(size, GFP_VMALLOC32, PAGE_KERNEL);
  524. }
  525. EXPORT_SYMBOL(vmalloc_32);
  526. /**
  527. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  528. * @size: allocation size
  529. *
  530. * The resulting memory area is 32bit addressable and zeroed so it can be
  531. * mapped to userspace without leaking data.
  532. */
  533. void *vmalloc_32_user(unsigned long size)
  534. {
  535. struct vm_struct *area;
  536. void *ret;
  537. ret = __vmalloc(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL);
  538. if (ret) {
  539. write_lock(&vmlist_lock);
  540. area = __find_vm_area(ret);
  541. area->flags |= VM_USERMAP;
  542. write_unlock(&vmlist_lock);
  543. }
  544. return ret;
  545. }
  546. EXPORT_SYMBOL(vmalloc_32_user);
  547. long vread(char *buf, char *addr, unsigned long count)
  548. {
  549. struct vm_struct *tmp;
  550. char *vaddr, *buf_start = buf;
  551. unsigned long n;
  552. /* Don't allow overflow */
  553. if ((unsigned long) addr + count < count)
  554. count = -(unsigned long) addr;
  555. read_lock(&vmlist_lock);
  556. for (tmp = vmlist; tmp; tmp = tmp->next) {
  557. vaddr = (char *) tmp->addr;
  558. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  559. continue;
  560. while (addr < vaddr) {
  561. if (count == 0)
  562. goto finished;
  563. *buf = '\0';
  564. buf++;
  565. addr++;
  566. count--;
  567. }
  568. n = vaddr + tmp->size - PAGE_SIZE - addr;
  569. do {
  570. if (count == 0)
  571. goto finished;
  572. *buf = *addr;
  573. buf++;
  574. addr++;
  575. count--;
  576. } while (--n > 0);
  577. }
  578. finished:
  579. read_unlock(&vmlist_lock);
  580. return buf - buf_start;
  581. }
  582. long vwrite(char *buf, char *addr, unsigned long count)
  583. {
  584. struct vm_struct *tmp;
  585. char *vaddr, *buf_start = buf;
  586. unsigned long n;
  587. /* Don't allow overflow */
  588. if ((unsigned long) addr + count < count)
  589. count = -(unsigned long) addr;
  590. read_lock(&vmlist_lock);
  591. for (tmp = vmlist; tmp; tmp = tmp->next) {
  592. vaddr = (char *) tmp->addr;
  593. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  594. continue;
  595. while (addr < vaddr) {
  596. if (count == 0)
  597. goto finished;
  598. buf++;
  599. addr++;
  600. count--;
  601. }
  602. n = vaddr + tmp->size - PAGE_SIZE - addr;
  603. do {
  604. if (count == 0)
  605. goto finished;
  606. *addr = *buf;
  607. buf++;
  608. addr++;
  609. count--;
  610. } while (--n > 0);
  611. }
  612. finished:
  613. read_unlock(&vmlist_lock);
  614. return buf - buf_start;
  615. }
  616. /**
  617. * remap_vmalloc_range - map vmalloc pages to userspace
  618. * @vma: vma to cover (map full range of vma)
  619. * @addr: vmalloc memory
  620. * @pgoff: number of pages into addr before first page to map
  621. * @returns: 0 for success, -Exxx on failure
  622. *
  623. * This function checks that addr is a valid vmalloc'ed area, and
  624. * that it is big enough to cover the vma. Will return failure if
  625. * that criteria isn't met.
  626. *
  627. * Similar to remap_pfn_range() (see mm/memory.c)
  628. */
  629. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  630. unsigned long pgoff)
  631. {
  632. struct vm_struct *area;
  633. unsigned long uaddr = vma->vm_start;
  634. unsigned long usize = vma->vm_end - vma->vm_start;
  635. int ret;
  636. if ((PAGE_SIZE-1) & (unsigned long)addr)
  637. return -EINVAL;
  638. read_lock(&vmlist_lock);
  639. area = __find_vm_area(addr);
  640. if (!area)
  641. goto out_einval_locked;
  642. if (!(area->flags & VM_USERMAP))
  643. goto out_einval_locked;
  644. if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
  645. goto out_einval_locked;
  646. read_unlock(&vmlist_lock);
  647. addr += pgoff << PAGE_SHIFT;
  648. do {
  649. struct page *page = vmalloc_to_page(addr);
  650. ret = vm_insert_page(vma, uaddr, page);
  651. if (ret)
  652. return ret;
  653. uaddr += PAGE_SIZE;
  654. addr += PAGE_SIZE;
  655. usize -= PAGE_SIZE;
  656. } while (usize > 0);
  657. /* Prevent "things" like memory migration? VM_flags need a cleanup... */
  658. vma->vm_flags |= VM_RESERVED;
  659. return ret;
  660. out_einval_locked:
  661. read_unlock(&vmlist_lock);
  662. return -EINVAL;
  663. }
  664. EXPORT_SYMBOL(remap_vmalloc_range);
  665. /*
  666. * Implement a stub for vmalloc_sync_all() if the architecture chose not to
  667. * have one.
  668. */
  669. void __attribute__((weak)) vmalloc_sync_all(void)
  670. {
  671. }
  672. static int f(pte_t *pte, struct page *pmd_page, unsigned long addr, void *data)
  673. {
  674. /* apply_to_page_range() does all the hard work. */
  675. return 0;
  676. }
  677. /**
  678. * alloc_vm_area - allocate a range of kernel address space
  679. * @size: size of the area
  680. * @returns: NULL on failure, vm_struct on success
  681. *
  682. * This function reserves a range of kernel address space, and
  683. * allocates pagetables to map that range. No actual mappings
  684. * are created. If the kernel address space is not shared
  685. * between processes, it syncs the pagetable across all
  686. * processes.
  687. */
  688. struct vm_struct *alloc_vm_area(size_t size)
  689. {
  690. struct vm_struct *area;
  691. area = get_vm_area(size, VM_IOREMAP);
  692. if (area == NULL)
  693. return NULL;
  694. /*
  695. * This ensures that page tables are constructed for this region
  696. * of kernel virtual address space and mapped into init_mm.
  697. */
  698. if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
  699. area->size, f, NULL)) {
  700. free_vm_area(area);
  701. return NULL;
  702. }
  703. /* Make sure the pagetables are constructed in process kernel
  704. mappings */
  705. vmalloc_sync_all();
  706. return area;
  707. }
  708. EXPORT_SYMBOL_GPL(alloc_vm_area);
  709. void free_vm_area(struct vm_struct *area)
  710. {
  711. struct vm_struct *ret;
  712. ret = remove_vm_area(area->addr);
  713. BUG_ON(ret != area);
  714. kfree(area);
  715. }
  716. EXPORT_SYMBOL_GPL(free_vm_area);