HID: asus: Add check for cancelling fn_lock_sync_work
PATCH 1/1 HID: asus: Add check for cancelling fn_lock_sync_work
syzbot reported a workqueue warning where fn_lock_sync_work is cancelled during device removal before the work has been initialized. This can happen when the device is disconnected while initialization is still in progress.
Fix this by initializing fn_lock_sync_work before marking fn_lock as enabled, and by using fn_lock as a flag in the remove path. This ensures cancel_work_sync() is only called for initialized work.
--- drivers/hid/hid-asus.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hid/hid-asus.c +2 -2
@@ -960,8 +960,8 @@ static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi) }if (drvdata->quirks &QUIRK_HID_FN_LOCK ) { - drvdata->fn_lock = true;INIT_WORK (&drvdata->fn_lock_sync_work, asus_sync_fn_lock); + drvdata->fn_lock = true;asus_kbd_set_fn_lock (hdev, true); }
@@ -1343,7 +1343,7 @@ static void asus_remove(struct hid_device *hdev)cancel_work_sync (&drvdata->kbd_backlight->work); } -if (drvdata->quirks &QUIRK_HID_FN_LOCK ) +if ((drvdata->quirks &QUIRK_HID_FN_LOCK ) && drvdata->fn_lock)cancel_work_sync (&drvdata->fn_lock_sync_work);hid_hw_stop (hdev);
Signature · 1 line
2.50.1
Quoted 11 lines · Sahil Chandna
syzbot reported a workqueue warning where
fn_lock_sync_workis cancelled during device removal before the work has been initialized. This can happen when the device is disconnected while initialization is still in progress.
Fix this by initializingfn_lock_sync_workbefore markingfn_lockas enabled, and by usingfn_lockas a flag in the remove path. This ensurescancel_work_sync()is only called for initialized work.Fixes f631011e36b8 ("HID: hid-asus: Implement fn lock for Asus ProArt P16")Reported-by [email protected]Signed-off-by Sahil Chandna <[email protected]>
Applied to hid.git#for-6.20/asus, thanks.
Signature · 2 lines
Jiri Kosina SUSE Labs
I just noticed this because of a related message in a different thread.
Quoted 30 lines · Sahil Chandna
syzbot reported a workqueue warning where
fn_lock_sync_workis cancelled during device removal before the work has been initialized. This can happen when the device is disconnected while initialization is still in progress.
Fix this by initializingfn_lock_sync_workbefore markingfn_lockas enabled, and by usingfn_lockas a flag in the remove path. This ensurescancel_work_sync()is only called for initialized work.Fixes f631011e36b8 ("HID: hid-asus: Implement fn lock for Asus ProArt P16")Reported-by [email protected]Signed-off-by Sahil Chandna <[email protected]>--- drivers/hid/hid-asus.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/hid/hid-asus.c +1
-1@@ -960,8 +960,8 @@ static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi) }if (drvdata->quirks &QUIRK_HID_FN_LOCK ) { - drvdata->fn_lock = true;INIT_WORK (&drvdata->fn_lock_sync_work, asus_sync_fn_lock); + drvdata->fn_lock = true;asus_kbd_set_fn_lock (hdev, true); }
drivers/hid/hid-asus.c static void asus_remove(struct hid_device *hdev) · line 1349
Quoted 8 lines
drivers/hid/hid-asus.c +1
-1@@ -1343,7 +1343,7 @@ static void asus_remove(struct hid_device *hdev)cancel_work_sync (&drvdata->kbd_backlight->work); } -if (drvdata->quirks &QUIRK_HID_FN_LOCK ) +if ((drvdata->quirks &QUIRK_HID_FN_LOCK ) && drvdata->fn_lock)cancel_work_sync (&drvdata->fn_lock_sync_work);hid_hw_stop (hdev);
With no synchronization between the two routines, this patch cannot possibly be correct. There's nothing to prevent the CPU running asus_input_configured() from executing the assignment to drvdata->fn_lock before doing the INIT_WORK() (unless the INIT_WORK() call itself contains some synchronization -- but obviously the code shouldn't depend on that).
At the very least there should be a pair of memory barriers. A more palatable substitute would be to protect both regions of code with a mutex.
Alan Stern
Quoted 50 lines · Alan Stern
I just noticed this because of a related message in a different thread.
Quoted 30 lines · Sahil Chandna
syzbot reported a workqueue warning where
fn_lock_sync_workis cancelled during device removal before the work has been initialized. This can happen when the device is disconnected while initialization is still in progress.
Fix this by initializingfn_lock_sync_workbefore markingfn_lockas enabled, and by usingfn_lockas a flag in the remove path. This ensurescancel_work_sync()is only called for initialized work.Fixes f631011e36b8 ("HID: hid-asus: Implement fn lock for Asus ProArt P16")Reported-by [email protected]Signed-off-by Sahil Chandna <[email protected]>--- drivers/hid/hid-asus.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/hid/hid-asus.c +1
-1@@ -960,8 +960,8 @@ static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi) }if (drvdata->quirks &QUIRK_HID_FN_LOCK ) { - drvdata->fn_lock = true;INIT_WORK (&drvdata->fn_lock_sync_work, asus_sync_fn_lock); + drvdata->fn_lock = true;asus_kbd_set_fn_lock (hdev, true); }drivers/hid/hid-asus.c static void asus_remove(struct hid_device *hdev) · line 1349
Quoted 8 lines
drivers/hid/hid-asus.c +1
-1@@ -1343,7 +1343,7 @@ static void asus_remove(struct hid_device *hdev)cancel_work_sync (&drvdata->kbd_backlight->work); } -if (drvdata->quirks &QUIRK_HID_FN_LOCK ) +if ((drvdata->quirks &QUIRK_HID_FN_LOCK ) && drvdata->fn_lock)cancel_work_sync (&drvdata->fn_lock_sync_work);hid_hw_stop (hdev);With no synchronization between the two routines, this patch cannot possibly be correct. There's nothing to prevent the CPU running
asus_input_configured()from executing the assignment todrvdata->fn_lockbefore doing theINIT_WORK()(unless theINIT_WORK()call itself contains some synchronization -- but obviously the code shouldn't depend on that).
Quoted 5 lines
At the very least there should be a pair of memory barriers. A more palatable substitute would be to protect both regions of code with a mutex.
Alan Stern
Thanks, I will test with mutex between INIT_WORK and cancel_work and share v2.
Regards,
Sahil
Jiri Kosina
maintainerQuoted 6 lines · Alan Stern
With no synchronization between the two routines, this patch cannot possibly be correct. There's nothing to prevent the CPU running
asus_input_configured()from executing the assignment todrvdata->fn_lockbefore doing theINIT_WORK()(unless theINIT_WORK()call itself contains some synchronization -- but obviously the code shouldn't depend on that).
Ouch, you are of course right, that escaped my attention. Thanks a lot for catching this, Alan!
Now dropped from the queue.
Signature · 2 lines
Jiri Kosina SUSE Labs