Received: by 2002:ab2:6991:0:b0:1f2:fff1:ace7 with SMTP id v17csp177907lqo; Wed, 27 Mar 2024 09:56:01 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV9g2yGP/vsGWsD2aDjMLnU3rT6vK/AZBlOs0tQSveh8mFZbh/5Fvj7ahqDXCdw5jDGrrXrrDD/eqfTl442HQ3ySkMHiMrvTJmmXkc8qg== X-Google-Smtp-Source: AGHT+IEovZW49/YLlxCDOBhXYKlpPHFSwJNWd9i2ah9d1R0sq2sfqu7RO8lxTYfJ3LwhrjLexcV5 X-Received: by 2002:a05:6a20:914f:b0:1a5:6a89:1d31 with SMTP id x15-20020a056a20914f00b001a56a891d31mr26251pzc.46.1711558561059; Wed, 27 Mar 2024 09:56:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711558561; cv=pass; d=google.com; s=arc-20160816; b=w8IEdHNdEiR3C7OeL42POp9rbPvVra/Fxaf12POh6nmyYcHQ5+MGWN7+WzAvQZb8Ee QZGjRvq+oaDKH0UJ3pVl8RewoPSLGU0ZwnpMJQnwRytHqBsmxazqsmrPGug93QnbDnXD BapPgJoiEINUFCy8+nPFtK25edQD0TldoZO5KAN6n3+xJ+U9uSUecW931IFsgfatpzBn FX05dNvoFkWYcT5wYMsvqymgJc62p8jsr7QPcRxJBTwlJIYaYXg0IS4mshfT/XWaXURn ky6tseyBfwpmpOdmhkMjmdu5Me0338eRlF9FO0ceUYaNXTqmv6YXNyUfSAZHcAf0VBAj 6Rig== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=l1gXoK9APxYS6FbVe5ubn4ADd6h1Xviqg0ke3xKCtyQ=; fh=Z7nwqFw9jo7mwUVP6sFBJfcE9oHe3fvGcHvWZvLmnnc=; b=kDbtfws+z6H2d3EtQp/rvoPsxJGL6QxdYrVJcK5Mg3WE8dQugycO200oN8UBG5R4ET vs0igru/lRMAvQvhJtjWL1q0OtSE2tLT6ymE4ixRQw8MGz0dnOVK90ueSOJ6J1snhUB2 NuV1wmZ7yeLPO0XujFzedSlWm7ebo6o/+i56VbRDHA56+/FEkored/03njTkZVYte6N2 uxpVnn/N119q0UWdST/M9u/7tZnT/hGyhbLjfai81f/2BDC8nZI/+mqisOkVKEG4mfTy lygO+aMkvVS5ECEpX2gyufbD0xPY9utR5Az80auHGeBnc1NhIyRH9PuOZUqnhP+4WYZ+ I4Gg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=N9TwqbfO; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-121522-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121522-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id w12-20020a63490c000000b005dc81a16015si11640114pga.118.2024.03.27.09.56.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 09:56:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-121522-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=N9TwqbfO; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-121522-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121522-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 10EBDB2F098 for ; Wed, 27 Mar 2024 15:35:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9464C148FE0; Wed, 27 Mar 2024 15:25:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="N9TwqbfO" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D17C6148851; Wed, 27 Mar 2024 15:25:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711553114; cv=none; b=c8YN2WxwaKCYE4gklxKaBMG+COGl7og2ejqURWNwLhNVsC53OCYHz8x8Yc5KDhGjqkPT1g9pqIEimc1jRsffnQvemb3wy7wWLrc5Ln1S+7brWe/XKFg/BE6slTnrQ8XovShXIHFby+9JhGMGCoR8FMsAS917OFGN9QM2mPbpEiA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711553114; c=relaxed/simple; bh=LrM4WoPjFzvxV5b6y4imbfLAlZQOHgrFLpfIW2DTmBk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I5JuouqBA2SOmgI0K/oJx9es+nzobC/iaDgyslC1C97TyszkYiJAuCvQtqABef/gdwCLJFD0QHjFgJbudQa2MO9/637bq1uofJeHlICZpXvFXYsr2fRdsi8WKUu0eiqS8hwAh8AmmxcKHuKXS6hkZd1MtH4qL3kT3mBMmlvRgH0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=N9TwqbfO; arc=none smtp.client-ip=198.175.65.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711553113; x=1743089113; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LrM4WoPjFzvxV5b6y4imbfLAlZQOHgrFLpfIW2DTmBk=; b=N9TwqbfO0j1Z6MJG/L7S8OMTu4mC7ZeUMLIIvJ2l3DLQ40vU8nkYXhX/ UA8xMjczo39zaoqkSlfmER0TVZLyWE6VuE/NCcvPR6BHuJubiSOvZ4j2a o4iV5IlmHA2iCl8MDhToa9nrkmDq+fz+htlAnOG//bPgBBs/SlovMjmik R7qOahiXZvCVd/SM/JtAX6JX4ajkmi0ultB1PZ2C+zEHLrjVjOk1Hs1Hc yXQM04FYzYKUbNU+FzahpJLcQsXvWeLT6knmzqmHMGSjfFivi8jrgXyqb ShKIBE2jH10U+9sQA2liZ5VfieOVBydpdTN7Gap8mnLI4IdzYs8M71K6W Q==; X-CSE-ConnectionGUID: yb/6ryPkTpmvaq01MGw7Nw== X-CSE-MsgGUID: PoAiEM7aQta12R2fv9Mhvg== X-IronPort-AV: E=McAfee;i="6600,9927,11026"; a="6518217" X-IronPort-AV: E=Sophos;i="6.07,159,1708416000"; d="scan'208";a="6518217" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Mar 2024 08:25:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,159,1708416000"; d="scan'208";a="16414293" Received: from newjersey.igk.intel.com ([10.102.20.203]) by fmviesa008.fm.intel.com with ESMTP; 27 Mar 2024 08:25:10 -0700 From: Alexander Lobakin To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Alexander Lobakin , Yury Norov , Alexander Potapenko , nex.sw.ncis.osdt.itp.upstreaming@intel.com, intel-wired-lan@lists.osuosl.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Przemek Kitszel Subject: [PATCH net-next v6 12/21] bitmap: introduce generic optimized bitmap_size() Date: Wed, 27 Mar 2024 16:23:49 +0100 Message-ID: <20240327152358.2368467-13-aleksander.lobakin@intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240327152358.2368467-1-aleksander.lobakin@intel.com> References: <20240327152358.2368467-1-aleksander.lobakin@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The number of times yet another open coded `BITS_TO_LONGS(nbits) * sizeof(long)` can be spotted is huge. Some generic helper is long overdue. Add one, bitmap_size(), but with one detail. BITS_TO_LONGS() uses DIV_ROUND_UP(). The latter works well when both divident and divisor are compile-time constants or when the divisor is not a pow-of-2. When it is however, the compilers sometimes tend to generate suboptimal code (GCC 13): 48 83 c0 3f add $0x3f,%rax 48 c1 e8 06 shr $0x6,%rax 48 8d 14 c5 00 00 00 00 lea 0x0(,%rax,8),%rdx %BITS_PER_LONG is always a pow-2 (either 32 or 64), but GCC still does full division of `nbits + 63` by it and then multiplication by 8. Instead of BITS_TO_LONGS(), use ALIGN() and then divide by 8. GCC: 8d 50 3f lea 0x3f(%rax),%edx c1 ea 03 shr $0x3,%edx 81 e2 f8 ff ff 1f and $0x1ffffff8,%edx Now it shifts `nbits + 63` by 3 positions (IOW performs fast division by 8) and then masks bits[2:0]. bloat-o-meter: add/remove: 0/0 grow/shrink: 20/133 up/down: 156/-773 (-617) Clang does it better and generates the same code before/after starting from -O1, except that with the ALIGN() approach it uses %edx and thus still saves some bytes: add/remove: 0/0 grow/shrink: 9/133 up/down: 18/-538 (-520) Note that we can't expand DIV_ROUND_UP() by adding a check and using this approach there, as it's used in array declarations where expressions are not allowed. Add this helper to tools/ as well. Reviewed-by: Przemek Kitszel Acked-by: Yury Norov Signed-off-by: Alexander Lobakin --- include/linux/bitmap.h | 8 +++++--- include/linux/cpumask.h | 2 +- tools/include/linux/bitmap.h | 7 ++++--- drivers/md/dm-clone-metadata.c | 5 ----- drivers/s390/cio/idset.c | 2 +- lib/math/prime_numbers.c | 2 -- 6 files changed, 11 insertions(+), 15 deletions(-) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 914c23e96f26..363e0b184a45 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -226,9 +226,11 @@ void bitmap_fold(unsigned long *dst, const unsigned long *orig, #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1))) +#define bitmap_size(nbits) (ALIGN(nbits, BITS_PER_LONG) / BITS_PER_BYTE) + static inline void bitmap_zero(unsigned long *dst, unsigned int nbits) { - unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); + unsigned int len = bitmap_size(nbits); if (small_const_nbits(nbits)) *dst = 0; @@ -238,7 +240,7 @@ static inline void bitmap_zero(unsigned long *dst, unsigned int nbits) static inline void bitmap_fill(unsigned long *dst, unsigned int nbits) { - unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); + unsigned int len = bitmap_size(nbits); if (small_const_nbits(nbits)) *dst = ~0UL; @@ -249,7 +251,7 @@ static inline void bitmap_fill(unsigned long *dst, unsigned int nbits) static inline void bitmap_copy(unsigned long *dst, const unsigned long *src, unsigned int nbits) { - unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); + unsigned int len = bitmap_size(nbits); if (small_const_nbits(nbits)) *dst = *src; diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index 1c29947db848..6519f9c77709 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -853,7 +853,7 @@ static inline int cpulist_parse(const char *buf, struct cpumask *dstp) */ static inline unsigned int cpumask_size(void) { - return BITS_TO_LONGS(large_cpumask_bits) * sizeof(long); + return bitmap_size(large_cpumask_bits); } /* diff --git a/tools/include/linux/bitmap.h b/tools/include/linux/bitmap.h index 8c6852dba04f..210c13b1b857 100644 --- a/tools/include/linux/bitmap.h +++ b/tools/include/linux/bitmap.h @@ -26,13 +26,14 @@ bool __bitmap_intersects(const unsigned long *bitmap1, #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1))) +#define bitmap_size(nbits) (ALIGN(nbits, BITS_PER_LONG) / BITS_PER_BYTE) + static inline void bitmap_zero(unsigned long *dst, unsigned int nbits) { if (small_const_nbits(nbits)) *dst = 0UL; else { - int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); - memset(dst, 0, len); + memset(dst, 0, bitmap_size(nbits)); } } @@ -84,7 +85,7 @@ static inline void bitmap_or(unsigned long *dst, const unsigned long *src1, */ static inline unsigned long *bitmap_zalloc(int nbits) { - return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long)); + return calloc(1, bitmap_size(nbits)); } /* diff --git a/drivers/md/dm-clone-metadata.c b/drivers/md/dm-clone-metadata.c index c43d55672bce..47c1fa7aad8b 100644 --- a/drivers/md/dm-clone-metadata.c +++ b/drivers/md/dm-clone-metadata.c @@ -465,11 +465,6 @@ static void __destroy_persistent_data_structures(struct dm_clone_metadata *cmd) /*---------------------------------------------------------------------------*/ -static size_t bitmap_size(unsigned long nr_bits) -{ - return BITS_TO_LONGS(nr_bits) * sizeof(long); -} - static int __dirty_map_init(struct dirty_map *dmap, unsigned long nr_words, unsigned long nr_regions) { diff --git a/drivers/s390/cio/idset.c b/drivers/s390/cio/idset.c index 0a1105a483bf..e5f28370a903 100644 --- a/drivers/s390/cio/idset.c +++ b/drivers/s390/cio/idset.c @@ -18,7 +18,7 @@ struct idset { static inline unsigned long idset_bitmap_size(int num_ssid, int num_id) { - return BITS_TO_LONGS(num_ssid * num_id) * sizeof(unsigned long); + return bitmap_size(size_mul(num_ssid, num_id)); } static struct idset *idset_new(int num_ssid, int num_id) diff --git a/lib/math/prime_numbers.c b/lib/math/prime_numbers.c index d42cebf7407f..d3b64b10da1c 100644 --- a/lib/math/prime_numbers.c +++ b/lib/math/prime_numbers.c @@ -6,8 +6,6 @@ #include #include -#define bitmap_size(nbits) (BITS_TO_LONGS(nbits) * sizeof(unsigned long)) - struct primes { struct rcu_head rcu; unsigned long last, sz; -- 2.44.0