swiotlb: Adjust SWIOTBL bounce buffer size for SEV guests.
Subsystems (3)
PATCH v4 1/1 swiotlb: Adjust SWIOTBL bounce buffer size for SEV guests.
From: Ashish Kalra <[email protected]>
For SEV, all DMA to and from guest has to use shared (un-encrypted) pages. SEV uses SWIOTLB to make this happen without requiring changes to device drivers. However, depending on workload being run, the default 64MB of SWIOTLB might not be enough and SWIOTLB may run out of buffers to use for DMA, resulting in I/O errors and/or performance degradation for high I/O workloads.
Increase the default size of SWIOTLB for SEV guests using a minimum value of 128MB and a maximum value of 512MB, determining on amount of provisioned guest memory.
Using late_initcall() interface to invoke swiotlb_adjust() does not work as the size adjustment needs to be done before mem_encrypt_init() and reserve_crashkernel() which use the allocated SWIOTLB buffer size, hence calling it explicitly from setup_arch().
The SWIOTLB default size adjustment is added as an architecture specific interface/callback to allow architectures such as those supporting memory encryption to adjust/expand SWIOTLB size for their use.
--- arch/x86/kernel/setup.c | 2 ++ arch/x86/mm/mem_encrypt.c | 32 ++++++++++++++++++++++++++++++++ include/linux/swiotlb.h | 1 + kernel/dma/swiotlb.c | 27 +++++++++++++++++++++++++++ 4 files changed, 62 insertions(+)
arch/x86/kernel/setup.c +2 -0
@@ -1166,6 +1166,8 @@ void __init setup_arch(char **cmdline_p)if (boot_cpu_has (X86_FEATURE_GBPAGES ))hugetlb_cma_reserve (PUD_SHIFT -PAGE_SHIFT ); +swiotlb_adjust (); +/* * Reserve memory for crash kernel after SRAT is parsed so that it * won't consume hotpluggable memory.
arch/x86/mm/mem_encrypt.c +32 -0
@@ -490,6 +490,38 @@ static void print_mem_encrypt_feature_info(void) }/* Architecture __weak replacement functions */ +unsigned long __init arch_swiotlb_adjust (unsigned long iotlb_default_size) +{ +unsigned long size =0 ; + +/* +* For SEV, all DMA has to occur via shared/unencrypted pages. +* SEV uses SWOTLB to make this happen without changing device +* drivers. However, depending on the workload being run, the +* default 64MB of SWIOTLB may not be enough & SWIOTLB may +* run out of buffers for DMA, resulting in I/O errors and/or +* performance degradation especially with high I/O workloads. +* Increase the default size of SWIOTLB for SEV guests using +* a minimum value of 128MB and a maximum value of 512MB, +* depending on amount of provisioned guest memory. +*/ +if (sev_active ()) { +phys_addr_t total_mem =memblock_phys_mem_size (); + +if (total_mem <=SZ_1G ) + size =max (iotlb_default_size, (unsigned long )SZ_128M ); +else if (total_mem <=SZ_4G ) + size =max (iotlb_default_size, (unsigned long )SZ_256M ); +else + size =max (iotlb_default_size, (unsigned long )SZ_512M ); + +pr_info ("SEV adjusted max SWIOTLB size = %luMB" , + size >>20 ); + } + +return size; +} +void __init mem_encrypt_init (void ) {if (!sme_me_mask)
include/linux/swiotlb.h +1 -0
@@ -33,6 +33,7 @@ extern void swiotlb_init(int verbose);int swiotlb_init_with_tbl (char *tlb,unsigned long nslabs,int verbose);extern unsigned long swiotlb_nr_tbl (void );unsigned long swiotlb_size_or_default (void ); +extern void __init swiotlb_adjust (void );extern int swiotlb_late_init_with_tbl (char *tlb,unsigned long nslabs);extern void __init swiotlb_update_mem_attributes (void );
kernel/dma/swiotlb.c +27 -0
@@ -163,6 +163,33 @@ unsigned long swiotlb_size_or_default(void)return size ? size : (IO_TLB_DEFAULT_SIZE ); } +unsigned long __init __weak arch_swiotlb_adjust (unsigned long size) +{ +return 0 ; +} + +void __init swiotlb_adjust (void ) +{ +unsigned long size; + +/* +* If swiotlb parameter has not been specified, give a chance to +* architectures such as those supporting memory encryption to +* adjust/expand SWIOTLB size for their use. +*/ +if (!io_tlb_nslabs) { + size =arch_swiotlb_adjust (IO_TLB_DEFAULT_SIZE ); +if (size) { + size =ALIGN (size,1 <<IO_TLB_SHIFT ); + io_tlb_nslabs = size >>IO_TLB_SHIFT ; + io_tlb_nslabs =ALIGN (io_tlb_nslabs,IO_TLB_SEGSIZE ); + +pr_info ("architecture adjusted SWIOTLB slabs = %lu\n" , + io_tlb_nslabs); + } + } +} +void swiotlb_print_info (void ) {unsigned long bytes = io_tlb_nslabs <<IO_TLB_SHIFT ;
Signature · 1 line
2.17.1
kernel test robot
build botThank you for the patch! Perhaps something to improve:
Hi Ashish,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on tip/x86/core]
[also build test WARNING on tip/x86/mm hch-configfs/for-next v5.10-rc4 next-20201117] [cannot apply to swiotlb/linux-next]
[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]
url: https://github.com/0day-ci/linux/commits/Ashish-Kalra/swiotlb-Adjust-SWIOTBL-bounce-buffer-size-for-SEV-guests/20201118-020049 base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 238c91115cd05c71447ea071624a4c9fe661f970 config: ia64-randconfig-r004-20201117 (attached as .config) compiler: ia64-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/ec17437123f45040f2139e36122a4021f1fd3b07
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Ashish-Kalra/swiotlb-Adjust-SWIOTBL-bounce-buffer-size-for-SEV-guests/20201118-020049 git checkout ec17437123f45040f2139e36122a4021f1fd3b07
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=ia64
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> kernel/dma/swiotlb.c:166:29: warning: no previous prototype for 'arch_swiotlb_adjust' [-Wmissing-prototypes]
166 | unsigned long __init __weak arch_swiotlb_adjust(unsigned long size)
| ^~~~~~~~~~~~~~~~~~~
kernel/dma/swiotlb.c:305:1: warning: no previous prototype for 'swiotlb_late_init_with_default_size' [-Wmissing-prototypes]
305 | swiotlb_late_init_with_default_size(size_t default_size)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vim +/arch_swiotlb_adjust +166 kernel/dma/swiotlb.c
165
> 166 unsigned long __init __weak arch_swiotlb_adjust(unsigned long size)
167 {
168 return 0;
169 }
170
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botThank you for the patch! Perhaps something to improve:
Hi Ashish,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on tip/x86/core]
[also build test WARNING on tip/x86/mm hch-configfs/for-next v5.10-rc4 next-20201117] [cannot apply to swiotlb/linux-next]
[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]
url: https://github.com/0day-ci/linux/commits/Ashish-Kalra/swiotlb-Adjust-SWIOTBL-bounce-buffer-size-for-SEV-guests/20201118-020049 base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 238c91115cd05c71447ea071624a4c9fe661f970 config: arm64-randconfig-r036-20201117 (attached as .config) compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project ace9653c11c6308401dcda2e8b26bf97e6e66e30)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/ec17437123f45040f2139e36122a4021f1fd3b07
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Ashish-Kalra/swiotlb-Adjust-SWIOTBL-bounce-buffer-size-for-SEV-guests/20201118-020049 git checkout ec17437123f45040f2139e36122a4021f1fd3b07
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> kernel/dma/swiotlb.c:166:29: warning: no previous prototype for function 'arch_swiotlb_adjust' [-Wmissing-prototypes]
unsigned long __init __weak arch_swiotlb_adjust(unsigned long size)
^ kernel/dma/swiotlb.c:166:1: note: declare 'static' if the function is not intended to be used outside of this translation unit unsigned long __init __weak arch_swiotlb_adjust(unsigned long size)
^
static
kernel/dma/swiotlb.c:305:1: warning: no previous prototype for function 'swiotlb_late_init_with_default_size' [-Wmissing-prototypes] swiotlb_late_init_with_default_size(size_t default_size)
^
kernel/dma/swiotlb.c:304:1: note: declare 'static' if the function is not intended to be used outside of this translation unit int
^
static
2 warnings generated.
vim +/arch_swiotlb_adjust +166 kernel/dma/swiotlb.c
165
> 166 unsigned long __init __weak arch_swiotlb_adjust(unsigned long size)
167 {
168 return 0;
169 }
170
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Ashish,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on tip/x86/core]
[also build test ERROR on tip/x86/mm hch-configfs/for-next v5.10-rc4 next-20201117] [cannot apply to swiotlb/linux-next]
[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]
url: https://github.com/0day-ci/linux/commits/Ashish-Kalra/swiotlb-Adjust-SWIOTBL-bounce-buffer-size-for-SEV-guests/20201118-020049 base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 238c91115cd05c71447ea071624a4c9fe661f970 config: i386-randconfig-r006-20201117 (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0 reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/ec17437123f45040f2139e36122a4021f1fd3b07 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Ashish-Kalra/swiotlb-Adjust-SWIOTBL-bounce-buffer-size-for-SEV-guests/20201118-020049 git checkout ec17437123f45040f2139e36122a4021f1fd3b07 # save the attached .config to linux build tree make W=1 ARCH=i386
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
ld: arch/x86/kernel/setup.o: in function `setup_arch':
>> arch/x86/kernel/setup.c:1169: undefined reference to `swiotlb_adjust'
vim +1169 arch/x86/kernel/setup.c
1098
1099 reserve_real_mode();
1100
1101 trim_platform_memory_ranges();
1102 trim_low_memory_range();
1103
1104 init_mem_mapping();
1105
1106 idt_setup_early_pf();
1107
1108 /*
1109 * Update mmu_cr4_features (and, indirectly, trampoline_cr4_features)
1110 * with the current CR4 value. This may not be necessary, but
1111 * auditing all the early-boot CR4 manipulation would be needed to
1112 * rule it out.
1113 *
1114 * Mask off features that don't work outside long mode (just
1115 * PCIDE for now).
1116 */
1117 mmu_cr4_features = __read_cr4() & ~X86_CR4_PCIDE;
1118
1119 memblock_set_current_limit(get_max_mapped());
1120
1121 /*
1122 * NOTE: On x86-32, only from this point on, fixmaps are ready for use.
1123 */
1124
1125 #ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT
1126 if (init_ohci1394_dma_early)
1127 init_ohci1394_dma_on_all_controllers();
1128 #endif
1129 /* Allocate bigger log buffer */
1130 setup_log_buf(1);
1131
1132 if (efi_enabled(EFI_BOOT)) {
1133 switch (boot_params.secure_boot) {
1134 case efi_secureboot_mode_disabled:
1135 pr_info("Secure boot disabled\n");
1136 break;
1137 case efi_secureboot_mode_enabled:
1138 pr_info("Secure boot enabled\n");
1139 break;
1140 default:
1141 pr_info("Secure boot could not be determined\n");
1142 break;
1143 }
1144 }
1145
1146 reserve_initrd();
1147
1148 acpi_table_upgrade();
1149
1150 vsmp_init();
1151
1152 io_delay_init();
1153
1154 early_platform_quirks();
1155
1156 /*
1157 * Parse the ACPI tables for possible boot-time SMP configuration.
1158 */
1159 acpi_boot_table_init();
1160
1161 early_acpi_boot_init();
1162
1163 initmem_init();
1164 dma_contiguous_reserve(max_pfn_mapped << PAGE_SHIFT);
1165
1166 if (boot_cpu_has(X86_FEATURE_GBPAGES))
1167 hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
1168
> 1169 swiotlb_adjust();
1170
1171 /*
1172 * Reserve memory for crash kernel after SRAT is parsed so that it
1173 * won't consume hotpluggable memory.
1174 */
1175 reserve_crashkernel();
1176
1177 memblock_find_dma_reserve();
1178
1179 if (!early_xdbc_setup_hardware())
1180 early_xdbc_register_console();
1181
1182 x86_init.paging.pagetable_init();
1183
1184 kasan_init();
1185
1186 /*
1187 * Sync back kernel address range.
1188 *
1189 * FIXME: Can the later sync in setup_cpu_entry_areas() replace
1190 * this call?
1191 */
1192 sync_initial_page_table();
1193
1194 tboot_probe();
1195
1196 map_vsyscall();
1197
1198 generic_apic_probe();
1199
1200 early_quirks();
1201
1202 /*
1203 * Read APIC and some other early information from ACPI tables.
1204 */
1205 acpi_boot_init();
1206 sfi_init();
1207 x86_dtb_init();
1208
1209 /*
1210 * get boot-time SMP configuration:
1211 */
1212 get_smp_config();
1213
1214 /*
1215 * Systems w/o ACPI and mptables might not have it mapped the local
1216 * APIC yet, but prefill_possible_map() might need to access it.
1217 */
1218 init_apic_mappings();
1219
1220 prefill_possible_map();
1221
1222 init_cpu_to_node();
1223
1224 io_apic_init_mappings();
1225
1226 x86_init.hyper.guest_late_init();
1227
1228 e820__reserve_resources();
1229 e820__register_nosave_regions(max_pfn);
1230
1231 x86_init.resources.reserve_resources();
1232
1233 e820__setup_pci_gap();
1234
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org