2 * SPDX-License-Identifier: BSD-4-Clause
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.
10 * This code is derived from software contributed to Berkeley by
11 * the Systems Programming Group of the University of Utah Computer
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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.
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
45 * Radix Bitmap 'blists'.
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.
53 * - on the fly reallocation of swap during putpages. The new system
54 * does not try to keep previously allocated swap blocks for dirty
57 * - on the fly deallocation of swap
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
65 * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$
67 * @(#)swap_pager.c 8.9 (Berkeley) 3/21/94
68 * @(#)vm_swap.c 8.5 (Berkeley) 2/17/94
71 #include <sys/cdefs.h>
72 __FBSDID("$FreeBSD$");
76 #include <sys/param.h>
78 #include <sys/blist.h>
82 #include <sys/disklabel.h>
83 #include <sys/eventhandler.h>
84 #include <sys/fcntl.h>
85 #include <sys/limits.h>
87 #include <sys/kernel.h>
88 #include <sys/mount.h>
89 #include <sys/namei.h>
90 #include <sys/malloc.h>
91 #include <sys/pctrie.h>
94 #include <sys/racct.h>
95 #include <sys/resource.h>
96 #include <sys/resourcevar.h>
97 #include <sys/rwlock.h>
99 #include <sys/sysctl.h>
100 #include <sys/sysproto.h>
101 #include <sys/systm.h>
103 #include <sys/unistd.h>
104 #include <sys/user.h>
105 #include <sys/vmmeter.h>
106 #include <sys/vnode.h>
108 #include <security/mac/mac_framework.h>
112 #include <vm/vm_map.h>
113 #include <vm/vm_kern.h>
114 #include <vm/vm_object.h>
115 #include <vm/vm_page.h>
116 #include <vm/vm_pager.h>
117 #include <vm/vm_pageout.h>
118 #include <vm/vm_param.h>
119 #include <vm/swap_pager.h>
120 #include <vm/vm_extern.h>
123 #include <geom/geom.h>
126 * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64.
127 * The 64-page limit is due to the radix code (kern/subr_blist.c).
129 #ifndef MAX_PAGEOUT_CLUSTER
130 #define MAX_PAGEOUT_CLUSTER 32
133 #if !defined(SWB_NPAGES)
134 #define SWB_NPAGES MAX_PAGEOUT_CLUSTER
137 #define SWAP_META_PAGES PCTRIE_COUNT
140 * A swblk structure maps each page index within a
141 * SWAP_META_PAGES-aligned and sized range to the address of an
142 * on-disk swap block (or SWAPBLK_NONE). The collection of these
143 * mappings for an entire vm object is implemented as a pc-trie.
147 daddr_t d[SWAP_META_PAGES];
150 static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data");
151 static struct mtx sw_dev_mtx;
152 static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq);
153 static struct swdevt *swdevhd; /* Allocate from here next */
154 static int nswapdev; /* Number of swap devices */
155 int swap_pager_avail;
156 static struct sx swdev_syscall_lock; /* serialize swap(on|off) */
158 static __exclusive_cache_line u_long swap_reserved;
159 static u_long swap_total;
160 static int sysctl_page_shift(SYSCTL_HANDLER_ARGS);
162 static SYSCTL_NODE(_vm_stats, OID_AUTO, swap, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
165 SYSCTL_PROC(_vm, OID_AUTO, swap_reserved, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
166 &swap_reserved, 0, sysctl_page_shift, "QU",
167 "Amount of swap storage needed to back all allocated anonymous memory.");
168 SYSCTL_PROC(_vm, OID_AUTO, swap_total, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
169 &swap_total, 0, sysctl_page_shift, "QU",
170 "Total amount of available swap storage.");
172 int vm_overcommit __read_mostly = 0;
173 SYSCTL_INT(_vm, VM_OVERCOMMIT, overcommit, CTLFLAG_RW, &vm_overcommit, 0,
174 "Configure virtual memory overcommit behavior. See tuning(7) "
176 static unsigned long swzone;
177 SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0,
178 "Actual size of swap metadata zone");
179 static unsigned long swap_maxpages;
180 SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0,
181 "Maximum amount of swap supported");
183 static COUNTER_U64_DEFINE_EARLY(swap_free_deferred);
184 SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_deferred,
185 CTLFLAG_RD, &swap_free_deferred,
186 "Number of pages that deferred freeing swap space");
188 static COUNTER_U64_DEFINE_EARLY(swap_free_completed);
189 SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_completed,
190 CTLFLAG_RD, &swap_free_completed,
191 "Number of deferred frees completed");
194 sysctl_page_shift(SYSCTL_HANDLER_ARGS)
197 u_long value = *(u_long *)arg1;
199 newval = ((uint64_t)value) << PAGE_SHIFT;
200 return (sysctl_handle_64(oidp, &newval, 0, req));
204 swap_reserve_by_cred_rlimit(u_long pincr, struct ucred *cred, int oc)
209 uip = cred->cr_ruidinfo;
211 prev = atomic_fetchadd_long(&uip->ui_vmsize, pincr);
212 if ((oc & SWAP_RESERVE_RLIMIT_ON) != 0 &&
213 prev + pincr > lim_cur(curthread, RLIMIT_SWAP) &&
214 priv_check(curthread, PRIV_VM_SWAP_NORLIMIT) != 0) {
215 prev = atomic_fetchadd_long(&uip->ui_vmsize, -pincr);
216 KASSERT(prev >= pincr,
217 ("negative vmsize for uid %d\n", uip->ui_uid));
224 swap_release_by_cred_rlimit(u_long pdecr, struct ucred *cred)
231 uip = cred->cr_ruidinfo;
234 prev = atomic_fetchadd_long(&uip->ui_vmsize, -pdecr);
235 KASSERT(prev >= pdecr,
236 ("negative vmsize for uid %d\n", uip->ui_uid));
238 atomic_subtract_long(&uip->ui_vmsize, pdecr);
243 swap_reserve_force_rlimit(u_long pincr, struct ucred *cred)
247 uip = cred->cr_ruidinfo;
248 atomic_add_long(&uip->ui_vmsize, pincr);
252 swap_reserve(vm_ooffset_t incr)
255 return (swap_reserve_by_cred(incr, curthread->td_ucred));
259 swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred)
261 u_long r, s, prev, pincr;
267 static struct timeval lastfail;
269 KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK",
270 __func__, (uintmax_t)incr));
273 if (RACCT_ENABLED()) {
275 error = racct_add(curproc, RACCT_SWAP, incr);
276 PROC_UNLOCK(curproc);
283 prev = atomic_fetchadd_long(&swap_reserved, pincr);
286 oc = atomic_load_int(&vm_overcommit);
287 if (r > s && (oc & SWAP_RESERVE_ALLOW_NONWIRED) != 0) {
288 s += vm_cnt.v_page_count - vm_cnt.v_free_reserved -
291 if ((oc & SWAP_RESERVE_FORCE_ON) != 0 && r > s &&
292 priv_check(curthread, PRIV_VM_SWAP_NOQUOTA) != 0) {
293 prev = atomic_fetchadd_long(&swap_reserved, -pincr);
294 KASSERT(prev >= pincr,
295 ("swap_reserved < incr on overcommit fail"));
299 if (!swap_reserve_by_cred_rlimit(pincr, cred, oc)) {
300 prev = atomic_fetchadd_long(&swap_reserved, -pincr);
301 KASSERT(prev >= pincr,
302 ("swap_reserved < incr on overcommit fail"));
309 if (ppsratecheck(&lastfail, &curfail, 1)) {
310 printf("uid %d, pid %d: swap reservation "
311 "for %jd bytes failed\n",
312 cred->cr_ruidinfo->ui_uid, curproc->p_pid, incr);
315 if (RACCT_ENABLED()) {
317 racct_sub(curproc, RACCT_SWAP, incr);
318 PROC_UNLOCK(curproc);
326 swap_reserve_force(vm_ooffset_t incr)
330 KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK",
331 __func__, (uintmax_t)incr));
334 if (RACCT_ENABLED()) {
336 racct_add_force(curproc, RACCT_SWAP, incr);
337 PROC_UNLOCK(curproc);
341 atomic_add_long(&swap_reserved, pincr);
342 swap_reserve_force_rlimit(pincr, curthread->td_ucred);
346 swap_release(vm_ooffset_t decr)
351 cred = curproc->p_ucred;
352 swap_release_by_cred(decr, cred);
353 PROC_UNLOCK(curproc);
357 swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred)
364 KASSERT((decr & PAGE_MASK) == 0, ("%s: decr: %ju & PAGE_MASK",
365 __func__, (uintmax_t)decr));
369 prev = atomic_fetchadd_long(&swap_reserved, -pdecr);
370 KASSERT(prev >= pdecr, ("swap_reserved < decr"));
372 atomic_subtract_long(&swap_reserved, pdecr);
375 swap_release_by_cred_rlimit(pdecr, cred);
378 racct_sub_cred(cred, RACCT_SWAP, decr);
382 static int swap_pager_full = 2; /* swap space exhaustion (task killing) */
383 static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/
384 static struct mtx swbuf_mtx; /* to sync nsw_wcount_async */
385 static int nsw_wcount_async; /* limit async write buffers */
386 static int nsw_wcount_async_max;/* assigned maximum */
387 int nsw_cluster_max; /* maximum VOP I/O allowed */
389 static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS);
390 SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW |
391 CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I",
392 "Maximum running async swap ops");
393 static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS);
394 SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD |
395 CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A",
396 "Swap Fragmentation Info");
398 static struct sx sw_alloc_sx;
401 * "named" and "unnamed" anon region objects. Try to reduce the overhead
402 * of searching a named list by hashing it just a little.
407 #define NOBJLIST(handle) \
408 (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
410 static struct pagerlst swap_pager_object_list[NOBJLISTS];
411 static uma_zone_t swwbuf_zone;
412 static uma_zone_t swrbuf_zone;
413 static uma_zone_t swblk_zone;
414 static uma_zone_t swpctrie_zone;
417 * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure
418 * calls hooked from other parts of the VM system and do not appear here.
419 * (see vm/swap_pager.h).
422 swap_pager_alloc(void *handle, vm_ooffset_t size,
423 vm_prot_t prot, vm_ooffset_t offset, struct ucred *);
424 static void swap_pager_dealloc(vm_object_t object);
425 static int swap_pager_getpages(vm_object_t, vm_page_t *, int, int *,
427 static int swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
428 int *, pgo_getpages_iodone_t, void *);
429 static void swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
431 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after);
432 static void swap_pager_init(void);
433 static void swap_pager_unswapped(vm_page_t);
434 static void swap_pager_swapoff(struct swdevt *sp);
435 static void swap_pager_update_writecount(vm_object_t object,
436 vm_offset_t start, vm_offset_t end);
437 static void swap_pager_release_writecount(vm_object_t object,
438 vm_offset_t start, vm_offset_t end);
439 static void swap_pager_freespace(vm_object_t object, vm_pindex_t start,
442 const struct pagerops swappagerops = {
443 .pgo_kvme_type = KVME_TYPE_SWAP,
444 .pgo_init = swap_pager_init, /* early system initialization of pager */
445 .pgo_alloc = swap_pager_alloc, /* allocate an OBJT_SWAP object */
446 .pgo_dealloc = swap_pager_dealloc, /* deallocate an OBJT_SWAP object */
447 .pgo_getpages = swap_pager_getpages, /* pagein */
448 .pgo_getpages_async = swap_pager_getpages_async, /* pagein (async) */
449 .pgo_putpages = swap_pager_putpages, /* pageout */
450 .pgo_haspage = swap_pager_haspage, /* get backing store status for page */
451 .pgo_pageunswapped = swap_pager_unswapped, /* remove swap related to page */
452 .pgo_update_writecount = swap_pager_update_writecount,
453 .pgo_release_writecount = swap_pager_release_writecount,
454 .pgo_freespace = swap_pager_freespace,
458 * swap_*() routines are externally accessible. swp_*() routines are
461 static int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */
462 static int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */
464 SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0,
465 "Maximum size of a swap block in pages");
467 static void swp_sizecheck(void);
468 static void swp_pager_async_iodone(struct buf *bp);
469 static bool swp_pager_swblk_empty(struct swblk *sb, int start, int limit);
470 static void swp_pager_free_empty_swblk(vm_object_t, struct swblk *sb);
471 static int swapongeom(struct vnode *);
472 static int swaponvp(struct thread *, struct vnode *, u_long);
473 static int swapoff_one(struct swdevt *sp, struct ucred *cred,
477 * Swap bitmap functions
479 static void swp_pager_freeswapspace(daddr_t blk, daddr_t npages);
480 static daddr_t swp_pager_getswapspace(int *npages);
485 static daddr_t swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t);
486 static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t);
487 static void swp_pager_meta_transfer(vm_object_t src, vm_object_t dst,
488 vm_pindex_t pindex, vm_pindex_t count);
489 static void swp_pager_meta_free_all(vm_object_t);
490 static daddr_t swp_pager_meta_lookup(vm_object_t, vm_pindex_t);
493 swp_pager_init_freerange(daddr_t *start, daddr_t *num)
496 *start = SWAPBLK_NONE;
501 swp_pager_update_freerange(daddr_t *start, daddr_t *num, daddr_t addr)
504 if (*start + *num == addr) {
507 swp_pager_freeswapspace(*start, *num);
514 swblk_trie_alloc(struct pctrie *ptree)
517 return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ?
518 M_USE_RESERVE : 0)));
522 swblk_trie_free(struct pctrie *ptree, void *node)
525 uma_zfree(swpctrie_zone, node);
528 PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free);
531 * SWP_SIZECHECK() - update swap_pager_full indication
533 * update the swap_pager_almost_full indication and warn when we are
534 * about to run out of swap space, using lowat/hiwat hysteresis.
536 * Clear swap_pager_full ( task killing ) indication when lowat is met.
538 * No restrictions on call
539 * This routine may not block.
545 if (swap_pager_avail < nswap_lowat) {
546 if (swap_pager_almost_full == 0) {
547 printf("swap_pager: out of swap space\n");
548 swap_pager_almost_full = 1;
552 if (swap_pager_avail > nswap_hiwat)
553 swap_pager_almost_full = 0;
558 * SWAP_PAGER_INIT() - initialize the swap pager!
560 * Expected to be started from system init. NOTE: This code is run
561 * before much else so be careful what you depend on. Most of the VM
562 * system has yet to be initialized at this point.
565 swap_pager_init(void)
568 * Initialize object lists
572 for (i = 0; i < NOBJLISTS; ++i)
573 TAILQ_INIT(&swap_pager_object_list[i]);
574 mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF);
575 sx_init(&sw_alloc_sx, "swspsx");
576 sx_init(&swdev_syscall_lock, "swsysc");
579 * The nsw_cluster_max is constrained by the bp->b_pages[]
580 * array, which has maxphys / PAGE_SIZE entries, and our locally
581 * defined MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are
582 * constrained by the swap device interleave stripe size.
584 * Initialized early so that GEOM_ELI can see it.
586 nsw_cluster_max = min(maxphys / PAGE_SIZE, MAX_PAGEOUT_CLUSTER);
590 * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
592 * Expected to be started from pageout process once, prior to entering
596 swap_pager_swap_init(void)
601 * Number of in-transit swap bp operations. Don't
602 * exhaust the pbufs completely. Make sure we
603 * initialize workable values (0 will work for hysteresis
604 * but it isn't very efficient).
606 * Currently we hardwire nsw_wcount_async to 4. This limit is
607 * designed to prevent other I/O from having high latencies due to
608 * our pageout I/O. The value 4 works well for one or two active swap
609 * devices but is probably a little low if you have more. Even so,
610 * a higher value would probably generate only a limited improvement
611 * with three or four active swap devices since the system does not
612 * typically have to pageout at extreme bandwidths. We will want
613 * at least 2 per swap devices, and 4 is a pretty good value if you
614 * have one NFS swap device due to the command/ack latency over NFS.
615 * So it all works out pretty well.
617 * nsw_cluster_max is initialized in swap_pager_init().
620 nsw_wcount_async = 4;
621 nsw_wcount_async_max = nsw_wcount_async;
622 mtx_init(&swbuf_mtx, "async swbuf mutex", NULL, MTX_DEF);
624 swwbuf_zone = pbuf_zsecond_create("swwbuf", nswbuf / 4);
625 swrbuf_zone = pbuf_zsecond_create("swrbuf", nswbuf / 2);
628 * Initialize our zone, taking the user's requested size or
629 * estimating the number we need based on the number of pages
632 n = maxswzone != 0 ? maxswzone / sizeof(struct swblk) :
633 vm_cnt.v_page_count / 2;
634 swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL,
635 pctrie_zone_init, NULL, UMA_ALIGN_PTR, 0);
636 swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL,
637 NULL, NULL, _Alignof(struct swblk) - 1, 0);
640 if (uma_zone_reserve_kva(swblk_zone, n))
643 * if the allocation failed, try a zone two thirds the
644 * size of the previous attempt.
650 * Often uma_zone_reserve_kva() cannot reserve exactly the
651 * requested size. Account for the difference when
652 * calculating swap_maxpages.
654 n = uma_zone_get_max(swblk_zone);
657 printf("Swap blk zone entries changed from %lu to %lu.\n",
659 /* absolute maximum we can handle assuming 100% efficiency */
660 swap_maxpages = n * SWAP_META_PAGES;
661 swzone = n * sizeof(struct swblk);
662 if (!uma_zone_reserve_kva(swpctrie_zone, n))
663 printf("Cannot reserve swap pctrie zone, "
664 "reduce kern.maxswzone.\n");
668 swap_pager_init_object(vm_object_t object, void *handle, struct ucred *cred,
669 vm_ooffset_t size, vm_ooffset_t offset)
672 if (!swap_reserve_by_cred(size, cred))
677 object->un_pager.swp.writemappings = 0;
678 object->handle = handle;
681 object->charge = size;
687 swap_pager_alloc_init(objtype_t otype, void *handle, struct ucred *cred,
688 vm_ooffset_t size, vm_ooffset_t offset)
693 * The un_pager.swp.swp_blks trie is initialized by
694 * vm_object_allocate() to ensure the correct order of
695 * visibility to other threads.
697 object = vm_object_allocate(otype, OFF_TO_IDX(offset +
700 if (!swap_pager_init_object(object, handle, cred, size, offset)) {
701 vm_object_deallocate(object);
708 * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate
709 * its metadata structures.
711 * This routine is called from the mmap and fork code to create a new
714 * This routine must ensure that no live duplicate is created for
715 * the named object request, which is protected against by
716 * holding the sw_alloc_sx lock in case handle != NULL.
719 swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
720 vm_ooffset_t offset, struct ucred *cred)
724 if (handle != NULL) {
726 * Reference existing named region or allocate new one. There
727 * should not be a race here against swp_pager_meta_build()
728 * as called from vm_page_remove() in regards to the lookup
731 sx_xlock(&sw_alloc_sx);
732 object = vm_pager_object_lookup(NOBJLIST(handle), handle);
733 if (object == NULL) {
734 object = swap_pager_alloc_init(OBJT_SWAP, handle, cred,
736 if (object != NULL) {
737 TAILQ_INSERT_TAIL(NOBJLIST(object->handle),
738 object, pager_object_list);
741 sx_xunlock(&sw_alloc_sx);
743 object = swap_pager_alloc_init(OBJT_SWAP, handle, cred,
750 * SWAP_PAGER_DEALLOC() - remove swap metadata from object
752 * The swap backing for the object is destroyed. The code is
753 * designed such that we can reinstantiate it later, but this
754 * routine is typically called only when the entire object is
755 * about to be destroyed.
757 * The object must be locked.
760 swap_pager_dealloc(vm_object_t object)
763 VM_OBJECT_ASSERT_WLOCKED(object);
764 KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj"));
767 * Remove from list right away so lookups will fail if we block for
768 * pageout completion.
770 if ((object->flags & OBJ_ANON) == 0 && object->handle != NULL) {
771 VM_OBJECT_WUNLOCK(object);
772 sx_xlock(&sw_alloc_sx);
773 TAILQ_REMOVE(NOBJLIST(object->handle), object,
775 sx_xunlock(&sw_alloc_sx);
776 VM_OBJECT_WLOCK(object);
779 vm_object_pip_wait(object, "swpdea");
782 * Free all remaining metadata. We only bother to free it from
783 * the swap meta data. We do not attempt to free swapblk's still
784 * associated with vm_page_t's for this object. We do not care
785 * if paging is still in progress on some objects.
787 swp_pager_meta_free_all(object);
788 object->handle = NULL;
789 object->type = OBJT_DEAD;
792 * Release the allocation charge.
794 if (object->cred != NULL) {
795 swap_release_by_cred(object->charge, object->cred);
797 crfree(object->cred);
802 * Hide the object from swap_pager_swapoff().
804 vm_object_clear_flag(object, OBJ_SWAP);
807 /************************************************************************
808 * SWAP PAGER BITMAP ROUTINES *
809 ************************************************************************/
812 * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space
814 * Allocate swap for up to the requested number of pages. The
815 * starting swap block number (a page index) is returned or
816 * SWAPBLK_NONE if the allocation failed.
818 * Also has the side effect of advising that somebody made a mistake
819 * when they configured swap and didn't configure enough.
821 * This routine may not sleep.
823 * We allocate in round-robin fashion from the configured devices.
826 swp_pager_getswapspace(int *io_npages)
832 KASSERT(*io_npages >= 1,
833 ("%s: npages not positive", __func__));
836 npages = imin(BLIST_MAX_ALLOC, mpages);
837 mtx_lock(&sw_dev_mtx);
839 while (!TAILQ_EMPTY(&swtailq)) {
841 sp = TAILQ_FIRST(&swtailq);
842 if ((sp->sw_flags & SW_CLOSING) == 0)
843 blk = blist_alloc(sp->sw_blist, &npages, mpages);
844 if (blk != SWAPBLK_NONE)
846 sp = TAILQ_NEXT(sp, sw_list);
854 if (blk != SWAPBLK_NONE) {
857 sp->sw_used += npages;
858 swap_pager_avail -= npages;
860 swdevhd = TAILQ_NEXT(sp, sw_list);
862 if (swap_pager_full != 2) {
863 printf("swp_pager_getswapspace(%d): failed\n",
866 swap_pager_almost_full = 1;
870 mtx_unlock(&sw_dev_mtx);
875 swp_pager_isondev(daddr_t blk, struct swdevt *sp)
878 return (blk >= sp->sw_first && blk < sp->sw_end);
882 swp_pager_strategy(struct buf *bp)
886 mtx_lock(&sw_dev_mtx);
887 TAILQ_FOREACH(sp, &swtailq, sw_list) {
888 if (swp_pager_isondev(bp->b_blkno, sp)) {
889 mtx_unlock(&sw_dev_mtx);
890 if ((sp->sw_flags & SW_UNMAPPED) != 0 &&
891 unmapped_buf_allowed) {
892 bp->b_data = unmapped_buf;
895 pmap_qenter((vm_offset_t)bp->b_data,
896 &bp->b_pages[0], bp->b_bcount / PAGE_SIZE);
898 sp->sw_strategy(bp, sp);
902 panic("Swapdev not found");
906 * SWP_PAGER_FREESWAPSPACE() - free raw swap space
908 * This routine returns the specified swap blocks back to the bitmap.
910 * This routine may not sleep.
913 swp_pager_freeswapspace(daddr_t blk, daddr_t npages)
919 mtx_lock(&sw_dev_mtx);
920 TAILQ_FOREACH(sp, &swtailq, sw_list) {
921 if (swp_pager_isondev(blk, sp)) {
922 sp->sw_used -= npages;
924 * If we are attempting to stop swapping on
925 * this device, we don't want to mark any
926 * blocks free lest they be reused.
928 if ((sp->sw_flags & SW_CLOSING) == 0) {
929 blist_free(sp->sw_blist, blk - sp->sw_first,
931 swap_pager_avail += npages;
934 mtx_unlock(&sw_dev_mtx);
938 panic("Swapdev not found");
942 * SYSCTL_SWAP_FRAGMENTATION() - produce raw swap space stats
945 sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS)
952 error = sysctl_wire_old_buffer(req, 0);
955 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
956 mtx_lock(&sw_dev_mtx);
957 TAILQ_FOREACH(sp, &swtailq, sw_list) {
958 if (vn_isdisk(sp->sw_vp))
959 devname = devtoname(sp->sw_vp->v_rdev);
962 sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname);
963 blist_stats(sp->sw_blist, &sbuf);
965 mtx_unlock(&sw_dev_mtx);
966 error = sbuf_finish(&sbuf);
972 * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page
973 * range within an object.
975 * This routine removes swapblk assignments from swap metadata.
977 * The external callers of this routine typically have already destroyed
978 * or renamed vm_page_t's associated with this range in the object so
981 * The object must be locked.
984 swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size)
987 swp_pager_meta_free(object, start, size);
991 * SWAP_PAGER_RESERVE() - reserve swap blocks in object
993 * Assigns swap blocks to the specified range within the object. The
994 * swap blocks are not zeroed. Any previous swap assignment is destroyed.
996 * Returns 0 on success, -1 on failure.
999 swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_pindex_t size)
1001 daddr_t addr, blk, n_free, s_free;
1005 swp_pager_init_freerange(&s_free, &n_free);
1006 VM_OBJECT_WLOCK(object);
1007 for (i = 0; i < size; i += n) {
1008 n = MIN(size - i, INT_MAX);
1009 blk = swp_pager_getswapspace(&n);
1010 if (blk == SWAPBLK_NONE) {
1011 swp_pager_meta_free(object, start, i);
1012 VM_OBJECT_WUNLOCK(object);
1015 for (j = 0; j < n; ++j) {
1016 addr = swp_pager_meta_build(object,
1017 start + i + j, blk + j);
1018 if (addr != SWAPBLK_NONE)
1019 swp_pager_update_freerange(&s_free, &n_free,
1023 swp_pager_freeswapspace(s_free, n_free);
1024 VM_OBJECT_WUNLOCK(object);
1029 swp_pager_xfer_source(vm_object_t srcobject, vm_object_t dstobject,
1030 vm_pindex_t pindex, daddr_t addr)
1032 daddr_t dstaddr __diagused;
1034 KASSERT((srcobject->flags & OBJ_SWAP) != 0,
1035 ("%s: srcobject not swappable", __func__));
1036 KASSERT((dstobject->flags & OBJ_SWAP) != 0,
1037 ("%s: dstobject not swappable", __func__));
1039 if (swp_pager_meta_lookup(dstobject, pindex) != SWAPBLK_NONE) {
1040 /* Caller should destroy the source block. */
1045 * Destination has no swapblk and is not resident, transfer source.
1046 * swp_pager_meta_build() can sleep.
1048 VM_OBJECT_WUNLOCK(srcobject);
1049 dstaddr = swp_pager_meta_build(dstobject, pindex, addr);
1050 KASSERT(dstaddr == SWAPBLK_NONE,
1051 ("Unexpected destination swapblk"));
1052 VM_OBJECT_WLOCK(srcobject);
1058 * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager
1059 * and destroy the source.
1061 * Copy any valid swapblks from the source to the destination. In
1062 * cases where both the source and destination have a valid swapblk,
1063 * we keep the destination's.
1065 * This routine is allowed to sleep. It may sleep allocating metadata
1066 * indirectly through swp_pager_meta_build().
1068 * The source object contains no vm_page_t's (which is just as well)
1070 * The source and destination objects must be locked.
1071 * Both object locks may temporarily be released.
1074 swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject,
1075 vm_pindex_t offset, int destroysource)
1077 VM_OBJECT_ASSERT_WLOCKED(srcobject);
1078 VM_OBJECT_ASSERT_WLOCKED(dstobject);
1081 * If destroysource is set, we remove the source object from the
1082 * swap_pager internal queue now.
1084 if (destroysource && (srcobject->flags & OBJ_ANON) == 0 &&
1085 srcobject->handle != NULL) {
1086 VM_OBJECT_WUNLOCK(srcobject);
1087 VM_OBJECT_WUNLOCK(dstobject);
1088 sx_xlock(&sw_alloc_sx);
1089 TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject,
1091 sx_xunlock(&sw_alloc_sx);
1092 VM_OBJECT_WLOCK(dstobject);
1093 VM_OBJECT_WLOCK(srcobject);
1097 * Transfer source to destination.
1099 swp_pager_meta_transfer(srcobject, dstobject, offset, dstobject->size);
1102 * Free left over swap blocks in source.
1105 swp_pager_meta_free_all(srcobject);
1109 * SWAP_PAGER_HASPAGE() - determine if we have good backing store for
1110 * the requested page.
1112 * We determine whether good backing store exists for the requested
1113 * page and return TRUE if it does, FALSE if it doesn't.
1115 * If TRUE, we also try to determine how much valid, contiguous backing
1116 * store exists before and after the requested page.
1119 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
1125 VM_OBJECT_ASSERT_LOCKED(object);
1126 KASSERT((object->flags & OBJ_SWAP) != 0,
1127 ("%s: object not swappable", __func__));
1130 * do we have good backing store at the requested index ?
1132 blk0 = swp_pager_meta_lookup(object, pindex);
1133 if (blk0 == SWAPBLK_NONE) {
1142 * find backwards-looking contiguous good backing store
1144 if (before != NULL) {
1145 for (i = 1; i < SWB_NPAGES; i++) {
1148 blk = swp_pager_meta_lookup(object, pindex - i);
1149 if (blk != blk0 - i)
1156 * find forward-looking contiguous good backing store
1158 if (after != NULL) {
1159 for (i = 1; i < SWB_NPAGES; i++) {
1160 blk = swp_pager_meta_lookup(object, pindex + i);
1161 if (blk != blk0 + i)
1170 * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
1172 * This removes any associated swap backing store, whether valid or
1173 * not, from the page.
1175 * This routine is typically called when a page is made dirty, at
1176 * which point any associated swap can be freed. MADV_FREE also
1177 * calls us in a special-case situation
1179 * NOTE!!! If the page is clean and the swap was valid, the caller
1180 * should make the page dirty before calling this routine. This routine
1181 * does NOT change the m->dirty status of the page. Also: MADV_FREE
1184 * This routine may not sleep.
1186 * The object containing the page may be locked.
1189 swap_pager_unswapped(vm_page_t m)
1195 * Handle enqueing deferred frees first. If we do not have the
1196 * object lock we wait for the page daemon to clear the space.
1199 if (!VM_OBJECT_WOWNED(obj)) {
1200 VM_PAGE_OBJECT_BUSY_ASSERT(m);
1202 * The caller is responsible for synchronization but we
1203 * will harmlessly handle races. This is typically provided
1204 * by only calling unswapped() when a page transitions from
1207 if ((m->a.flags & (PGA_SWAP_SPACE | PGA_SWAP_FREE)) ==
1209 vm_page_aflag_set(m, PGA_SWAP_FREE);
1210 counter_u64_add(swap_free_deferred, 1);
1214 if ((m->a.flags & PGA_SWAP_FREE) != 0)
1215 counter_u64_add(swap_free_completed, 1);
1216 vm_page_aflag_clear(m, PGA_SWAP_FREE | PGA_SWAP_SPACE);
1219 * The meta data only exists if the object is OBJT_SWAP
1220 * and even then might not be allocated yet.
1222 KASSERT((m->object->flags & OBJ_SWAP) != 0,
1223 ("Free object not swappable"));
1225 sb = SWAP_PCTRIE_LOOKUP(&m->object->un_pager.swp.swp_blks,
1226 rounddown(m->pindex, SWAP_META_PAGES));
1229 if (sb->d[m->pindex % SWAP_META_PAGES] == SWAPBLK_NONE)
1231 swp_pager_freeswapspace(sb->d[m->pindex % SWAP_META_PAGES], 1);
1232 sb->d[m->pindex % SWAP_META_PAGES] = SWAPBLK_NONE;
1233 swp_pager_free_empty_swblk(m->object, sb);
1237 * swap_pager_getpages() - bring pages in from swap
1239 * Attempt to page in the pages in array "ma" of length "count". The
1240 * caller may optionally specify that additional pages preceding and
1241 * succeeding the specified range be paged in. The number of such pages
1242 * is returned in the "rbehind" and "rahead" parameters, and they will
1243 * be in the inactive queue upon return.
1245 * The pages in "ma" must be busied and will remain busied upon return.
1248 swap_pager_getpages_locked(vm_object_t object, vm_page_t *ma, int count,
1249 int *rbehind, int *rahead)
1252 vm_page_t bm, mpred, msucc, p;
1255 int i, maxahead, maxbehind, reqcount;
1257 VM_OBJECT_ASSERT_WLOCKED(object);
1260 KASSERT((object->flags & OBJ_SWAP) != 0,
1261 ("%s: object not swappable", __func__));
1262 if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead)) {
1263 VM_OBJECT_WUNLOCK(object);
1264 return (VM_PAGER_FAIL);
1267 KASSERT(reqcount - 1 <= maxahead,
1268 ("page count %d extends beyond swap block", reqcount));
1271 * Do not transfer any pages other than those that are xbusied
1272 * when running during a split or collapse operation. This
1273 * prevents clustering from re-creating pages which are being
1274 * moved into another object.
1276 if ((object->flags & (OBJ_SPLIT | OBJ_DEAD)) != 0) {
1277 maxahead = reqcount - 1;
1282 * Clip the readahead and readbehind ranges to exclude resident pages.
1284 if (rahead != NULL) {
1285 *rahead = imin(*rahead, maxahead - (reqcount - 1));
1286 pindex = ma[reqcount - 1]->pindex;
1287 msucc = TAILQ_NEXT(ma[reqcount - 1], listq);
1288 if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead)
1289 *rahead = msucc->pindex - pindex - 1;
1291 if (rbehind != NULL) {
1292 *rbehind = imin(*rbehind, maxbehind);
1293 pindex = ma[0]->pindex;
1294 mpred = TAILQ_PREV(ma[0], pglist, listq);
1295 if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind)
1296 *rbehind = pindex - mpred->pindex - 1;
1300 for (i = 0; i < count; i++)
1301 ma[i]->oflags |= VPO_SWAPINPROG;
1304 * Allocate readahead and readbehind pages.
1306 if (rbehind != NULL) {
1307 for (i = 1; i <= *rbehind; i++) {
1308 p = vm_page_alloc(object, ma[0]->pindex - i,
1312 p->oflags |= VPO_SWAPINPROG;
1317 if (rahead != NULL) {
1318 for (i = 0; i < *rahead; i++) {
1319 p = vm_page_alloc(object,
1320 ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL);
1323 p->oflags |= VPO_SWAPINPROG;
1327 if (rbehind != NULL)
1332 vm_object_pip_add(object, count);
1334 pindex = bm->pindex;
1335 blk = swp_pager_meta_lookup(object, pindex);
1336 KASSERT(blk != SWAPBLK_NONE,
1337 ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex));
1339 VM_OBJECT_WUNLOCK(object);
1340 bp = uma_zalloc(swrbuf_zone, M_WAITOK);
1341 MPASS((bp->b_flags & B_MAXPHYS) != 0);
1342 /* Pages cannot leave the object while busy. */
1343 for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) {
1344 MPASS(p->pindex == bm->pindex + i);
1348 bp->b_flags |= B_PAGING;
1349 bp->b_iocmd = BIO_READ;
1350 bp->b_iodone = swp_pager_async_iodone;
1351 bp->b_rcred = crhold(thread0.td_ucred);
1352 bp->b_wcred = crhold(thread0.td_ucred);
1354 bp->b_bcount = PAGE_SIZE * count;
1355 bp->b_bufsize = PAGE_SIZE * count;
1356 bp->b_npages = count;
1357 bp->b_pgbefore = rbehind != NULL ? *rbehind : 0;
1358 bp->b_pgafter = rahead != NULL ? *rahead : 0;
1360 VM_CNT_INC(v_swapin);
1361 VM_CNT_ADD(v_swappgsin, count);
1364 * perform the I/O. NOTE!!! bp cannot be considered valid after
1365 * this point because we automatically release it on completion.
1366 * Instead, we look at the one page we are interested in which we
1367 * still hold a lock on even through the I/O completion.
1369 * The other pages in our ma[] array are also released on completion,
1370 * so we cannot assume they are valid anymore either.
1372 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
1375 swp_pager_strategy(bp);
1378 * Wait for the pages we want to complete. VPO_SWAPINPROG is always
1379 * cleared on completion. If an I/O error occurs, SWAPBLK_NONE
1380 * is set in the metadata for each page in the request.
1382 VM_OBJECT_WLOCK(object);
1383 /* This could be implemented more efficiently with aflags */
1384 while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) {
1385 ma[0]->oflags |= VPO_SWAPSLEEP;
1386 VM_CNT_INC(v_intrans);
1387 if (VM_OBJECT_SLEEP(object, &object->handle, PSWP,
1388 "swread", hz * 20)) {
1390 "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n",
1391 bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount);
1394 VM_OBJECT_WUNLOCK(object);
1397 * If we had an unrecoverable read error pages will not be valid.
1399 for (i = 0; i < reqcount; i++)
1400 if (ma[i]->valid != VM_PAGE_BITS_ALL)
1401 return (VM_PAGER_ERROR);
1403 return (VM_PAGER_OK);
1406 * A final note: in a low swap situation, we cannot deallocate swap
1407 * and mark a page dirty here because the caller is likely to mark
1408 * the page clean when we return, causing the page to possibly revert
1409 * to all-zero's later.
1414 swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count,
1415 int *rbehind, int *rahead)
1418 VM_OBJECT_WLOCK(object);
1419 return (swap_pager_getpages_locked(object, ma, count, rbehind, rahead));
1423 * swap_pager_getpages_async():
1425 * Right now this is emulation of asynchronous operation on top of
1426 * swap_pager_getpages().
1429 swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count,
1430 int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
1434 r = swap_pager_getpages(object, ma, count, rbehind, rahead);
1439 case VM_PAGER_ERROR:
1446 panic("unhandled swap_pager_getpages() error %d", r);
1448 (iodone)(arg, ma, count, error);
1454 * swap_pager_putpages:
1456 * Assign swap (if necessary) and initiate I/O on the specified pages.
1458 * In a low memory situation we may block in VOP_STRATEGY(), but the new
1459 * vm_page reservation system coupled with properly written VFS devices
1460 * should ensure that no low-memory deadlock occurs. This is an area
1463 * The parent has N vm_object_pip_add() references prior to
1464 * calling us and will remove references for rtvals[] that are
1465 * not set to VM_PAGER_PEND. We need to remove the rest on I/O
1468 * The parent has soft-busy'd the pages it passes us and will unbusy
1469 * those whose rtvals[] entry is not set to VM_PAGER_PEND on return.
1470 * We need to unbusy the rest on I/O completion.
1473 swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count,
1474 int flags, int *rtvals)
1477 daddr_t addr, blk, n_free, s_free;
1482 KASSERT(count == 0 || ma[0]->object == object,
1483 ("%s: object mismatch %p/%p",
1484 __func__, object, ma[0]->object));
1486 VM_OBJECT_WUNLOCK(object);
1487 async = curproc == pageproc && (flags & VM_PAGER_PUT_SYNC) == 0;
1488 swp_pager_init_freerange(&s_free, &n_free);
1491 * Assign swap blocks and issue I/O. We reallocate swap on the fly.
1492 * The page is left dirty until the pageout operation completes
1495 for (i = 0; i < count; i += n) {
1496 /* Maximum I/O size is limited by maximum swap block size. */
1497 n = min(count - i, nsw_cluster_max);
1500 mtx_lock(&swbuf_mtx);
1501 while (nsw_wcount_async == 0)
1502 msleep(&nsw_wcount_async, &swbuf_mtx, PVM,
1505 mtx_unlock(&swbuf_mtx);
1508 /* Get a block of swap of size up to size n. */
1509 blk = swp_pager_getswapspace(&n);
1510 if (blk == SWAPBLK_NONE) {
1511 mtx_lock(&swbuf_mtx);
1512 if (++nsw_wcount_async == 1)
1513 wakeup(&nsw_wcount_async);
1514 mtx_unlock(&swbuf_mtx);
1515 for (j = 0; j < n; ++j)
1516 rtvals[i + j] = VM_PAGER_FAIL;
1519 VM_OBJECT_WLOCK(object);
1520 for (j = 0; j < n; ++j) {
1522 vm_page_aflag_clear(mreq, PGA_SWAP_FREE);
1523 addr = swp_pager_meta_build(mreq->object, mreq->pindex,
1525 if (addr != SWAPBLK_NONE)
1526 swp_pager_update_freerange(&s_free, &n_free,
1528 MPASS(mreq->dirty == VM_PAGE_BITS_ALL);
1529 mreq->oflags |= VPO_SWAPINPROG;
1531 VM_OBJECT_WUNLOCK(object);
1533 bp = uma_zalloc(swwbuf_zone, M_WAITOK);
1534 MPASS((bp->b_flags & B_MAXPHYS) != 0);
1536 bp->b_flags |= B_ASYNC;
1537 bp->b_flags |= B_PAGING;
1538 bp->b_iocmd = BIO_WRITE;
1540 bp->b_rcred = crhold(thread0.td_ucred);
1541 bp->b_wcred = crhold(thread0.td_ucred);
1542 bp->b_bcount = PAGE_SIZE * n;
1543 bp->b_bufsize = PAGE_SIZE * n;
1545 for (j = 0; j < n; j++)
1546 bp->b_pages[j] = ma[i + j];
1550 * Must set dirty range for NFS to work.
1553 bp->b_dirtyend = bp->b_bcount;
1555 VM_CNT_INC(v_swapout);
1556 VM_CNT_ADD(v_swappgsout, bp->b_npages);
1559 * We unconditionally set rtvals[] to VM_PAGER_PEND so that we
1560 * can call the async completion routine at the end of a
1561 * synchronous I/O operation. Otherwise, our caller would
1562 * perform duplicate unbusy and wakeup operations on the page
1563 * and object, respectively.
1565 for (j = 0; j < n; j++)
1566 rtvals[i + j] = VM_PAGER_PEND;
1571 * NOTE: b_blkno is destroyed by the call to swapdev_strategy.
1574 bp->b_iodone = swp_pager_async_iodone;
1576 swp_pager_strategy(bp);
1583 * NOTE: b_blkno is destroyed by the call to swapdev_strategy.
1585 bp->b_iodone = bdone;
1586 swp_pager_strategy(bp);
1589 * Wait for the sync I/O to complete.
1591 bwait(bp, PVM, "swwrt");
1594 * Now that we are through with the bp, we can call the
1595 * normal async completion, which frees everything up.
1597 swp_pager_async_iodone(bp);
1599 swp_pager_freeswapspace(s_free, n_free);
1600 VM_OBJECT_WLOCK(object);
1604 * swp_pager_async_iodone:
1606 * Completion routine for asynchronous reads and writes from/to swap.
1607 * Also called manually by synchronous code to finish up a bp.
1609 * This routine may not sleep.
1612 swp_pager_async_iodone(struct buf *bp)
1615 vm_object_t object = NULL;
1618 * Report error - unless we ran out of memory, in which case
1619 * we've already logged it in swapgeom_strategy().
1621 if (bp->b_ioflags & BIO_ERROR && bp->b_error != ENOMEM) {
1623 "swap_pager: I/O error - %s failed; blkno %ld,"
1624 "size %ld, error %d\n",
1625 ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
1633 * remove the mapping for kernel virtual
1636 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1638 bp->b_data = bp->b_kvabase;
1641 object = bp->b_pages[0]->object;
1642 VM_OBJECT_WLOCK(object);
1646 * cleanup pages. If an error occurs writing to swap, we are in
1647 * very serious trouble. If it happens to be a disk error, though,
1648 * we may be able to recover by reassigning the swap later on. So
1649 * in this case we remove the m->swapblk assignment for the page
1650 * but do not free it in the rlist. The errornous block(s) are thus
1651 * never reallocated as swap. Redirty the page and continue.
1653 for (i = 0; i < bp->b_npages; ++i) {
1654 vm_page_t m = bp->b_pages[i];
1656 m->oflags &= ~VPO_SWAPINPROG;
1657 if (m->oflags & VPO_SWAPSLEEP) {
1658 m->oflags &= ~VPO_SWAPSLEEP;
1659 wakeup(&object->handle);
1662 /* We always have space after I/O, successful or not. */
1663 vm_page_aflag_set(m, PGA_SWAP_SPACE);
1665 if (bp->b_ioflags & BIO_ERROR) {
1667 * If an error occurs I'd love to throw the swapblk
1668 * away without freeing it back to swapspace, so it
1669 * can never be used again. But I can't from an
1672 if (bp->b_iocmd == BIO_READ) {
1674 * NOTE: for reads, m->dirty will probably
1675 * be overridden by the original caller of
1676 * getpages so don't play cute tricks here.
1681 * If a write error occurs, reactivate page
1682 * so it doesn't clog the inactive list,
1683 * then finish the I/O.
1685 MPASS(m->dirty == VM_PAGE_BITS_ALL);
1687 /* PQ_UNSWAPPABLE? */
1688 vm_page_activate(m);
1691 } else if (bp->b_iocmd == BIO_READ) {
1693 * NOTE: for reads, m->dirty will probably be
1694 * overridden by the original caller of getpages so
1695 * we cannot set them in order to free the underlying
1696 * swap in a low-swap situation. I don't think we'd
1697 * want to do that anyway, but it was an optimization
1698 * that existed in the old swapper for a time before
1699 * it got ripped out due to precisely this problem.
1701 KASSERT(!pmap_page_is_mapped(m),
1702 ("swp_pager_async_iodone: page %p is mapped", m));
1703 KASSERT(m->dirty == 0,
1704 ("swp_pager_async_iodone: page %p is dirty", m));
1707 if (i < bp->b_pgbefore ||
1708 i >= bp->b_npages - bp->b_pgafter)
1709 vm_page_readahead_finish(m);
1712 * For write success, clear the dirty
1713 * status, then finish the I/O ( which decrements the
1714 * busy count and possibly wakes waiter's up ).
1715 * A page is only written to swap after a period of
1716 * inactivity. Therefore, we do not expect it to be
1719 KASSERT(!pmap_page_is_write_mapped(m),
1720 ("swp_pager_async_iodone: page %p is not write"
1723 vm_page_deactivate_noreuse(m);
1729 * adjust pip. NOTE: the original parent may still have its own
1730 * pip refs on the object.
1732 if (object != NULL) {
1733 vm_object_pip_wakeupn(object, bp->b_npages);
1734 VM_OBJECT_WUNLOCK(object);
1738 * swapdev_strategy() manually sets b_vp and b_bufobj before calling
1739 * bstrategy(). Set them back to NULL now we're done with it, or we'll
1740 * trigger a KASSERT in relpbuf().
1744 bp->b_bufobj = NULL;
1747 * release the physical I/O buffer
1749 if (bp->b_flags & B_ASYNC) {
1750 mtx_lock(&swbuf_mtx);
1751 if (++nsw_wcount_async == 1)
1752 wakeup(&nsw_wcount_async);
1753 mtx_unlock(&swbuf_mtx);
1755 uma_zfree((bp->b_iocmd == BIO_READ) ? swrbuf_zone : swwbuf_zone, bp);
1759 swap_pager_nswapdev(void)
1766 swp_pager_force_dirty(vm_page_t m)
1770 swap_pager_unswapped(m);
1775 swap_pager_swapped_pages(vm_object_t object)
1782 VM_OBJECT_ASSERT_LOCKED(object);
1784 if (pctrie_is_empty(&object->un_pager.swp.swp_blks))
1787 for (res = 0, pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
1788 &object->un_pager.swp.swp_blks, pi)) != NULL;
1789 pi = sb->p + SWAP_META_PAGES) {
1790 for (i = 0; i < SWAP_META_PAGES; i++) {
1791 if (sb->d[i] != SWAPBLK_NONE)
1799 * swap_pager_swapoff_object:
1801 * Page in all of the pages that have been paged out for an object
1805 swap_pager_swapoff_object(struct swdevt *sp, vm_object_t object)
1811 int i, nv, rahead, rv;
1813 KASSERT((object->flags & OBJ_SWAP) != 0,
1814 ("%s: Object not swappable", __func__));
1816 for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
1817 &object->un_pager.swp.swp_blks, pi)) != NULL; ) {
1818 if ((object->flags & OBJ_DEAD) != 0) {
1820 * Make sure that pending writes finish before
1823 vm_object_pip_wait(object, "swpoff");
1824 swp_pager_meta_free_all(object);
1827 for (i = 0; i < SWAP_META_PAGES; i++) {
1829 * Count the number of contiguous valid blocks.
1831 for (nv = 0; nv < SWAP_META_PAGES - i; nv++) {
1832 blk = sb->d[i + nv];
1833 if (!swp_pager_isondev(blk, sp) ||
1834 blk == SWAPBLK_NONE)
1841 * Look for a page corresponding to the first
1842 * valid block and ensure that any pending paging
1843 * operations on it are complete. If the page is valid,
1844 * mark it dirty and free the swap block. Try to batch
1845 * this operation since it may cause sp to be freed,
1846 * meaning that we must restart the scan. Avoid busying
1847 * valid pages since we may block forever on kernel
1850 m = vm_page_lookup(object, sb->p + i);
1852 m = vm_page_alloc(object, sb->p + i,
1853 VM_ALLOC_NORMAL | VM_ALLOC_WAITFAIL);
1857 if ((m->oflags & VPO_SWAPINPROG) != 0) {
1858 m->oflags |= VPO_SWAPSLEEP;
1859 VM_OBJECT_SLEEP(object, &object->handle,
1863 if (vm_page_all_valid(m)) {
1865 swp_pager_force_dirty(m);
1866 } while (--nv > 0 &&
1867 (m = vm_page_next(m)) != NULL &&
1868 vm_page_all_valid(m) &&
1869 (m->oflags & VPO_SWAPINPROG) == 0);
1872 if (!vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL))
1876 vm_object_pip_add(object, 1);
1877 rahead = SWAP_META_PAGES;
1878 rv = swap_pager_getpages_locked(object, &m, 1, NULL,
1880 if (rv != VM_PAGER_OK)
1881 panic("%s: read from swap failed: %d",
1883 vm_object_pip_wakeupn(object, 1);
1884 VM_OBJECT_WLOCK(object);
1888 * The object lock was dropped so we must restart the
1889 * scan of this swap block. Pages paged in during this
1890 * iteration will be marked dirty in a future iteration.
1894 if (i == SWAP_META_PAGES)
1895 pi = sb->p + SWAP_META_PAGES;
1900 * swap_pager_swapoff:
1902 * Page in all of the pages that have been paged out to the
1903 * given device. The corresponding blocks in the bitmap must be
1904 * marked as allocated and the device must be flagged SW_CLOSING.
1905 * There may be no processes swapped out to the device.
1907 * This routine may block.
1910 swap_pager_swapoff(struct swdevt *sp)
1915 sx_assert(&swdev_syscall_lock, SA_XLOCKED);
1919 mtx_lock(&vm_object_list_mtx);
1920 TAILQ_FOREACH(object, &vm_object_list, object_list) {
1921 if ((object->flags & OBJ_SWAP) == 0)
1923 mtx_unlock(&vm_object_list_mtx);
1924 /* Depends on type-stability. */
1925 VM_OBJECT_WLOCK(object);
1928 * Dead objects are eventually terminated on their own.
1930 if ((object->flags & OBJ_DEAD) != 0)
1934 * Sync with fences placed after pctrie
1935 * initialization. We must not access pctrie below
1936 * unless we checked that our object is swap and not
1939 atomic_thread_fence_acq();
1940 if ((object->flags & OBJ_SWAP) == 0)
1943 swap_pager_swapoff_object(sp, object);
1945 VM_OBJECT_WUNLOCK(object);
1946 mtx_lock(&vm_object_list_mtx);
1948 mtx_unlock(&vm_object_list_mtx);
1952 * Objects may be locked or paging to the device being
1953 * removed, so we will miss their pages and need to
1954 * make another pass. We have marked this device as
1955 * SW_CLOSING, so the activity should finish soon.
1958 if (retries > 100) {
1959 panic("swapoff: failed to locate %d swap blocks",
1962 pause("swpoff", hz / 20);
1965 EVENTHANDLER_INVOKE(swapoff, sp);
1968 /************************************************************************
1970 ************************************************************************
1972 * These routines manipulate the swap metadata stored in the
1975 * Swap metadata is implemented with a global hash and not directly
1976 * linked into the object. Instead the object simply contains
1977 * appropriate tracking counters.
1981 * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free?
1984 swp_pager_swblk_empty(struct swblk *sb, int start, int limit)
1988 MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES);
1989 for (i = start; i < limit; i++) {
1990 if (sb->d[i] != SWAPBLK_NONE)
1997 * SWP_PAGER_FREE_EMPTY_SWBLK() - frees if a block is free
1999 * Nothing is done if the block is still in use.
2002 swp_pager_free_empty_swblk(vm_object_t object, struct swblk *sb)
2005 if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) {
2006 SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p);
2007 uma_zfree(swblk_zone, sb);
2012 * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object
2014 * The specified swapblk is added to the object's swap metadata. If
2015 * the swapblk is not valid, it is freed instead. Any previously
2016 * assigned swapblk is returned.
2019 swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk)
2021 static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted;
2022 struct swblk *sb, *sb1;
2023 vm_pindex_t modpi, rdpi;
2024 daddr_t prev_swapblk;
2027 VM_OBJECT_ASSERT_WLOCKED(object);
2029 rdpi = rounddown(pindex, SWAP_META_PAGES);
2030 sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi);
2032 if (swapblk == SWAPBLK_NONE)
2033 return (SWAPBLK_NONE);
2035 sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc ==
2036 pageproc ? M_USE_RESERVE : 0));
2039 for (i = 0; i < SWAP_META_PAGES; i++)
2040 sb->d[i] = SWAPBLK_NONE;
2041 if (atomic_cmpset_int(&swblk_zone_exhausted,
2043 printf("swblk zone ok\n");
2046 VM_OBJECT_WUNLOCK(object);
2047 if (uma_zone_exhausted(swblk_zone)) {
2048 if (atomic_cmpset_int(&swblk_zone_exhausted,
2050 printf("swap blk zone exhausted, "
2051 "increase kern.maxswzone\n");
2052 vm_pageout_oom(VM_OOM_SWAPZ);
2053 pause("swzonxb", 10);
2055 uma_zwait(swblk_zone);
2056 VM_OBJECT_WLOCK(object);
2057 sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
2061 * Somebody swapped out a nearby page,
2062 * allocating swblk at the rdpi index,
2063 * while we dropped the object lock.
2068 error = SWAP_PCTRIE_INSERT(
2069 &object->un_pager.swp.swp_blks, sb);
2071 if (atomic_cmpset_int(&swpctrie_zone_exhausted,
2073 printf("swpctrie zone ok\n");
2076 VM_OBJECT_WUNLOCK(object);
2077 if (uma_zone_exhausted(swpctrie_zone)) {
2078 if (atomic_cmpset_int(&swpctrie_zone_exhausted,
2080 printf("swap pctrie zone exhausted, "
2081 "increase kern.maxswzone\n");
2082 vm_pageout_oom(VM_OOM_SWAPZ);
2083 pause("swzonxp", 10);
2085 uma_zwait(swpctrie_zone);
2086 VM_OBJECT_WLOCK(object);
2087 sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
2090 uma_zfree(swblk_zone, sb);
2097 MPASS(sb->p == rdpi);
2099 modpi = pindex % SWAP_META_PAGES;
2100 /* Return prior contents of metadata. */
2101 prev_swapblk = sb->d[modpi];
2102 /* Enter block into metadata. */
2103 sb->d[modpi] = swapblk;
2106 * Free the swblk if we end up with the empty page run.
2108 if (swapblk == SWAPBLK_NONE)
2109 swp_pager_free_empty_swblk(object, sb);
2110 return (prev_swapblk);
2114 * SWP_PAGER_META_TRANSFER() - free a range of blocks in the srcobject's swap
2115 * metadata, or transfer it into dstobject.
2117 * This routine will free swap metadata structures as they are cleaned
2121 swp_pager_meta_transfer(vm_object_t srcobject, vm_object_t dstobject,
2122 vm_pindex_t pindex, vm_pindex_t count)
2125 daddr_t n_free, s_free;
2126 vm_pindex_t offset, last;
2127 int i, limit, start;
2129 VM_OBJECT_ASSERT_WLOCKED(srcobject);
2130 if (count == 0 || pctrie_is_empty(&srcobject->un_pager.swp.swp_blks))
2133 swp_pager_init_freerange(&s_free, &n_free);
2135 last = pindex + count;
2137 sb = SWAP_PCTRIE_LOOKUP_GE(&srcobject->un_pager.swp.swp_blks,
2138 rounddown(pindex, SWAP_META_PAGES));
2139 if (sb == NULL || sb->p >= last)
2141 start = pindex > sb->p ? pindex - sb->p : 0;
2142 limit = last - sb->p < SWAP_META_PAGES ? last - sb->p :
2144 for (i = start; i < limit; i++) {
2145 if (sb->d[i] == SWAPBLK_NONE)
2147 if (dstobject == NULL ||
2148 !swp_pager_xfer_source(srcobject, dstobject,
2149 sb->p + i - offset, sb->d[i])) {
2150 swp_pager_update_freerange(&s_free, &n_free,
2153 sb->d[i] = SWAPBLK_NONE;
2155 pindex = sb->p + SWAP_META_PAGES;
2156 if (swp_pager_swblk_empty(sb, 0, start) &&
2157 swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) {
2158 SWAP_PCTRIE_REMOVE(&srcobject->un_pager.swp.swp_blks,
2160 uma_zfree(swblk_zone, sb);
2163 swp_pager_freeswapspace(s_free, n_free);
2167 * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
2169 * The requested range of blocks is freed, with any associated swap
2170 * returned to the swap bitmap.
2172 * This routine will free swap metadata structures as they are cleaned
2173 * out. This routine does *NOT* operate on swap metadata associated
2174 * with resident pages.
2177 swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count)
2179 swp_pager_meta_transfer(object, NULL, pindex, count);
2183 * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
2185 * This routine locates and destroys all swap metadata associated with
2189 swp_pager_meta_free_all(vm_object_t object)
2192 daddr_t n_free, s_free;
2196 VM_OBJECT_ASSERT_WLOCKED(object);
2198 if (pctrie_is_empty(&object->un_pager.swp.swp_blks))
2201 swp_pager_init_freerange(&s_free, &n_free);
2202 for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
2203 &object->un_pager.swp.swp_blks, pindex)) != NULL;) {
2204 pindex = sb->p + SWAP_META_PAGES;
2205 for (i = 0; i < SWAP_META_PAGES; i++) {
2206 if (sb->d[i] == SWAPBLK_NONE)
2208 swp_pager_update_freerange(&s_free, &n_free, sb->d[i]);
2210 SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p);
2211 uma_zfree(swblk_zone, sb);
2213 swp_pager_freeswapspace(s_free, n_free);
2217 * SWP_PAGER_METACTL() - misc control of swap meta data.
2219 * This routine is capable of looking up, or removing swapblk
2220 * assignments in the swap meta data. It returns the swapblk being
2221 * looked-up, popped, or SWAPBLK_NONE if the block was invalid.
2223 * When acting on a busy resident page and paging is in progress, we
2224 * have to wait until paging is complete but otherwise can act on the
2228 swp_pager_meta_lookup(vm_object_t object, vm_pindex_t pindex)
2232 VM_OBJECT_ASSERT_LOCKED(object);
2235 * The meta data only exists if the object is OBJT_SWAP
2236 * and even then might not be allocated yet.
2238 KASSERT((object->flags & OBJ_SWAP) != 0,
2239 ("Lookup object not swappable"));
2241 sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
2242 rounddown(pindex, SWAP_META_PAGES));
2244 return (SWAPBLK_NONE);
2245 return (sb->d[pindex % SWAP_META_PAGES]);
2249 * Returns the least page index which is greater than or equal to the
2250 * parameter pindex and for which there is a swap block allocated.
2251 * Returns object's size if the object's type is not swap or if there
2252 * are no allocated swap blocks for the object after the requested
2256 swap_pager_find_least(vm_object_t object, vm_pindex_t pindex)
2261 VM_OBJECT_ASSERT_LOCKED(object);
2262 MPASS((object->flags & OBJ_SWAP) != 0);
2264 if (pctrie_is_empty(&object->un_pager.swp.swp_blks))
2265 return (object->size);
2266 sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2267 rounddown(pindex, SWAP_META_PAGES));
2269 return (object->size);
2270 if (sb->p < pindex) {
2271 for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) {
2272 if (sb->d[i] != SWAPBLK_NONE)
2275 sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2276 roundup(pindex, SWAP_META_PAGES));
2278 return (object->size);
2280 for (i = 0; i < SWAP_META_PAGES; i++) {
2281 if (sb->d[i] != SWAPBLK_NONE)
2286 * We get here if a swblk is present in the trie but it
2287 * doesn't map any blocks.
2290 return (object->size);
2294 * System call swapon(name) enables swapping on device name,
2295 * which must be in the swdevsw. Return EBUSY
2296 * if already swapping on this device.
2298 #ifndef _SYS_SYSPROTO_H_
2299 struct swapon_args {
2305 sys_swapon(struct thread *td, struct swapon_args *uap)
2309 struct nameidata nd;
2312 error = priv_check(td, PRIV_SWAPON);
2316 sx_xlock(&swdev_syscall_lock);
2319 * Swap metadata may not fit in the KVM if we have physical
2322 if (swblk_zone == NULL) {
2327 NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
2328 UIO_USERSPACE, uap->name);
2336 if (vn_isdisk_error(vp, &error)) {
2337 error = swapongeom(vp);
2338 } else if (vp->v_type == VREG &&
2339 (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
2340 (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) {
2342 * Allow direct swapping to NFS regular files in the same
2343 * way that nfs_mountroot() sets up diskless swapping.
2345 error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
2353 sx_xunlock(&swdev_syscall_lock);
2358 * Check that the total amount of swap currently configured does not
2359 * exceed half the theoretical maximum. If it does, print a warning
2363 swapon_check_swzone(void)
2366 /* recommend using no more than half that amount */
2367 if (swap_total > swap_maxpages / 2) {
2368 printf("warning: total configured swap (%lu pages) "
2369 "exceeds maximum recommended amount (%lu pages).\n",
2370 swap_total, swap_maxpages / 2);
2371 printf("warning: increase kern.maxswzone "
2372 "or reduce amount of swap.\n");
2377 swaponsomething(struct vnode *vp, void *id, u_long nblks,
2378 sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags)
2380 struct swdevt *sp, *tsp;
2384 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
2385 * First chop nblks off to page-align it, then convert.
2387 * sw->sw_nblks is in page-sized chunks now too.
2389 nblks &= ~(ctodb(1) - 1);
2390 nblks = dbtoc(nblks);
2392 sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO);
2393 sp->sw_blist = blist_create(nblks, M_WAITOK);
2397 sp->sw_nblks = nblks;
2399 sp->sw_strategy = strategy;
2400 sp->sw_close = close;
2401 sp->sw_flags = flags;
2404 * Do not free the first blocks in order to avoid overwriting
2405 * any bsd label at the front of the partition
2407 blist_free(sp->sw_blist, howmany(BBSIZE, PAGE_SIZE),
2408 nblks - howmany(BBSIZE, PAGE_SIZE));
2411 mtx_lock(&sw_dev_mtx);
2412 TAILQ_FOREACH(tsp, &swtailq, sw_list) {
2413 if (tsp->sw_end >= dvbase) {
2415 * We put one uncovered page between the devices
2416 * in order to definitively prevent any cross-device
2419 dvbase = tsp->sw_end + 1;
2422 sp->sw_first = dvbase;
2423 sp->sw_end = dvbase + nblks;
2424 TAILQ_INSERT_TAIL(&swtailq, sp, sw_list);
2426 swap_pager_avail += nblks - howmany(BBSIZE, PAGE_SIZE);
2427 swap_total += nblks;
2428 swapon_check_swzone();
2430 mtx_unlock(&sw_dev_mtx);
2431 EVENTHANDLER_INVOKE(swapon, sp);
2435 * SYSCALL: swapoff(devname)
2437 * Disable swapping on the given device.
2439 * XXX: Badly designed system call: it should use a device index
2440 * rather than filename as specification. We keep sw_vp around
2441 * only to make this work.
2444 kern_swapoff(struct thread *td, const char *name, enum uio_seg name_seg,
2448 struct nameidata nd;
2452 error = priv_check(td, PRIV_SWAPOFF);
2455 if ((flags & ~(SWAPOFF_FORCE)) != 0)
2458 sx_xlock(&swdev_syscall_lock);
2460 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, name_seg, name);
2467 mtx_lock(&sw_dev_mtx);
2468 TAILQ_FOREACH(sp, &swtailq, sw_list) {
2469 if (sp->sw_vp == vp)
2472 mtx_unlock(&sw_dev_mtx);
2477 error = swapoff_one(sp, td->td_ucred, flags);
2479 sx_xunlock(&swdev_syscall_lock);
2484 #ifdef COMPAT_FREEBSD13
2486 freebsd13_swapoff(struct thread *td, struct freebsd13_swapoff_args *uap)
2488 return (kern_swapoff(td, uap->name, UIO_USERSPACE, 0));
2493 sys_swapoff(struct thread *td, struct swapoff_args *uap)
2495 return (kern_swapoff(td, uap->name, UIO_USERSPACE, uap->flags));
2499 swapoff_one(struct swdevt *sp, struct ucred *cred, u_int flags)
2506 sx_assert(&swdev_syscall_lock, SA_XLOCKED);
2508 (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY);
2509 error = mac_system_check_swapoff(cred, sp->sw_vp);
2510 (void) VOP_UNLOCK(sp->sw_vp);
2514 nblks = sp->sw_nblks;
2517 * We can turn off this swap device safely only if the
2518 * available virtual memory in the system will fit the amount
2519 * of data we will have to page back in, plus an epsilon so
2520 * the system doesn't become critically low on swap space.
2521 * The vm_free_count() part does not account e.g. for clean
2522 * pages that can be immediately reclaimed without paging, so
2523 * this is a very rough estimation.
2525 * On the other hand, not turning swap off on swapoff_all()
2526 * means that we can lose swap data when filesystems go away,
2527 * which is arguably worse.
2529 if ((flags & SWAPOFF_FORCE) == 0 &&
2530 vm_free_count() + swap_pager_avail < nblks + nswap_lowat)
2534 * Prevent further allocations on this device.
2536 mtx_lock(&sw_dev_mtx);
2537 sp->sw_flags |= SW_CLOSING;
2538 swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks);
2539 swap_total -= nblks;
2540 mtx_unlock(&sw_dev_mtx);
2543 * Page in the contents of the device and close it.
2545 swap_pager_swapoff(sp);
2547 sp->sw_close(curthread, sp);
2548 mtx_lock(&sw_dev_mtx);
2550 TAILQ_REMOVE(&swtailq, sp, sw_list);
2552 if (nswapdev == 0) {
2553 swap_pager_full = 2;
2554 swap_pager_almost_full = 1;
2558 mtx_unlock(&sw_dev_mtx);
2559 blist_destroy(sp->sw_blist);
2560 free(sp, M_VMPGDATA);
2567 struct swdevt *sp, *spt;
2568 const char *devname;
2571 sx_xlock(&swdev_syscall_lock);
2573 mtx_lock(&sw_dev_mtx);
2574 TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) {
2575 mtx_unlock(&sw_dev_mtx);
2576 if (vn_isdisk(sp->sw_vp))
2577 devname = devtoname(sp->sw_vp->v_rdev);
2580 error = swapoff_one(sp, thread0.td_ucred, SWAPOFF_FORCE);
2582 printf("Cannot remove swap device %s (error=%d), "
2583 "skipping.\n", devname, error);
2584 } else if (bootverbose) {
2585 printf("Swap device %s removed.\n", devname);
2587 mtx_lock(&sw_dev_mtx);
2589 mtx_unlock(&sw_dev_mtx);
2591 sx_xunlock(&swdev_syscall_lock);
2595 swap_pager_status(int *total, int *used)
2598 *total = swap_total;
2599 *used = swap_total - swap_pager_avail -
2600 nswapdev * howmany(BBSIZE, PAGE_SIZE);
2604 swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len)
2607 const char *tmp_devname;
2612 mtx_lock(&sw_dev_mtx);
2613 TAILQ_FOREACH(sp, &swtailq, sw_list) {
2618 xs->xsw_version = XSWDEV_VERSION;
2619 xs->xsw_dev = sp->sw_dev;
2620 xs->xsw_flags = sp->sw_flags;
2621 xs->xsw_nblks = sp->sw_nblks;
2622 xs->xsw_used = sp->sw_used;
2623 if (devname != NULL) {
2624 if (vn_isdisk(sp->sw_vp))
2625 tmp_devname = devtoname(sp->sw_vp->v_rdev);
2627 tmp_devname = "[file]";
2628 strncpy(devname, tmp_devname, len);
2633 mtx_unlock(&sw_dev_mtx);
2637 #if defined(COMPAT_FREEBSD11)
2638 #define XSWDEV_VERSION_11 1
2648 #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2651 u_int xsw_dev1, xsw_dev2;
2659 sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
2662 #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2663 struct xswdev32 xs32;
2665 #if defined(COMPAT_FREEBSD11)
2666 struct xswdev11 xs11;
2670 if (arg2 != 1) /* name length */
2673 memset(&xs, 0, sizeof(xs));
2674 error = swap_dev_info(*(int *)arg1, &xs, NULL, 0);
2677 #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2678 if (req->oldlen == sizeof(xs32)) {
2679 memset(&xs32, 0, sizeof(xs32));
2680 xs32.xsw_version = XSWDEV_VERSION;
2681 xs32.xsw_dev1 = xs.xsw_dev;
2682 xs32.xsw_dev2 = xs.xsw_dev >> 32;
2683 xs32.xsw_flags = xs.xsw_flags;
2684 xs32.xsw_nblks = xs.xsw_nblks;
2685 xs32.xsw_used = xs.xsw_used;
2686 error = SYSCTL_OUT(req, &xs32, sizeof(xs32));
2690 #if defined(COMPAT_FREEBSD11)
2691 if (req->oldlen == sizeof(xs11)) {
2692 memset(&xs11, 0, sizeof(xs11));
2693 xs11.xsw_version = XSWDEV_VERSION_11;
2694 xs11.xsw_dev = xs.xsw_dev; /* truncation */
2695 xs11.xsw_flags = xs.xsw_flags;
2696 xs11.xsw_nblks = xs.xsw_nblks;
2697 xs11.xsw_used = xs.xsw_used;
2698 error = SYSCTL_OUT(req, &xs11, sizeof(xs11));
2702 error = SYSCTL_OUT(req, &xs, sizeof(xs));
2706 SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
2707 "Number of swap devices");
2708 SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE,
2709 sysctl_vm_swap_info,
2710 "Swap statistics by device");
2713 * Count the approximate swap usage in pages for a vmspace. The
2714 * shadowed or not yet copied on write swap blocks are not accounted.
2715 * The map must be locked.
2718 vmspace_swap_count(struct vmspace *vmspace)
2728 map = &vmspace->vm_map;
2731 VM_MAP_ENTRY_FOREACH(cur, map) {
2732 if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
2734 object = cur->object.vm_object;
2735 if (object == NULL || (object->flags & OBJ_SWAP) == 0)
2737 VM_OBJECT_RLOCK(object);
2738 if ((object->flags & OBJ_SWAP) == 0)
2740 pi = OFF_TO_IDX(cur->offset);
2741 e = pi + OFF_TO_IDX(cur->end - cur->start);
2742 for (;; pi = sb->p + SWAP_META_PAGES) {
2743 sb = SWAP_PCTRIE_LOOKUP_GE(
2744 &object->un_pager.swp.swp_blks, pi);
2745 if (sb == NULL || sb->p >= e)
2747 for (i = 0; i < SWAP_META_PAGES; i++) {
2748 if (sb->p + i < e &&
2749 sb->d[i] != SWAPBLK_NONE)
2754 VM_OBJECT_RUNLOCK(object);
2762 * Swapping onto disk devices.
2766 static g_orphan_t swapgeom_orphan;
2768 static struct g_class g_swap_class = {
2770 .version = G_VERSION,
2771 .orphan = swapgeom_orphan,
2774 DECLARE_GEOM_CLASS(g_swap_class, g_class);
2777 swapgeom_close_ev(void *arg, int flags)
2779 struct g_consumer *cp;
2782 g_access(cp, -1, -1, 0);
2784 g_destroy_consumer(cp);
2788 * Add a reference to the g_consumer for an inflight transaction.
2791 swapgeom_acquire(struct g_consumer *cp)
2794 mtx_assert(&sw_dev_mtx, MA_OWNED);
2799 * Remove a reference from the g_consumer. Post a close event if all
2800 * references go away, since the function might be called from the
2804 swapgeom_release(struct g_consumer *cp, struct swdevt *sp)
2807 mtx_assert(&sw_dev_mtx, MA_OWNED);
2809 if (cp->index == 0) {
2810 if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0)
2816 swapgeom_done(struct bio *bp2)
2820 struct g_consumer *cp;
2822 bp = bp2->bio_caller2;
2824 bp->b_ioflags = bp2->bio_flags;
2826 bp->b_ioflags |= BIO_ERROR;
2827 bp->b_resid = bp->b_bcount - bp2->bio_completed;
2828 bp->b_error = bp2->bio_error;
2829 bp->b_caller1 = NULL;
2831 sp = bp2->bio_caller1;
2832 mtx_lock(&sw_dev_mtx);
2833 swapgeom_release(cp, sp);
2834 mtx_unlock(&sw_dev_mtx);
2839 swapgeom_strategy(struct buf *bp, struct swdevt *sp)
2842 struct g_consumer *cp;
2844 mtx_lock(&sw_dev_mtx);
2847 mtx_unlock(&sw_dev_mtx);
2848 bp->b_error = ENXIO;
2849 bp->b_ioflags |= BIO_ERROR;
2853 swapgeom_acquire(cp);
2854 mtx_unlock(&sw_dev_mtx);
2855 if (bp->b_iocmd == BIO_WRITE)
2858 bio = g_alloc_bio();
2860 mtx_lock(&sw_dev_mtx);
2861 swapgeom_release(cp, sp);
2862 mtx_unlock(&sw_dev_mtx);
2863 bp->b_error = ENOMEM;
2864 bp->b_ioflags |= BIO_ERROR;
2865 printf("swap_pager: cannot allocate bio\n");
2870 bp->b_caller1 = bio;
2871 bio->bio_caller1 = sp;
2872 bio->bio_caller2 = bp;
2873 bio->bio_cmd = bp->b_iocmd;
2874 bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE;
2875 bio->bio_length = bp->b_bcount;
2876 bio->bio_done = swapgeom_done;
2877 bio->bio_flags |= BIO_SWAP;
2878 if (!buf_mapped(bp)) {
2879 bio->bio_ma = bp->b_pages;
2880 bio->bio_data = unmapped_buf;
2881 bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK;
2882 bio->bio_ma_n = bp->b_npages;
2883 bio->bio_flags |= BIO_UNMAPPED;
2885 bio->bio_data = bp->b_data;
2888 g_io_request(bio, cp);
2893 swapgeom_orphan(struct g_consumer *cp)
2898 mtx_lock(&sw_dev_mtx);
2899 TAILQ_FOREACH(sp, &swtailq, sw_list) {
2900 if (sp->sw_id == cp) {
2901 sp->sw_flags |= SW_CLOSING;
2906 * Drop reference we were created with. Do directly since we're in a
2907 * special context where we don't have to queue the call to
2908 * swapgeom_close_ev().
2911 destroy = ((sp != NULL) && (cp->index == 0));
2914 mtx_unlock(&sw_dev_mtx);
2916 swapgeom_close_ev(cp, 0);
2920 swapgeom_close(struct thread *td, struct swdevt *sw)
2922 struct g_consumer *cp;
2924 mtx_lock(&sw_dev_mtx);
2927 mtx_unlock(&sw_dev_mtx);
2930 * swapgeom_close() may be called from the biodone context,
2931 * where we cannot perform topology changes. Delegate the
2932 * work to the events thread.
2935 g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL);
2939 swapongeom_locked(struct cdev *dev, struct vnode *vp)
2941 struct g_provider *pp;
2942 struct g_consumer *cp;
2943 static struct g_geom *gp;
2948 pp = g_dev_getprovider(dev);
2951 mtx_lock(&sw_dev_mtx);
2952 TAILQ_FOREACH(sp, &swtailq, sw_list) {
2954 if (cp != NULL && cp->provider == pp) {
2955 mtx_unlock(&sw_dev_mtx);
2959 mtx_unlock(&sw_dev_mtx);
2961 gp = g_new_geomf(&g_swap_class, "swap");
2962 cp = g_new_consumer(gp);
2963 cp->index = 1; /* Number of active I/Os, plus one for being active. */
2964 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2967 * XXX: Every time you think you can improve the margin for
2968 * footshooting, somebody depends on the ability to do so:
2969 * savecore(8) wants to write to our swapdev so we cannot
2970 * set an exclusive count :-(
2972 error = g_access(cp, 1, 1, 0);
2975 g_destroy_consumer(cp);
2978 nblks = pp->mediasize / DEV_BSIZE;
2979 swaponsomething(vp, cp, nblks, swapgeom_strategy,
2980 swapgeom_close, dev2udev(dev),
2981 (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0);
2986 swapongeom(struct vnode *vp)
2990 ASSERT_VOP_ELOCKED(vp, "swapongeom");
2991 if (vp->v_type != VCHR || VN_IS_DOOMED(vp)) {
2995 error = swapongeom_locked(vp->v_rdev, vp);
2996 g_topology_unlock();
3004 * This is used mainly for network filesystem (read: probably only tested
3005 * with NFS) swapfiles.
3010 swapdev_strategy(struct buf *bp, struct swdevt *sp)
3014 bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first);
3018 if (bp->b_iocmd == BIO_WRITE) {
3019 vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY);
3021 bufobj_wdrop(bp->b_bufobj);
3022 bufobj_wref(&vp2->v_bufobj);
3024 vn_lock(vp2, LK_SHARED | LK_RETRY);
3026 if (bp->b_bufobj != &vp2->v_bufobj)
3027 bp->b_bufobj = &vp2->v_bufobj;
3029 bp->b_iooffset = dbtob(bp->b_blkno);
3035 swapdev_close(struct thread *td, struct swdevt *sp)
3040 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3041 VOP_CLOSE(vp, FREAD | FWRITE, td->td_ucred, td);
3046 swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
3051 ASSERT_VOP_ELOCKED(vp, "swaponvp");
3054 mtx_lock(&sw_dev_mtx);
3055 TAILQ_FOREACH(sp, &swtailq, sw_list) {
3056 if (sp->sw_id == vp) {
3057 mtx_unlock(&sw_dev_mtx);
3061 mtx_unlock(&sw_dev_mtx);
3064 error = mac_system_check_swapon(td->td_ucred, vp);
3067 error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL);
3071 swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close,
3077 sysctl_swap_async_max(SYSCTL_HANDLER_ARGS)
3081 new = nsw_wcount_async_max;
3082 error = sysctl_handle_int(oidp, &new, 0, req);
3083 if (error != 0 || req->newptr == NULL)
3086 if (new > nswbuf / 2 || new < 1)
3089 mtx_lock(&swbuf_mtx);
3090 while (nsw_wcount_async_max != new) {
3092 * Adjust difference. If the current async count is too low,
3093 * we will need to sqeeze our update slowly in. Sleep with a
3094 * higher priority than getpbuf() to finish faster.
3096 n = new - nsw_wcount_async_max;
3097 if (nsw_wcount_async + n >= 0) {
3098 nsw_wcount_async += n;
3099 nsw_wcount_async_max += n;
3100 wakeup(&nsw_wcount_async);
3102 nsw_wcount_async_max -= nsw_wcount_async;
3103 nsw_wcount_async = 0;
3104 msleep(&nsw_wcount_async, &swbuf_mtx, PSWP,
3108 mtx_unlock(&swbuf_mtx);
3114 swap_pager_update_writecount(vm_object_t object, vm_offset_t start,
3118 VM_OBJECT_WLOCK(object);
3119 KASSERT((object->flags & OBJ_ANON) == 0,
3120 ("Splittable object with writecount"));
3121 object->un_pager.swp.writemappings += (vm_ooffset_t)end - start;
3122 VM_OBJECT_WUNLOCK(object);
3126 swap_pager_release_writecount(vm_object_t object, vm_offset_t start,
3130 VM_OBJECT_WLOCK(object);
3131 KASSERT((object->flags & OBJ_ANON) == 0,
3132 ("Splittable object with writecount"));
3133 object->un_pager.swp.writemappings -= (vm_ooffset_t)end - start;
3134 VM_OBJECT_WUNLOCK(object);