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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01678C4167B for ; Wed, 8 Nov 2023 09:11:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 741846B00D9; Wed, 8 Nov 2023 04:11:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6EE906B00DD; Wed, 8 Nov 2023 04:11:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 566A86B00DB; Wed, 8 Nov 2023 04:11:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 42C8A6B00D7 for ; Wed, 8 Nov 2023 04:11:51 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1E720160325 for ; Wed, 8 Nov 2023 09:11:51 +0000 (UTC) X-FDA: 81434219622.18.7CEC40A Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf22.hostedemail.com (Postfix) with ESMTP id 362CAC000F for ; Wed, 8 Nov 2023 09:11:48 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b="n5/hAEXf"; spf=pass (imf22.hostedemail.com: domain of jpiotrowski@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=jpiotrowski@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699434709; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fSNLSSBG95PkWpM5rxlYaA3C46U8XXsKOZIEDIAiboE=; b=k58eowD2P30ld958kjHrd4XRZBAdcpyT+gd4speNlfmY+N8F8FPZkYdsFQeCa6pXjKkkPF 5Z3tzIeeX0Z9K/Rx/cv2idbWiIhqhnBnVyqvkhJ/6haSxp5hOrD0wVQBpWUWhjRfktDm+N z6OLIQLA784N56n8Dk94KDMWs+Zje3U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699434709; a=rsa-sha256; cv=none; b=wB8Ko/Cr0C+hFdErpyVHRCjPmKv1Aa9fc6mc3zBHhCiJzwe7EcgUthfQeZjx3UD29RyH6G /gwQa7cV9ur7Pn/qWPert+h3Ioac2NZH4fecCynySz2cy2e7U4GRJm5awtnI3TuPOYCKKt mPQCHaSSj7QfVrIkAqkjbq5CRmePiEo= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b="n5/hAEXf"; spf=pass (imf22.hostedemail.com: domain of jpiotrowski@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=jpiotrowski@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com Received: from [IPV6:2a02:a420:40:b6ea:bd47:84b4:35b0:c3f1] (77-63-99-98.mobile.kpn.net [77.63.99.98]) by linux.microsoft.com (Postfix) with ESMTPSA id BC45120B74C0; Wed, 8 Nov 2023 01:11:37 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com BC45120B74C0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1699434708; bh=fSNLSSBG95PkWpM5rxlYaA3C46U8XXsKOZIEDIAiboE=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=n5/hAEXf2dzieMftcO0zozxDGKiOud7ouqdRWRPbYAjhcj9YDX3kLItujrlDhh2Px IumdNzPkTleQsQKkPXu9ABOj7og39wtcOiR1e4DDBJsrI7sD+nqGZ6ATkTCM5UjjoV P0i0NRIhvFLS18I+cPLvv7OxYeP4osdJV2s5dz78= Message-ID: Date: Wed, 8 Nov 2023 10:11:35 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v10 06/50] x86/sev: Add the host SEV-SNP initialization support Content-Language: en-US To: Borislav Petkov , "Kalra, Ashish" Cc: Michael Roth , kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, jroedel@suse.de, thomas.lendacky@amd.com, hpa@zytor.com, ardb@kernel.org, pbonzini@redhat.com, seanjc@google.com, vkuznets@redhat.com, jmattson@google.com, luto@kernel.org, dave.hansen@linux.intel.com, slp@redhat.com, pgonda@google.com, peterz@infradead.org, srinivas.pandruvada@linux.intel.com, rientjes@google.com, dovmurik@linux.ibm.com, tobin@ibm.com, vbabka@suse.cz, kirill@shutemov.name, ak@linux.intel.com, tony.luck@intel.com, marcorr@google.com, sathyanarayanan.kuppuswamy@linux.intel.com, alpergun@google.com, jarkko@kernel.org, nikunj.dadhania@amd.com, pankaj.gupta@amd.com, liam.merwick@oracle.com, zhi.a.wang@intel.com, Brijesh Singh References: <20231016132819.1002933-1-michael.roth@amd.com> <20231016132819.1002933-7-michael.roth@amd.com> <20231107163142.GAZUpmbt/i3himIf+E@fat_crate.local> <4a2016d6-dc1f-ff68-9827-0b72b7c8eac2@amd.com> <20231107191931.GCZUqNwxP8JcSbjZ0/@fat_crate.local> <20231107202757.GEZUqdzYyzVBHTBhZX@fat_crate.local> <250f5513-91c0-d0b5-cb59-439e26ba16dc@amd.com> <20231107212740.GFZUqrzK7yzy41dRKp@fat_crate.local> <20231107220852.GGZUq1dHJ2q9LYV2oG@fat_crate.local> <4b68fd05-5d21-0472-42c3-6cf6f1f9f967@amd.com> <20231108061413.GAZUsnNVcmYZNMw2Kr@fat_crate.local> From: Jeremi Piotrowski In-Reply-To: <20231108061413.GAZUsnNVcmYZNMw2Kr@fat_crate.local> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 362CAC000F X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: cpztbx3xobrbaue3q4qk4zhgwjeybuki X-HE-Tag: 1699434708-565612 X-HE-Meta: U2FsdGVkX1+zvXMhdnjJCMhqz3rpXGwbUwXxee+rRzah2FPG2AGsP2MhopiXDJU5z1QLin+kdWG8SjiuUUEUTqRuoTkbqo/GeEHkob/HSuomJrC6ldrEaGF82HCuICTiYWnP1aGChI1sDWS7SitkGB7yx39uhU0yj+/ktS8XIRA0hq+ARy/A4BgHW9vUO2VuyYor5xNAny+VUiOEaBZpjTDoaTjqrlOkDi3HSmoSmLjfeFva1M2hkDxT7n18Unb4SFVogin1iMeUshAlJYO7bx+PfBAkg0W8iysZC5vKtJe0sJkMnrp3cAnAVHK4eIcVAXIgNiYOPEMy3ofoFofwFzk2Ikcr4HE5yE34yxKjMUCffIlk9YdzPAByCRKYxTE9MMYNbu5BIXHAQ6VHPpPrQWIWjKwOffIKVULXScwbrRHRgHteUtoIlei8xbG5BrLf2fx87Yy5+sKmsB61VNh79DN3OgLo6syhi0/0gK6rQLDw9eSbAcEpRxcjOTgh4AZHeu9qEAl6zEa6ff941t/L/lIxs5ihz+TH2oEYT0UXf65OqmWBad7PaaRRlk2tNgpHdN06KzrSnROvxeNYiIo3aWeEEbUdyA8V3LfgcU7DZYucfyZcRHBKUJQfGW1CoRLLz89gHYaSecM17B96j7wcyMGqGtXSyweZb15n8m3Hg26CcXn/zCscD5pvg3eIpdhXjolfcYeFgouYp01G/+SOvk+jYJsdwxXFfas9fbbUZOwYkWPDjqhB8MDHURemiEO1FVwD3BQhk4KV9a0S49pW2PoBLA37W5E10tXPp5ktr1nmxKlmlxAds3Q2D/j+cBLdOarse7zGB/uSMeELQsBrCPXhVzWf7eIwk8kWLtQIGV44AnNLpvf4ZG6DtOotmiJqLLxMgd5Fn2HlcZzo12pCSJxOpvCJ5VoXlR0osmhN1n8dO6IiBFLrqcjcwsF7vLojNYegsbbkJHWiNawTRzM CgxcZkyZ fNq7PASwAmwqukonvLIm0hSswcV8hZHRBoyXyY9Lq1UVhz0fIemU8kTs64q2YjNyJ9Y4qKglXoCJ/3fYZqVAN6AUKEyq9H4Kg017gQJ5LZrYx981K+FP7EMXmOMXiVN/nGpmuTcC8RGm7MgOhr1xH9/bmtawZZ99ubbNFWU0Grf28VNyOUIfEfJM2eUYVVEAkStup6VsV9njRYUJueW5EZNHGNT+/T/NpecjIYMViY48kSsxPo2QIh4uiNLsYarOyeoohLc3Uk57oJwSmLTFNhT78AmqLIml5h4eAMg8JmGghFdXMQk8EJtNuW2HxbpUB8EAkSWzp2t3UKXQkuJpoxZk0p7YpGePcEMYhjghHdpkfKgjVoCT9pM1pCQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 08/11/2023 07:14, Borislav Petkov wrote: > On Tue, Nov 07, 2023 at 04:33:41PM -0600, Kalra, Ashish wrote: >> We will still need some method to tell the IOMMU driver if SNP >> support/feature is disabled by this function, for example, when CPU family >> and model is not supported by SNP and we jump to no_snp label. > > See below. > >> The reliable way for this to work is to ensure snp_rmptable_init() is called >> before IOMMU initialization and then IOMMU initialization depends on SNP >> feature flag setup by snp_rmptable_init() to enable SNP support on IOMMU or >> not. > > Yes, this whole SNP initialization needs to be reworked and split this > way: I agree with Borislav and have some comments of my own. > > - early detection work which needs to be done once goes to > bsp_init_amd(): that's basically your early_detect_mem_encrypt() stuff > which needs to happen exactly only once and early. > > - Any work like: > > c->x86_phys_bits -= (cpuid_ebx(0x8000001f) >> 6) & 0x3f; > > and the like which needs to happen on each AP, gets put in a function > which gets called by init_amd(). > > By the time IOMMU gets to init, you already know whether it should > enable SNP and check X86_FEATURE_SEV_SNP. This flow would suit me better too. In SNP-host capable Hyper-V VMs there is no IOMMU and I've had to resort to early return from amd_iommu_snp_enable() to prevent it from disabling SNP [1]. In addition to what Borislav posted, you'd just need to enforce that if IOMMU is detected it actually gets enabled. [1]: https://lore.kernel.org/lkml/20230213103402.1189285-6-jpiotrowski@linux.microsoft.com/ > > Finally, you call __snp_rmptable_init() which does the *per-CPU* init > work which is still pending. Yes please, and the only rmp thing left to do per-CPU would be to check that the MSRs are set the same as the value read from CPU0. Running the early_rmptable_check() from early_detect_mem_encrypt() and on every CPU makes it difficult to support a kernel allocated RMP table. If you look at what I did for the mentioned Hyper-V SNP-host VMs [2] (which I think is reasonable) the RMP table is allocated in init_mem_mapping() and the addresses are propagated to other CPUs through hv_cpu_init(), which is called from cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, ...). So it would be great if any init works plays nice with cpu hotplug notifiers. [2]: https://lore.kernel.org/lkml/20230213103402.1189285-2-jpiotrowski@linux.microsoft.com/ Thanks, Jeremi > > Ok? > > Ontop of the previous ontop patch: > > --- > diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c > index 6cc2074fcea3..a9c95e5d6b06 100644 > --- a/arch/x86/kernel/cpu/amd.c > +++ b/arch/x86/kernel/cpu/amd.c > @@ -674,8 +674,19 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) > if (!(msr & MSR_K7_HWCR_SMMLOCK)) > goto clear_sev; > > - if (cpu_has(c, X86_FEATURE_SEV_SNP) && !early_rmptable_check()) > - goto clear_snp; > + if (cpu_has(c, X86_FEATURE_SEV_SNP)) { > + /* > + * RMP table entry format is not architectural and it can vary by processor > + * and is defined by the per-processor PPR. Restrict SNP support on the known > + * CPU model and family for which the RMP table entry format is currently > + * defined for. > + */ > + if (c->x86 != 0x19 || c->x86_model > 0xaf) > + goto clear_snp; > + > + if (!early_rmptable_check()) > + goto clear_snp; > + } > > return; > > diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c > index 9237c327ad6d..5a71df9ae4cb 100644 > --- a/arch/x86/virt/svm/sev.c > +++ b/arch/x86/virt/svm/sev.c > @@ -199,14 +199,6 @@ static int __init snp_rmptable_init(void) > if (!amd_iommu_snp_en) > return 0; > > - /* > - * RMP table entry format is not architectural and it can vary by processor and > - * is defined by the per-processor PPR. Restrict SNP support on the known CPU > - * model and family for which the RMP table entry format is currently defined for. > - */ > - if (boot_cpu_data.x86 != 0x19 || boot_cpu_data.x86_model > 0xaf) > - goto nosnp; > - > if (__snp_rmptable_init()) > goto nosnp; >