145749Smckusick /* 245749Smckusick * Copyright (c) 1990 University of Utah. 345749Smckusick * Copyright (c) 1991 The Regents of the University of California. 445749Smckusick * 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*53496Sheideman * @(#)swap_pager.c 7.8 (Berkeley) 05/14/92 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 2445749Smckusick #include "swappager.h" 2545749Smckusick #if NSWAPPAGER > 0 2645749Smckusick 2753341Sbostic #include <sys/param.h> 2853341Sbostic #include <sys/systm.h> 2953341Sbostic #include <sys/proc.h> 3053341Sbostic #include <sys/buf.h> 3153341Sbostic #include <sys/map.h> 32*53496Sheideman #include <sys/vnode.h> 3353341Sbostic #include <sys/specdev.h> 3453341Sbostic #include <sys/malloc.h> 3545749Smckusick 3653341Sbostic #include <vm/vm.h> 3753341Sbostic #include <vm/queue.h> 3853341Sbostic #include <vm/vm_page.h> 3953341Sbostic #include <vm/vm_pageout.h> 4053341Sbostic #include <vm/swap_pager.h> 4145749Smckusick 4245749Smckusick #define NSWSIZES 16 /* size of swtab */ 4345749Smckusick #define NPENDINGIO 64 /* max # of pending cleans */ 4445749Smckusick #define MAXDADDRS 64 /* max # of disk addrs for fixed allocations */ 4545749Smckusick 4645749Smckusick #ifdef DEBUG 4745749Smckusick int swpagerdebug = 0x100; 4845749Smckusick #define SDB_FOLLOW 0x001 4945749Smckusick #define SDB_INIT 0x002 5045749Smckusick #define SDB_ALLOC 0x004 5145749Smckusick #define SDB_IO 0x008 5245749Smckusick #define SDB_WRITE 0x010 5345749Smckusick #define SDB_FAIL 0x020 5445749Smckusick #define SDB_ALLOCBLK 0x040 5545749Smckusick #define SDB_FULL 0x080 5645749Smckusick #define SDB_ANOM 0x100 5745749Smckusick #define SDB_ANOMPANIC 0x200 5845749Smckusick #endif 5945749Smckusick 6045749Smckusick struct swpagerclean { 6145749Smckusick queue_head_t spc_list; 6245749Smckusick int spc_flags; 6345749Smckusick struct buf *spc_bp; 6445749Smckusick sw_pager_t spc_swp; 6545749Smckusick vm_offset_t spc_kva; 6645749Smckusick vm_page_t spc_m; 6745749Smckusick } swcleanlist[NPENDINGIO]; 6853341Sbostic typedef struct swpagerclean *swp_clean_t; 6945749Smckusick 7053341Sbostic 7145749Smckusick /* spc_flags values */ 7245749Smckusick #define SPC_FREE 0x00 7345749Smckusick #define SPC_BUSY 0x01 7445749Smckusick #define SPC_DONE 0x02 7545749Smckusick #define SPC_ERROR 0x04 7645749Smckusick #define SPC_DIRTY 0x08 7745749Smckusick 7845749Smckusick struct swtab { 7945749Smckusick vm_size_t st_osize; /* size of object (bytes) */ 8045749Smckusick int st_bsize; /* vs. size of swap block (DEV_BSIZE units) */ 8145749Smckusick #ifdef DEBUG 8245749Smckusick u_long st_inuse; /* number in this range in use */ 8345749Smckusick u_long st_usecnt; /* total used of this size */ 8445749Smckusick #endif 8545749Smckusick } swtab[NSWSIZES+1]; 8645749Smckusick 8745749Smckusick #ifdef DEBUG 8845749Smckusick int swap_pager_pendingio; /* max pending async "clean" ops */ 8945749Smckusick int swap_pager_poip; /* pageouts in progress */ 9045749Smckusick int swap_pager_piip; /* pageins in progress */ 9145749Smckusick #endif 9245749Smckusick 9345749Smckusick queue_head_t swap_pager_inuse; /* list of pending page cleans */ 9445749Smckusick queue_head_t swap_pager_free; /* list of free pager clean structs */ 9545749Smckusick queue_head_t swap_pager_list; /* list of "named" anon regions */ 9645749Smckusick 9753341Sbostic static int swap_pager_finish __P((swp_clean_t)); 9853341Sbostic static void swap_pager_init __P((void)); 9953341Sbostic static vm_pager_t swap_pager_alloc __P((caddr_t, vm_size_t, vm_prot_t)); 10053341Sbostic static boolean_t swap_pager_clean __P((vm_page_t, int)); 10153341Sbostic static void swap_pager_dealloc __P((vm_pager_t)); 10253341Sbostic static int swap_pager_getpage 10353341Sbostic __P((vm_pager_t, vm_page_t, boolean_t)); 10453341Sbostic static boolean_t swap_pager_haspage __P((vm_pager_t, vm_offset_t)); 10553341Sbostic static int swap_pager_io __P((sw_pager_t, vm_page_t, int)); 10653341Sbostic static void swap_pager_iodone __P((struct buf *)); 10753341Sbostic static int swap_pager_putpage 10853341Sbostic __P((vm_pager_t, vm_page_t, boolean_t)); 10953341Sbostic 11053341Sbostic struct pagerops swappagerops = { 11153341Sbostic swap_pager_init, 11253341Sbostic swap_pager_alloc, 11353341Sbostic swap_pager_dealloc, 11453341Sbostic swap_pager_getpage, 11553341Sbostic swap_pager_putpage, 11653341Sbostic swap_pager_haspage 11753341Sbostic }; 11853341Sbostic 11953341Sbostic static void 12045749Smckusick swap_pager_init() 12145749Smckusick { 12245749Smckusick register swp_clean_t spc; 12345749Smckusick register int i, bsize; 12445749Smckusick extern int dmmin, dmmax; 12545749Smckusick int maxbsize; 12645749Smckusick 12745749Smckusick #ifdef DEBUG 12845749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_INIT)) 12945749Smckusick printf("swpg_init()\n"); 13045749Smckusick #endif 13145749Smckusick dfltpagerops = &swappagerops; 13245749Smckusick queue_init(&swap_pager_list); 13345749Smckusick 13445749Smckusick /* 13545749Smckusick * Initialize clean lists 13645749Smckusick */ 13745749Smckusick queue_init(&swap_pager_inuse); 13845749Smckusick queue_init(&swap_pager_free); 13945749Smckusick for (i = 0, spc = swcleanlist; i < NPENDINGIO; i++, spc++) { 14045749Smckusick queue_enter(&swap_pager_free, spc, swp_clean_t, spc_list); 14145749Smckusick spc->spc_flags = SPC_FREE; 14245749Smckusick } 14345749Smckusick 14445749Smckusick /* 14545749Smckusick * Calculate the swap allocation constants. 14645749Smckusick */ 14745749Smckusick if (dmmin == 0) { 14845749Smckusick dmmin = DMMIN; 14945749Smckusick if (dmmin < CLBYTES/DEV_BSIZE) 15045749Smckusick dmmin = CLBYTES/DEV_BSIZE; 15145749Smckusick } 15245749Smckusick if (dmmax == 0) 15345749Smckusick dmmax = DMMAX; 15445749Smckusick 15545749Smckusick /* 15645749Smckusick * Fill in our table of object size vs. allocation size 15745749Smckusick */ 15845749Smckusick bsize = btodb(PAGE_SIZE); 15945749Smckusick if (bsize < dmmin) 16045749Smckusick bsize = dmmin; 16145749Smckusick maxbsize = btodb(sizeof(sw_bm_t) * NBBY * PAGE_SIZE); 16245749Smckusick if (maxbsize > dmmax) 16345749Smckusick maxbsize = dmmax; 16445749Smckusick for (i = 0; i < NSWSIZES; i++) { 16545749Smckusick swtab[i].st_osize = (vm_size_t) (MAXDADDRS * dbtob(bsize)); 16645749Smckusick swtab[i].st_bsize = bsize; 16745749Smckusick #ifdef DEBUG 16845749Smckusick if (swpagerdebug & SDB_INIT) 16945749Smckusick printf("swpg_init: ix %d, size %x, bsize %x\n", 17045749Smckusick i, swtab[i].st_osize, swtab[i].st_bsize); 17145749Smckusick #endif 17245749Smckusick if (bsize >= maxbsize) 17345749Smckusick break; 17445749Smckusick bsize *= 2; 17545749Smckusick } 17645749Smckusick swtab[i].st_osize = 0; 17745749Smckusick swtab[i].st_bsize = bsize; 17845749Smckusick } 17945749Smckusick 18045749Smckusick /* 18145749Smckusick * Allocate a pager structure and associated resources. 18245749Smckusick * Note that if we are called from the pageout daemon (handle == NULL) 18345749Smckusick * we should not wait for memory as it could resulting in deadlock. 18445749Smckusick */ 18553341Sbostic static vm_pager_t 18645749Smckusick swap_pager_alloc(handle, size, prot) 18745749Smckusick caddr_t handle; 18845749Smckusick register vm_size_t size; 18945749Smckusick vm_prot_t prot; 19045749Smckusick { 19145749Smckusick register vm_pager_t pager; 19245749Smckusick register sw_pager_t swp; 19345749Smckusick struct swtab *swt; 19445749Smckusick int waitok; 19545749Smckusick 19645749Smckusick #ifdef DEBUG 19745749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOC)) 19845749Smckusick printf("swpg_alloc(%x, %x, %x)\n", handle, size, prot); 19945749Smckusick #endif 20045749Smckusick /* 20145749Smckusick * If this is a "named" anonymous region, look it up and 20245749Smckusick * return the appropriate pager if it exists. 20345749Smckusick */ 20445749Smckusick if (handle) { 20545749Smckusick pager = vm_pager_lookup(&swap_pager_list, handle); 20648386Skarels if (pager != NULL) { 20745749Smckusick /* 20845749Smckusick * Use vm_object_lookup to gain a reference 20945749Smckusick * to the object and also to remove from the 21045749Smckusick * object cache. 21145749Smckusick */ 21248386Skarels if (vm_object_lookup(pager) == NULL) 21345749Smckusick panic("swap_pager_alloc: bad object"); 21445749Smckusick return(pager); 21545749Smckusick } 21645749Smckusick } 21745749Smckusick /* 21845749Smckusick * Pager doesn't exist, allocate swap management resources 21945749Smckusick * and initialize. 22045749Smckusick */ 22145749Smckusick waitok = handle ? M_WAITOK : M_NOWAIT; 22245749Smckusick pager = (vm_pager_t)malloc(sizeof *pager, M_VMPAGER, waitok); 22348386Skarels if (pager == NULL) 22448386Skarels return(NULL); 22545749Smckusick swp = (sw_pager_t)malloc(sizeof *swp, M_VMPGDATA, waitok); 22645749Smckusick if (swp == NULL) { 22745749Smckusick #ifdef DEBUG 22845749Smckusick if (swpagerdebug & SDB_FAIL) 22945749Smckusick printf("swpg_alloc: swpager malloc failed\n"); 23045749Smckusick #endif 23145749Smckusick free((caddr_t)pager, M_VMPAGER); 23248386Skarels return(NULL); 23345749Smckusick } 23445749Smckusick size = round_page(size); 23545749Smckusick for (swt = swtab; swt->st_osize; swt++) 23645749Smckusick if (size <= swt->st_osize) 23745749Smckusick break; 23845749Smckusick #ifdef DEBUG 23945749Smckusick swt->st_inuse++; 24045749Smckusick swt->st_usecnt++; 24145749Smckusick #endif 24245749Smckusick swp->sw_osize = size; 24345749Smckusick swp->sw_bsize = swt->st_bsize; 24445749Smckusick swp->sw_nblocks = (btodb(size) + swp->sw_bsize - 1) / swp->sw_bsize; 24545749Smckusick swp->sw_blocks = (sw_blk_t) 24645749Smckusick malloc(swp->sw_nblocks*sizeof(*swp->sw_blocks), 24745749Smckusick M_VMPGDATA, M_NOWAIT); 24845749Smckusick if (swp->sw_blocks == NULL) { 24945749Smckusick free((caddr_t)swp, M_VMPGDATA); 25045749Smckusick free((caddr_t)pager, M_VMPAGER); 25145749Smckusick #ifdef DEBUG 25245749Smckusick if (swpagerdebug & SDB_FAIL) 25345749Smckusick printf("swpg_alloc: sw_blocks malloc failed\n"); 25445749Smckusick swt->st_inuse--; 25545749Smckusick swt->st_usecnt--; 25645749Smckusick #endif 25745749Smckusick return(FALSE); 25845749Smckusick } 25945749Smckusick bzero((caddr_t)swp->sw_blocks, 26045749Smckusick swp->sw_nblocks * sizeof(*swp->sw_blocks)); 26145749Smckusick swp->sw_poip = 0; 26245749Smckusick if (handle) { 26345749Smckusick vm_object_t object; 26445749Smckusick 26545749Smckusick swp->sw_flags = SW_NAMED; 26645749Smckusick queue_enter(&swap_pager_list, pager, vm_pager_t, pg_list); 26745749Smckusick /* 26845749Smckusick * Consistant with other pagers: return with object 26945749Smckusick * referenced. Can't do this with handle == NULL 27045749Smckusick * since it might be the pageout daemon calling. 27145749Smckusick */ 27245749Smckusick object = vm_object_allocate(size); 27345749Smckusick vm_object_enter(object, pager); 27445749Smckusick vm_object_setpager(object, pager, 0, FALSE); 27545749Smckusick } else { 27645749Smckusick swp->sw_flags = 0; 27745749Smckusick queue_init(&pager->pg_list); 27845749Smckusick } 27945749Smckusick pager->pg_handle = handle; 28045749Smckusick pager->pg_ops = &swappagerops; 28145749Smckusick pager->pg_type = PG_SWAP; 28245749Smckusick pager->pg_data = (caddr_t)swp; 28345749Smckusick 28445749Smckusick #ifdef DEBUG 28545749Smckusick if (swpagerdebug & SDB_ALLOC) 28645749Smckusick printf("swpg_alloc: pg_data %x, %x of %x at %x\n", 28745749Smckusick swp, swp->sw_nblocks, swp->sw_bsize, swp->sw_blocks); 28845749Smckusick #endif 28945749Smckusick return(pager); 29045749Smckusick } 29145749Smckusick 29253341Sbostic static void 29345749Smckusick swap_pager_dealloc(pager) 29445749Smckusick vm_pager_t pager; 29545749Smckusick { 29645749Smckusick register int i; 29745749Smckusick register sw_blk_t bp; 29845749Smckusick register sw_pager_t swp; 29945749Smckusick struct swtab *swt; 30045749Smckusick int s; 30145749Smckusick 30245749Smckusick #ifdef DEBUG 30345749Smckusick /* save panic time state */ 30445749Smckusick if ((swpagerdebug & SDB_ANOMPANIC) && panicstr) 30545749Smckusick return; 30645749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOC)) 30745749Smckusick printf("swpg_dealloc(%x)\n", pager); 30845749Smckusick #endif 30945749Smckusick /* 31045749Smckusick * Remove from list right away so lookups will fail if we 31145749Smckusick * block for pageout completion. 31245749Smckusick */ 31345749Smckusick swp = (sw_pager_t) pager->pg_data; 31445749Smckusick if (swp->sw_flags & SW_NAMED) { 31545749Smckusick queue_remove(&swap_pager_list, pager, vm_pager_t, pg_list); 31645749Smckusick swp->sw_flags &= ~SW_NAMED; 31745749Smckusick } 31845749Smckusick #ifdef DEBUG 31945749Smckusick for (swt = swtab; swt->st_osize; swt++) 32045749Smckusick if (swp->sw_osize <= swt->st_osize) 32145749Smckusick break; 32245749Smckusick swt->st_inuse--; 32345749Smckusick #endif 32445749Smckusick 32545749Smckusick /* 32645749Smckusick * Wait for all pageouts to finish and remove 32745749Smckusick * all entries from cleaning list. 32845749Smckusick */ 32945749Smckusick s = splbio(); 33045749Smckusick while (swp->sw_poip) { 33145749Smckusick swp->sw_flags |= SW_WANTED; 33253341Sbostic assert_wait((int)swp, 0); 33345749Smckusick thread_block(); 33445749Smckusick } 33545749Smckusick splx(s); 33648386Skarels (void) swap_pager_clean(NULL, B_WRITE); 33745749Smckusick 33845749Smckusick /* 33945749Smckusick * Free left over swap blocks 34045749Smckusick */ 34145749Smckusick for (i = 0, bp = swp->sw_blocks; i < swp->sw_nblocks; i++, bp++) 34245749Smckusick if (bp->swb_block) { 34345749Smckusick #ifdef DEBUG 34445749Smckusick if (swpagerdebug & (SDB_ALLOCBLK|SDB_FULL)) 34545749Smckusick printf("swpg_dealloc: blk %x\n", 34645749Smckusick bp->swb_block); 34745749Smckusick #endif 34845749Smckusick rmfree(swapmap, swp->sw_bsize, bp->swb_block); 34945749Smckusick } 35045749Smckusick /* 35145749Smckusick * Free swap management resources 35245749Smckusick */ 35345749Smckusick free((caddr_t)swp->sw_blocks, M_VMPGDATA); 35445749Smckusick free((caddr_t)swp, M_VMPGDATA); 35545749Smckusick free((caddr_t)pager, M_VMPAGER); 35645749Smckusick } 35745749Smckusick 35853341Sbostic static int 35945749Smckusick swap_pager_getpage(pager, m, sync) 36045749Smckusick vm_pager_t pager; 36145749Smckusick vm_page_t m; 36245749Smckusick boolean_t sync; 36345749Smckusick { 36445749Smckusick #ifdef DEBUG 36545749Smckusick if (swpagerdebug & SDB_FOLLOW) 36645749Smckusick printf("swpg_getpage(%x, %x, %d)\n", pager, m, sync); 36745749Smckusick #endif 36845749Smckusick return(swap_pager_io((sw_pager_t)pager->pg_data, m, B_READ)); 36945749Smckusick } 37045749Smckusick 37153341Sbostic static int 37245749Smckusick swap_pager_putpage(pager, m, sync) 37345749Smckusick vm_pager_t pager; 37445749Smckusick vm_page_t m; 37545749Smckusick boolean_t sync; 37645749Smckusick { 37745749Smckusick int flags; 37845749Smckusick 37945749Smckusick #ifdef DEBUG 38045749Smckusick if (swpagerdebug & SDB_FOLLOW) 38145749Smckusick printf("swpg_putpage(%x, %x, %d)\n", pager, m, sync); 38245749Smckusick #endif 38348386Skarels if (pager == NULL) { 38448386Skarels (void) swap_pager_clean(NULL, B_WRITE); 38545749Smckusick return; 38645749Smckusick } 38745749Smckusick flags = B_WRITE; 38845749Smckusick if (!sync) 38945749Smckusick flags |= B_ASYNC; 39045749Smckusick return(swap_pager_io((sw_pager_t)pager->pg_data, m, flags)); 39145749Smckusick } 39245749Smckusick 39353341Sbostic static boolean_t 39445749Smckusick swap_pager_haspage(pager, offset) 39545749Smckusick vm_pager_t pager; 39645749Smckusick vm_offset_t offset; 39745749Smckusick { 39845749Smckusick register sw_pager_t swp; 39945749Smckusick register sw_blk_t swb; 40045749Smckusick int ix; 40145749Smckusick 40245749Smckusick #ifdef DEBUG 40345749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOCBLK)) 40445749Smckusick printf("swpg_haspage(%x, %x) ", pager, offset); 40545749Smckusick #endif 40645749Smckusick swp = (sw_pager_t) pager->pg_data; 40745749Smckusick ix = offset / dbtob(swp->sw_bsize); 40845749Smckusick if (swp->sw_blocks == NULL || ix >= swp->sw_nblocks) { 40945749Smckusick #ifdef DEBUG 41045749Smckusick if (swpagerdebug & (SDB_FAIL|SDB_FOLLOW|SDB_ALLOCBLK)) 41145749Smckusick printf("swpg_haspage: %x bad offset %x, ix %x\n", 41245749Smckusick swp->sw_blocks, offset, ix); 41345749Smckusick #endif 41445749Smckusick return(FALSE); 41545749Smckusick } 41645749Smckusick swb = &swp->sw_blocks[ix]; 41745749Smckusick if (swb->swb_block) 41845749Smckusick ix = atop(offset % dbtob(swp->sw_bsize)); 41945749Smckusick #ifdef DEBUG 42045749Smckusick if (swpagerdebug & SDB_ALLOCBLK) 42145749Smckusick printf("%x blk %x+%x ", swp->sw_blocks, swb->swb_block, ix); 42245749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOCBLK)) 42345749Smckusick printf("-> %c\n", 42445749Smckusick "FT"[swb->swb_block && (swb->swb_mask & (1 << ix))]); 42545749Smckusick #endif 42645749Smckusick if (swb->swb_block && (swb->swb_mask & (1 << ix))) 42745749Smckusick return(TRUE); 42845749Smckusick return(FALSE); 42945749Smckusick } 43045749Smckusick 43145749Smckusick /* 43245749Smckusick * Scaled down version of swap(). 43345749Smckusick * Assumes that PAGE_SIZE < MAXPHYS; i.e. only one operation needed. 43445749Smckusick * BOGUS: lower level IO routines expect a KVA so we have to map our 43545749Smckusick * provided physical page into the KVA to keep them happy. 43645749Smckusick */ 43753341Sbostic static int 43845749Smckusick swap_pager_io(swp, m, flags) 43945749Smckusick register sw_pager_t swp; 44045749Smckusick vm_page_t m; 44145749Smckusick int flags; 44245749Smckusick { 44345749Smckusick register struct buf *bp; 44445749Smckusick register sw_blk_t swb; 44545749Smckusick register int s; 44645749Smckusick int ix; 44745749Smckusick boolean_t rv; 44845749Smckusick vm_offset_t kva, off; 44945749Smckusick swp_clean_t spc; 45045749Smckusick 45145749Smckusick #ifdef DEBUG 45245749Smckusick /* save panic time state */ 45345749Smckusick if ((swpagerdebug & SDB_ANOMPANIC) && panicstr) 45453341Sbostic return (VM_PAGER_FAIL); /* XXX: correct return? */ 45545749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_IO)) 45645749Smckusick printf("swpg_io(%x, %x, %x)\n", swp, m, flags); 45745749Smckusick #endif 45845749Smckusick 45945749Smckusick /* 46045749Smckusick * For reads (pageins) and synchronous writes, we clean up 46149289Shibler * all completed async pageouts. 46245749Smckusick */ 46345749Smckusick if ((flags & B_ASYNC) == 0) { 46445749Smckusick s = splbio(); 46549289Shibler #ifdef DEBUG 46649289Shibler /* 46749289Shibler * Check to see if this page is currently being cleaned. 46849289Shibler * If it is, we just wait til the operation is done before 46949289Shibler * continuing. 47049289Shibler */ 47145749Smckusick while (swap_pager_clean(m, flags&B_READ)) { 47249289Shibler if (swpagerdebug & SDB_ANOM) 47349289Shibler printf("swap_pager_io: page %x cleaning\n", m); 47449289Shibler 47545749Smckusick swp->sw_flags |= SW_WANTED; 47653341Sbostic assert_wait((int)swp, 0); 47745749Smckusick thread_block(); 47845749Smckusick } 47949289Shibler #else 48049289Shibler (void) swap_pager_clean(m, flags&B_READ); 48149289Shibler #endif 48245749Smckusick splx(s); 48345749Smckusick } 48445749Smckusick /* 48545749Smckusick * For async writes (pageouts), we cleanup completed pageouts so 48645749Smckusick * that all available resources are freed. Also tells us if this 48745749Smckusick * page is already being cleaned. If it is, or no resources 48845749Smckusick * are available, we try again later. 48945749Smckusick */ 49049289Shibler else if (swap_pager_clean(m, B_WRITE) || 49149289Shibler queue_empty(&swap_pager_free)) { 49249289Shibler #ifdef DEBUG 49349289Shibler if ((swpagerdebug & SDB_ANOM) && 49449289Shibler !queue_empty(&swap_pager_free)) 49549289Shibler printf("swap_pager_io: page %x already cleaning\n", m); 49649289Shibler #endif 49745749Smckusick return(VM_PAGER_FAIL); 49849289Shibler } 49945749Smckusick 50045749Smckusick /* 50145749Smckusick * Determine swap block and allocate as necessary. 50245749Smckusick */ 50345749Smckusick off = m->offset + m->object->paging_offset; 50445749Smckusick ix = off / dbtob(swp->sw_bsize); 50545749Smckusick if (swp->sw_blocks == NULL || ix >= swp->sw_nblocks) { 50645749Smckusick #ifdef DEBUG 50745749Smckusick if (swpagerdebug & SDB_FAIL) 50845749Smckusick printf("swpg_io: bad offset %x+%x(%d) in %x\n", 50945749Smckusick m->offset, m->object->paging_offset, 51045749Smckusick ix, swp->sw_blocks); 51145749Smckusick #endif 51245749Smckusick return(VM_PAGER_FAIL); 51345749Smckusick } 51445749Smckusick swb = &swp->sw_blocks[ix]; 51545749Smckusick off = off % dbtob(swp->sw_bsize); 51645749Smckusick if (flags & B_READ) { 51745749Smckusick if (swb->swb_block == 0 || 51845749Smckusick (swb->swb_mask & (1 << atop(off))) == 0) { 51945749Smckusick #ifdef DEBUG 52045749Smckusick if (swpagerdebug & (SDB_ALLOCBLK|SDB_FAIL)) 52145749Smckusick printf("swpg_io: %x bad read: blk %x+%x, mask %x, off %x+%x\n", 52245749Smckusick swp->sw_blocks, 52345749Smckusick swb->swb_block, atop(off), 52445749Smckusick swb->swb_mask, 52545749Smckusick m->offset, m->object->paging_offset); 52645749Smckusick #endif 52745749Smckusick /* XXX: should we zero page here?? */ 52845749Smckusick return(VM_PAGER_FAIL); 52945749Smckusick } 53045749Smckusick } else if (swb->swb_block == 0) { 53145749Smckusick swb->swb_block = rmalloc(swapmap, swp->sw_bsize); 53245749Smckusick if (swb->swb_block == 0) { 53345749Smckusick #ifdef DEBUG 53445749Smckusick if (swpagerdebug & SDB_FAIL) 53545749Smckusick printf("swpg_io: rmalloc of %x failed\n", 53645749Smckusick swp->sw_bsize); 53745749Smckusick #endif 53845749Smckusick return(VM_PAGER_FAIL); 53945749Smckusick } 54045749Smckusick #ifdef DEBUG 54145749Smckusick if (swpagerdebug & (SDB_FULL|SDB_ALLOCBLK)) 54245749Smckusick printf("swpg_io: %x alloc blk %x at ix %x\n", 54345749Smckusick swp->sw_blocks, swb->swb_block, ix); 54445749Smckusick #endif 54545749Smckusick } 54645749Smckusick 54745749Smckusick /* 54845749Smckusick * Allocate a kernel virtual address and initialize so that PTE 54945749Smckusick * is available for lower level IO drivers. 55045749Smckusick */ 55145749Smckusick kva = vm_pager_map_page(m); 55245749Smckusick 55345749Smckusick /* 55445749Smckusick * Get a swap buffer header and perform the IO 55545749Smckusick */ 55645749Smckusick s = splbio(); 55745749Smckusick while (bswlist.av_forw == NULL) { 55845749Smckusick #ifdef DEBUG 55945749Smckusick if (swpagerdebug & SDB_ANOM) 56049289Shibler printf("swap_pager_io: wait on swbuf for %x (%d)\n", 56145749Smckusick m, flags); 56245749Smckusick #endif 56345749Smckusick bswlist.b_flags |= B_WANTED; 56445749Smckusick sleep((caddr_t)&bswlist, PSWP+1); 56545749Smckusick } 56645749Smckusick bp = bswlist.av_forw; 56745749Smckusick bswlist.av_forw = bp->av_forw; 56845749Smckusick splx(s); 56945749Smckusick bp->b_flags = B_BUSY | (flags & B_READ); 57048386Skarels bp->b_proc = &proc0; /* XXX (but without B_PHYS set this is ok) */ 57145749Smckusick bp->b_un.b_addr = (caddr_t)kva; 57245749Smckusick bp->b_blkno = swb->swb_block + btodb(off); 57345749Smckusick VHOLD(swapdev_vp); 57445749Smckusick bp->b_vp = swapdev_vp; 57546985Smckusick if (swapdev_vp->v_type == VBLK) 57646985Smckusick bp->b_dev = swapdev_vp->v_rdev; 57745749Smckusick bp->b_bcount = PAGE_SIZE; 57853213Smckusick if ((bp->b_flags & B_READ) == 0) { 57953213Smckusick bp->b_dirtyoff = 0; 58053213Smckusick bp->b_dirtyend = PAGE_SIZE; 58145749Smckusick swapdev_vp->v_numoutput++; 58253213Smckusick } 58345749Smckusick 58445749Smckusick /* 58545749Smckusick * If this is an async write we set up additional buffer fields 58645749Smckusick * and place a "cleaning" entry on the inuse queue. 58745749Smckusick */ 58845749Smckusick if ((flags & (B_READ|B_ASYNC)) == B_ASYNC) { 58945749Smckusick #ifdef DEBUG 59045749Smckusick if (queue_empty(&swap_pager_free)) 59145749Smckusick panic("swpg_io: lost spc"); 59245749Smckusick #endif 59345749Smckusick queue_remove_first(&swap_pager_free, 59445749Smckusick spc, swp_clean_t, spc_list); 59545749Smckusick #ifdef DEBUG 59645749Smckusick if (spc->spc_flags != SPC_FREE) 59745749Smckusick panic("swpg_io: bad free spc"); 59845749Smckusick #endif 59945749Smckusick spc->spc_flags = SPC_BUSY; 60045749Smckusick spc->spc_bp = bp; 60145749Smckusick spc->spc_swp = swp; 60245749Smckusick spc->spc_kva = kva; 60345749Smckusick spc->spc_m = m; 60445749Smckusick bp->b_flags |= B_CALL; 60545749Smckusick bp->b_iodone = swap_pager_iodone; 60645749Smckusick s = splbio(); 60745749Smckusick swp->sw_poip++; 60845749Smckusick queue_enter(&swap_pager_inuse, spc, swp_clean_t, spc_list); 60945749Smckusick 61045749Smckusick #ifdef DEBUG 61145749Smckusick swap_pager_poip++; 61245749Smckusick if (swpagerdebug & SDB_WRITE) 61345749Smckusick printf("swpg_io: write: bp=%x swp=%x spc=%x poip=%d\n", 61445749Smckusick bp, swp, spc, swp->sw_poip); 61545749Smckusick if ((swpagerdebug & SDB_ALLOCBLK) && 61645749Smckusick (swb->swb_mask & (1 << atop(off))) == 0) 61745749Smckusick printf("swpg_io: %x write blk %x+%x\n", 61845749Smckusick swp->sw_blocks, swb->swb_block, atop(off)); 61945749Smckusick #endif 62045749Smckusick swb->swb_mask |= (1 << atop(off)); 62145749Smckusick splx(s); 62245749Smckusick } 62345749Smckusick #ifdef DEBUG 62445749Smckusick if (swpagerdebug & SDB_IO) 62545749Smckusick printf("swpg_io: IO start: bp %x, db %x, va %x, pa %x\n", 62645749Smckusick bp, swb->swb_block+btodb(off), kva, VM_PAGE_TO_PHYS(m)); 62745749Smckusick #endif 62845749Smckusick VOP_STRATEGY(bp); 62945749Smckusick if ((flags & (B_READ|B_ASYNC)) == B_ASYNC) { 63045749Smckusick #ifdef DEBUG 63145749Smckusick if (swpagerdebug & SDB_IO) 63245749Smckusick printf("swpg_io: IO started: bp %x\n", bp); 63345749Smckusick #endif 63445749Smckusick return(VM_PAGER_PEND); 63545749Smckusick } 63645749Smckusick s = splbio(); 63745749Smckusick #ifdef DEBUG 63845749Smckusick if (flags & B_READ) 63945749Smckusick swap_pager_piip++; 64045749Smckusick else 64145749Smckusick swap_pager_poip++; 64245749Smckusick #endif 64345749Smckusick while ((bp->b_flags & B_DONE) == 0) { 64453341Sbostic assert_wait((int)bp, 0); 64545749Smckusick thread_block(); 64645749Smckusick } 64745749Smckusick #ifdef DEBUG 64845749Smckusick if (flags & B_READ) 64945749Smckusick --swap_pager_piip; 65045749Smckusick else 65145749Smckusick --swap_pager_poip; 65245749Smckusick #endif 65345749Smckusick rv = (bp->b_flags & B_ERROR) ? VM_PAGER_FAIL : VM_PAGER_OK; 65445749Smckusick bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 65545749Smckusick bp->av_forw = bswlist.av_forw; 65645749Smckusick bswlist.av_forw = bp; 65745749Smckusick if (bp->b_vp) 65845749Smckusick brelvp(bp); 65945749Smckusick if (bswlist.b_flags & B_WANTED) { 66045749Smckusick bswlist.b_flags &= ~B_WANTED; 66145749Smckusick thread_wakeup((int)&bswlist); 66245749Smckusick } 66345749Smckusick if ((flags & B_READ) == 0 && rv == VM_PAGER_OK) { 66449289Shibler m->clean = TRUE; 66545749Smckusick pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 66645749Smckusick } 66745749Smckusick splx(s); 66845749Smckusick #ifdef DEBUG 66945749Smckusick if (swpagerdebug & SDB_IO) 67045749Smckusick printf("swpg_io: IO done: bp %x, rv %d\n", bp, rv); 67145749Smckusick if ((swpagerdebug & SDB_FAIL) && rv == VM_PAGER_FAIL) 67245749Smckusick printf("swpg_io: IO error\n"); 67345749Smckusick #endif 67445749Smckusick vm_pager_unmap_page(kva); 67545749Smckusick return(rv); 67645749Smckusick } 67745749Smckusick 67853341Sbostic static boolean_t 67945749Smckusick swap_pager_clean(m, rw) 68045749Smckusick vm_page_t m; 68145749Smckusick int rw; 68245749Smckusick { 68345749Smckusick register swp_clean_t spc, tspc; 68445749Smckusick register int s; 68545749Smckusick 68645749Smckusick #ifdef DEBUG 68745749Smckusick /* save panic time state */ 68845749Smckusick if ((swpagerdebug & SDB_ANOMPANIC) && panicstr) 68945749Smckusick return; 69045749Smckusick if (swpagerdebug & SDB_FOLLOW) 69145749Smckusick printf("swpg_clean(%x, %d)\n", m, rw); 69245749Smckusick #endif 69348386Skarels tspc = NULL; 69445749Smckusick for (;;) { 69545749Smckusick /* 69645749Smckusick * Look up and removal from inuse list must be done 69745749Smckusick * at splbio() to avoid conflicts with swap_pager_iodone. 69845749Smckusick */ 69945749Smckusick s = splbio(); 70045749Smckusick spc = (swp_clean_t) queue_first(&swap_pager_inuse); 70145749Smckusick while (!queue_end(&swap_pager_inuse, (queue_entry_t)spc)) { 70245749Smckusick if ((spc->spc_flags & SPC_DONE) && 70345749Smckusick swap_pager_finish(spc)) { 70445749Smckusick queue_remove(&swap_pager_inuse, spc, 70545749Smckusick swp_clean_t, spc_list); 70645749Smckusick break; 70745749Smckusick } 70845749Smckusick if (m && m == spc->spc_m) { 70945749Smckusick #ifdef DEBUG 71045749Smckusick if (swpagerdebug & SDB_ANOM) 71149289Shibler printf("swap_pager_clean: page %x on list, flags %x\n", 71245749Smckusick m, spc->spc_flags); 71345749Smckusick #endif 71445749Smckusick tspc = spc; 71545749Smckusick } 71645749Smckusick spc = (swp_clean_t) queue_next(&spc->spc_list); 71745749Smckusick } 71845749Smckusick 71945749Smckusick /* 72045749Smckusick * No operations done, thats all we can do for now. 72145749Smckusick */ 72245749Smckusick if (queue_end(&swap_pager_inuse, (queue_entry_t)spc)) 72345749Smckusick break; 72445749Smckusick splx(s); 72545749Smckusick 72645749Smckusick /* 72745749Smckusick * The desired page was found to be busy earlier in 72845749Smckusick * the scan but has since completed. 72945749Smckusick */ 73045749Smckusick if (tspc && tspc == spc) { 73145749Smckusick #ifdef DEBUG 73245749Smckusick if (swpagerdebug & SDB_ANOM) 73349289Shibler printf("swap_pager_clean: page %x done while looking\n", 73445749Smckusick m); 73545749Smckusick #endif 73648386Skarels tspc = NULL; 73745749Smckusick } 73845749Smckusick spc->spc_flags = SPC_FREE; 73945749Smckusick vm_pager_unmap_page(spc->spc_kva); 74045749Smckusick queue_enter(&swap_pager_free, spc, swp_clean_t, spc_list); 74145749Smckusick #ifdef DEBUG 74245749Smckusick if (swpagerdebug & SDB_WRITE) 74345749Smckusick printf("swpg_clean: free spc %x\n", spc); 74445749Smckusick #endif 74545749Smckusick } 74649289Shibler #ifdef DEBUG 74745749Smckusick /* 74845749Smckusick * If we found that the desired page is already being cleaned 74945749Smckusick * mark it so that swap_pager_iodone() will not set the clean 75045749Smckusick * flag before the pageout daemon has another chance to clean it. 75145749Smckusick */ 75245749Smckusick if (tspc && rw == B_WRITE) { 75345749Smckusick if (swpagerdebug & SDB_ANOM) 75449289Shibler printf("swap_pager_clean: page %x on clean list\n", 75549289Shibler tspc); 75645749Smckusick tspc->spc_flags |= SPC_DIRTY; 75745749Smckusick } 75849289Shibler #endif 75945749Smckusick splx(s); 76045749Smckusick 76145749Smckusick #ifdef DEBUG 76245749Smckusick if (swpagerdebug & SDB_WRITE) 76345749Smckusick printf("swpg_clean: return %d\n", tspc ? TRUE : FALSE); 76445749Smckusick if ((swpagerdebug & SDB_ANOM) && tspc) 76545749Smckusick printf("swpg_clean: %s of cleaning page %x\n", 76645749Smckusick rw == B_READ ? "get" : "put", m); 76745749Smckusick #endif 76845749Smckusick return(tspc ? TRUE : FALSE); 76945749Smckusick } 77045749Smckusick 77153341Sbostic static int 77245749Smckusick swap_pager_finish(spc) 77345749Smckusick register swp_clean_t spc; 77445749Smckusick { 77545749Smckusick vm_object_t object = spc->spc_m->object; 77645749Smckusick 77745749Smckusick /* 77845749Smckusick * Mark the paging operation as done. 77945749Smckusick * (XXX) If we cannot get the lock, leave it til later. 78045749Smckusick * (XXX) Also we are assuming that an async write is a 78145749Smckusick * pageout operation that has incremented the counter. 78245749Smckusick */ 78345749Smckusick if (!vm_object_lock_try(object)) 78445749Smckusick return(0); 78545749Smckusick 78645749Smckusick if (--object->paging_in_progress == 0) 78745749Smckusick thread_wakeup((int) object); 78845749Smckusick 78949289Shibler #ifdef DEBUG 79045749Smckusick /* 79145749Smckusick * XXX: this isn't even close to the right thing to do, 79245749Smckusick * introduces a variety of race conditions. 79345749Smckusick * 79445749Smckusick * If dirty, vm_pageout() has attempted to clean the page 79545749Smckusick * again. In this case we do not do anything as we will 79649289Shibler * see the page again shortly. 79745749Smckusick */ 79849289Shibler if (spc->spc_flags & SPC_DIRTY) { 79949289Shibler if (swpagerdebug & SDB_ANOM) 80049289Shibler printf("swap_pager_finish: page %x dirty again\n", 80149289Shibler spc->spc_m); 80249289Shibler spc->spc_m->busy = FALSE; 80349289Shibler PAGE_WAKEUP(spc->spc_m); 80449289Shibler vm_object_unlock(object); 80549289Shibler return(1); 80645749Smckusick } 80749289Shibler #endif 80845749Smckusick /* 80949289Shibler * If no error mark as clean and inform the pmap system. 81049289Shibler * If error, mark as dirty so we will try again. 81149289Shibler * (XXX could get stuck doing this, should give up after awhile) 81245749Smckusick */ 81349289Shibler if (spc->spc_flags & SPC_ERROR) { 81449289Shibler printf("swap_pager_finish: clean of page %x failed\n", 81549289Shibler VM_PAGE_TO_PHYS(spc->spc_m)); 81649289Shibler spc->spc_m->laundry = TRUE; 81749289Shibler } else { 81849289Shibler spc->spc_m->clean = TRUE; 81949289Shibler pmap_clear_modify(VM_PAGE_TO_PHYS(spc->spc_m)); 82049289Shibler } 82149289Shibler spc->spc_m->busy = FALSE; 82245749Smckusick PAGE_WAKEUP(spc->spc_m); 82345749Smckusick 82445749Smckusick vm_object_unlock(object); 82545749Smckusick return(1); 82645749Smckusick } 82745749Smckusick 82853341Sbostic static void 82945749Smckusick swap_pager_iodone(bp) 83045749Smckusick register struct buf *bp; 83145749Smckusick { 83245749Smckusick register swp_clean_t spc; 83345749Smckusick daddr_t blk; 83445749Smckusick int s; 83545749Smckusick 83645749Smckusick #ifdef DEBUG 83745749Smckusick /* save panic time state */ 83845749Smckusick if ((swpagerdebug & SDB_ANOMPANIC) && panicstr) 83945749Smckusick return; 84045749Smckusick if (swpagerdebug & SDB_FOLLOW) 84145749Smckusick printf("swpg_iodone(%x)\n", bp); 84245749Smckusick #endif 84345749Smckusick s = splbio(); 84445749Smckusick spc = (swp_clean_t) queue_first(&swap_pager_inuse); 84545749Smckusick while (!queue_end(&swap_pager_inuse, (queue_entry_t)spc)) { 84645749Smckusick if (spc->spc_bp == bp) 84745749Smckusick break; 84845749Smckusick spc = (swp_clean_t) queue_next(&spc->spc_list); 84945749Smckusick } 85045749Smckusick #ifdef DEBUG 85145749Smckusick if (queue_end(&swap_pager_inuse, (queue_entry_t)spc)) 85249289Shibler panic("swap_pager_iodone: bp not found"); 85345749Smckusick #endif 85445749Smckusick 85545749Smckusick spc->spc_flags &= ~SPC_BUSY; 85645749Smckusick spc->spc_flags |= SPC_DONE; 85745749Smckusick if (bp->b_flags & B_ERROR) 85845749Smckusick spc->spc_flags |= SPC_ERROR; 85945749Smckusick spc->spc_bp = NULL; 86045749Smckusick blk = bp->b_blkno; 86145749Smckusick 86245749Smckusick #ifdef DEBUG 86345749Smckusick --swap_pager_poip; 86445749Smckusick if (swpagerdebug & SDB_WRITE) 86545749Smckusick printf("swpg_iodone: bp=%x swp=%x flags=%x spc=%x poip=%x\n", 86645749Smckusick bp, spc->spc_swp, spc->spc_swp->sw_flags, 86745749Smckusick spc, spc->spc_swp->sw_poip); 86845749Smckusick #endif 86945749Smckusick 87045749Smckusick spc->spc_swp->sw_poip--; 87145749Smckusick if (spc->spc_swp->sw_flags & SW_WANTED) { 87245749Smckusick spc->spc_swp->sw_flags &= ~SW_WANTED; 87345749Smckusick thread_wakeup((int)spc->spc_swp); 87445749Smckusick } 87545749Smckusick 87645749Smckusick bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 87745749Smckusick bp->av_forw = bswlist.av_forw; 87845749Smckusick bswlist.av_forw = bp; 87945749Smckusick if (bp->b_vp) 88045749Smckusick brelvp(bp); 88145749Smckusick if (bswlist.b_flags & B_WANTED) { 88245749Smckusick bswlist.b_flags &= ~B_WANTED; 88345749Smckusick thread_wakeup((int)&bswlist); 88445749Smckusick } 88545749Smckusick thread_wakeup((int) &vm_pages_needed); 88645749Smckusick splx(s); 88745749Smckusick } 88845749Smckusick #endif 889