From nobody Sun Feb 8 08:37:19 2026 Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7683E1DC9B5 for ; Sun, 8 Jun 2025 19:45:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749411944; cv=none; b=r2wcG5V3tacGCU2zKkQp6NRUkqODkEFYHPnlks/+5d73sl/eicogGyepAqsxrdfxlOiaxJi24ydk6rYx9+AV9hg/6mWi/iCqYXSns6er62+4jgmt0HgzEUGgxUJ3niAJhIXkrCNoTDDXYR38lX2aEZ/NtDG5pDawCMKGocB5HW8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749411944; c=relaxed/simple; bh=jvuqwmr/lQkbwxt68cOjpoUWY69SjpwbKNgucT7YAco=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JAqh2gJEwNXchGo0//a77r90hQAYs5yniIXVp9qE+fLhjNZbbC7/rF81jfOYj+xsa4KENujRsDR3aOLIMVnEl2lp/E3zzYXF2y1MwO1PlTH44T4dGttuytXw8inmHP9cDh6+6KvaiaJOxG1z7h3OjDlNtqkX0Xtm6Luhw8WDx2c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Caquv0n8; arc=none smtp.client-ip=209.85.128.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Caquv0n8" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-70f862dbeaeso36331617b3.1 for ; Sun, 08 Jun 2025 12:45:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749411940; x=1750016740; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=Ebejc2TdXmHZ5n4W7AD5k9zbeVCnHTbGLamRWttPuWM=; b=Caquv0n8d0wxEHZqxXuzSdsOqqx3JghPHPtfX2i/GGd41tYS521835ykOntjegqBEN 6LM2QuQ1OPtcAta5ch30sOulFdyBdnLXBFl0YJa+epeZG89Lcmk+BYw1pzmSMVCIzcMz Kz7hRO/fsGpM57vIY9Nv20J5mTw5RcyCpEdvwCKW1Q6THGTDr9C5vWjBEhzV/zoQYz4S CMzBuWT9fxd4PYrPxfnDyzpa6aylOMeHy8jJn90u+syqziwXd/w4Wbx5a1O3iumnGMP3 2ELjSQjtCoYtxuyWz/4/tP//CxccEp0slvkuLhy41/gLlz4dkTJW89cE6zBNbm15l9ZL jC7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749411940; x=1750016740; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ebejc2TdXmHZ5n4W7AD5k9zbeVCnHTbGLamRWttPuWM=; b=n2AP3LvthB53vN7MrDMrnSVuV4mI03jLG51jf2I8VPoPJifxsSrol/Tnk7r55wnpBW XVeGVtiT5Xe1YQItYdTn4LAu1T99mWlNgBzD6DTyLzQZS8jMsmiz34sgGSOAAGgsq7rJ 3IMplyI7vk/FEegtYmXW1hi7Lp0u9xLpx/dJBmOqk5KFzWquZ3T9XNCM2RhvxulOO/T1 5+mK/wq8ABK3w9EZR9t8i+BjiXXrBGDq4yqxZy1m2zp7ginJrtEz4rUke0bcPSqT68OD I2pCSJNLB735q/GV09Jub/TsGWsfaEOZRa5IUDqKSLdKiHt6ZfPxFMqkLmJ/1l2PCuUk QPSg== X-Gm-Message-State: AOJu0YyhX3oR7ian0GPsj7u6Jg3/u40C/kcXsczEKYtfMUBSRbkCP8MH 82GDMo+Ghar2kWEUadsV7EONL6Jo5fy7oi8xaTzwJZ9ggs6n+zva3YiHrq7z7w== X-Gm-Gg: ASbGncsQ44+ZZzg9++DlUC0DRFW7BpbD0TrWQxKjLiS8/TNY5aNlQUf8r3DU+ODXd+g 0vsa7ChBvspAA8HCY8Nn9EzudXuKQMJYHwZz9MrG3NC12+Fb2IICw+cAjKRrFFPDzS+1vNwJMKR kY5mrg19gG54l3kxn7cZ60qBFG4ePRrpg7awcP6orTOX0rzaHAdDomS2a/ZnGtjnoV5tSZd3485 ZH8vO19Er4F3HXzmAU+OvZTzloeYDsnhymhAqiQg2MPMeAy23cwDW6c9VcWaM64Bu7ddWe/KHhe 5+S2VIzsbZPot9uicHGjkRj7Hx0ppPebdMC/aIrSJT2IqA6KGwK8lLhTGnNWFJdsUxcYG+RZ5jP tVMfkCuNew7en9B2MeHOV2A== X-Google-Smtp-Source: AGHT+IFenZ/M1QgntFujjQr/MFL/cFeOqebzDK8Hf+6P/+v+M61FHONB48XPcZ13vZR56miRRKOJ2A== X-Received: by 2002:a05:690c:9987:b0:70e:731f:d4c7 with SMTP id 00721157ae682-710f762e824mr137930027b3.8.1749411940267; Sun, 08 Jun 2025 12:45:40 -0700 (PDT) Received: from localhost (c-73-224-175-84.hsd1.fl.comcast.net. [73.224.175.84]) by smtp.gmail.com with ESMTPSA id 00721157ae682-710f98afb31sm10052557b3.24.2025.06.08.12.45.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Jun 2025 12:45:39 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, Yury Norov , Rasmus Villemoes , John Stultz , Thomas Gleixner , Stephen Boyd , Andrew Morton Subject: [PATCH 1/3] bitmap: generalize node_random() Date: Sun, 8 Jun 2025 15:45:33 -0400 Message-ID: <20250608194536.28130-2-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250608194536.28130-1-yury.norov@gmail.com> References: <20250608194536.28130-1-yury.norov@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Yury Norov [NVIDIA]" Generalize node_random() and make it available to general bitmaps and cpumasks users. Notice, find_first_bit() is generally faster than find_nth_bit(), and we employ it when there's a single set bit in the bitmap. See commit 3e061d924fe9c7b4 ("lib/nodemask: optimize node_random for nodemask with single NUMA node"). Signed-off-by: Yury Norov [NVIDIA] --- include/linux/find.h | 2 ++ include/linux/nodemask.h | 17 +---------------- lib/find_bit.c | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/include/linux/find.h b/include/linux/find.h index 5a2c267ea7f9..98c61838002c 100644 --- a/include/linux/find.h +++ b/include/linux/find.h @@ -44,6 +44,8 @@ unsigned long _find_next_bit_le(const unsigned long *addr= , unsigned long size, unsigned long offset); #endif =20 +unsigned long find_random_bit(const unsigned long *addr, unsigned long siz= e); + #ifndef find_next_bit /** * find_next_bit - find the next set bit in a memory region diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h index f08ae71585fa..d49f3bd3e20b 100644 --- a/include/linux/nodemask.h +++ b/include/linux/nodemask.h @@ -91,7 +91,6 @@ #include #include #include -#include =20 extern nodemask_t _unused_nodemask_arg_; =20 @@ -492,21 +491,7 @@ static __always_inline int num_node_state(enum node_st= ates state) static __always_inline int node_random(const nodemask_t *maskp) { #if defined(CONFIG_NUMA) && (MAX_NUMNODES > 1) - int w, bit; - - w =3D nodes_weight(*maskp); - switch (w) { - case 0: - bit =3D NUMA_NO_NODE; - break; - case 1: - bit =3D first_node(*maskp); - break; - default: - bit =3D find_nth_bit(maskp->bits, MAX_NUMNODES, get_random_u32_below(w)); - break; - } - return bit; + return find_random_bit(maskp->bits, MAX_NUMNODES); #else return 0; #endif diff --git a/lib/find_bit.c b/lib/find_bit.c index 06b6342aa3ae..d4b5a29e3e72 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -18,6 +18,7 @@ #include #include #include +#include =20 /* * Common helper for find_bit() function family @@ -291,3 +292,26 @@ EXPORT_SYMBOL(_find_next_bit_le); #endif =20 #endif /* __BIG_ENDIAN */ + +/** + * find_random_bit - find a set bit at random position + * @addr: The address to base the search on + * @size: The bitmap size in bits + * + * Returns: a position of a random set bit; >=3D @size otherwise + */ +unsigned long find_random_bit(const unsigned long *addr, unsigned long siz= e) +{ + int w =3D bitmap_weight(addr, size); + + switch (w) { + case 0: + return size; + case 1: + /* Performance trick for single-bit bitmaps */ + return find_first_bit(addr, size); + default: + return find_nth_bit(addr, size, get_random_u32_below(w)); + } +} +EXPORT_SYMBOL(find_random_bit); --=20 2.43.0 From nobody Sun Feb 8 08:37:19 2026 Received: from mail-yb1-f169.google.com (mail-yb1-f169.google.com [209.85.219.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 902D51E3DF2 for ; Sun, 8 Jun 2025 19:45:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749411945; cv=none; b=oeRaK6/yrTe42iGkzIn5+6T8E8lIW0qZuCffaE4F/X+HwuLxY24kzAKoCQ672z6uoDjRSD5THvqczygm/nebGaDIV4PwgApEz8P7FCftU/HXXTqqaogvHIQvXpNtX3MzDh1aveF4RH/RUQbcbDX2xwHqm/cBkIVE+f6XW89wyLs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749411945; c=relaxed/simple; bh=WKZqq2YEuAVNJBHG19OIJ1BG7TY/8FPSe5eCYcffo/A=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B8MbTT+J/Z6JvNTSMeW85Dxdiw18dRizoMQzxgWIyrVXcTAhTXDDKjPZXdALNVvfvB/1owDqLSrCA0SajFXpxEqgRgkkMFVomVFFX8lf/+dLFf+vKCNi74o1vYPPQS0Ir+ObkQX55W4KecaZEYpdC8rCrAxJktM92dwWmVBknwk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ebG/nYK2; arc=none smtp.client-ip=209.85.219.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ebG/nYK2" Received: by mail-yb1-f169.google.com with SMTP id 3f1490d57ef6-e7311e66a8eso3261294276.2 for ; Sun, 08 Jun 2025 12:45:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749411942; x=1750016742; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=giWfCtGbWEqU63iGbzpmokmH0FMbVzty9wx804zxVgU=; b=ebG/nYK2+q8Id2ds3KNCoEdoQ6xa3zWyM2luyX6QQ/9Q41UlE/CGrf867ks1WFd1sg Cr1cdUcc77zrlxnd9SGKISyiscUlqKeku4uwYgGrQr2XpCmQcWsGBikiGCuezLcCIjCZ Q/3uxfHkmAaI31xg19kwyNEu4JuKQyjy4+XnKZfVr0fH/P3Bp5CUBxAIDzjh2bL6VDr1 t6W8DBtUg2/7Ip/wkIkyNUSsJfYPFmVVq4djhHqsSu/9xp+ZCPohj+OzK8u/BQjk6/8I A1nmTIlNWMm2wbadcyt34T2AtjRO1uyXf98a4g6pHwpjLJM3j1YXp0JbQjYHo6393XEY 2CqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749411942; x=1750016742; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=giWfCtGbWEqU63iGbzpmokmH0FMbVzty9wx804zxVgU=; b=nM4XxngkL2NgkZIrxvV0g+LXCvsdweyfq+GDaJuz/Am5MtPvBJsGMM1x+c9yOBpZYM lLIZgttnDqQuYWG/S9IoM6ku805uaqbLyBB6hrcUXWyoFaGjYYA9CqbexHPiRSkQbpC9 VGWVboO59GPeef+0hrnvukK0DKLX/PFFSl6vgEhI26PW6HoFlspuSmYztDdavOA5HrqH ceNzGE2QPuHNKH5LIiOioj/6yLih1mz0dimLNCLM+OK/1IVsapSSQvG5AFj9nHcXAj57 uqO/0ordzEkEny8FmhJjScV/OSaiOJDlNT33ITVNi4iSsiLM6NjHl7FLYkdaoikmgqiB /bKg== X-Gm-Message-State: AOJu0YzMzXPJr2MS2sjSCxKECv5UWlhGkOTUS67k67FVk/afJEbO7iY5 jO3egAJFSTq5/L9eWXjJEQ1nGEMWFIyKw9jhuoUe2AZft03kNFnge23XysVD6g== X-Gm-Gg: ASbGncvrSILh0U8Xoa766EruZ+JjcPslk6wj+a2IpmLw4hIRtiqsueM9Md/6SOcXNBh CipDW3ynJk38nAm5F3tYvDDGqPeHIR8a4xJHmrWwY7fFdYYgoBt1jpMGKsnzXdxW/u3Ff1Mf5ON O8jCzfOzIi6kt3rXvNuDUcy0Y4wh2kj3tSSeXdXSaYF/7ySVRhj8+v1sIhV5e7Hi0g/oK3kO2l3 TGXnyD5slF0LF+MpOpg4KdGf1ZhZ8yK8/dO1JQ3PWgNHF7Y/rFuyh1d5zYyx45wy5ASRDxLU8Nq q1EFIMCcne4u37T8Uv0DO9CB67VcRoH/QPNkBJSJsexR3Dvqw4MBm3uq84t8o/LkYpTV5asp1Tw khhkfaY7nGrj7u4m+crbVSvxYNqIhPzVZ X-Google-Smtp-Source: AGHT+IHlM3AMPnX1fi9ta4SGXd0hM+FTgie1jPdQ44wmf02Z4hAxdW1CZXUtAAQhLR5B5VMxM7x5bw== X-Received: by 2002:a05:6902:1249:b0:e81:db12:7950 with SMTP id 3f1490d57ef6-e81db127a0amr2535722276.30.1749411942442; Sun, 08 Jun 2025 12:45:42 -0700 (PDT) Received: from localhost (c-73-224-175-84.hsd1.fl.comcast.net. [73.224.175.84]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e81a400cc8csm1805622276.6.2025.06.08.12.45.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Jun 2025 12:45:41 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, Yury Norov , Rasmus Villemoes , John Stultz , Thomas Gleixner , Stephen Boyd , Andrew Morton Subject: [PATCH 2/3] cpumask: introduce cpumask_random() Date: Sun, 8 Jun 2025 15:45:34 -0400 Message-ID: <20250608194536.28130-3-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250608194536.28130-1-yury.norov@gmail.com> References: <20250608194536.28130-1-yury.norov@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Yury Norov [NVIDIA]" Propagate find_random_bit() to cpumask API. Signed-off-by: Yury Norov [NVIDIA] --- include/linux/cpumask.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index 7ae80a7ca81e..39b71b662da3 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -354,6 +354,18 @@ unsigned int cpumask_next_wrap(int n, const struct cpu= mask *src) return find_next_bit_wrap(cpumask_bits(src), small_cpumask_bits, n + 1); } =20 +/** + * cpumask_random - get random cpu in *src. + * @src: cpumask pointer + * + * Return: random set bit, or >=3D nr_cpu_ids if @src is empty. + */ +static __always_inline +unsigned int cpumask_random(const struct cpumask *src) +{ + return find_random_bit(cpumask_bits(src), nr_cpu_ids); +} + /** * for_each_cpu - iterate over every cpu in a mask * @cpu: the (optionally unsigned) integer iterator --=20 2.43.0 From nobody Sun Feb 8 08:37:19 2026 Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE1521EEA40 for ; Sun, 8 Jun 2025 19:45:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749411948; cv=none; b=YGT1M4TtG/PY0XfrBIORCpKVyUoYtrBqyxQp6crT9GcUG7YU8sn19gPKERyeRYziCmhTJCWggAosBfE1wO2M4NKibMX3qlaM4tdHlz5Hxr5moKuyY8A9N1HstiKQTI7TSUUAG781p+3cXaR+SfGEObFFV0cgT7OeFq3kZ7MznUY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749411948; c=relaxed/simple; bh=0z0z7BEAOwGXGFZrG5T/T3uQfwCpzUKGWzDdv99j4io=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=peT3wTUT0ndKBjnuaotLCJj/evjNW7lIkpHoA5YxaZBVhjvDdILv14nffAJ7iidbt7JA50Izh+Jl5x1H6Ve8+0uoouz/m+ABzgsFaMlA9+ChY8EeAQagWqpiFpP9/c9sJ8huMMuqreGMxfAYOb3PcyvDPICx5PIgb8Vc/uHpWc8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Z2MVTRFP; arc=none smtp.client-ip=209.85.128.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Z2MVTRFP" Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-70dd2d35449so27601787b3.3 for ; Sun, 08 Jun 2025 12:45:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749411944; x=1750016744; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=pdmjI2ThvlwaXhc0+WRx0izpT37E64MmtJ5nQi6GWpw=; b=Z2MVTRFPmnfB80wZZTtckN+G2TAFtbtVT6fHH0ggJtEXufVscdd+5zJYCA0+4vX3pV pi4EgIvEz45TmvitBGYE5llJMSVcL2Zd3fskI6+m0qxH7nrUmgGoYFrchO1lcOe+XK2j bV2Hz2EpqdLirMzV2aW5HL9L9w1Cz9I2R96xDG2HIpesAGvtfg6jywibXtWYRT6gVFee +lr2yDCqgOW7ppVtjIZQkSNoSLCNKzlBSOhe2MnRVEvNIfo3vTrDmeGsL3sRIQyfzr3U nnAfmGKt0J752kTbTlDrWCAfRGLVcfI1zkWj3OtP2xR1yQgn5t4Ja16KhJMXWjTSBblI hgMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749411944; x=1750016744; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pdmjI2ThvlwaXhc0+WRx0izpT37E64MmtJ5nQi6GWpw=; b=wGoH7oDk1w56ci6SublbOVcP0bq7JKvY+w8YlTcZeJvaHYTW1iW7/mU+CXnObKMkdF NinrhwNNvGhQdmQCL61Tcg5wszJp3NO8PuGyldTEknf+n9fI7ntJPJ6/mPU4jrWy/u8t FUTRJzDBz1q31pi7xGXfoLrwArQUjzvts8JKe915TY1vIRdCABDZ1vtQ3OHFoK9lNuMW c5fmm7CyotQIA6NGT8n97gieTtZEplSY1WXvh+/M47fsm4PFVmTrNAb0OkKr0oBRLqBO UYHktMOu4NHgpZw8MHSRLCeeAZzY4q1m3bSSYRNEMMq1ZCP1gJhYQCa26RKMGWuyFyC/ QFnQ== X-Gm-Message-State: AOJu0Yzk73ouCgBk1yzjImepkxrYipFokO7LGbMaBQd9dd3YRkAryUyc yJwun3c072DQjd5T8bGoF0bXknHf2HzoVxZbkmbTduJ8yqJu+RLb3doQZIXWrQ== X-Gm-Gg: ASbGnctHq6cry9R5NbiRA9XfCq1XuHS17nZjtMIvQQ42CFbDG7+7zxwlZBSqpHooR5+ wz462VmPi6I99RYOA9IKkabK4Y8jV4dzRqvt7SLzJV6JSW9Vwr3NgDS02ORlF2Bas2s/ZBn7dQi pc+FezFChsH3Gk+Gpe1SFnKXsZfxBqQWql2KzphwTc9co0db3P5xol6XO1JY38tQbBiF07iy4bO RCVl3yNzLbxysAzic6md77ObBxcL13b9R7ZOKF4UFvmU3L6Dku0J4km4DEAgvxMuGwZMj5fRjB8 V4O+ecokTxLKvwVga7Qjzt5Q1A+yU7wdfPQyK0uMJoBUGv9rOkObEuYcjUTcCSz8+gW4Ywcz/Z2 Jet3oyStMy3Nsdqsl6mgOxwrGO8F1IeVQ X-Google-Smtp-Source: AGHT+IG26jQrOUA1nsvWxPnchEGSBRX1882EKZVs2X+/4JcMjJI81Q1X/Xxc3iVECqZZloTddp0MEQ== X-Received: by 2002:a05:690c:4b0d:b0:710:f39f:a2bc with SMTP id 00721157ae682-710f763eb83mr142692157b3.8.1749411943911; Sun, 08 Jun 2025 12:45:43 -0700 (PDT) Received: from localhost (c-73-224-175-84.hsd1.fl.comcast.net. [73.224.175.84]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e81a41575casm1850732276.32.2025.06.08.12.45.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Jun 2025 12:45:43 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, Yury Norov , Rasmus Villemoes , John Stultz , Thomas Gleixner , Stephen Boyd , Andrew Morton Subject: [PATCH 3/3] clocksource: Improve randomness in clocksource_verify_choose_cpus() Date: Sun, 8 Jun 2025 15:45:35 -0400 Message-ID: <20250608194536.28130-4-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250608194536.28130-1-yury.norov@gmail.com> References: <20250608194536.28130-1-yury.norov@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Yury Norov [NVIDIA]" 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. Acked-by: John Stultz Signed-off-by: Yury Norov [NVIDIA] --- 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 6a8bc7da9062..4b005b2f3ef5 100644 --- a/kernel/time/clocksource.c +++ b/kernel/time/clocksource.c @@ -342,10 +342,7 @@ static void clocksource_verify_choose_cpus(void) * CPUs that are currently online. */ for (i =3D 1; i < n; i++) { - cpu =3D get_random_u32_below(nr_cpu_ids); - cpu =3D cpumask_next(cpu - 1, cpu_online_mask); - if (cpu >=3D nr_cpu_ids) - cpu =3D cpumask_first(cpu_online_mask); + cpu =3D cpumask_random(cpu_online_mask); if (!WARN_ON_ONCE(cpu >=3D nr_cpu_ids)) cpumask_set_cpu(cpu, &cpus_chosen); } --=20 2.43.0