Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * High memory handling common code and variables.
0004  *
0005  * (C) 1999 Andrea Arcangeli, SuSE GmbH, andrea@suse.de
0006  *          Gerhard Wichert, Siemens AG, Gerhard.Wichert@pdb.siemens.de
0007  *
0008  *
0009  * Redesigned the x86 32-bit VM architecture to deal with
0010  * 64-bit physical space. With current x86 CPUs this
0011  * means up to 64 Gigabytes physical RAM.
0012  *
0013  * Rewrote high memory support to move the page cache into
0014  * high memory. Implemented permanent (schedulable) kmaps
0015  * based on Linus' idea.
0016  *
0017  * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
0018  */
0019 
0020 #include <linux/mm.h>
0021 #include <linux/export.h>
0022 #include <linux/swap.h>
0023 #include <linux/bio.h>
0024 #include <linux/pagemap.h>
0025 #include <linux/mempool.h>
0026 #include <linux/init.h>
0027 #include <linux/hash.h>
0028 #include <linux/highmem.h>
0029 #include <linux/kgdb.h>
0030 #include <asm/tlbflush.h>
0031 #include <linux/vmalloc.h>
0032 
0033 /*
0034  * Virtual_count is not a pure "count".
0035  *  0 means that it is not mapped, and has not been mapped
0036  *    since a TLB flush - it is usable.
0037  *  1 means that there are no users, but it has been mapped
0038  *    since the last TLB flush - so we can't use it.
0039  *  n means that there are (n-1) current users of it.
0040  */
0041 #ifdef CONFIG_HIGHMEM
0042 
0043 /*
0044  * Architecture with aliasing data cache may define the following family of
0045  * helper functions in its asm/highmem.h to control cache color of virtual
0046  * addresses where physical memory pages are mapped by kmap.
0047  */
0048 #ifndef get_pkmap_color
0049 
0050 /*
0051  * Determine color of virtual address where the page should be mapped.
0052  */
0053 static inline unsigned int get_pkmap_color(struct page *page)
0054 {
0055     return 0;
0056 }
0057 #define get_pkmap_color get_pkmap_color
0058 
0059 /*
0060  * Get next index for mapping inside PKMAP region for page with given color.
0061  */
0062 static inline unsigned int get_next_pkmap_nr(unsigned int color)
0063 {
0064     static unsigned int last_pkmap_nr;
0065 
0066     last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
0067     return last_pkmap_nr;
0068 }
0069 
0070 /*
0071  * Determine if page index inside PKMAP region (pkmap_nr) of given color
0072  * has wrapped around PKMAP region end. When this happens an attempt to
0073  * flush all unused PKMAP slots is made.
0074  */
0075 static inline int no_more_pkmaps(unsigned int pkmap_nr, unsigned int color)
0076 {
0077     return pkmap_nr == 0;
0078 }
0079 
0080 /*
0081  * Get the number of PKMAP entries of the given color. If no free slot is
0082  * found after checking that many entries, kmap will sleep waiting for
0083  * someone to call kunmap and free PKMAP slot.
0084  */
0085 static inline int get_pkmap_entries_count(unsigned int color)
0086 {
0087     return LAST_PKMAP;
0088 }
0089 
0090 /*
0091  * Get head of a wait queue for PKMAP entries of the given color.
0092  * Wait queues for different mapping colors should be independent to avoid
0093  * unnecessary wakeups caused by freeing of slots of other colors.
0094  */
0095 static inline wait_queue_head_t *get_pkmap_wait_queue_head(unsigned int color)
0096 {
0097     static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
0098 
0099     return &pkmap_map_wait;
0100 }
0101 #endif
0102 
0103 atomic_long_t _totalhigh_pages __read_mostly;
0104 EXPORT_SYMBOL(_totalhigh_pages);
0105 
0106 unsigned int __nr_free_highpages(void)
0107 {
0108     struct zone *zone;
0109     unsigned int pages = 0;
0110 
0111     for_each_populated_zone(zone) {
0112         if (is_highmem(zone))
0113             pages += zone_page_state(zone, NR_FREE_PAGES);
0114     }
0115 
0116     return pages;
0117 }
0118 
0119 static int pkmap_count[LAST_PKMAP];
0120 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
0121 
0122 pte_t *pkmap_page_table;
0123 
0124 /*
0125  * Most architectures have no use for kmap_high_get(), so let's abstract
0126  * the disabling of IRQ out of the locking in that case to save on a
0127  * potential useless overhead.
0128  */
0129 #ifdef ARCH_NEEDS_KMAP_HIGH_GET
0130 #define lock_kmap()             spin_lock_irq(&kmap_lock)
0131 #define unlock_kmap()           spin_unlock_irq(&kmap_lock)
0132 #define lock_kmap_any(flags)    spin_lock_irqsave(&kmap_lock, flags)
0133 #define unlock_kmap_any(flags)  spin_unlock_irqrestore(&kmap_lock, flags)
0134 #else
0135 #define lock_kmap()             spin_lock(&kmap_lock)
0136 #define unlock_kmap()           spin_unlock(&kmap_lock)
0137 #define lock_kmap_any(flags)    \
0138         do { spin_lock(&kmap_lock); (void)(flags); } while (0)
0139 #define unlock_kmap_any(flags)  \
0140         do { spin_unlock(&kmap_lock); (void)(flags); } while (0)
0141 #endif
0142 
0143 struct page *__kmap_to_page(void *vaddr)
0144 {
0145     unsigned long addr = (unsigned long)vaddr;
0146 
0147     if (addr >= PKMAP_ADDR(0) && addr < PKMAP_ADDR(LAST_PKMAP)) {
0148         int i = PKMAP_NR(addr);
0149 
0150         return pte_page(pkmap_page_table[i]);
0151     }
0152 
0153     return virt_to_page(vaddr);
0154 }
0155 EXPORT_SYMBOL(__kmap_to_page);
0156 
0157 static void flush_all_zero_pkmaps(void)
0158 {
0159     int i;
0160     int need_flush = 0;
0161 
0162     flush_cache_kmaps();
0163 
0164     for (i = 0; i < LAST_PKMAP; i++) {
0165         struct page *page;
0166 
0167         /*
0168          * zero means we don't have anything to do,
0169          * >1 means that it is still in use. Only
0170          * a count of 1 means that it is free but
0171          * needs to be unmapped
0172          */
0173         if (pkmap_count[i] != 1)
0174             continue;
0175         pkmap_count[i] = 0;
0176 
0177         /* sanity check */
0178         BUG_ON(pte_none(pkmap_page_table[i]));
0179 
0180         /*
0181          * Don't need an atomic fetch-and-clear op here;
0182          * no-one has the page mapped, and cannot get at
0183          * its virtual address (and hence PTE) without first
0184          * getting the kmap_lock (which is held here).
0185          * So no dangers, even with speculative execution.
0186          */
0187         page = pte_page(pkmap_page_table[i]);
0188         pte_clear(&init_mm, PKMAP_ADDR(i), &pkmap_page_table[i]);
0189 
0190         set_page_address(page, NULL);
0191         need_flush = 1;
0192     }
0193     if (need_flush)
0194         flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
0195 }
0196 
0197 void __kmap_flush_unused(void)
0198 {
0199     lock_kmap();
0200     flush_all_zero_pkmaps();
0201     unlock_kmap();
0202 }
0203 
0204 static inline unsigned long map_new_virtual(struct page *page)
0205 {
0206     unsigned long vaddr;
0207     int count;
0208     unsigned int last_pkmap_nr;
0209     unsigned int color = get_pkmap_color(page);
0210 
0211 start:
0212     count = get_pkmap_entries_count(color);
0213     /* Find an empty entry */
0214     for (;;) {
0215         last_pkmap_nr = get_next_pkmap_nr(color);
0216         if (no_more_pkmaps(last_pkmap_nr, color)) {
0217             flush_all_zero_pkmaps();
0218             count = get_pkmap_entries_count(color);
0219         }
0220         if (!pkmap_count[last_pkmap_nr])
0221             break;  /* Found a usable entry */
0222         if (--count)
0223             continue;
0224 
0225         /*
0226          * Sleep for somebody else to unmap their entries
0227          */
0228         {
0229             DECLARE_WAITQUEUE(wait, current);
0230             wait_queue_head_t *pkmap_map_wait =
0231                 get_pkmap_wait_queue_head(color);
0232 
0233             __set_current_state(TASK_UNINTERRUPTIBLE);
0234             add_wait_queue(pkmap_map_wait, &wait);
0235             unlock_kmap();
0236             schedule();
0237             remove_wait_queue(pkmap_map_wait, &wait);
0238             lock_kmap();
0239 
0240             /* Somebody else might have mapped it while we slept */
0241             if (page_address(page))
0242                 return (unsigned long)page_address(page);
0243 
0244             /* Re-start */
0245             goto start;
0246         }
0247     }
0248     vaddr = PKMAP_ADDR(last_pkmap_nr);
0249     set_pte_at(&init_mm, vaddr,
0250            &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
0251 
0252     pkmap_count[last_pkmap_nr] = 1;
0253     set_page_address(page, (void *)vaddr);
0254 
0255     return vaddr;
0256 }
0257 
0258 /**
0259  * kmap_high - map a highmem page into memory
0260  * @page: &struct page to map
0261  *
0262  * Returns the page's virtual memory address.
0263  *
0264  * We cannot call this from interrupts, as it may block.
0265  */
0266 void *kmap_high(struct page *page)
0267 {
0268     unsigned long vaddr;
0269 
0270     /*
0271      * For highmem pages, we can't trust "virtual" until
0272      * after we have the lock.
0273      */
0274     lock_kmap();
0275     vaddr = (unsigned long)page_address(page);
0276     if (!vaddr)
0277         vaddr = map_new_virtual(page);
0278     pkmap_count[PKMAP_NR(vaddr)]++;
0279     BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
0280     unlock_kmap();
0281     return (void *) vaddr;
0282 }
0283 EXPORT_SYMBOL(kmap_high);
0284 
0285 #ifdef ARCH_NEEDS_KMAP_HIGH_GET
0286 /**
0287  * kmap_high_get - pin a highmem page into memory
0288  * @page: &struct page to pin
0289  *
0290  * Returns the page's current virtual memory address, or NULL if no mapping
0291  * exists.  If and only if a non null address is returned then a
0292  * matching call to kunmap_high() is necessary.
0293  *
0294  * This can be called from any context.
0295  */
0296 void *kmap_high_get(struct page *page)
0297 {
0298     unsigned long vaddr, flags;
0299 
0300     lock_kmap_any(flags);
0301     vaddr = (unsigned long)page_address(page);
0302     if (vaddr) {
0303         BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 1);
0304         pkmap_count[PKMAP_NR(vaddr)]++;
0305     }
0306     unlock_kmap_any(flags);
0307     return (void *) vaddr;
0308 }
0309 #endif
0310 
0311 /**
0312  * kunmap_high - unmap a highmem page into memory
0313  * @page: &struct page to unmap
0314  *
0315  * If ARCH_NEEDS_KMAP_HIGH_GET is not defined then this may be called
0316  * only from user context.
0317  */
0318 void kunmap_high(struct page *page)
0319 {
0320     unsigned long vaddr;
0321     unsigned long nr;
0322     unsigned long flags;
0323     int need_wakeup;
0324     unsigned int color = get_pkmap_color(page);
0325     wait_queue_head_t *pkmap_map_wait;
0326 
0327     lock_kmap_any(flags);
0328     vaddr = (unsigned long)page_address(page);
0329     BUG_ON(!vaddr);
0330     nr = PKMAP_NR(vaddr);
0331 
0332     /*
0333      * A count must never go down to zero
0334      * without a TLB flush!
0335      */
0336     need_wakeup = 0;
0337     switch (--pkmap_count[nr]) {
0338     case 0:
0339         BUG();
0340     case 1:
0341         /*
0342          * Avoid an unnecessary wake_up() function call.
0343          * The common case is pkmap_count[] == 1, but
0344          * no waiters.
0345          * The tasks queued in the wait-queue are guarded
0346          * by both the lock in the wait-queue-head and by
0347          * the kmap_lock.  As the kmap_lock is held here,
0348          * no need for the wait-queue-head's lock.  Simply
0349          * test if the queue is empty.
0350          */
0351         pkmap_map_wait = get_pkmap_wait_queue_head(color);
0352         need_wakeup = waitqueue_active(pkmap_map_wait);
0353     }
0354     unlock_kmap_any(flags);
0355 
0356     /* do wake-up, if needed, race-free outside of the spin lock */
0357     if (need_wakeup)
0358         wake_up(pkmap_map_wait);
0359 }
0360 EXPORT_SYMBOL(kunmap_high);
0361 
0362 void zero_user_segments(struct page *page, unsigned start1, unsigned end1,
0363         unsigned start2, unsigned end2)
0364 {
0365     unsigned int i;
0366 
0367     BUG_ON(end1 > page_size(page) || end2 > page_size(page));
0368 
0369     if (start1 >= end1)
0370         start1 = end1 = 0;
0371     if (start2 >= end2)
0372         start2 = end2 = 0;
0373 
0374     for (i = 0; i < compound_nr(page); i++) {
0375         void *kaddr = NULL;
0376 
0377         if (start1 >= PAGE_SIZE) {
0378             start1 -= PAGE_SIZE;
0379             end1 -= PAGE_SIZE;
0380         } else {
0381             unsigned this_end = min_t(unsigned, end1, PAGE_SIZE);
0382 
0383             if (end1 > start1) {
0384                 kaddr = kmap_local_page(page + i);
0385                 memset(kaddr + start1, 0, this_end - start1);
0386             }
0387             end1 -= this_end;
0388             start1 = 0;
0389         }
0390 
0391         if (start2 >= PAGE_SIZE) {
0392             start2 -= PAGE_SIZE;
0393             end2 -= PAGE_SIZE;
0394         } else {
0395             unsigned this_end = min_t(unsigned, end2, PAGE_SIZE);
0396 
0397             if (end2 > start2) {
0398                 if (!kaddr)
0399                     kaddr = kmap_local_page(page + i);
0400                 memset(kaddr + start2, 0, this_end - start2);
0401             }
0402             end2 -= this_end;
0403             start2 = 0;
0404         }
0405 
0406         if (kaddr) {
0407             kunmap_local(kaddr);
0408             flush_dcache_page(page + i);
0409         }
0410 
0411         if (!end1 && !end2)
0412             break;
0413     }
0414 
0415     BUG_ON((start1 | start2 | end1 | end2) != 0);
0416 }
0417 EXPORT_SYMBOL(zero_user_segments);
0418 #endif /* CONFIG_HIGHMEM */
0419 
0420 #ifdef CONFIG_KMAP_LOCAL
0421 
0422 #include <asm/kmap_size.h>
0423 
0424 /*
0425  * With DEBUG_KMAP_LOCAL the stack depth is doubled and every second
0426  * slot is unused which acts as a guard page
0427  */
0428 #ifdef CONFIG_DEBUG_KMAP_LOCAL
0429 # define KM_INCR    2
0430 #else
0431 # define KM_INCR    1
0432 #endif
0433 
0434 static inline int kmap_local_idx_push(void)
0435 {
0436     WARN_ON_ONCE(in_hardirq() && !irqs_disabled());
0437     current->kmap_ctrl.idx += KM_INCR;
0438     BUG_ON(current->kmap_ctrl.idx >= KM_MAX_IDX);
0439     return current->kmap_ctrl.idx - 1;
0440 }
0441 
0442 static inline int kmap_local_idx(void)
0443 {
0444     return current->kmap_ctrl.idx - 1;
0445 }
0446 
0447 static inline void kmap_local_idx_pop(void)
0448 {
0449     current->kmap_ctrl.idx -= KM_INCR;
0450     BUG_ON(current->kmap_ctrl.idx < 0);
0451 }
0452 
0453 #ifndef arch_kmap_local_post_map
0454 # define arch_kmap_local_post_map(vaddr, pteval)    do { } while (0)
0455 #endif
0456 
0457 #ifndef arch_kmap_local_pre_unmap
0458 # define arch_kmap_local_pre_unmap(vaddr)       do { } while (0)
0459 #endif
0460 
0461 #ifndef arch_kmap_local_post_unmap
0462 # define arch_kmap_local_post_unmap(vaddr)      do { } while (0)
0463 #endif
0464 
0465 #ifndef arch_kmap_local_map_idx
0466 #define arch_kmap_local_map_idx(idx, pfn)   kmap_local_calc_idx(idx)
0467 #endif
0468 
0469 #ifndef arch_kmap_local_unmap_idx
0470 #define arch_kmap_local_unmap_idx(idx, vaddr)   kmap_local_calc_idx(idx)
0471 #endif
0472 
0473 #ifndef arch_kmap_local_high_get
0474 static inline void *arch_kmap_local_high_get(struct page *page)
0475 {
0476     return NULL;
0477 }
0478 #endif
0479 
0480 #ifndef arch_kmap_local_set_pte
0481 #define arch_kmap_local_set_pte(mm, vaddr, ptep, ptev)  \
0482     set_pte_at(mm, vaddr, ptep, ptev)
0483 #endif
0484 
0485 /* Unmap a local mapping which was obtained by kmap_high_get() */
0486 static inline bool kmap_high_unmap_local(unsigned long vaddr)
0487 {
0488 #ifdef ARCH_NEEDS_KMAP_HIGH_GET
0489     if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP)) {
0490         kunmap_high(pte_page(pkmap_page_table[PKMAP_NR(vaddr)]));
0491         return true;
0492     }
0493 #endif
0494     return false;
0495 }
0496 
0497 static inline int kmap_local_calc_idx(int idx)
0498 {
0499     return idx + KM_MAX_IDX * smp_processor_id();
0500 }
0501 
0502 static pte_t *__kmap_pte;
0503 
0504 static pte_t *kmap_get_pte(unsigned long vaddr, int idx)
0505 {
0506     if (IS_ENABLED(CONFIG_KMAP_LOCAL_NON_LINEAR_PTE_ARRAY))
0507         /*
0508          * Set by the arch if __kmap_pte[-idx] does not produce
0509          * the correct entry.
0510          */
0511         return virt_to_kpte(vaddr);
0512     if (!__kmap_pte)
0513         __kmap_pte = virt_to_kpte(__fix_to_virt(FIX_KMAP_BEGIN));
0514     return &__kmap_pte[-idx];
0515 }
0516 
0517 void *__kmap_local_pfn_prot(unsigned long pfn, pgprot_t prot)
0518 {
0519     pte_t pteval, *kmap_pte;
0520     unsigned long vaddr;
0521     int idx;
0522 
0523     /*
0524      * Disable migration so resulting virtual address is stable
0525      * across preemption.
0526      */
0527     migrate_disable();
0528     preempt_disable();
0529     idx = arch_kmap_local_map_idx(kmap_local_idx_push(), pfn);
0530     vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
0531     kmap_pte = kmap_get_pte(vaddr, idx);
0532     BUG_ON(!pte_none(*kmap_pte));
0533     pteval = pfn_pte(pfn, prot);
0534     arch_kmap_local_set_pte(&init_mm, vaddr, kmap_pte, pteval);
0535     arch_kmap_local_post_map(vaddr, pteval);
0536     current->kmap_ctrl.pteval[kmap_local_idx()] = pteval;
0537     preempt_enable();
0538 
0539     return (void *)vaddr;
0540 }
0541 EXPORT_SYMBOL_GPL(__kmap_local_pfn_prot);
0542 
0543 void *__kmap_local_page_prot(struct page *page, pgprot_t prot)
0544 {
0545     void *kmap;
0546 
0547     /*
0548      * To broaden the usage of the actual kmap_local() machinery always map
0549      * pages when debugging is enabled and the architecture has no problems
0550      * with alias mappings.
0551      */
0552     if (!IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP) && !PageHighMem(page))
0553         return page_address(page);
0554 
0555     /* Try kmap_high_get() if architecture has it enabled */
0556     kmap = arch_kmap_local_high_get(page);
0557     if (kmap)
0558         return kmap;
0559 
0560     return __kmap_local_pfn_prot(page_to_pfn(page), prot);
0561 }
0562 EXPORT_SYMBOL(__kmap_local_page_prot);
0563 
0564 void kunmap_local_indexed(const void *vaddr)
0565 {
0566     unsigned long addr = (unsigned long) vaddr & PAGE_MASK;
0567     pte_t *kmap_pte;
0568     int idx;
0569 
0570     if (addr < __fix_to_virt(FIX_KMAP_END) ||
0571         addr > __fix_to_virt(FIX_KMAP_BEGIN)) {
0572         if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP)) {
0573             /* This _should_ never happen! See above. */
0574             WARN_ON_ONCE(1);
0575             return;
0576         }
0577         /*
0578          * Handle mappings which were obtained by kmap_high_get()
0579          * first as the virtual address of such mappings is below
0580          * PAGE_OFFSET. Warn for all other addresses which are in
0581          * the user space part of the virtual address space.
0582          */
0583         if (!kmap_high_unmap_local(addr))
0584             WARN_ON_ONCE(addr < PAGE_OFFSET);
0585         return;
0586     }
0587 
0588     preempt_disable();
0589     idx = arch_kmap_local_unmap_idx(kmap_local_idx(), addr);
0590     WARN_ON_ONCE(addr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
0591 
0592     kmap_pte = kmap_get_pte(addr, idx);
0593     arch_kmap_local_pre_unmap(addr);
0594     pte_clear(&init_mm, addr, kmap_pte);
0595     arch_kmap_local_post_unmap(addr);
0596     current->kmap_ctrl.pteval[kmap_local_idx()] = __pte(0);
0597     kmap_local_idx_pop();
0598     preempt_enable();
0599     migrate_enable();
0600 }
0601 EXPORT_SYMBOL(kunmap_local_indexed);
0602 
0603 /*
0604  * Invoked before switch_to(). This is safe even when during or after
0605  * clearing the maps an interrupt which needs a kmap_local happens because
0606  * the task::kmap_ctrl.idx is not modified by the unmapping code so a
0607  * nested kmap_local will use the next unused index and restore the index
0608  * on unmap. The already cleared kmaps of the outgoing task are irrelevant
0609  * because the interrupt context does not know about them. The same applies
0610  * when scheduling back in for an interrupt which happens before the
0611  * restore is complete.
0612  */
0613 void __kmap_local_sched_out(void)
0614 {
0615     struct task_struct *tsk = current;
0616     pte_t *kmap_pte;
0617     int i;
0618 
0619     /* Clear kmaps */
0620     for (i = 0; i < tsk->kmap_ctrl.idx; i++) {
0621         pte_t pteval = tsk->kmap_ctrl.pteval[i];
0622         unsigned long addr;
0623         int idx;
0624 
0625         /* With debug all even slots are unmapped and act as guard */
0626         if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL) && !(i & 0x01)) {
0627             WARN_ON_ONCE(pte_val(pteval) != 0);
0628             continue;
0629         }
0630         if (WARN_ON_ONCE(pte_none(pteval)))
0631             continue;
0632 
0633         /*
0634          * This is a horrible hack for XTENSA to calculate the
0635          * coloured PTE index. Uses the PFN encoded into the pteval
0636          * and the map index calculation because the actual mapped
0637          * virtual address is not stored in task::kmap_ctrl.
0638          * For any sane architecture this is optimized out.
0639          */
0640         idx = arch_kmap_local_map_idx(i, pte_pfn(pteval));
0641 
0642         addr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
0643         kmap_pte = kmap_get_pte(addr, idx);
0644         arch_kmap_local_pre_unmap(addr);
0645         pte_clear(&init_mm, addr, kmap_pte);
0646         arch_kmap_local_post_unmap(addr);
0647     }
0648 }
0649 
0650 void __kmap_local_sched_in(void)
0651 {
0652     struct task_struct *tsk = current;
0653     pte_t *kmap_pte;
0654     int i;
0655 
0656     /* Restore kmaps */
0657     for (i = 0; i < tsk->kmap_ctrl.idx; i++) {
0658         pte_t pteval = tsk->kmap_ctrl.pteval[i];
0659         unsigned long addr;
0660         int idx;
0661 
0662         /* With debug all even slots are unmapped and act as guard */
0663         if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL) && !(i & 0x01)) {
0664             WARN_ON_ONCE(pte_val(pteval) != 0);
0665             continue;
0666         }
0667         if (WARN_ON_ONCE(pte_none(pteval)))
0668             continue;
0669 
0670         /* See comment in __kmap_local_sched_out() */
0671         idx = arch_kmap_local_map_idx(i, pte_pfn(pteval));
0672         addr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
0673         kmap_pte = kmap_get_pte(addr, idx);
0674         set_pte_at(&init_mm, addr, kmap_pte, pteval);
0675         arch_kmap_local_post_map(addr, pteval);
0676     }
0677 }
0678 
0679 void kmap_local_fork(struct task_struct *tsk)
0680 {
0681     if (WARN_ON_ONCE(tsk->kmap_ctrl.idx))
0682         memset(&tsk->kmap_ctrl, 0, sizeof(tsk->kmap_ctrl));
0683 }
0684 
0685 #endif
0686 
0687 #if defined(HASHED_PAGE_VIRTUAL)
0688 
0689 #define PA_HASH_ORDER   7
0690 
0691 /*
0692  * Describes one page->virtual association
0693  */
0694 struct page_address_map {
0695     struct page *page;
0696     void *virtual;
0697     struct list_head list;
0698 };
0699 
0700 static struct page_address_map page_address_maps[LAST_PKMAP];
0701 
0702 /*
0703  * Hash table bucket
0704  */
0705 static struct page_address_slot {
0706     struct list_head lh;            /* List of page_address_maps */
0707     spinlock_t lock;            /* Protect this bucket's list */
0708 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
0709 
0710 static struct page_address_slot *page_slot(const struct page *page)
0711 {
0712     return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
0713 }
0714 
0715 /**
0716  * page_address - get the mapped virtual address of a page
0717  * @page: &struct page to get the virtual address of
0718  *
0719  * Returns the page's virtual address.
0720  */
0721 void *page_address(const struct page *page)
0722 {
0723     unsigned long flags;
0724     void *ret;
0725     struct page_address_slot *pas;
0726 
0727     if (!PageHighMem(page))
0728         return lowmem_page_address(page);
0729 
0730     pas = page_slot(page);
0731     ret = NULL;
0732     spin_lock_irqsave(&pas->lock, flags);
0733     if (!list_empty(&pas->lh)) {
0734         struct page_address_map *pam;
0735 
0736         list_for_each_entry(pam, &pas->lh, list) {
0737             if (pam->page == page) {
0738                 ret = pam->virtual;
0739                 break;
0740             }
0741         }
0742     }
0743 
0744     spin_unlock_irqrestore(&pas->lock, flags);
0745     return ret;
0746 }
0747 EXPORT_SYMBOL(page_address);
0748 
0749 /**
0750  * set_page_address - set a page's virtual address
0751  * @page: &struct page to set
0752  * @virtual: virtual address to use
0753  */
0754 void set_page_address(struct page *page, void *virtual)
0755 {
0756     unsigned long flags;
0757     struct page_address_slot *pas;
0758     struct page_address_map *pam;
0759 
0760     BUG_ON(!PageHighMem(page));
0761 
0762     pas = page_slot(page);
0763     if (virtual) {      /* Add */
0764         pam = &page_address_maps[PKMAP_NR((unsigned long)virtual)];
0765         pam->page = page;
0766         pam->virtual = virtual;
0767 
0768         spin_lock_irqsave(&pas->lock, flags);
0769         list_add_tail(&pam->list, &pas->lh);
0770         spin_unlock_irqrestore(&pas->lock, flags);
0771     } else {        /* Remove */
0772         spin_lock_irqsave(&pas->lock, flags);
0773         list_for_each_entry(pam, &pas->lh, list) {
0774             if (pam->page == page) {
0775                 list_del(&pam->list);
0776                 break;
0777             }
0778         }
0779         spin_unlock_irqrestore(&pas->lock, flags);
0780     }
0781 
0782     return;
0783 }
0784 
0785 void __init page_address_init(void)
0786 {
0787     int i;
0788 
0789     for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
0790         INIT_LIST_HEAD(&page_address_htable[i].lh);
0791         spin_lock_init(&page_address_htable[i].lock);
0792     }
0793 }
0794 
0795 #endif  /* defined(HASHED_PAGE_VIRTUAL) */