18d59ecb2SHans Petter Selasky /*- 28d59ecb2SHans Petter Selasky * Copyright (c) 2010 Isilon Systems, Inc. 38d59ecb2SHans Petter Selasky * Copyright (c) 2010 iX Systems, Inc. 48d59ecb2SHans Petter Selasky * Copyright (c) 2010 Panasas, Inc. 58186b527SHans Petter Selasky * Copyright (c) 2013-2017 Mellanox Technologies, Ltd. 68d59ecb2SHans Petter Selasky * Copyright (c) 2015 François Tigeot 78d59ecb2SHans Petter Selasky * Copyright (c) 2015 Matthew Dillon <dillon@backplane.com> 88d59ecb2SHans Petter Selasky * All rights reserved. 98d59ecb2SHans Petter Selasky * 108d59ecb2SHans Petter Selasky * Redistribution and use in source and binary forms, with or without 118d59ecb2SHans Petter Selasky * modification, are permitted provided that the following conditions 128d59ecb2SHans Petter Selasky * are met: 138d59ecb2SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright 148d59ecb2SHans Petter Selasky * notice unmodified, this list of conditions, and the following 158d59ecb2SHans Petter Selasky * disclaimer. 168d59ecb2SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright 178d59ecb2SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the 188d59ecb2SHans Petter Selasky * documentation and/or other materials provided with the distribution. 198d59ecb2SHans Petter Selasky * 208d59ecb2SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 218d59ecb2SHans Petter Selasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 228d59ecb2SHans Petter Selasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 238d59ecb2SHans Petter Selasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 248d59ecb2SHans Petter Selasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 258d59ecb2SHans Petter Selasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 268d59ecb2SHans Petter Selasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 278d59ecb2SHans Petter Selasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 288d59ecb2SHans Petter Selasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 298d59ecb2SHans Petter Selasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 308d59ecb2SHans Petter Selasky * 318d59ecb2SHans Petter Selasky * $FreeBSD$ 328d59ecb2SHans Petter Selasky */ 33307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_MM_H_ 34307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_MM_H_ 358d59ecb2SHans Petter Selasky 368d59ecb2SHans Petter Selasky #include <linux/spinlock.h> 378d59ecb2SHans Petter Selasky #include <linux/gfp.h> 388d59ecb2SHans Petter Selasky #include <linux/kernel.h> 3905d4f501SHans Petter Selasky #include <linux/mm_types.h> 408186b527SHans Petter Selasky #include <linux/pfn.h> 411ea4c857SHans Petter Selasky #include <linux/list.h> 42995c3b88SEmmanuel Vadot #include <linux/mmap_lock.h> 43*415ca4d2SJean-Sébastien Pédron #include <linux/shrinker.h> 448186b527SHans Petter Selasky 458186b527SHans Petter Selasky #include <asm/pgtable.h> 468d59ecb2SHans Petter Selasky 478d59ecb2SHans Petter Selasky #define PAGE_ALIGN(x) ALIGN(x, PAGE_SIZE) 488d59ecb2SHans Petter Selasky 498186b527SHans Petter Selasky /* 508186b527SHans Petter Selasky * Make sure our LinuxKPI defined virtual memory flags don't conflict 518186b527SHans Petter Selasky * with the ones defined by FreeBSD: 528186b527SHans Petter Selasky */ 538186b527SHans Petter Selasky CTASSERT((VM_PROT_ALL & -(1 << 8)) == 0); 548186b527SHans Petter Selasky 55480e2fd3SHans Petter Selasky #define VM_READ VM_PROT_READ 56480e2fd3SHans Petter Selasky #define VM_WRITE VM_PROT_WRITE 57480e2fd3SHans Petter Selasky #define VM_EXEC VM_PROT_EXECUTE 58480e2fd3SHans Petter Selasky 598186b527SHans Petter Selasky #define VM_PFNINTERNAL (1 << 8) /* FreeBSD private flag to vm_insert_pfn() */ 608186b527SHans Petter Selasky #define VM_MIXEDMAP (1 << 9) 618186b527SHans Petter Selasky #define VM_NORESERVE (1 << 10) 628186b527SHans Petter Selasky #define VM_PFNMAP (1 << 11) 638186b527SHans Petter Selasky #define VM_IO (1 << 12) 648186b527SHans Petter Selasky #define VM_MAYWRITE (1 << 13) 658186b527SHans Petter Selasky #define VM_DONTCOPY (1 << 14) 668186b527SHans Petter Selasky #define VM_DONTEXPAND (1 << 15) 678186b527SHans Petter Selasky #define VM_DONTDUMP (1 << 16) 68904390b4SHans Petter Selasky #define VM_SHARED (1 << 17) 698186b527SHans Petter Selasky 708186b527SHans Petter Selasky #define VMA_MAX_PREFAULT_RECORD 1 718186b527SHans Petter Selasky 728186b527SHans Petter Selasky #define FOLL_WRITE (1 << 0) 738186b527SHans Petter Selasky #define FOLL_FORCE (1 << 1) 748186b527SHans Petter Selasky 758186b527SHans Petter Selasky #define VM_FAULT_OOM (1 << 0) 768186b527SHans Petter Selasky #define VM_FAULT_SIGBUS (1 << 1) 778186b527SHans Petter Selasky #define VM_FAULT_MAJOR (1 << 2) 788186b527SHans Petter Selasky #define VM_FAULT_WRITE (1 << 3) 798186b527SHans Petter Selasky #define VM_FAULT_HWPOISON (1 << 4) 808186b527SHans Petter Selasky #define VM_FAULT_HWPOISON_LARGE (1 << 5) 818186b527SHans Petter Selasky #define VM_FAULT_SIGSEGV (1 << 6) 828186b527SHans Petter Selasky #define VM_FAULT_NOPAGE (1 << 7) 838186b527SHans Petter Selasky #define VM_FAULT_LOCKED (1 << 8) 848186b527SHans Petter Selasky #define VM_FAULT_RETRY (1 << 9) 858186b527SHans Petter Selasky #define VM_FAULT_FALLBACK (1 << 10) 868186b527SHans Petter Selasky 87f6823dacSVladimir Kondratyev #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV | \ 88f6823dacSVladimir Kondratyev VM_FAULT_HWPOISON |VM_FAULT_HWPOISON_LARGE | VM_FAULT_FALLBACK) 89f6823dacSVladimir Kondratyev 908186b527SHans Petter Selasky #define FAULT_FLAG_WRITE (1 << 0) 918186b527SHans Petter Selasky #define FAULT_FLAG_MKWRITE (1 << 1) 928186b527SHans Petter Selasky #define FAULT_FLAG_ALLOW_RETRY (1 << 2) 938186b527SHans Petter Selasky #define FAULT_FLAG_RETRY_NOWAIT (1 << 3) 948186b527SHans Petter Selasky #define FAULT_FLAG_KILLABLE (1 << 4) 958186b527SHans Petter Selasky #define FAULT_FLAG_TRIED (1 << 5) 968186b527SHans Petter Selasky #define FAULT_FLAG_USER (1 << 6) 978186b527SHans Petter Selasky #define FAULT_FLAG_REMOTE (1 << 7) 988186b527SHans Petter Selasky #define FAULT_FLAG_INSTRUCTION (1 << 8) 998186b527SHans Petter Selasky 100597cc550SGreg V #define fault_flag_allow_retry_first(flags) \ 101597cc550SGreg V (((flags) & (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_TRIED)) == FAULT_FLAG_ALLOW_RETRY) 102597cc550SGreg V 10388531adbSVladimir Kondratyev typedef int (*pte_fn_t)(linux_pte_t *, unsigned long addr, void *data); 1048186b527SHans Petter Selasky 1058d59ecb2SHans Petter Selasky struct vm_area_struct { 1068d59ecb2SHans Petter Selasky vm_offset_t vm_start; 1078d59ecb2SHans Petter Selasky vm_offset_t vm_end; 1088d59ecb2SHans Petter Selasky vm_offset_t vm_pgoff; 1091ea4c857SHans Petter Selasky pgprot_t vm_page_prot; 1101ea4c857SHans Petter Selasky unsigned long vm_flags; 1111ea4c857SHans Petter Selasky struct mm_struct *vm_mm; 1121ea4c857SHans Petter Selasky void *vm_private_data; 1131ea4c857SHans Petter Selasky const struct vm_operations_struct *vm_ops; 1141ea4c857SHans Petter Selasky struct linux_file *vm_file; 1151ea4c857SHans Petter Selasky 1161ea4c857SHans Petter Selasky /* internal operation */ 1171ea4c857SHans Petter Selasky vm_paddr_t vm_pfn; /* PFN for memory map */ 1181ea4c857SHans Petter Selasky vm_size_t vm_len; /* length for memory map */ 1191ea4c857SHans Petter Selasky vm_pindex_t vm_pfn_first; 1201ea4c857SHans Petter Selasky int vm_pfn_count; 1211ea4c857SHans Petter Selasky int *vm_pfn_pcount; 1221ea4c857SHans Petter Selasky vm_object_t vm_obj; 1231ea4c857SHans Petter Selasky vm_map_t vm_cached_map; 1241ea4c857SHans Petter Selasky TAILQ_ENTRY(vm_area_struct) vm_entry; 1258d59ecb2SHans Petter Selasky }; 1268d59ecb2SHans Petter Selasky 1278186b527SHans Petter Selasky struct vm_fault { 1288186b527SHans Petter Selasky unsigned int flags; 1298186b527SHans Petter Selasky pgoff_t pgoff; 1300628fc90SHans Petter Selasky union { 1310628fc90SHans Petter Selasky /* user-space address */ 132747d9a81SHans Petter Selasky void *virtual_address; /* < 4.11 */ 133747d9a81SHans Petter Selasky unsigned long address; /* >= 4.11 */ 1340628fc90SHans Petter Selasky }; 1358186b527SHans Petter Selasky struct page *page; 1360628fc90SHans Petter Selasky struct vm_area_struct *vma; 1378186b527SHans Petter Selasky }; 1388186b527SHans Petter Selasky 1398186b527SHans Petter Selasky struct vm_operations_struct { 1408186b527SHans Petter Selasky void (*open) (struct vm_area_struct *); 1418186b527SHans Petter Selasky void (*close) (struct vm_area_struct *); 142062f1500SVladimir Kondratyev int (*fault) (struct vm_fault *); 143747d9a81SHans Petter Selasky int (*access) (struct vm_area_struct *, unsigned long, void *, int, int); 1448186b527SHans Petter Selasky }; 1458186b527SHans Petter Selasky 1468734a562SHans Petter Selasky struct sysinfo { 1478734a562SHans Petter Selasky uint64_t totalram; 1488734a562SHans Petter Selasky uint64_t totalhigh; 1498734a562SHans Petter Selasky uint32_t mem_unit; 1508734a562SHans Petter Selasky }; 1518734a562SHans Petter Selasky 1528d59ecb2SHans Petter Selasky /* 1538d59ecb2SHans Petter Selasky * Compute log2 of the power of two rounded up count of pages 1548d59ecb2SHans Petter Selasky * needed for size bytes. 1558d59ecb2SHans Petter Selasky */ 1568d59ecb2SHans Petter Selasky static inline int 1578d59ecb2SHans Petter Selasky get_order(unsigned long size) 1588d59ecb2SHans Petter Selasky { 1598d59ecb2SHans Petter Selasky int order; 1608d59ecb2SHans Petter Selasky 1618d59ecb2SHans Petter Selasky size = (size - 1) >> PAGE_SHIFT; 1628d59ecb2SHans Petter Selasky order = 0; 1638d59ecb2SHans Petter Selasky while (size) { 1648d59ecb2SHans Petter Selasky order++; 1658d59ecb2SHans Petter Selasky size >>= 1; 1668d59ecb2SHans Petter Selasky } 1678d59ecb2SHans Petter Selasky return (order); 1688d59ecb2SHans Petter Selasky } 1698d59ecb2SHans Petter Selasky 1708d59ecb2SHans Petter Selasky static inline void * 1718d59ecb2SHans Petter Selasky lowmem_page_address(struct page *page) 1728d59ecb2SHans Petter Selasky { 1738186b527SHans Petter Selasky return (page_address(page)); 1748d59ecb2SHans Petter Selasky } 1758d59ecb2SHans Petter Selasky 1768d59ecb2SHans Petter Selasky /* 1778186b527SHans Petter Selasky * This only works via memory map operations. 1788d59ecb2SHans Petter Selasky */ 1798d59ecb2SHans Petter Selasky static inline int 1808d59ecb2SHans Petter Selasky io_remap_pfn_range(struct vm_area_struct *vma, 1818d59ecb2SHans Petter Selasky unsigned long addr, unsigned long pfn, unsigned long size, 1828d59ecb2SHans Petter Selasky vm_memattr_t prot) 1838d59ecb2SHans Petter Selasky { 1848d59ecb2SHans Petter Selasky vma->vm_page_prot = prot; 1858d59ecb2SHans Petter Selasky vma->vm_pfn = pfn; 1868d59ecb2SHans Petter Selasky vma->vm_len = size; 1878d59ecb2SHans Petter Selasky 1888d59ecb2SHans Petter Selasky return (0); 1898d59ecb2SHans Petter Selasky } 1908d59ecb2SHans Petter Selasky 191f6823dacSVladimir Kondratyev vm_fault_t 192f6823dacSVladimir Kondratyev lkpi_vmf_insert_pfn_prot_locked(struct vm_area_struct *vma, unsigned long addr, 193f6823dacSVladimir Kondratyev unsigned long pfn, pgprot_t prot); 194f6823dacSVladimir Kondratyev 195f6823dacSVladimir Kondratyev static inline vm_fault_t 196f6823dacSVladimir Kondratyev vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, 197f6823dacSVladimir Kondratyev unsigned long pfn, pgprot_t prot) 198f6823dacSVladimir Kondratyev { 199f6823dacSVladimir Kondratyev vm_fault_t ret; 200f6823dacSVladimir Kondratyev 201f6823dacSVladimir Kondratyev VM_OBJECT_WLOCK(vma->vm_obj); 202f6823dacSVladimir Kondratyev ret = lkpi_vmf_insert_pfn_prot_locked(vma, addr, pfn, prot); 203f6823dacSVladimir Kondratyev VM_OBJECT_WUNLOCK(vma->vm_obj); 204f6823dacSVladimir Kondratyev 205f6823dacSVladimir Kondratyev return (ret); 206f6823dacSVladimir Kondratyev } 207f6823dacSVladimir Kondratyev #define vmf_insert_pfn_prot(...) \ 208f6823dacSVladimir Kondratyev _Static_assert(false, \ 209f6823dacSVladimir Kondratyev "This function is always called in a loop. Consider using the locked version") 210f6823dacSVladimir Kondratyev 2118186b527SHans Petter Selasky static inline int 2128186b527SHans Petter Selasky apply_to_page_range(struct mm_struct *mm, unsigned long address, 2138186b527SHans Petter Selasky unsigned long size, pte_fn_t fn, void *data) 2148186b527SHans Petter Selasky { 2158186b527SHans Petter Selasky return (-ENOTSUP); 2168186b527SHans Petter Selasky } 2178186b527SHans Petter Selasky 218e5a3393aSKonstantin Belousov int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, 219e5a3393aSKonstantin Belousov unsigned long size); 2208186b527SHans Petter Selasky 2218186b527SHans Petter Selasky static inline int 2228186b527SHans Petter Selasky remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 2238186b527SHans Petter Selasky unsigned long pfn, unsigned long size, pgprot_t prot) 2248186b527SHans Petter Selasky { 2258186b527SHans Petter Selasky return (-ENOTSUP); 2268186b527SHans Petter Selasky } 2278186b527SHans Petter Selasky 2288d59ecb2SHans Petter Selasky static inline unsigned long 2298d59ecb2SHans Petter Selasky vma_pages(struct vm_area_struct *vma) 2308d59ecb2SHans Petter Selasky { 2318d59ecb2SHans Petter Selasky return ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT); 2328d59ecb2SHans Petter Selasky } 2338d59ecb2SHans Petter Selasky 23437eba5b7SVladimir Kondratyev #define offset_in_page(off) ((unsigned long)(off) & (PAGE_SIZE - 1)) 2358d59ecb2SHans Petter Selasky 2368d59ecb2SHans Petter Selasky static inline void 2378d59ecb2SHans Petter Selasky set_page_dirty(struct vm_page *page) 2388d59ecb2SHans Petter Selasky { 2398d59ecb2SHans Petter Selasky vm_page_dirty(page); 2408d59ecb2SHans Petter Selasky } 2418d59ecb2SHans Petter Selasky 2428d59ecb2SHans Petter Selasky static inline void 2438186b527SHans Petter Selasky mark_page_accessed(struct vm_page *page) 2448186b527SHans Petter Selasky { 2458186b527SHans Petter Selasky vm_page_reference(page); 2468186b527SHans Petter Selasky } 2478186b527SHans Petter Selasky 2488186b527SHans Petter Selasky static inline void 2498d59ecb2SHans Petter Selasky get_page(struct vm_page *page) 2508d59ecb2SHans Petter Selasky { 2518186b527SHans Petter Selasky vm_page_wire(page); 2528d59ecb2SHans Petter Selasky } 2538d59ecb2SHans Petter Selasky 2548186b527SHans Petter Selasky extern long 2558186b527SHans Petter Selasky get_user_pages(unsigned long start, unsigned long nr_pages, 2568186b527SHans Petter Selasky int gup_flags, struct page **, 2578186b527SHans Petter Selasky struct vm_area_struct **); 2588186b527SHans Petter Selasky 2598186b527SHans Petter Selasky extern int 2608186b527SHans Petter Selasky __get_user_pages_fast(unsigned long start, int nr_pages, int write, 2618186b527SHans Petter Selasky struct page **); 2628186b527SHans Petter Selasky 2638186b527SHans Petter Selasky extern long 2648186b527SHans Petter Selasky get_user_pages_remote(struct task_struct *, struct mm_struct *, 2658186b527SHans Petter Selasky unsigned long start, unsigned long nr_pages, 2668186b527SHans Petter Selasky int gup_flags, struct page **, 2678186b527SHans Petter Selasky struct vm_area_struct **); 2688186b527SHans Petter Selasky 2698186b527SHans Petter Selasky static inline void 2708186b527SHans Petter Selasky put_page(struct vm_page *page) 2718186b527SHans Petter Selasky { 272fee2a2faSMark Johnston vm_page_unwire(page, PQ_ACTIVE); 2738186b527SHans Petter Selasky } 2748186b527SHans Petter Selasky 2758186b527SHans Petter Selasky #define copy_highpage(to, from) pmap_copy_page(from, to) 2768186b527SHans Petter Selasky 2778186b527SHans Petter Selasky static inline pgprot_t 2788186b527SHans Petter Selasky vm_get_page_prot(unsigned long vm_flags) 2798186b527SHans Petter Selasky { 2808186b527SHans Petter Selasky return (vm_flags & VM_PROT_ALL); 2818186b527SHans Petter Selasky } 2828186b527SHans Petter Selasky 2838186b527SHans Petter Selasky static inline vm_page_t 2848186b527SHans Petter Selasky vmalloc_to_page(const void *addr) 2858186b527SHans Petter Selasky { 2868186b527SHans Petter Selasky vm_paddr_t paddr; 2878186b527SHans Petter Selasky 2888186b527SHans Petter Selasky paddr = pmap_kextract((vm_offset_t)addr); 2898186b527SHans Petter Selasky return (PHYS_TO_VM_PAGE(paddr)); 2908186b527SHans Petter Selasky } 2918186b527SHans Petter Selasky 292d1c3cfd7SEmmanuel Vadot static inline int 293d1c3cfd7SEmmanuel Vadot trylock_page(struct page *page) 294d1c3cfd7SEmmanuel Vadot { 295d1c3cfd7SEmmanuel Vadot return (vm_page_trylock(page)); 296d1c3cfd7SEmmanuel Vadot } 297d1c3cfd7SEmmanuel Vadot 298d1c3cfd7SEmmanuel Vadot static inline void 299d1c3cfd7SEmmanuel Vadot unlock_page(struct page *page) 300d1c3cfd7SEmmanuel Vadot { 301d1c3cfd7SEmmanuel Vadot 302d1c3cfd7SEmmanuel Vadot vm_page_unlock(page); 303d1c3cfd7SEmmanuel Vadot } 304d1c3cfd7SEmmanuel Vadot 3058186b527SHans Petter Selasky extern int is_vmalloc_addr(const void *addr); 3068734a562SHans Petter Selasky void si_meminfo(struct sysinfo *si); 3078186b527SHans Petter Selasky 308c072f6e8SVladimir Kondratyev #define unmap_mapping_range(...) lkpi_unmap_mapping_range(__VA_ARGS__) 309c072f6e8SVladimir Kondratyev void lkpi_unmap_mapping_range(void *obj, loff_t const holebegin __unused, 310c072f6e8SVladimir Kondratyev loff_t const holelen, int even_cows __unused); 311c072f6e8SVladimir Kondratyev 312ff479cc6SAlex Richardson #define PAGE_ALIGNED(p) __is_aligned(p, PAGE_SIZE) 313ff479cc6SAlex Richardson 314307f78f3SVladimir Kondratyev #endif /* _LINUXKPI_LINUX_MM_H_ */ 315