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(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(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 pages; 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 (int i = 0; i < pages;) { 567 int j, a, count, rv; 568 569 vm_page_assert_xbusied(ma[i]); 570 if (ma[i]->valid == VM_PAGE_BITS_ALL) { 571 vm_page_xunbusy(ma[i]); 572 i++; 573 continue; 574 } 575 vm_object_pip_add(ksobj, 1); 576 for (j = i + 1; j < pages; j++) 577 if (ma[j]->valid == VM_PAGE_BITS_ALL) 578 break; 579 rv = vm_pager_has_page(ksobj, ma[i]->pindex, NULL, &a); 580 KASSERT(rv == 1, ("%s: missing page %p", __func__, ma[i])); 581 count = min(a + 1, j - i); 582 rv = vm_pager_get_pages(ksobj, ma + i, count, NULL, NULL); 583 KASSERT(rv == VM_PAGER_OK, ("%s: cannot get kstack for proc %d", 584 __func__, td->td_proc->p_pid)); 585 vm_object_pip_wakeup(ksobj); 586 for (j = i; j < i + count; j++) 587 vm_page_xunbusy(ma[j]); 588 i += count; 589 } 590 VM_OBJECT_WUNLOCK(ksobj); 591 pmap_qenter(td->td_kstack, ma, pages); 592 cpu_thread_swapin(td); 593 } 594 595 void 596 faultin(struct proc *p) 597 { 598 struct thread *td; 599 600 PROC_LOCK_ASSERT(p, MA_OWNED); 601 /* 602 * If another process is swapping in this process, 603 * just wait until it finishes. 604 */ 605 if (p->p_flag & P_SWAPPINGIN) { 606 while (p->p_flag & P_SWAPPINGIN) 607 msleep(&p->p_flag, &p->p_mtx, PVM, "faultin", 0); 608 return; 609 } 610 if ((p->p_flag & P_INMEM) == 0) { 611 /* 612 * Don't let another thread swap process p out while we are 613 * busy swapping it in. 614 */ 615 ++p->p_lock; 616 p->p_flag |= P_SWAPPINGIN; 617 PROC_UNLOCK(p); 618 619 /* 620 * We hold no lock here because the list of threads 621 * can not change while all threads in the process are 622 * swapped out. 623 */ 624 FOREACH_THREAD_IN_PROC(p, td) 625 vm_thread_swapin(td); 626 PROC_LOCK(p); 627 swapclear(p); 628 p->p_swtick = ticks; 629 630 wakeup(&p->p_flag); 631 632 /* Allow other threads to swap p out now. */ 633 --p->p_lock; 634 } 635 } 636 637 /* 638 * This swapin algorithm attempts to swap-in processes only if there 639 * is enough space for them. Of course, if a process waits for a long 640 * time, it will be swapped in anyway. 641 */ 642 void 643 swapper(void) 644 { 645 struct proc *p; 646 struct thread *td; 647 struct proc *pp; 648 int slptime; 649 int swtime; 650 int ppri; 651 int pri; 652 653 loop: 654 if (vm_page_count_min()) { 655 VM_WAIT; 656 goto loop; 657 } 658 659 pp = NULL; 660 ppri = INT_MIN; 661 sx_slock(&allproc_lock); 662 FOREACH_PROC_IN_SYSTEM(p) { 663 PROC_LOCK(p); 664 if (p->p_state == PRS_NEW || 665 p->p_flag & (P_SWAPPINGOUT | P_SWAPPINGIN | P_INMEM)) { 666 PROC_UNLOCK(p); 667 continue; 668 } 669 swtime = (ticks - p->p_swtick) / hz; 670 FOREACH_THREAD_IN_PROC(p, td) { 671 /* 672 * An otherwise runnable thread of a process 673 * swapped out has only the TDI_SWAPPED bit set. 674 */ 675 thread_lock(td); 676 if (td->td_inhibitors == TDI_SWAPPED) { 677 slptime = (ticks - td->td_slptick) / hz; 678 pri = swtime + slptime; 679 if ((td->td_flags & TDF_SWAPINREQ) == 0) 680 pri -= p->p_nice * 8; 681 /* 682 * if this thread is higher priority 683 * and there is enough space, then select 684 * this process instead of the previous 685 * selection. 686 */ 687 if (pri > ppri) { 688 pp = p; 689 ppri = pri; 690 } 691 } 692 thread_unlock(td); 693 } 694 PROC_UNLOCK(p); 695 } 696 sx_sunlock(&allproc_lock); 697 698 /* 699 * Nothing to do, back to sleep. 700 */ 701 if ((p = pp) == NULL) { 702 tsleep(&proc0, PVM, "swapin", MAXSLP * hz / 2); 703 goto loop; 704 } 705 PROC_LOCK(p); 706 707 /* 708 * Another process may be bringing or may have already 709 * brought this process in while we traverse all threads. 710 * Or, this process may even be being swapped out again. 711 */ 712 if (p->p_flag & (P_INMEM | P_SWAPPINGOUT | P_SWAPPINGIN)) { 713 PROC_UNLOCK(p); 714 goto loop; 715 } 716 717 /* 718 * We would like to bring someone in. (only if there is space). 719 * [What checks the space? ] 720 */ 721 faultin(p); 722 PROC_UNLOCK(p); 723 goto loop; 724 } 725 726 /* 727 * First, if any processes have been sleeping or stopped for at least 728 * "swap_idle_threshold1" seconds, they are swapped out. If, however, 729 * no such processes exist, then the longest-sleeping or stopped 730 * process is swapped out. Finally, and only as a last resort, if 731 * there are no sleeping or stopped processes, the longest-resident 732 * process is swapped out. 733 */ 734 static void 735 swapout_procs(int action) 736 { 737 struct proc *p; 738 struct thread *td; 739 int didswap = 0; 740 741 retry: 742 sx_slock(&allproc_lock); 743 FOREACH_PROC_IN_SYSTEM(p) { 744 struct vmspace *vm; 745 int minslptime = 100000; 746 int slptime; 747 748 PROC_LOCK(p); 749 /* 750 * Watch out for a process in 751 * creation. It may have no 752 * address space or lock yet. 753 */ 754 if (p->p_state == PRS_NEW) { 755 PROC_UNLOCK(p); 756 continue; 757 } 758 /* 759 * An aio daemon switches its 760 * address space while running. 761 * Perform a quick check whether 762 * a process has P_SYSTEM. 763 * Filter out exiting processes. 764 */ 765 if ((p->p_flag & (P_SYSTEM | P_WEXIT)) != 0) { 766 PROC_UNLOCK(p); 767 continue; 768 } 769 _PHOLD_LITE(p); 770 PROC_UNLOCK(p); 771 sx_sunlock(&allproc_lock); 772 773 /* 774 * Do not swapout a process that 775 * is waiting for VM data 776 * structures as there is a possible 777 * deadlock. Test this first as 778 * this may block. 779 * 780 * Lock the map until swapout 781 * finishes, or a thread of this 782 * process may attempt to alter 783 * the map. 784 */ 785 vm = vmspace_acquire_ref(p); 786 if (vm == NULL) 787 goto nextproc2; 788 if (!vm_map_trylock(&vm->vm_map)) 789 goto nextproc1; 790 791 PROC_LOCK(p); 792 if (p->p_lock != 1 || (p->p_flag & (P_STOPPED_SINGLE | 793 P_TRACED | P_SYSTEM)) != 0) 794 goto nextproc; 795 796 /* 797 * only aiod changes vmspace, however it will be 798 * skipped because of the if statement above checking 799 * for P_SYSTEM 800 */ 801 if ((p->p_flag & (P_INMEM|P_SWAPPINGOUT|P_SWAPPINGIN)) != P_INMEM) 802 goto nextproc; 803 804 switch (p->p_state) { 805 default: 806 /* Don't swap out processes in any sort 807 * of 'special' state. */ 808 break; 809 810 case PRS_NORMAL: 811 /* 812 * do not swapout a realtime process 813 * Check all the thread groups.. 814 */ 815 FOREACH_THREAD_IN_PROC(p, td) { 816 thread_lock(td); 817 if (PRI_IS_REALTIME(td->td_pri_class)) { 818 thread_unlock(td); 819 goto nextproc; 820 } 821 slptime = (ticks - td->td_slptick) / hz; 822 /* 823 * Guarantee swap_idle_threshold1 824 * time in memory. 825 */ 826 if (slptime < swap_idle_threshold1) { 827 thread_unlock(td); 828 goto nextproc; 829 } 830 831 /* 832 * Do not swapout a process if it is 833 * waiting on a critical event of some 834 * kind or there is a thread whose 835 * pageable memory may be accessed. 836 * 837 * This could be refined to support 838 * swapping out a thread. 839 */ 840 if (!thread_safetoswapout(td)) { 841 thread_unlock(td); 842 goto nextproc; 843 } 844 /* 845 * If the system is under memory stress, 846 * or if we are swapping 847 * idle processes >= swap_idle_threshold2, 848 * then swap the process out. 849 */ 850 if (((action & VM_SWAP_NORMAL) == 0) && 851 (((action & VM_SWAP_IDLE) == 0) || 852 (slptime < swap_idle_threshold2))) { 853 thread_unlock(td); 854 goto nextproc; 855 } 856 857 if (minslptime > slptime) 858 minslptime = slptime; 859 thread_unlock(td); 860 } 861 862 /* 863 * If the pageout daemon didn't free enough pages, 864 * or if this process is idle and the system is 865 * configured to swap proactively, swap it out. 866 */ 867 if ((action & VM_SWAP_NORMAL) || 868 ((action & VM_SWAP_IDLE) && 869 (minslptime > swap_idle_threshold2))) { 870 _PRELE(p); 871 if (swapout(p) == 0) 872 didswap++; 873 PROC_UNLOCK(p); 874 vm_map_unlock(&vm->vm_map); 875 vmspace_free(vm); 876 goto retry; 877 } 878 } 879 nextproc: 880 PROC_UNLOCK(p); 881 vm_map_unlock(&vm->vm_map); 882 nextproc1: 883 vmspace_free(vm); 884 nextproc2: 885 sx_slock(&allproc_lock); 886 PRELE(p); 887 } 888 sx_sunlock(&allproc_lock); 889 /* 890 * If we swapped something out, and another process needed memory, 891 * then wakeup the sched process. 892 */ 893 if (didswap) 894 wakeup(&proc0); 895 } 896 897 static void 898 swapclear(struct proc *p) 899 { 900 struct thread *td; 901 902 PROC_LOCK_ASSERT(p, MA_OWNED); 903 904 FOREACH_THREAD_IN_PROC(p, td) { 905 thread_lock(td); 906 td->td_flags |= TDF_INMEM; 907 td->td_flags &= ~TDF_SWAPINREQ; 908 TD_CLR_SWAPPED(td); 909 if (TD_CAN_RUN(td)) 910 if (setrunnable(td)) { 911 #ifdef INVARIANTS 912 /* 913 * XXX: We just cleared TDI_SWAPPED 914 * above and set TDF_INMEM, so this 915 * should never happen. 916 */ 917 panic("not waking up swapper"); 918 #endif 919 } 920 thread_unlock(td); 921 } 922 p->p_flag &= ~(P_SWAPPINGIN | P_SWAPPINGOUT); 923 p->p_flag |= P_INMEM; 924 } 925 926 static int 927 swapout(struct proc *p) 928 { 929 struct thread *td; 930 931 PROC_LOCK_ASSERT(p, MA_OWNED); 932 933 /* 934 * The states of this process and its threads may have changed 935 * by now. Assuming that there is only one pageout daemon thread, 936 * this process should still be in memory. 937 */ 938 KASSERT((p->p_flag & (P_INMEM | P_SWAPPINGOUT | P_SWAPPINGIN)) == 939 P_INMEM, ("swapout: lost a swapout race?")); 940 941 /* 942 * remember the process resident count 943 */ 944 p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace); 945 /* 946 * Check and mark all threads before we proceed. 947 */ 948 p->p_flag &= ~P_INMEM; 949 p->p_flag |= P_SWAPPINGOUT; 950 FOREACH_THREAD_IN_PROC(p, td) { 951 thread_lock(td); 952 if (!thread_safetoswapout(td)) { 953 thread_unlock(td); 954 swapclear(p); 955 return (EBUSY); 956 } 957 td->td_flags &= ~TDF_INMEM; 958 TD_SET_SWAPPED(td); 959 thread_unlock(td); 960 } 961 td = FIRST_THREAD_IN_PROC(p); 962 ++td->td_ru.ru_nswap; 963 PROC_UNLOCK(p); 964 965 /* 966 * This list is stable because all threads are now prevented from 967 * running. The list is only modified in the context of a running 968 * thread in this process. 969 */ 970 FOREACH_THREAD_IN_PROC(p, td) 971 vm_thread_swapout(td); 972 973 PROC_LOCK(p); 974 p->p_flag &= ~P_SWAPPINGOUT; 975 p->p_swtick = ticks; 976 return (0); 977 } 978