highmem.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * High memory handling common code and variables.
  4. *
  5. * (C) 1999 Andrea Arcangeli, SuSE GmbH, [email protected]
  6. * Gerhard Wichert, Siemens AG, [email protected]
  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 <[email protected]>
  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/init.h>
  26. #include <linux/hash.h>
  27. #include <linux/highmem.h>
  28. #include <linux/kgdb.h>
  29. #include <asm/tlbflush.h>
  30. #include <linux/vmalloc.h>
  31. #ifdef CONFIG_KMAP_LOCAL
  32. static inline int kmap_local_calc_idx(int idx)
  33. {
  34. return idx + KM_MAX_IDX * smp_processor_id();
  35. }
  36. #ifndef arch_kmap_local_map_idx
  37. #define arch_kmap_local_map_idx(idx, pfn) kmap_local_calc_idx(idx)
  38. #endif
  39. #endif /* CONFIG_KMAP_LOCAL */
  40. /*
  41. * Virtual_count is not a pure "count".
  42. * 0 means that it is not mapped, and has not been mapped
  43. * since a TLB flush - it is usable.
  44. * 1 means that there are no users, but it has been mapped
  45. * since the last TLB flush - so we can't use it.
  46. * n means that there are (n-1) current users of it.
  47. */
  48. #ifdef CONFIG_HIGHMEM
  49. /*
  50. * Architecture with aliasing data cache may define the following family of
  51. * helper functions in its asm/highmem.h to control cache color of virtual
  52. * addresses where physical memory pages are mapped by kmap.
  53. */
  54. #ifndef get_pkmap_color
  55. /*
  56. * Determine color of virtual address where the page should be mapped.
  57. */
  58. static inline unsigned int get_pkmap_color(struct page *page)
  59. {
  60. return 0;
  61. }
  62. #define get_pkmap_color get_pkmap_color
  63. /*
  64. * Get next index for mapping inside PKMAP region for page with given color.
  65. */
  66. static inline unsigned int get_next_pkmap_nr(unsigned int color)
  67. {
  68. static unsigned int last_pkmap_nr;
  69. last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
  70. return last_pkmap_nr;
  71. }
  72. /*
  73. * Determine if page index inside PKMAP region (pkmap_nr) of given color
  74. * has wrapped around PKMAP region end. When this happens an attempt to
  75. * flush all unused PKMAP slots is made.
  76. */
  77. static inline int no_more_pkmaps(unsigned int pkmap_nr, unsigned int color)
  78. {
  79. return pkmap_nr == 0;
  80. }
  81. /*
  82. * Get the number of PKMAP entries of the given color. If no free slot is
  83. * found after checking that many entries, kmap will sleep waiting for
  84. * someone to call kunmap and free PKMAP slot.
  85. */
  86. static inline int get_pkmap_entries_count(unsigned int color)
  87. {
  88. return LAST_PKMAP;
  89. }
  90. /*
  91. * Get head of a wait queue for PKMAP entries of the given color.
  92. * Wait queues for different mapping colors should be independent to avoid
  93. * unnecessary wakeups caused by freeing of slots of other colors.
  94. */
  95. static inline wait_queue_head_t *get_pkmap_wait_queue_head(unsigned int color)
  96. {
  97. static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
  98. return &pkmap_map_wait;
  99. }
  100. #endif
  101. atomic_long_t _totalhigh_pages __read_mostly;
  102. EXPORT_SYMBOL(_totalhigh_pages);
  103. unsigned int __nr_free_highpages(void)
  104. {
  105. struct zone *zone;
  106. unsigned int pages = 0;
  107. for_each_populated_zone(zone) {
  108. if (is_highmem(zone))
  109. pages += zone_page_state(zone, NR_FREE_PAGES);
  110. }
  111. return pages;
  112. }
  113. static int pkmap_count[LAST_PKMAP];
  114. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
  115. pte_t *pkmap_page_table;
  116. /*
  117. * Most architectures have no use for kmap_high_get(), so let's abstract
  118. * the disabling of IRQ out of the locking in that case to save on a
  119. * potential useless overhead.
  120. */
  121. #ifdef ARCH_NEEDS_KMAP_HIGH_GET
  122. #define lock_kmap() spin_lock_irq(&kmap_lock)
  123. #define unlock_kmap() spin_unlock_irq(&kmap_lock)
  124. #define lock_kmap_any(flags) spin_lock_irqsave(&kmap_lock, flags)
  125. #define unlock_kmap_any(flags) spin_unlock_irqrestore(&kmap_lock, flags)
  126. #else
  127. #define lock_kmap() spin_lock(&kmap_lock)
  128. #define unlock_kmap() spin_unlock(&kmap_lock)
  129. #define lock_kmap_any(flags) \
  130. do { spin_lock(&kmap_lock); (void)(flags); } while (0)
  131. #define unlock_kmap_any(flags) \
  132. do { spin_unlock(&kmap_lock); (void)(flags); } while (0)
  133. #endif
  134. struct page *__kmap_to_page(void *vaddr)
  135. {
  136. unsigned long base = (unsigned long) vaddr & PAGE_MASK;
  137. struct kmap_ctrl *kctrl = &current->kmap_ctrl;
  138. unsigned long addr = (unsigned long)vaddr;
  139. int i;
  140. /* kmap() mappings */
  141. if (WARN_ON_ONCE(addr >= PKMAP_ADDR(0) &&
  142. addr < PKMAP_ADDR(LAST_PKMAP)))
  143. return pte_page(pkmap_page_table[PKMAP_NR(addr)]);
  144. /* kmap_local_page() mappings */
  145. if (WARN_ON_ONCE(base >= __fix_to_virt(FIX_KMAP_END) &&
  146. base < __fix_to_virt(FIX_KMAP_BEGIN))) {
  147. for (i = 0; i < kctrl->idx; i++) {
  148. unsigned long base_addr;
  149. int idx;
  150. idx = arch_kmap_local_map_idx(i, pte_pfn(pteval));
  151. base_addr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  152. if (base_addr == base)
  153. return pte_page(kctrl->pteval[i]);
  154. }
  155. }
  156. return virt_to_page(vaddr);
  157. }
  158. EXPORT_SYMBOL(__kmap_to_page);
  159. static void flush_all_zero_pkmaps(void)
  160. {
  161. int i;
  162. int need_flush = 0;
  163. flush_cache_kmaps();
  164. for (i = 0; i < LAST_PKMAP; i++) {
  165. struct page *page;
  166. /*
  167. * zero means we don't have anything to do,
  168. * >1 means that it is still in use. Only
  169. * a count of 1 means that it is free but
  170. * needs to be unmapped
  171. */
  172. if (pkmap_count[i] != 1)
  173. continue;
  174. pkmap_count[i] = 0;
  175. /* sanity check */
  176. BUG_ON(pte_none(pkmap_page_table[i]));
  177. /*
  178. * Don't need an atomic fetch-and-clear op here;
  179. * no-one has the page mapped, and cannot get at
  180. * its virtual address (and hence PTE) without first
  181. * getting the kmap_lock (which is held here).
  182. * So no dangers, even with speculative execution.
  183. */
  184. page = pte_page(pkmap_page_table[i]);
  185. pte_clear(&init_mm, PKMAP_ADDR(i), &pkmap_page_table[i]);
  186. set_page_address(page, NULL);
  187. need_flush = 1;
  188. }
  189. if (need_flush)
  190. flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
  191. }
  192. void __kmap_flush_unused(void)
  193. {
  194. lock_kmap();
  195. flush_all_zero_pkmaps();
  196. unlock_kmap();
  197. }
  198. static inline unsigned long map_new_virtual(struct page *page)
  199. {
  200. unsigned long vaddr;
  201. int count;
  202. unsigned int last_pkmap_nr;
  203. unsigned int color = get_pkmap_color(page);
  204. start:
  205. count = get_pkmap_entries_count(color);
  206. /* Find an empty entry */
  207. for (;;) {
  208. last_pkmap_nr = get_next_pkmap_nr(color);
  209. if (no_more_pkmaps(last_pkmap_nr, color)) {
  210. flush_all_zero_pkmaps();
  211. count = get_pkmap_entries_count(color);
  212. }
  213. if (!pkmap_count[last_pkmap_nr])
  214. break; /* Found a usable entry */
  215. if (--count)
  216. continue;
  217. /*
  218. * Sleep for somebody else to unmap their entries
  219. */
  220. {
  221. DECLARE_WAITQUEUE(wait, current);
  222. wait_queue_head_t *pkmap_map_wait =
  223. get_pkmap_wait_queue_head(color);
  224. __set_current_state(TASK_UNINTERRUPTIBLE);
  225. add_wait_queue(pkmap_map_wait, &wait);
  226. unlock_kmap();
  227. schedule();
  228. remove_wait_queue(pkmap_map_wait, &wait);
  229. lock_kmap();
  230. /* Somebody else might have mapped it while we slept */
  231. if (page_address(page))
  232. return (unsigned long)page_address(page);
  233. /* Re-start */
  234. goto start;
  235. }
  236. }
  237. vaddr = PKMAP_ADDR(last_pkmap_nr);
  238. set_pte_at(&init_mm, vaddr,
  239. &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
  240. pkmap_count[last_pkmap_nr] = 1;
  241. set_page_address(page, (void *)vaddr);
  242. return vaddr;
  243. }
  244. /**
  245. * kmap_high - map a highmem page into memory
  246. * @page: &struct page to map
  247. *
  248. * Returns the page's virtual memory address.
  249. *
  250. * We cannot call this from interrupts, as it may block.
  251. */
  252. void *kmap_high(struct page *page)
  253. {
  254. unsigned long vaddr;
  255. /*
  256. * For highmem pages, we can't trust "virtual" until
  257. * after we have the lock.
  258. */
  259. lock_kmap();
  260. vaddr = (unsigned long)page_address(page);
  261. if (!vaddr)
  262. vaddr = map_new_virtual(page);
  263. pkmap_count[PKMAP_NR(vaddr)]++;
  264. BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
  265. unlock_kmap();
  266. return (void *) vaddr;
  267. }
  268. EXPORT_SYMBOL(kmap_high);
  269. #ifdef ARCH_NEEDS_KMAP_HIGH_GET
  270. /**
  271. * kmap_high_get - pin a highmem page into memory
  272. * @page: &struct page to pin
  273. *
  274. * Returns the page's current virtual memory address, or NULL if no mapping
  275. * exists. If and only if a non null address is returned then a
  276. * matching call to kunmap_high() is necessary.
  277. *
  278. * This can be called from any context.
  279. */
  280. void *kmap_high_get(struct page *page)
  281. {
  282. unsigned long vaddr, flags;
  283. lock_kmap_any(flags);
  284. vaddr = (unsigned long)page_address(page);
  285. if (vaddr) {
  286. BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 1);
  287. pkmap_count[PKMAP_NR(vaddr)]++;
  288. }
  289. unlock_kmap_any(flags);
  290. return (void *) vaddr;
  291. }
  292. #endif
  293. /**
  294. * kunmap_high - unmap a highmem page into memory
  295. * @page: &struct page to unmap
  296. *
  297. * If ARCH_NEEDS_KMAP_HIGH_GET is not defined then this may be called
  298. * only from user context.
  299. */
  300. void kunmap_high(struct page *page)
  301. {
  302. unsigned long vaddr;
  303. unsigned long nr;
  304. unsigned long flags;
  305. int need_wakeup;
  306. unsigned int color = get_pkmap_color(page);
  307. wait_queue_head_t *pkmap_map_wait;
  308. lock_kmap_any(flags);
  309. vaddr = (unsigned long)page_address(page);
  310. BUG_ON(!vaddr);
  311. nr = PKMAP_NR(vaddr);
  312. /*
  313. * A count must never go down to zero
  314. * without a TLB flush!
  315. */
  316. need_wakeup = 0;
  317. switch (--pkmap_count[nr]) {
  318. case 0:
  319. BUG();
  320. case 1:
  321. /*
  322. * Avoid an unnecessary wake_up() function call.
  323. * The common case is pkmap_count[] == 1, but
  324. * no waiters.
  325. * The tasks queued in the wait-queue are guarded
  326. * by both the lock in the wait-queue-head and by
  327. * the kmap_lock. As the kmap_lock is held here,
  328. * no need for the wait-queue-head's lock. Simply
  329. * test if the queue is empty.
  330. */
  331. pkmap_map_wait = get_pkmap_wait_queue_head(color);
  332. need_wakeup = waitqueue_active(pkmap_map_wait);
  333. }
  334. unlock_kmap_any(flags);
  335. /* do wake-up, if needed, race-free outside of the spin lock */
  336. if (need_wakeup)
  337. wake_up(pkmap_map_wait);
  338. }
  339. EXPORT_SYMBOL(kunmap_high);
  340. void zero_user_segments(struct page *page, unsigned start1, unsigned end1,
  341. unsigned start2, unsigned end2)
  342. {
  343. unsigned int i;
  344. BUG_ON(end1 > page_size(page) || end2 > page_size(page));
  345. if (start1 >= end1)
  346. start1 = end1 = 0;
  347. if (start2 >= end2)
  348. start2 = end2 = 0;
  349. for (i = 0; i < compound_nr(page); i++) {
  350. void *kaddr = NULL;
  351. if (start1 >= PAGE_SIZE) {
  352. start1 -= PAGE_SIZE;
  353. end1 -= PAGE_SIZE;
  354. } else {
  355. unsigned this_end = min_t(unsigned, end1, PAGE_SIZE);
  356. if (end1 > start1) {
  357. kaddr = kmap_local_page(page + i);
  358. memset(kaddr + start1, 0, this_end - start1);
  359. }
  360. end1 -= this_end;
  361. start1 = 0;
  362. }
  363. if (start2 >= PAGE_SIZE) {
  364. start2 -= PAGE_SIZE;
  365. end2 -= PAGE_SIZE;
  366. } else {
  367. unsigned this_end = min_t(unsigned, end2, PAGE_SIZE);
  368. if (end2 > start2) {
  369. if (!kaddr)
  370. kaddr = kmap_local_page(page + i);
  371. memset(kaddr + start2, 0, this_end - start2);
  372. }
  373. end2 -= this_end;
  374. start2 = 0;
  375. }
  376. if (kaddr) {
  377. kunmap_local(kaddr);
  378. flush_dcache_page(page + i);
  379. }
  380. if (!end1 && !end2)
  381. break;
  382. }
  383. BUG_ON((start1 | start2 | end1 | end2) != 0);
  384. }
  385. EXPORT_SYMBOL(zero_user_segments);
  386. #endif /* CONFIG_HIGHMEM */
  387. #ifdef CONFIG_KMAP_LOCAL
  388. #include <asm/kmap_size.h>
  389. /*
  390. * With DEBUG_KMAP_LOCAL the stack depth is doubled and every second
  391. * slot is unused which acts as a guard page
  392. */
  393. #ifdef CONFIG_DEBUG_KMAP_LOCAL
  394. # define KM_INCR 2
  395. #else
  396. # define KM_INCR 1
  397. #endif
  398. static inline int kmap_local_idx_push(void)
  399. {
  400. WARN_ON_ONCE(in_hardirq() && !irqs_disabled());
  401. current->kmap_ctrl.idx += KM_INCR;
  402. BUG_ON(current->kmap_ctrl.idx >= KM_MAX_IDX);
  403. return current->kmap_ctrl.idx - 1;
  404. }
  405. static inline int kmap_local_idx(void)
  406. {
  407. return current->kmap_ctrl.idx - 1;
  408. }
  409. static inline void kmap_local_idx_pop(void)
  410. {
  411. current->kmap_ctrl.idx -= KM_INCR;
  412. BUG_ON(current->kmap_ctrl.idx < 0);
  413. }
  414. #ifndef arch_kmap_local_post_map
  415. # define arch_kmap_local_post_map(vaddr, pteval) do { } while (0)
  416. #endif
  417. #ifndef arch_kmap_local_pre_unmap
  418. # define arch_kmap_local_pre_unmap(vaddr) do { } while (0)
  419. #endif
  420. #ifndef arch_kmap_local_post_unmap
  421. # define arch_kmap_local_post_unmap(vaddr) do { } while (0)
  422. #endif
  423. #ifndef arch_kmap_local_unmap_idx
  424. #define arch_kmap_local_unmap_idx(idx, vaddr) kmap_local_calc_idx(idx)
  425. #endif
  426. #ifndef arch_kmap_local_high_get
  427. static inline void *arch_kmap_local_high_get(struct page *page)
  428. {
  429. return NULL;
  430. }
  431. #endif
  432. #ifndef arch_kmap_local_set_pte
  433. #define arch_kmap_local_set_pte(mm, vaddr, ptep, ptev) \
  434. set_pte_at(mm, vaddr, ptep, ptev)
  435. #endif
  436. /* Unmap a local mapping which was obtained by kmap_high_get() */
  437. static inline bool kmap_high_unmap_local(unsigned long vaddr)
  438. {
  439. #ifdef ARCH_NEEDS_KMAP_HIGH_GET
  440. if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP)) {
  441. kunmap_high(pte_page(pkmap_page_table[PKMAP_NR(vaddr)]));
  442. return true;
  443. }
  444. #endif
  445. return false;
  446. }
  447. static pte_t *__kmap_pte;
  448. static pte_t *kmap_get_pte(unsigned long vaddr, int idx)
  449. {
  450. if (IS_ENABLED(CONFIG_KMAP_LOCAL_NON_LINEAR_PTE_ARRAY))
  451. /*
  452. * Set by the arch if __kmap_pte[-idx] does not produce
  453. * the correct entry.
  454. */
  455. return virt_to_kpte(vaddr);
  456. if (!__kmap_pte)
  457. __kmap_pte = virt_to_kpte(__fix_to_virt(FIX_KMAP_BEGIN));
  458. return &__kmap_pte[-idx];
  459. }
  460. void *__kmap_local_pfn_prot(unsigned long pfn, pgprot_t prot)
  461. {
  462. pte_t pteval, *kmap_pte;
  463. unsigned long vaddr;
  464. int idx;
  465. /*
  466. * Disable migration so resulting virtual address is stable
  467. * across preemption.
  468. */
  469. migrate_disable();
  470. preempt_disable();
  471. idx = arch_kmap_local_map_idx(kmap_local_idx_push(), pfn);
  472. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  473. kmap_pte = kmap_get_pte(vaddr, idx);
  474. BUG_ON(!pte_none(*kmap_pte));
  475. pteval = pfn_pte(pfn, prot);
  476. arch_kmap_local_set_pte(&init_mm, vaddr, kmap_pte, pteval);
  477. arch_kmap_local_post_map(vaddr, pteval);
  478. current->kmap_ctrl.pteval[kmap_local_idx()] = pteval;
  479. preempt_enable();
  480. return (void *)vaddr;
  481. }
  482. EXPORT_SYMBOL_GPL(__kmap_local_pfn_prot);
  483. void *__kmap_local_page_prot(struct page *page, pgprot_t prot)
  484. {
  485. void *kmap;
  486. /*
  487. * To broaden the usage of the actual kmap_local() machinery always map
  488. * pages when debugging is enabled and the architecture has no problems
  489. * with alias mappings.
  490. */
  491. if (!IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP) && !PageHighMem(page))
  492. return page_address(page);
  493. /* Try kmap_high_get() if architecture has it enabled */
  494. kmap = arch_kmap_local_high_get(page);
  495. if (kmap)
  496. return kmap;
  497. return __kmap_local_pfn_prot(page_to_pfn(page), prot);
  498. }
  499. EXPORT_SYMBOL(__kmap_local_page_prot);
  500. void kunmap_local_indexed(const void *vaddr)
  501. {
  502. unsigned long addr = (unsigned long) vaddr & PAGE_MASK;
  503. pte_t *kmap_pte;
  504. int idx;
  505. if (addr < __fix_to_virt(FIX_KMAP_END) ||
  506. addr > __fix_to_virt(FIX_KMAP_BEGIN)) {
  507. if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP)) {
  508. /* This _should_ never happen! See above. */
  509. WARN_ON_ONCE(1);
  510. return;
  511. }
  512. /*
  513. * Handle mappings which were obtained by kmap_high_get()
  514. * first as the virtual address of such mappings is below
  515. * PAGE_OFFSET. Warn for all other addresses which are in
  516. * the user space part of the virtual address space.
  517. */
  518. if (!kmap_high_unmap_local(addr))
  519. WARN_ON_ONCE(addr < PAGE_OFFSET);
  520. return;
  521. }
  522. preempt_disable();
  523. idx = arch_kmap_local_unmap_idx(kmap_local_idx(), addr);
  524. WARN_ON_ONCE(addr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
  525. kmap_pte = kmap_get_pte(addr, idx);
  526. arch_kmap_local_pre_unmap(addr);
  527. pte_clear(&init_mm, addr, kmap_pte);
  528. arch_kmap_local_post_unmap(addr);
  529. current->kmap_ctrl.pteval[kmap_local_idx()] = __pte(0);
  530. kmap_local_idx_pop();
  531. preempt_enable();
  532. migrate_enable();
  533. }
  534. EXPORT_SYMBOL(kunmap_local_indexed);
  535. /*
  536. * Invoked before switch_to(). This is safe even when during or after
  537. * clearing the maps an interrupt which needs a kmap_local happens because
  538. * the task::kmap_ctrl.idx is not modified by the unmapping code so a
  539. * nested kmap_local will use the next unused index and restore the index
  540. * on unmap. The already cleared kmaps of the outgoing task are irrelevant
  541. * because the interrupt context does not know about them. The same applies
  542. * when scheduling back in for an interrupt which happens before the
  543. * restore is complete.
  544. */
  545. void __kmap_local_sched_out(void)
  546. {
  547. struct task_struct *tsk = current;
  548. pte_t *kmap_pte;
  549. int i;
  550. /* Clear kmaps */
  551. for (i = 0; i < tsk->kmap_ctrl.idx; i++) {
  552. pte_t pteval = tsk->kmap_ctrl.pteval[i];
  553. unsigned long addr;
  554. int idx;
  555. /* With debug all even slots are unmapped and act as guard */
  556. if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL) && !(i & 0x01)) {
  557. WARN_ON_ONCE(pte_val(pteval) != 0);
  558. continue;
  559. }
  560. if (WARN_ON_ONCE(pte_none(pteval)))
  561. continue;
  562. /*
  563. * This is a horrible hack for XTENSA to calculate the
  564. * coloured PTE index. Uses the PFN encoded into the pteval
  565. * and the map index calculation because the actual mapped
  566. * virtual address is not stored in task::kmap_ctrl.
  567. * For any sane architecture this is optimized out.
  568. */
  569. idx = arch_kmap_local_map_idx(i, pte_pfn(pteval));
  570. addr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  571. kmap_pte = kmap_get_pte(addr, idx);
  572. arch_kmap_local_pre_unmap(addr);
  573. pte_clear(&init_mm, addr, kmap_pte);
  574. arch_kmap_local_post_unmap(addr);
  575. }
  576. }
  577. void __kmap_local_sched_in(void)
  578. {
  579. struct task_struct *tsk = current;
  580. pte_t *kmap_pte;
  581. int i;
  582. /* Restore kmaps */
  583. for (i = 0; i < tsk->kmap_ctrl.idx; i++) {
  584. pte_t pteval = tsk->kmap_ctrl.pteval[i];
  585. unsigned long addr;
  586. int idx;
  587. /* With debug all even slots are unmapped and act as guard */
  588. if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL) && !(i & 0x01)) {
  589. WARN_ON_ONCE(pte_val(pteval) != 0);
  590. continue;
  591. }
  592. if (WARN_ON_ONCE(pte_none(pteval)))
  593. continue;
  594. /* See comment in __kmap_local_sched_out() */
  595. idx = arch_kmap_local_map_idx(i, pte_pfn(pteval));
  596. addr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  597. kmap_pte = kmap_get_pte(addr, idx);
  598. set_pte_at(&init_mm, addr, kmap_pte, pteval);
  599. arch_kmap_local_post_map(addr, pteval);
  600. }
  601. }
  602. void kmap_local_fork(struct task_struct *tsk)
  603. {
  604. if (WARN_ON_ONCE(tsk->kmap_ctrl.idx))
  605. memset(&tsk->kmap_ctrl, 0, sizeof(tsk->kmap_ctrl));
  606. }
  607. #endif
  608. #if defined(HASHED_PAGE_VIRTUAL)
  609. #define PA_HASH_ORDER 7
  610. /*
  611. * Describes one page->virtual association
  612. */
  613. struct page_address_map {
  614. struct page *page;
  615. void *virtual;
  616. struct list_head list;
  617. };
  618. static struct page_address_map page_address_maps[LAST_PKMAP];
  619. /*
  620. * Hash table bucket
  621. */
  622. static struct page_address_slot {
  623. struct list_head lh; /* List of page_address_maps */
  624. spinlock_t lock; /* Protect this bucket's list */
  625. } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
  626. static struct page_address_slot *page_slot(const struct page *page)
  627. {
  628. return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
  629. }
  630. /**
  631. * page_address - get the mapped virtual address of a page
  632. * @page: &struct page to get the virtual address of
  633. *
  634. * Returns the page's virtual address.
  635. */
  636. void *page_address(const struct page *page)
  637. {
  638. unsigned long flags;
  639. void *ret;
  640. struct page_address_slot *pas;
  641. if (!PageHighMem(page))
  642. return lowmem_page_address(page);
  643. pas = page_slot(page);
  644. ret = NULL;
  645. spin_lock_irqsave(&pas->lock, flags);
  646. if (!list_empty(&pas->lh)) {
  647. struct page_address_map *pam;
  648. list_for_each_entry(pam, &pas->lh, list) {
  649. if (pam->page == page) {
  650. ret = pam->virtual;
  651. break;
  652. }
  653. }
  654. }
  655. spin_unlock_irqrestore(&pas->lock, flags);
  656. return ret;
  657. }
  658. EXPORT_SYMBOL(page_address);
  659. /**
  660. * set_page_address - set a page's virtual address
  661. * @page: &struct page to set
  662. * @virtual: virtual address to use
  663. */
  664. void set_page_address(struct page *page, void *virtual)
  665. {
  666. unsigned long flags;
  667. struct page_address_slot *pas;
  668. struct page_address_map *pam;
  669. BUG_ON(!PageHighMem(page));
  670. pas = page_slot(page);
  671. if (virtual) { /* Add */
  672. pam = &page_address_maps[PKMAP_NR((unsigned long)virtual)];
  673. pam->page = page;
  674. pam->virtual = virtual;
  675. spin_lock_irqsave(&pas->lock, flags);
  676. list_add_tail(&pam->list, &pas->lh);
  677. spin_unlock_irqrestore(&pas->lock, flags);
  678. } else { /* Remove */
  679. spin_lock_irqsave(&pas->lock, flags);
  680. list_for_each_entry(pam, &pas->lh, list) {
  681. if (pam->page == page) {
  682. list_del(&pam->list);
  683. break;
  684. }
  685. }
  686. spin_unlock_irqrestore(&pas->lock, flags);
  687. }
  688. return;
  689. }
  690. void __init page_address_init(void)
  691. {
  692. int i;
  693. for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
  694. INIT_LIST_HEAD(&page_address_htable[i].lh);
  695. spin_lock_init(&page_address_htable[i].lock);
  696. }
  697. }
  698. #endif /* defined(HASHED_PAGE_VIRTUAL) */