highmem.c 9.9 KB

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