From: Yury Norov <yury.norov@gmail.com>
To: Alexander Lobakin <aleksander.lobakin@intel.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Alexander Potapenko <glider@google.com>,
Jakub Kicinski <kuba@kernel.org>,
Eric Dumazet <edumazet@google.com>,
David Ahern <dsahern@kernel.org>,
Przemek Kitszel <przemyslaw.kitszel@intel.com>,
Simon Horman <simon.horman@corigine.com>,
netdev@vger.kernel.org, linux-btrfs@vger.kernel.org,
dm-devel@redhat.com, ntfs3@lists.linux.dev,
linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 03/14] bitops: let the compiler optimize __assign_bit()
Date: Mon, 9 Oct 2023 09:18:40 -0700 [thread overview]
Message-ID: <ZSQn4Mppz9aJgFib@yury-ThinkPad> (raw)
In-Reply-To: <20231009151026.66145-4-aleksander.lobakin@intel.com>
On Mon, Oct 09, 2023 at 05:10:15PM +0200, Alexander Lobakin wrote:
> Since commit b03fc1173c0c ("bitops: let optimize out non-atomic bitops
> on compile-time constants"), the compilers are able to expand inline
> bitmap operations to compile-time initializers when possible.
> However, during the round of replacement if-__set-else-__clear with
> __assign_bit() as per Andy's advice, bloat-o-meter showed +1024 bytes
> difference in object code size for one module (even one function),
> where the pattern:
>
> DECLARE_BITMAP(foo) = { }; // on the stack, zeroed
>
> if (a)
> __set_bit(const_bit_num, foo);
> if (b)
> __set_bit(another_const_bit_num, foo);
> ...
>
> is heavily used, although there should be no difference: the bitmap is
> zeroed, so the second half of __assign_bit() should be compiled-out as
> a no-op.
> I either missed the fact that __assign_bit() has bitmap pointer marked
> as `volatile` (as we usually do for bitmaps) or was hoping that the
No, we usually don't. Atomic ops on individual bits is a notable exception
for bitmaps, as the comment for generic_test_bit() says, for example:
/*
* Unlike the bitops with the '__' prefix above, this one *is* atomic,
* so `volatile` must always stay here with no cast-aways. See
* `Documentation/atomic_bitops.txt` for the details.
*/
For non-atomic single-bit operations and all multi-bit ops, volatile is
useless, and generic___test_and_set_bit() in the same file casts the
*addr to a plain 'unsigned long *'.
> compilers would at least try to look past the `volatile` for
> __always_inline functions. Anyhow, due to that attribute, the compilers
> were always compiling the whole expression and no mentioned compile-time
> optimizations were working.
>
> Convert __assign_bit() to a macro since it's a very simple if-else and
> all of the checks are performed inside __set_bit() and __clear_bit(),
> thus that wrapper has to be as transparent as possible. After that
> change, despite it showing only -20 bytes change for vmlinux (due to
> that it's still relatively unpopular), no drastic code size changes
> happen when replacing if-set-else-clear for onstack bitmaps with
> __assign_bit(), meaning the compiler now expands them to the actual
> operations will all the expected optimizations.
>
> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
> Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
> ---
> include/linux/bitops.h | 10 ++--------
> 1 file changed, 2 insertions(+), 8 deletions(-)
>
> diff --git a/include/linux/bitops.h b/include/linux/bitops.h
> index e0cd09eb91cd..f98f4fd1047f 100644
> --- a/include/linux/bitops.h
> +++ b/include/linux/bitops.h
> @@ -284,14 +284,8 @@ static __always_inline void assign_bit(long nr, volatile unsigned long *addr,
> clear_bit(nr, addr);
> }
>
> -static __always_inline void __assign_bit(long nr, volatile unsigned long *addr,
> - bool value)
> -{
> - if (value)
> - __set_bit(nr, addr);
> - else
> - __clear_bit(nr, addr);
> -}
> +#define __assign_bit(nr, addr, value) \
> + ((value) ? __set_bit(nr, addr) : __clear_bit(nr, addr))
Can you protect nr and addr with braces just as well?
Can you convert the atomic version too, to keep them synchronized ?
>
> /**
> * __ptr_set_bit - Set bit in a pointer's value
> --
> 2.41.0
next prev parent reply other threads:[~2023-10-09 16:18 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-09 15:10 [PATCH 00/14] ip_tunnel: convert __be16 tunnel flags to bitmaps Alexander Lobakin
2023-10-09 15:10 ` [PATCH 01/14] bitops: add missing prototype check Alexander Lobakin
2023-10-09 15:10 ` [PATCH 02/14] bitops: make BYTES_TO_BITS() treewide-available Alexander Lobakin
2023-10-09 15:10 ` [PATCH 03/14] bitops: let the compiler optimize __assign_bit() Alexander Lobakin
2023-10-09 16:18 ` Yury Norov [this message]
2023-10-11 7:25 ` Alexander Lobakin
2023-10-09 15:10 ` [PATCH 04/14] linkmode: convert linkmode_{test,set,clear,mod}_bit() to macros Alexander Lobakin
2023-10-09 15:10 ` [PATCH 05/14] s390/cio: rename bitmap_size() -> idset_bitmap_size() Alexander Lobakin
2023-10-09 16:35 ` Yury Norov
2023-10-11 7:28 ` Alexander Lobakin
2023-10-09 15:10 ` [PATCH 06/14] fs/ntfs3: rename bitmap_size() -> ntfs3_bitmap_size() Alexander Lobakin
2023-10-09 16:50 ` Yury Norov
2023-10-11 7:36 ` Alexander Lobakin
2023-10-09 15:10 ` [PATCH 07/14] btrfs: rename bitmap_set_bits() -> btrfs_bitmap_set_bits() Alexander Lobakin
2023-10-09 15:23 ` David Sterba
2023-10-09 15:10 ` [PATCH 08/14] bitmap: introduce generic optimized bitmap_size() Alexander Lobakin
2023-10-09 17:04 ` Yury Norov
2023-10-09 15:10 ` [PATCH 09/14] bitmap: extend bitmap_{get,set}_value8() to bitmap_{get,set}_bits() Alexander Lobakin
2023-10-09 16:31 ` Yury Norov
2023-10-11 9:33 ` Alexander Lobakin
2023-10-11 10:36 ` Andy Shevchenko
2023-10-15 2:20 ` Yury Norov
2023-10-09 15:10 ` [PATCH 10/14] ip_tunnel: use a separate struct to store tunnel params in the kernel Alexander Lobakin
2023-10-09 15:10 ` [PATCH 11/14] ip_tunnel: convert __be16 tunnel flags to bitmaps Alexander Lobakin
2023-10-09 15:10 ` [PATCH 12/14] lib/bitmap: add compile-time test for __assign_bit() optimization Alexander Lobakin
2023-10-09 15:10 ` [PATCH 13/14] lib/bitmap: add tests for bitmap_{get,set}_bits() Alexander Lobakin
2023-10-09 15:10 ` [PATCH 14/14] lib/bitmap: add tests for IP tunnel flags conversion helpers Alexander Lobakin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZSQn4Mppz9aJgFib@yury-ThinkPad \
--to=yury.norov@gmail.com \
--cc=aleksander.lobakin@intel.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=dm-devel@redhat.com \
--cc=dsahern@kernel.org \
--cc=edumazet@google.com \
--cc=glider@google.com \
--cc=kuba@kernel.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=netdev@vger.kernel.org \
--cc=ntfs3@lists.linux.dev \
--cc=przemyslaw.kitszel@intel.com \
--cc=simon.horman@corigine.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).