Skip to content

Commit 8557c86

Browse files
committed
clocksource: Improve randomness in clocksource_verify_choose_cpus()
The current algorithm of picking a random CPU works OK for dense online cpumask, but if cpumask is non-dense, 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 online CPU with probability 1/4. Switch it to cpumask_random(), which has better statistical characteristics. CC: Andrew Morton <[email protected]> Acked-by: John Stultz <[email protected]> Reviewed-by: Thomas Gleixner <[email protected]> Signed-off-by: "Yury Norov [NVIDIA]" <[email protected]>
1 parent 012b104 commit 8557c86

File tree

1 file changed

+1
-4
lines changed

1 file changed

+1
-4
lines changed

kernel/time/clocksource.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,7 @@ static void clocksource_verify_choose_cpus(void)
342342
* CPUs that are currently online.
343343
*/
344344
for (i = 1; i < n; i++) {
345-
cpu = get_random_u32_below(nr_cpu_ids);
346-
cpu = cpumask_next(cpu - 1, cpu_online_mask);
347-
if (cpu >= nr_cpu_ids)
348-
cpu = cpumask_first(cpu_online_mask);
345+
cpu = cpumask_random(cpu_online_mask);
349346
if (!WARN_ON_ONCE(cpu >= nr_cpu_ids))
350347
cpumask_set_cpu(cpu, &cpus_chosen);
351348
}

0 commit comments

Comments
 (0)