vmalloc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. */
  9. #include <linux/mm.h>
  10. #include <linux/module.h>
  11. #include <linux/highmem.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/vmalloc.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/tlbflush.h>
  18. DEFINE_RWLOCK(vmlist_lock);
  19. struct vm_struct *vmlist;
  20. static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
  21. {
  22. pte_t *pte;
  23. pte = pte_offset_kernel(pmd, addr);
  24. do {
  25. pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
  26. WARN_ON(!pte_none(ptent) && !pte_present(ptent));
  27. } while (pte++, addr += PAGE_SIZE, addr != end);
  28. }
  29. static inline void vunmap_pmd_range(pud_t *pud, unsigned long addr,
  30. unsigned long end)
  31. {
  32. pmd_t *pmd;
  33. unsigned long next;
  34. pmd = pmd_offset(pud, addr);
  35. do {
  36. next = pmd_addr_end(addr, end);
  37. if (pmd_none_or_clear_bad(pmd))
  38. continue;
  39. vunmap_pte_range(pmd, addr, next);
  40. } while (pmd++, addr = next, addr != end);
  41. }
  42. static inline void vunmap_pud_range(pgd_t *pgd, unsigned long addr,
  43. unsigned long end)
  44. {
  45. pud_t *pud;
  46. unsigned long next;
  47. pud = pud_offset(pgd, addr);
  48. do {
  49. next = pud_addr_end(addr, end);
  50. if (pud_none_or_clear_bad(pud))
  51. continue;
  52. vunmap_pmd_range(pud, addr, next);
  53. } while (pud++, addr = next, addr != end);
  54. }
  55. void unmap_vm_area(struct vm_struct *area)
  56. {
  57. pgd_t *pgd;
  58. unsigned long next;
  59. unsigned long addr = (unsigned long) area->addr;
  60. unsigned long end = addr + area->size;
  61. BUG_ON(addr >= end);
  62. pgd = pgd_offset_k(addr);
  63. flush_cache_vunmap(addr, end);
  64. do {
  65. next = pgd_addr_end(addr, end);
  66. if (pgd_none_or_clear_bad(pgd))
  67. continue;
  68. vunmap_pud_range(pgd, addr, next);
  69. } while (pgd++, addr = next, addr != end);
  70. flush_tlb_kernel_range((unsigned long) area->addr, end);
  71. }
  72. static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
  73. unsigned long end, pgprot_t prot, struct page ***pages)
  74. {
  75. pte_t *pte;
  76. pte = pte_alloc_kernel(&init_mm, pmd, addr);
  77. if (!pte)
  78. return -ENOMEM;
  79. do {
  80. struct page *page = **pages;
  81. WARN_ON(!pte_none(*pte));
  82. if (!page)
  83. return -ENOMEM;
  84. set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
  85. (*pages)++;
  86. } while (pte++, addr += PAGE_SIZE, addr != end);
  87. return 0;
  88. }
  89. static inline int vmap_pmd_range(pud_t *pud, unsigned long addr,
  90. unsigned long end, pgprot_t prot, struct page ***pages)
  91. {
  92. pmd_t *pmd;
  93. unsigned long next;
  94. pmd = pmd_alloc(&init_mm, pud, addr);
  95. if (!pmd)
  96. return -ENOMEM;
  97. do {
  98. next = pmd_addr_end(addr, end);
  99. if (vmap_pte_range(pmd, addr, next, prot, pages))
  100. return -ENOMEM;
  101. } while (pmd++, addr = next, addr != end);
  102. return 0;
  103. }
  104. static inline int vmap_pud_range(pgd_t *pgd, unsigned long addr,
  105. unsigned long end, pgprot_t prot, struct page ***pages)
  106. {
  107. pud_t *pud;
  108. unsigned long next;
  109. pud = pud_alloc(&init_mm, pgd, addr);
  110. if (!pud)
  111. return -ENOMEM;
  112. do {
  113. next = pud_addr_end(addr, end);
  114. if (vmap_pmd_range(pud, addr, next, prot, pages))
  115. return -ENOMEM;
  116. } while (pud++, addr = next, addr != end);
  117. return 0;
  118. }
  119. int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
  120. {
  121. pgd_t *pgd;
  122. unsigned long next;
  123. unsigned long addr = (unsigned long) area->addr;
  124. unsigned long end = addr + area->size - PAGE_SIZE;
  125. int err;
  126. BUG_ON(addr >= end);
  127. pgd = pgd_offset_k(addr);
  128. spin_lock(&init_mm.page_table_lock);
  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. spin_unlock(&init_mm.page_table_lock);
  136. flush_cache_vmap((unsigned long) area->addr, end);
  137. return err;
  138. }
  139. #define IOREMAP_MAX_ORDER (7 + PAGE_SHIFT) /* 128 pages */
  140. struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
  141. unsigned long start, unsigned long end)
  142. {
  143. struct vm_struct **p, *tmp, *area;
  144. unsigned long align = 1;
  145. unsigned long addr;
  146. if (flags & VM_IOREMAP) {
  147. int bit = fls(size);
  148. if (bit > IOREMAP_MAX_ORDER)
  149. bit = IOREMAP_MAX_ORDER;
  150. else if (bit < PAGE_SHIFT)
  151. bit = PAGE_SHIFT;
  152. align = 1ul << bit;
  153. }
  154. addr = ALIGN(start, align);
  155. size = PAGE_ALIGN(size);
  156. area = kmalloc(sizeof(*area), GFP_KERNEL);
  157. if (unlikely(!area))
  158. return NULL;
  159. if (unlikely(!size)) {
  160. kfree (area);
  161. return NULL;
  162. }
  163. /*
  164. * We always allocate a guard page.
  165. */
  166. size += PAGE_SIZE;
  167. write_lock(&vmlist_lock);
  168. for (p = &vmlist; (tmp = *p) != NULL ;p = &tmp->next) {
  169. if ((unsigned long)tmp->addr < addr) {
  170. if((unsigned long)tmp->addr + tmp->size >= addr)
  171. addr = ALIGN(tmp->size +
  172. (unsigned long)tmp->addr, align);
  173. continue;
  174. }
  175. if ((size + addr) < addr)
  176. goto out;
  177. if (size + addr <= (unsigned long)tmp->addr)
  178. goto found;
  179. addr = ALIGN(tmp->size + (unsigned long)tmp->addr, align);
  180. if (addr > end - size)
  181. goto out;
  182. }
  183. found:
  184. area->next = *p;
  185. *p = area;
  186. area->flags = flags;
  187. area->addr = (void *)addr;
  188. area->size = size;
  189. area->pages = NULL;
  190. area->nr_pages = 0;
  191. area->phys_addr = 0;
  192. write_unlock(&vmlist_lock);
  193. return area;
  194. out:
  195. write_unlock(&vmlist_lock);
  196. kfree(area);
  197. if (printk_ratelimit())
  198. printk(KERN_WARNING "allocation failed: out of vmalloc space - use vmalloc=<size> to increase size.\n");
  199. return NULL;
  200. }
  201. /**
  202. * get_vm_area - reserve a contingous kernel virtual area
  203. *
  204. * @size: size of the area
  205. * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
  206. *
  207. * Search an area of @size in the kernel virtual mapping area,
  208. * and reserved it for out purposes. Returns the area descriptor
  209. * on success or %NULL on failure.
  210. */
  211. struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
  212. {
  213. return __get_vm_area(size, flags, VMALLOC_START, VMALLOC_END);
  214. }
  215. /* Caller must hold vmlist_lock */
  216. struct vm_struct *__remove_vm_area(void *addr)
  217. {
  218. struct vm_struct **p, *tmp;
  219. for (p = &vmlist ; (tmp = *p) != NULL ;p = &tmp->next) {
  220. if (tmp->addr == addr)
  221. goto found;
  222. }
  223. return NULL;
  224. found:
  225. unmap_vm_area(tmp);
  226. *p = tmp->next;
  227. /*
  228. * Remove the guard page.
  229. */
  230. tmp->size -= PAGE_SIZE;
  231. return tmp;
  232. }
  233. /**
  234. * remove_vm_area - find and remove a contingous kernel virtual area
  235. *
  236. * @addr: base address
  237. *
  238. * Search for the kernel VM area starting at @addr, and remove it.
  239. * This function returns the found VM area, but using it is NOT safe
  240. * on SMP machines, except for its size or flags.
  241. */
  242. struct vm_struct *remove_vm_area(void *addr)
  243. {
  244. struct vm_struct *v;
  245. write_lock(&vmlist_lock);
  246. v = __remove_vm_area(addr);
  247. write_unlock(&vmlist_lock);
  248. return v;
  249. }
  250. void __vunmap(void *addr, int deallocate_pages)
  251. {
  252. struct vm_struct *area;
  253. if (!addr)
  254. return;
  255. if ((PAGE_SIZE-1) & (unsigned long)addr) {
  256. printk(KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
  257. WARN_ON(1);
  258. return;
  259. }
  260. area = remove_vm_area(addr);
  261. if (unlikely(!area)) {
  262. printk(KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
  263. addr);
  264. WARN_ON(1);
  265. return;
  266. }
  267. if (deallocate_pages) {
  268. int i;
  269. for (i = 0; i < area->nr_pages; i++) {
  270. if (unlikely(!area->pages[i]))
  271. BUG();
  272. __free_page(area->pages[i]);
  273. }
  274. if (area->nr_pages > PAGE_SIZE/sizeof(struct page *))
  275. vfree(area->pages);
  276. else
  277. kfree(area->pages);
  278. }
  279. kfree(area);
  280. return;
  281. }
  282. /**
  283. * vfree - release memory allocated by vmalloc()
  284. *
  285. * @addr: memory base address
  286. *
  287. * Free the virtually contiguous memory area starting at @addr, as
  288. * obtained from vmalloc(), vmalloc_32() or __vmalloc().
  289. *
  290. * May not be called in interrupt context.
  291. */
  292. void vfree(void *addr)
  293. {
  294. BUG_ON(in_interrupt());
  295. __vunmap(addr, 1);
  296. }
  297. EXPORT_SYMBOL(vfree);
  298. /**
  299. * vunmap - release virtual mapping obtained by vmap()
  300. *
  301. * @addr: memory base address
  302. *
  303. * Free the virtually contiguous memory area starting at @addr,
  304. * which was created from the page array passed to vmap().
  305. *
  306. * May not be called in interrupt context.
  307. */
  308. void vunmap(void *addr)
  309. {
  310. BUG_ON(in_interrupt());
  311. __vunmap(addr, 0);
  312. }
  313. EXPORT_SYMBOL(vunmap);
  314. /**
  315. * vmap - map an array of pages into virtually contiguous space
  316. *
  317. * @pages: array of page pointers
  318. * @count: number of pages to map
  319. * @flags: vm_area->flags
  320. * @prot: page protection for the mapping
  321. *
  322. * Maps @count pages from @pages into contiguous kernel virtual
  323. * space.
  324. */
  325. void *vmap(struct page **pages, unsigned int count,
  326. unsigned long flags, pgprot_t prot)
  327. {
  328. struct vm_struct *area;
  329. if (count > num_physpages)
  330. return NULL;
  331. area = get_vm_area((count << PAGE_SHIFT), flags);
  332. if (!area)
  333. return NULL;
  334. if (map_vm_area(area, prot, &pages)) {
  335. vunmap(area->addr);
  336. return NULL;
  337. }
  338. return area->addr;
  339. }
  340. EXPORT_SYMBOL(vmap);
  341. void *__vmalloc_area(struct vm_struct *area, unsigned int __nocast gfp_mask, pgprot_t prot)
  342. {
  343. struct page **pages;
  344. unsigned int nr_pages, array_size, i;
  345. nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
  346. array_size = (nr_pages * sizeof(struct page *));
  347. area->nr_pages = nr_pages;
  348. /* Please note that the recursion is strictly bounded. */
  349. if (array_size > PAGE_SIZE)
  350. pages = __vmalloc(array_size, gfp_mask, PAGE_KERNEL);
  351. else
  352. pages = kmalloc(array_size, (gfp_mask & ~__GFP_HIGHMEM));
  353. area->pages = pages;
  354. if (!area->pages) {
  355. remove_vm_area(area->addr);
  356. kfree(area);
  357. return NULL;
  358. }
  359. memset(area->pages, 0, array_size);
  360. for (i = 0; i < area->nr_pages; i++) {
  361. area->pages[i] = alloc_page(gfp_mask);
  362. if (unlikely(!area->pages[i])) {
  363. /* Successfully allocated i pages, free them in __vunmap() */
  364. area->nr_pages = i;
  365. goto fail;
  366. }
  367. }
  368. if (map_vm_area(area, prot, &pages))
  369. goto fail;
  370. return area->addr;
  371. fail:
  372. vfree(area->addr);
  373. return NULL;
  374. }
  375. /**
  376. * __vmalloc - allocate virtually contiguous memory
  377. *
  378. * @size: allocation size
  379. * @gfp_mask: flags for the page level allocator
  380. * @prot: protection mask for the allocated pages
  381. *
  382. * Allocate enough pages to cover @size from the page level
  383. * allocator with @gfp_mask flags. Map them into contiguous
  384. * kernel virtual space, using a pagetable protection of @prot.
  385. */
  386. void *__vmalloc(unsigned long size, unsigned int __nocast gfp_mask, pgprot_t prot)
  387. {
  388. struct vm_struct *area;
  389. size = PAGE_ALIGN(size);
  390. if (!size || (size >> PAGE_SHIFT) > num_physpages)
  391. return NULL;
  392. area = get_vm_area(size, VM_ALLOC);
  393. if (!area)
  394. return NULL;
  395. return __vmalloc_area(area, gfp_mask, prot);
  396. }
  397. EXPORT_SYMBOL(__vmalloc);
  398. /**
  399. * vmalloc - allocate virtually contiguous memory
  400. *
  401. * @size: allocation size
  402. *
  403. * Allocate enough pages to cover @size from the page level
  404. * allocator and map them into contiguous kernel virtual space.
  405. *
  406. * For tight cotrol over page level allocator and protection flags
  407. * use __vmalloc() instead.
  408. */
  409. void *vmalloc(unsigned long size)
  410. {
  411. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
  412. }
  413. EXPORT_SYMBOL(vmalloc);
  414. #ifndef PAGE_KERNEL_EXEC
  415. # define PAGE_KERNEL_EXEC PAGE_KERNEL
  416. #endif
  417. /**
  418. * vmalloc_exec - allocate virtually contiguous, executable memory
  419. *
  420. * @size: allocation size
  421. *
  422. * Kernel-internal function to allocate enough pages to cover @size
  423. * the page level allocator and map them into contiguous and
  424. * executable kernel virtual space.
  425. *
  426. * For tight cotrol over page level allocator and protection flags
  427. * use __vmalloc() instead.
  428. */
  429. void *vmalloc_exec(unsigned long size)
  430. {
  431. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
  432. }
  433. /**
  434. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  435. *
  436. * @size: allocation size
  437. *
  438. * Allocate enough 32bit PA addressable pages to cover @size from the
  439. * page level allocator and map them into contiguous kernel virtual space.
  440. */
  441. void *vmalloc_32(unsigned long size)
  442. {
  443. return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
  444. }
  445. EXPORT_SYMBOL(vmalloc_32);
  446. long vread(char *buf, char *addr, unsigned long count)
  447. {
  448. struct vm_struct *tmp;
  449. char *vaddr, *buf_start = buf;
  450. unsigned long n;
  451. /* Don't allow overflow */
  452. if ((unsigned long) addr + count < count)
  453. count = -(unsigned long) addr;
  454. read_lock(&vmlist_lock);
  455. for (tmp = vmlist; tmp; tmp = tmp->next) {
  456. vaddr = (char *) tmp->addr;
  457. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  458. continue;
  459. while (addr < vaddr) {
  460. if (count == 0)
  461. goto finished;
  462. *buf = '\0';
  463. buf++;
  464. addr++;
  465. count--;
  466. }
  467. n = vaddr + tmp->size - PAGE_SIZE - addr;
  468. do {
  469. if (count == 0)
  470. goto finished;
  471. *buf = *addr;
  472. buf++;
  473. addr++;
  474. count--;
  475. } while (--n > 0);
  476. }
  477. finished:
  478. read_unlock(&vmlist_lock);
  479. return buf - buf_start;
  480. }
  481. long vwrite(char *buf, char *addr, unsigned long count)
  482. {
  483. struct vm_struct *tmp;
  484. char *vaddr, *buf_start = buf;
  485. unsigned long n;
  486. /* Don't allow overflow */
  487. if ((unsigned long) addr + count < count)
  488. count = -(unsigned long) addr;
  489. read_lock(&vmlist_lock);
  490. for (tmp = vmlist; tmp; tmp = tmp->next) {
  491. vaddr = (char *) tmp->addr;
  492. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  493. continue;
  494. while (addr < vaddr) {
  495. if (count == 0)
  496. goto finished;
  497. buf++;
  498. addr++;
  499. count--;
  500. }
  501. n = vaddr + tmp->size - PAGE_SIZE - addr;
  502. do {
  503. if (count == 0)
  504. goto finished;
  505. *addr = *buf;
  506. buf++;
  507. addr++;
  508. count--;
  509. } while (--n > 0);
  510. }
  511. finished:
  512. read_unlock(&vmlist_lock);
  513. return buf - buf_start;
  514. }