[PATCH 3/3] clocksource: improve randomness in clocksource_verify_choose_cpus()

Yury Norov posted 3 patches 6 months, 2 weeks ago
There is a newer version of this series
[PATCH 3/3] clocksource: improve randomness in clocksource_verify_choose_cpus()
Posted by Yury Norov 6 months, 2 weeks ago
From: Yury Norov [NVIDIA] <yury.norov@gmail.com>

The current algorithm of picking a random CPU works OK for dence online
cpumask, but if cpumask is non-dence, the distribution of picked CPUs
is skewed.

For example, on 8-CPU board with CPUs 4-7 offlined, the probability of
selecting CPU 0 is 5/8. Accordingly, cpus 1, 2 and 3 are chosen with
probability 1/8 each. The proper algorithm should pick each CPU with
probability 1/4.

Switch it to cpumask_random(), which has better statistical
characteristics.

Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
---
 kernel/time/clocksource.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c
index e400fe150f9d..0aef0e349e49 100644
--- a/kernel/time/clocksource.c
+++ b/kernel/time/clocksource.c
@@ -340,10 +340,7 @@ static void clocksource_verify_choose_cpus(void)
 	 * CPUs that are currently online.
 	 */
 	for (i = 1; i < n; i++) {
-		cpu = get_random_u32_below(nr_cpu_ids);
-		cpu = cpumask_next(cpu - 1, cpu_online_mask);
-		if (cpu >= nr_cpu_ids)
-			cpu = cpumask_first(cpu_online_mask);
+		cpu = cpumask_random(cpu_online_mask);
 		if (!WARN_ON_ONCE(cpu >= nr_cpu_ids))
 			cpumask_set_cpu(cpu, &cpus_chosen);
 	}
-- 
2.43.0
Re: [PATCH 3/3] clocksource: improve randomness in clocksource_verify_choose_cpus()
Posted by John Stultz 6 months, 2 weeks ago
On Wed, Jun 4, 2025 at 2:21 PM Yury Norov <yury.norov@gmail.com> wrote:
>
> From: Yury Norov [NVIDIA] <yury.norov@gmail.com>
>
> The current algorithm of picking a random CPU works OK for dence online

spelling nit: dence -> dense

> cpumask, but if cpumask is non-dence, the distribution of picked CPUs

same: non-dence -> non-dense


> is skewed.
>
> For example, on 8-CPU board with CPUs 4-7 offlined, the probability of
> selecting CPU 0 is 5/8. Accordingly, cpus 1, 2 and 3 are chosen with
> probability 1/8 each. The proper algorithm should pick each CPU with
> probability 1/4.
>
> Switch it to cpumask_random(), which has better statistical
> characteristics.
> Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> ---
>  kernel/time/clocksource.c | 5 +----
>  1 file changed, 1 insertion(+), 4 deletions(-)
>
> diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c
> index e400fe150f9d..0aef0e349e49 100644
> --- a/kernel/time/clocksource.c
> +++ b/kernel/time/clocksource.c
> @@ -340,10 +340,7 @@ static void clocksource_verify_choose_cpus(void)
>          * CPUs that are currently online.
>          */
>         for (i = 1; i < n; i++) {
> -               cpu = get_random_u32_below(nr_cpu_ids);
> -               cpu = cpumask_next(cpu - 1, cpu_online_mask);
> -               if (cpu >= nr_cpu_ids)
> -                       cpu = cpumask_first(cpu_online_mask);
> +               cpu = cpumask_random(cpu_online_mask);
>                 if (!WARN_ON_ONCE(cpu >= nr_cpu_ids))
>                         cpumask_set_cpu(cpu, &cpus_chosen);
>         }

This looks ok to me.   Again, just the smallest nit about the subject
line capitalization.

Acked-by: John Stultz <jstultz@google.com>

thanks
-john