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