diff mbox

ASoC: codec: wm9860: avoid maybe-uninitialized warning

Message ID 20170419170449.2965453-1-arnd@arndb.de (mailing list archive)
State New, archived
Headers show

Commit Message

Arnd Bergmann April 19, 2017, 5:04 p.m. UTC
The new PLL configuration code triggers a harmless warning:

sound/soc/codecs/wm8960.c: In function 'wm8960_configure_clocking':
sound/soc/codecs/wm8960.c:735:3: error: 'best_freq_out' may be used uninitialized in this function [-Werror=maybe-uninitialized]
   wm8960_set_pll(codec, freq_in, best_freq_out);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sound/soc/codecs/wm8960.c:699:12: note: 'best_freq_out' was declared here

I think the warning was introduced by Daniel's bugfix. I've come up
with a way to simplify the code in a way that is more readable to
both humans and to gcc, which gets us rid of the warning.

Fixes: 84fdc00d519f ("ASoC: codec: wm9860: Refactor PLL out freq search")
Fixes: 303e8954af8d ("ASoC: codec: wm8960: Stop when a matching PLL freq is found")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 sound/soc/codecs/wm8960.c | 25 ++++++++-----------------
 1 file changed, 8 insertions(+), 17 deletions(-)

Comments

Daniel Baluta April 20, 2017, 6:48 a.m. UTC | #1
Hi Arnd,

On Wed, Apr 19, 2017 at 8:04 PM, Arnd Bergmann <arnd@arndb.de> wrote:
> The new PLL configuration code triggers a harmless warning:
>
> sound/soc/codecs/wm8960.c: In function 'wm8960_configure_clocking':
> sound/soc/codecs/wm8960.c:735:3: error: 'best_freq_out' may be used uninitialized in this function [-Werror=maybe-uninitialized]
>    wm8960_set_pll(codec, freq_in, best_freq_out);
>    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> sound/soc/codecs/wm8960.c:699:12: note: 'best_freq_out' was declared here
>
> I think the warning was introduced by Daniel's bugfix. I've come up
> with a way to simplify the code in a way that is more readable to
> both humans and to gcc, which gets us rid of the warning.

I've struggled with this kind of warning when reworking wm8960
bitclock computation.

Anyhow, for this exact patch the warning didn't showed up.

I used:

gcc version 6.2.1 20161016 (Linaro GCC 6.2-2016.11)

My next patch:

https://patchwork.kernel.org/patch/9666921/ [1]

which is under review, had the issue you mention (in v1)

https://lkml.org/lkml/2017/4/5/246

but I initialized best_freq_out with 0 to get rid of the problem.

>
> Fixes: 84fdc00d519f ("ASoC: codec: wm9860: Refactor PLL out freq search")
> Fixes: 303e8954af8d ("ASoC: codec: wm8960: Stop when a matching PLL freq is found")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>  sound/soc/codecs/wm8960.c | 25 ++++++++-----------------
>  1 file changed, 8 insertions(+), 17 deletions(-)
>
> diff --git a/sound/soc/codecs/wm8960.c b/sound/soc/codecs/wm8960.c
> index ace69da97cb8..ba0f80504c6b 100644
> --- a/sound/soc/codecs/wm8960.c
> +++ b/sound/soc/codecs/wm8960.c
> @@ -696,14 +696,12 @@ int wm8960_configure_pll(struct snd_soc_codec *codec, int freq_in,
>  {
>         struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
>         int sysclk, bclk, lrclk, freq_out;
> -       int diff, best_freq_out;
> +       int diff;
>         int i, j, k;
>
>         bclk = wm8960->bclk;
>         lrclk = wm8960->lrclk;
>
> -       *bclk_idx = -1;
> -
>         for (i = 0; i < ARRAY_SIZE(sysclk_divs); ++i) {
>                 if (sysclk_divs[i] == -1)
>                         continue;
> @@ -720,22 +718,14 @@ int wm8960_configure_pll(struct snd_soc_codec *codec, int freq_in,
>                                         *sysclk_idx = i;
>                                         *dac_idx = j;
>                                         *bclk_idx = k;
> -                                       best_freq_out = freq_out;
> -                                       break;
> +                                       return freq_out;
>                                 }
>                         }
> -                       if (k != ARRAY_SIZE(bclk_divs))
> -                               break;
>                 }
> -               if (j != ARRAY_SIZE(dac_divs))
> -                       break;
>         }
> -
> -       if (*bclk_idx != -1)
> -               wm8960_set_pll(codec, freq_in, best_freq_out);

I think the compiler is tricked into thinking that best_freq_out might
be uninitialized. Notice
that each time *bclk_idx is assigned a value (which is always >=0) we
also initialize best_freq_out.

> -
> -       return *bclk_idx;
> +       return -EINVAL;
>  }
> +
>  static int wm8960_configure_clocking(struct snd_soc_codec *codec)
>  {
>         struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
> @@ -783,11 +773,12 @@ static int wm8960_configure_clocking(struct snd_soc_codec *codec)
>                 }
>         }
>
> -       ret = wm8960_configure_pll(codec, freq_in, &i, &j, &k);
> -       if (ret < 0) {
> +       freq_out = wm8960_configure_pll(codec, freq_in, &i, &j, &k);
> +       if (freq_out < 0) {
>                 dev_err(codec->dev, "failed to configure clock via PLL\n");
> -               return -EINVAL;
> +               return freq_out;
>         }
> +       wm8960_set_pll(codec, freq_in, freq_out);
>
>  configure_clock:
>         /* configure sysclk clock */

Your idea looks good, will need to rework my follow up patch on it:

https://patchwork.kernel.org/patch/9666921/

thanks,
Daniel.
Arnd Bergmann April 20, 2017, 7:44 a.m. UTC | #2
On Thu, Apr 20, 2017 at 8:48 AM, Daniel Baluta <daniel.baluta@gmail.com> wrote:
> Hi Arnd,
>
> On Wed, Apr 19, 2017 at 8:04 PM, Arnd Bergmann <arnd@arndb.de> wrote:
>> The new PLL configuration code triggers a harmless warning:
>>
>> sound/soc/codecs/wm8960.c: In function 'wm8960_configure_clocking':
>> sound/soc/codecs/wm8960.c:735:3: error: 'best_freq_out' may be used uninitialized in this function [-Werror=maybe-uninitialized]
>>    wm8960_set_pll(codec, freq_in, best_freq_out);
>>    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> sound/soc/codecs/wm8960.c:699:12: note: 'best_freq_out' was declared here
>>
>> I think the warning was introduced by Daniel's bugfix. I've come up
>> with a way to simplify the code in a way that is more readable to
>> both humans and to gcc, which gets us rid of the warning.
>
> I've struggled with this kind of warning when reworking wm8960
> bitclock computation.
>
> Anyhow, for this exact patch the warning didn't showed up.
>
> I used:
>
> gcc version 6.2.1 20161016 (Linaro GCC 6.2-2016.11)

I'm using gcc-7.0.1 here, which overall has better warnings for
-Wmaybe-uninitialized
than older versions, but sometimes also finds new false positives.

> My next patch:
>
> https://patchwork.kernel.org/patch/9666921/ [1]
>
> which is under review, had the issue you mention (in v1)
>
> https://lkml.org/lkml/2017/4/5/246
>
> but I initialized best_freq_out with 0 to get rid of the problem.

I try hard not to do that, because it can hide real problems in case the
code gets modified further to actually have an uninitialized use that we
would otherwise get a warning for.

>> @@ -720,22 +718,14 @@ int wm8960_configure_pll(struct snd_soc_codec *codec, int freq_in,
>>                                         *sysclk_idx = i;
>>                                         *dac_idx = j;
>>                                         *bclk_idx = k;
>> -                                       best_freq_out = freq_out;
>> -                                       break;
>> +                                       return freq_out;
>>                                 }
>>                         }
>> -                       if (k != ARRAY_SIZE(bclk_divs))
>> -                               break;
>>                 }
>> -               if (j != ARRAY_SIZE(dac_divs))
>> -                       break;
>>         }
>> -
>> -       if (*bclk_idx != -1)
>> -               wm8960_set_pll(codec, freq_in, best_freq_out);
>
> I think the compiler is tricked into thinking that best_freq_out might
> be uninitialized. Notice
> that each time *bclk_idx is assigned a value (which is always >=0) we
> also initialize best_freq_out.

Right. This is probably the result of one of two things that
prevent the compiler from figuring it out:

a) bclk_idx being an indirect pointer might cause the compiler to
    decide that another operation might overwrite it, e.g. if it points
    to the same location as one of the other pointers.

b) The flow analysis might just get too tricky, so when gcc gives
    up trying to work out whether the assignment has happened
    at least once for the two variables, it concludes that it doesn't
    know, without seeing that the answer is always the same for
    both of them.

>> @@ -783,11 +773,12 @@ static int wm8960_configure_clocking(struct snd_soc_codec *codec)
>>                 }
>>         }
>>
>> -       ret = wm8960_configure_pll(codec, freq_in, &i, &j, &k);
>> -       if (ret < 0) {
>> +       freq_out = wm8960_configure_pll(codec, freq_in, &i, &j, &k);
>> +       if (freq_out < 0) {
>>                 dev_err(codec->dev, "failed to configure clock via PLL\n");
>> -               return -EINVAL;
>> +               return freq_out;
>>         }
>> +       wm8960_set_pll(codec, freq_in, freq_out);
>>
>>  configure_clock:
>>         /* configure sysclk clock */
>
> Your idea looks good, will need to rework my follow up patch on it:
>
> https://patchwork.kernel.org/patch/9666921/

Ok. Or feel free to fold my patch into yours if that makes it easier for you.

     Arnd
Daniel Baluta April 20, 2017, 8:22 a.m. UTC | #3
On Thu, Apr 20, 2017 at 10:44 AM, Arnd Bergmann <arnd@arndb.de> wrote:
> On Thu, Apr 20, 2017 at 8:48 AM, Daniel Baluta <daniel.baluta@gmail.com> wrote:
>> Hi Arnd,
>>
>> On Wed, Apr 19, 2017 at 8:04 PM, Arnd Bergmann <arnd@arndb.de> wrote:
>>> The new PLL configuration code triggers a harmless warning:
>>>
>>> sound/soc/codecs/wm8960.c: In function 'wm8960_configure_clocking':
>>> sound/soc/codecs/wm8960.c:735:3: error: 'best_freq_out' may be used uninitialized in this function [-Werror=maybe-uninitialized]
>>>    wm8960_set_pll(codec, freq_in, best_freq_out);
>>>    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> sound/soc/codecs/wm8960.c:699:12: note: 'best_freq_out' was declared here
>>>
>>> I think the warning was introduced by Daniel's bugfix. I've come up
>>> with a way to simplify the code in a way that is more readable to
>>> both humans and to gcc, which gets us rid of the warning.
>>
>> I've struggled with this kind of warning when reworking wm8960
>> bitclock computation.
>>
>> Anyhow, for this exact patch the warning didn't showed up.
>>
>> I used:
>>
>> gcc version 6.2.1 20161016 (Linaro GCC 6.2-2016.11)
>
> I'm using gcc-7.0.1 here, which overall has better warnings for
> -Wmaybe-uninitialized
> than older versions, but sometimes also finds new false positives.
>
>> My next patch:
>>
>> https://patchwork.kernel.org/patch/9666921/ [1]
>>
>> which is under review, had the issue you mention (in v1)
>>
>> https://lkml.org/lkml/2017/4/5/246
>>
>> but I initialized best_freq_out with 0 to get rid of the problem.
>
> I try hard not to do that, because it can hide real problems in case the
> code gets modified further to actually have an uninitialized use that we
> would otherwise get a warning for.
>
>>> @@ -720,22 +718,14 @@ int wm8960_configure_pll(struct snd_soc_codec *codec, int freq_in,
>>>                                         *sysclk_idx = i;
>>>                                         *dac_idx = j;
>>>                                         *bclk_idx = k;
>>> -                                       best_freq_out = freq_out;
>>> -                                       break;
>>> +                                       return freq_out;
>>>                                 }
>>>                         }
>>> -                       if (k != ARRAY_SIZE(bclk_divs))
>>> -                               break;
>>>                 }
>>> -               if (j != ARRAY_SIZE(dac_divs))
>>> -                       break;
>>>         }
>>> -
>>> -       if (*bclk_idx != -1)
>>> -               wm8960_set_pll(codec, freq_in, best_freq_out);
>>
>> I think the compiler is tricked into thinking that best_freq_out might
>> be uninitialized. Notice
>> that each time *bclk_idx is assigned a value (which is always >=0) we
>> also initialize best_freq_out.
>
> Right. This is probably the result of one of two things that
> prevent the compiler from figuring it out:
>
> a) bclk_idx being an indirect pointer might cause the compiler to
>     decide that another operation might overwrite it, e.g. if it points
>     to the same location as one of the other pointers.
>
> b) The flow analysis might just get too tricky, so when gcc gives
>     up trying to work out whether the assignment has happened
>     at least once for the two variables, it concludes that it doesn't
>     know, without seeing that the answer is always the same for
>     both of them.
>
>>> @@ -783,11 +773,12 @@ static int wm8960_configure_clocking(struct snd_soc_codec *codec)
>>>                 }
>>>         }
>>>
>>> -       ret = wm8960_configure_pll(codec, freq_in, &i, &j, &k);
>>> -       if (ret < 0) {
>>> +       freq_out = wm8960_configure_pll(codec, freq_in, &i, &j, &k);
>>> +       if (freq_out < 0) {
>>>                 dev_err(codec->dev, "failed to configure clock via PLL\n");
>>> -               return -EINVAL;
>>> +               return freq_out;
>>>         }
>>> +       wm8960_set_pll(codec, freq_in, freq_out);
>>>
>>>  configure_clock:
>>>         /* configure sysclk clock */
>>
>> Your idea looks good, will need to rework my follow up patch on it:
>>
>> https://patchwork.kernel.org/patch/9666921/
>
> Ok. Or feel free to fold my patch into yours if that makes it easier for you.

Great! I will fold your patch into my changes.

thanks Arnd!

Daniel.
Charles Keepax April 20, 2017, 9:07 a.m. UTC | #4
On Wed, Apr 19, 2017 at 07:04:20PM +0200, Arnd Bergmann wrote:
> The new PLL configuration code triggers a harmless warning:
> 
> sound/soc/codecs/wm8960.c: In function 'wm8960_configure_clocking':
> sound/soc/codecs/wm8960.c:735:3: error: 'best_freq_out' may be used uninitialized in this function [-Werror=maybe-uninitialized]
>    wm8960_set_pll(codec, freq_in, best_freq_out);
>    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> sound/soc/codecs/wm8960.c:699:12: note: 'best_freq_out' was declared here
> 
> I think the warning was introduced by Daniel's bugfix. I've come up
> with a way to simplify the code in a way that is more readable to
> both humans and to gcc, which gets us rid of the warning.
> 
> Fixes: 84fdc00d519f ("ASoC: codec: wm9860: Refactor PLL out freq search")
> Fixes: 303e8954af8d ("ASoC: codec: wm8960: Stop when a matching PLL freq is found")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---

Acked-by: Charles Keepax <ckeepax@opensource.wolfsonmicro.com>

Thanks,
Charles
Mark Brown April 20, 2017, 7:48 p.m. UTC | #5
On Thu, Apr 20, 2017 at 09:44:28AM +0200, Arnd Bergmann wrote:
> On Thu, Apr 20, 2017 at 8:48 AM, Daniel Baluta <daniel.baluta@gmail.com> wrote:
> > On Wed, Apr 19, 2017 at 8:04 PM, Arnd Bergmann <arnd@arndb.de> wrote:

> > gcc version 6.2.1 20161016 (Linaro GCC 6.2-2016.11)

> I'm using gcc-7.0.1 here, which overall has better warnings for
> -Wmaybe-uninitialized
> than older versions, but sometimes also finds new false positives.

FWIW the PLL stuff has typically been really good at generating false
positives with older versions of GCC for some reason, this driver is a
bit different though.
Mark Brown May 14, 2017, 10:05 a.m. UTC | #6
On Wed, Apr 19, 2017 at 07:04:20PM +0200, Arnd Bergmann wrote:
> The new PLL configuration code triggers a harmless warning:

This doens't apply against current code, please check and resend.
Arnd Bergmann May 15, 2017, 2:03 p.m. UTC | #7
On Sun, May 14, 2017 at 12:05 PM, Mark Brown <broonie@kernel.org> wrote:
> On Wed, Apr 19, 2017 at 07:04:20PM +0200, Arnd Bergmann wrote:
>> The new PLL configuration code triggers a harmless warning:
>
> This doens't apply against current code, please check and resend.

I can confirm that you have already merged Daniel's updated version,
and that this addresses the problem.

       Arnd
diff mbox

Patch

diff --git a/sound/soc/codecs/wm8960.c b/sound/soc/codecs/wm8960.c
index ace69da97cb8..ba0f80504c6b 100644
--- a/sound/soc/codecs/wm8960.c
+++ b/sound/soc/codecs/wm8960.c
@@ -696,14 +696,12 @@  int wm8960_configure_pll(struct snd_soc_codec *codec, int freq_in,
 {
 	struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
 	int sysclk, bclk, lrclk, freq_out;
-	int diff, best_freq_out;
+	int diff;
 	int i, j, k;
 
 	bclk = wm8960->bclk;
 	lrclk = wm8960->lrclk;
 
-	*bclk_idx = -1;
-
 	for (i = 0; i < ARRAY_SIZE(sysclk_divs); ++i) {
 		if (sysclk_divs[i] == -1)
 			continue;
@@ -720,22 +718,14 @@  int wm8960_configure_pll(struct snd_soc_codec *codec, int freq_in,
 					*sysclk_idx = i;
 					*dac_idx = j;
 					*bclk_idx = k;
-					best_freq_out = freq_out;
-					break;
+					return freq_out;
 				}
 			}
-			if (k != ARRAY_SIZE(bclk_divs))
-				break;
 		}
-		if (j != ARRAY_SIZE(dac_divs))
-			break;
 	}
-
-	if (*bclk_idx != -1)
-		wm8960_set_pll(codec, freq_in, best_freq_out);
-
-	return *bclk_idx;
+	return -EINVAL;
 }
+
 static int wm8960_configure_clocking(struct snd_soc_codec *codec)
 {
 	struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
@@ -783,11 +773,12 @@  static int wm8960_configure_clocking(struct snd_soc_codec *codec)
 		}
 	}
 
-	ret = wm8960_configure_pll(codec, freq_in, &i, &j, &k);
-	if (ret < 0) {
+	freq_out = wm8960_configure_pll(codec, freq_in, &i, &j, &k);
+	if (freq_out < 0) {
 		dev_err(codec->dev, "failed to configure clock via PLL\n");
-		return -EINVAL;
+		return freq_out;
 	}
+	wm8960_set_pll(codec, freq_in, freq_out);
 
 configure_clock:
 	/* configure sysclk clock */