xref: /onnv-gate/usr/src/uts/common/os/vm_pageout.c (revision 6118:1c376397422f)
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
53290Sjohansen  * Common Development and Distribution License (the "License").
63290Sjohansen  * 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  */
210Sstevel@tonic-gate /*
22*6118Sjimp  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/t_lock.h>
430Sstevel@tonic-gate #include <sys/param.h>
440Sstevel@tonic-gate #include <sys/buf.h>
450Sstevel@tonic-gate #include <sys/uio.h>
460Sstevel@tonic-gate #include <sys/proc.h>
470Sstevel@tonic-gate #include <sys/systm.h>
480Sstevel@tonic-gate #include <sys/mman.h>
490Sstevel@tonic-gate #include <sys/cred.h>
500Sstevel@tonic-gate #include <sys/vnode.h>
510Sstevel@tonic-gate #include <sys/vm.h>
520Sstevel@tonic-gate #include <sys/vmparam.h>
530Sstevel@tonic-gate #include <sys/vtrace.h>
540Sstevel@tonic-gate #include <sys/cmn_err.h>
550Sstevel@tonic-gate #include <sys/cpuvar.h>
560Sstevel@tonic-gate #include <sys/user.h>
570Sstevel@tonic-gate #include <sys/kmem.h>
580Sstevel@tonic-gate #include <sys/debug.h>
590Sstevel@tonic-gate #include <sys/callb.h>
600Sstevel@tonic-gate #include <sys/tnf_probe.h>
610Sstevel@tonic-gate #include <sys/mem_cage.h>
620Sstevel@tonic-gate #include <sys/time.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <vm/hat.h>
650Sstevel@tonic-gate #include <vm/as.h>
660Sstevel@tonic-gate #include <vm/seg.h>
670Sstevel@tonic-gate #include <vm/page.h>
680Sstevel@tonic-gate #include <vm/pvn.h>
690Sstevel@tonic-gate #include <vm/seg_kmem.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static int checkpage(page_t *, int);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * The following parameters control operation of the page replacement
750Sstevel@tonic-gate  * algorithm.  They are initialized to 0, and then computed at boot time
760Sstevel@tonic-gate  * based on the size of the system.  If they are patched non-zero in
770Sstevel@tonic-gate  * a loaded vmunix they are left alone and may thus be changed per system
780Sstevel@tonic-gate  * using adb on the loaded system.
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate pgcnt_t		slowscan = 0;
810Sstevel@tonic-gate pgcnt_t		fastscan = 0;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static pgcnt_t	handspreadpages = 0;
840Sstevel@tonic-gate static int	loopfraction = 2;
850Sstevel@tonic-gate static pgcnt_t	looppages;
860Sstevel@tonic-gate static int	min_percent_cpu = 4;
870Sstevel@tonic-gate static int	max_percent_cpu = 80;
880Sstevel@tonic-gate static pgcnt_t	maxfastscan = 0;
890Sstevel@tonic-gate static pgcnt_t	maxslowscan = 100;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate pgcnt_t	maxpgio = 0;
920Sstevel@tonic-gate pgcnt_t	minfree = 0;
930Sstevel@tonic-gate pgcnt_t	desfree = 0;
940Sstevel@tonic-gate pgcnt_t	lotsfree = 0;
950Sstevel@tonic-gate pgcnt_t	needfree = 0;
960Sstevel@tonic-gate pgcnt_t	throttlefree = 0;
970Sstevel@tonic-gate pgcnt_t	pageout_reserve = 0;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate pgcnt_t	deficit;
1000Sstevel@tonic-gate pgcnt_t	nscan;
1010Sstevel@tonic-gate pgcnt_t	desscan;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * Values for min_pageout_ticks, max_pageout_ticks and pageout_ticks
1050Sstevel@tonic-gate  * are the number of ticks in each wakeup cycle that gives the
1060Sstevel@tonic-gate  * equivalent of some underlying %CPU duty cycle.
1070Sstevel@tonic-gate  * When RATETOSCHEDPAGING is 4,  and hz is 100, pageout_scanner is
1080Sstevel@tonic-gate  * awakened every 25 clock ticks.  So, converting from %CPU to ticks
1090Sstevel@tonic-gate  * per wakeup cycle would be x% of 25, that is (x * 100) / 25.
1100Sstevel@tonic-gate  * So, for example, 4% == 1 tick and 80% == 20 ticks.
1110Sstevel@tonic-gate  *
1120Sstevel@tonic-gate  * min_pageout_ticks:
1130Sstevel@tonic-gate  *     ticks/wakeup equivalent of min_percent_cpu.
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  * max_pageout_ticks:
1160Sstevel@tonic-gate  *     ticks/wakeup equivalent of max_percent_cpu.
1170Sstevel@tonic-gate  *
1180Sstevel@tonic-gate  * pageout_ticks:
1190Sstevel@tonic-gate  *     Number of clock ticks budgeted for each wakeup cycle.
1200Sstevel@tonic-gate  *     Computed each time around by schedpaging().
1210Sstevel@tonic-gate  *     Varies between min_pageout_ticks .. max_pageout_ticks,
1220Sstevel@tonic-gate  *     depending on memory pressure.
1230Sstevel@tonic-gate  *
1240Sstevel@tonic-gate  * pageout_lbolt:
1250Sstevel@tonic-gate  *     Timestamp of the last time pageout_scanner woke up and started
1260Sstevel@tonic-gate  *     (or resumed) scanning for not recently referenced pages.
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static clock_t	min_pageout_ticks;
1300Sstevel@tonic-gate static clock_t	max_pageout_ticks;
1310Sstevel@tonic-gate static clock_t	pageout_ticks;
1320Sstevel@tonic-gate static clock_t	pageout_lbolt;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate static uint_t	reset_hands;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate #define	PAGES_POLL_MASK	1023
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate  * pageout_sample_lim:
1400Sstevel@tonic-gate  *     The limit on the number of samples needed to establish a value
1410Sstevel@tonic-gate  *     for new pageout parameters, fastscan, slowscan, and handspreadpages.
1420Sstevel@tonic-gate  *
1430Sstevel@tonic-gate  * pageout_sample_cnt:
1440Sstevel@tonic-gate  *     Current sample number.  Once the sample gets large enough,
1450Sstevel@tonic-gate  *     set new values for handspreadpages, fastscan and slowscan.
1460Sstevel@tonic-gate  *
1470Sstevel@tonic-gate  * pageout_sample_pages:
1480Sstevel@tonic-gate  *     The accumulated number of pages scanned during sampling.
1490Sstevel@tonic-gate  *
1500Sstevel@tonic-gate  * pageout_sample_ticks:
1510Sstevel@tonic-gate  *     The accumulated clock ticks for the sample.
1520Sstevel@tonic-gate  *
1530Sstevel@tonic-gate  * pageout_rate:
1540Sstevel@tonic-gate  *     Rate in pages/nanosecond, computed at the end of sampling.
1550Sstevel@tonic-gate  *
1560Sstevel@tonic-gate  * pageout_new_spread:
1570Sstevel@tonic-gate  *     The new value to use for fastscan and handspreadpages.
1580Sstevel@tonic-gate  *     Calculated after enough samples have been taken.
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate typedef hrtime_t hrrate_t;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate static uint64_t	pageout_sample_lim = 4;
1640Sstevel@tonic-gate static uint64_t	pageout_sample_cnt = 0;
1650Sstevel@tonic-gate static pgcnt_t	pageout_sample_pages = 0;
1660Sstevel@tonic-gate static hrrate_t	pageout_rate = 0;
1670Sstevel@tonic-gate static pgcnt_t	pageout_new_spread = 0;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate static clock_t	pageout_cycle_ticks;
1700Sstevel@tonic-gate static hrtime_t	sample_start, sample_end;
1710Sstevel@tonic-gate static hrtime_t	pageout_sample_etime = 0;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * Record number of times a pageout_scanner wakeup cycle finished because it
1750Sstevel@tonic-gate  * timed out (exceeded its CPU budget), rather than because it visited
1760Sstevel@tonic-gate  * its budgeted number of pages.
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate uint64_t pageout_timeouts = 0;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate #ifdef VM_STATS
1810Sstevel@tonic-gate static struct pageoutvmstats_str {
1820Sstevel@tonic-gate 	ulong_t	checkpage[3];
1830Sstevel@tonic-gate } pageoutvmstats;
1840Sstevel@tonic-gate #endif /* VM_STATS */
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate  * Threads waiting for free memory use this condition variable and lock until
1880Sstevel@tonic-gate  * memory becomes available.
1890Sstevel@tonic-gate  */
1900Sstevel@tonic-gate kmutex_t	memavail_lock;
1910Sstevel@tonic-gate kcondvar_t	memavail_cv;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate  * The size of the clock loop.
1950Sstevel@tonic-gate  */
1960Sstevel@tonic-gate #define	LOOPPAGES	total_pages
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate  * Set up the paging constants for the clock algorithm.
2000Sstevel@tonic-gate  * Called after the system is initialized and the amount of memory
2010Sstevel@tonic-gate  * and number of paging devices is known.
2020Sstevel@tonic-gate  *
2030Sstevel@tonic-gate  * lotsfree is 1/64 of memory, but at least 512K.
2040Sstevel@tonic-gate  * desfree is 1/2 of lotsfree.
2050Sstevel@tonic-gate  * minfree is 1/2 of desfree.
2060Sstevel@tonic-gate  *
2070Sstevel@tonic-gate  * Note: to revert to the paging algorithm of Solaris 2.4/2.5, set:
2080Sstevel@tonic-gate  *
2090Sstevel@tonic-gate  *	lotsfree = btop(512K)
2100Sstevel@tonic-gate  *	desfree = btop(200K)
2110Sstevel@tonic-gate  *	minfree = btop(100K)
2120Sstevel@tonic-gate  *	throttlefree = INT_MIN
2130Sstevel@tonic-gate  *	max_percent_cpu = 4
2140Sstevel@tonic-gate  */
2150Sstevel@tonic-gate void
2160Sstevel@tonic-gate setupclock(int recalc)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	static spgcnt_t init_lfree, init_dfree, init_mfree;
2200Sstevel@tonic-gate 	static spgcnt_t init_tfree, init_preserve, init_mpgio;
2210Sstevel@tonic-gate 	static spgcnt_t init_mfscan, init_fscan, init_sscan, init_hspages;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	looppages = LOOPPAGES;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/*
2260Sstevel@tonic-gate 	 * setupclock can now be called to recalculate the paging
2270Sstevel@tonic-gate 	 * parameters in the case of dynamic addition of memory.
2280Sstevel@tonic-gate 	 * So to make sure we make the proper calculations, if such a
2290Sstevel@tonic-gate 	 * situation should arise, we save away the initial values
2300Sstevel@tonic-gate 	 * of each parameter so we can recall them when needed. This
2310Sstevel@tonic-gate 	 * way we don't lose the settings an admin might have made
2320Sstevel@tonic-gate 	 * through the /etc/system file.
2330Sstevel@tonic-gate 	 */
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (!recalc) {
2360Sstevel@tonic-gate 		init_lfree = lotsfree;
2370Sstevel@tonic-gate 		init_dfree = desfree;
2380Sstevel@tonic-gate 		init_mfree = minfree;
2390Sstevel@tonic-gate 		init_tfree = throttlefree;
2400Sstevel@tonic-gate 		init_preserve = pageout_reserve;
2410Sstevel@tonic-gate 		init_mpgio = maxpgio;
2420Sstevel@tonic-gate 		init_mfscan = maxfastscan;
2430Sstevel@tonic-gate 		init_fscan = fastscan;
2440Sstevel@tonic-gate 		init_sscan = slowscan;
2450Sstevel@tonic-gate 		init_hspages = handspreadpages;
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/*
2490Sstevel@tonic-gate 	 * Set up thresholds for paging:
2500Sstevel@tonic-gate 	 */
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	/*
2530Sstevel@tonic-gate 	 * Lotsfree is threshold where paging daemon turns on.
2540Sstevel@tonic-gate 	 */
2550Sstevel@tonic-gate 	if (init_lfree == 0 || init_lfree >= looppages)
2560Sstevel@tonic-gate 		lotsfree = MAX(looppages / 64, btop(512 * 1024));
2570Sstevel@tonic-gate 	else
2580Sstevel@tonic-gate 		lotsfree = init_lfree;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	 * Desfree is amount of memory desired free.
2620Sstevel@tonic-gate 	 * If less than this for extended period, start swapping.
2630Sstevel@tonic-gate 	 */
2640Sstevel@tonic-gate 	if (init_dfree == 0 || init_dfree >= lotsfree)
2650Sstevel@tonic-gate 		desfree = lotsfree / 2;
2660Sstevel@tonic-gate 	else
2670Sstevel@tonic-gate 		desfree = init_dfree;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	/*
2700Sstevel@tonic-gate 	 * Minfree is minimal amount of free memory which is tolerable.
2710Sstevel@tonic-gate 	 */
2720Sstevel@tonic-gate 	if (init_mfree == 0 || init_mfree >= desfree)
2730Sstevel@tonic-gate 		minfree = desfree / 2;
2740Sstevel@tonic-gate 	else
2750Sstevel@tonic-gate 		minfree = init_mfree;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * Throttlefree is the point at which we start throttling
2790Sstevel@tonic-gate 	 * PG_WAIT requests until enough memory becomes available.
2800Sstevel@tonic-gate 	 */
2810Sstevel@tonic-gate 	if (init_tfree == 0 || init_tfree >= desfree)
2820Sstevel@tonic-gate 		throttlefree = minfree;
2830Sstevel@tonic-gate 	else
2840Sstevel@tonic-gate 		throttlefree = init_tfree;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/*
2870Sstevel@tonic-gate 	 * Pageout_reserve is the number of pages that we keep in
2880Sstevel@tonic-gate 	 * stock for pageout's own use.  Having a few such pages
2890Sstevel@tonic-gate 	 * provides insurance against system deadlock due to
2900Sstevel@tonic-gate 	 * pageout needing pages.  When freemem < pageout_reserve,
2910Sstevel@tonic-gate 	 * non-blocking allocations are denied to any threads
2920Sstevel@tonic-gate 	 * other than pageout and sched.  (At some point we might
2930Sstevel@tonic-gate 	 * want to consider a per-thread flag like T_PUSHING_PAGES
2940Sstevel@tonic-gate 	 * to indicate that a thread is part of the page-pushing
2950Sstevel@tonic-gate 	 * dance (e.g. an interrupt thread) and thus is entitled
2960Sstevel@tonic-gate 	 * to the same special dispensation we accord pageout.)
2970Sstevel@tonic-gate 	 */
2980Sstevel@tonic-gate 	if (init_preserve == 0 || init_preserve >= throttlefree)
2990Sstevel@tonic-gate 		pageout_reserve = throttlefree / 2;
3000Sstevel@tonic-gate 	else
3010Sstevel@tonic-gate 		pageout_reserve = init_preserve;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/*
3040Sstevel@tonic-gate 	 * Maxpgio thresholds how much paging is acceptable.
3050Sstevel@tonic-gate 	 * This figures that 2/3 busy on an arm is all that is
3060Sstevel@tonic-gate 	 * tolerable for paging.  We assume one operation per disk rev.
3070Sstevel@tonic-gate 	 *
3080Sstevel@tonic-gate 	 * XXX - Does not account for multiple swap devices.
3090Sstevel@tonic-gate 	 */
3100Sstevel@tonic-gate 	if (init_mpgio == 0)
3110Sstevel@tonic-gate 		maxpgio = (DISKRPM * 2) / 3;
3120Sstevel@tonic-gate 	else
3130Sstevel@tonic-gate 		maxpgio = init_mpgio;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	/*
3160Sstevel@tonic-gate 	 * The clock scan rate varies between fastscan and slowscan
3170Sstevel@tonic-gate 	 * based on the amount of free memory available.  Fastscan
3180Sstevel@tonic-gate 	 * rate should be set based on the number pages that can be
3190Sstevel@tonic-gate 	 * scanned per sec using ~10% of processor time.  Since this
3200Sstevel@tonic-gate 	 * value depends on the processor, MMU, Mhz etc., it is
3210Sstevel@tonic-gate 	 * difficult to determine it in a generic manner for all
3220Sstevel@tonic-gate 	 * architectures.
3230Sstevel@tonic-gate 	 *
3240Sstevel@tonic-gate 	 * Instead of trying to determine the number of pages scanned
3250Sstevel@tonic-gate 	 * per sec for every processor, fastscan is set to be the smaller
3260Sstevel@tonic-gate 	 * of 1/2 of memory or MAXHANDSPREADPAGES and the sampling
3270Sstevel@tonic-gate 	 * time is limited to ~4% of processor time.
3280Sstevel@tonic-gate 	 *
3290Sstevel@tonic-gate 	 * Setting fastscan to be 1/2 of memory allows pageout to scan
3300Sstevel@tonic-gate 	 * all of memory in ~2 secs.  This implies that user pages not
3310Sstevel@tonic-gate 	 * accessed within 1 sec (assuming, handspreadpages == fastscan)
3320Sstevel@tonic-gate 	 * can be reclaimed when free memory is very low.  Stealing pages
3330Sstevel@tonic-gate 	 * not accessed within 1 sec seems reasonable and ensures that
3340Sstevel@tonic-gate 	 * active user processes don't thrash.
3350Sstevel@tonic-gate 	 *
3360Sstevel@tonic-gate 	 * Smaller values of fastscan result in scanning fewer pages
3370Sstevel@tonic-gate 	 * every second and consequently pageout may not be able to free
3380Sstevel@tonic-gate 	 * sufficient memory to maintain the minimum threshold.  Larger
3390Sstevel@tonic-gate 	 * values of fastscan result in scanning a lot more pages which
3400Sstevel@tonic-gate 	 * could lead to thrashing and higher CPU usage.
3410Sstevel@tonic-gate 	 *
3420Sstevel@tonic-gate 	 * Fastscan needs to be limited to a maximum value and should not
3430Sstevel@tonic-gate 	 * scale with memory to prevent pageout from consuming too much
3440Sstevel@tonic-gate 	 * time for scanning on slow CPU's and avoid thrashing, as a
3450Sstevel@tonic-gate 	 * result of scanning too many pages, on faster CPU's.
3460Sstevel@tonic-gate 	 * The value of 64 Meg was chosen for MAXHANDSPREADPAGES
3470Sstevel@tonic-gate 	 * (the upper bound for fastscan) based on the average number
3480Sstevel@tonic-gate 	 * of pages that can potentially be scanned in ~1 sec (using ~4%
3490Sstevel@tonic-gate 	 * of the CPU) on some of the following machines that currently
3500Sstevel@tonic-gate 	 * run Solaris 2.x:
3510Sstevel@tonic-gate 	 *
3520Sstevel@tonic-gate 	 *			average memory scanned in ~1 sec
3530Sstevel@tonic-gate 	 *
3540Sstevel@tonic-gate 	 *	25 Mhz SS1+:		23 Meg
3550Sstevel@tonic-gate 	 *	LX:			37 Meg
3560Sstevel@tonic-gate 	 *	50 Mhz SC2000:		68 Meg
3570Sstevel@tonic-gate 	 *
3580Sstevel@tonic-gate 	 *	40 Mhz 486:		26 Meg
3590Sstevel@tonic-gate 	 *	66 Mhz 486:		42 Meg
3600Sstevel@tonic-gate 	 *
3610Sstevel@tonic-gate 	 * When free memory falls just below lotsfree, the scan rate
3620Sstevel@tonic-gate 	 * goes from 0 to slowscan (i.e., pageout starts running).  This
3630Sstevel@tonic-gate 	 * transition needs to be smooth and is achieved by ensuring that
3640Sstevel@tonic-gate 	 * pageout scans a small number of pages to satisfy the transient
3650Sstevel@tonic-gate 	 * memory demand.  This is set to not exceed 100 pages/sec (25 per
3660Sstevel@tonic-gate 	 * wakeup) since scanning that many pages has no noticible impact
3670Sstevel@tonic-gate 	 * on system performance.
3680Sstevel@tonic-gate 	 *
3690Sstevel@tonic-gate 	 * In addition to setting fastscan and slowscan, pageout is
3700Sstevel@tonic-gate 	 * limited to using ~4% of the CPU.  This results in increasing
3710Sstevel@tonic-gate 	 * the time taken to scan all of memory, which in turn means that
3720Sstevel@tonic-gate 	 * user processes have a better opportunity of preventing their
3730Sstevel@tonic-gate 	 * pages from being stolen.  This has a positive effect on
3740Sstevel@tonic-gate 	 * interactive and overall system performance when memory demand
3750Sstevel@tonic-gate 	 * is high.
3760Sstevel@tonic-gate 	 *
3770Sstevel@tonic-gate 	 * Thus, the rate at which pages are scanned for replacement will
3780Sstevel@tonic-gate 	 * vary linearly between slowscan and the number of pages that
3790Sstevel@tonic-gate 	 * can be scanned using ~4% of processor time instead of varying
3800Sstevel@tonic-gate 	 * linearly between slowscan and fastscan.
3810Sstevel@tonic-gate 	 *
3820Sstevel@tonic-gate 	 * Also, the processor time used by pageout will vary from ~1%
3830Sstevel@tonic-gate 	 * at slowscan to ~4% at fastscan instead of varying between
3840Sstevel@tonic-gate 	 * ~1% at slowscan and ~10% at fastscan.
3850Sstevel@tonic-gate 	 *
3860Sstevel@tonic-gate 	 * The values chosen for the various VM parameters (fastscan,
3870Sstevel@tonic-gate 	 * handspreadpages, etc) are not universally true for all machines,
3880Sstevel@tonic-gate 	 * but appear to be a good rule of thumb for the machines we've
3890Sstevel@tonic-gate 	 * tested.  They have the following ranges:
3900Sstevel@tonic-gate 	 *
3910Sstevel@tonic-gate 	 *	cpu speed:	20 to 70 Mhz
3920Sstevel@tonic-gate 	 *	page size:	4K to 8K
3930Sstevel@tonic-gate 	 *	memory size:	16M to 5G
3940Sstevel@tonic-gate 	 *	page scan rate:	4000 - 17400 4K pages per sec
3950Sstevel@tonic-gate 	 *
3960Sstevel@tonic-gate 	 * The values need to be re-examined for machines which don't
3970Sstevel@tonic-gate 	 * fall into the various ranges (e.g., slower or faster CPUs,
3980Sstevel@tonic-gate 	 * smaller or larger pagesizes etc) shown above.
3990Sstevel@tonic-gate 	 *
4000Sstevel@tonic-gate 	 * On an MP machine, pageout is often unable to maintain the
4010Sstevel@tonic-gate 	 * minimum paging thresholds under heavy load.  This is due to
4020Sstevel@tonic-gate 	 * the fact that user processes running on other CPU's can be
4030Sstevel@tonic-gate 	 * dirtying memory at a much faster pace than pageout can find
4040Sstevel@tonic-gate 	 * pages to free.  The memory demands could be met by enabling
4050Sstevel@tonic-gate 	 * more than one CPU to run the clock algorithm in such a manner
4060Sstevel@tonic-gate 	 * that the various clock hands don't overlap.  This also makes
4070Sstevel@tonic-gate 	 * it more difficult to determine the values for fastscan, slowscan
4080Sstevel@tonic-gate 	 * and handspreadpages.
4090Sstevel@tonic-gate 	 *
4100Sstevel@tonic-gate 	 * The swapper is currently used to free up memory when pageout
4110Sstevel@tonic-gate 	 * is unable to meet memory demands by swapping out processes.
4120Sstevel@tonic-gate 	 * In addition to freeing up memory, swapping also reduces the
4130Sstevel@tonic-gate 	 * demand for memory by preventing user processes from running
4140Sstevel@tonic-gate 	 * and thereby consuming memory.
4150Sstevel@tonic-gate 	 */
4160Sstevel@tonic-gate 	if (init_mfscan == 0) {
4170Sstevel@tonic-gate 		if (pageout_new_spread != 0)
4180Sstevel@tonic-gate 			maxfastscan = pageout_new_spread;
4190Sstevel@tonic-gate 		else
4200Sstevel@tonic-gate 			maxfastscan = MAXHANDSPREADPAGES;
4210Sstevel@tonic-gate 	} else {
4220Sstevel@tonic-gate 		maxfastscan = init_mfscan;
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 	if (init_fscan == 0)
4250Sstevel@tonic-gate 		fastscan = MIN(looppages / loopfraction, maxfastscan);
4260Sstevel@tonic-gate 	else
4270Sstevel@tonic-gate 		fastscan = init_fscan;
4280Sstevel@tonic-gate 	if (fastscan > looppages / loopfraction)
4290Sstevel@tonic-gate 		fastscan = looppages / loopfraction;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/*
4320Sstevel@tonic-gate 	 * Set slow scan time to 1/10 the fast scan time, but
4330Sstevel@tonic-gate 	 * not to exceed maxslowscan.
4340Sstevel@tonic-gate 	 */
4350Sstevel@tonic-gate 	if (init_sscan == 0)
4360Sstevel@tonic-gate 		slowscan = MIN(fastscan / 10, maxslowscan);
4370Sstevel@tonic-gate 	else
4380Sstevel@tonic-gate 		slowscan = init_sscan;
4390Sstevel@tonic-gate 	if (slowscan > fastscan / 2)
4400Sstevel@tonic-gate 		slowscan = fastscan / 2;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	/*
4430Sstevel@tonic-gate 	 * Handspreadpages is distance (in pages) between front and back
4440Sstevel@tonic-gate 	 * pageout daemon hands.  The amount of time to reclaim a page
4450Sstevel@tonic-gate 	 * once pageout examines it increases with this distance and
4460Sstevel@tonic-gate 	 * decreases as the scan rate rises. It must be < the amount
4470Sstevel@tonic-gate 	 * of pageable memory.
4480Sstevel@tonic-gate 	 *
4490Sstevel@tonic-gate 	 * Since pageout is limited to ~4% of the CPU, setting handspreadpages
4500Sstevel@tonic-gate 	 * to be "fastscan" results in the front hand being a few secs
4510Sstevel@tonic-gate 	 * (varies based on the processor speed) ahead of the back hand
4520Sstevel@tonic-gate 	 * at fastscan rates.  This distance can be further reduced, if
4530Sstevel@tonic-gate 	 * necessary, by increasing the processor time used by pageout
4540Sstevel@tonic-gate 	 * to be more than ~4% and preferrably not more than ~10%.
4550Sstevel@tonic-gate 	 *
4560Sstevel@tonic-gate 	 * As a result, user processes have a much better chance of
4570Sstevel@tonic-gate 	 * referencing their pages before the back hand examines them.
4580Sstevel@tonic-gate 	 * This also significantly lowers the number of reclaims from
4590Sstevel@tonic-gate 	 * the freelist since pageout does not end up freeing pages which
4600Sstevel@tonic-gate 	 * may be referenced a sec later.
4610Sstevel@tonic-gate 	 */
4620Sstevel@tonic-gate 	if (init_hspages == 0)
4630Sstevel@tonic-gate 		handspreadpages = fastscan;
4640Sstevel@tonic-gate 	else
4650Sstevel@tonic-gate 		handspreadpages = init_hspages;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	/*
4680Sstevel@tonic-gate 	 * Make sure that back hand follows front hand by at least
4690Sstevel@tonic-gate 	 * 1/RATETOSCHEDPAGING seconds.  Without this test, it is possible
4700Sstevel@tonic-gate 	 * for the back hand to look at a page during the same wakeup of
4710Sstevel@tonic-gate 	 * the pageout daemon in which the front hand cleared its ref bit.
4720Sstevel@tonic-gate 	 */
4730Sstevel@tonic-gate 	if (handspreadpages >= looppages)
4740Sstevel@tonic-gate 		handspreadpages = looppages - 1;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	/*
4770Sstevel@tonic-gate 	 * If we have been called to recalculate the parameters,
4780Sstevel@tonic-gate 	 * set a flag to re-evaluate the clock hand pointers.
4790Sstevel@tonic-gate 	 */
4800Sstevel@tonic-gate 	if (recalc)
4810Sstevel@tonic-gate 		reset_hands = 1;
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate  * Pageout scheduling.
4860Sstevel@tonic-gate  *
4870Sstevel@tonic-gate  * Schedpaging controls the rate at which the page out daemon runs by
4880Sstevel@tonic-gate  * setting the global variables nscan and desscan RATETOSCHEDPAGING
4890Sstevel@tonic-gate  * times a second.  Nscan records the number of pages pageout has examined
4900Sstevel@tonic-gate  * in its current pass; schedpaging resets this value to zero each time
4910Sstevel@tonic-gate  * it runs.  Desscan records the number of pages pageout should examine
4920Sstevel@tonic-gate  * in its next pass; schedpaging sets this value based on the amount of
4930Sstevel@tonic-gate  * currently available memory.
4940Sstevel@tonic-gate  */
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate #define	RATETOSCHEDPAGING	4		/* hz that is */
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate static kmutex_t	pageout_mutex;	/* held while pageout or schedpaging running */
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate  * Pool of available async pageout putpage requests.
5020Sstevel@tonic-gate  */
5030Sstevel@tonic-gate static struct async_reqs *push_req;
5040Sstevel@tonic-gate static struct async_reqs *req_freelist;	/* available req structs */
5050Sstevel@tonic-gate static struct async_reqs *push_list;	/* pending reqs */
5060Sstevel@tonic-gate static kmutex_t push_lock;		/* protects req pool */
5070Sstevel@tonic-gate static kcondvar_t push_cv;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate static int async_list_size = 256;	/* number of async request structs */
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate static void pageout_scanner(void);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate /*
5140Sstevel@tonic-gate  * If a page is being shared more than "po_share" times
5150Sstevel@tonic-gate  * then leave it alone- don't page it out.
5160Sstevel@tonic-gate  */
5170Sstevel@tonic-gate #define	MIN_PO_SHARE	(8)
5180Sstevel@tonic-gate #define	MAX_PO_SHARE	((MIN_PO_SHARE) << 24)
5190Sstevel@tonic-gate ulong_t	po_share = MIN_PO_SHARE;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate /*
5220Sstevel@tonic-gate  * Schedule rate for paging.
5230Sstevel@tonic-gate  * Rate is linear interpolation between
5240Sstevel@tonic-gate  * slowscan with lotsfree and fastscan when out of memory.
5250Sstevel@tonic-gate  */
5260Sstevel@tonic-gate static void
5270Sstevel@tonic-gate schedpaging(void *arg)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	spgcnt_t vavail;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if (freemem < lotsfree + needfree + kmem_reapahead)
5320Sstevel@tonic-gate 		kmem_reap();
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (freemem < lotsfree + needfree + seg_preapahead)
5350Sstevel@tonic-gate 		seg_preap();
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	if (kcage_on && (kcage_freemem < kcage_desfree || kcage_needfree))
5380Sstevel@tonic-gate 		kcage_cageout_wakeup();
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	if (mutex_tryenter(&pageout_mutex)) {
5410Sstevel@tonic-gate 		/* pageout() not running */
5420Sstevel@tonic-gate 		nscan = 0;
5430Sstevel@tonic-gate 		vavail = freemem - deficit;
544*6118Sjimp 		if (pageout_new_spread != 0)
545*6118Sjimp 			vavail -= needfree;
5460Sstevel@tonic-gate 		if (vavail < 0)
5470Sstevel@tonic-gate 			vavail = 0;
5480Sstevel@tonic-gate 		if (vavail > lotsfree)
5490Sstevel@tonic-gate 			vavail = lotsfree;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 		/*
5520Sstevel@tonic-gate 		 * Fix for 1161438 (CRS SPR# 73922).  All variables
5530Sstevel@tonic-gate 		 * in the original calculation for desscan were 32 bit signed
5540Sstevel@tonic-gate 		 * ints.  As freemem approaches 0x0 on a system with 1 Gig or
5550Sstevel@tonic-gate 		 * more of memory, the calculation can overflow.  When this
5560Sstevel@tonic-gate 		 * happens, desscan becomes negative and pageout_scanner()
5570Sstevel@tonic-gate 		 * stops paging out.
5580Sstevel@tonic-gate 		 */
559*6118Sjimp 		if ((needfree) && (pageout_new_spread == 0)) {
560*6118Sjimp 			/*
561*6118Sjimp 			 * If we've not yet collected enough samples to
562*6118Sjimp 			 * calculate a spread, use the old logic of kicking
563*6118Sjimp 			 * into high gear anytime needfree is non-zero.
564*6118Sjimp 			 */
5650Sstevel@tonic-gate 			desscan = fastscan / RATETOSCHEDPAGING;
5660Sstevel@tonic-gate 		} else {
567*6118Sjimp 			/*
568*6118Sjimp 			 * Once we've calculated a spread based on system
569*6118Sjimp 			 * memory and usage, just treat needfree as another
570*6118Sjimp 			 * form of deficit.
571*6118Sjimp 			 */
5720Sstevel@tonic-gate 			spgcnt_t faststmp, slowstmp, result;
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 			slowstmp = slowscan * vavail;
5750Sstevel@tonic-gate 			faststmp = fastscan * (lotsfree - vavail);
5760Sstevel@tonic-gate 			result = (slowstmp + faststmp) /
577*6118Sjimp 			    nz(lotsfree) / RATETOSCHEDPAGING;
5780Sstevel@tonic-gate 			desscan = (pgcnt_t)result;
5790Sstevel@tonic-gate 		}
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 		pageout_ticks = min_pageout_ticks + (lotsfree - vavail) *
5820Sstevel@tonic-gate 		    (max_pageout_ticks - min_pageout_ticks) / nz(lotsfree);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 		if (freemem < lotsfree + needfree ||
5850Sstevel@tonic-gate 		    pageout_sample_cnt < pageout_sample_lim) {
5860Sstevel@tonic-gate 			TRACE_1(TR_FAC_VM, TR_PAGEOUT_CV_SIGNAL,
587*6118Sjimp 			    "pageout_cv_signal:freemem %ld", freemem);
5880Sstevel@tonic-gate 			cv_signal(&proc_pageout->p_cv);
5890Sstevel@tonic-gate 		} else {
5900Sstevel@tonic-gate 			/*
5910Sstevel@tonic-gate 			 * There are enough free pages, no need to
5920Sstevel@tonic-gate 			 * kick the scanner thread.  And next time
5930Sstevel@tonic-gate 			 * around, keep more of the `highly shared'
5940Sstevel@tonic-gate 			 * pages.
5950Sstevel@tonic-gate 			 */
5960Sstevel@tonic-gate 			cv_signal_pageout();
5970Sstevel@tonic-gate 			if (po_share > MIN_PO_SHARE) {
5980Sstevel@tonic-gate 				po_share >>= 1;
5990Sstevel@tonic-gate 			}
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 		mutex_exit(&pageout_mutex);
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	/*
6050Sstevel@tonic-gate 	 * Signal threads waiting for available memory.
6060Sstevel@tonic-gate 	 * NOTE: usually we need to grab memavail_lock before cv_broadcast, but
6070Sstevel@tonic-gate 	 * in this case it is not needed - the waiters will be waken up during
6080Sstevel@tonic-gate 	 * the next invocation of this function.
6090Sstevel@tonic-gate 	 */
6100Sstevel@tonic-gate 	if (kmem_avail() > 0)
6110Sstevel@tonic-gate 		cv_broadcast(&memavail_cv);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	(void) timeout(schedpaging, arg, hz / RATETOSCHEDPAGING);
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate pgcnt_t		pushes;
6170Sstevel@tonic-gate ulong_t		push_list_size;		/* # of requests on pageout queue */
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate #define	FRONT	1
6200Sstevel@tonic-gate #define	BACK	2
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate int dopageout = 1;	/* must be non-zero to turn page stealing on */
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate /*
6250Sstevel@tonic-gate  * The page out daemon, which runs as process 2.
6260Sstevel@tonic-gate  *
6270Sstevel@tonic-gate  * As long as there are at least lotsfree pages,
6280Sstevel@tonic-gate  * this process is not run.  When the number of free
6290Sstevel@tonic-gate  * pages stays in the range desfree to lotsfree,
6300Sstevel@tonic-gate  * this daemon runs through the pages in the loop
6310Sstevel@tonic-gate  * at a rate determined in schedpaging().  Pageout manages
6320Sstevel@tonic-gate  * two hands on the clock.  The front hand moves through
6330Sstevel@tonic-gate  * memory, clearing the reference bit,
6340Sstevel@tonic-gate  * and stealing pages from procs that are over maxrss.
6350Sstevel@tonic-gate  * The back hand travels a distance behind the front hand,
6360Sstevel@tonic-gate  * freeing the pages that have not been referenced in the time
6370Sstevel@tonic-gate  * since the front hand passed.  If modified, they are pushed to
6380Sstevel@tonic-gate  * swap before being freed.
6390Sstevel@tonic-gate  *
6400Sstevel@tonic-gate  * There are 2 threads that act on behalf of the pageout process.
6410Sstevel@tonic-gate  * One thread scans pages (pageout_scanner) and frees them up if
6420Sstevel@tonic-gate  * they don't require any VOP_PUTPAGE operation. If a page must be
6430Sstevel@tonic-gate  * written back to its backing store, the request is put on a list
6440Sstevel@tonic-gate  * and the other (pageout) thread is signaled. The pageout thread
6450Sstevel@tonic-gate  * grabs VOP_PUTPAGE requests from the list, and processes them.
6460Sstevel@tonic-gate  * Some filesystems may require resources for the VOP_PUTPAGE
6470Sstevel@tonic-gate  * operations (like memory) and hence can block the pageout
6480Sstevel@tonic-gate  * thread, but the scanner thread can still operate. There is still
6495331Samw  * no guarantee that memory deadlocks cannot occur.
6500Sstevel@tonic-gate  *
6510Sstevel@tonic-gate  * For now, this thing is in very rough form.
6520Sstevel@tonic-gate  */
6530Sstevel@tonic-gate void
6540Sstevel@tonic-gate pageout()
6550Sstevel@tonic-gate {
6560Sstevel@tonic-gate 	struct async_reqs *arg;
6570Sstevel@tonic-gate 	pri_t pageout_pri;
6580Sstevel@tonic-gate 	int i;
6590Sstevel@tonic-gate 	pgcnt_t max_pushes;
6600Sstevel@tonic-gate 	callb_cpr_t cprinfo;
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	proc_pageout = ttoproc(curthread);
6630Sstevel@tonic-gate 	proc_pageout->p_cstime = 0;
6640Sstevel@tonic-gate 	proc_pageout->p_stime =  0;
6650Sstevel@tonic-gate 	proc_pageout->p_cutime =  0;
6660Sstevel@tonic-gate 	proc_pageout->p_utime = 0;
6673446Smrj 	bcopy("pageout", PTOU(curproc)->u_psargs, 8);
6683446Smrj 	bcopy("pageout", PTOU(curproc)->u_comm, 7);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	/*
6710Sstevel@tonic-gate 	 * Create pageout scanner thread
6720Sstevel@tonic-gate 	 */
6730Sstevel@tonic-gate 	mutex_init(&pageout_mutex, NULL, MUTEX_DEFAULT, NULL);
6740Sstevel@tonic-gate 	mutex_init(&push_lock, NULL, MUTEX_DEFAULT, NULL);
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	/*
6770Sstevel@tonic-gate 	 * Allocate and initialize the async request structures
6780Sstevel@tonic-gate 	 * for pageout.
6790Sstevel@tonic-gate 	 */
6800Sstevel@tonic-gate 	push_req = (struct async_reqs *)
6810Sstevel@tonic-gate 	    kmem_zalloc(async_list_size * sizeof (struct async_reqs), KM_SLEEP);
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	req_freelist = push_req;
6840Sstevel@tonic-gate 	for (i = 0; i < async_list_size - 1; i++)
6850Sstevel@tonic-gate 		push_req[i].a_next = &push_req[i + 1];
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	pageout_pri = curthread->t_pri;
6880Sstevel@tonic-gate 	pageout_init(pageout_scanner, proc_pageout, pageout_pri - 1);
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	/*
6910Sstevel@tonic-gate 	 * kick off pageout scheduler.
6920Sstevel@tonic-gate 	 */
6930Sstevel@tonic-gate 	schedpaging(NULL);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/*
6960Sstevel@tonic-gate 	 * Create kernel cage thread.
6970Sstevel@tonic-gate 	 * The kernel cage thread is started under the pageout process
6980Sstevel@tonic-gate 	 * to take advantage of the less restricted page allocation
6990Sstevel@tonic-gate 	 * in page_create_throttle().
7000Sstevel@tonic-gate 	 */
7010Sstevel@tonic-gate 	kcage_cageout_init();
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	/*
7040Sstevel@tonic-gate 	 * Limit pushes to avoid saturating pageout devices.
7050Sstevel@tonic-gate 	 */
7060Sstevel@tonic-gate 	max_pushes = maxpgio / RATETOSCHEDPAGING;
7070Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &push_lock, callb_generic_cpr, "pageout");
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	for (;;) {
7100Sstevel@tonic-gate 		mutex_enter(&push_lock);
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 		while ((arg = push_list) == NULL || pushes > max_pushes) {
7130Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
7140Sstevel@tonic-gate 			cv_wait(&push_cv, &push_lock);
7150Sstevel@tonic-gate 			pushes = 0;
7160Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &push_lock);
7170Sstevel@tonic-gate 		}
7180Sstevel@tonic-gate 		push_list = arg->a_next;
7190Sstevel@tonic-gate 		arg->a_next = NULL;
7200Sstevel@tonic-gate 		mutex_exit(&push_lock);
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 		if (VOP_PUTPAGE(arg->a_vp, (offset_t)arg->a_off,
723*6118Sjimp 		    arg->a_len, arg->a_flags, arg->a_cred, NULL) == 0) {
7240Sstevel@tonic-gate 			pushes++;
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 		/* vp held by checkpage() */
7280Sstevel@tonic-gate 		VN_RELE(arg->a_vp);
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 		mutex_enter(&push_lock);
7310Sstevel@tonic-gate 		arg->a_next = req_freelist;	/* back on freelist */
7320Sstevel@tonic-gate 		req_freelist = arg;
7330Sstevel@tonic-gate 		push_list_size--;
7340Sstevel@tonic-gate 		mutex_exit(&push_lock);
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate /*
7390Sstevel@tonic-gate  * Kernel thread that scans pages looking for ones to free
7400Sstevel@tonic-gate  */
7410Sstevel@tonic-gate static void
7420Sstevel@tonic-gate pageout_scanner(void)
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate 	struct page *fronthand, *backhand;
7450Sstevel@tonic-gate 	uint_t count;
7460Sstevel@tonic-gate 	callb_cpr_t cprinfo;
7470Sstevel@tonic-gate 	pgcnt_t	nscan_limit;
7480Sstevel@tonic-gate 	pgcnt_t	pcount;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &pageout_mutex, callb_generic_cpr, "poscan");
7510Sstevel@tonic-gate 	mutex_enter(&pageout_mutex);
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	/*
7540Sstevel@tonic-gate 	 * The restart case does not attempt to point the hands at roughly
7550Sstevel@tonic-gate 	 * the right point on the assumption that after one circuit things
7560Sstevel@tonic-gate 	 * will have settled down - and restarts shouldn't be that often.
7570Sstevel@tonic-gate 	 */
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	/*
7600Sstevel@tonic-gate 	 * Set the two clock hands to be separated by a reasonable amount,
7610Sstevel@tonic-gate 	 * but no more than 360 degrees apart.
7620Sstevel@tonic-gate 	 */
7630Sstevel@tonic-gate 	backhand = page_first();
7640Sstevel@tonic-gate 	if (handspreadpages >= total_pages)
7650Sstevel@tonic-gate 		fronthand = page_nextn(backhand, total_pages - 1);
7660Sstevel@tonic-gate 	else
7670Sstevel@tonic-gate 		fronthand = page_nextn(backhand, handspreadpages);
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	min_pageout_ticks = MAX(1,
7700Sstevel@tonic-gate 	    ((hz * min_percent_cpu) / 100) / RATETOSCHEDPAGING);
7710Sstevel@tonic-gate 	max_pageout_ticks = MAX(min_pageout_ticks,
7720Sstevel@tonic-gate 	    ((hz * max_percent_cpu) / 100) / RATETOSCHEDPAGING);
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate loop:
7750Sstevel@tonic-gate 	cv_signal_pageout();
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	CALLB_CPR_SAFE_BEGIN(&cprinfo);
7780Sstevel@tonic-gate 	cv_wait(&proc_pageout->p_cv, &pageout_mutex);
7790Sstevel@tonic-gate 	CALLB_CPR_SAFE_END(&cprinfo, &pageout_mutex);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	if (!dopageout)
7820Sstevel@tonic-gate 		goto loop;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	if (reset_hands) {
7850Sstevel@tonic-gate 		reset_hands = 0;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		backhand = page_first();
7880Sstevel@tonic-gate 		if (handspreadpages >= total_pages)
7890Sstevel@tonic-gate 			fronthand = page_nextn(backhand, total_pages - 1);
7900Sstevel@tonic-gate 		else
7910Sstevel@tonic-gate 			fronthand = page_nextn(backhand, handspreadpages);
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, vm, pgrrun, 1);
7950Sstevel@tonic-gate 	count = 0;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	TRACE_4(TR_FAC_VM, TR_PAGEOUT_START,
798*6118Sjimp 	    "pageout_start:freemem %ld lotsfree %ld nscan %ld desscan %ld",
799*6118Sjimp 	    freemem, lotsfree, nscan, desscan);
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	/* Kernel probe */
8020Sstevel@tonic-gate 	TNF_PROBE_2(pageout_scan_start, "vm pagedaemon", /* CSTYLED */,
803*6118Sjimp 	    tnf_ulong, pages_free, freemem, tnf_ulong, pages_needed, needfree);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	pcount = 0;
8060Sstevel@tonic-gate 	if (pageout_sample_cnt < pageout_sample_lim) {
8070Sstevel@tonic-gate 		nscan_limit = total_pages;
8080Sstevel@tonic-gate 	} else {
8090Sstevel@tonic-gate 		nscan_limit = desscan;
8100Sstevel@tonic-gate 	}
8110Sstevel@tonic-gate 	pageout_lbolt = lbolt;
8120Sstevel@tonic-gate 	sample_start = gethrtime();
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	/*
8150Sstevel@tonic-gate 	 * Scan the appropriate number of pages for a single duty cycle.
8160Sstevel@tonic-gate 	 * However, stop scanning as soon as there is enough free memory.
8170Sstevel@tonic-gate 	 * For a short while, we will be sampling the performance of the
8180Sstevel@tonic-gate 	 * scanner and need to keep running just to get sample data, in
8190Sstevel@tonic-gate 	 * which case we keep going and don't pay attention to whether
8200Sstevel@tonic-gate 	 * or not there is enough free memory.
8210Sstevel@tonic-gate 	 */
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	while (nscan < nscan_limit && (freemem < lotsfree + needfree ||
8240Sstevel@tonic-gate 	    pageout_sample_cnt < pageout_sample_lim)) {
8250Sstevel@tonic-gate 		int rvfront, rvback;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 		/*
8280Sstevel@tonic-gate 		 * Check to see if we have exceeded our %CPU budget
8290Sstevel@tonic-gate 		 * for this wakeup, but not on every single page visited,
8300Sstevel@tonic-gate 		 * just every once in a while.
8310Sstevel@tonic-gate 		 */
8320Sstevel@tonic-gate 		if ((pcount & PAGES_POLL_MASK) == PAGES_POLL_MASK) {
8330Sstevel@tonic-gate 			pageout_cycle_ticks = lbolt - pageout_lbolt;
8340Sstevel@tonic-gate 			if (pageout_cycle_ticks >= pageout_ticks) {
8350Sstevel@tonic-gate 				++pageout_timeouts;
8360Sstevel@tonic-gate 				break;
8370Sstevel@tonic-gate 			}
8380Sstevel@tonic-gate 		}
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 		/*
8410Sstevel@tonic-gate 		 * If checkpage manages to add a page to the free list,
8420Sstevel@tonic-gate 		 * we give ourselves another couple of trips around the loop.
8430Sstevel@tonic-gate 		 */
8440Sstevel@tonic-gate 		if ((rvfront = checkpage(fronthand, FRONT)) == 1)
8450Sstevel@tonic-gate 			count = 0;
8460Sstevel@tonic-gate 		if ((rvback = checkpage(backhand, BACK)) == 1)
8470Sstevel@tonic-gate 			count = 0;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 		++pcount;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 		/*
8520Sstevel@tonic-gate 		 * protected by pageout_mutex instead of cpu_stat_lock
8530Sstevel@tonic-gate 		 */
8540Sstevel@tonic-gate 		CPU_STATS_ADDQ(CPU, vm, scan, 1);
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 		/*
8570Sstevel@tonic-gate 		 * Don't include ineligible pages in the number scanned.
8580Sstevel@tonic-gate 		 */
8590Sstevel@tonic-gate 		if (rvfront != -1 || rvback != -1)
8600Sstevel@tonic-gate 			nscan++;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 		backhand = page_next(backhand);
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 		/*
8650Sstevel@tonic-gate 		 * backhand update and wraparound check are done separately
8660Sstevel@tonic-gate 		 * because lint barks when it finds an empty "if" body
8670Sstevel@tonic-gate 		 */
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 		if ((fronthand = page_next(fronthand)) == page_first())	{
8700Sstevel@tonic-gate 			TRACE_2(TR_FAC_VM, TR_PAGEOUT_HAND_WRAP,
871*6118Sjimp 			    "pageout_hand_wrap:freemem %ld whichhand %d",
872*6118Sjimp 			    freemem, FRONT);
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 			/*
8750Sstevel@tonic-gate 			 * protected by pageout_mutex instead of cpu_stat_lock
8760Sstevel@tonic-gate 			 */
8770Sstevel@tonic-gate 			CPU_STATS_ADDQ(CPU, vm, rev, 1);
8780Sstevel@tonic-gate 			if (++count > 1) {
8790Sstevel@tonic-gate 				/*
8800Sstevel@tonic-gate 				 * Extremely unlikely, but it happens.
8810Sstevel@tonic-gate 				 * We went around the loop at least once
8820Sstevel@tonic-gate 				 * and didn't get far enough.
8830Sstevel@tonic-gate 				 * If we are still skipping `highly shared'
8840Sstevel@tonic-gate 				 * pages, skip fewer of them.  Otherwise,
8850Sstevel@tonic-gate 				 * give up till the next clock tick.
8860Sstevel@tonic-gate 				 */
8870Sstevel@tonic-gate 				if (po_share < MAX_PO_SHARE) {
8880Sstevel@tonic-gate 					po_share <<= 1;
8890Sstevel@tonic-gate 				} else {
8900Sstevel@tonic-gate 					/*
8910Sstevel@tonic-gate 					 * Really a "goto loop", but
8920Sstevel@tonic-gate 					 * if someone is TRACing or
8930Sstevel@tonic-gate 					 * TNF_PROBE_ing, at least
8940Sstevel@tonic-gate 					 * make records to show
8950Sstevel@tonic-gate 					 * where we are.
8960Sstevel@tonic-gate 					 */
8970Sstevel@tonic-gate 					break;
8980Sstevel@tonic-gate 				}
8990Sstevel@tonic-gate 			}
9000Sstevel@tonic-gate 		}
9010Sstevel@tonic-gate 	}
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	sample_end = gethrtime();
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	TRACE_5(TR_FAC_VM, TR_PAGEOUT_END,
906*6118Sjimp 	    "pageout_end:freemem %ld lots %ld nscan %ld des %ld count %u",
907*6118Sjimp 	    freemem, lotsfree, nscan, desscan, count);
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	/* Kernel probe */
9100Sstevel@tonic-gate 	TNF_PROBE_2(pageout_scan_end, "vm pagedaemon", /* CSTYLED */,
911*6118Sjimp 	    tnf_ulong, pages_scanned, nscan, tnf_ulong, pages_free, freemem);
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	if (pageout_sample_cnt < pageout_sample_lim) {
9140Sstevel@tonic-gate 		pageout_sample_pages += pcount;
9150Sstevel@tonic-gate 		pageout_sample_etime += sample_end - sample_start;
9160Sstevel@tonic-gate 		++pageout_sample_cnt;
9170Sstevel@tonic-gate 	}
9180Sstevel@tonic-gate 	if (pageout_sample_cnt >= pageout_sample_lim &&
9190Sstevel@tonic-gate 	    pageout_new_spread == 0) {
9200Sstevel@tonic-gate 		pageout_rate = (hrrate_t)pageout_sample_pages *
9210Sstevel@tonic-gate 		    (hrrate_t)(NANOSEC) / pageout_sample_etime;
9220Sstevel@tonic-gate 		pageout_new_spread = pageout_rate / 10;
9230Sstevel@tonic-gate 		setupclock(1);
9240Sstevel@tonic-gate 	}
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	goto loop;
9270Sstevel@tonic-gate }
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate /*
9300Sstevel@tonic-gate  * Look at the page at hand.  If it is locked (e.g., for physical i/o),
9310Sstevel@tonic-gate  * system (u., page table) or free, then leave it alone.  Otherwise,
9320Sstevel@tonic-gate  * if we are running the front hand, turn off the page's reference bit.
9330Sstevel@tonic-gate  * If the proc is over maxrss, we take it.  If running the back hand,
9340Sstevel@tonic-gate  * check whether the page has been reclaimed.  If not, free the page,
9350Sstevel@tonic-gate  * pushing it to disk first if necessary.
9360Sstevel@tonic-gate  *
9370Sstevel@tonic-gate  * Return values:
9380Sstevel@tonic-gate  *	-1 if the page is not a candidate at all,
9390Sstevel@tonic-gate  *	 0 if not freed, or
9400Sstevel@tonic-gate  *	 1 if we freed it.
9410Sstevel@tonic-gate  */
9420Sstevel@tonic-gate static int
9430Sstevel@tonic-gate checkpage(struct page *pp, int whichhand)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate 	int ppattr;
9460Sstevel@tonic-gate 	int isfs = 0;
9470Sstevel@tonic-gate 	int isexec = 0;
9480Sstevel@tonic-gate 	int pagesync_flag;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	/*
9510Sstevel@tonic-gate 	 * Skip pages:
9520Sstevel@tonic-gate 	 * 	- associated with the kernel vnode since
9530Sstevel@tonic-gate 	 *	    they are always "exclusively" locked.
9540Sstevel@tonic-gate 	 *	- that are free
9550Sstevel@tonic-gate 	 *	- that are shared more than po_share'd times
9560Sstevel@tonic-gate 	 *	- its already locked
9570Sstevel@tonic-gate 	 *
9580Sstevel@tonic-gate 	 * NOTE:  These optimizations assume that reads are atomic.
9590Sstevel@tonic-gate 	 */
9600Sstevel@tonic-gate top:
9613290Sjohansen 	if ((PP_ISKAS(pp)) || (PP_ISFREE(pp)) ||
9624528Spaulsan 	    hat_page_checkshare(pp, po_share) || PAGE_LOCKED(pp)) {
9630Sstevel@tonic-gate 		return (-1);
9640Sstevel@tonic-gate 	}
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	if (!page_trylock(pp, SE_EXCL)) {
9670Sstevel@tonic-gate 		/*
9680Sstevel@tonic-gate 		 * Skip the page if we can't acquire the "exclusive" lock.
9690Sstevel@tonic-gate 		 */
9700Sstevel@tonic-gate 		return (-1);
9710Sstevel@tonic-gate 	} else if (PP_ISFREE(pp)) {
9720Sstevel@tonic-gate 		/*
9730Sstevel@tonic-gate 		 * It became free between the above check and our actually
9740Sstevel@tonic-gate 		 * locking the page.  Oh, well there will be other pages.
9750Sstevel@tonic-gate 		 */
9760Sstevel@tonic-gate 		page_unlock(pp);
9770Sstevel@tonic-gate 		return (-1);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	/*
9810Sstevel@tonic-gate 	 * Reject pages that cannot be freed. The page_struct_lock
9820Sstevel@tonic-gate 	 * need not be acquired to examine these
9830Sstevel@tonic-gate 	 * fields since the page has an "exclusive" lock.
9840Sstevel@tonic-gate 	 */
9850Sstevel@tonic-gate 	if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
9860Sstevel@tonic-gate 		page_unlock(pp);
9870Sstevel@tonic-gate 		return (-1);
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	/*
9910Sstevel@tonic-gate 	 * Maintain statistics for what we are freeing
9920Sstevel@tonic-gate 	 */
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	if (pp->p_vnode != NULL) {
9950Sstevel@tonic-gate 		if (pp->p_vnode->v_flag & VVMEXEC)
9960Sstevel@tonic-gate 			isexec = 1;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 		if (!IS_SWAPFSVP(pp->p_vnode))
9990Sstevel@tonic-gate 			isfs = 1;
10000Sstevel@tonic-gate 	}
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	/*
10030Sstevel@tonic-gate 	 * Turn off REF and MOD bits with the front hand.
10040Sstevel@tonic-gate 	 * The back hand examines the REF bit and always considers
10050Sstevel@tonic-gate 	 * SHARED pages as referenced.
10060Sstevel@tonic-gate 	 */
10070Sstevel@tonic-gate 	if (whichhand == FRONT)
10080Sstevel@tonic-gate 		pagesync_flag = HAT_SYNC_ZERORM;
10090Sstevel@tonic-gate 	else
10100Sstevel@tonic-gate 		pagesync_flag = HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_REF |
10110Sstevel@tonic-gate 		    HAT_SYNC_STOPON_SHARED;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	ppattr = hat_pagesync(pp, pagesync_flag);
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate recheck:
10160Sstevel@tonic-gate 	/*
10170Sstevel@tonic-gate 	 * If page is referenced; make unreferenced but reclaimable.
10180Sstevel@tonic-gate 	 * If this page is not referenced, then it must be reclaimable
10190Sstevel@tonic-gate 	 * and we can add it to the free list.
10200Sstevel@tonic-gate 	 */
10210Sstevel@tonic-gate 	if (ppattr & P_REF) {
10220Sstevel@tonic-gate 		TRACE_2(TR_FAC_VM, TR_PAGEOUT_ISREF,
10230Sstevel@tonic-gate 		    "pageout_isref:pp %p whichhand %d", pp, whichhand);
10240Sstevel@tonic-gate 		if (whichhand == FRONT) {
10250Sstevel@tonic-gate 			/*
10260Sstevel@tonic-gate 			 * Checking of rss or madvise flags needed here...
10270Sstevel@tonic-gate 			 *
10280Sstevel@tonic-gate 			 * If not "well-behaved", fall through into the code
10290Sstevel@tonic-gate 			 * for not referenced.
10300Sstevel@tonic-gate 			 */
10310Sstevel@tonic-gate 			hat_clrref(pp);
10320Sstevel@tonic-gate 		}
10330Sstevel@tonic-gate 		/*
10340Sstevel@tonic-gate 		 * Somebody referenced the page since the front
10350Sstevel@tonic-gate 		 * hand went by, so it's not a candidate for
10360Sstevel@tonic-gate 		 * freeing up.
10370Sstevel@tonic-gate 		 */
10380Sstevel@tonic-gate 		page_unlock(pp);
10390Sstevel@tonic-gate 		return (0);
10400Sstevel@tonic-gate 	}
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	VM_STAT_ADD(pageoutvmstats.checkpage[0]);
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 	/*
10450Sstevel@tonic-gate 	 * If large page, attempt to demote it. If successfully demoted,
10460Sstevel@tonic-gate 	 * retry the checkpage.
10470Sstevel@tonic-gate 	 */
10480Sstevel@tonic-gate 	if (pp->p_szc != 0) {
10490Sstevel@tonic-gate 		if (!page_try_demote_pages(pp)) {
10500Sstevel@tonic-gate 			VM_STAT_ADD(pageoutvmstats.checkpage[1]);
10510Sstevel@tonic-gate 			page_unlock(pp);
10520Sstevel@tonic-gate 			return (-1);
10530Sstevel@tonic-gate 		}
10540Sstevel@tonic-gate 		ASSERT(pp->p_szc == 0);
10550Sstevel@tonic-gate 		VM_STAT_ADD(pageoutvmstats.checkpage[2]);
10560Sstevel@tonic-gate 		/*
10570Sstevel@tonic-gate 		 * since page_try_demote_pages() could have unloaded some
10580Sstevel@tonic-gate 		 * mappings it makes sense to reload ppattr.
10590Sstevel@tonic-gate 		 */
10600Sstevel@tonic-gate 		ppattr = hat_page_getattr(pp, P_MOD | P_REF);
10610Sstevel@tonic-gate 	}
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	/*
10640Sstevel@tonic-gate 	 * If the page is currently dirty, we have to arrange
10650Sstevel@tonic-gate 	 * to have it cleaned before it can be freed.
10660Sstevel@tonic-gate 	 *
10670Sstevel@tonic-gate 	 * XXX - ASSERT(pp->p_vnode != NULL);
10680Sstevel@tonic-gate 	 */
10690Sstevel@tonic-gate 	if ((ppattr & P_MOD) && pp->p_vnode) {
10700Sstevel@tonic-gate 		struct vnode *vp = pp->p_vnode;
10710Sstevel@tonic-gate 		u_offset_t offset = pp->p_offset;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 		/*
10740Sstevel@tonic-gate 		 * XXX - Test for process being swapped out or about to exit?
10750Sstevel@tonic-gate 		 * [Can't get back to process(es) using the page.]
10760Sstevel@tonic-gate 		 */
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 		/*
10790Sstevel@tonic-gate 		 * Hold the vnode before releasing the page lock to
10800Sstevel@tonic-gate 		 * prevent it from being freed and re-used by some
10810Sstevel@tonic-gate 		 * other thread.
10820Sstevel@tonic-gate 		 */
10830Sstevel@tonic-gate 		VN_HOLD(vp);
10840Sstevel@tonic-gate 		page_unlock(pp);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 		/*
10870Sstevel@tonic-gate 		 * Queue i/o request for the pageout thread.
10880Sstevel@tonic-gate 		 */
10890Sstevel@tonic-gate 		if (!queue_io_request(vp, offset)) {
10900Sstevel@tonic-gate 			VN_RELE(vp);
10910Sstevel@tonic-gate 			return (0);
10920Sstevel@tonic-gate 		}
10930Sstevel@tonic-gate 		return (1);
10940Sstevel@tonic-gate 	}
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	/*
10970Sstevel@tonic-gate 	 * Now we unload all the translations,
10980Sstevel@tonic-gate 	 * and put the page back on to the free list.
10990Sstevel@tonic-gate 	 * If the page was used (referenced or modified) after
11000Sstevel@tonic-gate 	 * the pagesync but before it was unloaded we catch it
11010Sstevel@tonic-gate 	 * and handle the page properly.
11020Sstevel@tonic-gate 	 */
11030Sstevel@tonic-gate 	TRACE_2(TR_FAC_VM, TR_PAGEOUT_FREE,
1104*6118Sjimp 	    "pageout_free:pp %p whichhand %d", pp, whichhand);
11050Sstevel@tonic-gate 	(void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
11060Sstevel@tonic-gate 	ppattr = hat_page_getattr(pp, P_MOD | P_REF);
11070Sstevel@tonic-gate 	if ((ppattr & P_REF) || ((ppattr & P_MOD) && pp->p_vnode))
11080Sstevel@tonic-gate 		goto recheck;
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	/*LINTED: constant in conditional context*/
11110Sstevel@tonic-gate 	VN_DISPOSE(pp, B_FREE, 0, kcred);
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	CPU_STATS_ADD_K(vm, dfree, 1);
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	if (isfs) {
11160Sstevel@tonic-gate 		if (isexec) {
11170Sstevel@tonic-gate 			CPU_STATS_ADD_K(vm, execfree, 1);
11180Sstevel@tonic-gate 		} else {
11190Sstevel@tonic-gate 			CPU_STATS_ADD_K(vm, fsfree, 1);
11200Sstevel@tonic-gate 		}
11210Sstevel@tonic-gate 	} else {
11220Sstevel@tonic-gate 		CPU_STATS_ADD_K(vm, anonfree, 1);
11230Sstevel@tonic-gate 	}
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	return (1);		/* freed a page! */
11260Sstevel@tonic-gate }
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate /*
11290Sstevel@tonic-gate  * Queue async i/o request from pageout_scanner and segment swapout
11300Sstevel@tonic-gate  * routines on one common list.  This ensures that pageout devices (swap)
11310Sstevel@tonic-gate  * are not saturated by pageout_scanner or swapout requests.
11320Sstevel@tonic-gate  * The pageout thread empties this list by initiating i/o operations.
11330Sstevel@tonic-gate  */
11340Sstevel@tonic-gate int
11350Sstevel@tonic-gate queue_io_request(vnode_t *vp, u_offset_t off)
11360Sstevel@tonic-gate {
11370Sstevel@tonic-gate 	struct async_reqs *arg;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	/*
11400Sstevel@tonic-gate 	 * If we cannot allocate an async request struct,
11410Sstevel@tonic-gate 	 * skip this page.
11420Sstevel@tonic-gate 	 */
11430Sstevel@tonic-gate 	mutex_enter(&push_lock);
11440Sstevel@tonic-gate 	if ((arg = req_freelist) == NULL) {
11450Sstevel@tonic-gate 		mutex_exit(&push_lock);
11460Sstevel@tonic-gate 		return (0);
11470Sstevel@tonic-gate 	}
11480Sstevel@tonic-gate 	req_freelist = arg->a_next;		/* adjust freelist */
11490Sstevel@tonic-gate 	push_list_size++;
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	arg->a_vp = vp;
11520Sstevel@tonic-gate 	arg->a_off = off;
11530Sstevel@tonic-gate 	arg->a_len = PAGESIZE;
11540Sstevel@tonic-gate 	arg->a_flags = B_ASYNC | B_FREE;
11550Sstevel@tonic-gate 	arg->a_cred = kcred;		/* always held */
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	/*
11580Sstevel@tonic-gate 	 * Add to list of pending write requests.
11590Sstevel@tonic-gate 	 */
11600Sstevel@tonic-gate 	arg->a_next = push_list;
11610Sstevel@tonic-gate 	push_list = arg;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	if (req_freelist == NULL) {
11640Sstevel@tonic-gate 		/*
11650Sstevel@tonic-gate 		 * No free async requests left. The lock is held so we
11660Sstevel@tonic-gate 		 * might as well signal the pusher thread now.
11670Sstevel@tonic-gate 		 */
11680Sstevel@tonic-gate 		cv_signal(&push_cv);
11690Sstevel@tonic-gate 	}
11700Sstevel@tonic-gate 	mutex_exit(&push_lock);
11710Sstevel@tonic-gate 	return (1);
11720Sstevel@tonic-gate }
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate /*
11750Sstevel@tonic-gate  * Wakeup pageout to initiate i/o if push_list is not empty.
11760Sstevel@tonic-gate  */
11770Sstevel@tonic-gate void
11780Sstevel@tonic-gate cv_signal_pageout()
11790Sstevel@tonic-gate {
11800Sstevel@tonic-gate 	if (push_list != NULL) {
11810Sstevel@tonic-gate 		mutex_enter(&push_lock);
11820Sstevel@tonic-gate 		cv_signal(&push_cv);
11830Sstevel@tonic-gate 		mutex_exit(&push_lock);
11840Sstevel@tonic-gate 	}
11850Sstevel@tonic-gate }
1186