highmem.c 11 KB

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