xref: /illumos-gate/usr/src/uts/common/vm/vm_pagelist.c (revision 6e375c8351497b82ffa4f33cbf61d712999b4605)
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 2008 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  * Portions of this source code were derived from Berkeley 4.3 BSD
31  * under license from the Regents of the University of California.
32  */
33 
34 
35 /*
36  * This file contains common functions to access and manage the page lists.
37  * Many of these routines originated from platform dependent modules
38  * (sun4/vm/vm_dep.c, i86pc/vm/vm_machdep.c) and modified to function in
39  * a platform independent manner.
40  *
41  * vm/vm_dep.h provides for platform specific support.
42  */
43 
44 #include <sys/types.h>
45 #include <sys/debug.h>
46 #include <sys/cmn_err.h>
47 #include <sys/systm.h>
48 #include <sys/atomic.h>
49 #include <sys/sysmacros.h>
50 #include <vm/as.h>
51 #include <vm/page.h>
52 #include <vm/seg_kmem.h>
53 #include <vm/seg_vn.h>
54 #include <sys/vmsystm.h>
55 #include <sys/memnode.h>
56 #include <vm/vm_dep.h>
57 #include <sys/lgrp.h>
58 #include <sys/mem_config.h>
59 #include <sys/callb.h>
60 #include <sys/mem_cage.h>
61 #include <sys/sdt.h>
62 
63 extern uint_t	vac_colors;
64 
65 #define	MAX_PRAGMA_ALIGN	128
66 
67 /* vm_cpu_data0 for the boot cpu before kmem is initialized */
68 
69 #if L2CACHE_ALIGN_MAX <= MAX_PRAGMA_ALIGN
70 #pragma align	L2CACHE_ALIGN_MAX(vm_cpu_data0)
71 #else
72 #pragma align	MAX_PRAGMA_ALIGN(vm_cpu_data0)
73 #endif
74 char		vm_cpu_data0[VM_CPU_DATA_PADSIZE];
75 
76 /*
77  * number of page colors equivalent to reqested color in page_get routines.
78  * If set, keeps large pages intact longer and keeps MPO allocation
79  * from the local mnode in favor of acquiring the 'correct' page color from
80  * a demoted large page or from a remote mnode.
81  */
82 uint_t	colorequiv;
83 
84 /*
85  * color equivalency mask for each page size.
86  * Mask is computed based on cpu L2$ way sizes and colorequiv global.
87  * High 4 bits determine the number of high order bits of the color to ignore.
88  * Low 4 bits determines number of low order bits of color to ignore (it's only
89  * relevant for hashed index based page coloring).
90  */
91 uchar_t colorequivszc[MMU_PAGE_SIZES];
92 
93 /*
94  * if set, specifies the percentage of large pages that are free from within
95  * a large page region before attempting to lock those pages for
96  * page_get_contig_pages processing.
97  *
98  * Should be turned on when kpr is available when page_trylock_contig_pages
99  * can be more selective.
100  */
101 
102 int	ptcpthreshold;
103 
104 /*
105  * Limit page get contig page search based on failure cnts in pgcpfailcnt[].
106  * Enabled by default via pgcplimitsearch.
107  *
108  * pgcpfailcnt[] is bounded by PGCPFAILMAX (>= 1/2 of installed
109  * memory). When reached, pgcpfailcnt[] is reset to 1/2 of this upper
110  * bound. This upper bound range guarantees:
111  *    - all large page 'slots' will be searched over time
112  *    - the minimum (1) large page candidates considered on each pgcp call
113  *    - count doesn't wrap around to 0
114  */
115 pgcnt_t	pgcpfailcnt[MMU_PAGE_SIZES];
116 int	pgcplimitsearch = 1;
117 
118 #define	PGCPFAILMAX		(1 << (highbit(physinstalled) - 1))
119 #define	SETPGCPFAILCNT(szc)						\
120 	if (++pgcpfailcnt[szc] >= PGCPFAILMAX)				\
121 		pgcpfailcnt[szc] = PGCPFAILMAX / 2;
122 
123 #ifdef VM_STATS
124 struct vmm_vmstats_str  vmm_vmstats;
125 
126 #endif /* VM_STATS */
127 
128 #if defined(__sparc)
129 #define	LPGCREATE	0
130 #else
131 /* enable page_get_contig_pages */
132 #define	LPGCREATE	1
133 #endif
134 
135 int pg_contig_disable;
136 int pg_lpgcreate_nocage = LPGCREATE;
137 
138 /*
139  * page_freelist_split pfn flag to signify no lo or hi pfn requirement.
140  */
141 #define	PFNNULL		0
142 
143 /* Flags involved in promotion and demotion routines */
144 #define	PC_FREE		0x1	/* put page on freelist */
145 #define	PC_ALLOC	0x2	/* return page for allocation */
146 
147 /*
148  * Flag for page_demote to be used with PC_FREE to denote that we don't care
149  * what the color is as the color parameter to the function is ignored.
150  */
151 #define	PC_NO_COLOR	(-1)
152 
153 /* mtype value for page_promote to use when mtype does not matter */
154 #define	PC_MTYPE_ANY	(-1)
155 
156 /*
157  * page counters candidates info
158  * See page_ctrs_cands comment below for more details.
159  * fields are as follows:
160  *	pcc_pages_free:		# pages which freelist coalesce can create
161  *	pcc_color_free:		pointer to page free counts per color
162  */
163 typedef struct pcc_info {
164 	pgcnt_t	pcc_pages_free;
165 	pgcnt_t	*pcc_color_free;
166 	uint_t	pad[12];
167 } pcc_info_t;
168 
169 /*
170  * On big machines it can take a long time to check page_counters
171  * arrays. page_ctrs_cands is a summary array whose elements are a dynamically
172  * updated sum of all elements of the corresponding page_counters arrays.
173  * page_freelist_coalesce() searches page_counters only if an appropriate
174  * element of page_ctrs_cands array is greater than 0.
175  *
176  * page_ctrs_cands is indexed by mutex (i), region (r), mnode (m), mrange (g)
177  */
178 pcc_info_t **page_ctrs_cands[NPC_MUTEX][MMU_PAGE_SIZES];
179 
180 /*
181  * Return in val the total number of free pages which can be created
182  * for the given mnode (m), mrange (g), and region size (r)
183  */
184 #define	PGCTRS_CANDS_GETVALUE(m, g, r, val) {				\
185 	int i;								\
186 	val = 0;							\
187 	for (i = 0; i < NPC_MUTEX; i++) {				\
188 	    val += page_ctrs_cands[i][(r)][(m)][(g)].pcc_pages_free;	\
189 	}								\
190 }
191 
192 /*
193  * Return in val the total number of free pages which can be created
194  * for the given mnode (m), mrange (g), region size (r), and color (c)
195  */
196 #define	PGCTRS_CANDS_GETVALUECOLOR(m, g, r, c, val) {			\
197 	int i;								\
198 	val = 0;							\
199 	ASSERT((c) < PAGE_GET_PAGECOLORS(r));				\
200 	for (i = 0; i < NPC_MUTEX; i++) {				\
201 	    val +=							\
202 		page_ctrs_cands[i][(r)][(m)][(g)].pcc_color_free[(c)];	\
203 	}								\
204 }
205 
206 /*
207  * We can only allow a single thread to update a counter within the physical
208  * range of the largest supported page size. That is the finest granularity
209  * possible since the counter values are dependent on each other
210  * as you move accross region sizes. PP_CTR_LOCK_INDX is used to determine the
211  * ctr_mutex lock index for a particular physical range.
212  */
213 static kmutex_t	*ctr_mutex[NPC_MUTEX];
214 
215 #define	PP_CTR_LOCK_INDX(pp)						\
216 	(((pp)->p_pagenum >>						\
217 	    (PAGE_BSZS_SHIFT(mmu_page_sizes - 1))) & (NPC_MUTEX - 1))
218 
219 #define	INVALID_COLOR 0xffffffff
220 #define	INVALID_MASK  0xffffffff
221 
222 /*
223  * Local functions prototypes.
224  */
225 
226 void page_ctr_add(int, int, page_t *, int);
227 void page_ctr_add_internal(int, int, page_t *, int);
228 void page_ctr_sub(int, int, page_t *, int);
229 void page_ctr_sub_internal(int, int, page_t *, int);
230 void page_freelist_lock(int);
231 void page_freelist_unlock(int);
232 page_t *page_promote(int, pfn_t, uchar_t, int, int);
233 page_t *page_demote(int, pfn_t, pfn_t, uchar_t, uchar_t, int, int);
234 page_t *page_freelist_split(uchar_t,
235     uint_t, int, int, pfn_t, pfn_t, page_list_walker_t *);
236 page_t *page_get_mnode_cachelist(uint_t, uint_t, int, int);
237 static int page_trylock_cons(page_t *pp, se_t se);
238 
239 /*
240  * The page_counters array below is used to keep track of free contiguous
241  * physical memory.  A hw_page_map_t will be allocated per mnode per szc.
242  * This contains an array of counters, the size of the array, a shift value
243  * used to convert a pagenum into a counter array index or vice versa, as
244  * well as a cache of the last successful index to be promoted to a larger
245  * page size.  As an optimization, we keep track of the last successful index
246  * to be promoted per page color for the given size region, and this is
247  * allocated dynamically based upon the number of colors for a given
248  * region size.
249  *
250  * Conceptually, the page counters are represented as:
251  *
252  *	page_counters[region_size][mnode]
253  *
254  *	region_size:	size code of a candidate larger page made up
255  *			of contiguous free smaller pages.
256  *
257  *	page_counters[region_size][mnode].hpm_counters[index]:
258  *		represents how many (region_size - 1) pages either
259  *		exist or can be created within the given index range.
260  *
261  * Let's look at a sparc example:
262  *	If we want to create a free 512k page, we look at region_size 2
263  *	for the mnode we want.  We calculate the index and look at a specific
264  *	hpm_counters location.  If we see 8 (FULL_REGION_CNT on sparc) at
265  *	this location, it means that 8 64k pages either exist or can be created
266  *	from 8K pages in order to make a single free 512k page at the given
267  *	index.  Note that when a region is full, it will contribute to the
268  *	counts in the region above it.  Thus we will not know what page
269  *	size the free pages will be which can be promoted to this new free
270  *	page unless we look at all regions below the current region.
271  */
272 
273 /*
274  * Note: hpmctr_t is defined in platform vm_dep.h
275  * hw_page_map_t contains all the information needed for the page_counters
276  * logic. The fields are as follows:
277  *
278  *	hpm_counters:	dynamically allocated array to hold counter data
279  *	hpm_entries:	entries in hpm_counters
280  *	hpm_shift:	shift for pnum/array index conv
281  *	hpm_base:	PFN mapped to counter index 0
282  *	hpm_color_current:	last index in counter array for this color at
283  *				which we successfully created a large page
284  */
285 typedef struct hw_page_map {
286 	hpmctr_t	*hpm_counters;
287 	size_t		hpm_entries;
288 	int		hpm_shift;
289 	pfn_t		hpm_base;
290 	size_t		*hpm_color_current[MAX_MNODE_MRANGES];
291 #if defined(__sparc)
292 	uint_t		pad[4];
293 #endif
294 } hw_page_map_t;
295 
296 /*
297  * Element zero is not used, but is allocated for convenience.
298  */
299 static hw_page_map_t *page_counters[MMU_PAGE_SIZES];
300 
301 /*
302  * Cached value of MNODE_RANGE_CNT(mnode).
303  * This is a function call in x86.
304  */
305 static int mnode_nranges[MAX_MEM_NODES];
306 static int mnode_maxmrange[MAX_MEM_NODES];
307 
308 /*
309  * The following macros are convenient ways to get access to the individual
310  * elements of the page_counters arrays.  They can be used on both
311  * the left side and right side of equations.
312  */
313 #define	PAGE_COUNTERS(mnode, rg_szc, idx)			\
314 	(page_counters[(rg_szc)][(mnode)].hpm_counters[(idx)])
315 
316 #define	PAGE_COUNTERS_COUNTERS(mnode, rg_szc) 			\
317 	(page_counters[(rg_szc)][(mnode)].hpm_counters)
318 
319 #define	PAGE_COUNTERS_SHIFT(mnode, rg_szc) 			\
320 	(page_counters[(rg_szc)][(mnode)].hpm_shift)
321 
322 #define	PAGE_COUNTERS_ENTRIES(mnode, rg_szc) 			\
323 	(page_counters[(rg_szc)][(mnode)].hpm_entries)
324 
325 #define	PAGE_COUNTERS_BASE(mnode, rg_szc) 			\
326 	(page_counters[(rg_szc)][(mnode)].hpm_base)
327 
328 #define	PAGE_COUNTERS_CURRENT_COLOR_ARRAY(mnode, rg_szc, g)		\
329 	(page_counters[(rg_szc)][(mnode)].hpm_color_current[(g)])
330 
331 #define	PAGE_COUNTERS_CURRENT_COLOR(mnode, rg_szc, color, mrange)	\
332 	(page_counters[(rg_szc)][(mnode)].				\
333 	hpm_color_current[(mrange)][(color)])
334 
335 #define	PNUM_TO_IDX(mnode, rg_szc, pnum)			\
336 	(((pnum) - PAGE_COUNTERS_BASE((mnode), (rg_szc))) >>	\
337 		PAGE_COUNTERS_SHIFT((mnode), (rg_szc)))
338 
339 #define	IDX_TO_PNUM(mnode, rg_szc, index) 			\
340 	(PAGE_COUNTERS_BASE((mnode), (rg_szc)) +		\
341 		((index) << PAGE_COUNTERS_SHIFT((mnode), (rg_szc))))
342 
343 /*
344  * Protects the hpm_counters and hpm_color_current memory from changing while
345  * looking at page counters information.
346  * Grab the write lock to modify what these fields point at.
347  * Grab the read lock to prevent any pointers from changing.
348  * The write lock can not be held during memory allocation due to a possible
349  * recursion deadlock with trying to grab the read lock while the
350  * write lock is already held.
351  */
352 krwlock_t page_ctrs_rwlock[MAX_MEM_NODES];
353 
354 
355 /*
356  * initialize cpu_vm_data to point at cache aligned vm_cpu_data_t.
357  */
358 void
359 cpu_vm_data_init(struct cpu *cp)
360 {
361 	if (cp == CPU0) {
362 		cp->cpu_vm_data = (void *)&vm_cpu_data0;
363 	} else {
364 		void	*kmptr;
365 		int	align;
366 		size_t	sz;
367 
368 		align = (L2CACHE_ALIGN) ? L2CACHE_ALIGN : L2CACHE_ALIGN_MAX;
369 		sz = P2ROUNDUP(sizeof (vm_cpu_data_t), align) + align;
370 		kmptr = kmem_zalloc(sz, KM_SLEEP);
371 		cp->cpu_vm_data = (void *) P2ROUNDUP((uintptr_t)kmptr, align);
372 		((vm_cpu_data_t *)cp->cpu_vm_data)->vc_kmptr = kmptr;
373 		((vm_cpu_data_t *)cp->cpu_vm_data)->vc_kmsize = sz;
374 	}
375 }
376 
377 /*
378  * free cpu_vm_data
379  */
380 void
381 cpu_vm_data_destroy(struct cpu *cp)
382 {
383 	if (cp->cpu_seqid && cp->cpu_vm_data) {
384 		ASSERT(cp != CPU0);
385 		kmem_free(((vm_cpu_data_t *)cp->cpu_vm_data)->vc_kmptr,
386 		    ((vm_cpu_data_t *)cp->cpu_vm_data)->vc_kmsize);
387 	}
388 	cp->cpu_vm_data = NULL;
389 }
390 
391 
392 /*
393  * page size to page size code
394  */
395 int
396 page_szc(size_t pagesize)
397 {
398 	int	i = 0;
399 
400 	while (hw_page_array[i].hp_size) {
401 		if (pagesize == hw_page_array[i].hp_size)
402 			return (i);
403 		i++;
404 	}
405 	return (-1);
406 }
407 
408 /*
409  * page size to page size code with the restriction that it be a supported
410  * user page size.  If it's not a supported user page size, -1 will be returned.
411  */
412 int
413 page_szc_user_filtered(size_t pagesize)
414 {
415 	int szc = page_szc(pagesize);
416 	if ((szc != -1) && (SZC_2_USERSZC(szc) != -1)) {
417 		return (szc);
418 	}
419 	return (-1);
420 }
421 
422 /*
423  * Return how many page sizes are available for the user to use.  This is
424  * what the hardware supports and not based upon how the OS implements the
425  * support of different page sizes.
426  *
427  * If legacy is non-zero, return the number of pagesizes available to legacy
428  * applications. The number of legacy page sizes might be less than the
429  * exported user page sizes. This is to prevent legacy applications that
430  * use the largest page size returned from getpagesizes(3c) from inadvertantly
431  * using the 'new' large pagesizes.
432  */
433 uint_t
434 page_num_user_pagesizes(int legacy)
435 {
436 	if (legacy)
437 		return (mmu_legacy_page_sizes);
438 	return (mmu_exported_page_sizes);
439 }
440 
441 uint_t
442 page_num_pagesizes(void)
443 {
444 	return (mmu_page_sizes);
445 }
446 
447 /*
448  * returns the count of the number of base pagesize pages associated with szc
449  */
450 pgcnt_t
451 page_get_pagecnt(uint_t szc)
452 {
453 	if (szc >= mmu_page_sizes)
454 		panic("page_get_pagecnt: out of range %d", szc);
455 	return (hw_page_array[szc].hp_pgcnt);
456 }
457 
458 size_t
459 page_get_pagesize(uint_t szc)
460 {
461 	if (szc >= mmu_page_sizes)
462 		panic("page_get_pagesize: out of range %d", szc);
463 	return (hw_page_array[szc].hp_size);
464 }
465 
466 /*
467  * Return the size of a page based upon the index passed in.  An index of
468  * zero refers to the smallest page size in the system, and as index increases
469  * it refers to the next larger supported page size in the system.
470  * Note that szc and userszc may not be the same due to unsupported szc's on
471  * some systems.
472  */
473 size_t
474 page_get_user_pagesize(uint_t userszc)
475 {
476 	uint_t szc = USERSZC_2_SZC(userszc);
477 
478 	if (szc >= mmu_page_sizes)
479 		panic("page_get_user_pagesize: out of range %d", szc);
480 	return (hw_page_array[szc].hp_size);
481 }
482 
483 uint_t
484 page_get_shift(uint_t szc)
485 {
486 	if (szc >= mmu_page_sizes)
487 		panic("page_get_shift: out of range %d", szc);
488 	return (PAGE_GET_SHIFT(szc));
489 }
490 
491 uint_t
492 page_get_pagecolors(uint_t szc)
493 {
494 	if (szc >= mmu_page_sizes)
495 		panic("page_get_pagecolors: out of range %d", szc);
496 	return (PAGE_GET_PAGECOLORS(szc));
497 }
498 
499 /*
500  * this assigns the desired equivalent color after a split
501  */
502 uint_t
503 page_correct_color(uchar_t szc, uchar_t nszc, uint_t color,
504     uint_t ncolor, uint_t ceq_mask)
505 {
506 	ASSERT(nszc > szc);
507 	ASSERT(szc < mmu_page_sizes);
508 	ASSERT(color < PAGE_GET_PAGECOLORS(szc));
509 	ASSERT(ncolor < PAGE_GET_PAGECOLORS(nszc));
510 
511 	color &= ceq_mask;
512 	ncolor = PAGE_CONVERT_COLOR(ncolor, szc, nszc);
513 	return (color | (ncolor & ~ceq_mask));
514 }
515 
516 /*
517  * The interleaved_mnodes flag is set when mnodes overlap in
518  * the physbase..physmax range, but have disjoint slices.
519  * In this case hpm_counters is shared by all mnodes.
520  * This flag is set dynamically by the platform.
521  */
522 int interleaved_mnodes = 0;
523 
524 /*
525  * Called by startup().
526  * Size up the per page size free list counters based on physmax
527  * of each node and max_mem_nodes.
528  *
529  * If interleaved_mnodes is set we need to find the first mnode that
530  * exists. hpm_counters for the first mnode will then be shared by
531  * all other mnodes. If interleaved_mnodes is not set, just set
532  * first=mnode each time. That means there will be no sharing.
533  */
534 size_t
535 page_ctrs_sz(void)
536 {
537 	int	r;		/* region size */
538 	int	mnode;
539 	int	firstmn;	/* first mnode that exists */
540 	int	nranges;
541 	pfn_t	physbase;
542 	pfn_t	physmax;
543 	uint_t	ctrs_sz = 0;
544 	int 	i;
545 	pgcnt_t colors_per_szc[MMU_PAGE_SIZES];
546 
547 	/*
548 	 * We need to determine how many page colors there are for each
549 	 * page size in order to allocate memory for any color specific
550 	 * arrays.
551 	 */
552 	for (i = 0; i < mmu_page_sizes; i++) {
553 		colors_per_szc[i] = PAGE_GET_PAGECOLORS(i);
554 	}
555 
556 	for (firstmn = -1, mnode = 0; mnode < max_mem_nodes; mnode++) {
557 
558 		pgcnt_t r_pgcnt;
559 		pfn_t   r_base;
560 		pgcnt_t r_align;
561 
562 		if (mem_node_config[mnode].exists == 0)
563 			continue;
564 
565 		HPM_COUNTERS_LIMITS(mnode, physbase, physmax, firstmn);
566 		nranges = MNODE_RANGE_CNT(mnode);
567 		mnode_nranges[mnode] = nranges;
568 		mnode_maxmrange[mnode] = MNODE_MAX_MRANGE(mnode);
569 
570 		/*
571 		 * determine size needed for page counter arrays with
572 		 * base aligned to large page size.
573 		 */
574 		for (r = 1; r < mmu_page_sizes; r++) {
575 			/* add in space for hpm_color_current */
576 			ctrs_sz += sizeof (size_t) *
577 			    colors_per_szc[r] * nranges;
578 
579 			if (firstmn != mnode)
580 				continue;
581 
582 			/* add in space for hpm_counters */
583 			r_align = page_get_pagecnt(r);
584 			r_base = physbase;
585 			r_base &= ~(r_align - 1);
586 			r_pgcnt = howmany(physmax - r_base + 1, r_align);
587 
588 			/*
589 			 * Round up to always allocate on pointer sized
590 			 * boundaries.
591 			 */
592 			ctrs_sz += P2ROUNDUP((r_pgcnt * sizeof (hpmctr_t)),
593 			    sizeof (hpmctr_t *));
594 		}
595 	}
596 
597 	for (r = 1; r < mmu_page_sizes; r++) {
598 		ctrs_sz += (max_mem_nodes * sizeof (hw_page_map_t));
599 	}
600 
601 	/* add in space for page_ctrs_cands and pcc_color_free */
602 	ctrs_sz += sizeof (pcc_info_t *) * max_mem_nodes *
603 	    mmu_page_sizes * NPC_MUTEX;
604 
605 	for (mnode = 0; mnode < max_mem_nodes; mnode++) {
606 
607 		if (mem_node_config[mnode].exists == 0)
608 			continue;
609 
610 		nranges = mnode_nranges[mnode];
611 		ctrs_sz += sizeof (pcc_info_t) * nranges *
612 		    mmu_page_sizes * NPC_MUTEX;
613 		for (r = 1; r < mmu_page_sizes; r++) {
614 			ctrs_sz += sizeof (pgcnt_t) * nranges *
615 			    colors_per_szc[r] * NPC_MUTEX;
616 		}
617 	}
618 
619 	/* ctr_mutex */
620 	ctrs_sz += (max_mem_nodes * NPC_MUTEX * sizeof (kmutex_t));
621 
622 	/* size for page list counts */
623 	PLCNT_SZ(ctrs_sz);
624 
625 	/*
626 	 * add some slop for roundups. page_ctrs_alloc will roundup the start
627 	 * address of the counters to ecache_alignsize boundary for every
628 	 * memory node.
629 	 */
630 	return (ctrs_sz + max_mem_nodes * L2CACHE_ALIGN);
631 }
632 
633 caddr_t
634 page_ctrs_alloc(caddr_t alloc_base)
635 {
636 	int	mnode;
637 	int	mrange, nranges;
638 	int	r;		/* region size */
639 	int	i;
640 	int	firstmn;	/* first mnode that exists */
641 	pfn_t	physbase;
642 	pfn_t	physmax;
643 	pgcnt_t colors_per_szc[MMU_PAGE_SIZES];
644 
645 	/*
646 	 * We need to determine how many page colors there are for each
647 	 * page size in order to allocate memory for any color specific
648 	 * arrays.
649 	 */
650 	for (i = 0; i < mmu_page_sizes; i++) {
651 		colors_per_szc[i] = PAGE_GET_PAGECOLORS(i);
652 	}
653 
654 	for (r = 1; r < mmu_page_sizes; r++) {
655 		page_counters[r] = (hw_page_map_t *)alloc_base;
656 		alloc_base += (max_mem_nodes * sizeof (hw_page_map_t));
657 	}
658 
659 	/* page_ctrs_cands and pcc_color_free array */
660 	for (i = 0; i < NPC_MUTEX; i++) {
661 		for (r = 1; r < mmu_page_sizes; r++) {
662 
663 			page_ctrs_cands[i][r] = (pcc_info_t **)alloc_base;
664 			alloc_base += sizeof (pcc_info_t *) * max_mem_nodes;
665 
666 			for (mnode = 0; mnode < max_mem_nodes; mnode++) {
667 				pcc_info_t *pi;
668 
669 				if (mem_node_config[mnode].exists == 0)
670 					continue;
671 
672 				nranges = mnode_nranges[mnode];
673 
674 				pi = (pcc_info_t *)alloc_base;
675 				alloc_base += sizeof (pcc_info_t) * nranges;
676 				page_ctrs_cands[i][r][mnode] = pi;
677 
678 				for (mrange = 0; mrange < nranges; mrange++) {
679 					pi->pcc_color_free =
680 					    (pgcnt_t *)alloc_base;
681 					alloc_base += sizeof (pgcnt_t) *
682 					    colors_per_szc[r];
683 					pi++;
684 				}
685 			}
686 		}
687 	}
688 
689 	/* ctr_mutex */
690 	for (i = 0; i < NPC_MUTEX; i++) {
691 		ctr_mutex[i] = (kmutex_t *)alloc_base;
692 		alloc_base += (max_mem_nodes * sizeof (kmutex_t));
693 	}
694 
695 	/* initialize page list counts */
696 	PLCNT_INIT(alloc_base);
697 
698 	for (firstmn = -1, mnode = 0; mnode < max_mem_nodes; mnode++) {
699 
700 		pgcnt_t r_pgcnt;
701 		pfn_t	r_base;
702 		pgcnt_t r_align;
703 		int	r_shift;
704 		int	nranges = mnode_nranges[mnode];
705 
706 		if (mem_node_config[mnode].exists == 0)
707 			continue;
708 
709 		HPM_COUNTERS_LIMITS(mnode, physbase, physmax, firstmn);
710 
711 		for (r = 1; r < mmu_page_sizes; r++) {
712 			/*
713 			 * the page_counters base has to be aligned to the
714 			 * page count of page size code r otherwise the counts
715 			 * will cross large page boundaries.
716 			 */
717 			r_align = page_get_pagecnt(r);
718 			r_base = physbase;
719 			/* base needs to be aligned - lower to aligned value */
720 			r_base &= ~(r_align - 1);
721 			r_pgcnt = howmany(physmax - r_base + 1, r_align);
722 			r_shift = PAGE_BSZS_SHIFT(r);
723 
724 			PAGE_COUNTERS_SHIFT(mnode, r) = r_shift;
725 			PAGE_COUNTERS_ENTRIES(mnode, r) = r_pgcnt;
726 			PAGE_COUNTERS_BASE(mnode, r) = r_base;
727 			for (mrange = 0; mrange < nranges; mrange++) {
728 				PAGE_COUNTERS_CURRENT_COLOR_ARRAY(mnode,
729 				    r, mrange) = (size_t *)alloc_base;
730 				alloc_base += sizeof (size_t) *
731 				    colors_per_szc[r];
732 			}
733 			for (i = 0; i < colors_per_szc[r]; i++) {
734 				uint_t color_mask = colors_per_szc[r] - 1;
735 				pfn_t  pfnum = r_base;
736 				size_t idx;
737 				int mrange;
738 				MEM_NODE_ITERATOR_DECL(it);
739 
740 				MEM_NODE_ITERATOR_INIT(pfnum, mnode, r, &it);
741 				if (pfnum == (pfn_t)-1) {
742 					idx = 0;
743 				} else {
744 					PAGE_NEXT_PFN_FOR_COLOR(pfnum, r, i,
745 					    color_mask, color_mask, &it);
746 					idx = PNUM_TO_IDX(mnode, r, pfnum);
747 					idx = (idx >= r_pgcnt) ? 0 : idx;
748 				}
749 				for (mrange = 0; mrange < nranges; mrange++) {
750 					PAGE_COUNTERS_CURRENT_COLOR(mnode,
751 					    r, i, mrange) = idx;
752 				}
753 			}
754 
755 			/* hpm_counters may be shared by all mnodes */
756 			if (firstmn == mnode) {
757 				PAGE_COUNTERS_COUNTERS(mnode, r) =
758 				    (hpmctr_t *)alloc_base;
759 				alloc_base +=
760 				    P2ROUNDUP((sizeof (hpmctr_t) * r_pgcnt),
761 				    sizeof (hpmctr_t *));
762 			} else {
763 				PAGE_COUNTERS_COUNTERS(mnode, r) =
764 				    PAGE_COUNTERS_COUNTERS(firstmn, r);
765 			}
766 
767 			/*
768 			 * Verify that PNUM_TO_IDX and IDX_TO_PNUM
769 			 * satisfy the identity requirement.
770 			 * We should be able to go from one to the other
771 			 * and get consistent values.
772 			 */
773 			ASSERT(PNUM_TO_IDX(mnode, r,
774 			    (IDX_TO_PNUM(mnode, r, 0))) == 0);
775 			ASSERT(IDX_TO_PNUM(mnode, r,
776 			    (PNUM_TO_IDX(mnode, r, r_base))) == r_base);
777 		}
778 		/*
779 		 * Roundup the start address of the page_counters to
780 		 * cache aligned boundary for every memory node.
781 		 * page_ctrs_sz() has added some slop for these roundups.
782 		 */
783 		alloc_base = (caddr_t)P2ROUNDUP((uintptr_t)alloc_base,
784 		    L2CACHE_ALIGN);
785 	}
786 
787 	/* Initialize other page counter specific data structures. */
788 	for (mnode = 0; mnode < MAX_MEM_NODES; mnode++) {
789 		rw_init(&page_ctrs_rwlock[mnode], NULL, RW_DEFAULT, NULL);
790 	}
791 
792 	return (alloc_base);
793 }
794 
795 /*
796  * Functions to adjust region counters for each size free list.
797  * Caller is responsible to acquire the ctr_mutex lock if necessary and
798  * thus can be called during startup without locks.
799  */
800 /* ARGSUSED */
801 void
802 page_ctr_add_internal(int mnode, int mtype, page_t *pp, int flags)
803 {
804 	ssize_t		r;	/* region size */
805 	ssize_t		idx;
806 	pfn_t		pfnum;
807 	int		lckidx;
808 
809 	ASSERT(mnode == PP_2_MEM_NODE(pp));
810 	ASSERT(mtype == PP_2_MTYPE(pp));
811 
812 	ASSERT(pp->p_szc < mmu_page_sizes);
813 
814 	PLCNT_INCR(pp, mnode, mtype, pp->p_szc, flags);
815 
816 	/* no counter update needed for largest page size */
817 	if (pp->p_szc >= mmu_page_sizes - 1) {
818 		return;
819 	}
820 
821 	r = pp->p_szc + 1;
822 	pfnum = pp->p_pagenum;
823 	lckidx = PP_CTR_LOCK_INDX(pp);
824 
825 	/*
826 	 * Increment the count of free pages for the current
827 	 * region. Continue looping up in region size incrementing
828 	 * count if the preceeding region is full.
829 	 */
830 	while (r < mmu_page_sizes) {
831 		idx = PNUM_TO_IDX(mnode, r, pfnum);
832 
833 		ASSERT(idx < PAGE_COUNTERS_ENTRIES(mnode, r));
834 		ASSERT(PAGE_COUNTERS(mnode, r, idx) < FULL_REGION_CNT(r));
835 
836 		if (++PAGE_COUNTERS(mnode, r, idx) != FULL_REGION_CNT(r)) {
837 			break;
838 		} else {
839 			int root_mtype = PP_2_MTYPE(PP_GROUPLEADER(pp, r));
840 			pcc_info_t *cand = &page_ctrs_cands[lckidx][r][mnode]
841 			    [MTYPE_2_MRANGE(mnode, root_mtype)];
842 
843 			cand->pcc_pages_free++;
844 			cand->pcc_color_free[PP_2_BIN_SZC(pp, r)]++;
845 		}
846 		r++;
847 	}
848 }
849 
850 void
851 page_ctr_add(int mnode, int mtype, page_t *pp, int flags)
852 {
853 	int		lckidx = PP_CTR_LOCK_INDX(pp);
854 	kmutex_t	*lock = &ctr_mutex[lckidx][mnode];
855 
856 	mutex_enter(lock);
857 	page_ctr_add_internal(mnode, mtype, pp, flags);
858 	mutex_exit(lock);
859 }
860 
861 void
862 page_ctr_sub_internal(int mnode, int mtype, page_t *pp, int flags)
863 {
864 	int		lckidx;
865 	ssize_t		r;	/* region size */
866 	ssize_t		idx;
867 	pfn_t		pfnum;
868 
869 	ASSERT(mnode == PP_2_MEM_NODE(pp));
870 	ASSERT(mtype == PP_2_MTYPE(pp));
871 
872 	ASSERT(pp->p_szc < mmu_page_sizes);
873 
874 	PLCNT_DECR(pp, mnode, mtype, pp->p_szc, flags);
875 
876 	/* no counter update needed for largest page size */
877 	if (pp->p_szc >= mmu_page_sizes - 1) {
878 		return;
879 	}
880 
881 	r = pp->p_szc + 1;
882 	pfnum = pp->p_pagenum;
883 	lckidx = PP_CTR_LOCK_INDX(pp);
884 
885 	/*
886 	 * Decrement the count of free pages for the current
887 	 * region. Continue looping up in region size decrementing
888 	 * count if the preceeding region was full.
889 	 */
890 	while (r < mmu_page_sizes) {
891 		idx = PNUM_TO_IDX(mnode, r, pfnum);
892 
893 		ASSERT(idx < PAGE_COUNTERS_ENTRIES(mnode, r));
894 		ASSERT(PAGE_COUNTERS(mnode, r, idx) > 0);
895 
896 		if (--PAGE_COUNTERS(mnode, r, idx) != FULL_REGION_CNT(r) - 1) {
897 			break;
898 		} else {
899 			int root_mtype = PP_2_MTYPE(PP_GROUPLEADER(pp, r));
900 			pcc_info_t *cand = &page_ctrs_cands[lckidx][r][mnode]
901 			    [MTYPE_2_MRANGE(mnode, root_mtype)];
902 
903 			ASSERT(cand->pcc_pages_free != 0);
904 			ASSERT(cand->pcc_color_free[PP_2_BIN_SZC(pp, r)] != 0);
905 
906 			cand->pcc_pages_free--;
907 			cand->pcc_color_free[PP_2_BIN_SZC(pp, r)]--;
908 		}
909 		r++;
910 	}
911 }
912 
913 void
914 page_ctr_sub(int mnode, int mtype, page_t *pp, int flags)
915 {
916 	int		lckidx = PP_CTR_LOCK_INDX(pp);
917 	kmutex_t	*lock = &ctr_mutex[lckidx][mnode];
918 
919 	mutex_enter(lock);
920 	page_ctr_sub_internal(mnode, mtype, pp, flags);
921 	mutex_exit(lock);
922 }
923 
924 /*
925  * Adjust page counters following a memory attach, since typically the
926  * size of the array needs to change, and the PFN to counter index
927  * mapping needs to change.
928  *
929  * It is possible this mnode did not exist at startup. In that case
930  * allocate pcc_info_t and pcc_color_free arrays. Also, allow for nranges
931  * to change (a theoretical possibility on x86), which means pcc_color_free
932  * arrays must be extended.
933  */
934 uint_t
935 page_ctrs_adjust(int mnode)
936 {
937 	pgcnt_t npgs;
938 	int	r;		/* region size */
939 	int	i;
940 	size_t	pcsz, old_csz;
941 	hpmctr_t *new_ctr, *old_ctr;
942 	pfn_t	oldbase, newbase;
943 	pfn_t	physbase, physmax;
944 	size_t	old_npgs;
945 	hpmctr_t *ctr_cache[MMU_PAGE_SIZES];
946 	size_t	size_cache[MMU_PAGE_SIZES];
947 	size_t	*color_cache[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
948 	size_t	*old_color_array[MAX_MNODE_MRANGES];
949 	pgcnt_t	colors_per_szc[MMU_PAGE_SIZES];
950 	pcc_info_t **cands_cache;
951 	pcc_info_t *old_pi, *pi;
952 	pgcnt_t *pgcntp;
953 	int nr, old_nranges, mrange, nranges = MNODE_RANGE_CNT(mnode);
954 	int cands_cache_nranges;
955 	int old_maxmrange, new_maxmrange;
956 	int rc = 0;
957 
958 	cands_cache = kmem_zalloc(sizeof (pcc_info_t *) * NPC_MUTEX *
959 	    MMU_PAGE_SIZES, KM_NOSLEEP);
960 	if (cands_cache == NULL)
961 		return (ENOMEM);
962 
963 	i = -1;
964 	HPM_COUNTERS_LIMITS(mnode, physbase, physmax, i);
965 
966 	newbase = physbase & ~PC_BASE_ALIGN_MASK;
967 	npgs = roundup(physmax, PC_BASE_ALIGN) - newbase;
968 
969 	/* prepare to free non-null pointers on the way out */
970 	cands_cache_nranges = nranges;
971 	bzero(ctr_cache, sizeof (ctr_cache));
972 	bzero(color_cache, sizeof (color_cache));
973 
974 	/*
975 	 * We need to determine how many page colors there are for each
976 	 * page size in order to allocate memory for any color specific
977 	 * arrays.
978 	 */
979 	for (r = 0; r < mmu_page_sizes; r++) {
980 		colors_per_szc[r] = PAGE_GET_PAGECOLORS(r);
981 	}
982 
983 	/*
984 	 * Preallocate all of the new hpm_counters arrays as we can't
985 	 * hold the page_ctrs_rwlock as a writer and allocate memory.
986 	 * If we can't allocate all of the arrays, undo our work so far
987 	 * and return failure.
988 	 */
989 	for (r = 1; r < mmu_page_sizes; r++) {
990 		pcsz = npgs >> PAGE_BSZS_SHIFT(r);
991 		size_cache[r] = pcsz;
992 		ctr_cache[r] = kmem_zalloc(pcsz *
993 		    sizeof (hpmctr_t), KM_NOSLEEP);
994 		if (ctr_cache[r] == NULL) {
995 			rc = ENOMEM;
996 			goto cleanup;
997 		}
998 	}
999 
1000 	/*
1001 	 * Preallocate all of the new color current arrays as we can't
1002 	 * hold the page_ctrs_rwlock as a writer and allocate memory.
1003 	 * If we can't allocate all of the arrays, undo our work so far
1004 	 * and return failure.
1005 	 */
1006 	for (r = 1; r < mmu_page_sizes; r++) {
1007 		for (mrange = 0; mrange < nranges; mrange++) {
1008 			color_cache[r][mrange] = kmem_zalloc(sizeof (size_t) *
1009 			    colors_per_szc[r], KM_NOSLEEP);
1010 			if (color_cache[r][mrange] == NULL) {
1011 				rc = ENOMEM;
1012 				goto cleanup;
1013 			}
1014 		}
1015 	}
1016 
1017 	/*
1018 	 * Preallocate all of the new pcc_info_t arrays as we can't
1019 	 * hold the page_ctrs_rwlock as a writer and allocate memory.
1020 	 * If we can't allocate all of the arrays, undo our work so far
1021 	 * and return failure.
1022 	 */
1023 	for (r = 1; r < mmu_page_sizes; r++) {
1024 		for (i = 0; i < NPC_MUTEX; i++) {
1025 			pi = kmem_zalloc(nranges * sizeof (pcc_info_t),
1026 			    KM_NOSLEEP);
1027 			if (pi == NULL) {
1028 				rc = ENOMEM;
1029 				goto cleanup;
1030 			}
1031 			cands_cache[i * MMU_PAGE_SIZES + r] = pi;
1032 
1033 			for (mrange = 0; mrange < nranges; mrange++, pi++) {
1034 				pgcntp = kmem_zalloc(colors_per_szc[r] *
1035 				    sizeof (pgcnt_t), KM_NOSLEEP);
1036 				if (pgcntp == NULL) {
1037 					rc = ENOMEM;
1038 					goto cleanup;
1039 				}
1040 				pi->pcc_color_free = pgcntp;
1041 			}
1042 		}
1043 	}
1044 
1045 	/*
1046 	 * Grab the write lock to prevent others from walking these arrays
1047 	 * while we are modifying them.
1048 	 */
1049 	PAGE_CTRS_WRITE_LOCK(mnode);
1050 
1051 	old_nranges = mnode_nranges[mnode];
1052 	cands_cache_nranges = old_nranges;
1053 	mnode_nranges[mnode] = nranges;
1054 	old_maxmrange = mnode_maxmrange[mnode];
1055 	mnode_maxmrange[mnode] = MNODE_MAX_MRANGE(mnode);
1056 	new_maxmrange = mnode_maxmrange[mnode];
1057 
1058 	for (r = 1; r < mmu_page_sizes; r++) {
1059 		PAGE_COUNTERS_SHIFT(mnode, r) = PAGE_BSZS_SHIFT(r);
1060 		old_ctr = PAGE_COUNTERS_COUNTERS(mnode, r);
1061 		old_csz = PAGE_COUNTERS_ENTRIES(mnode, r);
1062 		oldbase = PAGE_COUNTERS_BASE(mnode, r);
1063 		old_npgs = old_csz << PAGE_COUNTERS_SHIFT(mnode, r);
1064 		for (mrange = 0; mrange < MAX_MNODE_MRANGES; mrange++) {
1065 			old_color_array[mrange] =
1066 			    PAGE_COUNTERS_CURRENT_COLOR_ARRAY(mnode,
1067 			    r, mrange);
1068 		}
1069 
1070 		pcsz = npgs >> PAGE_COUNTERS_SHIFT(mnode, r);
1071 		new_ctr = ctr_cache[r];
1072 		ctr_cache[r] = NULL;
1073 		if (old_ctr != NULL &&
1074 		    (oldbase + old_npgs > newbase) &&
1075 		    (newbase + npgs > oldbase)) {
1076 			/*
1077 			 * Map the intersection of the old and new
1078 			 * counters into the new array.
1079 			 */
1080 			size_t offset;
1081 			if (newbase > oldbase) {
1082 				offset = (newbase - oldbase) >>
1083 				    PAGE_COUNTERS_SHIFT(mnode, r);
1084 				bcopy(old_ctr + offset, new_ctr,
1085 				    MIN(pcsz, (old_csz - offset)) *
1086 				    sizeof (hpmctr_t));
1087 			} else {
1088 				offset = (oldbase - newbase) >>
1089 				    PAGE_COUNTERS_SHIFT(mnode, r);
1090 				bcopy(old_ctr, new_ctr + offset,
1091 				    MIN(pcsz - offset, old_csz) *
1092 				    sizeof (hpmctr_t));
1093 			}
1094 		}
1095 
1096 		PAGE_COUNTERS_COUNTERS(mnode, r) = new_ctr;
1097 		PAGE_COUNTERS_ENTRIES(mnode, r) = pcsz;
1098 		PAGE_COUNTERS_BASE(mnode, r) = newbase;
1099 
1100 		/* update shared hpm_counters in other mnodes */
1101 		if (interleaved_mnodes) {
1102 			for (i = 0; i < max_mem_nodes; i++) {
1103 				if (i == mnode)
1104 					continue;
1105 				if (mem_node_config[i].exists == 0)
1106 					continue;
1107 				ASSERT(PAGE_COUNTERS_COUNTERS(i, r) == old_ctr);
1108 				PAGE_COUNTERS_COUNTERS(i, r) = new_ctr;
1109 				PAGE_COUNTERS_ENTRIES(i, r) = pcsz;
1110 				PAGE_COUNTERS_BASE(i, r) = newbase;
1111 			}
1112 		}
1113 
1114 		for (mrange = 0; mrange < MAX_MNODE_MRANGES; mrange++) {
1115 			PAGE_COUNTERS_CURRENT_COLOR_ARRAY(mnode, r, mrange) =
1116 			    color_cache[r][mrange];
1117 			color_cache[r][mrange] = NULL;
1118 		}
1119 		/*
1120 		 * for now, just reset on these events as it's probably
1121 		 * not worthwhile to try and optimize this.
1122 		 */
1123 		for (i = 0; i < colors_per_szc[r]; i++) {
1124 			uint_t color_mask = colors_per_szc[r] - 1;
1125 			int mlo = interleaved_mnodes ? 0 : mnode;
1126 			int mhi = interleaved_mnodes ? max_mem_nodes :
1127 			    (mnode + 1);
1128 			int m;
1129 			pfn_t  pfnum = newbase;
1130 			size_t idx;
1131 			MEM_NODE_ITERATOR_DECL(it);
1132 
1133 			for (m = mlo; m < mhi; m++) {
1134 				if (mem_node_config[m].exists == 0)
1135 					continue;
1136 				MEM_NODE_ITERATOR_INIT(pfnum, m, r, &it);
1137 				if (pfnum == (pfn_t)-1) {
1138 					idx = 0;
1139 				} else {
1140 					PAGE_NEXT_PFN_FOR_COLOR(pfnum, r, i,
1141 					    color_mask, color_mask, &it);
1142 					idx = PNUM_TO_IDX(m, r, pfnum);
1143 					idx = (idx < pcsz) ? idx : 0;
1144 				}
1145 				for (mrange = 0; mrange < nranges; mrange++) {
1146 					PAGE_COUNTERS_CURRENT_COLOR(m,
1147 					    r, i, mrange) = idx;
1148 				}
1149 			}
1150 		}
1151 
1152 		/* cache info for freeing out of the critical path */
1153 		if ((caddr_t)old_ctr >= kernelheap &&
1154 		    (caddr_t)old_ctr < ekernelheap) {
1155 			ctr_cache[r] = old_ctr;
1156 			size_cache[r] = old_csz;
1157 		}
1158 		for (mrange = 0; mrange < MAX_MNODE_MRANGES; mrange++) {
1159 			size_t *tmp = old_color_array[mrange];
1160 			if ((caddr_t)tmp >= kernelheap &&
1161 			    (caddr_t)tmp < ekernelheap) {
1162 				color_cache[r][mrange] = tmp;
1163 			}
1164 		}
1165 		/*
1166 		 * Verify that PNUM_TO_IDX and IDX_TO_PNUM
1167 		 * satisfy the identity requirement.
1168 		 * We should be able to go from one to the other
1169 		 * and get consistent values.
1170 		 */
1171 		ASSERT(PNUM_TO_IDX(mnode, r,
1172 		    (IDX_TO_PNUM(mnode, r, 0))) == 0);
1173 		ASSERT(IDX_TO_PNUM(mnode, r,
1174 		    (PNUM_TO_IDX(mnode, r, newbase))) == newbase);
1175 
1176 		/* pcc_info_t and pcc_color_free */
1177 		for (i = 0; i < NPC_MUTEX; i++) {
1178 			pcc_info_t *epi;
1179 			pcc_info_t *eold_pi;
1180 
1181 			pi = cands_cache[i * MMU_PAGE_SIZES + r];
1182 			old_pi = page_ctrs_cands[i][r][mnode];
1183 			page_ctrs_cands[i][r][mnode] = pi;
1184 			cands_cache[i * MMU_PAGE_SIZES + r] = old_pi;
1185 
1186 			/* preserve old pcc_color_free values, if any */
1187 			if (old_pi == NULL)
1188 				continue;
1189 
1190 			/*
1191 			 * when/if x86 does DR, must account for
1192 			 * possible change in range index when
1193 			 * preserving pcc_info
1194 			 */
1195 			epi = &pi[nranges];
1196 			eold_pi = &old_pi[old_nranges];
1197 			if (new_maxmrange > old_maxmrange) {
1198 				pi += new_maxmrange - old_maxmrange;
1199 			} else if (new_maxmrange < old_maxmrange) {
1200 				old_pi += old_maxmrange - new_maxmrange;
1201 			}
1202 			for (; pi < epi && old_pi < eold_pi; pi++, old_pi++) {
1203 				pcc_info_t tmp = *pi;
1204 				*pi = *old_pi;
1205 				*old_pi = tmp;
1206 			}
1207 		}
1208 	}
1209 	PAGE_CTRS_WRITE_UNLOCK(mnode);
1210 
1211 	/*
1212 	 * Now that we have dropped the write lock, it is safe to free all
1213 	 * of the memory we have cached above.
1214 	 * We come thru here to free memory when pre-alloc fails, and also to
1215 	 * free old pointers which were recorded while locked.
1216 	 */
1217 cleanup:
1218 	for (r = 1; r < mmu_page_sizes; r++) {
1219 		if (ctr_cache[r] != NULL) {
1220 			kmem_free(ctr_cache[r],
1221 			    size_cache[r] * sizeof (hpmctr_t));
1222 		}
1223 		for (mrange = 0; mrange < MAX_MNODE_MRANGES; mrange++) {
1224 			if (color_cache[r][mrange] != NULL) {
1225 				kmem_free(color_cache[r][mrange],
1226 				    colors_per_szc[r] * sizeof (size_t));
1227 			}
1228 		}
1229 		for (i = 0; i < NPC_MUTEX; i++) {
1230 			pi = cands_cache[i * MMU_PAGE_SIZES + r];
1231 			if (pi == NULL)
1232 				continue;
1233 			nr = cands_cache_nranges;
1234 			for (mrange = 0; mrange < nr; mrange++, pi++) {
1235 				pgcntp = pi->pcc_color_free;
1236 				if (pgcntp == NULL)
1237 					continue;
1238 				if ((caddr_t)pgcntp >= kernelheap &&
1239 				    (caddr_t)pgcntp < ekernelheap) {
1240 					kmem_free(pgcntp,
1241 					    colors_per_szc[r] *
1242 					    sizeof (pgcnt_t));
1243 				}
1244 			}
1245 			pi = cands_cache[i * MMU_PAGE_SIZES + r];
1246 			if ((caddr_t)pi >= kernelheap &&
1247 			    (caddr_t)pi < ekernelheap) {
1248 				kmem_free(pi, nr * sizeof (pcc_info_t));
1249 			}
1250 		}
1251 	}
1252 
1253 	kmem_free(cands_cache,
1254 	    sizeof (pcc_info_t *) * NPC_MUTEX * MMU_PAGE_SIZES);
1255 	return (rc);
1256 }
1257 
1258 
1259 #ifdef DEBUG
1260 
1261 /*
1262  * confirm pp is a large page corresponding to szc
1263  */
1264 void
1265 chk_lpg(page_t *pp, uchar_t szc)
1266 {
1267 	spgcnt_t npgs = page_get_pagecnt(pp->p_szc);
1268 	uint_t noreloc;
1269 
1270 	if (npgs == 1) {
1271 		ASSERT(pp->p_szc == 0);
1272 		ASSERT(pp->p_next == pp);
1273 		ASSERT(pp->p_prev == pp);
1274 		return;
1275 	}
1276 
1277 	ASSERT(pp->p_vpnext == pp || pp->p_vpnext == NULL);
1278 	ASSERT(pp->p_vpprev == pp || pp->p_vpprev == NULL);
1279 
1280 	ASSERT(IS_P2ALIGNED(pp->p_pagenum, npgs));
1281 	ASSERT(pp->p_pagenum == (pp->p_next->p_pagenum - 1));
1282 	ASSERT(pp->p_prev->p_pagenum == (pp->p_pagenum + (npgs - 1)));
1283 	ASSERT(pp->p_prev == (pp + (npgs - 1)));
1284 
1285 	/*
1286 	 * Check list of pages.
1287 	 */
1288 	noreloc = PP_ISNORELOC(pp);
1289 	while (npgs--) {
1290 		if (npgs != 0) {
1291 			ASSERT(pp->p_pagenum == pp->p_next->p_pagenum - 1);
1292 			ASSERT(pp->p_next == (pp + 1));
1293 		}
1294 		ASSERT(pp->p_szc == szc);
1295 		ASSERT(PP_ISFREE(pp));
1296 		ASSERT(PP_ISAGED(pp));
1297 		ASSERT(pp->p_vpnext == pp || pp->p_vpnext == NULL);
1298 		ASSERT(pp->p_vpprev == pp || pp->p_vpprev == NULL);
1299 		ASSERT(pp->p_vnode  == NULL);
1300 		ASSERT(PP_ISNORELOC(pp) == noreloc);
1301 
1302 		pp = pp->p_next;
1303 	}
1304 }
1305 #endif /* DEBUG */
1306 
1307 void
1308 page_freelist_lock(int mnode)
1309 {
1310 	int i;
1311 	for (i = 0; i < NPC_MUTEX; i++) {
1312 		mutex_enter(FPC_MUTEX(mnode, i));
1313 		mutex_enter(CPC_MUTEX(mnode, i));
1314 	}
1315 }
1316 
1317 void
1318 page_freelist_unlock(int mnode)
1319 {
1320 	int i;
1321 	for (i = 0; i < NPC_MUTEX; i++) {
1322 		mutex_exit(FPC_MUTEX(mnode, i));
1323 		mutex_exit(CPC_MUTEX(mnode, i));
1324 	}
1325 }
1326 
1327 /*
1328  * add pp to the specified page list. Defaults to head of the page list
1329  * unless PG_LIST_TAIL is specified.
1330  */
1331 void
1332 page_list_add(page_t *pp, int flags)
1333 {
1334 	page_t		**ppp;
1335 	kmutex_t	*pcm;
1336 	uint_t		bin, mtype;
1337 	int		mnode;
1338 
1339 	ASSERT(PAGE_EXCL(pp) || (flags & PG_LIST_ISINIT));
1340 	ASSERT(PP_ISFREE(pp));
1341 	ASSERT(!hat_page_is_mapped(pp));
1342 	ASSERT(hat_page_getshare(pp) == 0);
1343 
1344 	/*
1345 	 * Large pages should be freed via page_list_add_pages().
1346 	 */
1347 	ASSERT(pp->p_szc == 0);
1348 
1349 	/*
1350 	 * Don't need to lock the freelist first here
1351 	 * because the page isn't on the freelist yet.
1352 	 * This means p_szc can't change on us.
1353 	 */
1354 
1355 	bin = PP_2_BIN(pp);
1356 	mnode = PP_2_MEM_NODE(pp);
1357 	mtype = PP_2_MTYPE(pp);
1358 
1359 	if (flags & PG_LIST_ISINIT) {
1360 		/*
1361 		 * PG_LIST_ISINIT is set during system startup (ie. single
1362 		 * threaded), add a page to the free list and add to the
1363 		 * the free region counters w/o any locking
1364 		 */
1365 		ppp = &PAGE_FREELISTS(mnode, 0, bin, mtype);
1366 
1367 		/* inline version of page_add() */
1368 		if (*ppp != NULL) {
1369 			pp->p_next = *ppp;
1370 			pp->p_prev = (*ppp)->p_prev;
1371 			(*ppp)->p_prev = pp;
1372 			pp->p_prev->p_next = pp;
1373 		} else
1374 			*ppp = pp;
1375 
1376 		page_ctr_add_internal(mnode, mtype, pp, flags);
1377 		VM_STAT_ADD(vmm_vmstats.pladd_free[0]);
1378 	} else {
1379 		pcm = PC_BIN_MUTEX(mnode, bin, flags);
1380 
1381 		if (flags & PG_FREE_LIST) {
1382 			VM_STAT_ADD(vmm_vmstats.pladd_free[0]);
1383 			ASSERT(PP_ISAGED(pp));
1384 			ppp = &PAGE_FREELISTS(mnode, 0, bin, mtype);
1385 
1386 		} else {
1387 			VM_STAT_ADD(vmm_vmstats.pladd_cache);
1388 			ASSERT(pp->p_vnode);
1389 			ASSERT((pp->p_offset & PAGEOFFSET) == 0);
1390 			ppp = &PAGE_CACHELISTS(mnode, bin, mtype);
1391 		}
1392 		mutex_enter(pcm);
1393 		page_add(ppp, pp);
1394 
1395 		if (flags & PG_LIST_TAIL)
1396 			*ppp = (*ppp)->p_next;
1397 		/*
1398 		 * Add counters before releasing pcm mutex to avoid a race with
1399 		 * page_freelist_coalesce and page_freelist_split.
1400 		 */
1401 		page_ctr_add(mnode, mtype, pp, flags);
1402 		mutex_exit(pcm);
1403 	}
1404 
1405 
1406 #if defined(__sparc)
1407 	if (PP_ISNORELOC(pp)) {
1408 		kcage_freemem_add(1);
1409 	}
1410 #endif
1411 	/*
1412 	 * It is up to the caller to unlock the page!
1413 	 */
1414 	ASSERT(PAGE_EXCL(pp) || (flags & PG_LIST_ISINIT));
1415 }
1416 
1417 
1418 #ifdef __sparc
1419 /*
1420  * This routine is only used by kcage_init during system startup.
1421  * It performs the function of page_list_sub/PP_SETNORELOC/page_list_add
1422  * without the overhead of taking locks and updating counters.
1423  */
1424 void
1425 page_list_noreloc_startup(page_t *pp)
1426 {
1427 	page_t		**ppp;
1428 	uint_t		bin;
1429 	int		mnode;
1430 	int		mtype;
1431 	int		flags = 0;
1432 
1433 	/*
1434 	 * If this is a large page on the freelist then
1435 	 * break it up into smaller pages.
1436 	 */
1437 	if (pp->p_szc != 0)
1438 		page_boot_demote(pp);
1439 
1440 	/*
1441 	 * Get list page is currently on.
1442 	 */
1443 	bin = PP_2_BIN(pp);
1444 	mnode = PP_2_MEM_NODE(pp);
1445 	mtype = PP_2_MTYPE(pp);
1446 	ASSERT(mtype == MTYPE_RELOC);
1447 	ASSERT(pp->p_szc == 0);
1448 
1449 	if (PP_ISAGED(pp)) {
1450 		ppp = &PAGE_FREELISTS(mnode, 0, bin, mtype);
1451 		flags |= PG_FREE_LIST;
1452 	} else {
1453 		ppp = &PAGE_CACHELISTS(mnode, bin, mtype);
1454 		flags |= PG_CACHE_LIST;
1455 	}
1456 
1457 	ASSERT(*ppp != NULL);
1458 
1459 	/*
1460 	 * Delete page from current list.
1461 	 */
1462 	if (*ppp == pp)
1463 		*ppp = pp->p_next;		/* go to next page */
1464 	if (*ppp == pp) {
1465 		*ppp = NULL;			/* page list is gone */
1466 	} else {
1467 		pp->p_prev->p_next = pp->p_next;
1468 		pp->p_next->p_prev = pp->p_prev;
1469 	}
1470 
1471 	/*
1472 	 * Decrement page counters
1473 	 */
1474 	page_ctr_sub_internal(mnode, mtype, pp, flags);
1475 
1476 	/*
1477 	 * Set no reloc for cage initted pages.
1478 	 */
1479 	PP_SETNORELOC(pp);
1480 
1481 	mtype = PP_2_MTYPE(pp);
1482 	ASSERT(mtype == MTYPE_NORELOC);
1483 
1484 	/*
1485 	 * Get new list for page.
1486 	 */
1487 	if (PP_ISAGED(pp)) {
1488 		ppp = &PAGE_FREELISTS(mnode, 0, bin, mtype);
1489 	} else {
1490 		ppp = &PAGE_CACHELISTS(mnode, bin, mtype);
1491 	}
1492 
1493 	/*
1494 	 * Insert page on new list.
1495 	 */
1496 	if (*ppp == NULL) {
1497 		*ppp = pp;
1498 		pp->p_next = pp->p_prev = pp;
1499 	} else {
1500 		pp->p_next = *ppp;
1501 		pp->p_prev = (*ppp)->p_prev;
1502 		(*ppp)->p_prev = pp;
1503 		pp->p_prev->p_next = pp;
1504 	}
1505 
1506 	/*
1507 	 * Increment page counters
1508 	 */
1509 	page_ctr_add_internal(mnode, mtype, pp, flags);
1510 
1511 	/*
1512 	 * Update cage freemem counter
1513 	 */
1514 	atomic_add_long(&kcage_freemem, 1);
1515 }
1516 #else	/* __sparc */
1517 
1518 /* ARGSUSED */
1519 void
1520 page_list_noreloc_startup(page_t *pp)
1521 {
1522 	panic("page_list_noreloc_startup: should be here only for sparc");
1523 }
1524 #endif
1525 
1526 void
1527 page_list_add_pages(page_t *pp, int flags)
1528 {
1529 	kmutex_t *pcm;
1530 	pgcnt_t	pgcnt;
1531 	uint_t	bin, mtype, i;
1532 	int	mnode;
1533 
1534 	/* default to freelist/head */
1535 	ASSERT((flags & (PG_CACHE_LIST | PG_LIST_TAIL)) == 0);
1536 
1537 	CHK_LPG(pp, pp->p_szc);
1538 	VM_STAT_ADD(vmm_vmstats.pladd_free[pp->p_szc]);
1539 
1540 	bin = PP_2_BIN(pp);
1541 	mnode = PP_2_MEM_NODE(pp);
1542 	mtype = PP_2_MTYPE(pp);
1543 
1544 	if (flags & PG_LIST_ISINIT) {
1545 		ASSERT(pp->p_szc == mmu_page_sizes - 1);
1546 		page_vpadd(&PAGE_FREELISTS(mnode, pp->p_szc, bin, mtype), pp);
1547 		ASSERT(!PP_ISNORELOC(pp));
1548 		PLCNT_INCR(pp, mnode, mtype, pp->p_szc, flags);
1549 	} else {
1550 
1551 		ASSERT(pp->p_szc != 0 && pp->p_szc < mmu_page_sizes);
1552 
1553 		pcm = PC_BIN_MUTEX(mnode, bin, PG_FREE_LIST);
1554 
1555 		mutex_enter(pcm);
1556 		page_vpadd(&PAGE_FREELISTS(mnode, pp->p_szc, bin, mtype), pp);
1557 		page_ctr_add(mnode, mtype, pp, PG_FREE_LIST);
1558 		mutex_exit(pcm);
1559 
1560 		pgcnt = page_get_pagecnt(pp->p_szc);
1561 #if defined(__sparc)
1562 		if (PP_ISNORELOC(pp))
1563 			kcage_freemem_add(pgcnt);
1564 #endif
1565 		for (i = 0; i < pgcnt; i++, pp++)
1566 			page_unlock_nocapture(pp);
1567 	}
1568 }
1569 
1570 /*
1571  * During boot, need to demote a large page to base
1572  * pagesize pages for seg_kmem for use in boot_alloc()
1573  */
1574 void
1575 page_boot_demote(page_t *pp)
1576 {
1577 	ASSERT(pp->p_szc != 0);
1578 	ASSERT(PP_ISFREE(pp));
1579 	ASSERT(PP_ISAGED(pp));
1580 
1581 	(void) page_demote(PP_2_MEM_NODE(pp),
1582 	    PFN_BASE(pp->p_pagenum, pp->p_szc), 0, pp->p_szc, 0, PC_NO_COLOR,
1583 	    PC_FREE);
1584 
1585 	ASSERT(PP_ISFREE(pp));
1586 	ASSERT(PP_ISAGED(pp));
1587 	ASSERT(pp->p_szc == 0);
1588 }
1589 
1590 /*
1591  * Take a particular page off of whatever freelist the page
1592  * is claimed to be on.
1593  *
1594  * NOTE: Only used for PAGESIZE pages.
1595  */
1596 void
1597 page_list_sub(page_t *pp, int flags)
1598 {
1599 	int		bin;
1600 	uint_t		mtype;
1601 	int		mnode;
1602 	kmutex_t	*pcm;
1603 	page_t		**ppp;
1604 
1605 	ASSERT(PAGE_EXCL(pp));
1606 	ASSERT(PP_ISFREE(pp));
1607 
1608 	/*
1609 	 * The p_szc field can only be changed by page_promote()
1610 	 * and page_demote(). Only free pages can be promoted and
1611 	 * demoted and the free list MUST be locked during these
1612 	 * operations. So to prevent a race in page_list_sub()
1613 	 * between computing which bin of the freelist lock to
1614 	 * grab and actually grabing the lock we check again that
1615 	 * the bin we locked is still the correct one. Notice that
1616 	 * the p_szc field could have actually changed on us but
1617 	 * if the bin happens to still be the same we are safe.
1618 	 */
1619 try_again:
1620 	bin = PP_2_BIN(pp);
1621 	mnode = PP_2_MEM_NODE(pp);
1622 	pcm = PC_BIN_MUTEX(mnode, bin, flags);
1623 	mutex_enter(pcm);
1624 	if (PP_2_BIN(pp) != bin) {
1625 		mutex_exit(pcm);
1626 		goto try_again;
1627 	}
1628 	mtype = PP_2_MTYPE(pp);
1629 
1630 	if (flags & PG_FREE_LIST) {
1631 		VM_STAT_ADD(vmm_vmstats.plsub_free[0]);
1632 		ASSERT(PP_ISAGED(pp));
1633 		ppp = &PAGE_FREELISTS(mnode, pp->p_szc, bin, mtype);
1634 	} else {
1635 		VM_STAT_ADD(vmm_vmstats.plsub_cache);
1636 		ASSERT(!PP_ISAGED(pp));
1637 		ppp = &PAGE_CACHELISTS(mnode, bin, mtype);
1638 	}
1639 
1640 	/*
1641 	 * Common PAGESIZE case.
1642 	 *
1643 	 * Note that we locked the freelist. This prevents
1644 	 * any page promotion/demotion operations. Therefore
1645 	 * the p_szc will not change until we drop pcm mutex.
1646 	 */
1647 	if (pp->p_szc == 0) {
1648 		page_sub(ppp, pp);
1649 		/*
1650 		 * Subtract counters before releasing pcm mutex
1651 		 * to avoid race with page_freelist_coalesce.
1652 		 */
1653 		page_ctr_sub(mnode, mtype, pp, flags);
1654 		mutex_exit(pcm);
1655 
1656 #if defined(__sparc)
1657 		if (PP_ISNORELOC(pp)) {
1658 			kcage_freemem_sub(1);
1659 		}
1660 #endif
1661 		return;
1662 	}
1663 
1664 	/*
1665 	 * Large pages on the cache list are not supported.
1666 	 */
1667 	if (flags & PG_CACHE_LIST)
1668 		panic("page_list_sub: large page on cachelist");
1669 
1670 	/*
1671 	 * Slow but rare.
1672 	 *
1673 	 * Somebody wants this particular page which is part
1674 	 * of a large page. In this case we just demote the page
1675 	 * if it's on the freelist.
1676 	 *
1677 	 * We have to drop pcm before locking the entire freelist.
1678 	 * Once we have re-locked the freelist check to make sure
1679 	 * the page hasn't already been demoted or completely
1680 	 * freed.
1681 	 */
1682 	mutex_exit(pcm);
1683 	page_freelist_lock(mnode);
1684 	if (pp->p_szc != 0) {
1685 		/*
1686 		 * Large page is on freelist.
1687 		 */
1688 		(void) page_demote(mnode, PFN_BASE(pp->p_pagenum, pp->p_szc),
1689 		    0, pp->p_szc, 0, PC_NO_COLOR, PC_FREE);
1690 	}
1691 	ASSERT(PP_ISFREE(pp));
1692 	ASSERT(PP_ISAGED(pp));
1693 	ASSERT(pp->p_szc == 0);
1694 
1695 	/*
1696 	 * Subtract counters before releasing pcm mutex
1697 	 * to avoid race with page_freelist_coalesce.
1698 	 */
1699 	bin = PP_2_BIN(pp);
1700 	mtype = PP_2_MTYPE(pp);
1701 	ppp = &PAGE_FREELISTS(mnode, pp->p_szc, bin, mtype);
1702 
1703 	page_sub(ppp, pp);
1704 	page_ctr_sub(mnode, mtype, pp, flags);
1705 	page_freelist_unlock(mnode);
1706 
1707 #if defined(__sparc)
1708 	if (PP_ISNORELOC(pp)) {
1709 		kcage_freemem_sub(1);
1710 	}
1711 #endif
1712 }
1713 
1714 void
1715 page_list_sub_pages(page_t *pp, uint_t szc)
1716 {
1717 	kmutex_t *pcm;
1718 	uint_t	bin, mtype;
1719 	int	mnode;
1720 
1721 	ASSERT(PAGE_EXCL(pp));
1722 	ASSERT(PP_ISFREE(pp));
1723 	ASSERT(PP_ISAGED(pp));
1724 
1725 	/*
1726 	 * See comment in page_list_sub().
1727 	 */
1728 try_again:
1729 	bin = PP_2_BIN(pp);
1730 	mnode = PP_2_MEM_NODE(pp);
1731 	pcm = PC_BIN_MUTEX(mnode, bin, PG_FREE_LIST);
1732 	mutex_enter(pcm);
1733 	if (PP_2_BIN(pp) != bin) {
1734 		mutex_exit(pcm);
1735 		goto	try_again;
1736 	}
1737 
1738 	/*
1739 	 * If we're called with a page larger than szc or it got
1740 	 * promoted above szc before we locked the freelist then
1741 	 * drop pcm and re-lock entire freelist. If page still larger
1742 	 * than szc then demote it.
1743 	 */
1744 	if (pp->p_szc > szc) {
1745 		mutex_exit(pcm);
1746 		pcm = NULL;
1747 		page_freelist_lock(mnode);
1748 		if (pp->p_szc > szc) {
1749 			VM_STAT_ADD(vmm_vmstats.plsubpages_szcbig);
1750 			(void) page_demote(mnode,
1751 			    PFN_BASE(pp->p_pagenum, pp->p_szc), 0,
1752 			    pp->p_szc, szc, PC_NO_COLOR, PC_FREE);
1753 		}
1754 		bin = PP_2_BIN(pp);
1755 	}
1756 	ASSERT(PP_ISFREE(pp));
1757 	ASSERT(PP_ISAGED(pp));
1758 	ASSERT(pp->p_szc <= szc);
1759 	ASSERT(pp == PP_PAGEROOT(pp));
1760 
1761 	VM_STAT_ADD(vmm_vmstats.plsub_free[pp->p_szc]);
1762 
1763 	mtype = PP_2_MTYPE(pp);
1764 	if (pp->p_szc != 0) {
1765 		page_vpsub(&PAGE_FREELISTS(mnode, pp->p_szc, bin, mtype), pp);
1766 		CHK_LPG(pp, pp->p_szc);
1767 	} else {
1768 		VM_STAT_ADD(vmm_vmstats.plsubpages_szc0);
1769 		page_sub(&PAGE_FREELISTS(mnode, pp->p_szc, bin, mtype), pp);
1770 	}
1771 	page_ctr_sub(mnode, mtype, pp, PG_FREE_LIST);
1772 
1773 	if (pcm != NULL) {
1774 		mutex_exit(pcm);
1775 	} else {
1776 		page_freelist_unlock(mnode);
1777 	}
1778 
1779 #if defined(__sparc)
1780 	if (PP_ISNORELOC(pp)) {
1781 		pgcnt_t	pgcnt;
1782 
1783 		pgcnt = page_get_pagecnt(pp->p_szc);
1784 		kcage_freemem_sub(pgcnt);
1785 	}
1786 #endif
1787 }
1788 
1789 /*
1790  * Add the page to the front of a linked list of pages
1791  * using the p_next & p_prev pointers for the list.
1792  * The caller is responsible for protecting the list pointers.
1793  */
1794 void
1795 mach_page_add(page_t **ppp, page_t *pp)
1796 {
1797 	if (*ppp == NULL) {
1798 		pp->p_next = pp->p_prev = pp;
1799 	} else {
1800 		pp->p_next = *ppp;
1801 		pp->p_prev = (*ppp)->p_prev;
1802 		(*ppp)->p_prev = pp;
1803 		pp->p_prev->p_next = pp;
1804 	}
1805 	*ppp = pp;
1806 }
1807 
1808 /*
1809  * Remove this page from a linked list of pages
1810  * using the p_next & p_prev pointers for the list.
1811  *
1812  * The caller is responsible for protecting the list pointers.
1813  */
1814 void
1815 mach_page_sub(page_t **ppp, page_t *pp)
1816 {
1817 	ASSERT(PP_ISFREE(pp));
1818 
1819 	if (*ppp == NULL || pp == NULL)
1820 		panic("mach_page_sub");
1821 
1822 	if (*ppp == pp)
1823 		*ppp = pp->p_next;		/* go to next page */
1824 
1825 	if (*ppp == pp)
1826 		*ppp = NULL;			/* page list is gone */
1827 	else {
1828 		pp->p_prev->p_next = pp->p_next;
1829 		pp->p_next->p_prev = pp->p_prev;
1830 	}
1831 	pp->p_prev = pp->p_next = pp;		/* make pp a list of one */
1832 }
1833 
1834 /*
1835  * Routine fsflush uses to gradually coalesce the free list into larger pages.
1836  */
1837 void
1838 page_promote_size(page_t *pp, uint_t cur_szc)
1839 {
1840 	pfn_t pfn;
1841 	int mnode;
1842 	int idx;
1843 	int new_szc = cur_szc + 1;
1844 	int full = FULL_REGION_CNT(new_szc);
1845 
1846 	pfn = page_pptonum(pp);
1847 	mnode = PFN_2_MEM_NODE(pfn);
1848 
1849 	page_freelist_lock(mnode);
1850 
1851 	idx = PNUM_TO_IDX(mnode, new_szc, pfn);
1852 	if (PAGE_COUNTERS(mnode, new_szc, idx) == full)
1853 		(void) page_promote(mnode, pfn, new_szc, PC_FREE, PC_MTYPE_ANY);
1854 
1855 	page_freelist_unlock(mnode);
1856 }
1857 
1858 static uint_t page_promote_err;
1859 static uint_t page_promote_noreloc_err;
1860 
1861 /*
1862  * Create a single larger page (of szc new_szc) from smaller contiguous pages
1863  * for the given mnode starting at pfnum. Pages involved are on the freelist
1864  * before the call and may be returned to the caller if requested, otherwise
1865  * they will be placed back on the freelist.
1866  * If flags is PC_ALLOC, then the large page will be returned to the user in
1867  * a state which is consistent with a page being taken off the freelist.  If
1868  * we failed to lock the new large page, then we will return NULL to the
1869  * caller and put the large page on the freelist instead.
1870  * If flags is PC_FREE, then the large page will be placed on the freelist,
1871  * and NULL will be returned.
1872  * The caller is responsible for locking the freelist as well as any other
1873  * accounting which needs to be done for a returned page.
1874  *
1875  * RFE: For performance pass in pp instead of pfnum so
1876  * 	we can avoid excessive calls to page_numtopp_nolock().
1877  *	This would depend on an assumption that all contiguous
1878  *	pages are in the same memseg so we can just add/dec
1879  *	our pp.
1880  *
1881  * Lock ordering:
1882  *
1883  *	There is a potential but rare deadlock situation
1884  *	for page promotion and demotion operations. The problem
1885  *	is there are two paths into the freelist manager and
1886  *	they have different lock orders:
1887  *
1888  *	page_create()
1889  *		lock freelist
1890  *		page_lock(EXCL)
1891  *		unlock freelist
1892  *		return
1893  *		caller drops page_lock
1894  *
1895  *	page_free() and page_reclaim()
1896  *		caller grabs page_lock(EXCL)
1897  *
1898  *		lock freelist
1899  *		unlock freelist
1900  *		drop page_lock
1901  *
1902  *	What prevents a thread in page_create() from deadlocking
1903  *	with a thread freeing or reclaiming the same page is the
1904  *	page_trylock() in page_get_freelist(). If the trylock fails
1905  *	it skips the page.
1906  *
1907  *	The lock ordering for promotion and demotion is the same as
1908  *	for page_create(). Since the same deadlock could occur during
1909  *	page promotion and freeing or reclaiming of a page on the
1910  *	cache list we might have to fail the operation and undo what
1911  *	have done so far. Again this is rare.
1912  */
1913 page_t *
1914 page_promote(int mnode, pfn_t pfnum, uchar_t new_szc, int flags, int mtype)
1915 {
1916 	page_t		*pp, *pplist, *tpp, *start_pp;
1917 	pgcnt_t		new_npgs, npgs;
1918 	uint_t		bin;
1919 	pgcnt_t		tmpnpgs, pages_left;
1920 	uint_t		noreloc;
1921 	int 		which_list;
1922 	ulong_t		index;
1923 	kmutex_t	*phm;
1924 
1925 	/*
1926 	 * General algorithm:
1927 	 * Find the starting page
1928 	 * Walk each page struct removing it from the freelist,
1929 	 * and linking it to all the other pages removed.
1930 	 * Once all pages are off the freelist,
1931 	 * walk the list, modifying p_szc to new_szc and what
1932 	 * ever other info needs to be done to create a large free page.
1933 	 * According to the flags, either return the page or put it
1934 	 * on the freelist.
1935 	 */
1936 
1937 	start_pp = page_numtopp_nolock(pfnum);
1938 	ASSERT(start_pp && (start_pp->p_pagenum == pfnum));
1939 	new_npgs = page_get_pagecnt(new_szc);
1940 	ASSERT(IS_P2ALIGNED(pfnum, new_npgs));
1941 
1942 	/* don't return page of the wrong mtype */
1943 	if (mtype != PC_MTYPE_ANY && mtype != PP_2_MTYPE(start_pp))
1944 			return (NULL);
1945 
1946 	/*
1947 	 * Loop through smaller pages to confirm that all pages
1948 	 * give the same result for PP_ISNORELOC().
1949 	 * We can check this reliably here as the protocol for setting
1950 	 * P_NORELOC requires pages to be taken off the free list first.
1951 	 */
1952 	noreloc = PP_ISNORELOC(start_pp);
1953 	for (pp = start_pp + new_npgs; --pp > start_pp; ) {
1954 		if (noreloc != PP_ISNORELOC(pp)) {
1955 			page_promote_noreloc_err++;
1956 			page_promote_err++;
1957 			return (NULL);
1958 		}
1959 	}
1960 
1961 	pages_left = new_npgs;
1962 	pplist = NULL;
1963 	pp = start_pp;
1964 
1965 	/* Loop around coalescing the smaller pages into a big page. */
1966 	while (pages_left) {
1967 		/*
1968 		 * Remove from the freelist.
1969 		 */
1970 		ASSERT(PP_ISFREE(pp));
1971 		bin = PP_2_BIN(pp);
1972 		ASSERT(mnode == PP_2_MEM_NODE(pp));
1973 		mtype = PP_2_MTYPE(pp);
1974 		if (PP_ISAGED(pp)) {
1975 
1976 			/*
1977 			 * PG_FREE_LIST
1978 			 */
1979 			if (pp->p_szc) {
1980 				page_vpsub(&PAGE_FREELISTS(mnode,
1981 				    pp->p_szc, bin, mtype), pp);
1982 			} else {
1983 				mach_page_sub(&PAGE_FREELISTS(mnode, 0,
1984 				    bin, mtype), pp);
1985 			}
1986 			which_list = PG_FREE_LIST;
1987 		} else {
1988 			ASSERT(pp->p_szc == 0);
1989 
1990 			/*
1991 			 * PG_CACHE_LIST
1992 			 *
1993 			 * Since this page comes from the
1994 			 * cachelist, we must destroy the
1995 			 * vnode association.
1996 			 */
1997 			if (!page_trylock(pp, SE_EXCL)) {
1998 				goto fail_promote;
1999 			}
2000 
2001 			/*
2002 			 * We need to be careful not to deadlock
2003 			 * with another thread in page_lookup().
2004 			 * The page_lookup() thread could be holding
2005 			 * the same phm that we need if the two
2006 			 * pages happen to hash to the same phm lock.
2007 			 * At this point we have locked the entire
2008 			 * freelist and page_lookup() could be trying
2009 			 * to grab a freelist lock.
2010 			 */
2011 			index = PAGE_HASH_FUNC(pp->p_vnode, pp->p_offset);
2012 			phm = PAGE_HASH_MUTEX(index);
2013 			if (!mutex_tryenter(phm)) {
2014 				page_unlock_nocapture(pp);
2015 				goto fail_promote;
2016 			}
2017 
2018 			mach_page_sub(&PAGE_CACHELISTS(mnode, bin, mtype), pp);
2019 			page_hashout(pp, phm);
2020 			mutex_exit(phm);
2021 			PP_SETAGED(pp);
2022 			page_unlock_nocapture(pp);
2023 			which_list = PG_CACHE_LIST;
2024 		}
2025 		page_ctr_sub(mnode, mtype, pp, which_list);
2026 
2027 		/*
2028 		 * Concatenate the smaller page(s) onto
2029 		 * the large page list.
2030 		 */
2031 		tmpnpgs = npgs = page_get_pagecnt(pp->p_szc);
2032 		pages_left -= npgs;
2033 		tpp = pp;
2034 		while (npgs--) {
2035 			tpp->p_szc = new_szc;
2036 			tpp = tpp->p_next;
2037 		}
2038 		page_list_concat(&pplist, &pp);
2039 		pp += tmpnpgs;
2040 	}
2041 	CHK_LPG(pplist, new_szc);
2042 
2043 	/*
2044 	 * return the page to the user if requested
2045 	 * in the properly locked state.
2046 	 */
2047 	if (flags == PC_ALLOC && (page_trylock_cons(pplist, SE_EXCL))) {
2048 		return (pplist);
2049 	}
2050 
2051 	/*
2052 	 * Otherwise place the new large page on the freelist
2053 	 */
2054 	bin = PP_2_BIN(pplist);
2055 	mnode = PP_2_MEM_NODE(pplist);
2056 	mtype = PP_2_MTYPE(pplist);
2057 	page_vpadd(&PAGE_FREELISTS(mnode, new_szc, bin, mtype), pplist);
2058 
2059 	page_ctr_add(mnode, mtype, pplist, PG_FREE_LIST);
2060 	return (NULL);
2061 
2062 fail_promote:
2063 	/*
2064 	 * A thread must have still been freeing or
2065 	 * reclaiming the page on the cachelist.
2066 	 * To prevent a deadlock undo what we have
2067 	 * done sofar and return failure. This
2068 	 * situation can only happen while promoting
2069 	 * PAGESIZE pages.
2070 	 */
2071 	page_promote_err++;
2072 	while (pplist) {
2073 		pp = pplist;
2074 		mach_page_sub(&pplist, pp);
2075 		pp->p_szc = 0;
2076 		bin = PP_2_BIN(pp);
2077 		mtype = PP_2_MTYPE(pp);
2078 		mach_page_add(&PAGE_FREELISTS(mnode, 0, bin, mtype), pp);
2079 		page_ctr_add(mnode, mtype, pp, PG_FREE_LIST);
2080 	}
2081 	return (NULL);
2082 
2083 }
2084 
2085 /*
2086  * Break up a large page into smaller size pages.
2087  * Pages involved are on the freelist before the call and may
2088  * be returned to the caller if requested, otherwise they will
2089  * be placed back on the freelist.
2090  * The caller is responsible for locking the freelist as well as any other
2091  * accounting which needs to be done for a returned page.
2092  * If flags is not PC_ALLOC, the color argument is ignored, and thus
2093  * technically, any value may be passed in but PC_NO_COLOR is the standard
2094  * which should be followed for clarity's sake.
2095  * Returns a page whose pfn is < pfnmax
2096  */
2097 page_t *
2098 page_demote(int mnode, pfn_t pfnum, pfn_t pfnmax, uchar_t cur_szc,
2099     uchar_t new_szc, int color, int flags)
2100 {
2101 	page_t	*pp, *pplist, *npplist;
2102 	pgcnt_t	npgs, n;
2103 	uint_t	bin;
2104 	uint_t	mtype;
2105 	page_t	*ret_pp = NULL;
2106 
2107 	ASSERT(cur_szc != 0);
2108 	ASSERT(new_szc < cur_szc);
2109 
2110 	pplist = page_numtopp_nolock(pfnum);
2111 	ASSERT(pplist != NULL);
2112 
2113 	ASSERT(pplist->p_szc == cur_szc);
2114 
2115 	bin = PP_2_BIN(pplist);
2116 	ASSERT(mnode == PP_2_MEM_NODE(pplist));
2117 	mtype = PP_2_MTYPE(pplist);
2118 	page_vpsub(&PAGE_FREELISTS(mnode, cur_szc, bin, mtype), pplist);
2119 
2120 	CHK_LPG(pplist, cur_szc);
2121 	page_ctr_sub(mnode, mtype, pplist, PG_FREE_LIST);
2122 
2123 	/*
2124 	 * Number of PAGESIZE pages for smaller new_szc
2125 	 * page.
2126 	 */
2127 	npgs = page_get_pagecnt(new_szc);
2128 
2129 	while (pplist) {
2130 		pp = pplist;
2131 
2132 		ASSERT(pp->p_szc == cur_szc);
2133 
2134 		/*
2135 		 * We either break it up into PAGESIZE pages or larger.
2136 		 */
2137 		if (npgs == 1) {	/* PAGESIZE case */
2138 			mach_page_sub(&pplist, pp);
2139 			ASSERT(pp->p_szc == cur_szc);
2140 			ASSERT(new_szc == 0);
2141 			ASSERT(mnode == PP_2_MEM_NODE(pp));
2142 			pp->p_szc = new_szc;
2143 			bin = PP_2_BIN(pp);
2144 			if ((bin == color) && (flags == PC_ALLOC) &&
2145 			    (ret_pp == NULL) && (pfnmax == 0 ||
2146 			    pp->p_pagenum < pfnmax) &&
2147 			    page_trylock_cons(pp, SE_EXCL)) {
2148 				ret_pp = pp;
2149 			} else {
2150 				mtype = PP_2_MTYPE(pp);
2151 				mach_page_add(&PAGE_FREELISTS(mnode, 0, bin,
2152 				    mtype), pp);
2153 				page_ctr_add(mnode, mtype, pp, PG_FREE_LIST);
2154 			}
2155 		} else {
2156 			page_t *try_to_return_this_page = NULL;
2157 			int count = 0;
2158 
2159 			/*
2160 			 * Break down into smaller lists of pages.
2161 			 */
2162 			page_list_break(&pplist, &npplist, npgs);
2163 
2164 			pp = pplist;
2165 			n = npgs;
2166 			while (n--) {
2167 				ASSERT(pp->p_szc == cur_szc);
2168 				/*
2169 				 * Check whether all the pages in this list
2170 				 * fit the request criteria.
2171 				 */
2172 				if (pfnmax == 0 || pp->p_pagenum < pfnmax) {
2173 					count++;
2174 				}
2175 				pp->p_szc = new_szc;
2176 				pp = pp->p_next;
2177 			}
2178 
2179 			if (count == npgs &&
2180 			    (pfnmax == 0 || pp->p_pagenum < pfnmax)) {
2181 				try_to_return_this_page = pp;
2182 			}
2183 
2184 			CHK_LPG(pplist, new_szc);
2185 
2186 			bin = PP_2_BIN(pplist);
2187 			if (try_to_return_this_page)
2188 				ASSERT(mnode ==
2189 				    PP_2_MEM_NODE(try_to_return_this_page));
2190 			if ((bin == color) && (flags == PC_ALLOC) &&
2191 			    (ret_pp == NULL) && try_to_return_this_page &&
2192 			    page_trylock_cons(try_to_return_this_page,
2193 			    SE_EXCL)) {
2194 				ret_pp = try_to_return_this_page;
2195 			} else {
2196 				mtype = PP_2_MTYPE(pp);
2197 				page_vpadd(&PAGE_FREELISTS(mnode, new_szc,
2198 				    bin, mtype), pplist);
2199 
2200 				page_ctr_add(mnode, mtype, pplist,
2201 				    PG_FREE_LIST);
2202 			}
2203 			pplist = npplist;
2204 		}
2205 	}
2206 	return (ret_pp);
2207 }
2208 
2209 int mpss_coalesce_disable = 0;
2210 
2211 /*
2212  * Coalesce free pages into a page of the given szc and color if possible.
2213  * Return the pointer to the page created, otherwise, return NULL.
2214  *
2215  * If pfnhi is non-zero, search for large page with pfn range less than pfnhi.
2216  */
2217 page_t *
2218 page_freelist_coalesce(int mnode, uchar_t szc, uint_t color, uint_t ceq_mask,
2219     int mtype, pfn_t pfnhi)
2220 {
2221 	int 	r = szc;		/* region size */
2222 	int	mrange;
2223 	uint_t 	full, bin, color_mask, wrap = 0;
2224 	pfn_t	pfnum, lo, hi;
2225 	size_t	len, idx, idx0;
2226 	pgcnt_t	cands = 0, szcpgcnt = page_get_pagecnt(szc);
2227 	page_t	*ret_pp;
2228 	MEM_NODE_ITERATOR_DECL(it);
2229 #if defined(__sparc)
2230 	pfn_t pfnum0, nlo, nhi;
2231 #endif
2232 
2233 	if (mpss_coalesce_disable) {
2234 		ASSERT(szc < MMU_PAGE_SIZES);
2235 		VM_STAT_ADD(vmm_vmstats.page_ctrs_coalesce[szc][0]);
2236 		return (NULL);
2237 	}
2238 
2239 	ASSERT(szc < mmu_page_sizes);
2240 	color_mask = PAGE_GET_PAGECOLORS(szc) - 1;
2241 	ASSERT(ceq_mask <= color_mask);
2242 	ASSERT(color <= color_mask);
2243 	color &= ceq_mask;
2244 
2245 	/* Prevent page_counters dynamic memory from being freed */
2246 	rw_enter(&page_ctrs_rwlock[mnode], RW_READER);
2247 
2248 	mrange = MTYPE_2_MRANGE(mnode, mtype);
2249 	ASSERT(mrange < mnode_nranges[mnode]);
2250 	VM_STAT_ADD(vmm_vmstats.page_ctrs_coalesce[r][mrange]);
2251 
2252 	/* get pfn range for mtype */
2253 	len = PAGE_COUNTERS_ENTRIES(mnode, r);
2254 #if defined(__sparc)
2255 	lo = PAGE_COUNTERS_BASE(mnode, r);
2256 	hi = IDX_TO_PNUM(mnode, r, len);
2257 #else
2258 	MNODETYPE_2_PFN(mnode, mtype, lo, hi);
2259 	hi++;
2260 #endif
2261 
2262 	/* use lower limit if given */
2263 	if (pfnhi != PFNNULL && pfnhi < hi)
2264 		hi = pfnhi;
2265 
2266 	/* round to szcpgcnt boundaries */
2267 	lo = P2ROUNDUP(lo, szcpgcnt);
2268 	MEM_NODE_ITERATOR_INIT(lo, mnode, szc, &it);
2269 	if (lo == (pfn_t)-1) {
2270 		rw_exit(&page_ctrs_rwlock[mnode]);
2271 		return (NULL);
2272 	}
2273 	hi = hi & ~(szcpgcnt - 1);
2274 
2275 	/* set lo to the closest pfn of the right color */
2276 	if (((PFN_2_COLOR(lo, szc, &it) ^ color) & ceq_mask) ||
2277 	    (interleaved_mnodes && PFN_2_MEM_NODE(lo) != mnode)) {
2278 		PAGE_NEXT_PFN_FOR_COLOR(lo, szc, color, ceq_mask, color_mask,
2279 		    &it);
2280 	}
2281 
2282 	if (hi <= lo) {
2283 		rw_exit(&page_ctrs_rwlock[mnode]);
2284 		return (NULL);
2285 	}
2286 
2287 	full = FULL_REGION_CNT(r);
2288 
2289 	/* calculate the number of page candidates and initial search index */
2290 	bin = color;
2291 	idx0 = (size_t)(-1);
2292 	do {
2293 		pgcnt_t acand;
2294 
2295 		PGCTRS_CANDS_GETVALUECOLOR(mnode, mrange, r, bin, acand);
2296 		if (acand) {
2297 			idx = PAGE_COUNTERS_CURRENT_COLOR(mnode,
2298 			    r, bin, mrange);
2299 			idx0 = MIN(idx0, idx);
2300 			cands += acand;
2301 		}
2302 		bin = ADD_MASKED(bin, 1, ceq_mask, color_mask);
2303 	} while (bin != color);
2304 
2305 	if (cands == 0) {
2306 		VM_STAT_ADD(vmm_vmstats.page_ctrs_cands_skip[r][mrange]);
2307 		rw_exit(&page_ctrs_rwlock[mnode]);
2308 		return (NULL);
2309 	}
2310 
2311 	pfnum = IDX_TO_PNUM(mnode, r, idx0);
2312 	if (pfnum < lo || pfnum >= hi) {
2313 		pfnum = lo;
2314 	} else {
2315 		MEM_NODE_ITERATOR_INIT(pfnum, mnode, szc, &it);
2316 		if (pfnum == (pfn_t)-1) {
2317 			pfnum = lo;
2318 			MEM_NODE_ITERATOR_INIT(pfnum, mnode, szc, &it);
2319 			ASSERT(pfnum != (pfn_t)-1);
2320 		} else if ((PFN_2_COLOR(pfnum, szc, &it) ^ color) & ceq_mask ||
2321 		    (interleaved_mnodes && PFN_2_MEM_NODE(pfnum) != mnode)) {
2322 			/* invalid color, get the closest correct pfn */
2323 			PAGE_NEXT_PFN_FOR_COLOR(pfnum, szc, color, ceq_mask,
2324 			    color_mask, &it);
2325 			if (pfnum >= hi) {
2326 				pfnum = lo;
2327 				MEM_NODE_ITERATOR_INIT(pfnum, mnode, szc, &it);
2328 			}
2329 		}
2330 	}
2331 
2332 	/* set starting index */
2333 	idx0 = PNUM_TO_IDX(mnode, r, pfnum);
2334 	ASSERT(idx0 < len);
2335 
2336 #if defined(__sparc)
2337 	pfnum0 = pfnum;		/* page corresponding to idx0 */
2338 	nhi = 0;		/* search kcage ranges */
2339 #endif
2340 
2341 	for (idx = idx0; wrap == 0 || (idx < idx0 && wrap < 2); ) {
2342 
2343 #if defined(__sparc)
2344 		/*
2345 		 * Find lowest intersection of kcage ranges and mnode.
2346 		 * MTYPE_NORELOC means look in the cage, otherwise outside.
2347 		 */
2348 		if (nhi <= pfnum) {
2349 			if (kcage_next_range(mtype == MTYPE_NORELOC, pfnum,
2350 			    (wrap == 0 ? hi : pfnum0), &nlo, &nhi))
2351 				goto wrapit;
2352 
2353 			/* jump to the next page in the range */
2354 			if (pfnum < nlo) {
2355 				pfnum = P2ROUNDUP(nlo, szcpgcnt);
2356 				MEM_NODE_ITERATOR_INIT(pfnum, mnode, szc, &it);
2357 				idx = PNUM_TO_IDX(mnode, r, pfnum);
2358 				if (idx >= len || pfnum >= hi)
2359 					goto wrapit;
2360 				if ((PFN_2_COLOR(pfnum, szc, &it) ^ color) &
2361 				    ceq_mask)
2362 					goto next;
2363 				if (interleaved_mnodes &&
2364 				    PFN_2_MEM_NODE(pfnum) != mnode)
2365 					goto next;
2366 			}
2367 		}
2368 #endif
2369 
2370 		if (PAGE_COUNTERS(mnode, r, idx) != full)
2371 			goto next;
2372 
2373 		/*
2374 		 * RFE: For performance maybe we can do something less
2375 		 *	brutal than locking the entire freelist. So far
2376 		 * 	this doesn't seem to be a performance problem?
2377 		 */
2378 		page_freelist_lock(mnode);
2379 		if (PAGE_COUNTERS(mnode, r, idx) == full) {
2380 			ret_pp =
2381 			    page_promote(mnode, pfnum, r, PC_ALLOC, mtype);
2382 			if (ret_pp != NULL) {
2383 				VM_STAT_ADD(vmm_vmstats.pfc_coalok[r][mrange]);
2384 				PAGE_COUNTERS_CURRENT_COLOR(mnode, r,
2385 				    PFN_2_COLOR(pfnum, szc, &it), mrange) = idx;
2386 				page_freelist_unlock(mnode);
2387 				rw_exit(&page_ctrs_rwlock[mnode]);
2388 #if defined(__sparc)
2389 				if (PP_ISNORELOC(ret_pp)) {
2390 					pgcnt_t npgs;
2391 
2392 					npgs = page_get_pagecnt(ret_pp->p_szc);
2393 					kcage_freemem_sub(npgs);
2394 				}
2395 #endif
2396 				return (ret_pp);
2397 			}
2398 		} else {
2399 			VM_STAT_ADD(vmm_vmstats.page_ctrs_changed[r][mrange]);
2400 		}
2401 
2402 		page_freelist_unlock(mnode);
2403 		/*
2404 		 * No point looking for another page if we've
2405 		 * already tried all of the ones that
2406 		 * page_ctr_cands indicated.  Stash off where we left
2407 		 * off.
2408 		 * Note: this is not exact since we don't hold the
2409 		 * page_freelist_locks before we initially get the
2410 		 * value of cands for performance reasons, but should
2411 		 * be a decent approximation.
2412 		 */
2413 		if (--cands == 0) {
2414 			PAGE_COUNTERS_CURRENT_COLOR(mnode, r, color, mrange) =
2415 			    idx;
2416 			break;
2417 		}
2418 next:
2419 		PAGE_NEXT_PFN_FOR_COLOR(pfnum, szc, color, ceq_mask,
2420 		    color_mask, &it);
2421 		idx = PNUM_TO_IDX(mnode, r, pfnum);
2422 		if (idx >= len || pfnum >= hi) {
2423 wrapit:
2424 			pfnum = lo;
2425 			MEM_NODE_ITERATOR_INIT(pfnum, mnode, szc, &it);
2426 			idx = PNUM_TO_IDX(mnode, r, pfnum);
2427 			wrap++;
2428 #if defined(__sparc)
2429 			nhi = 0;	/* search kcage ranges */
2430 #endif
2431 		}
2432 	}
2433 
2434 	rw_exit(&page_ctrs_rwlock[mnode]);
2435 	VM_STAT_ADD(vmm_vmstats.page_ctrs_failed[r][mrange]);
2436 	return (NULL);
2437 }
2438 
2439 /*
2440  * For the given mnode, promote as many small pages to large pages as possible.
2441  * mnode can be -1, which means do them all
2442  */
2443 void
2444 page_freelist_coalesce_all(int mnode)
2445 {
2446 	int 	r;		/* region size */
2447 	int 	idx, full;
2448 	size_t	len;
2449 	int doall = interleaved_mnodes || mnode < 0;
2450 	int mlo = doall ? 0 : mnode;
2451 	int mhi = doall ? max_mem_nodes : (mnode + 1);
2452 
2453 	VM_STAT_ADD(vmm_vmstats.page_ctrs_coalesce_all);
2454 
2455 	if (mpss_coalesce_disable) {
2456 		return;
2457 	}
2458 
2459 	/*
2460 	 * Lock the entire freelist and coalesce what we can.
2461 	 *
2462 	 * Always promote to the largest page possible
2463 	 * first to reduce the number of page promotions.
2464 	 */
2465 	for (mnode = mlo; mnode < mhi; mnode++) {
2466 		rw_enter(&page_ctrs_rwlock[mnode], RW_READER);
2467 		page_freelist_lock(mnode);
2468 	}
2469 	for (r = mmu_page_sizes - 1; r > 0; r--) {
2470 		for (mnode = mlo; mnode < mhi; mnode++) {
2471 			pgcnt_t cands = 0;
2472 			int mrange, nranges = mnode_nranges[mnode];
2473 
2474 			for (mrange = 0; mrange < nranges; mrange++) {
2475 				PGCTRS_CANDS_GETVALUE(mnode, mrange, r, cands);
2476 				if (cands != 0)
2477 					break;
2478 			}
2479 			if (cands == 0) {
2480 				VM_STAT_ADD(vmm_vmstats.
2481 				    page_ctrs_cands_skip_all);
2482 				continue;
2483 			}
2484 
2485 			full = FULL_REGION_CNT(r);
2486 			len  = PAGE_COUNTERS_ENTRIES(mnode, r);
2487 
2488 			for (idx = 0; idx < len; idx++) {
2489 				if (PAGE_COUNTERS(mnode, r, idx) == full) {
2490 					pfn_t pfnum =
2491 					    IDX_TO_PNUM(mnode, r, idx);
2492 					int tmnode = interleaved_mnodes ?
2493 					    PFN_2_MEM_NODE(pfnum) : mnode;
2494 
2495 					ASSERT(pfnum >=
2496 					    mem_node_config[tmnode].physbase &&
2497 					    pfnum <
2498 					    mem_node_config[tmnode].physmax);
2499 
2500 					(void) page_promote(tmnode,
2501 					    pfnum, r, PC_FREE, PC_MTYPE_ANY);
2502 				}
2503 			}
2504 			/* shared hpm_counters covers all mnodes, so we quit */
2505 			if (interleaved_mnodes)
2506 				break;
2507 		}
2508 	}
2509 	for (mnode = mlo; mnode < mhi; mnode++) {
2510 		page_freelist_unlock(mnode);
2511 		rw_exit(&page_ctrs_rwlock[mnode]);
2512 	}
2513 }
2514 
2515 /*
2516  * This is where all polices for moving pages around
2517  * to different page size free lists is implemented.
2518  * Returns 1 on success, 0 on failure.
2519  *
2520  * So far these are the priorities for this algorithm in descending
2521  * order:
2522  *
2523  *	1) When servicing a request try to do so with a free page
2524  *	   from next size up. Helps defer fragmentation as long
2525  *	   as possible.
2526  *
2527  *	2) Page coalesce on demand. Only when a freelist
2528  *	   larger than PAGESIZE is empty and step 1
2529  *	   will not work since all larger size lists are
2530  *	   also empty.
2531  *
2532  * If pfnhi is non-zero, search for large page with pfn range less than pfnhi.
2533  */
2534 
2535 page_t *
2536 page_freelist_split(uchar_t szc, uint_t color, int mnode, int mtype,
2537     pfn_t pfnlo, pfn_t pfnhi, page_list_walker_t *plw)
2538 {
2539 	uchar_t nszc = szc + 1;
2540 	uint_t 	bin, sbin, bin_prev;
2541 	page_t	*pp, *firstpp;
2542 	page_t	*ret_pp = NULL;
2543 	uint_t  color_mask;
2544 
2545 	if (nszc == mmu_page_sizes)
2546 		return (NULL);
2547 
2548 	ASSERT(nszc < mmu_page_sizes);
2549 	color_mask = PAGE_GET_PAGECOLORS(nszc) - 1;
2550 	bin = sbin = PAGE_GET_NSZ_COLOR(szc, color);
2551 	bin_prev = (plw->plw_bin_split_prev == color) ? INVALID_COLOR :
2552 	    PAGE_GET_NSZ_COLOR(szc, plw->plw_bin_split_prev);
2553 
2554 	VM_STAT_ADD(vmm_vmstats.pfs_req[szc]);
2555 	/*
2556 	 * First try to break up a larger page to fill current size freelist.
2557 	 */
2558 	while (plw->plw_bins[nszc] != 0) {
2559 
2560 		ASSERT(nszc < mmu_page_sizes);
2561 
2562 		/*
2563 		 * If page found then demote it.
2564 		 */
2565 		if (PAGE_FREELISTS(mnode, nszc, bin, mtype)) {
2566 			page_freelist_lock(mnode);
2567 			firstpp = pp = PAGE_FREELISTS(mnode, nszc, bin, mtype);
2568 
2569 			/*
2570 			 * If pfnhi is not PFNNULL, look for large page below
2571 			 * pfnhi. PFNNULL signifies no pfn requirement.
2572 			 */
2573 			if ((pfnhi != PFNNULL && pp->p_pagenum >= pfnhi) ||
2574 			    (pfnlo != PFNNULL && pp->p_pagenum < pfnlo)) {
2575 				do {
2576 					pp = pp->p_vpnext;
2577 					if (pp == firstpp) {
2578 						pp = NULL;
2579 						break;
2580 					}
2581 				} while ((pfnhi != PFNNULL &&
2582 				    pp->p_pagenum >= pfnhi) ||
2583 				    (pfnlo != PFNNULL &&
2584 				    pp->p_pagenum < pfnlo));
2585 
2586 				if (pfnhi != PFNNULL && pp != NULL)
2587 					ASSERT(pp->p_pagenum < pfnhi);
2588 
2589 				if (pfnlo != PFNNULL && pp != NULL)
2590 					ASSERT(pp->p_pagenum >= pfnlo);
2591 			}
2592 			if (pp) {
2593 				uint_t ccolor = page_correct_color(szc, nszc,
2594 				    color, bin, plw->plw_ceq_mask[szc]);
2595 
2596 				ASSERT(pp->p_szc == nszc);
2597 				VM_STAT_ADD(vmm_vmstats.pfs_demote[nszc]);
2598 				ret_pp = page_demote(mnode, pp->p_pagenum,
2599 				    pfnhi, pp->p_szc, szc, ccolor, PC_ALLOC);
2600 				if (ret_pp) {
2601 					page_freelist_unlock(mnode);
2602 #if defined(__sparc)
2603 					if (PP_ISNORELOC(ret_pp)) {
2604 						pgcnt_t npgs;
2605 
2606 						npgs = page_get_pagecnt(
2607 						    ret_pp->p_szc);
2608 						kcage_freemem_sub(npgs);
2609 					}
2610 #endif
2611 					return (ret_pp);
2612 				}
2613 			}
2614 			page_freelist_unlock(mnode);
2615 		}
2616 
2617 		/* loop through next size bins */
2618 		bin = ADD_MASKED(bin, 1, plw->plw_ceq_mask[nszc], color_mask);
2619 		plw->plw_bins[nszc]--;
2620 
2621 		if (bin == sbin) {
2622 			uchar_t nnszc = nszc + 1;
2623 
2624 			/* we are done with this page size - check next */
2625 			if (plw->plw_bins[nnszc] == 0)
2626 				/* we have already checked next size bins */
2627 				break;
2628 
2629 			bin = sbin = PAGE_GET_NSZ_COLOR(nszc, bin);
2630 			if (bin_prev != INVALID_COLOR) {
2631 				bin_prev = PAGE_GET_NSZ_COLOR(nszc, bin_prev);
2632 				if (!((bin ^ bin_prev) &
2633 				    plw->plw_ceq_mask[nnszc]))
2634 					break;
2635 			}
2636 			ASSERT(nnszc < mmu_page_sizes);
2637 			color_mask = PAGE_GET_PAGECOLORS(nnszc) - 1;
2638 			nszc = nnszc;
2639 			ASSERT(nszc < mmu_page_sizes);
2640 		}
2641 	}
2642 
2643 	return (ret_pp);
2644 }
2645 
2646 /*
2647  * Helper routine used only by the freelist code to lock
2648  * a page. If the page is a large page then it succeeds in
2649  * locking all the constituent pages or none at all.
2650  * Returns 1 on sucess, 0 on failure.
2651  */
2652 static int
2653 page_trylock_cons(page_t *pp, se_t se)
2654 {
2655 	page_t	*tpp, *first_pp = pp;
2656 
2657 	/*
2658 	 * Fail if can't lock first or only page.
2659 	 */
2660 	if (!page_trylock(pp, se)) {
2661 		return (0);
2662 	}
2663 
2664 	/*
2665 	 * PAGESIZE: common case.
2666 	 */
2667 	if (pp->p_szc == 0) {
2668 		return (1);
2669 	}
2670 
2671 	/*
2672 	 * Large page case.
2673 	 */
2674 	tpp = pp->p_next;
2675 	while (tpp != pp) {
2676 		if (!page_trylock(tpp, se)) {
2677 			/*
2678 			 * On failure unlock what we have locked so far.
2679 			 * We want to avoid attempting to capture these
2680 			 * pages as the pcm mutex may be held which could
2681 			 * lead to a recursive mutex panic.
2682 			 */
2683 			while (first_pp != tpp) {
2684 				page_unlock_nocapture(first_pp);
2685 				first_pp = first_pp->p_next;
2686 			}
2687 			return (0);
2688 		}
2689 		tpp = tpp->p_next;
2690 	}
2691 	return (1);
2692 }
2693 
2694 /*
2695  * init context for walking page lists
2696  * Called when a page of the given szc in unavailable. Sets markers
2697  * for the beginning of the search to detect when search has
2698  * completed a full cycle. Sets flags for splitting larger pages
2699  * and coalescing smaller pages. Page walking procedes until a page
2700  * of the desired equivalent color is found.
2701  */
2702 void
2703 page_list_walk_init(uchar_t szc, uint_t flags, uint_t bin, int can_split,
2704     int use_ceq, page_list_walker_t *plw)
2705 {
2706 	uint_t  nszc, ceq_mask, colors;
2707 	uchar_t ceq = use_ceq ? colorequivszc[szc] : 0;
2708 
2709 	ASSERT(szc < mmu_page_sizes);
2710 	colors = PAGE_GET_PAGECOLORS(szc);
2711 
2712 	plw->plw_colors = colors;
2713 	plw->plw_color_mask = colors - 1;
2714 	plw->plw_bin_marker = plw->plw_bin0 = bin;
2715 	plw->plw_bin_split_prev = bin;
2716 	plw->plw_bin_step = (szc == 0) ? vac_colors : 1;
2717 
2718 	/*
2719 	 * if vac aliasing is possible make sure lower order color
2720 	 * bits are never ignored
2721 	 */
2722 	if (vac_colors > 1)
2723 		ceq &= 0xf0;
2724 
2725 	/*
2726 	 * calculate the number of non-equivalent colors and
2727 	 * color equivalency mask
2728 	 */
2729 	plw->plw_ceq_dif = colors >> ((ceq >> 4) + (ceq & 0xf));
2730 	ASSERT(szc > 0 || plw->plw_ceq_dif >= vac_colors);
2731 	ASSERT(plw->plw_ceq_dif > 0);
2732 	plw->plw_ceq_mask[szc] = (plw->plw_ceq_dif - 1) << (ceq & 0xf);
2733 
2734 	if (flags & PG_MATCH_COLOR) {
2735 		if (cpu_page_colors <  0) {
2736 			/*
2737 			 * this is a heterogeneous machine with different CPUs
2738 			 * having different size e$ (not supported for ni2/rock
2739 			 */
2740 			uint_t cpucolors = CPUSETSIZE() >> PAGE_GET_SHIFT(szc);
2741 			cpucolors = MAX(cpucolors, 1);
2742 			ceq_mask = plw->plw_color_mask & (cpucolors - 1);
2743 			plw->plw_ceq_mask[szc] =
2744 			    MIN(ceq_mask, plw->plw_ceq_mask[szc]);
2745 		}
2746 		plw->plw_ceq_dif = 1;
2747 	}
2748 
2749 	/* we can split pages in the freelist, but not the cachelist */
2750 	if (can_split) {
2751 		plw->plw_do_split = (szc + 1 < mmu_page_sizes) ? 1 : 0;
2752 
2753 		/* set next szc color masks and number of free list bins */
2754 		for (nszc = szc + 1; nszc < mmu_page_sizes; nszc++, szc++) {
2755 			plw->plw_ceq_mask[nszc] = PAGE_GET_NSZ_MASK(szc,
2756 			    plw->plw_ceq_mask[szc]);
2757 			plw->plw_bins[nszc] = PAGE_GET_PAGECOLORS(nszc);
2758 		}
2759 		plw->plw_ceq_mask[nszc] = INVALID_MASK;
2760 		plw->plw_bins[nszc] = 0;
2761 
2762 	} else {
2763 		ASSERT(szc == 0);
2764 		plw->plw_do_split = 0;
2765 		plw->plw_bins[1] = 0;
2766 		plw->plw_ceq_mask[1] = INVALID_MASK;
2767 	}
2768 }
2769 
2770 /*
2771  * set mark to flag where next split should occur
2772  */
2773 #define	PAGE_SET_NEXT_SPLIT_MARKER(szc, nszc, bin, plw) {		     \
2774 	uint_t bin_nsz = PAGE_GET_NSZ_COLOR(szc, bin);			     \
2775 	uint_t bin0_nsz = PAGE_GET_NSZ_COLOR(szc, plw->plw_bin0);	     \
2776 	uint_t neq_mask = ~plw->plw_ceq_mask[nszc] & plw->plw_color_mask;    \
2777 	plw->plw_split_next =						     \
2778 		INC_MASKED(bin_nsz, neq_mask, plw->plw_color_mask);	     \
2779 	if (!((plw->plw_split_next ^ bin0_nsz) & plw->plw_ceq_mask[nszc])) { \
2780 		plw->plw_split_next =					     \
2781 		INC_MASKED(plw->plw_split_next,				     \
2782 		    neq_mask, plw->plw_color_mask);			     \
2783 	}								     \
2784 }
2785 
2786 uint_t
2787 page_list_walk_next_bin(uchar_t szc, uint_t bin, page_list_walker_t *plw)
2788 {
2789 	uint_t  neq_mask = ~plw->plw_ceq_mask[szc] & plw->plw_color_mask;
2790 	uint_t  bin0_nsz, nbin_nsz, nbin0, nbin;
2791 	uchar_t nszc = szc + 1;
2792 
2793 	nbin = ADD_MASKED(bin,
2794 	    plw->plw_bin_step, neq_mask, plw->plw_color_mask);
2795 
2796 	if (plw->plw_do_split) {
2797 		plw->plw_bin_split_prev = bin;
2798 		PAGE_SET_NEXT_SPLIT_MARKER(szc, nszc, bin, plw);
2799 		plw->plw_do_split = 0;
2800 	}
2801 
2802 	if (szc == 0) {
2803 		if (plw->plw_count != 0 || plw->plw_ceq_dif == vac_colors) {
2804 			if (nbin == plw->plw_bin0 &&
2805 			    (vac_colors == 1 || nbin != plw->plw_bin_marker)) {
2806 				nbin = ADD_MASKED(nbin, plw->plw_bin_step,
2807 				    neq_mask, plw->plw_color_mask);
2808 				plw->plw_bin_split_prev = plw->plw_bin0;
2809 			}
2810 
2811 			if (vac_colors > 1 && nbin == plw->plw_bin_marker) {
2812 				plw->plw_bin_marker =
2813 				    nbin = INC_MASKED(nbin, neq_mask,
2814 				    plw->plw_color_mask);
2815 				plw->plw_bin_split_prev = plw->plw_bin0;
2816 				/*
2817 				 * large pages all have the same vac color
2818 				 * so by now we should be done with next
2819 				 * size page splitting process
2820 				 */
2821 				ASSERT(plw->plw_bins[1] == 0);
2822 				plw->plw_do_split = 0;
2823 				return (nbin);
2824 			}
2825 
2826 		} else {
2827 			uint_t bin_jump = (vac_colors == 1) ?
2828 			    (BIN_STEP & ~3) - (plw->plw_bin0 & 3) : BIN_STEP;
2829 
2830 			bin_jump &= ~(vac_colors - 1);
2831 
2832 			nbin0 = ADD_MASKED(plw->plw_bin0, bin_jump, neq_mask,
2833 			    plw->plw_color_mask);
2834 
2835 			if ((nbin0 ^ plw->plw_bin0) & plw->plw_ceq_mask[szc]) {
2836 
2837 				plw->plw_bin_marker = nbin = nbin0;
2838 
2839 				if (plw->plw_bins[nszc] != 0) {
2840 					/*
2841 					 * check if next page size bin is the
2842 					 * same as the next page size bin for
2843 					 * bin0
2844 					 */
2845 					nbin_nsz = PAGE_GET_NSZ_COLOR(szc,
2846 					    nbin);
2847 					bin0_nsz = PAGE_GET_NSZ_COLOR(szc,
2848 					    plw->plw_bin0);
2849 
2850 					if ((bin0_nsz ^ nbin_nsz) &
2851 					    plw->plw_ceq_mask[nszc])
2852 						plw->plw_do_split = 1;
2853 				}
2854 				return (nbin);
2855 			}
2856 		}
2857 	}
2858 
2859 	if (plw->plw_bins[nszc] != 0) {
2860 		nbin_nsz = PAGE_GET_NSZ_COLOR(szc, nbin);
2861 		if (!((plw->plw_split_next ^ nbin_nsz) &
2862 		    plw->plw_ceq_mask[nszc]))
2863 			plw->plw_do_split = 1;
2864 	}
2865 
2866 	return (nbin);
2867 }
2868 
2869 page_t *
2870 page_get_mnode_freelist(int mnode, uint_t bin, int mtype, uchar_t szc,
2871     uint_t flags)
2872 {
2873 	kmutex_t		*pcm;
2874 	page_t			*pp, *first_pp;
2875 	uint_t			sbin;
2876 	int			plw_initialized;
2877 	page_list_walker_t	plw;
2878 
2879 	ASSERT(szc < mmu_page_sizes);
2880 
2881 	VM_STAT_ADD(vmm_vmstats.pgmf_alloc[szc]);
2882 
2883 	MTYPE_START(mnode, mtype, flags);
2884 	if (mtype < 0) {	/* mnode does not have memory in mtype range */
2885 		VM_STAT_ADD(vmm_vmstats.pgmf_allocempty[szc]);
2886 		return (NULL);
2887 	}
2888 try_again:
2889 
2890 	plw_initialized = 0;
2891 	plw.plw_ceq_dif = 1;
2892 
2893 	/*
2894 	 * Only hold one freelist lock at a time, that way we
2895 	 * can start anywhere and not have to worry about lock
2896 	 * ordering.
2897 	 */
2898 	for (plw.plw_count = 0;
2899 	    plw.plw_count < plw.plw_ceq_dif; plw.plw_count++) {
2900 		sbin = bin;
2901 		do {
2902 			if (!PAGE_FREELISTS(mnode, szc, bin, mtype))
2903 				goto bin_empty_1;
2904 
2905 			pcm = PC_BIN_MUTEX(mnode, bin, PG_FREE_LIST);
2906 			mutex_enter(pcm);
2907 			pp = PAGE_FREELISTS(mnode, szc, bin, mtype);
2908 			if (pp == NULL)
2909 				goto bin_empty_0;
2910 
2911 			/*
2912 			 * These were set before the page
2913 			 * was put on the free list,
2914 			 * they must still be set.
2915 			 */
2916 			ASSERT(PP_ISFREE(pp));
2917 			ASSERT(PP_ISAGED(pp));
2918 			ASSERT(pp->p_vnode == NULL);
2919 			ASSERT(pp->p_hash == NULL);
2920 			ASSERT(pp->p_offset == (u_offset_t)-1);
2921 			ASSERT(pp->p_szc == szc);
2922 			ASSERT(PFN_2_MEM_NODE(pp->p_pagenum) == mnode);
2923 
2924 			/*
2925 			 * Walk down the hash chain.
2926 			 * 8k pages are linked on p_next
2927 			 * and p_prev fields. Large pages
2928 			 * are a contiguous group of
2929 			 * constituent pages linked together
2930 			 * on their p_next and p_prev fields.
2931 			 * The large pages are linked together
2932 			 * on the hash chain using p_vpnext
2933 			 * p_vpprev of the base constituent
2934 			 * page of each large page.
2935 			 */
2936 			first_pp = pp;
2937 			while (!page_trylock_cons(pp, SE_EXCL)) {
2938 				if (szc == 0) {
2939 					pp = pp->p_next;
2940 				} else {
2941 					pp = pp->p_vpnext;
2942 				}
2943 
2944 				ASSERT(PP_ISFREE(pp));
2945 				ASSERT(PP_ISAGED(pp));
2946 				ASSERT(pp->p_vnode == NULL);
2947 				ASSERT(pp->p_hash == NULL);
2948 				ASSERT(pp->p_offset == (u_offset_t)-1);
2949 				ASSERT(pp->p_szc == szc);
2950 				ASSERT(PFN_2_MEM_NODE(pp->p_pagenum) == mnode);
2951 
2952 				if (pp == first_pp)
2953 					goto bin_empty_0;
2954 			}
2955 
2956 			ASSERT(pp != NULL);
2957 			ASSERT(mtype == PP_2_MTYPE(pp));
2958 			ASSERT(pp->p_szc == szc);
2959 			if (szc == 0) {
2960 				page_sub(&PAGE_FREELISTS(mnode,
2961 				    szc, bin, mtype), pp);
2962 			} else {
2963 				page_vpsub(&PAGE_FREELISTS(mnode,
2964 				    szc, bin, mtype), pp);
2965 				CHK_LPG(pp, szc);
2966 			}
2967 			page_ctr_sub(mnode, mtype, pp, PG_FREE_LIST);
2968 
2969 			if ((PP_ISFREE(pp) == 0) || (PP_ISAGED(pp) == 0))
2970 				panic("free page is not. pp %p", (void *)pp);
2971 			mutex_exit(pcm);
2972 
2973 #if defined(__sparc)
2974 			ASSERT(!kcage_on || PP_ISNORELOC(pp) ||
2975 			    (flags & PG_NORELOC) == 0);
2976 
2977 			if (PP_ISNORELOC(pp))
2978 				kcage_freemem_sub(page_get_pagecnt(szc));
2979 #endif
2980 			VM_STAT_ADD(vmm_vmstats.pgmf_allocok[szc]);
2981 			return (pp);
2982 
2983 bin_empty_0:
2984 			mutex_exit(pcm);
2985 bin_empty_1:
2986 			if (plw_initialized == 0) {
2987 				page_list_walk_init(szc, flags, bin, 1, 1,
2988 				    &plw);
2989 				plw_initialized = 1;
2990 				ASSERT(plw.plw_colors <=
2991 				    PAGE_GET_PAGECOLORS(szc));
2992 				ASSERT(plw.plw_colors > 0);
2993 				ASSERT((plw.plw_colors &
2994 				    (plw.plw_colors - 1)) == 0);
2995 				ASSERT(bin < plw.plw_colors);
2996 				ASSERT(plw.plw_ceq_mask[szc] < plw.plw_colors);
2997 			}
2998 			/* calculate the next bin with equivalent color */
2999 			bin = ADD_MASKED(bin, plw.plw_bin_step,
3000 			    plw.plw_ceq_mask[szc], plw.plw_color_mask);
3001 		} while (sbin != bin);
3002 
3003 		/*
3004 		 * color bins are all empty if color match. Try and
3005 		 * satisfy the request by breaking up or coalescing
3006 		 * pages from a different size freelist of the correct
3007 		 * color that satisfies the ORIGINAL color requested.
3008 		 * If that fails then try pages of the same size but
3009 		 * different colors assuming we are not called with
3010 		 * PG_MATCH_COLOR.
3011 		 */
3012 		if (plw.plw_do_split &&
3013 		    (pp = page_freelist_split(szc, bin, mnode,
3014 		    mtype, PFNNULL, PFNNULL, &plw)) != NULL)
3015 			return (pp);
3016 
3017 		if (szc > 0 && (pp = page_freelist_coalesce(mnode, szc,
3018 		    bin, plw.plw_ceq_mask[szc], mtype, PFNNULL)) !=  NULL)
3019 			return (pp);
3020 
3021 		if (plw.plw_ceq_dif > 1)
3022 			bin = page_list_walk_next_bin(szc, bin, &plw);
3023 	}
3024 
3025 	/* if allowed, cycle through additional mtypes */
3026 	MTYPE_NEXT(mnode, mtype, flags);
3027 	if (mtype >= 0)
3028 		goto try_again;
3029 
3030 	VM_STAT_ADD(vmm_vmstats.pgmf_allocfailed[szc]);
3031 
3032 	return (NULL);
3033 }
3034 
3035 /*
3036  * Returns the count of free pages for 'pp' with size code 'szc'.
3037  * Note: This function does not return an exact value as the page freelist
3038  * locks are not held and thus the values in the page_counters may be
3039  * changing as we walk through the data.
3040  */
3041 static int
3042 page_freecnt(int mnode, page_t *pp, uchar_t szc)
3043 {
3044 	pgcnt_t	pgfree;
3045 	pgcnt_t cnt;
3046 	ssize_t	r = szc;	/* region size */
3047 	ssize_t	idx;
3048 	int	i;
3049 	int	full, range;
3050 
3051 	/* Make sure pagenum passed in is aligned properly */
3052 	ASSERT((pp->p_pagenum & (PNUM_SIZE(szc) - 1)) == 0);
3053 	ASSERT(szc > 0);
3054 
3055 	/* Prevent page_counters dynamic memory from being freed */
3056 	rw_enter(&page_ctrs_rwlock[mnode], RW_READER);
3057 	idx = PNUM_TO_IDX(mnode, r, pp->p_pagenum);
3058 	cnt = PAGE_COUNTERS(mnode, r, idx);
3059 	pgfree = cnt << PNUM_SHIFT(r - 1);
3060 	range = FULL_REGION_CNT(szc);
3061 
3062 	/* Check for completely full region */
3063 	if (cnt == range) {
3064 		rw_exit(&page_ctrs_rwlock[mnode]);
3065 		return (pgfree);
3066 	}
3067 
3068 	while (--r > 0) {
3069 		idx = PNUM_TO_IDX(mnode, r, pp->p_pagenum);
3070 		full = FULL_REGION_CNT(r);
3071 		for (i = 0; i < range; i++, idx++) {
3072 			cnt = PAGE_COUNTERS(mnode, r, idx);
3073 			/*
3074 			 * If cnt here is full, that means we have already
3075 			 * accounted for these pages earlier.
3076 			 */
3077 			if (cnt != full) {
3078 				pgfree += (cnt << PNUM_SHIFT(r - 1));
3079 			}
3080 		}
3081 		range *= full;
3082 	}
3083 	rw_exit(&page_ctrs_rwlock[mnode]);
3084 	return (pgfree);
3085 }
3086 
3087 /*
3088  * Called from page_geti_contig_pages to exclusively lock constituent pages
3089  * starting from 'spp' for page size code 'szc'.
3090  *
3091  * If 'ptcpthreshold' is set, the number of free pages needed in the 'szc'
3092  * region needs to be greater than or equal to the threshold.
3093  */
3094 static int
3095 page_trylock_contig_pages(int mnode, page_t *spp, uchar_t szc, int flags)
3096 {
3097 	pgcnt_t	pgcnt = PNUM_SIZE(szc);
3098 	pgcnt_t pgfree, i;
3099 	page_t *pp;
3100 
3101 	VM_STAT_ADD(vmm_vmstats.ptcp[szc]);
3102 
3103 
3104 	if ((ptcpthreshold == 0) || (flags & PGI_PGCPHIPRI))
3105 		goto skipptcpcheck;
3106 	/*
3107 	 * check if there are sufficient free pages available before attempting
3108 	 * to trylock. Count is approximate as page counters can change.
3109 	 */
3110 	pgfree = page_freecnt(mnode, spp, szc);
3111 
3112 	/* attempt to trylock if there are sufficient already free pages */
3113 	if (pgfree < pgcnt/ptcpthreshold) {
3114 		VM_STAT_ADD(vmm_vmstats.ptcpfreethresh[szc]);
3115 		return (0);
3116 	}
3117 
3118 skipptcpcheck:
3119 
3120 	for (i = 0; i < pgcnt; i++) {
3121 		pp = &spp[i];
3122 		if (!page_trylock(pp, SE_EXCL)) {
3123 			VM_STAT_ADD(vmm_vmstats.ptcpfailexcl[szc]);
3124 			while (--i != (pgcnt_t)-1) {
3125 				pp = &spp[i];
3126 				ASSERT(PAGE_EXCL(pp));
3127 				page_unlock_nocapture(pp);
3128 			}
3129 			return (0);
3130 		}
3131 		ASSERT(spp[i].p_pagenum == spp->p_pagenum + i);
3132 		if ((pp->p_szc > szc || (szc && pp->p_szc == szc)) &&
3133 		    !PP_ISFREE(pp)) {
3134 			VM_STAT_ADD(vmm_vmstats.ptcpfailszc[szc]);
3135 			ASSERT(i == 0);
3136 			page_unlock_nocapture(pp);
3137 			return (0);
3138 		}
3139 		if (PP_ISNORELOC(pp)) {
3140 			VM_STAT_ADD(vmm_vmstats.ptcpfailcage[szc]);
3141 			while (i != (pgcnt_t)-1) {
3142 				pp = &spp[i];
3143 				ASSERT(PAGE_EXCL(pp));
3144 				page_unlock_nocapture(pp);
3145 				i--;
3146 			}
3147 			return (0);
3148 		}
3149 	}
3150 	VM_STAT_ADD(vmm_vmstats.ptcpok[szc]);
3151 	return (1);
3152 }
3153 
3154 /*
3155  * Claim large page pointed to by 'pp'. 'pp' is the starting set
3156  * of 'szc' constituent pages that had been locked exclusively previously.
3157  * Will attempt to relocate constituent pages in use.
3158  */
3159 static page_t *
3160 page_claim_contig_pages(page_t *pp, uchar_t szc, int flags)
3161 {
3162 	spgcnt_t pgcnt, npgs, i;
3163 	page_t *targpp, *rpp, *hpp;
3164 	page_t *replpp = NULL;
3165 	page_t *pplist = NULL;
3166 
3167 	ASSERT(pp != NULL);
3168 
3169 	pgcnt = page_get_pagecnt(szc);
3170 	while (pgcnt) {
3171 		ASSERT(PAGE_EXCL(pp));
3172 		ASSERT(!PP_ISNORELOC(pp));
3173 		if (PP_ISFREE(pp)) {
3174 			/*
3175 			 * If this is a PG_FREE_LIST page then its
3176 			 * size code can change underneath us due to
3177 			 * page promotion or demotion. As an optimzation
3178 			 * use page_list_sub_pages() instead of
3179 			 * page_list_sub().
3180 			 */
3181 			if (PP_ISAGED(pp)) {
3182 				page_list_sub_pages(pp, szc);
3183 				if (pp->p_szc == szc) {
3184 					return (pp);
3185 				}
3186 				ASSERT(pp->p_szc < szc);
3187 				npgs = page_get_pagecnt(pp->p_szc);
3188 				hpp = pp;
3189 				for (i = 0; i < npgs; i++, pp++) {
3190 					pp->p_szc = szc;
3191 				}
3192 				page_list_concat(&pplist, &hpp);
3193 				pgcnt -= npgs;
3194 				continue;
3195 			}
3196 			ASSERT(!PP_ISAGED(pp));
3197 			ASSERT(pp->p_szc == 0);
3198 			page_list_sub(pp, PG_CACHE_LIST);
3199 			page_hashout(pp, NULL);
3200 			PP_SETAGED(pp);
3201 			pp->p_szc = szc;
3202 			page_list_concat(&pplist, &pp);
3203 			pp++;
3204 			pgcnt--;
3205 			continue;
3206 		}
3207 		npgs = page_get_pagecnt(pp->p_szc);
3208 
3209 		/*
3210 		 * page_create_wait freemem accounting done by caller of
3211 		 * page_get_freelist and not necessary to call it prior to
3212 		 * calling page_get_replacement_page.
3213 		 *
3214 		 * page_get_replacement_page can call page_get_contig_pages
3215 		 * to acquire a large page (szc > 0); the replacement must be
3216 		 * smaller than the contig page size to avoid looping or
3217 		 * szc == 0 and PGI_PGCPSZC0 is set.
3218 		 */
3219 		if (pp->p_szc < szc || (szc == 0 && (flags & PGI_PGCPSZC0))) {
3220 			replpp = page_get_replacement_page(pp, NULL, 0);
3221 			if (replpp) {
3222 				npgs = page_get_pagecnt(pp->p_szc);
3223 				ASSERT(npgs <= pgcnt);
3224 				targpp = pp;
3225 			}
3226 		}
3227 
3228 		/*
3229 		 * If replacement is NULL or do_page_relocate fails, fail
3230 		 * coalescing of pages.
3231 		 */
3232 		if (replpp == NULL || (do_page_relocate(&targpp, &replpp, 0,
3233 		    &npgs, NULL) != 0)) {
3234 			/*
3235 			 * Unlock un-processed target list
3236 			 */
3237 			while (pgcnt--) {
3238 				ASSERT(PAGE_EXCL(pp));
3239 				page_unlock_nocapture(pp);
3240 				pp++;
3241 			}
3242 			/*
3243 			 * Free the processed target list.
3244 			 */
3245 			while (pplist) {
3246 				pp = pplist;
3247 				page_sub(&pplist, pp);
3248 				ASSERT(PAGE_EXCL(pp));
3249 				ASSERT(pp->p_szc == szc);
3250 				ASSERT(PP_ISFREE(pp));
3251 				ASSERT(PP_ISAGED(pp));
3252 				pp->p_szc = 0;
3253 				page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL);
3254 				page_unlock_nocapture(pp);
3255 			}
3256 
3257 			if (replpp != NULL)
3258 				page_free_replacement_page(replpp);
3259 
3260 			return (NULL);
3261 		}
3262 		ASSERT(pp == targpp);
3263 
3264 		/* LINTED */
3265 		ASSERT(hpp = pp); /* That's right, it's an assignment */
3266 
3267 		pp += npgs;
3268 		pgcnt -= npgs;
3269 
3270 		while (npgs--) {
3271 			ASSERT(PAGE_EXCL(targpp));
3272 			ASSERT(!PP_ISFREE(targpp));
3273 			ASSERT(!PP_ISNORELOC(targpp));
3274 			PP_SETFREE(targpp);
3275 			ASSERT(PP_ISAGED(targpp));
3276 			ASSERT(targpp->p_szc < szc || (szc == 0 &&
3277 			    (flags & PGI_PGCPSZC0)));
3278 			targpp->p_szc = szc;
3279 			targpp = targpp->p_next;
3280 
3281 			rpp = replpp;
3282 			ASSERT(rpp != NULL);
3283 			page_sub(&replpp, rpp);
3284 			ASSERT(PAGE_EXCL(rpp));
3285 			ASSERT(!PP_ISFREE(rpp));
3286 			page_unlock_nocapture(rpp);
3287 		}
3288 		ASSERT(targpp == hpp);
3289 		ASSERT(replpp == NULL);
3290 		page_list_concat(&pplist, &targpp);
3291 	}
3292 	CHK_LPG(pplist, szc);
3293 	return (pplist);
3294 }
3295 
3296 /*
3297  * Trim kernel cage from pfnlo-pfnhi and store result in lo-hi. Return code
3298  * of 0 means nothing left after trim.
3299  */
3300 int
3301 trimkcage(struct memseg *mseg, pfn_t *lo, pfn_t *hi, pfn_t pfnlo, pfn_t pfnhi)
3302 {
3303 	pfn_t	kcagepfn;
3304 	int	decr;
3305 	int	rc = 0;
3306 
3307 	if (PP_ISNORELOC(mseg->pages)) {
3308 		if (PP_ISNORELOC(mseg->epages - 1) == 0) {
3309 
3310 			/* lower part of this mseg inside kernel cage */
3311 			decr = kcage_current_pfn(&kcagepfn);
3312 
3313 			/* kernel cage may have transitioned past mseg */
3314 			if (kcagepfn >= mseg->pages_base &&
3315 			    kcagepfn < mseg->pages_end) {
3316 				ASSERT(decr == 0);
3317 				*lo = MAX(kcagepfn, pfnlo);
3318 				*hi = MIN(pfnhi, (mseg->pages_end - 1));
3319 				rc = 1;
3320 			}
3321 		}
3322 		/* else entire mseg in the cage */
3323 	} else {
3324 		if (PP_ISNORELOC(mseg->epages - 1)) {
3325 
3326 			/* upper part of this mseg inside kernel cage */
3327 			decr = kcage_current_pfn(&kcagepfn);
3328 
3329 			/* kernel cage may have transitioned past mseg */
3330 			if (kcagepfn >= mseg->pages_base &&
3331 			    kcagepfn < mseg->pages_end) {
3332 				ASSERT(decr);
3333 				*hi = MIN(kcagepfn, pfnhi);
3334 				*lo = MAX(pfnlo, mseg->pages_base);
3335 				rc = 1;
3336 			}
3337 		} else {
3338 			/* entire mseg outside of kernel cage */
3339 			*lo = MAX(pfnlo, mseg->pages_base);
3340 			*hi = MIN(pfnhi, (mseg->pages_end - 1));
3341 			rc = 1;
3342 		}
3343 	}
3344 	return (rc);
3345 }
3346 
3347 /*
3348  * called from page_get_contig_pages to search 'pfnlo' thru 'pfnhi' to claim a
3349  * page with size code 'szc'. Claiming such a page requires acquiring
3350  * exclusive locks on all constituent pages (page_trylock_contig_pages),
3351  * relocating pages in use and concatenating these constituent pages into a
3352  * large page.
3353  *
3354  * The page lists do not have such a large page and page_freelist_split has
3355  * already failed to demote larger pages and/or coalesce smaller free pages.
3356  *
3357  * 'flags' may specify PG_COLOR_MATCH which would limit the search of large
3358  * pages with the same color as 'bin'.
3359  *
3360  * 'pfnflag' specifies the subset of the pfn range to search.
3361  */
3362 
3363 static page_t *
3364 page_geti_contig_pages(int mnode, uint_t bin, uchar_t szc, int flags,
3365     pfn_t pfnlo, pfn_t pfnhi, pgcnt_t pfnflag)
3366 {
3367 	struct memseg *mseg;
3368 	pgcnt_t	szcpgcnt = page_get_pagecnt(szc);
3369 	pgcnt_t szcpgmask = szcpgcnt - 1;
3370 	pfn_t	randpfn;
3371 	page_t *pp, *randpp, *endpp;
3372 	uint_t colors, ceq_mask;
3373 	/* LINTED : set but not used in function */
3374 	uint_t color_mask;
3375 	pfn_t hi, lo;
3376 	uint_t skip;
3377 	MEM_NODE_ITERATOR_DECL(it);
3378 
3379 	ASSERT(szc != 0 || (flags & PGI_PGCPSZC0));
3380 
3381 	pfnlo = P2ROUNDUP(pfnlo, szcpgcnt);
3382 
3383 	if ((pfnhi - pfnlo) + 1 < szcpgcnt || pfnlo >= pfnhi)
3384 		return (NULL);
3385 
3386 	ASSERT(szc < mmu_page_sizes);
3387 
3388 	colors = PAGE_GET_PAGECOLORS(szc);
3389 	color_mask = colors - 1;
3390 	if ((colors > 1) && (flags & PG_MATCH_COLOR)) {
3391 		uchar_t ceq = colorequivszc[szc];
3392 		uint_t  ceq_dif = colors >> ((ceq >> 4) + (ceq & 0xf));
3393 
3394 		ASSERT(ceq_dif > 0);
3395 		ceq_mask = (ceq_dif - 1) << (ceq & 0xf);
3396 	} else {
3397 		ceq_mask = 0;
3398 	}
3399 
3400 	ASSERT(bin < colors);
3401 
3402 	/* clear "non-significant" color bits */
3403 	bin &= ceq_mask;
3404 
3405 	/*
3406 	 * trim the pfn range to search based on pfnflag. pfnflag is set
3407 	 * when there have been previous page_get_contig_page failures to
3408 	 * limit the search.
3409 	 *
3410 	 * The high bit in pfnflag specifies the number of 'slots' in the
3411 	 * pfn range and the remainder of pfnflag specifies which slot.
3412 	 * For example, a value of 1010b would mean the second slot of
3413 	 * the pfn range that has been divided into 8 slots.
3414 	 */
3415 	if (pfnflag > 1) {
3416 		int	slots = 1 << (highbit(pfnflag) - 1);
3417 		int	slotid = pfnflag & (slots - 1);
3418 		pgcnt_t	szcpages;
3419 		int	slotlen;
3420 
3421 		pfnhi = P2ALIGN((pfnhi + 1), szcpgcnt) - 1;
3422 		szcpages = ((pfnhi - pfnlo) + 1) / szcpgcnt;
3423 		slotlen = howmany(szcpages, slots);
3424 		/* skip if 'slotid' slot is empty */
3425 		if (slotid * slotlen >= szcpages)
3426 			return (NULL);
3427 		pfnlo = pfnlo + (((slotid * slotlen) % szcpages) * szcpgcnt);
3428 		ASSERT(pfnlo < pfnhi);
3429 		if (pfnhi > pfnlo + (slotlen * szcpgcnt))
3430 			pfnhi = pfnlo + (slotlen * szcpgcnt) - 1;
3431 	}
3432 
3433 	memsegs_lock(0);
3434 
3435 	/*
3436 	 * loop through memsegs to look for contig page candidates
3437 	 */
3438 
3439 	for (mseg = memsegs; mseg != NULL; mseg = mseg->next) {
3440 		if (pfnhi < mseg->pages_base || pfnlo >= mseg->pages_end) {
3441 			/* no overlap */
3442 			continue;
3443 		}
3444 
3445 		if (mseg->pages_end - mseg->pages_base < szcpgcnt)
3446 			/* mseg too small */
3447 			continue;
3448 
3449 		/*
3450 		 * trim off kernel cage pages from pfn range and check for
3451 		 * a trimmed pfn range returned that does not span the
3452 		 * desired large page size.
3453 		 */
3454 		if (kcage_on) {
3455 			if (trimkcage(mseg, &lo, &hi, pfnlo, pfnhi) == 0 ||
3456 			    lo >= hi || ((hi - lo) + 1) < szcpgcnt)
3457 				continue;
3458 		} else {
3459 			lo = MAX(pfnlo, mseg->pages_base);
3460 			hi = MIN(pfnhi, (mseg->pages_end - 1));
3461 		}
3462 
3463 		/* round to szcpgcnt boundaries */
3464 		lo = P2ROUNDUP(lo, szcpgcnt);
3465 
3466 		MEM_NODE_ITERATOR_INIT(lo, mnode, szc, &it);
3467 		hi = P2ALIGN((hi + 1), szcpgcnt) - 1;
3468 
3469 		if (hi <= lo)
3470 			continue;
3471 
3472 		/*
3473 		 * set lo to point to the pfn for the desired bin. Large
3474 		 * page sizes may only have a single page color
3475 		 */
3476 		skip = szcpgcnt;
3477 		if (ceq_mask > 0 || interleaved_mnodes) {
3478 			/* set lo to point at appropriate color */
3479 			if (((PFN_2_COLOR(lo, szc, &it) ^ bin) & ceq_mask) ||
3480 			    (interleaved_mnodes &&
3481 			    PFN_2_MEM_NODE(lo) != mnode)) {
3482 				PAGE_NEXT_PFN_FOR_COLOR(lo, szc, bin, ceq_mask,
3483 				    color_mask, &it);
3484 			}
3485 			if (hi <= lo)
3486 				/* mseg cannot satisfy color request */
3487 				continue;
3488 		}
3489 
3490 		/* randomly choose a point between lo and hi to begin search */
3491 
3492 		randpfn = (pfn_t)GETTICK();
3493 		randpfn = ((randpfn % (hi - lo)) + lo) & ~(skip - 1);
3494 		MEM_NODE_ITERATOR_INIT(randpfn, mnode, szc, &it);
3495 		if (ceq_mask || interleaved_mnodes || randpfn == (pfn_t)-1) {
3496 			if (randpfn != (pfn_t)-1) {
3497 				PAGE_NEXT_PFN_FOR_COLOR(randpfn, szc, bin,
3498 				    ceq_mask, color_mask, &it);
3499 			}
3500 			if (randpfn >= hi) {
3501 				randpfn = lo;
3502 				MEM_NODE_ITERATOR_INIT(randpfn, mnode, szc,
3503 				    &it);
3504 			}
3505 		}
3506 		randpp = mseg->pages + (randpfn - mseg->pages_base);
3507 
3508 		ASSERT(randpp->p_pagenum == randpfn);
3509 
3510 		pp = randpp;
3511 		endpp =  mseg->pages + (hi - mseg->pages_base) + 1;
3512 
3513 		ASSERT(randpp + szcpgcnt <= endpp);
3514 
3515 		do {
3516 			ASSERT(!(pp->p_pagenum & szcpgmask));
3517 			ASSERT(((PP_2_BIN(pp) ^ bin) & ceq_mask) == 0);
3518 
3519 			if (page_trylock_contig_pages(mnode, pp, szc, flags)) {
3520 				/* pages unlocked by page_claim on failure */
3521 				if (page_claim_contig_pages(pp, szc, flags)) {
3522 					memsegs_unlock(0);
3523 					return (pp);
3524 				}
3525 			}
3526 
3527 			if (ceq_mask == 0 && !interleaved_mnodes) {
3528 				pp += skip;
3529 			} else {
3530 				pfn_t pfn = pp->p_pagenum;
3531 
3532 				PAGE_NEXT_PFN_FOR_COLOR(pfn, szc, bin,
3533 				    ceq_mask, color_mask, &it);
3534 				if (pfn == (pfn_t)-1) {
3535 					pp = endpp;
3536 				} else {
3537 					pp = mseg->pages +
3538 					    (pfn - mseg->pages_base);
3539 				}
3540 			}
3541 			if (pp >= endpp) {
3542 				/* start from the beginning */
3543 				MEM_NODE_ITERATOR_INIT(lo, mnode, szc, &it);
3544 				pp = mseg->pages + (lo - mseg->pages_base);
3545 				ASSERT(pp->p_pagenum == lo);
3546 				ASSERT(pp + szcpgcnt <= endpp);
3547 			}
3548 		} while (pp != randpp);
3549 	}
3550 	memsegs_unlock(0);
3551 	return (NULL);
3552 }
3553 
3554 
3555 /*
3556  * controlling routine that searches through physical memory in an attempt to
3557  * claim a large page based on the input parameters.
3558  * on the page free lists.
3559  *
3560  * calls page_geti_contig_pages with an initial pfn range from the mnode
3561  * and mtype. page_geti_contig_pages will trim off the parts of the pfn range
3562  * that overlaps with the kernel cage or does not match the requested page
3563  * color if PG_MATCH_COLOR is set.  Since this search is very expensive,
3564  * page_geti_contig_pages may further limit the search range based on
3565  * previous failure counts (pgcpfailcnt[]).
3566  *
3567  * for PGI_PGCPSZC0 requests, page_get_contig_pages will relocate a base
3568  * pagesize page that satisfies mtype.
3569  */
3570 page_t *
3571 page_get_contig_pages(int mnode, uint_t bin, int mtype, uchar_t szc,
3572     uint_t flags)
3573 {
3574 	pfn_t		pfnlo, pfnhi;	/* contig pages pfn range */
3575 	page_t		*pp;
3576 	pgcnt_t		pfnflag = 0;	/* no limit on search if 0 */
3577 
3578 	VM_STAT_ADD(vmm_vmstats.pgcp_alloc[szc]);
3579 
3580 	/* no allocations from cage */
3581 	flags |= PGI_NOCAGE;
3582 
3583 	/* LINTED */
3584 	MTYPE_START(mnode, mtype, flags);
3585 	if (mtype < 0) {	/* mnode does not have memory in mtype range */
3586 		VM_STAT_ADD(vmm_vmstats.pgcp_allocempty[szc]);
3587 		return (NULL);
3588 	}
3589 
3590 	ASSERT(szc > 0 || (flags & PGI_PGCPSZC0));
3591 
3592 	/* do not limit search and ignore color if hi pri */
3593 
3594 	if (pgcplimitsearch && ((flags & PGI_PGCPHIPRI) == 0))
3595 		pfnflag = pgcpfailcnt[szc];
3596 
3597 	/* remove color match to improve chances */
3598 
3599 	if (flags & PGI_PGCPHIPRI || pfnflag)
3600 		flags &= ~PG_MATCH_COLOR;
3601 
3602 	do {
3603 		/* get pfn range based on mnode and mtype */
3604 		MNODETYPE_2_PFN(mnode, mtype, pfnlo, pfnhi);
3605 
3606 		ASSERT(pfnhi >= pfnlo);
3607 
3608 		pp = page_geti_contig_pages(mnode, bin, szc, flags,
3609 		    pfnlo, pfnhi, pfnflag);
3610 
3611 		if (pp != NULL) {
3612 			pfnflag = pgcpfailcnt[szc];
3613 			if (pfnflag) {
3614 				/* double the search size */
3615 				pgcpfailcnt[szc] = pfnflag >> 1;
3616 			}
3617 			VM_STAT_ADD(vmm_vmstats.pgcp_allocok[szc]);
3618 			return (pp);
3619 		}
3620 		MTYPE_NEXT(mnode, mtype, flags);
3621 	} while (mtype >= 0);
3622 
3623 	VM_STAT_ADD(vmm_vmstats.pgcp_allocfailed[szc]);
3624 	return (NULL);
3625 }
3626 
3627 #if defined(__i386) || defined(__amd64)
3628 /*
3629  * Determine the likelihood of finding/coalescing a szc page.
3630  * Return 0 if the likelihood is small otherwise return 1.
3631  *
3632  * For now, be conservative and check only 1g pages and return 0
3633  * if there had been previous coalescing failures and the szc pages
3634  * needed to satisfy request would exhaust most of freemem.
3635  */
3636 int
3637 page_chk_freelist(uint_t szc)
3638 {
3639 	pgcnt_t		pgcnt;
3640 
3641 	if (szc <= 1)
3642 		return (1);
3643 
3644 	pgcnt = page_get_pagecnt(szc);
3645 	if (pgcpfailcnt[szc] && pgcnt + throttlefree >= freemem) {
3646 		VM_STAT_ADD(vmm_vmstats.pcf_deny[szc]);
3647 		return (0);
3648 	}
3649 	VM_STAT_ADD(vmm_vmstats.pcf_allow[szc]);
3650 	return (1);
3651 }
3652 #endif
3653 
3654 /*
3655  * Find the `best' page on the freelist for this (vp,off) (as,vaddr) pair.
3656  *
3657  * Does its own locking and accounting.
3658  * If PG_MATCH_COLOR is set, then NULL will be returned if there are no
3659  * pages of the proper color even if there are pages of a different color.
3660  *
3661  * Finds a page, removes it, THEN locks it.
3662  */
3663 
3664 /*ARGSUSED*/
3665 page_t *
3666 page_get_freelist(struct vnode *vp, u_offset_t off, struct seg *seg,
3667 	caddr_t vaddr, size_t size, uint_t flags, struct lgrp *lgrp)
3668 {
3669 	struct as	*as = seg->s_as;
3670 	page_t		*pp = NULL;
3671 	ulong_t		bin;
3672 	uchar_t		szc;
3673 	int		mnode;
3674 	int		mtype;
3675 	page_t		*(*page_get_func)(int, uint_t, int, uchar_t, uint_t);
3676 	lgrp_mnode_cookie_t	lgrp_cookie;
3677 
3678 	page_get_func = page_get_mnode_freelist;
3679 
3680 	/*
3681 	 * If we aren't passed a specific lgroup, or passed a freed lgrp
3682 	 * assume we wish to allocate near to the current thread's home.
3683 	 */
3684 	if (!LGRP_EXISTS(lgrp))
3685 		lgrp = lgrp_home_lgrp();
3686 
3687 	if (kcage_on) {
3688 		if ((flags & (PG_NORELOC | PG_PANIC)) == PG_NORELOC &&
3689 		    kcage_freemem < kcage_throttlefree + btop(size) &&
3690 		    curthread != kcage_cageout_thread) {
3691 			/*
3692 			 * Set a "reserve" of kcage_throttlefree pages for
3693 			 * PG_PANIC and cageout thread allocations.
3694 			 *
3695 			 * Everybody else has to serialize in
3696 			 * page_create_get_something() to get a cage page, so
3697 			 * that we don't deadlock cageout!
3698 			 */
3699 			return (NULL);
3700 		}
3701 	} else {
3702 		flags &= ~PG_NORELOC;
3703 		flags |= PGI_NOCAGE;
3704 	}
3705 
3706 	/* LINTED */
3707 	MTYPE_INIT(mtype, vp, vaddr, flags, size);
3708 
3709 	/*
3710 	 * Convert size to page size code.
3711 	 */
3712 	if ((szc = page_szc(size)) == (uchar_t)-1)
3713 		panic("page_get_freelist: illegal page size request");
3714 	ASSERT(szc < mmu_page_sizes);
3715 
3716 	VM_STAT_ADD(vmm_vmstats.pgf_alloc[szc]);
3717 
3718 	/* LINTED */
3719 	AS_2_BIN(as, seg, vp, vaddr, bin, szc);
3720 
3721 	ASSERT(bin < PAGE_GET_PAGECOLORS(szc));
3722 
3723 	/*
3724 	 * Try to get a local page first, but try remote if we can't
3725 	 * get a page of the right color.
3726 	 */
3727 pgretry:
3728 	LGRP_MNODE_COOKIE_INIT(lgrp_cookie, lgrp, LGRP_SRCH_LOCAL);
3729 	while ((mnode = lgrp_memnode_choose(&lgrp_cookie)) >= 0) {
3730 		pp = page_get_func(mnode, bin, mtype, szc, flags);
3731 		if (pp != NULL) {
3732 			VM_STAT_ADD(vmm_vmstats.pgf_allocok[szc]);
3733 			DTRACE_PROBE4(page__get,
3734 			    lgrp_t *, lgrp,
3735 			    int, mnode,
3736 			    ulong_t, bin,
3737 			    uint_t, flags);
3738 			return (pp);
3739 		}
3740 	}
3741 	ASSERT(pp == NULL);
3742 
3743 	/*
3744 	 * for non-SZC0 PAGESIZE requests, check cachelist before checking
3745 	 * remote free lists.  Caller expected to call page_get_cachelist which
3746 	 * will check local cache lists and remote free lists.
3747 	 */
3748 	if (szc == 0 && ((flags & PGI_PGCPSZC0) == 0)) {
3749 		VM_STAT_ADD(vmm_vmstats.pgf_allocdeferred);
3750 		return (NULL);
3751 	}
3752 
3753 	ASSERT(szc > 0 || (flags & PGI_PGCPSZC0));
3754 
3755 	lgrp_stat_add(lgrp->lgrp_id, LGRP_NUM_ALLOC_FAIL, 1);
3756 
3757 	if (!(flags & PG_LOCAL)) {
3758 		/*
3759 		 * Try to get a non-local freelist page.
3760 		 */
3761 		LGRP_MNODE_COOKIE_UPGRADE(lgrp_cookie);
3762 		while ((mnode = lgrp_memnode_choose(&lgrp_cookie)) >= 0) {
3763 			pp = page_get_func(mnode, bin, mtype, szc, flags);
3764 			if (pp != NULL) {
3765 				DTRACE_PROBE4(page__get,
3766 				    lgrp_t *, lgrp,
3767 				    int, mnode,
3768 				    ulong_t, bin,
3769 				    uint_t, flags);
3770 				VM_STAT_ADD(vmm_vmstats.pgf_allocokrem[szc]);
3771 				return (pp);
3772 			}
3773 		}
3774 		ASSERT(pp == NULL);
3775 	}
3776 
3777 	/*
3778 	 * when the cage is off chances are page_get_contig_pages() will fail
3779 	 * to lock a large page chunk therefore when the cage is off it's not
3780 	 * called by default.  this can be changed via /etc/system.
3781 	 *
3782 	 * page_get_contig_pages() also called to acquire a base pagesize page
3783 	 * for page_create_get_something().
3784 	 */
3785 	if (!(flags & PG_NORELOC) && (pg_contig_disable == 0) &&
3786 	    (kcage_on || pg_lpgcreate_nocage || szc == 0) &&
3787 	    (page_get_func != page_get_contig_pages)) {
3788 
3789 		VM_STAT_ADD(vmm_vmstats.pgf_allocretry[szc]);
3790 		page_get_func = page_get_contig_pages;
3791 		goto pgretry;
3792 	}
3793 
3794 	if (!(flags & PG_LOCAL) && pgcplimitsearch &&
3795 	    page_get_func == page_get_contig_pages)
3796 		SETPGCPFAILCNT(szc);
3797 
3798 	VM_STAT_ADD(vmm_vmstats.pgf_allocfailed[szc]);
3799 	return (NULL);
3800 }
3801 
3802 /*
3803  * Find the `best' page on the cachelist for this (vp,off) (as,vaddr) pair.
3804  *
3805  * Does its own locking.
3806  * If PG_MATCH_COLOR is set, then NULL will be returned if there are no
3807  * pages of the proper color even if there are pages of a different color.
3808  * Otherwise, scan the bins for ones with pages.  For each bin with pages,
3809  * try to lock one of them.  If no page can be locked, try the
3810  * next bin.  Return NULL if a page can not be found and locked.
3811  *
3812  * Finds a pages, trys to lock it, then removes it.
3813  */
3814 
3815 /*ARGSUSED*/
3816 page_t *
3817 page_get_cachelist(struct vnode *vp, u_offset_t off, struct seg *seg,
3818     caddr_t vaddr, uint_t flags, struct lgrp *lgrp)
3819 {
3820 	page_t		*pp;
3821 	struct as	*as = seg->s_as;
3822 	ulong_t		bin;
3823 	/*LINTED*/
3824 	int		mnode;
3825 	int		mtype;
3826 	lgrp_mnode_cookie_t	lgrp_cookie;
3827 
3828 	/*
3829 	 * If we aren't passed a specific lgroup, or pasased a freed lgrp
3830 	 * assume we wish to allocate near to the current thread's home.
3831 	 */
3832 	if (!LGRP_EXISTS(lgrp))
3833 		lgrp = lgrp_home_lgrp();
3834 
3835 	if (!kcage_on) {
3836 		flags &= ~PG_NORELOC;
3837 		flags |= PGI_NOCAGE;
3838 	}
3839 
3840 	if ((flags & (PG_NORELOC | PG_PANIC | PG_PUSHPAGE)) == PG_NORELOC &&
3841 	    kcage_freemem <= kcage_throttlefree) {
3842 		/*
3843 		 * Reserve kcage_throttlefree pages for critical kernel
3844 		 * threads.
3845 		 *
3846 		 * Everybody else has to go to page_create_get_something()
3847 		 * to get a cage page, so we don't deadlock cageout.
3848 		 */
3849 		return (NULL);
3850 	}
3851 
3852 	/* LINTED */
3853 	AS_2_BIN(as, seg, vp, vaddr, bin, 0);
3854 
3855 	ASSERT(bin < PAGE_GET_PAGECOLORS(0));
3856 
3857 	/* LINTED */
3858 	MTYPE_INIT(mtype, vp, vaddr, flags, MMU_PAGESIZE);
3859 
3860 	VM_STAT_ADD(vmm_vmstats.pgc_alloc);
3861 
3862 	/*
3863 	 * Try local cachelists first
3864 	 */
3865 	LGRP_MNODE_COOKIE_INIT(lgrp_cookie, lgrp, LGRP_SRCH_LOCAL);
3866 	while ((mnode = lgrp_memnode_choose(&lgrp_cookie)) >= 0) {
3867 		pp = page_get_mnode_cachelist(bin, flags, mnode, mtype);
3868 		if (pp != NULL) {
3869 			VM_STAT_ADD(vmm_vmstats.pgc_allocok);
3870 			DTRACE_PROBE4(page__get,
3871 			    lgrp_t *, lgrp,
3872 			    int, mnode,
3873 			    ulong_t, bin,
3874 			    uint_t, flags);
3875 			return (pp);
3876 		}
3877 	}
3878 
3879 	lgrp_stat_add(lgrp->lgrp_id, LGRP_NUM_ALLOC_FAIL, 1);
3880 
3881 	/*
3882 	 * Try freelists/cachelists that are farther away
3883 	 * This is our only chance to allocate remote pages for PAGESIZE
3884 	 * requests.
3885 	 */
3886 	LGRP_MNODE_COOKIE_UPGRADE(lgrp_cookie);
3887 	while ((mnode = lgrp_memnode_choose(&lgrp_cookie)) >= 0) {
3888 		pp = page_get_mnode_freelist(mnode, bin, mtype,
3889 		    0, flags);
3890 		if (pp != NULL) {
3891 			VM_STAT_ADD(vmm_vmstats.pgc_allocokdeferred);
3892 			DTRACE_PROBE4(page__get,
3893 			    lgrp_t *, lgrp,
3894 			    int, mnode,
3895 			    ulong_t, bin,
3896 			    uint_t, flags);
3897 			return (pp);
3898 		}
3899 		pp = page_get_mnode_cachelist(bin, flags, mnode, mtype);
3900 		if (pp != NULL) {
3901 			VM_STAT_ADD(vmm_vmstats.pgc_allocokrem);
3902 			DTRACE_PROBE4(page__get,
3903 			    lgrp_t *, lgrp,
3904 			    int, mnode,
3905 			    ulong_t, bin,
3906 			    uint_t, flags);
3907 			return (pp);
3908 		}
3909 	}
3910 
3911 	VM_STAT_ADD(vmm_vmstats.pgc_allocfailed);
3912 	return (NULL);
3913 }
3914 
3915 page_t *
3916 page_get_mnode_cachelist(uint_t bin, uint_t flags, int mnode, int mtype)
3917 {
3918 	kmutex_t		*pcm;
3919 	page_t			*pp, *first_pp;
3920 	uint_t			sbin;
3921 	int			plw_initialized;
3922 	page_list_walker_t	plw;
3923 
3924 	VM_STAT_ADD(vmm_vmstats.pgmc_alloc);
3925 
3926 	/* LINTED */
3927 	MTYPE_START(mnode, mtype, flags);
3928 	if (mtype < 0) {	/* mnode does not have memory in mtype range */
3929 		VM_STAT_ADD(vmm_vmstats.pgmc_allocempty);
3930 		return (NULL);
3931 	}
3932 
3933 try_again:
3934 
3935 	plw_initialized = 0;
3936 	plw.plw_ceq_dif = 1;
3937 
3938 	/*
3939 	 * Only hold one cachelist lock at a time, that way we
3940 	 * can start anywhere and not have to worry about lock
3941 	 * ordering.
3942 	 */
3943 
3944 	for (plw.plw_count = 0;
3945 	    plw.plw_count < plw.plw_ceq_dif; plw.plw_count++) {
3946 		sbin = bin;
3947 		do {
3948 
3949 			if (!PAGE_CACHELISTS(mnode, bin, mtype))
3950 				goto bin_empty_1;
3951 			pcm = PC_BIN_MUTEX(mnode, bin, PG_CACHE_LIST);
3952 			mutex_enter(pcm);
3953 			pp = PAGE_CACHELISTS(mnode, bin, mtype);
3954 			if (pp == NULL)
3955 				goto bin_empty_0;
3956 
3957 			first_pp = pp;
3958 			ASSERT(pp->p_vnode);
3959 			ASSERT(PP_ISAGED(pp) == 0);
3960 			ASSERT(pp->p_szc == 0);
3961 			ASSERT(PFN_2_MEM_NODE(pp->p_pagenum) == mnode);
3962 			while (!page_trylock(pp, SE_EXCL)) {
3963 				pp = pp->p_next;
3964 				ASSERT(pp->p_szc == 0);
3965 				if (pp == first_pp) {
3966 					/*
3967 					 * We have searched the complete list!
3968 					 * And all of them (might only be one)
3969 					 * are locked. This can happen since
3970 					 * these pages can also be found via
3971 					 * the hash list. When found via the
3972 					 * hash list, they are locked first,
3973 					 * then removed. We give up to let the
3974 					 * other thread run.
3975 					 */
3976 					pp = NULL;
3977 					break;
3978 				}
3979 				ASSERT(pp->p_vnode);
3980 				ASSERT(PP_ISFREE(pp));
3981 				ASSERT(PP_ISAGED(pp) == 0);
3982 				ASSERT(PFN_2_MEM_NODE(pp->p_pagenum) ==
3983 				    mnode);
3984 			}
3985 
3986 			if (pp) {
3987 				page_t	**ppp;
3988 				/*
3989 				 * Found and locked a page.
3990 				 * Pull it off the list.
3991 				 */
3992 				ASSERT(mtype == PP_2_MTYPE(pp));
3993 				ppp = &PAGE_CACHELISTS(mnode, bin, mtype);
3994 				page_sub(ppp, pp);
3995 				/*
3996 				 * Subtract counters before releasing pcm mutex
3997 				 * to avoid a race with page_freelist_coalesce
3998 				 * and page_freelist_split.
3999 				 */
4000 				page_ctr_sub(mnode, mtype, pp, PG_CACHE_LIST);
4001 				mutex_exit(pcm);
4002 				ASSERT(pp->p_vnode);
4003 				ASSERT(PP_ISAGED(pp) == 0);
4004 #if defined(__sparc)
4005 				ASSERT(!kcage_on ||
4006 				    (flags & PG_NORELOC) == 0 ||
4007 				    PP_ISNORELOC(pp));
4008 				if (PP_ISNORELOC(pp)) {
4009 					kcage_freemem_sub(1);
4010 				}
4011 #endif
4012 				VM_STAT_ADD(vmm_vmstats. pgmc_allocok);
4013 				return (pp);
4014 			}
4015 bin_empty_0:
4016 			mutex_exit(pcm);
4017 bin_empty_1:
4018 			if (plw_initialized == 0) {
4019 				page_list_walk_init(0, flags, bin, 0, 1, &plw);
4020 				plw_initialized = 1;
4021 			}
4022 			/* calculate the next bin with equivalent color */
4023 			bin = ADD_MASKED(bin, plw.plw_bin_step,
4024 			    plw.plw_ceq_mask[0], plw.plw_color_mask);
4025 		} while (sbin != bin);
4026 
4027 		if (plw.plw_ceq_dif > 1)
4028 			bin = page_list_walk_next_bin(0, bin, &plw);
4029 	}
4030 
4031 	MTYPE_NEXT(mnode, mtype, flags);
4032 	if (mtype >= 0)
4033 		goto try_again;
4034 
4035 	VM_STAT_ADD(vmm_vmstats.pgmc_allocfailed);
4036 	return (NULL);
4037 }
4038 
4039 #ifdef DEBUG
4040 #define	REPL_PAGE_STATS
4041 #endif /* DEBUG */
4042 
4043 #ifdef REPL_PAGE_STATS
4044 struct repl_page_stats {
4045 	uint_t	ngets;
4046 	uint_t	ngets_noreloc;
4047 	uint_t	npgr_noreloc;
4048 	uint_t	nnopage_first;
4049 	uint_t	nnopage;
4050 	uint_t	nhashout;
4051 	uint_t	nnofree;
4052 	uint_t	nnext_pp;
4053 } repl_page_stats;
4054 #define	REPL_STAT_INCR(v)	atomic_add_32(&repl_page_stats.v, 1)
4055 #else /* REPL_PAGE_STATS */
4056 #define	REPL_STAT_INCR(v)
4057 #endif /* REPL_PAGE_STATS */
4058 
4059 int	pgrppgcp;
4060 
4061 /*
4062  * The freemem accounting must be done by the caller.
4063  * First we try to get a replacement page of the same size as like_pp,
4064  * if that is not possible, then we just get a set of discontiguous
4065  * PAGESIZE pages.
4066  */
4067 page_t *
4068 page_get_replacement_page(page_t *orig_like_pp, struct lgrp *lgrp_target,
4069     uint_t pgrflags)
4070 {
4071 	page_t		*like_pp;
4072 	page_t		*pp, *pplist;
4073 	page_t		*pl = NULL;
4074 	ulong_t		bin;
4075 	int		mnode, page_mnode;
4076 	int		szc;
4077 	spgcnt_t	npgs, pg_cnt;
4078 	pfn_t		pfnum;
4079 	int		mtype;
4080 	int		flags = 0;
4081 	lgrp_mnode_cookie_t	lgrp_cookie;
4082 	lgrp_t		*lgrp;
4083 
4084 	REPL_STAT_INCR(ngets);
4085 	like_pp = orig_like_pp;
4086 	ASSERT(PAGE_EXCL(like_pp));
4087 
4088 	szc = like_pp->p_szc;
4089 	npgs = page_get_pagecnt(szc);
4090 	/*
4091 	 * Now we reset like_pp to the base page_t.
4092 	 * That way, we won't walk past the end of this 'szc' page.
4093 	 */
4094 	pfnum = PFN_BASE(like_pp->p_pagenum, szc);
4095 	like_pp = page_numtopp_nolock(pfnum);
4096 	ASSERT(like_pp->p_szc == szc);
4097 
4098 	if (PP_ISNORELOC(like_pp)) {
4099 		ASSERT(kcage_on);
4100 		REPL_STAT_INCR(ngets_noreloc);
4101 		flags = PGI_RELOCONLY;
4102 	} else if (pgrflags & PGR_NORELOC) {
4103 		ASSERT(kcage_on);
4104 		REPL_STAT_INCR(npgr_noreloc);
4105 		flags = PG_NORELOC;
4106 	}
4107 
4108 	/*
4109 	 * Kernel pages must always be replaced with the same size
4110 	 * pages, since we cannot properly handle demotion of kernel
4111 	 * pages.
4112 	 */
4113 	if (PP_ISKAS(like_pp))
4114 		pgrflags |= PGR_SAMESZC;
4115 
4116 	/* LINTED */
4117 	MTYPE_PGR_INIT(mtype, flags, like_pp, page_mnode, npgs);
4118 
4119 	while (npgs) {
4120 		pplist = NULL;
4121 		for (;;) {
4122 			pg_cnt = page_get_pagecnt(szc);
4123 			bin = PP_2_BIN(like_pp);
4124 			ASSERT(like_pp->p_szc == orig_like_pp->p_szc);
4125 			ASSERT(pg_cnt <= npgs);
4126 
4127 			/*
4128 			 * If an lgroup was specified, try to get the
4129 			 * page from that lgroup.
4130 			 * NOTE: Must be careful with code below because
4131 			 *	 lgroup may disappear and reappear since there
4132 			 *	 is no locking for lgroup here.
4133 			 */
4134 			if (LGRP_EXISTS(lgrp_target)) {
4135 				/*
4136 				 * Keep local variable for lgroup separate
4137 				 * from lgroup argument since this code should
4138 				 * only be exercised when lgroup argument
4139 				 * exists....
4140 				 */
4141 				lgrp = lgrp_target;
4142 
4143 				/* Try the lgroup's freelists first */
4144 				LGRP_MNODE_COOKIE_INIT(lgrp_cookie, lgrp,
4145 				    LGRP_SRCH_LOCAL);
4146 				while ((pplist == NULL) &&
4147 				    (mnode = lgrp_memnode_choose(&lgrp_cookie))
4148 				    != -1) {
4149 					pplist =
4150 					    page_get_mnode_freelist(mnode, bin,
4151 					    mtype, szc, flags);
4152 				}
4153 
4154 				/*
4155 				 * Now try it's cachelists if this is a
4156 				 * small page. Don't need to do it for
4157 				 * larger ones since page_freelist_coalesce()
4158 				 * already failed.
4159 				 */
4160 				if (pplist != NULL || szc != 0)
4161 					break;
4162 
4163 				/* Now try it's cachelists */
4164 				LGRP_MNODE_COOKIE_INIT(lgrp_cookie, lgrp,
4165 				    LGRP_SRCH_LOCAL);
4166 
4167 				while ((pplist == NULL) &&
4168 				    (mnode = lgrp_memnode_choose(&lgrp_cookie))
4169 				    != -1) {
4170 					pplist =
4171 					    page_get_mnode_cachelist(bin, flags,
4172 					    mnode, mtype);
4173 				}
4174 				if (pplist != NULL) {
4175 					page_hashout(pplist, NULL);
4176 					PP_SETAGED(pplist);
4177 					REPL_STAT_INCR(nhashout);
4178 					break;
4179 				}
4180 				/* Done looking in this lgroup. Bail out. */
4181 				break;
4182 			}
4183 
4184 			/*
4185 			 * No lgroup was specified (or lgroup was removed by
4186 			 * DR, so just try to get the page as close to
4187 			 * like_pp's mnode as possible.
4188 			 * First try the local freelist...
4189 			 */
4190 			mnode = PP_2_MEM_NODE(like_pp);
4191 			pplist = page_get_mnode_freelist(mnode, bin,
4192 			    mtype, szc, flags);
4193 			if (pplist != NULL)
4194 				break;
4195 
4196 			REPL_STAT_INCR(nnofree);
4197 
4198 			/*
4199 			 * ...then the local cachelist. Don't need to do it for
4200 			 * larger pages cause page_freelist_coalesce() already
4201 			 * failed there anyway.
4202 			 */
4203 			if (szc == 0) {
4204 				pplist = page_get_mnode_cachelist(bin, flags,
4205 				    mnode, mtype);
4206 				if (pplist != NULL) {
4207 					page_hashout(pplist, NULL);
4208 					PP_SETAGED(pplist);
4209 					REPL_STAT_INCR(nhashout);
4210 					break;
4211 				}
4212 			}
4213 
4214 			/* Now try remote freelists */
4215 			page_mnode = mnode;
4216 			lgrp =
4217 			    lgrp_hand_to_lgrp(MEM_NODE_2_LGRPHAND(page_mnode));
4218 			LGRP_MNODE_COOKIE_INIT(lgrp_cookie, lgrp,
4219 			    LGRP_SRCH_HIER);
4220 			while (pplist == NULL &&
4221 			    (mnode = lgrp_memnode_choose(&lgrp_cookie))
4222 			    != -1) {
4223 				/*
4224 				 * Skip local mnode.
4225 				 */
4226 				if ((mnode == page_mnode) ||
4227 				    (mem_node_config[mnode].exists == 0))
4228 					continue;
4229 
4230 				pplist = page_get_mnode_freelist(mnode,
4231 				    bin, mtype, szc, flags);
4232 			}
4233 
4234 			if (pplist != NULL)
4235 				break;
4236 
4237 
4238 			/* Now try remote cachelists */
4239 			LGRP_MNODE_COOKIE_INIT(lgrp_cookie, lgrp,
4240 			    LGRP_SRCH_HIER);
4241 			while (pplist == NULL && szc == 0) {
4242 				mnode = lgrp_memnode_choose(&lgrp_cookie);
4243 				if (mnode == -1)
4244 					break;
4245 				/*
4246 				 * Skip local mnode.
4247 				 */
4248 				if ((mnode == page_mnode) ||
4249 				    (mem_node_config[mnode].exists == 0))
4250 					continue;
4251 
4252 				pplist = page_get_mnode_cachelist(bin,
4253 				    flags, mnode, mtype);
4254 
4255 				if (pplist != NULL) {
4256 					page_hashout(pplist, NULL);
4257 					PP_SETAGED(pplist);
4258 					REPL_STAT_INCR(nhashout);
4259 					break;
4260 				}
4261 			}
4262 
4263 			/*
4264 			 * Break out of while loop under the following cases:
4265 			 * - If we successfully got a page.
4266 			 * - If pgrflags specified only returning a specific
4267 			 *   page size and we could not find that page size.
4268 			 * - If we could not satisfy the request with PAGESIZE
4269 			 *   or larger pages.
4270 			 */
4271 			if (pplist != NULL || szc == 0)
4272 				break;
4273 
4274 			if ((pgrflags & PGR_SAMESZC) || pgrppgcp) {
4275 				/* try to find contig page */
4276 
4277 				LGRP_MNODE_COOKIE_INIT(lgrp_cookie, lgrp,
4278 				    LGRP_SRCH_HIER);
4279 
4280 				while ((pplist == NULL) &&
4281 				    (mnode =
4282 				    lgrp_memnode_choose(&lgrp_cookie))
4283 				    != -1) {
4284 					pplist = page_get_contig_pages(
4285 					    mnode, bin, mtype, szc,
4286 					    flags | PGI_PGCPHIPRI);
4287 				}
4288 				break;
4289 			}
4290 
4291 			/*
4292 			 * The correct thing to do here is try the next
4293 			 * page size down using szc--. Due to a bug
4294 			 * with the processing of HAT_RELOAD_SHARE
4295 			 * where the sfmmu_ttecnt arrays of all
4296 			 * hats sharing an ISM segment don't get updated,
4297 			 * using intermediate size pages for relocation
4298 			 * can lead to continuous page faults.
4299 			 */
4300 			szc = 0;
4301 		}
4302 
4303 		if (pplist != NULL) {
4304 			DTRACE_PROBE4(page__get,
4305 			    lgrp_t *, lgrp,
4306 			    int, mnode,
4307 			    ulong_t, bin,
4308 			    uint_t, flags);
4309 
4310 			while (pplist != NULL && pg_cnt--) {
4311 				ASSERT(pplist != NULL);
4312 				pp = pplist;
4313 				page_sub(&pplist, pp);
4314 				PP_CLRFREE(pp);
4315 				PP_CLRAGED(pp);
4316 				page_list_concat(&pl, &pp);
4317 				npgs--;
4318 				like_pp = like_pp + 1;
4319 				REPL_STAT_INCR(nnext_pp);
4320 			}
4321 			ASSERT(pg_cnt == 0);
4322 		} else {
4323 			break;
4324 		}
4325 	}
4326 
4327 	if (npgs) {
4328 		/*
4329 		 * We were unable to allocate the necessary number
4330 		 * of pages.
4331 		 * We need to free up any pl.
4332 		 */
4333 		REPL_STAT_INCR(nnopage);
4334 		page_free_replacement_page(pl);
4335 		return (NULL);
4336 	} else {
4337 		return (pl);
4338 	}
4339 }
4340 
4341 /*
4342  * demote a free large page to it's constituent pages
4343  */
4344 void
4345 page_demote_free_pages(page_t *pp)
4346 {
4347 
4348 	int mnode;
4349 
4350 	ASSERT(pp != NULL);
4351 	ASSERT(PAGE_LOCKED(pp));
4352 	ASSERT(PP_ISFREE(pp));
4353 	ASSERT(pp->p_szc != 0 && pp->p_szc < mmu_page_sizes);
4354 
4355 	mnode = PP_2_MEM_NODE(pp);
4356 	page_freelist_lock(mnode);
4357 	if (pp->p_szc != 0) {
4358 		(void) page_demote(mnode, PFN_BASE(pp->p_pagenum,
4359 		    pp->p_szc), 0, pp->p_szc, 0, PC_NO_COLOR, PC_FREE);
4360 	}
4361 	page_freelist_unlock(mnode);
4362 	ASSERT(pp->p_szc == 0);
4363 }
4364 
4365 /*
4366  * Factor in colorequiv to check additional 'equivalent' bins.
4367  * colorequiv may be set in /etc/system
4368  */
4369 void
4370 page_set_colorequiv_arr(void)
4371 {
4372 	if (colorequiv > 1) {
4373 		int i;
4374 		uint_t sv_a = lowbit(colorequiv) - 1;
4375 
4376 		if (sv_a > 15)
4377 			sv_a = 15;
4378 
4379 		for (i = 0; i < MMU_PAGE_SIZES; i++) {
4380 			uint_t colors;
4381 			uint_t a = sv_a;
4382 
4383 			if ((colors = hw_page_array[i].hp_colors) <= 1) {
4384 				continue;
4385 			}
4386 			while ((colors >> a) == 0)
4387 				a--;
4388 			if ((a << 4) > colorequivszc[i]) {
4389 				colorequivszc[i] = (a << 4);
4390 			}
4391 		}
4392 	}
4393 }
4394