1 /*- 2 * Copyright (c) 1998 Matthew Dillon, 3 * Copyright (c) 1994 John S. Dyson 4 * Copyright (c) 1990 University of Utah. 5 * Copyright (c) 1982, 1986, 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * New Swap System 41 * Matthew Dillon 42 * 43 * Radix Bitmap 'blists'. 44 * 45 * - The new swapper uses the new radix bitmap code. This should scale 46 * to arbitrarily small or arbitrarily large swap spaces and an almost 47 * arbitrary degree of fragmentation. 48 * 49 * Features: 50 * 51 * - on the fly reallocation of swap during putpages. The new system 52 * does not try to keep previously allocated swap blocks for dirty 53 * pages. 54 * 55 * - on the fly deallocation of swap 56 * 57 * - No more garbage collection required. Unnecessarily allocated swap 58 * blocks only exist for dirty vm_page_t's now and these are already 59 * cycled (in a high-load system) by the pager. We also do on-the-fly 60 * removal of invalidated swap blocks when a page is destroyed 61 * or renamed. 62 * 63 * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$ 64 * 65 * @(#)swap_pager.c 8.9 (Berkeley) 3/21/94 66 * @(#)vm_swap.c 8.5 (Berkeley) 2/17/94 67 */ 68 69 #include <sys/cdefs.h> 70 __FBSDID("$FreeBSD$"); 71 72 #include "opt_swap.h" 73 #include "opt_vm.h" 74 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/conf.h> 78 #include <sys/kernel.h> 79 #include <sys/priv.h> 80 #include <sys/proc.h> 81 #include <sys/bio.h> 82 #include <sys/buf.h> 83 #include <sys/disk.h> 84 #include <sys/fcntl.h> 85 #include <sys/mount.h> 86 #include <sys/namei.h> 87 #include <sys/vnode.h> 88 #include <sys/malloc.h> 89 #include <sys/racct.h> 90 #include <sys/resource.h> 91 #include <sys/resourcevar.h> 92 #include <sys/sysctl.h> 93 #include <sys/sysproto.h> 94 #include <sys/blist.h> 95 #include <sys/lock.h> 96 #include <sys/sx.h> 97 #include <sys/vmmeter.h> 98 99 #include <security/mac/mac_framework.h> 100 101 #include <vm/vm.h> 102 #include <vm/pmap.h> 103 #include <vm/vm_map.h> 104 #include <vm/vm_kern.h> 105 #include <vm/vm_object.h> 106 #include <vm/vm_page.h> 107 #include <vm/vm_pager.h> 108 #include <vm/vm_pageout.h> 109 #include <vm/vm_param.h> 110 #include <vm/swap_pager.h> 111 #include <vm/vm_extern.h> 112 #include <vm/uma.h> 113 114 #include <geom/geom.h> 115 116 /* 117 * SWB_NPAGES must be a power of 2. It may be set to 1, 2, 4, 8, or 16 118 * pages per allocation. We recommend you stick with the default of 8. 119 * The 16-page limit is due to the radix code (kern/subr_blist.c). 120 */ 121 #ifndef MAX_PAGEOUT_CLUSTER 122 #define MAX_PAGEOUT_CLUSTER 16 123 #endif 124 125 #if !defined(SWB_NPAGES) 126 #define SWB_NPAGES MAX_PAGEOUT_CLUSTER 127 #endif 128 129 /* 130 * Piecemeal swap metadata structure. Swap is stored in a radix tree. 131 * 132 * If SWB_NPAGES is 8 and sizeof(char *) == sizeof(daddr_t), our radix 133 * is basically 8. Assuming PAGE_SIZE == 4096, one tree level represents 134 * 32K worth of data, two levels represent 256K, three levels represent 135 * 2 MBytes. This is acceptable. 136 * 137 * Overall memory utilization is about the same as the old swap structure. 138 */ 139 #define SWCORRECT(n) (sizeof(void *) * (n) / sizeof(daddr_t)) 140 #define SWAP_META_PAGES (SWB_NPAGES * 2) 141 #define SWAP_META_MASK (SWAP_META_PAGES - 1) 142 143 struct swblock { 144 struct swblock *swb_hnext; 145 vm_object_t swb_object; 146 vm_pindex_t swb_index; 147 int swb_count; 148 daddr_t swb_pages[SWAP_META_PAGES]; 149 }; 150 151 static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data"); 152 static struct mtx sw_dev_mtx; 153 static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq); 154 static struct swdevt *swdevhd; /* Allocate from here next */ 155 static int nswapdev; /* Number of swap devices */ 156 int swap_pager_avail; 157 static int swdev_syscall_active = 0; /* serialize swap(on|off) */ 158 159 static vm_ooffset_t swap_total; 160 SYSCTL_QUAD(_vm, OID_AUTO, swap_total, CTLFLAG_RD, &swap_total, 0, 161 "Total amount of available swap storage."); 162 static vm_ooffset_t swap_reserved; 163 SYSCTL_QUAD(_vm, OID_AUTO, swap_reserved, CTLFLAG_RD, &swap_reserved, 0, 164 "Amount of swap storage needed to back all allocated anonymous memory."); 165 static int overcommit = 0; 166 SYSCTL_INT(_vm, OID_AUTO, overcommit, CTLFLAG_RW, &overcommit, 0, 167 "Configure virtual memory overcommit behavior. See tuning(7) " 168 "for details."); 169 170 /* bits from overcommit */ 171 #define SWAP_RESERVE_FORCE_ON (1 << 0) 172 #define SWAP_RESERVE_RLIMIT_ON (1 << 1) 173 #define SWAP_RESERVE_ALLOW_NONWIRED (1 << 2) 174 175 int 176 swap_reserve(vm_ooffset_t incr) 177 { 178 179 return (swap_reserve_by_cred(incr, curthread->td_ucred)); 180 } 181 182 int 183 swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred) 184 { 185 vm_ooffset_t r, s; 186 int res, error; 187 static int curfail; 188 static struct timeval lastfail; 189 struct uidinfo *uip; 190 191 uip = cred->cr_ruidinfo; 192 193 if (incr & PAGE_MASK) 194 panic("swap_reserve: & PAGE_MASK"); 195 196 PROC_LOCK(curproc); 197 error = racct_add(curproc, RACCT_SWAP, incr); 198 PROC_UNLOCK(curproc); 199 if (error != 0) 200 return (0); 201 202 res = 0; 203 mtx_lock(&sw_dev_mtx); 204 r = swap_reserved + incr; 205 if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) { 206 s = cnt.v_page_count - cnt.v_free_reserved - cnt.v_wire_count; 207 s *= PAGE_SIZE; 208 } else 209 s = 0; 210 s += swap_total; 211 if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s || 212 (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) { 213 res = 1; 214 swap_reserved = r; 215 } 216 mtx_unlock(&sw_dev_mtx); 217 218 if (res) { 219 PROC_LOCK(curproc); 220 UIDINFO_VMSIZE_LOCK(uip); 221 if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 && 222 uip->ui_vmsize + incr > lim_cur(curproc, RLIMIT_SWAP) && 223 priv_check(curthread, PRIV_VM_SWAP_NORLIMIT)) 224 res = 0; 225 else 226 uip->ui_vmsize += incr; 227 UIDINFO_VMSIZE_UNLOCK(uip); 228 PROC_UNLOCK(curproc); 229 if (!res) { 230 mtx_lock(&sw_dev_mtx); 231 swap_reserved -= incr; 232 mtx_unlock(&sw_dev_mtx); 233 } 234 } 235 if (!res && ppsratecheck(&lastfail, &curfail, 1)) { 236 printf("uid %d, pid %d: swap reservation for %jd bytes failed\n", 237 curproc->p_pid, uip->ui_uid, incr); 238 } 239 240 if (!res) { 241 PROC_LOCK(curproc); 242 racct_sub(curproc, RACCT_SWAP, incr); 243 PROC_UNLOCK(curproc); 244 } 245 246 return (res); 247 } 248 249 void 250 swap_reserve_force(vm_ooffset_t incr) 251 { 252 struct uidinfo *uip; 253 254 mtx_lock(&sw_dev_mtx); 255 swap_reserved += incr; 256 mtx_unlock(&sw_dev_mtx); 257 258 PROC_LOCK(curproc); 259 racct_add_force(curproc, RACCT_SWAP, incr); 260 PROC_UNLOCK(curproc); 261 262 uip = curthread->td_ucred->cr_ruidinfo; 263 PROC_LOCK(curproc); 264 UIDINFO_VMSIZE_LOCK(uip); 265 uip->ui_vmsize += incr; 266 UIDINFO_VMSIZE_UNLOCK(uip); 267 PROC_UNLOCK(curproc); 268 } 269 270 void 271 swap_release(vm_ooffset_t decr) 272 { 273 struct ucred *cred; 274 275 PROC_LOCK(curproc); 276 cred = curthread->td_ucred; 277 swap_release_by_cred(decr, cred); 278 PROC_UNLOCK(curproc); 279 } 280 281 void 282 swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred) 283 { 284 struct uidinfo *uip; 285 286 uip = cred->cr_ruidinfo; 287 288 if (decr & PAGE_MASK) 289 panic("swap_release: & PAGE_MASK"); 290 291 mtx_lock(&sw_dev_mtx); 292 if (swap_reserved < decr) 293 panic("swap_reserved < decr"); 294 swap_reserved -= decr; 295 mtx_unlock(&sw_dev_mtx); 296 297 UIDINFO_VMSIZE_LOCK(uip); 298 if (uip->ui_vmsize < decr) 299 printf("negative vmsize for uid = %d\n", uip->ui_uid); 300 uip->ui_vmsize -= decr; 301 UIDINFO_VMSIZE_UNLOCK(uip); 302 303 racct_sub_cred(cred, RACCT_SWAP, decr); 304 } 305 306 static void swapdev_strategy(struct buf *, struct swdevt *sw); 307 308 #define SWM_FREE 0x02 /* free, period */ 309 #define SWM_POP 0x04 /* pop out */ 310 311 int swap_pager_full = 2; /* swap space exhaustion (task killing) */ 312 static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/ 313 static int nsw_rcount; /* free read buffers */ 314 static int nsw_wcount_sync; /* limit write buffers / synchronous */ 315 static int nsw_wcount_async; /* limit write buffers / asynchronous */ 316 static int nsw_wcount_async_max;/* assigned maximum */ 317 static int nsw_cluster_max; /* maximum VOP I/O allowed */ 318 319 static struct swblock **swhash; 320 static int swhash_mask; 321 static struct mtx swhash_mtx; 322 323 static int swap_async_max = 4; /* maximum in-progress async I/O's */ 324 static struct sx sw_alloc_sx; 325 326 327 SYSCTL_INT(_vm, OID_AUTO, swap_async_max, 328 CTLFLAG_RW, &swap_async_max, 0, "Maximum running async swap ops"); 329 330 /* 331 * "named" and "unnamed" anon region objects. Try to reduce the overhead 332 * of searching a named list by hashing it just a little. 333 */ 334 335 #define NOBJLISTS 8 336 337 #define NOBJLIST(handle) \ 338 (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)]) 339 340 static struct mtx sw_alloc_mtx; /* protect list manipulation */ 341 static struct pagerlst swap_pager_object_list[NOBJLISTS]; 342 static uma_zone_t swap_zone; 343 static struct vm_object swap_zone_obj; 344 345 /* 346 * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 347 * calls hooked from other parts of the VM system and do not appear here. 348 * (see vm/swap_pager.h). 349 */ 350 static vm_object_t 351 swap_pager_alloc(void *handle, vm_ooffset_t size, 352 vm_prot_t prot, vm_ooffset_t offset, struct ucred *); 353 static void swap_pager_dealloc(vm_object_t object); 354 static int swap_pager_getpages(vm_object_t, vm_page_t *, int, int); 355 static void swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *); 356 static boolean_t 357 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after); 358 static void swap_pager_init(void); 359 static void swap_pager_unswapped(vm_page_t); 360 static void swap_pager_swapoff(struct swdevt *sp); 361 362 struct pagerops swappagerops = { 363 .pgo_init = swap_pager_init, /* early system initialization of pager */ 364 .pgo_alloc = swap_pager_alloc, /* allocate an OBJT_SWAP object */ 365 .pgo_dealloc = swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 366 .pgo_getpages = swap_pager_getpages, /* pagein */ 367 .pgo_putpages = swap_pager_putpages, /* pageout */ 368 .pgo_haspage = swap_pager_haspage, /* get backing store status for page */ 369 .pgo_pageunswapped = swap_pager_unswapped, /* remove swap related to page */ 370 }; 371 372 /* 373 * dmmax is in page-sized chunks with the new swap system. It was 374 * dev-bsized chunks in the old. dmmax is always a power of 2. 375 * 376 * swap_*() routines are externally accessible. swp_*() routines are 377 * internal. 378 */ 379 static int dmmax; 380 static int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 381 static int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 382 383 SYSCTL_INT(_vm, OID_AUTO, dmmax, 384 CTLFLAG_RD, &dmmax, 0, "Maximum size of a swap block"); 385 386 static void swp_sizecheck(void); 387 static void swp_pager_async_iodone(struct buf *bp); 388 static int swapongeom(struct thread *, struct vnode *); 389 static int swaponvp(struct thread *, struct vnode *, u_long); 390 static int swapoff_one(struct swdevt *sp, struct ucred *cred); 391 392 /* 393 * Swap bitmap functions 394 */ 395 static void swp_pager_freeswapspace(daddr_t blk, int npages); 396 static daddr_t swp_pager_getswapspace(int npages); 397 398 /* 399 * Metadata functions 400 */ 401 static struct swblock **swp_pager_hash(vm_object_t object, vm_pindex_t index); 402 static void swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t); 403 static void swp_pager_meta_free(vm_object_t, vm_pindex_t, daddr_t); 404 static void swp_pager_meta_free_all(vm_object_t); 405 static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int); 406 407 static void 408 swp_pager_free_nrpage(vm_page_t m) 409 { 410 411 vm_page_lock(m); 412 if (m->wire_count == 0) 413 vm_page_free(m); 414 vm_page_unlock(m); 415 } 416 417 /* 418 * SWP_SIZECHECK() - update swap_pager_full indication 419 * 420 * update the swap_pager_almost_full indication and warn when we are 421 * about to run out of swap space, using lowat/hiwat hysteresis. 422 * 423 * Clear swap_pager_full ( task killing ) indication when lowat is met. 424 * 425 * No restrictions on call 426 * This routine may not block. 427 * This routine must be called at splvm() 428 */ 429 static void 430 swp_sizecheck(void) 431 { 432 433 if (swap_pager_avail < nswap_lowat) { 434 if (swap_pager_almost_full == 0) { 435 printf("swap_pager: out of swap space\n"); 436 swap_pager_almost_full = 1; 437 } 438 } else { 439 swap_pager_full = 0; 440 if (swap_pager_avail > nswap_hiwat) 441 swap_pager_almost_full = 0; 442 } 443 } 444 445 /* 446 * SWP_PAGER_HASH() - hash swap meta data 447 * 448 * This is an helper function which hashes the swapblk given 449 * the object and page index. It returns a pointer to a pointer 450 * to the object, or a pointer to a NULL pointer if it could not 451 * find a swapblk. 452 * 453 * This routine must be called at splvm(). 454 */ 455 static struct swblock ** 456 swp_pager_hash(vm_object_t object, vm_pindex_t index) 457 { 458 struct swblock **pswap; 459 struct swblock *swap; 460 461 index &= ~(vm_pindex_t)SWAP_META_MASK; 462 pswap = &swhash[(index ^ (int)(intptr_t)object) & swhash_mask]; 463 while ((swap = *pswap) != NULL) { 464 if (swap->swb_object == object && 465 swap->swb_index == index 466 ) { 467 break; 468 } 469 pswap = &swap->swb_hnext; 470 } 471 return (pswap); 472 } 473 474 /* 475 * SWAP_PAGER_INIT() - initialize the swap pager! 476 * 477 * Expected to be started from system init. NOTE: This code is run 478 * before much else so be careful what you depend on. Most of the VM 479 * system has yet to be initialized at this point. 480 */ 481 static void 482 swap_pager_init(void) 483 { 484 /* 485 * Initialize object lists 486 */ 487 int i; 488 489 for (i = 0; i < NOBJLISTS; ++i) 490 TAILQ_INIT(&swap_pager_object_list[i]); 491 mtx_init(&sw_alloc_mtx, "swap_pager list", NULL, MTX_DEF); 492 mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF); 493 494 /* 495 * Device Stripe, in PAGE_SIZE'd blocks 496 */ 497 dmmax = SWB_NPAGES * 2; 498 } 499 500 /* 501 * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 502 * 503 * Expected to be started from pageout process once, prior to entering 504 * its main loop. 505 */ 506 void 507 swap_pager_swap_init(void) 508 { 509 int n, n2; 510 511 /* 512 * Number of in-transit swap bp operations. Don't 513 * exhaust the pbufs completely. Make sure we 514 * initialize workable values (0 will work for hysteresis 515 * but it isn't very efficient). 516 * 517 * The nsw_cluster_max is constrained by the bp->b_pages[] 518 * array (MAXPHYS/PAGE_SIZE) and our locally defined 519 * MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 520 * constrained by the swap device interleave stripe size. 521 * 522 * Currently we hardwire nsw_wcount_async to 4. This limit is 523 * designed to prevent other I/O from having high latencies due to 524 * our pageout I/O. The value 4 works well for one or two active swap 525 * devices but is probably a little low if you have more. Even so, 526 * a higher value would probably generate only a limited improvement 527 * with three or four active swap devices since the system does not 528 * typically have to pageout at extreme bandwidths. We will want 529 * at least 2 per swap devices, and 4 is a pretty good value if you 530 * have one NFS swap device due to the command/ack latency over NFS. 531 * So it all works out pretty well. 532 */ 533 nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER); 534 535 mtx_lock(&pbuf_mtx); 536 nsw_rcount = (nswbuf + 1) / 2; 537 nsw_wcount_sync = (nswbuf + 3) / 4; 538 nsw_wcount_async = 4; 539 nsw_wcount_async_max = nsw_wcount_async; 540 mtx_unlock(&pbuf_mtx); 541 542 /* 543 * Initialize our zone. Right now I'm just guessing on the number 544 * we need based on the number of pages in the system. Each swblock 545 * can hold 16 pages, so this is probably overkill. This reservation 546 * is typically limited to around 32MB by default. 547 */ 548 n = cnt.v_page_count / 2; 549 if (maxswzone && n > maxswzone / sizeof(struct swblock)) 550 n = maxswzone / sizeof(struct swblock); 551 n2 = n; 552 swap_zone = uma_zcreate("SWAPMETA", sizeof(struct swblock), NULL, NULL, 553 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM); 554 if (swap_zone == NULL) 555 panic("failed to create swap_zone."); 556 do { 557 if (uma_zone_set_obj(swap_zone, &swap_zone_obj, n)) 558 break; 559 /* 560 * if the allocation failed, try a zone two thirds the 561 * size of the previous attempt. 562 */ 563 n -= ((n + 2) / 3); 564 } while (n > 0); 565 if (n2 != n) 566 printf("Swap zone entries reduced from %d to %d.\n", n2, n); 567 n2 = n; 568 569 /* 570 * Initialize our meta-data hash table. The swapper does not need to 571 * be quite as efficient as the VM system, so we do not use an 572 * oversized hash table. 573 * 574 * n: size of hash table, must be power of 2 575 * swhash_mask: hash table index mask 576 */ 577 for (n = 1; n < n2 / 8; n *= 2) 578 ; 579 swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK | M_ZERO); 580 swhash_mask = n - 1; 581 mtx_init(&swhash_mtx, "swap_pager swhash", NULL, MTX_DEF); 582 } 583 584 /* 585 * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 586 * its metadata structures. 587 * 588 * This routine is called from the mmap and fork code to create a new 589 * OBJT_SWAP object. We do this by creating an OBJT_DEFAULT object 590 * and then converting it with swp_pager_meta_build(). 591 * 592 * This routine may block in vm_object_allocate() and create a named 593 * object lookup race, so we must interlock. We must also run at 594 * splvm() for the object lookup to handle races with interrupts, but 595 * we do not have to maintain splvm() in between the lookup and the 596 * add because (I believe) it is not possible to attempt to create 597 * a new swap object w/handle when a default object with that handle 598 * already exists. 599 * 600 * MPSAFE 601 */ 602 static vm_object_t 603 swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 604 vm_ooffset_t offset, struct ucred *cred) 605 { 606 vm_object_t object; 607 vm_pindex_t pindex; 608 609 pindex = OFF_TO_IDX(offset + PAGE_MASK + size); 610 if (handle) { 611 mtx_lock(&Giant); 612 /* 613 * Reference existing named region or allocate new one. There 614 * should not be a race here against swp_pager_meta_build() 615 * as called from vm_page_remove() in regards to the lookup 616 * of the handle. 617 */ 618 sx_xlock(&sw_alloc_sx); 619 object = vm_pager_object_lookup(NOBJLIST(handle), handle); 620 if (object == NULL) { 621 if (cred != NULL) { 622 if (!swap_reserve_by_cred(size, cred)) { 623 sx_xunlock(&sw_alloc_sx); 624 mtx_unlock(&Giant); 625 return (NULL); 626 } 627 crhold(cred); 628 } 629 object = vm_object_allocate(OBJT_DEFAULT, pindex); 630 VM_OBJECT_LOCK(object); 631 object->handle = handle; 632 if (cred != NULL) { 633 object->cred = cred; 634 object->charge = size; 635 } 636 swp_pager_meta_build(object, 0, SWAPBLK_NONE); 637 VM_OBJECT_UNLOCK(object); 638 } 639 sx_xunlock(&sw_alloc_sx); 640 mtx_unlock(&Giant); 641 } else { 642 if (cred != NULL) { 643 if (!swap_reserve_by_cred(size, cred)) 644 return (NULL); 645 crhold(cred); 646 } 647 object = vm_object_allocate(OBJT_DEFAULT, pindex); 648 VM_OBJECT_LOCK(object); 649 if (cred != NULL) { 650 object->cred = cred; 651 object->charge = size; 652 } 653 swp_pager_meta_build(object, 0, SWAPBLK_NONE); 654 VM_OBJECT_UNLOCK(object); 655 } 656 return (object); 657 } 658 659 /* 660 * SWAP_PAGER_DEALLOC() - remove swap metadata from object 661 * 662 * The swap backing for the object is destroyed. The code is 663 * designed such that we can reinstantiate it later, but this 664 * routine is typically called only when the entire object is 665 * about to be destroyed. 666 * 667 * This routine may block, but no longer does. 668 * 669 * The object must be locked or unreferenceable. 670 */ 671 static void 672 swap_pager_dealloc(vm_object_t object) 673 { 674 675 /* 676 * Remove from list right away so lookups will fail if we block for 677 * pageout completion. 678 */ 679 if (object->handle != NULL) { 680 mtx_lock(&sw_alloc_mtx); 681 TAILQ_REMOVE(NOBJLIST(object->handle), object, pager_object_list); 682 mtx_unlock(&sw_alloc_mtx); 683 } 684 685 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 686 vm_object_pip_wait(object, "swpdea"); 687 688 /* 689 * Free all remaining metadata. We only bother to free it from 690 * the swap meta data. We do not attempt to free swapblk's still 691 * associated with vm_page_t's for this object. We do not care 692 * if paging is still in progress on some objects. 693 */ 694 swp_pager_meta_free_all(object); 695 } 696 697 /************************************************************************ 698 * SWAP PAGER BITMAP ROUTINES * 699 ************************************************************************/ 700 701 /* 702 * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 703 * 704 * Allocate swap for the requested number of pages. The starting 705 * swap block number (a page index) is returned or SWAPBLK_NONE 706 * if the allocation failed. 707 * 708 * Also has the side effect of advising that somebody made a mistake 709 * when they configured swap and didn't configure enough. 710 * 711 * Must be called at splvm() to avoid races with bitmap frees from 712 * vm_page_remove() aka swap_pager_page_removed(). 713 * 714 * This routine may not block 715 * This routine must be called at splvm(). 716 * 717 * We allocate in round-robin fashion from the configured devices. 718 */ 719 static daddr_t 720 swp_pager_getswapspace(int npages) 721 { 722 daddr_t blk; 723 struct swdevt *sp; 724 int i; 725 726 blk = SWAPBLK_NONE; 727 mtx_lock(&sw_dev_mtx); 728 sp = swdevhd; 729 for (i = 0; i < nswapdev; i++) { 730 if (sp == NULL) 731 sp = TAILQ_FIRST(&swtailq); 732 if (!(sp->sw_flags & SW_CLOSING)) { 733 blk = blist_alloc(sp->sw_blist, npages); 734 if (blk != SWAPBLK_NONE) { 735 blk += sp->sw_first; 736 sp->sw_used += npages; 737 swap_pager_avail -= npages; 738 swp_sizecheck(); 739 swdevhd = TAILQ_NEXT(sp, sw_list); 740 goto done; 741 } 742 } 743 sp = TAILQ_NEXT(sp, sw_list); 744 } 745 if (swap_pager_full != 2) { 746 printf("swap_pager_getswapspace(%d): failed\n", npages); 747 swap_pager_full = 2; 748 swap_pager_almost_full = 1; 749 } 750 swdevhd = NULL; 751 done: 752 mtx_unlock(&sw_dev_mtx); 753 return (blk); 754 } 755 756 static int 757 swp_pager_isondev(daddr_t blk, struct swdevt *sp) 758 { 759 760 return (blk >= sp->sw_first && blk < sp->sw_end); 761 } 762 763 static void 764 swp_pager_strategy(struct buf *bp) 765 { 766 struct swdevt *sp; 767 768 mtx_lock(&sw_dev_mtx); 769 TAILQ_FOREACH(sp, &swtailq, sw_list) { 770 if (bp->b_blkno >= sp->sw_first && bp->b_blkno < sp->sw_end) { 771 mtx_unlock(&sw_dev_mtx); 772 sp->sw_strategy(bp, sp); 773 return; 774 } 775 } 776 panic("Swapdev not found"); 777 } 778 779 780 /* 781 * SWP_PAGER_FREESWAPSPACE() - free raw swap space 782 * 783 * This routine returns the specified swap blocks back to the bitmap. 784 * 785 * Note: This routine may not block (it could in the old swap code), 786 * and through the use of the new blist routines it does not block. 787 * 788 * We must be called at splvm() to avoid races with bitmap frees from 789 * vm_page_remove() aka swap_pager_page_removed(). 790 * 791 * This routine may not block 792 * This routine must be called at splvm(). 793 */ 794 static void 795 swp_pager_freeswapspace(daddr_t blk, int npages) 796 { 797 struct swdevt *sp; 798 799 mtx_lock(&sw_dev_mtx); 800 TAILQ_FOREACH(sp, &swtailq, sw_list) { 801 if (blk >= sp->sw_first && blk < sp->sw_end) { 802 sp->sw_used -= npages; 803 /* 804 * If we are attempting to stop swapping on 805 * this device, we don't want to mark any 806 * blocks free lest they be reused. 807 */ 808 if ((sp->sw_flags & SW_CLOSING) == 0) { 809 blist_free(sp->sw_blist, blk - sp->sw_first, 810 npages); 811 swap_pager_avail += npages; 812 swp_sizecheck(); 813 } 814 mtx_unlock(&sw_dev_mtx); 815 return; 816 } 817 } 818 panic("Swapdev not found"); 819 } 820 821 /* 822 * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 823 * range within an object. 824 * 825 * This is a globally accessible routine. 826 * 827 * This routine removes swapblk assignments from swap metadata. 828 * 829 * The external callers of this routine typically have already destroyed 830 * or renamed vm_page_t's associated with this range in the object so 831 * we should be ok. 832 * 833 * This routine may be called at any spl. We up our spl to splvm temporarily 834 * in order to perform the metadata removal. 835 */ 836 void 837 swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size) 838 { 839 840 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 841 swp_pager_meta_free(object, start, size); 842 } 843 844 /* 845 * SWAP_PAGER_RESERVE() - reserve swap blocks in object 846 * 847 * Assigns swap blocks to the specified range within the object. The 848 * swap blocks are not zerod. Any previous swap assignment is destroyed. 849 * 850 * Returns 0 on success, -1 on failure. 851 */ 852 int 853 swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size) 854 { 855 int n = 0; 856 daddr_t blk = SWAPBLK_NONE; 857 vm_pindex_t beg = start; /* save start index */ 858 859 VM_OBJECT_LOCK(object); 860 while (size) { 861 if (n == 0) { 862 n = BLIST_MAX_ALLOC; 863 while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) { 864 n >>= 1; 865 if (n == 0) { 866 swp_pager_meta_free(object, beg, start - beg); 867 VM_OBJECT_UNLOCK(object); 868 return (-1); 869 } 870 } 871 } 872 swp_pager_meta_build(object, start, blk); 873 --size; 874 ++start; 875 ++blk; 876 --n; 877 } 878 swp_pager_meta_free(object, start, n); 879 VM_OBJECT_UNLOCK(object); 880 return (0); 881 } 882 883 /* 884 * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 885 * and destroy the source. 886 * 887 * Copy any valid swapblks from the source to the destination. In 888 * cases where both the source and destination have a valid swapblk, 889 * we keep the destination's. 890 * 891 * This routine is allowed to block. It may block allocating metadata 892 * indirectly through swp_pager_meta_build() or if paging is still in 893 * progress on the source. 894 * 895 * This routine can be called at any spl 896 * 897 * XXX vm_page_collapse() kinda expects us not to block because we 898 * supposedly do not need to allocate memory, but for the moment we 899 * *may* have to get a little memory from the zone allocator, but 900 * it is taken from the interrupt memory. We should be ok. 901 * 902 * The source object contains no vm_page_t's (which is just as well) 903 * 904 * The source object is of type OBJT_SWAP. 905 * 906 * The source and destination objects must be locked or 907 * inaccessible (XXX are they ?) 908 */ 909 void 910 swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject, 911 vm_pindex_t offset, int destroysource) 912 { 913 vm_pindex_t i; 914 915 VM_OBJECT_LOCK_ASSERT(srcobject, MA_OWNED); 916 VM_OBJECT_LOCK_ASSERT(dstobject, MA_OWNED); 917 918 /* 919 * If destroysource is set, we remove the source object from the 920 * swap_pager internal queue now. 921 */ 922 if (destroysource) { 923 if (srcobject->handle != NULL) { 924 mtx_lock(&sw_alloc_mtx); 925 TAILQ_REMOVE( 926 NOBJLIST(srcobject->handle), 927 srcobject, 928 pager_object_list 929 ); 930 mtx_unlock(&sw_alloc_mtx); 931 } 932 } 933 934 /* 935 * transfer source to destination. 936 */ 937 for (i = 0; i < dstobject->size; ++i) { 938 daddr_t dstaddr; 939 940 /* 941 * Locate (without changing) the swapblk on the destination, 942 * unless it is invalid in which case free it silently, or 943 * if the destination is a resident page, in which case the 944 * source is thrown away. 945 */ 946 dstaddr = swp_pager_meta_ctl(dstobject, i, 0); 947 948 if (dstaddr == SWAPBLK_NONE) { 949 /* 950 * Destination has no swapblk and is not resident, 951 * copy source. 952 */ 953 daddr_t srcaddr; 954 955 srcaddr = swp_pager_meta_ctl( 956 srcobject, 957 i + offset, 958 SWM_POP 959 ); 960 961 if (srcaddr != SWAPBLK_NONE) { 962 /* 963 * swp_pager_meta_build() can sleep. 964 */ 965 vm_object_pip_add(srcobject, 1); 966 VM_OBJECT_UNLOCK(srcobject); 967 vm_object_pip_add(dstobject, 1); 968 swp_pager_meta_build(dstobject, i, srcaddr); 969 vm_object_pip_wakeup(dstobject); 970 VM_OBJECT_LOCK(srcobject); 971 vm_object_pip_wakeup(srcobject); 972 } 973 } else { 974 /* 975 * Destination has valid swapblk or it is represented 976 * by a resident page. We destroy the sourceblock. 977 */ 978 979 swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE); 980 } 981 } 982 983 /* 984 * Free left over swap blocks in source. 985 * 986 * We have to revert the type to OBJT_DEFAULT so we do not accidently 987 * double-remove the object from the swap queues. 988 */ 989 if (destroysource) { 990 swp_pager_meta_free_all(srcobject); 991 /* 992 * Reverting the type is not necessary, the caller is going 993 * to destroy srcobject directly, but I'm doing it here 994 * for consistency since we've removed the object from its 995 * queues. 996 */ 997 srcobject->type = OBJT_DEFAULT; 998 } 999 } 1000 1001 /* 1002 * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 1003 * the requested page. 1004 * 1005 * We determine whether good backing store exists for the requested 1006 * page and return TRUE if it does, FALSE if it doesn't. 1007 * 1008 * If TRUE, we also try to determine how much valid, contiguous backing 1009 * store exists before and after the requested page within a reasonable 1010 * distance. We do not try to restrict it to the swap device stripe 1011 * (that is handled in getpages/putpages). It probably isn't worth 1012 * doing here. 1013 */ 1014 static boolean_t 1015 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after) 1016 { 1017 daddr_t blk0; 1018 1019 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1020 /* 1021 * do we have good backing store at the requested index ? 1022 */ 1023 blk0 = swp_pager_meta_ctl(object, pindex, 0); 1024 1025 if (blk0 == SWAPBLK_NONE) { 1026 if (before) 1027 *before = 0; 1028 if (after) 1029 *after = 0; 1030 return (FALSE); 1031 } 1032 1033 /* 1034 * find backwards-looking contiguous good backing store 1035 */ 1036 if (before != NULL) { 1037 int i; 1038 1039 for (i = 1; i < (SWB_NPAGES/2); ++i) { 1040 daddr_t blk; 1041 1042 if (i > pindex) 1043 break; 1044 blk = swp_pager_meta_ctl(object, pindex - i, 0); 1045 if (blk != blk0 - i) 1046 break; 1047 } 1048 *before = (i - 1); 1049 } 1050 1051 /* 1052 * find forward-looking contiguous good backing store 1053 */ 1054 if (after != NULL) { 1055 int i; 1056 1057 for (i = 1; i < (SWB_NPAGES/2); ++i) { 1058 daddr_t blk; 1059 1060 blk = swp_pager_meta_ctl(object, pindex + i, 0); 1061 if (blk != blk0 + i) 1062 break; 1063 } 1064 *after = (i - 1); 1065 } 1066 return (TRUE); 1067 } 1068 1069 /* 1070 * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 1071 * 1072 * This removes any associated swap backing store, whether valid or 1073 * not, from the page. 1074 * 1075 * This routine is typically called when a page is made dirty, at 1076 * which point any associated swap can be freed. MADV_FREE also 1077 * calls us in a special-case situation 1078 * 1079 * NOTE!!! If the page is clean and the swap was valid, the caller 1080 * should make the page dirty before calling this routine. This routine 1081 * does NOT change the m->dirty status of the page. Also: MADV_FREE 1082 * depends on it. 1083 * 1084 * This routine may not block 1085 * This routine must be called at splvm() 1086 */ 1087 static void 1088 swap_pager_unswapped(vm_page_t m) 1089 { 1090 1091 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1092 swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE); 1093 } 1094 1095 /* 1096 * SWAP_PAGER_GETPAGES() - bring pages in from swap 1097 * 1098 * Attempt to retrieve (m, count) pages from backing store, but make 1099 * sure we retrieve at least m[reqpage]. We try to load in as large 1100 * a chunk surrounding m[reqpage] as is contiguous in swap and which 1101 * belongs to the same object. 1102 * 1103 * The code is designed for asynchronous operation and 1104 * immediate-notification of 'reqpage' but tends not to be 1105 * used that way. Please do not optimize-out this algorithmic 1106 * feature, I intend to improve on it in the future. 1107 * 1108 * The parent has a single vm_object_pip_add() reference prior to 1109 * calling us and we should return with the same. 1110 * 1111 * The parent has BUSY'd the pages. We should return with 'm' 1112 * left busy, but the others adjusted. 1113 */ 1114 static int 1115 swap_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage) 1116 { 1117 struct buf *bp; 1118 vm_page_t mreq; 1119 int i; 1120 int j; 1121 daddr_t blk; 1122 1123 mreq = m[reqpage]; 1124 1125 KASSERT(mreq->object == object, 1126 ("swap_pager_getpages: object mismatch %p/%p", 1127 object, mreq->object)); 1128 1129 /* 1130 * Calculate range to retrieve. The pages have already been assigned 1131 * their swapblks. We require a *contiguous* range but we know it to 1132 * not span devices. If we do not supply it, bad things 1133 * happen. Note that blk, iblk & jblk can be SWAPBLK_NONE, but the 1134 * loops are set up such that the case(s) are handled implicitly. 1135 * 1136 * The swp_*() calls must be made with the object locked. 1137 */ 1138 blk = swp_pager_meta_ctl(mreq->object, mreq->pindex, 0); 1139 1140 for (i = reqpage - 1; i >= 0; --i) { 1141 daddr_t iblk; 1142 1143 iblk = swp_pager_meta_ctl(m[i]->object, m[i]->pindex, 0); 1144 if (blk != iblk + (reqpage - i)) 1145 break; 1146 } 1147 ++i; 1148 1149 for (j = reqpage + 1; j < count; ++j) { 1150 daddr_t jblk; 1151 1152 jblk = swp_pager_meta_ctl(m[j]->object, m[j]->pindex, 0); 1153 if (blk != jblk - (j - reqpage)) 1154 break; 1155 } 1156 1157 /* 1158 * free pages outside our collection range. Note: we never free 1159 * mreq, it must remain busy throughout. 1160 */ 1161 if (0 < i || j < count) { 1162 int k; 1163 1164 for (k = 0; k < i; ++k) 1165 swp_pager_free_nrpage(m[k]); 1166 for (k = j; k < count; ++k) 1167 swp_pager_free_nrpage(m[k]); 1168 } 1169 1170 /* 1171 * Return VM_PAGER_FAIL if we have nothing to do. Return mreq 1172 * still busy, but the others unbusied. 1173 */ 1174 if (blk == SWAPBLK_NONE) 1175 return (VM_PAGER_FAIL); 1176 1177 /* 1178 * Getpbuf() can sleep. 1179 */ 1180 VM_OBJECT_UNLOCK(object); 1181 /* 1182 * Get a swap buffer header to perform the IO 1183 */ 1184 bp = getpbuf(&nsw_rcount); 1185 bp->b_flags |= B_PAGING; 1186 1187 /* 1188 * map our page(s) into kva for input 1189 */ 1190 pmap_qenter((vm_offset_t)bp->b_data, m + i, j - i); 1191 1192 bp->b_iocmd = BIO_READ; 1193 bp->b_iodone = swp_pager_async_iodone; 1194 bp->b_rcred = crhold(thread0.td_ucred); 1195 bp->b_wcred = crhold(thread0.td_ucred); 1196 bp->b_blkno = blk - (reqpage - i); 1197 bp->b_bcount = PAGE_SIZE * (j - i); 1198 bp->b_bufsize = PAGE_SIZE * (j - i); 1199 bp->b_pager.pg_reqpage = reqpage - i; 1200 1201 VM_OBJECT_LOCK(object); 1202 { 1203 int k; 1204 1205 for (k = i; k < j; ++k) { 1206 bp->b_pages[k - i] = m[k]; 1207 m[k]->oflags |= VPO_SWAPINPROG; 1208 } 1209 } 1210 bp->b_npages = j - i; 1211 1212 PCPU_INC(cnt.v_swapin); 1213 PCPU_ADD(cnt.v_swappgsin, bp->b_npages); 1214 1215 /* 1216 * We still hold the lock on mreq, and our automatic completion routine 1217 * does not remove it. 1218 */ 1219 vm_object_pip_add(object, bp->b_npages); 1220 VM_OBJECT_UNLOCK(object); 1221 1222 /* 1223 * perform the I/O. NOTE!!! bp cannot be considered valid after 1224 * this point because we automatically release it on completion. 1225 * Instead, we look at the one page we are interested in which we 1226 * still hold a lock on even through the I/O completion. 1227 * 1228 * The other pages in our m[] array are also released on completion, 1229 * so we cannot assume they are valid anymore either. 1230 * 1231 * NOTE: b_blkno is destroyed by the call to swapdev_strategy 1232 */ 1233 BUF_KERNPROC(bp); 1234 swp_pager_strategy(bp); 1235 1236 /* 1237 * wait for the page we want to complete. VPO_SWAPINPROG is always 1238 * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1239 * is set in the meta-data. 1240 */ 1241 VM_OBJECT_LOCK(object); 1242 while ((mreq->oflags & VPO_SWAPINPROG) != 0) { 1243 mreq->oflags |= VPO_WANTED; 1244 PCPU_INC(cnt.v_intrans); 1245 if (msleep(mreq, VM_OBJECT_MTX(object), PSWP, "swread", hz*20)) { 1246 printf( 1247 "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n", 1248 bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount); 1249 } 1250 } 1251 1252 /* 1253 * mreq is left busied after completion, but all the other pages 1254 * are freed. If we had an unrecoverable read error the page will 1255 * not be valid. 1256 */ 1257 if (mreq->valid != VM_PAGE_BITS_ALL) { 1258 return (VM_PAGER_ERROR); 1259 } else { 1260 return (VM_PAGER_OK); 1261 } 1262 1263 /* 1264 * A final note: in a low swap situation, we cannot deallocate swap 1265 * and mark a page dirty here because the caller is likely to mark 1266 * the page clean when we return, causing the page to possibly revert 1267 * to all-zero's later. 1268 */ 1269 } 1270 1271 /* 1272 * swap_pager_putpages: 1273 * 1274 * Assign swap (if necessary) and initiate I/O on the specified pages. 1275 * 1276 * We support both OBJT_DEFAULT and OBJT_SWAP objects. DEFAULT objects 1277 * are automatically converted to SWAP objects. 1278 * 1279 * In a low memory situation we may block in VOP_STRATEGY(), but the new 1280 * vm_page reservation system coupled with properly written VFS devices 1281 * should ensure that no low-memory deadlock occurs. This is an area 1282 * which needs work. 1283 * 1284 * The parent has N vm_object_pip_add() references prior to 1285 * calling us and will remove references for rtvals[] that are 1286 * not set to VM_PAGER_PEND. We need to remove the rest on I/O 1287 * completion. 1288 * 1289 * The parent has soft-busy'd the pages it passes us and will unbusy 1290 * those whos rtvals[] entry is not set to VM_PAGER_PEND on return. 1291 * We need to unbusy the rest on I/O completion. 1292 */ 1293 void 1294 swap_pager_putpages(vm_object_t object, vm_page_t *m, int count, 1295 boolean_t sync, int *rtvals) 1296 { 1297 int i; 1298 int n = 0; 1299 1300 if (count && m[0]->object != object) { 1301 panic("swap_pager_putpages: object mismatch %p/%p", 1302 object, 1303 m[0]->object 1304 ); 1305 } 1306 1307 /* 1308 * Step 1 1309 * 1310 * Turn object into OBJT_SWAP 1311 * check for bogus sysops 1312 * force sync if not pageout process 1313 */ 1314 if (object->type != OBJT_SWAP) 1315 swp_pager_meta_build(object, 0, SWAPBLK_NONE); 1316 VM_OBJECT_UNLOCK(object); 1317 1318 if (curproc != pageproc) 1319 sync = TRUE; 1320 1321 /* 1322 * Step 2 1323 * 1324 * Update nsw parameters from swap_async_max sysctl values. 1325 * Do not let the sysop crash the machine with bogus numbers. 1326 */ 1327 mtx_lock(&pbuf_mtx); 1328 if (swap_async_max != nsw_wcount_async_max) { 1329 int n; 1330 1331 /* 1332 * limit range 1333 */ 1334 if ((n = swap_async_max) > nswbuf / 2) 1335 n = nswbuf / 2; 1336 if (n < 1) 1337 n = 1; 1338 swap_async_max = n; 1339 1340 /* 1341 * Adjust difference ( if possible ). If the current async 1342 * count is too low, we may not be able to make the adjustment 1343 * at this time. 1344 */ 1345 n -= nsw_wcount_async_max; 1346 if (nsw_wcount_async + n >= 0) { 1347 nsw_wcount_async += n; 1348 nsw_wcount_async_max += n; 1349 wakeup(&nsw_wcount_async); 1350 } 1351 } 1352 mtx_unlock(&pbuf_mtx); 1353 1354 /* 1355 * Step 3 1356 * 1357 * Assign swap blocks and issue I/O. We reallocate swap on the fly. 1358 * The page is left dirty until the pageout operation completes 1359 * successfully. 1360 */ 1361 for (i = 0; i < count; i += n) { 1362 int j; 1363 struct buf *bp; 1364 daddr_t blk; 1365 1366 /* 1367 * Maximum I/O size is limited by a number of factors. 1368 */ 1369 n = min(BLIST_MAX_ALLOC, count - i); 1370 n = min(n, nsw_cluster_max); 1371 1372 /* 1373 * Get biggest block of swap we can. If we fail, fall 1374 * back and try to allocate a smaller block. Don't go 1375 * overboard trying to allocate space if it would overly 1376 * fragment swap. 1377 */ 1378 while ( 1379 (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE && 1380 n > 4 1381 ) { 1382 n >>= 1; 1383 } 1384 if (blk == SWAPBLK_NONE) { 1385 for (j = 0; j < n; ++j) 1386 rtvals[i+j] = VM_PAGER_FAIL; 1387 continue; 1388 } 1389 1390 /* 1391 * All I/O parameters have been satisfied, build the I/O 1392 * request and assign the swap space. 1393 */ 1394 if (sync == TRUE) { 1395 bp = getpbuf(&nsw_wcount_sync); 1396 } else { 1397 bp = getpbuf(&nsw_wcount_async); 1398 bp->b_flags = B_ASYNC; 1399 } 1400 bp->b_flags |= B_PAGING; 1401 bp->b_iocmd = BIO_WRITE; 1402 1403 pmap_qenter((vm_offset_t)bp->b_data, &m[i], n); 1404 1405 bp->b_rcred = crhold(thread0.td_ucred); 1406 bp->b_wcred = crhold(thread0.td_ucred); 1407 bp->b_bcount = PAGE_SIZE * n; 1408 bp->b_bufsize = PAGE_SIZE * n; 1409 bp->b_blkno = blk; 1410 1411 VM_OBJECT_LOCK(object); 1412 for (j = 0; j < n; ++j) { 1413 vm_page_t mreq = m[i+j]; 1414 1415 swp_pager_meta_build( 1416 mreq->object, 1417 mreq->pindex, 1418 blk + j 1419 ); 1420 vm_page_dirty(mreq); 1421 rtvals[i+j] = VM_PAGER_OK; 1422 1423 mreq->oflags |= VPO_SWAPINPROG; 1424 bp->b_pages[j] = mreq; 1425 } 1426 VM_OBJECT_UNLOCK(object); 1427 bp->b_npages = n; 1428 /* 1429 * Must set dirty range for NFS to work. 1430 */ 1431 bp->b_dirtyoff = 0; 1432 bp->b_dirtyend = bp->b_bcount; 1433 1434 PCPU_INC(cnt.v_swapout); 1435 PCPU_ADD(cnt.v_swappgsout, bp->b_npages); 1436 1437 /* 1438 * asynchronous 1439 * 1440 * NOTE: b_blkno is destroyed by the call to swapdev_strategy 1441 */ 1442 if (sync == FALSE) { 1443 bp->b_iodone = swp_pager_async_iodone; 1444 BUF_KERNPROC(bp); 1445 swp_pager_strategy(bp); 1446 1447 for (j = 0; j < n; ++j) 1448 rtvals[i+j] = VM_PAGER_PEND; 1449 /* restart outter loop */ 1450 continue; 1451 } 1452 1453 /* 1454 * synchronous 1455 * 1456 * NOTE: b_blkno is destroyed by the call to swapdev_strategy 1457 */ 1458 bp->b_iodone = bdone; 1459 swp_pager_strategy(bp); 1460 1461 /* 1462 * Wait for the sync I/O to complete, then update rtvals. 1463 * We just set the rtvals[] to VM_PAGER_PEND so we can call 1464 * our async completion routine at the end, thus avoiding a 1465 * double-free. 1466 */ 1467 bwait(bp, PVM, "swwrt"); 1468 for (j = 0; j < n; ++j) 1469 rtvals[i+j] = VM_PAGER_PEND; 1470 /* 1471 * Now that we are through with the bp, we can call the 1472 * normal async completion, which frees everything up. 1473 */ 1474 swp_pager_async_iodone(bp); 1475 } 1476 VM_OBJECT_LOCK(object); 1477 } 1478 1479 /* 1480 * swp_pager_async_iodone: 1481 * 1482 * Completion routine for asynchronous reads and writes from/to swap. 1483 * Also called manually by synchronous code to finish up a bp. 1484 * 1485 * For READ operations, the pages are VPO_BUSY'd. For WRITE operations, 1486 * the pages are vm_page_t->busy'd. For READ operations, we VPO_BUSY 1487 * unbusy all pages except the 'main' request page. For WRITE 1488 * operations, we vm_page_t->busy'd unbusy all pages ( we can do this 1489 * because we marked them all VM_PAGER_PEND on return from putpages ). 1490 * 1491 * This routine may not block. 1492 */ 1493 static void 1494 swp_pager_async_iodone(struct buf *bp) 1495 { 1496 int i; 1497 vm_object_t object = NULL; 1498 1499 /* 1500 * report error 1501 */ 1502 if (bp->b_ioflags & BIO_ERROR) { 1503 printf( 1504 "swap_pager: I/O error - %s failed; blkno %ld," 1505 "size %ld, error %d\n", 1506 ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 1507 (long)bp->b_blkno, 1508 (long)bp->b_bcount, 1509 bp->b_error 1510 ); 1511 } 1512 1513 /* 1514 * remove the mapping for kernel virtual 1515 */ 1516 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1517 1518 if (bp->b_npages) { 1519 object = bp->b_pages[0]->object; 1520 VM_OBJECT_LOCK(object); 1521 } 1522 1523 /* 1524 * cleanup pages. If an error occurs writing to swap, we are in 1525 * very serious trouble. If it happens to be a disk error, though, 1526 * we may be able to recover by reassigning the swap later on. So 1527 * in this case we remove the m->swapblk assignment for the page 1528 * but do not free it in the rlist. The errornous block(s) are thus 1529 * never reallocated as swap. Redirty the page and continue. 1530 */ 1531 for (i = 0; i < bp->b_npages; ++i) { 1532 vm_page_t m = bp->b_pages[i]; 1533 1534 m->oflags &= ~VPO_SWAPINPROG; 1535 1536 if (bp->b_ioflags & BIO_ERROR) { 1537 /* 1538 * If an error occurs I'd love to throw the swapblk 1539 * away without freeing it back to swapspace, so it 1540 * can never be used again. But I can't from an 1541 * interrupt. 1542 */ 1543 if (bp->b_iocmd == BIO_READ) { 1544 /* 1545 * When reading, reqpage needs to stay 1546 * locked for the parent, but all other 1547 * pages can be freed. We still want to 1548 * wakeup the parent waiting on the page, 1549 * though. ( also: pg_reqpage can be -1 and 1550 * not match anything ). 1551 * 1552 * We have to wake specifically requested pages 1553 * up too because we cleared VPO_SWAPINPROG and 1554 * someone may be waiting for that. 1555 * 1556 * NOTE: for reads, m->dirty will probably 1557 * be overridden by the original caller of 1558 * getpages so don't play cute tricks here. 1559 */ 1560 m->valid = 0; 1561 if (i != bp->b_pager.pg_reqpage) 1562 swp_pager_free_nrpage(m); 1563 else 1564 vm_page_flash(m); 1565 /* 1566 * If i == bp->b_pager.pg_reqpage, do not wake 1567 * the page up. The caller needs to. 1568 */ 1569 } else { 1570 /* 1571 * If a write error occurs, reactivate page 1572 * so it doesn't clog the inactive list, 1573 * then finish the I/O. 1574 */ 1575 vm_page_dirty(m); 1576 vm_page_lock(m); 1577 vm_page_activate(m); 1578 vm_page_unlock(m); 1579 vm_page_io_finish(m); 1580 } 1581 } else if (bp->b_iocmd == BIO_READ) { 1582 /* 1583 * NOTE: for reads, m->dirty will probably be 1584 * overridden by the original caller of getpages so 1585 * we cannot set them in order to free the underlying 1586 * swap in a low-swap situation. I don't think we'd 1587 * want to do that anyway, but it was an optimization 1588 * that existed in the old swapper for a time before 1589 * it got ripped out due to precisely this problem. 1590 * 1591 * If not the requested page then deactivate it. 1592 * 1593 * Note that the requested page, reqpage, is left 1594 * busied, but we still have to wake it up. The 1595 * other pages are released (unbusied) by 1596 * vm_page_wakeup(). 1597 */ 1598 KASSERT(!pmap_page_is_mapped(m), 1599 ("swp_pager_async_iodone: page %p is mapped", m)); 1600 m->valid = VM_PAGE_BITS_ALL; 1601 KASSERT(m->dirty == 0, 1602 ("swp_pager_async_iodone: page %p is dirty", m)); 1603 1604 /* 1605 * We have to wake specifically requested pages 1606 * up too because we cleared VPO_SWAPINPROG and 1607 * could be waiting for it in getpages. However, 1608 * be sure to not unbusy getpages specifically 1609 * requested page - getpages expects it to be 1610 * left busy. 1611 */ 1612 if (i != bp->b_pager.pg_reqpage) { 1613 vm_page_lock(m); 1614 vm_page_deactivate(m); 1615 vm_page_unlock(m); 1616 vm_page_wakeup(m); 1617 } else 1618 vm_page_flash(m); 1619 } else { 1620 /* 1621 * For write success, clear the dirty 1622 * status, then finish the I/O ( which decrements the 1623 * busy count and possibly wakes waiter's up ). 1624 */ 1625 KASSERT((m->flags & PG_WRITEABLE) == 0, 1626 ("swp_pager_async_iodone: page %p is not write" 1627 " protected", m)); 1628 vm_page_undirty(m); 1629 vm_page_io_finish(m); 1630 if (vm_page_count_severe()) { 1631 vm_page_lock(m); 1632 vm_page_try_to_cache(m); 1633 vm_page_unlock(m); 1634 } 1635 } 1636 } 1637 1638 /* 1639 * adjust pip. NOTE: the original parent may still have its own 1640 * pip refs on the object. 1641 */ 1642 if (object != NULL) { 1643 vm_object_pip_wakeupn(object, bp->b_npages); 1644 VM_OBJECT_UNLOCK(object); 1645 } 1646 1647 /* 1648 * swapdev_strategy() manually sets b_vp and b_bufobj before calling 1649 * bstrategy(). Set them back to NULL now we're done with it, or we'll 1650 * trigger a KASSERT in relpbuf(). 1651 */ 1652 if (bp->b_vp) { 1653 bp->b_vp = NULL; 1654 bp->b_bufobj = NULL; 1655 } 1656 /* 1657 * release the physical I/O buffer 1658 */ 1659 relpbuf( 1660 bp, 1661 ((bp->b_iocmd == BIO_READ) ? &nsw_rcount : 1662 ((bp->b_flags & B_ASYNC) ? 1663 &nsw_wcount_async : 1664 &nsw_wcount_sync 1665 ) 1666 ) 1667 ); 1668 } 1669 1670 /* 1671 * swap_pager_isswapped: 1672 * 1673 * Return 1 if at least one page in the given object is paged 1674 * out to the given swap device. 1675 * 1676 * This routine may not block. 1677 */ 1678 int 1679 swap_pager_isswapped(vm_object_t object, struct swdevt *sp) 1680 { 1681 daddr_t index = 0; 1682 int bcount; 1683 int i; 1684 1685 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1686 if (object->type != OBJT_SWAP) 1687 return (0); 1688 1689 mtx_lock(&swhash_mtx); 1690 for (bcount = 0; bcount < object->un_pager.swp.swp_bcount; bcount++) { 1691 struct swblock *swap; 1692 1693 if ((swap = *swp_pager_hash(object, index)) != NULL) { 1694 for (i = 0; i < SWAP_META_PAGES; ++i) { 1695 if (swp_pager_isondev(swap->swb_pages[i], sp)) { 1696 mtx_unlock(&swhash_mtx); 1697 return (1); 1698 } 1699 } 1700 } 1701 index += SWAP_META_PAGES; 1702 } 1703 mtx_unlock(&swhash_mtx); 1704 return (0); 1705 } 1706 1707 /* 1708 * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in 1709 * 1710 * This routine dissociates the page at the given index within a 1711 * swap block from its backing store, paging it in if necessary. 1712 * If the page is paged in, it is placed in the inactive queue, 1713 * since it had its backing store ripped out from under it. 1714 * We also attempt to swap in all other pages in the swap block, 1715 * we only guarantee that the one at the specified index is 1716 * paged in. 1717 * 1718 * XXX - The code to page the whole block in doesn't work, so we 1719 * revert to the one-by-one behavior for now. Sigh. 1720 */ 1721 static inline void 1722 swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex) 1723 { 1724 vm_page_t m; 1725 1726 vm_object_pip_add(object, 1); 1727 m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL|VM_ALLOC_RETRY); 1728 if (m->valid == VM_PAGE_BITS_ALL) { 1729 vm_object_pip_subtract(object, 1); 1730 vm_page_dirty(m); 1731 vm_page_lock(m); 1732 vm_page_activate(m); 1733 vm_page_unlock(m); 1734 vm_page_wakeup(m); 1735 vm_pager_page_unswapped(m); 1736 return; 1737 } 1738 1739 if (swap_pager_getpages(object, &m, 1, 0) != VM_PAGER_OK) 1740 panic("swap_pager_force_pagein: read from swap failed");/*XXX*/ 1741 vm_object_pip_subtract(object, 1); 1742 vm_page_dirty(m); 1743 vm_page_lock(m); 1744 vm_page_deactivate(m); 1745 vm_page_unlock(m); 1746 vm_page_wakeup(m); 1747 vm_pager_page_unswapped(m); 1748 } 1749 1750 /* 1751 * swap_pager_swapoff: 1752 * 1753 * Page in all of the pages that have been paged out to the 1754 * given device. The corresponding blocks in the bitmap must be 1755 * marked as allocated and the device must be flagged SW_CLOSING. 1756 * There may be no processes swapped out to the device. 1757 * 1758 * This routine may block. 1759 */ 1760 static void 1761 swap_pager_swapoff(struct swdevt *sp) 1762 { 1763 struct swblock *swap; 1764 int i, j, retries; 1765 1766 GIANT_REQUIRED; 1767 1768 retries = 0; 1769 full_rescan: 1770 mtx_lock(&swhash_mtx); 1771 for (i = 0; i <= swhash_mask; i++) { /* '<=' is correct here */ 1772 restart: 1773 for (swap = swhash[i]; swap != NULL; swap = swap->swb_hnext) { 1774 vm_object_t object = swap->swb_object; 1775 vm_pindex_t pindex = swap->swb_index; 1776 for (j = 0; j < SWAP_META_PAGES; ++j) { 1777 if (swp_pager_isondev(swap->swb_pages[j], sp)) { 1778 /* avoid deadlock */ 1779 if (!VM_OBJECT_TRYLOCK(object)) { 1780 break; 1781 } else { 1782 mtx_unlock(&swhash_mtx); 1783 swp_pager_force_pagein(object, 1784 pindex + j); 1785 VM_OBJECT_UNLOCK(object); 1786 mtx_lock(&swhash_mtx); 1787 goto restart; 1788 } 1789 } 1790 } 1791 } 1792 } 1793 mtx_unlock(&swhash_mtx); 1794 if (sp->sw_used) { 1795 /* 1796 * Objects may be locked or paging to the device being 1797 * removed, so we will miss their pages and need to 1798 * make another pass. We have marked this device as 1799 * SW_CLOSING, so the activity should finish soon. 1800 */ 1801 retries++; 1802 if (retries > 100) { 1803 panic("swapoff: failed to locate %d swap blocks", 1804 sp->sw_used); 1805 } 1806 pause("swpoff", hz / 20); 1807 goto full_rescan; 1808 } 1809 } 1810 1811 /************************************************************************ 1812 * SWAP META DATA * 1813 ************************************************************************ 1814 * 1815 * These routines manipulate the swap metadata stored in the 1816 * OBJT_SWAP object. All swp_*() routines must be called at 1817 * splvm() because swap can be freed up by the low level vm_page 1818 * code which might be called from interrupts beyond what splbio() covers. 1819 * 1820 * Swap metadata is implemented with a global hash and not directly 1821 * linked into the object. Instead the object simply contains 1822 * appropriate tracking counters. 1823 */ 1824 1825 /* 1826 * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 1827 * 1828 * We first convert the object to a swap object if it is a default 1829 * object. 1830 * 1831 * The specified swapblk is added to the object's swap metadata. If 1832 * the swapblk is not valid, it is freed instead. Any previously 1833 * assigned swapblk is freed. 1834 * 1835 * This routine must be called at splvm(), except when used to convert 1836 * an OBJT_DEFAULT object into an OBJT_SWAP object. 1837 */ 1838 static void 1839 swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk) 1840 { 1841 struct swblock *swap; 1842 struct swblock **pswap; 1843 int idx; 1844 1845 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1846 /* 1847 * Convert default object to swap object if necessary 1848 */ 1849 if (object->type != OBJT_SWAP) { 1850 object->type = OBJT_SWAP; 1851 object->un_pager.swp.swp_bcount = 0; 1852 1853 if (object->handle != NULL) { 1854 mtx_lock(&sw_alloc_mtx); 1855 TAILQ_INSERT_TAIL( 1856 NOBJLIST(object->handle), 1857 object, 1858 pager_object_list 1859 ); 1860 mtx_unlock(&sw_alloc_mtx); 1861 } 1862 } 1863 1864 /* 1865 * Locate hash entry. If not found create, but if we aren't adding 1866 * anything just return. If we run out of space in the map we wait 1867 * and, since the hash table may have changed, retry. 1868 */ 1869 retry: 1870 mtx_lock(&swhash_mtx); 1871 pswap = swp_pager_hash(object, pindex); 1872 1873 if ((swap = *pswap) == NULL) { 1874 int i; 1875 1876 if (swapblk == SWAPBLK_NONE) 1877 goto done; 1878 1879 swap = *pswap = uma_zalloc(swap_zone, M_NOWAIT); 1880 if (swap == NULL) { 1881 mtx_unlock(&swhash_mtx); 1882 VM_OBJECT_UNLOCK(object); 1883 if (uma_zone_exhausted(swap_zone)) { 1884 printf("swap zone exhausted, increase kern.maxswzone\n"); 1885 vm_pageout_oom(VM_OOM_SWAPZ); 1886 pause("swzonex", 10); 1887 } else 1888 VM_WAIT; 1889 VM_OBJECT_LOCK(object); 1890 goto retry; 1891 } 1892 1893 swap->swb_hnext = NULL; 1894 swap->swb_object = object; 1895 swap->swb_index = pindex & ~(vm_pindex_t)SWAP_META_MASK; 1896 swap->swb_count = 0; 1897 1898 ++object->un_pager.swp.swp_bcount; 1899 1900 for (i = 0; i < SWAP_META_PAGES; ++i) 1901 swap->swb_pages[i] = SWAPBLK_NONE; 1902 } 1903 1904 /* 1905 * Delete prior contents of metadata 1906 */ 1907 idx = pindex & SWAP_META_MASK; 1908 1909 if (swap->swb_pages[idx] != SWAPBLK_NONE) { 1910 swp_pager_freeswapspace(swap->swb_pages[idx], 1); 1911 --swap->swb_count; 1912 } 1913 1914 /* 1915 * Enter block into metadata 1916 */ 1917 swap->swb_pages[idx] = swapblk; 1918 if (swapblk != SWAPBLK_NONE) 1919 ++swap->swb_count; 1920 done: 1921 mtx_unlock(&swhash_mtx); 1922 } 1923 1924 /* 1925 * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 1926 * 1927 * The requested range of blocks is freed, with any associated swap 1928 * returned to the swap bitmap. 1929 * 1930 * This routine will free swap metadata structures as they are cleaned 1931 * out. This routine does *NOT* operate on swap metadata associated 1932 * with resident pages. 1933 * 1934 * This routine must be called at splvm() 1935 */ 1936 static void 1937 swp_pager_meta_free(vm_object_t object, vm_pindex_t index, daddr_t count) 1938 { 1939 1940 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1941 if (object->type != OBJT_SWAP) 1942 return; 1943 1944 while (count > 0) { 1945 struct swblock **pswap; 1946 struct swblock *swap; 1947 1948 mtx_lock(&swhash_mtx); 1949 pswap = swp_pager_hash(object, index); 1950 1951 if ((swap = *pswap) != NULL) { 1952 daddr_t v = swap->swb_pages[index & SWAP_META_MASK]; 1953 1954 if (v != SWAPBLK_NONE) { 1955 swp_pager_freeswapspace(v, 1); 1956 swap->swb_pages[index & SWAP_META_MASK] = 1957 SWAPBLK_NONE; 1958 if (--swap->swb_count == 0) { 1959 *pswap = swap->swb_hnext; 1960 uma_zfree(swap_zone, swap); 1961 --object->un_pager.swp.swp_bcount; 1962 } 1963 } 1964 --count; 1965 ++index; 1966 } else { 1967 int n = SWAP_META_PAGES - (index & SWAP_META_MASK); 1968 count -= n; 1969 index += n; 1970 } 1971 mtx_unlock(&swhash_mtx); 1972 } 1973 } 1974 1975 /* 1976 * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 1977 * 1978 * This routine locates and destroys all swap metadata associated with 1979 * an object. 1980 * 1981 * This routine must be called at splvm() 1982 */ 1983 static void 1984 swp_pager_meta_free_all(vm_object_t object) 1985 { 1986 daddr_t index = 0; 1987 1988 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1989 if (object->type != OBJT_SWAP) 1990 return; 1991 1992 while (object->un_pager.swp.swp_bcount) { 1993 struct swblock **pswap; 1994 struct swblock *swap; 1995 1996 mtx_lock(&swhash_mtx); 1997 pswap = swp_pager_hash(object, index); 1998 if ((swap = *pswap) != NULL) { 1999 int i; 2000 2001 for (i = 0; i < SWAP_META_PAGES; ++i) { 2002 daddr_t v = swap->swb_pages[i]; 2003 if (v != SWAPBLK_NONE) { 2004 --swap->swb_count; 2005 swp_pager_freeswapspace(v, 1); 2006 } 2007 } 2008 if (swap->swb_count != 0) 2009 panic("swap_pager_meta_free_all: swb_count != 0"); 2010 *pswap = swap->swb_hnext; 2011 uma_zfree(swap_zone, swap); 2012 --object->un_pager.swp.swp_bcount; 2013 } 2014 mtx_unlock(&swhash_mtx); 2015 index += SWAP_META_PAGES; 2016 } 2017 } 2018 2019 /* 2020 * SWP_PAGER_METACTL() - misc control of swap and vm_page_t meta data. 2021 * 2022 * This routine is capable of looking up, popping, or freeing 2023 * swapblk assignments in the swap meta data or in the vm_page_t. 2024 * The routine typically returns the swapblk being looked-up, or popped, 2025 * or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block 2026 * was invalid. This routine will automatically free any invalid 2027 * meta-data swapblks. 2028 * 2029 * It is not possible to store invalid swapblks in the swap meta data 2030 * (other then a literal 'SWAPBLK_NONE'), so we don't bother checking. 2031 * 2032 * When acting on a busy resident page and paging is in progress, we 2033 * have to wait until paging is complete but otherwise can act on the 2034 * busy page. 2035 * 2036 * This routine must be called at splvm(). 2037 * 2038 * SWM_FREE remove and free swap block from metadata 2039 * SWM_POP remove from meta data but do not free.. pop it out 2040 */ 2041 static daddr_t 2042 swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags) 2043 { 2044 struct swblock **pswap; 2045 struct swblock *swap; 2046 daddr_t r1; 2047 int idx; 2048 2049 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 2050 /* 2051 * The meta data only exists of the object is OBJT_SWAP 2052 * and even then might not be allocated yet. 2053 */ 2054 if (object->type != OBJT_SWAP) 2055 return (SWAPBLK_NONE); 2056 2057 r1 = SWAPBLK_NONE; 2058 mtx_lock(&swhash_mtx); 2059 pswap = swp_pager_hash(object, pindex); 2060 2061 if ((swap = *pswap) != NULL) { 2062 idx = pindex & SWAP_META_MASK; 2063 r1 = swap->swb_pages[idx]; 2064 2065 if (r1 != SWAPBLK_NONE) { 2066 if (flags & SWM_FREE) { 2067 swp_pager_freeswapspace(r1, 1); 2068 r1 = SWAPBLK_NONE; 2069 } 2070 if (flags & (SWM_FREE|SWM_POP)) { 2071 swap->swb_pages[idx] = SWAPBLK_NONE; 2072 if (--swap->swb_count == 0) { 2073 *pswap = swap->swb_hnext; 2074 uma_zfree(swap_zone, swap); 2075 --object->un_pager.swp.swp_bcount; 2076 } 2077 } 2078 } 2079 } 2080 mtx_unlock(&swhash_mtx); 2081 return (r1); 2082 } 2083 2084 /* 2085 * System call swapon(name) enables swapping on device name, 2086 * which must be in the swdevsw. Return EBUSY 2087 * if already swapping on this device. 2088 */ 2089 #ifndef _SYS_SYSPROTO_H_ 2090 struct swapon_args { 2091 char *name; 2092 }; 2093 #endif 2094 2095 /* 2096 * MPSAFE 2097 */ 2098 /* ARGSUSED */ 2099 int 2100 swapon(struct thread *td, struct swapon_args *uap) 2101 { 2102 struct vattr attr; 2103 struct vnode *vp; 2104 struct nameidata nd; 2105 int error; 2106 2107 error = priv_check(td, PRIV_SWAPON); 2108 if (error) 2109 return (error); 2110 2111 mtx_lock(&Giant); 2112 while (swdev_syscall_active) 2113 tsleep(&swdev_syscall_active, PUSER - 1, "swpon", 0); 2114 swdev_syscall_active = 1; 2115 2116 /* 2117 * Swap metadata may not fit in the KVM if we have physical 2118 * memory of >1GB. 2119 */ 2120 if (swap_zone == NULL) { 2121 error = ENOMEM; 2122 goto done; 2123 } 2124 2125 NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE, 2126 uap->name, td); 2127 error = namei(&nd); 2128 if (error) 2129 goto done; 2130 2131 NDFREE(&nd, NDF_ONLY_PNBUF); 2132 vp = nd.ni_vp; 2133 2134 if (vn_isdisk(vp, &error)) { 2135 error = swapongeom(td, vp); 2136 } else if (vp->v_type == VREG && 2137 (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 2138 (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) { 2139 /* 2140 * Allow direct swapping to NFS regular files in the same 2141 * way that nfs_mountroot() sets up diskless swapping. 2142 */ 2143 error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 2144 } 2145 2146 if (error) 2147 vrele(vp); 2148 done: 2149 swdev_syscall_active = 0; 2150 wakeup_one(&swdev_syscall_active); 2151 mtx_unlock(&Giant); 2152 return (error); 2153 } 2154 2155 static void 2156 swaponsomething(struct vnode *vp, void *id, u_long nblks, sw_strategy_t *strategy, sw_close_t *close, dev_t dev) 2157 { 2158 struct swdevt *sp, *tsp; 2159 swblk_t dvbase; 2160 u_long mblocks; 2161 2162 /* 2163 * If we go beyond this, we get overflows in the radix 2164 * tree bitmap code. 2165 */ 2166 mblocks = 0x40000000 / BLIST_META_RADIX; 2167 if (nblks > mblocks) { 2168 printf("WARNING: reducing size to maximum of %lu blocks per swap unit\n", 2169 mblocks); 2170 nblks = mblocks; 2171 } 2172 /* 2173 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 2174 * First chop nblks off to page-align it, then convert. 2175 * 2176 * sw->sw_nblks is in page-sized chunks now too. 2177 */ 2178 nblks &= ~(ctodb(1) - 1); 2179 nblks = dbtoc(nblks); 2180 2181 sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO); 2182 sp->sw_vp = vp; 2183 sp->sw_id = id; 2184 sp->sw_dev = dev; 2185 sp->sw_flags = 0; 2186 sp->sw_nblks = nblks; 2187 sp->sw_used = 0; 2188 sp->sw_strategy = strategy; 2189 sp->sw_close = close; 2190 2191 sp->sw_blist = blist_create(nblks, M_WAITOK); 2192 /* 2193 * Do not free the first two block in order to avoid overwriting 2194 * any bsd label at the front of the partition 2195 */ 2196 blist_free(sp->sw_blist, 2, nblks - 2); 2197 2198 dvbase = 0; 2199 mtx_lock(&sw_dev_mtx); 2200 TAILQ_FOREACH(tsp, &swtailq, sw_list) { 2201 if (tsp->sw_end >= dvbase) { 2202 /* 2203 * We put one uncovered page between the devices 2204 * in order to definitively prevent any cross-device 2205 * I/O requests 2206 */ 2207 dvbase = tsp->sw_end + 1; 2208 } 2209 } 2210 sp->sw_first = dvbase; 2211 sp->sw_end = dvbase + nblks; 2212 TAILQ_INSERT_TAIL(&swtailq, sp, sw_list); 2213 nswapdev++; 2214 swap_pager_avail += nblks; 2215 swap_total += (vm_ooffset_t)nblks * PAGE_SIZE; 2216 swp_sizecheck(); 2217 mtx_unlock(&sw_dev_mtx); 2218 } 2219 2220 /* 2221 * SYSCALL: swapoff(devname) 2222 * 2223 * Disable swapping on the given device. 2224 * 2225 * XXX: Badly designed system call: it should use a device index 2226 * rather than filename as specification. We keep sw_vp around 2227 * only to make this work. 2228 */ 2229 #ifndef _SYS_SYSPROTO_H_ 2230 struct swapoff_args { 2231 char *name; 2232 }; 2233 #endif 2234 2235 /* 2236 * MPSAFE 2237 */ 2238 /* ARGSUSED */ 2239 int 2240 swapoff(struct thread *td, struct swapoff_args *uap) 2241 { 2242 struct vnode *vp; 2243 struct nameidata nd; 2244 struct swdevt *sp; 2245 int error; 2246 2247 error = priv_check(td, PRIV_SWAPOFF); 2248 if (error) 2249 return (error); 2250 2251 mtx_lock(&Giant); 2252 while (swdev_syscall_active) 2253 tsleep(&swdev_syscall_active, PUSER - 1, "swpoff", 0); 2254 swdev_syscall_active = 1; 2255 2256 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name, 2257 td); 2258 error = namei(&nd); 2259 if (error) 2260 goto done; 2261 NDFREE(&nd, NDF_ONLY_PNBUF); 2262 vp = nd.ni_vp; 2263 2264 mtx_lock(&sw_dev_mtx); 2265 TAILQ_FOREACH(sp, &swtailq, sw_list) { 2266 if (sp->sw_vp == vp) 2267 break; 2268 } 2269 mtx_unlock(&sw_dev_mtx); 2270 if (sp == NULL) { 2271 error = EINVAL; 2272 goto done; 2273 } 2274 error = swapoff_one(sp, td->td_ucred); 2275 done: 2276 swdev_syscall_active = 0; 2277 wakeup_one(&swdev_syscall_active); 2278 mtx_unlock(&Giant); 2279 return (error); 2280 } 2281 2282 static int 2283 swapoff_one(struct swdevt *sp, struct ucred *cred) 2284 { 2285 u_long nblks, dvbase; 2286 #ifdef MAC 2287 int error; 2288 #endif 2289 2290 mtx_assert(&Giant, MA_OWNED); 2291 #ifdef MAC 2292 (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY); 2293 error = mac_system_check_swapoff(cred, sp->sw_vp); 2294 (void) VOP_UNLOCK(sp->sw_vp, 0); 2295 if (error != 0) 2296 return (error); 2297 #endif 2298 nblks = sp->sw_nblks; 2299 2300 /* 2301 * We can turn off this swap device safely only if the 2302 * available virtual memory in the system will fit the amount 2303 * of data we will have to page back in, plus an epsilon so 2304 * the system doesn't become critically low on swap space. 2305 */ 2306 if (cnt.v_free_count + cnt.v_cache_count + swap_pager_avail < 2307 nblks + nswap_lowat) { 2308 return (ENOMEM); 2309 } 2310 2311 /* 2312 * Prevent further allocations on this device. 2313 */ 2314 mtx_lock(&sw_dev_mtx); 2315 sp->sw_flags |= SW_CLOSING; 2316 for (dvbase = 0; dvbase < sp->sw_end; dvbase += dmmax) { 2317 swap_pager_avail -= blist_fill(sp->sw_blist, 2318 dvbase, dmmax); 2319 } 2320 swap_total -= (vm_ooffset_t)nblks * PAGE_SIZE; 2321 mtx_unlock(&sw_dev_mtx); 2322 2323 /* 2324 * Page in the contents of the device and close it. 2325 */ 2326 swap_pager_swapoff(sp); 2327 2328 sp->sw_close(curthread, sp); 2329 sp->sw_id = NULL; 2330 mtx_lock(&sw_dev_mtx); 2331 TAILQ_REMOVE(&swtailq, sp, sw_list); 2332 nswapdev--; 2333 if (nswapdev == 0) { 2334 swap_pager_full = 2; 2335 swap_pager_almost_full = 1; 2336 } 2337 if (swdevhd == sp) 2338 swdevhd = NULL; 2339 mtx_unlock(&sw_dev_mtx); 2340 blist_destroy(sp->sw_blist); 2341 free(sp, M_VMPGDATA); 2342 return (0); 2343 } 2344 2345 void 2346 swapoff_all(void) 2347 { 2348 struct swdevt *sp, *spt; 2349 const char *devname; 2350 int error; 2351 2352 mtx_lock(&Giant); 2353 while (swdev_syscall_active) 2354 tsleep(&swdev_syscall_active, PUSER - 1, "swpoff", 0); 2355 swdev_syscall_active = 1; 2356 2357 mtx_lock(&sw_dev_mtx); 2358 TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) { 2359 mtx_unlock(&sw_dev_mtx); 2360 if (vn_isdisk(sp->sw_vp, NULL)) 2361 devname = sp->sw_vp->v_rdev->si_name; 2362 else 2363 devname = "[file]"; 2364 error = swapoff_one(sp, thread0.td_ucred); 2365 if (error != 0) { 2366 printf("Cannot remove swap device %s (error=%d), " 2367 "skipping.\n", devname, error); 2368 } else if (bootverbose) { 2369 printf("Swap device %s removed.\n", devname); 2370 } 2371 mtx_lock(&sw_dev_mtx); 2372 } 2373 mtx_unlock(&sw_dev_mtx); 2374 2375 swdev_syscall_active = 0; 2376 wakeup_one(&swdev_syscall_active); 2377 mtx_unlock(&Giant); 2378 } 2379 2380 void 2381 swap_pager_status(int *total, int *used) 2382 { 2383 struct swdevt *sp; 2384 2385 *total = 0; 2386 *used = 0; 2387 mtx_lock(&sw_dev_mtx); 2388 TAILQ_FOREACH(sp, &swtailq, sw_list) { 2389 *total += sp->sw_nblks; 2390 *used += sp->sw_used; 2391 } 2392 mtx_unlock(&sw_dev_mtx); 2393 } 2394 2395 static int 2396 sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 2397 { 2398 int *name = (int *)arg1; 2399 int error, n; 2400 struct xswdev xs; 2401 struct swdevt *sp; 2402 2403 if (arg2 != 1) /* name length */ 2404 return (EINVAL); 2405 2406 n = 0; 2407 mtx_lock(&sw_dev_mtx); 2408 TAILQ_FOREACH(sp, &swtailq, sw_list) { 2409 if (n == *name) { 2410 mtx_unlock(&sw_dev_mtx); 2411 xs.xsw_version = XSWDEV_VERSION; 2412 xs.xsw_dev = sp->sw_dev; 2413 xs.xsw_flags = sp->sw_flags; 2414 xs.xsw_nblks = sp->sw_nblks; 2415 xs.xsw_used = sp->sw_used; 2416 2417 error = SYSCTL_OUT(req, &xs, sizeof(xs)); 2418 return (error); 2419 } 2420 n++; 2421 } 2422 mtx_unlock(&sw_dev_mtx); 2423 return (ENOENT); 2424 } 2425 2426 SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0, 2427 "Number of swap devices"); 2428 SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD, sysctl_vm_swap_info, 2429 "Swap statistics by device"); 2430 2431 /* 2432 * vmspace_swap_count() - count the approximate swap usage in pages for a 2433 * vmspace. 2434 * 2435 * The map must be locked. 2436 * 2437 * Swap usage is determined by taking the proportional swap used by 2438 * VM objects backing the VM map. To make up for fractional losses, 2439 * if the VM object has any swap use at all the associated map entries 2440 * count for at least 1 swap page. 2441 */ 2442 long 2443 vmspace_swap_count(struct vmspace *vmspace) 2444 { 2445 vm_map_t map; 2446 vm_map_entry_t cur; 2447 vm_object_t object; 2448 long count, n; 2449 2450 map = &vmspace->vm_map; 2451 count = 0; 2452 2453 for (cur = map->header.next; cur != &map->header; cur = cur->next) { 2454 if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 && 2455 (object = cur->object.vm_object) != NULL) { 2456 VM_OBJECT_LOCK(object); 2457 if (object->type == OBJT_SWAP && 2458 object->un_pager.swp.swp_bcount != 0) { 2459 n = (cur->end - cur->start) / PAGE_SIZE; 2460 count += object->un_pager.swp.swp_bcount * 2461 SWAP_META_PAGES * n / object->size + 1; 2462 } 2463 VM_OBJECT_UNLOCK(object); 2464 } 2465 } 2466 return (count); 2467 } 2468 2469 /* 2470 * GEOM backend 2471 * 2472 * Swapping onto disk devices. 2473 * 2474 */ 2475 2476 static g_orphan_t swapgeom_orphan; 2477 2478 static struct g_class g_swap_class = { 2479 .name = "SWAP", 2480 .version = G_VERSION, 2481 .orphan = swapgeom_orphan, 2482 }; 2483 2484 DECLARE_GEOM_CLASS(g_swap_class, g_class); 2485 2486 2487 static void 2488 swapgeom_done(struct bio *bp2) 2489 { 2490 struct buf *bp; 2491 2492 bp = bp2->bio_caller2; 2493 bp->b_ioflags = bp2->bio_flags; 2494 if (bp2->bio_error) 2495 bp->b_ioflags |= BIO_ERROR; 2496 bp->b_resid = bp->b_bcount - bp2->bio_completed; 2497 bp->b_error = bp2->bio_error; 2498 bufdone(bp); 2499 g_destroy_bio(bp2); 2500 } 2501 2502 static void 2503 swapgeom_strategy(struct buf *bp, struct swdevt *sp) 2504 { 2505 struct bio *bio; 2506 struct g_consumer *cp; 2507 2508 cp = sp->sw_id; 2509 if (cp == NULL) { 2510 bp->b_error = ENXIO; 2511 bp->b_ioflags |= BIO_ERROR; 2512 bufdone(bp); 2513 return; 2514 } 2515 if (bp->b_iocmd == BIO_WRITE) 2516 bio = g_new_bio(); 2517 else 2518 bio = g_alloc_bio(); 2519 if (bio == NULL) { 2520 bp->b_error = ENOMEM; 2521 bp->b_ioflags |= BIO_ERROR; 2522 bufdone(bp); 2523 return; 2524 } 2525 2526 bio->bio_caller2 = bp; 2527 bio->bio_cmd = bp->b_iocmd; 2528 bio->bio_data = bp->b_data; 2529 bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE; 2530 bio->bio_length = bp->b_bcount; 2531 bio->bio_done = swapgeom_done; 2532 g_io_request(bio, cp); 2533 return; 2534 } 2535 2536 static void 2537 swapgeom_orphan(struct g_consumer *cp) 2538 { 2539 struct swdevt *sp; 2540 2541 mtx_lock(&sw_dev_mtx); 2542 TAILQ_FOREACH(sp, &swtailq, sw_list) 2543 if (sp->sw_id == cp) 2544 sp->sw_id = NULL; 2545 mtx_unlock(&sw_dev_mtx); 2546 } 2547 2548 static void 2549 swapgeom_close_ev(void *arg, int flags) 2550 { 2551 struct g_consumer *cp; 2552 2553 cp = arg; 2554 g_access(cp, -1, -1, 0); 2555 g_detach(cp); 2556 g_destroy_consumer(cp); 2557 } 2558 2559 static void 2560 swapgeom_close(struct thread *td, struct swdevt *sw) 2561 { 2562 2563 /* XXX: direct call when Giant untangled */ 2564 g_waitfor_event(swapgeom_close_ev, sw->sw_id, M_WAITOK, NULL); 2565 } 2566 2567 2568 struct swh0h0 { 2569 struct cdev *dev; 2570 struct vnode *vp; 2571 int error; 2572 }; 2573 2574 static void 2575 swapongeom_ev(void *arg, int flags) 2576 { 2577 struct swh0h0 *swh; 2578 struct g_provider *pp; 2579 struct g_consumer *cp; 2580 static struct g_geom *gp; 2581 struct swdevt *sp; 2582 u_long nblks; 2583 int error; 2584 2585 swh = arg; 2586 swh->error = 0; 2587 pp = g_dev_getprovider(swh->dev); 2588 if (pp == NULL) { 2589 swh->error = ENODEV; 2590 return; 2591 } 2592 mtx_lock(&sw_dev_mtx); 2593 TAILQ_FOREACH(sp, &swtailq, sw_list) { 2594 cp = sp->sw_id; 2595 if (cp != NULL && cp->provider == pp) { 2596 mtx_unlock(&sw_dev_mtx); 2597 swh->error = EBUSY; 2598 return; 2599 } 2600 } 2601 mtx_unlock(&sw_dev_mtx); 2602 if (gp == NULL) 2603 gp = g_new_geomf(&g_swap_class, "swap", NULL); 2604 cp = g_new_consumer(gp); 2605 g_attach(cp, pp); 2606 /* 2607 * XXX: Everytime you think you can improve the margin for 2608 * footshooting, somebody depends on the ability to do so: 2609 * savecore(8) wants to write to our swapdev so we cannot 2610 * set an exclusive count :-( 2611 */ 2612 error = g_access(cp, 1, 1, 0); 2613 if (error) { 2614 g_detach(cp); 2615 g_destroy_consumer(cp); 2616 swh->error = error; 2617 return; 2618 } 2619 nblks = pp->mediasize / DEV_BSIZE; 2620 swaponsomething(swh->vp, cp, nblks, swapgeom_strategy, 2621 swapgeom_close, dev2udev(swh->dev)); 2622 swh->error = 0; 2623 return; 2624 } 2625 2626 static int 2627 swapongeom(struct thread *td, struct vnode *vp) 2628 { 2629 int error; 2630 struct swh0h0 swh; 2631 2632 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2633 2634 swh.dev = vp->v_rdev; 2635 swh.vp = vp; 2636 swh.error = 0; 2637 /* XXX: direct call when Giant untangled */ 2638 error = g_waitfor_event(swapongeom_ev, &swh, M_WAITOK, NULL); 2639 if (!error) 2640 error = swh.error; 2641 VOP_UNLOCK(vp, 0); 2642 return (error); 2643 } 2644 2645 /* 2646 * VNODE backend 2647 * 2648 * This is used mainly for network filesystem (read: probably only tested 2649 * with NFS) swapfiles. 2650 * 2651 */ 2652 2653 static void 2654 swapdev_strategy(struct buf *bp, struct swdevt *sp) 2655 { 2656 struct vnode *vp2; 2657 2658 bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first); 2659 2660 vp2 = sp->sw_id; 2661 vhold(vp2); 2662 if (bp->b_iocmd == BIO_WRITE) { 2663 if (bp->b_bufobj) 2664 bufobj_wdrop(bp->b_bufobj); 2665 bufobj_wref(&vp2->v_bufobj); 2666 } 2667 if (bp->b_bufobj != &vp2->v_bufobj) 2668 bp->b_bufobj = &vp2->v_bufobj; 2669 bp->b_vp = vp2; 2670 bp->b_iooffset = dbtob(bp->b_blkno); 2671 bstrategy(bp); 2672 return; 2673 } 2674 2675 static void 2676 swapdev_close(struct thread *td, struct swdevt *sp) 2677 { 2678 2679 VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td); 2680 vrele(sp->sw_vp); 2681 } 2682 2683 2684 static int 2685 swaponvp(struct thread *td, struct vnode *vp, u_long nblks) 2686 { 2687 struct swdevt *sp; 2688 int error; 2689 2690 if (nblks == 0) 2691 return (ENXIO); 2692 mtx_lock(&sw_dev_mtx); 2693 TAILQ_FOREACH(sp, &swtailq, sw_list) { 2694 if (sp->sw_id == vp) { 2695 mtx_unlock(&sw_dev_mtx); 2696 return (EBUSY); 2697 } 2698 } 2699 mtx_unlock(&sw_dev_mtx); 2700 2701 (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2702 #ifdef MAC 2703 error = mac_system_check_swapon(td->td_ucred, vp); 2704 if (error == 0) 2705 #endif 2706 error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL); 2707 (void) VOP_UNLOCK(vp, 0); 2708 if (error) 2709 return (error); 2710 2711 swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close, 2712 NODEV); 2713 return (0); 2714 } 2715