highmem.c 8.6 KB

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