• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/mm.h>
9 #include <linux/uaccess.h>
10 #include <linux/hardirq.h>
11 
12 #include <asm/cacheflush.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  * @addr:	Virtual address to be unmapped
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.
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. Only use when really
91  * necessary.
92  *
93  * On HIGHMEM enabled systems mapping a highmem page has the side effect of
94  * disabling migration in order to keep the virtual address stable across
95  * preemption. No caller of kmap_local_page() can rely on this side effect.
96  */
97 static inline void *kmap_local_page(struct page *page);
98 
99 /**
100  * kmap_atomic - Atomically map a page for temporary usage - Deprecated!
101  * @page:	Pointer to the page to be mapped
102  *
103  * Returns: The virtual address of the mapping
104  *
105  * Effectively a wrapper around kmap_local_page() which disables pagefaults
106  * and preemption.
107  *
108  * Do not use in new code. Use kmap_local_page() instead.
109  */
110 static inline void *kmap_atomic(struct page *page);
111 
112 /**
113  * kunmap_atomic - Unmap the virtual address mapped by kmap_atomic()
114  * @addr:	Virtual address to be unmapped
115  *
116  * Counterpart to kmap_atomic().
117  *
118  * Effectively a wrapper around kunmap_local() which additionally undoes
119  * the side effects of kmap_atomic(), i.e. reenabling pagefaults and
120  * preemption.
121  */
122 
123 /* Highmem related interfaces for management code */
124 static inline unsigned int nr_free_highpages(void);
125 static inline unsigned long totalhigh_pages(void);
126 
127 #ifndef ARCH_HAS_FLUSH_ANON_PAGE
flush_anon_page(struct vm_area_struct * vma,struct page * page,unsigned long vmaddr)128 static inline void flush_anon_page(struct vm_area_struct *vma, struct page *page, unsigned long vmaddr)
129 {
130 }
131 #endif
132 
133 #ifndef ARCH_IMPLEMENTS_FLUSH_KERNEL_VMAP_RANGE
flush_kernel_vmap_range(void * vaddr,int size)134 static inline void flush_kernel_vmap_range(void *vaddr, int size)
135 {
136 }
invalidate_kernel_vmap_range(void * vaddr,int size)137 static inline void invalidate_kernel_vmap_range(void *vaddr, int size)
138 {
139 }
140 #endif
141 
142 /* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */
143 #ifndef clear_user_highpage
clear_user_highpage(struct page * page,unsigned long vaddr)144 static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
145 {
146 	void *addr = kmap_atomic(page);
147 	clear_user_page(addr, vaddr, page);
148 	kunmap_atomic(addr);
149 }
150 #endif
151 
152 #ifndef __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE_MOVABLE
153 /**
154  * alloc_zeroed_user_highpage_movable - Allocate a zeroed HIGHMEM page for a VMA that the caller knows can move
155  * @vma: The VMA the page is to be allocated for
156  * @vaddr: The virtual address the page will be inserted into
157  *
158  * This function will allocate a page for a VMA that the caller knows will
159  * be able to migrate in the future using move_pages() or reclaimed
160  *
161  * An architecture may override this function by defining
162  * __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE_MOVABLE and providing their own
163  * implementation.
164  */
165 static inline struct page *
alloc_zeroed_user_highpage_movable(struct vm_area_struct * vma,unsigned long vaddr)166 alloc_zeroed_user_highpage_movable(struct vm_area_struct *vma,
167 				   unsigned long vaddr)
168 {
169 	struct page *page = alloc_page_vma(GFP_HIGHUSER_MOVABLE | __GFP_CMA, vma, vaddr);
170 
171 	if (page)
172 		clear_user_highpage(page, vaddr);
173 
174 	return page;
175 }
176 #endif
177 
clear_highpage(struct page * page)178 static inline void clear_highpage(struct page *page)
179 {
180 	void *kaddr = kmap_atomic(page);
181 	clear_page(kaddr);
182 	kunmap_atomic(kaddr);
183 }
184 
185 #ifndef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
186 
tag_clear_highpage(struct page * page)187 static inline void tag_clear_highpage(struct page *page)
188 {
189 }
190 
191 #endif
192 
193 /*
194  * If we pass in a base or tail page, we can zero up to PAGE_SIZE.
195  * If we pass in a head page, we can zero up to the size of the compound page.
196  */
197 #if defined(CONFIG_HIGHMEM) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
198 void zero_user_segments(struct page *page, unsigned start1, unsigned end1,
199 		unsigned start2, unsigned end2);
200 #else /* !HIGHMEM || !TRANSPARENT_HUGEPAGE */
zero_user_segments(struct page * page,unsigned start1,unsigned end1,unsigned start2,unsigned end2)201 static inline void zero_user_segments(struct page *page,
202 		unsigned start1, unsigned end1,
203 		unsigned start2, unsigned end2)
204 {
205 	void *kaddr = kmap_atomic(page);
206 	unsigned int i;
207 
208 	BUG_ON(end1 > page_size(page) || end2 > page_size(page));
209 
210 	if (end1 > start1)
211 		memset(kaddr + start1, 0, end1 - start1);
212 
213 	if (end2 > start2)
214 		memset(kaddr + start2, 0, end2 - start2);
215 
216 	kunmap_atomic(kaddr);
217 	for (i = 0; i < compound_nr(page); i++)
218 		flush_dcache_page(page + i);
219 }
220 #endif /* !HIGHMEM || !TRANSPARENT_HUGEPAGE */
221 
zero_user_segment(struct page * page,unsigned start,unsigned end)222 static inline void zero_user_segment(struct page *page,
223 	unsigned start, unsigned end)
224 {
225 	zero_user_segments(page, start, end, 0, 0);
226 }
227 
zero_user(struct page * page,unsigned start,unsigned size)228 static inline void zero_user(struct page *page,
229 	unsigned start, unsigned size)
230 {
231 	zero_user_segments(page, start, start + size, 0, 0);
232 }
233 
234 #ifndef __HAVE_ARCH_COPY_USER_HIGHPAGE
235 
copy_user_highpage(struct page * to,struct page * from,unsigned long vaddr,struct vm_area_struct * vma)236 static inline void copy_user_highpage(struct page *to, struct page *from,
237 	unsigned long vaddr, struct vm_area_struct *vma)
238 {
239 	char *vfrom, *vto;
240 
241 	vfrom = kmap_atomic(from);
242 	vto = kmap_atomic(to);
243 	copy_user_page(vto, vfrom, vaddr, to);
244 	kunmap_atomic(vto);
245 	kunmap_atomic(vfrom);
246 }
247 
248 #endif
249 
250 #ifdef copy_mc_to_kernel
copy_mc_user_highpage(struct page * to,struct page * from,unsigned long vaddr,struct vm_area_struct * vma)251 static inline int copy_mc_user_highpage(struct page *to, struct page *from,
252 					unsigned long vaddr, struct vm_area_struct *vma)
253 {
254 	unsigned long ret;
255 	char *vfrom, *vto;
256 
257 	vfrom = kmap_local_page(from);
258 	vto = kmap_local_page(to);
259 	ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE);
260 	kunmap_local(vto);
261 	kunmap_local(vfrom);
262 
263 	return ret;
264 }
265 #else
copy_mc_user_highpage(struct page * to,struct page * from,unsigned long vaddr,struct vm_area_struct * vma)266 static inline int copy_mc_user_highpage(struct page *to, struct page *from,
267 					unsigned long vaddr, struct vm_area_struct *vma)
268 {
269 	copy_user_highpage(to, from, vaddr, vma);
270 	return 0;
271 }
272 #endif
273 
274 #ifndef __HAVE_ARCH_COPY_HIGHPAGE
275 
copy_highpage(struct page * to,struct page * from)276 static inline void copy_highpage(struct page *to, struct page *from)
277 {
278 	char *vfrom, *vto;
279 
280 	vfrom = kmap_atomic(from);
281 	vto = kmap_atomic(to);
282 	copy_page(vto, vfrom);
283 	kunmap_atomic(vto);
284 	kunmap_atomic(vfrom);
285 }
286 
287 #endif
288 
memcpy_page(struct page * dst_page,size_t dst_off,struct page * src_page,size_t src_off,size_t len)289 static inline void memcpy_page(struct page *dst_page, size_t dst_off,
290 			       struct page *src_page, size_t src_off,
291 			       size_t len)
292 {
293 	char *dst = kmap_local_page(dst_page);
294 	char *src = kmap_local_page(src_page);
295 
296 	VM_BUG_ON(dst_off + len > PAGE_SIZE || src_off + len > PAGE_SIZE);
297 	memcpy(dst + dst_off, src + src_off, len);
298 	kunmap_local(src);
299 	kunmap_local(dst);
300 }
301 
memmove_page(struct page * dst_page,size_t dst_off,struct page * src_page,size_t src_off,size_t len)302 static inline void memmove_page(struct page *dst_page, size_t dst_off,
303 			       struct page *src_page, size_t src_off,
304 			       size_t len)
305 {
306 	char *dst = kmap_local_page(dst_page);
307 	char *src = kmap_local_page(src_page);
308 
309 	VM_BUG_ON(dst_off + len > PAGE_SIZE || src_off + len > PAGE_SIZE);
310 	memmove(dst + dst_off, src + src_off, len);
311 	kunmap_local(src);
312 	kunmap_local(dst);
313 }
314 
memset_page(struct page * page,size_t offset,int val,size_t len)315 static inline void memset_page(struct page *page, size_t offset, int val,
316 			       size_t len)
317 {
318 	char *addr = kmap_local_page(page);
319 
320 	VM_BUG_ON(offset + len > PAGE_SIZE);
321 	memset(addr + offset, val, len);
322 	kunmap_local(addr);
323 }
324 
memcpy_from_page(char * to,struct page * page,size_t offset,size_t len)325 static inline void memcpy_from_page(char *to, struct page *page,
326 				    size_t offset, size_t len)
327 {
328 	char *from = kmap_local_page(page);
329 
330 	VM_BUG_ON(offset + len > PAGE_SIZE);
331 	memcpy(to, from + offset, len);
332 	kunmap_local(from);
333 }
334 
memcpy_to_page(struct page * page,size_t offset,const char * from,size_t len)335 static inline void memcpy_to_page(struct page *page, size_t offset,
336 				  const char *from, size_t len)
337 {
338 	char *to = kmap_local_page(page);
339 
340 	VM_BUG_ON(offset + len > PAGE_SIZE);
341 	memcpy(to + offset, from, len);
342 	flush_dcache_page(page);
343 	kunmap_local(to);
344 }
345 
memzero_page(struct page * page,size_t offset,size_t len)346 static inline void memzero_page(struct page *page, size_t offset, size_t len)
347 {
348 	char *addr = kmap_local_page(page);
349 	memset(addr + offset, 0, len);
350 	flush_dcache_page(page);
351 	kunmap_local(addr);
352 }
353 
354 #endif /* _LINUX_HIGHMEM_H */
355