xref: /linux/mm/highmem.c (revision cf79f291f985662150363b4a93d16f88f12643bc)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * High memory handling common code and variables.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * (C) 1999 Andrea Arcangeli, SuSE GmbH, andrea@suse.de
61da177e4SLinus Torvalds  *          Gerhard Wichert, Siemens AG, Gerhard.Wichert@pdb.siemens.de
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Redesigned the x86 32-bit VM architecture to deal with
101da177e4SLinus Torvalds  * 64-bit physical space. With current x86 CPUs this
111da177e4SLinus Torvalds  * means up to 64 Gigabytes physical RAM.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * Rewrote high memory support to move the page cache into
141da177e4SLinus Torvalds  * high memory. Implemented permanent (schedulable) kmaps
151da177e4SLinus Torvalds  * based on Linus' idea.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/mm.h>
21b95f1b31SPaul Gortmaker #include <linux/export.h>
221da177e4SLinus Torvalds #include <linux/swap.h>
231da177e4SLinus Torvalds #include <linux/bio.h>
241da177e4SLinus Torvalds #include <linux/pagemap.h>
251da177e4SLinus Torvalds #include <linux/mempool.h>
261da177e4SLinus Torvalds #include <linux/init.h>
271da177e4SLinus Torvalds #include <linux/hash.h>
281da177e4SLinus Torvalds #include <linux/highmem.h>
29eac79005SJason Wessel #include <linux/kgdb.h>
301da177e4SLinus Torvalds #include <asm/tlbflush.h>
31186525bdSIngo Molnar #include <linux/vmalloc.h>
32a8e23a29SPeter Zijlstra 
33ef6e06b2SIra Weiny #ifdef CONFIG_KMAP_LOCAL
kmap_local_calc_idx(int idx)34ef6e06b2SIra Weiny static inline int kmap_local_calc_idx(int idx)
35ef6e06b2SIra Weiny {
36ef6e06b2SIra Weiny 	return idx + KM_MAX_IDX * smp_processor_id();
37ef6e06b2SIra Weiny }
38ef6e06b2SIra Weiny 
39ef6e06b2SIra Weiny #ifndef arch_kmap_local_map_idx
40ef6e06b2SIra Weiny #define arch_kmap_local_map_idx(idx, pfn)	kmap_local_calc_idx(idx)
41ef6e06b2SIra Weiny #endif
42ef6e06b2SIra Weiny #endif /* CONFIG_KMAP_LOCAL */
43ef6e06b2SIra Weiny 
441da177e4SLinus Torvalds /*
451da177e4SLinus Torvalds  * Virtual_count is not a pure "count".
461da177e4SLinus Torvalds  *  0 means that it is not mapped, and has not been mapped
471da177e4SLinus Torvalds  *    since a TLB flush - it is usable.
481da177e4SLinus Torvalds  *  1 means that there are no users, but it has been mapped
491da177e4SLinus Torvalds  *    since the last TLB flush - so we can't use it.
501da177e4SLinus Torvalds  *  n means that there are (n-1) current users of it.
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds #ifdef CONFIG_HIGHMEM
53260b2367SAl Viro 
5415de36a4SMax Filippov /*
5515de36a4SMax Filippov  * Architecture with aliasing data cache may define the following family of
5615de36a4SMax Filippov  * helper functions in its asm/highmem.h to control cache color of virtual
5715de36a4SMax Filippov  * addresses where physical memory pages are mapped by kmap.
5815de36a4SMax Filippov  */
5915de36a4SMax Filippov #ifndef get_pkmap_color
6015de36a4SMax Filippov 
6115de36a4SMax Filippov /*
6215de36a4SMax Filippov  * Determine color of virtual address where the page should be mapped.
6315de36a4SMax Filippov  */
get_pkmap_color(struct page * page)6415de36a4SMax Filippov static inline unsigned int get_pkmap_color(struct page *page)
6515de36a4SMax Filippov {
6615de36a4SMax Filippov 	return 0;
6715de36a4SMax Filippov }
6815de36a4SMax Filippov #define get_pkmap_color get_pkmap_color
6915de36a4SMax Filippov 
7015de36a4SMax Filippov /*
7115de36a4SMax Filippov  * Get next index for mapping inside PKMAP region for page with given color.
7215de36a4SMax Filippov  */
get_next_pkmap_nr(unsigned int color)7315de36a4SMax Filippov static inline unsigned int get_next_pkmap_nr(unsigned int color)
7415de36a4SMax Filippov {
7515de36a4SMax Filippov 	static unsigned int last_pkmap_nr;
7615de36a4SMax Filippov 
7715de36a4SMax Filippov 	last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
7815de36a4SMax Filippov 	return last_pkmap_nr;
7915de36a4SMax Filippov }
8015de36a4SMax Filippov 
8115de36a4SMax Filippov /*
8215de36a4SMax Filippov  * Determine if page index inside PKMAP region (pkmap_nr) of given color
8315de36a4SMax Filippov  * has wrapped around PKMAP region end. When this happens an attempt to
8415de36a4SMax Filippov  * flush all unused PKMAP slots is made.
8515de36a4SMax Filippov  */
no_more_pkmaps(unsigned int pkmap_nr,unsigned int color)8615de36a4SMax Filippov static inline int no_more_pkmaps(unsigned int pkmap_nr, unsigned int color)
8715de36a4SMax Filippov {
8815de36a4SMax Filippov 	return pkmap_nr == 0;
8915de36a4SMax Filippov }
9015de36a4SMax Filippov 
9115de36a4SMax Filippov /*
9215de36a4SMax Filippov  * Get the number of PKMAP entries of the given color. If no free slot is
9315de36a4SMax Filippov  * found after checking that many entries, kmap will sleep waiting for
9415de36a4SMax Filippov  * someone to call kunmap and free PKMAP slot.
9515de36a4SMax Filippov  */
get_pkmap_entries_count(unsigned int color)9615de36a4SMax Filippov static inline int get_pkmap_entries_count(unsigned int color)
9715de36a4SMax Filippov {
9815de36a4SMax Filippov 	return LAST_PKMAP;
9915de36a4SMax Filippov }
10015de36a4SMax Filippov 
10115de36a4SMax Filippov /*
10215de36a4SMax Filippov  * Get head of a wait queue for PKMAP entries of the given color.
10315de36a4SMax Filippov  * Wait queues for different mapping colors should be independent to avoid
10415de36a4SMax Filippov  * unnecessary wakeups caused by freeing of slots of other colors.
10515de36a4SMax Filippov  */
get_pkmap_wait_queue_head(unsigned int color)10615de36a4SMax Filippov static inline wait_queue_head_t *get_pkmap_wait_queue_head(unsigned int color)
10715de36a4SMax Filippov {
10815de36a4SMax Filippov 	static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
10915de36a4SMax Filippov 
11015de36a4SMax Filippov 	return &pkmap_map_wait;
11115de36a4SMax Filippov }
11215de36a4SMax Filippov #endif
11315de36a4SMax Filippov 
114ca79b0c2SArun KS atomic_long_t _totalhigh_pages __read_mostly;
115ca79b0c2SArun KS EXPORT_SYMBOL(_totalhigh_pages);
1163e4d3af5SPeter Zijlstra 
__nr_free_highpages(void)11713f876baSThomas Gleixner unsigned int __nr_free_highpages(void)
118c1f60a5aSChristoph Lameter {
11933499bfeSJoonsoo Kim 	struct zone *zone;
120c1f60a5aSChristoph Lameter 	unsigned int pages = 0;
121c1f60a5aSChristoph Lameter 
12233499bfeSJoonsoo Kim 	for_each_populated_zone(zone) {
12333499bfeSJoonsoo Kim 		if (is_highmem(zone))
12433499bfeSJoonsoo Kim 			pages += zone_page_state(zone, NR_FREE_PAGES);
1252a1e274aSMel Gorman 	}
126c1f60a5aSChristoph Lameter 
127c1f60a5aSChristoph Lameter 	return pages;
128c1f60a5aSChristoph Lameter }
129c1f60a5aSChristoph Lameter 
1301da177e4SLinus Torvalds static int pkmap_count[LAST_PKMAP];
1311da177e4SLinus Torvalds static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds pte_t *pkmap_page_table;
1341da177e4SLinus Torvalds 
1353297e760SNicolas Pitre /*
1363297e760SNicolas Pitre  * Most architectures have no use for kmap_high_get(), so let's abstract
1373297e760SNicolas Pitre  * the disabling of IRQ out of the locking in that case to save on a
1383297e760SNicolas Pitre  * potential useless overhead.
1393297e760SNicolas Pitre  */
1403297e760SNicolas Pitre #ifdef ARCH_NEEDS_KMAP_HIGH_GET
1413297e760SNicolas Pitre #define lock_kmap()             spin_lock_irq(&kmap_lock)
1423297e760SNicolas Pitre #define unlock_kmap()           spin_unlock_irq(&kmap_lock)
1433297e760SNicolas Pitre #define lock_kmap_any(flags)    spin_lock_irqsave(&kmap_lock, flags)
1443297e760SNicolas Pitre #define unlock_kmap_any(flags)  spin_unlock_irqrestore(&kmap_lock, flags)
1453297e760SNicolas Pitre #else
1463297e760SNicolas Pitre #define lock_kmap()             spin_lock(&kmap_lock)
1473297e760SNicolas Pitre #define unlock_kmap()           spin_unlock(&kmap_lock)
1483297e760SNicolas Pitre #define lock_kmap_any(flags)    \
1493297e760SNicolas Pitre 		do { spin_lock(&kmap_lock); (void)(flags); } while (0)
1503297e760SNicolas Pitre #define unlock_kmap_any(flags)  \
1513297e760SNicolas Pitre 		do { spin_unlock(&kmap_lock); (void)(flags); } while (0)
1523297e760SNicolas Pitre #endif
1533297e760SNicolas Pitre 
__kmap_to_page(void * vaddr)15413f876baSThomas Gleixner struct page *__kmap_to_page(void *vaddr)
1555a178119SMel Gorman {
156ef6e06b2SIra Weiny 	unsigned long base = (unsigned long) vaddr & PAGE_MASK;
157ef6e06b2SIra Weiny 	struct kmap_ctrl *kctrl = &current->kmap_ctrl;
1585a178119SMel Gorman 	unsigned long addr = (unsigned long)vaddr;
159ef6e06b2SIra Weiny 	int i;
1605a178119SMel Gorman 
161ef6e06b2SIra Weiny 	/* kmap() mappings */
162ef6e06b2SIra Weiny 	if (WARN_ON_ONCE(addr >= PKMAP_ADDR(0) &&
163ef6e06b2SIra Weiny 			 addr < PKMAP_ADDR(LAST_PKMAP)))
164*c33c7948SRyan Roberts 		return pte_page(ptep_get(&pkmap_page_table[PKMAP_NR(addr)]));
1659727688dSsongqiang 
166ef6e06b2SIra Weiny 	/* kmap_local_page() mappings */
167ef6e06b2SIra Weiny 	if (WARN_ON_ONCE(base >= __fix_to_virt(FIX_KMAP_END) &&
168ef6e06b2SIra Weiny 			 base < __fix_to_virt(FIX_KMAP_BEGIN))) {
169ef6e06b2SIra Weiny 		for (i = 0; i < kctrl->idx; i++) {
170ef6e06b2SIra Weiny 			unsigned long base_addr;
171ef6e06b2SIra Weiny 			int idx;
172ef6e06b2SIra Weiny 
173ef6e06b2SIra Weiny 			idx = arch_kmap_local_map_idx(i, pte_pfn(pteval));
174ef6e06b2SIra Weiny 			base_addr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
175ef6e06b2SIra Weiny 
176ef6e06b2SIra Weiny 			if (base_addr == base)
177ef6e06b2SIra Weiny 				return pte_page(kctrl->pteval[i]);
178ef6e06b2SIra Weiny 		}
1795a178119SMel Gorman 	}
1805a178119SMel Gorman 
181259ecb34SLinus Walleij 	return virt_to_page(vaddr);
1825a178119SMel Gorman }
18313f876baSThomas Gleixner EXPORT_SYMBOL(__kmap_to_page);
1845a178119SMel Gorman 
flush_all_zero_pkmaps(void)1851da177e4SLinus Torvalds static void flush_all_zero_pkmaps(void)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds 	int i;
1885843d9a4SNick Piggin 	int need_flush = 0;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	flush_cache_kmaps();
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	for (i = 0; i < LAST_PKMAP; i++) {
1931da177e4SLinus Torvalds 		struct page *page;
194*c33c7948SRyan Roberts 		pte_t ptent;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 		/*
1971da177e4SLinus Torvalds 		 * zero means we don't have anything to do,
1981da177e4SLinus Torvalds 		 * >1 means that it is still in use. Only
1991da177e4SLinus Torvalds 		 * a count of 1 means that it is free but
2001da177e4SLinus Torvalds 		 * needs to be unmapped
2011da177e4SLinus Torvalds 		 */
2021da177e4SLinus Torvalds 		if (pkmap_count[i] != 1)
2031da177e4SLinus Torvalds 			continue;
2041da177e4SLinus Torvalds 		pkmap_count[i] = 0;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 		/* sanity check */
207*c33c7948SRyan Roberts 		ptent = ptep_get(&pkmap_page_table[i]);
208*c33c7948SRyan Roberts 		BUG_ON(pte_none(ptent));
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 		/*
2111da177e4SLinus Torvalds 		 * Don't need an atomic fetch-and-clear op here;
2121da177e4SLinus Torvalds 		 * no-one has the page mapped, and cannot get at
2131da177e4SLinus Torvalds 		 * its virtual address (and hence PTE) without first
2141da177e4SLinus Torvalds 		 * getting the kmap_lock (which is held here).
2151da177e4SLinus Torvalds 		 * So no dangers, even with speculative execution.
2161da177e4SLinus Torvalds 		 */
217*c33c7948SRyan Roberts 		page = pte_page(ptent);
218eb2db439SJoonsoo Kim 		pte_clear(&init_mm, PKMAP_ADDR(i), &pkmap_page_table[i]);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 		set_page_address(page, NULL);
2215843d9a4SNick Piggin 		need_flush = 1;
2221da177e4SLinus Torvalds 	}
2235843d9a4SNick Piggin 	if (need_flush)
2241da177e4SLinus Torvalds 		flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
__kmap_flush_unused(void)22713f876baSThomas Gleixner void __kmap_flush_unused(void)
228ce6234b5SJeremy Fitzhardinge {
2293297e760SNicolas Pitre 	lock_kmap();
230ce6234b5SJeremy Fitzhardinge 	flush_all_zero_pkmaps();
2313297e760SNicolas Pitre 	unlock_kmap();
232ce6234b5SJeremy Fitzhardinge }
233ce6234b5SJeremy Fitzhardinge 
map_new_virtual(struct page * page)2341da177e4SLinus Torvalds static inline unsigned long map_new_virtual(struct page *page)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	unsigned long vaddr;
2371da177e4SLinus Torvalds 	int count;
23815de36a4SMax Filippov 	unsigned int last_pkmap_nr;
23915de36a4SMax Filippov 	unsigned int color = get_pkmap_color(page);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds start:
24215de36a4SMax Filippov 	count = get_pkmap_entries_count(color);
2431da177e4SLinus Torvalds 	/* Find an empty entry */
2441da177e4SLinus Torvalds 	for (;;) {
24515de36a4SMax Filippov 		last_pkmap_nr = get_next_pkmap_nr(color);
24615de36a4SMax Filippov 		if (no_more_pkmaps(last_pkmap_nr, color)) {
2471da177e4SLinus Torvalds 			flush_all_zero_pkmaps();
24815de36a4SMax Filippov 			count = get_pkmap_entries_count(color);
2491da177e4SLinus Torvalds 		}
2501da177e4SLinus Torvalds 		if (!pkmap_count[last_pkmap_nr])
2511da177e4SLinus Torvalds 			break;	/* Found a usable entry */
2521da177e4SLinus Torvalds 		if (--count)
2531da177e4SLinus Torvalds 			continue;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 		/*
2561da177e4SLinus Torvalds 		 * Sleep for somebody else to unmap their entries
2571da177e4SLinus Torvalds 		 */
2581da177e4SLinus Torvalds 		{
2591da177e4SLinus Torvalds 			DECLARE_WAITQUEUE(wait, current);
26015de36a4SMax Filippov 			wait_queue_head_t *pkmap_map_wait =
26115de36a4SMax Filippov 				get_pkmap_wait_queue_head(color);
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 			__set_current_state(TASK_UNINTERRUPTIBLE);
26415de36a4SMax Filippov 			add_wait_queue(pkmap_map_wait, &wait);
2653297e760SNicolas Pitre 			unlock_kmap();
2661da177e4SLinus Torvalds 			schedule();
26715de36a4SMax Filippov 			remove_wait_queue(pkmap_map_wait, &wait);
2683297e760SNicolas Pitre 			lock_kmap();
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 			/* Somebody else might have mapped it while we slept */
2711da177e4SLinus Torvalds 			if (page_address(page))
2721da177e4SLinus Torvalds 				return (unsigned long)page_address(page);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 			/* Re-start */
2751da177e4SLinus Torvalds 			goto start;
2761da177e4SLinus Torvalds 		}
2771da177e4SLinus Torvalds 	}
2781da177e4SLinus Torvalds 	vaddr = PKMAP_ADDR(last_pkmap_nr);
2791da177e4SLinus Torvalds 	set_pte_at(&init_mm, vaddr,
2801da177e4SLinus Torvalds 		   &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	pkmap_count[last_pkmap_nr] = 1;
2831da177e4SLinus Torvalds 	set_page_address(page, (void *)vaddr);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	return vaddr;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
28877f6078aSRandy Dunlap /**
28977f6078aSRandy Dunlap  * kmap_high - map a highmem page into memory
29077f6078aSRandy Dunlap  * @page: &struct page to map
29177f6078aSRandy Dunlap  *
29277f6078aSRandy Dunlap  * Returns the page's virtual memory address.
29377f6078aSRandy Dunlap  *
29477f6078aSRandy Dunlap  * We cannot call this from interrupts, as it may block.
29577f6078aSRandy Dunlap  */
kmap_high(struct page * page)296920c7a5dSHarvey Harrison void *kmap_high(struct page *page)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	unsigned long vaddr;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	/*
3011da177e4SLinus Torvalds 	 * For highmem pages, we can't trust "virtual" until
3021da177e4SLinus Torvalds 	 * after we have the lock.
3031da177e4SLinus Torvalds 	 */
3043297e760SNicolas Pitre 	lock_kmap();
3051da177e4SLinus Torvalds 	vaddr = (unsigned long)page_address(page);
3061da177e4SLinus Torvalds 	if (!vaddr)
3071da177e4SLinus Torvalds 		vaddr = map_new_virtual(page);
3081da177e4SLinus Torvalds 	pkmap_count[PKMAP_NR(vaddr)]++;
30975babcacSEric Sesterhenn 	BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
3103297e760SNicolas Pitre 	unlock_kmap();
3111da177e4SLinus Torvalds 	return (void *) vaddr;
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds EXPORT_SYMBOL(kmap_high);
3141da177e4SLinus Torvalds 
3153297e760SNicolas Pitre #ifdef ARCH_NEEDS_KMAP_HIGH_GET
3163297e760SNicolas Pitre /**
3173297e760SNicolas Pitre  * kmap_high_get - pin a highmem page into memory
3183297e760SNicolas Pitre  * @page: &struct page to pin
3193297e760SNicolas Pitre  *
3203297e760SNicolas Pitre  * Returns the page's current virtual memory address, or NULL if no mapping
3215e39df56SUwe Kleine-König  * exists.  If and only if a non null address is returned then a
3223297e760SNicolas Pitre  * matching call to kunmap_high() is necessary.
3233297e760SNicolas Pitre  *
3243297e760SNicolas Pitre  * This can be called from any context.
3253297e760SNicolas Pitre  */
kmap_high_get(struct page * page)3263297e760SNicolas Pitre void *kmap_high_get(struct page *page)
3273297e760SNicolas Pitre {
3283297e760SNicolas Pitre 	unsigned long vaddr, flags;
3293297e760SNicolas Pitre 
3303297e760SNicolas Pitre 	lock_kmap_any(flags);
3313297e760SNicolas Pitre 	vaddr = (unsigned long)page_address(page);
3323297e760SNicolas Pitre 	if (vaddr) {
3333297e760SNicolas Pitre 		BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 1);
3343297e760SNicolas Pitre 		pkmap_count[PKMAP_NR(vaddr)]++;
3353297e760SNicolas Pitre 	}
3363297e760SNicolas Pitre 	unlock_kmap_any(flags);
3373297e760SNicolas Pitre 	return (void *) vaddr;
3383297e760SNicolas Pitre }
3393297e760SNicolas Pitre #endif
3403297e760SNicolas Pitre 
34177f6078aSRandy Dunlap /**
3424e9dc5dfSLi Haifeng  * kunmap_high - unmap a highmem page into memory
34377f6078aSRandy Dunlap  * @page: &struct page to unmap
3443297e760SNicolas Pitre  *
3453297e760SNicolas Pitre  * If ARCH_NEEDS_KMAP_HIGH_GET is not defined then this may be called
3463297e760SNicolas Pitre  * only from user context.
34777f6078aSRandy Dunlap  */
kunmap_high(struct page * page)348920c7a5dSHarvey Harrison void kunmap_high(struct page *page)
3491da177e4SLinus Torvalds {
3501da177e4SLinus Torvalds 	unsigned long vaddr;
3511da177e4SLinus Torvalds 	unsigned long nr;
3523297e760SNicolas Pitre 	unsigned long flags;
3531da177e4SLinus Torvalds 	int need_wakeup;
35415de36a4SMax Filippov 	unsigned int color = get_pkmap_color(page);
35515de36a4SMax Filippov 	wait_queue_head_t *pkmap_map_wait;
3561da177e4SLinus Torvalds 
3573297e760SNicolas Pitre 	lock_kmap_any(flags);
3581da177e4SLinus Torvalds 	vaddr = (unsigned long)page_address(page);
35975babcacSEric Sesterhenn 	BUG_ON(!vaddr);
3601da177e4SLinus Torvalds 	nr = PKMAP_NR(vaddr);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	/*
3631da177e4SLinus Torvalds 	 * A count must never go down to zero
3641da177e4SLinus Torvalds 	 * without a TLB flush!
3651da177e4SLinus Torvalds 	 */
3661da177e4SLinus Torvalds 	need_wakeup = 0;
3671da177e4SLinus Torvalds 	switch (--pkmap_count[nr]) {
3681da177e4SLinus Torvalds 	case 0:
3691da177e4SLinus Torvalds 		BUG();
3701da177e4SLinus Torvalds 	case 1:
3711da177e4SLinus Torvalds 		/*
3721da177e4SLinus Torvalds 		 * Avoid an unnecessary wake_up() function call.
3731da177e4SLinus Torvalds 		 * The common case is pkmap_count[] == 1, but
3741da177e4SLinus Torvalds 		 * no waiters.
3751da177e4SLinus Torvalds 		 * The tasks queued in the wait-queue are guarded
3761da177e4SLinus Torvalds 		 * by both the lock in the wait-queue-head and by
3771da177e4SLinus Torvalds 		 * the kmap_lock.  As the kmap_lock is held here,
3781da177e4SLinus Torvalds 		 * no need for the wait-queue-head's lock.  Simply
3791da177e4SLinus Torvalds 		 * test if the queue is empty.
3801da177e4SLinus Torvalds 		 */
38115de36a4SMax Filippov 		pkmap_map_wait = get_pkmap_wait_queue_head(color);
38215de36a4SMax Filippov 		need_wakeup = waitqueue_active(pkmap_map_wait);
3831da177e4SLinus Torvalds 	}
3843297e760SNicolas Pitre 	unlock_kmap_any(flags);
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	/* do wake-up, if needed, race-free outside of the spin lock */
3871da177e4SLinus Torvalds 	if (need_wakeup)
38815de36a4SMax Filippov 		wake_up(pkmap_map_wait);
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds EXPORT_SYMBOL(kunmap_high);
3910060ef3bSMatthew Wilcox (Oracle) 
zero_user_segments(struct page * page,unsigned start1,unsigned end1,unsigned start2,unsigned end2)3920060ef3bSMatthew Wilcox (Oracle) void zero_user_segments(struct page *page, unsigned start1, unsigned end1,
3930060ef3bSMatthew Wilcox (Oracle) 		unsigned start2, unsigned end2)
3940060ef3bSMatthew Wilcox (Oracle) {
3950060ef3bSMatthew Wilcox (Oracle) 	unsigned int i;
3960060ef3bSMatthew Wilcox (Oracle) 
3970060ef3bSMatthew Wilcox (Oracle) 	BUG_ON(end1 > page_size(page) || end2 > page_size(page));
3980060ef3bSMatthew Wilcox (Oracle) 
399184cee51SOGAWA Hirofumi 	if (start1 >= end1)
400184cee51SOGAWA Hirofumi 		start1 = end1 = 0;
401184cee51SOGAWA Hirofumi 	if (start2 >= end2)
402184cee51SOGAWA Hirofumi 		start2 = end2 = 0;
403184cee51SOGAWA Hirofumi 
4040060ef3bSMatthew Wilcox (Oracle) 	for (i = 0; i < compound_nr(page); i++) {
4050060ef3bSMatthew Wilcox (Oracle) 		void *kaddr = NULL;
4060060ef3bSMatthew Wilcox (Oracle) 
4070060ef3bSMatthew Wilcox (Oracle) 		if (start1 >= PAGE_SIZE) {
4080060ef3bSMatthew Wilcox (Oracle) 			start1 -= PAGE_SIZE;
4090060ef3bSMatthew Wilcox (Oracle) 			end1 -= PAGE_SIZE;
4100060ef3bSMatthew Wilcox (Oracle) 		} else {
4110060ef3bSMatthew Wilcox (Oracle) 			unsigned this_end = min_t(unsigned, end1, PAGE_SIZE);
4120060ef3bSMatthew Wilcox (Oracle) 
413184cee51SOGAWA Hirofumi 			if (end1 > start1) {
414d2c20e51SIra Weiny 				kaddr = kmap_local_page(page + i);
4150060ef3bSMatthew Wilcox (Oracle) 				memset(kaddr + start1, 0, this_end - start1);
416184cee51SOGAWA Hirofumi 			}
4170060ef3bSMatthew Wilcox (Oracle) 			end1 -= this_end;
4180060ef3bSMatthew Wilcox (Oracle) 			start1 = 0;
4190060ef3bSMatthew Wilcox (Oracle) 		}
4200060ef3bSMatthew Wilcox (Oracle) 
4210060ef3bSMatthew Wilcox (Oracle) 		if (start2 >= PAGE_SIZE) {
4220060ef3bSMatthew Wilcox (Oracle) 			start2 -= PAGE_SIZE;
4230060ef3bSMatthew Wilcox (Oracle) 			end2 -= PAGE_SIZE;
4240060ef3bSMatthew Wilcox (Oracle) 		} else {
4250060ef3bSMatthew Wilcox (Oracle) 			unsigned this_end = min_t(unsigned, end2, PAGE_SIZE);
4260060ef3bSMatthew Wilcox (Oracle) 
427184cee51SOGAWA Hirofumi 			if (end2 > start2) {
428184cee51SOGAWA Hirofumi 				if (!kaddr)
429d2c20e51SIra Weiny 					kaddr = kmap_local_page(page + i);
4300060ef3bSMatthew Wilcox (Oracle) 				memset(kaddr + start2, 0, this_end - start2);
431184cee51SOGAWA Hirofumi 			}
4320060ef3bSMatthew Wilcox (Oracle) 			end2 -= this_end;
4330060ef3bSMatthew Wilcox (Oracle) 			start2 = 0;
4340060ef3bSMatthew Wilcox (Oracle) 		}
4350060ef3bSMatthew Wilcox (Oracle) 
4360060ef3bSMatthew Wilcox (Oracle) 		if (kaddr) {
437d2c20e51SIra Weiny 			kunmap_local(kaddr);
4380060ef3bSMatthew Wilcox (Oracle) 			flush_dcache_page(page + i);
4390060ef3bSMatthew Wilcox (Oracle) 		}
4400060ef3bSMatthew Wilcox (Oracle) 
4410060ef3bSMatthew Wilcox (Oracle) 		if (!end1 && !end2)
4420060ef3bSMatthew Wilcox (Oracle) 			break;
4430060ef3bSMatthew Wilcox (Oracle) 	}
4440060ef3bSMatthew Wilcox (Oracle) 
4450060ef3bSMatthew Wilcox (Oracle) 	BUG_ON((start1 | start2 | end1 | end2) != 0);
4460060ef3bSMatthew Wilcox (Oracle) }
4470060ef3bSMatthew Wilcox (Oracle) EXPORT_SYMBOL(zero_user_segments);
448955cc774SIra Weiny #endif /* CONFIG_HIGHMEM */
4491da177e4SLinus Torvalds 
450298fa1adSThomas Gleixner #ifdef CONFIG_KMAP_LOCAL
451298fa1adSThomas Gleixner 
452298fa1adSThomas Gleixner #include <asm/kmap_size.h>
453298fa1adSThomas Gleixner 
454389755c2SThomas Gleixner /*
4556e799cb6SThomas Gleixner  * With DEBUG_KMAP_LOCAL the stack depth is doubled and every second
456389755c2SThomas Gleixner  * slot is unused which acts as a guard page
457389755c2SThomas Gleixner  */
4586e799cb6SThomas Gleixner #ifdef CONFIG_DEBUG_KMAP_LOCAL
459389755c2SThomas Gleixner # define KM_INCR	2
460389755c2SThomas Gleixner #else
461389755c2SThomas Gleixner # define KM_INCR	1
462389755c2SThomas Gleixner #endif
463389755c2SThomas Gleixner 
kmap_local_idx_push(void)464298fa1adSThomas Gleixner static inline int kmap_local_idx_push(void)
465298fa1adSThomas Gleixner {
466ea0eafeaSChangbin Du 	WARN_ON_ONCE(in_hardirq() && !irqs_disabled());
4675fbda3ecSThomas Gleixner 	current->kmap_ctrl.idx += KM_INCR;
4685fbda3ecSThomas Gleixner 	BUG_ON(current->kmap_ctrl.idx >= KM_MAX_IDX);
4695fbda3ecSThomas Gleixner 	return current->kmap_ctrl.idx - 1;
470298fa1adSThomas Gleixner }
471298fa1adSThomas Gleixner 
kmap_local_idx(void)472298fa1adSThomas Gleixner static inline int kmap_local_idx(void)
473298fa1adSThomas Gleixner {
4745fbda3ecSThomas Gleixner 	return current->kmap_ctrl.idx - 1;
475298fa1adSThomas Gleixner }
476298fa1adSThomas Gleixner 
kmap_local_idx_pop(void)477298fa1adSThomas Gleixner static inline void kmap_local_idx_pop(void)
478298fa1adSThomas Gleixner {
4795fbda3ecSThomas Gleixner 	current->kmap_ctrl.idx -= KM_INCR;
4805fbda3ecSThomas Gleixner 	BUG_ON(current->kmap_ctrl.idx < 0);
481298fa1adSThomas Gleixner }
482298fa1adSThomas Gleixner 
483298fa1adSThomas Gleixner #ifndef arch_kmap_local_post_map
484298fa1adSThomas Gleixner # define arch_kmap_local_post_map(vaddr, pteval)	do { } while (0)
485298fa1adSThomas Gleixner #endif
4863c1016b5SThomas Gleixner 
487298fa1adSThomas Gleixner #ifndef arch_kmap_local_pre_unmap
488298fa1adSThomas Gleixner # define arch_kmap_local_pre_unmap(vaddr)		do { } while (0)
489298fa1adSThomas Gleixner #endif
490298fa1adSThomas Gleixner 
491298fa1adSThomas Gleixner #ifndef arch_kmap_local_post_unmap
492298fa1adSThomas Gleixner # define arch_kmap_local_post_unmap(vaddr)		do { } while (0)
493298fa1adSThomas Gleixner #endif
494298fa1adSThomas Gleixner 
495298fa1adSThomas Gleixner #ifndef arch_kmap_local_unmap_idx
496298fa1adSThomas Gleixner #define arch_kmap_local_unmap_idx(idx, vaddr)	kmap_local_calc_idx(idx)
497298fa1adSThomas Gleixner #endif
498298fa1adSThomas Gleixner 
499298fa1adSThomas Gleixner #ifndef arch_kmap_local_high_get
arch_kmap_local_high_get(struct page * page)500298fa1adSThomas Gleixner static inline void *arch_kmap_local_high_get(struct page *page)
501298fa1adSThomas Gleixner {
502298fa1adSThomas Gleixner 	return NULL;
503298fa1adSThomas Gleixner }
504298fa1adSThomas Gleixner #endif
505298fa1adSThomas Gleixner 
506a1dce7fdSThomas Gleixner #ifndef arch_kmap_local_set_pte
507a1dce7fdSThomas Gleixner #define arch_kmap_local_set_pte(mm, vaddr, ptep, ptev)	\
508a1dce7fdSThomas Gleixner 	set_pte_at(mm, vaddr, ptep, ptev)
509a1dce7fdSThomas Gleixner #endif
510a1dce7fdSThomas Gleixner 
511298fa1adSThomas Gleixner /* Unmap a local mapping which was obtained by kmap_high_get() */
kmap_high_unmap_local(unsigned long vaddr)5122a656cadSThomas Gleixner static inline bool kmap_high_unmap_local(unsigned long vaddr)
513298fa1adSThomas Gleixner {
514298fa1adSThomas Gleixner #ifdef ARCH_NEEDS_KMAP_HIGH_GET
5152a656cadSThomas Gleixner 	if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP)) {
516*c33c7948SRyan Roberts 		kunmap_high(pte_page(ptep_get(&pkmap_page_table[PKMAP_NR(vaddr)])));
5172a656cadSThomas Gleixner 		return true;
5182a656cadSThomas Gleixner 	}
519298fa1adSThomas Gleixner #endif
5202a656cadSThomas Gleixner 	return false;
521298fa1adSThomas Gleixner }
522298fa1adSThomas Gleixner 
523298fa1adSThomas Gleixner static pte_t *__kmap_pte;
524298fa1adSThomas Gleixner 
kmap_get_pte(unsigned long vaddr,int idx)525825c43f5SArd Biesheuvel static pte_t *kmap_get_pte(unsigned long vaddr, int idx)
526298fa1adSThomas Gleixner {
527825c43f5SArd Biesheuvel 	if (IS_ENABLED(CONFIG_KMAP_LOCAL_NON_LINEAR_PTE_ARRAY))
528825c43f5SArd Biesheuvel 		/*
529825c43f5SArd Biesheuvel 		 * Set by the arch if __kmap_pte[-idx] does not produce
530825c43f5SArd Biesheuvel 		 * the correct entry.
531825c43f5SArd Biesheuvel 		 */
532825c43f5SArd Biesheuvel 		return virt_to_kpte(vaddr);
533298fa1adSThomas Gleixner 	if (!__kmap_pte)
534298fa1adSThomas Gleixner 		__kmap_pte = virt_to_kpte(__fix_to_virt(FIX_KMAP_BEGIN));
535825c43f5SArd Biesheuvel 	return &__kmap_pte[-idx];
536298fa1adSThomas Gleixner }
537298fa1adSThomas Gleixner 
__kmap_local_pfn_prot(unsigned long pfn,pgprot_t prot)538298fa1adSThomas Gleixner void *__kmap_local_pfn_prot(unsigned long pfn, pgprot_t prot)
539298fa1adSThomas Gleixner {
540825c43f5SArd Biesheuvel 	pte_t pteval, *kmap_pte;
541298fa1adSThomas Gleixner 	unsigned long vaddr;
542298fa1adSThomas Gleixner 	int idx;
543298fa1adSThomas Gleixner 
544f3ba3c71SThomas Gleixner 	/*
545f3ba3c71SThomas Gleixner 	 * Disable migration so resulting virtual address is stable
546f0953a1bSIngo Molnar 	 * across preemption.
547f3ba3c71SThomas Gleixner 	 */
548f3ba3c71SThomas Gleixner 	migrate_disable();
549298fa1adSThomas Gleixner 	preempt_disable();
550298fa1adSThomas Gleixner 	idx = arch_kmap_local_map_idx(kmap_local_idx_push(), pfn);
551298fa1adSThomas Gleixner 	vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
552825c43f5SArd Biesheuvel 	kmap_pte = kmap_get_pte(vaddr, idx);
553*c33c7948SRyan Roberts 	BUG_ON(!pte_none(ptep_get(kmap_pte)));
554298fa1adSThomas Gleixner 	pteval = pfn_pte(pfn, prot);
555825c43f5SArd Biesheuvel 	arch_kmap_local_set_pte(&init_mm, vaddr, kmap_pte, pteval);
556298fa1adSThomas Gleixner 	arch_kmap_local_post_map(vaddr, pteval);
5575fbda3ecSThomas Gleixner 	current->kmap_ctrl.pteval[kmap_local_idx()] = pteval;
558298fa1adSThomas Gleixner 	preempt_enable();
559298fa1adSThomas Gleixner 
560298fa1adSThomas Gleixner 	return (void *)vaddr;
561298fa1adSThomas Gleixner }
562298fa1adSThomas Gleixner EXPORT_SYMBOL_GPL(__kmap_local_pfn_prot);
563298fa1adSThomas Gleixner 
__kmap_local_page_prot(struct page * page,pgprot_t prot)564298fa1adSThomas Gleixner void *__kmap_local_page_prot(struct page *page, pgprot_t prot)
565298fa1adSThomas Gleixner {
566298fa1adSThomas Gleixner 	void *kmap;
567298fa1adSThomas Gleixner 
5680e91a0c6SThomas Gleixner 	/*
5690e91a0c6SThomas Gleixner 	 * To broaden the usage of the actual kmap_local() machinery always map
5700e91a0c6SThomas Gleixner 	 * pages when debugging is enabled and the architecture has no problems
5710e91a0c6SThomas Gleixner 	 * with alias mappings.
5720e91a0c6SThomas Gleixner 	 */
5730e91a0c6SThomas Gleixner 	if (!IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP) && !PageHighMem(page))
574298fa1adSThomas Gleixner 		return page_address(page);
575298fa1adSThomas Gleixner 
576298fa1adSThomas Gleixner 	/* Try kmap_high_get() if architecture has it enabled */
577298fa1adSThomas Gleixner 	kmap = arch_kmap_local_high_get(page);
578298fa1adSThomas Gleixner 	if (kmap)
579298fa1adSThomas Gleixner 		return kmap;
580298fa1adSThomas Gleixner 
581298fa1adSThomas Gleixner 	return __kmap_local_pfn_prot(page_to_pfn(page), prot);
582298fa1adSThomas Gleixner }
583298fa1adSThomas Gleixner EXPORT_SYMBOL(__kmap_local_page_prot);
584298fa1adSThomas Gleixner 
kunmap_local_indexed(const void * vaddr)58539ade048SFabio M. De Francesco void kunmap_local_indexed(const void *vaddr)
586298fa1adSThomas Gleixner {
587298fa1adSThomas Gleixner 	unsigned long addr = (unsigned long) vaddr & PAGE_MASK;
588825c43f5SArd Biesheuvel 	pte_t *kmap_pte;
589298fa1adSThomas Gleixner 	int idx;
590298fa1adSThomas Gleixner 
591298fa1adSThomas Gleixner 	if (addr < __fix_to_virt(FIX_KMAP_END) ||
592298fa1adSThomas Gleixner 	    addr > __fix_to_virt(FIX_KMAP_BEGIN)) {
5930e91a0c6SThomas Gleixner 		if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP)) {
5940e91a0c6SThomas Gleixner 			/* This _should_ never happen! See above. */
5950e91a0c6SThomas Gleixner 			WARN_ON_ONCE(1);
5960e91a0c6SThomas Gleixner 			return;
5970e91a0c6SThomas Gleixner 		}
5982a656cadSThomas Gleixner 		/*
5992a656cadSThomas Gleixner 		 * Handle mappings which were obtained by kmap_high_get()
6002a656cadSThomas Gleixner 		 * first as the virtual address of such mappings is below
6012a656cadSThomas Gleixner 		 * PAGE_OFFSET. Warn for all other addresses which are in
6022a656cadSThomas Gleixner 		 * the user space part of the virtual address space.
6032a656cadSThomas Gleixner 		 */
6042a656cadSThomas Gleixner 		if (!kmap_high_unmap_local(addr))
605298fa1adSThomas Gleixner 			WARN_ON_ONCE(addr < PAGE_OFFSET);
606298fa1adSThomas Gleixner 		return;
607298fa1adSThomas Gleixner 	}
608298fa1adSThomas Gleixner 
609298fa1adSThomas Gleixner 	preempt_disable();
610298fa1adSThomas Gleixner 	idx = arch_kmap_local_unmap_idx(kmap_local_idx(), addr);
611298fa1adSThomas Gleixner 	WARN_ON_ONCE(addr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
612298fa1adSThomas Gleixner 
613825c43f5SArd Biesheuvel 	kmap_pte = kmap_get_pte(addr, idx);
614298fa1adSThomas Gleixner 	arch_kmap_local_pre_unmap(addr);
615825c43f5SArd Biesheuvel 	pte_clear(&init_mm, addr, kmap_pte);
616298fa1adSThomas Gleixner 	arch_kmap_local_post_unmap(addr);
6175fbda3ecSThomas Gleixner 	current->kmap_ctrl.pteval[kmap_local_idx()] = __pte(0);
618298fa1adSThomas Gleixner 	kmap_local_idx_pop();
619298fa1adSThomas Gleixner 	preempt_enable();
620f3ba3c71SThomas Gleixner 	migrate_enable();
621298fa1adSThomas Gleixner }
622298fa1adSThomas Gleixner EXPORT_SYMBOL(kunmap_local_indexed);
6235fbda3ecSThomas Gleixner 
6245fbda3ecSThomas Gleixner /*
6255fbda3ecSThomas Gleixner  * Invoked before switch_to(). This is safe even when during or after
6265fbda3ecSThomas Gleixner  * clearing the maps an interrupt which needs a kmap_local happens because
6275fbda3ecSThomas Gleixner  * the task::kmap_ctrl.idx is not modified by the unmapping code so a
6285fbda3ecSThomas Gleixner  * nested kmap_local will use the next unused index and restore the index
6295fbda3ecSThomas Gleixner  * on unmap. The already cleared kmaps of the outgoing task are irrelevant
6305fbda3ecSThomas Gleixner  * because the interrupt context does not know about them. The same applies
6315fbda3ecSThomas Gleixner  * when scheduling back in for an interrupt which happens before the
6325fbda3ecSThomas Gleixner  * restore is complete.
6335fbda3ecSThomas Gleixner  */
__kmap_local_sched_out(void)6345fbda3ecSThomas Gleixner void __kmap_local_sched_out(void)
6355fbda3ecSThomas Gleixner {
6365fbda3ecSThomas Gleixner 	struct task_struct *tsk = current;
637825c43f5SArd Biesheuvel 	pte_t *kmap_pte;
6385fbda3ecSThomas Gleixner 	int i;
6395fbda3ecSThomas Gleixner 
6405fbda3ecSThomas Gleixner 	/* Clear kmaps */
6415fbda3ecSThomas Gleixner 	for (i = 0; i < tsk->kmap_ctrl.idx; i++) {
6425fbda3ecSThomas Gleixner 		pte_t pteval = tsk->kmap_ctrl.pteval[i];
6435fbda3ecSThomas Gleixner 		unsigned long addr;
6445fbda3ecSThomas Gleixner 		int idx;
6455fbda3ecSThomas Gleixner 
6465fbda3ecSThomas Gleixner 		/* With debug all even slots are unmapped and act as guard */
647487cfadeSIra Weiny 		if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL) && !(i & 0x01)) {
64866f133ceSMax Filippov 			WARN_ON_ONCE(pte_val(pteval) != 0);
6495fbda3ecSThomas Gleixner 			continue;
6505fbda3ecSThomas Gleixner 		}
6515fbda3ecSThomas Gleixner 		if (WARN_ON_ONCE(pte_none(pteval)))
6525fbda3ecSThomas Gleixner 			continue;
6535fbda3ecSThomas Gleixner 
6545fbda3ecSThomas Gleixner 		/*
6555fbda3ecSThomas Gleixner 		 * This is a horrible hack for XTENSA to calculate the
6565fbda3ecSThomas Gleixner 		 * coloured PTE index. Uses the PFN encoded into the pteval
6575fbda3ecSThomas Gleixner 		 * and the map index calculation because the actual mapped
6585fbda3ecSThomas Gleixner 		 * virtual address is not stored in task::kmap_ctrl.
6595fbda3ecSThomas Gleixner 		 * For any sane architecture this is optimized out.
6605fbda3ecSThomas Gleixner 		 */
6615fbda3ecSThomas Gleixner 		idx = arch_kmap_local_map_idx(i, pte_pfn(pteval));
6625fbda3ecSThomas Gleixner 
6635fbda3ecSThomas Gleixner 		addr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
664825c43f5SArd Biesheuvel 		kmap_pte = kmap_get_pte(addr, idx);
6655fbda3ecSThomas Gleixner 		arch_kmap_local_pre_unmap(addr);
666825c43f5SArd Biesheuvel 		pte_clear(&init_mm, addr, kmap_pte);
6675fbda3ecSThomas Gleixner 		arch_kmap_local_post_unmap(addr);
6685fbda3ecSThomas Gleixner 	}
6695fbda3ecSThomas Gleixner }
6705fbda3ecSThomas Gleixner 
__kmap_local_sched_in(void)6715fbda3ecSThomas Gleixner void __kmap_local_sched_in(void)
6725fbda3ecSThomas Gleixner {
6735fbda3ecSThomas Gleixner 	struct task_struct *tsk = current;
674825c43f5SArd Biesheuvel 	pte_t *kmap_pte;
6755fbda3ecSThomas Gleixner 	int i;
6765fbda3ecSThomas Gleixner 
6775fbda3ecSThomas Gleixner 	/* Restore kmaps */
6785fbda3ecSThomas Gleixner 	for (i = 0; i < tsk->kmap_ctrl.idx; i++) {
6795fbda3ecSThomas Gleixner 		pte_t pteval = tsk->kmap_ctrl.pteval[i];
6805fbda3ecSThomas Gleixner 		unsigned long addr;
6815fbda3ecSThomas Gleixner 		int idx;
6825fbda3ecSThomas Gleixner 
6835fbda3ecSThomas Gleixner 		/* With debug all even slots are unmapped and act as guard */
684487cfadeSIra Weiny 		if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL) && !(i & 0x01)) {
68566f133ceSMax Filippov 			WARN_ON_ONCE(pte_val(pteval) != 0);
6865fbda3ecSThomas Gleixner 			continue;
6875fbda3ecSThomas Gleixner 		}
6885fbda3ecSThomas Gleixner 		if (WARN_ON_ONCE(pte_none(pteval)))
6895fbda3ecSThomas Gleixner 			continue;
6905fbda3ecSThomas Gleixner 
6915fbda3ecSThomas Gleixner 		/* See comment in __kmap_local_sched_out() */
6925fbda3ecSThomas Gleixner 		idx = arch_kmap_local_map_idx(i, pte_pfn(pteval));
6935fbda3ecSThomas Gleixner 		addr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
694825c43f5SArd Biesheuvel 		kmap_pte = kmap_get_pte(addr, idx);
695825c43f5SArd Biesheuvel 		set_pte_at(&init_mm, addr, kmap_pte, pteval);
6965fbda3ecSThomas Gleixner 		arch_kmap_local_post_map(addr, pteval);
6975fbda3ecSThomas Gleixner 	}
6985fbda3ecSThomas Gleixner }
6995fbda3ecSThomas Gleixner 
kmap_local_fork(struct task_struct * tsk)7005fbda3ecSThomas Gleixner void kmap_local_fork(struct task_struct *tsk)
7015fbda3ecSThomas Gleixner {
7025fbda3ecSThomas Gleixner 	if (WARN_ON_ONCE(tsk->kmap_ctrl.idx))
7035fbda3ecSThomas Gleixner 		memset(&tsk->kmap_ctrl, 0, sizeof(tsk->kmap_ctrl));
7045fbda3ecSThomas Gleixner }
7055fbda3ecSThomas Gleixner 
706298fa1adSThomas Gleixner #endif
707298fa1adSThomas Gleixner 
7081da177e4SLinus Torvalds #if defined(HASHED_PAGE_VIRTUAL)
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds #define PA_HASH_ORDER	7
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds /*
7131da177e4SLinus Torvalds  * Describes one page->virtual association
7141da177e4SLinus Torvalds  */
7151da177e4SLinus Torvalds struct page_address_map {
7161da177e4SLinus Torvalds 	struct page *page;
7171da177e4SLinus Torvalds 	void *virtual;
7181da177e4SLinus Torvalds 	struct list_head list;
7191da177e4SLinus Torvalds };
7201da177e4SLinus Torvalds 
721a354e2c8SJoonsoo Kim static struct page_address_map page_address_maps[LAST_PKMAP];
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds /*
7241da177e4SLinus Torvalds  * Hash table bucket
7251da177e4SLinus Torvalds  */
7261da177e4SLinus Torvalds static struct page_address_slot {
7271da177e4SLinus Torvalds 	struct list_head lh;			/* List of page_address_maps */
7281da177e4SLinus Torvalds 	spinlock_t lock;			/* Protect this bucket's list */
7291da177e4SLinus Torvalds } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
7301da177e4SLinus Torvalds 
page_slot(const struct page * page)731f9918794SIan Campbell static struct page_address_slot *page_slot(const struct page *page)
7321da177e4SLinus Torvalds {
7331da177e4SLinus Torvalds 	return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
7341da177e4SLinus Torvalds }
7351da177e4SLinus Torvalds 
73677f6078aSRandy Dunlap /**
73777f6078aSRandy Dunlap  * page_address - get the mapped virtual address of a page
73877f6078aSRandy Dunlap  * @page: &struct page to get the virtual address of
73977f6078aSRandy Dunlap  *
74077f6078aSRandy Dunlap  * Returns the page's virtual address.
74177f6078aSRandy Dunlap  */
page_address(const struct page * page)742f9918794SIan Campbell void *page_address(const struct page *page)
7431da177e4SLinus Torvalds {
7441da177e4SLinus Torvalds 	unsigned long flags;
7451da177e4SLinus Torvalds 	void *ret;
7461da177e4SLinus Torvalds 	struct page_address_slot *pas;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	if (!PageHighMem(page))
7491da177e4SLinus Torvalds 		return lowmem_page_address(page);
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	pas = page_slot(page);
7521da177e4SLinus Torvalds 	ret = NULL;
7531da177e4SLinus Torvalds 	spin_lock_irqsave(&pas->lock, flags);
7541da177e4SLinus Torvalds 	if (!list_empty(&pas->lh)) {
7551da177e4SLinus Torvalds 		struct page_address_map *pam;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 		list_for_each_entry(pam, &pas->lh, list) {
7581da177e4SLinus Torvalds 			if (pam->page == page) {
7591da177e4SLinus Torvalds 				ret = pam->virtual;
7607a3f2263SMiaohe Lin 				break;
7611da177e4SLinus Torvalds 			}
7621da177e4SLinus Torvalds 		}
7631da177e4SLinus Torvalds 	}
7647a3f2263SMiaohe Lin 
7651da177e4SLinus Torvalds 	spin_unlock_irqrestore(&pas->lock, flags);
7661da177e4SLinus Torvalds 	return ret;
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds EXPORT_SYMBOL(page_address);
7691da177e4SLinus Torvalds 
77077f6078aSRandy Dunlap /**
77177f6078aSRandy Dunlap  * set_page_address - set a page's virtual address
77277f6078aSRandy Dunlap  * @page: &struct page to set
77377f6078aSRandy Dunlap  * @virtual: virtual address to use
77477f6078aSRandy Dunlap  */
set_page_address(struct page * page,void * virtual)7751da177e4SLinus Torvalds void set_page_address(struct page *page, void *virtual)
7761da177e4SLinus Torvalds {
7771da177e4SLinus Torvalds 	unsigned long flags;
7781da177e4SLinus Torvalds 	struct page_address_slot *pas;
7791da177e4SLinus Torvalds 	struct page_address_map *pam;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	BUG_ON(!PageHighMem(page));
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	pas = page_slot(page);
7841da177e4SLinus Torvalds 	if (virtual) {		/* Add */
785a354e2c8SJoonsoo Kim 		pam = &page_address_maps[PKMAP_NR((unsigned long)virtual)];
7861da177e4SLinus Torvalds 		pam->page = page;
7871da177e4SLinus Torvalds 		pam->virtual = virtual;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 		spin_lock_irqsave(&pas->lock, flags);
7901da177e4SLinus Torvalds 		list_add_tail(&pam->list, &pas->lh);
7911da177e4SLinus Torvalds 		spin_unlock_irqrestore(&pas->lock, flags);
7921da177e4SLinus Torvalds 	} else {		/* Remove */
7931da177e4SLinus Torvalds 		spin_lock_irqsave(&pas->lock, flags);
7941da177e4SLinus Torvalds 		list_for_each_entry(pam, &pas->lh, list) {
7951da177e4SLinus Torvalds 			if (pam->page == page) {
7961da177e4SLinus Torvalds 				list_del(&pam->list);
7977a3f2263SMiaohe Lin 				break;
7981da177e4SLinus Torvalds 			}
7991da177e4SLinus Torvalds 		}
8001da177e4SLinus Torvalds 		spin_unlock_irqrestore(&pas->lock, flags);
8011da177e4SLinus Torvalds 	}
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
page_address_init(void)8041da177e4SLinus Torvalds void __init page_address_init(void)
8051da177e4SLinus Torvalds {
8061da177e4SLinus Torvalds 	int i;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
8091da177e4SLinus Torvalds 		INIT_LIST_HEAD(&page_address_htable[i].lh);
8101da177e4SLinus Torvalds 		spin_lock_init(&page_address_htable[i].lock);
8111da177e4SLinus Torvalds 	}
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
814955cc774SIra Weiny #endif	/* defined(HASHED_PAGE_VIRTUAL) */
815