Skip to content

Commit 70eae03

Browse files
committed
KVM: x86/xen: Fix SRCU/RCU usage in readers of evtchn_ports
The evtchnfd structure itself 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, and is called in SRCU critical section from the kvm_x86_handle_exit function. 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. Remove a superfluous if (kvm) check before calling synchronize_srcu() in kvm_xen_eventfd_deassign() where kvm has been dereferenced already. Co-developed-by: Michal Luczaj <[email protected]> Signed-off-by: Michal Luczaj <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> Signed-off-by: David Woodhouse <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 92c5896 commit 70eae03

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

arch/x86/kvm/xen.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,41 +1808,45 @@ static int kvm_xen_eventfd_update(struct kvm *kvm,
18081808
{
18091809
u32 port = data->u.evtchn.send_port;
18101810
struct evtchnfd *evtchnfd;
1811+
int ret;
18111812

18121813
if (!port || port >= max_evtchn_port(kvm))
18131814
return -EINVAL;
18141815

1816+
/* Protect writes to evtchnfd as well as the idr lookup. */
18151817
mutex_lock(&kvm->lock);
18161818
evtchnfd = idr_find(&kvm->arch.xen.evtchn_ports, port);
1817-
mutex_unlock(&kvm->lock);
18181819

1820+
ret = -ENOENT;
18191821
if (!evtchnfd)
1820-
return -ENOENT;
1822+
goto out_unlock;
18211823

18221824
/* For an UPDATE, nothing may change except the priority/vcpu */
1825+
ret = -EINVAL;
18231826
if (evtchnfd->type != data->u.evtchn.type)
1824-
return -EINVAL;
1827+
goto out_unlock;
18251828

18261829
/*
18271830
* Port cannot change, and if it's zero that was an eventfd
18281831
* which can't be changed either.
18291832
*/
18301833
if (!evtchnfd->deliver.port.port ||
18311834
evtchnfd->deliver.port.port != data->u.evtchn.deliver.port.port)
1832-
return -EINVAL;
1835+
goto out_unlock;
18331836

18341837
/* We only support 2 level event channels for now */
18351838
if (data->u.evtchn.deliver.port.priority != KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL)
1836-
return -EINVAL;
1839+
goto out_unlock;
18371840

1838-
mutex_lock(&kvm->lock);
18391841
evtchnfd->deliver.port.priority = data->u.evtchn.deliver.port.priority;
18401842
if (evtchnfd->deliver.port.vcpu_id != data->u.evtchn.deliver.port.vcpu) {
18411843
evtchnfd->deliver.port.vcpu_id = data->u.evtchn.deliver.port.vcpu;
18421844
evtchnfd->deliver.port.vcpu_idx = -1;
18431845
}
1846+
ret = 0;
1847+
out_unlock:
18441848
mutex_unlock(&kvm->lock);
1845-
return 0;
1849+
return ret;
18461850
}
18471851

18481852
/*
@@ -1935,8 +1939,7 @@ static int kvm_xen_eventfd_deassign(struct kvm *kvm, u32 port)
19351939
if (!evtchnfd)
19361940
return -ENOENT;
19371941

1938-
if (kvm)
1939-
synchronize_srcu(&kvm->srcu);
1942+
synchronize_srcu(&kvm->srcu);
19401943
if (!evtchnfd->deliver.port.port)
19411944
eventfd_ctx_put(evtchnfd->deliver.eventfd.ctx);
19421945
kfree(evtchnfd);
@@ -1989,14 +1992,18 @@ static bool kvm_xen_hcall_evtchn_send(struct kvm_vcpu *vcpu, u64 param, u64 *r)
19891992

19901993
/* Sanity check: this structure is the same for 32-bit and 64-bit */
19911994
BUILD_BUG_ON(sizeof(send) != 4);
1992-
19931995
if (kvm_read_guest_virt(vcpu, param, &send, sizeof(send), &e)) {
19941996
*r = -EFAULT;
19951997
return true;
19961998
}
19971999

1998-
/* The evtchn_ports idr is protected by vcpu->kvm->srcu */
2000+
/*
2001+
* evtchnfd is protected by kvm->srcu; the idr lookup instead
2002+
* is protected by RCU.
2003+
*/
2004+
rcu_read_lock();
19992005
evtchnfd = idr_find(&vcpu->kvm->arch.xen.evtchn_ports, send.port);
2006+
rcu_read_unlock();
20002007
if (!evtchnfd)
20012008
return false;
20022009

0 commit comments

Comments
 (0)