Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp4587719rwr; Sun, 30 Apr 2023 10:19:18 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4HE1aDYa4Yt9cZDLR4pxWVLLe1d5FW7JDOGKFgdsXS2bvdMfEiG2F4ZYOqt7tTzhykVNYl X-Received: by 2002:a05:6a20:42a2:b0:f2:15f7:a7f8 with SMTP id o34-20020a056a2042a200b000f215f7a7f8mr15038939pzj.2.1682875158278; Sun, 30 Apr 2023 10:19:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682875158; cv=none; d=google.com; s=arc-20160816; b=sTaBjZ4NvZr+CasPa15KvLQaHVw+Ef9Isgo7I2dgQdM14SU/s0uMd/xEdmf8rs+9zS joSE8gQ3aDtRFMdU+qWwfcxyTAQFl69lztD3n8MRa/cEFcgRSG2e1N750Swmzfw5JLeu mVZKHEfTxxcxsy//24TNDDHSLIDpytCPEU8WJhSnhSNvPCA+9ACZCsL6+djrK195k1y8 GAQ12JsjJuSyHsle2gXoFQYpH1Dm6d6hlc28hjQtFbojC1vMsbS9wADlMZ+AM10K4Cq7 RQG4vOoojo5O8zL04I8qyk++D2wEebMR+xzP0DNP0Lx9V5s5YTUueusT60s0xCSR2HiH xNCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VUcX1u9Na09LWEG/YlEb5cTfwitTfjn6pv+M2nzybOg=; b=d+CrIrk+5kooi5lfx/UURVmEHiDsQdwycYcMeEc9Mi7QScp3GpxSNO+59ZqQcLcj99 m1XWnEpxzP8DGZSvPhaaLd+vUGotZaYYnwX+B+oMPITtY+IYw7AORJzxJSUgkYyBix7t /eVT6GTdPttraya4JmED+u8xWUxwi1o1jaK/+WnqxPYZWkLZBF604X8C/hnWLkR/n4DI 8J5OH/gQtcrjCyNkvI+jnBJBMs+nDtl2KtyBt7qiJauMJ0/bCzMyvgjB1g1Y9bl4wcX8 p8chnzXROkndtQZLtgmxoa7XVNQ+wDxwcXk9DHQxKUS/5zLyGKOmLGOgL109rNFcHHP2 SIJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=pRa6oNNk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bs129-20020a632887000000b005200cd1a9bdsi25642417pgb.827.2023.04.30.10.19.04; Sun, 30 Apr 2023 10:19:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=pRa6oNNk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231267AbjD3RS2 (ORCPT + 99 others); Sun, 30 Apr 2023 13:18:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231251AbjD3RSR (ORCPT ); Sun, 30 Apr 2023 13:18:17 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 880A72706; Sun, 30 Apr 2023 10:18:15 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id 98e67ed59e1d1-24ba5c1be6dso1196891a91.2; Sun, 30 Apr 2023 10:18:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682875095; x=1685467095; 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=VUcX1u9Na09LWEG/YlEb5cTfwitTfjn6pv+M2nzybOg=; b=pRa6oNNkpLd1dXDPDAdnGEFDyudn68KHFh/2Ibj9peU1jYkYw+KCmHXKxDOQVmi6f7 bvxqucMU/OlomYLoLnI27rw82VUeME69XiK+3JPmr3/qB/hJKPVCucAKNqR05cZuZ8vF gIIsbSCabbgbBgewuGivVkXrv4lbhgaH40GUryUTbNXuZ7QsKcfWah/ce5ut7N/YP0P7 KKpv4dM+eXR//oWEgz6VUD2nu64ZMZCJTq/BppIvDyn7IbLS0SGl8FWBau83jOrSGnqV jJ+n4XNTfmvTSnnewnAvgeI+ud2/b8sEiWriCRSEAuUvz13spcUZhEt5FUKPKDoREY10 kXuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682875095; x=1685467095; 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=VUcX1u9Na09LWEG/YlEb5cTfwitTfjn6pv+M2nzybOg=; b=bWMwl74Gnx63e3Wyr184orNYEzp3+TNk5wrilulDqIuqTNpf73V9isqANjTd86mlZ4 BezNBtHM52gc5jvSjxv+2yKHOmfPBil2eLeV1NVub6j7pE31umRyywhKX3VjjldzT5Wt C/GBcL2aO5hfqwA1Dx7z1w0vsUe9qFEAeiRsiXoLYTMb6QHSmXuLJ/xL+N832V45akqs kfg+j/5b9F/okr5y331K9bi0MTK84Iga0h7we7swKRdps7xJMVi5USssNXk4mQkUM9AC M38f4/1LgN8FzvCnT5HMxd/VyMmIGOOqa+4XYNUtT3gcq4Vmhu9p5Elk4Xa52Yw79T6n kZ4g== X-Gm-Message-State: AC+VfDx6SM/dS1sx4SofvHZc3bctoMl+aRUkO2wsoKvVHwaLT+AD7Bb8 M2SzT3vyDw3nfzmvaDjPtGc= X-Received: by 2002:a17:902:d505:b0:1a9:9c5d:9fac with SMTP id b5-20020a170902d50500b001a99c5d9facmr14847770plg.33.1682875094901; Sun, 30 Apr 2023 10:18:14 -0700 (PDT) Received: from localhost ([4.1.102.3]) by smtp.gmail.com with ESMTPSA id jd9-20020a170903260900b001a681fb3e77sm16155349plb.44.2023.04.30.10.18.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Apr 2023 10:18:14 -0700 (PDT) From: Yury Norov To: Jakub Kicinski , netdev@vger.kernel.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Yury Norov , Saeed Mahameed , Pawel Chmielewski , Leon Romanovsky , "David S. Miller" , Eric Dumazet , Paolo Abeni , Andy Shevchenko , Rasmus Villemoes , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Tariq Toukan , Gal Pressman , Greg Kroah-Hartman , Heiko Carstens , Barry Song Subject: [PATCH v3 2/8] lib/find: add find_next_and_andnot_bit() Date: Sun, 30 Apr 2023 10:18:03 -0700 Message-Id: <20230430171809.124686-3-yury.norov@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230430171809.124686-1-yury.norov@gmail.com> References: <20230430171809.124686-1-yury.norov@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Similarly to find_nth_and_andnot_bit(), find_next_and_andnot_bit() is a convenient helper that allows traversing bitmaps without storing intermediate results in a temporary bitmap. In the following patches the function is used to implement NUMA-aware CPUs enumeration. Signed-off-by: Yury Norov --- include/linux/find.h | 43 +++++++++++++++++++++++++++++++++++++++++++ lib/find_bit.c | 12 ++++++++++++ 2 files changed, 55 insertions(+) diff --git a/include/linux/find.h b/include/linux/find.h index 5e4f39ef2e72..90b68d76c073 100644 --- a/include/linux/find.h +++ b/include/linux/find.h @@ -16,6 +16,9 @@ unsigned long _find_next_andnot_bit(const unsigned long *addr1, const unsigned l unsigned long nbits, unsigned long start); unsigned long _find_next_or_bit(const unsigned long *addr1, const unsigned 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 nbits, + unsigned long start); unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits, unsigned long start); extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size); @@ -159,6 +162,40 @@ unsigned long find_next_or_bit(const unsigned long *addr1, } #endif +#ifndef find_next_and_andnot_bit +/** + * find_next_and_andnot_bit - find the next bit set 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 third address to base the search on + * @size: The bitmap size in bits + * @offset: The bitnumber to start searching at + * + * Return: the bit number for the next set bit + * If no bits are set, returns @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 >= size)) + return size; + + val = *addr1 & *addr2 & ~*addr3 & GENMASK(size - 1, offset); + return val ? __ffs(val) : size; + } + + return _find_next_and_andnot_bit(addr1, addr2, addr3, size, offset); +} +#endif + #ifndef find_next_zero_bit /** * find_next_zero_bit - find the next cleared bit in a memory region @@ -568,6 +605,12 @@ unsigned long find_next_bit_le(const void *addr, unsigned (bit) = find_next_andnot_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\ (bit)++) +#define for_each_and_andnot_bit(bit, addr1, addr2, addr3, size) \ + for ((bit) = 0; \ + (bit) = find_next_and_andnot_bit((addr1), (addr2), (addr3), (size), (bit)),\ + (bit) < (size); \ + (bit)++) + #define for_each_or_bit(bit, addr1, addr2, size) \ for ((bit) = 0; \ (bit) = find_next_or_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\ diff --git a/lib/find_bit.c b/lib/find_bit.c index 32f99e9a670e..4403e00890b1 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -182,6 +182,18 @@ unsigned long _find_next_andnot_bit(const unsigned long *addr1, const unsigned l EXPORT_SYMBOL(_find_next_andnot_bit); #endif +#ifndef find_next_and_andnot_bit +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 */, nbits, start); +} +EXPORT_SYMBOL(_find_next_and_andnot_bit); +#endif + #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) -- 2.37.2