1 /*- 2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU) 3 * 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * The Mach Operating System project at Carnegie-Mellon University. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * from: @(#)vm_pager.c 8.6 (Berkeley) 1/12/94 35 * 36 * 37 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 38 * All rights reserved. 39 * 40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 41 * 42 * Permission to use, copy, modify and distribute this software and 43 * its documentation is hereby granted, provided that both the copyright 44 * notice and this permission notice appear in all copies of the 45 * software, derivative works or modified versions, and any portions 46 * thereof, and that both notices appear in supporting documentation. 47 * 48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51 * 52 * Carnegie Mellon requests users of this software to return to 53 * 54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55 * School of Computer Science 56 * Carnegie Mellon University 57 * Pittsburgh PA 15213-3890 58 * 59 * any improvements or extensions that they make and grant Carnegie the 60 * rights to redistribute these changes. 61 */ 62 63 /* 64 * Paging space routine stubs. Emulates a matchmaker-like interface 65 * for builtin pagers. 66 */ 67 68 #include <sys/cdefs.h> 69 __FBSDID("$FreeBSD$"); 70 71 #include "opt_param.h" 72 73 #include <sys/param.h> 74 #include <sys/systm.h> 75 #include <sys/kernel.h> 76 #include <sys/vnode.h> 77 #include <sys/bio.h> 78 #include <sys/buf.h> 79 #include <sys/ucred.h> 80 #include <sys/malloc.h> 81 #include <sys/rwlock.h> 82 #include <sys/user.h> 83 84 #include <vm/vm.h> 85 #include <vm/vm_param.h> 86 #include <vm/vm_kern.h> 87 #include <vm/vm_object.h> 88 #include <vm/vm_page.h> 89 #include <vm/vm_pager.h> 90 #include <vm/vm_extern.h> 91 #include <vm/uma.h> 92 93 uma_zone_t pbuf_zone; 94 static int pbuf_init(void *, int, int); 95 static int pbuf_ctor(void *, int, void *, int); 96 static void pbuf_dtor(void *, int, void *); 97 98 static int dead_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *); 99 static vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 100 vm_ooffset_t, struct ucred *); 101 static void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *); 102 static boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 103 static void dead_pager_dealloc(vm_object_t); 104 static void dead_pager_getvp(vm_object_t, struct vnode **, bool *); 105 106 static int 107 dead_pager_getpages(vm_object_t obj, vm_page_t *ma, int count, int *rbehind, 108 int *rahead) 109 { 110 111 return (VM_PAGER_FAIL); 112 } 113 114 static vm_object_t 115 dead_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 116 vm_ooffset_t off, struct ucred *cred) 117 { 118 119 return (NULL); 120 } 121 122 static void 123 dead_pager_putpages(vm_object_t object, vm_page_t *m, int count, 124 int flags, int *rtvals) 125 { 126 int i; 127 128 for (i = 0; i < count; i++) 129 rtvals[i] = VM_PAGER_AGAIN; 130 } 131 132 static int 133 dead_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *prev, int *next) 134 { 135 136 if (prev != NULL) 137 *prev = 0; 138 if (next != NULL) 139 *next = 0; 140 return (FALSE); 141 } 142 143 static void 144 dead_pager_dealloc(vm_object_t object) 145 { 146 147 } 148 149 static void 150 dead_pager_getvp(vm_object_t object, struct vnode **vpp, bool *vp_heldp) 151 { 152 /* 153 * For OBJT_DEAD objects, v_writecount was handled in 154 * vnode_pager_dealloc(). 155 */ 156 } 157 158 static const struct pagerops deadpagerops = { 159 .pgo_kvme_type = KVME_TYPE_DEAD, 160 .pgo_alloc = dead_pager_alloc, 161 .pgo_dealloc = dead_pager_dealloc, 162 .pgo_getpages = dead_pager_getpages, 163 .pgo_putpages = dead_pager_putpages, 164 .pgo_haspage = dead_pager_haspage, 165 .pgo_getvp = dead_pager_getvp, 166 }; 167 168 const struct pagerops *pagertab[16] __read_mostly = { 169 [OBJT_SWAP] = &swappagerops, 170 [OBJT_VNODE] = &vnodepagerops, 171 [OBJT_DEVICE] = &devicepagerops, 172 [OBJT_PHYS] = &physpagerops, 173 [OBJT_DEAD] = &deadpagerops, 174 [OBJT_SG] = &sgpagerops, 175 [OBJT_MGTDEVICE] = &mgtdevicepagerops, 176 }; 177 static struct mtx pagertab_lock; 178 179 void 180 vm_pager_init(void) 181 { 182 const struct pagerops **pgops; 183 int i; 184 185 mtx_init(&pagertab_lock, "dynpag", NULL, MTX_DEF); 186 187 /* 188 * Initialize known pagers 189 */ 190 for (i = 0; i < OBJT_FIRST_DYN; i++) { 191 pgops = &pagertab[i]; 192 if (*pgops != NULL && (*pgops)->pgo_init != NULL) 193 (*(*pgops)->pgo_init)(); 194 } 195 } 196 197 static int nswbuf_max; 198 199 void 200 vm_pager_bufferinit(void) 201 { 202 203 /* Main zone for paging bufs. */ 204 pbuf_zone = uma_zcreate("pbuf", 205 sizeof(struct buf) + PBUF_PAGES * sizeof(vm_page_t), 206 pbuf_ctor, pbuf_dtor, pbuf_init, NULL, UMA_ALIGN_CACHE, 207 UMA_ZONE_NOFREE); 208 /* Few systems may still use this zone directly, so it needs a limit. */ 209 nswbuf_max += uma_zone_set_max(pbuf_zone, NSWBUF_MIN); 210 } 211 212 uma_zone_t 213 pbuf_zsecond_create(const char *name, int max) 214 { 215 uma_zone_t zone; 216 217 zone = uma_zsecond_create(name, pbuf_ctor, pbuf_dtor, NULL, NULL, 218 pbuf_zone); 219 220 #ifdef KMSAN 221 /* 222 * Shrink the size of the pbuf pools if KMSAN is enabled, otherwise the 223 * shadows of the large KVA allocations eat up too much memory. 224 */ 225 max /= 3; 226 #endif 227 228 /* 229 * uma_prealloc() rounds up to items per slab. If we would prealloc 230 * immediately on every pbuf_zsecond_create(), we may accumulate too 231 * much of difference between hard limit and prealloced items, which 232 * means wasted memory. 233 */ 234 if (nswbuf_max > 0) 235 nswbuf_max += uma_zone_set_max(zone, max); 236 else 237 uma_prealloc(pbuf_zone, uma_zone_set_max(zone, max)); 238 239 return (zone); 240 } 241 242 static void 243 pbuf_prealloc(void *arg __unused) 244 { 245 246 uma_prealloc(pbuf_zone, nswbuf_max); 247 nswbuf_max = -1; 248 } 249 250 SYSINIT(pbuf, SI_SUB_KTHREAD_BUF, SI_ORDER_ANY, pbuf_prealloc, NULL); 251 252 /* 253 * Allocate an instance of a pager of the given type. 254 * Size, protection and offset parameters are passed in for pagers that 255 * need to perform page-level validation (e.g. the device pager). 256 */ 257 vm_object_t 258 vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size, 259 vm_prot_t prot, vm_ooffset_t off, struct ucred *cred) 260 { 261 MPASS(type < nitems(pagertab)); 262 263 return ((*pagertab[type]->pgo_alloc)(handle, size, prot, off, cred)); 264 } 265 266 /* 267 * The object must be locked. 268 */ 269 void 270 vm_pager_deallocate(vm_object_t object) 271 { 272 273 VM_OBJECT_ASSERT_WLOCKED(object); 274 MPASS(object->type < nitems(pagertab)); 275 (*pagertab[object->type]->pgo_dealloc) (object); 276 } 277 278 static void 279 vm_pager_assert_in(vm_object_t object, vm_page_t *m, int count) 280 { 281 #ifdef INVARIANTS 282 283 /* 284 * All pages must be consecutive, busied, not mapped, not fully valid, 285 * not dirty and belong to the proper object. Some pages may be the 286 * bogus page, but the first and last pages must be a real ones. 287 */ 288 289 VM_OBJECT_ASSERT_UNLOCKED(object); 290 VM_OBJECT_ASSERT_PAGING(object); 291 KASSERT(count > 0, ("%s: 0 count", __func__)); 292 for (int i = 0 ; i < count; i++) { 293 if (m[i] == bogus_page) { 294 KASSERT(i != 0 && i != count - 1, 295 ("%s: page %d is the bogus page", __func__, i)); 296 continue; 297 } 298 vm_page_assert_xbusied(m[i]); 299 KASSERT(!pmap_page_is_mapped(m[i]), 300 ("%s: page %p is mapped", __func__, m[i])); 301 KASSERT(m[i]->valid != VM_PAGE_BITS_ALL, 302 ("%s: request for a valid page %p", __func__, m[i])); 303 KASSERT(m[i]->dirty == 0, 304 ("%s: page %p is dirty", __func__, m[i])); 305 KASSERT(m[i]->object == object, 306 ("%s: wrong object %p/%p", __func__, object, m[i]->object)); 307 KASSERT(m[i]->pindex == m[0]->pindex + i, 308 ("%s: page %p isn't consecutive", __func__, m[i])); 309 } 310 #endif 311 } 312 313 /* 314 * Page in the pages for the object using its associated pager. 315 * The requested page must be fully valid on successful return. 316 */ 317 int 318 vm_pager_get_pages(vm_object_t object, vm_page_t *m, int count, int *rbehind, 319 int *rahead) 320 { 321 #ifdef INVARIANTS 322 vm_pindex_t pindex = m[0]->pindex; 323 #endif 324 int r; 325 326 MPASS(object->type < nitems(pagertab)); 327 vm_pager_assert_in(object, m, count); 328 329 r = (*pagertab[object->type]->pgo_getpages)(object, m, count, rbehind, 330 rahead); 331 if (r != VM_PAGER_OK) 332 return (r); 333 334 for (int i = 0; i < count; i++) { 335 /* 336 * If pager has replaced a page, assert that it had 337 * updated the array. 338 */ 339 #ifdef INVARIANTS 340 KASSERT(m[i] == vm_page_relookup(object, pindex++), 341 ("%s: mismatch page %p pindex %ju", __func__, 342 m[i], (uintmax_t )pindex - 1)); 343 #endif 344 345 /* 346 * Zero out partially filled data. 347 */ 348 if (m[i]->valid != VM_PAGE_BITS_ALL) 349 vm_page_zero_invalid(m[i], TRUE); 350 } 351 return (VM_PAGER_OK); 352 } 353 354 int 355 vm_pager_get_pages_async(vm_object_t object, vm_page_t *m, int count, 356 int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg) 357 { 358 359 MPASS(object->type < nitems(pagertab)); 360 vm_pager_assert_in(object, m, count); 361 362 return ((*pagertab[object->type]->pgo_getpages_async)(object, m, 363 count, rbehind, rahead, iodone, arg)); 364 } 365 366 /* 367 * vm_pager_put_pages() - inline, see vm/vm_pager.h 368 * vm_pager_has_page() - inline, see vm/vm_pager.h 369 */ 370 371 /* 372 * Search the specified pager object list for an object with the 373 * specified handle. If an object with the specified handle is found, 374 * increase its reference count and return it. Otherwise, return NULL. 375 * 376 * The pager object list must be locked. 377 */ 378 vm_object_t 379 vm_pager_object_lookup(struct pagerlst *pg_list, void *handle) 380 { 381 vm_object_t object; 382 383 TAILQ_FOREACH(object, pg_list, pager_object_list) { 384 if (object->handle == handle) { 385 VM_OBJECT_WLOCK(object); 386 if ((object->flags & OBJ_DEAD) == 0) { 387 vm_object_reference_locked(object); 388 VM_OBJECT_WUNLOCK(object); 389 break; 390 } 391 VM_OBJECT_WUNLOCK(object); 392 } 393 } 394 return (object); 395 } 396 397 int 398 vm_pager_alloc_dyn_type(struct pagerops *ops, int base_type) 399 { 400 int res; 401 402 mtx_lock(&pagertab_lock); 403 MPASS(base_type == -1 || 404 (base_type >= OBJT_SWAP && base_type < nitems(pagertab))); 405 for (res = OBJT_FIRST_DYN; res < nitems(pagertab); res++) { 406 if (pagertab[res] == NULL) 407 break; 408 } 409 if (res == nitems(pagertab)) { 410 mtx_unlock(&pagertab_lock); 411 return (-1); 412 } 413 if (base_type != -1) { 414 MPASS(pagertab[base_type] != NULL); 415 #define FIX(n) \ 416 if (ops->pgo_##n == NULL) \ 417 ops->pgo_##n = pagertab[base_type]->pgo_##n 418 FIX(init); 419 FIX(alloc); 420 FIX(dealloc); 421 FIX(getpages); 422 FIX(getpages_async); 423 FIX(putpages); 424 FIX(haspage); 425 FIX(populate); 426 FIX(pageunswapped); 427 FIX(update_writecount); 428 FIX(release_writecount); 429 FIX(set_writeable_dirty); 430 FIX(mightbedirty); 431 FIX(getvp); 432 FIX(freespace); 433 #undef FIX 434 } 435 pagertab[res] = ops; /* XXXKIB should be rel, but acq is too much */ 436 mtx_unlock(&pagertab_lock); 437 return (res); 438 } 439 440 void 441 vm_pager_free_dyn_type(objtype_t type) 442 { 443 MPASS(type >= OBJT_FIRST_DYN && type < nitems(pagertab)); 444 445 mtx_lock(&pagertab_lock); 446 MPASS(pagertab[type] != NULL); 447 pagertab[type] = NULL; 448 mtx_unlock(&pagertab_lock); 449 } 450 451 static int 452 pbuf_ctor(void *mem, int size, void *arg, int flags) 453 { 454 struct buf *bp = mem; 455 456 bp->b_vp = NULL; 457 bp->b_bufobj = NULL; 458 459 /* copied from initpbuf() */ 460 bp->b_rcred = NOCRED; 461 bp->b_wcred = NOCRED; 462 bp->b_qindex = 0; /* On no queue (QUEUE_NONE) */ 463 bp->b_data = bp->b_kvabase; 464 bp->b_xflags = 0; 465 bp->b_flags = B_MAXPHYS; 466 bp->b_ioflags = 0; 467 bp->b_iodone = NULL; 468 bp->b_error = 0; 469 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL); 470 471 return (0); 472 } 473 474 static void 475 pbuf_dtor(void *mem, int size, void *arg) 476 { 477 struct buf *bp = mem; 478 479 if (bp->b_rcred != NOCRED) { 480 crfree(bp->b_rcred); 481 bp->b_rcred = NOCRED; 482 } 483 if (bp->b_wcred != NOCRED) { 484 crfree(bp->b_wcred); 485 bp->b_wcred = NOCRED; 486 } 487 488 BUF_UNLOCK(bp); 489 } 490 491 static const char pbuf_wmesg[] = "pbufwait"; 492 493 static int 494 pbuf_init(void *mem, int size, int flags) 495 { 496 struct buf *bp = mem; 497 498 TSENTER(); 499 500 bp->b_kvabase = (void *)kva_alloc(ptoa(PBUF_PAGES)); 501 if (bp->b_kvabase == NULL) 502 return (ENOMEM); 503 bp->b_kvasize = ptoa(PBUF_PAGES); 504 BUF_LOCKINIT(bp, pbuf_wmesg); 505 LIST_INIT(&bp->b_dep); 506 bp->b_rcred = bp->b_wcred = NOCRED; 507 bp->b_xflags = 0; 508 509 TSEXIT(); 510 511 return (0); 512 } 513 514 /* 515 * Associate a p-buffer with a vnode. 516 * 517 * Also sets B_PAGING flag to indicate that vnode is not fully associated 518 * with the buffer. i.e. the bp has not been linked into the vnode or 519 * ref-counted. 520 */ 521 void 522 pbgetvp(struct vnode *vp, struct buf *bp) 523 { 524 525 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free")); 526 KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)")); 527 528 bp->b_vp = vp; 529 bp->b_flags |= B_PAGING; 530 bp->b_bufobj = &vp->v_bufobj; 531 } 532 533 /* 534 * Associate a p-buffer with a vnode. 535 * 536 * Also sets B_PAGING flag to indicate that vnode is not fully associated 537 * with the buffer. i.e. the bp has not been linked into the vnode or 538 * ref-counted. 539 */ 540 void 541 pbgetbo(struct bufobj *bo, struct buf *bp) 542 { 543 544 KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)")); 545 KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)")); 546 547 bp->b_flags |= B_PAGING; 548 bp->b_bufobj = bo; 549 } 550 551 /* 552 * Disassociate a p-buffer from a vnode. 553 */ 554 void 555 pbrelvp(struct buf *bp) 556 { 557 558 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL")); 559 KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj")); 560 KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0, 561 ("pbrelvp: pager buf on vnode list.")); 562 563 bp->b_vp = NULL; 564 bp->b_bufobj = NULL; 565 bp->b_flags &= ~B_PAGING; 566 } 567 568 /* 569 * Disassociate a p-buffer from a bufobj. 570 */ 571 void 572 pbrelbo(struct buf *bp) 573 { 574 575 KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode")); 576 KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj")); 577 KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0, 578 ("pbrelbo: pager buf on vnode list.")); 579 580 bp->b_bufobj = NULL; 581 bp->b_flags &= ~B_PAGING; 582 } 583 584 void 585 vm_object_set_writeable_dirty(vm_object_t object) 586 { 587 pgo_set_writeable_dirty_t *method; 588 589 MPASS(object->type < nitems(pagertab)); 590 591 method = pagertab[object->type]->pgo_set_writeable_dirty; 592 if (method != NULL) 593 method(object); 594 } 595 596 bool 597 vm_object_mightbedirty(vm_object_t object) 598 { 599 pgo_mightbedirty_t *method; 600 601 MPASS(object->type < nitems(pagertab)); 602 603 method = pagertab[object->type]->pgo_mightbedirty; 604 if (method == NULL) 605 return (false); 606 return (method(object)); 607 } 608 609 /* 610 * Return the kvme type of the given object. 611 * If vpp is not NULL, set it to the object's vm_object_vnode() or NULL. 612 */ 613 int 614 vm_object_kvme_type(vm_object_t object, struct vnode **vpp) 615 { 616 VM_OBJECT_ASSERT_LOCKED(object); 617 MPASS(object->type < nitems(pagertab)); 618 619 if (vpp != NULL) 620 *vpp = vm_object_vnode(object); 621 return (pagertab[object->type]->pgo_kvme_type); 622 } 623