Skip to contentSkip to conversation

bus: mhi: ep: Fix state_lock protection issues

PATCH Superseded Sumit Kumar 2 participants 5 messages Feb 3 – Mar 2

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover bus: mhi: ep: Fix state_lock protection issues

This series fixes a deadlock in mhi_ep_reset_worker() where state_lock is acquired twice, and adds missing lock protection for mhi_state accesses in mhi_ep_handle_syserr() and mhi_ep_power_up() to prevent race conditions and state machine corruption.

---
Sumit Kumar (2):
bus: mhi: ep: Fix potential deadlock in mhi_ep_reset_worker()
bus: mhi: ep: Add missing state_lock protection for mhi_state accesses

 drivers/bus/mhi/ep/main.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

---
base-commit: 6fa9041b7177f6771817b95e83f6df17b147c8c6
change-id: 20251113-reset_worker_deadlock-0223907f7c9d

Best regards,

Signature · 1 line
Sumit Kumar <[email protected]>
#1 lore raw

PATCH 1/2 bus: mhi: ep: Fix potential deadlock in mhi_ep_reset_worker()

There is a potential deadlock scenario in mhi_ep_reset_worker() where the state_lock mutex is acquired twice in the same call chain:

mhi_ep_reset_worker()
mutex_lock(&mhi_cntrl->state_lock)

    mhi_ep_power_up()
      mhi_ep_set_ready_state()
        mutex_lock(&mhi_cntrl->state_lock)  <- Deadlock

Fix this by releasing the state_lock before calling mhi_ep_power_up(). The lock is only needed to protect current MHI state read operation. The lock can be safely released before proceeding with the power up sequence.

Fixes 7a97b6b47353 ("bus: mhi: ep: Add support for handling MHI_RESET")
---
 drivers/bus/mhi/ep/main.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/bus/mhi/ep/main.c +1 -2
index cdea24e9291959ae0a92487c1b9698dc8164d2f1..73597de373ef7e0c428bcbc126d63a9a97f95144 100644
@@ -1093,6 +1093,7 @@ static void mhi_ep_reset_worker(struct work_struct *work)
 	mhi_ep_mmio_reset(mhi_cntrl);
 	cur_state = mhi_cntrl->mhi_state;
 
+	mutex_unlock(&mhi_cntrl->state_lock);
 	/*
 	 * Only proceed further if the reset is due to SYS_ERR. The host will
 	 * issue reset during shutdown also and we don't need to do re-init in
@@ -1100,8 +1101,6 @@ static void mhi_ep_reset_worker(struct work_struct *work)
 	 */
 	if (cur_state == MHI_STATE_SYS_ERR)
 		mhi_ep_power_up(mhi_cntrl);
-
-	mutex_unlock(&mhi_cntrl->state_lock);
 }
 
 /*
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] bus: mhi: ep: Fix potential deadlock in mhi_ep_reset_worker()

Quoted 5 lines · Sumit Kumar

There is a potential deadlock scenario in mhi_ep_reset_worker() where the state_lock mutex is acquired twice in the same call chain:

mhi_ep_reset_worker()
mutex_lock(&mhi_cntrl->state_lock)

Quoted 8 lines
    mhi_ep_power_up()
      mhi_ep_set_ready_state()
        mutex_lock(&mhi_cntrl->state_lock)  <- Deadlock

Fix this by releasing the state_lock before calling mhi_ep_power_up(). The lock is only needed to protect current MHI state read operation. The lock can be safely released before proceeding with the power up sequence.

While inspecting the code, I also found one instance where mhi_ep_handle_syserr() was not locked. I've sent a patch to fix it. Please spare some time to test it and give a tested-by tag.

drivers/bus/mhi/ep/main.c static void mhi_ep_reset_worker(struct work_struct *work) · line 1093

Quoted 12 lines
Fixes 7a97b6b47353 ("bus: mhi: ep: Add support for handling MHI_RESET")
---
 drivers/bus/mhi/ep/main.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/bus/mhi/ep/main.c +0 -0
index cdea24e9291959ae0a92487c1b9698dc8164d2f1..73597de373ef7e0c428bcbc126d63a9a97f95144 100644
@@ -1093,6 +1093,7 @@ static void mhi_ep_reset_worker(struct work_struct *work)
 	mhi_ep_mmio_reset(mhi_cntrl);

You can also perform locking after mhi_ep_mmio_reset(). There is no need to protect mhi_ep_mmio_reset().

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
#3 lore raw

PATCH 2/2 bus: mhi: ep: Add missing state_lock protection for mhi_state accesses

In reply to Sumit Kumar

The mhi_cntrl->mhi_state field should be protected by state_lock to ensure atomic state transitions. However, mhi_ep_handle_syserr() and mhi_ep_power_up() access mhi_state without holding this lock, which can race with concurrent state transitions and lead to state corruption.

Add proper state_lock protection in both functions around their mhi_state accesses.

Fixes fb3a26b7e8af ("bus: mhi: ep: Add support for powering up the MHI endpoint stack")
Fixes f7d0806bdb1b3 ("bus: mhi: ep: Add support for handling SYS_ERR condition")
---
 drivers/bus/mhi/ep/main.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/bus/mhi/ep/main.c +4 -0
index 73597de373ef7e0c428bcbc126d63a9a97f95144..e9d14006453aa8b8999486a1cef17ca43f4cc4e1 100644
@@ -1113,7 +1113,9 @@ void mhi_ep_handle_syserr(struct mhi_ep_cntrl *mhi_cntrl)
 	struct device *dev = &mhi_cntrl->mhi_dev->dev;
 	int ret;
 
+	mutex_lock(&mhi_cntrl->state_lock);
 	ret = mhi_ep_set_mhi_state(mhi_cntrl, MHI_STATE_SYS_ERR);
+	mutex_unlock(&mhi_cntrl->state_lock);
 	if (ret)
 		return;
 
@@ -1148,7 +1150,9 @@ int mhi_ep_power_up(struct mhi_ep_cntrl *mhi_cntrl)
 	for (i = 0; i < mhi_cntrl->event_rings; i++)
 		mhi_ep_ring_init(&mhi_cntrl->mhi_event[i].ring, RING_TYPE_ER, i);
 
+	mutex_lock(&mhi_cntrl->state_lock);
 	mhi_cntrl->mhi_state = MHI_STATE_RESET;
+	mutex_unlock(&mhi_cntrl->state_lock);
 
 	/* Set AMSS EE before signaling ready state */
 	mhi_ep_mmio_set_env(mhi_cntrl, MHI_EE_AMSS);
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 2/2] bus: mhi: ep: Add missing state_lock protection for mhi_state accesses

Quoted 20 lines · Sumit Kumar

The mhi_cntrl->mhi_state field should be protected by state_lock to ensure atomic state transitions. However, mhi_ep_handle_syserr() and mhi_ep_power_up() access mhi_state without holding this lock, which can race with concurrent state transitions and lead to state corruption.

Add proper state_lock protection in both functions around their mhi_state accesses.

Fixes fb3a26b7e8af ("bus: mhi: ep: Add support for powering up the MHI endpoint stack")
Fixes f7d0806bdb1b3 ("bus: mhi: ep: Add support for handling SYS_ERR condition")
---
 drivers/bus/mhi/ep/main.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/bus/mhi/ep/main.c +0 -0
index 73597de373ef7e0c428bcbc126d63a9a97f95144..e9d14006453aa8b8999486a1cef17ca43f4cc4e1 100644

drivers/bus/mhi/ep/main.c void mhi_ep_handle_syserr(struct mhi_ep_cntrl *mhi_cntrl) · line 1118

Quoted 6 lines
drivers/bus/mhi/ep/main.c +2 -0
@@ -1113,7 +1113,9 @@ void mhi_ep_handle_syserr(struct mhi_ep_cntrl *mhi_cntrl)
 	struct device *dev = &mhi_cntrl->mhi_dev->dev;
 	int ret;
 
+	mutex_lock(&mhi_cntrl->state_lock);
 	ret = mhi_ep_set_mhi_state(mhi_cntrl, MHI_STATE_SYS_ERR);
+	mutex_unlock(&mhi_cntrl->state_lock);

Ah, I sent my patch *before* seeing this one. But still this part is wrong and will cause deadlock as callers were already holding 'state_lock'.

So drop this part in favor of:
https://lore.kernel.org/mhi/[email protected]

drivers/bus/mhi/ep/main.c int mhi_ep_power_up(struct mhi_ep_cntrl *mhi_cntrl) · line 1156

Quoted 11 lines
drivers/bus/mhi/ep/main.c +2 -0
 	if (ret)
 		return;
 
@@ -1148,7 +1150,9 @@ int mhi_ep_power_up(struct mhi_ep_cntrl *mhi_cntrl)
 	for (i = 0; i < mhi_cntrl->event_rings; i++)
 		mhi_ep_ring_init(&mhi_cntrl->mhi_event[i].ring, RING_TYPE_ER, i);
 
+	mutex_lock(&mhi_cntrl->state_lock);
 	mhi_cntrl->mhi_state = MHI_STATE_RESET;
+	mutex_unlock(&mhi_cntrl->state_lock);
 

This looks fine.

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்