These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / include / linux / highmem.h
1 #ifndef _LINUX_HIGHMEM_H
2 #define _LINUX_HIGHMEM_H
3
4 #include <linux/fs.h>
5 #include <linux/kernel.h>
6 #include <linux/bug.h>
7 #include <linux/mm.h>
8 #include <linux/uaccess.h>
9 #include <linux/hardirq.h>
10 #include <linux/sched.h>
11
12 #include <asm/cacheflush.h>
13
14 #ifndef ARCH_HAS_FLUSH_ANON_PAGE
15 static inline void flush_anon_page(struct vm_area_struct *vma, struct page *page, unsigned long vmaddr)
16 {
17 }
18 #endif
19
20 #ifndef ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE
21 static inline void flush_kernel_dcache_page(struct page *page)
22 {
23 }
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 }
30 #endif
31
32 #include <asm/kmap_types.h>
33
34 #ifdef CONFIG_HIGHMEM
35 #include <asm/highmem.h>
36
37 /* declarations for linux/mm/highmem.c */
38 unsigned int nr_free_highpages(void);
39 extern unsigned long totalhigh_pages;
40
41 void kmap_flush_unused(void);
42
43 struct page *kmap_to_page(void *addr);
44
45 #else /* CONFIG_HIGHMEM */
46
47 static inline unsigned int nr_free_highpages(void) { return 0; }
48
49 static inline struct page *kmap_to_page(void *addr)
50 {
51         return virt_to_page(addr);
52 }
53
54 #define totalhigh_pages 0UL
55
56 #ifndef ARCH_HAS_KMAP
57 static inline void *kmap(struct page *page)
58 {
59         might_sleep();
60         return page_address(page);
61 }
62
63 static inline void kunmap(struct page *page)
64 {
65 }
66
67 static inline void *kmap_atomic(struct page *page)
68 {
69         preempt_disable_nort();
70         pagefault_disable();
71         return page_address(page);
72 }
73 #define kmap_atomic_prot(page, prot)    kmap_atomic(page)
74
75 static inline void __kunmap_atomic(void *addr)
76 {
77         pagefault_enable();
78         preempt_enable_nort();
79 }
80
81 #define kmap_atomic_pfn(pfn)    kmap_atomic(pfn_to_page(pfn))
82
83 #define kmap_flush_unused()     do {} while(0)
84 #endif
85
86 #endif /* CONFIG_HIGHMEM */
87
88 #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
89
90 #ifndef CONFIG_PREEMPT_RT_FULL
91 DECLARE_PER_CPU(int, __kmap_atomic_idx);
92 #endif
93
94 static inline int kmap_atomic_idx_push(void)
95 {
96 #ifndef CONFIG_PREEMPT_RT_FULL
97         int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
98
99 # ifdef CONFIG_DEBUG_HIGHMEM
100         WARN_ON_ONCE(in_irq() && !irqs_disabled());
101         BUG_ON(idx >= KM_TYPE_NR);
102 # endif
103         return idx;
104 #else
105         current->kmap_idx++;
106         BUG_ON(current->kmap_idx > KM_TYPE_NR);
107         return current->kmap_idx - 1;
108 #endif
109 }
110
111 static inline int kmap_atomic_idx(void)
112 {
113 #ifndef CONFIG_PREEMPT_RT_FULL
114         return __this_cpu_read(__kmap_atomic_idx) - 1;
115 #else
116         return current->kmap_idx - 1;
117 #endif
118 }
119
120 static inline void kmap_atomic_idx_pop(void)
121 {
122 #ifndef CONFIG_PREEMPT_RT_FULL
123 # ifdef CONFIG_DEBUG_HIGHMEM
124         int idx = __this_cpu_dec_return(__kmap_atomic_idx);
125
126         BUG_ON(idx < 0);
127 # else
128         __this_cpu_dec(__kmap_atomic_idx);
129 # endif
130 #else
131         current->kmap_idx--;
132 # ifdef CONFIG_DEBUG_HIGHMEM
133         BUG_ON(current->kmap_idx < 0);
134 # endif
135 #endif
136 }
137
138 #endif
139
140 /*
141  * Prevent people trying to call kunmap_atomic() as if it were kunmap()
142  * kunmap_atomic() should get the return value of kmap_atomic, not the page.
143  */
144 #define kunmap_atomic(addr)                                     \
145 do {                                                            \
146         BUILD_BUG_ON(__same_type((addr), struct page *));       \
147         __kunmap_atomic(addr);                                  \
148 } while (0)
149
150
151 /* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */
152 #ifndef clear_user_highpage
153 static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
154 {
155         void *addr = kmap_atomic(page);
156         clear_user_page(addr, vaddr, page);
157         kunmap_atomic(addr);
158 }
159 #endif
160
161 #ifndef __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE
162 /**
163  * __alloc_zeroed_user_highpage - Allocate a zeroed HIGHMEM page for a VMA with caller-specified movable GFP flags
164  * @movableflags: The GFP flags related to the pages future ability to move like __GFP_MOVABLE
165  * @vma: The VMA the page is to be allocated for
166  * @vaddr: The virtual address the page will be inserted into
167  *
168  * This function will allocate a page for a VMA but the caller is expected
169  * to specify via movableflags whether the page will be movable in the
170  * future or not
171  *
172  * An architecture may override this function by defining
173  * __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE and providing their own
174  * implementation.
175  */
176 static inline struct page *
177 __alloc_zeroed_user_highpage(gfp_t movableflags,
178                         struct vm_area_struct *vma,
179                         unsigned long vaddr)
180 {
181         struct page *page = alloc_page_vma(GFP_HIGHUSER | movableflags,
182                         vma, vaddr);
183
184         if (page)
185                 clear_user_highpage(page, vaddr);
186
187         return page;
188 }
189 #endif
190
191 /**
192  * alloc_zeroed_user_highpage_movable - Allocate a zeroed HIGHMEM page for a VMA that the caller knows can move
193  * @vma: The VMA the page is to be allocated for
194  * @vaddr: The virtual address the page will be inserted into
195  *
196  * This function will allocate a page for a VMA that the caller knows will
197  * be able to migrate in the future using move_pages() or reclaimed
198  */
199 static inline struct page *
200 alloc_zeroed_user_highpage_movable(struct vm_area_struct *vma,
201                                         unsigned long vaddr)
202 {
203         return __alloc_zeroed_user_highpage(__GFP_MOVABLE, vma, vaddr);
204 }
205
206 static inline void clear_highpage(struct page *page)
207 {
208         void *kaddr = kmap_atomic(page);
209         clear_page(kaddr);
210         kunmap_atomic(kaddr);
211 }
212
213 static inline void zero_user_segments(struct page *page,
214         unsigned start1, unsigned end1,
215         unsigned start2, unsigned end2)
216 {
217         void *kaddr = kmap_atomic(page);
218
219         BUG_ON(end1 > PAGE_SIZE || end2 > PAGE_SIZE);
220
221         if (end1 > start1)
222                 memset(kaddr + start1, 0, end1 - start1);
223
224         if (end2 > start2)
225                 memset(kaddr + start2, 0, end2 - start2);
226
227         kunmap_atomic(kaddr);
228         flush_dcache_page(page);
229 }
230
231 static inline void zero_user_segment(struct page *page,
232         unsigned start, unsigned end)
233 {
234         zero_user_segments(page, start, end, 0, 0);
235 }
236
237 static inline void zero_user(struct page *page,
238         unsigned start, unsigned size)
239 {
240         zero_user_segments(page, start, start + size, 0, 0);
241 }
242
243 #ifndef __HAVE_ARCH_COPY_USER_HIGHPAGE
244
245 static inline void copy_user_highpage(struct page *to, struct page *from,
246         unsigned long vaddr, struct vm_area_struct *vma)
247 {
248         char *vfrom, *vto;
249
250         vfrom = kmap_atomic(from);
251         vto = kmap_atomic(to);
252         copy_user_page(vto, vfrom, vaddr, to);
253         kunmap_atomic(vto);
254         kunmap_atomic(vfrom);
255 }
256
257 #endif
258
259 static inline void copy_highpage(struct page *to, struct page *from)
260 {
261         char *vfrom, *vto;
262
263         vfrom = kmap_atomic(from);
264         vto = kmap_atomic(to);
265         copy_page(vto, vfrom);
266         kunmap_atomic(vto);
267         kunmap_atomic(vfrom);
268 }
269
270 #endif /* _LINUX_HIGHMEM_H */