highmem.c 20 KB

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