mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] media: sti: bdisp: fix a possible sleep-in-atomic-context bug in bdisp_device_run()
@ 2019-12-18 14:05 Jia-Ju Bai
  2019-12-19  9:01 ` Fabien DESSENNE
  0 siblings, 1 reply; 3+ messages in thread
From: Jia-Ju Bai @ 2019-12-18 14:05 UTC (permalink / raw)
  To: fabien.dessenne, mchehab; +Cc: linux-media, linux-kernel, Jia-Ju Bai

The driver may sleep while holding a spinlock.
The function call path (from bottom to top) in Linux 4.19 is:

drivers/media/platform/sti/bdisp/bdisp-hw.c, 385: 
	msleep in bdisp_hw_reset
drivers/media/platform/sti/bdisp/bdisp-v4l2.c, 341: 
	bdisp_hw_reset in bdisp_device_run
drivers/media/platform/sti/bdisp/bdisp-v4l2.c, 317:
	_raw_spin_lock_irqsave in bdisp_device_run

To fix this bug, msleep() is replaced with mdelay().

This bug is found by a static analysis tool STCheck written by myself.

Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
---
 drivers/media/platform/sti/bdisp/bdisp-hw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/media/platform/sti/bdisp/bdisp-hw.c b/drivers/media/platform/sti/bdisp/bdisp-hw.c
index 4372abbb5950..1a56348805a2 100644
--- a/drivers/media/platform/sti/bdisp/bdisp-hw.c
+++ b/drivers/media/platform/sti/bdisp/bdisp-hw.c
@@ -382,7 +382,7 @@ int bdisp_hw_reset(struct bdisp_dev *bdisp)
 	for (i = 0; i < POLL_RST_MAX; i++) {
 		if (readl(bdisp->regs + BLT_STA1) & BLT_STA1_IDLE)
 			break;
-		msleep(POLL_RST_DELAY_MS);
+		mdelay(POLL_RST_DELAY_MS);
 	}
 	if (i == POLL_RST_MAX)
 		dev_err(bdisp->dev, "Reset timeout\n");
-- 
2.17.1


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

* Re: [PATCH] media: sti: bdisp: fix a possible sleep-in-atomic-context bug in bdisp_device_run()
  2019-12-18 14:05 [PATCH] media: sti: bdisp: fix a possible sleep-in-atomic-context bug in bdisp_device_run() Jia-Ju Bai
@ 2019-12-19  9:01 ` Fabien DESSENNE
  2019-12-19  9:14   ` Fabien DESSENNE
  0 siblings, 1 reply; 3+ messages in thread
From: Fabien DESSENNE @ 2019-12-19  9:01 UTC (permalink / raw)
  To: Jia-Ju Bai, mchehab; +Cc: linux-media, linux-kernel

Hi Jia_Ju,


This is a good finding. See my remarks below.


On 18/12/2019 3:05 PM, Jia-Ju Bai wrote:
> The driver may sleep while holding a spinlock.
> The function call path (from bottom to top) in Linux 4.19 is:
>
> drivers/media/platform/sti/bdisp/bdisp-hw.c, 385:
> 	msleep in bdisp_hw_reset
> drivers/media/platform/sti/bdisp/bdisp-v4l2.c, 341:
> 	bdisp_hw_reset in bdisp_device_run
> drivers/media/platform/sti/bdisp/bdisp-v4l2.c, 317:
> 	_raw_spin_lock_irqsave in bdisp_device_run
>
> To fix this bug, msleep() is replaced with mdelay().
>
> This bug is found by a static analysis tool STCheck written by myself.
>
> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> ---
>   drivers/media/platform/sti/bdisp/bdisp-hw.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/media/platform/sti/bdisp/bdisp-hw.c b/drivers/media/platform/sti/bdisp/bdisp-hw.c
> index 4372abbb5950..1a56348805a2 100644
> --- a/drivers/media/platform/sti/bdisp/bdisp-hw.c
> +++ b/drivers/media/platform/sti/bdisp/bdisp-hw.c
> @@ -382,7 +382,7 @@ int bdisp_hw_reset(struct bdisp_dev *bdisp)
>   	for (i = 0; i < POLL_RST_MAX; i++) {
>   		if (readl(bdisp->regs + BLT_STA1) & BLT_STA1_IDLE)
>   			break;
> -		msleep(POLL_RST_DELAY_MS);
> +		mdelay(POLL_RST_DELAY_MS);

In general, use of mdelay is discouraged. Here we can use udelay instead 
of, with a max value of MAX_UDELAY_MS which is 2 on ARM.

So, instead of running 50 (POLL_RST_MAX) times a 20ms 
(POLL_RST_DELAY_MS) delay, we can run 500 times a 2ms (2000us) delay.

The following changes shall be fine:

     POLL_RST_MAX 50 --> 500

     POLL_RST_DELAY_MS  20 --> 2

     mdelay(POLL_RST_DELAY_MS) --> udelay(POLL_RST_DELAY_MS * 1000)


BR

Fabien


>   	}
>   	if (i == POLL_RST_MAX)
>   		dev_err(bdisp->dev, "Reset timeout\n");

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

* Re: [PATCH] media: sti: bdisp: fix a possible sleep-in-atomic-context bug in bdisp_device_run()
  2019-12-19  9:01 ` Fabien DESSENNE
@ 2019-12-19  9:14   ` Fabien DESSENNE
  0 siblings, 0 replies; 3+ messages in thread
From: Fabien DESSENNE @ 2019-12-19  9:14 UTC (permalink / raw)
  To: Jia-Ju Bai, mchehab; +Cc: linux-media, linux-kernel

And calling readl_poll_timeout_atomic() would probably be better :)


On 19/12/2019 10:01 AM, Fabien DESSENNE wrote:
> Hi Jia_Ju,
>
>
> This is a good finding. See my remarks below.
>
>
> On 18/12/2019 3:05 PM, Jia-Ju Bai wrote:
>> The driver may sleep while holding a spinlock.
>> The function call path (from bottom to top) in Linux 4.19 is:
>>
>> drivers/media/platform/sti/bdisp/bdisp-hw.c, 385:
>>     msleep in bdisp_hw_reset
>> drivers/media/platform/sti/bdisp/bdisp-v4l2.c, 341:
>>     bdisp_hw_reset in bdisp_device_run
>> drivers/media/platform/sti/bdisp/bdisp-v4l2.c, 317:
>>     _raw_spin_lock_irqsave in bdisp_device_run
>>
>> To fix this bug, msleep() is replaced with mdelay().
>>
>> This bug is found by a static analysis tool STCheck written by myself.
>>
>> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
>> ---
>>   drivers/media/platform/sti/bdisp/bdisp-hw.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/media/platform/sti/bdisp/bdisp-hw.c 
>> b/drivers/media/platform/sti/bdisp/bdisp-hw.c
>> index 4372abbb5950..1a56348805a2 100644
>> --- a/drivers/media/platform/sti/bdisp/bdisp-hw.c
>> +++ b/drivers/media/platform/sti/bdisp/bdisp-hw.c
>> @@ -382,7 +382,7 @@ int bdisp_hw_reset(struct bdisp_dev *bdisp)
>>       for (i = 0; i < POLL_RST_MAX; i++) {
>>           if (readl(bdisp->regs + BLT_STA1) & BLT_STA1_IDLE)
>>               break;
>> -        msleep(POLL_RST_DELAY_MS);
>> +        mdelay(POLL_RST_DELAY_MS);
>
> In general, use of mdelay is discouraged. Here we can use udelay 
> instead of, with a max value of MAX_UDELAY_MS which is 2 on ARM.
>
> So, instead of running 50 (POLL_RST_MAX) times a 20ms 
> (POLL_RST_DELAY_MS) delay, we can run 500 times a 2ms (2000us) delay.
>
> The following changes shall be fine:
>
>     POLL_RST_MAX 50 --> 500
>
>     POLL_RST_DELAY_MS  20 --> 2
>
>     mdelay(POLL_RST_DELAY_MS) --> udelay(POLL_RST_DELAY_MS * 1000)
>
>
> BR
>
> Fabien
>
>
>>       }
>>       if (i == POLL_RST_MAX)
>>           dev_err(bdisp->dev, "Reset timeout\n");

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

end of thread, other threads:[~2019-12-19  9:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-18 14:05 [PATCH] media: sti: bdisp: fix a possible sleep-in-atomic-context bug in bdisp_device_run() Jia-Ju Bai
2019-12-19  9:01 ` Fabien DESSENNE
2019-12-19  9:14   ` Fabien DESSENNE

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®