Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp1092242rdb; Fri, 20 Oct 2023 08:13:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH3ERsUUK5LqxlfOm+R5hwyxCjyL/IH0DDjr8vIkbwnCOY809nyWDk5PtMPdTX4swieOZx2 X-Received: by 2002:a05:6a21:a595:b0:155:1710:664a with SMTP id gd21-20020a056a21a59500b001551710664amr2975314pzc.18.1697814812623; Fri, 20 Oct 2023 08:13:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697814812; cv=none; d=google.com; s=arc-20160816; b=ic7fe4uxrd6kucYRNJF2Svd7WbfGcgbJ/szh/TznUl47cW2PdFt/0wQA6aeikjLf3H nK10xVrcu8Su8afm6+gO+iistbtKHYaKR0+YkrNzb22HA7WnYupOOtirOdh37uitOZCN mmh8YU0sqwI3wAzdXOQWLlQoc7oVPlnQTCj/gXX7jJgvQB/1Zh7cumRQUHx4X7mf+DKS J/XTSukuy63KUz3Nzcus3U1JIb6lx2UBjGhgC+XQk/EdC5Pf7+cThvr0+NT9EMsapl34 +FebTu8zNzk57VGixBR9g+qGO5Kd8oCfA42rtWqcGLTIyt2lTRykgw5KhMQWWMMg8giX jgvQ== 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=/SKC/+EC7AOik7UGT6LEbPAbobVNePMgKuf0evkJt3g=; fh=OvJRnOqsMTm9XoNmEwebcqh9Ud7yh1CTeKAP84ols98=; b=q0Kaamu5AvIL08uPsnZw/rTsdSVlJoqTg6tcTJuSqS7BOcWpTuf8U2OYU4/RpnjNXi wy+ZYZH590VH8FFHqeOz5lqWeR1zt5zJJSgVNMRo1UvakJzbny/5mxAbrGCTx21MId6m rdUBm+Avp3j281p189LoFSzyriSYgC78OUkTUGX7H0LKnL+8SCRI7hVmnJk0San9NmN+ 2fvXPT/wMJj2PB4dVs34yReblyzsgpkrLRZIy9aMjOXpbWBw5F/cJ4n3fppXOB7u4q9t CTGJnO0ICgX1zPCIhjnJ6nussGl3o0HukqlgGkkWiKEJqhKx/MqqlCrUXDpTENTjGRpf 561Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JwlV2vXM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id u14-20020a056a00158e00b0068e0fcf8c28si2244293pfk.336.2023.10.20.08.13.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 08:13:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JwlV2vXM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (Postfix) with ESMTP id 99BBF83FA169; Fri, 20 Oct 2023 08:13:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377701AbjJTPNP (ORCPT + 99 others); Fri, 20 Oct 2023 11:13:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377674AbjJTPNL (ORCPT ); Fri, 20 Oct 2023 11:13:11 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1EA0D79 for ; Fri, 20 Oct 2023 08:13:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697814783; x=1729350783; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FQt/ElawTffVXHEXsjLO8M8ZAD2Dp5CO9gICUmaNuMQ=; b=JwlV2vXMepybLmD1RTFGD6hjq0BkCEf2R5P7CfK6dONlApLTCPOSPOqE fPZen6ej1psU/SJyLsjR9Wp0XG3QlYNtDaIFbGhX/LRabHq8N0Z0g7mNW Eg7v8/D8ZL5+SDEBuXz8lSLPo3TPIiUsk2/snsKe3LGlEWn6RWPuMaKCO zrilSgKCmehK4eC38gWYZu4nUdaGQbI39v6ty23gcHtIlNVAiGo2fYKkO xc7D9x56LbQ9KQ3bDwshNT3LpDZ5CVCY8vzqYy7Q3a0yD+ZNVAGDIpqhm 2Uud2r0HNgN0r+rrjJKPNmVPlGAKHy8wuKpG50ECTZEyEor5XoHcibCd0 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10869"; a="376893679" X-IronPort-AV: E=Sophos;i="6.03,239,1694761200"; d="scan'208";a="376893679" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Oct 2023 08:13:01 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10869"; a="761080289" X-IronPort-AV: E=Sophos;i="6.03,239,1694761200"; d="scan'208";a="761080289" Received: from dgutows1-mobl.ger.corp.intel.com (HELO box.shutemov.name) ([10.249.39.237]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Oct 2023 08:12:56 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 3BB5B10A29A; Fri, 20 Oct 2023 18:12:45 +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: [PATCHv2 08/13] x86/tdx: Account shared memory Date: Fri, 20 Oct 2023 18:12:37 +0300 Message-ID: <20231020151242.1814-9-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231020151242.1814-1-kirill.shutemov@linux.intel.com> References: <20231020151242.1814-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email 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 (morse.vger.email [0.0.0.0]); Fri, 20 Oct 2023 08:13:28 -0700 (PDT) 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 8897d3cc8a15..587bdeb88afa 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) { @@ -799,6 +807,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; } @@ -874,3 +887,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