1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2016 Matt Macy (mmacy@nextbsd.org) 4 * Copyright (c) 2017 Mellanox Technologies, Ltd. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/malloc.h> 35 #include <sys/kernel.h> 36 #include <sys/sysctl.h> 37 #include <sys/lock.h> 38 #include <sys/mutex.h> 39 #include <sys/rwlock.h> 40 #include <sys/proc.h> 41 #include <sys/sched.h> 42 43 #include <machine/bus.h> 44 45 #include <vm/vm.h> 46 #include <vm/pmap.h> 47 #include <vm/vm_param.h> 48 #include <vm/vm_kern.h> 49 #include <vm/vm_object.h> 50 #include <vm/vm_map.h> 51 #include <vm/vm_page.h> 52 #include <vm/vm_pageout.h> 53 #include <vm/vm_pager.h> 54 #include <vm/vm_radix.h> 55 #include <vm/vm_reserv.h> 56 #include <vm/vm_extern.h> 57 58 #include <vm/uma.h> 59 #include <vm/uma_int.h> 60 61 #include <linux/gfp.h> 62 #include <linux/mm.h> 63 #include <linux/preempt.h> 64 65 #if defined(__amd64__) || defined(__aarch64__) || defined(__riscv__) 66 #define LINUXKPI_HAVE_DMAP 67 #else 68 #undef LINUXKPI_HAVE_DMAP 69 #endif 70 71 void * 72 linux_page_address(struct page *page) 73 { 74 75 if (page->object != kmem_object && page->object != kernel_object) { 76 #ifdef LINUXKPI_HAVE_DMAP 77 return ((void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(page))); 78 #else 79 return (NULL); 80 #endif 81 } 82 return ((void *)(uintptr_t)(VM_MIN_KERNEL_ADDRESS + 83 IDX_TO_OFF(page->pindex))); 84 } 85 86 vm_page_t 87 linux_alloc_pages(gfp_t flags, unsigned int order) 88 { 89 #ifdef LINUXKPI_HAVE_DMAP 90 unsigned long npages = 1UL << order; 91 int req = (flags & M_ZERO) ? (VM_ALLOC_ZERO | VM_ALLOC_NOOBJ | 92 VM_ALLOC_NORMAL) : (VM_ALLOC_NOOBJ | VM_ALLOC_NORMAL); 93 vm_page_t page; 94 95 if (order == 0 && (flags & GFP_DMA32) == 0) { 96 page = vm_page_alloc(NULL, 0, req); 97 if (page == NULL) 98 return (NULL); 99 } else { 100 vm_paddr_t pmax = (flags & GFP_DMA32) ? 101 BUS_SPACE_MAXADDR_32BIT : BUS_SPACE_MAXADDR; 102 retry: 103 page = vm_page_alloc_contig(NULL, 0, req, 104 npages, 0, pmax, PAGE_SIZE, 0, VM_MEMATTR_DEFAULT); 105 106 if (page == NULL) { 107 if (flags & M_WAITOK) { 108 if (!vm_page_reclaim_contig(req, 109 npages, 0, pmax, PAGE_SIZE, 0)) { 110 VM_WAIT; 111 } 112 flags &= ~M_WAITOK; 113 goto retry; 114 } 115 return (NULL); 116 } 117 } 118 if (flags & M_ZERO) { 119 unsigned long x; 120 121 for (x = 0; x != npages; x++) { 122 vm_page_t pgo = page + x; 123 124 if ((pgo->flags & PG_ZERO) == 0) 125 pmap_zero_page(pgo); 126 } 127 } 128 #else 129 vm_offset_t vaddr; 130 vm_page_t page; 131 132 vaddr = linux_alloc_kmem(flags, order); 133 if (vaddr == 0) 134 return (NULL); 135 136 page = PHYS_TO_VM_PAGE(vtophys((void *)vaddr)); 137 138 KASSERT(vaddr == (vm_offset_t)page_address(page), 139 ("Page address mismatch")); 140 #endif 141 return (page); 142 } 143 144 void 145 linux_free_pages(vm_page_t page, unsigned int order) 146 { 147 #ifdef LINUXKPI_HAVE_DMAP 148 unsigned long npages = 1UL << order; 149 unsigned long x; 150 151 for (x = 0; x != npages; x++) { 152 vm_page_t pgo = page + x; 153 154 vm_page_lock(pgo); 155 vm_page_free(pgo); 156 vm_page_unlock(pgo); 157 } 158 #else 159 vm_offset_t vaddr; 160 161 vaddr = (vm_offset_t)page_address(page); 162 163 linux_free_kmem(vaddr, order); 164 #endif 165 } 166 167 vm_offset_t 168 linux_alloc_kmem(gfp_t flags, unsigned int order) 169 { 170 size_t size = ((size_t)PAGE_SIZE) << order; 171 vm_offset_t addr; 172 173 if ((flags & GFP_DMA32) == 0) { 174 addr = kmem_malloc(kmem_arena, size, flags & GFP_NATIVE_MASK); 175 } else { 176 addr = kmem_alloc_contig(kmem_arena, size, 177 flags & GFP_NATIVE_MASK, 0, BUS_SPACE_MAXADDR_32BIT, 178 PAGE_SIZE, 0, VM_MEMATTR_DEFAULT); 179 } 180 return (addr); 181 } 182 183 void 184 linux_free_kmem(vm_offset_t addr, unsigned int order) 185 { 186 size_t size = ((size_t)PAGE_SIZE) << order; 187 188 kmem_free(kmem_arena, addr, size); 189 } 190 191 static int 192 linux_get_user_pages_internal(vm_map_t map, unsigned long start, int nr_pages, 193 int write, struct page **pages) 194 { 195 vm_prot_t prot; 196 size_t len; 197 int count; 198 int i; 199 200 prot = write ? (VM_PROT_READ | VM_PROT_WRITE) : VM_PROT_READ; 201 len = ((size_t)nr_pages) << PAGE_SHIFT; 202 count = vm_fault_quick_hold_pages(map, start, len, prot, pages, nr_pages); 203 if (count == -1) 204 return (-EFAULT); 205 206 for (i = 0; i != nr_pages; i++) { 207 struct page *pg = pages[i]; 208 209 vm_page_lock(pg); 210 vm_page_wire(pg); 211 vm_page_unlock(pg); 212 } 213 return (nr_pages); 214 } 215 216 int 217 __get_user_pages_fast(unsigned long start, int nr_pages, int write, 218 struct page **pages) 219 { 220 vm_map_t map; 221 vm_page_t *mp; 222 vm_offset_t va; 223 vm_offset_t end; 224 vm_prot_t prot; 225 int count; 226 227 if (nr_pages == 0 || in_interrupt()) 228 return (0); 229 230 MPASS(pages != NULL); 231 va = start; 232 map = &curthread->td_proc->p_vmspace->vm_map; 233 end = start + (((size_t)nr_pages) << PAGE_SHIFT); 234 if (start < vm_map_min(map) || end > vm_map_max(map)) 235 return (-EINVAL); 236 prot = write ? (VM_PROT_READ | VM_PROT_WRITE) : VM_PROT_READ; 237 for (count = 0, mp = pages, va = start; va < end; 238 mp++, va += PAGE_SIZE, count++) { 239 *mp = pmap_extract_and_hold(map->pmap, va, prot); 240 if (*mp == NULL) 241 break; 242 243 vm_page_lock(*mp); 244 vm_page_wire(*mp); 245 vm_page_unlock(*mp); 246 247 if ((prot & VM_PROT_WRITE) != 0 && 248 (*mp)->dirty != VM_PAGE_BITS_ALL) { 249 /* 250 * Explicitly dirty the physical page. Otherwise, the 251 * caller's changes may go unnoticed because they are 252 * performed through an unmanaged mapping or by a DMA 253 * operation. 254 * 255 * The object lock is not held here. 256 * See vm_page_clear_dirty_mask(). 257 */ 258 vm_page_dirty(*mp); 259 } 260 } 261 return (count); 262 } 263 264 long 265 get_user_pages_remote(struct task_struct *task, struct mm_struct *mm, 266 unsigned long start, unsigned long nr_pages, int gup_flags, 267 struct page **pages, struct vm_area_struct **vmas) 268 { 269 vm_map_t map; 270 271 map = &mm->vmspace->vm_map; 272 return (linux_get_user_pages_internal(map, start, nr_pages, 273 !!(gup_flags & FOLL_WRITE), pages)); 274 } 275 276 long 277 get_user_pages(unsigned long start, unsigned long nr_pages, int gup_flags, 278 struct page **pages, struct vm_area_struct **vmas) 279 { 280 vm_map_t map; 281 282 map = &curthread->td_proc->p_vmspace->vm_map; 283 return (linux_get_user_pages_internal(map, start, nr_pages, 284 !!(gup_flags & FOLL_WRITE), pages)); 285 } 286 287 int 288 is_vmalloc_addr(const void *addr) 289 { 290 return (vtoslab((vm_offset_t)addr & ~UMA_SLAB_MASK) != NULL); 291 } 292