Skip to contentSkip to conversation

x86/mm: Improve bitops in vcpumask_to_pcpumask()

PATCH In review Andrew Cooper 2 participants 2 messages May 2, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/mm: Improve bitops in vcpumask_to_pcpumask()

This loop is for_each_set_bit() in disguise.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

More detailed analysis:

add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-46 (-46)

  Function                                     old     new   delta
  vcpumask_to_pcpumask                         519     473     -46

Re-shifting 1 just to clear it is horrible code generation.

While the ffsl() is abtracted away, it doesn't logically move in terms of the loop position. All that is happening is a (more efficient) clearing is moved to the post condition, and 3/4 of a cacheline of logic disappears.

---
 xen/arch/x86/mm.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
xen/arch/x86/mm.c +2 -4
index 0b787ba55312..66c15a3c864f 100644
@@ -3411,7 +3411,7 @@ int new_guest_cr3(mfn_t mfn)
 static int vcpumask_to_pcpumask(
     struct domain *d, XEN_GUEST_HANDLE_PARAM(const_void) bmap, cpumask_t *pmask)
 {
-    unsigned int vcpu_id, vcpu_bias, offs;
+    unsigned int vcpu_bias, offs;
     unsigned long vmask;
     struct vcpu *v;
     bool is_native = !is_pv_32bit_domain(d);
@@ -3432,12 +3432,10 @@ static int vcpumask_to_pcpumask(
             return -EFAULT;
         }
 
-        while ( vmask )
+        for_each_set_bit ( vcpu_id, vmask )
         {
             unsigned int cpu;
 
-            vcpu_id = ffsl(vmask) - 1;
-            vmask &= ~(1UL << vcpu_id);
             vcpu_id += vcpu_bias;
             if ( (vcpu_id >= d->max_vcpus) )
                 return 0;
Signature · 1 line
2.39.5