Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp4564291rwd; Sun, 4 Jun 2023 07:58:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4kuScNM5PtZ/EsdmRz4Gua1RUn0sl1w0C3y96Du10XlUGRAnLjnWr9VrWU/LSM0LlZfYAw X-Received: by 2002:a05:6a00:2ea3:b0:646:f971:b179 with SMTP id fd35-20020a056a002ea300b00646f971b179mr13951241pfb.16.1685890735486; Sun, 04 Jun 2023 07:58:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685890735; cv=none; d=google.com; s=arc-20160816; b=1LdTb7nCU7IIq6xX6L/KziyvKVKM72kC/DhY5jIR5N06gl8ZHK8TlQHQZp+9IYL8Vi WT3mlhUpkESXmEQWv0XI7w18viMWhuXuD4120uPqRC9C8CPxN/OXbpa4XTPAlAi02Fnh cmHer8Pppzz00Znt9lCh499N3ZGSiHM0y+TKHD0/U0IwPIiPk0K9VC7K+PpjuVN5VXwO ctMHyDa96atHBYaQZi/vctq/ujlvawnYf9v6kJMRfEbqjz0ria+0LaJwEdS8OcGJeJqz eX3za1lvI7PUEEmVUaSZ2mD/OTK75g8fqbmY7upbdp3FZe4n/gQ6vf70+2epgduzaZYq zwTA== 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=guX7puLDYUp931TcKiW7asu3bjSVl7ICcQOSHwV7RPY=; b=nv2T2F4wTwLW7nbfwg+J6SxKe9p5SzstXuYG7tC9Zyuci+w20nXQssDMqbKJNvMCwa HOno8+G7KRZ5GBvl/U641nIjHvloe2vLDt2dXLqmq29lYCKbs077WuuoGmzcO3gTXAMb Nlkml5+vzjUopmvgMl8ylJnhKoQsYizAbanoi50EzRKFeUMn00G8nGkLFSe9M43d8HzC aqJX14tz7fpNVgxzSlr/JPeDmBNwOaDf+71OHvz0LfSOeeZaZpxtSbGP1j8OPK2C5T7s dqUAjRGBk7hyf0gqd2RjBY/EjX4qtE9OHkujk8nB32qPTAPwB/nvhS3mAMCCjC3IYjdK bwjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=laZAjaMN; 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 k24-20020aa79738000000b0064dbf7cdec2si3997743pfg.186.2023.06.04.07.58.43; Sun, 04 Jun 2023 07:58: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=laZAjaMN; 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 S232292AbjFDObB (ORCPT + 99 others); Sun, 4 Jun 2023 10:31:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231860AbjFDOam (ORCPT ); Sun, 4 Jun 2023 10:30:42 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2CC71B3; Sun, 4 Jun 2023 07:30: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=1685889035; x=1717425035; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=k4MMOcKeW3mtE7wFf+wIKDTivgf3+1s3ufWIu5AQpnU=; b=laZAjaMNiW/PRnDd+glN/y+Qz6SptmgzG9fI7lzZ7NKdoC5m5LK7uZja O0dyBuK6GZY94z0ZPYzq0dU0hoMgOvGH9KYFNdHkMgAGf0MU2X/J+mE19 0LOgcpQc8X5zd8sFbpz6L42N/L12gOF8GexAf868R94u46HINlHKpfUiP gp57aVZXM6cH5IQS3wkdPRDFjzHxBGQ+2HLVHGMZlIVLPp9v0+E0pXgTq xfS9sQ3dFBqXGnQ4LQa930j7/wkGIJ75+wTqukWCZFacsc5pkLwT8cUuw at7wlo/n0Ed7zdP6ysJ9IMW6VcwjCVt3RLBZmU02SJWKe2LFEr6fU7jf3 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10731"; a="353683665" X-IronPort-AV: E=Sophos;i="6.00,217,1681196400"; d="scan'208";a="353683665" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jun 2023 07:29:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10731"; a="1038501187" X-IronPort-AV: E=Sophos;i="6.00,217,1681196400"; d="scan'208";a="1038501187" Received: from tdhastx-mobl2.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.212.50.31]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jun 2023 07:29:09 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: linux-mm@kvack.org, dave.hansen@intel.com, kirill.shutemov@linux.intel.com, tony.luck@intel.com, peterz@infradead.org, tglx@linutronix.de, seanjc@google.com, pbonzini@redhat.com, david@redhat.com, dan.j.williams@intel.com, rafael.j.wysocki@intel.com, ying.huang@intel.com, reinette.chatre@intel.com, len.brown@intel.com, ak@linux.intel.com, isaku.yamahata@intel.com, chao.gao@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, bagasdotme@gmail.com, sagis@google.com, imammedo@redhat.com, kai.huang@intel.com Subject: [PATCH v11 16/20] x86/virt/tdx: Initialize all TDMRs Date: Mon, 5 Jun 2023 02:27:29 +1200 Message-Id: <7bd7d0c6196deb58b54d6e629603775844b1307d.1685887183.git.kai.huang@intel.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 After the global KeyID has been configured on all packages, initialize all TDMRs to make all TDX-usable memory regions that are passed to the TDX module become usable. This is the last step of initializing the TDX module. Initializing TDMRs can be time consuming on large memory systems as it involves initializing all metadata entries for all pages that can be used by TDX guests. Initializing different TDMRs can be parallelized. For now to keep it simple, just initialize all TDMRs one by one. It can be enhanced in the future. Signed-off-by: Kai Huang Reviewed-by: Isaku Yamahata --- v10 -> v11: - No update v9 -> v10: - Code change due to change static 'tdx_tdmr_list' to local 'tdmr_list'. v8 -> v9: - Improved changlog to explain why initializing TDMRs can take long time (Dave). - Improved comments around 'next-to-initialize' address (Dave). v7 -> v8: (Dave) - Changelog: - explicitly call out this is the last step of TDX module initialization. - Trimed down changelog by removing SEAMCALL name and details. - Removed/trimmed down unnecessary comments. - Other changes due to 'struct tdmr_info_list'. v6 -> v7: - Removed need_resched() check. -- Andi. --- arch/x86/virt/vmx/tdx/tdx.c | 60 ++++++++++++++++++++++++++++++++----- arch/x86/virt/vmx/tdx/tdx.h | 1 + 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 4aa6cf27e1c2..8ff07256a515 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -1029,6 +1029,56 @@ static int config_global_keyid(void) return ret; } +static int init_tdmr(struct tdmr_info *tdmr) +{ + u64 next; + + /* + * Initializing a TDMR can be time consuming. To avoid long + * SEAMCALLs, the TDX module may only initialize a part of the + * TDMR in each call. + */ + do { + struct tdx_module_output out; + int ret; + + /* All 0's are unused parameters, they mean nothing. */ + ret = seamcall(TDH_SYS_TDMR_INIT, tdmr->base, 0, 0, 0, NULL, + &out); + if (ret) + return ret; + /* + * RDX contains 'next-to-initialize' address if + * TDH.SYS.TDMR.INIT did not fully complete and + * should be retried. + */ + next = out.rdx; + cond_resched(); + /* Keep making SEAMCALLs until the TDMR is done */ + } while (next < tdmr->base + tdmr->size); + + return 0; +} + +static int init_tdmrs(struct tdmr_info_list *tdmr_list) +{ + int i; + + /* + * This operation is costly. It can be parallelized, + * but keep it simple for now. + */ + for (i = 0; i < tdmr_list->nr_consumed_tdmrs; i++) { + int ret; + + ret = init_tdmr(tdmr_entry(tdmr_list, i)); + if (ret) + return ret; + } + + return 0; +} + static int init_tdx_module(void) { static DECLARE_PADDED_STRUCT(tdsysinfo_struct, tdsysinfo, @@ -1088,14 +1138,8 @@ static int init_tdx_module(void) if (ret) goto out_reset_pamts; - /* - * TODO: - * - * - Initialize all TDMRs. - * - * Return error before all steps are done. - */ - ret = -EINVAL; + /* Initialize TDMRs to complete the TDX module initialization */ + ret = init_tdmrs(&tdx_tdmr_list); out_reset_pamts: if (ret) { /* diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index 15e7250e74c7..012153067440 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -26,6 +26,7 @@ #define TDH_SYS_INFO 32 #define TDH_SYS_CONFIG 45 #define TDH_SYS_KEY_CONFIG 31 +#define TDH_SYS_TDMR_INIT 36 struct cmr_info { u64 base; -- 2.40.1