1 /*- 2 * SPDX-License-Identifier: (BSD-4-Clause AND MIT-CMU) 3 * 4 * Copyright (c) 1991 Regents of the University of California. 5 * All rights reserved. 6 * Copyright (c) 1994 John S. Dyson 7 * All rights reserved. 8 * Copyright (c) 1994 David Greenman 9 * All rights reserved. 10 * Copyright (c) 2005 Yahoo! Technologies Norway AS 11 * All rights reserved. 12 * 13 * This code is derived from software contributed to Berkeley by 14 * The Mach Operating System project at Carnegie-Mellon University. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. All advertising materials mentioning features or use of this software 25 * must display the following acknowledgement: 26 * This product includes software developed by the University of 27 * California, Berkeley and its contributors. 28 * 4. Neither the name of the University nor the names of its contributors 29 * may be used to endorse or promote products derived from this software 30 * without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 * 44 * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 45 * 46 * 47 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 48 * All rights reserved. 49 * 50 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 51 * 52 * Permission to use, copy, modify and distribute this software and 53 * its documentation is hereby granted, provided that both the copyright 54 * notice and this permission notice appear in all copies of the 55 * software, derivative works or modified versions, and any portions 56 * thereof, and that both notices appear in supporting documentation. 57 * 58 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 59 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 60 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 61 * 62 * Carnegie Mellon requests users of this software to return to 63 * 64 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 65 * School of Computer Science 66 * Carnegie Mellon University 67 * Pittsburgh PA 15213-3890 68 * 69 * any improvements or extensions that they make and grant Carnegie the 70 * rights to redistribute these changes. 71 */ 72 73 #include <sys/cdefs.h> 74 __FBSDID("$FreeBSD$"); 75 76 #include "opt_kstack_pages.h" 77 #include "opt_kstack_max_pages.h" 78 #include "opt_vm.h" 79 80 #include <sys/param.h> 81 #include <sys/systm.h> 82 #include <sys/limits.h> 83 #include <sys/kernel.h> 84 #include <sys/eventhandler.h> 85 #include <sys/lock.h> 86 #include <sys/mutex.h> 87 #include <sys/proc.h> 88 #include <sys/_kstack_cache.h> 89 #include <sys/kthread.h> 90 #include <sys/ktr.h> 91 #include <sys/mount.h> 92 #include <sys/racct.h> 93 #include <sys/resourcevar.h> 94 #include <sys/sched.h> 95 #include <sys/sdt.h> 96 #include <sys/signalvar.h> 97 #include <sys/smp.h> 98 #include <sys/time.h> 99 #include <sys/vnode.h> 100 #include <sys/vmmeter.h> 101 #include <sys/rwlock.h> 102 #include <sys/sx.h> 103 #include <sys/sysctl.h> 104 105 #include <vm/vm.h> 106 #include <vm/vm_param.h> 107 #include <vm/vm_object.h> 108 #include <vm/vm_page.h> 109 #include <vm/vm_map.h> 110 #include <vm/vm_pageout.h> 111 #include <vm/vm_pager.h> 112 #include <vm/vm_phys.h> 113 #include <vm/swap_pager.h> 114 #include <vm/vm_extern.h> 115 #include <vm/uma.h> 116 117 /* the kernel process "vm_daemon" */ 118 static void vm_daemon(void); 119 static struct proc *vmproc; 120 121 static struct kproc_desc vm_kp = { 122 "vmdaemon", 123 vm_daemon, 124 &vmproc 125 }; 126 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp); 127 128 static int vm_swap_enabled = 1; 129 static int vm_swap_idle_enabled = 0; 130 131 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, CTLFLAG_RW, 132 &vm_swap_enabled, 0, 133 "Enable entire process swapout"); 134 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, CTLFLAG_RW, 135 &vm_swap_idle_enabled, 0, 136 "Allow swapout on idle criteria"); 137 138 /* 139 * Swap_idle_threshold1 is the guaranteed swapped in time for a process 140 */ 141 static int swap_idle_threshold1 = 2; 142 SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, CTLFLAG_RW, 143 &swap_idle_threshold1, 0, 144 "Guaranteed swapped in time for a process"); 145 146 /* 147 * Swap_idle_threshold2 is the time that a process can be idle before 148 * it will be swapped out, if idle swapping is enabled. 149 */ 150 static int swap_idle_threshold2 = 10; 151 SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, CTLFLAG_RW, 152 &swap_idle_threshold2, 0, 153 "Time before a process will be swapped out"); 154 155 static int vm_pageout_req_swapout; /* XXX */ 156 static int vm_daemon_needed; 157 static struct mtx vm_daemon_mtx; 158 /* Allow for use by vm_pageout before vm_daemon is initialized. */ 159 MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF); 160 161 static void swapclear(struct proc *); 162 static int swapout(struct proc *); 163 static void vm_swapout_map_deactivate_pages(vm_map_t, long); 164 static void vm_swapout_object_deactivate_pages(pmap_t, vm_object_t, long); 165 static void swapout_procs(int action); 166 static void vm_req_vmdaemon(int req); 167 static void vm_thread_swapin(struct thread *td); 168 static void vm_thread_swapout(struct thread *td); 169 170 /* 171 * vm_swapout_object_deactivate_pages 172 * 173 * Deactivate enough pages to satisfy the inactive target 174 * requirements. 175 * 176 * The object and map must be locked. 177 */ 178 static void 179 vm_swapout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object, 180 long desired) 181 { 182 vm_object_t backing_object, object; 183 vm_page_t p; 184 int act_delta, remove_mode; 185 186 VM_OBJECT_ASSERT_LOCKED(first_object); 187 if ((first_object->flags & OBJ_FICTITIOUS) != 0) 188 return; 189 for (object = first_object;; object = backing_object) { 190 if (pmap_resident_count(pmap) <= desired) 191 goto unlock_return; 192 VM_OBJECT_ASSERT_LOCKED(object); 193 if ((object->flags & OBJ_UNMANAGED) != 0 || 194 object->paging_in_progress != 0) 195 goto unlock_return; 196 197 remove_mode = 0; 198 if (object->shadow_count > 1) 199 remove_mode = 1; 200 /* 201 * Scan the object's entire memory queue. 202 */ 203 TAILQ_FOREACH(p, &object->memq, listq) { 204 if (pmap_resident_count(pmap) <= desired) 205 goto unlock_return; 206 if (vm_page_busied(p)) 207 continue; 208 VM_CNT_INC(v_pdpages); 209 vm_page_lock(p); 210 if (p->wire_count != 0 || p->hold_count != 0 || 211 !pmap_page_exists_quick(pmap, p)) { 212 vm_page_unlock(p); 213 continue; 214 } 215 act_delta = pmap_ts_referenced(p); 216 if ((p->aflags & PGA_REFERENCED) != 0) { 217 if (act_delta == 0) 218 act_delta = 1; 219 vm_page_aflag_clear(p, PGA_REFERENCED); 220 } 221 if (!vm_page_active(p) && act_delta != 0) { 222 vm_page_activate(p); 223 p->act_count += act_delta; 224 } else if (vm_page_active(p)) { 225 if (act_delta == 0) { 226 p->act_count -= min(p->act_count, 227 ACT_DECLINE); 228 if (!remove_mode && p->act_count == 0) { 229 pmap_remove_all(p); 230 vm_page_deactivate(p); 231 } else 232 vm_page_requeue(p); 233 } else { 234 vm_page_activate(p); 235 if (p->act_count < ACT_MAX - 236 ACT_ADVANCE) 237 p->act_count += ACT_ADVANCE; 238 vm_page_requeue(p); 239 } 240 } else if (vm_page_inactive(p)) 241 pmap_remove_all(p); 242 vm_page_unlock(p); 243 } 244 if ((backing_object = object->backing_object) == NULL) 245 goto unlock_return; 246 VM_OBJECT_RLOCK(backing_object); 247 if (object != first_object) 248 VM_OBJECT_RUNLOCK(object); 249 } 250 unlock_return: 251 if (object != first_object) 252 VM_OBJECT_RUNLOCK(object); 253 } 254 255 /* 256 * deactivate some number of pages in a map, try to do it fairly, but 257 * that is really hard to do. 258 */ 259 static void 260 vm_swapout_map_deactivate_pages(vm_map_t map, long desired) 261 { 262 vm_map_entry_t tmpe; 263 vm_object_t obj, bigobj; 264 int nothingwired; 265 266 if (!vm_map_trylock_read(map)) 267 return; 268 269 bigobj = NULL; 270 nothingwired = TRUE; 271 272 /* 273 * first, search out the biggest object, and try to free pages from 274 * that. 275 */ 276 tmpe = map->header.next; 277 while (tmpe != &map->header) { 278 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 279 obj = tmpe->object.vm_object; 280 if (obj != NULL && VM_OBJECT_TRYRLOCK(obj)) { 281 if (obj->shadow_count <= 1 && 282 (bigobj == NULL || 283 bigobj->resident_page_count < 284 obj->resident_page_count)) { 285 if (bigobj != NULL) 286 VM_OBJECT_RUNLOCK(bigobj); 287 bigobj = obj; 288 } else 289 VM_OBJECT_RUNLOCK(obj); 290 } 291 } 292 if (tmpe->wired_count > 0) 293 nothingwired = FALSE; 294 tmpe = tmpe->next; 295 } 296 297 if (bigobj != NULL) { 298 vm_swapout_object_deactivate_pages(map->pmap, bigobj, desired); 299 VM_OBJECT_RUNLOCK(bigobj); 300 } 301 /* 302 * Next, hunt around for other pages to deactivate. We actually 303 * do this search sort of wrong -- .text first is not the best idea. 304 */ 305 tmpe = map->header.next; 306 while (tmpe != &map->header) { 307 if (pmap_resident_count(vm_map_pmap(map)) <= desired) 308 break; 309 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 310 obj = tmpe->object.vm_object; 311 if (obj != NULL) { 312 VM_OBJECT_RLOCK(obj); 313 vm_swapout_object_deactivate_pages(map->pmap, 314 obj, desired); 315 VM_OBJECT_RUNLOCK(obj); 316 } 317 } 318 tmpe = tmpe->next; 319 } 320 321 /* 322 * Remove all mappings if a process is swapped out, this will free page 323 * table pages. 324 */ 325 if (desired == 0 && nothingwired) { 326 pmap_remove(vm_map_pmap(map), vm_map_min(map), 327 vm_map_max(map)); 328 } 329 330 vm_map_unlock_read(map); 331 } 332 333 /* 334 * Swap out requests 335 */ 336 #define VM_SWAP_NORMAL 1 337 #define VM_SWAP_IDLE 2 338 339 void 340 vm_swapout_run(void) 341 { 342 343 if (vm_swap_enabled) 344 vm_req_vmdaemon(VM_SWAP_NORMAL); 345 } 346 347 /* 348 * Idle process swapout -- run once per second when pagedaemons are 349 * reclaiming pages. 350 */ 351 void 352 vm_swapout_run_idle(void) 353 { 354 static long lsec; 355 356 if (!vm_swap_idle_enabled || time_second == lsec) 357 return; 358 vm_req_vmdaemon(VM_SWAP_IDLE); 359 lsec = time_second; 360 } 361 362 static void 363 vm_req_vmdaemon(int req) 364 { 365 static int lastrun = 0; 366 367 mtx_lock(&vm_daemon_mtx); 368 vm_pageout_req_swapout |= req; 369 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 370 wakeup(&vm_daemon_needed); 371 lastrun = ticks; 372 } 373 mtx_unlock(&vm_daemon_mtx); 374 } 375 376 static void 377 vm_daemon(void) 378 { 379 struct rlimit rsslim; 380 struct proc *p; 381 struct thread *td; 382 struct vmspace *vm; 383 int breakout, swapout_flags, tryagain, attempts; 384 #ifdef RACCT 385 uint64_t rsize, ravailable; 386 #endif 387 388 while (TRUE) { 389 mtx_lock(&vm_daemon_mtx); 390 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 391 #ifdef RACCT 392 racct_enable ? hz : 0 393 #else 394 0 395 #endif 396 ); 397 swapout_flags = vm_pageout_req_swapout; 398 vm_pageout_req_swapout = 0; 399 mtx_unlock(&vm_daemon_mtx); 400 if (swapout_flags) 401 swapout_procs(swapout_flags); 402 403 /* 404 * scan the processes for exceeding their rlimits or if 405 * process is swapped out -- deactivate pages 406 */ 407 tryagain = 0; 408 attempts = 0; 409 again: 410 attempts++; 411 sx_slock(&allproc_lock); 412 FOREACH_PROC_IN_SYSTEM(p) { 413 vm_pindex_t limit, size; 414 415 /* 416 * if this is a system process or if we have already 417 * looked at this process, skip it. 418 */ 419 PROC_LOCK(p); 420 if (p->p_state != PRS_NORMAL || 421 p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) { 422 PROC_UNLOCK(p); 423 continue; 424 } 425 /* 426 * if the process is in a non-running type state, 427 * don't touch it. 428 */ 429 breakout = 0; 430 FOREACH_THREAD_IN_PROC(p, td) { 431 thread_lock(td); 432 if (!TD_ON_RUNQ(td) && 433 !TD_IS_RUNNING(td) && 434 !TD_IS_SLEEPING(td) && 435 !TD_IS_SUSPENDED(td)) { 436 thread_unlock(td); 437 breakout = 1; 438 break; 439 } 440 thread_unlock(td); 441 } 442 if (breakout) { 443 PROC_UNLOCK(p); 444 continue; 445 } 446 /* 447 * get a limit 448 */ 449 lim_rlimit_proc(p, RLIMIT_RSS, &rsslim); 450 limit = OFF_TO_IDX( 451 qmin(rsslim.rlim_cur, rsslim.rlim_max)); 452 453 /* 454 * let processes that are swapped out really be 455 * swapped out set the limit to nothing (will force a 456 * swap-out.) 457 */ 458 if ((p->p_flag & P_INMEM) == 0) 459 limit = 0; /* XXX */ 460 vm = vmspace_acquire_ref(p); 461 _PHOLD_LITE(p); 462 PROC_UNLOCK(p); 463 if (vm == NULL) { 464 PRELE(p); 465 continue; 466 } 467 sx_sunlock(&allproc_lock); 468 469 size = vmspace_resident_count(vm); 470 if (size >= limit) { 471 vm_swapout_map_deactivate_pages( 472 &vm->vm_map, limit); 473 size = vmspace_resident_count(vm); 474 } 475 #ifdef RACCT 476 if (racct_enable) { 477 rsize = IDX_TO_OFF(size); 478 PROC_LOCK(p); 479 if (p->p_state == PRS_NORMAL) 480 racct_set(p, RACCT_RSS, rsize); 481 ravailable = racct_get_available(p, RACCT_RSS); 482 PROC_UNLOCK(p); 483 if (rsize > ravailable) { 484 /* 485 * Don't be overly aggressive; this 486 * might be an innocent process, 487 * and the limit could've been exceeded 488 * by some memory hog. Don't try 489 * to deactivate more than 1/4th 490 * of process' resident set size. 491 */ 492 if (attempts <= 8) { 493 if (ravailable < rsize - 494 (rsize / 4)) { 495 ravailable = rsize - 496 (rsize / 4); 497 } 498 } 499 vm_swapout_map_deactivate_pages( 500 &vm->vm_map, 501 OFF_TO_IDX(ravailable)); 502 /* Update RSS usage after paging out. */ 503 size = vmspace_resident_count(vm); 504 rsize = IDX_TO_OFF(size); 505 PROC_LOCK(p); 506 if (p->p_state == PRS_NORMAL) 507 racct_set(p, RACCT_RSS, rsize); 508 PROC_UNLOCK(p); 509 if (rsize > ravailable) 510 tryagain = 1; 511 } 512 } 513 #endif 514 vmspace_free(vm); 515 sx_slock(&allproc_lock); 516 PRELE(p); 517 } 518 sx_sunlock(&allproc_lock); 519 if (tryagain != 0 && attempts <= 10) 520 goto again; 521 } 522 } 523 524 /* 525 * Allow a thread's kernel stack to be paged out. 526 */ 527 static void 528 vm_thread_swapout(struct thread *td) 529 { 530 vm_object_t ksobj; 531 vm_page_t m; 532 int i, pages; 533 534 cpu_thread_swapout(td); 535 pages = td->td_kstack_pages; 536 ksobj = td->td_kstack_obj; 537 pmap_qremove(td->td_kstack, pages); 538 VM_OBJECT_WLOCK(ksobj); 539 for (i = 0; i < pages; i++) { 540 m = vm_page_lookup(ksobj, i); 541 if (m == NULL) 542 panic("vm_thread_swapout: kstack already missing?"); 543 vm_page_dirty(m); 544 vm_page_lock(m); 545 vm_page_unwire(m, PQ_INACTIVE); 546 vm_page_unlock(m); 547 } 548 VM_OBJECT_WUNLOCK(ksobj); 549 } 550 551 /* 552 * Bring the kernel stack for a specified thread back in. 553 */ 554 static void 555 vm_thread_swapin(struct thread *td) 556 { 557 vm_object_t ksobj; 558 vm_page_t ma[KSTACK_MAX_PAGES]; 559 int a, count, i, j, pages, rv; 560 561 pages = td->td_kstack_pages; 562 ksobj = td->td_kstack_obj; 563 VM_OBJECT_WLOCK(ksobj); 564 (void)vm_page_grab_pages(ksobj, 0, VM_ALLOC_NORMAL | VM_ALLOC_WIRED, ma, 565 pages); 566 for (i = 0; i < pages;) { 567 vm_page_assert_xbusied(ma[i]); 568 if (ma[i]->valid == VM_PAGE_BITS_ALL) { 569 vm_page_xunbusy(ma[i]); 570 i++; 571 continue; 572 } 573 vm_object_pip_add(ksobj, 1); 574 for (j = i + 1; j < pages; j++) 575 if (ma[j]->valid == VM_PAGE_BITS_ALL) 576 break; 577 rv = vm_pager_has_page(ksobj, ma[i]->pindex, NULL, &a); 578 KASSERT(rv == 1, ("%s: missing page %p", __func__, ma[i])); 579 count = min(a + 1, j - i); 580 rv = vm_pager_get_pages(ksobj, ma + i, count, NULL, NULL); 581 KASSERT(rv == VM_PAGER_OK, ("%s: cannot get kstack for proc %d", 582 __func__, td->td_proc->p_pid)); 583 vm_object_pip_wakeup(ksobj); 584 for (j = i; j < i + count; j++) 585 vm_page_xunbusy(ma[j]); 586 i += count; 587 } 588 VM_OBJECT_WUNLOCK(ksobj); 589 pmap_qenter(td->td_kstack, ma, pages); 590 cpu_thread_swapin(td); 591 } 592 593 void 594 faultin(struct proc *p) 595 { 596 struct thread *td; 597 598 PROC_LOCK_ASSERT(p, MA_OWNED); 599 /* 600 * If another process is swapping in this process, 601 * just wait until it finishes. 602 */ 603 if (p->p_flag & P_SWAPPINGIN) { 604 while (p->p_flag & P_SWAPPINGIN) 605 msleep(&p->p_flag, &p->p_mtx, PVM, "faultin", 0); 606 return; 607 } 608 if ((p->p_flag & P_INMEM) == 0) { 609 /* 610 * Don't let another thread swap process p out while we are 611 * busy swapping it in. 612 */ 613 ++p->p_lock; 614 p->p_flag |= P_SWAPPINGIN; 615 PROC_UNLOCK(p); 616 617 /* 618 * We hold no lock here because the list of threads 619 * can not change while all threads in the process are 620 * swapped out. 621 */ 622 FOREACH_THREAD_IN_PROC(p, td) 623 vm_thread_swapin(td); 624 PROC_LOCK(p); 625 swapclear(p); 626 p->p_swtick = ticks; 627 628 wakeup(&p->p_flag); 629 630 /* Allow other threads to swap p out now. */ 631 --p->p_lock; 632 } 633 } 634 635 /* 636 * This swapin algorithm attempts to swap-in processes only if there 637 * is enough space for them. Of course, if a process waits for a long 638 * time, it will be swapped in anyway. 639 */ 640 void 641 swapper(void) 642 { 643 struct proc *p, *pp; 644 struct thread *td; 645 int ppri, pri, slptime, swtime; 646 647 loop: 648 if (vm_page_count_min()) { 649 VM_WAIT; 650 goto loop; 651 } 652 653 pp = NULL; 654 ppri = INT_MIN; 655 sx_slock(&allproc_lock); 656 FOREACH_PROC_IN_SYSTEM(p) { 657 PROC_LOCK(p); 658 if (p->p_state == PRS_NEW || 659 p->p_flag & (P_SWAPPINGOUT | P_SWAPPINGIN | P_INMEM)) { 660 PROC_UNLOCK(p); 661 continue; 662 } 663 swtime = (ticks - p->p_swtick) / hz; 664 FOREACH_THREAD_IN_PROC(p, td) { 665 /* 666 * An otherwise runnable thread of a process 667 * swapped out has only the TDI_SWAPPED bit set. 668 */ 669 thread_lock(td); 670 if (td->td_inhibitors == TDI_SWAPPED) { 671 slptime = (ticks - td->td_slptick) / hz; 672 pri = swtime + slptime; 673 if ((td->td_flags & TDF_SWAPINREQ) == 0) 674 pri -= p->p_nice * 8; 675 /* 676 * if this thread is higher priority 677 * and there is enough space, then select 678 * this process instead of the previous 679 * selection. 680 */ 681 if (pri > ppri) { 682 pp = p; 683 ppri = pri; 684 } 685 } 686 thread_unlock(td); 687 } 688 PROC_UNLOCK(p); 689 } 690 sx_sunlock(&allproc_lock); 691 692 /* 693 * Nothing to do, back to sleep. 694 */ 695 if ((p = pp) == NULL) { 696 tsleep(&proc0, PVM, "swapin", MAXSLP * hz / 2); 697 goto loop; 698 } 699 PROC_LOCK(p); 700 701 /* 702 * Another process may be bringing or may have already 703 * brought this process in while we traverse all threads. 704 * Or, this process may even be being swapped out again. 705 */ 706 if (p->p_flag & (P_INMEM | P_SWAPPINGOUT | P_SWAPPINGIN)) { 707 PROC_UNLOCK(p); 708 goto loop; 709 } 710 711 /* 712 * We would like to bring someone in. 713 */ 714 faultin(p); 715 PROC_UNLOCK(p); 716 goto loop; 717 } 718 719 /* 720 * First, if any processes have been sleeping or stopped for at least 721 * "swap_idle_threshold1" seconds, they are swapped out. If, however, 722 * no such processes exist, then the longest-sleeping or stopped 723 * process is swapped out. Finally, and only as a last resort, if 724 * there are no sleeping or stopped processes, the longest-resident 725 * process is swapped out. 726 */ 727 static void 728 swapout_procs(int action) 729 { 730 struct proc *p; 731 struct thread *td; 732 int minslptime, slptime; 733 bool didswap; 734 735 minslptime = 100000; 736 didswap = false; 737 retry: 738 sx_slock(&allproc_lock); 739 FOREACH_PROC_IN_SYSTEM(p) { 740 PROC_LOCK(p); 741 /* 742 * Watch out for a process in 743 * creation. It may have no 744 * address space or lock yet. 745 */ 746 if (p->p_state == PRS_NEW) { 747 PROC_UNLOCK(p); 748 continue; 749 } 750 /* 751 * An aio daemon switches its 752 * address space while running. 753 * Perform a quick check whether 754 * a process has P_SYSTEM. 755 * Filter out exiting processes. 756 */ 757 if ((p->p_flag & (P_SYSTEM | P_WEXIT)) != 0) { 758 PROC_UNLOCK(p); 759 continue; 760 } 761 _PHOLD_LITE(p); 762 PROC_UNLOCK(p); 763 sx_sunlock(&allproc_lock); 764 765 PROC_LOCK(p); 766 if (p->p_lock != 1 || (p->p_flag & (P_STOPPED_SINGLE | 767 P_TRACED | P_SYSTEM)) != 0) 768 goto nextproc; 769 770 /* 771 * Only aiod changes vmspace. However, it will be 772 * skipped because of the if statement above checking 773 * for P_SYSTEM. 774 */ 775 if ((p->p_flag & (P_INMEM | P_SWAPPINGOUT | P_SWAPPINGIN)) != 776 P_INMEM) 777 goto nextproc; 778 779 switch (p->p_state) { 780 default: 781 /* 782 * Don't swap out processes in any sort 783 * of 'special' state. 784 */ 785 break; 786 787 case PRS_NORMAL: 788 /* 789 * do not swapout a realtime process 790 * Check all the thread groups.. 791 */ 792 FOREACH_THREAD_IN_PROC(p, td) { 793 thread_lock(td); 794 if (PRI_IS_REALTIME(td->td_pri_class)) { 795 thread_unlock(td); 796 goto nextproc; 797 } 798 slptime = (ticks - td->td_slptick) / hz; 799 /* 800 * Guarantee swap_idle_threshold1 801 * time in memory. 802 */ 803 if (slptime < swap_idle_threshold1) { 804 thread_unlock(td); 805 goto nextproc; 806 } 807 808 /* 809 * Do not swapout a process if it is 810 * waiting on a critical event of some 811 * kind or there is a thread whose 812 * pageable memory may be accessed. 813 * 814 * This could be refined to support 815 * swapping out a thread. 816 */ 817 if (!thread_safetoswapout(td)) { 818 thread_unlock(td); 819 goto nextproc; 820 } 821 /* 822 * If the system is under memory stress, 823 * or if we are swapping 824 * idle processes >= swap_idle_threshold2, 825 * then swap the process out. 826 */ 827 if ((action & VM_SWAP_NORMAL) == 0 && 828 ((action & VM_SWAP_IDLE) == 0 || 829 slptime < swap_idle_threshold2)) { 830 thread_unlock(td); 831 goto nextproc; 832 } 833 834 if (minslptime > slptime) 835 minslptime = slptime; 836 thread_unlock(td); 837 } 838 839 /* 840 * If the pageout daemon didn't free enough pages, 841 * or if this process is idle and the system is 842 * configured to swap proactively, swap it out. 843 */ 844 if ((action & VM_SWAP_NORMAL) != 0 || 845 ((action & VM_SWAP_IDLE) != 0 && 846 minslptime > swap_idle_threshold2)) { 847 _PRELE(p); 848 if (swapout(p) == 0) 849 didswap = true; 850 PROC_UNLOCK(p); 851 goto retry; 852 } 853 } 854 nextproc: 855 PROC_UNLOCK(p); 856 sx_slock(&allproc_lock); 857 PRELE(p); 858 } 859 sx_sunlock(&allproc_lock); 860 /* 861 * If we swapped something out, and another process needed memory, 862 * then wakeup the sched process. 863 */ 864 if (didswap) 865 wakeup(&proc0); 866 } 867 868 static void 869 swapclear(struct proc *p) 870 { 871 struct thread *td; 872 873 PROC_LOCK_ASSERT(p, MA_OWNED); 874 875 FOREACH_THREAD_IN_PROC(p, td) { 876 thread_lock(td); 877 td->td_flags |= TDF_INMEM; 878 td->td_flags &= ~TDF_SWAPINREQ; 879 TD_CLR_SWAPPED(td); 880 if (TD_CAN_RUN(td)) 881 if (setrunnable(td)) { 882 #ifdef INVARIANTS 883 /* 884 * XXX: We just cleared TDI_SWAPPED 885 * above and set TDF_INMEM, so this 886 * should never happen. 887 */ 888 panic("not waking up swapper"); 889 #endif 890 } 891 thread_unlock(td); 892 } 893 p->p_flag &= ~(P_SWAPPINGIN | P_SWAPPINGOUT); 894 p->p_flag |= P_INMEM; 895 } 896 897 static int 898 swapout(struct proc *p) 899 { 900 struct thread *td; 901 902 PROC_LOCK_ASSERT(p, MA_OWNED); 903 904 /* 905 * The states of this process and its threads may have changed 906 * by now. Assuming that there is only one pageout daemon thread, 907 * this process should still be in memory. 908 */ 909 KASSERT((p->p_flag & (P_INMEM | P_SWAPPINGOUT | P_SWAPPINGIN)) == 910 P_INMEM, ("swapout: lost a swapout race?")); 911 912 /* 913 * Remember the resident count. 914 */ 915 p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace); 916 917 /* 918 * Check and mark all threads before we proceed. 919 */ 920 p->p_flag &= ~P_INMEM; 921 p->p_flag |= P_SWAPPINGOUT; 922 FOREACH_THREAD_IN_PROC(p, td) { 923 thread_lock(td); 924 if (!thread_safetoswapout(td)) { 925 thread_unlock(td); 926 swapclear(p); 927 return (EBUSY); 928 } 929 td->td_flags &= ~TDF_INMEM; 930 TD_SET_SWAPPED(td); 931 thread_unlock(td); 932 } 933 td = FIRST_THREAD_IN_PROC(p); 934 ++td->td_ru.ru_nswap; 935 PROC_UNLOCK(p); 936 937 /* 938 * This list is stable because all threads are now prevented from 939 * running. The list is only modified in the context of a running 940 * thread in this process. 941 */ 942 FOREACH_THREAD_IN_PROC(p, td) 943 vm_thread_swapout(td); 944 945 PROC_LOCK(p); 946 p->p_flag &= ~P_SWAPPINGOUT; 947 p->p_swtick = ticks; 948 return (0); 949 } 950