From nobody Thu Oct 9 10:49:44 2025 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (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 306E42F0035 for ; Tue, 17 Jun 2025 20:09:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750190943; cv=none; b=t1c8yicXAqt6RzUIsVv8qW35i5hT33zzdbkU5qeddtyF/1n/eALncKyfcQBWyP3iC6JMV0XMTnh8Dol+RFoTXyKPrBekI4MVuhFV2nSPKcAEMjS8dXEg+vd0gHyIhaZUN4d0q+IklP83TptIgj5vNmpMekHvmaNeqytUYVAkfCQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750190943; c=relaxed/simple; bh=y/92ANFHZ511JoHlisgi/C+y3GJFbQaZiVIkYRVBlNk=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lFsIrbEXhmV/GoADwhumT4xh90sgVUdTVd0fHqWr69sfESv3Qk5SIsOPaqpAa5Oq5BXfobkDqxTDKo/REIRkMO5OhD22LHdpdDhVUpaBUOViJuPUYVJUUGEqBQ5oy0/ElJYJq6N9xe5HpsBEKQEpVFrWKjCQgZVqs6OWnfQyQZE= 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=R+Jwwots; arc=none smtp.client-ip=209.85.215.178 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="R+Jwwots" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-b3192eab3c9so4242800a12.3 for ; Tue, 17 Jun 2025 13:09:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750190941; x=1750795741; 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=00q9qP51ZqXMmsGMK1vazt4rv0rlL3V11s+lyKYXhXE=; b=R+JwwotsZ9b6GNVaM2ErqnQ5mNDFubeBAwEuN4TKl2VCjQsVPQqxm4M++Fpq4OKpoz j3/h6SzXCM1SWOyTbyTxL4RTjHkoUAlDya2F3DEvMlbiM8HRIhnfEE9m7qKDaIyASiUy guvWpIJ/WqhLHGzw2X0GiKNEY5mwK20bZLlh3kyVGAcXH+U3rwaT6KX/5D5l3yubGboq 8BEoQUmtky3Vo25qDn7yG77TEyrJDFi8ye6Zra8bgvRZvXxZ6ANBVju0LAVCEXKfmVHZ Jm9dNYFIJhE0H0j0P8y/xASUhgHPbcRjT45ymf/jZbuukPVabrq/ehXWO7Nmsj2bhrat NFXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750190941; x=1750795741; 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=00q9qP51ZqXMmsGMK1vazt4rv0rlL3V11s+lyKYXhXE=; b=AuYnDgpC+Hd3zNV7v6NAuieZ4nxrDjHIz3wF//WqUy/Nz7r27Tqg8N2yjjXTdD92b+ 9B5RBENS+yMkCaO8AdF7JtjmyM7/vxvy1S/c9gH4O2KsIWPGYGDyEWcL+e47UlIKYH67 ZAVjEsZxm7GYp6HHghVzy0SzMhGRonecceg2Jsn2UXEkMbXGKKvQDU//KCHbzn8g1X+l OcEulO3owaw7JToB/xD8m1+ogwkgfrLOYJXPrT4z6AXj6Y283XMww1HkEjztW7eO8ck7 Z3r9UOHeuQMS4roMpxydMmI9tO15tn1fsaaRxR8/CYKqns0lZRBJpq36kDK4eJdmOH9I Pbaw== X-Gm-Message-State: AOJu0Yxzi/eCEwsgY3YoCNQvg8npwV0lLBLAbFClQOCeaTSpgg4grfki 02CVL2reDEsWg/SS6nN/fJkiihObsnVb6nyRKJxbddvdlJA7l1c/YsqRfpS0Jg== X-Gm-Gg: ASbGncvSaSgB24xptzf1bn8ZqzyKEw5R4r/HMvdilcGAgw+EeZRnHh57/5Ib2H+VDIJ uleXFFEjBaPe6hePfqb1RtFT5dj5TU6xn+lH7P7kXUcZtOBibX9PI9qZYTR9/BdYBurdph6WVWl XATTIO7LWP6j4NfoN8vc33W/Kufckmr53L3D9IoxA/rnngrbISJKaWCrS2KhGuoANuq9Khi5Jqq RiOkvmMyeB6w0LBTyMyRKpaG5liDror4xcfdrypIs6FGhadnbdbfUBXGQFOZMZq/o1BEkRioPSq reYFgoPRpRpdP4P/nJg0T5zSfiuE2vGhZzQbyMWGw3hujBayz9afAiQsdTFc0Q== X-Google-Smtp-Source: AGHT+IH4aJlT4Jznp4cFCkV03jW/H5H2uxAyPFy6FzGxp9BA0qtEcKBLdDrG1BpopG6M5fjnOQXJZA== X-Received: by 2002:a05:6a20:47e4:b0:215:e60b:3bd3 with SMTP id adf61e73a8af0-21fbd57fbdcmr17971190637.29.1750190941232; Tue, 17 Jun 2025 13:09:01 -0700 (PDT) Received: from localhost ([216.228.127.128]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b2fe1680c54sm9390462a12.47.2025.06.17.13.09.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 13:09:00 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, "Yury Norov [NVIDIA]" , Rasmus Villemoes , John Stultz , Thomas Gleixner , Stephen Boyd , Andrew Morton Subject: [PATCH v3 1/3] bitmap: generalize node_random() Date: Tue, 17 Jun 2025 16:08:51 -0400 Message-ID: <20250617200854.60753-2-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250617200854.60753-1-yury.norov@gmail.com> References: <20250617200854.60753-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 | 16 +--------------- lib/find_bit.c | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 15 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..1cedc7132b76 100644 --- a/include/linux/nodemask.h +++ b/include/linux/nodemask.h @@ -492,21 +492,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 Thu Oct 9 10:49:44 2025 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (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 BCE8B2F0049 for ; Tue, 17 Jun 2025 20:09:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750190946; cv=none; b=RlSiuTNnMNuYduBfLUdL0zAZMtk/fBJHRuO0eomr381Yw7qcXO8qcy+LOQtVST5bnERICAP/iIo98qbBEHakJQ2DaeZI7dO4hUB7/d0kkleaKqNAS5eLkWfYdHdRAkL0muMp0MXVVb7XR6UUWqoCb7FoOyb1pYwlQ/lOfMzc+tU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750190946; c=relaxed/simple; bh=WKZqq2YEuAVNJBHG19OIJ1BG7TY/8FPSe5eCYcffo/A=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pczoQz9pBNQ/wHylgewppnOSuY0z1acMXRuyRK80UswpfcMOpTXTtGsKcGX+W6WQ6avJTnKjRr2A0MMYszUSdI/o5HANxEwSLMhoKiZ0acaBtx6kES6uNbFeVUBeyBx0rapBlWWnr58owvlvGatPVQH/SJ+lYsb7fEjag80D6TM= 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=l4B0F+nj; arc=none smtp.client-ip=209.85.216.43 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="l4B0F+nj" Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-3122368d7cfso4802176a91.1 for ; Tue, 17 Jun 2025 13:09:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750190944; x=1750795744; 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=l4B0F+nj7Z4CabdhItD7ShVrJXhoePvwZUWK/NmlS8JmMCGiZleAKb2g0nY3RHmzbF arlDGDQSGNBlTm8lKUg+jj3nbL5lNW4NFirbRCqNUVXrC874qyOrKOJPswPix//11E1t 7DF/w1OsYYQOC5HA5XHGB6Ip5Kv+hndVsi9u3G/nmwLDQdYVIZnxKweIKFw83pSarIg6 OVTjOsjmBai9cjd2JYURD7368H9srOcgUNfwpzHokxdptaeu2cPXVpOigr1VVk6StHq0 2Ye3IzVIc2gMR3qpqyrUh/unOy0KU2sdtkpk8cBqI/gp35oxJrBXijNJEtr3EFos2lHa Zw6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750190944; x=1750795744; 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=K3LrK8ssetXVoXKCDxOmJIG312/ucZUk3p5Pu55uiSM4fUYEHZrLP3HutyLdjfGp6/ jllUDHdTEOZSZrgP2xGYBTs9eIC4ScH1cTPJDPdunxpp6cLCTlPOTcYB/wu0f0UlIZ/4 k92s+fn4PEpP+yAOKNGIKoOi+D8iUncmJ4hOiYvjJFMVJ3LctiLRyIjD0zGN1G26evHW 0PkZRKQEsXgU8k88cVGkD4deUWn2McEOyUGOp7OkN86zf1ByJ5Hc60DdDKPZudL8ROTy PhPxyrYTAu45eMu9ETijTGGX7Log7U/NVqwGWbz3IT9pi3coeWKWi6zrpGKiaBOEkHZ4 CaCA== X-Gm-Message-State: AOJu0Yy1OX8T4t/4OvU1q4KuetKxfsRp2p0T9d5z7vXO+m6UmmKJ0+Zx Oz1XQZXEve5K1ujp8LcgkeTVJwBlEyYAv6xFgxT2axQ/ociXwvDH0KHj9jdcTw== X-Gm-Gg: ASbGnctOH04QQdpArpgeT0eowg4ZKCkPtNC7hWPiPFs7gADwGQSfY30p5RVViiJQuyx YKvgSXq7tandOS733GJxRw5EE9l5DiCwTWQdYB0IbFnCgXDVujZuKPTqYJyexnABXFT8QRi1QUt 9QLVOGDMdJ+VxIzYmxlthuWPcPLc4QSp5neyHgN/2YWohWr1WZFzrxOem45X1d8G/G3V649Rwlc fI9lNozBuKJKRY3qsSwXVy7zIdjO4voE1fjhwWZpUtkGmpvrC5dOsO0FVmO0RmTUxga7IUd46q5 SIDufA6sEV2W3tdI24yDQt09pRVUse1iI531IpHaed/U6BMHeoo3A10mKAp7Vw== X-Google-Smtp-Source: AGHT+IHx3MgIVSGDCv7wBGiIbtFBuDWjnqCVHuNC6JWptkhYTjdBZ7XEoFqqkfqKpxQhJESw4O+4Nw== X-Received: by 2002:a17:90b:1b48:b0:313:287e:f1e8 with SMTP id 98e67ed59e1d1-313f1ca745fmr20457548a91.8.1750190943937; Tue, 17 Jun 2025 13:09:03 -0700 (PDT) Received: from localhost ([216.228.127.128]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-313f9ee73c1sm7009784a91.16.2025.06.17.13.09.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 13:09:03 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, "Yury Norov [NVIDIA]" , Rasmus Villemoes , John Stultz , Thomas Gleixner , Stephen Boyd , Andrew Morton Subject: [PATCH v3 2/3] cpumask: introduce cpumask_random() Date: Tue, 17 Jun 2025 16:08:52 -0400 Message-ID: <20250617200854.60753-3-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250617200854.60753-1-yury.norov@gmail.com> References: <20250617200854.60753-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 Thu Oct 9 10:49:44 2025 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 6C3222F0C42 for ; Tue, 17 Jun 2025 20:09:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750190948; cv=none; b=EHnwcaoTp9cfb9ICAmt1E6QxC4k8kNLmVvSDqTzZiAiVAHeJD1AIa9OzPRZ/RlsCHiZFp5dX2iod5chhOZsUUXdqRZM/FUiIUMeP7B1udkG4OAUk2KSlIw8Tn8ofrj+uNK0ZIhSOZZWqOWWpGyr9wbrFriVHrBAIS7vxECdtZNs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750190948; c=relaxed/simple; bh=7ei3L9lEn3+xeaSdgHDRVafFEwUVf71uRbYEuYYerWY=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eJQ985VjuodCkedn5xOi/CWfnRStz2G+/+IszHxxO1bpvR0MOqOrnkykabafezN771xdIYgpGfw4c1KH8MVD3IaN89+qsdPTwKNm6L4NxuYypBwV/2KE0+vCdNvkgW4Bca8UOeTVPW63DUb0HkQnmqMIQXqCU5WyL8BrXhHuiXQ= 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=jk3gAxF1; arc=none smtp.client-ip=209.85.214.172 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="jk3gAxF1" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-23636167afeso61255035ad.3 for ; Tue, 17 Jun 2025 13:09:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750190946; x=1750795746; 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=SfL28LVHKWglO5omWIJDP+fyYvb9oxAH/n51aHNb2+Y=; b=jk3gAxF1Uk+iCORa4C34eDFPQTqw5iPvKK035pMkIVWFPiO3KMsgxnWAU157NmTRcp XIjNklsXC06MzlPa5doz9Kq1oMkHhHDirAJ8F9dezQjqq/XYK+tKRuRbIQEpwRqV6ySd bFJl5Sa9B/sAdHShsm/6DZvKTtxJJidz9Wbjzfs9qNE9s08BHXfWBY14NaGKz724o3ax jZihW7EXEm6J8KGNtCcOQhb0Ez+frDRiIrWpkG+K1zvwERf/UyEWZkI+QB5cU05Tv5B6 HlBXVhHHsvBOHpVVyZcPNfcsO06Lkk22snIxbTXuy3NcNBeCf0bGAt1eajre9mHvjumh 6NKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750190946; x=1750795746; 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=SfL28LVHKWglO5omWIJDP+fyYvb9oxAH/n51aHNb2+Y=; b=jDZSl5HHYRjBfzUvlzk/FxyyiTkDcNA7egChZ1WBAtJAtaMQ9uyGbxeq1mFh8NGAxG qsVvSj4E+IGSLVaDapxuOJO9hshQDkLDvM7RjXk3GPaIbyX2tG4+aBpyE/UFameKsO35 uSmzJlO1zcGJ6FT9Pjx7YPnauDHdSWzXzRs685i1JCNusOqwH3b5Y1Zm4VDle42xzTT/ 5Z852Rw5dGEJIzCnhnjj8HAtAFeD0+jiCJWn/SLaxBLUyHVgj0Gv1afAJo5GJNH6wQA4 wBjFKDfgOOtuhtAtOG3+KguGSi0y6cRxajPl1RwsVtGwSmJRTRSAzRloRyfNr6FJ7Fk1 kQ3A== X-Gm-Message-State: AOJu0YyQbFj+mpUjRIfi0zgJh8mVdei+WlShVYmhiT1EN4JlSHvgb8jj HJdYX7WiokVOOeJFz/kvP413zDQGBP9c7SV/tvYp6x9G+x8fAH+TRyF1lIWhNw== X-Gm-Gg: ASbGncsXDRhH6+E48qpLV4FY8069w0bDyprXw7uA8UmbGTmX3lwG6pszawvL1No31bR TiFJ2q/YOoPHg9RF4Sdl12VHFghYxYY4RzKNN/i3KXPOzBFvh2jPRVow6IZsK32UO+7Rjjw+cRd JoYJ+nS5hdOrCjh22aaypLM5k1czSSZH+sixs8aBZkLAZmkMcEZKUEvPQFPQsNRHrcKxrx+sqjQ utA2DAyyyBciYV5qpZvTvZVUn28A2Rd2YDjPMbsjFAUsX/U2LHZwfC8H/s9UhPdLyxR2psBWum0 g3o728r82jghr8yRofw9SajhQxKbSzr9SPal297ugRRM0DD8NUg+uWsP/SXIBDdlCH26zZ7z X-Google-Smtp-Source: AGHT+IHify4GZoSFFDiUXHB2yZ7FWrkl8YZ+uOy6ft/nbP1onf695Li9V8iAefEVOtrM60TrxsPvPg== X-Received: by 2002:a17:902:d4c9:b0:234:b430:cea7 with SMTP id d9443c01a7336-2366b32cdf5mr242241465ad.22.1750190946566; Tue, 17 Jun 2025 13:09:06 -0700 (PDT) Received: from localhost ([216.228.127.128]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2365d88be76sm84383085ad.32.2025.06.17.13.09.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 13:09:06 -0700 (PDT) From: Yury Norov To: linux-kernel@vger.kernel.org, "Yury Norov [NVIDIA]" , Rasmus Villemoes , John Stultz , Thomas Gleixner , Stephen Boyd , Andrew Morton Subject: [PATCH v3 3/3] clocksource: Improve randomness in clocksource_verify_choose_cpus() Date: Tue, 17 Jun 2025 16:08:53 -0400 Message-ID: <20250617200854.60753-4-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250617200854.60753-1-yury.norov@gmail.com> References: <20250617200854.60753-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 selected 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