145749Smckusick /* 245749Smckusick * Copyright (c) 1990 University of Utah. 3*63379Sbostic * Copyright (c) 1991, 1993 4*63379Sbostic * The Regents of the University of California. All rights reserved. 545749Smckusick * 645749Smckusick * This code is derived from software contributed to Berkeley by 745749Smckusick * the Systems Programming Group of the University of Utah Computer 845749Smckusick * Science Department. 945749Smckusick * 1045749Smckusick * %sccs.include.redist.c% 1145749Smckusick * 1249289Shibler * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$ 1349289Shibler * 14*63379Sbostic * @(#)swap_pager.c 8.1 (Berkeley) 06/11/93 1545749Smckusick */ 1645749Smckusick 1745749Smckusick /* 1845749Smckusick * Quick hack to page to dedicated partition(s). 1945749Smckusick * TODO: 2045749Smckusick * Add multiprocessor locks 2145749Smckusick * Deal with async writes in a better fashion 2245749Smckusick */ 2345749Smckusick 2453341Sbostic #include <sys/param.h> 2553341Sbostic #include <sys/systm.h> 2653341Sbostic #include <sys/proc.h> 2753341Sbostic #include <sys/buf.h> 2853341Sbostic #include <sys/map.h> 2953496Sheideman #include <sys/vnode.h> 3053341Sbostic #include <sys/malloc.h> 3145749Smckusick 3255051Spendry #include <miscfs/specfs/specdev.h> 3355051Spendry 3453341Sbostic #include <vm/vm.h> 3553341Sbostic #include <vm/vm_page.h> 3653341Sbostic #include <vm/vm_pageout.h> 3753341Sbostic #include <vm/swap_pager.h> 3845749Smckusick 3945749Smckusick #define NSWSIZES 16 /* size of swtab */ 4045749Smckusick #define NPENDINGIO 64 /* max # of pending cleans */ 4145749Smckusick #define MAXDADDRS 64 /* max # of disk addrs for fixed allocations */ 4245749Smckusick 4345749Smckusick #ifdef DEBUG 4445749Smckusick int swpagerdebug = 0x100; 4545749Smckusick #define SDB_FOLLOW 0x001 4645749Smckusick #define SDB_INIT 0x002 4745749Smckusick #define SDB_ALLOC 0x004 4845749Smckusick #define SDB_IO 0x008 4945749Smckusick #define SDB_WRITE 0x010 5045749Smckusick #define SDB_FAIL 0x020 5145749Smckusick #define SDB_ALLOCBLK 0x040 5245749Smckusick #define SDB_FULL 0x080 5345749Smckusick #define SDB_ANOM 0x100 5445749Smckusick #define SDB_ANOMPANIC 0x200 5545749Smckusick #endif 5645749Smckusick 5745749Smckusick struct swpagerclean { 5845749Smckusick queue_head_t spc_list; 5945749Smckusick int spc_flags; 6045749Smckusick struct buf *spc_bp; 6145749Smckusick sw_pager_t spc_swp; 6245749Smckusick vm_offset_t spc_kva; 6345749Smckusick vm_page_t spc_m; 6445749Smckusick } swcleanlist[NPENDINGIO]; 6553341Sbostic typedef struct swpagerclean *swp_clean_t; 6645749Smckusick 6753341Sbostic 6845749Smckusick /* spc_flags values */ 6945749Smckusick #define SPC_FREE 0x00 7045749Smckusick #define SPC_BUSY 0x01 7145749Smckusick #define SPC_DONE 0x02 7245749Smckusick #define SPC_ERROR 0x04 7345749Smckusick #define SPC_DIRTY 0x08 7445749Smckusick 7545749Smckusick struct swtab { 7645749Smckusick vm_size_t st_osize; /* size of object (bytes) */ 7745749Smckusick int st_bsize; /* vs. size of swap block (DEV_BSIZE units) */ 7845749Smckusick #ifdef DEBUG 7945749Smckusick u_long st_inuse; /* number in this range in use */ 8045749Smckusick u_long st_usecnt; /* total used of this size */ 8145749Smckusick #endif 8245749Smckusick } swtab[NSWSIZES+1]; 8345749Smckusick 8445749Smckusick #ifdef DEBUG 8545749Smckusick int swap_pager_pendingio; /* max pending async "clean" ops */ 8645749Smckusick int swap_pager_poip; /* pageouts in progress */ 8745749Smckusick int swap_pager_piip; /* pageins in progress */ 8845749Smckusick #endif 8945749Smckusick 9045749Smckusick queue_head_t swap_pager_inuse; /* list of pending page cleans */ 9145749Smckusick queue_head_t swap_pager_free; /* list of free pager clean structs */ 9245749Smckusick queue_head_t swap_pager_list; /* list of "named" anon regions */ 9345749Smckusick 9453341Sbostic static int swap_pager_finish __P((swp_clean_t)); 9553341Sbostic static void swap_pager_init __P((void)); 9653341Sbostic static vm_pager_t swap_pager_alloc __P((caddr_t, vm_size_t, vm_prot_t)); 9753341Sbostic static boolean_t swap_pager_clean __P((vm_page_t, int)); 9853341Sbostic static void swap_pager_dealloc __P((vm_pager_t)); 9953341Sbostic static int swap_pager_getpage 10053341Sbostic __P((vm_pager_t, vm_page_t, boolean_t)); 10153341Sbostic static boolean_t swap_pager_haspage __P((vm_pager_t, vm_offset_t)); 10253341Sbostic static int swap_pager_io __P((sw_pager_t, vm_page_t, int)); 10353341Sbostic static void swap_pager_iodone __P((struct buf *)); 10453341Sbostic static int swap_pager_putpage 10553341Sbostic __P((vm_pager_t, vm_page_t, boolean_t)); 10653341Sbostic 10753341Sbostic struct pagerops swappagerops = { 10853341Sbostic swap_pager_init, 10953341Sbostic swap_pager_alloc, 11053341Sbostic swap_pager_dealloc, 11153341Sbostic swap_pager_getpage, 11253341Sbostic swap_pager_putpage, 11353341Sbostic swap_pager_haspage 11453341Sbostic }; 11553341Sbostic 11653341Sbostic static void 11745749Smckusick swap_pager_init() 11845749Smckusick { 11945749Smckusick register swp_clean_t spc; 12045749Smckusick register int i, bsize; 12145749Smckusick extern int dmmin, dmmax; 12245749Smckusick int maxbsize; 12345749Smckusick 12445749Smckusick #ifdef DEBUG 12545749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_INIT)) 12645749Smckusick printf("swpg_init()\n"); 12745749Smckusick #endif 12845749Smckusick dfltpagerops = &swappagerops; 12945749Smckusick queue_init(&swap_pager_list); 13045749Smckusick 13145749Smckusick /* 13245749Smckusick * Initialize clean lists 13345749Smckusick */ 13445749Smckusick queue_init(&swap_pager_inuse); 13545749Smckusick queue_init(&swap_pager_free); 13645749Smckusick for (i = 0, spc = swcleanlist; i < NPENDINGIO; i++, spc++) { 13745749Smckusick queue_enter(&swap_pager_free, spc, swp_clean_t, spc_list); 13845749Smckusick spc->spc_flags = SPC_FREE; 13945749Smckusick } 14045749Smckusick 14145749Smckusick /* 14245749Smckusick * Calculate the swap allocation constants. 14345749Smckusick */ 14445749Smckusick if (dmmin == 0) { 14545749Smckusick dmmin = DMMIN; 14645749Smckusick if (dmmin < CLBYTES/DEV_BSIZE) 14745749Smckusick dmmin = CLBYTES/DEV_BSIZE; 14845749Smckusick } 14945749Smckusick if (dmmax == 0) 15045749Smckusick dmmax = DMMAX; 15145749Smckusick 15245749Smckusick /* 15345749Smckusick * Fill in our table of object size vs. allocation size 15445749Smckusick */ 15545749Smckusick bsize = btodb(PAGE_SIZE); 15645749Smckusick if (bsize < dmmin) 15745749Smckusick bsize = dmmin; 15845749Smckusick maxbsize = btodb(sizeof(sw_bm_t) * NBBY * PAGE_SIZE); 15945749Smckusick if (maxbsize > dmmax) 16045749Smckusick maxbsize = dmmax; 16145749Smckusick for (i = 0; i < NSWSIZES; i++) { 16245749Smckusick swtab[i].st_osize = (vm_size_t) (MAXDADDRS * dbtob(bsize)); 16345749Smckusick swtab[i].st_bsize = bsize; 16445749Smckusick #ifdef DEBUG 16545749Smckusick if (swpagerdebug & SDB_INIT) 16645749Smckusick printf("swpg_init: ix %d, size %x, bsize %x\n", 16745749Smckusick i, swtab[i].st_osize, swtab[i].st_bsize); 16845749Smckusick #endif 16945749Smckusick if (bsize >= maxbsize) 17045749Smckusick break; 17145749Smckusick bsize *= 2; 17245749Smckusick } 17345749Smckusick swtab[i].st_osize = 0; 17445749Smckusick swtab[i].st_bsize = bsize; 17545749Smckusick } 17645749Smckusick 17745749Smckusick /* 17845749Smckusick * Allocate a pager structure and associated resources. 17945749Smckusick * Note that if we are called from the pageout daemon (handle == NULL) 18045749Smckusick * we should not wait for memory as it could resulting in deadlock. 18145749Smckusick */ 18253341Sbostic static vm_pager_t 18345749Smckusick swap_pager_alloc(handle, size, prot) 18445749Smckusick caddr_t handle; 18545749Smckusick register vm_size_t size; 18645749Smckusick vm_prot_t prot; 18745749Smckusick { 18845749Smckusick register vm_pager_t pager; 18945749Smckusick register sw_pager_t swp; 19045749Smckusick struct swtab *swt; 19145749Smckusick int waitok; 19245749Smckusick 19345749Smckusick #ifdef DEBUG 19445749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOC)) 19545749Smckusick printf("swpg_alloc(%x, %x, %x)\n", handle, size, prot); 19645749Smckusick #endif 19745749Smckusick /* 19845749Smckusick * If this is a "named" anonymous region, look it up and 19945749Smckusick * return the appropriate pager if it exists. 20045749Smckusick */ 20145749Smckusick if (handle) { 20245749Smckusick pager = vm_pager_lookup(&swap_pager_list, handle); 20348386Skarels if (pager != NULL) { 20445749Smckusick /* 20545749Smckusick * Use vm_object_lookup to gain a reference 20645749Smckusick * to the object and also to remove from the 20745749Smckusick * object cache. 20845749Smckusick */ 20948386Skarels if (vm_object_lookup(pager) == NULL) 21045749Smckusick panic("swap_pager_alloc: bad object"); 21145749Smckusick return(pager); 21245749Smckusick } 21345749Smckusick } 21445749Smckusick /* 21545749Smckusick * Pager doesn't exist, allocate swap management resources 21645749Smckusick * and initialize. 21745749Smckusick */ 21845749Smckusick waitok = handle ? M_WAITOK : M_NOWAIT; 21945749Smckusick pager = (vm_pager_t)malloc(sizeof *pager, M_VMPAGER, waitok); 22048386Skarels if (pager == NULL) 22148386Skarels return(NULL); 22245749Smckusick swp = (sw_pager_t)malloc(sizeof *swp, M_VMPGDATA, waitok); 22345749Smckusick if (swp == NULL) { 22445749Smckusick #ifdef DEBUG 22545749Smckusick if (swpagerdebug & SDB_FAIL) 22645749Smckusick printf("swpg_alloc: swpager malloc failed\n"); 22745749Smckusick #endif 22845749Smckusick free((caddr_t)pager, M_VMPAGER); 22948386Skarels return(NULL); 23045749Smckusick } 23145749Smckusick size = round_page(size); 23245749Smckusick for (swt = swtab; swt->st_osize; swt++) 23345749Smckusick if (size <= swt->st_osize) 23445749Smckusick break; 23545749Smckusick #ifdef DEBUG 23645749Smckusick swt->st_inuse++; 23745749Smckusick swt->st_usecnt++; 23845749Smckusick #endif 23945749Smckusick swp->sw_osize = size; 24045749Smckusick swp->sw_bsize = swt->st_bsize; 24145749Smckusick swp->sw_nblocks = (btodb(size) + swp->sw_bsize - 1) / swp->sw_bsize; 24245749Smckusick swp->sw_blocks = (sw_blk_t) 24345749Smckusick malloc(swp->sw_nblocks*sizeof(*swp->sw_blocks), 24445749Smckusick M_VMPGDATA, M_NOWAIT); 24545749Smckusick if (swp->sw_blocks == NULL) { 24645749Smckusick free((caddr_t)swp, M_VMPGDATA); 24745749Smckusick free((caddr_t)pager, M_VMPAGER); 24845749Smckusick #ifdef DEBUG 24945749Smckusick if (swpagerdebug & SDB_FAIL) 25045749Smckusick printf("swpg_alloc: sw_blocks malloc failed\n"); 25145749Smckusick swt->st_inuse--; 25245749Smckusick swt->st_usecnt--; 25345749Smckusick #endif 25445749Smckusick return(FALSE); 25545749Smckusick } 25645749Smckusick bzero((caddr_t)swp->sw_blocks, 25745749Smckusick swp->sw_nblocks * sizeof(*swp->sw_blocks)); 25845749Smckusick swp->sw_poip = 0; 25945749Smckusick if (handle) { 26045749Smckusick vm_object_t object; 26145749Smckusick 26245749Smckusick swp->sw_flags = SW_NAMED; 26345749Smckusick queue_enter(&swap_pager_list, pager, vm_pager_t, pg_list); 26445749Smckusick /* 26545749Smckusick * Consistant with other pagers: return with object 26645749Smckusick * referenced. Can't do this with handle == NULL 26745749Smckusick * since it might be the pageout daemon calling. 26845749Smckusick */ 26945749Smckusick object = vm_object_allocate(size); 27045749Smckusick vm_object_enter(object, pager); 27145749Smckusick vm_object_setpager(object, pager, 0, FALSE); 27245749Smckusick } else { 27345749Smckusick swp->sw_flags = 0; 27445749Smckusick queue_init(&pager->pg_list); 27545749Smckusick } 27645749Smckusick pager->pg_handle = handle; 27745749Smckusick pager->pg_ops = &swappagerops; 27845749Smckusick pager->pg_type = PG_SWAP; 27945749Smckusick pager->pg_data = (caddr_t)swp; 28045749Smckusick 28145749Smckusick #ifdef DEBUG 28245749Smckusick if (swpagerdebug & SDB_ALLOC) 28345749Smckusick printf("swpg_alloc: pg_data %x, %x of %x at %x\n", 28445749Smckusick swp, swp->sw_nblocks, swp->sw_bsize, swp->sw_blocks); 28545749Smckusick #endif 28645749Smckusick return(pager); 28745749Smckusick } 28845749Smckusick 28953341Sbostic static void 29045749Smckusick swap_pager_dealloc(pager) 29145749Smckusick vm_pager_t pager; 29245749Smckusick { 29345749Smckusick register int i; 29445749Smckusick register sw_blk_t bp; 29545749Smckusick register sw_pager_t swp; 29645749Smckusick struct swtab *swt; 29745749Smckusick int s; 29845749Smckusick 29945749Smckusick #ifdef DEBUG 30045749Smckusick /* save panic time state */ 30145749Smckusick if ((swpagerdebug & SDB_ANOMPANIC) && panicstr) 30245749Smckusick return; 30345749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOC)) 30445749Smckusick printf("swpg_dealloc(%x)\n", pager); 30545749Smckusick #endif 30645749Smckusick /* 30745749Smckusick * Remove from list right away so lookups will fail if we 30845749Smckusick * block for pageout completion. 30945749Smckusick */ 31045749Smckusick swp = (sw_pager_t) pager->pg_data; 31145749Smckusick if (swp->sw_flags & SW_NAMED) { 31245749Smckusick queue_remove(&swap_pager_list, pager, vm_pager_t, pg_list); 31345749Smckusick swp->sw_flags &= ~SW_NAMED; 31445749Smckusick } 31545749Smckusick #ifdef DEBUG 31645749Smckusick for (swt = swtab; swt->st_osize; swt++) 31745749Smckusick if (swp->sw_osize <= swt->st_osize) 31845749Smckusick break; 31945749Smckusick swt->st_inuse--; 32045749Smckusick #endif 32145749Smckusick 32245749Smckusick /* 32345749Smckusick * Wait for all pageouts to finish and remove 32445749Smckusick * all entries from cleaning list. 32545749Smckusick */ 32645749Smckusick s = splbio(); 32745749Smckusick while (swp->sw_poip) { 32845749Smckusick swp->sw_flags |= SW_WANTED; 32953341Sbostic assert_wait((int)swp, 0); 33045749Smckusick thread_block(); 33145749Smckusick } 33245749Smckusick splx(s); 33348386Skarels (void) swap_pager_clean(NULL, B_WRITE); 33445749Smckusick 33545749Smckusick /* 33645749Smckusick * Free left over swap blocks 33745749Smckusick */ 33845749Smckusick for (i = 0, bp = swp->sw_blocks; i < swp->sw_nblocks; i++, bp++) 33945749Smckusick if (bp->swb_block) { 34045749Smckusick #ifdef DEBUG 34145749Smckusick if (swpagerdebug & (SDB_ALLOCBLK|SDB_FULL)) 34245749Smckusick printf("swpg_dealloc: blk %x\n", 34345749Smckusick bp->swb_block); 34445749Smckusick #endif 34545749Smckusick rmfree(swapmap, swp->sw_bsize, bp->swb_block); 34645749Smckusick } 34745749Smckusick /* 34845749Smckusick * Free swap management resources 34945749Smckusick */ 35045749Smckusick free((caddr_t)swp->sw_blocks, M_VMPGDATA); 35145749Smckusick free((caddr_t)swp, M_VMPGDATA); 35245749Smckusick free((caddr_t)pager, M_VMPAGER); 35345749Smckusick } 35445749Smckusick 35553341Sbostic static int 35645749Smckusick swap_pager_getpage(pager, m, sync) 35745749Smckusick vm_pager_t pager; 35845749Smckusick vm_page_t m; 35945749Smckusick boolean_t sync; 36045749Smckusick { 36145749Smckusick #ifdef DEBUG 36245749Smckusick if (swpagerdebug & SDB_FOLLOW) 36345749Smckusick printf("swpg_getpage(%x, %x, %d)\n", pager, m, sync); 36445749Smckusick #endif 36545749Smckusick return(swap_pager_io((sw_pager_t)pager->pg_data, m, B_READ)); 36645749Smckusick } 36745749Smckusick 36853341Sbostic static int 36945749Smckusick swap_pager_putpage(pager, m, sync) 37045749Smckusick vm_pager_t pager; 37145749Smckusick vm_page_t m; 37245749Smckusick boolean_t sync; 37345749Smckusick { 37445749Smckusick int flags; 37545749Smckusick 37645749Smckusick #ifdef DEBUG 37745749Smckusick if (swpagerdebug & SDB_FOLLOW) 37845749Smckusick printf("swpg_putpage(%x, %x, %d)\n", pager, m, sync); 37945749Smckusick #endif 38048386Skarels if (pager == NULL) { 38148386Skarels (void) swap_pager_clean(NULL, B_WRITE); 38254817Storek return (VM_PAGER_OK); /* ??? */ 38345749Smckusick } 38445749Smckusick flags = B_WRITE; 38545749Smckusick if (!sync) 38645749Smckusick flags |= B_ASYNC; 38745749Smckusick return(swap_pager_io((sw_pager_t)pager->pg_data, m, flags)); 38845749Smckusick } 38945749Smckusick 39053341Sbostic static boolean_t 39145749Smckusick swap_pager_haspage(pager, offset) 39245749Smckusick vm_pager_t pager; 39345749Smckusick vm_offset_t offset; 39445749Smckusick { 39545749Smckusick register sw_pager_t swp; 39645749Smckusick register sw_blk_t swb; 39745749Smckusick int ix; 39845749Smckusick 39945749Smckusick #ifdef DEBUG 40045749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOCBLK)) 40145749Smckusick printf("swpg_haspage(%x, %x) ", pager, offset); 40245749Smckusick #endif 40345749Smckusick swp = (sw_pager_t) pager->pg_data; 40445749Smckusick ix = offset / dbtob(swp->sw_bsize); 40545749Smckusick if (swp->sw_blocks == NULL || ix >= swp->sw_nblocks) { 40645749Smckusick #ifdef DEBUG 40745749Smckusick if (swpagerdebug & (SDB_FAIL|SDB_FOLLOW|SDB_ALLOCBLK)) 40845749Smckusick printf("swpg_haspage: %x bad offset %x, ix %x\n", 40945749Smckusick swp->sw_blocks, offset, ix); 41045749Smckusick #endif 41145749Smckusick return(FALSE); 41245749Smckusick } 41345749Smckusick swb = &swp->sw_blocks[ix]; 41445749Smckusick if (swb->swb_block) 41545749Smckusick ix = atop(offset % dbtob(swp->sw_bsize)); 41645749Smckusick #ifdef DEBUG 41745749Smckusick if (swpagerdebug & SDB_ALLOCBLK) 41845749Smckusick printf("%x blk %x+%x ", swp->sw_blocks, swb->swb_block, ix); 41945749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOCBLK)) 42045749Smckusick printf("-> %c\n", 42145749Smckusick "FT"[swb->swb_block && (swb->swb_mask & (1 << ix))]); 42245749Smckusick #endif 42345749Smckusick if (swb->swb_block && (swb->swb_mask & (1 << ix))) 42445749Smckusick return(TRUE); 42545749Smckusick return(FALSE); 42645749Smckusick } 42745749Smckusick 42845749Smckusick /* 42945749Smckusick * Scaled down version of swap(). 43045749Smckusick * Assumes that PAGE_SIZE < MAXPHYS; i.e. only one operation needed. 43145749Smckusick * BOGUS: lower level IO routines expect a KVA so we have to map our 43245749Smckusick * provided physical page into the KVA to keep them happy. 43345749Smckusick */ 43453341Sbostic static int 43545749Smckusick swap_pager_io(swp, m, flags) 43645749Smckusick register sw_pager_t swp; 43745749Smckusick vm_page_t m; 43845749Smckusick int flags; 43945749Smckusick { 44045749Smckusick register struct buf *bp; 44145749Smckusick register sw_blk_t swb; 44245749Smckusick register int s; 44345749Smckusick int ix; 44445749Smckusick boolean_t rv; 44545749Smckusick vm_offset_t kva, off; 44645749Smckusick swp_clean_t spc; 44745749Smckusick 44845749Smckusick #ifdef DEBUG 44945749Smckusick /* save panic time state */ 45045749Smckusick if ((swpagerdebug & SDB_ANOMPANIC) && panicstr) 45153341Sbostic return (VM_PAGER_FAIL); /* XXX: correct return? */ 45245749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_IO)) 45345749Smckusick printf("swpg_io(%x, %x, %x)\n", swp, m, flags); 45445749Smckusick #endif 45545749Smckusick 45645749Smckusick /* 45745749Smckusick * For reads (pageins) and synchronous writes, we clean up 45849289Shibler * all completed async pageouts. 45945749Smckusick */ 46045749Smckusick if ((flags & B_ASYNC) == 0) { 46145749Smckusick s = splbio(); 46249289Shibler #ifdef DEBUG 46349289Shibler /* 46449289Shibler * Check to see if this page is currently being cleaned. 46549289Shibler * If it is, we just wait til the operation is done before 46649289Shibler * continuing. 46749289Shibler */ 46845749Smckusick while (swap_pager_clean(m, flags&B_READ)) { 46949289Shibler if (swpagerdebug & SDB_ANOM) 47049289Shibler printf("swap_pager_io: page %x cleaning\n", m); 47149289Shibler 47245749Smckusick swp->sw_flags |= SW_WANTED; 47353341Sbostic assert_wait((int)swp, 0); 47445749Smckusick thread_block(); 47545749Smckusick } 47649289Shibler #else 47749289Shibler (void) swap_pager_clean(m, flags&B_READ); 47849289Shibler #endif 47945749Smckusick splx(s); 48045749Smckusick } 48145749Smckusick /* 48245749Smckusick * For async writes (pageouts), we cleanup completed pageouts so 48345749Smckusick * that all available resources are freed. Also tells us if this 48445749Smckusick * page is already being cleaned. If it is, or no resources 48545749Smckusick * are available, we try again later. 48645749Smckusick */ 48749289Shibler else if (swap_pager_clean(m, B_WRITE) || 48849289Shibler queue_empty(&swap_pager_free)) { 48949289Shibler #ifdef DEBUG 49049289Shibler if ((swpagerdebug & SDB_ANOM) && 49149289Shibler !queue_empty(&swap_pager_free)) 49249289Shibler printf("swap_pager_io: page %x already cleaning\n", m); 49349289Shibler #endif 49445749Smckusick return(VM_PAGER_FAIL); 49549289Shibler } 49645749Smckusick 49745749Smckusick /* 49845749Smckusick * Determine swap block and allocate as necessary. 49945749Smckusick */ 50045749Smckusick off = m->offset + m->object->paging_offset; 50145749Smckusick ix = off / dbtob(swp->sw_bsize); 50245749Smckusick if (swp->sw_blocks == NULL || ix >= swp->sw_nblocks) { 50345749Smckusick #ifdef DEBUG 50445749Smckusick if (swpagerdebug & SDB_FAIL) 50545749Smckusick printf("swpg_io: bad offset %x+%x(%d) in %x\n", 50645749Smckusick m->offset, m->object->paging_offset, 50745749Smckusick ix, swp->sw_blocks); 50845749Smckusick #endif 50945749Smckusick return(VM_PAGER_FAIL); 51045749Smckusick } 51145749Smckusick swb = &swp->sw_blocks[ix]; 51245749Smckusick off = off % dbtob(swp->sw_bsize); 51345749Smckusick if (flags & B_READ) { 51445749Smckusick if (swb->swb_block == 0 || 51545749Smckusick (swb->swb_mask & (1 << atop(off))) == 0) { 51645749Smckusick #ifdef DEBUG 51745749Smckusick if (swpagerdebug & (SDB_ALLOCBLK|SDB_FAIL)) 51845749Smckusick printf("swpg_io: %x bad read: blk %x+%x, mask %x, off %x+%x\n", 51945749Smckusick swp->sw_blocks, 52045749Smckusick swb->swb_block, atop(off), 52145749Smckusick swb->swb_mask, 52245749Smckusick m->offset, m->object->paging_offset); 52345749Smckusick #endif 52445749Smckusick /* XXX: should we zero page here?? */ 52545749Smckusick return(VM_PAGER_FAIL); 52645749Smckusick } 52745749Smckusick } else if (swb->swb_block == 0) { 52845749Smckusick swb->swb_block = rmalloc(swapmap, swp->sw_bsize); 52945749Smckusick if (swb->swb_block == 0) { 53045749Smckusick #ifdef DEBUG 53145749Smckusick if (swpagerdebug & SDB_FAIL) 53245749Smckusick printf("swpg_io: rmalloc of %x failed\n", 53345749Smckusick swp->sw_bsize); 53445749Smckusick #endif 53545749Smckusick return(VM_PAGER_FAIL); 53645749Smckusick } 53745749Smckusick #ifdef DEBUG 53845749Smckusick if (swpagerdebug & (SDB_FULL|SDB_ALLOCBLK)) 53945749Smckusick printf("swpg_io: %x alloc blk %x at ix %x\n", 54045749Smckusick swp->sw_blocks, swb->swb_block, ix); 54145749Smckusick #endif 54245749Smckusick } 54345749Smckusick 54445749Smckusick /* 54545749Smckusick * Allocate a kernel virtual address and initialize so that PTE 54645749Smckusick * is available for lower level IO drivers. 54745749Smckusick */ 54845749Smckusick kva = vm_pager_map_page(m); 54945749Smckusick 55045749Smckusick /* 55145749Smckusick * Get a swap buffer header and perform the IO 55245749Smckusick */ 55345749Smckusick s = splbio(); 55456393Smckusick while (bswlist.b_actf == NULL) { 55545749Smckusick #ifdef DEBUG 55645749Smckusick if (swpagerdebug & SDB_ANOM) 55749289Shibler printf("swap_pager_io: wait on swbuf for %x (%d)\n", 55845749Smckusick m, flags); 55945749Smckusick #endif 56045749Smckusick bswlist.b_flags |= B_WANTED; 56145749Smckusick sleep((caddr_t)&bswlist, PSWP+1); 56245749Smckusick } 56356393Smckusick bp = bswlist.b_actf; 56456393Smckusick bswlist.b_actf = bp->b_actf; 56545749Smckusick splx(s); 56645749Smckusick bp->b_flags = B_BUSY | (flags & B_READ); 56748386Skarels bp->b_proc = &proc0; /* XXX (but without B_PHYS set this is ok) */ 56845749Smckusick bp->b_un.b_addr = (caddr_t)kva; 56945749Smckusick bp->b_blkno = swb->swb_block + btodb(off); 57045749Smckusick VHOLD(swapdev_vp); 57145749Smckusick bp->b_vp = swapdev_vp; 57246985Smckusick if (swapdev_vp->v_type == VBLK) 57346985Smckusick bp->b_dev = swapdev_vp->v_rdev; 57445749Smckusick bp->b_bcount = PAGE_SIZE; 57553213Smckusick if ((bp->b_flags & B_READ) == 0) { 57653213Smckusick bp->b_dirtyoff = 0; 57753213Smckusick bp->b_dirtyend = PAGE_SIZE; 57845749Smckusick swapdev_vp->v_numoutput++; 57953213Smckusick } 58045749Smckusick 58145749Smckusick /* 58245749Smckusick * If this is an async write we set up additional buffer fields 58345749Smckusick * and place a "cleaning" entry on the inuse queue. 58445749Smckusick */ 58545749Smckusick if ((flags & (B_READ|B_ASYNC)) == B_ASYNC) { 58645749Smckusick #ifdef DEBUG 58745749Smckusick if (queue_empty(&swap_pager_free)) 58845749Smckusick panic("swpg_io: lost spc"); 58945749Smckusick #endif 59045749Smckusick queue_remove_first(&swap_pager_free, 59145749Smckusick spc, swp_clean_t, spc_list); 59245749Smckusick #ifdef DEBUG 59345749Smckusick if (spc->spc_flags != SPC_FREE) 59445749Smckusick panic("swpg_io: bad free spc"); 59545749Smckusick #endif 59645749Smckusick spc->spc_flags = SPC_BUSY; 59745749Smckusick spc->spc_bp = bp; 59845749Smckusick spc->spc_swp = swp; 59945749Smckusick spc->spc_kva = kva; 60045749Smckusick spc->spc_m = m; 60145749Smckusick bp->b_flags |= B_CALL; 60245749Smckusick bp->b_iodone = swap_pager_iodone; 60345749Smckusick s = splbio(); 60445749Smckusick swp->sw_poip++; 60545749Smckusick queue_enter(&swap_pager_inuse, spc, swp_clean_t, spc_list); 60645749Smckusick 60745749Smckusick #ifdef DEBUG 60845749Smckusick swap_pager_poip++; 60945749Smckusick if (swpagerdebug & SDB_WRITE) 61045749Smckusick printf("swpg_io: write: bp=%x swp=%x spc=%x poip=%d\n", 61145749Smckusick bp, swp, spc, swp->sw_poip); 61245749Smckusick if ((swpagerdebug & SDB_ALLOCBLK) && 61345749Smckusick (swb->swb_mask & (1 << atop(off))) == 0) 61445749Smckusick printf("swpg_io: %x write blk %x+%x\n", 61545749Smckusick swp->sw_blocks, swb->swb_block, atop(off)); 61645749Smckusick #endif 61745749Smckusick swb->swb_mask |= (1 << atop(off)); 61845749Smckusick splx(s); 61945749Smckusick } 62045749Smckusick #ifdef DEBUG 62145749Smckusick if (swpagerdebug & SDB_IO) 62245749Smckusick printf("swpg_io: IO start: bp %x, db %x, va %x, pa %x\n", 62345749Smckusick bp, swb->swb_block+btodb(off), kva, VM_PAGE_TO_PHYS(m)); 62445749Smckusick #endif 62545749Smckusick VOP_STRATEGY(bp); 62645749Smckusick if ((flags & (B_READ|B_ASYNC)) == B_ASYNC) { 62745749Smckusick #ifdef DEBUG 62845749Smckusick if (swpagerdebug & SDB_IO) 62945749Smckusick printf("swpg_io: IO started: bp %x\n", bp); 63045749Smckusick #endif 63145749Smckusick return(VM_PAGER_PEND); 63245749Smckusick } 63345749Smckusick s = splbio(); 63445749Smckusick #ifdef DEBUG 63545749Smckusick if (flags & B_READ) 63645749Smckusick swap_pager_piip++; 63745749Smckusick else 63845749Smckusick swap_pager_poip++; 63945749Smckusick #endif 64045749Smckusick while ((bp->b_flags & B_DONE) == 0) { 64153341Sbostic assert_wait((int)bp, 0); 64245749Smckusick thread_block(); 64345749Smckusick } 64445749Smckusick #ifdef DEBUG 64545749Smckusick if (flags & B_READ) 64645749Smckusick --swap_pager_piip; 64745749Smckusick else 64845749Smckusick --swap_pager_poip; 64945749Smckusick #endif 65056320Shibler rv = (bp->b_flags & B_ERROR) ? VM_PAGER_ERROR : VM_PAGER_OK; 65145749Smckusick bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 65256393Smckusick bp->b_actf = bswlist.b_actf; 65356393Smckusick bswlist.b_actf = bp; 65445749Smckusick if (bp->b_vp) 65545749Smckusick brelvp(bp); 65645749Smckusick if (bswlist.b_flags & B_WANTED) { 65745749Smckusick bswlist.b_flags &= ~B_WANTED; 65845749Smckusick thread_wakeup((int)&bswlist); 65945749Smckusick } 66045749Smckusick if ((flags & B_READ) == 0 && rv == VM_PAGER_OK) { 66156382Smckusick m->flags |= PG_CLEAN; 66245749Smckusick pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 66345749Smckusick } 66445749Smckusick splx(s); 66545749Smckusick #ifdef DEBUG 66645749Smckusick if (swpagerdebug & SDB_IO) 66745749Smckusick printf("swpg_io: IO done: bp %x, rv %d\n", bp, rv); 66856320Shibler if ((swpagerdebug & SDB_FAIL) && rv == VM_PAGER_ERROR) 66945749Smckusick printf("swpg_io: IO error\n"); 67045749Smckusick #endif 67145749Smckusick vm_pager_unmap_page(kva); 67245749Smckusick return(rv); 67345749Smckusick } 67445749Smckusick 67553341Sbostic static boolean_t 67645749Smckusick swap_pager_clean(m, rw) 67745749Smckusick vm_page_t m; 67845749Smckusick int rw; 67945749Smckusick { 68045749Smckusick register swp_clean_t spc, tspc; 68145749Smckusick register int s; 68245749Smckusick 68345749Smckusick #ifdef DEBUG 68445749Smckusick /* save panic time state */ 68545749Smckusick if ((swpagerdebug & SDB_ANOMPANIC) && panicstr) 68654817Storek return (FALSE); /* ??? */ 68745749Smckusick if (swpagerdebug & SDB_FOLLOW) 68845749Smckusick printf("swpg_clean(%x, %d)\n", m, rw); 68945749Smckusick #endif 69048386Skarels tspc = NULL; 69145749Smckusick for (;;) { 69245749Smckusick /* 69345749Smckusick * Look up and removal from inuse list must be done 69445749Smckusick * at splbio() to avoid conflicts with swap_pager_iodone. 69545749Smckusick */ 69645749Smckusick s = splbio(); 69745749Smckusick spc = (swp_clean_t) queue_first(&swap_pager_inuse); 69845749Smckusick while (!queue_end(&swap_pager_inuse, (queue_entry_t)spc)) { 69945749Smckusick if ((spc->spc_flags & SPC_DONE) && 70045749Smckusick swap_pager_finish(spc)) { 70145749Smckusick queue_remove(&swap_pager_inuse, spc, 70245749Smckusick swp_clean_t, spc_list); 70345749Smckusick break; 70445749Smckusick } 70545749Smckusick if (m && m == spc->spc_m) { 70645749Smckusick #ifdef DEBUG 70745749Smckusick if (swpagerdebug & SDB_ANOM) 70849289Shibler printf("swap_pager_clean: page %x on list, flags %x\n", 70945749Smckusick m, spc->spc_flags); 71045749Smckusick #endif 71145749Smckusick tspc = spc; 71245749Smckusick } 71345749Smckusick spc = (swp_clean_t) queue_next(&spc->spc_list); 71445749Smckusick } 71545749Smckusick 71645749Smckusick /* 71745749Smckusick * No operations done, thats all we can do for now. 71845749Smckusick */ 71945749Smckusick if (queue_end(&swap_pager_inuse, (queue_entry_t)spc)) 72045749Smckusick break; 72145749Smckusick splx(s); 72245749Smckusick 72345749Smckusick /* 72445749Smckusick * The desired page was found to be busy earlier in 72545749Smckusick * the scan but has since completed. 72645749Smckusick */ 72745749Smckusick if (tspc && tspc == spc) { 72845749Smckusick #ifdef DEBUG 72945749Smckusick if (swpagerdebug & SDB_ANOM) 73049289Shibler printf("swap_pager_clean: page %x done while looking\n", 73145749Smckusick m); 73245749Smckusick #endif 73348386Skarels tspc = NULL; 73445749Smckusick } 73545749Smckusick spc->spc_flags = SPC_FREE; 73645749Smckusick vm_pager_unmap_page(spc->spc_kva); 73745749Smckusick queue_enter(&swap_pager_free, spc, swp_clean_t, spc_list); 73845749Smckusick #ifdef DEBUG 73945749Smckusick if (swpagerdebug & SDB_WRITE) 74045749Smckusick printf("swpg_clean: free spc %x\n", spc); 74145749Smckusick #endif 74245749Smckusick } 74349289Shibler #ifdef DEBUG 74445749Smckusick /* 74545749Smckusick * If we found that the desired page is already being cleaned 74645749Smckusick * mark it so that swap_pager_iodone() will not set the clean 74745749Smckusick * flag before the pageout daemon has another chance to clean it. 74845749Smckusick */ 74945749Smckusick if (tspc && rw == B_WRITE) { 75045749Smckusick if (swpagerdebug & SDB_ANOM) 75149289Shibler printf("swap_pager_clean: page %x on clean list\n", 75249289Shibler tspc); 75345749Smckusick tspc->spc_flags |= SPC_DIRTY; 75445749Smckusick } 75549289Shibler #endif 75645749Smckusick splx(s); 75745749Smckusick 75845749Smckusick #ifdef DEBUG 75945749Smckusick if (swpagerdebug & SDB_WRITE) 76045749Smckusick printf("swpg_clean: return %d\n", tspc ? TRUE : FALSE); 76145749Smckusick if ((swpagerdebug & SDB_ANOM) && tspc) 76245749Smckusick printf("swpg_clean: %s of cleaning page %x\n", 76345749Smckusick rw == B_READ ? "get" : "put", m); 76445749Smckusick #endif 76545749Smckusick return(tspc ? TRUE : FALSE); 76645749Smckusick } 76745749Smckusick 76853341Sbostic static int 76945749Smckusick swap_pager_finish(spc) 77045749Smckusick register swp_clean_t spc; 77145749Smckusick { 77245749Smckusick vm_object_t object = spc->spc_m->object; 77345749Smckusick 77445749Smckusick /* 77545749Smckusick * Mark the paging operation as done. 77645749Smckusick * (XXX) If we cannot get the lock, leave it til later. 77745749Smckusick * (XXX) Also we are assuming that an async write is a 77845749Smckusick * pageout operation that has incremented the counter. 77945749Smckusick */ 78045749Smckusick if (!vm_object_lock_try(object)) 78145749Smckusick return(0); 78245749Smckusick 78345749Smckusick if (--object->paging_in_progress == 0) 78445749Smckusick thread_wakeup((int) object); 78545749Smckusick 78649289Shibler #ifdef DEBUG 78745749Smckusick /* 78845749Smckusick * XXX: this isn't even close to the right thing to do, 78945749Smckusick * introduces a variety of race conditions. 79045749Smckusick * 79145749Smckusick * If dirty, vm_pageout() has attempted to clean the page 79245749Smckusick * again. In this case we do not do anything as we will 79349289Shibler * see the page again shortly. 79445749Smckusick */ 79549289Shibler if (spc->spc_flags & SPC_DIRTY) { 79649289Shibler if (swpagerdebug & SDB_ANOM) 79749289Shibler printf("swap_pager_finish: page %x dirty again\n", 79849289Shibler spc->spc_m); 79956382Smckusick spc->spc_m->flags &= ~PG_BUSY; 80049289Shibler PAGE_WAKEUP(spc->spc_m); 80149289Shibler vm_object_unlock(object); 80249289Shibler return(1); 80345749Smckusick } 80449289Shibler #endif 80545749Smckusick /* 80649289Shibler * If no error mark as clean and inform the pmap system. 80749289Shibler * If error, mark as dirty so we will try again. 80849289Shibler * (XXX could get stuck doing this, should give up after awhile) 80945749Smckusick */ 81049289Shibler if (spc->spc_flags & SPC_ERROR) { 81149289Shibler printf("swap_pager_finish: clean of page %x failed\n", 81249289Shibler VM_PAGE_TO_PHYS(spc->spc_m)); 81356382Smckusick spc->spc_m->flags |= PG_LAUNDRY; 81449289Shibler } else { 81556382Smckusick spc->spc_m->flags |= PG_CLEAN; 81649289Shibler pmap_clear_modify(VM_PAGE_TO_PHYS(spc->spc_m)); 81749289Shibler } 81856382Smckusick spc->spc_m->flags &= ~PG_BUSY; 81945749Smckusick PAGE_WAKEUP(spc->spc_m); 82045749Smckusick 82145749Smckusick vm_object_unlock(object); 82245749Smckusick return(1); 82345749Smckusick } 82445749Smckusick 82553341Sbostic static void 82645749Smckusick swap_pager_iodone(bp) 82745749Smckusick register struct buf *bp; 82845749Smckusick { 82945749Smckusick register swp_clean_t spc; 83045749Smckusick daddr_t blk; 83145749Smckusick int s; 83245749Smckusick 83345749Smckusick #ifdef DEBUG 83445749Smckusick /* save panic time state */ 83545749Smckusick if ((swpagerdebug & SDB_ANOMPANIC) && panicstr) 83645749Smckusick return; 83745749Smckusick if (swpagerdebug & SDB_FOLLOW) 83845749Smckusick printf("swpg_iodone(%x)\n", bp); 83945749Smckusick #endif 84045749Smckusick s = splbio(); 84145749Smckusick spc = (swp_clean_t) queue_first(&swap_pager_inuse); 84245749Smckusick while (!queue_end(&swap_pager_inuse, (queue_entry_t)spc)) { 84345749Smckusick if (spc->spc_bp == bp) 84445749Smckusick break; 84545749Smckusick spc = (swp_clean_t) queue_next(&spc->spc_list); 84645749Smckusick } 84745749Smckusick #ifdef DEBUG 84845749Smckusick if (queue_end(&swap_pager_inuse, (queue_entry_t)spc)) 84949289Shibler panic("swap_pager_iodone: bp not found"); 85045749Smckusick #endif 85145749Smckusick 85245749Smckusick spc->spc_flags &= ~SPC_BUSY; 85345749Smckusick spc->spc_flags |= SPC_DONE; 85445749Smckusick if (bp->b_flags & B_ERROR) 85545749Smckusick spc->spc_flags |= SPC_ERROR; 85645749Smckusick spc->spc_bp = NULL; 85745749Smckusick blk = bp->b_blkno; 85845749Smckusick 85945749Smckusick #ifdef DEBUG 86045749Smckusick --swap_pager_poip; 86145749Smckusick if (swpagerdebug & SDB_WRITE) 86245749Smckusick printf("swpg_iodone: bp=%x swp=%x flags=%x spc=%x poip=%x\n", 86345749Smckusick bp, spc->spc_swp, spc->spc_swp->sw_flags, 86445749Smckusick spc, spc->spc_swp->sw_poip); 86545749Smckusick #endif 86645749Smckusick 86745749Smckusick spc->spc_swp->sw_poip--; 86845749Smckusick if (spc->spc_swp->sw_flags & SW_WANTED) { 86945749Smckusick spc->spc_swp->sw_flags &= ~SW_WANTED; 87045749Smckusick thread_wakeup((int)spc->spc_swp); 87145749Smckusick } 87245749Smckusick 87345749Smckusick bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 87456393Smckusick bp->b_actf = bswlist.b_actf; 87556393Smckusick bswlist.b_actf = bp; 87645749Smckusick if (bp->b_vp) 87745749Smckusick brelvp(bp); 87845749Smckusick if (bswlist.b_flags & B_WANTED) { 87945749Smckusick bswlist.b_flags &= ~B_WANTED; 88045749Smckusick thread_wakeup((int)&bswlist); 88145749Smckusick } 88256917Shibler /* 88356917Shibler * Only kick the pageout daemon if we are really hurting 88456917Shibler * for pages, otherwise this page will be picked up later. 88556917Shibler */ 88656917Shibler if (cnt.v_free_count < cnt.v_free_min) 88756917Shibler thread_wakeup((int) &vm_pages_needed); 88845749Smckusick splx(s); 88945749Smckusick } 890