lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230717-pll-mipi_set_rate_parent-v4-3-04acf1d39765@oltmanns.dev>
Date:   Mon, 17 Jul 2023 15:34:27 +0200
From:   Frank Oltmanns <frank@...manns.dev>
To:     Maxime Ripard <mripard@...nel.org>,
        Michael Turquette <mturquette@...libre.com>,
        Stephen Boyd <sboyd@...nel.org>, Chen-Yu Tsai <wens@...e.org>,
        Jernej Skrabec <jernej.skrabec@...il.com>,
        Samuel Holland <samuel@...lland.org>,
        Andre Przywara <andre.przywara@....com>,
        Roman Beranek <me@...y.cz>
Cc:     linux-clk@...r.kernel.org, linux-arm-kernel@...ts.infradead.org,
        linux-sunxi@...ts.linux.dev, linux-kernel@...r.kernel.org,
        Frank Oltmanns <frank@...manns.dev>
Subject: [PATCH v4 03/11] clk: sunxi-ng: nkm: Improve determine rate when
 setting parent

Make the SET_PARENT_RATE flag independent of the parents round_rate or
determine_rate implementation.

Currently, the algorithm for ccu_nkm_find_best_with_parent_adj simply
calculates the optimal parent rate as
    (1) parent = rate * m / (n * k)

Due to integer division (1) might return a parent rate that is too low.
So using this value for asking the parent for a rate it supports via
clk_hw_round_rate causes problems on
 a) parents that only support finding rates that are lower than the
    requested rate - which is the default for sunxi-ng ccu's.
 b) parents that incidentally also support the truncated rate.

In those cases ccu_nkm_determine_rate might return A' when A is
requested and A'' when rate A' is requested.

Prevent this by trying to find a parent rate so that
    (2) _rate = parent * n * k / m
matches the requested rate exactly, if possible.

Background:
===========
determine_rate may be called multiple times by the clk framework when
setting a clock's rate. But the clk framework expects that the values
determine_rate returns (i.e. the rate and parent_rate) are consistent
with previous calls.

Specifically, clock's have to ensure that if determine_rate is called
with requested rate A and the best rate it can find is A', it must also
return A' when called with requested rate A'.

Signed-off-by: Frank Oltmanns <frank@...manns.dev>
---
 drivers/clk/sunxi-ng/ccu_nkm.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/sunxi-ng/ccu_nkm.c b/drivers/clk/sunxi-ng/ccu_nkm.c
index 750e2b8da24b..793160bc2d47 100644
--- a/drivers/clk/sunxi-ng/ccu_nkm.c
+++ b/drivers/clk/sunxi-ng/ccu_nkm.c
@@ -17,6 +17,27 @@ struct _ccu_nkm {
 	unsigned long	m, min_m, max_m;
 };
 
+/*
+ * Calculate the optimal parent when determining the nkm clock's rate.
+ *
+ * This function is used when the nkm clock supports setting the parent rate and ensures that the
+ * determine_rate function returns consistent values. I.e., when determine rate is called for rate A
+ * and the best rate it can find is A', this function ensures that determine_rate will also return
+ * A' when A' is requested.
+ */
+static unsigned long ccu_nkm_optimal_parent_rate(unsigned long rate, unsigned long n,
+				 unsigned long k, unsigned long m)
+{
+	unsigned long _rate, parent;
+
+	parent = DIV_ROUND_UP(rate * m, n * k);
+
+	_rate = parent * n * k / m;
+	if (_rate > rate)
+		parent = rate * m / (n * k);
+	return parent;
+}
+
 static unsigned long ccu_nkm_find_best_with_parent_adj(struct clk_hw *phw, struct _ccu_nkm *nkm,
 						       unsigned long *parent, unsigned long rate)
 {
@@ -29,7 +50,8 @@ static unsigned long ccu_nkm_find_best_with_parent_adj(struct clk_hw *phw, struc
 			for (_m = nkm->min_m; _m <= nkm->max_m; _m++) {
 				unsigned long tmp_rate;
 
-				tmp_parent = clk_hw_round_rate(phw, rate * _m / (_n * _k));
+				tmp_parent = ccu_nkm_optimal_parent_rate(rate, _n, _k, _m);
+				tmp_parent = clk_hw_round_rate(phw, tmp_parent);
 
 				tmp_rate = tmp_parent * _n * _k / _m;
 				if (tmp_rate > rate)

-- 
2.41.0

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ