[scan-admin@coverity.com: New Defects reported by Coverity Scan for Das U-Boot]

----- 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 332930: Integer handling issues (NO_EFFECT) /lib/display_options.c: 216 in print_buffer()
________________________________________________________________________________________________________ *** CID 332930: Integer handling issues (NO_EFFECT) /lib/display_options.c: 216 in print_buffer() 210 while (count) { 211 uint thislinelen; 212 char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)]; 213 214 thislinelen = hexdump_line(addr, data, width, count, linelen, 215 buf, sizeof(buf));
CID 332930: Integer handling issues (NO_EFFECT) This greater-than-or-equal-to-zero comparison of an unsigned value is always true. "thislinelen >= 0U".
216 assert(thislinelen >= 0); 217 puts(buf); 218 putc('\n'); 219 220 /* update references */ 221 data += thislinelen * width;
** 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
** CID 332928: Integer handling issues (NO_EFFECT) /common/log.c: 305 in _log_buffer()
________________________________________________________________________________________________________ *** CID 332928: Integer handling issues (NO_EFFECT) /common/log.c: 305 in _log_buffer() 299 while (count) { 300 uint thislinelen; 301 char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)]; 302 303 thislinelen = hexdump_line(addr, data, width, count, linelen, 304 buf, sizeof(buf));
CID 332928: Integer handling issues (NO_EFFECT) This greater-than-or-equal-to-zero comparison of an unsigned value is always true. "thislinelen >= 0U".
305 assert(thislinelen >= 0); 306 _log(cat, level, file, line, func, "%s\n", buf); 307 308 /* update references */ 309 data += thislinelen * width; 310 addr += thislinelen * width;
** 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
** CID 332926: API usage errors (CHAR_IO) /common/autoboot.c: 95 in passwd_abort_crypt()
________________________________________________________________________________________________________ *** CID 332926: API usage errors (CHAR_IO) /common/autoboot.c: 95 in passwd_abort_crypt() 89 do { 90 if (tstc()) { 91 /* Check for input string overflow */ 92 if (presskey_len >= sizeof(presskey)) 93 return 0; 94
CID 332926: API usage errors (CHAR_IO) Assigning the return value of "getchar" to char "presskey[presskey_len]" truncates its value.
95 presskey[presskey_len] = getchar(); 96 97 if ((presskey[presskey_len] == '\r') || 98 (presskey[presskey_len] == '\n')) { 99 if (IS_ENABLED(CONFIG_AUTOBOOT_NEVER_TIMEOUT) && 100 !presskey_len) {
________________________________________________________________________________________________________ To view the defects in Coverity Scan visit, https://u15810271.ct.sendgrid.net/ls/click?upn=HRESupC-2F2Czv4BOaCWWCy7my0P0...
To manage Coverity Scan email notifications for "tom.rini@gmail.com", click https://u15810271.ct.sendgrid.net/ls/click?upn=HRESupC-2F2Czv4BOaCWWCy7my0P0...
----- End forwarded message -----

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.
--Sean

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!
participants (2)
-
Sean Anderson
-
Tom Rini