xref: /linux-6.15/include/linux/highmem.h (revision 9eefefd8)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_HIGHMEM_H
3 #define _LINUX_HIGHMEM_H
4 
5 #include <linux/fs.h>
6 #include <linux/kernel.h>
7 #include <linux/bug.h>
8 #include <linux/cacheflush.h>
9 #include <linux/kmsan.h>
10 #include <linux/mm.h>
11 #include <linux/uaccess.h>
12 #include <linux/hardirq.h>
13 
14 #include "highmem-internal.h"
15 
16 /**
17  * kmap - Map a page for long term usage
18  * @page:	Pointer to the page to be mapped
19  *
20  * Returns: The virtual address of the mapping
21  *
22  * Can only be invoked from preemptible task context because on 32bit
23  * systems with CONFIG_HIGHMEM enabled this function might sleep.
24  *
25  * For systems with CONFIG_HIGHMEM=n and for pages in the low memory area
26  * this returns the virtual address of the direct kernel mapping.
27  *
28  * The returned virtual address is globally visible and valid up to the
29  * point where it is unmapped via kunmap(). The pointer can be handed to
30  * other contexts.
31  *
32  * For highmem pages on 32bit systems this can be slow as the mapping space
33  * is limited and protected by a global lock. In case that there is no
34  * mapping slot available the function blocks until a slot is released via
35  * kunmap().
36  */
37 static inline void *kmap(struct page *page);
38 
39 /**
40  * kunmap - Unmap the virtual address mapped by kmap()
41  * @page:	Pointer to the page which was mapped by kmap()
42  *
43  * Counterpart to kmap(). A NOOP for CONFIG_HIGHMEM=n and for mappings of
44  * pages in the low memory area.
45  */
46 static inline void kunmap(struct page *page);
47 
48 /**
49  * kmap_to_page - Get the page for a kmap'ed address
50  * @addr:	The address to look up
51  *
52  * Returns: The page which is mapped to @addr.
53  */
54 static inline struct page *kmap_to_page(void *addr);
55 
56 /**
57  * kmap_flush_unused - Flush all unused kmap mappings in order to
58  *		       remove stray mappings
59  */
60 static inline void kmap_flush_unused(void);
61 
62 /**
63  * kmap_local_page - Map a page for temporary usage
64  * @page: Pointer to the page to be mapped
65  *
66  * Returns: The virtual address of the mapping
67  *
68  * Can be invoked from any context, including interrupts.
69  *
70  * Requires careful handling when nesting multiple mappings because the map
71  * management is stack based. The unmap has to be in the reverse order of
72  * the map operation:
73  *
74  * addr1 = kmap_local_page(page1);
75  * addr2 = kmap_local_page(page2);
76  * ...
77  * kunmap_local(addr2);
78  * kunmap_local(addr1);
79  *
80  * Unmapping addr1 before addr2 is invalid and causes malfunction.
81  *
82  * Contrary to kmap() mappings the mapping is only valid in the context of
83  * the caller and cannot be handed to other contexts.
84  *
85  * On CONFIG_HIGHMEM=n kernels and for low memory pages this returns the
86  * virtual address of the direct mapping. Only real highmem pages are
87  * temporarily mapped.
88  *
89  * While it is significantly faster than kmap() for the higmem case it
90  * comes with restrictions about the pointer validity.
91  *
92  * On HIGHMEM enabled systems mapping a highmem page has the side effect of
93  * disabling migration in order to keep the virtual address stable across
94  * preemption. No caller of kmap_local_page() can rely on this side effect.
95  */
96 static inline void *kmap_local_page(struct page *page);
97 
98 /**
99  * kmap_local_folio - Map a page in this folio for temporary usage
100  * @folio: The folio containing the page.
101  * @offset: The byte offset within the folio which identifies the page.
102  *
103  * Requires careful handling when nesting multiple mappings because the map
104  * management is stack based. The unmap has to be in the reverse order of
105  * the map operation::
106  *
107  *   addr1 = kmap_local_folio(folio1, offset1);
108  *   addr2 = kmap_local_folio(folio2, offset2);
109  *   ...
110  *   kunmap_local(addr2);
111  *   kunmap_local(addr1);
112  *
113  * Unmapping addr1 before addr2 is invalid and causes malfunction.
114  *
115  * Contrary to kmap() mappings the mapping is only valid in the context of
116  * the caller and cannot be handed to other contexts.
117  *
118  * On CONFIG_HIGHMEM=n kernels and for low memory pages this returns the
119  * virtual address of the direct mapping. Only real highmem pages are
120  * temporarily mapped.
121  *
122  * While it is significantly faster than kmap() for the highmem case it
123  * comes with restrictions about the pointer validity.
124  *
125  * On HIGHMEM enabled systems mapping a highmem page has the side effect of
126  * disabling migration in order to keep the virtual address stable across
127  * preemption. No caller of kmap_local_folio() can rely on this side effect.
128  *
129  * Context: Can be invoked from any context.
130  * Return: The virtual address of @offset.
131  */
132 static inline void *kmap_local_folio(struct folio *folio, size_t offset);
133 
134 /**
135  * kmap_atomic - Atomically map a page for temporary usage - Deprecated!
136  * @page:	Pointer to the page to be mapped
137  *
138  * Returns: The virtual address of the mapping
139  *
140  * In fact a wrapper around kmap_local_page() which also disables pagefaults
141  * and, depending on PREEMPT_RT configuration, also CPU migration and
142  * preemption. Therefore users should not count on the latter two side effects.
143  *
144  * Mappings should always be released by kunmap_atomic().
145  *
146  * Do not use in new code. Use kmap_local_page() instead.
147  *
148  * It is used in atomic context when code wants to access the contents of a
149  * page that might be allocated from high memory (see __GFP_HIGHMEM), for
150  * example a page in the pagecache.  The API has two functions, and they
151  * can be used in a manner similar to the following::
152  *
153  *   // Find the page of interest.
154  *   struct page *page = find_get_page(mapping, offset);
155  *
156  *   // Gain access to the contents of that page.
157  *   void *vaddr = kmap_atomic(page);
158  *
159  *   // Do something to the contents of that page.
160  *   memset(vaddr, 0, PAGE_SIZE);
161  *
162  *   // Unmap that page.
163  *   kunmap_atomic(vaddr);
164  *
165  * Note that the kunmap_atomic() call takes the result of the kmap_atomic()
166  * call, not the argument.
167  *
168  * If you need to map two pages because you want to copy from one page to
169  * another you need to keep the kmap_atomic calls strictly nested, like:
170  *
171  * vaddr1 = kmap_atomic(page1);
172  * vaddr2 = kmap_atomic(page2);
173  *
174  * memcpy(vaddr1, vaddr2, PAGE_SIZE);
175  *
176  * kunmap_atomic(vaddr2);
177  * kunmap_atomic(vaddr1);
178  */
179 static inline void *kmap_atomic(struct page *page);
180 
181 /* Highmem related interfaces for management code */
182 static inline unsigned int nr_free_highpages(void);
183 static inline unsigned long totalhigh_pages(void);
184 
185 #ifndef ARCH_HAS_FLUSH_ANON_PAGE
186 static inline void flush_anon_page(struct vm_area_struct *vma, struct page *page, unsigned long vmaddr)
187 {
188 }
189 #endif
190 
191 #ifndef ARCH_IMPLEMENTS_FLUSH_KERNEL_VMAP_RANGE
192 static inline void flush_kernel_vmap_range(void *vaddr, int size)
193 {
194 }
195 static inline void invalidate_kernel_vmap_range(void *vaddr, int size)
196 {
197 }
198 #endif
199 
200 /* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */
201 #ifndef clear_user_highpage
202 static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
203 {
204 	void *addr = kmap_local_page(page);
205 	clear_user_page(addr, vaddr, page);
206 	kunmap_local(addr);
207 }
208 #endif
209 
210 #ifndef __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE_MOVABLE
211 /**
212  * alloc_zeroed_user_highpage_movable - Allocate a zeroed HIGHMEM page for a VMA that the caller knows can move
213  * @vma: The VMA the page is to be allocated for
214  * @vaddr: The virtual address the page will be inserted into
215  *
216  * Returns: The allocated and zeroed HIGHMEM page
217  *
218  * This function will allocate a page for a VMA that the caller knows will
219  * be able to migrate in the future using move_pages() or reclaimed
220  *
221  * An architecture may override this function by defining
222  * __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE_MOVABLE and providing their own
223  * implementation.
224  */
225 static inline struct page *
226 alloc_zeroed_user_highpage_movable(struct vm_area_struct *vma,
227 				   unsigned long vaddr)
228 {
229 	struct page *page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
230 
231 	if (page)
232 		clear_user_highpage(page, vaddr);
233 
234 	return page;
235 }
236 #endif
237 
238 static inline void clear_highpage(struct page *page)
239 {
240 	void *kaddr = kmap_local_page(page);
241 	clear_page(kaddr);
242 	kunmap_local(kaddr);
243 }
244 
245 static inline void clear_highpage_kasan_tagged(struct page *page)
246 {
247 	u8 tag;
248 
249 	tag = page_kasan_tag(page);
250 	page_kasan_tag_reset(page);
251 	clear_highpage(page);
252 	page_kasan_tag_set(page, tag);
253 }
254 
255 #ifndef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
256 
257 static inline void tag_clear_highpage(struct page *page)
258 {
259 }
260 
261 #endif
262 
263 /*
264  * If we pass in a base or tail page, we can zero up to PAGE_SIZE.
265  * If we pass in a head page, we can zero up to the size of the compound page.
266  */
267 #ifdef CONFIG_HIGHMEM
268 void zero_user_segments(struct page *page, unsigned start1, unsigned end1,
269 		unsigned start2, unsigned end2);
270 #else
271 static inline void zero_user_segments(struct page *page,
272 		unsigned start1, unsigned end1,
273 		unsigned start2, unsigned end2)
274 {
275 	void *kaddr = kmap_local_page(page);
276 	unsigned int i;
277 
278 	BUG_ON(end1 > page_size(page) || end2 > page_size(page));
279 
280 	if (end1 > start1)
281 		memset(kaddr + start1, 0, end1 - start1);
282 
283 	if (end2 > start2)
284 		memset(kaddr + start2, 0, end2 - start2);
285 
286 	kunmap_local(kaddr);
287 	for (i = 0; i < compound_nr(page); i++)
288 		flush_dcache_page(page + i);
289 }
290 #endif
291 
292 static inline void zero_user_segment(struct page *page,
293 	unsigned start, unsigned end)
294 {
295 	zero_user_segments(page, start, end, 0, 0);
296 }
297 
298 static inline void zero_user(struct page *page,
299 	unsigned start, unsigned size)
300 {
301 	zero_user_segments(page, start, start + size, 0, 0);
302 }
303 
304 #ifndef __HAVE_ARCH_COPY_USER_HIGHPAGE
305 
306 static inline void copy_user_highpage(struct page *to, struct page *from,
307 	unsigned long vaddr, struct vm_area_struct *vma)
308 {
309 	char *vfrom, *vto;
310 
311 	vfrom = kmap_local_page(from);
312 	vto = kmap_local_page(to);
313 	copy_user_page(vto, vfrom, vaddr, to);
314 	kmsan_unpoison_memory(page_address(to), PAGE_SIZE);
315 	kunmap_local(vto);
316 	kunmap_local(vfrom);
317 }
318 
319 #endif
320 
321 #ifdef copy_mc_to_kernel
322 static inline int copy_mc_user_highpage(struct page *to, struct page *from,
323 					unsigned long vaddr, struct vm_area_struct *vma)
324 {
325 	unsigned long ret;
326 	char *vfrom, *vto;
327 
328 	vfrom = kmap_local_page(from);
329 	vto = kmap_local_page(to);
330 	ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE);
331 	if (!ret)
332 		kmsan_unpoison_memory(page_address(to), PAGE_SIZE);
333 	kunmap_local(vto);
334 	kunmap_local(vfrom);
335 
336 	return ret;
337 }
338 #else
339 static inline int copy_mc_user_highpage(struct page *to, struct page *from,
340 					unsigned long vaddr, struct vm_area_struct *vma)
341 {
342 	copy_user_highpage(to, from, vaddr, vma);
343 	return 0;
344 }
345 #endif
346 
347 #ifndef __HAVE_ARCH_COPY_HIGHPAGE
348 
349 static inline void copy_highpage(struct page *to, struct page *from)
350 {
351 	char *vfrom, *vto;
352 
353 	vfrom = kmap_local_page(from);
354 	vto = kmap_local_page(to);
355 	copy_page(vto, vfrom);
356 	kmsan_copy_page_meta(to, from);
357 	kunmap_local(vto);
358 	kunmap_local(vfrom);
359 }
360 
361 #endif
362 
363 static inline void memcpy_page(struct page *dst_page, size_t dst_off,
364 			       struct page *src_page, size_t src_off,
365 			       size_t len)
366 {
367 	char *dst = kmap_local_page(dst_page);
368 	char *src = kmap_local_page(src_page);
369 
370 	VM_BUG_ON(dst_off + len > PAGE_SIZE || src_off + len > PAGE_SIZE);
371 	memcpy(dst + dst_off, src + src_off, len);
372 	kunmap_local(src);
373 	kunmap_local(dst);
374 }
375 
376 static inline void memset_page(struct page *page, size_t offset, int val,
377 			       size_t len)
378 {
379 	char *addr = kmap_local_page(page);
380 
381 	VM_BUG_ON(offset + len > PAGE_SIZE);
382 	memset(addr + offset, val, len);
383 	kunmap_local(addr);
384 }
385 
386 static inline void memcpy_from_page(char *to, struct page *page,
387 				    size_t offset, size_t len)
388 {
389 	char *from = kmap_local_page(page);
390 
391 	VM_BUG_ON(offset + len > PAGE_SIZE);
392 	memcpy(to, from + offset, len);
393 	kunmap_local(from);
394 }
395 
396 static inline void memcpy_to_page(struct page *page, size_t offset,
397 				  const char *from, size_t len)
398 {
399 	char *to = kmap_local_page(page);
400 
401 	VM_BUG_ON(offset + len > PAGE_SIZE);
402 	memcpy(to + offset, from, len);
403 	flush_dcache_page(page);
404 	kunmap_local(to);
405 }
406 
407 static inline void memzero_page(struct page *page, size_t offset, size_t len)
408 {
409 	char *addr = kmap_local_page(page);
410 
411 	VM_BUG_ON(offset + len > PAGE_SIZE);
412 	memset(addr + offset, 0, len);
413 	flush_dcache_page(page);
414 	kunmap_local(addr);
415 }
416 
417 /**
418  * folio_zero_segments() - Zero two byte ranges in a folio.
419  * @folio: The folio to write to.
420  * @start1: The first byte to zero.
421  * @xend1: One more than the last byte in the first range.
422  * @start2: The first byte to zero in the second range.
423  * @xend2: One more than the last byte in the second range.
424  */
425 static inline void folio_zero_segments(struct folio *folio,
426 		size_t start1, size_t xend1, size_t start2, size_t xend2)
427 {
428 	zero_user_segments(&folio->page, start1, xend1, start2, xend2);
429 }
430 
431 /**
432  * folio_zero_segment() - Zero a byte range in a folio.
433  * @folio: The folio to write to.
434  * @start: The first byte to zero.
435  * @xend: One more than the last byte to zero.
436  */
437 static inline void folio_zero_segment(struct folio *folio,
438 		size_t start, size_t xend)
439 {
440 	zero_user_segments(&folio->page, start, xend, 0, 0);
441 }
442 
443 /**
444  * folio_zero_range() - Zero a byte range in a folio.
445  * @folio: The folio to write to.
446  * @start: The first byte to zero.
447  * @length: The number of bytes to zero.
448  */
449 static inline void folio_zero_range(struct folio *folio,
450 		size_t start, size_t length)
451 {
452 	zero_user_segments(&folio->page, start, start + length, 0, 0);
453 }
454 
455 #endif /* _LINUX_HIGHMEM_H */
456