* [PATCH] KVM: arm64: vgic-v3: Use the minimal BPR for the emulated active priority
@ 2026-12-04 17:19 ` Xuheng Li via B4 Relay
0 siblings, 0 replies; 5+ messages in thread
From: Xuheng Li @ 2026-12-04 17:19 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Eric Auger
Cc: linux-arm-kernel, kvmarm, Xuheng Li
When the ICV_IAR{0,1}_EL1 trap emulation acknowledges an interrupt,
__vgic_v3_set_active_priority() records its priority in
ICH_AP{0,1}R<n>_EL2. It first masks the priority with the group's
current binary point, using __vgic_v3_pri_to_pre(), and only then
normalizes it with the minimal BPR value. The comment above the function
says the priority is normalized using the minimal BPR value, and the
architecture agrees: VirtualReadIAR0/1() set the bit for
VPriorityGroup(Priority), the priority group for the minimum BPR (IHI
0069H.b, 13.1.15 and 13.1.16).
With a guest BPR above the minimum, the emulation therefore sets the
wrong bit. A guest that acknowledges a Group 1 SGI at priority 0x78 with
ICC_BPR1_EL1 = 7 reads ICC_AP1R0_EL1 = 0x1 and ICC_RPR_EL1 = 0x0 with
the trap enabled (kvm-arm.vgic_v3_group1_trap=1), where a GIC-600
without the trap gives 0x8000 and 0x78. While that interrupt is active,
the emulated running priority is the highest possible, so the guest
takes no interrupt that the hardware would let preempt it.
Normalize the priority itself, as the comment describes. The preemption
check in __vgic_v3_read_iar() keeps using the current BPR.
The fix and this message were drafted with an AI assistant (Claude) and
reviewed by the submitter.
Fixes: 132a324ab62f ("KVM: arm64: vgic-v3: Add ICV_IAR1_EL1 handler")
Signed-off-by: Xuheng Li <xuheng@cs.columbia.edu>
---
Tested under QEMU (TCG, nVHE host) with kvm-arm.vgic_v3_group1_trap=1:
the guest above now reads 0x8000 and 0x78, as on the GIC-600, and both
read 0x0 and 0xff again after its EOI.
QEMU's own GICv3 virtual CPU interface, with the trap off, also reports
0x1 and 0x0 for this guest, so it can't serve as the reference here. The
GIC-600 numbers are from an AWS c6g.metal.
---
arch/arm64/kvm/hyp/vgic-v3-sr.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/arch/arm64/kvm/hyp/vgic-v3-sr.c b/arch/arm64/kvm/hyp/vgic-v3-sr.c
index 2dfb5ff9db57..ef8f003cd52f 100644
--- a/arch/arm64/kvm/hyp/vgic-v3-sr.c
+++ b/arch/arm64/kvm/hyp/vgic-v3-sr.c
@@ -669,14 +669,13 @@ static u8 __vgic_v3_pri_to_pre(u8 pri, u32 vmcr, int grp)
* matter what the guest does with its BPR, we can always set/get the
* same value of a priority.
*/
-static void __vgic_v3_set_active_priority(u8 pri, u32 vmcr, int grp)
+static void __vgic_v3_set_active_priority(u8 pri, int grp)
{
- u8 pre, ap;
+ u8 ap;
u32 val;
int apr;
- pre = __vgic_v3_pri_to_pre(pri, vmcr, grp);
- ap = pre >> __vgic_v3_bpr_min();
+ ap = pri >> __vgic_v3_bpr_min();
apr = ap / 32;
if (!grp) {
@@ -752,7 +751,7 @@ static void __vgic_v3_read_iar(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
lr_val &= ~ICH_LR_STATE;
lr_val |= ICH_LR_ACTIVE_BIT;
__gic_v3_set_lr(lr_val, lr);
- __vgic_v3_set_active_priority(lr_prio, vmcr, grp);
+ __vgic_v3_set_active_priority(lr_prio, grp);
vcpu_set_reg(vcpu, rt, lr_val & ICH_LR_VIRTUAL_ID_MASK);
return;
---
base-commit: 007722b8211f7b34fdf21b1e6a9c32497633ea29
change-id: 20261204-vgic-v3-active-priority-5301356cb914
Best regards,
--
Xuheng Li <xuheng@cs.columbia.edu>
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH] KVM: arm64: vgic-v3: Use the minimal BPR for the emulated active priority
@ 2026-12-04 17:19 ` Xuheng Li via B4 Relay
0 siblings, 0 replies; 5+ messages in thread
From: Xuheng Li via B4 Relay @ 2026-12-04 17:19 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Eric Auger
Cc: linux-arm-kernel, kvmarm, Xuheng Li
From: Xuheng Li <xuheng@cs.columbia.edu>
When the ICV_IAR{0,1}_EL1 trap emulation acknowledges an interrupt,
__vgic_v3_set_active_priority() records its priority in
ICH_AP{0,1}R<n>_EL2. It first masks the priority with the group's
current binary point, using __vgic_v3_pri_to_pre(), and only then
normalizes it with the minimal BPR value. The comment above the function
says the priority is normalized using the minimal BPR value, and the
architecture agrees: VirtualReadIAR0/1() set the bit for
VPriorityGroup(Priority), the priority group for the minimum BPR (IHI
0069H.b, 13.1.15 and 13.1.16).
With a guest BPR above the minimum, the emulation therefore sets the
wrong bit. A guest that acknowledges a Group 1 SGI at priority 0x78 with
ICC_BPR1_EL1 = 7 reads ICC_AP1R0_EL1 = 0x1 and ICC_RPR_EL1 = 0x0 with
the trap enabled (kvm-arm.vgic_v3_group1_trap=1), where a GIC-600
without the trap gives 0x8000 and 0x78. While that interrupt is active,
the emulated running priority is the highest possible, so the guest
takes no interrupt that the hardware would let preempt it.
Normalize the priority itself, as the comment describes. The preemption
check in __vgic_v3_read_iar() keeps using the current BPR.
The fix and this message were drafted with an AI assistant (Claude) and
reviewed by the submitter.
Fixes: 132a324ab62f ("KVM: arm64: vgic-v3: Add ICV_IAR1_EL1 handler")
Signed-off-by: Xuheng Li <xuheng@cs.columbia.edu>
---
Tested under QEMU (TCG, nVHE host) with kvm-arm.vgic_v3_group1_trap=1:
the guest above now reads 0x8000 and 0x78, as on the GIC-600, and both
read 0x0 and 0xff again after its EOI.
QEMU's own GICv3 virtual CPU interface, with the trap off, also reports
0x1 and 0x0 for this guest, so it can't serve as the reference here. The
GIC-600 numbers are from an AWS c6g.metal.
---
arch/arm64/kvm/hyp/vgic-v3-sr.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/arch/arm64/kvm/hyp/vgic-v3-sr.c b/arch/arm64/kvm/hyp/vgic-v3-sr.c
index 2dfb5ff9db57..ef8f003cd52f 100644
--- a/arch/arm64/kvm/hyp/vgic-v3-sr.c
+++ b/arch/arm64/kvm/hyp/vgic-v3-sr.c
@@ -669,14 +669,13 @@ static u8 __vgic_v3_pri_to_pre(u8 pri, u32 vmcr, int grp)
* matter what the guest does with its BPR, we can always set/get the
* same value of a priority.
*/
-static void __vgic_v3_set_active_priority(u8 pri, u32 vmcr, int grp)
+static void __vgic_v3_set_active_priority(u8 pri, int grp)
{
- u8 pre, ap;
+ u8 ap;
u32 val;
int apr;
- pre = __vgic_v3_pri_to_pre(pri, vmcr, grp);
- ap = pre >> __vgic_v3_bpr_min();
+ ap = pri >> __vgic_v3_bpr_min();
apr = ap / 32;
if (!grp) {
@@ -752,7 +751,7 @@ static void __vgic_v3_read_iar(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
lr_val &= ~ICH_LR_STATE;
lr_val |= ICH_LR_ACTIVE_BIT;
__gic_v3_set_lr(lr_val, lr);
- __vgic_v3_set_active_priority(lr_prio, vmcr, grp);
+ __vgic_v3_set_active_priority(lr_prio, grp);
vcpu_set_reg(vcpu, rt, lr_val & ICH_LR_VIRTUAL_ID_MASK);
return;
---
base-commit: 007722b8211f7b34fdf21b1e6a9c32497633ea29
change-id: 20261204-vgic-v3-active-priority-5301356cb914
Best regards,
--
Xuheng Li <xuheng@cs.columbia.edu>
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] KVM: arm64: vgic-v3: Use the minimal BPR for the emulated active priority
2026-12-04 17:19 ` Xuheng Li via B4 Relay
(?)
@ 2026-12-04 17:32 ` sashiko-bot
2026-12-04 17:44 ` Xuheng Li
-1 siblings, 1 reply; 5+ messages in thread
From: sashiko-bot @ 2026-12-04 17:32 UTC (permalink / raw)
To: Xuheng Li; +Cc: kvmarm, Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] KVM: arm64: asymmetric BPR masking in __vgic_v3_read_iar()
preemption check
- [High] KVM: arm64: BPR mask mismatch in __vgic_v3_write_eoir() skips
LR deactivation
--
commit 4db6cbe64dc25717be9406f747d530f50d9a2d05
Author: Xuheng Li <xuheng@cs.columbia.edu>
KVM: arm64: vgic-v3: Use the minimal BPR for the emulated active priority
This commit stops masking the interrupt priority with the guest's current
BPR in __vgic_v3_set_active_priority(), normalizing the priority directly
with __vgic_v3_bpr_min() before recording it in ICH_AP{0,1}R<n>_EL2.
> diff --git a/arch/arm64/kvm/hyp/vgic-v3-sr.c b/arch/arm64/kvm/hyp/vgic-v3-sr.c
> index 2dfb5ff9db575..ef8f003cd52f9 100644
> --- a/arch/arm64/kvm/hyp/vgic-v3-sr.c
> +++ b/arch/arm64/kvm/hyp/vgic-v3-sr.c
> @@ -669,14 +669,13 @@ static u8 __vgic_v3_pri_to_pre(u8 pri, u32 vmcr, int grp)
> * matter what the guest does with its BPR, we can always set/get the
> * same value of a priority.
> */
> -static void __vgic_v3_set_active_priority(u8 pri, u32 vmcr, int grp)
> +static void __vgic_v3_set_active_priority(u8 pri, int grp)
> {
> - u8 pre, ap;
> + u8 ap;
> u32 val;
> int apr;
>
> - pre = __vgic_v3_pri_to_pre(pri, vmcr, grp);
> - ap = pre >> __vgic_v3_bpr_min();
> + ap = pri >> __vgic_v3_bpr_min();
> apr = ap / 32;
[Severity: High]
Does removing __vgic_v3_pri_to_pre() here break interrupt deactivation in
__vgic_v3_write_eoir()?
arch/arm64/kvm/hyp/vgic-v3-sr.c:__vgic_v3_write_eoir() {
...
act_prio = __vgic_v3_clear_highest_active_priority();
...
if (grp != !!(lr_val & ICH_LR_GROUP) ||
__vgic_v3_pri_to_pre(lr_prio, vmcr, grp) != act_prio)
return;
/* Let's now perform the deactivation */
__vgic_v3_clear_active_lr(lr, lr_val);
}
__vgic_v3_clear_highest_active_priority() now returns act_prio
normalized only to __vgic_v3_bpr_min(). With ICC_BPR1_EL1 == 7 and
priority 0x78, act_prio is 0x78 while __vgic_v3_pri_to_pre() yields
0x00, so __vgic_v3_write_eoir() returns early before clearing the LR.
For EOImode == 0 interrupts and LPIs in List Registers, this leaves
ICH_LR_ACTIVE_BIT set (and skips gic_write_dir() for ICH_LR_HW
interrupts), wedging the interrupt active even though the APR bit was
already cleared.
[ ... ]
> @@ -752,7 +751,7 @@ static void __vgic_v3_read_iar(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
> lr_val &= ~ICH_LR_STATE;
> lr_val |= ICH_LR_ACTIVE_BIT;
> __gic_v3_set_lr(lr_val, lr);
> - __vgic_v3_set_active_priority(lr_prio, vmcr, grp);
> + __vgic_v3_set_active_priority(lr_prio, grp);
> vcpu_set_reg(vcpu, rt, lr_val & ICH_LR_VIRTUAL_ID_MASK);
> return;
[Severity: High]
Can this asymmetric BPR masking in __vgic_v3_read_iar() let lower- or
equal-priority interrupts preempt an active interrupt?
arch/arm64/kvm/hyp/vgic-v3-sr.c:__vgic_v3_read_iar() {
...
if (__vgic_v3_get_highest_active_priority() <= __vgic_v3_pri_to_pre(lr_prio, vmcr, grp))
goto spurious;
...
}
__vgic_v3_get_highest_active_priority() now keeps bits below the
current BPR while the right-hand side masks them off. With 5
preemption bits and ICC_BPR1_EL1 == 7, an active interrupt at 0x40
is preempted by a lower-priority one at 0x78 (masked to 0x00).
That sets a second APR bit so the preempting interrupt's EOI clears the
higher-priority interrupt's bit instead (and equal-priority preemption
aliases the same APR bit). Masking the active priority with the current
BPR as well needs to preserve GICv3_IDLE_PRIORITY (0xff).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-vgic-v3-active-priority-v1-1-9ce5187224ce@cs.columbia.edu?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] KVM: arm64: vgic-v3: Use the minimal BPR for the emulated active priority
2026-12-04 17:32 ` sashiko-bot
@ 2026-12-04 17:44 ` Xuheng Li
2026-12-05 9:29 ` Marc Zyngier
0 siblings, 1 reply; 5+ messages in thread
From: Xuheng Li @ 2026-12-04 17:44 UTC (permalink / raw)
To: sashiko-reviews; +Cc: kvmarm, Oliver Upton, Marc Zyngier
Both points are right: this version only changes the recorded active
priority, and leaves __vgic_v3_write_eoir() and the preemption check in
__vgic_v3_read_iar() on the old BPR-masked comparison. Please disregard
it; I'll follow up with a v2 that fixes both.
--
Xuheng Li
--
Xuheng Li
On Fri, Dec 4, 2026 at 12:32 PM <sashiko-bot@kernel.org> wrote:
>
> Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] KVM: arm64: asymmetric BPR masking in __vgic_v3_read_iar() preemption check - [High] KVM: arm64: BPR mask mismatch in __vgic_v3_write_eoir() skips
> ZjQcmQRYFpfptBannerStart
> This Message Is From an External Sender
> This message came from outside your organization.
>
> ZjQcmQRYFpfptBannerEnd
>
> Thank you for your contribution! Sashiko AI review found 2 potential
> issues to consider:
> - [High] KVM: arm64: asymmetric BPR masking in __vgic_v3_read_iar()
> preemption check
> - [High] KVM: arm64: BPR mask mismatch in __vgic_v3_write_eoir() skips
> LR deactivation
> --
>
> commit 4db6cbe64dc25717be9406f747d530f50d9a2d05
> Author: Xuheng Li <xuheng@cs.columbia.edu>
>
> KVM: arm64: vgic-v3: Use the minimal BPR for the emulated active priority
>
> This commit stops masking the interrupt priority with the guest's current
> BPR in __vgic_v3_set_active_priority(), normalizing the priority directly
> with __vgic_v3_bpr_min() before recording it in ICH_AP{0,1}R<n>_EL2.
>
> > diff --git a/arch/arm64/kvm/hyp/vgic-v3-sr.c b/arch/arm64/kvm/hyp/vgic-v3-sr.c
> > index 2dfb5ff9db575..ef8f003cd52f9 100644
> > --- a/arch/arm64/kvm/hyp/vgic-v3-sr.c
> > +++ b/arch/arm64/kvm/hyp/vgic-v3-sr.c
> > @@ -669,14 +669,13 @@ static u8 __vgic_v3_pri_to_pre(u8 pri, u32 vmcr, int grp)
> > * matter what the guest does with its BPR, we can always set/get the
> > * same value of a priority.
> > */
> > -static void __vgic_v3_set_active_priority(u8 pri, u32 vmcr, int grp)
> > +static void __vgic_v3_set_active_priority(u8 pri, int grp)
> > {
> > - u8 pre, ap;
> > + u8 ap;
> > u32 val;
> > int apr;
> >
> > - pre = __vgic_v3_pri_to_pre(pri, vmcr, grp);
> > - ap = pre >> __vgic_v3_bpr_min();
> > + ap = pri >> __vgic_v3_bpr_min();
> > apr = ap / 32;
>
> [Severity: High]
> Does removing __vgic_v3_pri_to_pre() here break interrupt deactivation in
> __vgic_v3_write_eoir()?
>
> arch/arm64/kvm/hyp/vgic-v3-sr.c:__vgic_v3_write_eoir() {
> ...
> act_prio = __vgic_v3_clear_highest_active_priority();
> ...
> if (grp != !!(lr_val & ICH_LR_GROUP) ||
> __vgic_v3_pri_to_pre(lr_prio, vmcr, grp) != act_prio)
> return;
>
> /* Let's now perform the deactivation */
> __vgic_v3_clear_active_lr(lr, lr_val);
> }
>
> __vgic_v3_clear_highest_active_priority() now returns act_prio
> normalized only to __vgic_v3_bpr_min(). With ICC_BPR1_EL1 == 7 and
> priority 0x78, act_prio is 0x78 while __vgic_v3_pri_to_pre() yields
> 0x00, so __vgic_v3_write_eoir() returns early before clearing the LR.
>
> For EOImode == 0 interrupts and LPIs in List Registers, this leaves
> ICH_LR_ACTIVE_BIT set (and skips gic_write_dir() for ICH_LR_HW
> interrupts), wedging the interrupt active even though the APR bit was
> already cleared.
>
> [ ... ]
>
> > @@ -752,7 +751,7 @@ static void __vgic_v3_read_iar(struct kvm_vcpu *vcpu, u32 vmcr, int rt)
> > lr_val &= ~ICH_LR_STATE;
> > lr_val |= ICH_LR_ACTIVE_BIT;
> > __gic_v3_set_lr(lr_val, lr);
> > - __vgic_v3_set_active_priority(lr_prio, vmcr, grp);
> > + __vgic_v3_set_active_priority(lr_prio, grp);
> > vcpu_set_reg(vcpu, rt, lr_val & ICH_LR_VIRTUAL_ID_MASK);
> > return;
>
> [Severity: High]
> Can this asymmetric BPR masking in __vgic_v3_read_iar() let lower- or
> equal-priority interrupts preempt an active interrupt?
>
> arch/arm64/kvm/hyp/vgic-v3-sr.c:__vgic_v3_read_iar() {
> ...
> if (__vgic_v3_get_highest_active_priority() <= __vgic_v3_pri_to_pre(lr_prio, vmcr, grp))
> goto spurious;
> ...
> }
>
> __vgic_v3_get_highest_active_priority() now keeps bits below the
> current BPR while the right-hand side masks them off. With 5
> preemption bits and ICC_BPR1_EL1 == 7, an active interrupt at 0x40
> is preempted by a lower-priority one at 0x78 (masked to 0x00).
>
> That sets a second APR bit so the preempting interrupt's EOI clears the
> higher-priority interrupt's bit instead (and equal-priority preemption
> aliases the same APR bit). Masking the active priority with the current
> BPR as well needs to preserve GICv3_IDLE_PRIORITY (0xff).
>
> --
> Sashiko AI review · https://urldefense.com/v3/__https://sashiko.dev/*/patchset/20261204-vgic-v3-active-priority-v1-1-9ce5187224ce@cs.columbia.edu?part=1__;Iw!!BDUfV1Et5lrpZQ!Uk84SO-dcQK0c5Iex2KyHDghOFA4jfO1blQQsZ9OXF6dFIocnfNtHjyHJGCxw-dPbzApoSuwy6m8E0NhM38J7cHHb6Q$
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] KVM: arm64: vgic-v3: Use the minimal BPR for the emulated active priority
2026-12-04 17:44 ` Xuheng Li
@ 2026-12-05 9:29 ` Marc Zyngier
0 siblings, 0 replies; 5+ messages in thread
From: Marc Zyngier @ 2026-12-05 9:29 UTC (permalink / raw)
To: Xuheng Li; +Cc: sashiko-reviews, kvmarm, Oliver Upton
On Fri, 04 Dec 2026 17:44:48 +0000,
Xuheng Li <xuheng@cs.columbia.edu> wrote:
>
> Both points are right: this version only changes the recorded active
> priority, and leaves __vgic_v3_write_eoir() and the preemption check in
> __vgic_v3_read_iar() on the old BPR-masked comparison. Please disregard
> it; I'll follow up with a v2 that fixes both.
When you do, please include an update to the vgic_irq test so that we
can check for regression, as I'm afraid that non-default BPR values
are a pretty niche game (and doing that on broken HW that requires
emulation is even more niche).
Thanks,
M.
--
Without deviation from the norm, progress is not possible.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-05 9:29 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 17:19 [PATCH] KVM: arm64: vgic-v3: Use the minimal BPR for the emulated active priority Xuheng Li
2026-12-04 17:19 ` Xuheng Li via B4 Relay
2026-12-04 17:32 ` sashiko-bot
2026-12-04 17:44 ` Xuheng Li
2026-12-05 9:29 ` Marc Zyngier
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.