KVM: x86/xen: Fix SRCU/RCU usage in readers of evtchn_ports

Message ID 20221224085304.1629042-1-pbonzini@redhat.com
State New
Headers
Series KVM: x86/xen: Fix SRCU/RCU usage in readers of evtchn_ports |

Commit Message

Paolo Bonzini Dec. 24, 2022, 8:53 a.m. UTC
  evtchnfd must be protected by either kvm->lock or SRCU.  Use
the former in kvm_xen_eventfd_update(), since the lock is being
taken anyway; kvm_xen_hcall_evtchn_send() instead is a reader
and does not need kvm->lock, so extend the SRCU critical section
there.

It is also important to use rcu_read_{lock,unlock}() in
kvm_xen_hcall_evtchn_send(), because idr_remove() will *not*
use synchronize_srcu() to wait for readers to complete.

Co-developed-by: Michal Luczaj <mhal@rbox.co>
Signed-off-by: Michal Luczaj <mhal@rbox.co>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/xen.c | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)
  

Comments

David Woodhouse Dec. 24, 2022, 10:54 a.m. UTC | #1
On Sat, 2022-12-24 at 03:53 -0500, Paolo Bonzini wrote:
> @@ -2005,19 +2009,23 @@ static bool kvm_xen_hcall_evtchn_send(struct
> kvm_vcpu *vcpu, u64 param, u64 *r)
>         gpa_t gpa;
>         int idx;
>  
> +       /*
> +        * evtchnfd is protected by kvm->srcu; the idr lookup instead
> +        * is protected by RCU.
> +        */
>         idx = srcu_read_lock(&vcpu->kvm->srcu);
>         gpa = kvm_mmu_gva_to_gpa_system(vcpu, param, NULL);
> -       srcu_read_unlock(&vcpu->kvm->srcu, idx);
>  

I removed that srcu_read_lock() in
https://lore.kernel.org/kvm/d52040a8d46e68efd86273be66808fe4a8c70e1d.camel@infradead.org/
on the basis that this is a hypercall handler, called from the
handle_exit function, with the lock already taken.
  
David Woodhouse Dec. 26, 2022, 11:21 a.m. UTC | #2
On Sat, 2022-12-24 at 03:53 -0500, Paolo Bonzini wrote:
> @@ -2005,19 +2009,23 @@ static bool kvm_xen_hcall_evtchn_send(struct kvm_vcpu *vcpu, u64 param, u64 *r)
>         gpa_t gpa;
>         int idx;
>  
> +       /*
> +        * evtchnfd is protected by kvm->srcu; the idr lookup instead
> +        * is protected by RCU.
> +        */
>         idx = srcu_read_lock(&vcpu->kvm->srcu);
>         gpa = kvm_mmu_gva_to_gpa_system(vcpu, param, NULL);
> -       srcu_read_unlock(&vcpu->kvm->srcu, idx);
>  
>         if (!gpa || kvm_vcpu_read_guest(vcpu, gpa, &send, sizeof(send))) {
>                 *r = -EFAULT;
> -               return true;
> +               goto out_handled;
>         }
>  
> -       /* The evtchn_ports idr is protected by vcpu->kvm->srcu */
> +       rcu_read_lock();
>         evtchnfd = idr_find(&vcpu->kvm->arch.xen.evtchn_ports, send.port);
> +       rcu_read_unlock();
>         if (!evtchnfd)
> -               return false;
> +               goto out_not_handled;
>  
>         if (evtchnfd->deliver.port.port) {
>                 int ret = kvm_xen_set_evtchn(&evtchnfd->deliver.port, vcpu->kvm);


You left a 'return false' in the failure path of this
kvm_xen_set_evtchn() call instead of changing it to 'goto
out_not_handled'.

So rather than adding my kvm_read_guest_virt() patch on top and
removing all the gotos, I'm going to put my patch first and put a
simpler version of your patch on top.
  

Patch

diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c
index d7af40240248..935f845d005c 100644
--- a/arch/x86/kvm/xen.c
+++ b/arch/x86/kvm/xen.c
@@ -1825,20 +1825,23 @@  static int kvm_xen_eventfd_update(struct kvm *kvm,
 {
 	u32 port = data->u.evtchn.send_port;
 	struct evtchnfd *evtchnfd;
+	int ret;
 
 	if (!port || port >= max_evtchn_port(kvm))
 		return -EINVAL;
 
+	/* Protect writes to evtchnfd as well as the idr lookup.  */
 	mutex_lock(&kvm->lock);
 	evtchnfd = idr_find(&kvm->arch.xen.evtchn_ports, port);
-	mutex_unlock(&kvm->lock);
 
+	ret = -ENOENT;
 	if (!evtchnfd)
-		return -ENOENT;
+		goto out_unlock;
 
 	/* For an UPDATE, nothing may change except the priority/vcpu */
+	ret = -EINVAL;
 	if (evtchnfd->type != data->u.evtchn.type)
-		return -EINVAL;
+		goto out_unlock;
 
 	/*
 	 * Port cannot change, and if it's zero that was an eventfd
@@ -1846,20 +1849,21 @@  static int kvm_xen_eventfd_update(struct kvm *kvm,
 	 */
 	if (!evtchnfd->deliver.port.port ||
 	    evtchnfd->deliver.port.port != data->u.evtchn.deliver.port.port)
-		return -EINVAL;
+		goto out_unlock;
 
 	/* We only support 2 level event channels for now */
 	if (data->u.evtchn.deliver.port.priority != KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL)
-		return -EINVAL;
+		goto out_unlock;
 
-	mutex_lock(&kvm->lock);
 	evtchnfd->deliver.port.priority = data->u.evtchn.deliver.port.priority;
 	if (evtchnfd->deliver.port.vcpu_id != data->u.evtchn.deliver.port.vcpu) {
 		evtchnfd->deliver.port.vcpu_id = data->u.evtchn.deliver.port.vcpu;
 		evtchnfd->deliver.port.vcpu_idx = -1;
 	}
+	ret = 0;
+out_unlock:
 	mutex_unlock(&kvm->lock);
-	return 0;
+	return ret;
 }
 
 /*
@@ -2005,19 +2009,23 @@  static bool kvm_xen_hcall_evtchn_send(struct kvm_vcpu *vcpu, u64 param, u64 *r)
 	gpa_t gpa;
 	int idx;
 
+	/*
+	 * evtchnfd is protected by kvm->srcu; the idr lookup instead
+	 * is protected by RCU.
+	 */
 	idx = srcu_read_lock(&vcpu->kvm->srcu);
 	gpa = kvm_mmu_gva_to_gpa_system(vcpu, param, NULL);
-	srcu_read_unlock(&vcpu->kvm->srcu, idx);
 
 	if (!gpa || kvm_vcpu_read_guest(vcpu, gpa, &send, sizeof(send))) {
 		*r = -EFAULT;
-		return true;
+		goto out_handled;
 	}
 
-	/* The evtchn_ports idr is protected by vcpu->kvm->srcu */
+	rcu_read_lock();
 	evtchnfd = idr_find(&vcpu->kvm->arch.xen.evtchn_ports, send.port);
+	rcu_read_unlock();
 	if (!evtchnfd)
-		return false;
+		goto out_not_handled;
 
 	if (evtchnfd->deliver.port.port) {
 		int ret = kvm_xen_set_evtchn(&evtchnfd->deliver.port, vcpu->kvm);
@@ -2028,7 +2036,13 @@  static bool kvm_xen_hcall_evtchn_send(struct kvm_vcpu *vcpu, u64 param, u64 *r)
 	}
 
 	*r = 0;
+out_handled:
+	srcu_read_unlock(&vcpu->kvm->srcu, idx);
 	return true;
+
+out_not_handled:
+	srcu_read_unlock(&vcpu->kvm->srcu, idx);
+	return false;
 }
 
 void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu)