From nobody Sun May 10 20:34:12 2026 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 12620C433F5 for ; Sun, 24 Apr 2022 11:47:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239381AbiDXLuf (ORCPT ); Sun, 24 Apr 2022 07:50:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239343AbiDXLuW (ORCPT ); Sun, 24 Apr 2022 07:50:22 -0400 Received: from angie.orcam.me.uk (angie.orcam.me.uk [78.133.224.34]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 87CDD1344F6 for ; Sun, 24 Apr 2022 04:47:21 -0700 (PDT) Received: by angie.orcam.me.uk (Postfix, from userid 500) id BF91792009D; Sun, 24 Apr 2022 13:47:20 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by angie.orcam.me.uk (Postfix) with ESMTP id B7C5792009B; Sun, 24 Apr 2022 12:47:20 +0100 (BST) Date: Sun, 24 Apr 2022 12:47:20 +0100 (BST) From: "Maciej W. Rozycki" To: John Stultz , Thomas Gleixner , Stephen Boyd , Russell King , Ingo Molnar cc: linux-kernel@vger.kernel.org Subject: [PATCH 1/3] sched_clock: Round the frequency reported to nearest rather than down In-Reply-To: Message-ID: References: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We currently round the frequency reported for clock sources down, which=20 gives misleading figures, e.g.: I/O ASIC clock frequency 24999480Hz clocksource: dec-ioasic: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_= ns: 76452008078 ns sched_clock: 32 bits at 24MHz, resolution 40ns, wraps every 85901132779ns MIPS counter frequency 59998512Hz clocksource: MIPS: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 31= 855130776 ns sched_clock: 32 bits at 59MHz, resolution 16ns, wraps every 35792281591ns Rounding to nearest seems more adequate: I/O ASIC clock frequency 24999664Hz clocksource: dec-ioasic: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_= ns: 76451445358 ns sched_clock: 32 bits at 25MHz, resolution 40ns, wraps every 85900499947ns MIPS counter frequency 59999728Hz clocksource: MIPS: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 31= 854485176 ns sched_clock: 32 bits at 60MHz, resolution 16ns, wraps every 35791556599ns Signed-off-by: Maciej W. Rozycki Fixes: 112f38a4a316 ("ARM: sched_clock: provide common infrastructure for s= ched_clock()") Acked-by: John Stultz --- kernel/time/sched_clock.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) linux-sched-clock-rate-round.diff Index: linux-macro/kernel/time/sched_clock.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- linux-macro.orig/kernel/time/sched_clock.c +++ linux-macro/kernel/time/sched_clock.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -199,11 +200,11 @@ sched_clock_register(u64 (*read)(void), =20 r =3D rate; if (r >=3D 4000000) { - r /=3D 1000000; + r =3D DIV_ROUND_CLOSEST(r, 1000000); r_unit =3D 'M'; } else { if (r >=3D 1000) { - r /=3D 1000; + r =3D DIV_ROUND_CLOSEST(r, 1000); r_unit =3D 'k'; } else { r_unit =3D ' '; From nobody Sun May 10 20:34:12 2026 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 69C2BC433F5 for ; Sun, 24 Apr 2022 11:47:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236184AbiDXLuj (ORCPT ); Sun, 24 Apr 2022 07:50:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239359AbiDXLu1 (ORCPT ); Sun, 24 Apr 2022 07:50:27 -0400 Received: from angie.orcam.me.uk (angie.orcam.me.uk [IPv6:2001:4190:8020::34]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C7D60135B0D for ; Sun, 24 Apr 2022 04:47:26 -0700 (PDT) Received: by angie.orcam.me.uk (Postfix, from userid 500) id 2BB3292009E; Sun, 24 Apr 2022 13:47:26 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by angie.orcam.me.uk (Postfix) with ESMTP id 25EBA92009B; Sun, 24 Apr 2022 12:47:26 +0100 (BST) Date: Sun, 24 Apr 2022 12:47:26 +0100 (BST) From: "Maciej W. Rozycki" To: John Stultz , Thomas Gleixner , Stephen Boyd , Russell King , Ingo Molnar cc: linux-kernel@vger.kernel.org Subject: [PATCH 2/3] sched_clock: Use Hz as the unit for clock rate reporting below 4kHz In-Reply-To: Message-ID: References: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We have always used kHz as the unit for clock rates reported between=20 1MHz (inclusive) and 4MHz (exclusive), e.g.: riscv_timer_init_dt: Registering clocksource cpuid [0] hartid [1] clocksource: riscv_clocksource: mask: 0xffffffffffffffff max_cycles: 0x1d85= 4df40, max_idle_ns: 3526361616960 ns sched_clock: 64 bits at 1000kHz, resolution 1000ns, wraps every 21990232555= 00ns This reduces the amount of data lost due to rounding, but hasn't been=20 replicated for the kHz range when support was added for proper reporting=20 of sub-kHz clock rates. Take the same approach then for rates between=20 1kHz (inclusive) and 4kHz (exclusive), also for consistency. Signed-off-by: Maciej W. Rozycki Fixes: 2f0778afac79 ("ARM: 7205/2: sched_clock: allow sched_clock to be sel= ected at runtime") --- Russell, Please correct me if I am wrong with my guess for the origin of the 4MHz=20 boundary as there's no mention of it with your commit 112f38a4a316 ("ARM:=20 sched_clock: provide common infrastructure for sched_clock()"), which is=20 where this code has come from. Maciej --- kernel/time/sched_clock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) linux-sched-clock-rate-4khz.diff Index: linux-macro/kernel/time/sched_clock.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- linux-macro.orig/kernel/time/sched_clock.c +++ linux-macro/kernel/time/sched_clock.c @@ -203,7 +203,7 @@ sched_clock_register(u64 (*read)(void), r =3D DIV_ROUND_CLOSEST(r, 1000000); r_unit =3D 'M'; } else { - if (r >=3D 1000) { + if (r >=3D 4000) { r =3D DIV_ROUND_CLOSEST(r, 1000); r_unit =3D 'k'; } else { From nobody Sun May 10 20:34:12 2026 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 134AAC433F5 for ; Sun, 24 Apr 2022 11:47:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239407AbiDXLup (ORCPT ); Sun, 24 Apr 2022 07:50:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239369AbiDXLuc (ORCPT ); Sun, 24 Apr 2022 07:50:32 -0400 Received: from angie.orcam.me.uk (angie.orcam.me.uk [IPv6:2001:4190:8020::34]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7AEF8136E36 for ; Sun, 24 Apr 2022 04:47:31 -0700 (PDT) Received: by angie.orcam.me.uk (Postfix, from userid 500) id D4AA79200B3; Sun, 24 Apr 2022 13:47:30 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by angie.orcam.me.uk (Postfix) with ESMTP id CEE6592009B; Sun, 24 Apr 2022 12:47:30 +0100 (BST) Date: Sun, 24 Apr 2022 12:47:30 +0100 (BST) From: "Maciej W. Rozycki" To: John Stultz , Thomas Gleixner , Stephen Boyd , Russell King , Ingo Molnar cc: linux-kernel@vger.kernel.org Subject: [PATCH 3/3] sched_clock: Fix formatting of frequency reporting code In-Reply-To: Message-ID: References: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use flat rather than nested indentation for chained else/if clauses as=20 per coding-style.rst: if (x =3D=3D y) { .. } else if (x > y) { ... } else { .... } This also improves readability. Signed-off-by: Maciej W. Rozycki Fixes: 32fea568aec5b ("timers, sched/clock: Clean up the code a bit") Acked-by: John Stultz --- Hi, I guess this got broken with 32fea568aec5b by mistake. Maciej --- kernel/time/sched_clock.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) linux-sched-clock-rate-cond.diff Index: linux-macro/kernel/time/sched_clock.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- linux-macro.orig/kernel/time/sched_clock.c +++ linux-macro/kernel/time/sched_clock.c @@ -202,13 +202,11 @@ sched_clock_register(u64 (*read)(void), if (r >=3D 4000000) { r =3D DIV_ROUND_CLOSEST(r, 1000000); r_unit =3D 'M'; + } else if (r >=3D 4000) { + r =3D DIV_ROUND_CLOSEST(r, 1000); + r_unit =3D 'k'; } else { - if (r >=3D 4000) { - r =3D DIV_ROUND_CLOSEST(r, 1000); - r_unit =3D 'k'; - } else { - r_unit =3D ' '; - } + r_unit =3D ' '; } =20 /* Calculate the ns resolution of this counter */