Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp7359243rdb; Wed, 3 Jan 2024 13:11:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IH9oDNou8Of48aJOxrXqkShfu0k3EOoWDF5cdlKinDXUQ155wMiMVR5R0UxnK27MhgPCfLA X-Received: by 2002:a5d:5f4d:0:b0:336:c9a8:29bc with SMTP id cm13-20020a5d5f4d000000b00336c9a829bcmr9059135wrb.133.1704316271181; Wed, 03 Jan 2024 13:11:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704316271; cv=none; d=google.com; s=arc-20160816; b=KAHTbSiCLD9R7WSis78oZQsLzys79Lc3uygFaYvHqOprDfrkTI+Y+ZHlIin8EVzkKh 6HMoU8zBi6DNVg8dloGU6pnYK43sTkVth+T01CvTO2VnwGP5ktC7c2fmusM5uUJnSUAs Q/Ieo/RQgm7hieorBme8RuPhM4KBHuJvCDkFUU2Eq7xEGTp7unkWEnNC6/xwNy0HXBMH 9y46UEyNVvb1oRWgWo54OiTEN7l/9M1ugpy4W5RlZ+a/LiyEh7k8dy2F3ryepzqtTSFD muuHfTxSv7pzfN4dIp2UxFEftk2wIbBhcaM8MvNnrE2CTBpstJWIZxBZ+sfO4KISHLWQ BZhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=FBHxZQOqHKQWZQsNLhWIGO2APx/c3T+5AETXMz0BW6c=; fh=gPwq7VI0ACnjr97TfzX51vyKVm6NX8dL8R/mqZ06R0A=; b=RuT0p1a2smH6gcLlyo6v92cw8GB5BP+N1QQf6vwLHxjc23wGZeS2oKs2VVuK/pXuq8 pAlKJvj6npZAXJsjF7JOuk8JM8M+6k7X7mwSO1VuXJX0YP29Fj5YPLhAUc0qZdrTUAsA cqRQIYsmB957PnokE6aB7WDi6XmgjxJBSdSZyAgOV3lo4jCA61ARYYuSD5FAEm3/p5TM fd+6J8vL9d3I+0Y+OOby6yUGdusOerXoN08DfpGFGzqBy4NXkswu/OP2hTB/69kfgHuT vbElfG8EUO5TbOAvY/7SZvNN+F98b3zgzZi+rrkqawRcHYaH7QIP1nNcPSb23B2+4/qb u1cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Yc1kbZO4; spf=pass (google.com: domain of linux-kernel+bounces-16007-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-16007-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id h15-20020a05640250cf00b0055378cecf6bsi12661794edb.233.2024.01.03.13.11.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 13:11:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-16007-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Yc1kbZO4; spf=pass (google.com: domain of linux-kernel+bounces-16007-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-16007-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id BC13C1F2587A for ; Wed, 3 Jan 2024 21:11:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 69A491D698; Wed, 3 Jan 2024 21:10:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Yc1kbZO4" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E8241D555 for ; Wed, 3 Jan 2024 21:10:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dbe9c7932b3so1806415276.2 for ; Wed, 03 Jan 2024 13:10:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704316254; x=1704921054; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FBHxZQOqHKQWZQsNLhWIGO2APx/c3T+5AETXMz0BW6c=; b=Yc1kbZO4ea7AXmAsMqvcpf/ajIwTCNSgspDi2aFKYOGB6LkqZ+QzV1vXDcFOlvQ680 x/yBbl9YapBZai778kLoNcHwXoC0gcpKokl7eHFfUndkY0Lm+gAbqd61iNpQ7iBH49fh arYdHmBNQmfhj8Duu2yJeKho8NCmGwen+PT2lMaOhUA8rjZKhdK4MBgGn1U6JvB9Issb exdmNFY0CtvKuD1ZUlQZ4w763RUltcos2qASJnrsmcxAmGt+LdMjvyzH2sAUKhhN6Mop yeXASdaEAQ58Ql+9ltjS1sTDpmlY3E4dLeQ/eoF17YgbjUPFOEiQJNN7U00J3MiojwrQ Jc0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704316254; x=1704921054; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FBHxZQOqHKQWZQsNLhWIGO2APx/c3T+5AETXMz0BW6c=; b=IgHbWUJawC0uFO3UKXdGaB4IJd+kwY0tp9TCiDUKgm/h2DmIgP0LQy6zOpCONG3Huv mJ5vK85UaDXeamJH1LQM+F1AyLZkajsapht9GjXTCCmyLZokChi+opzW7DiQOk1hVoMq U3JJLVoUG9hejrQ/Kh3PZNPsSht8TrM9HFuEJEsG8jUMhH3d0cYr3jKfZYOvU9dA7OHP an+8hDPN1yBxGpMeGGQhwvpkDDYB/Bn7BA3PZG1J/ueOyG4N0Poy/e6qI4r5jbRaEkA5 0tr9q41HecRtyQZGTa2Vy+oyQQ4bLx6+CWeJYA+VnoJXxDkEnaqoDmn0IMaMnI6ayMT0 DlNw== X-Gm-Message-State: AOJu0Yxk/Wfx/izrqYD/gLlimqNfVO+Wiow0d0LI2vsLnrr3v/oyVtqa YwLGyM1Vg0HnFFSPI+H/+xEkgFueAcfFD06/Ww== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:9f03:0:b0:dbd:f0c7:8926 with SMTP id n3-20020a259f03000000b00dbdf0c78926mr6852351ybq.7.1704316254236; Wed, 03 Jan 2024 13:10:54 -0800 (PST) Date: Wed, 3 Jan 2024 13:10:52 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240102232136.38778-1-Ashish.Kalra@amd.com> Message-ID: Subject: Re: [PATCH] x86/sev: Add support for allowing zero SEV ASIDs. From: Sean Christopherson To: Ashish Kalra Cc: pbonzini@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, thomas.lendacky@amd.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, joro@8bytes.org Content-Type: text/plain; charset="us-ascii" On Wed, Jan 03, 2024, Ashish Kalra wrote: > Hello Sean, > > On 1/2/2024 6:30 PM, Sean Christopherson wrote: > > On Tue, Jan 02, 2024, Ashish Kalra wrote: > > > @@ -2172,8 +2176,10 @@ void sev_vm_destroy(struct kvm *kvm) > > > void __init sev_set_cpu_caps(void) > > > { > > > - if (!sev_enabled) > > > + if (!sev_guests_enabled) { > > Ugh, what a mess. The module param will show sev_enabled=false, but the caps > > and CPUID will show SEV=true. > > > > And this is doubly silly because "sev_enabled" is never actually checked, e.g. > > if misc cgroup support is disabled, KVM_SEV_INIT will try to reclaim ASIDs and > > eventually fail with -EBUSY, which is super confusing to users. > > But this is what we expect that KVM_SEV_INIT will fail. In this case, > sev_asid_new() will not actually try to reclaim any ASIDs as sev_misc_cg_try_charge() > will fail before any ASID bitmap walking/reclamation and return an error which > will eventually return -EBUSY to the user. Please read what I wrote. "if misc cgroup support is disabled", i.e. if CONFIG_CGROUP_MISC=n, then sev_misc_cg_try_charge() is a nop. > > The other weirdness is that KVM can cause sev_enabled=false && sev_es_enabled=true, > > but if *userspace* sets sev_enabled=false then sev_es_enabled is also forced off. > But that is already the behavior without this patch applied. > > > > In other words, the least awful option seems to be to keep sev_enabled true :-( > > > > > kvm_cpu_cap_clear(X86_FEATURE_SEV); > > > + return; > > This is blatantly wrong, as it can result in KVM advertising SEV-ES if SEV is > > disabled by the user. > No, this ensures that we don't advertise any SEV capability if neither > SEV/SEV-ES or in future SNP is enabled. No, it does not. There is an early return statement here that prevents KVM from invoking kvm_cpu_cap_clear() for X86_FEATURE_SEV_ES. Do I think userspace will actually be tripped up by seeing SEV_ES without SEV? No. Is it unnecessarily confusing? Yes. > > > + } > > > if (!sev_es_enabled) > > > kvm_cpu_cap_clear(X86_FEATURE_SEV_ES); > > > } > > > @@ -2229,9 +2235,11 @@ void __init sev_hardware_setup(void) > > > goto out; > > > } > > > - sev_asid_count = max_sev_asid - min_sev_asid + 1; > > > - WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count)); > > > - sev_supported = true; > > > + if (min_sev_asid <= max_sev_asid) { > > > + sev_asid_count = max_sev_asid - min_sev_asid + 1; > > > + WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count)); > > > + sev_supported = true; > > > + } > > > /* SEV-ES support requested? */ > > > if (!sev_es_enabled) > > > @@ -2262,7 +2270,8 @@ void __init sev_hardware_setup(void) > > > if (boot_cpu_has(X86_FEATURE_SEV)) > > > pr_info("SEV %s (ASIDs %u - %u)\n", > > > sev_supported ? "enabled" : "disabled", > > > - min_sev_asid, max_sev_asid); > > > + sev_supported ? min_sev_asid : 0, > > > + sev_supported ? max_sev_asid : 0); > > I honestly think we should print the "garbage" values. The whole point of > > printing the min/max SEV ASIDs was to help users understand why SEV is disabled, > > i.e. printing zeroes is counterproductive. > > > > > if (boot_cpu_has(X86_FEATURE_SEV_ES)) > > > pr_info("SEV-ES %s (ASIDs %u - %u)\n", > > > sev_es_supported ? "enabled" : "disabled", > > It's all a bit gross, but I think we want something like this (I'm definitely > > open to suggestions though): > > > > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c > > index d0c580607f00..bfac6d17462a 100644 > > --- a/arch/x86/kvm/svm/sev.c > > +++ b/arch/x86/kvm/svm/sev.c > > @@ -143,8 +143,20 @@ static void sev_misc_cg_uncharge(struct kvm_sev_info *sev) > > static int sev_asid_new(struct kvm_sev_info *sev) > > { > > - int asid, min_asid, max_asid, ret; > > + /* > > + * SEV-enabled guests must use asid from min_sev_asid to max_sev_asid. > > + * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1. Note, the > > + * min ASID can end up larger than the max if basic SEV support is > > + * effectively disabled by disallowing use of ASIDs for SEV guests. > > + */ > > + unsigned int min_asid = sev->es_active ? 1 : min_sev_asid; > > + unsigned int max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid; > > + unsigned int asid; > > bool retry = true; > > + int ret; > > + > > + if (min_asid > max_asid) > > + return -ENOTTY; > > This will still return -EBUSY to user. Huh? The above is obviously -ENOTTY, and I don't see anything in the call stack that will convert it to -EBUSY. > This check here or the failure return from sev_misc_cg_try_charge() are quite > similar in that sense. > > My point is that the same is achieved quite cleanly with > sev_misc_cg_try_charge() too. "Without additional effort" is not synonymous with "cleanly". Relying on an accounting restriction that is completely orthogonal to basic functionality is not "clean". > > WARN_ON(sev->misc_cg); > > sev->misc_cg = get_current_misc_cg(); > > @@ -157,12 +169,6 @@ static int sev_asid_new(struct kvm_sev_info *sev) > > mutex_lock(&sev_bitmap_lock); > > - /* > > - * SEV-enabled guests must use asid from min_sev_asid to max_sev_asid. > > - * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1. > > - */ > > - min_asid = sev->es_active ? 1 : min_sev_asid; > > - max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid; > > again: > > asid = find_next_zero_bit(sev_asid_bitmap, max_asid + 1, min_asid); > > if (asid > max_asid) { > > @@ -2232,8 +2238,10 @@ void __init sev_hardware_setup(void) > > goto out; > > } > > - sev_asid_count = max_sev_asid - min_sev_asid + 1; > > - WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count)); > > + if (min_sev_asid <= max_sev_asid) { > > + sev_asid_count = max_sev_asid - min_sev_asid + 1; > > + WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count)); > > + } > > sev_supported = true; > > /* SEV-ES support requested? */ > > @@ -2264,8 +2272,9 @@ void __init sev_hardware_setup(void) > > out: > > if (boot_cpu_has(X86_FEATURE_SEV)) > > pr_info("SEV %s (ASIDs %u - %u)\n", > > - sev_supported ? "enabled" : "disabled", > > - min_sev_asid, max_sev_asid); > > + sev_supported ? (min_sev_asid <= max_sev_asid ? "enabled" : "unusable") : "disabled", > > + sev_supported ? min_sev_asid : 0, > > + sev_supported ? max_sev_asid : 0); > > We are not showing min and max ASIDs for SEV as {0,0} with this patch as > sev_supported is true ? Yes, and that is deliberate. See this from above: : I honestly think we should print the "garbage" values. The whole point of : printing the min/max SEV ASIDs was to help users understand why SEV is disabled, : i.e. printing zeroes is counterproductive.