From nobody Wed Sep 17 01:27:10 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7BF49C4167B for ; Sat, 24 Dec 2022 08:53:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230212AbiLXIx6 (ORCPT ); Sat, 24 Dec 2022 03:53:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229445AbiLXIxz (ORCPT ); Sat, 24 Dec 2022 03:53:55 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 392ABB1DA for ; Sat, 24 Dec 2022 00:53:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671871990; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bE7Zv2G1RB6/xbT09U4GJ5lBkbria+fSg3I0JpKpvfY=; b=cZ8b7BG10rJD0SAxaISFb6syBwP0GS3WN+Phb9Oqj0PvFvWPbZsSUWhRlsDoGKGZHvYGOk mMU/6OZ2VwyMaE74Yhd0QWRmZ582Uh3ZkP7tY1w/3bq5VGn2n6ZB6C6aQdTStbICK+8T6W mFSPRV7+oHt1ZgBQ2YRDAJKWZGluv5o= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-556-IhNY73X2NEqzIjJDEiA0Iw-1; Sat, 24 Dec 2022 03:53:05 -0500 X-MC-Unique: IhNY73X2NEqzIjJDEiA0Iw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 64B9229AA2C1; Sat, 24 Dec 2022 08:53:05 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id C990340C2064; Sat, 24 Dec 2022 08:53:04 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: paul@xen.org, seanjc@google.com, dwmw2@infradead.org, Michal Luczaj Subject: [PATCH] KVM: x86/xen: Fix SRCU/RCU usage in readers of evtchn_ports Date: Sat, 24 Dec 2022 03:53:04 -0500 Message-Id: <20221224085304.1629042-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 Signed-off-by: Michal Luczaj Signed-off-by: Paolo Bonzini --- arch/x86/kvm/xen.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) 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 =3D data->u.evtchn.send_port; struct evtchnfd *evtchnfd; + int ret; =20 if (!port || port >=3D max_evtchn_port(kvm)) return -EINVAL; =20 + /* Protect writes to evtchnfd as well as the idr lookup. */ mutex_lock(&kvm->lock); evtchnfd =3D idr_find(&kvm->arch.xen.evtchn_ports, port); - mutex_unlock(&kvm->lock); =20 + ret =3D -ENOENT; if (!evtchnfd) - return -ENOENT; + goto out_unlock; =20 /* For an UPDATE, nothing may change except the priority/vcpu */ + ret =3D -EINVAL; if (evtchnfd->type !=3D data->u.evtchn.type) - return -EINVAL; + goto out_unlock; =20 /* * 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 !=3D data->u.evtchn.deliver.port.port) - return -EINVAL; + goto out_unlock; =20 /* We only support 2 level event channels for now */ if (data->u.evtchn.deliver.port.priority !=3D KVM_IRQ_ROUTING_XEN_EVTCHN_= PRIO_2LEVEL) - return -EINVAL; + goto out_unlock; =20 - mutex_lock(&kvm->lock); evtchnfd->deliver.port.priority =3D data->u.evtchn.deliver.port.priority; if (evtchnfd->deliver.port.vcpu_id !=3D data->u.evtchn.deliver.port.vcpu)= { evtchnfd->deliver.port.vcpu_id =3D data->u.evtchn.deliver.port.vcpu; evtchnfd->deliver.port.vcpu_idx =3D -1; } + ret =3D 0; +out_unlock: mutex_unlock(&kvm->lock); - return 0; + return ret; } =20 /* @@ -2005,19 +2009,23 @@ static bool kvm_xen_hcall_evtchn_send(struct kvm_vc= pu *vcpu, u64 param, u64 *r) gpa_t gpa; int idx; =20 + /* + * evtchnfd is protected by kvm->srcu; the idr lookup instead + * is protected by RCU. + */ idx =3D srcu_read_lock(&vcpu->kvm->srcu); gpa =3D kvm_mmu_gva_to_gpa_system(vcpu, param, NULL); - srcu_read_unlock(&vcpu->kvm->srcu, idx); =20 if (!gpa || kvm_vcpu_read_guest(vcpu, gpa, &send, sizeof(send))) { *r =3D -EFAULT; - return true; + goto out_handled; } =20 - /* The evtchn_ports idr is protected by vcpu->kvm->srcu */ + rcu_read_lock(); evtchnfd =3D idr_find(&vcpu->kvm->arch.xen.evtchn_ports, send.port); + rcu_read_unlock(); if (!evtchnfd) - return false; + goto out_not_handled; =20 if (evtchnfd->deliver.port.port) { int ret =3D kvm_xen_set_evtchn(&evtchnfd->deliver.port, vcpu->kvm); @@ -2028,7 +2036,13 @@ static bool kvm_xen_hcall_evtchn_send(struct kvm_vcp= u *vcpu, u64 param, u64 *r) } =20 *r =3D 0; +out_handled: + srcu_read_unlock(&vcpu->kvm->srcu, idx); return true; + +out_not_handled: + srcu_read_unlock(&vcpu->kvm->srcu, idx); + return false; } =20 void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu) --=20 2.31.1