xref: /openbmc/linux/drivers/ras/cec.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
209afc797SBorislav Petkov /*
309afc797SBorislav Petkov  * Copyright (c) 2017-2019 Borislav Petkov, SUSE Labs.
409afc797SBorislav Petkov  */
5011d8261SBorislav Petkov #include <linux/mm.h>
6011d8261SBorislav Petkov #include <linux/gfp.h>
70a54b809SValdis Klētnieks #include <linux/ras.h>
8011d8261SBorislav Petkov #include <linux/kernel.h>
90ade0b62SCong Wang #include <linux/workqueue.h>
10011d8261SBorislav Petkov 
11011d8261SBorislav Petkov #include <asm/mce.h>
12011d8261SBorislav Petkov 
13011d8261SBorislav Petkov #include "debugfs.h"
14011d8261SBorislav Petkov 
15011d8261SBorislav Petkov /*
16011d8261SBorislav Petkov  * RAS Correctable Errors Collector
17011d8261SBorislav Petkov  *
18011d8261SBorislav Petkov  * This is a simple gadget which collects correctable errors and counts their
19011d8261SBorislav Petkov  * occurrence per physical page address.
20011d8261SBorislav Petkov  *
21011d8261SBorislav Petkov  * We've opted for possibly the simplest data structure to collect those - an
22011d8261SBorislav Petkov  * array of the size of a memory page. It stores 512 u64's with the following
23011d8261SBorislav Petkov  * structure:
24011d8261SBorislav Petkov  *
25011d8261SBorislav Petkov  * [63 ... PFN ... 12 | 11 ... generation ... 10 | 9 ... count ... 0]
26011d8261SBorislav Petkov  *
27011d8261SBorislav Petkov  * The generation in the two highest order bits is two bits which are set to 11b
28011d8261SBorislav Petkov  * on every insertion. During the course of each entry's existence, the
29011d8261SBorislav Petkov  * generation field gets decremented during spring cleaning to 10b, then 01b and
30011d8261SBorislav Petkov  * then 00b.
31011d8261SBorislav Petkov  *
32011d8261SBorislav Petkov  * This way we're employing the natural numeric ordering to make sure that newly
33011d8261SBorislav Petkov  * inserted/touched elements have higher 12-bit counts (which we've manufactured)
34011d8261SBorislav Petkov  * and thus iterating over the array initially won't kick out those elements
35011d8261SBorislav Petkov  * which were inserted last.
36011d8261SBorislav Petkov  *
37011d8261SBorislav Petkov  * Spring cleaning is what we do when we reach a certain number CLEAN_ELEMS of
38011d8261SBorislav Petkov  * elements entered into the array, during which, we're decaying all elements.
39011d8261SBorislav Petkov  * If, after decay, an element gets inserted again, its generation is set to 11b
40011d8261SBorislav Petkov  * to make sure it has higher numerical count than other, older elements and
41b011a57eSJason Wang  * thus emulate an LRU-like behavior when deleting elements to free up space
42011d8261SBorislav Petkov  * in the page.
43011d8261SBorislav Petkov  *
44b8b5ca66SBorislav Petkov  * When an element reaches it's max count of action_threshold, we try to poison
45b8b5ca66SBorislav Petkov  * it by assuming that errors triggered action_threshold times in a single page
46b8b5ca66SBorislav Petkov  * are excessive and that page shouldn't be used anymore. action_threshold is
47011d8261SBorislav Petkov  * initialized to COUNT_MASK which is the maximum.
48011d8261SBorislav Petkov  *
49011d8261SBorislav Petkov  * That error event entry causes cec_add_elem() to return !0 value and thus
50011d8261SBorislav Petkov  * signal to its callers to log the error.
51011d8261SBorislav Petkov  *
52011d8261SBorislav Petkov  * To the question why we've chosen a page and moving elements around with
53011d8261SBorislav Petkov  * memmove(), it is because it is a very simple structure to handle and max data
54011d8261SBorislav Petkov  * movement is 4K which on highly optimized modern CPUs is almost unnoticeable.
55011d8261SBorislav Petkov  * We wanted to avoid the pointer traversal of more complex structures like a
56011d8261SBorislav Petkov  * linked list or some sort of a balancing search tree.
57011d8261SBorislav Petkov  *
58011d8261SBorislav Petkov  * Deleting an element takes O(n) but since it is only a single page, it should
59011d8261SBorislav Petkov  * be fast enough and it shouldn't happen all too often depending on error
60011d8261SBorislav Petkov  * patterns.
61011d8261SBorislav Petkov  */
62011d8261SBorislav Petkov 
63011d8261SBorislav Petkov #undef pr_fmt
64011d8261SBorislav Petkov #define pr_fmt(fmt) "RAS: " fmt
65011d8261SBorislav Petkov 
66011d8261SBorislav Petkov /*
67011d8261SBorislav Petkov  * We use DECAY_BITS bits of PAGE_SHIFT bits for counting decay, i.e., how long
68011d8261SBorislav Petkov  * elements have stayed in the array without having been accessed again.
69011d8261SBorislav Petkov  */
70011d8261SBorislav Petkov #define DECAY_BITS		2
71011d8261SBorislav Petkov #define DECAY_MASK		((1ULL << DECAY_BITS) - 1)
72011d8261SBorislav Petkov #define MAX_ELEMS		(PAGE_SIZE / sizeof(u64))
73011d8261SBorislav Petkov 
74011d8261SBorislav Petkov /*
75011d8261SBorislav Petkov  * Threshold amount of inserted elements after which we start spring
76011d8261SBorislav Petkov  * cleaning.
77011d8261SBorislav Petkov  */
78011d8261SBorislav Petkov #define CLEAN_ELEMS		(MAX_ELEMS >> DECAY_BITS)
79011d8261SBorislav Petkov 
80011d8261SBorislav Petkov /* Bits which count the number of errors happened in this 4K page. */
81011d8261SBorislav Petkov #define COUNT_BITS		(PAGE_SHIFT - DECAY_BITS)
82011d8261SBorislav Petkov #define COUNT_MASK		((1ULL << COUNT_BITS) - 1)
83011d8261SBorislav Petkov #define FULL_COUNT_MASK		(PAGE_SIZE - 1)
84011d8261SBorislav Petkov 
85011d8261SBorislav Petkov /*
86011d8261SBorislav Petkov  * u64: [ 63 ... 12 | DECAY_BITS | COUNT_BITS ]
87011d8261SBorislav Petkov  */
88011d8261SBorislav Petkov 
89011d8261SBorislav Petkov #define PFN(e)			((e) >> PAGE_SHIFT)
90011d8261SBorislav Petkov #define DECAY(e)		(((e) >> COUNT_BITS) & DECAY_MASK)
91011d8261SBorislav Petkov #define COUNT(e)		((unsigned int)(e) & COUNT_MASK)
92011d8261SBorislav Petkov #define FULL_COUNT(e)		((e) & (PAGE_SIZE - 1))
93011d8261SBorislav Petkov 
94011d8261SBorislav Petkov static struct ce_array {
95011d8261SBorislav Petkov 	u64 *array;			/* container page */
96011d8261SBorislav Petkov 	unsigned int n;			/* number of elements in the array */
97011d8261SBorislav Petkov 
98011d8261SBorislav Petkov 	unsigned int decay_count;	/*
99011d8261SBorislav Petkov 					 * number of element insertions/increments
100011d8261SBorislav Petkov 					 * since the last spring cleaning.
101011d8261SBorislav Petkov 					 */
102011d8261SBorislav Petkov 
103011d8261SBorislav Petkov 	u64 pfns_poisoned;		/*
104011d8261SBorislav Petkov 					 * number of PFNs which got poisoned.
105011d8261SBorislav Petkov 					 */
106011d8261SBorislav Petkov 
107011d8261SBorislav Petkov 	u64 ces_entered;		/*
108011d8261SBorislav Petkov 					 * The number of correctable errors
109011d8261SBorislav Petkov 					 * entered into the collector.
110011d8261SBorislav Petkov 					 */
111011d8261SBorislav Petkov 
112011d8261SBorislav Petkov 	u64 decays_done;		/*
113011d8261SBorislav Petkov 					 * Times we did spring cleaning.
114011d8261SBorislav Petkov 					 */
115011d8261SBorislav Petkov 
116011d8261SBorislav Petkov 	union {
117011d8261SBorislav Petkov 		struct {
118011d8261SBorislav Petkov 			__u32	disabled : 1,	/* cmdline disabled */
119011d8261SBorislav Petkov 			__resv   : 31;
120011d8261SBorislav Petkov 		};
121011d8261SBorislav Petkov 		__u32 flags;
122011d8261SBorislav Petkov 	};
123011d8261SBorislav Petkov } ce_arr;
124011d8261SBorislav Petkov 
125011d8261SBorislav Petkov static DEFINE_MUTEX(ce_mutex);
126011d8261SBorislav Petkov static u64 dfs_pfn;
127011d8261SBorislav Petkov 
128011d8261SBorislav Petkov /* Amount of errors after which we offline */
129b8b5ca66SBorislav Petkov static u64 action_threshold = COUNT_MASK;
130011d8261SBorislav Petkov 
1310ade0b62SCong Wang /* Each element "decays" each decay_interval which is 24hrs by default. */
1320ade0b62SCong Wang #define CEC_DECAY_DEFAULT_INTERVAL	24 * 60 * 60	/* 24 hrs */
1330ade0b62SCong Wang #define CEC_DECAY_MIN_INTERVAL		 1 * 60 * 60	/* 1h */
1340ade0b62SCong Wang #define CEC_DECAY_MAX_INTERVAL	   30 *	24 * 60 * 60	/* one month */
1350ade0b62SCong Wang static struct delayed_work cec_work;
1360ade0b62SCong Wang static u64 decay_interval = CEC_DECAY_DEFAULT_INTERVAL;
137011d8261SBorislav Petkov 
138011d8261SBorislav Petkov /*
139011d8261SBorislav Petkov  * Decrement decay value. We're using DECAY_BITS bits to denote decay of an
140011d8261SBorislav Petkov  * element in the array. On insertion and any access, it gets reset to max.
141011d8261SBorislav Petkov  */
do_spring_cleaning(struct ce_array * ca)142011d8261SBorislav Petkov static void do_spring_cleaning(struct ce_array *ca)
143011d8261SBorislav Petkov {
144011d8261SBorislav Petkov 	int i;
145011d8261SBorislav Petkov 
146011d8261SBorislav Petkov 	for (i = 0; i < ca->n; i++) {
147011d8261SBorislav Petkov 		u8 decay = DECAY(ca->array[i]);
148011d8261SBorislav Petkov 
149011d8261SBorislav Petkov 		if (!decay)
150011d8261SBorislav Petkov 			continue;
151011d8261SBorislav Petkov 
152011d8261SBorislav Petkov 		decay--;
153011d8261SBorislav Petkov 
154011d8261SBorislav Petkov 		ca->array[i] &= ~(DECAY_MASK << COUNT_BITS);
155011d8261SBorislav Petkov 		ca->array[i] |= (decay << COUNT_BITS);
156011d8261SBorislav Petkov 	}
157011d8261SBorislav Petkov 	ca->decay_count = 0;
158011d8261SBorislav Petkov 	ca->decays_done++;
159011d8261SBorislav Petkov }
160011d8261SBorislav Petkov 
161011d8261SBorislav Petkov /*
162011d8261SBorislav Petkov  * @interval in seconds
163011d8261SBorislav Petkov  */
cec_mod_work(unsigned long interval)1640ade0b62SCong Wang static void cec_mod_work(unsigned long interval)
165011d8261SBorislav Petkov {
166011d8261SBorislav Petkov 	unsigned long iv;
167011d8261SBorislav Petkov 
1680ade0b62SCong Wang 	iv = interval * HZ;
1690ade0b62SCong Wang 	mod_delayed_work(system_wq, &cec_work, round_jiffies(iv));
170011d8261SBorislav Petkov }
171011d8261SBorislav Petkov 
cec_work_fn(struct work_struct * work)1720ade0b62SCong Wang static void cec_work_fn(struct work_struct *work)
173011d8261SBorislav Petkov {
1740ade0b62SCong Wang 	mutex_lock(&ce_mutex);
175254db5bdSKees Cook 	do_spring_cleaning(&ce_arr);
1760ade0b62SCong Wang 	mutex_unlock(&ce_mutex);
177011d8261SBorislav Petkov 
1780ade0b62SCong Wang 	cec_mod_work(decay_interval);
179011d8261SBorislav Petkov }
180011d8261SBorislav Petkov 
181011d8261SBorislav Petkov /*
182011d8261SBorislav Petkov  * @to: index of the smallest element which is >= then @pfn.
183011d8261SBorislav Petkov  *
184011d8261SBorislav Petkov  * Return the index of the pfn if found, otherwise negative value.
185011d8261SBorislav Petkov  */
__find_elem(struct ce_array * ca,u64 pfn,unsigned int * to)186011d8261SBorislav Petkov static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
187011d8261SBorislav Petkov {
188f3c74b38SBorislav Petkov 	int min = 0, max = ca->n - 1;
189011d8261SBorislav Petkov 	u64 this_pfn;
190011d8261SBorislav Petkov 
191f3c74b38SBorislav Petkov 	while (min <= max) {
192f3c74b38SBorislav Petkov 		int i = (min + max) >> 1;
193011d8261SBorislav Petkov 
194f3c74b38SBorislav Petkov 		this_pfn = PFN(ca->array[i]);
195011d8261SBorislav Petkov 
196011d8261SBorislav Petkov 		if (this_pfn < pfn)
197f3c74b38SBorislav Petkov 			min = i + 1;
198011d8261SBorislav Petkov 		else if (this_pfn > pfn)
199f3c74b38SBorislav Petkov 			max = i - 1;
200f3c74b38SBorislav Petkov 		else if (this_pfn == pfn) {
201f3c74b38SBorislav Petkov 			if (to)
202f3c74b38SBorislav Petkov 				*to = i;
203f3c74b38SBorislav Petkov 
204f3c74b38SBorislav Petkov 			return i;
205011d8261SBorislav Petkov 		}
206011d8261SBorislav Petkov 	}
207011d8261SBorislav Petkov 
208f3c74b38SBorislav Petkov 	/*
209f3c74b38SBorislav Petkov 	 * When the loop terminates without finding @pfn, min has the index of
210f3c74b38SBorislav Petkov 	 * the element slot where the new @pfn should be inserted. The loop
211f3c74b38SBorislav Petkov 	 * terminates when min > max, which means the min index points to the
212f3c74b38SBorislav Petkov 	 * bigger element while the max index to the smaller element, in-between
213f3c74b38SBorislav Petkov 	 * which the new @pfn belongs to.
214f3c74b38SBorislav Petkov 	 *
215f3c74b38SBorislav Petkov 	 * For more details, see exercise 1, Section 6.2.1 in TAOCP, vol. 3.
216f3c74b38SBorislav Petkov 	 */
217011d8261SBorislav Petkov 	if (to)
218011d8261SBorislav Petkov 		*to = min;
219011d8261SBorislav Petkov 
220011d8261SBorislav Petkov 	return -ENOKEY;
221011d8261SBorislav Petkov }
222011d8261SBorislav Petkov 
find_elem(struct ce_array * ca,u64 pfn,unsigned int * to)223011d8261SBorislav Petkov static int find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
224011d8261SBorislav Petkov {
225011d8261SBorislav Petkov 	WARN_ON(!to);
226011d8261SBorislav Petkov 
227011d8261SBorislav Petkov 	if (!ca->n) {
228011d8261SBorislav Petkov 		*to = 0;
229011d8261SBorislav Petkov 		return -ENOKEY;
230011d8261SBorislav Petkov 	}
231011d8261SBorislav Petkov 	return __find_elem(ca, pfn, to);
232011d8261SBorislav Petkov }
233011d8261SBorislav Petkov 
del_elem(struct ce_array * ca,int idx)234011d8261SBorislav Petkov static void del_elem(struct ce_array *ca, int idx)
235011d8261SBorislav Petkov {
236011d8261SBorislav Petkov 	/* Save us a function call when deleting the last element. */
237011d8261SBorislav Petkov 	if (ca->n - (idx + 1))
238011d8261SBorislav Petkov 		memmove((void *)&ca->array[idx],
239011d8261SBorislav Petkov 			(void *)&ca->array[idx + 1],
240011d8261SBorislav Petkov 			(ca->n - (idx + 1)) * sizeof(u64));
241011d8261SBorislav Petkov 
242011d8261SBorislav Petkov 	ca->n--;
243011d8261SBorislav Petkov }
244011d8261SBorislav Petkov 
del_lru_elem_unlocked(struct ce_array * ca)245011d8261SBorislav Petkov static u64 del_lru_elem_unlocked(struct ce_array *ca)
246011d8261SBorislav Petkov {
247011d8261SBorislav Petkov 	unsigned int min = FULL_COUNT_MASK;
248011d8261SBorislav Petkov 	int i, min_idx = 0;
249011d8261SBorislav Petkov 
250011d8261SBorislav Petkov 	for (i = 0; i < ca->n; i++) {
251011d8261SBorislav Petkov 		unsigned int this = FULL_COUNT(ca->array[i]);
252011d8261SBorislav Petkov 
253011d8261SBorislav Petkov 		if (min > this) {
254011d8261SBorislav Petkov 			min = this;
255011d8261SBorislav Petkov 			min_idx = i;
256011d8261SBorislav Petkov 		}
257011d8261SBorislav Petkov 	}
258011d8261SBorislav Petkov 
259011d8261SBorislav Petkov 	del_elem(ca, min_idx);
260011d8261SBorislav Petkov 
261011d8261SBorislav Petkov 	return PFN(ca->array[min_idx]);
262011d8261SBorislav Petkov }
263011d8261SBorislav Petkov 
264011d8261SBorislav Petkov /*
265011d8261SBorislav Petkov  * We return the 0th pfn in the error case under the assumption that it cannot
266011d8261SBorislav Petkov  * be poisoned and excessive CEs in there are a serious deal anyway.
267011d8261SBorislav Petkov  */
del_lru_elem(void)268011d8261SBorislav Petkov static u64 __maybe_unused del_lru_elem(void)
269011d8261SBorislav Petkov {
270011d8261SBorislav Petkov 	struct ce_array *ca = &ce_arr;
271011d8261SBorislav Petkov 	u64 pfn;
272011d8261SBorislav Petkov 
273011d8261SBorislav Petkov 	if (!ca->n)
274011d8261SBorislav Petkov 		return 0;
275011d8261SBorislav Petkov 
276011d8261SBorislav Petkov 	mutex_lock(&ce_mutex);
277011d8261SBorislav Petkov 	pfn = del_lru_elem_unlocked(ca);
278011d8261SBorislav Petkov 	mutex_unlock(&ce_mutex);
279011d8261SBorislav Petkov 
280011d8261SBorislav Petkov 	return pfn;
281011d8261SBorislav Petkov }
282011d8261SBorislav Petkov 
sanity_check(struct ce_array * ca)2839632a329SBorislav Petkov static bool sanity_check(struct ce_array *ca)
2849632a329SBorislav Petkov {
2859632a329SBorislav Petkov 	bool ret = false;
2869632a329SBorislav Petkov 	u64 prev = 0;
2879632a329SBorislav Petkov 	int i;
2889632a329SBorislav Petkov 
2899632a329SBorislav Petkov 	for (i = 0; i < ca->n; i++) {
2909632a329SBorislav Petkov 		u64 this = PFN(ca->array[i]);
2919632a329SBorislav Petkov 
2929632a329SBorislav Petkov 		if (WARN(prev > this, "prev: 0x%016llx <-> this: 0x%016llx\n", prev, this))
2939632a329SBorislav Petkov 			ret = true;
2949632a329SBorislav Petkov 
2959632a329SBorislav Petkov 		prev = this;
2969632a329SBorislav Petkov 	}
2979632a329SBorislav Petkov 
2989632a329SBorislav Petkov 	if (!ret)
2999632a329SBorislav Petkov 		return ret;
3009632a329SBorislav Petkov 
3019632a329SBorislav Petkov 	pr_info("Sanity check dump:\n{ n: %d\n", ca->n);
3029632a329SBorislav Petkov 	for (i = 0; i < ca->n; i++) {
3039632a329SBorislav Petkov 		u64 this = PFN(ca->array[i]);
3049632a329SBorislav Petkov 
3059632a329SBorislav Petkov 		pr_info(" %03d: [%016llx|%03llx]\n", i, this, FULL_COUNT(ca->array[i]));
3069632a329SBorislav Petkov 	}
3079632a329SBorislav Petkov 	pr_info("}\n");
3089632a329SBorislav Petkov 
3099632a329SBorislav Petkov 	return ret;
3109632a329SBorislav Petkov }
311011d8261SBorislav Petkov 
3123a62583cSWilliam Roche /**
3133a62583cSWilliam Roche  * cec_add_elem - Add an element to the CEC array.
3143a62583cSWilliam Roche  * @pfn:	page frame number to insert
3153a62583cSWilliam Roche  *
3163a62583cSWilliam Roche  * Return values:
3173a62583cSWilliam Roche  * - <0:	on error
3183a62583cSWilliam Roche  * -  0:	on success
3193a62583cSWilliam Roche  * - >0:	when the inserted pfn was offlined
3203a62583cSWilliam Roche  */
cec_add_elem(u64 pfn)3219554bfe4STony Luck static int cec_add_elem(u64 pfn)
322011d8261SBorislav Petkov {
323011d8261SBorislav Petkov 	struct ce_array *ca = &ce_arr;
3243a62583cSWilliam Roche 	int count, err, ret = 0;
3259632a329SBorislav Petkov 	unsigned int to = 0;
326011d8261SBorislav Petkov 
327011d8261SBorislav Petkov 	/*
328011d8261SBorislav Petkov 	 * We can be called very early on the identify_cpu() path where we are
329011d8261SBorislav Petkov 	 * not initialized yet. We ignore the error for simplicity.
330011d8261SBorislav Petkov 	 */
331011d8261SBorislav Petkov 	if (!ce_arr.array || ce_arr.disabled)
332011d8261SBorislav Petkov 		return -ENODEV;
333011d8261SBorislav Petkov 
334011d8261SBorislav Petkov 	mutex_lock(&ce_mutex);
335011d8261SBorislav Petkov 
33609cbd219SWANG Chao 	ca->ces_entered++;
33709cbd219SWANG Chao 
338de0e0624SBorislav Petkov 	/* Array full, free the LRU slot. */
339011d8261SBorislav Petkov 	if (ca->n == MAX_ELEMS)
340011d8261SBorislav Petkov 		WARN_ON(!del_lru_elem_unlocked(ca));
341011d8261SBorislav Petkov 
3423a62583cSWilliam Roche 	err = find_elem(ca, pfn, &to);
3433a62583cSWilliam Roche 	if (err < 0) {
344011d8261SBorislav Petkov 		/*
345011d8261SBorislav Petkov 		 * Shift range [to-end] to make room for one more element.
346011d8261SBorislav Petkov 		 */
347011d8261SBorislav Petkov 		memmove((void *)&ca->array[to + 1],
348011d8261SBorislav Petkov 			(void *)&ca->array[to],
349011d8261SBorislav Petkov 			(ca->n - to) * sizeof(u64));
350011d8261SBorislav Petkov 
351de0e0624SBorislav Petkov 		ca->array[to] = pfn << PAGE_SHIFT;
352011d8261SBorislav Petkov 		ca->n++;
353011d8261SBorislav Petkov 	}
354011d8261SBorislav Petkov 
355de0e0624SBorislav Petkov 	/* Add/refresh element generation and increment count */
356de0e0624SBorislav Petkov 	ca->array[to] |= DECAY_MASK << COUNT_BITS;
357011d8261SBorislav Petkov 	ca->array[to]++;
358011d8261SBorislav Petkov 
359de0e0624SBorislav Petkov 	/* Check action threshold and soft-offline, if reached. */
360de0e0624SBorislav Petkov 	count = COUNT(ca->array[to]);
361b8b5ca66SBorislav Petkov 	if (count >= action_threshold) {
362011d8261SBorislav Petkov 		u64 pfn = ca->array[to] >> PAGE_SHIFT;
363011d8261SBorislav Petkov 
364011d8261SBorislav Petkov 		if (!pfn_valid(pfn)) {
365011d8261SBorislav Petkov 			pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn);
366011d8261SBorislav Petkov 		} else {
367011d8261SBorislav Petkov 			/* We have reached max count for this page, soft-offline it. */
368011d8261SBorislav Petkov 			pr_err("Soft-offlining pfn: 0x%llx\n", pfn);
36983b57531SEric W. Biederman 			memory_failure_queue(pfn, MF_SOFT_OFFLINE);
370011d8261SBorislav Petkov 			ca->pfns_poisoned++;
371011d8261SBorislav Petkov 		}
372011d8261SBorislav Petkov 
373011d8261SBorislav Petkov 		del_elem(ca, to);
374011d8261SBorislav Petkov 
375011d8261SBorislav Petkov 		/*
376de0e0624SBorislav Petkov 		 * Return a >0 value to callers, to denote that we've reached
377de0e0624SBorislav Petkov 		 * the offlining threshold.
378011d8261SBorislav Petkov 		 */
379011d8261SBorislav Petkov 		ret = 1;
380011d8261SBorislav Petkov 
381011d8261SBorislav Petkov 		goto unlock;
382011d8261SBorislav Petkov 	}
383011d8261SBorislav Petkov 
384011d8261SBorislav Petkov 	ca->decay_count++;
385011d8261SBorislav Petkov 
386011d8261SBorislav Petkov 	if (ca->decay_count >= CLEAN_ELEMS)
387011d8261SBorislav Petkov 		do_spring_cleaning(ca);
388011d8261SBorislav Petkov 
3899632a329SBorislav Petkov 	WARN_ON_ONCE(sanity_check(ca));
3909632a329SBorislav Petkov 
391011d8261SBorislav Petkov unlock:
392011d8261SBorislav Petkov 	mutex_unlock(&ce_mutex);
393011d8261SBorislav Petkov 
394011d8261SBorislav Petkov 	return ret;
395011d8261SBorislav Petkov }
396011d8261SBorislav Petkov 
u64_get(void * data,u64 * val)397011d8261SBorislav Petkov static int u64_get(void *data, u64 *val)
398011d8261SBorislav Petkov {
399011d8261SBorislav Petkov 	*val = *(u64 *)data;
400011d8261SBorislav Petkov 
401011d8261SBorislav Petkov 	return 0;
402011d8261SBorislav Petkov }
403011d8261SBorislav Petkov 
pfn_set(void * data,u64 val)404011d8261SBorislav Petkov static int pfn_set(void *data, u64 val)
405011d8261SBorislav Petkov {
406011d8261SBorislav Petkov 	*(u64 *)data = val;
407011d8261SBorislav Petkov 
4086d8e294bSBorislav Petkov 	cec_add_elem(val);
4096d8e294bSBorislav Petkov 
4106d8e294bSBorislav Petkov 	return 0;
411011d8261SBorislav Petkov }
412011d8261SBorislav Petkov 
413011d8261SBorislav Petkov DEFINE_DEBUGFS_ATTRIBUTE(pfn_ops, u64_get, pfn_set, "0x%llx\n");
414011d8261SBorislav Petkov 
decay_interval_set(void * data,u64 val)415011d8261SBorislav Petkov static int decay_interval_set(void *data, u64 val)
416011d8261SBorislav Petkov {
4170ade0b62SCong Wang 	if (val < CEC_DECAY_MIN_INTERVAL)
418011d8261SBorislav Petkov 		return -EINVAL;
419011d8261SBorislav Petkov 
4200ade0b62SCong Wang 	if (val > CEC_DECAY_MAX_INTERVAL)
421011d8261SBorislav Petkov 		return -EINVAL;
422011d8261SBorislav Petkov 
4235cc6b16eSBorislav Petkov 	*(u64 *)data   = val;
4240ade0b62SCong Wang 	decay_interval = val;
425011d8261SBorislav Petkov 
4260ade0b62SCong Wang 	cec_mod_work(decay_interval);
4275cc6b16eSBorislav Petkov 
428011d8261SBorislav Petkov 	return 0;
429011d8261SBorislav Petkov }
430011d8261SBorislav Petkov DEFINE_DEBUGFS_ATTRIBUTE(decay_interval_ops, u64_get, decay_interval_set, "%lld\n");
431011d8261SBorislav Petkov 
action_threshold_set(void * data,u64 val)432b8b5ca66SBorislav Petkov static int action_threshold_set(void *data, u64 val)
433011d8261SBorislav Petkov {
434011d8261SBorislav Petkov 	*(u64 *)data = val;
435011d8261SBorislav Petkov 
436011d8261SBorislav Petkov 	if (val > COUNT_MASK)
437011d8261SBorislav Petkov 		val = COUNT_MASK;
438011d8261SBorislav Petkov 
439b8b5ca66SBorislav Petkov 	action_threshold = val;
440011d8261SBorislav Petkov 
441011d8261SBorislav Petkov 	return 0;
442011d8261SBorislav Petkov }
443b8b5ca66SBorislav Petkov DEFINE_DEBUGFS_ATTRIBUTE(action_threshold_ops, u64_get, action_threshold_set, "%lld\n");
444011d8261SBorislav Petkov 
445f57518cdSBorislav Petkov static const char * const bins[] = { "00", "01", "10", "11" };
446f57518cdSBorislav Petkov 
array_show(struct seq_file * m,void * v)4474bd442e9SQinglang Miao static int array_show(struct seq_file *m, void *v)
448011d8261SBorislav Petkov {
449011d8261SBorislav Petkov 	struct ce_array *ca = &ce_arr;
450011d8261SBorislav Petkov 	int i;
451011d8261SBorislav Petkov 
452011d8261SBorislav Petkov 	mutex_lock(&ce_mutex);
453011d8261SBorislav Petkov 
454011d8261SBorislav Petkov 	seq_printf(m, "{ n: %d\n", ca->n);
455011d8261SBorislav Petkov 	for (i = 0; i < ca->n; i++) {
456011d8261SBorislav Petkov 		u64 this = PFN(ca->array[i]);
457011d8261SBorislav Petkov 
458f57518cdSBorislav Petkov 		seq_printf(m, " %3d: [%016llx|%s|%03llx]\n",
459f57518cdSBorislav Petkov 			   i, this, bins[DECAY(ca->array[i])], COUNT(ca->array[i]));
460011d8261SBorislav Petkov 	}
461011d8261SBorislav Petkov 
462011d8261SBorislav Petkov 	seq_printf(m, "}\n");
463011d8261SBorislav Petkov 
464011d8261SBorislav Petkov 	seq_printf(m, "Stats:\nCEs: %llu\nofflined pages: %llu\n",
465011d8261SBorislav Petkov 		   ca->ces_entered, ca->pfns_poisoned);
466011d8261SBorislav Petkov 
467011d8261SBorislav Petkov 	seq_printf(m, "Flags: 0x%x\n", ca->flags);
468011d8261SBorislav Petkov 
4690ade0b62SCong Wang 	seq_printf(m, "Decay interval: %lld seconds\n", decay_interval);
470011d8261SBorislav Petkov 	seq_printf(m, "Decays: %lld\n", ca->decays_done);
471011d8261SBorislav Petkov 
472b8b5ca66SBorislav Petkov 	seq_printf(m, "Action threshold: %lld\n", action_threshold);
473011d8261SBorislav Petkov 
474011d8261SBorislav Petkov 	mutex_unlock(&ce_mutex);
475011d8261SBorislav Petkov 
476011d8261SBorislav Petkov 	return 0;
477011d8261SBorislav Petkov }
478011d8261SBorislav Petkov 
4794bd442e9SQinglang Miao DEFINE_SHOW_ATTRIBUTE(array);
480011d8261SBorislav Petkov 
create_debugfs_nodes(void)481011d8261SBorislav Petkov static int __init create_debugfs_nodes(void)
482011d8261SBorislav Petkov {
483011d8261SBorislav Petkov 	struct dentry *d, *pfn, *decay, *count, *array;
484011d8261SBorislav Petkov 
485011d8261SBorislav Petkov 	d = debugfs_create_dir("cec", ras_debugfs_dir);
486011d8261SBorislav Petkov 	if (!d) {
487011d8261SBorislav Petkov 		pr_warn("Error creating cec debugfs node!\n");
488011d8261SBorislav Petkov 		return -1;
489011d8261SBorislav Petkov 	}
490011d8261SBorislav Petkov 
491011d8261SBorislav Petkov 	decay = debugfs_create_file("decay_interval", S_IRUSR | S_IWUSR, d,
4920ade0b62SCong Wang 				    &decay_interval, &decay_interval_ops);
493011d8261SBorislav Petkov 	if (!decay) {
494011d8261SBorislav Petkov 		pr_warn("Error creating decay_interval debugfs node!\n");
495011d8261SBorislav Petkov 		goto err;
496011d8261SBorislav Petkov 	}
497011d8261SBorislav Petkov 
498b8b5ca66SBorislav Petkov 	count = debugfs_create_file("action_threshold", S_IRUSR | S_IWUSR, d,
499b8b5ca66SBorislav Petkov 				    &action_threshold, &action_threshold_ops);
50032288dafSChristophe JAILLET 	if (!count) {
501b8b5ca66SBorislav Petkov 		pr_warn("Error creating action_threshold debugfs node!\n");
502011d8261SBorislav Petkov 		goto err;
503011d8261SBorislav Petkov 	}
504011d8261SBorislav Petkov 
50560fd42d2STony Luck 	if (!IS_ENABLED(CONFIG_RAS_CEC_DEBUG))
50660fd42d2STony Luck 		return 0;
50760fd42d2STony Luck 
50860fd42d2STony Luck 	pfn = debugfs_create_file("pfn", S_IRUSR | S_IWUSR, d, &dfs_pfn, &pfn_ops);
50960fd42d2STony Luck 	if (!pfn) {
51060fd42d2STony Luck 		pr_warn("Error creating pfn debugfs node!\n");
51160fd42d2STony Luck 		goto err;
51260fd42d2STony Luck 	}
51360fd42d2STony Luck 
5144bd442e9SQinglang Miao 	array = debugfs_create_file("array", S_IRUSR, d, NULL, &array_fops);
51560fd42d2STony Luck 	if (!array) {
51660fd42d2STony Luck 		pr_warn("Error creating array debugfs node!\n");
51760fd42d2STony Luck 		goto err;
51860fd42d2STony Luck 	}
519011d8261SBorislav Petkov 
520011d8261SBorislav Petkov 	return 0;
521011d8261SBorislav Petkov 
522011d8261SBorislav Petkov err:
523011d8261SBorislav Petkov 	debugfs_remove_recursive(d);
524011d8261SBorislav Petkov 
525011d8261SBorislav Petkov 	return 1;
526011d8261SBorislav Petkov }
527011d8261SBorislav Petkov 
cec_notifier(struct notifier_block * nb,unsigned long val,void * data)5289554bfe4STony Luck static int cec_notifier(struct notifier_block *nb, unsigned long val,
5299554bfe4STony Luck 			void *data)
5309554bfe4STony Luck {
5319554bfe4STony Luck 	struct mce *m = (struct mce *)data;
5329554bfe4STony Luck 
5339554bfe4STony Luck 	if (!m)
5349554bfe4STony Luck 		return NOTIFY_DONE;
5359554bfe4STony Luck 
5369554bfe4STony Luck 	/* We eat only correctable DRAM errors with usable addresses. */
5379554bfe4STony Luck 	if (mce_is_memory_error(m) &&
5389554bfe4STony Luck 	    mce_is_correctable(m)  &&
53923ba710aSTony Luck 	    mce_usable_address(m)) {
54023ba710aSTony Luck 		if (!cec_add_elem(m->addr >> PAGE_SHIFT)) {
54123ba710aSTony Luck 			m->kflags |= MCE_HANDLED_CEC;
54223ba710aSTony Luck 			return NOTIFY_OK;
54323ba710aSTony Luck 		}
54423ba710aSTony Luck 	}
5459554bfe4STony Luck 
5469554bfe4STony Luck 	return NOTIFY_DONE;
5479554bfe4STony Luck }
5489554bfe4STony Luck 
5499554bfe4STony Luck static struct notifier_block cec_nb = {
5509554bfe4STony Luck 	.notifier_call	= cec_notifier,
5519554bfe4STony Luck 	.priority	= MCE_PRIO_CEC,
5529554bfe4STony Luck };
5539554bfe4STony Luck 
cec_init(void)55485e6084eSLuca Stefani static int __init cec_init(void)
555011d8261SBorislav Petkov {
556011d8261SBorislav Petkov 	if (ce_arr.disabled)
55785e6084eSLuca Stefani 		return -ENODEV;
558011d8261SBorislav Petkov 
559*d25c6948STony Luck 	/*
560*d25c6948STony Luck 	 * Intel systems may avoid uncorrectable errors
561*d25c6948STony Luck 	 * if pages with corrected errors are aggressively
562*d25c6948STony Luck 	 * taken offline.
563*d25c6948STony Luck 	 */
564*d25c6948STony Luck 	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
565*d25c6948STony Luck 		action_threshold = 2;
566*d25c6948STony Luck 
567011d8261SBorislav Petkov 	ce_arr.array = (void *)get_zeroed_page(GFP_KERNEL);
568011d8261SBorislav Petkov 	if (!ce_arr.array) {
569011d8261SBorislav Petkov 		pr_err("Error allocating CE array page!\n");
57085e6084eSLuca Stefani 		return -ENOMEM;
571011d8261SBorislav Petkov 	}
572011d8261SBorislav Petkov 
573d0e375e8SBorislav Petkov 	if (create_debugfs_nodes()) {
574d0e375e8SBorislav Petkov 		free_page((unsigned long)ce_arr.array);
57585e6084eSLuca Stefani 		return -ENOMEM;
576d0e375e8SBorislav Petkov 	}
577011d8261SBorislav Petkov 
5780ade0b62SCong Wang 	INIT_DELAYED_WORK(&cec_work, cec_work_fn);
5790ade0b62SCong Wang 	schedule_delayed_work(&cec_work, CEC_DECAY_DEFAULT_INTERVAL);
580011d8261SBorislav Petkov 
5819554bfe4STony Luck 	mce_register_decode_chain(&cec_nb);
5829554bfe4STony Luck 
583011d8261SBorislav Petkov 	pr_info("Correctable Errors collector initialized.\n");
58485e6084eSLuca Stefani 	return 0;
585011d8261SBorislav Petkov }
5869554bfe4STony Luck late_initcall(cec_init);
587011d8261SBorislav Petkov 
parse_cec_param(char * str)588011d8261SBorislav Petkov int __init parse_cec_param(char *str)
589011d8261SBorislav Petkov {
590011d8261SBorislav Petkov 	if (!str)
591011d8261SBorislav Petkov 		return 0;
592011d8261SBorislav Petkov 
593011d8261SBorislav Petkov 	if (*str == '=')
594011d8261SBorislav Petkov 		str++;
595011d8261SBorislav Petkov 
59669a33000SNicolas Iooss 	if (!strcmp(str, "cec_disable"))
597011d8261SBorislav Petkov 		ce_arr.disabled = 1;
598011d8261SBorislav Petkov 	else
599011d8261SBorislav Petkov 		return 0;
600011d8261SBorislav Petkov 
601011d8261SBorislav Petkov 	return 1;
602011d8261SBorislav Petkov }
603