diff mbox series

[v5] clk: mvebu: Prevent division by zero in clk_double_div_recalc_rate()

Message ID 20241016101122.2092-1-adiupina@astralinux.ru (mailing list archive)
State Under Review
Headers show
Series [v5] clk: mvebu: Prevent division by zero in clk_double_div_recalc_rate() | expand

Commit Message

Alexandra Diupina Oct. 16, 2024, 10:11 a.m. UTC
It is not known exactly what values can be contained in the registers
at the addresses DIV_SEL0, DIV_SEL1, DIV_SEL2, so the return value of
get_div() can be zero.

The documentation does not describe the behavior of hardware when
receiving a zero rate divider, so add warning assertion and bail out
with a default value if it is violated to prevent division by zero.
Non panic_on_warn systems would at least survive in this case but
still have a valuable trace. The warning assertion would state in
the code that the condition is very unexpected and most probably
won't ever happen but not 100% sure because it depends on hardware
behavior.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Suggested-by: Fedor Pchelkin <pchelkin@ispras.ru>
Signed-off-by: Alexandra Diupina <adiupina@astralinux.ru>
---
v5: using WARN_ON_ONCE() and changing the commit message
v4: replace hw->init->name with clk_hw_get_name(hw)
v3: fix indentation
v2: added explanations to the commit message and printing 
of an error message when div==0
 drivers/clk/mvebu/armada-37xx-periph.c | 3 +++
 1 file changed, 3 insertions(+)
diff mbox series

Patch

diff --git a/drivers/clk/mvebu/armada-37xx-periph.c b/drivers/clk/mvebu/armada-37xx-periph.c
index 13906e31bef8..ad7b477596ed 100644
--- a/drivers/clk/mvebu/armada-37xx-periph.c
+++ b/drivers/clk/mvebu/armada-37xx-periph.c
@@ -343,6 +343,9 @@  static unsigned long clk_double_div_recalc_rate(struct clk_hw *hw,
 	div = get_div(double_div->reg1, double_div->shift1);
 	div *= get_div(double_div->reg2, double_div->shift2);
 
+	if (WARN_ON_ONCE(!div))
+		return 0;
+
 	return DIV_ROUND_UP_ULL((u64)parent_rate, div);
 }