xref: /titanic_51/usr/src/uts/common/vm/vm_anon.c (revision 42487ff1899d230ad6c2d55cf9573489ca5eb770)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 #pragma ident	"%Z%%M%	%I%	%E% SMI"
40 
41 /*
42  * VM - anonymous pages.
43  *
44  * This layer sits immediately above the vm_swap layer.  It manages
45  * physical pages that have no permanent identity in the file system
46  * name space, using the services of the vm_swap layer to allocate
47  * backing storage for these pages.  Since these pages have no external
48  * identity, they are discarded when the last reference is removed.
49  *
50  * An important function of this layer is to manage low-level sharing
51  * of pages that are logically distinct but that happen to be
52  * physically identical (e.g., the corresponding pages of the processes
53  * resulting from a fork before one process or the other changes their
54  * contents).  This pseudo-sharing is present only as an optimization
55  * and is not to be confused with true sharing in which multiple
56  * address spaces deliberately contain references to the same object;
57  * such sharing is managed at a higher level.
58  *
59  * The key data structure here is the anon struct, which contains a
60  * reference count for its associated physical page and a hint about
61  * the identity of that page.  Anon structs typically live in arrays,
62  * with an instance's position in its array determining where the
63  * corresponding backing storage is allocated; however, the swap_xlate()
64  * routine abstracts away this representation information so that the
65  * rest of the anon layer need not know it.  (See the swap layer for
66  * more details on anon struct layout.)
67  *
68  * In the future versions of the system, the association between an
69  * anon struct and its position on backing store will change so that
70  * we don't require backing store all anonymous pages in the system.
71  * This is important for consideration for large memory systems.
72  * We can also use this technique to delay binding physical locations
73  * to anonymous pages until pageout/swapout time where we can make
74  * smarter allocation decisions to improve anonymous klustering.
75  *
76  * Many of the routines defined here take a (struct anon **) argument,
77  * which allows the code at this level to manage anon pages directly,
78  * so that callers can regard anon structs as opaque objects and not be
79  * concerned with assigning or inspecting their contents.
80  *
81  * Clients of this layer refer to anon pages indirectly.  That is, they
82  * maintain arrays of pointers to anon structs rather than maintaining
83  * anon structs themselves.  The (struct anon **) arguments mentioned
84  * above are pointers to entries in these arrays.  It is these arrays
85  * that capture the mapping between offsets within a given segment and
86  * the corresponding anonymous backing storage address.
87  */
88 
89 #ifdef DEBUG
90 #define	ANON_DEBUG
91 #endif
92 
93 #include <sys/types.h>
94 #include <sys/t_lock.h>
95 #include <sys/param.h>
96 #include <sys/systm.h>
97 #include <sys/mman.h>
98 #include <sys/cred.h>
99 #include <sys/thread.h>
100 #include <sys/vnode.h>
101 #include <sys/cpuvar.h>
102 #include <sys/swap.h>
103 #include <sys/cmn_err.h>
104 #include <sys/vtrace.h>
105 #include <sys/kmem.h>
106 #include <sys/sysmacros.h>
107 #include <sys/bitmap.h>
108 #include <sys/vmsystm.h>
109 #include <sys/debug.h>
110 #include <sys/fs/swapnode.h>
111 #include <sys/tnf_probe.h>
112 #include <sys/lgrp.h>
113 #include <sys/policy.h>
114 #include <sys/condvar_impl.h>
115 #include <sys/mutex_impl.h>
116 
117 #include <vm/as.h>
118 #include <vm/hat.h>
119 #include <vm/anon.h>
120 #include <vm/page.h>
121 #include <vm/vpage.h>
122 #include <vm/seg.h>
123 #include <vm/rm.h>
124 
125 #include <fs/fs_subr.h>
126 
127 struct vnode *anon_vp;
128 
129 int anon_debug;
130 
131 kmutex_t	anoninfo_lock;
132 struct		k_anoninfo k_anoninfo;
133 ani_free_t	ani_free_pool[ANI_MAX_POOL];
134 pad_mutex_t	anon_array_lock[ANON_LOCKSIZE];
135 kcondvar_t	anon_array_cv[ANON_LOCKSIZE];
136 
137 /*
138  * Global hash table for (vp, off) -> anon slot
139  */
140 extern	int swap_maxcontig;
141 size_t	anon_hash_size;
142 struct anon **anon_hash;
143 
144 static struct kmem_cache *anon_cache;
145 static struct kmem_cache *anonmap_cache;
146 
147 #ifdef VM_STATS
148 static struct anonvmstats_str {
149 	ulong_t getpages[30];
150 	ulong_t privatepages[10];
151 	ulong_t demotepages[9];
152 	ulong_t decrefpages[9];
153 	ulong_t	dupfillholes[4];
154 	ulong_t freepages[1];
155 } anonvmstats;
156 #endif /* VM_STATS */
157 
158 
159 /*ARGSUSED*/
160 static int
161 anonmap_cache_constructor(void *buf, void *cdrarg, int kmflags)
162 {
163 	struct anon_map *amp = buf;
164 
165 	rw_init(&amp->a_rwlock, NULL, RW_DEFAULT, NULL);
166 	return (0);
167 }
168 
169 /*ARGSUSED1*/
170 static void
171 anonmap_cache_destructor(void *buf, void *cdrarg)
172 {
173 	struct anon_map *amp = buf;
174 
175 	rw_destroy(&amp->a_rwlock);
176 }
177 
178 kmutex_t	anonhash_lock[AH_LOCK_SIZE];
179 kmutex_t	anonpages_hash_lock[AH_LOCK_SIZE];
180 
181 void
182 anon_init(void)
183 {
184 	int i;
185 
186 	anon_hash_size = 1L << highbit(physmem / ANON_HASHAVELEN);
187 
188 	for (i = 0; i < AH_LOCK_SIZE; i++) {
189 		mutex_init(&anonhash_lock[i], NULL, MUTEX_DEFAULT, NULL);
190 		mutex_init(&anonpages_hash_lock[i], NULL, MUTEX_DEFAULT, NULL);
191 	}
192 
193 	for (i = 0; i < ANON_LOCKSIZE; i++) {
194 		mutex_init(&anon_array_lock[i].pad_mutex, NULL,
195 			MUTEX_DEFAULT, NULL);
196 		cv_init(&anon_array_cv[i], NULL, CV_DEFAULT, NULL);
197 	}
198 
199 	anon_hash = (struct anon **)
200 		kmem_zalloc(sizeof (struct anon *) * anon_hash_size, KM_SLEEP);
201 	anon_cache = kmem_cache_create("anon_cache", sizeof (struct anon),
202 		AN_CACHE_ALIGN, NULL, NULL, NULL, NULL, NULL, 0);
203 	anonmap_cache = kmem_cache_create("anonmap_cache",
204 		sizeof (struct anon_map), 0,
205 		anonmap_cache_constructor, anonmap_cache_destructor, NULL,
206 		NULL, NULL, 0);
207 	swap_maxcontig = (1024 * 1024) >> PAGESHIFT;	/* 1MB of pages */
208 
209 	anon_vp = vn_alloc(KM_SLEEP);
210 	vn_setops(anon_vp, swap_vnodeops);
211 	anon_vp->v_type = VREG;
212 	anon_vp->v_flag |= (VISSWAP|VISSWAPFS);
213 }
214 
215 /*
216  * Global anon slot hash table manipulation.
217  */
218 
219 static void
220 anon_addhash(struct anon *ap)
221 {
222 	int index;
223 
224 	ASSERT(MUTEX_HELD(&anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)]));
225 	index = ANON_HASH(ap->an_vp, ap->an_off);
226 	ap->an_hash = anon_hash[index];
227 	anon_hash[index] = ap;
228 }
229 
230 static void
231 anon_rmhash(struct anon *ap)
232 {
233 	struct anon **app;
234 
235 	ASSERT(MUTEX_HELD(&anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)]));
236 
237 	for (app = &anon_hash[ANON_HASH(ap->an_vp, ap->an_off)];
238 	    *app; app = &((*app)->an_hash)) {
239 		if (*app == ap) {
240 			*app = ap->an_hash;
241 			break;
242 		}
243 	}
244 }
245 
246 /*
247  * The anon array interfaces. Functions allocating,
248  * freeing array of pointers, and returning/setting
249  * entries in the array of pointers for a given offset.
250  *
251  * Create the list of pointers
252  */
253 struct anon_hdr *
254 anon_create(pgcnt_t npages, int flags)
255 {
256 	struct anon_hdr *ahp;
257 	ulong_t nchunks;
258 	int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
259 
260 	if ((ahp = kmem_zalloc(sizeof (struct anon_hdr), kmemflags)) == NULL) {
261 		return (NULL);
262 	}
263 
264 	mutex_init(&ahp->serial_lock, NULL, MUTEX_DEFAULT, NULL);
265 	/*
266 	 * Single level case.
267 	 */
268 	ahp->size = npages;
269 	if (npages <= ANON_CHUNK_SIZE || (flags & ANON_ALLOC_FORCE)) {
270 
271 		if (flags & ANON_ALLOC_FORCE)
272 			ahp->flags |= ANON_ALLOC_FORCE;
273 
274 		ahp->array_chunk = kmem_zalloc(
275 		    ahp->size * sizeof (struct anon *), kmemflags);
276 
277 		if (ahp->array_chunk == NULL) {
278 			kmem_free(ahp, sizeof (struct anon_hdr));
279 			return (NULL);
280 		}
281 	} else {
282 		/*
283 		 * 2 Level case.
284 		 */
285 		nchunks = (ahp->size + ANON_CHUNK_OFF) >> ANON_CHUNK_SHIFT;
286 
287 		ahp->array_chunk = kmem_zalloc(nchunks * sizeof (ulong_t *),
288 		    kmemflags);
289 
290 		if (ahp->array_chunk == NULL) {
291 			kmem_free(ahp, sizeof (struct anon_hdr));
292 			return (NULL);
293 		}
294 	}
295 	return (ahp);
296 }
297 
298 /*
299  * Free the array of pointers
300  */
301 void
302 anon_release(struct anon_hdr *ahp, pgcnt_t npages)
303 {
304 	ulong_t i;
305 	void **ppp;
306 	ulong_t nchunks;
307 
308 	ASSERT(npages == ahp->size);
309 
310 	/*
311 	 * Single level case.
312 	 */
313 	if (npages <= ANON_CHUNK_SIZE || (ahp->flags & ANON_ALLOC_FORCE)) {
314 		kmem_free(ahp->array_chunk, ahp->size * sizeof (struct anon *));
315 	} else {
316 		/*
317 		 * 2 level case.
318 		 */
319 		nchunks = (ahp->size + ANON_CHUNK_OFF) >> ANON_CHUNK_SHIFT;
320 		for (i = 0; i < nchunks; i++) {
321 			ppp = &ahp->array_chunk[i];
322 			if (*ppp != NULL)
323 				kmem_free(*ppp, PAGESIZE);
324 		}
325 		kmem_free(ahp->array_chunk, nchunks * sizeof (ulong_t *));
326 	}
327 	mutex_destroy(&ahp->serial_lock);
328 	kmem_free(ahp, sizeof (struct anon_hdr));
329 }
330 
331 /*
332  * Return the pointer from the list for a
333  * specified anon index.
334  */
335 struct anon *
336 anon_get_ptr(struct anon_hdr *ahp, ulong_t an_idx)
337 {
338 	struct anon **app;
339 
340 	ASSERT(an_idx < ahp->size);
341 
342 	/*
343 	 * Single level case.
344 	 */
345 	if ((ahp->size <= ANON_CHUNK_SIZE) || (ahp->flags & ANON_ALLOC_FORCE)) {
346 		return ((struct anon *)
347 			((uintptr_t)ahp->array_chunk[an_idx] & ANON_PTRMASK));
348 	} else {
349 
350 		/*
351 		 * 2 level case.
352 		 */
353 		app = ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
354 		if (app) {
355 			return ((struct anon *)
356 				((uintptr_t)app[an_idx & ANON_CHUNK_OFF] &
357 					ANON_PTRMASK));
358 		} else {
359 			return (NULL);
360 		}
361 	}
362 }
363 
364 /*
365  * Return the anon pointer for the first valid entry in the anon list,
366  * starting from the given index.
367  */
368 struct anon *
369 anon_get_next_ptr(struct anon_hdr *ahp, ulong_t *index)
370 {
371 	struct anon *ap;
372 	struct anon **app;
373 	ulong_t chunkoff;
374 	ulong_t i;
375 	ulong_t j;
376 	pgcnt_t size;
377 
378 	i = *index;
379 	size = ahp->size;
380 
381 	ASSERT(i < size);
382 
383 	if ((size <= ANON_CHUNK_SIZE) || (ahp->flags & ANON_ALLOC_FORCE)) {
384 		/*
385 		 * 1 level case
386 		 */
387 		while (i < size) {
388 			ap = (struct anon *)
389 				((uintptr_t)ahp->array_chunk[i] & ANON_PTRMASK);
390 			if (ap) {
391 				*index = i;
392 				return (ap);
393 			}
394 			i++;
395 		}
396 	} else {
397 		/*
398 		 * 2 level case
399 		 */
400 		chunkoff = i & ANON_CHUNK_OFF;
401 		while (i < size) {
402 			app = ahp->array_chunk[i >> ANON_CHUNK_SHIFT];
403 			if (app)
404 				for (j = chunkoff; j < ANON_CHUNK_SIZE; j++) {
405 					ap = (struct anon *)
406 						((uintptr_t)app[j] &
407 							ANON_PTRMASK);
408 					if (ap) {
409 						*index = i + (j - chunkoff);
410 						return (ap);
411 					}
412 				}
413 			chunkoff = 0;
414 			i = (i + ANON_CHUNK_SIZE) & ~ANON_CHUNK_OFF;
415 		}
416 	}
417 	*index = size;
418 	return (NULL);
419 }
420 
421 /*
422  * Set list entry with a given pointer for a specified offset
423  */
424 int
425 anon_set_ptr(struct anon_hdr *ahp, ulong_t an_idx, struct anon *ap, int flags)
426 {
427 	void		**ppp;
428 	struct anon	**app;
429 	int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
430 	uintptr_t	*ap_addr;
431 
432 	ASSERT(an_idx < ahp->size);
433 
434 	/*
435 	 * Single level case.
436 	 */
437 	if (ahp->size <= ANON_CHUNK_SIZE || (ahp->flags & ANON_ALLOC_FORCE)) {
438 		ap_addr = (uintptr_t *)&ahp->array_chunk[an_idx];
439 	} else {
440 
441 		/*
442 		 * 2 level case.
443 		 */
444 		ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
445 
446 		ASSERT(ppp != NULL);
447 		if (*ppp == NULL) {
448 			mutex_enter(&ahp->serial_lock);
449 			ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
450 			if (*ppp == NULL) {
451 				*ppp = kmem_zalloc(PAGESIZE, kmemflags);
452 				if (*ppp == NULL) {
453 					mutex_exit(&ahp->serial_lock);
454 					return (ENOMEM);
455 				}
456 			}
457 			mutex_exit(&ahp->serial_lock);
458 		}
459 		app = *ppp;
460 		ap_addr = (uintptr_t *)&app[an_idx & ANON_CHUNK_OFF];
461 	}
462 	*ap_addr = (*ap_addr & ~ANON_PTRMASK) | (uintptr_t)ap;
463 	return (0);
464 }
465 
466 /*
467  * Copy anon array into a given new anon array
468  */
469 int
470 anon_copy_ptr(struct anon_hdr *sahp, ulong_t s_idx,
471 	struct anon_hdr *dahp, ulong_t d_idx,
472 	pgcnt_t npages, int flags)
473 {
474 	void **sapp, **dapp;
475 	void *ap;
476 	int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
477 
478 	ASSERT((s_idx < sahp->size) && (d_idx < dahp->size));
479 	ASSERT((npages <= sahp->size) && (npages <= dahp->size));
480 
481 	/*
482 	 * Both arrays are 1 level.
483 	 */
484 	if (((sahp->size <= ANON_CHUNK_SIZE) &&
485 	    (dahp->size <= ANON_CHUNK_SIZE)) ||
486 	    ((sahp->flags & ANON_ALLOC_FORCE) &&
487 	    (dahp->flags & ANON_ALLOC_FORCE))) {
488 
489 		bcopy(&sahp->array_chunk[s_idx], &dahp->array_chunk[d_idx],
490 		    npages * sizeof (struct anon *));
491 		return (0);
492 	}
493 
494 	/*
495 	 * Both arrays are 2 levels.
496 	 */
497 	if (sahp->size > ANON_CHUNK_SIZE &&
498 	    dahp->size > ANON_CHUNK_SIZE &&
499 	    ((sahp->flags & ANON_ALLOC_FORCE) == 0) &&
500 	    ((dahp->flags & ANON_ALLOC_FORCE) == 0)) {
501 
502 		ulong_t sapidx, dapidx;
503 		ulong_t *sap, *dap;
504 		ulong_t chknp;
505 
506 		while (npages != 0) {
507 
508 			sapidx = s_idx & ANON_CHUNK_OFF;
509 			dapidx = d_idx & ANON_CHUNK_OFF;
510 			chknp = ANON_CHUNK_SIZE - MAX(sapidx, dapidx);
511 			if (chknp > npages)
512 				chknp = npages;
513 
514 			sapp = &sahp->array_chunk[s_idx >> ANON_CHUNK_SHIFT];
515 			if ((sap = *sapp) != NULL) {
516 				dapp = &dahp->array_chunk[d_idx
517 							>> ANON_CHUNK_SHIFT];
518 				if ((dap = *dapp) == NULL) {
519 					*dapp = kmem_zalloc(PAGESIZE,
520 					    kmemflags);
521 					if ((dap = *dapp) == NULL)
522 						return (ENOMEM);
523 				}
524 				bcopy((sap + sapidx), (dap + dapidx),
525 				    chknp << ANON_PTRSHIFT);
526 			}
527 			s_idx += chknp;
528 			d_idx += chknp;
529 			npages -= chknp;
530 		}
531 		return (0);
532 	}
533 
534 	/*
535 	 * At least one of the arrays is 2 level.
536 	 */
537 	while (npages--) {
538 		if ((ap = anon_get_ptr(sahp, s_idx)) != NULL) {
539 			ASSERT(!ANON_ISBUSY(anon_get_slot(sahp, s_idx)));
540 			if (anon_set_ptr(dahp, d_idx, ap, flags) == ENOMEM)
541 					return (ENOMEM);
542 		}
543 		s_idx++;
544 		d_idx++;
545 	}
546 	return (0);
547 }
548 
549 
550 /*
551  * ANON_INITBUF is a convenience macro for anon_grow() below. It
552  * takes a buffer dst, which is at least as large as buffer src. It
553  * does a bcopy from src into dst, and then bzeros the extra bytes
554  * of dst. If tail is set, the data in src is tail aligned within
555  * dst instead of head aligned.
556  */
557 
558 #define	ANON_INITBUF(src, srclen, dst, dstsize, tail)			      \
559 	if (tail) {							      \
560 		bzero((dst), (dstsize) - (srclen));			      \
561 		bcopy((src), (char *)(dst) + (dstsize) - (srclen), (srclen)); \
562 	} else {							      \
563 		bcopy((src), (dst), (srclen));				      \
564 		bzero((char *)(dst) + (srclen), (dstsize) - (srclen));	      \
565 	}
566 
567 #define	ANON_1_LEVEL_INC	(ANON_CHUNK_SIZE / 8)
568 #define	ANON_2_LEVEL_INC	(ANON_1_LEVEL_INC * ANON_CHUNK_SIZE)
569 
570 /*
571  * anon_grow() is used to efficiently extend an existing anon array.
572  * startidx_p points to the index into the anon array of the first page
573  * that is in use. oldseg_pgs is the number of pages in use, starting at
574  * *startidx_p. newpages is the number of additional pages desired.
575  *
576  * If startidx_p == NULL, startidx is taken to be 0 and cannot be changed.
577  *
578  * The growth is done by creating a new top level of the anon array,
579  * and (if the array is 2-level) reusing the existing second level arrays.
580  *
581  * flags can be used to specify ANON_NOSLEEP and ANON_GROWDOWN.
582  *
583  * Returns the new number of pages in the anon array.
584  */
585 pgcnt_t
586 anon_grow(struct anon_hdr *ahp, ulong_t *startidx_p, pgcnt_t oldseg_pgs,
587     pgcnt_t newseg_pgs, int flags)
588 {
589 	ulong_t startidx = startidx_p ? *startidx_p : 0;
590 	pgcnt_t oldamp_pgs = ahp->size, newamp_pgs;
591 	pgcnt_t oelems, nelems, totpages;
592 	void **level1;
593 	int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
594 	int growdown = (flags & ANON_GROWDOWN);
595 	size_t newarrsz, oldarrsz;
596 	void *level2;
597 
598 	ASSERT(!(startidx_p == NULL && growdown));
599 	ASSERT(startidx + oldseg_pgs <= ahp->size);
600 
601 	/*
602 	 * Determine the total number of pages needed in the new
603 	 * anon array. If growing down, totpages is all pages from
604 	 * startidx through the end of the array, plus <newseg_pgs>
605 	 * pages. If growing up, keep all pages from page 0 through
606 	 * the last page currently in use, plus <newseg_pgs> pages.
607 	 */
608 	if (growdown)
609 		totpages = oldamp_pgs - startidx + newseg_pgs;
610 	else
611 		totpages = startidx + oldseg_pgs + newseg_pgs;
612 
613 	/* If the array is already large enough, just return. */
614 
615 	if (oldamp_pgs >= totpages) {
616 		if (growdown)
617 			*startidx_p = oldamp_pgs - totpages;
618 		return (oldamp_pgs);
619 	}
620 
621 	/*
622 	 * oldamp_pgs/newamp_pgs are the total numbers of pages represented
623 	 * by the corresponding arrays.
624 	 * oelems/nelems are the number of pointers in the top level arrays
625 	 * which may be either level 1 or level 2.
626 	 * Will the new anon array be one level or two levels?
627 	 */
628 	if (totpages <= ANON_CHUNK_SIZE || (ahp->flags & ANON_ALLOC_FORCE)) {
629 		newamp_pgs = P2ROUNDUP(totpages, ANON_1_LEVEL_INC);
630 		oelems = oldamp_pgs;
631 		nelems = newamp_pgs;
632 	} else {
633 		newamp_pgs = P2ROUNDUP(totpages, ANON_2_LEVEL_INC);
634 		oelems = (oldamp_pgs + ANON_CHUNK_OFF) >> ANON_CHUNK_SHIFT;
635 		nelems = newamp_pgs >> ANON_CHUNK_SHIFT;
636 	}
637 
638 	newarrsz = nelems * sizeof (void *);
639 	level1 = kmem_alloc(newarrsz, kmemflags);
640 	if (level1 == NULL)
641 		return (0);
642 
643 	/* Are we converting from a one level to a two level anon array? */
644 
645 	if (newamp_pgs > ANON_CHUNK_SIZE && oldamp_pgs <= ANON_CHUNK_SIZE &&
646 	    !(ahp->flags & ANON_ALLOC_FORCE)) {
647 
648 		/*
649 		 * Yes, we're converting to a two level. Reuse old level 1
650 		 * as new level 2 if it is exactly PAGESIZE. Otherwise
651 		 * alloc a new level 2 and copy the old level 1 data into it.
652 		 */
653 		if (oldamp_pgs == ANON_CHUNK_SIZE) {
654 			level2 = (void *)ahp->array_chunk;
655 		} else {
656 			level2 = kmem_alloc(PAGESIZE, kmemflags);
657 			if (level2 == NULL) {
658 				kmem_free(level1, newarrsz);
659 				return (0);
660 			}
661 			oldarrsz = oldamp_pgs * sizeof (void *);
662 
663 			ANON_INITBUF(ahp->array_chunk, oldarrsz,
664 			    level2, PAGESIZE, growdown);
665 			kmem_free(ahp->array_chunk, oldarrsz);
666 		}
667 		bzero(level1, newarrsz);
668 		if (growdown)
669 			level1[nelems - 1] = level2;
670 		else
671 			level1[0] = level2;
672 	} else {
673 		oldarrsz = oelems * sizeof (void *);
674 
675 		ANON_INITBUF(ahp->array_chunk, oldarrsz,
676 		    level1, newarrsz, growdown);
677 		kmem_free(ahp->array_chunk, oldarrsz);
678 	}
679 
680 	ahp->array_chunk = level1;
681 	ahp->size = newamp_pgs;
682 	if (growdown) {
683 		*startidx_p = newamp_pgs - totpages;
684 		if (oldamp_pgs > ANON_CHUNK_SIZE)
685 			*startidx_p -= P2NPHASE(oldseg_pgs, ANON_CHUNK_SIZE);
686 	}
687 	return (newamp_pgs);
688 }
689 
690 
691 /*
692  * Called from clock handler to sync ani_free value.
693  */
694 
695 void
696 set_anoninfo(void)
697 {
698 	int	ix;
699 	pgcnt_t	total = 0;
700 
701 	for (ix = 0; ix < ANI_MAX_POOL; ix++) {
702 		total += ani_free_pool[ix].ani_count;
703 	}
704 	k_anoninfo.ani_free = total;
705 }
706 
707 /*
708  * Reserve anon space.
709  *
710  * It's no longer simply a matter of incrementing ani_resv to
711  * reserve swap space, we need to check memory-based as well
712  * as disk-backed (physical) swap.  The following algorithm
713  * is used:
714  * 	Check the space on physical swap
715  * 		i.e. amount needed < ani_max - ani_phys_resv
716  * 	If we are swapping on swapfs check
717  *		amount needed < (availrmem - swapfs_minfree)
718  * Since the algorithm to check for the quantity of swap space is
719  * almost the same as that for reserving it, we'll just use anon_resvmem
720  * with a flag to decrement availrmem.
721  *
722  * Return non-zero on success.
723  */
724 int
725 anon_resvmem(size_t size, uint_t takemem)
726 {
727 	pgcnt_t npages = btopr(size);
728 	pgcnt_t mswap_pages = 0;
729 	pgcnt_t pswap_pages = 0;
730 
731 	mutex_enter(&anoninfo_lock);
732 
733 	/*
734 	 * pswap_pages is the number of pages we can take from
735 	 * physical (i.e. disk-backed) swap.
736 	 */
737 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
738 	pswap_pages = k_anoninfo.ani_max - k_anoninfo.ani_phys_resv;
739 
740 	ANON_PRINT(A_RESV,
741 	    ("anon_resvmem: npages %lu takemem %u pswap %lu caller %p\n",
742 	    npages, takemem, pswap_pages, (void *)caller()));
743 
744 	if (npages <= pswap_pages) {
745 		/*
746 		 * we have enough space on a physical swap
747 		 */
748 		if (takemem)
749 			k_anoninfo.ani_phys_resv += npages;
750 		mutex_exit(&anoninfo_lock);
751 		return (1);
752 	} else if (pswap_pages != 0) {
753 		/*
754 		 * we have some space on a physical swap
755 		 */
756 		if (takemem) {
757 			/*
758 			 * use up remainder of phys swap
759 			 */
760 			k_anoninfo.ani_phys_resv += pswap_pages;
761 			ASSERT(k_anoninfo.ani_phys_resv == k_anoninfo.ani_max);
762 		}
763 	}
764 	/*
765 	 * since (npages > pswap_pages) we need mem swap
766 	 * mswap_pages is the number of pages needed from availrmem
767 	 */
768 	ASSERT(npages > pswap_pages);
769 	mswap_pages = npages - pswap_pages;
770 
771 	ANON_PRINT(A_RESV, ("anon_resvmem: need %ld pages from memory\n",
772 	    mswap_pages));
773 
774 	/*
775 	 * priv processes can reserve memory as swap as long as availrmem
776 	 * remains greater than swapfs_minfree; in the case of non-priv
777 	 * processes, memory can be reserved as swap only if availrmem
778 	 * doesn't fall below (swapfs_minfree + swapfs_reserve). Thus,
779 	 * swapfs_reserve amount of memswap is not available to non-priv
780 	 * processes. This protects daemons such as automounter dying
781 	 * as a result of application processes eating away almost entire
782 	 * membased swap. This safeguard becomes useless if apps are run
783 	 * with root access.
784 	 *
785 	 * swapfs_reserve is minimum of 4Mb or 1/16 of physmem.
786 	 *
787 	 */
788 	mutex_enter(&freemem_lock);
789 	if (availrmem > (swapfs_minfree + swapfs_reserve + mswap_pages) ||
790 		(availrmem > (swapfs_minfree + mswap_pages) &&
791 		secpolicy_resource(CRED()) == 0)) {
792 
793 		if (takemem) {
794 			/*
795 			 * Take the memory from the rest of the system.
796 			 */
797 			availrmem -= mswap_pages;
798 			mutex_exit(&freemem_lock);
799 			k_anoninfo.ani_mem_resv += mswap_pages;
800 			ANI_ADD(mswap_pages);
801 			ANON_PRINT((A_RESV | A_MRESV),
802 				("anon_resvmem: took %ld pages of availrmem\n",
803 				mswap_pages));
804 		} else {
805 			mutex_exit(&freemem_lock);
806 		}
807 
808 		ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
809 		mutex_exit(&anoninfo_lock);
810 		return (1);
811 
812 	} else {
813 		/*
814 		 * Fail if not enough memory
815 		 */
816 
817 		if (takemem) {
818 			k_anoninfo.ani_phys_resv -= pswap_pages;
819 		}
820 
821 		mutex_exit(&freemem_lock);
822 		mutex_exit(&anoninfo_lock);
823 		ANON_PRINT(A_RESV,
824 			("anon_resvmem: not enough space from swapfs\n"));
825 		return (0);
826 	}
827 }
828 
829 
830 /*
831  * Give back an anon reservation.
832  */
833 void
834 anon_unresv(size_t size)
835 {
836 	pgcnt_t npages = btopr(size);
837 	spgcnt_t mem_free_pages = 0;
838 	pgcnt_t phys_free_slots;
839 #ifdef	ANON_DEBUG
840 	pgcnt_t mem_resv;
841 #endif
842 
843 	mutex_enter(&anoninfo_lock);
844 
845 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
846 	/*
847 	 * If some of this reservation belonged to swapfs
848 	 * give it back to availrmem.
849 	 * ani_mem_resv is the amount of availrmem swapfs has reserved.
850 	 * but some of that memory could be locked by segspt so we can only
851 	 * return non locked ani_mem_resv back to availrmem
852 	 */
853 	if (k_anoninfo.ani_mem_resv > k_anoninfo.ani_locked_swap) {
854 		ANON_PRINT((A_RESV | A_MRESV),
855 		    ("anon_unresv: growing availrmem by %ld pages\n",
856 		    MIN(k_anoninfo.ani_mem_resv, npages)));
857 
858 		mem_free_pages = MIN((spgcnt_t)(k_anoninfo.ani_mem_resv -
859 		    k_anoninfo.ani_locked_swap), npages);
860 		mutex_enter(&freemem_lock);
861 		availrmem += mem_free_pages;
862 		mutex_exit(&freemem_lock);
863 		k_anoninfo.ani_mem_resv -= mem_free_pages;
864 
865 		ANI_ADD(-mem_free_pages);
866 	}
867 	/*
868 	 * The remainder of the pages is returned to phys swap
869 	 */
870 	ASSERT(npages >= mem_free_pages);
871 	phys_free_slots = npages - mem_free_pages;
872 
873 	if (phys_free_slots) {
874 	    k_anoninfo.ani_phys_resv -= phys_free_slots;
875 	}
876 
877 #ifdef	ANON_DEBUG
878 	mem_resv = k_anoninfo.ani_mem_resv;
879 #endif
880 
881 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
882 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
883 
884 	mutex_exit(&anoninfo_lock);
885 
886 	ANON_PRINT(A_RESV, ("anon_unresv: %lu, tot %lu, caller %p\n",
887 	    npages, mem_resv, (void *)caller()));
888 }
889 
890 /*
891  * Allocate an anon slot and return it with the lock held.
892  */
893 struct anon *
894 anon_alloc(struct vnode *vp, anoff_t off)
895 {
896 	struct anon	*ap;
897 	kmutex_t	*ahm;
898 
899 	ap = kmem_cache_alloc(anon_cache, KM_SLEEP);
900 	if (vp == NULL) {
901 		swap_alloc(ap);
902 	} else {
903 		ap->an_vp = vp;
904 		ap->an_off = off;
905 	}
906 	ap->an_refcnt = 1;
907 	ap->an_pvp = NULL;
908 	ap->an_poff = 0;
909 	ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
910 	mutex_enter(ahm);
911 	anon_addhash(ap);
912 	mutex_exit(ahm);
913 	ANI_ADD(-1);
914 	ANON_PRINT(A_ANON, ("anon_alloc: returning ap %p, vp %p\n",
915 	    (void *)ap, (ap ? (void *)ap->an_vp : NULL)));
916 	return (ap);
917 }
918 
919 /*
920  * Decrement the reference count of an anon page.
921  * If reference count goes to zero, free it and
922  * its associated page (if any).
923  */
924 void
925 anon_decref(struct anon *ap)
926 {
927 	page_t *pp;
928 	struct vnode *vp;
929 	anoff_t off;
930 	kmutex_t *ahm;
931 
932 	ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
933 	mutex_enter(ahm);
934 	ASSERT(ap->an_refcnt != 0);
935 	if (ap->an_refcnt == 0)
936 		panic("anon_decref: slot count 0");
937 	if (--ap->an_refcnt == 0) {
938 		swap_xlate(ap, &vp, &off);
939 		mutex_exit(ahm);
940 
941 		/*
942 		 * If there is a page for this anon slot we will need to
943 		 * call VN_DISPOSE to get rid of the vp association and
944 		 * put the page back on the free list as really free.
945 		 * Acquire the "exclusive" lock to ensure that any
946 		 * pending i/o always completes before the swap slot
947 		 * is freed.
948 		 */
949 		pp = page_lookup(vp, (u_offset_t)off, SE_EXCL);
950 
951 		/*
952 		 * If there was a page, we've synchronized on it (getting
953 		 * the exclusive lock is as good as gettting the iolock)
954 		 * so now we can free the physical backing store. Also, this
955 		 * is where we would free the name of the anonymous page
956 		 * (swap_free(ap)), a no-op in the current implementation.
957 		 */
958 		mutex_enter(ahm);
959 		ASSERT(ap->an_refcnt == 0);
960 		anon_rmhash(ap);
961 		if (ap->an_pvp)
962 			swap_phys_free(ap->an_pvp, ap->an_poff, PAGESIZE);
963 		mutex_exit(ahm);
964 
965 		if (pp != NULL) {
966 			/*LINTED: constant in conditional context */
967 			VN_DISPOSE(pp, B_INVAL, 0, kcred);
968 		}
969 		ANON_PRINT(A_ANON, ("anon_decref: free ap %p, vp %p\n",
970 		    (void *)ap, (void *)ap->an_vp));
971 		kmem_cache_free(anon_cache, ap);
972 
973 		ANI_ADD(1);
974 	} else {
975 		mutex_exit(ahm);
976 	}
977 }
978 
979 static int
980 anon_share(struct anon_hdr *ahp, ulong_t anon_index, pgcnt_t nslots)
981 {
982 	struct anon *ap;
983 
984 	while (nslots-- > 0) {
985 		if ((ap = anon_get_ptr(ahp, anon_index)) != NULL &&
986 		    ap->an_refcnt > 1)
987 			return (1);
988 		anon_index++;
989 	}
990 
991 	return (0);
992 }
993 
994 static void
995 anon_decref_pages(
996 	struct anon_hdr *ahp,
997 	ulong_t an_idx,
998 	uint_t szc)
999 {
1000 	struct anon *ap = anon_get_ptr(ahp, an_idx);
1001 	kmutex_t *ahmpages = NULL;
1002 	page_t *pp;
1003 	pgcnt_t pgcnt = page_get_pagecnt(szc);
1004 	pgcnt_t i;
1005 	struct vnode *vp;
1006 	anoff_t   off;
1007 	kmutex_t *ahm;
1008 #ifdef DEBUG
1009 	int refcnt = 1;
1010 #endif
1011 
1012 	ASSERT(szc != 0);
1013 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
1014 	ASSERT(IS_P2ALIGNED(an_idx, pgcnt));
1015 
1016 	VM_STAT_ADD(anonvmstats.decrefpages[0]);
1017 
1018 	if (ap != NULL) {
1019 		ahmpages = &anonpages_hash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
1020 		mutex_enter(ahmpages);
1021 		ASSERT((refcnt = ap->an_refcnt) != 0);
1022 		VM_STAT_ADD(anonvmstats.decrefpages[1]);
1023 		if (ap->an_refcnt == 1) {
1024 			VM_STAT_ADD(anonvmstats.decrefpages[2]);
1025 			ASSERT(!anon_share(ahp, an_idx, pgcnt));
1026 			mutex_exit(ahmpages);
1027 			ahmpages = NULL;
1028 		}
1029 	}
1030 
1031 	i = 0;
1032 	while (i < pgcnt) {
1033 		if ((ap = anon_get_ptr(ahp, an_idx + i)) == NULL) {
1034 			ASSERT(refcnt == 1 && ahmpages == NULL);
1035 			i++;
1036 			continue;
1037 		}
1038 		ASSERT(ap->an_refcnt == refcnt);
1039 		ASSERT(ahmpages != NULL || ap->an_refcnt == 1);
1040 		ASSERT(ahmpages == NULL || ap->an_refcnt > 1);
1041 
1042 		if (ahmpages == NULL) {
1043 			swap_xlate(ap, &vp, &off);
1044 			pp = page_lookup(vp, (u_offset_t)off, SE_EXCL);
1045 			if (pp == NULL || pp->p_szc == 0) {
1046 				VM_STAT_ADD(anonvmstats.decrefpages[3]);
1047 				ahm = &anonhash_lock[AH_LOCK(ap->an_vp,
1048 				    ap->an_off)];
1049 				(void) anon_set_ptr(ahp, an_idx + i, NULL,
1050 				    ANON_SLEEP);
1051 				mutex_enter(ahm);
1052 				ap->an_refcnt--;
1053 				ASSERT(ap->an_refcnt == 0);
1054 				anon_rmhash(ap);
1055 				if (ap->an_pvp)
1056 					swap_phys_free(ap->an_pvp, ap->an_poff,
1057 					    PAGESIZE);
1058 				mutex_exit(ahm);
1059 				if (pp != NULL) {
1060 					VM_STAT_ADD(anonvmstats.decrefpages[4]);
1061 					/*LINTED*/
1062 					VN_DISPOSE(pp, B_INVAL, 0, kcred);
1063 				}
1064 				kmem_cache_free(anon_cache, ap);
1065 				ANI_ADD(1);
1066 				i++;
1067 			} else {
1068 				pgcnt_t j;
1069 				pgcnt_t curpgcnt =
1070 				    page_get_pagecnt(pp->p_szc);
1071 				size_t ppasize = curpgcnt * sizeof (page_t *);
1072 				page_t **ppa = kmem_alloc(ppasize, KM_SLEEP);
1073 				int dispose = 0;
1074 
1075 				VM_STAT_ADD(anonvmstats.decrefpages[5]);
1076 
1077 				ASSERT(pp->p_szc <= szc);
1078 				ASSERT(IS_P2ALIGNED(curpgcnt, curpgcnt));
1079 				ASSERT(IS_P2ALIGNED(i, curpgcnt));
1080 				ASSERT(i + curpgcnt <= pgcnt);
1081 				ASSERT(!(page_pptonum(pp) & (curpgcnt - 1)));
1082 				ppa[0] = pp;
1083 				for (j = i + 1; j < i + curpgcnt; j++) {
1084 					ap = anon_get_ptr(ahp, an_idx + j);
1085 					ASSERT(ap != NULL &&
1086 					    ap->an_refcnt == 1);
1087 					swap_xlate(ap, &vp, &off);
1088 					pp = page_lookup(vp, (u_offset_t)off,
1089 					    SE_EXCL);
1090 					if (pp == NULL)
1091 						panic("anon_decref_pages: "
1092 						    "no page");
1093 
1094 					(void) hat_pageunload(pp,
1095 					    HAT_FORCE_PGUNLOAD);
1096 					ASSERT(pp->p_szc == ppa[0]->p_szc);
1097 					ASSERT(page_pptonum(pp) - 1 ==
1098 					    page_pptonum(ppa[j - i - 1]));
1099 					ppa[j - i] = pp;
1100 					if (ap->an_pvp != NULL &&
1101 					    !vn_matchopval(ap->an_pvp,
1102 						VOPNAME_DISPOSE,
1103 						(fs_generic_func_p)fs_dispose))
1104 						dispose = 1;
1105 				}
1106 				if (!dispose) {
1107 					VM_STAT_ADD(anonvmstats.decrefpages[6]);
1108 					page_destroy_pages(ppa[0]);
1109 				} else {
1110 					VM_STAT_ADD(anonvmstats.decrefpages[7]);
1111 					for (j = 0; j < curpgcnt; j++) {
1112 						ASSERT(PAGE_EXCL(ppa[j]));
1113 						ppa[j]->p_szc = 0;
1114 					}
1115 					for (j = 0; j < curpgcnt; j++) {
1116 						ASSERT(!hat_page_is_mapped(
1117 						    ppa[j]));
1118 						/*LINTED*/
1119 						VN_DISPOSE(ppa[j], B_INVAL, 0,
1120 						    kcred);
1121 					}
1122 				}
1123 				kmem_free(ppa, ppasize);
1124 				for (j = i; j < i + curpgcnt; j++) {
1125 					ap = anon_get_ptr(ahp, an_idx + j);
1126 					ASSERT(ap != NULL &&
1127 					    ap->an_refcnt == 1);
1128 					ahm = &anonhash_lock[AH_LOCK(ap->an_vp,
1129 					    ap->an_off)];
1130 					(void) anon_set_ptr(ahp, an_idx + j,
1131 					    NULL, ANON_SLEEP);
1132 					mutex_enter(ahm);
1133 					ap->an_refcnt--;
1134 					ASSERT(ap->an_refcnt == 0);
1135 					anon_rmhash(ap);
1136 					if (ap->an_pvp)
1137 						swap_phys_free(ap->an_pvp,
1138 							ap->an_poff, PAGESIZE);
1139 					mutex_exit(ahm);
1140 					kmem_cache_free(anon_cache, ap);
1141 					ANI_ADD(1);
1142 				}
1143 				i += curpgcnt;
1144 			}
1145 		} else {
1146 			VM_STAT_ADD(anonvmstats.decrefpages[8]);
1147 			(void) anon_set_ptr(ahp, an_idx + i, NULL, ANON_SLEEP);
1148 			ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
1149 			mutex_enter(ahm);
1150 			ap->an_refcnt--;
1151 			mutex_exit(ahm);
1152 			i++;
1153 		}
1154 	}
1155 
1156 	if (ahmpages != NULL) {
1157 		mutex_exit(ahmpages);
1158 	}
1159 }
1160 
1161 /*
1162  * Duplicate references to size bytes worth of anon pages.
1163  * Used when duplicating a segment that contains private anon pages.
1164  * This code assumes that procedure calling this one has already used
1165  * hat_chgprot() to disable write access to the range of addresses that
1166  * that *old actually refers to.
1167  */
1168 void
1169 anon_dup(struct anon_hdr *old, ulong_t old_idx, struct anon_hdr *new,
1170 			ulong_t new_idx, size_t size)
1171 {
1172 	spgcnt_t npages;
1173 	kmutex_t *ahm;
1174 	struct anon *ap;
1175 	ulong_t off;
1176 	ulong_t index;
1177 
1178 	npages = btopr(size);
1179 	while (npages > 0) {
1180 		index = old_idx;
1181 		if ((ap = anon_get_next_ptr(old, &index)) == NULL)
1182 			break;
1183 
1184 		ASSERT(!ANON_ISBUSY(anon_get_slot(old, index)));
1185 		off = index - old_idx;
1186 		npages -= off;
1187 		if (npages <= 0)
1188 			break;
1189 
1190 		(void) anon_set_ptr(new, new_idx + off, ap, ANON_SLEEP);
1191 		ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
1192 
1193 		mutex_enter(ahm);
1194 		ap->an_refcnt++;
1195 		mutex_exit(ahm);
1196 
1197 		off++;
1198 		new_idx += off;
1199 		old_idx += off;
1200 		npages--;
1201 	}
1202 }
1203 
1204 /*
1205  * Just like anon_dup but also guarantees there are no holes (unallocated anon
1206  * slots) within any large page region. That means if a large page region is
1207  * empty in the old array it will skip it. If there are 1 or more valid slots
1208  * in the large page region of the old array it will make sure to fill in any
1209  * unallocated ones and also copy them to the new array. If noalloc is 1 large
1210  * page region should either have no valid anon slots or all slots should be
1211  * valid.
1212  */
1213 void
1214 anon_dup_fill_holes(
1215 	struct anon_hdr *old,
1216 	ulong_t old_idx,
1217 	struct anon_hdr *new,
1218 	ulong_t new_idx,
1219 	size_t size,
1220 	uint_t szc,
1221 	int noalloc)
1222 {
1223 	struct anon	*ap;
1224 	spgcnt_t	npages;
1225 	kmutex_t	*ahm, *ahmpages = NULL;
1226 	pgcnt_t		pgcnt, i;
1227 	ulong_t		index, off;
1228 #ifdef DEBUG
1229 	int		refcnt;
1230 #endif
1231 
1232 	ASSERT(szc != 0);
1233 	pgcnt = page_get_pagecnt(szc);
1234 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
1235 	npages = btopr(size);
1236 	ASSERT(IS_P2ALIGNED(npages, pgcnt));
1237 	ASSERT(IS_P2ALIGNED(old_idx, pgcnt));
1238 
1239 	VM_STAT_ADD(anonvmstats.dupfillholes[0]);
1240 
1241 	while (npages > 0) {
1242 		index = old_idx;
1243 
1244 		/*
1245 		 * Find the next valid slot.
1246 		 */
1247 		if (anon_get_next_ptr(old, &index) == NULL)
1248 			break;
1249 
1250 		ASSERT(!ANON_ISBUSY(anon_get_slot(old, index)));
1251 		/*
1252 		 * Now backup index to the beginning of the
1253 		 * current large page region of the old array.
1254 		 */
1255 		index = P2ALIGN(index, pgcnt);
1256 		off = index - old_idx;
1257 		ASSERT(IS_P2ALIGNED(off, pgcnt));
1258 		npages -= off;
1259 		if (npages <= 0)
1260 			break;
1261 
1262 		/*
1263 		 * Fill and copy a large page regions worth
1264 		 * of anon slots.
1265 		 */
1266 		for (i = 0; i < pgcnt; i++) {
1267 			if ((ap = anon_get_ptr(old, index + i)) == NULL) {
1268 				if (noalloc) {
1269 					panic("anon_dup_fill_holes: "
1270 					    "empty anon slot\n");
1271 				}
1272 				VM_STAT_ADD(anonvmstats.dupfillholes[1]);
1273 				ap = anon_alloc(NULL, 0);
1274 				(void) anon_set_ptr(old, index + i, ap,
1275 				    ANON_SLEEP);
1276 			} else if (i == 0) {
1277 				/*
1278 				 * make the increment of all refcnts of all
1279 				 * anon slots of a large page appear atomic by
1280 				 * getting an anonpages_hash_lock for the
1281 				 * first anon slot of a large page.
1282 				 */
1283 				int hash = AH_LOCK(ap->an_vp, ap->an_off);
1284 
1285 				VM_STAT_ADD(anonvmstats.dupfillholes[2]);
1286 
1287 				ahmpages = &anonpages_hash_lock[hash];
1288 				mutex_enter(ahmpages);
1289 				/*LINTED*/
1290 				ASSERT(refcnt = ap->an_refcnt);
1291 
1292 				VM_STAT_COND_ADD(ap->an_refcnt > 1,
1293 				    anonvmstats.dupfillholes[3]);
1294 			}
1295 			(void) anon_set_ptr(new, new_idx + off + i, ap,
1296 			    ANON_SLEEP);
1297 			ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
1298 			mutex_enter(ahm);
1299 			ASSERT(ahmpages != NULL || ap->an_refcnt == 1);
1300 			ASSERT(i == 0 || ahmpages == NULL ||
1301 			    refcnt == ap->an_refcnt);
1302 			ap->an_refcnt++;
1303 			mutex_exit(ahm);
1304 		}
1305 		if (ahmpages != NULL) {
1306 			mutex_exit(ahmpages);
1307 			ahmpages = NULL;
1308 		}
1309 		off += pgcnt;
1310 		new_idx += off;
1311 		old_idx += off;
1312 		npages -= pgcnt;
1313 	}
1314 }
1315 
1316 /*
1317  * Used when a segment with a vnode changes szc. similarly to
1318  * anon_dup_fill_holes() makes sure each large page region either has no anon
1319  * slots or all of them. but new slots are created by COWing the file
1320  * pages. on entrance no anon slots should be shared.
1321  */
1322 int
1323 anon_fill_cow_holes(
1324 	struct seg *seg,
1325 	caddr_t addr,
1326 	struct anon_hdr *ahp,
1327 	ulong_t an_idx,
1328 	struct vnode *vp,
1329 	u_offset_t vp_off,
1330 	size_t size,
1331 	uint_t szc,
1332 	uint_t prot,
1333 	struct vpage vpage[],
1334 	struct cred *cred)
1335 {
1336 	struct anon	*ap;
1337 	spgcnt_t	npages;
1338 	pgcnt_t		pgcnt, i;
1339 	ulong_t		index, off;
1340 	int		err = 0;
1341 	int		pageflags = 0;
1342 
1343 	ASSERT(szc != 0);
1344 	pgcnt = page_get_pagecnt(szc);
1345 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
1346 	npages = btopr(size);
1347 	ASSERT(IS_P2ALIGNED(npages, pgcnt));
1348 	ASSERT(IS_P2ALIGNED(an_idx, pgcnt));
1349 
1350 	while (npages > 0) {
1351 		index = an_idx;
1352 
1353 		/*
1354 		 * Find the next valid slot.
1355 		 */
1356 		if (anon_get_next_ptr(ahp, &index) == NULL) {
1357 			break;
1358 		}
1359 
1360 		ASSERT(!ANON_ISBUSY(anon_get_slot(ahp, index)));
1361 		/*
1362 		 * Now backup index to the beginning of the
1363 		 * current large page region of the anon array.
1364 		 */
1365 		index = P2ALIGN(index, pgcnt);
1366 		off = index - an_idx;
1367 		ASSERT(IS_P2ALIGNED(off, pgcnt));
1368 		npages -= off;
1369 		if (npages <= 0)
1370 			break;
1371 		an_idx += off;
1372 		vp_off += ptob(off);
1373 		addr += ptob(off);
1374 		if (vpage != NULL) {
1375 			vpage += off;
1376 		}
1377 
1378 		for (i = 0; i < pgcnt; i++, an_idx++, vp_off += PAGESIZE) {
1379 			if ((ap = anon_get_ptr(ahp, an_idx)) == NULL) {
1380 				page_t *pl[1 + 1];
1381 				page_t *pp;
1382 
1383 				err = VOP_GETPAGE(vp, vp_off, PAGESIZE, NULL,
1384 				    pl, PAGESIZE, seg, addr, S_READ, cred);
1385 				if (err) {
1386 					break;
1387 				}
1388 				if (vpage != NULL) {
1389 					prot = VPP_PROT(vpage);
1390 					pageflags = VPP_ISPPLOCK(vpage) ?
1391 					    LOCK_PAGE : 0;
1392 				}
1393 				pp = anon_private(&ap, seg, addr, prot, pl[0],
1394 					pageflags, cred);
1395 				if (pp == NULL) {
1396 					err = ENOMEM;
1397 					break;
1398 				}
1399 				(void) anon_set_ptr(ahp, an_idx, ap,
1400 				    ANON_SLEEP);
1401 				page_unlock(pp);
1402 			}
1403 			ASSERT(ap->an_refcnt == 1);
1404 			addr += PAGESIZE;
1405 			if (vpage != NULL) {
1406 				vpage++;
1407 			}
1408 		}
1409 		npages -= pgcnt;
1410 	}
1411 
1412 	return (err);
1413 }
1414 
1415 /*
1416  * Free a group of "size" anon pages, size in bytes,
1417  * and clear out the pointers to the anon entries.
1418  */
1419 void
1420 anon_free(struct anon_hdr *ahp, ulong_t index, size_t size)
1421 {
1422 	spgcnt_t npages;
1423 	struct anon *ap;
1424 	ulong_t old;
1425 
1426 	npages = btopr(size);
1427 
1428 	while (npages > 0) {
1429 		old = index;
1430 		if ((ap = anon_get_next_ptr(ahp, &index)) == NULL)
1431 			break;
1432 
1433 		ASSERT(!ANON_ISBUSY(anon_get_slot(ahp, index)));
1434 		npages -= index - old;
1435 		if (npages <= 0)
1436 			break;
1437 
1438 		(void) anon_set_ptr(ahp, index, NULL, ANON_SLEEP);
1439 		anon_decref(ap);
1440 		/*
1441 		 * Bump index and decrement page count
1442 		 */
1443 		index++;
1444 		npages--;
1445 	}
1446 }
1447 
1448 void
1449 anon_free_pages(
1450 	struct anon_hdr *ahp,
1451 	ulong_t an_idx,
1452 	size_t size,
1453 	uint_t szc)
1454 {
1455 	spgcnt_t	npages;
1456 	pgcnt_t		pgcnt;
1457 	ulong_t		index, off;
1458 
1459 	ASSERT(szc != 0);
1460 	pgcnt = page_get_pagecnt(szc);
1461 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
1462 	npages = btopr(size);
1463 	ASSERT(IS_P2ALIGNED(npages, pgcnt));
1464 	ASSERT(IS_P2ALIGNED(an_idx, pgcnt));
1465 
1466 	VM_STAT_ADD(anonvmstats.freepages[0]);
1467 
1468 	while (npages > 0) {
1469 		index = an_idx;
1470 
1471 		/*
1472 		 * Find the next valid slot.
1473 		 */
1474 		if (anon_get_next_ptr(ahp, &index) == NULL)
1475 			break;
1476 
1477 		ASSERT(!ANON_ISBUSY(anon_get_slot(ahp, index)));
1478 		/*
1479 		 * Now backup index to the beginning of the
1480 		 * current large page region of the old array.
1481 		 */
1482 		index = P2ALIGN(index, pgcnt);
1483 		off = index - an_idx;
1484 		ASSERT(IS_P2ALIGNED(off, pgcnt));
1485 		npages -= off;
1486 		if (npages <= 0)
1487 			break;
1488 
1489 		anon_decref_pages(ahp, index, szc);
1490 
1491 		off += pgcnt;
1492 		an_idx += off;
1493 		npages -= pgcnt;
1494 	}
1495 }
1496 
1497 /*
1498  * Make anonymous pages discardable
1499  */
1500 void
1501 anon_disclaim(struct anon_map *amp, ulong_t index, size_t size, int flags)
1502 {
1503 	spgcnt_t npages = btopr(size);
1504 	struct anon *ap;
1505 	struct vnode *vp;
1506 	anoff_t off;
1507 	page_t *pp, *root_pp;
1508 	kmutex_t *ahm;
1509 	pgcnt_t pgcnt;
1510 	ulong_t old_idx, idx, i;
1511 	struct anon_hdr *ahp = amp->ahp;
1512 	anon_sync_obj_t cookie;
1513 
1514 	ASSERT(RW_READ_HELD(&amp->a_rwlock));
1515 	pgcnt = 1;
1516 	for (; npages > 0; index = (pgcnt == 1) ? index + 1:
1517 		P2ROUNDUP(index + 1, pgcnt), npages -= pgcnt) {
1518 
1519 		/*
1520 		 * get anon pointer and index for the first valid entry
1521 		 * in the anon list, starting from "index"
1522 		 */
1523 		old_idx = index;
1524 		if ((ap = anon_get_next_ptr(ahp, &index)) == NULL)
1525 			break;
1526 
1527 		/*
1528 		 * decrement npages by number of NULL anon slots we skipped
1529 		 */
1530 		npages -= index - old_idx;
1531 		if (npages <= 0)
1532 			break;
1533 
1534 		anon_array_enter(amp, index, &cookie);
1535 		ap = anon_get_ptr(ahp, index);
1536 		ASSERT(ap != NULL);
1537 
1538 		/*
1539 		 * Get anonymous page and try to lock it SE_EXCL;
1540 		 * For non blocking case if we couldn't grab the lock
1541 		 * we skip to next page.
1542 		 * For blocking case (ANON_PGLOOKUP_BLK) block
1543 		 * until we grab SE_EXCL lock.
1544 		 */
1545 		swap_xlate(ap, &vp, &off);
1546 		if (flags & ANON_PGLOOKUP_BLK)
1547 			pp = page_lookup_create(vp, (u_offset_t)off,
1548 			    SE_EXCL, NULL, NULL, SE_EXCL_WANTED);
1549 		else
1550 			pp = page_lookup_nowait(vp, (u_offset_t)off, SE_EXCL);
1551 		if (pp == NULL) {
1552 			segadvstat.MADV_FREE_miss.value.ul++;
1553 			pgcnt = 1;
1554 			anon_array_exit(&cookie);
1555 			continue;
1556 		}
1557 		pgcnt = page_get_pagecnt(pp->p_szc);
1558 
1559 		/*
1560 		 * we cannot free a page which is permanently locked.
1561 		 * The page_struct_lock need not be acquired to examine
1562 		 * these fields since the page has an "exclusive" lock.
1563 		 */
1564 		if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
1565 			page_unlock(pp);
1566 			segadvstat.MADV_FREE_miss.value.ul++;
1567 			anon_array_exit(&cookie);
1568 			continue;
1569 		}
1570 
1571 		ahm = &anonhash_lock[AH_LOCK(vp, off)];
1572 		mutex_enter(ahm);
1573 		ASSERT(ap->an_refcnt != 0);
1574 		/*
1575 		 * skip this one if copy-on-write is not yet broken.
1576 		 */
1577 		if (ap->an_refcnt > 1) {
1578 			mutex_exit(ahm);
1579 			page_unlock(pp);
1580 			segadvstat.MADV_FREE_miss.value.ul++;
1581 			anon_array_exit(&cookie);
1582 			continue;
1583 		}
1584 
1585 		if (pp->p_szc == 0) {
1586 			pgcnt = 1;
1587 
1588 			/*
1589 			 * free swap slot;
1590 			 */
1591 			if (ap->an_pvp) {
1592 				swap_phys_free(ap->an_pvp, ap->an_poff,
1593 				    PAGESIZE);
1594 				ap->an_pvp = NULL;
1595 				ap->an_poff = 0;
1596 			}
1597 			mutex_exit(ahm);
1598 			segadvstat.MADV_FREE_hit.value.ul++;
1599 
1600 			/*
1601 			 * while we are at it, unload all the translations
1602 			 * and attempt to free the page.
1603 			 */
1604 			(void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
1605 			/*LINTED: constant in conditional context */
1606 			VN_DISPOSE(pp, B_FREE, 0, kcred);
1607 			anon_array_exit(&cookie);
1608 			continue;
1609 		}
1610 
1611 		pgcnt = page_get_pagecnt(pp->p_szc);
1612 		if (!IS_P2ALIGNED(index, pgcnt)) {
1613 			if (!page_try_demote_pages(pp)) {
1614 				mutex_exit(ahm);
1615 				page_unlock(pp);
1616 				segadvstat.MADV_FREE_miss.value.ul++;
1617 				anon_array_exit(&cookie);
1618 				continue;
1619 			} else {
1620 				pgcnt = 1;
1621 				if (ap->an_pvp) {
1622 					swap_phys_free(ap->an_pvp,
1623 					    ap->an_poff, PAGESIZE);
1624 					    ap->an_pvp = NULL;
1625 					    ap->an_poff = 0;
1626 				}
1627 				mutex_exit(ahm);
1628 				(void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
1629 				/*LINTED*/
1630 				VN_DISPOSE(pp, B_FREE, 0, kcred);
1631 				segadvstat.MADV_FREE_hit.value.ul++;
1632 				anon_array_exit(&cookie);
1633 				continue;
1634 			}
1635 		}
1636 		mutex_exit(ahm);
1637 		root_pp = pp;
1638 
1639 		/*
1640 		 * try to lock remaining pages
1641 		 */
1642 		for (idx = 1; idx < pgcnt; idx++) {
1643 			pp++;
1644 			if (!page_trylock(pp, SE_EXCL))
1645 				break;
1646 			if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
1647 				page_unlock(pp);
1648 				break;
1649 			}
1650 		}
1651 
1652 		if (idx == pgcnt) {
1653 			for (i = 0; i < pgcnt; i++) {
1654 				ap = anon_get_ptr(ahp, index + i);
1655 				if (ap == NULL)
1656 					break;
1657 				swap_xlate(ap, &vp, &off);
1658 				ahm = &anonhash_lock[AH_LOCK(vp, off)];
1659 				mutex_enter(ahm);
1660 				ASSERT(ap->an_refcnt != 0);
1661 
1662 				/*
1663 				 * skip this one if copy-on-write
1664 				 * is not yet broken.
1665 				 */
1666 				if (ap->an_refcnt > 1) {
1667 					mutex_exit(ahm);
1668 					goto skiplp;
1669 				}
1670 				if (ap->an_pvp) {
1671 					swap_phys_free(ap->an_pvp,
1672 					    ap->an_poff, PAGESIZE);
1673 					    ap->an_pvp = NULL;
1674 					    ap->an_poff = 0;
1675 				}
1676 				mutex_exit(ahm);
1677 			}
1678 			page_destroy_pages(root_pp);
1679 			segadvstat.MADV_FREE_hit.value.ul += pgcnt;
1680 			anon_array_exit(&cookie);
1681 			continue;
1682 		}
1683 skiplp:
1684 		segadvstat.MADV_FREE_miss.value.ul += pgcnt;
1685 		for (i = 0, pp = root_pp; i < idx; pp++, i++)
1686 			page_unlock(pp);
1687 		anon_array_exit(&cookie);
1688 	}
1689 }
1690 
1691 /*
1692  * Return the kept page(s) and protections back to the segment driver.
1693  */
1694 int
1695 anon_getpage(
1696 	struct anon **app,
1697 	uint_t *protp,
1698 	page_t *pl[],
1699 	size_t plsz,
1700 	struct seg *seg,
1701 	caddr_t addr,
1702 	enum seg_rw rw,
1703 	struct cred *cred)
1704 {
1705 	page_t *pp;
1706 	struct anon *ap = *app;
1707 	struct vnode *vp;
1708 	anoff_t off;
1709 	int err;
1710 	kmutex_t *ahm;
1711 
1712 	swap_xlate(ap, &vp, &off);
1713 
1714 	/*
1715 	 * Lookup the page. If page is being paged in,
1716 	 * wait for it to finish as we must return a list of
1717 	 * pages since this routine acts like the VOP_GETPAGE
1718 	 * routine does.
1719 	 */
1720 	if (pl != NULL && (pp = page_lookup(vp, (u_offset_t)off, SE_SHARED))) {
1721 		ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
1722 		mutex_enter(ahm);
1723 		if (ap->an_refcnt == 1)
1724 			*protp = PROT_ALL;
1725 		else
1726 			*protp = PROT_ALL & ~PROT_WRITE;
1727 		mutex_exit(ahm);
1728 		pl[0] = pp;
1729 		pl[1] = NULL;
1730 		return (0);
1731 	}
1732 
1733 	/*
1734 	 * Simply treat it as a vnode fault on the anon vp.
1735 	 */
1736 
1737 	TRACE_3(TR_FAC_VM, TR_ANON_GETPAGE,
1738 		"anon_getpage:seg %x addr %x vp %x",
1739 		seg, addr, vp);
1740 
1741 	err = VOP_GETPAGE(vp, (u_offset_t)off, PAGESIZE, protp, pl, plsz,
1742 	    seg, addr, rw, cred);
1743 
1744 	if (err == 0 && pl != NULL) {
1745 		ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
1746 		mutex_enter(ahm);
1747 		if (ap->an_refcnt != 1)
1748 			*protp &= ~PROT_WRITE;	/* make read-only */
1749 		mutex_exit(ahm);
1750 	}
1751 	return (err);
1752 }
1753 
1754 /*
1755  * Creates or returns kept pages to the segment driver.  returns -1 if a large
1756  * page cannot be allocated. returns -2 if some other process has allocated a
1757  * larger page.
1758  *
1759  * For cowfault it will alocate any size pages to fill the requested area to
1760  * avoid partially overwritting anon slots (i.e. sharing only some of the anon
1761  * slots within a large page with other processes). This policy greatly
1762  * simplifies large page freeing (which is only freed when all anon slot
1763  * refcnts are 0).
1764  */
1765 int
1766 anon_map_getpages(
1767 	struct anon_map *amp,
1768 	ulong_t	start_idx,
1769 	uint_t	szc,
1770 	struct seg *seg,
1771 	caddr_t	addr,
1772 	uint_t prot,
1773 	uint_t *protp,
1774 	page_t	*ppa[],
1775 	uint_t	*ppa_szc,
1776 	struct vpage vpage[],
1777 	enum seg_rw rw,
1778 	int brkcow,
1779 	int anypgsz,
1780 	struct cred *cred)
1781 {
1782 	pgcnt_t		pgcnt;
1783 	struct anon	*ap;
1784 	struct vnode	*vp;
1785 	anoff_t		off;
1786 	page_t		*pp, *pl[2], *conpp = NULL;
1787 	caddr_t		vaddr;
1788 	ulong_t		pg_idx, an_idx, i;
1789 	spgcnt_t	nreloc = 0;
1790 	int		prealloc = 1;
1791 	int		err, slotcreate;
1792 	uint_t		vpprot;
1793 
1794 #if !defined(__i386) && !defined(__amd64)
1795 	ASSERT(seg->s_szc != 0);
1796 #endif
1797 	ASSERT(szc <= seg->s_szc);
1798 	ASSERT(ppa_szc != NULL);
1799 	ASSERT(rw != S_CREATE);
1800 
1801 	*protp = PROT_ALL;
1802 
1803 	VM_STAT_ADD(anonvmstats.getpages[0]);
1804 
1805 	if (szc == 0) {
1806 		VM_STAT_ADD(anonvmstats.getpages[1]);
1807 		if ((ap = anon_get_ptr(amp->ahp, start_idx)) != NULL) {
1808 			err = anon_getpage(&ap, protp, pl, PAGESIZE, seg,
1809 			    addr, rw, cred);
1810 			if (err)
1811 				return (err);
1812 			ppa[0] = pl[0];
1813 			if (brkcow == 0 || (*protp & PROT_WRITE)) {
1814 				VM_STAT_ADD(anonvmstats.getpages[2]);
1815 				if (ppa[0]->p_szc != 0) {
1816 					VM_STAT_ADD(anonvmstats.getpages[3]);
1817 					*ppa_szc = ppa[0]->p_szc;
1818 					page_unlock(ppa[0]);
1819 					return (-2);
1820 				}
1821 				return (0);
1822 			}
1823 			panic("anon_map_getpages: cowfault for szc 0");
1824 		} else {
1825 			VM_STAT_ADD(anonvmstats.getpages[4]);
1826 			ppa[0] = anon_zero(seg, addr, &ap, cred);
1827 			if (ppa[0] == NULL)
1828 				return (ENOMEM);
1829 			(void) anon_set_ptr(amp->ahp, start_idx, ap,
1830 			    ANON_SLEEP);
1831 			return (0);
1832 		}
1833 	}
1834 
1835 	pgcnt = page_get_pagecnt(szc);
1836 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
1837 	ASSERT(IS_P2ALIGNED(start_idx, pgcnt));
1838 
1839 	/*
1840 	 * First we check for the case that the requtested large
1841 	 * page or larger page already exists in the system.
1842 	 * Actually we only check if the first constituent page
1843 	 * exists and only preallocate if it's not found.
1844 	 */
1845 	ap = anon_get_ptr(amp->ahp, start_idx);
1846 	if (ap) {
1847 		uint_t pszc;
1848 		swap_xlate(ap, &vp, &off);
1849 		if (page_exists_forreal(vp, (u_offset_t)off, &pszc)) {
1850 			if (pszc > szc) {
1851 				*ppa_szc = pszc;
1852 				return (-2);
1853 			}
1854 			if (pszc == szc) {
1855 				prealloc = 0;
1856 			}
1857 		}
1858 	}
1859 
1860 	VM_STAT_COND_ADD(prealloc == 0, anonvmstats.getpages[5]);
1861 	VM_STAT_COND_ADD(prealloc != 0, anonvmstats.getpages[6]);
1862 
1863 top:
1864 	/*
1865 	 * If a smaller page or no page at all was found,
1866 	 * grab a large page off the freelist.
1867 	 */
1868 	if (prealloc) {
1869 		ASSERT(conpp == NULL);
1870 		if (page_alloc_pages(anon_vp, seg, addr, NULL, ppa,
1871 		    szc, 0) != 0) {
1872 			VM_STAT_ADD(anonvmstats.getpages[7]);
1873 			if (brkcow == 0 ||
1874 			    !anon_share(amp->ahp, start_idx, pgcnt)) {
1875 				/*
1876 				 * If the refcnt's of all anon slots are <= 1
1877 				 * they can't increase since we are holding
1878 				 * the address space's lock. So segvn can
1879 				 * safely decrease szc without risking to
1880 				 * generate a cow fault for the region smaller
1881 				 * than the segment's largest page size.
1882 				 */
1883 				VM_STAT_ADD(anonvmstats.getpages[8]);
1884 				return (-1);
1885 			}
1886 		docow:
1887 			/*
1888 			 * This is a cow fault. Copy away the entire 1 large
1889 			 * page region of this segment.
1890 			 */
1891 			if (szc != seg->s_szc)
1892 				panic("anon_map_getpages: cowfault for szc %d",
1893 				    szc);
1894 			vaddr = addr;
1895 			for (pg_idx = 0, an_idx = start_idx; pg_idx < pgcnt;
1896 			    pg_idx++, an_idx++, vaddr += PAGESIZE) {
1897 				if ((ap = anon_get_ptr(amp->ahp, an_idx)) !=
1898 				    NULL) {
1899 					err = anon_getpage(&ap, &vpprot, pl,
1900 					    PAGESIZE, seg, vaddr, rw, cred);
1901 					if (err) {
1902 						for (i = 0; i < pg_idx; i++) {
1903 							if ((pp = ppa[i]) !=
1904 							    NULL)
1905 								page_unlock(pp);
1906 						}
1907 						return (err);
1908 					}
1909 					ppa[pg_idx] = pl[0];
1910 				} else {
1911 					/*
1912 					 * Since this is a cowfault we know
1913 					 * that this address space has a
1914 					 * parent or children which means
1915 					 * anon_dup_fill_holes() has initialized
1916 					 * all anon slots within a large page
1917 					 * region that had at least one anon
1918 					 * slot at the time of fork().
1919 					 */
1920 					panic("anon_map_getpages: "
1921 					    "cowfault but anon slot is empty");
1922 				}
1923 			}
1924 			VM_STAT_ADD(anonvmstats.getpages[9]);
1925 			*protp = PROT_ALL;
1926 			return (anon_map_privatepages(amp, start_idx, szc, seg,
1927 			    addr, prot, ppa, vpage, anypgsz, cred));
1928 		}
1929 	}
1930 
1931 	VM_STAT_ADD(anonvmstats.getpages[10]);
1932 
1933 	an_idx = start_idx;
1934 	pg_idx = 0;
1935 	vaddr = addr;
1936 	while (pg_idx < pgcnt) {
1937 		slotcreate = 0;
1938 		if ((ap = anon_get_ptr(amp->ahp, an_idx)) == NULL) {
1939 			VM_STAT_ADD(anonvmstats.getpages[11]);
1940 			/*
1941 			 * For us to have decided not to preallocate
1942 			 * would have meant that a large page
1943 			 * was found. Which also means that all of the
1944 			 * anon slots for that page would have been
1945 			 * already created for us.
1946 			 */
1947 			if (prealloc == 0)
1948 				panic("anon_map_getpages: prealloc = 0");
1949 
1950 			slotcreate = 1;
1951 			ap = anon_alloc(NULL, 0);
1952 		}
1953 		swap_xlate(ap, &vp, &off);
1954 
1955 		/*
1956 		 * Now setup our preallocated page to pass down
1957 		 * to swap_getpage().
1958 		 */
1959 		if (prealloc) {
1960 			ASSERT(ppa[pg_idx]->p_szc == szc);
1961 			conpp = ppa[pg_idx];
1962 		}
1963 		ASSERT(prealloc || conpp == NULL);
1964 
1965 		/*
1966 		 * If we just created this anon slot then call
1967 		 * with S_CREATE to prevent doing IO on the page.
1968 		 * Similar to the anon_zero case.
1969 		 */
1970 		err = swap_getconpage(vp, (u_offset_t)off, PAGESIZE,
1971 		    NULL, pl, PAGESIZE, conpp, &nreloc, seg, vaddr,
1972 		    slotcreate == 1 ? S_CREATE : rw, cred);
1973 
1974 		if (err) {
1975 			VM_STAT_ADD(anonvmstats.getpages[12]);
1976 			ASSERT(slotcreate == 0);
1977 			goto io_err;
1978 		}
1979 
1980 		pp = pl[0];
1981 
1982 		if (pp->p_szc != szc) {
1983 			VM_STAT_ADD(anonvmstats.getpages[13]);
1984 			ASSERT(slotcreate == 0);
1985 			ASSERT(prealloc == 0);
1986 			ASSERT(pg_idx == 0);
1987 			if (pp->p_szc > szc) {
1988 				page_unlock(pp);
1989 				VM_STAT_ADD(anonvmstats.getpages[14]);
1990 				return (-2);
1991 			}
1992 			page_unlock(pp);
1993 			prealloc = 1;
1994 			goto top;
1995 		}
1996 
1997 		/*
1998 		 * If we decided to preallocate but VOP_GETPAGE
1999 		 * found a page in the system that satisfies our
2000 		 * request then free up our preallocated large page
2001 		 * and continue looping accross the existing large
2002 		 * page via VOP_GETPAGE.
2003 		 */
2004 		if (prealloc && pp != ppa[pg_idx]) {
2005 			VM_STAT_ADD(anonvmstats.getpages[15]);
2006 			ASSERT(slotcreate == 0);
2007 			ASSERT(pg_idx == 0);
2008 			conpp = NULL;
2009 			prealloc = 0;
2010 			page_free_pages(ppa[0]);
2011 		}
2012 
2013 		if (prealloc && nreloc > 1) {
2014 			/*
2015 			 * we have relocated out of a smaller large page.
2016 			 * skip npgs - 1 iterations and continue which will
2017 			 * increment by one the loop indices.
2018 			 */
2019 			spgcnt_t npgs = nreloc;
2020 
2021 			VM_STAT_ADD(anonvmstats.getpages[16]);
2022 
2023 			ASSERT(pp == ppa[pg_idx]);
2024 			ASSERT(slotcreate == 0);
2025 			ASSERT(pg_idx + npgs <= pgcnt);
2026 			if ((*protp & PROT_WRITE) &&
2027 			    anon_share(amp->ahp, an_idx, npgs)) {
2028 			    *protp &= ~PROT_WRITE;
2029 			}
2030 			pg_idx += npgs;
2031 			an_idx += npgs;
2032 			vaddr += PAGESIZE * npgs;
2033 			continue;
2034 		}
2035 
2036 		VM_STAT_ADD(anonvmstats.getpages[17]);
2037 
2038 		/*
2039 		 * Anon_zero case.
2040 		 */
2041 		if (slotcreate) {
2042 			ASSERT(prealloc);
2043 			pagezero(pp, 0, PAGESIZE);
2044 			CPU_STATS_ADD_K(vm, zfod, 1);
2045 			hat_setrefmod(pp);
2046 		}
2047 
2048 		ASSERT(prealloc == 0 || ppa[pg_idx] == pp);
2049 		ASSERT(prealloc != 0 || PAGE_SHARED(pp));
2050 		ASSERT(prealloc == 0 || PAGE_EXCL(pp));
2051 
2052 		if (pg_idx > 0 &&
2053 		    ((page_pptonum(pp) != page_pptonum(ppa[pg_idx - 1]) + 1) ||
2054 		    (pp->p_szc != ppa[pg_idx - 1]->p_szc)))
2055 			panic("anon_map_getpages: unexpected page");
2056 
2057 		if (prealloc == 0) {
2058 			ppa[pg_idx] = pp;
2059 		}
2060 
2061 		if (ap->an_refcnt > 1) {
2062 			VM_STAT_ADD(anonvmstats.getpages[18]);
2063 			*protp &= ~PROT_WRITE;
2064 		}
2065 
2066 		/*
2067 		 * If this is a new anon slot then initialize
2068 		 * the anon array entry.
2069 		 */
2070 		if (slotcreate) {
2071 			(void) anon_set_ptr(amp->ahp, an_idx, ap, ANON_SLEEP);
2072 		}
2073 		pg_idx++;
2074 		an_idx++;
2075 		vaddr += PAGESIZE;
2076 	}
2077 
2078 	/*
2079 	 * Since preallocated pages come off the freelist
2080 	 * they are locked SE_EXCL. Simply downgrade and return.
2081 	 */
2082 	if (prealloc) {
2083 		VM_STAT_ADD(anonvmstats.getpages[19]);
2084 		conpp = NULL;
2085 		for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) {
2086 			page_downgrade(ppa[pg_idx]);
2087 		}
2088 	}
2089 	ASSERT(conpp == NULL);
2090 
2091 	if (brkcow == 0 || (*protp & PROT_WRITE)) {
2092 		VM_STAT_ADD(anonvmstats.getpages[20]);
2093 		return (0);
2094 	}
2095 
2096 	if (szc < seg->s_szc)
2097 		panic("anon_map_getpages: cowfault for szc %d", szc);
2098 
2099 	VM_STAT_ADD(anonvmstats.getpages[21]);
2100 
2101 	*protp = PROT_ALL;
2102 	return (anon_map_privatepages(amp, start_idx, szc, seg, addr, prot,
2103 	    ppa, vpage, anypgsz, cred));
2104 io_err:
2105 	/*
2106 	 * We got an IO error somewhere in our large page.
2107 	 * If we were using a preallocated page then just demote
2108 	 * all the constituent pages that we've succeeded with sofar
2109 	 * to PAGESIZE pages and leave them in the system
2110 	 * unlocked.
2111 	 */
2112 
2113 	ASSERT(err != -2 || pg_idx == 0);
2114 
2115 	VM_STAT_COND_ADD(err > 0, anonvmstats.getpages[22]);
2116 	VM_STAT_COND_ADD(err == -1, anonvmstats.getpages[23]);
2117 	VM_STAT_COND_ADD(err == -2, anonvmstats.getpages[24]);
2118 
2119 	if (prealloc) {
2120 		conpp = NULL;
2121 		if (pg_idx > 0) {
2122 			VM_STAT_ADD(anonvmstats.getpages[25]);
2123 			for (i = 0; i < pgcnt; i++) {
2124 				pp = ppa[i];
2125 				ASSERT(PAGE_EXCL(pp));
2126 				ASSERT(pp->p_szc == szc);
2127 				pp->p_szc = 0;
2128 			}
2129 			for (i = 0; i < pg_idx; i++) {
2130 				ASSERT(!hat_page_is_mapped(ppa[i]));
2131 				page_unlock(ppa[i]);
2132 			}
2133 			/*
2134 			 * Now free up the remaining unused constituent
2135 			 * pages.
2136 			 */
2137 			while (pg_idx < pgcnt) {
2138 				ASSERT(!hat_page_is_mapped(ppa[pg_idx]));
2139 				page_free(ppa[pg_idx], 0);
2140 				pg_idx++;
2141 			}
2142 		} else {
2143 			VM_STAT_ADD(anonvmstats.getpages[26]);
2144 			page_free_pages(ppa[0]);
2145 		}
2146 	} else {
2147 		VM_STAT_ADD(anonvmstats.getpages[27]);
2148 		ASSERT(err > 0);
2149 		for (i = 0; i < pg_idx; i++)
2150 			page_unlock(ppa[i]);
2151 	}
2152 	ASSERT(conpp == NULL);
2153 	if (err != -1)
2154 		return (err);
2155 	/*
2156 	 * we are here because we failed to relocate.
2157 	 */
2158 	ASSERT(prealloc);
2159 	if (brkcow == 0 || !anon_share(amp->ahp, start_idx, pgcnt)) {
2160 		VM_STAT_ADD(anonvmstats.getpages[28]);
2161 		return (-1);
2162 	}
2163 	VM_STAT_ADD(anonvmstats.getpages[29]);
2164 	goto docow;
2165 }
2166 
2167 
2168 /*
2169  * Turn a reference to an object or shared anon page
2170  * into a private page with a copy of the data from the
2171  * original page which is always locked by the caller.
2172  * This routine unloads the translation and unlocks the
2173  * original page, if it isn't being stolen, before returning
2174  * to the caller.
2175  *
2176  * NOTE:  The original anon slot is not freed by this routine
2177  *	  It must be freed by the caller while holding the
2178  *	  "anon_map" lock to prevent races which can occur if
2179  *	  a process has multiple lwps in its address space.
2180  */
2181 page_t *
2182 anon_private(
2183 	struct anon **app,
2184 	struct seg *seg,
2185 	caddr_t addr,
2186 	uint_t	prot,
2187 	page_t *opp,
2188 	int oppflags,
2189 	struct cred *cred)
2190 {
2191 	struct anon *old = *app;
2192 	struct anon *new;
2193 	page_t *pp = NULL;
2194 	struct vnode *vp;
2195 	anoff_t off;
2196 	page_t *anon_pl[1 + 1];
2197 	int err;
2198 
2199 	if (oppflags & STEAL_PAGE)
2200 		ASSERT(PAGE_EXCL(opp));
2201 	else
2202 		ASSERT(PAGE_LOCKED(opp));
2203 
2204 	CPU_STATS_ADD_K(vm, cow_fault, 1);
2205 
2206 	/* Kernel probe */
2207 	TNF_PROBE_1(anon_private, "vm pagefault", /* CSTYLED */,
2208 		tnf_opaque,	address,	addr);
2209 
2210 	*app = new = anon_alloc(NULL, 0);
2211 	swap_xlate(new, &vp, &off);
2212 
2213 	if (oppflags & STEAL_PAGE) {
2214 		page_rename(opp, vp, (u_offset_t)off);
2215 		pp = opp;
2216 		TRACE_5(TR_FAC_VM, TR_ANON_PRIVATE,
2217 			"anon_private:seg %p addr %x pp %p vp %p off %lx",
2218 			seg, addr, pp, vp, off);
2219 		hat_setmod(pp);
2220 
2221 		/* bug 4026339 */
2222 		page_downgrade(pp);
2223 		return (pp);
2224 	}
2225 
2226 	/*
2227 	 * Call the VOP_GETPAGE routine to create the page, thereby
2228 	 * enabling the vnode driver to allocate any filesystem
2229 	 * space (e.g., disk block allocation for UFS).  This also
2230 	 * prevents more than one page from being added to the
2231 	 * vnode at the same time.
2232 	 */
2233 	err = VOP_GETPAGE(vp, (u_offset_t)off, PAGESIZE, NULL,
2234 	    anon_pl, PAGESIZE, seg, addr, S_CREATE, cred);
2235 	if (err)
2236 		goto out;
2237 
2238 	pp = anon_pl[0];
2239 
2240 	/*
2241 	 * If the original page was locked, we need to move the lock
2242 	 * to the new page by transfering 'cowcnt/lckcnt' of the original
2243 	 * page to 'cowcnt/lckcnt' of the new page.
2244 	 *
2245 	 * See Statement at the beginning of segvn_lockop() and
2246 	 * comments in page_pp_useclaim() regarding the way
2247 	 * cowcnts/lckcnts are handled.
2248 	 *
2249 	 * Also availrmem must be decremented up front for read only mapping
2250 	 * before calling page_pp_useclaim. page_pp_useclaim will bump it back
2251 	 * if availrmem did not need to be decremented after all.
2252 	 */
2253 	if (oppflags & LOCK_PAGE) {
2254 		if ((prot & PROT_WRITE) == 0) {
2255 			mutex_enter(&freemem_lock);
2256 			if (availrmem > pages_pp_maximum) {
2257 				availrmem--;
2258 				pages_useclaim++;
2259 			} else {
2260 				mutex_exit(&freemem_lock);
2261 				goto out;
2262 			}
2263 			mutex_exit(&freemem_lock);
2264 		}
2265 		page_pp_useclaim(opp, pp, prot & PROT_WRITE);
2266 	}
2267 
2268 	/*
2269 	 * Now copy the contents from the original page,
2270 	 * which is locked and loaded in the MMU by
2271 	 * the caller to prevent yet another page fault.
2272 	 */
2273 	ppcopy(opp, pp);		/* XXX - should set mod bit in here */
2274 
2275 	hat_setrefmod(pp);		/* mark as modified */
2276 
2277 	/*
2278 	 * Unload the old translation.
2279 	 */
2280 	hat_unload(seg->s_as->a_hat, addr, PAGESIZE, HAT_UNLOAD);
2281 
2282 	/*
2283 	 * Free unmapped, unmodified original page.
2284 	 * or release the lock on the original page,
2285 	 * otherwise the process will sleep forever in
2286 	 * anon_decref() waiting for the "exclusive" lock
2287 	 * on the page.
2288 	 */
2289 	(void) page_release(opp, 1);
2290 
2291 	/*
2292 	 * we are done with page creation so downgrade the new
2293 	 * page's selock to shared, this helps when multiple
2294 	 * as_fault(...SOFTLOCK...) are done to the same
2295 	 * page(aio)
2296 	 */
2297 	page_downgrade(pp);
2298 
2299 	/*
2300 	 * NOTE:  The original anon slot must be freed by the
2301 	 * caller while holding the "anon_map" lock, if we
2302 	 * copied away from an anonymous page.
2303 	 */
2304 	return (pp);
2305 
2306 out:
2307 	*app = old;
2308 	if (pp)
2309 		page_unlock(pp);
2310 	anon_decref(new);
2311 	page_unlock(opp);
2312 	return ((page_t *)NULL);
2313 }
2314 
2315 int
2316 anon_map_privatepages(
2317 	struct anon_map *amp,
2318 	ulong_t	start_idx,
2319 	uint_t	szc,
2320 	struct seg *seg,
2321 	caddr_t addr,
2322 	uint_t	prot,
2323 	page_t	*ppa[],
2324 	struct vpage vpage[],
2325 	int anypgsz,
2326 	struct cred *cred)
2327 {
2328 	pgcnt_t		pgcnt;
2329 	struct vnode	*vp;
2330 	anoff_t		off;
2331 	page_t		*pl[2], *conpp = NULL;
2332 	int		err;
2333 	int		prealloc = 1;
2334 	struct anon	*ap, *oldap;
2335 	caddr_t		vaddr;
2336 	page_t		*pplist, *pp;
2337 	ulong_t		pg_idx, an_idx;
2338 	spgcnt_t	nreloc = 0;
2339 	int		pagelock = 0;
2340 	kmutex_t	*ahmpages = NULL;
2341 #ifdef DEBUG
2342 	int		refcnt;
2343 #endif
2344 
2345 	ASSERT(szc != 0);
2346 	ASSERT(szc == seg->s_szc);
2347 
2348 	VM_STAT_ADD(anonvmstats.privatepages[0]);
2349 
2350 	pgcnt = page_get_pagecnt(szc);
2351 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
2352 	ASSERT(IS_P2ALIGNED(start_idx, pgcnt));
2353 
2354 	ASSERT(amp != NULL);
2355 	ap = anon_get_ptr(amp->ahp, start_idx);
2356 	ASSERT(ap == NULL || ap->an_refcnt >= 1);
2357 
2358 	VM_STAT_COND_ADD(ap == NULL, anonvmstats.privatepages[1]);
2359 
2360 	/*
2361 	 * Now try and allocate the large page. If we fail then just
2362 	 * let VOP_GETPAGE give us PAGESIZE pages. Normally we let
2363 	 * the caller make this decision but to avoid added complexity
2364 	 * it's simplier to handle that case here.
2365 	 */
2366 	if (anypgsz == -1) {
2367 		VM_STAT_ADD(anonvmstats.privatepages[2]);
2368 		prealloc = 0;
2369 	} else if (page_alloc_pages(anon_vp, seg, addr, &pplist, NULL, szc,
2370 	    anypgsz) != 0) {
2371 		VM_STAT_ADD(anonvmstats.privatepages[3]);
2372 		prealloc = 0;
2373 	}
2374 
2375 	/*
2376 	 * make the decrement of all refcnts of all
2377 	 * anon slots of a large page appear atomic by
2378 	 * getting an anonpages_hash_lock for the
2379 	 * first anon slot of a large page.
2380 	 */
2381 	if (ap != NULL) {
2382 		ahmpages = &anonpages_hash_lock[AH_LOCK(ap->an_vp,
2383 		    ap->an_off)];
2384 		mutex_enter(ahmpages);
2385 		if (ap->an_refcnt == 1) {
2386 			VM_STAT_ADD(anonvmstats.privatepages[4]);
2387 			ASSERT(!anon_share(amp->ahp, start_idx, pgcnt));
2388 			mutex_exit(ahmpages);
2389 
2390 			if (prealloc) {
2391 				page_free_replacement_page(pplist);
2392 				page_create_putback(pgcnt);
2393 			}
2394 			ASSERT(ppa[0]->p_szc <= szc);
2395 			if (ppa[0]->p_szc == szc) {
2396 				VM_STAT_ADD(anonvmstats.privatepages[5]);
2397 				return (0);
2398 			}
2399 			for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) {
2400 				ASSERT(ppa[pg_idx] != NULL);
2401 				page_unlock(ppa[pg_idx]);
2402 			}
2403 			return (-1);
2404 		}
2405 	}
2406 
2407 	/*
2408 	 * If we are passed in the vpage array and this is
2409 	 * not PROT_WRITE then we need to decrement availrmem
2410 	 * up front before we try anything. If we need to and
2411 	 * can't decrement availrmem then its better to fail now
2412 	 * than in the middle of processing the new large page.
2413 	 * page_pp_usclaim() on behalf of each constituent page
2414 	 * below will adjust availrmem back for the cases not needed.
2415 	 */
2416 	if (vpage != NULL && (prot & PROT_WRITE) == 0) {
2417 		for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) {
2418 			if (VPP_ISPPLOCK(&vpage[pg_idx])) {
2419 				pagelock = 1;
2420 				break;
2421 			}
2422 		}
2423 		if (pagelock) {
2424 			VM_STAT_ADD(anonvmstats.privatepages[6]);
2425 			mutex_enter(&freemem_lock);
2426 			if (availrmem >= pages_pp_maximum + pgcnt) {
2427 				availrmem -= pgcnt;
2428 				pages_useclaim += pgcnt;
2429 			} else {
2430 				VM_STAT_ADD(anonvmstats.privatepages[7]);
2431 				mutex_exit(&freemem_lock);
2432 				if (ahmpages != NULL) {
2433 					mutex_exit(ahmpages);
2434 				}
2435 				if (prealloc) {
2436 					page_free_replacement_page(pplist);
2437 					page_create_putback(pgcnt);
2438 				}
2439 				for (pg_idx = 0; pg_idx < pgcnt; pg_idx++)
2440 					if (ppa[pg_idx] != NULL)
2441 						page_unlock(ppa[pg_idx]);
2442 				return (ENOMEM);
2443 			}
2444 			mutex_exit(&freemem_lock);
2445 		}
2446 	}
2447 
2448 	CPU_STATS_ADD_K(vm, cow_fault, pgcnt);
2449 
2450 	VM_STAT_ADD(anonvmstats.privatepages[8]);
2451 
2452 	an_idx = start_idx;
2453 	pg_idx = 0;
2454 	vaddr = addr;
2455 	for (; pg_idx < pgcnt; pg_idx++, an_idx++, vaddr += PAGESIZE) {
2456 		ASSERT(ppa[pg_idx] != NULL);
2457 		oldap = anon_get_ptr(amp->ahp, an_idx);
2458 		ASSERT(ahmpages != NULL || oldap == NULL);
2459 		ASSERT(ahmpages == NULL || oldap != NULL);
2460 		ASSERT(ahmpages == NULL || oldap->an_refcnt > 1);
2461 		ASSERT(ahmpages == NULL || pg_idx != 0 ||
2462 		    (refcnt = oldap->an_refcnt));
2463 		ASSERT(ahmpages == NULL || pg_idx == 0 ||
2464 		    refcnt == oldap->an_refcnt);
2465 
2466 		ap = anon_alloc(NULL, 0);
2467 
2468 		swap_xlate(ap, &vp, &off);
2469 
2470 		/*
2471 		 * Now setup our preallocated page to pass down to
2472 		 * swap_getpage().
2473 		 */
2474 		if (prealloc) {
2475 			pp = pplist;
2476 			page_sub(&pplist, pp);
2477 			conpp = pp;
2478 		}
2479 
2480 		err = swap_getconpage(vp, (u_offset_t)off, PAGESIZE, NULL, pl,
2481 			PAGESIZE, conpp, &nreloc, seg, vaddr, S_CREATE, cred);
2482 
2483 		/*
2484 		 * Impossible to fail this is S_CREATE.
2485 		 */
2486 		if (err)
2487 			panic("anon_map_privatepages: VOP_GETPAGE failed");
2488 
2489 		ASSERT(prealloc ? pp == pl[0] : pl[0]->p_szc == 0);
2490 		ASSERT(prealloc == 0 || nreloc == 1);
2491 
2492 		pp = pl[0];
2493 
2494 		/*
2495 		 * If the original page was locked, we need to move
2496 		 * the lock to the new page by transfering
2497 		 * 'cowcnt/lckcnt' of the original page to 'cowcnt/lckcnt'
2498 		 * of the new page. pg_idx can be used to index
2499 		 * into the vpage array since the caller will guarentee
2500 		 * that vpage struct passed in corresponds to addr
2501 		 * and forward.
2502 		 */
2503 		if (vpage != NULL && VPP_ISPPLOCK(&vpage[pg_idx])) {
2504 			page_pp_useclaim(ppa[pg_idx], pp, prot & PROT_WRITE);
2505 		} else if (pagelock) {
2506 			mutex_enter(&freemem_lock);
2507 			availrmem++;
2508 			pages_useclaim--;
2509 			mutex_exit(&freemem_lock);
2510 		}
2511 
2512 		/*
2513 		 * Now copy the contents from the original page.
2514 		 */
2515 		ppcopy(ppa[pg_idx], pp);
2516 
2517 		hat_setrefmod(pp);		/* mark as modified */
2518 
2519 		/*
2520 		 * Release the lock on the original page,
2521 		 * derement the old slot, and down grade the lock
2522 		 * on the new copy.
2523 		 */
2524 		page_unlock(ppa[pg_idx]);
2525 
2526 		if (!prealloc)
2527 			page_downgrade(pp);
2528 
2529 		ppa[pg_idx] = pp;
2530 
2531 		/*
2532 		 * Now reflect the copy in the new anon array.
2533 		 */
2534 		ASSERT(ahmpages == NULL || oldap->an_refcnt > 1);
2535 		if (oldap != NULL)
2536 			anon_decref(oldap);
2537 		(void) anon_set_ptr(amp->ahp, an_idx, ap, ANON_SLEEP);
2538 	}
2539 	if (ahmpages != NULL) {
2540 		mutex_exit(ahmpages);
2541 	}
2542 	ASSERT(prealloc == 0 || pplist == NULL);
2543 	if (prealloc) {
2544 		VM_STAT_ADD(anonvmstats.privatepages[9]);
2545 		for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) {
2546 			page_downgrade(ppa[pg_idx]);
2547 		}
2548 	}
2549 
2550 	/*
2551 	 * Unload the old large page translation.
2552 	 */
2553 	hat_unload(seg->s_as->a_hat, addr, pgcnt << PAGESHIFT, HAT_UNLOAD);
2554 	return (0);
2555 }
2556 
2557 /*
2558  * Allocate a private zero-filled anon page.
2559  */
2560 page_t *
2561 anon_zero(struct seg *seg, caddr_t addr, struct anon **app, struct cred *cred)
2562 {
2563 	struct anon *ap;
2564 	page_t *pp;
2565 	struct vnode *vp;
2566 	anoff_t off;
2567 	page_t *anon_pl[1 + 1];
2568 	int err;
2569 
2570 	/* Kernel probe */
2571 	TNF_PROBE_1(anon_zero, "vm pagefault", /* CSTYLED */,
2572 		tnf_opaque,	address,	addr);
2573 
2574 	*app = ap = anon_alloc(NULL, 0);
2575 	swap_xlate(ap, &vp, &off);
2576 
2577 	/*
2578 	 * Call the VOP_GETPAGE routine to create the page, thereby
2579 	 * enabling the vnode driver to allocate any filesystem
2580 	 * dependent structures (e.g., disk block allocation for UFS).
2581 	 * This also prevents more than on page from being added to
2582 	 * the vnode at the same time since it is locked.
2583 	 */
2584 	err = VOP_GETPAGE(vp, off, PAGESIZE, NULL,
2585 	    anon_pl, PAGESIZE, seg, addr, S_CREATE, cred);
2586 	if (err) {
2587 		*app = NULL;
2588 		anon_decref(ap);
2589 		return (NULL);
2590 	}
2591 	pp = anon_pl[0];
2592 
2593 	pagezero(pp, 0, PAGESIZE);	/* XXX - should set mod bit */
2594 	page_downgrade(pp);
2595 	CPU_STATS_ADD_K(vm, zfod, 1);
2596 	hat_setrefmod(pp);	/* mark as modified so pageout writes back */
2597 	return (pp);
2598 }
2599 
2600 
2601 /*
2602  * Allocate array of private zero-filled anon pages for empty slots
2603  * and kept pages for non empty slots within given range.
2604  *
2605  * NOTE: This rontine will try and use large pages
2606  *	if available and supported by underlying platform.
2607  */
2608 int
2609 anon_map_createpages(
2610 	struct anon_map *amp,
2611 	ulong_t start_index,
2612 	size_t len,
2613 	page_t *ppa[],
2614 	struct seg *seg,
2615 	caddr_t addr,
2616 	enum seg_rw rw,
2617 	struct cred *cred)
2618 {
2619 
2620 	struct anon	*ap;
2621 	struct vnode	*ap_vp;
2622 	page_t		*pp, *pplist, *anon_pl[1 + 1], *conpp = NULL;
2623 	int		err = 0;
2624 	ulong_t		p_index, index;
2625 	pgcnt_t		npgs, pg_cnt;
2626 	spgcnt_t	nreloc = 0;
2627 	uint_t		l_szc, szc, prot;
2628 	anoff_t		ap_off;
2629 	size_t		pgsz;
2630 	lgrp_t		*lgrp;
2631 
2632 	/*
2633 	 * XXX For now only handle S_CREATE.
2634 	 */
2635 	ASSERT(rw == S_CREATE);
2636 
2637 	index	= start_index;
2638 	p_index	= 0;
2639 	npgs = btopr(len);
2640 
2641 	/*
2642 	 * If this platform supports multiple page sizes
2643 	 * then try and allocate directly from the free
2644 	 * list for pages larger than PAGESIZE.
2645 	 *
2646 	 * NOTE:When we have page_create_ru we can stop
2647 	 *	directly allocating from the freelist.
2648 	 */
2649 	l_szc  = seg->s_szc;
2650 	ANON_LOCK_ENTER(&amp->a_rwlock, RW_WRITER);
2651 	while (npgs) {
2652 
2653 		/*
2654 		 * if anon slot already exists
2655 		 *   (means page has been created)
2656 		 * so 1) look up the page
2657 		 *    2) if the page is still in memory, get it.
2658 		 *    3) if not, create a page and
2659 		 *	  page in from physical swap device.
2660 		 * These are done in anon_getpage().
2661 		 */
2662 		ap = anon_get_ptr(amp->ahp, index);
2663 		if (ap) {
2664 			err = anon_getpage(&ap, &prot, anon_pl, PAGESIZE,
2665 			    seg, addr, S_READ, cred);
2666 			if (err) {
2667 				ANON_LOCK_EXIT(&amp->a_rwlock);
2668 				panic("anon_map_createpages: anon_getpage");
2669 			}
2670 			pp = anon_pl[0];
2671 			ppa[p_index++] = pp;
2672 
2673 			addr += PAGESIZE;
2674 			index++;
2675 			npgs--;
2676 			continue;
2677 		}
2678 		/*
2679 		 * Now try and allocate the largest page possible
2680 		 * for the current address and range.
2681 		 * Keep dropping down in page size until:
2682 		 *
2683 		 *	1) Properly aligned
2684 		 *	2) Does not overlap existing anon pages
2685 		 *	3) Fits in remaining range.
2686 		 *	4) able to allocate one.
2687 		 *
2688 		 * NOTE: XXX When page_create_ru is completed this code
2689 		 *	 will change.
2690 		 */
2691 		szc    = l_szc;
2692 		pplist = NULL;
2693 		pg_cnt = 0;
2694 		while (szc) {
2695 			pgsz	= page_get_pagesize(szc);
2696 			pg_cnt	= pgsz >> PAGESHIFT;
2697 			if (IS_P2ALIGNED(addr, pgsz) && pg_cnt <= npgs &&
2698 				anon_pages(amp->ahp, index, pg_cnt) == 0) {
2699 				/*
2700 				 * XXX
2701 				 * Since we are faking page_create()
2702 				 * we also need to do the freemem and
2703 				 * pcf accounting.
2704 				 */
2705 				(void) page_create_wait(pg_cnt, PG_WAIT);
2706 
2707 				/*
2708 				 * Get lgroup to allocate next page of shared
2709 				 * memory from and use it to specify where to
2710 				 * allocate the physical memory
2711 				 */
2712 				lgrp = lgrp_mem_choose(seg, addr, pgsz);
2713 
2714 				pplist = page_get_freelist(
2715 				    anon_vp, (u_offset_t)0, seg,
2716 				    addr, pgsz, 0, lgrp);
2717 
2718 				if (pplist == NULL) {
2719 					page_create_putback(pg_cnt);
2720 				}
2721 
2722 				/*
2723 				 * If a request for a page of size
2724 				 * larger than PAGESIZE failed
2725 				 * then don't try that size anymore.
2726 				 */
2727 				if (pplist == NULL) {
2728 					l_szc = szc - 1;
2729 				} else {
2730 					break;
2731 				}
2732 			}
2733 			szc--;
2734 		}
2735 
2736 		/*
2737 		 * If just using PAGESIZE pages then don't
2738 		 * directly allocate from the free list.
2739 		 */
2740 		if (pplist == NULL) {
2741 			ASSERT(szc == 0);
2742 			pp = anon_zero(seg, addr, &ap, cred);
2743 			if (pp == NULL) {
2744 				ANON_LOCK_EXIT(&amp->a_rwlock);
2745 				panic("anon_map_createpages: anon_zero");
2746 			}
2747 			ppa[p_index++] = pp;
2748 
2749 			ASSERT(anon_get_ptr(amp->ahp, index) == NULL);
2750 			(void) anon_set_ptr(amp->ahp, index, ap, ANON_SLEEP);
2751 
2752 			addr += PAGESIZE;
2753 			index++;
2754 			npgs--;
2755 			continue;
2756 		}
2757 
2758 		/*
2759 		 * pplist is a list of pg_cnt PAGESIZE pages.
2760 		 * These pages are locked SE_EXCL since they
2761 		 * came directly off the free list.
2762 		 */
2763 		ASSERT(IS_P2ALIGNED(pg_cnt, pg_cnt));
2764 		ASSERT(IS_P2ALIGNED(index, pg_cnt));
2765 		ASSERT(conpp == NULL);
2766 		while (pg_cnt--) {
2767 
2768 			ap = anon_alloc(NULL, 0);
2769 			swap_xlate(ap, &ap_vp, &ap_off);
2770 
2771 			ASSERT(pplist != NULL);
2772 			pp = pplist;
2773 			page_sub(&pplist, pp);
2774 			PP_CLRFREE(pp);
2775 			PP_CLRAGED(pp);
2776 			conpp = pp;
2777 
2778 			err = swap_getconpage(ap_vp, ap_off, PAGESIZE,
2779 			    (uint_t *)NULL, anon_pl, PAGESIZE, conpp, &nreloc,
2780 			    seg, addr, S_CREATE, cred);
2781 
2782 			if (err) {
2783 				ANON_LOCK_EXIT(&amp->a_rwlock);
2784 				panic("anon_map_createpages: S_CREATE");
2785 			}
2786 
2787 			ASSERT(anon_pl[0] == pp);
2788 			ASSERT(nreloc == 1);
2789 			pagezero(pp, 0, PAGESIZE);
2790 			CPU_STATS_ADD_K(vm, zfod, 1);
2791 			hat_setrefmod(pp);
2792 
2793 			ASSERT(anon_get_ptr(amp->ahp, index) == NULL);
2794 			(void) anon_set_ptr(amp->ahp, index, ap, ANON_SLEEP);
2795 
2796 			ppa[p_index++] = pp;
2797 
2798 			addr += PAGESIZE;
2799 			index++;
2800 			npgs--;
2801 		}
2802 		conpp = NULL;
2803 		pg_cnt	= pgsz >> PAGESHIFT;
2804 		p_index = p_index - pg_cnt;
2805 		while (pg_cnt--) {
2806 			page_downgrade(ppa[p_index++]);
2807 		}
2808 	}
2809 	ANON_LOCK_EXIT(&amp->a_rwlock);
2810 	return (0);
2811 }
2812 
2813 int
2814 anon_map_demotepages(
2815 	struct anon_map *amp,
2816 	ulong_t	start_idx,
2817 	struct seg *seg,
2818 	caddr_t addr,
2819 	uint_t prot,
2820 	struct vpage vpage[],
2821 	struct cred *cred)
2822 {
2823 	struct anon	*ap;
2824 	uint_t		szc = seg->s_szc;
2825 	pgcnt_t		pgcnt = page_get_pagecnt(szc);
2826 	size_t		ppasize = pgcnt * sizeof (page_t *);
2827 	page_t		**ppa = kmem_alloc(ppasize, KM_SLEEP);
2828 	page_t		*pp;
2829 	page_t		*pl[2];
2830 	pgcnt_t		i, pg_idx;
2831 	ulong_t		an_idx;
2832 	caddr_t		vaddr;
2833 	kmutex_t	*ahmpages = NULL;
2834 	int 		err;
2835 	int		retry = 0;
2836 	uint_t		vpprot;
2837 
2838 	ASSERT(RW_WRITE_HELD(&amp->a_rwlock));
2839 	ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
2840 	ASSERT(IS_P2ALIGNED(start_idx, pgcnt));
2841 	ASSERT(ppa != NULL);
2842 
2843 	VM_STAT_ADD(anonvmstats.demotepages[0]);
2844 
2845 	ap = anon_get_ptr(amp->ahp, start_idx);
2846 	if (ap != NULL) {
2847 		VM_STAT_ADD(anonvmstats.demotepages[1]);
2848 		ahmpages = &anonpages_hash_lock[AH_LOCK(ap->an_vp, ap->an_off)];
2849 		mutex_enter(ahmpages);
2850 	}
2851 top:
2852 	if (ap == NULL || ap->an_refcnt <= 1) {
2853 		int root = 0;
2854 		pgcnt_t npgs, curnpgs = 0;
2855 
2856 		VM_STAT_ADD(anonvmstats.demotepages[2]);
2857 
2858 		ASSERT(retry == 0 || ap != NULL);
2859 
2860 		if (ahmpages != NULL)
2861 			mutex_exit(ahmpages);
2862 		an_idx = start_idx;
2863 		for (i = 0; i < pgcnt; i++, an_idx++) {
2864 			ap = anon_get_ptr(amp->ahp, an_idx);
2865 			if (ap != NULL) {
2866 				ASSERT(ap->an_refcnt == 1);
2867 				pp = ppa[i] = page_lookup(ap->an_vp, ap->an_off,
2868 				    SE_EXCL);
2869 				if (pp != NULL) {
2870 					(void) hat_pageunload(pp,
2871 					    HAT_FORCE_PGUNLOAD);
2872 				}
2873 			} else {
2874 				ppa[i] = NULL;
2875 			}
2876 		}
2877 		for (i = 0; i < pgcnt; i++) {
2878 			if ((pp = ppa[i]) != NULL && pp->p_szc != 0) {
2879 				ASSERT(pp->p_szc <= szc);
2880 				if (!root) {
2881 					VM_STAT_ADD(anonvmstats.demotepages[3]);
2882 					if (curnpgs != 0)
2883 						panic("anon_map_demotepages: "
2884 						    "bad large page");
2885 
2886 					root = 1;
2887 					curnpgs = npgs =
2888 					    page_get_pagecnt(pp->p_szc);
2889 
2890 					ASSERT(npgs <= pgcnt);
2891 					ASSERT(IS_P2ALIGNED(npgs, npgs));
2892 					ASSERT(!(page_pptonum(pp) &
2893 					    (npgs - 1)));
2894 				} else {
2895 					ASSERT(i > 0);
2896 					ASSERT(page_pptonum(pp) - 1 ==
2897 					    page_pptonum(ppa[i - 1]));
2898 					if ((page_pptonum(pp) & (npgs - 1)) ==
2899 					    npgs - 1)
2900 						root = 0;
2901 				}
2902 				ASSERT(PAGE_EXCL(pp));
2903 				pp->p_szc = 0;
2904 				curnpgs--;
2905 			}
2906 		}
2907 		if (root != 0 || curnpgs != 0)
2908 			panic("anon_map_demotepages: bad large page");
2909 
2910 		for (i = 0; i < pgcnt; i++) {
2911 			if ((pp = ppa[i]) != NULL) {
2912 				ASSERT(!hat_page_is_mapped(pp));
2913 				ASSERT(pp->p_szc == 0);
2914 				page_unlock(pp);
2915 			}
2916 		}
2917 		kmem_free(ppa, ppasize);
2918 		return (0);
2919 	}
2920 	ASSERT(ahmpages != NULL);
2921 	mutex_exit(ahmpages);
2922 	ahmpages = NULL;
2923 
2924 	VM_STAT_ADD(anonvmstats.demotepages[4]);
2925 
2926 	ASSERT(retry == 0); /* we can be here only once */
2927 
2928 	vaddr = addr;
2929 	for (pg_idx = 0, an_idx = start_idx; pg_idx < pgcnt;
2930 	    pg_idx++, an_idx++, vaddr += PAGESIZE) {
2931 		ap = anon_get_ptr(amp->ahp, an_idx);
2932 		if (ap == NULL)
2933 			panic("anon_map_demotepages: no anon slot");
2934 		err = anon_getpage(&ap, &vpprot, pl, PAGESIZE, seg, vaddr,
2935 		    S_READ, cred);
2936 		if (err) {
2937 			for (i = 0; i < pg_idx; i++) {
2938 				if ((pp = ppa[i]) != NULL)
2939 					page_unlock(pp);
2940 			}
2941 			kmem_free(ppa, ppasize);
2942 			return (err);
2943 		}
2944 		ppa[pg_idx] = pl[0];
2945 	}
2946 
2947 	err = anon_map_privatepages(amp, start_idx, szc, seg, addr, prot, ppa,
2948 	    vpage, -1, cred);
2949 	if (err > 0) {
2950 		VM_STAT_ADD(anonvmstats.demotepages[5]);
2951 		kmem_free(ppa, ppasize);
2952 		return (err);
2953 	}
2954 	ASSERT(err == 0 || err == -1);
2955 	if (err == -1) {
2956 		VM_STAT_ADD(anonvmstats.demotepages[6]);
2957 		retry = 1;
2958 		goto top;
2959 	}
2960 	for (i = 0; i < pgcnt; i++) {
2961 		ASSERT(ppa[i] != NULL);
2962 		if (ppa[i]->p_szc != 0)
2963 			retry = 1;
2964 		page_unlock(ppa[i]);
2965 	}
2966 	if (retry) {
2967 		VM_STAT_ADD(anonvmstats.demotepages[7]);
2968 		goto top;
2969 	}
2970 
2971 	VM_STAT_ADD(anonvmstats.demotepages[8]);
2972 
2973 	kmem_free(ppa, ppasize);
2974 
2975 	return (0);
2976 }
2977 
2978 /*
2979  * Allocate and initialize an anon_map structure for seg
2980  * associating the given swap reservation with the new anon_map.
2981  */
2982 struct anon_map *
2983 anonmap_alloc(size_t size, size_t swresv)
2984 {
2985 	struct anon_map *amp;
2986 
2987 	amp = kmem_cache_alloc(anonmap_cache, KM_SLEEP);
2988 
2989 	amp->refcnt = 1;
2990 	amp->size = size;
2991 
2992 	amp->ahp = anon_create(btopr(size), ANON_SLEEP);
2993 	amp->swresv = swresv;
2994 	amp->locality = 0;
2995 	amp->a_szc = 0;
2996 	return (amp);
2997 }
2998 
2999 void
3000 anonmap_free(struct anon_map *amp)
3001 {
3002 	ASSERT(amp->ahp);
3003 	ASSERT(amp->refcnt == 0);
3004 
3005 	lgrp_shm_policy_fini(amp, NULL);
3006 	anon_release(amp->ahp, btopr(amp->size));
3007 	kmem_cache_free(anonmap_cache, amp);
3008 }
3009 
3010 /*
3011  * Returns true if the app array has some empty slots.
3012  * The offp and lenp paramters are in/out paramters.  On entry
3013  * these values represent the starting offset and length of the
3014  * mapping.  When true is returned, these values may be modified
3015  * to be the largest range which includes empty slots.
3016  */
3017 int
3018 non_anon(struct anon_hdr *ahp, ulong_t anon_idx, u_offset_t *offp,
3019 				size_t *lenp)
3020 {
3021 	ulong_t i, el;
3022 	ssize_t low, high;
3023 	struct anon *ap;
3024 
3025 	low = -1;
3026 	for (i = 0, el = *lenp; i < el; i += PAGESIZE, anon_idx++) {
3027 		ap = anon_get_ptr(ahp, anon_idx);
3028 		if (ap == NULL) {
3029 			if (low == -1)
3030 				low = i;
3031 			high = i;
3032 		}
3033 	}
3034 	if (low != -1) {
3035 		/*
3036 		 * Found at least one non-anon page.
3037 		 * Set up the off and len return values.
3038 		 */
3039 		if (low != 0)
3040 			*offp += low;
3041 		*lenp = high - low + PAGESIZE;
3042 		return (1);
3043 	}
3044 	return (0);
3045 }
3046 
3047 /*
3048  * Return a count of the number of existing anon pages in the anon array
3049  * app in the range (off, off+len). The array and slots must be guaranteed
3050  * stable by the caller.
3051  */
3052 pgcnt_t
3053 anon_pages(struct anon_hdr *ahp, ulong_t anon_index, pgcnt_t nslots)
3054 {
3055 	pgcnt_t cnt = 0;
3056 
3057 	while (nslots-- > 0) {
3058 		if ((anon_get_ptr(ahp, anon_index)) != NULL)
3059 			cnt++;
3060 		anon_index++;
3061 	}
3062 	return (cnt);
3063 }
3064 
3065 /*
3066  * Move reserved phys swap into memory swap (unreserve phys swap
3067  * and reserve mem swap by the same amount).
3068  * Used by segspt when it needs to lock resrved swap npages in memory
3069  */
3070 int
3071 anon_swap_adjust(pgcnt_t npages)
3072 {
3073 	pgcnt_t unlocked_mem_swap;
3074 
3075 	mutex_enter(&anoninfo_lock);
3076 
3077 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
3078 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
3079 
3080 	unlocked_mem_swap = k_anoninfo.ani_mem_resv
3081 					- k_anoninfo.ani_locked_swap;
3082 	if (npages > unlocked_mem_swap) {
3083 		spgcnt_t adjusted_swap = npages - unlocked_mem_swap;
3084 
3085 		/*
3086 		 * if there is not enough unlocked mem swap we take missing
3087 		 * amount from phys swap and give it to mem swap
3088 		 */
3089 		mutex_enter(&freemem_lock);
3090 		if (availrmem < adjusted_swap + segspt_minfree) {
3091 			mutex_exit(&freemem_lock);
3092 			mutex_exit(&anoninfo_lock);
3093 			return (ENOMEM);
3094 		}
3095 		availrmem -= adjusted_swap;
3096 		mutex_exit(&freemem_lock);
3097 
3098 		k_anoninfo.ani_mem_resv += adjusted_swap;
3099 		ASSERT(k_anoninfo.ani_phys_resv >= adjusted_swap);
3100 		k_anoninfo.ani_phys_resv -= adjusted_swap;
3101 
3102 		ANI_ADD(adjusted_swap);
3103 	}
3104 	k_anoninfo.ani_locked_swap += npages;
3105 
3106 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
3107 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
3108 
3109 	mutex_exit(&anoninfo_lock);
3110 
3111 	return (0);
3112 }
3113 
3114 /*
3115  * 'unlocked' reserved mem swap so when it is unreserved it
3116  * can be moved back phys (disk) swap
3117  */
3118 void
3119 anon_swap_restore(pgcnt_t npages)
3120 {
3121 	mutex_enter(&anoninfo_lock);
3122 
3123 	ASSERT(k_anoninfo.ani_locked_swap <= k_anoninfo.ani_mem_resv);
3124 
3125 	ASSERT(k_anoninfo.ani_locked_swap >= npages);
3126 	k_anoninfo.ani_locked_swap -= npages;
3127 
3128 	ASSERT(k_anoninfo.ani_locked_swap <= k_anoninfo.ani_mem_resv);
3129 
3130 	mutex_exit(&anoninfo_lock);
3131 }
3132 
3133 /*
3134  * Return the pointer from the list for a
3135  * specified anon index.
3136  */
3137 ulong_t *
3138 anon_get_slot(struct anon_hdr *ahp, ulong_t an_idx)
3139 {
3140 	struct anon	**app;
3141 	void 		**ppp;
3142 
3143 	ASSERT(an_idx < ahp->size);
3144 
3145 	/*
3146 	 * Single level case.
3147 	 */
3148 	if ((ahp->size <= ANON_CHUNK_SIZE) || (ahp->flags & ANON_ALLOC_FORCE)) {
3149 		return ((ulong_t *)&ahp->array_chunk[an_idx]);
3150 	} else {
3151 
3152 		/*
3153 		 * 2 level case.
3154 		 */
3155 		ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
3156 		if (*ppp == NULL) {
3157 			mutex_enter(&ahp->serial_lock);
3158 			ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
3159 			if (*ppp == NULL)
3160 				*ppp = kmem_zalloc(PAGESIZE, KM_SLEEP);
3161 			mutex_exit(&ahp->serial_lock);
3162 		}
3163 		app = *ppp;
3164 		return ((ulong_t *)&app[an_idx & ANON_CHUNK_OFF]);
3165 	}
3166 }
3167 
3168 void
3169 anon_array_enter(struct anon_map *amp, ulong_t an_idx, anon_sync_obj_t *sobj)
3170 {
3171 	ulong_t		*ap_slot;
3172 	kmutex_t	*mtx;
3173 	kcondvar_t	*cv;
3174 	int		hash;
3175 
3176 	/*
3177 	 * Use szc to determine anon slot(s) to appear atomic.
3178 	 * If szc = 0, then lock the anon slot and mark it busy.
3179 	 * If szc > 0, then lock the range of slots by getting the
3180 	 * anon_array_lock for the first anon slot, and mark only the
3181 	 * first anon slot busy to represent whole range being busy.
3182 	 */
3183 
3184 	ASSERT(RW_READ_HELD(&amp->a_rwlock));
3185 	an_idx = P2ALIGN(an_idx, page_get_pagecnt(amp->a_szc));
3186 	hash = ANON_ARRAY_HASH(amp, an_idx);
3187 	sobj->sync_mutex = mtx = &anon_array_lock[hash].pad_mutex;
3188 	sobj->sync_cv = cv = &anon_array_cv[hash];
3189 	mutex_enter(mtx);
3190 	ap_slot = anon_get_slot(amp->ahp, an_idx);
3191 	while (ANON_ISBUSY(ap_slot))
3192 		cv_wait(cv, mtx);
3193 	ANON_SETBUSY(ap_slot);
3194 	sobj->sync_data = ap_slot;
3195 	mutex_exit(mtx);
3196 }
3197 
3198 int
3199 anon_array_try_enter(struct anon_map *amp, ulong_t an_idx,
3200 			anon_sync_obj_t *sobj)
3201 {
3202 	ulong_t		*ap_slot;
3203 	kmutex_t	*mtx;
3204 	int		hash;
3205 
3206 	/*
3207 	 * Try to lock a range of anon slots.
3208 	 * Use szc to determine anon slot(s) to appear atomic.
3209 	 * If szc = 0, then lock the anon slot and mark it busy.
3210 	 * If szc > 0, then lock the range of slots by getting the
3211 	 * anon_array_lock for the first anon slot, and mark only the
3212 	 * first anon slot busy to represent whole range being busy.
3213 	 * Fail if the mutex or the anon_array are busy.
3214 	 */
3215 
3216 	ASSERT(RW_READ_HELD(&amp->a_rwlock));
3217 	an_idx = P2ALIGN(an_idx, page_get_pagecnt(amp->a_szc));
3218 	hash = ANON_ARRAY_HASH(amp, an_idx);
3219 	sobj->sync_mutex = mtx = &anon_array_lock[hash].pad_mutex;
3220 	sobj->sync_cv = &anon_array_cv[hash];
3221 	if (!mutex_tryenter(mtx)) {
3222 		return (EWOULDBLOCK);
3223 	}
3224 	ap_slot = anon_get_slot(amp->ahp, an_idx);
3225 	if (ANON_ISBUSY(ap_slot)) {
3226 		mutex_exit(mtx);
3227 		return (EWOULDBLOCK);
3228 	}
3229 	ANON_SETBUSY(ap_slot);
3230 	sobj->sync_data = ap_slot;
3231 	mutex_exit(mtx);
3232 	return (0);
3233 }
3234 
3235 void
3236 anon_array_exit(anon_sync_obj_t *sobj)
3237 {
3238 	mutex_enter(sobj->sync_mutex);
3239 	ASSERT(ANON_ISBUSY(sobj->sync_data));
3240 	ANON_CLRBUSY(sobj->sync_data);
3241 	if (CV_HAS_WAITERS(sobj->sync_cv))
3242 		cv_broadcast(sobj->sync_cv);
3243 	mutex_exit(sobj->sync_mutex);
3244 }
3245