mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bin Du <Bin.Du@amd.com>
To: Yifei Gao <gyf161023@gmail.com>,
	Nirujogi Pratap <pratap.nirujogi@amd.com>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Sakari Ailus <sakari.ailus@linux.intel.com>
Cc: Sultan Alsawaf <sultan@kerneltoast.com>,
	Svetoslav Stoilov <Svetoslav.Stoilov@amd.com>,
	linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, "Chan,
	Benjamin (Koon Pan)" <Benjamin.Chan@amd.com>,
	"Li, King" <King.Li@amd.com>
Subject: Re: [PATCH] media: amd: isp4: fix self-deadlock in power-on error path
Date: Mon, 27 Jul 2026 11:32:16 +0800	[thread overview]
Message-ID: <81936907-d5d2-4058-a056-bfdb5a9d5d87@amd.com> (raw)
In-Reply-To: <20260725203640.915626-1-gyf161023@gmail.com>

Many thanks, Yifei, for catching the deadlock in the failure path.

On 7/26/2026 4:36 AM, Yifei Gao wrote:
> [You don't often get email from gyf161023@gmail.com. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
> 
> isp4sd_pwron_and_init() holds ops_mutex via guard(mutex) for the entire
> function. On any initialization failure it jumps to the err_deinit label
> and calls isp4sd_pwroff_and_deinit(), which acquires the same ops_mutex
> through its own guard(mutex). Since the guard in isp4sd_pwron_and_init()
> still holds the lock at err_deinit, this re-acquires a non-recursive
> mutex already held by the current thread and deadlocks.
> 
> Every failure path in isp4sd_pwron_and_init() reaches this: a failed
> pm_runtime_resume_and_get(), a failed dev_pm_genpd_set_performance_state(),
> a firmware start failure in isp4if_start(), or a response-thread creation
> failure.
> 
> Move the cleanup logic into a new lockless __isp4sd_pwroff_and_deinit()
> and have isp4sd_pwroff_and_deinit() call it under the lock. The
> err_deinit path, which already holds ops_mutex, now calls the lockless
> helper directly.
> 
> The "stream still running" check remains in the locked wrapper: it guards
> external close requests, and the power-on rollback path never reaches the
> STARTED state. The cleanup steps are safe on a partially initialized
> device: response-thread stop is guarded per-thread, and gpu memory pools
> are released through isp4if_gpu_mem_free(), which is a no-op on
> unallocated pools.
> 
> Fixes: 4e5e7a7ddb4a ("media: platform: amd: isp4 subdev and firmware loading handling added")
> Assisted-by: Claude:claude-opus-4-8 smatch
> Signed-off-by: Yifei Gao <gyf161023@gmail.com>
> ---
>  drivers/media/platform/amd/isp4/isp4_subdev.c | 31 ++++++++++---------
>  1 file changed, 16 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/media/platform/amd/isp4/isp4_subdev.c b/drivers/media/platform/amd/isp4/isp4_subdev.c
> index 48deea79ce6c..eabb6b36705f 100644
> --- a/drivers/media/platform/amd/isp4/isp4_subdev.c
> +++ b/drivers/media/platform/amd/isp4/isp4_subdev.c
> @@ -607,7 +607,8 @@ static int isp4sd_start_resp_proc_threads(struct isp4_subdev *isp_subdev)
>         return 0;
>  }
> 
> -int isp4sd_pwroff_and_deinit(struct v4l2_subdev *sd)
> +/* Caller must hold isp_subdev->ops_mutex. */
> +static void __isp4sd_pwroff_and_deinit(struct v4l2_subdev *sd)
>  {
>         struct isp4_subdev *isp_subdev = to_isp4_subdev(sd);
>         struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
> @@ -616,31 +617,20 @@ int isp4sd_pwroff_and_deinit(struct v4l2_subdev *sd)
>         struct device *dev = isp_subdev->dev;
>         int ret;
> 
> -       guard(mutex)(&isp_subdev->ops_mutex);
> -       if (sensor_info->status == ISP4SD_START_STATUS_STARTED) {
> -               dev_err(dev, "fail for stream still running\n");
> -               return -EINVAL;
> -       }
> -
>         sensor_info->status = ISP4SD_START_STATUS_OFF;
> -
>         if (isp_subdev->irq_enabled) {
>                 for (unsigned int i = 0; i < ISP4SD_MAX_FW_RESP_STREAM_NUM; i++)
>                         disable_irq(isp_subdev->irq[i]);
>                 isp_subdev->irq_enabled = false;
>         }
> -
>         isp4sd_stop_resp_proc_threads(isp_subdev);
>         dev_dbg(dev, "isp_subdev stop resp proc threads suc\n");
> -
>         isp4if_stop(ispif);
> -
>         ret = dev_pm_genpd_set_performance_state(dev, perf_state);
>         if (ret)
>                 dev_err(dev,
>                         "fail to set isp_subdev performance state %u,ret %d\n",
>                         perf_state, ret);
> -
>         /* hold ccpu reset */
>         isp4hw_wreg(isp_subdev->mmio, ISP_SOFT_RESET, 0);
>         isp4hw_wreg(isp_subdev->mmio, ISP_POWER_STATUS, 0);
> @@ -649,11 +639,9 @@ int isp4sd_pwroff_and_deinit(struct v4l2_subdev *sd)
>                 dev_err(dev, "power off isp_subdev fail %d\n", ret);
>         else
>                 dev_dbg(dev, "power off isp_subdev suc\n");
> -
>         ispif->status = ISP4IF_STATUS_PWR_OFF;
>         isp4if_clear_cmdq(ispif);
>         isp4sd_module_enable(isp_subdev, false);
> -
>         /*
>          * When opening the camera, isp4sd_module_enable(isp_subdev, true) is
>          * called. Hardware requires at least a 20ms delay between disabling
> @@ -661,7 +649,20 @@ int isp4sd_pwroff_and_deinit(struct v4l2_subdev *sd)
>          * during quick reopen scenarios.
>          */
>         msleep(20);
> +}
> 
> +int isp4sd_pwroff_and_deinit(struct v4l2_subdev *sd)
> +{
> +       struct isp4_subdev *isp_subdev = to_isp4_subdev(sd);
> +       struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
> +       struct device *dev = isp_subdev->dev;
> +
> +       guard(mutex)(&isp_subdev->ops_mutex);
> +       if (sensor_info->status == ISP4SD_START_STATUS_STARTED) {
> +               dev_err(dev, "fail for stream still running\n");
> +               return -EINVAL;
> +       }
> +       __isp4sd_pwroff_and_deinit(sd);
>         return 0;
>  }
> 
> @@ -725,7 +726,7 @@ int isp4sd_pwron_and_init(struct v4l2_subdev *sd)
> 
>         return 0;
>  err_deinit:
> -       isp4sd_pwroff_and_deinit(sd);
> +       __isp4sd_pwroff_and_deinit(sd);

However, if pm_runtime_resume_and_get() fails, the patch calls
__isp4sd_pwroff_and_deinit(), which unconditionally accesses ISP MMIO
while the hardware may be powered off and calls pm_runtime_put_sync()
without a corresponding acquired runtime-PM reference. Therefore, the
error paths need staged cleanup.

>         return -EINVAL;
>  }
> 
> --
> 2.43.0
> 


  reply	other threads:[~2026-07-27  3:32 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-25 20:36 Yifei Gao
2026-07-27  3:32 ` Bin Du [this message]
2026-07-27 18:34 ` [PATCH v2] media: amd: isp4: fix self-deadlock in isp4sd_pwron_and_init() " Yifei Gao
2026-07-28 10:17   ` Bin Du
2026-07-28 14:16   ` [PATCH v3 0/2] media: amd: isp4: fix error handling in isp4sd_pwron_and_init() Yifei Gao
2026-07-28 14:16     ` [PATCH v3 1/2] media: amd: isp4: release partial allocations in isp4if_alloc_fw_gpumem() Yifei Gao
2026-07-28 14:16     ` [PATCH v3 2/2] media: amd: isp4: fix self-deadlock in isp4sd_pwron_and_init() error path Yifei Gao
2026-07-28 17:35       ` Sakari Ailus
2026-07-28 19:07     ` [PATCH v4 0/2] media: amd: isp4: fix error handling in isp4sd_pwron_and_init() Yifei Gao
2026-07-28 19:07       ` [PATCH v4 1/2] media: amd: isp4: release partial allocations in isp4if_alloc_fw_gpumem() Yifei Gao
2026-07-30  9:57         ` Bin Du
2026-07-28 19:07       ` [PATCH v4 2/2] media: amd: isp4: fix self-deadlock in isp4sd_pwron_and_init() error path Yifei Gao
2026-07-30 14:14       ` [PATCH v5 0/2] media: amd: isp4: fix error handling in isp4sd_pwron_and_init() Yifei Gao
2026-07-30 14:14         ` [PATCH v5 1/2] media: amd: isp4: release partial allocations in isp4if_alloc_fw_gpumem() Yifei Gao
2026-08-03  7:22           ` Bin Du
2026-07-30 14:14         ` [PATCH v5 2/2] media: amd: isp4: fix self-deadlock in isp4sd_pwron_and_init() error path Yifei Gao
2026-08-03  7:22           ` Bin Du

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=81936907-d5d2-4058-a056-bfdb5a9d5d87@amd.com \
    --to=bin.du@amd.com \
    --cc=Benjamin.Chan@amd.com \
    --cc=King.Li@amd.com \
    --cc=Svetoslav.Stoilov@amd.com \
    --cc=gyf161023@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=pratap.nirujogi@amd.com \
    --cc=sakari.ailus@linux.intel.com \
    --cc=sultan@kerneltoast.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®