Skip to content

Commit 6e74b12

Browse files
virtuosoIngo Molnar
authored andcommitted
x86/sev: Move sev_setup_arch() to mem_encrypt.c
Since commit: 4d96f91 ("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 <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Tom Lendacky <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent c9babd5 commit 6e74b12

File tree

4 files changed

+37
-38
lines changed

4 files changed

+37
-38
lines changed

arch/x86/include/asm/mem_encrypt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
#ifdef CONFIG_X86_MEM_ENCRYPT
2121
void __init mem_encrypt_init(void);
22+
void __init mem_encrypt_setup_arch(void);
2223
#else
2324
static inline void mem_encrypt_init(void) { }
25+
static inline void __init mem_encrypt_setup_arch(void) { }
2426
#endif
2527

2628
#ifdef CONFIG_AMD_MEM_ENCRYPT
@@ -43,7 +45,6 @@ void __init sme_map_bootdata(char *real_mode_data);
4345
void __init sme_unmap_bootdata(char *real_mode_data);
4446

4547
void __init sme_early_init(void);
46-
void __init sev_setup_arch(void);
4748

4849
void __init sme_encrypt_kernel(struct boot_params *bp);
4950
void __init sme_enable(struct boot_params *bp);
@@ -73,7 +74,6 @@ static inline void __init sme_map_bootdata(char *real_mode_data) { }
7374
static inline void __init sme_unmap_bootdata(char *real_mode_data) { }
7475

7576
static inline void __init sme_early_init(void) { }
76-
static inline void __init sev_setup_arch(void) { }
7777

7878
static inline void __init sme_encrypt_kernel(struct boot_params *bp) { }
7979
static inline void __init sme_enable(struct boot_params *bp) { }

arch/x86/kernel/setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ void __init setup_arch(char **cmdline_p)
11241124
* Needs to run after memblock setup because it needs the physical
11251125
* memory size.
11261126
*/
1127-
sev_setup_arch();
1127+
mem_encrypt_setup_arch();
11281128

11291129
efi_fake_memmap();
11301130
efi_find_mirror();

arch/x86/mm/mem_encrypt.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/swiotlb.h>
1313
#include <linux/cc_platform.h>
1414
#include <linux/mem_encrypt.h>
15+
#include <linux/virtio_anchor.h>
1516

1617
/* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */
1718
bool force_dma_unencrypted(struct device *dev)
@@ -86,3 +87,36 @@ void __init mem_encrypt_init(void)
8687

8788
print_mem_encrypt_feature_info();
8889
}
90+
91+
void __init mem_encrypt_setup_arch(void)
92+
{
93+
phys_addr_t total_mem = memblock_phys_mem_size();
94+
unsigned long size;
95+
96+
if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
97+
return;
98+
99+
/*
100+
* For SEV and TDX, all DMA has to occur via shared/unencrypted pages.
101+
* Kernel uses SWIOTLB to make this happen without changing device
102+
* drivers. However, depending on the workload being run, the
103+
* default 64MB of SWIOTLB may not be enough and SWIOTLB may
104+
* run out of buffers for DMA, resulting in I/O errors and/or
105+
* performance degradation especially with high I/O workloads.
106+
*
107+
* Adjust the default size of SWIOTLB using a percentage of guest
108+
* memory for SWIOTLB buffers. Also, as the SWIOTLB bounce buffer
109+
* memory is allocated from low memory, ensure that the adjusted size
110+
* is within the limits of low available memory.
111+
*
112+
* The percentage of guest memory used here for SWIOTLB buffers
113+
* is more of an approximation of the static adjustment which
114+
* 64MB for <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
115+
*/
116+
size = total_mem * 6 / 100;
117+
size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);
118+
swiotlb_adjust_size(size);
119+
120+
/* Set restricted memory access for virtio. */
121+
virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
122+
}

arch/x86/mm/mem_encrypt_amd.c

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <linux/bitops.h>
2121
#include <linux/dma-mapping.h>
2222
#include <linux/virtio_config.h>
23-
#include <linux/virtio_anchor.h>
2423
#include <linux/cc_platform.h>
2524

2625
#include <asm/tlbflush.h>
@@ -215,40 +214,6 @@ void __init sme_map_bootdata(char *real_mode_data)
215214
__sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, true);
216215
}
217216

218-
void __init sev_setup_arch(void)
219-
{
220-
phys_addr_t total_mem = memblock_phys_mem_size();
221-
unsigned long size;
222-
223-
if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
224-
return;
225-
226-
/*
227-
* For SEV, all DMA has to occur via shared/unencrypted pages.
228-
* SEV uses SWIOTLB to make this happen without changing device
229-
* drivers. However, depending on the workload being run, the
230-
* default 64MB of SWIOTLB may not be enough and SWIOTLB may
231-
* run out of buffers for DMA, resulting in I/O errors and/or
232-
* performance degradation especially with high I/O workloads.
233-
*
234-
* Adjust the default size of SWIOTLB for SEV guests using
235-
* a percentage of guest memory for SWIOTLB buffers.
236-
* Also, as the SWIOTLB bounce buffer memory is allocated
237-
* from low memory, ensure that the adjusted size is within
238-
* the limits of low available memory.
239-
*
240-
* The percentage of guest memory used here for SWIOTLB buffers
241-
* is more of an approximation of the static adjustment which
242-
* 64MB for <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
243-
*/
244-
size = total_mem * 6 / 100;
245-
size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);
246-
swiotlb_adjust_size(size);
247-
248-
/* Set restricted memory access for virtio. */
249-
virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
250-
}
251-
252217
static unsigned long pg_level_to_pfn(int level, pte_t *kpte, pgprot_t *ret_prot)
253218
{
254219
unsigned long pfn = 0;

0 commit comments

Comments
 (0)