1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2016 Matthew Macy (mmacy@mattmacy.io) 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 #include <linux/fs.h> 65 66 void 67 si_meminfo(struct sysinfo *si) 68 { 69 si->totalram = physmem; 70 si->totalhigh = 0; 71 si->mem_unit = PAGE_SIZE; 72 } 73 74 void * 75 linux_page_address(struct page *page) 76 { 77 78 if (page->object != kmem_object && page->object != kernel_object) { 79 return (PMAP_HAS_DMAP ? 80 ((void *)(uintptr_t)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(page))) : 81 NULL); 82 } 83 return ((void *)(uintptr_t)(VM_MIN_KERNEL_ADDRESS + 84 IDX_TO_OFF(page->pindex))); 85 } 86 87 vm_page_t 88 linux_alloc_pages(gfp_t flags, unsigned int order) 89 { 90 vm_page_t page; 91 92 if (PMAP_HAS_DMAP) { 93 unsigned long npages = 1UL << order; 94 int req = VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_NORMAL; 95 96 #ifdef __GFP_NOTWIRED 97 if ((flags & __GFP_NOTWIRED) != 0) 98 req &= ~VM_ALLOC_WIRED; 99 #endif 100 if ((flags & M_ZERO) != 0) 101 req |= VM_ALLOC_ZERO; 102 if (order == 0 && (flags & GFP_DMA32) == 0) { 103 page = vm_page_alloc(NULL, 0, req); 104 if (page == NULL) 105 return (NULL); 106 } else { 107 vm_paddr_t pmax = (flags & GFP_DMA32) ? 108 BUS_SPACE_MAXADDR_32BIT : BUS_SPACE_MAXADDR; 109 retry: 110 page = vm_page_alloc_contig(NULL, 0, req, 111 npages, 0, pmax, PAGE_SIZE, 0, VM_MEMATTR_DEFAULT); 112 113 if (page == NULL) { 114 if (flags & M_WAITOK) { 115 if (!vm_page_reclaim_contig(req, 116 npages, 0, pmax, PAGE_SIZE, 0)) { 117 vm_wait(NULL); 118 } 119 flags &= ~M_WAITOK; 120 goto retry; 121 } 122 return (NULL); 123 } 124 } 125 if (flags & M_ZERO) { 126 unsigned long x; 127 128 for (x = 0; x != npages; x++) { 129 vm_page_t pgo = page + x; 130 131 if ((pgo->flags & PG_ZERO) == 0) 132 pmap_zero_page(pgo); 133 } 134 } 135 } else { 136 vm_offset_t vaddr; 137 138 vaddr = linux_alloc_kmem(flags, order); 139 if (vaddr == 0) 140 return (NULL); 141 142 page = PHYS_TO_VM_PAGE(vtophys((void *)vaddr)); 143 144 KASSERT(vaddr == (vm_offset_t)page_address(page), 145 ("Page address mismatch")); 146 } 147 148 return (page); 149 } 150 151 void 152 linux_free_pages(vm_page_t page, unsigned int order) 153 { 154 if (PMAP_HAS_DMAP) { 155 unsigned long npages = 1UL << order; 156 unsigned long x; 157 158 for (x = 0; x != npages; x++) { 159 vm_page_t pgo = page + x; 160 161 vm_page_lock(pgo); 162 if (vm_page_unwire_noq(pgo)) 163 vm_page_free(pgo); 164 vm_page_unlock(pgo); 165 } 166 } else { 167 vm_offset_t vaddr; 168 169 vaddr = (vm_offset_t)page_address(page); 170 171 linux_free_kmem(vaddr, order); 172 } 173 } 174 175 vm_offset_t 176 linux_alloc_kmem(gfp_t flags, unsigned int order) 177 { 178 size_t size = ((size_t)PAGE_SIZE) << order; 179 vm_offset_t addr; 180 181 if ((flags & GFP_DMA32) == 0) { 182 addr = kmem_malloc(size, flags & GFP_NATIVE_MASK); 183 } else { 184 addr = kmem_alloc_contig(size, flags & GFP_NATIVE_MASK, 0, 185 BUS_SPACE_MAXADDR_32BIT, PAGE_SIZE, 0, VM_MEMATTR_DEFAULT); 186 } 187 return (addr); 188 } 189 190 void 191 linux_free_kmem(vm_offset_t addr, unsigned int order) 192 { 193 size_t size = ((size_t)PAGE_SIZE) << order; 194 195 kmem_free(addr, size); 196 } 197 198 static int 199 linux_get_user_pages_internal(vm_map_t map, unsigned long start, int nr_pages, 200 int write, struct page **pages) 201 { 202 vm_prot_t prot; 203 size_t len; 204 int count; 205 int i; 206 207 prot = write ? (VM_PROT_READ | VM_PROT_WRITE) : VM_PROT_READ; 208 len = ((size_t)nr_pages) << PAGE_SHIFT; 209 count = vm_fault_quick_hold_pages(map, start, len, prot, pages, nr_pages); 210 if (count == -1) 211 return (-EFAULT); 212 213 for (i = 0; i != nr_pages; i++) { 214 struct page *pg = pages[i]; 215 216 vm_page_lock(pg); 217 vm_page_wire(pg); 218 vm_page_unhold(pg); 219 vm_page_unlock(pg); 220 } 221 return (nr_pages); 222 } 223 224 int 225 __get_user_pages_fast(unsigned long start, int nr_pages, int write, 226 struct page **pages) 227 { 228 vm_map_t map; 229 vm_page_t *mp; 230 vm_offset_t va; 231 vm_offset_t end; 232 vm_prot_t prot; 233 int count; 234 235 if (nr_pages == 0 || in_interrupt()) 236 return (0); 237 238 MPASS(pages != NULL); 239 va = start; 240 map = &curthread->td_proc->p_vmspace->vm_map; 241 end = start + (((size_t)nr_pages) << PAGE_SHIFT); 242 if (start < vm_map_min(map) || end > vm_map_max(map)) 243 return (-EINVAL); 244 prot = write ? (VM_PROT_READ | VM_PROT_WRITE) : VM_PROT_READ; 245 for (count = 0, mp = pages, va = start; va < end; 246 mp++, va += PAGE_SIZE, count++) { 247 *mp = pmap_extract_and_hold(map->pmap, va, prot); 248 if (*mp == NULL) 249 break; 250 251 vm_page_lock(*mp); 252 vm_page_wire(*mp); 253 vm_page_unhold(*mp); 254 vm_page_unlock(*mp); 255 256 if ((prot & VM_PROT_WRITE) != 0 && 257 (*mp)->dirty != VM_PAGE_BITS_ALL) { 258 /* 259 * Explicitly dirty the physical page. Otherwise, the 260 * caller's changes may go unnoticed because they are 261 * performed through an unmanaged mapping or by a DMA 262 * operation. 263 * 264 * The object lock is not held here. 265 * See vm_page_clear_dirty_mask(). 266 */ 267 vm_page_dirty(*mp); 268 } 269 } 270 return (count); 271 } 272 273 long 274 get_user_pages_remote(struct task_struct *task, struct mm_struct *mm, 275 unsigned long start, unsigned long nr_pages, int gup_flags, 276 struct page **pages, struct vm_area_struct **vmas) 277 { 278 vm_map_t map; 279 280 map = &task->task_thread->td_proc->p_vmspace->vm_map; 281 return (linux_get_user_pages_internal(map, start, nr_pages, 282 !!(gup_flags & FOLL_WRITE), pages)); 283 } 284 285 long 286 get_user_pages(unsigned long start, unsigned long nr_pages, int gup_flags, 287 struct page **pages, struct vm_area_struct **vmas) 288 { 289 vm_map_t map; 290 291 map = &curthread->td_proc->p_vmspace->vm_map; 292 return (linux_get_user_pages_internal(map, start, nr_pages, 293 !!(gup_flags & FOLL_WRITE), pages)); 294 } 295 296 int 297 is_vmalloc_addr(const void *addr) 298 { 299 return (vtoslab((vm_offset_t)addr & ~UMA_SLAB_MASK) != NULL); 300 } 301 302 struct page * 303 linux_shmem_read_mapping_page_gfp(vm_object_t obj, int pindex, gfp_t gfp) 304 { 305 vm_page_t page; 306 int rv; 307 308 if ((gfp & GFP_NOWAIT) != 0) 309 panic("GFP_NOWAIT is unimplemented"); 310 311 VM_OBJECT_WLOCK(obj); 312 page = vm_page_grab(obj, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY | 313 VM_ALLOC_WIRED); 314 if (page->valid != VM_PAGE_BITS_ALL) { 315 vm_page_xbusy(page); 316 if (vm_pager_has_page(obj, pindex, NULL, NULL)) { 317 rv = vm_pager_get_pages(obj, &page, 1, NULL, NULL); 318 if (rv != VM_PAGER_OK) { 319 vm_page_lock(page); 320 vm_page_unwire_noq(page); 321 vm_page_free(page); 322 vm_page_unlock(page); 323 VM_OBJECT_WUNLOCK(obj); 324 return (ERR_PTR(-EINVAL)); 325 } 326 MPASS(page->valid == VM_PAGE_BITS_ALL); 327 } else { 328 pmap_zero_page(page); 329 page->valid = VM_PAGE_BITS_ALL; 330 page->dirty = 0; 331 } 332 vm_page_xunbusy(page); 333 } 334 VM_OBJECT_WUNLOCK(obj); 335 return (page); 336 } 337 338 struct linux_file * 339 linux_shmem_file_setup(const char *name, loff_t size, unsigned long flags) 340 { 341 struct fileobj { 342 struct linux_file file __aligned(sizeof(void *)); 343 struct vnode vnode __aligned(sizeof(void *)); 344 }; 345 struct fileobj *fileobj; 346 struct linux_file *filp; 347 struct vnode *vp; 348 int error; 349 350 fileobj = kzalloc(sizeof(*fileobj), GFP_KERNEL); 351 if (fileobj == NULL) { 352 error = -ENOMEM; 353 goto err_0; 354 } 355 filp = &fileobj->file; 356 vp = &fileobj->vnode; 357 358 filp->f_count = 1; 359 filp->f_vnode = vp; 360 filp->f_shmem = vm_pager_allocate(OBJT_DEFAULT, NULL, size, 361 VM_PROT_READ | VM_PROT_WRITE, 0, curthread->td_ucred); 362 if (filp->f_shmem == NULL) { 363 error = -ENOMEM; 364 goto err_1; 365 } 366 return (filp); 367 err_1: 368 kfree(filp); 369 err_0: 370 return (ERR_PTR(error)); 371 } 372 373 static vm_ooffset_t 374 linux_invalidate_mapping_pages_sub(vm_object_t obj, vm_pindex_t start, 375 vm_pindex_t end, int flags) 376 { 377 int start_count, end_count; 378 379 VM_OBJECT_WLOCK(obj); 380 start_count = obj->resident_page_count; 381 vm_object_page_remove(obj, start, end, flags); 382 end_count = obj->resident_page_count; 383 VM_OBJECT_WUNLOCK(obj); 384 return (start_count - end_count); 385 } 386 387 unsigned long 388 linux_invalidate_mapping_pages(vm_object_t obj, pgoff_t start, pgoff_t end) 389 { 390 391 return (linux_invalidate_mapping_pages_sub(obj, start, end, OBJPR_CLEANONLY)); 392 } 393 394 void 395 linux_shmem_truncate_range(vm_object_t obj, loff_t lstart, loff_t lend) 396 { 397 vm_pindex_t start = OFF_TO_IDX(lstart + PAGE_SIZE - 1); 398 vm_pindex_t end = OFF_TO_IDX(lend + 1); 399 400 (void) linux_invalidate_mapping_pages_sub(obj, start, end, 0); 401 } 402