Skip to contentSkip to conversation

KVM: s390: pci: Fix aisb calculation

PATCH Applied Matthew Rosato 3 participants 5 messages Apr 24 – Apr 27

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

PATCH 1/1 KVM: s390: pci: Fix aisb calculation

The current implementation of aisb calculation will erroneously index via an unsigned long * as well as multiply by 8B for every 64-bits in the offset; only one or the other is required. This throws off aisb calculations once the number of devices exceeds 64, and can result in out-of-bounds access as well as failure to indicate summary bits associated with those devices in guests.

Fix this by converting to a physical address before applying the offset, as is already done in arch/s390/pci/pci_irq.c.

Fixes 3c5a1b6f0a18 ("KVM: s390: pci: provide routines for enabling/disabling interrupt forwarding")
---
 arch/s390/kvm/pci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/s390/kvm/pci.c +2 -2
index 86d93e8dddae..338171f9371b 100644
@@ -166,7 +166,7 @@ static int kvm_zpci_set_airq(struct zpci_dev *zdev)
 	fib.fmt0.noi = airq_iv_end(zdev->aibv);
 	fib.fmt0.aibv = virt_to_phys(zdev->aibv->vector);
 	fib.fmt0.aibvo = 0;
-	fib.fmt0.aisb = virt_to_phys(aift->sbv->vector + (zdev->aisb / 64) * 8);
+	fib.fmt0.aisb = virt_to_phys(aift->sbv->vector) + (zdev->aisb / 64) * 8;
 	fib.fmt0.aisbo = zdev->aisb & 63;
 	fib.gd = zdev->gisa;
 
@@ -309,7 +309,7 @@ static int kvm_s390_pci_aif_enable(struct zpci_dev *zdev, struct zpci_fib *fib,
 
 	/* Update guest FIB for re-issue */
 	fib->fmt0.aisbo = zdev->aisb & 63;
-	fib->fmt0.aisb = virt_to_phys(aift->sbv->vector + (zdev->aisb / 64) * 8);
+	fib->fmt0.aisb = virt_to_phys(aift->sbv->vector) + (zdev->aisb / 64) * 8;
 	fib->fmt0.isc = gisc;
 
 	/* Save some guest fib values in the host for later use */
Signature · 1 line
2.53.0
#1 lore raw
Quoted 21 lines · Matthew Rosato

The current implementation of aisb calculation will erroneously index via an unsigned long * as well as multiply by 8B for every 64-bits in the offset; only one or the other is required. This throws off aisb calculations once the number of devices exceeds 64, and can result in out-of-bounds access as well as failure to indicate summary bits associated with those devices in guests.

Fix this by converting to a physical address before applying the offset, as is already done in arch/s390/pci/pci_irq.c.

Fixes 3c5a1b6f0a18 ("KVM: s390: pci: provide routines for enabling/disabling interrupt forwarding")
---
 arch/s390/kvm/pci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/s390/kvm/pci.c +0 -0
index 86d93e8dddae..338171f9371b 100644

arch/s390/kvm/pci.c static int kvm_zpci_set_airq(struct zpci_dev *zdev) · line 169

Quoted 5 lines
arch/s390/kvm/pci.c +1 -1
@@ -166,7 +166,7 @@ static int kvm_zpci_set_airq(struct zpci_dev *zdev)
 	fib.fmt0.noi = airq_iv_end(zdev->aibv);
 	fib.fmt0.aibv = virt_to_phys(zdev->aibv->vector);
 	fib.fmt0.aibvo = 0;
-	fib.fmt0.aisb = virt_to_phys(aift->sbv->vector + (zdev->aisb / 64) * 8);
+	fib.fmt0.aisb = virt_to_phys(aift->sbv->vector) + (zdev->aisb / 64) * 8;

As you state, one or the other would work, wouldn't it be easy to read like below:

         fib.fmt0.aisb = virt_to_phys(aift->sbv->vector + (zdev->aisb / 64));

arch/s390/kvm/pci.c static int kvm_s390_pci_aif_enable(struct zpci_dev *zdev, struct zpci_fib *fib, · line 312

Quoted 9 lines
arch/s390/kvm/pci.c +1 -1
 	fib.fmt0.aisbo = zdev->aisb & 63;
 	fib.gd = zdev->gisa;
 
@@ -309,7 +309,7 @@ static int kvm_s390_pci_aif_enable(struct zpci_dev *zdev, struct zpci_fib *fib,
 
 	/* Update guest FIB for re-issue */
 	fib->fmt0.aisbo = zdev->aisb & 63;
-	fib->fmt0.aisb = virt_to_phys(aift->sbv->vector + (zdev->aisb / 64) * 8);
+	fib->fmt0.aisb = virt_to_phys(aift->sbv->vector) + (zdev->aisb / 64) * 8;

Same argument as above.

arch/s390/kvm/pci.c static int kvm_s390_pci_aif_enable(struct zpci_dev *zdev, struct zpci_fib *fib, · line 315

Quoted 3 lines
arch/s390/kvm/pci.c +0 -0
 	fib->fmt0.isc = gisc;
 
 	/* Save some guest fib values in the host for later use */

Ouch, good catch! Was this kind of a follow up to the issue someone found upstream with a wrong calculation in the gait? Or did you just notice things break with more than 64 devices?

Thanks,
Niklas

#2 lore raw
Quoted 31 lines · Niklas Schnelle
Quoted 21 lines · Matthew Rosato

The current implementation of aisb calculation will erroneously index via an unsigned long * as well as multiply by 8B for every 64-bits in the offset; only one or the other is required. This throws off aisb calculations once the number of devices exceeds 64, and can result in out-of-bounds access as well as failure to indicate summary bits associated with those devices in guests.

Fix this by converting to a physical address before applying the offset, as is already done in arch/s390/pci/pci_irq.c.

Fixes 3c5a1b6f0a18 ("KVM: s390: pci: provide routines for enabling/disabling interrupt forwarding")
---
 arch/s390/kvm/pci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/s390/kvm/pci.c +0 -0
index 86d93e8dddae..338171f9371b 100644

arch/s390/kvm/pci.c static int kvm_zpci_set_airq(struct zpci_dev *zdev) · line 169

Quoted 5 lines
arch/s390/kvm/pci.c +1 -1
@@ -166,7 +166,7 @@ static int kvm_zpci_set_airq(struct zpci_dev *zdev)
 	fib.fmt0.noi = airq_iv_end(zdev->aibv);
 	fib.fmt0.aibv = virt_to_phys(zdev->aibv->vector);
 	fib.fmt0.aibvo = 0;
-	fib.fmt0.aisb = virt_to_phys(aift->sbv->vector + (zdev->aisb / 64) * 8);
+	fib.fmt0.aisb = virt_to_phys(aift->sbv->vector) + (zdev->aisb / 64) * 8;

As you state, one or the other would work, wouldn't it be easy to read like below:

Quoted 1 line
         fib.fmt0.aisb = virt_to_phys(aift->sbv->vector + (zdev->aisb / 64));

My rationale was to match exactly what zpci_set_airq() in arch/s390/pci/pci_irq.c is doing (I tested that code at the same time to ensure it produced the expected result as a comparison to the code changed by this patch).

I'd rather all 3 places stay the same unless you have a strong opinion on it. I think the reason they ended up different in the first place is because this code and the pci_irq.c code were updated independently for V!=R.

Quoted 13 lines

arch/s390/kvm/pci.c static int kvm_s390_pci_aif_enable(struct zpci_dev *zdev, struct zpci_fib *fib, · line 312

Quoted 10 lines
arch/s390/kvm/pci.c +1 -1
 	fib.fmt0.aisbo = zdev->aisb & 63;
 	fib.gd = zdev->gisa;
 
@@ -309,7 +309,7 @@ static int kvm_s390_pci_aif_enable(struct zpci_dev *zdev, struct zpci_fib *fib,
 
 	/* Update guest FIB for re-issue */
 	fib->fmt0.aisbo = zdev->aisb & 63;
-	fib->fmt0.aisb = virt_to_phys(aift->sbv->vector + (zdev->aisb / 64) * 8);
+	fib->fmt0.aisb = virt_to_phys(aift->sbv->vector) + (zdev->aisb / 64) * 8;

Same argument as above.

Quoted 7 lines

arch/s390/kvm/pci.c static int kvm_s390_pci_aif_enable(struct zpci_dev *zdev, struct zpci_fib *fib, · line 315

Quoted 3 lines
arch/s390/kvm/pci.c +0 -0
 	fib->fmt0.isc = gisc;
 
 	/* Save some guest fib values in the host for later use */

Ouch, good catch! Was this kind of a follow up to the issue someone found upstream with a wrong calculation in the gait? Or did you just notice things break with more than 64 devices?

A follow-up of sorts. Sashiko gave various outputs to that gait patch that were unrelated to it; I'm looking into those things and it led me to at least this issue which I did verify is a real bug.

Thanks,
Matt

Reviewed-by
#3 lore raw
Quoted 39 lines · Matthew Rosato
Quoted 31 lines · Niklas Schnelle
Quoted 21 lines · Matthew Rosato

The current implementation of aisb calculation will erroneously index via an unsigned long * as well as multiply by 8B for every 64-bits in the offset; only one or the other is required. This throws off aisb calculations once the number of devices exceeds 64, and can result in out-of-bounds access as well as failure to indicate summary bits associated with those devices in guests.

Fix this by converting to a physical address before applying the offset, as is already done in arch/s390/pci/pci_irq.c.

Fixes 3c5a1b6f0a18 ("KVM: s390: pci: provide routines for enabling/disabling interrupt forwarding")
---
 arch/s390/kvm/pci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/s390/kvm/pci.c +0 -0
index 86d93e8dddae..338171f9371b 100644

arch/s390/kvm/pci.c static int kvm_zpci_set_airq(struct zpci_dev *zdev) · line 169

Quoted 5 lines
arch/s390/kvm/pci.c +1 -1
@@ -166,7 +166,7 @@ static int kvm_zpci_set_airq(struct zpci_dev *zdev)
 	fib.fmt0.noi = airq_iv_end(zdev->aibv);
 	fib.fmt0.aibv = virt_to_phys(zdev->aibv->vector);
 	fib.fmt0.aibvo = 0;
-	fib.fmt0.aisb = virt_to_phys(aift->sbv->vector + (zdev->aisb / 64) * 8);
+	fib.fmt0.aisb = virt_to_phys(aift->sbv->vector) + (zdev->aisb / 64) * 8;

As you state, one or the other would work, wouldn't it be easy to read like below:

Quoted 1 line
         fib.fmt0.aisb = virt_to_phys(aift->sbv->vector + (zdev->aisb / 64));

My rationale was to match exactly what zpci_set_airq() in arch/s390/pci/pci_irq.c is doing (I tested that code at the same time to ensure it produced the expected result as a comparison to the code changed by this patch).

Quoted 4 lines

I'd rather all 3 places stay the same unless you have a strong opinion on it. I think the reason they ended up different in the first place is because this code and the pci_irq.c code were updated independently for V!=R.

No strong feelings and I do agree about doing the calculation the same as in zpci_set_airq(). Your variant also is understood without knowing that aift->sbv->vector is an unsigned long*. So it's kind of a wash to me.

Quoted 22 lines
Quoted 14 lines

arch/s390/kvm/pci.c static int kvm_s390_pci_aif_enable(struct zpci_dev *zdev, struct zpci_fib *fib, · line 312

Quoted 11 lines
arch/s390/kvm/pci.c +1 -1
 	fib.fmt0.aisbo = zdev->aisb & 63;
 	fib.gd = zdev->gisa;
 
@@ -309,7 +309,7 @@ static int kvm_s390_pci_aif_enable(struct zpci_dev *zdev, struct zpci_fib *fib,
 
 	/* Update guest FIB for re-issue */
 	fib->fmt0.aisbo = zdev->aisb & 63;
-	fib->fmt0.aisb = virt_to_phys(aift->sbv->vector + (zdev->aisb / 64) * 8);
+	fib->fmt0.aisb = virt_to_phys(aift->sbv->vector) + (zdev->aisb / 64) * 8;

Same argument as above.

Quoted 7 lines

arch/s390/kvm/pci.c static int kvm_s390_pci_aif_enable(struct zpci_dev *zdev, struct zpci_fib *fib, · line 315

Quoted 3 lines
arch/s390/kvm/pci.c +0 -0
 	fib->fmt0.isc = gisc;
 
 	/* Save some guest fib values in the host for later use */

Ouch, good catch! Was this kind of a follow up to the issue someone found upstream with a wrong calculation in the gait? Or did you just notice things break with more than 64 devices?

Quoted 6 lines

A follow-up of sorts. Sashiko gave various outputs to that gait patch that were unrelated to it; I'm looking into those things and it led me to at least this issue which I did verify is a real bug.

Thanks,
Matt

Makes sense and great work! Feel free to add mu:

Thanks,
Niklas

maintainer Applied
#4 lore raw
In reply to Matthew Rosato

Am 24.04.26 um 21:39 schrieb Matthew Rosato:

Quoted 7 lines

The current implementation of aisb calculation will erroneously index via an unsigned long * as well as multiply by 8B for every 64-bits in the offset; only one or the other is required. This throws off aisb calculations once the number of devices exceeds 64, and can result in out-of-bounds access as well as failure to indicate summary bits associated with those devices in guests.

Quoted 5 lines

Fix this by converting to a physical address before applying the offset, as is already done in arch/s390/pci/pci_irq.c.

Fixes 3c5a1b6f0a18 ("KVM: s390: pci: provide routines for enabling/disabling interrupt forwarding")

thanks applied.