KVM: x86/xen: defer timer event delivery on PREEMPT_RT
Subsystems (4)
PATCH 1/1 KVM: x86/xen: defer timer event delivery on PREEMPT_RT
xen_timer_callback() runs from an hrtimer, so it executes with interrupts disabled. It calls kvm_xen_set_evtchn_fast(), which acquires gpc->lock with read_lock_irqsave(). On PREEMPT_RT rwlock_t is a sleeping lock, and taking it in that context reports:
BUG: sleeping function called from invalid context at
kernel/locking/spinlock_rt.c:248
in_atomic(): 1, irqs_disabled(): 1
kvm_xen_set_evtchn_fast
xen_timer_callbackThe callback already handles -EWOULDBLOCK by recording the pending timer and raising KVM_REQ_UNBLOCK, and the delivery is then completed from vCPU context by kvm_xen_inject_timer_irqs(). On PREEMPT_RT, skip the fast path and let that deferred path run instead of taking a sleeping lock from hardirq context.
--- arch/x86/kvm/xen.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/xen.c +5 -1
@@ -136,7 +136,11 @@ static enum hrtimer_restart xen_timer_callback(struct hrtimer *timer) e.port = vcpu->arch.xen.timer_virq; e.priority =KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL ; - rc =kvm_xen_set_evtchn_fast (&e, vcpu->kvm); +if (IS_ENABLED (CONFIG_PREEMPT_RT ) && !preemptible ()) + rc = -EWOULDBLOCK ; +else + rc =kvm_xen_set_evtchn_fast (&e, vcpu->kvm); +if (rc != -EWOULDBLOCK ) { vcpu->arch.xen.timer_expires =0 ;return HRTIMER_NORESTART ;
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] KVM: x86/xen: missing Fixes trailer for commit 77c9b9dea4fb --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] KVM: x86/xen: missing Fixes trailer for commit 77c9b9dea4fb --
commit 21cb04c9f920ff2e2b8066f225f0bcf72b648f06
Author: Yang Luohan <[email protected]>
Quoted 13 lines
KVM: x86/xen: defer timer event delivery on
PREEMPT_RT
xen_timer_callback()runs from an hrtimer, so it executes with interrupts disabled. It callskvm_xen_set_evtchn_fast(), which acquiresgpc->lockwithread_lock_irqsave(). OnPREEMPT_RTrwlock_tis a sleeping lock, and taking it in that context reports:BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:248 in_atomic(): 1, irqs_disabled(): 1 kvm_xen_set_evtchn_fast xen_timer_callback
Quoted 7 lines
The callback already handles
-EWOULDBLOCKby recording the pending timer and raisingKVM_REQ_UNBLOCK, and the delivery is then completed from vCPU context bykvm_xen_inject_timer_irqs(). OnPREEMPT_RT, skip the fast path and let that deferred path run instead of taking a sleeping lock from hardirq context.Signed-off-by Yang Luohan <[email protected]>
[Severity: Low]
This fixes a sleeping-in-invalid-context bug on PREEMPT_RT introduced when xen_timer_callback() started calling kvm_xen_set_evtchn_fast(). Should this include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 KVM: x86/xen: defer timer event delivery on PREEMPT_RT
In reply to Yang Luohanxen_timer_callback() runs from an hrtimer, so it executes with interrupts disabled. It calls kvm_xen_set_evtchn_fast(), which acquires gpc->lock with read_lock_irqsave(). On PREEMPT_RT rwlock_t is a sleeping lock, and taking it in that context reports:
BUG: sleeping function called from invalid context at
kernel/locking/spinlock_rt.c:248
in_atomic(): 1, irqs_disabled(): 1
kvm_xen_set_evtchn_fast
xen_timer_callbackThe callback already handles -EWOULDBLOCK by recording the pending timer and raising KVM_REQ_UNBLOCK, and the delivery is then completed from vCPU context by kvm_xen_inject_timer_irqs(). On PREEMPT_RT, skip the fast path and let that deferred path run instead of taking a sleeping lock from hardirq context.
--- arch/x86/kvm/xen.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/xen.c +5 -1
@@ -136,7 +136,11 @@ static enum hrtimer_restart xen_timer_callback(struct hrtimer *timer) e.port = vcpu->arch.xen.timer_virq; e.priority =KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL ; - rc =kvm_xen_set_evtchn_fast (&e, vcpu->kvm); +if (IS_ENABLED (CONFIG_PREEMPT_RT ) && !preemptible ()) + rc = -EWOULDBLOCK ; +else + rc =kvm_xen_set_evtchn_fast (&e, vcpu->kvm); +if (rc != -EWOULDBLOCK ) { vcpu->arch.xen.timer_expires =0 ;return HRTIMER_NORESTART ;
Signature · 1 line
2.43.0
Sean Christopherson
maintainerQuoted 28 lines · Yang Luohan
xen_timer_callback()runs from an hrtimer, so it executes with interrupts disabled. It callskvm_xen_set_evtchn_fast(), which acquiresgpc->lockwithread_lock_irqsave(). OnPREEMPT_RTrwlock_tis a sleeping lock, and taking it in that context reports:BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:248 in_atomic(): 1, irqs_disabled(): 1 kvm_xen_set_evtchn_fast xen_timer_callbackThe callback already handles
-EWOULDBLOCKby recording the pending timer and raisingKVM_REQ_UNBLOCK, and the delivery is then completed from vCPU context bykvm_xen_inject_timer_irqs(). OnPREEMPT_RT, skip the fast path and let that deferred path run instead of taking a sleeping lock from hardirq context.Fixes 77c9b9dea4fb ("KVM: x86/xen: Use fast path for Xen timer delivery")Signed-off-by Yang Luohan <[email protected]>--- arch/x86/kvm/xen.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)arch/x86/kvm/xen.c +0
-0
arch/x86/kvm/xen.c static enum hrtimer_restart xen_timer_callback(struct hrtimer *timer) · line 143
Quoted 9 lines
arch/x86/kvm/xen.c +5
-1@@ -136,7 +136,11 @@ static enum hrtimer_restart xen_timer_callback(struct hrtimer *timer) e.port = vcpu->arch.xen.timer_virq; e.priority =KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL ; - rc =kvm_xen_set_evtchn_fast (&e, vcpu->kvm); +if (IS_ENABLED (CONFIG_PREEMPT_RT ) && !preemptible ()) + rc = -EWOULDBLOCK ; +else + rc =kvm_xen_set_evtchn_fast (&e, vcpu->kvm); +
This is a long-known issue, and is being addressed by switching away from rwlocks entirely.
Thanks!
David Woodhouse
maintainerQuoted 42 lines · Sean Christopherson
Quoted 31 lines · Yang Luohan
xen_timer_callback()runs from an hrtimer, so it executes with interrupts disabled. It callskvm_xen_set_evtchn_fast(), which acquiresgpc->lockwithread_lock_irqsave(). OnPREEMPT_RTrwlock_tis a sleeping lock, and taking it in that context reports:BUG: sleeping function called from invalid context at
kernel/locking/spinlock_rt.c:248
in_atomic(): 1,irqs_disabled(): 1
kvm_xen_set_evtchn_fast
xen_timer_callbackThe callback already handles
-EWOULDBLOCKby recording the pending timer and raisingKVM_REQ_UNBLOCK, and the delivery is then completed from vCPU context bykvm_xen_inject_timer_irqs(). OnPREEMPT_RT, skip the fast path and let that deferred path run instead of taking a sleeping lock from hardirq context.Fixes 77c9b9dea4fb ("KVM: x86/xen: Use fast path for Xen timer delivery")Signed-off-by Yang Luohan <[email protected]>---
arch/x86/kvm/xen.c | 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)arch/x86/kvm/xen.c +0
-0@@ -136,7 +136,11 @@ static enum hrtimer_restart xen_timer_callback(struct hrtimer *timer) e.port = vcpu->arch.xen.timer_virq; e.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;arch/x86/kvm/xen.c static enum hrtimer_restart xen_timer_callback(struct hrtimer *timer) · line 143
Quoted 6 lines
arch/x86/kvm/xen.c +5
-1- rc =kvm_xen_set_evtchn_fast (&e, vcpu->kvm); +if (IS_ENABLED (CONFIG_PREEMPT_RT ) && !preemptible ()) + rc = -EWOULDBLOCK ; +else + rc =kvm_xen_set_evtchn_fast (&e, vcpu->kvm); +This is a long-known issue, and is being addressed by switching away from rwlocks entirely.
Quoted 3 lines
Thanks!
We said you'd pick up patches 1-9 of that series as-is, and I'd keep on with refining the nested stuff in the latter half of the series later, yes?
Sean Christopherson
maintainerQuoted 43 lines · David Woodhouse
Quoted 42 lines · Sean Christopherson
Quoted 31 lines · Yang Luohan
xen_timer_callback()runs from an hrtimer, so it executes with interrupts disabled. It callskvm_xen_set_evtchn_fast(), which acquiresgpc->lockwithread_lock_irqsave(). OnPREEMPT_RTrwlock_tis a sleeping lock, and taking it in that context reports:BUG: sleeping function called from invalid context at
kernel/locking/spinlock_rt.c:248
in_atomic(): 1,irqs_disabled(): 1
kvm_xen_set_evtchn_fast
xen_timer_callbackThe callback already handles
-EWOULDBLOCKby recording the pending timer and raisingKVM_REQ_UNBLOCK, and the delivery is then completed from vCPU context bykvm_xen_inject_timer_irqs(). OnPREEMPT_RT, skip the fast path and let that deferred path run instead of taking a sleeping lock from hardirq context.Fixes 77c9b9dea4fb ("KVM: x86/xen: Use fast path for Xen timer delivery")Signed-off-by Yang Luohan <[email protected]>---
arch/x86/kvm/xen.c | 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)arch/x86/kvm/xen.c +0
-0@@ -136,7 +136,11 @@ static enum hrtimer_restart xen_timer_callback(struct hrtimer *timer) e.port = vcpu->arch.xen.timer_virq; e.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;arch/x86/kvm/xen.c static enum hrtimer_restart xen_timer_callback(struct hrtimer *timer) · line 143
Quoted 6 lines
arch/x86/kvm/xen.c +5
-1- rc =kvm_xen_set_evtchn_fast (&e, vcpu->kvm); +if (IS_ENABLED (CONFIG_PREEMPT_RT ) && !preemptible ()) + rc = -EWOULDBLOCK ; +else + rc =kvm_xen_set_evtchn_fast (&e, vcpu->kvm); +This is a long-known issue, and is being addressed by switching away from rwlocks entirely.
Quoted 7 lines
Quoted 3 lines
Thanks!
We said you'd pick up patches 1-9 of that series as-is, and I'd keep on with refining the nested stuff in the latter half of the series later, yes?
Yep, I'm trying to get there.
David Woodhouse
maintainerQuoted 9 lines · Sean Christopherson
Quoted 6 lines · David Woodhouse
Quoted 1 line · Sean Christopherson
We said you'd pick up patches 1-9 of that series as-is, and I'd keep on with refining the nested stuff in the latter half of the series later, yes?
Yep, I'm trying to get there.
Ack, thanks. I guess the one thing that might tweak my series slightly is the outcome of the TLB flush thing — are you still planning to do that first? Got something I can rebase onto?