From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E2AECC4332F for ; Fri, 9 Dec 2022 21:04:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=8xanzJ4ToZdPD2tlfgHO/JTb5+TO+H4gsPQPJibdizQ=; b=eNLqPks32ZwFqc Hyalv3L5EpUpo/PR93WVu6Ada50TCaecwCbn4qDTJPEurbXVAPHRcWxRiLTYVCbLD7O3tdl5JHBx0 4Ikpq+vyOr+sN9DVNg87Jlw3kEvtJMVF6ckVBYMtlpJoKs4ogShKG1L3RQm/ycheZ8Y+sB39ZQk10 B382RunrlpKQftQtL7RnSXrPhXqyduTLTU93YfB7euX728nBIPhq0PwQt8j3fFSS1KPtG81vb0O8s 6QasiN5mB71vfwDQGihh5bUy5+LP+Yeh9+/KdAFe4bMeZpdHtPY1rTjskLI4Z8aeYSLXZAKFgPDsH fMxC+IWJ6HY7m7ZjILog==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1p3kXE-00BLqe-NU; Fri, 09 Dec 2022 21:03:56 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1p3kXB-00BLlU-CS for linux-arm-kernel@lists.infradead.org; Fri, 09 Dec 2022 21:03:54 +0000 Received: by mail-pl1-x62f.google.com with SMTP id 4so181902plj.3 for ; Fri, 09 Dec 2022 13:03:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=HqhkVKX+RdgYCWZiIceLsOHMpWv/ROuczp7qb/H7X1w=; b=geRGPSQKUlIYqCU/v/MAful0c2VKh6VA6SBeijyPLtHNmaNPvdTAZRBm+8BK0vMyF7 oJs/xaNz1FVL8jiMpUauu2wtzHgyNFuSzbsou1/FkQDUTY5R2F/BqoUtLcz7XXJd3ZAO Dmk3W82U//fL1/HxmzzBDyZ7VklM1UH74NpwkG3WyTvUXcSDELZRwL14XyE4yxNMMTiP 7vxNfdzUPeKjtlkEpdgj2U13i8nATEgPias3k43ZtZwrU5RjHtwlN5SiT1tERKN6A3Rk WNJjSbXl0byXigqUqjfuCWVXto0wnqtwZ9BO4n+XScjq16D9RNXVrELTuGdILxKMMIaA 6JaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=HqhkVKX+RdgYCWZiIceLsOHMpWv/ROuczp7qb/H7X1w=; b=oX34dQuX/JOKfRFrbB/gVZgTBhP74xgR9Eticmr31jTbMmQlnR17TSr09wdZSmLSnM X4pC3Ie/ZxPCKTldyDoAJ7tF+ww/3fzGtJCqSJwIR4EK/TKpHkE+/E3AatKqrT9FCBoT TbMliqnOm+qiD1C4Wsnkbk1dKoMGahn6YBg/+NhXmCiLuUt6fozJZIhN2Cb6prf8krXi x2JwECKjdQhnHqbaMAkprzxej0WAHOKCbLjn/5xdDr5r7PEYTL2i+BQbK9gbpkNcYWvL aEPEZp0hmz2V0hWHu4hw5fVm93/VVXLpJY+/73/VOPtKSMWwIKNmFiYRsAYoYG9lGwJe qC2g== X-Gm-Message-State: ANoB5pnD8S5hqgH1apUHF3gk8gPbDOzez59uYPFFLwj8VwW7TfOPIULG T7AsukTVfUU8oAKXb6VQcF3SjQ== X-Google-Smtp-Source: AA0mqf76S2APEPIqvhNO2xeCJ5k7zOJZF0cOVyP09wKMa17nX0r0d7TDym1CMnQt7VgOPeLCqe8Uuw== X-Received: by 2002:a05:6a20:7f5c:b0:9d:b8e6:d8e5 with SMTP id e28-20020a056a207f5c00b0009db8e6d8e5mr24495pzk.2.1670619828811; Fri, 09 Dec 2022 13:03:48 -0800 (PST) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id c11-20020a170903234b00b001885041d7b8sm1722515plh.293.2022.12.09.13.03.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Dec 2022 13:03:48 -0800 (PST) Date: Fri, 9 Dec 2022 21:03:45 +0000 From: Sean Christopherson To: Oliver Upton Cc: Marc Zyngier , James Morse , Alexandru Elisei , Paolo Bonzini , Shuah Khan , Andrew Jones , Peter Gonda , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, kvmarm@lists.linux.dev, Ricardo Koller , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 7/7] KVM: selftests: Avoid infinite loop if ucall_alloc() fails Message-ID: References: <20221209015307.1781352-1-oliver.upton@linux.dev> <20221209015307.1781352-8-oliver.upton@linux.dev> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20221209015307.1781352-8-oliver.upton@linux.dev> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221209_130353_470095_E9B9707E X-CRM114-Status: GOOD ( 31.74 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 > --- > 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 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 Signed-off-by: Sean Christopherson --- 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 -- _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel