All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tom Rini <trini@konsulko.com>
To: Sean Anderson <seanga2@gmail.com>
Cc: u-boot@lists.denx.de, Simon Glass <sjg@chromium.org>
Subject: Re: [scan-admin@coverity.com: New Defects reported by Coverity Scan for Das U-Boot]
Date: Tue, 27 Jul 2021 11:04:20 -0400	[thread overview]
Message-ID: <20210727150420.GG9379@bill-the-cat> (raw)
In-Reply-To: <f8d67c36-575f-caad-3a26-2bad8e6e7808@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 11419 bytes --]

On Mon, Jul 26, 2021 at 11:26:39PM -0400, Sean Anderson wrote:
> On 7/26/21 10:52 PM, Tom Rini wrote:
> > ----- Forwarded message from scan-admin@coverity.com -----
> > 
> > Date: Tue, 27 Jul 2021 01:10:27 +0000 (UTC)
> > From: scan-admin@coverity.com
> > To: tom.rini@gmail.com
> > Subject: New Defects reported by Coverity Scan for Das U-Boot
> > 
> > Hi,
> > 
> > Please find the latest report on new defect(s) introduced to Das U-Boot found with Coverity Scan.
> > 
> > 6 new defect(s) introduced to Das U-Boot found with Coverity Scan.
> > 9 defect(s), reported by Coverity Scan earlier, were marked fixed in the recent build analyzed by Coverity Scan.
> > 
> > New defect(s) Reported-by: Coverity Scan
> > Showing 6 of 6 defect(s)
> > 
> > 
> > ** CID 332931:  Control flow issues  (NO_EFFECT)
> > /drivers/clk/clk_kendryte.c: 852 in k210_pll_set_rate()
> > 
> > 
> > ________________________________________________________________________________________________________
> > *** CID 332931:  Control flow issues  (NO_EFFECT)
> > /drivers/clk/clk_kendryte.c: 852 in k210_pll_set_rate()
> > 846     	int err;
> > 847     	const struct k210_pll_params *pll = &k210_plls[id];
> > 848     	struct k210_pll_config config = {};
> > 849     	u32 reg;
> > 850     	ulong calc_rate;
> > 851
> > > > >      CID 332931:  Control flow issues  (NO_EFFECT)
> > > > >      This less-than-zero comparison of an unsigned value is never true. "rate_in < 0UL".
> > 852     	if (rate_in < 0)
> > 853     		return rate_in;
> > 854
> > 855     	err = k210_pll_calc_config(rate, rate_in, &config);
> > 856     	if (err)
> > 857     		return err;
> > 
> 
> > ** CID 332929:  Integer handling issues  (NO_EFFECT)
> > /drivers/clk/clk_kendryte.c: 898 in k210_pll_get_rate()
> > 
> > 
> > ________________________________________________________________________________________________________
> > *** CID 332929:  Integer handling issues  (NO_EFFECT)
> > /drivers/clk/clk_kendryte.c: 898 in k210_pll_get_rate()
> > 892     static ulong k210_pll_get_rate(struct k210_clk_priv *priv, int id,
> > 893     			       ulong rate_in)
> > 894     {
> > 895     	u64 r, f, od;
> > 896     	u32 reg = readl(priv->base + k210_plls[id].off);
> > 897
> > > > >      CID 332929:  Integer handling issues  (NO_EFFECT)
> > > > >      This less-than-zero comparison of an unsigned value is never true. "rate_in < 0UL".
> > 898     	if (rate_in < 0 || (reg & K210_PLL_BYPASS))
> > 899     		return rate_in;
> > 900
> > 901     	if (!(reg & K210_PLL_PWRD))
> > 902     		return 0;
> > 903
> > 
> 
> 
> Will send a patch for these.
> 
> > ** CID 332927:    (DIVIDE_BY_ZERO)
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > 
> > 
> > ________________________________________________________________________________________________________
> > *** CID 332927:    (DIVIDE_BY_ZERO)
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > 778     			} else {
> > 779     				/*
> > 780     				 * There is no way to only divide once; we need
> > 781     				 * to examine the frequency with and without the
> > 782     				 * effect of od.
> > 783     				 */
> > > > >      CID 332927:    (DIVIDE_BY_ZERO)
> > > > >      In function call "__div64_32", division by expression "__base" which may be zero has undefined behavior.
> > 784     				u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
> > 785
> > 786     				if (vco > 1750000000 || vco < 340000000)
> > 787     					out_of_spec = true;
> > 788     			}
> > 789
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > 778     			} else {
> > 779     				/*
> > 780     				 * There is no way to only divide once; we need
> > 781     				 * to examine the frequency with and without the
> > 782     				 * effect of od.
> > 783     				 */
> > > > >      CID 332927:    (DIVIDE_BY_ZERO)
> > > > >      In expression "(u32)_tmp % __base", modulo by expression "__base" which may be zero has undefined behavior.
> > 784     				u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
> > 785
> > 786     				if (vco > 1750000000 || vco < 340000000)
> > 787     					out_of_spec = true;
> > 788     			}
> > 789
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > 778     			} else {
> > 779     				/*
> > 780     				 * There is no way to only divide once; we need
> > 781     				 * to examine the frequency with and without the
> > 782     				 * effect of od.
> > 783     				 */
> > > > >      CID 332927:    (DIVIDE_BY_ZERO)
> > > > >      In function call "__div64_32", division by expression "__base" which may be zero has undefined behavior.
> > 784     				u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
> > 785
> > 786     				if (vco > 1750000000 || vco < 340000000)
> > 787     					out_of_spec = true;
> > 788     			}
> > 789
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > 778     			} else {
> > 779     				/*
> > 780     				 * There is no way to only divide once; we need
> > 781     				 * to examine the frequency with and without the
> > 782     				 * effect of od.
> > 783     				 */
> > > > >      CID 332927:    (DIVIDE_BY_ZERO)
> > > > >      In function call "__div64_32", division by expression "__base" which may be zero has undefined behavior.
> > 784     				u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
> > 785
> > 786     				if (vco > 1750000000 || vco < 340000000)
> > 787     					out_of_spec = true;
> > 788     			}
> > 789
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > 778     			} else {
> > 779     				/*
> > 780     				 * There is no way to only divide once; we need
> > 781     				 * to examine the frequency with and without the
> > 782     				 * effect of od.
> > 783     				 */
> > > > >      CID 332927:    (DIVIDE_BY_ZERO)
> > > > >      In function call "__div64_32", division by expression "__base" which may be zero has undefined behavior.
> > 784     				u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
> > 785
> > 786     				if (vco > 1750000000 || vco < 340000000)
> > 787     					out_of_spec = true;
> > 788     			}
> > 789
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > 778     			} else {
> > 779     				/*
> > 780     				 * There is no way to only divide once; we need
> > 781     				 * to examine the frequency with and without the
> > 782     				 * effect of od.
> > 783     				 */
> > > > >      CID 332927:    (DIVIDE_BY_ZERO)
> > > > >      In function call "__div64_32", division by expression "__base" which may be zero has undefined behavior.
> > 784     				u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
> > 785
> > 786     				if (vco > 1750000000 || vco < 340000000)
> > 787     					out_of_spec = true;
> > 788     			}
> > 789
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > 778     			} else {
> > 779     				/*
> > 780     				 * There is no way to only divide once; we need
> > 781     				 * to examine the frequency with and without the
> > 782     				 * effect of od.
> > 783     				 */
> > > > >      CID 332927:    (DIVIDE_BY_ZERO)
> > > > >      In expression "(u32)_tmp % __base", modulo by expression "__base" which may be zero has undefined behavior.
> > 784     				u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
> > 785
> > 786     				if (vco > 1750000000 || vco < 340000000)
> > 787     					out_of_spec = true;
> > 788     			}
> > 789
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > 778     			} else {
> > 779     				/*
> > 780     				 * There is no way to only divide once; we need
> > 781     				 * to examine the frequency with and without the
> > 782     				 * effect of od.
> > 783     				 */
> > > > >      CID 332927:    (DIVIDE_BY_ZERO)
> > > > >      In expression "(u32)_tmp % __base", modulo by expression "__base" which may be zero has undefined behavior.
> > 784     				u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
> > 785
> > 786     				if (vco > 1750000000 || vco < 340000000)
> > 787     					out_of_spec = true;
> > 788     			}
> > 789
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > 778     			} else {
> > 779     				/*
> > 780     				 * There is no way to only divide once; we need
> > 781     				 * to examine the frequency with and without the
> > 782     				 * effect of od.
> > 783     				 */
> > > > >      CID 332927:    (DIVIDE_BY_ZERO)
> > > > >      In expression "(u32)_tmp % __base", modulo by expression "__base" which may be zero has undefined behavior.
> > 784     				u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
> > 785
> > 786     				if (vco > 1750000000 || vco < 340000000)
> > 787     					out_of_spec = true;
> > 788     			}
> > 789
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > 778     			} else {
> > 779     				/*
> > 780     				 * There is no way to only divide once; we need
> > 781     				 * to examine the frequency with and without the
> > 782     				 * effect of od.
> > 783     				 */
> > > > >      CID 332927:    (DIVIDE_BY_ZERO)
> > > > >      In expression "(u32)_tmp % __base", modulo by expression "__base" which may be zero has undefined behavior.
> > 784     				u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
> > 785
> > 786     				if (vco > 1750000000 || vco < 340000000)
> > 787     					out_of_spec = true;
> > 788     			}
> > 789
> > /drivers/clk/clk_kendryte.c: 784 in k210_pll_calc_config()
> > 778     			} else {
> > 779     				/*
> > 780     				 * There is no way to only divide once; we need
> > 781     				 * to examine the frequency with and without the
> > 782     				 * effect of od.
> > 783     				 */
> > > > >      CID 332927:    (DIVIDE_BY_ZERO)
> > > > >      In expression "(u32)_tmp % __base", modulo by expression "__base" which may be zero has undefined behavior.
> > 784     				u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
> > 785
> > 786     				if (vco > 1750000000 || vco < 340000000)
> > 787     					out_of_spec = true;
> > 788     			}
> > 789
> 
> These are completely safe, but it is relatively non-obvious why. The
> only way that r can be 0 is on the very first iteration. When rate >
> rate_in, r gets assigned (to a non-zero number) immediately. For the
> converse, we only assign to r and od when r * od < goal. goal is
> calculated by multiplying f (which is always at least 1) with inv_ratio,
> shifted right by 32 bits. In the worst-case (the first iteration), this
> is just inv_ratio >> 32. But inv_ratio is rate_in << 32 / rate, and
> above we assumed that rate <= rate_in. So inv_ratio is always at least 1
> << 32, and we never divide by 0 :)
> 
> In the course of investigating the above, I added some additional test
> cases and discovered that we don't always get the best factors in some
> cases. I will also send a patch for this.

Thanks for looking so quickly!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

  reply	other threads:[~2021-07-27 15:04 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-27  2:52 [scan-admin@coverity.com: New Defects reported by Coverity Scan for Das U-Boot] Tom Rini
2021-07-27  3:26 ` Sean Anderson
2021-07-27 15:04   ` Tom Rini [this message]
  -- strict thread matches above, loose matches on Subject: below --
2022-09-06 15:50 Tom Rini
2022-05-09 17:22 Tom Rini
2022-04-25 23:41 Tom Rini
2022-03-05 18:27 Tom Rini
2022-02-15 19:29 Tom Rini
2022-02-01  0:33 Tom Rini
2021-11-15 18:02 Tom Rini
2021-11-02 16:22 Tom Rini
2021-11-01 20:06 Tom Rini
2021-09-15 14:11 Tom Rini
2021-08-30 17:39 Tom Rini
2021-08-31 15:18 ` Oleh Kravchenko
2021-09-06 14:05 ` Oleh Kravchenko
2021-09-06 15:23   ` Tom Rini
2021-08-16 19:57 Tom Rini
2021-08-16 20:15 ` Pali Rohár
2021-08-16 20:20   ` Tom Rini
2021-05-26 16:58 Tom Rini
2021-05-12 22:30 Tom Rini
2021-04-19 12:20 Tom Rini
2021-04-20  0:58 ` Asherah Connor
2021-04-20  1:17   ` Tom Rini
2021-04-20  6:13 ` Dario Binacchi
2021-03-30 19:55 Tom Rini
2021-03-02 14:42 Tom Rini
2021-02-23 16:15 Tom Rini
2021-02-01 19:51 Tom Rini
2021-01-26 16:41 Tom Rini
2021-01-20 19:04 Tom Rini
2021-01-20 20:43 ` Heinrich Schuchardt
2021-01-20 22:33   ` Heinrich Schuchardt
2021-01-21  2:09   ` AKASHI Takahiro
2021-01-26 17:02     ` Tom Rini
2021-01-20 21:03 ` Andre Przywara
2021-01-20 21:34   ` Tom Rini
2021-01-21 11:36 ` Sughosh Ganu
2021-01-21 13:44   ` Heinrich Schuchardt
2021-01-22  8:54     ` Sughosh Ganu
2021-01-22 11:37       ` Heinrich Schuchardt
2020-12-03 17:28 Tom Rini
2020-11-10 21:18 Tom Rini
2020-10-30 19:16 Tom Rini
2020-11-02 11:54 ` Pratyush Yadav

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=20210727150420.GG9379@bill-the-cat \
    --to=trini@konsulko.com \
    --cc=seanga2@gmail.com \
    --cc=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.