mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH V2 1/2] bdisp: Fix a possible sleep-in-atomic bug in bdisp_hw_reset
@ 2017-12-16 11:54 Jia-Ju Bai
  2017-12-19 10:43 ` Fabien DESSENNE
  0 siblings, 1 reply; 3+ messages in thread
From: Jia-Ju Bai @ 2017-12-16 11:54 UTC (permalink / raw)
  To: fabien.dessenne, hverkuil, benjamin.gaignard, mchehab
  Cc: linux-media, linux-kernel, Jia-Ju Bai

The driver may sleep under a spinlock.
The function call path is:
bdisp_device_run (acquire the spinlock)
  bdisp_hw_reset
    msleep --> may sleep

To fix it, readl_poll_timeout_atomic is used to replace msleep.

This bug is found by my static analysis tool(DSAC) and 
checked by my code review.

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

diff --git a/drivers/media/platform/sti/bdisp/bdisp-hw.c b/drivers/media/platform/sti/bdisp/bdisp-hw.c
index b7892f3..e94a371 100644
--- a/drivers/media/platform/sti/bdisp/bdisp-hw.c
+++ b/drivers/media/platform/sti/bdisp/bdisp-hw.c
@@ -5,6 +5,7 @@
  */
 
 #include <linux/delay.h>
+#include <linux/iopoll.h>
 
 #include "bdisp.h"
 #include "bdisp-filter.h"
@@ -366,7 +367,7 @@ struct bdisp_filter_addr {
  */
 int bdisp_hw_reset(struct bdisp_dev *bdisp)
 {
-	unsigned int i;
+	u32 tmp;
 
 	dev_dbg(bdisp->dev, "%s\n", __func__);
 
@@ -379,15 +380,14 @@ int bdisp_hw_reset(struct bdisp_dev *bdisp)
 	writel(0, bdisp->regs + BLT_CTL);
 
 	/* Wait for reset done */
-	for (i = 0; i < POLL_RST_MAX; i++) {
-		if (readl(bdisp->regs + BLT_STA1) & BLT_STA1_IDLE)
-			break;
-		msleep(POLL_RST_DELAY_MS);
-	}
-	if (i == POLL_RST_MAX)
+	if (readl_poll_timeout_atomic(bdisp->regs + BLT_STA1, tmp,
+		(tmp & BLT_STA1_IDLE), POLL_RST_DELAY_MS,
+			POLL_RST_DELAY_MS * POLL_RST_MAX)) {
 		dev_err(bdisp->dev, "Reset timeout\n");
+		return -EAGAIN;
+	}
 
-	return (i == POLL_RST_MAX) ? -EAGAIN : 0;
+	return 0;
 }
 
 /**
-- 
1.7.9.5

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

* Re: [PATCH V2 1/2] bdisp: Fix a possible sleep-in-atomic bug in bdisp_hw_reset
  2017-12-16 11:54 [PATCH V2 1/2] bdisp: Fix a possible sleep-in-atomic bug in bdisp_hw_reset Jia-Ju Bai
@ 2017-12-19 10:43 ` Fabien DESSENNE
  2017-12-19 13:57   ` Jia-Ju Bai
  0 siblings, 1 reply; 3+ messages in thread
From: Fabien DESSENNE @ 2017-12-19 10:43 UTC (permalink / raw)
  To: Jia-Ju Bai, hverkuil, Benjamin GAIGNARD, mchehab
  Cc: linux-media, linux-kernel

Hi,


On 16/12/17 12:54, Jia-Ju Bai wrote:
> The driver may sleep under a spinlock.
> The function call path is:
> bdisp_device_run (acquire the spinlock)
>    bdisp_hw_reset
>      msleep --> may sleep
>
> To fix it, readl_poll_timeout_atomic is used to replace msleep.
>
> This bug is found by my static analysis tool(DSAC) and
> checked by my code review.
>
> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> ---
>   drivers/media/platform/sti/bdisp/bdisp-hw.c |   16 ++++++++--------
>   1 file changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/media/platform/sti/bdisp/bdisp-hw.c b/drivers/media/platform/sti/bdisp/bdisp-hw.c
> index b7892f3..e94a371 100644
> --- a/drivers/media/platform/sti/bdisp/bdisp-hw.c
> +++ b/drivers/media/platform/sti/bdisp/bdisp-hw.c
> @@ -5,6 +5,7 @@
>    */
>   
>   #include <linux/delay.h>

This delay.h include is no more needed, remove it.

> +#include <linux/iopoll.h>
>   
>   #include "bdisp.h"
>   #include "bdisp-filter.h"
> @@ -366,7 +367,7 @@ struct bdisp_filter_addr {
>    */
>   int bdisp_hw_reset(struct bdisp_dev *bdisp)
>   {
> -	unsigned int i;
> +	u32 tmp;
>   
>   	dev_dbg(bdisp->dev, "%s\n", __func__);
>   
> @@ -379,15 +380,14 @@ int bdisp_hw_reset(struct bdisp_dev *bdisp)
>   	writel(0, bdisp->regs + BLT_CTL);
>   
>   	/* Wait for reset done */
> -	for (i = 0; i < POLL_RST_MAX; i++) {
> -		if (readl(bdisp->regs + BLT_STA1) & BLT_STA1_IDLE)
> -			break;
> -		msleep(POLL_RST_DELAY_MS);
> -	}
> -	if (i == POLL_RST_MAX)

As recommended by Mauro, please add this comment:
Despite the large timeout, most of the time the reset happens without 
needing any delays

> +	if (readl_poll_timeout_atomic(bdisp->regs + BLT_STA1, tmp,
> +		(tmp & BLT_STA1_IDLE), POLL_RST_DELAY_MS,
> +			POLL_RST_DELAY_MS * POLL_RST_MAX)) {

read_poll_timeout expects US timings, not MS.

>   		dev_err(bdisp->dev, "Reset timeout\n");
> +		return -EAGAIN;
> +	}
>   
> -	return (i == POLL_RST_MAX) ? -EAGAIN : 0;
> +	return 0;
>   }
>   
>   /**

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

* Re: [PATCH V2 1/2] bdisp: Fix a possible sleep-in-atomic bug in bdisp_hw_reset
  2017-12-19 10:43 ` Fabien DESSENNE
@ 2017-12-19 13:57   ` Jia-Ju Bai
  0 siblings, 0 replies; 3+ messages in thread
From: Jia-Ju Bai @ 2017-12-19 13:57 UTC (permalink / raw)
  To: Fabien DESSENNE, hverkuil, Benjamin GAIGNARD, mchehab
  Cc: linux-media, linux-kernel


On 2017/12/19 18:43, Fabien DESSENNE wrote:
> Hi,
>
>
> On 16/12/17 12:54, Jia-Ju Bai wrote:
>> The driver may sleep under a spinlock.
>> The function call path is:
>> bdisp_device_run (acquire the spinlock)
>>     bdisp_hw_reset
>>       msleep --> may sleep
>>
>> To fix it, readl_poll_timeout_atomic is used to replace msleep.
>>
>> This bug is found by my static analysis tool(DSAC) and
>> checked by my code review.
>>
>> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
>> ---
>>    drivers/media/platform/sti/bdisp/bdisp-hw.c |   16 ++++++++--------
>>    1 file changed, 8 insertions(+), 8 deletions(-)
>>
>> diff --git a/drivers/media/platform/sti/bdisp/bdisp-hw.c b/drivers/media/platform/sti/bdisp/bdisp-hw.c
>> index b7892f3..e94a371 100644
>> --- a/drivers/media/platform/sti/bdisp/bdisp-hw.c
>> +++ b/drivers/media/platform/sti/bdisp/bdisp-hw.c
>> @@ -5,6 +5,7 @@
>>     */
>>    
>>    #include <linux/delay.h>
> This delay.h include is no more needed, remove it.
>
>> +#include <linux/iopoll.h>
>>    
>>    #include "bdisp.h"
>>    #include "bdisp-filter.h"
>> @@ -366,7 +367,7 @@ struct bdisp_filter_addr {
>>     */
>>    int bdisp_hw_reset(struct bdisp_dev *bdisp)
>>    {
>> -	unsigned int i;
>> +	u32 tmp;
>>    
>>    	dev_dbg(bdisp->dev, "%s\n", __func__);
>>    
>> @@ -379,15 +380,14 @@ int bdisp_hw_reset(struct bdisp_dev *bdisp)
>>    	writel(0, bdisp->regs + BLT_CTL);
>>    
>>    	/* Wait for reset done */
>> -	for (i = 0; i < POLL_RST_MAX; i++) {
>> -		if (readl(bdisp->regs + BLT_STA1) & BLT_STA1_IDLE)
>> -			break;
>> -		msleep(POLL_RST_DELAY_MS);
>> -	}
>> -	if (i == POLL_RST_MAX)
> As recommended by Mauro, please add this comment:
> Despite the large timeout, most of the time the reset happens without
> needing any delays
>
>> +	if (readl_poll_timeout_atomic(bdisp->regs + BLT_STA1, tmp,
>> +		(tmp & BLT_STA1_IDLE), POLL_RST_DELAY_MS,
>> +			POLL_RST_DELAY_MS * POLL_RST_MAX)) {
> read_poll_timeout expects US timings, not MS.
>
>>    		dev_err(bdisp->dev, "Reset timeout\n");
>> +		return -EAGAIN;
>> +	}
>>    
>> -	return (i == POLL_RST_MAX) ? -EAGAIN : 0;
>> +	return 0;
>>    }
>>    
>>    /**

Hi,

Okay, I have submitted a new patch according to your advice :)


Thanks,
Jia-Ju Bai

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

end of thread, other threads:[~2017-12-19 13:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-16 11:54 [PATCH V2 1/2] bdisp: Fix a possible sleep-in-atomic bug in bdisp_hw_reset Jia-Ju Bai
2017-12-19 10:43 ` Fabien DESSENNE
2017-12-19 13:57   ` Jia-Ju Bai

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®