From patchwork Tue Jun 6 09:56:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 103701 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3278640vqr; Tue, 6 Jun 2023 03:06:56 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4xr6/5Z3vTGaOeWexo1ej5KkvX3wxoTC0GeDyB8+Edmo4jTWjhWaFNw2Fd2yucexJbmc74 X-Received: by 2002:a05:6a21:6d90:b0:10a:99a9:cb4b with SMTP id wl16-20020a056a216d9000b0010a99a9cb4bmr2561809pzb.12.1686046015981; Tue, 06 Jun 2023 03:06:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686046015; cv=none; d=google.com; s=arc-20160816; b=epQ7jbxXZ7y/GsyMgd7EzWlPzqQJyq3ePdighQPLgK6lQu/8dlyBnLYLYxeVJeez6+ nsjetwXQw0fSIXHLxn6q78H2dPJXy3M0TflUEU1bD1V8K3luZnwdZbg+CBPxoYcL0Qst U+LN6lKNA7UUytbr4AsKK6kAYwZvUk4xgkoC/8gEKA4vcEMBV74ElMzzauxBlb1/lRfa d4Hx2WqXvkeJOCT9s/TykjNDRakGPm4nFpaSPQ6dR3R4ZZkTB5YYptvKU4BhiLn/j2ev qzbNpCG01mXiYSjqBZVoOOIYTY6Aha6psk54hAI/QZHPYPQYHC8NWkIsa2GJkOcO7sDx ov4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OrVdZwr7JFbZNG8SMIU1Wl3EdSqWdVwvHoCBrcwPbwk=; b=pm/vkTwQTPVDvP3VMcfWOrZt17aSTQXMOWzjL383ynqTQfBzqbyKiuS96V+2gzrL7c 9gAVaBJuz8VXi+QTc4Sx8gTUr7WZnyHuMqej9zdZWj/tIaKT6SNkGlseWbE3EE/1WHWD hU6tkuBS9+ixmIN/BbiPkefKeOO+TUQvAZuVIzYp9kSd5bd7WT9hYFggbyddI6yFXboZ rrKyEYFGrc75jb/mz1ZGglKU4mdHsNzkhn/ww1p/uOPUSIDnv5FD5NMdxj96JIk1ESn5 fHjSJtjiOPMU6+8mWFIV3CiebF9B0fEzFlBSRymHIAiRtW1ZWbgrF3+eK8K0p+4q4eEF PV+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="RTn9U/Az"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t62-20020a638141000000b0053f8327e0e3si7062225pgd.668.2023.06.06.03.06.43; Tue, 06 Jun 2023 03:06:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="RTn9U/Az"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237022AbjFFJ4j (ORCPT + 99 others); Tue, 6 Jun 2023 05:56:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236904AbjFFJ4f (ORCPT ); Tue, 6 Jun 2023 05:56:35 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 999E4E55 for ; Tue, 6 Jun 2023 02:56:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686045392; x=1717581392; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=lsSlrIZGg5tq5EdHS+s864ysGMsAzauBzAwak0CSiJ0=; b=RTn9U/AzAh71uZSMf6DhJoSYWTXFI6wgJllrBTXbo9aofhD8xqCO18xW GqCdQlTdMG/FLw2U7sKTLqnmvoEmiJVrjz66EtMeYjwMBzbcQtA//81TL nfEbjNEeismNafVumsotTvY9tT3X8SoOl0pDaoi93ZBBS/xQIbkDxSJ9Q yKWxm+Z0HHm9MgRxO1/f4Z2xdqWkIC8+iP8d/V20RNfHsHG7oaE8ZuNQF 9NbAtzRDs9HhEZoHTiEP8vszy1psEMwHuFWrDb1/wqmbX0grm7wl4iCeD oOZz9jishd7jUX/qjTd7UdmWspv1tQeXjanBDypg+RNvgdJV4t2gm6Yq+ w==; X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="336249099" X-IronPort-AV: E=Sophos;i="6.00,219,1681196400"; d="scan'208";a="336249099" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Jun 2023 02:56:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="853348527" X-IronPort-AV: E=Sophos;i="6.00,219,1681196400"; d="scan'208";a="853348527" Received: from rgraefe-mobl1.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.58.173]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Jun 2023 02:56:29 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id A64D710CFD2; Tue, 6 Jun 2023 12:56:26 +0300 (+03) From: "Kirill A. Shutemov" To: dave.hansen@intel.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de Cc: decui@microsoft.com, rick.p.edgecombe@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, seanjc@google.com, thomas.lendacky@amd.com, x86@kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv3 1/3] x86/mm: Allow guest.enc_status_change_prepare() to fail Date: Tue, 6 Jun 2023 12:56:20 +0300 Message-Id: <20230606095622.1939-2-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230606095622.1939-1-kirill.shutemov@linux.intel.com> References: <20230606095622.1939-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_PASS, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767947386962737406?= X-GMAIL-MSGID: =?utf-8?q?1767947386962737406?= TDX code is going to provide guest.enc_status_change_prepare() that is able to fail. TDX will use the call to convert the GPA range from shared to private. This operation can fail. Add a way to return an error from the callback. Signed-off-by: Kirill A. Shutemov Reviewed-by: Kuppuswamy Sathyanarayanan --- arch/x86/include/asm/x86_init.h | 2 +- arch/x86/kernel/x86_init.c | 2 +- arch/x86/mm/mem_encrypt_amd.c | 4 +++- arch/x86/mm/pat/set_memory.c | 3 ++- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/x86_init.h b/arch/x86/include/asm/x86_init.h index 88085f369ff6..1ca9701917c5 100644 --- a/arch/x86/include/asm/x86_init.h +++ b/arch/x86/include/asm/x86_init.h @@ -150,7 +150,7 @@ struct x86_init_acpi { * @enc_cache_flush_required Returns true if a cache flush is needed before changing page encryption status */ struct x86_guest { - void (*enc_status_change_prepare)(unsigned long vaddr, int npages, bool enc); + bool (*enc_status_change_prepare)(unsigned long vaddr, int npages, bool enc); bool (*enc_status_change_finish)(unsigned long vaddr, int npages, bool enc); bool (*enc_tlb_flush_required)(bool enc); bool (*enc_cache_flush_required)(void); diff --git a/arch/x86/kernel/x86_init.c b/arch/x86/kernel/x86_init.c index d82f4fa2f1bf..f230d4d7d8eb 100644 --- a/arch/x86/kernel/x86_init.c +++ b/arch/x86/kernel/x86_init.c @@ -130,7 +130,7 @@ struct x86_cpuinit_ops x86_cpuinit = { static void default_nmi_init(void) { }; -static void enc_status_change_prepare_noop(unsigned long vaddr, int npages, bool enc) { } +static bool enc_status_change_prepare_noop(unsigned long vaddr, int npages, bool enc) { return true; } static bool enc_status_change_finish_noop(unsigned long vaddr, int npages, bool enc) { return false; } static bool enc_tlb_flush_required_noop(bool enc) { return false; } static bool enc_cache_flush_required_noop(void) { return false; } diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c index e0b51c09109f..4f95c449a406 100644 --- a/arch/x86/mm/mem_encrypt_amd.c +++ b/arch/x86/mm/mem_encrypt_amd.c @@ -319,7 +319,7 @@ static void enc_dec_hypercall(unsigned long vaddr, int npages, bool enc) #endif } -static void amd_enc_status_change_prepare(unsigned long vaddr, int npages, bool enc) +static bool amd_enc_status_change_prepare(unsigned long vaddr, int npages, bool enc) { /* * To maintain the security guarantees of SEV-SNP guests, make sure @@ -327,6 +327,8 @@ static void amd_enc_status_change_prepare(unsigned long vaddr, int npages, bool */ if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP) && !enc) snp_set_memory_shared(vaddr, npages); + + return true; } /* Return true unconditionally: return value doesn't matter for the SEV side */ diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 7159cf787613..b8f48ebe753c 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2151,7 +2151,8 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc) cpa_flush(&cpa, x86_platform.guest.enc_cache_flush_required()); /* Notify hypervisor that we are about to set/clr encryption attribute. */ - x86_platform.guest.enc_status_change_prepare(addr, numpages, enc); + if (!x86_platform.guest.enc_status_change_prepare(addr, numpages, enc)) + return -EIO; ret = __change_page_attr_set_clr(&cpa, 1); From patchwork Tue Jun 6 09:56:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 103702 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3278870vqr; Tue, 6 Jun 2023 03:07:24 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5L56Qbyok6H/4aTfyZa/LSYJvE/6TgTDWIzHQUWKLY2hgiRyEJy8btr88g9TTQ3fAnmh0F X-Received: by 2002:a05:6359:2e9f:b0:125:83a6:caa5 with SMTP id rp31-20020a0563592e9f00b0012583a6caa5mr2369887rwb.3.1686046044533; Tue, 06 Jun 2023 03:07:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686046044; cv=none; d=google.com; s=arc-20160816; b=u4tHJwkYWqBgAnHwX63PVU8blglS2Ysi+4dsOilZtSxHIWEge97bj0dgkZNsTBq1Uq AaL3xLxJB3m7TPhzXvotGr4CxOld2c/jHciSRJf6DtHX3bw7Mf6kY4u0Bi6S9ti0Kfxg I0gtdX/FYG5PsscqRWU9sRFi1P0qz10tpuBM4XDgrRbHAQfVldTswOUi0CodtOsAQgRh oxC3+dDstJ0Ncc01PI7958frWMXyXG67gSFcBR3c1vnB0+YESEYh/86wj4uIhT0xIEfF /a53Im+LjJlF+ziQdIgg0Q5jAKnEA9VfRZswpWG1wJPbnarx9Qwj0utQbTL/lgq5gamx ZliA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=RKyLtInM9xkQMcxdlhjJHh4pPvd9swjjqqaLnuUhyHs=; b=hyUKysgtrJ7g1rPJ9tSwVGuzn5ftAcESuDeWuwdCrzVsYKhIkH5DF7ox8kxctIb2vs DioMDtByKGAS4Fbz9ybu+4tVXsD+foDMS7C65bkH6SJ15GuD/5hX7AQoi2gJuU5iYazN hibEdKpaBRbM1rj4QCBVTsbtahaOJyMAX/qR8bvDHr9nNy/I6R0ynguGJBI7i61r0Juv Sr26549Il2xJdnRd5hmjtQNVns0kl1fC/0BtY4osDrboJIj4mQ2NgiZEJGGjLdkL6hMS +ZadiX5+7eFwLUvog7gQTL1YODcfvMNdXqbUkEP8IBvZ8vfSzXxi//fW5Lg8qwoadlJ0 3suA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ciV7fRbr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o14-20020a17090ac70e00b002402275fc56si7158830pjt.118.2023.06.06.03.07.12; Tue, 06 Jun 2023 03:07:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ciV7fRbr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237509AbjFFJ4q (ORCPT + 99 others); Tue, 6 Jun 2023 05:56:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236148AbjFFJ4f (ORCPT ); Tue, 6 Jun 2023 05:56:35 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73565E73 for ; Tue, 6 Jun 2023 02:56:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686045394; x=1717581394; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7RLFRqvdRV4EeGWD2/bOkXceiG/5lKBnbgdL4IoAVVE=; b=ciV7fRbrLWFPhM/KEYth/Vr28YZKwzcAPL6k7UWq14J93o9Zl0Ekpo4C Xwbxvv4V1/gyZ0cxgJNBC4j9x/ZNBrYtUbvygNIrRFt0spYfMXFQ9GZJv uNumEtDdLWp+q2Ua0igR8cV+2PBw1NLiEUw4tbA7oopeeXq7zF10l+z52 85x8ljd3socjSIK8QheAdwvUyJ7RpEM0Lt9PDtVto/IiW8xHje67b6uok jtaUG1j7515hFrXqsv51mJ1dHrxKUYmocUnWb0G1AK4wDZDPLjgEgYHt2 DPu6fD17W2tzuSijvfVEC8TjB9uOnirNgDvYWm6kSaJhVHz3USnVfYt26 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="422445929" X-IronPort-AV: E=Sophos;i="6.00,219,1681196400"; d="scan'208";a="422445929" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Jun 2023 02:56:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="738720781" X-IronPort-AV: E=Sophos;i="6.00,219,1681196400"; d="scan'208";a="738720781" Received: from rgraefe-mobl1.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.58.173]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Jun 2023 02:56:29 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id B154A10D02A; Tue, 6 Jun 2023 12:56:26 +0300 (+03) From: "Kirill A. Shutemov" To: dave.hansen@intel.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de Cc: decui@microsoft.com, rick.p.edgecombe@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, seanjc@google.com, thomas.lendacky@amd.com, x86@kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv3 2/3] x86/tdx: Fix race between set_memory_encrypted() and load_unaligned_zeropad() Date: Tue, 6 Jun 2023 12:56:21 +0300 Message-Id: <20230606095622.1939-3-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230606095622.1939-1-kirill.shutemov@linux.intel.com> References: <20230606095622.1939-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767947417257902691?= X-GMAIL-MSGID: =?utf-8?q?1767947417257902691?= Touching privately mapped GPA that is not properly converted to private with MapGPA and accepted leads to an unrecoverable exit to VMM. load_unaligned_zeropad() can touch memory that is not owned by the caller, but just happened to next after the owned memory. This load_unaligned_zeropad() behaviour makes it important when kernel asks VMM to convert a GPA from shared to private or back. Kernel must never have a page mapped into direct mapping (and aliases) as private when the GPA is already converted to shared or when GPA is not yet converted to private. load_unaligned_zeropad() can touch memory that is not owned by the caller, but just happens to be next after the owned memory. This load_unaligned_zeropad() behavior makes it important when the kernel asks VMM to convert a GPA from shared to private or back. The kernel must never have a page mapped into direct mapping (and aliases) as private when the GPA is already converted to shared or when the GPA is not yet converted to private. guest.enc_status_change_prepare() is called before adjusting direct mapping and therefore is responsible for converting the memory to private. guest.enc_status_change_finish() is called after adjusting direct mapping and it converts the memory to shared. It is okay to have a shared mapping of memory that is not properly converted. handle_mmio() knows how to deal with load_unaligned_zeropad() stepping on it. Signed-off-by: Kirill A. Shutemov Fixes: 7dbde7631629 ("x86/mm/cpa: Add support for TDX shared memory") Reviewed-by: Kuppuswamy Sathyanarayanan --- arch/x86/coco/tdx/tdx.c | 64 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index e146b599260f..f6213a10de3a 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -840,6 +840,30 @@ static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc) return true; } +static bool tdx_enc_status_change_prepare(unsigned long vaddr, int numpages, + bool enc) +{ + /* + * Only handle shared->private conversion here. + * See the comment in tdx_early_init(). + */ + if (enc) + return tdx_enc_status_changed(vaddr, numpages, enc); + return true; +} + +static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages, + bool enc) +{ + /* + * Only handle private->shared conversion here. + * See the comment in tdx_early_init(). + */ + if (!enc) + return tdx_enc_status_changed(vaddr, numpages, enc); + return true; +} + void __init tdx_early_init(void) { u64 cc_mask; @@ -867,9 +891,43 @@ void __init tdx_early_init(void) */ physical_mask &= cc_mask - 1; - x86_platform.guest.enc_cache_flush_required = tdx_cache_flush_required; - x86_platform.guest.enc_tlb_flush_required = tdx_tlb_flush_required; - x86_platform.guest.enc_status_change_finish = tdx_enc_status_changed; + /* + * Touching privately mapped GPA that is not properly converted to + * private with MapGPA and accepted leads to an unrecoverable exit + * to VMM. + * + * load_unaligned_zeropad() can touch memory that is not owned by the + * caller, but just happened to next after the owned memory. + * This load_unaligned_zeropad() behaviour makes it important when + * kernel asks VMM to convert a GPA from shared to private or back. + * Kernel must never have a page mapped into direct mapping (and + * aliases) as private when the GPA is already converted to shared or + * when GPA is not yet converted to private. + * + * load_unaligned_zeropad() can touch memory that is not owned by the + * caller, but just happens to be next after the owned memory. This + * load_unaligned_zeropad() behavior makes it important when the kernel + * asks VMM to convert a GPA from shared to private or back. The kernel + * must never have a page mapped into direct mapping (and aliases) as + * private when the GPA is already converted to shared or when the GPA + * is not yet converted to private. + * + * guest.enc_status_change_prepare() is called before adjusting direct + * mapping and therefore is responsible for converting the memory to + * private. + * + * guest.enc_status_change_finish() is called after adjusting direct + * mapping and it converts the memory to shared. + * + * It is okay to have a shared mapping of memory that is not properly + * converted. handle_mmio() knows how to deal with + * load_unaligned_zeropad() stepping on it. + */ + x86_platform.guest.enc_status_change_prepare = tdx_enc_status_change_prepare; + x86_platform.guest.enc_status_change_finish = tdx_enc_status_change_finish; + + x86_platform.guest.enc_cache_flush_required = tdx_cache_flush_required; + x86_platform.guest.enc_tlb_flush_required = tdx_tlb_flush_required; pr_info("Guest detected\n"); } From patchwork Tue Jun 6 09:56:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 103700 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3278597vqr; Tue, 6 Jun 2023 03:06:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4GuzzVuYzmzvoyhOUROuy3jI5LYsiOXCKa5GjukoBXd+ZnrLwCsJew0n+/JN+grQqFdNjm X-Received: by 2002:a05:620a:491a:b0:75e:cda3:16ab with SMTP id ed26-20020a05620a491a00b0075ecda316abmr824943qkb.33.1686046012077; Tue, 06 Jun 2023 03:06:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686046012; cv=none; d=google.com; s=arc-20160816; b=lLkTe1kVPj4Fd1elQrpvpWgsOPiZ17/JlEvgcjnPMsDbGZb1lauAOOwVSJfLToTNNC bNe4xsl3gsxFz0JsJ7xTKkTkevpDEjOThztTcTb7TsHbq1zXnAB2kukKxsAHoZfMTcz6 jgDCoX5b1nE//rRgkKLthNIKiFoQ6/iQv+0eq1W6D7/l66tvLes6hH23cSKNmekGgbR2 sNKzJK+QrdelmGM4+/5+5S8zRcDNaZde9mAQFnCV8/3B/7cNgIUaIsaTnr7y/Vo0cDLY ML23aK4RDS7bZPxRBhCH4R8TZg4KlNDrA6ytOJiEQRxQm6F3yoWQGKO+Amo9zncvLtvA IjhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tKLRM3caoZvBKL3YmQ7NiEoTe3Jf7IorLEJnq6ZTj9c=; b=L3H10psM4uXDZ7pjvM2M3gDACpcKZ7idYIpA4I+A5sUtxoPmrB/rJ9t3x81B57fQxe W2RHmQK1y0/8hQBUZL6xCWQvBnJb2j0V6/L4ZLqeyStm/VTRhkFkIfX9DV1HfrpiYpHd THzO8Ozt5s7FeUwqgo9TtGd86DrFzlU500MXlD1trk0cusYJ5jet7knMqYuLddlciMMg e0Fi6iNHEbU34jWmlaEjcJw8j2xoUJa5QM1WATP/GVL372t+J2E6izXkewSu3JUh4Zx6 FbSbXs8kXPgsy38/esw4OAxusB8foHbF28fgMLrp51TQBGm/pqNeFnHAi8Hp9VDfLHPR cOrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=X72g7Mnf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x11-20020ae9f80b000000b0074e186623a1si5673952qkh.23.2023.06.06.03.06.37; Tue, 06 Jun 2023 03:06:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=X72g7Mnf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234154AbjFFJ4h (ORCPT + 99 others); Tue, 6 Jun 2023 05:56:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237308AbjFFJ4f (ORCPT ); Tue, 6 Jun 2023 05:56:35 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27C63E6B for ; Tue, 6 Jun 2023 02:56:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686045393; x=1717581393; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9GElKVgLMZ9LqY2JLMC1Sd5gav9lSYApUQe9KiZ8CuU=; b=X72g7MnftcVRKdd1dpn26C7XfUdg8Yz129ZznK8T3MnkFZFmFGFSEctV 3gLrNiBYofD1LXQoVenbuv0YrsitGT028Abeb6HNrxeFQdmBXZc9DrzlI yr0p62eD28coTZYprC7uHc73uerCtleN7V0naQ3LZ6Havucb93WXUr1OP LY+3tT244hPxwWaWw8qY43P3h8wTvuqwmmdv9jste6iBb5AJM0A3BPruZ oekT8NkZpVE100EYqGSJQWsqAI9pk+uJ6Wv4pICB+8ViSLms1lTq05BOe 7pEvRjLsAkP9bfHSba2Ap1XU6/AjiJIj2FMC92RdQptoL+Wx4MPYHBjSE w==; X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="422445921" X-IronPort-AV: E=Sophos;i="6.00,219,1681196400"; d="scan'208";a="422445921" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Jun 2023 02:56:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="738720779" X-IronPort-AV: E=Sophos;i="6.00,219,1681196400"; d="scan'208";a="738720779" Received: from rgraefe-mobl1.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.58.173]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Jun 2023 02:56:29 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id BAACC10D348; Tue, 6 Jun 2023 12:56:26 +0300 (+03) From: "Kirill A. Shutemov" To: dave.hansen@intel.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de Cc: decui@microsoft.com, rick.p.edgecombe@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, seanjc@google.com, thomas.lendacky@amd.com, x86@kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv3 3/3] x86/mm: Fix enc_status_change_finish_noop() Date: Tue, 6 Jun 2023 12:56:22 +0300 Message-Id: <20230606095622.1939-4-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230606095622.1939-1-kirill.shutemov@linux.intel.com> References: <20230606095622.1939-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767947383050392048?= X-GMAIL-MSGID: =?utf-8?q?1767947383050392048?= enc_status_change_finish_noop() is now defined as always-fail, which doesn't make sense for noop. The change has no user-visible effect because it is only called if the platform has CC_ATTR_MEM_ENCRYPT. All platforms with the attribute override the callback with their own implementation. Signed-off-by: Kirill A. Shutemov Reviewed-by: Kuppuswamy Sathyanarayanan --- arch/x86/kernel/x86_init.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kernel/x86_init.c b/arch/x86/kernel/x86_init.c index f230d4d7d8eb..64664311ac2b 100644 --- a/arch/x86/kernel/x86_init.c +++ b/arch/x86/kernel/x86_init.c @@ -131,7 +131,7 @@ struct x86_cpuinit_ops x86_cpuinit = { static void default_nmi_init(void) { }; static bool enc_status_change_prepare_noop(unsigned long vaddr, int npages, bool enc) { return true; } -static bool enc_status_change_finish_noop(unsigned long vaddr, int npages, bool enc) { return false; } +static bool enc_status_change_finish_noop(unsigned long vaddr, int npages, bool enc) { return true; } static bool enc_tlb_flush_required_noop(bool enc) { return false; } static bool enc_cache_flush_required_noop(void) { return false; } static bool is_private_mmio_noop(u64 addr) {return false; }