From patchwork Sat Dec 24 08:53:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 36399 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp73405wrt; Sat, 24 Dec 2022 00:56:19 -0800 (PST) X-Google-Smtp-Source: AMrXdXtCZViru5+sZPw3wT64GYALS4g2XwVmTUcs9bUXcJv1tpF50esd2XGPdrbSgafWWkXGl98D X-Received: by 2002:a05:6402:1f0b:b0:467:7b2e:88a2 with SMTP id b11-20020a0564021f0b00b004677b2e88a2mr10961508edb.19.1671872179348; Sat, 24 Dec 2022 00:56:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671872179; cv=none; d=google.com; s=arc-20160816; b=pruGOgDXNMzp5I0zKAbrxf6Wa8su178Yk2lvWuOXQLI0pvzYLhU64T/CsjQzjhD2YG QiLWXOVy4K0FCRqc6U57aOUTWCvWB2VE+1Kd/Ag7gdBrjTulcnDiA+Hc/s/TVzwrJYg5 mOkrt+pRDsZHfNLgK1Ch49Oq2Li36aJUcRc6DrldXbPEkcwHwWs2GMI7ppIJWUdTkI0h /TtlDGoP/hTa5f63yTKyHSZpGLMobm9Qtn99rkJJu+HvsgShjgXMJGfWiNksl4d72zUD jo2QLv16MJnBUEFa/twrOffMbSgl3W8Gc3PPQpBxkKoKrKpsJUfJz6hnI0KQrae/Vjxd xYKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=bE7Zv2G1RB6/xbT09U4GJ5lBkbria+fSg3I0JpKpvfY=; b=NA+JwHbWj9mec1iOhY//O+dblSTWLGA6ond5EFPCY0IYtrQFhESwQYyhbQUqTf+6gt sDr1MftLUQnX2VFaPXTKOu/KUX72c/iTAkSGEC+zEXaMUOlNSKrNHLivdR4w03RGPi7y WNDSgABOeIIyX6XQ34ZgXdrl8MWGqhaqeXlwPP+6hvELwpNNpItKG+tk97hPNYZHqo4q zAxhIDB/GqpxTBYBxvRLfVMYiNVTkenlJ+eXBykrUkDODv4IoM+u6Fr/MeHF7n/VpQ7o PbLXwUG888SpL1u3sF+1ZPmsC/wPKObWrAoB+mt/HIVVR29sit4ZuuryFMb9GGixWoKz 3j5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cZ8b7BG1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n7-20020a50cc47000000b0046fe2f565e0si4350431edi.315.2022.12.24.00.55.55; Sat, 24 Dec 2022 00:56:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cZ8b7BG1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230191AbiLXIx5 (ORCPT + 99 others); Sat, 24 Dec 2022 03:53:57 -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 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753085042437312967?= X-GMAIL-MSGID: =?utf-8?q?1753085042437312967?= 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 = 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)