3 #include <linux/kernel.h>
10 * RAS Correctable Errors Collector
12 * This is a simple gadget which collects correctable errors and counts their
13 * occurrence per physical page address.
15 * We've opted for possibly the simplest data structure to collect those - an
16 * array of the size of a memory page. It stores 512 u64's with the following
19 * [63 ... PFN ... 12 | 11 ... generation ... 10 | 9 ... count ... 0]
21 * The generation in the two highest order bits is two bits which are set to 11b
22 * on every insertion. During the course of each entry's existence, the
23 * generation field gets decremented during spring cleaning to 10b, then 01b and
26 * This way we're employing the natural numeric ordering to make sure that newly
27 * inserted/touched elements have higher 12-bit counts (which we've manufactured)
28 * and thus iterating over the array initially won't kick out those elements
29 * which were inserted last.
31 * Spring cleaning is what we do when we reach a certain number CLEAN_ELEMS of
32 * elements entered into the array, during which, we're decaying all elements.
33 * If, after decay, an element gets inserted again, its generation is set to 11b
34 * to make sure it has higher numerical count than other, older elements and
35 * thus emulate an an LRU-like behavior when deleting elements to free up space
38 * When an element reaches it's max count of count_threshold, we try to poison
39 * it by assuming that errors triggered count_threshold times in a single page
40 * are excessive and that page shouldn't be used anymore. count_threshold is
41 * initialized to COUNT_MASK which is the maximum.
43 * That error event entry causes cec_add_elem() to return !0 value and thus
44 * signal to its callers to log the error.
46 * To the question why we've chosen a page and moving elements around with
47 * memmove(), it is because it is a very simple structure to handle and max data
48 * movement is 4K which on highly optimized modern CPUs is almost unnoticeable.
49 * We wanted to avoid the pointer traversal of more complex structures like a
50 * linked list or some sort of a balancing search tree.
52 * Deleting an element takes O(n) but since it is only a single page, it should
53 * be fast enough and it shouldn't happen all too often depending on error
58 #define pr_fmt(fmt) "RAS: " fmt
61 * We use DECAY_BITS bits of PAGE_SHIFT bits for counting decay, i.e., how long
62 * elements have stayed in the array without having been accessed again.
65 #define DECAY_MASK ((1ULL << DECAY_BITS) - 1)
66 #define MAX_ELEMS (PAGE_SIZE / sizeof(u64))
69 * Threshold amount of inserted elements after which we start spring
72 #define CLEAN_ELEMS (MAX_ELEMS >> DECAY_BITS)
74 /* Bits which count the number of errors happened in this 4K page. */
75 #define COUNT_BITS (PAGE_SHIFT - DECAY_BITS)
76 #define COUNT_MASK ((1ULL << COUNT_BITS) - 1)
77 #define FULL_COUNT_MASK (PAGE_SIZE - 1)
80 * u64: [ 63 ... 12 | DECAY_BITS | COUNT_BITS ]
83 #define PFN(e) ((e) >> PAGE_SHIFT)
84 #define DECAY(e) (((e) >> COUNT_BITS) & DECAY_MASK)
85 #define COUNT(e) ((unsigned int)(e) & COUNT_MASK)
86 #define FULL_COUNT(e) ((e) & (PAGE_SIZE - 1))
88 static struct ce_array
{
89 u64
*array
; /* container page */
90 unsigned int n
; /* number of elements in the array */
92 unsigned int decay_count
; /*
93 * number of element insertions/increments
94 * since the last spring cleaning.
98 * number of PFNs which got poisoned.
102 * The number of correctable errors
103 * entered into the collector.
107 * Times we did spring cleaning.
112 __u32 disabled
: 1, /* cmdline disabled */
119 static DEFINE_MUTEX(ce_mutex
);
122 /* Amount of errors after which we offline */
123 static unsigned int count_threshold
= COUNT_MASK
;
126 * The timer "decays" element count each timer_interval which is 24hrs by
130 #define CEC_TIMER_DEFAULT_INTERVAL 24 * 60 * 60 /* 24 hrs */
131 #define CEC_TIMER_MIN_INTERVAL 1 * 60 * 60 /* 1h */
132 #define CEC_TIMER_MAX_INTERVAL 30 * 24 * 60 * 60 /* one month */
133 static struct timer_list cec_timer
;
134 static u64 timer_interval
= CEC_TIMER_DEFAULT_INTERVAL
;
137 * Decrement decay value. We're using DECAY_BITS bits to denote decay of an
138 * element in the array. On insertion and any access, it gets reset to max.
140 static void do_spring_cleaning(struct ce_array
*ca
)
144 for (i
= 0; i
< ca
->n
; i
++) {
145 u8 decay
= DECAY(ca
->array
[i
]);
152 ca
->array
[i
] &= ~(DECAY_MASK
<< COUNT_BITS
);
153 ca
->array
[i
] |= (decay
<< COUNT_BITS
);
160 * @interval in seconds
162 static void cec_mod_timer(struct timer_list
*t
, unsigned long interval
)
166 iv
= interval
* HZ
+ jiffies
;
168 mod_timer(t
, round_jiffies(iv
));
171 static void cec_timer_fn(unsigned long data
)
173 struct ce_array
*ca
= (struct ce_array
*)data
;
175 do_spring_cleaning(ca
);
177 cec_mod_timer(&cec_timer
, timer_interval
);
181 * @to: index of the smallest element which is >= then @pfn.
183 * Return the index of the pfn if found, otherwise negative value.
185 static int __find_elem(struct ce_array
*ca
, u64 pfn
, unsigned int *to
)
188 int min
= 0, max
= ca
->n
;
191 int tmp
= (max
+ min
) >> 1;
193 this_pfn
= PFN(ca
->array
[tmp
]);
197 else if (this_pfn
> pfn
)
208 this_pfn
= PFN(ca
->array
[min
]);
216 static int find_elem(struct ce_array
*ca
, u64 pfn
, unsigned int *to
)
224 return __find_elem(ca
, pfn
, to
);
227 static void del_elem(struct ce_array
*ca
, int idx
)
229 /* Save us a function call when deleting the last element. */
230 if (ca
->n
- (idx
+ 1))
231 memmove((void *)&ca
->array
[idx
],
232 (void *)&ca
->array
[idx
+ 1],
233 (ca
->n
- (idx
+ 1)) * sizeof(u64
));
238 static u64
del_lru_elem_unlocked(struct ce_array
*ca
)
240 unsigned int min
= FULL_COUNT_MASK
;
243 for (i
= 0; i
< ca
->n
; i
++) {
244 unsigned int this = FULL_COUNT(ca
->array
[i
]);
252 del_elem(ca
, min_idx
);
254 return PFN(ca
->array
[min_idx
]);
258 * We return the 0th pfn in the error case under the assumption that it cannot
259 * be poisoned and excessive CEs in there are a serious deal anyway.
261 static u64 __maybe_unused
del_lru_elem(void)
263 struct ce_array
*ca
= &ce_arr
;
269 mutex_lock(&ce_mutex
);
270 pfn
= del_lru_elem_unlocked(ca
);
271 mutex_unlock(&ce_mutex
);
277 int cec_add_elem(u64 pfn
)
279 struct ce_array
*ca
= &ce_arr
;
284 * We can be called very early on the identify_cpu() path where we are
285 * not initialized yet. We ignore the error for simplicity.
287 if (!ce_arr
.array
|| ce_arr
.disabled
)
292 mutex_lock(&ce_mutex
);
294 if (ca
->n
== MAX_ELEMS
)
295 WARN_ON(!del_lru_elem_unlocked(ca
));
297 ret
= find_elem(ca
, pfn
, &to
);
300 * Shift range [to-end] to make room for one more element.
302 memmove((void *)&ca
->array
[to
+ 1],
303 (void *)&ca
->array
[to
],
304 (ca
->n
- to
) * sizeof(u64
));
306 ca
->array
[to
] = (pfn
<< PAGE_SHIFT
) |
307 (DECAY_MASK
<< COUNT_BITS
) | 1;
316 count
= COUNT(ca
->array
[to
]);
318 if (count
< count_threshold
) {
319 ca
->array
[to
] |= (DECAY_MASK
<< COUNT_BITS
);
324 u64 pfn
= ca
->array
[to
] >> PAGE_SHIFT
;
326 if (!pfn_valid(pfn
)) {
327 pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn
);
329 /* We have reached max count for this page, soft-offline it. */
330 pr_err("Soft-offlining pfn: 0x%llx\n", pfn
);
331 memory_failure_queue(pfn
, 0, MF_SOFT_OFFLINE
);
338 * Return a >0 value to denote that we've reached the offlining
349 if (ca
->decay_count
>= CLEAN_ELEMS
)
350 do_spring_cleaning(ca
);
353 mutex_unlock(&ce_mutex
);
358 static int u64_get(void *data
, u64
*val
)
365 static int pfn_set(void *data
, u64 val
)
369 return cec_add_elem(val
);
372 DEFINE_DEBUGFS_ATTRIBUTE(pfn_ops
, u64_get
, pfn_set
, "0x%llx\n");
374 static int decay_interval_set(void *data
, u64 val
)
378 if (val
< CEC_TIMER_MIN_INTERVAL
)
381 if (val
> CEC_TIMER_MAX_INTERVAL
)
384 timer_interval
= val
;
386 cec_mod_timer(&cec_timer
, timer_interval
);
389 DEFINE_DEBUGFS_ATTRIBUTE(decay_interval_ops
, u64_get
, decay_interval_set
, "%lld\n");
391 static int count_threshold_set(void *data
, u64 val
)
395 if (val
> COUNT_MASK
)
398 count_threshold
= val
;
402 DEFINE_DEBUGFS_ATTRIBUTE(count_threshold_ops
, u64_get
, count_threshold_set
, "%lld\n");
404 static int array_dump(struct seq_file
*m
, void *v
)
406 struct ce_array
*ca
= &ce_arr
;
410 mutex_lock(&ce_mutex
);
412 seq_printf(m
, "{ n: %d\n", ca
->n
);
413 for (i
= 0; i
< ca
->n
; i
++) {
414 u64
this = PFN(ca
->array
[i
]);
416 seq_printf(m
, " %03d: [%016llx|%03llx]\n", i
, this, FULL_COUNT(ca
->array
[i
]));
418 WARN_ON(prev
> this);
423 seq_printf(m
, "}\n");
425 seq_printf(m
, "Stats:\nCEs: %llu\nofflined pages: %llu\n",
426 ca
->ces_entered
, ca
->pfns_poisoned
);
428 seq_printf(m
, "Flags: 0x%x\n", ca
->flags
);
430 seq_printf(m
, "Timer interval: %lld seconds\n", timer_interval
);
431 seq_printf(m
, "Decays: %lld\n", ca
->decays_done
);
433 seq_printf(m
, "Action threshold: %d\n", count_threshold
);
435 mutex_unlock(&ce_mutex
);
440 static int array_open(struct inode
*inode
, struct file
*filp
)
442 return single_open(filp
, array_dump
, NULL
);
445 static const struct file_operations array_ops
= {
446 .owner
= THIS_MODULE
,
450 .release
= single_release
,
453 static int __init
create_debugfs_nodes(void)
455 struct dentry
*d
, *pfn
, *decay
, *count
, *array
;
457 d
= debugfs_create_dir("cec", ras_debugfs_dir
);
459 pr_warn("Error creating cec debugfs node!\n");
463 pfn
= debugfs_create_file("pfn", S_IRUSR
| S_IWUSR
, d
, &dfs_pfn
, &pfn_ops
);
465 pr_warn("Error creating pfn debugfs node!\n");
469 array
= debugfs_create_file("array", S_IRUSR
, d
, NULL
, &array_ops
);
471 pr_warn("Error creating array debugfs node!\n");
475 decay
= debugfs_create_file("decay_interval", S_IRUSR
| S_IWUSR
, d
,
476 &timer_interval
, &decay_interval_ops
);
478 pr_warn("Error creating decay_interval debugfs node!\n");
482 count
= debugfs_create_file("count_threshold", S_IRUSR
| S_IWUSR
, d
,
483 &count_threshold
, &count_threshold_ops
);
485 pr_warn("Error creating count_threshold debugfs node!\n");
493 debugfs_remove_recursive(d
);
498 void __init
cec_init(void)
503 ce_arr
.array
= (void *)get_zeroed_page(GFP_KERNEL
);
505 pr_err("Error allocating CE array page!\n");
509 if (create_debugfs_nodes())
512 setup_timer(&cec_timer
, cec_timer_fn
, (unsigned long)&ce_arr
);
513 cec_mod_timer(&cec_timer
, CEC_TIMER_DEFAULT_INTERVAL
);
515 pr_info("Correctable Errors collector initialized.\n");
518 int __init
parse_cec_param(char *str
)
526 if (!strncmp(str
, "cec_disable", 7))