xref: /dragonfly/sys/vm/vm_swapcache.c (revision eea5ad68)
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);
8564949baaSMatthew Dillon static int vm_swapcache_writing_heuristic(void);
8664949baaSMatthew Dillon static int vm_swapcache_writing(vm_page_t marker, int count, int scount);
87fde6be6aSMatthew Dillon static void vm_swapcache_cleaning(vm_object_t marker,
88fde6be6aSMatthew Dillon 			struct vm_object_hash **swindexp);
89fde6be6aSMatthew Dillon static void vm_swapcache_movemarker(vm_object_t marker,
90fde6be6aSMatthew Dillon 			struct vm_object_hash *swindex, vm_object_t object);
91096e95c0SMatthew Dillon struct thread *swapcached_thread;
92096e95c0SMatthew Dillon 
93096e95c0SMatthew Dillon SYSCTL_NODE(_vm, OID_AUTO, swapcache, CTLFLAG_RW, NULL, NULL);
94096e95c0SMatthew Dillon 
95c504e38eSMatthew Dillon int vm_swapcache_read_enable;
96e527fb6bSMatthew Dillon int vm_swapcache_inactive_heuristic;
97096e95c0SMatthew Dillon static int vm_swapcache_sleep;
9864949baaSMatthew Dillon static int vm_swapcache_maxscan = PQ_L2_SIZE * 8;
9964949baaSMatthew Dillon static int vm_swapcache_maxlaunder = PQ_L2_SIZE * 4;
100096e95c0SMatthew Dillon static int vm_swapcache_data_enable = 0;
101096e95c0SMatthew Dillon static int vm_swapcache_meta_enable = 0;
102e9b56058SMatthew Dillon static int vm_swapcache_maxswappct = 75;
103e527fb6bSMatthew Dillon static int vm_swapcache_hysteresis;
10464949baaSMatthew Dillon static int vm_swapcache_min_hysteresis;
105d4d9221dSMatthew Dillon int vm_swapcache_use_chflags = 0;	/* require chflags cache */
1063ffc7051SMatthew Dillon static int64_t vm_swapcache_minburst = 10000000LL;	/* 10MB */
1073ffc7051SMatthew Dillon static int64_t vm_swapcache_curburst = 4000000000LL;	/* 4G after boot */
1083ffc7051SMatthew Dillon static int64_t vm_swapcache_maxburst = 2000000000LL;	/* 2G nominal max */
1093ffc7051SMatthew Dillon static int64_t vm_swapcache_accrate = 100000LL;		/* 100K/s */
110096e95c0SMatthew Dillon static int64_t vm_swapcache_write_count;
1113ffc7051SMatthew Dillon static int64_t vm_swapcache_maxfilesize;
112f5f6d247SMatthew Dillon static int64_t vm_swapcache_cleanperobj = 16*1024*1024;
113096e95c0SMatthew Dillon 
114096e95c0SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, maxlaunder,
115096e95c0SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxlaunder, 0, "");
1160bf81261SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, maxscan,
1170bf81261SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxscan, 0, "");
118c504e38eSMatthew Dillon 
119096e95c0SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, data_enable,
120096e95c0SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_data_enable, 0, "");
121096e95c0SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, meta_enable,
122096e95c0SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_meta_enable, 0, "");
123c504e38eSMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, read_enable,
124c504e38eSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_read_enable, 0, "");
125e9b56058SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, maxswappct,
126e9b56058SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxswappct, 0, "");
127e527fb6bSMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, hysteresis,
12864949baaSMatthew Dillon 	CTLFLAG_RD, &vm_swapcache_hysteresis, 0, "");
12964949baaSMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, min_hysteresis,
13064949baaSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_min_hysteresis, 0, "");
131e9b56058SMatthew Dillon SYSCTL_INT(_vm_swapcache, OID_AUTO, use_chflags,
132e9b56058SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_use_chflags, 0, "");
133c504e38eSMatthew Dillon 
1343ffc7051SMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, minburst,
1353ffc7051SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_minburst, 0, "");
136c504e38eSMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, curburst,
137c504e38eSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_curburst, 0, "");
138c504e38eSMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxburst,
139c504e38eSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxburst, 0, "");
1403ffc7051SMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxfilesize,
1413ffc7051SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_maxfilesize, 0, "");
142c504e38eSMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, accrate,
143c504e38eSMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_accrate, 0, "");
144096e95c0SMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, write_count,
145096e95c0SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_write_count, 0, "");
146f5f6d247SMatthew Dillon SYSCTL_QUAD(_vm_swapcache, OID_AUTO, cleanperobj,
147f5f6d247SMatthew Dillon 	CTLFLAG_RW, &vm_swapcache_cleanperobj, 0, "");
148096e95c0SMatthew Dillon 
149e9b56058SMatthew Dillon #define SWAPMAX(adj)	\
150e9b56058SMatthew Dillon 	((int64_t)vm_swap_max * (vm_swapcache_maxswappct + (adj)) / 100)
151e9b56058SMatthew Dillon 
152096e95c0SMatthew Dillon /*
153497524bfSMatthew Dillon  * When shutting down the machine we want to stop swapcache operation
154497524bfSMatthew Dillon  * immediately so swap is not accessed after devices have been shuttered.
155497524bfSMatthew Dillon  */
156497524bfSMatthew Dillon static void
157497524bfSMatthew Dillon shutdown_swapcache(void *arg __unused)
158497524bfSMatthew Dillon {
159497524bfSMatthew Dillon 	vm_swapcache_read_enable = 0;
160497524bfSMatthew Dillon 	vm_swapcache_data_enable = 0;
161497524bfSMatthew Dillon 	vm_swapcache_meta_enable = 0;
162497524bfSMatthew Dillon 	wakeup(&vm_swapcache_sleep);	/* shortcut 5-second wait */
163497524bfSMatthew Dillon }
164497524bfSMatthew Dillon 
165497524bfSMatthew Dillon /*
166096e95c0SMatthew Dillon  * vm_swapcached is the high level pageout daemon.
1678e7c4729SMatthew Dillon  *
1688e7c4729SMatthew Dillon  * No requirements.
169096e95c0SMatthew Dillon  */
170096e95c0SMatthew Dillon static void
171cd8ab232SMatthew Dillon vm_swapcached_thread(void)
172096e95c0SMatthew Dillon {
17300a3fdcaSMatthew Dillon 	enum { SWAPC_WRITING, SWAPC_CLEANING } state = SWAPC_WRITING;
1743ffc7051SMatthew Dillon 	enum { SWAPB_BURSTING, SWAPB_RECOVERING } burst = SWAPB_BURSTING;
17551c99c61SMatthew Dillon 	static struct vm_page page_marker[PQ_L2_SIZE];
1767b00fbb4SMatthew Dillon 	static struct vm_object swmarker;
177fde6be6aSMatthew Dillon 	static struct vm_object_hash *swindex;
178027193ebSMatthew Dillon 	int q;
179096e95c0SMatthew Dillon 
180096e95c0SMatthew Dillon 	/*
181096e95c0SMatthew Dillon 	 * Thread setup
182096e95c0SMatthew Dillon 	 */
183096e95c0SMatthew Dillon 	curthread->td_flags |= TDF_SYSTHREAD;
184497524bfSMatthew Dillon 	EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc,
185497524bfSMatthew Dillon 			      swapcached_thread, SHUTDOWN_PRI_FIRST);
186497524bfSMatthew Dillon 	EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_swapcache,
187497524bfSMatthew Dillon 			      NULL, SHUTDOWN_PRI_SECOND);
188096e95c0SMatthew Dillon 
189096e95c0SMatthew Dillon 	/*
19000a3fdcaSMatthew Dillon 	 * Initialize our marker for the inactive scan (SWAPC_WRITING)
191096e95c0SMatthew Dillon 	 */
19200a3fdcaSMatthew Dillon 	bzero(&page_marker, sizeof(page_marker));
19351c99c61SMatthew Dillon 	for (q = 0; q < PQ_L2_SIZE; ++q) {
194bc0aa189SMatthew Dillon 		page_marker[q].flags = PG_FICTITIOUS | PG_MARKER;
195bc0aa189SMatthew Dillon 		page_marker[q].busy_count = PBUSY_LOCKED;
196027193ebSMatthew Dillon 		page_marker[q].queue = PQ_INACTIVE + q;
197027193ebSMatthew Dillon 		page_marker[q].pc = q;
198027193ebSMatthew Dillon 		page_marker[q].wire_count = 1;
199027193ebSMatthew Dillon 		vm_page_queues_spin_lock(PQ_INACTIVE + q);
200027193ebSMatthew Dillon 		TAILQ_INSERT_HEAD(
201027193ebSMatthew Dillon 			&vm_page_queues[PQ_INACTIVE + q].pl,
202027193ebSMatthew Dillon 			&page_marker[q], pageq);
203027193ebSMatthew Dillon 		vm_page_queues_spin_unlock(PQ_INACTIVE + q);
204027193ebSMatthew Dillon 	}
205b12defdcSMatthew Dillon 
20664949baaSMatthew Dillon 	vm_swapcache_min_hysteresis = 1024;
20764949baaSMatthew Dillon 	vm_swapcache_hysteresis = vm_swapcache_min_hysteresis;
208e527fb6bSMatthew Dillon 	vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
209096e95c0SMatthew Dillon 
21000a3fdcaSMatthew Dillon 	/*
21100a3fdcaSMatthew Dillon 	 * Initialize our marker for the vm_object scan (SWAPC_CLEANING)
21200a3fdcaSMatthew Dillon 	 */
2137b00fbb4SMatthew Dillon 	bzero(&swmarker, sizeof(swmarker));
2147b00fbb4SMatthew Dillon 	swmarker.type = OBJT_MARKER;
215fde6be6aSMatthew Dillon 	swindex = &vm_object_hash[0];
216fde6be6aSMatthew Dillon 	lwkt_gettoken(&swindex->token);
217fde6be6aSMatthew Dillon 	TAILQ_INSERT_HEAD(&swindex->list, &swmarker, object_list);
218fde6be6aSMatthew Dillon 	lwkt_reltoken(&swindex->token);
219096e95c0SMatthew Dillon 
220096e95c0SMatthew Dillon 	for (;;) {
22164949baaSMatthew Dillon 		int reached_end;
22264949baaSMatthew Dillon 		int scount;
22364949baaSMatthew Dillon 		int count;
22464949baaSMatthew Dillon 
225096e95c0SMatthew Dillon 		/*
226497524bfSMatthew Dillon 		 * Handle shutdown
227497524bfSMatthew Dillon 		 */
228497524bfSMatthew Dillon 		kproc_suspend_loop();
229497524bfSMatthew Dillon 
230497524bfSMatthew Dillon 		/*
2313da46bd7SMatthew Dillon 		 * Check every 5 seconds when not enabled or if no swap
2323da46bd7SMatthew Dillon 		 * is present.
233096e95c0SMatthew Dillon 		 */
2343da46bd7SMatthew Dillon 		if ((vm_swapcache_data_enable == 0 &&
235c8ddd5d8SMatthew Dillon 		     vm_swapcache_meta_enable == 0 &&
236c8ddd5d8SMatthew Dillon 		     vm_swap_cache_use <= SWAPMAX(0)) ||
2373da46bd7SMatthew Dillon 		    vm_swap_max == 0) {
238096e95c0SMatthew Dillon 			tsleep(&vm_swapcache_sleep, 0, "csleep", hz * 5);
239096e95c0SMatthew Dillon 			continue;
240096e95c0SMatthew Dillon 		}
241c504e38eSMatthew Dillon 
242c504e38eSMatthew Dillon 		/*
2433da46bd7SMatthew Dillon 		 * Polling rate when enabled is approximately 10 hz.
244c504e38eSMatthew Dillon 		 */
245c504e38eSMatthew Dillon 		tsleep(&vm_swapcache_sleep, 0, "csleep", hz / 10);
24600a3fdcaSMatthew Dillon 
24700a3fdcaSMatthew Dillon 		/*
24800a3fdcaSMatthew Dillon 		 * State hysteresis.  Generate write activity up to 75% of
24900a3fdcaSMatthew Dillon 		 * swap, then clean out swap assignments down to 70%, then
25000a3fdcaSMatthew Dillon 		 * repeat.
25100a3fdcaSMatthew Dillon 		 */
25200a3fdcaSMatthew Dillon 		if (state == SWAPC_WRITING) {
253e9b56058SMatthew Dillon 			if (vm_swap_cache_use > SWAPMAX(0))
25400a3fdcaSMatthew Dillon 				state = SWAPC_CLEANING;
25500a3fdcaSMatthew Dillon 		} else {
25608fb7a9dSMatthew Dillon 			if (vm_swap_cache_use < SWAPMAX(-10))
25700a3fdcaSMatthew Dillon 				state = SWAPC_WRITING;
25800a3fdcaSMatthew Dillon 		}
25900a3fdcaSMatthew Dillon 
26000a3fdcaSMatthew Dillon 		/*
26100a3fdcaSMatthew Dillon 		 * We are allowed to continue accumulating burst value
2623ffc7051SMatthew Dillon 		 * in either state.  Allow the user to set curburst > maxburst
2633ffc7051SMatthew Dillon 		 * for the initial load-in.
26400a3fdcaSMatthew Dillon 		 */
2653ffc7051SMatthew Dillon 		if (vm_swapcache_curburst < vm_swapcache_maxburst) {
266c504e38eSMatthew Dillon 			vm_swapcache_curburst += vm_swapcache_accrate / 10;
267c504e38eSMatthew Dillon 			if (vm_swapcache_curburst > vm_swapcache_maxburst)
268c504e38eSMatthew Dillon 				vm_swapcache_curburst = vm_swapcache_maxburst;
2693ffc7051SMatthew Dillon 		}
270c504e38eSMatthew Dillon 
271c504e38eSMatthew Dillon 		/*
27200a3fdcaSMatthew Dillon 		 * We don't want to nickle-and-dime the scan as that will
27300a3fdcaSMatthew Dillon 		 * create unnecessary fragmentation.  The minimum burst
27400a3fdcaSMatthew Dillon 		 * is one-seconds worth of accumulation.
275c504e38eSMatthew Dillon 		 */
27664949baaSMatthew Dillon 		if (state != SWAPC_WRITING) {
2777b00fbb4SMatthew Dillon 			vm_swapcache_cleaning(&swmarker, &swindex);
27864949baaSMatthew Dillon 			continue;
27964949baaSMatthew Dillon 		}
28064949baaSMatthew Dillon 		if (vm_swapcache_curburst < vm_swapcache_accrate)
28164949baaSMatthew Dillon 			continue;
28264949baaSMatthew Dillon 
28364949baaSMatthew Dillon 		reached_end = 0;
28464949baaSMatthew Dillon 		count = vm_swapcache_maxlaunder / PQ_L2_SIZE + 2;
28564949baaSMatthew Dillon 		scount = vm_swapcache_maxscan / PQ_L2_SIZE + 2;
28664949baaSMatthew Dillon 
2873ffc7051SMatthew Dillon 		if (burst == SWAPB_BURSTING) {
28864949baaSMatthew Dillon 			if (vm_swapcache_writing_heuristic()) {
28951c99c61SMatthew Dillon 				for (q = 0; q < PQ_L2_SIZE; ++q) {
29064949baaSMatthew Dillon 					reached_end +=
291027193ebSMatthew Dillon 						vm_swapcache_writing(
29264949baaSMatthew Dillon 							&page_marker[q],
29364949baaSMatthew Dillon 							count,
29464949baaSMatthew Dillon 							scount);
29564949baaSMatthew Dillon 				}
296027193ebSMatthew Dillon 			}
2973ffc7051SMatthew Dillon 			if (vm_swapcache_curburst <= 0)
2983ffc7051SMatthew Dillon 				burst = SWAPB_RECOVERING;
29964949baaSMatthew Dillon 		} else if (vm_swapcache_curburst > vm_swapcache_minburst) {
30064949baaSMatthew Dillon 			if (vm_swapcache_writing_heuristic()) {
30151c99c61SMatthew Dillon 				for (q = 0; q < PQ_L2_SIZE; ++q) {
30264949baaSMatthew Dillon 					reached_end +=
303027193ebSMatthew Dillon 						vm_swapcache_writing(
30464949baaSMatthew Dillon 							&page_marker[q],
30564949baaSMatthew Dillon 							count,
30664949baaSMatthew Dillon 							scount);
30764949baaSMatthew Dillon 				}
308027193ebSMatthew Dillon 			}
3093ffc7051SMatthew Dillon 			burst = SWAPB_BURSTING;
3103ffc7051SMatthew Dillon 		}
31164949baaSMatthew Dillon 		if (reached_end == PQ_L2_SIZE) {
31264949baaSMatthew Dillon 			vm_swapcache_inactive_heuristic =
31364949baaSMatthew Dillon 				-vm_swapcache_hysteresis;
31400a3fdcaSMatthew Dillon 		}
31500a3fdcaSMatthew Dillon 	}
316eccc8ca1SMatthew Dillon 
317eccc8ca1SMatthew Dillon 	/*
318eccc8ca1SMatthew Dillon 	 * Cleanup (NOT REACHED)
319eccc8ca1SMatthew Dillon 	 */
32051c99c61SMatthew Dillon 	for (q = 0; q < PQ_L2_SIZE; ++q) {
321027193ebSMatthew Dillon 		vm_page_queues_spin_lock(PQ_INACTIVE + q);
322027193ebSMatthew Dillon 		TAILQ_REMOVE(
323027193ebSMatthew Dillon 			&vm_page_queues[PQ_INACTIVE + q].pl,
324027193ebSMatthew Dillon 			&page_marker[q], pageq);
325027193ebSMatthew Dillon 		vm_page_queues_spin_unlock(PQ_INACTIVE + q);
326027193ebSMatthew Dillon 	}
327eccc8ca1SMatthew Dillon 
328fde6be6aSMatthew Dillon 	lwkt_gettoken(&swindex->token);
329fde6be6aSMatthew Dillon 	TAILQ_REMOVE(&swindex->list, &swmarker, object_list);
330fde6be6aSMatthew Dillon 	lwkt_reltoken(&swindex->token);
33100a3fdcaSMatthew Dillon }
332096e95c0SMatthew Dillon 
333cd8ab232SMatthew Dillon static struct kproc_desc swpc_kp = {
334cd8ab232SMatthew Dillon 	"swapcached",
335cd8ab232SMatthew Dillon 	vm_swapcached_thread,
336cd8ab232SMatthew Dillon 	&swapcached_thread
337cd8ab232SMatthew Dillon };
338f3f3eadbSSascha Wildner SYSINIT(swapcached, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, &swpc_kp);
339cd8ab232SMatthew Dillon 
340096e95c0SMatthew Dillon /*
341fdc53cc7SMatthew Dillon  * Deal with an overflow of the heuristic counter or if the user
342fdc53cc7SMatthew Dillon  * manually changes the hysteresis.
343fdc53cc7SMatthew Dillon  *
344e527fb6bSMatthew Dillon  * Try to avoid small incremental pageouts by waiting for enough
345e527fb6bSMatthew Dillon  * pages to buildup in the inactive queue to hopefully get a good
346e527fb6bSMatthew Dillon  * burst in.  This heuristic is bumped by the VM system and reset
347e527fb6bSMatthew Dillon  * when our scan hits the end of the queue.
34864949baaSMatthew Dillon  *
34964949baaSMatthew Dillon  * Return TRUE if we need to take a writing pass.
350e527fb6bSMatthew Dillon  */
35164949baaSMatthew Dillon static int
35264949baaSMatthew Dillon vm_swapcache_writing_heuristic(void)
35364949baaSMatthew Dillon {
35464949baaSMatthew Dillon 	int hyst;
35564949baaSMatthew Dillon 
35664949baaSMatthew Dillon 	hyst = vmstats.v_inactive_count / 4;
35764949baaSMatthew Dillon 	if (hyst < vm_swapcache_min_hysteresis)
35864949baaSMatthew Dillon 		hyst = vm_swapcache_min_hysteresis;
35964949baaSMatthew Dillon 	cpu_ccfence();
36064949baaSMatthew Dillon 	vm_swapcache_hysteresis = hyst;
36164949baaSMatthew Dillon 
36264949baaSMatthew Dillon 	if (vm_swapcache_inactive_heuristic < -hyst)
36364949baaSMatthew Dillon 		vm_swapcache_inactive_heuristic = -hyst;
36464949baaSMatthew Dillon 
36564949baaSMatthew Dillon 	return (vm_swapcache_inactive_heuristic >= 0);
36664949baaSMatthew Dillon }
36764949baaSMatthew Dillon 
36864949baaSMatthew Dillon /*
36964949baaSMatthew Dillon  * Take a writing pass on one of the inactive queues, return non-zero if
37064949baaSMatthew Dillon  * we hit the end of the queue.
37164949baaSMatthew Dillon  */
37264949baaSMatthew Dillon static int
37364949baaSMatthew Dillon vm_swapcache_writing(vm_page_t marker, int count, int scount)
37464949baaSMatthew Dillon {
37564949baaSMatthew Dillon 	vm_object_t object;
37664949baaSMatthew Dillon 	struct vnode *vp;
37764949baaSMatthew Dillon 	vm_page_t m;
37864949baaSMatthew Dillon 	int isblkdev;
379e527fb6bSMatthew Dillon 
380e527fb6bSMatthew Dillon 	/*
381096e95c0SMatthew Dillon 	 * Scan the inactive queue from our marker to locate
382096e95c0SMatthew Dillon 	 * suitable pages to push to the swap cache.
383096e95c0SMatthew Dillon 	 *
384096e95c0SMatthew Dillon 	 * We are looking for clean vnode-backed pages.
385096e95c0SMatthew Dillon 	 */
386027193ebSMatthew Dillon 	vm_page_queues_spin_lock(marker->queue);
3870bf81261SMatthew Dillon 	while ((m = TAILQ_NEXT(marker, pageq)) != NULL &&
3880bf81261SMatthew Dillon 	       count > 0 && scount-- > 0) {
389027193ebSMatthew Dillon 		KKASSERT(m->queue == marker->queue);
390b12defdcSMatthew Dillon 
3911de864f0SMatthew Dillon 		/*
3921de864f0SMatthew Dillon 		 * Stop using swap if paniced, dumping, or dumped.
3931de864f0SMatthew Dillon 		 * Don't try to write if our curburst has been exhausted.
3941de864f0SMatthew Dillon 		 */
3951de864f0SMatthew Dillon 		if (panicstr || dumping)
3961de864f0SMatthew Dillon 			break;
397b12defdcSMatthew Dillon 		if (vm_swapcache_curburst < 0)
398b12defdcSMatthew Dillon 			break;
3991de864f0SMatthew Dillon 
4001de864f0SMatthew Dillon 		/*
4011de864f0SMatthew Dillon 		 * Move marker
4021de864f0SMatthew Dillon 		 */
403027193ebSMatthew Dillon 		TAILQ_REMOVE(
404027193ebSMatthew Dillon 			&vm_page_queues[marker->queue].pl, marker, pageq);
405027193ebSMatthew Dillon 		TAILQ_INSERT_AFTER(
406027193ebSMatthew Dillon 			&vm_page_queues[marker->queue].pl, m, marker, pageq);
407f5f6d247SMatthew Dillon 
408f5f6d247SMatthew Dillon 		/*
409f5f6d247SMatthew Dillon 		 * Ignore markers and ignore pages that already have a swap
410f5f6d247SMatthew Dillon 		 * assignment.
411f5f6d247SMatthew Dillon 		 */
4120bf81261SMatthew Dillon 		if (m->flags & (PG_MARKER | PG_SWAPPED))
413096e95c0SMatthew Dillon 			continue;
414b12defdcSMatthew Dillon 		if (vm_page_busy_try(m, TRUE))
415096e95c0SMatthew Dillon 			continue;
416027193ebSMatthew Dillon 		vm_page_queues_spin_unlock(marker->queue);
417b12defdcSMatthew Dillon 
418b12defdcSMatthew Dillon 		if ((object = m->object) == NULL) {
419b12defdcSMatthew Dillon 			vm_page_wakeup(m);
420027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
421b12defdcSMatthew Dillon 			continue;
422b12defdcSMatthew Dillon 		}
423b12defdcSMatthew Dillon 		vm_object_hold(object);
424b12defdcSMatthew Dillon 		if (m->object != object) {
425b12defdcSMatthew Dillon 			vm_object_drop(object);
426b12defdcSMatthew Dillon 			vm_page_wakeup(m);
427027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
428b12defdcSMatthew Dillon 			continue;
429b12defdcSMatthew Dillon 		}
430b12defdcSMatthew Dillon 		if (vm_swapcache_test(m)) {
431b12defdcSMatthew Dillon 			vm_object_drop(object);
432b12defdcSMatthew Dillon 			vm_page_wakeup(m);
433027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
434b12defdcSMatthew Dillon 			continue;
435b12defdcSMatthew Dillon 		}
436b12defdcSMatthew Dillon 
437c504e38eSMatthew Dillon 		vp = object->handle;
438b12defdcSMatthew Dillon 		if (vp == NULL) {
439b12defdcSMatthew Dillon 			vm_object_drop(object);
440b12defdcSMatthew Dillon 			vm_page_wakeup(m);
441027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
442c504e38eSMatthew Dillon 			continue;
443b12defdcSMatthew Dillon 		}
444d3070b8dSMatthew Dillon 
445c504e38eSMatthew Dillon 		switch(vp->v_type) {
446c504e38eSMatthew Dillon 		case VREG:
447e9b56058SMatthew Dillon 			/*
448bfa86281SMatthew Dillon 			 * PG_NOTMETA generically means 'don't swapcache this',
449bfa86281SMatthew Dillon 			 * and HAMMER will set this for regular data buffers
450bfa86281SMatthew Dillon 			 * (and leave it unset for meta-data buffers) as
451bfa86281SMatthew Dillon 			 * appropriate when double buffering is enabled.
452bfa86281SMatthew Dillon 			 */
453b12defdcSMatthew Dillon 			if (m->flags & PG_NOTMETA) {
454b12defdcSMatthew Dillon 				vm_object_drop(object);
455b12defdcSMatthew Dillon 				vm_page_wakeup(m);
456027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
457bfa86281SMatthew Dillon 				continue;
458b12defdcSMatthew Dillon 			}
459bfa86281SMatthew Dillon 
460bfa86281SMatthew Dillon 			/*
461e9b56058SMatthew Dillon 			 * If data_enable is 0 do not try to swapcache data.
462e9b56058SMatthew Dillon 			 * If use_chflags is set then only swapcache data for
463e9b56058SMatthew Dillon 			 * VSWAPCACHE marked vnodes, otherwise any vnode.
464e9b56058SMatthew Dillon 			 */
465e9b56058SMatthew Dillon 			if (vm_swapcache_data_enable == 0 ||
466e9b56058SMatthew Dillon 			    ((vp->v_flag & VSWAPCACHE) == 0 &&
467e9b56058SMatthew Dillon 			     vm_swapcache_use_chflags)) {
468b12defdcSMatthew Dillon 				vm_object_drop(object);
469b12defdcSMatthew Dillon 				vm_page_wakeup(m);
470027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
471c504e38eSMatthew Dillon 				continue;
472e9b56058SMatthew Dillon 			}
473d3070b8dSMatthew Dillon 			if (vm_swapcache_maxfilesize &&
474d3070b8dSMatthew Dillon 			    object->size >
475d3070b8dSMatthew Dillon 			    (vm_swapcache_maxfilesize >> PAGE_SHIFT)) {
476b12defdcSMatthew Dillon 				vm_object_drop(object);
477b12defdcSMatthew Dillon 				vm_page_wakeup(m);
478027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
479d3070b8dSMatthew Dillon 				continue;
480d3070b8dSMatthew Dillon 			}
481aabd5ce8SMatthew Dillon 			isblkdev = 0;
482c504e38eSMatthew Dillon 			break;
483c504e38eSMatthew Dillon 		case VCHR:
484aabd5ce8SMatthew Dillon 			/*
485bfa86281SMatthew Dillon 			 * PG_NOTMETA generically means 'don't swapcache this',
486bfa86281SMatthew Dillon 			 * and HAMMER will set this for regular data buffers
487bfa86281SMatthew Dillon 			 * (and leave it unset for meta-data buffers) as
488bfa86281SMatthew Dillon 			 * appropriate when double buffering is enabled.
489aabd5ce8SMatthew Dillon 			 */
490b12defdcSMatthew Dillon 			if (m->flags & PG_NOTMETA) {
491b12defdcSMatthew Dillon 				vm_object_drop(object);
492b12defdcSMatthew Dillon 				vm_page_wakeup(m);
493027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
494aabd5ce8SMatthew Dillon 				continue;
495b12defdcSMatthew Dillon 			}
496b12defdcSMatthew Dillon 			if (vm_swapcache_meta_enable == 0) {
497b12defdcSMatthew Dillon 				vm_object_drop(object);
498b12defdcSMatthew Dillon 				vm_page_wakeup(m);
499027193ebSMatthew Dillon 				vm_page_queues_spin_lock(marker->queue);
500c504e38eSMatthew Dillon 				continue;
501b12defdcSMatthew Dillon 			}
502aabd5ce8SMatthew Dillon 			isblkdev = 1;
503c504e38eSMatthew Dillon 			break;
504c504e38eSMatthew Dillon 		default:
505b12defdcSMatthew Dillon 			vm_object_drop(object);
506b12defdcSMatthew Dillon 			vm_page_wakeup(m);
507027193ebSMatthew Dillon 			vm_page_queues_spin_lock(marker->queue);
508c504e38eSMatthew Dillon 			continue;
509c504e38eSMatthew Dillon 		}
510096e95c0SMatthew Dillon 
511096e95c0SMatthew Dillon 
512096e95c0SMatthew Dillon 		/*
5133ffc7051SMatthew Dillon 		 * Assign swap and initiate I/O.
5143ffc7051SMatthew Dillon 		 *
5153ffc7051SMatthew Dillon 		 * (adjust for the --count which also occurs in the loop)
516096e95c0SMatthew Dillon 		 */
5170bf81261SMatthew Dillon 		count -= vm_swapcached_flush(m, isblkdev);
518096e95c0SMatthew Dillon 
519096e95c0SMatthew Dillon 		/*
520096e95c0SMatthew Dillon 		 * Setup for next loop using marker.
521096e95c0SMatthew Dillon 		 */
522b12defdcSMatthew Dillon 		vm_object_drop(object);
523027193ebSMatthew Dillon 		vm_page_queues_spin_lock(marker->queue);
524096e95c0SMatthew Dillon 	}
5251e5196f0SMatthew Dillon 
5261e5196f0SMatthew Dillon 	/*
527b12defdcSMatthew Dillon 	 * The marker could wind up at the end, which is ok.  If we hit the
528b12defdcSMatthew Dillon 	 * end of the list adjust the heuristic.
5291e5196f0SMatthew Dillon 	 *
5301e5196f0SMatthew Dillon 	 * Earlier inactive pages that were dirty and become clean
5311e5196f0SMatthew Dillon 	 * are typically moved to the end of PQ_INACTIVE by virtue
5321e5196f0SMatthew Dillon 	 * of vfs_vmio_release() when they become unwired from the
5331e5196f0SMatthew Dillon 	 * buffer cache.
5341e5196f0SMatthew Dillon 	 */
535027193ebSMatthew Dillon 	vm_page_queues_spin_unlock(marker->queue);
53664949baaSMatthew Dillon 
53764949baaSMatthew Dillon 	/*
53864949baaSMatthew Dillon 	 * m invalid but can be used to test for NULL
53964949baaSMatthew Dillon 	 */
54064949baaSMatthew Dillon 	return (m == NULL);
541096e95c0SMatthew Dillon }
542096e95c0SMatthew Dillon 
543096e95c0SMatthew Dillon /*
544b12defdcSMatthew Dillon  * Flush the specified page using the swap_pager.  The page
545b12defdcSMatthew Dillon  * must be busied by the caller and its disposition will become
546b12defdcSMatthew Dillon  * the responsibility of this function.
5473ffc7051SMatthew Dillon  *
5483ffc7051SMatthew Dillon  * Try to collect surrounding pages, including pages which may
5493ffc7051SMatthew Dillon  * have already been assigned swap.  Try to cluster within a
5503ffc7051SMatthew Dillon  * contiguous aligned SMAP_META_PAGES (typ 16 x PAGE_SIZE) block
5513ffc7051SMatthew Dillon  * to match what swap_pager_putpages() can do.
5523ffc7051SMatthew Dillon  *
5533ffc7051SMatthew Dillon  * We also want to try to match against the buffer cache blocksize
5543ffc7051SMatthew Dillon  * but we don't really know what it is here.  Since the buffer cache
5553ffc7051SMatthew Dillon  * wires and unwires pages in groups the fact that we skip wired pages
5563ffc7051SMatthew Dillon  * should be sufficient.
5573ffc7051SMatthew Dillon  *
5583ffc7051SMatthew Dillon  * Returns a count of pages we might have flushed (minimum 1)
559096e95c0SMatthew Dillon  */
560096e95c0SMatthew Dillon static
5613ffc7051SMatthew Dillon int
562aabd5ce8SMatthew Dillon vm_swapcached_flush(vm_page_t m, int isblkdev)
563096e95c0SMatthew Dillon {
564096e95c0SMatthew Dillon 	vm_object_t object;
5653ffc7051SMatthew Dillon 	vm_page_t marray[SWAP_META_PAGES];
5663ffc7051SMatthew Dillon 	vm_pindex_t basei;
5673ffc7051SMatthew Dillon 	int rtvals[SWAP_META_PAGES];
5683ffc7051SMatthew Dillon 	int x;
5693ffc7051SMatthew Dillon 	int i;
5703ffc7051SMatthew Dillon 	int j;
5713ffc7051SMatthew Dillon 	int count;
572b12defdcSMatthew Dillon 	int error;
573096e95c0SMatthew Dillon 
574096e95c0SMatthew Dillon 	vm_page_io_start(m);
575096e95c0SMatthew Dillon 	vm_page_protect(m, VM_PROT_READ);
576096e95c0SMatthew Dillon 	object = m->object;
577b12defdcSMatthew Dillon 	vm_object_hold(object);
578096e95c0SMatthew Dillon 
5793ffc7051SMatthew Dillon 	/*
5803ffc7051SMatthew Dillon 	 * Try to cluster around (m), keeping in mind that the swap pager
5813ffc7051SMatthew Dillon 	 * can only do SMAP_META_PAGES worth of continguous write.
5823ffc7051SMatthew Dillon 	 */
5833ffc7051SMatthew Dillon 	x = (int)m->pindex & SWAP_META_MASK;
5843ffc7051SMatthew Dillon 	marray[x] = m;
5853ffc7051SMatthew Dillon 	basei = m->pindex;
586b12defdcSMatthew Dillon 	vm_page_wakeup(m);
5873ffc7051SMatthew Dillon 
5883ffc7051SMatthew Dillon 	for (i = x - 1; i >= 0; --i) {
589b12defdcSMatthew Dillon 		m = vm_page_lookup_busy_try(object, basei - x + i,
590b12defdcSMatthew Dillon 					    TRUE, &error);
591b12defdcSMatthew Dillon 		if (error || m == NULL)
5923ffc7051SMatthew Dillon 			break;
593b12defdcSMatthew Dillon 		if (vm_swapcache_test(m)) {
594b12defdcSMatthew Dillon 			vm_page_wakeup(m);
5953ffc7051SMatthew Dillon 			break;
596b12defdcSMatthew Dillon 		}
597b12defdcSMatthew Dillon 		if (isblkdev && (m->flags & PG_NOTMETA)) {
598b12defdcSMatthew Dillon 			vm_page_wakeup(m);
599aabd5ce8SMatthew Dillon 			break;
600b12defdcSMatthew Dillon 		}
6013ffc7051SMatthew Dillon 		vm_page_io_start(m);
6023ffc7051SMatthew Dillon 		vm_page_protect(m, VM_PROT_READ);
6033ffc7051SMatthew Dillon 		if (m->queue - m->pc == PQ_CACHE) {
6043ffc7051SMatthew Dillon 			vm_page_unqueue_nowakeup(m);
6053ffc7051SMatthew Dillon 			vm_page_deactivate(m);
6063ffc7051SMatthew Dillon 		}
6073ffc7051SMatthew Dillon 		marray[i] = m;
608b12defdcSMatthew Dillon 		vm_page_wakeup(m);
6093ffc7051SMatthew Dillon 	}
6103ffc7051SMatthew Dillon 	++i;
6113ffc7051SMatthew Dillon 
6123ffc7051SMatthew Dillon 	for (j = x + 1; j < SWAP_META_PAGES; ++j) {
613b12defdcSMatthew Dillon 		m = vm_page_lookup_busy_try(object, basei - x + j,
614b12defdcSMatthew Dillon 					    TRUE, &error);
615b12defdcSMatthew Dillon 		if (error || m == NULL)
6163ffc7051SMatthew Dillon 			break;
617b12defdcSMatthew Dillon 		if (vm_swapcache_test(m)) {
618b12defdcSMatthew Dillon 			vm_page_wakeup(m);
6193ffc7051SMatthew Dillon 			break;
620b12defdcSMatthew Dillon 		}
621b12defdcSMatthew Dillon 		if (isblkdev && (m->flags & PG_NOTMETA)) {
622b12defdcSMatthew Dillon 			vm_page_wakeup(m);
623aabd5ce8SMatthew Dillon 			break;
624b12defdcSMatthew Dillon 		}
6253ffc7051SMatthew Dillon 		vm_page_io_start(m);
6263ffc7051SMatthew Dillon 		vm_page_protect(m, VM_PROT_READ);
6273ffc7051SMatthew Dillon 		if (m->queue - m->pc == PQ_CACHE) {
6283ffc7051SMatthew Dillon 			vm_page_unqueue_nowakeup(m);
6293ffc7051SMatthew Dillon 			vm_page_deactivate(m);
6303ffc7051SMatthew Dillon 		}
6313ffc7051SMatthew Dillon 		marray[j] = m;
632b12defdcSMatthew Dillon 		vm_page_wakeup(m);
6333ffc7051SMatthew Dillon 	}
6343ffc7051SMatthew Dillon 
6353ffc7051SMatthew Dillon 	count = j - i;
6363ffc7051SMatthew Dillon 	vm_object_pip_add(object, count);
6373ffc7051SMatthew Dillon 	swap_pager_putpages(object, marray + i, count, FALSE, rtvals + i);
6383ffc7051SMatthew Dillon 	vm_swapcache_write_count += count * PAGE_SIZE;
6393ffc7051SMatthew Dillon 	vm_swapcache_curburst -= count * PAGE_SIZE;
6403ffc7051SMatthew Dillon 
6413ffc7051SMatthew Dillon 	while (i < j) {
6423ffc7051SMatthew Dillon 		if (rtvals[i] != VM_PAGER_PEND) {
643b12defdcSMatthew Dillon 			vm_page_busy_wait(marray[i], FALSE, "swppgfd");
6443ffc7051SMatthew Dillon 			vm_page_io_finish(marray[i]);
645b12defdcSMatthew Dillon 			vm_page_wakeup(marray[i]);
646096e95c0SMatthew Dillon 			vm_object_pip_wakeup(object);
647096e95c0SMatthew Dillon 		}
6483ffc7051SMatthew Dillon 		++i;
6493ffc7051SMatthew Dillon 	}
650b12defdcSMatthew Dillon 	vm_object_drop(object);
6513ffc7051SMatthew Dillon 	return(count);
652096e95c0SMatthew Dillon }
65300a3fdcaSMatthew Dillon 
6543ffc7051SMatthew Dillon /*
6553ffc7051SMatthew Dillon  * Test whether a VM page is suitable for writing to the swapcache.
6563ffc7051SMatthew Dillon  * Does not test m->queue, PG_MARKER, or PG_SWAPPED.
6573ffc7051SMatthew Dillon  *
6583ffc7051SMatthew Dillon  * Returns 0 on success, 1 on failure
6593ffc7051SMatthew Dillon  */
6603ffc7051SMatthew Dillon static int
6613ffc7051SMatthew Dillon vm_swapcache_test(vm_page_t m)
6623ffc7051SMatthew Dillon {
6633ffc7051SMatthew Dillon 	vm_object_t object;
6643ffc7051SMatthew Dillon 
665b12defdcSMatthew Dillon 	if (m->flags & PG_UNMANAGED)
6663ffc7051SMatthew Dillon 		return(1);
667b12defdcSMatthew Dillon 	if (m->hold_count || m->wire_count)
6683ffc7051SMatthew Dillon 		return(1);
6693ffc7051SMatthew Dillon 	if (m->valid != VM_PAGE_BITS_ALL)
6703ffc7051SMatthew Dillon 		return(1);
6713ffc7051SMatthew Dillon 	if (m->dirty & m->valid)
6723ffc7051SMatthew Dillon 		return(1);
6733ffc7051SMatthew Dillon 	if ((object = m->object) == NULL)
6743ffc7051SMatthew Dillon 		return(1);
6753ffc7051SMatthew Dillon 	if (object->type != OBJT_VNODE ||
6763ffc7051SMatthew Dillon 	    (object->flags & OBJ_DEAD)) {
6773ffc7051SMatthew Dillon 		return(1);
6783ffc7051SMatthew Dillon 	}
6793ffc7051SMatthew Dillon 	vm_page_test_dirty(m);
6803ffc7051SMatthew Dillon 	if (m->dirty & m->valid)
6813ffc7051SMatthew Dillon 		return(1);
6823ffc7051SMatthew Dillon 	return(0);
6833ffc7051SMatthew Dillon }
6843ffc7051SMatthew Dillon 
6853ffc7051SMatthew Dillon /*
686f5f6d247SMatthew Dillon  * Cleaning pass.
687f5f6d247SMatthew Dillon  *
688f5f6d247SMatthew Dillon  * We clean whole objects up to 16MB
6893ffc7051SMatthew Dillon  */
69000a3fdcaSMatthew Dillon static
69100a3fdcaSMatthew Dillon void
692fde6be6aSMatthew Dillon vm_swapcache_cleaning(vm_object_t marker, struct vm_object_hash **swindexp)
69300a3fdcaSMatthew Dillon {
69400a3fdcaSMatthew Dillon 	vm_object_t object;
69500a3fdcaSMatthew Dillon 	struct vnode *vp;
69600a3fdcaSMatthew Dillon 	int count;
6970bf81261SMatthew Dillon 	int scount;
69800a3fdcaSMatthew Dillon 	int n;
699*eea5ad68SMatthew Dillon 	int didmove;
70000a3fdcaSMatthew Dillon 
70100a3fdcaSMatthew Dillon 	count = vm_swapcache_maxlaunder;
7020bf81261SMatthew Dillon 	scount = vm_swapcache_maxscan;
70300a3fdcaSMatthew Dillon 
70400a3fdcaSMatthew Dillon 	/*
70500a3fdcaSMatthew Dillon 	 * Look for vnode objects
70600a3fdcaSMatthew Dillon 	 */
707fde6be6aSMatthew Dillon 	lwkt_gettoken(&(*swindexp)->token);
7082de4f77eSMatthew Dillon 
709*eea5ad68SMatthew Dillon 	didmove = 0;
710b2286ce7SMatthew Dillon outerloop:
711f5f6d247SMatthew Dillon 	while ((object = TAILQ_NEXT(marker, object_list)) != NULL) {
7122f2d9e58SVenkatesh Srinivas 		/*
713f5f6d247SMatthew Dillon 		 * We have to skip markers.  We cannot hold/drop marker
714f5f6d247SMatthew Dillon 		 * objects!
7152f2d9e58SVenkatesh Srinivas 		 */
716f5f6d247SMatthew Dillon 		if (object->type == OBJT_MARKER) {
7177b00fbb4SMatthew Dillon 			vm_swapcache_movemarker(marker, *swindexp, object);
718*eea5ad68SMatthew Dillon 			didmove = 1;
71900a3fdcaSMatthew Dillon 			continue;
7202f2d9e58SVenkatesh Srinivas 		}
72100a3fdcaSMatthew Dillon 
72200a3fdcaSMatthew Dillon 		/*
723f5f6d247SMatthew Dillon 		 * Safety, or in case there are millions of VM objects
724f5f6d247SMatthew Dillon 		 * without swapcache backing.
72500a3fdcaSMatthew Dillon 		 */
7260bf81261SMatthew Dillon 		if (--scount <= 0)
7277b00fbb4SMatthew Dillon 			goto breakout;
72800a3fdcaSMatthew Dillon 
72900a3fdcaSMatthew Dillon 		/*
730f5f6d247SMatthew Dillon 		 * We must hold the object before potentially yielding.
73100a3fdcaSMatthew Dillon 		 */
732f5f6d247SMatthew Dillon 		vm_object_hold(object);
733f5f6d247SMatthew Dillon 		lwkt_yield();
734f5f6d247SMatthew Dillon 
735f5f6d247SMatthew Dillon 		/*
736f5f6d247SMatthew Dillon 		 * Only operate on live VNODE objects that are either
737f5f6d247SMatthew Dillon 		 * VREG or VCHR (VCHR for meta-data).
738f5f6d247SMatthew Dillon 		 */
739f5f6d247SMatthew Dillon 		if ((object->type != OBJT_VNODE) ||
740f5f6d247SMatthew Dillon 		    ((object->flags & OBJ_DEAD) ||
741f5f6d247SMatthew Dillon 		     object->swblock_count == 0) ||
742f5f6d247SMatthew Dillon 		    ((vp = object->handle) == NULL) ||
743f5f6d247SMatthew Dillon 		    (vp->v_type != VREG && vp->v_type != VCHR)) {
744f5f6d247SMatthew Dillon 			vm_object_drop(object);
745f5f6d247SMatthew Dillon 			/* object may be invalid now */
7467b00fbb4SMatthew Dillon 			vm_swapcache_movemarker(marker, *swindexp, object);
747*eea5ad68SMatthew Dillon 			didmove = 1;
748f5f6d247SMatthew Dillon 			continue;
749f5f6d247SMatthew Dillon 		}
750f5f6d247SMatthew Dillon 
751f5f6d247SMatthew Dillon 		/*
752f5f6d247SMatthew Dillon 		 * Reset the object pindex stored in the marker if the
753f5f6d247SMatthew Dillon 		 * working object has changed.
754f5f6d247SMatthew Dillon 		 */
755*eea5ad68SMatthew Dillon 		if (marker->backing_object != object || didmove) {
756f5f6d247SMatthew Dillon 			marker->size = 0;
757f5f6d247SMatthew Dillon 			marker->backing_object_offset = 0;
758f5f6d247SMatthew Dillon 			marker->backing_object = object;
759*eea5ad68SMatthew Dillon 			didmove = 0;
760f5f6d247SMatthew Dillon 		}
76100a3fdcaSMatthew Dillon 
76200a3fdcaSMatthew Dillon 		/*
76300a3fdcaSMatthew Dillon 		 * Look for swblocks starting at our iterator.
76400a3fdcaSMatthew Dillon 		 *
76500a3fdcaSMatthew Dillon 		 * The swap_pager_condfree() function attempts to free
76600a3fdcaSMatthew Dillon 		 * swap space starting at the specified index.  The index
76700a3fdcaSMatthew Dillon 		 * will be updated on return.  The function will return
76800a3fdcaSMatthew Dillon 		 * a scan factor (NOT the number of blocks freed).
76900a3fdcaSMatthew Dillon 		 *
77000a3fdcaSMatthew Dillon 		 * If it must cut its scan of the object short due to an
77100a3fdcaSMatthew Dillon 		 * excessive number of swblocks, or is able to free the
77200a3fdcaSMatthew Dillon 		 * requested number of blocks, it will return n >= count
77300a3fdcaSMatthew Dillon 		 * and we break and pick it back up on a future attempt.
774f5f6d247SMatthew Dillon 		 *
775f5f6d247SMatthew Dillon 		 * Scan the object linearly and try to batch large sets of
776f5f6d247SMatthew Dillon 		 * blocks that are likely to clean out entire swap radix
777f5f6d247SMatthew Dillon 		 * tree leafs.
77800a3fdcaSMatthew Dillon 		 */
779739be60bSMatthew Dillon 		lwkt_token_swap();
780fde6be6aSMatthew Dillon 		lwkt_reltoken(&(*swindexp)->token);
78127b6ee03SMatthew Dillon 
782f5f6d247SMatthew Dillon 		n = swap_pager_condfree(object, &marker->size,
783f5f6d247SMatthew Dillon 				    (count + SWAP_META_MASK) & ~SWAP_META_MASK);
7842f2d9e58SVenkatesh Srinivas 
785f5f6d247SMatthew Dillon 		vm_object_drop(object);		/* object may be invalid now */
786fde6be6aSMatthew Dillon 		lwkt_gettoken(&(*swindexp)->token);
7872f2d9e58SVenkatesh Srinivas 
78800a3fdcaSMatthew Dillon 		/*
789f5f6d247SMatthew Dillon 		 * If we have exhausted the object or deleted our per-pass
790f5f6d247SMatthew Dillon 		 * page limit then move us to the next object.  Note that
791f5f6d247SMatthew Dillon 		 * the current object may no longer be on the vm_object_list.
79200a3fdcaSMatthew Dillon 		 */
793f5f6d247SMatthew Dillon 		if (n <= 0 ||
794f5f6d247SMatthew Dillon 		    marker->backing_object_offset > vm_swapcache_cleanperobj) {
7957b00fbb4SMatthew Dillon 			vm_swapcache_movemarker(marker, *swindexp, object);
796*eea5ad68SMatthew Dillon 			didmove = 1;
79700a3fdcaSMatthew Dillon 		}
79800a3fdcaSMatthew Dillon 
79900a3fdcaSMatthew Dillon 		/*
800f5f6d247SMatthew Dillon 		 * If we have exhausted our max-launder stop for now.
80100a3fdcaSMatthew Dillon 		 */
802f5f6d247SMatthew Dillon 		count -= n;
803f5f6d247SMatthew Dillon 		marker->backing_object_offset += n * PAGE_SIZE;
804f5f6d247SMatthew Dillon 		if (count < 0)
8057b00fbb4SMatthew Dillon 			goto breakout;
806f5f6d247SMatthew Dillon 	}
8077a175765SMatthew Dillon 
8087a175765SMatthew Dillon 	/*
8097b00fbb4SMatthew Dillon 	 * Iterate vm_object_lists[] hash table
8107a175765SMatthew Dillon 	 */
811fde6be6aSMatthew Dillon 	TAILQ_REMOVE(&(*swindexp)->list, marker, object_list);
812fde6be6aSMatthew Dillon 	lwkt_reltoken(&(*swindexp)->token);
813fde6be6aSMatthew Dillon 	if (++*swindexp >= &vm_object_hash[VMOBJ_HSIZE])
814fde6be6aSMatthew Dillon 		*swindexp = &vm_object_hash[0];
815fde6be6aSMatthew Dillon 	lwkt_gettoken(&(*swindexp)->token);
816fde6be6aSMatthew Dillon 	TAILQ_INSERT_HEAD(&(*swindexp)->list, marker, object_list);
8177a175765SMatthew Dillon 
818fde6be6aSMatthew Dillon 	if (*swindexp != &vm_object_hash[0])
8197b00fbb4SMatthew Dillon 		goto outerloop;
8207b00fbb4SMatthew Dillon 
8217b00fbb4SMatthew Dillon breakout:
822fde6be6aSMatthew Dillon 	lwkt_reltoken(&(*swindexp)->token);
82300a3fdcaSMatthew Dillon }
824f5f6d247SMatthew Dillon 
825f5f6d247SMatthew Dillon /*
826f5f6d247SMatthew Dillon  * Move the marker past the current object.  Object can be stale, but we
827f5f6d247SMatthew Dillon  * still need it to determine if the marker has to be moved.  If the object
828f5f6d247SMatthew Dillon  * is still the 'current object' (object after the marker), we hop-scotch
829f5f6d247SMatthew Dillon  * the marker past it.
830f5f6d247SMatthew Dillon  */
831f5f6d247SMatthew Dillon static void
832fde6be6aSMatthew Dillon vm_swapcache_movemarker(vm_object_t marker, struct vm_object_hash *swindex,
833fde6be6aSMatthew Dillon 			vm_object_t object)
834f5f6d247SMatthew Dillon {
835f5f6d247SMatthew Dillon 	if (TAILQ_NEXT(marker, object_list) == object) {
836fde6be6aSMatthew Dillon 		TAILQ_REMOVE(&swindex->list, marker, object_list);
837fde6be6aSMatthew Dillon 		TAILQ_INSERT_AFTER(&swindex->list, object, marker, object_list);
838f5f6d247SMatthew Dillon 	}
839f5f6d247SMatthew Dillon }
840