xref: /dragonfly/sys/vm/vm_swapcache.c (revision 831a8507)
1096e95c0SMatthew Dillon /*
28e7c4729SMatthew Dillon  * (MPSAFE)
38e7c4729SMatthew Dillon  *
49de48eadSMatthew Dillon  * Copyright (c) 2010,2019 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 
78b12defdcSMatthew Dillon #include <sys/spinlock2.h>
79096e95c0SMatthew Dillon #include <vm/vm_page2.h>
80096e95c0SMatthew Dillon 
819de48eadSMatthew Dillon struct swmarker {
829de48eadSMatthew Dillon 	struct vm_object dummy_obj;
839de48eadSMatthew Dillon 	struct vm_object *save_obj;
849de48eadSMatthew Dillon 	vm_ooffset_t save_off;
859de48eadSMatthew Dillon };
869de48eadSMatthew Dillon 
879de48eadSMatthew Dillon typedef struct swmarker swmarker_t;
889de48eadSMatthew Dillon 
89096e95c0SMatthew Dillon /* the kernel process "vm_pageout"*/
90aabd5ce8SMatthew Dillon static int vm_swapcached_flush (vm_page_t m, int isblkdev);
913ffc7051SMatthew Dillon static int vm_swapcache_test(vm_page_t m);
9264949baaSMatthew Dillon static int vm_swapcache_writing_heuristic(void);
9364949baaSMatthew Dillon static int vm_swapcache_writing(vm_page_t marker, int count, int scount);
949de48eadSMatthew Dillon static void vm_swapcache_cleaning(swmarker_t *marker,
95fde6be6aSMatthew Dillon 			struct vm_object_hash **swindexp);
969de48eadSMatthew Dillon static void vm_swapcache_movemarker(swmarker_t *marker,
97fde6be6aSMatthew Dillon 			struct vm_object_hash *swindex, vm_object_t object);
98096e95c0SMatthew Dillon struct thread *swapcached_thread;
99096e95c0SMatthew Dillon 
100096e95c0SMatthew Dillon SYSCTL_NODE(_vm, OID_AUTO, swapcache, CTLFLAG_RW, NULL, NULL);
101096e95c0SMatthew Dillon 
102c504e38eSMatthew Dillon int vm_swapcache_read_enable;
103b396bb03SMatthew Dillon static long vm_swapcache_wtrigger;
104096e95c0SMatthew Dillon static int vm_swapcache_sleep;
10564949baaSMatthew Dillon static int vm_swapcache_maxscan = PQ_L2_SIZE * 8;
10664949baaSMatthew Dillon static int vm_swapcache_maxlaunder = PQ_L2_SIZE * 4;
107096e95c0SMatthew Dillon static int vm_swapcache_data_enable = 0;
108096e95c0SMatthew Dillon static int vm_swapcache_meta_enable = 0;
109e9b56058SMatthew Dillon static int vm_swapcache_maxswappct = 75;
110e527fb6bSMatthew Dillon static int vm_swapcache_hysteresis;
11164949baaSMatthew Dillon static int vm_swapcache_min_hysteresis;
112d4d9221dSMatthew Dillon int vm_swapcache_use_chflags = 0;	/* require chflags cache */
1133ffc7051SMatthew Dillon static int64_t vm_swapcache_minburst = 10000000LL;	/* 10MB */
1143ffc7051SMatthew Dillon static int64_t vm_swapcache_curburst = 4000000000LL;	/* 4G after boot */
1153ffc7051SMatthew Dillon static int64_t vm_swapcache_maxburst = 2000000000LL;	/* 2G nominal max */
1163ffc7051SMatthew Dillon static int64_t vm_swapcache_accrate = 100000LL;		/* 100K/s */
117096e95c0SMatthew Dillon static int64_t vm_swapcache_write_count;
1183ffc7051SMatthew Dillon static int64_t vm_swapcache_maxfilesize;
119f5f6d247SMatthew Dillon static int64_t vm_swapcache_cleanperobj = 16*1024*1024;
120096e95c0SMatthew Dillon 
121096e95c0SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, maxlaunder,
122096e95c0SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxlaunder, 0, "");
1230bf81261SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, maxscan,
1240bf81261SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxscan, 0, "");
125c504e38eSMatthew Dillon 
126096e95c0SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, data_enable,
127096e95c0SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_data_enable, 0, "");
128096e95c0SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, meta_enable,
129096e95c0SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_meta_enable, 0, "");
130c504e38eSMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, read_enable,
131c504e38eSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_read_enable, 0, "");
132e9b56058SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, maxswappct,
133e9b56058SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxswappct, 0, "");
134e527fb6bSMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, hysteresis,
13564949baaSMatthew Dillon 	CTLFLAG_RD, &vm_swapcache_hysteresis, 0, "");
13664949baaSMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, min_hysteresis,
13764949baaSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_min_hysteresis, 0, "");
138e9b56058SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, use_chflags,
139e9b56058SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_use_chflags, 0, "");
140c504e38eSMatthew Dillon 
1413ffc7051SMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, minburst,
1423ffc7051SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_minburst, 0, "");
143c504e38eSMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, curburst,
144c504e38eSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_curburst, 0, "");
145c504e38eSMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxburst,
146c504e38eSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxburst, 0, "");
1473ffc7051SMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxfilesize,
1483ffc7051SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxfilesize, 0, "");
149c504e38eSMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, accrate,
150c504e38eSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_accrate, 0, "");
151096e95c0SMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, write_count,
152096e95c0SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_write_count, 0, "");
153f5f6d247SMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, cleanperobj,
154f5f6d247SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_cleanperobj, 0, "");
155096e95c0SMatthew Dillon 
156e9b56058SMatthew Dillon #define SWAPMAX(adj)	\
157e9b56058SMatthew Dillon 	((int64_t)vm_swap_max * (vm_swapcache_maxswappct + (adj)) / 100)
158e9b56058SMatthew Dillon 
159096e95c0SMatthew Dillon /*
160497524bfSMatthew Dillon  * When shutting down the machine we want to stop swapcache operation
161497524bfSMatthew Dillon  * immediately so swap is not accessed after devices have been shuttered.
162497524bfSMatthew Dillon  */
163497524bfSMatthew Dillon static void
164497524bfSMatthew Dillon shutdown_swapcache(void *arg __unused)
165497524bfSMatthew Dillon {
166497524bfSMatthew Dillon 	vm_swapcache_read_enable = 0;
167497524bfSMatthew Dillon 	vm_swapcache_data_enable = 0;
168497524bfSMatthew Dillon 	vm_swapcache_meta_enable = 0;
169497524bfSMatthew Dillon 	wakeup(&vm_swapcache_sleep);	/* shortcut 5-second wait */
170497524bfSMatthew Dillon }
171497524bfSMatthew Dillon 
172497524bfSMatthew Dillon /*
173096e95c0SMatthew Dillon  * vm_swapcached is the high level pageout daemon.
1748e7c4729SMatthew Dillon  *
1758e7c4729SMatthew Dillon  * No requirements.
176096e95c0SMatthew Dillon  */
177096e95c0SMatthew Dillon static void
178cd8ab232SMatthew Dillon vm_swapcached_thread(void)
179096e95c0SMatthew Dillon {
18000a3fdcaSMatthew Dillon 	enum { SWAPC_WRITING, SWAPC_CLEANING } state = SWAPC_WRITING;
1813ffc7051SMatthew Dillon 	enum { SWAPB_BURSTING, SWAPB_RECOVERING } burst = SWAPB_BURSTING;
18251c99c61SMatthew Dillon 	static struct vm_page page_marker[PQ_L2_SIZE];
1839de48eadSMatthew Dillon 	static swmarker_t swmarker;
184fde6be6aSMatthew Dillon 	static struct vm_object_hash *swindex;
185027193ebSMatthew Dillon 	int q;
186096e95c0SMatthew Dillon 
187096e95c0SMatthew Dillon 	/*
188096e95c0SMatthew Dillon 	 * Thread setup
189096e95c0SMatthew Dillon 	 */
190096e95c0SMatthew Dillon 	curthread->td_flags |= TDF_SYSTHREAD;
191497524bfSMatthew Dillon 	EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc,
192497524bfSMatthew Dillon 			      swapcached_thread, SHUTDOWN_PRI_FIRST);
193497524bfSMatthew Dillon 	EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_swapcache,
194497524bfSMatthew Dillon 			      NULL, SHUTDOWN_PRI_SECOND);
195096e95c0SMatthew Dillon 
196096e95c0SMatthew Dillon 	/*
19700a3fdcaSMatthew Dillon 	 * Initialize our marker for the inactive scan (SWAPC_WRITING)
198096e95c0SMatthew Dillon 	 */
19900a3fdcaSMatthew Dillon 	bzero(&page_marker, sizeof(page_marker));
20051c99c61SMatthew Dillon 	for (q = 0; q < PQ_L2_SIZE; ++q) {
201bc0aa189SMatthew Dillon 		page_marker[q].flags = PG_FICTITIOUS | PG_MARKER;
202bc0aa189SMatthew Dillon 		page_marker[q].busy_count = PBUSY_LOCKED;
203027193ebSMatthew Dillon 		page_marker[q].queue = PQ_INACTIVE + q;
204027193ebSMatthew Dillon 		page_marker[q].pc = q;
205027193ebSMatthew Dillon 		page_marker[q].wire_count = 1;
206027193ebSMatthew Dillon 		vm_page_queues_spin_lock(PQ_INACTIVE + q);
207027193ebSMatthew Dillon 		TAILQ_INSERT_HEAD(
208027193ebSMatthew Dillon 			&vm_page_queues[PQ_INACTIVE + q].pl,
209027193ebSMatthew Dillon 			&page_marker[q], pageq);
210027193ebSMatthew Dillon 		vm_page_queues_spin_unlock(PQ_INACTIVE + q);
211027193ebSMatthew Dillon 	}
212b12defdcSMatthew Dillon 
21364949baaSMatthew Dillon 	vm_swapcache_min_hysteresis = 1024;
21464949baaSMatthew Dillon 	vm_swapcache_hysteresis = vm_swapcache_min_hysteresis;
215b396bb03SMatthew Dillon 	vm_swapcache_wtrigger = -vm_swapcache_hysteresis;
216096e95c0SMatthew Dillon 
21700a3fdcaSMatthew Dillon 	/*
21800a3fdcaSMatthew Dillon 	 * Initialize our marker for the vm_object scan (SWAPC_CLEANING)
21900a3fdcaSMatthew Dillon 	 */
2207b00fbb4SMatthew Dillon 	bzero(&swmarker, sizeof(swmarker));
2219de48eadSMatthew Dillon 	swmarker.dummy_obj.type = OBJT_MARKER;
222fde6be6aSMatthew Dillon 	swindex = &vm_object_hash[0];
223fde6be6aSMatthew Dillon 	lwkt_gettoken(&swindex->token);
2245b329e62SMatthew Dillon 	TAILQ_INSERT_HEAD(&swindex->list, &swmarker.dummy_obj, object_entry);
225fde6be6aSMatthew Dillon 	lwkt_reltoken(&swindex->token);
226096e95c0SMatthew Dillon 
227096e95c0SMatthew Dillon 	for (;;) {
22864949baaSMatthew Dillon 		int reached_end;
22964949baaSMatthew Dillon 		int scount;
23064949baaSMatthew Dillon 		int count;
23164949baaSMatthew Dillon 
232096e95c0SMatthew Dillon 		/*
233497524bfSMatthew Dillon 		 * Handle shutdown
234497524bfSMatthew Dillon 		 */
235497524bfSMatthew Dillon 		kproc_suspend_loop();
236497524bfSMatthew Dillon 
237497524bfSMatthew Dillon 		/*
2383da46bd7SMatthew Dillon 		 * Check every 5 seconds when not enabled or if no swap
2393da46bd7SMatthew Dillon 		 * is present.
240096e95c0SMatthew Dillon 		 */
2413da46bd7SMatthew Dillon 		if ((vm_swapcache_data_enable == 0 &&
242c8ddd5d8SMatthew Dillon 		     vm_swapcache_meta_enable == 0 &&
243c8ddd5d8SMatthew Dillon 		     vm_swap_cache_use <= SWAPMAX(0)) ||
2443da46bd7SMatthew Dillon 		    vm_swap_max == 0) {
245096e95c0SMatthew Dillon 			tsleep(&vm_swapcache_sleep, 0, "csleep", hz * 5);
246096e95c0SMatthew Dillon 			continue;
247096e95c0SMatthew Dillon 		}
248c504e38eSMatthew Dillon 
249c504e38eSMatthew Dillon 		/*
2503da46bd7SMatthew Dillon 		 * Polling rate when enabled is approximately 10 hz.
251c504e38eSMatthew Dillon 		 */
252c504e38eSMatthew Dillon 		tsleep(&vm_swapcache_sleep, 0, "csleep", hz / 10);
25300a3fdcaSMatthew Dillon 
25400a3fdcaSMatthew Dillon 		/*
25500a3fdcaSMatthew Dillon 		 * State hysteresis.  Generate write activity up to 75% of
25600a3fdcaSMatthew Dillon 		 * swap, then clean out swap assignments down to 70%, then
25700a3fdcaSMatthew Dillon 		 * repeat.
25800a3fdcaSMatthew Dillon 		 */
25900a3fdcaSMatthew Dillon 		if (state == SWAPC_WRITING) {
260e9b56058SMatthew Dillon 			if (vm_swap_cache_use > SWAPMAX(0))
26100a3fdcaSMatthew Dillon 				state = SWAPC_CLEANING;
26200a3fdcaSMatthew Dillon 		} else {
26308fb7a9dSMatthew Dillon 			if (vm_swap_cache_use < SWAPMAX(-10))
26400a3fdcaSMatthew Dillon 				state = SWAPC_WRITING;
26500a3fdcaSMatthew Dillon 		}
26600a3fdcaSMatthew Dillon 
26700a3fdcaSMatthew Dillon 		/*
26800a3fdcaSMatthew Dillon 		 * We are allowed to continue accumulating burst value
2693ffc7051SMatthew Dillon 		 * in either state.  Allow the user to set curburst > maxburst
2703ffc7051SMatthew Dillon 		 * for the initial load-in.
27100a3fdcaSMatthew Dillon 		 */
2723ffc7051SMatthew Dillon 		if (vm_swapcache_curburst < vm_swapcache_maxburst) {
273c504e38eSMatthew Dillon 			vm_swapcache_curburst += vm_swapcache_accrate / 10;
274c504e38eSMatthew Dillon 			if (vm_swapcache_curburst > vm_swapcache_maxburst)
275c504e38eSMatthew Dillon 				vm_swapcache_curburst = vm_swapcache_maxburst;
2763ffc7051SMatthew Dillon 		}
277c504e38eSMatthew Dillon 
278c504e38eSMatthew Dillon 		/*
27900a3fdcaSMatthew Dillon 		 * We don't want to nickle-and-dime the scan as that will
28000a3fdcaSMatthew Dillon 		 * create unnecessary fragmentation.  The minimum burst
28100a3fdcaSMatthew Dillon 		 * is one-seconds worth of accumulation.
282c504e38eSMatthew Dillon 		 */
28364949baaSMatthew Dillon 		if (state != SWAPC_WRITING) {
2847b00fbb4SMatthew Dillon 			vm_swapcache_cleaning(&swmarker, &swindex);
28564949baaSMatthew Dillon 			continue;
28664949baaSMatthew Dillon 		}
28764949baaSMatthew Dillon 		if (vm_swapcache_curburst < vm_swapcache_accrate)
28864949baaSMatthew Dillon 			continue;
28964949baaSMatthew Dillon 
29064949baaSMatthew Dillon 		reached_end = 0;
29164949baaSMatthew Dillon 		count = vm_swapcache_maxlaunder / PQ_L2_SIZE + 2;
29264949baaSMatthew Dillon 		scount = vm_swapcache_maxscan / PQ_L2_SIZE + 2;
29364949baaSMatthew Dillon 
2943ffc7051SMatthew Dillon 		if (burst == SWAPB_BURSTING) {
29564949baaSMatthew Dillon 			if (vm_swapcache_writing_heuristic()) {
29651c99c61SMatthew Dillon 				for (q = 0; q < PQ_L2_SIZE; ++q) {
29764949baaSMatthew Dillon 					reached_end +=
298027193ebSMatthew Dillon 						vm_swapcache_writing(
29964949baaSMatthew Dillon 							&page_marker[q],
30064949baaSMatthew Dillon 							count,
30164949baaSMatthew Dillon 							scount);
30264949baaSMatthew Dillon 				}
303027193ebSMatthew Dillon 			}
3043ffc7051SMatthew Dillon 			if (vm_swapcache_curburst <= 0)
3053ffc7051SMatthew Dillon 				burst = SWAPB_RECOVERING;
30664949baaSMatthew Dillon 		} else if (vm_swapcache_curburst > vm_swapcache_minburst) {
30764949baaSMatthew Dillon 			if (vm_swapcache_writing_heuristic()) {
30851c99c61SMatthew Dillon 				for (q = 0; q < PQ_L2_SIZE; ++q) {
30964949baaSMatthew Dillon 					reached_end +=
310027193ebSMatthew Dillon 						vm_swapcache_writing(
31164949baaSMatthew Dillon 							&page_marker[q],
31264949baaSMatthew Dillon 							count,
31364949baaSMatthew Dillon 							scount);
31464949baaSMatthew Dillon 				}
315027193ebSMatthew Dillon 			}
3163ffc7051SMatthew Dillon 			burst = SWAPB_BURSTING;
3173ffc7051SMatthew Dillon 		}
31864949baaSMatthew Dillon 		if (reached_end == PQ_L2_SIZE) {
319b396bb03SMatthew Dillon 			vm_swapcache_wtrigger = -vm_swapcache_hysteresis;
32000a3fdcaSMatthew Dillon 		}
32100a3fdcaSMatthew Dillon 	}
322eccc8ca1SMatthew Dillon 
323eccc8ca1SMatthew Dillon 	/*
324eccc8ca1SMatthew Dillon 	 * Cleanup (NOT REACHED)
325eccc8ca1SMatthew Dillon 	 */
32651c99c61SMatthew Dillon 	for (q = 0; q < PQ_L2_SIZE; ++q) {
327027193ebSMatthew Dillon 		vm_page_queues_spin_lock(PQ_INACTIVE + q);
328027193ebSMatthew Dillon 		TAILQ_REMOVE(
329027193ebSMatthew Dillon 			&vm_page_queues[PQ_INACTIVE + q].pl,
330027193ebSMatthew Dillon 			&page_marker[q], pageq);
331027193ebSMatthew Dillon 		vm_page_queues_spin_unlock(PQ_INACTIVE + q);
332027193ebSMatthew Dillon 	}
333eccc8ca1SMatthew Dillon 
334fde6be6aSMatthew Dillon 	lwkt_gettoken(&swindex->token);
3355b329e62SMatthew Dillon 	TAILQ_REMOVE(&swindex->list, &swmarker.dummy_obj, object_entry);
336fde6be6aSMatthew Dillon 	lwkt_reltoken(&swindex->token);
33700a3fdcaSMatthew Dillon }
338096e95c0SMatthew Dillon 
339cd8ab232SMatthew Dillon static struct kproc_desc swpc_kp = {
340cd8ab232SMatthew Dillon 	"swapcached",
341cd8ab232SMatthew Dillon 	vm_swapcached_thread,
342cd8ab232SMatthew Dillon 	&swapcached_thread
343cd8ab232SMatthew Dillon };
344f3f3eadbSSascha Wildner SYSINIT(swapcached, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, &swpc_kp);
345cd8ab232SMatthew Dillon 
346096e95c0SMatthew Dillon /*
347fdc53cc7SMatthew Dillon  * Deal with an overflow of the heuristic counter or if the user
348fdc53cc7SMatthew Dillon  * manually changes the hysteresis.
349fdc53cc7SMatthew Dillon  *
350e527fb6bSMatthew Dillon  * Try to avoid small incremental pageouts by waiting for enough
351e527fb6bSMatthew Dillon  * pages to buildup in the inactive queue to hopefully get a good
352e527fb6bSMatthew Dillon  * burst in.  This heuristic is bumped by the VM system and reset
353e527fb6bSMatthew Dillon  * when our scan hits the end of the queue.
35464949baaSMatthew Dillon  *
35564949baaSMatthew Dillon  * Return TRUE if we need to take a writing pass.
356e527fb6bSMatthew Dillon  */
35764949baaSMatthew Dillon static int
35864949baaSMatthew Dillon vm_swapcache_writing_heuristic(void)
35964949baaSMatthew Dillon {
36064949baaSMatthew Dillon 	int hyst;
361b396bb03SMatthew Dillon 	int q;
362b396bb03SMatthew Dillon 	long adds;
36364949baaSMatthew Dillon 
36464949baaSMatthew Dillon 	hyst = vmstats.v_inactive_count / 4;
36564949baaSMatthew Dillon 	if (hyst < vm_swapcache_min_hysteresis)
36664949baaSMatthew Dillon 		hyst = vm_swapcache_min_hysteresis;
36764949baaSMatthew Dillon 	cpu_ccfence();
36864949baaSMatthew Dillon 	vm_swapcache_hysteresis = hyst;
36964949baaSMatthew Dillon 
370b396bb03SMatthew Dillon 	adds = 0;
371b396bb03SMatthew Dillon 	for (q = PQ_INACTIVE; q < PQ_INACTIVE + PQ_L2_SIZE; ++q) {
372b396bb03SMatthew Dillon 		adds += atomic_swap_long(&vm_page_queues[q].adds, 0);
373b396bb03SMatthew Dillon 	}
374b396bb03SMatthew Dillon 	vm_swapcache_wtrigger += adds;
375b396bb03SMatthew Dillon 	if (vm_swapcache_wtrigger < -hyst)
376b396bb03SMatthew Dillon 		vm_swapcache_wtrigger = -hyst;
377b396bb03SMatthew Dillon 	return (vm_swapcache_wtrigger >= 0);
37864949baaSMatthew Dillon }
37964949baaSMatthew Dillon 
38064949baaSMatthew Dillon /*
38164949baaSMatthew Dillon  * Take a writing pass on one of the inactive queues, return non-zero if
38264949baaSMatthew Dillon  * we hit the end of the queue.
38364949baaSMatthew Dillon  */
38464949baaSMatthew Dillon static int
38564949baaSMatthew Dillon vm_swapcache_writing(vm_page_t marker, int count, int scount)
38664949baaSMatthew Dillon {
38764949baaSMatthew Dillon 	vm_object_t object;
38864949baaSMatthew Dillon 	struct vnode *vp;
38964949baaSMatthew Dillon 	vm_page_t m;
39064949baaSMatthew Dillon 	int isblkdev;
391e527fb6bSMatthew Dillon 
392e527fb6bSMatthew Dillon 	/*
393096e95c0SMatthew Dillon 	 * Scan the inactive queue from our marker to locate
394096e95c0SMatthew Dillon 	 * suitable pages to push to the swap cache.
395096e95c0SMatthew Dillon 	 *
396096e95c0SMatthew Dillon 	 * We are looking for clean vnode-backed pages.
397096e95c0SMatthew Dillon 	 */
398027193ebSMatthew Dillon 	vm_page_queues_spin_lock(marker->queue);
3990bf81261SMatthew Dillon 	while ((m = TAILQ_NEXT(marker, pageq)) != NULL &&
4000bf81261SMatthew Dillon 	       count > 0 && scount-- > 0) {
401027193ebSMatthew Dillon 		KKASSERT(m->queue == marker->queue);
402b12defdcSMatthew Dillon 
4031de864f0SMatthew Dillon 		/*
4041de864f0SMatthew Dillon 		 * Stop using swap if paniced, dumping, or dumped.
4051de864f0SMatthew Dillon 		 * Don't try to write if our curburst has been exhausted.
4061de864f0SMatthew Dillon 		 */
4071de864f0SMatthew Dillon 		if (panicstr || dumping)
4081de864f0SMatthew Dillon 			break;
409b12defdcSMatthew Dillon 		if (vm_swapcache_curburst < 0)
410b12defdcSMatthew Dillon 			break;
4111de864f0SMatthew Dillon 
4121de864f0SMatthew Dillon 		/*
4131de864f0SMatthew Dillon 		 * Move marker
4141de864f0SMatthew Dillon 		 */
415027193ebSMatthew Dillon 		TAILQ_REMOVE(
416027193ebSMatthew Dillon 			&vm_page_queues[marker->queue].pl, marker, pageq);
417027193ebSMatthew Dillon 		TAILQ_INSERT_AFTER(
418027193ebSMatthew Dillon 			&vm_page_queues[marker->queue].pl, m, marker, pageq);
419f5f6d247SMatthew Dillon 
420f5f6d247SMatthew Dillon 		/*
421f5f6d247SMatthew Dillon 		 * Ignore markers and ignore pages that already have a swap
422f5f6d247SMatthew Dillon 		 * assignment.
423f5f6d247SMatthew Dillon 		 */
4240bf81261SMatthew Dillon 		if (m->flags & (PG_MARKER | PG_SWAPPED))
425096e95c0SMatthew Dillon 			continue;
426b12defdcSMatthew Dillon 		if (vm_page_busy_try(m, TRUE))
427096e95c0SMatthew Dillon 			continue;
428027193ebSMatthew Dillon 		vm_page_queues_spin_unlock(marker->queue);
429b12defdcSMatthew Dillon 
430b12defdcSMatthew Dillon 		if ((object = m->object) == NULL) {
431b12defdcSMatthew Dillon 			vm_page_wakeup(m);
432027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
433b12defdcSMatthew Dillon 			continue;
434b12defdcSMatthew Dillon 		}
435b12defdcSMatthew Dillon 		vm_object_hold(object);
436b12defdcSMatthew Dillon 		if (m->object != object) {
437b12defdcSMatthew Dillon 			vm_object_drop(object);
438b12defdcSMatthew Dillon 			vm_page_wakeup(m);
439027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
440b12defdcSMatthew Dillon 			continue;
441b12defdcSMatthew Dillon 		}
442b12defdcSMatthew Dillon 		if (vm_swapcache_test(m)) {
443b12defdcSMatthew Dillon 			vm_object_drop(object);
444b12defdcSMatthew Dillon 			vm_page_wakeup(m);
445027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
446b12defdcSMatthew Dillon 			continue;
447b12defdcSMatthew Dillon 		}
448b12defdcSMatthew Dillon 
449c504e38eSMatthew Dillon 		vp = object->handle;
450b12defdcSMatthew Dillon 		if (vp == NULL) {
451b12defdcSMatthew Dillon 			vm_object_drop(object);
452b12defdcSMatthew Dillon 			vm_page_wakeup(m);
453027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
454c504e38eSMatthew Dillon 			continue;
455b12defdcSMatthew Dillon 		}
456d3070b8dSMatthew Dillon 
457c504e38eSMatthew Dillon 		switch(vp->v_type) {
458c504e38eSMatthew Dillon 		case VREG:
459e9b56058SMatthew Dillon 			/*
460bfa86281SMatthew Dillon 			 * PG_NOTMETA generically means 'don't swapcache this',
461bfa86281SMatthew Dillon 			 * and HAMMER will set this for regular data buffers
462bfa86281SMatthew Dillon 			 * (and leave it unset for meta-data buffers) as
463bfa86281SMatthew Dillon 			 * appropriate when double buffering is enabled.
464bfa86281SMatthew Dillon 			 */
465b12defdcSMatthew Dillon 			if (m->flags & PG_NOTMETA) {
466b12defdcSMatthew Dillon 				vm_object_drop(object);
467b12defdcSMatthew Dillon 				vm_page_wakeup(m);
468027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
469bfa86281SMatthew Dillon 				continue;
470b12defdcSMatthew Dillon 			}
471bfa86281SMatthew Dillon 
472bfa86281SMatthew Dillon 			/*
473e9b56058SMatthew Dillon 			 * If data_enable is 0 do not try to swapcache data.
474e9b56058SMatthew Dillon 			 * If use_chflags is set then only swapcache data for
475e9b56058SMatthew Dillon 			 * VSWAPCACHE marked vnodes, otherwise any vnode.
476e9b56058SMatthew Dillon 			 */
477e9b56058SMatthew Dillon 			if (vm_swapcache_data_enable == 0 ||
478e9b56058SMatthew Dillon 			    ((vp->v_flag & VSWAPCACHE) == 0 &&
479e9b56058SMatthew Dillon 			     vm_swapcache_use_chflags)) {
480b12defdcSMatthew Dillon 				vm_object_drop(object);
481b12defdcSMatthew Dillon 				vm_page_wakeup(m);
482027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
483c504e38eSMatthew Dillon 				continue;
484e9b56058SMatthew Dillon 			}
485d3070b8dSMatthew Dillon 			if (vm_swapcache_maxfilesize &&
486d3070b8dSMatthew Dillon 			    object->size >
487d3070b8dSMatthew Dillon 			    (vm_swapcache_maxfilesize >> PAGE_SHIFT)) {
488b12defdcSMatthew Dillon 				vm_object_drop(object);
489b12defdcSMatthew Dillon 				vm_page_wakeup(m);
490027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
491d3070b8dSMatthew Dillon 				continue;
492d3070b8dSMatthew Dillon 			}
493aabd5ce8SMatthew Dillon 			isblkdev = 0;
494c504e38eSMatthew Dillon 			break;
495c504e38eSMatthew Dillon 		case VCHR:
496aabd5ce8SMatthew Dillon 			/*
497bfa86281SMatthew Dillon 			 * PG_NOTMETA generically means 'don't swapcache this',
498bfa86281SMatthew Dillon 			 * and HAMMER will set this for regular data buffers
499bfa86281SMatthew Dillon 			 * (and leave it unset for meta-data buffers) as
500bfa86281SMatthew Dillon 			 * appropriate when double buffering is enabled.
501aabd5ce8SMatthew Dillon 			 */
502b12defdcSMatthew Dillon 			if (m->flags & PG_NOTMETA) {
503b12defdcSMatthew Dillon 				vm_object_drop(object);
504b12defdcSMatthew Dillon 				vm_page_wakeup(m);
505027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
506aabd5ce8SMatthew Dillon 				continue;
507b12defdcSMatthew Dillon 			}
508b12defdcSMatthew Dillon 			if (vm_swapcache_meta_enable == 0) {
509b12defdcSMatthew Dillon 				vm_object_drop(object);
510b12defdcSMatthew Dillon 				vm_page_wakeup(m);
511027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
512c504e38eSMatthew Dillon 				continue;
513b12defdcSMatthew Dillon 			}
514aabd5ce8SMatthew Dillon 			isblkdev = 1;
515c504e38eSMatthew Dillon 			break;
516c504e38eSMatthew Dillon 		default:
517b12defdcSMatthew Dillon 			vm_object_drop(object);
518b12defdcSMatthew Dillon 			vm_page_wakeup(m);
519027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
520c504e38eSMatthew Dillon 			continue;
521c504e38eSMatthew Dillon 		}
522096e95c0SMatthew Dillon 
523096e95c0SMatthew Dillon 
524096e95c0SMatthew Dillon 		/*
5253ffc7051SMatthew Dillon 		 * Assign swap and initiate I/O.
5263ffc7051SMatthew Dillon 		 *
5273ffc7051SMatthew Dillon 		 * (adjust for the --count which also occurs in the loop)
528096e95c0SMatthew Dillon 		 */
5290bf81261SMatthew Dillon 		count -= vm_swapcached_flush(m, isblkdev);
530096e95c0SMatthew Dillon 
531096e95c0SMatthew Dillon 		/*
532096e95c0SMatthew Dillon 		 * Setup for next loop using marker.
533096e95c0SMatthew Dillon 		 */
534b12defdcSMatthew Dillon 		vm_object_drop(object);
535027193ebSMatthew Dillon 		vm_page_queues_spin_lock(marker->queue);
536096e95c0SMatthew Dillon 	}
5371e5196f0SMatthew Dillon 
5381e5196f0SMatthew Dillon 	/*
539b12defdcSMatthew Dillon 	 * The marker could wind up at the end, which is ok.  If we hit the
540b12defdcSMatthew Dillon 	 * end of the list adjust the heuristic.
5411e5196f0SMatthew Dillon 	 *
5421e5196f0SMatthew Dillon 	 * Earlier inactive pages that were dirty and become clean
5431e5196f0SMatthew Dillon 	 * are typically moved to the end of PQ_INACTIVE by virtue
5441e5196f0SMatthew Dillon 	 * of vfs_vmio_release() when they become unwired from the
5451e5196f0SMatthew Dillon 	 * buffer cache.
5461e5196f0SMatthew Dillon 	 */
547027193ebSMatthew Dillon 	vm_page_queues_spin_unlock(marker->queue);
54864949baaSMatthew Dillon 
54964949baaSMatthew Dillon 	/*
55064949baaSMatthew Dillon 	 * m invalid but can be used to test for NULL
55164949baaSMatthew Dillon 	 */
55264949baaSMatthew Dillon 	return (m == NULL);
553096e95c0SMatthew Dillon }
554096e95c0SMatthew Dillon 
555096e95c0SMatthew Dillon /*
556b12defdcSMatthew Dillon  * Flush the specified page using the swap_pager.  The page
557b12defdcSMatthew Dillon  * must be busied by the caller and its disposition will become
558b12defdcSMatthew Dillon  * the responsibility of this function.
5593ffc7051SMatthew Dillon  *
5603ffc7051SMatthew Dillon  * Try to collect surrounding pages, including pages which may
5613ffc7051SMatthew Dillon  * have already been assigned swap.  Try to cluster within a
5623ffc7051SMatthew Dillon  * contiguous aligned SMAP_META_PAGES (typ 16 x PAGE_SIZE) block
5633ffc7051SMatthew Dillon  * to match what swap_pager_putpages() can do.
5643ffc7051SMatthew Dillon  *
5653ffc7051SMatthew Dillon  * We also want to try to match against the buffer cache blocksize
5663ffc7051SMatthew Dillon  * but we don't really know what it is here.  Since the buffer cache
5673ffc7051SMatthew Dillon  * wires and unwires pages in groups the fact that we skip wired pages
5683ffc7051SMatthew Dillon  * should be sufficient.
5693ffc7051SMatthew Dillon  *
5703ffc7051SMatthew Dillon  * Returns a count of pages we might have flushed (minimum 1)
571096e95c0SMatthew Dillon  */
572096e95c0SMatthew Dillon static
5733ffc7051SMatthew Dillon int
574aabd5ce8SMatthew Dillon vm_swapcached_flush(vm_page_t m, int isblkdev)
575096e95c0SMatthew Dillon {
576096e95c0SMatthew Dillon 	vm_object_t object;
5773ffc7051SMatthew Dillon 	vm_page_t marray[SWAP_META_PAGES];
5783ffc7051SMatthew Dillon 	vm_pindex_t basei;
5793ffc7051SMatthew Dillon 	int rtvals[SWAP_META_PAGES];
5803ffc7051SMatthew Dillon 	int x;
5813ffc7051SMatthew Dillon 	int i;
5823ffc7051SMatthew Dillon 	int j;
5833ffc7051SMatthew Dillon 	int count;
584b12defdcSMatthew Dillon 	int error;
585096e95c0SMatthew Dillon 
586096e95c0SMatthew Dillon 	vm_page_io_start(m);
587096e95c0SMatthew Dillon 	vm_page_protect(m, VM_PROT_READ);
588096e95c0SMatthew Dillon 	object = m->object;
589b12defdcSMatthew Dillon 	vm_object_hold(object);
590096e95c0SMatthew Dillon 
5913ffc7051SMatthew Dillon 	/*
5923ffc7051SMatthew Dillon 	 * Try to cluster around (m), keeping in mind that the swap pager
5933ffc7051SMatthew Dillon 	 * can only do SMAP_META_PAGES worth of continguous write.
5943ffc7051SMatthew Dillon 	 */
5953ffc7051SMatthew Dillon 	x = (int)m->pindex & SWAP_META_MASK;
5963ffc7051SMatthew Dillon 	marray[x] = m;
5973ffc7051SMatthew Dillon 	basei = m->pindex;
598b12defdcSMatthew Dillon 	vm_page_wakeup(m);
5993ffc7051SMatthew Dillon 
6003ffc7051SMatthew Dillon 	for (i = x - 1; i >= 0; --i) {
601b12defdcSMatthew Dillon 		m = vm_page_lookup_busy_try(object, basei - x + i,
602b12defdcSMatthew Dillon 					    TRUE, &error);
603b12defdcSMatthew Dillon 		if (error || m == NULL)
6043ffc7051SMatthew Dillon 			break;
605b12defdcSMatthew Dillon 		if (vm_swapcache_test(m)) {
606b12defdcSMatthew Dillon 			vm_page_wakeup(m);
6073ffc7051SMatthew Dillon 			break;
608b12defdcSMatthew Dillon 		}
609b12defdcSMatthew Dillon 		if (isblkdev && (m->flags & PG_NOTMETA)) {
610b12defdcSMatthew Dillon 			vm_page_wakeup(m);
611aabd5ce8SMatthew Dillon 			break;
612b12defdcSMatthew Dillon 		}
6133ffc7051SMatthew Dillon 		vm_page_io_start(m);
6143ffc7051SMatthew Dillon 		vm_page_protect(m, VM_PROT_READ);
6153ffc7051SMatthew Dillon 		if (m->queue - m->pc == PQ_CACHE) {
6163ffc7051SMatthew Dillon 			vm_page_unqueue_nowakeup(m);
6173ffc7051SMatthew Dillon 			vm_page_deactivate(m);
6183ffc7051SMatthew Dillon 		}
6193ffc7051SMatthew Dillon 		marray[i] = m;
620b12defdcSMatthew Dillon 		vm_page_wakeup(m);
6213ffc7051SMatthew Dillon 	}
6223ffc7051SMatthew Dillon 	++i;
6233ffc7051SMatthew Dillon 
6243ffc7051SMatthew Dillon 	for (j = x + 1; j < SWAP_META_PAGES; ++j) {
625b12defdcSMatthew Dillon 		m = vm_page_lookup_busy_try(object, basei - x + j,
626b12defdcSMatthew Dillon 					    TRUE, &error);
627b12defdcSMatthew Dillon 		if (error || m == NULL)
6283ffc7051SMatthew Dillon 			break;
629b12defdcSMatthew Dillon 		if (vm_swapcache_test(m)) {
630b12defdcSMatthew Dillon 			vm_page_wakeup(m);
6313ffc7051SMatthew Dillon 			break;
632b12defdcSMatthew Dillon 		}
633b12defdcSMatthew Dillon 		if (isblkdev && (m->flags & PG_NOTMETA)) {
634b12defdcSMatthew Dillon 			vm_page_wakeup(m);
635aabd5ce8SMatthew Dillon 			break;
636b12defdcSMatthew Dillon 		}
6373ffc7051SMatthew Dillon 		vm_page_io_start(m);
6383ffc7051SMatthew Dillon 		vm_page_protect(m, VM_PROT_READ);
6393ffc7051SMatthew Dillon 		if (m->queue - m->pc == PQ_CACHE) {
6403ffc7051SMatthew Dillon 			vm_page_unqueue_nowakeup(m);
6413ffc7051SMatthew Dillon 			vm_page_deactivate(m);
6423ffc7051SMatthew Dillon 		}
6433ffc7051SMatthew Dillon 		marray[j] = m;
644b12defdcSMatthew Dillon 		vm_page_wakeup(m);
6453ffc7051SMatthew Dillon 	}
6463ffc7051SMatthew Dillon 
6473ffc7051SMatthew Dillon 	count = j - i;
6483ffc7051SMatthew Dillon 	vm_object_pip_add(object, count);
6493ffc7051SMatthew Dillon 	swap_pager_putpages(object, marray + i, count, FALSE, rtvals + i);
6503ffc7051SMatthew Dillon 	vm_swapcache_write_count += count * PAGE_SIZE;
6513ffc7051SMatthew Dillon 	vm_swapcache_curburst -= count * PAGE_SIZE;
6523ffc7051SMatthew Dillon 
6533ffc7051SMatthew Dillon 	while (i < j) {
6543ffc7051SMatthew Dillon 		if (rtvals[i] != VM_PAGER_PEND) {
655b12defdcSMatthew Dillon 			vm_page_busy_wait(marray[i], FALSE, "swppgfd");
6563ffc7051SMatthew Dillon 			vm_page_io_finish(marray[i]);
657b12defdcSMatthew Dillon 			vm_page_wakeup(marray[i]);
658096e95c0SMatthew Dillon 			vm_object_pip_wakeup(object);
659096e95c0SMatthew Dillon 		}
6603ffc7051SMatthew Dillon 		++i;
6613ffc7051SMatthew Dillon 	}
662b12defdcSMatthew Dillon 	vm_object_drop(object);
6633ffc7051SMatthew Dillon 	return(count);
664096e95c0SMatthew Dillon }
66500a3fdcaSMatthew Dillon 
6663ffc7051SMatthew Dillon /*
6673ffc7051SMatthew Dillon  * Test whether a VM page is suitable for writing to the swapcache.
6683ffc7051SMatthew Dillon  * Does not test m->queue, PG_MARKER, or PG_SWAPPED.
6693ffc7051SMatthew Dillon  *
6703ffc7051SMatthew Dillon  * Returns 0 on success, 1 on failure
6713ffc7051SMatthew Dillon  */
6723ffc7051SMatthew Dillon static int
6733ffc7051SMatthew Dillon vm_swapcache_test(vm_page_t m)
6743ffc7051SMatthew Dillon {
6753ffc7051SMatthew Dillon 	vm_object_t object;
6763ffc7051SMatthew Dillon 
677*831a8507SMatthew Dillon 	if (m->flags & (PG_UNQUEUED | PG_FICTITIOUS))
6783ffc7051SMatthew Dillon 		return(1);
679b12defdcSMatthew Dillon 	if (m->hold_count || m->wire_count)
6803ffc7051SMatthew Dillon 		return(1);
6813ffc7051SMatthew Dillon 	if (m->valid != VM_PAGE_BITS_ALL)
6823ffc7051SMatthew Dillon 		return(1);
6833ffc7051SMatthew Dillon 	if (m->dirty & m->valid)
6843ffc7051SMatthew Dillon 		return(1);
6853ffc7051SMatthew Dillon 	if ((object = m->object) == NULL)
6863ffc7051SMatthew Dillon 		return(1);
6873ffc7051SMatthew Dillon 	if (object->type != OBJT_VNODE ||
6883ffc7051SMatthew Dillon 	    (object->flags & OBJ_DEAD)) {
6893ffc7051SMatthew Dillon 		return(1);
6903ffc7051SMatthew Dillon 	}
6913ffc7051SMatthew Dillon 	vm_page_test_dirty(m);
6923ffc7051SMatthew Dillon 	if (m->dirty & m->valid)
6933ffc7051SMatthew Dillon 		return(1);
6943ffc7051SMatthew Dillon 	return(0);
6953ffc7051SMatthew Dillon }
6963ffc7051SMatthew Dillon 
6973ffc7051SMatthew Dillon /*
698f5f6d247SMatthew Dillon  * Cleaning pass.
699f5f6d247SMatthew Dillon  *
700f5f6d247SMatthew Dillon  * We clean whole objects up to 16MB
7013ffc7051SMatthew Dillon  */
70200a3fdcaSMatthew Dillon static
70300a3fdcaSMatthew Dillon void
7049de48eadSMatthew Dillon vm_swapcache_cleaning(swmarker_t *marker, struct vm_object_hash **swindexp)
70500a3fdcaSMatthew Dillon {
70600a3fdcaSMatthew Dillon 	vm_object_t object;
70700a3fdcaSMatthew Dillon 	struct vnode *vp;
70800a3fdcaSMatthew Dillon 	int count;
7090bf81261SMatthew Dillon 	int scount;
71000a3fdcaSMatthew Dillon 	int n;
711eea5ad68SMatthew Dillon 	int didmove;
71200a3fdcaSMatthew Dillon 
71300a3fdcaSMatthew Dillon 	count = vm_swapcache_maxlaunder;
7140bf81261SMatthew Dillon 	scount = vm_swapcache_maxscan;
71500a3fdcaSMatthew Dillon 
71600a3fdcaSMatthew Dillon 	/*
71700a3fdcaSMatthew Dillon 	 * Look for vnode objects
71800a3fdcaSMatthew Dillon 	 */
719fde6be6aSMatthew Dillon 	lwkt_gettoken(&(*swindexp)->token);
7202de4f77eSMatthew Dillon 
721eea5ad68SMatthew Dillon 	didmove = 0;
722b2286ce7SMatthew Dillon outerloop:
7235b329e62SMatthew Dillon 	while ((object = TAILQ_NEXT(&marker->dummy_obj,
7245b329e62SMatthew Dillon 				    object_entry)) != NULL) {
7252f2d9e58SVenkatesh Srinivas 		/*
726f5f6d247SMatthew Dillon 		 * We have to skip markers.  We cannot hold/drop marker
727f5f6d247SMatthew Dillon 		 * objects!
7282f2d9e58SVenkatesh Srinivas 		 */
729f5f6d247SMatthew Dillon 		if (object->type == OBJT_MARKER) {
7307b00fbb4SMatthew Dillon 			vm_swapcache_movemarker(marker, *swindexp, object);
731eea5ad68SMatthew Dillon 			didmove = 1;
73200a3fdcaSMatthew Dillon 			continue;
7332f2d9e58SVenkatesh Srinivas 		}
73400a3fdcaSMatthew Dillon 
73500a3fdcaSMatthew Dillon 		/*
736f5f6d247SMatthew Dillon 		 * Safety, or in case there are millions of VM objects
737f5f6d247SMatthew Dillon 		 * without swapcache backing.
73800a3fdcaSMatthew Dillon 		 */
7390bf81261SMatthew Dillon 		if (--scount <= 0)
7407b00fbb4SMatthew Dillon 			goto breakout;
74100a3fdcaSMatthew Dillon 
74200a3fdcaSMatthew Dillon 		/*
743f5f6d247SMatthew Dillon 		 * We must hold the object before potentially yielding.
74400a3fdcaSMatthew Dillon 		 */
745f5f6d247SMatthew Dillon 		vm_object_hold(object);
746f5f6d247SMatthew Dillon 		lwkt_yield();
747f5f6d247SMatthew Dillon 
748f5f6d247SMatthew Dillon 		/*
749f5f6d247SMatthew Dillon 		 * Only operate on live VNODE objects that are either
750f5f6d247SMatthew Dillon 		 * VREG or VCHR (VCHR for meta-data).
751f5f6d247SMatthew Dillon 		 */
752f5f6d247SMatthew Dillon 		if ((object->type != OBJT_VNODE) ||
753f5f6d247SMatthew Dillon 		    ((object->flags & OBJ_DEAD) ||
754f5f6d247SMatthew Dillon 		     object->swblock_count == 0) ||
755f5f6d247SMatthew Dillon 		    ((vp = object->handle) == NULL) ||
756f5f6d247SMatthew Dillon 		    (vp->v_type != VREG && vp->v_type != VCHR)) {
757f5f6d247SMatthew Dillon 			vm_object_drop(object);
758f5f6d247SMatthew Dillon 			/* object may be invalid now */
7597b00fbb4SMatthew Dillon 			vm_swapcache_movemarker(marker, *swindexp, object);
760eea5ad68SMatthew Dillon 			didmove = 1;
761f5f6d247SMatthew Dillon 			continue;
762f5f6d247SMatthew Dillon 		}
763f5f6d247SMatthew Dillon 
764f5f6d247SMatthew Dillon 		/*
765f5f6d247SMatthew Dillon 		 * Reset the object pindex stored in the marker if the
766f5f6d247SMatthew Dillon 		 * working object has changed.
767f5f6d247SMatthew Dillon 		 */
7689de48eadSMatthew Dillon 		if (marker->save_obj != object || didmove) {
7699de48eadSMatthew Dillon 			marker->dummy_obj.size = 0;
7709de48eadSMatthew Dillon 			marker->save_off = 0;
7719de48eadSMatthew Dillon 			marker->save_obj = object;
772eea5ad68SMatthew Dillon 			didmove = 0;
773f5f6d247SMatthew Dillon 		}
77400a3fdcaSMatthew Dillon 
77500a3fdcaSMatthew Dillon 		/*
77600a3fdcaSMatthew Dillon 		 * Look for swblocks starting at our iterator.
77700a3fdcaSMatthew Dillon 		 *
77800a3fdcaSMatthew Dillon 		 * The swap_pager_condfree() function attempts to free
77900a3fdcaSMatthew Dillon 		 * swap space starting at the specified index.  The index
78000a3fdcaSMatthew Dillon 		 * will be updated on return.  The function will return
78100a3fdcaSMatthew Dillon 		 * a scan factor (NOT the number of blocks freed).
78200a3fdcaSMatthew Dillon 		 *
78300a3fdcaSMatthew Dillon 		 * If it must cut its scan of the object short due to an
78400a3fdcaSMatthew Dillon 		 * excessive number of swblocks, or is able to free the
78500a3fdcaSMatthew Dillon 		 * requested number of blocks, it will return n >= count
78600a3fdcaSMatthew Dillon 		 * and we break and pick it back up on a future attempt.
787f5f6d247SMatthew Dillon 		 *
788f5f6d247SMatthew Dillon 		 * Scan the object linearly and try to batch large sets of
789f5f6d247SMatthew Dillon 		 * blocks that are likely to clean out entire swap radix
790f5f6d247SMatthew Dillon 		 * tree leafs.
79100a3fdcaSMatthew Dillon 		 */
792739be60bSMatthew Dillon 		lwkt_token_swap();
793fde6be6aSMatthew Dillon 		lwkt_reltoken(&(*swindexp)->token);
79427b6ee03SMatthew Dillon 
7959de48eadSMatthew Dillon 		n = swap_pager_condfree(object, &marker->dummy_obj.size,
796f5f6d247SMatthew Dillon 				    (count + SWAP_META_MASK) & ~SWAP_META_MASK);
7972f2d9e58SVenkatesh Srinivas 
798f5f6d247SMatthew Dillon 		vm_object_drop(object);		/* object may be invalid now */
799fde6be6aSMatthew Dillon 		lwkt_gettoken(&(*swindexp)->token);
8002f2d9e58SVenkatesh Srinivas 
80100a3fdcaSMatthew Dillon 		/*
802f5f6d247SMatthew Dillon 		 * If we have exhausted the object or deleted our per-pass
803f5f6d247SMatthew Dillon 		 * page limit then move us to the next object.  Note that
8045b329e62SMatthew Dillon 		 * the current object may no longer be on the vm_object_entry.
80500a3fdcaSMatthew Dillon 		 */
806f5f6d247SMatthew Dillon 		if (n <= 0 ||
8079de48eadSMatthew Dillon 		    marker->save_off > vm_swapcache_cleanperobj) {
8087b00fbb4SMatthew Dillon 			vm_swapcache_movemarker(marker, *swindexp, object);
809eea5ad68SMatthew Dillon 			didmove = 1;
81000a3fdcaSMatthew Dillon 		}
81100a3fdcaSMatthew Dillon 
81200a3fdcaSMatthew Dillon 		/*
813f5f6d247SMatthew Dillon 		 * If we have exhausted our max-launder stop for now.
81400a3fdcaSMatthew Dillon 		 */
815f5f6d247SMatthew Dillon 		count -= n;
8169de48eadSMatthew Dillon 		marker->save_off += n * PAGE_SIZE;
817f5f6d247SMatthew Dillon 		if (count < 0)
8187b00fbb4SMatthew Dillon 			goto breakout;
819f5f6d247SMatthew Dillon 	}
8207a175765SMatthew Dillon 
8217a175765SMatthew Dillon 	/*
8225b329e62SMatthew Dillon 	 * Iterate vm_object_hash[] hash table
8237a175765SMatthew Dillon 	 */
8245b329e62SMatthew Dillon 	TAILQ_REMOVE(&(*swindexp)->list, &marker->dummy_obj, object_entry);
825fde6be6aSMatthew Dillon 	lwkt_reltoken(&(*swindexp)->token);
826fde6be6aSMatthew Dillon 	if (++*swindexp >= &vm_object_hash[VMOBJ_HSIZE])
827fde6be6aSMatthew Dillon 		*swindexp = &vm_object_hash[0];
828fde6be6aSMatthew Dillon 	lwkt_gettoken(&(*swindexp)->token);
8295b329e62SMatthew Dillon 	TAILQ_INSERT_HEAD(&(*swindexp)->list, &marker->dummy_obj, object_entry);
8307a175765SMatthew Dillon 
831fde6be6aSMatthew Dillon 	if (*swindexp != &vm_object_hash[0])
8327b00fbb4SMatthew Dillon 		goto outerloop;
8337b00fbb4SMatthew Dillon 
8347b00fbb4SMatthew Dillon breakout:
835fde6be6aSMatthew Dillon 	lwkt_reltoken(&(*swindexp)->token);
83600a3fdcaSMatthew Dillon }
837f5f6d247SMatthew Dillon 
838f5f6d247SMatthew Dillon /*
839f5f6d247SMatthew Dillon  * Move the marker past the current object.  Object can be stale, but we
840f5f6d247SMatthew Dillon  * still need it to determine if the marker has to be moved.  If the object
841f5f6d247SMatthew Dillon  * is still the 'current object' (object after the marker), we hop-scotch
842f5f6d247SMatthew Dillon  * the marker past it.
843f5f6d247SMatthew Dillon  */
844f5f6d247SMatthew Dillon static void
8459de48eadSMatthew Dillon vm_swapcache_movemarker(swmarker_t *marker, struct vm_object_hash *swindex,
846fde6be6aSMatthew Dillon 			vm_object_t object)
847f5f6d247SMatthew Dillon {
8485b329e62SMatthew Dillon 	if (TAILQ_NEXT(&marker->dummy_obj, object_entry) == object) {
8495b329e62SMatthew Dillon 		TAILQ_REMOVE(&swindex->list, &marker->dummy_obj, object_entry);
8509de48eadSMatthew Dillon 		TAILQ_INSERT_AFTER(&swindex->list, object,
8515b329e62SMatthew Dillon 				   &marker->dummy_obj, object_entry);
852f5f6d247SMatthew Dillon 	}
853f5f6d247SMatthew Dillon }
854