From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 686393939A4 for ; Fri, 16 Jan 2026 16:44:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768581864; cv=none; b=jBaoNUaQmhHZRVzfSoby2PMcdvJ8k+7xRwLgGVdBnmxMuD79TA6LucbXalvlucefuO1Mt31QN6+RAsGwWOKoPf8nlW6x4yVgAIOP2CqeUcOzDOh6gRf4t5CBHKgWS7VqcRvSc272YbaKWSBZejQnkEcn6/Hzh7vmBn0P6FxoP1o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768581864; c=relaxed/simple; bh=FCPpuR5pyXbwuEHy7hY3eKJRjE8DIkNpAA05+01sLW0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=BMrjgFx7AHHit1/g3UiE/xGRPmdfRzoh+YsecJE3Bu1+me5W09sygDRplrdZqAXhWrlKQ5zBND9dKy+1tNAWuLc5Kt6DeJz9AIxbwfHSdK8mLgYeFpzmheGGQd7Gcz84kjr8Y/IezCpIqvFLYAVTNKiyYJQHui7BHRw5lqFp/do= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Ow+O5S0j; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=iMN1yO38; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Ow+O5S0j"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="iMN1yO38" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1768581861; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pRCF+slWnms33snim/Zhjou6RcOl1iwqXMoZkaC/5tU=; b=Ow+O5S0jKBuXDOT3zg2YAE66KEYyRa39hbnkkUT3eTxDx61RVxGIZBviY0vhpSpaE4ps5s 8NY1nZUvZaelRe10D+lNC5xfzDjrtYBpGR2zbyNPG7QwFlzNWHq21rzbHe413mBxs17c88 nrtbLAS9lDCDTc182KzXhZ72lIDCNtg= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-288-SU13nUd1PnG2JkYhA9GYCQ-1; Fri, 16 Jan 2026 11:44:19 -0500 X-MC-Unique: SU13nUd1PnG2JkYhA9GYCQ-1 X-Mimecast-MFC-AGG-ID: SU13nUd1PnG2JkYhA9GYCQ_1768581859 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-890587d4e87so61915436d6.0 for ; Fri, 16 Jan 2026 08:44:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1768581859; x=1769186659; darn=vger.kernel.org; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=pRCF+slWnms33snim/Zhjou6RcOl1iwqXMoZkaC/5tU=; b=iMN1yO38ndocL8NX2tM9vjfqn2WDxPKnIVQKmF8W+/oiVhv4OGEnIskx7gYVD7+Xup +oXBNLdejETwFqIur/jl/IBqO2et+txG6gPd1YgK90NyHHGjV/oiUYWNiOBtlHyQHp/E cbgKKiNiZyIHziAd7l8kwc9jVfkUFvc5YW2LLwdQB1XoJbl79eng/US2mkb0OtopePJx 5S3zxFfDut8eZ5peX5EoB2DtiSIjJUz29L0kK9qOapDiB1kQB9FAznlSnbOdrmfw7iof CB52qYJVYcD25HK6RtApq55x68kZcx5PK/ptk8x12w3Sa/VUIN+w0L6knt3XJ+vZMtwy Bn5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768581859; x=1769186659; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=pRCF+slWnms33snim/Zhjou6RcOl1iwqXMoZkaC/5tU=; b=EJimZ5U45cFvXgnODOW8d3J6xTCT6Wur4NuQjZ9aYoCjQlH6iRHXn5vQ1bxdh5QfIq HRQvEC/00dPCdF28/zX3tH+x/CwRwI9mX9CEF2JM7zLzjJLhYAhasBI0NE7h2obQhdaH tZFwEcBAnEEjoB3Ep/Nt6nUlcLmWAAERsaVSfPwAr4IxdpMyUqY4uHNTJwvZ8loMONp3 jGDH9TtxHXIQSc3QNbT7XyierSLJV2oMw0jQUtgkYu918PAS38IyQXEuVc7++mLRMJ1h iQ1aQeE/l/Dtw1S6IfaoMTznfwhp9lOE9Y1En+WgCbc0x2Jhl0qwHzJM0rs4BD+teflI KLVA== X-Forwarded-Encrypted: i=1; AJvYcCVuUT0rORGVQwsXTMfV/fvvf+GwL4v5zg6yvBNDeRLljTKWs9AlyiV5QiV6/856pgL9UMY4ydxzbgeoU24=@vger.kernel.org X-Gm-Message-State: AOJu0Yzrn1SB34dFUR9aKhsnxXkrdi3Ywg6a6ZtcHwyGLZx9y1ri40da B/9TQUmvDG4tK//Imq0+RPONO/yqz+cYMMcdo1yufR/EAYV+eQESAnSijm4/ZYooxBKPl8pQW22 g+VLajsbhHWXFDokGGOUx/+Tr8qrkzGiPnGy3jVj7Qrf82Y3qweWfUGpnQRlTTNzflg== X-Gm-Gg: AY/fxX5CGLgh2UxQJhbxf8BqTL7w5sAM1sn+B6aoq1N8KXXK8qbkRDQ9Is0sJtq/2K2 hbNqOq1YbWWGdgzU7eSjW1LOlyj7a9p7tv7Wr7GTgAJGjxTV19ORNrOAU1jLXHmMYADExrIqjZX fxoFku+odMD+1FmMPJq5R6VJgyP7tZCL/waRdNKhyB4Db51Ihd6IjZFWYzmeUxpuNvU1pPkRr32 HL6IX3ghqhgdxB1u/cTCyKSmYUdbD2EVqDnAYlMSfYrFQVvoZrXXeOYe2roB0MB9NoO5z0+TCiZ b/cio06hAhyQH4YNya1djbN/5JbjQMc8HJwzm9T8dXThaxC5CTpdiF9K7o/Z9d+17HAEoG8BzTG 7VfkEVda74C9Ia5BUrQ8keMa5xXR06IRdC76VjncQ3AOL X-Received: by 2002:a05:6214:242f:b0:88a:529a:a530 with SMTP id 6a1803df08f44-8942dd109a7mr50043346d6.23.1768581859197; Fri, 16 Jan 2026 08:44:19 -0800 (PST) X-Received: by 2002:a05:6214:242f:b0:88a:529a:a530 with SMTP id 6a1803df08f44-8942dd109a7mr50042666d6.23.1768581858515; Fri, 16 Jan 2026 08:44:18 -0800 (PST) Received: from redhat.com (c-73-183-52-120.hsd1.pa.comcast.net. [73.183.52.120]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8942e6c6605sm26555976d6.44.2026.01.16.08.44.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Jan 2026 08:44:17 -0800 (PST) Date: Fri, 16 Jan 2026 11:44:16 -0500 From: Brian Masney To: Chuan Liu Cc: Michael Turquette , Stephen Boyd , linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] clk: Ensure correct consumer's rate boundaries Message-ID: References: <20260109-fix_error_setting_clk_rate_range-v1-1-bae0b40e870f@amlogic.com> <6a66f0f8-3fbf-46b7-9370-5768b1acd96b@amlogic.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="TfOEVYXj3aZnLEB/" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <6a66f0f8-3fbf-46b7-9370-5768b1acd96b@amlogic.com> User-Agent: Mutt/2.2.14 (2025-02-20) --TfOEVYXj3aZnLEB/ Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit Hi Chuan, i On Fri, Jan 16, 2026 at 05:32:39PM +0800, Chuan Liu wrote: > On 1/15/2026 9:01 PM, Brian Masney wrote: > > On Thu, Jan 15, 2026 at 10:37:55AM +0800, Chuan Liu wrote: > > > On 1/15/2026 9:30 AM, Brian Masney wrote: > > > > On Fri, Jan 09, 2026 at 11:24:22AM +0800, Chuan Liu via B4 Relay wrote: > > > > > From: Chuan Liu > > > > > > > > > > If we were to have two users of the same clock, doing something like: > > > > > > > > > > clk_set_rate_range(user1, 1000, 2000); > > > > > clk_set_rate_range(user2, 3000, 4000); > > > > > > > > > > Even when user2's call returns -EINVAL, the min_rate and max_rate of > > > > > user2 are still incorrectly updated. This causes subsequent calls by > > > > > user1 to fail when setting the clock rate, as clk_core_get_boundaries() > > > > > returns corrupted boundaries (min_rate = 3000, max_rate = 2000). > > > > > > > > > > To prevent this, clk_core_check_boundaries() now rollback to the old > > > > > boundaries when the check fails. > > > > > > > > > > Signed-off-by: Chuan Liu > > > > > --- > > > > > drivers/clk/clk.c | 8 ++++++-- > > > > > 1 file changed, 6 insertions(+), 2 deletions(-) > > > > > > > > > > diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c > > > > > index 85d2f2481acf..0dfb16bf3f31 100644 > > > > > --- a/drivers/clk/clk.c > > > > > +++ b/drivers/clk/clk.c > > > > > @@ -2710,13 +2710,17 @@ static int clk_set_rate_range_nolock(struct clk *clk, > > > > > */ > > > > > rate = clamp(rate, min, max); > > > > > ret = clk_core_set_rate_nolock(clk->core, rate); > > > > > + > > > > > +out: > > > > > if (ret) { > > > > > - /* rollback the changes */ > > > > > + /* > > > > > + * Rollback the consumer’s old boundaries if check_boundaries or > > > > > + * set_rate fails. > > > > > + */ > > > > > clk->min_rate = old_min; > > > > > clk->max_rate = old_max; > > > > > } > > > > > > > > > > -out: > > > > > if (clk->exclusive_count) > > > > > clk_core_rate_protect(clk->core); > > > > > > > > This looks correct to me. Just a quick question though to possibly > > > > simplify this further. Currently clk_set_rate_range_nolock() has the > > > > following code: > > > > > > > > /* 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; > > > > > > > > if (!clk_core_check_boundaries(clk->core, min, max)) { > > > > ret = -EINVAL; > > > > goto out; > > > > } > > > > > > > > Since clk_core_check_boundaries() is a readonly operation, what do you > > > > think about moving clk_core_check_boundaries above the code that saves the > > > > previous values? That way we only need to rollback in the case where > > > > set_rate() fails. > > > > > > > > > > Perhaps it would be more appropriate to move the clk_core_check_boundaries() > > > check before saving the previous values, like this: > > > > > > if (!clk_core_check_boundaries(clk->core, min, max)) { > > > ret = -EINVAL; > > > goto out; > > > } > > > > > > /* 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; > > > > Yes, that's what I had in mind. > > > > > The changes in this patch are intended to avoid altering the original driver > > > execution flow, while making the minimal modification to fix the issue where > > > the range is incorrectly assigned. > > > > It's ultimately up to Stephen what he wants to take. I personally have a > > slight preference to the approach above, however I don't have a strong > > opinion about it. I'm just calling this out to help with reviews. > > > > The one thing that Stephen will want though is kunit tests for this > > since it changes the clk core. There's already a bunch of kunit tests in > > drivers/clk/clk_test.c. Feel free to reach out to me if you need help > > writing a new test. > > > > Thank you for the reminder. Stephen previously pointed out the need to > improve the kunit tests, and I will take this into account in future > submissions. > > I would greatly appreciate it if you could help enhance the kunit tests for > this particular test case.:) I attached a patch that adds a kunit test that you can apply with 'git am'. Try it with, and without your patch to see how it operates with and without your fix. If you like it, then feel free to include it in the next version of this patch series. Brian --TfOEVYXj3aZnLEB/ Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=0001-clk-test-introduce-test-to-test-the-rollback-of-cons.patch >From 2367a5e238f0d26ae38510fb0fa2eb33a43cc046 Mon Sep 17 00:00:00 2001 From: Brian Masney Date: Fri, 16 Jan 2026 11:21:21 -0500 Subject: [PATCH] clk: test: introduce test to test the rollback of consumer's rate boundaries Content-type: text/plain Add a kunit test to ensure that when a second consumer requests an invalid rate range that the aggregated range is rolled back correctly, and the first consumer is not impacted. Signed-off-by: Brian Masney --- drivers/clk/clk_test.c | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/drivers/clk/clk_test.c b/drivers/clk/clk_test.c index a268d7b5d4cb..afffa182559f 100644 --- a/drivers/clk/clk_test.c +++ b/drivers/clk/clk_test.c @@ -1500,6 +1500,58 @@ static void clk_range_test_multiple_disjoints_range(struct kunit *test) clk_put(user1); } +static void clk_range_test_consumer_boundaries_rollback(struct kunit *test) +{ + struct clk_dummy_context *ctx = test->priv; + struct clk_hw *hw = &ctx->hw; + unsigned long rate, min, max; + struct clk *user1, *user2; + int ret; + + user1 = clk_hw_get_clk(hw, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1); + + user2 = clk_hw_get_clk(hw, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2); + + /* user1 sets a range */ + ret = clk_set_rate_range(user1, 1000, 2000); + KUNIT_ASSERT_EQ(test, ret, 0); + + clk_hw_get_rate_range(hw, &min, &max); + KUNIT_ASSERT_EQ(test, min, 1000); + KUNIT_ASSERT_EQ(test, max, 2000); + + /* Verify user1 can set a rate within its range */ + ret = clk_set_rate(user1, 1500); + KUNIT_ASSERT_EQ(test, ret, 0); + rate = clk_get_rate(user1); + KUNIT_EXPECT_EQ(test, rate, 1500); + + /* user2 attempts to set a disjoint range [3000, 4000] - should fail */ + ret = clk_set_rate_range(user2, 3000, 4000); + KUNIT_EXPECT_EQ(test, ret, -EINVAL); + + /* + * After the failed call, user1 should still be able to set rates + * within its original range. This verifies that user2's boundaries + * were correctly rolled back and didn't corrupt the aggregated + * boundaries. + */ + + clk_hw_get_rate_range(hw, &min, &max); + KUNIT_EXPECT_EQ(test, min, 1000); + KUNIT_EXPECT_EQ(test, max, 2000); + + ret = clk_set_rate(user1, 1800); + KUNIT_EXPECT_EQ(test, ret, 0); + rate = clk_get_rate(user1); + KUNIT_EXPECT_EQ(test, rate, 1800); + + clk_put(user2); + clk_put(user1); +} + /* * Test that if our clock has some boundaries and we try to round a rate * lower than the minimum, the returned rate will be within range. @@ -1738,6 +1790,7 @@ static struct kunit_case clk_range_test_cases[] = { KUNIT_CASE(clk_range_test_set_range), KUNIT_CASE(clk_range_test_set_range_invalid), KUNIT_CASE(clk_range_test_multiple_disjoints_range), + KUNIT_CASE(clk_range_test_consumer_boundaries_rollback), KUNIT_CASE(clk_range_test_set_range_round_rate_lower), KUNIT_CASE(clk_range_test_set_range_set_rate_lower), KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_lower), -- 2.52.0 --TfOEVYXj3aZnLEB/--