Skip to contentSkip to conversation

regulator: cancel disable_work synchronously in regulator_unregister()

PATCH In review Chengyu Liu 2 participants 8 messages Nov 3 – Nov 8

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 regulator: cancel disable_work synchronously in regulator_unregister()

regulator_unregister() stops rdev->disable_work with flush_work(&dw->work), which is a no-op for a delayed_work pending on the timer wheel: flush_work() only waits for a queued or running work item. A disable deferred by a consumer's regulator_disable_deferred() therefore survives the provider's unbind, and regulator_disable_work() fires after device_unregister() has freed the rdev, dereferencing it via container_of(). Use-after-free.

The in-tree users are ASoC DAPM regulator widgets with a 20 ms power-down delay (sound/soc/soc-dapm.c): the window is the entire deferral period, not a race between adjacent instructions.

Use cancel_delayed_work_sync(), which also dequeues a timer-pending instance, waits for a running callback and blocks re-arming.

Found by static analysis.

---
 drivers/regulator/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/regulator/core.c +1 -1
index 6a4008f387..032b7298a5 100644
@@ -6300,7 +6300,7 @@ void regulator_unregister(struct regulator_dev *rdev)
 		regulator_put(rdev->supply);
 	}
 
-	flush_work(&rdev->disable_work.work);
+	cancel_delayed_work_sync(&rdev->disable_work);
 
 	mutex_lock(&regulator_list_mutex);
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 2 lines

Use cancel_delayed_work_sync(), which also dequeues a timer-pending instance, waits for a running callback and blocks re-arming.

I think you want disable_delayed_work_sync() here for the rearming part.

Signature · 1 line
Mark Brown <[email protected]>
#2 lore raw
Quoted 1 line · Mark Brown

I think you want disable_delayed_work_sync() here for the rearming part.

Agreed, that's a mistake in my patch. Thanks for pointing it out. I'll send a v2 switching to disable_delayed_work_sync(), which keeps the work disabled so a racing regulator_disable_deferred() can't re-arm it during the teardown.

#3 lore raw

PATCH v2 1/1 regulator: disable disable_work for good in regulator_unregister()

In reply to Chengyu Liu

regulator_unregister() stops rdev->disable_work with flush_work(&dw->work), which is a no-op for a delayed_work pending on the timer wheel: flush_work() only waits for a queued or running work item. A disable deferred by a consumer's regulator_disable_deferred() therefore survives the provider's unbind, and regulator_disable_work() fires after device_unregister() has freed the rdev, dereferencing it via container_of(). Use-after-free.

The in-tree users are ASoC DAPM regulator widgets with a 20 ms power-down delay (sound/soc/soc-dapm.c): the window is the entire deferral period, not a race between adjacent instructions.

Use disable_delayed_work_sync(): it dequeues a timer-pending instance and waits for a running callback, and keeps the work disabled so a concurrent regulator_disable_deferred() cannot re-arm it while the teardown proceeds.

Found by static analysis.

---
v2: switch to disable_delayed_work_sync() (Mark Brown): also keeps the
work disabled so nothing can re-arm it past the teardown.

 drivers/regulator/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/regulator/core.c +1 -1
index 6a4008f387b5..4ba0457c4747 100644
@@ -6300,7 +6300,7 @@ void regulator_unregister(struct regulator_dev *rdev)
 		regulator_put(rdev->supply);
 	}
 
-	flush_work(&rdev->disable_work.work);
+	disable_delayed_work_sync(&rdev->disable_work);
 
 	mutex_lock(&regulator_list_mutex);
 
Signature · 1 line
2.25.1
maintainer
#4 lore raw

Re: [PATCH v2] regulator: disable disable_work for good in regulator_unregister()

Quoted 7 lines · Chengyu Liu

regulator_unregister() stops rdev->disable_work with flush_work(&dw->work), which is a no-op for a delayed_work pending on the timer wheel: flush_work() only waits for a queued or running work item. A disable deferred by a consumer's regulator_disable_deferred() therefore survives the provider's unbind, and regulator_disable_work() fires after device_unregister() has freed the rdev, dereferencing it via container_of(). Use-after-free.

Please don't send new patches in reply to old patches or serieses, this makes it harder for both people and tools to understand what is going on - it can bury things in mailboxes and make it difficult to keep track of what current patches are, both for the new patches and the old ones.

#5 lore raw

Re: [PATCH v2] regulator: disable disable_work for good in regulator_unregister()

Quoted 4 lines · Mark Brown

Please don't send new patches in reply to old patches or serieses, this makes it harder for both people and tools to understand what is going on - it can bury things in mailboxes and make it difficult to keep track of what current patches are, both for the new patches and the old ones.

Understood, sorry about that - I'll repost v2 as a fresh thread with a link to v1.

maintainer
#6 lore raw

Re: [PATCH v2] regulator: disable disable_work for good in regulator_unregister()

Quoted 8 lines · Chengyu Liu
Quoted 4 lines · Mark Brown

Please don't send new patches in reply to old patches or serieses, this makes it harder for both people and tools to understand what is going on - it can bury things in mailboxes and make it difficult to keep track of what current patches are, both for the new patches and the old ones.

Understood, sorry about that - I'll repost v2 as a fresh thread with a link to v1.

No, please don't do that - that will create even more confusion as there will be multiple v2s floating about.

maintainer
#7 lore raw

Re: [PATCH v2] regulator: disable disable_work for good in regulator_unregister()

In reply to Chengyu Liu
Quoted 1 line · Chengyu Liu

regulator: disable disable_work for good in regulator_unregister()

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-7.5

Thanks!

[1/1] regulator: disable disable_work for good in regulator_unregister()

      https://git.kernel.org/broonie/regulator/c/1d8212cdfbbe

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark