Re: [PATCH 2/2] bus: mhi: ep: Add missing state_lock protection for mhi_state accesses
From: Manivannan Sadhasivam
Date: Mon Mar 02 2026 - 04:34:20 EST
On Tue, Feb 03, 2026 at 12:25:02PM +0530, Sumit Kumar wrote:
> 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")
> Signed-off-by: Sumit Kumar <sumit.kumar@xxxxxxxxxxxxxxxx>
> ---
> drivers/bus/mhi/ep/main.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/bus/mhi/ep/main.c b/drivers/bus/mhi/ep/main.c
> index 73597de373ef7e0c428bcbc126d63a9a97f95144..e9d14006453aa8b8999486a1cef17ca43f4cc4e1 100644
> --- a/drivers/bus/mhi/ep/main.c
> +++ b/drivers/bus/mhi/ep/main.c
> @@ -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/20260302085612.18725-1-manivannan.sadhasivam@xxxxxxxxxxxxxxxx
> 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
--
மணிவண்ணன் சதாசிவம்