From patchwork Tue Oct 24 16:18:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Reichel X-Patchwork-Id: 13435098 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EFF5BC25B48 for ; Tue, 24 Oct 2023 16:19:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234699AbjJXQTk (ORCPT ); Tue, 24 Oct 2023 12:19:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233585AbjJXQTi (ORCPT ); Tue, 24 Oct 2023 12:19:38 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B39E111; Tue, 24 Oct 2023 09:19:36 -0700 (PDT) Received: from jupiter.universe (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sre) by madras.collabora.co.uk (Postfix) with ESMTPSA id 264DD66072A2; Tue, 24 Oct 2023 17:19:35 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1698164375; bh=SqKf4LNwvpIYJ9bi8s2HSgXa96OgNa0HbeVAslfV1tU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A2hGpIqV1ZIGRSd6+nUApqHDqsXu2jtpQy8gHMlDfLtCXGef8ux0LOtJzTL4eLJu2 DUMaQUGSKyHGUHrOvPJjQgrhf4TD/pV9lYOP0rursa+TUzxci2toviIHXet9ksthcq hQWjVEeC4WAMatPGuv3DUauC5Q/LsNcEW9MCtVp9Y6JLS1ZUNV7klUwT8MQTe6IrDx w1z244Fv0hXQ/z3SmxPaRtFOXFIx6aMuTUzJ6SAVx/BbgDZHcdVh+qfxMzJwdKFoGo gAjeXhoZdk/zOXs1qMzCcLtDvfXn1gfJllO7jvBePlPNqf4hhc1VTbjfO3YhdAg1FG Z9ZOc+kgg7ZaA== Received: by jupiter.universe (Postfix, from userid 1000) id 9BC2B482AD8; Tue, 24 Oct 2023 18:19:32 +0200 (CEST) From: Sebastian Reichel To: Andy Shevchenko , Michael Turquette , Stephen Boyd , David Laight , linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Vasily Gorbik , Niklas Schnelle , Linus Torvalds , Sebastian Reichel , kernel@collabora.com Subject: [PATCH v4 1/3] math.h: add DIV_ROUND_UP_NO_OVERFLOW Date: Tue, 24 Oct 2023 18:18:15 +0200 Message-ID: <20231024161931.78567-2-sebastian.reichel@collabora.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231024161931.78567-1-sebastian.reichel@collabora.com> References: <20231024161931.78567-1-sebastian.reichel@collabora.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-clk@vger.kernel.org Add a new DIV_ROUND_UP helper, which cannot overflow when big numbers are being used. Signed-off-by: Sebastian Reichel --- include/linux/math.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/linux/math.h b/include/linux/math.h index dd4152711de7..f80bfb375ab9 100644 --- a/include/linux/math.h +++ b/include/linux/math.h @@ -36,6 +36,17 @@ #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP +/** + * DIV_ROUND_UP_NO_OVERFLOW - divide two numbers and always round up + * @n: numerator / dividend + * @d: denominator / divisor + * + * This functions does the same as DIV_ROUND_UP, but internally uses a + * division and a modulo operation instead of math tricks. This way it + * avoids overflowing when handling big numbers. + */ +#define DIV_ROUND_UP_NO_OVERFLOW(n, d) (((n) / (d)) + !!((n) % (d))) + #define DIV_ROUND_DOWN_ULL(ll, d) \ ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) From patchwork Tue Oct 24 16:18:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Reichel X-Patchwork-Id: 13435100 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 81783C07545 for ; Tue, 24 Oct 2023 16:19:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234717AbjJXQTl (ORCPT ); Tue, 24 Oct 2023 12:19:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234655AbjJXQTj (ORCPT ); Tue, 24 Oct 2023 12:19:39 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5DA2A1; Tue, 24 Oct 2023 09:19:36 -0700 (PDT) Received: from jupiter.universe (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sre) by madras.collabora.co.uk (Postfix) with ESMTPSA id 3494B6607324; Tue, 24 Oct 2023 17:19:35 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1698164375; bh=r5h0r4C6WuS6YaXUWi5dy7ZxeGnh1BcenNgCKge4E2c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=N89hTeneZ72qS/H3QXeA1c54E4oF+hYpxHnrkwPhhj1gpx2GKmaY+Q3laZp5tOykv isKa9er62kBNVNG2Ud0EUSwu8bpL0FUKr2QcStvnqpnJ2Ymp4pdUMTdsqOcGCjt0If qBdO32Pef0JavTxHUCH+5VsrfJeiVH3ti7O+p3F6zUAkvSr7af3rJpKMrWHjbqLeNe Y4HltnzAeOmX23Vk1MYdfGRa65UOkWsn8025R1wvQNuq+tLLDbOHJAVeqL1/os7zRM UFpeG9Hs2gJhhcqYeZLMttyCDuAEY6KNtyK8Px85xlbwvsfx2U3Rtxe12+D6sHr6sC dre61Yd6qyOUQ== Received: by jupiter.universe (Postfix, from userid 1000) id 9DAF3482ADB; Tue, 24 Oct 2023 18:19:32 +0200 (CEST) From: Sebastian Reichel To: Andy Shevchenko , Michael Turquette , Stephen Boyd , David Laight , linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Vasily Gorbik , Niklas Schnelle , Linus Torvalds , Sebastian Reichel , kernel@collabora.com Subject: [PATCH v4 2/3] clk: divider: Fix divisor masking on 64 bit platforms Date: Tue, 24 Oct 2023 18:18:16 +0200 Message-ID: <20231024161931.78567-3-sebastian.reichel@collabora.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231024161931.78567-1-sebastian.reichel@collabora.com> References: <20231024161931.78567-1-sebastian.reichel@collabora.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-clk@vger.kernel.org The clock framework handles clock rates as "unsigned long", so u32 on 32-bit architectures and u64 on 64-bit architectures. The current code casts the dividend to u64 on 32-bit to avoid a potential overflow. For example DIV_ROUND_UP(3000000000, 1500000000) = (3.0G + 1.5G - 1) / 1.5G = = OVERFLOW / 1.5G, which has been introduced in commit 9556f9dad8f5 ("clk: divider: handle integer overflow when dividing large clock rates"). On 64 bit platforms this masks the divisor, so that only the lower 32 bit are used. Thus requesting a frequency >= 4.3GHz results in incorrect values. For example requesting 4300000000 (4.3 GHz) will effectively request ca. 5 MHz. Requesting clk_round_rate(clk, ULONG_MAX) is a bit of a special case, since that still returns correct values as long as the parent clock is below 8.5 GHz. Fix this by switching to DIV_ROUND_UP_NO_OVERFLOW, which cannot overflow. This avoids any requirements on the arguments (except that divisor should not be 0 obviously). Signed-off-by: Sebastian Reichel --- drivers/clk/clk-divider.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c index a2c2b5203b0a..94b4fb66a60f 100644 --- a/drivers/clk/clk-divider.c +++ b/drivers/clk/clk-divider.c @@ -220,7 +220,7 @@ static int _div_round_up(const struct clk_div_table *table, unsigned long parent_rate, unsigned long rate, unsigned long flags) { - int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); + int div = DIV_ROUND_UP_NO_OVERFLOW(parent_rate, rate); if (flags & CLK_DIVIDER_POWER_OF_TWO) div = __roundup_pow_of_two(div); @@ -237,7 +237,7 @@ static int _div_round_closest(const struct clk_div_table *table, int up, down; unsigned long up_rate, down_rate; - up = DIV_ROUND_UP_ULL((u64)parent_rate, rate); + up = DIV_ROUND_UP_NO_OVERFLOW(parent_rate, rate); down = parent_rate / rate; if (flags & CLK_DIVIDER_POWER_OF_TWO) { @@ -473,7 +473,7 @@ int divider_get_val(unsigned long rate, unsigned long parent_rate, { unsigned int div, value; - div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); + div = DIV_ROUND_UP_NO_OVERFLOW(parent_rate, rate); if (!_is_valid_div(table, div, flags)) return -EINVAL; From patchwork Tue Oct 24 16:18:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Reichel X-Patchwork-Id: 13435099 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16FC0C25B6E for ; Tue, 24 Oct 2023 16:19:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343587AbjJXQTm (ORCPT ); Tue, 24 Oct 2023 12:19:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234658AbjJXQTj (ORCPT ); Tue, 24 Oct 2023 12:19:39 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5E7B83; Tue, 24 Oct 2023 09:19:36 -0700 (PDT) Received: from jupiter.universe (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sre) by madras.collabora.co.uk (Postfix) with ESMTPSA id 3B62B6607328; Tue, 24 Oct 2023 17:19:35 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1698164375; bh=x58O7/yeMCAwTvVvYfypW/Y5RY/B1MgCHBqd2HZdSiM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=baHTv0Q9SNhDPpjT/nA2urEKIhWdb+u2YwV8EXqJfF8sbpAiX9q/0UlN7Wxo5LB36 d0WqNO17GD/3nVB9KNG+S1Nb3rfRJKUvrdOyEBd9MHOPCTsDuoCOv45Z2dNHcq1dCN sBmYuLFuAMgT/iVJlXpbIAST4i2ejJEuC2pbMXAEiYgMK9tkFQMa5Uky/LcDKEq8Xb Ln9e8eAOWvUyT4CA9/XpNxm0SPgXR5rlOrAu5TeIZGY3D8s6LEKJeG6LjxBxJRw6rp /WcSXWRJdTkmv6gx8tlVU9X49FCvAwhI+uvWy3t+//Op6qU8pc/6cevg8QtsKzdBX2 P2Jpfo3Md/0Dg== Received: by jupiter.universe (Postfix, from userid 1000) id 9FA15482ADD; Tue, 24 Oct 2023 18:19:32 +0200 (CEST) From: Sebastian Reichel To: Andy Shevchenko , Michael Turquette , Stephen Boyd , David Laight , linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Vasily Gorbik , Niklas Schnelle , Linus Torvalds , Sebastian Reichel , kernel@collabora.com, Andy Shevchenko Subject: [PATCH v4 3/3] clk: composite: replace open-coded abs_diff() Date: Tue, 24 Oct 2023 18:18:17 +0200 Message-ID: <20231024161931.78567-4-sebastian.reichel@collabora.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231024161931.78567-1-sebastian.reichel@collabora.com> References: <20231024161931.78567-1-sebastian.reichel@collabora.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-clk@vger.kernel.org Replace the open coded abs_diff() with the existing helper function. Suggested-by: Andy Shevchenko Signed-off-by: Sebastian Reichel Reviewed-by: Andy Shevchenko --- drivers/clk/clk-composite.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/drivers/clk/clk-composite.c b/drivers/clk/clk-composite.c index 66759fe28fad..478a4e594336 100644 --- a/drivers/clk/clk-composite.c +++ b/drivers/clk/clk-composite.c @@ -6,6 +6,7 @@ #include #include #include +#include #include static u8 clk_composite_get_parent(struct clk_hw *hw) @@ -119,10 +120,7 @@ static int clk_composite_determine_rate(struct clk_hw *hw, if (ret) continue; - if (req->rate >= tmp_req.rate) - rate_diff = req->rate - tmp_req.rate; - else - rate_diff = tmp_req.rate - req->rate; + rate_diff = abs_diff(req->rate, tmp_req.rate); if (!rate_diff || !req->best_parent_hw || best_rate_diff > rate_diff) {