xref: /freebsd/sys/compat/linuxkpi/common/include/linux/mm.h (revision 797020a76406a8e7a4afbd3706884b58a6e4b4ec)
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 
268 static inline void
set_page_dirty(struct page * page)269 set_page_dirty(struct page *page)
270 {
271 	vm_page_dirty(page);
272 }
273 
274 static inline void
mark_page_accessed(struct page * page)275 mark_page_accessed(struct page *page)
276 {
277 	vm_page_reference(page);
278 }
279 
280 static inline void
get_page(struct page * page)281 get_page(struct page *page)
282 {
283 	vm_page_wire(page);
284 }
285 
286 static inline void
put_page(struct page * page)287 put_page(struct page *page)
288 {
289 	/* `__free_page()` takes care of the refcounting (unwire). */
290 	__free_page(page);
291 }
292 
293 static inline void
folio_get(struct folio * folio)294 folio_get(struct folio *folio)
295 {
296 	get_page(&folio->page);
297 }
298 
299 static inline void
folio_put(struct folio * folio)300 folio_put(struct folio *folio)
301 {
302 	put_page(&folio->page);
303 }
304 
305 /*
306  * Linux uses the following "transparent" union so that `release_pages()`
307  * accepts both a list of `struct page` or a list of `struct folio`. This
308  * relies on the fact that a `struct folio` can be cast to a `struct page`.
309  */
310 typedef union {
311 	struct page **pages;
312 	struct folio **folios;
313 } release_pages_arg __attribute__ ((__transparent_union__));
314 
315 void linux_release_pages(release_pages_arg arg, int nr);
316 #define	release_pages(arg, nr) linux_release_pages((arg), (nr))
317 
318 extern long
319 lkpi_get_user_pages(unsigned long start, unsigned long nr_pages,
320     unsigned int gup_flags, struct page **);
321 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 60500
322 #define	get_user_pages(start, nr_pages, gup_flags, pages)	\
323 	lkpi_get_user_pages(start, nr_pages, gup_flags, pages)
324 #else
325 #define	get_user_pages(start, nr_pages, gup_flags, pages, vmas)	\
326 	lkpi_get_user_pages(start, nr_pages, gup_flags, pages)
327 #endif
328 
329 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 60500
330 static inline long
pin_user_pages(unsigned long start,unsigned long nr_pages,unsigned int gup_flags,struct page ** pages)331 pin_user_pages(unsigned long start, unsigned long nr_pages,
332     unsigned int gup_flags, struct page **pages)
333 {
334 	return (get_user_pages(start, nr_pages, gup_flags, pages));
335 }
336 #else
337 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)338 pin_user_pages(unsigned long start, unsigned long nr_pages,
339     unsigned int gup_flags, struct page **pages,
340     struct vm_area_struct **vmas)
341 {
342 	return (get_user_pages(start, nr_pages, gup_flags, pages, vmas));
343 }
344 #endif
345 
346 extern int
347 __get_user_pages_fast(unsigned long start, int nr_pages, int write,
348     struct page **);
349 
350 static inline int
pin_user_pages_fast(unsigned long start,int nr_pages,unsigned int gup_flags,struct page ** pages)351 pin_user_pages_fast(unsigned long start, int nr_pages,
352     unsigned int gup_flags, struct page **pages)
353 {
354 	return __get_user_pages_fast(
355 	    start, nr_pages, !!(gup_flags & FOLL_WRITE), pages);
356 }
357 
358 extern long
359 get_user_pages_remote(struct task_struct *, struct mm_struct *,
360     unsigned long start, unsigned long nr_pages,
361     unsigned int gup_flags, struct page **,
362     struct vm_area_struct **);
363 
364 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)365 pin_user_pages_remote(struct task_struct *task, struct mm_struct *mm,
366     unsigned long start, unsigned long nr_pages,
367     unsigned int gup_flags, struct page **pages,
368     struct vm_area_struct **vmas)
369 {
370 	return get_user_pages_remote(
371 	    task, mm, start, nr_pages, gup_flags, pages, vmas);
372 }
373 
374 #define	unpin_user_page(page) put_page(page)
375 #define	unpin_user_pages(pages, npages) release_pages(pages, npages)
376 
377 #define	copy_highpage(to, from) pmap_copy_page(from, to)
378 
379 static inline pgprot_t
vm_get_page_prot(unsigned long vm_flags)380 vm_get_page_prot(unsigned long vm_flags)
381 {
382 	return (vm_flags & VM_PROT_ALL);
383 }
384 
385 static inline void
vm_flags_set(struct vm_area_struct * vma,unsigned long flags)386 vm_flags_set(struct vm_area_struct *vma, unsigned long flags)
387 {
388 	vma->vm_flags |= flags;
389 }
390 
391 static inline void
vm_flags_clear(struct vm_area_struct * vma,unsigned long flags)392 vm_flags_clear(struct vm_area_struct *vma, unsigned long flags)
393 {
394 	vma->vm_flags &= ~flags;
395 }
396 
397 static inline struct page *
vmalloc_to_page(const void * addr)398 vmalloc_to_page(const void *addr)
399 {
400 	vm_paddr_t paddr;
401 
402 	paddr = pmap_kextract((vm_offset_t)addr);
403 	return (PHYS_TO_VM_PAGE(paddr));
404 }
405 
406 static inline int
trylock_page(struct page * page)407 trylock_page(struct page *page)
408 {
409 	return (vm_page_tryxbusy(page));
410 }
411 
412 static inline void
unlock_page(struct page * page)413 unlock_page(struct page *page)
414 {
415 
416 	vm_page_xunbusy(page);
417 }
418 
419 extern int is_vmalloc_addr(const void *addr);
420 void si_meminfo(struct sysinfo *si);
421 
422 static inline unsigned long
totalram_pages(void)423 totalram_pages(void)
424 {
425 	return ((unsigned long)physmem);
426 }
427 
428 #define	unmap_mapping_range(...)	lkpi_unmap_mapping_range(__VA_ARGS__)
429 void lkpi_unmap_mapping_range(void *obj, loff_t const holebegin __unused,
430     loff_t const holelen, int even_cows __unused);
431 
432 #define PAGE_ALIGNED(p)	__is_aligned(p, PAGE_SIZE)
433 
434 void vma_set_file(struct vm_area_struct *vma, struct linux_file *file);
435 
436 static inline void
might_alloc(gfp_t gfp_mask __unused)437 might_alloc(gfp_t gfp_mask __unused)
438 {
439 }
440 
441 #define	is_cow_mapping(flags)	(false)
442 
443 static inline bool
want_init_on_free(void)444 want_init_on_free(void)
445 {
446 	return (false);
447 }
448 
449 static inline unsigned long
folio_pfn(struct folio * folio)450 folio_pfn(struct folio *folio)
451 {
452 	return (page_to_pfn(&folio->page));
453 }
454 
455 static inline long
folio_nr_pages(struct folio * folio)456 folio_nr_pages(struct folio *folio)
457 {
458 	return (1);
459 }
460 
461 static inline size_t
folio_size(struct folio * folio)462 folio_size(struct folio *folio)
463 {
464 	return (PAGE_SIZE);
465 }
466 
467 static inline void
folio_mark_dirty(struct folio * folio)468 folio_mark_dirty(struct folio *folio)
469 {
470 	set_page_dirty(&folio->page);
471 }
472 
473 static inline void *
folio_address(const struct folio * folio)474 folio_address(const struct folio *folio)
475 {
476 	return (page_address(&folio->page));
477 }
478 
479 #endif					/* _LINUXKPI_LINUX_MM_H_ */
480