[v6,29/30] LoongArch: KVM: Enable kvm config and add the makefile

Message ID 20230412083023.1693910-30-zhaotianrui@loongson.cn
State New
Headers
Series Add KVM LoongArch support |

Commit Message

zhaotianrui April 12, 2023, 8:30 a.m. UTC
  Enable loongarch kvm config and add the makefile to support build kvm
module.

Signed-off-by: Tianrui Zhao <zhaotianrui@loongson.cn>
---
 arch/loongarch/Kbuild                      |  1 +
 arch/loongarch/Kconfig                     |  2 ++
 arch/loongarch/configs/loongson3_defconfig |  2 ++
 arch/loongarch/kvm/Kconfig                 | 38 ++++++++++++++++++++++
 arch/loongarch/kvm/Makefile                | 22 +++++++++++++
 5 files changed, 65 insertions(+)
 create mode 100644 arch/loongarch/kvm/Kconfig
 create mode 100644 arch/loongarch/kvm/Makefile
  

Comments

kernel test robot April 13, 2023, 7:32 a.m. UTC | #1
Hi Tianrui,

kernel test robot noticed the following build errors:

[auto build test ERROR on kvm/queue]
[also build test ERROR on linus/master v6.3-rc6 next-20230412]
[cannot apply to kvm/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#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Tianrui-Zhao/LoongArch-KVM-Add-kvm-related-header-files/20230412-163935
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue
patch link:    https://lore.kernel.org/r/20230412083023.1693910-30-zhaotianrui%40loongson.cn
patch subject: [PATCH v6 29/30] LoongArch: KVM: Enable kvm config and add the makefile
config: loongarch-randconfig-c033-20230413 (https://download.01.org/0day-ci/archive/20230413/202304131526.iXfLaVZc-lkp@intel.com/config)
compiler: loongarch64-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/19c2472bf2612603ce4671e06fb185516a99e040
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Tianrui-Zhao/LoongArch-KVM-Add-kvm-related-header-files/20230412-163935
        git checkout 19c2472bf2612603ce4671e06fb185516a99e040
        # 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=loongarch olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=loongarch SHELL=/bin/bash arch/loongarch/kvm/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>
| Link: https://lore.kernel.org/oe-kbuild-all/202304131526.iXfLaVZc-lkp@intel.com/

All error/warnings (new ones prefixed by >>):

   In file included from arch/loongarch/include/asm/cpu-info.h:11,
                    from arch/loongarch/include/asm/processor.h:13,
                    from arch/loongarch/include/asm/thread_info.h:15,
                    from include/linux/thread_info.h:60,
                    from include/asm-generic/preempt.h:5,
                    from ./arch/loongarch/include/generated/asm/preempt.h:1,
                    from include/linux/preempt.h:78,
                    from include/linux/percpu.h:6,
                    from include/linux/context_tracking_state.h:5,
                    from include/linux/hardirq.h:5,
                    from include/linux/kvm_host.h:7,
                    from arch/loongarch/kvm/vcpu.c:6:
   In function 'gcsr_read',
       inlined from 'kvm_save_hw_gcsr' at arch/loongarch/include/asm/kvm_csr.h:24:19:
>> arch/loongarch/include/asm/loongarch.h:244:9: warning: 'asm' operand 1 probably does not match constraints
     244 |         asm volatile (
         |         ^~~
>> arch/loongarch/include/asm/loongarch.h:244:9: error: impossible constraint in 'asm'
   In function 'gcsr_write',
       inlined from 'kvm_restore_hw_gcsr' at arch/loongarch/include/asm/kvm_csr.h:29:2:
   arch/loongarch/include/asm/loongarch.h:261:9: warning: 'asm' operand 1 probably does not match constraints
     261 |         asm volatile (
         |         ^~~


vim +/asm +244 arch/loongarch/include/asm/loongarch.h

f2ac457a61389b Huacai Chen  2022-05-31  238  
2c87b678639d25 Tianrui Zhao 2023-04-12  239  /* GCSR */
2c87b678639d25 Tianrui Zhao 2023-04-12  240  static inline u64 gcsr_read(u32 reg)
2c87b678639d25 Tianrui Zhao 2023-04-12  241  {
2c87b678639d25 Tianrui Zhao 2023-04-12  242  	u64 val = 0;
2c87b678639d25 Tianrui Zhao 2023-04-12  243  	/* Instructions will be available in binutils later */
2c87b678639d25 Tianrui Zhao 2023-04-12 @244  	asm volatile (
2c87b678639d25 Tianrui Zhao 2023-04-12  245  		"parse_r __reg, %[val]\n\t"
2c87b678639d25 Tianrui Zhao 2023-04-12  246  		/*
2c87b678639d25 Tianrui Zhao 2023-04-12  247  		 * read val from guest csr register %[reg]
2c87b678639d25 Tianrui Zhao 2023-04-12  248  		 * gcsrrd %[val], %[reg]
2c87b678639d25 Tianrui Zhao 2023-04-12  249  		 */
2c87b678639d25 Tianrui Zhao 2023-04-12  250  		".word 0x5 << 24 | %[reg] << 10 | 0 << 5 | __reg\n\t"
2c87b678639d25 Tianrui Zhao 2023-04-12  251  		: [val] "+r" (val)
2c87b678639d25 Tianrui Zhao 2023-04-12  252  		: [reg] "i" (reg)
2c87b678639d25 Tianrui Zhao 2023-04-12  253  		: "memory");
2c87b678639d25 Tianrui Zhao 2023-04-12  254  
2c87b678639d25 Tianrui Zhao 2023-04-12  255  	return val;
2c87b678639d25 Tianrui Zhao 2023-04-12  256  }
2c87b678639d25 Tianrui Zhao 2023-04-12  257
  
Xi Ruoyao April 13, 2023, 8:33 a.m. UTC | #2
On Thu, 2023-04-13 at 15:32 +0800, kernel test robot wrote:

> vim +/asm +244 arch/loongarch/include/asm/loongarch.h
> 
> f2ac457a61389b Huacai Chen  2022-05-31  238  
> 2c87b678639d25 Tianrui Zhao 2023-04-12  239  /* GCSR */
> 2c87b678639d25 Tianrui Zhao 2023-04-12  240  static inline u64 gcsr_read(u32 reg)

Try __always_inline instead of inline because this "function" will
definitely break up if it's not inlined.

And I guess we should have something like:

extern u64 __compiletime_error("gcsr_read parameter is not a constant") 
__gcsr_read_non_const(void);

static __always_inline u64 gcsr_read(u32 reg)
{
	u64 val = 0;

	if (!__builtin_constant_p(reg))
		return __gcsr_read_non_const();

	asm volatile (
... ...

Or perhaps we should just rewrite gcsr_read to a macro.

> 2c87b678639d25 Tianrui Zhao 2023-04-12  241  {
> 2c87b678639d25 Tianrui Zhao 2023-04-12  242     u64 val = 0;
> 2c87b678639d25 Tianrui Zhao 2023-04-12  243     /* Instructions will be available in binutils later */
> 2c87b678639d25 Tianrui Zhao 2023-04-12 @244     asm volatile (
> 2c87b678639d25 Tianrui Zhao 2023-04-12  245             "parse_r __reg, %[val]\n\t"
> 2c87b678639d25 Tianrui Zhao 2023-04-12  246             /*
> 2c87b678639d25 Tianrui Zhao 2023-04-12  247              * read val from guest csr register %[reg]
> 2c87b678639d25 Tianrui Zhao 2023-04-12  248              * gcsrrd %[val], %[reg]
> 2c87b678639d25 Tianrui Zhao 2023-04-12  249              */
> 2c87b678639d25 Tianrui Zhao 2023-04-12  250             ".word 0x5 << 24 | %[reg] << 10 | 0 << 5 | __reg\n\t"
> 2c87b678639d25 Tianrui Zhao 2023-04-12  251             : [val] "+r" (val)
> 2c87b678639d25 Tianrui Zhao 2023-04-12  252             : [reg] "i" (reg)
> 2c87b678639d25 Tianrui Zhao 2023-04-12  253             : "memory");
> 2c87b678639d25 Tianrui Zhao 2023-04-12  254  
> 2c87b678639d25 Tianrui Zhao 2023-04-12  255     return val;
> 2c87b678639d25 Tianrui Zhao 2023-04-12  256  }
> 2c87b678639d25 Tianrui Zhao 2023-04-12  257  
>
  
zhaotianrui April 13, 2023, 11:52 a.m. UTC | #3
在 2023年04月13日 16:33, Xi Ruoyao 写道:
> On Thu, 2023-04-13 at 15:32 +0800, kernel test robot wrote:
>
>> vim +/asm +244 arch/loongarch/include/asm/loongarch.h
>>
>> f2ac457a61389b Huacai Chen  2022-05-31  238
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  239  /* GCSR */
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  240  static inline u64 gcsr_read(u32 reg)
> Try __always_inline instead of inline because this "function" will
> definitely break up if it's not inlined.
>
> And I guess we should have something like:
>
> extern u64 __compiletime_error("gcsr_read parameter is not a constant")
> __gcsr_read_non_const(void);
>
> static __always_inline u64 gcsr_read(u32 reg)
> {
> 	u64 val = 0;
>
> 	if (!__builtin_constant_p(reg))
> 		return __gcsr_read_non_const();
>
> 	asm volatile (
> ... ...
>
> Or perhaps we should just rewrite gcsr_read to a macro.
>
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  241  {
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  242     u64 val = 0;
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  243     /* Instructions will be available in binutils later */
>> 2c87b678639d25 Tianrui Zhao 2023-04-12 @244     asm volatile (
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  245             "parse_r __reg, %[val]\n\t"
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  246             /*
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  247              * read val from guest csr register %[reg]
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  248              * gcsrrd %[val], %[reg]
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  249              */
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  250             ".word 0x5 << 24 | %[reg] << 10 | 0 << 5 | __reg\n\t"
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  251             : [val] "+r" (val)
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  252             : [reg] "i" (reg)
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  253             : "memory");
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  254
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  255     return val;
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  256  }
>> 2c87b678639d25 Tianrui Zhao 2023-04-12  257
>>
Thanks, it really should use __always_inline there, and I will reconsider to fix this function mistakes about compiling.

Thanks
Tianrui Zhao
  

Patch

diff --git a/arch/loongarch/Kbuild b/arch/loongarch/Kbuild
index b01f5cdb27e0..40be8a1696f9 100644
--- a/arch/loongarch/Kbuild
+++ b/arch/loongarch/Kbuild
@@ -2,6 +2,7 @@  obj-y += kernel/
 obj-y += mm/
 obj-y += net/
 obj-y += vdso/
+obj-y += kvm/
 
 # for cleaning
 subdir- += boot
diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 7fd51257e0ed..f9beeb2e0cc9 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -148,6 +148,7 @@  config LOONGARCH
 	select USE_PERCPU_NUMA_NODE_ID
 	select USER_STACKTRACE_SUPPORT
 	select ZONE_DMA32
+	select HAVE_KVM
 
 config 32BIT
 	bool
@@ -588,3 +589,4 @@  source "drivers/acpi/Kconfig"
 endmenu
 
 source "drivers/firmware/Kconfig"
+source "arch/loongarch/kvm/Kconfig"
diff --git a/arch/loongarch/configs/loongson3_defconfig b/arch/loongarch/configs/loongson3_defconfig
index e18213f01cc4..c99520374aef 100644
--- a/arch/loongarch/configs/loongson3_defconfig
+++ b/arch/loongarch/configs/loongson3_defconfig
@@ -63,6 +63,8 @@  CONFIG_EFI_ZBOOT=y
 CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER=y
 CONFIG_EFI_CAPSULE_LOADER=m
 CONFIG_EFI_TEST=m
+CONFIG_VIRTUALIZATION=y
+CONFIG_KVM=m
 CONFIG_MODULES=y
 CONFIG_MODULE_FORCE_LOAD=y
 CONFIG_MODULE_UNLOAD=y
diff --git a/arch/loongarch/kvm/Kconfig b/arch/loongarch/kvm/Kconfig
new file mode 100644
index 000000000000..8a999b4c0232
--- /dev/null
+++ b/arch/loongarch/kvm/Kconfig
@@ -0,0 +1,38 @@ 
+# SPDX-License-Identifier: GPL-2.0
+#
+# KVM configuration
+#
+
+source "virt/kvm/Kconfig"
+
+menuconfig VIRTUALIZATION
+	bool "Virtualization"
+	help
+	  Say Y here to get to see options for using your Linux host to run
+	  other operating systems inside virtual machines (guests).
+	  This option alone does not add any kernel code.
+
+	  If you say N, all options in this submenu will be skipped and
+	  disabled.
+
+if VIRTUALIZATION
+
+config KVM
+	tristate "Kernel-based Virtual Machine (KVM) support"
+	depends on HAVE_KVM
+	select MMU_NOTIFIER
+	select ANON_INODES
+	select PREEMPT_NOTIFIERS
+	select KVM_MMIO
+	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
+	select HAVE_KVM_VCPU_ASYNC_IOCTL
+	select HAVE_KVM_EVENTFD
+	select SRCU
+	help
+	  Support hosting virtualized guest machines using hardware
+	  virtualization extensions. You will need a fairly processor
+	  equipped with virtualization extensions.
+
+	  If unsure, say N.
+
+endif # VIRTUALIZATION
diff --git a/arch/loongarch/kvm/Makefile b/arch/loongarch/kvm/Makefile
new file mode 100644
index 000000000000..2335e873a6ef
--- /dev/null
+++ b/arch/loongarch/kvm/Makefile
@@ -0,0 +1,22 @@ 
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for LOONGARCH KVM support
+#
+
+ccflags-y += -I $(srctree)/$(src)
+
+include $(srctree)/virt/kvm/Makefile.kvm
+
+obj-$(CONFIG_KVM) += kvm.o
+
+kvm-y += main.o
+kvm-y += vm.o
+kvm-y += vmid.o
+kvm-y += tlb.o
+kvm-y += mmu.o
+kvm-y += vcpu.o
+kvm-y += exit.o
+kvm-y += interrupt.o
+kvm-y += timer.o
+kvm-y += switch.o
+kvm-y += csr_ops.o