Skip to contentSkip to conversation

x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default

PATCH Applied Kirill A. Shutemov 2 participants 5 messages Feb 17, 2025 – Feb 21, 2025

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

v4 cover x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default

Make memremap(MEMREMAP_WB) produce encrypted/private mapping by default unless MEMREMAP_DEC is specified.

It fixes crash on kexec in TDX guests if CONFIG_EISA is enabled.

v4:
- Keep default-decrypted for SME machines;
v3:
- Update commit message;
- Add Cc: stable;
v2:
- Fix sparse warning;

Kirill A. Shutemov (2):
memremap: Pass down MEMREMAP_* flags to arch_memremap_wb() x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by t

Kirill A. Shutemov (2):
memremap: Pass down MEMREMAP_* flags to arch_memremap_wb() x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default

 arch/arm/include/asm/io.h   | 2 +-
 arch/arm/mm/ioremap.c       | 2 +-
 arch/arm/mm/nommu.c         | 2 +-
 arch/riscv/include/asm/io.h | 2 +-
 arch/x86/include/asm/io.h   | 3 +++
 arch/x86/mm/ioremap.c       | 8 ++++++++
 kernel/iomem.c              | 5 +++--
 7 files changed, 18 insertions(+), 6 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH v4 1/2 memremap: Pass down MEMREMAP_* flags to arch_memremap_wb()

x86 version of arch_memremap_wb() needs the flags to decide if the mapping has be encrypted or decrypted.

Pass down the flag to arch_memremap_wb(). All current implementations ignore the argument.

---
 arch/arm/include/asm/io.h   | 2 +-
 arch/arm/mm/ioremap.c       | 2 +-
 arch/arm/mm/nommu.c         | 2 +-
 arch/riscv/include/asm/io.h | 2 +-
 kernel/iomem.c              | 5 +++--
 5 files changed, 7 insertions(+), 6 deletions(-)
arch/arm/include/asm/io.h +1 -1
index 1815748f5d2a..bae5edf348ef 100644
@@ -381,7 +381,7 @@ void __iomem *ioremap_wc(resource_size_t res_cookie, size_t size);
 void iounmap(volatile void __iomem *io_addr);
 #define iounmap iounmap
 
-void *arch_memremap_wb(phys_addr_t phys_addr, size_t size);
+void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags);
 #define arch_memremap_wb arch_memremap_wb
 
 /*
arch/arm/mm/ioremap.c +1 -1
index 794cfea9f9d4..9f7883e6db46 100644
@@ -411,7 +411,7 @@ void __arm_iomem_set_ro(void __iomem *ptr, size_t size)
 	set_memory_ro((unsigned long)ptr, PAGE_ALIGN(size) / PAGE_SIZE);
 }
 
-void *arch_memremap_wb(phys_addr_t phys_addr, size_t size)
+void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags)
 {
 	return (__force void *)arch_ioremap_caller(phys_addr, size,
 						   MT_MEMORY_RW,
arch/arm/mm/nommu.c +1 -1
index c415f3859b20..279641f0780e 100644
@@ -251,7 +251,7 @@ void __iomem *pci_remap_cfgspace(resource_size_t res_cookie, size_t size)
 EXPORT_SYMBOL_GPL(pci_remap_cfgspace);
 #endif
 
-void *arch_memremap_wb(phys_addr_t phys_addr, size_t size)
+void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags)
 {
 	return (void *)phys_addr;
 }
arch/riscv/include/asm/io.h +1 -1
index 1c5c641075d2..0257f4aa7ff4 100644
@@ -136,7 +136,7 @@ __io_writes_outs(outs, u64, q, __io_pbr(), __io_paw())
 #include <asm-generic/io.h>
 
 #ifdef CONFIG_MMU
-#define arch_memremap_wb(addr, size)	\
+#define arch_memremap_wb(addr, size, flags)	\
 	((__force void *)ioremap_prot((addr), (size), _PAGE_KERNEL))
 #endif
 
kernel/iomem.c +3 -2
index dc2120776e1c..75e61c1c6bc0 100644
@@ -6,7 +6,8 @@
 #include <linux/ioremap.h>
 
 #ifndef arch_memremap_wb
-static void *arch_memremap_wb(resource_size_t offset, unsigned long size)
+static void *arch_memremap_wb(resource_size_t offset, unsigned long size,
+			      unsigned long flags)
 {
 #ifdef ioremap_cache
 	return (__force void *)ioremap_cache(offset, size);
@@ -91,7 +92,7 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags)
 		if (is_ram == REGION_INTERSECTS)
 			addr = try_ram_remap(offset, size, flags);
 		if (!addr)
-			addr = arch_memremap_wb(offset, size);
+			addr = arch_memremap_wb(offset, size, flags);
 	}
 
 	/*
Signature · 1 line
2.47.2
#2 lore raw

[tip: x86/mm] mm/memremap: Pass down MEMREMAP_* flags to arch_memremap_wb()

The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     a9ebcb88136ca80cb53de27ca5ae77de18bbe368
Gitweb:        https://git.kernel.org/tip/a9ebcb88136ca80cb53de27ca5ae77de18bbe368
Author:        Kirill A. Shutemov <[email protected]>
AuthorDate:    Mon, 17 Feb 2025 18:38:20 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 21 Feb 2025 15:05:38 +01:00

mm/memremap: Pass down MEMREMAP_* flags to arch_memremap_wb()

x86 version of arch_memremap_wb() needs the flags to decide if the mapping has to be encrypted or decrypted.

Pass down the flag to arch_memremap_wb(). All current implementations ignore the argument.

---
 arch/arm/include/asm/io.h   | 2 +-
 arch/arm/mm/ioremap.c       | 2 +-
 arch/arm/mm/nommu.c         | 2 +-
 arch/riscv/include/asm/io.h | 2 +-
 kernel/iomem.c              | 5 +++--
 5 files changed, 7 insertions(+), 6 deletions(-)
arch/arm/include/asm/io.h +1 -1
index 1815748..bae5edf 100644
@@ -381,7 +381,7 @@ void __iomem *ioremap_wc(resource_size_t res_cookie, size_t size);
 void iounmap(volatile void __iomem *io_addr);
 #define iounmap iounmap
 
-void *arch_memremap_wb(phys_addr_t phys_addr, size_t size);
+void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags);
 #define arch_memremap_wb arch_memremap_wb
 
 /*
arch/arm/mm/ioremap.c +1 -1
index 89f1c97..748698e 100644
@@ -436,7 +436,7 @@ void __arm_iomem_set_ro(void __iomem *ptr, size_t size)
 	set_memory_ro((unsigned long)ptr, PAGE_ALIGN(size) / PAGE_SIZE);
 }
 
-void *arch_memremap_wb(phys_addr_t phys_addr, size_t size)
+void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags)
 {
 	return (__force void *)arch_ioremap_caller(phys_addr, size,
 						   MT_MEMORY_RW,
arch/arm/mm/nommu.c +1 -1
index 1a8f691..d638cc8 100644
@@ -248,7 +248,7 @@ void __iomem *pci_remap_cfgspace(resource_size_t res_cookie, size_t size)
 EXPORT_SYMBOL_GPL(pci_remap_cfgspace);
 #endif
 
-void *arch_memremap_wb(phys_addr_t phys_addr, size_t size)
+void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags)
 {
 	return (void *)phys_addr;
 }
arch/riscv/include/asm/io.h +1 -1
index 1c5c641..0257f4a 100644
@@ -136,7 +136,7 @@ __io_writes_outs(outs, u64, q, __io_pbr(), __io_paw())
 #include <asm-generic/io.h>
 
 #ifdef CONFIG_MMU
-#define arch_memremap_wb(addr, size)	\
+#define arch_memremap_wb(addr, size, flags)	\
 	((__force void *)ioremap_prot((addr), (size), _PAGE_KERNEL))
 #endif
 
kernel/iomem.c +3 -2
index dc21207..75e61c1 100644
@@ -6,7 +6,8 @@
 #include <linux/ioremap.h>
 
 #ifndef arch_memremap_wb
-static void *arch_memremap_wb(resource_size_t offset, unsigned long size)
+static void *arch_memremap_wb(resource_size_t offset, unsigned long size,
+			      unsigned long flags)
 {
 #ifdef ioremap_cache
 	return (__force void *)ioremap_cache(offset, size);
@@ -91,7 +92,7 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags)
 		if (is_ram == REGION_INTERSECTS)
 			addr = try_ram_remap(offset, size, flags);
 		if (!addr)
-			addr = arch_memremap_wb(offset, size);
+			addr = arch_memremap_wb(offset, size, flags);
 	}
 
 	/*
#3 lore raw

PATCH v4 2/2 x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default

In reply to Kirill A. Shutemov

Currently memremap(MEMREMAP_WB) can produce decrypted/shared mapping:

memremap(MEMREMAP_WB)
arch_memremap_wb()
ioremap_cache()
__ioremap_caller(.encrytped = false)

In such cases, the IORES_MAP_ENCRYPTED flag on the memory will determine if the resulting mapping is encrypted or decrypted.

Creating a decrypted mapping without explicit request from the caller is risky:

- It can inadvertently expose the guest's data and compromise the guest.

- Accessing private memory via shared/decrypted mapping on TDX will either trigger implicit conversion to shared or #VE (depending on VMM implementation).

Implicit conversion is destructive: subsequent access to the same memory via private mapping will trigger a hard-to-debug #VE crash.

The kernel already provides a way to request decrypted mapping explicitly via the MEMREMAP_DEC flag.

Modify memremap(MEMREMAP_WB) to produce encrypted/private mapping by default unless MEMREMAP_DEC is specified or if the kernel runs on a machine with SME enabled.

It fixes the crash due to #VE on kexec in TDX guests if CONFIG_EISA is enabled.

Cc Tom Lendacky <[email protected]>
Cc Ashish Kalra <[email protected]>
Cc "Maciej W. Rozycki" <[email protected]>
---
 arch/x86/include/asm/io.h | 3 +++
 arch/x86/mm/ioremap.c     | 8 ++++++++
 2 files changed, 11 insertions(+)
arch/x86/include/asm/io.h +3 -0
index ed580c7f9d0a..1a0dc2b2bf5b 100644
@@ -175,6 +175,9 @@ extern void __iomem *ioremap_prot(resource_size_t offset, unsigned long size, un
 extern void __iomem *ioremap_encrypted(resource_size_t phys_addr, unsigned long size);
 #define ioremap_encrypted ioremap_encrypted
 
+void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags);
+#define arch_memremap_wb arch_memremap_wb
+
 /**
  * ioremap     -   map bus memory into CPU space
  * @offset:    bus address of the memory
arch/x86/mm/ioremap.c +8 -0
index 8d29163568a7..a4b23d2e92d2 100644
@@ -503,6 +503,14 @@ void iounmap(volatile void __iomem *addr)
 }
 EXPORT_SYMBOL(iounmap);
 
+void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags)
+{
+	if ((flags & MEMREMAP_DEC) || cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
+		return (void __force *)ioremap_cache(phys_addr, size);
+
+	return (void __force *)ioremap_encrypted(phys_addr, size);
+}
+
 /*
  * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  * access
Signature · 1 line
2.47.2
#4 lore raw
The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     81256a50aa0fddefbf4849db8cad9f70c5167c04
Gitweb:        https://git.kernel.org/tip/81256a50aa0fddefbf4849db8cad9f70c5167c04
Author:        Kirill A. Shutemov <[email protected]>
AuthorDate:    Mon, 17 Feb 2025 18:38:21 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 21 Feb 2025 15:05:45 +01:00

x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default

Currently memremap(MEMREMAP_WB) can produce decrypted/shared mapping:

memremap(MEMREMAP_WB)
arch_memremap_wb()
ioremap_cache()
__ioremap_caller(.encrytped = false)

In such cases, the IORES_MAP_ENCRYPTED flag on the memory will determine if the resulting mapping is encrypted or decrypted.

Creating a decrypted mapping without explicit request from the caller is risky:

- It can inadvertently expose the guest's data and compromise the guest.

- Accessing private memory via shared/decrypted mapping on TDX will either trigger implicit conversion to shared or #VE (depending on VMM implementation).

Implicit conversion is destructive: subsequent access to the same memory via private mapping will trigger a hard-to-debug #VE crash.

The kernel already provides a way to request decrypted mapping explicitly via the MEMREMAP_DEC flag.

Modify memremap(MEMREMAP_WB) to produce encrypted/private mapping by default unless MEMREMAP_DEC is specified or if the kernel runs on a machine with SME enabled.

It fixes the crash due to #VE on kexec in TDX guests if CONFIG_EISA is enabled.

---
 arch/x86/include/asm/io.h | 3 +++
 arch/x86/mm/ioremap.c     | 8 ++++++++
 2 files changed, 11 insertions(+)
arch/x86/include/asm/io.h +3 -0
index ed580c7..1a0dc2b 100644
@@ -175,6 +175,9 @@ extern void __iomem *ioremap_prot(resource_size_t offset, unsigned long size, un
 extern void __iomem *ioremap_encrypted(resource_size_t phys_addr, unsigned long size);
 #define ioremap_encrypted ioremap_encrypted
 
+void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags);
+#define arch_memremap_wb arch_memremap_wb
+
 /**
  * ioremap     -   map bus memory into CPU space
  * @offset:    bus address of the memory
arch/x86/mm/ioremap.c +8 -0
index 38ff779..42c90b4 100644
@@ -503,6 +503,14 @@ void iounmap(volatile void __iomem *addr)
 }
 EXPORT_SYMBOL(iounmap);
 
+void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags)
+{
+	if ((flags & MEMREMAP_DEC) || cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
+		return (void __force *)ioremap_cache(phys_addr, size);
+
+	return (void __force *)ioremap_encrypted(phys_addr, size);
+}
+
 /*
  * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  * access