From patchwork Fri Dec 9 06:52:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai Huang X-Patchwork-Id: 13069308 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 256CDC4332F for ; Fri, 9 Dec 2022 06:54:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B765D8E0009; Fri, 9 Dec 2022 01:54:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B26DB8E0001; Fri, 9 Dec 2022 01:54:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A0DD8E0009; Fri, 9 Dec 2022 01:54:16 -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 883CE8E0001 for ; Fri, 9 Dec 2022 01:54:16 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6D40EA0355 for ; Fri, 9 Dec 2022 06:54:16 +0000 (UTC) X-FDA: 80221853712.16.D3C28B8 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by imf20.hostedemail.com (Postfix) with ESMTP id 908701C000B for ; Fri, 9 Dec 2022 06:54:14 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XuQis5wE; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf20.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=1670568854; 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=ZWo8ODBA6Id644JBWFXRyxyDkBbdHD0fsDSlrtqGTcI=; b=4ODak8+tS/eF36YyNqiS8+HHW2Dd9jw0l7KIOkljNcbk9usWtz7AH4ZY46Xz6CRiBzWhc0 +s14/OoP14UdUNAtuCQLlGjMiq/KdskgyXIxtrHoBBuXn2b3DSm+/bIitRNhoeVU0Fu1kK cq1+sZXTMTl7AYkipNSa28ENrUUbGjg= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XuQis5wE; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf20.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=1670568854; a=rsa-sha256; cv=none; b=EI1kZq9Xa3/YJV97mjkOinNT62rmpXmZdx7c9w65ao8rwPe/YhtfceSsGpz+4qrrHTyIrz NATL506MvSfH8/AEZjRL4qiEUwOArQJTEflkv3Ho142ts0spFHboIH2tVev0Ke8yg4CLYl YhMyMc74szgKOlgzyF6OIJo4mlPXJcw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1670568854; x=1702104854; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pEzHyd4Ac6oG5xApJ9gB+fF+e/HTbnBocyb4jHOlMgw=; b=XuQis5wEV5ga0Y6MN9PBS0P3tQ2CLGvcoNyvSLzF/QjccwlWw1MHBvaB DgyJAm9r1+Fp4lPEBGGm3ZUUavv6xMTR/+gmHw0802TKQgIy0XVDGi9qa namkItGa5Z/LzH4Cm2VJ6pIhtddORe2lC9QVuT1xXV9hlKdIQsK6VjKYv RmbMtcUHFKljHNnUQ+F7bBbWRnHahlzLFrCGNs6yo8RvO4fT7cgsIRwDC 9ExHknXl2NFcwnEoKf3CGo1nLNf6z/y4rDW2EAEkl2U/p6kqEXKuqMloN wefEOtc6qI6G5GvWk1VsSutGavPkeFxK3jpSUkZBY6EHdYPRhr1SEtAfE w==; X-IronPort-AV: E=McAfee;i="6500,9779,10555"; a="318551512" X-IronPort-AV: E=Sophos;i="5.96,230,1665471600"; d="scan'208";a="318551512" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Dec 2022 22:54:14 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10555"; a="679837143" X-IronPort-AV: E=Sophos;i="5.96,230,1665471600"; d="scan'208";a="679837143" Received: from omiramon-mobl1.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.212.28.82]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Dec 2022 22:54:09 -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, bagasdotme@gmail.com, sagis@google.com, imammedo@redhat.com, kai.huang@intel.com Subject: [PATCH v8 14/16] x86/virt/tdx: Initialize all TDMRs Date: Fri, 9 Dec 2022 19:52:35 +1300 Message-Id: <16d47f9611d53c0f07a4af2b5739ed83e41b6e48.1670566861.git.kai.huang@intel.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 908701C000B X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: kdcrxu9nz73ixjetb9qxwqskyz734i5y X-HE-Tag: 1670568854-543394 X-HE-Meta: U2FsdGVkX18+Fhhn3pEPtbTCIertx0TiEnZ3bwLjNRWIUP4S8kEF+ey3kH16a62thr/nMzIcy+xZnol371k2oDIfH1ZRQQZYWLrkVuvtB/tAkfWMYeRymY/1gHwXb6IaRRMkwTSWSIY6x5BvgEJdXtUcQvtF5+OiFV3UksGkLaYzIEyHLNNy4OSz3fynIhvEILi7QAlZ2yykr+oz9PWta6B7LDflmVV21x+ll3VaRCh+Go/lmjKknTujC63r31Xccg8+nO2NGzTUuONaKvaxhYl/H/y2j6RMQ6cmBbQ0htMrbfRVWGM2J06cTOtGYSFvlC0n1x4iveHQ1toIFwMwIyKfPGMh+UKG7hP9px5Gn3NO7WercfMqzrf/4XaE+JdWI4ZSMy9Su0QdQIfZ7LOtWwqNEBc+SMSLVfGKhlp048Yh34Y+JwxFbM7MGTzPTtATQGleMSma6MiqOYXaMYU3oUhzc+lnowi+djZcY/wtx2a7+XjG9bikPqhQYrGazpyUfRW1jGDAPdvIifUP9l+O5KbIEj+AEfTglOU3tGvHpDrf9xKmkV4xcLpdTew+1ncTrNrfWYeLM918J4SmE5Ve7cseyskYhA+xznjkZiBAKd8Z7j4I2hgBqLEpDnmzkqz7q8wpYnKUIaoGztSwQl0WEWiQBDLVQGIQrgNXbk+MH6w5SsHOjIMGoQNVECvVvBlY0bLwVKgqz+zlP1TbfaH04gnN21yyRJbUBuWQXyndMqQZrUYyT8wl6OrUFmbEfDb9NKzt6XRLUvlJWdmFz+frIc/IQtG3yJZZ77CgWUu/qmAkKoTUo7R0j69M5+phkRisPCa2DEA0ddrW8a4VOGusa+E4o0xthjUHYHwT5EHK7uBg/AxUTBizeUmoTPHhYJl6VXgVwVEH3S2leL1prjRoeg== 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 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. Reviewed-by: Isaku Yamahata Signed-off-by: Kai Huang --- 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 | 61 ++++++++++++++++++++++++++++++++----- arch/x86/virt/vmx/tdx/tdx.h | 1 + 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 4c779e8412f1..8b7314f19df2 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -1006,6 +1006,55 @@ 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.INT succeeded. + */ + 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_tdmrs; i++) { + int ret; + + ret = init_tdmr(tdmr_entry(tdmr_list, i)); + if (ret) + return ret; + } + + return 0; +} + static int init_tdx_module(void) { /* @@ -1076,14 +1125,10 @@ static int init_tdx_module(void) if (ret) goto out_free_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(&tdmr_list); + if (ret) + goto out_free_pamts; out_free_pamts: if (ret) { /* diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index f5c12a2543d4..163c4876dee4 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -21,6 +21,7 @@ */ #define TDH_SYS_KEY_CONFIG 31 #define TDH_SYS_INFO 32 +#define TDH_SYS_TDMR_INIT 36 #define TDH_SYS_CONFIG 45 struct cmr_info {