Message ID | 20221209015307.1781352-8-oliver.upton@linux.dev |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp529958wrr; Thu, 8 Dec 2022 17:58:28 -0800 (PST) X-Google-Smtp-Source: AA0mqf7eDp1l/OAkBb0263jxc/qpI/KzUgfQq9UHFzR6v1MwnetK5vj9H61ZLheO5R/c+VD0IoBP X-Received: by 2002:a17:907:3e14:b0:7c0:f719:838d with SMTP id hp20-20020a1709073e1400b007c0f719838dmr4573645ejc.36.1670551108753; Thu, 08 Dec 2022 17:58:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670551108; cv=none; d=google.com; s=arc-20160816; b=jVDZVRBGPTYa2iLDMByjAhMsQv78hK8ZTP7QCw1LN439K1zuvwYtsTqJOUvZnkoH8n vI9iAEi6GaiY5m9VPoa2snEJy5ZkbabZTya6rX19LVGx/2/WpIAwpikm0OQLieNXj13P k/79TvE1hwc/TNA+iyNDQ6ia/2vBpSsNDo0Ej+G2RMpmeiEW4HJUX8O5wRzBpulSESZ2 9P42AH7E9OY01CPBifUK4zVIcMmjX3tpR7Z/jbKMvTuwOKWzjjrbPQERgncDqNL6B1Ye iyZPv5Gvx+yTDz3D3QO8pyDt9xaqlPNNQVQml8xRNjnMc9YBwa7I4ZBMaNjoftgXRnah biVg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=iGqzh1nn8M8qIu2HMX7NJCgwvQUB1383Z9Wplc2q4DE=; b=tKAveaWpebKHbCoa0WXrsjzKDBM2gcpE4xEitsMVA3dDa2oHz/gdnqHWIsvAk+uBaq I8Tdur7NKksekpmmZ8m3vfkS34rRCFl20OKtyUcp+yvQwXmU6Tul4ippVuAbGyH9/Jby ptYdJS7JAKD7wgnmcbBMyv5vLvN06X7CJkH0vDXIoiF/dHJSiZTec5HrDD/fjl9h3VU6 ugLR56IVr1X8IYtfUvnizSbQupG9ju6/LpkxxMbutb/bgmuRwuUe1Jbwch/5knq6dCUa O5Gn4bEYD8pJzJcsvKX+KLbBgNXSRbFZJlbG0JwWWQ2Gs3kefaSecVT5vEXoZwy8qZcn l5eQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hx9-20020a170906846900b007c10a0c590dsi72518ejc.25.2022.12.08.17.58.05; Thu, 08 Dec 2022 17:58:28 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230048AbiLIByk (ORCPT <rfc822;foxyelen666@gmail.com> + 99 others); Thu, 8 Dec 2022 20:54:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230129AbiLIByG (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 8 Dec 2022 20:54:06 -0500 Received: from out-74.mta0.migadu.com (out-74.mta0.migadu.com [91.218.175.74]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE657B1066 for <linux-kernel@vger.kernel.org>; Thu, 8 Dec 2022 17:53:48 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton <oliver.upton@linux.dev> To: Marc Zyngier <maz@kernel.org>, James Morse <james.morse@arm.com>, Alexandru Elisei <alexandru.elisei@arm.com>, Paolo Bonzini <pbonzini@redhat.com>, Shuah Khan <shuah@kernel.org>, Andrew Jones <andrew.jones@linux.dev>, Peter Gonda <pgonda@google.com>, Sean Christopherson <seanjc@google.com> Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, kvmarm@lists.linux.dev, Ricardo Koller <ricarkol@google.com>, Oliver Upton <oliver.upton@linux.dev>, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 7/7] KVM: selftests: Avoid infinite loop if ucall_alloc() fails Date: Fri, 9 Dec 2022 01:53:06 +0000 Message-Id: <20221209015307.1781352-8-oliver.upton@linux.dev> In-Reply-To: <20221209015307.1781352-1-oliver.upton@linux.dev> References: <20221209015307.1781352-1-oliver.upton@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS 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?1751699799672183925?= X-GMAIL-MSGID: =?utf-8?q?1751699799672183925?= |
Series |
[v2,1/7] KVM: selftests: Fix build due to ucall_uninit() removal
|
|
Commit Message
Oliver Upton
Dec. 9, 2022, 1:53 a.m. UTC
Guest assertions depend on successfully allocating a ucall structure. As
such, the use of guest assertions when ucall_alloc() fails simply leads
to an infinite loop in guest code.
Use GUEST_UCALL_NONE() to indicate failure instead. Though not
technically necessary, use a goto to have a single callsite and an
associated comment about why assertions don't work here. It isn't
perfect, at least the poor developer gets some signal out of the
guest...
Fixes: 426729b2cf2e ("KVM: selftests: Add ucall pool based implementation")
Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
---
tools/testing/selftests/kvm/lib/ucall_common.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
Comments
On Fri, Dec 09, 2022, Oliver Upton wrote: > Guest assertions depend on successfully allocating a ucall structure. As > such, the use of guest assertions when ucall_alloc() fails simply leads > to an infinite loop in guest code. > > Use GUEST_UCALL_NONE() to indicate failure instead. Though not > technically necessary, use a goto to have a single callsite and an > associated comment about why assertions don't work here. It isn't > perfect, at least the poor developer gets some signal out of the > guest... > > Fixes: 426729b2cf2e ("KVM: selftests: Add ucall pool based implementation") > Signed-off-by: Oliver Upton <oliver.upton@linux.dev> > --- > tools/testing/selftests/kvm/lib/ucall_common.c | 12 ++++++++++-- > 1 file changed, 10 insertions(+), 2 deletions(-) > > diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c > index 0cc0971ce60e..e8370da3de24 100644 > --- a/tools/testing/selftests/kvm/lib/ucall_common.c > +++ b/tools/testing/selftests/kvm/lib/ucall_common.c > @@ -41,7 +41,8 @@ static struct ucall *ucall_alloc(void) > struct ucall *uc; > int i; > > - GUEST_ASSERT(ucall_pool); > + if (!ucall_pool) > + goto out; > > for (i = 0; i < KVM_MAX_VCPUS; ++i) { > if (!test_and_set_bit(i, ucall_pool->in_use)) { > @@ -51,7 +52,14 @@ static struct ucall *ucall_alloc(void) > } > } > > - GUEST_ASSERT(0); > +out: > + /* > + * If the guest cannot grab a ucall structure from the pool then the > + * only option to get out to userspace is a bare ucall. This is probably > + * a good time to mention that guest assertions depend on ucalls with > + * arguments too. > + */ > + GUEST_UCALL_NONE(); UCALL_NONE isn't much better than infinite stack recursion, e.g. a test might end up passing by dumb luck, or go in the wrong direction because it sometimes handles UCALL_NONE. How about this? From: Sean Christopherson <seanjc@google.com> Date: Fri, 9 Dec 2022 12:55:44 -0800 Subject: [PATCH] KVM: selftests: Use magic value to signal ucall_alloc() failure Use a magic value to signal a ucall_alloc() failure instead of simply doing GUEST_ASSERT(). GUEST_ASSERT() relies on ucall_alloc() and so a failure puts the guest into an infinite loop. Use -1 as the magic value, as a real ucall struct should never wrap. Reported-by: Oliver Upton <oliver.upton@linux.dev> Signed-off-by: Sean Christopherson <seanjc@google.com> --- tools/testing/selftests/kvm/lib/ucall_common.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c index 0cc0971ce60e..2f0e2ea941cc 100644 --- a/tools/testing/selftests/kvm/lib/ucall_common.c +++ b/tools/testing/selftests/kvm/lib/ucall_common.c @@ -4,6 +4,8 @@ #include "linux/bitmap.h" #include "linux/atomic.h" +#define GUEST_UCALL_FAILED -1 + struct ucall_header { DECLARE_BITMAP(in_use, KVM_MAX_VCPUS); struct ucall ucalls[KVM_MAX_VCPUS]; @@ -41,7 +43,8 @@ static struct ucall *ucall_alloc(void) struct ucall *uc; int i; - GUEST_ASSERT(ucall_pool); + if (!ucall_pool) + goto ucall_failed; for (i = 0; i < KVM_MAX_VCPUS; ++i) { if (!test_and_set_bit(i, ucall_pool->in_use)) { @@ -51,7 +54,13 @@ static struct ucall *ucall_alloc(void) } } - GUEST_ASSERT(0); +ucall_failed: + /* + * If the vCPU cannot grab a ucall structure, make a bare ucall with a + * magic value to signal to get_ucall() that things went sideways. + * GUEST_ASSERT() depends on ucall_alloc() and so cannot be used here. + */ + ucall_arch_do_ucall(GUEST_UCALL_FAILED); return NULL; } @@ -93,6 +102,9 @@ uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) addr = ucall_arch_get_ucall(vcpu); if (addr) { + TEST_ASSERT(addr != (void *)GUEST_UCALL_FAILED, + "Guest failed to allocate ucall struct"); + memcpy(uc, addr, sizeof(*uc)); vcpu_run_complete_io(vcpu); } else { base-commit: dc2efbe4813e0dc4368779bc36c5f0e636cb8eb2 --
On Fri, Dec 09, 2022 at 09:03:45PM +0000, Sean Christopherson wrote: [...] > > - GUEST_ASSERT(0); > > +out: > > + /* > > + * If the guest cannot grab a ucall structure from the pool then the > > + * only option to get out to userspace is a bare ucall. This is probably > > + * a good time to mention that guest assertions depend on ucalls with > > + * arguments too. > > + */ > > + GUEST_UCALL_NONE(); > > UCALL_NONE isn't much better than infinite stack recursion, e.g. a test might end > up passing by dumb luck, or go in the wrong direction because it sometimes handles > UCALL_NONE. Oh, I was just seeking an end to my misery. Yeah, we can use a magic value to signal this instead. > How about this? LGTM. -- Thanks, Oliver > From: Sean Christopherson <seanjc@google.com> > Date: Fri, 9 Dec 2022 12:55:44 -0800 > Subject: [PATCH] KVM: selftests: Use magic value to signal ucall_alloc() > failure > > Use a magic value to signal a ucall_alloc() failure instead of simply > doing GUEST_ASSERT(). GUEST_ASSERT() relies on ucall_alloc() and so a > failure puts the guest into an infinite loop. > > Use -1 as the magic value, as a real ucall struct should never wrap. > > Reported-by: Oliver Upton <oliver.upton@linux.dev> > Signed-off-by: Sean Christopherson <seanjc@google.com> > --- > tools/testing/selftests/kvm/lib/ucall_common.c | 16 ++++++++++++++-- > 1 file changed, 14 insertions(+), 2 deletions(-) > > diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c > index 0cc0971ce60e..2f0e2ea941cc 100644 > --- a/tools/testing/selftests/kvm/lib/ucall_common.c > +++ b/tools/testing/selftests/kvm/lib/ucall_common.c > @@ -4,6 +4,8 @@ > #include "linux/bitmap.h" > #include "linux/atomic.h" > > +#define GUEST_UCALL_FAILED -1 > + > struct ucall_header { > DECLARE_BITMAP(in_use, KVM_MAX_VCPUS); > struct ucall ucalls[KVM_MAX_VCPUS]; > @@ -41,7 +43,8 @@ static struct ucall *ucall_alloc(void) > struct ucall *uc; > int i; > > - GUEST_ASSERT(ucall_pool); > + if (!ucall_pool) > + goto ucall_failed; > > for (i = 0; i < KVM_MAX_VCPUS; ++i) { > if (!test_and_set_bit(i, ucall_pool->in_use)) { > @@ -51,7 +54,13 @@ static struct ucall *ucall_alloc(void) > } > } > > - GUEST_ASSERT(0); > +ucall_failed: > + /* > + * If the vCPU cannot grab a ucall structure, make a bare ucall with a > + * magic value to signal to get_ucall() that things went sideways. > + * GUEST_ASSERT() depends on ucall_alloc() and so cannot be used here. > + */ > + ucall_arch_do_ucall(GUEST_UCALL_FAILED); > return NULL; > } > > @@ -93,6 +102,9 @@ uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) > > addr = ucall_arch_get_ucall(vcpu); > if (addr) { > + TEST_ASSERT(addr != (void *)GUEST_UCALL_FAILED, > + "Guest failed to allocate ucall struct"); > + > memcpy(uc, addr, sizeof(*uc)); > vcpu_run_complete_io(vcpu); > } else { > > base-commit: dc2efbe4813e0dc4368779bc36c5f0e636cb8eb2 > -- >
On 12/9/22 22:03, Sean Christopherson wrote: > From: Sean Christopherson<seanjc@google.com> > Date: Fri, 9 Dec 2022 12:55:44 -0800 > Subject: [PATCH] KVM: selftests: Use magic value to signal ucall_alloc() > failure > > Use a magic value to signal a ucall_alloc() failure instead of simply > doing GUEST_ASSERT(). GUEST_ASSERT() relies on ucall_alloc() and so a > failure puts the guest into an infinite loop. > > Use -1 as the magic value, as a real ucall struct should never wrap. > > Reported-by: Oliver Upton<oliver.upton@linux.dev> > Signed-off-by: Sean Christopherson<seanjc@google.com> > --- > tools/testing/selftests/kvm/lib/ucall_common.c | 16 ++++++++++++++-- > 1 file changed, 14 insertions(+), 2 deletions(-) > > diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c > index 0cc0971ce60e..2f0e2ea941cc 100644 > --- a/tools/testing/selftests/kvm/lib/ucall_common.c > +++ b/tools/testing/selftests/kvm/lib/ucall_common.c > @@ -4,6 +4,8 @@ > #include "linux/bitmap.h" > #include "linux/atomic.h" > > +#define GUEST_UCALL_FAILED -1 > + > struct ucall_header { > DECLARE_BITMAP(in_use, KVM_MAX_VCPUS); > struct ucall ucalls[KVM_MAX_VCPUS]; > @@ -41,7 +43,8 @@ static struct ucall *ucall_alloc(void) > struct ucall *uc; > int i; > > - GUEST_ASSERT(ucall_pool); > + if (!ucall_pool) > + goto ucall_failed; > > for (i = 0; i < KVM_MAX_VCPUS; ++i) { > if (!test_and_set_bit(i, ucall_pool->in_use)) { > @@ -51,7 +54,13 @@ static struct ucall *ucall_alloc(void) > } > } > > - GUEST_ASSERT(0); > +ucall_failed: > + /* > + * If the vCPU cannot grab a ucall structure, make a bare ucall with a > + * magic value to signal to get_ucall() that things went sideways. > + * GUEST_ASSERT() depends on ucall_alloc() and so cannot be used here. > + */ > + ucall_arch_do_ucall(GUEST_UCALL_FAILED); > return NULL; > } > > @@ -93,6 +102,9 @@ uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) > > addr = ucall_arch_get_ucall(vcpu); > if (addr) { > + TEST_ASSERT(addr != (void *)GUEST_UCALL_FAILED, > + "Guest failed to allocate ucall struct"); > + > memcpy(uc, addr, sizeof(*uc)); > vcpu_run_complete_io(vcpu); > } else { > > base-commit: dc2efbe4813e0dc4368779bc36c5f0e636cb8eb2 > -- Queued, thanks. Paolo
diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c index 0cc0971ce60e..e8370da3de24 100644 --- a/tools/testing/selftests/kvm/lib/ucall_common.c +++ b/tools/testing/selftests/kvm/lib/ucall_common.c @@ -41,7 +41,8 @@ static struct ucall *ucall_alloc(void) struct ucall *uc; int i; - GUEST_ASSERT(ucall_pool); + if (!ucall_pool) + goto out; for (i = 0; i < KVM_MAX_VCPUS; ++i) { if (!test_and_set_bit(i, ucall_pool->in_use)) { @@ -51,7 +52,14 @@ static struct ucall *ucall_alloc(void) } } - GUEST_ASSERT(0); +out: + /* + * If the guest cannot grab a ucall structure from the pool then the + * only option to get out to userspace is a bare ucall. This is probably + * a good time to mention that guest assertions depend on ucalls with + * arguments too. + */ + GUEST_UCALL_NONE(); return NULL; }