megaraid: fix use of delayed work
Subsystems (3)
PATCH 1/1 megaraid: fix use of delayed work
megaraid use INIT_WORK to declare a hotplug_work, but cast the hotplug_work from work_struct to delayed_work and schedule_delayed_work on it. This is very dangerous, as other part of delayed_work might be kernel memories allocated by others.
With commit 8852aac, schedule_delayed_work() will check dwork->timer before queue_work, this will cause megaraid code to hit the BUG_ON in workqueue code. Change megaraid code to use delayed work.
--- drivers/scsi/megaraid/megaraid_sas.h | 2 +- drivers/scsi/megaraid/megaraid_sas_base.c | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-)
drivers/scsi/megaraid/megaraid_sas.h +1 -1
@@ -1276,7 +1276,7 @@ struct megasas_evt_detail { } __attribute__ ((packed));struct megasas_aen_event { -struct work_struct hotplug_work; +struct delayed_work hotplug_work;struct megasas_instance *instance; };
drivers/scsi/megaraid/megaraid_sas_base.c +6 -8
@@ -2060,9 +2060,9 @@ megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd) }else { ev->instance = instance; instance->ev = ev; -INIT_WORK (&ev->hotplug_work, megasas_aen_polling); -schedule_delayed_work ( - (struct delayed_work *)&ev->hotplug_work,0 ); +INIT_DELAYED_WORK (&ev->hotplug_work, + megasas_aen_polling); +schedule_delayed_work (&ev->hotplug_work,0 ); } } }
@@ -4352,8 +4352,7 @@ megasas_suspend(struct pci_dev *pdev, pm_message_t state)/* cancel the delayed work if this work still in queue */ if (instance->ev !=NULL ) {struct megasas_aen_event *ev = instance->ev; -cancel_delayed_work_sync ( - (struct delayed_work *)&ev->hotplug_work); +cancel_delayed_work_sync (&ev->hotplug_work); instance->ev =NULL ; }
@@ -4545,8 +4544,7 @@ static void __devexit megasas_detach_one(struct pci_dev *pdev)/* cancel the delayed work if this work still in queue*/ if (instance->ev !=NULL ) {struct megasas_aen_event *ev = instance->ev; -cancel_delayed_work_sync ( - (struct delayed_work *)&ev->hotplug_work); +cancel_delayed_work_sync (&ev->hotplug_work); instance->ev =NULL ; }
@@ -5190,7 +5188,7 @@ static void megasas_aen_polling(struct work_struct *work) {struct megasas_aen_event *ev = - container_of(work, struct megasas_aen_event, hotplug_work); + container_of(work, struct megasas_aen_event, hotplug_work.work);struct megasas_instance *instance = ev->instance;union megasas_evt_class_locale class_locale;struct Scsi_Host *host;
Signature · 1 line
1.7.9.6 (Apple Git-31.1)
Tejun Heo
maintainerQuoted 9 lines · Xiaotian Feng
megaraid use
INIT_WORKto declare ahotplug_work, but cast thehotplug_workfromwork_structtodelayed_workandschedule_delayed_workon it. This is very dangerous, as other part ofdelayed_workmight be kernel memories allocated by others.With commit 8852aac,
schedule_delayed_work()will checkdwork->timerbeforequeue_work, this will cause megaraid code to hit theBUG_ONin workqueue code. Change megaraid code to use delayed work.
Quoted 5 lines
Signed-off-by Xiaotian Feng <[email protected]>Cc Tejun Heo <[email protected]>Cc Neela Syam Kolli <[email protected]>Cc "James E.J. Bottomley" <[email protected]>
Urgh... what the.... Didn't see that one coming. I'm gonna push this to Linus through the workqueue tree.
Thanks for the fix.
Signature · 1 line
tejun
Tejun Heo
maintainerPATCH 1/2 megaraid: fix BUG_ON() from incorrect use of delayed work
In reply to Xiaotian FengQuoted 1 line
From
c1d390d8e6128b050f0f66b1c33d390760deb3f4Mon Sep 17 00:00:00 2001
From: Xiaotian Feng <[email protected]>
Date: Tue, 4 Dec 2012 19:33:54 +0800
megaraid use INIT_WORK to declare a hotplug_work, but cast the hotplug_work from work_struct to delayed_work and schedule_delayed_work on it. This is very dangerous, as other part of delayed_work might be kernel memories allocated by others.
With commit 8852aac ("workqueue: mod_delayed_work_on() shouldn't queue timer on 0 delay"), schedule_delayed_work() will check dwork->timer before queue_work even when @delay is 0, this causes megaraid code to hit the BUG_ON() in workqueue code. Change megaraid code to use delayed work.
--- drivers/scsi/megaraid/megaraid_sas.h | 2 +- drivers/scsi/megaraid/megaraid_sas_base.c | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-)
drivers/scsi/megaraid/megaraid_sas.h +1 -1
@@ -1276,7 +1276,7 @@ struct megasas_evt_detail { } __attribute__ ((packed));struct megasas_aen_event { -struct work_struct hotplug_work; +struct delayed_work hotplug_work;struct megasas_instance *instance; };
drivers/scsi/megaraid/megaraid_sas_base.c +6 -8
@@ -2060,9 +2060,9 @@ megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd) }else { ev->instance = instance; instance->ev = ev; -INIT_WORK (&ev->hotplug_work, megasas_aen_polling); -schedule_delayed_work ( - (struct delayed_work *)&ev->hotplug_work,0 ); +INIT_DELAYED_WORK (&ev->hotplug_work, + megasas_aen_polling); +schedule_delayed_work (&ev->hotplug_work,0 ); } } }
@@ -4352,8 +4352,7 @@ megasas_suspend(struct pci_dev *pdev, pm_message_t state)/* cancel the delayed work if this work still in queue */ if (instance->ev !=NULL ) {struct megasas_aen_event *ev = instance->ev; -cancel_delayed_work_sync ( - (struct delayed_work *)&ev->hotplug_work); +cancel_delayed_work_sync (&ev->hotplug_work); instance->ev =NULL ; }
@@ -4545,8 +4544,7 @@ static void __devexit megasas_detach_one(struct pci_dev *pdev)/* cancel the delayed work if this work still in queue*/ if (instance->ev !=NULL ) {struct megasas_aen_event *ev = instance->ev; -cancel_delayed_work_sync ( - (struct delayed_work *)&ev->hotplug_work); +cancel_delayed_work_sync (&ev->hotplug_work); instance->ev =NULL ; }
@@ -5190,7 +5188,7 @@ static void megasas_aen_polling(struct work_struct *work) {struct megasas_aen_event *ev = - container_of(work, struct megasas_aen_event, hotplug_work); + container_of(work, struct megasas_aen_event, hotplug_work.work);struct megasas_instance *instance = ev->instance;union megasas_evt_class_locale class_locale;struct Scsi_Host *host;
Signature · 1 line
1.7.11.7
Tejun Heo
maintainerPATCH 2/2 workqueue: convert BUG_ON()s in __queue_delayed_work() to WARN_ON_ONCE()s
Quoted 1 line
From
87aa1e796ff6d491b5ed4e5663e5a4e449ac513bMon Sep 17 00:00:00 2001
From: Tejun Heo <[email protected]>
Date: Tue, 4 Dec 2012 07:40:39 -0800
8852aac25e ("workqueue: mod_delayed_work_on() shouldn't queue timer on 0 delay") unexpectedly uncovered a very nasty abuse of delayed_work in megaraid - it allocated work_struct, used container_of() to cast it to delayed_work and then pass that into queue_delayed_work().
Previously, this was okay because 0 @delay short-circuited to queue_work() before doing anything with delayed_work. 8852aac25e moved 0 @delay test into __queue_delayed_work() after sanity check on delayed_work making megaraid trigger BUG_ON().
Although megaraid is already fixed by c1d390d8e6 ("megaraid: fix BUG_ON() from incorrect use of delayed work"), this patch converts BUG_ON()s in __queue_delayed_work() to WARN_ON_ONCE()s so that such abusers, if there are more, trigger warning but don't crash the machine.
--- kernel/workqueue.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/workqueue.c +2 -2
@@ -1361,8 +1361,8 @@ static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,WARN_ON_ONCE (timer->function != delayed_work_timer_fn || timer->data != (unsigned long )dwork); -BUG_ON (timer_pending (timer)); -BUG_ON (!list_empty (&work->entry)); +WARN_ON_ONCE (timer_pending (timer)); +WARN_ON_ONCE (!list_empty (&work->entry));/* * If @delay is 0, queue @dwork->work immediately. This is for
Signature · 1 line
1.7.11.7
Tejun Heo
maintainerHello, again, Xiaotian.
Quoted 4 lines · Tejun Heo
Urgh... what the.... Didn't see that one coming. I'm gonna push this to Linus through the workqueue tree.
Thanks for the fix.
It seems like megaraid doesn't have any reason to use delayed_work at all. It can use a plain work and cancel_work_sync() instead. Maybe it was doing the container_of() thing because workqueue didn't use to have cancel_work_sync()? Anyways, please convert it to use plain work_struct post 3.7.
Thanks.
Signature · 1 line
tejun
Re: [PATCH 1/2] megaraid: fix BUG_ON() from incorrect use of delayed work
In reply to Tejun Heodrivers/scsi/megaraid/megaraid_sas_base.c static void · line 5194
Quoted 9 lines · Tejun Heo
drivers/scsi/megaraid/megaraid_sas_base.c +1
-1@@ -5190,7 +5188,7 @@ static void megasas_aen_polling(struct work_struct *work) {struct megasas_aen_event *ev = - container_of(work, struct megasas_aen_event, hotplug_work); + container_of(work, struct megasas_aen_event, hotplug_work.work);struct megasas_instance *instance = ev->instance;union megasas_evt_class_locale class_locale;struct Scsi_Host *host;
-megasas_aen_polling(struct work_struct *work)
+megasas_aen_polling(struct delayed_work *work)
Not really sure. Just asking?
--nX
Re: [PATCH 1/2] megaraid: fix BUG_ON() from incorrect use of delayed work
Quoted 14 lines · Daniel Vacek
Quoted 9 lines · Tejun Heo
drivers/scsi/megaraid/megaraid_sas_base.c +1
-1@@ -5190,7 +5188,7 @@ static void megasas_aen_polling(struct work_struct *work) {struct megasas_aen_event *ev = - container_of(work, struct megasas_aen_event, hotplug_work); + container_of(work, struct megasas_aen_event, hotplug_work.work);struct megasas_instance *instance = ev->instance;union megasas_evt_class_locale class_locale;struct Scsi_Host *host;(quoted diff) +1
-1-megasas_aen_polling(struct work_struct *work) +megasas_aen_polling(struct delayed_work *work)
Quoted 1 line
Not really sure. Just asking?
Never mind, it's ok. Now I get it. Sorry for the buzz.
--nX