All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scsi: ufs: core: disable exception work before removing hwmon
@ 2026-11-26 11:14 Yaohan Li
  2026-11-26 11:22 ` sashiko-bot
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Yaohan Li @ 2026-11-26 11:14 UTC (permalink / raw)
  To: Martin K . Petersen, James E . J . Bottomley
  Cc: Alim Akhtar, Bart Van Assche, Bean Huo, Stanley Jhu, linux-scsi,
	linux-kernel

ufshcd_remove() unregisters the hwmon device while hba->eeh_work can
still be running. A SCSI completion can also queue hba->eeh_work later
during scsi_remove_host(). When the worker runs ufs_hwmon_notify_event(),
it can access the freed hwmon device and trigger a use-after-free.

Disable and drain hba->eeh_work in ufshcd_remove() before removing
hwmon. This ensures any running handler finishes while hwmon is still
present and prevents later SCSI completions during removal from
requeuing the work.

Fixes: 66ec6d59407b ("[SCSI] ufs: Add support for host assisted background operations")
Cc: stable@vger.kernel.org # v6.10+
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
Tested in QEMU/KVM (KASAN + PROVE_LOCKING) with an out-of-tree virtual
UFS 3.1 platform controller driving the in-tree ufshcd-core.ko (no
kprobes):
- Unbinding the device during an exception-event alert flood hits a
  KASAN slab-use-after-free in hwmon_notify_event() /
  kobject_uevent_env() from ufshcd_exception_event_handler() (freed by
  ufs_hwmon_remove() <- ufshcd_remove()) on the unpatched kernel, and
  passes 10-cycle A/B and 30-cycle stress runs with no KASAN, warning,
  or lockdep reports after the patch.
- Exception alerts on SYNCHRONIZE_CACHE during scsi_remove_host()
  requeue eeh_work on 10/10 cycles before the patch and 0/10 cycles
  after.
- Compile-tested with W=1 on x86_64 and arm64; verified clean apply on
  mkp/scsi.git for-next.
- Not tested on physical UFS hardware.

AI assistance: AI tools assisted with static code triage, writing the
virtual UFS QEMU test module, and drafting the changelog; I reviewed and
verified the root cause, teardown order, workqueue/lock behavior, and
the final patch.

 drivers/ufs/core/ufshcd.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 3c28bc67c65f..86100ac90fce 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -10988,6 +10988,7 @@ void ufshcd_remove(struct ufs_hba *hba)
 {
 	if (hba->ufs_device_wlun)
 		ufshcd_rpm_get_sync(hba);
+	disable_work_sync(&hba->eeh_work);
 	ufs_hwmon_remove(hba);
 	ufs_bsg_remove(hba);
 	ufs_rpmb_remove(hba);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] scsi: ufs: core: disable exception work before removing hwmon
  2026-11-26 11:14 [PATCH] scsi: ufs: core: disable exception work before removing hwmon Yaohan Li
@ 2026-11-26 11:22 ` sashiko-bot
  2026-11-26 18:09 ` Bean Huo
  2026-11-27  7:25 ` [PATCH v2] " Yaohan Li
  2 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-26 11:22 UTC (permalink / raw)
  To: Yaohan Li; +Cc: linux-scsi

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] scsi: ufs: core: use-after-free in
  ufshcd_rpm_dev_flush_recheck_work() [1]
--

commit a1077f0758756bac493f25126be2d29218ae49d7
Author: Yaohan Li <shunsheng1218@gmail.com>

scsi: ufs: core: disable exception work before removing hwmon

This commit calls disable_work_sync(&hba->eeh_work) in ufshcd_remove()
before unregistering hwmon so that running or newly queued exception work
cannot access the freed hwmon device.

> diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
> index 00a7637a1487d..f2a132c27fa0e 100644
> --- a/drivers/ufs/core/ufshcd.c
> +++ b/drivers/ufs/core/ufshcd.c
> @@ -11024,6 +11024,7 @@ void ufshcd_remove(struct ufs_hba *hba)
>  {
>  	if (hba->ufs_device_wlun)
>  		ufshcd_rpm_get_sync(hba);
> +	disable_work_sync(&hba->eeh_work);

[Severity: Critical]
This problem wasn't introduced by this patch, but can
ufshcd_rpm_dev_flush_recheck_work() trigger a use-after-free on hba during
ufshcd_remove() [1]?

ufshcd_rpm_get_sync() resumes the device via __ufshcd_wl_resume(), which
only calls asynchronous cancel_delayed_work() on
hba->rpm_dev_flush_recheck_work:

drivers/ufs/core/ufshcd.c:__ufshcd_wl_resume() {
	...
	if (hba->dev_info.b_rpm_dev_flush_capable) {
		hba->dev_info.b_rpm_dev_flush_capable = false;
		cancel_delayed_work(&hba->rpm_dev_flush_recheck_work);
	}
	...
}

Because ufshcd_remove() does not call cancel_delayed_work_sync() on
hba->rpm_dev_flush_recheck_work, an already running worker can outlive
host teardown:

CPU0                                CPU1
ufshcd_remove()
                                    ufshcd_rpm_dev_flush_recheck_work()
  ufshcd_rpm_get_sync(hba)
    __ufshcd_wl_resume()
      cancel_delayed_work(...)
  ...
  // hba is torn down and freed
                                      ufshcd_rpm_put_sync(hba)  <-- use-after-free

[1] https://sashiko.dev/#/bug/linux-6d2939f8-3e6b-4a28-a988-2094ecabbec6

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126111400.2828356-1-shunsheng1218@gmail.com?part=1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] scsi: ufs: core: disable exception work before removing hwmon
  2026-11-26 11:14 [PATCH] scsi: ufs: core: disable exception work before removing hwmon Yaohan Li
  2026-11-26 11:22 ` sashiko-bot
@ 2026-11-26 18:09 ` Bean Huo
  2026-11-27  7:25 ` [PATCH v2] " Yaohan Li
  2 siblings, 0 replies; 7+ messages in thread
From: Bean Huo @ 2026-11-26 18:09 UTC (permalink / raw)
  To: Yaohan Li, Martin K . Petersen, James E . J . Bottomley
  Cc: Alim Akhtar, Bart Van Assche, Bean Huo, Stanley Jhu, linux-scsi,
	linux-kernel

On Thu, 2026-11-26 at 19:14 +0800, Yaohan Li wrote:
> ufshcd_remove() unregisters the hwmon device while hba->eeh_work can
> still be running. A SCSI completion can also queue hba->eeh_work later
> during scsi_remove_host(). When the worker runs ufs_hwmon_notify_event(),
> it can access the freed hwmon device and trigger a use-after-free.
> 
> Disable and drain hba->eeh_work in ufshcd_remove() before removing
> hwmon. This ensures any running handler finishes while hwmon is still
> present and prevents later SCSI completions during removal from
> requeuing the work.
> 
> Fixes: 66ec6d59407b ("[SCSI] ufs: Add support for host assisted background
> operations")

please double check this fix tag, should be  e88e2d32200a, where hwmon came in?

Kind regards,
Bean

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2] scsi: ufs: core: disable exception work before removing hwmon
  2026-11-26 11:14 [PATCH] scsi: ufs: core: disable exception work before removing hwmon Yaohan Li
  2026-11-26 11:22 ` sashiko-bot
  2026-11-26 18:09 ` Bean Huo
@ 2026-11-27  7:25 ` Yaohan Li
  2026-11-29 15:27   ` Bean Huo
  2026-11-29 16:37   ` Bart Van Assche
  2 siblings, 2 replies; 7+ messages in thread
From: Yaohan Li @ 2026-11-27  7:25 UTC (permalink / raw)
  To: Martin K . Petersen, James E . J . Bottomley
  Cc: Alim Akhtar, Bart Van Assche, Bean Huo, Bean Huo, Stanley Jhu,
	linux-scsi, linux-kernel

ufshcd_remove() unregisters the hwmon device while hba->eeh_work can
still be running. A SCSI completion can also queue hba->eeh_work later
during scsi_remove_host(). When the worker runs ufs_hwmon_notify_event(),
it can access the freed hwmon device and trigger a use-after-free.

Disable and drain hba->eeh_work in ufshcd_remove() before removing
hwmon. This ensures any running handler finishes while hwmon is still
present and prevents later SCSI completions during removal from
requeuing the work.

Fixes: e88e2d32200a ("scsi: ufs: core: Probe for temperature notification support")
Cc: stable@vger.kernel.org # v6.10+
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
Changes in v2:
- Correct the Fixes tag to e88e2d32200a, as pointed out by Bean Huo.
- No code changes.

Tested in QEMU/KVM (KASAN + PROVE_LOCKING) with an out-of-tree virtual
UFS 3.1 platform controller driving the in-tree ufshcd-core.ko (no
kprobes):
- Unbinding the device during an exception-event alert flood hits a
  KASAN slab-use-after-free in hwmon_notify_event() /
  kobject_uevent_env() from ufshcd_exception_event_handler() (freed by
  ufs_hwmon_remove() <- ufshcd_remove()) on the unpatched kernel, and
  passes 10-cycle A/B and 30-cycle stress runs with no KASAN, warning,
  or lockdep reports after the patch.
- Exception alerts on SYNCHRONIZE_CACHE during scsi_remove_host()
  requeue eeh_work on 10/10 cycles before the patch and 0/10 cycles
  after.
- Compile-tested with W=1 on x86_64 and arm64; verified clean apply on
  mkp/scsi.git for-next.
- Not tested on physical UFS hardware.

AI assistance: AI tools assisted with static code triage, writing the
virtual UFS QEMU test module, and drafting the changelog; I reviewed and
verified the root cause, teardown order, workqueue/lock behavior, and
the final patch.

 drivers/ufs/core/ufshcd.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 3c28bc67c65f..86100ac90fce 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -10988,6 +10988,7 @@ void ufshcd_remove(struct ufs_hba *hba)
 {
 	if (hba->ufs_device_wlun)
 		ufshcd_rpm_get_sync(hba);
+	disable_work_sync(&hba->eeh_work);
 	ufs_hwmon_remove(hba);
 	ufs_bsg_remove(hba);
 	ufs_rpmb_remove(hba);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] scsi: ufs: core: disable exception work before removing hwmon
  2026-11-27  7:25 ` [PATCH v2] " Yaohan Li
@ 2026-11-29 15:27   ` Bean Huo
  2026-11-29 16:37   ` Bart Van Assche
  1 sibling, 0 replies; 7+ messages in thread
From: Bean Huo @ 2026-11-29 15:27 UTC (permalink / raw)
  To: Yaohan Li, Martin K . Petersen, James E . J . Bottomley
  Cc: Alim Akhtar, Bart Van Assche, Bean Huo, Stanley Jhu, linux-scsi,
	linux-kernel


Reviewed-by: Bean Huo <beanhuo@micron.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] scsi: ufs: core: disable exception work before removing hwmon
  2026-11-27  7:25 ` [PATCH v2] " Yaohan Li
  2026-11-29 15:27   ` Bean Huo
@ 2026-11-29 16:37   ` Bart Van Assche
  2026-11-30 13:27     ` Yaohan Li
  1 sibling, 1 reply; 7+ messages in thread
From: Bart Van Assche @ 2026-11-29 16:37 UTC (permalink / raw)
  To: Yaohan Li, Martin K . Petersen, James E . J . Bottomley
  Cc: Alim Akhtar, Bean Huo, Bean Huo, Stanley Jhu, linux-scsi,
	linux-kernel

On 11/26/26 11:25 PM, Yaohan Li wrote:
 > Fixes: e88e2d32200a ("scsi: ufs: core: Probe for temperature 
notification support")
 > Cc: stable@vger.kernel.org # v6.10+

The Fixes tag looks inaccurate to me. Commit e88e2d32200a added
ufs_hwmon_probe() and ufs_hwmon_remove(), but did not touch
ufshcd_exception_event_handler(). Calling ufs_hwmon_notify_event() from
eeh_work was introduced in the next commit:

Fixes: 322c4b29ee1f ("scsi: ufs: core: Add temperature notification 
exception handling")

More generally, the missing teardown of hba->eeh_work in ufshcd_remove()
goes back to commit 66ec6d59407b ("[SCSI] ufs: Add support for host
assisted background operations"), as eeh_work can also outlive
ufshcd_remove() even without hwmon.

The code change itself looks good to me. Hence:

Reviewed-by: Bart Van Assche <bvanassche@acm.org>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] scsi: ufs: core: disable exception work before removing hwmon
  2026-11-29 16:37   ` Bart Van Assche
@ 2026-11-30 13:27     ` Yaohan Li
  0 siblings, 0 replies; 7+ messages in thread
From: Yaohan Li @ 2026-11-30 13:27 UTC (permalink / raw)
  To: Bart Van Assche, Martin K. Petersen, James E.J. Bottomley
  Cc: Bean Huo, Bean Huo, Alim Akhtar, Stanley Jhu, linux-scsi,
	linux-kernel

Hi Bart,

Thanks to both you and Bean for the reviews.

You're right -- e88e2d32200a only added ufs_hwmon_probe()/remove(), and
322c4b29ee1f is what actually wired ufs_hwmon_notify_event() into
eeh_work (with the broader missing eeh_work teardown going back to
66ec6d59407b).  Since the changelog focuses on the hwmon UAF,
322c4b29ee1f is the right Fixes tag.

Martin, would you mind updating the Fixes tag to:

  Fixes: 322c4b29ee1f ("scsi: ufs: core: Add temperature notification exception handling")

when applying, or would you prefer a v3 with that and the Reviewed-by
tags?

Thanks,
Yaohan

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-30 13:28 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 11:14 [PATCH] scsi: ufs: core: disable exception work before removing hwmon Yaohan Li
2026-11-26 11:22 ` sashiko-bot
2026-11-26 18:09 ` Bean Huo
2026-11-27  7:25 ` [PATCH v2] " Yaohan Li
2026-11-29 15:27   ` Bean Huo
2026-11-29 16:37   ` Bart Van Assche
2026-11-30 13:27     ` Yaohan Li

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.