diff mbox series

[v2,3/3] media: amphion: Remove lock in s_ctrl callback

Message ID 20240425065011.105915-3-ming.qian@nxp.com (mailing list archive)
State New, archived
Headers show
Series [v2,1/3] media: v4l2-ctrls: Add average qp control | expand

Commit Message

Ming Qian April 25, 2024, 6:50 a.m. UTC
There is no need to add lock in s_ctrl callback, it has been
synchronized by the ctrl_handler's lock, otherwise it may led to
deadlock if driver call v4l2_ctrl_s_ctrl().

Signed-off-by: Ming Qian <ming.qian@nxp.com>
---
 drivers/media/platform/amphion/vdec.c | 2 --
 drivers/media/platform/amphion/venc.c | 2 --
 2 files changed, 4 deletions(-)

Comments

Andrzej Pietrasiewicz April 29, 2024, 5:44 p.m. UTC | #1
Hi Ming Qian,

W dniu 25.04.2024 o 08:50, Ming Qian pisze:
> There is no need to add lock in s_ctrl callback, it has been
> synchronized by the ctrl_handler's lock, otherwise it may led to
> deadlock if driver call v4l2_ctrl_s_ctrl().
> 
> Signed-off-by: Ming Qian <ming.qian@nxp.com>
> ---
>   drivers/media/platform/amphion/vdec.c | 2 --
>   drivers/media/platform/amphion/venc.c | 2 --
>   2 files changed, 4 deletions(-)
> 
> diff --git a/drivers/media/platform/amphion/vdec.c b/drivers/media/platform/amphion/vdec.c
> index a57f9f4f3b87..6a38a0fa0e2d 100644
> --- a/drivers/media/platform/amphion/vdec.c
> +++ b/drivers/media/platform/amphion/vdec.c
> @@ -195,7 +195,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
>   	struct vdec_t *vdec = inst->priv;
>   	int ret = 0;
>   
> -	vpu_inst_lock(inst);

I assume that PATCH v2 2/3 might cause the said deadlock to happen?
If so, maybe it would make more sense to make the current patch preceed
  PATCH v2 2/3? Otherwise the kernel at PATCH v2 2/3 introduces a potential
deadlock.

Regards,

Andrzej

>   	switch (ctrl->id) {
>   	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
>   		vdec->params.display_delay_enable = ctrl->val;
> @@ -207,7 +206,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
>   		ret = -EINVAL;
>   		break;
>   	}
> -	vpu_inst_unlock(inst);
>   
>   	return ret;
>   }
> diff --git a/drivers/media/platform/amphion/venc.c b/drivers/media/platform/amphion/venc.c
> index cdfaba9d107b..351b4edc8742 100644
> --- a/drivers/media/platform/amphion/venc.c
> +++ b/drivers/media/platform/amphion/venc.c
> @@ -518,7 +518,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
>   	struct venc_t *venc = inst->priv;
>   	int ret = 0;
>   
> -	vpu_inst_lock(inst);
>   	switch (ctrl->id) {
>   	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
>   		venc->params.profile = ctrl->val;
> @@ -579,7 +578,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
>   		ret = -EINVAL;
>   		break;
>   	}
> -	vpu_inst_unlock(inst);
>   
>   	return ret;
>   }
Ming Qian(OSS) April 30, 2024, 2:32 a.m. UTC | #2
Hi Andrzej,

> Hi Ming Qian,
> 
> W dniu 25.04.2024 o 08:50, Ming Qian pisze:
>> There is no need to add lock in s_ctrl callback, it has been
>> synchronized by the ctrl_handler's lock, otherwise it may led to
>> deadlock if driver call v4l2_ctrl_s_ctrl().
>>
>> Signed-off-by: Ming Qian <ming.qian@nxp.com>
>> ---
>>   drivers/media/platform/amphion/vdec.c | 2 --
>>   drivers/media/platform/amphion/venc.c | 2 --
>>   2 files changed, 4 deletions(-)
>>
>> diff --git a/drivers/media/platform/amphion/vdec.c 
>> b/drivers/media/platform/amphion/vdec.c
>> index a57f9f4f3b87..6a38a0fa0e2d 100644
>> --- a/drivers/media/platform/amphion/vdec.c
>> +++ b/drivers/media/platform/amphion/vdec.c
>> @@ -195,7 +195,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
>>       struct vdec_t *vdec = inst->priv;
>>       int ret = 0;
>> -    vpu_inst_lock(inst);
> 
> I assume that PATCH v2 2/3 might cause the said deadlock to happen?
> If so, maybe it would make more sense to make the current patch preceed
>   PATCH v2 2/3? Otherwise the kernel at PATCH v2 2/3 introduces a potential
> deadlock.
> 
> Regards,
> 
> Andrzej
> 

I actually discovered this problem when I was preparing the v2 2/3 patch.

But in the v2 2/3 patch, it tried to add a read-only ctrl, then I just
unset the s_ctrl callback for the new added ctrl, the potential deadlock
is caused by call the s_ctrl back in a locked environment, so after unset
the s_ctrl callback, the 2/3 patch won't trigger the deadlock even if
this patch is missing.

In order to avoid encountering similar problems in the future, that
driver may set or get some ctrl, I added this patch.

Best regards,
Ming

>>       switch (ctrl->id) {
>>       case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
>>           vdec->params.display_delay_enable = ctrl->val;
>> @@ -207,7 +206,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
>>           ret = -EINVAL;
>>           break;
>>       }
>> -    vpu_inst_unlock(inst);
>>       return ret;
>>   }
>> diff --git a/drivers/media/platform/amphion/venc.c 
>> b/drivers/media/platform/amphion/venc.c
>> index cdfaba9d107b..351b4edc8742 100644
>> --- a/drivers/media/platform/amphion/venc.c
>> +++ b/drivers/media/platform/amphion/venc.c
>> @@ -518,7 +518,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
>>       struct venc_t *venc = inst->priv;
>>       int ret = 0;
>> -    vpu_inst_lock(inst);
>>       switch (ctrl->id) {
>>       case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
>>           venc->params.profile = ctrl->val;
>> @@ -579,7 +578,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
>>           ret = -EINVAL;
>>           break;
>>       }
>> -    vpu_inst_unlock(inst);
>>       return ret;
>>   }
>
Andrzej Pietrasiewicz April 30, 2024, 8 a.m. UTC | #3
Hi Ming,

W dniu 30.04.2024 o 04:32, ming qian pisze:
> Hi Andrzej,
> 
>> Hi Ming Qian,
>>
>> W dniu 25.04.2024 o 08:50, Ming Qian pisze:
>>> There is no need to add lock in s_ctrl callback, it has been
>>> synchronized by the ctrl_handler's lock, otherwise it may led to
>>> deadlock if driver call v4l2_ctrl_s_ctrl().
>>>
>>> Signed-off-by: Ming Qian <ming.qian@nxp.com>
>>> ---
>>>   drivers/media/platform/amphion/vdec.c | 2 --
>>>   drivers/media/platform/amphion/venc.c | 2 --
>>>   2 files changed, 4 deletions(-)
>>>
>>> diff --git a/drivers/media/platform/amphion/vdec.c 
>>> b/drivers/media/platform/amphion/vdec.c
>>> index a57f9f4f3b87..6a38a0fa0e2d 100644
>>> --- a/drivers/media/platform/amphion/vdec.c
>>> +++ b/drivers/media/platform/amphion/vdec.c
>>> @@ -195,7 +195,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
>>>       struct vdec_t *vdec = inst->priv;
>>>       int ret = 0;
>>> -    vpu_inst_lock(inst);
>>
>> I assume that PATCH v2 2/3 might cause the said deadlock to happen?
>> If so, maybe it would make more sense to make the current patch preceed
>>   PATCH v2 2/3? Otherwise the kernel at PATCH v2 2/3 introduces a potential
>> deadlock.
>>
>> Regards,
>>
>> Andrzej
>>
> 
> I actually discovered this problem when I was preparing the v2 2/3 patch.
> 
> But in the v2 2/3 patch, it tried to add a read-only ctrl, then I just
> unset the s_ctrl callback for the new added ctrl, the potential deadlock
> is caused by call the s_ctrl back in a locked environment, so after unset
> the s_ctrl callback, the 2/3 patch won't trigger the deadlock even if
> this patch is missing.
> 
> In order to avoid encountering similar problems in the future, that
> driver may set or get some ctrl, I added this patch.

Do I understand you correctly that patch 2/3 is written in such a way that
it does not introduce a deadlock, and you add patch 3/3 only to prevent future
problems? If so, it seems to me that patch 3/3 could/should be separate from
this series, as it does not quite match "Add average qp control".

Regards,

Andrzej

> 
> Best regards,
> Ming
> 
>>>       switch (ctrl->id) {
>>>       case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
>>>           vdec->params.display_delay_enable = ctrl->val;
>>> @@ -207,7 +206,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
>>>           ret = -EINVAL;
>>>           break;
>>>       }
>>> -    vpu_inst_unlock(inst);
>>>       return ret;
>>>   }
>>> diff --git a/drivers/media/platform/amphion/venc.c 
>>> b/drivers/media/platform/amphion/venc.c
>>> index cdfaba9d107b..351b4edc8742 100644
>>> --- a/drivers/media/platform/amphion/venc.c
>>> +++ b/drivers/media/platform/amphion/venc.c
>>> @@ -518,7 +518,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
>>>       struct venc_t *venc = inst->priv;
>>>       int ret = 0;
>>> -    vpu_inst_lock(inst);
>>>       switch (ctrl->id) {
>>>       case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
>>>           venc->params.profile = ctrl->val;
>>> @@ -579,7 +578,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
>>>           ret = -EINVAL;
>>>           break;
>>>       }
>>> -    vpu_inst_unlock(inst);
>>>       return ret;
>>>   }
>>
Ming Qian(OSS) April 30, 2024, 8:55 a.m. UTC | #4
Hi Andrzej,

> Hi Ming,
> 
> W dniu 30.04.2024 o 04:32, ming qian pisze:
>> Hi Andrzej,
>>
>>> Hi Ming Qian,
>>>
>>> W dniu 25.04.2024 o 08:50, Ming Qian pisze:
>>>> There is no need to add lock in s_ctrl callback, it has been
>>>> synchronized by the ctrl_handler's lock, otherwise it may led to
>>>> deadlock if driver call v4l2_ctrl_s_ctrl().
>>>>
>>>> Signed-off-by: Ming Qian <ming.qian@nxp.com>
>>>> ---
>>>>   drivers/media/platform/amphion/vdec.c | 2 --
>>>>   drivers/media/platform/amphion/venc.c | 2 --
>>>>   2 files changed, 4 deletions(-)
>>>>
>>>> diff --git a/drivers/media/platform/amphion/vdec.c 
>>>> b/drivers/media/platform/amphion/vdec.c
>>>> index a57f9f4f3b87..6a38a0fa0e2d 100644
>>>> --- a/drivers/media/platform/amphion/vdec.c
>>>> +++ b/drivers/media/platform/amphion/vdec.c
>>>> @@ -195,7 +195,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
>>>>       struct vdec_t *vdec = inst->priv;
>>>>       int ret = 0;
>>>> -    vpu_inst_lock(inst);
>>>
>>> I assume that PATCH v2 2/3 might cause the said deadlock to happen?
>>> If so, maybe it would make more sense to make the current patch preceed
>>>   PATCH v2 2/3? Otherwise the kernel at PATCH v2 2/3 introduces a 
>>> potential
>>> deadlock.
>>>
>>> Regards,
>>>
>>> Andrzej
>>>
>>
>> I actually discovered this problem when I was preparing the v2 2/3 patch.
>>
>> But in the v2 2/3 patch, it tried to add a read-only ctrl, then I just
>> unset the s_ctrl callback for the new added ctrl, the potential deadlock
>> is caused by call the s_ctrl back in a locked environment, so after unset
>> the s_ctrl callback, the 2/3 patch won't trigger the deadlock even if
>> this patch is missing.
>>
>> In order to avoid encountering similar problems in the future, that
>> driver may set or get some ctrl, I added this patch.
> 
> Do I understand you correctly that patch 2/3 is written in such a way that
> it does not introduce a deadlock, and you add patch 3/3 only to prevent 
> future
> problems? If so, it seems to me that patch 3/3 could/should be separate 
> from
> this series, as it does not quite match "Add average qp control".
> 
> Regards,
> 
> Andrzej
> 

You're right, when I prepare the 2/3 patch, I add the new ctrl just like
the previous ctrl, but I got a deadlock. then I tried to fix a deadlock
in 2/3 patch first, and make this patch again to avoid similar errors.

I put this patch to this series, as I think this better explains why I
made this change.

Regards,
Ming

>>
>> Best regards,
>> Ming
>>
>>>>       switch (ctrl->id) {
>>>>       case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
>>>>           vdec->params.display_delay_enable = ctrl->val;
>>>> @@ -207,7 +206,6 @@ static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
>>>>           ret = -EINVAL;
>>>>           break;
>>>>       }
>>>> -    vpu_inst_unlock(inst);
>>>>       return ret;
>>>>   }
>>>> diff --git a/drivers/media/platform/amphion/venc.c 
>>>> b/drivers/media/platform/amphion/venc.c
>>>> index cdfaba9d107b..351b4edc8742 100644
>>>> --- a/drivers/media/platform/amphion/venc.c
>>>> +++ b/drivers/media/platform/amphion/venc.c
>>>> @@ -518,7 +518,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
>>>>       struct venc_t *venc = inst->priv;
>>>>       int ret = 0;
>>>> -    vpu_inst_lock(inst);
>>>>       switch (ctrl->id) {
>>>>       case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
>>>>           venc->params.profile = ctrl->val;
>>>> @@ -579,7 +578,6 @@ static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
>>>>           ret = -EINVAL;
>>>>           break;
>>>>       }
>>>> -    vpu_inst_unlock(inst);
>>>>       return ret;
>>>>   }
>>>
>
diff mbox series

Patch

diff --git a/drivers/media/platform/amphion/vdec.c b/drivers/media/platform/amphion/vdec.c
index a57f9f4f3b87..6a38a0fa0e2d 100644
--- a/drivers/media/platform/amphion/vdec.c
+++ b/drivers/media/platform/amphion/vdec.c
@@ -195,7 +195,6 @@  static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
 	struct vdec_t *vdec = inst->priv;
 	int ret = 0;
 
-	vpu_inst_lock(inst);
 	switch (ctrl->id) {
 	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
 		vdec->params.display_delay_enable = ctrl->val;
@@ -207,7 +206,6 @@  static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
 		ret = -EINVAL;
 		break;
 	}
-	vpu_inst_unlock(inst);
 
 	return ret;
 }
diff --git a/drivers/media/platform/amphion/venc.c b/drivers/media/platform/amphion/venc.c
index cdfaba9d107b..351b4edc8742 100644
--- a/drivers/media/platform/amphion/venc.c
+++ b/drivers/media/platform/amphion/venc.c
@@ -518,7 +518,6 @@  static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
 	struct venc_t *venc = inst->priv;
 	int ret = 0;
 
-	vpu_inst_lock(inst);
 	switch (ctrl->id) {
 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
 		venc->params.profile = ctrl->val;
@@ -579,7 +578,6 @@  static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
 		ret = -EINVAL;
 		break;
 	}
-	vpu_inst_unlock(inst);
 
 	return ret;
 }