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