From nobody Fri Dec 19 06:39:16 2025 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 97E571DED7C for ; Sun, 27 Apr 2025 18:52:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745779971; cv=none; b=AOT1IGkv1lPLOqJlPxnfq5ZPVNC6u1hgZYIH1KfzWzz6VcoF3TrBy5J0TgHNvzWYhfLX79PrnqMN5Xs4QqOMC32FJwVRNBGISmVDGFRe8C6w1LGe5/fPCH6EUhh+epDQ7a88TkCZBUKT7ZWfU/NK/L7lqn8NqskHxtMwsxxStWU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745779971; c=relaxed/simple; bh=+yUIscScO6W9Sugfn+LBZSyzjJ+5sc1HqsohaPrnH1I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UM5w40BMLl8S3zVn5nHwOP0AMheZDqmBPvCvSLgYTS/giFwdgT89tsPQUrUSCGWAxjRYwho3lNGAPwPEI8vHrhH/ga/OhnkXqm2BMYei900OegzL+hQEOMMEmLpugsLzapwtJeiJq9kp365M4l2pXiKi+j4myq5P9VPLlGR0gr4= 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=TynIz5kR; arc=none smtp.client-ip=209.85.214.177 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="TynIz5kR" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-2241053582dso61975215ad.1 for ; Sun, 27 Apr 2025 11:52:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1745779969; x=1746384769; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=s6m/NTTSOwQiMzNwImiTLbzzWwaouvSW4fUwqzHTNl0=; b=TynIz5kRwSfVBjIhFEgqw2whYt7Y8SDLm6IIu1sbcrx8Fnkh2GSaVXxlmxdBn5oTtl yp05zDn9eGBXQoa9UIe0EdZUopb7UJ7MGOKmd5Oykz3bXQlyZA5WH6ctv3SPJ3nkMilM kAxDPGzXh8icHySb1EpJeN7QjU22wzDlSQWJVELfzMjCGa/sP88AinpyxU1nBjpzuaPY BB5qu/0WW7Hn91N7m4Ld6u0/cEmC6dDhxtDBti86TgVOOHdXM6lj+OAGVpPTQw0SAacl T7j5moSyYQCSaouUwkCktXoW+VgFrQRfbxH6c7ucfH8qi4v9XGjiCnpqoBj8oRDlSDfK hIyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745779969; x=1746384769; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=s6m/NTTSOwQiMzNwImiTLbzzWwaouvSW4fUwqzHTNl0=; b=i6z/eo4mBFY5pAkY5gPlx/TB2P69NfBXhXRcSY5tps2AZihv0PWWsOgyjlsvdGfFHt jupqTXdQN00iM+Bz9LbESQVCFZqhxSWAuqXoEcGnN5cUcISJoAG+aTt89YlwF1NvOUBz SNQVfsEGdclY4CP91VgygQYcfjeW5Ps/2CNRON2sVlI7gneohTmS9bzaqowYUp/DkEyw J6y16fuMP4vO8LwQ+AKFOuUqG2GBuZvGth0Djt2bwL8TpQCcORDH5nkpZNlzo1uBijJo NBbQEwwdimT65hGKOjqT8v7Wp78zlHJZfMBCgcfvaj0EHjqK7romxL7gj9m87kQqbfXQ DPmA== X-Forwarded-Encrypted: i=1; AJvYcCV8xn6GsiDoXpR1ZK8zbjMh4a/dU7q+Tl92BgtaDQOQ83M9S8tDxpDGnnMsktI6nu0bISJRSltXro0ajN4=@vger.kernel.org X-Gm-Message-State: AOJu0YwdM7wYTkX2mAn1JHnBFOIO6kS4mFRffD0EmoNGr5Rr9uAMPT8j hpKyRTVQUyAztI2O1bAbuI4LNbYLRIbDFLQM0rSY+ur2bRfk+TzifOkMvO11 X-Gm-Gg: ASbGncuB/ZAJUwYPqOt63tAc0qEZPgUoy9IVka/8/BRZwf1qk/Cj1+YyKvkTIZBIViL gsZcnQDY4eXkH0UGDu8NBIoS1rV7un/PtoLJ4nkJ/lFxMgVbSbI6bGmshF872C2TGIvrgVyZ+I8 s+Hdy4/8WI6UwjYHgplr3LZWpY/1SBGoKTmFA/2jeYK6eoZJUCjVR9qXBVq+GV7RkjDbl8msXVh xZmn6gFwiKcFhpHdFJPriWqCDshGpqJf/F6KxSx4rEF0Y+WcgwUIJv1HmIdEdNbkp5LLM3aaMbP +dEXYzsEpB0EUpaHoUdNRuBE7Z6GfUsW/ykhLVYN X-Google-Smtp-Source: AGHT+IFXvnEEFz/Pe0VZ/JgotiAoiUkN8gk7aEgobk0iuX3Yfzml3SC6hNsfkcHCSVow33zEt4imdw== X-Received: by 2002:a17:903:1a67:b0:220:bd61:a337 with SMTP id d9443c01a7336-22dc6a0f297mr99957415ad.23.1745779968763; Sun, 27 Apr 2025 11:52:48 -0700 (PDT) Received: from localhost ([216.228.127.130]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22db4dbbd37sm67676485ad.63.2025.04.27.11.52.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Apr 2025 11:52:48 -0700 (PDT) From: Yury Norov To: Reinette Chatre , James Morse Cc: "Yury Norov [NVIDIA]" , Tony Luck , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Rasmus Villemoes , x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/4] cpumask: relax cpumask_any_but() Date: Sun, 27 Apr 2025 14:52:28 -0400 Message-ID: <20250427185242.221974-2-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250427185242.221974-1-yury.norov@gmail.com> References: <20250427185242.221974-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]" Similarly to other cpumask search functions, accept -1, and consider it as 'any cpu' hint. This helps users to avoid coding special cases. Tested-by: James Morse Reviewed-by: James Morse Signed-off-by: Yury Norov [NVIDIA] Reviewed-by: Reinette Chatre --- include/linux/cpumask.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index beff4d26e605..0c57b9892d80 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -413,14 +413,18 @@ unsigned int cpumask_next_wrap(int n, const struct cp= umask *src) * @cpu: the cpu to ignore. * * Often used to find any cpu but smp_processor_id() in a mask. + * If @cpu =3D=3D -1, the function is equivalent to cpumask_any(). * Return: >=3D nr_cpu_ids if no cpus set. */ static __always_inline -unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu) +unsigned int cpumask_any_but(const struct cpumask *mask, int cpu) { unsigned int i; =20 - cpumask_check(cpu); + /* -1 is a legal arg here. */ + if (cpu !=3D -1) + cpumask_check(cpu); + for_each_cpu(i, mask) if (i !=3D cpu) break; @@ -433,16 +437,20 @@ unsigned int cpumask_any_but(const struct cpumask *ma= sk, unsigned int cpu) * @mask2: the second input cpumask * @cpu: the cpu to ignore * + * If @cpu =3D=3D -1, the function is equivalent to cpumask_any_and(). * Returns >=3D nr_cpu_ids if no cpus set. */ static __always_inline unsigned int cpumask_any_and_but(const struct cpumask *mask1, const struct cpumask *mask2, - unsigned int cpu) + int cpu) { unsigned int i; =20 - cpumask_check(cpu); + /* -1 is a legal arg here. */ + if (cpu !=3D -1) + cpumask_check(cpu); + i =3D cpumask_first_and(mask1, mask2); if (i !=3D cpu) return i; --=20 2.43.0 From nobody Fri Dec 19 06:39:16 2025 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.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 2ECAE1FBEA8 for ; Sun, 27 Apr 2025 18:52:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745779973; cv=none; b=uBOSdW7W8sbfwpl86B8D2jaS3YdjLtgzXdxpM7YeJcqyTyBXBZ95Z4f1lA/hLko0eSCPavnP/bsPyEqH7JkJGD+pj3N0wgYQdvM3oCQkgwxI3JEdqaKyGTfWVQC2CxS33qzYsGrRkxpl2/1c8ow9Iu0KbBaShlifFLWQz/lO4hE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745779973; c=relaxed/simple; bh=pqbhSihCYsOMPL4uXkzSmS/h3GOaMiX+inFO7uRy0A4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IxIonN83m5OukIAKGlCHRwiMdXFoPBta4bs8wYRG07ap0IjDuN3xxnrVtxKoZJk8bMTJO2l84Ca0tU37LI4i5Ue8zlMRRKDcC+4R7tXfK0HzaH+SE12yo6g8VwE+Gra7RNLmwOH7XSL4zTQ7C6O7kD3EXAoYXbZzWBm7DVIA9o4= 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=Y5WkZduP; arc=none smtp.client-ip=209.85.214.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="Y5WkZduP" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-22423adf751so41642455ad.2 for ; Sun, 27 Apr 2025 11:52:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1745779971; x=1746384771; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=c0h/Q0ZqpcrRUzIyzjMSsOP16UaeOVdlzCI6vE/Tuxw=; b=Y5WkZduP6u248hrewtMlY/PDjsMNLcz8Lqm5mz46mhCBz5UDHaa1YKGw0Yx/qJjQBx Fi3olgdJhIao2IcyxfBPuO2J/Za1ZPhnYMRsarDbpc/3lquUgPKvhaEM46BX2jOhshu0 ZVKM1IC7C3u3EoBo0oyhazLtZOe1jhHSX2MWKkDDIWHPq365q0XPvl0+5hJ1M9OOe5ty WmZublM3L7uBhKL+ERnwXgW3Diitx2cjVCnKObiAvvfxUi6ywazO/aunB5YRjDBOh2DS TGQ3sdectczkX/T2zbRTLRhHLMJM1Xo4gptxEw1zxuPU+DVlLDyL3YUrTUvQ7BYqaYLS WOvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745779971; x=1746384771; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=c0h/Q0ZqpcrRUzIyzjMSsOP16UaeOVdlzCI6vE/Tuxw=; b=waVphwTtyRnrShUxZRUvj67+YGwqIoXpilgdgQvvHKqEQMxXt3wKM2lanXSH5higma uqm9Q0C3tBamIDMzNUfcu87FIbi5eeH6IuoJ1svIUQr/tjicBMSGX0GM+WBMu64wMKv3 3FbUEwFUA5PAsbemjsOoshNtW0ZoS9aF8b/Rv/CbluSkwIWmUvLDfBeZ4pWQAXz/X25o D46k/7e87BFcYLD2K4SWOcHnDA0qqU8It14RPpvE5h+GlydcPF7P+HAP2VjBZcIVO6/P 99uOpV3dyhrhzCQvJ6ArzvaZEQg+bbnh59zSq7SO4DnZZOa0T7jJazgwSPyaPyT4bvBW /cwQ== X-Forwarded-Encrypted: i=1; AJvYcCWLanL52nrQxwIhWXQQvAN/ZshuiobtcJHpF0iA5nv3Mvxy871yDXZ2hmwGtomqIpKHNMqxKtrusaPaEWo=@vger.kernel.org X-Gm-Message-State: AOJu0YxqMZw/AKstBIq/TnI/irvqVCDvkgKK1mmagJgJKYlV0t/d2w4L t3uZQY98GYOtFsvJIzaQuDeKr4QQqEi08Wc/xVbSLrfMJQcn4lt5f/b+rS69 X-Gm-Gg: ASbGncv2PEURNEjPoUdm9/ClWpvOFYrkoqevBlD4ZqN2+hpwEclxtHH88O8GjY2FG8v dW3XD7nbiMAXwQGG/LjOlQPpTyANuXxoEXA275aXcHiY1cIWOPhpbjM5bSGi7/x26BpszNy3y4b Wgu5LSxFzeEfwDnv6UMOlsyu/FOcJX9Y2ZTg+kvoS9Li58YEmdny30piMK/dF2WIvdDK20LRapi 2KrurDbKR0MZlVnq2i2ybsaW2kQvkj7IieDsRUmDB1FAas3y6JewA6Q0MfO7eomwkpHnTyvqLMt F9GY1Tl//urS6G5/1LdMUFAWyJoqwCIcbLWWA2MR X-Google-Smtp-Source: AGHT+IHK3xnT18KzOjeXUa/Hk9HDjKHuRIcuSFrRBci4CoJ11vLoBGmDKq2nOqcrvR0gSOQogj3jHg== X-Received: by 2002:a17:902:ce8f:b0:224:b60:3cd3 with SMTP id d9443c01a7336-22dbf5f080dmr139096945ad.19.1745779971492; Sun, 27 Apr 2025 11:52:51 -0700 (PDT) Received: from localhost ([216.228.127.130]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22db50e7a40sm67519005ad.147.2025.04.27.11.52.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Apr 2025 11:52:50 -0700 (PDT) From: Yury Norov To: Reinette Chatre , James Morse Cc: "Yury Norov [NVIDIA]" , Tony Luck , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Rasmus Villemoes , x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/4] find: add find_first_andnot_bit() Date: Sun, 27 Apr 2025 14:52:29 -0400 Message-ID: <20250427185242.221974-3-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250427185242.221974-1-yury.norov@gmail.com> References: <20250427185242.221974-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 function helps to implement cpumask_andnot() APIs. Tested-by: James Morse Reviewed-by: James Morse Signed-off-by: Yury Norov [NVIDIA] Reviewed-by: Reinette Chatre --- include/linux/find.h | 25 +++++++++++++++++++++++++ lib/find_bit.c | 11 +++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/linux/find.h b/include/linux/find.h index 68685714bc18..5a2c267ea7f9 100644 --- a/include/linux/find.h +++ b/include/linux/find.h @@ -29,6 +29,8 @@ unsigned long __find_nth_and_andnot_bit(const unsigned lo= ng *addr1, const unsign unsigned long n); extern unsigned long _find_first_and_bit(const unsigned long *addr1, const unsigned long *addr2, unsigned long size); +unsigned long _find_first_andnot_bit(const unsigned long *addr1, const uns= igned long *addr2, + unsigned long size); unsigned long _find_first_and_and_bit(const unsigned long *addr1, const un= signed long *addr2, const unsigned long *addr3, unsigned long size); extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsig= ned long size); @@ -347,6 +349,29 @@ unsigned long find_first_and_bit(const unsigned long *= addr1, } #endif =20 +/** + * find_first_andnot_bit - find the first bit set in 1st memory region and= unset in 2nd + * @addr1: The first address to base the search on + * @addr2: The second address to base the search on + * @size: The bitmap size in bits + * + * Returns the bit number for the first set bit + * If no bits are set, returns >=3D @size. + */ +static __always_inline +unsigned long find_first_andnot_bit(const unsigned long *addr1, + const unsigned long *addr2, + unsigned long size) +{ + if (small_const_nbits(size)) { + unsigned long val =3D *addr1 & (~*addr2) & GENMASK(size - 1, 0); + + return val ? __ffs(val) : size; + } + + return _find_first_andnot_bit(addr1, addr2, size); +} + /** * find_first_and_and_bit - find the first set bit in 3 memory regions * @addr1: The first address to base the search on diff --git a/lib/find_bit.c b/lib/find_bit.c index 0836bb3d76c5..06b6342aa3ae 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -116,6 +116,17 @@ unsigned long _find_first_and_bit(const unsigned long = *addr1, EXPORT_SYMBOL(_find_first_and_bit); #endif =20 +/* + * Find the first bit set in 1st memory region and unset in 2nd. + */ +unsigned long _find_first_andnot_bit(const unsigned long *addr1, + const unsigned long *addr2, + unsigned long size) +{ + return FIND_FIRST_BIT(addr1[idx] & ~addr2[idx], /* nop */, size); +} +EXPORT_SYMBOL(_find_first_andnot_bit); + /* * Find the first set bit in three memory regions. */ --=20 2.43.0 From nobody Fri Dec 19 06:39:16 2025 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 13DDF1FDE22 for ; Sun, 27 Apr 2025 18:52:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745779976; cv=none; b=XMEVtX5sY2cMByS2dtUgKSsamqoguRCVP333eg1k65vNlS3SvvDQYnM+6mloDsedwLOppntL/uFv+CLGlQgZKlUfzCfVG15Po3VlnTMkWphiVlzezmYA5UBs6IouNNVy9lH8/IWSI6YOLlrKghJHubh8VXis4BWct5WnT69UXa0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745779976; c=relaxed/simple; bh=U2miFFKRSAPW3ozQaPz1OxT4XKvnYkF8vzHlwiewPpM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ndLw1Zsb9YXNJ17vrDq/ow5LAbHyU2yFj6KRinkkrlxQxS1G3axuUKQUs+KFzqv1UOyy6bkBhgWsf3yNe4Z0PmQWYLNyvSMFYbenuMFM70IgN04lC5YxC/nXnBuI54B0DsNiP7qT1p9hfJ6AqDZ2xKupICSsp1U1lsteBB+gFx4= 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=KZGrzl75; arc=none smtp.client-ip=209.85.210.179 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="KZGrzl75" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-736bfa487c3so3313188b3a.1 for ; Sun, 27 Apr 2025 11:52:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1745779974; x=1746384774; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Z46hNAzvS73eByM2wkb0Kh4cMzqI8gNnavxwTBWCHV0=; b=KZGrzl75N2UgBnY7IU4GKaRb7GKoHOvMjyBXQ4SPeYkoES0XKd6rTnUS38OUSueb0f WoiKqMPDchP4wkg6vWZK7Dnlrnz5qE5ZA75946FMwOWdadyvm99reKYYufATRAWnxT2j f5d7iFJR/sRd3str1K05mkxC1Ond5e9F4IYFm2TYko/cqT62eSaxuaT4X1hbkURSoHSY tky0iOVouYTUqIRjSNGD+BUrsBXLn4urm+Lzq+NYJo86Fhvk4IQ+76Y9ggv/hYOS9BoA b3D+YQXQy4zgxpFa5xcDJ13wuQZy8lPtbB8pl5jXFLp9/IypIiPUave8e/UbXZWD4gm+ NWwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745779974; x=1746384774; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Z46hNAzvS73eByM2wkb0Kh4cMzqI8gNnavxwTBWCHV0=; b=BtGcPcb43oRTAfmS1jVgbOC8JTvtUkXIrIEfcos4jy417uXydjUXFXHIKyg8ptc+cs CcrtiRTI54k1aA6+PzICS4Vx2fHMHN25RW5ZqkjAxpYofHTsWAkJTqEohUXmyU8WACxw 2asnsBeen/0/bvW2KS8UkJX6nox0d6PgDf1wNKDVP2AgKycNdvGTtmGOVFfggHaCy4GQ 05EaSmo5tgH4AZFKpceeOMfAEE6tnhNSYYdKCZByNSdibgJ/WCglGYtBJ2DdDJFe52dv vFV0yo2z3H+L3YkaklnyYAZ8JN5eA7zXmcOi5wSxTDHB0aC9wqSlA0yyflfrOHT/obKj srhg== X-Forwarded-Encrypted: i=1; AJvYcCVzW8ElXKrPI7qMBxs5O/UV9fXROxLXN3jjKd7mr/5IRKYgxygDSHbDJ9UzgwakK9au34Cw2p9djNBDvCE=@vger.kernel.org X-Gm-Message-State: AOJu0YzrtpbYu7N+tZ8ZinDuAAiXESc+Tnn+R189c4dDY2SKivBp/f9U QUar11un4jx/2vYdGjzg+IRHXfxVHhPfCFvllHZ8ggBkvpTwpW0Q X-Gm-Gg: ASbGncvoXAAa1ur2GTYvm3ZI8idioN9AV/bJw0LBwQDrsYyeJ/PLXYnBpDn8l8jgEUJ Ehy0ysfDS4ZvHTcPcJ6ZUzyNP+CbBhT1WBi81XoaqWE4WTf87mVttBHusFqo6YhT8Ln6etCF07O bkIxI602pHZ84Gs+GyLVWKDGakRvjPhgdywEdkAAHLCPqYa69I9DSLVyG/W+QqoKXKPpVLX3WiR rC5lUC9lUhVmJw1YdKQ8BNmx1+7Xba/gyfHzvTlQbYZ0zO+N0aMqPpqRFKAk+a0t9U6mOYNY5FR DxczaSLQAAzsdqS1+014Zizf/raIo6QQhi95wMKh X-Google-Smtp-Source: AGHT+IH4V33+fWd1Xo6DZDLmaGLax9OocWHELdYzP2FbwoFqY9mr79mO3NG3JwDzCCgFnHUJiBziVg== X-Received: by 2002:a05:6a00:986:b0:736:62a8:e52d with SMTP id d2e1a72fcca58-73ff72bf7b9mr8372678b3a.12.1745779974272; Sun, 27 Apr 2025 11:52:54 -0700 (PDT) Received: from localhost ([216.228.127.130]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e25965823sm6483009b3a.78.2025.04.27.11.52.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Apr 2025 11:52:53 -0700 (PDT) From: Yury Norov To: Reinette Chatre , James Morse Cc: "Yury Norov [NVIDIA]" , Tony Luck , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Rasmus Villemoes , x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/4] cpumask: add cpumask_{first,next}_andnot() API Date: Sun, 27 Apr 2025 14:52:30 -0400 Message-ID: <20250427185242.221974-4-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250427185242.221974-1-yury.norov@gmail.com> References: <20250427185242.221974-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]" With the lack of the functions, client code has to abuse less efficient cpumask_nth(). Tested-by: James Morse Reviewed-by: James Morse Signed-off-by: Yury Norov [NVIDIA] Reviewed-by: Reinette Chatre --- include/linux/cpumask.h | 59 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index 0c57b9892d80..7ae80a7ca81e 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -178,6 +178,19 @@ unsigned int cpumask_first_and(const struct cpumask *s= rcp1, const struct cpumask return find_first_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), small= _cpumask_bits); } =20 +/** + * cpumask_first_andnot - return the first cpu from *srcp1 & ~*srcp2 + * @srcp1: the first input + * @srcp2: the second input + * + * Return: >=3D nr_cpu_ids if no such cpu found. + */ +static __always_inline +unsigned int cpumask_first_andnot(const struct cpumask *srcp1, const struc= t cpumask *srcp2) +{ + return find_first_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), sm= all_cpumask_bits); +} + /** * cpumask_first_and_and - return the first cpu from *srcp1 & *srcp2 & *sr= cp3 * @srcp1: the first input @@ -284,6 +297,25 @@ unsigned int cpumask_next_and(int n, const struct cpum= ask *src1p, small_cpumask_bits, n + 1); } =20 +/** + * cpumask_next_andnot - get the next cpu in *src1p & ~*src2p + * @n: the cpu prior to the place to search (i.e. return will be > @n) + * @src1p: the first cpumask pointer + * @src2p: the second cpumask pointer + * + * Return: >=3D nr_cpu_ids if no further cpus set in both. + */ +static __always_inline +unsigned int cpumask_next_andnot(int n, const struct cpumask *src1p, + const struct cpumask *src2p) +{ + /* -1 is a legal arg here. */ + if (n !=3D -1) + cpumask_check(n); + return find_next_andnot_bit(cpumask_bits(src1p), cpumask_bits(src2p), + small_cpumask_bits, n + 1); +} + /** * cpumask_next_and_wrap - get the next cpu in *src1p & *src2p, starting f= rom * @n+1. If nothing found, wrap around and start from @@ -458,6 +490,33 @@ unsigned int cpumask_any_and_but(const struct cpumask = *mask1, return cpumask_next_and(cpu, mask1, mask2); } =20 +/** + * cpumask_any_andnot_but - pick an arbitrary cpu from *mask1 & ~*mask2, b= ut not this one. + * @mask1: the first input cpumask + * @mask2: the second input cpumask + * @cpu: the cpu to ignore + * + * If @cpu =3D=3D -1, the function returns the first matching cpu. + * Returns >=3D nr_cpu_ids if no cpus set. + */ +static __always_inline +unsigned int cpumask_any_andnot_but(const struct cpumask *mask1, + const struct cpumask *mask2, + int cpu) +{ + unsigned int i; + + /* -1 is a legal arg here. */ + if (cpu !=3D -1) + cpumask_check(cpu); + + i =3D cpumask_first_andnot(mask1, mask2); + if (i !=3D cpu) + return i; + + return cpumask_next_andnot(cpu, mask1, mask2); +} + /** * cpumask_nth - get the Nth cpu in a cpumask * @srcp: the cpumask pointer --=20 2.43.0 From nobody Fri Dec 19 06:39:16 2025 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.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 EC20D1FF1C9 for ; Sun, 27 Apr 2025 18:52:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745779979; cv=none; b=T55PPEwM56aj5ESAvelld+dYTnp/khhb7nrjw3fCGPE9RMCGXMFMHfRzAIGjXoBllAfYnbUh7dA1hWDK40xOXn068WOFGWVWAagtVTzmXLWGbT4Kwb3F1VUaCaivpKizjBj4RrzEzsSM4PxSgdQma/m42Q8EuNS7NZGS97se/pA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745779979; c=relaxed/simple; bh=RLuf2Vbd4p2z+T6UWIxXnrpwnN1+HvripSZDkxHS9Rs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uALIiOJ36kb+sMBlkkHOWzdl0y+92rlXCUPlf6GFqrthDu49ywWru/XB0lAGUTRqGb8uVcgvybs0K3e9K9BKRREul6uOwP6ZIEZz5IsBQKV/cOs/KEYvfPa/ayh5whNSqWMGWYLRMj9CZgN5wHF6dBsjIR33hJ+1u4lA88Tlq10= 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=Fx3YsRDv; arc=none smtp.client-ip=209.85.215.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="Fx3YsRDv" Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-af5139ad9a2so2680133a12.1 for ; Sun, 27 Apr 2025 11:52:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1745779977; x=1746384777; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qOc6qCpSk3atZC8FDuOHN/o9mk5mbvvO8ouakr9Vlok=; b=Fx3YsRDvhIvHrOCzKk/nIzzqnS717FWrP+WNkI/Bah/ZD5BvB63apLvnGz+pZADoA9 WssUuQBlInSCR0U3AbO0mIV70IigSZld/B8k92oqeCPQSl28J1k0ov6xPjNHRa155zlM Dp7+wqojEYZMgWKN2KzmDqhY0La40bteVxo0W3EEyOwHmnT5lGuAEWnGVqOhBVciIyvw Q81KNqXgYOH/956UKuKUfGgfpsMmwTudZ8rKJ/bqLgxITpd5+VK6Sp3Dm0kCXwLw6gob uwR0E/NF9FzH2ZoZCAsVqgQRaYhCip5Tf+CszCFJ5zjOnSZdYplfppYIKFoyPiivt0kd 5ZEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745779977; x=1746384777; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qOc6qCpSk3atZC8FDuOHN/o9mk5mbvvO8ouakr9Vlok=; b=d9TicdZVk9H26RjXpeAsEBDLhLzgKvYKzHAOnY5padn99Pjfzk2LEoJvyTXTKSJhYi dpMg4c7bIPmL5LeRpDrA8JUvHm9cIjM9vEUOub1SPOzE01tYHNYeeU5jsAhL8QEeh68+ YPehFP31P9DTEZrqkStFSGd5ol73ItDkmbXSEQ1T/TGx2uMLk7UWQ+BHsw1/g3OzWyzM rDcv4FAgguQqPfuy1zDoL3+no2QtoBbnao8w/VvuVK3ySD1dkklgEBnPzrZ2yGgjRjg6 nthCUb0wgV70AOabr6Hi6oJwLNJtjRACav36B7vsaL313x9uZFscOI+jiN2xdQltaFlL 7RGw== X-Forwarded-Encrypted: i=1; AJvYcCUv4mmB3wlowJrg5BP+Ht1GIYo+1YrwG2auWerU2GPOQ53/ZMeeuId8ESy/a98WFRPqrflswpFusLiu1dg=@vger.kernel.org X-Gm-Message-State: AOJu0Yzt1v/xO8Rj6eIpgSo+gaAW+GIko7LMEVLMvRbkodCJzOqpFM50 dkbOKg7gOi3BriRCgE41mmTw0lAoGnCnl9HiNRYzLUqgSVXLtV7z X-Gm-Gg: ASbGncv1IcaByyOEjt6yxESZ/1dPOBppOad4zr3AT3HHXS2GbEA2Pd699oGZD/BD4nh KEFaMhONPtKu6+MU5lyjyUfkhqQ02IBLhdRyrv4wQodf/rkEKhxVT/U9ygnkByeu03iNu1IyY/M Rxv60BvH5u99gGPvCIIHL4sxYIy1t/7MQ/mEVJwH00nSiAkPSRoyhMahNIZRrra8iPL+6IGu+3w v4UV0l/lf2VOO1p5x4zocUHEiI7a221rncaUh3IALpeol7nf5nEbZsSIua+M8NmYQsUPle4V25c FxdLqsO4CxCVzzQbFm8FiCkj93Rv53kjUQgVXz4c X-Google-Smtp-Source: AGHT+IE32q9L+jMRpGngzfnIfH6P1hRoSfmVzmUhHgMHF1IV/u+b0nml6twddH75x1QZ5ax+n6AoVA== X-Received: by 2002:a05:6a20:2592:b0:1f5:6d6f:28e with SMTP id adf61e73a8af0-2045b9fdbe1mr13202203637.42.1745779977054; Sun, 27 Apr 2025 11:52:57 -0700 (PDT) Received: from localhost ([216.228.127.130]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73e2593f4fbsm6492293b3a.45.2025.04.27.11.52.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Apr 2025 11:52:56 -0700 (PDT) From: Yury Norov To: Reinette Chatre , James Morse Cc: "Yury Norov [NVIDIA]" , Tony Luck , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Rasmus Villemoes , x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 4/4] x86/resctrl: Optimize cpumask_any_housekeeping() Date: Sun, 27 Apr 2025 14:52:31 -0400 Message-ID: <20250427185242.221974-5-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250427185242.221974-1-yury.norov@gmail.com> References: <20250427185242.221974-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]" With the lack of cpumask_any_andnot_but(), cpumask_any_housekeeping() has to abuse cpumask_nth() functions. Update cpumask_any_housekeeping() to use the new cpumask_any_but() and cpumask_any_andnot_but(). These two functions understand RESCTRL_PICK_ANY_CPU, which simplifies cpumask_any_housekeeping() significantly. Tested-by: James Morse Reviewed-by: James Morse Signed-off-by: Yury Norov [NVIDIA] Reviewed-by: Reinette Chatre --- arch/x86/kernel/cpu/resctrl/internal.h | 28 +++++++------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/r= esctrl/internal.h index eaae99602b61..25b61e466715 100644 --- a/arch/x86/kernel/cpu/resctrl/internal.h +++ b/arch/x86/kernel/cpu/resctrl/internal.h @@ -47,30 +47,16 @@ static inline unsigned int cpumask_any_housekeeping(const struct cpumask *mask, int exclude_cpu) { - unsigned int cpu, hk_cpu; - - if (exclude_cpu =3D=3D RESCTRL_PICK_ANY_CPU) - cpu =3D cpumask_any(mask); - else - cpu =3D cpumask_any_but(mask, exclude_cpu); - - /* Only continue if tick_nohz_full_mask has been initialized. */ - if (!tick_nohz_full_enabled()) - return cpu; - - /* If the CPU picked isn't marked nohz_full nothing more needs doing. */ - if (cpu < nr_cpu_ids && !tick_nohz_full_cpu(cpu)) - return cpu; + unsigned int cpu; =20 /* Try to find a CPU that isn't nohz_full to use in preference */ - hk_cpu =3D cpumask_nth_andnot(0, mask, tick_nohz_full_mask); - if (hk_cpu =3D=3D exclude_cpu) - hk_cpu =3D cpumask_nth_andnot(1, mask, tick_nohz_full_mask); - - if (hk_cpu < nr_cpu_ids) - cpu =3D hk_cpu; + if (tick_nohz_full_enabled()) { + cpu =3D cpumask_any_andnot_but(mask, tick_nohz_full_mask, exclude_cpu); + if (cpu < nr_cpu_ids) + return cpu; + } =20 - return cpu; + return cpumask_any_but(mask, exclude_cpu); } =20 struct rdt_fs_context { --=20 2.43.0