Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp3081227rdb; Mon, 4 Dec 2023 16:47:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IGLPArGDT4M5oSjX7Sp5k3JCSAJq6EZdsKopqhpFsLHPQdC9Nk412EkAOQxATzTVYvoZIGB X-Received: by 2002:aa7:9849:0:b0:6ce:6d8b:275e with SMTP id n9-20020aa79849000000b006ce6d8b275emr139769pfq.43.1701737236627; Mon, 04 Dec 2023 16:47:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701737236; cv=none; d=google.com; s=arc-20160816; b=JRW5SvuKCivWS59tk41+a8Ryk/AGvUc9/Fb/ptbwvjwSswgZ2/ervwONExTeY0aRek S6ZSQ6eLEknnNEP90oqSK1bihHcw4fi8danIwrckG8zDc8ZwGYuANDiigGu7U86tidZW FsHb1YZC53Ir/cLbPr8MTORekDhVv29Ug838mE7CWsWv1whwJKyWDUNyZ44pmXaf5mQy 3dglrN2cjT/3rzdjWhAQCvYEZMpCIbWNkljEtJpRilcYmJL2Z4mS+BTAbKawQ9Or8hLd R8LQhG/1UmowcXk1gG0nhOeAO0yeY5eeTLihq5JztSZSfV65501ZctWuMt0BG5AEmpOk wwhA== 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=DO8HgMM7A1WQh3RKV8RxDPAx0l0wzxYunP1lb1gYofM=; fh=OvJRnOqsMTm9XoNmEwebcqh9Ud7yh1CTeKAP84ols98=; b=vTwFL/YpmQOUl72uig8eTPRdTyWrN7FPrCRLeeDcqki6Fm2+K1iJLVgpuUUx/mN9O1 EXz3pCLlbpm68ynV2+2SeP2Ez9a5+wBlUlt8BNlpHqRohwfSi1in78GhVP8FIIkhbTjx hlzhHU+K1t4ste5pR8FEzOFMCvv2FK/Hu4RG1RMWDM/MVPHG5vp2/sNqHWAug4RnN9mk QyNNSxVw8KS0HVUuCJ3pUofn5Q0HpDB5/RhfHofTs30F17k9y+9mYboawXg3WzY84cX5 usevAe1VhLybUwBTUewHp3VoPtTpb7yImnra5vuEXtpZq4BrHZYeIDAajQxppmrtkBaE F26g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KJ8zsBnV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id t66-20020a628145000000b006cbfbb564edsi8419936pfd.129.2023.12.04.16.47.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 16:47:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KJ8zsBnV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id 776FC804C656; Mon, 4 Dec 2023 16:47:12 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343727AbjLEAqJ (ORCPT + 99 others); Mon, 4 Dec 2023 19:46:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343675AbjLEAp2 (ORCPT ); Mon, 4 Dec 2023 19:45:28 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02C11C4 for ; Mon, 4 Dec 2023 16:45:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701737134; x=1733273134; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RP/Va3aoGG5zb/Y76A/szY23yia8vizTXE1CbaIASh0=; b=KJ8zsBnVxD9AsmsdE6Q99B38wUuo1LXVyQp78+X3YfkB3V7PptrkLrAT GzrzG/92uHhQ+s9Zg1OKO7TLKjqls+FYDaAFp7zgDirfVsWUxMoX1s9qO WPz8QiBq9ZGFaevdItCl5DHDCX3piq5jXXl7igw7E4WuoiQW48WVA0+00 EbkoNkkkgQfFHaA2mf+m5lavkB4S5mcX3ppvExsAtWN3s5431j+TlXzCN OTDC1LWumfQCuG60715jN+O9v1/ci3jKszzQ3euUG4RrxVc2PpIXAznJ3 RBx9Jq7prt+IIsfDOBrareb7vr8mo9S3rvAfa8OH+/GvkEt9OrCx2XvlD g==; X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="392688666" X-IronPort-AV: E=Sophos;i="6.04,251,1695711600"; d="scan'208";a="392688666" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2023 16:45:34 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="888704404" X-IronPort-AV: E=Sophos;i="6.04,251,1695711600"; d="scan'208";a="888704404" Received: from abijaz-mobl2.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.61.240]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2023 16:45:30 -0800 Received: by box.shutemov.name (Postfix, from userid 1000) id 8959010A44A; Tue, 5 Dec 2023 03:45:20 +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: [PATCHv4 09/14] x86/tdx: Account shared memory Date: Tue, 5 Dec 2023 03:45:05 +0300 Message-ID: <20231205004510.27164-10-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231205004510.27164-1-kirill.shutemov@linux.intel.com> References: <20231205004510.27164-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,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Mon, 04 Dec 2023 16:47:12 -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..fcc159497554 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 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", S_IRUSR, arch_debugfs_dir, + NULL, &tdx_shared_memory_fops); + return 0; +} +fs_initcall(debug_tdx_shared_memory); +#endif -- 2.41.0