Skip to contentSkip to conversation

KVM: s390: Introduce arm64 KVM

PATCH Superseded Steffen Eiden 7 participants 82 messages Aug 31 – Sep 17

Subsystems (23)
x86 KVM PowerPC MIPS +19 more subsystems

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
author reviewer
#18 lore raw

PATCH v7 3/23 KVM: Allow KVM implementations to switch off MMIO independent of Kconfig

In reply to Steffen Eiden

Only defining KVM_MMIO is not flexible enough for multi-KVM systems where one KVM implements MMIO but others do not. Solve this by additionally testing for the non-existence of KVM_NO_MMIO before including MMIO code to KVM.

---
 virt/kvm/coalesced_mmio.c | 4 ++++
 virt/kvm/coalesced_mmio.h | 2 +-
 virt/kvm/kvm_main.c       | 8 ++++----
 3 files changed, 9 insertions(+), 5 deletions(-)
virt/kvm/coalesced_mmio.c +4 -0
index 6b1d90161099..8ff9d9a6f47a 100644
@@ -15,6 +15,8 @@
 #include <linux/slab.h>
 #include <linux/kvm.h>
 
+#ifndef KVM_NO_MMIO
+
 #include "coalesced_mmio.h"
 
 static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev)
@@ -188,3 +190,5 @@ int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
 	 */
 	return 0;
 }
+
+#endif /* ifndef KVM_NO_MMIO */
virt/kvm/coalesced_mmio.h +1 -1
index 36f84264ed25..826af421a5be 100644
@@ -11,7 +11,7 @@
  *
  */
 
-#ifdef CONFIG_KVM_MMIO
+#if defined(CONFIG_KVM_MMIO) && !defined(KVM_NO_MMIO)
 
 #include <linux/list.h>
 
virt/kvm/kvm_main.c +4 -4
index 823b693d89e6..7f37d3b5af48 100644
@@ -4061,7 +4061,7 @@ static vm_fault_t kvm_vcpu_fault(struct vm_fault *vmf)
 	else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
 		page = virt_to_page(vcpu->arch.pio_data);
 #endif
-#ifdef CONFIG_KVM_MMIO
+#if defined(CONFIG_KVM_MMIO) && !defined(KVM_NO_MMIO)
 	else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
 		page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
 #endif
@@ -4902,7 +4902,7 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 	case KVM_CAP_ENABLE_CAP_VM:
 	case KVM_CAP_HALT_POLL:
 		return 1;
-#ifdef CONFIG_KVM_MMIO
+#if defined(CONFIG_KVM_MMIO) && !defined(KVM_NO_MMIO)
 	case KVM_CAP_COALESCED_MMIO:
 		return KVM_COALESCED_MMIO_PAGE_OFFSET;
 	case KVM_CAP_COALESCED_PIO:
@@ -5243,7 +5243,7 @@ static long kvm_vm_ioctl(struct file *filp,
 		break;
 	}
 #endif
-#ifdef CONFIG_KVM_MMIO
+#if defined(CONFIG_KVM_MMIO) && !defined(KVM_NO_MMIO)
 	case KVM_REGISTER_COALESCED_MMIO: {
 		struct kvm_coalesced_mmio_zone zone;
 
@@ -5577,7 +5577,7 @@ static long kvm_dev_ioctl(struct file *filp,
 #ifdef CONFIG_X86
 		r += PAGE_SIZE;    /* pio data page */
 #endif
-#ifdef CONFIG_KVM_MMIO
+#if defined(CONFIG_KVM_MMIO) && !defined(KVM_NO_MMIO)
 		r += PAGE_SIZE;    /* coalesced mmio ring page */
 #endif
 		break;
Signature · 1 line
2.53.0
author reviewer
#26 lore raw

PATCH v7 4/23 arm64: Use proper include variant

In reply to Steffen Eiden

Use <> for including the sysreg header as it is not discoverable from the current directory.

Fixes c07d8017bceb ("arm64/sysreg: Enable automatic generation of system register definitions")
---
 arch/arm64/include/asm/sysreg.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/sysreg.h +1 -1
index 7aa08d59d494..4bfdac9401bd 100644
@@ -173,7 +173,7 @@
  * come from here. The header relies on the definition of sys_reg()
  * earlier in this file.
  */
-#include "asm/sysreg-defs.h"
+#include <asm/sysreg-defs.h>
 
 /*
  * System registers, organised loosely by encoding but grouped together
Signature · 1 line
2.53.0

Steffen Eiden

author reviewer
#0 lore raw

v7 cover KVM: s390: Introduce arm64 KVM

Introduce arm-on-s390. Enable KVM-accelerated ARM CPU virtualisation on s390.

For the original, now partially outdated, cover letter, have a look at versions one to three [1].

To share arm64 code with arm-on-s390 KVM, s390 extracts relevant parts of arm64 definitions and implementation and uses them locally. This results in minimal changes to existing arm64 code and headers.

To enable s390 to selectively extract relevant definitions while skipping architecture-specific code (e.g., arm64 assembly), a shared marker is introduced. The ARM64_S390_COMMON macro marks regions of arm64 code that are shared with s390:

in arch/arm64/kvm/<code>.c

  <arm64 local code>
  #ifdef ARM64_S390_COMMON
  <shared code>
  #endif /* ARM64_S390_COMMON */
  <arm64 local code>

ARM64_S390_COMMON is defined for all arm64 compilation units.

During the s390 build, an allow-list of arm64 headers is extracted into arch/s390/include/generated/arm64/. If a header contains at least one shared region, only those regions are copied along with the include guard. If no shared region is found, s390 uses the full header, which is copied in its entirety. In such cases, a comment is added at the top of the header to notify arm64 developers that the file is shared with s390 and changes may impact s390.

For C files, the process is similar. Selected KVM/arm64 C files are partially extracted into arch/s390/kvm/arm64/generated/code.inc. The s390 arm64 KVM implementation includes these snippets to reuse arm64 code without changing linkage.

The goal is to share as much functionality as possible.

Userspace tools requiring KVM/arm64 uapi on s390 must use a vendored KVM API, as s390 will not ship nor install arm64 KVM uapi headers.

This approach reduces the churn, especially for arm64 while keeping the maintenance burden low.

Series structure

A) KVM symbol cleanup and refactoring (patches 1-3):
Prepare KVM to support two KVM modules in parallel (spin-off: [5]). Clean up KVM module symbol exports, making it possible to load two KVM modules side by side. Make the KVM device name configurable and allow KVM implementations to disable MMIO regardless of CONFIG_KVM_MMIO.

B) Arm64 header and code sharing preparation (patches 4-11): Mark shareable parts with ARM64_S390_COMMON. But first perform minor refactoring to ease integration into s390. All the refactoring/cleanup/fix patches could be picked before the actual share-marker commits (10/11)

C) Code sharing infrastructure for s390 (patches 12 & 13):
Add Makefile and scripts to extract and build arm64 headers and code for use by s390.

D) KVM on s390 (patches 14-23):
Introduce the SAE (Start Arm Execution) instruction as the s390 mechanism for running arm64 guests. Build up the new kvm-arm64 module incrementally.

A, B, and C could be picked individually without requiring the others so they could go through different trees if required. D obviously requires A-C.

Based on v7.3-rc1.

Steffen

Changes from v6 [4]:
- Fix vfio <-> kvm keepalive
- add Reviewed-bys and ACKs
arm64:
- fix SPSR_ELx description
- use kvm_arch_flag_bits enum instead of multiple defines
- minor requested changes
s390:
- fix various smaller issues
- removed picked s390 refactoring patches (thanks Christian)

Changes from v5 [3]:
- fix multiple rcu and use after free issues in vfio
- multiple smaller mostly cosmetic improvements
arm64:
- fixed regression in compat enum & cleaned up
- fixed SPSR_ELx definitions
- removed reset_core_reg extracting which is now is already done (thanks Fuad)
- renamed and moved register access function to vcpu_<foo> to kvm_emulate.h they are now at the same position and naming scheme like the preexisting ones
- fix resetting reserved field in fpsimd

s390:
- gmap: Guard and not move s390-guest only gmap-code
- split refactoring into more patches to ease review
- fixed minor bugs in header/code shared code generation
- Kconfig reorganization:
- removed KVM_S390, the KVM for s390 guests keeps using by CONFIG_KVM
- KVM_ARM64 now depends on KVM
- KVM is selectable (as it was before this series)
- All (s390) KVM configs are in arch/s390/kvm/Kconfig

Changes from v4 [2]:
- do not remove CONFIG_KVM_MMIO but let KVM/s390 opt out of MMIO
- use kstrdup_const for copying the kvm-device name
- use file based vfio tracking instead of inlining struct kvm refcounting arm64:
- reorganized arm64 code share & refactoring - first refactoring then sharing
- add arm64 refactoring to reduce shared regions
- shared a bit more function (definitions) to reduce no of shared regions (which would be shared in later series anyways)
- add patch to generate SPSR sysreg definitions
- add patch which defines an enum for the arm32 compat registers
s390:
- fix arm64 header generation wiring
- awk scripts are more robust and error out if an end marker is missing
- postfix combined headers with -gen instead of -part
- removed pre-fault-memory implementation
- implemented system supported feature sanitisation
- removed some unnecessary functions
- moved prefix, cmma, and storage key code from gmap to kvm/s390
- properly reset vxregs

Changes from v3

- Complete rework of the code sharing approach
- Introduced ARM64_S390_COMMON marker for selective extraction
- Build-time extraction instead of development-time sharing
- Minimal impact on arm64 codebase
- Improved gmap refactoring and integration

[1] Previous versions:
v1 https://lore.kernel.org/all/[email protected]/
v2 https://lore.kernel.org/all/[email protected]/
v3 https://lore.kernel.org/all/[email protected]/

[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]

Hendrik Brueckner (1):
s390/hwcaps: Report SAE support as hwcap

Steffen Eiden (22):
vfio: Use file-based reference counting for KVM
KVM: Make device name configurable
KVM: Allow KVM implementations to switch off MMIO independent of
Kconfig
arm64: Use proper include variant
arm64: ptrace: Use constants for compat register numbers
arm64: sysreg: Convert SPSR_ELx to automatic register generation
KVM: arm64: Access elements of vcpu_gp_regs individually
KVM: arm64: Use accessor functions for core regs
arm64: Prepare sharing arm64 headers with s390
arm64: Share arm64 headers with s390
KVM: arm64: Share arm64 code with s390
s390/tools: Use arm64 headers
KVM: s390: Use arm64 code
KVM: s390: Prepare KVM/s390 for a second KVM module
s390: Introduce Start Arm Execution instruction
KVM: s390: arm64: Introduce host definitions
KVM: s390: Add basic arm64 kvm module
KVM: s390: arm64: Implement required functions
KVM: s390: arm64: Implement vm/vcpu create destroy.
KVM: s390: arm64: Implement vCPU IOCTLs
KVM: s390: arm64: Implement basic page fault handler
KVM: s390: arm64: Add KVM_S390_ARM64 Kconfig and Makefile

 MAINTAINERS                                  |   1 +
 arch/arm64/Makefile                          |   5 +
 arch/arm64/include/asm/brk-imm.h             |   1 +
 arch/arm64/include/asm/esr.h                 |   5 +-
 arch/arm64/include/asm/kvm_arm.h             |   6 +-
 arch/arm64/include/asm/kvm_emulate.h         |  69 +-
 arch/arm64/include/asm/kvm_host.h            | 117 +--
 arch/arm64/include/asm/ptrace.h              |  64 +-
 arch/arm64/include/asm/sysreg.h              |  22 +-
 arch/arm64/kvm/arm.c                         |   3 +
 arch/arm64/kvm/guest.c                       |  22 +-
 arch/arm64/kvm/handle_exit.c                 |   6 +
 arch/arm64/kvm/hyp/exception.c               |   4 +-
 arch/arm64/kvm/hyp/include/hyp/adjust_pc.h   |   4 +-
 arch/arm64/kvm/hyp/include/hyp/switch.h      |   4 +-
 arch/arm64/kvm/mmio.c                        |   2 +
 arch/arm64/tools/sysreg                      |  51 ++
 arch/s390/configs/debug_defconfig            |   1 +
 arch/s390/configs/defconfig                  |   1 +
 arch/s390/include/arm64/kvm_emulate.h        | 130 ++++
 arch/s390/include/arm64/kvm_nested.h         |  11 +
 arch/s390/include/arm64/ptrace.h             |  11 +
 arch/s390/include/arm64/sysreg.h             |  13 +
 arch/s390/include/asm/Kbuild                 |   3 +
 arch/s390/include/asm/asm-prototypes.h       |   1 +
 arch/s390/include/asm/elf.h                  |   2 +
 arch/s390/include/asm/kvm.h                  |   6 +
 arch/s390/include/asm/kvm_host.h             |   9 +
 arch/s390/include/asm/kvm_host_arm64.h       | 211 ++++++
 arch/s390/include/asm/kvm_host_arm64_types.h | 118 +++
 arch/s390/include/asm/kvm_host_s390.h        |   1 +
 arch/s390/include/asm/kvm_host_types.h       |   4 +
 arch/s390/include/asm/sae.h                  |  57 ++
 arch/s390/include/asm/sclp.h                 |   5 +-
 arch/s390/include/asm/stacktrace.h           |   5 +
 arch/s390/kernel/asm-offsets.c               |   1 +
 arch/s390/kernel/entry.S                     |  22 +
 arch/s390/kernel/processor.c                 |   3 +
 arch/s390/kvm/Kconfig                        |  29 +
 arch/s390/kvm/Makefile                       |   1 +
 arch/s390/kvm/arm64/.gitignore               |   2 +
 arch/s390/kvm/arm64/Makefile                 |  91 +++
 arch/s390/kvm/arm64/Makefile.gen             |  27 +
 arch/s390/kvm/arm64/arm.c                    | 758 +++++++++++++++++++
 arch/s390/kvm/arm64/arm.h                    |  61 ++
 arch/s390/kvm/arm64/copy-arm64c.awk          |  72 ++
 arch/s390/kvm/arm64/guest.c                  | 135 ++++
 arch/s390/kvm/arm64/handle_exit.c            | 131 ++++
 arch/s390/kvm/arm64/handle_exit.h            |   9 +
 arch/s390/kvm/arm64/inject_fault.c           |  21 +
 arch/s390/kvm/arm64/mmio.c                   |  15 +
 arch/s390/kvm/arm64/mmu.c                    | 177 +++++
 arch/s390/kvm/arm64/reset.c                  |  71 ++
 arch/s390/kvm/arm64/trace.h                  |  42 +
 arch/s390/kvm/gmap/faultin.c                 |   5 +
 arch/s390/kvm/gmap/gmap.c                    |   4 +
 arch/s390/kvm/gmap/kvm_mmu.c                 |   4 +
 arch/s390/kvm/gmap/trace_gmap.h              |  17 +
 arch/s390/kvm/s390/s390.c                    |   8 +
 arch/s390/tools/Makefile                     |   9 +-
 arch/s390/tools/Makefile.arm64h              |  56 ++
 arch/s390/tools/copy-arm64h-full.awk         |  47 ++
 arch/s390/tools/copy-arm64h.awk              |  72 ++
 arch/s390/tools/opcodes.txt                  |   3 +
 arch/x86/include/asm/kvm_page_track.h        |   8 +-
 arch/x86/kvm/Makefile                        |   5 +-
 arch/x86/kvm/mmu/page_track.c                |  20 +-
 drivers/s390/char/sclp_early.c               |   1 +
 drivers/s390/crypto/vfio_ap_ops.c            |  20 +-
 drivers/vfio/group.c                         |  11 +-
 drivers/vfio/pci/vfio_pci_zdev.c             |   8 +-
 drivers/vfio/vfio.h                          |  10 +-
 drivers/vfio/vfio_main.c                     |  57 +-
 include/linux/kvm_host.h                     |  12 +-
 include/linux/vfio.h                         |   5 +-
 virt/kvm/async_pf.c                          |  13 +-
 virt/kvm/async_pf.h                          |   4 +-
 virt/kvm/coalesced_mmio.c                    |   4 +
 virt/kvm/coalesced_mmio.h                    |   2 +-
 virt/kvm/kvm_main.c                          |  70 +-
 virt/kvm/vfio.c                              |  13 +-
 81 files changed, 2930 insertions(+), 201 deletions(-)
 create mode 100644 arch/s390/include/arm64/kvm_emulate.h
 create mode 100644 arch/s390/include/arm64/kvm_nested.h
 create mode 100644 arch/s390/include/arm64/ptrace.h
 create mode 100644 arch/s390/include/arm64/sysreg.h
 create mode 100644 arch/s390/include/asm/kvm.h
 create mode 100644 arch/s390/include/asm/kvm_host_arm64.h
 create mode 100644 arch/s390/include/asm/kvm_host_arm64_types.h
 create mode 100644 arch/s390/include/asm/sae.h
 create mode 100644 arch/s390/kvm/arm64/.gitignore
 create mode 100644 arch/s390/kvm/arm64/Makefile
 create mode 100644 arch/s390/kvm/arm64/Makefile.gen
 create mode 100644 arch/s390/kvm/arm64/arm.c
 create mode 100644 arch/s390/kvm/arm64/arm.h
 create mode 100644 arch/s390/kvm/arm64/copy-arm64c.awk
 create mode 100644 arch/s390/kvm/arm64/guest.c
 create mode 100644 arch/s390/kvm/arm64/handle_exit.c
 create mode 100644 arch/s390/kvm/arm64/handle_exit.h
 create mode 100644 arch/s390/kvm/arm64/inject_fault.c
 create mode 100644 arch/s390/kvm/arm64/mmio.c
 create mode 100644 arch/s390/kvm/arm64/mmu.c
 create mode 100644 arch/s390/kvm/arm64/reset.c
 create mode 100644 arch/s390/kvm/arm64/trace.h
 create mode 100644 arch/s390/tools/Makefile.arm64h
 create mode 100644 arch/s390/tools/copy-arm64h-full.awk
 create mode 100644 arch/s390/tools/copy-arm64h.awk

base-commit: cee9395acd8043be0644b25c34bfa86623f2b935

Signature · 1 line
2.53.0
author reviewer
#30 lore raw

PATCH v7 6/23 arm64: sysreg: Convert SPSR_ELx to automatic register generation

Convert SPSR_EL{1, 2, 12} to automatic register generation as per DDIO616 2026-06. No functional change.

---
 arch/arm64/include/asm/sysreg.h |  3 --
 arch/arm64/tools/sysreg         | 51 +++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 3 deletions(-)
arch/arm64/include/asm/sysreg.h +0 -3
index 4bfdac9401bd..1aa601c95188 100644
@@ -295,7 +295,6 @@
 #define SYS_APGAKEYLO_EL1		sys_reg(3, 0, 2, 3, 0)
 #define SYS_APGAKEYHI_EL1		sys_reg(3, 0, 2, 3, 1)
 
-#define SYS_SPSR_EL1			sys_reg(3, 0, 4, 0, 0)
 #define SYS_ELR_EL1			sys_reg(3, 0, 4, 0, 1)
 
 #define SYS_ICC_PMR_EL1			sys_reg(3, 0, 4, 6, 0)
@@ -518,7 +517,6 @@
 #define SYS_VTTBR_EL2			sys_reg(3, 4, 2, 1, 0)
 
 #define SYS_HAFGRTR_EL2			sys_reg(3, 4, 3, 1, 6)
-#define SYS_SPSR_EL2			sys_reg(3, 4, 4, 0, 0)
 #define SYS_ELR_EL2			sys_reg(3, 4, 4, 0, 1)
 #define SYS_SP_EL1			sys_reg(3, 4, 4, 1, 0)
 #define SYS_SPSR_irq			sys_reg(3, 4, 4, 3, 0)
@@ -604,7 +602,6 @@
 #define SYS_BRBCR_EL12			sys_reg(2, 5, 9, 0, 0)
 #define SYS_TTBR0_EL12			sys_reg(3, 5, 2, 0, 0)
 #define SYS_TTBR1_EL12			sys_reg(3, 5, 2, 0, 1)
-#define SYS_SPSR_EL12			sys_reg(3, 5, 4, 0, 0)
 #define SYS_ELR_EL12			sys_reg(3, 5, 4, 0, 1)
 #define SYS_AFSR0_EL12			sys_reg(3, 5, 5, 1, 0)
 #define SYS_AFSR1_EL12			sys_reg(3, 5, 5, 1, 1)
arch/arm64/tools/sysreg +51 -0

57 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#34 lore raw

PATCH v7 8/23 KVM: arm64: Use accessor functions for core regs

In reply to Steffen Eiden

Instead of accessing the value directly use assessor function that abstract the actual location of the register values. Add a helper function to reset fpsimd registers. Enable other KVM implementations to reuse this code.

No functional changes.

---
 arch/arm64/include/asm/kvm_emulate.h | 34 ++++++++++++++++++++++++++--
 arch/arm64/kvm/guest.c               | 14 ++++++------
 2 files changed, 39 insertions(+), 9 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +32 -2
index 8603766c1b4d..b31d50bdf59e 100644
@@ -141,6 +141,31 @@ static __always_inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
 	return (unsigned long *)&vcpu->arch.ctxt.regs.pstate;
 }
 
+static __always_inline unsigned long *vcpu_sp_el0(struct kvm_vcpu *vcpu)
+{
+	return (unsigned long *)&vcpu->arch.ctxt.regs.sp;
+}
+
+static __always_inline u64 *vcpu_sp_el1(struct kvm_vcpu *vcpu)
+{
+	return __ctxt_sys_reg(&vcpu->arch.ctxt, SP_EL1);
+}
+
+static __always_inline __u128 *vcpu_vreg(struct kvm_vcpu *vcpu, int n)
+{
+	return &vcpu->arch.ctxt.fp_regs.vregs[n];
+}
+
+static __always_inline __u32 *vcpu_fpsr(struct kvm_vcpu *vcpu)
+{
+	return &vcpu->arch.ctxt.fp_regs.fpsr;
+}
+
+static __always_inline __u32 *vcpu_fpcr(struct kvm_vcpu *vcpu)
+{
+	return &vcpu->arch.ctxt.fp_regs.fpcr;
+}
+
 static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
 {
 	return !!(*vcpu_cpsr(vcpu) & PSR_MODE32_BIT);
@@ -737,6 +762,11 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu)
 	}
 }
 
+static inline void kvm_reset_fpsimd(struct kvm_vcpu *vcpu)
+{
+	memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs));
+}
+
 /* Reset a vcpu's core registers. */
 static inline void kvm_reset_vcpu_core(struct kvm_vcpu *vcpu)
 {
@@ -752,8 +782,8 @@ static inline void kvm_reset_vcpu_core(struct kvm_vcpu *vcpu)
 	/* Reset core registers */
 	memset(vcpu_gp_regs(vcpu), 0, sizeof(vcpu_gp_regs(vcpu)));
 	*vcpu_pc(vcpu) = 0;
-	vcpu->arch.ctxt.regs.sp = 0;
-	memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs));
+	*vcpu_sp_el0(vcpu) = 0;
+	kvm_reset_fpsimd(vcpu);
 	vcpu->arch.ctxt.spsr_abt = 0;
 	vcpu->arch.ctxt.spsr_und = 0;
 	vcpu->arch.ctxt.spsr_irq = 0;
arch/arm64/kvm/guest.c +7 -7
index 81d850d5a328..773f6c8e5026 100644
@@ -137,16 +137,16 @@ static void *core_reg_addr(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 		return &vcpu_gp_regs(vcpu)[off];
 
 	case KVM_REG_ARM_CORE_REG(regs.sp):
-		return &vcpu->arch.ctxt.regs.sp;
+		return vcpu_sp_el0(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(regs.pc):
-		return &vcpu->arch.ctxt.regs.pc;
+		return vcpu_pc(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(regs.pstate):
-		return &vcpu->arch.ctxt.regs.pstate;
+		return vcpu_cpsr(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(sp_el1):
-		return __ctxt_sys_reg(&vcpu->arch.ctxt, SP_EL1);
+		return vcpu_sp_el1(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(elr_el1):
 		return __ctxt_sys_reg(&vcpu->arch.ctxt, ELR_EL1);
@@ -170,13 +170,13 @@ static void *core_reg_addr(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	     KVM_REG_ARM_CORE_REG(fp_regs.vregs[31]):
 		off -= KVM_REG_ARM_CORE_REG(fp_regs.vregs[0]);
 		off /= 4;
-		return &vcpu->arch.ctxt.fp_regs.vregs[off];
+		return vcpu_vreg(vcpu, off);
 
 	case KVM_REG_ARM_CORE_REG(fp_regs.fpsr):
-		return &vcpu->arch.ctxt.fp_regs.fpsr;
+		return vcpu_fpsr(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(fp_regs.fpcr):
-		return &vcpu->arch.ctxt.fp_regs.fpcr;
+		return vcpu_fpcr(vcpu);
 
 	default:
 		return NULL;
Signature · 1 line
2.53.0
author reviewer
#1 lore raw

PATCH v7 1/23 vfio: Use file-based reference counting for KVM

In reply to Steffen Eiden

Replace manual module reference counting with file-based reference counting for KVM integration. Previously, VFIO used symbol_get() to obtain function pointers for kvm_get_kvm_safe() and kvm_put_kvm(), then manually tracked module references through these symbols. This approach required storing the put_kvm function pointer in each device and carefully managing symbol references. Remove the put_kvm field in struct vfio_device as is it no longer used.

Pass struct file pointers instead of struct kvm pointers throughout the VFIO-KVM interface. This leverages the kernel's existing file reference counting mechanism via get_file()/get_file_active() and fput(), eliminating the need for manual module reference tracking. The file->private_data field provides access to the underlying struct kvm when needed.

group->kvm and df->kvm hold a reference of their own, taken when the pointer is stored and dropped when it is overwritten or cleared. They have to: the kvm-vfio device fd holds a VM reference of its own, so the VM file can be closed and released while the kvm-vfio device is still alive and still pointing at it. filp_cachep is SLAB_TYPESAFE_BY_RCU, so a stale pointer left in those slots could be made to reference a recycled, unrelated file.

kvm->file itself carries no reference, so that it does not pin the VM. It is only ever read with get_file_active(), which is safe because kvm_vm_release() clears it, i.e. before the struct file is freed.

This simplifies the code and removes all remaining externally exported symbols for KVM, paving the path for a second concurrent KVM module.

---
 arch/x86/include/asm/kvm_page_track.h |  8 ++--
 arch/x86/kvm/Makefile                 |  5 +--
 arch/x86/kvm/mmu/page_track.c         | 20 ++++++----
 drivers/s390/crypto/vfio_ap_ops.c     | 20 +++++++---
 drivers/vfio/group.c                  | 11 +++++-
 drivers/vfio/pci/vfio_pci_zdev.c      |  8 +++-
 drivers/vfio/vfio.h                   | 10 ++---
 drivers/vfio/vfio_main.c              | 57 +++++++++------------------
 include/linux/kvm_host.h              |  8 ++++
 include/linux/vfio.h                  |  5 +--
 virt/kvm/kvm_main.c                   | 20 ++++++++--
 virt/kvm/vfio.c                       | 13 ++++--
 12 files changed, 107 insertions(+), 78 deletions(-)
arch/x86/include/asm/kvm_page_track.h +4 -4
index 3d040741044b..046a25c8fe4f 100644
@@ -44,13 +44,13 @@ struct kvm_page_track_notifier_node {
 				    struct kvm_page_track_notifier_node *node);
 };
 
-int kvm_page_track_register_notifier(struct kvm *kvm,
+int kvm_page_track_register_notifier(struct file *file,
 				     struct kvm_page_track_notifier_node *n);
-void kvm_page_track_unregister_notifier(struct kvm *kvm,
+void kvm_page_track_unregister_notifier(struct file *file,
 					struct kvm_page_track_notifier_node *n);
 
-int kvm_write_track_add_gfn(struct kvm *kvm, gfn_t gfn);
-int kvm_write_track_remove_gfn(struct kvm *kvm, gfn_t gfn);
+int kvm_write_track_add_gfn(struct file *file, gfn_t gfn);
+int kvm_write_track_remove_gfn(struct file *file, gfn_t gfn);
 #else
 /*
  * Allow defining a node in a structure even if page tracking is disabled, e.g.
arch/x86/kvm/Makefile +1 -4
index 0474604ab8a1..96f89d6f7e6a 100644
@@ -60,10 +60,7 @@ exports_grep_trailer := --include='*.[ch]' -nrw $(srctree)/virt/kvm $(srctree)/a
 			grep -v -e kvm_page_track_register_notifier \
 				-e kvm_page_track_unregister_notifier \
 				-e kvm_write_track_add_gfn \
-				-e kvm_write_track_remove_gfn \
-				-e kvm_get_kvm \
-				-e kvm_get_kvm_safe \
-				-e kvm_put_kvm
+				-e kvm_write_track_remove_gfn
 
 # Force grep to emit a goofy group separator that can in turn be replaced with
 # the above newline macro (newlines in Make are a nightmare).  Note, grep only
arch/x86/kvm/mmu/page_track.c +12 -8
index 7e8195a311bb..98f133e4f0b3 100644
@@ -237,10 +237,11 @@ static int kvm_enable_external_write_tracking(struct kvm *kvm)
  * register the notifier so that event interception for the tracked guest
  * pages can be received.
  */
-int kvm_page_track_register_notifier(struct kvm *kvm,
+int kvm_page_track_register_notifier(struct file *file,
 				     struct kvm_page_track_notifier_node *n)
 {
 	struct kvm_page_track_notifier_head *head;
+	struct kvm *kvm = file_to_kvm(file);
 	int r;
 
 	if (!kvm || kvm->mm != current->mm)
@@ -252,7 +253,7 @@ int kvm_page_track_register_notifier(struct kvm *kvm,
 			return r;
 	}
 
-	kvm_get_kvm(kvm);
+	get_file(file);
 
 	head = &kvm->arch.track_notifier_head;
 
@@ -267,10 +268,11 @@ EXPORT_SYMBOL_GPL(kvm_page_track_register_notifier);
  * stop receiving the event interception. It is the opposed operation of
  * kvm_page_track_register_notifier().
  */
-void kvm_page_track_unregister_notifier(struct kvm *kvm,
+void kvm_page_track_unregister_notifier(struct file *file,
 					struct kvm_page_track_notifier_node *n)
 {
 	struct kvm_page_track_notifier_head *head;
+	struct kvm *kvm = file_to_kvm(file);
 
 	head = &kvm->arch.track_notifier_head;
 
@@ -279,7 +281,7 @@ void kvm_page_track_unregister_notifier(struct kvm *kvm,
 	write_unlock(&kvm->mmu_lock);
 	synchronize_srcu(&head->track_srcu);
 
-	kvm_put_kvm(kvm);
+	fput(file);
 }
 EXPORT_SYMBOL_GPL(kvm_page_track_unregister_notifier);
 
@@ -336,11 +338,12 @@ void kvm_page_track_delete_slot(struct kvm *kvm, struct kvm_memory_slot *slot)
  * add guest page to the tracking pool so that corresponding access on that
  * page will be intercepted.
  *
- * @kvm: the guest instance we are interested in.
+ * @file: the VM file of the guest instance we are interested in.
  * @gfn: the guest page.
  */
-int kvm_write_track_add_gfn(struct kvm *kvm, gfn_t gfn)
+int kvm_write_track_add_gfn(struct file *file, gfn_t gfn)
 {
+	struct kvm *kvm = file_to_kvm(file);
 	struct kvm_memory_slot *slot;
 	int idx;
 
@@ -366,11 +369,12 @@ EXPORT_SYMBOL_GPL(kvm_write_track_add_gfn);
  * remove the guest page from the tracking pool which stops the interception
  * of corresponding access on that page.
  *
- * @kvm: the guest instance we are interested in.
+ * @file: the VM file of the guest instance we are interested in.
  * @gfn: the guest page.
  */
-int kvm_write_track_remove_gfn(struct kvm *kvm, gfn_t gfn)
+int kvm_write_track_remove_gfn(struct file *file, gfn_t gfn)
 {
+	struct kvm *kvm = file_to_kvm(file);
 	struct kvm_memory_slot *slot;
 	int idx;
 
drivers/s390/crypto/vfio_ap_ops.c +14 -6
index 940c0ff668be..f160e1945fbc 100644
@@ -1822,17 +1822,27 @@ static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
 
 /**
  * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
- * to manage AP resources for the guest whose state is represented by @kvm
+ * to manage AP resources for the guest whose state is represented by
+ * @kvm_file
  *
  * @matrix_mdev: a mediated matrix device
- * @kvm: reference to KVM instance
+ * @kvm_file: the KVM VM file this vfio device is associated with
  *
- * Return: 0 if no other mediated matrix device has a reference to @kvm;
+ * Return: 0 if no other mediated matrix device has a reference to the VM;
  * otherwise, returns an -EPERM.
  */
 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
-				struct kvm *kvm)
+				struct file *kvm_file)
 {
+	struct kvm *kvm;
+
+	if (!kvm_file)
+		return -ENOENT;
+
+	kvm = kvm_file->private_data;
+	if (!kvm)
+		return -ENOENT;
+
 	if (kvm->arch.crypto.crycbd) {
 		get_update_locks_for_kvm(kvm);
 		if (kvm->arch.crypto.pqap_hook) {
@@ -1841,7 +1851,6 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
 		}
 		kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
 
-		kvm_get_kvm(kvm);
 		matrix_mdev->kvm = kvm;
 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
 		release_update_locks_for_kvm(kvm);
@@ -1894,7 +1903,6 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
 		matrix_mdev->kvm = NULL;
 
 		release_update_locks_for_kvm(kvm);
-		kvm_put_kvm(kvm);
 	}
 }
 
drivers/vfio/group.c +10 -1
index b2299e5bc6df..5bf8cbdff377 100644
@@ -860,11 +860,20 @@ bool vfio_group_enforced_coherent(struct vfio_group *group)
 	return ret;
 }
 
-void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
+void vfio_group_set_kvm(struct vfio_group *group, struct file *kvm)
 {
+	struct file *old;
+
+	if (kvm)
+		get_file(kvm);
+
 	spin_lock(&group->kvm_ref_lock);
+	old = group->kvm;
 	group->kvm = kvm;
 	spin_unlock(&group->kvm_ref_lock);
+
+	if (old)
+		fput(old);
 }
 
 /**
drivers/vfio/pci/vfio_pci_zdev.c +7 -1
index f47f36314a1c..3dc302062a67 100644
@@ -184,6 +184,7 @@ int vfio_pci_zdev_feature_err(struct vfio_device *device, u32 flags,
 int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev)
 {
 	struct zpci_dev *zdev = to_zpci(vdev->pdev);
+	struct kvm *kvm;
 	int ret;
 
 	if (!zdev)
@@ -195,8 +196,13 @@ int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev)
 		return 0;
 
 	ret = -ENOENT;
+
+	kvm = vdev->vdev.kvm->private_data;
+	if (!kvm)
+		return ret;
+
 	if (zpci_kvm_hook.kvm_register)
-		ret = zpci_kvm_hook.kvm_register(zdev, vdev->vdev.kvm);
+		ret = zpci_kvm_hook.kvm_register(zdev, kvm);
 
 	if (ret)
 		zpci_stop_mediated_recovery(zdev);
drivers/vfio/vfio.h +5 -5
index 7728bc99b63d..f76504d707e1 100644
@@ -23,7 +23,7 @@ struct vfio_device_file {
 	u8 access_granted;
 	u32 devid; /* only valid when iommufd is valid */
 	spinlock_t kvm_ref_lock; /* protect kvm field */
-	struct kvm *kvm;
+	struct file *kvm;
 	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
 };
 
@@ -88,7 +88,7 @@ struct vfio_group {
 #endif
 	enum vfio_group_type		type;
 	struct mutex			group_lock;
-	struct kvm			*kvm;
+	struct file			*kvm;
 	struct file			*opened_file;
 	struct iommufd_ctx		*iommufd;
 	spinlock_t			kvm_ref_lock;
@@ -107,7 +107,7 @@ void vfio_device_group_unuse_iommu(struct vfio_device *device);
 void vfio_df_group_close(struct vfio_device_file *df);
 struct vfio_group *vfio_group_from_file(struct file *file);
 bool vfio_group_enforced_coherent(struct vfio_group *group);
-void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
+void vfio_group_set_kvm(struct vfio_group *group, struct file *kvm);
 bool vfio_device_has_container(struct vfio_device *device);
 int __init vfio_group_init(void);
 void vfio_group_cleanup(void);
@@ -429,11 +429,11 @@ static inline void vfio_virqfd_exit(void)
 #endif
 
 #if IS_ENABLED(CONFIG_KVM)
-void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm);
+void vfio_device_get_kvm_safe(struct vfio_device *device, struct file *kvm);
 void vfio_device_put_kvm(struct vfio_device *device);
 #else
 static inline void vfio_device_get_kvm_safe(struct vfio_device *device,
-					    struct kvm *kvm)
+					    struct file *kvm)
 {
 }
 
drivers/vfio/vfio_main.c +18 -39

105 lines not on this page · raw message

include/linux/kvm_host.h +8 -0
index 03bfc92864b6..bfecce1b5955 100644
@@ -784,6 +784,13 @@ struct kvm {
 	 * kvm_swap_active_memslots().
 	 */
 	struct mutex slots_arch_lock;
+	/*
+	 * Back-reference to the VM file for subsystems (e.g., VFIO). Holds no
+	 * reference to avoid pinning the VM. Access via get_file_active(&kvm->file)
+	 * only—safe because cleared in kvm_vm_release() before file freed. Callers
+	 * storing the result must take their own reference via get_file()
+	 */
+	struct file *file;
 	struct mm_struct *mm; /* userspace tied to this vm */
 	unsigned long nr_memslot_pages;
 	/* The two memslot sets - active and inactive (per address space) */
@@ -1082,6 +1089,7 @@ void kvm_get_kvm(struct kvm *kvm);
 bool kvm_get_kvm_safe(struct kvm *kvm);
 void kvm_put_kvm(struct kvm *kvm);
 bool file_is_kvm(struct file *file);
+struct kvm *file_to_kvm(struct file *file);
 void kvm_put_kvm_no_destroy(struct kvm *kvm);
 
 static inline struct kvm_memslots *__kvm_memslots(struct kvm *kvm, int as_id)
include/linux/vfio.h +2 -3
index 45f08986359e..0cc91c6f96d2 100644
@@ -54,7 +54,7 @@ struct vfio_device {
 	struct list_head dev_set_list;
 	unsigned int migration_flags;
 	u8 precopy_info_v2;
-	struct kvm *kvm;
+	struct file *kvm;
 
 	/* Members below here are private, not for driver use */
 	unsigned int index;
@@ -66,7 +66,6 @@ struct vfio_device {
 	unsigned int open_count;
 	struct completion comp;
 	struct iommufd_access *iommufd_access;
-	void (*put_kvm)(struct kvm *kvm);
 	struct inode *inode;
 #if IS_ENABLED(CONFIG_IOMMUFD)
 	struct iommufd_device *iommufd_device;
@@ -378,7 +377,7 @@ static inline bool vfio_file_has_dev(struct file *file, struct vfio_device *devi
 #endif
 bool vfio_file_is_valid(struct file *file);
 bool vfio_file_enforced_coherent(struct file *file);
-void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
+void vfio_file_set_kvm(struct file *vfio_file, struct file *kvm);
 
 #define VFIO_PIN_PAGES_MAX_ENTRIES	(PAGE_SIZE/sizeof(unsigned long))
 
virt/kvm/kvm_main.c +17 -3
index 65eb26a0520d..34b4c43908b3 100644
@@ -1314,7 +1314,7 @@ void kvm_get_kvm(struct kvm *kvm)
 {
 	refcount_inc(&kvm->users_count);
 }
-EXPORT_SYMBOL_GPL(kvm_get_kvm);
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_get_kvm);
 
 /*
  * Make sure the vm is not during destruction, which is a safe version of
@@ -1324,14 +1324,14 @@ bool kvm_get_kvm_safe(struct kvm *kvm)
 {
 	return refcount_inc_not_zero(&kvm->users_count);
 }
-EXPORT_SYMBOL_GPL(kvm_get_kvm_safe);
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_get_kvm_safe);
 
 void kvm_put_kvm(struct kvm *kvm)
 {
 	if (refcount_dec_and_test(&kvm->users_count))
 		kvm_destroy_vm(kvm);
 }
-EXPORT_SYMBOL_GPL(kvm_put_kvm);
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_put_kvm);
 
 /*
  * Used to put a reference that was taken on behalf of an object associated
@@ -1352,6 +1352,8 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
 
 	kvm_irqfd_release(kvm);
 
+	WRITE_ONCE(kvm->file, NULL);
+
 	kvm_put_kvm(kvm);
 	return 0;
 }
@@ -5496,6 +5498,15 @@ bool file_is_kvm(struct file *file)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(file_is_kvm);
 
+struct kvm *file_to_kvm(struct file *file)
+{
+	if (!file_is_kvm(file))
+		return NULL;
+
+	return file->private_data;
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(file_to_kvm);
+
 static int kvm_dev_ioctl_create_vm(unsigned long type)
 {
 	char fdname[ITOA_MAX_LEN + 1];
@@ -5527,6 +5538,9 @@ static int kvm_dev_ioctl_create_vm(unsigned long type)
 	 * cases it will be called by the final fput(file) and will take
 	 * care of doing kvm_put_kvm(kvm).
 	 */
+
+	kvm->file = file;
+
 	kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm);
 
 	fd_install(fd, file);
virt/kvm/vfio.c +9 -4
index 6cdc4e9a333a..19548a430942 100644
@@ -35,15 +35,15 @@ struct kvm_vfio {
 	bool noncoherent;
 };
 
-static void kvm_vfio_file_set_kvm(struct file *file, struct kvm *kvm)
+static void kvm_vfio_file_set_kvm(struct file *vfio_file, struct file *kvm)
 {
-	void (*fn)(struct file *file, struct kvm *kvm);
+	void (*fn)(struct file *vfio_file, struct file *kvm);
 
 	fn = symbol_get(vfio_file_set_kvm);
 	if (!fn)
 		return;
 
-	fn(file, kvm);
+	fn(vfio_file, kvm);
 
 	symbol_put(vfio_file_set_kvm);
 }
@@ -144,6 +144,7 @@ static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
 {
 	struct kvm_vfio *kv = dev->private;
 	struct kvm_vfio_file *kvf;
+	struct file *kvm_file __free(fput) = NULL;
 	struct file *filp __free(fput) = NULL;
 
 	filp = fget(fd);
@@ -154,6 +155,10 @@ static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
 	if (!kvm_vfio_file_is_valid(filp))
 		return -EINVAL;
 
+	kvm_file = get_file_active(&dev->kvm->file);
+	if (!kvm_file)
+		return -ENOENT;
+
 	guard(mutex)(&kv->lock);
 
 	list_for_each_entry(kvf, &kv->file_list, node) {
@@ -168,7 +173,7 @@ static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
 	kvf->file = get_file(filp);
 	list_add_tail(&kvf->node, &kv->file_list);
 
-	kvm_vfio_file_set_kvm(kvf->file, dev->kvm);
+	kvm_vfio_file_set_kvm(kvf->file, kvm_file);
 	kvm_vfio_update_coherency(dev);
 
 	return 0;
Signature · 1 line
2.53.0
author reviewer
#32 lore raw

PATCH v7 7/23 KVM: arm64: Access elements of vcpu_gp_regs individually

In reply to Steffen Eiden

While for arm64 the members of vcpu_gp_regs are allocated continuous this is not necessarily true for other architectures implementing ARM.

Let vcpu_gp_regs() no longer return the address of the user_pt_regs in the vcpu context but the address of the gp-register array field in the user_pt_reg struct. Note, this change semantically excludes pc from the GPRs.

No functional change.

---
 arch/arm64/include/asm/kvm_emulate.h       | 14 ++++++++------
 arch/arm64/include/asm/kvm_host.h          |  2 +-
 arch/arm64/kvm/guest.c                     |  2 +-
 arch/arm64/kvm/hyp/exception.c             |  4 ++--
 arch/arm64/kvm/hyp/include/hyp/adjust_pc.h |  4 ++--
 arch/arm64/kvm/hyp/include/hyp/switch.h    |  4 ++--
 6 files changed, 16 insertions(+), 14 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +8 -6
index a3c1928bdf74..8603766c1b4d 100644
@@ -133,12 +133,12 @@ static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr)
 
 static __always_inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu)
 {
-	return (unsigned long *)&vcpu_gp_regs(vcpu)->pc;
+	return (unsigned long *)&vcpu->arch.ctxt.regs.pc;
 }
 
 static __always_inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
 {
-	return (unsigned long *)&vcpu_gp_regs(vcpu)->pstate;
+	return (unsigned long *)&vcpu->arch.ctxt.regs.pstate;
 }
 
 static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
@@ -167,14 +167,14 @@ static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
 static __always_inline unsigned long vcpu_get_reg(const struct kvm_vcpu *vcpu,
 					 u8 reg_num)
 {
-	return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)->regs[reg_num];
+	return (reg_num == 31) ? 0 : vcpu_gp_regs(vcpu)[reg_num];
 }
 
 static __always_inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
 				unsigned long val)
 {
 	if (reg_num != 31)
-		vcpu_gp_regs(vcpu)->regs[reg_num] = val;
+		vcpu_gp_regs(vcpu)[reg_num] = val;
 }
 
 static inline bool vcpu_is_el2_ctxt(const struct kvm_cpu_context *ctxt)
@@ -750,13 +750,15 @@ static inline void kvm_reset_vcpu_core(struct kvm_vcpu *vcpu)
 		pstate = VCPU_RESET_PSTATE_EL1;
 
 	/* Reset core registers */
-	memset(vcpu_gp_regs(vcpu), 0, sizeof(*vcpu_gp_regs(vcpu)));
+	memset(vcpu_gp_regs(vcpu), 0, sizeof(vcpu_gp_regs(vcpu)));
+	*vcpu_pc(vcpu) = 0;
+	vcpu->arch.ctxt.regs.sp = 0;
 	memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs));
 	vcpu->arch.ctxt.spsr_abt = 0;
 	vcpu->arch.ctxt.spsr_und = 0;
 	vcpu->arch.ctxt.spsr_irq = 0;
 	vcpu->arch.ctxt.spsr_fiq = 0;
-	vcpu_gp_regs(vcpu)->pstate = pstate;
+	*vcpu_cpsr(vcpu) = pstate;
 }
 
 /* PSCI reset handling for a vcpu. */
arch/arm64/include/asm/kvm_host.h +1 -1
index 27fe0cd5b2d7..46c8d7928b3f 100644
@@ -1158,7 +1158,7 @@ struct kvm_vcpu_arch {
 #define vcpu_clear_on_unsupported_cpu(vcpu)				\
 	vcpu_clear_flag(vcpu, ON_UNSUPPORTED_CPU)
 
-#define vcpu_gp_regs(v)		(&(v)->arch.ctxt.regs)
+#define vcpu_gp_regs(v)		((v)->arch.ctxt.regs.regs)
 
 /*
  * Only use __vcpu_sys_reg/ctxt_sys_reg if you know you want the
arch/arm64/kvm/guest.c +1 -1
index b01d6622b872..81d850d5a328 100644
@@ -134,7 +134,7 @@ static void *core_reg_addr(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	     KVM_REG_ARM_CORE_REG(regs.regs[30]):
 		off -= KVM_REG_ARM_CORE_REG(regs.regs[0]);
 		off /= 2;
-		return &vcpu->arch.ctxt.regs.regs[off];
+		return &vcpu_gp_regs(vcpu)[off];
 
 	case KVM_REG_ARM_CORE_REG(regs.sp):
 		return &vcpu->arch.ctxt.regs.sp;
arch/arm64/kvm/hyp/exception.c +2 -2
index 754e2dc1df54..0640c9ccaa3c 100644
@@ -276,12 +276,12 @@ static void enter_exception32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset)
 	switch(mode) {
 	case PSR_AA32_MODE_ABT:
 		__vcpu_write_spsr_abt(vcpu, host_spsr_to_spsr32(spsr));
-		vcpu_gp_regs(vcpu)->compat_lr_abt = return_address;
+		vcpu_gp_regs(vcpu)[__compat_lr_abt] = return_address;
 		break;
 
 	case PSR_AA32_MODE_UND:
 		__vcpu_write_spsr_und(vcpu, host_spsr_to_spsr32(spsr));
-		vcpu_gp_regs(vcpu)->compat_lr_und = return_address;
+		vcpu_gp_regs(vcpu)[__compat_lr_und] = return_address;
 		break;
 	}
 
arch/arm64/kvm/hyp/include/hyp/adjust_pc.h +2 -2
index 4fdfeabefeb4..a049983bd5c3 100644
@@ -33,11 +33,11 @@ static inline void kvm_skip_instr(struct kvm_vcpu *vcpu)
 static inline void __kvm_skip_instr(struct kvm_vcpu *vcpu)
 {
 	*vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
-	vcpu_gp_regs(vcpu)->pstate = read_sysreg_el2(SYS_SPSR);
+	*vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
 
 	kvm_skip_instr(vcpu);
 
-	write_sysreg_el2(vcpu_gp_regs(vcpu)->pstate, SYS_SPSR);
+	write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
 	write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
 }
 
arch/arm64/kvm/hyp/include/hyp/switch.h +2 -2
index 1ce7130e2549..a2a054fec2e2 100644
@@ -470,7 +470,7 @@ static inline bool kvm_hyp_handle_mops(struct kvm_vcpu *vcpu, u64 *exit_code)
 	u64 spsr;
 
 	*vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
-	arm64_mops_reset_regs(vcpu_gp_regs(vcpu), vcpu->arch.fault.esr_el2);
+	arm64_mops_reset_regs(&vcpu->arch.ctxt.regs, vcpu->arch.fault.esr_el2);
 	write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
 
 	/*
@@ -902,7 +902,7 @@ static inline void synchronize_vcpu_pstate(struct kvm_vcpu *vcpu)
 	/*
 	 * Check for the conditions of Cortex-A510's #2077057. When these occur
 	 * SPSR_EL2 can't be trusted, but isn't needed either as it is
-	 * unchanged from the value in vcpu_gp_regs(vcpu)->pstate.
+	 * unchanged from the value in vcpu_cpsr(vcpu).
 	 * Are we single-stepping the guest, and took a PAC exception from the
 	 * active-not-pending state?
 	 */
Signature · 1 line
2.53.0
author reviewer
#40 lore raw

PATCH v7 11/23 KVM: arm64: Share arm64 code with s390

In reply to Steffen Eiden

Mark functions that s390 can use to implement arm on s390 as shared functions.

No functional change.

---
 arch/arm64/kvm/arm.c         | 3 +++
 arch/arm64/kvm/guest.c       | 6 ++++++
 arch/arm64/kvm/handle_exit.c | 6 ++++++
 arch/arm64/kvm/mmio.c        | 2 ++
 4 files changed, 17 insertions(+)
arch/arm64/kvm/arm.c +3 -0
index 8b080804bc90..6b92a3c1d490 100644
@@ -1603,6 +1603,7 @@ static unsigned long system_supported_vcpu_features(void)
 	return features;
 }
 
+#ifdef ARM64_S390_COMMON
 static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
 					const struct kvm_vcpu_init *init)
 {
@@ -1656,6 +1657,8 @@ static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
 			     KVM_VCPU_MAX_FEATURES);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static int kvm_setup_vcpu(struct kvm_vcpu *vcpu)
 {
 	struct kvm *kvm = vcpu->kvm;
arch/arm64/kvm/guest.c +6 -0
index 773f6c8e5026..6ca5a9f357cd 100644
@@ -62,6 +62,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {
 		       sizeof(kvm_vcpu_stats_desc),
 };
 
+#ifdef ARM64_S390_COMMON
 static bool core_reg_offset_is_vreg(u64 off)
 {
 	return off >= KVM_REG_ARM_CORE_REG(fp_regs.vregs) &&
@@ -306,6 +307,8 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	return err;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 #define vq_word(vq) (((vq) - SVE_VQ_MIN) / 64)
 #define vq_mask(vq) ((u64)1 << ((vq) - SVE_VQ_MIN) % 64)
 #define vq_present(vqs, vq) (!!((vqs)[vq_word(vq)] & vq_mask(vq)))
@@ -543,6 +546,7 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
 	return -EINVAL;
 }
 
+#ifdef ARM64_S390_COMMON
 static int copy_core_reg_indices(const struct kvm_vcpu *vcpu,
 				 u64 __user *uindices)
 {
@@ -591,6 +595,8 @@ static unsigned long num_core_regs(const struct kvm_vcpu *vcpu)
 	return copy_core_reg_indices(vcpu, NULL);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static unsigned long num_sve_regs(const struct kvm_vcpu *vcpu)
 {
 	const unsigned int slices = vcpu_sve_slices(vcpu);
arch/arm64/kvm/handle_exit.c +6 -0
index db37678dcb05..6e59a7b12d40 100644
@@ -213,6 +213,7 @@ static int kvm_handle_guest_debug(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
+#ifdef ARM64_S390_COMMON
 static int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu)
 {
 	u64 esr = kvm_vcpu_get_esr(vcpu);
@@ -224,6 +225,8 @@ static int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * Guest access to SVE registers should be routed to this handler only
  * when the system doesn't support SVE.
@@ -404,6 +407,7 @@ static exit_handle_fn arm_exit_handlers[] = {
 	[ESR_ELx_EC_GCS]	= kvm_handle_gcs,
 };
 
+#ifdef ARM64_S390_COMMON
 static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu)
 {
 	u64 esr = kvm_vcpu_get_esr(vcpu);
@@ -439,6 +443,8 @@ static int handle_trap_exceptions(struct kvm_vcpu *vcpu)
 	return handled;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
  * proper exit to userspace.
arch/arm64/kvm/mmio.c +2 -0
index d1c3a352d5a2..5161102da507 100644
@@ -10,6 +10,7 @@
 
 #include "trace.h"
 
+#ifdef ARM64_S390_COMMON
 void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data)
 {
 	void *datap = NULL;
@@ -258,3 +259,4 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
 	run->exit_reason	= KVM_EXIT_MMIO;
 	return 0;
 }
+#endif /* ARM64_S390_COMMON */
Signature · 1 line
2.53.0
author reviewer
#28 lore raw

PATCH v7 5/23 arm64: ptrace: Use constants for compat register numbers

In reply to Steffen Eiden

Define an enum to determine the register number for the mapping from AArch32 and AArch64 registers. This allows for a more flexible use of the register mappings.

---
 arch/arm64/include/asm/ptrace.h | 60 ++++++++++++++++++++++-----------
 1 file changed, 41 insertions(+), 19 deletions(-)
arch/arm64/include/asm/ptrace.h +41 -19
index f7dc5fb9427d..49a83a0636b9 100644
@@ -102,26 +102,48 @@
 #define COMPAT_USER_SZ	296
 
 /* Architecturally defined mapping between AArch32 and AArch64 registers */
+enum {
+	__compat_fp		= 11,
+	__compat_sp		= 13,
+	__compat_lr		= 14,
+	__compat_sp_hyp		= 15,
+	__compat_lr_irq		= 16,
+	__compat_sp_irq		= 17,
+	__compat_lr_svc		= 18,
+	__compat_sp_svc		= 19,
+	__compat_lr_abt		= 20,
+	__compat_sp_abt		= 21,
+	__compat_lr_und		= 22,
+	__compat_sp_und		= 23,
+	__compat_r8_fiq		= 24,
+	__compat_r9_fiq		= 25,
+	__compat_r10_fiq	= 26,
+	__compat_r11_fiq	= 27,
+	__compat_r12_fiq	= 28,
+	__compat_sp_fiq		= 29,
+	__compat_lr_fiq		= 30,
+};
+
 #define compat_usr(x)	regs[(x)]
-#define compat_fp	regs[11]
-#define compat_sp	regs[13]
-#define compat_lr	regs[14]
-#define compat_sp_hyp	regs[15]
-#define compat_lr_irq	regs[16]
-#define compat_sp_irq	regs[17]
-#define compat_lr_svc	regs[18]
-#define compat_sp_svc	regs[19]
-#define compat_lr_abt	regs[20]
-#define compat_sp_abt	regs[21]
-#define compat_lr_und	regs[22]
-#define compat_sp_und	regs[23]
-#define compat_r8_fiq	regs[24]
-#define compat_r9_fiq	regs[25]
-#define compat_r10_fiq	regs[26]
-#define compat_r11_fiq	regs[27]
-#define compat_r12_fiq	regs[28]
-#define compat_sp_fiq	regs[29]
-#define compat_lr_fiq	regs[30]
+#define compat_fp	regs[__compat_fp]
+#define compat_sp	regs[__compat_sp]
+#define compat_lr	regs[__compat_lr]
+#define compat_sp_hyp	regs[__compat_sp_hyp]
+#define compat_lr_irq	regs[__compat_lr_irq]
+#define compat_sp_irq	regs[__compat_sp_irq]
+#define compat_lr_svc	regs[__compat_lr_svc]
+#define compat_sp_svc	regs[__compat_sp_svc]
+#define compat_lr_abt	regs[__compat_lr_abt]
+#define compat_sp_abt	regs[__compat_sp_abt]
+#define compat_lr_und	regs[__compat_lr_und]
+#define compat_sp_und	regs[__compat_sp_und]
+#define compat_r8_fiq	regs[__compat_r8_fiq]
+#define compat_r9_fiq	regs[__compat_r9_fiq]
+#define compat_r10_fiq	regs[__compat_r10_fiq]
+#define compat_r11_fiq	regs[__compat_r11_fiq]
+#define compat_r12_fiq	regs[__compat_r12_fiq]
+#define compat_sp_fiq	regs[__compat_sp_fiq]
+#define compat_lr_fiq	regs[__compat_lr_fiq]
 
 static inline unsigned long compat_psr_to_pstate(const unsigned long psr)
 {
Signature · 1 line
2.53.0
author reviewer
#52 lore raw

PATCH v7 14/23 KVM: s390: Prepare KVM/s390 for a second KVM module

In reply to Steffen Eiden

The second KVM module will have a different Kconfig set. When both modules are compiled the Kconfig sets get merged and the native s390 KVM needs to implement functionality required by the respective config options. Ensure that s390-KVM will still compile by implementing a stub for kvm_arch_vcpu_run_pid_change if CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE is enabled and defining KVM_NO_MMIO to disable KVM_MMIO.

---
 arch/s390/include/asm/kvm_host_s390.h | 1 +
 arch/s390/kvm/s390/s390.c             | 8 ++++++++
 2 files changed, 9 insertions(+)
arch/s390/include/asm/kvm_host_s390.h +1 -0
index cd692f8fb764..207f44cb86d9 100644
@@ -29,6 +29,7 @@
 
 #define KVM_HAVE_MMU_RWLOCK
 #define KVM_MAX_VCPUS 255
+#define KVM_NO_MMIO
 
 #define KVM_INTERNAL_MEM_SLOTS 1
 
arch/s390/kvm/s390/s390.c +8 -0
index b0839e887221..697be391f0aa 100644
@@ -5873,6 +5873,14 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 	return gmap_unmap_gfn_range(kvm->arch.gmap, range->slot, range->start, range->end);
 }
 
+#ifdef CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE
+/* Make s390 compile if arm64-on-s390 is selected */
+int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+#endif /* CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE */
+
 static inline unsigned long nonhyp_mask(int i)
 {
 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
Signature · 1 line
2.53.0
author reviewer
#56 lore raw

PATCH v7 16/23 KVM: s390: arm64: Introduce host definitions

In reply to Steffen Eiden

Add all basic definitions the arm on s390 KVM host requires. Including, but not limited to, struct kvm*arch definitions, various functions (to be implemented in the following patches), and various defines required to run arm64 guests.

Enlighten asm/kvm_host.h for the new header variant. The new header is chosen instead of asm/kvm_host_s390.h if KVM_S390_ARM64 is defined.

---
 arch/s390/include/asm/kvm.h            |   6 +
 arch/s390/include/asm/kvm_host.h       |   9 ++
 arch/s390/include/asm/kvm_host_arm64.h | 211 +++++++++++++++++++++++++
 3 files changed, 226 insertions(+)
 create mode 100644 arch/s390/include/asm/kvm.h
 create mode 100644 arch/s390/include/asm/kvm_host_arm64.h
arch/s390/include/asm/kvm.h new file +6 -0
new file mode 100644
index 000000000000..aeb11dc631d5
@@ -0,0 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifdef KVM_S390_ARM64
+#include <uapi/arm64/kvm.h>
+#else
+#include <uapi/asm/kvm.h>
+#endif
arch/s390/include/asm/kvm_host.h +9 -0
index 147325978225..cd6d572883ab 100644
@@ -3,7 +3,16 @@
 #ifndef ASM_KVM_HOST_H
 #define ASM_KVM_HOST_H
 
+#ifdef KVM_S390_ARM64
+#include <asm/kvm_host_arm64.h>
+#else
 #include <asm/kvm_host_s390.h>
+#endif
+
+static inline  bool kvm_arch_pmi_in_guest(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
 
 #define PGM_OPERATION			0x01
 #define PGM_PRIVILEGED_OP		0x02
arch/s390/include/asm/kvm_host_arm64.h new file +211 -0

211 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#58 lore raw

PATCH v7 17/23 s390/hwcaps: Report SAE support as hwcap

In reply to Steffen Eiden

From: Hendrik Brueckner <[email protected]>

Report SAE support as hwcap (and /proc/cpuinfo)

---
 arch/s390/include/asm/elf.h    | 2 ++
 arch/s390/include/asm/sclp.h   | 5 ++++-
 arch/s390/kernel/processor.c   | 3 +++
 drivers/s390/char/sclp_early.c | 1 +
 4 files changed, 10 insertions(+), 1 deletion(-)
arch/s390/include/asm/elf.h +2 -0
index bb63fa4d20bb..ad3108ecfb07 100644
@@ -123,6 +123,7 @@ enum {
 	HWCAP_NR_NNPA		= 20,
 	HWCAP_NR_PCI_MIO	= 21,
 	HWCAP_NR_SIE		= 22,
+	HWCAP_NR_SAE		= 23,
 	HWCAP_NR_MAX
 };
 
@@ -150,6 +151,7 @@ enum {
 #define HWCAP_NNPA		BIT(HWCAP_NR_NNPA)
 #define HWCAP_PCI_MIO		BIT(HWCAP_NR_PCI_MIO)
 #define HWCAP_SIE		BIT(HWCAP_NR_SIE)
+#define HWCAP_SAE		BIT(HWCAP_NR_SAE)
 
 /*
  * These are used to set parameters in the core dumps.
arch/s390/include/asm/sclp.h +4 -1
index 44066550b9b1..68f61b71a2d5 100644
@@ -56,7 +56,9 @@ struct sclp_core_entry {
 	u8 siif : 1;
 	u8 sigpif : 1;
 	u8 : 3;
-	u8 reserved2[3];
+	u8 aef: 1;
+	u8 : 7;
+	u8 reserved2[2];
 	u8 : 2;
 	u8 ib : 1;
 	u8 cei : 1;
@@ -109,6 +111,7 @@ struct sclp_info {
 	unsigned char has_aeni : 1;
 	unsigned char has_aisi : 1;
 	unsigned char has_astfleie2 : 1;
+	unsigned char has_aef : 1;
 	unsigned int ibc;
 	unsigned int mtid;
 	unsigned int mtid_cp;
arch/s390/kernel/processor.c +3 -0
index e33a3eccda56..6da55a158027 100644
@@ -150,6 +150,7 @@ static void show_cpu_summary(struct seq_file *m, void *v)
 		[HWCAP_NR_NNPA]		= "nnpa",
 		[HWCAP_NR_PCI_MIO]	= "pcimio",
 		[HWCAP_NR_SIE]		= "sie",
+		[HWCAP_NR_SAE]		= "sae",
 	};
 	int i, cpu;
 
@@ -254,6 +255,8 @@ static int __init setup_hwcaps(void)
 	/* virtualization support */
 	if (sclp.has_sief2)
 		elf_hwcap |= HWCAP_SIE;
+	if (sclp.has_aef)
+		elf_hwcap |= HWCAP_SAE;
 
 	return 0;
 }
drivers/s390/char/sclp_early.c +1 -0
index 22dd797e6229..e58d24b95bd1 100644
@@ -96,6 +96,7 @@ static void __init sclp_early_facilities_detect(void)
 		sclp.has_ib = cpue->ib;
 		sclp.has_cei = cpue->cei;
 		sclp.has_skey = cpue->skey;
+		sclp.has_aef = cpue->aef;
 		break;
 	}
 
Signature · 1 line
2.53.0
author reviewer
#48 lore raw

PATCH v7 12/23 s390/tools: Use arm64 headers

In reply to Steffen Eiden

Introduce a flexible system for sharing ARM64 headers with s390 KVM. Selected arm64 headers or snippet of headers will be reachable by s390 code through asm/header.h.

Add copy-arm64h.awk that detects ARM64_S390_COMMON markers and extracts marked section into a generated header consumable by s390. Falls back to full file copy when no markers are found.

Transform copied includes by replacing '#include <asm/...>' with '#include <arm64/...>' and similar for uapi headers to ensure correct path resolution on s390 and allow for a clear separation of s390 asm headers and arm64 asm headers.

Integrate this extraction into the s390/kapi target, conditional on (the not yet existing) CONFIG_KVM_ARM64.

---
 arch/s390/tools/Makefile             |  9 +++-
 arch/s390/tools/Makefile.arm64h      | 56 ++++++++++++++++++++++
 arch/s390/tools/copy-arm64h-full.awk | 47 ++++++++++++++++++
 arch/s390/tools/copy-arm64h.awk      | 72 ++++++++++++++++++++++++++++
 4 files changed, 183 insertions(+), 1 deletion(-)
 create mode 100644 arch/s390/tools/Makefile.arm64h
 create mode 100644 arch/s390/tools/copy-arm64h-full.awk
 create mode 100644 arch/s390/tools/copy-arm64h.awk
arch/s390/tools/Makefile +8 -1
index f2862364fb42..b19e8bbfbf5f 100644
@@ -3,12 +3,19 @@
 # Makefile for s390 specific build tools
 #
 
+include $(srctree)/arch/s390/tools/Makefile.arm64h
+
 kapi := arch/$(ARCH)/include/generated/asm
 kapi-hdrs-y := $(kapi)/facility-defs.h $(kapi)/dis-defs.h
 
+targets += $(addprefix ../../../,$(kapi-hdrs-y))
+targets += $(patsubst $(objtree)/%,../../../%,$(ARM64_TARGETS))
+
+kapi-hdrs-$(CONFIG_KVM_S390_ARM64) += $(ARM64_TARGETS)
+
 PHONY += kapi
 
-kapi:	$(kapi-hdrs-y)
+kapi:	$(kapi-hdrs-y) $(kapi-hdrs-m)
 
 hostprogs		    += gen_facilities
 hostprogs		    += gen_opcode_table
arch/s390/tools/Makefile.arm64h new file +56 -0

56 lines not on this page · raw message

arch/s390/tools/copy-arm64h-full.awk new file +47 -0

47 lines not on this page · raw message

arch/s390/tools/copy-arm64h.awk new file +72 -0

72 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#50 lore raw

PATCH v7 13/23 KVM: s390: Use arm64 code

In reply to Steffen Eiden

Add the infrastructure to extract KVM/arm64 code into s390 at built time similar to the arm64 header sharing. Add copy-arm64c.awk hat detects ARM64_S390_COMMON markers and extracts marked section into an .inc file that can be consumed by arm on s390 host code. If no marker is found make will fail.

To ensure that no code is consumed twice by accident a guard is added during file generation. A s390 C file wanting to consume such an inc file must first define __INCL_GEN_ARM_FILE otherwise the inc file will emit a compile error.

Example:

	#define __INCL_GEN_ARM_FILE
	#include <generated/arm.inc>
	#undef __INCL_GEN_ARM_FILE
---
 arch/s390/kvm/arm64/.gitignore      |  2 +
 arch/s390/kvm/arm64/Makefile.gen    | 27 +++++++++++
 arch/s390/kvm/arm64/copy-arm64c.awk | 72 +++++++++++++++++++++++++++++
 3 files changed, 101 insertions(+)
 create mode 100644 arch/s390/kvm/arm64/.gitignore
 create mode 100644 arch/s390/kvm/arm64/Makefile.gen
 create mode 100644 arch/s390/kvm/arm64/copy-arm64c.awk
arch/s390/kvm/arm64/.gitignore new file +2 -0
new file mode 100644
index 000000000000..bd9d90694676
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+generated/*
arch/s390/kvm/arm64/Makefile.gen new file +27 -0
new file mode 100644
index 000000000000..6ac7602b57cb
@@ -0,0 +1,27 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Extracts ARM64 marked sections to .inc files that are included by main source
+# files
+
+# List of ARM64 C files to extract
+ARM64_CFILES := \
+	arm.c \
+	guest.c \
+	handle_exit.c \
+	mmio.c \
+
+quiet_cmd_extract_inc = GEN     $@
+      cmd_extract_inc = mkdir -p $(dir $@); \
+                        $(AWK) -f $(src)/copy-arm64c.awk $< > $@ || \
+                        { echo "Error: No or unbalanced ARM64_S390_COMMON markers in $<" >&2; rm -f $@; exit 1; }
+
+$(obj)/generated/%.inc: $(srctree)/arch/arm64/kvm/%.c $(src)/copy-arm64c.awk FORCE
+	$(call if_changed,extract_inc)
+
+ARM64_INC_FILES := $(foreach cfile,$(ARM64_CFILES),generated/$(basename $(cfile)).inc)
+
+targets += $(ARM64_INC_FILES)
+clean-files += generated/*.inc
+
+$(foreach cfile,$(basename $(ARM64_CFILES)),\
+	$(eval $(obj)/$(cfile).o: $(obj)/generated/$(cfile).inc))
arch/s390/kvm/arm64/copy-arm64c.awk new file +72 -0

72 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#60 lore raw

PATCH v7 18/23 KVM: s390: Add basic arm64 kvm module

In reply to Steffen Eiden

Add basic code for the new arm64 on s390 KVM implementation. Add kernel module boilerplate code and trivial functions. Add tracing scaffolding.

---
 arch/s390/kvm/arm64/arm.c   | 155 ++++++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/arm.h   |   7 ++
 arch/s390/kvm/arm64/guest.c |  92 +++++++++++++++++++++
 arch/s390/kvm/arm64/trace.h |  18 +++++
 4 files changed, 272 insertions(+)
 create mode 100644 arch/s390/kvm/arm64/arm.c
 create mode 100644 arch/s390/kvm/arm64/arm.h
 create mode 100644 arch/s390/kvm/arm64/guest.c
 create mode 100644 arch/s390/kvm/arm64/trace.h
arch/s390/kvm/arm64/arm.c new file +155 -0
new file mode 100644
index 000000000000..299b070f8b6d
@@ -0,0 +1,155 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define KMSG_COMPONENT "kvm-s390-arm64"
+#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
+
+#include <linux/miscdevice.h>
+#include <linux/kvm.h>
+#include <linux/kvm_types.h>
+#include <linux/kvm_host.h>
+
+#include "arm.h"
+
+#define CREATE_TRACE_POINTS
+#include "trace.h"
+
+static unsigned long system_supported_vcpu_features(void);
+
+#define __INCL_GEN_ARM_FILE
+#include "generated/arm.inc"
+#undef __INCL_GEN_ARM_FILE
+
+int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
+{
+	int ret;
+
+	switch (ext) {
+	case KVM_CAP_NR_VCPUS:
+	case KVM_CAP_MAX_VCPUS:
+	case KVM_CAP_MAX_VCPU_ID:
+		ret = KVM_MAX_VCPUS;
+		break;
+	case KVM_CAP_ARM_VM_IPA_SIZE:
+		ret = get_kvm_ipa_limit();
+		break;
+	case KVM_CAP_IOEVENTFD:
+		ret = 1;
+		break;
+	default:
+		ret = 0;
+	}
+
+	return ret;
+}
+
+static u64 kvm_max_guest_address(void)
+{
+	u64 max_addr;
+
+	max_addr = min_t(u64, TASK_SIZE_MAX, sclp.hamax);
+	max_addr = max_t(u64, max_addr, SZ_1G - 1);
+	return ALIGN_DOWN(max_addr + 1, SZ_1G) - 1;
+}
+
+vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
+{
+	return VM_FAULT_SIGBUS;
+}
+
+long kvm_arch_dev_ioctl(struct file *filp,
+			unsigned int ioctl, unsigned long arg)
+{
+	return -EINVAL;
+}
+
+u32 get_kvm_ipa_limit(void)
+{
+	return fls64(kvm_max_guest_address() + 1) - 1;
+}
+
+int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
+{
+	return 0;
+}
+
+void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
+{
+}
+
+int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
+				    struct kvm_mp_state *mp_state)
+{
+	*mp_state = READ_ONCE(vcpu->arch.mp_state);
+	return 0;
+}
+
+int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
+				    struct kvm_mp_state *mp_state)
+{
+	return -EINVAL;
+}
+
+int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+
+static unsigned long system_supported_vcpu_features(void)
+{
+	unsigned long features = KVM_S390_ARM64_IMPL_FEATURES;
+
+	return features;
+}
+
+bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
+{
+	return false;
+}
+
+int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
+		int irq_source_id, int level, bool line_status)
+{
+	return -EINVAL;
+}
+
+int kvm_set_routing_entry(struct kvm *kvm,
+			  struct kvm_kernel_irq_routing_entry *e,
+			  const struct kvm_irq_routing_entry *ue)
+{
+	return -EINVAL;
+}
+
+int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+
+#ifdef CONFIG_HAVE_KVM_NO_POLL
+__weak bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+#endif
+
+long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
+				  unsigned long arg)
+{
+	return -ENOIOCTLCMD;
+}
+
+static int __init kvm_s390_arm64_init(void)
+{
+	if (!sclp.has_aef)
+		return -ENXIO;
+
+	return kvm_init_with_dev(sizeof(struct kvm_vcpu), 0, THIS_MODULE,
+				 KVM_DEV_NAME, MISC_DYNAMIC_MINOR);
+}
+
+static __exit void kvm_s390_arm64_exit(void)
+{
+	kvm_exit();
+}
+
+module_init(kvm_s390_arm64_init);
+module_exit(kvm_s390_arm64_exit);
arch/s390/kvm/arm64/arm.h new file +7 -0
new file mode 100644
index 000000000000..a3db254462c0
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef ARCH_S390_KVM_ARM64_H
+#define ARCH_S390_KVM_ARM64_H
+
+#define KVM_DEV_NAME "kvm-arm64"
+
+#endif /* ARCH_S390_KVM_ARM64_H */
arch/s390/kvm/arm64/guest.c new file +92 -0

92 lines not on this page · raw message

arch/s390/kvm/arm64/trace.h new file +18 -0
new file mode 100644
index 000000000000..3a5dcd039c13
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#if !defined(KVM_ARM64_TRACE_KVM_H) || defined(TRACE_HEADER_MULTI_READ)
+#define KVM_ARM64_TRACE_KVM_H
+
+#include <linux/tracepoint.h>
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM kvm
+
+#endif /* KVM_ARM64_TRACE_KVM_H */
+
+#undef TRACE_INCLUDE_PATH
+#define TRACE_INCLUDE_PATH .
+#undef TRACE_INCLUDE_FILE
+#define TRACE_INCLUDE_FILE trace
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
Signature · 1 line
2.53.0
author reviewer
#54 lore raw

PATCH v7 15/23 s390: Introduce Start Arm Execution instruction

In reply to Steffen Eiden

The Start Arm Execution (SAE) instruction is the centerpiece for executing arm64 (KVM) guests on s390. Its purpose is, similar to SIE, to enable accelerated execution of arm64 virtual machines. SAE expects the physical address of a control block as the only argument.

The host is responsible to save & restore
- GPRs 0-13
- access register 0-15
- breaking event register (BEAR)
- vector/floating point registers
between SAE executions to guarantee host consistency.

GPRs and BEAR are save and restores in the asm functions. The other register are handled in within C code. Access registers are handled in a later patch and SVEs will be handled when they are introduced in a future series. Most arm64 registers are handled by a satellite block called save_area. Some registers, frequently used by hypervisors, are placed into the SAE control block itself.

Enlighten asm/kvm_host_types.h for the new header variant. The new header is chosen instead of asm/kvm_host_s390_types.h if KVM_S390_ARM64 is defined.

---
 arch/s390/include/asm/asm-prototypes.h       |   1 +
 arch/s390/include/asm/kvm_host_arm64_types.h | 118 +++++++++++++++++++
 arch/s390/include/asm/kvm_host_types.h       |   4 +
 arch/s390/include/asm/sae.h                  |  57 +++++++++
 arch/s390/include/asm/stacktrace.h           |   5 +
 arch/s390/kernel/asm-offsets.c               |   1 +
 arch/s390/kernel/entry.S                     |  22 ++++
 arch/s390/tools/opcodes.txt                  |   3 +
 8 files changed, 211 insertions(+)
 create mode 100644 arch/s390/include/asm/kvm_host_arm64_types.h
 create mode 100644 arch/s390/include/asm/sae.h
arch/s390/include/asm/asm-prototypes.h +1 -0
index d4da4436d02b..faa2afb259a1 100644
@@ -6,6 +6,7 @@
 #include <asm/bug.h>
 #include <asm/fpu.h>
 #include <asm/nospec-branch.h>
+#include <asm/sae.h>
 #include <asm-generic/asm-prototypes.h>
 
 #endif /* _ASM_S390_PROTOTYPES_H */
arch/s390/include/asm/kvm_host_arm64_types.h new file +118 -0

118 lines not on this page · raw message

arch/s390/include/asm/kvm_host_types.h +4 -0
index e5bdba07cab0..d308739ebcf5 100644
@@ -3,6 +3,10 @@
 #ifndef ASM_KVM_HOST_TYPES_H
 #define ASM_KVM_HOST_TYPES_H
 
+#ifdef KVM_S390_ARM64
+#include <asm/kvm_host_arm64_types.h>
+#else
 #include <asm/kvm_host_s390_types.h>
+#endif /* KVM_S390_ARM64 */
 
 #endif /* ASM_KVM_HOST_TYPES_H */
arch/s390/include/asm/sae.h new file +57 -0

57 lines not on this page · raw message

arch/s390/include/asm/stacktrace.h +5 -0
index ac3606c3babe..2d332d7c8145 100644
@@ -59,6 +59,7 @@ static inline bool on_stack(struct stack_info *info,
 struct stack_frame {
 	union {
 		unsigned long empty[9];
+		/* SIE stack frame */
 		struct {
 			unsigned long sie_control_block;
 			unsigned long sie_savearea;
@@ -68,6 +69,10 @@ struct stack_frame {
 			unsigned long sie_guest_asce;
 			unsigned long sie_irq;
 		};
+		/* SAE stack frame */
+		struct {
+			unsigned long sae_bear;
+		};
 	};
 	unsigned long gprs[10];
 	unsigned long back_chain;
arch/s390/kernel/asm-offsets.c +1 -0
index f6dd2b67dcee..f9f9c2246074 100644
@@ -70,6 +70,7 @@ int main(void)
 	OFFSET(__SF_SIE_CONTROL_PHYS, stack_frame, sie_control_block_phys);
 	OFFSET(__SF_SIE_GUEST_ASCE, stack_frame, sie_guest_asce);
 	OFFSET(__SF_SIE_IRQ, stack_frame, sie_irq);
+	OFFSET(__SF_SAE_BEAR, stack_frame, sae_bear);
 	DEFINE(STACK_FRAME_OVERHEAD, sizeof(struct stack_frame));
 	BLANK();
 	OFFSET(__SFUSER_BACKCHAIN, stack_frame_user, back_chain);
arch/s390/kernel/entry.S +22 -0
index 10dd9bbdf985..6f6f8f6ff9c4 100644
@@ -245,6 +245,28 @@ EXPORT_SYMBOL(__sie64a)
 EXPORT_SYMBOL(sie_exit)
 #endif
 
+#if IS_ENABLED(CONFIG_KVM_S390_ARM64)
+/*
+ * __sae64a calling convention:
+ * %r2 pointer to sae control block physical address
+ */
+SYM_FUNC_START(__sae64a)
+	stmg	%r6,%r14,__SF_GPRS(%r15)	# store kernel registers
+	STBEAR	__SF_SAE_BEAR(%r15)		# save breaking event address register
+	.insn	rre,0xb9a50000,%r2,0		# Start Arm Execution
+	LBEAR	__SF_SAE_BEAR(%r15)		# restore breaking event address register
+	lmg	%r6,%r14,__SF_GPRS(%r15)	# restore kernel registers
+	xgr	%r0,%r0				# clear guest registers to
+	xgr	%r1,%r1				# prevent speculative use
+	xgr	%r2,%r2
+	xgr	%r3,%r3
+	xgr	%r4,%r4
+	xgr	%r5,%r5
+	BR_EX	%r14
+SYM_FUNC_END(__sae64a)
+EXPORT_SYMBOL(__sae64a)
+#endif
+
 /*
  * SVC interrupt handler routine. System calls are synchronous events and
  * are entered with interrupts disabled.
arch/s390/tools/opcodes.txt +3 -0
index def2659f6602..0e4773c94af0 100644
@@ -594,6 +594,9 @@ b9a0	clp	RRF_U0RR
 b9a1	tpei	RRE_RR
 b9a2	ptf	RRE_R0
 b9a4	uvc	RRF_URR
+b9a5	sae	RRE_R0
+b9a6	lasrm	RRE_R0
+b9a7	stiasrm	RRE_R0
 b9aa	lptea	RRF_RURR2
 b9ab	essa	RRF_U0RR
 b9ac	irbm	RRE_RR
Signature · 1 line
2.53.0
author reviewer
#62 lore raw

PATCH v7 19/23 KVM: s390: arm64: Implement required functions

In reply to Steffen Eiden

Implement the mostly trivial functions that the shared arm64 (kvm) code & headers oblige s390 to implement.

MAINTAINERS: Add arch/s390/include/arm64/* to KVM/s390

Implement a very basic smccc handler that (non-compliantly) is just able to stop a vcpu.

---
 MAINTAINERS                           |   1 +
 arch/s390/include/arm64/kvm_emulate.h | 130 ++++++++++++++++++++++++++
 arch/s390/include/arm64/kvm_nested.h  |  11 +++
 arch/s390/include/arm64/ptrace.h      |  11 +++
 arch/s390/include/arm64/sysreg.h      |  13 +++
 arch/s390/kvm/arm64/handle_exit.c     |  98 +++++++++++++++++++
 arch/s390/kvm/arm64/inject_fault.c    |  21 +++++
 7 files changed, 285 insertions(+)
 create mode 100644 arch/s390/include/arm64/kvm_emulate.h
 create mode 100644 arch/s390/include/arm64/kvm_nested.h
 create mode 100644 arch/s390/include/arm64/ptrace.h
 create mode 100644 arch/s390/include/arm64/sysreg.h
 create mode 100644 arch/s390/kvm/arm64/handle_exit.c
 create mode 100644 arch/s390/kvm/arm64/inject_fault.c
MAINTAINERS +1 -0
index 3a19da74d00c..8399f48ffcf7 100644
@@ -14379,6 +14379,7 @@ L:	[email protected]
 S:	Supported
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/kvms390/linux.git
 F:	Documentation/virt/kvm/s390*
+F:	arch/s390/include/arm64/*
 F:	arch/s390/include/asm/gmap_helpers.h
 F:	arch/s390/include/asm/kvm*
 F:	arch/s390/include/uapi/asm/kvm*
arch/s390/include/arm64/kvm_emulate.h new file +130 -0

130 lines not on this page · raw message

arch/s390/include/arm64/kvm_nested.h new file +11 -0
new file mode 100644
index 000000000000..e950b1a10c41
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef ASM_KVM_NESTED_H
+#define ASM_KVM_NESTED_H
+
+static inline bool vcpu_has_nv(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+#endif /* ASM_KVM_NESTED_H */
arch/s390/include/arm64/ptrace.h new file +11 -0
new file mode 100644
index 000000000000..449b85582c97
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __S390_ARM64_PTRACE_H__
+#define __S390_ARM64_PTRACE_H__
+
+#include <arm64/sysreg.h>
+#include <uapi/arm64/ptrace.h>
+
+#include <arm64/ptrace-gen.h>
+
+#endif /* __S390_ARM64_PTRACE_H__ */
arch/s390/include/arm64/sysreg.h new file +13 -0
new file mode 100644
index 000000000000..ac6b638fef56
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __S390_ARM64_SYSREG_H__
+#define __S390_ARM64_SYSREG_H__
+
+#include <linux/bits.h>
+#include <linux/stringify.h>
+#include <linux/kasan-tags.h>
+#include <linux/kconfig.h>
+
+#include <arm64/sysreg-gen.h>
+
+#endif /* __S390_ARM64_SYSREG_H__ */
arch/s390/kvm/arm64/handle_exit.c new file +98 -0

98 lines not on this page · raw message

arch/s390/kvm/arm64/inject_fault.c new file +21 -0
new file mode 100644
index 000000000000..425dbeaa421c
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <arm64/kvm_emulate.h>
+
+/**
+ * kvm_inject_undefined - inject an undefined instruction into the guest
+ * @vcpu: The vCPU in which to inject the exception
+ *
+ * It is assumed that this code is called from the VCPU thread and that the
+ * VCPU therefore is not currently executing guest code.
+ */
+void kvm_inject_undefined(struct kvm_vcpu *vcpu)
+{
+	/* Stub until s390 supports arm64 sysregs TODO sysregs*/
+}
+
+int kvm_inject_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr)
+{
+	/* Stub until s390 supports arm64 sysregs TODO sysregs*/
+	return 1;
+}
Signature · 1 line
2.53.0
author reviewer
#66 lore raw

PATCH v7 21/23 KVM: s390: arm64: Implement vCPU IOCTLs

In reply to Steffen Eiden

Implement all required vCPU (arch) IOCTLs.

---
 arch/s390/kvm/arm64/arm.c         | 406 ++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/guest.c       |  47 +++-
 arch/s390/kvm/arm64/handle_exit.c |  31 +++
 arch/s390/kvm/arm64/handle_exit.h |   9 +
 arch/s390/kvm/arm64/reset.c       |  71 ++++++
 arch/s390/kvm/arm64/trace.h       |   2 +-
 6 files changed, 563 insertions(+), 3 deletions(-)
 create mode 100644 arch/s390/kvm/arm64/handle_exit.h
 create mode 100644 arch/s390/kvm/arm64/reset.c
arch/s390/kvm/arm64/arm.c +406 -0
index 7df0e4e697e8..3caa72f780a5 100644
@@ -7,11 +7,19 @@
 #include <linux/kvm.h>
 #include <linux/kvm_types.h>
 #include <linux/kvm_host.h>
+#include <linux/cleanup.h>
+#include <linux/fpu.h>
+
+#include <asm/access-regs.h>
+
+#include <arm64/kvm_emulate.h>
+#include <arm64/sysreg.h>
 
 #include <gmap.h>
 #include <kvm_mmu.h>
 
 #include "arm.h"
+#include "handle_exit.h"
 
 #define CREATE_TRACE_POINTS
 #include "trace.h"
@@ -198,6 +206,22 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
 }
 
+void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
+{
+	save_access_regs(&vcpu->arch.host_acrs[0]);
+	vcpu->cpu = cpu;
+
+	lasrm(&vcpu->arch.save_area);
+}
+
+void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
+{
+	stiasrm(&vcpu->arch.save_area);
+
+	vcpu->cpu = -1;
+	restore_access_regs(&vcpu->arch.host_acrs[0]);
+}
+
 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
 				    struct kvm_mp_state *mp_state)
 {
@@ -223,6 +247,388 @@ static unsigned long system_supported_vcpu_features(void)
 	return features;
 }
 
+bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
+{
+	return vcpu_mode_priv(vcpu);
+}
+
+int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm_vcpu_initialized(vcpu))
+		return -ENOEXEC;
+
+	if (!kvm_arm_vcpu_is_finalized(vcpu))
+		return -EPERM;
+
+	if (likely(READ_ONCE(vcpu->pid)))
+		return 0;
+
+	scoped_guard(mutex, &kvm->arch.config_lock)
+		set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
+
+	return 0;
+}
+
+/**
+ * check_vcpu_requests - check and handle pending vCPU requests
+ * @vcpu:	the VCPU pointer
+ *
+ * Return: 1 if we should enter the guest
+ *	   0 if we should exit to userspace
+ *	   < 0 if we should exit to userspace, where the return value indicates
+ *	   an error
+ */
+static int check_vcpu_requests(struct kvm_vcpu *vcpu)
+{
+	if (kvm_request_pending(vcpu)) {
+		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
+			kvm_reset_vcpu(vcpu);
+		/*
+		 * Clear IRQ_PENDING requests that were made to guarantee
+		 * that a VCPU sees new virtual interrupts.
+		 */
+		kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
+	}
+
+	return 1;
+}
+
+static int kvm_vcpu_initialize(struct kvm_vcpu *vcpu,
+			       const struct kvm_vcpu_init *init)
+{
+	unsigned long features = init->features[0];
+	struct kvm *kvm = vcpu->kvm;
+
+	guard(mutex)(&kvm->arch.config_lock);
+	if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) &&
+	    kvm_vcpu_init_changed(vcpu, init))
+		return -EINVAL;
+
+	bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES);
+
+	kvm_reset_vcpu(vcpu);
+
+	set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags);
+	vcpu_set_flag(vcpu, VCPU_INITIALIZED);
+
+	return 0;
+}
+
+static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
+			       const struct kvm_vcpu_init *init)
+{
+	int ret;
+
+	if (init->target != KVM_ARM_TARGET_GENERIC_V8)
+		return -EINVAL;
+
+	ret = kvm_vcpu_init_check_features(vcpu, init);
+	if (ret)
+		return ret;
+
+	if (!kvm_vcpu_initialized(vcpu))
+		return kvm_vcpu_initialize(vcpu, init);
+
+	if (kvm_vcpu_init_changed(vcpu, init))
+		return -EINVAL;
+
+	kvm_reset_vcpu(vcpu);
+
+	return 0;
+}
+
+static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
+					 struct kvm_vcpu_init *init)
+{
+	struct kvm_sae_save_area *save_area = &vcpu->arch.save_area;
+	struct kvm_sae_block *sae_block = &vcpu->arch.sae_block;
+	int ret;
+
+	sae_block->save_area = virt_to_phys(save_area);
+	save_area->sdo = virt_to_phys(sae_block);
+
+	ret = kvm_vcpu_set_target(vcpu, init);
+	if (ret)
+		return ret;
+
+	scoped_guard(spinlock, &vcpu->arch.mp_state_lock) {
+		WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
+	}
+
+	return 0;
+}
+
+int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
+			  bool line_status)
+{
+	/* stub for now*/
+	return -EINVAL;
+}
+
+static void adjust_pc(struct kvm_vcpu *vcpu)
+{
+	if (vcpu_get_flag(vcpu, INCREMENT_PC)) {
+		kvm_skip_instr(vcpu);
+		vcpu_clear_flag(vcpu, INCREMENT_PC);
+	}
+}
+
+static void arm_vcpu_run(struct kvm_vcpu *vcpu)
+{
+	struct kvm_sae_block *sae_block = &vcpu->arch.sae_block;
+
+	adjust_pc(vcpu);
+
+	local_irq_disable();
+	guest_timing_enter_irqoff();
+	guest_state_enter_irqoff();
+	local_irq_enable();
+
+	sae_block->icptr = 0;
+
+	sae64a(sae_block);
+
+	local_irq_disable();
+	guest_state_exit_irqoff();
+	guest_timing_exit_irqoff();
+	local_irq_enable();
+}
+
+/** kvm_arch_vcpu_ioctl_run() - run arm64 vCPU
+ *
+ * Execute arm64 guest instructions using SAE.
+ *
+ * Returns:
+ * 1 enter the guest (should not be observed by userspace)
+ * 0 exit to userspace
+ * < 0 exit to userspace, where the return value indicates n error
+ *
+ *
+ */
+int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
+{
+	DECLARE_KERNEL_FPU_ONSTACK32(fpu_save);
+	struct kvm_run *kvm_run = vcpu->run;
+	int ret;
+
+	if (kvm_run->exit_reason == KVM_EXIT_MMIO) {
+		ret = kvm_handle_mmio_return(vcpu);
+		if (ret <= 0)
+			return ret;
+	}
+
+	vcpu_load(vcpu);
+
+	kernel_fpu_begin(&fpu_save, KERNEL_FPC | KERNEL_VXR);
+	load_vx_regs((vcpu->arch.ctxt.vregs));
+
+	if (!vcpu->wants_to_run) {
+		ret = -EINTR;
+		goto out;
+	}
+
+	kvm_sigset_activate(vcpu);
+
+	might_fault();
+
+	kvm_vcpu_srcu_read_lock(vcpu);
+
+	ret = 1;
+	do {
+		if (signal_pending(current)) {
+			kvm_run->exit_reason = KVM_EXIT_INTR;
+			ret = -EINTR;
+			break;
+		}
+
+		if (need_resched())
+			schedule();
+
+		if (ret > 0)
+			ret = check_vcpu_requests(vcpu);
+
+		vcpu->arch.sae_block.icptr = 0;
+
+		smp_store_mb(vcpu->mode, IN_GUEST_MODE);
+
+		if (kvm_request_pending(vcpu)) {
+			vcpu->mode = OUTSIDE_GUEST_MODE;
+			continue;
+		}
+
+		kvm_vcpu_srcu_read_unlock(vcpu);
+
+		arm_vcpu_run(vcpu);
+
+		vcpu->mode = OUTSIDE_GUEST_MODE;
+
+		kvm_vcpu_srcu_read_lock(vcpu);
+
+		ret = handle_exit(vcpu);
+
+	} while (ret > 0);
+
+	kvm_vcpu_srcu_read_unlock(vcpu);
+
+	kvm_sigset_deactivate(vcpu);
+out:
+	if (unlikely(vcpu_get_flag(vcpu, INCREMENT_PC)))
+		adjust_pc(vcpu);
+
+	save_vx_regs(vcpu->arch.ctxt.vregs);
+	kernel_fpu_end(&fpu_save, KERNEL_FPC | KERNEL_VXR);
+	vcpu_put(vcpu);
+
+	return ret;
+}
+
+static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
+				 struct kvm_device_attr *attr)
+{
+	int ret;
+
+	switch (attr->group) {
+	default:
+		ret = -ENXIO;
+		break;
+	}
+
+	return ret;
+}
+
+static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
+				 struct kvm_device_attr *attr)
+{
+	int ret;
+
+	switch (attr->group) {
+	default:
+		ret = -ENXIO;
+		break;
+	}
+
+	return ret;
+}
+
+static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
+				 struct kvm_device_attr *attr)
+{
+	int ret;
+
+	switch (attr->group) {
+	default:
+		ret = -ENXIO;
+		break;
+	}
+
+	return ret;
+}
+
+long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
+{
+	struct kvm_vcpu *vcpu = filp->private_data;
+	void __user *argp = (void __user *)arg;
+	struct kvm_device_attr attr;
+	int ret;
+
+	switch (ioctl) {
+	case KVM_ARM_VCPU_INIT: {
+		struct kvm_vcpu_init init;
+
+		ret = -EFAULT;
+		if (copy_from_user(&init, argp, sizeof(init)))
+			break;
+
+		ret = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
+		break;
+	}
+	case KVM_SET_ONE_REG:
+	case KVM_GET_ONE_REG: {
+		struct kvm_one_reg reg;
+
+		ret = -ENOEXEC;
+		if (unlikely(!kvm_vcpu_initialized(vcpu)))
+			break;
+
+		ret = -EFAULT;
+		if (copy_from_user(&reg, argp, sizeof(reg)))
+			break;
+
+		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
+			kvm_reset_vcpu(vcpu);
+
+		if (ioctl == KVM_SET_ONE_REG)
+			ret = kvm_arm_set_reg(vcpu, &reg);
+		else
+			ret = kvm_arm_get_reg(vcpu, &reg);
+		break;
+	}
+	case KVM_GET_REG_LIST: {
+		struct kvm_reg_list __user *user_list = argp;
+		struct kvm_reg_list reg_list;
+		unsigned int n;
+
+		ret = -ENOEXEC;
+		if (unlikely(!kvm_vcpu_initialized(vcpu)))
+			break;
+		ret = -EPERM;
+		if (!kvm_arm_vcpu_is_finalized(vcpu))
+			break;
+		ret = -EFAULT;
+		if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
+			break;
+		n = reg_list.n;
+		reg_list.n = kvm_arm_num_regs(vcpu);
+		if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
+			break;
+		ret = -E2BIG;
+		if (n < reg_list.n)
+			break;
+		ret = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
+		break;
+	}
+	case KVM_ARM_VCPU_FINALIZE: {
+		int what;
+
+		if (!kvm_vcpu_initialized(vcpu))
+			return -ENOEXEC;
+
+		if (get_user(what, (const int __user *)argp))
+			return -EFAULT;
+
+		ret = kvm_arm_vcpu_finalize(vcpu, what);
+		break;
+	}
+	case KVM_SET_DEVICE_ATTR: {
+		ret = -EFAULT;
+		if (copy_from_user(&attr, argp, sizeof(attr)))
+			break;
+		ret = kvm_arm_vcpu_set_attr(vcpu, &attr);
+		break;
+	}
+	case KVM_GET_DEVICE_ATTR: {
+		ret = -EFAULT;
+		if (copy_from_user(&attr, argp, sizeof(attr)))
+			break;
+		ret = kvm_arm_vcpu_get_attr(vcpu, &attr);
+		break;
+	}
+	case KVM_HAS_DEVICE_ATTR: {
+		ret = -EFAULT;
+		if (copy_from_user(&attr, argp, sizeof(attr)))
+			break;
+		ret = kvm_arm_vcpu_has_attr(vcpu, &attr);
+		break;
+	}
+	default:
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
 			       struct kvm_dirty_log *log)
 {
arch/s390/kvm/arm64/guest.c +45 -2

59 lines not on this page · raw message

arch/s390/kvm/arm64/handle_exit.c +31 -0
index ed872cd804dc..1286a64783e0 100644
@@ -5,6 +5,8 @@
 #include <arm64/esr.h>
 #include <arm64/kvm_emulate.h>
 
+#include "handle_exit.h"
+
 typedef int (*exit_handle_fn)(struct kvm_vcpu *);
 static exit_handle_fn arm_exit_handlers[ESR_ELx_EC_MAX + 1];
 
@@ -55,6 +57,35 @@ static exit_handle_fn arm_exit_handlers[] = {
 	[ESR_ELx_EC_HVC64]	= handle_hvc,
 };
 
+/*
+ * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
+ * proper exit to userspace.
+ */
+int handle_exit(struct kvm_vcpu *vcpu)
+{
+	u8 icptr = vcpu->arch.sae_block.icptr;
+	int ret = 1;
+
+	switch (icptr) {
+	case SAE_ICPTR_SPURIOUS:
+		break;
+	case SAE_ICPTR_VALIDITY:
+		WARN_ONCE(true, "SAE: validity intercept. vir: 0x%04x",
+			  vcpu->arch.sae_block.vir);
+		ret = -EINVAL;
+		break;
+	case SAE_ICPTR_HOST_ACCESS_EXCEPTION:
+	case SAE_ICPTR_SYNCHRONOUS_EXCEPTION:
+		ret = handle_trap_exceptions(vcpu);
+		break;
+	default:
+		WARN_ONCE(true, "SAE: unknown interception reason 0x%02x",
+			  icptr);
+		ret = -EINVAL;
+	}
+	return ret;
+}
+
 /* manually copied from arch/arm64/kernel/traps.c */
 static const char * const esr_class_str[] = {
 	[0 ... ESR_ELx_EC_MAX]		= "UNRECOGNIZED EC",
arch/s390/kvm/arm64/handle_exit.h new file +9 -0
new file mode 100644
index 000000000000..65ae58721537
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef KVM_ARM64_HANDLE_EXIT_H
+#define KVM_ARM64_HANDLE_EXIT_H
+
+#include <linux/kvm_host.h>
+
+int handle_exit(struct kvm_vcpu *vcpu);
+
+#endif /* KVM_ARM64_HANDLE_EXIT_H */
arch/s390/kvm/arm64/reset.c new file +71 -0

71 lines not on this page · raw message

arch/s390/kvm/arm64/trace.h +1 -1
index 3a5dcd039c13..1e3cb6108196 100644
@@ -5,7 +5,7 @@
 #include <linux/tracepoint.h>
 
 #undef TRACE_SYSTEM
-#define TRACE_SYSTEM kvm
+#define TRACE_SYSTEM kvm_arm64
 
 #endif /* KVM_ARM64_TRACE_KVM_H */
 
Signature · 1 line
2.53.0
author reviewer
#72 lore raw

PATCH v7 23/23 KVM: s390: arm64: Add KVM_S390_ARM64 Kconfig and Makefile

In reply to Steffen Eiden

Add all Kbuild/Makefile configurations to build a second KVM module on s390 implementing the arm64-KVM API. To prevent symbol conflicts with kvm-s390 all internal symbols in kvm-arm64 are mangled if compiled as built-in. The new module ins named kvm-arm64.

As in this case the build does not go through the normal build process the module parameter handling would be messed up. By forcing KBUILD_MODNAME to kvm-arm64 all parameters are at the same location and not at object/basename of the object file where the parameter is introduced.

---
 arch/s390/configs/debug_defconfig |  1 +
 arch/s390/configs/defconfig       |  1 +
 arch/s390/include/asm/Kbuild      |  3 +
 arch/s390/kvm/Kconfig             | 29 ++++++++++
 arch/s390/kvm/Makefile            |  1 +
 arch/s390/kvm/arm64/Makefile      | 91 +++++++++++++++++++++++++++++++
 6 files changed, 126 insertions(+)
 create mode 100644 arch/s390/kvm/arm64/Makefile
arch/s390/configs/debug_defconfig +1 -0
index 3dae71474333..ed9a38e763cd 100644
@@ -66,6 +66,7 @@ CONFIG_CMM=m
 CONFIG_APPLDATA_BASE=y
 CONFIG_S390_HYPFS_FS=y
 CONFIG_KVM=m
+CONFIG_KVM_S390_ARM64=m
 CONFIG_KVM_S390_UCONTROL=y
 CONFIG_S390_UNWIND_SELFTEST=m
 CONFIG_S390_KPROBES_SANITY_TEST=m
arch/s390/configs/defconfig +1 -0
index 6f5722634b4d..92d3c6442b0c 100644
@@ -64,6 +64,7 @@ CONFIG_CMM=m
 CONFIG_APPLDATA_BASE=y
 CONFIG_S390_HYPFS_FS=y
 CONFIG_KVM=m
+CONFIG_KVM_S390_ARM64=m
 CONFIG_S390_UNWIND_SELFTEST=m
 CONFIG_S390_KPROBES_SANITY_TEST=m
 CONFIG_S390_MODULES_SANITY_TEST=m
arch/s390/include/asm/Kbuild +3 -0
index 0c1fc47c3ba0..40a81419e353 100644
@@ -8,3 +8,6 @@ generic-y += asm-offsets.h
 generic-y += mcs_spinlock.h
 generic-y += mmzone.h
 generic-y += ring_buffer.h
+
+generated-y += sysreg-defs.h
+generated-y += kvm_arm64_modname.h
arch/s390/kvm/Kconfig +29 -0
index 8d3ee17a1bcb..9c69ea16031e 100644
@@ -53,4 +53,33 @@ config KVM_S390_UCONTROL
 
 	  If unsure, say N.
 
+config KVM_S390_ARM64
+	def_tristate y
+	prompt "KVM support for hardware accelerated arm64 guests"
+	depends on HAS_IOMEM
+	depends on KVM
+	select GUEST_PERF_EVENTS if PERF_EVENTS
+	select HAVE_KVM_CPU_RELAX_INTERCEPT
+	select HAVE_KVM_IRQCHIP
+	select HAVE_KVM_VCPU_RUN_PID_CHANGE
+	select IRQ_BYPASS_MANAGER
+	select KVM_COMMON
+	select KVM_MMIO
+	select KVM_VFIO
+	select SCHED_INFO
+	select XARRAY_MULTI
+	help
+	  Enable support for hosting virtualized, hardware-accelerated arm64
+	  virtual machines on s390 systems using the Start ARM Execution (SAE)
+	  instruction. This requires hardware models that support the Arm
+	  execution facility (AEF).
+
+	  The module provides a character device named /dev/kvm-arm64 to expose
+	  the hardware capabilities.
+
+	  To compile this driver as a module, choose M here. The module will
+	  be called kvm-arm64.
+
+	  If unsure, say N.
+
 endif # VIRTUALIZATION
arch/s390/kvm/Makefile +1 -0
index a4e3875b5bdd..00fabbdaac80 100644
@@ -4,3 +4,4 @@
 # Copyright IBM Corp. 2008
 
 obj-$(CONFIG_KVM) += s390/
+obj-$(CONFIG_KVM_S390_ARM64) += arm64/
arch/s390/kvm/arm64/Makefile new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#70 lore raw

PATCH v7 22/23 KVM: s390: arm64: Implement basic page fault handler

In reply to Steffen Eiden

Add host functionality to page in guest memory. If the guest does something unexpected or illegal exit to userspace abort guest execution. This behaviour will be changed to guest error injects once all sysregs are accessible for the host.

---
 arch/s390/kvm/arm64/handle_exit.c |   2 +
 arch/s390/kvm/arm64/mmio.c        |  15 +++
 arch/s390/kvm/arm64/mmu.c         | 177 ++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/trace.h       |  24 ++++
 4 files changed, 218 insertions(+)
 create mode 100644 arch/s390/kvm/arm64/mmio.c
 create mode 100644 arch/s390/kvm/arm64/mmu.c
arch/s390/kvm/arm64/handle_exit.c +2 -0
index 1286a64783e0..8b03e8ee5acf 100644
@@ -55,6 +55,8 @@ static int handle_hvc(struct kvm_vcpu *vcpu)
 static exit_handle_fn arm_exit_handlers[] = {
 	[0 ... ESR_ELx_EC_MAX]	= kvm_handle_unknown_ec,
 	[ESR_ELx_EC_HVC64]	= handle_hvc,
+	[ESR_ELx_EC_IABT_LOW]	= kvm_handle_guest_abort,
+	[ESR_ELx_EC_DABT_LOW]	= kvm_handle_guest_abort,
 };
 
 /*
arch/s390/kvm/arm64/mmio.c new file +15 -0
new file mode 100644
index 000000000000..908a6b43ddf7
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/kvm_host.h>
+#include <linux/bitfield.h>
+
+#include <trace/events/kvm.h>
+
+#include <arm64/kvm_emulate.h>
+#include <arm64/sysreg.h>
+
+#include "trace.h"
+
+#define __INCL_GEN_ARM_FILE
+#include "generated/mmio.inc"
+#undef __INCL_GEN_ARM_FILE
arch/s390/kvm/arm64/mmu.c new file +177 -0

177 lines not on this page · raw message

arch/s390/kvm/arm64/trace.h +24 -0
index 1e3cb6108196..2a205ba0165c 100644
@@ -7,6 +7,30 @@
 #undef TRACE_SYSTEM
 #define TRACE_SYSTEM kvm_arm64
 
+TRACE_EVENT(kvm_mmio_nisv,
+	TP_PROTO(unsigned long vcpu_pc, unsigned long esr,
+		 unsigned long far, unsigned long ipa),
+	TP_ARGS(vcpu_pc, esr, far, ipa),
+
+	TP_STRUCT__entry(
+		__field(	unsigned long,	vcpu_pc		)
+		__field(	unsigned long,	esr		)
+		__field(	unsigned long,	far		)
+		__field(	unsigned long,	ipa		)
+	),
+
+	TP_fast_assign(
+		__entry->vcpu_pc		= vcpu_pc;
+		__entry->esr			= esr;
+		__entry->far			= far;
+		__entry->ipa			= ipa;
+	),
+
+	TP_printk("ipa %#016lx, esr %#016lx, far %#016lx, pc %#016lx",
+		  __entry->ipa, __entry->esr,
+		  __entry->far, __entry->vcpu_pc)
+);
+
 #endif /* KVM_ARM64_TRACE_KVM_H */
 
 #undef TRACE_INCLUDE_PATH
Signature · 1 line
2.53.0
author reviewer
#3 lore raw

PATCH v7 2/23 KVM: Make device name configurable

In reply to Steffen Eiden

Allow KVM implementations to choose alternative device names. This is especially useful for architectures providing multiple KVM implementations simultaneously. Architectures providing multiple KVM implementations must compile the KVM common code once per implementation and mange symbols.

---
 include/linux/kvm_host.h |  4 +++-
 virt/kvm/async_pf.c      | 13 +++++++++++--
 virt/kvm/async_pf.h      |  4 ++--
 virt/kvm/kvm_main.c      | 42 ++++++++++++++++++++++++++++++----------
 4 files changed, 48 insertions(+), 15 deletions(-)
include/linux/kvm_host.h +3 -1
index bfecce1b5955..ec294e419762 100644
@@ -1082,7 +1082,9 @@ static inline void kvm_irqfd_exit(void)
 {
 }
 #endif
-int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module);
+int kvm_init(unsigned int vcpu_size, unsigned int vcpu_align, struct module *module);
+int kvm_init_with_dev(unsigned int vcpu_size, unsigned int vcpu_align,
+		      struct module *module, const char *dev_name, int minor);
 void kvm_exit(void);
 
 void kvm_get_kvm(struct kvm *kvm);
virt/kvm/async_pf.c +11 -2
index b8aaa96b799b..015761249107 100644
@@ -19,9 +19,18 @@
 
 static struct kmem_cache *async_pf_cache;
 
-int kvm_async_pf_init(void)
+int kvm_async_pf_init(const char *cache_name)
 {
-	async_pf_cache = KMEM_CACHE(kvm_async_pf, 0);
+	char *full_cache_name;
+
+	full_cache_name = kasprintf(GFP_KERNEL, "%s_async_pf", cache_name);
+	if (!full_cache_name)
+		return -ENOMEM;
+
+	async_pf_cache = kmem_cache_create(full_cache_name,
+					   sizeof(struct kvm_async_pf), 0,
+					   SLAB_ACCOUNT, NULL);
+	kfree(full_cache_name);
 
 	if (!async_pf_cache)
 		return -ENOMEM;
virt/kvm/async_pf.h +2 -2
index 90d1a7d8c6de..4a7b81743248 100644
@@ -12,11 +12,11 @@
 #define __KVM_ASYNC_PF_H__
 
 #ifdef CONFIG_KVM_ASYNC_PF
-int kvm_async_pf_init(void);
+int kvm_async_pf_init(const char *cache_name);
 void kvm_async_pf_deinit(void);
 void kvm_async_pf_vcpu_init(struct kvm_vcpu *vcpu);
 #else
-#define kvm_async_pf_init() (0)
+#define kvm_async_pf_init(_name) (0)
 #define kvm_async_pf_deinit() do {} while (0)
 #define kvm_async_pf_vcpu_init(C) do {} while (0)
 #endif
virt/kvm/kvm_main.c +32 -10

102 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#38 lore raw

PATCH v7 10/23 arm64: Share arm64 headers with s390

In reply to Steffen Eiden

Allow sharing of arm64 headers with s390 by marking the shared regions or add a comment that the whole file is shared.

The shared regions are marked with:

#ifdef ARM64_S390_COMMON

/* insert shared definitions here */

#endif /* ARM64_S390_COMMON */

The preprocessor symbol ARM64_S390_COMMON is always defined for arm64. s390 later will generate its own view of arm64 headers based on the content inside the ifdef/endif block.

No functional change.

---
 arch/arm64/Makefile                  |  5 +++++
 arch/arm64/include/asm/brk-imm.h     |  1 +
 arch/arm64/include/asm/esr.h         |  1 +
 arch/arm64/include/asm/kvm_arm.h     |  1 +
 arch/arm64/include/asm/kvm_emulate.h | 18 ++++++++++++++++++
 arch/arm64/include/asm/kvm_host.h    | 13 +++++++++++++
 arch/arm64/include/asm/ptrace.h      |  4 ++++
 arch/arm64/include/asm/sysreg.h      | 15 +++++++++++++++
 8 files changed, 58 insertions(+)
arch/arm64/Makefile +5 -0
index 6b005c8fef70..12cbad460258 100644
@@ -45,6 +45,11 @@ KBUILD_CFLAGS	+= $(CC_FLAGS_NO_FPU) \
 KBUILD_CFLAGS	+= $(call cc-disable-warning, psabi)
 KBUILD_AFLAGS	+= $(compat_vdso)
 
+# Enable all code shared to s390
+KBUILD_CFLAGS	+= -DARM64_S390_COMMON
+KBUILD_AFLAGS	+= -DARM64_S390_COMMON
+KBUILD_CPPFLAGS	+= -DARM64_S390_COMMON
+
 ifeq ($(call rustc-min-version, 108500),y)
 KBUILD_RUSTFLAGS += --target=aarch64-unknown-none-softfloat
 else
arch/arm64/include/asm/brk-imm.h +1 -0
index beb42c62b6ac..dd2d153dc0d8 100644
@@ -2,6 +2,7 @@
 /*
  * Copyright (C) 2012 ARM Ltd.
  */
+/* Whole file is shared with s390 */
 
 #ifndef __ASM_BRK_IMM_H
 #define __ASM_BRK_IMM_H
arch/arm64/include/asm/esr.h +1 -0
index da085c83b57e..2cb73bde7346 100644
@@ -3,6 +3,7 @@
  * Copyright (C) 2013 - ARM Ltd
  * Author: Marc Zyngier <[email protected]>
  */
+/* Whole file is shared with s390 */
 
 #ifndef __ASM_ESR_H
 #define __ASM_ESR_H
arch/arm64/include/asm/kvm_arm.h +1 -0
index 57edd8437200..678f0daed445 100644
@@ -3,6 +3,7 @@
  * Copyright (C) 2012,2013 - ARM Ltd
  * Author: Marc Zyngier <[email protected]>
  */
+/* Whole file is shared with s390 */
 
 #ifndef __ARM64_KVM_ARM_H__
 #define __ARM64_KVM_ARM_H__
arch/arm64/include/asm/kvm_emulate.h +18 -0
index a34a9417ff4d..09f04a5686a1 100644
@@ -47,6 +47,7 @@ void kvm_skip_instr32(struct kvm_vcpu *vcpu);
 int kvm_inject_dabt_excl_atomic(struct kvm_vcpu *vcpu, u64 addr);
 int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr);
 
+#ifdef ARM64_S390_COMMON
 void kvm_inject_undefined(struct kvm_vcpu *vcpu);
 void kvm_inject_sync(struct kvm_vcpu *vcpu, u64 esr);
 int kvm_inject_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr);
@@ -62,6 +63,8 @@ static inline int kvm_inject_sea_iabt(struct kvm_vcpu *vcpu, u64 addr)
 	return kvm_inject_sea(vcpu, true, addr);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static inline int kvm_inject_serror(struct kvm_vcpu *vcpu)
 {
 	/*
@@ -185,6 +188,7 @@ static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
 	*vcpu_cpsr(vcpu) |= PSR_AA32_T_BIT;
 }
 
+#ifdef ARM64_S390_COMMON
 /*
  * vcpu_get_reg and vcpu_set_reg should always be passed a register number
  * coming from a read of ESR_EL2. Otherwise, it may give the wrong result on
@@ -203,6 +207,8 @@ static __always_inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
 		vcpu_gp_regs(vcpu)[reg_num] = val;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static inline bool vcpu_is_el2_ctxt(const struct kvm_cpu_context *ctxt)
 {
 	switch (ctxt->regs.pstate & (PSR_MODE32_BIT | PSR_MODE_MASK)) {
@@ -405,6 +411,7 @@ static inline u64 kvm_vcpu_get_disr(const struct kvm_vcpu *vcpu)
 	return vcpu->arch.fault.disr_el1;
 }
 
+#ifdef ARM64_S390_COMMON
 static inline u32 kvm_vcpu_hvc_get_imm(const struct kvm_vcpu *vcpu)
 {
 	return kvm_vcpu_get_esr(vcpu) & ESR_ELx_xVC_IMM_MASK;
@@ -482,6 +489,8 @@ static __always_inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu)
 	return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static inline
 bool kvm_vcpu_trap_is_permission_fault(const struct kvm_vcpu *vcpu)
 {
@@ -522,6 +531,7 @@ static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)
 	return ESR_ELx_SYS64_ISS_RT(esr);
 }
 
+#ifdef ARM64_S390_COMMON
 static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
 {
 	if (kvm_vcpu_abt_iss1tw(vcpu)) {
@@ -546,6 +556,8 @@ static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
 	return kvm_vcpu_dabt_iswrite(vcpu);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
 {
 	return __vcpu_sys_reg(vcpu, MPIDR_EL1) & MPIDR_HWID_BITMASK;
@@ -587,6 +599,7 @@ static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
 	return vcpu_read_sys_reg(vcpu, r) & bit;
 }
 
+#ifdef ARM64_S390_COMMON
 static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
 						    unsigned long data,
 						    unsigned int len)
@@ -662,6 +675,8 @@ static __always_inline void kvm_incr_pc(struct kvm_vcpu *vcpu)
 		vcpu_set_flag((v), e);					\
 	} while (0)
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * Returns a 'sanitised' view of CPTR_EL2, translating from nVHE to the VHE
  * format if E2H isn't set.
@@ -768,6 +783,7 @@ static inline void kvm_reset_fpsimd(struct kvm_vcpu *vcpu)
 	memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs));
 }
 
+#ifdef ARM64_S390_COMMON
 /* Reset a vcpu's core registers. */
 static inline void kvm_reset_vcpu_core(struct kvm_vcpu *vcpu)
 {
@@ -792,6 +808,8 @@ static inline void kvm_reset_vcpu_core(struct kvm_vcpu *vcpu)
 	*vcpu_cpsr(vcpu) = pstate;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 /* PSCI reset handling for a vcpu. */
 static inline void kvm_reset_vcpu_psci(struct kvm_vcpu *vcpu,
 				       struct vcpu_reset_state *reset_state)
arch/arm64/include/asm/kvm_host.h +13 -0
index 5261c21a437c..a8ddce3921b8 100644
@@ -39,6 +39,7 @@
 
 #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS
 
+#ifdef ARM64_S390_COMMON
 #define KVM_VCPU_MAX_FEATURES 10
 #define KVM_VCPU_VALID_FEATURES	(BIT(KVM_VCPU_MAX_FEATURES) - 1)
 
@@ -96,6 +97,8 @@ struct vcpu_reset_state {
 	bool		reset;
 };
 
+#endif /* ARM64_S390_COMMON */
+
 #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
 				     KVM_DIRTY_LOG_INITIALLY_SET)
 
@@ -955,6 +958,7 @@ struct kvm_vcpu_arch {
 	pid_t pid;
 };
 
+#ifdef ARM64_S390_COMMON
 /*
  * Each 'flag' is composed of a comma-separated triplet:
  *
@@ -1113,6 +1117,8 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
 /* KVM is currently emulating an L2 to L1 exception */
 #define IN_NESTED_EXCEPTION	__vcpu_single_flag(sflags, BIT(9))
 
+#endif /* ARM64_S390_COMMON */
+
 #define vcpu_sve_max_vq(vcpu)	sve_vq_from_vl((vcpu)->arch.sve_max_vl)
 
 #define vcpu_sve_zcr_elx(vcpu)						\
@@ -1326,6 +1332,7 @@ int __init populate_nv_trap_config(void);
 
 void kvm_calculate_traps(struct kvm_vcpu *vcpu);
 
+#ifdef ARM64_S390_COMMON
 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
@@ -1341,6 +1348,8 @@ unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len);
 int kvm_handle_mmio_return(struct kvm_vcpu *vcpu);
 int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa);
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * Returns true if a Performance Monitoring Interrupt (PMI), a.k.a. perf event,
  * arrived in guest context.  For arm64, any event that arrives while a vCPU is
@@ -1533,11 +1542,15 @@ static inline bool __vcpu_has_feature(const struct kvm_arch *ka, int feature)
 	return test_bit(feature, ka->vcpu_features);
 }
 
+#ifdef ARM64_S390_COMMON
+
 #define kvm_vcpu_has_feature(k, f)	__vcpu_has_feature(&(k)->arch, (f))
 #define vcpu_has_feature(v, f)	__vcpu_has_feature(&(v)->kvm->arch, (f))
 
 #define kvm_vcpu_initialized(v) vcpu_get_flag(v, VCPU_INITIALIZED)
 
+#endif /* ARM64_S390_COMMON */
+
 int kvm_trng_call(struct kvm_vcpu *vcpu);
 #ifdef CONFIG_KVM
 extern phys_addr_t hyp_mem_base;
arch/arm64/include/asm/ptrace.h +4 -0
index 49a83a0636b9..05c1307fa62f 100644
@@ -26,6 +26,8 @@
 
 #define GIC_PRIO_PSR_I_SET	GICV3_PRIO_PSR_I_SET
 
+#ifdef ARM64_S390_COMMON
+
 /* Additional SPSR bits not exposed in the UABI */
 #define PSR_MODE_THREAD_BIT	(1 << 0)
 #define PSR_IL_BIT		(1 << 20)
@@ -66,6 +68,8 @@
 #define PSR_AA32_IT_MASK	0x0600fc00	/* If-Then execution state mask */
 #define PSR_AA32_GE_MASK	0x000f0000
 
+#endif /* ARM64_S390_COMMON */
+
 #ifdef CONFIG_CPU_BIG_ENDIAN
 #define PSR_AA32_ENDSTATE	PSR_AA32_E_BIT
 #else
arch/arm64/include/asm/sysreg.h +15 -0
index ab205f9db94a..1c5c4df260be 100644
@@ -16,6 +16,8 @@
 
 #include <asm/gpr-num.h>
 
+#ifdef ARM64_S390_COMMON
+
 /*
  * ARMv8 ARM reserves the following encoding for system registers:
  * (Ref: ARMv8 ARM, Section: "System instruction class encoding overview",
@@ -50,6 +52,8 @@
 #define sys_reg_CRm(id)	(((id) >> CRm_shift) & CRm_mask)
 #define sys_reg_Op2(id)	(((id) >> Op2_shift) & Op2_mask)
 
+#endif /* ARM64_S390_COMMON */
+
 #ifndef CONFIG_BROKEN_GAS_INST
 
 #ifdef __ASSEMBLER__
@@ -123,6 +127,8 @@
 #define GSB_SYS_BARRIER_INSN		__SYS_BARRIER_INSN(1, 0, 12, 0, 0, 31)
 #define GSB_ACK_BARRIER_INSN		__SYS_BARRIER_INSN(1, 0, 12, 0, 1, 31)
 
+#ifdef ARM64_S390_COMMON
+
 /* Data cache zero operations */
 #define SYS_DC_ISW			sys_insn(1, 0, 7, 6, 2)
 #define SYS_DC_IGSW			sys_insn(1, 0, 7, 6, 4)
@@ -832,6 +838,8 @@
 #define SCTLR_ELx_A	 (BIT(1))
 #define SCTLR_ELx_M	 (BIT(0))
 
+#endif /* ARM64_S390_COMMON */
+
 #ifdef CONFIG_CPU_BIG_ENDIAN
 #define ENDIAN_SET_EL2		SCTLR_ELx_EE
 #else
@@ -866,6 +874,7 @@
 	 SCTLR_EL1_LSMAOE | SCTLR_EL1_nTLSMD | SCTLR_EL1_EIS   | \
 	 SCTLR_EL1_TSCXT  | SCTLR_EL1_EOS)
 
+#ifdef ARM64_S390_COMMON
 /* MAIR_ELx memory attributes (used by Linux) */
 #define MAIR_ATTR_DEVICE_nGnRnE		UL(0x00)
 #define MAIR_ATTR_DEVICE_nGnRE		UL(0x04)
@@ -1102,6 +1111,8 @@
 #define GICV5_GICR_CDNMIA_TYPE_MASK	GENMASK_ULL(31, 29)
 #define GICV5_GICR_CDNMIA_ID_MASK	GENMASK_ULL(23, 0)
 
+#endif /* ARM64_S390_COMMON */
+
 #define gicr_insn(insn)			read_sysreg_s(GICV5_OP_GICR_##insn)
 #define gic_insn(v, insn)		write_sysreg_s(v, GICV5_OP_GIC_##insn)
 
@@ -1251,6 +1262,8 @@
 	par;								\
 })
 
+#ifdef ARM64_S390_COMMON
+
 #define SYS_FIELD_VALUE(reg, field, val)	reg##_##field##_##val
 
 #define SYS_FIELD_GET(reg, field, val)		\
@@ -1263,6 +1276,8 @@
 		 FIELD_PREP(reg##_##field##_MASK,	\
 			    SYS_FIELD_VALUE(reg, field, val))
 
+#endif /* ARM64_S390_COMMON */
+
 #endif /* __ASSEMBLER__ */
 
 #endif	/* __ASM_SYSREG_H */
Signature · 1 line
2.53.0
author reviewer
#36 lore raw

PATCH v7 9/23 arm64: Prepare sharing arm64 headers with s390

In reply to Steffen Eiden

Prepare the sharing of arm64 headers with s390 by moving definitions inside headers to reduce the number of shared markers. Clean up includes to further streamline the sharing of headers.

No functional change.

---
 arch/arm64/include/asm/esr.h         |   4 +-
 arch/arm64/include/asm/kvm_arm.h     |   5 +-
 arch/arm64/include/asm/kvm_emulate.h |   5 +-
 arch/arm64/include/asm/kvm_host.h    | 102 ++++++++++++++-------------
 arch/arm64/include/asm/sysreg.h      |   2 +-
 5 files changed, 62 insertions(+), 56 deletions(-)
arch/arm64/include/asm/esr.h +2 -2
index f816f5d77f1a..da085c83b57e 100644
@@ -7,7 +7,7 @@
 #ifndef __ASM_ESR_H
 #define __ASM_ESR_H
 
-#include <asm/memory.h>
+#include <asm/brk-imm.h>
 #include <asm/sysreg.h>
 
 #define ESR_ELx_EC_UNKNOWN	UL(0x00)
@@ -435,7 +435,7 @@
 #define ESR_ELx_IT_GCSPOPX		7
 
 #ifndef __ASSEMBLER__
-#include <asm/types.h>
+#include <linux/types.h>
 
 static inline unsigned long esr_brk_comment(unsigned long esr)
 {
arch/arm64/include/asm/kvm_arm.h +3 -2
index 4bfbd827c5aa..57edd8437200 100644
@@ -7,10 +7,11 @@
 #ifndef __ARM64_KVM_ARM_H__
 #define __ARM64_KVM_ARM_H__
 
+#include <linux/const.h>
+#include <linux/bits.h>
+#include <linux/types.h>
 #include <asm/esr.h>
-#include <asm/memory.h>
 #include <asm/sysreg.h>
-#include <asm/types.h>
 
 /*
  * Because I'm terribly lazy and that repainting the whole of the KVM
arch/arm64/include/asm/kvm_emulate.h +3 -2
index b31d50bdf59e..a34a9417ff4d 100644
@@ -44,11 +44,12 @@ enum exception_type {
 bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
 void kvm_skip_instr32(struct kvm_vcpu *vcpu);
 
+int kvm_inject_dabt_excl_atomic(struct kvm_vcpu *vcpu, u64 addr);
+int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr);
+
 void kvm_inject_undefined(struct kvm_vcpu *vcpu);
 void kvm_inject_sync(struct kvm_vcpu *vcpu, u64 esr);
-int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr);
 int kvm_inject_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr);
-int kvm_inject_dabt_excl_atomic(struct kvm_vcpu *vcpu, u64 addr);
 void kvm_inject_size_fault(struct kvm_vcpu *vcpu);
 
 static inline int kvm_inject_sea_dabt(struct kvm_vcpu *vcpu, u64 addr)
arch/arm64/include/asm/kvm_host.h +53 -49

144 lines not on this page · raw message

arch/arm64/include/asm/sysreg.h +1 -1
index 1aa601c95188..ab205f9db94a 100644
@@ -1263,6 +1263,6 @@
 		 FIELD_PREP(reg##_##field##_MASK,	\
 			    SYS_FIELD_VALUE(reg, field, val))
 
-#endif
+#endif /* __ASSEMBLER__ */
 
 #endif	/* __ASM_SYSREG_H */
Signature · 1 line
2.53.0
author reviewer
#64 lore raw

PATCH v7 20/23 KVM: s390: arm64: Implement vm/vcpu create destroy.

In reply to Steffen Eiden

Implement init and destroy IOCTLS for vcpu and vm. Implement arch vm IOCTL. Use s390 gmap and gmap plumbing. Implement function (stubs) required by gamp.

---
 arch/s390/kvm/arm64/arm.c       | 197 ++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/arm.h       |  54 +++++++++
 arch/s390/kvm/gmap/faultin.c    |   5 +
 arch/s390/kvm/gmap/gmap.c       |   4 +
 arch/s390/kvm/gmap/kvm_mmu.c    |   4 +
 arch/s390/kvm/gmap/trace_gmap.h |  17 +++
 6 files changed, 281 insertions(+)
arch/s390/kvm/arm64/arm.c +197 -0
index 299b070f8b6d..7df0e4e697e8 100644
@@ -8,6 +8,9 @@
 #include <linux/kvm_types.h>
 #include <linux/kvm_host.h>
 
+#include <gmap.h>
+#include <kvm_mmu.h>
+
 #include "arm.h"
 
 #define CREATE_TRACE_POINTS
@@ -19,6 +22,8 @@ static unsigned long system_supported_vcpu_features(void);
 #include "generated/arm.inc"
 #undef __INCL_GEN_ARM_FILE
 
+#define KVM_PHYS_SHIFT (40)
+
 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 {
 	int ret;
@@ -51,6 +56,83 @@ static u64 kvm_max_guest_address(void)
 	return ALIGN_DOWN(max_addr + 1, SZ_1G) - 1;
 }
 
+static int kvm_gmap_init(struct kvm *kvm)
+{
+	struct crst_table *table;
+
+	kvm->arch.gmap = gmap_new(kvm, gpa_to_gfn(kvm->arch.guest_phys_size));
+
+	if (!kvm->arch.gmap)
+		return -ENOMEM;
+
+	/* arm64 (on s390) do not have pfault */
+	clear_bit(GMAP_FLAG_PFAULT_ENABLED, &kvm->arch.gmap->flags);
+	set_bit(GMAP_FLAG_ALLOW_HPAGE_1M, &kvm->arch.gmap->flags);
+
+	table = dereference_asce(kvm->arch.gmap->asce);
+	crst_table_init((void *)table, _CRSTE_HOLE(table->crstes[0].h.tt).val);
+
+	return 0;
+}
+
+static int kvm_vm_type_ipa_size_shift(unsigned long type)
+{
+	int phys_shift;
+
+	phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type);
+	if (phys_shift) {
+		if (phys_shift > get_kvm_ipa_limit() ||
+		    phys_shift < ARM64_MIN_PARANGE_BITS)
+			return -EINVAL;
+	} else {
+		phys_shift = KVM_PHYS_SHIFT;
+		if (phys_shift > get_kvm_ipa_limit()) {
+			pr_warn_once("%s using unsupported default IPA limit\n",
+				     current->comm);
+			return -EINVAL;
+		}
+	}
+
+	return phys_shift;
+}
+
+int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
+{
+	char debug_name[32];
+	int ret;
+
+	if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK)
+		return -EINVAL;
+
+	ret = kvm_vm_type_ipa_size_shift(type);
+	if (ret < 0)
+		return ret;
+	kvm->arch.guest_phys_size = 1UL << ret;
+
+	mutex_init(&kvm->arch.config_lock);
+	bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
+
+	snprintf(debug_name, sizeof(debug_name), "kvm-arm64-%u-%u",
+		 current->pid, get_random_u32());
+	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
+	if (!kvm->arch.dbf)
+		return -ENOMEM;
+	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
+
+	ret = kvm_gmap_init(kvm);
+	if (ret)
+		goto out_err;
+	kvm->arch.mem_limit = kvm->arch.guest_phys_size;
+
+	VM_EVENT(kvm, 3, "vm created with type %lu", type);
+	return 0;
+
+out_err:
+	debug_unregister(kvm->arch.dbf);
+
+	return ret;
+}
+
 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
 {
 	return VM_FAULT_SIGBUS;
@@ -62,6 +144,13 @@ long kvm_arch_dev_ioctl(struct file *filp,
 	return -EINVAL;
 }
 
+void kvm_arch_destroy_vm(struct kvm *kvm)
+{
+	kvm_destroy_vcpus(kvm);
+	debug_unregister(kvm->arch.dbf);
+	kvm->arch.gmap = gmap_put(kvm->arch.gmap);
+}
+
 u32 get_kvm_ipa_limit(void)
 {
 	return fls64(kvm_max_guest_address() + 1) - 1;
@@ -72,10 +161,43 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
 	return 0;
 }
 
+int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
+{
+	struct kvm_sae_block *sae_block = &vcpu->arch.sae_block;
+	struct kvm_sae_save_area *save_area = &vcpu->arch.save_area;
+
+	spin_lock_init(&vcpu->arch.mp_state_lock);
+
+	/* Force users to call KVM_ARM_VCPU_INIT */
+	vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
+
+	vcpu->arch.mc = kvm_s390_new_mmu_cache();
+	if (!vcpu->arch.mc)
+		return -ENOMEM;
+
+	sae_block->hbasce = vcpu->kvm->arch.gmap->asce.val;
+	sae_block->mso = 0L;
+	sae_block->msl = vcpu->kvm->arch.mem_limit;
+	sae_block->save_area = virt_to_phys(save_area);
+
+	VM_EVENT(vcpu->kvm, 3,
+		 "create cpu %d at 0x%p, sae block at 0x%p, satellite at 0x%p",
+		 vcpu->vcpu_id, vcpu, &vcpu->arch.sae_block,
+		 &vcpu->arch.save_area);
+	return 0;
+}
+
 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
 {
 }
 
+void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
+{
+	kvm_s390_free_mmu_cache(vcpu->arch.mc);
+
+	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
+}
+
 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
 				    struct kvm_mp_state *mp_state)
 {
@@ -101,6 +223,81 @@ static unsigned long system_supported_vcpu_features(void)
 	return features;
 }
 
+int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
+			       struct kvm_dirty_log *log)
+{
+	return s390_kvm_mmu_get_dirty_log(kvm, log);
+}
+
+bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
+{
+	scoped_guard(read_lock, &kvm->mmu_lock)
+		return gmap_age_gfn(kvm->arch.gmap, range->start, range->end);
+}
+
+void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
+{
+	gfn_t last_gfn = memslot->base_gfn + memslot->npages;
+
+	scoped_guard(read_lock, &kvm->mmu_lock)
+		gmap_sync_dirty_log(kvm->arch.gmap, memslot->base_gfn, last_gfn);
+}
+
+int kvm_arch_prepare_memory_region(struct kvm *kvm,
+				   const struct kvm_memory_slot *old,
+				   struct kvm_memory_slot *new,
+				   enum kvm_mr_change change)
+{
+	return s390_kvm_mmu_prepare_memory_region(kvm, old, new, change);
+}
+
+void kvm_arch_commit_memory_region(struct kvm *kvm, struct kvm_memory_slot *old,
+				   const struct kvm_memory_slot *new,
+				   enum kvm_mr_change change)
+{
+	s390_kvm_mmu_commit_memory_region(kvm, old, new, change);
+}
+
+bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
+{
+	return gmap_unmap_gfn_range(kvm->arch.gmap, range->slot, range->start, range->end);
+}
+
+bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
+{
+	scoped_guard(read_lock, &kvm->mmu_lock)
+		return dat_test_age_gfn(kvm->arch.gmap->asce, range->start, range->end);
+}
+
+#ifdef CONFIG_KVM_GENERIC_PRE_FAULT_MEMORY
+long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
+				    struct kvm_pre_fault_memory *range)
+{
+	return -EOPNOTSUPP;
+}
+#endif /* CONFIG_KVM_GENERIC_PRE_FAULT_MEMORY*/
+
+int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
+{
+	void __user *argp = (void __user *)arg;
+
+	switch (ioctl) {
+	case KVM_ARM_PREFERRED_TARGET: {
+		struct kvm_vcpu_init init = {
+			.target = KVM_ARM_TARGET_GENERIC_V8,
+		};
+
+		if (copy_to_user(argp, &init, sizeof(init)))
+			return -EFAULT;
+
+		return 0;
+	}
+
+	default:
+		return -EINVAL;
+	}
+}
+
 bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
 {
 	return false;
arch/s390/kvm/arm64/arm.h +54 -0

60 lines not on this page · raw message

arch/s390/kvm/gmap/faultin.c +5 -0
index 1dc79807012c..2e93bd563d5c 100644
@@ -10,6 +10,11 @@
 
 #include "gmap.h"
 #include "faultin.h"
+#ifdef KVM_S390_ARM64
+#include "arm.h"
+#else
+#include "s390.h"
+#endif
 
 bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu);
 #define CREATE_TRACE_POINTS
arch/s390/kvm/gmap/gmap.c +4 -0
index 4968330e9553..b7071f50f7ad 100644
@@ -21,7 +21,11 @@
 
 #include "dat.h"
 #include "gmap.h"
+#ifdef KVM_S390_ARM64
+#include "arm.h"
+#else
 #include "s390.h"
+#endif
 #include "faultin.h"
 
 static int gmap_limit_to_type(gfn_t limit)
arch/s390/kvm/gmap/kvm_mmu.c +4 -0
index b08b8229bb6f..a26a16193467 100644
@@ -3,7 +3,11 @@
 #include <linux/kvm_types.h>
 #include <linux/kvm_host.h>
 
+#ifdef KVM_S390_ARM64
+#include "arm.h"
+#else
 #include "s390.h"
+#endif
 #include "gmap.h"
 #include "dat.h"
 #include "kvm_mmu.h"
arch/s390/kvm/gmap/trace_gmap.h +17 -0
index 15ec88bdce55..431c9442b10c 100644
@@ -5,12 +5,28 @@
 #include <linux/tracepoint.h>
 
 #undef TRACE_SYSTEM
+#ifdef KVM_S390_ARM64
+#define TRACE_SYSTEM kvm_arm64
+#else
 #define TRACE_SYSTEM kvm
+#endif
 #undef TRACE_INCLUDE_PATH
 #define TRACE_INCLUDE_PATH ../gmap
 #undef TRACE_INCLUDE_FILE
 #define TRACE_INCLUDE_FILE trace_gmap
 
+#ifdef KVM_S390_ARM64
+#define __KVM_FIELDS \
+	__field(unsigned long, pstate) \
+	__field(unsigned long, pc)
+#define __KVM_ASSIGN ({\
+	__entry->pstate = vcpu->arch.sae_block.pstate; \
+	__entry->pc = vcpu->arch.sae_block.pc; \
+	})
+#define __KVM_PRINT \
+	__entry->pstate, \
+	__entry->pc
+#else
 #define __KVM_FIELDS \
 	__field(unsigned long, pswmask) \
 	__field(unsigned long, pswaddr)
@@ -21,6 +37,7 @@
 #define __KVM_PRINT \
 	__entry->pswmask,\
 	__entry->pswaddr
+#endif
 
 TRACE_EVENT(kvm_s390_major_guest_pfault,
 	    TP_PROTO(struct kvm_vcpu *vcpu),
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v7 01/23] vfio: Use file-based reference counting for KVM

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Unconditional error-path clearing of device->kvm in vfio_df_ioctl_bind_iommufd causes Use-After-Free of KVM VM file. --

commit 17abe1f20f0f7d26badf30ce42a340a4d82f7c46
Author: Steffen Eiden <[email protected]>

vfio: Use file-based reference counting for KVM

Replace manual module reference counting with file-based reference counting for KVM integration. Previously, VFIO used symbol_get() to obtain function pointers for kvm_get_kvm_safe() and kvm_put_kvm(), then manually tracked module references through these symbols. This approach required storing the put_kvm function pointer in each device and carefully managing symbol references. Remove the put_kvm field in struct vfio_device as is it no longer used.

Quoted 4 lines
drivers/vfio/vfio_main.c +0 -0
index 423ead48aafe3..ed96acfa86358 100644

[ ... ]

Quoted 5 lines
drivers/vfio/vfio_main.c +0 -1
@@ -511,14 +489,7 @@ void vfio_device_put_kvm(struct vfio_device *device)
 	if (!device->kvm)
 		return;
 
-	if (WARN_ON(!device->put_kvm))

drivers/vfio/vfio_main.c void vfio_device_put_kvm(struct vfio_device *device) · line 494

Quoted 10 lines
drivers/vfio/vfio_main.c +1 -7
-		goto clear;
-
-	device->put_kvm(device->kvm);
-	device->put_kvm = NULL;
-	symbol_put(kvm_put_kvm);
-
-clear:
+	fput(device->kvm);
 	device->kvm = NULL;
 }

[Severity: Critical]
This is a pre-existing issue, but looking at how this function is called from the error path in vfio_df_ioctl_bind_iommufd(), could this cause a use-after-free of the KVM VM file?

If a process opens a VFIO cdev (df1) and successfully binds it via VFIO_DEVICE_BIND_IOMMUFD, device->kvm is set to the KVM file and device->open_count is 1.

If the process then opens a second cdev fd (df2) for the same device and calls VFIO_DEVICE_BIND_IOMMUFD again without associating a KVM VM:

df2->access_granted is false, bypassing the already bound check. vfio_df_get_kvm_safe(df2) does nothing because df2->kvm is NULL, leaving device->kvm intact.
vfio_df_open(df2) then fails because the device is already open (device->open_count != 0).

The error path in vfio_df_ioctl_bind_iommufd() then unconditionally drops the KVM reference:

drivers/vfio/device_cdev.c:vfio_df_ioctl_bind_iommufd() {
    ...
out_put_kvm:
    vfio_device_put_kvm(device);
    ...
}

This calls fput(device->kvm) and sets device->kvm = NULL here in vfio_device_put_kvm(), dropping the legitimate KVM reference held by df1. When userspace subsequently closes the KVM fd, the VM is freed, but the active VFIO driver (e.g., vfio_ap) retains a dangling pointer to it.

Could this lead to a use-after-free upon the next operation?

Should the error path in vfio_df_ioctl_bind_iommufd() avoid calling vfio_device_put_kvm() if the failed caller wasn't the one that set device->kvm?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

Allow KVM implementations to choose alternative device names. This is especially useful for architectures providing multiple KVM implementations simultaneously. Architectures providing multiple KVM implementations must compile the KVM common code once per implementation and mange symbols.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#19 lore raw

Re: [PATCH v7 03/23] KVM: Allow KVM implementations to switch off MMIO independent of Kconfig

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

Only defining KVM_MMIO is not flexible enough for multi-KVM systems where one KVM implements MMIO but others do not. Solve this by additionally testing for the non-existence of KVM_NO_MMIO before including MMIO code to KVM.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#27 lore raw

Re: [PATCH v7 04/23] arm64: Use proper include variant

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

Use <> for including the sysreg header as it is not discoverable from the current directory.

Fixes c07d8017bceb ("arm64/sysreg: Enable automatic generation of system register definitions")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#29 lore raw

Re: [PATCH v7 05/23] arm64: ptrace: Use constants for compat register numbers

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

Define an enum to determine the register number for the mapping from AArch32 and AArch64 registers. This allows for a more flexible use of the register mappings.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#31 lore raw

Re: [PATCH v7 06/23] arm64: sysreg: Convert SPSR_ELx to automatic register generation

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

Convert SPSR_EL{1, 2, 12} to automatic register generation as per DDIO616 2026-06. No functional change.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#33 lore raw

Re: [PATCH v7 07/23] KVM: arm64: Access elements of vcpu_gp_regs individually

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

While for arm64 the members of vcpu_gp_regs are allocated continuous this is not necessarily true for other architectures implementing ARM.

Let vcpu_gp_regs() no longer return the address of the user_pt_regs in the vcpu context but the address of the gp-register array field in the user_pt_reg struct. Note, this change semantically excludes pc from the GPRs.

Quoted 5 lines

No functional change.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#35 lore raw

Re: [PATCH v7 08/23] KVM: arm64: Use accessor functions for core regs

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

Instead of accessing the value directly use assessor function that abstract the actual location of the register values. Add a helper function to reset fpsimd registers. Enable other KVM implementations to reuse this code.

No functional changes.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#37 lore raw

Re: [PATCH v7 09/23] arm64: Prepare sharing arm64 headers with s390

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

Prepare the sharing of arm64 headers with s390 by moving definitions inside headers to reduce the number of shared markers. Clean up includes to further streamline the sharing of headers.

No functional change.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#39 lore raw

Re: [PATCH v7 10/23] arm64: Share arm64 headers with s390

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Using #ifdef ARM64_S390_COMMON as a marker for shared code hides critical macros from userspace…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Using #ifdef ARM64_S390_COMMON as a marker for shared code hides critical macros from userspace tools, breaking their compilation. --

commit ef8ab1698ed178bf6b33deea77a5a3895fabd918
Author: Steffen Eiden <[email protected]>

arm64: Share arm64 headers with s390

This commit modifies arm64 headers to allow sharing definitions with s390 by enclosing shared regions within an ARM64_S390_COMMON ifdef block. The preprocessor symbol is defined for arm64 via the Makefile.

arch/arm64/Makefile KBUILD_CFLAGS += $(CC_FLAGS_NO_FPU) \ · line 49

Quoted 10 lines
arch/arm64/Makefile +2 -0
index 6b005c8fef706..12cbad460258f 100644
@@ -45,6 +45,11 @@ KBUILD_CFLAGS	+= $(CC_FLAGS_NO_FPU) \
 KBUILD_CFLAGS	+= $(call cc-disable-warning, psabi)
 KBUILD_AFLAGS	+= $(compat_vdso)
 
+# Enable all code shared to s390
+KBUILD_CFLAGS	+= -DARM64_S390_COMMON

[Severity: Medium]
Will this break standalone compilation environments? The macro is only injected into the core kernel build flags (KBUILD_CFLAGS) in arch/arm64/Makefile. Standalone tools like KVM selftests or perf, as well as out-of-tree tracing scripts that do not inherit these core flags, will not have ARM64_S390_COMMON defined.

[ ... ]

arch/arm64/include/asm/kvm_host.h line 43

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +1 -0
index 5261c21a437cc..a8ddce3921b87 100644
@@ -39,6 +39,7 @@
 
 #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS
 
+#ifdef ARM64_S390_COMMON
 #define KVM_VCPU_MAX_FEATURES 10

[Severity: Medium]
Does this code break the struct kvm_arch definition for environments where ARM64_S390_COMMON is not defined? The KVM_VCPU_MAX_FEATURES macro in arch/arm64/include/asm/kvm_host.h is hidden inside the ifdef block, but it is used outside the block to define the size in DECLARE_BITMAP(vcpu_features, KVM_VCPU_MAX_FEATURES).

[ ... ]

Quoted 14 lines
arch/arm64/include/asm/sysreg.h +2 -0
index ab205f9db94ad..1c5c4df260bea 100644
@@ -16,6 +16,8 @@
 
 #include <asm/gpr-num.h>
 
+#ifdef ARM64_S390_COMMON
+
 /*
  * ARMv8 ARM reserves the following encoding for system registers:
  * (Ref: ARMv8 ARM, Section: "System instruction class encoding overview",

arch/arm64/include/asm/sysreg.h line 55

Quoted 4 lines
arch/arm64/include/asm/sysreg.h +1 -0
@@ -50,6 +52,8 @@
 #define sys_reg_CRm(id)	(((id) >> CRm_shift) & CRm_mask)
 #define sys_reg_Op2(id)	(((id) >> Op2_shift) & Op2_mask)
 
+#endif /* ARM64_S390_COMMON */

[Severity: Medium]
Does hiding the sys_reg macro inside the ifdef block break the compilation of all SYS_* registers for tools that sync arch/arm64/include/asm/sysreg.h? If the tool does not define ARM64_S390_COMMON, dependent macros like SYS_SCTLR_EL1 will become undefined.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#41 lore raw

Re: [PATCH v7 11/23] KVM: arm64: Share arm64 code with s390

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

Mark functions that s390 can use to implement arm on s390 as shared functions.

No functional change.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#49 lore raw

Re: [PATCH v7 12/23] s390/tools: Use arm64 headers

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines

Introduce a flexible system for sharing ARM64 headers with s390 KVM. Selected arm64 headers or snippet of headers will be reachable by s390 code through asm/header.h.

Add copy-arm64h.awk that detects ARM64_S390_COMMON markers and extracts marked section into a generated header consumable by s390. Falls back to full file copy when no markers are found.

Transform copied includes by replacing '#include <asm/...>' with '#include <arm64/...>' and similar for uapi headers to ensure correct path resolution on s390 and allow for a clear separation of s390 asm headers and arm64 asm headers.

Quoted 4 lines

Integrate this extraction into the s390/kapi target, conditional on (the not yet existing) CONFIG_KVM_ARM64.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#51 lore raw

Re: [PATCH v7 13/23] KVM: s390: Use arm64 code

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The AWK extraction script silently suppresses errors for unbalanced end markers if a valid block…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The AWK extraction script silently suppresses errors for unbalanced end markers if a valid block was already processed, potentially leading to silently dropped code. --

commit 695b985ed66a46463a7810ac95a1f61e48477f7a
Author: Steffen Eiden <[email protected]>

KVM: s390: Use arm64 code

This commit adds infrastructure to extract KVM/arm64 code into s390 at build time. It introduces copy-arm64c.awk to detect ARM64_S390_COMMON markers and extract marked sections into .inc files.

Quoted 5 lines
arch/s390/kvm/arm64/copy-arm64c.awk new file +0 -0
new file mode 100644
index 0000000000000..20bd73c52ec48

[ ... ]

Quoted 5 lines

+$0 ~ end_pattern {

arch/s390/kvm/arm64/copy-arm64c.awk new file +4 -0
+	if (!copying) { exit 1 }
+	copying = 0
+	next
+}

[ ... ]

arch/s390/kvm/arm64/copy-arm64c.awk line 72

Quoted 3 lines
arch/s390/kvm/arm64/copy-arm64c.awk new file +3 -0
+END {
+	exit !found_marker || copying
+}

[Severity: Medium]
Does the END block unintentionally suppress errors from unbalanced end markers?

If a source file contains a valid ARM64_S390_COMMON block, found_marker becomes 1 and copying becomes 0 after it finishes. If an extra, unbalanced end marker is encountered later in the file, the end_pattern block evaluates if (!copying) and calls exit 1.

However, when exit 1 is called, awk proceeds to the END block. Since found_marker is 1 and copying is 0, the expression !found_marker || copying evaluates to 0. This causes the script to override the exit 1 with an exit 0, silently returning success and stopping file processing instead of failing the build.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#53 lore raw

Re: [PATCH v7 14/23] KVM: s390: Prepare KVM/s390 for a second KVM module

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

The second KVM module will have a different Kconfig set. When both modules are compiled the Kconfig sets get merged and the native s390 KVM needs to implement functionality required by the respective config options. Ensure that s390-KVM will still compile by implementing a stub for kvm_arch_vcpu_run_pid_change if CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE is enabled and defining KVM_NO_MMIO to disable KVM_MMIO.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#55 lore raw

Re: [PATCH v7 15/23] s390: Introduce Start Arm Execution instruction

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Missing hardware branch predictor isolation (Spectre v2 mitigations) and alignment padding around…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Missing hardware branch predictor isolation (Spectre v2 mitigations) and alignment padding around the Start Arm Execution (SAE) instruction. --

commit 5f700400137f55bf83aa7484eb428f79dafd9340
Author: Steffen Eiden <[email protected]>

s390: Introduce Start Arm Execution instruction

This commit introduces the Start Arm Execution (SAE) instruction to enable accelerated execution of arm64 virtual machines on s390. It adds the new header variants, control block structures, and the __sae64a assembly routine to handle host-guest register state transitions.

Quoted 4 lines
arch/s390/kernel/entry.S +0 -0
index 10dd9bbdf9856..6f6f8f6ff9c48 100644

[ ... ]

Quoted 4 lines
arch/s390/kernel/entry.S +0 -0
@@ -245,6 +245,28 @@ EXPORT_SYMBOL(__sie64a)
 EXPORT_SYMBOL(sie_exit)
 #endif
 

arch/s390/kernel/entry.S EXPORT_SYMBOL(__sie64a) · line 257

Quoted 10 lines
arch/s390/kernel/entry.S +10 -0
+#if IS_ENABLED(CONFIG_KVM_S390_ARM64)
+/*
+ * __sae64a calling convention:
+ * %r2 pointer to sae control block physical address
+ */
+SYM_FUNC_START(__sae64a)
+	stmg	%r6,%r14,__SF_GPRS(%r15)	# store kernel registers
+	STBEAR	__SF_SAE_BEAR(%r15)		# save breaking event address register
+	.insn	rre,0xb9a50000,%r2,0		# Start Arm Execution
+	LBEAR	__SF_SAE_BEAR(%r15)		# restore breaking event address register

[Severity: High]
Does this sequence safely handle branch predictor state when transitioning to and from the untrusted guest?

Unlike the analogous __sie64a implementation, __sae64a appears to lack the BPEXIT, BPOFF, and BPENTER macros to flush or isolate the branch predictor. Could a malicious guest poison the host CPU's Branch Target Buffer (BTB) or Return Stack Buffer (RSB), leading to speculative execution vulnerabilities (Spectre v2) that leak host kernel memory?

Additionally, is a padding instruction such as "nopr 7" needed immediately following the SAE instruction here? For SIE, this padding is required to safely handle mishandled machine checks on the instruction boundary.

Quoted 11 lines
arch/s390/kernel/entry.S +11 -0
+	lmg	%r6,%r14,__SF_GPRS(%r15)	# restore kernel registers
+	xgr	%r0,%r0				# clear guest registers to
+	xgr	%r1,%r1				# prevent speculative use
+	xgr	%r2,%r2
+	xgr	%r3,%r3
+	xgr	%r4,%r4
+	xgr	%r5,%r5
+	BR_EX	%r14
+SYM_FUNC_END(__sae64a)
+EXPORT_SYMBOL(__sae64a)
+#endif
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
#57 lore raw

Re: [PATCH v7 16/23] KVM: s390: arm64: Introduce host definitions

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The vCPU Hypervisor Configuration Register is named `hcr_elz` instead of `hcr_el2`, introducing a…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The vCPU Hypervisor Configuration Register is named hcr_elz instead of hcr_el2, introducing a confusing typo/naming anomaly. --

commit 22fd1b09d8ddab9e5e022c14adc2bbf3373594b6
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Introduce host definitions

Add all basic definitions the arm on s390 KVM host requires. This includes struct kvm*arch definitions, various functions, and macros to run arm64 guests, and updates asm/kvm_host.h to use the new header if KVM_S390_ARM64 is defined.

Quoted 5 lines
arch/s390/include/asm/kvm_host_arm64.h new file +0 -0
new file mode 100644
index 0000000000000..b8a689c649bd8

[ ... ]

Quoted 1 line
arch/s390/include/asm/kvm_host_arm64.h new file +0 -0
@@ -0,0 +1,211 @@

[ ... ]

arch/s390/include/asm/kvm_host_arm64.h line 52

Quoted 9 lines
arch/s390/include/asm/kvm_host_arm64.h new file +9 -0
+struct kvm_vcpu_arch {
+	struct kvm_sae_block sae_block;
+	struct kvm_sae_save_area save_area;
+	struct kvm_cpu_context ctxt;
+
+	u32 host_acrs[NUM_ACRS];
+
+	/* Hypervisor Configuration Register */
+	u64 hcr_elz;

[Severity: Low]
Is hcr_elz intended to be hcr_el2 here?

While this might be an intentional pun and does not break compilation because shared ARM64 KVM logic compiled under ARM64_S390_COMMON does not currently access vcpu->arch.hcr_el2, naming this register hcr_elz departs from standard ARM64 architectural nomenclature.

Could this cause confusion for future development trying to match ARM64 architectural registers?

Quoted 3 lines
arch/s390/include/asm/kvm_host_arm64.h new file +3 -0
+
+	/* Configuration flags, set once and for all before the vcpu can run */
+	u8 cflags;

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
#59 lore raw

Re: [PATCH v7 17/23] s390/hwcaps: Report SAE support as hwcap

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

From: Hendrik Brueckner <[email protected]>

Report SAE support as hwcap (and /proc/cpuinfo)

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
#61 lore raw

Re: [PATCH v7 18/23] KVM: s390: Add basic arm64 kvm module

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

Add basic code for the new arm64 on s390 KVM implementation. Add kernel module boilerplate code and trivial functions. Add tracing scaffolding.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
#63 lore raw

Re: [PATCH v7 19/23] KVM: s390: arm64: Implement required functions

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Stubbed exception injection functions cause infinite trap loops, allowing guest userspace to…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Stubbed exception injection functions cause infinite trap loops, allowing guest userspace to permanently hang a VCPU.
- [High] Incorrect endianness mapping for 32-bit FPSR/FPCR registers accessed via KVM_GET_ONE_REG / KVM_SET_ONE_REG. --

commit 93d3dbc5966621adfcec645f4ad14f3ea7a0f69a
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Implement required functions

This commit implements the required arm64 KVM shared functions for s390, adding necessary headers and basic exception injection stubs. It also provides a temporary SMC/HVC exit handler.

Quoted 4 lines
arch/s390/include/arm64/kvm_emulate.h new file +0 -0
new file mode 100644

[ ... ]

arch/s390/include/arm64/kvm_emulate.h line 42

Quoted 4 lines
arch/s390/include/arm64/kvm_emulate.h new file +4 -0
+static __always_inline u64 *vcpu_fpsr(struct kvm_vcpu *vcpu)
+{
+	return &vcpu->arch.sae_block.fpsr;
+}

[Severity: High]
Because s390 is big-endian, does returning a u64 pointer here cause data corruption during KVM_GET_ONE_REG or KVM_SET_ONE_REG calls?

The shared arm64 code treats fp_regs.fpsr as a 32-bit value and casts the returned pointer to (u32 *). Pointing to a 64-bit value on a big-endian system means the cast will access the most-significant 32 bits (which are zero) instead of the lower 32 bits containing the actual register state.

arch/s390/include/arm64/kvm_emulate.h line 47

Quoted 5 lines
arch/s390/include/arm64/kvm_emulate.h new file +5 -0
+
+static __always_inline u64 *vcpu_fpcr(struct kvm_vcpu *vcpu)
+{
+	return &vcpu->arch.sae_block.fpcr;
+}

[Severity: High]
Does this have the same 32-bit KVM API endianness corruption issue as vcpu_fpsr() above?

[ ... ]

Quoted 4 lines
arch/s390/kvm/arm64/inject_fault.c new file +0 -0
new file mode 100644

[ ... ]

arch/s390/kvm/arm64/inject_fault.c line 15

Quoted 4 lines
arch/s390/kvm/arm64/inject_fault.c new file +4 -0
+void kvm_inject_undefined(struct kvm_vcpu *vcpu)
+{
+	/* Stub until s390 supports arm64 sysregs TODO sysregs*/
+}

[Severity: High]
Does leaving this stub empty cause an infinite trap loop on the host?

When an unprivileged guest userspace process executes an undefined instruction, KVM triggers an exception intercept. The KVM handler kvm_handle_unknown_ec() calls kvm_inject_undefined(), which does nothing here.

Since kvm_handle_unknown_ec() returns 1 and the stub doesn't advance the guest PC, KVM will immediately re-enter the guest at the exact same faulting instruction, permanently hanging the guest VCPU.

arch/s390/kvm/arm64/inject_fault.c line 21

Quoted 6 lines
arch/s390/kvm/arm64/inject_fault.c new file +6 -0
+
+int kvm_inject_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr)
+{
+	/* Stub until s390 supports arm64 sysregs TODO sysregs*/
+	return 1;
+}

[Severity: High]
Does this stub act identically to kvm_inject_undefined() and cause a hang when a Synchronous External Abort is triggered (for example, via KVM_SET_VCPU_EVENTS)?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
#65 lore raw

Re: [PATCH v7 20/23] KVM: s390: arm64: Implement vm/vcpu create destroy.

In reply to Steffen Eiden
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Implement init and destroy IOCTLS for vcpu and vm. Implement arch vm IOCTL. Use s390 gmap and gmap plumbing. Implement function (stubs) required by gamp.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
#67 lore raw

Re: [PATCH v7 21/23] KVM: s390: arm64: Implement vCPU IOCTLs

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [Critical] Double vcpu_load() call in kvm_arch_vcpu_ioctl_run causes preempt notifier list corruption.…

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [Critical] Double vcpu_load() call in kvm_arch_vcpu_ioctl_run causes preempt notifier list corruption.
- [Critical] Unhandled generic vCPU requests cause an infinite loop in check_vcpu_requests.
- [High] Missing vcpu_load() and vcpu_put() around architectural KVM IOCTLs.
- [High] Host-userspace reachable WARN_ONCE for SAE validity intercepts enables Denial of Service.
- [High] KVM_ARM_VCPU_FINALIZE uses incorrect initialization gates and silently ignores feature finalization.
- [High] TOCTOU race in KVM request checking due to host IRQs being enabled before guest entry.
- [Low] The return value of check_vcpu_requests() is dead code and incorrectly handled.
--

commit 3e429b0d42b7560ba0a2cd3dac96eb26a3ffe9cd
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Implement vCPU IOCTLs

This commit implements the required architectural vCPU IOCTLs for arm64 on s390. It adds the main vCPU execution loop, state initialization, feature finalization, and handling of architectural registers.

Quoted 4 lines
arch/s390/kvm/arm64/arm.c +0 -0
index 7df0e4e697e80..3caa72f780a52 100644

[ ... ]

Quoted 4 lines
arch/s390/kvm/arm64/arm.c +0 -0
@@ -223,6 +247,388 @@ static unsigned long system_supported_vcpu_features(void)
 	return features;
 }
 

[ ... ]

Quoted 4 lines
arch/s390/kvm/arm64/arm.c +4 -0
+static int check_vcpu_requests(struct kvm_vcpu *vcpu)
+{
+	if (kvm_request_pending(vcpu)) {
+		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))

arch/s390/kvm/arm64/arm.c static unsigned long system_supported_vcpu_features(void) · line 296

Quoted 10 lines
arch/s390/kvm/arm64/arm.c +10 -0
+			kvm_reset_vcpu(vcpu);
+		/*
+		 * Clear IRQ_PENDING requests that were made to guarantee
+		 * that a VCPU sees new virtual interrupts.
+		 */
+		kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
+	}
+
+	return 1;
+}

[Severity: Critical]
Does this code handle other generic KVM requests properly?

If a request other than KVM_REQ_VCPU_RESET or KVM_REQ_IRQ_PENDING is made (such as KVM_REQ_SLEEP), it seems it will remain pending.

This could interact with the run loop in kvm_arch_vcpu_ioctl_run():

kvm_arch_vcpu_ioctl_run() {
    ...
    if (kvm_request_pending(vcpu)) {
        vcpu->mode = OUTSIDE_GUEST_MODE;
        continue;
    }
    ...
}

Because the unhandled request is never cleared, would the vCPU thread become permanently stuck in an infinite loop without entering the guest?

[ ... ]

arch/s390/kvm/arm64/arm.c static unsigned long system_supported_vcpu_features(void) · line 422

Quoted 13 lines
arch/s390/kvm/arm64/arm.c +13 -0
+int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
+{
+	DECLARE_KERNEL_FPU_ONSTACK32(fpu_save);
+	struct kvm_run *kvm_run = vcpu->run;
+	int ret;
+
+	if (kvm_run->exit_reason == KVM_EXIT_MMIO) {
+		ret = kvm_handle_mmio_return(vcpu);
+		if (ret <= 0)
+			return ret;
+	}
+
+	vcpu_load(vcpu);

[Severity: Critical]
Could this corrupt the per-CPU preempt notifiers hlist?

The generic KVM ioctl handler in virt/kvm/kvm_main.c already calls vcpu_load() before invoking kvm_arch_vcpu_ioctl_run(). Calling it a second time here registers the preempt notifier twice, which appears to create a self-referential cycle in the hlist and could cause a kernel panic during scheduling.

[ ... ]

Quoted 21 lines
arch/s390/kvm/arm64/arm.c +21 -0
+	do {
+		if (signal_pending(current)) {
+			kvm_run->exit_reason = KVM_EXIT_INTR;
+			ret = -EINTR;
+			break;
+		}
+
+		if (need_resched())
+			schedule();
+
+		if (ret > 0)
+			ret = check_vcpu_requests(vcpu);
+
+		vcpu->arch.sae_block.icptr = 0;
+
+		smp_store_mb(vcpu->mode, IN_GUEST_MODE);
+
+		if (kvm_request_pending(vcpu)) {
+			vcpu->mode = OUTSIDE_GUEST_MODE;
+			continue;
+		}

arch/s390/kvm/arm64/arm.c static unsigned long system_supported_vcpu_features(void) · line 469

Quoted 10 lines
arch/s390/kvm/arm64/arm.c +10 -0
+
+		kvm_vcpu_srcu_read_unlock(vcpu);
+
+		arm_vcpu_run(vcpu);
+
+		vcpu->mode = OUTSIDE_GUEST_MODE;
+
+		kvm_vcpu_srcu_read_lock(vcpu);
+
+		ret = handle_exit(vcpu);

[Severity: Low]
Is the return value from check_vcpu_requests() intentionally ignored?

The result is assigned to ret, but there is no check to break out of the loop if it returns an error. The value is then overwritten by the call to handle_exit(). If check_vcpu_requests() ever returns an error code, it appears it would be silently discarded.

[Severity: High]
Is there a race condition here when checking requests with host interrupts enabled?

The vCPU mode transitions to IN_GUEST_MODE and requests are checked before interrupts are disabled in arm_vcpu_run(). Furthermore, arm_vcpu_run() re-enables interrupts just before the sae64a instruction:

arm_vcpu_run() {
    ...
    local_irq_disable();
    guest_timing_enter_irqoff();
    guest_state_enter_irqoff();
    local_irq_enable();

    sae_block->icptr = 0;

    sae64a(sae_block);
    ...
}

If an IPI arrives during this window after the request check but before sae64a executes, would the kick be lost and leave the vCPU in the guest without processing the request?

[ ... ]

Quoted 9 lines
arch/s390/kvm/arm64/arm.c +9 -0
+long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
+{
+	struct kvm_vcpu *vcpu = filp->private_data;
+	void __user *argp = (void __user *)arg;
+	struct kvm_device_attr attr;
+	int ret;
+
+	switch (ioctl) {
+	case KVM_ARM_VCPU_INIT: {

[ ... ]

Quoted 11 lines
arch/s390/kvm/arm64/arm.c +11 -0
+	case KVM_SET_ONE_REG:
+	case KVM_GET_ONE_REG: {
+		struct kvm_one_reg reg;
+
+		ret = -ENOEXEC;
+		if (unlikely(!kvm_vcpu_initialized(vcpu)))
+			break;
+
+		ret = -EFAULT;
+		if (copy_from_user(&reg, argp, sizeof(reg)))
+			break;

arch/s390/kvm/arm64/arm.c static unsigned long system_supported_vcpu_features(void) · line 567

Quoted 10 lines
arch/s390/kvm/arm64/arm.c +10 -0
+
+		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
+			kvm_reset_vcpu(vcpu);
+
+		if (ioctl == KVM_SET_ONE_REG)
+			ret = kvm_arm_set_reg(vcpu, &reg);
+		else
+			ret = kvm_arm_get_reg(vcpu, &reg);
+		break;
+	}

[Severity: High]
Are vcpu_load() and vcpu_put() required around these architectural ioctls?

The kvm_arch_vcpu_ioctl() function routes operations like KVM_SET_ONE_REG and KVM_GET_ONE_REG, but it appears to lack the necessary vcpu_load() and vcpu_put() bracketing.

Since generic KVM does not hold the vCPU lock when delegating to kvm_arch_vcpu_ioctl(), could this allow VCPU state to be modified without proper synchronization?

[ ... ]

arch/s390/kvm/arm64/arm.c static unsigned long system_supported_vcpu_features(void) · line 603

Quoted 12 lines
arch/s390/kvm/arm64/arm.c +12 -0
+	case KVM_ARM_VCPU_FINALIZE: {
+		int what;
+
+		if (!kvm_vcpu_initialized(vcpu))
+			return -ENOEXEC;
+
+		if (get_user(what, (const int __user *)argp))
+			return -EFAULT;
+
+		ret = kvm_arm_vcpu_finalize(vcpu, what);
+		break;
+	}

[Severity: High]
Does this code use the correct gate to reject finalization calls?

The operation is gated using !kvm_vcpu_initialized(), but it seems it should be rejected if the guest has already run. Should this be using vcpu_has_run_once(vcpu) instead?

Additionally, kvm_arm_vcpu_finalize() in arch/s390/kvm/arm64/reset.c unconditionally returns 0:

kvm_arm_vcpu_finalize() {
    return 0;
}

Could this cause KVM to silently accept any unsupported features requested by userspace?

Quoted 5 lines
arch/s390/kvm/arm64/handle_exit.c new file +0 -0
new file mode 100644
index 0000000000000..1286a64783e07

[ ... ]

Quoted 14 lines
arch/s390/kvm/arm64/handle_exit.c new file +14 -0
+int handle_exit(struct kvm_vcpu *vcpu)
+{
+	u8 icptr = vcpu->arch.sae_block.icptr;
+	int ret = 1;
+
+	switch (icptr) {
+	case SAE_ICPTR_SPURIOUS:
+		break;
+	case SAE_ICPTR_VALIDITY:
+		WARN_ONCE(true, "SAE: validity intercept. vir: 0x%04x",
+			  vcpu->arch.sae_block.vir);
+		ret = -EINVAL;
+		break;
+	case SAE_ICPTR_HOST_ACCESS_EXCEPTION:

arch/s390/kvm/arm64/handle_exit.c static exit_handle_fn arm_exit_handlers[] = { · line 87

Quoted 10 lines
arch/s390/kvm/arm64/handle_exit.c new file +10 -0
+	case SAE_ICPTR_SYNCHRONOUS_EXCEPTION:
+		ret = handle_trap_exceptions(vcpu);
+		break;
+	default:
+		WARN_ONCE(true, "SAE: unknown interception reason 0x%02x",
+			  icptr);
+		ret = -EINVAL;
+	}
+	return ret;
+}

[Severity: High]
Are these WARN_ONCE calls reachable by unprivileged userspace or guests?

If userspace injects a malformed SAE state or an unknown intercept code, it could trigger these warnings. Reaching a WARN_ONCE from guest-controlled or host-userspace-controlled state can lead to a Denial of Service via log flooding or a kernel panic if panic_on_warn is enabled.

Could these be changed to return an error or use pr_warn_once() instead?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
#71 lore raw

Re: [PATCH v7 22/23] KVM: s390: arm64: Implement basic page fault handler

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Unused local variable 'esr' and unused parameters 'slot' and 'hva'. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Unused local variable 'esr' and unused parameters 'slot' and 'hva'. --

commit 0b610e782223a98a4d9a93986b45a74687aeae46
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Implement basic page fault handler

This commit adds host functionality to page in guest memory. If the guest does something unexpected or illegal, it exits to userspace and aborts execution.

[ ... ]

Quoted 6 lines
arch/s390/kvm/arm64/mmu.c new file +0 -0
new file mode 100644
index 0000000000000..bf3442d3609b2
@@ -0,0 +1,177 @@

[ ... ]

arch/s390/kvm/arm64/mmu.c line 22

Quoted 9 lines
arch/s390/kvm/arm64/mmu.c new file +9 -0
+/*
+ * user_mem_abort() - handle a dat fault for the gmap of a vcpu
+ *
+ * Return: 0 on success, < 0 in case of error.
+ * Context: The mm lock must not be held before calling. May sleep.
+ */
+static int user_mem_abort(struct kvm_vcpu *vcpu, gpa_t fault_ipa,
+			  struct kvm_memory_slot *slot, hva_t hva)
+{

[Severity: Low]
Are the slot and hva parameters needed for user_mem_abort in arch/s390/kvm/arm64/mmu.c? They are passed into the function but never accessed inside the function body. Could they be removed to avoid unused parameter warnings?

Quoted 8 lines
arch/s390/kvm/arm64/mmu.c new file +8 -0
+	struct guest_fault f = { };
+	int ret;
+
+	if (kvm_s390_cur_gmap_fault_is_write(vcpu))
+		f.write_attempt = FOLL_WRITE;
+	f.gfn = gpa_to_gfn(fault_ipa);
+
+	ret = kvm_s390_faultin_gfn(vcpu, NULL, &f);

[ ... ]

Quoted 6 lines
arch/s390/kvm/arm64/mmu.c new file +6 -0
+int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
+{
+	struct kvm_memory_slot *memslot;
+	bool translation = false;
+	phys_addr_t fault_ipa;
+	unsigned long esr;

arch/s390/kvm/arm64/mmu.c line 86

Quoted 10 lines
arch/s390/kvm/arm64/mmu.c new file +10 -0
+	unsigned long hva;
+	bool write_fault;
+	bool guest_size_err;
+	bool writable;
+	bool is_iabt;
+	int ret;
+	gfn_t gfn;
+	int idx;
+
+	esr = kvm_vcpu_get_esr(vcpu);

[Severity: Low]
Is the esr variable needed here? It is assigned the result of kvm_vcpu_get_esr(vcpu) in kvm_handle_guest_abort(), but it is never evaluated subsequently in the function.

Quoted 3 lines
arch/s390/kvm/arm64/mmu.c new file +3 -0
+	fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
+	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
+	guest_size_err = vcpu->arch.sae_block.icptr == SAE_ICPTR_GUEST_ADDRESS_SIZE;

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
#73 lore raw

Re: [PATCH v7 23/23] KVM: s390: arm64: Add KVM_S390_ARM64 Kconfig and Makefile

In reply to Steffen Eiden
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Duplicate tracepoint registrations due to unmodified TRACE_SYSTEM in `virt/kvm/kvm_main.c`.…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Duplicate tracepoint registrations due to unmodified TRACE_SYSTEM in virt/kvm/kvm_main.c.
- [Low] The kvm-arm64 module will fail to build in modpost under all configurations (built-in or module) due to EXPORT_SYMBOL conflicts when compiling KVM core files twice. --

commit faac05d8e4c537c5a5862961cb0c8e0746920675
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Add KVM_S390_ARM64 Kconfig and Makefile

This commit adds Kbuild and Makefile configurations to build a second KVM module on s390 implementing the arm64-KVM API. To prevent symbol conflicts with kvm-s390, internal symbols in kvm-arm64 are mangled if compiled as built-in. It also overrides KBUILD_MODNAME to fix module parameter handling.

Quoted 5 lines
arch/s390/kvm/arm64/Makefile new file +0 -0
new file mode 100644
index 0000000000000..ae614a62f7b5f

[ ... ]

arch/s390/kvm/arm64/Makefile line 19

Quoted 10 lines
arch/s390/kvm/arm64/Makefile new file +10 -0
+kvm-arm64-obj := \
+	arm.o \
+	guest.o \
+	handle_exit.o \
+	inject_fault.o \
+	mmio.o \
+	mmu.o \
+	reset.o \
+
+kvm-arm64-obj += $(patsubst %.o,%-arm64.o,$(kvm-y))

[Severity: Medium]
Does compiling KVM core files a second time cause duplicate tracepoint registrations?

When virt/kvm/kvm_main.c is compiled again for the kvm-arm64 module, it instantiates the tracepoint metadata for CREATE_TRACE_POINTS. The linker will aggregate the .ftrace_events and __tracepoints_ptrs sections, causing the trace subsystem to attempt registering identical sets of tracepoints under the same system and name. This will fail with -EEXIST at boot and leave the tracepoints broken for the new module.

arch/s390/kvm/arm64/Makefile line 30

Quoted 11 lines
arch/s390/kvm/arm64/Makefile new file +11 -0
+kvm-arm64-obj += $(patsubst %.o,%-arm64.o,$(gmap-y))
+
+targets += $(kvm-arm64-obj)
+
+$(obj)/%-arm64.o: $(src)/%.c FORCE
+	@mkdir -p $(dir $@)
+	$(call if_changed_rule,cc_o_c)
+
+ifeq ($(CONFIG_KVM_S390_ARM64),m)
+
+kvm-arm64-y = $(kvm-arm64-obj)

[Severity: Low]
Will this fail to build in modpost when compiled as a module?

When CONFIG_KVM_S390_ARM64=m, Kbuild processes the EXPORT_SYMBOL macros natively without symbol mangling. Since kvm-arm64.ko and kvm.ko will both natively export identical KVM core symbols (such as kvm_init), modpost will fail with duplicate export errors.

Quoted 2 lines
arch/s390/kvm/arm64/Makefile new file +2 -0
+
+else ifeq ($(CONFIG_KVM_S390_ARM64),y)

[ ... ]

arch/s390/kvm/arm64/Makefile line 81

Quoted 7 lines
arch/s390/kvm/arm64/Makefile new file +7 -0
+define rule_oc_o_o
+	$(call cmd_and_savecmd,objcopy)
+	$(call cmd,gen_objtooldep)
+	$(call cmd,gen_symversions_o)
+endef
+
+OBJCOPYFLAGS_kvm-namespaced.o := -O $(LD_BFD) --localize-symbols=$(obj)/kvm_symbol_list

[Severity: Low]
Will this localization step cause modpost to fail on built-in code?

When compiled with CONFIG_KVM_S390_ARM64=y, objcopy --localize-symbols localizes all defined symbols. However, modpost strictly rejects exporting local symbols via __ksymtab entries, which results in errors like
fatal: local symbol 'kvm_init' was exported.

Quoted 8 lines

+$(obj)/kvm-namespaced.o: $(obj)/kvm-unnamespaced.o $(obj)/kvm_symbol_list FORCE

arch/s390/kvm/arm64/Makefile new file +7 -0
+	$(call if_changed_rule,oc_o_o)
+
+kvm-arm64-y = kvm-namespaced.o
+
+targets += kvm-unnamespaced.o kvm_symbol_list kvm-namespaced.o
+
+endif
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
maintainer
#42 lore raw

Re: [PATCH v7 11/23] KVM: arm64: Share arm64 code with s390

In reply to Steffen Eiden
Quoted 103 lines · Steffen Eiden

Mark functions that s390 can use to implement arm on s390 as shared functions.

No functional change.

---
 arch/arm64/kvm/arm.c         | 3 +++
 arch/arm64/kvm/guest.c       | 6 ++++++
 arch/arm64/kvm/handle_exit.c | 6 ++++++
 arch/arm64/kvm/mmio.c        | 2 ++
 4 files changed, 17 insertions(+)
arch/arm64/kvm/arm.c +3 -0
index 8b080804bc90..6b92a3c1d490 100644
@@ -1603,6 +1603,7 @@ static unsigned long system_supported_vcpu_features(void)
 	return features;
 }
 
+#ifdef ARM64_S390_COMMON
 static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
 					const struct kvm_vcpu_init *init)
 {
@@ -1656,6 +1657,8 @@ static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
 			     KVM_VCPU_MAX_FEATURES);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static int kvm_setup_vcpu(struct kvm_vcpu *vcpu)
 {
 	struct kvm *kvm = vcpu->kvm;
arch/arm64/kvm/guest.c +6 -0
index 773f6c8e5026..6ca5a9f357cd 100644
@@ -62,6 +62,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {
 		       sizeof(kvm_vcpu_stats_desc),
 };
 
+#ifdef ARM64_S390_COMMON
 static bool core_reg_offset_is_vreg(u64 off)
 {
 	return off >= KVM_REG_ARM_CORE_REG(fp_regs.vregs) &&
@@ -306,6 +307,8 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	return err;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 #define vq_word(vq) (((vq) - SVE_VQ_MIN) / 64)
 #define vq_mask(vq) ((u64)1 << ((vq) - SVE_VQ_MIN) % 64)
 #define vq_present(vqs, vq) (!!((vqs)[vq_word(vq)] & vq_mask(vq)))
@@ -543,6 +546,7 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
 	return -EINVAL;
 }
 
+#ifdef ARM64_S390_COMMON
 static int copy_core_reg_indices(const struct kvm_vcpu *vcpu,
 				 u64 __user *uindices)
 {
@@ -591,6 +595,8 @@ static unsigned long num_core_regs(const struct kvm_vcpu *vcpu)
 	return copy_core_reg_indices(vcpu, NULL);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static unsigned long num_sve_regs(const struct kvm_vcpu *vcpu)
 {
 	const unsigned int slices = vcpu_sve_slices(vcpu);
arch/arm64/kvm/handle_exit.c +4 -0
index db37678dcb05..6e59a7b12d40 100644
@@ -213,6 +213,7 @@ static int kvm_handle_guest_debug(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
+#ifdef ARM64_S390_COMMON
 static int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu)
 {
 	u64 esr = kvm_vcpu_get_esr(vcpu);
@@ -224,6 +225,8 @@ static int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * Guest access to SVE registers should be routed to this handler only
  * when the system doesn't support SVE.
@@ -404,6 +407,7 @@ static exit_handle_fn arm_exit_handlers[] = {
 	[ESR_ELx_EC_GCS]	= kvm_handle_gcs,
 };
 
+#ifdef ARM64_S390_COMMON
 static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu)
 {
 	u64 esr = kvm_vcpu_get_esr(vcpu);

arch/arm64/kvm/handle_exit.c static int handle_trap_exceptions(struct kvm_vcpu *vcpu) · line 446

Quoted 4 lines
arch/arm64/kvm/handle_exit.c +1 -0
@@ -439,6 +443,8 @@ static int handle_trap_exceptions(struct kvm_vcpu *vcpu)
 	return handled;
 }
 
+#endif /* ARM64_S390_COMMON */

Why the exit handler, and not the EC handlers aside from the UNKNOWN one? Yes, you probably aren't there yet in this series, but I can tell you are going to add more and more of these.

But the EC array is absolutely architectural, and there is no good reason to maintain your own.

So instead of this, why not keep the EC array altogether, and implement stubs for the ECs you don't support?

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
author reviewer
#43 lore raw

Re: [PATCH v7 11/23] KVM: arm64: Share arm64 code with s390

Quoted 114 lines · Marc Zyngier
Quoted 103 lines · Steffen Eiden

Mark functions that s390 can use to implement arm on s390 as shared functions.

No functional change.

---
 arch/arm64/kvm/arm.c         | 3 +++
 arch/arm64/kvm/guest.c       | 6 ++++++
 arch/arm64/kvm/handle_exit.c | 6 ++++++
 arch/arm64/kvm/mmio.c        | 2 ++
 4 files changed, 17 insertions(+)
arch/arm64/kvm/arm.c +3 -0
index 8b080804bc90..6b92a3c1d490 100644
@@ -1603,6 +1603,7 @@ static unsigned long system_supported_vcpu_features(void)
 	return features;
 }
 
+#ifdef ARM64_S390_COMMON
 static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
 					const struct kvm_vcpu_init *init)
 {
@@ -1656,6 +1657,8 @@ static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
 			     KVM_VCPU_MAX_FEATURES);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static int kvm_setup_vcpu(struct kvm_vcpu *vcpu)
 {
 	struct kvm *kvm = vcpu->kvm;
arch/arm64/kvm/guest.c +6 -0
index 773f6c8e5026..6ca5a9f357cd 100644
@@ -62,6 +62,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {
 		       sizeof(kvm_vcpu_stats_desc),
 };
 
+#ifdef ARM64_S390_COMMON
 static bool core_reg_offset_is_vreg(u64 off)
 {
 	return off >= KVM_REG_ARM_CORE_REG(fp_regs.vregs) &&
@@ -306,6 +307,8 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	return err;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 #define vq_word(vq) (((vq) - SVE_VQ_MIN) / 64)
 #define vq_mask(vq) ((u64)1 << ((vq) - SVE_VQ_MIN) % 64)
 #define vq_present(vqs, vq) (!!((vqs)[vq_word(vq)] & vq_mask(vq)))
@@ -543,6 +546,7 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
 	return -EINVAL;
 }
 
+#ifdef ARM64_S390_COMMON
 static int copy_core_reg_indices(const struct kvm_vcpu *vcpu,
 				 u64 __user *uindices)
 {
@@ -591,6 +595,8 @@ static unsigned long num_core_regs(const struct kvm_vcpu *vcpu)
 	return copy_core_reg_indices(vcpu, NULL);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static unsigned long num_sve_regs(const struct kvm_vcpu *vcpu)
 {
 	const unsigned int slices = vcpu_sve_slices(vcpu);
arch/arm64/kvm/handle_exit.c +4 -0
index db37678dcb05..6e59a7b12d40 100644
@@ -213,6 +213,7 @@ static int kvm_handle_guest_debug(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
+#ifdef ARM64_S390_COMMON
 static int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu)
 {
 	u64 esr = kvm_vcpu_get_esr(vcpu);
@@ -224,6 +225,8 @@ static int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * Guest access to SVE registers should be routed to this handler only
  * when the system doesn't support SVE.
@@ -404,6 +407,7 @@ static exit_handle_fn arm_exit_handlers[] = {
 	[ESR_ELx_EC_GCS]	= kvm_handle_gcs,
 };
 
+#ifdef ARM64_S390_COMMON
 static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu)
 {
 	u64 esr = kvm_vcpu_get_esr(vcpu);

arch/arm64/kvm/handle_exit.c static int handle_trap_exceptions(struct kvm_vcpu *vcpu) · line 446

Quoted 4 lines
arch/arm64/kvm/handle_exit.c +1 -0
@@ -439,6 +443,8 @@ static int handle_trap_exceptions(struct kvm_vcpu *vcpu)
 	return handled;
 }
 
+#endif /* ARM64_S390_COMMON */

Why the exit handler, and not the EC handlers aside from the UNKNOWN one? Yes, you probably aren't there yet in this series, but I can tell you are going to add more and more of these.

Quoted 8 lines

But the EC array is absolutely architectural, and there is no good reason to maintain your own.

So instead of this, why not keep the EC array altogether, and implement stubs for the ECs you don't support?

	M.

There was no specific reasson to define our own EC array as far as I remember. I will try reusing the array and find out if there are any issues with that. But I do not expect any.

I assume the stubs probably just forward to handle_unknown_ec, or do you have a different suggestion?

Steffen

#20 lore raw

Re: [PATCH v7 03/23] KVM: Allow KVM implementations to switch off MMIO independent of Kconfig

In reply to Steffen Eiden
Quoted 4 lines · Steffen Eiden

Only defining KVM_MMIO is not flexible enough for multi-KVM systems where one KVM implements MMIO but others do not. Solve this by additionally testing for the non-existence of KVM_NO_MMIO before including MMIO code to KVM.

Ewwwww. IIUC, s390 ends up with two KVM modules, i.e. there really are two different KVMs being built. Rather than have a KVM_MMIO and NO_KVM_MMIO, I vote very strongly to drop CONFIG_KVM_MMIO entirely and replace it with __KVM_HAVE_ARCH_MMIO, a la __KVM_HAVE_ARCH_VM_FREE and others. Tracking two separate macros is going to be annoying to maintain, and will definitely be confusing for non-s390 people.

It'll mean linking in an emtpy coalesced_mmio.o on s390 when building without arm64 support, but I'm guessing that won't be at all common for s390 builds.

#5 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

In reply to Steffen Eiden
Quoted 5 lines · Steffen Eiden

Allow KVM implementations to choose alternative device names. This is especially useful for architectures providing multiple KVM implementations simultaneously. Architectures providing multiple KVM implementations must compile the KVM common code once per implementation and mange symbols.

What about tracepoints? Or do those show up as "kvm" and "kvm-arm64"?

maintainer
#44 lore raw

Re: [PATCH v7 11/23] KVM: arm64: Share arm64 code with s390

In reply to Steffen Eiden
Quoted 15 lines · Steffen Eiden
Quoted 4 lines · Marc Zyngier

Why the exit handler, and not the EC handlers aside from the UNKNOWN one? Yes, you probably aren't there yet in this series, but I can tell you are going to add more and more of these.

Quoted 8 lines

But the EC array is absolutely architectural, and there is no good reason to maintain your own.

So instead of this, why not keep the EC array altogether, and implement stubs for the ECs you don't support?

	M.
Quoted 6 lines

There was no specific reasson to define our own EC array as far as I remember. I will try reusing the array and find out if there are any issues with that. But I do not expect any.

I assume the stubs probably just forward to handle_unknown_ec, or do you have a different suggestion?

Probably. This is an indication of something being deeply wrong in the implementation (HW or SW). On reflection, kvm_handle_unknown_ec() should result in a KVM_BUG().

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
author reviewer
#6 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

In reply to Sean Christopherson
Quoted 8 lines · Sean Christopherson
Quoted 5 lines · Steffen Eiden

Allow KVM implementations to choose alternative device names. This is especially useful for architectures providing multiple KVM implementations simultaneously. Architectures providing multiple KVM implementations must compile the KVM common code once per implementation and mange symbols.

What about tracepoints? Or do those show up as "kvm" and "kvm-arm64"?

Yes, I want them to show up as kvm and kvm-arm64.

Thanks for pointing that out - I just noticed that I forgot to switch the trace system to kvm-arm64 for the common tracepoints in trace/events/kvm.h
I only did it for the arch-local traces in PATCH 21.

I would just do the following:

include/trace/events/kvm.h +4 -0
index b282e3a86769..5d4f8a0693a3 100644
@@ -5,7 +5,11 @@
 #include <linux/tracepoint.h>
 
 #undef TRACE_SYSTEM
+#ifdef KVM_S390_ARM64
+#define TRACE_SYSTEM kvm-arm64
+#else
 #define TRACE_SYSTEM kvm
+#endif /* KVM_S390_ARM64 */
 
 #define ERSN(x) { KVM_EXIT_##x, "KVM_EXIT_" #x }
 

This would leak a bit of arm on s390 into common KVM but I do not see another way.

KVM_S390_ARM64 is defined for all arm-on-s390 compile units.

I think I'll add this in PATCH 14? of this series or create a separate PATCH.

Does this make sense?

Steffen

author reviewer
#21 lore raw

Re: [PATCH v7 03/23] KVM: Allow KVM implementations to switch off MMIO independent of Kconfig

In reply to Sean Christopherson
Quoted 11 lines · Sean Christopherson
Quoted 4 lines · Steffen Eiden

Only defining KVM_MMIO is not flexible enough for multi-KVM systems where one KVM implements MMIO but others do not. Solve this by additionally testing for the non-existence of KVM_NO_MMIO before including MMIO code to KVM.

Ewwwww. IIUC, s390 ends up with two KVM modules, i.e. there really are two different KVMs being built. Rather than have a KVM_MMIO and NO_KVM_MMIO, I vote very strongly to drop CONFIG_KVM_MMIO entirely and replace it with __KVM_HAVE_ARCH_MMIO, a la __KVM_HAVE_ARCH_VM_FREE and others. Tracking two separate macros is going to be annoying to maintain, and will definitely be confusing for non-s390 people.

You understand correclty we compile kvm twice into different modules.

I already did this approach (with a differnt name) and dropped the config in (v1,v2,v3, and) v4 of this Series[1] and Marc voted against this :)

I have no strong opinion on this and do whatever the both of you come up with :)

Quoted 3 lines

It'll mean linking in an emtpy coalesced_mmio.o on s390 when building without arm64 support, but I'm guessing that won't be at all common for s390 builds.

This should be no problem, I assume.

Steffen

[1] https://lore.kernel.org/all/[email protected]/

maintainer
#22 lore raw

Re: [PATCH v7 03/23] KVM: Allow KVM implementations to switch off MMIO independent of Kconfig

In reply to Sean Christopherson
Quoted 7 lines · Sean Christopherson
Quoted 4 lines · Steffen Eiden

Only defining KVM_MMIO is not flexible enough for multi-KVM systems where one KVM implements MMIO but others do not. Solve this by additionally testing for the non-existence of KVM_NO_MMIO before including MMIO code to KVM.

Quoted 9 lines

Ewwwww. IIUC, s390 ends up with two KVM modules, i.e. there really are two different KVMs being built. Rather than have a KVM_MMIO and NO_KVM_MMIO, I vote very strongly to drop CONFIG_KVM_MMIO entirely and replace it with __KVM_HAVE_ARCH_MMIO, a la __KVM_HAVE_ARCH_VM_FREE and others. Tracking two separate macros is going to be annoying to maintain, and will definitely be confusing for non-s390 people.

It'll mean linking in an emtpy coalesced_mmio.o on s390 when building without arm64 support, but I'm guessing that won't be at all common for s390 builds.

I ended-up doing something very similar locally, because I couldn't really make head or tail of the logic while being in holiday mode (which doesn't help). However, I kept the negative logic, so that we don't need to add anything to any architecture but S390 (they are the odd one out here).

Untested hack follows.

	M.
arch/arm64/kvm/Kconfig +0 -1
index 449154f9a4852..dd138b730217e 100644
@@ -22,7 +22,6 @@ menuconfig KVM
 	select KVM_COMMON
 	select KVM_GENERIC_HARDWARE_ENABLING
 	select HAVE_KVM_CPU_RELAX_INTERCEPT
-	select KVM_MMIO
 	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
 	select VIRT_XFER_TO_GUEST_WORK
 	select KVM_VFIO
arch/loongarch/kvm/Kconfig +0 -1
index 15da2d88c0c10..f03872ba0ba8c 100644
@@ -28,7 +28,6 @@ config KVM
 	select KVM_COMMON
 	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
 	select KVM_GENERIC_HARDWARE_ENABLING
-	select KVM_MMIO
 	select VIRT_XFER_TO_GUEST_WORK
 	select SCHED_INFO
 	select GUEST_PERF_EVENTS if PERF_EVENTS
arch/mips/kvm/Kconfig +0 -1
index b1b9a1d677581..c65596fdf54f4 100644
@@ -22,7 +22,6 @@ config KVM
 	select EXPORT_UASM
 	select KVM_COMMON
 	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
-	select KVM_MMIO
 	select KVM_GENERIC_HARDWARE_ENABLING
 	select HAVE_KVM_READONLY_MEM
 	help
arch/powerpc/kvm/Kconfig +0 -4
index b6bc2fc86dca6..c5565d6fb27e4 100644
@@ -30,7 +30,6 @@ config KVM_BOOK3S_HANDLER
 config KVM_BOOK3S_32_HANDLER
 	bool
 	select KVM_BOOK3S_HANDLER
-	select KVM_MMIO
 
 config KVM_BOOK3S_64_HANDLER
 	bool
@@ -38,7 +37,6 @@ config KVM_BOOK3S_64_HANDLER
 
 config KVM_BOOK3S_PR_POSSIBLE
 	bool
-	select KVM_MMIO
 
 config KVM_BOOK3S_HV_POSSIBLE
 	bool
@@ -201,7 +199,6 @@ config KVM_E500V2
 	depends on PPC_E500 && !PPC_E500MC
 	depends on !CONTEXT_TRACKING_USER
 	select KVM
-	select KVM_MMIO
 	help
 	  Support running unmodified E500 guest kernels in virtual machines on
 	  E500v2 host processors.
@@ -216,7 +213,6 @@ config KVM_E500MC
 	depends on PPC_E500MC
 	depends on !CONTEXT_TRACKING_USER
 	select KVM
-	select KVM_MMIO
 	select KVM_BOOKE_HV
 	help
 	  Support running unmodified E500MC/E5500/E6500 guest kernels in
arch/riscv/kvm/Kconfig +0 -1
index ec2cee0a39e01..3aea8d4939d2e 100644
@@ -28,7 +28,6 @@ config KVM
 	select KVM_COMMON
 	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
 	select KVM_GENERIC_HARDWARE_ENABLING
-	select KVM_MMIO
 	select VIRT_XFER_TO_GUEST_WORK
 	select SCHED_INFO
 	select GUEST_PERF_EVENTS if PERF_EVENTS
arch/s390/include/asm/kvm_host_s390.h +2 -0
index cd692f8fb7643..3c65c52d6682b 100644
@@ -34,6 +34,8 @@
 
 #define KVM_S390_MANAGES_S390_GUEST 1
 
+#define __KVM_NO_MMIO
+
 /*
  * These seem to be used for allocating ->chip in the routing table, which we
  * don't use. 1 is as small as we can get to reduce the needed memory. If we
arch/x86/kvm/Kconfig +0 -1
index 538ed1e803326..48ca567b0a846 100644
@@ -32,7 +32,6 @@ config KVM_X86
 	select VHOST_TASK
 	select KVM_ASYNC_PF
 	select USER_RETURN_NOTIFIER
-	select KVM_MMIO
 	select SCHED_INFO
 	select PERF_EVENTS
 	select GUEST_PERF_EVENTS
include/linux/kvm_host.h +1 -1
index 03bfc92864b6e..dc9b68793f0d7 100644
@@ -833,7 +833,7 @@ struct kvm {
 	struct kvm_vm_stat stat;
 	struct kvm_arch arch;
 	refcount_t users_count;
-#ifdef CONFIG_KVM_MMIO
+#ifndef __KVM_NO_MMIO
 	struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
 	spinlock_t ring_lock;
 	struct list_head coalesced_zones;
virt/kvm/Kconfig +0 -3
index c3c0ee253fc77..c012f30127e38 100644
@@ -40,9 +40,6 @@ config NEED_KVM_DIRTY_RING_WITH_BITMAP
 	bool
 	depends on HAVE_KVM_DIRTY_RING
 
-config KVM_MMIO
-       bool
-
 config KVM_ASYNC_PF
        bool
 
virt/kvm/Makefile.kvm +1 -2
index d047d4cf58c9f..c4fb1e01d0c89 100644
@@ -5,9 +5,8 @@
 
 KVM ?= ../../../virt/kvm
 
-kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o
+kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/coalesced_mmio.o $(KVM)/binary_stats.o
 kvm-$(CONFIG_KVM_VFIO) += $(KVM)/vfio.o
-kvm-$(CONFIG_KVM_MMIO) += $(KVM)/coalesced_mmio.o
 kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
 kvm-$(CONFIG_HAVE_KVM_IRQ_ROUTING) += $(KVM)/irqchip.o
 kvm-$(CONFIG_HAVE_KVM_DIRTY_RING) += $(KVM)/dirty_ring.o
virt/kvm/coalesced_mmio.c +3 -0
index 6b1d901610998..6bd4157b3ef41 100644
@@ -15,6 +15,8 @@
 #include <linux/slab.h>
 #include <linux/kvm.h>
 
+#ifndef __KVM_NO_MMIO
+
 #include "coalesced_mmio.h"
 
 static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev)
@@ -188,3 +190,4 @@ int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
 	 */
 	return 0;
 }
+#endif
virt/kvm/coalesced_mmio.h +1 -1
index 36f84264ed257..77b811dee27c2 100644
@@ -11,7 +11,7 @@
  *
  */
 
-#ifdef CONFIG_KVM_MMIO
+#ifndef __KVM_NO_MMIO
 
 #include <linux/list.h>
 
virt/kvm/kvm_main.c +4 -4
index 65eb26a0520d8..2f25b75dbfd44 100644
@@ -4059,7 +4059,7 @@ static vm_fault_t kvm_vcpu_fault(struct vm_fault *vmf)
 	else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
 		page = virt_to_page(vcpu->arch.pio_data);
 #endif
-#ifdef CONFIG_KVM_MMIO
+#ifndef __KVM_NO_MMIO
 	else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
 		page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
 #endif
@@ -4900,7 +4900,7 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 	case KVM_CAP_ENABLE_CAP_VM:
 	case KVM_CAP_HALT_POLL:
 		return 1;
-#ifdef CONFIG_KVM_MMIO
+#ifndef __KVM_NO_MMIO
 	case KVM_CAP_COALESCED_MMIO:
 		return KVM_COALESCED_MMIO_PAGE_OFFSET;
 	case KVM_CAP_COALESCED_PIO:
@@ -5241,7 +5241,7 @@ static long kvm_vm_ioctl(struct file *filp,
 		break;
 	}
 #endif
-#ifdef CONFIG_KVM_MMIO
+#ifndef __KVM_NO_MMIO
 	case KVM_REGISTER_COALESCED_MMIO: {
 		struct kvm_coalesced_mmio_zone zone;
 
@@ -5563,7 +5563,7 @@ static long kvm_dev_ioctl(struct file *filp,
 #ifdef CONFIG_X86
 		r += PAGE_SIZE;    /* pio data page */
 #endif
-#ifdef CONFIG_KVM_MMIO
+#ifndef __KVM_NO_MMIO
 		r += PAGE_SIZE;    /* coalesced mmio ring page */
 #endif
 		break;
Signature · 1 line
Jazz isn't dead. It just smells funny.
author reviewer
#23 lore raw

Re: [PATCH v7 03/23] KVM: Allow KVM implementations to switch off MMIO independent of Kconfig

Quoted 19 lines · Marc Zyngier
Quoted 7 lines · Sean Christopherson
Quoted 4 lines · Steffen Eiden

Only defining KVM_MMIO is not flexible enough for multi-KVM systems where one KVM implements MMIO but others do not. Solve this by additionally testing for the non-existence of KVM_NO_MMIO before including MMIO code to KVM.

Quoted 9 lines

Ewwwww. IIUC, s390 ends up with two KVM modules, i.e. there really are two different KVMs being built. Rather than have a KVM_MMIO and NO_KVM_MMIO, I vote very strongly to drop CONFIG_KVM_MMIO entirely and replace it with __KVM_HAVE_ARCH_MMIO, a la __KVM_HAVE_ARCH_VM_FREE and others. Tracking two separate macros is going to be annoying to maintain, and will definitely be confusing for non-s390 people.

It'll mean linking in an emtpy coalesced_mmio.o on s390 when building without arm64 support, but I'm guessing that won't be at all common for s390 builds.

Quoted 5 lines

I ended-up doing something very similar locally, because I couldn't really make head or tail of the logic while being in holiday mode (which doesn't help). However, I kept the negative logic, so that we don't need to add anything to any architecture but S390 (they are the odd one out here).

negative makes sense.
IIRC KVM/power does select KVM_MMIO most of the times but not always. I'll check that.

Quoted 4 lines

Untested hack follows.

	M.

Thanks!
I'll verify and integrate it

Steffen

...

author reviewer
#45 lore raw

Re: [PATCH v7 11/23] KVM: arm64: Share arm64 code with s390

In reply to Marc Zyngier
Quoted 21 lines · Marc Zyngier
Quoted 19 lines · Steffen Eiden
Quoted 4 lines · Marc Zyngier

Why the exit handler, and not the EC handlers aside from the UNKNOWN one? Yes, you probably aren't there yet in this series, but I can tell you are going to add more and more of these.

Quoted 8 lines

But the EC array is absolutely architectural, and there is no good reason to maintain your own.

So instead of this, why not keep the EC array altogether, and implement stubs for the ECs you don't support?

	M.

There was no specific reasson to define our own EC array as far as I remember. I will try reusing the array and find out if there are any issues with that. But I do not expect any.

Quoted 11 lines
Quoted 2 lines

I assume the stubs probably just forward to handle_unknown_ec, or do you have a different suggestion?

Probably. This is an indication of something being deeply wrong in the implementation (HW or SW). On reflection, kvm_handle_unknown_ec() should result in a KVM_BUG().

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.

While I tried to implement the change I noticed that it might be a good idea to share most if not all EC functions defined in handle_exit.c The handling is architectural as well and s390 and arm do (and should) not differ in their implementation.

But switching stubs to a real shared implementation is not possible if we want to not mix up changes in arm64 and s390 in one patch. I.e. in one patch expand the shared region and remove the stub. But we would neet to do that in one so the (s390) kernel keeps compiling.

So my suggestion is:

For series 1 (and 2) s390 has its own, local implementation of handle_exit without sharing any code with arm64 in regard to EC

When s390 has all the relevant code available (likely series 3) s390 can switch to using most (if not all) EC handling code used by arm_exit_handlers implemented in handle_exit.c.

This can be done without a mixed patch:

1. Mark arm64 handle_exit.c as shared (roughly the first 400Loc w/o includes) - s390 will not use it after this patch.
2. remove the handle exit code from s390's handle_exit.c and include the new generated handle_exit.inc

This approach brings us further into the share architecture not only code approach you meantrioned in the comments regarding sys-regs. (I or Andreas will provide a solution for this later :) )

Is that OK with you, what do you think?

Steffen

#24 lore raw

Re: [PATCH v7 03/23] KVM: Allow KVM implementations to switch off MMIO independent of Kconfig

In reply to Steffen Eiden
Quoted 20 lines · Steffen Eiden
Quoted 19 lines · Marc Zyngier
Quoted 16 lines · Sean Christopherson
Quoted 4 lines · Steffen Eiden

Only defining KVM_MMIO is not flexible enough for multi-KVM systems where one KVM implements MMIO but others do not. Solve this by additionally testing for the non-existence of KVM_NO_MMIO before including MMIO code to KVM.

Ewwwww. IIUC, s390 ends up with two KVM modules, i.e. there really are two different KVMs being built. Rather than have a KVM_MMIO and NO_KVM_MMIO, I vote very strongly to drop CONFIG_KVM_MMIO entirely and replace it with __KVM_HAVE_ARCH_MMIO, a la __KVM_HAVE_ARCH_VM_FREE and others. Tracking two separate macros is going to be annoying to maintain, and will definitely be confusing for non-s390 people.

It'll mean linking in an emtpy coalesced_mmio.o on s390 when building without arm64 support, but I'm guessing that won't be at all common for s390 builds.

Quoted 6 lines
Quoted 5 lines

I ended-up doing something very similar locally, because I couldn't really make head or tail of the logic while being in holiday mode (which doesn't help). However, I kept the negative logic, so that we don't need to add anything to any architecture but S390 (they are the odd one out here).

negative makes sense.

Ya, an opt-out for s390 works for me.

Quoted 2 lines

IIRC KVM/power does select KVM_MMIO most of the times but not always. I'll check that.

Huh, indeed. KVM_BOOK3S_64_HV=y && KVM_BOOK3S_64_PR=n ends up with KVM_MMIO=n. Though I'm not surprised given the multitude of variants PPC has.

#25 lore raw

Re: [PATCH v7 03/23] KVM: Allow KVM implementations to switch off MMIO independent of Kconfig

Quoted 21 lines · Sean Christopherson
Quoted 20 lines · Steffen Eiden
Quoted 19 lines · Marc Zyngier
Quoted 16 lines · Sean Christopherson
Quoted 4 lines · Steffen Eiden

Only defining KVM_MMIO is not flexible enough for multi-KVM systems where one KVM implements MMIO but others do not. Solve this by additionally testing for the non-existence of KVM_NO_MMIO before including MMIO code to KVM.

Ewwwww. IIUC, s390 ends up with two KVM modules, i.e. there really are two different KVMs being built. Rather than have a KVM_MMIO and NO_KVM_MMIO, I vote very strongly to drop CONFIG_KVM_MMIO entirely and replace it with __KVM_HAVE_ARCH_MMIO, a la __KVM_HAVE_ARCH_VM_FREE and others. Tracking two separate macros is going to be annoying to maintain, and will definitely be confusing for non-s390 people.

It'll mean linking in an emtpy coalesced_mmio.o on s390 when building without arm64 support, but I'm guessing that won't be at all common for s390 builds.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines

I ended-up doing something very similar locally, because I couldn't really make head or tail of the logic while being in holiday mode (which doesn't help). However, I kept the negative logic, so that we don't need to add anything to any architecture but S390 (they are the odd one out here).

negative makes sense.

Ya, an opt-out for s390 works for me.

Actually, I have a better idea. I'll explain more in the subthread about tracepoints.

#7 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

In reply to Steffen Eiden
Quoted 24 lines · Steffen Eiden
Quoted 8 lines · Sean Christopherson
Quoted 5 lines · Steffen Eiden

Allow KVM implementations to choose alternative device names. This is especially useful for architectures providing multiple KVM implementations simultaneously. Architectures providing multiple KVM implementations must compile the KVM common code once per implementation and mange symbols.

What about tracepoints? Or do those show up as "kvm" and "kvm-arm64"?

Yes, I want them to show up as kvm and kvm-arm64.

Thanks for pointing that out - I just noticed that I forgot to switch the trace system to kvm-arm64 for the common tracepoints in trace/events/kvm.h
I only did it for the arch-local traces in PATCH 21.

I would just do the following:

include/trace/events/kvm.h +0 -0
index b282e3a86769..5d4f8a0693a3 100644

include/trace/events/kvm.h line 8

Quoted 4 lines
include/trace/events/kvm.h +1 -0
@@ -5,7 +5,11 @@
 #include <linux/tracepoint.h>
 
 #undef TRACE_SYSTEM
+#ifdef KVM_S390_ARM64

Side topic, I recommend choosing a macro name that doesn't have a near-collision with CONFIG_KVM_S390_ARM64. This *looks* like a typo, i.e. it looks like you forgot the CONFIG_ prefix. Especially since the macro is defined in the Makefile and won't show up with e.g. "git grep -w KVM_S390_ARM64". E.g. KVM_S390_BUILD_ARM64 or something?

Side topic #2, this entire approach seems extremely brittle unless you make it all but impossible for non-KVM code to get at KVM structure definitions. Outside of KVM, all compilation units will see the s390 version of KVM structures. Which is "fine", but obviously dangerous and IMO asking for maintenance issues down the road.

Quoted 10 lines
include/trace/events/kvm.h +3 -0
+#define TRACE_SYSTEM kvm-arm64
+#else
 #define TRACE_SYSTEM kvm
+#endif /* KVM_S390_ARM64 */
 
 #define ERSN(x) { KVM_EXIT_##x, "KVM_EXIT_" #x }
 

This would leak a bit of arm on s390 into common KVM but I do not see another way.

Morpheus: Stop trying to use macros, and use macros!

The most annoying thing is that macro shenanigans don't play well with hyphens, but that can be handled either by using a different macro for the trace name, or by creating /dev/kvm_arm64 instead of /dev/kvm-arm64. My vote would be to have the device be /dev/kvm_arm64, assuming that doesn't cause problems elsewhere.

And taking things a few steps further, we can solve the MMIO issue in a more elegant way, and eliminate the runtime string building in this patch (after looking more closely, that code needs to be jettisoned no matter what, there's simply no reason to specify the names at runtime since they're separate compilation units).

Rather than splatter #defines throughout header files, deal with the bulk of the pain in Makefile.kvm. By feeding conditionals into Makefile.kvm, the s390+arm64 build can easily omit coalesced_mmio.o and async_pf.o, define __KVM_HAVE_ARCH_MMIO programatically without having to change other architectures, and solve the naming stuff.

Full diff relative to this series at the bottom. Compile tested only (all architectures, though I certainly didn't get every combination of PPC insanity). Oh, and I also created an empty include/trace/events/kvm_arm64.h.

virt/kvm/Makefile.kvm

KVM ?= ../../../virt/kvm

  KVM_WANT_MMIO ?= $(CONFIG_KVM_MMIO)
  KVM_WANT_ASYNC_PF ?= $(CONFIG_KVM_ASYNC_PF)

KVM_DEV_NAME ?= kvm
KVM_DEV_MINOR ?= KVM_MINOR

  ccflags-y += -DKVM_DEV_NAME=$(KVM_DEV_NAME) -DKVM_DEV_MINOR=$(KVM_DEV_MINOR)
  
  kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o
  kvm-$(CONFIG_KVM_VFIO) += $(KVM)/vfio.o
  ifeq ($(KVM_WANT_MMIO),y)
  ccflags-y += -D__KVM_HAVE_ARCH_MMIO
  kvm-$(CONFIG_KVM_MMIO) += $(KVM)/coalesced_mmio.o
  endif
  ifeq ($(KVM_WANT_ASYNC_PF),y)
  kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
  endif
  kvm-$(CONFIG_HAVE_KVM_IRQ_ROUTING) += $(KVM)/irqchip.o
  kvm-$(CONFIG_HAVE_KVM_DIRTY_RING) += $(KVM)/dirty_ring.o
  kvm-$(CONFIG_HAVE_KVM_PFNCACHE) += $(KVM)/pfncache.o
  kvm-$(CONFIG_KVM_GUEST_MEMFD) += $(KVM)/guest_memfd.o

arch/s390/kvm/s390/Makefile:

  KVM := ../../../../virt/kvm
  KVM_WANT_MMIO := n
  include $(srctree)/virt/kvm/Makefile.kvm
  include $(srctree)/arch/s390/kvm/gmap/Makefile
  
  ccflags-y += -I$(src) -I$(srctree)/arch/s390/kvm/gmap
  
  kvm-y += s390.o intercept.o interrupt.o priv.o sigp.o
  kvm-y += diag.o gaccess.o guestdbg.o vsie.o pv.o
  kvm-y += $(gmap-y)
  
  kvm-$(CONFIG_VFIO_PCI_ZDEV_KVM) += pci.o
  obj-$(CONFIG_KVM) += kvm.o

arch/s390/kvm/arm64/Makefile (partial):

KVM := ../../../../virt/kvm
KVM_WANT_ASYNC_PF := n
KVM_DEV_NAME := kvm_arm64
KVM_DEV_MINOR := MISC_DYNAMIC_MINOR

  include $(srctree)/virt/kvm/Makefile.kvm
  include $(srctree)/arch/s390/kvm/gmap/Makefile
  include $(src)/Makefile.gen
  
  ccflags-y += -I $(src) -I $(objtree)/$(obj) -I$(srctree)/arch/s390/kvm/gmap -DKVM_S390_BUILD_ARM64

And the full diff (it's big because I literally reverted this patch):

arch/s390/include/asm/kvm.h +1 -1
index aeb11dc631d5..ae9b7509a3c1 100644
@@ -1,5 +1,5 @@
 /* SPDX-License-Identifier: GPL-2.0 */
-#ifdef KVM_S390_ARM64
+#ifdef KVM_S390_BUILD_ARM64
 #include <uapi/arm64/kvm.h>
 #else
 #include <uapi/asm/kvm.h>
arch/s390/include/asm/kvm_host.h +1 -1
index cd6d572883ab..2206c5791373 100644
@@ -3,7 +3,7 @@
 #ifndef ASM_KVM_HOST_H
 #define ASM_KVM_HOST_H
 
-#ifdef KVM_S390_ARM64
+#ifdef KVM_S390_BUILD_ARM64
 #include <asm/kvm_host_arm64.h>
 #else
 #include <asm/kvm_host_s390.h>
arch/s390/include/asm/kvm_host_s390.h +0 -1
index 207f44cb86d9..cd692f8fb764 100644
@@ -29,7 +29,6 @@
 
 #define KVM_HAVE_MMU_RWLOCK
 #define KVM_MAX_VCPUS 255
-#define KVM_NO_MMIO
 
 #define KVM_INTERNAL_MEM_SLOTS 1
 
arch/s390/include/asm/kvm_host_types.h +2 -2
index d308739ebcf5..39da4ac5407c 100644
@@ -3,10 +3,10 @@
 #ifndef ASM_KVM_HOST_TYPES_H
 #define ASM_KVM_HOST_TYPES_H
 
-#ifdef KVM_S390_ARM64
+#ifdef KVM_S390_BUILD_ARM64
 #include <asm/kvm_host_arm64_types.h>
 #else
 #include <asm/kvm_host_s390_types.h>
-#endif /* KVM_S390_ARM64 */
+#endif /* KVM_S390_BUILD_ARM64 */
 
 #endif /* ASM_KVM_HOST_TYPES_H */
arch/s390/kvm/arm64/Makefile +5 -1
index ae614a62f7b5..1d744d4dfc06 100644
@@ -1,11 +1,15 @@
 # SPDX-License-Identifier: GPL-2.0
 
 KVM := ../../../../virt/kvm
+KVM_WANT_ASYNC_PF := n
+KVM_DEV_NAME := kvm_arm64
+KVM_DEV_MINOR := MISC_DYNAMIC_MINOR
+
 include $(srctree)/virt/kvm/Makefile.kvm
 include $(srctree)/arch/s390/kvm/gmap/Makefile
 include $(src)/Makefile.gen
 
-ccflags-y += -I $(src) -I $(objtree)/$(obj) -I$(srctree)/arch/s390/kvm/gmap -DKVM_S390_ARM64
+ccflags-y += -I $(src) -I $(objtree)/$(obj) -I$(srctree)/arch/s390/kvm/gmap -DKVM_S390_BUILD_ARM64
 
 kvm-arm64-obj := \
 	arm.o \
arch/s390/kvm/arm64/arm.c +1 -2
index 3caa72f780a5..7cf62fa7b77b 100644
@@ -745,8 +745,7 @@ static int __init kvm_s390_arm64_init(void)
 	if (!sclp.has_aef)
 		return -ENXIO;
 
-	return kvm_init_with_dev(sizeof(struct kvm_vcpu), 0, THIS_MODULE,
-				 KVM_DEV_NAME, MISC_DYNAMIC_MINOR);
+	return kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
 }
 
 static __exit void kvm_s390_arm64_exit(void)
arch/s390/kvm/arm64/arm.h +10 -11
index 093a638e2cf9..e176bf403850 100644
@@ -5,18 +5,17 @@
 #include <linux/compiler_attributes.h>
 #include <linux/kvm_host.h>
 
-#define KVM_DEV_NAME "kvm-arm64"
+#define VM_EVENT(d_kvm, d_loglevel, d_string, d_args...)	\
+	debug_sprintf_event((d_kvm)->arch.dbf, d_loglevel,	\
+			    __stringify(KVM_DEV_NAME) ": " d_string "\n", d_args)
 
-#define VM_EVENT(d_kvm, d_loglevel, d_string, d_args...) \
-	debug_sprintf_event((d_kvm)->arch.dbf, d_loglevel, KVM_DEV_NAME ": " d_string "\n", d_args)
-
-#define VCPU_EVENT(d_vcpu, d_loglevel, d_string, d_args...)			\
-	do {									\
-		debug_sprintf_event(						\
-			(d_vcpu)->kvm->arch.dbf, d_loglevel,			\
-			KVM_DEV_NAME " %02d[%016llx-%016llx]: " d_string "\n",	\
-			(d_vcpu)->vcpu_id, (d_vcpu)->arch.sae_block.pstate,	\
-			(d_vcpu)->arch.sae_block.pc, d_args);			\
+#define VCPU_EVENT(d_vcpu, d_loglevel, d_string, d_args...)					\
+	do {											\
+		debug_sprintf_event(								\
+			(d_vcpu)->kvm->arch.dbf, d_loglevel,					\
+			__stringify(KVM_DEV_NAME) " %02d[%016llx-%016llx]: " d_string "\n",	\
+			(d_vcpu)->vcpu_id, (d_vcpu)->arch.sae_block.pstate,			\
+			(d_vcpu)->arch.sae_block.pc, d_args);					\
 	} while (0)
 
 static __always_inline int kvm_is_ucontrol(struct kvm *kvm)
arch/s390/kvm/gmap/faultin.c +1 -1
index 2e93bd563d5c..f2d3779db2d4 100644
@@ -10,7 +10,7 @@
 
 #include "gmap.h"
 #include "faultin.h"
-#ifdef KVM_S390_ARM64
+#ifdef KVM_S390_BUILD_ARM64
 #include "arm.h"
 #else
 #include "s390.h"
arch/s390/kvm/gmap/gmap.c +1 -1
index b7071f50f7ad..a75275be542e 100644
@@ -21,7 +21,7 @@
 
 #include "dat.h"
 #include "gmap.h"
-#ifdef KVM_S390_ARM64
+#ifdef KVM_S390_BUILD_ARM64
 #include "arm.h"
 #else
 #include "s390.h"
arch/s390/kvm/gmap/kvm_mmu.c +1 -1
index a26a16193467..5536ae279aad 100644
@@ -3,7 +3,7 @@
 #include <linux/kvm_types.h>
 #include <linux/kvm_host.h>
 
-#ifdef KVM_S390_ARM64
+#ifdef KVM_S390_BUILD_ARM64
 #include "arm.h"
 #else
 #include "s390.h"
arch/s390/kvm/gmap/trace_gmap.h +4 -6
index 431c9442b10c..6ac0d4961b96 100644
@@ -4,18 +4,16 @@
 
 #include <linux/tracepoint.h>
 
+#undef UNPACK_KVM
+#define UNPACK_KVM(name) name
 #undef TRACE_SYSTEM
-#ifdef KVM_S390_ARM64
-#define TRACE_SYSTEM kvm_arm64
-#else
-#define TRACE_SYSTEM kvm
-#endif
+#define TRACE_SYSTEM UNPACK_KVM(KVM_DEV_NAME)
 #undef TRACE_INCLUDE_PATH
 #define TRACE_INCLUDE_PATH ../gmap
 #undef TRACE_INCLUDE_FILE
 #define TRACE_INCLUDE_FILE trace_gmap
 
-#ifdef KVM_S390_ARM64
+#ifdef KVM_S390_BUILD_ARM64
 #define __KVM_FIELDS \
 	__field(unsigned long, pstate) \
 	__field(unsigned long, pc)
arch/s390/kvm/s390/Makefile +2 -1
index 762a63826423..56495321dbe8 100644
@@ -1,10 +1,11 @@
 # SPDX-License-Identifier: GPL-2.0
 
 KVM := ../../../../virt/kvm
+KVM_WANT_MMIO := n
 include $(srctree)/virt/kvm/Makefile.kvm
 include $(srctree)/arch/s390/kvm/gmap/Makefile
 
-ccflags-y := -I$(src) -I$(srctree)/arch/s390/kvm/gmap
+ccflags-y += -I$(src) -I$(srctree)/arch/s390/kvm/gmap
 
 kvm-y += s390.o intercept.o interrupt.o priv.o sigp.o
 kvm-y += diag.o gaccess.o guestdbg.o vsie.o pv.o
include/linux/kvm_host.h +3 -3
index ec294e419762..be76b79a1e26 100644
@@ -45,6 +45,8 @@
 #include <asm/kvm_host.h>
 #include <linux/kvm_dirty_ring.h>
 
+#define KVM_DEV_STRING(x) __stringify(KVM_DEV_NAME ## _ ## x)
+
 #ifndef KVM_MAX_VCPU_IDS
 #define KVM_MAX_VCPU_IDS KVM_MAX_VCPUS
 #endif
@@ -1082,9 +1084,7 @@ static inline void kvm_irqfd_exit(void)
 {
 }
 #endif
-int kvm_init(unsigned int vcpu_size, unsigned int vcpu_align, struct module *module);
-int kvm_init_with_dev(unsigned int vcpu_size, unsigned int vcpu_align,
-		      struct module *module, const char *dev_name, int minor);
+int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module);
 void kvm_exit(void);
 
 void kvm_get_kvm(struct kvm *kvm);
include/trace/events/kvm.h +3 -1
index b282e3a86769..23792ba49e83 100644
@@ -4,8 +4,10 @@
 
 #include <linux/tracepoint.h>
 
+#undef UNPACK_KVM
+#define UNPACK_KVM(name) name
 #undef TRACE_SYSTEM
-#define TRACE_SYSTEM kvm
+#define TRACE_SYSTEM UNPACK_KVM(KVM_DEV_NAME)
 
 #define ERSN(x) { KVM_EXIT_##x, "KVM_EXIT_" #x }
 
virt/kvm/Makefile.kvm +13 -0
index d047d4cf58c9..2b64e6745d79 100644
@@ -5,10 +5,23 @@
 
 KVM ?= ../../../virt/kvm
 
+KVM_WANT_MMIO ?= $(CONFIG_KVM_MMIO)
+KVM_WANT_ASYNC_PF ?= $(CONFIG_KVM_ASYNC_PF)
+
+KVM_DEV_NAME ?= kvm
+KVM_DEV_MINOR ?= KVM_MINOR
+
+ccflags-y += -DKVM_DEV_NAME=$(KVM_DEV_NAME) -DKVM_DEV_MINOR=$(KVM_DEV_MINOR)
+
 kvm-y := $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/binary_stats.o
 kvm-$(CONFIG_KVM_VFIO) += $(KVM)/vfio.o
+ifeq ($(KVM_WANT_MMIO),y)
+ccflags-y += -D__KVM_HAVE_ARCH_MMIO
 kvm-$(CONFIG_KVM_MMIO) += $(KVM)/coalesced_mmio.o
+endif
+ifeq ($(KVM_WANT_ASYNC_PF),y)
 kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
+endif
 kvm-$(CONFIG_HAVE_KVM_IRQ_ROUTING) += $(KVM)/irqchip.o
 kvm-$(CONFIG_HAVE_KVM_DIRTY_RING) += $(KVM)/dirty_ring.o
 kvm-$(CONFIG_HAVE_KVM_PFNCACHE) += $(KVM)/pfncache.o
virt/kvm/async_pf.c +2 -10
index 015761249107..de2d086d816f 100644
@@ -19,19 +19,11 @@
 
 static struct kmem_cache *async_pf_cache;
 
-int kvm_async_pf_init(const char *cache_name)
+int kvm_async_pf_init(void)
 {
-	char *full_cache_name;
-
-	full_cache_name = kasprintf(GFP_KERNEL, "%s_async_pf", cache_name);
-	if (!full_cache_name)
-		return -ENOMEM;
-
-	async_pf_cache = kmem_cache_create(full_cache_name,
+	async_pf_cache = kmem_cache_create(KVM_DEV_STRING(async_pf),
 					   sizeof(struct kvm_async_pf), 0,
 					   SLAB_ACCOUNT, NULL);
-	kfree(full_cache_name);
-
 	if (!async_pf_cache)
 		return -ENOMEM;
 
virt/kvm/async_pf.h +2 -2
index 4a7b81743248..90d1a7d8c6de 100644
@@ -12,11 +12,11 @@
 #define __KVM_ASYNC_PF_H__
 
 #ifdef CONFIG_KVM_ASYNC_PF
-int kvm_async_pf_init(const char *cache_name);
+int kvm_async_pf_init(void);
 void kvm_async_pf_deinit(void);
 void kvm_async_pf_vcpu_init(struct kvm_vcpu *vcpu);
 #else
-#define kvm_async_pf_init(_name) (0)
+#define kvm_async_pf_init() (0)
 #define kvm_async_pf_deinit() do {} while (0)
 #define kvm_async_pf_vcpu_init(C) do {} while (0)
 #endif
virt/kvm/coalesced_mmio.c +0 -4
index 8ff9d9a6f47a..6b1d90161099 100644
@@ -15,8 +15,6 @@
 #include <linux/slab.h>
 #include <linux/kvm.h>
 
-#ifndef KVM_NO_MMIO
-
 #include "coalesced_mmio.h"
 
 static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev)
@@ -190,5 +188,3 @@ int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
 	 */
 	return 0;
 }
-
-#endif /* ifndef KVM_NO_MMIO */
virt/kvm/coalesced_mmio.h +1 -1
index 826af421a5be..b7ff8b009412 100644
@@ -11,7 +11,7 @@
  *
  */
 
-#if defined(CONFIG_KVM_MMIO) && !defined(KVM_NO_MMIO)
+#ifdef __KVM_HAVE_ARCH_MMIO
 
 #include <linux/list.h>
 
virt/kvm/kvm_main.c +15 -37

135 lines not on this page · raw message

#74 lore raw

Re: [PATCH v7 23/23] KVM: s390: arm64: Add KVM_S390_ARM64 Kconfig and Makefile

In reply to Steffen Eiden

arch/s390/kvm/Kconfig config KVM_S390_UCONTROL · line 60

Quoted 13 lines · Steffen Eiden
arch/s390/kvm/Kconfig +5 -0
index 8d3ee17a1bcb..9c69ea16031e 100644
@@ -53,4 +53,33 @@ config KVM_S390_UCONTROL
 
 	  If unsure, say N.
 
+config KVM_S390_ARM64
+	def_tristate y
+	prompt "KVM support for hardware accelerated arm64 guests"
+	depends on HAS_IOMEM
+	depends on KVM

Is this actually necessary? I.e. does kvm.ko need to be *loaded* in order for kvm-arm64.ko to be loaded?

If kvm-arm64.ko does indeed have a hard dependency on kvm.ko, then I think it makes sense to not have redundant "select" statements below. As an outsider, it's super confusing because the KVM_S390_ARM64 are incomplete, e.g. are lacking things like VIRT_XFER_TO_GUEST_WORK (at least, I assume those are lacking), and makes it hard to see what is actually unique to kvm-arm64.ko.

If kvm-arm64.ko doesn't have a dependency on kvm.ko, e.g. to initialize hardware or something, then this "depends on" should go away.

author reviewer
#75 lore raw

Re: [PATCH v7 23/23] KVM: s390: arm64: Add KVM_S390_ARM64 Kconfig and Makefile

Quoted 18 lines · Sean Christopherson

arch/s390/kvm/Kconfig config KVM_S390_UCONTROL · line 60

Quoted 13 lines · Steffen Eiden
arch/s390/kvm/Kconfig +5 -0
index 8d3ee17a1bcb..9c69ea16031e 100644
@@ -53,4 +53,33 @@ config KVM_S390_UCONTROL
 
 	  If unsure, say N.
 
+config KVM_S390_ARM64
+	def_tristate y
+	prompt "KVM support for hardware accelerated arm64 guests"
+	depends on HAS_IOMEM
+	depends on KVM

Is this actually necessary? I.e. does kvm.ko need to be *loaded* in order for kvm-arm64.ko to be loaded?

Quoted 8 lines

If kvm-arm64.ko does indeed have a hard dependency on kvm.ko, then I think it makes sense to not have redundant "select" statements below. As an outsider, it's super confusing because the KVM_S390_ARM64 are incomplete, e.g. are lacking things like VIRT_XFER_TO_GUEST_WORK (at least, I assume those are lacking), and makes it hard to see what is actually unique to kvm-arm64.ko.

If kvm-arm64.ko doesn't have a dependency on kvm.ko, e.g. to initialize hardware or something, then this "depends on" should go away.

It is a bit more complicated unfortunately. KVM_S390_ARM64 depends on KVM as there is code around in the kernel & drivers (arch and common) which depends on CONFIG_KVM we need as well. I did not want to leak arch local configs to common code if I can avoid that.
I could change it to select KVM ? Do you have another idea on how to solve this issue?

The two modules are completely independent at compile and runtime. So kvm does not have to be loaded for kvm-arm64 to work.

VIRT_XFER_TO_GUEST_WORK is only lacking for now. I will add it later. Currently the kvm-arm64 module is incomplete anyways but I did not want to send a 60+patches series and rather stage the introduction over multiple series.

I will remove the duplicated config selections. Thanks for pointing it out. In previous series I had an explicit KVM_S390 config. Both KVM_S390 and KVM_ARM64 selected KVM. But this created too much churn around the whole tech-stack (default config change, s390 is the odd one out for the KVM config meaning, ...).

Steffen

author reviewer
#8 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

In reply to Sean Christopherson
Quoted 27 lines · Sean Christopherson
Quoted 26 lines · Steffen Eiden
Quoted 8 lines · Sean Christopherson
Quoted 5 lines · Steffen Eiden

Allow KVM implementations to choose alternative device names. This is especially useful for architectures providing multiple KVM implementations simultaneously. Architectures providing multiple KVM implementations must compile the KVM common code once per implementation and mange symbols.

What about tracepoints? Or do those show up as "kvm" and "kvm-arm64"?

Yes, I want them to show up as kvm and kvm-arm64.

Thanks for pointing that out - I just noticed that I forgot to switch the trace system to kvm-arm64 for the common tracepoints in trace/events/kvm.h
I only did it for the arch-local traces in PATCH 21.

I would just do the following:

include/trace/events/kvm.h +0 -0
index b282e3a86769..5d4f8a0693a3 100644
@@ -5,7 +5,11 @@
 #include <linux/tracepoint.h>
 
Quoted 8 lines

include/trace/events/kvm.h line 8

Quoted 2 lines
include/trace/events/kvm.h +1 -0
 #undef TRACE_SYSTEM
+#ifdef KVM_S390_ARM64

Side topic, I recommend choosing a macro name that doesn't have a near-collision with CONFIG_KVM_S390_ARM64. This *looks* like a typo, i.e. it looks like you forgot the CONFIG_ prefix. Especially since the macro is defined in the Makefile and won't show up with e.g. "git grep -w KVM_S390_ARM64". E.g. KVM_S390_BUILD_ARM64 or something?

Interesting. The name was deliberately chosen to be similar. But I see that it could be confusing.

I am not totally happy with KVM_S390_BUILD_ARM64 but I cannot find a better name either.

Quoted 20 lines

Side topic #2, this entire approach seems extremely brittle unless you make it all but impossible for non-KVM code to get at KVM structure definitions. Outside of KVM, all compilation units will see the s390 version of KVM structures. Which is "fine", but obviously dangerous and IMO asking for maintenance issues down the road.

Quoted 10 lines
include/trace/events/kvm.h +3 -0
+#define TRACE_SYSTEM kvm-arm64
+#else
 #define TRACE_SYSTEM kvm
+#endif /* KVM_S390_ARM64 */
 
 #define ERSN(x) { KVM_EXIT_##x, "KVM_EXIT_" #x }
 

This would leak a bit of arm on s390 into common KVM but I do not see another way.

Morpheus: Stop trying to use macros, and use macros!

Quoted 4 lines

The most annoying thing is that macro shenanigans don't play well with hyphens, but that can be handled either by using a different macro for the trace name, or by creating /dev/kvm_arm64 instead of /dev/kvm-arm64. My vote would be to have the device be /dev/kvm_arm64, assuming that doesn't cause problems elsewhere.

I am not aware of any problems that could cause. It was just a personal preference IIRC.

Quoted 10 lines

And taking things a few steps further, we can solve the MMIO issue in a more elegant way, and eliminate the runtime string building in this patch (after looking more closely, that code needs to be jettisoned no matter what, there's simply no reason to specify the names at runtime since they're separate compilation units).

Rather than splatter #defines throughout header files, deal with the bulk of the pain in Makefile.kvm. By feeding conditionals into Makefile.kvm, the s390+arm64 build can easily omit coalesced_mmio.o and async_pf.o, define __KVM_HAVE_ARCH_MMIO programatically without having to change other architectures, and solve the naming stuff.

Yes, this is a great idea. Thank you. I second you, this looks more clean and stable than the stuff we came up with :)

I'll integrate it into the series and send it with the next round.

Thank you for your input. Very much appreciated.

Steffen

...

maintainer
#68 lore raw

Re: [PATCH v7 21/23] KVM: s390: arm64: Implement vCPU IOCTLs

In reply to Steffen Eiden
Quoted 8 lines · Steffen Eiden

Implement all required vCPU (arch) IOCTLs.

---

[...]

arch/s390/kvm/arm64/arm.c static unsigned long system_supported_vcpu_features(void) · line 389

Quoted 12 lines
arch/s390/kvm/arm64/arm.c +12 -0
+static void arm_vcpu_run(struct kvm_vcpu *vcpu)
+{
+	struct kvm_sae_block *sae_block = &vcpu->arch.sae_block;
+
+	adjust_pc(vcpu);
+
+	local_irq_disable();
+	guest_timing_enter_irqoff();
+	guest_state_enter_irqoff();
+	local_irq_enable();
+
+	sae_block->icptr = 0;

We already do that in the function below, no?
If possible I'd like to avoid setting ictpr here.

Quoted 53 lines
arch/s390/kvm/arm64/arm.c +53 -0
+
+	sae64a(sae_block);
+
+	local_irq_disable();
+	guest_state_exit_irqoff();
+	guest_timing_exit_irqoff();
+	local_irq_enable();
+}
+
+/** kvm_arch_vcpu_ioctl_run() - run arm64 vCPU
+ *
+ * Execute arm64 guest instructions using SAE.
+ *
+ * Returns:
+ * 1 enter the guest (should not be observed by userspace)
+ * 0 exit to userspace
+ * < 0 exit to userspace, where the return value indicates n error
+ *
+ *
+ */
+int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
+{
+	DECLARE_KERNEL_FPU_ONSTACK32(fpu_save);
+	struct kvm_run *kvm_run = vcpu->run;
+	int ret;
+
+	if (kvm_run->exit_reason == KVM_EXIT_MMIO) {
+		ret = kvm_handle_mmio_return(vcpu);
+		if (ret <= 0)
+			return ret;
+	}
+
+	vcpu_load(vcpu);
+
+	kernel_fpu_begin(&fpu_save, KERNEL_FPC | KERNEL_VXR);
+	load_vx_regs((vcpu->arch.ctxt.vregs));
+
+	if (!vcpu->wants_to_run) {
+		ret = -EINTR;
+		goto out;
+	}
+
+	kvm_sigset_activate(vcpu);
+
+	might_fault();
+
+	kvm_vcpu_srcu_read_lock(vcpu);
+
+	ret = 1;
+	do {
+		if (signal_pending(current)) {
+			kvm_run->exit_reason = KVM_EXIT_INTR;
+			ret = -EINTR;

arch/s390/kvm/arm64/arm.c static unsigned long system_supported_vcpu_features(void) · line 452

Quoted 10 lines
arch/s390/kvm/arm64/arm.c +10 -0
+			break;
+		}
+
+		if (need_resched())
+			schedule();
+
+		if (ret > 0)
+			ret = check_vcpu_requests(vcpu);
+
+		vcpu->arch.sae_block.icptr = 0;

Here

Quoted 7 lines
arch/s390/kvm/arm64/arm.c +7 -0
+
+		smp_store_mb(vcpu->mode, IN_GUEST_MODE);
+
+		if (kvm_request_pending(vcpu)) {
+			vcpu->mode = OUTSIDE_GUEST_MODE;
+			continue;
+		}

arch/s390/kvm/arm64/arm.c static unsigned long system_supported_vcpu_features(void) · line 469

Quoted 10 lines
arch/s390/kvm/arm64/arm.c +10 -0
+
+		kvm_vcpu_srcu_read_unlock(vcpu);
+
+		arm_vcpu_run(vcpu);
+
+		vcpu->mode = OUTSIDE_GUEST_MODE;
+
+		kvm_vcpu_srcu_read_lock(vcpu);
+
+		ret = handle_exit(vcpu);

ARM increases stat.exits here and since you copied the stat fields over we could do that too.

General question to the ARM maintainers:
We'll be adding more stat fields in the very soon future.
Are you fine with us adding them under any name or should we prefix them so it's clear they exist because of our architecture / implementation and will likely never be added to ARM KVM?

In the past we've added a lot of stats and I'd expect us to at least add stats for every intercept reason.

Quoted 16 lines
arch/s390/kvm/arm64/arm.c +16 -0
+
+	} while (ret > 0);
+
+	kvm_vcpu_srcu_read_unlock(vcpu);
+
+	kvm_sigset_deactivate(vcpu);
+out:
+	if (unlikely(vcpu_get_flag(vcpu, INCREMENT_PC)))
+		adjust_pc(vcpu);
+
+	save_vx_regs(vcpu->arch.ctxt.vregs);
+	kernel_fpu_end(&fpu_save, KERNEL_FPC | KERNEL_VXR);
+	vcpu_put(vcpu);
+
+	return ret;
+}
#9 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

In reply to Steffen Eiden
Quoted 6 lines · Steffen Eiden
Quoted 5 lines · Sean Christopherson

And taking things a few steps further, we can solve the MMIO issue in a more elegant way, and eliminate the runtime string building in this patch (after looking more closely, that code needs to be jettisoned no matter what, there's simply no reason to specify the names at runtime since they're separate compilation units).

Quoted 8 lines
Quoted 5 lines

Rather than splatter #defines throughout header files, deal with the bulk of the pain in Makefile.kvm. By feeding conditionals into Makefile.kvm, the s390+arm64 build can easily omit coalesced_mmio.o and async_pf.o, define __KVM_HAVE_ARCH_MMIO programatically without having to change other architectures, and solve the naming stuff.

Yes, this is a great idea. Thank you. I second you, this looks more clean and stable than the stuff we came up with :)

Looking more at arch/s390/kvm/Kconfig and virt/kvm/Kconfig, we might need/want to to build out infrastructure to handle this sort of thing in a more generic fashion?

Which probably isn't that much infrastructure? It's more just changing how arch code communicates with common KVM? I.e. instead of providing boolean configs in virt/kvm/Kconfig, formalize communicating HAVE-type macros through the Makefile. That might even be a net positive in the long run, as it will make it easier to provide defaults for the common cases.

I say that because unless there's magic I'm unaware of these Kconfigs also needs to be configured per-KVM, not per-kernel:

- KVM_MMU_LOCKLESS_AGING, otherwise aging on arm64 will unintentionally be done outside of mmu_lock.

- KVM_GENERIC_PRE_FAULT_MEMORY, so that arm64 doesn't need to provide a stub for something it doesn't support

- HAVE_KVM_MSI, because presumably it's needed for arm64 support.

- HAVE_KVM_READONLY_MEM, same story as HAVE_KVM_MSI.

Which isn't _that_ many knobs, but add in KVM_MMIO, KVM_ASYNC_PF, and KVM_ASYNC_PF_SYNC, and it's enough that I think we should think about the big picture and not play whack-a-mole.

Actually, thinking about this more, what I've proposed here, plus the pattern of #define-ing macros in arch-specific kvm_host.h files, should suffice. For things like __KVM_HAVE_ARCH_VM_FREE, it absolutely makes sense to #define the macro in kvm_host.h since it's very directly tied to an arch callback. Whereas with KVM_MIO and KVM_ASYNC_PF, because they enable compilation of C files, it makes sense to define them in Makefiles.

So I think the "gap" is purely that the four knobs listed above need handling (and arguably KVM_MMU_LOCKLESS_AGING is ok as-proposed). But I do think we should try our best to be more thoughtful than we usually are when deciding how to enable common code, because obviously s390+arm64 is adding wrinkles no one has had to deal with before.

#10 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

Quoted 31 lines · Sean Christopherson
Quoted 6 lines · Steffen Eiden
Quoted 5 lines · Sean Christopherson

And taking things a few steps further, we can solve the MMIO issue in a more elegant way, and eliminate the runtime string building in this patch (after looking more closely, that code needs to be jettisoned no matter what, there's simply no reason to specify the names at runtime since they're separate compilation units).

Quoted 8 lines
Quoted 5 lines

Rather than splatter #defines throughout header files, deal with the bulk of the pain in Makefile.kvm. By feeding conditionals into Makefile.kvm, the s390+arm64 build can easily omit coalesced_mmio.o and async_pf.o, define __KVM_HAVE_ARCH_MMIO programatically without having to change other architectures, and solve the naming stuff.

Yes, this is a great idea. Thank you. I second you, this looks more clean and stable than the stuff we came up with :)

Looking more at arch/s390/kvm/Kconfig and virt/kvm/Kconfig, we might need/want to to build out infrastructure to handle this sort of thing in a more generic fashion?

Which probably isn't that much infrastructure? It's more just changing how arch code communicates with common KVM? I.e. instead of providing boolean configs in virt/kvm/Kconfig, formalize communicating HAVE-type macros through the Makefile. That might even be a net positive in the long run, as it will make it easier to provide defaults for the common cases.

I say that because unless there's magic I'm unaware of these Kconfigs also needs to be configured per-KVM, not per-kernel:

- KVM_MMU_LOCKLESS_AGING, otherwise aging on arm64 will unintentionally be done outside of mmu_lock.

Quoted 6 lines

- KVM_GENERIC_PRE_FAULT_MEMORY, so that arm64 doesn't need to provide a stub for something it doesn't support

- HAVE_KVM_MSI, because presumably it's needed for arm64 support.

- HAVE_KVM_READONLY_MEM, same story as HAVE_KVM_MSI.

And in the opposite direction, HAVE_KVM_VCPU_RUN_PID_CHANGE also falls into this category. That one is probably better handled as a #define in header files?

Quoted 17 lines

Which isn't _that_ many knobs, but add in KVM_MMIO, KVM_ASYNC_PF, and KVM_ASYNC_PF_SYNC, and it's enough that I think we should think about the big picture and not play whack-a-mole.

Actually, thinking about this more, what I've proposed here, plus the pattern of #define-ing macros in arch-specific kvm_host.h files, should suffice. For things like __KVM_HAVE_ARCH_VM_FREE, it absolutely makes sense to #define the macro in kvm_host.h since it's very directly tied to an arch callback. Whereas with KVM_MIO and KVM_ASYNC_PF, because they enable compilation of C files, it makes sense to define them in Makefiles.

So I think the "gap" is purely that the four knobs listed above need handling (and arguably KVM_MMU_LOCKLESS_AGING is ok as-proposed). But I do think we should try our best to be more thoughtful than we usually are when deciding how to enable common code, because obviously s390+arm64 is adding wrinkles no one has had to deal with before.

#76 lore raw

Re: [PATCH v7 23/23] KVM: s390: arm64: Add KVM_S390_ARM64 Kconfig and Makefile

In reply to Steffen Eiden
Quoted 29 lines · Steffen Eiden
Quoted 18 lines · Sean Christopherson

arch/s390/kvm/Kconfig config KVM_S390_UCONTROL · line 60

Quoted 13 lines · Steffen Eiden
arch/s390/kvm/Kconfig +5 -0
index 8d3ee17a1bcb..9c69ea16031e 100644
@@ -53,4 +53,33 @@ config KVM_S390_UCONTROL
 
 	  If unsure, say N.
 
+config KVM_S390_ARM64
+	def_tristate y
+	prompt "KVM support for hardware accelerated arm64 guests"
+	depends on HAS_IOMEM
+	depends on KVM

Is this actually necessary? I.e. does kvm.ko need to be *loaded* in order for kvm-arm64.ko to be loaded?

Quoted 8 lines

If kvm-arm64.ko does indeed have a hard dependency on kvm.ko, then I think it makes sense to not have redundant "select" statements below. As an outsider, it's super confusing because the KVM_S390_ARM64 are incomplete, e.g. are lacking things like VIRT_XFER_TO_GUEST_WORK (at least, I assume those are lacking), and makes it hard to see what is actually unique to kvm-arm64.ko.

If kvm-arm64.ko doesn't have a dependency on kvm.ko, e.g. to initialize hardware or something, then this "depends on" should go away.

Quoted 6 lines

It is a bit more complicated unfortunately. KVM_S390_ARM64 depends on KVM as there is code around in the kernel & drivers (arch and common) which depends on CONFIG_KVM we need as well. I did not want to leak arch local configs to common code if I can avoid that.
I could change it to select KVM ? Do you have another idea on how to solve this issue?

I would do something similar to what KVM x86 does.

Quoted 8 lines

The two modules are completely independent at compile and runtime. So kvm does not have to be loaded for kvm-arm64 to work.

VIRT_XFER_TO_GUEST_WORK is only lacking for now. I will add it later. Currently the kvm-arm64 module is incomplete anyways but I did not want to send a 60+patches series and rather stage the introduction over multiple series.

Quoted 4 lines

I will remove the duplicated config selections. Thanks for pointing it out. In previous series I had an explicit KVM_S390 config. Both KVM_S390 and KVM_ARM64 selected KVM. But this created too much churn around the whole tech-stack

LOL, what exactly are you expecting to happen? You're trying to squeeze support in for a completely different architecture, of course there's going to be churn.

This absolutely needs to be as precisely and correct as possible, otherwise it'll be painfully difficult to maintain, And to some extent, just for others to review. E.g. as is, it's at all not clear to me which of the IS_ENABLED(CONFIG_KVM) checks in arch/s390 apply to both flavors of virtualization, versus which are specific to "native" s390 virtualization.

I realize this is outside of my immediate scope, but getting this "right" isn't just an s390 thing, because these details bleed into common KVM, and even affect other architectures, e.g. when trying to make "treewide" KVM changes.

And FWIW, while it might seem daunting, from my perspective it's not actually that much churn to do things "right". Provide KVM_S390_NATIVE, and then have KVM reflect the "weakest" of S390_NATIVE vs. S390_ARCH. I.e. make KVM=m if either of the "real" KVMs will be a module. That requires some creative shenanigans, but it's not hard, just weird.

Quoted 1 line

(default config change, s390 is the odd one out for the KVM config meaning, ...).

Nah, x86 is the odd one, where CONFIG_KVM=m doesn't even guarantee kvm.ko gets built :-)

E.g. something like this, which probably doesn't compile and isn't the desired end state, as several of the Kconfig selections need to be reworked into #defines provided by header files, but AFAICT it does what I want/intend.

config KVM

	def_tristate m if (KVM_S390_NATIVE = m || KVM_S390_ARM64 = m)
	select HAVE_KVM_CPU_RELAX_INTERCEPT
	select KVM_COMMON
	select HAVE_KVM_IRQCHIP
	select HAVE_KVM_IRQ_ROUTING
	select KVM_VFIO
	select VIRT_XFER_TO_GUEST_WORK

config KVM_S390_NATIVE
def_tristate y
prompt "Kernel-based Virtual Machine (KVM) support" select KVM if (y && KVM_S390_ARM64 != m)
select KVM_ASYNC_PF
select KVM_ASYNC_PF_SYNC
select HAVE_KVM_NO_POLL
select KVM_MMU_LOCKLESS_AGING
select KVM_GENERIC_PRE_FAULT_MEMORY
select HAVE_KVM_INVALID_WAKEUPS
help
Support hosting paravirtualized guest machines using the SIE virtualization capability on the mainframe. This should work on any 64bit machine.

This module provides access to the hardware capabilities through a character device node named /dev/kvm.

To compile this as a module, choose M here: the module will be called kvm.

If unsure, say N.

config KVM_S390_UCONTROL
bool "Userspace controlled virtual machines"
depends on KVM
help
Allow CAP_SYS_ADMIN users to create KVM virtual machines that are controlled by userspace.

If unsure, say N.

config KVM_S390_ARM64
def_tristate y
prompt "KVM support for hardware accelerated arm64 guests" depends on HAS_IOMEM
select KVM if (y && KVM_S390_NATIVE != m)
select GUEST_PERF_EVENTS if PERF_EVENTS
select HAVE_KVM_VCPU_RUN_PID_CHANGE
select HAVE_KVM_IRQ_BYPASS
select KVM_MMIO
select SCHED_INFO
select XARRAY_MULTI
help
Enable support for hosting virtualized, hardware-accelerated arm64 virtual machines on s390 systems using the Start ARM Execution (SAE) instruction. This requires hardware models that support the Arm execution facility (AEF).

The module provides a character device named /dev/kvm-arm64 to expose the hardware capabilities.

To compile this driver as a module, choose M here. The module will be called kvm-arm64.

If unsure, say N.

#11 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

In reply to Sean Christopherson
Quoted 35 lines · Sean Christopherson
Quoted 34 lines · Sean Christopherson
Quoted 6 lines · Steffen Eiden
Quoted 5 lines · Sean Christopherson

And taking things a few steps further, we can solve the MMIO issue in a more elegant way, and eliminate the runtime string building in this patch (after looking more closely, that code needs to be jettisoned no matter what, there's simply no reason to specify the names at runtime since they're separate compilation units).

Quoted 8 lines
Quoted 5 lines

Rather than splatter #defines throughout header files, deal with the bulk of the pain in Makefile.kvm. By feeding conditionals into Makefile.kvm, the s390+arm64 build can easily omit coalesced_mmio.o and async_pf.o, define __KVM_HAVE_ARCH_MMIO programatically without having to change other architectures, and solve the naming stuff.

Yes, this is a great idea. Thank you. I second you, this looks more clean and stable than the stuff we came up with :)

Looking more at arch/s390/kvm/Kconfig and virt/kvm/Kconfig, we might need/want to to build out infrastructure to handle this sort of thing in a more generic fashion?

Which probably isn't that much infrastructure? It's more just changing how arch code communicates with common KVM? I.e. instead of providing boolean configs in virt/kvm/Kconfig, formalize communicating HAVE-type macros through the Makefile. That might even be a net positive in the long run, as it will make it easier to provide defaults for the common cases.

I say that because unless there's magic I'm unaware of these Kconfigs also needs to be configured per-KVM, not per-kernel:

- KVM_MMU_LOCKLESS_AGING, otherwise aging on arm64 will unintentionally be done outside of mmu_lock.

- KVM_GENERIC_PRE_FAULT_MEMORY, so that arm64 doesn't need to provide a stub for something it doesn't support

Quoted 6 lines
Quoted 3 lines

- HAVE_KVM_MSI, because presumably it's needed for arm64 support.

- HAVE_KVM_READONLY_MEM, same story as HAVE_KVM_MSI.

And in the opposite direction, HAVE_KVM_VCPU_RUN_PID_CHANGE also falls into this category. That one is probably better handled as a #define in header files?

Continuing the conversation with myself, add in HAVE_KVM_NO_POLL and HAVE_KVM_INVALID_WAKEUPS (which reminds me, valid_wakeup should really be moved into s390's kvm_vcpu_arch).

Quoted 17 lines
Quoted 17 lines

Which isn't _that_ many knobs, but add in KVM_MMIO, KVM_ASYNC_PF, and KVM_ASYNC_PF_SYNC, and it's enough that I think we should think about the big picture and not play whack-a-mole.

Actually, thinking about this more, what I've proposed here, plus the pattern of #define-ing macros in arch-specific kvm_host.h files, should suffice. For things like __KVM_HAVE_ARCH_VM_FREE, it absolutely makes sense to #define the macro in kvm_host.h since it's very directly tied to an arch callback. Whereas with KVM_MIO and KVM_ASYNC_PF, because they enable compilation of C files, it makes sense to define them in Makefiles.

So I think the "gap" is purely that the four knobs listed above need handling (and arguably KVM_MMU_LOCKLESS_AGING is ok as-proposed). But I do think we should try our best to be more thoughtful than we usually are when deciding how to enable common code, because obviously s390+arm64 is adding wrinkles no one has had to deal with before.

maintainer
#12 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

Quoted 38 lines · Sean Christopherson
Quoted 37 lines · Sean Christopherson
Quoted 36 lines · Sean Christopherson
Quoted 6 lines · Steffen Eiden
Quoted 5 lines · Sean Christopherson

And taking things a few steps further, we can solve the MMIO issue in a more elegant way, and eliminate the runtime string building in this patch (after looking more closely, that code needs to be jettisoned no matter what, there's simply no reason to specify the names at runtime since they're separate compilation units).

Quoted 8 lines
Quoted 5 lines

Rather than splatter #defines throughout header files, deal with the bulk of the pain in Makefile.kvm. By feeding conditionals into Makefile.kvm, the s390+arm64 build can easily omit coalesced_mmio.o and async_pf.o, define __KVM_HAVE_ARCH_MMIO programatically without having to change other architectures, and solve the naming stuff.

Yes, this is a great idea. Thank you. I second you, this looks more clean and stable than the stuff we came up with :)

Looking more at arch/s390/kvm/Kconfig and virt/kvm/Kconfig, we might need/want to to build out infrastructure to handle this sort of thing in a more generic fashion?

Which probably isn't that much infrastructure? It's more just changing how arch code communicates with common KVM? I.e. instead of providing boolean configs in virt/kvm/Kconfig, formalize communicating HAVE-type macros through the Makefile. That might even be a net positive in the long run, as it will make it easier to provide defaults for the common cases.

I say that because unless there's magic I'm unaware of these Kconfigs also needs to be configured per-KVM, not per-kernel:

- KVM_MMU_LOCKLESS_AGING, otherwise aging on arm64 will unintentionally be done outside of mmu_lock.

- KVM_GENERIC_PRE_FAULT_MEMORY, so that arm64 doesn't need to provide a stub for something it doesn't support

- HAVE_KVM_MSI, because presumably it's needed for arm64 support.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

- HAVE_KVM_READONLY_MEM, same story as HAVE_KVM_MSI.

And in the opposite direction, HAVE_KVM_VCPU_RUN_PID_CHANGE also falls into this category. That one is probably better handled as a #define in header files?

Continuing the conversation with myself, add in HAVE_KVM_NO_POLL and HAVE_KVM_INVALID_WAKEUPS (which reminds me, valid_wakeup should really be moved into s390's kvm_vcpu_arch).

Yes, these three can be handled in a similar way as this in arch/x86/include/asm/atomic64_64.h:

#define arch_atomic64_xchg arch_atomic64_xchg

and then a #ifndef replaces the "#ifdef CONFIG_*". Do this, and moving vcpu->valid_wakeup to vcpu->arch comes almost for free.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Actually, thinking about this more, what I've proposed here, plus the pattern of #define-ing macros in arch-specific kvm_host.h files, should suffice. For things like __KVM_HAVE_ARCH_VM_FREE, it absolutely makes sense to #define the macro in kvm_host.h since it's very directly tied to an arch callback. Whereas with KVM_MIO and KVM_ASYNC_PF, because they enable compilation of C files, it makes sense to define them in Makefiles.

Ugh, defining CONFIG symbols in Makefiles sucks. I don't want KVM to be the one that does things differently *once more*. I'd prefer to stub out the contents of the .c files instead.

Paolo

author reviewer
#77 lore raw

Re: [PATCH v7 23/23] KVM: s390: arm64: Add KVM_S390_ARM64 Kconfig and Makefile

In reply to Sean Christopherson
Quoted 61 lines · Sean Christopherson
Quoted 29 lines · Steffen Eiden
Quoted 18 lines · Sean Christopherson

arch/s390/kvm/Kconfig config KVM_S390_UCONTROL · line 60

Quoted 13 lines · Steffen Eiden
arch/s390/kvm/Kconfig +5 -0
index 8d3ee17a1bcb..9c69ea16031e 100644
@@ -53,4 +53,33 @@ config KVM_S390_UCONTROL
 
 	  If unsure, say N.
 
+config KVM_S390_ARM64
+	def_tristate y
+	prompt "KVM support for hardware accelerated arm64 guests"
+	depends on HAS_IOMEM
+	depends on KVM

Is this actually necessary? I.e. does kvm.ko need to be *loaded* in order for kvm-arm64.ko to be loaded?

Quoted 8 lines

If kvm-arm64.ko does indeed have a hard dependency on kvm.ko, then I think it makes sense to not have redundant "select" statements below. As an outsider, it's super confusing because the KVM_S390_ARM64 are incomplete, e.g. are lacking things like VIRT_XFER_TO_GUEST_WORK (at least, I assume those are lacking), and makes it hard to see what is actually unique to kvm-arm64.ko.

If kvm-arm64.ko doesn't have a dependency on kvm.ko, e.g. to initialize hardware or something, then this "depends on" should go away.

Quoted 6 lines

It is a bit more complicated unfortunately. KVM_S390_ARM64 depends on KVM as there is code around in the kernel & drivers (arch and common) which depends on CONFIG_KVM we need as well. I did not want to leak arch local configs to common code if I can avoid that.
I could change it to select KVM ? Do you have another idea on how to solve this issue?

I would do something similar to what KVM x86 does.

Quoted 8 lines

The two modules are completely independent at compile and runtime. So kvm does not have to be loaded for kvm-arm64 to work.

VIRT_XFER_TO_GUEST_WORK is only lacking for now. I will add it later. Currently the kvm-arm64 module is incomplete anyways but I did not want to send a 60+patches series and rather stage the introduction over multiple series.

Quoted 4 lines

I will remove the duplicated config selections. Thanks for pointing it out. In previous series I had an explicit KVM_S390 config. Both KVM_S390 and KVM_ARM64 selected KVM. But this created too much churn around the whole tech-stack

LOL, what exactly are you expecting to happen? You're trying to squeeze support in for a completely different architecture, of course there's going to be churn.

This absolutely needs to be as precisely and correct as possible, otherwise it'll be painfully difficult to maintain, And to some extent, just for others to review. E.g. as is, it's at all not clear to me which of the IS_ENABLED(CONFIG_KVM) checks in arch/s390 apply to both flavors of virtualization, versus which are specific to "native" s390 virtualization.

Quoted 3 lines

I realize this is outside of my immediate scope, but getting this "right" isn't just an s390 thing, because these details bleed into common KVM, and even affect other architectures, e.g. when trying to make "treewide" KVM changes.

I want to find the 'right' way as well - any ideas are welcome.

Quoted 6 lines

And FWIW, while it might seem daunting, from my perspective it's not actually that much churn to do things "right". Provide KVM_S390_NATIVE, and then have KVM reflect the "weakest" of S390_NATIVE vs. S390_ARCH. I.e. make KVM=m if either of the "real" KVMs will be a module. That requires some creative shenanigans, but it's not hard, just weird.

FYI the first versions of this series had a 3 configs approach very similar to yours.

IIRC it was not so much the churn we have in (upstream) kernel code but more on the distro side and to everyone building the kernel in their favourite architecture (s390 :))
Suddenly the KVM config changed its behaviour (effectively becoming a noop)

I am starting to think that having no separate config option for arm on s390 might be the easisest way. Just KVM and guard both modules behind it. It reduces the config space bloat and I do not see a reason why somoeone should only compile one KVM module but not the other. kvm-arm64 won't load if you do not have the hardware anyways.

But I am not opposed to the x86 approach. Just thinking loud.

Anyways, I am off for vacation expect a reduced reply frequency from my side :)

Steffen

Quoted 75 lines
Quoted 2 lines

(default config change, s390 is the odd one out for the KVM config meaning, ...).

Nah, x86 is the odd one, where CONFIG_KVM=m doesn't even guarantee kvm.ko gets built :-)

E.g. something like this, which probably doesn't compile and isn't the desired end state, as several of the Kconfig selections need to be reworked into #defines provided by header files, but AFAICT it does what I want/intend.

config KVM

	def_tristate m if (KVM_S390_NATIVE = m || KVM_S390_ARM64 = m)
	select HAVE_KVM_CPU_RELAX_INTERCEPT
	select KVM_COMMON
	select HAVE_KVM_IRQCHIP
	select HAVE_KVM_IRQ_ROUTING
	select KVM_VFIO
	select VIRT_XFER_TO_GUEST_WORK

config KVM_S390_NATIVE
def_tristate y
prompt "Kernel-based Virtual Machine (KVM) support" select KVM if (y && KVM_S390_ARM64 != m)
select KVM_ASYNC_PF
select KVM_ASYNC_PF_SYNC
select HAVE_KVM_NO_POLL
select KVM_MMU_LOCKLESS_AGING
select KVM_GENERIC_PRE_FAULT_MEMORY
select HAVE_KVM_INVALID_WAKEUPS
help
Support hosting paravirtualized guest machines using the SIE virtualization capability on the mainframe. This should work on any 64bit machine.

This module provides access to the hardware capabilities through a character device node named /dev/kvm.

To compile this as a module, choose M here: the module will be called kvm.

If unsure, say N.

config KVM_S390_UCONTROL
bool "Userspace controlled virtual machines"
depends on KVM
help
Allow CAP_SYS_ADMIN users to create KVM virtual machines that are controlled by userspace.

If unsure, say N.

config KVM_S390_ARM64
def_tristate y
prompt "KVM support for hardware accelerated arm64 guests" depends on HAS_IOMEM
select KVM if (y && KVM_S390_NATIVE != m)
select GUEST_PERF_EVENTS if PERF_EVENTS
select HAVE_KVM_VCPU_RUN_PID_CHANGE
select HAVE_KVM_IRQ_BYPASS
select KVM_MMIO
select SCHED_INFO
select XARRAY_MULTI
help
Enable support for hosting virtualized, hardware-accelerated arm64 virtual machines on s390 systems using the Start ARM Execution (SAE) instruction. This requires hardware models that support the Arm execution facility (AEF).

The module provides a character device named /dev/kvm-arm64 to expose the hardware capabilities.

To compile this driver as a module, choose M here. The module will be called kvm-arm64.

If unsure, say N.

#17 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

In reply to Steffen Eiden
Quoted 37 lines · Steffen Eiden
Quoted 27 lines · Sean Christopherson
Quoted 26 lines · Steffen Eiden
Quoted 8 lines · Sean Christopherson
Quoted 5 lines · Steffen Eiden

Allow KVM implementations to choose alternative device names. This is especially useful for architectures providing multiple KVM implementations simultaneously. Architectures providing multiple KVM implementations must compile the KVM common code once per implementation and mange symbols.

What about tracepoints? Or do those show up as "kvm" and "kvm-arm64"?

Yes, I want them to show up as kvm and kvm-arm64.

Thanks for pointing that out - I just noticed that I forgot to switch the trace system to kvm-arm64 for the common tracepoints in trace/events/kvm.h
I only did it for the arch-local traces in PATCH 21.

I would just do the following:

include/trace/events/kvm.h +0 -0
index b282e3a86769..5d4f8a0693a3 100644
@@ -5,7 +5,11 @@
 #include <linux/tracepoint.h>
 
Quoted 8 lines

include/trace/events/kvm.h line 8

Quoted 2 lines
include/trace/events/kvm.h +1 -0
 #undef TRACE_SYSTEM
+#ifdef KVM_S390_ARM64

Side topic, I recommend choosing a macro name that doesn't have a near-collision with CONFIG_KVM_S390_ARM64. This *looks* like a typo, i.e. it looks like you forgot the CONFIG_ prefix. Especially since the macro is defined in the Makefile and won't show up with e.g. "git grep -w KVM_S390_ARM64". E.g. KVM_S390_BUILD_ARM64 or something?

Quoted 5 lines

Interesting. The name was deliberately chosen to be similar. But I see that it could be confusing.

I am not totally happy with KVM_S390_BUILD_ARM64 but I cannot find a better name either.

Y'all need to pick *one* method. KVM_S390_MANAGES_S390_GUEST was already added by a07276d5d188 ("KVM: s390: gmap: Make storage keys optional"), and then this series effectively adds another macro to provide the inversion of that, except they're not even tied together.

#13 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Sean Christopherson
Quoted 6 lines
Quoted 6 lines

Actually, thinking about this more, what I've proposed here, plus the pattern of #define-ing macros in arch-specific kvm_host.h files, should suffice. For things like __KVM_HAVE_ARCH_VM_FREE, it absolutely makes sense to #define the macro in kvm_host.h since it's very directly tied to an arch callback. Whereas with KVM_MIO and KVM_ASYNC_PF, because they enable compilation of C files, it makes sense to define them in Makefiles.

Ugh, defining CONFIG symbols in Makefiles sucks.

Sure, but IMO wrapping entire files in an #ifdef that comes from an arch header sucks more. And technically, these aren't CONFIG symbols.

Quoted 1 line

I don't want KVM to be the. one that does things differently *once more*.

Too late :-D

arch/arm64/kvm/hyp/nvhe/Makefile:ccflags-y := -D__KVM_NVHE_HYPERVISOR__ -D__DISABLE_EXPORTS -D__DISABLE_TRACE_MMIO__ arch/arm64/kvm/hyp/vhe/Makefile:ccflags-y := -D__KVM_VHE_HYPERVISOR__

And I think there's value in deliberately being different, because CONFIGs are kernel-wide things, whereas the thingies in question are KVM-local macros. I.e. IMO, having the behavior stand out is a good thing.

Quoted 1 line

I'd prefer to stub out the contents of the .c files instead.

#78 lore raw

Re: [PATCH v7 23/23] KVM: s390: arm64: Add KVM_S390_ARM64 Kconfig and Makefile

In reply to Steffen Eiden
Quoted 7 lines · Steffen Eiden
Quoted 5 lines · Sean Christopherson

And FWIW, while it might seem daunting, from my perspective it's not actually that much churn to do things "right". Provide KVM_S390_NATIVE, and then have KVM reflect the "weakest" of S390_NATIVE vs. S390_ARCH. I.e. make KVM=m if either of the "real" KVMs will be a module. That requires some creative shenanigans, but it's not hard, just weird.

Quoted 6 lines

FYI the first versions of this series had a 3 configs approach very similar to yours.

IIRC it was not so much the churn we have in (upstream) kernel code but more on the distro side and to everyone building the kernel in their favourite architecture (s390 :))

How many people are running distro kernels on s390 hardware? And how many distros actually change the default KVM settings, e.g. to build KVM as a module instead of baking it into the kernel? My guess is "not many" and "almost none", i.e. the actual impact on downstream users is likely miniscule.

Quoted 1 line

Suddenly the KVM config changed its behaviour (effectively becoming a noop)

Not really, because "KVM" itself is inaccessible (well, unless someone is hand- editing .configs or generating them by script, but that's their own fault). E.g. upgrading to a new kernel will explicitly prompt the user to choose for both KVM_S390_NATIVE and KVM_S390_ARM64 (or whatever they get called).

Quoted 5 lines

I am starting to think that having no separate config option for arm on s390 might be the easisest way. Just KVM and guard both modules behind it. It reduces the config space bloat and I do not see a reason why somoeone should only compile one KVM module but not the other. kvm-arm64 won't load if you do not have the hardware anyways.

Because there may be an unforseen need down the road? Smushing things together after the fact is generally easy, pulling things apart without breaking everything is usually much, much harder.

Quoted 4 lines

But I am not opposed to the x86 approach. Just thinking loud.

Anyways, I am off for vacation expect a reduced reply frequency from my side :)

Enjoy your holiday!

maintainer
#79 lore raw

Re: [PATCH v7 23/23] KVM: s390: arm64: Add KVM_S390_ARM64 Kconfig and Makefile

Il gio 3 set 2026, 18:33 Sean Christopherson <[email protected]> ha scritto:

Quoted 5 lines

How many people are running distro kernels on s390 hardware? And how many distros actually change the default KVM settings, e.g. to build KVM as a module instead of baking it into the kernel? My guess is "not many" and "almost none", i.e. the actual impact on downstream users is likely miniscule.

RHEL and SLES *are* distros, and I am not sure which bank or insurance company would be running non-distro kernels. :) So I assume you mean "how many people are running non-distro kernels on s390".

At least RHEL builds KVM as a module for s390x, but it's not a problem. I agree with you that having the equivalent of INTEL/AMD, if possible, is a desirable self-check.

Paolo

maintainer
#14 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

In reply to Sean Christopherson
Quoted 9 lines · Sean Christopherson
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Sean Christopherson
Quoted 6 lines
Quoted 6 lines

Actually, thinking about this more, what I've proposed here, plus the pattern of #define-ing macros in arch-specific kvm_host.h files, should suffice. For things like __KVM_HAVE_ARCH_VM_FREE, it absolutely makes sense to #define the macro in kvm_host.h since it's very directly tied to an arch callback. Whereas with KVM_MIO and KVM_ASYNC_PF, because they enable compilation of C files, it makes sense to define them in Makefiles.

Quoted 4 lines
Quoted 1 line

Ugh, defining CONFIG symbols in Makefiles sucks.

Sure, but IMO wrapping entire files in an #ifdef that comes from an arch header sucks more. And technically, these aren't CONFIG symbols.

True it wouldn't be -DCONFIG_ (fortunately). But even if technically correct is the best kind of correct...

Quoted 6 lines
Quoted 1 line

I don't want KVM to be the. one that does things differently *once more*.

Too late :-D

arch/arm64/kvm/hyp/nvhe/Makefile:ccflags-y := -D__KVM_NVHE_HYPERVISOR__ -D__DISABLE_EXPORTS -D__DISABLE_TRACE_MMIO__ arch/arm64/kvm/hyp/vhe/Makefile:ccflags-y := -D__KVM_VHE_HYPERVISOR__

Sure, VHE/nVHE is fair game because the nVHE case is in the same boat as vDSO, arch/*/boot, and so on.

Looking at a grep for (cflags|CFLAGS).*\-D most are -DDEBUG, -DDISABLE_BRANCH_PROFILING, -DDEFAULT_SYMBOL_NAMESPACE, etc. and in most cases they seem like a bad idea. Don't do it for KVM please; requiring -D for all architectures and for random pieces of config, when you can put it in asm/kvm_host.h, is just as bad if not worse.

Paolo

Quoted 6 lines

And I think there's value in deliberately being different, because CONFIGs are kernel-wide things, whereas the thingies in question are KVM-local macros. I.e. IMO, having the behavior stand out is a good thing.

Quoted 1 line

I'd prefer to stub out the contents of the .c files instead.

#15 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Sean Christopherson
Quoted 1 line

I don't want KVM to be the. one that does things differently *once more*.

Too late :-D

arch/arm64/kvm/hyp/nvhe/Makefile:ccflags-y := -D__KVM_NVHE_HYPERVISOR__ -D__DISABLE_EXPORTS -D__DISABLE_TRACE_MMIO__ arch/arm64/kvm/hyp/vhe/Makefile:ccflags-y := -D__KVM_VHE_HYPERVISOR__

Quoted 8 lines

Sure, VHE/nVHE is fair game because the nVHE case is in the same boat as vDSO, arch/*/boot, and so on.

Looking at a grep for (cflags|CFLAGS).*\-D most are -DDEBUG, -DDISABLE_BRANCH_PROFILING, -DDEFAULT_SYMBOL_NAMESPACE, etc. and in most cases they seem like a bad idea. Don't do it for KVM please; requiring -D for all architectures and for random pieces of config, when you can put it in asm/kvm_host.h, is just as bad if not worse.

Boo, it's never fun when a parent shows up and takes away your toys :-D

maintainer
#46 lore raw

Re: [PATCH v7 11/23] KVM: arm64: Share arm64 code with s390

In reply to Steffen Eiden

On Wed, 02 Sep 2026 13:41:52 +0100,
Steffen Eiden <[email protected]> wrote:

[...]

Quoted 16 lines

So my suggestion is:

For series 1 (and 2) s390 has its own, local implementation of handle_exit without sharing any code with arm64 in regard to EC

When s390 has all the relevant code available (likely series 3) s390 can switch to using most (if not all) EC handling code used by arm_exit_handlers implemented in handle_exit.c.

This can be done without a mixed patch:

1. Mark arm64 handle_exit.c as shared (roughly the first 400Loc w/o includes) - s390 will not use it after this patch.
2. remove the handle exit code from s390's handle_exit.c and include the new generated handle_exit.inc

Quoted 5 lines

This approach brings us further into the share architecture not only code approach you meantrioned in the comments regarding sys-regs. (I or Andreas will provide a solution for this later :) )

Is that OK with you, what do you think?

I *think* that's OK. I just want to make sure that in the long run, we don't end-up with pointless divergence.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author reviewer
#16 lore raw

Re: [PATCH v7 02/23] KVM: Make device name configurable

In reply to Sean Christopherson
Quoted 12 lines · Sean Christopherson
Quoted 11 lines · Paolo Bonzini
Quoted 6 lines · Sean Christopherson
Quoted 1 line

I don't want KVM to be the. one that does things differently *once more*.

Too late :-D

arch/arm64/kvm/hyp/nvhe/Makefile:ccflags-y := -D__KVM_NVHE_HYPERVISOR__ -D__DISABLE_EXPORTS -D__DISABLE_TRACE_MMIO__ arch/arm64/kvm/hyp/vhe/Makefile:ccflags-y := -D__KVM_VHE_HYPERVISOR__

Sure, VHE/nVHE is fair game because the nVHE case is in the same boat as vDSO, arch/*/boot, and so on.

Quoted 7 lines
Quoted 5 lines

Looking at a grep for (cflags|CFLAGS).*\-D most are -DDEBUG, -DDISABLE_BRANCH_PROFILING, -DDEFAULT_SYMBOL_NAMESPACE, etc. and in most cases they seem like a bad idea. Don't do it for KVM please; requiring -D for all architectures and for random pieces of config, when you can put it in asm/kvm_host.h, is just as bad if not worse.

Boo, it's never fun when a parent shows up and takes away your toys :-D

:)

I will then create a new patch:

I will move config options to asm/kvm_host.h
It makes more sense to me as well to do this in the header.

Shean identified:

KVM_MMU_LOCKLESS_AGING
KVM_GENERIC_PRE_FAULT_MEMORY
HAVE_KVM_MSI
HAVE_KVM_READONLY_MEM
HAVE_KVM_NO_POLL
HAVE_KVM_INVALID_WAKEUPS

I will check the Kconfig files if I find more.

Similary to Kconfig I want to implement them as opt-in.

While I am at it I would streamline the naming and remove the HAVE_ prefix.

<arch>/include/asm/kvm_host.h

#define KVM_GENERIC_PRE_FAULT_MEMORY
#define KVM_INVALID_WAKEUPS
#define KVM_MMU_LOCKLESS_AGING
#define KVM_MSI
#define KVM_NO_POLL
#define KVM_READONLY_MEM

(similar to the MMIO patch)

Any objections for doing this in one patch?

The patch changes the Kconfigs, asm/kvm_host.h and removes the CONFIG_ prefixes of the configs in the code.

I would handle MMIO in a different patch as this is the only config that also modifies the Makefile but I am defenitle not aggainst merging it into the new conversion patch.

If there are no objections I will use the Makefilevariable approach for KVM_DEV_NAME and KVM_DEV_MINOR. It feels wrong for me to define those in a header. But defenitely, the devname and minor should be defined at compile time.

Steffen

author reviewer
#47 lore raw

Re: [PATCH v7 11/23] KVM: arm64: Share arm64 code with s390

In reply to Marc Zyngier
Quoted 21 lines · Marc Zyngier

On Wed, 02 Sep 2026 13:41:52 +0100,
Steffen Eiden <[email protected]> wrote:

[...]

Quoted 16 lines

So my suggestion is:

For series 1 (and 2) s390 has its own, local implementation of handle_exit without sharing any code with arm64 in regard to EC

When s390 has all the relevant code available (likely series 3) s390 can switch to using most (if not all) EC handling code used by arm_exit_handlers implemented in handle_exit.c.

This can be done without a mixed patch:

1. Mark arm64 handle_exit.c as shared (roughly the first 400Loc w/o includes) - s390 will not use it after this patch.
2. remove the handle exit code from s390's handle_exit.c and include the new generated handle_exit.inc

Quoted 9 lines
Quoted 5 lines

This approach brings us further into the share architecture not only code approach you meantrioned in the comments regarding sys-regs. (I or Andreas will provide a solution for this later :) )

Is that OK with you, what do you think?

I *think* that's OK. I just want to make sure that in the long run, we don't end-up with pointless divergence.

Yes, that is what I want to avoid as well. I want like to share as much architecture code/concepts as possible while keeping the churn as low as possible.

Steffen

author reviewer
#69 lore raw

Re: [PATCH v7 21/23] KVM: s390: arm64: Implement vCPU IOCTLs

In reply to Janosch Frank
Quoted 12 lines · Janosch Frank
Quoted 8 lines · Steffen Eiden

Implement all required vCPU (arch) IOCTLs.

---

[...]

Quoted 16 lines

arch/s390/kvm/arm64/arm.c static unsigned long system_supported_vcpu_features(void) · line 389

Quoted 12 lines
arch/s390/kvm/arm64/arm.c +12 -0
+static void arm_vcpu_run(struct kvm_vcpu *vcpu)
+{
+	struct kvm_sae_block *sae_block = &vcpu->arch.sae_block;
+
+	adjust_pc(vcpu);
+
+	local_irq_disable();
+	guest_timing_enter_irqoff();
+	guest_state_enter_irqoff();
+	local_irq_enable();
+
+	sae_block->icptr = 0;

We already do that in the function below, no?
If possible I'd like to avoid setting ictpr here.

yes, removed it at this location. thanks.

...

Quoted 4 lines
Quoted 4 lines
arch/s390/kvm/arm64/arm.c +4 -0
+		kvm_vcpu_srcu_read_unlock(vcpu);
+
+		arm_vcpu_run(vcpu);
+
Quoted 8 lines

arch/s390/kvm/arm64/arm.c static unsigned long system_supported_vcpu_features(void) · line 469

Quoted 5 lines
arch/s390/kvm/arm64/arm.c +5 -0
+		vcpu->mode = OUTSIDE_GUEST_MODE;
+
+		kvm_vcpu_srcu_read_lock(vcpu);
+
+		ret = handle_exit(vcpu);

ARM increases stat.exits here and since you copied the stat fields over we could do that too.

Good catch, thanks. Adding the stat.

Quoted 9 lines

General question to the ARM maintainers:
We'll be adding more stat fields in the very soon future.
Are you fine with us adding them under any name or should we prefix them so it's clear they exist because of our architecture / implementation and will likely never be added to ARM KVM?

In the past we've added a lot of stats and I'd expect us to at least add stats for every intercept reason.

That is the exact reason I copied the struct and not reuse the arm64 defenition. I already added a gmap stat we have but native arm not.

If the stat is meaningfull I am all for adding the stat to arm as well.

Steffen

#80 lore raw

Re: [PATCH v7 23/23] KVM: s390: arm64: Add KVM_S390_ARM64 Kconfig and Makefile

In reply to Sean Christopherson

Am 03.09.26 um 18:33 schrieb Sean Christopherson:

Quoted 33 lines · Steffen Eiden
Quoted 7 lines · Sean Christopherson
Quoted 5 lines

And FWIW, while it might seem daunting, from my perspective it's not actually that much churn to do things "right". Provide KVM_S390_NATIVE, and then have KVM reflect the "weakest" of S390_NATIVE vs. S390_ARCH. I.e. make KVM=m if either of the "real" KVMs will be a module. That requires some creative shenanigans, but it's not hard, just weird.

Quoted 6 lines

FYI the first versions of this series had a 3 configs approach very similar to yours.

IIRC it was not so much the churn we have in (upstream) kernel code but more on the distro side and to everyone building the kernel in their favourite architecture (s390 :))

How many people are running distro kernels on s390 hardware? And how many distros actually change the default KVM settings, e.g. to build KVM as a module instead of baking it into the kernel? My guess is "not many" and "almost none", i.e. the actual impact on downstream users is likely miniscule.

Quoted 1 line

Suddenly the KVM config changed its behaviour (effectively becoming a noop)

Not really, because "KVM" itself is inaccessible (well, unless someone is hand- editing .configs or generating them by script, but that's their own fault). E.g. upgrading to a new kernel will explicitly prompt the user to choose for both KVM_S390_NATIVE and KVM_S390_ARM64 (or whatever they get called).

Quoted 5 lines

I am starting to think that having no separate config option for arm on s390 might be the easisest way. Just KVM and guard both modules behind it. It reduces the config space bloat and I do not see a reason why somoeone should only compile one KVM module but not the other. kvm-arm64 won't load if you do not have the hardware anyways.

Quoted 8 lines

Because there may be an unforseen need down the road? Smushing things together after the fact is generally easy, pulling things apart without breaking everything is usually much, much harder.

Quoted 4 lines

But I am not opposed to the x86 approach. Just thinking loud.

Anyways, I am off for vacation expect a reduced reply frequency from my side :)

Yet another option
I think there is value in keeping the config space simpte for users. Why not simply keep CONFIG_KVM and it will disable, module build, compile-in both variants.

author reviewer
#81 lore raw

Re: [PATCH v7 23/23] KVM: s390: arm64: Add KVM_S390_ARM64 Kconfig and Makefile

Quoted 40 lines · Christian Borntraeger

Am 03.09.26 um 18:33 schrieb Sean Christopherson:

Quoted 37 lines · Steffen Eiden
Quoted 7 lines · Sean Christopherson
Quoted 5 lines

And FWIW, while it might seem daunting, from my perspective it's not actually that much churn to do things "right". Provide KVM_S390_NATIVE, and then have KVM reflect the "weakest" of S390_NATIVE vs. S390_ARCH. I.e. make KVM=m if either of the "real" KVMs will be a module. That requires some creative shenanigans, but it's not hard, just weird.

Quoted 6 lines

FYI the first versions of this series had a 3 configs approach very similar to yours.

IIRC it was not so much the churn we have in (upstream) kernel code but more on the distro side and to everyone building the kernel in their favourite architecture (s390 :))

How many people are running distro kernels on s390 hardware? And how many distros actually change the default KVM settings, e.g. to build KVM as a module instead of baking it into the kernel? My guess is "not many" and "almost none", i.e. the actual impact on downstream users is likely miniscule.

Quoted 1 line

Suddenly the KVM config changed its behaviour (effectively becoming a noop)

Not really, because "KVM" itself is inaccessible (well, unless someone is hand- editing .configs or generating them by script, but that's their own fault). E.g. upgrading to a new kernel will explicitly prompt the user to choose for both KVM_S390_NATIVE and KVM_S390_ARM64 (or whatever they get called).

Quoted 5 lines

I am starting to think that having no separate config option for arm on s390 might be the easisest way. Just KVM and guard both modules behind it. It reduces the config space bloat and I do not see a reason why somoeone should only compile one KVM module but not the other. kvm-arm64 won't load if you do not have the hardware anyways.

Because there may be an unforseen need down the road? Smushing things together after the fact is generally easy, pulling things apart without breaking everything is usually much, much harder.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

But I am not opposed to the x86 approach. Just thinking loud.

Anyways, I am off for vacation expect a reduced reply frequency from my side :)

Yet another option
I think there is value in keeping the config space simpte for users. Why not simply keep CONFIG_KVM and it will disable, module build, compile-in both variants.

Good Idea. I'll merge the configs into one.