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