Home | History | Annotate | Line # | Download | only in uvm
uvm_bio.c revision 1.102
      1 /*	$NetBSD: uvm_bio.c,v 1.102 2019/12/31 22:42:51 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 Chuck Silvers.
      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, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  *
     30  */
     31 
     32 /*
     33  * uvm_bio.c: buffered i/o object mapping cache
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: uvm_bio.c,v 1.102 2019/12/31 22:42:51 ad Exp $");
     38 
     39 #include "opt_uvmhist.h"
     40 #include "opt_ubc.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kmem.h>
     45 #include <sys/kernel.h>
     46 #include <sys/proc.h>
     47 #include <sys/vnode.h>
     48 
     49 #include <uvm/uvm.h>
     50 
     51 #ifdef PMAP_DIRECT
     52 #  define UBC_USE_PMAP_DIRECT
     53 #endif
     54 
     55 /*
     56  * local functions
     57  */
     58 
     59 static int	ubc_fault(struct uvm_faultinfo *, vaddr_t, struct vm_page **,
     60 			  int, int, vm_prot_t, int);
     61 static struct ubc_map *ubc_find_mapping(struct uvm_object *, voff_t);
     62 #ifdef UBC_USE_PMAP_DIRECT
     63 static int __noinline ubc_uiomove_direct(struct uvm_object *, struct uio *, vsize_t,
     64 			  int, int);
     65 static void __noinline ubc_zerorange_direct(struct uvm_object *, off_t, size_t, int);
     66 
     67 bool ubc_direct = false; /* XXX */
     68 #endif
     69 
     70 /*
     71  * local data structues
     72  */
     73 
     74 #define UBC_HASH(uobj, offset) 						\
     75 	(((((u_long)(uobj)) >> 8) + (((u_long)(offset)) >> PAGE_SHIFT)) & \
     76 				ubc_object.hashmask)
     77 
     78 #define UBC_QUEUE(offset)						\
     79 	(&ubc_object.inactive[(((u_long)(offset)) >> ubc_winshift) &	\
     80 			     (UBC_NQUEUES - 1)])
     81 
     82 #define UBC_UMAP_ADDR(u)						\
     83 	(vaddr_t)(ubc_object.kva + (((u) - ubc_object.umap) << ubc_winshift))
     84 
     85 
     86 #define UMAP_PAGES_LOCKED	0x0001
     87 #define UMAP_MAPPING_CACHED	0x0002
     88 
     89 struct ubc_map {
     90 	struct uvm_object *	uobj;		/* mapped object */
     91 	voff_t			offset;		/* offset into uobj */
     92 	voff_t			writeoff;	/* write offset */
     93 	vsize_t			writelen;	/* write len */
     94 	int			refcount;	/* refcount on mapping */
     95 	int			flags;		/* extra state */
     96 	int			advice;
     97 
     98 	LIST_ENTRY(ubc_map)	hash;		/* hash table */
     99 	TAILQ_ENTRY(ubc_map)	inactive;	/* inactive queue */
    100 	LIST_ENTRY(ubc_map)	list;		/* per-object list */
    101 };
    102 
    103 TAILQ_HEAD(ubc_inactive_head, ubc_map);
    104 static struct ubc_object {
    105 	struct uvm_object uobj;		/* glue for uvm_map() */
    106 	char *kva;			/* where ubc_object is mapped */
    107 	struct ubc_map *umap;		/* array of ubc_map's */
    108 
    109 	LIST_HEAD(, ubc_map) *hash;	/* hashtable for cached ubc_map's */
    110 	u_long hashmask;		/* mask for hashtable */
    111 
    112 	struct ubc_inactive_head *inactive;
    113 					/* inactive queues for ubc_map's */
    114 } ubc_object;
    115 
    116 const struct uvm_pagerops ubc_pager = {
    117 	.pgo_fault = ubc_fault,
    118 	/* ... rest are NULL */
    119 };
    120 
    121 int ubc_nwins = UBC_NWINS;
    122 int ubc_winshift __read_mostly = UBC_WINSHIFT;
    123 int ubc_winsize __read_mostly;
    124 #if defined(PMAP_PREFER)
    125 int ubc_nqueues;
    126 #define UBC_NQUEUES ubc_nqueues
    127 #else
    128 #define UBC_NQUEUES 1
    129 #endif
    130 
    131 #if defined(UBC_STATS)
    132 
    133 #define	UBC_EVCNT_DEFINE(name) \
    134 struct evcnt ubc_evcnt_##name = \
    135 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "ubc", #name); \
    136 EVCNT_ATTACH_STATIC(ubc_evcnt_##name);
    137 #define	UBC_EVCNT_INCR(name) ubc_evcnt_##name.ev_count++
    138 
    139 #else /* defined(UBC_STATS) */
    140 
    141 #define	UBC_EVCNT_DEFINE(name)	/* nothing */
    142 #define	UBC_EVCNT_INCR(name)	/* nothing */
    143 
    144 #endif /* defined(UBC_STATS) */
    145 
    146 UBC_EVCNT_DEFINE(wincachehit)
    147 UBC_EVCNT_DEFINE(wincachemiss)
    148 UBC_EVCNT_DEFINE(faultbusy)
    149 
    150 /*
    151  * ubc_init
    152  *
    153  * init pager private data structures.
    154  */
    155 
    156 void
    157 ubc_init(void)
    158 {
    159 	/*
    160 	 * Make sure ubc_winshift is sane.
    161 	 */
    162 	if (ubc_winshift < PAGE_SHIFT)
    163 		ubc_winshift = PAGE_SHIFT;
    164 	ubc_winsize = 1 << ubc_winshift;
    165 
    166 	/*
    167 	 * init ubc_object.
    168 	 * alloc and init ubc_map's.
    169 	 * init inactive queues.
    170 	 * alloc and init hashtable.
    171 	 * map in ubc_object.
    172 	 */
    173 
    174 	uvm_obj_init(&ubc_object.uobj, &ubc_pager, true, UVM_OBJ_KERN);
    175 
    176 	ubc_object.umap = kmem_zalloc(ubc_nwins * sizeof(struct ubc_map),
    177 	    KM_SLEEP);
    178 	if (ubc_object.umap == NULL)
    179 		panic("ubc_init: failed to allocate ubc_map");
    180 
    181 	vaddr_t va = (vaddr_t)1L;
    182 #ifdef PMAP_PREFER
    183 	PMAP_PREFER(0, &va, 0, 0);	/* kernel is never topdown */
    184 	ubc_nqueues = va >> ubc_winshift;
    185 	if (ubc_nqueues == 0) {
    186 		ubc_nqueues = 1;
    187 	}
    188 #endif
    189 	ubc_object.inactive = kmem_alloc(UBC_NQUEUES *
    190 	    sizeof(struct ubc_inactive_head), KM_SLEEP);
    191 	for (int i = 0; i < UBC_NQUEUES; i++) {
    192 		TAILQ_INIT(&ubc_object.inactive[i]);
    193 	}
    194 	for (int i = 0; i < ubc_nwins; i++) {
    195 		struct ubc_map *umap;
    196 		umap = &ubc_object.umap[i];
    197 		TAILQ_INSERT_TAIL(&ubc_object.inactive[i & (UBC_NQUEUES - 1)],
    198 				  umap, inactive);
    199 	}
    200 
    201 	ubc_object.hash = hashinit(ubc_nwins, HASH_LIST, true,
    202 	    &ubc_object.hashmask);
    203 	for (int i = 0; i <= ubc_object.hashmask; i++) {
    204 		LIST_INIT(&ubc_object.hash[i]);
    205 	}
    206 
    207 	if (uvm_map(kernel_map, (vaddr_t *)&ubc_object.kva,
    208 		    ubc_nwins << ubc_winshift, &ubc_object.uobj, 0, (vsize_t)va,
    209 		    UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_NONE,
    210 				UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != 0) {
    211 		panic("ubc_init: failed to map ubc_object");
    212 	}
    213 }
    214 
    215 void
    216 ubchist_init(void)
    217 {
    218 
    219 	UVMHIST_INIT(ubchist, 300);
    220 }
    221 
    222 /*
    223  * ubc_fault_page: helper of ubc_fault to handle a single page.
    224  *
    225  * => Caller has UVM object locked.
    226  * => Caller will perform pmap_update().
    227  */
    228 
    229 static inline int
    230 ubc_fault_page(const struct uvm_faultinfo *ufi, const struct ubc_map *umap,
    231     struct vm_page *pg, vm_prot_t prot, vm_prot_t access_type, vaddr_t va)
    232 {
    233 	struct uvm_object *uobj;
    234 	vm_prot_t mask;
    235 	int error;
    236 	bool rdonly;
    237 
    238 	uobj = pg->uobject;
    239 	KASSERT(mutex_owned(uobj->vmobjlock));
    240 
    241 	if (pg->flags & PG_WANTED) {
    242 		wakeup(pg);
    243 	}
    244 	KASSERT((pg->flags & PG_FAKE) == 0);
    245 	if (pg->flags & PG_RELEASED) {
    246 		uvm_pagefree(pg);
    247 		return 0;
    248 	}
    249 	if (pg->loan_count != 0) {
    250 
    251 		/*
    252 		 * Avoid unneeded loan break, if possible.
    253 		 */
    254 
    255 		if ((access_type & VM_PROT_WRITE) == 0) {
    256 			prot &= ~VM_PROT_WRITE;
    257 		}
    258 		if (prot & VM_PROT_WRITE) {
    259 			struct vm_page *newpg;
    260 
    261 			newpg = uvm_loanbreak(pg);
    262 			if (newpg == NULL) {
    263 				uvm_page_unbusy(&pg, 1);
    264 				return ENOMEM;
    265 			}
    266 			pg = newpg;
    267 		}
    268 	}
    269 
    270 	/*
    271 	 * Note that a page whose backing store is partially allocated
    272 	 * is marked as PG_RDONLY.
    273 	 */
    274 
    275 	KASSERT((pg->flags & PG_RDONLY) == 0 ||
    276 	    (access_type & VM_PROT_WRITE) == 0 ||
    277 	    pg->offset < umap->writeoff ||
    278 	    pg->offset + PAGE_SIZE > umap->writeoff + umap->writelen);
    279 
    280 	rdonly = ((access_type & VM_PROT_WRITE) == 0 &&
    281 	    (pg->flags & PG_RDONLY) != 0) ||
    282 	    UVM_OBJ_NEEDS_WRITEFAULT(uobj);
    283 	mask = rdonly ? ~VM_PROT_WRITE : VM_PROT_ALL;
    284 
    285 	error = pmap_enter(ufi->orig_map->pmap, va, VM_PAGE_TO_PHYS(pg),
    286 	    prot & mask, PMAP_CANFAIL | (access_type & mask));
    287 
    288 	uvm_pagelock(pg);
    289 	uvm_pageactivate(pg);
    290 	uvm_pageunlock(pg);
    291 	pg->flags &= ~(PG_BUSY|PG_WANTED);
    292 	UVM_PAGE_OWN(pg, NULL);
    293 
    294 	return error;
    295 }
    296 
    297 /*
    298  * ubc_fault: fault routine for ubc mapping
    299  */
    300 
    301 static int
    302 ubc_fault(struct uvm_faultinfo *ufi, vaddr_t ign1, struct vm_page **ign2,
    303     int ign3, int ign4, vm_prot_t access_type, int flags)
    304 {
    305 	struct uvm_object *uobj;
    306 	struct ubc_map *umap;
    307 	vaddr_t va, eva, ubc_offset, slot_offset;
    308 	struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT];
    309 	int i, error, npages;
    310 	vm_prot_t prot;
    311 
    312 	UVMHIST_FUNC("ubc_fault"); UVMHIST_CALLED(ubchist);
    313 
    314 	/*
    315 	 * no need to try with PGO_LOCKED...
    316 	 * we don't need to have the map locked since we know that
    317 	 * no one will mess with it until our reference is released.
    318 	 */
    319 
    320 	if (flags & PGO_LOCKED) {
    321 		uvmfault_unlockall(ufi, NULL, &ubc_object.uobj);
    322 		flags &= ~PGO_LOCKED;
    323 	}
    324 
    325 	va = ufi->orig_rvaddr;
    326 	ubc_offset = va - (vaddr_t)ubc_object.kva;
    327 	umap = &ubc_object.umap[ubc_offset >> ubc_winshift];
    328 	KASSERT(umap->refcount != 0);
    329 	KASSERT((umap->flags & UMAP_PAGES_LOCKED) == 0);
    330 	slot_offset = ubc_offset & (ubc_winsize - 1);
    331 
    332 	/*
    333 	 * some platforms cannot write to individual bytes atomically, so
    334 	 * software has to do read/modify/write of larger quantities instead.
    335 	 * this means that the access_type for "write" operations
    336 	 * can be VM_PROT_READ, which confuses us mightily.
    337 	 *
    338 	 * deal with this by resetting access_type based on the info
    339 	 * that ubc_alloc() stores for us.
    340 	 */
    341 
    342 	access_type = umap->writelen ? VM_PROT_WRITE : VM_PROT_READ;
    343 	UVMHIST_LOG(ubchist, "va 0x%jx ubc_offset 0x%jx access_type %jd",
    344 	    va, ubc_offset, access_type, 0);
    345 
    346 	if ((access_type & VM_PROT_WRITE) != 0) {
    347 #ifndef PRIxOFF		/* XXX */
    348 #define PRIxOFF "jx"	/* XXX */
    349 #endif			/* XXX */
    350 		KASSERTMSG((trunc_page(umap->writeoff) <= slot_offset),
    351 		    "out of range write: slot=%#"PRIxVSIZE" off=%#"PRIxOFF,
    352 		    slot_offset, (intmax_t)umap->writeoff);
    353 		KASSERTMSG((slot_offset < umap->writeoff + umap->writelen),
    354 		    "out of range write: slot=%#"PRIxVADDR
    355 		        " off=%#"PRIxOFF" len=%#"PRIxVSIZE,
    356 		    slot_offset, (intmax_t)umap->writeoff, umap->writelen);
    357 	}
    358 
    359 	/* no umap locking needed since we have a ref on the umap */
    360 	uobj = umap->uobj;
    361 
    362 	if ((access_type & VM_PROT_WRITE) == 0) {
    363 		npages = (ubc_winsize - slot_offset) >> PAGE_SHIFT;
    364 	} else {
    365 		npages = (round_page(umap->offset + umap->writeoff +
    366 		    umap->writelen) - (umap->offset + slot_offset))
    367 		    >> PAGE_SHIFT;
    368 		flags |= PGO_PASTEOF;
    369 	}
    370 
    371 again:
    372 	memset(pgs, 0, sizeof (pgs));
    373 	mutex_enter(uobj->vmobjlock);
    374 
    375 	UVMHIST_LOG(ubchist, "slot_offset 0x%jx writeoff 0x%jx writelen 0x%jx ",
    376 	    slot_offset, umap->writeoff, umap->writelen, 0);
    377 	UVMHIST_LOG(ubchist, "getpages uobj %#jx offset 0x%jx npages %jd",
    378 	    (uintptr_t)uobj, umap->offset + slot_offset, npages, 0);
    379 
    380 	error = (*uobj->pgops->pgo_get)(uobj, umap->offset + slot_offset, pgs,
    381 	    &npages, 0, access_type, umap->advice, flags | PGO_NOBLOCKALLOC |
    382 	    PGO_NOTIMESTAMP);
    383 	UVMHIST_LOG(ubchist, "getpages error %jd npages %jd", error, npages, 0,
    384 	    0);
    385 
    386 	if (error == EAGAIN) {
    387 		kpause("ubc_fault", false, hz >> 2, NULL);
    388 		goto again;
    389 	}
    390 	if (error) {
    391 		return error;
    392 	}
    393 
    394 	/*
    395 	 * For virtually-indexed, virtually-tagged caches we should avoid
    396 	 * creating writable mappings when we do not absolutely need them,
    397 	 * since the "compatible alias" trick does not work on such caches.
    398 	 * Otherwise, we can always map the pages writable.
    399 	 */
    400 
    401 #ifdef PMAP_CACHE_VIVT
    402 	prot = VM_PROT_READ | access_type;
    403 #else
    404 	prot = VM_PROT_READ | VM_PROT_WRITE;
    405 #endif
    406 
    407 	va = ufi->orig_rvaddr;
    408 	eva = ufi->orig_rvaddr + (npages << PAGE_SHIFT);
    409 
    410 	UVMHIST_LOG(ubchist, "va 0x%jx eva 0x%jx", va, eva, 0, 0);
    411 
    412 	/*
    413 	 * Note: normally all returned pages would have the same UVM object.
    414 	 * However, layered file-systems and e.g. tmpfs, may return pages
    415 	 * which belong to underlying UVM object.  In such case, lock is
    416 	 * shared amongst the objects.
    417 	 */
    418 	mutex_enter(uobj->vmobjlock);
    419 	for (i = 0; va < eva; i++, va += PAGE_SIZE) {
    420 		struct vm_page *pg;
    421 
    422 		UVMHIST_LOG(ubchist, "pgs[%jd] = %#jx", i, (uintptr_t)pgs[i],
    423 		    0, 0);
    424 		pg = pgs[i];
    425 
    426 		if (pg == NULL || pg == PGO_DONTCARE) {
    427 			continue;
    428 		}
    429 		KASSERT(uobj->vmobjlock == pg->uobject->vmobjlock);
    430 		error = ubc_fault_page(ufi, umap, pg, prot, access_type, va);
    431 		if (error) {
    432 			/*
    433 			 * Flush (there might be pages entered), drop the lock,
    434 			 * and perform uvm_wait().  Note: page will re-fault.
    435 			 */
    436 			pmap_update(ufi->orig_map->pmap);
    437 			mutex_exit(uobj->vmobjlock);
    438 			uvm_wait("ubc_fault");
    439 			mutex_enter(uobj->vmobjlock);
    440 		}
    441 	}
    442 	/* Must make VA visible before the unlock. */
    443 	pmap_update(ufi->orig_map->pmap);
    444 	mutex_exit(uobj->vmobjlock);
    445 
    446 	return 0;
    447 }
    448 
    449 /*
    450  * local functions
    451  */
    452 
    453 static struct ubc_map *
    454 ubc_find_mapping(struct uvm_object *uobj, voff_t offset)
    455 {
    456 	struct ubc_map *umap;
    457 
    458 	LIST_FOREACH(umap, &ubc_object.hash[UBC_HASH(uobj, offset)], hash) {
    459 		if (umap->uobj == uobj && umap->offset == offset) {
    460 			return umap;
    461 		}
    462 	}
    463 	return NULL;
    464 }
    465 
    466 
    467 /*
    468  * ubc interface functions
    469  */
    470 
    471 /*
    472  * ubc_alloc:  allocate a file mapping window
    473  */
    474 
    475 static void * __noinline
    476 ubc_alloc(struct uvm_object *uobj, voff_t offset, vsize_t *lenp, int advice,
    477     int flags)
    478 {
    479 	vaddr_t slot_offset, va;
    480 	struct ubc_map *umap;
    481 	voff_t umap_offset;
    482 	int error;
    483 	UVMHIST_FUNC("ubc_alloc"); UVMHIST_CALLED(ubchist);
    484 
    485 	UVMHIST_LOG(ubchist, "uobj %#jx offset 0x%jx len 0x%jx",
    486 	    (uintptr_t)uobj, offset, *lenp, 0);
    487 
    488 	KASSERT(*lenp > 0);
    489 	umap_offset = (offset & ~((voff_t)ubc_winsize - 1));
    490 	slot_offset = (vaddr_t)(offset & ((voff_t)ubc_winsize - 1));
    491 	*lenp = MIN(*lenp, ubc_winsize - slot_offset);
    492 
    493 	mutex_enter(ubc_object.uobj.vmobjlock);
    494 again:
    495 	/*
    496 	 * The UVM object is already referenced.
    497 	 * Lock order: UBC object -> ubc_map::uobj.
    498 	 */
    499 	umap = ubc_find_mapping(uobj, umap_offset);
    500 	if (umap == NULL) {
    501 		struct uvm_object *oobj;
    502 
    503 		UBC_EVCNT_INCR(wincachemiss);
    504 		umap = TAILQ_FIRST(UBC_QUEUE(offset));
    505 		if (umap == NULL) {
    506 			kpause("ubc_alloc", false, hz >> 2,
    507 			    ubc_object.uobj.vmobjlock);
    508 			goto again;
    509 		}
    510 
    511 		va = UBC_UMAP_ADDR(umap);
    512 		oobj = umap->uobj;
    513 
    514 		/*
    515 		 * Remove from old hash (if any), add to new hash.
    516 		 */
    517 
    518 		if (oobj != NULL) {
    519 			/*
    520 			 * Mapping must be removed before the list entry,
    521 			 * since there is a race with ubc_purge().
    522 			 */
    523 			if (umap->flags & UMAP_MAPPING_CACHED) {
    524 				umap->flags &= ~UMAP_MAPPING_CACHED;
    525 				mutex_enter(oobj->vmobjlock);
    526 				pmap_remove(pmap_kernel(), va,
    527 				    va + ubc_winsize);
    528 				pmap_update(pmap_kernel());
    529 				mutex_exit(oobj->vmobjlock);
    530 			}
    531 			LIST_REMOVE(umap, hash);
    532 			LIST_REMOVE(umap, list);
    533 		} else {
    534 			KASSERT((umap->flags & UMAP_MAPPING_CACHED) == 0);
    535 		}
    536 		umap->uobj = uobj;
    537 		umap->offset = umap_offset;
    538 		LIST_INSERT_HEAD(&ubc_object.hash[UBC_HASH(uobj, umap_offset)],
    539 		    umap, hash);
    540 		LIST_INSERT_HEAD(&uobj->uo_ubc, umap, list);
    541 	} else {
    542 		UBC_EVCNT_INCR(wincachehit);
    543 		va = UBC_UMAP_ADDR(umap);
    544 	}
    545 
    546 	if (umap->refcount == 0) {
    547 		TAILQ_REMOVE(UBC_QUEUE(offset), umap, inactive);
    548 	}
    549 
    550 	if (flags & UBC_WRITE) {
    551 		KASSERTMSG(umap->writeoff == 0 && umap->writelen == 0,
    552 		    "ubc_alloc: concurrent writes to uobj %p", uobj);
    553 		umap->writeoff = slot_offset;
    554 		umap->writelen = *lenp;
    555 	}
    556 
    557 	umap->refcount++;
    558 	umap->advice = advice;
    559 	mutex_exit(ubc_object.uobj.vmobjlock);
    560 	UVMHIST_LOG(ubchist, "umap %#jx refs %jd va %#jx flags 0x%jx",
    561 	    (uintptr_t)umap, umap->refcount, (uintptr_t)va, flags);
    562 
    563 	if (flags & UBC_FAULTBUSY) {
    564 		// XXX add offset from slot_offset?
    565 		int npages = (*lenp + PAGE_SIZE - 1) >> PAGE_SHIFT;
    566 		struct vm_page *pgs[npages];
    567 		int gpflags =
    568 		    PGO_SYNCIO|PGO_OVERWRITE|PGO_PASTEOF|PGO_NOBLOCKALLOC|
    569 		    PGO_NOTIMESTAMP;
    570 		int i;
    571 		KDASSERT(flags & UBC_WRITE);
    572 		KASSERT(umap->refcount == 1);
    573 
    574 		UBC_EVCNT_INCR(faultbusy);
    575 again_faultbusy:
    576 		mutex_enter(uobj->vmobjlock);
    577 		if (umap->flags & UMAP_MAPPING_CACHED) {
    578 			umap->flags &= ~UMAP_MAPPING_CACHED;
    579 			pmap_remove(pmap_kernel(), va, va + ubc_winsize);
    580 		}
    581 		memset(pgs, 0, sizeof(pgs));
    582 
    583 		error = (*uobj->pgops->pgo_get)(uobj, trunc_page(offset), pgs,
    584 		    &npages, 0, VM_PROT_READ | VM_PROT_WRITE, advice, gpflags);
    585 		UVMHIST_LOG(ubchist, "faultbusy getpages %jd", error, 0, 0, 0);
    586 		if (error) {
    587 			/*
    588 			 * Flush: the mapping above might have been removed.
    589 			 */
    590 			pmap_update(pmap_kernel());
    591 			goto out;
    592 		}
    593 		for (i = 0; i < npages; i++) {
    594 			struct vm_page *pg = pgs[i];
    595 
    596 			KASSERT(pg->uobject == uobj);
    597 			if (pg->loan_count != 0) {
    598 				mutex_enter(uobj->vmobjlock);
    599 				if (pg->loan_count != 0) {
    600 					pg = uvm_loanbreak(pg);
    601 				}
    602 				if (pg == NULL) {
    603 					pmap_kremove(va, ubc_winsize);
    604 					pmap_update(pmap_kernel());
    605 					uvm_page_unbusy(pgs, npages);
    606 					mutex_exit(uobj->vmobjlock);
    607 					uvm_wait("ubc_alloc");
    608 					goto again_faultbusy;
    609 				}
    610 				mutex_exit(uobj->vmobjlock);
    611 				pgs[i] = pg;
    612 			}
    613 			pmap_kenter_pa(va + slot_offset + (i << PAGE_SHIFT),
    614 			    VM_PAGE_TO_PHYS(pg),
    615 			    VM_PROT_READ | VM_PROT_WRITE, 0);
    616 		}
    617 		pmap_update(pmap_kernel());
    618 		umap->flags |= UMAP_PAGES_LOCKED;
    619 	} else {
    620 		KASSERT((umap->flags & UMAP_PAGES_LOCKED) == 0);
    621 	}
    622 
    623 out:
    624 	return (void *)(va + slot_offset);
    625 }
    626 
    627 /*
    628  * ubc_release:  free a file mapping window.
    629  */
    630 
    631 static void __noinline
    632 ubc_release(void *va, int flags)
    633 {
    634 	struct ubc_map *umap;
    635 	struct uvm_object *uobj;
    636 	vaddr_t umapva;
    637 	bool unmapped;
    638 	UVMHIST_FUNC("ubc_release"); UVMHIST_CALLED(ubchist);
    639 
    640 	UVMHIST_LOG(ubchist, "va %#jx", (uintptr_t)va, 0, 0, 0);
    641 	umap = &ubc_object.umap[((char *)va - ubc_object.kva) >> ubc_winshift];
    642 	umapva = UBC_UMAP_ADDR(umap);
    643 	uobj = umap->uobj;
    644 	KASSERT(uobj != NULL);
    645 
    646 	if (umap->flags & UMAP_PAGES_LOCKED) {
    647 		const voff_t slot_offset = umap->writeoff;
    648 		const voff_t endoff = umap->writeoff + umap->writelen;
    649 		const voff_t zerolen = round_page(endoff) - endoff;
    650 		const u_int npages = (round_page(endoff) -
    651 		    trunc_page(slot_offset)) >> PAGE_SHIFT;
    652 		struct vm_page *pgs[npages];
    653 
    654 		KASSERT((umap->flags & UMAP_MAPPING_CACHED) == 0);
    655 		if (zerolen) {
    656 			memset((char *)umapva + endoff, 0, zerolen);
    657 		}
    658 		umap->flags &= ~UMAP_PAGES_LOCKED;
    659 		mutex_enter(uobj->vmobjlock);
    660 		for (u_int i = 0; i < npages; i++) {
    661 			paddr_t pa;
    662 			bool rv __diagused;
    663 
    664 			rv = pmap_extract(pmap_kernel(),
    665 			    umapva + slot_offset + (i << PAGE_SHIFT), &pa);
    666 			KASSERT(rv);
    667 			pgs[i] = PHYS_TO_VM_PAGE(pa);
    668 			pgs[i]->flags &= ~(PG_FAKE|PG_CLEAN);
    669 			KASSERT(pgs[i]->loan_count == 0);
    670 			uvm_pagelock(pgs[i]);
    671 			uvm_pageactivate(pgs[i]);
    672 			uvm_pageunlock(pgs[i]);
    673 		}
    674 		pmap_kremove(umapva, ubc_winsize);
    675 		pmap_update(pmap_kernel());
    676 		uvm_page_unbusy(pgs, npages);
    677 		mutex_exit(uobj->vmobjlock);
    678 		unmapped = true;
    679 	} else {
    680 		unmapped = false;
    681 	}
    682 
    683 	mutex_enter(ubc_object.uobj.vmobjlock);
    684 	umap->writeoff = 0;
    685 	umap->writelen = 0;
    686 	umap->refcount--;
    687 	if (umap->refcount == 0) {
    688 		if (flags & UBC_UNMAP) {
    689 			/*
    690 			 * Invalidate any cached mappings if requested.
    691 			 * This is typically used to avoid leaving
    692 			 * incompatible cache aliases around indefinitely.
    693 			 */
    694 			mutex_enter(uobj->vmobjlock);
    695 			pmap_remove(pmap_kernel(), umapva,
    696 				    umapva + ubc_winsize);
    697 			pmap_update(pmap_kernel());
    698 			mutex_exit(uobj->vmobjlock);
    699 
    700 			umap->flags &= ~UMAP_MAPPING_CACHED;
    701 			LIST_REMOVE(umap, hash);
    702 			LIST_REMOVE(umap, list);
    703 			umap->uobj = NULL;
    704 			TAILQ_INSERT_HEAD(UBC_QUEUE(umap->offset), umap,
    705 			    inactive);
    706 		} else {
    707 			if (!unmapped) {
    708 				umap->flags |= UMAP_MAPPING_CACHED;
    709 			}
    710 			TAILQ_INSERT_TAIL(UBC_QUEUE(umap->offset), umap,
    711 			    inactive);
    712 		}
    713 	}
    714 	UVMHIST_LOG(ubchist, "umap %#jx refs %jd", (uintptr_t)umap,
    715 	    umap->refcount, 0, 0);
    716 	mutex_exit(ubc_object.uobj.vmobjlock);
    717 }
    718 
    719 /*
    720  * ubc_uiomove: move data to/from an object.
    721  */
    722 
    723 int
    724 ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo, int advice,
    725     int flags)
    726 {
    727 	const bool overwrite = (flags & UBC_FAULTBUSY) != 0;
    728 	voff_t off;
    729 	int error;
    730 
    731 	KASSERT(todo <= uio->uio_resid);
    732 	KASSERT(((flags & UBC_WRITE) != 0 && uio->uio_rw == UIO_WRITE) ||
    733 	    ((flags & UBC_READ) != 0 && uio->uio_rw == UIO_READ));
    734 
    735 #ifdef UBC_USE_PMAP_DIRECT
    736 	if (ubc_direct) {
    737 		return ubc_uiomove_direct(uobj, uio, todo, advice, flags);
    738 	}
    739 #endif
    740 
    741 	off = uio->uio_offset;
    742 	error = 0;
    743 	while (todo > 0) {
    744 		vsize_t bytelen = todo;
    745 		void *win;
    746 
    747 		win = ubc_alloc(uobj, off, &bytelen, advice, flags);
    748 		if (error == 0) {
    749 			error = uiomove(win, bytelen, uio);
    750 		}
    751 		if (error != 0 && overwrite) {
    752 			/*
    753 			 * if we haven't initialized the pages yet,
    754 			 * do it now.  it's safe to use memset here
    755 			 * because we just mapped the pages above.
    756 			 */
    757 			printf("%s: error=%d\n", __func__, error);
    758 			memset(win, 0, bytelen);
    759 		}
    760 		ubc_release(win, flags);
    761 		off += bytelen;
    762 		todo -= bytelen;
    763 		if (error != 0 && (flags & UBC_PARTIALOK) != 0) {
    764 			break;
    765 		}
    766 	}
    767 
    768 	return error;
    769 }
    770 
    771 /*
    772  * ubc_zerorange: set a range of bytes in an object to zero.
    773  */
    774 
    775 void
    776 ubc_zerorange(struct uvm_object *uobj, off_t off, size_t len, int flags)
    777 {
    778 
    779 #ifdef UBC_USE_PMAP_DIRECT
    780 	if (ubc_direct) {
    781 		ubc_zerorange_direct(uobj, off, len, flags);
    782 		return;
    783 	}
    784 #endif
    785 
    786 	/*
    787 	 * XXXUBC invent kzero() and use it
    788 	 */
    789 
    790 	while (len) {
    791 		void *win;
    792 		vsize_t bytelen = len;
    793 
    794 		win = ubc_alloc(uobj, off, &bytelen, UVM_ADV_NORMAL, UBC_WRITE);
    795 		memset(win, 0, bytelen);
    796 		ubc_release(win, flags);
    797 
    798 		off += bytelen;
    799 		len -= bytelen;
    800 	}
    801 }
    802 
    803 #ifdef UBC_USE_PMAP_DIRECT
    804 /* Copy data using direct map */
    805 
    806 /*
    807  * ubc_alloc_direct:  allocate a file mapping window using direct map
    808  */
    809 static int __noinline
    810 ubc_alloc_direct(struct uvm_object *uobj, voff_t offset, vsize_t *lenp,
    811     int advice, int flags, struct vm_page **pgs, int *npages)
    812 {
    813 	voff_t pgoff;
    814 	int error;
    815 	int gpflags = flags | PGO_NOTIMESTAMP | PGO_SYNCIO | PGO_ALLPAGES;
    816 	int access_type = VM_PROT_READ;
    817 	UVMHIST_FUNC("ubc_alloc_direct"); UVMHIST_CALLED(ubchist);
    818 
    819 	if (flags & UBC_WRITE) {
    820 		if (flags & UBC_FAULTBUSY)
    821 			gpflags |= PGO_OVERWRITE;
    822 #if 0
    823 		KASSERT(!UVM_OBJ_NEEDS_WRITEFAULT(uobj));
    824 #endif
    825 
    826 		/*
    827 		 * Tell genfs_getpages() we already have the journal lock,
    828 		 * allow allocation past current EOF.
    829 		 */
    830 		gpflags |= PGO_JOURNALLOCKED | PGO_PASTEOF;
    831 		access_type |= VM_PROT_WRITE;
    832 	} else {
    833 		/* Don't need the empty blocks allocated, PG_RDONLY is okay */
    834 		gpflags |= PGO_NOBLOCKALLOC;
    835 	}
    836 
    837 	pgoff = (offset & PAGE_MASK);
    838 	*lenp = MIN(*lenp, ubc_winsize - pgoff);
    839 
    840 again:
    841 	*npages = (*lenp + pgoff + PAGE_SIZE - 1) >> PAGE_SHIFT;
    842 	KASSERT((*npages * PAGE_SIZE) <= ubc_winsize);
    843 	KASSERT(*lenp + pgoff <= ubc_winsize);
    844 	memset(pgs, 0, *npages * sizeof(pgs[0]));
    845 
    846 	mutex_enter(uobj->vmobjlock);
    847 	error = (*uobj->pgops->pgo_get)(uobj, trunc_page(offset), pgs,
    848 	    npages, 0, access_type, advice, gpflags);
    849 	UVMHIST_LOG(ubchist, "alloc_direct getpages %jd", error, 0, 0, 0);
    850 	if (error) {
    851 		if (error == EAGAIN) {
    852 			kpause("ubc_alloc_directg", false, hz >> 2, NULL);
    853 			goto again;
    854 		}
    855 		return error;
    856 	}
    857 
    858 	mutex_enter(uobj->vmobjlock);
    859 	for (int i = 0; i < *npages; i++) {
    860 		struct vm_page *pg = pgs[i];
    861 
    862 		KASSERT(pg != NULL);
    863 		KASSERT(pg != PGO_DONTCARE);
    864 		KASSERT((pg->flags & PG_FAKE) == 0 || (gpflags & PGO_OVERWRITE));
    865 		KASSERT(pg->uobject->vmobjlock == uobj->vmobjlock);
    866 
    867 		/* Avoid breaking loan if possible, only do it on write */
    868 		if ((flags & UBC_WRITE) && pg->loan_count != 0) {
    869 			pg = uvm_loanbreak(pg);
    870 			if (pg == NULL) {
    871 				uvm_page_unbusy(pgs, *npages);
    872 				mutex_exit(uobj->vmobjlock);
    873 				uvm_wait("ubc_alloc_directl");
    874 				goto again;
    875 			}
    876 			pgs[i] = pg;
    877 		}
    878 
    879 		/* Page must be writable by now */
    880 		KASSERT((pg->flags & PG_RDONLY) == 0 || (flags & UBC_WRITE) == 0);
    881 	}
    882 	mutex_exit(uobj->vmobjlock);
    883 
    884 	return 0;
    885 }
    886 
    887 static void __noinline
    888 ubc_direct_release(struct uvm_object *uobj,
    889 	int flags, struct vm_page **pgs, int npages)
    890 {
    891 	mutex_enter(uobj->vmobjlock);
    892 	for (int i = 0; i < npages; i++) {
    893 		struct vm_page *pg = pgs[i];
    894 
    895 		uvm_pagelock(pg);
    896 		uvm_pageactivate(pg);
    897 		uvm_pageunlock(pg);
    898 
    899 		/* Page was changed, no longer fake and neither clean */
    900 		if (flags & UBC_WRITE)
    901 			pg->flags &= ~(PG_FAKE|PG_CLEAN);
    902 	}
    903 	uvm_page_unbusy(pgs, npages);
    904 	mutex_exit(uobj->vmobjlock);
    905 }
    906 
    907 static int
    908 ubc_uiomove_process(void *win, size_t len, void *arg)
    909 {
    910 	struct uio *uio = (struct uio *)arg;
    911 
    912 	return uiomove(win, len, uio);
    913 }
    914 
    915 static int
    916 ubc_zerorange_process(void *win, size_t len, void *arg)
    917 {
    918 	memset(win, 0, len);
    919 	return 0;
    920 }
    921 
    922 static int __noinline
    923 ubc_uiomove_direct(struct uvm_object *uobj, struct uio *uio, vsize_t todo, int advice,
    924     int flags)
    925 {
    926 	const bool overwrite = (flags & UBC_FAULTBUSY) != 0;
    927 	voff_t off;
    928 	int error, npages;
    929 	struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT];
    930 
    931 	KASSERT(todo <= uio->uio_resid);
    932 	KASSERT(((flags & UBC_WRITE) != 0 && uio->uio_rw == UIO_WRITE) ||
    933 	    ((flags & UBC_READ) != 0 && uio->uio_rw == UIO_READ));
    934 
    935 	off = uio->uio_offset;
    936 	error = 0;
    937 	while (todo > 0) {
    938 		vsize_t bytelen = todo;
    939 
    940 		error = ubc_alloc_direct(uobj, off, &bytelen, advice, flags,
    941 		    pgs, &npages);
    942 		if (error != 0) {
    943 			/* can't do anything, failed to get the pages */
    944 			break;
    945 		}
    946 
    947 		if (error == 0) {
    948 			error = uvm_direct_process(pgs, npages, off, bytelen,
    949 			    ubc_uiomove_process, uio);
    950 		}
    951 		if (error != 0 && overwrite) {
    952 			/*
    953 			 * if we haven't initialized the pages yet,
    954 			 * do it now.  it's safe to use memset here
    955 			 * because we just mapped the pages above.
    956 			 */
    957 			printf("%s: error=%d\n", __func__, error);
    958 			(void) uvm_direct_process(pgs, npages, off, bytelen,
    959 			    ubc_zerorange_process, NULL);
    960 		}
    961 
    962 		ubc_direct_release(uobj, flags, pgs, npages);
    963 
    964 		off += bytelen;
    965 		todo -= bytelen;
    966 
    967 		if (error != 0 && ISSET(flags, UBC_PARTIALOK)) {
    968 			break;
    969 		}
    970 	}
    971 
    972 	return error;
    973 }
    974 
    975 static void __noinline
    976 ubc_zerorange_direct(struct uvm_object *uobj, off_t off, size_t todo, int flags)
    977 {
    978 	int error, npages;
    979 	struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT];
    980 
    981 	flags |= UBC_WRITE;
    982 
    983 	error = 0;
    984 	while (todo > 0) {
    985 		vsize_t bytelen = todo;
    986 
    987 		error = ubc_alloc_direct(uobj, off, &bytelen, UVM_ADV_NORMAL,
    988 		    flags, pgs, &npages);
    989 		if (error != 0) {
    990 			/* can't do anything, failed to get the pages */
    991 			break;
    992 		}
    993 
    994 		error = uvm_direct_process(pgs, npages, off, bytelen,
    995 		    ubc_zerorange_process, NULL);
    996 
    997 		ubc_direct_release(uobj, flags, pgs, npages);
    998 
    999 		off += bytelen;
   1000 		todo -= bytelen;
   1001 	}
   1002 }
   1003 
   1004 #endif /* UBC_USE_PMAP_DIRECT */
   1005 
   1006 /*
   1007  * ubc_purge: disassociate ubc_map structures from an empty uvm_object.
   1008  */
   1009 
   1010 void
   1011 ubc_purge(struct uvm_object *uobj)
   1012 {
   1013 	struct ubc_map *umap;
   1014 	vaddr_t va;
   1015 
   1016 	KASSERT(uobj->uo_npages == 0);
   1017 
   1018 	/*
   1019 	 * Safe to check without lock held, as ubc_alloc() removes
   1020 	 * the mapping and list entry in the correct order.
   1021 	 */
   1022 	if (__predict_true(LIST_EMPTY(&uobj->uo_ubc))) {
   1023 		return;
   1024 	}
   1025 	mutex_enter(ubc_object.uobj.vmobjlock);
   1026 	while ((umap = LIST_FIRST(&uobj->uo_ubc)) != NULL) {
   1027 		KASSERT(umap->refcount == 0);
   1028 		for (va = 0; va < ubc_winsize; va += PAGE_SIZE) {
   1029 			KASSERT(!pmap_extract(pmap_kernel(),
   1030 			    va + UBC_UMAP_ADDR(umap), NULL));
   1031 		}
   1032 		LIST_REMOVE(umap, list);
   1033 		LIST_REMOVE(umap, hash);
   1034 		umap->flags &= ~UMAP_MAPPING_CACHED;
   1035 		umap->uobj = NULL;
   1036 	}
   1037 	mutex_exit(ubc_object.uobj.vmobjlock);
   1038 }
   1039