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 |
1da177e4 LT |
35 | #include <asm/highmem.h> |
36 | ||
525aaf9b IW |
37 | #ifndef ARCH_HAS_KMAP_FLUSH_TLB |
38 | static inline void kmap_flush_tlb(unsigned long addr) { } | |
39 | #endif | |
40 | ||
41 | void *kmap_high(struct page *page); | |
42 | static inline void *kmap(struct page *page) | |
43 | { | |
44 | void *addr; | |
45 | ||
46 | might_sleep(); | |
47 | if (!PageHighMem(page)) | |
48 | addr = page_address(page); | |
49 | else | |
50 | addr = kmap_high(page); | |
51 | kmap_flush_tlb((unsigned long)addr); | |
52 | return addr; | |
53 | } | |
54 | ||
e23c4597 IW |
55 | void kunmap_high(struct page *page); |
56 | ||
57 | static inline void kunmap(struct page *page) | |
58 | { | |
59 | might_sleep(); | |
60 | if (!PageHighMem(page)) | |
61 | return; | |
62 | kunmap_high(page); | |
63 | } | |
64 | ||
1da177e4 LT |
65 | /* declarations for linux/mm/highmem.c */ |
66 | unsigned int nr_free_highpages(void); | |
ca79b0c2 AK |
67 | extern atomic_long_t _totalhigh_pages; |
68 | static inline unsigned long totalhigh_pages(void) | |
69 | { | |
70 | return (unsigned long)atomic_long_read(&_totalhigh_pages); | |
71 | } | |
72 | ||
73 | static inline void totalhigh_pages_inc(void) | |
74 | { | |
75 | atomic_long_inc(&_totalhigh_pages); | |
76 | } | |
77 | ||
78 | static inline void totalhigh_pages_dec(void) | |
79 | { | |
80 | atomic_long_dec(&_totalhigh_pages); | |
81 | } | |
82 | ||
83 | static inline void totalhigh_pages_add(long count) | |
84 | { | |
85 | atomic_long_add(count, &_totalhigh_pages); | |
86 | } | |
87 | ||
88 | static inline void totalhigh_pages_set(long val) | |
89 | { | |
90 | atomic_long_set(&_totalhigh_pages, val); | |
91 | } | |
1da177e4 | 92 | |
ce6234b5 JF |
93 | void kmap_flush_unused(void); |
94 | ||
5a178119 MG |
95 | struct page *kmap_to_page(void *addr); |
96 | ||
1da177e4 LT |
97 | #else /* CONFIG_HIGHMEM */ |
98 | ||
99 | static inline unsigned int nr_free_highpages(void) { return 0; } | |
100 | ||
5a178119 MG |
101 | static inline struct page *kmap_to_page(void *addr) |
102 | { | |
103 | return virt_to_page(addr); | |
104 | } | |
105 | ||
ca79b0c2 | 106 | static inline unsigned long totalhigh_pages(void) { return 0UL; } |
c1f60a5a | 107 | |
a6ca1b99 | 108 | #ifndef ARCH_HAS_KMAP |
1da177e4 LT |
109 | static inline void *kmap(struct page *page) |
110 | { | |
111 | might_sleep(); | |
112 | return page_address(page); | |
113 | } | |
114 | ||
e23c4597 IW |
115 | static inline void kunmap_high(struct page *page) |
116 | { | |
117 | } | |
118 | ||
31c91132 MW |
119 | static inline void kunmap(struct page *page) |
120 | { | |
121 | } | |
1da177e4 | 122 | |
a24401bc | 123 | static inline void *kmap_atomic(struct page *page) |
254f9c5c | 124 | { |
2cb7c9cb | 125 | preempt_disable(); |
254f9c5c GU |
126 | pagefault_disable(); |
127 | return page_address(page); | |
128 | } | |
a24401bc | 129 | #define kmap_atomic_prot(page, prot) kmap_atomic(page) |
254f9c5c | 130 | |
3e4d3af5 | 131 | static inline void __kunmap_atomic(void *addr) |
4e60c86b AK |
132 | { |
133 | pagefault_enable(); | |
2cb7c9cb | 134 | preempt_enable(); |
4e60c86b AK |
135 | } |
136 | ||
3e4d3af5 | 137 | #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn)) |
ce6234b5 JF |
138 | |
139 | #define kmap_flush_unused() do {} while(0) | |
a6ca1b99 | 140 | #endif |
1da177e4 LT |
141 | |
142 | #endif /* CONFIG_HIGHMEM */ | |
143 | ||
a8e23a29 PZ |
144 | #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32) |
145 | ||
146 | DECLARE_PER_CPU(int, __kmap_atomic_idx); | |
147 | ||
148 | static inline int kmap_atomic_idx_push(void) | |
149 | { | |
cfb82434 CL |
150 | int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1; |
151 | ||
a8e23a29 PZ |
152 | #ifdef CONFIG_DEBUG_HIGHMEM |
153 | WARN_ON_ONCE(in_irq() && !irqs_disabled()); | |
1d352bfd | 154 | BUG_ON(idx >= KM_TYPE_NR); |
a8e23a29 PZ |
155 | #endif |
156 | return idx; | |
157 | } | |
158 | ||
20273941 PZ |
159 | static inline int kmap_atomic_idx(void) |
160 | { | |
cfb82434 | 161 | return __this_cpu_read(__kmap_atomic_idx) - 1; |
20273941 PZ |
162 | } |
163 | ||
cfb82434 | 164 | static inline void kmap_atomic_idx_pop(void) |
a8e23a29 | 165 | { |
a8e23a29 | 166 | #ifdef CONFIG_DEBUG_HIGHMEM |
cfb82434 CL |
167 | int idx = __this_cpu_dec_return(__kmap_atomic_idx); |
168 | ||
a8e23a29 | 169 | BUG_ON(idx < 0); |
cfb82434 CL |
170 | #else |
171 | __this_cpu_dec(__kmap_atomic_idx); | |
a8e23a29 | 172 | #endif |
a8e23a29 PZ |
173 | } |
174 | ||
175 | #endif | |
176 | ||
3e4d3af5 PZ |
177 | /* |
178 | * Prevent people trying to call kunmap_atomic() as if it were kunmap() | |
179 | * kunmap_atomic() should get the return value of kmap_atomic, not the page. | |
180 | */ | |
1285e4c8 | 181 | #define kunmap_atomic(addr) \ |
980c19e3 CW |
182 | do { \ |
183 | BUILD_BUG_ON(__same_type((addr), struct page *)); \ | |
184 | __kunmap_atomic(addr); \ | |
185 | } while (0) | |
186 | ||
980c19e3 | 187 | |
1da177e4 | 188 | /* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */ |
487ff320 | 189 | #ifndef clear_user_highpage |
1da177e4 LT |
190 | static inline void clear_user_highpage(struct page *page, unsigned long vaddr) |
191 | { | |
1ec9c5dd | 192 | void *addr = kmap_atomic(page); |
1da177e4 | 193 | clear_user_page(addr, vaddr, page); |
1ec9c5dd | 194 | kunmap_atomic(addr); |
1da177e4 | 195 | } |
487ff320 | 196 | #endif |
1da177e4 LT |
197 | |
198 | #ifndef __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE | |
769848c0 MG |
199 | /** |
200 | * __alloc_zeroed_user_highpage - Allocate a zeroed HIGHMEM page for a VMA with caller-specified movable GFP flags | |
201 | * @movableflags: The GFP flags related to the pages future ability to move like __GFP_MOVABLE | |
202 | * @vma: The VMA the page is to be allocated for | |
203 | * @vaddr: The virtual address the page will be inserted into | |
204 | * | |
205 | * This function will allocate a page for a VMA but the caller is expected | |
206 | * to specify via movableflags whether the page will be movable in the | |
207 | * future or not | |
208 | * | |
209 | * An architecture may override this function by defining | |
210 | * __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE and providing their own | |
211 | * implementation. | |
212 | */ | |
1da177e4 | 213 | static inline struct page * |
769848c0 MG |
214 | __alloc_zeroed_user_highpage(gfp_t movableflags, |
215 | struct vm_area_struct *vma, | |
216 | unsigned long vaddr) | |
1da177e4 | 217 | { |
769848c0 MG |
218 | struct page *page = alloc_page_vma(GFP_HIGHUSER | movableflags, |
219 | vma, vaddr); | |
1da177e4 LT |
220 | |
221 | if (page) | |
222 | clear_user_highpage(page, vaddr); | |
223 | ||
224 | return page; | |
225 | } | |
226 | #endif | |
227 | ||
769848c0 MG |
228 | /** |
229 | * alloc_zeroed_user_highpage_movable - Allocate a zeroed HIGHMEM page for a VMA that the caller knows can move | |
230 | * @vma: The VMA the page is to be allocated for | |
231 | * @vaddr: The virtual address the page will be inserted into | |
232 | * | |
233 | * This function will allocate a page for a VMA that the caller knows will | |
234 | * be able to migrate in the future using move_pages() or reclaimed | |
235 | */ | |
236 | static inline struct page * | |
237 | alloc_zeroed_user_highpage_movable(struct vm_area_struct *vma, | |
238 | unsigned long vaddr) | |
239 | { | |
240 | return __alloc_zeroed_user_highpage(__GFP_MOVABLE, vma, vaddr); | |
241 | } | |
242 | ||
1da177e4 LT |
243 | static inline void clear_highpage(struct page *page) |
244 | { | |
1ec9c5dd | 245 | void *kaddr = kmap_atomic(page); |
1da177e4 | 246 | clear_page(kaddr); |
1ec9c5dd | 247 | kunmap_atomic(kaddr); |
1da177e4 LT |
248 | } |
249 | ||
eebd2aa3 CL |
250 | static inline void zero_user_segments(struct page *page, |
251 | unsigned start1, unsigned end1, | |
252 | unsigned start2, unsigned end2) | |
253 | { | |
1ec9c5dd | 254 | void *kaddr = kmap_atomic(page); |
eebd2aa3 CL |
255 | |
256 | BUG_ON(end1 > PAGE_SIZE || end2 > PAGE_SIZE); | |
257 | ||
258 | if (end1 > start1) | |
259 | memset(kaddr + start1, 0, end1 - start1); | |
260 | ||
261 | if (end2 > start2) | |
262 | memset(kaddr + start2, 0, end2 - start2); | |
263 | ||
1ec9c5dd | 264 | kunmap_atomic(kaddr); |
eebd2aa3 CL |
265 | flush_dcache_page(page); |
266 | } | |
267 | ||
268 | static inline void zero_user_segment(struct page *page, | |
269 | unsigned start, unsigned end) | |
270 | { | |
271 | zero_user_segments(page, start, end, 0, 0); | |
272 | } | |
273 | ||
274 | static inline void zero_user(struct page *page, | |
275 | unsigned start, unsigned size) | |
276 | { | |
277 | zero_user_segments(page, start, start + size, 0, 0); | |
278 | } | |
01f2705d | 279 | |
77fff4ae AN |
280 | #ifndef __HAVE_ARCH_COPY_USER_HIGHPAGE |
281 | ||
9de455b2 AN |
282 | static inline void copy_user_highpage(struct page *to, struct page *from, |
283 | unsigned long vaddr, struct vm_area_struct *vma) | |
1da177e4 LT |
284 | { |
285 | char *vfrom, *vto; | |
286 | ||
1ec9c5dd CW |
287 | vfrom = kmap_atomic(from); |
288 | vto = kmap_atomic(to); | |
1da177e4 | 289 | copy_user_page(vto, vfrom, vaddr, to); |
1ec9c5dd CW |
290 | kunmap_atomic(vto); |
291 | kunmap_atomic(vfrom); | |
1da177e4 LT |
292 | } |
293 | ||
77fff4ae AN |
294 | #endif |
295 | ||
a4602b62 KA |
296 | #ifndef __HAVE_ARCH_COPY_HIGHPAGE |
297 | ||
1da177e4 LT |
298 | static inline void copy_highpage(struct page *to, struct page *from) |
299 | { | |
300 | char *vfrom, *vto; | |
301 | ||
1ec9c5dd CW |
302 | vfrom = kmap_atomic(from); |
303 | vto = kmap_atomic(to); | |
1da177e4 | 304 | copy_page(vto, vfrom); |
1ec9c5dd CW |
305 | kunmap_atomic(vto); |
306 | kunmap_atomic(vfrom); | |
1da177e4 LT |
307 | } |
308 | ||
a4602b62 KA |
309 | #endif |
310 | ||
1da177e4 | 311 | #endif /* _LINUX_HIGHMEM_H */ |