staging: erofs: fix a compile warning of Z_EROFS_VLE_VMAP_ONSTACK_PAGES
[linux/fpc-iii.git] / drivers / ras / cec.c
blob2d9ec378a8bc34c92019cadb515315717fd2a7f2
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/mm.h>
3 #include <linux/gfp.h>
4 #include <linux/kernel.h>
6 #include <asm/mce.h>
8 #include "debugfs.h"
11 * RAS Correctable Errors Collector
13 * This is a simple gadget which collects correctable errors and counts their
14 * occurrence per physical page address.
16 * We've opted for possibly the simplest data structure to collect those - an
17 * array of the size of a memory page. It stores 512 u64's with the following
18 * structure:
20 * [63 ... PFN ... 12 | 11 ... generation ... 10 | 9 ... count ... 0]
22 * The generation in the two highest order bits is two bits which are set to 11b
23 * on every insertion. During the course of each entry's existence, the
24 * generation field gets decremented during spring cleaning to 10b, then 01b and
25 * then 00b.
27 * This way we're employing the natural numeric ordering to make sure that newly
28 * inserted/touched elements have higher 12-bit counts (which we've manufactured)
29 * and thus iterating over the array initially won't kick out those elements
30 * which were inserted last.
32 * Spring cleaning is what we do when we reach a certain number CLEAN_ELEMS of
33 * elements entered into the array, during which, we're decaying all elements.
34 * If, after decay, an element gets inserted again, its generation is set to 11b
35 * to make sure it has higher numerical count than other, older elements and
36 * thus emulate an an LRU-like behavior when deleting elements to free up space
37 * in the page.
39 * When an element reaches it's max count of count_threshold, we try to poison
40 * it by assuming that errors triggered count_threshold times in a single page
41 * are excessive and that page shouldn't be used anymore. count_threshold is
42 * initialized to COUNT_MASK which is the maximum.
44 * That error event entry causes cec_add_elem() to return !0 value and thus
45 * signal to its callers to log the error.
47 * To the question why we've chosen a page and moving elements around with
48 * memmove(), it is because it is a very simple structure to handle and max data
49 * movement is 4K which on highly optimized modern CPUs is almost unnoticeable.
50 * We wanted to avoid the pointer traversal of more complex structures like a
51 * linked list or some sort of a balancing search tree.
53 * Deleting an element takes O(n) but since it is only a single page, it should
54 * be fast enough and it shouldn't happen all too often depending on error
55 * patterns.
58 #undef pr_fmt
59 #define pr_fmt(fmt) "RAS: " fmt
62 * We use DECAY_BITS bits of PAGE_SHIFT bits for counting decay, i.e., how long
63 * elements have stayed in the array without having been accessed again.
65 #define DECAY_BITS 2
66 #define DECAY_MASK ((1ULL << DECAY_BITS) - 1)
67 #define MAX_ELEMS (PAGE_SIZE / sizeof(u64))
70 * Threshold amount of inserted elements after which we start spring
71 * cleaning.
73 #define CLEAN_ELEMS (MAX_ELEMS >> DECAY_BITS)
75 /* Bits which count the number of errors happened in this 4K page. */
76 #define COUNT_BITS (PAGE_SHIFT - DECAY_BITS)
77 #define COUNT_MASK ((1ULL << COUNT_BITS) - 1)
78 #define FULL_COUNT_MASK (PAGE_SIZE - 1)
81 * u64: [ 63 ... 12 | DECAY_BITS | COUNT_BITS ]
84 #define PFN(e) ((e) >> PAGE_SHIFT)
85 #define DECAY(e) (((e) >> COUNT_BITS) & DECAY_MASK)
86 #define COUNT(e) ((unsigned int)(e) & COUNT_MASK)
87 #define FULL_COUNT(e) ((e) & (PAGE_SIZE - 1))
89 static struct ce_array {
90 u64 *array; /* container page */
91 unsigned int n; /* number of elements in the array */
93 unsigned int decay_count; /*
94 * number of element insertions/increments
95 * since the last spring cleaning.
98 u64 pfns_poisoned; /*
99 * number of PFNs which got poisoned.
102 u64 ces_entered; /*
103 * The number of correctable errors
104 * entered into the collector.
107 u64 decays_done; /*
108 * Times we did spring cleaning.
111 union {
112 struct {
113 __u32 disabled : 1, /* cmdline disabled */
114 __resv : 31;
116 __u32 flags;
118 } ce_arr;
120 static DEFINE_MUTEX(ce_mutex);
121 static u64 dfs_pfn;
123 /* Amount of errors after which we offline */
124 static unsigned int count_threshold = COUNT_MASK;
127 * The timer "decays" element count each timer_interval which is 24hrs by
128 * default.
131 #define CEC_TIMER_DEFAULT_INTERVAL 24 * 60 * 60 /* 24 hrs */
132 #define CEC_TIMER_MIN_INTERVAL 1 * 60 * 60 /* 1h */
133 #define CEC_TIMER_MAX_INTERVAL 30 * 24 * 60 * 60 /* one month */
134 static struct timer_list cec_timer;
135 static u64 timer_interval = CEC_TIMER_DEFAULT_INTERVAL;
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.
141 static void do_spring_cleaning(struct ce_array *ca)
143 int i;
145 for (i = 0; i < ca->n; i++) {
146 u8 decay = DECAY(ca->array[i]);
148 if (!decay)
149 continue;
151 decay--;
153 ca->array[i] &= ~(DECAY_MASK << COUNT_BITS);
154 ca->array[i] |= (decay << COUNT_BITS);
156 ca->decay_count = 0;
157 ca->decays_done++;
161 * @interval in seconds
163 static void cec_mod_timer(struct timer_list *t, unsigned long interval)
165 unsigned long iv;
167 iv = interval * HZ + jiffies;
169 mod_timer(t, round_jiffies(iv));
172 static void cec_timer_fn(struct timer_list *unused)
174 do_spring_cleaning(&ce_arr);
176 cec_mod_timer(&cec_timer, timer_interval);
180 * @to: index of the smallest element which is >= then @pfn.
182 * Return the index of the pfn if found, otherwise negative value.
184 static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
186 u64 this_pfn;
187 int min = 0, max = ca->n;
189 while (min < max) {
190 int tmp = (max + min) >> 1;
192 this_pfn = PFN(ca->array[tmp]);
194 if (this_pfn < pfn)
195 min = tmp + 1;
196 else if (this_pfn > pfn)
197 max = tmp;
198 else {
199 min = tmp;
200 break;
204 if (to)
205 *to = min;
207 this_pfn = PFN(ca->array[min]);
209 if (this_pfn == pfn)
210 return min;
212 return -ENOKEY;
215 static int find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
217 WARN_ON(!to);
219 if (!ca->n) {
220 *to = 0;
221 return -ENOKEY;
223 return __find_elem(ca, pfn, to);
226 static void del_elem(struct ce_array *ca, int idx)
228 /* Save us a function call when deleting the last element. */
229 if (ca->n - (idx + 1))
230 memmove((void *)&ca->array[idx],
231 (void *)&ca->array[idx + 1],
232 (ca->n - (idx + 1)) * sizeof(u64));
234 ca->n--;
237 static u64 del_lru_elem_unlocked(struct ce_array *ca)
239 unsigned int min = FULL_COUNT_MASK;
240 int i, min_idx = 0;
242 for (i = 0; i < ca->n; i++) {
243 unsigned int this = FULL_COUNT(ca->array[i]);
245 if (min > this) {
246 min = this;
247 min_idx = i;
251 del_elem(ca, min_idx);
253 return PFN(ca->array[min_idx]);
257 * We return the 0th pfn in the error case under the assumption that it cannot
258 * be poisoned and excessive CEs in there are a serious deal anyway.
260 static u64 __maybe_unused del_lru_elem(void)
262 struct ce_array *ca = &ce_arr;
263 u64 pfn;
265 if (!ca->n)
266 return 0;
268 mutex_lock(&ce_mutex);
269 pfn = del_lru_elem_unlocked(ca);
270 mutex_unlock(&ce_mutex);
272 return pfn;
276 int cec_add_elem(u64 pfn)
278 struct ce_array *ca = &ce_arr;
279 unsigned int to;
280 int count, ret = 0;
283 * We can be called very early on the identify_cpu() path where we are
284 * not initialized yet. We ignore the error for simplicity.
286 if (!ce_arr.array || ce_arr.disabled)
287 return -ENODEV;
289 ca->ces_entered++;
291 mutex_lock(&ce_mutex);
293 if (ca->n == MAX_ELEMS)
294 WARN_ON(!del_lru_elem_unlocked(ca));
296 ret = find_elem(ca, pfn, &to);
297 if (ret < 0) {
299 * Shift range [to-end] to make room for one more element.
301 memmove((void *)&ca->array[to + 1],
302 (void *)&ca->array[to],
303 (ca->n - to) * sizeof(u64));
305 ca->array[to] = (pfn << PAGE_SHIFT) |
306 (DECAY_MASK << COUNT_BITS) | 1;
308 ca->n++;
310 ret = 0;
312 goto decay;
315 count = COUNT(ca->array[to]);
317 if (count < count_threshold) {
318 ca->array[to] |= (DECAY_MASK << COUNT_BITS);
319 ca->array[to]++;
321 ret = 0;
322 } else {
323 u64 pfn = ca->array[to] >> PAGE_SHIFT;
325 if (!pfn_valid(pfn)) {
326 pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn);
327 } else {
328 /* We have reached max count for this page, soft-offline it. */
329 pr_err("Soft-offlining pfn: 0x%llx\n", pfn);
330 memory_failure_queue(pfn, MF_SOFT_OFFLINE);
331 ca->pfns_poisoned++;
334 del_elem(ca, to);
337 * Return a >0 value to denote that we've reached the offlining
338 * threshold.
340 ret = 1;
342 goto unlock;
345 decay:
346 ca->decay_count++;
348 if (ca->decay_count >= CLEAN_ELEMS)
349 do_spring_cleaning(ca);
351 unlock:
352 mutex_unlock(&ce_mutex);
354 return ret;
357 static int u64_get(void *data, u64 *val)
359 *val = *(u64 *)data;
361 return 0;
364 static int pfn_set(void *data, u64 val)
366 *(u64 *)data = val;
368 return cec_add_elem(val);
371 DEFINE_DEBUGFS_ATTRIBUTE(pfn_ops, u64_get, pfn_set, "0x%llx\n");
373 static int decay_interval_set(void *data, u64 val)
375 *(u64 *)data = val;
377 if (val < CEC_TIMER_MIN_INTERVAL)
378 return -EINVAL;
380 if (val > CEC_TIMER_MAX_INTERVAL)
381 return -EINVAL;
383 timer_interval = val;
385 cec_mod_timer(&cec_timer, timer_interval);
386 return 0;
388 DEFINE_DEBUGFS_ATTRIBUTE(decay_interval_ops, u64_get, decay_interval_set, "%lld\n");
390 static int count_threshold_set(void *data, u64 val)
392 *(u64 *)data = val;
394 if (val > COUNT_MASK)
395 val = COUNT_MASK;
397 count_threshold = val;
399 return 0;
401 DEFINE_DEBUGFS_ATTRIBUTE(count_threshold_ops, u64_get, count_threshold_set, "%lld\n");
403 static int array_dump(struct seq_file *m, void *v)
405 struct ce_array *ca = &ce_arr;
406 u64 prev = 0;
407 int i;
409 mutex_lock(&ce_mutex);
411 seq_printf(m, "{ n: %d\n", ca->n);
412 for (i = 0; i < ca->n; i++) {
413 u64 this = PFN(ca->array[i]);
415 seq_printf(m, " %03d: [%016llx|%03llx]\n", i, this, FULL_COUNT(ca->array[i]));
417 WARN_ON(prev > this);
419 prev = this;
422 seq_printf(m, "}\n");
424 seq_printf(m, "Stats:\nCEs: %llu\nofflined pages: %llu\n",
425 ca->ces_entered, ca->pfns_poisoned);
427 seq_printf(m, "Flags: 0x%x\n", ca->flags);
429 seq_printf(m, "Timer interval: %lld seconds\n", timer_interval);
430 seq_printf(m, "Decays: %lld\n", ca->decays_done);
432 seq_printf(m, "Action threshold: %d\n", count_threshold);
434 mutex_unlock(&ce_mutex);
436 return 0;
439 static int array_open(struct inode *inode, struct file *filp)
441 return single_open(filp, array_dump, NULL);
444 static const struct file_operations array_ops = {
445 .owner = THIS_MODULE,
446 .open = array_open,
447 .read = seq_read,
448 .llseek = seq_lseek,
449 .release = single_release,
452 static int __init create_debugfs_nodes(void)
454 struct dentry *d, *pfn, *decay, *count, *array;
456 d = debugfs_create_dir("cec", ras_debugfs_dir);
457 if (!d) {
458 pr_warn("Error creating cec debugfs node!\n");
459 return -1;
462 pfn = debugfs_create_file("pfn", S_IRUSR | S_IWUSR, d, &dfs_pfn, &pfn_ops);
463 if (!pfn) {
464 pr_warn("Error creating pfn debugfs node!\n");
465 goto err;
468 array = debugfs_create_file("array", S_IRUSR, d, NULL, &array_ops);
469 if (!array) {
470 pr_warn("Error creating array debugfs node!\n");
471 goto err;
474 decay = debugfs_create_file("decay_interval", S_IRUSR | S_IWUSR, d,
475 &timer_interval, &decay_interval_ops);
476 if (!decay) {
477 pr_warn("Error creating decay_interval debugfs node!\n");
478 goto err;
481 count = debugfs_create_file("count_threshold", S_IRUSR | S_IWUSR, d,
482 &count_threshold, &count_threshold_ops);
483 if (!count) {
484 pr_warn("Error creating count_threshold debugfs node!\n");
485 goto err;
489 return 0;
491 err:
492 debugfs_remove_recursive(d);
494 return 1;
497 void __init cec_init(void)
499 if (ce_arr.disabled)
500 return;
502 ce_arr.array = (void *)get_zeroed_page(GFP_KERNEL);
503 if (!ce_arr.array) {
504 pr_err("Error allocating CE array page!\n");
505 return;
508 if (create_debugfs_nodes())
509 return;
511 timer_setup(&cec_timer, cec_timer_fn, 0);
512 cec_mod_timer(&cec_timer, CEC_TIMER_DEFAULT_INTERVAL);
514 pr_info("Correctable Errors collector initialized.\n");
517 int __init parse_cec_param(char *str)
519 if (!str)
520 return 0;
522 if (*str == '=')
523 str++;
525 if (!strcmp(str, "cec_disable"))
526 ce_arr.disabled = 1;
527 else
528 return 0;
530 return 1;