Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
2 | #ifndef _LINUX_HIGHMEM_H |
3 | #define _LINUX_HIGHMEM_H | |
4 | ||
1da177e4 | 5 | #include <linux/fs.h> |
597781f3 | 6 | #include <linux/kernel.h> |
187f1882 | 7 | #include <linux/bug.h> |
1da177e4 | 8 | #include <linux/mm.h> |
ad76fb6b | 9 | #include <linux/uaccess.h> |
43b3a0c7 | 10 | #include <linux/hardirq.h> |
1da177e4 LT |
11 | |
12 | #include <asm/cacheflush.h> | |
13 | ||
03beb076 | 14 | #ifndef ARCH_HAS_FLUSH_ANON_PAGE |
a6f36be3 | 15 | static inline void flush_anon_page(struct vm_area_struct *vma, struct page *page, unsigned long vmaddr) |
03beb076 JB |
16 | { |
17 | } | |
18 | #endif | |
19 | ||
5a3a5a98 JB |
20 | #ifndef ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE |
21 | static inline void flush_kernel_dcache_page(struct page *page) | |
22 | { | |
23 | } | |
9df5f741 JB |
24 | static inline void flush_kernel_vmap_range(void *vaddr, int size) |
25 | { | |
26 | } | |
27 | static inline void invalidate_kernel_vmap_range(void *vaddr, int size) | |
28 | { | |
29 | } | |
5a3a5a98 JB |
30 | #endif |
31 | ||
3688e07f KG |
32 | #include <asm/kmap_types.h> |
33 | ||
3688e07f | 34 | #ifdef CONFIG_HIGHMEM |
20b271df | 35 | extern void *kmap_atomic_high_prot(struct page *page, pgprot_t prot); |
abca2500 | 36 | extern void kunmap_atomic_high(void *kvaddr); |
1da177e4 LT |
37 | #include <asm/highmem.h> |
38 | ||
525aaf9b IW |
39 | #ifndef ARCH_HAS_KMAP_FLUSH_TLB |
40 | static inline void kmap_flush_tlb(unsigned long addr) { } | |
41 | #endif | |
42 | ||
43 | void *kmap_high(struct page *page); | |
44 | static inline void *kmap(struct page *page) | |
45 | { | |
46 | void *addr; | |
47 | ||
48 | might_sleep(); | |
49 | if (!PageHighMem(page)) | |
50 | addr = page_address(page); | |
51 | else | |
52 | addr = kmap_high(page); | |
53 | kmap_flush_tlb((unsigned long)addr); | |
54 | return addr; | |
55 | } | |
56 | ||
e23c4597 IW |
57 | void kunmap_high(struct page *page); |
58 | ||
59 | static inline void kunmap(struct page *page) | |
60 | { | |
61 | might_sleep(); | |
62 | if (!PageHighMem(page)) | |
63 | return; | |
64 | kunmap_high(page); | |
65 | } | |
66 | ||
78b6d91e IW |
67 | /* |
68 | * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap because | |
69 | * no global lock is needed and because the kmap code must perform a global TLB | |
70 | * invalidation when the kmap pool wraps. | |
71 | * | |
72 | * However when holding an atomic kmap is is not legal to sleep, so atomic | |
73 | * kmaps are appropriate for short, tight code paths only. | |
74 | * | |
75 | * The use of kmap_atomic/kunmap_atomic is discouraged - kmap/kunmap | |
76 | * gives a more generic (and caching) interface. But kmap_atomic can | |
77 | * be used in IRQ contexts, so in some (very limited) cases we need | |
78 | * it. | |
79 | */ | |
20b271df | 80 | static inline void *kmap_atomic_prot(struct page *page, pgprot_t prot) |
78b6d91e IW |
81 | { |
82 | preempt_disable(); | |
83 | pagefault_disable(); | |
84 | if (!PageHighMem(page)) | |
85 | return page_address(page); | |
20b271df | 86 | return kmap_atomic_high_prot(page, prot); |
78b6d91e | 87 | } |
20b271df | 88 | #define kmap_atomic(page) kmap_atomic_prot(page, kmap_prot) |
78b6d91e | 89 | |
1da177e4 LT |
90 | /* declarations for linux/mm/highmem.c */ |
91 | unsigned int nr_free_highpages(void); | |
ca79b0c2 AK |
92 | extern atomic_long_t _totalhigh_pages; |
93 | static inline unsigned long totalhigh_pages(void) | |
94 | { | |
95 | return (unsigned long)atomic_long_read(&_totalhigh_pages); | |
96 | } | |
97 | ||
98 | static inline void totalhigh_pages_inc(void) | |
99 | { | |
100 | atomic_long_inc(&_totalhigh_pages); | |
101 | } | |
102 | ||
103 | static inline void totalhigh_pages_dec(void) | |
104 | { | |
105 | atomic_long_dec(&_totalhigh_pages); | |
106 | } | |
107 | ||
108 | static inline void totalhigh_pages_add(long count) | |
109 | { | |
110 | atomic_long_add(count, &_totalhigh_pages); | |
111 | } | |
112 | ||
113 | static inline void totalhigh_pages_set(long val) | |
114 | { | |
115 | atomic_long_set(&_totalhigh_pages, val); | |
116 | } | |
1da177e4 | 117 | |
ce6234b5 JF |
118 | void kmap_flush_unused(void); |
119 | ||
5a178119 MG |
120 | struct page *kmap_to_page(void *addr); |
121 | ||
1da177e4 LT |
122 | #else /* CONFIG_HIGHMEM */ |
123 | ||
124 | static inline unsigned int nr_free_highpages(void) { return 0; } | |
125 | ||
5a178119 MG |
126 | static inline struct page *kmap_to_page(void *addr) |
127 | { | |
128 | return virt_to_page(addr); | |
129 | } | |
130 | ||
ca79b0c2 | 131 | static inline unsigned long totalhigh_pages(void) { return 0UL; } |
c1f60a5a | 132 | |
1da177e4 LT |
133 | static inline void *kmap(struct page *page) |
134 | { | |
135 | might_sleep(); | |
136 | return page_address(page); | |
137 | } | |
138 | ||
e23c4597 IW |
139 | static inline void kunmap_high(struct page *page) |
140 | { | |
141 | } | |
142 | ||
31c91132 MW |
143 | static inline void kunmap(struct page *page) |
144 | { | |
7438f363 IW |
145 | #ifdef ARCH_HAS_FLUSH_ON_KUNMAP |
146 | kunmap_flush_on_unmap(page_address(page)); | |
147 | #endif | |
31c91132 | 148 | } |
1da177e4 | 149 | |
a24401bc | 150 | static inline void *kmap_atomic(struct page *page) |
254f9c5c | 151 | { |
2cb7c9cb | 152 | preempt_disable(); |
254f9c5c GU |
153 | pagefault_disable(); |
154 | return page_address(page); | |
155 | } | |
a24401bc | 156 | #define kmap_atomic_prot(page, prot) kmap_atomic(page) |
254f9c5c | 157 | |
abca2500 | 158 | static inline void kunmap_atomic_high(void *addr) |
4e60c86b | 159 | { |
abca2500 | 160 | /* |
7438f363 | 161 | * Mostly nothing to do in the CONFIG_HIGHMEM=n case as kunmap_atomic() |
abca2500 IW |
162 | * handles re-enabling faults + preemption |
163 | */ | |
7438f363 IW |
164 | #ifdef ARCH_HAS_FLUSH_ON_KUNMAP |
165 | kunmap_flush_on_unmap(addr); | |
166 | #endif | |
4e60c86b AK |
167 | } |
168 | ||
3e4d3af5 | 169 | #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn)) |
ce6234b5 JF |
170 | |
171 | #define kmap_flush_unused() do {} while(0) | |
1da177e4 LT |
172 | |
173 | #endif /* CONFIG_HIGHMEM */ | |
174 | ||
a8e23a29 PZ |
175 | #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32) |
176 | ||
177 | DECLARE_PER_CPU(int, __kmap_atomic_idx); | |
178 | ||
179 | static inline int kmap_atomic_idx_push(void) | |
180 | { | |
cfb82434 CL |
181 | int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1; |
182 | ||
a8e23a29 PZ |
183 | #ifdef CONFIG_DEBUG_HIGHMEM |
184 | WARN_ON_ONCE(in_irq() && !irqs_disabled()); | |
1d352bfd | 185 | BUG_ON(idx >= KM_TYPE_NR); |
a8e23a29 PZ |
186 | #endif |
187 | return idx; | |
188 | } | |
189 | ||
20273941 PZ |
190 | static inline int kmap_atomic_idx(void) |
191 | { | |
cfb82434 | 192 | return __this_cpu_read(__kmap_atomic_idx) - 1; |
20273941 PZ |
193 | } |
194 | ||
cfb82434 | 195 | static inline void kmap_atomic_idx_pop(void) |
a8e23a29 | 196 | { |
a8e23a29 | 197 | #ifdef CONFIG_DEBUG_HIGHMEM |
cfb82434 CL |
198 | int idx = __this_cpu_dec_return(__kmap_atomic_idx); |
199 | ||
a8e23a29 | 200 | BUG_ON(idx < 0); |
cfb82434 CL |
201 | #else |
202 | __this_cpu_dec(__kmap_atomic_idx); | |
a8e23a29 | 203 | #endif |
a8e23a29 PZ |
204 | } |
205 | ||
206 | #endif | |
207 | ||
3e4d3af5 PZ |
208 | /* |
209 | * Prevent people trying to call kunmap_atomic() as if it were kunmap() | |
210 | * kunmap_atomic() should get the return value of kmap_atomic, not the page. | |
211 | */ | |
1285e4c8 | 212 | #define kunmap_atomic(addr) \ |
980c19e3 CW |
213 | do { \ |
214 | BUILD_BUG_ON(__same_type((addr), struct page *)); \ | |
abca2500 IW |
215 | kunmap_atomic_high(addr); \ |
216 | pagefault_enable(); \ | |
217 | preempt_enable(); \ | |
980c19e3 CW |
218 | } while (0) |
219 | ||
980c19e3 | 220 | |
1da177e4 | 221 | /* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */ |
487ff320 | 222 | #ifndef clear_user_highpage |
1da177e4 LT |
223 | static inline void clear_user_highpage(struct page *page, unsigned long vaddr) |
224 | { | |
1ec9c5dd | 225 | void *addr = kmap_atomic(page); |
1da177e4 | 226 | clear_user_page(addr, vaddr, page); |
1ec9c5dd | 227 | kunmap_atomic(addr); |
1da177e4 | 228 | } |
487ff320 | 229 | #endif |
1da177e4 LT |
230 | |
231 | #ifndef __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE | |
769848c0 MG |
232 | /** |
233 | * __alloc_zeroed_user_highpage - Allocate a zeroed HIGHMEM page for a VMA with caller-specified movable GFP flags | |
234 | * @movableflags: The GFP flags related to the pages future ability to move like __GFP_MOVABLE | |
235 | * @vma: The VMA the page is to be allocated for | |
236 | * @vaddr: The virtual address the page will be inserted into | |
237 | * | |
238 | * This function will allocate a page for a VMA but the caller is expected | |
239 | * to specify via movableflags whether the page will be movable in the | |
240 | * future or not | |
241 | * | |
242 | * An architecture may override this function by defining | |
243 | * __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE and providing their own | |
244 | * implementation. | |
245 | */ | |
1da177e4 | 246 | static inline struct page * |
769848c0 MG |
247 | __alloc_zeroed_user_highpage(gfp_t movableflags, |
248 | struct vm_area_struct *vma, | |
249 | unsigned long vaddr) | |
1da177e4 | 250 | { |
769848c0 MG |
251 | struct page *page = alloc_page_vma(GFP_HIGHUSER | movableflags, |
252 | vma, vaddr); | |
1da177e4 LT |
253 | |
254 | if (page) | |
255 | clear_user_highpage(page, vaddr); | |
256 | ||
257 | return page; | |
258 | } | |
259 | #endif | |
260 | ||
769848c0 MG |
261 | /** |
262 | * alloc_zeroed_user_highpage_movable - Allocate a zeroed HIGHMEM page for a VMA that the caller knows can move | |
263 | * @vma: The VMA the page is to be allocated for | |
264 | * @vaddr: The virtual address the page will be inserted into | |
265 | * | |
266 | * This function will allocate a page for a VMA that the caller knows will | |
267 | * be able to migrate in the future using move_pages() or reclaimed | |
268 | */ | |
269 | static inline struct page * | |
270 | alloc_zeroed_user_highpage_movable(struct vm_area_struct *vma, | |
271 | unsigned long vaddr) | |
272 | { | |
273 | return __alloc_zeroed_user_highpage(__GFP_MOVABLE, vma, vaddr); | |
274 | } | |
275 | ||
1da177e4 LT |
276 | static inline void clear_highpage(struct page *page) |
277 | { | |
1ec9c5dd | 278 | void *kaddr = kmap_atomic(page); |
1da177e4 | 279 | clear_page(kaddr); |
1ec9c5dd | 280 | kunmap_atomic(kaddr); |
1da177e4 LT |
281 | } |
282 | ||
eebd2aa3 CL |
283 | static inline void zero_user_segments(struct page *page, |
284 | unsigned start1, unsigned end1, | |
285 | unsigned start2, unsigned end2) | |
286 | { | |
1ec9c5dd | 287 | void *kaddr = kmap_atomic(page); |
eebd2aa3 CL |
288 | |
289 | BUG_ON(end1 > PAGE_SIZE || end2 > PAGE_SIZE); | |
290 | ||
291 | if (end1 > start1) | |
292 | memset(kaddr + start1, 0, end1 - start1); | |
293 | ||
294 | if (end2 > start2) | |
295 | memset(kaddr + start2, 0, end2 - start2); | |
296 | ||
1ec9c5dd | 297 | kunmap_atomic(kaddr); |
eebd2aa3 CL |
298 | flush_dcache_page(page); |
299 | } | |
300 | ||
301 | static inline void zero_user_segment(struct page *page, | |
302 | unsigned start, unsigned end) | |
303 | { | |
304 | zero_user_segments(page, start, end, 0, 0); | |
305 | } | |
306 | ||
307 | static inline void zero_user(struct page *page, | |
308 | unsigned start, unsigned size) | |
309 | { | |
310 | zero_user_segments(page, start, start + size, 0, 0); | |
311 | } | |
01f2705d | 312 | |
77fff4ae AN |
313 | #ifndef __HAVE_ARCH_COPY_USER_HIGHPAGE |
314 | ||
9de455b2 AN |
315 | static inline void copy_user_highpage(struct page *to, struct page *from, |
316 | unsigned long vaddr, struct vm_area_struct *vma) | |
1da177e4 LT |
317 | { |
318 | char *vfrom, *vto; | |
319 | ||
1ec9c5dd CW |
320 | vfrom = kmap_atomic(from); |
321 | vto = kmap_atomic(to); | |
1da177e4 | 322 | copy_user_page(vto, vfrom, vaddr, to); |
1ec9c5dd CW |
323 | kunmap_atomic(vto); |
324 | kunmap_atomic(vfrom); | |
1da177e4 LT |
325 | } |
326 | ||
77fff4ae AN |
327 | #endif |
328 | ||
a4602b62 KA |
329 | #ifndef __HAVE_ARCH_COPY_HIGHPAGE |
330 | ||
1da177e4 LT |
331 | static inline void copy_highpage(struct page *to, struct page *from) |
332 | { | |
333 | char *vfrom, *vto; | |
334 | ||
1ec9c5dd CW |
335 | vfrom = kmap_atomic(from); |
336 | vto = kmap_atomic(to); | |
1da177e4 | 337 | copy_page(vto, vfrom); |
1ec9c5dd CW |
338 | kunmap_atomic(vto); |
339 | kunmap_atomic(vfrom); | |
1da177e4 LT |
340 | } |
341 | ||
a4602b62 KA |
342 | #endif |
343 | ||
1da177e4 | 344 | #endif /* _LINUX_HIGHMEM_H */ |