xref: /titanic_52/usr/src/uts/i86pc/os/memscrub.c (revision 98157a7002f4f2cf7978f3084ca5577f0a1d72b2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * i86pc Memory Scrubbing
31  *
32  * On detection of a correctable memory ECC error, the i86pc hardware
33  * returns the corrected data to the requester and may re-write it
34  * to memory (DRAM or NVRAM). Machines which do not re-write this to
35  * memory should add an NMI handler to correct and rewrite.
36  *
37  * Scrubbing thus reduces the likelyhood that multiple transient errors
38  * will occur in the same memory word, making uncorrectable errors due
39  * to transients less likely.
40  *
41  * Thus is born the desire that every memory location be periodically
42  * accessed.
43  *
44  * This file implements a memory scrubbing thread.  This scrubber
45  * guarantees that all of physical memory is accessed periodically
46  * (memscrub_period_sec -- 12 hours).
47  *
48  * It attempts to do this as unobtrusively as possible.  The thread
49  * schedules itself to wake up at an interval such that if it reads
50  * memscrub_span_pages (4MB) on each wakeup, it will read all of physical
51  * memory in in memscrub_period_sec (12 hours).
52  *
53  * The scrubber uses the REP LODS so it reads 4MB in 0.15 secs (on P5-200).
54  * When it completes a span, if all the CPUs are idle, it reads another span.
55  * Typically it soaks up idle time this way to reach its deadline early
56  * -- and sleeps until the next period begins.
57  *
58  * Maximal Cost Estimate:  8GB @ xxMB/s = xxx seconds spent in 640 wakeups
59  * that run for 0.15 seconds at intervals of 67 seconds.
60  *
61  * In practice, the scrubber finds enough idle time to finish in a few
62  * minutes, and sleeps until its 12 hour deadline.
63  *
64  * The scrubber maintains a private copy of the phys_install memory list
65  * to keep track of what memory should be scrubbed.
66  *
67  * The following parameters can be set via /etc/system
68  *
69  * memscrub_span_pages = MEMSCRUB_DFL_SPAN_PAGES (4MB)
70  * memscrub_period_sec = MEMSCRUB_DFL_PERIOD_SEC (12 hours)
71  * memscrub_thread_pri = MEMSCRUB_DFL_THREAD_PRI (0)
72  * memscrub_delay_start_sec = (10 seconds)
73  * disable_memscrub = (0)
74  *
75  * the scrubber will exit (or never be started) if it finds the variable
76  * "disable_memscrub" set.
77  *
78  * MEMSCRUB_DFL_SPAN_PAGES  is based on the guess that 0.15 sec
79  * is a "good" amount of minimum time for the thread to run at a time.
80  *
81  * MEMSCRUB_DFL_PERIOD_SEC (12 hours) is nearly a total guess --
82  * twice the frequency the hardware folk estimated would be necessary.
83  *
84  * MEMSCRUB_DFL_THREAD_PRI (0) is based on the assumption that nearly
85  * any other use of the system should be higher priority than scrubbing.
86  */
87 
88 #include <sys/types.h>
89 #include <sys/systm.h>		/* timeout, types, t_lock */
90 #include <sys/cmn_err.h>
91 #include <sys/sysmacros.h>	/* MIN */
92 #include <sys/memlist.h>	/* memlist */
93 #include <sys/kmem.h>		/* KMEM_NOSLEEP */
94 #include <sys/cpuvar.h>		/* ncpus_online */
95 #include <sys/debug.h>		/* ASSERTs */
96 #include <sys/vmem.h>
97 #include <sys/mman.h>
98 #include <vm/seg_kmem.h>
99 #include <vm/seg_kpm.h>
100 #include <vm/hat_i86.h>
101 #include <sys/callb.h>		/* CPR callback */
102 
103 static caddr_t	memscrub_window;
104 static hat_mempte_t memscrub_pte;
105 
106 /*
107  * Global Data:
108  */
109 /*
110  * scan all of physical memory at least once every MEMSCRUB_PERIOD_SEC
111  */
112 #define	MEMSCRUB_DFL_PERIOD_SEC	(12 * 60 * 60)	/* 12 hours */
113 
114 /*
115  * start only if at least MEMSCRUB_MIN_PAGES in system
116  */
117 #define	MEMSCRUB_MIN_PAGES	((32 * 1024 * 1024) / PAGESIZE)
118 
119 /*
120  * scan at least MEMSCRUB_DFL_SPAN_PAGES each iteration
121  */
122 #define	MEMSCRUB_DFL_SPAN_PAGES	((4 * 1024 * 1024) / PAGESIZE)
123 
124 /*
125  * almost anything is higher priority than scrubbing
126  */
127 #define	MEMSCRUB_DFL_THREAD_PRI	0
128 
129 /*
130  * we can patch these defaults in /etc/system if necessary
131  */
132 uint_t disable_memscrub = 0;
133 static uint_t disable_memscrub_quietly = 0;
134 pgcnt_t memscrub_min_pages = MEMSCRUB_MIN_PAGES;
135 pgcnt_t memscrub_span_pages = MEMSCRUB_DFL_SPAN_PAGES;
136 time_t memscrub_period_sec = MEMSCRUB_DFL_PERIOD_SEC;
137 uint_t memscrub_thread_pri = MEMSCRUB_DFL_THREAD_PRI;
138 time_t memscrub_delay_start_sec = 10;
139 
140 /*
141  * Static Routines
142  */
143 static void memscrubber(void);
144 static int system_is_idle(void);
145 static int memscrub_add_span(uint64_t, uint64_t);
146 
147 /*
148  * Static Data
149  */
150 static struct memlist *memscrub_memlist;
151 static uint_t memscrub_phys_pages;
152 
153 static kcondvar_t memscrub_cv;
154 static kmutex_t memscrub_lock;
155 
156 /*
157  * memscrub_lock protects memscrub_memlist
158  */
159 uint_t memscrub_scans_done;
160 
161 uint_t memscrub_done_early;
162 uint_t memscrub_early_sec;
163 
164 uint_t memscrub_done_late;
165 time_t memscrub_late_sec;
166 
167 /*
168  * create memscrub_memlist from phys_install list
169  * initialize locks, set memscrub_phys_pages.
170  */
171 void
172 memscrub_init()
173 {
174 	struct memlist *src;
175 
176 	if (physmem < memscrub_min_pages)
177 		return;
178 
179 	if (!kpm_enable) {
180 		memscrub_window = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
181 		memscrub_pte = hat_mempte_setup(memscrub_window);
182 	}
183 
184 	/*
185 	 * copy phys_install to memscrub_memlist
186 	 */
187 	for (src = phys_install; src; src = src->next) {
188 		if (memscrub_add_span(src->address, src->size)) {
189 			cmn_err(CE_WARN,
190 			    "Memory scrubber failed to initialize\n");
191 			return;
192 		}
193 	}
194 
195 	mutex_init(&memscrub_lock, NULL, MUTEX_DRIVER, NULL);
196 	cv_init(&memscrub_cv, NULL, CV_DRIVER, NULL);
197 
198 	/*
199 	 * create memscrubber thread
200 	 */
201 	(void) thread_create(NULL, 0, (void (*)())memscrubber, NULL, 0, &p0,
202 	    TS_RUN, memscrub_thread_pri);
203 }
204 
205 /*
206  * Function to cause the software memscrubber to exit quietly if the
207  * platform support has located a hardware scrubber and enabled it.
208  */
209 void
210 memscrub_disable(void)
211 {
212 	disable_memscrub_quietly = 1;
213 }
214 
215 #ifdef MEMSCRUB_DEBUG
216 void
217 memscrub_printmemlist(char *title, struct memlist *listp)
218 {
219 	struct memlist *list;
220 
221 	cmn_err(CE_CONT, "%s:\n", title);
222 
223 	for (list = listp; list; list = list->next) {
224 		cmn_err(CE_CONT, "addr = 0x%llx, size = 0x%llx\n",
225 		    list->address, list->size);
226 	}
227 }
228 #endif /* MEMSCRUB_DEBUG */
229 
230 /* ARGSUSED */
231 void
232 memscrub_wakeup(void *c)
233 {
234 	/*
235 	 * grab mutex to guarantee that our wakeup call
236 	 * arrives after we go to sleep -- so we can't sleep forever.
237 	 */
238 	mutex_enter(&memscrub_lock);
239 	cv_signal(&memscrub_cv);
240 	mutex_exit(&memscrub_lock);
241 }
242 
243 /*
244  * this calculation doesn't account for the time that the actual scan
245  * consumes -- so we'd fall slightly behind schedule with this
246  * interval_sec.  but the idle loop optimization below usually makes us
247  * come in way ahead of schedule.
248  */
249 static int
250 compute_interval_sec()
251 {
252 	if (memscrub_phys_pages <= memscrub_span_pages)
253 		return (memscrub_period_sec);
254 	else
255 		return (memscrub_period_sec/
256 		    (memscrub_phys_pages/memscrub_span_pages));
257 }
258 
259 void
260 memscrubber()
261 {
262 	time_t deadline;
263 	uint64_t mlp_last_addr;
264 	uint64_t mlp_next_addr;
265 	int reached_end = 1;
266 	time_t interval_sec = 0;
267 	struct memlist *mlp;
268 
269 	extern void scan_memory(caddr_t, size_t);
270 	callb_cpr_t cprinfo;
271 
272 	/*
273 	 * notify CPR of our existence
274 	 */
275 	CALLB_CPR_INIT(&cprinfo, &memscrub_lock, callb_generic_cpr, "memscrub");
276 
277 	if (memscrub_memlist == NULL) {
278 		cmn_err(CE_WARN, "memscrub_memlist not initialized.");
279 		goto memscrub_exit;
280 	}
281 
282 	mlp = memscrub_memlist;
283 	mlp_next_addr = mlp->address;
284 	mlp_last_addr = mlp->address + mlp->size;
285 
286 	deadline = gethrestime_sec() + memscrub_delay_start_sec;
287 
288 	for (;;) {
289 		if (disable_memscrub || disable_memscrub_quietly)
290 			break;
291 
292 		mutex_enter(&memscrub_lock);
293 
294 		/*
295 		 * did we just reach the end of memory?
296 		 */
297 		if (reached_end) {
298 			time_t now = gethrestime_sec();
299 
300 			if (now >= deadline) {
301 				memscrub_done_late++;
302 				memscrub_late_sec += (now - deadline);
303 				/*
304 				 * past deadline, start right away
305 				 */
306 				interval_sec = 0;
307 
308 				deadline = now + memscrub_period_sec;
309 			} else {
310 				/*
311 				 * we finished ahead of schedule.
312 				 * wait till previous dealine before re-start.
313 				 */
314 				interval_sec = deadline - now;
315 				memscrub_done_early++;
316 				memscrub_early_sec += interval_sec;
317 				deadline += memscrub_period_sec;
318 			}
319 		} else {
320 			interval_sec = compute_interval_sec();
321 		}
322 
323 		/*
324 		 * it is safe from our standpoint for CPR to
325 		 * suspend the system
326 		 */
327 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
328 
329 		/*
330 		 * hit the snooze bar
331 		 */
332 		(void) timeout(memscrub_wakeup, NULL, interval_sec * hz);
333 
334 		/*
335 		 * go to sleep
336 		 */
337 		cv_wait(&memscrub_cv, &memscrub_lock);
338 
339 		/* we need to goto work */
340 		CALLB_CPR_SAFE_END(&cprinfo, &memscrub_lock);
341 
342 		mutex_exit(&memscrub_lock);
343 
344 		do {
345 			pgcnt_t pages = memscrub_span_pages;
346 			uint64_t address = mlp_next_addr;
347 
348 			if (disable_memscrub || disable_memscrub_quietly)
349 				break;
350 
351 			mutex_enter(&memscrub_lock);
352 
353 			/*
354 			 * Make sure we don't try to scan beyond the end of
355 			 * the current memlist.  If we would, then resize
356 			 * our scan target for this iteration, and prepare
357 			 * to read the next memlist entry on the next
358 			 * iteration.
359 			 */
360 			reached_end = 0;
361 			if (address + mmu_ptob(pages) >= mlp_last_addr) {
362 				pages = mmu_btop(mlp_last_addr - address);
363 				mlp = mlp->next;
364 				if (mlp == NULL) {
365 					reached_end = 1;
366 					mlp = memscrub_memlist;
367 				}
368 				mlp_next_addr = mlp->address;
369 				mlp_last_addr = mlp->address + mlp->size;
370 			} else {
371 				mlp_next_addr += mmu_ptob(pages);
372 			}
373 
374 			mutex_exit(&memscrub_lock);
375 
376 			while (pages--) {
377 				pfn_t pfn = btop(address);
378 
379 				/*
380 				 * Without segkpm, the memscrubber cannot
381 				 * be allowed to migrate across CPUs, as
382 				 * the CPU-specific mapping of
383 				 * memscrub_window would be incorrect.
384 				 * With segkpm, switching CPUs is legal, but
385 				 * inefficient.  We don't use
386 				 * kpreempt_disable as it might hold a
387 				 * higher priority thread (eg, RT) too long
388 				 * off CPU.
389 				 */
390 				thread_affinity_set(curthread, CPU_CURRENT);
391 				if (kpm_enable)
392 					memscrub_window = hat_kpm_pfn2va(pfn);
393 				else
394 					hat_mempte_remap(pfn, memscrub_window,
395 					    memscrub_pte,
396 					    PROT_READ, HAT_LOAD_NOCONSIST);
397 
398 				scan_memory(memscrub_window, PAGESIZE);
399 
400 				thread_affinity_clear(curthread);
401 				address += MMU_PAGESIZE;
402 			}
403 
404 			memscrub_scans_done++;
405 		} while (!reached_end && system_is_idle());
406 	}
407 
408 memscrub_exit:
409 
410 	if (!disable_memscrub_quietly)
411 		cmn_err(CE_NOTE, "memory scrubber exiting.");
412 	/*
413 	 * We are about to bail, but don't have the memscrub_lock,
414 	 * and it is needed for CALLB_CPR_EXIT.
415 	 */
416 	mutex_enter(&memscrub_lock);
417 	CALLB_CPR_EXIT(&cprinfo);
418 
419 	cv_destroy(&memscrub_cv);
420 
421 	thread_exit();
422 }
423 
424 
425 /*
426  * return 1 if we're MP and all the other CPUs are idle
427  */
428 static int
429 system_is_idle()
430 {
431 	int cpu_id;
432 	int found = 0;
433 
434 	if (1 == ncpus_online)
435 		return (0);
436 
437 	for (cpu_id = 0; cpu_id < NCPU; ++cpu_id) {
438 		if (!cpu[cpu_id])
439 			continue;
440 
441 		found++;
442 
443 		if (cpu[cpu_id]->cpu_thread != cpu[cpu_id]->cpu_idle_thread) {
444 			if (CPU->cpu_id == cpu_id &&
445 			    CPU->cpu_disp->disp_nrunnable == 0)
446 				continue;
447 			return (0);
448 		}
449 
450 		if (found == ncpus)
451 			break;
452 	}
453 	return (1);
454 }
455 
456 /*
457  * add a span to the memscrub list
458  */
459 static int
460 memscrub_add_span(uint64_t start, uint64_t bytes)
461 {
462 	struct memlist *dst;
463 	struct memlist *prev, *next;
464 	uint64_t end = start + bytes - 1;
465 	int retval = 0;
466 
467 	mutex_enter(&memscrub_lock);
468 
469 #ifdef MEMSCRUB_DEBUG
470 	memscrub_printmemlist("memscrub_memlist before", memscrub_memlist);
471 	cmn_err(CE_CONT, "memscrub_phys_pages: 0x%x\n", memscrub_phys_pages);
472 	cmn_err(CE_CONT, "memscrub_add_span: address: 0x%llx"
473 	    " size: 0x%llx\n", start, bytes);
474 #endif /* MEMSCRUB_DEBUG */
475 
476 	/*
477 	 * Scan through the list to find the proper place to install it.
478 	 */
479 	prev = NULL;
480 	next = memscrub_memlist;
481 	while (next) {
482 		uint64_t ns = next->address;
483 		uint64_t ne = next->address + next->size - 1;
484 
485 		/*
486 		 * If this span overlaps with an existing span, then
487 		 * something has gone horribly wrong with the phys_install
488 		 * list.  In fact, I'm surprised we made it this far.
489 		 */
490 		if ((start >= ns && start <= ne) || (end >= ns && end <= ne) ||
491 		    (start < ns && end > ne))
492 			panic("memscrub found overlapping memory ranges "
493 			    "(0x%p-0x%p) and (0x%p-0x%p)",
494 			    (void *)(uintptr_t)start, (void *)(uintptr_t)end,
495 			    (void *)(uintptr_t)ns, (void *)(uintptr_t)ne);
496 
497 		/*
498 		 * New span can be appended to an existing one.
499 		 */
500 		if (start == ne + 1) {
501 			next->size += bytes;
502 			goto add_done;
503 		}
504 
505 		/*
506 		 * New span can be prepended to an existing one.
507 		 */
508 		if (end + 1 == ns) {
509 			next->size += bytes;
510 			next->address = start;
511 			goto add_done;
512 		}
513 
514 		/*
515 		 * If the next span has a higher start address than the new
516 		 * one, then we have found the right spot for our
517 		 * insertion.
518 		 */
519 		if (ns > start)
520 			break;
521 
522 		prev = next;
523 		next = next->next;
524 	}
525 
526 	/*
527 	 * allocate a new struct memlist
528 	 */
529 	dst = kmem_alloc(sizeof (struct memlist), KM_NOSLEEP);
530 	if (dst == NULL) {
531 		retval = -1;
532 		goto add_done;
533 	}
534 	dst->address = start;
535 	dst->size = bytes;
536 	dst->prev = prev;
537 	dst->next = next;
538 
539 	if (prev)
540 		prev->next = dst;
541 	else
542 		memscrub_memlist = dst;
543 
544 	if (next)
545 		next->prev = dst;
546 
547 add_done:
548 
549 	if (retval != -1)
550 		memscrub_phys_pages += mmu_btop(bytes);
551 
552 #ifdef MEMSCRUB_DEBUG
553 	memscrub_printmemlist("memscrub_memlist after", memscrub_memlist);
554 	cmn_err(CE_CONT, "memscrub_phys_pages: 0x%x\n", memscrub_phys_pages);
555 #endif /* MEMSCRUB_DEBUG */
556 
557 	mutex_exit(&memscrub_lock);
558 	return (retval);
559 }
560