From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
To: Dave Hansen <dave.hansen@intel.com>, Borislav Petkov <bp@alien8.de>
Cc: Kuppuswamy Sathyanarayanan
<sathyanarayanan.kuppuswamy@linux.intel.com>,
Thomas Gleixner <tglx@linutronix.de>,
Isaku Yamahata <isaku.yamahata@intel.com>,
x86@kernel.org, linux-coco@lists.linux.dev,
kexec@lists.infradead.org, linux-kernel@vger.kernel.org,
"Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Subject: [PATCH 2/2] x86/tdx: Convert shared memory back to private on kexec
Date: Tue, 14 Feb 2023 02:48:36 +0300 [thread overview]
Message-ID: <20230213234836.3683-3-kirill.shutemov@linux.intel.com> (raw)
In-Reply-To: <20230213234836.3683-1-kirill.shutemov@linux.intel.com>
TDX guests allocate shared buffers to perform I/O. It is done by
allocating pages normally from the buddy allocator and converting them
to shared with set_memory_decrypted().
The target kernel has no idea what memory is converted this way. It only
sees E820_TYPE_RAM.
Accessing shared memory via private mapping is fatal. It leads to
unrecoverable TD exit.
Walk direct mapping and convert all shared memory back to private.
It makes all RAM private again and target kernel may use it normally.
Skip the conversion on kexec of crashkernel. It uses own pool of memory
and will not accidentally allocate from the memory the first kernel made
shared.
For crash investigation, it might be useful to access data in the shared
buffers.
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
---
arch/x86/coco/tdx/Makefile | 1 +
arch/x86/coco/tdx/kexec.c | 82 ++++++++++++++++++++++++++++++
arch/x86/include/asm/tdx.h | 4 ++
arch/x86/kernel/machine_kexec_64.c | 2 +
4 files changed, 89 insertions(+)
create mode 100644 arch/x86/coco/tdx/kexec.c
diff --git a/arch/x86/coco/tdx/Makefile b/arch/x86/coco/tdx/Makefile
index 46c55998557d..a5daa2a33531 100644
--- a/arch/x86/coco/tdx/Makefile
+++ b/arch/x86/coco/tdx/Makefile
@@ -1,3 +1,4 @@
# SPDX-License-Identifier: GPL-2.0
obj-y += tdx.o tdcall.o
+obj-$(CONFIG_KEXEC_CORE) += kexec.o
diff --git a/arch/x86/coco/tdx/kexec.c b/arch/x86/coco/tdx/kexec.c
new file mode 100644
index 000000000000..f1f31515f372
--- /dev/null
+++ b/arch/x86/coco/tdx/kexec.c
@@ -0,0 +1,82 @@
+#define pr_fmt(fmt) "tdx: " fmt
+
+#include <linux/pagewalk.h>
+#include <asm/tdx.h>
+#include <asm/x86_init.h>
+
+static inline bool pud_decrypted(pud_t pud)
+{
+ return cc_mkdec(pud_val(pud)) == pud_val(pud);
+}
+
+static inline bool pmd_decrypted(pmd_t pmd)
+{
+ return cc_mkdec(pmd_val(pmd)) == pmd_val(pmd);
+}
+
+static inline bool pte_decrypted(pte_t pte)
+{
+ return cc_mkdec(pte_val(pte)) == pte_val(pte);
+}
+
+static inline void unshare_range(unsigned long start, unsigned long end)
+{
+ int pages = (end - start) / PAGE_SIZE;
+
+ if (!x86_platform.guest.enc_status_change_finish(start, pages, true))
+ pr_err("Failed to unshare range %#lx-%#lx\n", start, end);
+}
+
+static int unshare_pud(pud_t *pud, unsigned long addr, unsigned long next,
+ struct mm_walk *walk)
+{
+ if (pud_decrypted(*pud))
+ unshare_range(addr, next);
+
+ return 0;
+}
+
+static int unshare_pmd(pmd_t *pmd, unsigned long addr, unsigned long next,
+ struct mm_walk *walk)
+{
+ if (pmd_decrypted(*pmd))
+ unshare_range(addr, next);
+
+ return 0;
+}
+
+static int unshare_pte(pte_t *pte, unsigned long addr, unsigned long next,
+ struct mm_walk *walk)
+{
+ if (pte_decrypted(*pte))
+ unshare_range(addr, next);
+
+ return 0;
+}
+
+static const struct mm_walk_ops unshare_ops = {
+ .pud_entry = unshare_pud,
+ .pmd_entry = unshare_pmd,
+ .pte_entry = unshare_pte,
+};
+
+void tdx_kexec_prepare(bool crash)
+{
+ /*
+ * Crash kernel may want to see data in the shared buffers.
+ * Do not revert them to private on kexec of crash kernel.
+ */
+ if (crash)
+ return;
+
+ /*
+ * Walk direct mapping and convert all shared memory back to private,
+ * so the target kernel will be able use it normally.
+ */
+ mmap_write_lock(&init_mm);
+ walk_page_range_novma(&init_mm,
+ PAGE_OFFSET,
+ PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT),
+ &unshare_ops, init_mm.pgd, NULL);
+ mmap_write_unlock(&init_mm);
+}
diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index 28d889c9aa16..7cdbf10e9f7d 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -69,6 +69,8 @@ bool tdx_early_handle_ve(struct pt_regs *regs);
int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport);
+void tdx_kexec_prepare(bool crash);
+
#else
static inline void tdx_early_init(void) { };
@@ -76,6 +78,8 @@ static inline void tdx_safe_halt(void) { };
static inline bool tdx_early_handle_ve(struct pt_regs *regs) { return false; }
+static inline void tdx_kexec_prepare(bool crash) {}
+
#endif /* CONFIG_INTEL_TDX_GUEST */
#if defined(CONFIG_KVM_GUEST) && defined(CONFIG_INTEL_TDX_GUEST)
diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c
index 0611fd83858e..adbb3e5347da 100644
--- a/arch/x86/kernel/machine_kexec_64.c
+++ b/arch/x86/kernel/machine_kexec_64.c
@@ -28,6 +28,7 @@
#include <asm/setup.h>
#include <asm/set_memory.h>
#include <asm/cpu.h>
+#include <asm/tdx.h>
#ifdef CONFIG_ACPI
/*
@@ -312,6 +313,7 @@ void machine_kexec(struct kimage *image)
local_irq_disable();
hw_breakpoint_disable();
cet_disable();
+ tdx_kexec_prepare(image->type == KEXEC_TYPE_CRASH);
if (image->preserve_context) {
#ifdef CONFIG_X86_IO_APIC
--
2.39.1
next prev parent reply other threads:[~2023-02-13 23:48 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-13 23:48 [PATCH 0/2] Kexec enabling in TDX guest Kirill A. Shutemov
2023-02-13 23:48 ` [PATCH 1/2] x86/kexec: Preserve CR4.MCE during kexec Kirill A. Shutemov
2023-02-16 1:49 ` Edgecombe, Rick P
2023-02-16 9:43 ` Kirill A. Shutemov
2023-02-16 17:33 ` Edgecombe, Rick P
2023-02-16 19:46 ` Andrew Cooper
2023-02-13 23:48 ` Kirill A. Shutemov [this message]
2023-02-15 1:53 ` [PATCH 2/2] x86/tdx: Convert shared memory back to private on kexec Huang, Kai
2023-02-16 17:50 ` [PATCH 0/2] Kexec enabling in TDX guest Dave Hansen
2023-02-16 18:12 ` Kirill A. Shutemov
2023-02-16 18:32 ` Dave Hansen
2023-02-22 10:26 ` David Woodhouse
2023-02-24 14:30 ` Kirill A. Shutemov
2023-02-24 15:22 ` Dave Hansen
2023-02-24 16:12 ` Kirill A. Shutemov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230213234836.3683-3-kirill.shutemov@linux.intel.com \
--to=kirill.shutemov@linux.intel.com \
--cc=bp@alien8.de \
--cc=dave.hansen@intel.com \
--cc=isaku.yamahata@intel.com \
--cc=kexec@lists.infradead.org \
--cc=linux-coco@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=sathyanarayanan.kuppuswamy@linux.intel.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).