xref: /onnv-gate/usr/src/uts/common/os/sched.c (revision 1483:425d0bc03c15)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1483Svb70745  * Common Development and Distribution License (the "License").
6*1483Svb70745  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21390Sraf 
220Sstevel@tonic-gate /*
23*1483Svb70745  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/proc.h>
380Sstevel@tonic-gate #include <sys/cpuvar.h>
390Sstevel@tonic-gate #include <sys/var.h>
400Sstevel@tonic-gate #include <sys/tuneable.h>
410Sstevel@tonic-gate #include <sys/cmn_err.h>
420Sstevel@tonic-gate #include <sys/buf.h>
430Sstevel@tonic-gate #include <sys/disp.h>
440Sstevel@tonic-gate #include <sys/vmsystm.h>
450Sstevel@tonic-gate #include <sys/vmparam.h>
460Sstevel@tonic-gate #include <sys/class.h>
470Sstevel@tonic-gate #include <sys/vtrace.h>
480Sstevel@tonic-gate #include <sys/modctl.h>
490Sstevel@tonic-gate #include <sys/debug.h>
500Sstevel@tonic-gate #include <sys/tnf_probe.h>
510Sstevel@tonic-gate #include <sys/procfs.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include <vm/seg.h>
540Sstevel@tonic-gate #include <vm/seg_kp.h>
550Sstevel@tonic-gate #include <vm/as.h>
560Sstevel@tonic-gate #include <vm/rm.h>
570Sstevel@tonic-gate #include <vm/seg_kmem.h>
580Sstevel@tonic-gate #include <sys/callb.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * The swapper sleeps on runout when there is no one to swap in.
620Sstevel@tonic-gate  * It sleeps on runin when it could not find space to swap someone
630Sstevel@tonic-gate  * in or after swapping someone in.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate char	runout;
660Sstevel@tonic-gate char	runin;
670Sstevel@tonic-gate char	wake_sched;	/* flag tells clock to wake swapper on next tick */
680Sstevel@tonic-gate char	wake_sched_sec;	/* flag tells clock to wake swapper after a second */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * The swapper swaps processes to reduce memory demand and runs
720Sstevel@tonic-gate  * when avefree < desfree.  The swapper resorts to SOFTSWAP when
730Sstevel@tonic-gate  * avefree < desfree which results in swapping out all processes
740Sstevel@tonic-gate  * sleeping for more than maxslp seconds.  HARDSWAP occurs when the
750Sstevel@tonic-gate  * system is on the verge of thrashing and this results in swapping
760Sstevel@tonic-gate  * out runnable threads or threads sleeping for less than maxslp secs.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * The swapper runs through all the active processes in the system
790Sstevel@tonic-gate  * and invokes the scheduling class specific swapin/swapout routine
800Sstevel@tonic-gate  * for every thread in the process to obtain an effective priority
810Sstevel@tonic-gate  * for the process.  A priority of -1 implies that the thread isn't
820Sstevel@tonic-gate  * swappable.  This effective priority is used to find the most
830Sstevel@tonic-gate  * eligible process to swapout or swapin.
840Sstevel@tonic-gate  *
850Sstevel@tonic-gate  * NOTE:  Threads which have been swapped are not linked on any
860Sstevel@tonic-gate  *	  queue and their dispatcher lock points at the "swapped_lock".
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  * Processes containing threads with the TS_DONT_SWAP flag set cannot be
890Sstevel@tonic-gate  * swapped out immediately by the swapper.  This is due to the fact that
900Sstevel@tonic-gate  * such threads may be holding locks which may be needed by the swapper
910Sstevel@tonic-gate  * to push its pages out.  The TS_SWAPENQ flag is set on such threads
920Sstevel@tonic-gate  * to prevent them running in user mode.  When such threads reach a
930Sstevel@tonic-gate  * safe point (i.e., are not holding any locks - CL_TRAPRET), they
940Sstevel@tonic-gate  * queue themseleves onto the swap queue which is processed by the
950Sstevel@tonic-gate  * swapper.  This results in reducing memory demand when the system
960Sstevel@tonic-gate  * is desparate for memory as the thread can't run in user mode.
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  * The swap queue consists of threads, linked via t_link, which are
990Sstevel@tonic-gate  * haven't been swapped, are runnable but not on the run queue.  The
1000Sstevel@tonic-gate  * swap queue is protected by the "swapped_lock".  The dispatcher
1010Sstevel@tonic-gate  * lock (t_lockp) of all threads on the swap queue points at the
1020Sstevel@tonic-gate  * "swapped_lock".  Thus, the entire queue and/or threads on the
1030Sstevel@tonic-gate  * queue can be locked by acquiring "swapped_lock".
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate static kthread_t *tswap_queue;
1060Sstevel@tonic-gate extern disp_lock_t swapped_lock; /* protects swap queue and threads on it */
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate int	maxslp = 0;
1090Sstevel@tonic-gate pgcnt_t	avefree;	/* 5 sec moving average of free memory */
1100Sstevel@tonic-gate pgcnt_t	avefree30;	/* 30 sec moving average of free memory */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * Minimum size used to decide if sufficient memory is available
1140Sstevel@tonic-gate  * before a process is swapped in.  This is necessary since in most
1150Sstevel@tonic-gate  * cases the actual size of a process (p_swrss) being swapped in
1160Sstevel@tonic-gate  * is usually 2 pages (kernel stack pages).  This is due to the fact
1170Sstevel@tonic-gate  * almost all user pages of a process are stolen by pageout before
1180Sstevel@tonic-gate  * the swapper decides to swapout it out.
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate int	min_procsize = 12;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate static int	swapin(proc_t *);
1230Sstevel@tonic-gate static int	swapout(proc_t *, uint_t *, int);
1240Sstevel@tonic-gate static void	process_swap_queue();
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate #ifdef __sparc
1270Sstevel@tonic-gate extern void lwp_swapin(kthread_t *);
1280Sstevel@tonic-gate #endif /* __sparc */
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate  * Counters to keep track of the number of swapins or swapouts.
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate uint_t tot_swapped_in, tot_swapped_out;
1340Sstevel@tonic-gate uint_t softswap, hardswap, swapqswap;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * Macro to determine if a process is eligble to be swapped.
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate #define	not_swappable(p)					\
1400Sstevel@tonic-gate 	(((p)->p_flag & SSYS) || (p)->p_stat == SIDL ||		\
1410Sstevel@tonic-gate 	    (p)->p_stat == SZOMB || (p)->p_as == NULL ||	\
1420Sstevel@tonic-gate 	    (p)->p_as == &kas)
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * Memory scheduler.
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate void
1480Sstevel@tonic-gate sched()
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	kthread_id_t	t;
1510Sstevel@tonic-gate 	pri_t		proc_pri;
1520Sstevel@tonic-gate 	pri_t		thread_pri;
1530Sstevel@tonic-gate 	pri_t		swapin_pri;
1540Sstevel@tonic-gate 	int		desperate;
1550Sstevel@tonic-gate 	pgcnt_t		needs;
1560Sstevel@tonic-gate 	int		divisor;
1570Sstevel@tonic-gate 	proc_t		*prp;
1580Sstevel@tonic-gate 	proc_t		*swapout_prp;
1590Sstevel@tonic-gate 	proc_t		*swapin_prp;
1600Sstevel@tonic-gate 	spgcnt_t	avail;
1610Sstevel@tonic-gate 	int		chosen_pri;
1620Sstevel@tonic-gate 	time_t		swapout_time;
1630Sstevel@tonic-gate 	time_t		swapin_proc_time;
1640Sstevel@tonic-gate 	callb_cpr_t	cprinfo;
1650Sstevel@tonic-gate 	kmutex_t	swap_cpr_lock;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	mutex_init(&swap_cpr_lock, NULL, MUTEX_DEFAULT, NULL);
1680Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &swap_cpr_lock, callb_generic_cpr, "sched");
1690Sstevel@tonic-gate 	if (maxslp == 0)
1700Sstevel@tonic-gate 		maxslp = MAXSLP;
1710Sstevel@tonic-gate loop:
1720Sstevel@tonic-gate 	needs = 0;
1730Sstevel@tonic-gate 	desperate = 0;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	swapin_pri = v.v_nglobpris;
1760Sstevel@tonic-gate 	swapin_prp = NULL;
1770Sstevel@tonic-gate 	chosen_pri = -1;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	process_swap_queue();
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * Set desperate if
1830Sstevel@tonic-gate 	 * 	1.  At least 2 runnable processes (on average).
1840Sstevel@tonic-gate 	 *	2.  Short (5 sec) and longer (30 sec) average is less
1850Sstevel@tonic-gate 	 *	    than minfree and desfree respectively.
1860Sstevel@tonic-gate 	 *	3.  Pagein + pageout rate is excessive.
1870Sstevel@tonic-gate 	 */
1880Sstevel@tonic-gate 	if (avenrun[0] >= 2 * FSCALE &&
1890Sstevel@tonic-gate 	    (MAX(avefree, avefree30) < desfree) &&
1900Sstevel@tonic-gate 	    (pginrate + pgoutrate > maxpgio || avefree < minfree)) {
1910Sstevel@tonic-gate 		TRACE_4(TR_FAC_SCHED, TR_DESPERATE,
1920Sstevel@tonic-gate 		    "desp:avefree: %d, avefree30: %d, freemem: %d"
1930Sstevel@tonic-gate 		    " pginrate: %d\n", avefree, avefree30, freemem, pginrate);
1940Sstevel@tonic-gate 		desperate = 1;
1950Sstevel@tonic-gate 		goto unload;
1960Sstevel@tonic-gate 	}
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	/*
1990Sstevel@tonic-gate 	 * Search list of processes to swapin and swapout deadwood.
2000Sstevel@tonic-gate 	 */
2010Sstevel@tonic-gate 	swapin_proc_time = 0;
2020Sstevel@tonic-gate top:
2030Sstevel@tonic-gate 	mutex_enter(&pidlock);
2040Sstevel@tonic-gate 	for (prp = practive; prp != NULL; prp = prp->p_next) {
2050Sstevel@tonic-gate 		if (not_swappable(prp))
2060Sstevel@tonic-gate 			continue;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 		/*
2090Sstevel@tonic-gate 		 * Look at processes with at least one swapped lwp.
2100Sstevel@tonic-gate 		 */
2110Sstevel@tonic-gate 		if (prp->p_swapcnt) {
2120Sstevel@tonic-gate 			time_t proc_time;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 			/*
2150Sstevel@tonic-gate 			 * Higher priority processes are good candidates
2160Sstevel@tonic-gate 			 * to swapin.
2170Sstevel@tonic-gate 			 */
2180Sstevel@tonic-gate 			mutex_enter(&prp->p_lock);
2190Sstevel@tonic-gate 			proc_pri = -1;
2200Sstevel@tonic-gate 			t = prp->p_tlist;
2210Sstevel@tonic-gate 			proc_time = 0;
2220Sstevel@tonic-gate 			do {
2230Sstevel@tonic-gate 				if (t->t_schedflag & TS_LOAD)
2240Sstevel@tonic-gate 					continue;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 				thread_lock(t);
2270Sstevel@tonic-gate 				thread_pri = CL_SWAPIN(t, 0);
2280Sstevel@tonic-gate 				thread_unlock(t);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 				if (t->t_stime - proc_time > 0)
2310Sstevel@tonic-gate 					proc_time = t->t_stime;
2320Sstevel@tonic-gate 				if (thread_pri > proc_pri)
2330Sstevel@tonic-gate 					proc_pri = thread_pri;
2340Sstevel@tonic-gate 			} while ((t = t->t_forw) != prp->p_tlist);
2350Sstevel@tonic-gate 			mutex_exit(&prp->p_lock);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 			if (proc_pri == -1)
2380Sstevel@tonic-gate 				continue;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 			TRACE_3(TR_FAC_SCHED, TR_CHOOSE_SWAPIN,
2410Sstevel@tonic-gate 			    "prp %p epri %d proc_time %d",
2420Sstevel@tonic-gate 			    prp, proc_pri, proc_time);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 			/*
2450Sstevel@tonic-gate 			 * Swapin processes with a high effective priority.
2460Sstevel@tonic-gate 			 */
2470Sstevel@tonic-gate 			if (swapin_prp == NULL || proc_pri > chosen_pri) {
2480Sstevel@tonic-gate 				swapin_prp = prp;
2490Sstevel@tonic-gate 				chosen_pri = proc_pri;
2500Sstevel@tonic-gate 				swapin_pri = proc_pri;
2510Sstevel@tonic-gate 				swapin_proc_time = proc_time;
2520Sstevel@tonic-gate 			}
2530Sstevel@tonic-gate 		} else {
2540Sstevel@tonic-gate 			/*
2550Sstevel@tonic-gate 			 * No need to soft swap if we have sufficient
2560Sstevel@tonic-gate 			 * memory.
2570Sstevel@tonic-gate 			 */
2580Sstevel@tonic-gate 			if (avefree > desfree ||
2590Sstevel@tonic-gate 			    avefree < desfree && freemem > desfree)
2600Sstevel@tonic-gate 				continue;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 			/*
263390Sraf 			 * Skip processes that are exiting
264390Sraf 			 * or whose address spaces are locked.
2650Sstevel@tonic-gate 			 */
2660Sstevel@tonic-gate 			mutex_enter(&prp->p_lock);
267390Sraf 			if ((prp->p_flag & SEXITING) ||
2680Sstevel@tonic-gate 			    (prp->p_as != NULL && AS_ISPGLCK(prp->p_as))) {
2690Sstevel@tonic-gate 				mutex_exit(&prp->p_lock);
2700Sstevel@tonic-gate 				continue;
2710Sstevel@tonic-gate 			}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 			/*
2740Sstevel@tonic-gate 			 * Softswapping to kick out deadwood.
2750Sstevel@tonic-gate 			 */
2760Sstevel@tonic-gate 			proc_pri = -1;
2770Sstevel@tonic-gate 			t = prp->p_tlist;
2780Sstevel@tonic-gate 			do {
2790Sstevel@tonic-gate 				if ((t->t_schedflag & (TS_SWAPENQ |
2800Sstevel@tonic-gate 				    TS_ON_SWAPQ | TS_LOAD)) != TS_LOAD)
2810Sstevel@tonic-gate 					continue;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 				thread_lock(t);
2840Sstevel@tonic-gate 				thread_pri = CL_SWAPOUT(t, SOFTSWAP);
2850Sstevel@tonic-gate 				thread_unlock(t);
2860Sstevel@tonic-gate 				if (thread_pri > proc_pri)
2870Sstevel@tonic-gate 					proc_pri = thread_pri;
2880Sstevel@tonic-gate 			} while ((t = t->t_forw) != prp->p_tlist);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 			if (proc_pri != -1) {
2910Sstevel@tonic-gate 				uint_t swrss;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 				mutex_exit(&pidlock);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 				TRACE_1(TR_FAC_SCHED, TR_SOFTSWAP,
2960Sstevel@tonic-gate 				    "softswap:prp %p", prp);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 				(void) swapout(prp, &swrss, SOFTSWAP);
2990Sstevel@tonic-gate 				softswap++;
3000Sstevel@tonic-gate 				prp->p_swrss += swrss;
3010Sstevel@tonic-gate 				mutex_exit(&prp->p_lock);
3020Sstevel@tonic-gate 				goto top;
3030Sstevel@tonic-gate 			}
3040Sstevel@tonic-gate 			mutex_exit(&prp->p_lock);
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 	if (swapin_prp != NULL)
3080Sstevel@tonic-gate 		mutex_enter(&swapin_prp->p_lock);
3090Sstevel@tonic-gate 	mutex_exit(&pidlock);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	if (swapin_prp == NULL) {
3120Sstevel@tonic-gate 		TRACE_3(TR_FAC_SCHED, TR_RUNOUT,
3130Sstevel@tonic-gate 		"schedrunout:runout nswapped: %d, avefree: %ld freemem: %ld",
3140Sstevel@tonic-gate 		    nswapped, avefree, freemem);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 		t = curthread;
3170Sstevel@tonic-gate 		thread_lock(t);
3180Sstevel@tonic-gate 		runout++;
3190Sstevel@tonic-gate 		t->t_schedflag |= (TS_ALLSTART & ~TS_CSTART);
3200Sstevel@tonic-gate 		t->t_whystop = PR_SUSPENDED;
3210Sstevel@tonic-gate 		t->t_whatstop = SUSPEND_NORMAL;
3220Sstevel@tonic-gate 		(void) new_mstate(t, LMS_SLEEP);
3230Sstevel@tonic-gate 		mutex_enter(&swap_cpr_lock);
3240Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
3250Sstevel@tonic-gate 		mutex_exit(&swap_cpr_lock);
3260Sstevel@tonic-gate 		thread_stop(t);		/* change state and drop lock */
3270Sstevel@tonic-gate 		swtch();
3280Sstevel@tonic-gate 		mutex_enter(&swap_cpr_lock);
3290Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &swap_cpr_lock);
3300Sstevel@tonic-gate 		mutex_exit(&swap_cpr_lock);
3310Sstevel@tonic-gate 		goto loop;
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	/*
3350Sstevel@tonic-gate 	 * Decide how deserving this process is to be brought in.
3360Sstevel@tonic-gate 	 * Needs is an estimate of how much core the process will
3370Sstevel@tonic-gate 	 * need.  If the process has been out for a while, then we
3380Sstevel@tonic-gate 	 * will bring it in with 1/2 the core needed, otherwise
3390Sstevel@tonic-gate 	 * we are conservative.
3400Sstevel@tonic-gate 	 */
3410Sstevel@tonic-gate 	divisor = 1;
3420Sstevel@tonic-gate 	swapout_time = (lbolt - swapin_proc_time) / hz;
3430Sstevel@tonic-gate 	if (swapout_time > maxslp / 2)
3440Sstevel@tonic-gate 		divisor = 2;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	needs = MIN(swapin_prp->p_swrss, lotsfree);
3470Sstevel@tonic-gate 	needs = MAX(needs, min_procsize);
3480Sstevel@tonic-gate 	needs = needs / divisor;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	/*
3510Sstevel@tonic-gate 	 * Use freemem, since we want processes to be swapped
3520Sstevel@tonic-gate 	 * in quickly.
3530Sstevel@tonic-gate 	 */
3540Sstevel@tonic-gate 	avail = freemem - deficit;
3550Sstevel@tonic-gate 	if (avail > (spgcnt_t)needs) {
3560Sstevel@tonic-gate 		deficit += needs;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 		TRACE_2(TR_FAC_SCHED, TR_SWAPIN_VALUES,
3590Sstevel@tonic-gate 		    "swapin_values: prp %p needs %lu", swapin_prp, needs);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 		if (swapin(swapin_prp)) {
3620Sstevel@tonic-gate 			mutex_exit(&swapin_prp->p_lock);
3630Sstevel@tonic-gate 			goto loop;
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 		deficit -= MIN(needs, deficit);
3660Sstevel@tonic-gate 		mutex_exit(&swapin_prp->p_lock);
3670Sstevel@tonic-gate 	} else {
3680Sstevel@tonic-gate 		mutex_exit(&swapin_prp->p_lock);
3690Sstevel@tonic-gate 		/*
3700Sstevel@tonic-gate 		 * If deficit is high, too many processes have been
3710Sstevel@tonic-gate 		 * swapped in so wait a sec before attempting to
3720Sstevel@tonic-gate 		 * swapin more.
3730Sstevel@tonic-gate 		 */
3740Sstevel@tonic-gate 		if (freemem > needs) {
3750Sstevel@tonic-gate 			TRACE_2(TR_FAC_SCHED, TR_HIGH_DEFICIT,
3760Sstevel@tonic-gate 			    "deficit: prp %p needs %lu", swapin_prp, needs);
3770Sstevel@tonic-gate 			goto block;
3780Sstevel@tonic-gate 		}
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	TRACE_2(TR_FAC_SCHED, TR_UNLOAD,
3820Sstevel@tonic-gate 	    "unload: prp %p needs %lu", swapin_prp, needs);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate unload:
3850Sstevel@tonic-gate 	/*
3860Sstevel@tonic-gate 	 * Unload all unloadable modules, free all other memory
3870Sstevel@tonic-gate 	 * resources we can find, then look for a thread to hardswap.
3880Sstevel@tonic-gate 	 */
3890Sstevel@tonic-gate 	modreap();
3900Sstevel@tonic-gate 	segkp_cache_free();
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	swapout_prp = NULL;
3930Sstevel@tonic-gate 	mutex_enter(&pidlock);
3940Sstevel@tonic-gate 	for (prp = practive; prp != NULL; prp = prp->p_next) {
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 		/*
3970Sstevel@tonic-gate 		 * No need to soft swap if we have sufficient
3980Sstevel@tonic-gate 		 * memory.
3990Sstevel@tonic-gate 		 */
4000Sstevel@tonic-gate 		if (not_swappable(prp))
4010Sstevel@tonic-gate 			continue;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 		if (avefree > minfree ||
4040Sstevel@tonic-gate 		    avefree < minfree && freemem > desfree) {
4050Sstevel@tonic-gate 			swapout_prp = NULL;
4060Sstevel@tonic-gate 			break;
4070Sstevel@tonic-gate 		}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 		/*
410390Sraf 		 * Skip processes that are exiting
411390Sraf 		 * or whose address spaces are locked.
4120Sstevel@tonic-gate 		 */
4130Sstevel@tonic-gate 		mutex_enter(&prp->p_lock);
414390Sraf 		if ((prp->p_flag & SEXITING) ||
4150Sstevel@tonic-gate 		    (prp->p_as != NULL && AS_ISPGLCK(prp->p_as))) {
4160Sstevel@tonic-gate 			mutex_exit(&prp->p_lock);
4170Sstevel@tonic-gate 			continue;
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 		proc_pri = -1;
4210Sstevel@tonic-gate 		t = prp->p_tlist;
4220Sstevel@tonic-gate 		do {
4230Sstevel@tonic-gate 			if ((t->t_schedflag & (TS_SWAPENQ |
4240Sstevel@tonic-gate 			    TS_ON_SWAPQ | TS_LOAD)) != TS_LOAD)
4250Sstevel@tonic-gate 				continue;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 			thread_lock(t);
4280Sstevel@tonic-gate 			thread_pri = CL_SWAPOUT(t, HARDSWAP);
4290Sstevel@tonic-gate 			thread_unlock(t);
4300Sstevel@tonic-gate 			if (thread_pri > proc_pri)
4310Sstevel@tonic-gate 				proc_pri = thread_pri;
4320Sstevel@tonic-gate 		} while ((t = t->t_forw) != prp->p_tlist);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 		mutex_exit(&prp->p_lock);
4350Sstevel@tonic-gate 		if (proc_pri == -1)
4360Sstevel@tonic-gate 			continue;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 		/*
4390Sstevel@tonic-gate 		 * Swapout processes sleeping with a lower priority
4400Sstevel@tonic-gate 		 * than the one currently being swapped in, if any.
4410Sstevel@tonic-gate 		 */
4420Sstevel@tonic-gate 		if (swapin_prp == NULL || swapin_pri > proc_pri) {
4430Sstevel@tonic-gate 			TRACE_2(TR_FAC_SCHED, TR_CHOOSE_SWAPOUT,
4440Sstevel@tonic-gate 			    "hardswap: prp %p needs %lu", prp, needs);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 			if (swapout_prp == NULL || proc_pri < chosen_pri) {
4470Sstevel@tonic-gate 				swapout_prp = prp;
4480Sstevel@tonic-gate 				chosen_pri = proc_pri;
4490Sstevel@tonic-gate 			}
4500Sstevel@tonic-gate 		}
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	/*
4540Sstevel@tonic-gate 	 * Acquire the "p_lock" before dropping "pidlock"
4550Sstevel@tonic-gate 	 * to prevent the proc structure from being freed
4560Sstevel@tonic-gate 	 * if the process exits before swapout completes.
4570Sstevel@tonic-gate 	 */
4580Sstevel@tonic-gate 	if (swapout_prp != NULL)
4590Sstevel@tonic-gate 		mutex_enter(&swapout_prp->p_lock);
4600Sstevel@tonic-gate 	mutex_exit(&pidlock);
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	if ((prp = swapout_prp) != NULL) {
4630Sstevel@tonic-gate 		uint_t swrss = 0;
4640Sstevel@tonic-gate 		int swapped;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 		swapped = swapout(prp, &swrss, HARDSWAP);
4670Sstevel@tonic-gate 		if (swapped) {
4680Sstevel@tonic-gate 			/*
4690Sstevel@tonic-gate 			 * If desperate, we want to give the space obtained
4700Sstevel@tonic-gate 			 * by swapping this process out to processes in core,
4710Sstevel@tonic-gate 			 * so we give them a chance by increasing deficit.
4720Sstevel@tonic-gate 			 */
4730Sstevel@tonic-gate 			prp->p_swrss += swrss;
4740Sstevel@tonic-gate 			if (desperate)
4750Sstevel@tonic-gate 				deficit += MIN(prp->p_swrss, lotsfree);
4760Sstevel@tonic-gate 			hardswap++;
4770Sstevel@tonic-gate 		}
4780Sstevel@tonic-gate 		mutex_exit(&swapout_prp->p_lock);
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		if (swapped)
4810Sstevel@tonic-gate 			goto loop;
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	/*
4850Sstevel@tonic-gate 	 * Delay for 1 second and look again later.
4860Sstevel@tonic-gate 	 */
4870Sstevel@tonic-gate 	TRACE_3(TR_FAC_SCHED, TR_RUNIN,
4880Sstevel@tonic-gate 	    "schedrunin:runin nswapped: %d, avefree: %ld freemem: %ld",
4890Sstevel@tonic-gate 	    nswapped, avefree, freemem);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate block:
4920Sstevel@tonic-gate 	t = curthread;
4930Sstevel@tonic-gate 	thread_lock(t);
4940Sstevel@tonic-gate 	runin++;
4950Sstevel@tonic-gate 	t->t_schedflag |= (TS_ALLSTART & ~TS_CSTART);
4960Sstevel@tonic-gate 	t->t_whystop = PR_SUSPENDED;
4970Sstevel@tonic-gate 	t->t_whatstop = SUSPEND_NORMAL;
4980Sstevel@tonic-gate 	(void) new_mstate(t, LMS_SLEEP);
4990Sstevel@tonic-gate 	mutex_enter(&swap_cpr_lock);
5000Sstevel@tonic-gate 	CALLB_CPR_SAFE_BEGIN(&cprinfo);
5010Sstevel@tonic-gate 	mutex_exit(&swap_cpr_lock);
5020Sstevel@tonic-gate 	thread_stop(t);		/* change to stop state and drop lock */
5030Sstevel@tonic-gate 	swtch();
5040Sstevel@tonic-gate 	mutex_enter(&swap_cpr_lock);
5050Sstevel@tonic-gate 	CALLB_CPR_SAFE_END(&cprinfo, &swap_cpr_lock);
5060Sstevel@tonic-gate 	mutex_exit(&swap_cpr_lock);
5070Sstevel@tonic-gate 	goto loop;
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate /*
5110Sstevel@tonic-gate  * Remove the specified thread from the swap queue.
5120Sstevel@tonic-gate  */
5130Sstevel@tonic-gate static void
5140Sstevel@tonic-gate swapdeq(kthread_id_t tp)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	kthread_id_t *tpp;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(tp));
5190Sstevel@tonic-gate 	ASSERT(tp->t_schedflag & TS_ON_SWAPQ);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	tpp = &tswap_queue;
5220Sstevel@tonic-gate 	for (;;) {
5230Sstevel@tonic-gate 		ASSERT(*tpp != NULL);
5240Sstevel@tonic-gate 		if (*tpp == tp)
5250Sstevel@tonic-gate 			break;
5260Sstevel@tonic-gate 		tpp = &(*tpp)->t_link;
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 	*tpp = tp->t_link;
5290Sstevel@tonic-gate 	tp->t_schedflag &= ~TS_ON_SWAPQ;
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate /*
5330Sstevel@tonic-gate  * Swap in lwps.  Returns nonzero on success (i.e., if at least one lwp is
5340Sstevel@tonic-gate  * swapped in) and 0 on failure.
5350Sstevel@tonic-gate  */
5360Sstevel@tonic-gate static int
5370Sstevel@tonic-gate swapin(proc_t *pp)
5380Sstevel@tonic-gate {
5390Sstevel@tonic-gate 	kthread_id_t tp;
5400Sstevel@tonic-gate 	int err;
5410Sstevel@tonic-gate 	int num_swapped_in = 0;
5420Sstevel@tonic-gate 	struct cpu *cpup = CPU;
5430Sstevel@tonic-gate 	pri_t thread_pri;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pp->p_lock));
5460Sstevel@tonic-gate 	ASSERT(pp->p_swapcnt);
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate top:
5490Sstevel@tonic-gate 	tp = pp->p_tlist;
5500Sstevel@tonic-gate 	do {
5510Sstevel@tonic-gate 		/*
5520Sstevel@tonic-gate 		 * Only swapin eligible lwps (specified by the scheduling
5530Sstevel@tonic-gate 		 * class) which are unloaded and ready to run.
5540Sstevel@tonic-gate 		 */
5550Sstevel@tonic-gate 		thread_lock(tp);
5560Sstevel@tonic-gate 		thread_pri = CL_SWAPIN(tp, 0);
5570Sstevel@tonic-gate 		if (thread_pri != -1 && tp->t_state == TS_RUN &&
5580Sstevel@tonic-gate 		    (tp->t_schedflag & TS_LOAD) == 0) {
5590Sstevel@tonic-gate 			size_t stack_size;
5600Sstevel@tonic-gate 			pgcnt_t stack_pages;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 			ASSERT((tp->t_schedflag & TS_ON_SWAPQ) == 0);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 			thread_unlock(tp);
5650Sstevel@tonic-gate 			/*
5660Sstevel@tonic-gate 			 * Now drop the p_lock since the stack needs
5670Sstevel@tonic-gate 			 * to brought in.
5680Sstevel@tonic-gate 			 */
5690Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 			stack_size = swapsize(tp->t_swap);
5720Sstevel@tonic-gate 			stack_pages = btopr(stack_size);
5730Sstevel@tonic-gate 			/* Kernel probe */
5740Sstevel@tonic-gate 			TNF_PROBE_4(swapin_lwp, "vm swap swapin", /* CSTYLED */,
5750Sstevel@tonic-gate 				tnf_pid,	pid,		pp->p_pid,
5760Sstevel@tonic-gate 				tnf_lwpid,	lwpid,		tp->t_tid,
5770Sstevel@tonic-gate 				tnf_kthread_id,	tid,		tp,
5780Sstevel@tonic-gate 				tnf_ulong,	page_count,	stack_pages);
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 			rw_enter(&kas.a_lock, RW_READER);
5810Sstevel@tonic-gate 			err = segkp_fault(segkp->s_as->a_hat, segkp,
5820Sstevel@tonic-gate 			    tp->t_swap, stack_size, F_SOFTLOCK, S_OTHER);
5830Sstevel@tonic-gate 			rw_exit(&kas.a_lock);
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 			/*
5860Sstevel@tonic-gate 			 * Re-acquire the p_lock.
5870Sstevel@tonic-gate 			 */
5880Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
5890Sstevel@tonic-gate 			if (err) {
5900Sstevel@tonic-gate 				num_swapped_in = 0;
5910Sstevel@tonic-gate 				break;
5920Sstevel@tonic-gate 			} else {
593*1483Svb70745 #ifdef __sparc
594*1483Svb70745 				lwp_swapin(tp);
595*1483Svb70745 #endif /* __sparc */
5960Sstevel@tonic-gate 				CPU_STATS_ADDQ(cpup, vm, swapin, 1);
5970Sstevel@tonic-gate 				CPU_STATS_ADDQ(cpup, vm, pgswapin,
5980Sstevel@tonic-gate 				    stack_pages);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 				pp->p_swapcnt--;
6010Sstevel@tonic-gate 				pp->p_swrss -= stack_pages;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 				thread_lock(tp);
6040Sstevel@tonic-gate 				tp->t_schedflag |= TS_LOAD;
6050Sstevel@tonic-gate 				dq_sruninc(tp);
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 				tp->t_stime = lbolt;	/* set swapin time */
6080Sstevel@tonic-gate 				thread_unlock(tp);
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 				nswapped--;
6110Sstevel@tonic-gate 				tot_swapped_in++;
6120Sstevel@tonic-gate 				num_swapped_in++;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 				TRACE_2(TR_FAC_SCHED, TR_SWAPIN,
6150Sstevel@tonic-gate 				    "swapin: pp %p stack_pages %lu",
6160Sstevel@tonic-gate 				    pp, stack_pages);
6170Sstevel@tonic-gate 				goto top;
6180Sstevel@tonic-gate 			}
6190Sstevel@tonic-gate 		}
6200Sstevel@tonic-gate 		thread_unlock(tp);
6210Sstevel@tonic-gate 	} while ((tp = tp->t_forw) != pp->p_tlist);
6220Sstevel@tonic-gate 	return (num_swapped_in);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate /*
6260Sstevel@tonic-gate  * Swap out lwps.  Returns nonzero on success (i.e., if at least one lwp is
6270Sstevel@tonic-gate  * swapped out) and 0 on failure.
6280Sstevel@tonic-gate  */
6290Sstevel@tonic-gate static int
6300Sstevel@tonic-gate swapout(proc_t *pp, uint_t *swrss, int swapflags)
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate 	kthread_id_t tp;
6330Sstevel@tonic-gate 	pgcnt_t ws_pages = 0;
6340Sstevel@tonic-gate 	int err;
6350Sstevel@tonic-gate 	int swapped_lwps = 0;
6360Sstevel@tonic-gate 	struct as *as = pp->p_as;
6370Sstevel@tonic-gate 	struct cpu *cpup = CPU;
6380Sstevel@tonic-gate 	pri_t thread_pri;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pp->p_lock));
6410Sstevel@tonic-gate 
642390Sraf 	if (pp->p_flag & SEXITING)
6430Sstevel@tonic-gate 		return (0);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate top:
6460Sstevel@tonic-gate 	tp = pp->p_tlist;
6470Sstevel@tonic-gate 	do {
6480Sstevel@tonic-gate 		klwp_t *lwp = ttolwp(tp);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 		/*
6510Sstevel@tonic-gate 		 * Swapout eligible lwps (specified by the scheduling
6520Sstevel@tonic-gate 		 * class) which don't have TS_DONT_SWAP set.  Set the
6530Sstevel@tonic-gate 		 * "intent to swap" flag (TS_SWAPENQ) on threads
6540Sstevel@tonic-gate 		 * which have TS_DONT_SWAP set so that they can be
6550Sstevel@tonic-gate 		 * swapped if and when they reach a safe point.
6560Sstevel@tonic-gate 		 */
6570Sstevel@tonic-gate 		thread_lock(tp);
6580Sstevel@tonic-gate 		thread_pri = CL_SWAPOUT(tp, swapflags);
6590Sstevel@tonic-gate 		if (thread_pri != -1) {
6600Sstevel@tonic-gate 			if (tp->t_schedflag & TS_DONT_SWAP) {
6610Sstevel@tonic-gate 				tp->t_schedflag |= TS_SWAPENQ;
6620Sstevel@tonic-gate 				tp->t_trapret = 1;
6630Sstevel@tonic-gate 				aston(tp);
6640Sstevel@tonic-gate 			} else {
6650Sstevel@tonic-gate 				pgcnt_t stack_pages;
6660Sstevel@tonic-gate 				size_t stack_size;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 				ASSERT((tp->t_schedflag &
6690Sstevel@tonic-gate 				    (TS_DONT_SWAP | TS_LOAD)) == TS_LOAD);
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 				if (lock_try(&tp->t_lock)) {
6720Sstevel@tonic-gate 					/*
6730Sstevel@tonic-gate 					 * Remove thread from the swap_queue.
6740Sstevel@tonic-gate 					 */
6750Sstevel@tonic-gate 					if (tp->t_schedflag & TS_ON_SWAPQ) {
6760Sstevel@tonic-gate 						ASSERT(!(tp->t_schedflag &
6770Sstevel@tonic-gate 						    TS_SWAPENQ));
6780Sstevel@tonic-gate 						swapdeq(tp);
6790Sstevel@tonic-gate 					} else if (tp->t_state == TS_RUN)
6800Sstevel@tonic-gate 						dq_srundec(tp);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 					tp->t_schedflag &=
6830Sstevel@tonic-gate 					    ~(TS_LOAD | TS_SWAPENQ);
6840Sstevel@tonic-gate 					lock_clear(&tp->t_lock);
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 					/*
6870Sstevel@tonic-gate 					 * Set swapout time if the thread isn't
6880Sstevel@tonic-gate 					 * sleeping.
6890Sstevel@tonic-gate 					 */
6900Sstevel@tonic-gate 					if (tp->t_state != TS_SLEEP)
6910Sstevel@tonic-gate 						tp->t_stime = lbolt;
6920Sstevel@tonic-gate 					thread_unlock(tp);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 					nswapped++;
6950Sstevel@tonic-gate 					tot_swapped_out++;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 					lwp->lwp_ru.nswap++;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 					/*
7000Sstevel@tonic-gate 					 * Now drop the p_lock since the
7010Sstevel@tonic-gate 					 * stack needs to pushed out.
7020Sstevel@tonic-gate 					 */
7030Sstevel@tonic-gate 					mutex_exit(&pp->p_lock);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 					stack_size = swapsize(tp->t_swap);
7060Sstevel@tonic-gate 					stack_pages = btopr(stack_size);
7070Sstevel@tonic-gate 					ws_pages += stack_pages;
7080Sstevel@tonic-gate 					/* Kernel probe */
7090Sstevel@tonic-gate 					TNF_PROBE_4(swapout_lwp,
7100Sstevel@tonic-gate 						"vm swap swapout",
7110Sstevel@tonic-gate 						/* CSTYLED */,
7120Sstevel@tonic-gate 						tnf_pid, pid, pp->p_pid,
7130Sstevel@tonic-gate 						tnf_lwpid, lwpid, tp->t_tid,
7140Sstevel@tonic-gate 						tnf_kthread_id, tid, tp,
7150Sstevel@tonic-gate 						tnf_ulong, page_count,
7160Sstevel@tonic-gate 							stack_pages);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 					rw_enter(&kas.a_lock, RW_READER);
7190Sstevel@tonic-gate 					err = segkp_fault(segkp->s_as->a_hat,
7200Sstevel@tonic-gate 					    segkp, tp->t_swap, stack_size,
7210Sstevel@tonic-gate 					    F_SOFTUNLOCK, S_WRITE);
7220Sstevel@tonic-gate 					rw_exit(&kas.a_lock);
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 					if (err) {
7250Sstevel@tonic-gate 						cmn_err(CE_PANIC,
7260Sstevel@tonic-gate 						    "swapout: segkp_fault "
7270Sstevel@tonic-gate 						    "failed err: %d", err);
7280Sstevel@tonic-gate 					}
7290Sstevel@tonic-gate 					CPU_STATS_ADDQ(cpup,
7300Sstevel@tonic-gate 					    vm, pgswapout, stack_pages);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 					mutex_enter(&pp->p_lock);
7330Sstevel@tonic-gate 					pp->p_swapcnt++;
7340Sstevel@tonic-gate 					swapped_lwps++;
7350Sstevel@tonic-gate 					goto top;
7360Sstevel@tonic-gate 				}
7370Sstevel@tonic-gate 			}
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 		thread_unlock(tp);
7400Sstevel@tonic-gate 	} while ((tp = tp->t_forw) != pp->p_tlist);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	/*
7430Sstevel@tonic-gate 	 * Unload address space when all lwps are swapped out.
7440Sstevel@tonic-gate 	 */
7450Sstevel@tonic-gate 	if (pp->p_swapcnt == pp->p_lwpcnt) {
7460Sstevel@tonic-gate 		size_t as_size = 0;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 		/*
7490Sstevel@tonic-gate 		 * Avoid invoking as_swapout() if the process has
7500Sstevel@tonic-gate 		 * no MMU resources since pageout will eventually
7510Sstevel@tonic-gate 		 * steal pages belonging to this address space.  This
7520Sstevel@tonic-gate 		 * saves CPU cycles as the number of pages that are
7530Sstevel@tonic-gate 		 * potentially freed or pushed out by the segment
7540Sstevel@tonic-gate 		 * swapout operation is very small.
7550Sstevel@tonic-gate 		 */
7560Sstevel@tonic-gate 		if (rm_asrss(pp->p_as) != 0)
7570Sstevel@tonic-gate 			as_size = as_swapout(as);
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 		CPU_STATS_ADDQ(cpup, vm, pgswapout, btop(as_size));
7600Sstevel@tonic-gate 		CPU_STATS_ADDQ(cpup, vm, swapout, 1);
7610Sstevel@tonic-gate 		ws_pages += btop(as_size);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		TRACE_2(TR_FAC_SCHED, TR_SWAPOUT,
7640Sstevel@tonic-gate 		    "swapout: pp %p pages_pushed %lu", pp, ws_pages);
7650Sstevel@tonic-gate 		/* Kernel probe */
7660Sstevel@tonic-gate 		TNF_PROBE_2(swapout_process, "vm swap swapout", /* CSTYLED */,
7670Sstevel@tonic-gate 			tnf_pid,	pid,		pp->p_pid,
7680Sstevel@tonic-gate 			tnf_ulong,	page_count,	ws_pages);
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	*swrss = ws_pages;
7710Sstevel@tonic-gate 	return (swapped_lwps);
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate void
7750Sstevel@tonic-gate swapout_lwp(klwp_t *lwp)
7760Sstevel@tonic-gate {
7770Sstevel@tonic-gate 	kthread_id_t tp = curthread;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	ASSERT(curthread == lwptot(lwp));
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	/*
7820Sstevel@tonic-gate 	 * Don't insert the thread onto the swap queue if
7830Sstevel@tonic-gate 	 * sufficient memory is available.
7840Sstevel@tonic-gate 	 */
7850Sstevel@tonic-gate 	if (avefree > desfree || avefree < desfree && freemem > desfree) {
7860Sstevel@tonic-gate 		thread_lock(tp);
7870Sstevel@tonic-gate 		tp->t_schedflag &= ~TS_SWAPENQ;
7880Sstevel@tonic-gate 		thread_unlock(tp);
7890Sstevel@tonic-gate 		return;
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	/*
7930Sstevel@tonic-gate 	 * Lock the thread, then move it to the swapped queue from the
7940Sstevel@tonic-gate 	 * onproc queue and set its state to be TS_RUN.
7950Sstevel@tonic-gate 	 */
7960Sstevel@tonic-gate 	thread_lock(tp);
7970Sstevel@tonic-gate 	ASSERT(tp->t_state == TS_ONPROC);
7980Sstevel@tonic-gate 	if (tp->t_schedflag & TS_SWAPENQ) {
7990Sstevel@tonic-gate 		tp->t_schedflag &= ~TS_SWAPENQ;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 		/*
8020Sstevel@tonic-gate 		 * Set the state of this thread to be runnable
8030Sstevel@tonic-gate 		 * and move it from the onproc queue to the swap queue.
8040Sstevel@tonic-gate 		 */
8050Sstevel@tonic-gate 		disp_swapped_enq(tp);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 		/*
8080Sstevel@tonic-gate 		 * Insert the thread onto the swap queue.
8090Sstevel@tonic-gate 		 */
8100Sstevel@tonic-gate 		tp->t_link = tswap_queue;
8110Sstevel@tonic-gate 		tswap_queue = tp;
8120Sstevel@tonic-gate 		tp->t_schedflag |= TS_ON_SWAPQ;
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 		thread_unlock_nopreempt(tp);
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 		TRACE_1(TR_FAC_SCHED, TR_SWAPOUT_LWP, "swapout_lwp:%x", lwp);
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 		swtch();
8190Sstevel@tonic-gate 	} else {
8200Sstevel@tonic-gate 		thread_unlock(tp);
8210Sstevel@tonic-gate 	}
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate /*
8250Sstevel@tonic-gate  * Swap all threads on the swap queue.
8260Sstevel@tonic-gate  */
8270Sstevel@tonic-gate static void
8280Sstevel@tonic-gate process_swap_queue(void)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate 	kthread_id_t tp;
8310Sstevel@tonic-gate 	uint_t ws_pages;
8320Sstevel@tonic-gate 	proc_t *pp;
8330Sstevel@tonic-gate 	struct cpu *cpup = CPU;
8340Sstevel@tonic-gate 	klwp_t *lwp;
8350Sstevel@tonic-gate 	int err;
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	if (tswap_queue == NULL)
8380Sstevel@tonic-gate 		return;
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	/*
8410Sstevel@tonic-gate 	 * Acquire the "swapped_lock" which locks the swap queue,
8420Sstevel@tonic-gate 	 * and unload the stacks of all threads on it.
8430Sstevel@tonic-gate 	 */
8440Sstevel@tonic-gate 	disp_lock_enter(&swapped_lock);
8450Sstevel@tonic-gate 	while ((tp = tswap_queue) != NULL) {
8460Sstevel@tonic-gate 		pgcnt_t stack_pages;
8470Sstevel@tonic-gate 		size_t stack_size;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 		tswap_queue = tp->t_link;
8500Sstevel@tonic-gate 		tp->t_link = NULL;
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 		/*
8530Sstevel@tonic-gate 		 * Drop the "dispatcher lock" before acquiring "t_lock"
8540Sstevel@tonic-gate 		 * to avoid spinning on it since the thread at the front
8550Sstevel@tonic-gate 		 * of the swap queue could be pinned before giving up
8560Sstevel@tonic-gate 		 * its "t_lock" in resume.
8570Sstevel@tonic-gate 		 */
8580Sstevel@tonic-gate 		disp_lock_exit(&swapped_lock);
8590Sstevel@tonic-gate 		lock_set(&tp->t_lock);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 		/*
8620Sstevel@tonic-gate 		 * Now, re-acquire the "swapped_lock".  Acquiring this lock
8630Sstevel@tonic-gate 		 * results in locking the thread since its dispatcher lock
8640Sstevel@tonic-gate 		 * (t_lockp) is the "swapped_lock".
8650Sstevel@tonic-gate 		 */
8660Sstevel@tonic-gate 		disp_lock_enter(&swapped_lock);
8670Sstevel@tonic-gate 		ASSERT(tp->t_state == TS_RUN);
8680Sstevel@tonic-gate 		ASSERT(tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ));
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 		tp->t_schedflag &= ~(TS_LOAD | TS_ON_SWAPQ);
8710Sstevel@tonic-gate 		tp->t_stime = lbolt;		/* swapout time */
8720Sstevel@tonic-gate 		disp_lock_exit(&swapped_lock);
8730Sstevel@tonic-gate 		lock_clear(&tp->t_lock);
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 		lwp = ttolwp(tp);
8760Sstevel@tonic-gate 		lwp->lwp_ru.nswap++;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 		pp = ttoproc(tp);
8790Sstevel@tonic-gate 		stack_size = swapsize(tp->t_swap);
8800Sstevel@tonic-gate 		stack_pages = btopr(stack_size);
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 		/* Kernel probe */
8830Sstevel@tonic-gate 		TNF_PROBE_4(swapout_lwp, "vm swap swapout", /* CSTYLED */,
8840Sstevel@tonic-gate 			tnf_pid,	pid,		pp->p_pid,
8850Sstevel@tonic-gate 			tnf_lwpid,	lwpid,		tp->t_tid,
8860Sstevel@tonic-gate 			tnf_kthread_id,	tid,		tp,
8870Sstevel@tonic-gate 			tnf_ulong,	page_count,	stack_pages);
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 		rw_enter(&kas.a_lock, RW_READER);
8900Sstevel@tonic-gate 		err = segkp_fault(segkp->s_as->a_hat, segkp, tp->t_swap,
8910Sstevel@tonic-gate 		    stack_size, F_SOFTUNLOCK, S_WRITE);
8920Sstevel@tonic-gate 		rw_exit(&kas.a_lock);
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 		if (err) {
8950Sstevel@tonic-gate 			cmn_err(CE_PANIC,
8960Sstevel@tonic-gate 			"process_swap_list: segkp_fault failed err: %d", err);
8970Sstevel@tonic-gate 		}
8980Sstevel@tonic-gate 		CPU_STATS_ADDQ(cpup, vm, pgswapout, stack_pages);
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 		nswapped++;
9010Sstevel@tonic-gate 		tot_swapped_out++;
9020Sstevel@tonic-gate 		swapqswap++;
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 		/*
9050Sstevel@tonic-gate 		 * Don't need p_lock since the swapper is the only
9060Sstevel@tonic-gate 		 * thread which increments/decrements p_swapcnt and p_swrss.
9070Sstevel@tonic-gate 		 */
9080Sstevel@tonic-gate 		ws_pages = stack_pages;
9090Sstevel@tonic-gate 		pp->p_swapcnt++;
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 		TRACE_1(TR_FAC_SCHED, TR_SWAPQ_LWP, "swaplist: pp %p", pp);
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 		/*
9140Sstevel@tonic-gate 		 * Unload address space when all lwps are swapped out.
9150Sstevel@tonic-gate 		 */
9160Sstevel@tonic-gate 		if (pp->p_swapcnt == pp->p_lwpcnt) {
9170Sstevel@tonic-gate 			size_t as_size = 0;
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 			if (rm_asrss(pp->p_as) != 0)
9200Sstevel@tonic-gate 				as_size = as_swapout(pp->p_as);
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 			CPU_STATS_ADDQ(cpup, vm, pgswapout,
9230Sstevel@tonic-gate 			    btop(as_size));
9240Sstevel@tonic-gate 			CPU_STATS_ADDQ(cpup, vm, swapout, 1);
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 			ws_pages += btop(as_size);
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 			TRACE_2(TR_FAC_SCHED, TR_SWAPQ_PROC,
9290Sstevel@tonic-gate 			    "swaplist_proc: pp %p pages_pushed: %lu",
9300Sstevel@tonic-gate 			    pp, ws_pages);
9310Sstevel@tonic-gate 			/* Kernel probe */
9320Sstevel@tonic-gate 			TNF_PROBE_2(swapout_process, "vm swap swapout",
9330Sstevel@tonic-gate 				/* CSTYLED */,
9340Sstevel@tonic-gate 				tnf_pid,	pid,		pp->p_pid,
9350Sstevel@tonic-gate 				tnf_ulong,	page_count,	ws_pages);
9360Sstevel@tonic-gate 		}
9370Sstevel@tonic-gate 		pp->p_swrss += ws_pages;
9380Sstevel@tonic-gate 		disp_lock_enter(&swapped_lock);
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 	disp_lock_exit(&swapped_lock);
9410Sstevel@tonic-gate }
942