highmem.c 8.7 KB

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