mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yury Norov <yury.norov@gmail.com>
To: linux-kernel@vger.kernel.org, Yury Norov <yury.norov@gmail.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	John Stultz <jstultz@google.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Stephen Boyd <sboyd@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: [PATCH 3/3] clocksource: improve randomness in clocksource_verify_choose_cpus()
Date: Wed,  4 Jun 2025 17:21:23 -0400	[thread overview]
Message-ID: <20250604212125.25656-4-yury.norov@gmail.com> (raw)
In-Reply-To: <20250604212125.25656-1-yury.norov@gmail.com>

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


  parent reply	other threads:[~2025-06-04 21:21 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-04 21:21 [PATCH 0/3] bitmap: introduce find_random_bit() and use in clocksource Yury Norov
2025-06-04 21:21 ` [PATCH 1/3] bitmap: generalize node_random() Yury Norov
2025-06-04 21:34   ` Andrew Morton
2025-06-04 21:46     ` Yury Norov
2025-06-04 22:37       ` Andrew Morton
2025-06-04 21:21 ` [PATCH 2/3] cpumask: introduce cpumask_random() Yury Norov
2025-06-04 21:21 ` Yury Norov [this message]
2025-06-05 18:17   ` [PATCH 3/3] clocksource: improve randomness in clocksource_verify_choose_cpus() John Stultz
2025-06-08 19:45 [PATCH v2 0/3] bitmap: introduce find_random_bit() and use in clocksource Yury Norov
2025-06-08 19:45 ` [PATCH 3/3] clocksource: Improve randomness in clocksource_verify_choose_cpus() Yury Norov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250604212125.25656-4-yury.norov@gmail.com \
    --to=yury.norov@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=jstultz@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=sboyd@kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®