1 /*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
6 * Copyright (c) 2015 François Tigeot
7 * Copyright (c) 2015 Matthew Dillon <dillon@backplane.com>
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
15 * disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _LINUXKPI_LINUX_MM_H_
32 #define _LINUXKPI_LINUX_MM_H_
33
34 #include <linux/spinlock.h>
35 #include <linux/gfp.h>
36 #include <linux/kernel.h>
37 #include <linux/mm_types.h>
38 #include <linux/mmzone.h>
39 #include <linux/pfn.h>
40 #include <linux/list.h>
41 #include <linux/mmap_lock.h>
42 #include <linux/overflow.h>
43 #include <linux/shrinker.h>
44 #include <linux/page.h>
45
46 #include <asm/pgtable.h>
47
48 #define PAGE_ALIGN(x) ALIGN(x, PAGE_SIZE)
49
50 /*
51 * Make sure our LinuxKPI defined virtual memory flags don't conflict
52 * with the ones defined by FreeBSD:
53 */
54 CTASSERT((VM_PROT_ALL & -(1 << 8)) == 0);
55
56 #define VM_READ VM_PROT_READ
57 #define VM_WRITE VM_PROT_WRITE
58 #define VM_EXEC VM_PROT_EXECUTE
59
60 #define VM_ACCESS_FLAGS (VM_READ | VM_WRITE | VM_EXEC)
61
62 #define VM_PFNINTERNAL (1 << 8) /* FreeBSD private flag to vm_insert_pfn() */
63 #define VM_MIXEDMAP (1 << 9)
64 #define VM_NORESERVE (1 << 10)
65 #define VM_PFNMAP (1 << 11)
66 #define VM_IO (1 << 12)
67 #define VM_MAYWRITE (1 << 13)
68 #define VM_DONTCOPY (1 << 14)
69 #define VM_DONTEXPAND (1 << 15)
70 #define VM_DONTDUMP (1 << 16)
71 #define VM_SHARED (1 << 17)
72
73 #define VMA_MAX_PREFAULT_RECORD 1
74
75 #define FOLL_WRITE (1 << 0)
76 #define FOLL_FORCE (1 << 1)
77
78 #define VM_FAULT_OOM (1 << 0)
79 #define VM_FAULT_SIGBUS (1 << 1)
80 #define VM_FAULT_MAJOR (1 << 2)
81 #define VM_FAULT_WRITE (1 << 3)
82 #define VM_FAULT_HWPOISON (1 << 4)
83 #define VM_FAULT_HWPOISON_LARGE (1 << 5)
84 #define VM_FAULT_SIGSEGV (1 << 6)
85 #define VM_FAULT_NOPAGE (1 << 7)
86 #define VM_FAULT_LOCKED (1 << 8)
87 #define VM_FAULT_RETRY (1 << 9)
88 #define VM_FAULT_FALLBACK (1 << 10)
89
90 #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV | \
91 VM_FAULT_HWPOISON |VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK)
92
93 #define FAULT_FLAG_WRITE (1 << 0)
94 #define FAULT_FLAG_MKWRITE (1 << 1)
95 #define FAULT_FLAG_ALLOW_RETRY (1 << 2)
96 #define FAULT_FLAG_RETRY_NOWAIT (1 << 3)
97 #define FAULT_FLAG_KILLABLE (1 << 4)
98 #define FAULT_FLAG_TRIED (1 << 5)
99 #define FAULT_FLAG_USER (1 << 6)
100 #define FAULT_FLAG_REMOTE (1 << 7)
101 #define FAULT_FLAG_INSTRUCTION (1 << 8)
102
103 #define fault_flag_allow_retry_first(flags) \
104 (((flags) & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_TRIED)) == FAULT_FLAG_ALLOW_RETRY)
105
106 typedef int (*pte_fn_t)(linux_pte_t *, unsigned long addr, void *data);
107
108 struct vm_area_struct {
109 vm_offset_t vm_start;
110 vm_offset_t vm_end;
111 vm_offset_t vm_pgoff;
112 pgprot_t vm_page_prot;
113 unsigned long vm_flags;
114 struct mm_struct *vm_mm;
115 void *vm_private_data;
116 const struct vm_operations_struct *vm_ops;
117 struct linux_file *vm_file;
118
119 /* internal operation */
120 vm_paddr_t vm_pfn; /* PFN for memory map */
121 vm_size_t vm_len; /* length for memory map */
122 vm_pindex_t vm_pfn_first;
123 int vm_pfn_count;
124 int *vm_pfn_pcount;
125 vm_object_t vm_obj;
126 vm_map_t vm_cached_map;
127 TAILQ_ENTRY(vm_area_struct) vm_entry;
128 };
129
130 struct vm_fault {
131 unsigned int flags;
132 pgoff_t pgoff;
133 union {
134 /* user-space address */
135 void *virtual_address; /* < 4.11 */
136 unsigned long address; /* >= 4.11 */
137 };
138 struct page *page;
139 struct vm_area_struct *vma;
140 };
141
142 struct vm_operations_struct {
143 void (*open) (struct vm_area_struct *);
144 void (*close) (struct vm_area_struct *);
145 int (*fault) (struct vm_fault *);
146 int (*access) (struct vm_area_struct *, unsigned long, void *, int, int);
147 };
148
149 struct sysinfo {
150 uint64_t totalram; /* Total usable main memory size */
151 uint64_t freeram; /* Available memory size */
152 uint64_t totalhigh; /* Total high memory size */
153 uint64_t freehigh; /* Available high memory size */
154 uint32_t mem_unit; /* Memory unit size in bytes */
155 };
156
157 static inline struct page *
virt_to_head_page(const void * p)158 virt_to_head_page(const void *p)
159 {
160
161 return (virt_to_page(p));
162 }
163
164 static inline struct folio *
virt_to_folio(const void * p)165 virt_to_folio(const void *p)
166 {
167 struct page *page = virt_to_page(p);
168
169 return (page_folio(page));
170 }
171
172 /*
173 * Compute log2 of the power of two rounded up count of pages
174 * needed for size bytes.
175 */
176 static inline int
get_order(unsigned long size)177 get_order(unsigned long size)
178 {
179 int order;
180
181 size = (size - 1) >> PAGE_SHIFT;
182 order = 0;
183 while (size) {
184 order++;
185 size >>= 1;
186 }
187 return (order);
188 }
189
190 /*
191 * Resolve a page into a virtual address:
192 *
193 * NOTE: This function only works for pages allocated by the kernel.
194 */
195 void *linux_page_address(const struct page *);
196 #define page_address(page) linux_page_address(page)
197
198 static inline void *
lowmem_page_address(struct page * page)199 lowmem_page_address(struct page *page)
200 {
201 return (page_address(page));
202 }
203
204 /*
205 * This only works via memory map operations.
206 */
207 static inline int
io_remap_pfn_range(struct vm_area_struct * vma,unsigned long addr,unsigned long pfn,unsigned long size,vm_memattr_t prot)208 io_remap_pfn_range(struct vm_area_struct *vma,
209 unsigned long addr, unsigned long pfn, unsigned long size,
210 vm_memattr_t prot)
211 {
212 vma->vm_page_prot = prot;
213 vma->vm_pfn = pfn;
214 vma->vm_len = size;
215
216 return (0);
217 }
218
219 vm_fault_t
220 lkpi_vmf_insert_pfn_prot_locked(struct vm_area_struct *vma, unsigned long addr,
221 unsigned long pfn, pgprot_t prot);
222
223 static inline vm_fault_t
vmf_insert_pfn_prot(struct vm_area_struct * vma,unsigned long addr,unsigned long pfn,pgprot_t prot)224 vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
225 unsigned long pfn, pgprot_t prot)
226 {
227 vm_fault_t ret;
228
229 VM_OBJECT_WLOCK(vma->vm_obj);
230 ret = lkpi_vmf_insert_pfn_prot_locked(vma, addr, pfn, prot);
231 VM_OBJECT_WUNLOCK(vma->vm_obj);
232
233 return (ret);
234 }
235 #define vmf_insert_pfn_prot(...) \
236 _Static_assert(false, \
237 "This function is always called in a loop. Consider using the locked version")
238
239 static inline int
apply_to_page_range(struct mm_struct * mm,unsigned long address,unsigned long size,pte_fn_t fn,void * data)240 apply_to_page_range(struct mm_struct *mm, unsigned long address,
241 unsigned long size, pte_fn_t fn, void *data)
242 {
243 return (-ENOTSUP);
244 }
245
246 int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
247 unsigned long size);
248
249 int lkpi_remap_pfn_range(struct vm_area_struct *vma,
250 unsigned long start_addr, unsigned long start_pfn, unsigned long size,
251 pgprot_t prot);
252
253 static inline int
remap_pfn_range(struct vm_area_struct * vma,unsigned long addr,unsigned long pfn,unsigned long size,pgprot_t prot)254 remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
255 unsigned long pfn, unsigned long size, pgprot_t prot)
256 {
257 return (lkpi_remap_pfn_range(vma, addr, pfn, size, prot));
258 }
259
260 static inline unsigned long
vma_pages(struct vm_area_struct * vma)261 vma_pages(struct vm_area_struct *vma)
262 {
263 return ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
264 }
265
266 #define offset_in_page(off) ((unsigned long)(off) & (PAGE_SIZE - 1))
267 #define offset_in_folio(folio, p) ((unsigned long)(p) & (folio_size(folio) - 1))
268
269 static inline void
set_page_dirty(struct page * page)270 set_page_dirty(struct page *page)
271 {
272 vm_page_dirty(page);
273 }
274
275 static inline void
mark_page_accessed(struct page * page)276 mark_page_accessed(struct page *page)
277 {
278 vm_page_reference(page);
279 }
280
281 static inline void
get_page(struct page * page)282 get_page(struct page *page)
283 {
284 vm_page_wire(page);
285 }
286
287 static inline void
put_page(struct page * page)288 put_page(struct page *page)
289 {
290 /* `__free_page()` takes care of the refcounting (unwire). */
291 __free_page(page);
292 }
293
294 static inline void
folio_get(struct folio * folio)295 folio_get(struct folio *folio)
296 {
297 get_page(&folio->page);
298 }
299
300 static inline void
folio_put(struct folio * folio)301 folio_put(struct folio *folio)
302 {
303 put_page(&folio->page);
304 }
305
306 /*
307 * Linux uses the following "transparent" union so that `release_pages()`
308 * accepts both a list of `struct page` or a list of `struct folio`. This
309 * relies on the fact that a `struct folio` can be cast to a `struct page`.
310 */
311 typedef union {
312 struct page **pages;
313 struct folio **folios;
314 } release_pages_arg __attribute__ ((__transparent_union__));
315
316 void linux_release_pages(release_pages_arg arg, int nr);
317 #define release_pages(arg, nr) linux_release_pages((arg), (nr))
318
319 extern long
320 lkpi_get_user_pages(unsigned long start, unsigned long nr_pages,
321 unsigned int gup_flags, struct page **);
322 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 60500
323 #define get_user_pages(start, nr_pages, gup_flags, pages) \
324 lkpi_get_user_pages(start, nr_pages, gup_flags, pages)
325 #else
326 #define get_user_pages(start, nr_pages, gup_flags, pages, vmas) \
327 lkpi_get_user_pages(start, nr_pages, gup_flags, pages)
328 #endif
329
330 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 60500
331 static inline long
pin_user_pages(unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages)332 pin_user_pages(unsigned long start, unsigned long nr_pages,
333 unsigned int gup_flags, struct page **pages)
334 {
335 return (get_user_pages(start, nr_pages, gup_flags, pages));
336 }
337 #else
338 static inline long
pin_user_pages(unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages,struct vm_area_struct ** vmas)339 pin_user_pages(unsigned long start, unsigned long nr_pages,
340 unsigned int gup_flags, struct page **pages,
341 struct vm_area_struct **vmas)
342 {
343 return (get_user_pages(start, nr_pages, gup_flags, pages, vmas));
344 }
345 #endif
346
347 extern int
348 __get_user_pages_fast(unsigned long start, int nr_pages, int write,
349 struct page **);
350
351 static inline int
pin_user_pages_fast(unsigned long start,int nr_pages,unsigned int gup_flags,struct page ** pages)352 pin_user_pages_fast(unsigned long start, int nr_pages,
353 unsigned int gup_flags, struct page **pages)
354 {
355 return __get_user_pages_fast(
356 start, nr_pages, !!(gup_flags & FOLL_WRITE), pages);
357 }
358
359 extern long
360 get_user_pages_remote(struct task_struct *, struct mm_struct *,
361 unsigned long start, unsigned long nr_pages,
362 unsigned int gup_flags, struct page **,
363 struct vm_area_struct **);
364
365 static inline long
pin_user_pages_remote(struct task_struct * task,struct mm_struct * mm,unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages,struct vm_area_struct ** vmas)366 pin_user_pages_remote(struct task_struct *task, struct mm_struct *mm,
367 unsigned long start, unsigned long nr_pages,
368 unsigned int gup_flags, struct page **pages,
369 struct vm_area_struct **vmas)
370 {
371 return get_user_pages_remote(
372 task, mm, start, nr_pages, gup_flags, pages, vmas);
373 }
374
375 #define unpin_user_page(page) put_page(page)
376 #define unpin_user_pages(pages, npages) release_pages(pages, npages)
377
378 #define copy_highpage(to, from) pmap_copy_page(from, to)
379
380 static inline pgprot_t
vm_get_page_prot(unsigned long vm_flags)381 vm_get_page_prot(unsigned long vm_flags)
382 {
383 return (vm_flags & VM_PROT_ALL);
384 }
385
386 static inline void
vm_flags_set(struct vm_area_struct * vma,unsigned long flags)387 vm_flags_set(struct vm_area_struct *vma, unsigned long flags)
388 {
389 vma->vm_flags |= flags;
390 }
391
392 static inline void
vm_flags_clear(struct vm_area_struct * vma,unsigned long flags)393 vm_flags_clear(struct vm_area_struct *vma, unsigned long flags)
394 {
395 vma->vm_flags &= ~flags;
396 }
397
398 static inline struct page *
vmalloc_to_page(const void * addr)399 vmalloc_to_page(const void *addr)
400 {
401 vm_paddr_t paddr;
402
403 paddr = pmap_kextract((vm_offset_t)addr);
404 return (PHYS_TO_VM_PAGE(paddr));
405 }
406
407 static inline int
trylock_page(struct page * page)408 trylock_page(struct page *page)
409 {
410 return (vm_page_tryxbusy(page));
411 }
412
413 static inline void
unlock_page(struct page * page)414 unlock_page(struct page *page)
415 {
416
417 vm_page_xunbusy(page);
418 }
419
420 extern int is_vmalloc_addr(const void *addr);
421 void si_meminfo(struct sysinfo *si);
422
423 static inline unsigned long
totalram_pages(void)424 totalram_pages(void)
425 {
426 return ((unsigned long)physmem);
427 }
428
429 #define unmap_mapping_range(...) lkpi_unmap_mapping_range(__VA_ARGS__)
430 void lkpi_unmap_mapping_range(void *obj, loff_t const holebegin __unused,
431 loff_t const holelen, int even_cows __unused);
432
433 #define PAGE_ALIGNED(p) __is_aligned(p, PAGE_SIZE)
434
435 void vma_set_file(struct vm_area_struct *vma, struct linux_file *file);
436
437 static inline void
might_alloc(gfp_t gfp_mask __unused)438 might_alloc(gfp_t gfp_mask __unused)
439 {
440 }
441
442 #define is_cow_mapping(flags) (false)
443
444 static inline bool
want_init_on_free(void)445 want_init_on_free(void)
446 {
447 return (false);
448 }
449
450 static inline unsigned long
folio_pfn(struct folio * folio)451 folio_pfn(struct folio *folio)
452 {
453 return (page_to_pfn(&folio->page));
454 }
455
456 static inline long
folio_nr_pages(struct folio * folio)457 folio_nr_pages(struct folio *folio)
458 {
459 return (1);
460 }
461
462 static inline size_t
folio_size(struct folio * folio)463 folio_size(struct folio *folio)
464 {
465 return (PAGE_SIZE);
466 }
467
468 static inline void
folio_mark_dirty(struct folio * folio)469 folio_mark_dirty(struct folio *folio)
470 {
471 set_page_dirty(&folio->page);
472 }
473
474 static inline void *
folio_address(const struct folio * folio)475 folio_address(const struct folio *folio)
476 {
477 return (page_address(&folio->page));
478 }
479
480 #endif /* _LINUXKPI_LINUX_MM_H_ */
481