highmem.c 20 KB

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