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