x86/sev: Move sev_setup_arch() to mem_encrypt.c
authorAlexander Shishkin <alexander.shishkin@linux.intel.com>
Tue, 10 Oct 2023 14:52:19 +0000 (17:52 +0300)
committerIngo Molnar <mingo@kernel.org>
Wed, 11 Oct 2023 08:15:47 +0000 (10:15 +0200)
Since commit:

  4d96f9109109b ("x86/sev: Replace occurrences of sev_active() with cc_platform_has()")

... the SWIOTLB bounce buffer size adjustment and restricted virtio memory
setting also inadvertently apply to TDX: the code is using
cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) as a gatekeeping condition,
which is also true for TDX, and this is also what we want.

To reflect this, move the corresponding code to generic mem_encrypt.c.

No functional changes intended.

Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
Link: https://lore.kernel.org/r/20231010145220.3960055-2-alexander.shishkin@linux.intel.com
arch/x86/include/asm/mem_encrypt.h
arch/x86/kernel/setup.c
arch/x86/mm/mem_encrypt.c
arch/x86/mm/mem_encrypt_amd.c

index 473b16d73b4710849349902b54d1ec1e725ff054..359ada486fa92da37dfe77b1958a066612a42e4b 100644 (file)
 
 #ifdef CONFIG_X86_MEM_ENCRYPT
 void __init mem_encrypt_init(void);
+void __init mem_encrypt_setup_arch(void);
 #else
 static inline void mem_encrypt_init(void) { }
+static inline void __init mem_encrypt_setup_arch(void) { }
 #endif
 
 #ifdef CONFIG_AMD_MEM_ENCRYPT
@@ -43,7 +45,6 @@ void __init sme_map_bootdata(char *real_mode_data);
 void __init sme_unmap_bootdata(char *real_mode_data);
 
 void __init sme_early_init(void);
-void __init sev_setup_arch(void);
 
 void __init sme_encrypt_kernel(struct boot_params *bp);
 void __init sme_enable(struct boot_params *bp);
@@ -73,7 +74,6 @@ static inline void __init sme_map_bootdata(char *real_mode_data) { }
 static inline void __init sme_unmap_bootdata(char *real_mode_data) { }
 
 static inline void __init sme_early_init(void) { }
-static inline void __init sev_setup_arch(void) { }
 
 static inline void __init sme_encrypt_kernel(struct boot_params *bp) { }
 static inline void __init sme_enable(struct boot_params *bp) { }
index b9145a63da77d83586eb6fe608bfa0ff617ddd9b..ec44dc56ebc8002fa371c43a45541f5006d4a1b3 100644 (file)
@@ -1124,7 +1124,7 @@ void __init setup_arch(char **cmdline_p)
         * Needs to run after memblock setup because it needs the physical
         * memory size.
         */
-       sev_setup_arch();
+       mem_encrypt_setup_arch();
 
        efi_fake_memmap();
        efi_find_mirror();
index 9f27e14e185f339f58010d859c7941b6c8345f3e..c290c55b632bd76e99385831c45748b7c7ada891 100644 (file)
@@ -12,6 +12,7 @@
 #include <linux/swiotlb.h>
 #include <linux/cc_platform.h>
 #include <linux/mem_encrypt.h>
+#include <linux/virtio_anchor.h>
 
 /* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */
 bool force_dma_unencrypted(struct device *dev)
@@ -86,3 +87,36 @@ void __init mem_encrypt_init(void)
 
        print_mem_encrypt_feature_info();
 }
+
+void __init mem_encrypt_setup_arch(void)
+{
+       phys_addr_t total_mem = memblock_phys_mem_size();
+       unsigned long size;
+
+       if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
+               return;
+
+       /*
+        * For SEV and TDX, all DMA has to occur via shared/unencrypted pages.
+        * Kernel uses SWIOTLB to make this happen without changing device
+        * drivers. However, depending on the workload being run, the
+        * default 64MB of SWIOTLB may not be enough and SWIOTLB may
+        * run out of buffers for DMA, resulting in I/O errors and/or
+        * performance degradation especially with high I/O workloads.
+        *
+        * Adjust the default size of SWIOTLB using a percentage of guest
+        * memory for SWIOTLB buffers. Also, as the SWIOTLB bounce buffer
+        * memory is allocated from low memory, ensure that the adjusted size
+        * is within the limits of low available memory.
+        *
+        * The percentage of guest memory used here for SWIOTLB buffers
+        * is more of an approximation of the static adjustment which
+        * 64MB for <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
+        */
+       size = total_mem * 6 / 100;
+       size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);
+       swiotlb_adjust_size(size);
+
+       /* Set restricted memory access for virtio. */
+       virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
+}
index 6faea41e99b6bb16ebf0d68aa274b024ac43051d..62dde75d41faae740d3077faac4ac63f1593be0e 100644 (file)
@@ -20,7 +20,6 @@
 #include <linux/bitops.h>
 #include <linux/dma-mapping.h>
 #include <linux/virtio_config.h>
-#include <linux/virtio_anchor.h>
 #include <linux/cc_platform.h>
 
 #include <asm/tlbflush.h>
@@ -215,40 +214,6 @@ void __init sme_map_bootdata(char *real_mode_data)
        __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, true);
 }
 
-void __init sev_setup_arch(void)
-{
-       phys_addr_t total_mem = memblock_phys_mem_size();
-       unsigned long size;
-
-       if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
-               return;
-
-       /*
-        * For SEV, all DMA has to occur via shared/unencrypted pages.
-        * SEV uses SWIOTLB to make this happen without changing device
-        * drivers. However, depending on the workload being run, the
-        * default 64MB of SWIOTLB may not be enough and SWIOTLB may
-        * run out of buffers for DMA, resulting in I/O errors and/or
-        * performance degradation especially with high I/O workloads.
-        *
-        * Adjust the default size of SWIOTLB for SEV guests using
-        * a percentage of guest memory for SWIOTLB buffers.
-        * Also, as the SWIOTLB bounce buffer memory is allocated
-        * from low memory, ensure that the adjusted size is within
-        * the limits of low available memory.
-        *
-        * The percentage of guest memory used here for SWIOTLB buffers
-        * is more of an approximation of the static adjustment which
-        * 64MB for <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
-        */
-       size = total_mem * 6 / 100;
-       size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);
-       swiotlb_adjust_size(size);
-
-       /* Set restricted memory access for virtio. */
-       virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
-}
-
 static unsigned long pg_level_to_pfn(int level, pte_t *kpte, pgprot_t *ret_prot)
 {
        unsigned long pfn = 0;