[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] x86/bitops: Improve code generation for __scanbit()


  • To: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Tue, 10 Nov 2026 15:26:35 +0100
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=google header.d=suse.com header.i="@suse.com" header.h="Content-Transfer-Encoding:Content-Type:In-Reply-To:Autocrypt:From:Content-Language:References:Cc:To:Subject:User-Agent:MIME-Version:Date:Message-ID"
  • Autocrypt: addr=jbeulich@xxxxxxxx; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL
  • Cc: Roger Pau Monné <roger@xxxxxxxxxxxxxx>, Teddy Astie <teddy.astie@xxxxxxxxxx>, Xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • Delivery-date: Tue, 10 Nov 2026 14:26:45 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 10.11.2026 13:14, Andrew Cooper wrote:
> On 09/11/2026 8:46 am, Jan Beulich wrote:
>> On 06.11.2026 18:12, Andrew Cooper wrote:
>>> There's no need for an alternative.  Use the same technique as arch_ffs() by
>>> pre-loading the destination register using the caller chosen "zero case"
>>> instead of -1.
>>>
>>> No functional change.
>>>
>>> Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
>>> ---
>>> CC: Jan Beulich <jbeulich@xxxxxxxx>
>>> CC: Roger Pau Monné <roger@xxxxxxxxxxxxxx>
>>> CC: Teddy Astie <teddy.astie@xxxxxxxxxx>
>>>
>>> Slightly RFC.  I don't particualrly like forward-declaring ffsl(), but I 
>>> don't
>>> see another option while maintaining a non-tangled common/arch header split.
>> Forward-declaring isn't a problem by itself, just that imo that wants putting
>> in xen/bitops.h, not ...
>>
>>> --- a/xen/arch/x86/include/asm/bitops.h
>>> +++ b/xen/arch/x86/include/asm/bitops.h
>>> @@ -320,18 +320,35 @@ extern unsigned int __find_first_zero_bit(
>>>  extern unsigned int __find_next_zero_bit(
>>>      const unsigned long *addr, unsigned int size, unsigned int offset);
>>>  
>>> +static attr_const unsigned int ffsl(unsigned long x);
>> ... here. asm/bitops.h already isn't included at the very top of the file, so
>> putting the decl there looks clean enough to me.
>>
>> (Another alternative would be to see about doing away with __scanbit().)
> 
> I'd like to do away with it.  There are only 4 users:
> 
> xen.git/xen$ git grep __scanbit
> arch/x86/bitops.c:38:        set = __scanbit(*p >> bit, BITS_PER_LONG);
> arch/x86/bitops.c:88:        set = __scanbit(~*p >> bit, BITS_PER_LONG);
> arch/x86/include/asm/bitops.h:329:static always_inline unsigned int 
> __scanbit(unsigned long val, unsigned int max)
> arch/x86/include/asm/bitops.h:378:        r__ = o__ + __scanbit(*a__ >> o__, 
> s__);                            \
> arch/x86/include/asm/bitops.h:410:        r__ = o__ + __scanbit(~*a__ >> o__, 
> s__);                           \
> 
> 
> i.e. the find_next{_zero,}bit() infrastructure.
> 
> For the two real functions, we could convert along the following lines:
> 
> diff --git a/xen/arch/x86/bitops.c b/xen/arch/x86/bitops.c
> index 22cfb6d8ec3a..009146df92b5 100644
> --- a/xen/arch/x86/bitops.c
> +++ b/xen/arch/x86/bitops.c
> @@ -35,9 +35,11 @@ unsigned int __find_next_bit(
>      if ( bit != 0 )
>      {
>          /* Look for a bit in the first word. */
> -        set = __scanbit(*p >> bit, BITS_PER_LONG);
> -        if ( set < BITS_PER_LONG )
> -            return (offset + set);
> +        set = ffsl(*p >> bit);
> +
> +        if ( set > 0 )
> +            return (offset + set - 1);
> +
>          offset += BITS_PER_LONG - bit;
>          p++;
>      }
> 
> except it has the disadvantage of turning a TZCNT into a BSR when in
> this path the "64 in the zero case" is fine.  That said, it's only older
> AMD CPUs where TZCNT vs BSR matters perf-wise, and this is a horrible
> path anyway because of the hand-rolled asm in __find_first_bit() so I'm
> most likely worrying over nothing.
> 
> What I don't see is an easy way to make the same transformation in the
> find_next{_zero,}_bit() macros.  This accounts for all the other
> invocations when expanded, which are quite a lot including behind
> bitmap_for_each().
> 
> 
> Alternatively, we could have a dedicated tzcnt(), and lean on the fact
> that "64 in the zero case" is probably fine in find_next{_zero,}_bit()
> even for the s__ < BITS_PER_LONG case.

I think this isn't just "probably", as istr us going through all
find_next{_zero,}_bit() (and derivatives) to check that they don't rely
on getting back the exact upper bound when no bit is set. IOW yes, I
think this might be a yet better way to go.

Jan



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.