1 #ifndef _LINUX_HIGHMEM_H
2 #define _LINUX_HIGHMEM_H
5 #include <linux/kernel.h>
8 #include <linux/uaccess.h>
9 #include <linux/hardirq.h>
10 #include <linux/sched.h>
12 #include <asm/cacheflush.h>
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)
20 #ifndef ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE
21 static inline void flush_kernel_dcache_page(struct page *page)
24 static inline void flush_kernel_vmap_range(void *vaddr, int size)
27 static inline void invalidate_kernel_vmap_range(void *vaddr, int size)
32 #include <asm/kmap_types.h>
35 #include <asm/highmem.h>
37 /* declarations for linux/mm/highmem.c */
38 unsigned int nr_free_highpages(void);
39 extern unsigned long totalhigh_pages;
41 void kmap_flush_unused(void);
43 struct page *kmap_to_page(void *addr);
45 #else /* CONFIG_HIGHMEM */
47 static inline unsigned int nr_free_highpages(void) { return 0; }
49 static inline struct page *kmap_to_page(void *addr)
51 return virt_to_page(addr);
54 #define totalhigh_pages 0UL
57 static inline void *kmap(struct page *page)
60 return page_address(page);
63 static inline void kunmap(struct page *page)
67 static inline void *kmap_atomic(struct page *page)
69 preempt_disable_nort();
71 return page_address(page);
73 #define kmap_atomic_prot(page, prot) kmap_atomic(page)
75 static inline void __kunmap_atomic(void *addr)
78 preempt_enable_nort();
81 #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))
83 #define kmap_flush_unused() do {} while(0)
86 #endif /* CONFIG_HIGHMEM */
88 #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
90 #ifndef CONFIG_PREEMPT_RT_FULL
91 DECLARE_PER_CPU(int, __kmap_atomic_idx);
94 static inline int kmap_atomic_idx_push(void)
96 #ifndef CONFIG_PREEMPT_RT_FULL
97 int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
99 # ifdef CONFIG_DEBUG_HIGHMEM
100 WARN_ON_ONCE(in_irq() && !irqs_disabled());
101 BUG_ON(idx >= KM_TYPE_NR);
106 BUG_ON(current->kmap_idx > KM_TYPE_NR);
107 return current->kmap_idx - 1;
111 static inline int kmap_atomic_idx(void)
113 #ifndef CONFIG_PREEMPT_RT_FULL
114 return __this_cpu_read(__kmap_atomic_idx) - 1;
116 return current->kmap_idx - 1;
120 static inline void kmap_atomic_idx_pop(void)
122 #ifndef CONFIG_PREEMPT_RT_FULL
123 # ifdef CONFIG_DEBUG_HIGHMEM
124 int idx = __this_cpu_dec_return(__kmap_atomic_idx);
128 __this_cpu_dec(__kmap_atomic_idx);
132 # ifdef CONFIG_DEBUG_HIGHMEM
133 BUG_ON(current->kmap_idx < 0);
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.
144 #define kunmap_atomic(addr) \
146 BUILD_BUG_ON(__same_type((addr), struct page *)); \
147 __kunmap_atomic(addr); \
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)
155 void *addr = kmap_atomic(page);
156 clear_user_page(addr, vaddr, page);
161 #ifndef __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE
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
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
172 * An architecture may override this function by defining
173 * __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE and providing their own
176 static inline struct page *
177 __alloc_zeroed_user_highpage(gfp_t movableflags,
178 struct vm_area_struct *vma,
181 struct page *page = alloc_page_vma(GFP_HIGHUSER | movableflags,
185 clear_user_highpage(page, vaddr);
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
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
199 static inline struct page *
200 alloc_zeroed_user_highpage_movable(struct vm_area_struct *vma,
203 return __alloc_zeroed_user_highpage(__GFP_MOVABLE, vma, vaddr);
206 static inline void clear_highpage(struct page *page)
208 void *kaddr = kmap_atomic(page);
210 kunmap_atomic(kaddr);
213 static inline void zero_user_segments(struct page *page,
214 unsigned start1, unsigned end1,
215 unsigned start2, unsigned end2)
217 void *kaddr = kmap_atomic(page);
219 BUG_ON(end1 > PAGE_SIZE || end2 > PAGE_SIZE);
222 memset(kaddr + start1, 0, end1 - start1);
225 memset(kaddr + start2, 0, end2 - start2);
227 kunmap_atomic(kaddr);
228 flush_dcache_page(page);
231 static inline void zero_user_segment(struct page *page,
232 unsigned start, unsigned end)
234 zero_user_segments(page, start, end, 0, 0);
237 static inline void zero_user(struct page *page,
238 unsigned start, unsigned size)
240 zero_user_segments(page, start, start + size, 0, 0);
243 #ifndef __HAVE_ARCH_COPY_USER_HIGHPAGE
245 static inline void copy_user_highpage(struct page *to, struct page *from,
246 unsigned long vaddr, struct vm_area_struct *vma)
250 vfrom = kmap_atomic(from);
251 vto = kmap_atomic(to);
252 copy_user_page(vto, vfrom, vaddr, to);
254 kunmap_atomic(vfrom);
259 static inline void copy_highpage(struct page *to, struct page *from)
263 vfrom = kmap_atomic(from);
264 vto = kmap_atomic(to);
265 copy_page(vto, vfrom);
267 kunmap_atomic(vfrom);
270 #endif /* _LINUX_HIGHMEM_H */