1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_HIGHMEM_H
3 #define _LINUX_HIGHMEM_H
6 #include <linux/kernel.h>
8 #include <linux/cacheflush.h>
9 #include <linux/kmsan.h>
11 #include <linux/uaccess.h>
12 #include <linux/hardirq.h>
14 #include "highmem-internal.h"
17 * kmap - Map a page for long term usage
18 * @page: Pointer to the page to be mapped
20 * Returns: The virtual address of the mapping
22 * Can only be invoked from preemptible task context because on 32bit
23 * systems with CONFIG_HIGHMEM enabled this function might sleep.
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.
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
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
37 static inline void *kmap(struct page
*page
);
40 * kunmap - Unmap the virtual address mapped by kmap()
41 * @page: Pointer to the page which was mapped by kmap()
43 * Counterpart to kmap(). A NOOP for CONFIG_HIGHMEM=n and for mappings of
44 * pages in the low memory area.
46 static inline void kunmap(struct page
*page
);
49 * kmap_to_page - Get the page for a kmap'ed address
50 * @addr: The address to look up
52 * Returns: The page which is mapped to @addr.
54 static inline struct page
*kmap_to_page(void *addr
);
57 * kmap_flush_unused - Flush all unused kmap mappings in order to
58 * remove stray mappings
60 static inline void kmap_flush_unused(void);
63 * kmap_local_page - Map a page for temporary usage
64 * @page: Pointer to the page to be mapped
66 * Returns: The virtual address of the mapping
68 * Can be invoked from any context, including interrupts.
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
74 * addr1 = kmap_local_page(page1);
75 * addr2 = kmap_local_page(page2);
77 * kunmap_local(addr2);
78 * kunmap_local(addr1);
80 * Unmapping addr1 before addr2 is invalid and causes malfunction.
82 * Contrary to kmap() mappings the mapping is only valid in the context of
83 * the caller and cannot be handed to other contexts.
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
89 * While kmap_local_page() is significantly faster than kmap() for the highmem
90 * case it comes with restrictions about the pointer validity.
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.
96 static inline void *kmap_local_page(struct page
*page
);
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.
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::
107 * addr1 = kmap_local_folio(folio1, offset1);
108 * addr2 = kmap_local_folio(folio2, offset2);
110 * kunmap_local(addr2);
111 * kunmap_local(addr1);
113 * Unmapping addr1 before addr2 is invalid and causes malfunction.
115 * Contrary to kmap() mappings the mapping is only valid in the context of
116 * the caller and cannot be handed to other contexts.
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.
122 * While it is significantly faster than kmap() for the highmem case it
123 * comes with restrictions about the pointer validity.
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.
129 * Context: Can be invoked from any context.
130 * Return: The virtual address of @offset.
132 static inline void *kmap_local_folio(struct folio
*folio
, size_t offset
);
135 * kmap_atomic - Atomically map a page for temporary usage - Deprecated!
136 * @page: Pointer to the page to be mapped
138 * Returns: The virtual address of the mapping
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.
144 * Mappings should always be released by kunmap_atomic().
146 * Do not use in new code. Use kmap_local_page() instead.
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::
153 * // Find the page of interest.
154 * struct page *page = find_get_page(mapping, offset);
156 * // Gain access to the contents of that page.
157 * void *vaddr = kmap_atomic(page);
159 * // Do something to the contents of that page.
160 * memset(vaddr, 0, PAGE_SIZE);
162 * // Unmap that page.
163 * kunmap_atomic(vaddr);
165 * Note that the kunmap_atomic() call takes the result of the kmap_atomic()
166 * call, not the argument.
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:
171 * vaddr1 = kmap_atomic(page1);
172 * vaddr2 = kmap_atomic(page2);
174 * memcpy(vaddr1, vaddr2, PAGE_SIZE);
176 * kunmap_atomic(vaddr2);
177 * kunmap_atomic(vaddr1);
179 static inline void *kmap_atomic(struct page
*page
);
181 /* Highmem related interfaces for management code */
182 static inline unsigned long nr_free_highpages(void);
183 static inline unsigned long totalhigh_pages(void);
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
)
191 #ifndef ARCH_IMPLEMENTS_FLUSH_KERNEL_VMAP_RANGE
192 static inline void flush_kernel_vmap_range(void *vaddr
, int size
)
195 static inline void invalidate_kernel_vmap_range(void *vaddr
, int size
)
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
)
204 void *addr
= kmap_local_page(page
);
205 clear_user_page(addr
, vaddr
, page
);
210 #ifndef vma_alloc_zeroed_movable_folio
212 * vma_alloc_zeroed_movable_folio - Allocate a zeroed page for a VMA.
213 * @vma: The VMA the page is to be allocated for.
214 * @vaddr: The virtual address the page will be inserted into.
216 * This function will allocate a page suitable for inserting into this
217 * VMA at this virtual address. It may be allocated from highmem or
218 * the movable zone. An architecture may provide its own implementation.
220 * Return: A folio containing one allocated and zeroed page or NULL if
221 * we are out of memory.
224 struct folio
*vma_alloc_zeroed_movable_folio(struct vm_area_struct
*vma
,
227 return vma_alloc_folio(GFP_HIGHUSER_MOVABLE
| __GFP_ZERO
, 0, vma
, vaddr
);
231 static inline void clear_highpage(struct page
*page
)
233 void *kaddr
= kmap_local_page(page
);
238 static inline void clear_highpage_kasan_tagged(struct page
*page
)
240 void *kaddr
= kmap_local_page(page
);
242 clear_page(kasan_reset_tag(kaddr
));
246 #ifndef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
248 static inline void tag_clear_highpage(struct page
*page
)
255 * If we pass in a base or tail page, we can zero up to PAGE_SIZE.
256 * If we pass in a head page, we can zero up to the size of the compound page.
258 #ifdef CONFIG_HIGHMEM
259 void zero_user_segments(struct page
*page
, unsigned start1
, unsigned end1
,
260 unsigned start2
, unsigned end2
);
262 static inline void zero_user_segments(struct page
*page
,
263 unsigned start1
, unsigned end1
,
264 unsigned start2
, unsigned end2
)
266 void *kaddr
= kmap_local_page(page
);
269 BUG_ON(end1
> page_size(page
) || end2
> page_size(page
));
272 memset(kaddr
+ start1
, 0, end1
- start1
);
275 memset(kaddr
+ start2
, 0, end2
- start2
);
278 for (i
= 0; i
< compound_nr(page
); i
++)
279 flush_dcache_page(page
+ i
);
283 static inline void zero_user_segment(struct page
*page
,
284 unsigned start
, unsigned end
)
286 zero_user_segments(page
, start
, end
, 0, 0);
289 static inline void zero_user(struct page
*page
,
290 unsigned start
, unsigned size
)
292 zero_user_segments(page
, start
, start
+ size
, 0, 0);
295 #ifndef __HAVE_ARCH_COPY_USER_HIGHPAGE
297 static inline void copy_user_highpage(struct page
*to
, struct page
*from
,
298 unsigned long vaddr
, struct vm_area_struct
*vma
)
302 vfrom
= kmap_local_page(from
);
303 vto
= kmap_local_page(to
);
304 copy_user_page(vto
, vfrom
, vaddr
, to
);
305 kmsan_unpoison_memory(page_address(to
), PAGE_SIZE
);
312 #ifndef __HAVE_ARCH_COPY_HIGHPAGE
314 static inline void copy_highpage(struct page
*to
, struct page
*from
)
318 vfrom
= kmap_local_page(from
);
319 vto
= kmap_local_page(to
);
320 copy_page(vto
, vfrom
);
321 kmsan_copy_page_meta(to
, from
);
328 #ifdef copy_mc_to_kernel
330 * If architecture supports machine check exception handling, define the
331 * #MC versions of copy_user_highpage and copy_highpage. They copy a memory
332 * page with #MC in source page (@from) handled, and return the number
333 * of bytes not copied if there was a #MC, otherwise 0 for success.
335 static inline int copy_mc_user_highpage(struct page
*to
, struct page
*from
,
336 unsigned long vaddr
, struct vm_area_struct
*vma
)
341 vfrom
= kmap_local_page(from
);
342 vto
= kmap_local_page(to
);
343 ret
= copy_mc_to_kernel(vto
, vfrom
, PAGE_SIZE
);
345 kmsan_unpoison_memory(page_address(to
), PAGE_SIZE
);
350 memory_failure_queue(page_to_pfn(from
), 0);
355 static inline int copy_mc_highpage(struct page
*to
, struct page
*from
)
360 vfrom
= kmap_local_page(from
);
361 vto
= kmap_local_page(to
);
362 ret
= copy_mc_to_kernel(vto
, vfrom
, PAGE_SIZE
);
364 kmsan_copy_page_meta(to
, from
);
369 memory_failure_queue(page_to_pfn(from
), 0);
374 static inline int copy_mc_user_highpage(struct page
*to
, struct page
*from
,
375 unsigned long vaddr
, struct vm_area_struct
*vma
)
377 copy_user_highpage(to
, from
, vaddr
, vma
);
381 static inline int copy_mc_highpage(struct page
*to
, struct page
*from
)
383 copy_highpage(to
, from
);
388 static inline void memcpy_page(struct page
*dst_page
, size_t dst_off
,
389 struct page
*src_page
, size_t src_off
,
392 char *dst
= kmap_local_page(dst_page
);
393 char *src
= kmap_local_page(src_page
);
395 VM_BUG_ON(dst_off
+ len
> PAGE_SIZE
|| src_off
+ len
> PAGE_SIZE
);
396 memcpy(dst
+ dst_off
, src
+ src_off
, len
);
401 static inline void memset_page(struct page
*page
, size_t offset
, int val
,
404 char *addr
= kmap_local_page(page
);
406 VM_BUG_ON(offset
+ len
> PAGE_SIZE
);
407 memset(addr
+ offset
, val
, len
);
411 static inline void memcpy_from_page(char *to
, struct page
*page
,
412 size_t offset
, size_t len
)
414 char *from
= kmap_local_page(page
);
416 VM_BUG_ON(offset
+ len
> PAGE_SIZE
);
417 memcpy(to
, from
+ offset
, len
);
421 static inline void memcpy_to_page(struct page
*page
, size_t offset
,
422 const char *from
, size_t len
)
424 char *to
= kmap_local_page(page
);
426 VM_BUG_ON(offset
+ len
> PAGE_SIZE
);
427 memcpy(to
+ offset
, from
, len
);
428 flush_dcache_page(page
);
432 static inline void memzero_page(struct page
*page
, size_t offset
, size_t len
)
434 char *addr
= kmap_local_page(page
);
436 VM_BUG_ON(offset
+ len
> PAGE_SIZE
);
437 memset(addr
+ offset
, 0, len
);
438 flush_dcache_page(page
);
443 * memcpy_from_folio - Copy a range of bytes from a folio.
444 * @to: The memory to copy to.
445 * @folio: The folio to read from.
446 * @offset: The first byte in the folio to read.
447 * @len: The number of bytes to copy.
449 static inline void memcpy_from_folio(char *to
, struct folio
*folio
,
450 size_t offset
, size_t len
)
452 VM_BUG_ON(offset
+ len
> folio_size(folio
));
455 const char *from
= kmap_local_folio(folio
, offset
);
458 if (folio_test_highmem(folio
) &&
459 chunk
> PAGE_SIZE
- offset_in_page(offset
))
460 chunk
= PAGE_SIZE
- offset_in_page(offset
);
461 memcpy(to
, from
, chunk
);
471 * memcpy_to_folio - Copy a range of bytes to a folio.
472 * @folio: The folio to write to.
473 * @offset: The first byte in the folio to store to.
474 * @from: The memory to copy from.
475 * @len: The number of bytes to copy.
477 static inline void memcpy_to_folio(struct folio
*folio
, size_t offset
,
478 const char *from
, size_t len
)
480 VM_BUG_ON(offset
+ len
> folio_size(folio
));
483 char *to
= kmap_local_folio(folio
, offset
);
486 if (folio_test_highmem(folio
) &&
487 chunk
> PAGE_SIZE
- offset_in_page(offset
))
488 chunk
= PAGE_SIZE
- offset_in_page(offset
);
489 memcpy(to
, from
, chunk
);
497 flush_dcache_folio(folio
);
501 * folio_zero_tail - Zero the tail of a folio.
502 * @folio: The folio to zero.
503 * @offset: The byte offset in the folio to start zeroing at.
504 * @kaddr: The address the folio is currently mapped to.
506 * If you have already used kmap_local_folio() to map a folio, written
507 * some data to it and now need to zero the end of the folio (and flush
508 * the dcache), you can use this function. If you do not have the
509 * folio kmapped (eg the folio has been partially populated by DMA),
510 * use folio_zero_range() or folio_zero_segment() instead.
512 * Return: An address which can be passed to kunmap_local().
514 static inline __must_check
void *folio_zero_tail(struct folio
*folio
,
515 size_t offset
, void *kaddr
)
517 size_t len
= folio_size(folio
) - offset
;
519 if (folio_test_highmem(folio
)) {
520 size_t max
= PAGE_SIZE
- offset_in_page(offset
);
523 memset(kaddr
, 0, max
);
528 kaddr
= kmap_local_folio(folio
, offset
);
532 memset(kaddr
, 0, len
);
533 flush_dcache_folio(folio
);
539 * folio_fill_tail - Copy some data to a folio and pad with zeroes.
540 * @folio: The destination folio.
541 * @offset: The offset into @folio at which to start copying.
542 * @from: The data to copy.
543 * @len: How many bytes of data to copy.
545 * This function is most useful for filesystems which support inline data.
546 * When they want to copy data from the inode into the page cache, this
547 * function does everything for them. It supports large folios even on
548 * HIGHMEM configurations.
550 static inline void folio_fill_tail(struct folio
*folio
, size_t offset
,
551 const char *from
, size_t len
)
553 char *to
= kmap_local_folio(folio
, offset
);
555 VM_BUG_ON(offset
+ len
> folio_size(folio
));
557 if (folio_test_highmem(folio
)) {
558 size_t max
= PAGE_SIZE
- offset_in_page(offset
);
561 memcpy(to
, from
, max
);
567 to
= kmap_local_folio(folio
, offset
);
571 memcpy(to
, from
, len
);
572 to
= folio_zero_tail(folio
, offset
+ len
, to
+ len
);
577 * memcpy_from_file_folio - Copy some bytes from a file folio.
578 * @to: The destination buffer.
579 * @folio: The folio to copy from.
580 * @pos: The position in the file.
581 * @len: The maximum number of bytes to copy.
583 * Copy up to @len bytes from this folio. This may be limited by PAGE_SIZE
584 * if the folio comes from HIGHMEM, and by the size of the folio.
586 * Return: The number of bytes copied from the folio.
588 static inline size_t memcpy_from_file_folio(char *to
, struct folio
*folio
,
589 loff_t pos
, size_t len
)
591 size_t offset
= offset_in_folio(folio
, pos
);
592 char *from
= kmap_local_folio(folio
, offset
);
594 if (folio_test_highmem(folio
)) {
595 offset
= offset_in_page(offset
);
596 len
= min_t(size_t, len
, PAGE_SIZE
- offset
);
598 len
= min(len
, folio_size(folio
) - offset
);
600 memcpy(to
, from
, len
);
607 * folio_zero_segments() - Zero two byte ranges in a folio.
608 * @folio: The folio to write to.
609 * @start1: The first byte to zero.
610 * @xend1: One more than the last byte in the first range.
611 * @start2: The first byte to zero in the second range.
612 * @xend2: One more than the last byte in the second range.
614 static inline void folio_zero_segments(struct folio
*folio
,
615 size_t start1
, size_t xend1
, size_t start2
, size_t xend2
)
617 zero_user_segments(&folio
->page
, start1
, xend1
, start2
, xend2
);
621 * folio_zero_segment() - Zero a byte range in a folio.
622 * @folio: The folio to write to.
623 * @start: The first byte to zero.
624 * @xend: One more than the last byte to zero.
626 static inline void folio_zero_segment(struct folio
*folio
,
627 size_t start
, size_t xend
)
629 zero_user_segments(&folio
->page
, start
, xend
, 0, 0);
633 * folio_zero_range() - Zero a byte range in a folio.
634 * @folio: The folio to write to.
635 * @start: The first byte to zero.
636 * @length: The number of bytes to zero.
638 static inline void folio_zero_range(struct folio
*folio
,
639 size_t start
, size_t length
)
641 zero_user_segments(&folio
->page
, start
, start
+ length
, 0, 0);
645 * folio_release_kmap - Unmap a folio and drop a refcount.
646 * @folio: The folio to release.
647 * @addr: The address previously returned by a call to kmap_local_folio().
649 * It is common, eg in directory handling to kmap a folio. This function
650 * unmaps the folio and drops the refcount that was being held to keep the
651 * folio alive while we accessed it.
653 static inline void folio_release_kmap(struct folio
*folio
, void *addr
)
659 static inline void unmap_and_put_page(struct page
*page
, void *addr
)
661 folio_release_kmap(page_folio(page
), addr
);
664 #endif /* _LINUX_HIGHMEM_H */