vmalloc.c 17 KB

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