From patchwork Wed Jan 24 12:55:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 191586 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp1019124dyi; Wed, 24 Jan 2024 06:20:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IHvS2qjaj+sZDznJmatYGdvWzp2FJTkJza1JY4YRBmsfqrBk1BZ1AqKpl2odFkKyalPMWgh X-Received: by 2002:a0c:e185:0:b0:686:4408:a41a with SMTP id p5-20020a0ce185000000b006864408a41amr2445241qvl.100.1706106040217; Wed, 24 Jan 2024 06:20:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706106040; cv=pass; d=google.com; s=arc-20160816; b=JlIAyLinxZzyC02tviRrWGYlOYxYwowznqfpu5wCAFwu0xQavQDSSRZJcZyl6mFJcZ 3Q4eRB4gxCUYHe1JED/fZPi/c6K65Hi+2zu5L2vtmvQ4u8yRdcVSvuz/GI61ZDNhOb3m +2ymZLVp3wxHayq2BYFBusGSk26rybKq7WILIARwN0e/3/4qjT4sjSQul+eQ2eFsdKs1 CKIGSCg0IwbZtk4lpROpRG63gUa2cCJvCheJxU4eWKLMjlu/zE3fS1rSsK2nAB6qu6Yt uRm1oqngDJ2a+oB2niUHCCYdIITV43bKbwqSZPYxYwGqOqkjZV6WzDtnBI6Z9KePFirh 2bgQ== ARC-Message-Signature: i=2; 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=QbNVvvHkrGHh7BR7Qwt2J0OgUR8dAXMlMSzqos0/4mY=; fh=OvJRnOqsMTm9XoNmEwebcqh9Ud7yh1CTeKAP84ols98=; b=b8GdH1262O1zsoywc3P1tZLGt9/E2pB/dZ46EqjPLXzLD3qkjJsqCxv6tz3uYTv8MN kPzAr27GWmnqjmr5sRIHmPjrSBo23yp5bcCcZhgC5BD8OgdSMAV+bCF7mptCx5gGMbpi UJvsvKW9WMryPwuk/GfoVESb62HnslHTvWO/6ubtPvF5Zo/RgfxPgGOpkI8fB6JBvVfs uzKySFNNm+J17C8EZUo33b4KXHtFRGe2cZRYz8+oGLp8sVKeFp2b+mTbGuYVzk8f1nKz MjEEK0H1N/gwPWoIsr54O2PXKf98yzjhGGj02Cf69anLyOHksjygedmNIOPgroNz0lru j8zg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TmVphWRR; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-37040-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37040-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id f5-20020a0ccc85000000b00681778d1495si10586830qvl.47.2024.01.24.06.20.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 06:20:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-37040-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TmVphWRR; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-37040-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37040-ouuuleilei=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 5224F1C27793 for ; Wed, 24 Jan 2024 13:01:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5FEBA7C08A; Wed, 24 Jan 2024 12:59:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="TmVphWRR" Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.31]) (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 4999C77654 for ; Wed, 24 Jan 2024 12:59:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=134.134.136.31 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706101149; cv=none; b=qv4EC1oF3ngdxkrmZcMgVZ/caJaWg1sJjc/bnwgv9ror5a+5UocMGW5f0b3qGwtSuCkd3SVYNh945IWzm6CXTTA4YdkVCyGNAHJX99uQrsaoUja/YfVo34JY1KubzYOU3WAxsuPHH+d57uh02qQ2vMvgRYOaGf/YRWuJUFvRUOc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706101149; c=relaxed/simple; bh=ttorKfrAsCfwHjNDg0dNsgpEDrTBddN8JkJtVUzul2k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=go0jV6r4OfeqzqEBQ4z1PbZJirw9FmMkdbLYw0MELjcmweCxXPdo9sU2nlWKsl62ldH8ZIt2tx2TIPBxzqd2ALKtPJo3JsLl4aaPVPrinPxGS63LXrlImI+hVQG8jH2QgvN3Gw/hj+fuydWqbZWBQJP+sZKBvrNXDNJr7u9w0qY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=TmVphWRR; arc=none smtp.client-ip=134.134.136.31 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706101148; x=1737637148; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ttorKfrAsCfwHjNDg0dNsgpEDrTBddN8JkJtVUzul2k=; b=TmVphWRR7I+yNOPKnJhmoYqbA9DV9IeeDCJdN6iQh00riT0T1JWhYvAe R8OoJdQe1dstzFxD/ZItrK6TBioLkMxuZyXmR0IShIEvT4wUuq5+5nmMQ oye00VfFv4SHvS6wTqMLBfUA2KFZ7giyD9EDxbkc6Z8XJqOaKHBnDivc7 9LukxAISjSLMjAm6sv6MK+q2egMCwKozt30toVusSyDIzXk+V/Fq9PTWs NG2FKSOq24PAWFrtlDo9tinESvECl4ipNlHxOzkr1cyylPKZU0E06aFWM 1DPsDx/fad5Ux2iZvuX87c2uU9IITidE9TsEOaRvB5dROYBD+O9PFOqwm g==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="466110888" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="466110888" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jan 2024 04:59:06 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="735924196" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="735924196" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga003.jf.intel.com with ESMTP; 24 Jan 2024 04:58:59 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id 79DB976F; Wed, 24 Jan 2024 14:56:02 +0200 (EET) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org Cc: "Rafael J. Wysocki" , Peter Zijlstra , Adrian Hunter , Kuppuswamy Sathyanarayanan , Elena Reshetova , Jun Nakajima , Rick Edgecombe , Tom Lendacky , "Kalra, Ashish" , Sean Christopherson , "Huang, Kai" , Baoquan He , kexec@lists.infradead.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv6 08/16] x86/tdx: Account shared memory Date: Wed, 24 Jan 2024 14:55:49 +0200 Message-ID: <20240124125557.493675-9-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240124125557.493675-1-kirill.shutemov@linux.intel.com> References: <20240124125557.493675-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788981847136641363 X-GMAIL-MSGID: 1788981847136641363 The kernel will convert all shared memory back to private during kexec. The direct mapping page tables will provide information on which memory is shared. It is extremely important to convert all shared memory. If a page is missed, it will cause the second kernel to crash when it accesses it. Keep track of the number of shared pages. This will allow for cross-checking against the shared information in the direct mapping and reporting if the shared bit is lost. Include a debugfs interface that allows for the check to be performed at any point. Signed-off-by: Kirill A. Shutemov --- arch/x86/coco/tdx/tdx.c | 69 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 26fa47db5782..fd212c9bad89 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -5,6 +5,7 @@ #define pr_fmt(fmt) "tdx: " fmt #include +#include #include #include #include @@ -38,6 +39,13 @@ #define TDREPORT_SUBTYPE_0 0 +static atomic_long_t nr_shared; + +static inline bool pte_decrypted(pte_t pte) +{ + return cc_mkdec(pte_val(pte)) == pte_val(pte); +} + /* Called from __tdx_hypercall() for unrecoverable failure */ noinstr void __noreturn __tdx_hypercall_failed(void) { @@ -821,6 +829,11 @@ static int tdx_enc_status_change_finish(unsigned long vaddr, int numpages, if (!enc && !tdx_enc_status_changed(vaddr, numpages, enc)) return -EIO; + if (enc) + atomic_long_sub(numpages, &nr_shared); + else + atomic_long_add(numpages, &nr_shared); + return 0; } @@ -896,3 +909,59 @@ void __init tdx_early_init(void) pr_info("Guest detected\n"); } + +#ifdef CONFIG_DEBUG_FS +static int tdx_shared_memory_show(struct seq_file *m, void *p) +{ + unsigned long addr, end; + unsigned long found = 0; + + addr = PAGE_OFFSET; + end = PAGE_OFFSET + get_max_mapped(); + + while (addr < end) { + unsigned long size; + unsigned int level; + pte_t *pte; + + pte = lookup_address(addr, &level); + size = page_level_size(level); + + if (pte && pte_decrypted(*pte)) + found += size / PAGE_SIZE; + + addr += size; + + cond_resched(); + } + + seq_printf(m, "Number of shared pages in kernel page tables: %16lu\n", + found); + seq_printf(m, "Number of pages accounted as shared: %16ld\n", + atomic_long_read(&nr_shared)); + return 0; +} + +static int tdx_shared_memory_open(struct inode *inode, struct file *file) +{ + return single_open(file, tdx_shared_memory_show, NULL); +} + +static const struct file_operations tdx_shared_memory_fops = { + .open = tdx_shared_memory_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +static __init int debug_tdx_shared_memory(void) +{ + if (!cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) + return 0; + + debugfs_create_file("tdx_shared_memory", 0400, arch_debugfs_dir, + NULL, &tdx_shared_memory_fops); + return 0; +} +fs_initcall(debug_tdx_shared_memory); +#endif