highmem.c 12 KB

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