highmem.c 12 KB

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