FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_swapout.c
1 /*-
2 * Copyright (c) 1991 Regents of the University of California.
3 * All rights reserved.
4 * Copyright (c) 1994 John S. Dyson
5 * All rights reserved.
6 * Copyright (c) 1994 David Greenman
7 * All rights reserved.
8 * Copyright (c) 2005 Yahoo! Technologies Norway AS
9 * All rights reserved.
10 *
11 * This code is derived from software contributed to Berkeley by
12 * The Mach Operating System project at Carnegie-Mellon University.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91
43 *
44 *
45 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 * All rights reserved.
47 *
48 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
49 *
50 * Permission to use, copy, modify and distribute this software and
51 * its documentation is hereby granted, provided that both the copyright
52 * notice and this permission notice appear in all copies of the
53 * software, derivative works or modified versions, and any portions
54 * thereof, and that both notices appear in supporting documentation.
55 *
56 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
57 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
58 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
59 *
60 * Carnegie Mellon requests users of this software to return to
61 *
62 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
63 * School of Computer Science
64 * Carnegie Mellon University
65 * Pittsburgh PA 15213-3890
66 *
67 * any improvements or extensions that they make and grant Carnegie the
68 * rights to redistribute these changes.
69 */
70
71 #include <sys/cdefs.h>
72 __FBSDID("$FreeBSD: releng/11.2/sys/vm/vm_swapout.c 328122 2018-01-18 12:06:01Z kib $");
73
74 #include "opt_kstack_pages.h"
75 #include "opt_kstack_max_pages.h"
76 #include "opt_vm.h"
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/limits.h>
81 #include <sys/kernel.h>
82 #include <sys/eventhandler.h>
83 #include <sys/lock.h>
84 #include <sys/mutex.h>
85 #include <sys/proc.h>
86 #include <sys/_kstack_cache.h>
87 #include <sys/kthread.h>
88 #include <sys/ktr.h>
89 #include <sys/mount.h>
90 #include <sys/racct.h>
91 #include <sys/resourcevar.h>
92 #include <sys/sched.h>
93 #include <sys/sdt.h>
94 #include <sys/signalvar.h>
95 #include <sys/smp.h>
96 #include <sys/time.h>
97 #include <sys/vnode.h>
98 #include <sys/vmmeter.h>
99 #include <sys/rwlock.h>
100 #include <sys/sx.h>
101 #include <sys/sysctl.h>
102
103 #include <vm/vm.h>
104 #include <vm/vm_param.h>
105 #include <vm/vm_object.h>
106 #include <vm/vm_page.h>
107 #include <vm/vm_map.h>
108 #include <vm/vm_pageout.h>
109 #include <vm/vm_pager.h>
110 #include <vm/vm_phys.h>
111 #include <vm/swap_pager.h>
112 #include <vm/vm_extern.h>
113 #include <vm/uma.h>
114
115 /* the kernel process "vm_daemon" */
116 static void vm_daemon(void);
117 static struct proc *vmproc;
118
119 static struct kproc_desc vm_kp = {
120 "vmdaemon",
121 vm_daemon,
122 &vmproc
123 };
124 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp);
125
126 static int vm_swap_enabled = 1;
127 static int vm_swap_idle_enabled = 0;
128
129 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, CTLFLAG_RW,
130 &vm_swap_enabled, 0,
131 "Enable entire process swapout");
132 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, CTLFLAG_RW,
133 &vm_swap_idle_enabled, 0,
134 "Allow swapout on idle criteria");
135
136 /*
137 * Swap_idle_threshold1 is the guaranteed swapped in time for a process
138 */
139 static int swap_idle_threshold1 = 2;
140 SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, CTLFLAG_RW,
141 &swap_idle_threshold1, 0,
142 "Guaranteed swapped in time for a process");
143
144 /*
145 * Swap_idle_threshold2 is the time that a process can be idle before
146 * it will be swapped out, if idle swapping is enabled.
147 */
148 static int swap_idle_threshold2 = 10;
149 SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, CTLFLAG_RW,
150 &swap_idle_threshold2, 0,
151 "Time before a process will be swapped out");
152
153 static int vm_pageout_req_swapout; /* XXX */
154 static int vm_daemon_needed;
155 static struct mtx vm_daemon_mtx;
156 /* Allow for use by vm_pageout before vm_daemon is initialized. */
157 MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF);
158
159 static void swapclear(struct proc *);
160 static int swapout(struct proc *);
161 static void vm_swapout_map_deactivate_pages(vm_map_t, long);
162 static void vm_swapout_object_deactivate_pages(pmap_t, vm_object_t, long);
163 static void swapout_procs(int action);
164 static void vm_req_vmdaemon(int req);
165 static void vm_thread_swapin(struct thread *td);
166 static void vm_thread_swapout(struct thread *td);
167
168 /*
169 * vm_swapout_object_deactivate_pages
170 *
171 * Deactivate enough pages to satisfy the inactive target
172 * requirements.
173 *
174 * The object and map must be locked.
175 */
176 static void
177 vm_swapout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object,
178 long desired)
179 {
180 vm_object_t backing_object, object;
181 vm_page_t p;
182 int act_delta, remove_mode;
183
184 VM_OBJECT_ASSERT_LOCKED(first_object);
185 if ((first_object->flags & OBJ_FICTITIOUS) != 0)
186 return;
187 for (object = first_object;; object = backing_object) {
188 if (pmap_resident_count(pmap) <= desired)
189 goto unlock_return;
190 VM_OBJECT_ASSERT_LOCKED(object);
191 if ((object->flags & OBJ_UNMANAGED) != 0 ||
192 object->paging_in_progress != 0)
193 goto unlock_return;
194
195 remove_mode = 0;
196 if (object->shadow_count > 1)
197 remove_mode = 1;
198 /*
199 * Scan the object's entire memory queue.
200 */
201 TAILQ_FOREACH(p, &object->memq, listq) {
202 if (pmap_resident_count(pmap) <= desired)
203 goto unlock_return;
204 if (should_yield())
205 goto unlock_return;
206 if (vm_page_busied(p))
207 continue;
208 PCPU_INC(cnt.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 maybe_yield();
521 goto again;
522 }
523 }
524 }
525
526 /*
527 * Allow a thread's kernel stack to be paged out.
528 */
529 static void
530 vm_thread_swapout(struct thread *td)
531 {
532 vm_object_t ksobj;
533 vm_page_t m;
534 int i, pages;
535
536 cpu_thread_swapout(td);
537 pages = td->td_kstack_pages;
538 ksobj = td->td_kstack_obj;
539 pmap_qremove(td->td_kstack, pages);
540 VM_OBJECT_WLOCK(ksobj);
541 for (i = 0; i < pages; i++) {
542 m = vm_page_lookup(ksobj, i);
543 if (m == NULL)
544 panic("vm_thread_swapout: kstack already missing?");
545 vm_page_dirty(m);
546 vm_page_lock(m);
547 vm_page_unwire(m, PQ_INACTIVE);
548 vm_page_unlock(m);
549 }
550 VM_OBJECT_WUNLOCK(ksobj);
551 }
552
553 /*
554 * Bring the kernel stack for a specified thread back in.
555 */
556 static void
557 vm_thread_swapin(struct thread *td)
558 {
559 vm_object_t ksobj;
560 vm_page_t ma[KSTACK_MAX_PAGES];
561 int a, count, i, j, pages, rv;
562
563 pages = td->td_kstack_pages;
564 ksobj = td->td_kstack_obj;
565 VM_OBJECT_WLOCK(ksobj);
566 (void)vm_page_grab_pages(ksobj, 0, VM_ALLOC_NORMAL | VM_ALLOC_WIRED, ma,
567 pages);
568 for (i = 0; i < pages;) {
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, *pp;
646 struct thread *td;
647 int ppri, pri, slptime, swtime;
648
649 loop:
650 if (vm_page_count_min()) {
651 VM_WAIT;
652 goto loop;
653 }
654
655 pp = NULL;
656 ppri = INT_MIN;
657 sx_slock(&allproc_lock);
658 FOREACH_PROC_IN_SYSTEM(p) {
659 PROC_LOCK(p);
660 if (p->p_state == PRS_NEW ||
661 p->p_flag & (P_SWAPPINGOUT | P_SWAPPINGIN | P_INMEM)) {
662 PROC_UNLOCK(p);
663 continue;
664 }
665 swtime = (ticks - p->p_swtick) / hz;
666 FOREACH_THREAD_IN_PROC(p, td) {
667 /*
668 * An otherwise runnable thread of a process
669 * swapped out has only the TDI_SWAPPED bit set.
670 */
671 thread_lock(td);
672 if (td->td_inhibitors == TDI_SWAPPED) {
673 slptime = (ticks - td->td_slptick) / hz;
674 pri = swtime + slptime;
675 if ((td->td_flags & TDF_SWAPINREQ) == 0)
676 pri -= p->p_nice * 8;
677 /*
678 * if this thread is higher priority
679 * and there is enough space, then select
680 * this process instead of the previous
681 * selection.
682 */
683 if (pri > ppri) {
684 pp = p;
685 ppri = pri;
686 }
687 }
688 thread_unlock(td);
689 }
690 PROC_UNLOCK(p);
691 }
692 sx_sunlock(&allproc_lock);
693
694 /*
695 * Nothing to do, back to sleep.
696 */
697 if ((p = pp) == NULL) {
698 tsleep(&proc0, PVM, "swapin", MAXSLP * hz / 2);
699 goto loop;
700 }
701 PROC_LOCK(p);
702
703 /*
704 * Another process may be bringing or may have already
705 * brought this process in while we traverse all threads.
706 * Or, this process may even be being swapped out again.
707 */
708 if (p->p_flag & (P_INMEM | P_SWAPPINGOUT | P_SWAPPINGIN)) {
709 PROC_UNLOCK(p);
710 goto loop;
711 }
712
713 /*
714 * We would like to bring someone in.
715 */
716 faultin(p);
717 PROC_UNLOCK(p);
718 goto loop;
719 }
720
721 /*
722 * First, if any processes have been sleeping or stopped for at least
723 * "swap_idle_threshold1" seconds, they are swapped out. If, however,
724 * no such processes exist, then the longest-sleeping or stopped
725 * process is swapped out. Finally, and only as a last resort, if
726 * there are no sleeping or stopped processes, the longest-resident
727 * process is swapped out.
728 */
729 static void
730 swapout_procs(int action)
731 {
732 struct proc *p;
733 struct thread *td;
734 int slptime;
735 bool didswap, doswap;
736
737 MPASS((action & (VM_SWAP_NORMAL | VM_SWAP_IDLE)) != 0);
738
739 didswap = false;
740 sx_slock(&allproc_lock);
741 FOREACH_PROC_IN_SYSTEM(p) {
742 /*
743 * Filter out not yet fully constructed processes. Do
744 * not swap out held processes. Avoid processes which
745 * are system, exiting, execing, traced, already swapped
746 * out or are in the process of being swapped in or out.
747 */
748 PROC_LOCK(p);
749 if (p->p_state != PRS_NORMAL || p->p_lock != 0 || (p->p_flag &
750 (P_SYSTEM | P_WEXIT | P_INEXEC | P_STOPPED_SINGLE |
751 P_TRACED | P_SWAPPINGOUT | P_SWAPPINGIN | P_INMEM)) !=
752 P_INMEM) {
753 PROC_UNLOCK(p);
754 continue;
755 }
756
757 /*
758 * Further consideration of this process for swap out
759 * requires iterating over its threads. We release
760 * allproc_lock here so that process creation and
761 * destruction are not blocked while we iterate.
762 *
763 * To later reacquire allproc_lock and resume
764 * iteration over the allproc list, we will first have
765 * to release the lock on the process. We place a
766 * hold on the process so that it remains in the
767 * allproc list while it is unlocked.
768 */
769 _PHOLD_LITE(p);
770 sx_sunlock(&allproc_lock);
771
772 /*
773 * Do not swapout a realtime process.
774 * Guarantee swap_idle_threshold1 time in memory.
775 * If the system is under memory stress, or if we are
776 * swapping idle processes >= swap_idle_threshold2,
777 * then swap the process out.
778 */
779 doswap = true;
780 FOREACH_THREAD_IN_PROC(p, td) {
781 thread_lock(td);
782 slptime = (ticks - td->td_slptick) / hz;
783 if (PRI_IS_REALTIME(td->td_pri_class) ||
784 slptime < swap_idle_threshold1 ||
785 !thread_safetoswapout(td) ||
786 ((action & VM_SWAP_NORMAL) == 0 &&
787 slptime < swap_idle_threshold2))
788 doswap = false;
789 thread_unlock(td);
790 if (!doswap)
791 break;
792 }
793 if (doswap && swapout(p) == 0)
794 didswap = true;
795
796 PROC_UNLOCK(p);
797 sx_slock(&allproc_lock);
798 PRELE(p);
799 }
800 sx_sunlock(&allproc_lock);
801
802 /*
803 * If we swapped something out, and another process needed memory,
804 * then wakeup the sched process.
805 */
806 if (didswap)
807 wakeup(&proc0);
808 }
809
810 static void
811 swapclear(struct proc *p)
812 {
813 struct thread *td;
814
815 PROC_LOCK_ASSERT(p, MA_OWNED);
816
817 FOREACH_THREAD_IN_PROC(p, td) {
818 thread_lock(td);
819 td->td_flags |= TDF_INMEM;
820 td->td_flags &= ~TDF_SWAPINREQ;
821 TD_CLR_SWAPPED(td);
822 if (TD_CAN_RUN(td))
823 if (setrunnable(td)) {
824 #ifdef INVARIANTS
825 /*
826 * XXX: We just cleared TDI_SWAPPED
827 * above and set TDF_INMEM, so this
828 * should never happen.
829 */
830 panic("not waking up swapper");
831 #endif
832 }
833 thread_unlock(td);
834 }
835 p->p_flag &= ~(P_SWAPPINGIN | P_SWAPPINGOUT);
836 p->p_flag |= P_INMEM;
837 }
838
839 static int
840 swapout(struct proc *p)
841 {
842 struct thread *td;
843
844 PROC_LOCK_ASSERT(p, MA_OWNED);
845
846 /*
847 * The states of this process and its threads may have changed
848 * by now. Assuming that there is only one pageout daemon thread,
849 * this process should still be in memory.
850 */
851 KASSERT((p->p_flag & (P_INMEM | P_SWAPPINGOUT | P_SWAPPINGIN)) ==
852 P_INMEM, ("swapout: lost a swapout race?"));
853
854 /*
855 * Remember the resident count.
856 */
857 p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace);
858
859 /*
860 * Check and mark all threads before we proceed.
861 */
862 p->p_flag &= ~P_INMEM;
863 p->p_flag |= P_SWAPPINGOUT;
864 FOREACH_THREAD_IN_PROC(p, td) {
865 thread_lock(td);
866 if (!thread_safetoswapout(td)) {
867 thread_unlock(td);
868 swapclear(p);
869 return (EBUSY);
870 }
871 td->td_flags &= ~TDF_INMEM;
872 TD_SET_SWAPPED(td);
873 thread_unlock(td);
874 }
875 td = FIRST_THREAD_IN_PROC(p);
876 ++td->td_ru.ru_nswap;
877 PROC_UNLOCK(p);
878
879 /*
880 * This list is stable because all threads are now prevented from
881 * running. The list is only modified in the context of a running
882 * thread in this process.
883 */
884 FOREACH_THREAD_IN_PROC(p, td)
885 vm_thread_swapout(td);
886
887 PROC_LOCK(p);
888 p->p_flag &= ~P_SWAPPINGOUT;
889 p->p_swtick = ticks;
890 return (0);
891 }
Cache object: 2c52c8f2b1b62c12c1362d455f294e05
|