xen: arm: correct use of find_next_bit
PATCH 1/1 xen: arm: correct use of find_next_bit
find_next_bit takes a "const unsigned long *" but forcing a cast of an "uint32_t *" throws away the alignment constraints and ends up causing an alignment fault on arm64 if the input happened to be 4 but not 8 byte aligned.
Instead of casting use a temporary variable of the right type.
I've had a look around for similar constructs and the only thing I found was maintenance_interrupt which cases a uint64_t down to an unsigned long, which although perhaps not best advised is safe I think.
This was observed with the AArch64 Linaro toolchain 2013.12 but I think that is just coincidental due to subtle changes to the stack layout etc.
--- xen/arch/arm/vgic.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
xen/arch/arm/vgic.c +4 -2
@@ -362,11 +362,12 @@ read_as_zero:static void vgic_disable_irqs (struct vcpu *v,uint32_t r,int n) { +const unsigned long mask = r;struct pending_irq *p;unsigned int irq;int i =0 ; -while ( (i =find_next_bit ((const long unsigned int *) &r,32 , i)) <32 ) { +while ( (i =find_next_bit (&mask,32 , i)) <32 ) { irq = i + (32 * n); p =irq_to_pending (v, irq);clear_bit (GIC_IRQ_GUEST_ENABLED , &p->status);
@@ -379,11 +380,12 @@ static void vgic_disable_irqs(struct vcpu *v, uint32_t r, int n)static void vgic_enable_irqs (struct vcpu *v,uint32_t r,int n) { +const unsigned long mask = r;struct pending_irq *p;unsigned int irq;int i =0 ; -while ( (i =find_next_bit ((const long unsigned int *) &r,32 , i)) <32 ) { +while ( (i =find_next_bit (&mask,32 , i)) <32 ) { irq = i + (32 * n); p =irq_to_pending (v, irq);set_bit (GIC_IRQ_GUEST_ENABLED , &p->status);
Signature · 1 line
1.7.10.4
Quoted 10 lines · Ian Campbell
find_next_bittakes a "const unsigned long *" but forcing a cast of an "uint32_t*" throws away the alignment constraints and ends up causing an alignment fault on arm64 if the input happened to be 4 but not 8 byte aligned.Instead of casting use a temporary variable of the right type.
I've had a look around for similar constructs and the only thing I found was
maintenance_interruptwhich cases auint64_tdown to an unsigned long, which although perhaps not best advised is safe I think.
Quoted 5 lines
This was observed with the AArch64 Linaro toolchain 2013.12 but I think that is just coincidental due to subtle changes to the stack layout etc.
Reported-by Fu Wei <[email protected]>Signed-off-by Ian Campbell <[email protected]>
Good catch! Do you plan to apply this patch for Xen 4.4?
Quoted 37 lines
--- xen/arch/arm/vgic.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)xen/arch/arm/vgic.c +4
-2@@ -362,11 +362,12 @@ read_as_zero:static void vgic_disable_irqs (struct vcpu *v,uint32_t r,int n) { +const unsigned long mask = r;struct pending_irq *p;unsigned int irq;int i =0 ; -while ( (i =find_next_bit ((const long unsigned int *) &r,32 , i)) <32 ) { +while ( (i =find_next_bit (&mask,32 , i)) <32 ) { irq = i + (32 * n); p =irq_to_pending (v, irq);clear_bit (GIC_IRQ_GUEST_ENABLED , &p->status);@@ -379,11 +380,12 @@ static void vgic_disable_irqs(struct vcpu *v, uint32_t r, int n)static void vgic_enable_irqs (struct vcpu *v,uint32_t r,int n) { +const unsigned long mask = r;struct pending_irq *p;unsigned int irq;int i =0 ; -while ( (i =find_next_bit ((const long unsigned int *) &r,32 , i)) <32 ) { +while ( (i =find_next_bit (&mask,32 , i)) <32 ) { irq = i + (32 * n); p =irq_to_pending (v, irq);set_bit (GIC_IRQ_GUEST_ENABLED , &p->status);
Signature · 1 line
Julien Grall
Quoted 11 lines · Julien Grall
Quoted 10 lines · Ian Campbell
find_next_bittakes a "const unsigned long *" but forcing a cast of an "uint32_t*" throws away the alignment constraints and ends up causing an alignment fault on arm64 if the input happened to be 4 but not 8 byte aligned.Instead of casting use a temporary variable of the right type.
I've had a look around for similar constructs and the only thing I found was
maintenance_interruptwhich cases auint64_tdown to an unsigned long, which although perhaps not best advised is safe I think.
Quoted 7 lines
Quoted 5 lines
This was observed with the AArch64 Linaro toolchain 2013.12 but I think that is just coincidental due to subtle changes to the stack layout etc.
Reported-by Fu Wei <[email protected]>Signed-off-by Ian Campbell <[email protected]>Good catch! Do you plan to apply this patch for Xen 4.4?
Yes, I think it is a suitable bug fix.
Quoted 42 lines
Acked-by Julien Grall <[email protected]>Quoted 37 lines
--- xen/arch/arm/vgic.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)xen/arch/arm/vgic.c +4
-2@@ -362,11 +362,12 @@ read_as_zero:static void vgic_disable_irqs (struct vcpu *v,uint32_t r,int n) { +const unsigned long mask = r;struct pending_irq *p;unsigned int irq;int i =0 ; -while ( (i =find_next_bit ((const long unsigned int *) &r,32 , i)) <32 ) { +while ( (i =find_next_bit (&mask,32 , i)) <32 ) { irq = i + (32 * n); p =irq_to_pending (v, irq);clear_bit (GIC_IRQ_GUEST_ENABLED , &p->status);@@ -379,11 +380,12 @@ static void vgic_disable_irqs(struct vcpu *v, uint32_t r, int n)static void vgic_enable_irqs (struct vcpu *v,uint32_t r,int n) { +const unsigned long mask = r;struct pending_irq *p;unsigned int irq;int i =0 ; -while ( (i =find_next_bit ((const long unsigned int *) &r,32 , i)) <32 ) { +while ( (i =find_next_bit (&mask,32 , i)) <32 ) { irq = i + (32 * n); p =irq_to_pending (v, irq);set_bit (GIC_IRQ_GUEST_ENABLED , &p->status);
Ian Campbell
AppliedQuoted 15 lines · Ian Campbell
Quoted 14 lines · Julien Grall
Quoted 13 lines · Ian Campbell
find_next_bittakes a "const unsigned long *" but forcing a cast of an "uint32_t*" throws away the alignment constraints and ends up causing an alignment fault on arm64 if the input happened to be 4 but not 8 byte aligned.Instead of casting use a temporary variable of the right type.
I've had a look around for similar constructs and the only thing I found was
maintenance_interruptwhich cases auint64_tdown to an unsigned long, which although perhaps not best advised is safe I think.This was observed with the AArch64 Linaro toolchain 2013.12 but I think that is just coincidental due to subtle changes to the stack layout etc.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Reported-by Fu Wei <[email protected]>Signed-off-by Ian Campbell <[email protected]>Good catch! Do you plan to apply this patch for Xen 4.4?
Yes, I think it is a suitable bug fix.
Applied, thanks.
Quoted 26 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
find_next_bittakes a "const unsigned long *" but forcing a cast of an "uint32_t*" throws away the alignment constraints and ends up causing an alignment fault on arm64 if the input happened to be 4 but not 8 byte aligned.Instead of casting use a temporary variable of the right type.
I've had a look around for similar constructs and the only thing I found was
maintenance_interruptwhich cases auint64_tdown to an unsigned long, which although perhaps not best advised is safe I think.This was observed with the AArch64 Linaro toolchain 2013.12 but I think that is just coincidental due to subtle changes to the stack layout etc.
Reported-by Fu Wei <[email protected]>Signed-off-by Ian Campbell <[email protected]>--- xen/arch/arm/vgic.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)xen/arch/arm/vgic.c +0
-0
xen/arch/arm/vgic.c read_as_zero: · line 365
Quoted 4 lines
xen/arch/arm/vgic.c +1
-0@@ -362,11 +362,12 @@ read_as_zero:static void vgic_disable_irqs (struct vcpu *v,uint32_t r,int n) { +const unsigned long mask = r;
Why don't you just change the type of "r" to "unsigned long"?
Jan
Quoted 34 lines
xen/arch/arm/vgic.c +1
-1struct pending_irq *p;unsigned int irq;int i =0 ; -while ( (i =find_next_bit ((const long unsigned int *) &r,32 , i)) <32 ) { +while ( (i =find_next_bit (&mask,32 , i)) <32 ) { irq = i + (32 * n); p =irq_to_pending (v, irq);clear_bit(GIC_IRQ_GUEST_ENABLED, &p->status);xen/arch/arm/vgic.c +2
-1@@ -379,11 +380,12 @@ static void vgic_disable_irqs(struct vcpu *v, uint32_t r, int n)static void vgic_enable_irqs (struct vcpu *v,uint32_t r,int n) { +const unsigned long mask = r;struct pending_irq *p;unsigned int irq;int i =0 ; -while ( (i =find_next_bit ((const long unsigned int *) &r,32 , i)) <32 ) { +while ( (i =find_next_bit (&mask,32 , i)) <32 ) { irq = i + (32 * n);p = irq_to_pending(v, irq); set_bit(GIC_IRQ_GUEST_ENABLED, &p->status);Signature · 7 lines
1.7.10.4 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Quoted 11 lines · Jan Beulich
xen/arch/arm/vgic.c read_as_zero: · line 365
Quoted 9 lines
xen/arch/arm/vgic.c +1
-0@@ -362,11 +362,12 @@ read_as_zero:static void vgic_disable_irqs (struct vcpu *v,uint32_t r,int n) { +const unsigned long mask = r;Why don't you just change the type of "r" to "unsigned long"?
The MMIO register which this function is emulating is a 32-bit register, so I preferred to keep this wrinkle confined to the internals.
Ian.
Quoted 3 lines · Ian Campbell
find_next_bittakes a "const unsigned long *" but forcing a cast of an "uint32_t*" throws away the alignment constraints and ends up causing an alignment fault on arm64 if the input happened to be 4 but not 8 byte aligned.
I am not opposed to this patch, but for the sake of clarity, isn't the alignment of (uint32_t*) and (const unsigned long*) the same? It should be 8 bytes in both cases on ARM64.
It seems to me that the problem is not the cast to (const unsigned long*), but the usage of &r: maybe the tools aren't able to covert &r to a properly aligned pointer?
Am I getting this wrong?
Quoted 50 lines
Instead of casting use a temporary variable of the right type.
I've had a look around for similar constructs and the only thing I found was
maintenance_interruptwhich cases auint64_tdown to an unsigned long, which although perhaps not best advised is safe I think.This was observed with the AArch64 Linaro toolchain 2013.12 but I think that is just coincidental due to subtle changes to the stack layout etc.
Reported-by Fu Wei <[email protected]>Signed-off-by Ian Campbell <[email protected]>--- xen/arch/arm/vgic.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)xen/arch/arm/vgic.c +4
-2@@ -362,11 +362,12 @@ read_as_zero:static void vgic_disable_irqs (struct vcpu *v,uint32_t r,int n) { +const unsigned long mask = r;struct pending_irq *p;unsigned int irq;int i =0 ; -while ( (i =find_next_bit ((const long unsigned int *) &r,32 , i)) <32 ) { +while ( (i =find_next_bit (&mask,32 , i)) <32 ) { irq = i + (32 * n); p =irq_to_pending (v, irq);clear_bit (GIC_IRQ_GUEST_ENABLED , &p->status);@@ -379,11 +380,12 @@ static void vgic_disable_irqs(struct vcpu *v, uint32_t r, int n)static void vgic_enable_irqs (struct vcpu *v,uint32_t r,int n) { +const unsigned long mask = r;struct pending_irq *p;unsigned int irq;int i =0 ; -while ( (i =find_next_bit ((const long unsigned int *) &r,32 , i)) <32 ) { +while ( (i =find_next_bit (&mask,32 , i)) <32 ) { irq = i + (32 * n); p =irq_to_pending (v, irq);set_bit (GIC_IRQ_GUEST_ENABLED , &p->status);Signature · 1 line
1.7.10.4
Quoted 8 lines · Stefano Stabellini
Quoted 3 lines · Ian Campbell
find_next_bittakes a "const unsigned long *" but forcing a cast of an "uint32_t*" throws away the alignment constraints and ends up causing an alignment fault on arm64 if the input happened to be 4 but not 8 byte aligned.I am not opposed to this patch, but for the sake of clarity, isn't the alignment of (uint32_t*) and (const unsigned long*) the same? It should be 8 bytes in both cases on ARM64.
The target of a uint32_t * only needs to be 4 byte aligned on arm64, since that is the natural alignment of a 32-bit type.
Quoted 3 lines
It seems to me that the problem is not the cast to (const unsigned long*), but the usage of &r: maybe the tools aren't able to covert &r to a properly aligned pointer?
No, &r behaves exactly as expected and returns the address of r, whatever the alignment of r is.
Quoted 1 line
Am I getting this wrong?
I think so, yes.
Ian.