10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * VM - page locking primitives 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate #include <sys/param.h> 330Sstevel@tonic-gate #include <sys/t_lock.h> 340Sstevel@tonic-gate #include <sys/vtrace.h> 350Sstevel@tonic-gate #include <sys/debug.h> 360Sstevel@tonic-gate #include <sys/cmn_err.h> 370Sstevel@tonic-gate #include <sys/vnode.h> 380Sstevel@tonic-gate #include <sys/bitmap.h> 390Sstevel@tonic-gate #include <sys/lockstat.h> 400Sstevel@tonic-gate #include <sys/condvar_impl.h> 410Sstevel@tonic-gate #include <vm/page.h> 420Sstevel@tonic-gate #include <vm/seg_enum.h> 430Sstevel@tonic-gate #include <vm/vm_dep.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate /* 460Sstevel@tonic-gate * This global mutex is for logical page locking. 470Sstevel@tonic-gate * The following fields in the page structure are protected 480Sstevel@tonic-gate * by this lock: 490Sstevel@tonic-gate * 500Sstevel@tonic-gate * p_lckcnt 510Sstevel@tonic-gate * p_cowcnt 520Sstevel@tonic-gate */ 530Sstevel@tonic-gate kmutex_t page_llock; 540Sstevel@tonic-gate 550Sstevel@tonic-gate /* 560Sstevel@tonic-gate * This is a global lock for the logical page free list. The 570Sstevel@tonic-gate * logical free list, in this implementation, is maintained as two 580Sstevel@tonic-gate * separate physical lists - the cache list and the free list. 590Sstevel@tonic-gate */ 600Sstevel@tonic-gate kmutex_t page_freelock; 610Sstevel@tonic-gate 620Sstevel@tonic-gate /* 630Sstevel@tonic-gate * The hash table, page_hash[], the p_selock fields, and the 640Sstevel@tonic-gate * list of pages associated with vnodes are protected by arrays of mutexes. 650Sstevel@tonic-gate * 660Sstevel@tonic-gate * Unless the hashes are changed radically, the table sizes must be 670Sstevel@tonic-gate * a power of two. Also, we typically need more mutexes for the 680Sstevel@tonic-gate * vnodes since these locks are occasionally held for long periods. 690Sstevel@tonic-gate * And since there seem to be two special vnodes (kvp and swapvp), 700Sstevel@tonic-gate * we make room for private mutexes for them. 710Sstevel@tonic-gate * 720Sstevel@tonic-gate * The pse_mutex[] array holds the mutexes to protect the p_selock 730Sstevel@tonic-gate * fields of all page_t structures. 740Sstevel@tonic-gate * 750Sstevel@tonic-gate * PAGE_SE_MUTEX(pp) returns the address of the appropriate mutex 760Sstevel@tonic-gate * when given a pointer to a page_t. 770Sstevel@tonic-gate * 780Sstevel@tonic-gate * PSE_TABLE_SIZE must be a power of two. One could argue that we 790Sstevel@tonic-gate * should go to the trouble of setting it up at run time and base it 800Sstevel@tonic-gate * on memory size rather than the number of compile time CPUs. 810Sstevel@tonic-gate * 820Sstevel@tonic-gate * XX64 We should be using physmem size to calculate PSE_TABLE_SIZE, 830Sstevel@tonic-gate * PSE_SHIFT, PIO_SHIFT. 840Sstevel@tonic-gate * 850Sstevel@tonic-gate * These might break in 64 bit world. 860Sstevel@tonic-gate */ 870Sstevel@tonic-gate #define PSE_SHIFT 7 /* log2(PSE_TABLE_SIZE) */ 880Sstevel@tonic-gate 890Sstevel@tonic-gate #define PSE_TABLE_SIZE 128 /* number of mutexes to have */ 900Sstevel@tonic-gate 910Sstevel@tonic-gate #define PIO_SHIFT PSE_SHIFT /* next power of 2 bigger than page_t */ 920Sstevel@tonic-gate #define PIO_TABLE_SIZE PSE_TABLE_SIZE /* number of io mutexes to have */ 930Sstevel@tonic-gate 940Sstevel@tonic-gate pad_mutex_t ph_mutex[PH_TABLE_SIZE]; 950Sstevel@tonic-gate pad_mutex_t pse_mutex[PSE_TABLE_SIZE]; 960Sstevel@tonic-gate kmutex_t pio_mutex[PIO_TABLE_SIZE]; 970Sstevel@tonic-gate 980Sstevel@tonic-gate #define PAGE_SE_MUTEX(pp) \ 990Sstevel@tonic-gate &pse_mutex[((((uintptr_t)(pp) >> PSE_SHIFT) ^ \ 1000Sstevel@tonic-gate ((uintptr_t)(pp) >> (PSE_SHIFT << 1))) & \ 1010Sstevel@tonic-gate (PSE_TABLE_SIZE - 1))].pad_mutex 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate #define PAGE_IO_MUTEX(pp) \ 1040Sstevel@tonic-gate &pio_mutex[(((uintptr_t)pp) >> PIO_SHIFT) & (PIO_TABLE_SIZE - 1)] 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate #define PSZC_MTX_TABLE_SIZE 128 1070Sstevel@tonic-gate #define PSZC_MTX_TABLE_SHIFT 7 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate static pad_mutex_t pszc_mutex[PSZC_MTX_TABLE_SIZE]; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate #define PAGE_SZC_MUTEX(_pp) \ 1120Sstevel@tonic-gate &pszc_mutex[((((uintptr_t)(_pp) >> PSZC_MTX_TABLE_SHIFT) ^ \ 1130Sstevel@tonic-gate ((uintptr_t)(_pp) >> (PSZC_MTX_TABLE_SHIFT << 1)) ^ \ 1140Sstevel@tonic-gate ((uintptr_t)(_pp) >> (3 * PSZC_MTX_TABLE_SHIFT))) & \ 1150Sstevel@tonic-gate (PSZC_MTX_TABLE_SIZE - 1))].pad_mutex 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /* 1180Sstevel@tonic-gate * The vph_mutex[] array holds the mutexes to protect the vnode chains, 1190Sstevel@tonic-gate * (i.e., the list of pages anchored by v_pages and connected via p_vpprev 1200Sstevel@tonic-gate * and p_vpnext). 1210Sstevel@tonic-gate * 1220Sstevel@tonic-gate * The page_vnode_mutex(vp) function returns the address of the appropriate 1230Sstevel@tonic-gate * mutex from this array given a pointer to a vnode. It is complicated 1240Sstevel@tonic-gate * by the fact that the kernel's vnode and the swapfs vnode are referenced 1250Sstevel@tonic-gate * frequently enough to warrent their own mutexes. 1260Sstevel@tonic-gate * 1270Sstevel@tonic-gate * The VP_HASH_FUNC returns the index into the vph_mutex array given 1280Sstevel@tonic-gate * an address of a vnode. 1290Sstevel@tonic-gate */ 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate /* 1320Sstevel@tonic-gate * XX64 VPH_TABLE_SIZE and VP_HASH_FUNC might break in 64 bit world. 1330Sstevel@tonic-gate * Need to review again. 1340Sstevel@tonic-gate */ 1350Sstevel@tonic-gate #define VPH_TABLE_SIZE (2 << VP_SHIFT) 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate #define VP_HASH_FUNC(vp) \ 1380Sstevel@tonic-gate ((((uintptr_t)(vp) >> 6) + \ 1390Sstevel@tonic-gate ((uintptr_t)(vp) >> 8) + \ 1400Sstevel@tonic-gate ((uintptr_t)(vp) >> 10) + \ 1410Sstevel@tonic-gate ((uintptr_t)(vp) >> 12)) \ 1420Sstevel@tonic-gate & (VPH_TABLE_SIZE - 1)) 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate extern struct vnode kvp; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate kmutex_t vph_mutex[VPH_TABLE_SIZE + 2]; 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate /* 1490Sstevel@tonic-gate * Initialize the locks used by the Virtual Memory Management system. 1500Sstevel@tonic-gate */ 1510Sstevel@tonic-gate void 1520Sstevel@tonic-gate page_lock_init() 1530Sstevel@tonic-gate { 1540Sstevel@tonic-gate } 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate /* 1570Sstevel@tonic-gate * At present we only use page ownership to aid debugging, so it's 1580Sstevel@tonic-gate * OK if the owner field isn't exact. In the 32-bit world two thread ids 1590Sstevel@tonic-gate * can map to the same owner because we just 'or' in 0x80000000 and 1600Sstevel@tonic-gate * then clear the second highest bit, so that (for example) 0x2faced00 1610Sstevel@tonic-gate * and 0xafaced00 both map to 0xafaced00. 1620Sstevel@tonic-gate * In the 64-bit world, p_selock may not be large enough to hold a full 1630Sstevel@tonic-gate * thread pointer. If we ever need precise ownership (e.g. if we implement 1640Sstevel@tonic-gate * priority inheritance for page locks) then p_selock should become a 1650Sstevel@tonic-gate * uintptr_t and SE_WRITER should be -((uintptr_t)curthread >> 2). 1660Sstevel@tonic-gate */ 1670Sstevel@tonic-gate #define SE_WRITER (((selock_t)(ulong_t)curthread | INT_MIN) & ~SE_EWANTED) 1680Sstevel@tonic-gate #define SE_READER 1 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate /* 1710Sstevel@tonic-gate * A page that is deleted must be marked as such using the 1720Sstevel@tonic-gate * page_lock_delete() function. The page must be exclusively locked. 1730Sstevel@tonic-gate * The SE_DELETED marker is put in p_selock when this function is called. 1740Sstevel@tonic-gate * SE_DELETED must be distinct from any SE_WRITER value. 1750Sstevel@tonic-gate */ 1760Sstevel@tonic-gate #define SE_DELETED (1 | INT_MIN) 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate #ifdef VM_STATS 1790Sstevel@tonic-gate uint_t vph_kvp_count; 1800Sstevel@tonic-gate uint_t vph_swapfsvp_count; 1810Sstevel@tonic-gate uint_t vph_other; 1820Sstevel@tonic-gate #endif /* VM_STATS */ 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate #ifdef VM_STATS 1850Sstevel@tonic-gate uint_t page_lock_count; 1860Sstevel@tonic-gate uint_t page_lock_miss; 1870Sstevel@tonic-gate uint_t page_lock_miss_lock; 1880Sstevel@tonic-gate uint_t page_lock_reclaim; 1890Sstevel@tonic-gate uint_t page_lock_bad_reclaim; 1900Sstevel@tonic-gate uint_t page_lock_same_page; 1910Sstevel@tonic-gate uint_t page_lock_upgrade; 192*917Selowe uint_t page_lock_retired; 1930Sstevel@tonic-gate uint_t page_lock_upgrade_failed; 1940Sstevel@tonic-gate uint_t page_lock_deleted; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate uint_t page_trylock_locked; 197*917Selowe uint_t page_trylock_failed; 1980Sstevel@tonic-gate uint_t page_trylock_missed; 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate uint_t page_try_reclaim_upgrade; 2010Sstevel@tonic-gate #endif /* VM_STATS */ 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate /* 2040Sstevel@tonic-gate * Acquire the "shared/exclusive" lock on a page. 2050Sstevel@tonic-gate * 2060Sstevel@tonic-gate * Returns 1 on success and locks the page appropriately. 2070Sstevel@tonic-gate * 0 on failure and does not lock the page. 2080Sstevel@tonic-gate * 2090Sstevel@tonic-gate * If `lock' is non-NULL, it will be dropped and reacquired in the 2100Sstevel@tonic-gate * failure case. This routine can block, and if it does 2110Sstevel@tonic-gate * it will always return a failure since the page identity [vp, off] 2120Sstevel@tonic-gate * or state may have changed. 2130Sstevel@tonic-gate */ 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate int 2160Sstevel@tonic-gate page_lock(page_t *pp, se_t se, kmutex_t *lock, reclaim_t reclaim) 2170Sstevel@tonic-gate { 2180Sstevel@tonic-gate return (page_lock_es(pp, se, lock, reclaim, 0)); 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate /* 2220Sstevel@tonic-gate * With the addition of reader-writer lock semantics to page_lock_es, 2230Sstevel@tonic-gate * callers wanting an exclusive (writer) lock may prevent shared-lock 2240Sstevel@tonic-gate * (reader) starvation by setting the es parameter to SE_EXCL_WANTED. 2250Sstevel@tonic-gate * In this case, when an exclusive lock cannot be acquired, p_selock's 226*917Selowe * SE_EWANTED bit is set. Shared-lock (reader) requests are also denied 227*917Selowe * if the page is slated for retirement. 228*917Selowe * 229*917Selowe * The se and es parameters determine if the lock should be granted 230*917Selowe * based on the following decision table: 231*917Selowe * 232*917Selowe * Lock wanted es flags p_selock/SE_EWANTED Action 233*917Selowe * ----------- -------------- ------------------- --------- 234*917Selowe * SE_EXCL any [1][2] unlocked/any grant lock, clear SE_EWANTED 235*917Selowe * SE_EXCL SE_EWANTED any lock/any deny, set SE_EWANTED 236*917Selowe * SE_EXCL none any lock/any deny 237*917Selowe * SE_SHARED n/a [2][3] shared/0 grant 238*917Selowe * SE_SHARED n/a [2][3] unlocked/0 grant 239*917Selowe * SE_SHARED n/a shared/1 deny 240*917Selowe * SE_SHARED n/a unlocked/1 deny 241*917Selowe * SE_SHARED n/a excl/any deny 2420Sstevel@tonic-gate * 243*917Selowe * Notes: 244*917Selowe * [1] The code grants an exclusive lock to the caller and clears the bit 245*917Selowe * SE_EWANTED whenever p_selock is unlocked, regardless of the SE_EWANTED 246*917Selowe * bit's value. This was deemed acceptable as we are not concerned about 247*917Selowe * exclusive-lock starvation. If this ever becomes an issue, a priority or 248*917Selowe * fifo mechanism should also be implemented. Meantime, the thread that 249*917Selowe * set SE_EWANTED should be prepared to catch this condition and reset it 250*917Selowe * 251*917Selowe * [2] Retired pages may not be locked at any time, regardless of the 252*917Selowe * dispostion of se, unless the es parameter has SE_RETIRED flag set. 2530Sstevel@tonic-gate * 254*917Selowe * [3] If the page is slated for retirement the lock is denied. 255*917Selowe * 256*917Selowe * Notes on values of "es": 257*917Selowe * 258*917Selowe * es & 1: page_lookup_create will attempt page relocation 259*917Selowe * es & SE_EXCL_WANTED: caller wants SE_EWANTED set (eg. delete 260*917Selowe * memory thread); this prevents reader-starvation of waiting 261*917Selowe * writer thread(s) by giving priority to writers over readers. 262*917Selowe * es & SE_RETIRED: caller wants to lock pages even if they are 263*917Selowe * retired. Default is to deny the lock if the page is retired. 264*917Selowe * 265*917Selowe * And yes, we know, the semantics of this function are too complicated. 266*917Selowe * It's on the list to be cleaned up. 2670Sstevel@tonic-gate */ 2680Sstevel@tonic-gate int 2690Sstevel@tonic-gate page_lock_es(page_t *pp, se_t se, kmutex_t *lock, reclaim_t reclaim, int es) 2700Sstevel@tonic-gate { 2710Sstevel@tonic-gate int retval; 2720Sstevel@tonic-gate kmutex_t *pse = PAGE_SE_MUTEX(pp); 2730Sstevel@tonic-gate int upgraded; 2740Sstevel@tonic-gate int reclaim_it; 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate ASSERT(lock != NULL ? MUTEX_HELD(lock) : 1); 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate VM_STAT_ADD(page_lock_count); 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate upgraded = 0; 2810Sstevel@tonic-gate reclaim_it = 0; 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate mutex_enter(pse); 2840Sstevel@tonic-gate 285*917Selowe ASSERT(((es & SE_EXCL_WANTED) == 0) || 286*917Selowe ((es & SE_EXCL_WANTED) && (se == SE_EXCL))); 2870Sstevel@tonic-gate 288*917Selowe if (PP_RETIRED(pp) && !(es & SE_RETIRED)) { 289*917Selowe mutex_exit(pse); 290*917Selowe VM_STAT_ADD(page_lock_retired); 291*917Selowe return (0); 292*917Selowe } 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate if (se == SE_SHARED && es == 1 && pp->p_selock == 0) { 2950Sstevel@tonic-gate se = SE_EXCL; 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate if ((reclaim == P_RECLAIM) && (PP_ISFREE(pp))) { 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate reclaim_it = 1; 3010Sstevel@tonic-gate if (se == SE_SHARED) { 3020Sstevel@tonic-gate /* 3030Sstevel@tonic-gate * This is an interesting situation. 3040Sstevel@tonic-gate * 3050Sstevel@tonic-gate * Remember that p_free can only change if 3060Sstevel@tonic-gate * p_selock < 0. 3070Sstevel@tonic-gate * p_free does not depend on our holding `pse'. 3080Sstevel@tonic-gate * And, since we hold `pse', p_selock can not change. 3090Sstevel@tonic-gate * So, if p_free changes on us, the page is already 3100Sstevel@tonic-gate * exclusively held, and we would fail to get p_selock 3110Sstevel@tonic-gate * regardless. 3120Sstevel@tonic-gate * 3130Sstevel@tonic-gate * We want to avoid getting the share 3140Sstevel@tonic-gate * lock on a free page that needs to be reclaimed. 3150Sstevel@tonic-gate * It is possible that some other thread has the share 3160Sstevel@tonic-gate * lock and has left the free page on the cache list. 3170Sstevel@tonic-gate * pvn_vplist_dirty() does this for brief periods. 3180Sstevel@tonic-gate * If the se_share is currently SE_EXCL, we will fail 3190Sstevel@tonic-gate * to acquire p_selock anyway. Blocking is the 3200Sstevel@tonic-gate * right thing to do. 3210Sstevel@tonic-gate * If we need to reclaim this page, we must get 3220Sstevel@tonic-gate * exclusive access to it, force the upgrade now. 3230Sstevel@tonic-gate * Again, we will fail to acquire p_selock if the 3240Sstevel@tonic-gate * page is not free and block. 3250Sstevel@tonic-gate */ 3260Sstevel@tonic-gate upgraded = 1; 3270Sstevel@tonic-gate se = SE_EXCL; 3280Sstevel@tonic-gate VM_STAT_ADD(page_lock_upgrade); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate } 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate if (se == SE_EXCL) { 333*917Selowe if (!(es & SE_EXCL_WANTED) && (pp->p_selock & SE_EWANTED)) { 3340Sstevel@tonic-gate /* 3350Sstevel@tonic-gate * if the caller wants a writer lock (but did not 3360Sstevel@tonic-gate * specify exclusive access), and there is a pending 3370Sstevel@tonic-gate * writer that wants exclusive access, return failure 3380Sstevel@tonic-gate */ 3390Sstevel@tonic-gate retval = 0; 3400Sstevel@tonic-gate } else if ((pp->p_selock & ~SE_EWANTED) == 0) { 3410Sstevel@tonic-gate /* no reader/writer lock held */ 3420Sstevel@tonic-gate THREAD_KPRI_REQUEST(); 3430Sstevel@tonic-gate /* this clears our setting of the SE_EWANTED bit */ 3440Sstevel@tonic-gate pp->p_selock = SE_WRITER; 3450Sstevel@tonic-gate retval = 1; 3460Sstevel@tonic-gate } else { 3470Sstevel@tonic-gate /* page is locked */ 348*917Selowe if (es & SE_EXCL_WANTED) { 3490Sstevel@tonic-gate /* set the SE_EWANTED bit */ 3500Sstevel@tonic-gate pp->p_selock |= SE_EWANTED; 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate retval = 0; 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate } else { 3550Sstevel@tonic-gate retval = 0; 3560Sstevel@tonic-gate if (pp->p_selock >= 0) { 357*917Selowe /* 358*917Selowe * Readers are not allowed when excl wanted or 359*917Selowe * a retire is pending. Since kvp pages can take 360*917Selowe * a long time to be retired, we make an exception 361*917Selowe * for them to avoid hanging threads unnecessarily. 362*917Selowe */ 363*917Selowe if ((pp->p_selock & SE_EWANTED) == 0) { 364*917Selowe if (!PP_PR_REQ(pp) || pp->p_vnode == &kvp) { 365*917Selowe pp->p_selock += SE_READER; 366*917Selowe retval = 1; 367*917Selowe } 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate } 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate if (retval == 0) { 3730Sstevel@tonic-gate if ((pp->p_selock & ~SE_EWANTED) == SE_DELETED) { 3740Sstevel@tonic-gate VM_STAT_ADD(page_lock_deleted); 3750Sstevel@tonic-gate mutex_exit(pse); 3760Sstevel@tonic-gate return (retval); 3770Sstevel@tonic-gate } 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate #ifdef VM_STATS 3800Sstevel@tonic-gate VM_STAT_ADD(page_lock_miss); 3810Sstevel@tonic-gate if (upgraded) { 3820Sstevel@tonic-gate VM_STAT_ADD(page_lock_upgrade_failed); 3830Sstevel@tonic-gate } 3840Sstevel@tonic-gate #endif 3850Sstevel@tonic-gate if (lock) { 3860Sstevel@tonic-gate VM_STAT_ADD(page_lock_miss_lock); 3870Sstevel@tonic-gate mutex_exit(lock); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate /* 3910Sstevel@tonic-gate * Now, wait for the page to be unlocked and 3920Sstevel@tonic-gate * release the lock protecting p_cv and p_selock. 3930Sstevel@tonic-gate */ 3940Sstevel@tonic-gate cv_wait(&pp->p_cv, pse); 3950Sstevel@tonic-gate mutex_exit(pse); 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate /* 3980Sstevel@tonic-gate * The page identity may have changed while we were 3990Sstevel@tonic-gate * blocked. If we are willing to depend on "pp" 4000Sstevel@tonic-gate * still pointing to a valid page structure (i.e., 4010Sstevel@tonic-gate * assuming page structures are not dynamically allocated 4020Sstevel@tonic-gate * or freed), we could try to lock the page if its 4030Sstevel@tonic-gate * identity hasn't changed. 4040Sstevel@tonic-gate * 4050Sstevel@tonic-gate * This needs to be measured, since we come back from 4060Sstevel@tonic-gate * cv_wait holding pse (the expensive part of this 4070Sstevel@tonic-gate * operation) we might as well try the cheap part. 4080Sstevel@tonic-gate * Though we would also have to confirm that dropping 4090Sstevel@tonic-gate * `lock' did not cause any grief to the callers. 4100Sstevel@tonic-gate */ 4110Sstevel@tonic-gate if (lock) { 4120Sstevel@tonic-gate mutex_enter(lock); 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate } else { 4150Sstevel@tonic-gate /* 4160Sstevel@tonic-gate * We have the page lock. 4170Sstevel@tonic-gate * If we needed to reclaim the page, and the page 4180Sstevel@tonic-gate * needed reclaiming (ie, it was free), then we 4190Sstevel@tonic-gate * have the page exclusively locked. We may need 4200Sstevel@tonic-gate * to downgrade the page. 4210Sstevel@tonic-gate */ 4220Sstevel@tonic-gate ASSERT((upgraded) ? 4230Sstevel@tonic-gate ((PP_ISFREE(pp)) && PAGE_EXCL(pp)) : 1); 4240Sstevel@tonic-gate mutex_exit(pse); 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate /* 4270Sstevel@tonic-gate * We now hold this page's lock, either shared or 4280Sstevel@tonic-gate * exclusive. This will prevent its identity from changing. 4290Sstevel@tonic-gate * The page, however, may or may not be free. If the caller 4300Sstevel@tonic-gate * requested, and it is free, go reclaim it from the 4310Sstevel@tonic-gate * free list. If the page can't be reclaimed, return failure 4320Sstevel@tonic-gate * so that the caller can start all over again. 4330Sstevel@tonic-gate * 4340Sstevel@tonic-gate * NOTE:page_reclaim() releases the page lock (p_selock) 4350Sstevel@tonic-gate * if it can't be reclaimed. 4360Sstevel@tonic-gate */ 4370Sstevel@tonic-gate if (reclaim_it) { 4380Sstevel@tonic-gate if (!page_reclaim(pp, lock)) { 4390Sstevel@tonic-gate VM_STAT_ADD(page_lock_bad_reclaim); 4400Sstevel@tonic-gate retval = 0; 4410Sstevel@tonic-gate } else { 4420Sstevel@tonic-gate VM_STAT_ADD(page_lock_reclaim); 4430Sstevel@tonic-gate if (upgraded) { 4440Sstevel@tonic-gate page_downgrade(pp); 4450Sstevel@tonic-gate } 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate } 4490Sstevel@tonic-gate return (retval); 4500Sstevel@tonic-gate } 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate /* 4530Sstevel@tonic-gate * Clear the SE_EWANTED bit from p_selock. This function allows 4540Sstevel@tonic-gate * callers of page_lock_es and page_try_reclaim_lock to clear 4550Sstevel@tonic-gate * their setting of this bit if they decide they no longer wish 4560Sstevel@tonic-gate * to gain exclusive access to the page. Currently only 4570Sstevel@tonic-gate * delete_memory_thread uses this when the delete memory 4580Sstevel@tonic-gate * operation is cancelled. 4590Sstevel@tonic-gate */ 4600Sstevel@tonic-gate void 4610Sstevel@tonic-gate page_lock_clr_exclwanted(page_t *pp) 4620Sstevel@tonic-gate { 4630Sstevel@tonic-gate kmutex_t *pse = PAGE_SE_MUTEX(pp); 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate mutex_enter(pse); 4660Sstevel@tonic-gate pp->p_selock &= ~SE_EWANTED; 4670Sstevel@tonic-gate if (CV_HAS_WAITERS(&pp->p_cv)) 4680Sstevel@tonic-gate cv_broadcast(&pp->p_cv); 4690Sstevel@tonic-gate mutex_exit(pse); 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate /* 4730Sstevel@tonic-gate * Read the comments inside of page_lock_es() carefully. 4740Sstevel@tonic-gate * 4750Sstevel@tonic-gate * SE_EXCL callers specifying es == SE_EXCL_WANTED will cause the 4760Sstevel@tonic-gate * SE_EWANTED bit of p_selock to be set when the lock cannot be obtained. 4770Sstevel@tonic-gate * This is used by threads subject to reader-starvation (eg. memory delete). 4780Sstevel@tonic-gate * 4790Sstevel@tonic-gate * When a thread using SE_EXCL_WANTED does not obtain the SE_EXCL lock, 4800Sstevel@tonic-gate * it is expected that it will retry at a later time. Threads that will 4810Sstevel@tonic-gate * not retry the lock *must* call page_lock_clr_exclwanted to clear the 4820Sstevel@tonic-gate * SE_EWANTED bit. (When a thread using SE_EXCL_WANTED obtains the lock, 4830Sstevel@tonic-gate * the bit is cleared.) 4840Sstevel@tonic-gate */ 4850Sstevel@tonic-gate int 4860Sstevel@tonic-gate page_try_reclaim_lock(page_t *pp, se_t se, int es) 4870Sstevel@tonic-gate { 4880Sstevel@tonic-gate kmutex_t *pse = PAGE_SE_MUTEX(pp); 4890Sstevel@tonic-gate selock_t old; 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate mutex_enter(pse); 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate old = pp->p_selock; 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate ASSERT(((es & SE_EXCL_WANTED) == 0) || 496*917Selowe ((es & SE_EXCL_WANTED) && (se == SE_EXCL))); 497*917Selowe 498*917Selowe if (PP_RETIRED(pp) && !(es & SE_RETIRED)) { 499*917Selowe mutex_exit(pse); 500*917Selowe VM_STAT_ADD(page_trylock_failed); 501*917Selowe return (0); 502*917Selowe } 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate if (se == SE_SHARED && es == 1 && old == 0) { 5050Sstevel@tonic-gate se = SE_EXCL; 5060Sstevel@tonic-gate } 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate if (se == SE_SHARED) { 5090Sstevel@tonic-gate if (!PP_ISFREE(pp)) { 5100Sstevel@tonic-gate if (old >= 0) { 511*917Selowe /* 512*917Selowe * Readers are not allowed when excl wanted 513*917Selowe * or a retire is pending. Since kvp pages can 514*917Selowe * take a long time to be retired, we make an 515*917Selowe * exception for them to avoid hanging threads 516*917Selowe * unnecessarily. 517*917Selowe */ 518*917Selowe if ((old & SE_EWANTED) == 0) { 519*917Selowe if (!PP_PR_REQ(pp) || 520*917Selowe pp->p_vnode == &kvp) { 521*917Selowe pp->p_selock = old + SE_READER; 522*917Selowe mutex_exit(pse); 523*917Selowe return (1); 524*917Selowe } 5250Sstevel@tonic-gate } 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate mutex_exit(pse); 5280Sstevel@tonic-gate return (0); 5290Sstevel@tonic-gate } 5300Sstevel@tonic-gate /* 5310Sstevel@tonic-gate * The page is free, so we really want SE_EXCL (below) 5320Sstevel@tonic-gate */ 5330Sstevel@tonic-gate VM_STAT_ADD(page_try_reclaim_upgrade); 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate /* 5370Sstevel@tonic-gate * The caller wants a writer lock. We try for it only if 5380Sstevel@tonic-gate * SE_EWANTED is not set, or if the caller specified 5390Sstevel@tonic-gate * SE_EXCL_WANTED. 5400Sstevel@tonic-gate */ 541*917Selowe if (!(old & SE_EWANTED) || (es & SE_EXCL_WANTED)) { 5420Sstevel@tonic-gate if ((old & ~SE_EWANTED) == 0) { 5430Sstevel@tonic-gate /* no reader/writer lock held */ 5440Sstevel@tonic-gate THREAD_KPRI_REQUEST(); 5450Sstevel@tonic-gate /* this clears out our setting of the SE_EWANTED bit */ 5460Sstevel@tonic-gate pp->p_selock = SE_WRITER; 5470Sstevel@tonic-gate mutex_exit(pse); 5480Sstevel@tonic-gate return (1); 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate } 551*917Selowe if (es & SE_EXCL_WANTED) { 5520Sstevel@tonic-gate /* page is locked, set the SE_EWANTED bit */ 5530Sstevel@tonic-gate pp->p_selock |= SE_EWANTED; 5540Sstevel@tonic-gate } 5550Sstevel@tonic-gate mutex_exit(pse); 5560Sstevel@tonic-gate return (0); 5570Sstevel@tonic-gate } 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate /* 5600Sstevel@tonic-gate * Acquire a page's "shared/exclusive" lock, but never block. 5610Sstevel@tonic-gate * Returns 1 on success, 0 on failure. 5620Sstevel@tonic-gate */ 5630Sstevel@tonic-gate int 5640Sstevel@tonic-gate page_trylock(page_t *pp, se_t se) 5650Sstevel@tonic-gate { 5660Sstevel@tonic-gate kmutex_t *pse = PAGE_SE_MUTEX(pp); 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate mutex_enter(pse); 569*917Selowe if (pp->p_selock & SE_EWANTED || PP_RETIRED(pp) || 570*917Selowe (se == SE_SHARED && PP_PR_REQ(pp) && pp->p_vnode != &kvp)) { 571*917Selowe /* 572*917Selowe * Fail if a thread wants exclusive access and page is 573*917Selowe * retired, if the page is slated for retirement, or a 574*917Selowe * share lock is requested. 575*917Selowe */ 5760Sstevel@tonic-gate mutex_exit(pse); 577*917Selowe VM_STAT_ADD(page_trylock_failed); 5780Sstevel@tonic-gate return (0); 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate if (se == SE_EXCL) { 5820Sstevel@tonic-gate if (pp->p_selock == 0) { 5830Sstevel@tonic-gate THREAD_KPRI_REQUEST(); 5840Sstevel@tonic-gate pp->p_selock = SE_WRITER; 5850Sstevel@tonic-gate mutex_exit(pse); 5860Sstevel@tonic-gate return (1); 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate } else { 5890Sstevel@tonic-gate if (pp->p_selock >= 0) { 5900Sstevel@tonic-gate pp->p_selock += SE_READER; 5910Sstevel@tonic-gate mutex_exit(pse); 5920Sstevel@tonic-gate return (1); 5930Sstevel@tonic-gate } 5940Sstevel@tonic-gate } 5950Sstevel@tonic-gate mutex_exit(pse); 5960Sstevel@tonic-gate return (0); 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate /* 600*917Selowe * Variant of page_unlock() specifically for the page freelist 601*917Selowe * code. The mere existence of this code is a vile hack that 602*917Selowe * has resulted due to the backwards locking order of the page 603*917Selowe * freelist manager; please don't call it. 604*917Selowe */ 605*917Selowe void 606*917Selowe page_unlock_noretire(page_t *pp) 607*917Selowe { 608*917Selowe kmutex_t *pse = PAGE_SE_MUTEX(pp); 609*917Selowe selock_t old; 610*917Selowe 611*917Selowe mutex_enter(pse); 612*917Selowe 613*917Selowe old = pp->p_selock; 614*917Selowe if ((old & ~SE_EWANTED) == SE_READER) { 615*917Selowe pp->p_selock = old & ~SE_READER; 616*917Selowe if (CV_HAS_WAITERS(&pp->p_cv)) 617*917Selowe cv_broadcast(&pp->p_cv); 618*917Selowe } else if ((old & ~SE_EWANTED) == SE_DELETED) { 619*917Selowe panic("page_unlock_noretire: page %p is deleted", pp); 620*917Selowe } else if (old < 0) { 621*917Selowe THREAD_KPRI_RELEASE(); 622*917Selowe pp->p_selock &= SE_EWANTED; 623*917Selowe if (CV_HAS_WAITERS(&pp->p_cv)) 624*917Selowe cv_broadcast(&pp->p_cv); 625*917Selowe } else if ((old & ~SE_EWANTED) > SE_READER) { 626*917Selowe pp->p_selock = old - SE_READER; 627*917Selowe } else { 628*917Selowe panic("page_unlock_noretire: page %p is not locked", pp); 629*917Selowe } 630*917Selowe 631*917Selowe mutex_exit(pse); 632*917Selowe } 633*917Selowe 634*917Selowe /* 6350Sstevel@tonic-gate * Release the page's "shared/exclusive" lock and wake up anyone 6360Sstevel@tonic-gate * who might be waiting for it. 6370Sstevel@tonic-gate */ 6380Sstevel@tonic-gate void 6390Sstevel@tonic-gate page_unlock(page_t *pp) 6400Sstevel@tonic-gate { 6410Sstevel@tonic-gate kmutex_t *pse = PAGE_SE_MUTEX(pp); 6420Sstevel@tonic-gate selock_t old; 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate mutex_enter(pse); 645*917Selowe 6460Sstevel@tonic-gate old = pp->p_selock; 6470Sstevel@tonic-gate if ((old & ~SE_EWANTED) == SE_READER) { 6480Sstevel@tonic-gate pp->p_selock = old & ~SE_READER; 6490Sstevel@tonic-gate if (CV_HAS_WAITERS(&pp->p_cv)) 6500Sstevel@tonic-gate cv_broadcast(&pp->p_cv); 6510Sstevel@tonic-gate } else if ((old & ~SE_EWANTED) == SE_DELETED) { 6520Sstevel@tonic-gate panic("page_unlock: page %p is deleted", pp); 6530Sstevel@tonic-gate } else if (old < 0) { 6540Sstevel@tonic-gate THREAD_KPRI_RELEASE(); 6550Sstevel@tonic-gate pp->p_selock &= SE_EWANTED; 6560Sstevel@tonic-gate if (CV_HAS_WAITERS(&pp->p_cv)) 6570Sstevel@tonic-gate cv_broadcast(&pp->p_cv); 6580Sstevel@tonic-gate } else if ((old & ~SE_EWANTED) > SE_READER) { 6590Sstevel@tonic-gate pp->p_selock = old - SE_READER; 6600Sstevel@tonic-gate } else { 6610Sstevel@tonic-gate panic("page_unlock: page %p is not locked", pp); 6620Sstevel@tonic-gate } 663*917Selowe 664*917Selowe if (pp->p_selock == 0 && PP_PR_REQ(pp)) { 665*917Selowe /* 666*917Selowe * Try to retire the page. If it retires, great. 667*917Selowe * If not, oh well, we'll get it in the next unlock 668*917Selowe * request, and repeat the cycle. Regardless, 669*917Selowe * page_tryretire() will drop the page lock. 670*917Selowe */ 671*917Selowe if ((pp->p_toxic & PR_BUSY) == 0) { 672*917Selowe THREAD_KPRI_REQUEST(); 673*917Selowe pp->p_selock = SE_WRITER; 674*917Selowe page_settoxic(pp, PR_BUSY); 675*917Selowe mutex_exit(pse); 676*917Selowe page_tryretire(pp); 677*917Selowe } else { 678*917Selowe pp->p_selock = SE_WRITER; 679*917Selowe page_clrtoxic(pp, PR_BUSY); 680*917Selowe pp->p_selock = 0; 681*917Selowe mutex_exit(pse); 682*917Selowe } 683*917Selowe } else { 684*917Selowe mutex_exit(pse); 685*917Selowe } 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate /* 6890Sstevel@tonic-gate * Try to upgrade the lock on the page from a "shared" to an 6900Sstevel@tonic-gate * "exclusive" lock. Since this upgrade operation is done while 6910Sstevel@tonic-gate * holding the mutex protecting this page, no one else can acquire this page's 6920Sstevel@tonic-gate * lock and change the page. Thus, it is safe to drop the "shared" 6930Sstevel@tonic-gate * lock and attempt to acquire the "exclusive" lock. 6940Sstevel@tonic-gate * 6950Sstevel@tonic-gate * Returns 1 on success, 0 on failure. 6960Sstevel@tonic-gate */ 6970Sstevel@tonic-gate int 6980Sstevel@tonic-gate page_tryupgrade(page_t *pp) 6990Sstevel@tonic-gate { 7000Sstevel@tonic-gate kmutex_t *pse = PAGE_SE_MUTEX(pp); 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate mutex_enter(pse); 7030Sstevel@tonic-gate if (!(pp->p_selock & SE_EWANTED)) { 7040Sstevel@tonic-gate /* no threads want exclusive access, try upgrade */ 7050Sstevel@tonic-gate if (pp->p_selock == SE_READER) { 7060Sstevel@tonic-gate THREAD_KPRI_REQUEST(); 7070Sstevel@tonic-gate /* convert to exclusive lock */ 7080Sstevel@tonic-gate pp->p_selock = SE_WRITER; 7090Sstevel@tonic-gate mutex_exit(pse); 7100Sstevel@tonic-gate return (1); 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate mutex_exit(pse); 7140Sstevel@tonic-gate return (0); 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate /* 7180Sstevel@tonic-gate * Downgrade the "exclusive" lock on the page to a "shared" lock 7190Sstevel@tonic-gate * while holding the mutex protecting this page's p_selock field. 7200Sstevel@tonic-gate */ 7210Sstevel@tonic-gate void 7220Sstevel@tonic-gate page_downgrade(page_t *pp) 7230Sstevel@tonic-gate { 7240Sstevel@tonic-gate kmutex_t *pse = PAGE_SE_MUTEX(pp); 7250Sstevel@tonic-gate int excl_waiting; 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate ASSERT((pp->p_selock & ~SE_EWANTED) != SE_DELETED); 7280Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate mutex_enter(pse); 7310Sstevel@tonic-gate excl_waiting = pp->p_selock & SE_EWANTED; 7320Sstevel@tonic-gate THREAD_KPRI_RELEASE(); 7330Sstevel@tonic-gate pp->p_selock = SE_READER | excl_waiting; 7340Sstevel@tonic-gate if (CV_HAS_WAITERS(&pp->p_cv)) 7350Sstevel@tonic-gate cv_broadcast(&pp->p_cv); 7360Sstevel@tonic-gate mutex_exit(pse); 7370Sstevel@tonic-gate } 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate void 7400Sstevel@tonic-gate page_lock_delete(page_t *pp) 7410Sstevel@tonic-gate { 7420Sstevel@tonic-gate kmutex_t *pse = PAGE_SE_MUTEX(pp); 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 7450Sstevel@tonic-gate ASSERT(pp->p_vnode == NULL); 7460Sstevel@tonic-gate ASSERT(pp->p_offset == (u_offset_t)-1); 7470Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate mutex_enter(pse); 7500Sstevel@tonic-gate THREAD_KPRI_RELEASE(); 7510Sstevel@tonic-gate pp->p_selock = SE_DELETED; 7520Sstevel@tonic-gate if (CV_HAS_WAITERS(&pp->p_cv)) 7530Sstevel@tonic-gate cv_broadcast(&pp->p_cv); 7540Sstevel@tonic-gate mutex_exit(pse); 7550Sstevel@tonic-gate } 7560Sstevel@tonic-gate 7570Sstevel@tonic-gate /* 7580Sstevel@tonic-gate * Implement the io lock for pages 7590Sstevel@tonic-gate */ 7600Sstevel@tonic-gate void 7610Sstevel@tonic-gate page_iolock_init(page_t *pp) 7620Sstevel@tonic-gate { 7630Sstevel@tonic-gate pp->p_iolock_state = 0; 7640Sstevel@tonic-gate cv_init(&pp->p_io_cv, NULL, CV_DEFAULT, NULL); 7650Sstevel@tonic-gate } 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate /* 7680Sstevel@tonic-gate * Acquire the i/o lock on a page. 7690Sstevel@tonic-gate */ 7700Sstevel@tonic-gate void 7710Sstevel@tonic-gate page_io_lock(page_t *pp) 7720Sstevel@tonic-gate { 7730Sstevel@tonic-gate kmutex_t *pio; 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate pio = PAGE_IO_MUTEX(pp); 7760Sstevel@tonic-gate mutex_enter(pio); 7770Sstevel@tonic-gate while (pp->p_iolock_state & PAGE_IO_INUSE) { 7780Sstevel@tonic-gate cv_wait(&(pp->p_io_cv), pio); 7790Sstevel@tonic-gate } 7800Sstevel@tonic-gate pp->p_iolock_state |= PAGE_IO_INUSE; 7810Sstevel@tonic-gate mutex_exit(pio); 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate /* 7850Sstevel@tonic-gate * Release the i/o lock on a page. 7860Sstevel@tonic-gate */ 7870Sstevel@tonic-gate void 7880Sstevel@tonic-gate page_io_unlock(page_t *pp) 7890Sstevel@tonic-gate { 7900Sstevel@tonic-gate kmutex_t *pio; 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate pio = PAGE_IO_MUTEX(pp); 7930Sstevel@tonic-gate mutex_enter(pio); 7940Sstevel@tonic-gate cv_signal(&pp->p_io_cv); 7950Sstevel@tonic-gate pp->p_iolock_state &= ~PAGE_IO_INUSE; 7960Sstevel@tonic-gate mutex_exit(pio); 7970Sstevel@tonic-gate } 7980Sstevel@tonic-gate 7990Sstevel@tonic-gate /* 8000Sstevel@tonic-gate * Try to acquire the i/o lock on a page without blocking. 8010Sstevel@tonic-gate * Returns 1 on success, 0 on failure. 8020Sstevel@tonic-gate */ 8030Sstevel@tonic-gate int 8040Sstevel@tonic-gate page_io_trylock(page_t *pp) 8050Sstevel@tonic-gate { 8060Sstevel@tonic-gate kmutex_t *pio; 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate if (pp->p_iolock_state & PAGE_IO_INUSE) 8090Sstevel@tonic-gate return (0); 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate pio = PAGE_IO_MUTEX(pp); 8120Sstevel@tonic-gate mutex_enter(pio); 8130Sstevel@tonic-gate 8140Sstevel@tonic-gate if (pp->p_iolock_state & PAGE_IO_INUSE) { 8150Sstevel@tonic-gate mutex_exit(pio); 8160Sstevel@tonic-gate return (0); 8170Sstevel@tonic-gate } 8180Sstevel@tonic-gate pp->p_iolock_state |= PAGE_IO_INUSE; 8190Sstevel@tonic-gate mutex_exit(pio); 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate return (1); 8220Sstevel@tonic-gate } 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate /* 8250Sstevel@tonic-gate * Assert that the i/o lock on a page is held. 8260Sstevel@tonic-gate * Returns 1 on success, 0 on failure. 8270Sstevel@tonic-gate */ 8280Sstevel@tonic-gate int 8290Sstevel@tonic-gate page_iolock_assert(page_t *pp) 8300Sstevel@tonic-gate { 8310Sstevel@tonic-gate return (pp->p_iolock_state & PAGE_IO_INUSE); 8320Sstevel@tonic-gate } 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate /* 8350Sstevel@tonic-gate * Wrapper exported to kernel routines that are built 8360Sstevel@tonic-gate * platform-independent (the macro is platform-dependent; 8370Sstevel@tonic-gate * the size of vph_mutex[] is based on NCPU). 8380Sstevel@tonic-gate * 8390Sstevel@tonic-gate * Note that you can do stress testing on this by setting the 8400Sstevel@tonic-gate * variable page_vnode_mutex_stress to something other than 8410Sstevel@tonic-gate * zero in a DEBUG kernel in a debugger after loading the kernel. 8420Sstevel@tonic-gate * Setting it after the kernel is running may not work correctly. 8430Sstevel@tonic-gate */ 8440Sstevel@tonic-gate #ifdef DEBUG 8450Sstevel@tonic-gate static int page_vnode_mutex_stress = 0; 8460Sstevel@tonic-gate #endif 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate kmutex_t * 8490Sstevel@tonic-gate page_vnode_mutex(vnode_t *vp) 8500Sstevel@tonic-gate { 8510Sstevel@tonic-gate if (vp == &kvp) 8520Sstevel@tonic-gate return (&vph_mutex[VPH_TABLE_SIZE + 0]); 8530Sstevel@tonic-gate #ifdef DEBUG 8540Sstevel@tonic-gate if (page_vnode_mutex_stress != 0) 8550Sstevel@tonic-gate return (&vph_mutex[0]); 8560Sstevel@tonic-gate #endif 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate return (&vph_mutex[VP_HASH_FUNC(vp)]); 8590Sstevel@tonic-gate } 8600Sstevel@tonic-gate 8610Sstevel@tonic-gate kmutex_t * 8620Sstevel@tonic-gate page_se_mutex(page_t *pp) 8630Sstevel@tonic-gate { 8640Sstevel@tonic-gate return (PAGE_SE_MUTEX(pp)); 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate #ifdef VM_STATS 8680Sstevel@tonic-gate uint_t pszclck_stat[4]; 8690Sstevel@tonic-gate #endif 8700Sstevel@tonic-gate /* 8710Sstevel@tonic-gate * Find, take and return a mutex held by hat_page_demote(). 8720Sstevel@tonic-gate * Called by page_demote_vp_pages() before hat_page_demote() call and by 8730Sstevel@tonic-gate * routines that want to block hat_page_demote() but can't do it 8740Sstevel@tonic-gate * via locking all constituent pages. 8750Sstevel@tonic-gate * 8760Sstevel@tonic-gate * Return NULL if p_szc is 0. 8770Sstevel@tonic-gate * 8780Sstevel@tonic-gate * It should only be used for pages that can be demoted by hat_page_demote() 8790Sstevel@tonic-gate * i.e. non swapfs file system pages. The logic here is lifted from 8800Sstevel@tonic-gate * sfmmu_mlspl_enter() except there's no need to worry about p_szc increase 8810Sstevel@tonic-gate * since the page is locked and not free. 8820Sstevel@tonic-gate * 8830Sstevel@tonic-gate * Hash of the root page is used to find the lock. 8840Sstevel@tonic-gate * To find the root in the presense of hat_page_demote() chageing the location 8850Sstevel@tonic-gate * of the root this routine relies on the fact that hat_page_demote() changes 8860Sstevel@tonic-gate * root last. 8870Sstevel@tonic-gate * 8880Sstevel@tonic-gate * If NULL is returned pp's p_szc is guaranteed to be 0. If non NULL is 8890Sstevel@tonic-gate * returned pp's p_szc may be any value. 8900Sstevel@tonic-gate */ 8910Sstevel@tonic-gate kmutex_t * 8920Sstevel@tonic-gate page_szc_lock(page_t *pp) 8930Sstevel@tonic-gate { 8940Sstevel@tonic-gate kmutex_t *mtx; 8950Sstevel@tonic-gate page_t *rootpp; 8960Sstevel@tonic-gate uint_t szc; 8970Sstevel@tonic-gate uint_t rszc; 8980Sstevel@tonic-gate uint_t pszc = pp->p_szc; 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate ASSERT(pp != NULL); 9010Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 9020Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 9030Sstevel@tonic-gate ASSERT(pp->p_vnode != NULL); 9040Sstevel@tonic-gate ASSERT(!IS_SWAPFSVP(pp->p_vnode)); 9050Sstevel@tonic-gate ASSERT(pp->p_vnode != &kvp); 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate again: 9080Sstevel@tonic-gate if (pszc == 0) { 9090Sstevel@tonic-gate VM_STAT_ADD(pszclck_stat[0]); 9100Sstevel@tonic-gate return (NULL); 9110Sstevel@tonic-gate } 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate /* The lock lives in the root page */ 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate rootpp = PP_GROUPLEADER(pp, pszc); 9160Sstevel@tonic-gate mtx = PAGE_SZC_MUTEX(rootpp); 9170Sstevel@tonic-gate mutex_enter(mtx); 9180Sstevel@tonic-gate 9190Sstevel@tonic-gate /* 9200Sstevel@tonic-gate * since p_szc can only decrease if pp == rootpp 9210Sstevel@tonic-gate * rootpp will be always the same i.e we have the right root 9220Sstevel@tonic-gate * regardless of rootpp->p_szc. 9230Sstevel@tonic-gate * If location of pp's root didn't change after we took 9240Sstevel@tonic-gate * the lock we have the right root. return mutex hashed off it. 9250Sstevel@tonic-gate */ 9260Sstevel@tonic-gate if (pp == rootpp || (rszc = rootpp->p_szc) == pszc) { 9270Sstevel@tonic-gate VM_STAT_ADD(pszclck_stat[1]); 9280Sstevel@tonic-gate return (mtx); 9290Sstevel@tonic-gate } 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate /* 9320Sstevel@tonic-gate * root location changed because page got demoted. 9330Sstevel@tonic-gate * locate the new root. 9340Sstevel@tonic-gate */ 9350Sstevel@tonic-gate if (rszc < pszc) { 9360Sstevel@tonic-gate szc = pp->p_szc; 9370Sstevel@tonic-gate ASSERT(szc < pszc); 9380Sstevel@tonic-gate mutex_exit(mtx); 9390Sstevel@tonic-gate pszc = szc; 9400Sstevel@tonic-gate VM_STAT_ADD(pszclck_stat[2]); 9410Sstevel@tonic-gate goto again; 9420Sstevel@tonic-gate } 9430Sstevel@tonic-gate 9440Sstevel@tonic-gate VM_STAT_ADD(pszclck_stat[3]); 9450Sstevel@tonic-gate /* 9460Sstevel@tonic-gate * current hat_page_demote not done yet. 9470Sstevel@tonic-gate * wait for it to finish. 9480Sstevel@tonic-gate */ 9490Sstevel@tonic-gate mutex_exit(mtx); 9500Sstevel@tonic-gate rootpp = PP_GROUPLEADER(rootpp, rszc); 9510Sstevel@tonic-gate mtx = PAGE_SZC_MUTEX(rootpp); 9520Sstevel@tonic-gate mutex_enter(mtx); 9530Sstevel@tonic-gate mutex_exit(mtx); 9540Sstevel@tonic-gate ASSERT(rootpp->p_szc < rszc); 9550Sstevel@tonic-gate goto again; 9560Sstevel@tonic-gate } 9570Sstevel@tonic-gate 9580Sstevel@tonic-gate int 9590Sstevel@tonic-gate page_szc_lock_assert(page_t *pp) 9600Sstevel@tonic-gate { 9610Sstevel@tonic-gate page_t *rootpp = PP_PAGEROOT(pp); 9620Sstevel@tonic-gate kmutex_t *mtx = PAGE_SZC_MUTEX(rootpp); 9630Sstevel@tonic-gate 9640Sstevel@tonic-gate return (MUTEX_HELD(mtx)); 9650Sstevel@tonic-gate } 966