Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp5192640rdb; Sat, 30 Dec 2023 09:33:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IHNUPD9D2pDcTSZeVwd2f4aYhK7yPnPDho3lk9oACsDMtQRftP/7rrEfHeaMUfD/08e+c7l X-Received: by 2002:a05:6808:2107:b0:3bb:f65a:c46b with SMTP id r7-20020a056808210700b003bbf65ac46bmr906759oiw.37.1703957627620; Sat, 30 Dec 2023 09:33:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1703957627; cv=pass; d=google.com; s=arc-20160816; b=KPvy2PSXFCeTqodGlCFNjE47/Oh67cx9eGCTInT1adI/3Eubo8e6tUCGLd/murxi74 mjEhz2IUKDjN2dXhdbeTRJyymsc6lqIYM2xgrKp98HH8xtrQTAJgGIe3CnCyUqtQRS5P XLVYu3lNP90L5B2NVLpRcMIGX3G5/MZB/OssfU4oINqmk0IB5XBeavzz7Ca3YpaFqMLO jkVXPz8Sj/MHkhGnhlq2pzibwDebLxsiGh+F8D039ec8oNqH3ONwMOrp18dOQDrvpFBG 0RE2BMCsu8u1M+fSRXsA3XSpZwOCq7As1wZuy6Me9GbYBlsAOe7VdpLWHS1zrhs2JU83 lpRQ== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=5JsQBSzcT7e4MZ/uvITG5UcHhkGq444WmXADqD/SnnE=; fh=lVLw0fMohyu23Gmdby27Gdtb7ZKjucvqIKd0C+wdqNQ=; b=JQXYBhqMtzJrdiv8TduL82DakAw7iQx0WKoB9A01vozDaHKDdqnX43JEUnDBgYtrxb TqRjiRZtvYBfKrIkyYa4DZIIq4kilYO7QAGVDahJw8PkuU6EVLi92AhaqzpCstvkMDuN qAsUsKQaOInHZkwdzwyqZQ6cAE0A3igatTWuDhOn/EjvJvaIM+tuZWkq02HbcVr18K2Q kMfqm0rG3zjHuqabYx29iErkoGvnMH2cliuoRpXNJ95xpduY9YbXmPqs8yBCjq2MfXzt y1mWkwuTM2suCXxQ8+l+MMMm7TjKmdSAU8/PuC+jTVGBVMxs2ldqPmFX7jnOp5BTk31K hREg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=GkPKsY3f; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel+bounces-13546-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13546-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id o2-20020a0ccb02000000b0067eb5d46befsi22055850qvk.502.2023.12.30.09.33.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Dec 2023 09:33:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-13546-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=GkPKsY3f; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel+bounces-13546-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13546-linux.lists.archive=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 502681C220C7 for ; Sat, 30 Dec 2023 17:33:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F9A7BE5D; Sat, 30 Dec 2023 17:32:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="GkPKsY3f" X-Original-To: linux-kernel@vger.kernel.org Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2040.outbound.protection.outlook.com [40.107.220.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 907C014A9E; Sat, 30 Dec 2023 17:32:24 +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=UecjD9/bjLhxqM7AWFRVvoPuS4hoxjaHWUxldgkJqYtmEu4OIgXFkAOqqMEuSgkD2otUgz2Gt298VIxwcaNyd5VhNJDRykFLd1a1q2iUDxo2F1pVA30/IKsCG64WXYO//69K3PO6hEq/LrrT865S/f7Yqc2hnMzn+RVa5JFYFaV3OKyb/23CKDN1yYtWxS6QY/T3UnHN+RpPjohfH2wt+QPBunOJ3uksK27bXwo+RXZcOFhjYBONmFi0Fo/oS55V3WDtgsc7OijMNkMx7+Dgnp8ttdgG5xeLi8OfSVJCSS7Nx92QBRP0z15yuVSloWDggZFX7Yk2uoMbKy/3GwQj5A== 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=5JsQBSzcT7e4MZ/uvITG5UcHhkGq444WmXADqD/SnnE=; b=mGOwaWoT09sTIX39bJfHjPzKACu/LyVhK9kX31vj7N38Tgm3CoEHTLicP/H8d8PbAIjo2QbpV20l4GJuMZ89sQV2sow2NKlRemrPmnsB/mWwIAWyvOK0A/TGPqEkqnn3egMQztanJ02bAvbw3cmh9Af+zBxGcHi39BZjRvi9XE1OTwMuMX/MIgLhy4DiiMteww158M3CnmHeY3BkEmGH8cweFli5bETAbLkUePWQkYz6OJc3GTXS5cKSQCX9oK1yrC65LUBQryyaNvD9INQ4+8ELMM6/pF0o1nJDrTL6k/qBAvAC03nqzIyGx5OOxisHS3fT7dvu9pQx7PkBPOJ1yA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org 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=5JsQBSzcT7e4MZ/uvITG5UcHhkGq444WmXADqD/SnnE=; b=GkPKsY3fxS9C0iIUoiisAbfjT+RcqFeTTFd4PcuiQrpvcuWgtdNZLncAU62Gj8zgClKEDkzcbl2pJKHl1/gqy1wOEQ63HaWNIg8tECtzKIxIZgfCeLeeJfj7lRq3upfYwshbnpEaam/wK6dso2pSULSNRpSfi57VCnO0i9jhqTw= Received: from MW4PR03CA0261.namprd03.prod.outlook.com (2603:10b6:303:b4::26) by PH8PR12MB7184.namprd12.prod.outlook.com (2603:10b6:510:227::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7135.22; Sat, 30 Dec 2023 17:32:20 +0000 Received: from CO1PEPF000044EF.namprd05.prod.outlook.com (2603:10b6:303:b4:cafe::59) by MW4PR03CA0261.outlook.office365.com (2603:10b6:303:b4::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7135.20 via Frontend Transport; Sat, 30 Dec 2023 17:32:20 +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 CO1PEPF000044EF.mail.protection.outlook.com (10.167.241.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7159.9 via Frontend Transport; Sat, 30 Dec 2023 17:32:20 +0000 Received: from localhost (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; Sat, 30 Dec 2023 11:32:19 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v11 30/35] KVM: x86: Add gmem hook for determining max NPT mapping level Date: Sat, 30 Dec 2023 11:23:46 -0600 Message-ID: <20231230172351.574091-31-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231230172351.574091-1-michael.roth@amd.com> References: <20231230172351.574091-1-michael.roth@amd.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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: CO1PEPF000044EF:EE_|PH8PR12MB7184:EE_ X-MS-Office365-Filtering-Correlation-Id: 2e803a5d-74a1-4cd4-bd93-08dc095d4705 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8GPdSa5oTBIKOHJzBzYatoAeugAZcxVEESHEgQje4Gw3XHCvqO1wsonpxJC/di3qn97i6RsHhhtp6aAbCczRO43SSl3KKUtNfCyOM0N8BTn7p9Lxd0gw/zM57TuIrSBXEUCq6O2RaKXO1m/1VK1g0n9d/2dofFBEspYRT/8Ets7uWY/DoWgAB+KRxGVQX6BdNwuOGVipG+zyKboKzLss/g9PEbG2oEuiXb+jVCvr/SxUo6L72ro404qSBkp8vaOVTdVjQVrGnCi2QIG2yTuTRnCW5Wa2OtUNhFkfvTAa3EZLhccJ880pwLC30h/oELZNPHonPndAIb52C9Ar+VzmPpUwjINZIXYt3c/GPZPBKnVlkv0gaF5SfHNtrEeDxjYV6UEPl2WY3O0rYnGTb3wXojMsyT+99kHMTmg3B3hGKfaR4nDKlZ88EeD4trPLp3Snvz/24Mi6zS1K3bbBjimlalxe0082hTs+kHpWdeWLNGL0yPBidTD0B20X1UTEpSAPEfhdqS00FvRLNHDIgkIpYFL7NgGbp7lIHAgwBKQj5pE0mKYh2Bda0GXk21F6yOQ/iRkWuxTkuby5bkmOywOtT6/ww9g8c4KMcjeapojED1Rc+CbAL6buZzMggzUbP7/5uj4AEpxWg0drdJGICHgtIwEiwKqjUJW/HwiEuFOEc17Ef9Q9OT5iKlEwXo6nXrjGLv6fDnYevnxV8//MsCH+ObuShI64OpFWvjpRoX/QwexGOYOJ5txGMqmNrydLeFSrq22Sun1K26y4OfuTjf9fWg== 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)(136003)(39860400002)(396003)(376002)(346002)(230922051799003)(82310400011)(1800799012)(186009)(451199024)(64100799003)(46966006)(36840700001)(40470700004)(26005)(1076003)(54906003)(70586007)(70206006)(40460700003)(6916009)(40480700001)(2616005)(6666004)(83380400001)(4326008)(8676002)(8936002)(36756003)(316002)(336012)(426003)(478600001)(16526019)(44832011)(7416002)(7406005)(5660300002)(47076005)(86362001)(356005)(2906002)(81166007)(36860700001)(82740400003)(41300700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Dec 2023 17:32:20.6373 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2e803a5d-74a1-4cd4-bd93-08dc095d4705 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: CO1PEPF000044EF.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB7184 In the case of SEV-SNP, whether or not a 2MB page can be mapped via a 2MB mapping in the guest's nested page table depends on whether or not any subpages within the range have already been initialized as private in the RMP table. The existing mixed-attribute tracking in KVM is insufficient here, for instance: - gmem allocates 2MB page - guest issues PVALIDATE on 2MB page - guest later converts a subpage to shared - SNP host code issues PSMASH to split 2MB RMP mapping to 4K - KVM MMU splits NPT mapping to 4K At this point there are no mixed attributes, and KVM would normally allow for 2MB NPT mappings again, but this is actually not allowed because the RMP table mappings are 4K and cannot be promoted on the hypervisor side, so the NPT mappings must still be limited to 4K to match this. Add a hook to determine the max NPT mapping size in situations like this. Signed-off-by: Michael Roth --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 12 ++++++++++-- arch/x86/kvm/svm/sev.c | 27 +++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index c4b7b0db7be3..b0a174213dad 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -140,6 +140,7 @@ KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) KVM_X86_OP_OPTIONAL(alloc_apic_backing_page) KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) +KVM_X86_OP_OPTIONAL_RET0(gmem_max_level) KVM_X86_OP_OPTIONAL(gmem_invalidate) #undef KVM_X86_OP diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 9e45402e51bc..ee1e81608e07 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1806,6 +1806,7 @@ struct kvm_x86_ops { void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); int (*gmem_prepare)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order); void (*gmem_invalidate)(kvm_pfn_t start, kvm_pfn_t end); + int (*gmem_max_level)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, u8 *max_level); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 1882096fba3e..21f44ec37b29 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4308,6 +4308,7 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { int max_order, r; + u8 max_level; if (!kvm_slot_can_be_private(fault->slot)) { kvm_mmu_prepare_memory_fault_exit(vcpu, fault); @@ -4321,8 +4322,15 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, return r; } - fault->max_level = min(kvm_max_level_for_order(max_order), - fault->max_level); + max_level = kvm_max_level_for_order(max_order); + r = static_call(kvm_x86_gmem_max_level)(vcpu->kvm, fault->pfn, + fault->gfn, &max_level); + if (r) { + kvm_release_pfn_clean(fault->pfn); + return r; + } + + fault->max_level = min(max_level, fault->max_level); fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY); return RET_PF_CONTINUE; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 85f63b6842b6..5eb836b73131 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -4315,3 +4315,30 @@ void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end) pfn += use_2m_update ? PTRS_PER_PMD : 1; } } + +int sev_gmem_max_level(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, u8 *max_level) +{ + int level, rc; + bool assigned; + + if (!sev_snp_guest(kvm)) + return 0; + + rc = snp_lookup_rmpentry(pfn, &assigned, &level); + if (rc) { + pr_err_ratelimited("SEV: RMP entry not found: GFN %llx PFN %llx level %d error %d\n", + gfn, pfn, level, rc); + return -ENOENT; + } + + if (!assigned) { + pr_err_ratelimited("SEV: RMP entry is not assigned: GFN %llx PFN %llx level %d\n", + gfn, pfn, level); + return -EINVAL; + } + + if (level < *max_level) + *max_level = level; + + return 0; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index f26b8c2a8be4..f745022f7454 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5067,6 +5067,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .alloc_apic_backing_page = svm_alloc_apic_backing_page, .gmem_prepare = sev_gmem_prepare, + .gmem_max_level = sev_gmem_max_level, .gmem_invalidate = sev_gmem_invalidate, }; -- 2.25.1