kernel - More many-cores SMP work
[dragonfly.git] / sys / vm / vm_swapcache.c
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
5  *
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@backplane.com>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  * 3. Neither the name of The DragonFly Project nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific, prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
27  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 /*
38  * Implement the swapcache daemon.  When enabled swap is assumed to be
39  * configured on a fast storage device such as a SSD.  Swap is assigned
40  * to clean vnode-backed pages in the inactive queue, clustered by object
41  * if possible, and written out.  The swap assignment sticks around even
42  * after the underlying pages have been recycled.
43  *
44  * The daemon manages write bandwidth based on sysctl settings to control
45  * wear on the SSD.
46  *
47  * The vnode strategy code will check for the swap assignments and divert
48  * reads to the swap device when the data is present in the swapcache.
49  *
50  * This operates on both regular files and the block device vnodes used by
51  * filesystems to manage meta-data.
52  */
53
54 #include "opt_vm.h"
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/proc.h>
59 #include <sys/kthread.h>
60 #include <sys/resourcevar.h>
61 #include <sys/signalvar.h>
62 #include <sys/vnode.h>
63 #include <sys/vmmeter.h>
64 #include <sys/sysctl.h>
65 #include <sys/eventhandler.h>
66
67 #include <vm/vm.h>
68 #include <vm/vm_param.h>
69 #include <sys/lock.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_pageout.h>
74 #include <vm/vm_pager.h>
75 #include <vm/swap_pager.h>
76 #include <vm/vm_extern.h>
77
78 #include <sys/thread2.h>
79 #include <sys/spinlock2.h>
80 #include <vm/vm_page2.h>
81
82 #define INACTIVE_LIST   (&vm_page_queues[PQ_INACTIVE].pl)
83
84 /* the kernel process "vm_pageout"*/
85 static int vm_swapcached_flush (vm_page_t m, int isblkdev);
86 static int vm_swapcache_test(vm_page_t m);
87 static void vm_swapcache_writing(vm_page_t marker);
88 static void vm_swapcache_cleaning(vm_object_t marker);
89 struct thread *swapcached_thread;
90
91 SYSCTL_NODE(_vm, OID_AUTO, swapcache, CTLFLAG_RW, NULL, NULL);
92
93 int vm_swapcache_read_enable;
94 int vm_swapcache_inactive_heuristic;
95 static int vm_swapcache_sleep;
96 static int vm_swapcache_maxlaunder = 256;
97 static int vm_swapcache_data_enable = 0;
98 static int vm_swapcache_meta_enable = 0;
99 static int vm_swapcache_maxswappct = 75;
100 static int vm_swapcache_hysteresis;
101 int vm_swapcache_use_chflags = 1;       /* require chflags cache */
102 static int64_t vm_swapcache_minburst = 10000000LL;      /* 10MB */
103 static int64_t vm_swapcache_curburst = 4000000000LL;    /* 4G after boot */
104 static int64_t vm_swapcache_maxburst = 2000000000LL;    /* 2G nominal max */
105 static int64_t vm_swapcache_accrate = 100000LL;         /* 100K/s */
106 static int64_t vm_swapcache_write_count;
107 static int64_t vm_swapcache_maxfilesize;
108
109 SYSCTL_INT(_vm_swapcache, OID_AUTO, maxlaunder,
110         CTLFLAG_RW, &vm_swapcache_maxlaunder, 0, "");
111
112 SYSCTL_INT(_vm_swapcache, OID_AUTO, data_enable,
113         CTLFLAG_RW, &vm_swapcache_data_enable, 0, "");
114 SYSCTL_INT(_vm_swapcache, OID_AUTO, meta_enable,
115         CTLFLAG_RW, &vm_swapcache_meta_enable, 0, "");
116 SYSCTL_INT(_vm_swapcache, OID_AUTO, read_enable,
117         CTLFLAG_RW, &vm_swapcache_read_enable, 0, "");
118 SYSCTL_INT(_vm_swapcache, OID_AUTO, maxswappct,
119         CTLFLAG_RW, &vm_swapcache_maxswappct, 0, "");
120 SYSCTL_INT(_vm_swapcache, OID_AUTO, hysteresis,
121         CTLFLAG_RW, &vm_swapcache_hysteresis, 0, "");
122 SYSCTL_INT(_vm_swapcache, OID_AUTO, use_chflags,
123         CTLFLAG_RW, &vm_swapcache_use_chflags, 0, "");
124
125 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, minburst,
126         CTLFLAG_RW, &vm_swapcache_minburst, 0, "");
127 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, curburst,
128         CTLFLAG_RW, &vm_swapcache_curburst, 0, "");
129 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxburst,
130         CTLFLAG_RW, &vm_swapcache_maxburst, 0, "");
131 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxfilesize,
132         CTLFLAG_RW, &vm_swapcache_maxfilesize, 0, "");
133 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, accrate,
134         CTLFLAG_RW, &vm_swapcache_accrate, 0, "");
135 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, write_count,
136         CTLFLAG_RW, &vm_swapcache_write_count, 0, "");
137
138 #define SWAPMAX(adj)    \
139         ((int64_t)vm_swap_max * (vm_swapcache_maxswappct + (adj)) / 100)
140
141 /*
142  * When shutting down the machine we want to stop swapcache operation
143  * immediately so swap is not accessed after devices have been shuttered.
144  */
145 static void
146 shutdown_swapcache(void *arg __unused)
147 {
148         vm_swapcache_read_enable = 0;
149         vm_swapcache_data_enable = 0;
150         vm_swapcache_meta_enable = 0;
151         wakeup(&vm_swapcache_sleep);    /* shortcut 5-second wait */
152 }
153
154 /*
155  * vm_swapcached is the high level pageout daemon.
156  *
157  * No requirements.
158  */
159 static void
160 vm_swapcached_thread(void)
161 {
162         enum { SWAPC_WRITING, SWAPC_CLEANING } state = SWAPC_WRITING;
163         enum { SWAPB_BURSTING, SWAPB_RECOVERING } burst = SWAPB_BURSTING;
164         struct vm_page page_marker;
165         struct vm_object object_marker;
166
167         /*
168          * Thread setup
169          */
170         curthread->td_flags |= TDF_SYSTHREAD;
171         EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc,
172                               swapcached_thread, SHUTDOWN_PRI_FIRST);
173         EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_swapcache,
174                               NULL, SHUTDOWN_PRI_SECOND);
175
176         /*
177          * Initialize our marker for the inactive scan (SWAPC_WRITING)
178          */
179         bzero(&page_marker, sizeof(page_marker));
180         page_marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
181         page_marker.queue = PQ_INACTIVE;
182         page_marker.wire_count = 1;
183
184         vm_page_queues_spin_lock(PQ_INACTIVE);
185         TAILQ_INSERT_HEAD(INACTIVE_LIST, &page_marker, pageq);
186         vm_page_queues_spin_unlock(PQ_INACTIVE);
187
188         vm_swapcache_hysteresis = vmstats.v_inactive_target / 2;
189         vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
190
191         /*
192          * Initialize our marker for the vm_object scan (SWAPC_CLEANING)
193          */
194         bzero(&object_marker, sizeof(object_marker));
195         object_marker.type = OBJT_MARKER;
196         lwkt_gettoken(&vmobj_token);
197         TAILQ_INSERT_HEAD(&vm_object_list, &object_marker, object_list);
198         lwkt_reltoken(&vmobj_token);
199
200         for (;;) {
201                 /*
202                  * Handle shutdown
203                  */
204                 kproc_suspend_loop();
205
206                 /*
207                  * Check every 5 seconds when not enabled or if no swap
208                  * is present.
209                  */
210                 if ((vm_swapcache_data_enable == 0 &&
211                      vm_swapcache_meta_enable == 0) ||
212                     vm_swap_max == 0) {
213                         tsleep(&vm_swapcache_sleep, 0, "csleep", hz * 5);
214                         continue;
215                 }
216
217                 /*
218                  * Polling rate when enabled is approximately 10 hz.
219                  */
220                 tsleep(&vm_swapcache_sleep, 0, "csleep", hz / 10);
221
222                 /*
223                  * State hysteresis.  Generate write activity up to 75% of
224                  * swap, then clean out swap assignments down to 70%, then
225                  * repeat.
226                  */
227                 if (state == SWAPC_WRITING) {
228                         if (vm_swap_cache_use > SWAPMAX(0))
229                                 state = SWAPC_CLEANING;
230                 } else {
231                         if (vm_swap_cache_use < SWAPMAX(-5))
232                                 state = SWAPC_WRITING;
233                 }
234
235                 /*
236                  * We are allowed to continue accumulating burst value
237                  * in either state.  Allow the user to set curburst > maxburst
238                  * for the initial load-in.
239                  */
240                 if (vm_swapcache_curburst < vm_swapcache_maxburst) {
241                         vm_swapcache_curburst += vm_swapcache_accrate / 10;
242                         if (vm_swapcache_curburst > vm_swapcache_maxburst)
243                                 vm_swapcache_curburst = vm_swapcache_maxburst;
244                 }
245
246                 /*
247                  * We don't want to nickle-and-dime the scan as that will
248                  * create unnecessary fragmentation.  The minimum burst
249                  * is one-seconds worth of accumulation.
250                  */
251                 if (state == SWAPC_WRITING) {
252                         if (vm_swapcache_curburst >= vm_swapcache_accrate) {
253                                 if (burst == SWAPB_BURSTING) {
254                                         vm_swapcache_writing(&page_marker);
255                                         if (vm_swapcache_curburst <= 0)
256                                                 burst = SWAPB_RECOVERING;
257                                 } else if (vm_swapcache_curburst >
258                                            vm_swapcache_minburst) {
259                                         vm_swapcache_writing(&page_marker);
260                                         burst = SWAPB_BURSTING;
261                                 }
262                         }
263                 } else {
264                         vm_swapcache_cleaning(&object_marker);
265                 }
266         }
267
268         /*
269          * Cleanup (NOT REACHED)
270          */
271         vm_page_queues_spin_lock(PQ_INACTIVE);
272         TAILQ_REMOVE(INACTIVE_LIST, &page_marker, pageq);
273         vm_page_queues_spin_unlock(PQ_INACTIVE);
274
275         lwkt_gettoken(&vmobj_token);
276         TAILQ_REMOVE(&vm_object_list, &object_marker, object_list);
277         lwkt_reltoken(&vmobj_token);
278 }
279
280 static struct kproc_desc swpc_kp = {
281         "swapcached",
282         vm_swapcached_thread,
283         &swapcached_thread
284 };
285 SYSINIT(swapcached, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, &swpc_kp)
286
287 static void
288 vm_swapcache_writing(vm_page_t marker)
289 {
290         vm_object_t object;
291         struct vnode *vp;
292         vm_page_t m;
293         int count;
294         int isblkdev;
295
296         /*
297          * Deal with an overflow of the heuristic counter or if the user
298          * manually changes the hysteresis.
299          *
300          * Try to avoid small incremental pageouts by waiting for enough
301          * pages to buildup in the inactive queue to hopefully get a good
302          * burst in.  This heuristic is bumped by the VM system and reset
303          * when our scan hits the end of the queue.
304          */
305         if (vm_swapcache_inactive_heuristic < -vm_swapcache_hysteresis)
306                 vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
307         if (vm_swapcache_inactive_heuristic < 0)
308                 return;
309
310         /*
311          * Scan the inactive queue from our marker to locate
312          * suitable pages to push to the swap cache.
313          *
314          * We are looking for clean vnode-backed pages.
315          *
316          * NOTE: PG_SWAPPED pages in particular are not part of
317          *       our count because once the cache stabilizes we
318          *       can end up with a very high datarate of VM pages
319          *       cycling from it.
320          */
321         count = vm_swapcache_maxlaunder;
322
323         vm_page_queues_spin_lock(PQ_INACTIVE);
324         while ((m = TAILQ_NEXT(marker, pageq)) != NULL && count-- > 0) {
325                 KKASSERT(m->queue == PQ_INACTIVE);
326
327                 if (vm_swapcache_curburst < 0)
328                         break;
329                 TAILQ_REMOVE(INACTIVE_LIST, marker, pageq);
330                 TAILQ_INSERT_AFTER(INACTIVE_LIST, m, marker, pageq);
331                 if (m->flags & (PG_MARKER | PG_SWAPPED)) {
332                         ++count;
333                         continue;
334                 }
335                 if (vm_page_busy_try(m, TRUE))
336                         continue;
337                 vm_page_queues_spin_unlock(PQ_INACTIVE);
338
339                 if ((object = m->object) == NULL) {
340                         vm_page_wakeup(m);
341                         vm_page_queues_spin_lock(PQ_INACTIVE);
342                         continue;
343                 }
344                 vm_object_hold(object);
345                 if (m->object != object) {
346                         vm_object_drop(object);
347                         vm_page_wakeup(m);
348                         vm_page_queues_spin_lock(PQ_INACTIVE);
349                         continue;
350                 }
351                 if (vm_swapcache_test(m)) {
352                         vm_object_drop(object);
353                         vm_page_wakeup(m);
354                         vm_page_queues_spin_lock(PQ_INACTIVE);
355                         continue;
356                 }
357
358                 vp = object->handle;
359                 if (vp == NULL) {
360                         vm_object_drop(object);
361                         vm_page_wakeup(m);
362                         vm_page_queues_spin_lock(PQ_INACTIVE);
363                         continue;
364                 }
365
366                 switch(vp->v_type) {
367                 case VREG:
368                         /*
369                          * PG_NOTMETA generically means 'don't swapcache this',
370                          * and HAMMER will set this for regular data buffers
371                          * (and leave it unset for meta-data buffers) as
372                          * appropriate when double buffering is enabled.
373                          */
374                         if (m->flags & PG_NOTMETA) {
375                                 vm_object_drop(object);
376                                 vm_page_wakeup(m);
377                                 vm_page_queues_spin_lock(PQ_INACTIVE);
378                                 continue;
379                         }
380
381                         /*
382                          * If data_enable is 0 do not try to swapcache data.
383                          * If use_chflags is set then only swapcache data for
384                          * VSWAPCACHE marked vnodes, otherwise any vnode.
385                          */
386                         if (vm_swapcache_data_enable == 0 ||
387                             ((vp->v_flag & VSWAPCACHE) == 0 &&
388                              vm_swapcache_use_chflags)) {
389                                 vm_object_drop(object);
390                                 vm_page_wakeup(m);
391                                 vm_page_queues_spin_lock(PQ_INACTIVE);
392                                 continue;
393                         }
394                         if (vm_swapcache_maxfilesize &&
395                             object->size >
396                             (vm_swapcache_maxfilesize >> PAGE_SHIFT)) {
397                                 vm_object_drop(object);
398                                 vm_page_wakeup(m);
399                                 vm_page_queues_spin_lock(PQ_INACTIVE);
400                                 continue;
401                         }
402                         isblkdev = 0;
403                         break;
404                 case VCHR:
405                         /*
406                          * PG_NOTMETA generically means 'don't swapcache this',
407                          * and HAMMER will set this for regular data buffers
408                          * (and leave it unset for meta-data buffers) as
409                          * appropriate when double buffering is enabled.
410                          */
411                         if (m->flags & PG_NOTMETA) {
412                                 vm_object_drop(object);
413                                 vm_page_wakeup(m);
414                                 vm_page_queues_spin_lock(PQ_INACTIVE);
415                                 continue;
416                         }
417                         if (vm_swapcache_meta_enable == 0) {
418                                 vm_object_drop(object);
419                                 vm_page_wakeup(m);
420                                 vm_page_queues_spin_lock(PQ_INACTIVE);
421                                 continue;
422                         }
423                         isblkdev = 1;
424                         break;
425                 default:
426                         vm_object_drop(object);
427                         vm_page_wakeup(m);
428                         vm_page_queues_spin_lock(PQ_INACTIVE);
429                         continue;
430                 }
431
432
433                 /*
434                  * Assign swap and initiate I/O.
435                  *
436                  * (adjust for the --count which also occurs in the loop)
437                  */
438                 count -= vm_swapcached_flush(m, isblkdev) - 1;
439
440                 /*
441                  * Setup for next loop using marker.
442                  */
443                 vm_object_drop(object);
444                 vm_page_queues_spin_lock(PQ_INACTIVE);
445         }
446
447         /*
448          * The marker could wind up at the end, which is ok.  If we hit the
449          * end of the list adjust the heuristic.
450          *
451          * Earlier inactive pages that were dirty and become clean
452          * are typically moved to the end of PQ_INACTIVE by virtue
453          * of vfs_vmio_release() when they become unwired from the
454          * buffer cache.
455          */
456         if (m == NULL)
457                 vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
458         vm_page_queues_spin_unlock(PQ_INACTIVE);
459 }
460
461 /*
462  * Flush the specified page using the swap_pager.  The page
463  * must be busied by the caller and its disposition will become
464  * the responsibility of this function.
465  *
466  * Try to collect surrounding pages, including pages which may
467  * have already been assigned swap.  Try to cluster within a
468  * contiguous aligned SMAP_META_PAGES (typ 16 x PAGE_SIZE) block
469  * to match what swap_pager_putpages() can do.
470  *
471  * We also want to try to match against the buffer cache blocksize
472  * but we don't really know what it is here.  Since the buffer cache
473  * wires and unwires pages in groups the fact that we skip wired pages
474  * should be sufficient.
475  *
476  * Returns a count of pages we might have flushed (minimum 1)
477  */
478 static
479 int
480 vm_swapcached_flush(vm_page_t m, int isblkdev)
481 {
482         vm_object_t object;
483         vm_page_t marray[SWAP_META_PAGES];
484         vm_pindex_t basei;
485         int rtvals[SWAP_META_PAGES];
486         int x;
487         int i;
488         int j;
489         int count;
490         int error;
491
492         vm_page_io_start(m);
493         vm_page_protect(m, VM_PROT_READ);
494         object = m->object;
495         vm_object_hold(object);
496
497         /*
498          * Try to cluster around (m), keeping in mind that the swap pager
499          * can only do SMAP_META_PAGES worth of continguous write.
500          */
501         x = (int)m->pindex & SWAP_META_MASK;
502         marray[x] = m;
503         basei = m->pindex;
504         vm_page_wakeup(m);
505
506         for (i = x - 1; i >= 0; --i) {
507                 m = vm_page_lookup_busy_try(object, basei - x + i,
508                                             TRUE, &error);
509                 if (error || m == NULL)
510                         break;
511                 if (vm_swapcache_test(m)) {
512                         vm_page_wakeup(m);
513                         break;
514                 }
515                 if (isblkdev && (m->flags & PG_NOTMETA)) {
516                         vm_page_wakeup(m);
517                         break;
518                 }
519                 vm_page_io_start(m);
520                 vm_page_protect(m, VM_PROT_READ);
521                 if (m->queue - m->pc == PQ_CACHE) {
522                         vm_page_unqueue_nowakeup(m);
523                         vm_page_deactivate(m);
524                 }
525                 marray[i] = m;
526                 vm_page_wakeup(m);
527         }
528         ++i;
529
530         for (j = x + 1; j < SWAP_META_PAGES; ++j) {
531                 m = vm_page_lookup_busy_try(object, basei - x + j,
532                                             TRUE, &error);
533                 if (error || m == NULL)
534                         break;
535                 if (vm_swapcache_test(m)) {
536                         vm_page_wakeup(m);
537                         break;
538                 }
539                 if (isblkdev && (m->flags & PG_NOTMETA)) {
540                         vm_page_wakeup(m);
541                         break;
542                 }
543                 vm_page_io_start(m);
544                 vm_page_protect(m, VM_PROT_READ);
545                 if (m->queue - m->pc == PQ_CACHE) {
546                         vm_page_unqueue_nowakeup(m);
547                         vm_page_deactivate(m);
548                 }
549                 marray[j] = m;
550                 vm_page_wakeup(m);
551         }
552
553         count = j - i;
554         vm_object_pip_add(object, count);
555         swap_pager_putpages(object, marray + i, count, FALSE, rtvals + i);
556         vm_swapcache_write_count += count * PAGE_SIZE;
557         vm_swapcache_curburst -= count * PAGE_SIZE;
558
559         while (i < j) {
560                 if (rtvals[i] != VM_PAGER_PEND) {
561                         vm_page_busy_wait(marray[i], FALSE, "swppgfd");
562                         vm_page_io_finish(marray[i]);
563                         vm_page_wakeup(marray[i]);
564                         vm_object_pip_wakeup(object);
565                 }
566                 ++i;
567         }
568         vm_object_drop(object);
569         return(count);
570 }
571
572 /*
573  * Test whether a VM page is suitable for writing to the swapcache.
574  * Does not test m->queue, PG_MARKER, or PG_SWAPPED.
575  *
576  * Returns 0 on success, 1 on failure
577  */
578 static int
579 vm_swapcache_test(vm_page_t m)
580 {
581         vm_object_t object;
582
583         if (m->flags & PG_UNMANAGED)
584                 return(1);
585         if (m->hold_count || m->wire_count)
586                 return(1);
587         if (m->valid != VM_PAGE_BITS_ALL)
588                 return(1);
589         if (m->dirty & m->valid)
590                 return(1);
591         if ((object = m->object) == NULL)
592                 return(1);
593         if (object->type != OBJT_VNODE ||
594             (object->flags & OBJ_DEAD)) {
595                 return(1);
596         }
597         vm_page_test_dirty(m);
598         if (m->dirty & m->valid)
599                 return(1);
600         return(0);
601 }
602
603 /*
604  * Cleaning pass
605  */
606 static
607 void
608 vm_swapcache_cleaning(vm_object_t marker)
609 {
610         vm_object_t object;
611         struct vnode *vp;
612         int count;
613         int n;
614
615         object = marker;
616         count = vm_swapcache_maxlaunder;
617
618         /*
619          * Look for vnode objects
620          */
621         lwkt_gettoken(&vmobj_token);
622
623         while ((object = TAILQ_NEXT(object, object_list)) != NULL) {
624                 lwkt_yield();
625                 if (--count <= 0)
626                         break;
627                 vm_object_hold(object);
628
629                 /* 
630                  * Only operate on live VNODE objects with regular/chardev types
631                  */
632                 if ((object->type != OBJT_VNODE) ||
633                     ((object->flags & OBJ_DEAD) || object->swblock_count == 0) ||
634                     ((vp = object->handle) == NULL) ||
635                     (vp->v_type != VREG && vp->v_type != VCHR)) {
636                         vm_object_drop(object);
637                         continue;
638                 }
639
640                 /*
641                  * Adjust iterator.
642                  */
643                 if (marker->backing_object != object)
644                         marker->size = 0;
645
646                 /*
647                  * Move the marker so we can work on the VM object
648                  */
649                 TAILQ_REMOVE(&vm_object_list, marker, object_list);
650                 TAILQ_INSERT_AFTER(&vm_object_list, object,
651                                    marker, object_list);
652
653                 /*
654                  * Look for swblocks starting at our iterator.
655                  *
656                  * The swap_pager_condfree() function attempts to free
657                  * swap space starting at the specified index.  The index
658                  * will be updated on return.  The function will return
659                  * a scan factor (NOT the number of blocks freed).
660                  *
661                  * If it must cut its scan of the object short due to an
662                  * excessive number of swblocks, or is able to free the
663                  * requested number of blocks, it will return n >= count
664                  * and we break and pick it back up on a future attempt.
665                  */
666                 n = swap_pager_condfree(object, &marker->size, count);
667
668                 vm_object_drop(object);
669         
670                 count -= n;
671                 if (count < 0) 
672                         break;
673
674                 /*
675                  * Setup for loop.
676                  */
677                 marker->size = 0;
678                 object = marker;
679         }
680
681         /*
682          * Adjust marker so we continue the scan from where we left off.
683          * When we reach the end we start back at the beginning.
684          */
685         TAILQ_REMOVE(&vm_object_list, marker, object_list);
686         if (object)
687                 TAILQ_INSERT_BEFORE(object, marker, object_list);
688         else
689                 TAILQ_INSERT_HEAD(&vm_object_list, marker, object_list);
690         marker->backing_object = object;
691
692         lwkt_reltoken(&vmobj_token);
693 }