]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/swap_pager.c
Extract eventfilter declarations to sys/_eventfilter.h
[FreeBSD/FreeBSD.git] / sys / vm / swap_pager.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1998 Matthew Dillon,
5  * Copyright (c) 1994 John S. Dyson
6  * Copyright (c) 1990 University of Utah.
7  * Copyright (c) 1982, 1986, 1989, 1993
8  *      The Regents of the University of California.  All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * the Systems Programming Group of the University of Utah Computer
12  * Science Department.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *      This product includes software developed by the University of
25  *      California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *                              New Swap System
43  *                              Matthew Dillon
44  *
45  * Radix Bitmap 'blists'.
46  *
47  *      - The new swapper uses the new radix bitmap code.  This should scale
48  *        to arbitrarily small or arbitrarily large swap spaces and an almost
49  *        arbitrary degree of fragmentation.
50  *
51  * Features:
52  *
53  *      - on the fly reallocation of swap during putpages.  The new system
54  *        does not try to keep previously allocated swap blocks for dirty
55  *        pages.
56  *
57  *      - on the fly deallocation of swap
58  *
59  *      - No more garbage collection required.  Unnecessarily allocated swap
60  *        blocks only exist for dirty vm_page_t's now and these are already
61  *        cycled (in a high-load system) by the pager.  We also do on-the-fly
62  *        removal of invalidated swap blocks when a page is destroyed
63  *        or renamed.
64  *
65  * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$
66  *
67  *      @(#)swap_pager.c        8.9 (Berkeley) 3/21/94
68  *      @(#)vm_swap.c   8.5 (Berkeley) 2/17/94
69  */
70
71 #include <sys/cdefs.h>
72 __FBSDID("$FreeBSD$");
73
74 #include "opt_vm.h"
75
76 #include <sys/param.h>
77 #include <sys/bio.h>
78 #include <sys/blist.h>
79 #include <sys/buf.h>
80 #include <sys/conf.h>
81 #include <sys/disk.h>
82 #include <sys/eventhandler.h>
83 #include <sys/fcntl.h>
84 #include <sys/lock.h>
85 #include <sys/kernel.h>
86 #include <sys/mount.h>
87 #include <sys/namei.h>
88 #include <sys/malloc.h>
89 #include <sys/pctrie.h>
90 #include <sys/priv.h>
91 #include <sys/proc.h>
92 #include <sys/racct.h>
93 #include <sys/resource.h>
94 #include <sys/resourcevar.h>
95 #include <sys/rwlock.h>
96 #include <sys/sbuf.h>
97 #include <sys/sysctl.h>
98 #include <sys/sysproto.h>
99 #include <sys/systm.h>
100 #include <sys/sx.h>
101 #include <sys/vmmeter.h>
102 #include <sys/vnode.h>
103
104 #include <security/mac/mac_framework.h>
105
106 #include <vm/vm.h>
107 #include <vm/pmap.h>
108 #include <vm/vm_map.h>
109 #include <vm/vm_kern.h>
110 #include <vm/vm_object.h>
111 #include <vm/vm_page.h>
112 #include <vm/vm_pager.h>
113 #include <vm/vm_pageout.h>
114 #include <vm/vm_param.h>
115 #include <vm/swap_pager.h>
116 #include <vm/vm_extern.h>
117 #include <vm/uma.h>
118
119 #include <geom/geom.h>
120
121 /*
122  * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64.
123  * The 64-page limit is due to the radix code (kern/subr_blist.c).
124  */
125 #ifndef MAX_PAGEOUT_CLUSTER
126 #define MAX_PAGEOUT_CLUSTER     32
127 #endif
128
129 #if !defined(SWB_NPAGES)
130 #define SWB_NPAGES      MAX_PAGEOUT_CLUSTER
131 #endif
132
133 #define SWAP_META_PAGES         PCTRIE_COUNT
134
135 /*
136  * A swblk structure maps each page index within a
137  * SWAP_META_PAGES-aligned and sized range to the address of an
138  * on-disk swap block (or SWAPBLK_NONE). The collection of these
139  * mappings for an entire vm object is implemented as a pc-trie.
140  */
141 struct swblk {
142         vm_pindex_t     p;
143         daddr_t         d[SWAP_META_PAGES];
144 };
145
146 static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data");
147 static struct mtx sw_dev_mtx;
148 static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq);
149 static struct swdevt *swdevhd;  /* Allocate from here next */
150 static int nswapdev;            /* Number of swap devices */
151 int swap_pager_avail;
152 static struct sx swdev_syscall_lock;    /* serialize swap(on|off) */
153
154 static u_long swap_reserved;
155 static u_long swap_total;
156 static int sysctl_page_shift(SYSCTL_HANDLER_ARGS);
157 SYSCTL_PROC(_vm, OID_AUTO, swap_reserved, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
158     &swap_reserved, 0, sysctl_page_shift, "A", 
159     "Amount of swap storage needed to back all allocated anonymous memory.");
160 SYSCTL_PROC(_vm, OID_AUTO, swap_total, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
161     &swap_total, 0, sysctl_page_shift, "A", 
162     "Total amount of available swap storage.");
163
164 static int overcommit = 0;
165 SYSCTL_INT(_vm, VM_OVERCOMMIT, overcommit, CTLFLAG_RW, &overcommit, 0,
166     "Configure virtual memory overcommit behavior. See tuning(7) "
167     "for details.");
168 static unsigned long swzone;
169 SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0,
170     "Actual size of swap metadata zone");
171 static unsigned long swap_maxpages;
172 SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0,
173     "Maximum amount of swap supported");
174
175 /* bits from overcommit */
176 #define SWAP_RESERVE_FORCE_ON           (1 << 0)
177 #define SWAP_RESERVE_RLIMIT_ON          (1 << 1)
178 #define SWAP_RESERVE_ALLOW_NONWIRED     (1 << 2)
179
180 static int
181 sysctl_page_shift(SYSCTL_HANDLER_ARGS)
182 {
183         uint64_t newval;
184         u_long value = *(u_long *)arg1;
185
186         newval = ((uint64_t)value) << PAGE_SHIFT;
187         return (sysctl_handle_64(oidp, &newval, 0, req));
188 }
189
190 int
191 swap_reserve(vm_ooffset_t incr)
192 {
193
194         return (swap_reserve_by_cred(incr, curthread->td_ucred));
195 }
196
197 int
198 swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred)
199 {
200         u_long r, s, prev, pincr;
201         int res, error;
202         static int curfail;
203         static struct timeval lastfail;
204         struct uidinfo *uip;
205
206         uip = cred->cr_ruidinfo;
207
208         KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__,
209             (uintmax_t)incr));
210
211 #ifdef RACCT
212         if (racct_enable) {
213                 PROC_LOCK(curproc);
214                 error = racct_add(curproc, RACCT_SWAP, incr);
215                 PROC_UNLOCK(curproc);
216                 if (error != 0)
217                         return (0);
218         }
219 #endif
220
221         pincr = atop(incr);
222         res = 0;
223         prev = atomic_fetchadd_long(&swap_reserved, pincr);
224         r = prev + pincr;
225         if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) {
226                 s = vm_cnt.v_page_count - vm_cnt.v_free_reserved -
227                     vm_wire_count();
228         } else
229                 s = 0;
230         s += swap_total;
231         if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s ||
232             (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) {
233                 res = 1;
234         } else {
235                 prev = atomic_fetchadd_long(&swap_reserved, -pincr);
236                 if (prev < pincr)
237                         panic("swap_reserved < incr on overcommit fail");
238         }
239         if (res) {
240                 prev = atomic_fetchadd_long(&uip->ui_vmsize, pincr);
241                 if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 &&
242                     prev + pincr > lim_cur(curthread, RLIMIT_SWAP) &&
243                     priv_check(curthread, PRIV_VM_SWAP_NORLIMIT)) {
244                         res = 0;
245                         prev = atomic_fetchadd_long(&uip->ui_vmsize, -pincr);
246                         if (prev < pincr)
247                                 panic("uip->ui_vmsize < incr on overcommit fail");
248                 }
249         }
250         if (!res && ppsratecheck(&lastfail, &curfail, 1)) {
251                 printf("uid %d, pid %d: swap reservation for %jd bytes failed\n",
252                     uip->ui_uid, curproc->p_pid, incr);
253         }
254
255 #ifdef RACCT
256         if (racct_enable && !res) {
257                 PROC_LOCK(curproc);
258                 racct_sub(curproc, RACCT_SWAP, incr);
259                 PROC_UNLOCK(curproc);
260         }
261 #endif
262
263         return (res);
264 }
265
266 void
267 swap_reserve_force(vm_ooffset_t incr)
268 {
269         struct uidinfo *uip;
270         u_long pincr;
271
272         KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__,
273             (uintmax_t)incr));
274
275         PROC_LOCK(curproc);
276 #ifdef RACCT
277         if (racct_enable)
278                 racct_add_force(curproc, RACCT_SWAP, incr);
279 #endif
280         pincr = atop(incr);
281         atomic_add_long(&swap_reserved, pincr);
282         uip = curproc->p_ucred->cr_ruidinfo;
283         atomic_add_long(&uip->ui_vmsize, pincr);
284         PROC_UNLOCK(curproc);
285 }
286
287 void
288 swap_release(vm_ooffset_t decr)
289 {
290         struct ucred *cred;
291
292         PROC_LOCK(curproc);
293         cred = curproc->p_ucred;
294         swap_release_by_cred(decr, cred);
295         PROC_UNLOCK(curproc);
296 }
297
298 void
299 swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred)
300 {
301         u_long prev, pdecr;
302         struct uidinfo *uip;
303
304         uip = cred->cr_ruidinfo;
305
306         KASSERT((decr & PAGE_MASK) == 0, ("%s: decr: %ju & PAGE_MASK", __func__,
307             (uintmax_t)decr));
308
309         pdecr = atop(decr);
310         prev = atomic_fetchadd_long(&swap_reserved, -pdecr);
311         if (prev < pdecr)
312                 panic("swap_reserved < decr");
313
314         prev = atomic_fetchadd_long(&uip->ui_vmsize, -pdecr);
315         if (prev < pdecr)
316                 printf("negative vmsize for uid = %d\n", uip->ui_uid);
317 #ifdef RACCT
318         if (racct_enable)
319                 racct_sub_cred(cred, RACCT_SWAP, decr);
320 #endif
321 }
322
323 #define SWM_POP         0x01    /* pop out                      */
324
325 static int swap_pager_full = 2; /* swap space exhaustion (task killing) */
326 static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/
327 static struct mtx swbuf_mtx;    /* to sync nsw_wcount_async */
328 static int nsw_wcount_async;    /* limit async write buffers */
329 static int nsw_wcount_async_max;/* assigned maximum                     */
330 static int nsw_cluster_max;     /* maximum VOP I/O allowed              */
331
332 static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS);
333 SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW |
334     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I",
335     "Maximum running async swap ops");
336 static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS);
337 SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD |
338     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A",
339     "Swap Fragmentation Info");
340
341 static struct sx sw_alloc_sx;
342
343 /*
344  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
345  * of searching a named list by hashing it just a little.
346  */
347
348 #define NOBJLISTS               8
349
350 #define NOBJLIST(handle)        \
351         (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
352
353 static struct pagerlst  swap_pager_object_list[NOBJLISTS];
354 static uma_zone_t swwbuf_zone;
355 static uma_zone_t swrbuf_zone;
356 static uma_zone_t swblk_zone;
357 static uma_zone_t swpctrie_zone;
358
359 /*
360  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
361  * calls hooked from other parts of the VM system and do not appear here.
362  * (see vm/swap_pager.h).
363  */
364 static vm_object_t
365                 swap_pager_alloc(void *handle, vm_ooffset_t size,
366                     vm_prot_t prot, vm_ooffset_t offset, struct ucred *);
367 static void     swap_pager_dealloc(vm_object_t object);
368 static int      swap_pager_getpages(vm_object_t, vm_page_t *, int, int *,
369     int *);
370 static int      swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
371     int *, pgo_getpages_iodone_t, void *);
372 static void     swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
373 static boolean_t
374                 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after);
375 static void     swap_pager_init(void);
376 static void     swap_pager_unswapped(vm_page_t);
377 static void     swap_pager_swapoff(struct swdevt *sp);
378
379 struct pagerops swappagerops = {
380         .pgo_init =     swap_pager_init,        /* early system initialization of pager */
381         .pgo_alloc =    swap_pager_alloc,       /* allocate an OBJT_SWAP object         */
382         .pgo_dealloc =  swap_pager_dealloc,     /* deallocate an OBJT_SWAP object       */
383         .pgo_getpages = swap_pager_getpages,    /* pagein                               */
384         .pgo_getpages_async = swap_pager_getpages_async, /* pagein (async)              */
385         .pgo_putpages = swap_pager_putpages,    /* pageout                              */
386         .pgo_haspage =  swap_pager_haspage,     /* get backing store status for page    */
387         .pgo_pageunswapped = swap_pager_unswapped,      /* remove swap related to page          */
388 };
389
390 /*
391  * swap_*() routines are externally accessible.  swp_*() routines are
392  * internal.
393  */
394 static int nswap_lowat = 128;   /* in pages, swap_pager_almost_full warn */
395 static int nswap_hiwat = 512;   /* in pages, swap_pager_almost_full warn */
396
397 SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0,
398     "Maximum size of a swap block in pages");
399
400 static void     swp_sizecheck(void);
401 static void     swp_pager_async_iodone(struct buf *bp);
402 static bool     swp_pager_swblk_empty(struct swblk *sb, int start, int limit);
403 static int      swapongeom(struct vnode *);
404 static int      swaponvp(struct thread *, struct vnode *, u_long);
405 static int      swapoff_one(struct swdevt *sp, struct ucred *cred);
406
407 /*
408  * Swap bitmap functions
409  */
410 static void     swp_pager_freeswapspace(daddr_t blk, daddr_t npages);
411 static daddr_t  swp_pager_getswapspace(int *npages, int limit);
412
413 /*
414  * Metadata functions
415  */
416 static daddr_t swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t);
417 static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t);
418 static void swp_pager_meta_free_all(vm_object_t);
419 static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int);
420
421 static void
422 swp_pager_init_freerange(daddr_t *start, daddr_t *num)
423 {
424
425         *start = SWAPBLK_NONE;
426         *num = 0;
427 }
428
429 static void
430 swp_pager_update_freerange(daddr_t *start, daddr_t *num, daddr_t addr)
431 {
432
433         if (*start + *num == addr) {
434                 (*num)++;
435         } else {
436                 swp_pager_freeswapspace(*start, *num);
437                 *start = addr;
438                 *num = 1;
439         }
440 }
441
442 static void *
443 swblk_trie_alloc(struct pctrie *ptree)
444 {
445
446         return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ?
447             M_USE_RESERVE : 0)));
448 }
449
450 static void
451 swblk_trie_free(struct pctrie *ptree, void *node)
452 {
453
454         uma_zfree(swpctrie_zone, node);
455 }
456
457 PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free);
458
459 /*
460  * SWP_SIZECHECK() -    update swap_pager_full indication
461  *
462  *      update the swap_pager_almost_full indication and warn when we are
463  *      about to run out of swap space, using lowat/hiwat hysteresis.
464  *
465  *      Clear swap_pager_full ( task killing ) indication when lowat is met.
466  *
467  *      No restrictions on call
468  *      This routine may not block.
469  */
470 static void
471 swp_sizecheck(void)
472 {
473
474         if (swap_pager_avail < nswap_lowat) {
475                 if (swap_pager_almost_full == 0) {
476                         printf("swap_pager: out of swap space\n");
477                         swap_pager_almost_full = 1;
478                 }
479         } else {
480                 swap_pager_full = 0;
481                 if (swap_pager_avail > nswap_hiwat)
482                         swap_pager_almost_full = 0;
483         }
484 }
485
486 /*
487  * SWAP_PAGER_INIT() -  initialize the swap pager!
488  *
489  *      Expected to be started from system init.  NOTE:  This code is run
490  *      before much else so be careful what you depend on.  Most of the VM
491  *      system has yet to be initialized at this point.
492  */
493 static void
494 swap_pager_init(void)
495 {
496         /*
497          * Initialize object lists
498          */
499         int i;
500
501         for (i = 0; i < NOBJLISTS; ++i)
502                 TAILQ_INIT(&swap_pager_object_list[i]);
503         mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF);
504         sx_init(&sw_alloc_sx, "swspsx");
505         sx_init(&swdev_syscall_lock, "swsysc");
506 }
507
508 /*
509  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
510  *
511  *      Expected to be started from pageout process once, prior to entering
512  *      its main loop.
513  */
514 void
515 swap_pager_swap_init(void)
516 {
517         unsigned long n, n2;
518
519         /*
520          * Number of in-transit swap bp operations.  Don't
521          * exhaust the pbufs completely.  Make sure we
522          * initialize workable values (0 will work for hysteresis
523          * but it isn't very efficient).
524          *
525          * The nsw_cluster_max is constrained by the bp->b_pages[]
526          * array (MAXPHYS/PAGE_SIZE) and our locally defined
527          * MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
528          * constrained by the swap device interleave stripe size.
529          *
530          * Currently we hardwire nsw_wcount_async to 4.  This limit is
531          * designed to prevent other I/O from having high latencies due to
532          * our pageout I/O.  The value 4 works well for one or two active swap
533          * devices but is probably a little low if you have more.  Even so,
534          * a higher value would probably generate only a limited improvement
535          * with three or four active swap devices since the system does not
536          * typically have to pageout at extreme bandwidths.   We will want
537          * at least 2 per swap devices, and 4 is a pretty good value if you
538          * have one NFS swap device due to the command/ack latency over NFS.
539          * So it all works out pretty well.
540          */
541         nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER);
542
543         nsw_wcount_async = 4;
544         nsw_wcount_async_max = nsw_wcount_async;
545         mtx_init(&swbuf_mtx, "async swbuf mutex", NULL, MTX_DEF);
546
547         swwbuf_zone = pbuf_zsecond_create("swwbuf", nswbuf / 4);
548         swrbuf_zone = pbuf_zsecond_create("swrbuf", nswbuf / 2);
549
550         /*
551          * Initialize our zone, taking the user's requested size or
552          * estimating the number we need based on the number of pages
553          * in the system.
554          */
555         n = maxswzone != 0 ? maxswzone / sizeof(struct swblk) :
556             vm_cnt.v_page_count / 2;
557         swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL,
558             pctrie_zone_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM);
559         if (swpctrie_zone == NULL)
560                 panic("failed to create swap pctrie zone.");
561         swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL,
562             NULL, NULL, _Alignof(struct swblk) - 1, UMA_ZONE_VM);
563         if (swblk_zone == NULL)
564                 panic("failed to create swap blk zone.");
565         n2 = n;
566         do {
567                 if (uma_zone_reserve_kva(swblk_zone, n))
568                         break;
569                 /*
570                  * if the allocation failed, try a zone two thirds the
571                  * size of the previous attempt.
572                  */
573                 n -= ((n + 2) / 3);
574         } while (n > 0);
575
576         /*
577          * Often uma_zone_reserve_kva() cannot reserve exactly the
578          * requested size.  Account for the difference when
579          * calculating swap_maxpages.
580          */
581         n = uma_zone_get_max(swblk_zone);
582
583         if (n < n2)
584                 printf("Swap blk zone entries changed from %lu to %lu.\n",
585                     n2, n);
586         swap_maxpages = n * SWAP_META_PAGES;
587         swzone = n * sizeof(struct swblk);
588         if (!uma_zone_reserve_kva(swpctrie_zone, n))
589                 printf("Cannot reserve swap pctrie zone, "
590                     "reduce kern.maxswzone.\n");
591 }
592
593 static vm_object_t
594 swap_pager_alloc_init(void *handle, struct ucred *cred, vm_ooffset_t size,
595     vm_ooffset_t offset)
596 {
597         vm_object_t object;
598
599         if (cred != NULL) {
600                 if (!swap_reserve_by_cred(size, cred))
601                         return (NULL);
602                 crhold(cred);
603         }
604
605         /*
606          * The un_pager.swp.swp_blks trie is initialized by
607          * vm_object_allocate() to ensure the correct order of
608          * visibility to other threads.
609          */
610         object = vm_object_allocate(OBJT_SWAP, OFF_TO_IDX(offset +
611             PAGE_MASK + size));
612
613         object->handle = handle;
614         if (cred != NULL) {
615                 object->cred = cred;
616                 object->charge = size;
617         }
618         return (object);
619 }
620
621 /*
622  * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate
623  *                      its metadata structures.
624  *
625  *      This routine is called from the mmap and fork code to create a new
626  *      OBJT_SWAP object.
627  *
628  *      This routine must ensure that no live duplicate is created for
629  *      the named object request, which is protected against by
630  *      holding the sw_alloc_sx lock in case handle != NULL.
631  */
632 static vm_object_t
633 swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
634     vm_ooffset_t offset, struct ucred *cred)
635 {
636         vm_object_t object;
637
638         if (handle != NULL) {
639                 /*
640                  * Reference existing named region or allocate new one.  There
641                  * should not be a race here against swp_pager_meta_build()
642                  * as called from vm_page_remove() in regards to the lookup
643                  * of the handle.
644                  */
645                 sx_xlock(&sw_alloc_sx);
646                 object = vm_pager_object_lookup(NOBJLIST(handle), handle);
647                 if (object == NULL) {
648                         object = swap_pager_alloc_init(handle, cred, size,
649                             offset);
650                         if (object != NULL) {
651                                 TAILQ_INSERT_TAIL(NOBJLIST(object->handle),
652                                     object, pager_object_list);
653                         }
654                 }
655                 sx_xunlock(&sw_alloc_sx);
656         } else {
657                 object = swap_pager_alloc_init(handle, cred, size, offset);
658         }
659         return (object);
660 }
661
662 /*
663  * SWAP_PAGER_DEALLOC() -       remove swap metadata from object
664  *
665  *      The swap backing for the object is destroyed.  The code is
666  *      designed such that we can reinstantiate it later, but this
667  *      routine is typically called only when the entire object is
668  *      about to be destroyed.
669  *
670  *      The object must be locked.
671  */
672 static void
673 swap_pager_dealloc(vm_object_t object)
674 {
675
676         VM_OBJECT_ASSERT_WLOCKED(object);
677         KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj"));
678
679         /*
680          * Remove from list right away so lookups will fail if we block for
681          * pageout completion.
682          */
683         if (object->handle != NULL) {
684                 VM_OBJECT_WUNLOCK(object);
685                 sx_xlock(&sw_alloc_sx);
686                 TAILQ_REMOVE(NOBJLIST(object->handle), object,
687                     pager_object_list);
688                 sx_xunlock(&sw_alloc_sx);
689                 VM_OBJECT_WLOCK(object);
690         }
691
692         vm_object_pip_wait(object, "swpdea");
693
694         /*
695          * Free all remaining metadata.  We only bother to free it from
696          * the swap meta data.  We do not attempt to free swapblk's still
697          * associated with vm_page_t's for this object.  We do not care
698          * if paging is still in progress on some objects.
699          */
700         swp_pager_meta_free_all(object);
701         object->handle = NULL;
702         object->type = OBJT_DEAD;
703 }
704
705 /************************************************************************
706  *                      SWAP PAGER BITMAP ROUTINES                      *
707  ************************************************************************/
708
709 /*
710  * SWP_PAGER_GETSWAPSPACE() -   allocate raw swap space
711  *
712  *      Allocate swap for up to the requested number of pages, and at
713  *      least a minimum number of pages.  The starting swap block number
714  *      (a page index) is returned or SWAPBLK_NONE if the allocation
715  *      failed.
716  *
717  *      Also has the side effect of advising that somebody made a mistake
718  *      when they configured swap and didn't configure enough.
719  *
720  *      This routine may not sleep.
721  *
722  *      We allocate in round-robin fashion from the configured devices.
723  */
724 static daddr_t
725 swp_pager_getswapspace(int *io_npages, int limit)
726 {
727         daddr_t blk;
728         struct swdevt *sp;
729         int mpages, npages;
730
731         blk = SWAPBLK_NONE;
732         npages = mpages = *io_npages;
733         mtx_lock(&sw_dev_mtx);
734         sp = swdevhd;
735         while (!TAILQ_EMPTY(&swtailq)) {
736                 if (sp == NULL)
737                         sp = TAILQ_FIRST(&swtailq);
738                 if ((sp->sw_flags & SW_CLOSING) == 0)
739                         blk = blist_alloc(sp->sw_blist, &npages, mpages);
740                 if (blk != SWAPBLK_NONE)
741                         break;
742                 sp = TAILQ_NEXT(sp, sw_list);
743                 if (swdevhd == sp) {
744                         if (npages <= limit)
745                                 break;
746                         mpages = npages - 1;
747                         npages >>= 1;
748                 }
749         }
750         if (blk != SWAPBLK_NONE) {
751                 *io_npages = npages;
752                 blk += sp->sw_first;
753                 sp->sw_used += npages;
754                 swap_pager_avail -= npages;
755                 swp_sizecheck();
756                 swdevhd = TAILQ_NEXT(sp, sw_list);
757         } else {
758                 if (swap_pager_full != 2) {
759                         printf("swp_pager_getswapspace(%d): failed\n",
760                             *io_npages);
761                         swap_pager_full = 2;
762                         swap_pager_almost_full = 1;
763                 }
764                 swdevhd = NULL;
765         }
766         mtx_unlock(&sw_dev_mtx);
767         return (blk);
768 }
769
770 static bool
771 swp_pager_isondev(daddr_t blk, struct swdevt *sp)
772 {
773
774         return (blk >= sp->sw_first && blk < sp->sw_end);
775 }
776
777 static void
778 swp_pager_strategy(struct buf *bp)
779 {
780         struct swdevt *sp;
781
782         mtx_lock(&sw_dev_mtx);
783         TAILQ_FOREACH(sp, &swtailq, sw_list) {
784                 if (swp_pager_isondev(bp->b_blkno, sp)) {
785                         mtx_unlock(&sw_dev_mtx);
786                         if ((sp->sw_flags & SW_UNMAPPED) != 0 &&
787                             unmapped_buf_allowed) {
788                                 bp->b_data = unmapped_buf;
789                                 bp->b_offset = 0;
790                         } else {
791                                 pmap_qenter((vm_offset_t)bp->b_data,
792                                     &bp->b_pages[0], bp->b_bcount / PAGE_SIZE);
793                         }
794                         sp->sw_strategy(bp, sp);
795                         return;
796                 }
797         }
798         panic("Swapdev not found");
799 }
800
801
802 /*
803  * SWP_PAGER_FREESWAPSPACE() -  free raw swap space
804  *
805  *      This routine returns the specified swap blocks back to the bitmap.
806  *
807  *      This routine may not sleep.
808  */
809 static void
810 swp_pager_freeswapspace(daddr_t blk, daddr_t npages)
811 {
812         struct swdevt *sp;
813
814         if (npages == 0)
815                 return;
816         mtx_lock(&sw_dev_mtx);
817         TAILQ_FOREACH(sp, &swtailq, sw_list) {
818                 if (swp_pager_isondev(blk, sp)) {
819                         sp->sw_used -= npages;
820                         /*
821                          * If we are attempting to stop swapping on
822                          * this device, we don't want to mark any
823                          * blocks free lest they be reused.
824                          */
825                         if ((sp->sw_flags & SW_CLOSING) == 0) {
826                                 blist_free(sp->sw_blist, blk - sp->sw_first,
827                                     npages);
828                                 swap_pager_avail += npages;
829                                 swp_sizecheck();
830                         }
831                         mtx_unlock(&sw_dev_mtx);
832                         return;
833                 }
834         }
835         panic("Swapdev not found");
836 }
837
838 /*
839  * SYSCTL_SWAP_FRAGMENTATION() -        produce raw swap space stats
840  */
841 static int
842 sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS)
843 {
844         struct sbuf sbuf;
845         struct swdevt *sp;
846         const char *devname;
847         int error;
848
849         error = sysctl_wire_old_buffer(req, 0);
850         if (error != 0)
851                 return (error);
852         sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
853         mtx_lock(&sw_dev_mtx);
854         TAILQ_FOREACH(sp, &swtailq, sw_list) {
855                 if (vn_isdisk(sp->sw_vp, NULL))
856                         devname = devtoname(sp->sw_vp->v_rdev);
857                 else
858                         devname = "[file]";
859                 sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname);
860                 blist_stats(sp->sw_blist, &sbuf);
861         }
862         mtx_unlock(&sw_dev_mtx);
863         error = sbuf_finish(&sbuf);
864         sbuf_delete(&sbuf);
865         return (error);
866 }
867
868 /*
869  * SWAP_PAGER_FREESPACE() -     frees swap blocks associated with a page
870  *                              range within an object.
871  *
872  *      This is a globally accessible routine.
873  *
874  *      This routine removes swapblk assignments from swap metadata.
875  *
876  *      The external callers of this routine typically have already destroyed
877  *      or renamed vm_page_t's associated with this range in the object so
878  *      we should be ok.
879  *
880  *      The object must be locked.
881  */
882 void
883 swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size)
884 {
885
886         swp_pager_meta_free(object, start, size);
887 }
888
889 /*
890  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
891  *
892  *      Assigns swap blocks to the specified range within the object.  The
893  *      swap blocks are not zeroed.  Any previous swap assignment is destroyed.
894  *
895  *      Returns 0 on success, -1 on failure.
896  */
897 int
898 swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
899 {
900         daddr_t addr, blk, n_free, s_free;
901         int i, j, n;
902
903         swp_pager_init_freerange(&s_free, &n_free);
904         VM_OBJECT_WLOCK(object);
905         for (i = 0; i < size; i += n) {
906                 n = min(BLIST_MAX_ALLOC, size - i);
907                 blk = swp_pager_getswapspace(&n, 1);
908                 if (blk == SWAPBLK_NONE) {
909                         swp_pager_meta_free(object, start, i);
910                         VM_OBJECT_WUNLOCK(object);
911                         return (-1);
912                 }
913                 for (j = 0; j < n; ++j) {
914                         addr = swp_pager_meta_build(object,
915                             start + i + j, blk + j);
916                         if (addr != SWAPBLK_NONE)
917                                 swp_pager_update_freerange(&s_free, &n_free,
918                                     addr);
919                 }
920         }
921         swp_pager_freeswapspace(s_free, n_free);
922         VM_OBJECT_WUNLOCK(object);
923         return (0);
924 }
925
926 /*
927  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
928  *                      and destroy the source.
929  *
930  *      Copy any valid swapblks from the source to the destination.  In
931  *      cases where both the source and destination have a valid swapblk,
932  *      we keep the destination's.
933  *
934  *      This routine is allowed to sleep.  It may sleep allocating metadata
935  *      indirectly through swp_pager_meta_build() or if paging is still in
936  *      progress on the source.
937  *
938  *      The source object contains no vm_page_t's (which is just as well)
939  *
940  *      The source object is of type OBJT_SWAP.
941  *
942  *      The source and destination objects must be locked.
943  *      Both object locks may temporarily be released.
944  */
945 void
946 swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject,
947     vm_pindex_t offset, int destroysource)
948 {
949         vm_pindex_t i;
950         daddr_t dstaddr, n_free, s_free, srcaddr;
951
952         VM_OBJECT_ASSERT_WLOCKED(srcobject);
953         VM_OBJECT_ASSERT_WLOCKED(dstobject);
954
955         /*
956          * If destroysource is set, we remove the source object from the
957          * swap_pager internal queue now.
958          */
959         if (destroysource && srcobject->handle != NULL) {
960                 vm_object_pip_add(srcobject, 1);
961                 VM_OBJECT_WUNLOCK(srcobject);
962                 vm_object_pip_add(dstobject, 1);
963                 VM_OBJECT_WUNLOCK(dstobject);
964                 sx_xlock(&sw_alloc_sx);
965                 TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject,
966                     pager_object_list);
967                 sx_xunlock(&sw_alloc_sx);
968                 VM_OBJECT_WLOCK(dstobject);
969                 vm_object_pip_wakeup(dstobject);
970                 VM_OBJECT_WLOCK(srcobject);
971                 vm_object_pip_wakeup(srcobject);
972         }
973
974         /*
975          * Transfer source to destination.
976          */
977         swp_pager_init_freerange(&s_free, &n_free);
978         for (i = 0; i < dstobject->size; ++i) {
979                 srcaddr = swp_pager_meta_ctl(srcobject, i + offset, SWM_POP);
980                 if (srcaddr == SWAPBLK_NONE)
981                         continue;
982                 dstaddr = swp_pager_meta_ctl(dstobject, i, 0);
983                 if (dstaddr != SWAPBLK_NONE) {
984                         /*
985                          * Destination has valid swapblk or it is represented
986                          * by a resident page.  We destroy the source block.
987                          */
988                         swp_pager_update_freerange(&s_free, &n_free, srcaddr);
989                         continue;
990                 }
991
992                 /*
993                  * Destination has no swapblk and is not resident,
994                  * copy source.
995                  *
996                  * swp_pager_meta_build() can sleep.
997                  */
998                 vm_object_pip_add(srcobject, 1);
999                 VM_OBJECT_WUNLOCK(srcobject);
1000                 vm_object_pip_add(dstobject, 1);
1001                 dstaddr = swp_pager_meta_build(dstobject, i, srcaddr);
1002                 KASSERT(dstaddr == SWAPBLK_NONE,
1003                     ("Unexpected destination swapblk"));
1004                 vm_object_pip_wakeup(dstobject);
1005                 VM_OBJECT_WLOCK(srcobject);
1006                 vm_object_pip_wakeup(srcobject);
1007         }
1008         swp_pager_freeswapspace(s_free, n_free);
1009
1010         /*
1011          * Free left over swap blocks in source.
1012          *
1013          * We have to revert the type to OBJT_DEFAULT so we do not accidentally
1014          * double-remove the object from the swap queues.
1015          */
1016         if (destroysource) {
1017                 swp_pager_meta_free_all(srcobject);
1018                 /*
1019                  * Reverting the type is not necessary, the caller is going
1020                  * to destroy srcobject directly, but I'm doing it here
1021                  * for consistency since we've removed the object from its
1022                  * queues.
1023                  */
1024                 srcobject->type = OBJT_DEFAULT;
1025         }
1026 }
1027
1028 /*
1029  * SWAP_PAGER_HASPAGE() -       determine if we have good backing store for
1030  *                              the requested page.
1031  *
1032  *      We determine whether good backing store exists for the requested
1033  *      page and return TRUE if it does, FALSE if it doesn't.
1034  *
1035  *      If TRUE, we also try to determine how much valid, contiguous backing
1036  *      store exists before and after the requested page.
1037  */
1038 static boolean_t
1039 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
1040     int *after)
1041 {
1042         daddr_t blk, blk0;
1043         int i;
1044
1045         VM_OBJECT_ASSERT_LOCKED(object);
1046
1047         /*
1048          * do we have good backing store at the requested index ?
1049          */
1050         blk0 = swp_pager_meta_ctl(object, pindex, 0);
1051         if (blk0 == SWAPBLK_NONE) {
1052                 if (before)
1053                         *before = 0;
1054                 if (after)
1055                         *after = 0;
1056                 return (FALSE);
1057         }
1058
1059         /*
1060          * find backwards-looking contiguous good backing store
1061          */
1062         if (before != NULL) {
1063                 for (i = 1; i < SWB_NPAGES; i++) {
1064                         if (i > pindex)
1065                                 break;
1066                         blk = swp_pager_meta_ctl(object, pindex - i, 0);
1067                         if (blk != blk0 - i)
1068                                 break;
1069                 }
1070                 *before = i - 1;
1071         }
1072
1073         /*
1074          * find forward-looking contiguous good backing store
1075          */
1076         if (after != NULL) {
1077                 for (i = 1; i < SWB_NPAGES; i++) {
1078                         blk = swp_pager_meta_ctl(object, pindex + i, 0);
1079                         if (blk != blk0 + i)
1080                                 break;
1081                 }
1082                 *after = i - 1;
1083         }
1084         return (TRUE);
1085 }
1086
1087 /*
1088  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
1089  *
1090  *      This removes any associated swap backing store, whether valid or
1091  *      not, from the page.
1092  *
1093  *      This routine is typically called when a page is made dirty, at
1094  *      which point any associated swap can be freed.  MADV_FREE also
1095  *      calls us in a special-case situation
1096  *
1097  *      NOTE!!!  If the page is clean and the swap was valid, the caller
1098  *      should make the page dirty before calling this routine.  This routine
1099  *      does NOT change the m->dirty status of the page.  Also: MADV_FREE
1100  *      depends on it.
1101  *
1102  *      This routine may not sleep.
1103  *
1104  *      The object containing the page must be locked.
1105  */
1106 static void
1107 swap_pager_unswapped(vm_page_t m)
1108 {
1109         daddr_t srcaddr;
1110
1111         srcaddr = swp_pager_meta_ctl(m->object, m->pindex, SWM_POP);
1112         if (srcaddr != SWAPBLK_NONE)
1113                 swp_pager_freeswapspace(srcaddr, 1);
1114 }
1115
1116 /*
1117  * swap_pager_getpages() - bring pages in from swap
1118  *
1119  *      Attempt to page in the pages in array "ma" of length "count".  The
1120  *      caller may optionally specify that additional pages preceding and
1121  *      succeeding the specified range be paged in.  The number of such pages
1122  *      is returned in the "rbehind" and "rahead" parameters, and they will
1123  *      be in the inactive queue upon return.
1124  *
1125  *      The pages in "ma" must be busied and will remain busied upon return.
1126  */
1127 static int
1128 swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind,
1129     int *rahead)
1130 {
1131         struct buf *bp;
1132         vm_page_t bm, mpred, msucc, p;
1133         vm_pindex_t pindex;
1134         daddr_t blk;
1135         int i, maxahead, maxbehind, reqcount;
1136
1137         reqcount = count;
1138
1139         /*
1140          * Determine the final number of read-behind pages and
1141          * allocate them BEFORE releasing the object lock.  Otherwise,
1142          * there can be a problematic race with vm_object_split().
1143          * Specifically, vm_object_split() might first transfer pages
1144          * that precede ma[0] in the current object to a new object,
1145          * and then this function incorrectly recreates those pages as
1146          * read-behind pages in the current object.
1147          */
1148         if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead))
1149                 return (VM_PAGER_FAIL);
1150
1151         /*
1152          * Clip the readahead and readbehind ranges to exclude resident pages.
1153          */
1154         if (rahead != NULL) {
1155                 KASSERT(reqcount - 1 <= maxahead,
1156                     ("page count %d extends beyond swap block", reqcount));
1157                 *rahead = imin(*rahead, maxahead - (reqcount - 1));
1158                 pindex = ma[reqcount - 1]->pindex;
1159                 msucc = TAILQ_NEXT(ma[reqcount - 1], listq);
1160                 if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead)
1161                         *rahead = msucc->pindex - pindex - 1;
1162         }
1163         if (rbehind != NULL) {
1164                 *rbehind = imin(*rbehind, maxbehind);
1165                 pindex = ma[0]->pindex;
1166                 mpred = TAILQ_PREV(ma[0], pglist, listq);
1167                 if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind)
1168                         *rbehind = pindex - mpred->pindex - 1;
1169         }
1170
1171         bm = ma[0];
1172         for (i = 0; i < count; i++)
1173                 ma[i]->oflags |= VPO_SWAPINPROG;
1174
1175         /*
1176          * Allocate readahead and readbehind pages.
1177          */
1178         if (rbehind != NULL) {
1179                 for (i = 1; i <= *rbehind; i++) {
1180                         p = vm_page_alloc(object, ma[0]->pindex - i,
1181                             VM_ALLOC_NORMAL);
1182                         if (p == NULL)
1183                                 break;
1184                         p->oflags |= VPO_SWAPINPROG;
1185                         bm = p;
1186                 }
1187                 *rbehind = i - 1;
1188         }
1189         if (rahead != NULL) {
1190                 for (i = 0; i < *rahead; i++) {
1191                         p = vm_page_alloc(object,
1192                             ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL);
1193                         if (p == NULL)
1194                                 break;
1195                         p->oflags |= VPO_SWAPINPROG;
1196                 }
1197                 *rahead = i;
1198         }
1199         if (rbehind != NULL)
1200                 count += *rbehind;
1201         if (rahead != NULL)
1202                 count += *rahead;
1203
1204         vm_object_pip_add(object, count);
1205
1206         pindex = bm->pindex;
1207         blk = swp_pager_meta_ctl(object, pindex, 0);
1208         KASSERT(blk != SWAPBLK_NONE,
1209             ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex));
1210
1211         VM_OBJECT_WUNLOCK(object);
1212         bp = uma_zalloc(swrbuf_zone, M_WAITOK);
1213         /* Pages cannot leave the object while busy. */
1214         for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) {
1215                 MPASS(p->pindex == bm->pindex + i);
1216                 bp->b_pages[i] = p;
1217         }
1218
1219         bp->b_flags |= B_PAGING;
1220         bp->b_iocmd = BIO_READ;
1221         bp->b_iodone = swp_pager_async_iodone;
1222         bp->b_rcred = crhold(thread0.td_ucred);
1223         bp->b_wcred = crhold(thread0.td_ucred);
1224         bp->b_blkno = blk;
1225         bp->b_bcount = PAGE_SIZE * count;
1226         bp->b_bufsize = PAGE_SIZE * count;
1227         bp->b_npages = count;
1228         bp->b_pgbefore = rbehind != NULL ? *rbehind : 0;
1229         bp->b_pgafter = rahead != NULL ? *rahead : 0;
1230
1231         VM_CNT_INC(v_swapin);
1232         VM_CNT_ADD(v_swappgsin, count);
1233
1234         /*
1235          * perform the I/O.  NOTE!!!  bp cannot be considered valid after
1236          * this point because we automatically release it on completion.
1237          * Instead, we look at the one page we are interested in which we
1238          * still hold a lock on even through the I/O completion.
1239          *
1240          * The other pages in our ma[] array are also released on completion,
1241          * so we cannot assume they are valid anymore either.
1242          *
1243          * NOTE: b_blkno is destroyed by the call to swapdev_strategy
1244          */
1245         BUF_KERNPROC(bp);
1246         swp_pager_strategy(bp);
1247
1248         /*
1249          * Wait for the pages we want to complete.  VPO_SWAPINPROG is always
1250          * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
1251          * is set in the metadata for each page in the request.
1252          */
1253         VM_OBJECT_WLOCK(object);
1254         while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) {
1255                 ma[0]->oflags |= VPO_SWAPSLEEP;
1256                 VM_CNT_INC(v_intrans);
1257                 if (VM_OBJECT_SLEEP(object, &object->paging_in_progress, PSWP,
1258                     "swread", hz * 20)) {
1259                         printf(
1260 "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n",
1261                             bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount);
1262                 }
1263         }
1264
1265         /*
1266          * If we had an unrecoverable read error pages will not be valid.
1267          */
1268         for (i = 0; i < reqcount; i++)
1269                 if (ma[i]->valid != VM_PAGE_BITS_ALL)
1270                         return (VM_PAGER_ERROR);
1271
1272         return (VM_PAGER_OK);
1273
1274         /*
1275          * A final note: in a low swap situation, we cannot deallocate swap
1276          * and mark a page dirty here because the caller is likely to mark
1277          * the page clean when we return, causing the page to possibly revert
1278          * to all-zero's later.
1279          */
1280 }
1281
1282 /*
1283  *      swap_pager_getpages_async():
1284  *
1285  *      Right now this is emulation of asynchronous operation on top of
1286  *      swap_pager_getpages().
1287  */
1288 static int
1289 swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count,
1290     int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
1291 {
1292         int r, error;
1293
1294         r = swap_pager_getpages(object, ma, count, rbehind, rahead);
1295         VM_OBJECT_WUNLOCK(object);
1296         switch (r) {
1297         case VM_PAGER_OK:
1298                 error = 0;
1299                 break;
1300         case VM_PAGER_ERROR:
1301                 error = EIO;
1302                 break;
1303         case VM_PAGER_FAIL:
1304                 error = EINVAL;
1305                 break;
1306         default:
1307                 panic("unhandled swap_pager_getpages() error %d", r);
1308         }
1309         (iodone)(arg, ma, count, error);
1310         VM_OBJECT_WLOCK(object);
1311
1312         return (r);
1313 }
1314
1315 /*
1316  *      swap_pager_putpages:
1317  *
1318  *      Assign swap (if necessary) and initiate I/O on the specified pages.
1319  *
1320  *      We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
1321  *      are automatically converted to SWAP objects.
1322  *
1323  *      In a low memory situation we may block in VOP_STRATEGY(), but the new
1324  *      vm_page reservation system coupled with properly written VFS devices
1325  *      should ensure that no low-memory deadlock occurs.  This is an area
1326  *      which needs work.
1327  *
1328  *      The parent has N vm_object_pip_add() references prior to
1329  *      calling us and will remove references for rtvals[] that are
1330  *      not set to VM_PAGER_PEND.  We need to remove the rest on I/O
1331  *      completion.
1332  *
1333  *      The parent has soft-busy'd the pages it passes us and will unbusy
1334  *      those whos rtvals[] entry is not set to VM_PAGER_PEND on return.
1335  *      We need to unbusy the rest on I/O completion.
1336  */
1337 static void
1338 swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count,
1339     int flags, int *rtvals)
1340 {
1341         int i, n;
1342         boolean_t sync;
1343         daddr_t addr, n_free, s_free;
1344
1345         swp_pager_init_freerange(&s_free, &n_free);
1346         if (count && ma[0]->object != object) {
1347                 panic("swap_pager_putpages: object mismatch %p/%p",
1348                     object,
1349                     ma[0]->object
1350                 );
1351         }
1352
1353         /*
1354          * Step 1
1355          *
1356          * Turn object into OBJT_SWAP
1357          * check for bogus sysops
1358          * force sync if not pageout process
1359          */
1360         if (object->type != OBJT_SWAP) {
1361                 addr = swp_pager_meta_build(object, 0, SWAPBLK_NONE);
1362                 KASSERT(addr == SWAPBLK_NONE,
1363                     ("unexpected object swap block"));
1364         }
1365         VM_OBJECT_WUNLOCK(object);
1366
1367         n = 0;
1368         if (curproc != pageproc)
1369                 sync = TRUE;
1370         else
1371                 sync = (flags & VM_PAGER_PUT_SYNC) != 0;
1372
1373         /*
1374          * Step 2
1375          *
1376          * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
1377          * The page is left dirty until the pageout operation completes
1378          * successfully.
1379          */
1380         for (i = 0; i < count; i += n) {
1381                 int j;
1382                 struct buf *bp;
1383                 daddr_t blk;
1384
1385                 /*
1386                  * Maximum I/O size is limited by a number of factors.
1387                  */
1388                 n = min(BLIST_MAX_ALLOC, count - i);
1389                 n = min(n, nsw_cluster_max);
1390
1391                 /* Get a block of swap of size up to size n. */
1392                 blk = swp_pager_getswapspace(&n, 4);
1393                 if (blk == SWAPBLK_NONE) {
1394                         for (j = 0; j < n; ++j)
1395                                 rtvals[i+j] = VM_PAGER_FAIL;
1396                         continue;
1397                 }
1398
1399                 /*
1400                  * All I/O parameters have been satisfied, build the I/O
1401                  * request and assign the swap space.
1402                  */
1403                 if (sync != TRUE) {
1404                         mtx_lock(&swbuf_mtx);
1405                         while (nsw_wcount_async == 0)
1406                                 msleep(&nsw_wcount_async, &swbuf_mtx, PVM,
1407                                     "swbufa", 0);
1408                         nsw_wcount_async--;
1409                         mtx_unlock(&swbuf_mtx);
1410                 }
1411                 bp = uma_zalloc(swwbuf_zone, M_WAITOK);
1412                 if (sync != TRUE)
1413                         bp->b_flags = B_ASYNC;
1414                 bp->b_flags |= B_PAGING;
1415                 bp->b_iocmd = BIO_WRITE;
1416
1417                 bp->b_rcred = crhold(thread0.td_ucred);
1418                 bp->b_wcred = crhold(thread0.td_ucred);
1419                 bp->b_bcount = PAGE_SIZE * n;
1420                 bp->b_bufsize = PAGE_SIZE * n;
1421                 bp->b_blkno = blk;
1422
1423                 VM_OBJECT_WLOCK(object);
1424                 for (j = 0; j < n; ++j) {
1425                         vm_page_t mreq = ma[i+j];
1426
1427                         addr = swp_pager_meta_build(mreq->object, mreq->pindex,
1428                             blk + j);
1429                         if (addr != SWAPBLK_NONE)
1430                                 swp_pager_update_freerange(&s_free, &n_free,
1431                                     addr);
1432                         MPASS(mreq->dirty == VM_PAGE_BITS_ALL);
1433                         mreq->oflags |= VPO_SWAPINPROG;
1434                         bp->b_pages[j] = mreq;
1435                 }
1436                 VM_OBJECT_WUNLOCK(object);
1437                 bp->b_npages = n;
1438                 /*
1439                  * Must set dirty range for NFS to work.
1440                  */
1441                 bp->b_dirtyoff = 0;
1442                 bp->b_dirtyend = bp->b_bcount;
1443
1444                 VM_CNT_INC(v_swapout);
1445                 VM_CNT_ADD(v_swappgsout, bp->b_npages);
1446
1447                 /*
1448                  * We unconditionally set rtvals[] to VM_PAGER_PEND so that we
1449                  * can call the async completion routine at the end of a
1450                  * synchronous I/O operation.  Otherwise, our caller would
1451                  * perform duplicate unbusy and wakeup operations on the page
1452                  * and object, respectively.
1453                  */
1454                 for (j = 0; j < n; j++)
1455                         rtvals[i + j] = VM_PAGER_PEND;
1456
1457                 /*
1458                  * asynchronous
1459                  *
1460                  * NOTE: b_blkno is destroyed by the call to swapdev_strategy
1461                  */
1462                 if (sync == FALSE) {
1463                         bp->b_iodone = swp_pager_async_iodone;
1464                         BUF_KERNPROC(bp);
1465                         swp_pager_strategy(bp);
1466                         continue;
1467                 }
1468
1469                 /*
1470                  * synchronous
1471                  *
1472                  * NOTE: b_blkno is destroyed by the call to swapdev_strategy
1473                  */
1474                 bp->b_iodone = bdone;
1475                 swp_pager_strategy(bp);
1476
1477                 /*
1478                  * Wait for the sync I/O to complete.
1479                  */
1480                 bwait(bp, PVM, "swwrt");
1481
1482                 /*
1483                  * Now that we are through with the bp, we can call the
1484                  * normal async completion, which frees everything up.
1485                  */
1486                 swp_pager_async_iodone(bp);
1487         }
1488         VM_OBJECT_WLOCK(object);
1489         swp_pager_freeswapspace(s_free, n_free);
1490 }
1491
1492 /*
1493  *      swp_pager_async_iodone:
1494  *
1495  *      Completion routine for asynchronous reads and writes from/to swap.
1496  *      Also called manually by synchronous code to finish up a bp.
1497  *
1498  *      This routine may not sleep.
1499  */
1500 static void
1501 swp_pager_async_iodone(struct buf *bp)
1502 {
1503         int i;
1504         vm_object_t object = NULL;
1505
1506         /*
1507          * Report error - unless we ran out of memory, in which case
1508          * we've already logged it in swapgeom_strategy().
1509          */
1510         if (bp->b_ioflags & BIO_ERROR && bp->b_error != ENOMEM) {
1511                 printf(
1512                     "swap_pager: I/O error - %s failed; blkno %ld,"
1513                         "size %ld, error %d\n",
1514                     ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
1515                     (long)bp->b_blkno,
1516                     (long)bp->b_bcount,
1517                     bp->b_error
1518                 );
1519         }
1520
1521         /*
1522          * remove the mapping for kernel virtual
1523          */
1524         if (buf_mapped(bp))
1525                 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1526         else
1527                 bp->b_data = bp->b_kvabase;
1528
1529         if (bp->b_npages) {
1530                 object = bp->b_pages[0]->object;
1531                 VM_OBJECT_WLOCK(object);
1532         }
1533
1534         /*
1535          * cleanup pages.  If an error occurs writing to swap, we are in
1536          * very serious trouble.  If it happens to be a disk error, though,
1537          * we may be able to recover by reassigning the swap later on.  So
1538          * in this case we remove the m->swapblk assignment for the page
1539          * but do not free it in the rlist.  The errornous block(s) are thus
1540          * never reallocated as swap.  Redirty the page and continue.
1541          */
1542         for (i = 0; i < bp->b_npages; ++i) {
1543                 vm_page_t m = bp->b_pages[i];
1544
1545                 m->oflags &= ~VPO_SWAPINPROG;
1546                 if (m->oflags & VPO_SWAPSLEEP) {
1547                         m->oflags &= ~VPO_SWAPSLEEP;
1548                         wakeup(&object->paging_in_progress);
1549                 }
1550
1551                 if (bp->b_ioflags & BIO_ERROR) {
1552                         /*
1553                          * If an error occurs I'd love to throw the swapblk
1554                          * away without freeing it back to swapspace, so it
1555                          * can never be used again.  But I can't from an
1556                          * interrupt.
1557                          */
1558                         if (bp->b_iocmd == BIO_READ) {
1559                                 /*
1560                                  * NOTE: for reads, m->dirty will probably
1561                                  * be overridden by the original caller of
1562                                  * getpages so don't play cute tricks here.
1563                                  */
1564                                 m->valid = 0;
1565                         } else {
1566                                 /*
1567                                  * If a write error occurs, reactivate page
1568                                  * so it doesn't clog the inactive list,
1569                                  * then finish the I/O.
1570                                  */
1571                                 MPASS(m->dirty == VM_PAGE_BITS_ALL);
1572                                 vm_page_lock(m);
1573                                 vm_page_activate(m);
1574                                 vm_page_unlock(m);
1575                                 vm_page_sunbusy(m);
1576                         }
1577                 } else if (bp->b_iocmd == BIO_READ) {
1578                         /*
1579                          * NOTE: for reads, m->dirty will probably be
1580                          * overridden by the original caller of getpages so
1581                          * we cannot set them in order to free the underlying
1582                          * swap in a low-swap situation.  I don't think we'd
1583                          * want to do that anyway, but it was an optimization
1584                          * that existed in the old swapper for a time before
1585                          * it got ripped out due to precisely this problem.
1586                          */
1587                         KASSERT(!pmap_page_is_mapped(m),
1588                             ("swp_pager_async_iodone: page %p is mapped", m));
1589                         KASSERT(m->dirty == 0,
1590                             ("swp_pager_async_iodone: page %p is dirty", m));
1591
1592                         m->valid = VM_PAGE_BITS_ALL;
1593                         if (i < bp->b_pgbefore ||
1594                             i >= bp->b_npages - bp->b_pgafter)
1595                                 vm_page_readahead_finish(m);
1596                 } else {
1597                         /*
1598                          * For write success, clear the dirty
1599                          * status, then finish the I/O ( which decrements the
1600                          * busy count and possibly wakes waiter's up ).
1601                          * A page is only written to swap after a period of
1602                          * inactivity.  Therefore, we do not expect it to be
1603                          * reused.
1604                          */
1605                         KASSERT(!pmap_page_is_write_mapped(m),
1606                             ("swp_pager_async_iodone: page %p is not write"
1607                             " protected", m));
1608                         vm_page_undirty(m);
1609                         vm_page_lock(m);
1610                         vm_page_deactivate_noreuse(m);
1611                         vm_page_unlock(m);
1612                         vm_page_sunbusy(m);
1613                 }
1614         }
1615
1616         /*
1617          * adjust pip.  NOTE: the original parent may still have its own
1618          * pip refs on the object.
1619          */
1620         if (object != NULL) {
1621                 vm_object_pip_wakeupn(object, bp->b_npages);
1622                 VM_OBJECT_WUNLOCK(object);
1623         }
1624
1625         /*
1626          * swapdev_strategy() manually sets b_vp and b_bufobj before calling
1627          * bstrategy(). Set them back to NULL now we're done with it, or we'll
1628          * trigger a KASSERT in relpbuf().
1629          */
1630         if (bp->b_vp) {
1631                     bp->b_vp = NULL;
1632                     bp->b_bufobj = NULL;
1633         }
1634         /*
1635          * release the physical I/O buffer
1636          */
1637         if (bp->b_flags & B_ASYNC) {
1638                 mtx_lock(&swbuf_mtx);
1639                 if (++nsw_wcount_async == 1)
1640                         wakeup(&nsw_wcount_async);
1641                 mtx_unlock(&swbuf_mtx);
1642         }
1643         uma_zfree((bp->b_iocmd == BIO_READ) ? swrbuf_zone : swwbuf_zone, bp);
1644 }
1645
1646 int
1647 swap_pager_nswapdev(void)
1648 {
1649
1650         return (nswapdev);
1651 }
1652
1653 /*
1654  * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in
1655  *
1656  *      This routine dissociates the page at the given index within an object
1657  *      from its backing store, paging it in if it does not reside in memory.
1658  *      If the page is paged in, it is marked dirty and placed in the laundry
1659  *      queue.  The page is marked dirty because it no longer has backing
1660  *      store.  It is placed in the laundry queue because it has not been
1661  *      accessed recently.  Otherwise, it would already reside in memory.
1662  *
1663  *      We also attempt to swap in all other pages in the swap block.
1664  *      However, we only guarantee that the one at the specified index is
1665  *      paged in.
1666  *
1667  *      XXX - The code to page the whole block in doesn't work, so we
1668  *            revert to the one-by-one behavior for now.  Sigh.
1669  */
1670 static inline void
1671 swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex)
1672 {
1673         vm_page_t m;
1674
1675         vm_object_pip_add(object, 1);
1676         m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL);
1677         if (m->valid == VM_PAGE_BITS_ALL) {
1678                 vm_object_pip_wakeup(object);
1679                 vm_page_dirty(m);
1680 #ifdef INVARIANTS
1681                 vm_page_lock(m);
1682                 if (m->wire_count == 0 && m->queue == PQ_NONE)
1683                         panic("page %p is neither wired nor queued", m);
1684                 vm_page_unlock(m);
1685 #endif
1686                 vm_page_xunbusy(m);
1687                 vm_pager_page_unswapped(m);
1688                 return;
1689         }
1690
1691         if (swap_pager_getpages(object, &m, 1, NULL, NULL) != VM_PAGER_OK)
1692                 panic("swap_pager_force_pagein: read from swap failed");/*XXX*/
1693         vm_object_pip_wakeup(object);
1694         vm_page_dirty(m);
1695         vm_page_lock(m);
1696         vm_page_launder(m);
1697         vm_page_unlock(m);
1698         vm_page_xunbusy(m);
1699         vm_pager_page_unswapped(m);
1700 }
1701
1702 /*
1703  *      swap_pager_swapoff:
1704  *
1705  *      Page in all of the pages that have been paged out to the
1706  *      given device.  The corresponding blocks in the bitmap must be
1707  *      marked as allocated and the device must be flagged SW_CLOSING.
1708  *      There may be no processes swapped out to the device.
1709  *
1710  *      This routine may block.
1711  */
1712 static void
1713 swap_pager_swapoff(struct swdevt *sp)
1714 {
1715         struct swblk *sb;
1716         vm_object_t object;
1717         vm_pindex_t pi;
1718         int i, retries;
1719
1720         sx_assert(&swdev_syscall_lock, SA_XLOCKED);
1721
1722         retries = 0;
1723 full_rescan:
1724         mtx_lock(&vm_object_list_mtx);
1725         TAILQ_FOREACH(object, &vm_object_list, object_list) {
1726                 if (object->type != OBJT_SWAP)
1727                         continue;
1728                 mtx_unlock(&vm_object_list_mtx);
1729                 /* Depends on type-stability. */
1730                 VM_OBJECT_WLOCK(object);
1731
1732                 /*
1733                  * Dead objects are eventually terminated on their own.
1734                  */
1735                 if ((object->flags & OBJ_DEAD) != 0)
1736                         goto next_obj;
1737
1738                 /*
1739                  * Sync with fences placed after pctrie
1740                  * initialization.  We must not access pctrie below
1741                  * unless we checked that our object is swap and not
1742                  * dead.
1743                  */
1744                 atomic_thread_fence_acq();
1745                 if (object->type != OBJT_SWAP)
1746                         goto next_obj;
1747
1748                 for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
1749                     &object->un_pager.swp.swp_blks, pi)) != NULL; ) {
1750                         pi = sb->p + SWAP_META_PAGES;
1751                         for (i = 0; i < SWAP_META_PAGES; i++) {
1752                                 if (sb->d[i] == SWAPBLK_NONE)
1753                                         continue;
1754                                 if (swp_pager_isondev(sb->d[i], sp))
1755                                         swp_pager_force_pagein(object,
1756                                             sb->p + i);
1757                         }
1758                 }
1759 next_obj:
1760                 VM_OBJECT_WUNLOCK(object);
1761                 mtx_lock(&vm_object_list_mtx);
1762         }
1763         mtx_unlock(&vm_object_list_mtx);
1764
1765         if (sp->sw_used) {
1766                 /*
1767                  * Objects may be locked or paging to the device being
1768                  * removed, so we will miss their pages and need to
1769                  * make another pass.  We have marked this device as
1770                  * SW_CLOSING, so the activity should finish soon.
1771                  */
1772                 retries++;
1773                 if (retries > 100) {
1774                         panic("swapoff: failed to locate %d swap blocks",
1775                             sp->sw_used);
1776                 }
1777                 pause("swpoff", hz / 20);
1778                 goto full_rescan;
1779         }
1780         EVENTHANDLER_INVOKE(swapoff, sp);
1781 }
1782
1783 /************************************************************************
1784  *                              SWAP META DATA                          *
1785  ************************************************************************
1786  *
1787  *      These routines manipulate the swap metadata stored in the
1788  *      OBJT_SWAP object.
1789  *
1790  *      Swap metadata is implemented with a global hash and not directly
1791  *      linked into the object.  Instead the object simply contains
1792  *      appropriate tracking counters.
1793  */
1794
1795 /*
1796  * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free?
1797  */
1798 static bool
1799 swp_pager_swblk_empty(struct swblk *sb, int start, int limit)
1800 {
1801         int i;
1802
1803         MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES);
1804         for (i = start; i < limit; i++) {
1805                 if (sb->d[i] != SWAPBLK_NONE)
1806                         return (false);
1807         }
1808         return (true);
1809 }
1810    
1811 /*
1812  * SWP_PAGER_META_BUILD() -     add swap block to swap meta data for object
1813  *
1814  *      We first convert the object to a swap object if it is a default
1815  *      object.
1816  *
1817  *      The specified swapblk is added to the object's swap metadata.  If
1818  *      the swapblk is not valid, it is freed instead.  Any previously
1819  *      assigned swapblk is returned.
1820  */
1821 static daddr_t
1822 swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk)
1823 {
1824         static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted;
1825         struct swblk *sb, *sb1;
1826         vm_pindex_t modpi, rdpi;
1827         daddr_t prev_swapblk;
1828         int error, i;
1829
1830         VM_OBJECT_ASSERT_WLOCKED(object);
1831
1832         /*
1833          * Convert default object to swap object if necessary
1834          */
1835         if (object->type != OBJT_SWAP) {
1836                 pctrie_init(&object->un_pager.swp.swp_blks);
1837
1838                 /*
1839                  * Ensure that swap_pager_swapoff()'s iteration over
1840                  * object_list does not see a garbage pctrie.
1841                  */
1842                 atomic_thread_fence_rel();
1843
1844                 object->type = OBJT_SWAP;
1845                 KASSERT(object->handle == NULL, ("default pager with handle"));
1846         }
1847
1848         rdpi = rounddown(pindex, SWAP_META_PAGES);
1849         sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi);
1850         if (sb == NULL) {
1851                 if (swapblk == SWAPBLK_NONE)
1852                         return (SWAPBLK_NONE);
1853                 for (;;) {
1854                         sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc ==
1855                             pageproc ? M_USE_RESERVE : 0));
1856                         if (sb != NULL) {
1857                                 sb->p = rdpi;
1858                                 for (i = 0; i < SWAP_META_PAGES; i++)
1859                                         sb->d[i] = SWAPBLK_NONE;
1860                                 if (atomic_cmpset_int(&swblk_zone_exhausted,
1861                                     1, 0))
1862                                         printf("swblk zone ok\n");
1863                                 break;
1864                         }
1865                         VM_OBJECT_WUNLOCK(object);
1866                         if (uma_zone_exhausted(swblk_zone)) {
1867                                 if (atomic_cmpset_int(&swblk_zone_exhausted,
1868                                     0, 1))
1869                                         printf("swap blk zone exhausted, "
1870                                             "increase kern.maxswzone\n");
1871                                 vm_pageout_oom(VM_OOM_SWAPZ);
1872                                 pause("swzonxb", 10);
1873                         } else
1874                                 uma_zwait(swblk_zone);
1875                         VM_OBJECT_WLOCK(object);
1876                         sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
1877                             rdpi);
1878                         if (sb != NULL)
1879                                 /*
1880                                  * Somebody swapped out a nearby page,
1881                                  * allocating swblk at the rdpi index,
1882                                  * while we dropped the object lock.
1883                                  */
1884                                 goto allocated;
1885                 }
1886                 for (;;) {
1887                         error = SWAP_PCTRIE_INSERT(
1888                             &object->un_pager.swp.swp_blks, sb);
1889                         if (error == 0) {
1890                                 if (atomic_cmpset_int(&swpctrie_zone_exhausted,
1891                                     1, 0))
1892                                         printf("swpctrie zone ok\n");
1893                                 break;
1894                         }
1895                         VM_OBJECT_WUNLOCK(object);
1896                         if (uma_zone_exhausted(swpctrie_zone)) {
1897                                 if (atomic_cmpset_int(&swpctrie_zone_exhausted,
1898                                     0, 1))
1899                                         printf("swap pctrie zone exhausted, "
1900                                             "increase kern.maxswzone\n");
1901                                 vm_pageout_oom(VM_OOM_SWAPZ);
1902                                 pause("swzonxp", 10);
1903                         } else
1904                                 uma_zwait(swpctrie_zone);
1905                         VM_OBJECT_WLOCK(object);
1906                         sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
1907                             rdpi);
1908                         if (sb1 != NULL) {
1909                                 uma_zfree(swblk_zone, sb);
1910                                 sb = sb1;
1911                                 goto allocated;
1912                         }
1913                 }
1914         }
1915 allocated:
1916         MPASS(sb->p == rdpi);
1917
1918         modpi = pindex % SWAP_META_PAGES;
1919         /* Return prior contents of metadata. */
1920         prev_swapblk = sb->d[modpi];
1921         /* Enter block into metadata. */
1922         sb->d[modpi] = swapblk;
1923
1924         /*
1925          * Free the swblk if we end up with the empty page run.
1926          */
1927         if (swapblk == SWAPBLK_NONE &&
1928             swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) {
1929                 SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, rdpi);
1930                 uma_zfree(swblk_zone, sb);
1931         }
1932         return (prev_swapblk);
1933 }
1934
1935 /*
1936  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
1937  *
1938  *      The requested range of blocks is freed, with any associated swap
1939  *      returned to the swap bitmap.
1940  *
1941  *      This routine will free swap metadata structures as they are cleaned
1942  *      out.  This routine does *NOT* operate on swap metadata associated
1943  *      with resident pages.
1944  */
1945 static void
1946 swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count)
1947 {
1948         struct swblk *sb;
1949         daddr_t n_free, s_free;
1950         vm_pindex_t last;
1951         int i, limit, start;
1952
1953         VM_OBJECT_ASSERT_WLOCKED(object);
1954         if (object->type != OBJT_SWAP || count == 0)
1955                 return;
1956
1957         swp_pager_init_freerange(&s_free, &n_free);
1958         last = pindex + count;
1959         for (;;) {
1960                 sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
1961                     rounddown(pindex, SWAP_META_PAGES));
1962                 if (sb == NULL || sb->p >= last)
1963                         break;
1964                 start = pindex > sb->p ? pindex - sb->p : 0;
1965                 limit = last - sb->p < SWAP_META_PAGES ? last - sb->p :
1966                     SWAP_META_PAGES;
1967                 for (i = start; i < limit; i++) {
1968                         if (sb->d[i] == SWAPBLK_NONE)
1969                                 continue;
1970                         swp_pager_update_freerange(&s_free, &n_free, sb->d[i]);
1971                         sb->d[i] = SWAPBLK_NONE;
1972                 }
1973                 pindex = sb->p + SWAP_META_PAGES;
1974                 if (swp_pager_swblk_empty(sb, 0, start) &&
1975                     swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) {
1976                         SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks,
1977                             sb->p);
1978                         uma_zfree(swblk_zone, sb);
1979                 }
1980         }
1981         swp_pager_freeswapspace(s_free, n_free);
1982 }
1983
1984 /*
1985  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
1986  *
1987  *      This routine locates and destroys all swap metadata associated with
1988  *      an object.
1989  */
1990 static void
1991 swp_pager_meta_free_all(vm_object_t object)
1992 {
1993         struct swblk *sb;
1994         daddr_t n_free, s_free;
1995         vm_pindex_t pindex;
1996         int i;
1997
1998         VM_OBJECT_ASSERT_WLOCKED(object);
1999         if (object->type != OBJT_SWAP)
2000                 return;
2001
2002         swp_pager_init_freerange(&s_free, &n_free);
2003         for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
2004             &object->un_pager.swp.swp_blks, pindex)) != NULL;) {
2005                 pindex = sb->p + SWAP_META_PAGES;
2006                 for (i = 0; i < SWAP_META_PAGES; i++) {
2007                         if (sb->d[i] == SWAPBLK_NONE)
2008                                 continue;
2009                         swp_pager_update_freerange(&s_free, &n_free, sb->d[i]);
2010                 }
2011                 SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p);
2012                 uma_zfree(swblk_zone, sb);
2013         }
2014         swp_pager_freeswapspace(s_free, n_free);
2015 }
2016
2017 /*
2018  * SWP_PAGER_METACTL() -  misc control of swap meta data.
2019  *
2020  *      This routine is capable of looking up, or removing swapblk
2021  *      assignments in the swap meta data.  It returns the swapblk being
2022  *      looked-up, popped, or SWAPBLK_NONE if the block was invalid.
2023  *
2024  *      When acting on a busy resident page and paging is in progress, we
2025  *      have to wait until paging is complete but otherwise can act on the
2026  *      busy page.
2027  *
2028  *      SWM_POP         remove from meta data but do not free it
2029  */
2030 static daddr_t
2031 swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags)
2032 {
2033         struct swblk *sb;
2034         daddr_t r1;
2035
2036         if ((flags & SWM_POP) != 0)
2037                 VM_OBJECT_ASSERT_WLOCKED(object);
2038         else
2039                 VM_OBJECT_ASSERT_LOCKED(object);
2040
2041         /*
2042          * The meta data only exists if the object is OBJT_SWAP
2043          * and even then might not be allocated yet.
2044          */
2045         if (object->type != OBJT_SWAP)
2046                 return (SWAPBLK_NONE);
2047
2048         sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
2049             rounddown(pindex, SWAP_META_PAGES));
2050         if (sb == NULL)
2051                 return (SWAPBLK_NONE);
2052         r1 = sb->d[pindex % SWAP_META_PAGES];
2053         if (r1 == SWAPBLK_NONE)
2054                 return (SWAPBLK_NONE);
2055         if ((flags & SWM_POP) != 0) {
2056                 sb->d[pindex % SWAP_META_PAGES] = SWAPBLK_NONE;
2057                 if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) {
2058                         SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks,
2059                             rounddown(pindex, SWAP_META_PAGES));
2060                         uma_zfree(swblk_zone, sb);
2061                 }
2062         }
2063         return (r1);
2064 }
2065
2066 /*
2067  * Returns the least page index which is greater than or equal to the
2068  * parameter pindex and for which there is a swap block allocated.
2069  * Returns object's size if the object's type is not swap or if there
2070  * are no allocated swap blocks for the object after the requested
2071  * pindex.
2072  */
2073 vm_pindex_t
2074 swap_pager_find_least(vm_object_t object, vm_pindex_t pindex)
2075 {
2076         struct swblk *sb;
2077         int i;
2078
2079         VM_OBJECT_ASSERT_LOCKED(object);
2080         if (object->type != OBJT_SWAP)
2081                 return (object->size);
2082
2083         sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2084             rounddown(pindex, SWAP_META_PAGES));
2085         if (sb == NULL)
2086                 return (object->size);
2087         if (sb->p < pindex) {
2088                 for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) {
2089                         if (sb->d[i] != SWAPBLK_NONE)
2090                                 return (sb->p + i);
2091                 }
2092                 sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2093                     roundup(pindex, SWAP_META_PAGES));
2094                 if (sb == NULL)
2095                         return (object->size);
2096         }
2097         for (i = 0; i < SWAP_META_PAGES; i++) {
2098                 if (sb->d[i] != SWAPBLK_NONE)
2099                         return (sb->p + i);
2100         }
2101
2102         /*
2103          * We get here if a swblk is present in the trie but it
2104          * doesn't map any blocks.
2105          */
2106         MPASS(0);
2107         return (object->size);
2108 }
2109
2110 /*
2111  * System call swapon(name) enables swapping on device name,
2112  * which must be in the swdevsw.  Return EBUSY
2113  * if already swapping on this device.
2114  */
2115 #ifndef _SYS_SYSPROTO_H_
2116 struct swapon_args {
2117         char *name;
2118 };
2119 #endif
2120
2121 /*
2122  * MPSAFE
2123  */
2124 /* ARGSUSED */
2125 int
2126 sys_swapon(struct thread *td, struct swapon_args *uap)
2127 {
2128         struct vattr attr;
2129         struct vnode *vp;
2130         struct nameidata nd;
2131         int error;
2132
2133         error = priv_check(td, PRIV_SWAPON);
2134         if (error)
2135                 return (error);
2136
2137         sx_xlock(&swdev_syscall_lock);
2138
2139         /*
2140          * Swap metadata may not fit in the KVM if we have physical
2141          * memory of >1GB.
2142          */
2143         if (swblk_zone == NULL) {
2144                 error = ENOMEM;
2145                 goto done;
2146         }
2147
2148         NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE,
2149             uap->name, td);
2150         error = namei(&nd);
2151         if (error)
2152                 goto done;
2153
2154         NDFREE(&nd, NDF_ONLY_PNBUF);
2155         vp = nd.ni_vp;
2156
2157         if (vn_isdisk(vp, &error)) {
2158                 error = swapongeom(vp);
2159         } else if (vp->v_type == VREG &&
2160             (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
2161             (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) {
2162                 /*
2163                  * Allow direct swapping to NFS regular files in the same
2164                  * way that nfs_mountroot() sets up diskless swapping.
2165                  */
2166                 error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
2167         }
2168
2169         if (error)
2170                 vrele(vp);
2171 done:
2172         sx_xunlock(&swdev_syscall_lock);
2173         return (error);
2174 }
2175
2176 /*
2177  * Check that the total amount of swap currently configured does not
2178  * exceed half the theoretical maximum.  If it does, print a warning
2179  * message.
2180  */
2181 static void
2182 swapon_check_swzone(void)
2183 {
2184         unsigned long maxpages, npages;
2185
2186         npages = swap_total;
2187         /* absolute maximum we can handle assuming 100% efficiency */
2188         maxpages = uma_zone_get_max(swblk_zone) * SWAP_META_PAGES;
2189
2190         /* recommend using no more than half that amount */
2191         if (npages > maxpages / 2) {
2192                 printf("warning: total configured swap (%lu pages) "
2193                     "exceeds maximum recommended amount (%lu pages).\n",
2194                     npages, maxpages / 2);
2195                 printf("warning: increase kern.maxswzone "
2196                     "or reduce amount of swap.\n");
2197         }
2198 }
2199
2200 static void
2201 swaponsomething(struct vnode *vp, void *id, u_long nblks,
2202     sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags)
2203 {
2204         struct swdevt *sp, *tsp;
2205         swblk_t dvbase;
2206         u_long mblocks;
2207
2208         /*
2209          * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
2210          * First chop nblks off to page-align it, then convert.
2211          *
2212          * sw->sw_nblks is in page-sized chunks now too.
2213          */
2214         nblks &= ~(ctodb(1) - 1);
2215         nblks = dbtoc(nblks);
2216
2217         /*
2218          * If we go beyond this, we get overflows in the radix
2219          * tree bitmap code.
2220          */
2221         mblocks = 0x40000000 / BLIST_META_RADIX;
2222         if (nblks > mblocks) {
2223                 printf(
2224     "WARNING: reducing swap size to maximum of %luMB per unit\n",
2225                     mblocks / 1024 / 1024 * PAGE_SIZE);
2226                 nblks = mblocks;
2227         }
2228
2229         sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO);
2230         sp->sw_vp = vp;
2231         sp->sw_id = id;
2232         sp->sw_dev = dev;
2233         sp->sw_flags = 0;
2234         sp->sw_nblks = nblks;
2235         sp->sw_used = 0;
2236         sp->sw_strategy = strategy;
2237         sp->sw_close = close;
2238         sp->sw_flags = flags;
2239
2240         sp->sw_blist = blist_create(nblks, M_WAITOK);
2241         /*
2242          * Do not free the first two block in order to avoid overwriting
2243          * any bsd label at the front of the partition
2244          */
2245         blist_free(sp->sw_blist, 2, nblks - 2);
2246
2247         dvbase = 0;
2248         mtx_lock(&sw_dev_mtx);
2249         TAILQ_FOREACH(tsp, &swtailq, sw_list) {
2250                 if (tsp->sw_end >= dvbase) {
2251                         /*
2252                          * We put one uncovered page between the devices
2253                          * in order to definitively prevent any cross-device
2254                          * I/O requests
2255                          */
2256                         dvbase = tsp->sw_end + 1;
2257                 }
2258         }
2259         sp->sw_first = dvbase;
2260         sp->sw_end = dvbase + nblks;
2261         TAILQ_INSERT_TAIL(&swtailq, sp, sw_list);
2262         nswapdev++;
2263         swap_pager_avail += nblks - 2;
2264         swap_total += nblks;
2265         swapon_check_swzone();
2266         swp_sizecheck();
2267         mtx_unlock(&sw_dev_mtx);
2268         EVENTHANDLER_INVOKE(swapon, sp);
2269 }
2270
2271 /*
2272  * SYSCALL: swapoff(devname)
2273  *
2274  * Disable swapping on the given device.
2275  *
2276  * XXX: Badly designed system call: it should use a device index
2277  * rather than filename as specification.  We keep sw_vp around
2278  * only to make this work.
2279  */
2280 #ifndef _SYS_SYSPROTO_H_
2281 struct swapoff_args {
2282         char *name;
2283 };
2284 #endif
2285
2286 /*
2287  * MPSAFE
2288  */
2289 /* ARGSUSED */
2290 int
2291 sys_swapoff(struct thread *td, struct swapoff_args *uap)
2292 {
2293         struct vnode *vp;
2294         struct nameidata nd;
2295         struct swdevt *sp;
2296         int error;
2297
2298         error = priv_check(td, PRIV_SWAPOFF);
2299         if (error)
2300                 return (error);
2301
2302         sx_xlock(&swdev_syscall_lock);
2303
2304         NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name,
2305             td);
2306         error = namei(&nd);
2307         if (error)
2308                 goto done;
2309         NDFREE(&nd, NDF_ONLY_PNBUF);
2310         vp = nd.ni_vp;
2311
2312         mtx_lock(&sw_dev_mtx);
2313         TAILQ_FOREACH(sp, &swtailq, sw_list) {
2314                 if (sp->sw_vp == vp)
2315                         break;
2316         }
2317         mtx_unlock(&sw_dev_mtx);
2318         if (sp == NULL) {
2319                 error = EINVAL;
2320                 goto done;
2321         }
2322         error = swapoff_one(sp, td->td_ucred);
2323 done:
2324         sx_xunlock(&swdev_syscall_lock);
2325         return (error);
2326 }
2327
2328 static int
2329 swapoff_one(struct swdevt *sp, struct ucred *cred)
2330 {
2331         u_long nblks;
2332 #ifdef MAC
2333         int error;
2334 #endif
2335
2336         sx_assert(&swdev_syscall_lock, SA_XLOCKED);
2337 #ifdef MAC
2338         (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY);
2339         error = mac_system_check_swapoff(cred, sp->sw_vp);
2340         (void) VOP_UNLOCK(sp->sw_vp, 0);
2341         if (error != 0)
2342                 return (error);
2343 #endif
2344         nblks = sp->sw_nblks;
2345
2346         /*
2347          * We can turn off this swap device safely only if the
2348          * available virtual memory in the system will fit the amount
2349          * of data we will have to page back in, plus an epsilon so
2350          * the system doesn't become critically low on swap space.
2351          */
2352         if (vm_free_count() + swap_pager_avail < nblks + nswap_lowat)
2353                 return (ENOMEM);
2354
2355         /*
2356          * Prevent further allocations on this device.
2357          */
2358         mtx_lock(&sw_dev_mtx);
2359         sp->sw_flags |= SW_CLOSING;
2360         swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks);
2361         swap_total -= nblks;
2362         mtx_unlock(&sw_dev_mtx);
2363
2364         /*
2365          * Page in the contents of the device and close it.
2366          */
2367         swap_pager_swapoff(sp);
2368
2369         sp->sw_close(curthread, sp);
2370         mtx_lock(&sw_dev_mtx);
2371         sp->sw_id = NULL;
2372         TAILQ_REMOVE(&swtailq, sp, sw_list);
2373         nswapdev--;
2374         if (nswapdev == 0) {
2375                 swap_pager_full = 2;
2376                 swap_pager_almost_full = 1;
2377         }
2378         if (swdevhd == sp)
2379                 swdevhd = NULL;
2380         mtx_unlock(&sw_dev_mtx);
2381         blist_destroy(sp->sw_blist);
2382         free(sp, M_VMPGDATA);
2383         return (0);
2384 }
2385
2386 void
2387 swapoff_all(void)
2388 {
2389         struct swdevt *sp, *spt;
2390         const char *devname;
2391         int error;
2392
2393         sx_xlock(&swdev_syscall_lock);
2394
2395         mtx_lock(&sw_dev_mtx);
2396         TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) {
2397                 mtx_unlock(&sw_dev_mtx);
2398                 if (vn_isdisk(sp->sw_vp, NULL))
2399                         devname = devtoname(sp->sw_vp->v_rdev);
2400                 else
2401                         devname = "[file]";
2402                 error = swapoff_one(sp, thread0.td_ucred);
2403                 if (error != 0) {
2404                         printf("Cannot remove swap device %s (error=%d), "
2405                             "skipping.\n", devname, error);
2406                 } else if (bootverbose) {
2407                         printf("Swap device %s removed.\n", devname);
2408                 }
2409                 mtx_lock(&sw_dev_mtx);
2410         }
2411         mtx_unlock(&sw_dev_mtx);
2412
2413         sx_xunlock(&swdev_syscall_lock);
2414 }
2415
2416 void
2417 swap_pager_status(int *total, int *used)
2418 {
2419         struct swdevt *sp;
2420
2421         *total = 0;
2422         *used = 0;
2423         mtx_lock(&sw_dev_mtx);
2424         TAILQ_FOREACH(sp, &swtailq, sw_list) {
2425                 *total += sp->sw_nblks;
2426                 *used += sp->sw_used;
2427         }
2428         mtx_unlock(&sw_dev_mtx);
2429 }
2430
2431 int
2432 swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len)
2433 {
2434         struct swdevt *sp;
2435         const char *tmp_devname;
2436         int error, n;
2437
2438         n = 0;
2439         error = ENOENT;
2440         mtx_lock(&sw_dev_mtx);
2441         TAILQ_FOREACH(sp, &swtailq, sw_list) {
2442                 if (n != name) {
2443                         n++;
2444                         continue;
2445                 }
2446                 xs->xsw_version = XSWDEV_VERSION;
2447                 xs->xsw_dev = sp->sw_dev;
2448                 xs->xsw_flags = sp->sw_flags;
2449                 xs->xsw_nblks = sp->sw_nblks;
2450                 xs->xsw_used = sp->sw_used;
2451                 if (devname != NULL) {
2452                         if (vn_isdisk(sp->sw_vp, NULL))
2453                                 tmp_devname = devtoname(sp->sw_vp->v_rdev);
2454                         else
2455                                 tmp_devname = "[file]";
2456                         strncpy(devname, tmp_devname, len);
2457                 }
2458                 error = 0;
2459                 break;
2460         }
2461         mtx_unlock(&sw_dev_mtx);
2462         return (error);
2463 }
2464
2465 #if defined(COMPAT_FREEBSD11)
2466 #define XSWDEV_VERSION_11       1
2467 struct xswdev11 {
2468         u_int   xsw_version;
2469         uint32_t xsw_dev;
2470         int     xsw_flags;
2471         int     xsw_nblks;
2472         int     xsw_used;
2473 };
2474 #endif
2475
2476 #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2477 struct xswdev32 {
2478         u_int   xsw_version;
2479         u_int   xsw_dev1, xsw_dev2;
2480         int     xsw_flags;
2481         int     xsw_nblks;
2482         int     xsw_used;
2483 };
2484 #endif
2485
2486 static int
2487 sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
2488 {
2489         struct xswdev xs;
2490 #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2491         struct xswdev32 xs32;
2492 #endif
2493 #if defined(COMPAT_FREEBSD11)
2494         struct xswdev11 xs11;
2495 #endif
2496         int error;
2497
2498         if (arg2 != 1)                  /* name length */
2499                 return (EINVAL);
2500         error = swap_dev_info(*(int *)arg1, &xs, NULL, 0);
2501         if (error != 0)
2502                 return (error);
2503 #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2504         if (req->oldlen == sizeof(xs32)) {
2505                 xs32.xsw_version = XSWDEV_VERSION;
2506                 xs32.xsw_dev1 = xs.xsw_dev;
2507                 xs32.xsw_dev2 = xs.xsw_dev >> 32;
2508                 xs32.xsw_flags = xs.xsw_flags;
2509                 xs32.xsw_nblks = xs.xsw_nblks;
2510                 xs32.xsw_used = xs.xsw_used;
2511                 error = SYSCTL_OUT(req, &xs32, sizeof(xs32));
2512                 return (error);
2513         }
2514 #endif
2515 #if defined(COMPAT_FREEBSD11)
2516         if (req->oldlen == sizeof(xs11)) {
2517                 xs11.xsw_version = XSWDEV_VERSION_11;
2518                 xs11.xsw_dev = xs.xsw_dev; /* truncation */
2519                 xs11.xsw_flags = xs.xsw_flags;
2520                 xs11.xsw_nblks = xs.xsw_nblks;
2521                 xs11.xsw_used = xs.xsw_used;
2522                 error = SYSCTL_OUT(req, &xs11, sizeof(xs11));
2523                 return (error);
2524         }
2525 #endif
2526         error = SYSCTL_OUT(req, &xs, sizeof(xs));
2527         return (error);
2528 }
2529
2530 SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
2531     "Number of swap devices");
2532 SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE,
2533     sysctl_vm_swap_info,
2534     "Swap statistics by device");
2535
2536 /*
2537  * Count the approximate swap usage in pages for a vmspace.  The
2538  * shadowed or not yet copied on write swap blocks are not accounted.
2539  * The map must be locked.
2540  */
2541 long
2542 vmspace_swap_count(struct vmspace *vmspace)
2543 {
2544         vm_map_t map;
2545         vm_map_entry_t cur;
2546         vm_object_t object;
2547         struct swblk *sb;
2548         vm_pindex_t e, pi;
2549         long count;
2550         int i;
2551
2552         map = &vmspace->vm_map;
2553         count = 0;
2554
2555         for (cur = map->header.next; cur != &map->header; cur = cur->next) {
2556                 if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
2557                         continue;
2558                 object = cur->object.vm_object;
2559                 if (object == NULL || object->type != OBJT_SWAP)
2560                         continue;
2561                 VM_OBJECT_RLOCK(object);
2562                 if (object->type != OBJT_SWAP)
2563                         goto unlock;
2564                 pi = OFF_TO_IDX(cur->offset);
2565                 e = pi + OFF_TO_IDX(cur->end - cur->start);
2566                 for (;; pi = sb->p + SWAP_META_PAGES) {
2567                         sb = SWAP_PCTRIE_LOOKUP_GE(
2568                             &object->un_pager.swp.swp_blks, pi);
2569                         if (sb == NULL || sb->p >= e)
2570                                 break;
2571                         for (i = 0; i < SWAP_META_PAGES; i++) {
2572                                 if (sb->p + i < e &&
2573                                     sb->d[i] != SWAPBLK_NONE)
2574                                         count++;
2575                         }
2576                 }
2577 unlock:
2578                 VM_OBJECT_RUNLOCK(object);
2579         }
2580         return (count);
2581 }
2582
2583 /*
2584  * GEOM backend
2585  *
2586  * Swapping onto disk devices.
2587  *
2588  */
2589
2590 static g_orphan_t swapgeom_orphan;
2591
2592 static struct g_class g_swap_class = {
2593         .name = "SWAP",
2594         .version = G_VERSION,
2595         .orphan = swapgeom_orphan,
2596 };
2597
2598 DECLARE_GEOM_CLASS(g_swap_class, g_class);
2599
2600
2601 static void
2602 swapgeom_close_ev(void *arg, int flags)
2603 {
2604         struct g_consumer *cp;
2605
2606         cp = arg;
2607         g_access(cp, -1, -1, 0);
2608         g_detach(cp);
2609         g_destroy_consumer(cp);
2610 }
2611
2612 /*
2613  * Add a reference to the g_consumer for an inflight transaction.
2614  */
2615 static void
2616 swapgeom_acquire(struct g_consumer *cp)
2617 {
2618
2619         mtx_assert(&sw_dev_mtx, MA_OWNED);
2620         cp->index++;
2621 }
2622
2623 /*
2624  * Remove a reference from the g_consumer.  Post a close event if all
2625  * references go away, since the function might be called from the
2626  * biodone context.
2627  */
2628 static void
2629 swapgeom_release(struct g_consumer *cp, struct swdevt *sp)
2630 {
2631
2632         mtx_assert(&sw_dev_mtx, MA_OWNED);
2633         cp->index--;
2634         if (cp->index == 0) {
2635                 if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0)
2636                         sp->sw_id = NULL;
2637         }
2638 }
2639
2640 static void
2641 swapgeom_done(struct bio *bp2)
2642 {
2643         struct swdevt *sp;
2644         struct buf *bp;
2645         struct g_consumer *cp;
2646
2647         bp = bp2->bio_caller2;
2648         cp = bp2->bio_from;
2649         bp->b_ioflags = bp2->bio_flags;
2650         if (bp2->bio_error)
2651                 bp->b_ioflags |= BIO_ERROR;
2652         bp->b_resid = bp->b_bcount - bp2->bio_completed;
2653         bp->b_error = bp2->bio_error;
2654         bp->b_caller1 = NULL;
2655         bufdone(bp);
2656         sp = bp2->bio_caller1;
2657         mtx_lock(&sw_dev_mtx);
2658         swapgeom_release(cp, sp);
2659         mtx_unlock(&sw_dev_mtx);
2660         g_destroy_bio(bp2);
2661 }
2662
2663 static void
2664 swapgeom_strategy(struct buf *bp, struct swdevt *sp)
2665 {
2666         struct bio *bio;
2667         struct g_consumer *cp;
2668
2669         mtx_lock(&sw_dev_mtx);
2670         cp = sp->sw_id;
2671         if (cp == NULL) {
2672                 mtx_unlock(&sw_dev_mtx);
2673                 bp->b_error = ENXIO;
2674                 bp->b_ioflags |= BIO_ERROR;
2675                 bufdone(bp);
2676                 return;
2677         }
2678         swapgeom_acquire(cp);
2679         mtx_unlock(&sw_dev_mtx);
2680         if (bp->b_iocmd == BIO_WRITE)
2681                 bio = g_new_bio();
2682         else
2683                 bio = g_alloc_bio();
2684         if (bio == NULL) {
2685                 mtx_lock(&sw_dev_mtx);
2686                 swapgeom_release(cp, sp);
2687                 mtx_unlock(&sw_dev_mtx);
2688                 bp->b_error = ENOMEM;
2689                 bp->b_ioflags |= BIO_ERROR;
2690                 printf("swap_pager: cannot allocate bio\n");
2691                 bufdone(bp);
2692                 return;
2693         }
2694
2695         bp->b_caller1 = bio;
2696         bio->bio_caller1 = sp;
2697         bio->bio_caller2 = bp;
2698         bio->bio_cmd = bp->b_iocmd;
2699         bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE;
2700         bio->bio_length = bp->b_bcount;
2701         bio->bio_done = swapgeom_done;
2702         if (!buf_mapped(bp)) {
2703                 bio->bio_ma = bp->b_pages;
2704                 bio->bio_data = unmapped_buf;
2705                 bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK;
2706                 bio->bio_ma_n = bp->b_npages;
2707                 bio->bio_flags |= BIO_UNMAPPED;
2708         } else {
2709                 bio->bio_data = bp->b_data;
2710                 bio->bio_ma = NULL;
2711         }
2712         g_io_request(bio, cp);
2713         return;
2714 }
2715
2716 static void
2717 swapgeom_orphan(struct g_consumer *cp)
2718 {
2719         struct swdevt *sp;
2720         int destroy;
2721
2722         mtx_lock(&sw_dev_mtx);
2723         TAILQ_FOREACH(sp, &swtailq, sw_list) {
2724                 if (sp->sw_id == cp) {
2725                         sp->sw_flags |= SW_CLOSING;
2726                         break;
2727                 }
2728         }
2729         /*
2730          * Drop reference we were created with. Do directly since we're in a
2731          * special context where we don't have to queue the call to
2732          * swapgeom_close_ev().
2733          */
2734         cp->index--;
2735         destroy = ((sp != NULL) && (cp->index == 0));
2736         if (destroy)
2737                 sp->sw_id = NULL;
2738         mtx_unlock(&sw_dev_mtx);
2739         if (destroy)
2740                 swapgeom_close_ev(cp, 0);
2741 }
2742
2743 static void
2744 swapgeom_close(struct thread *td, struct swdevt *sw)
2745 {
2746         struct g_consumer *cp;
2747
2748         mtx_lock(&sw_dev_mtx);
2749         cp = sw->sw_id;
2750         sw->sw_id = NULL;
2751         mtx_unlock(&sw_dev_mtx);
2752
2753         /*
2754          * swapgeom_close() may be called from the biodone context,
2755          * where we cannot perform topology changes.  Delegate the
2756          * work to the events thread.
2757          */
2758         if (cp != NULL)
2759                 g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL);
2760 }
2761
2762 static int
2763 swapongeom_locked(struct cdev *dev, struct vnode *vp)
2764 {
2765         struct g_provider *pp;
2766         struct g_consumer *cp;
2767         static struct g_geom *gp;
2768         struct swdevt *sp;
2769         u_long nblks;
2770         int error;
2771
2772         pp = g_dev_getprovider(dev);
2773         if (pp == NULL)
2774                 return (ENODEV);
2775         mtx_lock(&sw_dev_mtx);
2776         TAILQ_FOREACH(sp, &swtailq, sw_list) {
2777                 cp = sp->sw_id;
2778                 if (cp != NULL && cp->provider == pp) {
2779                         mtx_unlock(&sw_dev_mtx);
2780                         return (EBUSY);
2781                 }
2782         }
2783         mtx_unlock(&sw_dev_mtx);
2784         if (gp == NULL)
2785                 gp = g_new_geomf(&g_swap_class, "swap");
2786         cp = g_new_consumer(gp);
2787         cp->index = 1;  /* Number of active I/Os, plus one for being active. */
2788         cp->flags |=  G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2789         g_attach(cp, pp);
2790         /*
2791          * XXX: Every time you think you can improve the margin for
2792          * footshooting, somebody depends on the ability to do so:
2793          * savecore(8) wants to write to our swapdev so we cannot
2794          * set an exclusive count :-(
2795          */
2796         error = g_access(cp, 1, 1, 0);
2797         if (error != 0) {
2798                 g_detach(cp);
2799                 g_destroy_consumer(cp);
2800                 return (error);
2801         }
2802         nblks = pp->mediasize / DEV_BSIZE;
2803         swaponsomething(vp, cp, nblks, swapgeom_strategy,
2804             swapgeom_close, dev2udev(dev),
2805             (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0);
2806         return (0);
2807 }
2808
2809 static int
2810 swapongeom(struct vnode *vp)
2811 {
2812         int error;
2813
2814         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2815         if (vp->v_type != VCHR || (vp->v_iflag & VI_DOOMED) != 0) {
2816                 error = ENOENT;
2817         } else {
2818                 g_topology_lock();
2819                 error = swapongeom_locked(vp->v_rdev, vp);
2820                 g_topology_unlock();
2821         }
2822         VOP_UNLOCK(vp, 0);
2823         return (error);
2824 }
2825
2826 /*
2827  * VNODE backend
2828  *
2829  * This is used mainly for network filesystem (read: probably only tested
2830  * with NFS) swapfiles.
2831  *
2832  */
2833
2834 static void
2835 swapdev_strategy(struct buf *bp, struct swdevt *sp)
2836 {
2837         struct vnode *vp2;
2838
2839         bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first);
2840
2841         vp2 = sp->sw_id;
2842         vhold(vp2);
2843         if (bp->b_iocmd == BIO_WRITE) {
2844                 if (bp->b_bufobj)
2845                         bufobj_wdrop(bp->b_bufobj);
2846                 bufobj_wref(&vp2->v_bufobj);
2847         }
2848         if (bp->b_bufobj != &vp2->v_bufobj)
2849                 bp->b_bufobj = &vp2->v_bufobj;
2850         bp->b_vp = vp2;
2851         bp->b_iooffset = dbtob(bp->b_blkno);
2852         bstrategy(bp);
2853         return;
2854 }
2855
2856 static void
2857 swapdev_close(struct thread *td, struct swdevt *sp)
2858 {
2859
2860         VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td);
2861         vrele(sp->sw_vp);
2862 }
2863
2864
2865 static int
2866 swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
2867 {
2868         struct swdevt *sp;
2869         int error;
2870
2871         if (nblks == 0)
2872                 return (ENXIO);
2873         mtx_lock(&sw_dev_mtx);
2874         TAILQ_FOREACH(sp, &swtailq, sw_list) {
2875                 if (sp->sw_id == vp) {
2876                         mtx_unlock(&sw_dev_mtx);
2877                         return (EBUSY);
2878                 }
2879         }
2880         mtx_unlock(&sw_dev_mtx);
2881
2882         (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2883 #ifdef MAC
2884         error = mac_system_check_swapon(td->td_ucred, vp);
2885         if (error == 0)
2886 #endif
2887                 error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL);
2888         (void) VOP_UNLOCK(vp, 0);
2889         if (error)
2890                 return (error);
2891
2892         swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close,
2893             NODEV, 0);
2894         return (0);
2895 }
2896
2897 static int
2898 sysctl_swap_async_max(SYSCTL_HANDLER_ARGS)
2899 {
2900         int error, new, n;
2901
2902         new = nsw_wcount_async_max;
2903         error = sysctl_handle_int(oidp, &new, 0, req);
2904         if (error != 0 || req->newptr == NULL)
2905                 return (error);
2906
2907         if (new > nswbuf / 2 || new < 1)
2908                 return (EINVAL);
2909
2910         mtx_lock(&swbuf_mtx);
2911         while (nsw_wcount_async_max != new) {
2912                 /*
2913                  * Adjust difference.  If the current async count is too low,
2914                  * we will need to sqeeze our update slowly in.  Sleep with a
2915                  * higher priority than getpbuf() to finish faster.
2916                  */
2917                 n = new - nsw_wcount_async_max;
2918                 if (nsw_wcount_async + n >= 0) {
2919                         nsw_wcount_async += n;
2920                         nsw_wcount_async_max += n;
2921                         wakeup(&nsw_wcount_async);
2922                 } else {
2923                         nsw_wcount_async_max -= nsw_wcount_async;
2924                         nsw_wcount_async = 0;
2925                         msleep(&nsw_wcount_async, &swbuf_mtx, PSWP,
2926                             "swpsysctl", 0);
2927                 }
2928         }
2929         mtx_unlock(&swbuf_mtx);
2930
2931         return (0);
2932 }