From patchwork Fri Dec 22 23:52:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 182819 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2483:b0:fb:cd0c:d3e with SMTP id q3csp1395173dyi; Fri, 22 Dec 2023 15:56:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IGxFEg5nYGGA5dnsf81ibbOCu0w3C68AOCD5fOYGFCk/LjVO0upItiJVn6Y8X5VxOOr1Ovf X-Received: by 2002:a05:6808:1211:b0:3bb:9761:7f7d with SMTP id a17-20020a056808121100b003bb97617f7dmr122608oil.68.1703289384811; Fri, 22 Dec 2023 15:56:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703289384; cv=none; d=google.com; s=arc-20160816; b=mSgexWqsy84ooJmmbGEvrfGRLfeijlZV01zOCO3SOrlew5/4v257nKSqp5aKNKKV/3 GCb3w8RBqRdRifxEVJqNS3HdME1HoTlPQhBnQ5napg2v1FiNN4/wpBIC8fluN+5iHcRH VH5inTI50VPiZQLIFpjjdCOLqdeyzwiP0hoKgHZdXVgVVVlBOZT/hIyexlyzF/v1EM+l VhHKM0o4IPaBbTM4ZbpEtbruVr/u1DFD0yhYLFpMImfJ9ewqmA36+Hj+WMIzaKI2ADr1 EgMQorAT78/iudqD4LguB2E0R7JJwKtaDNsdc5KnaMLhpE/oFRk5MUHOySB9bHRcfxEZ lqUw== ARC-Message-Signature: i=1; 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=3KH5f6CWcQidM10Z+WVYilOIAJxlUOn95N5EqJfJdQg=; fh=OvJRnOqsMTm9XoNmEwebcqh9Ud7yh1CTeKAP84ols98=; b=pOpggc9AlyLXQ7INYVQu0deG3/O+QETYxMC7SaAVyYi035EzwLuA5ycmAsHkGTiUli qLZPzAPEQaBcG9FYbPxvq9z7jKG77Uc1EcrOWHD/E4V9RUjlnMjP53ZVAD7GgVM3Mg+U T8PVXxFvSh297QvkyfhsK5glzW527dHukMPE1xUmvk0tr0fSXP1w75wtQICiu18frjQG uk5JOOBbIRaFhBIsHopvcdi6GP0WmvY9KUBBkwigUBMCptbjbe/hzeEm4maOSxOIkbuc tNQp/iqgtCdkL81JhHityihxYkVB8F9mPZVQa4Dj1BzHeRjsQVweY4ZgXArhnA/ZwA7u RbfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WuGeY3jL; spf=pass (google.com: domain of linux-kernel+bounces-10133-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-10133-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id i64-20020a639d43000000b005cdfcb10c49si1105650pgd.868.2023.12.22.15.56.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Dec 2023 15:56:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-10133-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WuGeY3jL; spf=pass (google.com: domain of linux-kernel+bounces-10133-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-10133-ouuuleilei=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 2BB5AB228EE for ; Fri, 22 Dec 2023 23:54:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 48F5B4A986; Fri, 22 Dec 2023 23:52:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="WuGeY3jL" X-Original-To: linux-kernel@vger.kernel.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (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 30E233B19D for ; Fri, 22 Dec 2023 23:52:28 +0000 (UTC) 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.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1703289148; x=1734825148; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=JV7Vomm3hL/vDYDiUsjcPKFfbqW2xkr31p+WaTWWLWA=; b=WuGeY3jLHxg6Cn3fG2lMkuIx2wt8J48/9lc8yXrXM5yZQEvidEP5Kgjv LKlxfCYbUleZmrXNipBKAmvEIk0s0gBVJgVxeL7Ukn5NEcuxEwt8HpwhV wVqtCVXLUqaB4DG7QX4ldJUSjbKboSBhUcGaYcHsI8rIoPRGtX5q/ZgVI wW0FX4kJoa/FwNOyjNJ2cgp0jCnezErHOr21G8fhahLlRXBTxsjy9nf9m OLiK5ci3YlBDxTbsDkWyQ8LI7M9DK5ZtdKPgfQDA86LrFk+BFtO5NtjEo 5hR+Q4tMZhgOQQjx54yygV2RsIe+6QIxh1LE08wXAWhKdNYgN/p5MdnFm Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10932"; a="3414346" X-IronPort-AV: E=Sophos;i="6.04,297,1695711600"; d="scan'208";a="3414346" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Dec 2023 15:52:27 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10932"; a="726961491" X-IronPort-AV: E=Sophos;i="6.04,297,1695711600"; d="scan'208";a="726961491" Received: from jeroenke-mobl.ger.corp.intel.com (HELO box.shutemov.name) ([10.249.35.180]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Dec 2023 15:52:21 -0800 Received: by box.shutemov.name (Postfix, from userid 1000) id 387D010A4D9; Sat, 23 Dec 2023 02:52: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: [PATCHv5 09/16] x86/tdx: Account shared memory Date: Sat, 23 Dec 2023 02:52:01 +0300 Message-ID: <20231222235209.32143-10-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231222235209.32143-1-kirill.shutemov@linux.intel.com> References: <20231222235209.32143-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786028369791162914 X-GMAIL-MSGID: 1786028369791162914 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 4397a6b2b04c..8a49484a2917 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