highmem.c 10 KB

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