Skip to content

Commit bf52fa4

Browse files
Doug ThompsonLinus Torvalds
authored andcommitted
drivers/edac: fix workq reset deadlock
Fix mutex locking deadlock on the device controller linked list. Was calling a lock then a function that could call the same lock. Moved the cancel workq function to outside the lock Added some short circuit logic in the workq code Added comments of description Code tidying Signed-off-by: Doug Thompson <dougthompson@xmission.com> Cc: Greg KH <greg@kroah.com> Cc: Alan Cox <alan@lxorguk.ukuu.org.uk> Cc: Oleg Nesterov <oleg@tv-sign.ru> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent fb3fb20 commit bf52fa4

2 files changed

Lines changed: 100 additions & 28 deletions

File tree

‎drivers/edac/edac_device.c‎

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
#include "edac_core.h"
3333
#include "edac_module.h"
3434

35-
/* lock to memory controller's control array 'edac_device_list' */
35+
/* lock for the list: 'edac_device_list', manipulation of this list
36+
* is protected by the 'device_ctls_mutex' lock
37+
*/
3638
static DEFINE_MUTEX(device_ctls_mutex);
3739
static struct list_head edac_device_list = LIST_HEAD_INIT(edac_device_list);
3840

@@ -386,6 +388,14 @@ EXPORT_SYMBOL_GPL(edac_device_find);
386388
/*
387389
* edac_device_workq_function
388390
* performs the operation scheduled by a workq request
391+
*
392+
* this workq is embedded within an edac_device_ctl_info
393+
* structure, that needs to be polled for possible error events.
394+
*
395+
* This operation is to acquire the list mutex lock
396+
* (thus preventing insertation or deletion)
397+
* and then call the device's poll function IFF this device is
398+
* running polled and there is a poll function defined.
389399
*/
390400
static void edac_device_workq_function(struct work_struct *work_req)
391401
{
@@ -403,8 +413,17 @@ static void edac_device_workq_function(struct work_struct *work_req)
403413

404414
mutex_unlock(&device_ctls_mutex);
405415

406-
/* Reschedule */
407-
queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
416+
/* Reschedule the workq for the next time period to start again
417+
* if the number of msec is for 1 sec, then adjust to the next
418+
* whole one second to save timers fireing all over the period
419+
* between integral seconds
420+
*/
421+
if (edac_dev->poll_msec == 1000)
422+
queue_delayed_work(edac_workqueue, &edac_dev->work,
423+
round_jiffies(edac_dev->delay));
424+
else
425+
queue_delayed_work(edac_workqueue, &edac_dev->work,
426+
edac_dev->delay);
408427
}
409428

410429
/*
@@ -417,11 +436,26 @@ void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
417436
{
418437
debugf0("%s()\n", __func__);
419438

439+
/* take the arg 'msec' and set it into the control structure
440+
* to used in the time period calculation
441+
* then calc the number of jiffies that represents
442+
*/
420443
edac_dev->poll_msec = msec;
421-
edac_dev->delay = msecs_to_jiffies(msec); /* Calc delay jiffies */
444+
edac_dev->delay = msecs_to_jiffies(msec);
422445

423446
INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
424-
queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
447+
448+
/* optimize here for the 1 second case, which will be normal value, to
449+
* fire ON the 1 second time event. This helps reduce all sorts of
450+
* timers firing on sub-second basis, while they are happy
451+
* to fire together on the 1 second exactly
452+
*/
453+
if (edac_dev->poll_msec == 1000)
454+
queue_delayed_work(edac_workqueue, &edac_dev->work,
455+
round_jiffies(edac_dev->delay));
456+
else
457+
queue_delayed_work(edac_workqueue, &edac_dev->work,
458+
edac_dev->delay);
425459
}
426460

427461
/*
@@ -441,16 +475,20 @@ void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
441475

442476
/*
443477
* edac_device_reset_delay_period
478+
*
479+
* need to stop any outstanding workq queued up at this time
480+
* because we will be resetting the sleep time.
481+
* Then restart the workq on the new delay
444482
*/
445-
446483
void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
447484
unsigned long value)
448485
{
449-
mutex_lock(&device_ctls_mutex);
450-
451-
/* cancel the current workq request */
486+
/* cancel the current workq request, without the mutex lock */
452487
edac_device_workq_teardown(edac_dev);
453488

489+
/* acquire the mutex before doing the workq setup */
490+
mutex_lock(&device_ctls_mutex);
491+
454492
/* restart the workq request, with new delay value */
455493
edac_device_workq_setup(edac_dev, value);
456494

‎drivers/edac/edac_mc.c‎

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@ static void edac_mc_workq_function(struct work_struct *work_req)
258258

259259
mutex_lock(&mem_ctls_mutex);
260260

261+
/* if this control struct has movd to offline state, we are done */
262+
if (mci->op_state == OP_OFFLINE) {
263+
mutex_unlock(&mem_ctls_mutex);
264+
return;
265+
}
266+
261267
/* Only poll controllers that are running polled and have a check */
262268
if (edac_mc_assert_error_check_and_clear() && (mci->edac_check != NULL))
263269
mci->edac_check(mci);
@@ -279,41 +285,59 @@ static void edac_mc_workq_function(struct work_struct *work_req)
279285
* edac_mc_workq_setup
280286
* initialize a workq item for this mci
281287
* passing in the new delay period in msec
288+
*
289+
* locking model:
290+
*
291+
* called with the mem_ctls_mutex held
282292
*/
283-
void edac_mc_workq_setup(struct mem_ctl_info *mci, unsigned msec)
293+
static void edac_mc_workq_setup(struct mem_ctl_info *mci, unsigned msec)
284294
{
285295
debugf0("%s()\n", __func__);
286296

297+
/* if this instance is not in the POLL state, then simply return */
298+
if (mci->op_state != OP_RUNNING_POLL)
299+
return;
300+
287301
INIT_DELAYED_WORK(&mci->work, edac_mc_workq_function);
288302
queue_delayed_work(edac_workqueue, &mci->work, msecs_to_jiffies(msec));
289303
}
290304

291305
/*
292306
* edac_mc_workq_teardown
293307
* stop the workq processing on this mci
308+
*
309+
* locking model:
310+
*
311+
* called WITHOUT lock held
294312
*/
295-
void edac_mc_workq_teardown(struct mem_ctl_info *mci)
313+
static void edac_mc_workq_teardown(struct mem_ctl_info *mci)
296314
{
297315
int status;
298316

299-
status = cancel_delayed_work(&mci->work);
300-
if (status == 0) {
301-
/* workq instance might be running, wait for it */
302-
flush_workqueue(edac_workqueue);
317+
/* if not running POLL, leave now */
318+
if (mci->op_state == OP_RUNNING_POLL) {
319+
status = cancel_delayed_work(&mci->work);
320+
if (status == 0) {
321+
debugf0("%s() not canceled, flush the queue\n",
322+
__func__);
323+
324+
/* workq instance might be running, wait for it */
325+
flush_workqueue(edac_workqueue);
326+
}
303327
}
304328
}
305329

306330
/*
307331
* edac_reset_delay_period
308332
*/
309-
310-
void edac_reset_delay_period(struct mem_ctl_info *mci, unsigned long value)
333+
static void edac_reset_delay_period(struct mem_ctl_info *mci, unsigned long value)
311334
{
312-
mutex_lock(&mem_ctls_mutex);
313-
314335
/* cancel the current workq request */
315336
edac_mc_workq_teardown(mci);
316337

338+
/* lock the list of devices for the new setup */
339+
mutex_lock(&mem_ctls_mutex);
340+
317341
/* restart the workq request, with new delay value */
318342
edac_mc_workq_setup(mci, value);
319343

@@ -323,6 +347,10 @@ void edac_reset_delay_period(struct mem_ctl_info *mci, unsigned long value)
323347
/* Return 0 on success, 1 on failure.
324348
* Before calling this function, caller must
325349
* assign a unique value to mci->mc_idx.
350+
*
351+
* locking model:
352+
*
353+
* called with the mem_ctls_mutex lock held
326354
*/
327355
static int add_mc_to_global_list(struct mem_ctl_info *mci)
328356
{
@@ -331,7 +359,8 @@ static int add_mc_to_global_list(struct mem_ctl_info *mci)
331359

332360
insert_before = &mc_devices;
333361

334-
if (unlikely((p = find_mci_by_dev(mci->dev)) != NULL))
362+
p = find_mci_by_dev(mci->dev);
363+
if (unlikely(p != NULL))
335364
goto fail0;
336365

337366
list_for_each(item, &mc_devices) {
@@ -467,8 +496,8 @@ int edac_mc_add_mc(struct mem_ctl_info *mci)
467496
}
468497

469498
/* Report action taken */
470-
edac_mc_printk(mci, KERN_INFO, "Giving out device to %s %s: DEV %s\n",
471-
mci->mod_name, mci->ctl_name, dev_name(mci));
499+
edac_mc_printk(mci, KERN_INFO, "Giving out device to '%s' '%s':"
500+
" DEV %s\n", mci->mod_name, mci->ctl_name, dev_name(mci));
472501

473502
mutex_unlock(&mem_ctls_mutex);
474503
return 0;
@@ -493,26 +522,31 @@ struct mem_ctl_info *edac_mc_del_mc(struct device *dev)
493522
{
494523
struct mem_ctl_info *mci;
495524

496-
debugf0("MC: %s()\n", __func__);
525+
debugf0("%s()\n", __func__);
526+
497527
mutex_lock(&mem_ctls_mutex);
498528

499-
if ((mci = find_mci_by_dev(dev)) == NULL) {
529+
/* find the requested mci struct in the global list */
530+
mci = find_mci_by_dev(dev);
531+
if (mci == NULL) {
500532
mutex_unlock(&mem_ctls_mutex);
501533
return NULL;
502534
}
503535

504536
/* marking MCI offline */
505537
mci->op_state = OP_OFFLINE;
506538

507-
/* flush workq processes */
508-
edac_mc_workq_teardown(mci);
509-
510-
edac_remove_sysfs_mci_device(mci);
511539
del_mc_from_global_list(mci);
512540
mutex_unlock(&mem_ctls_mutex);
541+
542+
/* flush workq processes and remove sysfs */
543+
edac_mc_workq_teardown(mci);
544+
edac_remove_sysfs_mci_device(mci);
545+
513546
edac_printk(KERN_INFO, EDAC_MC,
514547
"Removed device %d for %s %s: DEV %s\n", mci->mc_idx,
515548
mci->mod_name, mci->ctl_name, dev_name(mci));
549+
516550
return mci;
517551
}
518552
EXPORT_SYMBOL_GPL(edac_mc_del_mc);

0 commit comments

Comments
 (0)