Received: by 2002:a05:7412:518d:b0:e2:908c:2ebd with SMTP id fn13csp351912rdb; Thu, 5 Oct 2023 07:49:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGwhzPelAQvYSDIwoC0c89WxkIOjqnn2pEt3u+a0d5NkIWc4/iUSMyL9ikYxDezql96S+td X-Received: by 2002:a05:6358:7e49:b0:142:d0dc:a3ce with SMTP id p9-20020a0563587e4900b00142d0dca3cemr5540110rwm.2.1696517390580; Thu, 05 Oct 2023 07:49:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696517390; cv=none; d=google.com; s=arc-20160816; b=X/r5Bjj9y/A+Otm3Nkbm/YGVRa49mKxR96sgAbDoYnpJrFPvfqDSWuEEO79fOx/9WS P0yKQYSaaomP9vC2/aDS9e6RSdyS3WUN7U7VRXp6Aqkvtcjk4XB9/A0ehhQ4A5MhpLbB OSlO+miX4AWCYP7CeogrSpoO9x28Vvchi3RZt9f+W2hQV6rhjShJcPYynmZb1ldjeXZk vc7+tn4rDbugQvewYUxn18u8QbWTsSSYSdEnzLvRKvfmWYPx86Iw/VBvhhv/D1vHP/ov IsX//SaGZI+cyBb1VdzQj6tSRkdWSubQek9RAO+reVI4pu3CDuZXK2wbUZ2aiVLKw9sb U4/Q== 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=jHiamILBM6KlSfdzrVgbjC9434iu2/bUgSuoF9ZTY+g=; fh=F1gpqSS/HYttK+doOKuW4yrAifo5qykyq1MCI9SIQmQ=; b=Fcaj9nPjiM0K8haIHAmdWeJmkIWz3HumGIVolBLmvcWKFp++3aUcpsWpgrvf0SASD8 SrVkW+ORG2Jl32IsDlb/PbCRkaxau3XCSQjO+gSDMtpcft9P45gstPrk3S14Ae40+sd/ gQqYqINES6yceF4SyeH4NixFyRas8WgSoqJLXU8jeH0sdA57nqAIsd9/uR3Anw3VGKOs V2O5HX1Z3xQHeupZy79OAaY/XNn6YRCLWz2VX2Nfd7jZfpjArv2RXUMV6ruLf+VMabxa XHnvjnypdca43joJUCPYhSTOxyU2OM0CpgQTA/O+261+0qg0tQrbKtaSg83y8X1eWDvt ASDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=L1wO4vVp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id x10-20020a634a0a000000b0057745b2d018si1505225pga.390.2023.10.05.07.49.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 07:49:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=L1wO4vVp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id 23BA980732A5; Thu, 5 Oct 2023 07:49:12 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238653AbjJEOnq (ORCPT + 99 others); Thu, 5 Oct 2023 10:43:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237219AbjJEOiu (ORCPT ); Thu, 5 Oct 2023 10:38:50 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F16FD7295 for ; Thu, 5 Oct 2023 07:04:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1696514675; x=1728050675; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ja5J3yNUmltgYZsQ6ex9qPK0e1eU53KBqHupwbwonDY=; b=L1wO4vVprZSzdywwOp/YBzgs9eTrK8qx6VKQ8AOYWR6Bh56GFtN3Negw dboSAyDrWFejxKCWIPpKT+feM63Dyvz/RzUSE9XK/i8Fyu1rIs586R6NF c5B9nAzADeYoeTR+lG+q3tIPfh0NlVn/+VLvVJebDCdoEl0Njhbcqf3AX Cu9YpN+wfJ8DqpQ5SV9Ck35DqnqmFweFqQUmue+2CrTJ1IwoatTQqzfkL GEy8EG20evEiaCm1B8DkueFt+p3J9NBApsgIaSzYWkhZoKfqLKxzuzose LGUaxYPUewqF9hakOeEt5coJzhaKv8y8kIVsXbmky6N/LZvGVe/MMCcVs A==; X-IronPort-AV: E=McAfee;i="6600,9927,10854"; a="382357574" X-IronPort-AV: E=Sophos;i="6.03,203,1694761200"; d="scan'208";a="382357574" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Oct 2023 06:14:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10854"; a="817564322" X-IronPort-AV: E=Sophos;i="6.03,203,1694761200"; d="scan'208";a="817564322" Received: from skwasnia-mobl.ger.corp.intel.com (HELO box.shutemov.name) ([10.251.222.71]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Oct 2023 06:14:23 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 6748410A153; Thu, 5 Oct 2023 16:14:14 +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 , kexec@lists.infradead.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCH 09/13] x86/tdx: Account shared memory Date: Thu, 5 Oct 2023 16:13:58 +0300 Message-ID: <20231005131402.14611-10-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231005131402.14611-1-kirill.shutemov@linux.intel.com> References: <20231005131402.14611-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,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.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 (pete.vger.email [0.0.0.0]); Thu, 05 Oct 2023 07:49:12 -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 target 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 | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 46022283d955..56e152126f20 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; } @@ -871,3 +884,57 @@ 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; + } + + 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