highmem.c 11 KB

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