xref: /illumos-gate/usr/src/uts/common/vm/anon.h (revision 2cb27123)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
507b65a64Saguzovsk  * Common Development and Distribution License (the "License").
607b65a64Saguzovsk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*2cb27123Saguzovsk  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /*	 All Rights Reserved   */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #ifndef	_VM_ANON_H
407c478bd9Sstevel@tonic-gate #define	_VM_ANON_H
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/cred.h>
450209230bSgjelinek #include <sys/zone.h>
467c478bd9Sstevel@tonic-gate #include <vm/seg.h>
477c478bd9Sstevel@tonic-gate #include <vm/vpage.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
507c478bd9Sstevel@tonic-gate extern "C" {
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * VM - Anonymous pages.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate typedef	unsigned long anoff_t;		/* anon offsets */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  *	Each anonymous page, either in memory or in swap, has an anon structure.
617c478bd9Sstevel@tonic-gate  * The structure (slot) provides a level of indirection between anonymous pages
627c478bd9Sstevel@tonic-gate  * and their backing store.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  *	(an_vp, an_off) names the vnode of the anonymous page for this slot.
657c478bd9Sstevel@tonic-gate  *
667c478bd9Sstevel@tonic-gate  * 	(an_pvp, an_poff) names the location of the physical backing store
677c478bd9Sstevel@tonic-gate  * 	for the page this slot represents. If the name is null there is no
687c478bd9Sstevel@tonic-gate  * 	associated physical store. The physical backing store location can
697c478bd9Sstevel@tonic-gate  *	change while the slot is in use.
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  *	an_hash is a hash list of anon slots. The list is hashed by
727c478bd9Sstevel@tonic-gate  * 	(an_vp, an_off) of the associated anonymous page and provides a
737c478bd9Sstevel@tonic-gate  *	method of going from the name of an anonymous page to its
747c478bd9Sstevel@tonic-gate  * 	associated anon slot.
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  *	an_refcnt holds a reference count which is the number of separate
777c478bd9Sstevel@tonic-gate  * 	copies that will need to be created in case of copy-on-write.
787c478bd9Sstevel@tonic-gate  *	A refcnt > 0 protects the existence of the slot. The refcnt is
797c478bd9Sstevel@tonic-gate  * 	initialized to 1 when the anon slot is created in anon_alloc().
807c478bd9Sstevel@tonic-gate  *	If a client obtains an anon slot and allows multiple threads to
817c478bd9Sstevel@tonic-gate  * 	share it, then it is the client's responsibility to insure that
827c478bd9Sstevel@tonic-gate  *	it does not allow one thread to try to reference the slot at the
837c478bd9Sstevel@tonic-gate  *	same time as another is trying to decrement the last count and
847c478bd9Sstevel@tonic-gate  *	destroy the anon slot. E.g., the seg_vn segment type protects
857c478bd9Sstevel@tonic-gate  *	against this with higher level locks.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate struct anon {
897c478bd9Sstevel@tonic-gate 	struct vnode *an_vp;	/* vnode of anon page */
907c478bd9Sstevel@tonic-gate 	struct vnode *an_pvp;	/* vnode of physical backing store */
917c478bd9Sstevel@tonic-gate 	anoff_t an_off;		/* offset of anon page */
927c478bd9Sstevel@tonic-gate 	anoff_t an_poff;	/* offset in vnode */
937c478bd9Sstevel@tonic-gate 	struct anon *an_hash;	/* hash table of anon slots */
947c478bd9Sstevel@tonic-gate 	int an_refcnt;		/* # of people sharing slot */
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #ifdef _KERNEL
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * The swapinfo_lock protects:
1007c478bd9Sstevel@tonic-gate  *		swapinfo list
1017c478bd9Sstevel@tonic-gate  *		individual swapinfo structures
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * The anoninfo_lock protects:
1047c478bd9Sstevel@tonic-gate  *		anoninfo counters
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  * The anonhash_lock protects:
1077c478bd9Sstevel@tonic-gate  *		anon hash lists
1087c478bd9Sstevel@tonic-gate  *		anon slot fields
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  * Fields in the anon slot which are read-only for the life of the slot
1117c478bd9Sstevel@tonic-gate  * (an_vp, an_off) do not require the anonhash_lock be held to access them.
1127c478bd9Sstevel@tonic-gate  * If you access a field without the anonhash_lock held you must be holding
1137c478bd9Sstevel@tonic-gate  * the slot with an_refcnt to make sure it isn't destroyed.
1147c478bd9Sstevel@tonic-gate  * To write (an_pvp, an_poff) in a given slot you must also hold the
1157c478bd9Sstevel@tonic-gate  * p_iolock of the anonymous page for slot.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate extern kmutex_t anoninfo_lock;
1187c478bd9Sstevel@tonic-gate extern kmutex_t swapinfo_lock;
1197c478bd9Sstevel@tonic-gate extern kmutex_t anonhash_lock[];
1207c478bd9Sstevel@tonic-gate extern pad_mutex_t anon_array_lock[];
1217c478bd9Sstevel@tonic-gate extern kcondvar_t anon_array_cv[];
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Global hash table to provide a function from (vp, off) -> ap
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate extern size_t anon_hash_size;
1277c478bd9Sstevel@tonic-gate extern struct anon **anon_hash;
1287c478bd9Sstevel@tonic-gate #define	ANON_HASH_SIZE	anon_hash_size
1297c478bd9Sstevel@tonic-gate #define	ANON_HASHAVELEN	4
1307c478bd9Sstevel@tonic-gate #define	ANON_HASH(VP, OFF)	\
1317c478bd9Sstevel@tonic-gate ((((uintptr_t)(VP) >> 7)  ^ ((OFF) >> PAGESHIFT)) & (ANON_HASH_SIZE - 1))
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #define	AH_LOCK_SIZE	64
1347c478bd9Sstevel@tonic-gate #define	AH_LOCK(vp, off) (ANON_HASH((vp), (off)) & (AH_LOCK_SIZE -1))
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * Declaration for the Global counters to accurately
1407c478bd9Sstevel@tonic-gate  * track the kernel foot print in memory.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate extern  pgcnt_t segvn_pages_locked;
1437c478bd9Sstevel@tonic-gate extern  pgcnt_t pages_locked;
1447c478bd9Sstevel@tonic-gate extern  pgcnt_t pages_claimed;
1457c478bd9Sstevel@tonic-gate extern  pgcnt_t pages_useclaim;
1467c478bd9Sstevel@tonic-gate extern  pgcnt_t obp_pages;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate  * Anonymous backing store accounting structure for swapctl.
1507c478bd9Sstevel@tonic-gate  *
1517c478bd9Sstevel@tonic-gate  * ani_max = maximum amount of swap space
1527c478bd9Sstevel@tonic-gate  *	(including potentially available physical memory)
1537c478bd9Sstevel@tonic-gate  * ani_free = amount of unallocated anonymous memory
1547c478bd9Sstevel@tonic-gate  *	(some of which might be reserved and including
1557c478bd9Sstevel@tonic-gate  *	potentially available physical memory)
1567c478bd9Sstevel@tonic-gate  * ani_resv = amount of claimed (reserved) anonymous memory
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * The swap data can be aquired more efficiently through the
1597c478bd9Sstevel@tonic-gate  * kstats interface.
1607c478bd9Sstevel@tonic-gate  * Total slots currently available for reservation =
1617c478bd9Sstevel@tonic-gate  *	MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree)
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate struct anoninfo {
1647c478bd9Sstevel@tonic-gate 	pgcnt_t	ani_max;
1657c478bd9Sstevel@tonic-gate 	pgcnt_t	ani_free;
1667c478bd9Sstevel@tonic-gate 	pgcnt_t	ani_resv;
1677c478bd9Sstevel@tonic-gate };
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32
1707c478bd9Sstevel@tonic-gate struct anoninfo32 {
1717c478bd9Sstevel@tonic-gate 	size32_t ani_max;
1727c478bd9Sstevel@tonic-gate 	size32_t ani_free;
1737c478bd9Sstevel@tonic-gate 	size32_t ani_resv;
1747c478bd9Sstevel@tonic-gate };
1757c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32 */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * Define the NCPU pool of the ani_free counters. Update the counter
1797c478bd9Sstevel@tonic-gate  * of the cpu on which the thread is running and in every clock intr
1807c478bd9Sstevel@tonic-gate  * sync anoninfo.ani_free with the current total off all the NCPU entries.
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate typedef	struct	ani_free {
1847c478bd9Sstevel@tonic-gate 	kmutex_t	ani_lock;
1857c478bd9Sstevel@tonic-gate 	pgcnt_t		ani_count;
1867c478bd9Sstevel@tonic-gate 	uchar_t		pad[64 - sizeof (kmutex_t) - sizeof (pgcnt_t)];
1877c478bd9Sstevel@tonic-gate 			/* XXX 64 = cacheline size */
1887c478bd9Sstevel@tonic-gate } ani_free_t;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate #define	ANI_MAX_POOL	128
1917c478bd9Sstevel@tonic-gate extern	ani_free_t	ani_free_pool[];
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate #define	ANI_ADD(inc)	{ \
1947c478bd9Sstevel@tonic-gate 	ani_free_t	*anifp; \
1957c478bd9Sstevel@tonic-gate 	int		index; \
1967c478bd9Sstevel@tonic-gate 	index = (CPU->cpu_id & (ANI_MAX_POOL - 1)); \
1977c478bd9Sstevel@tonic-gate 	anifp = &ani_free_pool[index]; \
1987c478bd9Sstevel@tonic-gate 	mutex_enter(&anifp->ani_lock); \
1997c478bd9Sstevel@tonic-gate 	anifp->ani_count += inc; \
2007c478bd9Sstevel@tonic-gate 	mutex_exit(&anifp->ani_lock); \
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate  * Anon array pointers are allocated in chunks. Each chunk
2057c478bd9Sstevel@tonic-gate  * has PAGESIZE/sizeof(u_long *) of anon pointers.
2067c478bd9Sstevel@tonic-gate  * There are two levels of arrays for anon array pointers larger
2077c478bd9Sstevel@tonic-gate  * than a chunk. The first level points to anon array chunks.
2087c478bd9Sstevel@tonic-gate  * The second level consists of chunks of anon pointers.
2097c478bd9Sstevel@tonic-gate  *
2107c478bd9Sstevel@tonic-gate  * If anon array is smaller than a chunk then the whole anon array
2117c478bd9Sstevel@tonic-gate  * is created (memory is allocated for whole anon array).
2127c478bd9Sstevel@tonic-gate  * If anon array is larger than a chunk only first level array is
2137c478bd9Sstevel@tonic-gate  * allocated. Then other arrays (chunks) are allocated only when
2147c478bd9Sstevel@tonic-gate  * they are initialized with anon pointers.
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate struct anon_hdr {
2177c478bd9Sstevel@tonic-gate 	kmutex_t serial_lock;	/* serialize array chunk allocation */
2187c478bd9Sstevel@tonic-gate 	pgcnt_t	size;		/* number of pointers to (anon) pages */
2197c478bd9Sstevel@tonic-gate 	void	**array_chunk;	/* pointers to anon pointers or chunks of */
2207c478bd9Sstevel@tonic-gate 				/* anon pointers */
2217c478bd9Sstevel@tonic-gate 	int	flags;		/* ANON_ALLOC_FORCE force preallocation of */
2227c478bd9Sstevel@tonic-gate 				/* whole anon array	*/
2237c478bd9Sstevel@tonic-gate };
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate #ifdef	_LP64
2267c478bd9Sstevel@tonic-gate #define	ANON_PTRSHIFT	3
2277c478bd9Sstevel@tonic-gate #define	ANON_PTRMASK	~7
2287c478bd9Sstevel@tonic-gate #else
2297c478bd9Sstevel@tonic-gate #define	ANON_PTRSHIFT	2
2307c478bd9Sstevel@tonic-gate #define	ANON_PTRMASK	~3
2317c478bd9Sstevel@tonic-gate #endif
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate #define	ANON_CHUNK_SIZE		(PAGESIZE >> ANON_PTRSHIFT)
2347c478bd9Sstevel@tonic-gate #define	ANON_CHUNK_SHIFT	(PAGESHIFT - ANON_PTRSHIFT)
2357c478bd9Sstevel@tonic-gate #define	ANON_CHUNK_OFF		(ANON_CHUNK_SIZE - 1)
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate  * Anon flags.
2397c478bd9Sstevel@tonic-gate  */
2407c478bd9Sstevel@tonic-gate #define	ANON_SLEEP		0x0	/* ok to block */
2417c478bd9Sstevel@tonic-gate #define	ANON_NOSLEEP		0x1	/* non-blocking call */
2427c478bd9Sstevel@tonic-gate #define	ANON_ALLOC_FORCE	0x2	/* force single level anon array */
2437c478bd9Sstevel@tonic-gate #define	ANON_GROWDOWN		0x4	/* anon array should grow downward */
2447c478bd9Sstevel@tonic-gate 
245c6939658Ssl struct kshmid;
246c6939658Ssl 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * The anon_map structure is used by various clients of the anon layer to
2497c478bd9Sstevel@tonic-gate  * manage anonymous memory.   When anonymous memory is shared,
2507c478bd9Sstevel@tonic-gate  * then the different clients sharing it will point to the
2517c478bd9Sstevel@tonic-gate  * same anon_map structure.  Also, if a segment is unmapped
2527c478bd9Sstevel@tonic-gate  * in the middle where an anon_map structure exists, the
2537c478bd9Sstevel@tonic-gate  * newly created segment will also share the anon_map structure,
2547c478bd9Sstevel@tonic-gate  * although the two segments will use different ranges of the
2557c478bd9Sstevel@tonic-gate  * anon array.  When mappings are private (or shared with
2567c478bd9Sstevel@tonic-gate  * a reference count of 1), an unmap operation will free up
2577c478bd9Sstevel@tonic-gate  * a range of anon slots in the array given by the anon_map
2587c478bd9Sstevel@tonic-gate  * structure.  Because of fragmentation due to this unmapping,
2597c478bd9Sstevel@tonic-gate  * we have to store the size of the anon array in the anon_map
2607c478bd9Sstevel@tonic-gate  * structure so that we can free everything when the referernce
2617c478bd9Sstevel@tonic-gate  * count goes to zero.
2627c478bd9Sstevel@tonic-gate  *
2637c478bd9Sstevel@tonic-gate  * A new rangelock scheme is introduced to make the anon layer scale.
2647c478bd9Sstevel@tonic-gate  * A reader/writer lock per anon_amp and an array of system-wide hash
2657c478bd9Sstevel@tonic-gate  * locks, anon_array_lock[] are introduced to replace serial_lock and
2667c478bd9Sstevel@tonic-gate  * anonmap lock.  The writer lock is held when we want to singlethreaD
2677c478bd9Sstevel@tonic-gate  * the reference to the anon array pointers or when references to
2687c478bd9Sstevel@tonic-gate  * anon_map's members, whereas reader lock and anon_array_lock are
2697c478bd9Sstevel@tonic-gate  * held to allows multiple threads to reference different part of
2707c478bd9Sstevel@tonic-gate  * anon array.  A global set of condition variables, anon_array_cv,
2717c478bd9Sstevel@tonic-gate  * are used with anon_array_lock[] to make the hold time of the locks
2727c478bd9Sstevel@tonic-gate  * short.
2737c478bd9Sstevel@tonic-gate  *
2747c478bd9Sstevel@tonic-gate  * szc is used to calculate the index of hash locks and cv's.  We
2757c478bd9Sstevel@tonic-gate  * could've just used seg->s_szc if not for the possible sharing of
2767c478bd9Sstevel@tonic-gate  * anon_amp between SYSV shared memory and ISM, so now we introduce
2777c478bd9Sstevel@tonic-gate  * szc in the anon_map structure.  For MAP_SHARED, the amp->szc is either
2787c478bd9Sstevel@tonic-gate  * 0 (base page size) or page_num_pagesizes() - 1, while MAP_PRIVATE
2797c478bd9Sstevel@tonic-gate  * the amp->szc could be anything in [0, page_num_pagesizes() - 1].
2807c478bd9Sstevel@tonic-gate  */
2817c478bd9Sstevel@tonic-gate struct anon_map {
2827c478bd9Sstevel@tonic-gate 	krwlock_t a_rwlock;	/* protect anon_map and anon array */
2837c478bd9Sstevel@tonic-gate 	size_t	size;		/* size in bytes mapped by the anon array */
2847c478bd9Sstevel@tonic-gate 	struct	anon_hdr *ahp; 	/* anon array header pointer, containing */
2857c478bd9Sstevel@tonic-gate 				/* anon pointer array(s) */
2867c478bd9Sstevel@tonic-gate 	size_t	swresv;		/* swap space reserved for this anon_map */
28707b65a64Saguzovsk 	ulong_t	refcnt;		/* reference count on this structure */
2887c478bd9Sstevel@tonic-gate 	ushort_t a_szc;		/* max szc among shared processes */
2897c478bd9Sstevel@tonic-gate 	void	*locality;	/* lgroup locality info */
290c6939658Ssl 	struct kshmid *a_sp;	/* kshmid if amp backs sysV, or NULL */
2917c478bd9Sstevel@tonic-gate };
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate #ifdef _KERNEL
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate #define	ANON_BUSY		0x1
2967c478bd9Sstevel@tonic-gate #define	ANON_ISBUSY(slot)	(*(slot) & ANON_BUSY)
2977c478bd9Sstevel@tonic-gate #define	ANON_SETBUSY(slot)	(*(slot) |= ANON_BUSY)
2987c478bd9Sstevel@tonic-gate #define	ANON_CLRBUSY(slot)	(*(slot) &= ~ANON_BUSY)
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate #define	ANON_MAP_SHIFT		6	/* log2(sizeof (struct anon_map)) */
3017c478bd9Sstevel@tonic-gate #define	ANON_ARRAY_SHIFT	7	/* log2(ANON_LOCKSIZE) */
3027c478bd9Sstevel@tonic-gate #define	ANON_LOCKSIZE		128
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate #define	ANON_LOCK_ENTER(lock, type)	rw_enter((lock), (type))
3057c478bd9Sstevel@tonic-gate #define	ANON_LOCK_EXIT(lock)		rw_exit((lock))
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate #define	ANON_ARRAY_HASH(amp, idx)\
3087c478bd9Sstevel@tonic-gate 	((((idx) + ((idx) >> ANON_ARRAY_SHIFT) +\
3097c478bd9Sstevel@tonic-gate 	((idx) >> (ANON_ARRAY_SHIFT << 1)) +\
3107c478bd9Sstevel@tonic-gate 	((idx) >> (ANON_ARRAY_SHIFT + (ANON_ARRAY_SHIFT << 1)))) ^\
3117c478bd9Sstevel@tonic-gate 	((uintptr_t)(amp) >> ANON_MAP_SHIFT)) & (ANON_LOCKSIZE - 1))
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate typedef struct anon_sync_obj {
3147c478bd9Sstevel@tonic-gate 	kmutex_t	*sync_mutex;
3157c478bd9Sstevel@tonic-gate 	kcondvar_t	*sync_cv;
3167c478bd9Sstevel@tonic-gate 	ulong_t		*sync_data;
3177c478bd9Sstevel@tonic-gate } anon_sync_obj_t;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  * Anonymous backing store accounting structure for kernel.
3217c478bd9Sstevel@tonic-gate  * ani_max = total reservable slots on physical (disk-backed) swap
3227c478bd9Sstevel@tonic-gate  * ani_phys_resv = total phys slots reserved for use by clients
3237c478bd9Sstevel@tonic-gate  * ani_mem_resv = total mem slots reserved for use by clients
3247c478bd9Sstevel@tonic-gate  * ani_free = # unallocated physical slots + # of reserved unallocated
3257c478bd9Sstevel@tonic-gate  * memory slots
3267c478bd9Sstevel@tonic-gate  */
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * Initial total swap slots available for reservation
3307c478bd9Sstevel@tonic-gate  */
3317c478bd9Sstevel@tonic-gate #define	TOTAL_AVAILABLE_SWAP \
3327c478bd9Sstevel@tonic-gate 	(k_anoninfo.ani_max + MAX((spgcnt_t)(availrmem - swapfs_minfree), 0))
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate  * Swap slots currently available for reservation
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@tonic-gate #define	CURRENT_TOTAL_AVAILABLE_SWAP \
3387c478bd9Sstevel@tonic-gate 	((k_anoninfo.ani_max - k_anoninfo.ani_phys_resv) +	\
3397c478bd9Sstevel@tonic-gate 			MAX((spgcnt_t)(availrmem - swapfs_minfree), 0))
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate struct k_anoninfo {
3427c478bd9Sstevel@tonic-gate 	pgcnt_t	ani_max;	/* total reservable slots on phys */
3437c478bd9Sstevel@tonic-gate 					/* (disk) swap */
3447c478bd9Sstevel@tonic-gate 	pgcnt_t	ani_free;	/* # of unallocated phys and mem slots */
3457c478bd9Sstevel@tonic-gate 	pgcnt_t	ani_phys_resv;	/* # of reserved phys (disk) slots */
3467c478bd9Sstevel@tonic-gate 	pgcnt_t	ani_mem_resv;	/* # of reserved mem slots */
3477c478bd9Sstevel@tonic-gate 	pgcnt_t	ani_locked_swap; /* # of swap slots locked in reserved */
3487c478bd9Sstevel@tonic-gate 				/* mem swap */
3497c478bd9Sstevel@tonic-gate };
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate extern	struct k_anoninfo k_anoninfo;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate extern void	anon_init(void);
3547c478bd9Sstevel@tonic-gate extern struct	anon *anon_alloc(struct vnode *, anoff_t);
3557c478bd9Sstevel@tonic-gate extern void	anon_dup(struct anon_hdr *, ulong_t,
3567c478bd9Sstevel@tonic-gate 		    struct anon_hdr *, ulong_t, size_t);
3577c478bd9Sstevel@tonic-gate extern void	anon_dup_fill_holes(struct anon_hdr *, ulong_t,
3587c478bd9Sstevel@tonic-gate 		    struct anon_hdr *, ulong_t, size_t, uint_t, int);
3597c478bd9Sstevel@tonic-gate extern int	anon_fill_cow_holes(struct seg *, caddr_t, struct anon_hdr *,
3607c478bd9Sstevel@tonic-gate 		    ulong_t, struct vnode *, u_offset_t, size_t, uint_t,
3617c478bd9Sstevel@tonic-gate 		    uint_t, struct vpage [], struct cred *);
3627c478bd9Sstevel@tonic-gate extern void	anon_free(struct anon_hdr *, ulong_t, size_t);
3637c478bd9Sstevel@tonic-gate extern void	anon_free_pages(struct anon_hdr *, ulong_t, size_t, uint_t);
3647c478bd9Sstevel@tonic-gate extern void	anon_disclaim(struct anon_map *, ulong_t, size_t, int);
3657c478bd9Sstevel@tonic-gate extern int	anon_getpage(struct anon **, uint_t *, struct page **,
3667c478bd9Sstevel@tonic-gate 		    size_t, struct seg *, caddr_t, enum seg_rw, struct cred *);
3677c478bd9Sstevel@tonic-gate extern int	swap_getconpage(struct vnode *, u_offset_t, size_t,
36807b65a64Saguzovsk 		    uint_t *, page_t *[], size_t, page_t *, uint_t *,
3697c478bd9Sstevel@tonic-gate 		    spgcnt_t *, struct seg *, caddr_t,
3707c478bd9Sstevel@tonic-gate 		    enum seg_rw, struct cred *);
3717c478bd9Sstevel@tonic-gate extern int	anon_map_getpages(struct anon_map *, ulong_t,
3727c478bd9Sstevel@tonic-gate 		    uint_t, struct seg *, caddr_t, uint_t,
3737c478bd9Sstevel@tonic-gate 		    uint_t *, page_t *[], uint_t *,
374*2cb27123Saguzovsk 		    struct vpage [], enum seg_rw, int, int, int, struct cred *);
3757c478bd9Sstevel@tonic-gate extern int	anon_map_privatepages(struct anon_map *, ulong_t,
3767c478bd9Sstevel@tonic-gate 		    uint_t, struct seg *, caddr_t, uint_t,
377*2cb27123Saguzovsk 		    page_t *[], struct vpage [], int, int, struct cred *);
3787c478bd9Sstevel@tonic-gate extern struct	page *anon_private(struct anon **, struct seg *,
3797c478bd9Sstevel@tonic-gate 		    caddr_t, uint_t, struct page *,
3807c478bd9Sstevel@tonic-gate 		    int, struct cred *);
3817c478bd9Sstevel@tonic-gate extern struct	page *anon_zero(struct seg *, caddr_t,
3827c478bd9Sstevel@tonic-gate 		    struct anon **, struct cred *);
3837c478bd9Sstevel@tonic-gate extern int	anon_map_createpages(struct anon_map *, ulong_t,
3847c478bd9Sstevel@tonic-gate 		    size_t, struct page **,
3857c478bd9Sstevel@tonic-gate 		    struct seg *, caddr_t,
3867c478bd9Sstevel@tonic-gate 		    enum seg_rw, struct cred *);
3877c478bd9Sstevel@tonic-gate extern int	anon_map_demotepages(struct anon_map *, ulong_t,
3887c478bd9Sstevel@tonic-gate 		    struct seg *, caddr_t, uint_t,
3897c478bd9Sstevel@tonic-gate 		    struct vpage [], struct cred *);
39007b65a64Saguzovsk extern void	anon_shmap_free_pages(struct anon_map *, ulong_t, size_t);
391*2cb27123Saguzovsk extern int	anon_resvmem(size_t, boolean_t, zone_t *, int);
3920209230bSgjelinek extern void	anon_unresvmem(size_t, zone_t *);
393*2cb27123Saguzovsk extern struct	anon_map *anonmap_alloc(size_t, size_t, int);
3947c478bd9Sstevel@tonic-gate extern void	anonmap_free(struct anon_map *);
3957c478bd9Sstevel@tonic-gate extern void	anon_decref(struct anon *);
3967c478bd9Sstevel@tonic-gate extern int	non_anon(struct anon_hdr *, ulong_t, u_offset_t *, size_t *);
3977c478bd9Sstevel@tonic-gate extern pgcnt_t	anon_pages(struct anon_hdr *, ulong_t, pgcnt_t);
3987c478bd9Sstevel@tonic-gate extern int	anon_swap_adjust(pgcnt_t);
3997c478bd9Sstevel@tonic-gate extern void	anon_swap_restore(pgcnt_t);
4007c478bd9Sstevel@tonic-gate extern struct	anon_hdr *anon_create(pgcnt_t, int);
4017c478bd9Sstevel@tonic-gate extern void	anon_release(struct anon_hdr *, pgcnt_t);
4027c478bd9Sstevel@tonic-gate extern struct	anon *anon_get_ptr(struct anon_hdr *, ulong_t);
4037c478bd9Sstevel@tonic-gate extern ulong_t	*anon_get_slot(struct anon_hdr *, ulong_t);
4047c478bd9Sstevel@tonic-gate extern struct	anon *anon_get_next_ptr(struct anon_hdr *, ulong_t *);
4057c478bd9Sstevel@tonic-gate extern int	anon_set_ptr(struct anon_hdr *, ulong_t, struct anon *, int);
4067c478bd9Sstevel@tonic-gate extern int 	anon_copy_ptr(struct anon_hdr *, ulong_t,
4077c478bd9Sstevel@tonic-gate 		    struct anon_hdr *, ulong_t, pgcnt_t, int);
4087c478bd9Sstevel@tonic-gate extern pgcnt_t	anon_grow(struct anon_hdr *, ulong_t *, pgcnt_t, pgcnt_t, int);
4097c478bd9Sstevel@tonic-gate extern void	anon_array_enter(struct anon_map *, ulong_t,
4107c478bd9Sstevel@tonic-gate 			anon_sync_obj_t *);
41187015465Scwb extern int	anon_array_try_enter(struct anon_map *, ulong_t,
41287015465Scwb 			anon_sync_obj_t *);
4137c478bd9Sstevel@tonic-gate extern void	anon_array_exit(anon_sync_obj_t *);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate  * anon_resv checks to see if there is enough swap space to fulfill a
4177c478bd9Sstevel@tonic-gate  * request and if so, reserves the appropriate anonymous memory resources.
4187c478bd9Sstevel@tonic-gate  * anon_checkspace just checks to see if there is space to fulfill the request,
4197c478bd9Sstevel@tonic-gate  * without taking any resources.  Both return 1 if successful and 0 if not.
4200209230bSgjelinek  *
4210209230bSgjelinek  * Macros are provided as anon reservation is usually charged to the zone of
4220209230bSgjelinek  * the current process.  In some cases (such as anon reserved by tmpfs), a
4230209230bSgjelinek  * zone pointer is needed to charge the appropriate zone.
4247c478bd9Sstevel@tonic-gate  */
4250209230bSgjelinek #define	anon_unresv(size)		anon_unresvmem(size, curproc->p_zone)
4260209230bSgjelinek #define	anon_unresv_zone(size, zone)	anon_unresvmem(size, zone)
427*2cb27123Saguzovsk #define	anon_resv(size)			\
428*2cb27123Saguzovsk 	anon_resvmem((size), 1, curproc->p_zone, 1)
429*2cb27123Saguzovsk #define	anon_resv_zone(size, zone)	anon_resvmem((size), 1, zone, 1)
430*2cb27123Saguzovsk #define	anon_checkspace(size, zone)	anon_resvmem((size), 0, zone, 1)
431*2cb27123Saguzovsk #define	anon_try_resv_zone(size, zone)	anon_resvmem((size), 1, zone, 0)
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * Flags to anon_private
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate #define	STEAL_PAGE	0x1	/* page can be stolen */
4377c478bd9Sstevel@tonic-gate #define	LOCK_PAGE	0x2	/* page must be ``logically'' locked */
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate /*
4407c478bd9Sstevel@tonic-gate  * Flags to anon_disclaim
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate #define	ANON_PGLOOKUP_BLK	0x1	/* block on locked pages */
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate /*
4457c478bd9Sstevel@tonic-gate  * SEGKP ANON pages that are locked are assumed to be LWP stack pages
4467c478bd9Sstevel@tonic-gate  * and thus count towards the user pages locked count.
4477c478bd9Sstevel@tonic-gate  * This value is protected by the same lock as availrmem.
4487c478bd9Sstevel@tonic-gate  */
4497c478bd9Sstevel@tonic-gate extern pgcnt_t anon_segkp_pages_locked;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate extern int anon_debug;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate #ifdef ANON_DEBUG
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate #define	A_ANON	0x01
4567c478bd9Sstevel@tonic-gate #define	A_RESV	0x02
4577c478bd9Sstevel@tonic-gate #define	A_MRESV	0x04
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate /* vararg-like debugging macro. */
4607c478bd9Sstevel@tonic-gate #define	ANON_PRINT(f, printf_args) \
4617c478bd9Sstevel@tonic-gate 		if (anon_debug & f) \
4627c478bd9Sstevel@tonic-gate 			printf printf_args
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate #else	/* ANON_DEBUG */
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate #define	ANON_PRINT(f, printf_args)
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate #endif	/* ANON_DEBUG */
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate #endif
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate #endif	/* _VM_ANON_H */
477