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 #include <linux/shmem_fs.h> 66 67 void 68 si_meminfo(struct sysinfo *si) 69 { 70 si->totalram = physmem; 71 si->totalhigh = 0; 72 si->mem_unit = PAGE_SIZE; 73 } 74 75 void * 76 linux_page_address(struct page *page) 77 { 78 79 if (page->object != kernel_object) { 80 return (PMAP_HAS_DMAP ? 81 ((void *)(uintptr_t)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(page))) : 82 NULL); 83 } 84 return ((void *)(uintptr_t)(VM_MIN_KERNEL_ADDRESS + 85 IDX_TO_OFF(page->pindex))); 86 } 87 88 vm_page_t 89 linux_alloc_pages(gfp_t flags, unsigned int order) 90 { 91 vm_page_t page; 92 93 if (PMAP_HAS_DMAP) { 94 unsigned long npages = 1UL << order; 95 int req = VM_ALLOC_WIRED; 96 97 if ((flags & M_ZERO) != 0) 98 req |= VM_ALLOC_ZERO; 99 if (order == 0 && (flags & GFP_DMA32) == 0) { 100 page = vm_page_alloc_noobj(req); 101 if (page == NULL) 102 return (NULL); 103 } else { 104 vm_paddr_t pmax = (flags & GFP_DMA32) ? 105 BUS_SPACE_MAXADDR_32BIT : BUS_SPACE_MAXADDR; 106 retry: 107 page = vm_page_alloc_noobj_contig(req, npages, 0, pmax, 108 PAGE_SIZE, 0, VM_MEMATTR_DEFAULT); 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 } else { 122 vm_offset_t vaddr; 123 124 vaddr = linux_alloc_kmem(flags, order); 125 if (vaddr == 0) 126 return (NULL); 127 128 page = PHYS_TO_VM_PAGE(vtophys((void *)vaddr)); 129 130 KASSERT(vaddr == (vm_offset_t)page_address(page), 131 ("Page address mismatch")); 132 } 133 134 return (page); 135 } 136 137 void 138 linux_free_pages(vm_page_t page, unsigned int order) 139 { 140 if (PMAP_HAS_DMAP) { 141 unsigned long npages = 1UL << order; 142 unsigned long x; 143 144 for (x = 0; x != npages; x++) { 145 vm_page_t pgo = page + x; 146 147 if (vm_page_unwire_noq(pgo)) 148 vm_page_free(pgo); 149 } 150 } else { 151 vm_offset_t vaddr; 152 153 vaddr = (vm_offset_t)page_address(page); 154 155 linux_free_kmem(vaddr, order); 156 } 157 } 158 159 vm_offset_t 160 linux_alloc_kmem(gfp_t flags, unsigned int order) 161 { 162 size_t size = ((size_t)PAGE_SIZE) << order; 163 vm_offset_t addr; 164 165 if ((flags & GFP_DMA32) == 0) { 166 addr = kmem_malloc(size, flags & GFP_NATIVE_MASK); 167 } else { 168 addr = kmem_alloc_contig(size, flags & GFP_NATIVE_MASK, 0, 169 BUS_SPACE_MAXADDR_32BIT, PAGE_SIZE, 0, VM_MEMATTR_DEFAULT); 170 } 171 return (addr); 172 } 173 174 void 175 linux_free_kmem(vm_offset_t addr, unsigned int order) 176 { 177 size_t size = ((size_t)PAGE_SIZE) << order; 178 179 kmem_free(addr, size); 180 } 181 182 static int 183 linux_get_user_pages_internal(vm_map_t map, unsigned long start, int nr_pages, 184 int write, struct page **pages) 185 { 186 vm_prot_t prot; 187 size_t len; 188 int count; 189 190 prot = write ? (VM_PROT_READ | VM_PROT_WRITE) : VM_PROT_READ; 191 len = ptoa((vm_offset_t)nr_pages); 192 count = vm_fault_quick_hold_pages(map, start, len, prot, pages, nr_pages); 193 return (count == -1 ? -EFAULT : nr_pages); 194 } 195 196 int 197 __get_user_pages_fast(unsigned long start, int nr_pages, int write, 198 struct page **pages) 199 { 200 vm_map_t map; 201 vm_page_t *mp; 202 vm_offset_t va; 203 vm_offset_t end; 204 vm_prot_t prot; 205 int count; 206 207 if (nr_pages == 0 || in_interrupt()) 208 return (0); 209 210 MPASS(pages != NULL); 211 map = &curthread->td_proc->p_vmspace->vm_map; 212 end = start + ptoa((vm_offset_t)nr_pages); 213 if (!vm_map_range_valid(map, start, end)) 214 return (-EINVAL); 215 prot = write ? (VM_PROT_READ | VM_PROT_WRITE) : VM_PROT_READ; 216 for (count = 0, mp = pages, va = start; va < end; 217 mp++, va += PAGE_SIZE, count++) { 218 *mp = pmap_extract_and_hold(map->pmap, va, prot); 219 if (*mp == NULL) 220 break; 221 222 if ((prot & VM_PROT_WRITE) != 0 && 223 (*mp)->dirty != VM_PAGE_BITS_ALL) { 224 /* 225 * Explicitly dirty the physical page. Otherwise, the 226 * caller's changes may go unnoticed because they are 227 * performed through an unmanaged mapping or by a DMA 228 * operation. 229 * 230 * The object lock is not held here. 231 * See vm_page_clear_dirty_mask(). 232 */ 233 vm_page_dirty(*mp); 234 } 235 } 236 return (count); 237 } 238 239 long 240 get_user_pages_remote(struct task_struct *task, struct mm_struct *mm, 241 unsigned long start, unsigned long nr_pages, int gup_flags, 242 struct page **pages, struct vm_area_struct **vmas) 243 { 244 vm_map_t map; 245 246 map = &task->task_thread->td_proc->p_vmspace->vm_map; 247 return (linux_get_user_pages_internal(map, start, nr_pages, 248 !!(gup_flags & FOLL_WRITE), pages)); 249 } 250 251 long 252 get_user_pages(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 = &curthread->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 int 263 is_vmalloc_addr(const void *addr) 264 { 265 return (vtoslab((vm_offset_t)addr & ~UMA_SLAB_MASK) != NULL); 266 } 267 268 vm_fault_t 269 lkpi_vmf_insert_pfn_prot_locked(struct vm_area_struct *vma, unsigned long addr, 270 unsigned long pfn, pgprot_t prot) 271 { 272 vm_object_t vm_obj = vma->vm_obj; 273 vm_object_t tmp_obj; 274 vm_page_t page; 275 vm_pindex_t pindex; 276 277 VM_OBJECT_ASSERT_WLOCKED(vm_obj); 278 pindex = OFF_TO_IDX(addr - vma->vm_start); 279 if (vma->vm_pfn_count == 0) 280 vma->vm_pfn_first = pindex; 281 MPASS(pindex <= OFF_TO_IDX(vma->vm_end)); 282 283 retry: 284 page = vm_page_grab(vm_obj, pindex, VM_ALLOC_NOCREAT); 285 if (page == NULL) { 286 page = PHYS_TO_VM_PAGE(IDX_TO_OFF(pfn)); 287 if (!vm_page_busy_acquire(page, VM_ALLOC_WAITFAIL)) 288 goto retry; 289 if (page->object != NULL) { 290 tmp_obj = page->object; 291 vm_page_xunbusy(page); 292 VM_OBJECT_WUNLOCK(vm_obj); 293 VM_OBJECT_WLOCK(tmp_obj); 294 if (page->object == tmp_obj && 295 vm_page_busy_acquire(page, VM_ALLOC_WAITFAIL)) { 296 KASSERT(page->object == tmp_obj, 297 ("page has changed identity")); 298 KASSERT((page->oflags & VPO_UNMANAGED) == 0, 299 ("page does not belong to shmem")); 300 vm_pager_page_unswapped(page); 301 if (pmap_page_is_mapped(page)) { 302 vm_page_xunbusy(page); 303 VM_OBJECT_WUNLOCK(tmp_obj); 304 printf("%s: page rename failed: page " 305 "is mapped\n", __func__); 306 VM_OBJECT_WLOCK(vm_obj); 307 return (VM_FAULT_NOPAGE); 308 } 309 vm_page_remove(page); 310 } 311 VM_OBJECT_WUNLOCK(tmp_obj); 312 VM_OBJECT_WLOCK(vm_obj); 313 goto retry; 314 } 315 if (vm_page_insert(page, vm_obj, pindex)) { 316 vm_page_xunbusy(page); 317 return (VM_FAULT_OOM); 318 } 319 vm_page_valid(page); 320 } 321 pmap_page_set_memattr(page, pgprot2cachemode(prot)); 322 vma->vm_pfn_count++; 323 324 return (VM_FAULT_NOPAGE); 325 } 326 327 /* 328 * Although FreeBSD version of unmap_mapping_range has semantics and types of 329 * parameters compatible with Linux version, the values passed in are different 330 * @obj should match to vm_private_data field of vm_area_struct returned by 331 * mmap file operation handler, see linux_file_mmap_single() sources 332 * @holelen should match to size of area to be munmapped. 333 */ 334 void 335 lkpi_unmap_mapping_range(void *obj, loff_t const holebegin __unused, 336 loff_t const holelen, int even_cows __unused) 337 { 338 vm_object_t devobj; 339 vm_page_t page; 340 int i, page_count; 341 342 devobj = cdev_pager_lookup(obj); 343 if (devobj != NULL) { 344 page_count = OFF_TO_IDX(holelen); 345 346 VM_OBJECT_WLOCK(devobj); 347 retry: 348 for (i = 0; i < page_count; i++) { 349 page = vm_page_lookup(devobj, i); 350 if (page == NULL) 351 continue; 352 if (!vm_page_busy_acquire(page, VM_ALLOC_WAITFAIL)) 353 goto retry; 354 cdev_pager_free_page(devobj, page); 355 } 356 VM_OBJECT_WUNLOCK(devobj); 357 vm_object_deallocate(devobj); 358 } 359 } 360