Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1695891rwd; Thu, 25 May 2023 17:06:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4WnsyleTM+U0jZDtxMUzZOwGG1i+MyBGTwMaLx0iOOCbZSsc/Bf5EuKxO2gThaTGOkMGxV X-Received: by 2002:a05:6a20:4315:b0:104:a053:12fb with SMTP id h21-20020a056a20431500b00104a05312fbmr22145612pzk.60.1685059602674; Thu, 25 May 2023 17:06:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685059602; cv=none; d=google.com; s=arc-20160816; b=YZfDMdVIgQQ5wOUoPzvwHeV1Nwzh2pB4mEcpv+8lluEOA+4Eenh3p+vGuUJwYvOst6 2jWPmdtg6QLUFUEfzUQR8VzXjiUWBcAafS0Dsivs4X5seTpnr8FfJaQJttoURUEBlTGx SXwP0f+gi+en+PBwi9JfXZedPCCXbhpJlsCyu0ehk/IpgSg2PhqRQ3SfgBpn/lg0h4fA K/uVnNOuyEZ/TIEKgivgouLYaZ2JcLo0l/adhLZVYjcHc/CUEyFfF0Kn9WRNZhJDkhft /xTCB8dWiDZOIR0X+UuiHQu+c7yFIGfYJJmjLPabyiN42IlP6ZbYPnKdpT1lq8sXk3JW km4g== 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=QRqJTfdZMauWnYEpP+vbCdHeHbmY7k6m7Gw8vWYDlOk=; b=zeoOYVNffeJFM7FpPdaFk6JLK4QvEoy/T/DKAeM0yCe8yCzu1cyaBXdSY4nsstEDxP xf+j+3ADRFVjJlFK5KRLPCkWo+I4YkDFmwCjTjpHgoYQmHdL+sJc1TgLmIg/9LnjLHoV 6y42uYbXkS3kOwfyVveieBPyjGHPUFTiMbPqd7EGTrHzccOxO6KCeAvLwxX0LgmXeIfQ c1K8nawdklAFFF7g1R2ijeEwz+3Zt7UPZ2DFfjRW04B9FRKJbJu8tdYgT15zBx50uoZj KJB4hXT7BUVLRc9NExE2xmk1wgjKXEZQhVHP7o+XidPQmjX7SJAm9CUWr+Z5nsl72USF dyYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=czL9250S; 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 e2-20020a636902000000b0053440fe0817si2075537pgc.611.2023.05.25.17.06.28; Thu, 25 May 2023 17:06:42 -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=czL9250S; 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 S241424AbjEYXkH (ORCPT + 99 others); Thu, 25 May 2023 19:40:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241274AbjEYXkF (ORCPT ); Thu, 25 May 2023 19:40:05 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A23C19C; Thu, 25 May 2023 16:40:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685058002; x=1716594002; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=n8tS25fObeTtNS/qQCrIYQeRMkQmVgP3uApvxw4lLGM=; b=czL9250SQkolpr8ZG+H0Cb/Dl34ikfPnky5g4PxpKzaUA3AGdSZLA1FZ bXWMWSXRxCJDaoSjI6v+UjbFuxqdY4uizQiPWK5vE9tok0nvJpHN8m2eg 1LkrVAueHXa7AdDHA7Vh0HtjmUalo5FSRSM0w+c5WAapu6veHv6FbGSdC voiZW/xnp0DxsxCJP8xr09CF2wotTuuUwLj8kj8ISVQRaw0UVfrwQLNzq r9mKH5llxtPNmN87X3MvX8zYlBsNz1BTXOk1wTUV/CfmbZw9TDDjknQ/A ZgLu9qfuijfTJuNRutl++melP6SjGfaehOH8qot3ewAC6P7ct5OOHmSl0 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10721"; a="352899731" X-IronPort-AV: E=Sophos;i="6.00,192,1681196400"; d="scan'208";a="352899731" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 May 2023 16:40:02 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10721"; a="708189446" X-IronPort-AV: E=Sophos;i="6.00,192,1681196400"; d="scan'208";a="708189446" Received: from mseamste-mobl1.amr.corp.intel.com (HELO [10.209.120.148]) ([10.209.120.148]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 May 2023 16:40:00 -0700 Message-ID: Date: Thu, 25 May 2023 16:40:00 -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: [PATCH 2/2] 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: <20230525225847.28592-1-kirill.shutemov@linux.intel.com> <20230525225847.28592-3-kirill.shutemov@linux.intel.com> From: Sathyanarayanan Kuppuswamy In-Reply-To: <20230525225847.28592-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 Hi, On 5/25/23 3:58 PM, 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. > 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_tlb_flush_required() called after adjusting direct mapping and > it converts the memory to shared. Do you mean .enc_status_change_finish() here? Isn't enc_tlb_flush_required() called before adjusting the mapping? > > 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..84525df750d4 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_tlb_flush_required() called after adjusting direct mapping > + * and it converts the memory to shared. Same as above. Is it .enc_status_change_finish() here? > + * > + * 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