vmalloc.c 13 KB

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