17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5aa59c4cbSrsb  * Common Development and Distribution License (the "License").
6aa59c4cbSrsb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22604f3914SVijay Balakrishna, SG-RPE  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
26*d5a81659SBryan Cantrill /*
27*d5a81659SBryan Cantrill  * Copyright 2024 Oxide Computer Company
28*d5a81659SBryan Cantrill  */
29*d5a81659SBryan Cantrill 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
36aa59c4cbSrsb #include <sys/vfs_opreg.h>
377c478bd9Sstevel@tonic-gate #include <sys/swap.h>
387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
397c478bd9Sstevel@tonic-gate #include <sys/buf.h>
407c478bd9Sstevel@tonic-gate #include <sys/callb.h>
417c478bd9Sstevel@tonic-gate #include <sys/debug.h>
427c478bd9Sstevel@tonic-gate #include <vm/seg.h>
437c478bd9Sstevel@tonic-gate #include <sys/fs/swapnode.h>
447c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
457c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
467c478bd9Sstevel@tonic-gate #include <sys/mem_config.h>
477c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate extern const fs_operation_def_t swap_vnodeops_template[];
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * swapfs_minfree is the amount of physical memory (actually remaining
537c478bd9Sstevel@tonic-gate  * availrmem) that we want to keep free for the rest of the system.  This
547c478bd9Sstevel@tonic-gate  * means that swapfs can only grow to availrmem - swapfs_minfree.  This
557c478bd9Sstevel@tonic-gate  * can be set as just constant value or a certain percentage of installed
567c478bd9Sstevel@tonic-gate  * physical memory. It is set in swapinit().
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * Users who want to change the amount of memory that can be used as swap
597c478bd9Sstevel@tonic-gate  * space should do so by setting swapfs_desfree at boot time,
607c478bd9Sstevel@tonic-gate  * not swapfs_minfree.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate pgcnt_t swapfs_desfree = 0;
647c478bd9Sstevel@tonic-gate pgcnt_t swapfs_minfree = 0;
657c478bd9Sstevel@tonic-gate pgcnt_t swapfs_reserve = 0;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #ifdef SWAPFS_DEBUG
687c478bd9Sstevel@tonic-gate int swapfs_debug;
697c478bd9Sstevel@tonic-gate #endif /* SWAPFS_DEBUG */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static int swapfs_vpcount;
737c478bd9Sstevel@tonic-gate static kmutex_t swapfs_lock;
747c478bd9Sstevel@tonic-gate static struct async_reqs *sw_ar, *sw_pendlist, *sw_freelist;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static struct vnode **swap_vnodes;	/* ptr's to swap vnodes */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static void swap_init_mem_config(void);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static pgcnt_t initial_swapfs_desfree;
817c478bd9Sstevel@tonic-gate static pgcnt_t initial_swapfs_minfree;
827c478bd9Sstevel@tonic-gate static pgcnt_t initial_swapfs_reserve;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static int swap_sync(struct vfs *vfsp, short flag, struct cred *cr);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static void
swapfs_recalc_save_initial(void)877c478bd9Sstevel@tonic-gate swapfs_recalc_save_initial(void)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	initial_swapfs_desfree = swapfs_desfree;
907c478bd9Sstevel@tonic-gate 	initial_swapfs_minfree = swapfs_minfree;
917c478bd9Sstevel@tonic-gate 	initial_swapfs_reserve = swapfs_reserve;
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static int
swapfs_recalc(pgcnt_t pgs)957c478bd9Sstevel@tonic-gate swapfs_recalc(pgcnt_t pgs)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	pgcnt_t new_swapfs_desfree;
987c478bd9Sstevel@tonic-gate 	pgcnt_t new_swapfs_minfree;
997c478bd9Sstevel@tonic-gate 	pgcnt_t new_swapfs_reserve;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	new_swapfs_desfree = initial_swapfs_desfree;
1027c478bd9Sstevel@tonic-gate 	new_swapfs_minfree = initial_swapfs_minfree;
1037c478bd9Sstevel@tonic-gate 	new_swapfs_reserve = initial_swapfs_reserve;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (new_swapfs_desfree == 0)
1067c478bd9Sstevel@tonic-gate 		new_swapfs_desfree = btopr(7 * 512 * 1024); /* 3-1/2Mb */;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (new_swapfs_minfree == 0) {
1097c478bd9Sstevel@tonic-gate 		/*
110*d5a81659SBryan Cantrill 		 * Set swapfs_minfree to be an eighth of physical, but
111*d5a81659SBryan Cantrill 		 * capped at 512 MiB.
1127c478bd9Sstevel@tonic-gate 		 */
113*d5a81659SBryan Cantrill 		new_swapfs_minfree = MIN(btopr(512 * 1024 * 1024), pgs >> 3);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/*
1177c478bd9Sstevel@tonic-gate 	 * priv processes can reserve memory as swap as long as availrmem
1187c478bd9Sstevel@tonic-gate 	 * remains greater than swapfs_minfree; in the case of non-priv
1197c478bd9Sstevel@tonic-gate 	 * processes, memory can be reserved as swap only if availrmem
1207c478bd9Sstevel@tonic-gate 	 * doesn't fall below (swapfs_minfree + swapfs_reserve). Thus,
1217c478bd9Sstevel@tonic-gate 	 * swapfs_reserve amount of memswap is not available to non-priv
1227c478bd9Sstevel@tonic-gate 	 * processes. This protects daemons such as automounter dying
1237c478bd9Sstevel@tonic-gate 	 * as a result of application processes eating away almost entire
1247c478bd9Sstevel@tonic-gate 	 * membased swap. This safeguard becomes useless if apps are run
1257c478bd9Sstevel@tonic-gate 	 * with root access.
1267c478bd9Sstevel@tonic-gate 	 *
1277c478bd9Sstevel@tonic-gate 	 * set swapfs_reserve to a minimum of 4Mb or 1/128 of physmem whichever
1287c478bd9Sstevel@tonic-gate 	 * is greater up to the limit of 128 MB.
1297c478bd9Sstevel@tonic-gate 	 */
1307c478bd9Sstevel@tonic-gate 	if (new_swapfs_reserve == 0)
1317c478bd9Sstevel@tonic-gate 		new_swapfs_reserve = MIN(btopr(128 * 1024 * 1024),
1327c478bd9Sstevel@tonic-gate 		    MAX(btopr(4 * 1024 * 1024), pgs >> 7));
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/* Test basic numeric viability. */
1357c478bd9Sstevel@tonic-gate 	if (new_swapfs_minfree > pgs)
1367c478bd9Sstevel@tonic-gate 		return (0);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/* Equivalent test to anon_resvmem() check. */
1397c478bd9Sstevel@tonic-gate 	if (availrmem < new_swapfs_minfree) {
1407c478bd9Sstevel@tonic-gate 		/*
1417c478bd9Sstevel@tonic-gate 		 * If ism pages are being used, then there must be agreement
1427c478bd9Sstevel@tonic-gate 		 * between these two policies.
1437c478bd9Sstevel@tonic-gate 		 */
1447c478bd9Sstevel@tonic-gate 		if ((availrmem > segspt_minfree) && (segspt_minfree > 0)) {
1457c478bd9Sstevel@tonic-gate 			new_swapfs_minfree = segspt_minfree;
1467c478bd9Sstevel@tonic-gate 		} else {
1477c478bd9Sstevel@tonic-gate 			return (0);
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	swapfs_desfree = new_swapfs_desfree;
1527c478bd9Sstevel@tonic-gate 	swapfs_minfree = new_swapfs_minfree;
1537c478bd9Sstevel@tonic-gate 	swapfs_reserve = new_swapfs_reserve;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	return (1);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
1597c478bd9Sstevel@tonic-gate int
swapinit(int fstype,char * name)1607c478bd9Sstevel@tonic-gate swapinit(int fstype, char *name)
161*d5a81659SBryan Cantrill {
162*d5a81659SBryan Cantrill 	/* reserve for mp */
1637c478bd9Sstevel@tonic-gate 	ssize_t sw_freelist_size = klustsize / PAGESIZE * 2;
1647c478bd9Sstevel@tonic-gate 	int i, error;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t swap_vfsops[] = {
167aa59c4cbSrsb 		VFSNAME_SYNC, { .vfs_sync = swap_sync },
1687c478bd9Sstevel@tonic-gate 		NULL, NULL
1697c478bd9Sstevel@tonic-gate 	};
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	SWAPFS_PRINT(SWAP_SUBR, "swapinit\n", 0, 0, 0, 0, 0);
1727c478bd9Sstevel@tonic-gate 	mutex_init(&swapfs_lock, NULL, MUTEX_DEFAULT, NULL);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	swap_vnodes = kmem_zalloc(MAX_SWAP_VNODES * sizeof (struct vnode *),
1757c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	swapfs_recalc_save_initial();
1787c478bd9Sstevel@tonic-gate 	if (!swapfs_recalc(physmem))
1797c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "swapfs_minfree(%lu) > physmem(%lu)",
1807c478bd9Sstevel@tonic-gate 		    swapfs_minfree, physmem);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * Arrange for a callback on memory size change.
1847c478bd9Sstevel@tonic-gate 	 */
1857c478bd9Sstevel@tonic-gate 	swap_init_mem_config();
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	sw_ar = (struct async_reqs *)
1887c478bd9Sstevel@tonic-gate 	    kmem_zalloc(sw_freelist_size*sizeof (struct async_reqs), KM_SLEEP);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstype, swap_vfsops, NULL);
1917c478bd9Sstevel@tonic-gate 	if (error != 0) {
1927c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "swapinit: bad vfs ops template");
1937c478bd9Sstevel@tonic-gate 		return (error);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	error = vn_make_ops(name, swap_vnodeops_template, &swap_vnodeops);
1977c478bd9Sstevel@tonic-gate 	if (error != 0) {
1987c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
1997c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "swapinit: bad vnode ops template");
2007c478bd9Sstevel@tonic-gate 		return (error);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 	sw_freelist = sw_ar;
2037c478bd9Sstevel@tonic-gate 	for (i = 0; i < sw_freelist_size - 1; i++)
2047c478bd9Sstevel@tonic-gate 		sw_ar[i].a_next = &sw_ar[i + 1];
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	return (0);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * Get a swapfs vnode corresponding to the specified identifier.
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate struct vnode *
swapfs_getvp(ulong_t vidx)2137c478bd9Sstevel@tonic-gate swapfs_getvp(ulong_t vidx)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	struct vnode *vp;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	vp = swap_vnodes[vidx];
2187c478bd9Sstevel@tonic-gate 	if (vp) {
2197c478bd9Sstevel@tonic-gate 		return (vp);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	mutex_enter(&swapfs_lock);
2237c478bd9Sstevel@tonic-gate 	vp = swap_vnodes[vidx];
2247c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
2257c478bd9Sstevel@tonic-gate 		vp = vn_alloc(KM_SLEEP);
2267c478bd9Sstevel@tonic-gate 		vn_setops(vp, swap_vnodeops);
2277c478bd9Sstevel@tonic-gate 		vp->v_type = VREG;
2287c478bd9Sstevel@tonic-gate 		vp->v_flag |= (VISSWAP|VISSWAPFS);
2297c478bd9Sstevel@tonic-gate 		swap_vnodes[vidx] = vp;
2307c478bd9Sstevel@tonic-gate 		swapfs_vpcount++;
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 	mutex_exit(&swapfs_lock);
2337c478bd9Sstevel@tonic-gate 	return (vp);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate int swap_lo;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2397c478bd9Sstevel@tonic-gate static int
swap_sync(struct vfs * vfsp,short flag,struct cred * cr)2407c478bd9Sstevel@tonic-gate swap_sync(struct vfs *vfsp, short flag, struct cred *cr)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	struct vnode *vp;
2437c478bd9Sstevel@tonic-gate 	int i;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (!(flag & SYNC_ALL))
2467c478bd9Sstevel@tonic-gate 		return (1);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/*
2497c478bd9Sstevel@tonic-gate 	 * assumes that we are the only one left to access this so that
2507c478bd9Sstevel@tonic-gate 	 * no need to use swapfs_lock (since it's staticly defined)
2517c478bd9Sstevel@tonic-gate 	 */
2527c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SWAP_VNODES; i++) {
2537c478bd9Sstevel@tonic-gate 		vp = swap_vnodes[i];
2547c478bd9Sstevel@tonic-gate 		if (vp) {
2557c478bd9Sstevel@tonic-gate 			VN_HOLD(vp);
2567c478bd9Sstevel@tonic-gate 			(void) VOP_PUTPAGE(vp, (offset_t)0, 0,
257da6c28aaSamw 			    (B_ASYNC | B_FREE), kcred, NULL);
2587c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 	return (0);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate extern int sw_pending_size;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * Take an async request off the pending queue
2687c478bd9Sstevel@tonic-gate  */
2697c478bd9Sstevel@tonic-gate struct async_reqs *
sw_getreq()2707c478bd9Sstevel@tonic-gate sw_getreq()
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	struct async_reqs *arg;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	mutex_enter(&swapfs_lock);
2757c478bd9Sstevel@tonic-gate 	arg = sw_pendlist;
2767c478bd9Sstevel@tonic-gate 	if (arg) {
2777c478bd9Sstevel@tonic-gate 		sw_pendlist = arg->a_next;
2787c478bd9Sstevel@tonic-gate 		arg->a_next = NULL;
2797c478bd9Sstevel@tonic-gate 		sw_pending_size -= PAGESIZE;
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 	ASSERT(sw_pending_size >= 0);
2827c478bd9Sstevel@tonic-gate 	mutex_exit(&swapfs_lock);
2837c478bd9Sstevel@tonic-gate 	return (arg);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate  * Put an async request on the pending queue
2887c478bd9Sstevel@tonic-gate  */
2897c478bd9Sstevel@tonic-gate void
sw_putreq(struct async_reqs * arg)2907c478bd9Sstevel@tonic-gate sw_putreq(struct async_reqs *arg)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	/* Hold onto it */
2937c478bd9Sstevel@tonic-gate 	VN_HOLD(arg->a_vp);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	mutex_enter(&swapfs_lock);
2967c478bd9Sstevel@tonic-gate 	arg->a_next = sw_pendlist;
2977c478bd9Sstevel@tonic-gate 	sw_pendlist = arg;
2987c478bd9Sstevel@tonic-gate 	sw_pending_size += PAGESIZE;
2997c478bd9Sstevel@tonic-gate 	mutex_exit(&swapfs_lock);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * Put an async request back on the pending queue
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate void
sw_putbackreq(struct async_reqs * arg)3067c478bd9Sstevel@tonic-gate sw_putbackreq(struct async_reqs *arg)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	mutex_enter(&swapfs_lock);
3097c478bd9Sstevel@tonic-gate 	arg->a_next = sw_pendlist;
3107c478bd9Sstevel@tonic-gate 	sw_pendlist = arg;
3117c478bd9Sstevel@tonic-gate 	sw_pending_size += PAGESIZE;
3127c478bd9Sstevel@tonic-gate 	mutex_exit(&swapfs_lock);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate /*
3167c478bd9Sstevel@tonic-gate  * Take an async request structure off the free list
3177c478bd9Sstevel@tonic-gate  */
3187c478bd9Sstevel@tonic-gate struct async_reqs *
sw_getfree()3197c478bd9Sstevel@tonic-gate sw_getfree()
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	struct async_reqs *arg;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	mutex_enter(&swapfs_lock);
3247c478bd9Sstevel@tonic-gate 	arg = sw_freelist;
3257c478bd9Sstevel@tonic-gate 	if (arg) {
3267c478bd9Sstevel@tonic-gate 		sw_freelist = arg->a_next;
3277c478bd9Sstevel@tonic-gate 		arg->a_next = NULL;
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 	mutex_exit(&swapfs_lock);
3307c478bd9Sstevel@tonic-gate 	return (arg);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  * Put an async request structure on the free list
3357c478bd9Sstevel@tonic-gate  */
3367c478bd9Sstevel@tonic-gate void
sw_putfree(struct async_reqs * arg)3377c478bd9Sstevel@tonic-gate sw_putfree(struct async_reqs *arg)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	/* Release our hold - should have locked the page by now */
3407c478bd9Sstevel@tonic-gate 	VN_RELE(arg->a_vp);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	mutex_enter(&swapfs_lock);
3437c478bd9Sstevel@tonic-gate 	arg->a_next = sw_freelist;
3447c478bd9Sstevel@tonic-gate 	sw_freelist = arg;
3457c478bd9Sstevel@tonic-gate 	mutex_exit(&swapfs_lock);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate static pgcnt_t swapfs_pending_delete;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3517c478bd9Sstevel@tonic-gate static void
swap_mem_config_post_add(void * arg,pgcnt_t delta_swaps)3527c478bd9Sstevel@tonic-gate swap_mem_config_post_add(
3537c478bd9Sstevel@tonic-gate 	void *arg,
3547c478bd9Sstevel@tonic-gate 	pgcnt_t delta_swaps)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	(void) swapfs_recalc(physmem - swapfs_pending_delete);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3607c478bd9Sstevel@tonic-gate static int
swap_mem_config_pre_del(void * arg,pgcnt_t delta_swaps)3617c478bd9Sstevel@tonic-gate swap_mem_config_pre_del(
3627c478bd9Sstevel@tonic-gate 	void *arg,
3637c478bd9Sstevel@tonic-gate 	pgcnt_t delta_swaps)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	pgcnt_t nv;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	nv = atomic_add_long_nv(&swapfs_pending_delete, (spgcnt_t)delta_swaps);
3687c478bd9Sstevel@tonic-gate 	if (!swapfs_recalc(physmem - nv)) {
3697c478bd9Sstevel@tonic-gate 		/*
3707c478bd9Sstevel@tonic-gate 		 * Tidy-up is done by the call to post_del which
3717c478bd9Sstevel@tonic-gate 		 * is always made.
3727c478bd9Sstevel@tonic-gate 		 */
373604f3914SVijay Balakrishna, SG-RPE 		cmn_err(CE_NOTE, "Memory operation refused to ensure system "
374604f3914SVijay Balakrishna, SG-RPE 		    "doesn't deadlock due to excessive consumption by swapfs.");
3757c478bd9Sstevel@tonic-gate 		return (EBUSY);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 	return (0);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3817c478bd9Sstevel@tonic-gate static void
swap_mem_config_post_del(void * arg,pgcnt_t delta_swaps,int cancelled)3827c478bd9Sstevel@tonic-gate swap_mem_config_post_del(
3837c478bd9Sstevel@tonic-gate 	void *arg,
3847c478bd9Sstevel@tonic-gate 	pgcnt_t delta_swaps,
3857c478bd9Sstevel@tonic-gate 	int cancelled)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	pgcnt_t nv;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	nv = atomic_add_long_nv(&swapfs_pending_delete, -(spgcnt_t)delta_swaps);
3907c478bd9Sstevel@tonic-gate 	(void) swapfs_recalc(physmem - nv);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate static kphysm_setup_vector_t swap_mem_config_vec = {
3947c478bd9Sstevel@tonic-gate 	KPHYSM_SETUP_VECTOR_VERSION,
3957c478bd9Sstevel@tonic-gate 	swap_mem_config_post_add,
3967c478bd9Sstevel@tonic-gate 	swap_mem_config_pre_del,
3977c478bd9Sstevel@tonic-gate 	swap_mem_config_post_del,
3987c478bd9Sstevel@tonic-gate };
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate static void
swap_init_mem_config(void)4017c478bd9Sstevel@tonic-gate swap_init_mem_config(void)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	int ret;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	ret = kphysm_setup_func_register(&swap_mem_config_vec, (void *)NULL);
4067c478bd9Sstevel@tonic-gate 	ASSERT(ret == 0);
4077c478bd9Sstevel@tonic-gate }
408