From nobody Tue Nov 26 05:46:20 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.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 4EA821E7C07 for ; Mon, 21 Oct 2024 10:57:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729508258; cv=none; b=a6I0sNGNnFhkP7PUmquTqChPwLj8GBZkEa33HFyKZSiESwWfaUZIANTnnqh2xULIXgZ4Wze4yuTOVfXh2nOTP8sQSD3+r+UvHTbE0o+J9AT/R2tAsLFCIAtIe68/dqjF85eVhN7nacIIBy4Xn0ojgdBq4LdvcUgrShjwAlXtN98= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729508258; c=relaxed/simple; bh=X7oqsq0bMXF50IQWvSxcxdLufH4ePr/ItKwrUI5ikCs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ep1BKEoxn4Gmkt6R/IH9e9Jp84nuhi0pAlpPvCeMDvwvPo0fuvdDe0/Lo5TSkQLjF82mhmqH6R4aRFQsQMr7o3/jx5TFYJfnVGUjAyLUV664+NwG4Ms3zW2FpaA8CeltRYVfwl30YZNYHGOr7ODFFZGk2zfsIhT70K9qPuz91Ek= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=DV7Ybs72; arc=none smtp.client-ip=198.175.65.10 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.helo=mgamail.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="DV7Ybs72" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1729508255; x=1761044255; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=X7oqsq0bMXF50IQWvSxcxdLufH4ePr/ItKwrUI5ikCs=; b=DV7Ybs72/2xRDz2tWOpcMM4rfqE1EeLFzzPAg7ZN3naKuxiJwQ3lt0MN Da1b5jLq3DBwPnvs7cgKwRecVq/OZaTTKfNm4sfpGRwhtQ6TabTSOJgfI hvSfE+ZWWh1Wjjvx9yx+4IvBazUs9Svlyw8b0FyujkVdSxHNMNiSydlL1 khy2+breFwMZfDSZ3iuWHq8WDZPpYnw8EjqcTbGende9zgaEFgPgTStgb Xq7aHFIH4+CB5AcwVSph1ZzSeOL+vwQG0d4DJ5FL3usSs6IgUjrFS/717 y0sj92ok71utPPKzOJ1TAbOoqT8/Nyl5Iryr8WsBGSQVBJV4SjTdNR++j Q==; X-CSE-ConnectionGUID: 5WqIFAQcTHirZPAwEqGirg== X-CSE-MsgGUID: lR+J0oqRRgS4Vc4wsfkLYA== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="46451194" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="46451194" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Oct 2024 03:57:35 -0700 X-CSE-ConnectionGUID: 7ZR6J6H3Sle/1LEQ0yzQuQ== X-CSE-MsgGUID: bp03jTXsSMSXTCJ7NVwGzA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,220,1725346800"; d="scan'208";a="79085639" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa006.fm.intel.com with ESMTP; 21 Oct 2024 03:57:29 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id BEFF612B; Mon, 21 Oct 2024 13:57:27 +0300 (EEST) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "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@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, "Kirill A. Shutemov" Subject: [PATCHv2 1/2] memremap: Pass down MEMREMAP_* flags to arch_memremap_wb() Date: Mon, 21 Oct 2024 13:57:22 +0300 Message-ID: <20241021105723.2707507-2-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241021105723.2707507-1-kirill.shutemov@linux.intel.com> References: <20241021105723.2707507-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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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, si= ze_t size); void iounmap(volatile void __iomem *io_addr); #define iounmap iounmap =20 -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 f= lags); #define arch_memremap_wb arch_memremap_wb =20 /* 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); } =20 -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 f= lags) { 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_co= okie, size_t size) EXPORT_SYMBOL_GPL(pci_remap_cfgspace); #endif =20 -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 f= lags) { 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 =20 #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 =20 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 =20 #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, unsig= ned long flags) if (is_ram =3D=3D REGION_INTERSECTS) addr =3D try_ram_remap(offset, size, flags); if (!addr) - addr =3D arch_memremap_wb(offset, size); + addr =3D arch_memremap_wb(offset, size, flags); } =20 /* --=20 2.45.2 From nobody Tue Nov 26 05:46:20 2024 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.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 46F311EBA00 for ; Mon, 21 Oct 2024 10:57:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729508260; cv=none; b=ZIGh+WlyO2QKC8NrHugKen8bA/NhWDY9yc0l67ct2fNa+kxmsNQhSeGUlZpF68RACXCc7Su/FZGPTEq1a/gpl4kOGVG3V0wuccqMdP2cSsceTNKrsY0/+aAYxtOfM+bK6H/DAACBz4Rct7Tl0zBUPOBgPh34kjNa0TShMvpF5so= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729508260; c=relaxed/simple; bh=NJWPKuhJFb05qZHrkoFF+r7qnXfD1RCxCtsgrjO7Isw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YTWHZkKTRqVSfI6V83rYlepZPphWYfYK/+K/pJF0BgoUtJJpCGzTgykg+pSEVLYnKcbl96qNbY+v/2dqlXo9T+rguWJo4B2y3FnLboP7Z3kDgIO01UY1HWO0GRefcIvKOkJw02nfui+qcmmgFx7WACO+OO8HeFq6FRAQ57pYMEw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=LenFqjpz; arc=none smtp.client-ip=198.175.65.10 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.helo=mgamail.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="LenFqjpz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1729508258; x=1761044258; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NJWPKuhJFb05qZHrkoFF+r7qnXfD1RCxCtsgrjO7Isw=; b=LenFqjpzssmxYpY6KHV7NKu8TQalynFQlQGfqYXgr7AEUoP99QIZ2dUL zMTXoaCWMDPnd17er0TOmpIJYdm+Y/6DUt3xrAqm67M9qUVnF3EuSmSmS h8Qzm2vxda1P+soegVAy+CBQX3LTEPpLt4I8yJkN7kq7dwgYDx/3oflbu CR0dH3OJJqEnapch79gvp89g9yUv0re08MyVAhrR5SD1iXVs+KVcaZ0fn 0HpXkUzWZAg9X9xl36qnI1kGj5Nk3i/crDihOZgth1zJMXQl6kXyvK/0o clK76874xx9q9qecA0drnKUtjQE4uieXQzCECOegAze0p5FdRq+yijE6o A==; X-CSE-ConnectionGUID: dGZtpzhHRT6OZPRusyTBWw== X-CSE-MsgGUID: TgTx/apURJ2K1wvvAQyDMg== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="46451210" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="46451210" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Oct 2024 03:57:35 -0700 X-CSE-ConnectionGUID: e96WSJFySh+rbRuPL1Zqig== X-CSE-MsgGUID: 7LZcYE+iTLmj/X/JF4w6CQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,220,1725346800"; d="scan'208";a="79085643" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa006.fm.intel.com with ESMTP; 21 Oct 2024 03:57:29 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id CE8F235B; Mon, 21 Oct 2024 13:57:27 +0300 (EEST) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "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@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, "Kirill A. Shutemov" , Tom Lendacky , Ashish Kalra , "Maciej W. Rozycki" Subject: [PATCHv2 2/2] x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default Date: Mon, 21 Oct 2024 13:57:23 +0300 Message-ID: <20241021105723.2707507-3-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241021105723.2707507-1-kirill.shutemov@linux.intel.com> References: <20241021105723.2707507-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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently memremap(MEMREMAP_WB) produces decrypted/shared mapping: memremap(MEMREMAP_WB) arch_memremap_wb() ioremap_cache() __ioremap_caller(.encrytped =3D 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@lin= ux.intel.com Signed-off-by: Kirill A. Shutemov Cc: Tom Lendacky Cc: Ashish Kalra Cc: "Maciej W. Rozycki" --- 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 offse= t, unsigned long size, un extern void __iomem *ioremap_encrypted(resource_size_t phys_addr, unsigned= long size); #define ioremap_encrypted ioremap_encrypted =20 +void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long f= lags); +#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..f04df30f0a27 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); =20 +void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long f= lags) +{ + if (flags & MEMREMAP_DEC) + 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 --=20 2.45.2