Skip to contentSkip to conversation

clk: use ULONG_MAX as the initial value for the iteration in clk_mux_determine_rate_flags()

PATCH Superseded Yang Xiwen 2 participants 2 messages Apr 21, 2023 – Apr 25, 2023 Common clk

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yang Xiwen

author
#0 lore raw

PATCH 1/1 clk: use ULONG_MAX as the initial value for the iteration in clk_mux_determine_rate_flags()

Currently, clk_mux_determine_rate_flags() use 0 as the initial value for selecting the best matching parent. However, this will choose a non-existant rate(0) if the requested rate is closer to 0 than the minimum rate the parents have.

Fix that by initializing the initial value to ULONG_MAX and treat it as a magic number.

---
 drivers/clk/clk.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/clk/clk.c +4 -1
index ae07685c7588b..ab8a2acfac8f3 100644
@@ -541,6 +541,9 @@ EXPORT_SYMBOL_GPL(__clk_is_enabled);
 static bool mux_is_better_rate(unsigned long rate, unsigned long now,
 			   unsigned long best, unsigned long flags)
 {
+	if (best == ULONG_MAX)
+		return true;
+
 	if (flags & CLK_MUX_ROUND_CLOSEST)
 		return abs(now - rate) < abs(best - rate);
 
@@ -600,7 +603,7 @@ int clk_mux_determine_rate_flags(struct clk_hw *hw,
 {
 	struct clk_core *core = hw->core, *parent, *best_parent = NULL;
 	int i, num_parents, ret;
-	unsigned long best = 0;
+	unsigned long best = ULONG_MAX;
 
 	/* if NO_REPARENT flag set, pass through to current parent */
 	if (core->flags & CLK_SET_RATE_NO_REPARENT) {

---
base-commit: 76f598ba7d8e2bfb4855b5298caedd5af0c374a8
change-id: 20230421-clk-64c6b6e21cdb

Best regards,

Signature · 1 line
Yang Xiwen <[email protected]>
maintainer
#1 lore raw

Quoting Yang Xiwen via B4 Relay (2023-04-21 08:56:38)

Quoted 9 lines

From: Yang Xiwen <[email protected]>

Currently, clk_mux_determine_rate_flags() use 0 as the initial value for selecting the best matching parent. However, this will choose a non-existant rate(0) if the requested rate is closer to 0 than the minimum rate the parents have.

Fix that by initializing the initial value to ULONG_MAX and treat it as a magic number.

Can you add a unit test to clk_test.c as well?