vmalloc.c 18 KB

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