Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp102100rwd; Fri, 26 May 2023 15:49:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5HB8J0vGsyfal0nPyv/fwrho2QwehdKwmO6/fD+7xMHlXHyZae3SH27c51NhH4rOTBG+pq X-Received: by 2002:a17:90a:98f:b0:255:a904:7a7b with SMTP id 15-20020a17090a098f00b00255a9047a7bmr4489255pjo.26.1685141364852; Fri, 26 May 2023 15:49:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685141364; cv=none; d=google.com; s=arc-20160816; b=Ffv8Ccf/yf6Z4LHRlACI9RO2mr1Xvc5xx/+2NIjS02d6sMvLKPghOUoytanmvQQzEZ CVcolBFLaoGJoMXduAVvPNTKcViYGqoopktPFB+nu+jXAQTuz45EdfkB6h6gc2+OzGb2 jH7thGF7n6iMxQ/s64d99bS/d/IhjOUVdCNv3B5uBGLH4U3ODlWbwiQs6CtoaYMdSl7y P6iQ/wjtCoSE9IedVTsHRpld3zpXYN0Q+U0Axx2Zr04RwkWfpmlHI84jkjs9Ye/13zjB SZZbk+xZhUTsVfoWoFqjkJU0Erk7DWciI2SRT1WCjspXznzLtSJaA640bJs9xhClsT2V SoFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=9toTAXXqFXUrQcO/0AknFlGjPuiTSglxDEUXEYfWw/Y=; b=YBYkRfogBoMk4tRgr8NKwhI8WM5dZT2Hw91B2mXsKgj/+3vMD2iek3roS6A4i3ixQd m6VFhIPgLfUrOQA+8AFMDzD8AfRa+bCQBCJx7WYzgFgpG8hKNEwZP4o435nLLjg5+KNt 5HQWZiFC8GaUufhu7jrGlh0PTawO1jb/f0PqzuY95Ik5SPIUK3s6OIP3pLw/XDMsg2jU fpkFVIEzjhEfqrLubhgI/US1rmnTHrNxFBQUfsXy4fb8Dfihq/VjQrqIPimbOYZJKoGp c1notTZ1W6KISG7R7L6W/tzc3n3TMgTWadA3TysPsnWQ2ow9nbnE1mN4a9qtZc9vXpSv 5dXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LvaxQf3e; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ob17-20020a17090b391100b0025359a570fesi3172508pjb.19.2023.05.26.15.49.12; Fri, 26 May 2023 15:49: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=LvaxQf3e; 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 S244300AbjEZWLC (ORCPT + 99 others); Fri, 26 May 2023 18:11:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231265AbjEZWK7 (ORCPT ); Fri, 26 May 2023 18:10:59 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CC68D8; Fri, 26 May 2023 15:10:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685139058; x=1716675058; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=DY5NsYdEM14/qoHMw4y56aAWs6TvL0qHN+cTi8SiYVM=; b=LvaxQf3e82k62R//h7BiPnwvaVNjS7IC+Bz2GidrNT/S8lUrYLNze9gj 6pmHp8qw5N4P+JcT6GgnmgBAjgb9Vb+vqa4Y3T2Wx+1K2ljPgeVlWnGOM 6xNRZccDXLfhS6KbQO/FVPkrIK5vPH5HBCBnQUkSWc5Xx/BeHa/HQQluk lGUIc9sYaIw49vCB2if2LlG0O4sjhrDs64hUY75+VmNxeNjlmTquDl84V 8jf/4s8AK4A+l3XL+4GR34opPlEVlAAlkE1Vtae4QR0WlOjPGN+EQGIyS N8csxKKj2Ju9CVGhrF9igdw0BDXXG7Ium4T9IHRXZnshPnR/b3W39H4Dw Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10722"; a="357566316" X-IronPort-AV: E=Sophos;i="6.00,195,1681196400"; d="scan'208";a="357566316" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2023 15:10:57 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10722"; a="775219198" X-IronPort-AV: E=Sophos;i="6.00,195,1681196400"; d="scan'208";a="775219198" Received: from svepakom-mobl1.amr.corp.intel.com (HELO [10.252.139.203]) ([10.252.139.203]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 May 2023 15:10:56 -0700 Message-ID: <19d9977f-bf07-2948-1b45-456af9c09e2f@linux.intel.com> Date: Fri, 26 May 2023 15:10:56 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0 Thunderbird/102.11.0 Subject: Re: [PATCHv2 2/3] x86/tdx: Fix race between set_memory_encrypted() and load_unaligned_zeropad() Content-Language: en-US To: "Kirill A. Shutemov" , dave.hansen@intel.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de Cc: decui@microsoft.com, rick.p.edgecombe@intel.com, seanjc@google.com, thomas.lendacky@amd.com, x86@kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20230526120225.31936-1-kirill.shutemov@linux.intel.com> <20230526120225.31936-3-kirill.shutemov@linux.intel.com> From: Sathyanarayanan Kuppuswamy In-Reply-To: <20230526120225.31936-3-kirill.shutemov@linux.intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,NICE_REPLY_A,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 On 5/26/23 5:02 AM, Kirill A. Shutemov wrote: > 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. /s/to/to be ? > 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. I am wondering whether this issue exist in the AMD code? IMO, you can add some info on the window in set_memory_encrypted() where this race exists. > > 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 > --- > 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; I think you don't need to change the order here. > - 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"); > } -- Sathyanarayanan Kuppuswamy Linux Kernel Developer