vmalloc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. struct vm_struct *__remove_vm_area(void *addr)
  224. {
  225. struct vm_struct **p, *tmp;
  226. for (p = &vmlist ; (tmp = *p) != NULL ;p = &tmp->next) {
  227. if (tmp->addr == addr)
  228. goto found;
  229. }
  230. return NULL;
  231. found:
  232. unmap_vm_area(tmp);
  233. *p = tmp->next;
  234. /*
  235. * Remove the guard page.
  236. */
  237. tmp->size -= PAGE_SIZE;
  238. return tmp;
  239. }
  240. /**
  241. * remove_vm_area - find and remove a contingous kernel virtual area
  242. *
  243. * @addr: base address
  244. *
  245. * Search for the kernel VM area starting at @addr, and remove it.
  246. * This function returns the found VM area, but using it is NOT safe
  247. * on SMP machines, except for its size or flags.
  248. */
  249. struct vm_struct *remove_vm_area(void *addr)
  250. {
  251. struct vm_struct *v;
  252. write_lock(&vmlist_lock);
  253. v = __remove_vm_area(addr);
  254. write_unlock(&vmlist_lock);
  255. return v;
  256. }
  257. void __vunmap(void *addr, int deallocate_pages)
  258. {
  259. struct vm_struct *area;
  260. if (!addr)
  261. return;
  262. if ((PAGE_SIZE-1) & (unsigned long)addr) {
  263. printk(KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
  264. WARN_ON(1);
  265. return;
  266. }
  267. area = remove_vm_area(addr);
  268. if (unlikely(!area)) {
  269. printk(KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
  270. addr);
  271. WARN_ON(1);
  272. return;
  273. }
  274. if (deallocate_pages) {
  275. int i;
  276. for (i = 0; i < area->nr_pages; i++) {
  277. if (unlikely(!area->pages[i]))
  278. BUG();
  279. __free_page(area->pages[i]);
  280. }
  281. if (area->nr_pages > PAGE_SIZE/sizeof(struct page *))
  282. vfree(area->pages);
  283. else
  284. kfree(area->pages);
  285. }
  286. kfree(area);
  287. return;
  288. }
  289. /**
  290. * vfree - release memory allocated by vmalloc()
  291. *
  292. * @addr: memory base address
  293. *
  294. * Free the virtually contiguous memory area starting at @addr, as
  295. * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
  296. * NULL, no operation is performed.
  297. *
  298. * Must not be called in interrupt context.
  299. */
  300. void vfree(void *addr)
  301. {
  302. BUG_ON(in_interrupt());
  303. __vunmap(addr, 1);
  304. }
  305. EXPORT_SYMBOL(vfree);
  306. /**
  307. * vunmap - release virtual mapping obtained by vmap()
  308. *
  309. * @addr: memory base address
  310. *
  311. * Free the virtually contiguous memory area starting at @addr,
  312. * which was created from the page array passed to vmap().
  313. *
  314. * Must not be called in interrupt context.
  315. */
  316. void vunmap(void *addr)
  317. {
  318. BUG_ON(in_interrupt());
  319. __vunmap(addr, 0);
  320. }
  321. EXPORT_SYMBOL(vunmap);
  322. /**
  323. * vmap - map an array of pages into virtually contiguous space
  324. *
  325. * @pages: array of page pointers
  326. * @count: number of pages to map
  327. * @flags: vm_area->flags
  328. * @prot: page protection for the mapping
  329. *
  330. * Maps @count pages from @pages into contiguous kernel virtual
  331. * space.
  332. */
  333. void *vmap(struct page **pages, unsigned int count,
  334. unsigned long flags, pgprot_t prot)
  335. {
  336. struct vm_struct *area;
  337. if (count > num_physpages)
  338. return NULL;
  339. area = get_vm_area((count << PAGE_SHIFT), flags);
  340. if (!area)
  341. return NULL;
  342. if (map_vm_area(area, prot, &pages)) {
  343. vunmap(area->addr);
  344. return NULL;
  345. }
  346. return area->addr;
  347. }
  348. EXPORT_SYMBOL(vmap);
  349. void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  350. pgprot_t prot, int node)
  351. {
  352. struct page **pages;
  353. unsigned int nr_pages, array_size, i;
  354. nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
  355. array_size = (nr_pages * sizeof(struct page *));
  356. area->nr_pages = nr_pages;
  357. /* Please note that the recursion is strictly bounded. */
  358. if (array_size > PAGE_SIZE)
  359. pages = __vmalloc_node(array_size, gfp_mask, PAGE_KERNEL, node);
  360. else
  361. pages = kmalloc_node(array_size, (gfp_mask & ~__GFP_HIGHMEM), node);
  362. area->pages = pages;
  363. if (!area->pages) {
  364. remove_vm_area(area->addr);
  365. kfree(area);
  366. return NULL;
  367. }
  368. memset(area->pages, 0, array_size);
  369. for (i = 0; i < area->nr_pages; i++) {
  370. if (node < 0)
  371. area->pages[i] = alloc_page(gfp_mask);
  372. else
  373. area->pages[i] = alloc_pages_node(node, gfp_mask, 0);
  374. if (unlikely(!area->pages[i])) {
  375. /* Successfully allocated i pages, free them in __vunmap() */
  376. area->nr_pages = i;
  377. goto fail;
  378. }
  379. }
  380. if (map_vm_area(area, prot, &pages))
  381. goto fail;
  382. return area->addr;
  383. fail:
  384. vfree(area->addr);
  385. return NULL;
  386. }
  387. void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
  388. {
  389. return __vmalloc_area_node(area, gfp_mask, prot, -1);
  390. }
  391. /**
  392. * __vmalloc_node - allocate virtually contiguous memory
  393. *
  394. * @size: allocation size
  395. * @gfp_mask: flags for the page level allocator
  396. * @prot: protection mask for the allocated pages
  397. * @node: node to use for allocation or -1
  398. *
  399. * Allocate enough pages to cover @size from the page level
  400. * allocator with @gfp_mask flags. Map them into contiguous
  401. * kernel virtual space, using a pagetable protection of @prot.
  402. */
  403. void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  404. int node)
  405. {
  406. struct vm_struct *area;
  407. size = PAGE_ALIGN(size);
  408. if (!size || (size >> PAGE_SHIFT) > num_physpages)
  409. return NULL;
  410. area = get_vm_area_node(size, VM_ALLOC, node);
  411. if (!area)
  412. return NULL;
  413. return __vmalloc_area_node(area, gfp_mask, prot, node);
  414. }
  415. EXPORT_SYMBOL(__vmalloc_node);
  416. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  417. {
  418. return __vmalloc_node(size, gfp_mask, prot, -1);
  419. }
  420. EXPORT_SYMBOL(__vmalloc);
  421. /**
  422. * vmalloc - allocate virtually contiguous memory
  423. *
  424. * @size: allocation size
  425. *
  426. * Allocate enough pages to cover @size from the page level
  427. * allocator and map them into contiguous kernel virtual space.
  428. *
  429. * For tight cotrol over page level allocator and protection flags
  430. * use __vmalloc() instead.
  431. */
  432. void *vmalloc(unsigned long size)
  433. {
  434. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
  435. }
  436. EXPORT_SYMBOL(vmalloc);
  437. /**
  438. * vmalloc_node - allocate memory on a specific node
  439. *
  440. * @size: allocation size
  441. * @node: numa node
  442. *
  443. * Allocate enough pages to cover @size from the page level
  444. * allocator and map them into contiguous kernel virtual space.
  445. *
  446. * For tight cotrol over page level allocator and protection flags
  447. * use __vmalloc() instead.
  448. */
  449. void *vmalloc_node(unsigned long size, int node)
  450. {
  451. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL, node);
  452. }
  453. EXPORT_SYMBOL(vmalloc_node);
  454. #ifndef PAGE_KERNEL_EXEC
  455. # define PAGE_KERNEL_EXEC PAGE_KERNEL
  456. #endif
  457. /**
  458. * vmalloc_exec - allocate virtually contiguous, executable memory
  459. *
  460. * @size: allocation size
  461. *
  462. * Kernel-internal function to allocate enough pages to cover @size
  463. * the page level allocator and map them into contiguous and
  464. * executable kernel virtual space.
  465. *
  466. * For tight cotrol over page level allocator and protection flags
  467. * use __vmalloc() instead.
  468. */
  469. void *vmalloc_exec(unsigned long size)
  470. {
  471. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
  472. }
  473. /**
  474. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  475. *
  476. * @size: allocation size
  477. *
  478. * Allocate enough 32bit PA addressable pages to cover @size from the
  479. * page level allocator and map them into contiguous kernel virtual space.
  480. */
  481. void *vmalloc_32(unsigned long size)
  482. {
  483. return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
  484. }
  485. EXPORT_SYMBOL(vmalloc_32);
  486. long vread(char *buf, char *addr, unsigned long count)
  487. {
  488. struct vm_struct *tmp;
  489. char *vaddr, *buf_start = buf;
  490. unsigned long n;
  491. /* Don't allow overflow */
  492. if ((unsigned long) addr + count < count)
  493. count = -(unsigned long) addr;
  494. read_lock(&vmlist_lock);
  495. for (tmp = vmlist; tmp; tmp = tmp->next) {
  496. vaddr = (char *) tmp->addr;
  497. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  498. continue;
  499. while (addr < vaddr) {
  500. if (count == 0)
  501. goto finished;
  502. *buf = '\0';
  503. buf++;
  504. addr++;
  505. count--;
  506. }
  507. n = vaddr + tmp->size - PAGE_SIZE - addr;
  508. do {
  509. if (count == 0)
  510. goto finished;
  511. *buf = *addr;
  512. buf++;
  513. addr++;
  514. count--;
  515. } while (--n > 0);
  516. }
  517. finished:
  518. read_unlock(&vmlist_lock);
  519. return buf - buf_start;
  520. }
  521. long vwrite(char *buf, char *addr, unsigned long count)
  522. {
  523. struct vm_struct *tmp;
  524. char *vaddr, *buf_start = buf;
  525. unsigned long n;
  526. /* Don't allow overflow */
  527. if ((unsigned long) addr + count < count)
  528. count = -(unsigned long) addr;
  529. read_lock(&vmlist_lock);
  530. for (tmp = vmlist; tmp; tmp = tmp->next) {
  531. vaddr = (char *) tmp->addr;
  532. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  533. continue;
  534. while (addr < vaddr) {
  535. if (count == 0)
  536. goto finished;
  537. buf++;
  538. addr++;
  539. count--;
  540. }
  541. n = vaddr + tmp->size - PAGE_SIZE - addr;
  542. do {
  543. if (count == 0)
  544. goto finished;
  545. *addr = *buf;
  546. buf++;
  547. addr++;
  548. count--;
  549. } while (--n > 0);
  550. }
  551. finished:
  552. read_unlock(&vmlist_lock);
  553. return buf - buf_start;
  554. }