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 if ((flags & M_ZERO) != 0) 97 req |= VM_ALLOC_ZERO; 98 if (order == 0 && (flags & GFP_DMA32) == 0) { 99 page = vm_page_alloc(NULL, 0, req); 100 if (page == NULL) 101 return (NULL); 102 } else { 103 vm_paddr_t pmax = (flags & GFP_DMA32) ? 104 BUS_SPACE_MAXADDR_32BIT : BUS_SPACE_MAXADDR; 105 retry: 106 page = vm_page_alloc_contig(NULL, 0, req, 107 npages, 0, pmax, PAGE_SIZE, 0, VM_MEMATTR_DEFAULT); 108 109 if (page == NULL) { 110 if (flags & M_WAITOK) { 111 if (!vm_page_reclaim_contig(req, 112 npages, 0, pmax, PAGE_SIZE, 0)) { 113 vm_wait(NULL); 114 } 115 flags &= ~M_WAITOK; 116 goto retry; 117 } 118 return (NULL); 119 } 120 } 121 if (flags & M_ZERO) { 122 unsigned long x; 123 124 for (x = 0; x != npages; x++) { 125 vm_page_t pgo = page + x; 126 127 if ((pgo->flags & PG_ZERO) == 0) 128 pmap_zero_page(pgo); 129 } 130 } 131 } else { 132 vm_offset_t vaddr; 133 134 vaddr = linux_alloc_kmem(flags, order); 135 if (vaddr == 0) 136 return (NULL); 137 138 page = PHYS_TO_VM_PAGE(vtophys((void *)vaddr)); 139 140 KASSERT(vaddr == (vm_offset_t)page_address(page), 141 ("Page address mismatch")); 142 } 143 144 return (page); 145 } 146 147 void 148 linux_free_pages(vm_page_t page, unsigned int order) 149 { 150 if (PMAP_HAS_DMAP) { 151 unsigned long npages = 1UL << order; 152 unsigned long x; 153 154 for (x = 0; x != npages; x++) { 155 vm_page_t pgo = page + x; 156 157 if (vm_page_unwire_noq(pgo)) 158 vm_page_free(pgo); 159 } 160 } else { 161 vm_offset_t vaddr; 162 163 vaddr = (vm_offset_t)page_address(page); 164 165 linux_free_kmem(vaddr, order); 166 } 167 } 168 169 vm_offset_t 170 linux_alloc_kmem(gfp_t flags, unsigned int order) 171 { 172 size_t size = ((size_t)PAGE_SIZE) << order; 173 vm_offset_t addr; 174 175 if ((flags & GFP_DMA32) == 0) { 176 addr = kmem_malloc(size, flags & GFP_NATIVE_MASK); 177 } else { 178 addr = kmem_alloc_contig(size, flags & GFP_NATIVE_MASK, 0, 179 BUS_SPACE_MAXADDR_32BIT, PAGE_SIZE, 0, VM_MEMATTR_DEFAULT); 180 } 181 return (addr); 182 } 183 184 void 185 linux_free_kmem(vm_offset_t addr, unsigned int order) 186 { 187 size_t size = ((size_t)PAGE_SIZE) << order; 188 189 kmem_free(addr, size); 190 } 191 192 static int 193 linux_get_user_pages_internal(vm_map_t map, unsigned long start, int nr_pages, 194 int write, struct page **pages) 195 { 196 vm_prot_t prot; 197 size_t len; 198 int count; 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 return (count == -1 ? -EFAULT : nr_pages); 204 } 205 206 int 207 __get_user_pages_fast(unsigned long start, int nr_pages, int write, 208 struct page **pages) 209 { 210 vm_map_t map; 211 vm_page_t *mp; 212 vm_offset_t va; 213 vm_offset_t end; 214 vm_prot_t prot; 215 int count; 216 217 if (nr_pages == 0 || in_interrupt()) 218 return (0); 219 220 MPASS(pages != NULL); 221 va = start; 222 map = &curthread->td_proc->p_vmspace->vm_map; 223 end = start + (((size_t)nr_pages) << PAGE_SHIFT); 224 if (start < vm_map_min(map) || end > vm_map_max(map)) 225 return (-EINVAL); 226 prot = write ? (VM_PROT_READ | VM_PROT_WRITE) : VM_PROT_READ; 227 for (count = 0, mp = pages, va = start; va < end; 228 mp++, va += PAGE_SIZE, count++) { 229 *mp = pmap_extract_and_hold(map->pmap, va, prot); 230 if (*mp == NULL) 231 break; 232 233 if ((prot & VM_PROT_WRITE) != 0 && 234 (*mp)->dirty != VM_PAGE_BITS_ALL) { 235 /* 236 * Explicitly dirty the physical page. Otherwise, the 237 * caller's changes may go unnoticed because they are 238 * performed through an unmanaged mapping or by a DMA 239 * operation. 240 * 241 * The object lock is not held here. 242 * See vm_page_clear_dirty_mask(). 243 */ 244 vm_page_dirty(*mp); 245 } 246 } 247 return (count); 248 } 249 250 long 251 get_user_pages_remote(struct task_struct *task, struct mm_struct *mm, 252 unsigned long start, unsigned long nr_pages, int gup_flags, 253 struct page **pages, struct vm_area_struct **vmas) 254 { 255 vm_map_t map; 256 257 map = &task->task_thread->td_proc->p_vmspace->vm_map; 258 return (linux_get_user_pages_internal(map, start, nr_pages, 259 !!(gup_flags & FOLL_WRITE), pages)); 260 } 261 262 long 263 get_user_pages(unsigned long start, unsigned long nr_pages, int gup_flags, 264 struct page **pages, struct vm_area_struct **vmas) 265 { 266 vm_map_t map; 267 268 map = &curthread->td_proc->p_vmspace->vm_map; 269 return (linux_get_user_pages_internal(map, start, nr_pages, 270 !!(gup_flags & FOLL_WRITE), pages)); 271 } 272 273 int 274 is_vmalloc_addr(const void *addr) 275 { 276 return (vtoslab((vm_offset_t)addr & ~UMA_SLAB_MASK) != NULL); 277 } 278 279 struct page * 280 linux_shmem_read_mapping_page_gfp(vm_object_t obj, int pindex, gfp_t gfp) 281 { 282 vm_page_t page; 283 int rv; 284 285 if ((gfp & GFP_NOWAIT) != 0) 286 panic("GFP_NOWAIT is unimplemented"); 287 288 VM_OBJECT_WLOCK(obj); 289 rv = vm_page_grab_valid(&page, obj, pindex, VM_ALLOC_NORMAL | 290 VM_ALLOC_NOBUSY | VM_ALLOC_WIRED); 291 VM_OBJECT_WUNLOCK(obj); 292 if (rv != VM_PAGER_OK) 293 return (ERR_PTR(-EINVAL)); 294 return (page); 295 } 296 297 struct linux_file * 298 linux_shmem_file_setup(const char *name, loff_t size, unsigned long flags) 299 { 300 struct fileobj { 301 struct linux_file file __aligned(sizeof(void *)); 302 struct vnode vnode __aligned(sizeof(void *)); 303 }; 304 struct fileobj *fileobj; 305 struct linux_file *filp; 306 struct vnode *vp; 307 int error; 308 309 fileobj = kzalloc(sizeof(*fileobj), GFP_KERNEL); 310 if (fileobj == NULL) { 311 error = -ENOMEM; 312 goto err_0; 313 } 314 filp = &fileobj->file; 315 vp = &fileobj->vnode; 316 317 filp->f_count = 1; 318 filp->f_vnode = vp; 319 filp->f_shmem = vm_pager_allocate(OBJT_DEFAULT, NULL, size, 320 VM_PROT_READ | VM_PROT_WRITE, 0, curthread->td_ucred); 321 if (filp->f_shmem == NULL) { 322 error = -ENOMEM; 323 goto err_1; 324 } 325 return (filp); 326 err_1: 327 kfree(filp); 328 err_0: 329 return (ERR_PTR(error)); 330 } 331 332 static vm_ooffset_t 333 linux_invalidate_mapping_pages_sub(vm_object_t obj, vm_pindex_t start, 334 vm_pindex_t end, int flags) 335 { 336 int start_count, end_count; 337 338 VM_OBJECT_WLOCK(obj); 339 start_count = obj->resident_page_count; 340 vm_object_page_remove(obj, start, end, flags); 341 end_count = obj->resident_page_count; 342 VM_OBJECT_WUNLOCK(obj); 343 return (start_count - end_count); 344 } 345 346 unsigned long 347 linux_invalidate_mapping_pages(vm_object_t obj, pgoff_t start, pgoff_t end) 348 { 349 350 return (linux_invalidate_mapping_pages_sub(obj, start, end, OBJPR_CLEANONLY)); 351 } 352 353 void 354 linux_shmem_truncate_range(vm_object_t obj, loff_t lstart, loff_t lend) 355 { 356 vm_pindex_t start = OFF_TO_IDX(lstart + PAGE_SIZE - 1); 357 vm_pindex_t end = OFF_TO_IDX(lend + 1); 358 359 (void) linux_invalidate_mapping_pages_sub(obj, start, end, 0); 360 } 361