Skip to contentSkip to conversation

ARM/vgic: Fix variable shadowing in vgic_to_sgi()

PATCH In review Andrew Cooper 2 participants 3 messages Aug 27, 2024 – Aug 28, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM/vgic: Fix variable shadowing in vgic_to_sgi()

for_each_set_bit() allocates its own variable intentionally as loop-scope only. Unfortunately, this causes the inner 'i' to shadow the outer 'i'.

Drop the outermost 'i' and 'vcpuid' variables, moving them into a more narrow scope and correcting them to be unsigned which they should have been all along.

Fixes 9429f1a6c475 ("ARM/vgic: Use for_each_set_bit() in vgic_to_sgi()")

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>

---
 xen/arch/arm/vgic.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
xen/arch/arm/vgic.c +3 -4
index 8ffe099bcb5f..6ecd9146511c 100644
@@ -468,8 +468,6 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,
                  int virq, const struct sgi_target *target)
 {
     struct domain *d = v->domain;
-    int vcpuid;
-    int i;
     unsigned int base, bitmap;
 
     ASSERT( virq < 16 );
@@ -483,7 +481,8 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,
 
         for_each_set_bit ( i, bitmap )
         {
-            vcpuid = base + i;
+            unsigned int vcpuid = base + i;
+
             if ( vcpuid >= d->max_vcpus || d->vcpu[vcpuid] == NULL ||
                  !is_vcpu_online(d->vcpu[vcpuid]) )
             {
@@ -497,7 +496,7 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,
         break;
     case SGI_TARGET_OTHERS:
         perfc_incr(vgic_sgi_others);
-        for ( i = 0; i < d->max_vcpus; i++ )
+        for ( unsigned int i = 0; i < d->max_vcpus; i++ )
         {
             if ( i != current->vcpu_id && d->vcpu[i] != NULL &&
                  is_vcpu_online(d->vcpu[i]) )

base-commit: b8cdfac2be38c98dd3ad0e911a3f7f787f5bcf6b
prerequisite-patch-id: 87415b68ed015b8f36405b2554f2abd6c02f28aa
prerequisite-patch-id: d87fe52c264dc5a33883a04b615043fbefd94f92
prerequisite-patch-id: abb68a851297bbf63c16093c6362c0d4b9c39334

Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Andrew Cooper

for_each_set_bit() allocates its own variable intentionally as loop-scope only. Unfortunately, this causes the inner 'i' to shadow the outer 'i'.

NIT: I'd mention it violates MISRA R5.3

Quoted 31 lines

Drop the outermost 'i' and 'vcpuid' variables, moving them into a more narrow scope and correcting them to be unsigned which they should have been all along.

Fixes 9429f1a6c475 ("ARM/vgic: Use for_each_set_bit() in vgic_to_sgi()")

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>

---
 xen/arch/arm/vgic.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
xen/arch/arm/vgic.c +0 -2
index 8ffe099bcb5f..6ecd9146511c 100644
@@ -468,8 +468,6 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,
                  int virq, const struct sgi_target *target)
 {
     struct domain *d = v->domain;
-    int vcpuid;
-    int i;
     unsigned int base, bitmap;
 
     ASSERT( virq < 16 );

xen/arch/arm/vgic.c bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, · line 484

Quoted 5 lines
xen/arch/arm/vgic.c +1 -1
@@ -483,7 +481,8 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,
 
         for_each_set_bit ( i, bitmap )
         {
-            vcpuid = base + i;
+            unsigned int vcpuid = base + i;

With this change you should replace the printk specifier from %d to %u for vcpuid.

Apart from that:

~Michal

#2 lore raw
Quoted 6 lines · Michal Orzel
Quoted 3 lines · Andrew Cooper

for_each_set_bit() allocates its own variable intentionally as loop-scope only. Unfortunately, this causes the inner 'i' to shadow the outer 'i'.

NIT: I'd mention it violates MISRA R5.3

Done.

Quoted 29 lines
Quoted 29 lines

Drop the outermost 'i' and 'vcpuid' variables, moving them into a more narrow scope and correcting them to be unsigned which they should have been all along.

Fixes 9429f1a6c475 ("ARM/vgic: Use for_each_set_bit() in vgic_to_sgi()")

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>

---
 xen/arch/arm/vgic.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
xen/arch/arm/vgic.c +0 -2
index 8ffe099bcb5f..6ecd9146511c 100644
@@ -468,8 +468,6 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,
                  int virq, const struct sgi_target *target)
 {
     struct domain *d = v->domain;
-    int vcpuid;
-    int i;
     unsigned int base, bitmap;
 
Quoted 8 lines

xen/arch/arm/vgic.c bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, · line 484

Quoted 7 lines
xen/arch/arm/vgic.c +1 -1
     ASSERT( virq < 16 );
@@ -483,7 +481,8 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,
 
         for_each_set_bit ( i, bitmap )
         {
-            vcpuid = base + i;
+            unsigned int vcpuid = base + i;

With this change you should replace the printk specifier from %d to %u for vcpuid.

So I should, yes.

Quoted 3 lines

Apart from that:

Thanks.  Sorry for breaking CI.

I did run Eclair on it, but for the manually triggered run, it only generates a warning, and does not trigger a "pipeline {failed,fixed}" event, because the pipeline hasn't changed state from the last time I ran it.

I'm not sure if we can do any better for something which is conditionally manually triggered.

~Andrew