xref: /dragonfly/sys/vm/vm_swapcache.c (revision 64949baa)
1096e95c0SMatthew Dillon /*
28e7c4729SMatthew Dillon  * (MPSAFE)
38e7c4729SMatthew Dillon  *
4096e95c0SMatthew Dillon  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
5096e95c0SMatthew Dillon  *
6096e95c0SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
7096e95c0SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
8096e95c0SMatthew Dillon  *
9096e95c0SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
10096e95c0SMatthew Dillon  * modification, are permitted provided that the following conditions
11096e95c0SMatthew Dillon  * are met:
12096e95c0SMatthew Dillon  *
13096e95c0SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
14096e95c0SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
15096e95c0SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
16096e95c0SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
17096e95c0SMatthew Dillon  *    the documentation and/or other materials provided with the
18096e95c0SMatthew Dillon  *    distribution.
19096e95c0SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
20096e95c0SMatthew Dillon  *    contributors may be used to endorse or promote products derived
21096e95c0SMatthew Dillon  *    from this software without specific, prior written permission.
22096e95c0SMatthew Dillon  *
23096e95c0SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24096e95c0SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25096e95c0SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26096e95c0SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
27096e95c0SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28096e95c0SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29096e95c0SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30096e95c0SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31096e95c0SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32096e95c0SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33096e95c0SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34096e95c0SMatthew Dillon  * SUCH DAMAGE.
35096e95c0SMatthew Dillon  */
36096e95c0SMatthew Dillon 
37096e95c0SMatthew Dillon /*
38096e95c0SMatthew Dillon  * Implement the swapcache daemon.  When enabled swap is assumed to be
39096e95c0SMatthew Dillon  * configured on a fast storage device such as a SSD.  Swap is assigned
40096e95c0SMatthew Dillon  * to clean vnode-backed pages in the inactive queue, clustered by object
41096e95c0SMatthew Dillon  * if possible, and written out.  The swap assignment sticks around even
42096e95c0SMatthew Dillon  * after the underlying pages have been recycled.
43096e95c0SMatthew Dillon  *
44096e95c0SMatthew Dillon  * The daemon manages write bandwidth based on sysctl settings to control
45096e95c0SMatthew Dillon  * wear on the SSD.
46096e95c0SMatthew Dillon  *
47096e95c0SMatthew Dillon  * The vnode strategy code will check for the swap assignments and divert
483ffc7051SMatthew Dillon  * reads to the swap device when the data is present in the swapcache.
49096e95c0SMatthew Dillon  *
50096e95c0SMatthew Dillon  * This operates on both regular files and the block device vnodes used by
51096e95c0SMatthew Dillon  * filesystems to manage meta-data.
52096e95c0SMatthew Dillon  */
53096e95c0SMatthew Dillon 
54096e95c0SMatthew Dillon #include "opt_vm.h"
55096e95c0SMatthew Dillon #include <sys/param.h>
56096e95c0SMatthew Dillon #include <sys/systm.h>
57096e95c0SMatthew Dillon #include <sys/kernel.h>
58096e95c0SMatthew Dillon #include <sys/proc.h>
59096e95c0SMatthew Dillon #include <sys/kthread.h>
60096e95c0SMatthew Dillon #include <sys/resourcevar.h>
61096e95c0SMatthew Dillon #include <sys/signalvar.h>
62096e95c0SMatthew Dillon #include <sys/vnode.h>
63096e95c0SMatthew Dillon #include <sys/vmmeter.h>
64096e95c0SMatthew Dillon #include <sys/sysctl.h>
65497524bfSMatthew Dillon #include <sys/eventhandler.h>
66096e95c0SMatthew Dillon 
67096e95c0SMatthew Dillon #include <vm/vm.h>
68096e95c0SMatthew Dillon #include <vm/vm_param.h>
69096e95c0SMatthew Dillon #include <sys/lock.h>
70096e95c0SMatthew Dillon #include <vm/vm_object.h>
71096e95c0SMatthew Dillon #include <vm/vm_page.h>
72096e95c0SMatthew Dillon #include <vm/vm_map.h>
73096e95c0SMatthew Dillon #include <vm/vm_pageout.h>
74096e95c0SMatthew Dillon #include <vm/vm_pager.h>
75096e95c0SMatthew Dillon #include <vm/swap_pager.h>
76096e95c0SMatthew Dillon #include <vm/vm_extern.h>
77096e95c0SMatthew Dillon 
78096e95c0SMatthew Dillon #include <sys/thread2.h>
79b12defdcSMatthew Dillon #include <sys/spinlock2.h>
80096e95c0SMatthew Dillon #include <vm/vm_page2.h>
81096e95c0SMatthew Dillon 
82096e95c0SMatthew Dillon /* the kernel process "vm_pageout"*/
83aabd5ce8SMatthew Dillon static int vm_swapcached_flush (vm_page_t m, int isblkdev);
843ffc7051SMatthew Dillon static int vm_swapcache_test(vm_page_t m);
85*64949baaSMatthew Dillon static int vm_swapcache_writing_heuristic(void);
86*64949baaSMatthew Dillon static int vm_swapcache_writing(vm_page_t marker, int count, int scount);
8700a3fdcaSMatthew Dillon static void vm_swapcache_cleaning(vm_object_t marker);
88f5f6d247SMatthew Dillon static void vm_swapcache_movemarker(vm_object_t marker, vm_object_t object);
89096e95c0SMatthew Dillon struct thread *swapcached_thread;
90096e95c0SMatthew Dillon 
91096e95c0SMatthew Dillon SYSCTL_NODE(_vm, OID_AUTO, swapcache, CTLFLAG_RW, NULL, NULL);
92096e95c0SMatthew Dillon 
93c504e38eSMatthew Dillon int vm_swapcache_read_enable;
94e527fb6bSMatthew Dillon int vm_swapcache_inactive_heuristic;
95096e95c0SMatthew Dillon static int vm_swapcache_sleep;
96*64949baaSMatthew Dillon static int vm_swapcache_maxscan = PQ_L2_SIZE * 8;
97*64949baaSMatthew Dillon static int vm_swapcache_maxlaunder = PQ_L2_SIZE * 4;
98096e95c0SMatthew Dillon static int vm_swapcache_data_enable = 0;
99096e95c0SMatthew Dillon static int vm_swapcache_meta_enable = 0;
100e9b56058SMatthew Dillon static int vm_swapcache_maxswappct = 75;
101e527fb6bSMatthew Dillon static int vm_swapcache_hysteresis;
102*64949baaSMatthew Dillon static int vm_swapcache_min_hysteresis;
103bfa86281SMatthew Dillon int vm_swapcache_use_chflags = 1;	/* require chflags cache */
1043ffc7051SMatthew Dillon static int64_t vm_swapcache_minburst = 10000000LL;	/* 10MB */
1053ffc7051SMatthew Dillon static int64_t vm_swapcache_curburst = 4000000000LL;	/* 4G after boot */
1063ffc7051SMatthew Dillon static int64_t vm_swapcache_maxburst = 2000000000LL;	/* 2G nominal max */
1073ffc7051SMatthew Dillon static int64_t vm_swapcache_accrate = 100000LL;		/* 100K/s */
108096e95c0SMatthew Dillon static int64_t vm_swapcache_write_count;
1093ffc7051SMatthew Dillon static int64_t vm_swapcache_maxfilesize;
110f5f6d247SMatthew Dillon static int64_t vm_swapcache_cleanperobj = 16*1024*1024;
111096e95c0SMatthew Dillon 
112096e95c0SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, maxlaunder,
113096e95c0SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxlaunder, 0, "");
1140bf81261SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, maxscan,
1150bf81261SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxscan, 0, "");
116c504e38eSMatthew Dillon 
117096e95c0SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, data_enable,
118096e95c0SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_data_enable, 0, "");
119096e95c0SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, meta_enable,
120096e95c0SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_meta_enable, 0, "");
121c504e38eSMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, read_enable,
122c504e38eSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_read_enable, 0, "");
123e9b56058SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, maxswappct,
124e9b56058SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxswappct, 0, "");
125e527fb6bSMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, hysteresis,
126*64949baaSMatthew Dillon 	CTLFLAG_RD, &vm_swapcache_hysteresis, 0, "");
127*64949baaSMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, min_hysteresis,
128*64949baaSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_min_hysteresis, 0, "");
129e9b56058SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, use_chflags,
130e9b56058SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_use_chflags, 0, "");
131c504e38eSMatthew Dillon 
1323ffc7051SMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, minburst,
1333ffc7051SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_minburst, 0, "");
134c504e38eSMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, curburst,
135c504e38eSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_curburst, 0, "");
136c504e38eSMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxburst,
137c504e38eSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxburst, 0, "");
1383ffc7051SMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxfilesize,
1393ffc7051SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxfilesize, 0, "");
140c504e38eSMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, accrate,
141c504e38eSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_accrate, 0, "");
142096e95c0SMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, write_count,
143096e95c0SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_write_count, 0, "");
144f5f6d247SMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, cleanperobj,
145f5f6d247SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_cleanperobj, 0, "");
146096e95c0SMatthew Dillon 
147e9b56058SMatthew Dillon #define SWAPMAX(adj)	\
148e9b56058SMatthew Dillon 	((int64_t)vm_swap_max * (vm_swapcache_maxswappct + (adj)) / 100)
149e9b56058SMatthew Dillon 
150096e95c0SMatthew Dillon /*
151497524bfSMatthew Dillon  * When shutting down the machine we want to stop swapcache operation
152497524bfSMatthew Dillon  * immediately so swap is not accessed after devices have been shuttered.
153497524bfSMatthew Dillon  */
154497524bfSMatthew Dillon static void
155497524bfSMatthew Dillon shutdown_swapcache(void *arg __unused)
156497524bfSMatthew Dillon {
157497524bfSMatthew Dillon 	vm_swapcache_read_enable = 0;
158497524bfSMatthew Dillon 	vm_swapcache_data_enable = 0;
159497524bfSMatthew Dillon 	vm_swapcache_meta_enable = 0;
160497524bfSMatthew Dillon 	wakeup(&vm_swapcache_sleep);	/* shortcut 5-second wait */
161497524bfSMatthew Dillon }
162497524bfSMatthew Dillon 
163497524bfSMatthew Dillon /*
164096e95c0SMatthew Dillon  * vm_swapcached is the high level pageout daemon.
1658e7c4729SMatthew Dillon  *
1668e7c4729SMatthew Dillon  * No requirements.
167096e95c0SMatthew Dillon  */
168096e95c0SMatthew Dillon static void
169cd8ab232SMatthew Dillon vm_swapcached_thread(void)
170096e95c0SMatthew Dillon {
17100a3fdcaSMatthew Dillon 	enum { SWAPC_WRITING, SWAPC_CLEANING } state = SWAPC_WRITING;
1723ffc7051SMatthew Dillon 	enum { SWAPB_BURSTING, SWAPB_RECOVERING } burst = SWAPB_BURSTING;
17351c99c61SMatthew Dillon 	static struct vm_page page_marker[PQ_L2_SIZE];
174027193ebSMatthew Dillon 	static struct vm_object object_marker;
175027193ebSMatthew Dillon 	int q;
176096e95c0SMatthew Dillon 
177096e95c0SMatthew Dillon 	/*
178096e95c0SMatthew Dillon 	 * Thread setup
179096e95c0SMatthew Dillon 	 */
180096e95c0SMatthew Dillon 	curthread->td_flags |= TDF_SYSTHREAD;
181497524bfSMatthew Dillon 	EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc,
182497524bfSMatthew Dillon 			      swapcached_thread, SHUTDOWN_PRI_FIRST);
183497524bfSMatthew Dillon 	EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_swapcache,
184497524bfSMatthew Dillon 			      NULL, SHUTDOWN_PRI_SECOND);
185096e95c0SMatthew Dillon 
186096e95c0SMatthew Dillon 	/*
18700a3fdcaSMatthew Dillon 	 * Initialize our marker for the inactive scan (SWAPC_WRITING)
188096e95c0SMatthew Dillon 	 */
18900a3fdcaSMatthew Dillon 	bzero(&page_marker, sizeof(page_marker));
19051c99c61SMatthew Dillon 	for (q = 0; q < PQ_L2_SIZE; ++q) {
191027193ebSMatthew Dillon 		page_marker[q].flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
192027193ebSMatthew Dillon 		page_marker[q].queue = PQ_INACTIVE + q;
193027193ebSMatthew Dillon 		page_marker[q].pc = q;
194027193ebSMatthew Dillon 		page_marker[q].wire_count = 1;
195027193ebSMatthew Dillon 		vm_page_queues_spin_lock(PQ_INACTIVE + q);
196027193ebSMatthew Dillon 		TAILQ_INSERT_HEAD(
197027193ebSMatthew Dillon 			&vm_page_queues[PQ_INACTIVE + q].pl,
198027193ebSMatthew Dillon 			&page_marker[q], pageq);
199027193ebSMatthew Dillon 		vm_page_queues_spin_unlock(PQ_INACTIVE + q);
200027193ebSMatthew Dillon 	}
201b12defdcSMatthew Dillon 
202*64949baaSMatthew Dillon 	vm_swapcache_min_hysteresis = 1024;
203*64949baaSMatthew Dillon 	vm_swapcache_hysteresis = vm_swapcache_min_hysteresis;
204e527fb6bSMatthew Dillon 	vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
205096e95c0SMatthew Dillon 
20600a3fdcaSMatthew Dillon 	/*
20700a3fdcaSMatthew Dillon 	 * Initialize our marker for the vm_object scan (SWAPC_CLEANING)
20800a3fdcaSMatthew Dillon 	 */
20900a3fdcaSMatthew Dillon 	bzero(&object_marker, sizeof(object_marker));
21000a3fdcaSMatthew Dillon 	object_marker.type = OBJT_MARKER;
2112de4f77eSMatthew Dillon 	lwkt_gettoken(&vmobj_token);
21200a3fdcaSMatthew Dillon 	TAILQ_INSERT_HEAD(&vm_object_list, &object_marker, object_list);
2132de4f77eSMatthew Dillon 	lwkt_reltoken(&vmobj_token);
214096e95c0SMatthew Dillon 
215096e95c0SMatthew Dillon 	for (;;) {
216*64949baaSMatthew Dillon 		int reached_end;
217*64949baaSMatthew Dillon 		int scount;
218*64949baaSMatthew Dillon 		int count;
219*64949baaSMatthew Dillon 
220096e95c0SMatthew Dillon 		/*
221497524bfSMatthew Dillon 		 * Handle shutdown
222497524bfSMatthew Dillon 		 */
223497524bfSMatthew Dillon 		kproc_suspend_loop();
224497524bfSMatthew Dillon 
225497524bfSMatthew Dillon 		/*
2263da46bd7SMatthew Dillon 		 * Check every 5 seconds when not enabled or if no swap
2273da46bd7SMatthew Dillon 		 * is present.
228096e95c0SMatthew Dillon 		 */
2293da46bd7SMatthew Dillon 		if ((vm_swapcache_data_enable == 0 &&
2303da46bd7SMatthew Dillon 		     vm_swapcache_meta_enable == 0) ||
2313da46bd7SMatthew Dillon 		    vm_swap_max == 0) {
232096e95c0SMatthew Dillon 			tsleep(&vm_swapcache_sleep, 0, "csleep", hz * 5);
233096e95c0SMatthew Dillon 			continue;
234096e95c0SMatthew Dillon 		}
235c504e38eSMatthew Dillon 
236c504e38eSMatthew Dillon 		/*
2373da46bd7SMatthew Dillon 		 * Polling rate when enabled is approximately 10 hz.
238c504e38eSMatthew Dillon 		 */
239c504e38eSMatthew Dillon 		tsleep(&vm_swapcache_sleep, 0, "csleep", hz / 10);
24000a3fdcaSMatthew Dillon 
24100a3fdcaSMatthew Dillon 		/*
24200a3fdcaSMatthew Dillon 		 * State hysteresis.  Generate write activity up to 75% of
24300a3fdcaSMatthew Dillon 		 * swap, then clean out swap assignments down to 70%, then
24400a3fdcaSMatthew Dillon 		 * repeat.
24500a3fdcaSMatthew Dillon 		 */
24600a3fdcaSMatthew Dillon 		if (state == SWAPC_WRITING) {
247e9b56058SMatthew Dillon 			if (vm_swap_cache_use > SWAPMAX(0))
24800a3fdcaSMatthew Dillon 				state = SWAPC_CLEANING;
24900a3fdcaSMatthew Dillon 		} else {
25008fb7a9dSMatthew Dillon 			if (vm_swap_cache_use < SWAPMAX(-10))
25100a3fdcaSMatthew Dillon 				state = SWAPC_WRITING;
25200a3fdcaSMatthew Dillon 		}
25300a3fdcaSMatthew Dillon 
25400a3fdcaSMatthew Dillon 		/*
25500a3fdcaSMatthew Dillon 		 * We are allowed to continue accumulating burst value
2563ffc7051SMatthew Dillon 		 * in either state.  Allow the user to set curburst > maxburst
2573ffc7051SMatthew Dillon 		 * for the initial load-in.
25800a3fdcaSMatthew Dillon 		 */
2593ffc7051SMatthew Dillon 		if (vm_swapcache_curburst < vm_swapcache_maxburst) {
260c504e38eSMatthew Dillon 			vm_swapcache_curburst += vm_swapcache_accrate / 10;
261c504e38eSMatthew Dillon 			if (vm_swapcache_curburst > vm_swapcache_maxburst)
262c504e38eSMatthew Dillon 				vm_swapcache_curburst = vm_swapcache_maxburst;
2633ffc7051SMatthew Dillon 		}
264c504e38eSMatthew Dillon 
265c504e38eSMatthew Dillon 		/*
26600a3fdcaSMatthew Dillon 		 * We don't want to nickle-and-dime the scan as that will
26700a3fdcaSMatthew Dillon 		 * create unnecessary fragmentation.  The minimum burst
26800a3fdcaSMatthew Dillon 		 * is one-seconds worth of accumulation.
269c504e38eSMatthew Dillon 		 */
270*64949baaSMatthew Dillon 		if (state != SWAPC_WRITING) {
271*64949baaSMatthew Dillon 			vm_swapcache_cleaning(&object_marker);
272*64949baaSMatthew Dillon 			continue;
273*64949baaSMatthew Dillon 		}
274*64949baaSMatthew Dillon 		if (vm_swapcache_curburst < vm_swapcache_accrate)
275*64949baaSMatthew Dillon 			continue;
276*64949baaSMatthew Dillon 
277*64949baaSMatthew Dillon 		reached_end = 0;
278*64949baaSMatthew Dillon 		count = vm_swapcache_maxlaunder / PQ_L2_SIZE + 2;
279*64949baaSMatthew Dillon 		scount = vm_swapcache_maxscan / PQ_L2_SIZE + 2;
280*64949baaSMatthew Dillon 
2813ffc7051SMatthew Dillon 		if (burst == SWAPB_BURSTING) {
282*64949baaSMatthew Dillon 			if (vm_swapcache_writing_heuristic()) {
28351c99c61SMatthew Dillon 				for (q = 0; q < PQ_L2_SIZE; ++q) {
284*64949baaSMatthew Dillon 					reached_end +=
285027193ebSMatthew Dillon 						vm_swapcache_writing(
286*64949baaSMatthew Dillon 							&page_marker[q],
287*64949baaSMatthew Dillon 							count,
288*64949baaSMatthew Dillon 							scount);
289*64949baaSMatthew Dillon 				}
290027193ebSMatthew Dillon 			}
2913ffc7051SMatthew Dillon 			if (vm_swapcache_curburst <= 0)
2923ffc7051SMatthew Dillon 				burst = SWAPB_RECOVERING;
293*64949baaSMatthew Dillon 		} else if (vm_swapcache_curburst > vm_swapcache_minburst) {
294*64949baaSMatthew Dillon 			if (vm_swapcache_writing_heuristic()) {
29551c99c61SMatthew Dillon 				for (q = 0; q < PQ_L2_SIZE; ++q) {
296*64949baaSMatthew Dillon 					reached_end +=
297027193ebSMatthew Dillon 						vm_swapcache_writing(
298*64949baaSMatthew Dillon 							&page_marker[q],
299*64949baaSMatthew Dillon 							count,
300*64949baaSMatthew Dillon 							scount);
301*64949baaSMatthew Dillon 				}
302027193ebSMatthew Dillon 			}
3033ffc7051SMatthew Dillon 			burst = SWAPB_BURSTING;
3043ffc7051SMatthew Dillon 		}
305*64949baaSMatthew Dillon 		if (reached_end == PQ_L2_SIZE) {
306*64949baaSMatthew Dillon 			vm_swapcache_inactive_heuristic =
307*64949baaSMatthew Dillon 				-vm_swapcache_hysteresis;
30800a3fdcaSMatthew Dillon 		}
30900a3fdcaSMatthew Dillon 	}
310eccc8ca1SMatthew Dillon 
311eccc8ca1SMatthew Dillon 	/*
312eccc8ca1SMatthew Dillon 	 * Cleanup (NOT REACHED)
313eccc8ca1SMatthew Dillon 	 */
31451c99c61SMatthew Dillon 	for (q = 0; q < PQ_L2_SIZE; ++q) {
315027193ebSMatthew Dillon 		vm_page_queues_spin_lock(PQ_INACTIVE + q);
316027193ebSMatthew Dillon 		TAILQ_REMOVE(
317027193ebSMatthew Dillon 			&vm_page_queues[PQ_INACTIVE + q].pl,
318027193ebSMatthew Dillon 			&page_marker[q], pageq);
319027193ebSMatthew Dillon 		vm_page_queues_spin_unlock(PQ_INACTIVE + q);
320027193ebSMatthew Dillon 	}
321eccc8ca1SMatthew Dillon 
3222de4f77eSMatthew Dillon 	lwkt_gettoken(&vmobj_token);
32300a3fdcaSMatthew Dillon 	TAILQ_REMOVE(&vm_object_list, &object_marker, object_list);
3242de4f77eSMatthew Dillon 	lwkt_reltoken(&vmobj_token);
32500a3fdcaSMatthew Dillon }
326096e95c0SMatthew Dillon 
327cd8ab232SMatthew Dillon static struct kproc_desc swpc_kp = {
328cd8ab232SMatthew Dillon 	"swapcached",
329cd8ab232SMatthew Dillon 	vm_swapcached_thread,
330cd8ab232SMatthew Dillon 	&swapcached_thread
331cd8ab232SMatthew Dillon };
332cd8ab232SMatthew Dillon SYSINIT(swapcached, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, &swpc_kp)
333cd8ab232SMatthew Dillon 
334096e95c0SMatthew Dillon /*
335fdc53cc7SMatthew Dillon  * Deal with an overflow of the heuristic counter or if the user
336fdc53cc7SMatthew Dillon  * manually changes the hysteresis.
337fdc53cc7SMatthew Dillon  *
338e527fb6bSMatthew Dillon  * Try to avoid small incremental pageouts by waiting for enough
339e527fb6bSMatthew Dillon  * pages to buildup in the inactive queue to hopefully get a good
340e527fb6bSMatthew Dillon  * burst in.  This heuristic is bumped by the VM system and reset
341e527fb6bSMatthew Dillon  * when our scan hits the end of the queue.
342*64949baaSMatthew Dillon  *
343*64949baaSMatthew Dillon  * Return TRUE if we need to take a writing pass.
344e527fb6bSMatthew Dillon  */
345*64949baaSMatthew Dillon static int
346*64949baaSMatthew Dillon vm_swapcache_writing_heuristic(void)
347*64949baaSMatthew Dillon {
348*64949baaSMatthew Dillon 	int hyst;
349*64949baaSMatthew Dillon 
350*64949baaSMatthew Dillon 	hyst = vmstats.v_inactive_count / 4;
351*64949baaSMatthew Dillon 	if (hyst < vm_swapcache_min_hysteresis)
352*64949baaSMatthew Dillon 		hyst = vm_swapcache_min_hysteresis;
353*64949baaSMatthew Dillon 	cpu_ccfence();
354*64949baaSMatthew Dillon 	vm_swapcache_hysteresis = hyst;
355*64949baaSMatthew Dillon 
356*64949baaSMatthew Dillon 	if (vm_swapcache_inactive_heuristic < -hyst)
357*64949baaSMatthew Dillon 		vm_swapcache_inactive_heuristic = -hyst;
358*64949baaSMatthew Dillon 
359*64949baaSMatthew Dillon 	return (vm_swapcache_inactive_heuristic >= 0);
360*64949baaSMatthew Dillon }
361*64949baaSMatthew Dillon 
362*64949baaSMatthew Dillon /*
363*64949baaSMatthew Dillon  * Take a writing pass on one of the inactive queues, return non-zero if
364*64949baaSMatthew Dillon  * we hit the end of the queue.
365*64949baaSMatthew Dillon  */
366*64949baaSMatthew Dillon static int
367*64949baaSMatthew Dillon vm_swapcache_writing(vm_page_t marker, int count, int scount)
368*64949baaSMatthew Dillon {
369*64949baaSMatthew Dillon 	vm_object_t object;
370*64949baaSMatthew Dillon 	struct vnode *vp;
371*64949baaSMatthew Dillon 	vm_page_t m;
372*64949baaSMatthew Dillon 	int isblkdev;
373e527fb6bSMatthew Dillon 
374e527fb6bSMatthew Dillon 	/*
375096e95c0SMatthew Dillon 	 * Scan the inactive queue from our marker to locate
376096e95c0SMatthew Dillon 	 * suitable pages to push to the swap cache.
377096e95c0SMatthew Dillon 	 *
378096e95c0SMatthew Dillon 	 * We are looking for clean vnode-backed pages.
379096e95c0SMatthew Dillon 	 */
380027193ebSMatthew Dillon 	vm_page_queues_spin_lock(marker->queue);
3810bf81261SMatthew Dillon 	while ((m = TAILQ_NEXT(marker, pageq)) != NULL &&
3820bf81261SMatthew Dillon 	       count > 0 && scount-- > 0) {
383027193ebSMatthew Dillon 		KKASSERT(m->queue == marker->queue);
384b12defdcSMatthew Dillon 
385b12defdcSMatthew Dillon 		if (vm_swapcache_curburst < 0)
386b12defdcSMatthew Dillon 			break;
387027193ebSMatthew Dillon 		TAILQ_REMOVE(
388027193ebSMatthew Dillon 			&vm_page_queues[marker->queue].pl, marker, pageq);
389027193ebSMatthew Dillon 		TAILQ_INSERT_AFTER(
390027193ebSMatthew Dillon 			&vm_page_queues[marker->queue].pl, m, marker, pageq);
391f5f6d247SMatthew Dillon 
392f5f6d247SMatthew Dillon 		/*
393f5f6d247SMatthew Dillon 		 * Ignore markers and ignore pages that already have a swap
394f5f6d247SMatthew Dillon 		 * assignment.
395f5f6d247SMatthew Dillon 		 */
3960bf81261SMatthew Dillon 		if (m->flags & (PG_MARKER | PG_SWAPPED))
397096e95c0SMatthew Dillon 			continue;
398b12defdcSMatthew Dillon 		if (vm_page_busy_try(m, TRUE))
399096e95c0SMatthew Dillon 			continue;
400027193ebSMatthew Dillon 		vm_page_queues_spin_unlock(marker->queue);
401b12defdcSMatthew Dillon 
402b12defdcSMatthew Dillon 		if ((object = m->object) == NULL) {
403b12defdcSMatthew Dillon 			vm_page_wakeup(m);
404027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
405b12defdcSMatthew Dillon 			continue;
406b12defdcSMatthew Dillon 		}
407b12defdcSMatthew Dillon 		vm_object_hold(object);
408b12defdcSMatthew Dillon 		if (m->object != object) {
409b12defdcSMatthew Dillon 			vm_object_drop(object);
410b12defdcSMatthew Dillon 			vm_page_wakeup(m);
411027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
412b12defdcSMatthew Dillon 			continue;
413b12defdcSMatthew Dillon 		}
414b12defdcSMatthew Dillon 		if (vm_swapcache_test(m)) {
415b12defdcSMatthew Dillon 			vm_object_drop(object);
416b12defdcSMatthew Dillon 			vm_page_wakeup(m);
417027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
418b12defdcSMatthew Dillon 			continue;
419b12defdcSMatthew Dillon 		}
420b12defdcSMatthew Dillon 
421c504e38eSMatthew Dillon 		vp = object->handle;
422b12defdcSMatthew Dillon 		if (vp == NULL) {
423b12defdcSMatthew Dillon 			vm_object_drop(object);
424b12defdcSMatthew Dillon 			vm_page_wakeup(m);
425027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
426c504e38eSMatthew Dillon 			continue;
427b12defdcSMatthew Dillon 		}
428d3070b8dSMatthew Dillon 
429c504e38eSMatthew Dillon 		switch(vp->v_type) {
430c504e38eSMatthew Dillon 		case VREG:
431e9b56058SMatthew Dillon 			/*
432bfa86281SMatthew Dillon 			 * PG_NOTMETA generically means 'don't swapcache this',
433bfa86281SMatthew Dillon 			 * and HAMMER will set this for regular data buffers
434bfa86281SMatthew Dillon 			 * (and leave it unset for meta-data buffers) as
435bfa86281SMatthew Dillon 			 * appropriate when double buffering is enabled.
436bfa86281SMatthew Dillon 			 */
437b12defdcSMatthew Dillon 			if (m->flags & PG_NOTMETA) {
438b12defdcSMatthew Dillon 				vm_object_drop(object);
439b12defdcSMatthew Dillon 				vm_page_wakeup(m);
440027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
441bfa86281SMatthew Dillon 				continue;
442b12defdcSMatthew Dillon 			}
443bfa86281SMatthew Dillon 
444bfa86281SMatthew Dillon 			/*
445e9b56058SMatthew Dillon 			 * If data_enable is 0 do not try to swapcache data.
446e9b56058SMatthew Dillon 			 * If use_chflags is set then only swapcache data for
447e9b56058SMatthew Dillon 			 * VSWAPCACHE marked vnodes, otherwise any vnode.
448e9b56058SMatthew Dillon 			 */
449e9b56058SMatthew Dillon 			if (vm_swapcache_data_enable == 0 ||
450e9b56058SMatthew Dillon 			    ((vp->v_flag & VSWAPCACHE) == 0 &&
451e9b56058SMatthew Dillon 			     vm_swapcache_use_chflags)) {
452b12defdcSMatthew Dillon 				vm_object_drop(object);
453b12defdcSMatthew Dillon 				vm_page_wakeup(m);
454027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
455c504e38eSMatthew Dillon 				continue;
456e9b56058SMatthew Dillon 			}
457d3070b8dSMatthew Dillon 			if (vm_swapcache_maxfilesize &&
458d3070b8dSMatthew Dillon 			    object->size >
459d3070b8dSMatthew Dillon 			    (vm_swapcache_maxfilesize >> PAGE_SHIFT)) {
460b12defdcSMatthew Dillon 				vm_object_drop(object);
461b12defdcSMatthew Dillon 				vm_page_wakeup(m);
462027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
463d3070b8dSMatthew Dillon 				continue;
464d3070b8dSMatthew Dillon 			}
465aabd5ce8SMatthew Dillon 			isblkdev = 0;
466c504e38eSMatthew Dillon 			break;
467c504e38eSMatthew Dillon 		case VCHR:
468aabd5ce8SMatthew Dillon 			/*
469bfa86281SMatthew Dillon 			 * PG_NOTMETA generically means 'don't swapcache this',
470bfa86281SMatthew Dillon 			 * and HAMMER will set this for regular data buffers
471bfa86281SMatthew Dillon 			 * (and leave it unset for meta-data buffers) as
472bfa86281SMatthew Dillon 			 * appropriate when double buffering is enabled.
473aabd5ce8SMatthew Dillon 			 */
474b12defdcSMatthew Dillon 			if (m->flags & PG_NOTMETA) {
475b12defdcSMatthew Dillon 				vm_object_drop(object);
476b12defdcSMatthew Dillon 				vm_page_wakeup(m);
477027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
478aabd5ce8SMatthew Dillon 				continue;
479b12defdcSMatthew Dillon 			}
480b12defdcSMatthew Dillon 			if (vm_swapcache_meta_enable == 0) {
481b12defdcSMatthew Dillon 				vm_object_drop(object);
482b12defdcSMatthew Dillon 				vm_page_wakeup(m);
483027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
484c504e38eSMatthew Dillon 				continue;
485b12defdcSMatthew Dillon 			}
486aabd5ce8SMatthew Dillon 			isblkdev = 1;
487c504e38eSMatthew Dillon 			break;
488c504e38eSMatthew Dillon 		default:
489b12defdcSMatthew Dillon 			vm_object_drop(object);
490b12defdcSMatthew Dillon 			vm_page_wakeup(m);
491027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
492c504e38eSMatthew Dillon 			continue;
493c504e38eSMatthew Dillon 		}
494096e95c0SMatthew Dillon 
495096e95c0SMatthew Dillon 
496096e95c0SMatthew Dillon 		/*
4973ffc7051SMatthew Dillon 		 * Assign swap and initiate I/O.
4983ffc7051SMatthew Dillon 		 *
4993ffc7051SMatthew Dillon 		 * (adjust for the --count which also occurs in the loop)
500096e95c0SMatthew Dillon 		 */
5010bf81261SMatthew Dillon 		count -= vm_swapcached_flush(m, isblkdev);
502096e95c0SMatthew Dillon 
503096e95c0SMatthew Dillon 		/*
504096e95c0SMatthew Dillon 		 * Setup for next loop using marker.
505096e95c0SMatthew Dillon 		 */
506b12defdcSMatthew Dillon 		vm_object_drop(object);
507027193ebSMatthew Dillon 		vm_page_queues_spin_lock(marker->queue);
508096e95c0SMatthew Dillon 	}
5091e5196f0SMatthew Dillon 
5101e5196f0SMatthew Dillon 	/*
511b12defdcSMatthew Dillon 	 * The marker could wind up at the end, which is ok.  If we hit the
512b12defdcSMatthew Dillon 	 * end of the list adjust the heuristic.
5131e5196f0SMatthew Dillon 	 *
5141e5196f0SMatthew Dillon 	 * Earlier inactive pages that were dirty and become clean
5151e5196f0SMatthew Dillon 	 * are typically moved to the end of PQ_INACTIVE by virtue
5161e5196f0SMatthew Dillon 	 * of vfs_vmio_release() when they become unwired from the
5171e5196f0SMatthew Dillon 	 * buffer cache.
5181e5196f0SMatthew Dillon 	 */
519027193ebSMatthew Dillon 	vm_page_queues_spin_unlock(marker->queue);
520*64949baaSMatthew Dillon 
521*64949baaSMatthew Dillon 	/*
522*64949baaSMatthew Dillon 	 * m invalid but can be used to test for NULL
523*64949baaSMatthew Dillon 	 */
524*64949baaSMatthew Dillon 	return (m == NULL);
525096e95c0SMatthew Dillon }
526096e95c0SMatthew Dillon 
527096e95c0SMatthew Dillon /*
528b12defdcSMatthew Dillon  * Flush the specified page using the swap_pager.  The page
529b12defdcSMatthew Dillon  * must be busied by the caller and its disposition will become
530b12defdcSMatthew Dillon  * the responsibility of this function.
5313ffc7051SMatthew Dillon  *
5323ffc7051SMatthew Dillon  * Try to collect surrounding pages, including pages which may
5333ffc7051SMatthew Dillon  * have already been assigned swap.  Try to cluster within a
5343ffc7051SMatthew Dillon  * contiguous aligned SMAP_META_PAGES (typ 16 x PAGE_SIZE) block
5353ffc7051SMatthew Dillon  * to match what swap_pager_putpages() can do.
5363ffc7051SMatthew Dillon  *
5373ffc7051SMatthew Dillon  * We also want to try to match against the buffer cache blocksize
5383ffc7051SMatthew Dillon  * but we don't really know what it is here.  Since the buffer cache
5393ffc7051SMatthew Dillon  * wires and unwires pages in groups the fact that we skip wired pages
5403ffc7051SMatthew Dillon  * should be sufficient.
5413ffc7051SMatthew Dillon  *
5423ffc7051SMatthew Dillon  * Returns a count of pages we might have flushed (minimum 1)
543096e95c0SMatthew Dillon  */
544096e95c0SMatthew Dillon static
5453ffc7051SMatthew Dillon int
546aabd5ce8SMatthew Dillon vm_swapcached_flush(vm_page_t m, int isblkdev)
547096e95c0SMatthew Dillon {
548096e95c0SMatthew Dillon 	vm_object_t object;
5493ffc7051SMatthew Dillon 	vm_page_t marray[SWAP_META_PAGES];
5503ffc7051SMatthew Dillon 	vm_pindex_t basei;
5513ffc7051SMatthew Dillon 	int rtvals[SWAP_META_PAGES];
5523ffc7051SMatthew Dillon 	int x;
5533ffc7051SMatthew Dillon 	int i;
5543ffc7051SMatthew Dillon 	int j;
5553ffc7051SMatthew Dillon 	int count;
556b12defdcSMatthew Dillon 	int error;
557096e95c0SMatthew Dillon 
558096e95c0SMatthew Dillon 	vm_page_io_start(m);
559096e95c0SMatthew Dillon 	vm_page_protect(m, VM_PROT_READ);
560096e95c0SMatthew Dillon 	object = m->object;
561b12defdcSMatthew Dillon 	vm_object_hold(object);
562096e95c0SMatthew Dillon 
5633ffc7051SMatthew Dillon 	/*
5643ffc7051SMatthew Dillon 	 * Try to cluster around (m), keeping in mind that the swap pager
5653ffc7051SMatthew Dillon 	 * can only do SMAP_META_PAGES worth of continguous write.
5663ffc7051SMatthew Dillon 	 */
5673ffc7051SMatthew Dillon 	x = (int)m->pindex & SWAP_META_MASK;
5683ffc7051SMatthew Dillon 	marray[x] = m;
5693ffc7051SMatthew Dillon 	basei = m->pindex;
570b12defdcSMatthew Dillon 	vm_page_wakeup(m);
5713ffc7051SMatthew Dillon 
5723ffc7051SMatthew Dillon 	for (i = x - 1; i >= 0; --i) {
573b12defdcSMatthew Dillon 		m = vm_page_lookup_busy_try(object, basei - x + i,
574b12defdcSMatthew Dillon 					    TRUE, &error);
575b12defdcSMatthew Dillon 		if (error || m == NULL)
5763ffc7051SMatthew Dillon 			break;
577b12defdcSMatthew Dillon 		if (vm_swapcache_test(m)) {
578b12defdcSMatthew Dillon 			vm_page_wakeup(m);
5793ffc7051SMatthew Dillon 			break;
580b12defdcSMatthew Dillon 		}
581b12defdcSMatthew Dillon 		if (isblkdev && (m->flags & PG_NOTMETA)) {
582b12defdcSMatthew Dillon 			vm_page_wakeup(m);
583aabd5ce8SMatthew Dillon 			break;
584b12defdcSMatthew Dillon 		}
5853ffc7051SMatthew Dillon 		vm_page_io_start(m);
5863ffc7051SMatthew Dillon 		vm_page_protect(m, VM_PROT_READ);
5873ffc7051SMatthew Dillon 		if (m->queue - m->pc == PQ_CACHE) {
5883ffc7051SMatthew Dillon 			vm_page_unqueue_nowakeup(m);
5893ffc7051SMatthew Dillon 			vm_page_deactivate(m);
5903ffc7051SMatthew Dillon 		}
5913ffc7051SMatthew Dillon 		marray[i] = m;
592b12defdcSMatthew Dillon 		vm_page_wakeup(m);
5933ffc7051SMatthew Dillon 	}
5943ffc7051SMatthew Dillon 	++i;
5953ffc7051SMatthew Dillon 
5963ffc7051SMatthew Dillon 	for (j = x + 1; j < SWAP_META_PAGES; ++j) {
597b12defdcSMatthew Dillon 		m = vm_page_lookup_busy_try(object, basei - x + j,
598b12defdcSMatthew Dillon 					    TRUE, &error);
599b12defdcSMatthew Dillon 		if (error || m == NULL)
6003ffc7051SMatthew Dillon 			break;
601b12defdcSMatthew Dillon 		if (vm_swapcache_test(m)) {
602b12defdcSMatthew Dillon 			vm_page_wakeup(m);
6033ffc7051SMatthew Dillon 			break;
604b12defdcSMatthew Dillon 		}
605b12defdcSMatthew Dillon 		if (isblkdev && (m->flags & PG_NOTMETA)) {
606b12defdcSMatthew Dillon 			vm_page_wakeup(m);
607aabd5ce8SMatthew Dillon 			break;
608b12defdcSMatthew Dillon 		}
6093ffc7051SMatthew Dillon 		vm_page_io_start(m);
6103ffc7051SMatthew Dillon 		vm_page_protect(m, VM_PROT_READ);
6113ffc7051SMatthew Dillon 		if (m->queue - m->pc == PQ_CACHE) {
6123ffc7051SMatthew Dillon 			vm_page_unqueue_nowakeup(m);
6133ffc7051SMatthew Dillon 			vm_page_deactivate(m);
6143ffc7051SMatthew Dillon 		}
6153ffc7051SMatthew Dillon 		marray[j] = m;
616b12defdcSMatthew Dillon 		vm_page_wakeup(m);
6173ffc7051SMatthew Dillon 	}
6183ffc7051SMatthew Dillon 
6193ffc7051SMatthew Dillon 	count = j - i;
6203ffc7051SMatthew Dillon 	vm_object_pip_add(object, count);
6213ffc7051SMatthew Dillon 	swap_pager_putpages(object, marray + i, count, FALSE, rtvals + i);
6223ffc7051SMatthew Dillon 	vm_swapcache_write_count += count * PAGE_SIZE;
6233ffc7051SMatthew Dillon 	vm_swapcache_curburst -= count * PAGE_SIZE;
6243ffc7051SMatthew Dillon 
6253ffc7051SMatthew Dillon 	while (i < j) {
6263ffc7051SMatthew Dillon 		if (rtvals[i] != VM_PAGER_PEND) {
627b12defdcSMatthew Dillon 			vm_page_busy_wait(marray[i], FALSE, "swppgfd");
6283ffc7051SMatthew Dillon 			vm_page_io_finish(marray[i]);
629b12defdcSMatthew Dillon 			vm_page_wakeup(marray[i]);
630096e95c0SMatthew Dillon 			vm_object_pip_wakeup(object);
631096e95c0SMatthew Dillon 		}
6323ffc7051SMatthew Dillon 		++i;
6333ffc7051SMatthew Dillon 	}
634b12defdcSMatthew Dillon 	vm_object_drop(object);
6353ffc7051SMatthew Dillon 	return(count);
636096e95c0SMatthew Dillon }
63700a3fdcaSMatthew Dillon 
6383ffc7051SMatthew Dillon /*
6393ffc7051SMatthew Dillon  * Test whether a VM page is suitable for writing to the swapcache.
6403ffc7051SMatthew Dillon  * Does not test m->queue, PG_MARKER, or PG_SWAPPED.
6413ffc7051SMatthew Dillon  *
6423ffc7051SMatthew Dillon  * Returns 0 on success, 1 on failure
6433ffc7051SMatthew Dillon  */
6443ffc7051SMatthew Dillon static int
6453ffc7051SMatthew Dillon vm_swapcache_test(vm_page_t m)
6463ffc7051SMatthew Dillon {
6473ffc7051SMatthew Dillon 	vm_object_t object;
6483ffc7051SMatthew Dillon 
649b12defdcSMatthew Dillon 	if (m->flags & PG_UNMANAGED)
6503ffc7051SMatthew Dillon 		return(1);
651b12defdcSMatthew Dillon 	if (m->hold_count || m->wire_count)
6523ffc7051SMatthew Dillon 		return(1);
6533ffc7051SMatthew Dillon 	if (m->valid != VM_PAGE_BITS_ALL)
6543ffc7051SMatthew Dillon 		return(1);
6553ffc7051SMatthew Dillon 	if (m->dirty & m->valid)
6563ffc7051SMatthew Dillon 		return(1);
6573ffc7051SMatthew Dillon 	if ((object = m->object) == NULL)
6583ffc7051SMatthew Dillon 		return(1);
6593ffc7051SMatthew Dillon 	if (object->type != OBJT_VNODE ||
6603ffc7051SMatthew Dillon 	    (object->flags & OBJ_DEAD)) {
6613ffc7051SMatthew Dillon 		return(1);
6623ffc7051SMatthew Dillon 	}
6633ffc7051SMatthew Dillon 	vm_page_test_dirty(m);
6643ffc7051SMatthew Dillon 	if (m->dirty & m->valid)
6653ffc7051SMatthew Dillon 		return(1);
6663ffc7051SMatthew Dillon 	return(0);
6673ffc7051SMatthew Dillon }
6683ffc7051SMatthew Dillon 
6693ffc7051SMatthew Dillon /*
670f5f6d247SMatthew Dillon  * Cleaning pass.
671f5f6d247SMatthew Dillon  *
672f5f6d247SMatthew Dillon  * We clean whole objects up to 16MB
6733ffc7051SMatthew Dillon  */
67400a3fdcaSMatthew Dillon static
67500a3fdcaSMatthew Dillon void
67600a3fdcaSMatthew Dillon vm_swapcache_cleaning(vm_object_t marker)
67700a3fdcaSMatthew Dillon {
67800a3fdcaSMatthew Dillon 	vm_object_t object;
67900a3fdcaSMatthew Dillon 	struct vnode *vp;
68000a3fdcaSMatthew Dillon 	int count;
6810bf81261SMatthew Dillon 	int scount;
68200a3fdcaSMatthew Dillon 	int n;
68300a3fdcaSMatthew Dillon 
68400a3fdcaSMatthew Dillon 	count = vm_swapcache_maxlaunder;
6850bf81261SMatthew Dillon 	scount = vm_swapcache_maxscan;
68600a3fdcaSMatthew Dillon 
68700a3fdcaSMatthew Dillon 	/*
68800a3fdcaSMatthew Dillon 	 * Look for vnode objects
68900a3fdcaSMatthew Dillon 	 */
6902de4f77eSMatthew Dillon 	lwkt_gettoken(&vmobj_token);
6912de4f77eSMatthew Dillon 
692f5f6d247SMatthew Dillon 	while ((object = TAILQ_NEXT(marker, object_list)) != NULL) {
6932f2d9e58SVenkatesh Srinivas 		/*
694f5f6d247SMatthew Dillon 		 * We have to skip markers.  We cannot hold/drop marker
695f5f6d247SMatthew Dillon 		 * objects!
6962f2d9e58SVenkatesh Srinivas 		 */
697f5f6d247SMatthew Dillon 		if (object->type == OBJT_MARKER) {
698f5f6d247SMatthew Dillon 			vm_swapcache_movemarker(marker, object);
69900a3fdcaSMatthew Dillon 			continue;
7002f2d9e58SVenkatesh Srinivas 		}
70100a3fdcaSMatthew Dillon 
70200a3fdcaSMatthew Dillon 		/*
703f5f6d247SMatthew Dillon 		 * Safety, or in case there are millions of VM objects
704f5f6d247SMatthew Dillon 		 * without swapcache backing.
70500a3fdcaSMatthew Dillon 		 */
7060bf81261SMatthew Dillon 		if (--scount <= 0)
707f5f6d247SMatthew Dillon 			break;
70800a3fdcaSMatthew Dillon 
70900a3fdcaSMatthew Dillon 		/*
710f5f6d247SMatthew Dillon 		 * We must hold the object before potentially yielding.
71100a3fdcaSMatthew Dillon 		 */
712f5f6d247SMatthew Dillon 		vm_object_hold(object);
713f5f6d247SMatthew Dillon 		lwkt_yield();
714f5f6d247SMatthew Dillon 
715f5f6d247SMatthew Dillon 		/*
716f5f6d247SMatthew Dillon 		 * Only operate on live VNODE objects that are either
717f5f6d247SMatthew Dillon 		 * VREG or VCHR (VCHR for meta-data).
718f5f6d247SMatthew Dillon 		 */
719f5f6d247SMatthew Dillon 		if ((object->type != OBJT_VNODE) ||
720f5f6d247SMatthew Dillon 		    ((object->flags & OBJ_DEAD) ||
721f5f6d247SMatthew Dillon 		     object->swblock_count == 0) ||
722f5f6d247SMatthew Dillon 		    ((vp = object->handle) == NULL) ||
723f5f6d247SMatthew Dillon 		    (vp->v_type != VREG && vp->v_type != VCHR)) {
724f5f6d247SMatthew Dillon 			vm_object_drop(object);
725f5f6d247SMatthew Dillon 			/* object may be invalid now */
726f5f6d247SMatthew Dillon 			vm_swapcache_movemarker(marker, object);
727f5f6d247SMatthew Dillon 			continue;
728f5f6d247SMatthew Dillon 		}
729f5f6d247SMatthew Dillon 
730f5f6d247SMatthew Dillon 		/*
731f5f6d247SMatthew Dillon 		 * Reset the object pindex stored in the marker if the
732f5f6d247SMatthew Dillon 		 * working object has changed.
733f5f6d247SMatthew Dillon 		 */
734f5f6d247SMatthew Dillon 		if (marker->backing_object != object) {
735f5f6d247SMatthew Dillon 			marker->size = 0;
736f5f6d247SMatthew Dillon 			marker->backing_object_offset = 0;
737f5f6d247SMatthew Dillon 			marker->backing_object = object;
738f5f6d247SMatthew Dillon 		}
73900a3fdcaSMatthew Dillon 
74000a3fdcaSMatthew Dillon 		/*
74100a3fdcaSMatthew Dillon 		 * Look for swblocks starting at our iterator.
74200a3fdcaSMatthew Dillon 		 *
74300a3fdcaSMatthew Dillon 		 * The swap_pager_condfree() function attempts to free
74400a3fdcaSMatthew Dillon 		 * swap space starting at the specified index.  The index
74500a3fdcaSMatthew Dillon 		 * will be updated on return.  The function will return
74600a3fdcaSMatthew Dillon 		 * a scan factor (NOT the number of blocks freed).
74700a3fdcaSMatthew Dillon 		 *
74800a3fdcaSMatthew Dillon 		 * If it must cut its scan of the object short due to an
74900a3fdcaSMatthew Dillon 		 * excessive number of swblocks, or is able to free the
75000a3fdcaSMatthew Dillon 		 * requested number of blocks, it will return n >= count
75100a3fdcaSMatthew Dillon 		 * and we break and pick it back up on a future attempt.
752f5f6d247SMatthew Dillon 		 *
753f5f6d247SMatthew Dillon 		 * Scan the object linearly and try to batch large sets of
754f5f6d247SMatthew Dillon 		 * blocks that are likely to clean out entire swap radix
755f5f6d247SMatthew Dillon 		 * tree leafs.
75600a3fdcaSMatthew Dillon 		 */
757739be60bSMatthew Dillon 		lwkt_token_swap();
75808fb7a9dSMatthew Dillon 		lwkt_reltoken(&vmobj_token);
75927b6ee03SMatthew Dillon 
760f5f6d247SMatthew Dillon 		n = swap_pager_condfree(object, &marker->size,
761f5f6d247SMatthew Dillon 				    (count + SWAP_META_MASK) & ~SWAP_META_MASK);
7622f2d9e58SVenkatesh Srinivas 
763f5f6d247SMatthew Dillon 		vm_object_drop(object);		/* object may be invalid now */
76427b6ee03SMatthew Dillon 		lwkt_gettoken(&vmobj_token);
7652f2d9e58SVenkatesh Srinivas 
76600a3fdcaSMatthew Dillon 		/*
767f5f6d247SMatthew Dillon 		 * If we have exhausted the object or deleted our per-pass
768f5f6d247SMatthew Dillon 		 * page limit then move us to the next object.  Note that
769f5f6d247SMatthew Dillon 		 * the current object may no longer be on the vm_object_list.
77000a3fdcaSMatthew Dillon 		 */
771f5f6d247SMatthew Dillon 		if (n <= 0 ||
772f5f6d247SMatthew Dillon 		    marker->backing_object_offset > vm_swapcache_cleanperobj) {
773f5f6d247SMatthew Dillon 			vm_swapcache_movemarker(marker, object);
77400a3fdcaSMatthew Dillon 		}
77500a3fdcaSMatthew Dillon 
77600a3fdcaSMatthew Dillon 		/*
777f5f6d247SMatthew Dillon 		 * If we have exhausted our max-launder stop for now.
77800a3fdcaSMatthew Dillon 		 */
779f5f6d247SMatthew Dillon 		count -= n;
780f5f6d247SMatthew Dillon 		marker->backing_object_offset += n * PAGE_SIZE;
781f5f6d247SMatthew Dillon 		if (count < 0)
782f5f6d247SMatthew Dillon 			break;
783f5f6d247SMatthew Dillon 	}
7847a175765SMatthew Dillon 
7857a175765SMatthew Dillon 	/*
7867a175765SMatthew Dillon 	 * If we wound up at the end of the list this will move the
7877a175765SMatthew Dillon 	 * marker back to the beginning.
7887a175765SMatthew Dillon 	 */
7897a175765SMatthew Dillon 	if (object == NULL)
7907a175765SMatthew Dillon 		vm_swapcache_movemarker(marker, NULL);
7917a175765SMatthew Dillon 
7922de4f77eSMatthew Dillon 	lwkt_reltoken(&vmobj_token);
79300a3fdcaSMatthew Dillon }
794f5f6d247SMatthew Dillon 
795f5f6d247SMatthew Dillon /*
796f5f6d247SMatthew Dillon  * Move the marker past the current object.  Object can be stale, but we
797f5f6d247SMatthew Dillon  * still need it to determine if the marker has to be moved.  If the object
798f5f6d247SMatthew Dillon  * is still the 'current object' (object after the marker), we hop-scotch
799f5f6d247SMatthew Dillon  * the marker past it.
800f5f6d247SMatthew Dillon  */
801f5f6d247SMatthew Dillon static void
802f5f6d247SMatthew Dillon vm_swapcache_movemarker(vm_object_t marker, vm_object_t object)
803f5f6d247SMatthew Dillon {
804f5f6d247SMatthew Dillon 	if (TAILQ_NEXT(marker, object_list) == object) {
805f5f6d247SMatthew Dillon 		TAILQ_REMOVE(&vm_object_list, marker, object_list);
806f5f6d247SMatthew Dillon 		if (object) {
807f5f6d247SMatthew Dillon 			TAILQ_INSERT_AFTER(&vm_object_list, object,
808f5f6d247SMatthew Dillon 					   marker, object_list);
809f5f6d247SMatthew Dillon 		} else {
810f5f6d247SMatthew Dillon 			TAILQ_INSERT_HEAD(&vm_object_list,
811f5f6d247SMatthew Dillon 					  marker, object_list);
812f5f6d247SMatthew Dillon 		}
813f5f6d247SMatthew Dillon 	}
814f5f6d247SMatthew Dillon }
815