Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp131711rdd; Mon, 8 Jan 2024 21:47:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IHfB6nZTntpKCjn7ZGw/bj5K6LEaKE065XUGWphCceyGfb4DyqqfLuKB7jN/SZamYrdPK8e X-Received: by 2002:a50:8ac2:0:b0:552:e43d:cc7c with SMTP id k2-20020a508ac2000000b00552e43dcc7cmr2691017edk.16.1704779247416; Mon, 08 Jan 2024 21:47:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704779247; cv=none; d=google.com; s=arc-20160816; b=A+o+h0NPk/NyjL3SLpc9lqzjtwHeCdNVn04gE0yZy7uiZ+DcMhK+ljywKuFG69mt3y lIPziLjMwwjScrK2iGEVmnnG809fOXzOrMGi5gtogaMt1ItrPZP4adoQJzyXTGlvek6z 9ZDaXTeGzM/t78nWb7Wwzy9fUaJ2DOUV7tufucYXmh0AJiRm6nCutLtIYU7LR8K4laGp uv7umNuHcMYOUjAyjJXxi/aBIcA2lPb5ds5E8eNlpI9KXA1/fBEBYpjlVLlMEE5cAb1F qqMF5I1y1E39PKE5VamQVhxtAF75ig/5ue3DLpQWMSIPxt8hk7C0yDmKOB9JoYZm5BU+ ++LA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=DN5tiRem7flbzk7PuO92gmk8xb1Lr7IDFyVd5hsE6p8=; fh=xLT4vyjDhOResVFvAN9vmEYxTOsga+bYG9LTuSfGN8s=; b=dA40Zgt17MYnGoZ2ldkVj/k9PLnvCsobs7hxM2H/NokYy649s63qaZL+R4EG48KhEy 9uqgGiWqHVMGkMEOxcQz6u1JZewpwEpt4IpPR83VV/kr7wYerMUFV21J+5l1dZcOAP0n 0bejMlPMg4mrICSW37Gesr3Q1ODNAhE1fA90G4D6XRG7mhXbuXnr6Jc6EftAInSuGnr9 1mTpUDYmepvFzfaVemJAfQxzpwpKwaeOH53V7uLPGTeDVLKJGCGV7Q5rdkjr89QjIGc8 3sI4OL1twGbCSTGaEWperAxGSUXP0z2OolMGAGgetI6FEfgM7RoqD06a1ZElKq3qRiSH /Flg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LGP+knpB; spf=pass (google.com: domain of linux-kernel+bounces-20414-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20414-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id b7-20020a056402350700b005572a9c667esi487683edd.482.2024.01.08.21.47.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 21:47:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-20414-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LGP+knpB; spf=pass (google.com: domain of linux-kernel+bounces-20414-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20414-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 2A9531F24644 for ; Tue, 9 Jan 2024 05:47:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B107E17C2; Tue, 9 Jan 2024 05:47:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="LGP+knpB" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 73BD78F48 for ; Tue, 9 Jan 2024 05:47:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704779232; x=1736315232; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=0jk4H4zTEKceAubMlnxoOuNCCA0OuhbNOC9tdrpj7ig=; b=LGP+knpBFMvEpf/3ffGvLO5apGUzAkiXdQikt2b8odFCs+y1MP8dhwkt FYS7/04lWfFoAywjhGmI8RPzloW3qtgAcea1Cx3NNo4gyaWXpqIjqeyIZ UBjvDA9g0IqmBWq98XTxTazDWYmm+p4Fvbj+o5Sh6d/MVXQkXdSeYAg2Z 0+LaHh5FsvjlIiPPMpBvlkBoE+fovw3FHz1KY9pIn23HG5gMbLdqi7Mhx vs5Tuq6pK5Qc75NifhtXDGFMnxgkdZ8dc9JOYVzMCmErwCxc19Eha80t/ MqQydhIrWySR3NGd3xUB3MzVuMGCwTJTTe3FH0peOuysdmSk4ZCKxA+sx w==; X-IronPort-AV: E=McAfee;i="6600,9927,10947"; a="19591419" X-IronPort-AV: E=Sophos;i="6.04,181,1695711600"; d="scan'208";a="19591419" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Jan 2024 21:47:11 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10947"; a="1028638035" X-IronPort-AV: E=Sophos;i="6.04,181,1695711600"; d="scan'208";a="1028638035" Received: from ubuntu.bj.intel.com ([10.238.156.105]) by fmsmga006.fm.intel.com with ESMTP; 08 Jan 2024 21:47:08 -0800 From: Jun Miao To: kirill.shutemov@linux.intel.com, dave.hansen@linux.intel.com Cc: jun.miao@intel.com, x86@kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH] x86/tdx: Optimize try_accept_memory() to reduce 1GB page accepted failed times Date: Tue, 9 Jan 2024 13:48:24 +0800 Message-Id: <20240109054824.9023-1-jun.miao@intel.com> X-Mailer: git-send-email 2.32.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since the current TDX module ABI spec TDG.MEM.PAGE.ACCEPT Leaf show: "Level of the Secure EPT leaf entry that maps the private page to be accepted: either 0 (4KB) or 1 (2MB)". There is not 1G page accept dynamically, if every time try to accept 1G size but always fail, then cost more time from two cases: - When size < 1G, judge failed return 0 - Really TDCALL 1G failed when size >= 1G So skip the 1G and optimize it to 2M directly to save time. Run the eatmemory with different memories to get the cost time as follow: [root@td-guest ~]# ./eatmemory 8G Currently total memory: 100169027584 Currently avail memory: 99901911040 Eating 8589934592 bytes in chunks of 1024... Start time:1704699207487 ms End time:1704699222966 ms Cost time: 15479 ms # # Compare with/without this optimization # # Hardware: ArcherCity Sapphire Rapids 128cores # Test eatmemory: https://github.com/jmiao2018/eatmemory.git # Detail test log link: https://github.com/jmiao2018/eatmemory/blob/master/log-tdx.txt # # Accept Memeory Sizes Before(ms) After(ms) Trigger 1G Failed Times Reduce Time% # .................... .......... ......... ....................... ............. # 1G 3414 3402 751824 -12(-0.035%) 2G 3853 3804 1015126 -349(-0.128%) 4G 7773 7561 1557834 -212(-0.281%) 8G 15479 15173 2633686 -306(-0.201%) 16G 31527 30379 4785649 -1148(-0.378%) 32G 65058 63723 9087686 -1335(-0.209%) 64G 133379 128354 17693366 -5025(-0.391%) Co-developed-by: Zhiquan Li Signed-off-by: Jun Miao --- arch/x86/coco/tdx/tdx-shared.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/arch/x86/coco/tdx/tdx-shared.c b/arch/x86/coco/tdx/tdx-shared.c index 1655aa56a0a5..1694b7eba93b 100644 --- a/arch/x86/coco/tdx/tdx-shared.c +++ b/arch/x86/coco/tdx/tdx-shared.c @@ -18,7 +18,7 @@ static unsigned long try_accept_one(phys_addr_t start, unsigned long len, * Pass the page physical address to the TDX module to accept the * pending, private page. * - * Bits 2:0 of RCX encode page size: 0 - 4K, 1 - 2M, 2 - 1G. + * Bits 2:0 of RCX encode page size: 0 - 4K, 1 - 2M. */ switch (pg_level) { case PG_LEVEL_4K: @@ -27,9 +27,6 @@ static unsigned long try_accept_one(phys_addr_t start, unsigned long len, case PG_LEVEL_2M: page_size = TDX_PS_2M; break; - case PG_LEVEL_1G: - page_size = TDX_PS_1G; - break; default: return 0; } @@ -55,11 +52,14 @@ bool tdx_accept_memory(phys_addr_t start, phys_addr_t end) * Try larger accepts first. It gives chance to VMM to keep * 1G/2M Secure EPT entries where possible and speeds up * process by cutting number of hypercalls (if successful). - */ + * Since per current TDX spec, only support for adding 4KB or + * 2MB page dynamically. + * / - accept_size = try_accept_one(start, len, PG_LEVEL_1G); - if (!accept_size) + if (IS_ALIGNED(start, PMD_SIZE) && len >= PMD_SIZE) accept_size = try_accept_one(start, len, PG_LEVEL_2M); + + /* The 4KB page case or accept 2MB page failed case. */ if (!accept_size) accept_size = try_accept_one(start, len, PG_LEVEL_4K); if (!accept_size) -- 2.32.0