Message ID | 20240102232136.38778-1-Ashish.Kalra@amd.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-14968-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp4733705dyb; Tue, 2 Jan 2024 15:22:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IFKnziik8OBR+ak4X2qn2WJORz2TXiIlWoG9zOjckIaCfIAa0dUJN8vIUzw+QPci231MqDk X-Received: by 2002:a17:906:6142:b0:a23:6c63:30a3 with SMTP id p2-20020a170906614200b00a236c6330a3mr158684ejl.32.1704237735311; Tue, 02 Jan 2024 15:22:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704237735; cv=pass; d=google.com; s=arc-20160816; b=hPoeku74uUOLkvn3lhRMS14hYwzwAIPUbixoJNaU9vSqZUzV+fIKhehZ0y3M3CgJBV 12cxE4MdF7mrBVo7qiWwgjPEi+NPEQudoMB16656FIrTqLrpLxC1briEDtgzHhHCY0Tg 6Kp/zuXZfj8Z8JGz/qws2yD177kgd9+YxjdKpO2c+EsXyIPEOtzJRnxkaPcjrWiJx0zx Xd77iBq++W5OJolsP391qokxY7J9M0FHUDJ/QoiRxUmykbeIJDY8Wya5qVO79F1KLOxo WaYtPsDxuXE0CYCz/YwulsFT+Iqs7q7E3H7xzxBN5MsNHhcEvur+eZ1IyeJPW5xgSKjo AKWw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=cx1KCH4vzCYjZr/dJQJAt9v3mDo5m2kpsLg5RRyXb8I=; fh=sXYBQUbe074EMTSJSvlGT6s2FIscOJBzb7K+iO7U4Y0=; b=VW3lz+XJxs+PdWT77Bx7dOoABLmeV1jEJ2RQuTFQHi0jdJRnYClQ6dTbzHc+n8GiAm IgZ2rW/q9kDsS8NUP5jYcCGOuo/b0lFAUT9ZrIeypV7p84a/WYdfc/BRpfR/A3qpt3DW FOXQrCN0oE+S919hrJz1AJZphoRw+HZYyxPSpKjATQEwc+PbjZDMZfqzHvzO6OL8Q4Hx vkrj40GNZescCvbURrOi+lxhfCxql37fG2FDZTaJSVu3oND7+7VWj4Kc1XrA7FEAqiOh /sHHkA7WaFCDtwaqUfDe/2bpxGXW9dl/uHIKyzleEBvZi28ttLBuXo4Xlyxnw+3ZVDz8 Sm0A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b="lLHQn/Bf"; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel+bounces-14968-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14968-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id pv18-20020a170907209200b00a28129dd17esi1614188ejb.651.2024.01.02.15.22.14 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 15:22:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14968-ouuuleilei=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=@amd.com header.s=selector1 header.b="lLHQn/Bf"; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel+bounces-14968-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14968-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.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 979881F23413 for <ouuuleilei@gmail.com>; Tue, 2 Jan 2024 23:22:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 79B2D17994; Tue, 2 Jan 2024 23:21:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="lLHQn/Bf" X-Original-To: linux-kernel@vger.kernel.org Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2040.outbound.protection.outlook.com [40.107.244.40]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19E321773F; Tue, 2 Jan 2024 23:21:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=drrXFiIjpSaq9otzGtlZ9SwtgLAwjh7pmnzhyjIh7NLpXYdPe+jXi59EMNLErQ7AUT3lYu6QYEWIZgjAEdtDOKzPQUXXjO+YXKR0Ok6lgn2iqNZRbB5EpYU298XEDtFeIyXBNLZgxwNIAdYWCF9nqC155jrIAumPC3qxciUnHeHiA8VzHldMBkX93Gps50FWqReO6vEo7v3Kjw75HZqK2ndLH2XH6viGf7LbmxLU8Y2FTaIbbTs3uJesSxmtqtba+CYniQ4H23UUtZDPz/1X1FB20FEn/MQfXXGGrxNL9vSQC6+xIyn7d+z9xJmeLWSym6NByPwKp8L/L9GU/VoIpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=cx1KCH4vzCYjZr/dJQJAt9v3mDo5m2kpsLg5RRyXb8I=; b=I4/HR5KKMLtAskiYVNkufKttjpmk5EKc5mfLVqmu7pmCQN1MFgJe8VwwL2/p4aT7do7vBYJxjjpjRTznA5I4aSdIYK8ecp+IqIrUmw/LkRN261m/xQAkmWjBaaEyBPJYxtXWbl/LtZzaemxQFIdUe3V43Ef8hrTY/NBhhrW4hKHK5Y5qlrkcBFMNiZVKXdojg/2Y9VLt/qifn4K+nBUC7nowIauc1/E3ZJETj64Ntjdh5cM6HTt1gCt15f9Y5E5m4CDTsn8Rx8yOKm9e6qM23ROd5giCDQMOJwSXaW449WZUJ8tXwelngRN0G5YhsG96RFfMyaRedwirlH+D5s6U9Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=cx1KCH4vzCYjZr/dJQJAt9v3mDo5m2kpsLg5RRyXb8I=; b=lLHQn/BfgEYVkNHVXcNlvpB2qM5BH0oFWC6149yzCbk4Tt5mBB2SD+ydw+T7awyuaDw6awvNsYvnnVtLZoxokmpbowC6y6TlVVaD3SpAhuM/zgHFcZq1NzgykitdDLewjCzd37Sx8pXJbsQAgdEfyjliCcRs+Af7WuEc9cpuMkY= Received: from DS7PR03CA0356.namprd03.prod.outlook.com (2603:10b6:8:55::22) by BL1PR12MB5270.namprd12.prod.outlook.com (2603:10b6:208:31e::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7159.13; Tue, 2 Jan 2024 23:21:48 +0000 Received: from DS2PEPF0000343F.namprd02.prod.outlook.com (2603:10b6:8:55:cafe::a1) by DS7PR03CA0356.outlook.office365.com (2603:10b6:8:55::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7135.23 via Frontend Transport; Tue, 2 Jan 2024 23:21:48 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DS2PEPF0000343F.mail.protection.outlook.com (10.167.18.42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7159.9 via Frontend Transport; Tue, 2 Jan 2024 23:21:48 +0000 Received: from purico-ed09host.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Tue, 2 Jan 2024 17:21:46 -0600 From: Ashish Kalra <Ashish.Kalra@amd.com> To: <pbonzini@redhat.com> CC: <seanjc@google.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> Subject: [PATCH] x86/sev: Add support for allowing zero SEV ASIDs. Date: Tue, 2 Jan 2024 23:21:36 +0000 Message-ID: <20240102232136.38778-1-Ashish.Kalra@amd.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343F:EE_|BL1PR12MB5270:EE_ X-MS-Office365-Filtering-Correlation-Id: 2448efd4-ea16-4695-4a09-08dc0be997da X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: z3tNTV6mLoxI7qBcPl62QPTp7mDv4H5JGDNeKlehUZ3nDkeSRaJpUa/LyfuIb682q8Qteggh0IT3EBbORzE1nwHgXFan+9T9q80MuFBvXch4XAd0OknNH9rZ5fKBHSyrFqchcFTcickAd6fRFLeOGB1wTcF9wrJwqlxxCS11/OcMpdaIY48KYOPpujeCOm9y4qUhhpMwg6UmDi5K5Q/5ru+sd+oCbu/DMwqkiTrrIrZ7QsP/cpa4k6Ewy/9xBQRU9uQwJ9DrNW17loitdEQyR+LKIF0t1Y+N63epqg/vbOOglB1KXZxZINutXvBQUrMrLqXEiIfEwNULBDPqFJn0kIWFU/H19QohjH2f1mx6Iu2IvIah4fYV6jgNfQAvGomR8aZ1K/0a/G0EJ54M3jQBFnCeHHHtxmguuaIhfWF0DOSBN9UluReTmM/cQD+y4AqMG1uOs7wd42ByQqCj2g1FaC/zsNX1SFddco+62z5bOPMxlFGYzv2cc3kl0//XyCiqrV9NzpnIxbjgdjihfAdOfVLtNbxhxGPutMOjdtWdVe7mmKjjz58DYmYlbCLtlqtz5r6ipc4hLpf0Kfd2T6XiYLhIi+sWnmazBSzCjDhJgSFWmOewDaeCJ74pkuXt7dh+EqB/o3nRV6TIhIfie975UxXItZB8eoplJieP2ySbJ+uSSjfXqrdDQxsutuCvuNLEUWh3Jhw1iKBPicn9N6UsOEPJfmBeVtwGrl00TNM29s0qyhR8vFL0vbB+/Ed/zQXlSqxNNkFBtnBXONY3Wb+5FQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(346002)(396003)(39860400002)(136003)(376002)(230922051799003)(64100799003)(451199024)(1800799012)(186009)(82310400011)(40470700004)(36840700001)(46966006)(2906002)(26005)(2616005)(47076005)(83380400001)(41300700001)(336012)(356005)(81166007)(16526019)(1076003)(426003)(82740400003)(36860700001)(316002)(8676002)(8936002)(54906003)(7416002)(4326008)(5660300002)(478600001)(70206006)(6666004)(6916009)(70586007)(7696005)(86362001)(36756003)(40460700003)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jan 2024 23:21:48.1937 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2448efd4-ea16-4695-4a09-08dc0be997da X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS2PEPF0000343F.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5270 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787022787474227472 X-GMAIL-MSGID: 1787022787474227472 |
Series |
x86/sev: Add support for allowing zero SEV ASIDs.
|
|
Commit Message
Kalra, Ashish
Jan. 2, 2024, 11:21 p.m. UTC
From: Ashish Kalra <ashish.kalra@amd.com> Some BIOSes allow the end user to set the minimum SEV ASID value (CPUID 0x8000001F_EDX) to be greater than the maximum number of encrypted guests, or maximum SEV ASID value (CPUID 0x8000001F_ECX) in order to dedicate all the SEV ASIDs to SEV-ES or SEV-SNP. The SEV support, as coded, does not handle the case where the minimum SEV ASID value can be greater than the maximum SEV ASID value. As a result, the following confusing message is issued: [ 30.715724] kvm_amd: SEV enabled (ASIDs 1007 - 1006) Fix the support to properly handle this case. Fixes: 916391a2d1dc ("KVM: SVM: Add support for SEV-ES capability in KVM") Signed-off-by: Ashish Kalra <ashish.kalra@amd.com> Cc: stable@vger.kernel.org --- arch/x86/kvm/svm/sev.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-)
Comments
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. 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. 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. > + } > 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; 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); if (boot_cpu_has(X86_FEATURE_SEV_ES)) pr_info("SEV-ES %s (ASIDs %u - %u)\n", sev_es_supported ? "enabled" : "disabled",
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. > > 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. > >> + } >> 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. 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. > 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 ? Thanks, Ashish > if (boot_cpu_has(X86_FEATURE_SEV_ES)) > pr_info("SEV-ES %s (ASIDs %u - %u)\n", > sev_es_supported ? "enabled" : "disabled",
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.
On 1/3/2024 3:10 PM, Sean Christopherson wrote: > 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. Actually, sev_asid_new() returning failure to sev_guest_init() will cause it to return -EBUSY to user. Thanks, Ashish >> 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.
On Wed, Jan 03, 2024, Ashish Kalra wrote: > On 1/3/2024 3:10 PM, Sean Christopherson wrote: > > > > 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. > > Actually, sev_asid_new() returning failure to sev_guest_init() will cause it > to return -EBUSY to user. Argh, I see it now. That too should be fixed, e.g. diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index d0c580607f00..79eb11083ad5 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -246,21 +246,20 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; - int asid, ret; + int ret; if (kvm->created_vcpus) return -EINVAL; - ret = -EBUSY; if (unlikely(sev->active)) - return ret; + return -EINVAL; sev->active = true; sev->es_active = argp->id == KVM_SEV_ES_INIT; - asid = sev_asid_new(sev); - if (asid < 0) + ret = sev_asid_new(sev); + if (ret < 0) goto e_no_asid; - sev->asid = asid; + sev->asid = ret; ret = sev_platform_init(&argp->error); if (ret)
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 4900c078045a..ad41008ca0d9 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -59,10 +59,14 @@ module_param_named(sev_es, sev_es_enabled, bool, 0444); /* enable/disable SEV-ES DebugSwap support */ static bool sev_es_debug_swap_enabled = true; module_param_named(debug_swap, sev_es_debug_swap_enabled, bool, 0444); + +/* When true, at least one type of SEV guest is enabled to run */ +static bool sev_guests_enabled; #else #define sev_enabled false #define sev_es_enabled false #define sev_es_debug_swap_enabled false +#define sev_guests_enabled false #endif /* CONFIG_KVM_AMD_SEV */ static u8 sev_enc_bit; @@ -1854,7 +1858,7 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) struct kvm_sev_cmd sev_cmd; int r; - if (!sev_enabled) + if (!sev_guests_enabled) return -ENOTTY; if (!argp) @@ -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) { kvm_cpu_cap_clear(X86_FEATURE_SEV); + return; + } 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); if (boot_cpu_has(X86_FEATURE_SEV_ES)) pr_info("SEV-ES %s (ASIDs %u - %u)\n", sev_es_supported ? "enabled" : "disabled", @@ -2270,6 +2279,7 @@ void __init sev_hardware_setup(void) sev_enabled = sev_supported; sev_es_enabled = sev_es_supported; + sev_guests_enabled = sev_enabled || sev_es_enabled; if (!sev_es_enabled || !cpu_feature_enabled(X86_FEATURE_DEBUG_SWAP) || !cpu_feature_enabled(X86_FEATURE_NO_NESTED_DATA_BP)) sev_es_debug_swap_enabled = false; @@ -2278,7 +2288,7 @@ void __init sev_hardware_setup(void) void sev_hardware_unsetup(void) { - if (!sev_enabled) + if (!sev_guests_enabled) return; /* No need to take sev_bitmap_lock, all VMs have been destroyed. */ @@ -2293,7 +2303,7 @@ void sev_hardware_unsetup(void) int sev_cpu_init(struct svm_cpu_data *sd) { - if (!sev_enabled) + if (!sev_guests_enabled) return 0; sd->sev_vmcbs = kcalloc(nr_asids, sizeof(void *), GFP_KERNEL);