2022-04-25 07:52:38

by Maciej W. Rozycki

[permalink] [raw]
Subject: [PATCH 0/3] sched_clock: Fixes for frequency reporting

Hi,

Hmm, kernel/time/sched_clock.c lacks a MAINTAINERS entry, but I guess it
goes along with either high-resolution timers or clocksource core. Please
advise otherwise.

In the course of reviewing the MIPS part of the `random_get_entropy'
patch set recently posted I have noticed the odd rounding mode used for
clock source frequency reporting, and then upon inspecting code involved
I've come across a couple of further small issues.

This patch set addresses these issues. Please see individual change
descriptions for details.

Maciej


2022-04-25 08:01:52

by Maciej W. Rozycki

[permalink] [raw]
Subject: [PATCH 3/3] sched_clock: Fix formatting of frequency reporting code

Use flat rather than nested indentation for chained else/if clauses as
per coding-style.rst:

if (x == y) {
..
} else if (x > y) {
...
} else {
....
}

This also improves readability.

Signed-off-by: Maciej W. Rozycki <[email protected]>
Fixes: 32fea568aec5b ("timers, sched/clock: Clean up the code a bit")
---
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
===================================================================
--- 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 >= 4000000) {
r = DIV_ROUND_CLOSEST(r, 1000000);
r_unit = 'M';
+ } else if (r >= 4000) {
+ r = DIV_ROUND_CLOSEST(r, 1000);
+ r_unit = 'k';
} else {
- if (r >= 4000) {
- r = DIV_ROUND_CLOSEST(r, 1000);
- r_unit = 'k';
- } else {
- r_unit = ' ';
- }
+ r_unit = ' ';
}

/* Calculate the ns resolution of this counter */

2022-04-25 08:02:44

by Maciej W. Rozycki

[permalink] [raw]
Subject: [PATCH 1/3] sched_clock: Round the frequency reported to nearest rather than down

We currently round the frequency reported for clock sources down, which
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: 31855130776 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: 31854485176 ns
sched_clock: 32 bits at 60MHz, resolution 16ns, wraps every 35791556599ns

Signed-off-by: Maciej W. Rozycki <[email protected]>
Fixes: 112f38a4a316 ("ARM: sched_clock: provide common infrastructure for sched_clock()")
---
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
===================================================================
--- linux-macro.orig/kernel/time/sched_clock.c
+++ linux-macro/kernel/time/sched_clock.c
@@ -8,6 +8,7 @@
#include <linux/jiffies.h>
#include <linux/ktime.h>
#include <linux/kernel.h>
+#include <linux/math.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/sched/clock.h>
@@ -199,11 +200,11 @@ sched_clock_register(u64 (*read)(void),

r = rate;
if (r >= 4000000) {
- r /= 1000000;
+ r = DIV_ROUND_CLOSEST(r, 1000000);
r_unit = 'M';
} else {
if (r >= 1000) {
- r /= 1000;
+ r = DIV_ROUND_CLOSEST(r, 1000);
r_unit = 'k';
} else {
r_unit = ' ';

2022-04-27 09:54:28

by John Stultz

[permalink] [raw]
Subject: Re: [PATCH 3/3] sched_clock: Fix formatting of frequency reporting code

On Sun, Apr 24, 2022 at 4:47 AM Maciej W. Rozycki <[email protected]> wrote:
>
> Use flat rather than nested indentation for chained else/if clauses as
> per coding-style.rst:
>
> if (x == y) {
> ..
> } else if (x > y) {
> ...
> } else {
> ....
> }
>
> This also improves readability.
>
> Signed-off-by: Maciej W. Rozycki <[email protected]>
> Fixes: 32fea568aec5b ("timers, sched/clock: Clean up the code a bit")

This patch seems fine to me. Though as Ingo was the one to introduce
the change, his style preference may override in this case.

Acked-by: John Stultz <[email protected]>

thanks
-john

2022-04-27 11:10:44

by John Stultz

[permalink] [raw]
Subject: Re: [PATCH 1/3] sched_clock: Round the frequency reported to nearest rather than down

On Sun, Apr 24, 2022 at 4:47 AM Maciej W. Rozycki <[email protected]> wrote:
>
> We currently round the frequency reported for clock sources down, which
> 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: 31855130776 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: 31854485176 ns
> sched_clock: 32 bits at 60MHz, resolution 16ns, wraps every 35791556599ns
>
> Signed-off-by: Maciej W. Rozycki <[email protected]>
> Fixes: 112f38a4a316 ("ARM: sched_clock: provide common infrastructure for sched_clock()")
> ---

This seems sane to me.

Acked-by: John Stultz <[email protected]>

thanks
-john

Subject: [tip: timers/core] time/sched_clock: Fix formatting of frequency reporting code

The following commit has been merged into the timers/core branch of tip:

Commit-ID: f4b62e1e1137507268c2c63dc4e6da279dc58e9f
Gitweb: https://git.kernel.org/tip/f4b62e1e1137507268c2c63dc4e6da279dc58e9f
Author: Maciej W. Rozycki <[email protected]>
AuthorDate: Sun, 24 Apr 2022 12:47:30 +01:00
Committer: Thomas Gleixner <[email protected]>
CommitterDate: Mon, 02 May 2022 14:29:04 +02:00

time/sched_clock: Fix formatting of frequency reporting code

Use flat rather than nested indentation for chained else/if clauses as
per coding-style.rst:

if (x == y) {
..
} else if (x > y) {
...
} else {
....
}

This also improves readability.

Signed-off-by: Maciej W. Rozycki <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Acked-by: John Stultz <[email protected]>
Link: https://lore.kernel.org/r/[email protected]

---
kernel/time/sched_clock.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/kernel/time/sched_clock.c b/kernel/time/sched_clock.c
index 4a95c0b..8464c5a 100644
--- a/kernel/time/sched_clock.c
+++ b/kernel/time/sched_clock.c
@@ -202,13 +202,11 @@ sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
if (r >= 4000000) {
r = DIV_ROUND_CLOSEST(r, 1000000);
r_unit = 'M';
+ } else if (r >= 4000) {
+ r = DIV_ROUND_CLOSEST(r, 1000);
+ r_unit = 'k';
} else {
- if (r >= 4000) {
- r = DIV_ROUND_CLOSEST(r, 1000);
- r_unit = 'k';
- } else {
- r_unit = ' ';
- }
+ r_unit = ' ';
}

/* Calculate the ns resolution of this counter */

Subject: [tip: timers/core] time/sched_clock: Round the frequency reported to nearest rather than down

The following commit has been merged into the timers/core branch of tip:

Commit-ID: 92067440f1311dfa4d77b57a9da6b3706f5da32e
Gitweb: https://git.kernel.org/tip/92067440f1311dfa4d77b57a9da6b3706f5da32e
Author: Maciej W. Rozycki <[email protected]>
AuthorDate: Sun, 24 Apr 2022 12:47:20 +01:00
Committer: Thomas Gleixner <[email protected]>
CommitterDate: Mon, 02 May 2022 14:29:04 +02:00

time/sched_clock: Round the frequency reported to nearest rather than down

The frequency reported for clock sources are rounded down, which gives
misleading figures, e.g.:

I/O ASIC clock frequency 24999480Hz
sched_clock: 32 bits at 24MHz, resolution 40ns, wraps every 85901132779ns
MIPS counter frequency 59998512Hz
sched_clock: 32 bits at 59MHz, resolution 16ns, wraps every 35792281591ns

Rounding to nearest is more adequate:

I/O ASIC clock frequency 24999664Hz
sched_clock: 32 bits at 25MHz, resolution 40ns, wraps every 85900499947ns
MIPS counter frequency 59999728Hz
sched_clock: 32 bits at 60MHz, resolution 16ns, wraps every 35791556599ns

Signed-off-by: Maciej W. Rozycki <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Acked-by: John Stultz <[email protected]>
Link: https://lore.kernel.org/r/[email protected]

---
kernel/time/sched_clock.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/kernel/time/sched_clock.c b/kernel/time/sched_clock.c
index b1b9b12..ee07f3a 100644
--- a/kernel/time/sched_clock.c
+++ b/kernel/time/sched_clock.c
@@ -8,6 +8,7 @@
#include <linux/jiffies.h>
#include <linux/ktime.h>
#include <linux/kernel.h>
+#include <linux/math.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/sched/clock.h>
@@ -199,11 +200,11 @@ sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)

r = rate;
if (r >= 4000000) {
- r /= 1000000;
+ r = DIV_ROUND_CLOSEST(r, 1000000);
r_unit = 'M';
} else {
if (r >= 1000) {
- r /= 1000;
+ r = DIV_ROUND_CLOSEST(r, 1000);
r_unit = 'k';
} else {
r_unit = ' ';