From nobody Tue Dec 2 02:41:47 2025 Received: from mail-qv1-f54.google.com (mail-qv1-f54.google.com [209.85.219.54]) (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 F41922E5B27 for ; Wed, 19 Nov 2025 03:13:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763521993; cv=none; b=j2JP3hqb9Yg6ZzVXV08qf/lOd4ZAw0YCNNTaxdwxfsHo+0mOrFKqAGaNCSPvfiBRQPbDCgGMKh3QC5D9Jj4Rbwdc7nVFx8Gb5TupQ14xjlYv6RiMbpb37MR1sAFkVLEF8l+NKcK63YWjMAZXMJrLTmCwhwgJf+t+3XTiqEo+hCs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763521993; c=relaxed/simple; bh=FzQoN0Fz/5DX4bgbgC0hvq+Q/W90GfzxBvwdlrQmhYU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GQAIBdyG5Amxxg9ioxQK38EttOR6o/fuM6xk+h4x+ZwtLwvqoDphlTxAlsBOyMECz8caAQCmqZgkZR0xA38qaiCJUHzI1gdslXbANoLrVbbA0lE3CNKXB47sOB4lDZOc7Gn97kBOspOUnzztt4SSiHpwrOqmgFYtRk2ENTSPFF0= 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=FmJN6xMi; arc=none smtp.client-ip=209.85.219.54 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="FmJN6xMi" Received: by mail-qv1-f54.google.com with SMTP id 6a1803df08f44-88246401c9eso64377566d6.1 for ; Tue, 18 Nov 2025 19:13:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763521991; x=1764126791; 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=c4l16W1Wn26X05sYd70JqRscVlE8+b33U+EjlbNpCeI=; b=FmJN6xMi7dhJudBYKsPK0RfzQUkekPdUZ8cA00ojRez/IS8wpHNj067CG0sc8khDDq 3qKecDVWUSnTwX7cZdBeqnhVbeW425OXd5nENu67+kzAgJe8BY0SXb6Ib8VA1dtunXbi l3VcMspLpNT28Vtkqbiu4Tcsou67ShFll8z+iIfKoQNUkUPuc63OxgeAMVkUBwLft0Zm zpSuNAi7N1dG/qdwGGKADvc0Mwbmr/ekrkX5LRt+1OGiscQ4+XDC7gBJyiGxIOG2n81t wuIBi4ks3iIs/broB2tlUMGrrT5hIGGcfszBPsuvHokw+DLH3A4jSAAHjMVQJcD1dmCF X3UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763521991; x=1764126791; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=c4l16W1Wn26X05sYd70JqRscVlE8+b33U+EjlbNpCeI=; b=rwxn/w6mG6Rzr0AKyrbDS1Nz1jzU6I1b4i1apvg/FGk3vune3nFLKHwdf7P5dhVw4p iWr2KgySd4mY1xfVAMLyKuc5lQISjSnRI2bRUCrFLpmUSkH/i1nzWRmENvnTXNGC0ZyA WHw0Mrf+yeQ0uKST64igHcPkpXpR7ECQwRt6eh6VbLd1gQU5vgqleu62mr/0oDEisnhG 7Y6AZ1TNuCJJMuFQKHjZHDYiKYnNXEHPtNyPqtsd+uNCDYpGf6dxrZr3boEzA//cHmHb G4xjNUPTzh9oJt3t23zJbFZE8EDRdFRPcxkkd83p+EnbiKceIx9cNIhMdUhWODwaEScJ y/aQ== X-Forwarded-Encrypted: i=1; AJvYcCUu0xk5wBdzTGixNuS9pW7YqPk2LLMhQhd2nfhijFAjGG+Ha063S/gDHC8/Lu860VC+XZsA+ccAFnnbNuQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxbkhUdvE5TtiTRQb8+FN8+nCfqrS+vPSnIbQw66C9WjS981cic PKXRWvRaYN7wpvVnIUrC0RlFL4DHX/3m/EQl7L0uFAdIHk+AH+3PcMt9 X-Gm-Gg: ASbGnct0Rczsvlw0pkvpYIwrhBbxmCZ65RZUfbd9gwLYSLtnGSu1hI0yYPBjqyPIkij whPDKmR6bUGLcqvB10Zxlq/cHYtN3U5sqv87AMIU1nuMAHR0RaELDFKRdZUU6OFdtV/H8mHnSSg AiqKg6NAQzA9+xxQaU8OiyT2jhmg7RbpkIKl24NX/+5ghPdLlow9dYoXF9VuXLFcWQ3ipIL2pAe LxFF9deeX1/ZwskaTF1LHQNs0TS4n+udQLi8txNC3FWgBXkioPTZ3BQwNbIMyqfcJcDEo/fHm2b VnaC8XLzT2yWnH7Xc0cqidk77S7bDvuwQ0o+ZdkuDW+8W1xt39E8s8tP+neHwYoiHdY4F+MabyI TsFU/gk5SYUhLtWkcDT52lk7/GhqHUTMVzm5vkca9uhrkpU5xk7zZZmgfVaFXZHSJFn8EwsCg6E 1dRWbRuvTnW5VvTgbo6Q== X-Google-Smtp-Source: AGHT+IFEbjE+oNmvrGPTatpweMC+bRlR73gGZI6ca81xg2FWv4mqMyMtqfy/KoPx8GCi/Mvfx+83ng== X-Received: by 2002:ad4:5cae:0:b0:882:4a63:63a0 with SMTP id 6a1803df08f44-8829273fca0mr286088926d6.58.1763521990795; Tue, 18 Nov 2025 19:13:10 -0800 (PST) Received: from localhost ([12.22.141.131]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8828631454csm125873926d6.18.2025.11.18.19.13.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Nov 2025 19:13:10 -0800 (PST) From: "Yury Norov (NVIDIA)" To: Andrew Morton , Thomas Gleixner Cc: "Yury Norov (NVIDIA)" , Rasmus Villemoes , linux-kernel@vger.kernel.org Subject: [PATCH 1/3] bitmap: cpumask: introduce and_andnot search helper and iterator Date: Tue, 18 Nov 2025 22:13:03 -0500 Message-ID: <20251119031306.644129-2-yury.norov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251119031306.644129-1-yury.norov@gmail.com> References: <20251119031306.644129-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" Like other similar iterators, *_and_andnot helps to get rid of temporary on-stack bitmaps and associate housekeeping code. Signed-off-by: Yury Norov (NVIDIA) --- include/linux/cpumask.h | 22 ++++++++++++++++++++++ include/linux/find.h | 38 ++++++++++++++++++++++++++++++++++++++ lib/find_bit.c | 9 +++++++++ 3 files changed, 69 insertions(+) diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index ff8f41ab7ce6..6de16a0e6e7b 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -406,6 +406,28 @@ 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_and_andnot_from - iterate over every cpu in all masks + * @cpu: the (optionally unsigned) integer iterator + * @mask1: the first cpumask pointer + * @mask2: the second cpumask pointer + * @mask3: the third cpumask pointer + * + * This saves a temporary CPU mask in many places. It is equivalent to: + * struct cpumask tmp; + * cpumask_and(&tmp, &mask1, &mask2); + * cpumask_andnot(&tmp, &tmp, &mask3); + * for_each_cpu_from(cpu, &tmp) + * ... + * + * After the loop, cpu is >=3D nr_cpu_ids. + */ +#define for_each_cpu_and_andnot_from(cpu, mask1, mask2, mask3) \ + for_each_and_andnot_bit_from(cpu, cpumask_bits(mask1), \ + cpumask_bits(mask2), \ + cpumask_bits(mask3), \ + small_cpumask_bits) + /** * 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..daf72078c25e 100644 --- a/include/linux/find.h +++ b/include/linux/find.h @@ -14,6 +14,9 @@ unsigned long _find_next_and_bit(const unsigned long *add= r1, const unsigned long unsigned long nbits, unsigned long start); unsigned long _find_next_andnot_bit(const unsigned long *addr1, const unsi= gned long *addr2, unsigned long nbits, unsigned long start); +unsigned long _find_next_and_andnot_bit(const unsigned long *addr1, const = unsigned long *addr2, + const unsigned long *addr3, unsigned long size, + unsigned long n); unsigned long _find_next_or_bit(const unsigned long *addr1, const unsigned= long *addr2, unsigned long nbits, unsigned long start); unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long= nbits, @@ -135,6 +138,36 @@ unsigned long find_next_andnot_bit(const unsigned long= *addr1, } #endif =20 +/** + * find_next_and_andnot_bit - find the next set bit in *addr1 and *addr2 + * excluding all the bits in *addr3 + * @addr1: The first address to base the search on + * @addr2: The second address to base the search on + * @addr3: The second address to base the search on + * @size: The bitmap size in bits + * @offset: The bitnumber to start searching at + * + * Returns the bit number for the next set bit + * If no bits found, returns >=3D @size. + */ +static __always_inline +unsigned long find_next_and_andnot_bit(const unsigned long *addr1, + const unsigned long *addr2, const unsigned long *addr3, + unsigned long size, unsigned long offset) +{ + if (small_const_nbits(size)) { + unsigned long val; + + if (unlikely(offset >=3D size)) + return size; + + val =3D *addr1 & *addr2 & ~*addr3; + return val ? __ffs(val) : size; + } + + return _find_next_and_andnot_bit(addr1, addr2, addr3, size, offset); +} + #ifndef find_next_or_bit /** * find_next_or_bit - find the next set bit in either memory regions @@ -595,6 +628,11 @@ unsigned long find_next_bit_le(const void *addr, unsig= ned (bit) =3D find_next_andnot_bit((addr1), (addr2), (size), (bit)), (bi= t) < (size);\ (bit)++) =20 +#define for_each_and_andnot_bit_from(bit, addr1, addr2, addr3, size) \ + for (; (bit) =3D find_next_and_andnot_bit((addr1), (addr2), (addr3), \ + (size), (bit)), (bit) < (size); \ + (bit)++) + #define for_each_or_bit(bit, addr1, addr2, size) \ for ((bit) =3D 0; \ (bit) =3D find_next_or_bit((addr1), (addr2), (size), (bit)), (bit) <= (size);\ diff --git a/lib/find_bit.c b/lib/find_bit.c index d4b5a29e3e72..aec79207c566 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -206,6 +206,15 @@ unsigned long _find_next_andnot_bit(const unsigned lon= g *addr1, const unsigned l EXPORT_SYMBOL(_find_next_andnot_bit); #endif =20 +unsigned long _find_next_and_andnot_bit(const unsigned long *addr1, + const unsigned long *addr2, + const unsigned long *addr3, + unsigned long nbits, unsigned long start) +{ + return FIND_NEXT_BIT(addr1[idx] & addr2[idx] & ~addr3[idx], /* nop */, nb= its, start); +} +EXPORT_SYMBOL(_find_next_and_andnot_bit); + #ifndef find_next_or_bit unsigned long _find_next_or_bit(const unsigned long *addr1, const unsigned= long *addr2, unsigned long nbits, unsigned long start) --=20 2.43.0