Received: by 2002:a05:7412:b101:b0:e2:908c:2ebd with SMTP id az1csp2541830rdb; Wed, 15 Nov 2023 04:03:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IG8N5eqOcCupB0TL4ytKpXL/A9W0bbVO2jgjV/aYEy26QLUyjU90qLkiZ5OXLoiG57xNLRN X-Received: by 2002:a5d:8ad1:0:b0:7a9:5e03:a785 with SMTP id e17-20020a5d8ad1000000b007a95e03a785mr11952087iot.15.1700049805009; Wed, 15 Nov 2023 04:03:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700049804; cv=none; d=google.com; s=arc-20160816; b=Op8+nrweBMXhD1lzweo2USX3S64CW2dpofT7fINNrhI60hIUcgDAy1Mb1ZwD0TJzUr 2Iif8YZ9z1nbyCeBociasDn4f5bgMaTuGgJtCitYvEobv712XhYPTow9RzcHUFGc+75c OT2ABzsNJHYXm7dT14IE1yUusEugyP/hnTtoITPO6W+02GNwiipJMY9DbcxJ2yCIs4An TJKnftPeOJHnK59KQpy9hOSlpYeqVLqsgn5QASmqkKCAaTeaBL88zxuihUR+aRgEGcgz vxyu63D/DzPPGicUbaD23gubN5cvzP5QDZH8Jq4EvqI19H1WRABCZ19w24OCjkjt0XEs cT2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jckTfOuU6C/zqq3hd9sz+WgLGOiGExOGswLakyMmjmU=; fh=OvJRnOqsMTm9XoNmEwebcqh9Ud7yh1CTeKAP84ols98=; b=kJbx6YijiwNl8OJzX6lRY7ibgLeppOsMb2z8vAZdip/HIJk7dsB9kFTb+Qfrx+zzdN +PpnBdnq03CwVqzUk3G6MM5Ou2lMcFmifeFmkgxBi7egpyrFIACyqItmKMzCOAaTGqjI +gcTi/pIjTlRDgCAc9OjRZEo7+7Fn2DiHOpsdx2kNVUEKz2mA5K975MnOzkQFSZqjlYN 0qRZ3bgd3bmAjbpFa7ZZ4hi6As0hWCykrllFYnsperNI+31kpXulagCcliQOSi75Zkxr 50yun4FQPwuUHtjD190M8rfIXtq8Io/JQnow5JsnrSuDiZzKBd+Wty7dgBk2lqCeHR/3 LGGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Y0L7Agpq; 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=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id x12-20020a63170c000000b005bd3ca6c398si10058173pgl.736.2023.11.15.04.03.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Nov 2023 04:03:24 -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=@intel.com header.s=Intel header.b=Y0L7Agpq; 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=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 143C1815D7DA; Wed, 15 Nov 2023 04:02:24 -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 S1343720AbjKOMCI (ORCPT + 99 others); Wed, 15 Nov 2023 07:02:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343713AbjKOMBd (ORCPT ); Wed, 15 Nov 2023 07:01:33 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6647E130 for ; Wed, 15 Nov 2023 04:01:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700049689; x=1731585689; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mn/ktomsmTvfB65J7SyJ0k/NJICrPsNe3CbwcOdaxtI=; b=Y0L7AgpqrBSjS6f0mtY8cqGC2N/c6L08BJogxaT5RQNvYYblnoGNOnwB P3NIvdfm6GUSQadjBNHo47Ec2Mhpewn30OyOQbpHK99XOn20hXPPBi4eJ 0D3d5U5Elkn4Yf0PtgoeCqe/+an0jlQKOjbbvSu2XgIQrMBjahkIDyR1D YN+HofW2uMBUUqdSCBcDwm2O4uy9ocpfTgJ61lQKRedjvJK6Q7AC+17DA w05tjRDPFMoCG79k0KuWG7eKw8UIGE1jGPZNI8B+tmll250J6UapVN/0Z fSgHmyXilGtzp7qrgoHysEIwMmg0bMrnynMjmKTpzGPEAlO88xyyJQm21 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10894"; a="12411950" X-IronPort-AV: E=Sophos;i="6.03,304,1694761200"; d="scan'208";a="12411950" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Nov 2023 04:01:28 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.03,304,1694761200"; d="scan'208";a="6377705" Received: from mituomis-mobl.ger.corp.intel.com (HELO box.shutemov.name) ([10.249.44.135]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Nov 2023 04:01:22 -0800 Received: by box.shutemov.name (Postfix, from userid 1000) id 9421310A31B; Wed, 15 Nov 2023 15:01:12 +0300 (+03) 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: [PATCHv3 09/14] x86/tdx: Account shared memory Date: Wed, 15 Nov 2023 15:00:39 +0300 Message-ID: <20231115120044.8034-10-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231115120044.8034-1-kirill.shutemov@linux.intel.com> References: <20231115120044.8034-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_NONE,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]); Wed, 15 Nov 2023 04:02:24 -0800 (PST) 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 2d90043a0e91..039f81b7c172 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 @@ -37,6 +38,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) { @@ -820,6 +828,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; } @@ -895,3 +908,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 unshared pages in kernel page tables: %16lu\n", + found); + seq_printf(m, "Number of pages accounted as unshared: %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", S_IRUSR, arch_debugfs_dir, + NULL, &tdx_shared_memory_fops); + return 0; +} +fs_initcall(debug_tdx_shared_memory); +#endif -- 2.41.0