Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2375263rdb; Mon, 12 Feb 2024 02:48:26 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVSFPl67aiTmdJMBlnGhBO3LstDMZLb8lBh2DPfeEm42QxTAUJ2fK1FqWpNDJNNVU1wRK5rjJdHi887WD9FMBkdLAVFs9QzQxVt69k+3w== X-Google-Smtp-Source: AGHT+IE3wdAeA/75TYw+2blkcRiaJjKhzc0l+47VKL1FJMHuNvOIgjxMkwadxOk4GSo5XpAc93WR X-Received: by 2002:a05:620a:40c1:b0:785:b43f:644a with SMTP id g1-20020a05620a40c100b00785b43f644amr9057219qko.28.1707734906405; Mon, 12 Feb 2024 02:48:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707734906; cv=pass; d=google.com; s=arc-20160816; b=LmTSQ+tEA7Wub0T81GN/DMQ50Ykfj60YG8OO2sEgC1Zx7cQvPdlgk7hAkjtzNiXQEo HWwIGKSUKxJojqpGoPEB4XVq3cNSgSw60ahZzQf/Qu+oI/NRHytCeB/2hyhvGRRMZPsY Iwoh5HL2dGtf3dgmNldzhgaESJOusPwcvNwnGJsBYLfkR/6Y2NBdfYa26s2ADA5KwNGh 8oU5jW7KO3EiaWmGzInwRExz2PiTBlHNKftKNnmRIapg1cyDZzUvF7+/uEOOT52+c5XZ mT3Ybi4Td0WI9HbNJZq0plSXNrqwJMoalvrlDFHakiUauw3t3WF23Z0vJoNTKjsyWdpu w3KQ== 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=Tk2l6iXs6RbTgoJl7TkkzEnC9fC4m+6WgZHWZC0zQSg=; b=XiedmYiVb3A46LBgJdp2yHgdZQLFKvsD1Gg+9MRytzIO3ondsOjTogIbX/zNu574nF VVMvfWgFRWUHYhZSGqWNLatMddcG5R+2xZs2UuKc1fzqABmjhymO6dRG7CS0nJCr7Mrl xrDFTJXdhKpFhKzt3iZ2Mul6Sz7q9jzFaVYRX94VwJB5lEtENNRwuilJ4wqAdNSqZzBi ODRQOlNhCzA3IzRSW1M8N8+pK1MsTmg1qhjC1pzxWkfdfZOGFsmM7OybY9G0mT7BuNY/ JKhGEIkuGRsRn8cKnvNhJFO8+4gJLehKUhFSnAr9fv6gZ21t3eOAnFy3dcfOzFb3AE3U vJlw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jY57t3qY; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-61358-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61358-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=2; AJvYcCWB91XD4MWOhEjNHJCUO+MhvGidEvgL8AoLs3m2RbZ3ec9WECfpI6rmcucjTG44ZUApHvKbrHoqDEqNxH5Cz8w77hzU0UxhIDA9dkOEtg== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id b6-20020a05620a118600b0078394900b65si5464767qkk.257.2024.02.12.02.48.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 02:48:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-61358-linux.lists.archive=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=jY57t3qY; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-61358-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61358-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 200171C227AF for ; Mon, 12 Feb 2024 10:48:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 29F603BB4C; Mon, 12 Feb 2024 10:45:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="jY57t3qY" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (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 5566A3B191 for ; Mon, 12 Feb 2024 10:45:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707734710; cv=none; b=rs6Wg0bhYdn7lrVgyW4GgJxTuffhwBtMg6RNeemgDha/G0jFt4NP7e2u/0+8kzB/DK22hyntpS1XrSNPYHWioddT69IMdNEhwDYdVGjm4guNPfMH/oxjwe1eRhDYm3kYEqLdh+KGrLozTCSqlImqMI6U5No1miM1SwLJRQUIMxk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707734710; c=relaxed/simple; bh=ttorKfrAsCfwHjNDg0dNsgpEDrTBddN8JkJtVUzul2k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Yw+uVtD+wZuIlUamYAMWXEoQ7IpjJQ3NRnQ93429jU6XN1XPHyvDWJD7SYeeXGIS4hull4sWlEzEl3K9qHCbQiO3flvRR1mrfTfxZn+uwpIz8T/ITZ6DPaZc1Vp09ZQMcpUAjBBSZFnaHo823cc0HnAdiDXkofOXrxMRYUkPEDU= 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=jY57t3qY; arc=none smtp.client-ip=192.198.163.17 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=1707734707; x=1739270707; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ttorKfrAsCfwHjNDg0dNsgpEDrTBddN8JkJtVUzul2k=; b=jY57t3qYL1YgZPvA3KmwHJC8ugSV5cqWXYdxz5wXsGvJVUbq8RLyZV+z K1zPqp9ye+QdowjxCsbVlvJK97g50II/pZKIAuOu4jv+3634NmK4SwLYM I3aBcJv8660NLOFh1OgTf5YagMTGsv7vvkeOJoYJ0Qw4LmReUwmaeBC4q MGTVsqrwSkQUvMxiwi66EDnf3CJMjnfComelVHZ+ioLlWWS0brEWvgSVy qI9g3D/sH9E9EALLrMcWCO0I44wiaMJ2F+0YAtPAbT1urIqr+Y0QPULby EXJN5ddd9tnGBRR1cMupBbbEBtKUu5M3xqrwlJvBt5Jt6DQhsn2GpStJy Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10981"; a="1585068" X-IronPort-AV: E=Sophos;i="6.05,263,1701158400"; d="scan'208";a="1585068" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Feb 2024 02:45:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10981"; a="935035601" X-IronPort-AV: E=Sophos;i="6.05,262,1701158400"; d="scan'208";a="935035601" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 12 Feb 2024 02:45:00 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id 54901541; Mon, 12 Feb 2024 12:44:53 +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: [PATCHv7 08/16] x86/tdx: Account shared memory Date: Mon, 12 Feb 2024 12:44:40 +0200 Message-ID: <20240212104448.2589568-9-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240212104448.2589568-1-kirill.shutemov@linux.intel.com> References: <20240212104448.2589568-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 Content-Transfer-Encoding: 8bit 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 -- 2.43.0