highmem.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * High memory handling common code and variables.
  3. *
  4. * (C) 1999 Andrea Arcangeli, SuSE GmbH, andrea@suse.de
  5. * Gerhard Wichert, Siemens AG, Gerhard.Wichert@pdb.siemens.de
  6. *
  7. *
  8. * Redesigned the x86 32-bit VM architecture to deal with
  9. * 64-bit physical space. With current x86 CPUs this
  10. * means up to 64 Gigabytes physical RAM.
  11. *
  12. * Rewrote high memory support to move the page cache into
  13. * high memory. Implemented permanent (schedulable) kmaps
  14. * based on Linus' idea.
  15. *
  16. * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
  17. */
  18. #include <linux/mm.h>
  19. #include <linux/module.h>
  20. #include <linux/swap.h>
  21. #include <linux/bio.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/mempool.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/init.h>
  26. #include <linux/hash.h>
  27. #include <linux/highmem.h>
  28. #include <linux/blktrace_api.h>
  29. #include <asm/tlbflush.h>
  30. /*
  31. * Virtual_count is not a pure "count".
  32. * 0 means that it is not mapped, and has not been mapped
  33. * since a TLB flush - it is usable.
  34. * 1 means that there are no users, but it has been mapped
  35. * since the last TLB flush - so we can't use it.
  36. * n means that there are (n-1) current users of it.
  37. */
  38. #ifdef CONFIG_HIGHMEM
  39. unsigned long totalhigh_pages __read_mostly;
  40. EXPORT_SYMBOL(totalhigh_pages);
  41. unsigned int nr_free_highpages (void)
  42. {
  43. pg_data_t *pgdat;
  44. unsigned int pages = 0;
  45. for_each_online_pgdat(pgdat) {
  46. pages += zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
  47. NR_FREE_PAGES);
  48. if (zone_movable_is_highmem())
  49. pages += zone_page_state(
  50. &pgdat->node_zones[ZONE_MOVABLE],
  51. NR_FREE_PAGES);
  52. }
  53. return pages;
  54. }
  55. static int pkmap_count[LAST_PKMAP];
  56. static unsigned int last_pkmap_nr;
  57. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
  58. pte_t * pkmap_page_table;
  59. static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
  60. static void flush_all_zero_pkmaps(void)
  61. {
  62. int i;
  63. flush_cache_kmaps();
  64. for (i = 0; i < LAST_PKMAP; i++) {
  65. struct page *page;
  66. /*
  67. * zero means we don't have anything to do,
  68. * >1 means that it is still in use. Only
  69. * a count of 1 means that it is free but
  70. * needs to be unmapped
  71. */
  72. if (pkmap_count[i] != 1)
  73. continue;
  74. pkmap_count[i] = 0;
  75. /* sanity check */
  76. BUG_ON(pte_none(pkmap_page_table[i]));
  77. /*
  78. * Don't need an atomic fetch-and-clear op here;
  79. * no-one has the page mapped, and cannot get at
  80. * its virtual address (and hence PTE) without first
  81. * getting the kmap_lock (which is held here).
  82. * So no dangers, even with speculative execution.
  83. */
  84. page = pte_page(pkmap_page_table[i]);
  85. pte_clear(&init_mm, (unsigned long)page_address(page),
  86. &pkmap_page_table[i]);
  87. set_page_address(page, NULL);
  88. }
  89. flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
  90. }
  91. /**
  92. * kmap_flush_unused - flush all unused kmap mappings in order to remove stray mappings
  93. */
  94. void kmap_flush_unused(void)
  95. {
  96. spin_lock(&kmap_lock);
  97. flush_all_zero_pkmaps();
  98. spin_unlock(&kmap_lock);
  99. }
  100. static inline unsigned long map_new_virtual(struct page *page)
  101. {
  102. unsigned long vaddr;
  103. int count;
  104. start:
  105. count = LAST_PKMAP;
  106. /* Find an empty entry */
  107. for (;;) {
  108. last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
  109. if (!last_pkmap_nr) {
  110. flush_all_zero_pkmaps();
  111. count = LAST_PKMAP;
  112. }
  113. if (!pkmap_count[last_pkmap_nr])
  114. break; /* Found a usable entry */
  115. if (--count)
  116. continue;
  117. /*
  118. * Sleep for somebody else to unmap their entries
  119. */
  120. {
  121. DECLARE_WAITQUEUE(wait, current);
  122. __set_current_state(TASK_UNINTERRUPTIBLE);
  123. add_wait_queue(&pkmap_map_wait, &wait);
  124. spin_unlock(&kmap_lock);
  125. schedule();
  126. remove_wait_queue(&pkmap_map_wait, &wait);
  127. spin_lock(&kmap_lock);
  128. /* Somebody else might have mapped it while we slept */
  129. if (page_address(page))
  130. return (unsigned long)page_address(page);
  131. /* Re-start */
  132. goto start;
  133. }
  134. }
  135. vaddr = PKMAP_ADDR(last_pkmap_nr);
  136. set_pte_at(&init_mm, vaddr,
  137. &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
  138. pkmap_count[last_pkmap_nr] = 1;
  139. set_page_address(page, (void *)vaddr);
  140. return vaddr;
  141. }
  142. /**
  143. * kmap_high - map a highmem page into memory
  144. * @page: &struct page to map
  145. *
  146. * Returns the page's virtual memory address.
  147. *
  148. * We cannot call this from interrupts, as it may block.
  149. */
  150. void *kmap_high(struct page *page)
  151. {
  152. unsigned long vaddr;
  153. /*
  154. * For highmem pages, we can't trust "virtual" until
  155. * after we have the lock.
  156. */
  157. spin_lock(&kmap_lock);
  158. vaddr = (unsigned long)page_address(page);
  159. if (!vaddr)
  160. vaddr = map_new_virtual(page);
  161. pkmap_count[PKMAP_NR(vaddr)]++;
  162. BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
  163. spin_unlock(&kmap_lock);
  164. return (void*) vaddr;
  165. }
  166. EXPORT_SYMBOL(kmap_high);
  167. /**
  168. * kunmap_high - map a highmem page into memory
  169. * @page: &struct page to unmap
  170. */
  171. void kunmap_high(struct page *page)
  172. {
  173. unsigned long vaddr;
  174. unsigned long nr;
  175. int need_wakeup;
  176. spin_lock(&kmap_lock);
  177. vaddr = (unsigned long)page_address(page);
  178. BUG_ON(!vaddr);
  179. nr = PKMAP_NR(vaddr);
  180. /*
  181. * A count must never go down to zero
  182. * without a TLB flush!
  183. */
  184. need_wakeup = 0;
  185. switch (--pkmap_count[nr]) {
  186. case 0:
  187. BUG();
  188. case 1:
  189. /*
  190. * Avoid an unnecessary wake_up() function call.
  191. * The common case is pkmap_count[] == 1, but
  192. * no waiters.
  193. * The tasks queued in the wait-queue are guarded
  194. * by both the lock in the wait-queue-head and by
  195. * the kmap_lock. As the kmap_lock is held here,
  196. * no need for the wait-queue-head's lock. Simply
  197. * test if the queue is empty.
  198. */
  199. need_wakeup = waitqueue_active(&pkmap_map_wait);
  200. }
  201. spin_unlock(&kmap_lock);
  202. /* do wake-up, if needed, race-free outside of the spin lock */
  203. if (need_wakeup)
  204. wake_up(&pkmap_map_wait);
  205. }
  206. EXPORT_SYMBOL(kunmap_high);
  207. #endif
  208. #if defined(HASHED_PAGE_VIRTUAL)
  209. #define PA_HASH_ORDER 7
  210. /*
  211. * Describes one page->virtual association
  212. */
  213. struct page_address_map {
  214. struct page *page;
  215. void *virtual;
  216. struct list_head list;
  217. };
  218. /*
  219. * page_address_map freelist, allocated from page_address_maps.
  220. */
  221. static struct list_head page_address_pool; /* freelist */
  222. static spinlock_t pool_lock; /* protects page_address_pool */
  223. /*
  224. * Hash table bucket
  225. */
  226. static struct page_address_slot {
  227. struct list_head lh; /* List of page_address_maps */
  228. spinlock_t lock; /* Protect this bucket's list */
  229. } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
  230. static struct page_address_slot *page_slot(struct page *page)
  231. {
  232. return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
  233. }
  234. /**
  235. * page_address - get the mapped virtual address of a page
  236. * @page: &struct page to get the virtual address of
  237. *
  238. * Returns the page's virtual address.
  239. */
  240. void *page_address(struct page *page)
  241. {
  242. unsigned long flags;
  243. void *ret;
  244. struct page_address_slot *pas;
  245. if (!PageHighMem(page))
  246. return lowmem_page_address(page);
  247. pas = page_slot(page);
  248. ret = NULL;
  249. spin_lock_irqsave(&pas->lock, flags);
  250. if (!list_empty(&pas->lh)) {
  251. struct page_address_map *pam;
  252. list_for_each_entry(pam, &pas->lh, list) {
  253. if (pam->page == page) {
  254. ret = pam->virtual;
  255. goto done;
  256. }
  257. }
  258. }
  259. done:
  260. spin_unlock_irqrestore(&pas->lock, flags);
  261. return ret;
  262. }
  263. EXPORT_SYMBOL(page_address);
  264. /**
  265. * set_page_address - set a page's virtual address
  266. * @page: &struct page to set
  267. * @virtual: virtual address to use
  268. */
  269. void set_page_address(struct page *page, void *virtual)
  270. {
  271. unsigned long flags;
  272. struct page_address_slot *pas;
  273. struct page_address_map *pam;
  274. BUG_ON(!PageHighMem(page));
  275. pas = page_slot(page);
  276. if (virtual) { /* Add */
  277. BUG_ON(list_empty(&page_address_pool));
  278. spin_lock_irqsave(&pool_lock, flags);
  279. pam = list_entry(page_address_pool.next,
  280. struct page_address_map, list);
  281. list_del(&pam->list);
  282. spin_unlock_irqrestore(&pool_lock, flags);
  283. pam->page = page;
  284. pam->virtual = virtual;
  285. spin_lock_irqsave(&pas->lock, flags);
  286. list_add_tail(&pam->list, &pas->lh);
  287. spin_unlock_irqrestore(&pas->lock, flags);
  288. } else { /* Remove */
  289. spin_lock_irqsave(&pas->lock, flags);
  290. list_for_each_entry(pam, &pas->lh, list) {
  291. if (pam->page == page) {
  292. list_del(&pam->list);
  293. spin_unlock_irqrestore(&pas->lock, flags);
  294. spin_lock_irqsave(&pool_lock, flags);
  295. list_add_tail(&pam->list, &page_address_pool);
  296. spin_unlock_irqrestore(&pool_lock, flags);
  297. goto done;
  298. }
  299. }
  300. spin_unlock_irqrestore(&pas->lock, flags);
  301. }
  302. done:
  303. return;
  304. }
  305. static struct page_address_map page_address_maps[LAST_PKMAP];
  306. void __init page_address_init(void)
  307. {
  308. int i;
  309. INIT_LIST_HEAD(&page_address_pool);
  310. for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
  311. list_add(&page_address_maps[i].list, &page_address_pool);
  312. for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
  313. INIT_LIST_HEAD(&page_address_htable[i].lh);
  314. spin_lock_init(&page_address_htable[i].lock);
  315. }
  316. spin_lock_init(&pool_lock);
  317. }
  318. #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */