kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kvm: Check irqchip mode before assign irqfd
@ 2019-05-05  8:56 Peter Xu
  2019-05-05  9:20 ` Peter Xu
  2019-05-20 12:54 ` Paolo Bonzini
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Xu @ 2019-05-05  8:56 UTC (permalink / raw)
  To: kvm
  Cc: peterx, Paolo Bonzini, Radim Krčmář,
	Alex Williamson, Eduardo Habkost

When assigning kvm irqfd we didn't check the irqchip mode but we allow
KVM_IRQFD to succeed with all the irqchip modes.  However it does not
make much sense to create irqfd even without the kernel chips.  Let's
provide a arch-dependent helper to check whether a specific irqfd is
allowed by the arch.  At least for x86, it should make sense to check:

- when irqchip mode is NONE, all irqfds should be disallowed, and,

- when irqchip mode is SPLIT, irqfds that are with resamplefd should
  be disallowed.

For either of the case, previously we'll silently ignore the irq or
the irq ack event if the irqchip mode is incorrect.  However that can
cause misterious guest behaviors and it can be hard to triage.  Let's
fail KVM_IRQFD even earlier to detect these incorrect configurations.

CC: Paolo Bonzini <pbonzini@redhat.com>
CC: Radim Krčmář <rkrcmar@redhat.com>
CC: Alex Williamson <alex.williamson@redhat.com>
CC: Eduardo Habkost <ehabkost@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 arch/x86/kvm/irq.c | 7 +++++++
 arch/x86/kvm/irq.h | 1 +
 virt/kvm/eventfd.c | 9 +++++++++
 3 files changed, 17 insertions(+)

diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c
index faa264822cee..007bc654f928 100644
--- a/arch/x86/kvm/irq.c
+++ b/arch/x86/kvm/irq.c
@@ -172,3 +172,10 @@ void __kvm_migrate_timers(struct kvm_vcpu *vcpu)
 	__kvm_migrate_apic_timer(vcpu);
 	__kvm_migrate_pit_timer(vcpu);
 }
+
+bool kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args)
+{
+	bool resample = args->flags & KVM_IRQFD_FLAG_RESAMPLE;
+
+	return resample ? irqchip_kernel(kvm) : irqchip_in_kernel(kvm);
+}
diff --git a/arch/x86/kvm/irq.h b/arch/x86/kvm/irq.h
index d5005cc26521..fd210cdd4983 100644
--- a/arch/x86/kvm/irq.h
+++ b/arch/x86/kvm/irq.h
@@ -114,6 +114,7 @@ static inline int irqchip_in_kernel(struct kvm *kvm)
 	return mode != KVM_IRQCHIP_NONE;
 }
 
+bool kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args);
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu);
 void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu);
 void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu);
diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c
index 001aeda4c154..3972a9564c76 100644
--- a/virt/kvm/eventfd.c
+++ b/virt/kvm/eventfd.c
@@ -44,6 +44,12 @@
 
 static struct workqueue_struct *irqfd_cleanup_wq;
 
+bool __attribute__((weak))
+kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args)
+{
+	return true;
+}
+
 static void
 irqfd_inject(struct work_struct *work)
 {
@@ -297,6 +303,9 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
 	if (!kvm_arch_intc_initialized(kvm))
 		return -EAGAIN;
 
+	if (!kvm_arch_irqfd_allowed(kvm, args))
+		return -EINVAL;
+
 	irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL_ACCOUNT);
 	if (!irqfd)
 		return -ENOMEM;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] kvm: Check irqchip mode before assign irqfd
  2019-05-05  8:56 [PATCH] kvm: Check irqchip mode before assign irqfd Peter Xu
@ 2019-05-05  9:20 ` Peter Xu
  2019-05-17  2:23   ` Peter Xu
  2019-05-20 12:54 ` Paolo Bonzini
  1 sibling, 1 reply; 4+ messages in thread
From: Peter Xu @ 2019-05-05  9:20 UTC (permalink / raw)
  To: kvm
  Cc: Paolo Bonzini, Radim Krčmář,
	Alex Williamson, Eduardo Habkost

On Sun, May 05, 2019 at 04:56:42PM +0800, Peter Xu wrote:
> When assigning kvm irqfd we didn't check the irqchip mode but we allow
> KVM_IRQFD to succeed with all the irqchip modes.  However it does not
> make much sense to create irqfd even without the kernel chips.  Let's
> provide a arch-dependent helper to check whether a specific irqfd is
> allowed by the arch.  At least for x86, it should make sense to check:
> 
> - when irqchip mode is NONE, all irqfds should be disallowed, and,
> 
> - when irqchip mode is SPLIT, irqfds that are with resamplefd should
>   be disallowed.
> 
> For either of the case, previously we'll silently ignore the irq or
> the irq ack event if the irqchip mode is incorrect.  However that can
> cause misterious guest behaviors and it can be hard to triage.  Let's
> fail KVM_IRQFD even earlier to detect these incorrect configurations.
> 
> CC: Paolo Bonzini <pbonzini@redhat.com>
> CC: Radim Krčmář <rkrcmar@redhat.com>
> CC: Alex Williamson <alex.williamson@redhat.com>
> CC: Eduardo Habkost <ehabkost@redhat.com>
> Signed-off-by: Peter Xu <peterx@redhat.com>

Note: haven't tested, but IIUC QEMU's vfio will naturally fall back to
no-irqfd mode (actually virtio seems to also have this but virtio
should not be affected after all) if the KVM_IRQFD ioctl failed so I
feel like this patch could also at least fix the broken guests
reported besides any future fixes from QEMU side on the issue:

https://bugs.launchpad.net/qemu/+bug/1826422

Thanks,

-- 
Peter Xu

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kvm: Check irqchip mode before assign irqfd
  2019-05-05  9:20 ` Peter Xu
@ 2019-05-17  2:23   ` Peter Xu
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Xu @ 2019-05-17  2:23 UTC (permalink / raw)
  To: kvm
  Cc: Paolo Bonzini, Radim Krčmář,
	Alex Williamson, Eduardo Habkost

On Sun, May 05, 2019 at 05:20:22PM +0800, Peter Xu wrote:
> On Sun, May 05, 2019 at 04:56:42PM +0800, Peter Xu wrote:
> > When assigning kvm irqfd we didn't check the irqchip mode but we allow
> > KVM_IRQFD to succeed with all the irqchip modes.  However it does not
> > make much sense to create irqfd even without the kernel chips.  Let's
> > provide a arch-dependent helper to check whether a specific irqfd is
> > allowed by the arch.  At least for x86, it should make sense to check:
> > 
> > - when irqchip mode is NONE, all irqfds should be disallowed, and,
> > 
> > - when irqchip mode is SPLIT, irqfds that are with resamplefd should
> >   be disallowed.
> > 
> > For either of the case, previously we'll silently ignore the irq or
> > the irq ack event if the irqchip mode is incorrect.  However that can
> > cause misterious guest behaviors and it can be hard to triage.  Let's
> > fail KVM_IRQFD even earlier to detect these incorrect configurations.
> > 
> > CC: Paolo Bonzini <pbonzini@redhat.com>
> > CC: Radim Krčmář <rkrcmar@redhat.com>
> > CC: Alex Williamson <alex.williamson@redhat.com>
> > CC: Eduardo Habkost <ehabkost@redhat.com>
> > Signed-off-by: Peter Xu <peterx@redhat.com>
> 
> Note: haven't tested, but IIUC QEMU's vfio will naturally fall back to
> no-irqfd mode (actually virtio seems to also have this but virtio
> should not be affected after all) if the KVM_IRQFD ioctl failed so I
> feel like this patch could also at least fix the broken guests
> reported besides any future fixes from QEMU side on the issue:
> 
> https://bugs.launchpad.net/qemu/+bug/1826422

Ping - Paolo, should patch still worth to have before the complete fix
between split irqchip & resamplefds?

Thanks,

-- 
Peter Xu

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kvm: Check irqchip mode before assign irqfd
  2019-05-05  8:56 [PATCH] kvm: Check irqchip mode before assign irqfd Peter Xu
  2019-05-05  9:20 ` Peter Xu
@ 2019-05-20 12:54 ` Paolo Bonzini
  1 sibling, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2019-05-20 12:54 UTC (permalink / raw)
  To: Peter Xu, kvm
  Cc: Radim Krčmář, Alex Williamson, Eduardo Habkost

On 05/05/19 10:56, Peter Xu wrote:
> When assigning kvm irqfd we didn't check the irqchip mode but we allow
> KVM_IRQFD to succeed with all the irqchip modes.  However it does not
> make much sense to create irqfd even without the kernel chips.  Let's
> provide a arch-dependent helper to check whether a specific irqfd is
> allowed by the arch.  At least for x86, it should make sense to check:
> 
> - when irqchip mode is NONE, all irqfds should be disallowed, and,
> 
> - when irqchip mode is SPLIT, irqfds that are with resamplefd should
>   be disallowed.
> 
> For either of the case, previously we'll silently ignore the irq or
> the irq ack event if the irqchip mode is incorrect.  However that can
> cause misterious guest behaviors and it can be hard to triage.  Let's
> fail KVM_IRQFD even earlier to detect these incorrect configurations.
> 
> CC: Paolo Bonzini <pbonzini@redhat.com>
> CC: Radim Krčmář <rkrcmar@redhat.com>
> CC: Alex Williamson <alex.williamson@redhat.com>
> CC: Eduardo Habkost <ehabkost@redhat.com>
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  arch/x86/kvm/irq.c | 7 +++++++
>  arch/x86/kvm/irq.h | 1 +
>  virt/kvm/eventfd.c | 9 +++++++++
>  3 files changed, 17 insertions(+)
> 
> diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c
> index faa264822cee..007bc654f928 100644
> --- a/arch/x86/kvm/irq.c
> +++ b/arch/x86/kvm/irq.c
> @@ -172,3 +172,10 @@ void __kvm_migrate_timers(struct kvm_vcpu *vcpu)
>  	__kvm_migrate_apic_timer(vcpu);
>  	__kvm_migrate_pit_timer(vcpu);
>  }
> +
> +bool kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args)
> +{
> +	bool resample = args->flags & KVM_IRQFD_FLAG_RESAMPLE;
> +
> +	return resample ? irqchip_kernel(kvm) : irqchip_in_kernel(kvm);
> +}
> diff --git a/arch/x86/kvm/irq.h b/arch/x86/kvm/irq.h
> index d5005cc26521..fd210cdd4983 100644
> --- a/arch/x86/kvm/irq.h
> +++ b/arch/x86/kvm/irq.h
> @@ -114,6 +114,7 @@ static inline int irqchip_in_kernel(struct kvm *kvm)
>  	return mode != KVM_IRQCHIP_NONE;
>  }
>  
> +bool kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args);
>  void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu);
>  void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu);
>  void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu);
> diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c
> index 001aeda4c154..3972a9564c76 100644
> --- a/virt/kvm/eventfd.c
> +++ b/virt/kvm/eventfd.c
> @@ -44,6 +44,12 @@
>  
>  static struct workqueue_struct *irqfd_cleanup_wq;
>  
> +bool __attribute__((weak))
> +kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args)
> +{
> +	return true;
> +}
> +
>  static void
>  irqfd_inject(struct work_struct *work)
>  {
> @@ -297,6 +303,9 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
>  	if (!kvm_arch_intc_initialized(kvm))
>  		return -EAGAIN;
>  
> +	if (!kvm_arch_irqfd_allowed(kvm, args))
> +		return -EINVAL;
> +
>  	irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL_ACCOUNT);
>  	if (!irqfd)
>  		return -ENOMEM;
> 

Queued, thanks.

Paolo

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-05-20 12:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-05  8:56 [PATCH] kvm: Check irqchip mode before assign irqfd Peter Xu
2019-05-05  9:20 ` Peter Xu
2019-05-17  2:23   ` Peter Xu
2019-05-20 12:54 ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).