Skip to contentSkip to conversation

KVM: arm64/sve: Fix vq_present() macro to yield a bool

PATCH In review Dave Martin 5 participants 8 messages Jul 3, 2019 – Jul 4, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: arm64/sve: Fix vq_present() macro to yield a bool

From: Zhang Lei <[email protected]>

The original implementation of vq_present() relied on aggressive inlining in order for the compiler to know that the code is correct, due to some const-casting issues. This was causing sparse and clang to complain, while GCC compiled cleanly.

Commit 0c529ff789bc addressed this problem, but since vq_present() is no longer a function, there is now no implicit casting of the returned value to the return type (bool).

In set_sve_vls(), this uncast bit value is compared against a bool, and so may spuriously compare as unequal when both are nonzero. As a result, KVM may reject valid SVE vector length configurations as invalid, and vice versa.

Fix it by forcing the returned value to a bool.

Fixes 0c529ff789bc ("KVM: arm64: Implement vq_present() as a macro")
Cc Viresh Kumar <[email protected]>

---

Posting this under Zhang Lei's authorship, due to the need to turn this fix around quickly. The fix is as per the original suggestion [1].

Originally observed with the QEMU KVM SVE support under review:
https://lists.gnu.org/archive/html/qemu-devel/2019-06/msg04945.html

Bug reproduced and fix tested on the Arm Fast Model, with
http://linux-arm.org/git?p=kvmtool-dm.git;a=shortlog;h=refs/heads/sve/v3/head
(After rerunning util/update_headers.sh.)

(the --sve-vls command line argument was removed in v4 of the kvmtool patches).

[1] http://lists.infradead.org/pipermail/linux-arm-kernel/2019-July/664633.html

---
 arch/arm64/kvm/guest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/guest.c +1 -1
index c2afa79..dfd6264 100644
@@ -208,7 +208,7 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 
 #define vq_word(vq) (((vq) - SVE_VQ_MIN) / 64)
 #define vq_mask(vq) ((u64)1 << ((vq) - SVE_VQ_MIN) % 64)
-#define vq_present(vqs, vq) ((vqs)[vq_word(vq)] & vq_mask(vq))
+#define vq_present(vqs, vq) (!!((vqs)[vq_word(vq)] & vq_mask(vq)))
 
 static int get_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
Signature · 7 lines
2.1.4


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 48 lines · Dave Martin

From: Zhang Lei <[email protected]>

The original implementation of vq_present() relied on aggressive inlining in order for the compiler to know that the code is correct, due to some const-casting issues. This was causing sparse and clang to complain, while GCC compiled cleanly.

Commit 0c529ff789bc addressed this problem, but since vq_present() is no longer a function, there is now no implicit casting of the returned value to the return type (bool).

In set_sve_vls(), this uncast bit value is compared against a bool, and so may spuriously compare as unequal when both are nonzero. As a result, KVM may reject valid SVE vector length configurations as invalid, and vice versa.

Fix it by forcing the returned value to a bool.

Fixes 0c529ff789bc ("KVM: arm64: Implement vq_present() as a macro")
Cc Viresh Kumar <[email protected]>

---

Posting this under Zhang Lei's authorship, due to the need to turn this fix around quickly. The fix is as per the original suggestion [1].

Originally observed with the QEMU KVM SVE support under review:
https://lists.gnu.org/archive/html/qemu-devel/2019-06/msg04945.html

Bug reproduced and fix tested on the Arm Fast Model, with
http://linux-arm.org/git?p=kvmtool-dm.git;a=shortlog;h=refs/heads/sve/v3/head
(After rerunning util/update_headers.sh.)

(the --sve-vls command line argument was removed in v4 of the kvmtool patches).

[1] http://lists.infradead.org/pipermail/linux-arm-kernel/2019-July/664633.html

---
 arch/arm64/kvm/guest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/guest.c +0 -0
index c2afa79..dfd6264 100644

arch/arm64/kvm/guest.c static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 214

Quoted 8 lines
arch/arm64/kvm/guest.c +1 -1
@@ -208,7 +208,7 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 
 #define vq_word(vq) (((vq) - SVE_VQ_MIN) / 64)
 #define vq_mask(vq) ((u64)1 << ((vq) - SVE_VQ_MIN) % 64)
-#define vq_present(vqs, vq) ((vqs)[vq_word(vq)] & vq_mask(vq))
+#define vq_present(vqs, vq) (!!((vqs)[vq_word(vq)] & vq_mask(vq)))
 
 static int get_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {

It was a really nice bug :)

Signature · 6 lines
viresh

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#5 lore raw
In reply to Dave Martin

+KVM, Paolo and Radim,

Guys, do you mind picking this single patch and sending it to Linus? That's the only fix left for 5.2. Alternatively, I can send you a pull request, but it feels overkill.

Either way, please let me know.

Thanks,

	M.
Quoted 60 lines · Dave Martin

From: Zhang Lei <[email protected]>

The original implementation of vq_present() relied on aggressive inlining in order for the compiler to know that the code is correct, due to some const-casting issues. This was causing sparse and clang to complain, while GCC compiled cleanly.

Commit 0c529ff789bc addressed this problem, but since vq_present() is no longer a function, there is now no implicit casting of the returned value to the return type (bool).

In set_sve_vls(), this uncast bit value is compared against a bool, and so may spuriously compare as unequal when both are nonzero. As a result, KVM may reject valid SVE vector length configurations as invalid, and vice versa.

Fix it by forcing the returned value to a bool.

Fixes 0c529ff789bc ("KVM: arm64: Implement vq_present() as a macro")
Cc Viresh Kumar <[email protected]>

---

Posting this under Zhang Lei's authorship, due to the need to turn this fix around quickly. The fix is as per the original suggestion [1].

Originally observed with the QEMU KVM SVE support under review:
https://lists.gnu.org/archive/html/qemu-devel/2019-06/msg04945.html

Bug reproduced and fix tested on the Arm Fast Model, with
http://linux-arm.org/git?p=kvmtool-dm.git;a=shortlog;h=refs/heads/sve/v3/head
(After rerunning util/update_headers.sh.)

(the --sve-vls command line argument was removed in v4 of the kvmtool patches).

[1] http://lists.infradead.org/pipermail/linux-arm-kernel/2019-July/664633.html

---
 arch/arm64/kvm/guest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/guest.c +1 -1
index c2afa79..dfd6264 100644
@@ -208,7 +208,7 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 
 #define vq_word(vq) (((vq) - SVE_VQ_MIN) / 64)
 #define vq_mask(vq) ((u64)1 << ((vq) - SVE_VQ_MIN) % 64)
-#define vq_present(vqs, vq) ((vqs)[vq_word(vq)] & vq_mask(vq))
+#define vq_present(vqs, vq) (!!((vqs)[vq_word(vq)] & vq_mask(vq)))
 
 static int get_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
Signature · 1 line
2.1.4
Signature · 1 line
Jazz is not dead, it just smells funny.
#2 lore raw
In reply to Viresh Kumar
Quoted 55 lines · Viresh Kumar
Quoted 54 lines · Dave Martin

From: Zhang Lei <[email protected]>

The original implementation of vq_present() relied on aggressive inlining in order for the compiler to know that the code is correct, due to some const-casting issues. This was causing sparse and clang to complain, while GCC compiled cleanly.

Commit 0c529ff789bc addressed this problem, but since vq_present() is no longer a function, there is now no implicit casting of the returned value to the return type (bool).

In set_sve_vls(), this uncast bit value is compared against a bool, and so may spuriously compare as unequal when both are nonzero. As a result, KVM may reject valid SVE vector length configurations as invalid, and vice versa.

Fix it by forcing the returned value to a bool.

Fixes 0c529ff789bc ("KVM: arm64: Implement vq_present() as a macro")
Cc Viresh Kumar <[email protected]>

---

Posting this under Zhang Lei's authorship, due to the need to turn this fix around quickly. The fix is as per the original suggestion [1].

Originally observed with the QEMU KVM SVE support under review:
https://lists.gnu.org/archive/html/qemu-devel/2019-06/msg04945.html

Bug reproduced and fix tested on the Arm Fast Model, with
http://linux-arm.org/git?p=kvmtool-dm.git;a=shortlog;h=refs/heads/sve/v3/head
(After rerunning util/update_headers.sh.)

(the --sve-vls command line argument was removed in v4 of the kvmtool patches).

[1] http://lists.infradead.org/pipermail/linux-arm-kernel/2019-July/664633.html

---
 arch/arm64/kvm/guest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/guest.c +1 -1
index c2afa79..dfd6264 100644
@@ -208,7 +208,7 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 
 #define vq_word(vq) (((vq) - SVE_VQ_MIN) / 64)
 #define vq_mask(vq) ((u64)1 << ((vq) - SVE_VQ_MIN) % 64)
-#define vq_present(vqs, vq) ((vqs)[vq_word(vq)] & vq_mask(vq))
+#define vq_present(vqs, vq) (!!((vqs)[vq_word(vq)] & vq_mask(vq)))
 
Quoted 6 lines

arch/arm64/kvm/guest.c static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 214

Quoted 2 lines
arch/arm64/kvm/guest.c +0 -0
 static int get_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {

It was a really nice bug :)

Thanks for the quick review!

Maybe it makes sense to write equality comparisons on bools as !x == !y to be more defensive against this kind of thing. Anyway, probably best to leave this as-is while the dust settles.

Cheers
---Dave
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

Tested-by
#3 lore raw

Hi guys,

I have started up KVM guest os successfully with SVE feature with Dave' patch.

Quoted 80 lines

-----Original Message-----
From: Dave Martin <[email protected]>
Sent: Thursday, July 04, 2019 5:35 PM
To: Viresh Kumar <[email protected]>
Cc: Okamoto, Takayuki/岡本 高幸 <[email protected]>; Christoffer Dall <[email protected]>; Ard Biesheuvel <[email protected]>; Marc Zyngier <[email protected]>; Catalin Marinas <[email protected]>; Will Deacon <[email protected]>; Zhang, Lei/張 雷 <[email protected]>; Julien Grall <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] KVM: arm64/sve: Fix vq_present() macro to yield a bool

Quoted 60 lines · Viresh Kumar
Quoted 59 lines · Dave Martin

From: Zhang Lei <[email protected]>

The original implementation of vq_present() relied on aggressive inlining in order for the compiler to know that the code is correct, due to some const-casting issues. This was causing sparse and clang to complain, while GCC compiled cleanly.

Commit 0c529ff789bc addressed this problem, but since vq_present() is no longer a function, there is now no implicit casting of the returned value to the return type (bool).

In set_sve_vls(), this uncast bit value is compared against a bool, and so may spuriously compare as unequal when both are nonzero. As a result, KVM may reject valid SVE vector length configurations as invalid, and vice versa.

Fix it by forcing the returned value to a bool.

Fixes 0c529ff789bc ("KVM: arm64: Implement vq_present() as a

macro")

rewrite]
Cc: Viresh Kumar <[email protected]>

---

Posting this under Zhang Lei's authorship, due to the need to turn this fix around quickly. The fix is as per the original suggestion [1].

Originally observed with the QEMU KVM SVE support under review:
https://lists.gnu.org/archive/html/qemu-devel/2019-06/msg04945.html

Bug reproduced and fix tested on the Arm Fast Model, with
http://linux-arm.org/git?p=kvmtool-dm.git;a=shortlog;h=refs/heads/sv
e/v3/head (After rerunning util/update_headers.sh.)

(the --sve-vls command line argument was removed in v4 of the kvmtool patches).

[1]
http://lists.infradead.org/pipermail/linux-arm-kernel/2019-July/6646
33.html

---
 arch/arm64/kvm/guest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/guest.c index renamed from arch/arm64/kvm/guest.c +0 -0

c2afa79..dfd6264 100644

arch/arm64/kvm/guest.c +1 -0
@@ -208,7 +208,7 @@ static int set_core_reg(struct kvm_vcpu *vcpu,
 const struct kvm_one_reg *reg)
 
 #define vq_word(vq) (((vq) - SVE_VQ_MIN) / 64)  #define vq_mask(vq)
 ((u64)1 << ((vq) - SVE_VQ_MIN) % 64) -#define vq_present(vqs, vq)
 ((vqs)[vq_word(vq)] & vq_mask(vq))
+#define vq_present(vqs, vq) (!!((vqs)[vq_word(vq)] & vq_mask(vq)))
 
Quoted 6 lines
Quoted 2 lines

static int get_sve_vls(struct kvm_vcpu *vcpu, const struct
kvm_one_reg *reg) {

It was a really nice bug :)

Quoted 8 lines

Thanks for the quick review!

Maybe it makes sense to write equality comparisons on bools as !x == !y to be more defensive against this kind of thing. Anyway, probably best to leave this as-is while the dust settles.

Cheers
---Dave

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#6 lore raw
In reply to Marc Zyngier
Quoted 5 lines · Marc Zyngier

+KVM, Paolo and Radim,

Guys, do you mind picking this single patch and sending it to Linus? That's the only fix left for 5.2. Alternatively, I can send you a pull request, but it feels overkill.

Sure, will do.

Paolo

Quoted 69 lines

Either way, please let me know.

Thanks,

	M.
Quoted 60 lines · Dave Martin

From: Zhang Lei <[email protected]>

The original implementation of vq_present() relied on aggressive inlining in order for the compiler to know that the code is correct, due to some const-casting issues. This was causing sparse and clang to complain, while GCC compiled cleanly.

Commit 0c529ff789bc addressed this problem, but since vq_present() is no longer a function, there is now no implicit casting of the returned value to the return type (bool).

In set_sve_vls(), this uncast bit value is compared against a bool, and so may spuriously compare as unequal when both are nonzero. As a result, KVM may reject valid SVE vector length configurations as invalid, and vice versa.

Fix it by forcing the returned value to a bool.

Fixes 0c529ff789bc ("KVM: arm64: Implement vq_present() as a macro")
Cc Viresh Kumar <[email protected]>

---

Posting this under Zhang Lei's authorship, due to the need to turn this fix around quickly. The fix is as per the original suggestion [1].

Originally observed with the QEMU KVM SVE support under review:
https://lists.gnu.org/archive/html/qemu-devel/2019-06/msg04945.html

Bug reproduced and fix tested on the Arm Fast Model, with
http://linux-arm.org/git?p=kvmtool-dm.git;a=shortlog;h=refs/heads/sve/v3/head
(After rerunning util/update_headers.sh.)

(the --sve-vls command line argument was removed in v4 of the kvmtool patches).

[1] http://lists.infradead.org/pipermail/linux-arm-kernel/2019-July/664633.html

---
 arch/arm64/kvm/guest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/guest.c +1 -1
index c2afa79..dfd6264 100644
@@ -208,7 +208,7 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 
 #define vq_word(vq) (((vq) - SVE_VQ_MIN) / 64)
 #define vq_mask(vq) ((u64)1 << ((vq) - SVE_VQ_MIN) % 64)
-#define vq_present(vqs, vq) ((vqs)[vq_word(vq)] & vq_mask(vq))
+#define vq_present(vqs, vq) (!!((vqs)[vq_word(vq)] & vq_mask(vq)))
 
 static int get_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
Signature · 1 line
2.1.4
#7 lore raw
Quoted 10 lines · Paolo Bonzini
Quoted 5 lines · Marc Zyngier

+KVM, Paolo and Radim,

Guys, do you mind picking this single patch and sending it to Linus? That's the only fix left for 5.2. Alternatively, I can send you a pull request, but it feels overkill.

Sure, will do.

Paolo

Thanks all for the quick turnaround!

[...]

Cheers
---Dave