From patchwork Mon Mar 6 14:13:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Kai" X-Patchwork-Id: 13161229 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 93DF9C678D4 for ; Mon, 6 Mar 2023 14:15:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 38A826B0073; Mon, 6 Mar 2023 09:15:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 33B2A280007; Mon, 6 Mar 2023 09:15:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 202DC280005; Mon, 6 Mar 2023 09:15:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 0DDB36B0073 for ; Mon, 6 Mar 2023 09:15:42 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A1D84C01A2 for ; Mon, 6 Mar 2023 14:15:41 +0000 (UTC) X-FDA: 80538671682.18.45A3C04 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by imf05.hostedemail.com (Postfix) with ESMTP id 8D14E10000C for ; Mon, 6 Mar 2023 14:15:39 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QTpprzmK; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf05.hostedemail.com: domain of kai.huang@intel.com designates 134.134.136.24 as permitted sender) smtp.mailfrom=kai.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678112139; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=82UsUEXgm4/KQKJbveyF1q6egiYV3ZKoqLprAH6FkFw=; b=sL9feBYQcK4rrDadr6OrNiYjla9r/jdt/osx+R+cy9x0RTWluWOzsNalRrWyEdsUFQRG9H wc8+lfDlt6iid3X/MF0VXjUiqdmbEnp+Ucn0o+yUCJgTLg44TBYZZD4oN2BKkfvtEziLk4 5oVK2SVdk0b83TPUmTnUJDn/a3GUti8= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QTpprzmK; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf05.hostedemail.com: domain of kai.huang@intel.com designates 134.134.136.24 as permitted sender) smtp.mailfrom=kai.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678112139; a=rsa-sha256; cv=none; b=vBxgVUE183PVX3+QhnzTrX83jZr1uOkWtzNrVVEhS0WucdvC6XPiSNCCmvDKru38BZs+pw 8SWajXcgA96hW9AKLn/Z1ki2P0Q7vQEW8uzJvBaYE+ZCc9GnfUgKL+roHXzam1dnluAvYZ onibyMsidd8Yg4UFp1d3GJIq+fPWepI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678112139; x=1709648139; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zHYj/4Y5tzqrg8Uf+TD7J7htjEWwNRrJOSQ2jhIx+Zo=; b=QTpprzmKUUruigHe057z1FSUwg0cAgX0989zKfKs9GPLpLnBwN11TNPP UF4pzsBFJv+V+JXHDMFPfuFzX/OuLWqcoXYHdorrpU66uufmjjmHUgwDf hnvxZ3HZ8/7w5GKMieU2oaDymlLbdLnpkOinUTWQ98GtFETaQhG5Uzxcz pYPghD3OJKhbS1k2cOtUzQDZm/sin3RY41A2y6H0HhYhT5bwL3x53rxhV CgSJiuoFFUPn49wIQ+00I8vq7cT9eHxjW00nr8B0tUMmXulYxWpArtziH 86myZlQHSspgFmXE4IA5/s+mbUaX+etk9JugFZqxZKphN1vkowJTDXR7A A==; X-IronPort-AV: E=McAfee;i="6500,9779,10641"; a="337080309" X-IronPort-AV: E=Sophos;i="5.98,238,1673942400"; d="scan'208";a="337080309" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2023 06:15:39 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10641"; a="765232506" X-IronPort-AV: E=Sophos;i="5.98,238,1673942400"; d="scan'208";a="765232506" Received: from jwhisle1-mobl2.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.212.92.57]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2023 06:15:32 -0800 From: Kai Huang To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: linux-mm@kvack.org, dave.hansen@intel.com, peterz@infradead.org, tglx@linutronix.de, seanjc@google.com, pbonzini@redhat.com, dan.j.williams@intel.com, rafael.j.wysocki@intel.com, kirill.shutemov@linux.intel.com, ying.huang@intel.com, reinette.chatre@intel.com, len.brown@intel.com, tony.luck@intel.com, ak@linux.intel.com, isaku.yamahata@intel.com, chao.gao@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, david@redhat.com, bagasdotme@gmail.com, sagis@google.com, imammedo@redhat.com, kai.huang@intel.com Subject: [PATCH v10 14/16] x86/virt/tdx: Initialize all TDMRs Date: Tue, 7 Mar 2023 03:13:59 +1300 Message-Id: <7cee98d35f9d613e2dcaa86cd69b28583d2e2ee7.1678111292.git.kai.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.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9BGz+ESBcVvkDkHhifGPdNzw/wSNtR uc6TwJai CvN+1DYraoBLmkfcrTjo5mjK1ynWsDyH3nPJ3NQVym5e1tiV0NPUJg4CMcVbsBt1tpB7dxwEiNGhVcVNf1laieao5mLKxVEmpeLIMUFzfOu3iMHKMpGKueC65CzAw5OIgpEcyVYP4Vo2u9gA4v0rFkQ1fUa4sDRKSGtpwiVmSlDxYqRU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- 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 0a3b3374c5cb..ee94a7327d93 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -1108,6 +1108,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, @@ -1168,15 +1218,9 @@ static int init_tdx_module(void) if (ret) goto out_free_pamts; - /* - * TODO: - * - * - Initialize all TDMRs. - * - * Return error before all steps are done. - */ + /* Initialize TDMRs to complete the TDX module initialization */ + ret = init_tdmrs(&tdmr_list); - ret = -EINVAL; out_free_pamts: if (ret) { /* diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index 880e90dedb3f..48f830087e7e 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -21,6 +21,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;