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