From patchwork Sat Dec 18 21:20:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Norov X-Patchwork-Id: 12686395 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E7116C43217 for ; Sat, 18 Dec 2021 21:22:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=7N1zUrGL41Nq3YA+7c7CfJndE6x24KAEClIguE1CV9E=; b=e53IcjOIHwVRnW Zfrv4/qeKpAPTplSR4qftJJoqX/BI+GiiNXk83+ppaVyM71qULf987PhYCFI7M8VHxwkkCAE31BKi b9NJAygech+vYkEU644RTPHHiGTXECHKr3mKPOoREWYLePKB094HcjHKZdNRjXpipL+CD+PR4vjNb kBbx4W6CtjE4PVltzUweAcpP7kdil/ugPXPIZvQkTVa5IGl2HSKXccqqn894sWt87iQPBXBdqIbYt 8p3F/cYHd7hgj26FOgCKFAu6613P1YGzzmJQWgrHA4dw5P33evvMkxEMvC8vb9lfgBPg33yijv68s K0Q03Ekf4N/yGYzjbrHA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1myh9w-00El5b-2U; Sat, 18 Dec 2021 21:22:28 +0000 Received: from mail-ot1-x333.google.com ([2607:f8b0:4864:20::333]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1myh8f-00Ek7k-GY; Sat, 18 Dec 2021 21:21:11 +0000 Received: by mail-ot1-x333.google.com with SMTP id r10-20020a056830080a00b0055c8fd2cebdso7463766ots.6; Sat, 18 Dec 2021 13:21:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=cEbjV/8Ctr1QdXazsGfe+PbDs+iZrrBmmo9YNkbLBy0=; b=RKeJvy+TVkzSfE1DWFH8SBdk923FOhJaAQ0Dv9PrOLQIHftv5DaeBCB4tNKlvmYIPN nbkzjP+1Trtphdpdl224Vn1sYOGVLTg43YwIfJM5nH7KPfFHi7vCRPN0TaIMV0CuMzT2 orCQwO8bJ9qk1nVA1GCOdVDNRIyHknNBzcXqS9rgvqtOTbjhHhWXGI5No2Z0UTz3TByx 0Ag3UOeLtIQjs7s0rKEZ/24IhDlJLiBtoBDajcozUIGSHrQXSkW6AtgOu9kPk/Lq3qoc U8VDenhPvveU8b2bWmVJzG2Nt6HLvQlM90dIQDx+rucC4nLouHJTrpPqTNt21VK+DFl2 nNhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cEbjV/8Ctr1QdXazsGfe+PbDs+iZrrBmmo9YNkbLBy0=; b=loI7MRJKq1LJEXI6DCNShfhUBXEUmT2IVP8m7ew77FU1vEhashEDSnf0cJ5CmsjlQF dXRL7AhJrdVrTvKrCEswWMAaFNuolwJ7mvUV7hZbBZ7Y9825WoSCPN7l3m20PYuzaB8v gO0ksFROS7UIeeZ7PS9KhYof6f4ogmzdP28pYQ9NQjOL5k4Z2CWuak+WaIE1r4t4CVTp sJ7NhaZRfVfl9cGCHge5Vi3j72vrcxJxvloTUHtQjCwzhJMMa4grx7RNoPwk+reQKcGX UTEqsibPzfOgG45+BOKJ7vlSA50UDWbCgH9kTMICydCC6j6eeKzQjv56nDArX5+VOVv3 dMqA== X-Gm-Message-State: AOAM533YMpe2qTwq9uQtZtVx9WYHSiCoFqJ0wAstWP566TKXl80yR3Y5 ikKVwe1VZx1eCLWLC07d59A= X-Google-Smtp-Source: ABdhPJwWLUowWO6z/6XzIWLGx6yfCwM40JettYuf4PfjeM7nkzCXO3jf+T1ejyUHBwjqYOIYE68+UA== X-Received: by 2002:a9d:82a:: with SMTP id 39mr6499375oty.282.1639862468685; Sat, 18 Dec 2021 13:21:08 -0800 (PST) Received: from localhost (searspoint.nvidia.com. [216.228.112.21]) by smtp.gmail.com with ESMTPSA id b17sm2560486ots.66.2021.12.18.13.21.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Dec 2021 13:21:08 -0800 (PST) From: Yury Norov To: linux-kernel@vger.kernel.org, Yury Norov , "James E.J. Bottomley" , "Martin K. Petersen" , =?utf-8?b?TWljaGHFgiBN?= =?utf-8?b?aXJvc8WCYXc=?= , "Paul E. McKenney" , "Rafael J. Wysocki" , Alexander Shishkin , Alexey Klimov , Amitkumar Karwar , Andi Kleen , Andrew Lunn , Andrew Morton , Andy Gross , Andy Lutomirski , Andy Shevchenko , Anup Patel , Ard Biesheuvel , Arnaldo Carvalho de Melo , Arnd Bergmann , Borislav Petkov , Catalin Marinas , Christoph Hellwig , Christoph Lameter , Daniel Vetter , Dave Hansen , David Airlie , David Laight , Dennis Zhou , Emil Renner Berthing , Geert Uytterhoeven , Geetha sowjanya , Greg Kroah-Hartman , Guo Ren , Hans de Goede , Heiko Carstens , Ian Rogers , Ingo Molnar , Jakub Kicinski , Jason Wessel , Jens Axboe , Jiri Olsa , Joe Perches , Jonathan Cameron , Juri Lelli , Kees Cook , Krzysztof Kozlowski , Lee Jones , Marc Zyngier , Marcin Wojtas , Mark Gross , Mark Rutland , Matti Vaittinen , Mauro Carvalho Chehab , Mel Gorman , Michael Ellerman , Mike Marciniszyn , Nicholas Piggin , Palmer Dabbelt , Peter Zijlstra , Petr Mladek , Randy Dunlap , Rasmus Villemoes , Russell King , Saeed Mahameed , Sagi Grimberg , Sergey Senozhatsky , Solomon Peachy , Stephen Boyd , Stephen Rothwell , Steven Rostedt , Subbaraya Sundeep , Sudeep Holla , Sunil Goutham , Tariq Toukan , Tejun Heo , Thomas Bogendoerfer , Thomas Gleixner , Ulf Hansson , Vincent Guittot , Vineet Gupta , Viresh Kumar , Vivien Didelot , Vlastimil Babka , Will Deacon , bcm-kernel-feedback-list@broadcom.com, kvm@vger.kernel.org, linux-alpha@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-crypto@vger.kernel.org, linux-csky@vger.kernel.org, linux-ia64@vger.kernel.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-perf-users@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-snps-arc@lists.infradead.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 16/17] tools/bitmap: sync bitmap_weight Date: Sat, 18 Dec 2021 13:20:12 -0800 Message-Id: <20211218212014.1315894-17-yury.norov@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211218212014.1315894-1-yury.norov@gmail.com> References: <20211218212014.1315894-1-yury.norov@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211218_132109_619992_7460A5F0 X-CRM114-Status: GOOD ( 19.24 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Pull bitmap_weight_{cmp,eq,gt,ge,lt,le} from mother kernel and use where applicable. Signed-off-by: Yury Norov --- tools/include/linux/bitmap.h | 44 ++++++++++++++++++++++++++++++++++++ tools/lib/bitmap.c | 20 ++++++++++++++++ tools/perf/util/pmu.c | 2 +- 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/tools/include/linux/bitmap.h b/tools/include/linux/bitmap.h index ea97804d04d4..e8ae9a85d555 100644 --- a/tools/include/linux/bitmap.h +++ b/tools/include/linux/bitmap.h @@ -12,6 +12,8 @@ unsigned long name[BITS_TO_LONGS(bits)] int __bitmap_weight(const unsigned long *bitmap, int bits); +int __bitmap_weight_cmp(const unsigned long *bitmap, unsigned int bits, + unsigned int num); void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, const unsigned long *bitmap2, int bits); int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, @@ -68,6 +70,48 @@ static inline int bitmap_weight(const unsigned long *src, unsigned int nbits) return __bitmap_weight(src, nbits); } +static __always_inline +int bitmap_weight_cmp(const unsigned long *src, unsigned int nbits, int num) +{ + if (num > (int)nbits || num < 0) + return -num; + + if (small_const_nbits(nbits)) + return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits)) - num; + + return __bitmap_weight_cmp(src, nbits, num); +} + +static __always_inline +bool bitmap_weight_eq(const unsigned long *src, unsigned int nbits, int num) +{ + return bitmap_weight_cmp(src, nbits, num) == 0; +} + +static __always_inline +bool bitmap_weight_gt(const unsigned long *src, unsigned int nbits, int num) +{ + return bitmap_weight_cmp(src, nbits, num) > 0; +} + +static __always_inline +bool bitmap_weight_ge(const unsigned long *src, unsigned int nbits, int num) +{ + return bitmap_weight_cmp(src, nbits, num - 1) > 0; +} + +static __always_inline +bool bitmap_weight_lt(const unsigned long *src, unsigned int nbits, int num) +{ + return bitmap_weight_cmp(src, nbits, num - 1) <= 0; +} + +static __always_inline +bool bitmap_weight_le(const unsigned long *src, unsigned int nbits, int num) +{ + return bitmap_weight_cmp(src, nbits, num) <= 0; +} + static inline void bitmap_or(unsigned long *dst, const unsigned long *src1, const unsigned long *src2, unsigned int nbits) { diff --git a/tools/lib/bitmap.c b/tools/lib/bitmap.c index db466ef7be9d..06e58fee8523 100644 --- a/tools/lib/bitmap.c +++ b/tools/lib/bitmap.c @@ -18,6 +18,26 @@ int __bitmap_weight(const unsigned long *bitmap, int bits) return w; } +int __bitmap_weight_cmp(const unsigned long *bitmap, unsigned int bits, int num) +{ + unsigned int k, w, lim = bits / BITS_PER_LONG; + + for (k = 0, w = 0; k < lim; k++) { + if (w + bits - k * BITS_PER_LONG < num) + goto out; + + w += hweight_long(bitmap[k]); + + if (w > num) + goto out; + } + + if (bits % BITS_PER_LONG) + w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits)); +out: + return w - num; +} + void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, const unsigned long *bitmap2, int bits) { diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 6ae58406f4fc..015ee1321c7c 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1314,7 +1314,7 @@ static int pmu_config_term(const char *pmu_name, */ if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { if (term->no_value && - bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { + bitmap_weight_gt(format->bits, PERF_PMU_FORMAT_BITS, 1)) { if (err) { parse_events_error__handle(err, term->err_val, strdup("no value assigned for term"),