1917Selowe /*
2917Selowe  * CDDL HEADER START
3917Selowe  *
4917Selowe  * The contents of this file are subject to the terms of the
5917Selowe  * Common Development and Distribution License, Version 1.0 only
6917Selowe  * (the "License").  You may not use this file except in compliance
7917Selowe  * with the License.
8917Selowe  *
9917Selowe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10917Selowe  * or http://www.opensolaris.org/os/licensing.
11917Selowe  * See the License for the specific language governing permissions
12917Selowe  * and limitations under the License.
13917Selowe  *
14917Selowe  * When distributing Covered Code, include this CDDL HEADER in each
15917Selowe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16917Selowe  * If applicable, add the following below this CDDL HEADER, with the
17917Selowe  * fields enclosed by brackets "[]" replaced with your own identifying
18917Selowe  * information: Portions Copyright [yyyy] [name of copyright owner]
19917Selowe  *
20917Selowe  * CDDL HEADER END
21917Selowe  */
22917Selowe /*
23917Selowe  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24917Selowe  * Use is subject to license terms.
25917Selowe  */
26917Selowe 
27917Selowe #pragma ident	"%Z%%M%	%I%	%E% SMI"
28917Selowe 
29917Selowe /*
30917Selowe  * Page Retire - Big Theory Statement.
31917Selowe  *
32917Selowe  * This file handles removing sections of faulty memory from use when the
33917Selowe  * user land FMA Diagnosis Engine requests that a page be removed or when
34917Selowe  * a CE or UE is detected by the hardware.
35917Selowe  *
36917Selowe  * In the bad old days, the kernel side of Page Retire did a lot of the work
37917Selowe  * on its own. Now, with the DE keeping track of errors, the kernel side is
38917Selowe  * rather simple minded on most platforms.
39917Selowe  *
40917Selowe  * Errors are all reflected to the DE, and after digesting the error and
41917Selowe  * looking at all previously reported errors, the DE decides what should
42917Selowe  * be done about the current error. If the DE wants a particular page to
43917Selowe  * be retired, then the kernel page retire code is invoked via an ioctl.
44917Selowe  * On non-FMA platforms, the ue_drain and ce_drain paths ends up calling
45917Selowe  * page retire to handle the error. Since page retire is just a simple
46917Selowe  * mechanism it doesn't need to differentiate between the different callers.
47917Selowe  *
48917Selowe  * The p_toxic field in the page_t is used to indicate which errors have
49917Selowe  * occurred and what action has been taken on a given page. Because errors are
50917Selowe  * reported without regard to the locked state of a page, no locks are used
51917Selowe  * to SET the error bits in p_toxic. However, in order to clear the error
52917Selowe  * bits, the page_t must be held exclusively locked.
53917Selowe  *
54917Selowe  * When page_retire() is called, it must be able to acquire locks, sleep, etc.
55917Selowe  * It must not be called from high-level interrupt context.
56917Selowe  *
57917Selowe  * Depending on how the requested page is being used at the time of the retire
58917Selowe  * request (and on the availability of sufficient system resources), the page
59917Selowe  * may be retired immediately, or just marked for retirement later. For
60917Selowe  * example, locked pages are marked, while free pages are retired. Multiple
61917Selowe  * requests may be made to retire the same page, although there is no need
62917Selowe  * to: once the p_toxic flags are set, the page will be retired as soon as it
63917Selowe  * can be exclusively locked.
64917Selowe  *
65917Selowe  * The retire mechanism is driven centrally out of page_unlock(). To expedite
66917Selowe  * the retirement of pages, further requests for SE_SHARED locks are denied
67917Selowe  * as long as a page retirement is pending. In addition, as long as pages are
68917Selowe  * pending retirement a background thread runs periodically trying to retire
69917Selowe  * those pages. Pages which could not be retired while the system is running
70917Selowe  * are scrubbed prior to rebooting to avoid latent errors on the next boot.
71917Selowe  *
72917Selowe  * Single CE pages and UE pages without persistent errors are scrubbed and
73917Selowe  * returned to service. Recidivist pages, as well as FMA-directed requests
74917Selowe  * for retirement, result in the page being taken out of service. Once the
75917Selowe  * decision is made to take a page out of service, the page is cleared, hashed
76917Selowe  * onto the retired_pages vnode, marked as retired, and it is unlocked.  No
77917Selowe  * other requesters (except for unretire) are allowed to lock retired pages.
78917Selowe  *
79917Selowe  * The public routines return (sadly) 0 if they worked and a non-zero error
80917Selowe  * value if something went wrong. This is done for the ioctl side of the
81917Selowe  * world to allow errors to be reflected all the way out to user land. The
82917Selowe  * non-zero values are explained in comments atop each function.
83917Selowe  */
84917Selowe 
85917Selowe /*
86917Selowe  * Things to fix:
87917Selowe  *
88917Selowe  * 	1. Cleanup SE_EWANTED.  Since we're aggressive about trying to retire
89917Selowe  *	pages, we can use page_retire_pp() to replace SE_EWANTED and all
90917Selowe  *	the special delete_memory_thread() code just goes away.
91917Selowe  *
92917Selowe  * 	2. Trying to retire non-relocatable kvp pages may result in a
93917Selowe  *      quagmire. This is because seg_kmem() no longer keeps its pages locked,
94917Selowe  *      and calls page_lookup() in the free path; since kvp pages are modified
95917Selowe  *      and don't have a usable backing store, page_retire() can't do anything
96917Selowe  *      with them, and we'll keep denying the lock to seg_kmem_free() in a
97917Selowe  *      vicious cycle. To prevent that, we don't deny locks to kvp pages, and
98917Selowe  *      hence only call page_retire_pp() from page_unlock() in the free path.
99917Selowe  *      Since most kernel pages are indefinitely held anyway, and don't
100917Selowe  *      participate in I/O, this is of little consequence.
101917Selowe  *
102917Selowe  *      3. Low memory situations will be interesting. If we don't have
103917Selowe  *      enough memory for page_relocate() to succeed, we won't be able to
104917Selowe  *      retire dirty pages; nobody will be able to push them out to disk
105917Selowe  *      either, since we aggressively deny the page lock. We could change
106917Selowe  *      fsflush so it can recognize this situation, grab the lock, and push
107917Selowe  *      the page out, where we'll catch it in the free path and retire it.
108917Selowe  *
109917Selowe  *	4. Beware of places that have code like this in them:
110917Selowe  *
111917Selowe  *		if (! page_tryupgrade(pp)) {
112917Selowe  *			page_unlock(pp);
113917Selowe  *			while (! page_lock(pp, SE_EXCL, NULL, P_RECLAIM)) {
114917Selowe  *				/ *NOTHING* /
115917Selowe  *			}
116917Selowe  *		}
117917Selowe  *		page_free(pp);
118917Selowe  *
119917Selowe  *	The problem is that pp can change identity right after the
120917Selowe  *	page_unlock() call.  In particular, page_retire() can step in
121917Selowe  *	there, change pp's identity, and hash pp onto the retired_vnode.
122917Selowe  *
123917Selowe  *	Of course, other functions besides page_retire() can have the
124917Selowe  *	same effect. A kmem reader can waltz by, set up a mapping to the
125917Selowe  *	page, and then unlock the page. Page_free() will then go castors
126917Selowe  *	up. So if anybody is doing this, it's already a bug.
127917Selowe  *
128917Selowe  *      5. mdboot()'s call into page_retire_hunt() should probably be
129917Selowe  *      moved lower. Where the call is made now, we can get into trouble
130917Selowe  *      by scrubbing a kernel page that is then accessed later.
131917Selowe  */
132917Selowe 
133917Selowe #include <sys/types.h>
134917Selowe #include <sys/param.h>
135917Selowe #include <sys/systm.h>
136917Selowe #include <sys/mman.h>
137917Selowe #include <sys/vnode.h>
138917Selowe #include <sys/cmn_err.h>
139917Selowe #include <sys/ksynch.h>
140917Selowe #include <sys/thread.h>
141917Selowe #include <sys/disp.h>
142917Selowe #include <sys/ontrap.h>
143917Selowe #include <sys/vmsystm.h>
144917Selowe #include <sys/mem_config.h>
145917Selowe #include <sys/atomic.h>
146917Selowe #include <sys/callb.h>
147917Selowe #include <vm/page.h>
148917Selowe #include <vm/vm_dep.h>
149917Selowe #include <vm/as.h>
150917Selowe #include <vm/hat.h>
151917Selowe 
152917Selowe /*
153917Selowe  * vnode for all pages which are retired from the VM system;
154917Selowe  */
155917Selowe vnode_t *retired_pages;
156917Selowe 
157917Selowe /*
158917Selowe  * Background thread that wakes up periodically to try to retire pending
159917Selowe  * pages. This prevents threads from becoming blocked indefinitely in
160917Selowe  * page_lookup() or some other routine should the page(s) they are waiting
161917Selowe  * on become eligible for social security.
162917Selowe  */
163917Selowe static void page_retire_thread(void);
164917Selowe static kthread_t *pr_thread_id;
165917Selowe static kcondvar_t pr_cv;
166917Selowe static kmutex_t pr_thread_mutex;
167917Selowe static clock_t pr_thread_shortwait;
168917Selowe static clock_t pr_thread_longwait;
169917Selowe 
170917Selowe /*
171917Selowe  * Make a list of all of the pages that have been marked for retirement
172917Selowe  * but are not yet retired.  At system shutdown, we will scrub all of the
173917Selowe  * pages in the list in case there are outstanding UEs.  Then, we
174917Selowe  * cross-check this list against the number of pages that are yet to be
175917Selowe  * retired, and if we find inconsistencies, we scan every page_t in the
176917Selowe  * whole system looking for any pages that need to be scrubbed for UEs.
177917Selowe  * The background thread also uses this queue to determine which pages
178917Selowe  * it should keep trying to retire.
179917Selowe  */
180917Selowe #ifdef	DEBUG
181917Selowe #define	PR_PENDING_QMAX	32
182917Selowe #else	/* DEBUG */
183917Selowe #define	PR_PENDING_QMAX	256
184917Selowe #endif	/* DEBUG */
185917Selowe page_t		*pr_pending_q[PR_PENDING_QMAX];
186917Selowe kmutex_t	pr_q_mutex;
187917Selowe 
188917Selowe /*
189917Selowe  * Page retire global kstats
190917Selowe  */
191917Selowe struct page_retire_kstat {
192917Selowe 	kstat_named_t	pr_retired;
193917Selowe 	kstat_named_t	pr_requested;
194917Selowe 	kstat_named_t	pr_requested_free;
195917Selowe 	kstat_named_t	pr_enqueue_fail;
196917Selowe 	kstat_named_t	pr_dequeue_fail;
197917Selowe 	kstat_named_t	pr_pending;
198917Selowe 	kstat_named_t	pr_failed;
199917Selowe 	kstat_named_t	pr_failed_kernel;
200917Selowe 	kstat_named_t	pr_limit;
201917Selowe 	kstat_named_t	pr_limit_exceeded;
202917Selowe 	kstat_named_t	pr_fma;
203917Selowe 	kstat_named_t	pr_mce;
204917Selowe 	kstat_named_t	pr_ue;
205917Selowe 	kstat_named_t	pr_ue_cleared_retire;
206917Selowe 	kstat_named_t	pr_ue_cleared_free;
207917Selowe 	kstat_named_t	pr_ue_persistent;
208917Selowe 	kstat_named_t	pr_unretired;
209917Selowe };
210917Selowe 
211917Selowe static struct page_retire_kstat page_retire_kstat = {
212917Selowe 	{ "pages_retired",		KSTAT_DATA_UINT64},
213917Selowe 	{ "pages_retire_request",	KSTAT_DATA_UINT64},
214917Selowe 	{ "pages_retire_request_free",	KSTAT_DATA_UINT64},
215917Selowe 	{ "pages_notenqueued", 		KSTAT_DATA_UINT64},
216917Selowe 	{ "pages_notdequeued", 		KSTAT_DATA_UINT64},
217917Selowe 	{ "pages_pending", 		KSTAT_DATA_UINT64},
218917Selowe 	{ "pages_deferred",		KSTAT_DATA_UINT64},
219917Selowe 	{ "pages_deferred_kernel",	KSTAT_DATA_UINT64},
220917Selowe 	{ "pages_limit",		KSTAT_DATA_UINT64},
221917Selowe 	{ "pages_limit_exceeded",	KSTAT_DATA_UINT64},
222917Selowe 	{ "pages_fma",			KSTAT_DATA_UINT64},
223917Selowe 	{ "pages_multiple_ce",		KSTAT_DATA_UINT64},
224917Selowe 	{ "pages_ue",			KSTAT_DATA_UINT64},
225917Selowe 	{ "pages_ue_cleared_retired",	KSTAT_DATA_UINT64},
226917Selowe 	{ "pages_ue_cleared_freed",	KSTAT_DATA_UINT64},
227917Selowe 	{ "pages_ue_persistent",	KSTAT_DATA_UINT64},
228917Selowe 	{ "pages_unretired",		KSTAT_DATA_UINT64},
229917Selowe };
230917Selowe 
231917Selowe static kstat_t  *page_retire_ksp = NULL;
232917Selowe 
233917Selowe #define	PR_INCR_KSTAT(stat)	\
234917Selowe 	atomic_add_64(&(page_retire_kstat.stat.value.ui64), 1)
235917Selowe #define	PR_DECR_KSTAT(stat)	\
236917Selowe 	atomic_add_64(&(page_retire_kstat.stat.value.ui64), -1)
237917Selowe 
238917Selowe #define	PR_KSTAT_RETIRED_CE	(page_retire_kstat.pr_mce.value.ui64)
239917Selowe #define	PR_KSTAT_RETIRED_FMA	(page_retire_kstat.pr_fma.value.ui64)
240917Selowe #define	PR_KSTAT_RETIRED_NOTUE	(PR_KSTAT_RETIRED_CE + PR_KSTAT_RETIRED_FMA)
241917Selowe #define	PR_KSTAT_PENDING	(page_retire_kstat.pr_pending.value.ui64)
242917Selowe #define	PR_KSTAT_EQFAIL		(page_retire_kstat.pr_enqueue_fail.value.ui64)
243917Selowe #define	PR_KSTAT_DQFAIL		(page_retire_kstat.pr_dequeue_fail.value.ui64)
244917Selowe 
245917Selowe /*
246917Selowe  * Limit the number of multiple CE page retires.
247917Selowe  * The default is 0.1% of physmem, or 1 in 1000 pages. This is set in
248917Selowe  * basis points, where 100 basis points equals one percent.
249917Selowe  */
250917Selowe #define	MCE_BPT	10
251917Selowe uint64_t	max_pages_retired_bps = MCE_BPT;
252917Selowe #define	PAGE_RETIRE_LIMIT	((physmem * max_pages_retired_bps) / 10000)
253917Selowe 
254917Selowe /*
255917Selowe  * Control over the verbosity of page retirement.
256917Selowe  *
257917Selowe  * When set to zero (the default), no messages will be printed.
258917Selowe  * When set to one, summary messages will be printed.
259917Selowe  * When set > one, all messages will be printed.
260917Selowe  *
261917Selowe  * A value of one will trigger detailed messages for retirement operations,
262917Selowe  * and is intended as a platform tunable for processors where FMA's DE does
263917Selowe  * not run (e.g., spitfire). Values > one are intended for debugging only.
264917Selowe  */
265917Selowe int page_retire_messages = 0;
266917Selowe 
267917Selowe /*
268917Selowe  * Control whether or not we return scrubbed UE pages to service.
269917Selowe  * By default we do not since FMA wants to run its diagnostics first
270917Selowe  * and then ask us to unretire the page if it passes. Non-FMA platforms
271917Selowe  * may set this to zero so we will only retire recidivist pages. It should
272917Selowe  * not be changed by the user.
273917Selowe  */
274917Selowe int page_retire_first_ue = 1;
275917Selowe 
276917Selowe /*
277917Selowe  * Master enable for page retire. This prevents a CE or UE early in boot
278917Selowe  * from trying to retire a page before page_retire_init() has finished
279917Selowe  * setting things up. This is internal only and is not a tunable!
280917Selowe  */
281917Selowe static int pr_enable = 0;
282917Selowe 
283917Selowe extern struct vnode kvp;
284917Selowe 
285917Selowe #ifdef	DEBUG
286917Selowe struct page_retire_debug {
287917Selowe 	int prd_dup;
288917Selowe 	int prd_noaction;
289917Selowe 	int prd_queued;
290917Selowe 	int prd_notqueued;
291917Selowe 	int prd_dequeue;
292917Selowe 	int prd_top;
293917Selowe 	int prd_locked;
294917Selowe 	int prd_reloc;
295*973Selowe 	int prd_relocfail;
296*973Selowe 	int prd_mod;
297*973Selowe 	int prd_mod_late;
298917Selowe 	int prd_kern;
299917Selowe 	int prd_free;
300917Selowe 	int prd_noreclaim;
301917Selowe 	int prd_hashout;
302917Selowe 	int prd_fma;
303917Selowe 	int prd_uescrubbed;
304917Selowe 	int prd_uenotscrubbed;
305917Selowe 	int prd_mce;
306917Selowe 	int prd_prlocked;
307917Selowe 	int prd_prnotlocked;
308917Selowe 	int prd_prretired;
309917Selowe 	int prd_ulocked;
310917Selowe 	int prd_unotretired;
311917Selowe 	int prd_udestroy;
312917Selowe 	int prd_uhashout;
313917Selowe 	int prd_uunretired;
314917Selowe 	int prd_unotlocked;
315917Selowe 	int prd_checkhit;
316917Selowe 	int prd_checkmiss;
317917Selowe 	int prd_tctop;
318917Selowe 	int prd_tclocked;
319917Selowe 	int prd_hunt;
320917Selowe 	int prd_dohunt;
321917Selowe 	int prd_earlyhunt;
322917Selowe 	int prd_latehunt;
323917Selowe 	int prd_nofreedemote;
324917Selowe 	int prd_nodemote;
325917Selowe 	int prd_demoted;
326917Selowe } pr_debug;
327917Selowe 
328917Selowe #define	PR_DEBUG(foo)	((pr_debug.foo)++)
329917Selowe 
330917Selowe /*
331917Selowe  * A type histogram. We record the incidence of the various toxic
332917Selowe  * flag combinations along with the interesting page attributes. The
333917Selowe  * goal is to get as many combinations as we can while driving all
334917Selowe  * pr_debug values nonzero (indicating we've exercised all possible
335917Selowe  * code paths across all possible page types). Not all combinations
336917Selowe  * will make sense -- e.g. PRT_MOD|PRT_KERNEL.
337917Selowe  *
338917Selowe  * pr_type offset bit encoding (when examining with a debugger):
339917Selowe  *
340917Selowe  *    PRT_NAMED  - 0x4
341917Selowe  *    PRT_KERNEL - 0x8
342917Selowe  *    PRT_FREE   - 0x10
343917Selowe  *    PRT_MOD    - 0x20
344917Selowe  *    PRT_FMA    - 0x0
345917Selowe  *    PRT_MCE    - 0x40
346917Selowe  *    PRT_UE     - 0x80
347917Selowe  */
348917Selowe 
349917Selowe #define	PRT_NAMED	0x01
350917Selowe #define	PRT_KERNEL	0x02
351917Selowe #define	PRT_FREE	0x04
352917Selowe #define	PRT_MOD		0x08
353917Selowe #define	PRT_FMA		0x00	/* yes, this is not a mistake */
354917Selowe #define	PRT_MCE		0x10
355917Selowe #define	PRT_UE		0x20
356917Selowe #define	PRT_ALL		0x3F
357917Selowe 
358917Selowe int pr_types[PRT_ALL+1];
359917Selowe 
360917Selowe #define	PR_TYPES(pp)	{			\
361917Selowe 	int whichtype = 0;			\
362917Selowe 	if (pp->p_vnode)			\
363917Selowe 		whichtype |= PRT_NAMED;		\
364*973Selowe 	if (PP_ISKVP(pp))			\
365917Selowe 		whichtype |= PRT_KERNEL;	\
366917Selowe 	if (PP_ISFREE(pp))			\
367917Selowe 		whichtype |= PRT_FREE;		\
368917Selowe 	if (hat_ismod(pp))			\
369917Selowe 		whichtype |= PRT_MOD;		\
370917Selowe 	if (pp->p_toxic & PR_UE)		\
371917Selowe 		whichtype |= PRT_UE;		\
372917Selowe 	if (pp->p_toxic & PR_MCE)		\
373917Selowe 		whichtype |= PRT_MCE;		\
374917Selowe 	pr_types[whichtype]++;			\
375917Selowe }
376917Selowe 
377917Selowe int recl_calls;
378917Selowe int recl_mtbf = 3;
379917Selowe int reloc_calls;
380917Selowe int reloc_mtbf = 7;
381917Selowe int pr_calls;
382917Selowe int pr_mtbf = 15;
383917Selowe 
384917Selowe #define	MTBF(v, f)	(((++(v)) & (f)) != (f))
385917Selowe 
386917Selowe #else	/* DEBUG */
387917Selowe 
388917Selowe #define	PR_DEBUG(foo)	/* nothing */
389917Selowe #define	PR_TYPES(foo)	/* nothing */
390917Selowe #define	MTBF(v, f)	(1)
391917Selowe 
392917Selowe #endif	/* DEBUG */
393917Selowe 
394917Selowe /*
395917Selowe  * page_retire_done() - completion processing
396917Selowe  *
397917Selowe  * Used by the page_retire code for common completion processing.
398917Selowe  * It keeps track of how many times a given result has happened,
399917Selowe  * and writes out an occasional message.
400917Selowe  *
401917Selowe  * May be called with a NULL pp (PRD_INVALID_PA case).
402917Selowe  */
403917Selowe #define	PRD_INVALID_KEY		-1
404917Selowe #define	PRD_SUCCESS		0
405917Selowe #define	PRD_PENDING		1
406917Selowe #define	PRD_FAILED		2
407917Selowe #define	PRD_DUPLICATE		3
408917Selowe #define	PRD_INVALID_PA		4
409917Selowe #define	PRD_LIMIT		5
410917Selowe #define	PRD_UE_SCRUBBED		6
411917Selowe #define	PRD_UNR_SUCCESS		7
412917Selowe #define	PRD_UNR_CANTLOCK	8
413917Selowe #define	PRD_UNR_NOT		9
414917Selowe 
415917Selowe typedef struct page_retire_op {
416917Selowe 	int	pr_key;		/* one of the PRD_* defines from above */
417917Selowe 	int	pr_count;	/* How many times this has happened */
418917Selowe 	int	pr_retval;	/* return value */
419917Selowe 	int	pr_msglvl;	/* message level - when to print */
420917Selowe 	char	*pr_message;	/* Cryptic message for field service */
421917Selowe } page_retire_op_t;
422917Selowe 
423917Selowe static page_retire_op_t page_retire_ops[] = {
424917Selowe 	/* key			count	retval	msglvl	message */
425917Selowe 	{PRD_SUCCESS,		0,	0,	1,
426917Selowe 		"Page 0x%08x.%08x removed from service"},
427917Selowe 	{PRD_PENDING,		0,	EAGAIN,	2,
428917Selowe 		"Page 0x%08x.%08x will be retired on free"},
429917Selowe 	{PRD_FAILED,		0,	EAGAIN,	0, NULL},
430917Selowe 	{PRD_DUPLICATE,		0,	EBUSY,	2,
431917Selowe 		"Page 0x%08x.%08x already retired"},
432917Selowe 	{PRD_INVALID_PA,	0,	EINVAL, 2,
433917Selowe 		"PA 0x%08x.%08x is not a relocatable page"},
434917Selowe 	{PRD_LIMIT,		0,	0,	1,
435917Selowe 		"Page 0x%08x.%08x not retired due to limit exceeded"},
436917Selowe 	{PRD_UE_SCRUBBED,	0,	0,	1,
437917Selowe 		"Previously reported error on page 0x%08x.%08x cleared"},
438917Selowe 	{PRD_UNR_SUCCESS,	0,	0,	1,
439917Selowe 		"Page 0x%08x.%08x returned to service"},
440917Selowe 	{PRD_UNR_CANTLOCK,	0,	EAGAIN,	2,
441917Selowe 		"Page 0x%08x.%08x could not be unretired"},
442917Selowe 	{PRD_UNR_NOT,		0,	EBADF,	2,
443917Selowe 		"Page 0x%08x.%08x is not retired"},
444917Selowe 	{PRD_INVALID_KEY,	0,	0,	0, NULL} /* MUST BE LAST! */
445917Selowe };
446917Selowe 
447917Selowe /*
448917Selowe  * print a message if page_retire_messages is true.
449917Selowe  */
450917Selowe #define	PR_MESSAGE(debuglvl, msglvl, msg, pa)				\
451917Selowe {									\
452917Selowe 	uint64_t p = (uint64_t)pa;					\
453917Selowe 	if (page_retire_messages >= msglvl && msg != NULL) {		\
454917Selowe 		cmn_err(debuglvl, msg,					\
455917Selowe 		    (uint32_t)(p >> 32), (uint32_t)p);			\
456917Selowe 	}								\
457917Selowe }
458917Selowe 
459917Selowe /*
460917Selowe  * Note that multiple bits may be set in a single settoxic operation.
461917Selowe  * May be called without the page locked.
462917Selowe  */
463917Selowe void
464917Selowe page_settoxic(page_t *pp, uchar_t bits)
465917Selowe {
466917Selowe 	atomic_or_8(&pp->p_toxic, bits);
467917Selowe }
468917Selowe 
469917Selowe /*
470917Selowe  * Note that multiple bits may cleared in a single clrtoxic operation.
471917Selowe  * Must be called with the page exclusively locked.
472917Selowe  */
473917Selowe void
474917Selowe page_clrtoxic(page_t *pp, uchar_t bits)
475917Selowe {
476917Selowe 	ASSERT(PAGE_EXCL(pp));
477917Selowe 	atomic_and_8(&pp->p_toxic, ~bits);
478917Selowe }
479917Selowe 
480917Selowe /*
481917Selowe  * Prints any page retire messages to the user, and decides what
482917Selowe  * error code is appropriate for the condition reported.
483917Selowe  */
484917Selowe static int
485917Selowe page_retire_done(page_t *pp, int code)
486917Selowe {
487917Selowe 	page_retire_op_t *prop;
488917Selowe 	uint64_t	pa = 0;
489917Selowe 	int		i;
490917Selowe 
491917Selowe 	if (pp != NULL) {
492917Selowe 		pa = mmu_ptob(pp->p_pagenum);
493917Selowe 	}
494917Selowe 
495917Selowe 	prop = NULL;
496917Selowe 	for (i = 0; page_retire_ops[i].pr_key != PRD_INVALID_KEY; i++) {
497917Selowe 		if (page_retire_ops[i].pr_key == code) {
498917Selowe 			prop = &page_retire_ops[i];
499917Selowe 			break;
500917Selowe 		}
501917Selowe 	}
502917Selowe 
503917Selowe #ifdef	DEBUG
504917Selowe 	if (page_retire_ops[i].pr_key == PRD_INVALID_KEY) {
505917Selowe 		cmn_err(CE_PANIC, "page_retire_done: Invalid opcode %d", code);
506917Selowe 	}
507917Selowe #endif
508917Selowe 
509917Selowe 	ASSERT(prop->pr_key == code);
510917Selowe 
511917Selowe 	prop->pr_count++;
512917Selowe 
513917Selowe 	PR_MESSAGE(CE_NOTE, prop->pr_msglvl, prop->pr_message, pa);
514917Selowe 	if (pp != NULL) {
515917Selowe 		page_settoxic(pp, PR_MSG);
516917Selowe 	}
517917Selowe 
518917Selowe 	return (prop->pr_retval);
519917Selowe }
520917Selowe 
521917Selowe /*
522917Selowe  * On a reboot, our friend mdboot() wants to clear up any PP_PR_REQ() pages
523917Selowe  * that we were not able to retire. On large machines, walking the complete
524917Selowe  * page_t array and looking at every page_t takes too long. So, as a page is
525917Selowe  * marked toxic, we track it using a list that can be processed at reboot
526917Selowe  * time.  page_retire_enqueue() will do its best to try to avoid duplicate
527917Selowe  * entries, but if we get too many errors at once the queue can overflow,
528917Selowe  * in which case we will end up walking every page_t as a last resort.
529917Selowe  * The background thread also makes use of this queue to find which pages
530917Selowe  * are pending retirement.
531917Selowe  */
532917Selowe static void
533917Selowe page_retire_enqueue(page_t *pp)
534917Selowe {
535917Selowe 	int	nslot = -1;
536917Selowe 	int	i;
537917Selowe 
538917Selowe 	mutex_enter(&pr_q_mutex);
539917Selowe 
540917Selowe 	/*
541917Selowe 	 * Check to make sure retire hasn't already dequeued it.
542917Selowe 	 * In the meantime if the page was cleaned up, no need
543917Selowe 	 * to enqueue it.
544917Selowe 	 */
545917Selowe 	if (PP_RETIRED(pp) || pp->p_toxic == 0) {
546917Selowe 		mutex_exit(&pr_q_mutex);
547917Selowe 		PR_DEBUG(prd_noaction);
548917Selowe 		return;
549917Selowe 	}
550917Selowe 
551917Selowe 	for (i = 0; i < PR_PENDING_QMAX; i++) {
552917Selowe 		if (pr_pending_q[i] == pp) {
553917Selowe 			mutex_exit(&pr_q_mutex);
554917Selowe 			PR_DEBUG(prd_dup);
555917Selowe 			return;
556917Selowe 		} else if (nslot == -1 && pr_pending_q[i] == NULL) {
557917Selowe 			nslot = i;
558917Selowe 		}
559917Selowe 	}
560917Selowe 
561917Selowe 	PR_INCR_KSTAT(pr_pending);
562917Selowe 
563917Selowe 	if (nslot != -1) {
564917Selowe 		pr_pending_q[nslot] = pp;
565917Selowe 		PR_DEBUG(prd_queued);
566917Selowe 	} else {
567917Selowe 		PR_INCR_KSTAT(pr_enqueue_fail);
568917Selowe 		PR_DEBUG(prd_notqueued);
569917Selowe 	}
570917Selowe 	mutex_exit(&pr_q_mutex);
571917Selowe }
572917Selowe 
573917Selowe static void
574917Selowe page_retire_dequeue(page_t *pp)
575917Selowe {
576917Selowe 	int i;
577917Selowe 
578917Selowe 	mutex_enter(&pr_q_mutex);
579917Selowe 
580917Selowe 	for (i = 0; i < PR_PENDING_QMAX; i++) {
581917Selowe 		if (pr_pending_q[i] == pp) {
582917Selowe 			pr_pending_q[i] = NULL;
583917Selowe 			break;
584917Selowe 		}
585917Selowe 	}
586917Selowe 
587917Selowe 	if (i == PR_PENDING_QMAX) {
588917Selowe 		PR_INCR_KSTAT(pr_dequeue_fail);
589917Selowe 	}
590917Selowe 
591917Selowe 	PR_DECR_KSTAT(pr_pending);
592917Selowe 	PR_DEBUG(prd_dequeue);
593917Selowe 
594917Selowe 	mutex_exit(&pr_q_mutex);
595917Selowe }
596917Selowe 
597917Selowe /*
598917Selowe  * Act like page_destroy(), but instead of freeing the page, hash it onto
599917Selowe  * the retired_pages vnode, and mark it retired.
600917Selowe  *
601917Selowe  * For fun, we try to scrub the page until it's squeaky clean.
602917Selowe  * availrmem is adjusted here.
603917Selowe  */
604917Selowe static void
605917Selowe page_retire_destroy(page_t *pp)
606917Selowe {
607*973Selowe 	u_offset_t off = (u_offset_t)((uintptr_t)pp);
608*973Selowe 
609917Selowe 	ASSERT(PAGE_EXCL(pp));
610917Selowe 	ASSERT(!PP_ISFREE(pp));
611917Selowe 	ASSERT(pp->p_szc == 0);
612917Selowe 	ASSERT(!hat_page_is_mapped(pp));
613917Selowe 	ASSERT(!pp->p_vnode);
614917Selowe 
615917Selowe 	page_clr_all_props(pp);
616917Selowe 	pagescrub(pp, 0, MMU_PAGESIZE);
617917Selowe 
618917Selowe 	pp->p_next = NULL;
619917Selowe 	pp->p_prev = NULL;
620*973Selowe 	if (page_hashin(pp, retired_pages, off, NULL) == 0) {
621917Selowe 		cmn_err(CE_PANIC, "retired page %p hashin failed", (void *)pp);
622917Selowe 	}
623917Selowe 
624917Selowe 	page_settoxic(pp, PR_RETIRED);
625917Selowe 	page_clrtoxic(pp, PR_BUSY);
626917Selowe 	page_retire_dequeue(pp);
627917Selowe 	PR_INCR_KSTAT(pr_retired);
628917Selowe 
629917Selowe 	if (pp->p_toxic & PR_FMA) {
630917Selowe 		PR_INCR_KSTAT(pr_fma);
631917Selowe 	} else if (pp->p_toxic & PR_UE) {
632917Selowe 		PR_INCR_KSTAT(pr_ue);
633917Selowe 	} else {
634917Selowe 		PR_INCR_KSTAT(pr_mce);
635917Selowe 	}
636917Selowe 
637917Selowe 	mutex_enter(&freemem_lock);
638917Selowe 	availrmem--;
639917Selowe 	mutex_exit(&freemem_lock);
640917Selowe 
641917Selowe 	page_unlock(pp);
642917Selowe }
643917Selowe 
644917Selowe /*
645917Selowe  * Check whether the number of pages which have been retired already exceeds
646917Selowe  * the maximum allowable percentage of memory which may be retired.
647917Selowe  *
648917Selowe  * Returns 1 if the limit has been exceeded.
649917Selowe  */
650917Selowe static int
651917Selowe page_retire_limit(void)
652917Selowe {
653917Selowe 	if (PR_KSTAT_RETIRED_NOTUE >= (uint64_t)PAGE_RETIRE_LIMIT) {
654917Selowe 		PR_INCR_KSTAT(pr_limit_exceeded);
655917Selowe 		return (1);
656917Selowe 	}
657917Selowe 
658917Selowe 	return (0);
659917Selowe }
660917Selowe 
661917Selowe #define	MSG_DM	"Data Mismatch occurred at PA 0x%08x.%08x"		\
662917Selowe 	"[ 0x%x != 0x%x ] while attempting to clear previously "	\
663917Selowe 	"reported error; page removed from service"
664917Selowe 
665917Selowe #define	MSG_UE	"Uncorrectable Error occurred at PA 0x%08x.%08x while "	\
666917Selowe 	"attempting to clear previously reported error; page removed "	\
667917Selowe 	"from service"
668917Selowe 
669917Selowe /*
670917Selowe  * Attempt to clear a UE from a page.
671917Selowe  * Returns 1 if the error has been successfully cleared.
672917Selowe  */
673917Selowe static int
674917Selowe page_clear_transient_ue(page_t *pp)
675917Selowe {
676917Selowe 	caddr_t		kaddr;
677917Selowe 	uint8_t		rb, wb;
678917Selowe 	uint64_t	pa;
679917Selowe 	uint32_t	pa_hi, pa_lo;
680917Selowe 	on_trap_data_t	otd;
681917Selowe 	int		errors = 0;
682917Selowe 	int		i;
683917Selowe 
684917Selowe 	ASSERT(PAGE_EXCL(pp));
685917Selowe 	ASSERT(PP_PR_REQ(pp));
686917Selowe 	ASSERT(pp->p_szc == 0);
687917Selowe 	ASSERT(!hat_page_is_mapped(pp));
688917Selowe 
689917Selowe 	/*
690917Selowe 	 * Clear the page and attempt to clear the UE.  If we trap
691917Selowe 	 * on the next access to the page, we know the UE has recurred.
692917Selowe 	 */
693917Selowe 	pagescrub(pp, 0, PAGESIZE);
694917Selowe 
695917Selowe 	/*
696917Selowe 	 * Map the page and write a bunch of bit patterns to compare
697917Selowe 	 * what we wrote with what we read back.  This isn't a perfect
698917Selowe 	 * test but it should be good enough to catch most of the
699917Selowe 	 * recurring UEs. If this fails to catch a recurrent UE, we'll
700917Selowe 	 * retire the page the next time we see a UE on the page.
701917Selowe 	 */
702917Selowe 	kaddr = ppmapin(pp, PROT_READ|PROT_WRITE, (caddr_t)-1);
703917Selowe 
704917Selowe 	pa = ptob((uint64_t)page_pptonum(pp));
705917Selowe 	pa_hi = (uint32_t)(pa >> 32);
706917Selowe 	pa_lo = (uint32_t)pa;
707917Selowe 
708917Selowe 	/*
709917Selowe 	 * Fill the page with each (0x00 - 0xFF] bit pattern, flushing
710917Selowe 	 * the cache in between reading and writing.  We do this under
711917Selowe 	 * on_trap() protection to avoid recursion.
712917Selowe 	 */
713917Selowe 	if (on_trap(&otd, OT_DATA_EC)) {
714917Selowe 		PR_MESSAGE(CE_WARN, 1, MSG_UE, pa);
715917Selowe 		errors = 1;
716917Selowe 	} else {
717917Selowe 		for (wb = 0xff; wb > 0; wb--) {
718917Selowe 			for (i = 0; i < PAGESIZE; i++) {
719917Selowe 				kaddr[i] = wb;
720917Selowe 			}
721917Selowe 
722917Selowe 			sync_data_memory(kaddr, PAGESIZE);
723917Selowe 
724917Selowe 			for (i = 0; i < PAGESIZE; i++) {
725917Selowe 				rb = kaddr[i];
726917Selowe 				if (rb != wb) {
727917Selowe 					/*
728917Selowe 					 * We had a mismatch without a trap.
729917Selowe 					 * Uh-oh. Something is really wrong
730917Selowe 					 * with this system.
731917Selowe 					 */
732917Selowe 					if (page_retire_messages) {
733917Selowe 						cmn_err(CE_WARN, MSG_DM,
734917Selowe 						    pa_hi, pa_lo, rb, wb);
735917Selowe 					}
736917Selowe 					errors = 1;
737917Selowe 					goto out;	/* double break */
738917Selowe 				}
739917Selowe 			}
740917Selowe 		}
741917Selowe 	}
742917Selowe out:
743917Selowe 	no_trap();
744917Selowe 	ppmapout(kaddr);
745917Selowe 
746917Selowe 	return (errors ? 0 : 1);
747917Selowe }
748917Selowe 
749917Selowe /*
750917Selowe  * Try to clear a page_t with a single UE. If the UE was transient, it is
751917Selowe  * returned to service, and we return 1. Otherwise we return 0 meaning
752917Selowe  * that further processing is required to retire the page.
753917Selowe  */
754917Selowe static int
755917Selowe page_retire_transient_ue(page_t *pp)
756917Selowe {
757917Selowe 	ASSERT(PAGE_EXCL(pp));
758917Selowe 	ASSERT(!hat_page_is_mapped(pp));
759917Selowe 
760917Selowe 	/*
761917Selowe 	 * If this page is a repeat offender, retire him under the
762917Selowe 	 * "two strikes and you're out" rule. The caller is responsible
763917Selowe 	 * for scrubbing the page to try to clear the error.
764917Selowe 	 */
765917Selowe 	if (pp->p_toxic & PR_UE_SCRUBBED) {
766917Selowe 		PR_INCR_KSTAT(pr_ue_persistent);
767917Selowe 		return (0);
768917Selowe 	}
769917Selowe 
770917Selowe 	if (page_clear_transient_ue(pp)) {
771917Selowe 		/*
772917Selowe 		 * We set the PR_SCRUBBED_UE bit; if we ever see this
773917Selowe 		 * page again, we will retire it, no questions asked.
774917Selowe 		 */
775917Selowe 		page_settoxic(pp, PR_UE_SCRUBBED);
776917Selowe 
777917Selowe 		if (page_retire_first_ue) {
778917Selowe 			PR_INCR_KSTAT(pr_ue_cleared_retire);
779917Selowe 			return (0);
780917Selowe 		} else {
781917Selowe 			PR_INCR_KSTAT(pr_ue_cleared_free);
782917Selowe 
783917Selowe 			page_clrtoxic(pp, PR_UE | PR_MCE | PR_MSG | PR_BUSY);
784917Selowe 			page_retire_dequeue(pp);
785917Selowe 
786917Selowe 			/* LINTED: CONSTCOND */
787917Selowe 			VN_DISPOSE(pp, B_FREE, 1, kcred);
788917Selowe 			return (1);
789917Selowe 		}
790917Selowe 	}
791917Selowe 
792917Selowe 	PR_INCR_KSTAT(pr_ue_persistent);
793917Selowe 	return (0);
794917Selowe }
795917Selowe 
796917Selowe /*
797917Selowe  * Update the statistics dynamically when our kstat is read.
798917Selowe  */
799917Selowe static int
800917Selowe page_retire_kstat_update(kstat_t *ksp, int rw)
801917Selowe {
802917Selowe 	struct page_retire_kstat *pr;
803917Selowe 
804917Selowe 	if (ksp == NULL)
805917Selowe 	    return (EINVAL);
806917Selowe 
807917Selowe 	switch (rw) {
808917Selowe 
809917Selowe 	case KSTAT_READ:
810917Selowe 		pr = (struct page_retire_kstat *)ksp->ks_data;
811917Selowe 		ASSERT(pr == &page_retire_kstat);
812917Selowe 		pr->pr_limit.value.ui64 = PAGE_RETIRE_LIMIT;
813917Selowe 		return (0);
814917Selowe 
815917Selowe 	case KSTAT_WRITE:
816917Selowe 		return (EACCES);
817917Selowe 
818917Selowe 	default:
819917Selowe 		return (EINVAL);
820917Selowe 	}
821917Selowe 	/*NOTREACHED*/
822917Selowe }
823917Selowe 
824917Selowe /*
825917Selowe  * Initialize the page retire mechanism:
826917Selowe  *
827917Selowe  *   - Establish the correctable error retire limit.
828917Selowe  *   - Initialize locks.
829917Selowe  *   - Build the retired_pages vnode.
830917Selowe  *   - Set up the kstats.
831917Selowe  *   - Fire off the background thread.
832917Selowe  *   - Tell page_tryretire() it's OK to start retiring pages.
833917Selowe  */
834917Selowe void
835917Selowe page_retire_init(void)
836917Selowe {
837917Selowe 	const fs_operation_def_t retired_vnodeops_template[] = {NULL, NULL};
838917Selowe 	struct vnodeops *vops;
839917Selowe 
840917Selowe 	const uint_t page_retire_ndata =
841917Selowe 	    sizeof (page_retire_kstat) / sizeof (kstat_named_t);
842917Selowe 
843917Selowe 	ASSERT(page_retire_ksp == NULL);
844917Selowe 
845917Selowe 	if (max_pages_retired_bps <= 0) {
846917Selowe 		max_pages_retired_bps = MCE_BPT;
847917Selowe 	}
848917Selowe 
849917Selowe 	mutex_init(&pr_q_mutex, NULL, MUTEX_DEFAULT, NULL);
850917Selowe 
851917Selowe 	retired_pages = vn_alloc(KM_SLEEP);
852917Selowe 	if (vn_make_ops("retired_pages", retired_vnodeops_template, &vops)) {
853917Selowe 		cmn_err(CE_PANIC,
854917Selowe 		    "page_retired_init: can't make retired vnodeops");
855917Selowe 	}
856917Selowe 	vn_setops(retired_pages, vops);
857917Selowe 
858917Selowe 	if ((page_retire_ksp = kstat_create("unix", 0, "page_retire",
859917Selowe 	    "misc", KSTAT_TYPE_NAMED, page_retire_ndata,
860917Selowe 	    KSTAT_FLAG_VIRTUAL)) == NULL) {
861917Selowe 		cmn_err(CE_WARN, "kstat_create for page_retire failed");
862917Selowe 	} else {
863917Selowe 		page_retire_ksp->ks_data = (void *)&page_retire_kstat;
864917Selowe 		page_retire_ksp->ks_update = page_retire_kstat_update;
865917Selowe 		kstat_install(page_retire_ksp);
866917Selowe 	}
867917Selowe 
868917Selowe 	pr_thread_shortwait = 23 * hz;
869917Selowe 	pr_thread_longwait = 1201 * hz;
870917Selowe 	mutex_init(&pr_thread_mutex, NULL, MUTEX_DEFAULT, NULL);
871917Selowe 	cv_init(&pr_cv, NULL, CV_DEFAULT, NULL);
872917Selowe 	pr_thread_id = thread_create(NULL, 0, page_retire_thread, NULL, 0, &p0,
873917Selowe 	    TS_RUN, minclsyspri);
874917Selowe 
875917Selowe 	pr_enable = 1;
876917Selowe }
877917Selowe 
878917Selowe /*
879917Selowe  * page_retire_hunt() callback for the retire thread.
880917Selowe  */
881917Selowe static void
882917Selowe page_retire_thread_cb(page_t *pp)
883917Selowe {
884917Selowe 	PR_DEBUG(prd_tctop);
885*973Selowe 	if (!PP_ISKVP(pp) && page_trylock(pp, SE_EXCL)) {
886917Selowe 		PR_DEBUG(prd_tclocked);
887917Selowe 		page_unlock(pp);
888917Selowe 	}
889917Selowe }
890917Selowe 
891917Selowe /*
892917Selowe  * page_retire_hunt() callback for mdboot().
893917Selowe  *
894917Selowe  * It is necessary to scrub any failing pages prior to reboot in order to
895917Selowe  * prevent a latent error trap from occurring on the next boot.
896917Selowe  */
897917Selowe void
898917Selowe page_retire_mdboot_cb(page_t *pp)
899917Selowe {
900917Selowe 	/*
901917Selowe 	 * Don't scrub the kernel, since we might still need it, unless
902917Selowe 	 * we have UEs on the page, in which case we have nothing to lose.
903917Selowe 	 */
904*973Selowe 	if (!PP_ISKVP(pp) || PP_TOXIC(pp)) {
905917Selowe 		pp->p_selock = -1;	/* pacify ASSERTs */
906*973Selowe 		PP_CLRFREE(pp);
907917Selowe 		pagescrub(pp, 0, PAGESIZE);
908917Selowe 		pp->p_selock = 0;
909917Selowe 	}
910917Selowe 	pp->p_toxic = 0;
911917Selowe }
912917Selowe 
913917Selowe /*
914917Selowe  * Hunt down any pages in the system that have not yet been retired, invoking
915917Selowe  * the provided callback function on each of them.
916917Selowe  */
917917Selowe void
918917Selowe page_retire_hunt(void (*callback)(page_t *))
919917Selowe {
920917Selowe 	page_t *pp;
921917Selowe 	page_t *first;
922*973Selowe 	uint64_t tbr, found;
923*973Selowe 	int i;
924917Selowe 
925917Selowe 	PR_DEBUG(prd_hunt);
926917Selowe 
927917Selowe 	if (PR_KSTAT_PENDING == 0) {
928917Selowe 		return;
929917Selowe 	}
930917Selowe 
931917Selowe 	PR_DEBUG(prd_dohunt);
932917Selowe 
933917Selowe 	found = 0;
934917Selowe 	mutex_enter(&pr_q_mutex);
935917Selowe 
936*973Selowe 	tbr = PR_KSTAT_PENDING;
937*973Selowe 
938917Selowe 	for (i = 0; i < PR_PENDING_QMAX; i++) {
939917Selowe 		if ((pp = pr_pending_q[i]) != NULL) {
940917Selowe 			mutex_exit(&pr_q_mutex);
941917Selowe 			callback(pp);
942917Selowe 			mutex_enter(&pr_q_mutex);
943917Selowe 			found++;
944917Selowe 		}
945917Selowe 	}
946917Selowe 
947*973Selowe 	if (PR_KSTAT_EQFAIL == PR_KSTAT_DQFAIL && found == tbr) {
948917Selowe 		mutex_exit(&pr_q_mutex);
949917Selowe 		PR_DEBUG(prd_earlyhunt);
950917Selowe 		return;
951917Selowe 	}
952917Selowe 	mutex_exit(&pr_q_mutex);
953917Selowe 
954917Selowe 	PR_DEBUG(prd_latehunt);
955917Selowe 
956917Selowe 	/*
957917Selowe 	 * We've lost track of a page somewhere. Hunt it down.
958917Selowe 	 */
959917Selowe 	memsegs_lock(0);
960917Selowe 	pp = first = page_first();
961917Selowe 	do {
962917Selowe 		if (PP_PR_REQ(pp)) {
963917Selowe 			callback(pp);
964*973Selowe 			if (++found == tbr) {
965917Selowe 				break;	/* got 'em all */
966917Selowe 			}
967917Selowe 		}
968917Selowe 	} while ((pp = page_next(pp)) != first);
969917Selowe 	memsegs_unlock(0);
970917Selowe }
971917Selowe 
972917Selowe /*
973917Selowe  * The page_retire_thread loops forever, looking to see if there are
974917Selowe  * pages still waiting to be retired.
975917Selowe  */
976917Selowe static void
977917Selowe page_retire_thread(void)
978917Selowe {
979917Selowe 	callb_cpr_t c;
980917Selowe 
981917Selowe 	CALLB_CPR_INIT(&c, &pr_thread_mutex, callb_generic_cpr, "page_retire");
982917Selowe 
983917Selowe 	mutex_enter(&pr_thread_mutex);
984917Selowe 	for (;;) {
985917Selowe 		if (pr_enable && PR_KSTAT_PENDING) {
986917Selowe 			kmem_reap();
987917Selowe 			seg_preap();
988917Selowe 			page_retire_hunt(page_retire_thread_cb);
989917Selowe 			CALLB_CPR_SAFE_BEGIN(&c);
990917Selowe 			(void) cv_timedwait(&pr_cv, &pr_thread_mutex,
991917Selowe 			    lbolt + pr_thread_shortwait);
992917Selowe 			CALLB_CPR_SAFE_END(&c, &pr_thread_mutex);
993917Selowe 		} else {
994917Selowe 			CALLB_CPR_SAFE_BEGIN(&c);
995917Selowe 			(void) cv_timedwait(&pr_cv, &pr_thread_mutex,
996917Selowe 			    lbolt + pr_thread_longwait);
997917Selowe 			CALLB_CPR_SAFE_END(&c, &pr_thread_mutex);
998917Selowe 		}
999917Selowe 	}
1000917Selowe 	/*NOTREACHED*/
1001917Selowe }
1002917Selowe 
1003917Selowe /*
1004917Selowe  * page_retire_pp() decides what to do with a failing page.
1005917Selowe  *
1006917Selowe  * When we get a free page (e.g. the scrubber or in the free path) life is
1007917Selowe  * nice because the page is clean and marked free -- those always retire
1008917Selowe  * nicely. From there we go by order of difficulty. If the page has data,
1009917Selowe  * we attempt to relocate its contents to a suitable replacement page. If
1010917Selowe  * that does not succeed, we look to see if it is clean. If after all of
1011917Selowe  * this we have a clean, unmapped page (which we usually do!), we retire it.
1012917Selowe  * If the page is not clean, we still process it regardless on a UE; for
1013917Selowe  * CEs or FMA requests, we fail leaving the page in service. The page will
1014917Selowe  * eventually be tried again later. We always return with the page unlocked
1015917Selowe  * since we are called from page_unlock().
1016917Selowe  *
1017917Selowe  * We don't call panic or do anything fancy down in here. Our boss the DE
1018917Selowe  * gets paid handsomely to do his job of figuring out what to do when errors
1019917Selowe  * occur. We just do what he tells us to do.
1020917Selowe  */
1021917Selowe static int
1022917Selowe page_retire_pp(page_t *pp)
1023917Selowe {
1024917Selowe 	int		toxic;
1025917Selowe 
1026917Selowe 	ASSERT(PAGE_EXCL(pp));
1027917Selowe 	ASSERT(pp->p_iolock_state == 0);
1028917Selowe 	ASSERT(pp->p_szc == 0);
1029917Selowe 
1030917Selowe 	PR_DEBUG(prd_top);
1031917Selowe 	PR_TYPES(pp);
1032917Selowe 
1033917Selowe 	toxic = pp->p_toxic;
1034917Selowe 	ASSERT(toxic & PR_REASONS);
1035917Selowe 
1036917Selowe 	if ((toxic & (PR_FMA | PR_MCE)) && !(toxic & PR_UE) &&
1037917Selowe 	    page_retire_limit()) {
1038917Selowe 		page_clrtoxic(pp, PR_FMA | PR_MCE | PR_MSG | PR_BUSY);
1039917Selowe 		page_retire_dequeue(pp);
1040917Selowe 		page_unlock(pp);
1041917Selowe 		return (page_retire_done(pp, PRD_LIMIT));
1042917Selowe 	}
1043917Selowe 
1044917Selowe 	if (PP_ISFREE(pp)) {
1045917Selowe 		PR_DEBUG(prd_free);
1046917Selowe 		if (!MTBF(recl_calls, recl_mtbf) || !page_reclaim(pp, NULL)) {
1047917Selowe 			PR_DEBUG(prd_noreclaim);
1048917Selowe 			PR_INCR_KSTAT(pr_failed);
1049917Selowe 			page_unlock(pp);
1050917Selowe 			return (page_retire_done(pp, PRD_FAILED));
1051917Selowe 		}
1052917Selowe 	}
1053917Selowe 
1054917Selowe 	if ((toxic & PR_UE) == 0 && pp->p_vnode && !PP_ISFREE(pp) &&
1055*973Selowe 	    !PP_ISNORELOCKERNEL(pp) && MTBF(reloc_calls, reloc_mtbf)) {
1056917Selowe 		page_t *newpp;
1057917Selowe 		spgcnt_t count;
1058917Selowe 
1059917Selowe 		/*
1060917Selowe 		 * If we can relocate the page, great! newpp will go
1061917Selowe 		 * on without us, and everything is fine.  Regardless
1062917Selowe 		 * of whether the relocation succeeds, we are still
1063917Selowe 		 * going to take `pp' around back and shoot it.
1064917Selowe 		 */
1065917Selowe 		newpp = NULL;
1066917Selowe 		if (page_relocate(&pp, &newpp, 0, 0, &count, NULL) == 0) {
1067*973Selowe 			PR_DEBUG(prd_reloc);
1068917Selowe 			page_unlock(newpp);
1069917Selowe 			ASSERT(hat_page_getattr(pp, P_MOD) == 0);
1070*973Selowe 		} else {
1071*973Selowe 			PR_DEBUG(prd_relocfail);
1072917Selowe 		}
1073917Selowe 	}
1074917Selowe 
1075*973Selowe 	if (hat_ismod(pp)) {
1076*973Selowe 		PR_DEBUG(prd_mod);
1077*973Selowe 		PR_INCR_KSTAT(pr_failed);
1078*973Selowe 		page_unlock(pp);
1079*973Selowe 		return (page_retire_done(pp, PRD_FAILED));
1080*973Selowe 	}
1081*973Selowe 
1082*973Selowe 	if (PP_ISKVP(pp)) {
1083917Selowe 		PR_DEBUG(prd_kern);
1084917Selowe 		PR_INCR_KSTAT(pr_failed_kernel);
1085917Selowe 		page_unlock(pp);
1086917Selowe 		return (page_retire_done(pp, PRD_FAILED));
1087917Selowe 	}
1088917Selowe 
1089917Selowe 	if (pp->p_lckcnt || pp->p_cowcnt) {
1090*973Selowe 		PR_DEBUG(prd_locked);
1091*973Selowe 		PR_INCR_KSTAT(pr_failed);
1092*973Selowe 		page_unlock(pp);
1093*973Selowe 		return (page_retire_done(pp, PRD_FAILED));
1094917Selowe 	}
1095917Selowe 
1096917Selowe 	(void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
1097917Selowe 	ASSERT(!PP_ISFREE(pp));
1098917Selowe 	ASSERT(!hat_page_is_mapped(pp));
1099917Selowe 
1100917Selowe 	/*
1101*973Selowe 	 * If the page is modified, and was not relocated; we can't
1102*973Selowe 	 * retire it without dropping data on the floor. We have to
1103*973Selowe 	 * recheck after unloading since the dirty bit could have been
1104*973Selowe 	 * set since we last checked.
1105917Selowe 	 */
1106917Selowe 	if (hat_ismod(pp)) {
1107*973Selowe 		PR_DEBUG(prd_mod_late);
1108*973Selowe 		PR_INCR_KSTAT(pr_failed);
1109*973Selowe 		page_unlock(pp);
1110*973Selowe 		return (page_retire_done(pp, PRD_FAILED));
1111917Selowe 	}
1112917Selowe 
1113917Selowe 	if (pp->p_vnode) {
1114917Selowe 		PR_DEBUG(prd_hashout);
1115917Selowe 		page_hashout(pp, NULL);
1116917Selowe 	}
1117917Selowe 	ASSERT(!pp->p_vnode);
1118917Selowe 
1119917Selowe 	/*
1120917Selowe 	 * The problem page is locked, demoted, unmapped, not free,
1121917Selowe 	 * hashed out, and not COW or mlocked (whew!).
1122917Selowe 	 *
1123917Selowe 	 * Now we select our ammunition, take it around back, and shoot it.
1124917Selowe 	 */
1125917Selowe 	if (toxic & PR_UE) {
1126917Selowe 		if (page_retire_transient_ue(pp)) {
1127917Selowe 			PR_DEBUG(prd_uescrubbed);
1128917Selowe 			return (page_retire_done(pp, PRD_UE_SCRUBBED));
1129917Selowe 		} else {
1130917Selowe 			PR_DEBUG(prd_uenotscrubbed);
1131917Selowe 			page_retire_destroy(pp);
1132917Selowe 			return (page_retire_done(pp, PRD_SUCCESS));
1133917Selowe 		}
1134917Selowe 	} else if (toxic & PR_FMA) {
1135917Selowe 		PR_DEBUG(prd_fma);
1136917Selowe 		page_retire_destroy(pp);
1137917Selowe 		return (page_retire_done(pp, PRD_SUCCESS));
1138917Selowe 	} else if (toxic & PR_MCE) {
1139917Selowe 		PR_DEBUG(prd_mce);
1140917Selowe 		page_retire_destroy(pp);
1141917Selowe 		return (page_retire_done(pp, PRD_SUCCESS));
1142917Selowe 	}
1143917Selowe 	panic("page_retire_pp: bad toxic flags %d", toxic);
1144917Selowe 	/*NOTREACHED*/
1145917Selowe }
1146917Selowe 
1147917Selowe /*
1148917Selowe  * Try to retire a page when we stumble onto it in the page lock routines.
1149917Selowe  */
1150917Selowe void
1151917Selowe page_tryretire(page_t *pp)
1152917Selowe {
1153917Selowe 	ASSERT(PAGE_EXCL(pp));
1154917Selowe 
1155917Selowe 	if (!pr_enable) {
1156917Selowe 		page_unlock(pp);
1157917Selowe 		return;
1158917Selowe 	}
1159917Selowe 
1160917Selowe 	/*
1161917Selowe 	 * If the page is a big page, try to break it up.
1162917Selowe 	 *
1163917Selowe 	 * If there are other bad pages besides `pp', they will be
1164917Selowe 	 * recursively retired for us thanks to a bit of magic.
1165917Selowe 	 * If the page is a small page with errors, try to retire it.
1166917Selowe 	 */
1167917Selowe 	if (pp->p_szc > 0) {
1168917Selowe 		if (PP_ISFREE(pp) && !page_try_demote_free_pages(pp)) {
1169917Selowe 			page_unlock(pp);
1170917Selowe 			PR_DEBUG(prd_nofreedemote);
1171917Selowe 			return;
1172917Selowe 		} else if (!page_try_demote_pages(pp)) {
1173917Selowe 			page_unlock(pp);
1174917Selowe 			PR_DEBUG(prd_nodemote);
1175917Selowe 			return;
1176917Selowe 		}
1177917Selowe 		PR_DEBUG(prd_demoted);
1178917Selowe 		page_unlock(pp);
1179917Selowe 	} else {
1180917Selowe 		(void) page_retire_pp(pp);
1181917Selowe 	}
1182917Selowe }
1183917Selowe 
1184917Selowe /*
1185917Selowe  * page_retire() - the front door in to retire a page.
1186917Selowe  *
1187917Selowe  * Ideally, page_retire() would instantly retire the requested page.
1188917Selowe  * Unfortunately, some pages are locked or otherwise tied up and cannot be
1189917Selowe  * retired right away. To deal with that, bits are set in p_toxic of the
1190917Selowe  * page_t. An attempt is made to lock the page; if the attempt is successful,
1191917Selowe  * we instantly unlock the page counting on page_unlock() to notice p_toxic
1192917Selowe  * is nonzero and to call back into page_retire_pp(). Success is determined
1193917Selowe  * by looking to see whether the page has been retired once it has been
1194917Selowe  * unlocked.
1195917Selowe  *
1196917Selowe  * Returns:
1197917Selowe  *
1198917Selowe  *   - 0 on success,
1199917Selowe  *   - EINVAL when the PA is whacko,
1200917Selowe  *   - EBUSY if the page is already retired, or
1201917Selowe  *   - EAGAIN if the page could not be _immediately_ retired.
1202917Selowe  */
1203917Selowe int
1204917Selowe page_retire(uint64_t pa, uchar_t reason)
1205917Selowe {
1206917Selowe 	page_t	*pp;
1207917Selowe 
1208917Selowe 	ASSERT(reason & PR_REASONS);		/* there must be a reason */
1209917Selowe 	ASSERT(!(reason & ~PR_REASONS));	/* but no other bits */
1210917Selowe 
1211917Selowe 	pp = page_numtopp_nolock(mmu_btop(pa));
1212917Selowe 	if (pp == NULL) {
1213917Selowe 		PR_MESSAGE(CE_WARN, 1, "Cannot schedule clearing of error on"
1214917Selowe 		    " page 0x%08x.%08x; page is not relocatable memory", pa);
1215917Selowe 		return (page_retire_done(pp, PRD_INVALID_PA));
1216917Selowe 	}
1217917Selowe 	if (PP_RETIRED(pp)) {
1218917Selowe 		return (page_retire_done(pp, PRD_DUPLICATE));
1219917Selowe 	}
1220917Selowe 
1221917Selowe 	if (reason & PR_UE) {
1222917Selowe 		PR_MESSAGE(CE_NOTE, 1, "Scheduling clearing of error on"
1223917Selowe 		    " page 0x%08x.%08x", pa);
1224917Selowe 	} else {
1225917Selowe 		PR_MESSAGE(CE_NOTE, 1, "Scheduling removal of"
1226917Selowe 		    " page 0x%08x.%08x", pa);
1227917Selowe 	}
1228917Selowe 	page_settoxic(pp, reason);
1229917Selowe 	page_retire_enqueue(pp);
1230917Selowe 
1231917Selowe 	/*
1232917Selowe 	 * And now for some magic.
1233917Selowe 	 *
1234917Selowe 	 * We marked this page toxic up above.  All there is left to do is
1235917Selowe 	 * to try to lock the page and then unlock it.  The page lock routines
1236917Selowe 	 * will intercept the page and retire it if they can.  If the page
1237917Selowe 	 * cannot be locked, 's okay -- page_unlock() will eventually get it,
1238917Selowe 	 * or the background thread, until then the lock routines will deny
1239917Selowe 	 * further locks on it.
1240917Selowe 	 */
1241917Selowe 	if (MTBF(pr_calls, pr_mtbf) && page_trylock(pp, SE_EXCL)) {
1242917Selowe 		PR_DEBUG(prd_prlocked);
1243917Selowe 		page_unlock(pp);
1244917Selowe 	} else {
1245917Selowe 		PR_DEBUG(prd_prnotlocked);
1246917Selowe 	}
1247917Selowe 
1248917Selowe 	if (PP_RETIRED(pp)) {
1249917Selowe 		PR_DEBUG(prd_prretired);
1250917Selowe 		return (0);
1251917Selowe 	} else {
1252917Selowe 		cv_signal(&pr_cv);
1253917Selowe 		PR_INCR_KSTAT(pr_failed);
1254917Selowe 
1255917Selowe 		if (pp->p_toxic & PR_MSG) {
1256917Selowe 			return (page_retire_done(pp, PRD_FAILED));
1257917Selowe 		} else {
1258917Selowe 			return (page_retire_done(pp, PRD_PENDING));
1259917Selowe 		}
1260917Selowe 	}
1261917Selowe }
1262917Selowe 
1263917Selowe /*
1264917Selowe  * Take a retired page off the retired-pages vnode and clear the toxic flags.
1265917Selowe  * If "free" is nonzero, lock it and put it back on the freelist. If "free"
1266917Selowe  * is zero, the caller already holds SE_EXCL lock so we simply unretire it
1267917Selowe  * and don't do anything else with it.
1268917Selowe  *
1269917Selowe  * Any unretire messages are printed from this routine.
1270917Selowe  *
1271917Selowe  * Returns 0 if page pp was unretired; else an error code.
1272917Selowe  */
1273917Selowe int
1274917Selowe page_unretire_pp(page_t *pp, int free)
1275917Selowe {
1276917Selowe 	/*
1277917Selowe 	 * To be retired, a page has to be hashed onto the retired_pages vnode
1278917Selowe 	 * and have PR_RETIRED set in p_toxic.
1279917Selowe 	 */
1280917Selowe 	if (free == 0 || page_try_reclaim_lock(pp, SE_EXCL, SE_RETIRED)) {
1281917Selowe 		ASSERT(PAGE_EXCL(pp));
1282917Selowe 		PR_DEBUG(prd_ulocked);
1283917Selowe 		if (!PP_RETIRED(pp)) {
1284917Selowe 			PR_DEBUG(prd_unotretired);
1285917Selowe 			page_unlock(pp);
1286917Selowe 			return (page_retire_done(pp, PRD_UNR_NOT));
1287917Selowe 		}
1288917Selowe 
1289917Selowe 		PR_MESSAGE(CE_NOTE, 1, "unretiring retired"
1290917Selowe 		    " page 0x%08x.%08x", mmu_ptob(pp->p_pagenum));
1291917Selowe 		if (pp->p_toxic & PR_FMA) {
1292917Selowe 			PR_DECR_KSTAT(pr_fma);
1293917Selowe 		} else if (pp->p_toxic & PR_UE) {
1294917Selowe 			PR_DECR_KSTAT(pr_ue);
1295917Selowe 		} else {
1296917Selowe 			PR_DECR_KSTAT(pr_mce);
1297917Selowe 		}
1298917Selowe 		page_clrtoxic(pp, PR_ALLFLAGS);
1299917Selowe 
1300917Selowe 		if (free) {
1301917Selowe 			PR_DEBUG(prd_udestroy);
1302917Selowe 			page_destroy(pp, 0);
1303917Selowe 		} else {
1304917Selowe 			PR_DEBUG(prd_uhashout);
1305917Selowe 			page_hashout(pp, NULL);
1306917Selowe 		}
1307917Selowe 
1308917Selowe 		mutex_enter(&freemem_lock);
1309917Selowe 		availrmem++;
1310917Selowe 		mutex_exit(&freemem_lock);
1311917Selowe 
1312917Selowe 		PR_DEBUG(prd_uunretired);
1313917Selowe 		PR_DECR_KSTAT(pr_retired);
1314917Selowe 		PR_INCR_KSTAT(pr_unretired);
1315917Selowe 		return (page_retire_done(pp, PRD_UNR_SUCCESS));
1316917Selowe 	}
1317917Selowe 	PR_DEBUG(prd_unotlocked);
1318917Selowe 	return (page_retire_done(pp, PRD_UNR_CANTLOCK));
1319917Selowe }
1320917Selowe 
1321917Selowe /*
1322917Selowe  * Return a page to service by moving it from the retired_pages vnode
1323917Selowe  * onto the freelist.
1324917Selowe  *
1325917Selowe  * Called from mmioctl_page_retire() on behalf of the FMA DE.
1326917Selowe  *
1327917Selowe  * Returns:
1328917Selowe  *
1329917Selowe  *   - 0 if the page is unretired,
1330917Selowe  *   - EAGAIN if the pp can not be locked,
1331917Selowe  *   - EINVAL if the PA is whacko, and
1332917Selowe  *   - EBADF if the pp is not retired.
1333917Selowe  */
1334917Selowe int
1335917Selowe page_unretire(uint64_t pa)
1336917Selowe {
1337917Selowe 	page_t	*pp;
1338917Selowe 
1339917Selowe 	pp = page_numtopp_nolock(mmu_btop(pa));
1340917Selowe 	if (pp == NULL) {
1341917Selowe 		return (page_retire_done(pp, PRD_INVALID_PA));
1342917Selowe 	}
1343917Selowe 
1344917Selowe 	return (page_unretire_pp(pp, 1));
1345917Selowe }
1346917Selowe 
1347917Selowe /*
1348917Selowe  * Test a page to see if it is retired. If errors is non-NULL, the toxic
1349917Selowe  * bits of the page are returned. Returns 0 on success, error code on failure.
1350917Selowe  */
1351917Selowe int
1352917Selowe page_retire_check_pp(page_t *pp, uint64_t *errors)
1353917Selowe {
1354917Selowe 	int rc;
1355917Selowe 
1356917Selowe 	if (PP_RETIRED(pp)) {
1357917Selowe 		PR_DEBUG(prd_checkhit);
1358917Selowe 		rc = 0;
1359917Selowe 	} else {
1360917Selowe 		PR_DEBUG(prd_checkmiss);
1361917Selowe 		rc = EAGAIN;
1362917Selowe 	}
1363917Selowe 
1364917Selowe 	/*
1365917Selowe 	 * We have magically arranged the bit values returned to fmd(1M)
1366917Selowe 	 * to line up with the FMA, MCE, and UE bits of the page_t.
1367917Selowe 	 */
1368917Selowe 	if (errors) {
1369917Selowe 		uint64_t toxic = (uint64_t)(pp->p_toxic & PR_ERRMASK);
1370917Selowe 		if (toxic & PR_UE_SCRUBBED) {
1371917Selowe 			toxic &= ~PR_UE_SCRUBBED;
1372917Selowe 			toxic |= PR_UE;
1373917Selowe 		}
1374917Selowe 		*errors = toxic;
1375917Selowe 	}
1376917Selowe 
1377917Selowe 	return (rc);
1378917Selowe }
1379917Selowe 
1380917Selowe /*
1381917Selowe  * Test to see if the page_t for a given PA is retired, and return the
1382917Selowe  * hardware errors we have seen on the page if requested.
1383917Selowe  *
1384917Selowe  * Called from mmioctl_page_retire on behalf of the FMA DE.
1385917Selowe  *
1386917Selowe  * Returns:
1387917Selowe  *
1388917Selowe  *   - 0 if the page is retired,
1389917Selowe  *   - EAGAIN if it is not, and
1390917Selowe  *   - EINVAL if the PA is whacko.
1391917Selowe  */
1392917Selowe int
1393917Selowe page_retire_check(uint64_t pa, uint64_t *errors)
1394917Selowe {
1395917Selowe 	page_t	*pp;
1396917Selowe 
1397917Selowe 	if (errors) {
1398917Selowe 		*errors = 0;
1399917Selowe 	}
1400917Selowe 
1401917Selowe 	pp = page_numtopp_nolock(mmu_btop(pa));
1402917Selowe 	if (pp == NULL) {
1403917Selowe 		return (page_retire_done(pp, PRD_INVALID_PA));
1404917Selowe 	}
1405917Selowe 
1406917Selowe 	return (page_retire_check_pp(pp, errors));
1407917Selowe }
1408917Selowe 
1409917Selowe /*
1410917Selowe  * Page retire self-test. For now, it always returns 0.
1411917Selowe  */
1412917Selowe int
1413917Selowe page_retire_test(void)
1414917Selowe {
1415917Selowe 	page_t *first, *pp, *cpp, *cpp2, *lpp;
1416917Selowe 
1417917Selowe 	/*
1418917Selowe 	 * Tests the corner case where a large page can't be retired
1419917Selowe 	 * because one of the constituent pages is locked. We mark
1420917Selowe 	 * one page to be retired and try to retire it, and mark the
1421917Selowe 	 * other page to be retired but don't try to retire it, so
1422917Selowe 	 * that page_unlock() in the failure path will recurse and try
1423917Selowe 	 * to retire THAT page. This is the worst possible situation
1424917Selowe 	 * we can get ourselves into.
1425917Selowe 	 */
1426917Selowe 	memsegs_lock(0);
1427917Selowe 	pp = first = page_first();
1428917Selowe 	do {
1429917Selowe 		if (pp->p_szc && PP_PAGEROOT(pp) == pp) {
1430917Selowe 			cpp = pp + 1;
1431917Selowe 			lpp = PP_ISFREE(pp)? pp : pp + 2;
1432917Selowe 			cpp2 = pp + 3;
1433917Selowe 			if (!page_trylock(lpp, pp == lpp? SE_EXCL : SE_SHARED))
1434917Selowe 				continue;
1435917Selowe 			if (!page_trylock(cpp, SE_EXCL)) {
1436917Selowe 				page_unlock(lpp);
1437917Selowe 				continue;
1438917Selowe 			}
1439917Selowe 			page_settoxic(cpp, PR_FMA | PR_BUSY);
1440917Selowe 			page_settoxic(cpp2, PR_FMA);
1441917Selowe 			page_tryretire(cpp);	/* will fail */
1442917Selowe 			page_unlock(lpp);
1443917Selowe 			(void) page_retire(cpp->p_pagenum, PR_FMA);
1444917Selowe 			(void) page_retire(cpp2->p_pagenum, PR_FMA);
1445917Selowe 		}
1446917Selowe 	} while ((pp = page_next(pp)) != first);
1447917Selowe 	memsegs_unlock(0);
1448917Selowe 
1449917Selowe 	return (0);
1450917Selowe }
1451