1 // SPDX-License-Identifier: GPL-2.0
3 * High memory handling common code and variables.
5 * (C) 1999 Andrea Arcangeli, SuSE GmbH, andrea@suse.de
6 * Gerhard Wichert, Siemens AG, Gerhard.Wichert@pdb.siemens.de
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.
13 * Rewrote high memory support to move the page cache into
14 * high memory. Implemented permanent (schedulable) kmaps
15 * based on Linus' idea.
17 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
21 #include <linux/export.h>
22 #include <linux/swap.h>
23 #include <linux/bio.h>
24 #include <linux/pagemap.h>
25 #include <linux/mempool.h>
26 #include <linux/blkdev.h>
27 #include <linux/init.h>
28 #include <linux/hash.h>
29 #include <linux/highmem.h>
30 #include <linux/kgdb.h>
31 #include <asm/tlbflush.h>
32 #include <linux/vmalloc.h>
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.
45 * Architecture with aliasing data cache may define the following family of
46 * helper functions in its asm/highmem.h to control cache color of virtual
47 * addresses where physical memory pages are mapped by kmap.
49 #ifndef get_pkmap_color
52 * Determine color of virtual address where the page should be mapped.
54 static inline unsigned int get_pkmap_color(struct page *page)
58 #define get_pkmap_color get_pkmap_color
61 * Get next index for mapping inside PKMAP region for page with given color.
63 static inline unsigned int get_next_pkmap_nr(unsigned int color)
65 static unsigned int last_pkmap_nr;
67 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
72 * Determine if page index inside PKMAP region (pkmap_nr) of given color
73 * has wrapped around PKMAP region end. When this happens an attempt to
74 * flush all unused PKMAP slots is made.
76 static inline int no_more_pkmaps(unsigned int pkmap_nr, unsigned int color)
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.
86 static inline int get_pkmap_entries_count(unsigned int color)
92 * Get head of a wait queue for PKMAP entries of the given color.
93 * Wait queues for different mapping colors should be independent to avoid
94 * unnecessary wakeups caused by freeing of slots of other colors.
96 static inline wait_queue_head_t *get_pkmap_wait_queue_head(unsigned int color)
98 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
100 return &pkmap_map_wait;
104 atomic_long_t _totalhigh_pages __read_mostly;
105 EXPORT_SYMBOL(_totalhigh_pages);
107 unsigned int __nr_free_highpages(void)
110 unsigned int pages = 0;
112 for_each_populated_zone(zone) {
113 if (is_highmem(zone))
114 pages += zone_page_state(zone, NR_FREE_PAGES);
120 static int pkmap_count[LAST_PKMAP];
121 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
123 pte_t *pkmap_page_table;
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.
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)
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)
144 struct page *__kmap_to_page(void *vaddr)
146 unsigned long addr = (unsigned long)vaddr;
148 if (addr >= PKMAP_ADDR(0) && addr < PKMAP_ADDR(LAST_PKMAP)) {
149 int i = PKMAP_NR(addr);
151 return pte_page(pkmap_page_table[i]);
154 return virt_to_page(addr);
156 EXPORT_SYMBOL(__kmap_to_page);
158 static void flush_all_zero_pkmaps(void)
165 for (i = 0; i < LAST_PKMAP; i++) {
169 * zero means we don't have anything to do,
170 * >1 means that it is still in use. Only
171 * a count of 1 means that it is free but
172 * needs to be unmapped
174 if (pkmap_count[i] != 1)
179 BUG_ON(pte_none(pkmap_page_table[i]));
182 * Don't need an atomic fetch-and-clear op here;
183 * no-one has the page mapped, and cannot get at
184 * its virtual address (and hence PTE) without first
185 * getting the kmap_lock (which is held here).
186 * So no dangers, even with speculative execution.
188 page = pte_page(pkmap_page_table[i]);
189 pte_clear(&init_mm, PKMAP_ADDR(i), &pkmap_page_table[i]);
191 set_page_address(page, NULL);
195 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
198 void __kmap_flush_unused(void)
201 flush_all_zero_pkmaps();
205 static inline unsigned long map_new_virtual(struct page *page)
209 unsigned int last_pkmap_nr;
210 unsigned int color = get_pkmap_color(page);
213 count = get_pkmap_entries_count(color);
214 /* Find an empty entry */
216 last_pkmap_nr = get_next_pkmap_nr(color);
217 if (no_more_pkmaps(last_pkmap_nr, color)) {
218 flush_all_zero_pkmaps();
219 count = get_pkmap_entries_count(color);
221 if (!pkmap_count[last_pkmap_nr])
222 break; /* Found a usable entry */
227 * Sleep for somebody else to unmap their entries
230 DECLARE_WAITQUEUE(wait, current);
231 wait_queue_head_t *pkmap_map_wait =
232 get_pkmap_wait_queue_head(color);
234 __set_current_state(TASK_UNINTERRUPTIBLE);
235 add_wait_queue(pkmap_map_wait, &wait);
238 remove_wait_queue(pkmap_map_wait, &wait);
241 /* Somebody else might have mapped it while we slept */
242 if (page_address(page))
243 return (unsigned long)page_address(page);
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));
253 pkmap_count[last_pkmap_nr] = 1;
254 set_page_address(page, (void *)vaddr);
260 * kmap_high - map a highmem page into memory
261 * @page: &struct page to map
263 * Returns the page's virtual memory address.
265 * We cannot call this from interrupts, as it may block.
267 void *kmap_high(struct page *page)
272 * For highmem pages, we can't trust "virtual" until
273 * after we have the lock.
276 vaddr = (unsigned long)page_address(page);
278 vaddr = map_new_virtual(page);
279 pkmap_count[PKMAP_NR(vaddr)]++;
280 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
282 return (void *) vaddr;
284 EXPORT_SYMBOL(kmap_high);
286 #ifdef ARCH_NEEDS_KMAP_HIGH_GET
288 * kmap_high_get - pin a highmem page into memory
289 * @page: &struct page to pin
291 * Returns the page's current virtual memory address, or NULL if no mapping
292 * exists. If and only if a non null address is returned then a
293 * matching call to kunmap_high() is necessary.
295 * This can be called from any context.
297 void *kmap_high_get(struct page *page)
299 unsigned long vaddr, flags;
301 lock_kmap_any(flags);
302 vaddr = (unsigned long)page_address(page);
304 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 1);
305 pkmap_count[PKMAP_NR(vaddr)]++;
307 unlock_kmap_any(flags);
308 return (void *) vaddr;
313 * kunmap_high - unmap a highmem page into memory
314 * @page: &struct page to unmap
316 * If ARCH_NEEDS_KMAP_HIGH_GET is not defined then this may be called
317 * only from user context.
319 void kunmap_high(struct page *page)
325 unsigned int color = get_pkmap_color(page);
326 wait_queue_head_t *pkmap_map_wait;
328 lock_kmap_any(flags);
329 vaddr = (unsigned long)page_address(page);
331 nr = PKMAP_NR(vaddr);
334 * A count must never go down to zero
335 * without a TLB flush!
338 switch (--pkmap_count[nr]) {
343 * Avoid an unnecessary wake_up() function call.
344 * The common case is pkmap_count[] == 1, but
346 * The tasks queued in the wait-queue are guarded
347 * by both the lock in the wait-queue-head and by
348 * the kmap_lock. As the kmap_lock is held here,
349 * no need for the wait-queue-head's lock. Simply
350 * test if the queue is empty.
352 pkmap_map_wait = get_pkmap_wait_queue_head(color);
353 need_wakeup = waitqueue_active(pkmap_map_wait);
355 unlock_kmap_any(flags);
357 /* do wake-up, if needed, race-free outside of the spin lock */
359 wake_up(pkmap_map_wait);
361 EXPORT_SYMBOL(kunmap_high);
363 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
364 void zero_user_segments(struct page *page, unsigned start1, unsigned end1,
365 unsigned start2, unsigned end2)
369 BUG_ON(end1 > page_size(page) || end2 > page_size(page));
376 for (i = 0; i < compound_nr(page); i++) {
379 if (start1 >= PAGE_SIZE) {
383 unsigned this_end = min_t(unsigned, end1, PAGE_SIZE);
386 kaddr = kmap_atomic(page + i);
387 memset(kaddr + start1, 0, this_end - start1);
393 if (start2 >= PAGE_SIZE) {
397 unsigned this_end = min_t(unsigned, end2, PAGE_SIZE);
401 kaddr = kmap_atomic(page + i);
402 memset(kaddr + start2, 0, this_end - start2);
409 kunmap_atomic(kaddr);
410 flush_dcache_page(page + i);
417 BUG_ON((start1 | start2 | end1 | end2) != 0);
419 EXPORT_SYMBOL(zero_user_segments);
420 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
421 #endif /* CONFIG_HIGHMEM */
423 #ifdef CONFIG_KMAP_LOCAL
425 #include <asm/kmap_size.h>
428 * With DEBUG_KMAP_LOCAL the stack depth is doubled and every second
429 * slot is unused which acts as a guard page
431 #ifdef CONFIG_DEBUG_KMAP_LOCAL
437 static inline int kmap_local_idx_push(void)
439 WARN_ON_ONCE(in_hardirq() && !irqs_disabled());
440 current->kmap_ctrl.idx += KM_INCR;
441 BUG_ON(current->kmap_ctrl.idx >= KM_MAX_IDX);
442 return current->kmap_ctrl.idx - 1;
445 static inline int kmap_local_idx(void)
447 return current->kmap_ctrl.idx - 1;
450 static inline void kmap_local_idx_pop(void)
452 current->kmap_ctrl.idx -= KM_INCR;
453 BUG_ON(current->kmap_ctrl.idx < 0);
456 #ifndef arch_kmap_local_post_map
457 # define arch_kmap_local_post_map(vaddr, pteval) do { } while (0)
460 #ifndef arch_kmap_local_pre_unmap
461 # define arch_kmap_local_pre_unmap(vaddr) do { } while (0)
464 #ifndef arch_kmap_local_post_unmap
465 # define arch_kmap_local_post_unmap(vaddr) do { } while (0)
468 #ifndef arch_kmap_local_map_idx
469 #define arch_kmap_local_map_idx(idx, pfn) kmap_local_calc_idx(idx)
472 #ifndef arch_kmap_local_unmap_idx
473 #define arch_kmap_local_unmap_idx(idx, vaddr) kmap_local_calc_idx(idx)
476 #ifndef arch_kmap_local_high_get
477 static inline void *arch_kmap_local_high_get(struct page *page)
483 #ifndef arch_kmap_local_set_pte
484 #define arch_kmap_local_set_pte(mm, vaddr, ptep, ptev) \
485 set_pte_at(mm, vaddr, ptep, ptev)
488 /* Unmap a local mapping which was obtained by kmap_high_get() */
489 static inline bool kmap_high_unmap_local(unsigned long vaddr)
491 #ifdef ARCH_NEEDS_KMAP_HIGH_GET
492 if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP)) {
493 kunmap_high(pte_page(pkmap_page_table[PKMAP_NR(vaddr)]));
500 static inline int kmap_local_calc_idx(int idx)
502 return idx + KM_MAX_IDX * smp_processor_id();
505 static pte_t *__kmap_pte;
507 static pte_t *kmap_get_pte(unsigned long vaddr, int idx)
509 if (IS_ENABLED(CONFIG_KMAP_LOCAL_NON_LINEAR_PTE_ARRAY))
511 * Set by the arch if __kmap_pte[-idx] does not produce
514 return virt_to_kpte(vaddr);
516 __kmap_pte = virt_to_kpte(__fix_to_virt(FIX_KMAP_BEGIN));
517 return &__kmap_pte[-idx];
520 void *__kmap_local_pfn_prot(unsigned long pfn, pgprot_t prot)
522 pte_t pteval, *kmap_pte;
527 * Disable migration so resulting virtual address is stable
532 idx = arch_kmap_local_map_idx(kmap_local_idx_push(), pfn);
533 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
534 kmap_pte = kmap_get_pte(vaddr, idx);
535 BUG_ON(!pte_none(*kmap_pte));
536 pteval = pfn_pte(pfn, prot);
537 arch_kmap_local_set_pte(&init_mm, vaddr, kmap_pte, pteval);
538 arch_kmap_local_post_map(vaddr, pteval);
539 current->kmap_ctrl.pteval[kmap_local_idx()] = pteval;
542 return (void *)vaddr;
544 EXPORT_SYMBOL_GPL(__kmap_local_pfn_prot);
546 void *__kmap_local_page_prot(struct page *page, pgprot_t prot)
551 * To broaden the usage of the actual kmap_local() machinery always map
552 * pages when debugging is enabled and the architecture has no problems
553 * with alias mappings.
555 if (!IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP) && !PageHighMem(page))
556 return page_address(page);
558 /* Try kmap_high_get() if architecture has it enabled */
559 kmap = arch_kmap_local_high_get(page);
563 return __kmap_local_pfn_prot(page_to_pfn(page), prot);
565 EXPORT_SYMBOL(__kmap_local_page_prot);
567 void kunmap_local_indexed(void *vaddr)
569 unsigned long addr = (unsigned long) vaddr & PAGE_MASK;
573 if (addr < __fix_to_virt(FIX_KMAP_END) ||
574 addr > __fix_to_virt(FIX_KMAP_BEGIN)) {
575 if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP)) {
576 /* This _should_ never happen! See above. */
581 * Handle mappings which were obtained by kmap_high_get()
582 * first as the virtual address of such mappings is below
583 * PAGE_OFFSET. Warn for all other addresses which are in
584 * the user space part of the virtual address space.
586 if (!kmap_high_unmap_local(addr))
587 WARN_ON_ONCE(addr < PAGE_OFFSET);
592 idx = arch_kmap_local_unmap_idx(kmap_local_idx(), addr);
593 WARN_ON_ONCE(addr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
595 kmap_pte = kmap_get_pte(addr, idx);
596 arch_kmap_local_pre_unmap(addr);
597 pte_clear(&init_mm, addr, kmap_pte);
598 arch_kmap_local_post_unmap(addr);
599 current->kmap_ctrl.pteval[kmap_local_idx()] = __pte(0);
600 kmap_local_idx_pop();
604 EXPORT_SYMBOL(kunmap_local_indexed);
607 * Invoked before switch_to(). This is safe even when during or after
608 * clearing the maps an interrupt which needs a kmap_local happens because
609 * the task::kmap_ctrl.idx is not modified by the unmapping code so a
610 * nested kmap_local will use the next unused index and restore the index
611 * on unmap. The already cleared kmaps of the outgoing task are irrelevant
612 * because the interrupt context does not know about them. The same applies
613 * when scheduling back in for an interrupt which happens before the
614 * restore is complete.
616 void __kmap_local_sched_out(void)
618 struct task_struct *tsk = current;
623 for (i = 0; i < tsk->kmap_ctrl.idx; i++) {
624 pte_t pteval = tsk->kmap_ctrl.pteval[i];
628 /* With debug all even slots are unmapped and act as guard */
629 if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL) && !(i & 0x01)) {
630 WARN_ON_ONCE(pte_val(pteval) != 0);
633 if (WARN_ON_ONCE(pte_none(pteval)))
637 * This is a horrible hack for XTENSA to calculate the
638 * coloured PTE index. Uses the PFN encoded into the pteval
639 * and the map index calculation because the actual mapped
640 * virtual address is not stored in task::kmap_ctrl.
641 * For any sane architecture this is optimized out.
643 idx = arch_kmap_local_map_idx(i, pte_pfn(pteval));
645 addr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
646 kmap_pte = kmap_get_pte(addr, idx);
647 arch_kmap_local_pre_unmap(addr);
648 pte_clear(&init_mm, addr, kmap_pte);
649 arch_kmap_local_post_unmap(addr);
653 void __kmap_local_sched_in(void)
655 struct task_struct *tsk = current;
660 for (i = 0; i < tsk->kmap_ctrl.idx; i++) {
661 pte_t pteval = tsk->kmap_ctrl.pteval[i];
665 /* With debug all even slots are unmapped and act as guard */
666 if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL) && !(i & 0x01)) {
667 WARN_ON_ONCE(pte_val(pteval) != 0);
670 if (WARN_ON_ONCE(pte_none(pteval)))
673 /* See comment in __kmap_local_sched_out() */
674 idx = arch_kmap_local_map_idx(i, pte_pfn(pteval));
675 addr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
676 kmap_pte = kmap_get_pte(addr, idx);
677 set_pte_at(&init_mm, addr, kmap_pte, pteval);
678 arch_kmap_local_post_map(addr, pteval);
682 void kmap_local_fork(struct task_struct *tsk)
684 if (WARN_ON_ONCE(tsk->kmap_ctrl.idx))
685 memset(&tsk->kmap_ctrl, 0, sizeof(tsk->kmap_ctrl));
690 #if defined(HASHED_PAGE_VIRTUAL)
692 #define PA_HASH_ORDER 7
695 * Describes one page->virtual association
697 struct page_address_map {
700 struct list_head list;
703 static struct page_address_map page_address_maps[LAST_PKMAP];
708 static struct page_address_slot {
709 struct list_head lh; /* List of page_address_maps */
710 spinlock_t lock; /* Protect this bucket's list */
711 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
713 static struct page_address_slot *page_slot(const struct page *page)
715 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
719 * page_address - get the mapped virtual address of a page
720 * @page: &struct page to get the virtual address of
722 * Returns the page's virtual address.
724 void *page_address(const struct page *page)
728 struct page_address_slot *pas;
730 if (!PageHighMem(page))
731 return lowmem_page_address(page);
733 pas = page_slot(page);
735 spin_lock_irqsave(&pas->lock, flags);
736 if (!list_empty(&pas->lh)) {
737 struct page_address_map *pam;
739 list_for_each_entry(pam, &pas->lh, list) {
740 if (pam->page == page) {
747 spin_unlock_irqrestore(&pas->lock, flags);
750 EXPORT_SYMBOL(page_address);
753 * set_page_address - set a page's virtual address
754 * @page: &struct page to set
755 * @virtual: virtual address to use
757 void set_page_address(struct page *page, void *virtual)
760 struct page_address_slot *pas;
761 struct page_address_map *pam;
763 BUG_ON(!PageHighMem(page));
765 pas = page_slot(page);
766 if (virtual) { /* Add */
767 pam = &page_address_maps[PKMAP_NR((unsigned long)virtual)];
769 pam->virtual = virtual;
771 spin_lock_irqsave(&pas->lock, flags);
772 list_add_tail(&pam->list, &pas->lh);
773 spin_unlock_irqrestore(&pas->lock, flags);
774 } else { /* Remove */
775 spin_lock_irqsave(&pas->lock, flags);
776 list_for_each_entry(pam, &pas->lh, list) {
777 if (pam->page == page) {
778 list_del(&pam->list);
779 spin_unlock_irqrestore(&pas->lock, flags);
783 spin_unlock_irqrestore(&pas->lock, flags);
789 void __init page_address_init(void)
793 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
794 INIT_LIST_HEAD(&page_address_htable[i].lh);
795 spin_lock_init(&page_address_htable[i].lock);
799 #endif /* defined(HASHED_PAGE_VIRTUAL) */