Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1738268

[PATCH v4 10/10] clk: fix set_rate_range when current rate is out of range

From Jerome Brunet <jbrunet@baylibre.com>
Newsgroups linux.kernel
Subject [PATCH v4 10/10] clk: fix set_rate_range when current rate is out of range
Date 2017-09-24 22:10 +0200
Message-ID <utlnZ-4Gy-43@gated-at.bofh.it> (permalink)
References <utlnX-4Gy-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


Calling clk_core_set_rate() with core->req_rate is basically a no-op
because of the early bail-out mechanism.

This may leave the clock in inconsistent state if the rate is out the
requested range. Calling clk_core_set_rate() with the closest rate
limit could solve the problem but:
- The underlying determine_rate() callback needs to account for this
  corner case (rounding within the range, if possible)
- if only round_rate() is available, we rely on luck unfortunately.

Fixes: 1c8e600440c7 ("clk: Add rate constraints to clocks")
Signed-off-by: Jerome Brunet <jbrunet@baylibre.com>
---
 drivers/clk/clk.c | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index cbfff541ec8a..8bc3d9d4c7ff 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -1921,6 +1921,7 @@ EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
 {
 	int ret = 0;
+	unsigned long old_min, old_max, rate;
 
 	if (!clk)
 		return 0;
@@ -1937,10 +1938,38 @@ int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
 	if (clk->exclusive_count)
 		clk_core_rate_unprotect(clk->core);
 
-	if (min != clk->min_rate || max != clk->max_rate) {
-		clk->min_rate = min;
-		clk->max_rate = max;
-		ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
+	/* Save the current values in case we need to rollback the change */
+	old_min = clk->min_rate;
+	old_max = clk->max_rate;
+	clk->min_rate = min;
+	clk->max_rate = max;
+
+	rate = clk_core_get_rate_nolock(clk->core);
+	if (rate < min || rate > max) {
+		/*
+		 * FIXME:
+		 * We are in bit of trouble here, current rate is outside the
+		 * the requested range. We are going try to request appropriate
+		 * range boundary but there is a catch. It may fail for the
+		 * usual reason (clock broken, clock protected, etc) but also
+		 * because:
+		 * - round_rate() was not favorable and fell on the wrong
+		 *   side of the boundary
+		 * - the determine_rate() callback does not really check for
+		 *   this corner case when determining the rate
+		 */
+
+		if (rate < min)
+			rate = min;
+		else
+			rate = max;
+
+		ret = clk_core_set_rate_nolock(clk->core, rate);
+		if (ret) {
+			/* rollback the changes */
+			clk->min_rate = old_min;
+			clk->max_rate = old_max;
+		}
 	}
 
 	if (clk->exclusive_count)
-- 
2.13.5

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v4 00/10] clk: implement clock rate protection mechanism Jerome Brunet <jbrunet@baylibre.com> - 2017-09-24 22:10 +0200
  [PATCH v4 04/10] clk: rework calls to round and determine rate callbacks Jerome Brunet <jbrunet@baylibre.com> - 2017-09-24 22:10 +0200
  [PATCH v4 08/10] clk: fix CLK_SET_RATE_GATE with clock rate protection Jerome Brunet <jbrunet@baylibre.com> - 2017-09-24 22:10 +0200
  [PATCH v4 02/10] clk: take the prepare lock out of clk_core_set_parent Jerome Brunet <jbrunet@baylibre.com> - 2017-09-24 22:10 +0200
  [PATCH v4 10/10] clk: fix set_rate_range when current rate is out of range Jerome Brunet <jbrunet@baylibre.com> - 2017-09-24 22:10 +0200
  [PATCH v4 06/10] clk: add clock protection mechanism to clk core Jerome Brunet <jbrunet@baylibre.com> - 2017-09-24 22:10 +0200
  [PATCH v4 05/10] clk: use round rate to bail out early in set_rate Jerome Brunet <jbrunet@baylibre.com> - 2017-09-24 22:10 +0200
  [PATCH v4 09/10] clk: add clk_rate_exclusive api Jerome Brunet <jbrunet@baylibre.com> - 2017-09-24 22:10 +0200

csiph-web