From patchwork Mon Jan 29 06:21:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Souradeep Chakrabarti X-Patchwork-Id: 193267 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp389440dyb; Sun, 28 Jan 2024 22:23:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IGVvpX7e46CVT8He4Dv/nq+UCZHo8BVGVBzz/ZZLHjsohX6x3wIj03RsOGDOlk9ZJXns1iC X-Received: by 2002:ae9:e409:0:b0:783:1a32:a62c with SMTP id q9-20020ae9e409000000b007831a32a62cmr4571451qkc.149.1706509397161; Sun, 28 Jan 2024 22:23:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706509397; cv=pass; d=google.com; s=arc-20160816; b=DFAq2v50/+V4tEKc7f9edtZR90MDvMb9wzy5EGJ+gIdcpfLle/Hr4grhEsvjVZGEj2 UfEsb5aCv1/q3XillpEgEt69xiP3EXG0fUKxeq0gnKbQrIk3acAjg5OFZUGg1IRKXbgW Tw5BncMnDSjS5xUWSfkq+VOq266qS7IhYzdo7rVW/Gg4HgMhdbquGB7jgpVDWW0a9y/e 8VxNVa46SVFbEcR5d0h55gBmvEdHk2MRAylLpDnesvumpJm4AXGoYg8WgJqSe7ikC18V tz4bJ2qBYDAs5j/ra381pnh8arsQYz4yDjaxqkljLTU0ie4Bq8c0LKpf2zDit3ALidvS Qbug== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-id:precedence:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :dkim-filter; bh=Qh4vCcMbs6V3VMTJ7hXH2GEKRppPqxUeLFTB/Ig1nok=; fh=tZ+d+kaZjTJMarg6XpXtL9/z8hMSaArJbuj7ZVP9x6U=; b=wNoxBap/mgmN19VWA3DMecWHPbW/ZIED350thzCv6t/dCz4aiuBmJthRDyibgHJzja 8FNJy7V0dUZzIBgd1hWO1KDl+He7RrC9ZHK2Ij1QvcolZ/NHEKz7LgxB1NrE/9Ilb5kv MNP3GgcaeHY1PAKo5x0PZWAL5gfa3+kGLNuiQINMn07sY5CdmaCJtA8pPZjWL95W0+VT 7WAx2vWhoE9SiEgf7veRQohQO0qH1Y+rp+A7QDtTF9O+xJuQl3T0KuPacq+UO69V7OMK weba6D4lG4ttsSgQK2anz9EQJt1arpwh8VVn+sfzyMHaXBscMwPopsto8mJXkMcOdHV5 vlJA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=C11V5SYu; arc=pass (i=1 spf=pass spfdomain=linux.microsoft.com dkim=pass dkdomain=linux.microsoft.com dmarc=pass fromdomain=linux.microsoft.com); spf=pass (google.com: domain of linux-kernel+bounces-42225-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-42225-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id c15-20020a05620a164f00b00783dcad7227si5946474qko.517.2024.01.28.22.23.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jan 2024 22:23:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-42225-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=C11V5SYu; arc=pass (i=1 spf=pass spfdomain=linux.microsoft.com dkim=pass dkdomain=linux.microsoft.com dmarc=pass fromdomain=linux.microsoft.com); spf=pass (google.com: domain of linux-kernel+bounces-42225-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-42225-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id EDDFB1C22425 for ; Mon, 29 Jan 2024 06:23:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C8699524C8; Mon, 29 Jan 2024 06:21:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="C11V5SYu" Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 402A84C629; Mon, 29 Jan 2024 06:21:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706509293; cv=none; b=TdkxLIO+TqpwqT/nzDjcuwuvrsKbC6gr979pvOOSZwc5v1CNTYoQmo+TGx5osaaTvt6CiB5Tc2/qTZyCNo+EgN36E1dp4RjLQCGT4I0GTXShlGbbBU0Rllc+2MDT9iOcy6A+3I3SFFG7wV9rbd51wSbtpmTaagYTPcAF88iymUI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706509293; c=relaxed/simple; bh=JWip2UTTjunONm5W+kW2X28AocSMuT6jlRKMeBFBPOA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=fX5EU0Lfhd9zwlczt5Bjt9Tv46hUecpNLQ5ZsxI2vnNbNzr/N3oxfPyGqpc5iX+N+4wo0SfmxpUSCuusgOAe31sFNq+Beik4p4drrM199AGUBBWsFV1PxsIbHKVsJbvwkfshgU6dJJ+lYW8GTCIOnJI8WSiBK406cv3gVmaJkcQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=C11V5SYu; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Received: by linux.microsoft.com (Postfix, from userid 1099) id AFFC420E67D4; Sun, 28 Jan 2024 22:21:26 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com AFFC420E67D4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1706509286; bh=Qh4vCcMbs6V3VMTJ7hXH2GEKRppPqxUeLFTB/Ig1nok=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C11V5SYuf6FExFn0KKr+UA0GfphvYgEKbgmfBpugNq/RJemDlCfcLJHmmENkQaT+r yg+PlOFu/8Uopt6suDXj6TGUz3jdAh0xNQzC1NPPCIuQ10jsrMR+056At5idjoxIs3 +heXAAG4NSNuH3BYJmXuwHz08GVf06NbgCnmo5Y4= From: Souradeep Chakrabarti To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, longli@microsoft.com, yury.norov@gmail.com, leon@kernel.org, cai.huoqing@linux.dev, ssengar@linux.microsoft.com, vkuznets@redhat.com, tglx@linutronix.de, linux-hyperv@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org Cc: schakrabarti@microsoft.com, paulros@microsoft.com Subject: [PATCH 1/4 V3 net-next] cpumask: add cpumask_weight_andnot() Date: Sun, 28 Jan 2024 22:21:04 -0800 Message-Id: <1706509267-17754-2-git-send-email-schakrabarti@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1706509267-17754-1-git-send-email-schakrabarti@linux.microsoft.com> References: <1706509267-17754-1-git-send-email-schakrabarti@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789404797465088680 X-GMAIL-MSGID: 1789404797465088680 From: Yury Norov Similarly to cpumask_weight_and(), cpumask_weight_andnot() is a handy helper that may help to avoid creating an intermediate mask just to calculate number of bits that set in a 1st given mask, and clear in 2nd one. Signed-off-by: Yury Norov Reviewed-by: Jacob Keller --- include/linux/bitmap.h | 12 ++++++++++++ include/linux/cpumask.h | 13 +++++++++++++ lib/bitmap.c | 7 +++++++ 3 files changed, 32 insertions(+) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 99451431e4d6..5814e9ee40ba 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -54,6 +54,7 @@ struct device; * bitmap_full(src, nbits) Are all bits set in *src? * bitmap_weight(src, nbits) Hamming Weight: number set bits * bitmap_weight_and(src1, src2, nbits) Hamming Weight of and'ed bitmap + * bitmap_weight_andnot(src1, src2, nbits) Hamming Weight of andnot'ed bitmap * bitmap_set(dst, pos, nbits) Set specified bit area * bitmap_clear(dst, pos, nbits) Clear specified bit area * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area @@ -169,6 +170,8 @@ bool __bitmap_subset(const unsigned long *bitmap1, unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits); unsigned int __bitmap_weight_and(const unsigned long *bitmap1, const unsigned long *bitmap2, unsigned int nbits); +unsigned int __bitmap_weight_andnot(const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int nbits); void __bitmap_set(unsigned long *map, unsigned int start, int len); void __bitmap_clear(unsigned long *map, unsigned int start, int len); @@ -425,6 +428,15 @@ unsigned long bitmap_weight_and(const unsigned long *src1, return __bitmap_weight_and(src1, src2, nbits); } +static __always_inline +unsigned long bitmap_weight_andnot(const unsigned long *src1, + const unsigned long *src2, unsigned int nbits) +{ + if (small_const_nbits(nbits)) + return hweight_long(*src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)); + return __bitmap_weight_andnot(src1, src2, nbits); +} + static __always_inline void bitmap_set(unsigned long *map, unsigned int start, unsigned int nbits) { diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index cfb545841a2c..228c23eb36d2 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -719,6 +719,19 @@ static inline unsigned int cpumask_weight_and(const struct cpumask *srcp1, return bitmap_weight_and(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits); } +/** + * cpumask_weight_andnot - Count of bits in (*srcp1 & ~*srcp2) + * @srcp1: the cpumask to count bits (< nr_cpu_ids) in. + * @srcp2: the cpumask to count bits (< nr_cpu_ids) in. + * + * Return: count of bits set in both *srcp1 and *srcp2 + */ +static inline unsigned int cpumask_weight_andnot(const struct cpumask *srcp1, + const struct cpumask *srcp2) +{ + return bitmap_weight_andnot(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits); +} + /** * cpumask_shift_right - *dstp = *srcp >> n * @dstp: the cpumask result diff --git a/lib/bitmap.c b/lib/bitmap.c index 09522af227f1..b97692854966 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -348,6 +348,13 @@ unsigned int __bitmap_weight_and(const unsigned long *bitmap1, } EXPORT_SYMBOL(__bitmap_weight_and); +unsigned int __bitmap_weight_andnot(const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int bits) +{ + return BITMAP_WEIGHT(bitmap1[idx] & ~bitmap2[idx], bits); +} +EXPORT_SYMBOL(__bitmap_weight_andnot); + void __bitmap_set(unsigned long *map, unsigned int start, int len) { unsigned long *p = map + BIT_WORD(start);