Message ID | 20221228110410.1682852-1-pbonzini@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp1836452wrt; Wed, 28 Dec 2022 03:12:35 -0800 (PST) X-Google-Smtp-Source: AMrXdXtZVldZt85PwNyjP3m0bhqbbVUjnYFFbQ8NzcH/VA/2kBlb0ipCux9LWmcecqOvPrXUpDR2 X-Received: by 2002:a05:6a21:998f:b0:aa:6efd:1885 with SMTP id ve15-20020a056a21998f00b000aa6efd1885mr38758700pzb.1.1672225955613; Wed, 28 Dec 2022 03:12:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672225955; cv=none; d=google.com; s=arc-20160816; b=lH/qrpeD81bp2uZ/tXTCSPcxVcR2Bq5OmmfplddlDrRjsb2wZLFxIG13xZToZ8YoWS UC9yiJ3/yg7paAsjvPB16tQOiIOJNaPUZXQoFvzp/L30xv/znURvOH23db4qMB6tdIzN COzxLSKTKvN4qFPElnL7yDrW9pVVwMG4VOO4g3skLlFJRgfkWHfjeHb+lL7zh79idk3w FtiFmrdGDCPx0ow2oPi/ZMTnahnYaqqLEKe+dJ1cjJLKFUxBTDBoQDtripwryI/YqhcD OrEwnTE2N49sNaIdnn2hUSdKLjJej2o9nEjjfuFA97yS6hdcciicOSBIAEf88TCU35ob NxuQ== 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=1pjwclVxU00ME1HQWci/rX5qpnSJvDd+pioG/KTQXLw=; b=aYfeAqDoChzeZfZrd9FUS/bLOZJoCmsKsWJFFz6EP2vFHMW46yd9DSckea5BPN/K3N BwRxUrmF2BsfJg07aU77QChRCajDxfo1mFrd3jW7kKe2Z2fbPBEUn+YMlW602K3WqHyZ 0vqRo3h0NFyz8xT+IJOiRThtwjEm4KWfZZ4KZxVeWBhXmuRU4/hliwe9bxMxfR9/O9uo 0gth/DJEniHX95AeHS8SIAglGyyi12zZIrXGSQ7Jd4bbU6/luPZ661ukZUAzrDNAQtif nHDkav4fGGcuEDbiWo4dmZPzog907knppNRrAqPaaOPOJPtkPMzQQyZY416hXJMYDBXs rdgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=I0SLZ6gm; 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 z20-20020a63e554000000b0047905100b71si16230227pgj.675.2022.12.28.03.12.23; Wed, 28 Dec 2022 03:12:35 -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=I0SLZ6gm; 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 S230311AbiL1LFO (ORCPT <rfc822;eddaouddi.ayoub@gmail.com> + 99 others); Wed, 28 Dec 2022 06:05:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230083AbiL1LFF (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 28 Dec 2022 06:05:05 -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 19D5B6456 for <linux-kernel@vger.kernel.org>; Wed, 28 Dec 2022 03:04:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672225456; 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=1pjwclVxU00ME1HQWci/rX5qpnSJvDd+pioG/KTQXLw=; b=I0SLZ6gmqLQT1krNGbJzmyoEE3NKLHOObCTPbIrnIXCDUNHEZJdiOnpdh7jhVz4fI87vjo OcO/sNuLWkHAhU5nYMJ0FFtrGydFPOEFomhlD33Bp3HzFPCPBOAK3Taf2VoJPN6emWtcpU 9h0L6Ng+BV372ZPrp4woITK/96i6Ij8= 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-282-n5pRSaJPNyWZ_I0WqJTFOQ-1; Wed, 28 Dec 2022 06:04:11 -0500 X-MC-Unique: n5pRSaJPNyWZ_I0WqJTFOQ-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 8FDCB3814946; Wed, 28 Dec 2022 11:04:11 +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 26F5840AE1E9; Wed, 28 Dec 2022 11:04:11 +0000 (UTC) From: Paolo Bonzini <pbonzini@redhat.com> To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, Michal Luczaj <mhal@rbox.co>, David Woodhouse <dwmw@amazon.co.uk> Subject: [PATCH] KVM: x86: fix deadlock for KVM_XEN_EVTCHN_RESET Date: Wed, 28 Dec 2022 06:04:09 -0500 Message-Id: <20221228110410.1682852-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753456003556842654?= X-GMAIL-MSGID: =?utf-8?q?1753456003556842654?= |
Series |
KVM: x86: fix deadlock for KVM_XEN_EVTCHN_RESET
|
|
Commit Message
Paolo Bonzini
Dec. 28, 2022, 11:04 a.m. UTC
While KVM_XEN_EVTCHN_RESET is usually called with no vCPUs running,
if that happened it could cause a deadlock. This is due to
kvm_xen_eventfd_reset() doing a synchronize_srcu() inside
a kvm->lock critical section.
To avoid this, first collect all the evtchnfd objects in an
array and free all of them once the kvm->lock critical section
is over and th SRCU grace period has expired.
Reported-by: Michal Luczaj <mhal@rbox.co>
Cc: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
arch/x86/kvm/xen.c | 30 +++++++++++++++++--
.../selftests/kvm/x86_64/xen_shinfo_test.c | 6 ++++
2 files changed, 33 insertions(+), 3 deletions(-)
Comments
Hi Paolo, > diff --git a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c > index 721f6a693799..dae510c263b4 100644 > --- a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c > +++ b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c > @@ -962,6 +962,12 @@ int main(int argc, char *argv[]) > } > > done: > + struct kvm_xen_hvm_attr evt_reset = { > + .type = KVM_XEN_ATTR_TYPE_EVTCHN, > + .u.evtchn.flags = KVM_XEN_EVTCHN_RESET, > + }; > + vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &evt_reset); > + > alarm(0); > clock_gettime(CLOCK_REALTIME, &max_ts); > This change generates a build failure with error message: "error: a label can only be part of a statement and a declaration is not a statement". Moving the definition of evt_reset to the beginning of main() can fix it: diff --git a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c index dae510c263b4..d71f1508bb21 100644 --- a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c +++ b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c @@ -623,6 +623,10 @@ int main(int argc, char *argv[]) rs->state = 0x5a; bool evtchn_irq_expected = false; + struct kvm_xen_hvm_attr evt_reset = { + .type = KVM_XEN_ATTR_TYPE_EVTCHN, + .u.evtchn.flags = KVM_XEN_EVTCHN_RESET, + }; for (;;) { volatile struct kvm_run *run = vcpu->run; @@ -962,10 +966,6 @@ int main(int argc, char *argv[]) } done: - struct kvm_xen_hvm_attr evt_reset = { - .type = KVM_XEN_ATTR_TYPE_EVTCHN, - .u.evtchn.flags = KVM_XEN_EVTCHN_RESET, - }; vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &evt_reset); alarm(0); B.R. Yu
On Thu, Jan 19, 2023, Yu Zhang wrote: > Hi Paolo, > > > diff --git a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c > > index 721f6a693799..dae510c263b4 100644 > > --- a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c > > +++ b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c > > @@ -962,6 +962,12 @@ int main(int argc, char *argv[]) > > } > > > > done: > > + struct kvm_xen_hvm_attr evt_reset = { > > + .type = KVM_XEN_ATTR_TYPE_EVTCHN, > > + .u.evtchn.flags = KVM_XEN_EVTCHN_RESET, > > + }; > > + vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &evt_reset); > > + > > alarm(0); > > clock_gettime(CLOCK_REALTIME, &max_ts); > > > > This change generates a build failure with error message: > "error: a label can only be part of a statement and a declaration is not a statement". And other flavors too, e.g. x86_64/xen_shinfo_test.c:965:2: error: expected expression struct kvm_xen_hvm_attr evt_reset = { ^ x86_64/xen_shinfo_test.c:969:38: error: use of undeclared identifier 'evt_reset' vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &evt_reset); ^ x86_64/xen_shinfo_test.c:969:38: error: use of undeclared identifier 'evt_reset' 3 errors generated. make: *** [../lib.mk:145: tools/testing/selftests/kvm/x86_64/xen_shinfo_test] Error 1 make: *** Waiting for unfinished jobs.... I'm surprised bots haven't complained about this, haven't seen any reports. > Moving the definition of evt_reset to the beginning of main() can fix it: I'll queue a patch, this is already in Linus' tree and I've collected a few other tiny fixes for v6.2-rcwhatever that I'll send to Paolo. Thanks!
On Thu, Jan 19, 2023 at 6:57 PM Sean Christopherson <seanjc@google.com> wrote: > > This change generates a build failure with error message: > > "error: a label can only be part of a statement and a declaration is not a statement". > > And other flavors too, e.g. > > x86_64/xen_shinfo_test.c:965:2: error: expected expression > struct kvm_xen_hvm_attr evt_reset = { > ^ > x86_64/xen_shinfo_test.c:969:38: error: use of undeclared identifier 'evt_reset' > vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &evt_reset); > ^ > x86_64/xen_shinfo_test.c:969:38: error: use of undeclared identifier 'evt_reset' > 3 errors generated. > make: *** [../lib.mk:145: tools/testing/selftests/kvm/x86_64/xen_shinfo_test] Error 1 > make: *** Waiting for unfinished jobs.... > > I'm surprised bots haven't complained about this, haven't seen any reports. It's clang only; GCC only warns with -Wpedantic. Plus, bots probably don't compile tools/ that much. Paolo
On Thu, Jan 19, 2023, Paolo Bonzini wrote: > On Thu, Jan 19, 2023 at 6:57 PM Sean Christopherson <seanjc@google.com> wrote: > > > This change generates a build failure with error message: > > > "error: a label can only be part of a statement and a declaration is not a statement". > > > > And other flavors too, e.g. > > > > x86_64/xen_shinfo_test.c:965:2: error: expected expression > > struct kvm_xen_hvm_attr evt_reset = { > > ^ > > x86_64/xen_shinfo_test.c:969:38: error: use of undeclared identifier 'evt_reset' > > vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &evt_reset); > > ^ > > x86_64/xen_shinfo_test.c:969:38: error: use of undeclared identifier 'evt_reset' > > 3 errors generated. > > make: *** [../lib.mk:145: tools/testing/selftests/kvm/x86_64/xen_shinfo_test] Error 1 > > make: *** Waiting for unfinished jobs.... > > > > I'm surprised bots haven't complained about this, haven't seen any reports. > > It's clang only; GCC only warns with -Wpedantic. Plus, bots probably > don't compile tools/ that much. /wave Want to queue Yu's fix directly Paolo? I was assuming you'd be offline until sometime tomorrow.
On Thu, Jan 19, 2023 at 7:04 PM Sean Christopherson <seanjc@google.com> wrote: > > It's clang only; GCC only warns with -Wpedantic. Plus, bots probably > > don't compile tools/ that much. > > /wave > > Want to queue Yu's fix directly Paolo? I was assuming you'd be offline until > sometime tomorrow. Yes, I can, but what other patches were you meaning to send? Paolo
On Thu, Jan 19, 2023, Paolo Bonzini wrote: > On Thu, Jan 19, 2023 at 7:04 PM Sean Christopherson <seanjc@google.com> wrote: > > > It's clang only; GCC only warns with -Wpedantic. Plus, bots probably > > > don't compile tools/ that much. > > > > /wave > > > > Want to queue Yu's fix directly Paolo? I was assuming you'd be offline until > > sometime tomorrow. > > Yes, I can, but what other patches were you meaning to send? A minor selftest fix https://lore.kernel.org/all/20230111183408.104491-1-vipinsh@google.com and a fix for a longstanding VMX bug that seems problematic enough that it warrants going into this cycle. https://lore.kernel.org/all/20221114164823.69555-1-hborghor@amazon.de
On Thu, Jan 19, 2023 at 7:15 PM Sean Christopherson <seanjc@google.com> wrote: > A minor selftest fix > > https://lore.kernel.org/all/20230111183408.104491-1-vipinsh@google.com > > and a fix for a longstanding VMX bug that seems problematic enough that it > warrants going into this cycle. > > https://lore.kernel.org/all/20221114164823.69555-1-hborghor@amazon.de Ok, I had seen the latter so I'll put together a pull request. Paolo
On 28/12/2022 12:04, Paolo Bonzini wrote: > While KVM_XEN_EVTCHN_RESET is usually called with no vCPUs running, > if that happened it could cause a deadlock. This is due to > kvm_xen_eventfd_reset() doing a synchronize_srcu() inside > a kvm->lock critical section. > > [...] > > + /* > + * Because synchronize_srcu() cannot be called inside the > + * critical section, first collect all the evtchnfd objects > + * in an array as they are removed from evtchn_ports. > + */ With the recent changes regarding the locking order (locking.rst: "synchronize_srcu(&kvm->srcu) is called inside critical sections for kvm->lock, vcpu->mutex and kvm->slots_lock"), is this comment still valid? Or is there a rule that forbids synchronize_srcu() under the newly introduced kvm->arch.xen.xen_lock? thanks, Michal
diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index b178f40bd863..2e29bdc2949c 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -1942,18 +1942,42 @@ static int kvm_xen_eventfd_deassign(struct kvm *kvm, u32 port) static int kvm_xen_eventfd_reset(struct kvm *kvm) { - struct evtchnfd *evtchnfd; + struct evtchnfd *evtchnfd, **all_evtchnfds; int i; + int n = 0; mutex_lock(&kvm->lock); + + /* + * Because synchronize_srcu() cannot be called inside the + * critical section, first collect all the evtchnfd objects + * in an array as they are removed from evtchn_ports. + */ + idr_for_each_entry(&kvm->arch.xen.evtchn_ports, evtchnfd, i) + n++; + + all_evtchnfds = kmalloc_array(n, sizeof(struct evtchnfd *), GFP_KERNEL); + if (!all_evtchnfds) { + mutex_unlock(&kvm->lock); + return -ENOMEM; + } + + n = 0; idr_for_each_entry(&kvm->arch.xen.evtchn_ports, evtchnfd, i) { + all_evtchnfds[n++] = evtchnfd; idr_remove(&kvm->arch.xen.evtchn_ports, evtchnfd->send_port); - synchronize_srcu(&kvm->srcu); + } + mutex_unlock(&kvm->lock); + + synchronize_srcu(&kvm->srcu); + + while (n--) { + evtchnfd = all_evtchnfds[n]; if (!evtchnfd->deliver.port.port) eventfd_ctx_put(evtchnfd->deliver.eventfd.ctx); kfree(evtchnfd); } - mutex_unlock(&kvm->lock); + kfree(all_evtchnfds); return 0; } diff --git a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c index 721f6a693799..dae510c263b4 100644 --- a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c +++ b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c @@ -962,6 +962,12 @@ int main(int argc, char *argv[]) } done: + struct kvm_xen_hvm_attr evt_reset = { + .type = KVM_XEN_ATTR_TYPE_EVTCHN, + .u.evtchn.flags = KVM_XEN_EVTCHN_RESET, + }; + vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &evt_reset); + alarm(0); clock_gettime(CLOCK_REALTIME, &max_ts);