2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from: @(#)vm_glue.c 8.6 (Berkeley) 1/5/94
35 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36 * All rights reserved.
38 * Permission to use, copy, modify and distribute this software and
39 * its documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
46 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie the
56 * rights to redistribute these changes.
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
63 #include "opt_kstack_pages.h"
64 #include "opt_kstack_max_pages.h"
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/limits.h>
70 #include <sys/mutex.h>
72 #include <sys/resourcevar.h>
74 #include <sys/vmmeter.h>
76 #include <sys/sysctl.h>
78 #include <sys/kernel.h>
80 #include <sys/unistd.h>
83 #include <vm/vm_param.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_pageout.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_extern.h>
91 #include <vm/vm_pager.h>
92 #include <vm/swap_pager.h>
97 * System initialization
99 * Note: proc0 from proc.h
101 static void vm_init_limits(void *);
102 SYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0)
105 * THIS MUST BE THE LAST INITIALIZATION ITEM!!!
107 * Note: run scheduling should be divorced from the vm system.
109 static void scheduler(void *);
110 SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_ANY, scheduler, NULL)
113 static void swapout(struct proc *);
117 static volatile int proc0_rescan;
123 * WARNING! This code calls vm_map_check_protection() which only checks
124 * the associated vm_map_entry range. It does not determine whether the
125 * contents of the memory is actually readable or writable. In most cases
126 * just checking the vm_map_entry is sufficient within the kernel's address
130 kernacc(addr, len, rw)
135 vm_offset_t saddr, eaddr;
138 KASSERT((rw & ~VM_PROT_ALL) == 0,
139 ("illegal ``rw'' argument to kernacc (%x)\n", rw));
141 if ((vm_offset_t)addr + len > kernel_map->max_offset ||
142 (vm_offset_t)addr + len < (vm_offset_t)addr)
146 saddr = trunc_page((vm_offset_t)addr);
147 eaddr = round_page((vm_offset_t)addr + len);
148 vm_map_lock_read(kernel_map);
149 rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot);
150 vm_map_unlock_read(kernel_map);
157 * WARNING! This code calls vm_map_check_protection() which only checks
158 * the associated vm_map_entry range. It does not determine whether the
159 * contents of the memory is actually readable or writable. vmapbuf(),
160 * vm_fault_quick(), or copyin()/copout()/su*()/fu*() functions should be
161 * used in conjuction with this call.
164 useracc(addr, len, rw)
172 KASSERT((rw & ~VM_PROT_ALL) == 0,
173 ("illegal ``rw'' argument to useracc (%x)\n", rw));
175 map = &curproc->p_vmspace->vm_map;
176 if ((vm_offset_t)addr + len > vm_map_max(map) ||
177 (vm_offset_t)addr + len < (vm_offset_t)addr) {
180 vm_map_lock_read(map);
181 rv = vm_map_check_protection(map, trunc_page((vm_offset_t)addr),
182 round_page((vm_offset_t)addr + len), prot);
183 vm_map_unlock_read(map);
188 vslock(void *addr, size_t len)
190 vm_offset_t end, last, start;
194 last = (vm_offset_t)addr + len;
195 start = trunc_page((vm_offset_t)addr);
196 end = round_page(last);
197 if (last < (vm_offset_t)addr || end < (vm_offset_t)addr)
199 npages = atop(end - start);
200 if (npages > vm_page_max_wired)
204 pmap_wired_count(vm_map_pmap(&curproc->p_vmspace->vm_map))) >
205 lim_cur(curproc, RLIMIT_MEMLOCK)) {
206 PROC_UNLOCK(curproc);
209 PROC_UNLOCK(curproc);
214 * The limit for transient usage of wired pages should be
215 * larger than for "permanent" wired pages (mlock()).
217 * Also, the sysctl code, which is the only present user
218 * of vslock(), does a hard loop on EAGAIN.
220 if (npages + cnt.v_wire_count > vm_page_max_wired)
223 error = vm_map_wire(&curproc->p_vmspace->vm_map, start, end,
224 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
226 * Return EFAULT on error to match copy{in,out}() behaviour
227 * rather than returning ENOMEM like mlock() would.
229 return (error == KERN_SUCCESS ? 0 : EFAULT);
233 vsunlock(void *addr, size_t len)
236 /* Rely on the parameter sanity checks performed by vslock(). */
237 (void)vm_map_unwire(&curproc->p_vmspace->vm_map,
238 trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len),
239 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
242 #ifndef KSTACK_MAX_PAGES
243 #define KSTACK_MAX_PAGES 32
247 * Create the kernel stack (including pcb for i386) for a new thread.
248 * This routine directly affects the fork perf for a process and
249 * create performance for a thread.
252 vm_thread_new(struct thread *td, int pages)
256 vm_page_t m, ma[KSTACK_MAX_PAGES];
261 pages = KSTACK_PAGES;
262 else if (pages > KSTACK_MAX_PAGES)
263 pages = KSTACK_MAX_PAGES;
265 * Allocate an object for the kstack.
267 ksobj = vm_object_allocate(OBJT_DEFAULT, pages);
268 td->td_kstack_obj = ksobj;
270 * Get a kernel virtual address for this thread's kstack.
272 ks = kmem_alloc_nofault(kernel_map,
273 (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE);
275 panic("vm_thread_new: kstack allocation failed");
276 if (KSTACK_GUARD_PAGES != 0) {
277 pmap_qremove(ks, KSTACK_GUARD_PAGES);
278 ks += KSTACK_GUARD_PAGES * PAGE_SIZE;
282 * Knowing the number of pages allocated is useful when you
283 * want to deallocate them.
285 td->td_kstack_pages = pages;
287 * For the length of the stack, link in a real page of ram for each
290 VM_OBJECT_LOCK(ksobj);
291 for (i = 0; i < pages; i++) {
293 * Get a kernel stack page.
295 m = vm_page_grab(ksobj, i, VM_ALLOC_NOBUSY |
296 VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED);
298 m->valid = VM_PAGE_BITS_ALL;
300 VM_OBJECT_UNLOCK(ksobj);
301 pmap_qenter(ks, ma, pages);
305 * Dispose of a thread's kernel stack.
308 vm_thread_dispose(struct thread *td)
315 pages = td->td_kstack_pages;
316 ksobj = td->td_kstack_obj;
318 pmap_qremove(ks, pages);
319 VM_OBJECT_LOCK(ksobj);
320 for (i = 0; i < pages; i++) {
321 m = vm_page_lookup(ksobj, i);
323 panic("vm_thread_dispose: kstack already missing?");
324 vm_page_lock_queues();
325 vm_page_unwire(m, 0);
327 vm_page_unlock_queues();
329 VM_OBJECT_UNLOCK(ksobj);
330 vm_object_deallocate(ksobj);
331 kmem_free(kernel_map, ks - (KSTACK_GUARD_PAGES * PAGE_SIZE),
332 (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE);
336 * Allow a thread's kernel stack to be paged out.
339 vm_thread_swapout(struct thread *td)
345 cpu_thread_swapout(td);
346 pages = td->td_kstack_pages;
347 ksobj = td->td_kstack_obj;
348 pmap_qremove(td->td_kstack, pages);
349 VM_OBJECT_LOCK(ksobj);
350 for (i = 0; i < pages; i++) {
351 m = vm_page_lookup(ksobj, i);
353 panic("vm_thread_swapout: kstack already missing?");
354 vm_page_lock_queues();
356 vm_page_unwire(m, 0);
357 vm_page_unlock_queues();
359 VM_OBJECT_UNLOCK(ksobj);
363 * Bring the kernel stack for a specified thread back in.
366 vm_thread_swapin(struct thread *td)
369 vm_page_t m, ma[KSTACK_MAX_PAGES];
372 pages = td->td_kstack_pages;
373 ksobj = td->td_kstack_obj;
374 VM_OBJECT_LOCK(ksobj);
375 for (i = 0; i < pages; i++) {
376 m = vm_page_grab(ksobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
377 if (m->valid != VM_PAGE_BITS_ALL) {
378 rv = vm_pager_get_pages(ksobj, &m, 1, 0);
379 if (rv != VM_PAGER_OK)
380 panic("vm_thread_swapin: cannot get kstack for proc: %d", td->td_proc->p_pid);
381 m = vm_page_lookup(ksobj, i);
382 m->valid = VM_PAGE_BITS_ALL;
385 vm_page_lock_queues();
388 vm_page_unlock_queues();
390 VM_OBJECT_UNLOCK(ksobj);
391 pmap_qenter(td->td_kstack, ma, pages);
392 cpu_thread_swapin(td);
396 * Set up a variable-sized alternate kstack.
399 vm_thread_new_altkstack(struct thread *td, int pages)
402 td->td_altkstack = td->td_kstack;
403 td->td_altkstack_obj = td->td_kstack_obj;
404 td->td_altkstack_pages = td->td_kstack_pages;
406 vm_thread_new(td, pages);
410 * Restore the original kstack.
413 vm_thread_dispose_altkstack(struct thread *td)
416 vm_thread_dispose(td);
418 td->td_kstack = td->td_altkstack;
419 td->td_kstack_obj = td->td_altkstack_obj;
420 td->td_kstack_pages = td->td_altkstack_pages;
421 td->td_altkstack = 0;
422 td->td_altkstack_obj = NULL;
423 td->td_altkstack_pages = 0;
427 * Implement fork's actions on an address space.
428 * Here we arrange for the address space to be copied or referenced,
429 * allocate a user struct (pcb and kernel stack), then call the
430 * machine-dependent layer to fill those in and make the new process
431 * ready to run. The new process is set up so that it returns directly
432 * to user mode to avoid stack copying and relocation problems.
435 vm_forkproc(td, p2, td2, flags)
441 struct proc *p1 = td->td_proc;
443 if ((flags & RFPROC) == 0) {
445 * Divorce the memory, if it is shared, essentially
446 * this changes shared memory amongst threads, into
449 if ((flags & RFMEM) == 0) {
450 if (p1->p_vmspace->vm_refcnt > 1) {
454 cpu_fork(td, p2, td2, flags);
459 p2->p_vmspace = p1->p_vmspace;
460 atomic_add_int(&p1->p_vmspace->vm_refcnt, 1);
463 while (vm_page_count_severe()) {
467 if ((flags & RFMEM) == 0) {
468 p2->p_vmspace = vmspace_fork(p1->p_vmspace);
469 if (p1->p_vmspace->vm_shm)
474 * cpu_fork will copy and update the pcb, set up the kernel stack,
475 * and make the child ready to run.
477 cpu_fork(td, p2, td2, flags);
481 * Called after process has been wait(2)'ed apon and is being reaped.
482 * The idea is to reclaim resources that we could not reclaim while
483 * the process was still executing.
490 vmspace_exitfree(p); /* and clean-out the vmspace */
494 * Set default limits for VM system.
495 * Called for proc 0, and then inherited by all others.
497 * XXX should probably act directly on proc0.
500 vm_init_limits(udata)
503 struct proc *p = udata;
508 * Set up the initial limits on process VM. Set the maximum resident
509 * set size to be half of (reasonably) available memory. Since this
510 * is a soft limit, it comes into effect only when the system is out
511 * of memory - half of main memory helps to favor smaller processes,
512 * and reduces thrashing of the object cache.
515 limp->pl_rlimit[RLIMIT_STACK].rlim_cur = dflssiz;
516 limp->pl_rlimit[RLIMIT_STACK].rlim_max = maxssiz;
517 limp->pl_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz;
518 limp->pl_rlimit[RLIMIT_DATA].rlim_max = maxdsiz;
519 /* limit the limit to no less than 2MB */
520 rss_limit = max(cnt.v_free_count, 512);
521 limp->pl_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit);
522 limp->pl_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY;
531 PROC_LOCK_ASSERT(p, MA_OWNED);
532 if ((p->p_sflag & PS_INMEM) == 0)
533 panic("faultin: proc swapped out with NO_SWAPPING!");
534 #else /* !NO_SWAPPING */
537 PROC_LOCK_ASSERT(p, MA_OWNED);
539 * If another process is swapping in this process,
540 * just wait until it finishes.
542 if (p->p_sflag & PS_SWAPPINGIN)
543 msleep(&p->p_sflag, &p->p_mtx, PVM, "faultin", 0);
544 else if ((p->p_sflag & PS_INMEM) == 0) {
546 * Don't let another thread swap process p out while we are
547 * busy swapping it in.
550 mtx_lock_spin(&sched_lock);
551 p->p_sflag |= PS_SWAPPINGIN;
552 mtx_unlock_spin(&sched_lock);
555 FOREACH_THREAD_IN_PROC(p, td)
556 vm_thread_swapin(td);
559 mtx_lock_spin(&sched_lock);
560 p->p_sflag &= ~PS_SWAPPINGIN;
561 p->p_sflag |= PS_INMEM;
562 FOREACH_THREAD_IN_PROC(p, td) {
567 mtx_unlock_spin(&sched_lock);
571 /* Allow other threads to swap p out now. */
574 #endif /* NO_SWAPPING */
578 * This swapin algorithm attempts to swap-in processes only if there
579 * is enough space for them. Of course, if a process waits for a long
580 * time, it will be swapped in anyway.
582 * XXXKSE - process with the thread with highest priority counts..
584 * Giant is held on entry.
597 mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED);
601 if (vm_page_count_min()) {
603 mtx_lock_spin(&sched_lock);
605 mtx_unlock_spin(&sched_lock);
611 sx_slock(&allproc_lock);
612 FOREACH_PROC_IN_SYSTEM(p) {
614 if (p->p_sflag & (PS_INMEM | PS_SWAPPINGOUT | PS_SWAPPINGIN)) {
617 mtx_lock_spin(&sched_lock);
618 FOREACH_THREAD_IN_PROC(p, td) {
620 * An otherwise runnable thread of a process
621 * swapped out has only the TDI_SWAPPED bit set.
624 if (td->td_inhibitors == TDI_SWAPPED) {
626 pri = p->p_swtime + kg->kg_slptime;
627 if ((p->p_sflag & PS_SWAPINREQ) == 0) {
628 pri -= p->p_nice * 8;
632 * if this ksegrp is higher priority
633 * and there is enough space, then select
634 * this process instead of the previous
643 mtx_unlock_spin(&sched_lock);
645 sx_sunlock(&allproc_lock);
648 * Nothing to do, back to sleep.
650 if ((p = pp) == NULL) {
651 mtx_lock_spin(&sched_lock);
653 TD_SET_IWAIT(&thread0);
654 mi_switch(SW_VOL, NULL);
657 mtx_unlock_spin(&sched_lock);
663 * Another process may be bringing or may have already
664 * brought this process in while we traverse all threads.
665 * Or, this process may even be being swapped out again.
667 if (p->p_sflag & (PS_INMEM | PS_SWAPPINGOUT | PS_SWAPPINGIN)) {
669 mtx_lock_spin(&sched_lock);
671 mtx_unlock_spin(&sched_lock);
675 mtx_lock_spin(&sched_lock);
676 p->p_sflag &= ~PS_SWAPINREQ;
677 mtx_unlock_spin(&sched_lock);
680 * We would like to bring someone in. (only if there is space).
681 * [What checks the space? ]
685 mtx_lock_spin(&sched_lock);
688 mtx_unlock_spin(&sched_lock);
692 void kick_proc0(void)
694 struct thread *td = &thread0;
697 if (TD_AWAITING_INTR(td)) {
698 CTR2(KTR_INTR, "%s: setrunqueue %d", __func__, 0);
700 setrunqueue(td, SRQ_INTR);
703 CTR2(KTR_INTR, "%s: state %d",
704 __func__, td->td_state);
713 * Swap_idle_threshold1 is the guaranteed swapped in time for a process
715 static int swap_idle_threshold1 = 2;
716 SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, CTLFLAG_RW,
717 &swap_idle_threshold1, 0, "Guaranteed swapped in time for a process");
720 * Swap_idle_threshold2 is the time that a process can be idle before
721 * it will be swapped out, if idle swapping is enabled.
723 static int swap_idle_threshold2 = 10;
724 SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, CTLFLAG_RW,
725 &swap_idle_threshold2, 0, "Time before a process will be swapped out");
728 * Swapout is driven by the pageout daemon. Very simple, we find eligible
729 * procs and unwire their u-areas. We try to always "swap" at least one
730 * process in case we need the room for a swapin.
731 * If any procs have been sleeping/stopped for at least maxslp seconds,
732 * they are swapped. Else, we swap the longest-sleeping or stopped process,
733 * if any, otherwise the longest-resident process.
736 swapout_procs(action)
745 sx_slock(&allproc_lock);
746 FOREACH_PROC_IN_SYSTEM(p) {
748 int minslptime = 100000;
751 * Watch out for a process in
752 * creation. It may have no
753 * address space or lock yet.
755 mtx_lock_spin(&sched_lock);
756 if (p->p_state == PRS_NEW) {
757 mtx_unlock_spin(&sched_lock);
760 mtx_unlock_spin(&sched_lock);
763 * An aio daemon switches its
764 * address space while running.
765 * Perform a quick check whether
766 * a process has P_SYSTEM.
768 if ((p->p_flag & P_SYSTEM) != 0)
772 * Do not swapout a process that
773 * is waiting for VM data
774 * structures as there is a possible
775 * deadlock. Test this first as
778 * Lock the map until swapout
779 * finishes, or a thread of this
780 * process may attempt to alter
786 ("swapout_procs: a process has no address space"));
787 atomic_add_int(&vm->vm_refcnt, 1);
789 if (!vm_map_trylock(&vm->vm_map))
793 if (p->p_lock != 0 ||
794 (p->p_flag & (P_STOPPED_SINGLE|P_TRACED|P_SYSTEM|P_WEXIT)
799 * only aiod changes vmspace, however it will be
800 * skipped because of the if statement above checking
803 if ((p->p_sflag & (PS_INMEM|PS_SWAPPINGOUT|PS_SWAPPINGIN)) != PS_INMEM)
806 switch (p->p_state) {
808 /* Don't swap out processes in any sort
809 * of 'special' state. */
813 mtx_lock_spin(&sched_lock);
815 * do not swapout a realtime process
816 * Check all the thread groups..
818 FOREACH_KSEGRP_IN_PROC(p, kg) {
819 if (PRI_IS_REALTIME(kg->kg_pri_class))
823 * Guarantee swap_idle_threshold1
826 if (kg->kg_slptime < swap_idle_threshold1)
830 * Do not swapout a process if it is
831 * waiting on a critical event of some
832 * kind or there is a thread whose
833 * pageable memory may be accessed.
835 * This could be refined to support
836 * swapping out a thread.
838 FOREACH_THREAD_IN_GROUP(kg, td) {
839 if ((td->td_priority) < PSOCK ||
840 !thread_safetoswapout(td))
844 * If the system is under memory stress,
845 * or if we are swapping
846 * idle processes >= swap_idle_threshold2,
847 * then swap the process out.
849 if (((action & VM_SWAP_NORMAL) == 0) &&
850 (((action & VM_SWAP_IDLE) == 0) ||
851 (kg->kg_slptime < swap_idle_threshold2)))
854 if (minslptime > kg->kg_slptime)
855 minslptime = kg->kg_slptime;
859 * If the pageout daemon didn't free enough pages,
860 * or if this process is idle and the system is
861 * configured to swap proactively, swap it out.
863 if ((action & VM_SWAP_NORMAL) ||
864 ((action & VM_SWAP_IDLE) &&
865 (minslptime > swap_idle_threshold2))) {
868 mtx_unlock_spin(&sched_lock);
870 vm_map_unlock(&vm->vm_map);
872 sx_sunlock(&allproc_lock);
876 mtx_unlock_spin(&sched_lock);
880 vm_map_unlock(&vm->vm_map);
885 sx_sunlock(&allproc_lock);
887 * If we swapped something out, and another process needed memory,
888 * then wakeup the sched process.
900 PROC_LOCK_ASSERT(p, MA_OWNED);
901 mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
902 #if defined(SWAP_DEBUG)
903 printf("swapping out %d\n", p->p_pid);
907 * The states of this process and its threads may have changed
908 * by now. Assuming that there is only one pageout daemon thread,
909 * this process should still be in memory.
911 KASSERT((p->p_sflag & (PS_INMEM|PS_SWAPPINGOUT|PS_SWAPPINGIN)) == PS_INMEM,
912 ("swapout: lost a swapout race?"));
914 #if defined(INVARIANTS)
916 * Make sure that all threads are safe to be swapped out.
918 * Alternatively, we could swap out only safe threads.
920 FOREACH_THREAD_IN_PROC(p, td) {
921 KASSERT(thread_safetoswapout(td),
922 ("swapout: there is a thread not safe for swapout"));
924 #endif /* INVARIANTS */
926 ++p->p_stats->p_ru.ru_nswap;
928 * remember the process resident count
930 p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace);
932 p->p_sflag &= ~PS_INMEM;
933 p->p_sflag |= PS_SWAPPINGOUT;
935 FOREACH_THREAD_IN_PROC(p, td)
937 mtx_unlock_spin(&sched_lock);
939 FOREACH_THREAD_IN_PROC(p, td)
940 vm_thread_swapout(td);
943 mtx_lock_spin(&sched_lock);
944 p->p_sflag &= ~PS_SWAPPINGOUT;
947 #endif /* !NO_SWAPPING */