From patchwork Fri Dec 8 17:17:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 175958 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp5598175vqy; Fri, 8 Dec 2023 09:17:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IEUweGUR6C32dYFe0967fgoh4lm7RXs/gq9ZZSjSEqsyAsX+4P9/xv1IFL+jd9xa8341sQD X-Received: by 2002:a05:6358:a1b:b0:170:17eb:14d1 with SMTP id 27-20020a0563580a1b00b0017017eb14d1mr195765rwa.65.1702055859238; Fri, 08 Dec 2023 09:17:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702055859; cv=none; d=google.com; s=arc-20160816; b=NOsHpyVdSb5kW2OQ2rWrpDb7D1okzpRkLSN49Y45kou2V7TcpPhFANGf/5RgZCJXpO WiUaUGGG6YmAU9jJZkNarTT+WKDAsdWRiPQDgDt6HbGKgyN5Vkfe3GzQMMqQjuPwl5BN q7GQNQTZAyiaOt5xhtoo/+1wfcvaNtc4tK5813zrpaqYUt5gpTSv1ebleg5DVOZ4JBkM krDwtM+dos//m1UosoQBXMQCTqrrAX3ga1eXMHo1DHZcF02uYaLV8UFMxtxRNUPLwUUH b7meVcb4VeyTbK6O06o5ShKHq/u9WQ7ZMzgu7v+C+3mAeFHSs8yHzrX2Z9bOMUFdtDjf f9YQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=Pa9fzJ/ZtUWmQ4aU5PKAI7vyFrBsSSEyOZIXYm7TOqo=; fh=6X+JyjsAoK9TUwjvxGea9vfUmnvlnnDsu3Ji0y9ItP4=; b=K6lVlnVuUqI3CDGbgO8uOykKN16D3YkMNTnYcku774CXl1AQTbSAcuPsMI6fjfRVeM ft4vqjlmWTLFafVhwRpm28JdwluX2fYWApAhBMhPXdSJVBnXKccaz6uiZBvx1r4227cJ ROZJzE09Vqz626pi4msKXn5eF8iFoYoUJbg0rx5rfC4Zhxg2NSYDvEWZfQjTYBhUooB9 K0JaAF4DQKC70PNppwkTNB9aKSt/nnE1LDj+zt7CIjk5xJBPaOkEmz2NHfbtBu9Ho9mM NtH5m7PMJ+UebIPRms29Ll1+mwD0uSdYgBOAEJnp5RAPQfTW73t8i3JN3YQC/EgOzuI5 AdFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=CGf6etna; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id b2-20020a63eb42000000b005be209ac7fesi1784056pgk.744.2023.12.08.09.17.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 09:17:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=CGf6etna; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id D5E3A80FDDFB; Fri, 8 Dec 2023 09:17:37 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233784AbjLHRRY (ORCPT + 99 others); Fri, 8 Dec 2023 12:17:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233788AbjLHRRQ (ORCPT ); Fri, 8 Dec 2023 12:17:16 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73F9711D; Fri, 8 Dec 2023 09:17:22 -0800 (PST) Date: Fri, 08 Dec 2023 17:17:20 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1702055841; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Pa9fzJ/ZtUWmQ4aU5PKAI7vyFrBsSSEyOZIXYm7TOqo=; b=CGf6etnabUaAX0K+Joj/BNcNV31YWXKiBx2bO4LCDsxA1Zr9f70L64wzi52Nn6NoBtksy5 8nsbBih7XibMn7PaVnN93nafULgSeP4vRfJDfFJyb3tq0ZnhkKs5j0yZxbj3Xb9hd2O+rR LzKieGZSslg880acozhxuW9pcanDK33tQiA37fVvz1D7KPzYrKFcEc9seMqDNNlJuB/tBq 0etEtpfBqH/2kSg5+DEXkcz3VbrpxdoVRwrWbmuV4P2EoJf7Yp72d2E6fNS/Gv+Oly8cFw +wm1G93JITz74vbDAzLFUVCxkHbGmHXgnMYsh1rk+5EvZTBjwhDVJR2jXndcbQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1702055841; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Pa9fzJ/ZtUWmQ4aU5PKAI7vyFrBsSSEyOZIXYm7TOqo=; b=ZCwhItm+pQruF81n2RqIWWF2fgPYkdj2plnCz3Ip2mXSrzriy4qNGzx9bRHjpMO96EQ2hL oXPs6/G/eU33RWCQ== From: "tip-bot2 for Kai Huang" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/tdx] x86/virt/tdx: Initialize all TDMRs Cc: Kai Huang , Dave Hansen , Isaku Yamahata , "Kirill A. Shutemov" , Yuan Yao , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <170205584043.398.16635105990694346142.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 08 Dec 2023 09:17:38 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784734924558784710 X-GMAIL-MSGID: 1784734924558784710 The following commit has been merged into the x86/tdx branch of tip: Commit-ID: 0b2bc38131f02d6fd38695f191bbd8c6109ecffc Gitweb: https://git.kernel.org/tip/0b2bc38131f02d6fd38695f191bbd8c6109ecffc Author: Kai Huang AuthorDate: Fri, 08 Dec 2023 09:07:35 -08:00 Committer: Dave Hansen CommitterDate: Fri, 08 Dec 2023 09:12:45 -08:00 x86/virt/tdx: Initialize all TDMRs 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 Signed-off-by: Dave Hansen Reviewed-by: Isaku Yamahata Reviewed-by: Kirill A. Shutemov Reviewed-by: Yuan Yao Reviewed-by: Dave Hansen Link: https://lore.kernel.org/all/20231208170740.53979-15-dave.hansen%40intel.com --- arch/x86/virt/vmx/tdx/tdx.c | 61 ++++++++++++++++++++++++++++++------ arch/x86/virt/vmx/tdx/tdx.h | 1 +- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index d2e4180..48fb1b3 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -1034,6 +1034,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_args args = { + .rcx = tdmr->base, + }; + int ret; + + ret = seamcall_prerr_ret(TDH_SYS_TDMR_INIT, &args); + if (ret) + return ret; + /* + * RDX contains 'next-to-initialize' address if + * TDH.SYS.TDMR.INIT did not fully complete and + * should be retried. + */ + next = args.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) { struct tdx_tdmr_sysinfo tdmr_sysinfo; @@ -1079,15 +1129,8 @@ static int init_tdx_module(void) if (ret) goto err_reset_pamts; - /* - * TODO: - * - * - Configure the global KeyID on all packages. - * - 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); if (ret) goto err_reset_pamts; diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index dd35baf..c0610f0 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -18,6 +18,7 @@ #define TDH_SYS_INIT 33 #define TDH_SYS_RD 34 #define TDH_SYS_LP_INIT 35 +#define TDH_SYS_TDMR_INIT 36 #define TDH_SYS_CONFIG 45 /*