highmem.c 11 KB

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