Skip to content

Commit f763cf8

Browse files
committed
Merge branch 'ras-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull RAS fixes from Thomas Gleixner: "Two small fixes for RAS: - Use a proper search algorithm to find the correct element in the CEC array. The replacement was a better choice than fixing the crash causes by the original search function with horrible duct tape. - Move the timer based decay function into thread context so it can actually acquire the mutex which protects the CEC array to prevent corruption" * 'ras-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: RAS/CEC: Convert the timer callback to a workqueue RAS/CEC: Fix binary search function
2 parents e01e060 + 0ade0b6 commit f763cf8

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

drivers/ras/cec.c

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <linux/mm.h>
33
#include <linux/gfp.h>
44
#include <linux/kernel.h>
5+
#include <linux/workqueue.h>
56

67
#include <asm/mce.h>
78

@@ -123,16 +124,12 @@ static u64 dfs_pfn;
123124
/* Amount of errors after which we offline */
124125
static unsigned int count_threshold = COUNT_MASK;
125126

126-
/*
127-
* The timer "decays" element count each timer_interval which is 24hrs by
128-
* default.
129-
*/
130-
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;
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;
136133

137134
/*
138135
* Decrement decay value. We're using DECAY_BITS bits to denote decay of an
@@ -160,20 +157,21 @@ static void do_spring_cleaning(struct ce_array *ca)
160157
/*
161158
* @interval in seconds
162159
*/
163-
static void cec_mod_timer(struct timer_list *t, unsigned long interval)
160+
static void cec_mod_work(unsigned long interval)
164161
{
165162
unsigned long iv;
166163

167-
iv = interval * HZ + jiffies;
168-
169-
mod_timer(t, round_jiffies(iv));
164+
iv = interval * HZ;
165+
mod_delayed_work(system_wq, &cec_work, round_jiffies(iv));
170166
}
171167

172-
static void cec_timer_fn(struct timer_list *unused)
168+
static void cec_work_fn(struct work_struct *work)
173169
{
170+
mutex_lock(&ce_mutex);
174171
do_spring_cleaning(&ce_arr);
172+
mutex_unlock(&ce_mutex);
175173

176-
cec_mod_timer(&cec_timer, timer_interval);
174+
cec_mod_work(decay_interval);
177175
}
178176

179177
/*
@@ -183,32 +181,38 @@ static void cec_timer_fn(struct timer_list *unused)
183181
*/
184182
static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
185183
{
184+
int min = 0, max = ca->n - 1;
186185
u64 this_pfn;
187-
int min = 0, max = ca->n;
188186

189-
while (min < max) {
190-
int tmp = (max + min) >> 1;
187+
while (min <= max) {
188+
int i = (min + max) >> 1;
191189

192-
this_pfn = PFN(ca->array[tmp]);
190+
this_pfn = PFN(ca->array[i]);
193191

194192
if (this_pfn < pfn)
195-
min = tmp + 1;
193+
min = i + 1;
196194
else if (this_pfn > pfn)
197-
max = tmp;
198-
else {
199-
min = tmp;
200-
break;
195+
max = i - 1;
196+
else if (this_pfn == pfn) {
197+
if (to)
198+
*to = i;
199+
200+
return i;
201201
}
202202
}
203203

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+
*/
204213
if (to)
205214
*to = min;
206215

207-
this_pfn = PFN(ca->array[min]);
208-
209-
if (this_pfn == pfn)
210-
return min;
211-
212216
return -ENOKEY;
213217
}
214218

@@ -374,15 +378,15 @@ static int decay_interval_set(void *data, u64 val)
374378
{
375379
*(u64 *)data = val;
376380

377-
if (val < CEC_TIMER_MIN_INTERVAL)
381+
if (val < CEC_DECAY_MIN_INTERVAL)
378382
return -EINVAL;
379383

380-
if (val > CEC_TIMER_MAX_INTERVAL)
384+
if (val > CEC_DECAY_MAX_INTERVAL)
381385
return -EINVAL;
382386

383-
timer_interval = val;
387+
decay_interval = val;
384388

385-
cec_mod_timer(&cec_timer, timer_interval);
389+
cec_mod_work(decay_interval);
386390
return 0;
387391
}
388392
DEFINE_DEBUGFS_ATTRIBUTE(decay_interval_ops, u64_get, decay_interval_set, "%lld\n");
@@ -426,7 +430,7 @@ static int array_dump(struct seq_file *m, void *v)
426430

427431
seq_printf(m, "Flags: 0x%x\n", ca->flags);
428432

429-
seq_printf(m, "Timer interval: %lld seconds\n", timer_interval);
433+
seq_printf(m, "Decay interval: %lld seconds\n", decay_interval);
430434
seq_printf(m, "Decays: %lld\n", ca->decays_done);
431435

432436
seq_printf(m, "Action threshold: %d\n", count_threshold);
@@ -472,7 +476,7 @@ static int __init create_debugfs_nodes(void)
472476
}
473477

474478
decay = debugfs_create_file("decay_interval", S_IRUSR | S_IWUSR, d,
475-
&timer_interval, &decay_interval_ops);
479+
&decay_interval, &decay_interval_ops);
476480
if (!decay) {
477481
pr_warn("Error creating decay_interval debugfs node!\n");
478482
goto err;
@@ -508,8 +512,8 @@ void __init cec_init(void)
508512
if (create_debugfs_nodes())
509513
return;
510514

511-
timer_setup(&cec_timer, cec_timer_fn, 0);
512-
cec_mod_timer(&cec_timer, CEC_TIMER_DEFAULT_INTERVAL);
515+
INIT_DELAYED_WORK(&cec_work, cec_work_fn);
516+
schedule_delayed_work(&cec_work, CEC_DECAY_DEFAULT_INTERVAL);
513517

514518
pr_info("Correctable Errors collector initialized.\n");
515519
}

0 commit comments

Comments
 (0)