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