From patchwork Fri May 26 12:02:23 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: 99467 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp425099vqr; Fri, 26 May 2023 05:16:39 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ478gqoFjrrBS9XBLu537oiSlm+EzhNNLslox3rs+KS5sA6f3x/qA8koqX2HPj/0CkhernH X-Received: by 2002:a05:6a00:1587:b0:643:9e7c:3829 with SMTP id u7-20020a056a00158700b006439e7c3829mr3241971pfk.12.1685103399542; Fri, 26 May 2023 05:16:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685103399; cv=none; d=google.com; s=arc-20160816; b=z5fE5igPlPfMtytIZLaaZM56wbDwpDi+UxD+2EciYLEQHeFkXNeRnFtON6Wh9ObZ/r J/Ywe4o2Irk+3OaWiCdtcGUrrb3rVTaqzAAsKzoBYOAQf+uXzqRabfat2TPWhSRg3Jsw k1t4P2yXaSniuXnveuiggieP1BaPLX8UeidMQJZqEglC1fu+H3nFHVqfUzPch+WKUCoz 1Wqi6GTD2kRyxSVrIbFlvRVR8TTrS7eed88TGkgCUoEF07HkYx9PoP6PDLxdEw7madU8 yZAtSV/wcrQ/ivBD4rGVBOltRtNFIUuyE9Z48S1AxE5Iav3bsQR7J7XkRkOA6E/gaUZ1 WucA== 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=0+zh0CuFhvTQ11U6R5sIsopN+2T6mfZXZJAsKUWsWF8=; b=yoMfLo6AF45B1F+AubugVuGUYgZWC00iKRId9XPAlpUJUeaf/9/Ie4inHnbQaufrQ+ OTdUfu18awmuLqDhfIJ2aCZdbpivFiPaifaWZ9IE6oE2f8P96bP/GdI9PV1JAiSDSX57 wqfWmz/d+a5ZeMmhD2umeFRRfSF/Yhg4d9PkmdaM3qFKeKRl9OGAJhb7DfyCYGsFzWox NI6g/KNXFML1jFr+a8PtMv6hzuANlgdMWl97N1HiFlN9GsQk5xnF8VUj/hNN5gutFCF0 5iYVEuGkh0kKQT3sKEcUrRonKA9u9LMdfFj1WWTmU2+AF4OD+axiY92gocUwUxpdqi8Z DTww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=AoUOuX9g; 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 s187-20020a625ec4000000b005e1cabb612fsi3944170pfb.67.2023.05.26.05.16.25; Fri, 26 May 2023 05:16:39 -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=AoUOuX9g; 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 S237092AbjEZMCn (ORCPT + 99 others); Fri, 26 May 2023 08:02:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231815AbjEZMCl (ORCPT ); Fri, 26 May 2023 08:02:41 -0400 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 215ED195; Fri, 26 May 2023 05:02:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685102559; x=1716638559; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OOmH2mGuJmVEQEJwVjzFq4ACkBIsPCefRb15KF/4X2c=; b=AoUOuX9guL1F/FDrdR07dJN4agdq+CERT1uSVJ14jvnTWVpECZOwUM1Y 9/r6dI6s+xTTiwXLJhyXvcP3g5BgY8ecGLeMKuDbJHXXTy62pQ0k17epr uJCAAK4fOaT+0JKnsmqY1GAkKvNx/DaFb7y31fkTKt82UbEBiN4F2spTr kagr5cp8PV2jHdjRxLrF8wnpgYX9QMTKgTfYYsCT9RYayFTsWykG46vIY Hah0XRtW/eHLQ3iBLgNNhu8dMHP+oIYXj9s37PORw0KuWdoy61FQW6B01 +E+uproGctWuhXl7Gj3CVcjcslYA44PSvBc5J3XQOqxAjBk6JtQ8qw+Jg A==; X-IronPort-AV: E=McAfee;i="6600,9927,10721"; a="356568398" X-IronPort-AV: E=Sophos;i="6.00,194,1681196400"; d="scan'208";a="356568398" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2023 05:02:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10721"; a="817513633" X-IronPort-AV: E=Sophos;i="6.00,194,1681196400"; d="scan'208";a="817513633" Received: from fgarrona-mobl.ger.corp.intel.com (HELO box.shutemov.name) ([10.251.208.169]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2023 05:02:34 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 9813010DC16; Fri, 26 May 2023 15:02:31 +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" , stable@vger.kernel.org Subject: [PATCHv2 1/3] x86/mm: Allow guest.enc_status_change_prepare() to fail Date: Fri, 26 May 2023 15:02:23 +0300 Message-Id: <20230526120225.31936-2-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230526120225.31936-1-kirill.shutemov@linux.intel.com> References: <20230526120225.31936-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, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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?1766958982279229552?= X-GMAIL-MSGID: =?utf-8?q?1766958982279229552?= 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 Cc: stable@vger.kernel.org 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 Fri May 26 12:02:24 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: 99466 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp422786vqr; Fri, 26 May 2023 05:13:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6tvpkEvm0cxh6H1L7dTaUdVLQFDmK2pCHyambr7gEs6n8RI7se1hq/88sw6lM/MtL5scD9 X-Received: by 2002:a05:6a20:7350:b0:10f:6529:543b with SMTP id v16-20020a056a20735000b0010f6529543bmr2468516pzc.3.1685103194691; Fri, 26 May 2023 05:13:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685103194; cv=none; d=google.com; s=arc-20160816; b=KirUn0fw+RfLt3kCj/+621nwiwfn/ssHJu5XRb98hWDamlkMzIa6+v0gNQ397oGoN1 Kt42y3y5fGHfd0pUcI0jULO55kWXF4kAbYJlyCpSKC4WxNPLXGdBsgcWvPEaSk+Nlb12 gWhGDMiO9tF48mVZ62wYyMQfH41hOzVIRY2rayBmVplALo0Z8AcgHpfKbft7+Ak8BJBJ tURmfjy4CIVY8nB7AMC6Eok/JNUene/YCgdI+XsKyHENwKfbZdkEy3yNyNSffaJqDIJx 5DJr+EkVNAoQ9Y44rottZGE5luHV1f55FvYfELWuTCFPd0JWqlkUwpOkzHMiSa+h5+JO xzwQ== 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=Xhf5WcGABMs7mmpnznluCBlSm8NKMmeeWNE3BOkq6ls=; b=VfL6wvmHr6582Ksk2lFCrd9oxR/VwWCmcEMhlc5Qnowm310IqJLNB3v7UcKRCNu5T2 IL7xv+hTTsTn3lnWX2CzfFu0mhoWZDoDIaSd4pQiwJfnLLnoHTpabm5vKvYLUp2cVS9k +3b9trrIKrBzgznpztlFclQ3Rj4n7TpnaWa+H8DSIM5BXSn3DCK7LMJwD1K+OuFinebQ Y1B1ZR9U8eoMI94qo9CSzdUjfTGNCidXcMO6DE/igncacbMOpwFvDFG3HNlCKzjVNkmy pni9je6QKLWDYLknGBWsaKfBsTIomlOKaRQuJsIVoD4FJ4khwGVb2viFiR0rPGlEgrCs wS9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JwmBl1k4; 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 7-20020a621807000000b0063d45a6d0e5si2293986pfy.17.2023.05.26.05.13.02; Fri, 26 May 2023 05:13:14 -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=JwmBl1k4; 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 S231447AbjEZMDR (ORCPT + 99 others); Fri, 26 May 2023 08:03:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231679AbjEZMDO (ORCPT ); Fri, 26 May 2023 08:03:14 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D2DFE46; Fri, 26 May 2023 05:02:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685102579; x=1716638579; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rT7cwEP37q2mK0/mhhF1SbjEW1zoGpi7jl7FkWAvvT4=; b=JwmBl1k464CcRfIFxUknh6/UqKNaTJZoAOxw/kF+yZ8hT5Zn3DNx+7jv Csb8x0YI5kFU/swerh+pdXuyRMYuFnX4zSLx55sLOHnluzVCAPQXW/gLc iCRaw1mcr2xxlecRqIu7lD5H2YvGYP38mvIYDY5uGpHqCkCRI5ywca0bi SdttbM3FdD3fJO6WzxrZ2rar8eRjYYZ+1Llp1uhlQz95q9UUWT2TXSjn9 YWhl1O1kcI+EqusRgdxJaFvdkNQEY+YhXjZgMf5drrjQYTyKWh+AxwRlU oLzfqm655ljtwyNxgMC/SD1ORwjDeUNRiQZvx0txeK7BNdbJYHfkCZ6u1 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10721"; a="354201705" X-IronPort-AV: E=Sophos;i="6.00,194,1681196400"; d="scan'208";a="354201705" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2023 05:02:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10721"; a="735987427" X-IronPort-AV: E=Sophos;i="6.00,194,1681196400"; d="scan'208";a="735987427" Received: from fgarrona-mobl.ger.corp.intel.com (HELO box.shutemov.name) ([10.251.208.169]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2023 05:02:34 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 3C48510DC17; Fri, 26 May 2023 15:02:32 +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" , stable@vger.kernel.org Subject: [PATCHv2 2/3] x86/tdx: Fix race between set_memory_encrypted() and load_unaligned_zeropad() Date: Fri, 26 May 2023 15:02:24 +0300 Message-Id: <20230526120225.31936-3-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230526120225.31936-1-kirill.shutemov@linux.intel.com> References: <20230526120225.31936-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?1766958767741341171?= X-GMAIL-MSGID: =?utf-8?q?1766958767741341171?= Touching privately mapped GPA that is not properly converted to private with MapGPA and accepted leads to 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. guest.enc_status_change_prepare() called before adjusting direct mapping and therefore it is responsible for converting the memory to private. guest.enc_status_change_finish() 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 converted properly. 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") Cc: stable@vger.kernel.org Reviewed-by: Kuppuswamy Sathyanarayanan --- arch/x86/coco/tdx/tdx.c | 56 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index e146b599260f..59cc13e41aa6 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,35 @@ 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 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. + * + * guest.enc_status_change_prepare() called before adjusting direct + * mapping and therefore it is responsible for converting the memory + * to private. + * + * guest.enc_status_change_finish() 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 converted + * properly. 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 Fri May 26 12:02:25 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: 99465 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp416151vqr; Fri, 26 May 2023 05:04:41 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7EO5FMAhhBj25FIvCqrgFID7VYvnWGmCt7A2FzOX1r5npKK7Azz5ILuVXgY1vT5vq37OpF X-Received: by 2002:a05:6a20:1442:b0:10b:97c8:2e16 with SMTP id a2-20020a056a20144200b0010b97c82e16mr2485231pzi.29.1685102680791; Fri, 26 May 2023 05:04:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685102680; cv=none; d=google.com; s=arc-20160816; b=OkoMZP2g3CY5QcJUsezYvO8KdXF7b4J/8m3YCc/NIw2f81zEgWvqR9Ew6S74rds8qj 2cJa/+5gqe8oKXMdpwK3Z5XPu+GjKRwUhrDalYGY7Y0jrW2ONX+nK9p8L0GzYhyIfowD dSQWPNaW1hSeh8LGNEjW+n7C4+fT5XxLHL5qAkQOWtic0dOI76OWf0X4P1+QQyS1099U 5SjOv+M7SQmpbdBZcYMinWNbTIrY5edMajazZPTJq9wh2Po4jIsKuUpjFVBaDQnVB1ck 3HdCRRqxY1DryMnt2sUshrpyL6bauqh5NBieb0DpiiwIQ1zVer9D5S7qtxjPXbL2lLQz RSrw== 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=GrkLEhje+fFbiF6hhK49miuYR4T3Ann/mtj9cEzCP0E=; b=ijg7tSkuUCkS5CjTGSYLJhLPzDAsBI7+HZs+yyTc9o2ES5LbCGnxaB4h9VTHpiwavK Dgo2VqrGeFSJ6hb+mZr9ZLdP4F861o80AlV7hURDDL6ilTAK0hweXIVD/6lUu2w33V6j DpFxJhIruMjrQP3t3DF0o0hlKq3dQhGw9ihzcw8CU60hVC4plG+n0R8kLKwaVGmTNIHr LG0CBHRs5097z0eoHAc8mj0b3dnnWrdkn5jCBsYfxqw7klcmWjjsz+INR3BACG/SI5FI PSBIXcYFQ/TWkkvdxQoVcztQ4Ovhm7Ec/Bss6tz06PLZt3EXV/iM+Y99MTiO62kPU0N3 Q+uQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="h9/tEo95"; 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 j197-20020a636ece000000b0053ef31be221si3310790pgc.841.2023.05.26.05.04.27; Fri, 26 May 2023 05:04:40 -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="h9/tEo95"; 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 S236799AbjEZMCq (ORCPT + 99 others); Fri, 26 May 2023 08:02:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236664AbjEZMCl (ORCPT ); Fri, 26 May 2023 08:02:41 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED8C419A for ; Fri, 26 May 2023 05:02:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685102559; x=1716638559; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=oo/dvf1ZlrvtcR59TYCugDRCyxgFSECoi5qSl2YCdyA=; b=h9/tEo95eqkQAtdJ7/8gKFrwWVCwn7HX8Zg4WO2pd3Uwl4HnNy3I9nmU obE3GMYy9im0KsbO1sOZ/axZFqWT2ORrr+X07FfJNmTPJCwEb+PfnKJLE J85wqn6wHp8OjbL97kVJFYmMECXJdq+vfO+TFger+n63nlIir4fDEe44X gwV2/EBXvcG6pReiASVrgQWijq1X6hMFjMV2zieOQFuHkbYZ/0kLznwB/ q1dWIleJ9MW3hb7uDDtXkxpB+3zhel1QPyUc0Xs/ccMHt6jigvCnzm/+m WFnPmCWUE2Xetev99p1vkhVR3wgxagZ8k2YTwXvRsqo3CqUnBvTHlRsAu g==; X-IronPort-AV: E=McAfee;i="6600,9927,10721"; a="334538200" X-IronPort-AV: E=Sophos;i="6.00,194,1681196400"; d="scan'208";a="334538200" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2023 05:02:39 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10721"; a="951854338" X-IronPort-AV: E=Sophos;i="6.00,194,1681196400"; d="scan'208";a="951854338" Received: from fgarrona-mobl.ger.corp.intel.com (HELO box.shutemov.name) ([10.251.208.169]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2023 05:02:36 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 5785F10DC18; Fri, 26 May 2023 15:02:33 +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: [PATCHv2 3/3] x86/mm: Fix enc_status_change_finish_noop() Date: Fri, 26 May 2023 15:02:25 +0300 Message-Id: <20230526120225.31936-4-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230526120225.31936-1-kirill.shutemov@linux.intel.com> References: <20230526120225.31936-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?1766958228489523032?= X-GMAIL-MSGID: =?utf-8?q?1766958228489523032?= enc_status_change_finish_noop() defined as always-fail now which doesn't make sense for noop. The change doesn't have user-visible effect because it only gets called if the platform has CC_ATTR_MEM_ENCRYPT. All platforms with the attribute override the callback with 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; }