All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] xen/vpci: Improve code generation in mask_write()
@ 2024-03-15 12:13 Andrew Cooper
  2024-03-15 15:13 ` Roger Pau Monné
  2024-03-18 10:14 ` Roger Pau Monné
  0 siblings, 2 replies; 4+ messages in thread
From: Andrew Cooper @ 2024-03-15 12:13 UTC (permalink / raw)
  To: Xen-devel; +Cc: Andrew Cooper, Roger Pau Monné

The use of __clear_bit() forces dmask to be spilled to the stack, and
interferes with the compiler heuristcs for some upcoming improvements to the
ffs() code generation.

First, shrink dmask to just the active vectors by making out the upper bits.
This replaces the "i < msi->vectors" part of the loop condition.

Next, use a simple while() loop with "clear bottom bit" expressed in plane C,
which affords the optimiser a far better understanding of what the loop is
doing.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Roger Pau Monné <roger.pau@citrix.com>

Noticed when looking at the ffs() code gen improvements.

Any suggestion on how to test this?  test_vcpi doesn't seem to check anything
here.  I think I've got the boundary conditions for msi->vectors right, but
I'd be lying if I said I was certain...

bloat-o-meter reports:

  add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-28 (-28)
  Function                                     old     new   delta
  mask_write                                   142     114     -28

which is a consequence of the compiler having a much better idea of what's
going on in the loop.  There's more to come with the ffs() improvements too.
---
 xen/drivers/vpci/msi.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/xen/drivers/vpci/msi.c b/xen/drivers/vpci/msi.c
index d3aa5df08941..30adcf7df05d 100644
--- a/xen/drivers/vpci/msi.c
+++ b/xen/drivers/vpci/msi.c
@@ -169,13 +169,15 @@ static void cf_check mask_write(
 
     if ( msi->enabled )
     {
-        unsigned int i;
+        /* Skip changes to vectors which aren't enabled. */
+        dmask &= (~0U >> (32 - msi->vectors));
 
-        for ( i = ffs(dmask) - 1; dmask && i < msi->vectors;
-              i = ffs(dmask) - 1 )
+        while ( dmask )
         {
+            unsigned int i = ffs(dmask) - 1;
+
             vpci_msi_arch_mask(msi, pdev, i, (val >> i) & 1);
-            __clear_bit(i, &dmask);
+            dmask &= (dmask - 1);
         }
     }
 

base-commit: d638e304f13a5ef7d125de5ace5f7828a7b25bac
-- 
2.30.2



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] xen/vpci: Improve code generation in mask_write()
  2024-03-15 12:13 [PATCH] xen/vpci: Improve code generation in mask_write() Andrew Cooper
@ 2024-03-15 15:13 ` Roger Pau Monné
  2024-03-15 16:19   ` Andrew Cooper
  2024-03-18 10:14 ` Roger Pau Monné
  1 sibling, 1 reply; 4+ messages in thread
From: Roger Pau Monné @ 2024-03-15 15:13 UTC (permalink / raw)
  To: Andrew Cooper; +Cc: Xen-devel

On Fri, Mar 15, 2024 at 12:13:22PM +0000, Andrew Cooper wrote:
> The use of __clear_bit() forces dmask to be spilled to the stack, and
> interferes with the compiler heuristcs for some upcoming improvements to the
> ffs() code generation.
> 
> First, shrink dmask to just the active vectors by making out the upper bits.
> This replaces the "i < msi->vectors" part of the loop condition.
> 
> Next, use a simple while() loop with "clear bottom bit" expressed in plane C,
> which affords the optimiser a far better understanding of what the loop is
> doing.
> 
> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
> ---
> CC: Roger Pau Monné <roger.pau@citrix.com>
> 
> Noticed when looking at the ffs() code gen improvements.
> 
> Any suggestion on how to test this?  test_vcpi doesn't seem to check anything
> here.  I think I've got the boundary conditions for msi->vectors right, but
> I'd be lying if I said I was certain...

There's no easy way to test this because it relies on having a PCI
device underneath.  test_vpci just checks the logic to add & remove
handlers, but doesn't get remotely close as to attempting to provide
some kind of dummy environment for pass through to be sanity tested.

I should look into it.

> 
> bloat-o-meter reports:
> 
>   add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-28 (-28)
>   Function                                     old     new   delta
>   mask_write                                   142     114     -28
> 
> which is a consequence of the compiler having a much better idea of what's
> going on in the loop.  There's more to come with the ffs() improvements too.
> ---
>  xen/drivers/vpci/msi.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/xen/drivers/vpci/msi.c b/xen/drivers/vpci/msi.c
> index d3aa5df08941..30adcf7df05d 100644
> --- a/xen/drivers/vpci/msi.c
> +++ b/xen/drivers/vpci/msi.c
> @@ -169,13 +169,15 @@ static void cf_check mask_write(
>  
>      if ( msi->enabled )
>      {
> -        unsigned int i;
> +        /* Skip changes to vectors which aren't enabled. */
> +        dmask &= (~0U >> (32 - msi->vectors));

Do we need to ASSERT that msi->vectors <= 32 in order to avoid
theoretical UB?

Thanks, Roger.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] xen/vpci: Improve code generation in mask_write()
  2024-03-15 15:13 ` Roger Pau Monné
@ 2024-03-15 16:19   ` Andrew Cooper
  0 siblings, 0 replies; 4+ messages in thread
From: Andrew Cooper @ 2024-03-15 16:19 UTC (permalink / raw)
  To: Roger Pau Monné; +Cc: Xen-devel

On 15/03/2024 3:13 pm, Roger Pau Monné wrote:
> On Fri, Mar 15, 2024 at 12:13:22PM +0000, Andrew Cooper wrote:
>> The use of __clear_bit() forces dmask to be spilled to the stack, and
>> interferes with the compiler heuristcs for some upcoming improvements to the
>> ffs() code generation.
>>
>> First, shrink dmask to just the active vectors by making out the upper bits.
>> This replaces the "i < msi->vectors" part of the loop condition.
>>
>> Next, use a simple while() loop with "clear bottom bit" expressed in plane C,
>> which affords the optimiser a far better understanding of what the loop is
>> doing.
>>
>> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
>> ---
>> CC: Roger Pau Monné <roger.pau@citrix.com>
>>
>> Noticed when looking at the ffs() code gen improvements.
>>
>> Any suggestion on how to test this?  test_vcpi doesn't seem to check anything
>> here.  I think I've got the boundary conditions for msi->vectors right, but
>> I'd be lying if I said I was certain...
> There's no easy way to test this because it relies on having a PCI
> device underneath.  test_vpci just checks the logic to add & remove
> handlers, but doesn't get remotely close as to attempting to provide
> some kind of dummy environment for pass through to be sanity tested.
>
> I should look into it.
>
>> bloat-o-meter reports:
>>
>>   add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-28 (-28)
>>   Function                                     old     new   delta
>>   mask_write                                   142     114     -28
>>
>> which is a consequence of the compiler having a much better idea of what's
>> going on in the loop.  There's more to come with the ffs() improvements too.
>> ---
>>  xen/drivers/vpci/msi.c | 10 ++++++----
>>  1 file changed, 6 insertions(+), 4 deletions(-)
>>
>> diff --git a/xen/drivers/vpci/msi.c b/xen/drivers/vpci/msi.c
>> index d3aa5df08941..30adcf7df05d 100644
>> --- a/xen/drivers/vpci/msi.c
>> +++ b/xen/drivers/vpci/msi.c
>> @@ -169,13 +169,15 @@ static void cf_check mask_write(
>>  
>>      if ( msi->enabled )
>>      {
>> -        unsigned int i;
>> +        /* Skip changes to vectors which aren't enabled. */
>> +        dmask &= (~0U >> (32 - msi->vectors));
> Do we need to ASSERT that msi->vectors <= 32 in order to avoid
> theoretical UB?

I don't think so.  Things have gone catastrophically wrong elsewhere to
get here with 64 or 128.

All this does is stop calling the set-mask callback for disabled vectors.

~Andrew


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] xen/vpci: Improve code generation in mask_write()
  2024-03-15 12:13 [PATCH] xen/vpci: Improve code generation in mask_write() Andrew Cooper
  2024-03-15 15:13 ` Roger Pau Monné
@ 2024-03-18 10:14 ` Roger Pau Monné
  1 sibling, 0 replies; 4+ messages in thread
From: Roger Pau Monné @ 2024-03-18 10:14 UTC (permalink / raw)
  To: Andrew Cooper; +Cc: Xen-devel

On Fri, Mar 15, 2024 at 12:13:22PM +0000, Andrew Cooper wrote:
> The use of __clear_bit() forces dmask to be spilled to the stack, and
> interferes with the compiler heuristcs for some upcoming improvements to the
> ffs() code generation.
> 
> First, shrink dmask to just the active vectors by making out the upper bits.
> This replaces the "i < msi->vectors" part of the loop condition.
> 
> Next, use a simple while() loop with "clear bottom bit" expressed in plane C,
> which affords the optimiser a far better understanding of what the loop is
> doing.
> 
> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>

Reviewed-by: Roger Pau Monné <roger.pau@citrix.com>

Thanks, Roger.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-03-18 10:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-15 12:13 [PATCH] xen/vpci: Improve code generation in mask_write() Andrew Cooper
2024-03-15 15:13 ` Roger Pau Monné
2024-03-15 16:19   ` Andrew Cooper
2024-03-18 10:14 ` Roger Pau Monné

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.