xref: /openbmc/linux/drivers/ras/cec.c (revision ec2da07c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2017-2019 Borislav Petkov, SUSE Labs.
4  */
5 #include <linux/mm.h>
6 #include <linux/gfp.h>
7 #include <linux/kernel.h>
8 #include <linux/workqueue.h>
9 
10 #include <asm/mce.h>
11 
12 #include "debugfs.h"
13 
14 /*
15  * RAS Correctable Errors Collector
16  *
17  * This is a simple gadget which collects correctable errors and counts their
18  * occurrence per physical page address.
19  *
20  * We've opted for possibly the simplest data structure to collect those - an
21  * array of the size of a memory page. It stores 512 u64's with the following
22  * structure:
23  *
24  * [63 ... PFN ... 12 | 11 ... generation ... 10 | 9 ... count ... 0]
25  *
26  * The generation in the two highest order bits is two bits which are set to 11b
27  * on every insertion. During the course of each entry's existence, the
28  * generation field gets decremented during spring cleaning to 10b, then 01b and
29  * then 00b.
30  *
31  * This way we're employing the natural numeric ordering to make sure that newly
32  * inserted/touched elements have higher 12-bit counts (which we've manufactured)
33  * and thus iterating over the array initially won't kick out those elements
34  * which were inserted last.
35  *
36  * Spring cleaning is what we do when we reach a certain number CLEAN_ELEMS of
37  * elements entered into the array, during which, we're decaying all elements.
38  * If, after decay, an element gets inserted again, its generation is set to 11b
39  * to make sure it has higher numerical count than other, older elements and
40  * thus emulate an an LRU-like behavior when deleting elements to free up space
41  * in the page.
42  *
43  * When an element reaches it's max count of action_threshold, we try to poison
44  * it by assuming that errors triggered action_threshold times in a single page
45  * are excessive and that page shouldn't be used anymore. action_threshold is
46  * initialized to COUNT_MASK which is the maximum.
47  *
48  * That error event entry causes cec_add_elem() to return !0 value and thus
49  * signal to its callers to log the error.
50  *
51  * To the question why we've chosen a page and moving elements around with
52  * memmove(), it is because it is a very simple structure to handle and max data
53  * movement is 4K which on highly optimized modern CPUs is almost unnoticeable.
54  * We wanted to avoid the pointer traversal of more complex structures like a
55  * linked list or some sort of a balancing search tree.
56  *
57  * Deleting an element takes O(n) but since it is only a single page, it should
58  * be fast enough and it shouldn't happen all too often depending on error
59  * patterns.
60  */
61 
62 #undef pr_fmt
63 #define pr_fmt(fmt) "RAS: " fmt
64 
65 /*
66  * We use DECAY_BITS bits of PAGE_SHIFT bits for counting decay, i.e., how long
67  * elements have stayed in the array without having been accessed again.
68  */
69 #define DECAY_BITS		2
70 #define DECAY_MASK		((1ULL << DECAY_BITS) - 1)
71 #define MAX_ELEMS		(PAGE_SIZE / sizeof(u64))
72 
73 /*
74  * Threshold amount of inserted elements after which we start spring
75  * cleaning.
76  */
77 #define CLEAN_ELEMS		(MAX_ELEMS >> DECAY_BITS)
78 
79 /* Bits which count the number of errors happened in this 4K page. */
80 #define COUNT_BITS		(PAGE_SHIFT - DECAY_BITS)
81 #define COUNT_MASK		((1ULL << COUNT_BITS) - 1)
82 #define FULL_COUNT_MASK		(PAGE_SIZE - 1)
83 
84 /*
85  * u64: [ 63 ... 12 | DECAY_BITS | COUNT_BITS ]
86  */
87 
88 #define PFN(e)			((e) >> PAGE_SHIFT)
89 #define DECAY(e)		(((e) >> COUNT_BITS) & DECAY_MASK)
90 #define COUNT(e)		((unsigned int)(e) & COUNT_MASK)
91 #define FULL_COUNT(e)		((e) & (PAGE_SIZE - 1))
92 
93 static struct ce_array {
94 	u64 *array;			/* container page */
95 	unsigned int n;			/* number of elements in the array */
96 
97 	unsigned int decay_count;	/*
98 					 * number of element insertions/increments
99 					 * since the last spring cleaning.
100 					 */
101 
102 	u64 pfns_poisoned;		/*
103 					 * number of PFNs which got poisoned.
104 					 */
105 
106 	u64 ces_entered;		/*
107 					 * The number of correctable errors
108 					 * entered into the collector.
109 					 */
110 
111 	u64 decays_done;		/*
112 					 * Times we did spring cleaning.
113 					 */
114 
115 	union {
116 		struct {
117 			__u32	disabled : 1,	/* cmdline disabled */
118 			__resv   : 31;
119 		};
120 		__u32 flags;
121 	};
122 } ce_arr;
123 
124 static DEFINE_MUTEX(ce_mutex);
125 static u64 dfs_pfn;
126 
127 /* Amount of errors after which we offline */
128 static u64 action_threshold = COUNT_MASK;
129 
130 /* Each element "decays" each decay_interval which is 24hrs by default. */
131 #define CEC_DECAY_DEFAULT_INTERVAL	24 * 60 * 60	/* 24 hrs */
132 #define CEC_DECAY_MIN_INTERVAL		 1 * 60 * 60	/* 1h */
133 #define CEC_DECAY_MAX_INTERVAL	   30 *	24 * 60 * 60	/* one month */
134 static struct delayed_work cec_work;
135 static u64 decay_interval = CEC_DECAY_DEFAULT_INTERVAL;
136 
137 /*
138  * Decrement decay value. We're using DECAY_BITS bits to denote decay of an
139  * element in the array. On insertion and any access, it gets reset to max.
140  */
141 static void do_spring_cleaning(struct ce_array *ca)
142 {
143 	int i;
144 
145 	for (i = 0; i < ca->n; i++) {
146 		u8 decay = DECAY(ca->array[i]);
147 
148 		if (!decay)
149 			continue;
150 
151 		decay--;
152 
153 		ca->array[i] &= ~(DECAY_MASK << COUNT_BITS);
154 		ca->array[i] |= (decay << COUNT_BITS);
155 	}
156 	ca->decay_count = 0;
157 	ca->decays_done++;
158 }
159 
160 /*
161  * @interval in seconds
162  */
163 static void cec_mod_work(unsigned long interval)
164 {
165 	unsigned long iv;
166 
167 	iv = interval * HZ;
168 	mod_delayed_work(system_wq, &cec_work, round_jiffies(iv));
169 }
170 
171 static void cec_work_fn(struct work_struct *work)
172 {
173 	mutex_lock(&ce_mutex);
174 	do_spring_cleaning(&ce_arr);
175 	mutex_unlock(&ce_mutex);
176 
177 	cec_mod_work(decay_interval);
178 }
179 
180 /*
181  * @to: index of the smallest element which is >= then @pfn.
182  *
183  * Return the index of the pfn if found, otherwise negative value.
184  */
185 static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
186 {
187 	int min = 0, max = ca->n - 1;
188 	u64 this_pfn;
189 
190 	while (min <= max) {
191 		int i = (min + max) >> 1;
192 
193 		this_pfn = PFN(ca->array[i]);
194 
195 		if (this_pfn < pfn)
196 			min = i + 1;
197 		else if (this_pfn > pfn)
198 			max = i - 1;
199 		else if (this_pfn == pfn) {
200 			if (to)
201 				*to = i;
202 
203 			return i;
204 		}
205 	}
206 
207 	/*
208 	 * When the loop terminates without finding @pfn, min has the index of
209 	 * the element slot where the new @pfn should be inserted. The loop
210 	 * terminates when min > max, which means the min index points to the
211 	 * bigger element while the max index to the smaller element, in-between
212 	 * which the new @pfn belongs to.
213 	 *
214 	 * For more details, see exercise 1, Section 6.2.1 in TAOCP, vol. 3.
215 	 */
216 	if (to)
217 		*to = min;
218 
219 	return -ENOKEY;
220 }
221 
222 static int find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
223 {
224 	WARN_ON(!to);
225 
226 	if (!ca->n) {
227 		*to = 0;
228 		return -ENOKEY;
229 	}
230 	return __find_elem(ca, pfn, to);
231 }
232 
233 static void del_elem(struct ce_array *ca, int idx)
234 {
235 	/* Save us a function call when deleting the last element. */
236 	if (ca->n - (idx + 1))
237 		memmove((void *)&ca->array[idx],
238 			(void *)&ca->array[idx + 1],
239 			(ca->n - (idx + 1)) * sizeof(u64));
240 
241 	ca->n--;
242 }
243 
244 static u64 del_lru_elem_unlocked(struct ce_array *ca)
245 {
246 	unsigned int min = FULL_COUNT_MASK;
247 	int i, min_idx = 0;
248 
249 	for (i = 0; i < ca->n; i++) {
250 		unsigned int this = FULL_COUNT(ca->array[i]);
251 
252 		if (min > this) {
253 			min = this;
254 			min_idx = i;
255 		}
256 	}
257 
258 	del_elem(ca, min_idx);
259 
260 	return PFN(ca->array[min_idx]);
261 }
262 
263 /*
264  * We return the 0th pfn in the error case under the assumption that it cannot
265  * be poisoned and excessive CEs in there are a serious deal anyway.
266  */
267 static u64 __maybe_unused del_lru_elem(void)
268 {
269 	struct ce_array *ca = &ce_arr;
270 	u64 pfn;
271 
272 	if (!ca->n)
273 		return 0;
274 
275 	mutex_lock(&ce_mutex);
276 	pfn = del_lru_elem_unlocked(ca);
277 	mutex_unlock(&ce_mutex);
278 
279 	return pfn;
280 }
281 
282 static bool sanity_check(struct ce_array *ca)
283 {
284 	bool ret = false;
285 	u64 prev = 0;
286 	int i;
287 
288 	for (i = 0; i < ca->n; i++) {
289 		u64 this = PFN(ca->array[i]);
290 
291 		if (WARN(prev > this, "prev: 0x%016llx <-> this: 0x%016llx\n", prev, this))
292 			ret = true;
293 
294 		prev = this;
295 	}
296 
297 	if (!ret)
298 		return ret;
299 
300 	pr_info("Sanity check dump:\n{ n: %d\n", ca->n);
301 	for (i = 0; i < ca->n; i++) {
302 		u64 this = PFN(ca->array[i]);
303 
304 		pr_info(" %03d: [%016llx|%03llx]\n", i, this, FULL_COUNT(ca->array[i]));
305 	}
306 	pr_info("}\n");
307 
308 	return ret;
309 }
310 
311 int cec_add_elem(u64 pfn)
312 {
313 	struct ce_array *ca = &ce_arr;
314 	unsigned int to = 0;
315 	int count, ret = 0;
316 
317 	/*
318 	 * We can be called very early on the identify_cpu() path where we are
319 	 * not initialized yet. We ignore the error for simplicity.
320 	 */
321 	if (!ce_arr.array || ce_arr.disabled)
322 		return -ENODEV;
323 
324 	mutex_lock(&ce_mutex);
325 
326 	ca->ces_entered++;
327 
328 	/* Array full, free the LRU slot. */
329 	if (ca->n == MAX_ELEMS)
330 		WARN_ON(!del_lru_elem_unlocked(ca));
331 
332 	ret = find_elem(ca, pfn, &to);
333 	if (ret < 0) {
334 		/*
335 		 * Shift range [to-end] to make room for one more element.
336 		 */
337 		memmove((void *)&ca->array[to + 1],
338 			(void *)&ca->array[to],
339 			(ca->n - to) * sizeof(u64));
340 
341 		ca->array[to] = pfn << PAGE_SHIFT;
342 		ca->n++;
343 	}
344 
345 	/* Add/refresh element generation and increment count */
346 	ca->array[to] |= DECAY_MASK << COUNT_BITS;
347 	ca->array[to]++;
348 
349 	/* Check action threshold and soft-offline, if reached. */
350 	count = COUNT(ca->array[to]);
351 	if (count >= action_threshold) {
352 		u64 pfn = ca->array[to] >> PAGE_SHIFT;
353 
354 		if (!pfn_valid(pfn)) {
355 			pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn);
356 		} else {
357 			/* We have reached max count for this page, soft-offline it. */
358 			pr_err("Soft-offlining pfn: 0x%llx\n", pfn);
359 			memory_failure_queue(pfn, MF_SOFT_OFFLINE);
360 			ca->pfns_poisoned++;
361 		}
362 
363 		del_elem(ca, to);
364 
365 		/*
366 		 * Return a >0 value to callers, to denote that we've reached
367 		 * the offlining threshold.
368 		 */
369 		ret = 1;
370 
371 		goto unlock;
372 	}
373 
374 	ca->decay_count++;
375 
376 	if (ca->decay_count >= CLEAN_ELEMS)
377 		do_spring_cleaning(ca);
378 
379 	WARN_ON_ONCE(sanity_check(ca));
380 
381 unlock:
382 	mutex_unlock(&ce_mutex);
383 
384 	return ret;
385 }
386 
387 static int u64_get(void *data, u64 *val)
388 {
389 	*val = *(u64 *)data;
390 
391 	return 0;
392 }
393 
394 static int pfn_set(void *data, u64 val)
395 {
396 	*(u64 *)data = val;
397 
398 	cec_add_elem(val);
399 
400 	return 0;
401 }
402 
403 DEFINE_DEBUGFS_ATTRIBUTE(pfn_ops, u64_get, pfn_set, "0x%llx\n");
404 
405 static int decay_interval_set(void *data, u64 val)
406 {
407 	if (val < CEC_DECAY_MIN_INTERVAL)
408 		return -EINVAL;
409 
410 	if (val > CEC_DECAY_MAX_INTERVAL)
411 		return -EINVAL;
412 
413 	*(u64 *)data   = val;
414 	decay_interval = val;
415 
416 	cec_mod_work(decay_interval);
417 
418 	return 0;
419 }
420 DEFINE_DEBUGFS_ATTRIBUTE(decay_interval_ops, u64_get, decay_interval_set, "%lld\n");
421 
422 static int action_threshold_set(void *data, u64 val)
423 {
424 	*(u64 *)data = val;
425 
426 	if (val > COUNT_MASK)
427 		val = COUNT_MASK;
428 
429 	action_threshold = val;
430 
431 	return 0;
432 }
433 DEFINE_DEBUGFS_ATTRIBUTE(action_threshold_ops, u64_get, action_threshold_set, "%lld\n");
434 
435 static const char * const bins[] = { "00", "01", "10", "11" };
436 
437 static int array_dump(struct seq_file *m, void *v)
438 {
439 	struct ce_array *ca = &ce_arr;
440 	int i;
441 
442 	mutex_lock(&ce_mutex);
443 
444 	seq_printf(m, "{ n: %d\n", ca->n);
445 	for (i = 0; i < ca->n; i++) {
446 		u64 this = PFN(ca->array[i]);
447 
448 		seq_printf(m, " %3d: [%016llx|%s|%03llx]\n",
449 			   i, this, bins[DECAY(ca->array[i])], COUNT(ca->array[i]));
450 	}
451 
452 	seq_printf(m, "}\n");
453 
454 	seq_printf(m, "Stats:\nCEs: %llu\nofflined pages: %llu\n",
455 		   ca->ces_entered, ca->pfns_poisoned);
456 
457 	seq_printf(m, "Flags: 0x%x\n", ca->flags);
458 
459 	seq_printf(m, "Decay interval: %lld seconds\n", decay_interval);
460 	seq_printf(m, "Decays: %lld\n", ca->decays_done);
461 
462 	seq_printf(m, "Action threshold: %lld\n", action_threshold);
463 
464 	mutex_unlock(&ce_mutex);
465 
466 	return 0;
467 }
468 
469 static int array_open(struct inode *inode, struct file *filp)
470 {
471 	return single_open(filp, array_dump, NULL);
472 }
473 
474 static const struct file_operations array_ops = {
475 	.owner	 = THIS_MODULE,
476 	.open	 = array_open,
477 	.read	 = seq_read,
478 	.llseek	 = seq_lseek,
479 	.release = single_release,
480 };
481 
482 static int __init create_debugfs_nodes(void)
483 {
484 	struct dentry *d, *pfn, *decay, *count, *array;
485 
486 	d = debugfs_create_dir("cec", ras_debugfs_dir);
487 	if (!d) {
488 		pr_warn("Error creating cec debugfs node!\n");
489 		return -1;
490 	}
491 
492 	decay = debugfs_create_file("decay_interval", S_IRUSR | S_IWUSR, d,
493 				    &decay_interval, &decay_interval_ops);
494 	if (!decay) {
495 		pr_warn("Error creating decay_interval debugfs node!\n");
496 		goto err;
497 	}
498 
499 	count = debugfs_create_file("action_threshold", S_IRUSR | S_IWUSR, d,
500 				    &action_threshold, &action_threshold_ops);
501 	if (!count) {
502 		pr_warn("Error creating action_threshold debugfs node!\n");
503 		goto err;
504 	}
505 
506 	if (!IS_ENABLED(CONFIG_RAS_CEC_DEBUG))
507 		return 0;
508 
509 	pfn = debugfs_create_file("pfn", S_IRUSR | S_IWUSR, d, &dfs_pfn, &pfn_ops);
510 	if (!pfn) {
511 		pr_warn("Error creating pfn debugfs node!\n");
512 		goto err;
513 	}
514 
515 	array = debugfs_create_file("array", S_IRUSR, d, NULL, &array_ops);
516 	if (!array) {
517 		pr_warn("Error creating array debugfs node!\n");
518 		goto err;
519 	}
520 
521 	return 0;
522 
523 err:
524 	debugfs_remove_recursive(d);
525 
526 	return 1;
527 }
528 
529 void __init cec_init(void)
530 {
531 	if (ce_arr.disabled)
532 		return;
533 
534 	ce_arr.array = (void *)get_zeroed_page(GFP_KERNEL);
535 	if (!ce_arr.array) {
536 		pr_err("Error allocating CE array page!\n");
537 		return;
538 	}
539 
540 	if (create_debugfs_nodes()) {
541 		free_page((unsigned long)ce_arr.array);
542 		return;
543 	}
544 
545 	INIT_DELAYED_WORK(&cec_work, cec_work_fn);
546 	schedule_delayed_work(&cec_work, CEC_DECAY_DEFAULT_INTERVAL);
547 
548 	pr_info("Correctable Errors collector initialized.\n");
549 }
550 
551 int __init parse_cec_param(char *str)
552 {
553 	if (!str)
554 		return 0;
555 
556 	if (*str == '=')
557 		str++;
558 
559 	if (!strcmp(str, "cec_disable"))
560 		ce_arr.disabled = 1;
561 	else
562 		return 0;
563 
564 	return 1;
565 }
566