[4/4] cc-swiotlb: Allow set swiotlb watermark from cmdline

Message ID 20230128083254.86012-5-GuoRui.Yu@linux.alibaba.com
State New
Headers
Series [1/4] swiotlb: Split common code from swiotlb.{c,h} |

Commit Message

Guorui Yu Jan. 28, 2023, 8:32 a.m. UTC
  Signed-off-by: GuoRui.Yu <GuoRui.Yu@linux.alibaba.com>
---
 .../admin-guide/kernel-parameters.txt         | 11 ++++++
 kernel/dma/cc-swiotlb.c                       | 34 ++++++++++++++++++-
 2 files changed, 44 insertions(+), 1 deletion(-)
  

Comments

kernel test robot Jan. 28, 2023, 8:19 p.m. UTC | #1
Hi GuoRui.Yu",

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on tip/x86/core]
[also build test WARNING on joro-iommu/next driver-core/driver-core-testing driver-core/driver-core-next driver-core/driver-core-linus linus/master v6.2-rc5 next-20230127]
[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/GuoRui-Yu/swiotlb-Add-a-new-cc-swiotlb-implementation-for-Confidential-VMs/20230128-181154
patch link:    https://lore.kernel.org/r/20230128083254.86012-5-GuoRui.Yu%40linux.alibaba.com
patch subject: [PATCH 4/4] cc-swiotlb: Allow set swiotlb watermark from cmdline
config: nios2-buildonly-randconfig-r002-20230129 (https://download.01.org/0day-ci/archive/20230129/202301290417.n1KMDPQt-lkp@intel.com/config)
compiler: nios2-linux-gcc (GCC) 12.1.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/intel-lab-lkp/linux/commit/fcbd538c875f5f1086682829671935d7155e4029
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review GuoRui-Yu/swiotlb-Add-a-new-cc-swiotlb-implementation-for-Confidential-VMs/20230128-181154
        git checkout fcbd538c875f5f1086682829671935d7155e4029
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=nios2 olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=nios2 SHELL=/bin/bash kernel/dma/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   kernel/dma/cc-swiotlb.c: In function 'swiotlb_bounce':
   kernel/dma/cc-swiotlb.c:200:32: warning: variable 'tlb_end' set but not used [-Wunused-but-set-variable]
     200 |         phys_addr_t orig_addr, tlb_end, slot_start, slot_end = tlb_addr ;
         |                                ^~~~~~~
   In file included from kernel/dma/cc-swiotlb.c:13:
   kernel/dma/cc-swiotlb.c: In function 'cc_populate_pages':
>> include/linux/kern_levels.h:5:25: warning: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'size_t' {aka 'unsigned int'} [-Wformat=]
       5 | #define KERN_SOH        "\001"          /* ASCII Start Of Header */
         |                         ^~~~~~
   include/linux/printk.h:429:25: note: in definition of macro 'printk_index_wrap'
     429 |                 _p_func(_fmt, ##__VA_ARGS__);                           \
         |                         ^~~~
   include/linux/printk.h:530:9: note: in expansion of macro 'printk'
     530 |         printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
         |         ^~~~~~
   include/linux/kern_levels.h:14:25: note: in expansion of macro 'KERN_SOH'
      14 | #define KERN_INFO       KERN_SOH "6"    /* informational */
         |                         ^~~~~~~~
   include/linux/printk.h:530:16: note: in expansion of macro 'KERN_INFO'
     530 |         printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
         |                ^~~~~~~~~
   kernel/dma/cc-swiotlb.c:411:9: note: in expansion of macro 'pr_info'
     411 |         pr_info("bounce buffer size adjusted to %luMB", size >> 20);
         |         ^~~~~~~
   kernel/dma/cc-swiotlb.c: At top level:
   kernel/dma/cc-swiotlb.c:524:13: warning: no previous prototype for 'swiotlb_hint_cpus' [-Wmissing-prototypes]
     524 | void __init swiotlb_hint_cpus(int cpus) {}
         |             ^~~~~~~~~~~~~~~~~


vim +5 include/linux/kern_levels.h

314ba3520e513a Joe Perches 2012-07-30  4  
04d2c8c83d0e3a Joe Perches 2012-07-30 @5  #define KERN_SOH	"\001"		/* ASCII Start Of Header */
04d2c8c83d0e3a Joe Perches 2012-07-30  6  #define KERN_SOH_ASCII	'\001'
04d2c8c83d0e3a Joe Perches 2012-07-30  7
  

Patch

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 6cfa6e3996cf..ad510e392998 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -6090,6 +6090,17 @@ 
 			         wouldn't be automatically used by the kernel
 			noforce -- Never use bounce buffers (for debugging)
 
+	cc_swiotlb=	[X86]
+			Format: { [<int>, <int>, <int>, <int>]| force ]}
+			Set the swiotlb TLB buffer initial size for Confidential Computing like
+			TDX and SEV-SNP.
+			<int> -- Number of I/O TLB of 256 bytes
+			<int> -- Number of I/O TLB of 4 KiB
+			<int> -- Number of I/O TLB of 64 KiB
+			<int> -- Number of I/O TLB of 256 KiB
+			force -- force using of cc bounce buffers even if they
+			         wouldn't be automatically used by the kernel (for debugging)
+
 	switches=	[HW,M68k]
 
 	sysctl.*=	[KNL]
diff --git a/kernel/dma/cc-swiotlb.c b/kernel/dma/cc-swiotlb.c
index e85d3ce2272f..ff1cc52189b9 100644
--- a/kernel/dma/cc-swiotlb.c
+++ b/kernel/dma/cc-swiotlb.c
@@ -52,6 +52,32 @@  enum SLOT_SIZE {
 
 const unsigned int SLOT_SIZES[NR_OF_SIZES] = {512, 2048, 4096, 16 * 1024, 512 * 1024};
 unsigned int WATERMARK_SLOT_SIZE[NR_OF_SIZES] = {256, 16384, 8192, 1024, 32};
+static bool swiotlb_force_bounce = false;
+
+static int __init
+setup_io_tlb_watermark(char *str)
+{
+	int i;
+	size_t size;
+
+	for (i = 0; i < NR_OF_SIZES; i++) {
+		if (isdigit(*str)) {
+			size = simple_strtoul(str, &str, 0);
+			WATERMARK_SLOT_SIZE[i] = size;
+
+			if (*str == ',')
+				++str;
+		}
+		else
+			break;
+	}
+
+	if (!strcmp(str, "force"))
+		swiotlb_force_bounce = true;
+
+	return 0;
+}
+early_param("cc_swiotlb", setup_io_tlb_watermark);
 
 struct slot_terrace {
 	struct list_head slots_by_size[NR_OF_SIZES];
@@ -330,6 +356,7 @@  static void cc_populate_pages(void)
 	size_t i, j, k;
 	const size_t max_bytes_per_alloc = (MAX_ORDER_NR_PAGES << PAGE_SHIFT);
 	size_t remain_alloc_size, rounds, round_size, round_slot_nr;
+	size_t size = 0;
 
 	for (i = 0; i < NR_OF_SIZES; i++) {
 		if (atomic_read(&terrace.free_count_by_size[i]) > WATERMARK_SLOT_SIZE[i])
@@ -377,6 +404,11 @@  static void cc_populate_pages(void)
 			spin_unlock_irqrestore(&terrace.lock_by_size[i], flags);
 		}
 	}
+
+	for (i = 0; i < NR_OF_SIZES; i++) {
+		size += (size_t)atomic_read(&terrace.total_count_by_size[i]) * SLOT_SIZES[i];
+	}
+	pr_info("bounce buffer size adjusted to %luMB", size >> 20);
 }
 
 static int kccd(void *p)
@@ -437,7 +469,7 @@  void __init swiotlb_init(bool addressing_limit, unsigned int flags)
 		return;
 	}
 
-	io_tlb_default_mem.force_bounce = cc_platform_has(CC_ATTR_MEM_ENCRYPT);
+	io_tlb_default_mem.force_bounce = cc_platform_has(CC_ATTR_MEM_ENCRYPT) || swiotlb_force_bounce;
 
 	xa_init_flags(io_tlb_default_mem.mapping, 0);