xref: /onnv-gate/usr/src/uts/common/vm/anon.h (revision 13035:0847ab4cffd0)
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
52414Saguzovsk  * Common Development and Distribution License (the "License").
62414Saguzovsk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2212173SMichael.Corcoran@Sun.COM  * Copyright (c) 1986, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
260Sstevel@tonic-gate /*	 All Rights Reserved   */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
300Sstevel@tonic-gate  * The Regents of the University of California
310Sstevel@tonic-gate  * All Rights Reserved
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
340Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
350Sstevel@tonic-gate  * contributors.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #ifndef	_VM_ANON_H
390Sstevel@tonic-gate #define	_VM_ANON_H
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <sys/cred.h>
423247Sgjelinek #include <sys/zone.h>
430Sstevel@tonic-gate #include <vm/seg.h>
440Sstevel@tonic-gate #include <vm/vpage.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #ifdef	__cplusplus
470Sstevel@tonic-gate extern "C" {
480Sstevel@tonic-gate #endif
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * VM - Anonymous pages.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate typedef	unsigned long anoff_t;		/* anon offsets */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  *	Each anonymous page, either in memory or in swap, has an anon structure.
580Sstevel@tonic-gate  * The structure (slot) provides a level of indirection between anonymous pages
590Sstevel@tonic-gate  * and their backing store.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  *	(an_vp, an_off) names the vnode of the anonymous page for this slot.
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * 	(an_pvp, an_poff) names the location of the physical backing store
640Sstevel@tonic-gate  * 	for the page this slot represents. If the name is null there is no
650Sstevel@tonic-gate  * 	associated physical store. The physical backing store location can
660Sstevel@tonic-gate  *	change while the slot is in use.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  *	an_hash is a hash list of anon slots. The list is hashed by
690Sstevel@tonic-gate  * 	(an_vp, an_off) of the associated anonymous page and provides a
700Sstevel@tonic-gate  *	method of going from the name of an anonymous page to its
710Sstevel@tonic-gate  * 	associated anon slot.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  *	an_refcnt holds a reference count which is the number of separate
740Sstevel@tonic-gate  * 	copies that will need to be created in case of copy-on-write.
750Sstevel@tonic-gate  *	A refcnt > 0 protects the existence of the slot. The refcnt is
760Sstevel@tonic-gate  * 	initialized to 1 when the anon slot is created in anon_alloc().
770Sstevel@tonic-gate  *	If a client obtains an anon slot and allows multiple threads to
780Sstevel@tonic-gate  * 	share it, then it is the client's responsibility to insure that
790Sstevel@tonic-gate  *	it does not allow one thread to try to reference the slot at the
800Sstevel@tonic-gate  *	same time as another is trying to decrement the last count and
810Sstevel@tonic-gate  *	destroy the anon slot. E.g., the seg_vn segment type protects
820Sstevel@tonic-gate  *	against this with higher level locks.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate struct anon {
860Sstevel@tonic-gate 	struct vnode *an_vp;	/* vnode of anon page */
870Sstevel@tonic-gate 	struct vnode *an_pvp;	/* vnode of physical backing store */
880Sstevel@tonic-gate 	anoff_t an_off;		/* offset of anon page */
890Sstevel@tonic-gate 	anoff_t an_poff;	/* offset in vnode */
900Sstevel@tonic-gate 	struct anon *an_hash;	/* hash table of anon slots */
910Sstevel@tonic-gate 	int an_refcnt;		/* # of people sharing slot */
920Sstevel@tonic-gate };
930Sstevel@tonic-gate 
9412230SFrank.Rival@oracle.com #define	AN_CACHE_ALIGN_LOG2	4	/* log2(AN_CACHE_ALIGN) */
9512230SFrank.Rival@oracle.com #define	AN_CACHE_ALIGN	(1U << AN_CACHE_ALIGN_LOG2) /* anon address aligned */
9612230SFrank.Rival@oracle.com 						/* 16 bytes */
9712230SFrank.Rival@oracle.com 
9812230SFrank.Rival@oracle.com 
990Sstevel@tonic-gate #ifdef _KERNEL
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * The swapinfo_lock protects:
1020Sstevel@tonic-gate  *		swapinfo list
1030Sstevel@tonic-gate  *		individual swapinfo structures
1040Sstevel@tonic-gate  *
1050Sstevel@tonic-gate  * The anoninfo_lock protects:
1060Sstevel@tonic-gate  *		anoninfo counters
1070Sstevel@tonic-gate  *
1080Sstevel@tonic-gate  * The anonhash_lock protects:
1090Sstevel@tonic-gate  *		anon hash lists
1100Sstevel@tonic-gate  *		anon slot fields
1110Sstevel@tonic-gate  *
1120Sstevel@tonic-gate  * Fields in the anon slot which are read-only for the life of the slot
1130Sstevel@tonic-gate  * (an_vp, an_off) do not require the anonhash_lock be held to access them.
1140Sstevel@tonic-gate  * If you access a field without the anonhash_lock held you must be holding
1150Sstevel@tonic-gate  * the slot with an_refcnt to make sure it isn't destroyed.
1160Sstevel@tonic-gate  * To write (an_pvp, an_poff) in a given slot you must also hold the
1170Sstevel@tonic-gate  * p_iolock of the anonymous page for slot.
1180Sstevel@tonic-gate  */
1190Sstevel@tonic-gate extern kmutex_t anoninfo_lock;
1200Sstevel@tonic-gate extern kmutex_t swapinfo_lock;
12112173SMichael.Corcoran@Sun.COM extern pad_mutex_t *anonhash_lock;
1220Sstevel@tonic-gate extern pad_mutex_t anon_array_lock[];
1230Sstevel@tonic-gate extern kcondvar_t anon_array_cv[];
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate  * Global hash table to provide a function from (vp, off) -> ap
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate extern size_t anon_hash_size;
12912230SFrank.Rival@oracle.com extern unsigned int anon_hash_shift;
1300Sstevel@tonic-gate extern struct anon **anon_hash;
1310Sstevel@tonic-gate #define	ANON_HASH_SIZE	anon_hash_size
1320Sstevel@tonic-gate #define	ANON_HASHAVELEN	4
13312230SFrank.Rival@oracle.com /*
13412230SFrank.Rival@oracle.com  * Try to use as many bits of randomness from both vp and off as we can.
13512230SFrank.Rival@oracle.com  * This should help spreading evenly for a variety of workloads.  See comments
13612230SFrank.Rival@oracle.com  * for PAGE_HASH_FUNC for more explanation.
13712230SFrank.Rival@oracle.com  */
13812230SFrank.Rival@oracle.com #define	ANON_HASH(vp, off)	\
13912230SFrank.Rival@oracle.com 	(((((uintptr_t)(off) >> PAGESHIFT) ^ \
14012230SFrank.Rival@oracle.com 		((uintptr_t)(off) >> (PAGESHIFT + anon_hash_shift))) ^ \
14112230SFrank.Rival@oracle.com 		(((uintptr_t)(vp) >> 3) ^ \
14212230SFrank.Rival@oracle.com 		((uintptr_t)(vp) >> (3 + anon_hash_shift)) ^ \
14312230SFrank.Rival@oracle.com 		((uintptr_t)(vp) >> (3 + 2 * anon_hash_shift)) ^ \
14412230SFrank.Rival@oracle.com 		((uintptr_t)(vp) << \
14512230SFrank.Rival@oracle.com 		    (anon_hash_shift - AN_VPSHIFT - VNODE_ALIGN_LOG2)))) & \
14612230SFrank.Rival@oracle.com 		(anon_hash_size - 1))
1470Sstevel@tonic-gate 
14812173SMichael.Corcoran@Sun.COM #define	AH_LOCK_SIZE	(2 << NCPU_LOG2)
14912173SMichael.Corcoran@Sun.COM 
15012173SMichael.Corcoran@Sun.COM #define	AH_MUTEX(vp, off)				\
15112173SMichael.Corcoran@Sun.COM 	(&anonhash_lock[(ANON_HASH((vp), (off)) &	\
15212173SMichael.Corcoran@Sun.COM 	    (AH_LOCK_SIZE - 1))].pad_mutex)
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate #endif	/* _KERNEL */
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate  * Declaration for the Global counters to accurately
1580Sstevel@tonic-gate  * track the kernel foot print in memory.
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate extern  pgcnt_t pages_locked;
1610Sstevel@tonic-gate extern  pgcnt_t pages_claimed;
1620Sstevel@tonic-gate extern  pgcnt_t pages_useclaim;
1630Sstevel@tonic-gate extern  pgcnt_t obp_pages;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate  * Anonymous backing store accounting structure for swapctl.
1670Sstevel@tonic-gate  *
1680Sstevel@tonic-gate  * ani_max = maximum amount of swap space
1690Sstevel@tonic-gate  *	(including potentially available physical memory)
1700Sstevel@tonic-gate  * ani_free = amount of unallocated anonymous memory
1710Sstevel@tonic-gate  *	(some of which might be reserved and including
1720Sstevel@tonic-gate  *	potentially available physical memory)
1730Sstevel@tonic-gate  * ani_resv = amount of claimed (reserved) anonymous memory
1740Sstevel@tonic-gate  *
1750Sstevel@tonic-gate  * The swap data can be aquired more efficiently through the
1760Sstevel@tonic-gate  * kstats interface.
1770Sstevel@tonic-gate  * Total slots currently available for reservation =
1780Sstevel@tonic-gate  *	MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree)
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate struct anoninfo {
1810Sstevel@tonic-gate 	pgcnt_t	ani_max;
1820Sstevel@tonic-gate 	pgcnt_t	ani_free;
1830Sstevel@tonic-gate 	pgcnt_t	ani_resv;
1840Sstevel@tonic-gate };
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate #ifdef _SYSCALL32
1870Sstevel@tonic-gate struct anoninfo32 {
1880Sstevel@tonic-gate 	size32_t ani_max;
1890Sstevel@tonic-gate 	size32_t ani_free;
1900Sstevel@tonic-gate 	size32_t ani_resv;
1910Sstevel@tonic-gate };
1920Sstevel@tonic-gate #endif /* _SYSCALL32 */
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * Define the NCPU pool of the ani_free counters. Update the counter
1960Sstevel@tonic-gate  * of the cpu on which the thread is running and in every clock intr
1970Sstevel@tonic-gate  * sync anoninfo.ani_free with the current total off all the NCPU entries.
1980Sstevel@tonic-gate  */
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate typedef	struct	ani_free {
2010Sstevel@tonic-gate 	pgcnt_t		ani_count;
20212908SPavel.Tatashin@Sun.COM 	uchar_t		pad[64 - sizeof (pgcnt_t)];
2030Sstevel@tonic-gate 			/* XXX 64 = cacheline size */
2040Sstevel@tonic-gate } ani_free_t;
2050Sstevel@tonic-gate 
20612908SPavel.Tatashin@Sun.COM #define	ANI_MAX_POOL	(NCPU_P2)
20712908SPavel.Tatashin@Sun.COM extern	ani_free_t	*ani_free_pool;
2080Sstevel@tonic-gate 
20912908SPavel.Tatashin@Sun.COM /*
21012908SPavel.Tatashin@Sun.COM  * Since each CPU has its own bucket in ani_free_pool, there should be no
21112908SPavel.Tatashin@Sun.COM  * contention here.
21212908SPavel.Tatashin@Sun.COM  */
2130Sstevel@tonic-gate #define	ANI_ADD(inc)	{ \
21412908SPavel.Tatashin@Sun.COM 	pgcnt_t	*ani_countp; \
21512908SPavel.Tatashin@Sun.COM 	int	index; \
21612908SPavel.Tatashin@Sun.COM 	index = (CPU->cpu_seqid & (ANI_MAX_POOL - 1)); \
21712908SPavel.Tatashin@Sun.COM 	ani_countp = &ani_free_pool[index].ani_count; \
21812908SPavel.Tatashin@Sun.COM 	atomic_add_long(ani_countp, inc); \
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
22112908SPavel.Tatashin@Sun.COM extern void	set_anoninfo(void);
22212908SPavel.Tatashin@Sun.COM 
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate  * Anon array pointers are allocated in chunks. Each chunk
2250Sstevel@tonic-gate  * has PAGESIZE/sizeof(u_long *) of anon pointers.
2260Sstevel@tonic-gate  * There are two levels of arrays for anon array pointers larger
2270Sstevel@tonic-gate  * than a chunk. The first level points to anon array chunks.
2280Sstevel@tonic-gate  * The second level consists of chunks of anon pointers.
2290Sstevel@tonic-gate  *
2300Sstevel@tonic-gate  * If anon array is smaller than a chunk then the whole anon array
2310Sstevel@tonic-gate  * is created (memory is allocated for whole anon array).
2320Sstevel@tonic-gate  * If anon array is larger than a chunk only first level array is
2330Sstevel@tonic-gate  * allocated. Then other arrays (chunks) are allocated only when
2340Sstevel@tonic-gate  * they are initialized with anon pointers.
2350Sstevel@tonic-gate  */
2360Sstevel@tonic-gate struct anon_hdr {
2370Sstevel@tonic-gate 	kmutex_t serial_lock;	/* serialize array chunk allocation */
2380Sstevel@tonic-gate 	pgcnt_t	size;		/* number of pointers to (anon) pages */
2390Sstevel@tonic-gate 	void	**array_chunk;	/* pointers to anon pointers or chunks of */
2400Sstevel@tonic-gate 				/* anon pointers */
2410Sstevel@tonic-gate 	int	flags;		/* ANON_ALLOC_FORCE force preallocation of */
2420Sstevel@tonic-gate 				/* whole anon array	*/
2430Sstevel@tonic-gate };
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate #ifdef	_LP64
2460Sstevel@tonic-gate #define	ANON_PTRSHIFT	3
2470Sstevel@tonic-gate #define	ANON_PTRMASK	~7
2480Sstevel@tonic-gate #else
2490Sstevel@tonic-gate #define	ANON_PTRSHIFT	2
2500Sstevel@tonic-gate #define	ANON_PTRMASK	~3
2510Sstevel@tonic-gate #endif
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate #define	ANON_CHUNK_SIZE		(PAGESIZE >> ANON_PTRSHIFT)
2540Sstevel@tonic-gate #define	ANON_CHUNK_SHIFT	(PAGESHIFT - ANON_PTRSHIFT)
2550Sstevel@tonic-gate #define	ANON_CHUNK_OFF		(ANON_CHUNK_SIZE - 1)
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate  * Anon flags.
2590Sstevel@tonic-gate  */
2600Sstevel@tonic-gate #define	ANON_SLEEP		0x0	/* ok to block */
2610Sstevel@tonic-gate #define	ANON_NOSLEEP		0x1	/* non-blocking call */
2620Sstevel@tonic-gate #define	ANON_ALLOC_FORCE	0x2	/* force single level anon array */
2630Sstevel@tonic-gate #define	ANON_GROWDOWN		0x4	/* anon array should grow downward */
2640Sstevel@tonic-gate 
2652768Ssl108498 struct kshmid;
2662768Ssl108498 
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate  * The anon_map structure is used by various clients of the anon layer to
2690Sstevel@tonic-gate  * manage anonymous memory.   When anonymous memory is shared,
2700Sstevel@tonic-gate  * then the different clients sharing it will point to the
2710Sstevel@tonic-gate  * same anon_map structure.  Also, if a segment is unmapped
2720Sstevel@tonic-gate  * in the middle where an anon_map structure exists, the
2730Sstevel@tonic-gate  * newly created segment will also share the anon_map structure,
2740Sstevel@tonic-gate  * although the two segments will use different ranges of the
2750Sstevel@tonic-gate  * anon array.  When mappings are private (or shared with
2760Sstevel@tonic-gate  * a reference count of 1), an unmap operation will free up
2770Sstevel@tonic-gate  * a range of anon slots in the array given by the anon_map
2780Sstevel@tonic-gate  * structure.  Because of fragmentation due to this unmapping,
2790Sstevel@tonic-gate  * we have to store the size of the anon array in the anon_map
2800Sstevel@tonic-gate  * structure so that we can free everything when the referernce
2810Sstevel@tonic-gate  * count goes to zero.
2820Sstevel@tonic-gate  *
2830Sstevel@tonic-gate  * A new rangelock scheme is introduced to make the anon layer scale.
2840Sstevel@tonic-gate  * A reader/writer lock per anon_amp and an array of system-wide hash
2850Sstevel@tonic-gate  * locks, anon_array_lock[] are introduced to replace serial_lock and
2860Sstevel@tonic-gate  * anonmap lock.  The writer lock is held when we want to singlethreaD
2870Sstevel@tonic-gate  * the reference to the anon array pointers or when references to
2880Sstevel@tonic-gate  * anon_map's members, whereas reader lock and anon_array_lock are
2890Sstevel@tonic-gate  * held to allows multiple threads to reference different part of
2900Sstevel@tonic-gate  * anon array.  A global set of condition variables, anon_array_cv,
2910Sstevel@tonic-gate  * are used with anon_array_lock[] to make the hold time of the locks
2920Sstevel@tonic-gate  * short.
2930Sstevel@tonic-gate  *
2940Sstevel@tonic-gate  * szc is used to calculate the index of hash locks and cv's.  We
2950Sstevel@tonic-gate  * could've just used seg->s_szc if not for the possible sharing of
2960Sstevel@tonic-gate  * anon_amp between SYSV shared memory and ISM, so now we introduce
2970Sstevel@tonic-gate  * szc in the anon_map structure.  For MAP_SHARED, the amp->szc is either
2980Sstevel@tonic-gate  * 0 (base page size) or page_num_pagesizes() - 1, while MAP_PRIVATE
2990Sstevel@tonic-gate  * the amp->szc could be anything in [0, page_num_pagesizes() - 1].
3000Sstevel@tonic-gate  */
3016695Saguzovsk typedef struct anon_map {
3020Sstevel@tonic-gate 	krwlock_t a_rwlock;	/* protect anon_map and anon array */
3030Sstevel@tonic-gate 	size_t	size;		/* size in bytes mapped by the anon array */
3040Sstevel@tonic-gate 	struct	anon_hdr *ahp; 	/* anon array header pointer, containing */
3050Sstevel@tonic-gate 				/* anon pointer array(s) */
3060Sstevel@tonic-gate 	size_t	swresv;		/* swap space reserved for this anon_map */
3072414Saguzovsk 	ulong_t	refcnt;		/* reference count on this structure */
3080Sstevel@tonic-gate 	ushort_t a_szc;		/* max szc among shared processes */
3090Sstevel@tonic-gate 	void	*locality;	/* lgroup locality info */
3102768Ssl108498 	struct kshmid *a_sp;	/* kshmid if amp backs sysV, or NULL */
3116695Saguzovsk 	int	a_purgewait;	/* somebody waits for slocks to go away */
3126695Saguzovsk 	kcondvar_t a_purgecv;	/* cv for waiting for slocks to go away */
3136695Saguzovsk 	kmutex_t a_purgemtx;	/* mutex for anonmap_purge() */
3146695Saguzovsk 	spgcnt_t a_softlockcnt; /* number of pages locked in pcache */
3156695Saguzovsk 	kmutex_t a_pmtx;	/* protects amp's pcache list */
3166695Saguzovsk 	pcache_link_t a_phead;	/* head of amp's pcache list */
3176695Saguzovsk } amp_t;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate #ifdef _KERNEL
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate #define	ANON_BUSY		0x1
3220Sstevel@tonic-gate #define	ANON_ISBUSY(slot)	(*(slot) & ANON_BUSY)
3230Sstevel@tonic-gate #define	ANON_SETBUSY(slot)	(*(slot) |= ANON_BUSY)
3240Sstevel@tonic-gate #define	ANON_CLRBUSY(slot)	(*(slot) &= ~ANON_BUSY)
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate #define	ANON_MAP_SHIFT		6	/* log2(sizeof (struct anon_map)) */
3270Sstevel@tonic-gate #define	ANON_ARRAY_SHIFT	7	/* log2(ANON_LOCKSIZE) */
3280Sstevel@tonic-gate #define	ANON_LOCKSIZE		128
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate #define	ANON_LOCK_ENTER(lock, type)	rw_enter((lock), (type))
3310Sstevel@tonic-gate #define	ANON_LOCK_EXIT(lock)		rw_exit((lock))
3326695Saguzovsk #define	ANON_LOCK_HELD(lock)		RW_LOCK_HELD((lock))
3336695Saguzovsk #define	ANON_READ_HELD(lock)		RW_READ_HELD((lock))
3346695Saguzovsk #define	ANON_WRITE_HELD(lock)		RW_WRITE_HELD((lock))
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate #define	ANON_ARRAY_HASH(amp, idx)\
3370Sstevel@tonic-gate 	((((idx) + ((idx) >> ANON_ARRAY_SHIFT) +\
3380Sstevel@tonic-gate 	((idx) >> (ANON_ARRAY_SHIFT << 1)) +\
3390Sstevel@tonic-gate 	((idx) >> (ANON_ARRAY_SHIFT + (ANON_ARRAY_SHIFT << 1)))) ^\
3400Sstevel@tonic-gate 	((uintptr_t)(amp) >> ANON_MAP_SHIFT)) & (ANON_LOCKSIZE - 1))
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate typedef struct anon_sync_obj {
3430Sstevel@tonic-gate 	kmutex_t	*sync_mutex;
3440Sstevel@tonic-gate 	kcondvar_t	*sync_cv;
3450Sstevel@tonic-gate 	ulong_t		*sync_data;
3460Sstevel@tonic-gate } anon_sync_obj_t;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate /*
3490Sstevel@tonic-gate  * Anonymous backing store accounting structure for kernel.
3500Sstevel@tonic-gate  * ani_max = total reservable slots on physical (disk-backed) swap
3510Sstevel@tonic-gate  * ani_phys_resv = total phys slots reserved for use by clients
3520Sstevel@tonic-gate  * ani_mem_resv = total mem slots reserved for use by clients
3530Sstevel@tonic-gate  * ani_free = # unallocated physical slots + # of reserved unallocated
3540Sstevel@tonic-gate  * memory slots
3550Sstevel@tonic-gate  */
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * Initial total swap slots available for reservation
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate #define	TOTAL_AVAILABLE_SWAP \
3610Sstevel@tonic-gate 	(k_anoninfo.ani_max + MAX((spgcnt_t)(availrmem - swapfs_minfree), 0))
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate  * Swap slots currently available for reservation
3650Sstevel@tonic-gate  */
3666695Saguzovsk #define	CURRENT_TOTAL_AVAILABLE_SWAP				\
3670Sstevel@tonic-gate 	((k_anoninfo.ani_max - k_anoninfo.ani_phys_resv) +	\
3686695Saguzovsk 	    MAX((spgcnt_t)(availrmem - swapfs_minfree), 0))
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate struct k_anoninfo {
3710Sstevel@tonic-gate 	pgcnt_t	ani_max;	/* total reservable slots on phys */
3720Sstevel@tonic-gate 					/* (disk) swap */
3730Sstevel@tonic-gate 	pgcnt_t	ani_free;	/* # of unallocated phys and mem slots */
3740Sstevel@tonic-gate 	pgcnt_t	ani_phys_resv;	/* # of reserved phys (disk) slots */
3750Sstevel@tonic-gate 	pgcnt_t	ani_mem_resv;	/* # of reserved mem slots */
3760Sstevel@tonic-gate 	pgcnt_t	ani_locked_swap; /* # of swap slots locked in reserved */
3770Sstevel@tonic-gate 				/* mem swap */
3780Sstevel@tonic-gate };
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate extern	struct k_anoninfo k_anoninfo;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate extern void	anon_init(void);
3830Sstevel@tonic-gate extern struct	anon *anon_alloc(struct vnode *, anoff_t);
3840Sstevel@tonic-gate extern void	anon_dup(struct anon_hdr *, ulong_t,
3850Sstevel@tonic-gate 		    struct anon_hdr *, ulong_t, size_t);
3860Sstevel@tonic-gate extern void	anon_dup_fill_holes(struct anon_hdr *, ulong_t,
3870Sstevel@tonic-gate 		    struct anon_hdr *, ulong_t, size_t, uint_t, int);
3880Sstevel@tonic-gate extern int	anon_fill_cow_holes(struct seg *, caddr_t, struct anon_hdr *,
3890Sstevel@tonic-gate 		    ulong_t, struct vnode *, u_offset_t, size_t, uint_t,
3900Sstevel@tonic-gate 		    uint_t, struct vpage [], struct cred *);
3910Sstevel@tonic-gate extern void	anon_free(struct anon_hdr *, ulong_t, size_t);
3920Sstevel@tonic-gate extern void	anon_free_pages(struct anon_hdr *, ulong_t, size_t, uint_t);
3935224Smec extern void	anon_disclaim(struct anon_map *, ulong_t, size_t);
3940Sstevel@tonic-gate extern int	anon_getpage(struct anon **, uint_t *, struct page **,
3950Sstevel@tonic-gate 		    size_t, struct seg *, caddr_t, enum seg_rw, struct cred *);
3960Sstevel@tonic-gate extern int	swap_getconpage(struct vnode *, u_offset_t, size_t,
3972414Saguzovsk 		    uint_t *, page_t *[], size_t, page_t *, uint_t *,
3980Sstevel@tonic-gate 		    spgcnt_t *, struct seg *, caddr_t,
3990Sstevel@tonic-gate 		    enum seg_rw, struct cred *);
4000Sstevel@tonic-gate extern int	anon_map_getpages(struct anon_map *, ulong_t,
4010Sstevel@tonic-gate 		    uint_t, struct seg *, caddr_t, uint_t,
4020Sstevel@tonic-gate 		    uint_t *, page_t *[], uint_t *,
4034426Saguzovsk 		    struct vpage [], enum seg_rw, int, int, int, struct cred *);
4040Sstevel@tonic-gate extern int	anon_map_privatepages(struct anon_map *, ulong_t,
4050Sstevel@tonic-gate 		    uint_t, struct seg *, caddr_t, uint_t,
4064426Saguzovsk 		    page_t *[], struct vpage [], int, int, struct cred *);
4070Sstevel@tonic-gate extern struct	page *anon_private(struct anon **, struct seg *,
4080Sstevel@tonic-gate 		    caddr_t, uint_t, struct page *,
4090Sstevel@tonic-gate 		    int, struct cred *);
4100Sstevel@tonic-gate extern struct	page *anon_zero(struct seg *, caddr_t,
4110Sstevel@tonic-gate 		    struct anon **, struct cred *);
4120Sstevel@tonic-gate extern int	anon_map_createpages(struct anon_map *, ulong_t,
4130Sstevel@tonic-gate 		    size_t, struct page **,
4140Sstevel@tonic-gate 		    struct seg *, caddr_t,
4150Sstevel@tonic-gate 		    enum seg_rw, struct cred *);
4160Sstevel@tonic-gate extern int	anon_map_demotepages(struct anon_map *, ulong_t,
4170Sstevel@tonic-gate 		    struct seg *, caddr_t, uint_t,
4180Sstevel@tonic-gate 		    struct vpage [], struct cred *);
4192414Saguzovsk extern void	anon_shmap_free_pages(struct anon_map *, ulong_t, size_t);
4204426Saguzovsk extern int	anon_resvmem(size_t, boolean_t, zone_t *, int);
4213247Sgjelinek extern void	anon_unresvmem(size_t, zone_t *);
4224426Saguzovsk extern struct	anon_map *anonmap_alloc(size_t, size_t, int);
4230Sstevel@tonic-gate extern void	anonmap_free(struct anon_map *);
4246695Saguzovsk extern void	anonmap_purge(struct anon_map *);
4256695Saguzovsk extern void	anon_swap_free(struct anon *, struct page *);
4260Sstevel@tonic-gate extern void	anon_decref(struct anon *);
4270Sstevel@tonic-gate extern int	non_anon(struct anon_hdr *, ulong_t, u_offset_t *, size_t *);
4280Sstevel@tonic-gate extern pgcnt_t	anon_pages(struct anon_hdr *, ulong_t, pgcnt_t);
429*13035SOndrej.Kubecka@Sun.COM extern int	anon_swap_adjust(pgcnt_t);
4300Sstevel@tonic-gate extern void	anon_swap_restore(pgcnt_t);
4310Sstevel@tonic-gate extern struct	anon_hdr *anon_create(pgcnt_t, int);
4320Sstevel@tonic-gate extern void	anon_release(struct anon_hdr *, pgcnt_t);
4330Sstevel@tonic-gate extern struct	anon *anon_get_ptr(struct anon_hdr *, ulong_t);
4340Sstevel@tonic-gate extern ulong_t	*anon_get_slot(struct anon_hdr *, ulong_t);
4350Sstevel@tonic-gate extern struct	anon *anon_get_next_ptr(struct anon_hdr *, ulong_t *);
4360Sstevel@tonic-gate extern int	anon_set_ptr(struct anon_hdr *, ulong_t, struct anon *, int);
4370Sstevel@tonic-gate extern int 	anon_copy_ptr(struct anon_hdr *, ulong_t,
4380Sstevel@tonic-gate 		    struct anon_hdr *, ulong_t, pgcnt_t, int);
4390Sstevel@tonic-gate extern pgcnt_t	anon_grow(struct anon_hdr *, ulong_t *, pgcnt_t, pgcnt_t, int);
4400Sstevel@tonic-gate extern void	anon_array_enter(struct anon_map *, ulong_t,
4410Sstevel@tonic-gate 			anon_sync_obj_t *);
442888Scwb extern int	anon_array_try_enter(struct anon_map *, ulong_t,
443888Scwb 			anon_sync_obj_t *);
4440Sstevel@tonic-gate extern void	anon_array_exit(anon_sync_obj_t *);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate  * anon_resv checks to see if there is enough swap space to fulfill a
4480Sstevel@tonic-gate  * request and if so, reserves the appropriate anonymous memory resources.
4490Sstevel@tonic-gate  * anon_checkspace just checks to see if there is space to fulfill the request,
4500Sstevel@tonic-gate  * without taking any resources.  Both return 1 if successful and 0 if not.
4513247Sgjelinek  *
4523247Sgjelinek  * Macros are provided as anon reservation is usually charged to the zone of
4533247Sgjelinek  * the current process.  In some cases (such as anon reserved by tmpfs), a
4543247Sgjelinek  * zone pointer is needed to charge the appropriate zone.
4550Sstevel@tonic-gate  */
4563247Sgjelinek #define	anon_unresv(size)		anon_unresvmem(size, curproc->p_zone)
4573247Sgjelinek #define	anon_unresv_zone(size, zone)	anon_unresvmem(size, zone)
4584426Saguzovsk #define	anon_resv(size)			\
4594426Saguzovsk 	anon_resvmem((size), 1, curproc->p_zone, 1)
4604426Saguzovsk #define	anon_resv_zone(size, zone)	anon_resvmem((size), 1, zone, 1)
4614789Sjj204856 #define	anon_checkspace(size, zone)	anon_resvmem((size), 0, zone, 0)
4624426Saguzovsk #define	anon_try_resv_zone(size, zone)	anon_resvmem((size), 1, zone, 0)
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate /*
4650Sstevel@tonic-gate  * Flags to anon_private
4660Sstevel@tonic-gate  */
4670Sstevel@tonic-gate #define	STEAL_PAGE	0x1	/* page can be stolen */
4680Sstevel@tonic-gate #define	LOCK_PAGE	0x2	/* page must be ``logically'' locked */
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate /*
4710Sstevel@tonic-gate  * SEGKP ANON pages that are locked are assumed to be LWP stack pages
4720Sstevel@tonic-gate  * and thus count towards the user pages locked count.
4730Sstevel@tonic-gate  * This value is protected by the same lock as availrmem.
4740Sstevel@tonic-gate  */
4750Sstevel@tonic-gate extern pgcnt_t anon_segkp_pages_locked;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate extern int anon_debug;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate #ifdef ANON_DEBUG
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate #define	A_ANON	0x01
4820Sstevel@tonic-gate #define	A_RESV	0x02
4830Sstevel@tonic-gate #define	A_MRESV	0x04
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate /* vararg-like debugging macro. */
4860Sstevel@tonic-gate #define	ANON_PRINT(f, printf_args) \
4870Sstevel@tonic-gate 		if (anon_debug & f) \
4880Sstevel@tonic-gate 			printf printf_args
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate #else	/* ANON_DEBUG */
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate #define	ANON_PRINT(f, printf_args)
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate #endif	/* ANON_DEBUG */
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate #endif	/* _KERNEL */
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate #ifdef	__cplusplus
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate #endif
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate #endif	/* _VM_ANON_H */
503