xref: /titanic_50/usr/src/uts/common/os/mem_cage.c (revision 60b08185ce63023f22fd6b2ed0db8c0d119b2023)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/thread.h>
32 #include <sys/proc.h>
33 #include <sys/callb.h>
34 #include <sys/vnode.h>
35 #include <sys/debug.h>
36 #include <sys/systm.h>		/* for bzero */
37 #include <sys/memlist.h>
38 #include <sys/cmn_err.h>
39 #include <sys/sysmacros.h>
40 #include <sys/vmsystm.h>	/* for NOMEMWAIT() */
41 #include <sys/atomic.h>		/* used to update kcage_freemem */
42 #include <sys/kmem.h>		/* for kmem_reap */
43 #include <sys/errno.h>
44 #include <sys/mem_cage.h>
45 #include <vm/seg_kmem.h>
46 #include <vm/page.h>
47 #include <vm/hat.h>
48 #include <vm/vm_dep.h>
49 #include <sys/mem_config.h>
50 #include <sys/lgrp.h>
51 
52 extern pri_t maxclsyspri;
53 
54 #ifdef DEBUG
55 #define	KCAGE_STATS
56 #endif
57 
58 #ifdef KCAGE_STATS
59 
60 #define	KCAGE_STATS_VERSION 9	/* can help report generators */
61 #define	KCAGE_STATS_NSCANS 256	/* depth of scan statistics buffer */
62 
63 struct kcage_stats_scan {
64 	/* managed by KCAGE_STAT_* macros */
65 	clock_t	scan_lbolt;
66 	uint_t	scan_id;
67 
68 	/* set in kcage_cageout() */
69 	uint_t	kt_passes;
70 	clock_t	kt_ticks;
71 	pgcnt_t	kt_kcage_freemem_start;
72 	pgcnt_t	kt_kcage_freemem_end;
73 	pgcnt_t kt_freemem_start;
74 	pgcnt_t kt_freemem_end;
75 	uint_t	kt_examined;
76 	uint_t	kt_cantlock;
77 	uint_t	kt_gotone;
78 	uint_t	kt_gotonefree;
79 	uint_t	kt_skiplevel;
80 	uint_t	kt_skipshared;
81 	uint_t	kt_skiprefd;
82 	uint_t	kt_destroy;
83 
84 	/* set in kcage_invalidate_page() */
85 	uint_t	kip_reloclocked;
86 	uint_t	kip_relocmod;
87 	uint_t	kip_destroy;
88 	uint_t	kip_nomem;
89 	uint_t	kip_demotefailed;
90 
91 	/* set in kcage_expand() */
92 	uint_t	ke_wanted;
93 	uint_t	ke_examined;
94 	uint_t	ke_lefthole;
95 	uint_t	ke_gotone;
96 	uint_t	ke_gotonefree;
97 };
98 
99 struct kcage_stats {
100 	/* managed by KCAGE_STAT_* macros */
101 	uint_t	version;
102 	uint_t	size;
103 
104 	/* set in kcage_cageout */
105 	uint_t	kt_wakeups;
106 	uint_t	kt_scans;
107 	uint_t	kt_cageout_break;
108 
109 	/* set in kcage_expand */
110 	uint_t	ke_calls;
111 	uint_t	ke_nopfn;
112 	uint_t	ke_nopaget;
113 	uint_t	ke_isnoreloc;
114 	uint_t	ke_deleting;
115 	uint_t	ke_lowfreemem;
116 	uint_t	ke_terminate;
117 
118 	/* set in kcage_freemem_add() */
119 	uint_t	kfa_trottlewake;
120 
121 	/* set in kcage_freemem_sub() */
122 	uint_t	kfs_cagewake;
123 
124 	/* set in kcage_create_throttle */
125 	uint_t	kct_calls;
126 	uint_t	kct_cageout;
127 	uint_t	kct_critical;
128 	uint_t	kct_exempt;
129 	uint_t	kct_cagewake;
130 	uint_t	kct_wait;
131 	uint_t	kct_progress;
132 	uint_t	kct_noprogress;
133 	uint_t	kct_timeout;
134 
135 	/* set in kcage_cageout_wakeup */
136 	uint_t	kcw_expandearly;
137 
138 	/* managed by KCAGE_STAT_* macros */
139 	uint_t	scan_array_size;
140 	uint_t	scan_index;
141 	struct kcage_stats_scan scans[KCAGE_STATS_NSCANS];
142 };
143 
144 static struct kcage_stats kcage_stats;
145 static struct kcage_stats_scan kcage_stats_scan_zero;
146 
147 /*
148  * No real need for atomics here. For the most part the incs and sets are
149  * done by the kernel cage thread. There are a few that are done by any
150  * number of other threads. Those cases are noted by comments.
151  */
152 #define	KCAGE_STAT_INCR(m)	kcage_stats.m++
153 
154 #define	KCAGE_STAT_NINCR(m, v) kcage_stats.m += (v)
155 
156 #define	KCAGE_STAT_INCR_SCAN(m)	\
157 	KCAGE_STAT_INCR(scans[kcage_stats.scan_index].m)
158 
159 #define	KCAGE_STAT_NINCR_SCAN(m, v) \
160 	KCAGE_STAT_NINCR(scans[kcage_stats.scan_index].m, v)
161 
162 #define	KCAGE_STAT_SET(m, v)	kcage_stats.m = (v)
163 
164 #define	KCAGE_STAT_SETZ(m, v)	\
165 	if (kcage_stats.m == 0) kcage_stats.m = (v)
166 
167 #define	KCAGE_STAT_SET_SCAN(m, v)	\
168 	KCAGE_STAT_SET(scans[kcage_stats.scan_index].m, v)
169 
170 #define	KCAGE_STAT_SETZ_SCAN(m, v)	\
171 	KCAGE_STAT_SETZ(scans[kcage_stats.scan_index].m, v)
172 
173 #define	KCAGE_STAT_INC_SCAN_INDEX \
174 	KCAGE_STAT_SET_SCAN(scan_lbolt, lbolt); \
175 	KCAGE_STAT_SET_SCAN(scan_id, kcage_stats.scan_index); \
176 	kcage_stats.scan_index = \
177 	(kcage_stats.scan_index + 1) % KCAGE_STATS_NSCANS; \
178 	kcage_stats.scans[kcage_stats.scan_index] = kcage_stats_scan_zero
179 
180 #define	KCAGE_STAT_INIT_SCAN_INDEX \
181 	kcage_stats.version = KCAGE_STATS_VERSION; \
182 	kcage_stats.size = sizeof (kcage_stats); \
183 	kcage_stats.scan_array_size = KCAGE_STATS_NSCANS; \
184 	kcage_stats.scan_index = 0
185 
186 #else /* KCAGE_STATS */
187 
188 #define	KCAGE_STAT_INCR(v)
189 #define	KCAGE_STAT_NINCR(m, v)
190 #define	KCAGE_STAT_INCR_SCAN(v)
191 #define	KCAGE_STAT_NINCR_SCAN(m, v)
192 #define	KCAGE_STAT_SET(m, v)
193 #define	KCAGE_STAT_SETZ(m, v)
194 #define	KCAGE_STAT_SET_SCAN(m, v)
195 #define	KCAGE_STAT_SETZ_SCAN(m, v)
196 #define	KCAGE_STAT_INC_SCAN_INDEX
197 #define	KCAGE_STAT_INIT_SCAN_INDEX
198 
199 #endif /* KCAGE_STATS */
200 
201 static kmutex_t kcage_throttle_mutex;	/* protects kcage_throttle_cv */
202 static kcondvar_t kcage_throttle_cv;
203 
204 static kmutex_t kcage_cageout_mutex;	/* protects cv and ready flag */
205 static kcondvar_t kcage_cageout_cv;	/* cageout thread naps here */
206 static int kcage_cageout_ready;		/* nonzero when cageout thread ready */
207 kthread_id_t kcage_cageout_thread;	/* to aid debugging */
208 
209 static kmutex_t kcage_range_mutex;	/* proctects kcage_glist elements */
210 
211 /*
212  * Cage expansion happens within a range.
213  */
214 struct kcage_glist {
215 	struct kcage_glist	*next;
216 	pfn_t			base;
217 	pfn_t			lim;
218 	pfn_t			curr;
219 	int			decr;
220 };
221 
222 static struct kcage_glist *kcage_glist;
223 static struct kcage_glist *kcage_current_glist;
224 
225 /*
226  * The firstfree element is provided so that kmem_alloc can be avoided
227  * until that cage has somewhere to go. This is not currently a problem
228  * as early kmem_alloc's use BOP_ALLOC instead of page_create_va.
229  */
230 static struct kcage_glist kcage_glist_firstfree;
231 static struct kcage_glist *kcage_glist_freelist = &kcage_glist_firstfree;
232 
233 /*
234  * Miscellaneous forward references
235  */
236 static struct kcage_glist *kcage_glist_alloc(void);
237 static int kcage_glist_delete(pfn_t, pfn_t, struct kcage_glist **);
238 static void kcage_cageout(void);
239 static int kcage_invalidate_page(page_t *, pgcnt_t *);
240 static int kcage_setnoreloc_pages(page_t *, se_t);
241 
242 /*
243  * Kernel Memory Cage counters and thresholds.
244  */
245 int kcage_on = 0;
246 pgcnt_t kcage_freemem;
247 pgcnt_t kcage_needfree;
248 pgcnt_t kcage_lotsfree;
249 pgcnt_t kcage_desfree;
250 pgcnt_t kcage_minfree;
251 pgcnt_t kcage_throttlefree;
252 int kcage_maxwait = 10;	/* in seconds */
253 
254 /* when we use lp for kmem we start the cage at a higher initial value */
255 pgcnt_t kcage_kmemlp_mincage;
256 
257 #ifdef DEBUG
258 pgcnt_t	kcage_pagets;
259 #define	KCAGEPAGETS_INC()	kcage_pagets++
260 #else
261 #define	KCAGEPAGETS_INC()
262 #endif
263 
264 /*
265  * Startup and Dynamic Reconfiguration interfaces.
266  * kcage_range_lock()
267  * kcage_range_unlock()
268  * kcage_range_islocked()
269  * kcage_range_add()
270  * kcage_range_del()
271  * kcage_init()
272  * kcage_set_thresholds()
273  */
274 
275 int
276 kcage_range_trylock(void)
277 {
278 	return (mutex_tryenter(&kcage_range_mutex));
279 }
280 
281 void
282 kcage_range_lock(void)
283 {
284 	mutex_enter(&kcage_range_mutex);
285 }
286 
287 void
288 kcage_range_unlock(void)
289 {
290 	mutex_exit(&kcage_range_mutex);
291 }
292 
293 int
294 kcage_range_islocked(void)
295 {
296 	return (MUTEX_HELD(&kcage_range_mutex));
297 }
298 
299 /*
300  * Called from page_get_contig_pages to get the approximate kcage pfn range
301  * for exclusion from search for contiguous pages. This routine is called
302  * without kcage_range lock (kcage routines can call page_get_contig_pages
303  * through page_relocate) and with the assumption, based on kcage_range_add,
304  * that kcage_current_glist always contain a valid pointer.
305  */
306 
307 int
308 kcage_current_pfn(pfn_t *pfncur)
309 {
310 	struct kcage_glist *lp = kcage_current_glist;
311 
312 	ASSERT(kcage_on);
313 
314 	ASSERT(lp != NULL);
315 
316 	*pfncur = lp->curr;
317 
318 	return (lp->decr);
319 }
320 
321 int
322 kcage_range_init(struct memlist *ml, int decr)
323 {
324 	int ret = 0;
325 
326 	ASSERT(kcage_range_islocked());
327 
328 	if (decr) {
329 		while (ml->next != NULL)
330 			ml = ml->next;
331 	}
332 
333 	while (ml != NULL) {
334 		ret = kcage_range_add(btop(ml->address), btop(ml->size), decr);
335 		if (ret)
336 			break;
337 
338 		ml = (decr ? ml->prev : ml->next);
339 	}
340 
341 	return (ret);
342 }
343 
344 /*
345  * Third arg controls direction of growth: 0: increasing pfns,
346  * 1: decreasing.
347  * Calls to add and delete must be protected by calls to
348  * kcage_range_lock() and kcage_range_unlock().
349  */
350 int
351 kcage_range_add(pfn_t base, pgcnt_t npgs, int decr)
352 {
353 	struct kcage_glist *new, **lpp;
354 	pfn_t lim;
355 
356 	ASSERT(kcage_range_islocked());
357 
358 	ASSERT(npgs != 0);
359 	if (npgs == 0)
360 		return (EINVAL);
361 
362 	lim = base + npgs;
363 
364 	ASSERT(lim > base);
365 	if (lim <= base)
366 		return (EINVAL);
367 
368 	new = kcage_glist_alloc();
369 	if (new == NULL) {
370 		return (ENOMEM);
371 	}
372 
373 	new->base = base;
374 	new->lim = lim;
375 	new->decr = decr;
376 	if (new->decr != 0)
377 		new->curr = new->lim;
378 	else
379 		new->curr = new->base;
380 	/*
381 	 * Any overlapping existing ranges are removed by deleting
382 	 * from the new list as we search for the tail.
383 	 */
384 	lpp = &kcage_glist;
385 	while (*lpp != NULL) {
386 		int ret;
387 		ret = kcage_glist_delete((*lpp)->base, (*lpp)->lim, &new);
388 		if (ret != 0)
389 			return (ret);
390 		lpp = &(*lpp)->next;
391 	}
392 
393 	*lpp = new;
394 
395 	if (kcage_current_glist == NULL) {
396 		kcage_current_glist = kcage_glist;
397 	}
398 
399 	return (0);
400 }
401 
402 /*
403  * Calls to add and delete must be protected by calls to
404  * kcage_range_lock() and kcage_range_unlock().
405  */
406 int
407 kcage_range_delete(pfn_t base, pgcnt_t npgs)
408 {
409 	struct kcage_glist *lp;
410 	pfn_t lim;
411 
412 	ASSERT(kcage_range_islocked());
413 
414 	ASSERT(npgs != 0);
415 	if (npgs == 0)
416 		return (EINVAL);
417 
418 	lim = base + npgs;
419 
420 	ASSERT(lim > base);
421 	if (lim <= base)
422 		return (EINVAL);
423 
424 	/*
425 	 * Check if the delete is OK first as a number of elements
426 	 * might be involved and it will be difficult to go
427 	 * back and undo (can't just add the range back in).
428 	 */
429 	for (lp = kcage_glist; lp != NULL; lp = lp->next) {
430 		/*
431 		 * If there have been no pages allocated from this
432 		 * element, we don't need to check it.
433 		 */
434 		if ((lp->decr == 0 && lp->curr == lp->base) ||
435 		    (lp->decr != 0 && lp->curr == lp->lim))
436 			continue;
437 		/*
438 		 * If the element does not overlap, its OK.
439 		 */
440 		if (base >= lp->lim || lim <= lp->base)
441 			continue;
442 		/*
443 		 * Overlapping element: Does the range to be deleted
444 		 * overlap the area already used? If so fail.
445 		 */
446 		if (lp->decr == 0 && base < lp->curr && lim >= lp->base) {
447 			return (EBUSY);
448 		}
449 		if (lp->decr != 0 && base < lp->lim && lim >= lp->curr) {
450 			return (EBUSY);
451 		}
452 	}
453 	return (kcage_glist_delete(base, lim, &kcage_glist));
454 }
455 
456 /*
457  * Calls to add and delete must be protected by calls to
458  * kcage_range_lock() and kcage_range_unlock().
459  * This routine gets called after successful Solaris memory
460  * delete operation from DR post memory delete routines.
461  */
462 int
463 kcage_range_delete_post_mem_del(pfn_t base, pgcnt_t npgs)
464 {
465 	pfn_t lim;
466 
467 	ASSERT(kcage_range_islocked());
468 
469 	ASSERT(npgs != 0);
470 	if (npgs == 0)
471 		return (EINVAL);
472 
473 	lim = base + npgs;
474 
475 	ASSERT(lim > base);
476 	if (lim <= base)
477 		return (EINVAL);
478 
479 	return (kcage_glist_delete(base, lim, &kcage_glist));
480 }
481 
482 /*
483  * No locking is required here as the whole operation is covered
484  * by the kcage_range_lock().
485  */
486 static struct kcage_glist *
487 kcage_glist_alloc(void)
488 {
489 	struct kcage_glist *new;
490 
491 	if ((new = kcage_glist_freelist) != NULL) {
492 		kcage_glist_freelist = new->next;
493 		bzero(new, sizeof (*new));
494 	} else {
495 		new = kmem_zalloc(sizeof (struct kcage_glist), KM_NOSLEEP);
496 	}
497 	return (new);
498 }
499 
500 static void
501 kcage_glist_free(struct kcage_glist *lp)
502 {
503 	lp->next = kcage_glist_freelist;
504 	kcage_glist_freelist = lp;
505 }
506 
507 static int
508 kcage_glist_delete(pfn_t base, pfn_t lim, struct kcage_glist **lpp)
509 {
510 	struct kcage_glist *lp, *prev = *lpp;
511 
512 	while ((lp = *lpp) != NULL) {
513 		if (lim > lp->base && base < lp->lim) {
514 			/* The delete range overlaps this element. */
515 			if (base <= lp->base && lim >= lp->lim) {
516 				/* Delete whole element. */
517 				*lpp = lp->next;
518 				if (lp == kcage_current_glist) {
519 					/* This can never happen. */
520 					ASSERT(kcage_current_glist != prev);
521 					kcage_current_glist = prev;
522 				}
523 				kcage_glist_free(lp);
524 				continue;
525 			}
526 
527 			/* Partial delete. */
528 			if (base > lp->base && lim < lp->lim) {
529 				struct kcage_glist *new;
530 
531 				/*
532 				 * Remove a section from the middle,
533 				 * need to allocate a new element.
534 				 */
535 				new = kcage_glist_alloc();
536 				if (new == NULL) {
537 					return (ENOMEM);
538 				}
539 
540 				/*
541 				 * Tranfser unused range to new.
542 				 * Edit lp in place to preserve
543 				 * kcage_current_glist.
544 				 */
545 				new->decr = lp->decr;
546 				if (new->decr != 0) {
547 					new->base = lp->base;
548 					new->lim = base;
549 					new->curr = base;
550 
551 					lp->base = lim;
552 				} else {
553 					new->base = lim;
554 					new->lim = lp->lim;
555 					new->curr = new->base;
556 
557 					lp->lim = base;
558 				}
559 
560 				/* Insert new. */
561 				new->next = lp->next;
562 				lp->next = new;
563 				lpp = &lp->next;
564 			} else {
565 				/* Delete part of current block. */
566 				if (base > lp->base) {
567 					ASSERT(lim >= lp->lim);
568 					ASSERT(base < lp->lim);
569 					if (lp->decr != 0 &&
570 					    lp->curr == lp->lim)
571 						lp->curr = base;
572 					lp->lim = base;
573 				} else {
574 					ASSERT(base <= lp->base);
575 					ASSERT(lim > lp->base);
576 					if (lp->decr == 0 &&
577 					    lp->curr == lp->base)
578 						lp->curr = lim;
579 					lp->base = lim;
580 				}
581 			}
582 		}
583 		prev = *lpp;
584 		lpp = &(*lpp)->next;
585 	}
586 
587 	return (0);
588 }
589 
590 /*
591  * The caller of kcage_get_pfn must hold the kcage_range_lock to make
592  * sure that there are no concurrent calls. The same lock
593  * must be obtained for range add and delete by calling
594  * kcage_range_lock() and kcage_range_unlock().
595  */
596 static pfn_t
597 kcage_get_pfn(void)
598 {
599 	struct kcage_glist *lp;
600 	pfn_t pfn;
601 
602 	ASSERT(kcage_range_islocked());
603 
604 	lp = kcage_current_glist;
605 	while (lp != NULL) {
606 		if (lp->decr != 0) {
607 			if (lp->curr != lp->base) {
608 				pfn = --lp->curr;
609 				return (pfn);
610 			}
611 		} else {
612 			if (lp->curr != lp->lim) {
613 				pfn = lp->curr++;
614 				return (pfn);
615 			}
616 		}
617 
618 		lp = lp->next;
619 		if (lp)
620 			kcage_current_glist = lp;
621 	}
622 
623 	return (PFN_INVALID);
624 }
625 
626 /*
627  * Walk the physical address space of the cage.
628  * This routine does not guarantee to return PFNs in the order
629  * in which they were allocated to the cage. Instead, it walks
630  * each range as they appear on the growth list returning the PFNs
631  * range in ascending order.
632  *
633  * To begin scanning at lower edge of cage, reset should be nonzero.
634  * To step through cage, reset should be zero.
635  *
636  * PFN_INVALID will be returned when the upper end of the cage is
637  * reached -- indicating a full scan of the cage has been completed since
638  * previous reset. PFN_INVALID will continue to be returned until
639  * kcage_walk_cage is reset.
640  *
641  * It is possible to receive a PFN_INVALID result on reset if a growth
642  * list is not installed or if none of the PFNs in the installed list have
643  * been allocated to the cage. In otherwords, there is no cage.
644  *
645  * Caller need not hold kcage_range_lock while calling this function
646  * as the front part of the list is static - pages never come out of
647  * the cage.
648  *
649  * The caller is expected to only be kcage_cageout().
650  */
651 static pfn_t
652 kcage_walk_cage(int reset)
653 {
654 	static struct kcage_glist *lp = NULL;
655 	static pfn_t pfn;
656 
657 	if (reset)
658 		lp = NULL;
659 	if (lp == NULL) {
660 		lp = kcage_glist;
661 		pfn = PFN_INVALID;
662 	}
663 again:
664 	if (pfn == PFN_INVALID) {
665 		if (lp == NULL)
666 			return (PFN_INVALID);
667 
668 		if (lp->decr != 0) {
669 			/*
670 			 * In this range the cage grows from the highest
671 			 * address towards the lowest.
672 			 * Arrange to return pfns from curr to lim-1,
673 			 * inclusive, in ascending order.
674 			 */
675 
676 			pfn = lp->curr;
677 		} else {
678 			/*
679 			 * In this range the cage grows from the lowest
680 			 * address towards the highest.
681 			 * Arrange to return pfns from base to curr,
682 			 * inclusive, in ascending order.
683 			 */
684 
685 			pfn = lp->base;
686 		}
687 	}
688 
689 	if (lp->decr != 0) {		/* decrementing pfn */
690 		if (pfn == lp->lim) {
691 			/* Don't go beyond the static part of the glist. */
692 			if (lp == kcage_current_glist)
693 				lp = NULL;
694 			else
695 				lp = lp->next;
696 			pfn = PFN_INVALID;
697 			goto again;
698 		}
699 
700 		ASSERT(pfn >= lp->curr && pfn < lp->lim);
701 	} else {			/* incrementing pfn */
702 		if (pfn == lp->curr) {
703 			/* Don't go beyond the static part of the glist. */
704 			if (lp == kcage_current_glist)
705 				lp = NULL;
706 			else
707 				lp = lp->next;
708 			pfn = PFN_INVALID;
709 			goto again;
710 		}
711 
712 		ASSERT(pfn >= lp->base && pfn < lp->curr);
713 	}
714 
715 	return (pfn++);
716 }
717 
718 /*
719  * Callback functions for to recalc cage thresholds after
720  * Kphysm memory add/delete operations.
721  */
722 /*ARGSUSED*/
723 static void
724 kcage_kphysm_postadd_cb(void *arg, pgcnt_t delta_pages)
725 {
726 	kcage_recalc_thresholds();
727 }
728 
729 /*ARGSUSED*/
730 static int
731 kcage_kphysm_predel_cb(void *arg, pgcnt_t delta_pages)
732 {
733 	/* TODO: when should cage refuse memory delete requests? */
734 	return (0);
735 }
736 
737 /*ARGSUSED*/
738 static  void
739 kcage_kphysm_postdel_cb(void *arg, pgcnt_t delta_pages, int cancelled)
740 {
741 	kcage_recalc_thresholds();
742 }
743 
744 static kphysm_setup_vector_t kcage_kphysm_vectors = {
745 	KPHYSM_SETUP_VECTOR_VERSION,
746 	kcage_kphysm_postadd_cb,
747 	kcage_kphysm_predel_cb,
748 	kcage_kphysm_postdel_cb
749 };
750 
751 /*
752  * This is called before a CPR suspend and after a CPR resume.  We have to
753  * turn off kcage_cageout_ready before a suspend, and turn it back on after a
754  * restart.
755  */
756 /*ARGSUSED*/
757 static boolean_t
758 kcage_cageout_cpr(void *arg, int code)
759 {
760 	if (code == CB_CODE_CPR_CHKPT) {
761 		ASSERT(kcage_cageout_ready);
762 		kcage_cageout_ready = 0;
763 		return (B_TRUE);
764 	} else if (code == CB_CODE_CPR_RESUME) {
765 		ASSERT(kcage_cageout_ready == 0);
766 		kcage_cageout_ready = 1;
767 		return (B_TRUE);
768 	}
769 	return (B_FALSE);
770 }
771 
772 /*
773  * kcage_recalc_preferred_size() increases initial cage size to improve large
774  * page availability when lp for kmem is enabled and kpr is disabled
775  */
776 static pgcnt_t
777 kcage_recalc_preferred_size(pgcnt_t preferred_size)
778 {
779 	if (SEGKMEM_USE_LARGEPAGES && segkmem_reloc == 0) {
780 		pgcnt_t lpmincage = kcage_kmemlp_mincage;
781 		if (lpmincage == 0) {
782 			lpmincage = MIN(P2ROUNDUP(((physmem * PAGESIZE) / 8),
783 			    segkmem_heaplp_quantum), 0x40000000UL) / PAGESIZE;
784 		}
785 		kcage_kmemlp_mincage = MIN(lpmincage,
786 			    (segkmem_kmemlp_max / PAGESIZE));
787 		preferred_size = MAX(kcage_kmemlp_mincage, preferred_size);
788 	}
789 	return (preferred_size);
790 }
791 
792 /*
793  * Kcage_init() builds the cage and initializes the cage thresholds.
794  * The size of the cage is determined by the argument preferred_size.
795  * or the actual amount of memory, whichever is smaller.
796  */
797 void
798 kcage_init(pgcnt_t preferred_size)
799 {
800 	pgcnt_t wanted;
801 	pfn_t pfn;
802 	page_t *pp;
803 	extern struct vnode kvp;
804 	extern void page_list_noreloc_startup(page_t *);
805 
806 	ASSERT(!kcage_on);
807 	ASSERT(kcage_range_islocked());
808 
809 	/* increase preferred cage size for lp for kmem */
810 	preferred_size = kcage_recalc_preferred_size(preferred_size);
811 
812 	/* Debug note: initialize this now so early expansions can stat */
813 	KCAGE_STAT_INIT_SCAN_INDEX;
814 
815 	/*
816 	 * Initialize cage thresholds and install kphysm callback.
817 	 * If we can't arrange to have the thresholds track with
818 	 * available physical memory, then the cage thresholds may
819 	 * end up over time at levels that adversly effect system
820 	 * performance; so, bail out.
821 	 */
822 	kcage_recalc_thresholds();
823 	if (kphysm_setup_func_register(&kcage_kphysm_vectors, NULL)) {
824 		ASSERT(0);		/* Catch this in DEBUG kernels. */
825 		return;
826 	}
827 
828 	/*
829 	 * Limit startup cage size within the range of kcage_minfree
830 	 * and availrmem, inclusively.
831 	 */
832 	wanted = MIN(MAX(preferred_size, kcage_minfree), availrmem);
833 
834 	/*
835 	 * Construct the cage. PFNs are allocated from the glist. It
836 	 * is assumed that the list has been properly ordered for the
837 	 * platform by the platform code. Typically, this is as simple
838 	 * as calling kcage_range_init(phys_avail, decr), where decr is
839 	 * 1 if the kernel has been loaded into upper end of physical
840 	 * memory, or 0 if the kernel has been loaded at the low end.
841 	 *
842 	 * Note: it is assumed that we are in the startup flow, so there
843 	 * is no reason to grab the page lock.
844 	 */
845 	kcage_freemem = 0;
846 	pfn = PFN_INVALID;			/* prime for alignment test */
847 	while (wanted != 0) {
848 		if ((pfn = kcage_get_pfn()) == PFN_INVALID)
849 			break;
850 
851 		if ((pp = page_numtopp_nolock(pfn)) != NULL) {
852 			KCAGEPAGETS_INC();
853 			/*
854 			 * Set the noreloc state on the page.
855 			 * If the page is free and not already
856 			 * on the noreloc list then move it.
857 			 */
858 			if (PP_ISFREE(pp)) {
859 				if (PP_ISNORELOC(pp) == 0)
860 					page_list_noreloc_startup(pp);
861 			} else {
862 				ASSERT(pp->p_szc == 0);
863 				PP_SETNORELOC(pp);
864 			}
865 		}
866 
867 		wanted -= 1;
868 	}
869 
870 	/*
871 	 * Need to go through and find kernel allocated pages
872 	 * and capture them into the Cage.  These will primarily
873 	 * be pages gotten through boot_alloc().
874 	 */
875 	if (kvp.v_pages) {
876 
877 		pp = kvp.v_pages;
878 		do {
879 			ASSERT(!PP_ISFREE(pp));
880 			ASSERT(pp->p_szc == 0);
881 			PP_SETNORELOC(pp);
882 		} while ((pp = pp->p_vpnext) != kvp.v_pages);
883 
884 	}
885 
886 	kcage_on = 1;
887 
888 	/*
889 	 * CB_CL_CPR_POST_KERNEL is the class that executes from cpr_suspend()
890 	 * after the cageout thread is blocked, and executes from cpr_resume()
891 	 * before the cageout thread is restarted.  By executing in this class,
892 	 * we are assured that the kernel cage thread won't miss wakeup calls
893 	 * and also CPR's larger kmem_alloc requests will not fail after
894 	 * CPR shuts down the cageout kernel thread.
895 	 */
896 	(void) callb_add(kcage_cageout_cpr, NULL, CB_CL_CPR_POST_KERNEL,
897 	    "cageout");
898 
899 	/*
900 	 * Coalesce pages to improve large page availability. A better fix
901 	 * would to coalesce pages as they are included in the cage
902 	 */
903 	if (SEGKMEM_USE_LARGEPAGES) {
904 		extern void page_freelist_coalesce_all(int mnode);
905 		extern int max_mem_nodes;
906 		int mnode, max_mnodes = max_mem_nodes;
907 		for (mnode = 0; mnode < max_mnodes; mnode++) {
908 			page_freelist_coalesce_all(mnode);
909 		}
910 	}
911 }
912 
913 void
914 kcage_recalc_thresholds()
915 {
916 	static int first = 1;
917 	static pgcnt_t init_lotsfree;
918 	static pgcnt_t init_desfree;
919 	static pgcnt_t init_minfree;
920 	static pgcnt_t init_throttlefree;
921 
922 	/* TODO: any reason to take more care than this with live editing? */
923 	mutex_enter(&kcage_cageout_mutex);
924 	mutex_enter(&freemem_lock);
925 
926 	if (first) {
927 		first = 0;
928 		init_lotsfree = kcage_lotsfree;
929 		init_desfree = kcage_desfree;
930 		init_minfree = kcage_minfree;
931 		init_throttlefree = kcage_throttlefree;
932 	} else {
933 		kcage_lotsfree = init_lotsfree;
934 		kcage_desfree = init_desfree;
935 		kcage_minfree = init_minfree;
936 		kcage_throttlefree = init_throttlefree;
937 	}
938 
939 	if (kcage_lotsfree == 0)
940 		kcage_lotsfree = MAX(32, total_pages / 256);
941 
942 	if (kcage_minfree == 0)
943 		kcage_minfree = MAX(32, kcage_lotsfree / 2);
944 
945 	if (kcage_desfree == 0)
946 		kcage_desfree = MAX(32, kcage_minfree);
947 
948 	if (kcage_throttlefree == 0)
949 		kcage_throttlefree = MAX(32, kcage_minfree / 2);
950 
951 	mutex_exit(&freemem_lock);
952 	mutex_exit(&kcage_cageout_mutex);
953 
954 	if (kcage_cageout_ready) {
955 		if (kcage_freemem < kcage_desfree)
956 			kcage_cageout_wakeup();
957 
958 		if (kcage_needfree) {
959 			mutex_enter(&kcage_throttle_mutex);
960 			cv_broadcast(&kcage_throttle_cv);
961 			mutex_exit(&kcage_throttle_mutex);
962 		}
963 	}
964 }
965 
966 /*
967  * Pageout interface:
968  * kcage_cageout_init()
969  */
970 void
971 kcage_cageout_init()
972 {
973 	if (kcage_on) {
974 
975 		(void) thread_create(NULL, 0, kcage_cageout,
976 		    NULL, 0, proc_pageout, TS_RUN, maxclsyspri - 1);
977 	}
978 }
979 
980 
981 /*
982  * VM Interfaces:
983  * kcage_create_throttle()
984  * kcage_freemem_add()
985  * kcage_freemem_sub()
986  */
987 
988 /*
989  * Wakeup cageout thread and throttle waiting for the number of pages
990  * requested to become available.  For non-critical requests, a
991  * timeout is added, since freemem accounting is separate from cage
992  * freemem accounting: it's possible for us to get stuck and not make
993  * forward progress even though there was sufficient freemem before
994  * arriving here.
995  */
996 int
997 kcage_create_throttle(pgcnt_t npages, int flags)
998 {
999 	int niter = 0;
1000 	pgcnt_t lastfree;
1001 	int enough = kcage_freemem > kcage_throttlefree + npages;
1002 
1003 	KCAGE_STAT_INCR(kct_calls);		/* unprotected incr. */
1004 
1005 	kcage_cageout_wakeup();			/* just to be sure */
1006 	KCAGE_STAT_INCR(kct_cagewake);		/* unprotected incr. */
1007 
1008 	/*
1009 	 * Obviously, we can't throttle the cageout thread since
1010 	 * we depend on it.  We also can't throttle the panic thread.
1011 	 */
1012 	if (curthread == kcage_cageout_thread || panicstr) {
1013 		KCAGE_STAT_INCR(kct_cageout);	/* unprotected incr. */
1014 		return (KCT_CRIT);
1015 	}
1016 
1017 	/*
1018 	 * Don't throttle threads which are critical for proper
1019 	 * vm management if we're above kcage_throttlefree or
1020 	 * if freemem is very low.
1021 	 */
1022 	if (NOMEMWAIT()) {
1023 		if (enough) {
1024 			KCAGE_STAT_INCR(kct_exempt);	/* unprotected incr. */
1025 			return (KCT_CRIT);
1026 		} else if (freemem < minfree) {
1027 			KCAGE_STAT_INCR(kct_critical);  /* unprotected incr. */
1028 			return (KCT_CRIT);
1029 		}
1030 	}
1031 
1032 	/*
1033 	 * Don't throttle real-time threads.
1034 	 */
1035 	if (DISP_PRIO(curthread) > maxclsyspri) {
1036 		KCAGE_STAT_INCR(kct_exempt);	/* unprotected incr. */
1037 		return (KCT_CRIT);
1038 	}
1039 
1040 	/*
1041 	 * Cause all other threads (which are assumed to not be
1042 	 * critical to cageout) to wait here until their request
1043 	 * can be satisfied. Be a little paranoid and wake the
1044 	 * kernel cage on each loop through this logic.
1045 	 */
1046 	while (kcage_freemem < kcage_throttlefree + npages) {
1047 		ASSERT(kcage_on);
1048 
1049 		lastfree = kcage_freemem;
1050 
1051 		if (kcage_cageout_ready) {
1052 			mutex_enter(&kcage_throttle_mutex);
1053 
1054 			kcage_needfree += npages;
1055 			KCAGE_STAT_INCR(kct_wait);
1056 
1057 			kcage_cageout_wakeup();
1058 			KCAGE_STAT_INCR(kct_cagewake);
1059 
1060 			cv_wait(&kcage_throttle_cv, &kcage_throttle_mutex);
1061 
1062 			kcage_needfree -= npages;
1063 
1064 			mutex_exit(&kcage_throttle_mutex);
1065 		} else {
1066 			/*
1067 			 * NOTE: atomics are used just in case we enter
1068 			 * mp operation before the cageout thread is ready.
1069 			 */
1070 			atomic_add_long(&kcage_needfree, npages);
1071 
1072 			kcage_cageout_wakeup();
1073 			KCAGE_STAT_INCR(kct_cagewake);	/* unprotected incr. */
1074 
1075 			atomic_add_long(&kcage_needfree, -npages);
1076 		}
1077 
1078 		if ((flags & PG_WAIT) == 0) {
1079 			if (kcage_freemem > lastfree) {
1080 				KCAGE_STAT_INCR(kct_progress);
1081 				niter = 0;
1082 			} else {
1083 				KCAGE_STAT_INCR(kct_noprogress);
1084 				if (++niter >= kcage_maxwait) {
1085 					KCAGE_STAT_INCR(kct_timeout);
1086 					return (KCT_FAILURE);
1087 				}
1088 			}
1089 		}
1090 	}
1091 	return (KCT_NONCRIT);
1092 }
1093 
1094 void
1095 kcage_freemem_add(pgcnt_t npages)
1096 {
1097 	extern void wakeup_pcgs(void);
1098 
1099 	atomic_add_long(&kcage_freemem, npages);
1100 
1101 	wakeup_pcgs();  /* wakeup threads in pcgs() */
1102 
1103 	if (kcage_needfree != 0 &&
1104 		kcage_freemem >= (kcage_throttlefree + kcage_needfree)) {
1105 
1106 		mutex_enter(&kcage_throttle_mutex);
1107 		cv_broadcast(&kcage_throttle_cv);
1108 		KCAGE_STAT_INCR(kfa_trottlewake);
1109 		mutex_exit(&kcage_throttle_mutex);
1110 	}
1111 }
1112 
1113 void
1114 kcage_freemem_sub(pgcnt_t npages)
1115 {
1116 	atomic_add_long(&kcage_freemem, -npages);
1117 
1118 	if (kcage_freemem < kcage_desfree) {
1119 		kcage_cageout_wakeup();
1120 		KCAGE_STAT_INCR(kfs_cagewake); /* unprotected incr. */
1121 	}
1122 }
1123 
1124 /*
1125  * return 0 on failure and 1 on success.
1126  */
1127 static int
1128 kcage_setnoreloc_pages(page_t *rootpp, se_t se)
1129 {
1130 	pgcnt_t npgs, i;
1131 	page_t *pp;
1132 	pfn_t rootpfn = page_pptonum(rootpp);
1133 	uint_t szc;
1134 
1135 	ASSERT(!PP_ISFREE(rootpp));
1136 	ASSERT(PAGE_LOCKED_SE(rootpp, se));
1137 	if (!group_page_trylock(rootpp, se)) {
1138 		return (0);
1139 	}
1140 	szc = rootpp->p_szc;
1141 	if (szc == 0) {
1142 		/*
1143 		 * The szc of a locked page can only change for pages that are
1144 		 * non-swapfs (i.e. anonymous memory) file system pages.
1145 		 */
1146 		ASSERT(rootpp->p_vnode != NULL &&
1147 		    rootpp->p_vnode != &kvp &&
1148 		    !IS_SWAPFSVP(rootpp->p_vnode));
1149 		PP_SETNORELOC(rootpp);
1150 		return (1);
1151 	}
1152 	npgs = page_get_pagecnt(szc);
1153 	ASSERT(IS_P2ALIGNED(rootpfn, npgs));
1154 	pp = rootpp;
1155 	for (i = 0; i < npgs; i++, pp++) {
1156 		ASSERT(PAGE_LOCKED_SE(pp, se));
1157 		ASSERT(!PP_ISFREE(pp));
1158 		ASSERT(pp->p_szc == szc);
1159 		PP_SETNORELOC(pp);
1160 	}
1161 	group_page_unlock(rootpp);
1162 	return (1);
1163 }
1164 
1165 /*
1166  * Attempt to convert page to a caged page (set the P_NORELOC flag).
1167  * If successful and pages is free, move page to the tail of whichever
1168  * list it is on.
1169  * Returns:
1170  *   EBUSY  page already locked, assimilated but not free.
1171  *   ENOMEM page assimilated, but memory too low to relocate. Page not free.
1172  *   EAGAIN page not assimilated. Page not free.
1173  *   ERANGE page assimilated. Page not root.
1174  *   0      page assimilated. Page free.
1175  *   *nfreedp number of pages freed.
1176  * NOTE: With error codes ENOMEM, EBUSY, and 0 (zero), there is no way
1177  * to distinguish between a page that was already a NORELOC page from
1178  * those newly converted to NORELOC pages by this invocation of
1179  * kcage_assimilate_page.
1180  */
1181 static int
1182 kcage_assimilate_page(page_t *pp, pgcnt_t *nfreedp)
1183 {
1184 	if (page_trylock(pp, SE_EXCL)) {
1185 		if (PP_ISNORELOC(pp)) {
1186 check_free_and_return:
1187 			if (PP_ISFREE(pp)) {
1188 				page_unlock(pp);
1189 				*nfreedp = 0;
1190 				return (0);
1191 			} else {
1192 				page_unlock(pp);
1193 				return (EBUSY);
1194 			}
1195 			/*NOTREACHED*/
1196 		}
1197 	} else {
1198 		if (page_trylock(pp, SE_SHARED)) {
1199 			if (PP_ISNORELOC(pp))
1200 				goto check_free_and_return;
1201 		} else
1202 			return (EAGAIN);
1203 
1204 		if (!PP_ISFREE(pp)) {
1205 			page_unlock(pp);
1206 			return (EAGAIN);
1207 		}
1208 
1209 		/*
1210 		 * Need to upgrade the lock on it and set the NORELOC
1211 		 * bit. If it is free then remove it from the free
1212 		 * list so that the platform free list code can keep
1213 		 * NORELOC pages where they should be.
1214 		 */
1215 		/*
1216 		 * Before doing anything, get the exclusive lock.
1217 		 * This may fail (eg ISM pages are left shared locked).
1218 		 * If the page is free this will leave a hole in the
1219 		 * cage. There is no solution yet to this.
1220 		 */
1221 		if (!page_tryupgrade(pp)) {
1222 			page_unlock(pp);
1223 			return (EAGAIN);
1224 		}
1225 	}
1226 
1227 	ASSERT(PAGE_EXCL(pp));
1228 
1229 	if (PP_ISFREE(pp)) {
1230 		int which = PP_ISAGED(pp) ? PG_FREE_LIST : PG_CACHE_LIST;
1231 
1232 		page_list_sub(pp, which | PG_LIST_ISCAGE);
1233 		ASSERT(pp->p_szc == 0);
1234 		PP_SETNORELOC(pp);
1235 		page_list_add(pp, which | PG_LIST_TAIL | PG_LIST_ISCAGE);
1236 
1237 		page_unlock(pp);
1238 		*nfreedp = 1;
1239 		return (0);
1240 	} else {
1241 		if (pp->p_szc != 0) {
1242 			if (!kcage_setnoreloc_pages(pp, SE_EXCL)) {
1243 				page_unlock(pp);
1244 				return (EAGAIN);
1245 			}
1246 			ASSERT(PP_ISNORELOC(pp));
1247 		} else {
1248 			PP_SETNORELOC(pp);
1249 		}
1250 		page_list_xfer(pp, MTYPE_NORELOC, MTYPE_RELOC);
1251 		return (kcage_invalidate_page(pp, nfreedp));
1252 	}
1253 	/*NOTREACHED*/
1254 }
1255 
1256 static int
1257 kcage_expand()
1258 {
1259 	int did_something = 0;
1260 
1261 	spgcnt_t wanted;
1262 	pfn_t pfn;
1263 	page_t *pp;
1264 	/* TODO: we don't really need n any more? */
1265 	pgcnt_t n;
1266 	pgcnt_t nf, nfreed;
1267 
1268 	/*
1269 	 * Expand the cage if available cage memory is really low. Calculate
1270 	 * the amount required to return kcage_freemem to the level of
1271 	 * kcage_lotsfree, or to satisfy throttled requests, whichever is
1272 	 * more.  It is rare for their sum to create an artificial threshold
1273 	 * above kcage_lotsfree, but it is possible.
1274 	 *
1275 	 * Exit early if expansion amount is equal to or less than zero.
1276 	 * (<0 is possible if kcage_freemem rises suddenly.)
1277 	 *
1278 	 * Exit early when the global page pool (apparently) does not
1279 	 * have enough free pages to page_relocate() even a single page.
1280 	 */
1281 	wanted = MAX(kcage_lotsfree, kcage_throttlefree + kcage_needfree)
1282 		- kcage_freemem;
1283 	if (wanted <= 0)
1284 		return (0);
1285 	else if (freemem < pageout_reserve + 1) {
1286 		KCAGE_STAT_INCR(ke_lowfreemem);
1287 		return (0);
1288 	}
1289 
1290 	/*
1291 	 * Try to get the range list lock. If the lock is already
1292 	 * held, then don't get stuck here waiting for it.
1293 	 */
1294 	if (!kcage_range_trylock())
1295 		return (0);
1296 
1297 	KCAGE_STAT_INCR(ke_calls);
1298 	KCAGE_STAT_SET_SCAN(ke_wanted, (uint_t)wanted);
1299 
1300 	/*
1301 	 * Assimilate more pages from the global page pool into the cage.
1302 	 */
1303 	n = 0;				/* number of pages PP_SETNORELOC'd */
1304 	nf = 0;				/* number of those actually free */
1305 	while (kcage_on && nf < wanted) {
1306 		pfn = kcage_get_pfn();
1307 		if (pfn == PFN_INVALID) {	/* eek! no where to grow */
1308 			KCAGE_STAT_INCR(ke_nopfn);
1309 			goto terminate;
1310 		}
1311 
1312 		KCAGE_STAT_INCR_SCAN(ke_examined);
1313 
1314 		if ((pp = page_numtopp_nolock(pfn)) == NULL) {
1315 			KCAGE_STAT_INCR(ke_nopaget);
1316 			continue;
1317 		}
1318 		KCAGEPAGETS_INC();
1319 		/*
1320 		 * Sanity check. Skip this pfn if it is
1321 		 * being deleted.
1322 		 */
1323 		if (pfn_is_being_deleted(pfn)) {
1324 			KCAGE_STAT_INCR(ke_deleting);
1325 			continue;
1326 		}
1327 
1328 		/*
1329 		 * NORELOC is only set at boot-time or by this routine
1330 		 * under the kcage_range_mutex lock which is currently
1331 		 * held. This means we can do a fast check here before
1332 		 * locking the page in kcage_assimilate_page.
1333 		 */
1334 		if (PP_ISNORELOC(pp)) {
1335 			KCAGE_STAT_INCR(ke_isnoreloc);
1336 			continue;
1337 		}
1338 
1339 		switch (kcage_assimilate_page(pp, &nfreed)) {
1340 			case 0:		/* assimilated, page is free */
1341 				KCAGE_STAT_NINCR_SCAN(ke_gotonefree, nfreed);
1342 				did_something = 1;
1343 				nf += nfreed;
1344 				n++;
1345 				break;
1346 
1347 			case EBUSY:	/* assimilated, page not free */
1348 			case ERANGE:	/* assimilated, page not root */
1349 				KCAGE_STAT_INCR_SCAN(ke_gotone);
1350 				did_something = 1;
1351 				n++;
1352 				break;
1353 
1354 			case ENOMEM:	/* assimilated, but no mem */
1355 				KCAGE_STAT_INCR(ke_terminate);
1356 				did_something = 1;
1357 				n++;
1358 				goto terminate;
1359 
1360 			case EAGAIN:	/* can't assimilate */
1361 				KCAGE_STAT_INCR_SCAN(ke_lefthole);
1362 				break;
1363 
1364 			default:	/* catch this with debug kernels */
1365 				ASSERT(0);
1366 				break;
1367 		}
1368 	}
1369 
1370 	/*
1371 	 * Realign cage edge with the nearest physical address
1372 	 * boundry for big pages. This is done to give us a
1373 	 * better chance of actually getting usable big pages
1374 	 * in the cage.
1375 	 */
1376 
1377 terminate:
1378 	kcage_range_unlock();
1379 
1380 	return (did_something);
1381 }
1382 
1383 /*
1384  * Relocate page opp (Original Page Pointer) from cage pool to page rpp
1385  * (Replacement Page Pointer) in the global pool. Page opp will be freed
1386  * if relocation is successful, otherwise it is only unlocked.
1387  * On entry, page opp must be exclusively locked and not free.
1388  * *nfreedp: number of pages freed.
1389  */
1390 static int
1391 kcage_relocate_page(page_t *pp, pgcnt_t *nfreedp)
1392 {
1393 	page_t *opp = pp;
1394 	page_t *rpp = NULL;
1395 	spgcnt_t npgs;
1396 	int result;
1397 
1398 	ASSERT(!PP_ISFREE(opp));
1399 	ASSERT(PAGE_EXCL(opp));
1400 
1401 	result = page_relocate(&opp, &rpp, 1, 1, &npgs, NULL);
1402 	*nfreedp = npgs;
1403 	if (result == 0) {
1404 		while (npgs-- > 0) {
1405 			page_t *tpp;
1406 
1407 			ASSERT(rpp != NULL);
1408 			tpp = rpp;
1409 			page_sub(&rpp, tpp);
1410 			page_unlock(tpp);
1411 		}
1412 
1413 		ASSERT(rpp == NULL);
1414 
1415 		return (0);		/* success */
1416 	}
1417 
1418 	page_unlock(opp);
1419 	return (result);
1420 }
1421 
1422 /*
1423  * Based on page_invalidate_pages()
1424  *
1425  * Kcage_invalidate_page() uses page_relocate() twice. Both instances
1426  * of use must be updated to match the new page_relocate() when it
1427  * becomes available.
1428  *
1429  * Return result of kcage_relocate_page or zero if page was directly freed.
1430  * *nfreedp: number of pages freed.
1431  */
1432 static int
1433 kcage_invalidate_page(page_t *pp, pgcnt_t *nfreedp)
1434 {
1435 	int result;
1436 
1437 #if defined(__sparc)
1438 	extern struct vnode prom_ppages;
1439 	ASSERT(pp->p_vnode != &prom_ppages);
1440 #endif /* __sparc */
1441 
1442 	ASSERT(!PP_ISFREE(pp));
1443 	ASSERT(PAGE_EXCL(pp));
1444 
1445 	/*
1446 	 * Is this page involved in some I/O? shared?
1447 	 * The page_struct_lock need not be acquired to
1448 	 * examine these fields since the page has an
1449 	 * "exclusive" lock.
1450 	 */
1451 	if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
1452 		result = kcage_relocate_page(pp, nfreedp);
1453 #ifdef KCAGE_STATS
1454 		if (result == 0)
1455 			KCAGE_STAT_INCR_SCAN(kip_reloclocked);
1456 		else if (result == ENOMEM)
1457 			KCAGE_STAT_INCR_SCAN(kip_nomem);
1458 #endif
1459 		return (result);
1460 	}
1461 
1462 	ASSERT(pp->p_vnode->v_type != VCHR);
1463 
1464 	/*
1465 	 * Unload the mappings and check if mod bit is set.
1466 	 */
1467 	(void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
1468 
1469 	if (hat_ismod(pp)) {
1470 		result = kcage_relocate_page(pp, nfreedp);
1471 #ifdef KCAGE_STATS
1472 		if (result == 0)
1473 			KCAGE_STAT_INCR_SCAN(kip_relocmod);
1474 		else if (result == ENOMEM)
1475 			KCAGE_STAT_INCR_SCAN(kip_nomem);
1476 #endif
1477 		return (result);
1478 	}
1479 
1480 	if (!page_try_demote_pages(pp)) {
1481 		KCAGE_STAT_INCR_SCAN(kip_demotefailed);
1482 		page_unlock(pp);
1483 		return (EAGAIN);
1484 	}
1485 
1486 	page_destroy(pp, 0);
1487 	KCAGE_STAT_INCR_SCAN(kip_destroy);
1488 	*nfreedp = 1;
1489 	return (0);
1490 }
1491 
1492 static void
1493 kcage_cageout()
1494 {
1495 	pfn_t pfn;
1496 	page_t *pp;
1497 	callb_cpr_t cprinfo;
1498 	int did_something;
1499 	int scan_again;
1500 	pfn_t start_pfn;
1501 	int pass;
1502 	int last_pass;
1503 	int pages_skipped;
1504 	int shared_skipped;
1505 	uint_t shared_level = 8;
1506 	pgcnt_t nfreed;
1507 #ifdef KCAGE_STATS
1508 	clock_t scan_start;
1509 #endif
1510 
1511 	CALLB_CPR_INIT(&cprinfo, &kcage_cageout_mutex,
1512 		callb_generic_cpr, "cageout");
1513 
1514 	mutex_enter(&kcage_cageout_mutex);
1515 	kcage_cageout_thread = curthread;
1516 
1517 	pfn = PFN_INVALID;		/* force scan reset */
1518 	start_pfn = PFN_INVALID;	/* force init with 1st cage pfn */
1519 	kcage_cageout_ready = 1;	/* switch kcage_cageout_wakeup mode */
1520 
1521 loop:
1522 	/*
1523 	 * Wait here. Sooner or later, kcage_freemem_sub() will notice
1524 	 * that kcage_freemem is less than kcage_desfree. When it does
1525 	 * notice, kcage_freemem_sub() will wake us up via call to
1526 	 * kcage_cageout_wakeup().
1527 	 */
1528 	CALLB_CPR_SAFE_BEGIN(&cprinfo);
1529 	cv_wait(&kcage_cageout_cv, &kcage_cageout_mutex);
1530 	CALLB_CPR_SAFE_END(&cprinfo, &kcage_cageout_mutex);
1531 
1532 	KCAGE_STAT_INCR(kt_wakeups);
1533 	KCAGE_STAT_SET_SCAN(kt_freemem_start, freemem);
1534 	KCAGE_STAT_SET_SCAN(kt_kcage_freemem_start, kcage_freemem);
1535 	pass = 0;
1536 	last_pass = 0;
1537 
1538 #ifdef KCAGE_STATS
1539 	scan_start = lbolt;
1540 #endif
1541 
1542 again:
1543 	if (!kcage_on)
1544 		goto loop;
1545 
1546 	KCAGE_STAT_INCR(kt_scans);
1547 	KCAGE_STAT_INCR_SCAN(kt_passes);
1548 
1549 	did_something = 0;
1550 	pages_skipped = 0;
1551 	shared_skipped = 0;
1552 	while ((kcage_freemem < kcage_lotsfree || kcage_needfree) &&
1553 		(pfn = kcage_walk_cage(pfn == PFN_INVALID)) != PFN_INVALID) {
1554 
1555 		if (start_pfn == PFN_INVALID)
1556 			start_pfn = pfn;
1557 		else if (start_pfn == pfn) {
1558 			last_pass = pass;
1559 			pass += 1;
1560 			/*
1561 			 * Did a complete walk of kernel cage, but didn't free
1562 			 * any pages.  If only one cpu is online then
1563 			 * stop kernel cage walk and try expanding.
1564 			 */
1565 			if (ncpus_online == 1 && did_something == 0) {
1566 				KCAGE_STAT_INCR(kt_cageout_break);
1567 				break;
1568 			}
1569 		}
1570 
1571 		pp = page_numtopp_nolock(pfn);
1572 		if (pp == NULL) {
1573 			continue;
1574 		}
1575 
1576 		KCAGE_STAT_INCR_SCAN(kt_examined);
1577 
1578 		/*
1579 		 * Do a quick PP_ISNORELOC() and PP_ISFREE test outside
1580 		 * of the lock. If one is missed it will be seen next
1581 		 * time through.
1582 		 *
1583 		 * Skip non-caged-pages. These pages can exist in the cage
1584 		 * because, if during cage expansion, a page is
1585 		 * encountered that is long-term locked the lock prevents the
1586 		 * expansion logic from setting the P_NORELOC flag. Hence,
1587 		 * non-caged-pages surrounded by caged-pages.
1588 		 */
1589 		if (!PP_ISNORELOC(pp)) {
1590 			switch (kcage_assimilate_page(pp, &nfreed)) {
1591 				case 0:
1592 					did_something = 1;
1593 					KCAGE_STAT_NINCR_SCAN(kt_gotonefree,
1594 					    nfreed);
1595 					break;
1596 
1597 				case EBUSY:
1598 				case ERANGE:
1599 					did_something = 1;
1600 					KCAGE_STAT_INCR_SCAN(kt_gotone);
1601 					break;
1602 
1603 				case EAGAIN:
1604 				case ENOMEM:
1605 					break;
1606 
1607 				default:
1608 					/* catch this with debug kernels */
1609 					ASSERT(0);
1610 					break;
1611 			}
1612 
1613 			continue;
1614 		} else {
1615 			int prm;
1616 
1617 			if (PP_ISFREE(pp)) {
1618 				continue;
1619 			}
1620 
1621 			if ((pp->p_vnode == &kvp && pp->p_lckcnt > 0) ||
1622 			    !page_trylock(pp, SE_EXCL)) {
1623 				KCAGE_STAT_INCR_SCAN(kt_cantlock);
1624 				continue;
1625 			}
1626 
1627 			/* P_NORELOC bit should not have gone away. */
1628 			ASSERT(PP_ISNORELOC(pp));
1629 			if (PP_ISFREE(pp) || (pp->p_vnode == &kvp &&
1630 			    pp->p_lckcnt > 0)) {
1631 				page_unlock(pp);
1632 				continue;
1633 			}
1634 
1635 			KCAGE_STAT_SET_SCAN(kt_skiplevel, shared_level);
1636 			if (hat_page_getshare(pp) > shared_level) {
1637 				page_unlock(pp);
1638 				pages_skipped = 1;
1639 				shared_skipped = 1;
1640 				KCAGE_STAT_INCR_SCAN(kt_skipshared);
1641 				continue;
1642 			}
1643 
1644 			/*
1645 			 * In pass {0, 1}, skip page if ref bit is set.
1646 			 * In pass {0, 1, 2}, skip page if mod bit is set.
1647 			 */
1648 			prm = hat_pagesync(pp,
1649 				HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_MOD);
1650 
1651 			/* On first pass ignore ref'd pages */
1652 			if (pass <= 1 && (prm & P_REF)) {
1653 				KCAGE_STAT_INCR_SCAN(kt_skiprefd);
1654 				pages_skipped = 1;
1655 				page_unlock(pp);
1656 				continue;
1657 			}
1658 
1659 			/* On pass 2, page_destroy if mod bit is not set */
1660 			if (pass <= 2) {
1661 				if (pp->p_szc != 0 || (prm & P_MOD) ||
1662 					pp->p_lckcnt || pp->p_cowcnt) {
1663 					pages_skipped = 1;
1664 					page_unlock(pp);
1665 				} else {
1666 
1667 					/*
1668 					 * unload the mappings before
1669 					 * checking if mod bit is set
1670 					 */
1671 					(void) hat_pageunload(pp,
1672 						HAT_FORCE_PGUNLOAD);
1673 
1674 					/*
1675 					 * skip this page if modified
1676 					 */
1677 					if (hat_ismod(pp)) {
1678 						pages_skipped = 1;
1679 						page_unlock(pp);
1680 						continue;
1681 					}
1682 
1683 					KCAGE_STAT_INCR_SCAN(kt_destroy);
1684 					page_destroy(pp, 0);
1685 					did_something = 1;
1686 				}
1687 				continue;
1688 			}
1689 
1690 			if (kcage_invalidate_page(pp, &nfreed) == 0) {
1691 				did_something = 1;
1692 				KCAGE_STAT_NINCR_SCAN(kt_gotonefree, nfreed);
1693 			}
1694 
1695 			/*
1696 			 * No need to drop the page lock here.
1697 			 * Kcage_invalidate_page has done that for us
1698 			 * either explicitly or through a page_free.
1699 			 */
1700 		}
1701 	}
1702 
1703 	/*
1704 	 * Expand the cage only if available cage memory is really low.
1705 	 * This test is done only after a complete scan of the cage.
1706 	 * The reason for not checking and expanding more often is to
1707 	 * avoid rapid expansion of the cage. Naturally, scanning the
1708 	 * cage takes time. So by scanning first, we use that work as a
1709 	 * delay loop in between expand decisions.
1710 	 */
1711 
1712 	scan_again = 0;
1713 	if (kcage_freemem < kcage_minfree || kcage_needfree) {
1714 		/*
1715 		 * Kcage_expand() will return a non-zero value if it was
1716 		 * able to expand the cage -- whether or not the new
1717 		 * pages are free and immediately usable. If non-zero,
1718 		 * we do another scan of the cage. The pages might be
1719 		 * freed during that scan or by time we get back here.
1720 		 * If not, we will attempt another expansion.
1721 		 * However, if kcage_expand() returns zero, then it was
1722 		 * unable to expand the cage. This is the case when the
1723 		 * the growth list is exausted, therefore no work was done
1724 		 * and there is no reason to scan the cage again.
1725 		 * Note: Kernel cage scan is not repeated on single-cpu
1726 		 * system to avoid kernel cage thread hogging cpu.
1727 		 */
1728 		if (pass <= 3 && pages_skipped && ncpus_online > 1)
1729 			scan_again = 1;
1730 		else
1731 			(void) kcage_expand(); /* don't scan again */
1732 	} else if (kcage_freemem < kcage_lotsfree) {
1733 		/*
1734 		 * If available cage memory is less than abundant
1735 		 * and a full scan of the cage has not yet been completed,
1736 		 * or a scan has completed and some work was performed,
1737 		 * or pages were skipped because of sharing,
1738 		 * or we simply have not yet completed two passes,
1739 		 * then do another scan.
1740 		 */
1741 		if (pass <= 2 && pages_skipped)
1742 			scan_again = 1;
1743 		if (pass == last_pass || did_something)
1744 			scan_again = 1;
1745 		else if (shared_skipped && shared_level < (8<<24)) {
1746 			shared_level <<= 1;
1747 			scan_again = 1;
1748 		}
1749 	}
1750 
1751 	if (scan_again && ncpus_online > 1)
1752 		goto again;
1753 	else {
1754 		if (shared_level > 8)
1755 			shared_level >>= 1;
1756 
1757 		KCAGE_STAT_SET_SCAN(kt_freemem_end, freemem);
1758 		KCAGE_STAT_SET_SCAN(kt_kcage_freemem_end, kcage_freemem);
1759 		KCAGE_STAT_SET_SCAN(kt_ticks, lbolt - scan_start);
1760 		KCAGE_STAT_INC_SCAN_INDEX;
1761 		goto loop;
1762 	}
1763 
1764 	/*NOTREACHED*/
1765 }
1766 
1767 void
1768 kcage_cageout_wakeup()
1769 {
1770 	if (mutex_tryenter(&kcage_cageout_mutex)) {
1771 		if (kcage_cageout_ready) {
1772 			cv_signal(&kcage_cageout_cv);
1773 		} else if (kcage_freemem < kcage_minfree || kcage_needfree) {
1774 			/*
1775 			 * Available cage memory is really low. Time to
1776 			 * start expanding the cage. However, the
1777 			 * kernel cage thread is not yet ready to
1778 			 * do the work. Use *this* thread, which is
1779 			 * most likely to be t0, to do the work.
1780 			 */
1781 			KCAGE_STAT_INCR(kcw_expandearly);
1782 			(void) kcage_expand();
1783 			KCAGE_STAT_INC_SCAN_INDEX;
1784 		}
1785 
1786 		mutex_exit(&kcage_cageout_mutex);
1787 	}
1788 	/* else, kernel cage thread is already running */
1789 }
1790 
1791 void
1792 kcage_tick()
1793 {
1794 	/*
1795 	 * Once per second we wake up all the threads throttled
1796 	 * waiting for cage memory, in case we've become stuck
1797 	 * and haven't made forward progress expanding the cage.
1798 	 */
1799 	if (kcage_on && kcage_cageout_ready)
1800 		cv_broadcast(&kcage_throttle_cv);
1801 }
1802