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 /* 23*1338Selowe * Copyright 2006 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 * 72*1338Selowe * UE pages without persistent errors are scrubbed and returned to service. 73*1338Selowe * Recidivist pages, as well as FMA-directed requests for retirement, result 74*1338Selowe * in the page being taken out of service. Once the decision is made to take 75*1338Selowe * a page out of service, the page is cleared, hashed onto the retired_pages 76*1338Selowe * vnode, marked as retired, and it is unlocked. No other requesters (except 77*1338Selowe * 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; 295973Selowe int prd_relocfail; 296973Selowe int prd_mod; 297973Selowe 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; \ 364973Selowe 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. 471*1338Selowe * Must be called with the page exclusively locked to prevent races which 472*1338Selowe * may attempt to retire a page without any toxic bits set. 473917Selowe */ 474917Selowe void 475917Selowe page_clrtoxic(page_t *pp, uchar_t bits) 476917Selowe { 477917Selowe ASSERT(PAGE_EXCL(pp)); 478917Selowe atomic_and_8(&pp->p_toxic, ~bits); 479917Selowe } 480917Selowe 481917Selowe /* 482917Selowe * Prints any page retire messages to the user, and decides what 483917Selowe * error code is appropriate for the condition reported. 484917Selowe */ 485917Selowe static int 486917Selowe page_retire_done(page_t *pp, int code) 487917Selowe { 488917Selowe page_retire_op_t *prop; 489917Selowe uint64_t pa = 0; 490917Selowe int i; 491917Selowe 492917Selowe if (pp != NULL) { 493*1338Selowe pa = mmu_ptob((uint64_t)pp->p_pagenum); 494917Selowe } 495917Selowe 496917Selowe prop = NULL; 497917Selowe for (i = 0; page_retire_ops[i].pr_key != PRD_INVALID_KEY; i++) { 498917Selowe if (page_retire_ops[i].pr_key == code) { 499917Selowe prop = &page_retire_ops[i]; 500917Selowe break; 501917Selowe } 502917Selowe } 503917Selowe 504917Selowe #ifdef DEBUG 505917Selowe if (page_retire_ops[i].pr_key == PRD_INVALID_KEY) { 506917Selowe cmn_err(CE_PANIC, "page_retire_done: Invalid opcode %d", code); 507917Selowe } 508917Selowe #endif 509917Selowe 510917Selowe ASSERT(prop->pr_key == code); 511917Selowe 512917Selowe prop->pr_count++; 513917Selowe 514917Selowe PR_MESSAGE(CE_NOTE, prop->pr_msglvl, prop->pr_message, pa); 515917Selowe if (pp != NULL) { 516917Selowe page_settoxic(pp, PR_MSG); 517917Selowe } 518917Selowe 519917Selowe return (prop->pr_retval); 520917Selowe } 521917Selowe 522917Selowe /* 523917Selowe * On a reboot, our friend mdboot() wants to clear up any PP_PR_REQ() pages 524917Selowe * that we were not able to retire. On large machines, walking the complete 525917Selowe * page_t array and looking at every page_t takes too long. So, as a page is 526917Selowe * marked toxic, we track it using a list that can be processed at reboot 527917Selowe * time. page_retire_enqueue() will do its best to try to avoid duplicate 528917Selowe * entries, but if we get too many errors at once the queue can overflow, 529917Selowe * in which case we will end up walking every page_t as a last resort. 530917Selowe * The background thread also makes use of this queue to find which pages 531917Selowe * are pending retirement. 532917Selowe */ 533917Selowe static void 534917Selowe page_retire_enqueue(page_t *pp) 535917Selowe { 536917Selowe int nslot = -1; 537917Selowe int i; 538917Selowe 539917Selowe mutex_enter(&pr_q_mutex); 540917Selowe 541917Selowe /* 542917Selowe * Check to make sure retire hasn't already dequeued it. 543917Selowe * In the meantime if the page was cleaned up, no need 544917Selowe * to enqueue it. 545917Selowe */ 546917Selowe if (PP_RETIRED(pp) || pp->p_toxic == 0) { 547917Selowe mutex_exit(&pr_q_mutex); 548917Selowe PR_DEBUG(prd_noaction); 549917Selowe return; 550917Selowe } 551917Selowe 552917Selowe for (i = 0; i < PR_PENDING_QMAX; i++) { 553917Selowe if (pr_pending_q[i] == pp) { 554917Selowe mutex_exit(&pr_q_mutex); 555917Selowe PR_DEBUG(prd_dup); 556917Selowe return; 557917Selowe } else if (nslot == -1 && pr_pending_q[i] == NULL) { 558917Selowe nslot = i; 559917Selowe } 560917Selowe } 561917Selowe 562917Selowe PR_INCR_KSTAT(pr_pending); 563917Selowe 564917Selowe if (nslot != -1) { 565917Selowe pr_pending_q[nslot] = pp; 566917Selowe PR_DEBUG(prd_queued); 567917Selowe } else { 568917Selowe PR_INCR_KSTAT(pr_enqueue_fail); 569917Selowe PR_DEBUG(prd_notqueued); 570917Selowe } 571917Selowe mutex_exit(&pr_q_mutex); 572917Selowe } 573917Selowe 574917Selowe static void 575917Selowe page_retire_dequeue(page_t *pp) 576917Selowe { 577917Selowe int i; 578917Selowe 579917Selowe mutex_enter(&pr_q_mutex); 580917Selowe 581917Selowe for (i = 0; i < PR_PENDING_QMAX; i++) { 582917Selowe if (pr_pending_q[i] == pp) { 583917Selowe pr_pending_q[i] = NULL; 584917Selowe break; 585917Selowe } 586917Selowe } 587917Selowe 588917Selowe if (i == PR_PENDING_QMAX) { 589917Selowe PR_INCR_KSTAT(pr_dequeue_fail); 590917Selowe } 591917Selowe 592917Selowe PR_DECR_KSTAT(pr_pending); 593917Selowe PR_DEBUG(prd_dequeue); 594917Selowe 595917Selowe mutex_exit(&pr_q_mutex); 596917Selowe } 597917Selowe 598917Selowe /* 599917Selowe * Act like page_destroy(), but instead of freeing the page, hash it onto 600917Selowe * the retired_pages vnode, and mark it retired. 601917Selowe * 602917Selowe * For fun, we try to scrub the page until it's squeaky clean. 603917Selowe * availrmem is adjusted here. 604917Selowe */ 605917Selowe static void 606917Selowe page_retire_destroy(page_t *pp) 607917Selowe { 608973Selowe u_offset_t off = (u_offset_t)((uintptr_t)pp); 609973Selowe 610917Selowe ASSERT(PAGE_EXCL(pp)); 611917Selowe ASSERT(!PP_ISFREE(pp)); 612917Selowe ASSERT(pp->p_szc == 0); 613917Selowe ASSERT(!hat_page_is_mapped(pp)); 614917Selowe ASSERT(!pp->p_vnode); 615917Selowe 616917Selowe page_clr_all_props(pp); 617917Selowe pagescrub(pp, 0, MMU_PAGESIZE); 618917Selowe 619917Selowe pp->p_next = NULL; 620917Selowe pp->p_prev = NULL; 621973Selowe if (page_hashin(pp, retired_pages, off, NULL) == 0) { 622917Selowe cmn_err(CE_PANIC, "retired page %p hashin failed", (void *)pp); 623917Selowe } 624917Selowe 625917Selowe page_settoxic(pp, PR_RETIRED); 626917Selowe page_clrtoxic(pp, PR_BUSY); 627917Selowe page_retire_dequeue(pp); 628917Selowe PR_INCR_KSTAT(pr_retired); 629917Selowe 630917Selowe if (pp->p_toxic & PR_FMA) { 631917Selowe PR_INCR_KSTAT(pr_fma); 632917Selowe } else if (pp->p_toxic & PR_UE) { 633917Selowe PR_INCR_KSTAT(pr_ue); 634917Selowe } else { 635917Selowe PR_INCR_KSTAT(pr_mce); 636917Selowe } 637917Selowe 638917Selowe mutex_enter(&freemem_lock); 639917Selowe availrmem--; 640917Selowe mutex_exit(&freemem_lock); 641917Selowe 642917Selowe page_unlock(pp); 643917Selowe } 644917Selowe 645917Selowe /* 646917Selowe * Check whether the number of pages which have been retired already exceeds 647917Selowe * the maximum allowable percentage of memory which may be retired. 648917Selowe * 649917Selowe * Returns 1 if the limit has been exceeded. 650917Selowe */ 651917Selowe static int 652917Selowe page_retire_limit(void) 653917Selowe { 654917Selowe if (PR_KSTAT_RETIRED_NOTUE >= (uint64_t)PAGE_RETIRE_LIMIT) { 655917Selowe PR_INCR_KSTAT(pr_limit_exceeded); 656917Selowe return (1); 657917Selowe } 658917Selowe 659917Selowe return (0); 660917Selowe } 661917Selowe 662917Selowe #define MSG_DM "Data Mismatch occurred at PA 0x%08x.%08x" \ 663917Selowe "[ 0x%x != 0x%x ] while attempting to clear previously " \ 664917Selowe "reported error; page removed from service" 665917Selowe 666917Selowe #define MSG_UE "Uncorrectable Error occurred at PA 0x%08x.%08x while " \ 667917Selowe "attempting to clear previously reported error; page removed " \ 668917Selowe "from service" 669917Selowe 670917Selowe /* 671917Selowe * Attempt to clear a UE from a page. 672917Selowe * Returns 1 if the error has been successfully cleared. 673917Selowe */ 674917Selowe static int 675917Selowe page_clear_transient_ue(page_t *pp) 676917Selowe { 677917Selowe caddr_t kaddr; 678917Selowe uint8_t rb, wb; 679917Selowe uint64_t pa; 680917Selowe uint32_t pa_hi, pa_lo; 681917Selowe on_trap_data_t otd; 682917Selowe int errors = 0; 683917Selowe int i; 684917Selowe 685917Selowe ASSERT(PAGE_EXCL(pp)); 686917Selowe ASSERT(PP_PR_REQ(pp)); 687917Selowe ASSERT(pp->p_szc == 0); 688917Selowe ASSERT(!hat_page_is_mapped(pp)); 689917Selowe 690917Selowe /* 691917Selowe * Clear the page and attempt to clear the UE. If we trap 692917Selowe * on the next access to the page, we know the UE has recurred. 693917Selowe */ 694917Selowe pagescrub(pp, 0, PAGESIZE); 695917Selowe 696917Selowe /* 697917Selowe * Map the page and write a bunch of bit patterns to compare 698917Selowe * what we wrote with what we read back. This isn't a perfect 699917Selowe * test but it should be good enough to catch most of the 700917Selowe * recurring UEs. If this fails to catch a recurrent UE, we'll 701917Selowe * retire the page the next time we see a UE on the page. 702917Selowe */ 703917Selowe kaddr = ppmapin(pp, PROT_READ|PROT_WRITE, (caddr_t)-1); 704917Selowe 705917Selowe pa = ptob((uint64_t)page_pptonum(pp)); 706917Selowe pa_hi = (uint32_t)(pa >> 32); 707917Selowe pa_lo = (uint32_t)pa; 708917Selowe 709917Selowe /* 710917Selowe * Fill the page with each (0x00 - 0xFF] bit pattern, flushing 711917Selowe * the cache in between reading and writing. We do this under 712917Selowe * on_trap() protection to avoid recursion. 713917Selowe */ 714917Selowe if (on_trap(&otd, OT_DATA_EC)) { 715917Selowe PR_MESSAGE(CE_WARN, 1, MSG_UE, pa); 716917Selowe errors = 1; 717917Selowe } else { 718917Selowe for (wb = 0xff; wb > 0; wb--) { 719917Selowe for (i = 0; i < PAGESIZE; i++) { 720917Selowe kaddr[i] = wb; 721917Selowe } 722917Selowe 723917Selowe sync_data_memory(kaddr, PAGESIZE); 724917Selowe 725917Selowe for (i = 0; i < PAGESIZE; i++) { 726917Selowe rb = kaddr[i]; 727917Selowe if (rb != wb) { 728917Selowe /* 729917Selowe * We had a mismatch without a trap. 730917Selowe * Uh-oh. Something is really wrong 731917Selowe * with this system. 732917Selowe */ 733917Selowe if (page_retire_messages) { 734917Selowe cmn_err(CE_WARN, MSG_DM, 735917Selowe pa_hi, pa_lo, rb, wb); 736917Selowe } 737917Selowe errors = 1; 738917Selowe goto out; /* double break */ 739917Selowe } 740917Selowe } 741917Selowe } 742917Selowe } 743917Selowe out: 744917Selowe no_trap(); 745917Selowe ppmapout(kaddr); 746917Selowe 747917Selowe return (errors ? 0 : 1); 748917Selowe } 749917Selowe 750917Selowe /* 751917Selowe * Try to clear a page_t with a single UE. If the UE was transient, it is 752917Selowe * returned to service, and we return 1. Otherwise we return 0 meaning 753917Selowe * that further processing is required to retire the page. 754917Selowe */ 755917Selowe static int 756917Selowe page_retire_transient_ue(page_t *pp) 757917Selowe { 758917Selowe ASSERT(PAGE_EXCL(pp)); 759917Selowe ASSERT(!hat_page_is_mapped(pp)); 760917Selowe 761917Selowe /* 762917Selowe * If this page is a repeat offender, retire him under the 763917Selowe * "two strikes and you're out" rule. The caller is responsible 764917Selowe * for scrubbing the page to try to clear the error. 765917Selowe */ 766917Selowe if (pp->p_toxic & PR_UE_SCRUBBED) { 767917Selowe PR_INCR_KSTAT(pr_ue_persistent); 768917Selowe return (0); 769917Selowe } 770917Selowe 771917Selowe if (page_clear_transient_ue(pp)) { 772917Selowe /* 773917Selowe * We set the PR_SCRUBBED_UE bit; if we ever see this 774917Selowe * page again, we will retire it, no questions asked. 775917Selowe */ 776917Selowe page_settoxic(pp, PR_UE_SCRUBBED); 777917Selowe 778917Selowe if (page_retire_first_ue) { 779917Selowe PR_INCR_KSTAT(pr_ue_cleared_retire); 780917Selowe return (0); 781917Selowe } else { 782917Selowe PR_INCR_KSTAT(pr_ue_cleared_free); 783917Selowe 784917Selowe page_clrtoxic(pp, PR_UE | PR_MCE | PR_MSG | PR_BUSY); 785917Selowe page_retire_dequeue(pp); 786917Selowe 787917Selowe /* LINTED: CONSTCOND */ 788917Selowe VN_DISPOSE(pp, B_FREE, 1, kcred); 789917Selowe return (1); 790917Selowe } 791917Selowe } 792917Selowe 793917Selowe PR_INCR_KSTAT(pr_ue_persistent); 794917Selowe return (0); 795917Selowe } 796917Selowe 797917Selowe /* 798917Selowe * Update the statistics dynamically when our kstat is read. 799917Selowe */ 800917Selowe static int 801917Selowe page_retire_kstat_update(kstat_t *ksp, int rw) 802917Selowe { 803917Selowe struct page_retire_kstat *pr; 804917Selowe 805917Selowe if (ksp == NULL) 806917Selowe return (EINVAL); 807917Selowe 808917Selowe switch (rw) { 809917Selowe 810917Selowe case KSTAT_READ: 811917Selowe pr = (struct page_retire_kstat *)ksp->ks_data; 812917Selowe ASSERT(pr == &page_retire_kstat); 813917Selowe pr->pr_limit.value.ui64 = PAGE_RETIRE_LIMIT; 814917Selowe return (0); 815917Selowe 816917Selowe case KSTAT_WRITE: 817917Selowe return (EACCES); 818917Selowe 819917Selowe default: 820917Selowe return (EINVAL); 821917Selowe } 822917Selowe /*NOTREACHED*/ 823917Selowe } 824917Selowe 825917Selowe /* 826917Selowe * Initialize the page retire mechanism: 827917Selowe * 828917Selowe * - Establish the correctable error retire limit. 829917Selowe * - Initialize locks. 830917Selowe * - Build the retired_pages vnode. 831917Selowe * - Set up the kstats. 832917Selowe * - Fire off the background thread. 833917Selowe * - Tell page_tryretire() it's OK to start retiring pages. 834917Selowe */ 835917Selowe void 836917Selowe page_retire_init(void) 837917Selowe { 838917Selowe const fs_operation_def_t retired_vnodeops_template[] = {NULL, NULL}; 839917Selowe struct vnodeops *vops; 840917Selowe 841917Selowe const uint_t page_retire_ndata = 842917Selowe sizeof (page_retire_kstat) / sizeof (kstat_named_t); 843917Selowe 844917Selowe ASSERT(page_retire_ksp == NULL); 845917Selowe 846917Selowe if (max_pages_retired_bps <= 0) { 847917Selowe max_pages_retired_bps = MCE_BPT; 848917Selowe } 849917Selowe 850917Selowe mutex_init(&pr_q_mutex, NULL, MUTEX_DEFAULT, NULL); 851917Selowe 852917Selowe retired_pages = vn_alloc(KM_SLEEP); 853917Selowe if (vn_make_ops("retired_pages", retired_vnodeops_template, &vops)) { 854917Selowe cmn_err(CE_PANIC, 855917Selowe "page_retired_init: can't make retired vnodeops"); 856917Selowe } 857917Selowe vn_setops(retired_pages, vops); 858917Selowe 859917Selowe if ((page_retire_ksp = kstat_create("unix", 0, "page_retire", 860917Selowe "misc", KSTAT_TYPE_NAMED, page_retire_ndata, 861917Selowe KSTAT_FLAG_VIRTUAL)) == NULL) { 862917Selowe cmn_err(CE_WARN, "kstat_create for page_retire failed"); 863917Selowe } else { 864917Selowe page_retire_ksp->ks_data = (void *)&page_retire_kstat; 865917Selowe page_retire_ksp->ks_update = page_retire_kstat_update; 866917Selowe kstat_install(page_retire_ksp); 867917Selowe } 868917Selowe 869917Selowe pr_thread_shortwait = 23 * hz; 870917Selowe pr_thread_longwait = 1201 * hz; 871917Selowe mutex_init(&pr_thread_mutex, NULL, MUTEX_DEFAULT, NULL); 872917Selowe cv_init(&pr_cv, NULL, CV_DEFAULT, NULL); 873917Selowe pr_thread_id = thread_create(NULL, 0, page_retire_thread, NULL, 0, &p0, 874917Selowe TS_RUN, minclsyspri); 875917Selowe 876917Selowe pr_enable = 1; 877917Selowe } 878917Selowe 879917Selowe /* 880917Selowe * page_retire_hunt() callback for the retire thread. 881917Selowe */ 882917Selowe static void 883917Selowe page_retire_thread_cb(page_t *pp) 884917Selowe { 885917Selowe PR_DEBUG(prd_tctop); 886973Selowe if (!PP_ISKVP(pp) && page_trylock(pp, SE_EXCL)) { 887917Selowe PR_DEBUG(prd_tclocked); 888917Selowe page_unlock(pp); 889917Selowe } 890917Selowe } 891917Selowe 892917Selowe /* 893917Selowe * page_retire_hunt() callback for mdboot(). 894917Selowe * 895917Selowe * It is necessary to scrub any failing pages prior to reboot in order to 896917Selowe * prevent a latent error trap from occurring on the next boot. 897917Selowe */ 898917Selowe void 899917Selowe page_retire_mdboot_cb(page_t *pp) 900917Selowe { 901917Selowe /* 902917Selowe * Don't scrub the kernel, since we might still need it, unless 903917Selowe * we have UEs on the page, in which case we have nothing to lose. 904917Selowe */ 905973Selowe if (!PP_ISKVP(pp) || PP_TOXIC(pp)) { 906917Selowe pp->p_selock = -1; /* pacify ASSERTs */ 907973Selowe PP_CLRFREE(pp); 908917Selowe pagescrub(pp, 0, PAGESIZE); 909917Selowe pp->p_selock = 0; 910917Selowe } 911917Selowe pp->p_toxic = 0; 912917Selowe } 913917Selowe 914917Selowe /* 915917Selowe * Hunt down any pages in the system that have not yet been retired, invoking 916917Selowe * the provided callback function on each of them. 917917Selowe */ 918917Selowe void 919917Selowe page_retire_hunt(void (*callback)(page_t *)) 920917Selowe { 921917Selowe page_t *pp; 922917Selowe page_t *first; 923973Selowe uint64_t tbr, found; 924973Selowe int i; 925917Selowe 926917Selowe PR_DEBUG(prd_hunt); 927917Selowe 928917Selowe if (PR_KSTAT_PENDING == 0) { 929917Selowe return; 930917Selowe } 931917Selowe 932917Selowe PR_DEBUG(prd_dohunt); 933917Selowe 934917Selowe found = 0; 935917Selowe mutex_enter(&pr_q_mutex); 936917Selowe 937973Selowe tbr = PR_KSTAT_PENDING; 938973Selowe 939917Selowe for (i = 0; i < PR_PENDING_QMAX; i++) { 940917Selowe if ((pp = pr_pending_q[i]) != NULL) { 941917Selowe mutex_exit(&pr_q_mutex); 942917Selowe callback(pp); 943917Selowe mutex_enter(&pr_q_mutex); 944917Selowe found++; 945917Selowe } 946917Selowe } 947917Selowe 948973Selowe if (PR_KSTAT_EQFAIL == PR_KSTAT_DQFAIL && found == tbr) { 949917Selowe mutex_exit(&pr_q_mutex); 950917Selowe PR_DEBUG(prd_earlyhunt); 951917Selowe return; 952917Selowe } 953917Selowe mutex_exit(&pr_q_mutex); 954917Selowe 955917Selowe PR_DEBUG(prd_latehunt); 956917Selowe 957917Selowe /* 958917Selowe * We've lost track of a page somewhere. Hunt it down. 959917Selowe */ 960917Selowe memsegs_lock(0); 961917Selowe pp = first = page_first(); 962917Selowe do { 963917Selowe if (PP_PR_REQ(pp)) { 964917Selowe callback(pp); 965973Selowe if (++found == tbr) { 966917Selowe break; /* got 'em all */ 967917Selowe } 968917Selowe } 969917Selowe } while ((pp = page_next(pp)) != first); 970917Selowe memsegs_unlock(0); 971917Selowe } 972917Selowe 973917Selowe /* 974917Selowe * The page_retire_thread loops forever, looking to see if there are 975917Selowe * pages still waiting to be retired. 976917Selowe */ 977917Selowe static void 978917Selowe page_retire_thread(void) 979917Selowe { 980917Selowe callb_cpr_t c; 981917Selowe 982917Selowe CALLB_CPR_INIT(&c, &pr_thread_mutex, callb_generic_cpr, "page_retire"); 983917Selowe 984917Selowe mutex_enter(&pr_thread_mutex); 985917Selowe for (;;) { 986917Selowe if (pr_enable && PR_KSTAT_PENDING) { 987*1338Selowe /* 988*1338Selowe * Sigh. It's SO broken how we have to try to shake 989*1338Selowe * loose the holder of the page. Since we have no 990*1338Selowe * idea who or what has it locked, we go bang on 991*1338Selowe * every door in the city to try to locate it. 992*1338Selowe */ 993917Selowe kmem_reap(); 994917Selowe seg_preap(); 995917Selowe page_retire_hunt(page_retire_thread_cb); 996917Selowe CALLB_CPR_SAFE_BEGIN(&c); 997917Selowe (void) cv_timedwait(&pr_cv, &pr_thread_mutex, 998917Selowe lbolt + pr_thread_shortwait); 999917Selowe CALLB_CPR_SAFE_END(&c, &pr_thread_mutex); 1000917Selowe } else { 1001917Selowe CALLB_CPR_SAFE_BEGIN(&c); 1002917Selowe (void) cv_timedwait(&pr_cv, &pr_thread_mutex, 1003917Selowe lbolt + pr_thread_longwait); 1004917Selowe CALLB_CPR_SAFE_END(&c, &pr_thread_mutex); 1005917Selowe } 1006917Selowe } 1007917Selowe /*NOTREACHED*/ 1008917Selowe } 1009917Selowe 1010917Selowe /* 1011917Selowe * page_retire_pp() decides what to do with a failing page. 1012917Selowe * 1013917Selowe * When we get a free page (e.g. the scrubber or in the free path) life is 1014917Selowe * nice because the page is clean and marked free -- those always retire 1015917Selowe * nicely. From there we go by order of difficulty. If the page has data, 1016917Selowe * we attempt to relocate its contents to a suitable replacement page. If 1017917Selowe * that does not succeed, we look to see if it is clean. If after all of 1018917Selowe * this we have a clean, unmapped page (which we usually do!), we retire it. 1019917Selowe * If the page is not clean, we still process it regardless on a UE; for 1020917Selowe * CEs or FMA requests, we fail leaving the page in service. The page will 1021917Selowe * eventually be tried again later. We always return with the page unlocked 1022917Selowe * since we are called from page_unlock(). 1023917Selowe * 1024917Selowe * We don't call panic or do anything fancy down in here. Our boss the DE 1025917Selowe * gets paid handsomely to do his job of figuring out what to do when errors 1026917Selowe * occur. We just do what he tells us to do. 1027917Selowe */ 1028917Selowe static int 1029917Selowe page_retire_pp(page_t *pp) 1030917Selowe { 1031917Selowe int toxic; 1032917Selowe 1033917Selowe ASSERT(PAGE_EXCL(pp)); 1034917Selowe ASSERT(pp->p_iolock_state == 0); 1035917Selowe ASSERT(pp->p_szc == 0); 1036917Selowe 1037917Selowe PR_DEBUG(prd_top); 1038917Selowe PR_TYPES(pp); 1039917Selowe 1040917Selowe toxic = pp->p_toxic; 1041917Selowe ASSERT(toxic & PR_REASONS); 1042917Selowe 1043917Selowe if ((toxic & (PR_FMA | PR_MCE)) && !(toxic & PR_UE) && 1044917Selowe page_retire_limit()) { 1045917Selowe page_clrtoxic(pp, PR_FMA | PR_MCE | PR_MSG | PR_BUSY); 1046917Selowe page_retire_dequeue(pp); 1047917Selowe page_unlock(pp); 1048917Selowe return (page_retire_done(pp, PRD_LIMIT)); 1049917Selowe } 1050917Selowe 1051917Selowe if (PP_ISFREE(pp)) { 1052*1338Selowe int dbgnoreclaim = MTBF(recl_calls, recl_mtbf) == 0; 1053*1338Selowe 1054917Selowe PR_DEBUG(prd_free); 1055*1338Selowe 1056*1338Selowe if (dbgnoreclaim || !page_reclaim(pp, NULL)) { 1057917Selowe PR_DEBUG(prd_noreclaim); 1058917Selowe PR_INCR_KSTAT(pr_failed); 1059*1338Selowe /* 1060*1338Selowe * page_reclaim() returns with `pp' unlocked when 1061*1338Selowe * it fails. 1062*1338Selowe */ 1063*1338Selowe if (dbgnoreclaim) 1064*1338Selowe page_unlock(pp); 1065917Selowe return (page_retire_done(pp, PRD_FAILED)); 1066917Selowe } 1067917Selowe } 1068*1338Selowe ASSERT(!PP_ISFREE(pp)); 1069917Selowe 1070*1338Selowe if ((toxic & PR_UE) == 0 && pp->p_vnode && !PP_ISNORELOCKERNEL(pp) && 1071*1338Selowe MTBF(reloc_calls, reloc_mtbf)) { 1072917Selowe page_t *newpp; 1073917Selowe spgcnt_t count; 1074917Selowe 1075917Selowe /* 1076917Selowe * If we can relocate the page, great! newpp will go 1077917Selowe * on without us, and everything is fine. Regardless 1078917Selowe * of whether the relocation succeeds, we are still 1079917Selowe * going to take `pp' around back and shoot it. 1080917Selowe */ 1081917Selowe newpp = NULL; 1082917Selowe if (page_relocate(&pp, &newpp, 0, 0, &count, NULL) == 0) { 1083973Selowe PR_DEBUG(prd_reloc); 1084917Selowe page_unlock(newpp); 1085917Selowe ASSERT(hat_page_getattr(pp, P_MOD) == 0); 1086973Selowe } else { 1087973Selowe PR_DEBUG(prd_relocfail); 1088917Selowe } 1089917Selowe } 1090917Selowe 1091973Selowe if (hat_ismod(pp)) { 1092973Selowe PR_DEBUG(prd_mod); 1093973Selowe PR_INCR_KSTAT(pr_failed); 1094973Selowe page_unlock(pp); 1095973Selowe return (page_retire_done(pp, PRD_FAILED)); 1096973Selowe } 1097973Selowe 1098973Selowe if (PP_ISKVP(pp)) { 1099917Selowe PR_DEBUG(prd_kern); 1100917Selowe PR_INCR_KSTAT(pr_failed_kernel); 1101917Selowe page_unlock(pp); 1102917Selowe return (page_retire_done(pp, PRD_FAILED)); 1103917Selowe } 1104917Selowe 1105917Selowe if (pp->p_lckcnt || pp->p_cowcnt) { 1106973Selowe PR_DEBUG(prd_locked); 1107973Selowe PR_INCR_KSTAT(pr_failed); 1108973Selowe page_unlock(pp); 1109973Selowe return (page_retire_done(pp, PRD_FAILED)); 1110917Selowe } 1111917Selowe 1112917Selowe (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 1113917Selowe ASSERT(!hat_page_is_mapped(pp)); 1114917Selowe 1115917Selowe /* 1116973Selowe * If the page is modified, and was not relocated; we can't 1117973Selowe * retire it without dropping data on the floor. We have to 1118973Selowe * recheck after unloading since the dirty bit could have been 1119973Selowe * set since we last checked. 1120917Selowe */ 1121917Selowe if (hat_ismod(pp)) { 1122973Selowe PR_DEBUG(prd_mod_late); 1123973Selowe PR_INCR_KSTAT(pr_failed); 1124973Selowe page_unlock(pp); 1125973Selowe return (page_retire_done(pp, PRD_FAILED)); 1126917Selowe } 1127917Selowe 1128917Selowe if (pp->p_vnode) { 1129917Selowe PR_DEBUG(prd_hashout); 1130917Selowe page_hashout(pp, NULL); 1131917Selowe } 1132917Selowe ASSERT(!pp->p_vnode); 1133917Selowe 1134917Selowe /* 1135917Selowe * The problem page is locked, demoted, unmapped, not free, 1136917Selowe * hashed out, and not COW or mlocked (whew!). 1137917Selowe * 1138917Selowe * Now we select our ammunition, take it around back, and shoot it. 1139917Selowe */ 1140917Selowe if (toxic & PR_UE) { 1141917Selowe if (page_retire_transient_ue(pp)) { 1142917Selowe PR_DEBUG(prd_uescrubbed); 1143917Selowe return (page_retire_done(pp, PRD_UE_SCRUBBED)); 1144917Selowe } else { 1145917Selowe PR_DEBUG(prd_uenotscrubbed); 1146917Selowe page_retire_destroy(pp); 1147917Selowe return (page_retire_done(pp, PRD_SUCCESS)); 1148917Selowe } 1149917Selowe } else if (toxic & PR_FMA) { 1150917Selowe PR_DEBUG(prd_fma); 1151917Selowe page_retire_destroy(pp); 1152917Selowe return (page_retire_done(pp, PRD_SUCCESS)); 1153917Selowe } else if (toxic & PR_MCE) { 1154917Selowe PR_DEBUG(prd_mce); 1155917Selowe page_retire_destroy(pp); 1156917Selowe return (page_retire_done(pp, PRD_SUCCESS)); 1157917Selowe } 1158917Selowe panic("page_retire_pp: bad toxic flags %d", toxic); 1159917Selowe /*NOTREACHED*/ 1160917Selowe } 1161917Selowe 1162917Selowe /* 1163917Selowe * Try to retire a page when we stumble onto it in the page lock routines. 1164917Selowe */ 1165917Selowe void 1166917Selowe page_tryretire(page_t *pp) 1167917Selowe { 1168917Selowe ASSERT(PAGE_EXCL(pp)); 1169917Selowe 1170917Selowe if (!pr_enable) { 1171917Selowe page_unlock(pp); 1172917Selowe return; 1173917Selowe } 1174917Selowe 1175917Selowe /* 1176917Selowe * If the page is a big page, try to break it up. 1177917Selowe * 1178917Selowe * If there are other bad pages besides `pp', they will be 1179917Selowe * recursively retired for us thanks to a bit of magic. 1180917Selowe * If the page is a small page with errors, try to retire it. 1181917Selowe */ 1182917Selowe if (pp->p_szc > 0) { 1183917Selowe if (PP_ISFREE(pp) && !page_try_demote_free_pages(pp)) { 1184917Selowe page_unlock(pp); 1185917Selowe PR_DEBUG(prd_nofreedemote); 1186917Selowe return; 1187917Selowe } else if (!page_try_demote_pages(pp)) { 1188917Selowe page_unlock(pp); 1189917Selowe PR_DEBUG(prd_nodemote); 1190917Selowe return; 1191917Selowe } 1192917Selowe PR_DEBUG(prd_demoted); 1193917Selowe page_unlock(pp); 1194917Selowe } else { 1195917Selowe (void) page_retire_pp(pp); 1196917Selowe } 1197917Selowe } 1198917Selowe 1199917Selowe /* 1200917Selowe * page_retire() - the front door in to retire a page. 1201917Selowe * 1202917Selowe * Ideally, page_retire() would instantly retire the requested page. 1203917Selowe * Unfortunately, some pages are locked or otherwise tied up and cannot be 1204917Selowe * retired right away. To deal with that, bits are set in p_toxic of the 1205917Selowe * page_t. An attempt is made to lock the page; if the attempt is successful, 1206917Selowe * we instantly unlock the page counting on page_unlock() to notice p_toxic 1207917Selowe * is nonzero and to call back into page_retire_pp(). Success is determined 1208917Selowe * by looking to see whether the page has been retired once it has been 1209917Selowe * unlocked. 1210917Selowe * 1211917Selowe * Returns: 1212917Selowe * 1213917Selowe * - 0 on success, 1214917Selowe * - EINVAL when the PA is whacko, 1215917Selowe * - EBUSY if the page is already retired, or 1216917Selowe * - EAGAIN if the page could not be _immediately_ retired. 1217917Selowe */ 1218917Selowe int 1219917Selowe page_retire(uint64_t pa, uchar_t reason) 1220917Selowe { 1221917Selowe page_t *pp; 1222917Selowe 1223917Selowe ASSERT(reason & PR_REASONS); /* there must be a reason */ 1224917Selowe ASSERT(!(reason & ~PR_REASONS)); /* but no other bits */ 1225917Selowe 1226917Selowe pp = page_numtopp_nolock(mmu_btop(pa)); 1227917Selowe if (pp == NULL) { 1228917Selowe PR_MESSAGE(CE_WARN, 1, "Cannot schedule clearing of error on" 1229917Selowe " page 0x%08x.%08x; page is not relocatable memory", pa); 1230917Selowe return (page_retire_done(pp, PRD_INVALID_PA)); 1231917Selowe } 1232917Selowe if (PP_RETIRED(pp)) { 1233917Selowe return (page_retire_done(pp, PRD_DUPLICATE)); 1234917Selowe } 1235917Selowe 1236917Selowe if (reason & PR_UE) { 1237917Selowe PR_MESSAGE(CE_NOTE, 1, "Scheduling clearing of error on" 1238917Selowe " page 0x%08x.%08x", pa); 1239917Selowe } else { 1240917Selowe PR_MESSAGE(CE_NOTE, 1, "Scheduling removal of" 1241917Selowe " page 0x%08x.%08x", pa); 1242917Selowe } 1243917Selowe page_settoxic(pp, reason); 1244917Selowe page_retire_enqueue(pp); 1245917Selowe 1246917Selowe /* 1247917Selowe * And now for some magic. 1248917Selowe * 1249917Selowe * We marked this page toxic up above. All there is left to do is 1250917Selowe * to try to lock the page and then unlock it. The page lock routines 1251917Selowe * will intercept the page and retire it if they can. If the page 1252917Selowe * cannot be locked, 's okay -- page_unlock() will eventually get it, 1253917Selowe * or the background thread, until then the lock routines will deny 1254917Selowe * further locks on it. 1255917Selowe */ 1256917Selowe if (MTBF(pr_calls, pr_mtbf) && page_trylock(pp, SE_EXCL)) { 1257917Selowe PR_DEBUG(prd_prlocked); 1258917Selowe page_unlock(pp); 1259917Selowe } else { 1260917Selowe PR_DEBUG(prd_prnotlocked); 1261917Selowe } 1262917Selowe 1263917Selowe if (PP_RETIRED(pp)) { 1264917Selowe PR_DEBUG(prd_prretired); 1265917Selowe return (0); 1266917Selowe } else { 1267917Selowe cv_signal(&pr_cv); 1268917Selowe PR_INCR_KSTAT(pr_failed); 1269917Selowe 1270917Selowe if (pp->p_toxic & PR_MSG) { 1271917Selowe return (page_retire_done(pp, PRD_FAILED)); 1272917Selowe } else { 1273917Selowe return (page_retire_done(pp, PRD_PENDING)); 1274917Selowe } 1275917Selowe } 1276917Selowe } 1277917Selowe 1278917Selowe /* 1279917Selowe * Take a retired page off the retired-pages vnode and clear the toxic flags. 1280917Selowe * If "free" is nonzero, lock it and put it back on the freelist. If "free" 1281917Selowe * is zero, the caller already holds SE_EXCL lock so we simply unretire it 1282917Selowe * and don't do anything else with it. 1283917Selowe * 1284917Selowe * Any unretire messages are printed from this routine. 1285917Selowe * 1286917Selowe * Returns 0 if page pp was unretired; else an error code. 1287917Selowe */ 1288917Selowe int 1289917Selowe page_unretire_pp(page_t *pp, int free) 1290917Selowe { 1291917Selowe /* 1292917Selowe * To be retired, a page has to be hashed onto the retired_pages vnode 1293917Selowe * and have PR_RETIRED set in p_toxic. 1294917Selowe */ 1295917Selowe if (free == 0 || page_try_reclaim_lock(pp, SE_EXCL, SE_RETIRED)) { 1296917Selowe ASSERT(PAGE_EXCL(pp)); 1297917Selowe PR_DEBUG(prd_ulocked); 1298917Selowe if (!PP_RETIRED(pp)) { 1299917Selowe PR_DEBUG(prd_unotretired); 1300917Selowe page_unlock(pp); 1301917Selowe return (page_retire_done(pp, PRD_UNR_NOT)); 1302917Selowe } 1303917Selowe 1304917Selowe PR_MESSAGE(CE_NOTE, 1, "unretiring retired" 1305*1338Selowe " page 0x%08x.%08x", mmu_ptob((uint64_t)pp->p_pagenum)); 1306917Selowe if (pp->p_toxic & PR_FMA) { 1307917Selowe PR_DECR_KSTAT(pr_fma); 1308917Selowe } else if (pp->p_toxic & PR_UE) { 1309917Selowe PR_DECR_KSTAT(pr_ue); 1310917Selowe } else { 1311917Selowe PR_DECR_KSTAT(pr_mce); 1312917Selowe } 1313917Selowe page_clrtoxic(pp, PR_ALLFLAGS); 1314917Selowe 1315917Selowe if (free) { 1316917Selowe PR_DEBUG(prd_udestroy); 1317917Selowe page_destroy(pp, 0); 1318917Selowe } else { 1319917Selowe PR_DEBUG(prd_uhashout); 1320917Selowe page_hashout(pp, NULL); 1321917Selowe } 1322917Selowe 1323917Selowe mutex_enter(&freemem_lock); 1324917Selowe availrmem++; 1325917Selowe mutex_exit(&freemem_lock); 1326917Selowe 1327917Selowe PR_DEBUG(prd_uunretired); 1328917Selowe PR_DECR_KSTAT(pr_retired); 1329917Selowe PR_INCR_KSTAT(pr_unretired); 1330917Selowe return (page_retire_done(pp, PRD_UNR_SUCCESS)); 1331917Selowe } 1332917Selowe PR_DEBUG(prd_unotlocked); 1333917Selowe return (page_retire_done(pp, PRD_UNR_CANTLOCK)); 1334917Selowe } 1335917Selowe 1336917Selowe /* 1337917Selowe * Return a page to service by moving it from the retired_pages vnode 1338917Selowe * onto the freelist. 1339917Selowe * 1340917Selowe * Called from mmioctl_page_retire() on behalf of the FMA DE. 1341917Selowe * 1342917Selowe * Returns: 1343917Selowe * 1344917Selowe * - 0 if the page is unretired, 1345917Selowe * - EAGAIN if the pp can not be locked, 1346917Selowe * - EINVAL if the PA is whacko, and 1347917Selowe * - EBADF if the pp is not retired. 1348917Selowe */ 1349917Selowe int 1350917Selowe page_unretire(uint64_t pa) 1351917Selowe { 1352917Selowe page_t *pp; 1353917Selowe 1354917Selowe pp = page_numtopp_nolock(mmu_btop(pa)); 1355917Selowe if (pp == NULL) { 1356917Selowe return (page_retire_done(pp, PRD_INVALID_PA)); 1357917Selowe } 1358917Selowe 1359917Selowe return (page_unretire_pp(pp, 1)); 1360917Selowe } 1361917Selowe 1362917Selowe /* 1363917Selowe * Test a page to see if it is retired. If errors is non-NULL, the toxic 1364917Selowe * bits of the page are returned. Returns 0 on success, error code on failure. 1365917Selowe */ 1366917Selowe int 1367917Selowe page_retire_check_pp(page_t *pp, uint64_t *errors) 1368917Selowe { 1369917Selowe int rc; 1370917Selowe 1371917Selowe if (PP_RETIRED(pp)) { 1372917Selowe PR_DEBUG(prd_checkhit); 1373917Selowe rc = 0; 1374917Selowe } else { 1375917Selowe PR_DEBUG(prd_checkmiss); 1376917Selowe rc = EAGAIN; 1377917Selowe } 1378917Selowe 1379917Selowe /* 1380917Selowe * We have magically arranged the bit values returned to fmd(1M) 1381917Selowe * to line up with the FMA, MCE, and UE bits of the page_t. 1382917Selowe */ 1383917Selowe if (errors) { 1384917Selowe uint64_t toxic = (uint64_t)(pp->p_toxic & PR_ERRMASK); 1385917Selowe if (toxic & PR_UE_SCRUBBED) { 1386917Selowe toxic &= ~PR_UE_SCRUBBED; 1387917Selowe toxic |= PR_UE; 1388917Selowe } 1389917Selowe *errors = toxic; 1390917Selowe } 1391917Selowe 1392917Selowe return (rc); 1393917Selowe } 1394917Selowe 1395917Selowe /* 1396917Selowe * Test to see if the page_t for a given PA is retired, and return the 1397917Selowe * hardware errors we have seen on the page if requested. 1398917Selowe * 1399917Selowe * Called from mmioctl_page_retire on behalf of the FMA DE. 1400917Selowe * 1401917Selowe * Returns: 1402917Selowe * 1403917Selowe * - 0 if the page is retired, 1404917Selowe * - EAGAIN if it is not, and 1405917Selowe * - EINVAL if the PA is whacko. 1406917Selowe */ 1407917Selowe int 1408917Selowe page_retire_check(uint64_t pa, uint64_t *errors) 1409917Selowe { 1410917Selowe page_t *pp; 1411917Selowe 1412917Selowe if (errors) { 1413917Selowe *errors = 0; 1414917Selowe } 1415917Selowe 1416917Selowe pp = page_numtopp_nolock(mmu_btop(pa)); 1417917Selowe if (pp == NULL) { 1418917Selowe return (page_retire_done(pp, PRD_INVALID_PA)); 1419917Selowe } 1420917Selowe 1421917Selowe return (page_retire_check_pp(pp, errors)); 1422917Selowe } 1423917Selowe 1424917Selowe /* 1425917Selowe * Page retire self-test. For now, it always returns 0. 1426917Selowe */ 1427917Selowe int 1428917Selowe page_retire_test(void) 1429917Selowe { 1430917Selowe page_t *first, *pp, *cpp, *cpp2, *lpp; 1431917Selowe 1432917Selowe /* 1433917Selowe * Tests the corner case where a large page can't be retired 1434917Selowe * because one of the constituent pages is locked. We mark 1435917Selowe * one page to be retired and try to retire it, and mark the 1436917Selowe * other page to be retired but don't try to retire it, so 1437917Selowe * that page_unlock() in the failure path will recurse and try 1438917Selowe * to retire THAT page. This is the worst possible situation 1439917Selowe * we can get ourselves into. 1440917Selowe */ 1441917Selowe memsegs_lock(0); 1442917Selowe pp = first = page_first(); 1443917Selowe do { 1444917Selowe if (pp->p_szc && PP_PAGEROOT(pp) == pp) { 1445917Selowe cpp = pp + 1; 1446917Selowe lpp = PP_ISFREE(pp)? pp : pp + 2; 1447917Selowe cpp2 = pp + 3; 1448917Selowe if (!page_trylock(lpp, pp == lpp? SE_EXCL : SE_SHARED)) 1449917Selowe continue; 1450917Selowe if (!page_trylock(cpp, SE_EXCL)) { 1451917Selowe page_unlock(lpp); 1452917Selowe continue; 1453917Selowe } 1454917Selowe page_settoxic(cpp, PR_FMA | PR_BUSY); 1455917Selowe page_settoxic(cpp2, PR_FMA); 1456917Selowe page_tryretire(cpp); /* will fail */ 1457917Selowe page_unlock(lpp); 1458917Selowe (void) page_retire(cpp->p_pagenum, PR_FMA); 1459917Selowe (void) page_retire(cpp2->p_pagenum, PR_FMA); 1460917Selowe } 1461917Selowe } while ((pp = page_next(pp)) != first); 1462917Selowe memsegs_unlock(0); 1463917Selowe 1464917Selowe return (0); 1465917Selowe } 1466