From nobody Fri Dec 19 13:09:32 2025 Received: from BYAPR05CU005.outbound.protection.outlook.com (mail-westusazon11010005.outbound.protection.outlook.com [52.101.85.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 621293128CF for ; Mon, 8 Dec 2025 09:29:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.85.5 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765186172; cv=fail; b=L8uh60l1wouXMNPJA3NJRplL13/iZR08F7PpvAOaZA1lCnsw//+xTZta6dy0uGWblKBDVTo7dq2UmJYvDo3wEVekFXrvwHnhG7UHLjpI0mEO7caH81YIbg/HKAM32Zf4jpco99QbGNHSu8bN2XVmbBukCTm+H1d/vOd631zRdcc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765186172; c=relaxed/simple; bh=kWlvxjZIH9okFUQvmxq2rPQe4gyLd2Aw+s8og7Ky8Xk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ZRJf81h+qZmoNG2Z8wUiHac9B/XZ9+pDiF4nY0eBYwBoLPA6U92B8n+0ueGCsrCOEXUWxLnLJb7tYT4vjVs3hO3psBGzeTPXNKQqBkrOkTuruLfgg502AF4OxfU3dXZJI3Ep7qNOX3slJuj3HVkLzbK87jKm+4ApmCdkPO/5JHs= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=qeo6QUNS; arc=fail smtp.client-ip=52.101.85.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="qeo6QUNS" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=quQv3dwfMyl5/JiTiwgoDIE0511NzcoxKfnutAivrAKCezqPH9RQ3pf4iqqwHrLSomCCNZMsMhE/tr/Zh74kEr5hNgoc/FxjpE05yeX/qLt+xm2fLePp4tuDPog4TOsOQCISraec/RC/ZaA5TNLVbSUByun9qPw//ZEfq5ZKQ0+IUGSLDty1TRmZx8arr+QVFOF4eJdrfDX83aHGfaFyYau97Ygs7iQAk16zgfCm383ErxIELnqNXMk0KB7H75Pm0EDxLGuUcfLRfd7p3y7oIh/fRgZSRnduEh//iy8S5JJVHDdUX1o0J79HsS0B10mwnJa19RFfz6a4pXI17pGDyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=MLWgjDEFiQzTCqSG/H8AKfU0r77bsx7JzJ0s1whpbNY=; b=GMS5CWcuevuzY+MVLtUaecnQe5G9WPauF20HiO5D4TwADogxaueMDuozDdXLfTy+haCJCzbwNsLmXVJrna7qyAYxII6svICEV94KvsHVJ0hsJfQEo2bssL0/nFTWCt7eQiROoKzJfEoqmcVE2IsMTnU9U9vI1Gc98hB/1ZKnKhxIkSs5BGuJpILHzkBj6mGFx7+cpUHc+I/CjlgmMLo+2LU3GqdqEYDrbQeAkfYBiqJdhmqFBf6omj7lJDpPUPuLrAdFuvEFp7+UruVcmIKEcw8lsVLE1+8w1ZBnDQdV+dQYKcY9LpD6s/ePzH3VlQ1UYoUfT6QLArRKHAdTWfom2Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=MLWgjDEFiQzTCqSG/H8AKfU0r77bsx7JzJ0s1whpbNY=; b=qeo6QUNSgqCuGfxb8QnOXA0fcPU6HvvZDDQIX7bI/oeN9eTfB0NqN9JVXkTDGRMB3FbYulO+2SKHJ085fPQ2h7dLGuuPMKzuy9ZVNjhwSc7NS4A9fB6fvdS8MngCgv9bGFW4QyTyTIXPnQP+dVoKNLSy/olqqWMQ096CXbVV4CM= Received: from BYAPR01CA0066.prod.exchangelabs.com (2603:10b6:a03:94::43) by CH3PR12MB9123.namprd12.prod.outlook.com (2603:10b6:610:1a4::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9388.14; Mon, 8 Dec 2025 09:29:28 +0000 Received: from CO1PEPF000042AC.namprd03.prod.outlook.com (2603:10b6:a03:94:cafe::2b) by BYAPR01CA0066.outlook.office365.com (2603:10b6:a03:94::43) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9388.14 via Frontend Transport; Mon, 8 Dec 2025 09:30:59 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by CO1PEPF000042AC.mail.protection.outlook.com (10.167.243.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9388.8 via Frontend Transport; Mon, 8 Dec 2025 09:29:27 +0000 Received: from BLRKPRNAYAK.amd.com (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.17; Mon, 8 Dec 2025 03:29:20 -0600 From: K Prateek Nayak To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Anna-Maria Behnsen , Frederic Weisbecker , Thomas Gleixner CC: , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak , "Gautham R. Shenoy" , Swapnil Sapkal , Shrikanth Hegde , Chen Yu , "Yury Norov" , Rasmus Villemoes Subject: [RESEND RFC PATCH v2 06/29] cpumask: Introduce for_each_cpu_and_wrap() and bitfield helpers Date: Mon, 8 Dec 2025 09:26:52 +0000 Message-ID: <20251208092744.32737-6-kprateek.nayak@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251208083602.31898-1-kprateek.nayak@amd.com> References: <20251208083602.31898-1-kprateek.nayak@amd.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 X-ClientProxiedBy: satlexmb07.amd.com (10.181.42.216) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000042AC:EE_|CH3PR12MB9123:EE_ X-MS-Office365-Filtering-Correlation-Id: ad35998d-212e-442f-8126-08de363c4887 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|82310400026|7416014|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?tdGz12+k5XbMzXIObU9FZJ9Wa3J46eGqwO41bQ3IsIB5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d7SlV/X9NuPGEDRuFIM+JNjEuwiwvpTO6Gc9i6Ko8XKFu+j?= =?us-ascii?Q?Avmwb2EPaCM3fSWMcDr3vyvoIJF8JZMNd+7eVMWLoHyBUHok5drx4Tq1cPes?= =?us-ascii?Q?iVWrTc9HzDDTEnyzuhv68drtQYEpuVqJ1ItX6fFt23ZDB5+nImc6Ja5V2tx0?= =?us-ascii?Q?JH9GbLjuZDdmPELOxXOl+uHAtcE2L+ZPk4fnm502LnGuPh9ssvpIoCxMrrV5?= =?us-ascii?Q?IHsCiJXbjTwT4SgX/prkDWHgMoE9ZfiQJ5VTXFrHyKdzqSgFl79GAH8leq4X?= =?us-ascii?Q?nLO4P6GCtI+QsHKt1l5E0kt+/gZblKiLsvTSM3f3o5XnEZkeJF5Rv0om/V6t?= =?us-ascii?Q?gE2kclECvbAQAsNp8vS57BX18HCzl/3vUU7Jqin1zCkC1qJ1sLgypJue8HEL?= =?us-ascii?Q?c80du+fcODkNcRbsF8ouiU3Ysk9LIcuf5ki6k7M0dCAkJan+RpOusfjTfInU?= =?us-ascii?Q?aZKrcTjlzGoKSKUBbsf8JS9PU+3i8n1l3K1pxOBqoInXe7L4PZKMA15g64Ka?= =?us-ascii?Q?GBX7F5gLb3GYJqCUydkU1+85Y0Elf+8sUGarQr3ChXItUzt/+Msp+n5PFcfA?= =?us-ascii?Q?Isv7bj0GdEBt+5jdVIDjCtP7TulbtLYOurNvmR8TKi+5HTgbXW22e4J9/8Gb?= =?us-ascii?Q?KraLy9uD+N0rD8J7HyM=3D?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700013)(1800799024)(82310400026)(7416014)(376014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Dec 2025 09:29:27.4625 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ad35998d-212e-442f-8126-08de363c4887 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000042AC.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB9123 Content-Type: text/plain; charset="utf-8" There is a developing pattern of using cpumask_and() followed by for_each_cpu_wrap() in the scheduler. To avoid a temporary variable and needlessly iterating over the cpumask twice - once for the cpumask_and() operation and the second in the for_each_cpu_wrap() loop - introduce a new for_each_cpu_and_wrap() helper to iterate over the common bits set on two bitfields starting at a specific position without needing an intermediate cpumask variable. Cc: Yury Norov Cc: Rasmus Villemoes Signed-off-by: K Prateek Nayak Acked-by: Yury Norov (NVIDIA) --- include/linux/cpumask.h | 20 ++++++++++++++++++++ include/linux/find.h | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index ff8f41ab7ce6..b7a984c2a7d5 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -406,6 +406,26 @@ unsigned int cpumask_random(const struct cpumask *src) #define for_each_cpu_and(cpu, mask1, mask2) \ for_each_and_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpu= mask_bits) =20 +/** + * for_each_cpu_wrap - iterate over every cpu in both masks, starting at a + * specified location. + * @cpu: the (optionally unsigned) integer iterator + * @mask1: the first cpumask pointer + * @mask2: the second cpumask pointer + * @start: the start location + * + * This saves a temporary CPU mask in many places. It is equivalent to: + * struct cpumask tmp; + * cpumask_and(&tmp, &mask1, &mask2); + * for_each_cpu_wrap(cpu, &tmp, start) + * ... + * + * After the loop, cpu is >=3D nr_cpu_ids. + */ +#define for_each_cpu_and_wrap(cpu, mask1, mask2, start) \ + for_each_and_bit_wrap(cpu, cpumask_bits(mask1), cpumask_bits(mask2), \ + small_cpumask_bits, start) + /** * for_each_cpu_andnot - iterate over every cpu present in one mask, exclu= ding * those present in another. diff --git a/include/linux/find.h b/include/linux/find.h index 9d720ad92bc1..fff9e2d55e4d 100644 --- a/include/linux/find.h +++ b/include/linux/find.h @@ -487,6 +487,29 @@ unsigned long __for_each_wrap(const unsigned long *bit= map, unsigned long size, return bit < start ? bit : size; } =20 +/* Helper for for_each_and_bit_wrap(). */ +static __always_inline +unsigned long __for_each_and_wrap(const unsigned long *bitmap1, const unsi= gned long *bitmap2, + unsigned long size, unsigned long start, unsigned long n) +{ + unsigned long bit; + + /* If not wrapped around */ + if (n > start) { + /* and have a bit, just return it. */ + bit =3D find_next_and_bit(bitmap1, bitmap2, size, n); + if (bit < size) + return bit; + + /* Otherwise, wrap around and ... */ + n =3D 0; + } + + /* Search the other part. */ + bit =3D find_next_and_bit(bitmap1, bitmap2, start, n); + return bit < start ? bit : size; +} + /** * find_next_clump8 - find next 8-bit clump with set bits in a memory regi= on * @clump: location to store copy of found clump @@ -682,6 +705,20 @@ unsigned long find_next_bit_le(const void *addr, unsig= ned (bit) < (size); \ (bit) =3D __for_each_wrap((addr), (size), (start), (bit) + 1)) =20 +/** + * for_each_and_bit_wrap - iterate over all set bits in (*addr1 & *addr2) + * starting from @start, and wrapping around the end of bitmap. + * @bit: offset for current iteration + * @addr1: address of first bitmap + * @addr2: address of second bitmask to and with the first + * @size: bitmap size in number of bits + * @start: Starting bit for bitmap traversing, wrapping around the bitmap = end + */ +#define for_each_and_bit_wrap(bit, addr1, addr2, size, start) \ + for ((bit) =3D find_next_and_bit_wrap((addr1), (addr2), (size), (start));= \ + (bit) < (size); \ + (bit) =3D __for_each_and_wrap((addr1), (addr2), (size), (start), (bi= t) + 1)) + /** * for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set= bits * @start: bit offset to start search and to store the current iteration o= ffset --=20 2.43.0