mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default
@ 2024-10-17 15:56 Kirill A. Shutemov
  2024-10-17 15:56 ` [PATCH 1/2] memremap: Pass down MEMREMAP_* flags to arch_memremap_wb() Kirill A. Shutemov
  2024-10-17 15:56 ` [PATCH 2/2] x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default Kirill A. Shutemov
  0 siblings, 2 replies; 4+ messages in thread
From: Kirill A. Shutemov @ 2024-10-17 15:56 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Andy Lutomirski
  Cc: Albert Ou, Alexei Starovoitov, Andrea Parri, Arnd Bergmann,
	Daniel Borkmann, Eric Chan, Jason Gunthorpe, Kai Huang,
	Kefeng Wang, Kent Overstreet, Palmer Dabbelt, Paul Walmsley,
	Russell King, Samuel Holland, Suren Baghdasaryan, Yuntao Wang,
	linux-arm-kernel, linux-kernel, linux-riscv, Kirill A. Shutemov

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.

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(-)

-- 
2.45.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] memremap: Pass down MEMREMAP_* flags to arch_memremap_wb()
  2024-10-17 15:56 [PATCH 0/2] x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default Kirill A. Shutemov
@ 2024-10-17 15:56 ` Kirill A. Shutemov
  2024-10-17 15:56 ` [PATCH 2/2] x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default Kirill A. Shutemov
  1 sibling, 0 replies; 4+ messages in thread
From: Kirill A. Shutemov @ 2024-10-17 15:56 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Andy Lutomirski
  Cc: Albert Ou, Alexei Starovoitov, Andrea Parri, Arnd Bergmann,
	Daniel Borkmann, Eric Chan, Jason Gunthorpe, Kai Huang,
	Kefeng Wang, Kent Overstreet, Palmer Dabbelt, Paul Walmsley,
	Russell King, Samuel Holland, Suren Baghdasaryan, Yuntao Wang,
	linux-arm-kernel, linux-kernel, linux-riscv, Kirill A. Shutemov

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.

Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
---
 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(-)

diff --git a/arch/arm/include/asm/io.h b/arch/arm/include/asm/io.h
index 1815748f5d2a..bae5edf348ef 100644
--- a/arch/arm/include/asm/io.h
+++ b/arch/arm/include/asm/io.h
@@ -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
 
 /*
diff --git a/arch/arm/mm/ioremap.c b/arch/arm/mm/ioremap.c
index 794cfea9f9d4..9f7883e6db46 100644
--- a/arch/arm/mm/ioremap.c
+++ b/arch/arm/mm/ioremap.c
@@ -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,
diff --git a/arch/arm/mm/nommu.c b/arch/arm/mm/nommu.c
index c415f3859b20..279641f0780e 100644
--- a/arch/arm/mm/nommu.c
+++ b/arch/arm/mm/nommu.c
@@ -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;
 }
diff --git a/arch/riscv/include/asm/io.h b/arch/riscv/include/asm/io.h
index 1c5c641075d2..0257f4aa7ff4 100644
--- a/arch/riscv/include/asm/io.h
+++ b/arch/riscv/include/asm/io.h
@@ -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
 
diff --git a/kernel/iomem.c b/kernel/iomem.c
index dc2120776e1c..75e61c1c6bc0 100644
--- a/kernel/iomem.c
+++ b/kernel/iomem.c
@@ -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);
 	}
 
 	/*
-- 
2.45.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default
  2024-10-17 15:56 [PATCH 0/2] x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default Kirill A. Shutemov
  2024-10-17 15:56 ` [PATCH 1/2] memremap: Pass down MEMREMAP_* flags to arch_memremap_wb() Kirill A. Shutemov
@ 2024-10-17 15:56 ` Kirill A. Shutemov
  2024-10-19 17:28   ` kernel test robot
  1 sibling, 1 reply; 4+ messages in thread
From: Kirill A. Shutemov @ 2024-10-17 15:56 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Andy Lutomirski
  Cc: Albert Ou, Alexei Starovoitov, Andrea Parri, Arnd Bergmann,
	Daniel Borkmann, Eric Chan, Jason Gunthorpe, Kai Huang,
	Kefeng Wang, Kent Overstreet, Palmer Dabbelt, Paul Walmsley,
	Russell King, Samuel Holland, Suren Baghdasaryan, Yuntao Wang,
	linux-arm-kernel, linux-kernel, linux-riscv, Kirill A. Shutemov,
	Tom Lendacky, Ashish Kalra, Maciej W. Rozycki

Currently memremap(MEMREMAP_WB) produces decrypted/shared mapping:

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

It is a bad default. On TDX guests, access via shared mapping can be
destructive[1].

Kernel already provides a way to request decrypted mapping explicitly
via MEMREMAP_DEC flag.

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.

[1] https://lore.kernel.org/all/20240822095122.736522-1-kirill.shutemov@linux.intel.com

Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Ashish Kalra <ashish.kalra@amd.com>
Cc: "Maciej W. Rozycki" <macro@orcam.me.uk>
---
 arch/x86/include/asm/io.h | 3 +++
 arch/x86/mm/ioremap.c     | 8 ++++++++
 2 files changed, 11 insertions(+)

diff --git a/arch/x86/include/asm/io.h b/arch/x86/include/asm/io.h
index 1d60427379c9..1a3a34b40598 100644
--- a/arch/x86/include/asm/io.h
+++ b/arch/x86/include/asm/io.h
@@ -180,6 +180,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
diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c
index 70b02fc61d93..fc65a81fd777 100644
--- a/arch/x86/mm/ioremap.c
+++ b/arch/x86/mm/ioremap.c
@@ -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)
+		return ioremap_cache(phys_addr, size);
+
+	return ioremap_encrypted(phys_addr, size);
+}
+
 /*
  * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  * access
-- 
2.45.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default
  2024-10-17 15:56 ` [PATCH 2/2] x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default Kirill A. Shutemov
@ 2024-10-19 17:28   ` kernel test robot
  0 siblings, 0 replies; 4+ messages in thread
From: kernel test robot @ 2024-10-19 17:28 UTC (permalink / raw)
  To: Kirill A. Shutemov, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
	Andy Lutomirski
  Cc: oe-kbuild-all, Albert Ou, Alexei Starovoitov, Andrea Parri,
	Arnd Bergmann, Daniel Borkmann, Eric Chan, Jason Gunthorpe,
	Kai Huang, Kefeng Wang, Kent Overstreet, Palmer Dabbelt,
	Paul Walmsley, Russell King, Samuel Holland, Suren Baghdasaryan,
	Yuntao Wang, linux-arm-kernel, linux-kernel, linux-riscv,
	Kirill A. Shutemov, Tom Lendacky, Ashish Kalra,
	Maciej W. Rozycki

Hi Kirill,

kernel test robot noticed the following build warnings:

[auto build test WARNING on tip/master]
[also build test WARNING on tip/x86/mm linus/master tip/auto-latest tip/x86/core v6.12-rc3 next-20241018]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Kirill-A-Shutemov/memremap-Pass-down-MEMREMAP_-flags-to-arch_memremap_wb/20241018-001138
base:   tip/master
patch link:    https://lore.kernel.org/r/20241017155642.1942514-3-kirill.shutemov%40linux.intel.com
patch subject: [PATCH 2/2] x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default
config: i386-randconfig-061-20241019 (https://download.01.org/0day-ci/archive/20241020/202410200112.Ut6HtXKD-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241020/202410200112.Ut6HtXKD-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202410200112.Ut6HtXKD-lkp@intel.com/

sparse warnings: (new ones prefixed by >>)
>> arch/x86/mm/ioremap.c:509:37: sparse: sparse: incorrect type in return expression (different address spaces) @@     expected void * @@     got void [noderef] __iomem * @@
   arch/x86/mm/ioremap.c:509:37: sparse:     expected void *
   arch/x86/mm/ioremap.c:509:37: sparse:     got void [noderef] __iomem *
   arch/x86/mm/ioremap.c:511:33: sparse: sparse: incorrect type in return expression (different address spaces) @@     expected void * @@     got void [noderef] __iomem * @@
   arch/x86/mm/ioremap.c:511:33: sparse:     expected void *
   arch/x86/mm/ioremap.c:511:33: sparse:     got void [noderef] __iomem *

vim +509 arch/x86/mm/ioremap.c

   505	
   506	void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags)
   507	{
   508		if (flags & MEMREMAP_DEC)
 > 509			return ioremap_cache(phys_addr, size);
   510	
   511		return ioremap_encrypted(phys_addr, size);
   512	}
   513	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-10-19 17:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-17 15:56 [PATCH 0/2] x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default Kirill A. Shutemov
2024-10-17 15:56 ` [PATCH 1/2] memremap: Pass down MEMREMAP_* flags to arch_memremap_wb() Kirill A. Shutemov
2024-10-17 15:56 ` [PATCH 2/2] x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default Kirill A. Shutemov
2024-10-19 17:28   ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®