1df8bae1dSRodney W. Grimes /* 21c7c3c6aSMatthew Dillon * Copyright (c) 1998 Matthew Dillon, 326f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 4df8bae1dSRodney W. Grimes * Copyright (c) 1990 University of Utah. 5e9c0cc15SPoul-Henning Kamp * Copyright (c) 1982, 1986, 1989, 1993 6df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 9df8bae1dSRodney W. Grimes * the Systems Programming Group of the University of Utah Computer 10df8bae1dSRodney W. Grimes * Science Department. 11df8bae1dSRodney W. Grimes * 12df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 13df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 14df8bae1dSRodney W. Grimes * are met: 15df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 17df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 19df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 20df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 215929bcfaSPhilippe Charnier * must display the following acknowledgement: 22df8bae1dSRodney W. Grimes * This product includes software developed by the University of 23df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 24df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 25df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 26df8bae1dSRodney W. Grimes * without specific prior written permission. 27df8bae1dSRodney W. Grimes * 28df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38df8bae1dSRodney W. Grimes * SUCH DAMAGE. 39df8bae1dSRodney W. Grimes * 401c7c3c6aSMatthew Dillon * New Swap System 411c7c3c6aSMatthew Dillon * Matthew Dillon 421c7c3c6aSMatthew Dillon * 431c7c3c6aSMatthew Dillon * Radix Bitmap 'blists'. 441c7c3c6aSMatthew Dillon * 451c7c3c6aSMatthew Dillon * - The new swapper uses the new radix bitmap code. This should scale 461c7c3c6aSMatthew Dillon * to arbitrarily small or arbitrarily large swap spaces and an almost 471c7c3c6aSMatthew Dillon * arbitrary degree of fragmentation. 481c7c3c6aSMatthew Dillon * 491c7c3c6aSMatthew Dillon * Features: 501c7c3c6aSMatthew Dillon * 511c7c3c6aSMatthew Dillon * - on the fly reallocation of swap during putpages. The new system 521c7c3c6aSMatthew Dillon * does not try to keep previously allocated swap blocks for dirty 531c7c3c6aSMatthew Dillon * pages. 541c7c3c6aSMatthew Dillon * 551c7c3c6aSMatthew Dillon * - on the fly deallocation of swap 561c7c3c6aSMatthew Dillon * 571c7c3c6aSMatthew Dillon * - No more garbage collection required. Unnecessarily allocated swap 581c7c3c6aSMatthew Dillon * blocks only exist for dirty vm_page_t's now and these are already 591c7c3c6aSMatthew Dillon * cycled (in a high-load system) by the pager. We also do on-the-fly 601c7c3c6aSMatthew Dillon * removal of invalidated swap blocks when a page is destroyed 611c7c3c6aSMatthew Dillon * or renamed. 621c7c3c6aSMatthew Dillon * 63df8bae1dSRodney W. Grimes * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$ 64df8bae1dSRodney W. Grimes * 65df8bae1dSRodney W. Grimes * @(#)swap_pager.c 8.9 (Berkeley) 3/21/94 66e9c0cc15SPoul-Henning Kamp * @(#)vm_swap.c 8.5 (Berkeley) 2/17/94 67df8bae1dSRodney W. Grimes */ 68df8bae1dSRodney W. Grimes 69874651b1SDavid E. O'Brien #include <sys/cdefs.h> 70874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 71874651b1SDavid E. O'Brien 72e9c0cc15SPoul-Henning Kamp #include "opt_mac.h" 73e9c0cc15SPoul-Henning Kamp #include "opt_swap.h" 74e9c0cc15SPoul-Henning Kamp #include "opt_vm.h" 75e9c0cc15SPoul-Henning Kamp 76df8bae1dSRodney W. Grimes #include <sys/param.h> 77df8bae1dSRodney W. Grimes #include <sys/systm.h> 78af647ddeSBruce Evans #include <sys/conf.h> 7964abb5a5SDavid Greenman #include <sys/kernel.h> 80df8bae1dSRodney W. Grimes #include <sys/proc.h> 819626b608SPoul-Henning Kamp #include <sys/bio.h> 82df8bae1dSRodney W. Grimes #include <sys/buf.h> 83e9c0cc15SPoul-Henning Kamp #include <sys/disk.h> 84e9c0cc15SPoul-Henning Kamp #include <sys/fcntl.h> 85e9c0cc15SPoul-Henning Kamp #include <sys/mount.h> 86e9c0cc15SPoul-Henning Kamp #include <sys/namei.h> 87df8bae1dSRodney W. Grimes #include <sys/vnode.h> 88e9c0cc15SPoul-Henning Kamp #include <sys/mac.h> 89df8bae1dSRodney W. Grimes #include <sys/malloc.h> 90327f4e83SMatthew Dillon #include <sys/sysctl.h> 91e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h> 921c7c3c6aSMatthew Dillon #include <sys/blist.h> 931c7c3c6aSMatthew Dillon #include <sys/lock.h> 940cddd8f0SMatthew Dillon #include <sys/sx.h> 95936524aaSMatthew Dillon #include <sys/vmmeter.h> 96df8bae1dSRodney W. Grimes 97df8bae1dSRodney W. Grimes #include <vm/vm.h> 9821cd6e62SSeigo Tanimura #include <vm/pmap.h> 9921cd6e62SSeigo Tanimura #include <vm/vm_map.h> 10021cd6e62SSeigo Tanimura #include <vm/vm_kern.h> 101efeaf95aSDavid Greenman #include <vm/vm_object.h> 102df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 103efeaf95aSDavid Greenman #include <vm/vm_pager.h> 104df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 105e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h> 106df8bae1dSRodney W. Grimes #include <vm/swap_pager.h> 107efeaf95aSDavid Greenman #include <vm/vm_extern.h> 108670d17b5SJeff Roberson #include <vm/uma.h> 109df8bae1dSRodney W. Grimes 110e9c0cc15SPoul-Henning Kamp #ifndef NSWAPDEV 111e9c0cc15SPoul-Henning Kamp #define NSWAPDEV 4 112e9c0cc15SPoul-Henning Kamp #endif 113e9c0cc15SPoul-Henning Kamp 114ec38b344SPoul-Henning Kamp /* 115ec38b344SPoul-Henning Kamp * SWB_NPAGES must be a power of 2. It may be set to 1, 2, 4, 8, or 16 116ec38b344SPoul-Henning Kamp * pages per allocation. We recommend you stick with the default of 8. 117ec38b344SPoul-Henning Kamp * The 16-page limit is due to the radix code (kern/subr_blist.c). 118ec38b344SPoul-Henning Kamp */ 119ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER 120ec38b344SPoul-Henning Kamp #define MAX_PAGEOUT_CLUSTER 16 121ec38b344SPoul-Henning Kamp #endif 122ec38b344SPoul-Henning Kamp 123ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES) 124ec38b344SPoul-Henning Kamp #define SWB_NPAGES MAX_PAGEOUT_CLUSTER 125ec38b344SPoul-Henning Kamp #endif 126ec38b344SPoul-Henning Kamp 127ec38b344SPoul-Henning Kamp /* 128ec38b344SPoul-Henning Kamp * Piecemeal swap metadata structure. Swap is stored in a radix tree. 129ec38b344SPoul-Henning Kamp * 130ec38b344SPoul-Henning Kamp * If SWB_NPAGES is 8 and sizeof(char *) == sizeof(daddr_t), our radix 131ec38b344SPoul-Henning Kamp * is basically 8. Assuming PAGE_SIZE == 4096, one tree level represents 132ec38b344SPoul-Henning Kamp * 32K worth of data, two levels represent 256K, three levels represent 133ec38b344SPoul-Henning Kamp * 2 MBytes. This is acceptable. 134ec38b344SPoul-Henning Kamp * 135ec38b344SPoul-Henning Kamp * Overall memory utilization is about the same as the old swap structure. 136ec38b344SPoul-Henning Kamp */ 137ec38b344SPoul-Henning Kamp #define SWCORRECT(n) (sizeof(void *) * (n) / sizeof(daddr_t)) 138ec38b344SPoul-Henning Kamp #define SWAP_META_PAGES (SWB_NPAGES * 2) 139ec38b344SPoul-Henning Kamp #define SWAP_META_MASK (SWAP_META_PAGES - 1) 140ec38b344SPoul-Henning Kamp 141e9c0cc15SPoul-Henning Kamp typedef int32_t swblk_t; /* swap offset */ 142e9c0cc15SPoul-Henning Kamp 143e9c0cc15SPoul-Henning Kamp struct swblock { 144e9c0cc15SPoul-Henning Kamp struct swblock *swb_hnext; 145e9c0cc15SPoul-Henning Kamp vm_object_t swb_object; 146e9c0cc15SPoul-Henning Kamp vm_pindex_t swb_index; 147e9c0cc15SPoul-Henning Kamp int swb_count; 148e9c0cc15SPoul-Henning Kamp daddr_t swb_pages[SWAP_META_PAGES]; 149e9c0cc15SPoul-Henning Kamp }; 150e9c0cc15SPoul-Henning Kamp 151e9c0cc15SPoul-Henning Kamp static struct swdevt should_be_malloced[NSWAPDEV]; 152e9c0cc15SPoul-Henning Kamp static struct swdevt *swdevt = should_be_malloced; 153e9c0cc15SPoul-Henning Kamp static int nswap; /* first block after the interleaved devs */ 154e9c0cc15SPoul-Henning Kamp static int nswdev = NSWAPDEV; 155e9c0cc15SPoul-Henning Kamp int vm_swap_size; 156e9c0cc15SPoul-Henning Kamp static int swdev_syscall_active = 0; /* serialize swap(on|off) */ 157e9c0cc15SPoul-Henning Kamp 158e9c0cc15SPoul-Henning Kamp static int swapdev_strategy(struct vop_strategy_args *ap); 159e9c0cc15SPoul-Henning Kamp static struct vnode *swapdev_vp; 160e9c0cc15SPoul-Henning Kamp 1611c7c3c6aSMatthew Dillon #define SWM_FREE 0x02 /* free, period */ 1621c7c3c6aSMatthew Dillon #define SWM_POP 0x04 /* pop out */ 16326f9a767SRodney W. Grimes 16420d3034fSMatthew Dillon int swap_pager_full; /* swap space exhaustion (task killing) */ 16520d3034fSMatthew Dillon static int swap_pager_almost_full; /* swap space exhaustion (w/ hysteresis)*/ 1661c7c3c6aSMatthew Dillon static int nsw_rcount; /* free read buffers */ 167327f4e83SMatthew Dillon static int nsw_wcount_sync; /* limit write buffers / synchronous */ 168327f4e83SMatthew Dillon static int nsw_wcount_async; /* limit write buffers / asynchronous */ 169327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum */ 170327f4e83SMatthew Dillon static int nsw_cluster_max; /* maximum VOP I/O allowed */ 1711c7c3c6aSMatthew Dillon 172567104a1SPoul-Henning Kamp static struct blist *swapblist; 1731c7c3c6aSMatthew Dillon static struct swblock **swhash; 1741c7c3c6aSMatthew Dillon static int swhash_mask; 175327f4e83SMatthew Dillon static int swap_async_max = 4; /* maximum in-progress async I/O's */ 1760cddd8f0SMatthew Dillon static struct sx sw_alloc_sx; 177327f4e83SMatthew Dillon 17824e7ab7cSPoul-Henning Kamp 179327f4e83SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, swap_async_max, 180327f4e83SMatthew Dillon CTLFLAG_RW, &swap_async_max, 0, "Maximum running async swap ops"); 1811c7c3c6aSMatthew Dillon 182edfa785aSRobert Watson #define BLK2DEVIDX(blk) (nswdev > 1 ? blk / dmmax % nswdev : 0) 183edfa785aSRobert Watson 1841c7c3c6aSMatthew Dillon /* 1851c7c3c6aSMatthew Dillon * "named" and "unnamed" anon region objects. Try to reduce the overhead 1861c7c3c6aSMatthew Dillon * of searching a named list by hashing it just a little. 1871c7c3c6aSMatthew Dillon */ 1881c7c3c6aSMatthew Dillon 1891c7c3c6aSMatthew Dillon #define NOBJLISTS 8 1901c7c3c6aSMatthew Dillon 1911c7c3c6aSMatthew Dillon #define NOBJLIST(handle) \ 192af647ddeSBruce Evans (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)]) 1931c7c3c6aSMatthew Dillon 194a9fa2c05SAlfred Perlstein static struct mtx sw_alloc_mtx; /* protect list manipulation */ 1951c7c3c6aSMatthew Dillon static struct pagerlst swap_pager_object_list[NOBJLISTS]; 196e9c0cc15SPoul-Henning Kamp static struct pagerlst swap_pager_un_object_list; 197e9c0cc15SPoul-Henning Kamp static uma_zone_t swap_zone; 1981c7c3c6aSMatthew Dillon 1991c7c3c6aSMatthew Dillon /* 2001c7c3c6aSMatthew Dillon * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 2011c7c3c6aSMatthew Dillon * calls hooked from other parts of the VM system and do not appear here. 2021c7c3c6aSMatthew Dillon * (see vm/swap_pager.h). 2031c7c3c6aSMatthew Dillon */ 204ff98689dSBruce Evans static vm_object_t 20511caded3SAlfred Perlstein swap_pager_alloc(void *handle, vm_ooffset_t size, 20611caded3SAlfred Perlstein vm_prot_t prot, vm_ooffset_t offset); 20711caded3SAlfred Perlstein static void swap_pager_dealloc(vm_object_t object); 20811caded3SAlfred Perlstein static int swap_pager_getpages(vm_object_t, vm_page_t *, int, int); 2095ea4972cSAlan Cox static boolean_t 2105ea4972cSAlan Cox swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after); 21111caded3SAlfred Perlstein static void swap_pager_init(void); 21211caded3SAlfred Perlstein static void swap_pager_unswapped(vm_page_t); 21311caded3SAlfred Perlstein static void swap_pager_strategy(vm_object_t, struct bio *); 214e9c0cc15SPoul-Henning Kamp static void swap_pager_swapoff(int devidx, int *sw_used); 215f708ef1bSPoul-Henning Kamp 216df8bae1dSRodney W. Grimes struct pagerops swappagerops = { 2171c7c3c6aSMatthew Dillon swap_pager_init, /* early system initialization of pager */ 2181c7c3c6aSMatthew Dillon swap_pager_alloc, /* allocate an OBJT_SWAP object */ 2191c7c3c6aSMatthew Dillon swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 2201c7c3c6aSMatthew Dillon swap_pager_getpages, /* pagein */ 2211c7c3c6aSMatthew Dillon swap_pager_putpages, /* pageout */ 2221c7c3c6aSMatthew Dillon swap_pager_haspage, /* get backing store status for page */ 223a5296b05SJulian Elischer swap_pager_unswapped, /* remove swap related to page */ 224a5296b05SJulian Elischer swap_pager_strategy /* pager strategy call */ 225df8bae1dSRodney W. Grimes }; 226df8bae1dSRodney W. Grimes 2270b441832SPoul-Henning Kamp static struct buf *getchainbuf(struct bio *bp, struct vnode *vp, int flags); 228e4057dbdSPoul-Henning Kamp static void flushchainbuf(struct buf *nbp); 2290b441832SPoul-Henning Kamp static void waitchainbuf(struct bio *bp, int count, int done); 230e4057dbdSPoul-Henning Kamp 2311c7c3c6aSMatthew Dillon /* 2321c7c3c6aSMatthew Dillon * dmmax is in page-sized chunks with the new swap system. It was 23364bcb9c8SMatthew Dillon * dev-bsized chunks in the old. dmmax is always a power of 2. 2341c7c3c6aSMatthew Dillon * 2351c7c3c6aSMatthew Dillon * swap_*() routines are externally accessible. swp_*() routines are 2361c7c3c6aSMatthew Dillon * internal. 2371c7c3c6aSMatthew Dillon */ 238e9c0cc15SPoul-Henning Kamp static int dmmax, dmmax_mask; 239e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 240e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 24126f9a767SRodney W. Grimes 242cee313c4SRobert Watson SYSCTL_INT(_vm, OID_AUTO, dmmax, 243cee313c4SRobert Watson CTLFLAG_RD, &dmmax, 0, "Maximum size of a swap block"); 244cee313c4SRobert Watson 24511caded3SAlfred Perlstein static __inline void swp_sizecheck(void); 24611caded3SAlfred Perlstein static void swp_pager_sync_iodone(struct buf *bp); 24711caded3SAlfred Perlstein static void swp_pager_async_iodone(struct buf *bp); 24824a1cce3SDavid Greenman 2491c7c3c6aSMatthew Dillon /* 2501c7c3c6aSMatthew Dillon * Swap bitmap functions 2511c7c3c6aSMatthew Dillon */ 25211caded3SAlfred Perlstein static __inline void swp_pager_freeswapspace(daddr_t blk, int npages); 25311caded3SAlfred Perlstein static __inline daddr_t swp_pager_getswapspace(int npages); 2541c7c3c6aSMatthew Dillon 2551c7c3c6aSMatthew Dillon /* 2561c7c3c6aSMatthew Dillon * Metadata functions 2571c7c3c6aSMatthew Dillon */ 25892da00bbSMatthew Dillon static __inline struct swblock ** 25992da00bbSMatthew Dillon swp_pager_hash(vm_object_t object, vm_pindex_t index); 26011caded3SAlfred Perlstein static void swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t); 26111caded3SAlfred Perlstein static void swp_pager_meta_free(vm_object_t, vm_pindex_t, daddr_t); 26211caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t); 26311caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int); 2641c7c3c6aSMatthew Dillon 2651c7c3c6aSMatthew Dillon /* 2661c7c3c6aSMatthew Dillon * SWP_SIZECHECK() - update swap_pager_full indication 2671c7c3c6aSMatthew Dillon * 26820d3034fSMatthew Dillon * update the swap_pager_almost_full indication and warn when we are 26920d3034fSMatthew Dillon * about to run out of swap space, using lowat/hiwat hysteresis. 27020d3034fSMatthew Dillon * 27120d3034fSMatthew Dillon * Clear swap_pager_full ( task killing ) indication when lowat is met. 2721c7c3c6aSMatthew Dillon * 2731c7c3c6aSMatthew Dillon * No restrictions on call 2741c7c3c6aSMatthew Dillon * This routine may not block. 2751c7c3c6aSMatthew Dillon * This routine must be called at splvm() 2761c7c3c6aSMatthew Dillon */ 277c1087c13SBruce Evans static __inline void 2781c7c3c6aSMatthew Dillon swp_sizecheck() 2790d94caffSDavid Greenman { 2800cddd8f0SMatthew Dillon GIANT_REQUIRED; 28123955314SAlfred Perlstein 2821c7c3c6aSMatthew Dillon if (vm_swap_size < nswap_lowat) { 28320d3034fSMatthew Dillon if (swap_pager_almost_full == 0) { 2841af87c92SDavid Greenman printf("swap_pager: out of swap space\n"); 28520d3034fSMatthew Dillon swap_pager_almost_full = 1; 2862b0d37a4SMatthew Dillon } 28720d3034fSMatthew Dillon } else { 28826f9a767SRodney W. Grimes swap_pager_full = 0; 28920d3034fSMatthew Dillon if (vm_swap_size > nswap_hiwat) 29020d3034fSMatthew Dillon swap_pager_almost_full = 0; 29126f9a767SRodney W. Grimes } 2921c7c3c6aSMatthew Dillon } 2931c7c3c6aSMatthew Dillon 2941c7c3c6aSMatthew Dillon /* 2951c7c3c6aSMatthew Dillon * SWAP_PAGER_INIT() - initialize the swap pager! 2961c7c3c6aSMatthew Dillon * 2971c7c3c6aSMatthew Dillon * Expected to be started from system init. NOTE: This code is run 2981c7c3c6aSMatthew Dillon * before much else so be careful what you depend on. Most of the VM 2991c7c3c6aSMatthew Dillon * system has yet to be initialized at this point. 3001c7c3c6aSMatthew Dillon */ 301f5a12711SPoul-Henning Kamp static void 302df8bae1dSRodney W. Grimes swap_pager_init() 303df8bae1dSRodney W. Grimes { 3041c7c3c6aSMatthew Dillon /* 3051c7c3c6aSMatthew Dillon * Initialize object lists 3061c7c3c6aSMatthew Dillon */ 3071c7c3c6aSMatthew Dillon int i; 3081c7c3c6aSMatthew Dillon 3091c7c3c6aSMatthew Dillon for (i = 0; i < NOBJLISTS; ++i) 3101c7c3c6aSMatthew Dillon TAILQ_INIT(&swap_pager_object_list[i]); 31124a1cce3SDavid Greenman TAILQ_INIT(&swap_pager_un_object_list); 3126008862bSJohn Baldwin mtx_init(&sw_alloc_mtx, "swap_pager list", NULL, MTX_DEF); 313df8bae1dSRodney W. Grimes 314df8bae1dSRodney W. Grimes /* 3151c7c3c6aSMatthew Dillon * Device Stripe, in PAGE_SIZE'd blocks 316df8bae1dSRodney W. Grimes */ 3171c7c3c6aSMatthew Dillon dmmax = SWB_NPAGES * 2; 3181c7c3c6aSMatthew Dillon dmmax_mask = ~(dmmax - 1); 3191c7c3c6aSMatthew Dillon } 32026f9a767SRodney W. Grimes 321df8bae1dSRodney W. Grimes /* 3221c7c3c6aSMatthew Dillon * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 3231c7c3c6aSMatthew Dillon * 3241c7c3c6aSMatthew Dillon * Expected to be started from pageout process once, prior to entering 3251c7c3c6aSMatthew Dillon * its main loop. 326df8bae1dSRodney W. Grimes */ 32724a1cce3SDavid Greenman void 32824a1cce3SDavid Greenman swap_pager_swap_init() 329df8bae1dSRodney W. Grimes { 33021cd6e62SSeigo Tanimura int n, n2; 3310d94caffSDavid Greenman 33226f9a767SRodney W. Grimes /* 3331c7c3c6aSMatthew Dillon * Number of in-transit swap bp operations. Don't 3341c7c3c6aSMatthew Dillon * exhaust the pbufs completely. Make sure we 3351c7c3c6aSMatthew Dillon * initialize workable values (0 will work for hysteresis 3361c7c3c6aSMatthew Dillon * but it isn't very efficient). 3371c7c3c6aSMatthew Dillon * 338327f4e83SMatthew Dillon * The nsw_cluster_max is constrained by the bp->b_pages[] 3391c7c3c6aSMatthew Dillon * array (MAXPHYS/PAGE_SIZE) and our locally defined 3401c7c3c6aSMatthew Dillon * MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 3411c7c3c6aSMatthew Dillon * constrained by the swap device interleave stripe size. 342327f4e83SMatthew Dillon * 343327f4e83SMatthew Dillon * Currently we hardwire nsw_wcount_async to 4. This limit is 344327f4e83SMatthew Dillon * designed to prevent other I/O from having high latencies due to 345327f4e83SMatthew Dillon * our pageout I/O. The value 4 works well for one or two active swap 346327f4e83SMatthew Dillon * devices but is probably a little low if you have more. Even so, 347327f4e83SMatthew Dillon * a higher value would probably generate only a limited improvement 348327f4e83SMatthew Dillon * with three or four active swap devices since the system does not 349327f4e83SMatthew Dillon * typically have to pageout at extreme bandwidths. We will want 350327f4e83SMatthew Dillon * at least 2 per swap devices, and 4 is a pretty good value if you 351327f4e83SMatthew Dillon * have one NFS swap device due to the command/ack latency over NFS. 352327f4e83SMatthew Dillon * So it all works out pretty well. 35326f9a767SRodney W. Grimes */ 354ad3cce20SMatthew Dillon nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER); 355327f4e83SMatthew Dillon 3566d541bf1SJohn Baldwin mtx_lock(&pbuf_mtx); 3571c7c3c6aSMatthew Dillon nsw_rcount = (nswbuf + 1) / 2; 358327f4e83SMatthew Dillon nsw_wcount_sync = (nswbuf + 3) / 4; 359327f4e83SMatthew Dillon nsw_wcount_async = 4; 360327f4e83SMatthew Dillon nsw_wcount_async_max = nsw_wcount_async; 3616d541bf1SJohn Baldwin mtx_unlock(&pbuf_mtx); 36224a1cce3SDavid Greenman 3631c7c3c6aSMatthew Dillon /* 3641c7c3c6aSMatthew Dillon * Initialize our zone. Right now I'm just guessing on the number 3651c7c3c6aSMatthew Dillon * we need based on the number of pages in the system. Each swblock 3662f9e4e80SMatthew Dillon * can hold 16 pages, so this is probably overkill. This reservation 367ec61f55dSMatthew Dillon * is typically limited to around 32MB by default. 3681c7c3c6aSMatthew Dillon */ 369ec61f55dSMatthew Dillon n = cnt.v_page_count / 2; 3702f9e4e80SMatthew Dillon if (maxswzone && n > maxswzone / sizeof(struct swblock)) 3712f9e4e80SMatthew Dillon n = maxswzone / sizeof(struct swblock); 37221cd6e62SSeigo Tanimura n2 = n; 373670d17b5SJeff Roberson swap_zone = uma_zcreate("SWAPMETA", sizeof(struct swblock), NULL, NULL, 374670d17b5SJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 3758355f576SJeff Roberson do { 3768355f576SJeff Roberson if (uma_zone_set_obj(swap_zone, NULL, n)) 37761ce6eeeSAlfred Perlstein break; 37861ce6eeeSAlfred Perlstein /* 37961ce6eeeSAlfred Perlstein * if the allocation failed, try a zone two thirds the 38061ce6eeeSAlfred Perlstein * size of the previous attempt. 38161ce6eeeSAlfred Perlstein */ 38261ce6eeeSAlfred Perlstein n -= ((n + 2) / 3); 38361ce6eeeSAlfred Perlstein } while (n > 0); 38421cd6e62SSeigo Tanimura if (swap_zone == NULL) 385670d17b5SJeff Roberson panic("failed to create swap_zone."); 38621cd6e62SSeigo Tanimura if (n2 != n) 38761ce6eeeSAlfred Perlstein printf("Swap zone entries reduced from %d to %d.\n", n2, n); 38821cd6e62SSeigo Tanimura n2 = n; 38924a1cce3SDavid Greenman 3901c7c3c6aSMatthew Dillon /* 3911c7c3c6aSMatthew Dillon * Initialize our meta-data hash table. The swapper does not need to 3921c7c3c6aSMatthew Dillon * be quite as efficient as the VM system, so we do not use an 3931c7c3c6aSMatthew Dillon * oversized hash table. 3941c7c3c6aSMatthew Dillon * 3951c7c3c6aSMatthew Dillon * n: size of hash table, must be power of 2 3961c7c3c6aSMatthew Dillon * swhash_mask: hash table index mask 3971c7c3c6aSMatthew Dillon */ 39861ce6eeeSAlfred Perlstein for (n = 1; n < n2 / 8; n *= 2) 3991c7c3c6aSMatthew Dillon ; 400a163d034SWarner Losh swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK | M_ZERO); 4011c7c3c6aSMatthew Dillon swhash_mask = n - 1; 40224a1cce3SDavid Greenman } 40324a1cce3SDavid Greenman 40424a1cce3SDavid Greenman /* 4051c7c3c6aSMatthew Dillon * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 4061c7c3c6aSMatthew Dillon * its metadata structures. 4071c7c3c6aSMatthew Dillon * 4081c7c3c6aSMatthew Dillon * This routine is called from the mmap and fork code to create a new 4091c7c3c6aSMatthew Dillon * OBJT_SWAP object. We do this by creating an OBJT_DEFAULT object 4101c7c3c6aSMatthew Dillon * and then converting it with swp_pager_meta_build(). 4111c7c3c6aSMatthew Dillon * 4121c7c3c6aSMatthew Dillon * This routine may block in vm_object_allocate() and create a named 4131c7c3c6aSMatthew Dillon * object lookup race, so we must interlock. We must also run at 4141c7c3c6aSMatthew Dillon * splvm() for the object lookup to handle races with interrupts, but 4151c7c3c6aSMatthew Dillon * we do not have to maintain splvm() in between the lookup and the 4161c7c3c6aSMatthew Dillon * add because (I believe) it is not possible to attempt to create 4171c7c3c6aSMatthew Dillon * a new swap object w/handle when a default object with that handle 4181c7c3c6aSMatthew Dillon * already exists. 41924c46d03SAlan Cox * 42024c46d03SAlan Cox * MPSAFE 42124a1cce3SDavid Greenman */ 422f5a12711SPoul-Henning Kamp static vm_object_t 4236cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 424b9dcd593SBruce Evans vm_ooffset_t offset) 42524a1cce3SDavid Greenman { 42624a1cce3SDavid Greenman vm_object_t object; 42724a1cce3SDavid Greenman 42824c46d03SAlan Cox mtx_lock(&Giant); 42924a1cce3SDavid Greenman if (handle) { 4301c7c3c6aSMatthew Dillon /* 4311c7c3c6aSMatthew Dillon * Reference existing named region or allocate new one. There 4321c7c3c6aSMatthew Dillon * should not be a race here against swp_pager_meta_build() 4331c7c3c6aSMatthew Dillon * as called from vm_page_remove() in regards to the lookup 4341c7c3c6aSMatthew Dillon * of the handle. 4351c7c3c6aSMatthew Dillon */ 4360cddd8f0SMatthew Dillon sx_xlock(&sw_alloc_sx); 4371c7c3c6aSMatthew Dillon object = vm_pager_object_lookup(NOBJLIST(handle), handle); 4381c7c3c6aSMatthew Dillon 43924a1cce3SDavid Greenman if (object != NULL) { 44024a1cce3SDavid Greenman vm_object_reference(object); 44124a1cce3SDavid Greenman } else { 4421c7c3c6aSMatthew Dillon object = vm_object_allocate(OBJT_DEFAULT, 4436cde7a16SDavid Greenman OFF_TO_IDX(offset + PAGE_MASK + size)); 44424a1cce3SDavid Greenman object->handle = handle; 4451c7c3c6aSMatthew Dillon 4464dcc5c2dSMatthew Dillon swp_pager_meta_build(object, 0, SWAPBLK_NONE); 44724a1cce3SDavid Greenman } 4480cddd8f0SMatthew Dillon sx_xunlock(&sw_alloc_sx); 44924a1cce3SDavid Greenman } else { 4501c7c3c6aSMatthew Dillon object = vm_object_allocate(OBJT_DEFAULT, 4516cde7a16SDavid Greenman OFF_TO_IDX(offset + PAGE_MASK + size)); 4521c7c3c6aSMatthew Dillon 4534dcc5c2dSMatthew Dillon swp_pager_meta_build(object, 0, SWAPBLK_NONE); 45424a1cce3SDavid Greenman } 45524c46d03SAlan Cox mtx_unlock(&Giant); 45624a1cce3SDavid Greenman return (object); 457df8bae1dSRodney W. Grimes } 458df8bae1dSRodney W. Grimes 45926f9a767SRodney W. Grimes /* 4601c7c3c6aSMatthew Dillon * SWAP_PAGER_DEALLOC() - remove swap metadata from object 4611c7c3c6aSMatthew Dillon * 4621c7c3c6aSMatthew Dillon * The swap backing for the object is destroyed. The code is 4631c7c3c6aSMatthew Dillon * designed such that we can reinstantiate it later, but this 4641c7c3c6aSMatthew Dillon * routine is typically called only when the entire object is 4651c7c3c6aSMatthew Dillon * about to be destroyed. 4661c7c3c6aSMatthew Dillon * 4671c7c3c6aSMatthew Dillon * This routine may block, but no longer does. 4681c7c3c6aSMatthew Dillon * 4691c7c3c6aSMatthew Dillon * The object must be locked or unreferenceable. 47026f9a767SRodney W. Grimes */ 471df8bae1dSRodney W. Grimes static void 4721c7c3c6aSMatthew Dillon swap_pager_dealloc(object) 4732a4895f4SDavid Greenman vm_object_t object; 47426f9a767SRodney W. Grimes { 4754dcc5c2dSMatthew Dillon int s; 4764dcc5c2dSMatthew Dillon 4770cddd8f0SMatthew Dillon GIANT_REQUIRED; 4780cddd8f0SMatthew Dillon 47926f9a767SRodney W. Grimes /* 4801c7c3c6aSMatthew Dillon * Remove from list right away so lookups will fail if we block for 4811c7c3c6aSMatthew Dillon * pageout completion. 48226f9a767SRodney W. Grimes */ 483a9fa2c05SAlfred Perlstein mtx_lock(&sw_alloc_mtx); 4841c7c3c6aSMatthew Dillon if (object->handle == NULL) { 4851c7c3c6aSMatthew Dillon TAILQ_REMOVE(&swap_pager_un_object_list, object, pager_object_list); 48624ea4a96SDavid Greenman } else { 4871c7c3c6aSMatthew Dillon TAILQ_REMOVE(NOBJLIST(object->handle), object, pager_object_list); 48826f9a767SRodney W. Grimes } 489a9fa2c05SAlfred Perlstein mtx_unlock(&sw_alloc_mtx); 4901c7c3c6aSMatthew Dillon 491658ad5ffSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 4921c7c3c6aSMatthew Dillon vm_object_pip_wait(object, "swpdea"); 4931c7c3c6aSMatthew Dillon 4941c7c3c6aSMatthew Dillon /* 4951c7c3c6aSMatthew Dillon * Free all remaining metadata. We only bother to free it from 4961c7c3c6aSMatthew Dillon * the swap meta data. We do not attempt to free swapblk's still 4971c7c3c6aSMatthew Dillon * associated with vm_page_t's for this object. We do not care 4981c7c3c6aSMatthew Dillon * if paging is still in progress on some objects. 4991c7c3c6aSMatthew Dillon */ 5004dcc5c2dSMatthew Dillon s = splvm(); 5011c7c3c6aSMatthew Dillon swp_pager_meta_free_all(object); 5024dcc5c2dSMatthew Dillon splx(s); 5031c7c3c6aSMatthew Dillon } 5041c7c3c6aSMatthew Dillon 5051c7c3c6aSMatthew Dillon /************************************************************************ 5061c7c3c6aSMatthew Dillon * SWAP PAGER BITMAP ROUTINES * 5071c7c3c6aSMatthew Dillon ************************************************************************/ 5081c7c3c6aSMatthew Dillon 5091c7c3c6aSMatthew Dillon /* 5101c7c3c6aSMatthew Dillon * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 5111c7c3c6aSMatthew Dillon * 5121c7c3c6aSMatthew Dillon * Allocate swap for the requested number of pages. The starting 5131c7c3c6aSMatthew Dillon * swap block number (a page index) is returned or SWAPBLK_NONE 5141c7c3c6aSMatthew Dillon * if the allocation failed. 5151c7c3c6aSMatthew Dillon * 5161c7c3c6aSMatthew Dillon * Also has the side effect of advising that somebody made a mistake 5171c7c3c6aSMatthew Dillon * when they configured swap and didn't configure enough. 5181c7c3c6aSMatthew Dillon * 5191c7c3c6aSMatthew Dillon * Must be called at splvm() to avoid races with bitmap frees from 5201c7c3c6aSMatthew Dillon * vm_page_remove() aka swap_pager_page_removed(). 5211c7c3c6aSMatthew Dillon * 5221c7c3c6aSMatthew Dillon * This routine may not block 5231c7c3c6aSMatthew Dillon * This routine must be called at splvm(). 5241c7c3c6aSMatthew Dillon */ 5251c7c3c6aSMatthew Dillon static __inline daddr_t 5261c7c3c6aSMatthew Dillon swp_pager_getswapspace(npages) 5271c7c3c6aSMatthew Dillon int npages; 5281c7c3c6aSMatthew Dillon { 5291c7c3c6aSMatthew Dillon daddr_t blk; 5301c7c3c6aSMatthew Dillon 5310cddd8f0SMatthew Dillon GIANT_REQUIRED; 5320cddd8f0SMatthew Dillon 5331c7c3c6aSMatthew Dillon if ((blk = blist_alloc(swapblist, npages)) == SWAPBLK_NONE) { 5342b0d37a4SMatthew Dillon if (swap_pager_full != 2) { 5351c7c3c6aSMatthew Dillon printf("swap_pager_getswapspace: failed\n"); 5362b0d37a4SMatthew Dillon swap_pager_full = 2; 53720d3034fSMatthew Dillon swap_pager_almost_full = 1; 5382b0d37a4SMatthew Dillon } 5391c7c3c6aSMatthew Dillon } else { 5401c7c3c6aSMatthew Dillon vm_swap_size -= npages; 541edfa785aSRobert Watson /* per-swap area stats */ 542edfa785aSRobert Watson swdevt[BLK2DEVIDX(blk)].sw_used += npages; 5431c7c3c6aSMatthew Dillon swp_sizecheck(); 5441c7c3c6aSMatthew Dillon } 5451c7c3c6aSMatthew Dillon return (blk); 54626f9a767SRodney W. Grimes } 54726f9a767SRodney W. Grimes 54826f9a767SRodney W. Grimes /* 5491c7c3c6aSMatthew Dillon * SWP_PAGER_FREESWAPSPACE() - free raw swap space 5501c7c3c6aSMatthew Dillon * 5511c7c3c6aSMatthew Dillon * This routine returns the specified swap blocks back to the bitmap. 5521c7c3c6aSMatthew Dillon * 5531c7c3c6aSMatthew Dillon * Note: This routine may not block (it could in the old swap code), 5541c7c3c6aSMatthew Dillon * and through the use of the new blist routines it does not block. 5551c7c3c6aSMatthew Dillon * 5561c7c3c6aSMatthew Dillon * We must be called at splvm() to avoid races with bitmap frees from 5571c7c3c6aSMatthew Dillon * vm_page_remove() aka swap_pager_page_removed(). 5581c7c3c6aSMatthew Dillon * 5591c7c3c6aSMatthew Dillon * This routine may not block 5601c7c3c6aSMatthew Dillon * This routine must be called at splvm(). 56126f9a767SRodney W. Grimes */ 5621c7c3c6aSMatthew Dillon static __inline void 5631c7c3c6aSMatthew Dillon swp_pager_freeswapspace(blk, npages) 5641c7c3c6aSMatthew Dillon daddr_t blk; 5651c7c3c6aSMatthew Dillon int npages; 5660d94caffSDavid Greenman { 56792da00bbSMatthew Dillon struct swdevt *sp = &swdevt[BLK2DEVIDX(blk)]; 56892da00bbSMatthew Dillon 5690cddd8f0SMatthew Dillon GIANT_REQUIRED; 57023955314SAlfred Perlstein 57192da00bbSMatthew Dillon /* per-swap area stats */ 57292da00bbSMatthew Dillon sp->sw_used -= npages; 57392da00bbSMatthew Dillon 57492da00bbSMatthew Dillon /* 57592da00bbSMatthew Dillon * If we are attempting to stop swapping on this device, we 57692da00bbSMatthew Dillon * don't want to mark any blocks free lest they be reused. 57792da00bbSMatthew Dillon */ 57892da00bbSMatthew Dillon if (sp->sw_flags & SW_CLOSING) 57992da00bbSMatthew Dillon return; 58092da00bbSMatthew Dillon 5811c7c3c6aSMatthew Dillon blist_free(swapblist, blk, npages); 5821c7c3c6aSMatthew Dillon vm_swap_size += npages; 5831c7c3c6aSMatthew Dillon swp_sizecheck(); 58426f9a767SRodney W. Grimes } 5851c7c3c6aSMatthew Dillon 58626f9a767SRodney W. Grimes /* 5871c7c3c6aSMatthew Dillon * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 5881c7c3c6aSMatthew Dillon * range within an object. 5891c7c3c6aSMatthew Dillon * 5901c7c3c6aSMatthew Dillon * This is a globally accessible routine. 5911c7c3c6aSMatthew Dillon * 5921c7c3c6aSMatthew Dillon * This routine removes swapblk assignments from swap metadata. 5931c7c3c6aSMatthew Dillon * 5941c7c3c6aSMatthew Dillon * The external callers of this routine typically have already destroyed 5951c7c3c6aSMatthew Dillon * or renamed vm_page_t's associated with this range in the object so 5961c7c3c6aSMatthew Dillon * we should be ok. 5974dcc5c2dSMatthew Dillon * 5984dcc5c2dSMatthew Dillon * This routine may be called at any spl. We up our spl to splvm temporarily 5994dcc5c2dSMatthew Dillon * in order to perform the metadata removal. 60026f9a767SRodney W. Grimes */ 60126f9a767SRodney W. Grimes void 60224a1cce3SDavid Greenman swap_pager_freespace(object, start, size) 60324a1cce3SDavid Greenman vm_object_t object; 604a316d390SJohn Dyson vm_pindex_t start; 605a316d390SJohn Dyson vm_size_t size; 60626f9a767SRodney W. Grimes { 6074dcc5c2dSMatthew Dillon int s = splvm(); 60823955314SAlfred Perlstein 60919ba4c8eSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 6101c7c3c6aSMatthew Dillon swp_pager_meta_free(object, start, size); 6114dcc5c2dSMatthew Dillon splx(s); 6124dcc5c2dSMatthew Dillon } 6134dcc5c2dSMatthew Dillon 6144dcc5c2dSMatthew Dillon /* 6154dcc5c2dSMatthew Dillon * SWAP_PAGER_RESERVE() - reserve swap blocks in object 6164dcc5c2dSMatthew Dillon * 6174dcc5c2dSMatthew Dillon * Assigns swap blocks to the specified range within the object. The 6184dcc5c2dSMatthew Dillon * swap blocks are not zerod. Any previous swap assignment is destroyed. 6194dcc5c2dSMatthew Dillon * 6204dcc5c2dSMatthew Dillon * Returns 0 on success, -1 on failure. 6214dcc5c2dSMatthew Dillon */ 6224dcc5c2dSMatthew Dillon int 6234dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size) 6244dcc5c2dSMatthew Dillon { 6254dcc5c2dSMatthew Dillon int s; 6264dcc5c2dSMatthew Dillon int n = 0; 6274dcc5c2dSMatthew Dillon daddr_t blk = SWAPBLK_NONE; 6284dcc5c2dSMatthew Dillon vm_pindex_t beg = start; /* save start index */ 6294dcc5c2dSMatthew Dillon 6304dcc5c2dSMatthew Dillon s = splvm(); 6314dcc5c2dSMatthew Dillon while (size) { 6324dcc5c2dSMatthew Dillon if (n == 0) { 6334dcc5c2dSMatthew Dillon n = BLIST_MAX_ALLOC; 6344dcc5c2dSMatthew Dillon while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) { 6354dcc5c2dSMatthew Dillon n >>= 1; 6364dcc5c2dSMatthew Dillon if (n == 0) { 6374dcc5c2dSMatthew Dillon swp_pager_meta_free(object, beg, start - beg); 6384dcc5c2dSMatthew Dillon splx(s); 6394dcc5c2dSMatthew Dillon return (-1); 6404dcc5c2dSMatthew Dillon } 6414dcc5c2dSMatthew Dillon } 6424dcc5c2dSMatthew Dillon } 6434dcc5c2dSMatthew Dillon swp_pager_meta_build(object, start, blk); 6444dcc5c2dSMatthew Dillon --size; 6454dcc5c2dSMatthew Dillon ++start; 6464dcc5c2dSMatthew Dillon ++blk; 6474dcc5c2dSMatthew Dillon --n; 6484dcc5c2dSMatthew Dillon } 6494dcc5c2dSMatthew Dillon swp_pager_meta_free(object, start, n); 6504dcc5c2dSMatthew Dillon splx(s); 6514dcc5c2dSMatthew Dillon return (0); 65226f9a767SRodney W. Grimes } 65326f9a767SRodney W. Grimes 6540a47b48bSJohn Dyson /* 6551c7c3c6aSMatthew Dillon * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 6561c7c3c6aSMatthew Dillon * and destroy the source. 6571c7c3c6aSMatthew Dillon * 6581c7c3c6aSMatthew Dillon * Copy any valid swapblks from the source to the destination. In 6591c7c3c6aSMatthew Dillon * cases where both the source and destination have a valid swapblk, 6601c7c3c6aSMatthew Dillon * we keep the destination's. 6611c7c3c6aSMatthew Dillon * 6621c7c3c6aSMatthew Dillon * This routine is allowed to block. It may block allocating metadata 6631c7c3c6aSMatthew Dillon * indirectly through swp_pager_meta_build() or if paging is still in 6641c7c3c6aSMatthew Dillon * progress on the source. 6651c7c3c6aSMatthew Dillon * 6664dcc5c2dSMatthew Dillon * This routine can be called at any spl 6674dcc5c2dSMatthew Dillon * 6681c7c3c6aSMatthew Dillon * XXX vm_page_collapse() kinda expects us not to block because we 6691c7c3c6aSMatthew Dillon * supposedly do not need to allocate memory, but for the moment we 6701c7c3c6aSMatthew Dillon * *may* have to get a little memory from the zone allocator, but 6711c7c3c6aSMatthew Dillon * it is taken from the interrupt memory. We should be ok. 6721c7c3c6aSMatthew Dillon * 6731c7c3c6aSMatthew Dillon * The source object contains no vm_page_t's (which is just as well) 6741c7c3c6aSMatthew Dillon * 6751c7c3c6aSMatthew Dillon * The source object is of type OBJT_SWAP. 6761c7c3c6aSMatthew Dillon * 6774dcc5c2dSMatthew Dillon * The source and destination objects must be locked or 6784dcc5c2dSMatthew Dillon * inaccessible (XXX are they ?) 67926f9a767SRodney W. Grimes */ 68026f9a767SRodney W. Grimes void 6811c7c3c6aSMatthew Dillon swap_pager_copy(srcobject, dstobject, offset, destroysource) 68224a1cce3SDavid Greenman vm_object_t srcobject; 68324a1cce3SDavid Greenman vm_object_t dstobject; 684a316d390SJohn Dyson vm_pindex_t offset; 685c0877f10SJohn Dyson int destroysource; 68626f9a767SRodney W. Grimes { 687a316d390SJohn Dyson vm_pindex_t i; 6884dcc5c2dSMatthew Dillon int s; 6894dcc5c2dSMatthew Dillon 6900cddd8f0SMatthew Dillon GIANT_REQUIRED; 6910cddd8f0SMatthew Dillon 6924dcc5c2dSMatthew Dillon s = splvm(); 69326f9a767SRodney W. Grimes /* 6941c7c3c6aSMatthew Dillon * If destroysource is set, we remove the source object from the 6951c7c3c6aSMatthew Dillon * swap_pager internal queue now. 69626f9a767SRodney W. Grimes */ 697cbd8ec09SJohn Dyson if (destroysource) { 698a9fa2c05SAlfred Perlstein mtx_lock(&sw_alloc_mtx); 69924a1cce3SDavid Greenman if (srcobject->handle == NULL) { 7001c7c3c6aSMatthew Dillon TAILQ_REMOVE( 7011c7c3c6aSMatthew Dillon &swap_pager_un_object_list, 7021c7c3c6aSMatthew Dillon srcobject, 7031c7c3c6aSMatthew Dillon pager_object_list 7041c7c3c6aSMatthew Dillon ); 70526f9a767SRodney W. Grimes } else { 7061c7c3c6aSMatthew Dillon TAILQ_REMOVE( 7071c7c3c6aSMatthew Dillon NOBJLIST(srcobject->handle), 7081c7c3c6aSMatthew Dillon srcobject, 7091c7c3c6aSMatthew Dillon pager_object_list 7101c7c3c6aSMatthew Dillon ); 71126f9a767SRodney W. Grimes } 712a9fa2c05SAlfred Perlstein mtx_unlock(&sw_alloc_mtx); 713cbd8ec09SJohn Dyson } 71426f9a767SRodney W. Grimes 7151c7c3c6aSMatthew Dillon /* 7161c7c3c6aSMatthew Dillon * transfer source to destination. 7171c7c3c6aSMatthew Dillon */ 7181c7c3c6aSMatthew Dillon for (i = 0; i < dstobject->size; ++i) { 7191c7c3c6aSMatthew Dillon daddr_t dstaddr; 7201c7c3c6aSMatthew Dillon 7211c7c3c6aSMatthew Dillon /* 7221c7c3c6aSMatthew Dillon * Locate (without changing) the swapblk on the destination, 7231c7c3c6aSMatthew Dillon * unless it is invalid in which case free it silently, or 7241c7c3c6aSMatthew Dillon * if the destination is a resident page, in which case the 7251c7c3c6aSMatthew Dillon * source is thrown away. 7261c7c3c6aSMatthew Dillon */ 7271c7c3c6aSMatthew Dillon dstaddr = swp_pager_meta_ctl(dstobject, i, 0); 7281c7c3c6aSMatthew Dillon 7291c7c3c6aSMatthew Dillon if (dstaddr == SWAPBLK_NONE) { 7301c7c3c6aSMatthew Dillon /* 7311c7c3c6aSMatthew Dillon * Destination has no swapblk and is not resident, 7321c7c3c6aSMatthew Dillon * copy source. 7331c7c3c6aSMatthew Dillon */ 7341c7c3c6aSMatthew Dillon daddr_t srcaddr; 7351c7c3c6aSMatthew Dillon 7361c7c3c6aSMatthew Dillon srcaddr = swp_pager_meta_ctl( 7371c7c3c6aSMatthew Dillon srcobject, 7381c7c3c6aSMatthew Dillon i + offset, 7391c7c3c6aSMatthew Dillon SWM_POP 7401c7c3c6aSMatthew Dillon ); 7411c7c3c6aSMatthew Dillon 7421c7c3c6aSMatthew Dillon if (srcaddr != SWAPBLK_NONE) 7434dcc5c2dSMatthew Dillon swp_pager_meta_build(dstobject, i, srcaddr); 7441c7c3c6aSMatthew Dillon } else { 7451c7c3c6aSMatthew Dillon /* 7461c7c3c6aSMatthew Dillon * Destination has valid swapblk or it is represented 7471c7c3c6aSMatthew Dillon * by a resident page. We destroy the sourceblock. 7481c7c3c6aSMatthew Dillon */ 7491c7c3c6aSMatthew Dillon 7501c7c3c6aSMatthew Dillon swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE); 7511c7c3c6aSMatthew Dillon } 75226f9a767SRodney W. Grimes } 75326f9a767SRodney W. Grimes 75426f9a767SRodney W. Grimes /* 7551c7c3c6aSMatthew Dillon * Free left over swap blocks in source. 7561c7c3c6aSMatthew Dillon * 7571c7c3c6aSMatthew Dillon * We have to revert the type to OBJT_DEFAULT so we do not accidently 7581c7c3c6aSMatthew Dillon * double-remove the object from the swap queues. 75926f9a767SRodney W. Grimes */ 760c0877f10SJohn Dyson if (destroysource) { 7611c7c3c6aSMatthew Dillon swp_pager_meta_free_all(srcobject); 7621c7c3c6aSMatthew Dillon /* 7631c7c3c6aSMatthew Dillon * Reverting the type is not necessary, the caller is going 7641c7c3c6aSMatthew Dillon * to destroy srcobject directly, but I'm doing it here 765956f3135SPhilippe Charnier * for consistency since we've removed the object from its 7661c7c3c6aSMatthew Dillon * queues. 7671c7c3c6aSMatthew Dillon */ 7681c7c3c6aSMatthew Dillon srcobject->type = OBJT_DEFAULT; 769c0877f10SJohn Dyson } 7704dcc5c2dSMatthew Dillon splx(s); 77126f9a767SRodney W. Grimes } 77226f9a767SRodney W. Grimes 773df8bae1dSRodney W. Grimes /* 7741c7c3c6aSMatthew Dillon * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 7751c7c3c6aSMatthew Dillon * the requested page. 7761c7c3c6aSMatthew Dillon * 7771c7c3c6aSMatthew Dillon * We determine whether good backing store exists for the requested 7781c7c3c6aSMatthew Dillon * page and return TRUE if it does, FALSE if it doesn't. 7791c7c3c6aSMatthew Dillon * 7801c7c3c6aSMatthew Dillon * If TRUE, we also try to determine how much valid, contiguous backing 7811c7c3c6aSMatthew Dillon * store exists before and after the requested page within a reasonable 7821c7c3c6aSMatthew Dillon * distance. We do not try to restrict it to the swap device stripe 7831c7c3c6aSMatthew Dillon * (that is handled in getpages/putpages). It probably isn't worth 7841c7c3c6aSMatthew Dillon * doing here. 785df8bae1dSRodney W. Grimes */ 7865ea4972cSAlan Cox static boolean_t 787a316d390SJohn Dyson swap_pager_haspage(object, pindex, before, after) 78824a1cce3SDavid Greenman vm_object_t object; 789a316d390SJohn Dyson vm_pindex_t pindex; 79024a1cce3SDavid Greenman int *before; 79124a1cce3SDavid Greenman int *after; 79226f9a767SRodney W. Grimes { 7931c7c3c6aSMatthew Dillon daddr_t blk0; 79425db2c54SMatthew Dillon int s; 79526f9a767SRodney W. Grimes 7961c7c3c6aSMatthew Dillon /* 7971c7c3c6aSMatthew Dillon * do we have good backing store at the requested index ? 7981c7c3c6aSMatthew Dillon */ 79925db2c54SMatthew Dillon s = splvm(); 8001c7c3c6aSMatthew Dillon blk0 = swp_pager_meta_ctl(object, pindex, 0); 8011c7c3c6aSMatthew Dillon 8024dcc5c2dSMatthew Dillon if (blk0 == SWAPBLK_NONE) { 80325db2c54SMatthew Dillon splx(s); 8041c7c3c6aSMatthew Dillon if (before) 80524a1cce3SDavid Greenman *before = 0; 8061c7c3c6aSMatthew Dillon if (after) 80724a1cce3SDavid Greenman *after = 0; 80826f9a767SRodney W. Grimes return (FALSE); 80926f9a767SRodney W. Grimes } 81026f9a767SRodney W. Grimes 81126f9a767SRodney W. Grimes /* 8121c7c3c6aSMatthew Dillon * find backwards-looking contiguous good backing store 813e47ed70bSJohn Dyson */ 8141c7c3c6aSMatthew Dillon if (before != NULL) { 81526f9a767SRodney W. Grimes int i; 8160d94caffSDavid Greenman 8171c7c3c6aSMatthew Dillon for (i = 1; i < (SWB_NPAGES/2); ++i) { 8181c7c3c6aSMatthew Dillon daddr_t blk; 8191c7c3c6aSMatthew Dillon 8201c7c3c6aSMatthew Dillon if (i > pindex) 8211c7c3c6aSMatthew Dillon break; 8221c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex - i, 0); 8231c7c3c6aSMatthew Dillon if (blk != blk0 - i) 8241c7c3c6aSMatthew Dillon break; 825ffc82b0aSJohn Dyson } 8261c7c3c6aSMatthew Dillon *before = (i - 1); 82726f9a767SRodney W. Grimes } 82826f9a767SRodney W. Grimes 82926f9a767SRodney W. Grimes /* 8301c7c3c6aSMatthew Dillon * find forward-looking contiguous good backing store 83126f9a767SRodney W. Grimes */ 8321c7c3c6aSMatthew Dillon if (after != NULL) { 8331c7c3c6aSMatthew Dillon int i; 8341c7c3c6aSMatthew Dillon 8351c7c3c6aSMatthew Dillon for (i = 1; i < (SWB_NPAGES/2); ++i) { 8361c7c3c6aSMatthew Dillon daddr_t blk; 8371c7c3c6aSMatthew Dillon 8381c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex + i, 0); 8391c7c3c6aSMatthew Dillon if (blk != blk0 + i) 8401c7c3c6aSMatthew Dillon break; 84126f9a767SRodney W. Grimes } 8421c7c3c6aSMatthew Dillon *after = (i - 1); 8431c7c3c6aSMatthew Dillon } 84425db2c54SMatthew Dillon splx(s); 8451c7c3c6aSMatthew Dillon return (TRUE); 8461c7c3c6aSMatthew Dillon } 8471c7c3c6aSMatthew Dillon 8481c7c3c6aSMatthew Dillon /* 8491c7c3c6aSMatthew Dillon * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 8501c7c3c6aSMatthew Dillon * 8511c7c3c6aSMatthew Dillon * This removes any associated swap backing store, whether valid or 8521c7c3c6aSMatthew Dillon * not, from the page. 8531c7c3c6aSMatthew Dillon * 8541c7c3c6aSMatthew Dillon * This routine is typically called when a page is made dirty, at 8551c7c3c6aSMatthew Dillon * which point any associated swap can be freed. MADV_FREE also 8561c7c3c6aSMatthew Dillon * calls us in a special-case situation 8571c7c3c6aSMatthew Dillon * 8581c7c3c6aSMatthew Dillon * NOTE!!! If the page is clean and the swap was valid, the caller 8591c7c3c6aSMatthew Dillon * should make the page dirty before calling this routine. This routine 8601c7c3c6aSMatthew Dillon * does NOT change the m->dirty status of the page. Also: MADV_FREE 8611c7c3c6aSMatthew Dillon * depends on it. 8621c7c3c6aSMatthew Dillon * 8631c7c3c6aSMatthew Dillon * This routine may not block 8644dcc5c2dSMatthew Dillon * This routine must be called at splvm() 8651c7c3c6aSMatthew Dillon */ 8661c7c3c6aSMatthew Dillon static void 8671c7c3c6aSMatthew Dillon swap_pager_unswapped(m) 8681c7c3c6aSMatthew Dillon vm_page_t m; 8691c7c3c6aSMatthew Dillon { 8701c7c3c6aSMatthew Dillon swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE); 8711c7c3c6aSMatthew Dillon } 8721c7c3c6aSMatthew Dillon 8731c7c3c6aSMatthew Dillon /* 874a5296b05SJulian Elischer * SWAP_PAGER_STRATEGY() - read, write, free blocks 875a5296b05SJulian Elischer * 876a5296b05SJulian Elischer * This implements the vm_pager_strategy() interface to swap and allows 877a5296b05SJulian Elischer * other parts of the system to directly access swap as backing store 878a5296b05SJulian Elischer * through vm_objects of type OBJT_SWAP. This is intended to be a 879a5296b05SJulian Elischer * cacheless interface ( i.e. caching occurs at higher levels ). 880a5296b05SJulian Elischer * Therefore we do not maintain any resident pages. All I/O goes 8814dcc5c2dSMatthew Dillon * directly to and from the swap device. 882a5296b05SJulian Elischer * 883a5296b05SJulian Elischer * Note that b_blkno is scaled for PAGE_SIZE 884a5296b05SJulian Elischer * 885a5296b05SJulian Elischer * We currently attempt to run I/O synchronously or asynchronously as 886a5296b05SJulian Elischer * the caller requests. This isn't perfect because we loose error 887a5296b05SJulian Elischer * sequencing when we run multiple ops in parallel to satisfy a request. 888a5296b05SJulian Elischer * But this is swap, so we let it all hang out. 889a5296b05SJulian Elischer */ 890a5296b05SJulian Elischer static void 8910b441832SPoul-Henning Kamp swap_pager_strategy(vm_object_t object, struct bio *bp) 892a5296b05SJulian Elischer { 893a5296b05SJulian Elischer vm_pindex_t start; 894a5296b05SJulian Elischer int count; 8954dcc5c2dSMatthew Dillon int s; 896a5296b05SJulian Elischer char *data; 897a5296b05SJulian Elischer struct buf *nbp = NULL; 898a5296b05SJulian Elischer 8990cddd8f0SMatthew Dillon GIANT_REQUIRED; 9000cddd8f0SMatthew Dillon 9010b441832SPoul-Henning Kamp /* XXX: KASSERT instead ? */ 9020b441832SPoul-Henning Kamp if (bp->bio_bcount & PAGE_MASK) { 903a468031cSPoul-Henning Kamp biofinish(bp, NULL, EINVAL); 9040b441832SPoul-Henning Kamp printf("swap_pager_strategy: bp %p blk %d size %d, not page bounded\n", bp, (int)bp->bio_pblkno, (int)bp->bio_bcount); 905a5296b05SJulian Elischer return; 906a5296b05SJulian Elischer } 907a5296b05SJulian Elischer 908a5296b05SJulian Elischer /* 909a5296b05SJulian Elischer * Clear error indication, initialize page index, count, data pointer. 910a5296b05SJulian Elischer */ 9110b441832SPoul-Henning Kamp bp->bio_error = 0; 9120b441832SPoul-Henning Kamp bp->bio_flags &= ~BIO_ERROR; 9130b441832SPoul-Henning Kamp bp->bio_resid = bp->bio_bcount; 914d6844b6bSTor Egge *(u_int *) &bp->bio_driver1 = 0; 915a5296b05SJulian Elischer 9160b441832SPoul-Henning Kamp start = bp->bio_pblkno; 9170b441832SPoul-Henning Kamp count = howmany(bp->bio_bcount, PAGE_SIZE); 9180b441832SPoul-Henning Kamp data = bp->bio_data; 919a5296b05SJulian Elischer 9204dcc5c2dSMatthew Dillon s = splvm(); 9214dcc5c2dSMatthew Dillon 922a5296b05SJulian Elischer /* 92321144e3bSPoul-Henning Kamp * Deal with BIO_DELETE 924a5296b05SJulian Elischer */ 9250b441832SPoul-Henning Kamp if (bp->bio_cmd == BIO_DELETE) { 926a5296b05SJulian Elischer /* 927a5296b05SJulian Elischer * FREE PAGE(s) - destroy underlying swap that is no longer 928a5296b05SJulian Elischer * needed. 929a5296b05SJulian Elischer */ 930a5296b05SJulian Elischer swp_pager_meta_free(object, start, count); 931a5296b05SJulian Elischer splx(s); 9320b441832SPoul-Henning Kamp bp->bio_resid = 0; 9330b441832SPoul-Henning Kamp biodone(bp); 9344dcc5c2dSMatthew Dillon return; 9354dcc5c2dSMatthew Dillon } 9364dcc5c2dSMatthew Dillon 937a5296b05SJulian Elischer /* 9384dcc5c2dSMatthew Dillon * Execute read or write 939a5296b05SJulian Elischer */ 940a5296b05SJulian Elischer while (count > 0) { 941a5296b05SJulian Elischer daddr_t blk; 942a5296b05SJulian Elischer 943a5296b05SJulian Elischer /* 9444dcc5c2dSMatthew Dillon * Obtain block. If block not found and writing, allocate a 9454dcc5c2dSMatthew Dillon * new block and build it into the object. 9464dcc5c2dSMatthew Dillon */ 9474dcc5c2dSMatthew Dillon 9484dcc5c2dSMatthew Dillon blk = swp_pager_meta_ctl(object, start, 0); 9490b441832SPoul-Henning Kamp if ((blk == SWAPBLK_NONE) && (bp->bio_cmd == BIO_WRITE)) { 9504dcc5c2dSMatthew Dillon blk = swp_pager_getswapspace(1); 9514dcc5c2dSMatthew Dillon if (blk == SWAPBLK_NONE) { 9520b441832SPoul-Henning Kamp bp->bio_error = ENOMEM; 9530b441832SPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 9544dcc5c2dSMatthew Dillon break; 9554dcc5c2dSMatthew Dillon } 9564dcc5c2dSMatthew Dillon swp_pager_meta_build(object, start, blk); 9574dcc5c2dSMatthew Dillon } 9584dcc5c2dSMatthew Dillon 9594dcc5c2dSMatthew Dillon /* 9604dcc5c2dSMatthew Dillon * Do we have to flush our current collection? Yes if: 9614dcc5c2dSMatthew Dillon * 9624dcc5c2dSMatthew Dillon * - no swap block at this index 9634dcc5c2dSMatthew Dillon * - swap block is not contiguous 9644dcc5c2dSMatthew Dillon * - we cross a physical disk boundry in the 9654dcc5c2dSMatthew Dillon * stripe. 966a5296b05SJulian Elischer */ 967a5296b05SJulian Elischer if ( 9684dcc5c2dSMatthew Dillon nbp && (nbp->b_blkno + btoc(nbp->b_bcount) != blk || 9694dcc5c2dSMatthew Dillon ((nbp->b_blkno ^ blk) & dmmax_mask) 970a5296b05SJulian Elischer ) 971a5296b05SJulian Elischer ) { 9724dcc5c2dSMatthew Dillon splx(s); 9730b441832SPoul-Henning Kamp if (bp->bio_cmd == BIO_READ) { 974a5296b05SJulian Elischer ++cnt.v_swapin; 975a5296b05SJulian Elischer cnt.v_swappgsin += btoc(nbp->b_bcount); 9764dcc5c2dSMatthew Dillon } else { 9774dcc5c2dSMatthew Dillon ++cnt.v_swapout; 9784dcc5c2dSMatthew Dillon cnt.v_swappgsout += btoc(nbp->b_bcount); 9794dcc5c2dSMatthew Dillon nbp->b_dirtyend = nbp->b_bcount; 9804dcc5c2dSMatthew Dillon } 981a5296b05SJulian Elischer flushchainbuf(nbp); 9824dcc5c2dSMatthew Dillon s = splvm(); 983a5296b05SJulian Elischer nbp = NULL; 984a5296b05SJulian Elischer } 985a5296b05SJulian Elischer 986a5296b05SJulian Elischer /* 9874dcc5c2dSMatthew Dillon * Add new swapblk to nbp, instantiating nbp if necessary. 9884dcc5c2dSMatthew Dillon * Zero-fill reads are able to take a shortcut. 989a5296b05SJulian Elischer */ 9904dcc5c2dSMatthew Dillon if (blk == SWAPBLK_NONE) { 9914dcc5c2dSMatthew Dillon /* 9924dcc5c2dSMatthew Dillon * We can only get here if we are reading. Since 9934dcc5c2dSMatthew Dillon * we are at splvm() we can safely modify b_resid, 9944dcc5c2dSMatthew Dillon * even if chain ops are in progress. 9954dcc5c2dSMatthew Dillon */ 996a5296b05SJulian Elischer bzero(data, PAGE_SIZE); 9970b441832SPoul-Henning Kamp bp->bio_resid -= PAGE_SIZE; 998a5296b05SJulian Elischer } else { 999a5296b05SJulian Elischer if (nbp == NULL) { 10000b441832SPoul-Henning Kamp nbp = getchainbuf(bp, swapdev_vp, B_ASYNC); 1001a5296b05SJulian Elischer nbp->b_blkno = blk; 10024dcc5c2dSMatthew Dillon nbp->b_bcount = 0; 1003a5296b05SJulian Elischer nbp->b_data = data; 1004a5296b05SJulian Elischer } 1005a5296b05SJulian Elischer nbp->b_bcount += PAGE_SIZE; 1006a5296b05SJulian Elischer } 1007a5296b05SJulian Elischer --count; 1008a5296b05SJulian Elischer ++start; 1009a5296b05SJulian Elischer data += PAGE_SIZE; 1010a5296b05SJulian Elischer } 1011a5296b05SJulian Elischer 1012a5296b05SJulian Elischer /* 10134dcc5c2dSMatthew Dillon * Flush out last buffer 1014a5296b05SJulian Elischer */ 1015a5296b05SJulian Elischer splx(s); 1016a5296b05SJulian Elischer 1017a5296b05SJulian Elischer if (nbp) { 101821144e3bSPoul-Henning Kamp if (nbp->b_iocmd == BIO_READ) { 1019a5296b05SJulian Elischer ++cnt.v_swapin; 1020a5296b05SJulian Elischer cnt.v_swappgsin += btoc(nbp->b_bcount); 1021a5296b05SJulian Elischer } else { 1022a5296b05SJulian Elischer ++cnt.v_swapout; 1023a5296b05SJulian Elischer cnt.v_swappgsout += btoc(nbp->b_bcount); 10244dcc5c2dSMatthew Dillon nbp->b_dirtyend = nbp->b_bcount; 1025a5296b05SJulian Elischer } 1026a5296b05SJulian Elischer flushchainbuf(nbp); 10274dcc5c2dSMatthew Dillon /* nbp = NULL; */ 10280cddd8f0SMatthew Dillon } 10294dcc5c2dSMatthew Dillon /* 10304dcc5c2dSMatthew Dillon * Wait for completion. 10314dcc5c2dSMatthew Dillon */ 1032a5296b05SJulian Elischer waitchainbuf(bp, 0, 1); 1033a5296b05SJulian Elischer } 1034a5296b05SJulian Elischer 1035a5296b05SJulian Elischer /* 10361c7c3c6aSMatthew Dillon * SWAP_PAGER_GETPAGES() - bring pages in from swap 10371c7c3c6aSMatthew Dillon * 10381c7c3c6aSMatthew Dillon * Attempt to retrieve (m, count) pages from backing store, but make 10391c7c3c6aSMatthew Dillon * sure we retrieve at least m[reqpage]. We try to load in as large 10401c7c3c6aSMatthew Dillon * a chunk surrounding m[reqpage] as is contiguous in swap and which 10411c7c3c6aSMatthew Dillon * belongs to the same object. 10421c7c3c6aSMatthew Dillon * 10431c7c3c6aSMatthew Dillon * The code is designed for asynchronous operation and 10441c7c3c6aSMatthew Dillon * immediate-notification of 'reqpage' but tends not to be 10451c7c3c6aSMatthew Dillon * used that way. Please do not optimize-out this algorithmic 10461c7c3c6aSMatthew Dillon * feature, I intend to improve on it in the future. 10471c7c3c6aSMatthew Dillon * 10481c7c3c6aSMatthew Dillon * The parent has a single vm_object_pip_add() reference prior to 10491c7c3c6aSMatthew Dillon * calling us and we should return with the same. 10501c7c3c6aSMatthew Dillon * 10511c7c3c6aSMatthew Dillon * The parent has BUSY'd the pages. We should return with 'm' 10521c7c3c6aSMatthew Dillon * left busy, but the others adjusted. 10531c7c3c6aSMatthew Dillon */ 1054f708ef1bSPoul-Henning Kamp static int 105524a1cce3SDavid Greenman swap_pager_getpages(object, m, count, reqpage) 105624a1cce3SDavid Greenman vm_object_t object; 105726f9a767SRodney W. Grimes vm_page_t *m; 105826f9a767SRodney W. Grimes int count, reqpage; 1059df8bae1dSRodney W. Grimes { 10601c7c3c6aSMatthew Dillon struct buf *bp; 10611c7c3c6aSMatthew Dillon vm_page_t mreq; 10621c7c3c6aSMatthew Dillon int s; 106326f9a767SRodney W. Grimes int i; 106426f9a767SRodney W. Grimes int j; 10651c7c3c6aSMatthew Dillon daddr_t blk; 10661c7c3c6aSMatthew Dillon vm_pindex_t lastpindex; 10670d94caffSDavid Greenman 10681c7c3c6aSMatthew Dillon mreq = m[reqpage]; 10691c7c3c6aSMatthew Dillon 10701c7c3c6aSMatthew Dillon if (mreq->object != object) { 10711c7c3c6aSMatthew Dillon panic("swap_pager_getpages: object mismatch %p/%p", 10721c7c3c6aSMatthew Dillon object, 10731c7c3c6aSMatthew Dillon mreq->object 10741c7c3c6aSMatthew Dillon ); 107526f9a767SRodney W. Grimes } 10761c7c3c6aSMatthew Dillon /* 10771c7c3c6aSMatthew Dillon * Calculate range to retrieve. The pages have already been assigned 10781c7c3c6aSMatthew Dillon * their swapblks. We require a *contiguous* range that falls entirely 10791c7c3c6aSMatthew Dillon * within a single device stripe. If we do not supply it, bad things 10804dcc5c2dSMatthew Dillon * happen. Note that blk, iblk & jblk can be SWAPBLK_NONE, but the 10814dcc5c2dSMatthew Dillon * loops are set up such that the case(s) are handled implicitly. 10824dcc5c2dSMatthew Dillon * 10834dcc5c2dSMatthew Dillon * The swp_*() calls must be made at splvm(). vm_page_free() does 10844dcc5c2dSMatthew Dillon * not need to be, but it will go a little faster if it is. 10851c7c3c6aSMatthew Dillon */ 10864dcc5c2dSMatthew Dillon s = splvm(); 10871c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(mreq->object, mreq->pindex, 0); 10881c7c3c6aSMatthew Dillon 10891c7c3c6aSMatthew Dillon for (i = reqpage - 1; i >= 0; --i) { 10901c7c3c6aSMatthew Dillon daddr_t iblk; 10911c7c3c6aSMatthew Dillon 10921c7c3c6aSMatthew Dillon iblk = swp_pager_meta_ctl(m[i]->object, m[i]->pindex, 0); 10931c7c3c6aSMatthew Dillon if (blk != iblk + (reqpage - i)) 109426f9a767SRodney W. Grimes break; 10954dcc5c2dSMatthew Dillon if ((blk ^ iblk) & dmmax_mask) 10964dcc5c2dSMatthew Dillon break; 109726f9a767SRodney W. Grimes } 10981c7c3c6aSMatthew Dillon ++i; 10991c7c3c6aSMatthew Dillon 11001c7c3c6aSMatthew Dillon for (j = reqpage + 1; j < count; ++j) { 11011c7c3c6aSMatthew Dillon daddr_t jblk; 11021c7c3c6aSMatthew Dillon 11031c7c3c6aSMatthew Dillon jblk = swp_pager_meta_ctl(m[j]->object, m[j]->pindex, 0); 11041c7c3c6aSMatthew Dillon if (blk != jblk - (j - reqpage)) 11051c7c3c6aSMatthew Dillon break; 11064dcc5c2dSMatthew Dillon if ((blk ^ jblk) & dmmax_mask) 11074dcc5c2dSMatthew Dillon break; 11081c7c3c6aSMatthew Dillon } 11091c7c3c6aSMatthew Dillon 11101c7c3c6aSMatthew Dillon /* 11111c7c3c6aSMatthew Dillon * free pages outside our collection range. Note: we never free 11121c7c3c6aSMatthew Dillon * mreq, it must remain busy throughout. 11131c7c3c6aSMatthew Dillon */ 1114ab9abe5dSAlan Cox vm_page_lock_queues(); 11151c7c3c6aSMatthew Dillon { 11161c7c3c6aSMatthew Dillon int k; 11171c7c3c6aSMatthew Dillon 11184dcc5c2dSMatthew Dillon for (k = 0; k < i; ++k) 11194dcc5c2dSMatthew Dillon vm_page_free(m[k]); 11204dcc5c2dSMatthew Dillon for (k = j; k < count; ++k) 11211c7c3c6aSMatthew Dillon vm_page_free(m[k]); 11221c7c3c6aSMatthew Dillon } 1123ab9abe5dSAlan Cox vm_page_unlock_queues(); 11244dcc5c2dSMatthew Dillon splx(s); 11254dcc5c2dSMatthew Dillon 11261c7c3c6aSMatthew Dillon 11271c7c3c6aSMatthew Dillon /* 11284dcc5c2dSMatthew Dillon * Return VM_PAGER_FAIL if we have nothing to do. Return mreq 11294dcc5c2dSMatthew Dillon * still busy, but the others unbusied. 11301c7c3c6aSMatthew Dillon */ 11314dcc5c2dSMatthew Dillon if (blk == SWAPBLK_NONE) 113226f9a767SRodney W. Grimes return (VM_PAGER_FAIL); 1133df8bae1dSRodney W. Grimes 113416f62314SDavid Greenman /* 11358630c117SAlan Cox * Getpbuf() can sleep. 11368630c117SAlan Cox */ 11378630c117SAlan Cox VM_OBJECT_UNLOCK(object); 11388630c117SAlan Cox /* 113916f62314SDavid Greenman * Get a swap buffer header to perform the IO 114016f62314SDavid Greenman */ 11411c7c3c6aSMatthew Dillon bp = getpbuf(&nsw_rcount); 114226f9a767SRodney W. Grimes 114316f62314SDavid Greenman /* 114416f62314SDavid Greenman * map our page(s) into kva for input 11451c7c3c6aSMatthew Dillon * 11461c7c3c6aSMatthew Dillon * NOTE: B_PAGING is set by pbgetvp() 114716f62314SDavid Greenman */ 1148d68d828bSAlan Cox pmap_qenter((vm_offset_t)bp->b_data, m + i, j - i); 11491c7c3c6aSMatthew Dillon 115021144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 11511c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 1152fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1153fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 11541c7c3c6aSMatthew Dillon bp->b_blkno = blk - (reqpage - i); 11551c7c3c6aSMatthew Dillon bp->b_bcount = PAGE_SIZE * (j - i); 11561c7c3c6aSMatthew Dillon bp->b_bufsize = PAGE_SIZE * (j - i); 11571c7c3c6aSMatthew Dillon bp->b_pager.pg_reqpage = reqpage - i; 11581c7c3c6aSMatthew Dillon 11598630c117SAlan Cox VM_OBJECT_LOCK(object); 1160b365ea9eSAlan Cox vm_page_lock_queues(); 11611c7c3c6aSMatthew Dillon { 11621c7c3c6aSMatthew Dillon int k; 11631c7c3c6aSMatthew Dillon 11641c7c3c6aSMatthew Dillon for (k = i; k < j; ++k) { 11651c7c3c6aSMatthew Dillon bp->b_pages[k - i] = m[k]; 11661c7c3c6aSMatthew Dillon vm_page_flag_set(m[k], PG_SWAPINPROG); 11671c7c3c6aSMatthew Dillon } 11681c7c3c6aSMatthew Dillon } 1169b365ea9eSAlan Cox vm_page_unlock_queues(); 11708630c117SAlan Cox VM_OBJECT_UNLOCK(object); 11711c7c3c6aSMatthew Dillon bp->b_npages = j - i; 117226f9a767SRodney W. Grimes 11730d94caffSDavid Greenman pbgetvp(swapdev_vp, bp); 1174df8bae1dSRodney W. Grimes 1175976e77fcSDavid Greenman cnt.v_swapin++; 11761c7c3c6aSMatthew Dillon cnt.v_swappgsin += bp->b_npages; 11771c7c3c6aSMatthew Dillon 1178df8bae1dSRodney W. Grimes /* 11791c7c3c6aSMatthew Dillon * We still hold the lock on mreq, and our automatic completion routine 11801c7c3c6aSMatthew Dillon * does not remove it. 1181df8bae1dSRodney W. Grimes */ 1182d68d828bSAlan Cox VM_OBJECT_LOCK(mreq->object); 11831c7c3c6aSMatthew Dillon vm_object_pip_add(mreq->object, bp->b_npages); 1184d68d828bSAlan Cox VM_OBJECT_UNLOCK(mreq->object); 11851c7c3c6aSMatthew Dillon lastpindex = m[j-1]->pindex; 11861c7c3c6aSMatthew Dillon 11871c7c3c6aSMatthew Dillon /* 11881c7c3c6aSMatthew Dillon * perform the I/O. NOTE!!! bp cannot be considered valid after 11891c7c3c6aSMatthew Dillon * this point because we automatically release it on completion. 11901c7c3c6aSMatthew Dillon * Instead, we look at the one page we are interested in which we 11911c7c3c6aSMatthew Dillon * still hold a lock on even through the I/O completion. 11921c7c3c6aSMatthew Dillon * 11931c7c3c6aSMatthew Dillon * The other pages in our m[] array are also released on completion, 11941c7c3c6aSMatthew Dillon * so we cannot assume they are valid anymore either. 11951c7c3c6aSMatthew Dillon * 1196ea3aecf5SPeter Wemm * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY 11971c7c3c6aSMatthew Dillon */ 1198b890cb2cSPeter Wemm BUF_KERNPROC(bp); 119986270230SPoul-Henning Kamp VOP_STRATEGY(bp->b_vp, bp); 120026f9a767SRodney W. Grimes 120126f9a767SRodney W. Grimes /* 12021c7c3c6aSMatthew Dillon * wait for the page we want to complete. PG_SWAPINPROG is always 12031c7c3c6aSMatthew Dillon * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 12041c7c3c6aSMatthew Dillon * is set in the meta-data. 120526f9a767SRodney W. Grimes */ 12061c7c3c6aSMatthew Dillon s = splvm(); 1207b365ea9eSAlan Cox vm_page_lock_queues(); 12081c7c3c6aSMatthew Dillon while ((mreq->flags & PG_SWAPINPROG) != 0) { 12091c7c3c6aSMatthew Dillon vm_page_flag_set(mreq, PG_WANTED | PG_REFERENCED); 12101c7c3c6aSMatthew Dillon cnt.v_intrans++; 1211b365ea9eSAlan Cox if (msleep(mreq, &vm_page_queue_mtx, PSWP, "swread", hz*20)) { 1212ac1e407bSBruce Evans printf( 12131c7c3c6aSMatthew Dillon "swap_pager: indefinite wait buffer: device:" 1214af647ddeSBruce Evans " %s, blkno: %ld, size: %ld\n", 1215af647ddeSBruce Evans devtoname(bp->b_dev), (long)bp->b_blkno, 1216af647ddeSBruce Evans bp->b_bcount 12171c7c3c6aSMatthew Dillon ); 12181c7c3c6aSMatthew Dillon } 12191b119d9dSDavid Greenman } 1220b365ea9eSAlan Cox vm_page_unlock_queues(); 1221df8bae1dSRodney W. Grimes splx(s); 122226f9a767SRodney W. Grimes 12238630c117SAlan Cox VM_OBJECT_LOCK(mreq->object); 122426f9a767SRodney W. Grimes /* 1225a1287949SEivind Eklund * mreq is left busied after completion, but all the other pages 12261c7c3c6aSMatthew Dillon * are freed. If we had an unrecoverable read error the page will 12271c7c3c6aSMatthew Dillon * not be valid. 122826f9a767SRodney W. Grimes */ 12291c7c3c6aSMatthew Dillon if (mreq->valid != VM_PAGE_BITS_ALL) { 12301c7c3c6aSMatthew Dillon return (VM_PAGER_ERROR); 123126f9a767SRodney W. Grimes } else { 12321c7c3c6aSMatthew Dillon return (VM_PAGER_OK); 123326f9a767SRodney W. Grimes } 12341c7c3c6aSMatthew Dillon 12351c7c3c6aSMatthew Dillon /* 12361c7c3c6aSMatthew Dillon * A final note: in a low swap situation, we cannot deallocate swap 12371c7c3c6aSMatthew Dillon * and mark a page dirty here because the caller is likely to mark 12381c7c3c6aSMatthew Dillon * the page clean when we return, causing the page to possibly revert 12391c7c3c6aSMatthew Dillon * to all-zero's later. 12401c7c3c6aSMatthew Dillon */ 1241df8bae1dSRodney W. Grimes } 1242df8bae1dSRodney W. Grimes 12431c7c3c6aSMatthew Dillon /* 12441c7c3c6aSMatthew Dillon * swap_pager_putpages: 12451c7c3c6aSMatthew Dillon * 12461c7c3c6aSMatthew Dillon * Assign swap (if necessary) and initiate I/O on the specified pages. 12471c7c3c6aSMatthew Dillon * 12481c7c3c6aSMatthew Dillon * We support both OBJT_DEFAULT and OBJT_SWAP objects. DEFAULT objects 12491c7c3c6aSMatthew Dillon * are automatically converted to SWAP objects. 12501c7c3c6aSMatthew Dillon * 1251ea3aecf5SPeter Wemm * In a low memory situation we may block in VOP_STRATEGY(), but the new 12521c7c3c6aSMatthew Dillon * vm_page reservation system coupled with properly written VFS devices 12531c7c3c6aSMatthew Dillon * should ensure that no low-memory deadlock occurs. This is an area 12541c7c3c6aSMatthew Dillon * which needs work. 12551c7c3c6aSMatthew Dillon * 12561c7c3c6aSMatthew Dillon * The parent has N vm_object_pip_add() references prior to 12571c7c3c6aSMatthew Dillon * calling us and will remove references for rtvals[] that are 12581c7c3c6aSMatthew Dillon * not set to VM_PAGER_PEND. We need to remove the rest on I/O 12591c7c3c6aSMatthew Dillon * completion. 12601c7c3c6aSMatthew Dillon * 12611c7c3c6aSMatthew Dillon * The parent has soft-busy'd the pages it passes us and will unbusy 12621c7c3c6aSMatthew Dillon * those whos rtvals[] entry is not set to VM_PAGER_PEND on return. 12631c7c3c6aSMatthew Dillon * We need to unbusy the rest on I/O completion. 12641c7c3c6aSMatthew Dillon */ 1265e4542174SMatthew Dillon void 126624a1cce3SDavid Greenman swap_pager_putpages(object, m, count, sync, rtvals) 126724a1cce3SDavid Greenman vm_object_t object; 126826f9a767SRodney W. Grimes vm_page_t *m; 126926f9a767SRodney W. Grimes int count; 127024a1cce3SDavid Greenman boolean_t sync; 127126f9a767SRodney W. Grimes int *rtvals; 1272df8bae1dSRodney W. Grimes { 12731c7c3c6aSMatthew Dillon int i; 12741c7c3c6aSMatthew Dillon int n = 0; 1275df8bae1dSRodney W. Grimes 12760cddd8f0SMatthew Dillon GIANT_REQUIRED; 12771c7c3c6aSMatthew Dillon if (count && m[0]->object != object) { 12781c7c3c6aSMatthew Dillon panic("swap_pager_getpages: object mismatch %p/%p", 12791c7c3c6aSMatthew Dillon object, 12801c7c3c6aSMatthew Dillon m[0]->object 12811c7c3c6aSMatthew Dillon ); 12821c7c3c6aSMatthew Dillon } 12831c7c3c6aSMatthew Dillon /* 12841c7c3c6aSMatthew Dillon * Step 1 12851c7c3c6aSMatthew Dillon * 12861c7c3c6aSMatthew Dillon * Turn object into OBJT_SWAP 12871c7c3c6aSMatthew Dillon * check for bogus sysops 12881c7c3c6aSMatthew Dillon * force sync if not pageout process 12891c7c3c6aSMatthew Dillon */ 12904dcc5c2dSMatthew Dillon if (object->type != OBJT_SWAP) 12914dcc5c2dSMatthew Dillon swp_pager_meta_build(object, 0, SWAPBLK_NONE); 1292e47ed70bSJohn Dyson 1293e47ed70bSJohn Dyson if (curproc != pageproc) 1294e47ed70bSJohn Dyson sync = TRUE; 129526f9a767SRodney W. Grimes 12961c7c3c6aSMatthew Dillon /* 12971c7c3c6aSMatthew Dillon * Step 2 12981c7c3c6aSMatthew Dillon * 1299ad3cce20SMatthew Dillon * Update nsw parameters from swap_async_max sysctl values. 1300ad3cce20SMatthew Dillon * Do not let the sysop crash the machine with bogus numbers. 1301327f4e83SMatthew Dillon */ 13026d541bf1SJohn Baldwin mtx_lock(&pbuf_mtx); 1303327f4e83SMatthew Dillon if (swap_async_max != nsw_wcount_async_max) { 1304327f4e83SMatthew Dillon int n; 1305327f4e83SMatthew Dillon int s; 1306327f4e83SMatthew Dillon 1307327f4e83SMatthew Dillon /* 1308327f4e83SMatthew Dillon * limit range 1309327f4e83SMatthew Dillon */ 1310327f4e83SMatthew Dillon if ((n = swap_async_max) > nswbuf / 2) 1311327f4e83SMatthew Dillon n = nswbuf / 2; 1312327f4e83SMatthew Dillon if (n < 1) 1313327f4e83SMatthew Dillon n = 1; 1314327f4e83SMatthew Dillon swap_async_max = n; 1315327f4e83SMatthew Dillon 1316327f4e83SMatthew Dillon /* 1317327f4e83SMatthew Dillon * Adjust difference ( if possible ). If the current async 1318327f4e83SMatthew Dillon * count is too low, we may not be able to make the adjustment 1319327f4e83SMatthew Dillon * at this time. 1320327f4e83SMatthew Dillon */ 1321327f4e83SMatthew Dillon s = splvm(); 1322327f4e83SMatthew Dillon n -= nsw_wcount_async_max; 1323327f4e83SMatthew Dillon if (nsw_wcount_async + n >= 0) { 1324327f4e83SMatthew Dillon nsw_wcount_async += n; 1325327f4e83SMatthew Dillon nsw_wcount_async_max += n; 1326327f4e83SMatthew Dillon wakeup(&nsw_wcount_async); 1327327f4e83SMatthew Dillon } 1328327f4e83SMatthew Dillon splx(s); 1329327f4e83SMatthew Dillon } 13306d541bf1SJohn Baldwin mtx_unlock(&pbuf_mtx); 1331327f4e83SMatthew Dillon 1332327f4e83SMatthew Dillon /* 1333327f4e83SMatthew Dillon * Step 3 1334327f4e83SMatthew Dillon * 13351c7c3c6aSMatthew Dillon * Assign swap blocks and issue I/O. We reallocate swap on the fly. 13361c7c3c6aSMatthew Dillon * The page is left dirty until the pageout operation completes 13371c7c3c6aSMatthew Dillon * successfully. 13381c7c3c6aSMatthew Dillon */ 13391c7c3c6aSMatthew Dillon for (i = 0; i < count; i += n) { 13401c7c3c6aSMatthew Dillon int s; 13411c7c3c6aSMatthew Dillon int j; 13421c7c3c6aSMatthew Dillon struct buf *bp; 1343a316d390SJohn Dyson daddr_t blk; 134426f9a767SRodney W. Grimes 1345df8bae1dSRodney W. Grimes /* 13461c7c3c6aSMatthew Dillon * Maximum I/O size is limited by a number of factors. 1347df8bae1dSRodney W. Grimes */ 13481c7c3c6aSMatthew Dillon n = min(BLIST_MAX_ALLOC, count - i); 1349327f4e83SMatthew Dillon n = min(n, nsw_cluster_max); 13501c7c3c6aSMatthew Dillon 13514dcc5c2dSMatthew Dillon s = splvm(); 13524dcc5c2dSMatthew Dillon 135326f9a767SRodney W. Grimes /* 13541c7c3c6aSMatthew Dillon * Get biggest block of swap we can. If we fail, fall 13551c7c3c6aSMatthew Dillon * back and try to allocate a smaller block. Don't go 13561c7c3c6aSMatthew Dillon * overboard trying to allocate space if it would overly 13571c7c3c6aSMatthew Dillon * fragment swap. 135826f9a767SRodney W. Grimes */ 13591c7c3c6aSMatthew Dillon while ( 13601c7c3c6aSMatthew Dillon (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE && 13611c7c3c6aSMatthew Dillon n > 4 13621c7c3c6aSMatthew Dillon ) { 13631c7c3c6aSMatthew Dillon n >>= 1; 136426f9a767SRodney W. Grimes } 13651c7c3c6aSMatthew Dillon if (blk == SWAPBLK_NONE) { 13664dcc5c2dSMatthew Dillon for (j = 0; j < n; ++j) 13671c7c3c6aSMatthew Dillon rtvals[i+j] = VM_PAGER_FAIL; 13684dcc5c2dSMatthew Dillon splx(s); 13691c7c3c6aSMatthew Dillon continue; 137026f9a767SRodney W. Grimes } 137126f9a767SRodney W. Grimes 137226f9a767SRodney W. Grimes /* 13734dcc5c2dSMatthew Dillon * The I/O we are constructing cannot cross a physical 13744dcc5c2dSMatthew Dillon * disk boundry in the swap stripe. Note: we are still 13754dcc5c2dSMatthew Dillon * at splvm(). 137626f9a767SRodney W. Grimes */ 13771c7c3c6aSMatthew Dillon if ((blk ^ (blk + n)) & dmmax_mask) { 13781c7c3c6aSMatthew Dillon j = ((blk + dmmax) & dmmax_mask) - blk; 13791c7c3c6aSMatthew Dillon swp_pager_freeswapspace(blk + j, n - j); 13801c7c3c6aSMatthew Dillon n = j; 1381e47ed70bSJohn Dyson } 138226f9a767SRodney W. Grimes 138326f9a767SRodney W. Grimes /* 13841c7c3c6aSMatthew Dillon * All I/O parameters have been satisfied, build the I/O 13851c7c3c6aSMatthew Dillon * request and assign the swap space. 13861c7c3c6aSMatthew Dillon * 13871c7c3c6aSMatthew Dillon * NOTE: B_PAGING is set by pbgetvp() 138826f9a767SRodney W. Grimes */ 1389327f4e83SMatthew Dillon if (sync == TRUE) { 1390327f4e83SMatthew Dillon bp = getpbuf(&nsw_wcount_sync); 1391327f4e83SMatthew Dillon } else { 1392327f4e83SMatthew Dillon bp = getpbuf(&nsw_wcount_async); 139321144e3bSPoul-Henning Kamp bp->b_flags = B_ASYNC; 1394327f4e83SMatthew Dillon } 1395912e4ae9SPoul-Henning Kamp bp->b_iocmd = BIO_WRITE; 139626f9a767SRodney W. Grimes 13971c7c3c6aSMatthew Dillon pmap_qenter((vm_offset_t)bp->b_data, &m[i], n); 13981c7c3c6aSMatthew Dillon 1399fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1400fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 14011c7c3c6aSMatthew Dillon bp->b_bcount = PAGE_SIZE * n; 14021c7c3c6aSMatthew Dillon bp->b_bufsize = PAGE_SIZE * n; 14031c7c3c6aSMatthew Dillon bp->b_blkno = blk; 1404e47ed70bSJohn Dyson 1405a5296b05SJulian Elischer pbgetvp(swapdev_vp, bp); 1406a5296b05SJulian Elischer 14071c7c3c6aSMatthew Dillon for (j = 0; j < n; ++j) { 14081c7c3c6aSMatthew Dillon vm_page_t mreq = m[i+j]; 14091c7c3c6aSMatthew Dillon 14101c7c3c6aSMatthew Dillon swp_pager_meta_build( 14111c7c3c6aSMatthew Dillon mreq->object, 14121c7c3c6aSMatthew Dillon mreq->pindex, 14134dcc5c2dSMatthew Dillon blk + j 14141c7c3c6aSMatthew Dillon ); 14157dbf82dcSMatthew Dillon vm_page_dirty(mreq); 14161c7c3c6aSMatthew Dillon rtvals[i+j] = VM_PAGER_OK; 14171c7c3c6aSMatthew Dillon 1418b365ea9eSAlan Cox vm_page_lock_queues(); 14191c7c3c6aSMatthew Dillon vm_page_flag_set(mreq, PG_SWAPINPROG); 1420b365ea9eSAlan Cox vm_page_unlock_queues(); 14211c7c3c6aSMatthew Dillon bp->b_pages[j] = mreq; 14221c7c3c6aSMatthew Dillon } 14231c7c3c6aSMatthew Dillon bp->b_npages = n; 1424a5296b05SJulian Elischer /* 1425a5296b05SJulian Elischer * Must set dirty range for NFS to work. 1426a5296b05SJulian Elischer */ 1427a5296b05SJulian Elischer bp->b_dirtyoff = 0; 1428a5296b05SJulian Elischer bp->b_dirtyend = bp->b_bcount; 14291c7c3c6aSMatthew Dillon 14301c7c3c6aSMatthew Dillon cnt.v_swapout++; 14311c7c3c6aSMatthew Dillon cnt.v_swappgsout += bp->b_npages; 14326a2eac8aSJeff Roberson VI_LOCK(swapdev_vp); 143326f9a767SRodney W. Grimes swapdev_vp->v_numoutput++; 14346a2eac8aSJeff Roberson VI_UNLOCK(swapdev_vp); 143526f9a767SRodney W. Grimes 14364dcc5c2dSMatthew Dillon splx(s); 14374dcc5c2dSMatthew Dillon 143826f9a767SRodney W. Grimes /* 14391c7c3c6aSMatthew Dillon * asynchronous 14401c7c3c6aSMatthew Dillon * 1441ea3aecf5SPeter Wemm * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY 144226f9a767SRodney W. Grimes */ 14431c7c3c6aSMatthew Dillon if (sync == FALSE) { 14441c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 144567812eacSKirk McKusick BUF_KERNPROC(bp); 144686270230SPoul-Henning Kamp VOP_STRATEGY(bp->b_vp, bp); 14471c7c3c6aSMatthew Dillon 14481c7c3c6aSMatthew Dillon for (j = 0; j < n; ++j) 14491c7c3c6aSMatthew Dillon rtvals[i+j] = VM_PAGER_PEND; 145023955314SAlfred Perlstein /* restart outter loop */ 14511c7c3c6aSMatthew Dillon continue; 145226f9a767SRodney W. Grimes } 1453e47ed70bSJohn Dyson 145426f9a767SRodney W. Grimes /* 14551c7c3c6aSMatthew Dillon * synchronous 14561c7c3c6aSMatthew Dillon * 1457ea3aecf5SPeter Wemm * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY 14581c7c3c6aSMatthew Dillon */ 14591c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_sync_iodone; 146086270230SPoul-Henning Kamp VOP_STRATEGY(bp->b_vp, bp); 14611c7c3c6aSMatthew Dillon 14621c7c3c6aSMatthew Dillon /* 14631c7c3c6aSMatthew Dillon * Wait for the sync I/O to complete, then update rtvals. 14641c7c3c6aSMatthew Dillon * We just set the rtvals[] to VM_PAGER_PEND so we can call 14651c7c3c6aSMatthew Dillon * our async completion routine at the end, thus avoiding a 14661c7c3c6aSMatthew Dillon * double-free. 146726f9a767SRodney W. Grimes */ 14684dcc5c2dSMatthew Dillon s = splbio(); 146926f9a767SRodney W. Grimes while ((bp->b_flags & B_DONE) == 0) { 147024a1cce3SDavid Greenman tsleep(bp, PVM, "swwrt", 0); 147126f9a767SRodney W. Grimes } 14721c7c3c6aSMatthew Dillon for (j = 0; j < n; ++j) 14731c7c3c6aSMatthew Dillon rtvals[i+j] = VM_PAGER_PEND; 14741c7c3c6aSMatthew Dillon /* 14751c7c3c6aSMatthew Dillon * Now that we are through with the bp, we can call the 14761c7c3c6aSMatthew Dillon * normal async completion, which frees everything up. 14771c7c3c6aSMatthew Dillon */ 14781c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp); 147926f9a767SRodney W. Grimes splx(s); 14801c7c3c6aSMatthew Dillon } 14811c7c3c6aSMatthew Dillon } 14821c7c3c6aSMatthew Dillon 14831c7c3c6aSMatthew Dillon /* 14841c7c3c6aSMatthew Dillon * swap_pager_sync_iodone: 14851c7c3c6aSMatthew Dillon * 14861c7c3c6aSMatthew Dillon * Completion routine for synchronous reads and writes from/to swap. 14871c7c3c6aSMatthew Dillon * We just mark the bp is complete and wake up anyone waiting on it. 14881c7c3c6aSMatthew Dillon * 14894dcc5c2dSMatthew Dillon * This routine may not block. This routine is called at splbio() or better. 14901c7c3c6aSMatthew Dillon */ 14911c7c3c6aSMatthew Dillon static void 14921c7c3c6aSMatthew Dillon swp_pager_sync_iodone(bp) 14931c7c3c6aSMatthew Dillon struct buf *bp; 14941c7c3c6aSMatthew Dillon { 14951c7c3c6aSMatthew Dillon bp->b_flags |= B_DONE; 14961c7c3c6aSMatthew Dillon bp->b_flags &= ~B_ASYNC; 14971c7c3c6aSMatthew Dillon wakeup(bp); 14981c7c3c6aSMatthew Dillon } 14991c7c3c6aSMatthew Dillon 15001c7c3c6aSMatthew Dillon /* 15011c7c3c6aSMatthew Dillon * swp_pager_async_iodone: 15021c7c3c6aSMatthew Dillon * 15031c7c3c6aSMatthew Dillon * Completion routine for asynchronous reads and writes from/to swap. 15041c7c3c6aSMatthew Dillon * Also called manually by synchronous code to finish up a bp. 15051c7c3c6aSMatthew Dillon * 15061c7c3c6aSMatthew Dillon * For READ operations, the pages are PG_BUSY'd. For WRITE operations, 15071c7c3c6aSMatthew Dillon * the pages are vm_page_t->busy'd. For READ operations, we PG_BUSY 15081c7c3c6aSMatthew Dillon * unbusy all pages except the 'main' request page. For WRITE 15091c7c3c6aSMatthew Dillon * operations, we vm_page_t->busy'd unbusy all pages ( we can do this 15101c7c3c6aSMatthew Dillon * because we marked them all VM_PAGER_PEND on return from putpages ). 15111c7c3c6aSMatthew Dillon * 15121c7c3c6aSMatthew Dillon * This routine may not block. 15134dcc5c2dSMatthew Dillon * This routine is called at splbio() or better 15144dcc5c2dSMatthew Dillon * 15154dcc5c2dSMatthew Dillon * We up ourselves to splvm() as required for various vm_page related 15164dcc5c2dSMatthew Dillon * calls. 15171c7c3c6aSMatthew Dillon */ 15181c7c3c6aSMatthew Dillon static void 15191c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp) 152054d92145SMatthew Dillon struct buf *bp; 15211c7c3c6aSMatthew Dillon { 15221c7c3c6aSMatthew Dillon int s; 15231c7c3c6aSMatthew Dillon int i; 15241c7c3c6aSMatthew Dillon vm_object_t object = NULL; 15251c7c3c6aSMatthew Dillon 15260cddd8f0SMatthew Dillon GIANT_REQUIRED; 15271c7c3c6aSMatthew Dillon bp->b_flags |= B_DONE; 15281c7c3c6aSMatthew Dillon 15291c7c3c6aSMatthew Dillon /* 15301c7c3c6aSMatthew Dillon * report error 15311c7c3c6aSMatthew Dillon */ 1532c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 15331c7c3c6aSMatthew Dillon printf( 15341c7c3c6aSMatthew Dillon "swap_pager: I/O error - %s failed; blkno %ld," 15351c7c3c6aSMatthew Dillon "size %ld, error %d\n", 153621144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 15371c7c3c6aSMatthew Dillon (long)bp->b_blkno, 15381c7c3c6aSMatthew Dillon (long)bp->b_bcount, 15391c7c3c6aSMatthew Dillon bp->b_error 15401c7c3c6aSMatthew Dillon ); 15411c7c3c6aSMatthew Dillon } 15421c7c3c6aSMatthew Dillon 15431c7c3c6aSMatthew Dillon /* 15444dcc5c2dSMatthew Dillon * set object, raise to splvm(). 15451c7c3c6aSMatthew Dillon */ 15464dcc5c2dSMatthew Dillon s = splvm(); 154726f9a767SRodney W. Grimes 154826f9a767SRodney W. Grimes /* 154926f9a767SRodney W. Grimes * remove the mapping for kernel virtual 155026f9a767SRodney W. Grimes */ 15511c7c3c6aSMatthew Dillon pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 155226f9a767SRodney W. Grimes 155333a609ecSAlan Cox if (bp->b_npages) { 155433a609ecSAlan Cox object = bp->b_pages[0]->object; 155533a609ecSAlan Cox VM_OBJECT_LOCK(object); 155633a609ecSAlan Cox } 155740eab1e9SAlan Cox vm_page_lock_queues(); 155826f9a767SRodney W. Grimes /* 15591c7c3c6aSMatthew Dillon * cleanup pages. If an error occurs writing to swap, we are in 15601c7c3c6aSMatthew Dillon * very serious trouble. If it happens to be a disk error, though, 15611c7c3c6aSMatthew Dillon * we may be able to recover by reassigning the swap later on. So 15621c7c3c6aSMatthew Dillon * in this case we remove the m->swapblk assignment for the page 15631c7c3c6aSMatthew Dillon * but do not free it in the rlist. The errornous block(s) are thus 15641c7c3c6aSMatthew Dillon * never reallocated as swap. Redirty the page and continue. 156526f9a767SRodney W. Grimes */ 15661c7c3c6aSMatthew Dillon for (i = 0; i < bp->b_npages; ++i) { 15671c7c3c6aSMatthew Dillon vm_page_t m = bp->b_pages[i]; 1568e47ed70bSJohn Dyson 15691c7c3c6aSMatthew Dillon vm_page_flag_clear(m, PG_SWAPINPROG); 1570e47ed70bSJohn Dyson 1571c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 1572ffc82b0aSJohn Dyson /* 15731c7c3c6aSMatthew Dillon * If an error occurs I'd love to throw the swapblk 15741c7c3c6aSMatthew Dillon * away without freeing it back to swapspace, so it 15751c7c3c6aSMatthew Dillon * can never be used again. But I can't from an 15761c7c3c6aSMatthew Dillon * interrupt. 1577ffc82b0aSJohn Dyson */ 157821144e3bSPoul-Henning Kamp if (bp->b_iocmd == BIO_READ) { 15791c7c3c6aSMatthew Dillon /* 15801c7c3c6aSMatthew Dillon * When reading, reqpage needs to stay 15811c7c3c6aSMatthew Dillon * locked for the parent, but all other 15821c7c3c6aSMatthew Dillon * pages can be freed. We still want to 15831c7c3c6aSMatthew Dillon * wakeup the parent waiting on the page, 15841c7c3c6aSMatthew Dillon * though. ( also: pg_reqpage can be -1 and 15851c7c3c6aSMatthew Dillon * not match anything ). 15861c7c3c6aSMatthew Dillon * 15871c7c3c6aSMatthew Dillon * We have to wake specifically requested pages 15881c7c3c6aSMatthew Dillon * up too because we cleared PG_SWAPINPROG and 15891c7c3c6aSMatthew Dillon * someone may be waiting for that. 15901c7c3c6aSMatthew Dillon * 15911c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably 1592956f3135SPhilippe Charnier * be overridden by the original caller of 15931c7c3c6aSMatthew Dillon * getpages so don't play cute tricks here. 15941c7c3c6aSMatthew Dillon * 1595279d7226SMatthew Dillon * XXX IT IS NOT LEGAL TO FREE THE PAGE HERE 1596279d7226SMatthew Dillon * AS THIS MESSES WITH object->memq, and it is 1597279d7226SMatthew Dillon * not legal to mess with object->memq from an 1598279d7226SMatthew Dillon * interrupt. 15991c7c3c6aSMatthew Dillon */ 16001c7c3c6aSMatthew Dillon m->valid = 0; 16011c7c3c6aSMatthew Dillon vm_page_flag_clear(m, PG_ZERO); 16021c7c3c6aSMatthew Dillon if (i != bp->b_pager.pg_reqpage) 16031c7c3c6aSMatthew Dillon vm_page_free(m); 16041c7c3c6aSMatthew Dillon else 16051c7c3c6aSMatthew Dillon vm_page_flash(m); 16061c7c3c6aSMatthew Dillon /* 16071c7c3c6aSMatthew Dillon * If i == bp->b_pager.pg_reqpage, do not wake 16081c7c3c6aSMatthew Dillon * the page up. The caller needs to. 16091c7c3c6aSMatthew Dillon */ 16101c7c3c6aSMatthew Dillon } else { 16111c7c3c6aSMatthew Dillon /* 16121c7c3c6aSMatthew Dillon * If a write error occurs, reactivate page 16131c7c3c6aSMatthew Dillon * so it doesn't clog the inactive list, 16141c7c3c6aSMatthew Dillon * then finish the I/O. 16151c7c3c6aSMatthew Dillon */ 16167dbf82dcSMatthew Dillon vm_page_dirty(m); 16171c7c3c6aSMatthew Dillon vm_page_activate(m); 16181c7c3c6aSMatthew Dillon vm_page_io_finish(m); 16191c7c3c6aSMatthew Dillon } 162021144e3bSPoul-Henning Kamp } else if (bp->b_iocmd == BIO_READ) { 16211c7c3c6aSMatthew Dillon /* 16221c7c3c6aSMatthew Dillon * For read success, clear dirty bits. Nobody should 16231c7c3c6aSMatthew Dillon * have this page mapped but don't take any chances, 16241c7c3c6aSMatthew Dillon * make sure the pmap modify bits are also cleared. 16251c7c3c6aSMatthew Dillon * 16261c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably be 1627956f3135SPhilippe Charnier * overridden by the original caller of getpages so 16281c7c3c6aSMatthew Dillon * we cannot set them in order to free the underlying 16291c7c3c6aSMatthew Dillon * swap in a low-swap situation. I don't think we'd 16301c7c3c6aSMatthew Dillon * want to do that anyway, but it was an optimization 16311c7c3c6aSMatthew Dillon * that existed in the old swapper for a time before 16321c7c3c6aSMatthew Dillon * it got ripped out due to precisely this problem. 16331c7c3c6aSMatthew Dillon * 16341c7c3c6aSMatthew Dillon * clear PG_ZERO in page. 16351c7c3c6aSMatthew Dillon * 16361c7c3c6aSMatthew Dillon * If not the requested page then deactivate it. 16371c7c3c6aSMatthew Dillon * 16381c7c3c6aSMatthew Dillon * Note that the requested page, reqpage, is left 16391c7c3c6aSMatthew Dillon * busied, but we still have to wake it up. The 16401c7c3c6aSMatthew Dillon * other pages are released (unbusied) by 16411c7c3c6aSMatthew Dillon * vm_page_wakeup(). We do not set reqpage's 16421c7c3c6aSMatthew Dillon * valid bits here, it is up to the caller. 16431c7c3c6aSMatthew Dillon */ 16440385347cSPeter Wemm pmap_clear_modify(m); 16451c7c3c6aSMatthew Dillon m->valid = VM_PAGE_BITS_ALL; 16462c28a105SAlan Cox vm_page_undirty(m); 16471c7c3c6aSMatthew Dillon vm_page_flag_clear(m, PG_ZERO); 16481c7c3c6aSMatthew Dillon 16491c7c3c6aSMatthew Dillon /* 16501c7c3c6aSMatthew Dillon * We have to wake specifically requested pages 16511c7c3c6aSMatthew Dillon * up too because we cleared PG_SWAPINPROG and 16521c7c3c6aSMatthew Dillon * could be waiting for it in getpages. However, 16531c7c3c6aSMatthew Dillon * be sure to not unbusy getpages specifically 16541c7c3c6aSMatthew Dillon * requested page - getpages expects it to be 16551c7c3c6aSMatthew Dillon * left busy. 16561c7c3c6aSMatthew Dillon */ 16571c7c3c6aSMatthew Dillon if (i != bp->b_pager.pg_reqpage) { 16581c7c3c6aSMatthew Dillon vm_page_deactivate(m); 16591c7c3c6aSMatthew Dillon vm_page_wakeup(m); 16601c7c3c6aSMatthew Dillon } else { 16611c7c3c6aSMatthew Dillon vm_page_flash(m); 16621c7c3c6aSMatthew Dillon } 16631c7c3c6aSMatthew Dillon } else { 16641c7c3c6aSMatthew Dillon /* 16651c7c3c6aSMatthew Dillon * For write success, clear the modify and dirty 16661c7c3c6aSMatthew Dillon * status, then finish the I/O ( which decrements the 16671c7c3c6aSMatthew Dillon * busy count and possibly wakes waiter's up ). 16681c7c3c6aSMatthew Dillon */ 16690385347cSPeter Wemm pmap_clear_modify(m); 1670c52e7044SAlan Cox vm_page_undirty(m); 16711c7c3c6aSMatthew Dillon vm_page_io_finish(m); 1672936524aaSMatthew Dillon if (!vm_page_count_severe() || !vm_page_try_to_cache(m)) 1673a12cc0e4SAlan Cox pmap_page_protect(m, VM_PROT_READ); 1674ffc82b0aSJohn Dyson } 1675df8bae1dSRodney W. Grimes } 167640eab1e9SAlan Cox vm_page_unlock_queues(); 167726f9a767SRodney W. Grimes 16781c7c3c6aSMatthew Dillon /* 16791c7c3c6aSMatthew Dillon * adjust pip. NOTE: the original parent may still have its own 16801c7c3c6aSMatthew Dillon * pip refs on the object. 16811c7c3c6aSMatthew Dillon */ 16820d420ad3SAlan Cox if (object != NULL) { 16831c7c3c6aSMatthew Dillon vm_object_pip_wakeupn(object, bp->b_npages); 16840d420ad3SAlan Cox VM_OBJECT_UNLOCK(object); 16850d420ad3SAlan Cox } 168626f9a767SRodney W. Grimes 16871c7c3c6aSMatthew Dillon /* 16881c7c3c6aSMatthew Dillon * release the physical I/O buffer 16891c7c3c6aSMatthew Dillon */ 1690327f4e83SMatthew Dillon relpbuf( 1691327f4e83SMatthew Dillon bp, 169221144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? &nsw_rcount : 1693327f4e83SMatthew Dillon ((bp->b_flags & B_ASYNC) ? 1694327f4e83SMatthew Dillon &nsw_wcount_async : 1695327f4e83SMatthew Dillon &nsw_wcount_sync 1696327f4e83SMatthew Dillon ) 1697327f4e83SMatthew Dillon ) 1698327f4e83SMatthew Dillon ); 169926f9a767SRodney W. Grimes splx(s); 170026f9a767SRodney W. Grimes } 17011c7c3c6aSMatthew Dillon 170292da00bbSMatthew Dillon /* 170392da00bbSMatthew Dillon * swap_pager_isswapped: 170492da00bbSMatthew Dillon * 170592da00bbSMatthew Dillon * Return 1 if at least one page in the given object is paged 170692da00bbSMatthew Dillon * out to the given swap device. 170792da00bbSMatthew Dillon * 170892da00bbSMatthew Dillon * This routine may not block. 170992da00bbSMatthew Dillon */ 171092da00bbSMatthew Dillon int swap_pager_isswapped(vm_object_t object, int devidx) { 171192da00bbSMatthew Dillon daddr_t index = 0; 171292da00bbSMatthew Dillon int bcount; 171392da00bbSMatthew Dillon int i; 171492da00bbSMatthew Dillon 171517cd3642SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 171692da00bbSMatthew Dillon for (bcount = 0; bcount < object->un_pager.swp.swp_bcount; bcount++) { 171792da00bbSMatthew Dillon struct swblock *swap; 171892da00bbSMatthew Dillon 171992da00bbSMatthew Dillon if ((swap = *swp_pager_hash(object, index)) != NULL) { 172092da00bbSMatthew Dillon for (i = 0; i < SWAP_META_PAGES; ++i) { 172192da00bbSMatthew Dillon daddr_t v = swap->swb_pages[i]; 172292da00bbSMatthew Dillon if (v != SWAPBLK_NONE && 172392da00bbSMatthew Dillon BLK2DEVIDX(v) == devidx) 172492da00bbSMatthew Dillon return 1; 172592da00bbSMatthew Dillon } 172692da00bbSMatthew Dillon } 172792da00bbSMatthew Dillon 172892da00bbSMatthew Dillon index += SWAP_META_PAGES; 172992da00bbSMatthew Dillon if (index > 0x20000000) 173092da00bbSMatthew Dillon panic("swap_pager_isswapped: failed to locate all swap meta blocks"); 173192da00bbSMatthew Dillon } 173292da00bbSMatthew Dillon return 0; 173392da00bbSMatthew Dillon } 173492da00bbSMatthew Dillon 173592da00bbSMatthew Dillon /* 173692da00bbSMatthew Dillon * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in 173792da00bbSMatthew Dillon * 173892da00bbSMatthew Dillon * This routine dissociates the page at the given index within a 173992da00bbSMatthew Dillon * swap block from its backing store, paging it in if necessary. 174092da00bbSMatthew Dillon * If the page is paged in, it is placed in the inactive queue, 174192da00bbSMatthew Dillon * since it had its backing store ripped out from under it. 174292da00bbSMatthew Dillon * We also attempt to swap in all other pages in the swap block, 174392da00bbSMatthew Dillon * we only guarantee that the one at the specified index is 174492da00bbSMatthew Dillon * paged in. 174592da00bbSMatthew Dillon * 174692da00bbSMatthew Dillon * XXX - The code to page the whole block in doesn't work, so we 174792da00bbSMatthew Dillon * revert to the one-by-one behavior for now. Sigh. 174892da00bbSMatthew Dillon */ 174992da00bbSMatthew Dillon static __inline void 175092da00bbSMatthew Dillon swp_pager_force_pagein(struct swblock *swap, int idx) 175192da00bbSMatthew Dillon { 175292da00bbSMatthew Dillon vm_object_t object; 175392da00bbSMatthew Dillon vm_page_t m; 175492da00bbSMatthew Dillon vm_pindex_t pindex; 175592da00bbSMatthew Dillon 175692da00bbSMatthew Dillon object = swap->swb_object; 175792da00bbSMatthew Dillon pindex = swap->swb_index; 175892da00bbSMatthew Dillon 1759d22bc710SAlan Cox VM_OBJECT_LOCK(object); 176092da00bbSMatthew Dillon vm_object_pip_add(object, 1); 176192da00bbSMatthew Dillon m = vm_page_grab(object, pindex + idx, VM_ALLOC_NORMAL|VM_ALLOC_RETRY); 176292da00bbSMatthew Dillon if (m->valid == VM_PAGE_BITS_ALL) { 176392da00bbSMatthew Dillon vm_object_pip_subtract(object, 1); 17640fa05eaeSAlan Cox VM_OBJECT_UNLOCK(object); 176592da00bbSMatthew Dillon vm_page_lock_queues(); 176692da00bbSMatthew Dillon vm_page_activate(m); 176792da00bbSMatthew Dillon vm_page_dirty(m); 176892da00bbSMatthew Dillon vm_page_wakeup(m); 176992da00bbSMatthew Dillon vm_page_unlock_queues(); 177092da00bbSMatthew Dillon vm_pager_page_unswapped(m); 177192da00bbSMatthew Dillon return; 177292da00bbSMatthew Dillon } 177392da00bbSMatthew Dillon 177492da00bbSMatthew Dillon if (swap_pager_getpages(object, &m, 1, 0) != 177592da00bbSMatthew Dillon VM_PAGER_OK) 177692da00bbSMatthew Dillon panic("swap_pager_force_pagein: read from swap failed");/*XXX*/ 177792da00bbSMatthew Dillon vm_object_pip_subtract(object, 1); 17780fa05eaeSAlan Cox VM_OBJECT_UNLOCK(object); 177992da00bbSMatthew Dillon 178092da00bbSMatthew Dillon vm_page_lock_queues(); 178192da00bbSMatthew Dillon vm_page_dirty(m); 178292da00bbSMatthew Dillon vm_page_dontneed(m); 178392da00bbSMatthew Dillon vm_page_wakeup(m); 178492da00bbSMatthew Dillon vm_page_unlock_queues(); 178592da00bbSMatthew Dillon vm_pager_page_unswapped(m); 178692da00bbSMatthew Dillon } 178792da00bbSMatthew Dillon 178892da00bbSMatthew Dillon 178992da00bbSMatthew Dillon /* 179092da00bbSMatthew Dillon * swap_pager_swapoff: 179192da00bbSMatthew Dillon * 179292da00bbSMatthew Dillon * Page in all of the pages that have been paged out to the 179392da00bbSMatthew Dillon * given device. The corresponding blocks in the bitmap must be 179492da00bbSMatthew Dillon * marked as allocated and the device must be flagged SW_CLOSING. 179592da00bbSMatthew Dillon * There may be no processes swapped out to the device. 179692da00bbSMatthew Dillon * 179792da00bbSMatthew Dillon * The sw_used parameter points to the field in the swdev structure 179892da00bbSMatthew Dillon * that contains a count of the number of blocks still allocated 179992da00bbSMatthew Dillon * on the device. If we encounter objects with a nonzero pip count 180092da00bbSMatthew Dillon * in our scan, we use this number to determine if we're really done. 180192da00bbSMatthew Dillon * 180292da00bbSMatthew Dillon * This routine may block. 180392da00bbSMatthew Dillon */ 1804e9c0cc15SPoul-Henning Kamp static void 180592da00bbSMatthew Dillon swap_pager_swapoff(int devidx, int *sw_used) 180692da00bbSMatthew Dillon { 180792da00bbSMatthew Dillon struct swblock **pswap; 180892da00bbSMatthew Dillon struct swblock *swap; 180992da00bbSMatthew Dillon vm_object_t waitobj; 181092da00bbSMatthew Dillon daddr_t v; 181192da00bbSMatthew Dillon int i, j; 181292da00bbSMatthew Dillon 181392da00bbSMatthew Dillon GIANT_REQUIRED; 181492da00bbSMatthew Dillon 181592da00bbSMatthew Dillon full_rescan: 181692da00bbSMatthew Dillon waitobj = NULL; 181792da00bbSMatthew Dillon for (i = 0; i <= swhash_mask; i++) { /* '<=' is correct here */ 181892da00bbSMatthew Dillon restart: 181992da00bbSMatthew Dillon pswap = &swhash[i]; 182092da00bbSMatthew Dillon while ((swap = *pswap) != NULL) { 182192da00bbSMatthew Dillon for (j = 0; j < SWAP_META_PAGES; ++j) { 182292da00bbSMatthew Dillon v = swap->swb_pages[j]; 182392da00bbSMatthew Dillon if (v != SWAPBLK_NONE && 182492da00bbSMatthew Dillon BLK2DEVIDX(v) == devidx) 182592da00bbSMatthew Dillon break; 182692da00bbSMatthew Dillon } 182792da00bbSMatthew Dillon if (j < SWAP_META_PAGES) { 182892da00bbSMatthew Dillon swp_pager_force_pagein(swap, j); 182992da00bbSMatthew Dillon goto restart; 183092da00bbSMatthew Dillon } else if (swap->swb_object->paging_in_progress) { 183192da00bbSMatthew Dillon if (!waitobj) 183292da00bbSMatthew Dillon waitobj = swap->swb_object; 183392da00bbSMatthew Dillon } 183492da00bbSMatthew Dillon pswap = &swap->swb_hnext; 183592da00bbSMatthew Dillon } 183692da00bbSMatthew Dillon } 183792da00bbSMatthew Dillon if (waitobj && *sw_used) { 183892da00bbSMatthew Dillon /* 183992da00bbSMatthew Dillon * We wait on an arbitrary object to clock our rescans 184092da00bbSMatthew Dillon * to the rate of paging completion. 184192da00bbSMatthew Dillon */ 18421ca58953SAlan Cox VM_OBJECT_LOCK(waitobj); 184392da00bbSMatthew Dillon vm_object_pip_wait(waitobj, "swpoff"); 18441ca58953SAlan Cox VM_OBJECT_UNLOCK(waitobj); 184592da00bbSMatthew Dillon goto full_rescan; 184692da00bbSMatthew Dillon } 184792da00bbSMatthew Dillon if (*sw_used) 184892da00bbSMatthew Dillon panic("swapoff: failed to locate %d swap blocks", *sw_used); 184992da00bbSMatthew Dillon } 185092da00bbSMatthew Dillon 18511c7c3c6aSMatthew Dillon /************************************************************************ 18521c7c3c6aSMatthew Dillon * SWAP META DATA * 18531c7c3c6aSMatthew Dillon ************************************************************************ 18541c7c3c6aSMatthew Dillon * 18551c7c3c6aSMatthew Dillon * These routines manipulate the swap metadata stored in the 18564dcc5c2dSMatthew Dillon * OBJT_SWAP object. All swp_*() routines must be called at 18574dcc5c2dSMatthew Dillon * splvm() because swap can be freed up by the low level vm_page 18584dcc5c2dSMatthew Dillon * code which might be called from interrupts beyond what splbio() covers. 18591c7c3c6aSMatthew Dillon * 18604dcc5c2dSMatthew Dillon * Swap metadata is implemented with a global hash and not directly 18614dcc5c2dSMatthew Dillon * linked into the object. Instead the object simply contains 18624dcc5c2dSMatthew Dillon * appropriate tracking counters. 18631c7c3c6aSMatthew Dillon */ 18641c7c3c6aSMatthew Dillon 18651c7c3c6aSMatthew Dillon /* 18661c7c3c6aSMatthew Dillon * SWP_PAGER_HASH() - hash swap meta data 18671c7c3c6aSMatthew Dillon * 18684dcc5c2dSMatthew Dillon * This is an inline helper function which hashes the swapblk given 18691c7c3c6aSMatthew Dillon * the object and page index. It returns a pointer to a pointer 18701c7c3c6aSMatthew Dillon * to the object, or a pointer to a NULL pointer if it could not 18711c7c3c6aSMatthew Dillon * find a swapblk. 18724dcc5c2dSMatthew Dillon * 18734dcc5c2dSMatthew Dillon * This routine must be called at splvm(). 18741c7c3c6aSMatthew Dillon */ 18751c7c3c6aSMatthew Dillon static __inline struct swblock ** 18764dcc5c2dSMatthew Dillon swp_pager_hash(vm_object_t object, vm_pindex_t index) 18771c7c3c6aSMatthew Dillon { 18781c7c3c6aSMatthew Dillon struct swblock **pswap; 18791c7c3c6aSMatthew Dillon struct swblock *swap; 18801c7c3c6aSMatthew Dillon 18815125fe4fSIan Dowse index &= ~(vm_pindex_t)SWAP_META_MASK; 1882af647ddeSBruce Evans pswap = &swhash[(index ^ (int)(intptr_t)object) & swhash_mask]; 18831c7c3c6aSMatthew Dillon while ((swap = *pswap) != NULL) { 18841c7c3c6aSMatthew Dillon if (swap->swb_object == object && 18851c7c3c6aSMatthew Dillon swap->swb_index == index 18861c7c3c6aSMatthew Dillon ) { 18871c7c3c6aSMatthew Dillon break; 18881c7c3c6aSMatthew Dillon } 18891c7c3c6aSMatthew Dillon pswap = &swap->swb_hnext; 18901c7c3c6aSMatthew Dillon } 18911c7c3c6aSMatthew Dillon return (pswap); 18921c7c3c6aSMatthew Dillon } 18931c7c3c6aSMatthew Dillon 18941c7c3c6aSMatthew Dillon /* 18951c7c3c6aSMatthew Dillon * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 18961c7c3c6aSMatthew Dillon * 18971c7c3c6aSMatthew Dillon * We first convert the object to a swap object if it is a default 18981c7c3c6aSMatthew Dillon * object. 18991c7c3c6aSMatthew Dillon * 19001c7c3c6aSMatthew Dillon * The specified swapblk is added to the object's swap metadata. If 19011c7c3c6aSMatthew Dillon * the swapblk is not valid, it is freed instead. Any previously 19021c7c3c6aSMatthew Dillon * assigned swapblk is freed. 19034dcc5c2dSMatthew Dillon * 19044dcc5c2dSMatthew Dillon * This routine must be called at splvm(), except when used to convert 19054dcc5c2dSMatthew Dillon * an OBJT_DEFAULT object into an OBJT_SWAP object. 19061c7c3c6aSMatthew Dillon */ 19071c7c3c6aSMatthew Dillon static void 19081c7c3c6aSMatthew Dillon swp_pager_meta_build( 19091c7c3c6aSMatthew Dillon vm_object_t object, 191023f09d50SIan Dowse vm_pindex_t pindex, 19114dcc5c2dSMatthew Dillon daddr_t swapblk 19121c7c3c6aSMatthew Dillon ) { 19131c7c3c6aSMatthew Dillon struct swblock *swap; 19141c7c3c6aSMatthew Dillon struct swblock **pswap; 191523f09d50SIan Dowse int idx; 19161c7c3c6aSMatthew Dillon 19170cddd8f0SMatthew Dillon GIANT_REQUIRED; 19181c7c3c6aSMatthew Dillon /* 19191c7c3c6aSMatthew Dillon * Convert default object to swap object if necessary 19201c7c3c6aSMatthew Dillon */ 19211c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) { 19221c7c3c6aSMatthew Dillon object->type = OBJT_SWAP; 19231c7c3c6aSMatthew Dillon object->un_pager.swp.swp_bcount = 0; 19241c7c3c6aSMatthew Dillon 1925a9fa2c05SAlfred Perlstein mtx_lock(&sw_alloc_mtx); 19261c7c3c6aSMatthew Dillon if (object->handle != NULL) { 19271c7c3c6aSMatthew Dillon TAILQ_INSERT_TAIL( 19281c7c3c6aSMatthew Dillon NOBJLIST(object->handle), 19291c7c3c6aSMatthew Dillon object, 19301c7c3c6aSMatthew Dillon pager_object_list 19311c7c3c6aSMatthew Dillon ); 19321c7c3c6aSMatthew Dillon } else { 19331c7c3c6aSMatthew Dillon TAILQ_INSERT_TAIL( 19341c7c3c6aSMatthew Dillon &swap_pager_un_object_list, 19351c7c3c6aSMatthew Dillon object, 19361c7c3c6aSMatthew Dillon pager_object_list 19371c7c3c6aSMatthew Dillon ); 19381c7c3c6aSMatthew Dillon } 1939a9fa2c05SAlfred Perlstein mtx_unlock(&sw_alloc_mtx); 19401c7c3c6aSMatthew Dillon } 19411c7c3c6aSMatthew Dillon 19421c7c3c6aSMatthew Dillon /* 19431c7c3c6aSMatthew Dillon * Locate hash entry. If not found create, but if we aren't adding 19444dcc5c2dSMatthew Dillon * anything just return. If we run out of space in the map we wait 19454dcc5c2dSMatthew Dillon * and, since the hash table may have changed, retry. 19461c7c3c6aSMatthew Dillon */ 19474dcc5c2dSMatthew Dillon retry: 194823f09d50SIan Dowse pswap = swp_pager_hash(object, pindex); 19491c7c3c6aSMatthew Dillon 19501c7c3c6aSMatthew Dillon if ((swap = *pswap) == NULL) { 19511c7c3c6aSMatthew Dillon int i; 19521c7c3c6aSMatthew Dillon 19531c7c3c6aSMatthew Dillon if (swapblk == SWAPBLK_NONE) 19541c7c3c6aSMatthew Dillon return; 19551c7c3c6aSMatthew Dillon 1956670d17b5SJeff Roberson swap = *pswap = uma_zalloc(swap_zone, M_NOWAIT); 19574dcc5c2dSMatthew Dillon if (swap == NULL) { 19584dcc5c2dSMatthew Dillon VM_WAIT; 19594dcc5c2dSMatthew Dillon goto retry; 19604dcc5c2dSMatthew Dillon } 1961670d17b5SJeff Roberson 19621c7c3c6aSMatthew Dillon swap->swb_hnext = NULL; 19631c7c3c6aSMatthew Dillon swap->swb_object = object; 196423f09d50SIan Dowse swap->swb_index = pindex & ~(vm_pindex_t)SWAP_META_MASK; 19651c7c3c6aSMatthew Dillon swap->swb_count = 0; 19661c7c3c6aSMatthew Dillon 19671c7c3c6aSMatthew Dillon ++object->un_pager.swp.swp_bcount; 19681c7c3c6aSMatthew Dillon 19691c7c3c6aSMatthew Dillon for (i = 0; i < SWAP_META_PAGES; ++i) 19701c7c3c6aSMatthew Dillon swap->swb_pages[i] = SWAPBLK_NONE; 19711c7c3c6aSMatthew Dillon } 19721c7c3c6aSMatthew Dillon 19731c7c3c6aSMatthew Dillon /* 19741c7c3c6aSMatthew Dillon * Delete prior contents of metadata 19751c7c3c6aSMatthew Dillon */ 197623f09d50SIan Dowse idx = pindex & SWAP_META_MASK; 19771c7c3c6aSMatthew Dillon 197823f09d50SIan Dowse if (swap->swb_pages[idx] != SWAPBLK_NONE) { 197923f09d50SIan Dowse swp_pager_freeswapspace(swap->swb_pages[idx], 1); 19801c7c3c6aSMatthew Dillon --swap->swb_count; 19811c7c3c6aSMatthew Dillon } 19821c7c3c6aSMatthew Dillon 19831c7c3c6aSMatthew Dillon /* 19841c7c3c6aSMatthew Dillon * Enter block into metadata 19851c7c3c6aSMatthew Dillon */ 198623f09d50SIan Dowse swap->swb_pages[idx] = swapblk; 19874dcc5c2dSMatthew Dillon if (swapblk != SWAPBLK_NONE) 19881c7c3c6aSMatthew Dillon ++swap->swb_count; 19891c7c3c6aSMatthew Dillon } 19901c7c3c6aSMatthew Dillon 19911c7c3c6aSMatthew Dillon /* 19921c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 19931c7c3c6aSMatthew Dillon * 19941c7c3c6aSMatthew Dillon * The requested range of blocks is freed, with any associated swap 19951c7c3c6aSMatthew Dillon * returned to the swap bitmap. 19961c7c3c6aSMatthew Dillon * 19971c7c3c6aSMatthew Dillon * This routine will free swap metadata structures as they are cleaned 19981c7c3c6aSMatthew Dillon * out. This routine does *NOT* operate on swap metadata associated 19991c7c3c6aSMatthew Dillon * with resident pages. 20001c7c3c6aSMatthew Dillon * 20011c7c3c6aSMatthew Dillon * This routine must be called at splvm() 20021c7c3c6aSMatthew Dillon */ 20031c7c3c6aSMatthew Dillon static void 20044dcc5c2dSMatthew Dillon swp_pager_meta_free(vm_object_t object, vm_pindex_t index, daddr_t count) 20051c7c3c6aSMatthew Dillon { 20060cddd8f0SMatthew Dillon GIANT_REQUIRED; 200723955314SAlfred Perlstein 20081c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) 20091c7c3c6aSMatthew Dillon return; 20101c7c3c6aSMatthew Dillon 20111c7c3c6aSMatthew Dillon while (count > 0) { 20121c7c3c6aSMatthew Dillon struct swblock **pswap; 20131c7c3c6aSMatthew Dillon struct swblock *swap; 20141c7c3c6aSMatthew Dillon 20151c7c3c6aSMatthew Dillon pswap = swp_pager_hash(object, index); 20161c7c3c6aSMatthew Dillon 20171c7c3c6aSMatthew Dillon if ((swap = *pswap) != NULL) { 20181c7c3c6aSMatthew Dillon daddr_t v = swap->swb_pages[index & SWAP_META_MASK]; 20191c7c3c6aSMatthew Dillon 20201c7c3c6aSMatthew Dillon if (v != SWAPBLK_NONE) { 20211c7c3c6aSMatthew Dillon swp_pager_freeswapspace(v, 1); 20221c7c3c6aSMatthew Dillon swap->swb_pages[index & SWAP_META_MASK] = 20231c7c3c6aSMatthew Dillon SWAPBLK_NONE; 20241c7c3c6aSMatthew Dillon if (--swap->swb_count == 0) { 20251c7c3c6aSMatthew Dillon *pswap = swap->swb_hnext; 2026670d17b5SJeff Roberson uma_zfree(swap_zone, swap); 20271c7c3c6aSMatthew Dillon --object->un_pager.swp.swp_bcount; 20281c7c3c6aSMatthew Dillon } 20291c7c3c6aSMatthew Dillon } 20301c7c3c6aSMatthew Dillon --count; 20311c7c3c6aSMatthew Dillon ++index; 20321c7c3c6aSMatthew Dillon } else { 20334dcc5c2dSMatthew Dillon int n = SWAP_META_PAGES - (index & SWAP_META_MASK); 20341c7c3c6aSMatthew Dillon count -= n; 20351c7c3c6aSMatthew Dillon index += n; 20361c7c3c6aSMatthew Dillon } 20371c7c3c6aSMatthew Dillon } 20381c7c3c6aSMatthew Dillon } 20391c7c3c6aSMatthew Dillon 20401c7c3c6aSMatthew Dillon /* 20411c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 20421c7c3c6aSMatthew Dillon * 20431c7c3c6aSMatthew Dillon * This routine locates and destroys all swap metadata associated with 20441c7c3c6aSMatthew Dillon * an object. 20454dcc5c2dSMatthew Dillon * 20464dcc5c2dSMatthew Dillon * This routine must be called at splvm() 20471c7c3c6aSMatthew Dillon */ 20481c7c3c6aSMatthew Dillon static void 20491c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object) 20501c7c3c6aSMatthew Dillon { 20511c7c3c6aSMatthew Dillon daddr_t index = 0; 20521c7c3c6aSMatthew Dillon 20530cddd8f0SMatthew Dillon GIANT_REQUIRED; 205423955314SAlfred Perlstein 20551c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) 20561c7c3c6aSMatthew Dillon return; 20571c7c3c6aSMatthew Dillon 20581c7c3c6aSMatthew Dillon while (object->un_pager.swp.swp_bcount) { 20591c7c3c6aSMatthew Dillon struct swblock **pswap; 20601c7c3c6aSMatthew Dillon struct swblock *swap; 20611c7c3c6aSMatthew Dillon 20621c7c3c6aSMatthew Dillon pswap = swp_pager_hash(object, index); 20631c7c3c6aSMatthew Dillon if ((swap = *pswap) != NULL) { 20641c7c3c6aSMatthew Dillon int i; 20651c7c3c6aSMatthew Dillon 20661c7c3c6aSMatthew Dillon for (i = 0; i < SWAP_META_PAGES; ++i) { 20671c7c3c6aSMatthew Dillon daddr_t v = swap->swb_pages[i]; 20681c7c3c6aSMatthew Dillon if (v != SWAPBLK_NONE) { 20691c7c3c6aSMatthew Dillon --swap->swb_count; 20704dcc5c2dSMatthew Dillon swp_pager_freeswapspace(v, 1); 20711c7c3c6aSMatthew Dillon } 20721c7c3c6aSMatthew Dillon } 20731c7c3c6aSMatthew Dillon if (swap->swb_count != 0) 20741c7c3c6aSMatthew Dillon panic("swap_pager_meta_free_all: swb_count != 0"); 20751c7c3c6aSMatthew Dillon *pswap = swap->swb_hnext; 2076670d17b5SJeff Roberson uma_zfree(swap_zone, swap); 20771c7c3c6aSMatthew Dillon --object->un_pager.swp.swp_bcount; 20781c7c3c6aSMatthew Dillon } 20791c7c3c6aSMatthew Dillon index += SWAP_META_PAGES; 20801c7c3c6aSMatthew Dillon if (index > 0x20000000) 20811c7c3c6aSMatthew Dillon panic("swp_pager_meta_free_all: failed to locate all swap meta blocks"); 20821c7c3c6aSMatthew Dillon } 20831c7c3c6aSMatthew Dillon } 20841c7c3c6aSMatthew Dillon 20851c7c3c6aSMatthew Dillon /* 20861c7c3c6aSMatthew Dillon * SWP_PAGER_METACTL() - misc control of swap and vm_page_t meta data. 20871c7c3c6aSMatthew Dillon * 20881c7c3c6aSMatthew Dillon * This routine is capable of looking up, popping, or freeing 20891c7c3c6aSMatthew Dillon * swapblk assignments in the swap meta data or in the vm_page_t. 20901c7c3c6aSMatthew Dillon * The routine typically returns the swapblk being looked-up, or popped, 20911c7c3c6aSMatthew Dillon * or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block 20921c7c3c6aSMatthew Dillon * was invalid. This routine will automatically free any invalid 20931c7c3c6aSMatthew Dillon * meta-data swapblks. 20941c7c3c6aSMatthew Dillon * 20951c7c3c6aSMatthew Dillon * It is not possible to store invalid swapblks in the swap meta data 20961c7c3c6aSMatthew Dillon * (other then a literal 'SWAPBLK_NONE'), so we don't bother checking. 20971c7c3c6aSMatthew Dillon * 20981c7c3c6aSMatthew Dillon * When acting on a busy resident page and paging is in progress, we 20991c7c3c6aSMatthew Dillon * have to wait until paging is complete but otherwise can act on the 21001c7c3c6aSMatthew Dillon * busy page. 21011c7c3c6aSMatthew Dillon * 21024dcc5c2dSMatthew Dillon * This routine must be called at splvm(). 21031c7c3c6aSMatthew Dillon * 21044dcc5c2dSMatthew Dillon * SWM_FREE remove and free swap block from metadata 21051c7c3c6aSMatthew Dillon * SWM_POP remove from meta data but do not free.. pop it out 21061c7c3c6aSMatthew Dillon */ 21071c7c3c6aSMatthew Dillon static daddr_t 21081c7c3c6aSMatthew Dillon swp_pager_meta_ctl( 21091c7c3c6aSMatthew Dillon vm_object_t object, 211023f09d50SIan Dowse vm_pindex_t pindex, 21111c7c3c6aSMatthew Dillon int flags 21121c7c3c6aSMatthew Dillon ) { 21134dcc5c2dSMatthew Dillon struct swblock **pswap; 21144dcc5c2dSMatthew Dillon struct swblock *swap; 21154dcc5c2dSMatthew Dillon daddr_t r1; 211623f09d50SIan Dowse int idx; 21174dcc5c2dSMatthew Dillon 21180cddd8f0SMatthew Dillon GIANT_REQUIRED; 21191c7c3c6aSMatthew Dillon /* 21201c7c3c6aSMatthew Dillon * The meta data only exists of the object is OBJT_SWAP 21211c7c3c6aSMatthew Dillon * and even then might not be allocated yet. 21221c7c3c6aSMatthew Dillon */ 21234dcc5c2dSMatthew Dillon if (object->type != OBJT_SWAP) 21241c7c3c6aSMatthew Dillon return (SWAPBLK_NONE); 21251c7c3c6aSMatthew Dillon 21264dcc5c2dSMatthew Dillon r1 = SWAPBLK_NONE; 212723f09d50SIan Dowse pswap = swp_pager_hash(object, pindex); 21281c7c3c6aSMatthew Dillon 21291c7c3c6aSMatthew Dillon if ((swap = *pswap) != NULL) { 213023f09d50SIan Dowse idx = pindex & SWAP_META_MASK; 213123f09d50SIan Dowse r1 = swap->swb_pages[idx]; 21321c7c3c6aSMatthew Dillon 21331c7c3c6aSMatthew Dillon if (r1 != SWAPBLK_NONE) { 21341c7c3c6aSMatthew Dillon if (flags & SWM_FREE) { 21354dcc5c2dSMatthew Dillon swp_pager_freeswapspace(r1, 1); 21361c7c3c6aSMatthew Dillon r1 = SWAPBLK_NONE; 21371c7c3c6aSMatthew Dillon } 21381c7c3c6aSMatthew Dillon if (flags & (SWM_FREE|SWM_POP)) { 213923f09d50SIan Dowse swap->swb_pages[idx] = SWAPBLK_NONE; 21401c7c3c6aSMatthew Dillon if (--swap->swb_count == 0) { 21411c7c3c6aSMatthew Dillon *pswap = swap->swb_hnext; 2142670d17b5SJeff Roberson uma_zfree(swap_zone, swap); 21431c7c3c6aSMatthew Dillon --object->un_pager.swp.swp_bcount; 21441c7c3c6aSMatthew Dillon } 21451c7c3c6aSMatthew Dillon } 21461c7c3c6aSMatthew Dillon } 21471c7c3c6aSMatthew Dillon } 21481c7c3c6aSMatthew Dillon return (r1); 21491c7c3c6aSMatthew Dillon } 21501c7c3c6aSMatthew Dillon 2151e4057dbdSPoul-Henning Kamp /******************************************************** 2152e4057dbdSPoul-Henning Kamp * CHAINING FUNCTIONS * 2153e4057dbdSPoul-Henning Kamp ******************************************************** 2154e4057dbdSPoul-Henning Kamp * 2155e4057dbdSPoul-Henning Kamp * These functions support recursion of I/O operations 2156e4057dbdSPoul-Henning Kamp * on bp's, typically by chaining one or more 'child' bp's 2157e4057dbdSPoul-Henning Kamp * to the parent. Synchronous, asynchronous, and semi-synchronous 2158e4057dbdSPoul-Henning Kamp * chaining is possible. 2159e4057dbdSPoul-Henning Kamp */ 2160e4057dbdSPoul-Henning Kamp 2161e4057dbdSPoul-Henning Kamp /* 2162e4057dbdSPoul-Henning Kamp * vm_pager_chain_iodone: 2163e4057dbdSPoul-Henning Kamp * 2164e4057dbdSPoul-Henning Kamp * io completion routine for child bp. Currently we fudge a bit 2165e4057dbdSPoul-Henning Kamp * on dealing with b_resid. Since users of these routines may issue 2166e4057dbdSPoul-Henning Kamp * multiple children simultaneously, sequencing of the error can be lost. 2167e4057dbdSPoul-Henning Kamp */ 2168e4057dbdSPoul-Henning Kamp static void 2169e4057dbdSPoul-Henning Kamp vm_pager_chain_iodone(struct buf *nbp) 2170e4057dbdSPoul-Henning Kamp { 21710b441832SPoul-Henning Kamp struct bio *bp; 21720b441832SPoul-Henning Kamp u_int *count; 2173e4057dbdSPoul-Henning Kamp 21740b441832SPoul-Henning Kamp bp = nbp->b_caller1; 2175d6844b6bSTor Egge count = (u_int *)&(bp->bio_driver1); 21760b441832SPoul-Henning Kamp if (bp != NULL) { 2177e4057dbdSPoul-Henning Kamp if (nbp->b_ioflags & BIO_ERROR) { 21780b441832SPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 21790b441832SPoul-Henning Kamp bp->bio_error = nbp->b_error; 2180e4057dbdSPoul-Henning Kamp } else if (nbp->b_resid != 0) { 21810b441832SPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 21820b441832SPoul-Henning Kamp bp->bio_error = EINVAL; 2183e4057dbdSPoul-Henning Kamp } else { 21840b441832SPoul-Henning Kamp bp->bio_resid -= nbp->b_bcount; 2185e4057dbdSPoul-Henning Kamp } 21860b441832SPoul-Henning Kamp nbp->b_caller1 = NULL; 21870b441832SPoul-Henning Kamp --(*count); 21880b441832SPoul-Henning Kamp if (bp->bio_flags & BIO_FLAG1) { 21890b441832SPoul-Henning Kamp bp->bio_flags &= ~BIO_FLAG1; 2190e4057dbdSPoul-Henning Kamp wakeup(bp); 2191e4057dbdSPoul-Henning Kamp } 2192e4057dbdSPoul-Henning Kamp } 2193e4057dbdSPoul-Henning Kamp nbp->b_flags |= B_DONE; 2194e4057dbdSPoul-Henning Kamp nbp->b_flags &= ~B_ASYNC; 2195e4057dbdSPoul-Henning Kamp relpbuf(nbp, NULL); 2196e4057dbdSPoul-Henning Kamp } 2197e4057dbdSPoul-Henning Kamp 2198e4057dbdSPoul-Henning Kamp /* 2199e4057dbdSPoul-Henning Kamp * getchainbuf: 2200e4057dbdSPoul-Henning Kamp * 2201e4057dbdSPoul-Henning Kamp * Obtain a physical buffer and chain it to its parent buffer. When 2202e4057dbdSPoul-Henning Kamp * I/O completes, the parent buffer will be B_SIGNAL'd. Errors are 2203e4057dbdSPoul-Henning Kamp * automatically propagated to the parent 2204e4057dbdSPoul-Henning Kamp */ 220537c84183SPoul-Henning Kamp static struct buf * 22060b441832SPoul-Henning Kamp getchainbuf(struct bio *bp, struct vnode *vp, int flags) 2207e4057dbdSPoul-Henning Kamp { 220823955314SAlfred Perlstein struct buf *nbp; 220923955314SAlfred Perlstein u_int *count; 221023955314SAlfred Perlstein 22110cddd8f0SMatthew Dillon GIANT_REQUIRED; 221223955314SAlfred Perlstein nbp = getpbuf(NULL); 2213d6844b6bSTor Egge count = (u_int *)&(bp->bio_driver1); 2214e4057dbdSPoul-Henning Kamp 22150b441832SPoul-Henning Kamp nbp->b_caller1 = bp; 22160b441832SPoul-Henning Kamp ++(*count); 2217e4057dbdSPoul-Henning Kamp 22180b441832SPoul-Henning Kamp if (*count > 4) 2219e4057dbdSPoul-Henning Kamp waitchainbuf(bp, 4, 0); 2220e4057dbdSPoul-Henning Kamp 22210b441832SPoul-Henning Kamp nbp->b_iocmd = bp->bio_cmd; 222257c10583SPoul-Henning Kamp nbp->b_ioflags = 0; 2223e4057dbdSPoul-Henning Kamp nbp->b_flags = flags; 2224fdcc1cc0SJohn Baldwin nbp->b_rcred = crhold(thread0.td_ucred); 2225fdcc1cc0SJohn Baldwin nbp->b_wcred = crhold(thread0.td_ucred); 2226e4057dbdSPoul-Henning Kamp nbp->b_iodone = vm_pager_chain_iodone; 2227e4057dbdSPoul-Henning Kamp 2228e4057dbdSPoul-Henning Kamp if (vp) 2229e4057dbdSPoul-Henning Kamp pbgetvp(vp, nbp); 2230e4057dbdSPoul-Henning Kamp return (nbp); 2231e4057dbdSPoul-Henning Kamp } 2232e4057dbdSPoul-Henning Kamp 223337c84183SPoul-Henning Kamp static void 2234e4057dbdSPoul-Henning Kamp flushchainbuf(struct buf *nbp) 2235e4057dbdSPoul-Henning Kamp { 22360cddd8f0SMatthew Dillon GIANT_REQUIRED; 2237e4057dbdSPoul-Henning Kamp if (nbp->b_bcount) { 2238e4057dbdSPoul-Henning Kamp nbp->b_bufsize = nbp->b_bcount; 2239e4057dbdSPoul-Henning Kamp if (nbp->b_iocmd == BIO_WRITE) 2240e4057dbdSPoul-Henning Kamp nbp->b_dirtyend = nbp->b_bcount; 2241e4057dbdSPoul-Henning Kamp BUF_KERNPROC(nbp); 224286270230SPoul-Henning Kamp VOP_STRATEGY(nbp->b_vp, nbp); 2243e4057dbdSPoul-Henning Kamp } else { 2244e4057dbdSPoul-Henning Kamp bufdone(nbp); 2245e4057dbdSPoul-Henning Kamp } 2246e4057dbdSPoul-Henning Kamp } 2247e4057dbdSPoul-Henning Kamp 224823955314SAlfred Perlstein static void 22490b441832SPoul-Henning Kamp waitchainbuf(struct bio *bp, int limit, int done) 2250e4057dbdSPoul-Henning Kamp { 2251e4057dbdSPoul-Henning Kamp int s; 225223955314SAlfred Perlstein u_int *count; 2253e4057dbdSPoul-Henning Kamp 22540cddd8f0SMatthew Dillon GIANT_REQUIRED; 2255d6844b6bSTor Egge count = (u_int *)&(bp->bio_driver1); 2256e4057dbdSPoul-Henning Kamp s = splbio(); 22570b441832SPoul-Henning Kamp while (*count > limit) { 22580b441832SPoul-Henning Kamp bp->bio_flags |= BIO_FLAG1; 2259e4057dbdSPoul-Henning Kamp tsleep(bp, PRIBIO + 4, "bpchain", 0); 2260e4057dbdSPoul-Henning Kamp } 2261e4057dbdSPoul-Henning Kamp if (done) { 22620b441832SPoul-Henning Kamp if (bp->bio_resid != 0 && !(bp->bio_flags & BIO_ERROR)) { 22630b441832SPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 22640b441832SPoul-Henning Kamp bp->bio_error = EINVAL; 2265e4057dbdSPoul-Henning Kamp } 22660b441832SPoul-Henning Kamp biodone(bp); 2267e4057dbdSPoul-Henning Kamp } 2268e4057dbdSPoul-Henning Kamp splx(s); 2269e4057dbdSPoul-Henning Kamp } 2270e4057dbdSPoul-Henning Kamp 2271e9c0cc15SPoul-Henning Kamp /* 2272e9c0cc15SPoul-Henning Kamp * swapdev_strategy: 2273e9c0cc15SPoul-Henning Kamp * 2274e9c0cc15SPoul-Henning Kamp * VOP_STRATEGY() for swapdev_vp. 2275e9c0cc15SPoul-Henning Kamp * Perform swap strategy interleave device selection. 2276e9c0cc15SPoul-Henning Kamp * 2277e9c0cc15SPoul-Henning Kamp * The bp is expected to be locked and *not* B_DONE on call. 2278e9c0cc15SPoul-Henning Kamp */ 2279e9c0cc15SPoul-Henning Kamp static int 2280e9c0cc15SPoul-Henning Kamp swapdev_strategy(ap) 2281e9c0cc15SPoul-Henning Kamp struct vop_strategy_args /* { 2282e9c0cc15SPoul-Henning Kamp struct vnode *a_vp; 2283e9c0cc15SPoul-Henning Kamp struct buf *a_bp; 2284e9c0cc15SPoul-Henning Kamp } */ *ap; 2285e9c0cc15SPoul-Henning Kamp { 2286e9c0cc15SPoul-Henning Kamp int s, sz, off, seg, index; 2287e9c0cc15SPoul-Henning Kamp struct swdevt *sp; 2288e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2289e9c0cc15SPoul-Henning Kamp struct buf *bp; 2290e9c0cc15SPoul-Henning Kamp 2291e9c0cc15SPoul-Henning Kamp KASSERT(ap->a_vp == ap->a_bp->b_vp, ("%s(%p != %p)", 2292e9c0cc15SPoul-Henning Kamp __func__, ap->a_vp, ap->a_bp->b_vp)); 2293e9c0cc15SPoul-Henning Kamp bp = ap->a_bp; 2294e9c0cc15SPoul-Henning Kamp sz = howmany(bp->b_bcount, PAGE_SIZE); 2295e9c0cc15SPoul-Henning Kamp 2296e9c0cc15SPoul-Henning Kamp /* 2297e9c0cc15SPoul-Henning Kamp * Convert interleaved swap into per-device swap. Note that 2298e9c0cc15SPoul-Henning Kamp * the block size is left in PAGE_SIZE'd chunks (for the newswap) 2299e9c0cc15SPoul-Henning Kamp * here. 2300e9c0cc15SPoul-Henning Kamp */ 2301e9c0cc15SPoul-Henning Kamp if (nswdev > 1) { 2302e9c0cc15SPoul-Henning Kamp off = bp->b_blkno % dmmax; 2303e9c0cc15SPoul-Henning Kamp if (off + sz > dmmax) { 2304e9c0cc15SPoul-Henning Kamp bp->b_error = EINVAL; 2305e9c0cc15SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2306e9c0cc15SPoul-Henning Kamp bufdone(bp); 2307e9c0cc15SPoul-Henning Kamp return 0; 2308e9c0cc15SPoul-Henning Kamp } 2309e9c0cc15SPoul-Henning Kamp seg = bp->b_blkno / dmmax; 2310e9c0cc15SPoul-Henning Kamp index = seg % nswdev; 2311e9c0cc15SPoul-Henning Kamp seg /= nswdev; 2312e9c0cc15SPoul-Henning Kamp bp->b_blkno = seg * dmmax + off; 2313e9c0cc15SPoul-Henning Kamp } else { 2314e9c0cc15SPoul-Henning Kamp index = 0; 2315e9c0cc15SPoul-Henning Kamp } 2316e9c0cc15SPoul-Henning Kamp sp = &swdevt[index]; 2317e9c0cc15SPoul-Henning Kamp if (bp->b_blkno + sz > sp->sw_nblks) { 2318e9c0cc15SPoul-Henning Kamp bp->b_error = EINVAL; 2319e9c0cc15SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2320e9c0cc15SPoul-Henning Kamp bufdone(bp); 2321e9c0cc15SPoul-Henning Kamp return 0; 2322e9c0cc15SPoul-Henning Kamp } 2323e9c0cc15SPoul-Henning Kamp bp->b_dev = sp->sw_device; 2324e9c0cc15SPoul-Henning Kamp if (sp->sw_vp == NULL) { 2325e9c0cc15SPoul-Henning Kamp bp->b_error = ENODEV; 2326e9c0cc15SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2327e9c0cc15SPoul-Henning Kamp bufdone(bp); 2328e9c0cc15SPoul-Henning Kamp return 0; 2329e9c0cc15SPoul-Henning Kamp } 2330e9c0cc15SPoul-Henning Kamp 2331e9c0cc15SPoul-Henning Kamp /* 2332e9c0cc15SPoul-Henning Kamp * Convert from PAGE_SIZE'd to DEV_BSIZE'd chunks for the actual I/O 2333e9c0cc15SPoul-Henning Kamp */ 2334e9c0cc15SPoul-Henning Kamp bp->b_blkno = ctodb(bp->b_blkno); 2335e9c0cc15SPoul-Henning Kamp 2336e9c0cc15SPoul-Henning Kamp vhold(sp->sw_vp); 2337e9c0cc15SPoul-Henning Kamp s = splvm(); 2338e9c0cc15SPoul-Henning Kamp if (bp->b_iocmd == BIO_WRITE) { 2339e9c0cc15SPoul-Henning Kamp vp = bp->b_vp; 2340e9c0cc15SPoul-Henning Kamp if (vp) { 2341e9c0cc15SPoul-Henning Kamp VI_LOCK(vp); 2342e9c0cc15SPoul-Henning Kamp vp->v_numoutput--; 2343e9c0cc15SPoul-Henning Kamp if ((vp->v_iflag & VI_BWAIT) && vp->v_numoutput <= 0) { 2344e9c0cc15SPoul-Henning Kamp vp->v_iflag &= ~VI_BWAIT; 2345e9c0cc15SPoul-Henning Kamp wakeup(&vp->v_numoutput); 2346e9c0cc15SPoul-Henning Kamp } 2347e9c0cc15SPoul-Henning Kamp VI_UNLOCK(vp); 2348e9c0cc15SPoul-Henning Kamp } 2349e9c0cc15SPoul-Henning Kamp VI_LOCK(sp->sw_vp); 2350e9c0cc15SPoul-Henning Kamp sp->sw_vp->v_numoutput++; 2351e9c0cc15SPoul-Henning Kamp VI_UNLOCK(sp->sw_vp); 2352e9c0cc15SPoul-Henning Kamp } 2353e9c0cc15SPoul-Henning Kamp bp->b_vp = sp->sw_vp; 2354e9c0cc15SPoul-Henning Kamp splx(s); 2355e9c0cc15SPoul-Henning Kamp if (bp->b_vp->v_type == VCHR) 2356e9c0cc15SPoul-Henning Kamp VOP_SPECSTRATEGY(bp->b_vp, bp); 2357e9c0cc15SPoul-Henning Kamp else 2358e9c0cc15SPoul-Henning Kamp VOP_STRATEGY(bp->b_vp, bp); 2359e9c0cc15SPoul-Henning Kamp return 0; 2360e9c0cc15SPoul-Henning Kamp } 2361e9c0cc15SPoul-Henning Kamp 2362e9c0cc15SPoul-Henning Kamp /* 2363e9c0cc15SPoul-Henning Kamp * Create a special vnode op vector for swapdev_vp - we only use 2364e9c0cc15SPoul-Henning Kamp * VOP_STRATEGY() and reclaim; everything else returns an error. 2365e9c0cc15SPoul-Henning Kamp */ 2366e9c0cc15SPoul-Henning Kamp vop_t **swapdev_vnodeop_p; 2367e9c0cc15SPoul-Henning Kamp static struct vnodeopv_entry_desc swapdev_vnodeop_entries[] = { 2368e9c0cc15SPoul-Henning Kamp { &vop_default_desc, (vop_t *) vop_defaultop }, 2369e9c0cc15SPoul-Henning Kamp { &vop_reclaim_desc, (vop_t *) vop_null }, 2370e9c0cc15SPoul-Henning Kamp { &vop_strategy_desc, (vop_t *) swapdev_strategy }, 2371e9c0cc15SPoul-Henning Kamp { NULL, NULL } 2372e9c0cc15SPoul-Henning Kamp }; 2373e9c0cc15SPoul-Henning Kamp static struct vnodeopv_desc swapdev_vnodeop_opv_desc = 2374e9c0cc15SPoul-Henning Kamp { &swapdev_vnodeop_p, swapdev_vnodeop_entries }; 2375e9c0cc15SPoul-Henning Kamp 2376e9c0cc15SPoul-Henning Kamp VNODEOP_SET(swapdev_vnodeop_opv_desc); 2377e9c0cc15SPoul-Henning Kamp 2378e9c0cc15SPoul-Henning Kamp /* 2379e9c0cc15SPoul-Henning Kamp * System call swapon(name) enables swapping on device name, 2380e9c0cc15SPoul-Henning Kamp * which must be in the swdevsw. Return EBUSY 2381e9c0cc15SPoul-Henning Kamp * if already swapping on this device. 2382e9c0cc15SPoul-Henning Kamp */ 2383e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2384e9c0cc15SPoul-Henning Kamp struct swapon_args { 2385e9c0cc15SPoul-Henning Kamp char *name; 2386e9c0cc15SPoul-Henning Kamp }; 2387e9c0cc15SPoul-Henning Kamp #endif 2388e9c0cc15SPoul-Henning Kamp 2389e9c0cc15SPoul-Henning Kamp /* 2390e9c0cc15SPoul-Henning Kamp * MPSAFE 2391e9c0cc15SPoul-Henning Kamp */ 2392e9c0cc15SPoul-Henning Kamp /* ARGSUSED */ 2393e9c0cc15SPoul-Henning Kamp int 2394e9c0cc15SPoul-Henning Kamp swapon(td, uap) 2395e9c0cc15SPoul-Henning Kamp struct thread *td; 2396e9c0cc15SPoul-Henning Kamp struct swapon_args *uap; 2397e9c0cc15SPoul-Henning Kamp { 2398e9c0cc15SPoul-Henning Kamp struct vattr attr; 2399e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2400e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2401e9c0cc15SPoul-Henning Kamp int error; 2402e9c0cc15SPoul-Henning Kamp 2403e9c0cc15SPoul-Henning Kamp mtx_lock(&Giant); 2404e9c0cc15SPoul-Henning Kamp error = suser(td); 2405e9c0cc15SPoul-Henning Kamp if (error) 2406e9c0cc15SPoul-Henning Kamp goto done2; 2407e9c0cc15SPoul-Henning Kamp 2408e9c0cc15SPoul-Henning Kamp while (swdev_syscall_active) 2409e9c0cc15SPoul-Henning Kamp tsleep(&swdev_syscall_active, PUSER - 1, "swpon", 0); 2410e9c0cc15SPoul-Henning Kamp swdev_syscall_active = 1; 2411e9c0cc15SPoul-Henning Kamp 2412e9c0cc15SPoul-Henning Kamp /* 2413e9c0cc15SPoul-Henning Kamp * Swap metadata may not fit in the KVM if we have physical 2414e9c0cc15SPoul-Henning Kamp * memory of >1GB. 2415e9c0cc15SPoul-Henning Kamp */ 2416e9c0cc15SPoul-Henning Kamp if (swap_zone == NULL) { 2417e9c0cc15SPoul-Henning Kamp error = ENOMEM; 2418e9c0cc15SPoul-Henning Kamp goto done; 2419e9c0cc15SPoul-Henning Kamp } 2420e9c0cc15SPoul-Henning Kamp 2421e9c0cc15SPoul-Henning Kamp NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->name, td); 2422e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2423e9c0cc15SPoul-Henning Kamp if (error) 2424e9c0cc15SPoul-Henning Kamp goto done; 2425e9c0cc15SPoul-Henning Kamp 2426e9c0cc15SPoul-Henning Kamp NDFREE(&nd, NDF_ONLY_PNBUF); 2427e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2428e9c0cc15SPoul-Henning Kamp 2429e9c0cc15SPoul-Henning Kamp if (vn_isdisk(vp, &error)) 2430e9c0cc15SPoul-Henning Kamp error = swaponvp(td, vp, vp->v_rdev, 0); 2431e9c0cc15SPoul-Henning Kamp else if (vp->v_type == VREG && 2432e9c0cc15SPoul-Henning Kamp (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 2433e9c0cc15SPoul-Henning Kamp (error = VOP_GETATTR(vp, &attr, td->td_ucred, td)) == 0) { 2434e9c0cc15SPoul-Henning Kamp /* 2435e9c0cc15SPoul-Henning Kamp * Allow direct swapping to NFS regular files in the same 2436e9c0cc15SPoul-Henning Kamp * way that nfs_mountroot() sets up diskless swapping. 2437e9c0cc15SPoul-Henning Kamp */ 2438e9c0cc15SPoul-Henning Kamp error = swaponvp(td, vp, NODEV, attr.va_size / DEV_BSIZE); 2439e9c0cc15SPoul-Henning Kamp } 2440e9c0cc15SPoul-Henning Kamp 2441e9c0cc15SPoul-Henning Kamp if (error) 2442e9c0cc15SPoul-Henning Kamp vrele(vp); 2443e9c0cc15SPoul-Henning Kamp done: 2444e9c0cc15SPoul-Henning Kamp swdev_syscall_active = 0; 2445e9c0cc15SPoul-Henning Kamp wakeup_one(&swdev_syscall_active); 2446e9c0cc15SPoul-Henning Kamp done2: 2447e9c0cc15SPoul-Henning Kamp mtx_unlock(&Giant); 2448e9c0cc15SPoul-Henning Kamp return (error); 2449e9c0cc15SPoul-Henning Kamp } 2450e9c0cc15SPoul-Henning Kamp 2451e9c0cc15SPoul-Henning Kamp /* 2452e9c0cc15SPoul-Henning Kamp * Swfree(index) frees the index'th portion of the swap map. 2453e9c0cc15SPoul-Henning Kamp * Each of the nswdev devices provides 1/nswdev'th of the swap 2454e9c0cc15SPoul-Henning Kamp * space, which is laid out with blocks of dmmax pages circularly 2455e9c0cc15SPoul-Henning Kamp * among the devices. 2456e9c0cc15SPoul-Henning Kamp * 2457e9c0cc15SPoul-Henning Kamp * The new swap code uses page-sized blocks. The old swap code used 2458e9c0cc15SPoul-Henning Kamp * DEV_BSIZE'd chunks. 2459e9c0cc15SPoul-Henning Kamp */ 2460e9c0cc15SPoul-Henning Kamp int 2461e9c0cc15SPoul-Henning Kamp swaponvp(td, vp, dev, nblks) 2462e9c0cc15SPoul-Henning Kamp struct thread *td; 2463e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2464e9c0cc15SPoul-Henning Kamp dev_t dev; 2465e9c0cc15SPoul-Henning Kamp u_long nblks; 2466e9c0cc15SPoul-Henning Kamp { 2467e9c0cc15SPoul-Henning Kamp int index; 2468e9c0cc15SPoul-Henning Kamp struct swdevt *sp; 2469e9c0cc15SPoul-Henning Kamp swblk_t vsbase; 2470e9c0cc15SPoul-Henning Kamp long blk; 2471e9c0cc15SPoul-Henning Kamp swblk_t dvbase; 2472e9c0cc15SPoul-Henning Kamp int error; 2473e9c0cc15SPoul-Henning Kamp u_long aligned_nblks; 2474e9c0cc15SPoul-Henning Kamp off_t mediasize; 2475e9c0cc15SPoul-Henning Kamp 2476e9c0cc15SPoul-Henning Kamp if (!swapdev_vp) { 2477e9c0cc15SPoul-Henning Kamp error = getnewvnode("none", NULL, swapdev_vnodeop_p, 2478e9c0cc15SPoul-Henning Kamp &swapdev_vp); 2479e9c0cc15SPoul-Henning Kamp if (error) 2480e9c0cc15SPoul-Henning Kamp panic("Cannot get vnode for swapdev"); 2481e9c0cc15SPoul-Henning Kamp swapdev_vp->v_type = VNON; /* Untyped */ 2482e9c0cc15SPoul-Henning Kamp } 2483e9c0cc15SPoul-Henning Kamp 2484e9c0cc15SPoul-Henning Kamp ASSERT_VOP_UNLOCKED(vp, "swaponvp"); 2485e9c0cc15SPoul-Henning Kamp for (sp = swdevt, index = 0 ; index < nswdev; index++, sp++) { 2486e9c0cc15SPoul-Henning Kamp if (sp->sw_vp == vp) 2487e9c0cc15SPoul-Henning Kamp return EBUSY; 2488e9c0cc15SPoul-Henning Kamp if (!sp->sw_vp) 2489e9c0cc15SPoul-Henning Kamp goto found; 2490e9c0cc15SPoul-Henning Kamp 2491e9c0cc15SPoul-Henning Kamp } 2492e9c0cc15SPoul-Henning Kamp return EINVAL; 2493e9c0cc15SPoul-Henning Kamp found: 2494e9c0cc15SPoul-Henning Kamp (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2495e9c0cc15SPoul-Henning Kamp #ifdef MAC 2496e9c0cc15SPoul-Henning Kamp error = mac_check_system_swapon(td->td_ucred, vp); 2497e9c0cc15SPoul-Henning Kamp if (error == 0) 2498e9c0cc15SPoul-Henning Kamp #endif 2499e9c0cc15SPoul-Henning Kamp error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td); 2500e9c0cc15SPoul-Henning Kamp (void) VOP_UNLOCK(vp, 0, td); 2501e9c0cc15SPoul-Henning Kamp if (error) 2502e9c0cc15SPoul-Henning Kamp return (error); 2503e9c0cc15SPoul-Henning Kamp 2504e9c0cc15SPoul-Henning Kamp if (nblks == 0) { 2505e9c0cc15SPoul-Henning Kamp error = VOP_IOCTL(vp, DIOCGMEDIASIZE, (caddr_t)&mediasize, 2506e9c0cc15SPoul-Henning Kamp FREAD, td->td_ucred, td); 2507e9c0cc15SPoul-Henning Kamp if (error == 0) 2508e9c0cc15SPoul-Henning Kamp nblks = mediasize / DEV_BSIZE; 2509e9c0cc15SPoul-Henning Kamp } 2510e9c0cc15SPoul-Henning Kamp /* 2511e9c0cc15SPoul-Henning Kamp * XXX: We should also check that the sectorsize makes sense 2512e9c0cc15SPoul-Henning Kamp * XXX: it should be a power of two, no larger than the page size. 2513e9c0cc15SPoul-Henning Kamp */ 2514e9c0cc15SPoul-Henning Kamp if (nblks == 0) { 2515e9c0cc15SPoul-Henning Kamp (void) VOP_CLOSE(vp, FREAD | FWRITE, td->td_ucred, td); 2516e9c0cc15SPoul-Henning Kamp return (ENXIO); 2517e9c0cc15SPoul-Henning Kamp } 2518e9c0cc15SPoul-Henning Kamp 2519e9c0cc15SPoul-Henning Kamp /* 2520e9c0cc15SPoul-Henning Kamp * If we go beyond this, we get overflows in the radix 2521e9c0cc15SPoul-Henning Kamp * tree bitmap code. 2522e9c0cc15SPoul-Henning Kamp */ 2523e9c0cc15SPoul-Henning Kamp if (nblks > 0x40000000 / BLIST_META_RADIX / nswdev) { 2524e9c0cc15SPoul-Henning Kamp printf("exceeded maximum of %d blocks per swap unit\n", 2525e9c0cc15SPoul-Henning Kamp 0x40000000 / BLIST_META_RADIX / nswdev); 2526e9c0cc15SPoul-Henning Kamp (void) VOP_CLOSE(vp, FREAD | FWRITE, td->td_ucred, td); 2527e9c0cc15SPoul-Henning Kamp return (ENXIO); 2528e9c0cc15SPoul-Henning Kamp } 2529e9c0cc15SPoul-Henning Kamp /* 2530e9c0cc15SPoul-Henning Kamp * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 2531e9c0cc15SPoul-Henning Kamp * First chop nblks off to page-align it, then convert. 2532e9c0cc15SPoul-Henning Kamp * 2533e9c0cc15SPoul-Henning Kamp * sw->sw_nblks is in page-sized chunks now too. 2534e9c0cc15SPoul-Henning Kamp */ 2535e9c0cc15SPoul-Henning Kamp nblks &= ~(ctodb(1) - 1); 2536e9c0cc15SPoul-Henning Kamp nblks = dbtoc(nblks); 2537e9c0cc15SPoul-Henning Kamp 2538e9c0cc15SPoul-Henning Kamp sp->sw_vp = vp; 2539e9c0cc15SPoul-Henning Kamp sp->sw_dev = dev2udev(dev); 2540e9c0cc15SPoul-Henning Kamp sp->sw_device = dev; 2541e9c0cc15SPoul-Henning Kamp sp->sw_flags = SW_FREED; 2542e9c0cc15SPoul-Henning Kamp sp->sw_nblks = nblks; 2543e9c0cc15SPoul-Henning Kamp sp->sw_used = 0; 2544e9c0cc15SPoul-Henning Kamp 2545e9c0cc15SPoul-Henning Kamp /* 2546e9c0cc15SPoul-Henning Kamp * nblks, nswap, and dmmax are PAGE_SIZE'd parameters now, not 2547e9c0cc15SPoul-Henning Kamp * DEV_BSIZE'd. aligned_nblks is used to calculate the 2548e9c0cc15SPoul-Henning Kamp * size of the swap bitmap, taking into account the stripe size. 2549e9c0cc15SPoul-Henning Kamp */ 2550e9c0cc15SPoul-Henning Kamp aligned_nblks = (nblks + (dmmax -1)) & ~(u_long)(dmmax -1); 2551e9c0cc15SPoul-Henning Kamp 2552e9c0cc15SPoul-Henning Kamp if (aligned_nblks * nswdev > nswap) 2553e9c0cc15SPoul-Henning Kamp nswap = aligned_nblks * nswdev; 2554e9c0cc15SPoul-Henning Kamp 2555e9c0cc15SPoul-Henning Kamp if (swapblist == NULL) 2556e9c0cc15SPoul-Henning Kamp swapblist = blist_create(nswap); 2557e9c0cc15SPoul-Henning Kamp else 2558e9c0cc15SPoul-Henning Kamp blist_resize(&swapblist, nswap, 0); 2559e9c0cc15SPoul-Henning Kamp 2560e9c0cc15SPoul-Henning Kamp for (dvbase = dmmax; dvbase < nblks; dvbase += dmmax) { 2561e9c0cc15SPoul-Henning Kamp blk = min(nblks - dvbase, dmmax); 2562e9c0cc15SPoul-Henning Kamp vsbase = index * dmmax + dvbase * nswdev; 2563e9c0cc15SPoul-Henning Kamp blist_free(swapblist, vsbase, blk); 2564e9c0cc15SPoul-Henning Kamp vm_swap_size += blk; 2565e9c0cc15SPoul-Henning Kamp } 2566e9c0cc15SPoul-Henning Kamp 2567e9c0cc15SPoul-Henning Kamp swap_pager_full = 0; 2568e9c0cc15SPoul-Henning Kamp 2569e9c0cc15SPoul-Henning Kamp return (0); 2570e9c0cc15SPoul-Henning Kamp } 2571e9c0cc15SPoul-Henning Kamp 2572e9c0cc15SPoul-Henning Kamp /* 2573e9c0cc15SPoul-Henning Kamp * SYSCALL: swapoff(devname) 2574e9c0cc15SPoul-Henning Kamp * 2575e9c0cc15SPoul-Henning Kamp * Disable swapping on the given device. 2576e9c0cc15SPoul-Henning Kamp */ 2577e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2578e9c0cc15SPoul-Henning Kamp struct swapoff_args { 2579e9c0cc15SPoul-Henning Kamp char *name; 2580e9c0cc15SPoul-Henning Kamp }; 2581e9c0cc15SPoul-Henning Kamp #endif 2582e9c0cc15SPoul-Henning Kamp 2583e9c0cc15SPoul-Henning Kamp /* 2584e9c0cc15SPoul-Henning Kamp * MPSAFE 2585e9c0cc15SPoul-Henning Kamp */ 2586e9c0cc15SPoul-Henning Kamp /* ARGSUSED */ 2587e9c0cc15SPoul-Henning Kamp int 2588e9c0cc15SPoul-Henning Kamp swapoff(td, uap) 2589e9c0cc15SPoul-Henning Kamp struct thread *td; 2590e9c0cc15SPoul-Henning Kamp struct swapoff_args *uap; 2591e9c0cc15SPoul-Henning Kamp { 2592e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2593e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2594e9c0cc15SPoul-Henning Kamp struct swdevt *sp; 2595e9c0cc15SPoul-Henning Kamp swblk_t dvbase, vsbase; 2596e9c0cc15SPoul-Henning Kamp u_long nblks, aligned_nblks, blk; 2597e9c0cc15SPoul-Henning Kamp int error, index; 2598e9c0cc15SPoul-Henning Kamp 2599e9c0cc15SPoul-Henning Kamp mtx_lock(&Giant); 2600e9c0cc15SPoul-Henning Kamp 2601e9c0cc15SPoul-Henning Kamp error = suser(td); 2602e9c0cc15SPoul-Henning Kamp if (error) 2603e9c0cc15SPoul-Henning Kamp goto done2; 2604e9c0cc15SPoul-Henning Kamp 2605e9c0cc15SPoul-Henning Kamp while (swdev_syscall_active) 2606e9c0cc15SPoul-Henning Kamp tsleep(&swdev_syscall_active, PUSER - 1, "swpoff", 0); 2607e9c0cc15SPoul-Henning Kamp swdev_syscall_active = 1; 2608e9c0cc15SPoul-Henning Kamp 2609e9c0cc15SPoul-Henning Kamp NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->name, td); 2610e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2611e9c0cc15SPoul-Henning Kamp if (error) 2612e9c0cc15SPoul-Henning Kamp goto done; 2613e9c0cc15SPoul-Henning Kamp NDFREE(&nd, NDF_ONLY_PNBUF); 2614e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2615e9c0cc15SPoul-Henning Kamp 2616e9c0cc15SPoul-Henning Kamp for (sp = swdevt, index = 0 ; index < nswdev; index++, sp++) { 2617e9c0cc15SPoul-Henning Kamp if (sp->sw_vp == vp) 2618e9c0cc15SPoul-Henning Kamp goto found; 2619e9c0cc15SPoul-Henning Kamp } 2620e9c0cc15SPoul-Henning Kamp error = EINVAL; 2621e9c0cc15SPoul-Henning Kamp goto done; 2622e9c0cc15SPoul-Henning Kamp found: 2623e9c0cc15SPoul-Henning Kamp #ifdef MAC 2624e9c0cc15SPoul-Henning Kamp (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 2625e9c0cc15SPoul-Henning Kamp error = mac_check_system_swapoff(td->td_ucred, vp); 2626e9c0cc15SPoul-Henning Kamp (void) VOP_UNLOCK(vp, 0, td); 2627e9c0cc15SPoul-Henning Kamp if (error != 0) 2628e9c0cc15SPoul-Henning Kamp goto done; 2629e9c0cc15SPoul-Henning Kamp #endif 2630e9c0cc15SPoul-Henning Kamp 2631e9c0cc15SPoul-Henning Kamp nblks = sp->sw_nblks; 2632e9c0cc15SPoul-Henning Kamp 2633e9c0cc15SPoul-Henning Kamp /* 2634e9c0cc15SPoul-Henning Kamp * We can turn off this swap device safely only if the 2635e9c0cc15SPoul-Henning Kamp * available virtual memory in the system will fit the amount 2636e9c0cc15SPoul-Henning Kamp * of data we will have to page back in, plus an epsilon so 2637e9c0cc15SPoul-Henning Kamp * the system doesn't become critically low on swap space. 2638e9c0cc15SPoul-Henning Kamp */ 2639e9c0cc15SPoul-Henning Kamp if (cnt.v_free_count + cnt.v_cache_count + vm_swap_size < 2640e9c0cc15SPoul-Henning Kamp nblks + nswap_lowat) { 2641e9c0cc15SPoul-Henning Kamp error = ENOMEM; 2642e9c0cc15SPoul-Henning Kamp goto done; 2643e9c0cc15SPoul-Henning Kamp } 2644e9c0cc15SPoul-Henning Kamp 2645e9c0cc15SPoul-Henning Kamp /* 2646e9c0cc15SPoul-Henning Kamp * Prevent further allocations on this device. 2647e9c0cc15SPoul-Henning Kamp */ 2648e9c0cc15SPoul-Henning Kamp sp->sw_flags |= SW_CLOSING; 2649e9c0cc15SPoul-Henning Kamp for (dvbase = dmmax; dvbase < nblks; dvbase += dmmax) { 2650e9c0cc15SPoul-Henning Kamp blk = min(nblks - dvbase, dmmax); 2651e9c0cc15SPoul-Henning Kamp vsbase = index * dmmax + dvbase * nswdev; 2652e9c0cc15SPoul-Henning Kamp vm_swap_size -= blist_fill(swapblist, vsbase, blk); 2653e9c0cc15SPoul-Henning Kamp } 2654e9c0cc15SPoul-Henning Kamp 2655e9c0cc15SPoul-Henning Kamp /* 2656e9c0cc15SPoul-Henning Kamp * Page in the contents of the device and close it. 2657e9c0cc15SPoul-Henning Kamp */ 2658e9c0cc15SPoul-Henning Kamp #ifndef NO_SWAPPING 2659e9c0cc15SPoul-Henning Kamp vm_proc_swapin_all(index); 2660e9c0cc15SPoul-Henning Kamp #endif /* !NO_SWAPPING */ 2661e9c0cc15SPoul-Henning Kamp swap_pager_swapoff(index, &sp->sw_used); 2662e9c0cc15SPoul-Henning Kamp 2663e9c0cc15SPoul-Henning Kamp VOP_CLOSE(vp, FREAD | FWRITE, td->td_ucred, td); 2664e9c0cc15SPoul-Henning Kamp vrele(vp); 2665e9c0cc15SPoul-Henning Kamp sp->sw_vp = NULL; 2666e9c0cc15SPoul-Henning Kamp 2667e9c0cc15SPoul-Henning Kamp /* 2668e9c0cc15SPoul-Henning Kamp * Resize the bitmap based on the new largest swap device, 2669e9c0cc15SPoul-Henning Kamp * or free the bitmap if there are no more devices. 2670e9c0cc15SPoul-Henning Kamp */ 2671e9c0cc15SPoul-Henning Kamp for (sp = swdevt, nblks = 0; sp < swdevt + nswdev; sp++) { 2672e9c0cc15SPoul-Henning Kamp if (sp->sw_vp == NULL) 2673e9c0cc15SPoul-Henning Kamp continue; 2674e9c0cc15SPoul-Henning Kamp nblks = max(nblks, sp->sw_nblks); 2675e9c0cc15SPoul-Henning Kamp } 2676e9c0cc15SPoul-Henning Kamp 2677e9c0cc15SPoul-Henning Kamp aligned_nblks = (nblks + (dmmax -1)) & ~(u_long)(dmmax -1); 2678e9c0cc15SPoul-Henning Kamp nswap = aligned_nblks * nswdev; 2679e9c0cc15SPoul-Henning Kamp 2680e9c0cc15SPoul-Henning Kamp if (nswap == 0) { 2681e9c0cc15SPoul-Henning Kamp blist_destroy(swapblist); 2682e9c0cc15SPoul-Henning Kamp swapblist = NULL; 2683e9c0cc15SPoul-Henning Kamp vrele(swapdev_vp); 2684e9c0cc15SPoul-Henning Kamp swapdev_vp = NULL; 2685e9c0cc15SPoul-Henning Kamp } else 2686e9c0cc15SPoul-Henning Kamp blist_resize(&swapblist, nswap, 0); 2687e9c0cc15SPoul-Henning Kamp 2688e9c0cc15SPoul-Henning Kamp done: 2689e9c0cc15SPoul-Henning Kamp swdev_syscall_active = 0; 2690e9c0cc15SPoul-Henning Kamp wakeup_one(&swdev_syscall_active); 2691e9c0cc15SPoul-Henning Kamp done2: 2692e9c0cc15SPoul-Henning Kamp mtx_unlock(&Giant); 2693e9c0cc15SPoul-Henning Kamp return (error); 2694e9c0cc15SPoul-Henning Kamp } 2695e9c0cc15SPoul-Henning Kamp 2696567104a1SPoul-Henning Kamp void 2697567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used) 2698567104a1SPoul-Henning Kamp { 2699567104a1SPoul-Henning Kamp struct swdevt *sp; 2700567104a1SPoul-Henning Kamp int i; 2701567104a1SPoul-Henning Kamp 2702567104a1SPoul-Henning Kamp *total = 0; 2703567104a1SPoul-Henning Kamp *used = 0; 2704567104a1SPoul-Henning Kamp for (sp = swdevt, i = 0; i < nswdev; i++, sp++) { 2705567104a1SPoul-Henning Kamp if (sp->sw_vp == NULL) 2706567104a1SPoul-Henning Kamp continue; 2707567104a1SPoul-Henning Kamp *total += sp->sw_nblks; 2708567104a1SPoul-Henning Kamp *used += sp->sw_used; 2709567104a1SPoul-Henning Kamp } 2710567104a1SPoul-Henning Kamp } 2711567104a1SPoul-Henning Kamp 2712e9c0cc15SPoul-Henning Kamp static int 2713e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 2714e9c0cc15SPoul-Henning Kamp { 2715e9c0cc15SPoul-Henning Kamp int *name = (int *)arg1; 2716e9c0cc15SPoul-Henning Kamp int error, i, n; 2717e9c0cc15SPoul-Henning Kamp struct xswdev xs; 2718e9c0cc15SPoul-Henning Kamp struct swdevt *sp; 2719e9c0cc15SPoul-Henning Kamp 2720e9c0cc15SPoul-Henning Kamp if (arg2 != 1) /* name length */ 2721e9c0cc15SPoul-Henning Kamp return (EINVAL); 2722e9c0cc15SPoul-Henning Kamp 2723e9c0cc15SPoul-Henning Kamp for (sp = swdevt, i = 0, n = 0 ; i < nswdev; i++, sp++) { 2724e9c0cc15SPoul-Henning Kamp if (sp->sw_vp) { 2725e9c0cc15SPoul-Henning Kamp if (n == *name) { 2726e9c0cc15SPoul-Henning Kamp xs.xsw_version = XSWDEV_VERSION; 2727e9c0cc15SPoul-Henning Kamp xs.xsw_dev = sp->sw_dev; 2728e9c0cc15SPoul-Henning Kamp xs.xsw_flags = sp->sw_flags; 2729e9c0cc15SPoul-Henning Kamp xs.xsw_nblks = sp->sw_nblks; 2730e9c0cc15SPoul-Henning Kamp xs.xsw_used = sp->sw_used; 2731e9c0cc15SPoul-Henning Kamp 2732e9c0cc15SPoul-Henning Kamp error = SYSCTL_OUT(req, &xs, sizeof(xs)); 2733e9c0cc15SPoul-Henning Kamp return (error); 2734e9c0cc15SPoul-Henning Kamp } 2735e9c0cc15SPoul-Henning Kamp n++; 2736e9c0cc15SPoul-Henning Kamp } 2737e9c0cc15SPoul-Henning Kamp 2738e9c0cc15SPoul-Henning Kamp } 2739e9c0cc15SPoul-Henning Kamp return (ENOENT); 2740e9c0cc15SPoul-Henning Kamp } 2741e9c0cc15SPoul-Henning Kamp 2742e9c0cc15SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswdev, 0, 2743e9c0cc15SPoul-Henning Kamp "Number of swap devices"); 2744e9c0cc15SPoul-Henning Kamp SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD, sysctl_vm_swap_info, 2745e9c0cc15SPoul-Henning Kamp "Swap statistics by device"); 2746ec38b344SPoul-Henning Kamp 2747ec38b344SPoul-Henning Kamp /* 2748ec38b344SPoul-Henning Kamp * vmspace_swap_count() - count the approximate swap useage in pages for a 2749ec38b344SPoul-Henning Kamp * vmspace. 2750ec38b344SPoul-Henning Kamp * 2751ec38b344SPoul-Henning Kamp * The map must be locked. 2752ec38b344SPoul-Henning Kamp * 2753ec38b344SPoul-Henning Kamp * Swap useage is determined by taking the proportional swap used by 2754ec38b344SPoul-Henning Kamp * VM objects backing the VM map. To make up for fractional losses, 2755ec38b344SPoul-Henning Kamp * if the VM object has any swap use at all the associated map entries 2756ec38b344SPoul-Henning Kamp * count for at least 1 swap page. 2757ec38b344SPoul-Henning Kamp */ 2758ec38b344SPoul-Henning Kamp int 2759ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace) 2760ec38b344SPoul-Henning Kamp { 2761ec38b344SPoul-Henning Kamp vm_map_t map = &vmspace->vm_map; 2762ec38b344SPoul-Henning Kamp vm_map_entry_t cur; 2763ec38b344SPoul-Henning Kamp int count = 0; 2764ec38b344SPoul-Henning Kamp 2765ec38b344SPoul-Henning Kamp for (cur = map->header.next; cur != &map->header; cur = cur->next) { 2766ec38b344SPoul-Henning Kamp vm_object_t object; 2767ec38b344SPoul-Henning Kamp 2768ec38b344SPoul-Henning Kamp if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 && 2769ec38b344SPoul-Henning Kamp (object = cur->object.vm_object) != NULL) { 2770ec38b344SPoul-Henning Kamp VM_OBJECT_LOCK(object); 2771ec38b344SPoul-Henning Kamp if (object->type == OBJT_SWAP && 2772ec38b344SPoul-Henning Kamp object->un_pager.swp.swp_bcount != 0) { 2773ec38b344SPoul-Henning Kamp int n = (cur->end - cur->start) / PAGE_SIZE; 2774ec38b344SPoul-Henning Kamp 2775ec38b344SPoul-Henning Kamp count += object->un_pager.swp.swp_bcount * 2776ec38b344SPoul-Henning Kamp SWAP_META_PAGES * n / object->size + 1; 2777ec38b344SPoul-Henning Kamp } 2778ec38b344SPoul-Henning Kamp VM_OBJECT_UNLOCK(object); 2779ec38b344SPoul-Henning Kamp } 2780ec38b344SPoul-Henning Kamp } 2781ec38b344SPoul-Henning Kamp return (count); 2782ec38b344SPoul-Henning Kamp } 2783ec38b344SPoul-Henning Kamp 2784