highmem.c 7.8 KB

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