xref: /onnv-gate/usr/src/uts/common/vm/vm_anon.c (revision 575)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * VM - anonymous pages.
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  * This layer sits immediately above the vm_swap layer.  It manages
460Sstevel@tonic-gate  * physical pages that have no permanent identity in the file system
470Sstevel@tonic-gate  * name space, using the services of the vm_swap layer to allocate
480Sstevel@tonic-gate  * backing storage for these pages.  Since these pages have no external
490Sstevel@tonic-gate  * identity, they are discarded when the last reference is removed.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * An important function of this layer is to manage low-level sharing
520Sstevel@tonic-gate  * of pages that are logically distinct but that happen to be
530Sstevel@tonic-gate  * physically identical (e.g., the corresponding pages of the processes
540Sstevel@tonic-gate  * resulting from a fork before one process or the other changes their
550Sstevel@tonic-gate  * contents).  This pseudo-sharing is present only as an optimization
560Sstevel@tonic-gate  * and is not to be confused with true sharing in which multiple
570Sstevel@tonic-gate  * address spaces deliberately contain references to the same object;
580Sstevel@tonic-gate  * such sharing is managed at a higher level.
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  * The key data structure here is the anon struct, which contains a
610Sstevel@tonic-gate  * reference count for its associated physical page and a hint about
620Sstevel@tonic-gate  * the identity of that page.  Anon structs typically live in arrays,
630Sstevel@tonic-gate  * with an instance's position in its array determining where the
640Sstevel@tonic-gate  * corresponding backing storage is allocated; however, the swap_xlate()
650Sstevel@tonic-gate  * routine abstracts away this representation information so that the
660Sstevel@tonic-gate  * rest of the anon layer need not know it.  (See the swap layer for
670Sstevel@tonic-gate  * more details on anon struct layout.)
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * In the future versions of the system, the association between an
700Sstevel@tonic-gate  * anon struct and its position on backing store will change so that
710Sstevel@tonic-gate  * we don't require backing store all anonymous pages in the system.
720Sstevel@tonic-gate  * This is important for consideration for large memory systems.
730Sstevel@tonic-gate  * We can also use this technique to delay binding physical locations
740Sstevel@tonic-gate  * to anonymous pages until pageout/swapout time where we can make
750Sstevel@tonic-gate  * smarter allocation decisions to improve anonymous klustering.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * Many of the routines defined here take a (struct anon **) argument,
780Sstevel@tonic-gate  * which allows the code at this level to manage anon pages directly,
790Sstevel@tonic-gate  * so that callers can regard anon structs as opaque objects and not be
800Sstevel@tonic-gate  * concerned with assigning or inspecting their contents.
810Sstevel@tonic-gate  *
820Sstevel@tonic-gate  * Clients of this layer refer to anon pages indirectly.  That is, they
830Sstevel@tonic-gate  * maintain arrays of pointers to anon structs rather than maintaining
840Sstevel@tonic-gate  * anon structs themselves.  The (struct anon **) arguments mentioned
850Sstevel@tonic-gate  * above are pointers to entries in these arrays.  It is these arrays
860Sstevel@tonic-gate  * that capture the mapping between offsets within a given segment and
870Sstevel@tonic-gate  * the corresponding anonymous backing storage address.
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate 
900Sstevel@tonic-gate #ifdef DEBUG
910Sstevel@tonic-gate #define	ANON_DEBUG
920Sstevel@tonic-gate #endif
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #include <sys/types.h>
950Sstevel@tonic-gate #include <sys/t_lock.h>
960Sstevel@tonic-gate #include <sys/param.h>
970Sstevel@tonic-gate #include <sys/systm.h>
980Sstevel@tonic-gate #include <sys/mman.h>
990Sstevel@tonic-gate #include <sys/cred.h>
1000Sstevel@tonic-gate #include <sys/thread.h>
1010Sstevel@tonic-gate #include <sys/vnode.h>
1020Sstevel@tonic-gate #include <sys/cpuvar.h>
1030Sstevel@tonic-gate #include <sys/swap.h>
1040Sstevel@tonic-gate #include <sys/cmn_err.h>
1050Sstevel@tonic-gate #include <sys/vtrace.h>
1060Sstevel@tonic-gate #include <sys/kmem.h>
1070Sstevel@tonic-gate #include <sys/sysmacros.h>
1080Sstevel@tonic-gate #include <sys/bitmap.h>
1090Sstevel@tonic-gate #include <sys/vmsystm.h>
1100Sstevel@tonic-gate #include <sys/debug.h>
1110Sstevel@tonic-gate #include <sys/tnf_probe.h>
1120Sstevel@tonic-gate #include <sys/lgrp.h>
1130Sstevel@tonic-gate #include <sys/policy.h>
1140Sstevel@tonic-gate #include <sys/condvar_impl.h>
1150Sstevel@tonic-gate #include <sys/mutex_impl.h>
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate #include <vm/as.h>
1180Sstevel@tonic-gate #include <vm/hat.h>
1190Sstevel@tonic-gate #include <vm/anon.h>
1200Sstevel@tonic-gate #include <vm/page.h>
1210Sstevel@tonic-gate #include <vm/vpage.h>
1220Sstevel@tonic-gate #include <vm/seg.h>
1230Sstevel@tonic-gate #include <vm/rm.h>
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #include <fs/fs_subr.h>
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate int anon_debug;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate kmutex_t	anoninfo_lock;
1300Sstevel@tonic-gate struct		k_anoninfo k_anoninfo;
1310Sstevel@tonic-gate ani_free_t	ani_free_pool[ANI_MAX_POOL];
1320Sstevel@tonic-gate pad_mutex_t	anon_array_lock[ANON_LOCKSIZE];
1330Sstevel@tonic-gate kcondvar_t	anon_array_cv[ANON_LOCKSIZE];
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate  * Global hash table for (vp, off) -> anon slot
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate extern	int swap_maxcontig;
1390Sstevel@tonic-gate size_t	anon_hash_size;
1400Sstevel@tonic-gate struct anon **anon_hash;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate static struct kmem_cache *anon_cache;
1430Sstevel@tonic-gate static struct kmem_cache *anonmap_cache;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate #ifdef VM_STATS
1460Sstevel@tonic-gate static struct anonvmstats_str {
1470Sstevel@tonic-gate 	ulong_t getpages[30];
1480Sstevel@tonic-gate 	ulong_t privatepages[10];
1490Sstevel@tonic-gate 	ulong_t demotepages[9];
1500Sstevel@tonic-gate 	ulong_t decrefpages[9];
1510Sstevel@tonic-gate 	ulong_t	dupfillholes[4];
1520Sstevel@tonic-gate 	ulong_t freepages[1];
1530Sstevel@tonic-gate } anonvmstats;
1540Sstevel@tonic-gate #endif /* VM_STATS */
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*ARGSUSED*/
1580Sstevel@tonic-gate static int
1590Sstevel@tonic-gate anonmap_cache_constructor(void *buf, void *cdrarg, int kmflags)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	struct anon_map *amp = buf;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	rw_init(&amp->a_rwlock, NULL, RW_DEFAULT, NULL);
1640Sstevel@tonic-gate 	return (0);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*ARGSUSED1*/
1680Sstevel@tonic-gate static void
1690Sstevel@tonic-gate anonmap_cache_destructor(void *buf, void *cdrarg)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	struct anon_map *amp = buf;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	rw_destroy(&amp->a_rwlock);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate kmutex_t	anonhash_lock[AH_LOCK_SIZE];
1770Sstevel@tonic-gate kmutex_t	anonpages_hash_lock[AH_LOCK_SIZE];
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate void
1800Sstevel@tonic-gate anon_init(void)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	int i;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	anon_hash_size = 1L << highbit(physmem / ANON_HASHAVELEN);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	for (i = 0; i < AH_LOCK_SIZE; i++) {
1870Sstevel@tonic-gate 		mutex_init(&anonhash_lock[i], NULL, MUTEX_DEFAULT, NULL);
1880Sstevel@tonic-gate 		mutex_init(&anonpages_hash_lock[i], NULL, MUTEX_DEFAULT, NULL);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	for (i = 0; i < ANON_LOCKSIZE; i++) {
1920Sstevel@tonic-gate 		mutex_init(&anon_array_lock[i].pad_mutex, NULL,
1930Sstevel@tonic-gate 			MUTEX_DEFAULT, NULL);
1940Sstevel@tonic-gate 		cv_init(&anon_array_cv[i], NULL, CV_DEFAULT, NULL);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	anon_hash = (struct anon **)
1980Sstevel@tonic-gate 		kmem_zalloc(sizeof (struct anon *) * anon_hash_size, KM_SLEEP);
1990Sstevel@tonic-gate 	anon_cache = kmem_cache_create("anon_cache", sizeof (struct anon),
2000Sstevel@tonic-gate 		AN_CACHE_ALIGN, NULL, NULL, NULL, NULL, NULL, 0);
2010Sstevel@tonic-gate 	anonmap_cache = kmem_cache_create("anonmap_cache",
2020Sstevel@tonic-gate 		sizeof (struct anon_map), 0,
2030Sstevel@tonic-gate 		anonmap_cache_constructor, anonmap_cache_destructor, NULL,
2040Sstevel@tonic-gate 		NULL, NULL, 0);
2050Sstevel@tonic-gate 	swap_maxcontig = (1024 * 1024) >> PAGESHIFT;	/* 1MB of pages */
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate  * Global anon slot hash table manipulation.
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate static void
2130Sstevel@tonic-gate anon_addhash(struct anon *ap)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	int index;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)]));
2180Sstevel@tonic-gate 	index = ANON_HASH(ap->an_vp, ap->an_off);
2190Sstevel@tonic-gate 	ap->an_hash = anon_hash[index];
2200Sstevel@tonic-gate 	anon_hash[index] = ap;
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate static void
2240Sstevel@tonic-gate anon_rmhash(struct anon *ap)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	struct anon **app;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)]));
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	for (app = &anon_hash[ANON_HASH(ap->an_vp, ap->an_off)];
2310Sstevel@tonic-gate 	    *app; app = &((*app)->an_hash)) {
2320Sstevel@tonic-gate 		if (*app == ap) {
2330Sstevel@tonic-gate 			*app = ap->an_hash;
2340Sstevel@tonic-gate 			break;
2350Sstevel@tonic-gate 		}
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate  * The anon array interfaces. Functions allocating,
2410Sstevel@tonic-gate  * freeing array of pointers, and returning/setting
2420Sstevel@tonic-gate  * entries in the array of pointers for a given offset.
2430Sstevel@tonic-gate  *
2440Sstevel@tonic-gate  * Create the list of pointers
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate struct anon_hdr *
2470Sstevel@tonic-gate anon_create(pgcnt_t npages, int flags)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	struct anon_hdr *ahp;
2500Sstevel@tonic-gate 	ulong_t nchunks;
2510Sstevel@tonic-gate 	int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if ((ahp = kmem_zalloc(sizeof (struct anon_hdr), kmemflags)) == NULL) {
2540Sstevel@tonic-gate 		return (NULL);
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	mutex_init(&ahp->serial_lock, NULL, MUTEX_DEFAULT, NULL);
2580Sstevel@tonic-gate 	/*
2590Sstevel@tonic-gate 	 * Single level case.
2600Sstevel@tonic-gate 	 */
2610Sstevel@tonic-gate 	ahp->size = npages;
2620Sstevel@tonic-gate 	if (npages <= ANON_CHUNK_SIZE || (flags & ANON_ALLOC_FORCE)) {
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		if (flags & ANON_ALLOC_FORCE)
2650Sstevel@tonic-gate 			ahp->flags |= ANON_ALLOC_FORCE;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 		ahp->array_chunk = kmem_zalloc(
2680Sstevel@tonic-gate 		    ahp->size * sizeof (struct anon *), kmemflags);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 		if (ahp->array_chunk == NULL) {
2710Sstevel@tonic-gate 			kmem_free(ahp, sizeof (struct anon_hdr));
2720Sstevel@tonic-gate 			return (NULL);
2730Sstevel@tonic-gate 		}
2740Sstevel@tonic-gate 	} else {
2750Sstevel@tonic-gate 		/*
2760Sstevel@tonic-gate 		 * 2 Level case.
2770Sstevel@tonic-gate 		 */
2780Sstevel@tonic-gate 		nchunks = (ahp->size + ANON_CHUNK_OFF) >> ANON_CHUNK_SHIFT;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 		ahp->array_chunk = kmem_zalloc(nchunks * sizeof (ulong_t *),
2810Sstevel@tonic-gate 		    kmemflags);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		if (ahp->array_chunk == NULL) {
2840Sstevel@tonic-gate 			kmem_free(ahp, sizeof (struct anon_hdr));
2850Sstevel@tonic-gate 			return (NULL);
2860Sstevel@tonic-gate 		}
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 	return (ahp);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate  * Free the array of pointers
2930Sstevel@tonic-gate  */
2940Sstevel@tonic-gate void
2950Sstevel@tonic-gate anon_release(struct anon_hdr *ahp, pgcnt_t npages)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	ulong_t i;
2980Sstevel@tonic-gate 	void **ppp;
2990Sstevel@tonic-gate 	ulong_t nchunks;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	ASSERT(npages == ahp->size);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/*
3040Sstevel@tonic-gate 	 * Single level case.
3050Sstevel@tonic-gate 	 */
3060Sstevel@tonic-gate 	if (npages <= ANON_CHUNK_SIZE || (ahp->flags & ANON_ALLOC_FORCE)) {
3070Sstevel@tonic-gate 		kmem_free(ahp->array_chunk, ahp->size * sizeof (struct anon *));
3080Sstevel@tonic-gate 	} else {
3090Sstevel@tonic-gate 		/*
3100Sstevel@tonic-gate 		 * 2 level case.
3110Sstevel@tonic-gate 		 */
3120Sstevel@tonic-gate 		nchunks = (ahp->size + ANON_CHUNK_OFF) >> ANON_CHUNK_SHIFT;
3130Sstevel@tonic-gate 		for (i = 0; i < nchunks; i++) {
3140Sstevel@tonic-gate 			ppp = &ahp->array_chunk[i];
3150Sstevel@tonic-gate 			if (*ppp != NULL)
3160Sstevel@tonic-gate 				kmem_free(*ppp, PAGESIZE);
3170Sstevel@tonic-gate 		}
3180Sstevel@tonic-gate 		kmem_free(ahp->array_chunk, nchunks * sizeof (ulong_t *));
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 	mutex_destroy(&ahp->serial_lock);
3210Sstevel@tonic-gate 	kmem_free(ahp, sizeof (struct anon_hdr));
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate  * Return the pointer from the list for a
3260Sstevel@tonic-gate  * specified anon index.
3270Sstevel@tonic-gate  */
3280Sstevel@tonic-gate struct anon *
3290Sstevel@tonic-gate anon_get_ptr(struct anon_hdr *ahp, ulong_t an_idx)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	struct anon **app;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	ASSERT(an_idx < ahp->size);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/*
3360Sstevel@tonic-gate 	 * Single level case.
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	if ((ahp->size <= ANON_CHUNK_SIZE) || (ahp->flags & ANON_ALLOC_FORCE)) {
3390Sstevel@tonic-gate 		return ((struct anon *)
3400Sstevel@tonic-gate 			((uintptr_t)ahp->array_chunk[an_idx] & ANON_PTRMASK));
3410Sstevel@tonic-gate 	} else {
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		/*
3440Sstevel@tonic-gate 		 * 2 level case.
3450Sstevel@tonic-gate 		 */
3460Sstevel@tonic-gate 		app = ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
3470Sstevel@tonic-gate 		if (app) {
3480Sstevel@tonic-gate 			return ((struct anon *)
3490Sstevel@tonic-gate 				((uintptr_t)app[an_idx & ANON_CHUNK_OFF] &
3500Sstevel@tonic-gate 					ANON_PTRMASK));
3510Sstevel@tonic-gate 		} else {
3520Sstevel@tonic-gate 			return (NULL);
3530Sstevel@tonic-gate 		}
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * Return the anon pointer for the first valid entry in the anon list,
3590Sstevel@tonic-gate  * starting from the given index.
3600Sstevel@tonic-gate  */
3610Sstevel@tonic-gate struct anon *
3620Sstevel@tonic-gate anon_get_next_ptr(struct anon_hdr *ahp, ulong_t *index)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	struct anon *ap;
3650Sstevel@tonic-gate 	struct anon **app;
3660Sstevel@tonic-gate 	ulong_t chunkoff;
3670Sstevel@tonic-gate 	ulong_t i;
3680Sstevel@tonic-gate 	ulong_t j;
3690Sstevel@tonic-gate 	pgcnt_t size;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	i = *index;
3720Sstevel@tonic-gate 	size = ahp->size;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	ASSERT(i < size);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	if ((size <= ANON_CHUNK_SIZE) || (ahp->flags & ANON_ALLOC_FORCE)) {
3770Sstevel@tonic-gate 		/*
3780Sstevel@tonic-gate 		 * 1 level case
3790Sstevel@tonic-gate 		 */
3800Sstevel@tonic-gate 		while (i < size) {
3810Sstevel@tonic-gate 			ap = (struct anon *)
3820Sstevel@tonic-gate 				((uintptr_t)ahp->array_chunk[i] & ANON_PTRMASK);
3830Sstevel@tonic-gate 			if (ap) {
3840Sstevel@tonic-gate 				*index = i;
3850Sstevel@tonic-gate 				return (ap);
3860Sstevel@tonic-gate 			}
3870Sstevel@tonic-gate 			i++;
3880Sstevel@tonic-gate 		}
3890Sstevel@tonic-gate 	} else {
3900Sstevel@tonic-gate 		/*
3910Sstevel@tonic-gate 		 * 2 level case
3920Sstevel@tonic-gate 		 */
3930Sstevel@tonic-gate 		chunkoff = i & ANON_CHUNK_OFF;
3940Sstevel@tonic-gate 		while (i < size) {
3950Sstevel@tonic-gate 			app = ahp->array_chunk[i >> ANON_CHUNK_SHIFT];
3960Sstevel@tonic-gate 			if (app)
3970Sstevel@tonic-gate 				for (j = chunkoff; j < ANON_CHUNK_SIZE; j++) {
3980Sstevel@tonic-gate 					ap = (struct anon *)
3990Sstevel@tonic-gate 						((uintptr_t)app[j] &
4000Sstevel@tonic-gate 							ANON_PTRMASK);
4010Sstevel@tonic-gate 					if (ap) {
4020Sstevel@tonic-gate 						*index = i + (j - chunkoff);
4030Sstevel@tonic-gate 						return (ap);
4040Sstevel@tonic-gate 					}
4050Sstevel@tonic-gate 				}
4060Sstevel@tonic-gate 			chunkoff = 0;
4070Sstevel@tonic-gate 			i = (i + ANON_CHUNK_SIZE) & ~ANON_CHUNK_OFF;
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 	*index = size;
4110Sstevel@tonic-gate 	return (NULL);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate  * Set list entry with a given pointer for a specified offset
4160Sstevel@tonic-gate  */
4170Sstevel@tonic-gate int
4180Sstevel@tonic-gate anon_set_ptr(struct anon_hdr *ahp, ulong_t an_idx, struct anon *ap, int flags)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate 	void		**ppp;
4210Sstevel@tonic-gate 	struct anon	**app;
4220Sstevel@tonic-gate 	int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
4230Sstevel@tonic-gate 	uintptr_t	*ap_addr;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	ASSERT(an_idx < ahp->size);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	/*
4280Sstevel@tonic-gate 	 * Single level case.
4290Sstevel@tonic-gate 	 */
4300Sstevel@tonic-gate 	if (ahp->size <= ANON_CHUNK_SIZE || (ahp->flags & ANON_ALLOC_FORCE)) {
4310Sstevel@tonic-gate 		ap_addr = (uintptr_t *)&ahp->array_chunk[an_idx];
4320Sstevel@tonic-gate 	} else {
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 		/*
4350Sstevel@tonic-gate 		 * 2 level case.
4360Sstevel@tonic-gate 		 */
4370Sstevel@tonic-gate 		ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 		ASSERT(ppp != NULL);
4400Sstevel@tonic-gate 		if (*ppp == NULL) {
4410Sstevel@tonic-gate 			mutex_enter(&ahp->serial_lock);
4420Sstevel@tonic-gate 			ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
4430Sstevel@tonic-gate 			if (*ppp == NULL) {
4440Sstevel@tonic-gate 				*ppp = kmem_zalloc(PAGESIZE, kmemflags);
4450Sstevel@tonic-gate 				if (*ppp == NULL) {
4460Sstevel@tonic-gate 					mutex_exit(&ahp->serial_lock);
4470Sstevel@tonic-gate 					return (ENOMEM);
4480Sstevel@tonic-gate 				}
4490Sstevel@tonic-gate 			}
4500Sstevel@tonic-gate 			mutex_exit(&ahp->serial_lock);
4510Sstevel@tonic-gate 		}
4520Sstevel@tonic-gate 		app = *ppp;
4530Sstevel@tonic-gate 		ap_addr = (uintptr_t *)&app[an_idx & ANON_CHUNK_OFF];
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 	*ap_addr = (*ap_addr & ~ANON_PTRMASK) | (uintptr_t)ap;
4560Sstevel@tonic-gate 	return (0);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate /*
4600Sstevel@tonic-gate  * Copy anon array into a given new anon array
4610Sstevel@tonic-gate  */
4620Sstevel@tonic-gate int
4630Sstevel@tonic-gate anon_copy_ptr(struct anon_hdr *sahp, ulong_t s_idx,
4640Sstevel@tonic-gate 	struct anon_hdr *dahp, ulong_t d_idx,
4650Sstevel@tonic-gate 	pgcnt_t npages, int flags)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	void **sapp, **dapp;
4680Sstevel@tonic-gate 	void *ap;
4690Sstevel@tonic-gate 	int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	ASSERT((s_idx < sahp->size) && (d_idx < dahp->size));
4720Sstevel@tonic-gate 	ASSERT((npages <= sahp->size) && (npages <= dahp->size));
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	/*
4750Sstevel@tonic-gate 	 * Both arrays are 1 level.
4760Sstevel@tonic-gate 	 */
4770Sstevel@tonic-gate 	if (((sahp->size <= ANON_CHUNK_SIZE) &&
4780Sstevel@tonic-gate 	    (dahp->size <= ANON_CHUNK_SIZE)) ||
4790Sstevel@tonic-gate 	    ((sahp->flags & ANON_ALLOC_FORCE) &&
4800Sstevel@tonic-gate 	    (dahp->flags & ANON_ALLOC_FORCE))) {
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 		bcopy(&sahp->array_chunk[s_idx], &dahp->array_chunk[d_idx],
4830Sstevel@tonic-gate 		    npages * sizeof (struct anon *));
4840Sstevel@tonic-gate 		return (0);
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/*
4880Sstevel@tonic-gate 	 * Both arrays are 2 levels.
4890Sstevel@tonic-gate 	 */
4900Sstevel@tonic-gate 	if (sahp->size > ANON_CHUNK_SIZE &&
4910Sstevel@tonic-gate 	    dahp->size > ANON_CHUNK_SIZE &&
4920Sstevel@tonic-gate 	    ((sahp->flags & ANON_ALLOC_FORCE) == 0) &&
4930Sstevel@tonic-gate 	    ((dahp->flags & ANON_ALLOC_FORCE) == 0)) {
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 		ulong_t sapidx, dapidx;
4960Sstevel@tonic-gate 		ulong_t *sap, *dap;
4970Sstevel@tonic-gate 		ulong_t chknp;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 		while (npages != 0) {
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 			sapidx = s_idx & ANON_CHUNK_OFF;
5020Sstevel@tonic-gate 			dapidx = d_idx & ANON_CHUNK_OFF;
5030Sstevel@tonic-gate 			chknp = ANON_CHUNK_SIZE - MAX(sapidx, dapidx);
5040Sstevel@tonic-gate 			if (chknp > npages)
5050Sstevel@tonic-gate 				chknp = npages;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 			sapp = &sahp->array_chunk[s_idx >> ANON_CHUNK_SHIFT];
5080Sstevel@tonic-gate 			if ((sap = *sapp) != NULL) {
5090Sstevel@tonic-gate 				dapp = &dahp->array_chunk[d_idx
5100Sstevel@tonic-gate 							>> ANON_CHUNK_SHIFT];
5110Sstevel@tonic-gate 				if ((dap = *dapp) == NULL) {
5120Sstevel@tonic-gate 					*dapp = kmem_zalloc(PAGESIZE,
5130Sstevel@tonic-gate 					    kmemflags);
5140Sstevel@tonic-gate 					if ((dap = *dapp) == NULL)
5150Sstevel@tonic-gate 						return (ENOMEM);
5160Sstevel@tonic-gate 				}
5170Sstevel@tonic-gate 				bcopy((sap + sapidx), (dap + dapidx),
5180Sstevel@tonic-gate 				    chknp << ANON_PTRSHIFT);
5190Sstevel@tonic-gate 			}
5200Sstevel@tonic-gate 			s_idx += chknp;
5210Sstevel@tonic-gate 			d_idx += chknp;
5220Sstevel@tonic-gate 			npages -= chknp;
5230Sstevel@tonic-gate 		}
5240Sstevel@tonic-gate 		return (0);
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	/*
5280Sstevel@tonic-gate 	 * At least one of the arrays is 2 level.
5290Sstevel@tonic-gate 	 */
5300Sstevel@tonic-gate 	while (npages--) {
5310Sstevel@tonic-gate 		if ((ap = anon_get_ptr(sahp, s_idx)) != NULL) {
5320Sstevel@tonic-gate 			ASSERT(!ANON_ISBUSY(anon_get_slot(sahp, s_idx)));
5330Sstevel@tonic-gate 			if (anon_set_ptr(dahp, d_idx, ap, flags) == ENOMEM)
5340Sstevel@tonic-gate 					return (ENOMEM);
5350Sstevel@tonic-gate 		}
5360Sstevel@tonic-gate 		s_idx++;
5370Sstevel@tonic-gate 		d_idx++;
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 	return (0);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate /*
5440Sstevel@tonic-gate  * ANON_INITBUF is a convenience macro for anon_grow() below. It
5450Sstevel@tonic-gate  * takes a buffer dst, which is at least as large as buffer src. It
5460Sstevel@tonic-gate  * does a bcopy from src into dst, and then bzeros the extra bytes
5470Sstevel@tonic-gate  * of dst. If tail is set, the data in src is tail aligned within
5480Sstevel@tonic-gate  * dst instead of head aligned.
5490Sstevel@tonic-gate  */
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate #define	ANON_INITBUF(src, srclen, dst, dstsize, tail)			      \
5520Sstevel@tonic-gate 	if (tail) {							      \
5530Sstevel@tonic-gate 		bzero((dst), (dstsize) - (srclen));			      \
5540Sstevel@tonic-gate 		bcopy((src), (char *)(dst) + (dstsize) - (srclen), (srclen)); \
5550Sstevel@tonic-gate 	} else {							      \
5560Sstevel@tonic-gate 		bcopy((src), (dst), (srclen));				      \
5570Sstevel@tonic-gate 		bzero((char *)(dst) + (srclen), (dstsize) - (srclen));	      \
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate #define	ANON_1_LEVEL_INC	(ANON_CHUNK_SIZE / 8)
5610Sstevel@tonic-gate #define	ANON_2_LEVEL_INC	(ANON_1_LEVEL_INC * ANON_CHUNK_SIZE)
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate /*
5640Sstevel@tonic-gate  * anon_grow() is used to efficiently extend an existing anon array.
5650Sstevel@tonic-gate  * startidx_p points to the index into the anon array of the first page
566*575Sstans  * that is in use. oldseg_pgs is the number of pages in use, starting at
5670Sstevel@tonic-gate  * *startidx_p. newpages is the number of additional pages desired.
5680Sstevel@tonic-gate  *
5690Sstevel@tonic-gate  * If startidx_p == NULL, startidx is taken to be 0 and cannot be changed.
5700Sstevel@tonic-gate  *
5710Sstevel@tonic-gate  * The growth is done by creating a new top level of the anon array,
5720Sstevel@tonic-gate  * and (if the array is 2-level) reusing the existing second level arrays.
5730Sstevel@tonic-gate  *
5740Sstevel@tonic-gate  * flags can be used to specify ANON_NOSLEEP and ANON_GROWDOWN.
5750Sstevel@tonic-gate  *
5760Sstevel@tonic-gate  * Returns the new number of pages in the anon array.
5770Sstevel@tonic-gate  */
5780Sstevel@tonic-gate pgcnt_t
579*575Sstans anon_grow(struct anon_hdr *ahp, ulong_t *startidx_p, pgcnt_t oldseg_pgs,
580*575Sstans     pgcnt_t newseg_pgs, int flags)
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate 	ulong_t startidx = startidx_p ? *startidx_p : 0;
583*575Sstans 	pgcnt_t oldamp_pgs = ahp->size, newamp_pgs;
5840Sstevel@tonic-gate 	pgcnt_t oelems, nelems, totpages;
5850Sstevel@tonic-gate 	void **level1;
5860Sstevel@tonic-gate 	int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
5870Sstevel@tonic-gate 	int growdown = (flags & ANON_GROWDOWN);
5880Sstevel@tonic-gate 	size_t newarrsz, oldarrsz;
5890Sstevel@tonic-gate 	void *level2;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	ASSERT(!(startidx_p == NULL && growdown));
592*575Sstans 	ASSERT(startidx + oldseg_pgs <= ahp->size);
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	/*
5950Sstevel@tonic-gate 	 * Determine the total number of pages needed in the new
5960Sstevel@tonic-gate 	 * anon array. If growing down, totpages is all pages from
597*575Sstans 	 * startidx through the end of the array, plus <newseg_pgs>
5980Sstevel@tonic-gate 	 * pages. If growing up, keep all pages from page 0 through
599*575Sstans 	 * the last page currently in use, plus <newseg_pgs> pages.
6000Sstevel@tonic-gate 	 */
6010Sstevel@tonic-gate 	if (growdown)
602*575Sstans 		totpages = oldamp_pgs - startidx + newseg_pgs;
6030Sstevel@tonic-gate 	else
604*575Sstans 		totpages = startidx + oldseg_pgs + newseg_pgs;
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	/* If the array is already large enough, just return. */
6070Sstevel@tonic-gate 
608*575Sstans 	if (oldamp_pgs >= totpages) {
609*575Sstans 		if (growdown)
610*575Sstans 			*startidx_p = oldamp_pgs - totpages;
611*575Sstans 		return (oldamp_pgs);
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	/*
615*575Sstans 	 * oldamp_pgs/newamp_pgs are the total numbers of pages represented
616*575Sstans 	 * by the corresponding arrays.
617*575Sstans 	 * oelems/nelems are the number of pointers in the top level arrays
618*575Sstans 	 * which may be either level 1 or level 2.
6190Sstevel@tonic-gate 	 * Will the new anon array be one level or two levels?
6200Sstevel@tonic-gate 	 */
6210Sstevel@tonic-gate 	if (totpages <= ANON_CHUNK_SIZE || (ahp->flags & ANON_ALLOC_FORCE)) {
622*575Sstans 		newamp_pgs = P2ROUNDUP(totpages, ANON_1_LEVEL_INC);
623*575Sstans 		oelems = oldamp_pgs;
624*575Sstans 		nelems = newamp_pgs;
6250Sstevel@tonic-gate 	} else {
626*575Sstans 		newamp_pgs = P2ROUNDUP(totpages, ANON_2_LEVEL_INC);
627*575Sstans 		oelems = (oldamp_pgs + ANON_CHUNK_OFF) >> ANON_CHUNK_SHIFT;
628*575Sstans 		nelems = newamp_pgs >> ANON_CHUNK_SHIFT;
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	newarrsz = nelems * sizeof (void *);
6320Sstevel@tonic-gate 	level1 = kmem_alloc(newarrsz, kmemflags);
6330Sstevel@tonic-gate 	if (level1 == NULL)
6340Sstevel@tonic-gate 		return (0);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	/* Are we converting from a one level to a two level anon array? */
6370Sstevel@tonic-gate 
638*575Sstans 	if (newamp_pgs > ANON_CHUNK_SIZE && oldamp_pgs <= ANON_CHUNK_SIZE &&
6390Sstevel@tonic-gate 	    !(ahp->flags & ANON_ALLOC_FORCE)) {
640*575Sstans 
6410Sstevel@tonic-gate 		/*
6420Sstevel@tonic-gate 		 * Yes, we're converting to a two level. Reuse old level 1
6430Sstevel@tonic-gate 		 * as new level 2 if it is exactly PAGESIZE. Otherwise
6440Sstevel@tonic-gate 		 * alloc a new level 2 and copy the old level 1 data into it.
6450Sstevel@tonic-gate 		 */
646*575Sstans 		if (oldamp_pgs == ANON_CHUNK_SIZE) {
6470Sstevel@tonic-gate 			level2 = (void *)ahp->array_chunk;
6480Sstevel@tonic-gate 		} else {
6490Sstevel@tonic-gate 			level2 = kmem_alloc(PAGESIZE, kmemflags);
6500Sstevel@tonic-gate 			if (level2 == NULL) {
6510Sstevel@tonic-gate 				kmem_free(level1, newarrsz);
6520Sstevel@tonic-gate 				return (0);
6530Sstevel@tonic-gate 			}
654*575Sstans 			oldarrsz = oldamp_pgs * sizeof (void *);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 			ANON_INITBUF(ahp->array_chunk, oldarrsz,
6570Sstevel@tonic-gate 			    level2, PAGESIZE, growdown);
6580Sstevel@tonic-gate 			kmem_free(ahp->array_chunk, oldarrsz);
6590Sstevel@tonic-gate 		}
6600Sstevel@tonic-gate 		bzero(level1, newarrsz);
6610Sstevel@tonic-gate 		if (growdown)
6620Sstevel@tonic-gate 			level1[nelems - 1] = level2;
6630Sstevel@tonic-gate 		else
6640Sstevel@tonic-gate 			level1[0] = level2;
6650Sstevel@tonic-gate 	} else {
6660Sstevel@tonic-gate 		oldarrsz = oelems * sizeof (void *);
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		ANON_INITBUF(ahp->array_chunk, oldarrsz,
6690Sstevel@tonic-gate 		    level1, newarrsz, growdown);
6700Sstevel@tonic-gate 		kmem_free(ahp->array_chunk, oldarrsz);
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	ahp->array_chunk = level1;
674*575Sstans 	ahp->size = newamp_pgs;
675*575Sstans 	if (growdown) {
676*575Sstans 		*startidx_p = newamp_pgs - totpages;
677*575Sstans 		if (oldamp_pgs > ANON_CHUNK_SIZE)
678*575Sstans 			*startidx_p -= P2NPHASE(oldseg_pgs, ANON_CHUNK_SIZE);
679*575Sstans 	}
680*575Sstans 	return (newamp_pgs);
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate 
683*575Sstans 
6840Sstevel@tonic-gate /*
6850Sstevel@tonic-gate  * Called from clock handler to sync ani_free value.
6860Sstevel@tonic-gate  */
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate void
6890Sstevel@tonic-gate set_anoninfo(void)
6900Sstevel@tonic-gate {
6910Sstevel@tonic-gate 	int	ix;
6920Sstevel@tonic-gate 	pgcnt_t	total = 0;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	for (ix = 0; ix < ANI_MAX_POOL; ix++) {
6950Sstevel@tonic-gate 		total += ani_free_pool[ix].ani_count;
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 	k_anoninfo.ani_free = total;
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate /*
7010Sstevel@tonic-gate  * Reserve anon space.
7020Sstevel@tonic-gate  *
7030Sstevel@tonic-gate  * It's no longer simply a matter of incrementing ani_resv to
7040Sstevel@tonic-gate  * reserve swap space, we need to check memory-based as well
7050Sstevel@tonic-gate  * as disk-backed (physical) swap.  The following algorithm
7060Sstevel@tonic-gate  * is used:
7070Sstevel@tonic-gate  * 	Check the space on physical swap
7080Sstevel@tonic-gate  * 		i.e. amount needed < ani_max - ani_phys_resv
7090Sstevel@tonic-gate  * 	If we are swapping on swapfs check
7100Sstevel@tonic-gate  *		amount needed < (availrmem - swapfs_minfree)
7110Sstevel@tonic-gate  * Since the algorithm to check for the quantity of swap space is
7120Sstevel@tonic-gate  * almost the same as that for reserving it, we'll just use anon_resvmem
7130Sstevel@tonic-gate  * with a flag to decrement availrmem.
7140Sstevel@tonic-gate  *
7150Sstevel@tonic-gate  * Return non-zero on success.
7160Sstevel@tonic-gate  */
7170Sstevel@tonic-gate int
7180Sstevel@tonic-gate anon_resvmem(size_t size, uint_t takemem)
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate 	pgcnt_t npages = btopr(size);
7210Sstevel@tonic-gate 	pgcnt_t mswap_pages = 0;
7220Sstevel@tonic-gate 	pgcnt_t pswap_pages = 0;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	mutex_enter(&anoninfo_lock);
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	/*
7270Sstevel@tonic-gate 	 * pswap_pages is the number of pages we can take from
7280Sstevel@tonic-gate 	 * physical (i.e. disk-backed) swap.
7290Sstevel@tonic-gate 	 */
7300Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
7310Sstevel@tonic-gate 	pswap_pages = k_anoninfo.ani_max - k_anoninfo.ani_phys_resv;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	ANON_PRINT(A_RESV,
7340Sstevel@tonic-gate 	    ("anon_resvmem: npages %lu takemem %u pswap %lu caller %p\n",
7350Sstevel@tonic-gate 	    npages, takemem, pswap_pages, (void *)caller()));
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	if (npages <= pswap_pages) {
7380Sstevel@tonic-gate 		/*
7390Sstevel@tonic-gate 		 * we have enough space on a physical swap
7400Sstevel@tonic-gate 		 */
7410Sstevel@tonic-gate 		if (takemem)
7420Sstevel@tonic-gate 			k_anoninfo.ani_phys_resv += npages;
7430Sstevel@tonic-gate 		mutex_exit(&anoninfo_lock);
7440Sstevel@tonic-gate 		return (1);
7450Sstevel@tonic-gate 	} else if (pswap_pages != 0) {
7460Sstevel@tonic-gate 		/*
7470Sstevel@tonic-gate 		 * we have some space on a physical swap
7480Sstevel@tonic-gate 		 */
7490Sstevel@tonic-gate 		if (takemem) {
7500Sstevel@tonic-gate 			/*
7510Sstevel@tonic-gate 			 * use up remainder of phys swap
7520Sstevel@tonic-gate 			 */
7530Sstevel@tonic-gate 			k_anoninfo.ani_phys_resv += pswap_pages;
7540Sstevel@tonic-gate 			ASSERT(k_anoninfo.ani_phys_resv == k_anoninfo.ani_max);
7550Sstevel@tonic-gate 		}
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 	/*
7580Sstevel@tonic-gate 	 * since (npages > pswap_pages) we need mem swap
7590Sstevel@tonic-gate 	 * mswap_pages is the number of pages needed from availrmem
7600Sstevel@tonic-gate 	 */
7610Sstevel@tonic-gate 	ASSERT(npages > pswap_pages);
7620Sstevel@tonic-gate 	mswap_pages = npages - pswap_pages;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	ANON_PRINT(A_RESV, ("anon_resvmem: need %ld pages from memory\n",
7650Sstevel@tonic-gate 	    mswap_pages));
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	/*
7680Sstevel@tonic-gate 	 * priv processes can reserve memory as swap as long as availrmem
7690Sstevel@tonic-gate 	 * remains greater than swapfs_minfree; in the case of non-priv
7700Sstevel@tonic-gate 	 * processes, memory can be reserved as swap only if availrmem
7710Sstevel@tonic-gate 	 * doesn't fall below (swapfs_minfree + swapfs_reserve). Thus,
7720Sstevel@tonic-gate 	 * swapfs_reserve amount of memswap is not available to non-priv
7730Sstevel@tonic-gate 	 * processes. This protects daemons such as automounter dying
7740Sstevel@tonic-gate 	 * as a result of application processes eating away almost entire
7750Sstevel@tonic-gate 	 * membased swap. This safeguard becomes useless if apps are run
7760Sstevel@tonic-gate 	 * with root access.
7770Sstevel@tonic-gate 	 *
7780Sstevel@tonic-gate 	 * swapfs_reserve is minimum of 4Mb or 1/16 of physmem.
7790Sstevel@tonic-gate 	 *
7800Sstevel@tonic-gate 	 */
7810Sstevel@tonic-gate 	mutex_enter(&freemem_lock);
7820Sstevel@tonic-gate 	if (availrmem > (swapfs_minfree + swapfs_reserve + mswap_pages) ||
7830Sstevel@tonic-gate 		(availrmem > (swapfs_minfree + mswap_pages) &&
7840Sstevel@tonic-gate 		secpolicy_resource(CRED()) == 0)) {
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 		if (takemem) {
7870Sstevel@tonic-gate 			/*
7880Sstevel@tonic-gate 			 * Take the memory from the rest of the system.
7890Sstevel@tonic-gate 			 */
7900Sstevel@tonic-gate 			availrmem -= mswap_pages;
7910Sstevel@tonic-gate 			mutex_exit(&freemem_lock);
7920Sstevel@tonic-gate 			k_anoninfo.ani_mem_resv += mswap_pages;
7930Sstevel@tonic-gate 			ANI_ADD(mswap_pages);
7940Sstevel@tonic-gate 			ANON_PRINT((A_RESV | A_MRESV),
7950Sstevel@tonic-gate 				("anon_resvmem: took %ld pages of availrmem\n",
7960Sstevel@tonic-gate 				mswap_pages));
7970Sstevel@tonic-gate 		} else {
7980Sstevel@tonic-gate 			mutex_exit(&freemem_lock);
7990Sstevel@tonic-gate 		}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 		ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
8020Sstevel@tonic-gate 		mutex_exit(&anoninfo_lock);
8030Sstevel@tonic-gate 		return (1);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	} else {
8060Sstevel@tonic-gate 		/*
8070Sstevel@tonic-gate 		 * Fail if not enough memory
8080Sstevel@tonic-gate 		 */
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 		if (takemem) {
8110Sstevel@tonic-gate 			k_anoninfo.ani_phys_resv -= pswap_pages;
8120Sstevel@tonic-gate 		}
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
8150Sstevel@tonic-gate 		mutex_exit(&anoninfo_lock);
8160Sstevel@tonic-gate 		ANON_PRINT(A_RESV,
8170Sstevel@tonic-gate 			("anon_resvmem: not enough space from swapfs\n"));
8180Sstevel@tonic-gate 		return (0);
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate }
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate /*
8240Sstevel@tonic-gate  * Give back an anon reservation.
8250Sstevel@tonic-gate  */
8260Sstevel@tonic-gate void
8270Sstevel@tonic-gate anon_unresv(size_t size)
8280Sstevel@tonic-gate {
8290Sstevel@tonic-gate 	pgcnt_t npages = btopr(size);
8300Sstevel@tonic-gate 	spgcnt_t mem_free_pages = 0;
8310Sstevel@tonic-gate 	pgcnt_t phys_free_slots;
8320Sstevel@tonic-gate #ifdef	ANON_DEBUG
8330Sstevel@tonic-gate 	pgcnt_t mem_resv;
8340Sstevel@tonic-gate #endif
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	mutex_enter(&anoninfo_lock);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
8390Sstevel@tonic-gate 	/*
8400Sstevel@tonic-gate 	 * If some of this reservation belonged to swapfs
8410Sstevel@tonic-gate 	 * give it back to availrmem.
8420Sstevel@tonic-gate 	 * ani_mem_resv is the amount of availrmem swapfs has reserved.
8430Sstevel@tonic-gate 	 * but some of that memory could be locked by segspt so we can only
8440Sstevel@tonic-gate 	 * return non locked ani_mem_resv back to availrmem
8450Sstevel@tonic-gate 	 */
8460Sstevel@tonic-gate 	if (k_anoninfo.ani_mem_resv > k_anoninfo.ani_locked_swap) {
8470Sstevel@tonic-gate 		ANON_PRINT((A_RESV | A_MRESV),
8480Sstevel@tonic-gate 		    ("anon_unresv: growing availrmem by %ld pages\n",
8490Sstevel@tonic-gate 		    MIN(k_anoninfo.ani_mem_resv, npages)));
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 		mem_free_pages = MIN((spgcnt_t)(k_anoninfo.ani_mem_resv -
8520Sstevel@tonic-gate 		    k_anoninfo.ani_locked_swap), npages);
8530Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
8540Sstevel@tonic-gate 		availrmem += mem_free_pages;
8550Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
8560Sstevel@tonic-gate 		k_anoninfo.ani_mem_resv -= mem_free_pages;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		ANI_ADD(-mem_free_pages);
8590Sstevel@tonic-gate 	}
8600Sstevel@tonic-gate 	/*
8610Sstevel@tonic-gate 	 * The remainder of the pages is returned to phys swap
8620Sstevel@tonic-gate 	 */
8630Sstevel@tonic-gate 	ASSERT(npages >= mem_free_pages);
8640Sstevel@tonic-gate 	phys_free_slots = npages - mem_free_pages;
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	if (phys_free_slots) {
8670Sstevel@tonic-gate 	    k_anoninfo.ani_phys_resv -= phys_free_slots;
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate #ifdef	ANON_DEBUG
8710Sstevel@tonic-gate 	mem_resv = k_anoninfo.ani_mem_resv;
8720Sstevel@tonic-gate #endif
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
8750Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	mutex_exit(&anoninfo_lock);
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	ANON_PRINT(A_RESV, ("anon_unresv: %lu, tot %lu, caller %p\n",
8800Sstevel@tonic-gate 	    npages, mem_resv, (void *)caller()));
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate /*
8840Sstevel@tonic-gate  * Allocate an anon slot and return it with the lock held.
8850Sstevel@tonic-gate  */
8860Sstevel@tonic-gate struct anon *
8870Sstevel@tonic-gate anon_alloc(struct vnode *vp, anoff_t off)
8880Sstevel@tonic-gate {
8890Sstevel@tonic-gate 	struct anon	*ap;
8900Sstevel@tonic-gate 	kmutex_t	*ahm;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	ap = kmem_cache_alloc(anon_cache, KM_SLEEP);
8930Sstevel@tonic-gate 	if (vp == NULL) {
8940Sstevel@tonic-gate 		swap_alloc(ap);
8950Sstevel@tonic-gate 	} else {
8960Sstevel@tonic-gate 		ap->an_vp = vp;
8970Sstevel@tonic-gate 		ap->an_off = off;
8980Sstevel@tonic-gate 	}
8990Sstevel@tonic-gate 	ap->an_refcnt = 1;
9000Sstevel@tonic-gate 	ap->an_pvp = NULL;
9010Sstevel@tonic-gate 	ap->an_poff = 0;
9020Sstevel@tonic-gate 	ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
9030Sstevel@tonic-gate 	mutex_enter(ahm);
9040Sstevel@tonic-gate 	anon_addhash(ap);
9050Sstevel@tonic-gate 	mutex_exit(ahm);
9060Sstevel@tonic-gate 	ANI_ADD(-1);
9070Sstevel@tonic-gate 	ANON_PRINT(A_ANON, ("anon_alloc: returning ap %p, vp %p\n",
9080Sstevel@tonic-gate 	    (void *)ap, (ap ? (void *)ap->an_vp : NULL)));
9090Sstevel@tonic-gate 	return (ap);
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate /*
9130Sstevel@tonic-gate  * Decrement the reference count of an anon page.
9140Sstevel@tonic-gate  * If reference count goes to zero, free it and
9150Sstevel@tonic-gate  * its associated page (if any).
9160Sstevel@tonic-gate  */
9170Sstevel@tonic-gate void
9180Sstevel@tonic-gate anon_decref(struct anon *ap)
9190Sstevel@tonic-gate {
9200Sstevel@tonic-gate 	page_t *pp;
9210Sstevel@tonic-gate 	struct vnode *vp;
9220Sstevel@tonic-gate 	anoff_t off;
9230Sstevel@tonic-gate 	kmutex_t *ahm;
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
9260Sstevel@tonic-gate 	mutex_enter(ahm);
9270Sstevel@tonic-gate 	ASSERT(ap->an_refcnt != 0);
9280Sstevel@tonic-gate 	if (ap->an_refcnt == 0)
9290Sstevel@tonic-gate 		panic("anon_decref: slot count 0");
9300Sstevel@tonic-gate 	if (--ap->an_refcnt == 0) {
9310Sstevel@tonic-gate 		swap_xlate(ap, &vp, &off);
9320Sstevel@tonic-gate 		mutex_exit(ahm);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 		/*
9350Sstevel@tonic-gate 		 * If there is a page for this anon slot we will need to
9360Sstevel@tonic-gate 		 * call VN_DISPOSE to get rid of the vp association and
9370Sstevel@tonic-gate 		 * put the page back on the free list as really free.
9380Sstevel@tonic-gate 		 * Acquire the "exclusive" lock to ensure that any
9390Sstevel@tonic-gate 		 * pending i/o always completes before the swap slot
9400Sstevel@tonic-gate 		 * is freed.
9410Sstevel@tonic-gate 		 */
9420Sstevel@tonic-gate 		pp = page_lookup(vp, (u_offset_t)off, SE_EXCL);
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 		/*
9450Sstevel@tonic-gate 		 * If there was a page, we've synchronized on it (getting
9460Sstevel@tonic-gate 		 * the exclusive lock is as good as gettting the iolock)
9470Sstevel@tonic-gate 		 * so now we can free the physical backing store. Also, this
9480Sstevel@tonic-gate 		 * is where we would free the name of the anonymous page
9490Sstevel@tonic-gate 		 * (swap_free(ap)), a no-op in the current implementation.
9500Sstevel@tonic-gate 		 */
9510Sstevel@tonic-gate 		mutex_enter(ahm);
9520Sstevel@tonic-gate 		ASSERT(ap->an_refcnt == 0);
9530Sstevel@tonic-gate 		anon_rmhash(ap);
9540Sstevel@tonic-gate 		if (ap->an_pvp)
9550Sstevel@tonic-gate 			swap_phys_free(ap->an_pvp, ap->an_poff, PAGESIZE);
9560Sstevel@tonic-gate 		mutex_exit(ahm);
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 		if (pp != NULL) {
9590Sstevel@tonic-gate 			/*LINTED: constant in conditional context */
9600Sstevel@tonic-gate 			VN_DISPOSE(pp, B_INVAL, 0, kcred);
9610Sstevel@tonic-gate 		}
9620Sstevel@tonic-gate 		ANON_PRINT(A_ANON, ("anon_decref: free ap %p, vp %p\n",
9630Sstevel@tonic-gate 		    (void *)ap, (void *)ap->an_vp));
9640Sstevel@tonic-gate 		kmem_cache_free(anon_cache, ap);
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 		ANI_ADD(1);
9670Sstevel@tonic-gate 	} else {
9680Sstevel@tonic-gate 		mutex_exit(ahm);
9690Sstevel@tonic-gate 	}
9700Sstevel@tonic-gate }
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate static int
9730Sstevel@tonic-gate anon_share(struct anon_hdr *ahp, ulong_t anon_index, pgcnt_t nslots)
9740Sstevel@tonic-gate {
9750Sstevel@tonic-gate 	struct anon *ap;
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	while (nslots-- > 0) {
9780Sstevel@tonic-gate 		if ((ap = anon_get_ptr(ahp, anon_index)) != NULL &&
9790Sstevel@tonic-gate 		    ap->an_refcnt > 1)
9800Sstevel@tonic-gate 			return (1);
9810Sstevel@tonic-gate 		anon_index++;
9820Sstevel@tonic-gate 	}
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	return (0);
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate static void
9880Sstevel@tonic-gate anon_decref_pages(
9890Sstevel@tonic-gate 	struct anon_hdr *ahp,
9900Sstevel@tonic-gate 	ulong_t an_idx,
9910Sstevel@tonic-gate 	uint_t szc)
9920Sstevel@tonic-gate {
9930Sstevel@tonic-gate 	struct anon *ap = anon_get_ptr(ahp, an_idx);
9940Sstevel@tonic-gate 	kmutex_t *ahmpages = NULL;
9950Sstevel@tonic-gate 	page_t *pp;
9960Sstevel@tonic-gate 	pgcnt_t pgcnt = page_get_pagecnt(szc);
9970Sstevel@tonic-gate 	pgcnt_t i;
9980Sstevel@tonic-gate 	struct vnode *vp;
9990Sstevel@tonic-gate 	anoff_t   off;
10000Sstevel@tonic-gate 	kmutex_t *ahm;
10010Sstevel@tonic-gate #ifdef DEBUG
10020Sstevel@tonic-gate 	int refcnt = 1;
10030Sstevel@tonic-gate #endif
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	ASSERT(szc != 0);
10060Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
10070Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(an_idx, pgcnt));
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	VM_STAT_ADD(anonvmstats.decrefpages[0]);
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	if (ap != NULL) {
10120Sstevel@tonic-gate 		ahmpages = &anonpages_hash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
10130Sstevel@tonic-gate 		mutex_enter(ahmpages);
10140Sstevel@tonic-gate 		ASSERT((refcnt = ap->an_refcnt) != 0);
10150Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.decrefpages[1]);
10160Sstevel@tonic-gate 		if (ap->an_refcnt == 1) {
10170Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.decrefpages[2]);
10180Sstevel@tonic-gate 			ASSERT(!anon_share(ahp, an_idx, pgcnt));
10190Sstevel@tonic-gate 			mutex_exit(ahmpages);
10200Sstevel@tonic-gate 			ahmpages = NULL;
10210Sstevel@tonic-gate 		}
10220Sstevel@tonic-gate 	}
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	i = 0;
10250Sstevel@tonic-gate 	while (i < pgcnt) {
10260Sstevel@tonic-gate 		if ((ap = anon_get_ptr(ahp, an_idx + i)) == NULL) {
10270Sstevel@tonic-gate 			ASSERT(refcnt == 1 && ahmpages == NULL);
10280Sstevel@tonic-gate 			i++;
10290Sstevel@tonic-gate 			continue;
10300Sstevel@tonic-gate 		}
10310Sstevel@tonic-gate 		ASSERT(ap->an_refcnt == refcnt);
10320Sstevel@tonic-gate 		ASSERT(ahmpages != NULL || ap->an_refcnt == 1);
10330Sstevel@tonic-gate 		ASSERT(ahmpages == NULL || ap->an_refcnt > 1);
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 		if (ahmpages == NULL) {
10360Sstevel@tonic-gate 			swap_xlate(ap, &vp, &off);
10370Sstevel@tonic-gate 			pp = page_lookup(vp, (u_offset_t)off, SE_EXCL);
10380Sstevel@tonic-gate 			if (pp == NULL || pp->p_szc == 0) {
10390Sstevel@tonic-gate 				VM_STAT_ADD(anonvmstats.decrefpages[3]);
10400Sstevel@tonic-gate 				ahm = &anonhash_lock[AH_LOCK(ap->an_vp,
10410Sstevel@tonic-gate 				    ap->an_off)];
10420Sstevel@tonic-gate 				(void) anon_set_ptr(ahp, an_idx + i, NULL,
10430Sstevel@tonic-gate 				    ANON_SLEEP);
10440Sstevel@tonic-gate 				mutex_enter(ahm);
10450Sstevel@tonic-gate 				ap->an_refcnt--;
10460Sstevel@tonic-gate 				ASSERT(ap->an_refcnt == 0);
10470Sstevel@tonic-gate 				anon_rmhash(ap);
10480Sstevel@tonic-gate 				if (ap->an_pvp)
10490Sstevel@tonic-gate 					swap_phys_free(ap->an_pvp, ap->an_poff,
10500Sstevel@tonic-gate 					    PAGESIZE);
10510Sstevel@tonic-gate 				mutex_exit(ahm);
10520Sstevel@tonic-gate 				if (pp != NULL) {
10530Sstevel@tonic-gate 					VM_STAT_ADD(anonvmstats.decrefpages[4]);
10540Sstevel@tonic-gate 					/*LINTED*/
10550Sstevel@tonic-gate 					VN_DISPOSE(pp, B_INVAL, 0, kcred);
10560Sstevel@tonic-gate 				}
10570Sstevel@tonic-gate 				kmem_cache_free(anon_cache, ap);
10580Sstevel@tonic-gate 				ANI_ADD(1);
10590Sstevel@tonic-gate 				i++;
10600Sstevel@tonic-gate 			} else {
10610Sstevel@tonic-gate 				pgcnt_t j;
10620Sstevel@tonic-gate 				pgcnt_t curpgcnt =
10630Sstevel@tonic-gate 				    page_get_pagecnt(pp->p_szc);
10640Sstevel@tonic-gate 				size_t ppasize = curpgcnt * sizeof (page_t *);
10650Sstevel@tonic-gate 				page_t **ppa = kmem_alloc(ppasize, KM_SLEEP);
10660Sstevel@tonic-gate 				int dispose = 0;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 				VM_STAT_ADD(anonvmstats.decrefpages[5]);
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 				ASSERT(pp->p_szc <= szc);
10710Sstevel@tonic-gate 				ASSERT(IS_P2ALIGNED(curpgcnt, curpgcnt));
10720Sstevel@tonic-gate 				ASSERT(IS_P2ALIGNED(i, curpgcnt));
10730Sstevel@tonic-gate 				ASSERT(i + curpgcnt <= pgcnt);
10740Sstevel@tonic-gate 				ASSERT(!(page_pptonum(pp) & (curpgcnt - 1)));
10750Sstevel@tonic-gate 				ppa[0] = pp;
10760Sstevel@tonic-gate 				for (j = i + 1; j < i + curpgcnt; j++) {
10770Sstevel@tonic-gate 					ap = anon_get_ptr(ahp, an_idx + j);
10780Sstevel@tonic-gate 					ASSERT(ap != NULL &&
10790Sstevel@tonic-gate 					    ap->an_refcnt == 1);
10800Sstevel@tonic-gate 					swap_xlate(ap, &vp, &off);
10810Sstevel@tonic-gate 					pp = page_lookup(vp, (u_offset_t)off,
10820Sstevel@tonic-gate 					    SE_EXCL);
10830Sstevel@tonic-gate 					if (pp == NULL)
10840Sstevel@tonic-gate 						panic("anon_decref_pages: "
10850Sstevel@tonic-gate 						    "no page");
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 					(void) hat_pageunload(pp,
10880Sstevel@tonic-gate 					    HAT_FORCE_PGUNLOAD);
10890Sstevel@tonic-gate 					ASSERT(pp->p_szc == ppa[0]->p_szc);
10900Sstevel@tonic-gate 					ASSERT(page_pptonum(pp) - 1 ==
10910Sstevel@tonic-gate 					    page_pptonum(ppa[j - i - 1]));
10920Sstevel@tonic-gate 					ppa[j - i] = pp;
10930Sstevel@tonic-gate 					if (ap->an_pvp != NULL &&
10940Sstevel@tonic-gate 					    !vn_matchopval(ap->an_pvp,
10950Sstevel@tonic-gate 						VOPNAME_DISPOSE,
10960Sstevel@tonic-gate 						(fs_generic_func_p)fs_dispose))
10970Sstevel@tonic-gate 						dispose = 1;
10980Sstevel@tonic-gate 				}
10990Sstevel@tonic-gate 				if (!dispose) {
11000Sstevel@tonic-gate 					VM_STAT_ADD(anonvmstats.decrefpages[6]);
11010Sstevel@tonic-gate 					page_destroy_pages(ppa[0]);
11020Sstevel@tonic-gate 				} else {
11030Sstevel@tonic-gate 					VM_STAT_ADD(anonvmstats.decrefpages[7]);
11040Sstevel@tonic-gate 					for (j = 0; j < curpgcnt; j++) {
11050Sstevel@tonic-gate 						ASSERT(PAGE_EXCL(ppa[j]));
11060Sstevel@tonic-gate 						ppa[j]->p_szc = 0;
11070Sstevel@tonic-gate 					}
11080Sstevel@tonic-gate 					for (j = 0; j < curpgcnt; j++) {
11090Sstevel@tonic-gate 						ASSERT(!hat_page_is_mapped(
11100Sstevel@tonic-gate 						    ppa[j]));
11110Sstevel@tonic-gate 						/*LINTED*/
11120Sstevel@tonic-gate 						VN_DISPOSE(ppa[j], B_INVAL, 0,
11130Sstevel@tonic-gate 						    kcred);
11140Sstevel@tonic-gate 					}
11150Sstevel@tonic-gate 				}
11160Sstevel@tonic-gate 				kmem_free(ppa, ppasize);
11170Sstevel@tonic-gate 				for (j = i; j < i + curpgcnt; j++) {
11180Sstevel@tonic-gate 					ap = anon_get_ptr(ahp, an_idx + j);
11190Sstevel@tonic-gate 					ASSERT(ap != NULL &&
11200Sstevel@tonic-gate 					    ap->an_refcnt == 1);
11210Sstevel@tonic-gate 					ahm = &anonhash_lock[AH_LOCK(ap->an_vp,
11220Sstevel@tonic-gate 					    ap->an_off)];
11230Sstevel@tonic-gate 					(void) anon_set_ptr(ahp, an_idx + j,
11240Sstevel@tonic-gate 					    NULL, ANON_SLEEP);
11250Sstevel@tonic-gate 					mutex_enter(ahm);
11260Sstevel@tonic-gate 					ap->an_refcnt--;
11270Sstevel@tonic-gate 					ASSERT(ap->an_refcnt == 0);
11280Sstevel@tonic-gate 					anon_rmhash(ap);
11290Sstevel@tonic-gate 					if (ap->an_pvp)
11300Sstevel@tonic-gate 						swap_phys_free(ap->an_pvp,
11310Sstevel@tonic-gate 							ap->an_poff, PAGESIZE);
11320Sstevel@tonic-gate 					mutex_exit(ahm);
11330Sstevel@tonic-gate 					kmem_cache_free(anon_cache, ap);
11340Sstevel@tonic-gate 					ANI_ADD(1);
11350Sstevel@tonic-gate 				}
11360Sstevel@tonic-gate 				i += curpgcnt;
11370Sstevel@tonic-gate 			}
11380Sstevel@tonic-gate 		} else {
11390Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.decrefpages[8]);
11400Sstevel@tonic-gate 			(void) anon_set_ptr(ahp, an_idx + i, NULL, ANON_SLEEP);
11410Sstevel@tonic-gate 			ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
11420Sstevel@tonic-gate 			mutex_enter(ahm);
11430Sstevel@tonic-gate 			ap->an_refcnt--;
11440Sstevel@tonic-gate 			mutex_exit(ahm);
11450Sstevel@tonic-gate 			i++;
11460Sstevel@tonic-gate 		}
11470Sstevel@tonic-gate 	}
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	if (ahmpages != NULL) {
11500Sstevel@tonic-gate 		mutex_exit(ahmpages);
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate }
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate /*
11550Sstevel@tonic-gate  * Duplicate references to size bytes worth of anon pages.
11560Sstevel@tonic-gate  * Used when duplicating a segment that contains private anon pages.
11570Sstevel@tonic-gate  * This code assumes that procedure calling this one has already used
11580Sstevel@tonic-gate  * hat_chgprot() to disable write access to the range of addresses that
11590Sstevel@tonic-gate  * that *old actually refers to.
11600Sstevel@tonic-gate  */
11610Sstevel@tonic-gate void
11620Sstevel@tonic-gate anon_dup(struct anon_hdr *old, ulong_t old_idx, struct anon_hdr *new,
11630Sstevel@tonic-gate 			ulong_t new_idx, size_t size)
11640Sstevel@tonic-gate {
11650Sstevel@tonic-gate 	spgcnt_t npages;
11660Sstevel@tonic-gate 	kmutex_t *ahm;
11670Sstevel@tonic-gate 	struct anon *ap;
11680Sstevel@tonic-gate 	ulong_t off;
11690Sstevel@tonic-gate 	ulong_t index;
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	npages = btopr(size);
11720Sstevel@tonic-gate 	while (npages > 0) {
11730Sstevel@tonic-gate 		index = old_idx;
11740Sstevel@tonic-gate 		if ((ap = anon_get_next_ptr(old, &index)) == NULL)
11750Sstevel@tonic-gate 			break;
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 		ASSERT(!ANON_ISBUSY(anon_get_slot(old, index)));
11780Sstevel@tonic-gate 		off = index - old_idx;
11790Sstevel@tonic-gate 		npages -= off;
11800Sstevel@tonic-gate 		if (npages <= 0)
11810Sstevel@tonic-gate 			break;
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 		(void) anon_set_ptr(new, new_idx + off, ap, ANON_SLEEP);
11840Sstevel@tonic-gate 		ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 		mutex_enter(ahm);
11870Sstevel@tonic-gate 		ap->an_refcnt++;
11880Sstevel@tonic-gate 		mutex_exit(ahm);
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 		off++;
11910Sstevel@tonic-gate 		new_idx += off;
11920Sstevel@tonic-gate 		old_idx += off;
11930Sstevel@tonic-gate 		npages--;
11940Sstevel@tonic-gate 	}
11950Sstevel@tonic-gate }
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate /*
11980Sstevel@tonic-gate  * Just like anon_dup but also guarantees there are no holes (unallocated anon
11990Sstevel@tonic-gate  * slots) within any large page region. That means if a large page region is
12000Sstevel@tonic-gate  * empty in the old array it will skip it. If there are 1 or more valid slots
12010Sstevel@tonic-gate  * in the large page region of the old array it will make sure to fill in any
12020Sstevel@tonic-gate  * unallocated ones and also copy them to the new array. If noalloc is 1 large
12030Sstevel@tonic-gate  * page region should either have no valid anon slots or all slots should be
12040Sstevel@tonic-gate  * valid.
12050Sstevel@tonic-gate  */
12060Sstevel@tonic-gate void
12070Sstevel@tonic-gate anon_dup_fill_holes(
12080Sstevel@tonic-gate 	struct anon_hdr *old,
12090Sstevel@tonic-gate 	ulong_t old_idx,
12100Sstevel@tonic-gate 	struct anon_hdr *new,
12110Sstevel@tonic-gate 	ulong_t new_idx,
12120Sstevel@tonic-gate 	size_t size,
12130Sstevel@tonic-gate 	uint_t szc,
12140Sstevel@tonic-gate 	int noalloc)
12150Sstevel@tonic-gate {
12160Sstevel@tonic-gate 	struct anon	*ap;
12170Sstevel@tonic-gate 	spgcnt_t	npages;
12180Sstevel@tonic-gate 	kmutex_t	*ahm, *ahmpages = NULL;
12190Sstevel@tonic-gate 	pgcnt_t		pgcnt, i;
12200Sstevel@tonic-gate 	ulong_t		index, off;
12210Sstevel@tonic-gate #ifdef DEBUG
12220Sstevel@tonic-gate 	int		refcnt;
12230Sstevel@tonic-gate #endif
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	ASSERT(szc != 0);
12260Sstevel@tonic-gate 	pgcnt = page_get_pagecnt(szc);
12270Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
12280Sstevel@tonic-gate 	npages = btopr(size);
12290Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(npages, pgcnt));
12300Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(old_idx, pgcnt));
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	VM_STAT_ADD(anonvmstats.dupfillholes[0]);
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	while (npages > 0) {
12350Sstevel@tonic-gate 		index = old_idx;
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 		/*
12380Sstevel@tonic-gate 		 * Find the next valid slot.
12390Sstevel@tonic-gate 		 */
12400Sstevel@tonic-gate 		if (anon_get_next_ptr(old, &index) == NULL)
12410Sstevel@tonic-gate 			break;
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 		ASSERT(!ANON_ISBUSY(anon_get_slot(old, index)));
12440Sstevel@tonic-gate 		/*
12450Sstevel@tonic-gate 		 * Now backup index to the beginning of the
12460Sstevel@tonic-gate 		 * current large page region of the old array.
12470Sstevel@tonic-gate 		 */
12480Sstevel@tonic-gate 		index = P2ALIGN(index, pgcnt);
12490Sstevel@tonic-gate 		off = index - old_idx;
12500Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(off, pgcnt));
12510Sstevel@tonic-gate 		npages -= off;
12520Sstevel@tonic-gate 		if (npages <= 0)
12530Sstevel@tonic-gate 			break;
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 		/*
12560Sstevel@tonic-gate 		 * Fill and copy a large page regions worth
12570Sstevel@tonic-gate 		 * of anon slots.
12580Sstevel@tonic-gate 		 */
12590Sstevel@tonic-gate 		for (i = 0; i < pgcnt; i++) {
12600Sstevel@tonic-gate 			if ((ap = anon_get_ptr(old, index + i)) == NULL) {
12610Sstevel@tonic-gate 				if (noalloc) {
12620Sstevel@tonic-gate 					panic("anon_dup_fill_holes: "
12630Sstevel@tonic-gate 					    "empty anon slot\n");
12640Sstevel@tonic-gate 				}
12650Sstevel@tonic-gate 				VM_STAT_ADD(anonvmstats.dupfillholes[1]);
12660Sstevel@tonic-gate 				ap = anon_alloc(NULL, 0);
12670Sstevel@tonic-gate 				(void) anon_set_ptr(old, index + i, ap,
12680Sstevel@tonic-gate 				    ANON_SLEEP);
12690Sstevel@tonic-gate 			} else if (i == 0) {
12700Sstevel@tonic-gate 				/*
12710Sstevel@tonic-gate 				 * make the increment of all refcnts of all
12720Sstevel@tonic-gate 				 * anon slots of a large page appear atomic by
12730Sstevel@tonic-gate 				 * getting an anonpages_hash_lock for the
12740Sstevel@tonic-gate 				 * first anon slot of a large page.
12750Sstevel@tonic-gate 				 */
12760Sstevel@tonic-gate 				int hash = AH_LOCK(ap->an_vp, ap->an_off);
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 				VM_STAT_ADD(anonvmstats.dupfillholes[2]);
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 				ahmpages = &anonpages_hash_lock[hash];
12810Sstevel@tonic-gate 				mutex_enter(ahmpages);
12820Sstevel@tonic-gate 				/*LINTED*/
12830Sstevel@tonic-gate 				ASSERT(refcnt = ap->an_refcnt);
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 				VM_STAT_COND_ADD(ap->an_refcnt > 1,
12860Sstevel@tonic-gate 				    anonvmstats.dupfillholes[3]);
12870Sstevel@tonic-gate 			}
12880Sstevel@tonic-gate 			(void) anon_set_ptr(new, new_idx + off + i, ap,
12890Sstevel@tonic-gate 			    ANON_SLEEP);
12900Sstevel@tonic-gate 			ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
12910Sstevel@tonic-gate 			mutex_enter(ahm);
12920Sstevel@tonic-gate 			ASSERT(ahmpages != NULL || ap->an_refcnt == 1);
12930Sstevel@tonic-gate 			ASSERT(i == 0 || ahmpages == NULL ||
12940Sstevel@tonic-gate 			    refcnt == ap->an_refcnt);
12950Sstevel@tonic-gate 			ap->an_refcnt++;
12960Sstevel@tonic-gate 			mutex_exit(ahm);
12970Sstevel@tonic-gate 		}
12980Sstevel@tonic-gate 		if (ahmpages != NULL) {
12990Sstevel@tonic-gate 			mutex_exit(ahmpages);
13000Sstevel@tonic-gate 			ahmpages = NULL;
13010Sstevel@tonic-gate 		}
13020Sstevel@tonic-gate 		off += pgcnt;
13030Sstevel@tonic-gate 		new_idx += off;
13040Sstevel@tonic-gate 		old_idx += off;
13050Sstevel@tonic-gate 		npages -= pgcnt;
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate }
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate /*
13100Sstevel@tonic-gate  * Used when a segment with a vnode changes szc. similarly to
13110Sstevel@tonic-gate  * anon_dup_fill_holes() makes sure each large page region either has no anon
13120Sstevel@tonic-gate  * slots or all of them. but new slots are created by COWing the file
13130Sstevel@tonic-gate  * pages. on entrance no anon slots should be shared.
13140Sstevel@tonic-gate  */
13150Sstevel@tonic-gate int
13160Sstevel@tonic-gate anon_fill_cow_holes(
13170Sstevel@tonic-gate 	struct seg *seg,
13180Sstevel@tonic-gate 	caddr_t addr,
13190Sstevel@tonic-gate 	struct anon_hdr *ahp,
13200Sstevel@tonic-gate 	ulong_t an_idx,
13210Sstevel@tonic-gate 	struct vnode *vp,
13220Sstevel@tonic-gate 	u_offset_t vp_off,
13230Sstevel@tonic-gate 	size_t size,
13240Sstevel@tonic-gate 	uint_t szc,
13250Sstevel@tonic-gate 	uint_t prot,
13260Sstevel@tonic-gate 	struct vpage vpage[],
13270Sstevel@tonic-gate 	struct cred *cred)
13280Sstevel@tonic-gate {
13290Sstevel@tonic-gate 	struct anon	*ap;
13300Sstevel@tonic-gate 	spgcnt_t	npages;
13310Sstevel@tonic-gate 	pgcnt_t		pgcnt, i;
13320Sstevel@tonic-gate 	ulong_t		index, off;
13330Sstevel@tonic-gate 	int		err = 0;
13340Sstevel@tonic-gate 	int		pageflags = 0;
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	ASSERT(szc != 0);
13370Sstevel@tonic-gate 	pgcnt = page_get_pagecnt(szc);
13380Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
13390Sstevel@tonic-gate 	npages = btopr(size);
13400Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(npages, pgcnt));
13410Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(an_idx, pgcnt));
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	while (npages > 0) {
13440Sstevel@tonic-gate 		index = an_idx;
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 		/*
13470Sstevel@tonic-gate 		 * Find the next valid slot.
13480Sstevel@tonic-gate 		 */
13490Sstevel@tonic-gate 		if (anon_get_next_ptr(ahp, &index) == NULL) {
13500Sstevel@tonic-gate 			break;
13510Sstevel@tonic-gate 		}
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 		ASSERT(!ANON_ISBUSY(anon_get_slot(ahp, index)));
13540Sstevel@tonic-gate 		/*
13550Sstevel@tonic-gate 		 * Now backup index to the beginning of the
13560Sstevel@tonic-gate 		 * current large page region of the anon array.
13570Sstevel@tonic-gate 		 */
13580Sstevel@tonic-gate 		index = P2ALIGN(index, pgcnt);
13590Sstevel@tonic-gate 		off = index - an_idx;
13600Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(off, pgcnt));
13610Sstevel@tonic-gate 		npages -= off;
13620Sstevel@tonic-gate 		if (npages <= 0)
13630Sstevel@tonic-gate 			break;
13640Sstevel@tonic-gate 		an_idx += off;
13650Sstevel@tonic-gate 		vp_off += ptob(off);
13660Sstevel@tonic-gate 		addr += ptob(off);
13670Sstevel@tonic-gate 		if (vpage != NULL) {
13680Sstevel@tonic-gate 			vpage += off;
13690Sstevel@tonic-gate 		}
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 		for (i = 0; i < pgcnt; i++, an_idx++, vp_off += PAGESIZE) {
13720Sstevel@tonic-gate 			if ((ap = anon_get_ptr(ahp, an_idx)) == NULL) {
13730Sstevel@tonic-gate 				page_t *pl[1 + 1];
13740Sstevel@tonic-gate 				page_t *pp;
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 				err = VOP_GETPAGE(vp, vp_off, PAGESIZE, NULL,
13770Sstevel@tonic-gate 				    pl, PAGESIZE, seg, addr, S_READ, cred);
13780Sstevel@tonic-gate 				if (err) {
13790Sstevel@tonic-gate 					break;
13800Sstevel@tonic-gate 				}
13810Sstevel@tonic-gate 				if (vpage != NULL) {
13820Sstevel@tonic-gate 					prot = VPP_PROT(vpage);
13830Sstevel@tonic-gate 					pageflags = VPP_ISPPLOCK(vpage) ?
13840Sstevel@tonic-gate 					    LOCK_PAGE : 0;
13850Sstevel@tonic-gate 				}
13860Sstevel@tonic-gate 				pp = anon_private(&ap, seg, addr, prot, pl[0],
13870Sstevel@tonic-gate 					pageflags, cred);
13880Sstevel@tonic-gate 				if (pp == NULL) {
13890Sstevel@tonic-gate 					err = ENOMEM;
13900Sstevel@tonic-gate 					break;
13910Sstevel@tonic-gate 				}
13920Sstevel@tonic-gate 				(void) anon_set_ptr(ahp, an_idx, ap,
13930Sstevel@tonic-gate 				    ANON_SLEEP);
13940Sstevel@tonic-gate 				page_unlock(pp);
13950Sstevel@tonic-gate 			}
13960Sstevel@tonic-gate 			ASSERT(ap->an_refcnt == 1);
13970Sstevel@tonic-gate 			addr += PAGESIZE;
13980Sstevel@tonic-gate 			if (vpage != NULL) {
13990Sstevel@tonic-gate 				vpage++;
14000Sstevel@tonic-gate 			}
14010Sstevel@tonic-gate 		}
14020Sstevel@tonic-gate 		npages -= pgcnt;
14030Sstevel@tonic-gate 	}
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	return (err);
14060Sstevel@tonic-gate }
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate /*
14090Sstevel@tonic-gate  * Free a group of "size" anon pages, size in bytes,
14100Sstevel@tonic-gate  * and clear out the pointers to the anon entries.
14110Sstevel@tonic-gate  */
14120Sstevel@tonic-gate void
14130Sstevel@tonic-gate anon_free(struct anon_hdr *ahp, ulong_t index, size_t size)
14140Sstevel@tonic-gate {
14150Sstevel@tonic-gate 	spgcnt_t npages;
14160Sstevel@tonic-gate 	struct anon *ap;
14170Sstevel@tonic-gate 	ulong_t old;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	npages = btopr(size);
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	while (npages > 0) {
14220Sstevel@tonic-gate 		old = index;
14230Sstevel@tonic-gate 		if ((ap = anon_get_next_ptr(ahp, &index)) == NULL)
14240Sstevel@tonic-gate 			break;
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 		ASSERT(!ANON_ISBUSY(anon_get_slot(ahp, index)));
14270Sstevel@tonic-gate 		npages -= index - old;
14280Sstevel@tonic-gate 		if (npages <= 0)
14290Sstevel@tonic-gate 			break;
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 		(void) anon_set_ptr(ahp, index, NULL, ANON_SLEEP);
14320Sstevel@tonic-gate 		anon_decref(ap);
14330Sstevel@tonic-gate 		/*
14340Sstevel@tonic-gate 		 * Bump index and decrement page count
14350Sstevel@tonic-gate 		 */
14360Sstevel@tonic-gate 		index++;
14370Sstevel@tonic-gate 		npages--;
14380Sstevel@tonic-gate 	}
14390Sstevel@tonic-gate }
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate void
14420Sstevel@tonic-gate anon_free_pages(
14430Sstevel@tonic-gate 	struct anon_hdr *ahp,
14440Sstevel@tonic-gate 	ulong_t an_idx,
14450Sstevel@tonic-gate 	size_t size,
14460Sstevel@tonic-gate 	uint_t szc)
14470Sstevel@tonic-gate {
14480Sstevel@tonic-gate 	spgcnt_t	npages;
14490Sstevel@tonic-gate 	pgcnt_t		pgcnt;
14500Sstevel@tonic-gate 	ulong_t		index, off;
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 	ASSERT(szc != 0);
14530Sstevel@tonic-gate 	pgcnt = page_get_pagecnt(szc);
14540Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
14550Sstevel@tonic-gate 	npages = btopr(size);
14560Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(npages, pgcnt));
14570Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(an_idx, pgcnt));
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	VM_STAT_ADD(anonvmstats.freepages[0]);
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	while (npages > 0) {
14620Sstevel@tonic-gate 		index = an_idx;
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 		/*
14650Sstevel@tonic-gate 		 * Find the next valid slot.
14660Sstevel@tonic-gate 		 */
14670Sstevel@tonic-gate 		if (anon_get_next_ptr(ahp, &index) == NULL)
14680Sstevel@tonic-gate 			break;
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 		ASSERT(!ANON_ISBUSY(anon_get_slot(ahp, index)));
14710Sstevel@tonic-gate 		/*
14720Sstevel@tonic-gate 		 * Now backup index to the beginning of the
14730Sstevel@tonic-gate 		 * current large page region of the old array.
14740Sstevel@tonic-gate 		 */
14750Sstevel@tonic-gate 		index = P2ALIGN(index, pgcnt);
14760Sstevel@tonic-gate 		off = index - an_idx;
14770Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(off, pgcnt));
14780Sstevel@tonic-gate 		npages -= off;
14790Sstevel@tonic-gate 		if (npages <= 0)
14800Sstevel@tonic-gate 			break;
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 		anon_decref_pages(ahp, index, szc);
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 		off += pgcnt;
14850Sstevel@tonic-gate 		an_idx += off;
14860Sstevel@tonic-gate 		npages -= pgcnt;
14870Sstevel@tonic-gate 	}
14880Sstevel@tonic-gate }
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate /*
14910Sstevel@tonic-gate  * Make anonymous pages discardable
14920Sstevel@tonic-gate  */
14930Sstevel@tonic-gate void
14940Sstevel@tonic-gate anon_disclaim(struct anon_map *amp, ulong_t index, size_t size, int flags)
14950Sstevel@tonic-gate {
14960Sstevel@tonic-gate 	spgcnt_t npages = btopr(size);
14970Sstevel@tonic-gate 	struct anon *ap;
14980Sstevel@tonic-gate 	struct vnode *vp;
14990Sstevel@tonic-gate 	anoff_t off;
15000Sstevel@tonic-gate 	page_t *pp, *root_pp;
15010Sstevel@tonic-gate 	kmutex_t *ahm;
15020Sstevel@tonic-gate 	pgcnt_t pgcnt;
15030Sstevel@tonic-gate 	ulong_t old_idx, idx, i;
15040Sstevel@tonic-gate 	struct anon_hdr *ahp = amp->ahp;
15050Sstevel@tonic-gate 	anon_sync_obj_t cookie;
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&amp->a_rwlock));
15080Sstevel@tonic-gate 	pgcnt = 1;
15090Sstevel@tonic-gate 	for (; npages > 0; index = (pgcnt == 1) ? index + 1:
15100Sstevel@tonic-gate 		P2ROUNDUP(index + 1, pgcnt), npages -= pgcnt) {
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 		/*
15130Sstevel@tonic-gate 		 * get anon pointer and index for the first valid entry
15140Sstevel@tonic-gate 		 * in the anon list, starting from "index"
15150Sstevel@tonic-gate 		 */
15160Sstevel@tonic-gate 		old_idx = index;
15170Sstevel@tonic-gate 		if ((ap = anon_get_next_ptr(ahp, &index)) == NULL)
15180Sstevel@tonic-gate 			break;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 		/*
15210Sstevel@tonic-gate 		 * decrement npages by number of NULL anon slots we skipped
15220Sstevel@tonic-gate 		 */
15230Sstevel@tonic-gate 		npages -= index - old_idx;
15240Sstevel@tonic-gate 		if (npages <= 0)
15250Sstevel@tonic-gate 			break;
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 		anon_array_enter(amp, index, &cookie);
15280Sstevel@tonic-gate 		ap = anon_get_ptr(ahp, index);
15290Sstevel@tonic-gate 		ASSERT(ap != NULL);
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 		/*
15320Sstevel@tonic-gate 		 * Get anonymous page and try to lock it SE_EXCL;
15330Sstevel@tonic-gate 		 * For non blocking case if we couldn't grab the lock
15340Sstevel@tonic-gate 		 * we skip to next page.
15350Sstevel@tonic-gate 		 * For blocking case (ANON_PGLOOKUP_BLK) block
15360Sstevel@tonic-gate 		 * until we grab SE_EXCL lock.
15370Sstevel@tonic-gate 		 */
15380Sstevel@tonic-gate 		swap_xlate(ap, &vp, &off);
15390Sstevel@tonic-gate 		if (flags & ANON_PGLOOKUP_BLK)
15400Sstevel@tonic-gate 			pp = page_lookup_create(vp, (u_offset_t)off,
15410Sstevel@tonic-gate 			    SE_EXCL, NULL, NULL, SE_EXCL_WANTED);
15420Sstevel@tonic-gate 		else
15430Sstevel@tonic-gate 			pp = page_lookup_nowait(vp, (u_offset_t)off, SE_EXCL);
15440Sstevel@tonic-gate 		if (pp == NULL) {
15450Sstevel@tonic-gate 			segadvstat.MADV_FREE_miss.value.ul++;
15460Sstevel@tonic-gate 			pgcnt = 1;
15470Sstevel@tonic-gate 			anon_array_exit(&cookie);
15480Sstevel@tonic-gate 			continue;
15490Sstevel@tonic-gate 		}
15500Sstevel@tonic-gate 		pgcnt = page_get_pagecnt(pp->p_szc);
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 		/*
15530Sstevel@tonic-gate 		 * we cannot free a page which is permanently locked.
15540Sstevel@tonic-gate 		 * The page_struct_lock need not be acquired to examine
15550Sstevel@tonic-gate 		 * these fields since the page has an "exclusive" lock.
15560Sstevel@tonic-gate 		 */
15570Sstevel@tonic-gate 		if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
15580Sstevel@tonic-gate 			page_unlock(pp);
15590Sstevel@tonic-gate 			segadvstat.MADV_FREE_miss.value.ul++;
15600Sstevel@tonic-gate 			anon_array_exit(&cookie);
15610Sstevel@tonic-gate 			continue;
15620Sstevel@tonic-gate 		}
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 		ahm = &anonhash_lock[AH_LOCK(vp, off)];
15650Sstevel@tonic-gate 		mutex_enter(ahm);
15660Sstevel@tonic-gate 		ASSERT(ap->an_refcnt != 0);
15670Sstevel@tonic-gate 		/*
15680Sstevel@tonic-gate 		 * skip this one if copy-on-write is not yet broken.
15690Sstevel@tonic-gate 		 */
15700Sstevel@tonic-gate 		if (ap->an_refcnt > 1) {
15710Sstevel@tonic-gate 			mutex_exit(ahm);
15720Sstevel@tonic-gate 			page_unlock(pp);
15730Sstevel@tonic-gate 			segadvstat.MADV_FREE_miss.value.ul++;
15740Sstevel@tonic-gate 			anon_array_exit(&cookie);
15750Sstevel@tonic-gate 			continue;
15760Sstevel@tonic-gate 		}
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 		if (pp->p_szc == 0) {
15790Sstevel@tonic-gate 			pgcnt = 1;
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 			/*
15820Sstevel@tonic-gate 			 * free swap slot;
15830Sstevel@tonic-gate 			 */
15840Sstevel@tonic-gate 			if (ap->an_pvp) {
15850Sstevel@tonic-gate 				swap_phys_free(ap->an_pvp, ap->an_poff,
15860Sstevel@tonic-gate 				    PAGESIZE);
15870Sstevel@tonic-gate 				ap->an_pvp = NULL;
15880Sstevel@tonic-gate 				ap->an_poff = 0;
15890Sstevel@tonic-gate 			}
15900Sstevel@tonic-gate 			mutex_exit(ahm);
15910Sstevel@tonic-gate 			segadvstat.MADV_FREE_hit.value.ul++;
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 			/*
15940Sstevel@tonic-gate 			 * while we are at it, unload all the translations
15950Sstevel@tonic-gate 			 * and attempt to free the page.
15960Sstevel@tonic-gate 			 */
15970Sstevel@tonic-gate 			(void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
15980Sstevel@tonic-gate 			/*LINTED: constant in conditional context */
15990Sstevel@tonic-gate 			VN_DISPOSE(pp, B_FREE, 0, kcred);
16000Sstevel@tonic-gate 			anon_array_exit(&cookie);
16010Sstevel@tonic-gate 			continue;
16020Sstevel@tonic-gate 		}
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 		pgcnt = page_get_pagecnt(pp->p_szc);
16050Sstevel@tonic-gate 		if (!IS_P2ALIGNED(index, pgcnt)) {
16060Sstevel@tonic-gate 			if (!page_try_demote_pages(pp)) {
16070Sstevel@tonic-gate 				mutex_exit(ahm);
16080Sstevel@tonic-gate 				page_unlock(pp);
16090Sstevel@tonic-gate 				segadvstat.MADV_FREE_miss.value.ul++;
16100Sstevel@tonic-gate 				anon_array_exit(&cookie);
16110Sstevel@tonic-gate 				continue;
16120Sstevel@tonic-gate 			} else {
16130Sstevel@tonic-gate 				pgcnt = 1;
16140Sstevel@tonic-gate 				if (ap->an_pvp) {
16150Sstevel@tonic-gate 					swap_phys_free(ap->an_pvp,
16160Sstevel@tonic-gate 					    ap->an_poff, PAGESIZE);
16170Sstevel@tonic-gate 					    ap->an_pvp = NULL;
16180Sstevel@tonic-gate 					    ap->an_poff = 0;
16190Sstevel@tonic-gate 				}
16200Sstevel@tonic-gate 				mutex_exit(ahm);
16210Sstevel@tonic-gate 				(void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
16220Sstevel@tonic-gate 				/*LINTED*/
16230Sstevel@tonic-gate 				VN_DISPOSE(pp, B_FREE, 0, kcred);
16240Sstevel@tonic-gate 				segadvstat.MADV_FREE_hit.value.ul++;
16250Sstevel@tonic-gate 				anon_array_exit(&cookie);
16260Sstevel@tonic-gate 				continue;
16270Sstevel@tonic-gate 			}
16280Sstevel@tonic-gate 		}
16290Sstevel@tonic-gate 		mutex_exit(ahm);
16300Sstevel@tonic-gate 		root_pp = pp;
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 		/*
16330Sstevel@tonic-gate 		 * try to lock remaining pages
16340Sstevel@tonic-gate 		 */
16350Sstevel@tonic-gate 		for (idx = 1; idx < pgcnt; idx++) {
1636414Skchow 			pp++;
16370Sstevel@tonic-gate 			if (!page_trylock(pp, SE_EXCL))
16380Sstevel@tonic-gate 				break;
16390Sstevel@tonic-gate 			if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
16400Sstevel@tonic-gate 				page_unlock(pp);
16410Sstevel@tonic-gate 				break;
16420Sstevel@tonic-gate 			}
16430Sstevel@tonic-gate 		}
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 		if (idx == pgcnt) {
16460Sstevel@tonic-gate 			for (i = 0; i < pgcnt; i++) {
16470Sstevel@tonic-gate 				ap = anon_get_ptr(ahp, index + i);
16480Sstevel@tonic-gate 				if (ap == NULL)
16490Sstevel@tonic-gate 					break;
16500Sstevel@tonic-gate 				swap_xlate(ap, &vp, &off);
16510Sstevel@tonic-gate 				ahm = &anonhash_lock[AH_LOCK(vp, off)];
16520Sstevel@tonic-gate 				mutex_enter(ahm);
16530Sstevel@tonic-gate 				ASSERT(ap->an_refcnt != 0);
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 				/*
16560Sstevel@tonic-gate 				 * skip this one if copy-on-write
16570Sstevel@tonic-gate 				 * is not yet broken.
16580Sstevel@tonic-gate 				 */
16590Sstevel@tonic-gate 				if (ap->an_refcnt > 1) {
16600Sstevel@tonic-gate 					mutex_exit(ahm);
16610Sstevel@tonic-gate 					goto skiplp;
16620Sstevel@tonic-gate 				}
16630Sstevel@tonic-gate 				if (ap->an_pvp) {
16640Sstevel@tonic-gate 					swap_phys_free(ap->an_pvp,
16650Sstevel@tonic-gate 					    ap->an_poff, PAGESIZE);
16660Sstevel@tonic-gate 					    ap->an_pvp = NULL;
16670Sstevel@tonic-gate 					    ap->an_poff = 0;
16680Sstevel@tonic-gate 				}
16690Sstevel@tonic-gate 				mutex_exit(ahm);
16700Sstevel@tonic-gate 			}
16710Sstevel@tonic-gate 			page_destroy_pages(root_pp);
16720Sstevel@tonic-gate 			segadvstat.MADV_FREE_hit.value.ul += pgcnt;
16730Sstevel@tonic-gate 			anon_array_exit(&cookie);
16740Sstevel@tonic-gate 			continue;
16750Sstevel@tonic-gate 		}
16760Sstevel@tonic-gate skiplp:
16770Sstevel@tonic-gate 		segadvstat.MADV_FREE_miss.value.ul += pgcnt;
1678414Skchow 		for (i = 0, pp = root_pp; i < idx; pp++, i++)
16790Sstevel@tonic-gate 			page_unlock(pp);
16800Sstevel@tonic-gate 		anon_array_exit(&cookie);
16810Sstevel@tonic-gate 	}
16820Sstevel@tonic-gate }
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate /*
16850Sstevel@tonic-gate  * Return the kept page(s) and protections back to the segment driver.
16860Sstevel@tonic-gate  */
16870Sstevel@tonic-gate int
16880Sstevel@tonic-gate anon_getpage(
16890Sstevel@tonic-gate 	struct anon **app,
16900Sstevel@tonic-gate 	uint_t *protp,
16910Sstevel@tonic-gate 	page_t *pl[],
16920Sstevel@tonic-gate 	size_t plsz,
16930Sstevel@tonic-gate 	struct seg *seg,
16940Sstevel@tonic-gate 	caddr_t addr,
16950Sstevel@tonic-gate 	enum seg_rw rw,
16960Sstevel@tonic-gate 	struct cred *cred)
16970Sstevel@tonic-gate {
16980Sstevel@tonic-gate 	page_t *pp;
16990Sstevel@tonic-gate 	struct anon *ap = *app;
17000Sstevel@tonic-gate 	struct vnode *vp;
17010Sstevel@tonic-gate 	anoff_t off;
17020Sstevel@tonic-gate 	int err;
17030Sstevel@tonic-gate 	kmutex_t *ahm;
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	swap_xlate(ap, &vp, &off);
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	/*
17080Sstevel@tonic-gate 	 * Lookup the page. If page is being paged in,
17090Sstevel@tonic-gate 	 * wait for it to finish as we must return a list of
17100Sstevel@tonic-gate 	 * pages since this routine acts like the VOP_GETPAGE
17110Sstevel@tonic-gate 	 * routine does.
17120Sstevel@tonic-gate 	 */
17130Sstevel@tonic-gate 	if (pl != NULL && (pp = page_lookup(vp, (u_offset_t)off, SE_SHARED))) {
17140Sstevel@tonic-gate 		ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
17150Sstevel@tonic-gate 		mutex_enter(ahm);
17160Sstevel@tonic-gate 		if (ap->an_refcnt == 1)
17170Sstevel@tonic-gate 			*protp = PROT_ALL;
17180Sstevel@tonic-gate 		else
17190Sstevel@tonic-gate 			*protp = PROT_ALL & ~PROT_WRITE;
17200Sstevel@tonic-gate 		mutex_exit(ahm);
17210Sstevel@tonic-gate 		pl[0] = pp;
17220Sstevel@tonic-gate 		pl[1] = NULL;
17230Sstevel@tonic-gate 		return (0);
17240Sstevel@tonic-gate 	}
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	/*
17270Sstevel@tonic-gate 	 * Simply treat it as a vnode fault on the anon vp.
17280Sstevel@tonic-gate 	 */
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	TRACE_3(TR_FAC_VM, TR_ANON_GETPAGE,
17310Sstevel@tonic-gate 		"anon_getpage:seg %x addr %x vp %x",
17320Sstevel@tonic-gate 		seg, addr, vp);
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate 	err = VOP_GETPAGE(vp, (u_offset_t)off, PAGESIZE, protp, pl, plsz,
17350Sstevel@tonic-gate 	    seg, addr, rw, cred);
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	if (err == 0 && pl != NULL) {
17380Sstevel@tonic-gate 		ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
17390Sstevel@tonic-gate 		mutex_enter(ahm);
17400Sstevel@tonic-gate 		if (ap->an_refcnt != 1)
17410Sstevel@tonic-gate 			*protp &= ~PROT_WRITE;	/* make read-only */
17420Sstevel@tonic-gate 		mutex_exit(ahm);
17430Sstevel@tonic-gate 	}
17440Sstevel@tonic-gate 	return (err);
17450Sstevel@tonic-gate }
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate /*
17480Sstevel@tonic-gate  * Creates or returns kept pages to the segment driver.  returns -1 if a large
17490Sstevel@tonic-gate  * page cannot be allocated. returns -2 if some other process has allocated a
17500Sstevel@tonic-gate  * larger page.
17510Sstevel@tonic-gate  *
17520Sstevel@tonic-gate  * For cowfault it will alocate any size pages to fill the requested area to
17530Sstevel@tonic-gate  * avoid partially overwritting anon slots (i.e. sharing only some of the anon
17540Sstevel@tonic-gate  * slots within a large page with other processes). This policy greatly
17550Sstevel@tonic-gate  * simplifies large page freeing (which is only freed when all anon slot
17560Sstevel@tonic-gate  * refcnts are 0).
17570Sstevel@tonic-gate  */
17580Sstevel@tonic-gate int
17590Sstevel@tonic-gate anon_map_getpages(
17600Sstevel@tonic-gate 	struct anon_map *amp,
17610Sstevel@tonic-gate 	ulong_t	start_idx,
17620Sstevel@tonic-gate 	uint_t	szc,
17630Sstevel@tonic-gate 	struct seg *seg,
17640Sstevel@tonic-gate 	caddr_t	addr,
17650Sstevel@tonic-gate 	uint_t prot,
17660Sstevel@tonic-gate 	uint_t *protp,
17670Sstevel@tonic-gate 	page_t	*ppa[],
17680Sstevel@tonic-gate 	uint_t	*ppa_szc,
17690Sstevel@tonic-gate 	struct vpage vpage[],
17700Sstevel@tonic-gate 	enum seg_rw rw,
17710Sstevel@tonic-gate 	int brkcow,
17720Sstevel@tonic-gate 	int anypgsz,
17730Sstevel@tonic-gate 	struct cred *cred)
17740Sstevel@tonic-gate {
17750Sstevel@tonic-gate 	pgcnt_t		pgcnt;
17760Sstevel@tonic-gate 	struct anon	*ap;
17770Sstevel@tonic-gate 	struct vnode	*vp;
17780Sstevel@tonic-gate 	anoff_t		off;
17790Sstevel@tonic-gate 	page_t		*pp, *pl[2], *conpp = NULL;
17800Sstevel@tonic-gate 	caddr_t		vaddr;
17810Sstevel@tonic-gate 	ulong_t		pg_idx, an_idx, i;
17820Sstevel@tonic-gate 	spgcnt_t	nreloc = 0;
17830Sstevel@tonic-gate 	int		prealloc = 1;
17840Sstevel@tonic-gate 	int		err, slotcreate;
17850Sstevel@tonic-gate 	uint_t		vpprot;
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
17880Sstevel@tonic-gate 	ASSERT(seg->s_szc != 0);
17890Sstevel@tonic-gate #endif
17900Sstevel@tonic-gate 	ASSERT(szc <= seg->s_szc);
17910Sstevel@tonic-gate 	ASSERT(ppa_szc != NULL);
17920Sstevel@tonic-gate 	ASSERT(rw != S_CREATE);
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	*protp = PROT_ALL;
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 	VM_STAT_ADD(anonvmstats.getpages[0]);
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 	if (szc == 0) {
17990Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.getpages[1]);
18000Sstevel@tonic-gate 		if ((ap = anon_get_ptr(amp->ahp, start_idx)) != NULL) {
18010Sstevel@tonic-gate 			err = anon_getpage(&ap, protp, pl, PAGESIZE, seg,
18020Sstevel@tonic-gate 			    addr, rw, cred);
18030Sstevel@tonic-gate 			if (err)
18040Sstevel@tonic-gate 				return (err);
18050Sstevel@tonic-gate 			ppa[0] = pl[0];
18060Sstevel@tonic-gate 			if (brkcow == 0 || (*protp & PROT_WRITE)) {
18070Sstevel@tonic-gate 				VM_STAT_ADD(anonvmstats.getpages[2]);
18080Sstevel@tonic-gate 				if (ppa[0]->p_szc != 0) {
18090Sstevel@tonic-gate 					VM_STAT_ADD(anonvmstats.getpages[3]);
18100Sstevel@tonic-gate 					*ppa_szc = ppa[0]->p_szc;
18110Sstevel@tonic-gate 					page_unlock(ppa[0]);
18120Sstevel@tonic-gate 					return (-2);
18130Sstevel@tonic-gate 				}
18140Sstevel@tonic-gate 				return (0);
18150Sstevel@tonic-gate 			}
18160Sstevel@tonic-gate 			panic("anon_map_getpages: cowfault for szc 0");
18170Sstevel@tonic-gate 		} else {
18180Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.getpages[4]);
18190Sstevel@tonic-gate 			ppa[0] = anon_zero(seg, addr, &ap, cred);
18200Sstevel@tonic-gate 			if (ppa[0] == NULL)
18210Sstevel@tonic-gate 				return (ENOMEM);
18220Sstevel@tonic-gate 			(void) anon_set_ptr(amp->ahp, start_idx, ap,
18230Sstevel@tonic-gate 			    ANON_SLEEP);
18240Sstevel@tonic-gate 			return (0);
18250Sstevel@tonic-gate 		}
18260Sstevel@tonic-gate 	}
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	pgcnt = page_get_pagecnt(szc);
18290Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
18300Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(start_idx, pgcnt));
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate 	/*
18330Sstevel@tonic-gate 	 * First we check for the case that the requtested large
18340Sstevel@tonic-gate 	 * page or larger page already exists in the system.
18350Sstevel@tonic-gate 	 * Actually we only check if the first constituent page
18360Sstevel@tonic-gate 	 * exists and only preallocate if it's not found.
18370Sstevel@tonic-gate 	 */
18380Sstevel@tonic-gate 	ap = anon_get_ptr(amp->ahp, start_idx);
18390Sstevel@tonic-gate 	if (ap) {
18400Sstevel@tonic-gate 		uint_t pszc;
18410Sstevel@tonic-gate 		swap_xlate(ap, &vp, &off);
18420Sstevel@tonic-gate 		if (page_exists_forreal(vp, (u_offset_t)off, &pszc)) {
18430Sstevel@tonic-gate 			if (pszc > szc) {
18440Sstevel@tonic-gate 				*ppa_szc = pszc;
18450Sstevel@tonic-gate 				return (-2);
18460Sstevel@tonic-gate 			}
18470Sstevel@tonic-gate 			if (pszc == szc) {
18480Sstevel@tonic-gate 				prealloc = 0;
18490Sstevel@tonic-gate 			}
18500Sstevel@tonic-gate 		}
18510Sstevel@tonic-gate 	}
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	VM_STAT_COND_ADD(prealloc == 0, anonvmstats.getpages[5]);
18540Sstevel@tonic-gate 	VM_STAT_COND_ADD(prealloc != 0, anonvmstats.getpages[6]);
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate top:
18570Sstevel@tonic-gate 	/*
18580Sstevel@tonic-gate 	 * If a smaller page or no page at all was found,
18590Sstevel@tonic-gate 	 * grab a large page off the freelist.
18600Sstevel@tonic-gate 	 */
18610Sstevel@tonic-gate 	if (prealloc) {
18620Sstevel@tonic-gate 		ASSERT(conpp == NULL);
18630Sstevel@tonic-gate 		if (page_alloc_pages(seg, addr, NULL, ppa, szc, 0) != 0) {
18640Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.getpages[7]);
18650Sstevel@tonic-gate 			if (brkcow == 0 ||
18660Sstevel@tonic-gate 			    !anon_share(amp->ahp, start_idx, pgcnt)) {
18670Sstevel@tonic-gate 				/*
18680Sstevel@tonic-gate 				 * If the refcnt's of all anon slots are <= 1
18690Sstevel@tonic-gate 				 * they can't increase since we are holding
18700Sstevel@tonic-gate 				 * the address space's lock. So segvn can
18710Sstevel@tonic-gate 				 * safely decrease szc without risking to
18720Sstevel@tonic-gate 				 * generate a cow fault for the region smaller
18730Sstevel@tonic-gate 				 * than the segment's largest page size.
18740Sstevel@tonic-gate 				 */
18750Sstevel@tonic-gate 				VM_STAT_ADD(anonvmstats.getpages[8]);
18760Sstevel@tonic-gate 				return (-1);
18770Sstevel@tonic-gate 			}
18780Sstevel@tonic-gate 		docow:
18790Sstevel@tonic-gate 			/*
18800Sstevel@tonic-gate 			 * This is a cow fault. Copy away the entire 1 large
18810Sstevel@tonic-gate 			 * page region of this segment.
18820Sstevel@tonic-gate 			 */
18830Sstevel@tonic-gate 			if (szc != seg->s_szc)
18840Sstevel@tonic-gate 				panic("anon_map_getpages: cowfault for szc %d",
18850Sstevel@tonic-gate 				    szc);
18860Sstevel@tonic-gate 			vaddr = addr;
18870Sstevel@tonic-gate 			for (pg_idx = 0, an_idx = start_idx; pg_idx < pgcnt;
18880Sstevel@tonic-gate 			    pg_idx++, an_idx++, vaddr += PAGESIZE) {
18890Sstevel@tonic-gate 				if ((ap = anon_get_ptr(amp->ahp, an_idx)) !=
18900Sstevel@tonic-gate 				    NULL) {
18910Sstevel@tonic-gate 					err = anon_getpage(&ap, &vpprot, pl,
18920Sstevel@tonic-gate 					    PAGESIZE, seg, vaddr, rw, cred);
18930Sstevel@tonic-gate 					if (err) {
18940Sstevel@tonic-gate 						for (i = 0; i < pg_idx; i++) {
18950Sstevel@tonic-gate 							if ((pp = ppa[i]) !=
18960Sstevel@tonic-gate 							    NULL)
18970Sstevel@tonic-gate 								page_unlock(pp);
18980Sstevel@tonic-gate 						}
18990Sstevel@tonic-gate 						return (err);
19000Sstevel@tonic-gate 					}
19010Sstevel@tonic-gate 					ppa[pg_idx] = pl[0];
19020Sstevel@tonic-gate 				} else {
19030Sstevel@tonic-gate 					/*
19040Sstevel@tonic-gate 					 * Since this is a cowfault we know
19050Sstevel@tonic-gate 					 * that this address space has a
19060Sstevel@tonic-gate 					 * parent or children which means
19070Sstevel@tonic-gate 					 * anon_dup_fill_holes() has initialized
19080Sstevel@tonic-gate 					 * all anon slots within a large page
19090Sstevel@tonic-gate 					 * region that had at least one anon
19100Sstevel@tonic-gate 					 * slot at the time of fork().
19110Sstevel@tonic-gate 					 */
19120Sstevel@tonic-gate 					panic("anon_map_getpages: "
19130Sstevel@tonic-gate 					    "cowfault but anon slot is empty");
19140Sstevel@tonic-gate 				}
19150Sstevel@tonic-gate 			}
19160Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.getpages[9]);
19170Sstevel@tonic-gate 			*protp = PROT_ALL;
19180Sstevel@tonic-gate 			return (anon_map_privatepages(amp, start_idx, szc, seg,
19190Sstevel@tonic-gate 			    addr, prot, ppa, vpage, anypgsz, cred));
19200Sstevel@tonic-gate 		}
19210Sstevel@tonic-gate 	}
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	VM_STAT_ADD(anonvmstats.getpages[10]);
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 	an_idx = start_idx;
19260Sstevel@tonic-gate 	pg_idx = 0;
19270Sstevel@tonic-gate 	vaddr = addr;
19280Sstevel@tonic-gate 	while (pg_idx < pgcnt) {
19290Sstevel@tonic-gate 		slotcreate = 0;
19300Sstevel@tonic-gate 		if ((ap = anon_get_ptr(amp->ahp, an_idx)) == NULL) {
19310Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.getpages[11]);
19320Sstevel@tonic-gate 			/*
19330Sstevel@tonic-gate 			 * For us to have decided not to preallocate
19340Sstevel@tonic-gate 			 * would have meant that a large page
19350Sstevel@tonic-gate 			 * was found. Which also means that all of the
19360Sstevel@tonic-gate 			 * anon slots for that page would have been
19370Sstevel@tonic-gate 			 * already created for us.
19380Sstevel@tonic-gate 			 */
19390Sstevel@tonic-gate 			if (prealloc == 0)
19400Sstevel@tonic-gate 				panic("anon_map_getpages: prealloc = 0");
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 			slotcreate = 1;
19430Sstevel@tonic-gate 			ap = anon_alloc(NULL, 0);
19440Sstevel@tonic-gate 		}
19450Sstevel@tonic-gate 		swap_xlate(ap, &vp, &off);
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 		/*
19480Sstevel@tonic-gate 		 * Now setup our preallocated page to pass down
19490Sstevel@tonic-gate 		 * to swap_getpage().
19500Sstevel@tonic-gate 		 */
19510Sstevel@tonic-gate 		if (prealloc) {
19520Sstevel@tonic-gate 			ASSERT(ppa[pg_idx]->p_szc == szc);
19530Sstevel@tonic-gate 			conpp = ppa[pg_idx];
19540Sstevel@tonic-gate 		}
19550Sstevel@tonic-gate 		ASSERT(prealloc || conpp == NULL);
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 		/*
19580Sstevel@tonic-gate 		 * If we just created this anon slot then call
19590Sstevel@tonic-gate 		 * with S_CREATE to prevent doing IO on the page.
19600Sstevel@tonic-gate 		 * Similar to the anon_zero case.
19610Sstevel@tonic-gate 		 */
19620Sstevel@tonic-gate 		err = swap_getconpage(vp, (u_offset_t)off, PAGESIZE,
19630Sstevel@tonic-gate 		    NULL, pl, PAGESIZE, conpp, &nreloc, seg, vaddr,
19640Sstevel@tonic-gate 		    slotcreate == 1 ? S_CREATE : rw, cred);
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 		if (err) {
19670Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.getpages[12]);
19680Sstevel@tonic-gate 			ASSERT(slotcreate == 0);
19690Sstevel@tonic-gate 			goto io_err;
19700Sstevel@tonic-gate 		}
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 		pp = pl[0];
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 		if (pp->p_szc != szc) {
19750Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.getpages[13]);
19760Sstevel@tonic-gate 			ASSERT(slotcreate == 0);
19770Sstevel@tonic-gate 			ASSERT(prealloc == 0);
19780Sstevel@tonic-gate 			ASSERT(pg_idx == 0);
19790Sstevel@tonic-gate 			if (pp->p_szc > szc) {
19800Sstevel@tonic-gate 				page_unlock(pp);
19810Sstevel@tonic-gate 				VM_STAT_ADD(anonvmstats.getpages[14]);
19820Sstevel@tonic-gate 				return (-2);
19830Sstevel@tonic-gate 			}
19840Sstevel@tonic-gate 			page_unlock(pp);
19850Sstevel@tonic-gate 			prealloc = 1;
19860Sstevel@tonic-gate 			goto top;
19870Sstevel@tonic-gate 		}
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 		/*
19900Sstevel@tonic-gate 		 * If we decided to preallocate but VOP_GETPAGE
19910Sstevel@tonic-gate 		 * found a page in the system that satisfies our
19920Sstevel@tonic-gate 		 * request then free up our preallocated large page
19930Sstevel@tonic-gate 		 * and continue looping accross the existing large
19940Sstevel@tonic-gate 		 * page via VOP_GETPAGE.
19950Sstevel@tonic-gate 		 */
19960Sstevel@tonic-gate 		if (prealloc && pp != ppa[pg_idx]) {
19970Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.getpages[15]);
19980Sstevel@tonic-gate 			ASSERT(slotcreate == 0);
19990Sstevel@tonic-gate 			ASSERT(pg_idx == 0);
20000Sstevel@tonic-gate 			conpp = NULL;
20010Sstevel@tonic-gate 			prealloc = 0;
20020Sstevel@tonic-gate 			page_free_pages(ppa[0]);
20030Sstevel@tonic-gate 		}
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 		if (prealloc && nreloc > 1) {
20060Sstevel@tonic-gate 			/*
20070Sstevel@tonic-gate 			 * we have relocated out of a smaller large page.
20080Sstevel@tonic-gate 			 * skip npgs - 1 iterations and continue which will
20090Sstevel@tonic-gate 			 * increment by one the loop indices.
20100Sstevel@tonic-gate 			 */
20110Sstevel@tonic-gate 			spgcnt_t npgs = nreloc;
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.getpages[16]);
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 			ASSERT(pp == ppa[pg_idx]);
20160Sstevel@tonic-gate 			ASSERT(slotcreate == 0);
20170Sstevel@tonic-gate 			ASSERT(pg_idx + npgs <= pgcnt);
20180Sstevel@tonic-gate 			if ((*protp & PROT_WRITE) &&
20190Sstevel@tonic-gate 			    anon_share(amp->ahp, an_idx, npgs)) {
20200Sstevel@tonic-gate 			    *protp &= ~PROT_WRITE;
20210Sstevel@tonic-gate 			}
20220Sstevel@tonic-gate 			pg_idx += npgs;
20230Sstevel@tonic-gate 			an_idx += npgs;
20240Sstevel@tonic-gate 			vaddr += PAGESIZE * npgs;
20250Sstevel@tonic-gate 			continue;
20260Sstevel@tonic-gate 		}
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.getpages[17]);
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 		/*
20310Sstevel@tonic-gate 		 * Anon_zero case.
20320Sstevel@tonic-gate 		 */
20330Sstevel@tonic-gate 		if (slotcreate) {
20340Sstevel@tonic-gate 			ASSERT(prealloc);
20350Sstevel@tonic-gate 			pagezero(pp, 0, PAGESIZE);
20360Sstevel@tonic-gate 			CPU_STATS_ADD_K(vm, zfod, 1);
20370Sstevel@tonic-gate 			hat_setrefmod(pp);
20380Sstevel@tonic-gate 		}
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 		ASSERT(prealloc == 0 || ppa[pg_idx] == pp);
20410Sstevel@tonic-gate 		ASSERT(prealloc != 0 || PAGE_SHARED(pp));
20420Sstevel@tonic-gate 		ASSERT(prealloc == 0 || PAGE_EXCL(pp));
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 		if (pg_idx > 0 &&
20450Sstevel@tonic-gate 		    ((page_pptonum(pp) != page_pptonum(ppa[pg_idx - 1]) + 1) ||
20460Sstevel@tonic-gate 		    (pp->p_szc != ppa[pg_idx - 1]->p_szc)))
20470Sstevel@tonic-gate 			panic("anon_map_getpages: unexpected page");
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 		if (prealloc == 0) {
20500Sstevel@tonic-gate 			ppa[pg_idx] = pp;
20510Sstevel@tonic-gate 		}
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 		if (ap->an_refcnt > 1) {
20540Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.getpages[18]);
20550Sstevel@tonic-gate 			*protp &= ~PROT_WRITE;
20560Sstevel@tonic-gate 		}
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 		/*
20590Sstevel@tonic-gate 		 * If this is a new anon slot then initialize
20600Sstevel@tonic-gate 		 * the anon array entry.
20610Sstevel@tonic-gate 		 */
20620Sstevel@tonic-gate 		if (slotcreate) {
20630Sstevel@tonic-gate 			(void) anon_set_ptr(amp->ahp, an_idx, ap, ANON_SLEEP);
20640Sstevel@tonic-gate 		}
20650Sstevel@tonic-gate 		pg_idx++;
20660Sstevel@tonic-gate 		an_idx++;
20670Sstevel@tonic-gate 		vaddr += PAGESIZE;
20680Sstevel@tonic-gate 	}
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 	/*
20710Sstevel@tonic-gate 	 * Since preallocated pages come off the freelist
20720Sstevel@tonic-gate 	 * they are locked SE_EXCL. Simply downgrade and return.
20730Sstevel@tonic-gate 	 */
20740Sstevel@tonic-gate 	if (prealloc) {
20750Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.getpages[19]);
20760Sstevel@tonic-gate 		conpp = NULL;
20770Sstevel@tonic-gate 		for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) {
20780Sstevel@tonic-gate 			page_downgrade(ppa[pg_idx]);
20790Sstevel@tonic-gate 		}
20800Sstevel@tonic-gate 	}
20810Sstevel@tonic-gate 	ASSERT(conpp == NULL);
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 	if (brkcow == 0 || (*protp & PROT_WRITE)) {
20840Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.getpages[20]);
20850Sstevel@tonic-gate 		return (0);
20860Sstevel@tonic-gate 	}
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 	if (szc < seg->s_szc)
20890Sstevel@tonic-gate 		panic("anon_map_getpages: cowfault for szc %d", szc);
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 	VM_STAT_ADD(anonvmstats.getpages[21]);
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 	*protp = PROT_ALL;
20940Sstevel@tonic-gate 	return (anon_map_privatepages(amp, start_idx, szc, seg, addr, prot,
20950Sstevel@tonic-gate 	    ppa, vpage, anypgsz, cred));
20960Sstevel@tonic-gate io_err:
20970Sstevel@tonic-gate 	/*
20980Sstevel@tonic-gate 	 * We got an IO error somewhere in our large page.
20990Sstevel@tonic-gate 	 * If we were using a preallocated page then just demote
21000Sstevel@tonic-gate 	 * all the constituent pages that we've succeeded with sofar
21010Sstevel@tonic-gate 	 * to PAGESIZE pages and leave them in the system
21020Sstevel@tonic-gate 	 * unlocked.
21030Sstevel@tonic-gate 	 */
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	ASSERT(err != -2 || pg_idx == 0);
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 	VM_STAT_COND_ADD(err > 0, anonvmstats.getpages[22]);
21080Sstevel@tonic-gate 	VM_STAT_COND_ADD(err == -1, anonvmstats.getpages[23]);
21090Sstevel@tonic-gate 	VM_STAT_COND_ADD(err == -2, anonvmstats.getpages[24]);
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 	if (prealloc) {
21120Sstevel@tonic-gate 		conpp = NULL;
21130Sstevel@tonic-gate 		if (pg_idx > 0) {
21140Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.getpages[25]);
21150Sstevel@tonic-gate 			for (i = 0; i < pgcnt; i++) {
21160Sstevel@tonic-gate 				pp = ppa[i];
21170Sstevel@tonic-gate 				ASSERT(PAGE_EXCL(pp));
21180Sstevel@tonic-gate 				ASSERT(pp->p_szc == szc);
21190Sstevel@tonic-gate 				pp->p_szc = 0;
21200Sstevel@tonic-gate 			}
21210Sstevel@tonic-gate 			for (i = 0; i < pg_idx; i++) {
21220Sstevel@tonic-gate 				ASSERT(!hat_page_is_mapped(ppa[i]));
21230Sstevel@tonic-gate 				page_unlock(ppa[i]);
21240Sstevel@tonic-gate 			}
21250Sstevel@tonic-gate 			/*
21260Sstevel@tonic-gate 			 * Now free up the remaining unused constituent
21270Sstevel@tonic-gate 			 * pages.
21280Sstevel@tonic-gate 			 */
21290Sstevel@tonic-gate 			while (pg_idx < pgcnt) {
21300Sstevel@tonic-gate 				ASSERT(!hat_page_is_mapped(ppa[pg_idx]));
21310Sstevel@tonic-gate 				page_free(ppa[pg_idx], 0);
21320Sstevel@tonic-gate 				pg_idx++;
21330Sstevel@tonic-gate 			}
21340Sstevel@tonic-gate 		} else {
21350Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.getpages[26]);
21360Sstevel@tonic-gate 			page_free_pages(ppa[0]);
21370Sstevel@tonic-gate 		}
21380Sstevel@tonic-gate 	} else {
21390Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.getpages[27]);
21400Sstevel@tonic-gate 		ASSERT(err > 0);
21410Sstevel@tonic-gate 		for (i = 0; i < pg_idx; i++)
21420Sstevel@tonic-gate 			page_unlock(ppa[i]);
21430Sstevel@tonic-gate 	}
21440Sstevel@tonic-gate 	ASSERT(conpp == NULL);
21450Sstevel@tonic-gate 	if (err != -1)
21460Sstevel@tonic-gate 		return (err);
21470Sstevel@tonic-gate 	/*
21480Sstevel@tonic-gate 	 * we are here because we failed to relocate.
21490Sstevel@tonic-gate 	 */
21500Sstevel@tonic-gate 	ASSERT(prealloc);
21510Sstevel@tonic-gate 	if (brkcow == 0 || !anon_share(amp->ahp, start_idx, pgcnt)) {
21520Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.getpages[28]);
21530Sstevel@tonic-gate 		return (-1);
21540Sstevel@tonic-gate 	}
21550Sstevel@tonic-gate 	VM_STAT_ADD(anonvmstats.getpages[29]);
21560Sstevel@tonic-gate 	goto docow;
21570Sstevel@tonic-gate }
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate /*
21610Sstevel@tonic-gate  * Turn a reference to an object or shared anon page
21620Sstevel@tonic-gate  * into a private page with a copy of the data from the
21630Sstevel@tonic-gate  * original page which is always locked by the caller.
21640Sstevel@tonic-gate  * This routine unloads the translation and unlocks the
21650Sstevel@tonic-gate  * original page, if it isn't being stolen, before returning
21660Sstevel@tonic-gate  * to the caller.
21670Sstevel@tonic-gate  *
21680Sstevel@tonic-gate  * NOTE:  The original anon slot is not freed by this routine
21690Sstevel@tonic-gate  *	  It must be freed by the caller while holding the
21700Sstevel@tonic-gate  *	  "anon_map" lock to prevent races which can occur if
21710Sstevel@tonic-gate  *	  a process has multiple lwps in its address space.
21720Sstevel@tonic-gate  */
21730Sstevel@tonic-gate page_t *
21740Sstevel@tonic-gate anon_private(
21750Sstevel@tonic-gate 	struct anon **app,
21760Sstevel@tonic-gate 	struct seg *seg,
21770Sstevel@tonic-gate 	caddr_t addr,
21780Sstevel@tonic-gate 	uint_t	prot,
21790Sstevel@tonic-gate 	page_t *opp,
21800Sstevel@tonic-gate 	int oppflags,
21810Sstevel@tonic-gate 	struct cred *cred)
21820Sstevel@tonic-gate {
21830Sstevel@tonic-gate 	struct anon *old = *app;
21840Sstevel@tonic-gate 	struct anon *new;
21850Sstevel@tonic-gate 	page_t *pp = NULL;
21860Sstevel@tonic-gate 	struct vnode *vp;
21870Sstevel@tonic-gate 	anoff_t off;
21880Sstevel@tonic-gate 	page_t *anon_pl[1 + 1];
21890Sstevel@tonic-gate 	int err;
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	if (oppflags & STEAL_PAGE)
21920Sstevel@tonic-gate 		ASSERT(PAGE_EXCL(opp));
21930Sstevel@tonic-gate 	else
21940Sstevel@tonic-gate 		ASSERT(PAGE_LOCKED(opp));
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	CPU_STATS_ADD_K(vm, cow_fault, 1);
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 	/* Kernel probe */
21990Sstevel@tonic-gate 	TNF_PROBE_1(anon_private, "vm pagefault", /* CSTYLED */,
22000Sstevel@tonic-gate 		tnf_opaque,	address,	addr);
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	*app = new = anon_alloc(NULL, 0);
22030Sstevel@tonic-gate 	swap_xlate(new, &vp, &off);
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	if (oppflags & STEAL_PAGE) {
22060Sstevel@tonic-gate 		page_rename(opp, vp, (u_offset_t)off);
22070Sstevel@tonic-gate 		pp = opp;
22080Sstevel@tonic-gate 		TRACE_5(TR_FAC_VM, TR_ANON_PRIVATE,
22090Sstevel@tonic-gate 			"anon_private:seg %p addr %x pp %p vp %p off %lx",
22100Sstevel@tonic-gate 			seg, addr, pp, vp, off);
22110Sstevel@tonic-gate 		hat_setmod(pp);
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 		/* bug 4026339 */
22140Sstevel@tonic-gate 		page_downgrade(pp);
22150Sstevel@tonic-gate 		return (pp);
22160Sstevel@tonic-gate 	}
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate 	/*
22190Sstevel@tonic-gate 	 * Call the VOP_GETPAGE routine to create the page, thereby
22200Sstevel@tonic-gate 	 * enabling the vnode driver to allocate any filesystem
22210Sstevel@tonic-gate 	 * space (e.g., disk block allocation for UFS).  This also
22220Sstevel@tonic-gate 	 * prevents more than one page from being added to the
22230Sstevel@tonic-gate 	 * vnode at the same time.
22240Sstevel@tonic-gate 	 */
22250Sstevel@tonic-gate 	err = VOP_GETPAGE(vp, (u_offset_t)off, PAGESIZE, NULL,
22260Sstevel@tonic-gate 	    anon_pl, PAGESIZE, seg, addr, S_CREATE, cred);
22270Sstevel@tonic-gate 	if (err)
22280Sstevel@tonic-gate 		goto out;
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 	pp = anon_pl[0];
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 	/*
22330Sstevel@tonic-gate 	 * If the original page was locked, we need to move the lock
22340Sstevel@tonic-gate 	 * to the new page by transfering 'cowcnt/lckcnt' of the original
22350Sstevel@tonic-gate 	 * page to 'cowcnt/lckcnt' of the new page.
22360Sstevel@tonic-gate 	 *
22370Sstevel@tonic-gate 	 * See Statement at the beginning of segvn_lockop() and
22380Sstevel@tonic-gate 	 * comments in page_pp_useclaim() regarding the way
22390Sstevel@tonic-gate 	 * cowcnts/lckcnts are handled.
22400Sstevel@tonic-gate 	 *
22410Sstevel@tonic-gate 	 * Also availrmem must be decremented up front for read only mapping
22420Sstevel@tonic-gate 	 * before calling page_pp_useclaim. page_pp_useclaim will bump it back
22430Sstevel@tonic-gate 	 * if availrmem did not need to be decremented after all.
22440Sstevel@tonic-gate 	 */
22450Sstevel@tonic-gate 	if (oppflags & LOCK_PAGE) {
22460Sstevel@tonic-gate 		if ((prot & PROT_WRITE) == 0) {
22470Sstevel@tonic-gate 			mutex_enter(&freemem_lock);
22480Sstevel@tonic-gate 			if (availrmem > pages_pp_maximum) {
22490Sstevel@tonic-gate 				availrmem--;
22500Sstevel@tonic-gate 				pages_useclaim++;
22510Sstevel@tonic-gate 			} else {
22520Sstevel@tonic-gate 				mutex_exit(&freemem_lock);
22530Sstevel@tonic-gate 				goto out;
22540Sstevel@tonic-gate 			}
22550Sstevel@tonic-gate 			mutex_exit(&freemem_lock);
22560Sstevel@tonic-gate 		}
22570Sstevel@tonic-gate 		page_pp_useclaim(opp, pp, prot & PROT_WRITE);
22580Sstevel@tonic-gate 	}
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 	/*
22610Sstevel@tonic-gate 	 * Now copy the contents from the original page,
22620Sstevel@tonic-gate 	 * which is locked and loaded in the MMU by
22630Sstevel@tonic-gate 	 * the caller to prevent yet another page fault.
22640Sstevel@tonic-gate 	 */
22650Sstevel@tonic-gate 	ppcopy(opp, pp);		/* XXX - should set mod bit in here */
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	hat_setrefmod(pp);		/* mark as modified */
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 	/*
22700Sstevel@tonic-gate 	 * Unload the old translation.
22710Sstevel@tonic-gate 	 */
22720Sstevel@tonic-gate 	hat_unload(seg->s_as->a_hat, addr, PAGESIZE, HAT_UNLOAD);
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 	/*
22750Sstevel@tonic-gate 	 * Free unmapped, unmodified original page.
22760Sstevel@tonic-gate 	 * or release the lock on the original page,
22770Sstevel@tonic-gate 	 * otherwise the process will sleep forever in
22780Sstevel@tonic-gate 	 * anon_decref() waiting for the "exclusive" lock
22790Sstevel@tonic-gate 	 * on the page.
22800Sstevel@tonic-gate 	 */
22810Sstevel@tonic-gate 	(void) page_release(opp, 1);
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate 	/*
22840Sstevel@tonic-gate 	 * we are done with page creation so downgrade the new
22850Sstevel@tonic-gate 	 * page's selock to shared, this helps when multiple
22860Sstevel@tonic-gate 	 * as_fault(...SOFTLOCK...) are done to the same
22870Sstevel@tonic-gate 	 * page(aio)
22880Sstevel@tonic-gate 	 */
22890Sstevel@tonic-gate 	page_downgrade(pp);
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 	/*
22920Sstevel@tonic-gate 	 * NOTE:  The original anon slot must be freed by the
22930Sstevel@tonic-gate 	 * caller while holding the "anon_map" lock, if we
22940Sstevel@tonic-gate 	 * copied away from an anonymous page.
22950Sstevel@tonic-gate 	 */
22960Sstevel@tonic-gate 	return (pp);
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate out:
22990Sstevel@tonic-gate 	*app = old;
23000Sstevel@tonic-gate 	if (pp)
23010Sstevel@tonic-gate 		page_unlock(pp);
23020Sstevel@tonic-gate 	anon_decref(new);
23030Sstevel@tonic-gate 	page_unlock(opp);
23040Sstevel@tonic-gate 	return ((page_t *)NULL);
23050Sstevel@tonic-gate }
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate int
23080Sstevel@tonic-gate anon_map_privatepages(
23090Sstevel@tonic-gate 	struct anon_map *amp,
23100Sstevel@tonic-gate 	ulong_t	start_idx,
23110Sstevel@tonic-gate 	uint_t	szc,
23120Sstevel@tonic-gate 	struct seg *seg,
23130Sstevel@tonic-gate 	caddr_t addr,
23140Sstevel@tonic-gate 	uint_t	prot,
23150Sstevel@tonic-gate 	page_t	*ppa[],
23160Sstevel@tonic-gate 	struct vpage vpage[],
23170Sstevel@tonic-gate 	int anypgsz,
23180Sstevel@tonic-gate 	struct cred *cred)
23190Sstevel@tonic-gate {
23200Sstevel@tonic-gate 	pgcnt_t		pgcnt;
23210Sstevel@tonic-gate 	struct vnode	*vp;
23220Sstevel@tonic-gate 	anoff_t		off;
23230Sstevel@tonic-gate 	page_t		*pl[2], *conpp = NULL;
23240Sstevel@tonic-gate 	int		err;
23250Sstevel@tonic-gate 	int		prealloc = 1;
23260Sstevel@tonic-gate 	struct anon	*ap, *oldap;
23270Sstevel@tonic-gate 	caddr_t		vaddr;
23280Sstevel@tonic-gate 	page_t		*pplist, *pp;
23290Sstevel@tonic-gate 	ulong_t		pg_idx, an_idx;
23300Sstevel@tonic-gate 	spgcnt_t	nreloc = 0;
23310Sstevel@tonic-gate 	int		pagelock = 0;
23320Sstevel@tonic-gate 	kmutex_t	*ahmpages = NULL;
23330Sstevel@tonic-gate #ifdef DEBUG
23340Sstevel@tonic-gate 	int		refcnt;
23350Sstevel@tonic-gate #endif
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate 	ASSERT(szc != 0);
23380Sstevel@tonic-gate 	ASSERT(szc == seg->s_szc);
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate 	VM_STAT_ADD(anonvmstats.privatepages[0]);
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate 	pgcnt = page_get_pagecnt(szc);
23430Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
23440Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(start_idx, pgcnt));
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 	ASSERT(amp != NULL);
23470Sstevel@tonic-gate 	ap = anon_get_ptr(amp->ahp, start_idx);
23480Sstevel@tonic-gate 	ASSERT(ap == NULL || ap->an_refcnt >= 1);
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	VM_STAT_COND_ADD(ap == NULL, anonvmstats.privatepages[1]);
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 	/*
23530Sstevel@tonic-gate 	 * Now try and allocate the large page. If we fail then just
23540Sstevel@tonic-gate 	 * let VOP_GETPAGE give us PAGESIZE pages. Normally we let
23550Sstevel@tonic-gate 	 * the caller make this decision but to avoid added complexity
23560Sstevel@tonic-gate 	 * it's simplier to handle that case here.
23570Sstevel@tonic-gate 	 */
23580Sstevel@tonic-gate 	if (anypgsz == -1) {
23590Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.privatepages[2]);
23600Sstevel@tonic-gate 		prealloc = 0;
23610Sstevel@tonic-gate 	} else if (page_alloc_pages(seg, addr, &pplist, NULL, szc,
23620Sstevel@tonic-gate 	    anypgsz) != 0) {
23630Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.privatepages[3]);
23640Sstevel@tonic-gate 		prealloc = 0;
23650Sstevel@tonic-gate 	}
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 	/*
23680Sstevel@tonic-gate 	 * make the decrement of all refcnts of all
23690Sstevel@tonic-gate 	 * anon slots of a large page appear atomic by
23700Sstevel@tonic-gate 	 * getting an anonpages_hash_lock for the
23710Sstevel@tonic-gate 	 * first anon slot of a large page.
23720Sstevel@tonic-gate 	 */
23730Sstevel@tonic-gate 	if (ap != NULL) {
23740Sstevel@tonic-gate 		ahmpages = &anonpages_hash_lock[AH_LOCK(ap->an_vp,
23750Sstevel@tonic-gate 		    ap->an_off)];
23760Sstevel@tonic-gate 		mutex_enter(ahmpages);
23770Sstevel@tonic-gate 		if (ap->an_refcnt == 1) {
23780Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.privatepages[4]);
23790Sstevel@tonic-gate 			ASSERT(!anon_share(amp->ahp, start_idx, pgcnt));
23800Sstevel@tonic-gate 			mutex_exit(ahmpages);
23810Sstevel@tonic-gate 
23820Sstevel@tonic-gate 			if (prealloc) {
23830Sstevel@tonic-gate 				page_free_replacement_page(pplist);
23840Sstevel@tonic-gate 				page_create_putback(pgcnt);
23850Sstevel@tonic-gate 			}
23860Sstevel@tonic-gate 			ASSERT(ppa[0]->p_szc <= szc);
23870Sstevel@tonic-gate 			if (ppa[0]->p_szc == szc) {
23880Sstevel@tonic-gate 				VM_STAT_ADD(anonvmstats.privatepages[5]);
23890Sstevel@tonic-gate 				return (0);
23900Sstevel@tonic-gate 			}
23910Sstevel@tonic-gate 			for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) {
23920Sstevel@tonic-gate 				ASSERT(ppa[pg_idx] != NULL);
23930Sstevel@tonic-gate 				page_unlock(ppa[pg_idx]);
23940Sstevel@tonic-gate 			}
23950Sstevel@tonic-gate 			return (-1);
23960Sstevel@tonic-gate 		}
23970Sstevel@tonic-gate 	}
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	/*
24000Sstevel@tonic-gate 	 * If we are passed in the vpage array and this is
24010Sstevel@tonic-gate 	 * not PROT_WRITE then we need to decrement availrmem
24020Sstevel@tonic-gate 	 * up front before we try anything. If we need to and
24030Sstevel@tonic-gate 	 * can't decrement availrmem then its better to fail now
24040Sstevel@tonic-gate 	 * than in the middle of processing the new large page.
24050Sstevel@tonic-gate 	 * page_pp_usclaim() on behalf of each constituent page
24060Sstevel@tonic-gate 	 * below will adjust availrmem back for the cases not needed.
24070Sstevel@tonic-gate 	 */
24080Sstevel@tonic-gate 	if (vpage != NULL && (prot & PROT_WRITE) == 0) {
24090Sstevel@tonic-gate 		for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) {
24100Sstevel@tonic-gate 			if (VPP_ISPPLOCK(&vpage[pg_idx])) {
24110Sstevel@tonic-gate 				pagelock = 1;
24120Sstevel@tonic-gate 				break;
24130Sstevel@tonic-gate 			}
24140Sstevel@tonic-gate 		}
24150Sstevel@tonic-gate 		if (pagelock) {
24160Sstevel@tonic-gate 			VM_STAT_ADD(anonvmstats.privatepages[6]);
24170Sstevel@tonic-gate 			mutex_enter(&freemem_lock);
24180Sstevel@tonic-gate 			if (availrmem >= pages_pp_maximum + pgcnt) {
24190Sstevel@tonic-gate 				availrmem -= pgcnt;
24200Sstevel@tonic-gate 				pages_useclaim += pgcnt;
24210Sstevel@tonic-gate 			} else {
24220Sstevel@tonic-gate 				VM_STAT_ADD(anonvmstats.privatepages[7]);
24230Sstevel@tonic-gate 				mutex_exit(&freemem_lock);
24240Sstevel@tonic-gate 				if (ahmpages != NULL) {
24250Sstevel@tonic-gate 					mutex_exit(ahmpages);
24260Sstevel@tonic-gate 				}
24270Sstevel@tonic-gate 				if (prealloc) {
24280Sstevel@tonic-gate 					page_free_replacement_page(pplist);
24290Sstevel@tonic-gate 					page_create_putback(pgcnt);
24300Sstevel@tonic-gate 				}
24310Sstevel@tonic-gate 				for (pg_idx = 0; pg_idx < pgcnt; pg_idx++)
24320Sstevel@tonic-gate 					if (ppa[pg_idx] != NULL)
24330Sstevel@tonic-gate 						page_unlock(ppa[pg_idx]);
24340Sstevel@tonic-gate 				return (ENOMEM);
24350Sstevel@tonic-gate 			}
24360Sstevel@tonic-gate 			mutex_exit(&freemem_lock);
24370Sstevel@tonic-gate 		}
24380Sstevel@tonic-gate 	}
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate 	CPU_STATS_ADD_K(vm, cow_fault, pgcnt);
24410Sstevel@tonic-gate 
24420Sstevel@tonic-gate 	VM_STAT_ADD(anonvmstats.privatepages[8]);
24430Sstevel@tonic-gate 
24440Sstevel@tonic-gate 	an_idx = start_idx;
24450Sstevel@tonic-gate 	pg_idx = 0;
24460Sstevel@tonic-gate 	vaddr = addr;
24470Sstevel@tonic-gate 	for (; pg_idx < pgcnt; pg_idx++, an_idx++, vaddr += PAGESIZE) {
24480Sstevel@tonic-gate 		ASSERT(ppa[pg_idx] != NULL);
24490Sstevel@tonic-gate 		oldap = anon_get_ptr(amp->ahp, an_idx);
24500Sstevel@tonic-gate 		ASSERT(ahmpages != NULL || oldap == NULL);
24510Sstevel@tonic-gate 		ASSERT(ahmpages == NULL || oldap != NULL);
24520Sstevel@tonic-gate 		ASSERT(ahmpages == NULL || oldap->an_refcnt > 1);
24530Sstevel@tonic-gate 		ASSERT(ahmpages == NULL || pg_idx != 0 ||
24540Sstevel@tonic-gate 		    (refcnt = oldap->an_refcnt));
24550Sstevel@tonic-gate 		ASSERT(ahmpages == NULL || pg_idx == 0 ||
24560Sstevel@tonic-gate 		    refcnt == oldap->an_refcnt);
24570Sstevel@tonic-gate 
24580Sstevel@tonic-gate 		ap = anon_alloc(NULL, 0);
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 		swap_xlate(ap, &vp, &off);
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 		/*
24630Sstevel@tonic-gate 		 * Now setup our preallocated page to pass down to
24640Sstevel@tonic-gate 		 * swap_getpage().
24650Sstevel@tonic-gate 		 */
24660Sstevel@tonic-gate 		if (prealloc) {
24670Sstevel@tonic-gate 			pp = pplist;
24680Sstevel@tonic-gate 			page_sub(&pplist, pp);
24690Sstevel@tonic-gate 			conpp = pp;
24700Sstevel@tonic-gate 		}
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 		err = swap_getconpage(vp, (u_offset_t)off, PAGESIZE, NULL, pl,
24730Sstevel@tonic-gate 			PAGESIZE, conpp, &nreloc, seg, vaddr, S_CREATE, cred);
24740Sstevel@tonic-gate 
24750Sstevel@tonic-gate 		/*
24760Sstevel@tonic-gate 		 * Impossible to fail this is S_CREATE.
24770Sstevel@tonic-gate 		 */
24780Sstevel@tonic-gate 		if (err)
24790Sstevel@tonic-gate 			panic("anon_map_privatepages: VOP_GETPAGE failed");
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 		ASSERT(prealloc ? pp == pl[0] : pl[0]->p_szc == 0);
24820Sstevel@tonic-gate 		ASSERT(prealloc == 0 || nreloc == 1);
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate 		pp = pl[0];
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 		/*
24870Sstevel@tonic-gate 		 * If the original page was locked, we need to move
24880Sstevel@tonic-gate 		 * the lock to the new page by transfering
24890Sstevel@tonic-gate 		 * 'cowcnt/lckcnt' of the original page to 'cowcnt/lckcnt'
24900Sstevel@tonic-gate 		 * of the new page. pg_idx can be used to index
24910Sstevel@tonic-gate 		 * into the vpage array since the caller will guarentee
24920Sstevel@tonic-gate 		 * that vpage struct passed in corresponds to addr
24930Sstevel@tonic-gate 		 * and forward.
24940Sstevel@tonic-gate 		 */
24950Sstevel@tonic-gate 		if (vpage != NULL && VPP_ISPPLOCK(&vpage[pg_idx])) {
24960Sstevel@tonic-gate 			page_pp_useclaim(ppa[pg_idx], pp, prot & PROT_WRITE);
24970Sstevel@tonic-gate 		} else if (pagelock) {
24980Sstevel@tonic-gate 			mutex_enter(&freemem_lock);
24990Sstevel@tonic-gate 			availrmem++;
25000Sstevel@tonic-gate 			pages_useclaim--;
25010Sstevel@tonic-gate 			mutex_exit(&freemem_lock);
25020Sstevel@tonic-gate 		}
25030Sstevel@tonic-gate 
25040Sstevel@tonic-gate 		/*
25050Sstevel@tonic-gate 		 * Now copy the contents from the original page.
25060Sstevel@tonic-gate 		 */
25070Sstevel@tonic-gate 		ppcopy(ppa[pg_idx], pp);
25080Sstevel@tonic-gate 
25090Sstevel@tonic-gate 		hat_setrefmod(pp);		/* mark as modified */
25100Sstevel@tonic-gate 
25110Sstevel@tonic-gate 		/*
25120Sstevel@tonic-gate 		 * Release the lock on the original page,
25130Sstevel@tonic-gate 		 * derement the old slot, and down grade the lock
25140Sstevel@tonic-gate 		 * on the new copy.
25150Sstevel@tonic-gate 		 */
25160Sstevel@tonic-gate 		page_unlock(ppa[pg_idx]);
25170Sstevel@tonic-gate 
25180Sstevel@tonic-gate 		if (!prealloc)
25190Sstevel@tonic-gate 			page_downgrade(pp);
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 		ppa[pg_idx] = pp;
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 		/*
25240Sstevel@tonic-gate 		 * Now reflect the copy in the new anon array.
25250Sstevel@tonic-gate 		 */
25260Sstevel@tonic-gate 		ASSERT(ahmpages == NULL || oldap->an_refcnt > 1);
25270Sstevel@tonic-gate 		if (oldap != NULL)
25280Sstevel@tonic-gate 			anon_decref(oldap);
25290Sstevel@tonic-gate 		(void) anon_set_ptr(amp->ahp, an_idx, ap, ANON_SLEEP);
25300Sstevel@tonic-gate 	}
25310Sstevel@tonic-gate 	if (ahmpages != NULL) {
25320Sstevel@tonic-gate 		mutex_exit(ahmpages);
25330Sstevel@tonic-gate 	}
25340Sstevel@tonic-gate 	ASSERT(prealloc == 0 || pplist == NULL);
25350Sstevel@tonic-gate 	if (prealloc) {
25360Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.privatepages[9]);
25370Sstevel@tonic-gate 		for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) {
25380Sstevel@tonic-gate 			page_downgrade(ppa[pg_idx]);
25390Sstevel@tonic-gate 		}
25400Sstevel@tonic-gate 	}
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 	/*
25430Sstevel@tonic-gate 	 * Unload the old large page translation.
25440Sstevel@tonic-gate 	 */
25450Sstevel@tonic-gate 	hat_unload(seg->s_as->a_hat, addr, pgcnt << PAGESHIFT, HAT_UNLOAD);
25460Sstevel@tonic-gate 	return (0);
25470Sstevel@tonic-gate }
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate /*
25500Sstevel@tonic-gate  * Allocate a private zero-filled anon page.
25510Sstevel@tonic-gate  */
25520Sstevel@tonic-gate page_t *
25530Sstevel@tonic-gate anon_zero(struct seg *seg, caddr_t addr, struct anon **app, struct cred *cred)
25540Sstevel@tonic-gate {
25550Sstevel@tonic-gate 	struct anon *ap;
25560Sstevel@tonic-gate 	page_t *pp;
25570Sstevel@tonic-gate 	struct vnode *vp;
25580Sstevel@tonic-gate 	anoff_t off;
25590Sstevel@tonic-gate 	page_t *anon_pl[1 + 1];
25600Sstevel@tonic-gate 	int err;
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 	/* Kernel probe */
25630Sstevel@tonic-gate 	TNF_PROBE_1(anon_zero, "vm pagefault", /* CSTYLED */,
25640Sstevel@tonic-gate 		tnf_opaque,	address,	addr);
25650Sstevel@tonic-gate 
25660Sstevel@tonic-gate 	*app = ap = anon_alloc(NULL, 0);
25670Sstevel@tonic-gate 	swap_xlate(ap, &vp, &off);
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 	/*
25700Sstevel@tonic-gate 	 * Call the VOP_GETPAGE routine to create the page, thereby
25710Sstevel@tonic-gate 	 * enabling the vnode driver to allocate any filesystem
25720Sstevel@tonic-gate 	 * dependent structures (e.g., disk block allocation for UFS).
25730Sstevel@tonic-gate 	 * This also prevents more than on page from being added to
25740Sstevel@tonic-gate 	 * the vnode at the same time since it is locked.
25750Sstevel@tonic-gate 	 */
25760Sstevel@tonic-gate 	err = VOP_GETPAGE(vp, off, PAGESIZE, NULL,
25770Sstevel@tonic-gate 	    anon_pl, PAGESIZE, seg, addr, S_CREATE, cred);
25780Sstevel@tonic-gate 	if (err) {
25790Sstevel@tonic-gate 		*app = NULL;
25800Sstevel@tonic-gate 		anon_decref(ap);
25810Sstevel@tonic-gate 		return (NULL);
25820Sstevel@tonic-gate 	}
25830Sstevel@tonic-gate 	pp = anon_pl[0];
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 	pagezero(pp, 0, PAGESIZE);	/* XXX - should set mod bit */
25860Sstevel@tonic-gate 	page_downgrade(pp);
25870Sstevel@tonic-gate 	CPU_STATS_ADD_K(vm, zfod, 1);
25880Sstevel@tonic-gate 	hat_setrefmod(pp);	/* mark as modified so pageout writes back */
25890Sstevel@tonic-gate 	return (pp);
25900Sstevel@tonic-gate }
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate /*
25940Sstevel@tonic-gate  * Allocate array of private zero-filled anon pages for empty slots
25950Sstevel@tonic-gate  * and kept pages for non empty slots within given range.
25960Sstevel@tonic-gate  *
25970Sstevel@tonic-gate  * NOTE: This rontine will try and use large pages
25980Sstevel@tonic-gate  *	if available and supported by underlying platform.
25990Sstevel@tonic-gate  */
26000Sstevel@tonic-gate int
26010Sstevel@tonic-gate anon_map_createpages(
26020Sstevel@tonic-gate 	struct anon_map *amp,
26030Sstevel@tonic-gate 	ulong_t start_index,
26040Sstevel@tonic-gate 	size_t len,
26050Sstevel@tonic-gate 	page_t *ppa[],
26060Sstevel@tonic-gate 	struct seg *seg,
26070Sstevel@tonic-gate 	caddr_t addr,
26080Sstevel@tonic-gate 	enum seg_rw rw,
26090Sstevel@tonic-gate 	struct cred *cred)
26100Sstevel@tonic-gate {
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate 	struct anon	*ap;
26130Sstevel@tonic-gate 	struct vnode	*ap_vp;
26140Sstevel@tonic-gate 	page_t		*pp, *pplist, *anon_pl[1 + 1], *conpp = NULL;
26150Sstevel@tonic-gate 	int		err = 0;
26160Sstevel@tonic-gate 	ulong_t		p_index, index;
26170Sstevel@tonic-gate 	pgcnt_t		npgs, pg_cnt;
26180Sstevel@tonic-gate 	spgcnt_t	nreloc = 0;
26190Sstevel@tonic-gate 	uint_t		l_szc, szc, prot;
26200Sstevel@tonic-gate 	anoff_t		ap_off;
26210Sstevel@tonic-gate 	size_t		pgsz;
26220Sstevel@tonic-gate 	lgrp_t		*lgrp;
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 	/*
26250Sstevel@tonic-gate 	 * XXX For now only handle S_CREATE.
26260Sstevel@tonic-gate 	 */
26270Sstevel@tonic-gate 	ASSERT(rw == S_CREATE);
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate 	index	= start_index;
26300Sstevel@tonic-gate 	p_index	= 0;
26310Sstevel@tonic-gate 	npgs = btopr(len);
26320Sstevel@tonic-gate 
26330Sstevel@tonic-gate 	/*
26340Sstevel@tonic-gate 	 * If this platform supports multiple page sizes
26350Sstevel@tonic-gate 	 * then try and allocate directly from the free
26360Sstevel@tonic-gate 	 * list for pages larger than PAGESIZE.
26370Sstevel@tonic-gate 	 *
26380Sstevel@tonic-gate 	 * NOTE:When we have page_create_ru we can stop
26390Sstevel@tonic-gate 	 *	directly allocating from the freelist.
26400Sstevel@tonic-gate 	 */
26410Sstevel@tonic-gate 	l_szc  = seg->s_szc;
26420Sstevel@tonic-gate 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
26430Sstevel@tonic-gate 	while (npgs) {
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate 		/*
26460Sstevel@tonic-gate 		 * if anon slot already exists
26470Sstevel@tonic-gate 		 *   (means page has been created)
26480Sstevel@tonic-gate 		 * so 1) look up the page
26490Sstevel@tonic-gate 		 *    2) if the page is still in memory, get it.
26500Sstevel@tonic-gate 		 *    3) if not, create a page and
26510Sstevel@tonic-gate 		 *	  page in from physical swap device.
26520Sstevel@tonic-gate 		 * These are done in anon_getpage().
26530Sstevel@tonic-gate 		 */
26540Sstevel@tonic-gate 		ap = anon_get_ptr(amp->ahp, index);
26550Sstevel@tonic-gate 		if (ap) {
26560Sstevel@tonic-gate 			err = anon_getpage(&ap, &prot, anon_pl, PAGESIZE,
26570Sstevel@tonic-gate 			    seg, addr, S_READ, cred);
26580Sstevel@tonic-gate 			if (err) {
26590Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
26600Sstevel@tonic-gate 				panic("anon_map_createpages: anon_getpage");
26610Sstevel@tonic-gate 			}
26620Sstevel@tonic-gate 			pp = anon_pl[0];
26630Sstevel@tonic-gate 			ppa[p_index++] = pp;
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate 			addr += PAGESIZE;
26660Sstevel@tonic-gate 			index++;
26670Sstevel@tonic-gate 			npgs--;
26680Sstevel@tonic-gate 			continue;
26690Sstevel@tonic-gate 		}
26700Sstevel@tonic-gate 		/*
26710Sstevel@tonic-gate 		 * Now try and allocate the largest page possible
26720Sstevel@tonic-gate 		 * for the current address and range.
26730Sstevel@tonic-gate 		 * Keep dropping down in page size until:
26740Sstevel@tonic-gate 		 *
26750Sstevel@tonic-gate 		 *	1) Properly aligned
26760Sstevel@tonic-gate 		 *	2) Does not overlap existing anon pages
26770Sstevel@tonic-gate 		 *	3) Fits in remaining range.
26780Sstevel@tonic-gate 		 *	4) able to allocate one.
26790Sstevel@tonic-gate 		 *
26800Sstevel@tonic-gate 		 * NOTE: XXX When page_create_ru is completed this code
26810Sstevel@tonic-gate 		 *	 will change.
26820Sstevel@tonic-gate 		 */
26830Sstevel@tonic-gate 		szc    = l_szc;
26840Sstevel@tonic-gate 		pplist = NULL;
26850Sstevel@tonic-gate 		pg_cnt = 0;
26860Sstevel@tonic-gate 		while (szc) {
26870Sstevel@tonic-gate 			pgsz	= page_get_pagesize(szc);
26880Sstevel@tonic-gate 			pg_cnt	= pgsz >> PAGESHIFT;
26890Sstevel@tonic-gate 			if (IS_P2ALIGNED(addr, pgsz) && pg_cnt <= npgs &&
26900Sstevel@tonic-gate 				anon_pages(amp->ahp, index, pg_cnt) == 0) {
26910Sstevel@tonic-gate 				/*
26920Sstevel@tonic-gate 				 * XXX
26930Sstevel@tonic-gate 				 * Since we are faking page_create()
26940Sstevel@tonic-gate 				 * we also need to do the freemem and
26950Sstevel@tonic-gate 				 * pcf accounting.
26960Sstevel@tonic-gate 				 */
26970Sstevel@tonic-gate 				(void) page_create_wait(pg_cnt, PG_WAIT);
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate 				/*
27000Sstevel@tonic-gate 				 * Get lgroup to allocate next page of shared
27010Sstevel@tonic-gate 				 * memory from and use it to specify where to
27020Sstevel@tonic-gate 				 * allocate the physical memory
27030Sstevel@tonic-gate 				 */
27040Sstevel@tonic-gate 				lgrp = lgrp_mem_choose(seg, addr, pgsz);
27050Sstevel@tonic-gate 
27060Sstevel@tonic-gate 				pplist = page_get_freelist(
27070Sstevel@tonic-gate 				    (struct vnode *)NULL, (u_offset_t)0, seg,
27080Sstevel@tonic-gate 				    addr, pgsz, 0, lgrp);
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate 				if (pplist == NULL) {
27110Sstevel@tonic-gate 					page_create_putback(pg_cnt);
27120Sstevel@tonic-gate 				}
27130Sstevel@tonic-gate 
27140Sstevel@tonic-gate 				/*
27150Sstevel@tonic-gate 				 * If a request for a page of size
27160Sstevel@tonic-gate 				 * larger than PAGESIZE failed
27170Sstevel@tonic-gate 				 * then don't try that size anymore.
27180Sstevel@tonic-gate 				 */
27190Sstevel@tonic-gate 				if (pplist == NULL) {
27200Sstevel@tonic-gate 					l_szc = szc - 1;
27210Sstevel@tonic-gate 				} else {
27220Sstevel@tonic-gate 					break;
27230Sstevel@tonic-gate 				}
27240Sstevel@tonic-gate 			}
27250Sstevel@tonic-gate 			szc--;
27260Sstevel@tonic-gate 		}
27270Sstevel@tonic-gate 
27280Sstevel@tonic-gate 		/*
27290Sstevel@tonic-gate 		 * If just using PAGESIZE pages then don't
27300Sstevel@tonic-gate 		 * directly allocate from the free list.
27310Sstevel@tonic-gate 		 */
27320Sstevel@tonic-gate 		if (pplist == NULL) {
27330Sstevel@tonic-gate 			ASSERT(szc == 0);
27340Sstevel@tonic-gate 			pp = anon_zero(seg, addr, &ap, cred);
27350Sstevel@tonic-gate 			if (pp == NULL) {
27360Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
27370Sstevel@tonic-gate 				panic("anon_map_createpages: anon_zero");
27380Sstevel@tonic-gate 			}
27390Sstevel@tonic-gate 			ppa[p_index++] = pp;
27400Sstevel@tonic-gate 
27410Sstevel@tonic-gate 			ASSERT(anon_get_ptr(amp->ahp, index) == NULL);
27420Sstevel@tonic-gate 			(void) anon_set_ptr(amp->ahp, index, ap, ANON_SLEEP);
27430Sstevel@tonic-gate 
27440Sstevel@tonic-gate 			addr += PAGESIZE;
27450Sstevel@tonic-gate 			index++;
27460Sstevel@tonic-gate 			npgs--;
27470Sstevel@tonic-gate 			continue;
27480Sstevel@tonic-gate 		}
27490Sstevel@tonic-gate 
27500Sstevel@tonic-gate 		/*
27510Sstevel@tonic-gate 		 * pplist is a list of pg_cnt PAGESIZE pages.
27520Sstevel@tonic-gate 		 * These pages are locked SE_EXCL since they
27530Sstevel@tonic-gate 		 * came directly off the free list.
27540Sstevel@tonic-gate 		 */
27550Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(pg_cnt, pg_cnt));
27560Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(index, pg_cnt));
27570Sstevel@tonic-gate 		ASSERT(conpp == NULL);
27580Sstevel@tonic-gate 		while (pg_cnt--) {
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 			ap = anon_alloc(NULL, 0);
27610Sstevel@tonic-gate 			swap_xlate(ap, &ap_vp, &ap_off);
27620Sstevel@tonic-gate 
27630Sstevel@tonic-gate 			ASSERT(pplist != NULL);
27640Sstevel@tonic-gate 			pp = pplist;
27650Sstevel@tonic-gate 			page_sub(&pplist, pp);
27660Sstevel@tonic-gate 			PP_CLRFREE(pp);
27670Sstevel@tonic-gate 			PP_CLRAGED(pp);
27680Sstevel@tonic-gate 			conpp = pp;
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate 			err = swap_getconpage(ap_vp, ap_off, PAGESIZE,
27710Sstevel@tonic-gate 			    (uint_t *)NULL, anon_pl, PAGESIZE, conpp, &nreloc,
27720Sstevel@tonic-gate 			    seg, addr, S_CREATE, cred);
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate 			if (err) {
27750Sstevel@tonic-gate 				ANON_LOCK_EXIT(&amp->a_rwlock);
27760Sstevel@tonic-gate 				panic("anon_map_createpages: S_CREATE");
27770Sstevel@tonic-gate 			}
27780Sstevel@tonic-gate 
27790Sstevel@tonic-gate 			ASSERT(anon_pl[0] == pp);
27800Sstevel@tonic-gate 			ASSERT(nreloc == 1);
27810Sstevel@tonic-gate 			pagezero(pp, 0, PAGESIZE);
27820Sstevel@tonic-gate 			CPU_STATS_ADD_K(vm, zfod, 1);
27830Sstevel@tonic-gate 			hat_setrefmod(pp);
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 			ASSERT(anon_get_ptr(amp->ahp, index) == NULL);
27860Sstevel@tonic-gate 			(void) anon_set_ptr(amp->ahp, index, ap, ANON_SLEEP);
27870Sstevel@tonic-gate 
27880Sstevel@tonic-gate 			ppa[p_index++] = pp;
27890Sstevel@tonic-gate 
27900Sstevel@tonic-gate 			addr += PAGESIZE;
27910Sstevel@tonic-gate 			index++;
27920Sstevel@tonic-gate 			npgs--;
27930Sstevel@tonic-gate 		}
27940Sstevel@tonic-gate 		conpp = NULL;
27950Sstevel@tonic-gate 		pg_cnt	= pgsz >> PAGESHIFT;
27960Sstevel@tonic-gate 		p_index = p_index - pg_cnt;
27970Sstevel@tonic-gate 		while (pg_cnt--) {
27980Sstevel@tonic-gate 			page_downgrade(ppa[p_index++]);
27990Sstevel@tonic-gate 		}
28000Sstevel@tonic-gate 	}
28010Sstevel@tonic-gate 	ANON_LOCK_EXIT(&amp->a_rwlock);
28020Sstevel@tonic-gate 	return (0);
28030Sstevel@tonic-gate }
28040Sstevel@tonic-gate 
28050Sstevel@tonic-gate int
28060Sstevel@tonic-gate anon_map_demotepages(
28070Sstevel@tonic-gate 	struct anon_map *amp,
28080Sstevel@tonic-gate 	ulong_t	start_idx,
28090Sstevel@tonic-gate 	struct seg *seg,
28100Sstevel@tonic-gate 	caddr_t addr,
28110Sstevel@tonic-gate 	uint_t prot,
28120Sstevel@tonic-gate 	struct vpage vpage[],
28130Sstevel@tonic-gate 	struct cred *cred)
28140Sstevel@tonic-gate {
28150Sstevel@tonic-gate 	struct anon	*ap;
28160Sstevel@tonic-gate 	uint_t		szc = seg->s_szc;
28170Sstevel@tonic-gate 	pgcnt_t		pgcnt = page_get_pagecnt(szc);
28180Sstevel@tonic-gate 	size_t		ppasize = pgcnt * sizeof (page_t *);
28190Sstevel@tonic-gate 	page_t		**ppa = kmem_alloc(ppasize, KM_SLEEP);
28200Sstevel@tonic-gate 	page_t		*pp;
28210Sstevel@tonic-gate 	page_t		*pl[2];
28220Sstevel@tonic-gate 	pgcnt_t		i, pg_idx;
28230Sstevel@tonic-gate 	ulong_t		an_idx;
28240Sstevel@tonic-gate 	caddr_t		vaddr;
28250Sstevel@tonic-gate 	kmutex_t	*ahmpages = NULL;
28260Sstevel@tonic-gate 	int 		err;
28270Sstevel@tonic-gate 	int		retry = 0;
28280Sstevel@tonic-gate 	uint_t		vpprot;
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&amp->a_rwlock));
28310Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
28320Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(start_idx, pgcnt));
28330Sstevel@tonic-gate 	ASSERT(ppa != NULL);
28340Sstevel@tonic-gate 
28350Sstevel@tonic-gate 	VM_STAT_ADD(anonvmstats.demotepages[0]);
28360Sstevel@tonic-gate 
28370Sstevel@tonic-gate 	ap = anon_get_ptr(amp->ahp, start_idx);
28380Sstevel@tonic-gate 	if (ap != NULL) {
28390Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.demotepages[1]);
28400Sstevel@tonic-gate 		ahmpages = &anonpages_hash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
28410Sstevel@tonic-gate 		mutex_enter(ahmpages);
28420Sstevel@tonic-gate 	}
28430Sstevel@tonic-gate top:
28440Sstevel@tonic-gate 	if (ap == NULL || ap->an_refcnt <= 1) {
28450Sstevel@tonic-gate 		int root = 0;
28460Sstevel@tonic-gate 		pgcnt_t npgs, curnpgs = 0;
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.demotepages[2]);
28490Sstevel@tonic-gate 
28500Sstevel@tonic-gate 		ASSERT(retry == 0 || ap != NULL);
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate 		if (ahmpages != NULL)
28530Sstevel@tonic-gate 			mutex_exit(ahmpages);
28540Sstevel@tonic-gate 		an_idx = start_idx;
28550Sstevel@tonic-gate 		for (i = 0; i < pgcnt; i++, an_idx++) {
28560Sstevel@tonic-gate 			ap = anon_get_ptr(amp->ahp, an_idx);
28570Sstevel@tonic-gate 			if (ap != NULL) {
28580Sstevel@tonic-gate 				ASSERT(ap->an_refcnt == 1);
28590Sstevel@tonic-gate 				pp = ppa[i] = page_lookup(ap->an_vp, ap->an_off,
28600Sstevel@tonic-gate 				    SE_EXCL);
28610Sstevel@tonic-gate 				if (pp != NULL) {
28620Sstevel@tonic-gate 					(void) hat_pageunload(pp,
28630Sstevel@tonic-gate 					    HAT_FORCE_PGUNLOAD);
28640Sstevel@tonic-gate 				}
28650Sstevel@tonic-gate 			} else {
28660Sstevel@tonic-gate 				ppa[i] = NULL;
28670Sstevel@tonic-gate 			}
28680Sstevel@tonic-gate 		}
28690Sstevel@tonic-gate 		for (i = 0; i < pgcnt; i++) {
28700Sstevel@tonic-gate 			if ((pp = ppa[i]) != NULL && pp->p_szc != 0) {
28710Sstevel@tonic-gate 				ASSERT(pp->p_szc <= szc);
28720Sstevel@tonic-gate 				if (!root) {
28730Sstevel@tonic-gate 					VM_STAT_ADD(anonvmstats.demotepages[3]);
28740Sstevel@tonic-gate 					if (curnpgs != 0)
28750Sstevel@tonic-gate 						panic("anon_map_demotepages: "
28760Sstevel@tonic-gate 						    "bad large page");
28770Sstevel@tonic-gate 
28780Sstevel@tonic-gate 					root = 1;
28790Sstevel@tonic-gate 					curnpgs = npgs =
28800Sstevel@tonic-gate 					    page_get_pagecnt(pp->p_szc);
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 					ASSERT(npgs <= pgcnt);
28830Sstevel@tonic-gate 					ASSERT(IS_P2ALIGNED(npgs, npgs));
28840Sstevel@tonic-gate 					ASSERT(!(page_pptonum(pp) &
28850Sstevel@tonic-gate 					    (npgs - 1)));
28860Sstevel@tonic-gate 				} else {
28870Sstevel@tonic-gate 					ASSERT(i > 0);
28880Sstevel@tonic-gate 					ASSERT(page_pptonum(pp) - 1 ==
28890Sstevel@tonic-gate 					    page_pptonum(ppa[i - 1]));
28900Sstevel@tonic-gate 					if ((page_pptonum(pp) & (npgs - 1)) ==
28910Sstevel@tonic-gate 					    npgs - 1)
28920Sstevel@tonic-gate 						root = 0;
28930Sstevel@tonic-gate 				}
28940Sstevel@tonic-gate 				ASSERT(PAGE_EXCL(pp));
28950Sstevel@tonic-gate 				pp->p_szc = 0;
28960Sstevel@tonic-gate 				curnpgs--;
28970Sstevel@tonic-gate 			}
28980Sstevel@tonic-gate 		}
28990Sstevel@tonic-gate 		if (root != 0 || curnpgs != 0)
29000Sstevel@tonic-gate 			panic("anon_map_demotepages: bad large page");
29010Sstevel@tonic-gate 
29020Sstevel@tonic-gate 		for (i = 0; i < pgcnt; i++) {
29030Sstevel@tonic-gate 			if ((pp = ppa[i]) != NULL) {
29040Sstevel@tonic-gate 				ASSERT(!hat_page_is_mapped(pp));
29050Sstevel@tonic-gate 				ASSERT(pp->p_szc == 0);
29060Sstevel@tonic-gate 				page_unlock(pp);
29070Sstevel@tonic-gate 			}
29080Sstevel@tonic-gate 		}
29090Sstevel@tonic-gate 		kmem_free(ppa, ppasize);
29100Sstevel@tonic-gate 		return (0);
29110Sstevel@tonic-gate 	}
29120Sstevel@tonic-gate 	ASSERT(ahmpages != NULL);
29130Sstevel@tonic-gate 	mutex_exit(ahmpages);
29140Sstevel@tonic-gate 	ahmpages = NULL;
29150Sstevel@tonic-gate 
29160Sstevel@tonic-gate 	VM_STAT_ADD(anonvmstats.demotepages[4]);
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 	ASSERT(retry == 0); /* we can be here only once */
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate 	vaddr = addr;
29210Sstevel@tonic-gate 	for (pg_idx = 0, an_idx = start_idx; pg_idx < pgcnt;
29220Sstevel@tonic-gate 	    pg_idx++, an_idx++, vaddr += PAGESIZE) {
29230Sstevel@tonic-gate 		ap = anon_get_ptr(amp->ahp, an_idx);
29240Sstevel@tonic-gate 		if (ap == NULL)
29250Sstevel@tonic-gate 			panic("anon_map_demotepages: no anon slot");
29260Sstevel@tonic-gate 		err = anon_getpage(&ap, &vpprot, pl, PAGESIZE, seg, vaddr,
29270Sstevel@tonic-gate 		    S_READ, cred);
29280Sstevel@tonic-gate 		if (err) {
29290Sstevel@tonic-gate 			for (i = 0; i < pg_idx; i++) {
29300Sstevel@tonic-gate 				if ((pp = ppa[i]) != NULL)
29310Sstevel@tonic-gate 					page_unlock(pp);
29320Sstevel@tonic-gate 			}
29330Sstevel@tonic-gate 			kmem_free(ppa, ppasize);
29340Sstevel@tonic-gate 			return (err);
29350Sstevel@tonic-gate 		}
29360Sstevel@tonic-gate 		ppa[pg_idx] = pl[0];
29370Sstevel@tonic-gate 	}
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate 	err = anon_map_privatepages(amp, start_idx, szc, seg, addr, prot, ppa,
29400Sstevel@tonic-gate 	    vpage, -1, cred);
29410Sstevel@tonic-gate 	if (err > 0) {
29420Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.demotepages[5]);
29430Sstevel@tonic-gate 		kmem_free(ppa, ppasize);
29440Sstevel@tonic-gate 		return (err);
29450Sstevel@tonic-gate 	}
29460Sstevel@tonic-gate 	ASSERT(err == 0 || err == -1);
29470Sstevel@tonic-gate 	if (err == -1) {
29480Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.demotepages[6]);
29490Sstevel@tonic-gate 		retry = 1;
29500Sstevel@tonic-gate 		goto top;
29510Sstevel@tonic-gate 	}
29520Sstevel@tonic-gate 	for (i = 0; i < pgcnt; i++) {
29530Sstevel@tonic-gate 		ASSERT(ppa[i] != NULL);
29540Sstevel@tonic-gate 		if (ppa[i]->p_szc != 0)
29550Sstevel@tonic-gate 			retry = 1;
29560Sstevel@tonic-gate 		page_unlock(ppa[i]);
29570Sstevel@tonic-gate 	}
29580Sstevel@tonic-gate 	if (retry) {
29590Sstevel@tonic-gate 		VM_STAT_ADD(anonvmstats.demotepages[7]);
29600Sstevel@tonic-gate 		goto top;
29610Sstevel@tonic-gate 	}
29620Sstevel@tonic-gate 
29630Sstevel@tonic-gate 	VM_STAT_ADD(anonvmstats.demotepages[8]);
29640Sstevel@tonic-gate 
29650Sstevel@tonic-gate 	kmem_free(ppa, ppasize);
29660Sstevel@tonic-gate 
29670Sstevel@tonic-gate 	return (0);
29680Sstevel@tonic-gate }
29690Sstevel@tonic-gate 
29700Sstevel@tonic-gate /*
29710Sstevel@tonic-gate  * Allocate and initialize an anon_map structure for seg
29720Sstevel@tonic-gate  * associating the given swap reservation with the new anon_map.
29730Sstevel@tonic-gate  */
29740Sstevel@tonic-gate struct anon_map *
29750Sstevel@tonic-gate anonmap_alloc(size_t size, size_t swresv)
29760Sstevel@tonic-gate {
29770Sstevel@tonic-gate 	struct anon_map *amp;
29780Sstevel@tonic-gate 
29790Sstevel@tonic-gate 	amp = kmem_cache_alloc(anonmap_cache, KM_SLEEP);
29800Sstevel@tonic-gate 
29810Sstevel@tonic-gate 	amp->refcnt = 1;
29820Sstevel@tonic-gate 	amp->size = size;
29830Sstevel@tonic-gate 
29840Sstevel@tonic-gate 	amp->ahp = anon_create(btopr(size), ANON_SLEEP);
29850Sstevel@tonic-gate 	amp->swresv = swresv;
29860Sstevel@tonic-gate 	amp->locality = 0;
29870Sstevel@tonic-gate 	amp->a_szc = 0;
29880Sstevel@tonic-gate 	return (amp);
29890Sstevel@tonic-gate }
29900Sstevel@tonic-gate 
29910Sstevel@tonic-gate void
29920Sstevel@tonic-gate anonmap_free(struct anon_map *amp)
29930Sstevel@tonic-gate {
29940Sstevel@tonic-gate 	ASSERT(amp->ahp);
29950Sstevel@tonic-gate 	ASSERT(amp->refcnt == 0);
29960Sstevel@tonic-gate 
29970Sstevel@tonic-gate 	lgrp_shm_policy_fini(amp, NULL);
29980Sstevel@tonic-gate 	anon_release(amp->ahp, btopr(amp->size));
29990Sstevel@tonic-gate 	kmem_cache_free(anonmap_cache, amp);
30000Sstevel@tonic-gate }
30010Sstevel@tonic-gate 
30020Sstevel@tonic-gate /*
30030Sstevel@tonic-gate  * Returns true if the app array has some empty slots.
30040Sstevel@tonic-gate  * The offp and lenp paramters are in/out paramters.  On entry
30050Sstevel@tonic-gate  * these values represent the starting offset and length of the
30060Sstevel@tonic-gate  * mapping.  When true is returned, these values may be modified
30070Sstevel@tonic-gate  * to be the largest range which includes empty slots.
30080Sstevel@tonic-gate  */
30090Sstevel@tonic-gate int
30100Sstevel@tonic-gate non_anon(struct anon_hdr *ahp, ulong_t anon_idx, u_offset_t *offp,
30110Sstevel@tonic-gate 				size_t *lenp)
30120Sstevel@tonic-gate {
30130Sstevel@tonic-gate 	ulong_t i, el;
30140Sstevel@tonic-gate 	ssize_t low, high;
30150Sstevel@tonic-gate 	struct anon *ap;
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate 	low = -1;
30180Sstevel@tonic-gate 	for (i = 0, el = *lenp; i < el; i += PAGESIZE, anon_idx++) {
30190Sstevel@tonic-gate 		ap = anon_get_ptr(ahp, anon_idx);
30200Sstevel@tonic-gate 		if (ap == NULL) {
30210Sstevel@tonic-gate 			if (low == -1)
30220Sstevel@tonic-gate 				low = i;
30230Sstevel@tonic-gate 			high = i;
30240Sstevel@tonic-gate 		}
30250Sstevel@tonic-gate 	}
30260Sstevel@tonic-gate 	if (low != -1) {
30270Sstevel@tonic-gate 		/*
30280Sstevel@tonic-gate 		 * Found at least one non-anon page.
30290Sstevel@tonic-gate 		 * Set up the off and len return values.
30300Sstevel@tonic-gate 		 */
30310Sstevel@tonic-gate 		if (low != 0)
30320Sstevel@tonic-gate 			*offp += low;
30330Sstevel@tonic-gate 		*lenp = high - low + PAGESIZE;
30340Sstevel@tonic-gate 		return (1);
30350Sstevel@tonic-gate 	}
30360Sstevel@tonic-gate 	return (0);
30370Sstevel@tonic-gate }
30380Sstevel@tonic-gate 
30390Sstevel@tonic-gate /*
30400Sstevel@tonic-gate  * Return a count of the number of existing anon pages in the anon array
30410Sstevel@tonic-gate  * app in the range (off, off+len). The array and slots must be guaranteed
30420Sstevel@tonic-gate  * stable by the caller.
30430Sstevel@tonic-gate  */
30440Sstevel@tonic-gate pgcnt_t
30450Sstevel@tonic-gate anon_pages(struct anon_hdr *ahp, ulong_t anon_index, pgcnt_t nslots)
30460Sstevel@tonic-gate {
30470Sstevel@tonic-gate 	pgcnt_t cnt = 0;
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 	while (nslots-- > 0) {
30500Sstevel@tonic-gate 		if ((anon_get_ptr(ahp, anon_index)) != NULL)
30510Sstevel@tonic-gate 			cnt++;
30520Sstevel@tonic-gate 		anon_index++;
30530Sstevel@tonic-gate 	}
30540Sstevel@tonic-gate 	return (cnt);
30550Sstevel@tonic-gate }
30560Sstevel@tonic-gate 
30570Sstevel@tonic-gate /*
30580Sstevel@tonic-gate  * Move reserved phys swap into memory swap (unreserve phys swap
30590Sstevel@tonic-gate  * and reserve mem swap by the same amount).
30600Sstevel@tonic-gate  * Used by segspt when it needs to lock resrved swap npages in memory
30610Sstevel@tonic-gate  */
30620Sstevel@tonic-gate int
30630Sstevel@tonic-gate anon_swap_adjust(pgcnt_t npages)
30640Sstevel@tonic-gate {
30650Sstevel@tonic-gate 	pgcnt_t unlocked_mem_swap;
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate 	mutex_enter(&anoninfo_lock);
30680Sstevel@tonic-gate 
30690Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
30700Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
30710Sstevel@tonic-gate 
30720Sstevel@tonic-gate 	unlocked_mem_swap = k_anoninfo.ani_mem_resv
30730Sstevel@tonic-gate 					- k_anoninfo.ani_locked_swap;
30740Sstevel@tonic-gate 	if (npages > unlocked_mem_swap) {
30750Sstevel@tonic-gate 		spgcnt_t adjusted_swap = npages - unlocked_mem_swap;
30760Sstevel@tonic-gate 
30770Sstevel@tonic-gate 		/*
30780Sstevel@tonic-gate 		 * if there is not enough unlocked mem swap we take missing
30790Sstevel@tonic-gate 		 * amount from phys swap and give it to mem swap
30800Sstevel@tonic-gate 		 */
30810Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
30820Sstevel@tonic-gate 		if (availrmem < adjusted_swap + segspt_minfree) {
30830Sstevel@tonic-gate 			mutex_exit(&freemem_lock);
30840Sstevel@tonic-gate 			mutex_exit(&anoninfo_lock);
30850Sstevel@tonic-gate 			return (ENOMEM);
30860Sstevel@tonic-gate 		}
30870Sstevel@tonic-gate 		availrmem -= adjusted_swap;
30880Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
30890Sstevel@tonic-gate 
30900Sstevel@tonic-gate 		k_anoninfo.ani_mem_resv += adjusted_swap;
30910Sstevel@tonic-gate 		ASSERT(k_anoninfo.ani_phys_resv >= adjusted_swap);
30920Sstevel@tonic-gate 		k_anoninfo.ani_phys_resv -= adjusted_swap;
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 		ANI_ADD(adjusted_swap);
30950Sstevel@tonic-gate 	}
30960Sstevel@tonic-gate 	k_anoninfo.ani_locked_swap += npages;
30970Sstevel@tonic-gate 
30980Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
30990Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
31000Sstevel@tonic-gate 
31010Sstevel@tonic-gate 	mutex_exit(&anoninfo_lock);
31020Sstevel@tonic-gate 
31030Sstevel@tonic-gate 	return (0);
31040Sstevel@tonic-gate }
31050Sstevel@tonic-gate 
31060Sstevel@tonic-gate /*
31070Sstevel@tonic-gate  * 'unlocked' reserved mem swap so when it is unreserved it
31080Sstevel@tonic-gate  * can be moved back phys (disk) swap
31090Sstevel@tonic-gate  */
31100Sstevel@tonic-gate void
31110Sstevel@tonic-gate anon_swap_restore(pgcnt_t npages)
31120Sstevel@tonic-gate {
31130Sstevel@tonic-gate 	mutex_enter(&anoninfo_lock);
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_locked_swap <= k_anoninfo.ani_mem_resv);
31160Sstevel@tonic-gate 
31170Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_locked_swap >= npages);
31180Sstevel@tonic-gate 	k_anoninfo.ani_locked_swap -= npages;
31190Sstevel@tonic-gate 
31200Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_locked_swap <= k_anoninfo.ani_mem_resv);
31210Sstevel@tonic-gate 
31220Sstevel@tonic-gate 	mutex_exit(&anoninfo_lock);
31230Sstevel@tonic-gate }
31240Sstevel@tonic-gate 
31250Sstevel@tonic-gate /*
31260Sstevel@tonic-gate  * Return the pointer from the list for a
31270Sstevel@tonic-gate  * specified anon index.
31280Sstevel@tonic-gate  */
31290Sstevel@tonic-gate ulong_t *
31300Sstevel@tonic-gate anon_get_slot(struct anon_hdr *ahp, ulong_t an_idx)
31310Sstevel@tonic-gate {
31320Sstevel@tonic-gate 	struct anon	**app;
31330Sstevel@tonic-gate 	void 		**ppp;
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate 	ASSERT(an_idx < ahp->size);
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate 	/*
31380Sstevel@tonic-gate 	 * Single level case.
31390Sstevel@tonic-gate 	 */
31400Sstevel@tonic-gate 	if ((ahp->size <= ANON_CHUNK_SIZE) || (ahp->flags & ANON_ALLOC_FORCE)) {
31410Sstevel@tonic-gate 		return ((ulong_t *)&ahp->array_chunk[an_idx]);
31420Sstevel@tonic-gate 	} else {
31430Sstevel@tonic-gate 
31440Sstevel@tonic-gate 		/*
31450Sstevel@tonic-gate 		 * 2 level case.
31460Sstevel@tonic-gate 		 */
31470Sstevel@tonic-gate 		ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
31480Sstevel@tonic-gate 		if (*ppp == NULL) {
31490Sstevel@tonic-gate 			mutex_enter(&ahp->serial_lock);
31500Sstevel@tonic-gate 			ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
31510Sstevel@tonic-gate 			if (*ppp == NULL)
31520Sstevel@tonic-gate 				*ppp = kmem_zalloc(PAGESIZE, KM_SLEEP);
31530Sstevel@tonic-gate 			mutex_exit(&ahp->serial_lock);
31540Sstevel@tonic-gate 		}
31550Sstevel@tonic-gate 		app = *ppp;
31560Sstevel@tonic-gate 		return ((ulong_t *)&app[an_idx & ANON_CHUNK_OFF]);
31570Sstevel@tonic-gate 	}
31580Sstevel@tonic-gate }
31590Sstevel@tonic-gate 
31600Sstevel@tonic-gate void
31610Sstevel@tonic-gate anon_array_enter(struct anon_map *amp, ulong_t an_idx, anon_sync_obj_t *sobj)
31620Sstevel@tonic-gate {
31630Sstevel@tonic-gate 	ulong_t		*ap_slot;
31640Sstevel@tonic-gate 	kmutex_t	*mtx;
31650Sstevel@tonic-gate 	kcondvar_t	*cv;
31660Sstevel@tonic-gate 	int		hash;
31670Sstevel@tonic-gate 
31680Sstevel@tonic-gate 	/*
31690Sstevel@tonic-gate 	 * Use szc to determine anon slot(s) to appear atomic.
31700Sstevel@tonic-gate 	 * If szc = 0, then lock the anon slot and mark it busy.
31710Sstevel@tonic-gate 	 * If szc > 0, then lock the range of slots by getting the
31720Sstevel@tonic-gate 	 * anon_array_lock for the first anon slot, and mark only the
31730Sstevel@tonic-gate 	 * first anon slot busy to represent whole range being busy.
31740Sstevel@tonic-gate 	 */
31750Sstevel@tonic-gate 
31760Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&amp->a_rwlock));
31770Sstevel@tonic-gate 	an_idx = P2ALIGN(an_idx, page_get_pagecnt(amp->a_szc));
31780Sstevel@tonic-gate 	hash = ANON_ARRAY_HASH(amp, an_idx);
31790Sstevel@tonic-gate 	sobj->sync_mutex = mtx = &anon_array_lock[hash].pad_mutex;
31800Sstevel@tonic-gate 	sobj->sync_cv = cv = &anon_array_cv[hash];
31810Sstevel@tonic-gate 	mutex_enter(mtx);
31820Sstevel@tonic-gate 	ap_slot = anon_get_slot(amp->ahp, an_idx);
31830Sstevel@tonic-gate 	while (ANON_ISBUSY(ap_slot))
31840Sstevel@tonic-gate 		cv_wait(cv, mtx);
31850Sstevel@tonic-gate 	ANON_SETBUSY(ap_slot);
31860Sstevel@tonic-gate 	sobj->sync_data = ap_slot;
31870Sstevel@tonic-gate 	mutex_exit(mtx);
31880Sstevel@tonic-gate }
31890Sstevel@tonic-gate 
31900Sstevel@tonic-gate void
31910Sstevel@tonic-gate anon_array_exit(anon_sync_obj_t *sobj)
31920Sstevel@tonic-gate {
31930Sstevel@tonic-gate 	mutex_enter(sobj->sync_mutex);
31940Sstevel@tonic-gate 	ASSERT(ANON_ISBUSY(sobj->sync_data));
31950Sstevel@tonic-gate 	ANON_CLRBUSY(sobj->sync_data);
31960Sstevel@tonic-gate 	if (CV_HAS_WAITERS(sobj->sync_cv))
31970Sstevel@tonic-gate 		cv_broadcast(sobj->sync_cv);
31980Sstevel@tonic-gate 	mutex_exit(sobj->sync_mutex);
31990Sstevel@tonic-gate }
3200