Skip to contentSkip to conversation

KVM: s390: Introduce arm64 KVM

PATCH Superseded Steffen Eiden 8 participants 77 messages Jul 6 – Jul 23

Subsystems (21)
x86 KVM PowerPC MIPS +17 more subsystems

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Steffen Eiden

author reviewer
#0 lore raw

v4 cover KVM: s390: Introduce arm64 KVM

This series is a complete rework of previous versions [1], fundamentally changing the approach to code and header sharing between s390 and arm64.

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

The new approach does not move arm64 code to a shared location. Instead, during build time, 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.

Userspace tools requiring KVM/arm64 uapi on s390 must use the 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

KVM symbol cleanup and refactoring (patches 1-5):
Prepare KVM to support two KVM modules in parallel. Clean up KVM module symbol exports, making it possible to load two KVM modules side by side. Make the KVM device name configurable and remove KVM_MMIO as a config option.

Arm64 header and code sharing preparation (patches 6-9):
Mark shareable parts with ARM64_S390_COMMON. Perform minor refactoring to ease integration into s390.

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

KVM/s390 reorganization (patches 12-17):
Prepare s390 for a second KVM module. Move existing s390 KVM code into dedicated s390 and gmap subdirectories to make room for a second KVM implementation. Refactor gmap to better integrate with the second KVM implementation.

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

I hope that this new approach suits arm64 better. I definitely think this way of doing things is more elegant, has less churn, and less work for us all.

Some comments:

I want to create a verification tool for you arm guys that mimics the s390 use of the arm64 code and headers but compiles/works on arm64 as a quick sanity check for any arm64 developer. I have some general Ideas (A C file including all the arm code & headers and see if it compiles) about how to do this but no concrete ideas yet. Pointers/Ideas are highly appreciated

To further harden the extraction of code/headers one could do the following:
1. Expand all the macros on the header/code

       a) with -DARM64_S390_COMMON
       b) without -DARM64_S390_COMMON

2. The diff of a & b should be the stuff s390 needs
3. Postprocess it a bit (guards and copyight)

But the AWK approach looked more straight forward and simpler.

There is no way around it - In the future Arm64 maintainers should at least build s390 before sending merge request.

Steffen

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]/

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

Paolo Bonzini (3):
VFIO: take reference to the KVM module
KVM, vfio: remove symbol_get(kvm_get_kvm_safe) from vfio KVM, vfio: remove symbol_get(kvm_put_kvm) from vfio

Steffen Eiden (23):
KVM: Make device name configurable
KVM: Remove KVM_MMIO as config option
arm64: Use proper include variant
arm64: Prepare sharing arm64 headers with s390
KVM: arm64: Prepare sharing arm64 code with s390
KVM: arm64: Access elements of vcpu_gp_regs individually
KVM: arm64: Refactor core-reset into a separate function
s390: Use arm64 headers
KVM: s390: Use arm64 code
KVM: s390: Prepare KVM/s390 for a second KVM module
KVM: s390: Move s390 kvm code into a subdirectory
KVM: S390: Prepare gmap for a second KVM implementation
KVM: s390: gmap: Refactor storage key and CMMA code into separate
files
KVM: s390: Refactor prefix handling into a separate file
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: Enable KVM_ARM64 config and Kbuild

 arch/arm64/Makefile                           |   2 +
 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          |  32 +-
 arch/arm64/include/asm/kvm_host.h             |  38 +-
 arch/arm64/include/asm/kvm_mmu.h              |   6 +
 arch/arm64/include/asm/ptrace.h               |   9 +
 arch/arm64/include/asm/sysreg.h               |  31 +-
 arch/arm64/kvm/Kconfig                        |   1 -
 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                |   7 +-
 arch/arm64/kvm/hyp/include/hyp/adjust_pc.h    |   4 +-
 arch/arm64/kvm/hyp/include/hyp/switch.h       |   6 +-
 arch/arm64/kvm/mmio.c                         |  14 +
 arch/arm64/kvm/reset.c                        |  46 +-
 arch/loongarch/include/asm/kvm_host.h         |   2 +
 arch/loongarch/kvm/Kconfig                    |   1 -
 arch/mips/include/asm/kvm_host.h              |   2 +
 arch/mips/kvm/Kconfig                         |   1 -
 arch/powerpc/include/asm/kvm_host.h           |   7 +
 arch/powerpc/kvm/Kconfig                      |   4 -
 arch/riscv/include/asm/kvm_host.h             |   2 +
 arch/riscv/kvm/Kconfig                        |   1 -
 arch/s390/Kconfig                             |   2 +-
 arch/s390/boot/ipl_parm.c                     |   2 +-
 arch/s390/boot/uv.c                           |   2 +-
 arch/s390/configs/debug_defconfig             |   2 +-
 arch/s390/configs/defconfig                   |   3 +-
 arch/s390/include/arm64/kvm_emulate.h         | 135 +++
 arch/s390/include/arm64/kvm_nested.h          |  11 +
 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              | 772 +-----------------
 arch/s390/include/asm/kvm_host_arm64.h        | 210 +++++
 arch/s390/include/asm/kvm_host_arm64_types.h  | 129 +++
 .../asm/{kvm_host.h => kvm_host_s390.h}       |  17 +-
 ...kvm_host_types.h => kvm_host_s390_types.h} |   6 +-
 arch/s390/include/asm/kvm_host_types.h        | 351 +-------
 arch/s390/include/asm/sae.h                   |  56 ++
 arch/s390/include/asm/sclp.h                  |   5 +-
 arch/s390/include/asm/stacktrace.h            |   5 +
 arch/s390/kernel/asm-offsets.c                |   1 +
 arch/s390/kernel/early.c                      |   2 +-
 arch/s390/kernel/entry.S                      |  35 +-
 arch/s390/kernel/perf_event.c                 |   2 +-
 arch/s390/kernel/processor.c                  |   3 +
 arch/s390/kvm/Kconfig                         |  37 +-
 arch/s390/kvm/Makefile                        |  12 +-
 arch/s390/kvm/arm64/Kconfig                   |  23 +
 arch/s390/kvm/arm64/Makefile                  |  94 +++
 arch/s390/kvm/arm64/Makefile.gen              |  30 +
 arch/s390/kvm/arm64/arm.c                     | 726 ++++++++++++++++
 arch/s390/kvm/arm64/arm.h                     |  63 ++
 arch/s390/kvm/arm64/copy-arm64c.awk           |  69 ++
 arch/s390/kvm/arm64/guest.c                   | 169 ++++
 arch/s390/kvm/arm64/guest.h                   |  14 +
 arch/s390/kvm/arm64/handle_exit.c             | 143 ++++
 arch/s390/kvm/arm64/handle_exit.h             |   9 +
 arch/s390/kvm/arm64/inject_fault.c            |  21 +
 arch/s390/kvm/arm64/mmio.c                    |  12 +
 arch/s390/kvm/arm64/mmu.c                     | 178 ++++
 arch/s390/kvm/arm64/reset.c                   |  62 ++
 arch/s390/kvm/arm64/reset.h                   |  11 +
 arch/s390/kvm/gmap/Makefile                   |  12 +
 arch/s390/kvm/gmap/cmma.c                     | 313 +++++++
 arch/s390/kvm/gmap/cmma.h                     |  36 +
 arch/s390/kvm/{ => gmap}/dat.c                | 552 +------------
 arch/s390/kvm/{ => gmap}/dat.h                |  20 +-
 arch/s390/kvm/{ => gmap}/faultin.c            |  11 +-
 arch/s390/kvm/{ => gmap}/faultin.h            |   6 +-
 arch/s390/kvm/{ => gmap}/gmap.c               | 114 +--
 arch/s390/kvm/{ => gmap}/gmap.h               |  44 +-
 arch/s390/kvm/gmap/mmu.c                      | 193 +++++
 arch/s390/kvm/gmap/prefix-stubs.c             |  21 +
 arch/s390/kvm/gmap/prefix.c                   |  80 ++
 arch/s390/kvm/gmap/prefix.h                   |  68 ++
 arch/s390/kvm/gmap/sk.c                       | 264 ++++++
 arch/s390/kvm/gmap/sk.h                       |  18 +
 arch/s390/kvm/gmap/trace-gmap.h               |  59 ++
 arch/s390/kvm/{ => s390}/Kconfig              |  27 +-
 arch/s390/kvm/{ => s390}/Makefile             |  10 +-
 arch/s390/kvm/{ => s390}/diag.c               |   2 +-
 arch/s390/kvm/{ => s390}/gaccess.c            |   3 +-
 arch/s390/kvm/{ => s390}/gaccess.h            |   2 +-
 arch/s390/kvm/{ => s390}/guestdbg.c           |   2 +-
 arch/s390/kvm/{ => s390}/intercept.c          |   2 +-
 arch/s390/kvm/{ => s390}/interrupt.c          |   2 +-
 arch/s390/kvm/{ => s390}/pci.c                |   2 +-
 arch/s390/kvm/{ => s390}/pci.h                |   0
 arch/s390/kvm/{ => s390}/priv.c               |   4 +-
 arch/s390/kvm/{ => s390}/pv.c                 |   2 +-
 arch/s390/kvm/{kvm-s390.c => s390/s390.c}     | 146 +---
 arch/s390/kvm/{kvm-s390.h => s390/s390.h}     |  18 +-
 arch/s390/kvm/{ => s390}/sigp.c               |   2 +-
 arch/s390/kvm/{ => s390}/trace-s390.h         |   0
 arch/s390/kvm/{ => s390}/trace.h              |  14 -
 arch/s390/kvm/{ => s390}/vsie.c               |   2 +-
 arch/s390/tools/Makefile                      |   5 +
 arch/s390/tools/Makefile.arm64h               |  58 ++
 arch/s390/tools/copy-arm64h-full.awk          |  43 +
 arch/s390/tools/copy-arm64h.awk               |  71 ++
 arch/s390/tools/opcodes.txt                   |   3 +
 arch/x86/include/asm/kvm_host.h               |   2 +
 arch/x86/kvm/Kconfig                          |   1 -
 arch/x86/kvm/mmu/tdp_mmu.c                    |   2 +-
 arch/x86/kvm/svm/sev.c                        |   2 +-
 arch/x86/kvm/vmx/nested.h                     |   4 +-
 drivers/s390/char/sclp_early.c                |   1 +
 drivers/vfio/device_cdev.c                    |   2 +-
 drivers/vfio/group.c                          |   5 +-
 drivers/vfio/vfio.h                           |  15 +-
 drivers/vfio/vfio_main.c                      |  52 +-
 include/linux/kvm_host.h                      |  17 +-
 include/linux/kvm_types.h                     |  33 +
 include/linux/vfio.h                          |   4 +-
 virt/kvm/Kconfig                              |   3 -
 virt/kvm/Makefile.kvm                         |   3 +-
 virt/kvm/coalesced_mmio.c                     |   3 +
 virt/kvm/coalesced_mmio.h                     |   2 +-
 virt/kvm/kvm_main.c                           |  75 +-
 virt/kvm/vfio.c                               |   4 +-
 126 files changed, 3944 insertions(+), 2212 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/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
 copy arch/s390/include/asm/{kvm_host.h => kvm_host_s390.h} (98%)
 copy arch/s390/include/asm/{kvm_host_types.h => kvm_host_s390_types.h} (98%)
 create mode 100644 arch/s390/include/asm/sae.h
 create mode 100644 arch/s390/kvm/arm64/Kconfig
 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/guest.h
 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/reset.h
 create mode 100644 arch/s390/kvm/gmap/Makefile
 create mode 100644 arch/s390/kvm/gmap/cmma.c
 create mode 100644 arch/s390/kvm/gmap/cmma.h
 rename arch/s390/kvm/{ => gmap}/dat.c (61%)
 rename arch/s390/kvm/{ => gmap}/dat.h (96%)
 rename arch/s390/kvm/{ => gmap}/faultin.c (96%)
 rename arch/s390/kvm/{ => gmap}/faultin.h (96%)
 rename arch/s390/kvm/{ => gmap}/gmap.c (93%)
 rename arch/s390/kvm/{ => gmap}/gmap.h (94%)
 create mode 100644 arch/s390/kvm/gmap/mmu.c
 create mode 100644 arch/s390/kvm/gmap/prefix-stubs.c
 create mode 100644 arch/s390/kvm/gmap/prefix.c
 create mode 100644 arch/s390/kvm/gmap/prefix.h
 create mode 100644 arch/s390/kvm/gmap/sk.c
 create mode 100644 arch/s390/kvm/gmap/sk.h
 create mode 100644 arch/s390/kvm/gmap/trace-gmap.h
 copy arch/s390/kvm/{ => s390}/Kconfig (61%)
 copy arch/s390/kvm/{ => s390}/Makefile (53%)
 rename arch/s390/kvm/{ => s390}/diag.c (99%)
 rename arch/s390/kvm/{ => s390}/gaccess.c (99%)
 rename arch/s390/kvm/{ => s390}/gaccess.h (99%)
 rename arch/s390/kvm/{ => s390}/guestdbg.c (99%)
 rename arch/s390/kvm/{ => s390}/intercept.c (99%)
 rename arch/s390/kvm/{ => s390}/interrupt.c (99%)
 rename arch/s390/kvm/{ => s390}/pci.c (99%)
 rename arch/s390/kvm/{ => s390}/pci.h (100%)
 rename arch/s390/kvm/{ => s390}/priv.c (99%)
 rename arch/s390/kvm/{ => s390}/pv.c (99%)
 rename arch/s390/kvm/{kvm-s390.c => s390/s390.c} (98%)
 rename arch/s390/kvm/{kvm-s390.h => s390/s390.h} (97%)
 rename arch/s390/kvm/{ => s390}/sigp.c (99%)
 rename arch/s390/kvm/{ => s390}/trace-s390.h (100%)
 rename arch/s390/kvm/{ => s390}/trace.h (97%)
 rename arch/s390/kvm/{ => s390}/vsie.c (99%)
 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: 8cdeaa50eae8dad34885515f62559ee83e7e8dda

Signature · 1 line
2.53.0
author reviewer
#1 lore raw

PATCH v4 1/27 VFIO: take reference to the KVM module

From: Paolo Bonzini <[email protected]>

VFIO is implicitly taking a reference to the KVM module between vfio_device_get_kvm_safe and vfio_device_put_kvm, thanks to symbol_get and symbol_put.

In preparation for removing symbol_get and symbol_put themselves from VFIO, actually store a pointer to the KVM module and use module_get()/module_put() to keep KVM alive.

---
 drivers/vfio/device_cdev.c |  2 +-
 drivers/vfio/group.c       |  5 +++--
 drivers/vfio/vfio.h        | 15 ++++++++++-----
 drivers/vfio/vfio_main.c   | 38 ++++++++++++++++++++++++--------------
 include/linux/vfio.h       |  3 ++-
 virt/kvm/vfio.c            |  4 ++--
 6 files changed, 42 insertions(+), 25 deletions(-)
drivers/vfio/device_cdev.c +1 -1
index 54abf312cf04..a729be99d06c 100644
@@ -56,7 +56,7 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
 static void vfio_df_get_kvm_safe(struct vfio_device_file *df)
 {
 	spin_lock(&df->kvm_ref_lock);
-	vfio_device_get_kvm_safe(df->device, df->kvm);
+	vfio_device_get_kvm_safe(df->device, df->kvm, df->kvm_module);
 	spin_unlock(&df->kvm_ref_lock);
 }
 
drivers/vfio/group.c +3 -2
index b2299e5bc6df..fc24c0b2cc6f 100644
@@ -163,7 +163,7 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group,
 static void vfio_device_group_get_kvm_safe(struct vfio_device *device)
 {
 	spin_lock(&device->group->kvm_ref_lock);
-	vfio_device_get_kvm_safe(device, device->group->kvm);
+	vfio_device_get_kvm_safe(device, device->group->kvm, device->group->kvm_module);
 	spin_unlock(&device->group->kvm_ref_lock);
 }
 
@@ -860,10 +860,11 @@ 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 kvm *kvm, struct module *kvm_module)
 {
 	spin_lock(&group->kvm_ref_lock);
 	group->kvm = kvm;
+	group->kvm_module = kvm_module;
 	spin_unlock(&group->kvm_ref_lock);
 }
 
drivers/vfio/vfio.h +10 -5
index e4b72e79b7e3..8209da17246c 100644
@@ -22,8 +22,9 @@ struct vfio_device_file {
 
 	u8 access_granted;
 	u32 devid; /* only valid when iommufd is valid */
-	spinlock_t kvm_ref_lock; /* protect kvm field */
+	spinlock_t kvm_ref_lock; /* protect kvm and kvm_module fields */
 	struct kvm *kvm;
+	struct module *kvm_module;
 	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
 };
 
@@ -89,6 +90,7 @@ struct vfio_group {
 	enum vfio_group_type		type;
 	struct mutex			group_lock;
 	struct kvm			*kvm;
+	struct module			*kvm_module;
 	struct file			*opened_file;
 	struct iommufd_ctx		*iommufd;
 	spinlock_t			kvm_ref_lock;
@@ -107,7 +109,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 kvm *kvm, struct module *kvm_module);
 bool vfio_device_has_container(struct vfio_device *device);
 int __init vfio_group_init(void);
 void vfio_group_cleanup(void);
@@ -170,7 +172,8 @@ static inline bool vfio_group_enforced_coherent(struct vfio_group *group)
 	return true;
 }
 
-static inline void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
+static inline void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm,
+				      struct module *kvm_module)
 {
 }
 
@@ -434,11 +437,13 @@ 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 kvm *kvm,
+			      struct module *kvm_module);
 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 kvm *kvm,
+					    struct module *kvm_module)
 {
 }
 
drivers/vfio/vfio_main.c +24 -14
index ed538aebb0b8..8289aca97b6b 100644
@@ -448,7 +448,8 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);
 
 #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 kvm *kvm,
+			      struct module *kvm_module)
 {
 	void (*pfn)(struct kvm *kvm);
 	bool (*fn)(struct kvm *kvm);
@@ -459,25 +460,31 @@ void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm)
 	if (!kvm)
 		return;
 
+	if (!try_module_get(kvm_module))
+		return;
+
 	pfn = symbol_get(kvm_put_kvm);
 	if (WARN_ON(!pfn))
-		return;
+		goto out_put_mod;
 
 	fn = symbol_get(kvm_get_kvm_safe);
-	if (WARN_ON(!fn)) {
-		symbol_put(kvm_put_kvm);
-		return;
-	}
+	if (WARN_ON(!fn))
+		goto out_put_sym;
 
 	ret = fn(kvm);
 	symbol_put(kvm_get_kvm_safe);
-	if (!ret) {
-		symbol_put(kvm_put_kvm);
-		return;
-	}
+	if (!ret)
+		goto out_put_sym;
 
 	device->put_kvm = pfn;
 	device->kvm = kvm;
+	device->kvm_module = kvm_module;
+	return;
+
+out_put_sym:
+	symbol_put(kvm_put_kvm);
+out_put_mod:
+	module_put(kvm_module);
 }
 
 void vfio_device_put_kvm(struct vfio_device *device)
@@ -496,6 +503,8 @@ void vfio_device_put_kvm(struct vfio_device *device)
 
 clear:
 	device->kvm = NULL;
+	module_put(device->kvm_module);
+	device->kvm_module = NULL;
 }
 #endif
 
@@ -1520,7 +1529,7 @@ bool vfio_file_enforced_coherent(struct file *file)
 }
 EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
 
-static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)
+static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm, struct module *kvm_module)
 {
 	struct vfio_device_file *df = file->private_data;
 
@@ -1531,6 +1540,7 @@ static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)
 	 */
 	spin_lock(&df->kvm_ref_lock);
 	df->kvm = kvm;
+	df->kvm_module = kvm_module;
 	spin_unlock(&df->kvm_ref_lock);
 }
 
@@ -1542,16 +1552,16 @@ static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)
  * When a VFIO device is first opened the KVM will be available in
  * device->kvm if one was associated with the file.
  */
-void vfio_file_set_kvm(struct file *file, struct kvm *kvm)
+void vfio_file_set_kvm(struct file *file, struct kvm *kvm, struct module *kvm_module)
 {
 	struct vfio_group *group;
 
 	group = vfio_group_from_file(file);
 	if (group)
-		vfio_group_set_kvm(group, kvm);
+		vfio_group_set_kvm(group, kvm, kvm_module);
 
 	if (vfio_device_from_file(file))
-		vfio_device_file_set_kvm(file, kvm);
+		vfio_device_file_set_kvm(file, kvm, kvm_module);
 }
 EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
 
include/linux/vfio.h +2 -1
index 31b826efba00..5bfec0339804 100644
@@ -55,6 +55,7 @@ struct vfio_device {
 	unsigned int migration_flags;
 	u8 precopy_info_v2;
 	struct kvm *kvm;
+	struct module *kvm_module;
 
 	/* Members below here are private, not for driver use */
 	unsigned int index;
@@ -377,7 +378,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 *file, struct kvm *kvm, struct module *kvm_module);
 
 #define VFIO_PIN_PAGES_MAX_ENTRIES	(PAGE_SIZE/sizeof(unsigned long))
 
virt/kvm/vfio.c +2 -2
index 6cdc4e9a333a..c593f31136f9 100644
@@ -37,13 +37,13 @@ struct kvm_vfio {
 
 static void kvm_vfio_file_set_kvm(struct file *file, struct kvm *kvm)
 {
-	void (*fn)(struct file *file, struct kvm *kvm);
+	void (*fn)(struct file *file, struct kvm *kvm, struct module *kvm_module);
 
 	fn = symbol_get(vfio_file_set_kvm);
 	if (!fn)
 		return;
 
-	fn(file, kvm);
+	fn(file, kvm, kvm ? THIS_MODULE : NULL);
 
 	symbol_put(vfio_file_set_kvm);
 }
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v4 01/27] VFIO: take reference to the KVM module

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:
- [High] In the VFIO cdev binding path, concurrent or repeated calls to VFIO_DEVICE_BIND_IOMMUFD on multiple file descriptors for the same device cause KVM reference leaks and state corruption. --

commit 298282ceb4d6390cae8f782bcdd7f84408cf4d30
Author: Paolo Bonzini <[email protected]>

VFIO: take reference to the KVM module

This patch modifies VFIO to store a pointer to the KVM module alongside the KVM pointer. It updates the get/set paths to take and release a module reference to prevent the KVM module from unloading while in use, in preparation for removing symbol_get and symbol_put.

Quoted 4 lines
drivers/vfio/vfio_main.c +0 -0
index ed538aebb0b8e..8289aca97b6be 100644

[ ... ]

Quoted 33 lines
drivers/vfio/vfio_main.c +8 -7
@@ -448,7 +448,8 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);
 
 #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 kvm *kvm,
+			      struct module *kvm_module)
 {
 	void (*pfn)(struct kvm *kvm);
 	bool (*fn)(struct kvm *kvm);
@@ -459,25 +460,31 @@ void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm)
 	if (!kvm)
 		return;
 
+	if (!try_module_get(kvm_module))
+		return;
+
 	pfn = symbol_get(kvm_put_kvm);
 	if (WARN_ON(!pfn))
-		return;
+		goto out_put_mod;
 
 	fn = symbol_get(kvm_get_kvm_safe);
-	if (WARN_ON(!fn)) {
-		symbol_put(kvm_put_kvm);
-		return;
-	}
+	if (WARN_ON(!fn))
+		goto out_put_sym;
 
 	ret = fn(kvm);
 	symbol_put(kvm_get_kvm_safe);
-	if (!ret) {

drivers/vfio/vfio_main.c void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm) · line 482

Quoted 10 lines
drivers/vfio/vfio_main.c +4 -3
-		symbol_put(kvm_put_kvm);
-		return;
-	}
+	if (!ret)
+		goto out_put_sym;
 
 	device->put_kvm = pfn;
 	device->kvm = kvm;
+	device->kvm_module = kvm_module;
+	return;

[Severity: High]
This is a pre-existing issue, but can concurrent or repeated calls to VFIO_DEVICE_BIND_IOMMUFD on multiple file descriptors for the same device cause KVM reference leaks and state corruption here?

In vfio_device_get_kvm_safe() in drivers/vfio/vfio_main.c, the assignments above blindly overwrite device->kvm (and now device->kvm_module) without dropping any existing references.

If a user opens the VFIO device node twice (fd1 and fd2) and binds fd1, device->kvm is set and the reference is taken. If they then call VFIO_DEVICE_BIND_IOMMUFD on fd2, this function overwrites the pointers, leaking the initial KVM and module references.

Furthermore, when vfio_df_open() subsequently fails in the fd2 bind path because device->open_count > 0, the error path in vfio_df_ioctl_bind_iommufd() unconditionally calls vfio_device_put_kvm(device). This drops the newly overwritten reference and clears device->kvm to NULL, entirely breaking the KVM integration for the still-active fd1.

Quoted 8 lines
drivers/vfio/vfio_main.c +5 -0
+
+out_put_sym:
+	symbol_put(kvm_put_kvm);
+out_put_mod:
+	module_put(kvm_module);
 }
 
 void vfio_device_put_kvm(struct vfio_device *device)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v4 01/27] VFIO: take reference to the KVM module

In reply to Steffen Eiden
Quoted 5 lines · Steffen Eiden

From: Paolo Bonzini <[email protected]>

VFIO is implicitly taking a reference to the KVM module between vfio_device_get_kvm_safe and vfio_device_put_kvm, thanks to symbol_get and symbol_put.

I thought we weren't doing this approach? Use the struct file as the kvm handle. I thought I saw patches doing that already?

Jason

#4 lore raw

Re: [PATCH v4 01/27] VFIO: take reference to the KVM module

Quoted 9 lines · Jason Gunthorpe
Quoted 5 lines · Steffen Eiden

From: Paolo Bonzini <[email protected]>

VFIO is implicitly taking a reference to the KVM module between vfio_device_get_kvm_safe and vfio_device_put_kvm, thanks to symbol_get and symbol_put.

I thought we weren't doing this approach? Use the struct file as the kvm handle. I thought I saw patches doing that already?

+1, my joking-but-serious NAK stands.

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

author reviewer
#5 lore raw

Re: [PATCH v4 01/27] VFIO: take reference to the KVM module

Quoted 8 lines · Sean Christopherson
Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · Steffen Eiden

From: Paolo Bonzini <[email protected]>

VFIO is implicitly taking a reference to the KVM module between vfio_device_get_kvm_safe and vfio_device_put_kvm, thanks to symbol_get and symbol_put.

Quoted 6 lines
Quoted 2 lines

I thought we weren't doing this approach? Use the struct file as the kvm handle. I thought I saw patches doing that already?

+1, my joking-but-serious NAK stands.

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

My bad, I did not cover this topic in the cover letter:

I included those tree patches so that the rest of the series links in all configurations. I know we will not use this approach to mitigate this issue.

Steffen

author reviewer
#6 lore raw

PATCH v4 2/27 KVM, vfio: remove symbol_get(kvm_get_kvm_safe) from vfio

In reply to Steffen Eiden

From: Paolo Bonzini <[email protected]>

Right now, KVM and VFIO are using symbol_get to access each other's symbols because of a circular reference between the modules, as well as to avoid loading them unnecessarily.

However, usage of symbol_get is mostly deprecated and there are just a handful of users left. In the case of VFIO, in particular, the functions it calls can be made inline. Start with kvm_get_kvm_safe, for which it is trivial to do so.

While at it, move the function from kvm_host.h to kvm_types.h. Unlike e.g. drivers/s390/crypto/vfio_ap_ops.c, there's no need for VFIO to know any implementation details of KVM, and struct kvm can be treated as an opaque type.

---
 arch/x86/kvm/mmu/tdp_mmu.c |  2 +-
 arch/x86/kvm/svm/sev.c     |  2 +-
 arch/x86/kvm/vmx/nested.h  |  4 ++--
 drivers/vfio/vfio_main.c   |  8 +-------
 include/linux/kvm_host.h   |  8 ++++----
 include/linux/kvm_types.h  | 24 ++++++++++++++++++++++++
 virt/kvm/kvm_main.c        | 29 +++++++----------------------
 7 files changed, 40 insertions(+), 37 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +1 -1
index c1cbae65d239..913edd9403e5 100644
@@ -1089,7 +1089,7 @@ void kvm_tdp_mmu_invalidate_roots(struct kvm *kvm,
 	 * being destroyed in an error path of KVM_CREATE_VM.
 	 */
 	if (IS_ENABLED(CONFIG_PROVE_LOCKING) &&
-	    refcount_read(&kvm->users_count) && kvm->created_vcpus)
+	    refcount_read(&kvm->rc.users_count) && kvm->created_vcpus)
 		lockdep_assert_held_write(&kvm->mmu_lock);
 
 	/*
arch/x86/kvm/svm/sev.c +1 -1
index 427229347876..f588129f7a4f 100644
@@ -115,7 +115,7 @@ static __always_inline void kvm_lockdep_assert_sev_lock_held(struct kvm *kvm)
 	 * Querying SEV+ support is safe if there are no other references, i.e.
 	 * if concurrent initialization of SEV+ is impossible.
 	 */
-	if (!refcount_read(&kvm->users_count))
+	if (!refcount_read(&kvm->rc.users_count))
 		return;
 
 	/*
arch/x86/kvm/vmx/nested.h +2 -2
index 6d6cd5904ddf..a4647859fe1f 100644
@@ -58,7 +58,7 @@ bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
 static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
 {
 	lockdep_assert_once(lockdep_is_held(&vcpu->mutex) ||
-			    !refcount_read(&vcpu->kvm->users_count));
+			    !refcount_read(&vcpu->kvm->rc.users_count));
 
 	return to_vmx(vcpu)->nested.cached_vmcs12;
 }
@@ -66,7 +66,7 @@ static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
 static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
 {
 	lockdep_assert_once(lockdep_is_held(&vcpu->mutex) ||
-			    !refcount_read(&vcpu->kvm->users_count));
+			    !refcount_read(&vcpu->kvm->rc.users_count));
 
 	return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
 }
drivers/vfio/vfio_main.c +1 -7
index 8289aca97b6b..b7b8b45f1cec 100644
@@ -452,7 +452,6 @@ void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm,
 			      struct module *kvm_module)
 {
 	void (*pfn)(struct kvm *kvm);
-	bool (*fn)(struct kvm *kvm);
 	bool ret;
 
 	lockdep_assert_held(&device->dev_set->lock);
@@ -467,12 +466,7 @@ void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm,
 	if (WARN_ON(!pfn))
 		goto out_put_mod;
 
-	fn = symbol_get(kvm_get_kvm_safe);
-	if (WARN_ON(!fn))
-		goto out_put_sym;
-
-	ret = fn(kvm);
-	symbol_put(kvm_get_kvm_safe);
+	ret = kvm_get_kvm_safe(kvm);
 	if (!ret)
 		goto out_put_sym;
 
include/linux/kvm_host.h +4 -4
index ab8cfaec82d3..015d02018109 100644
@@ -768,6 +768,9 @@ struct kvm_memslots {
 };
 
 struct kvm {
+	/* Must be the first field, see function definitions in kvm_types.h.  */
+	struct kvm_refcount rc;
+
 #ifdef KVM_HAVE_MMU_RWLOCK
 	rwlock_t mmu_lock;
 #else
@@ -831,7 +834,6 @@ struct kvm {
 	struct list_head ioeventfds;
 	struct kvm_vm_stat stat;
 	struct kvm_arch arch;
-	refcount_t users_count;
 #ifdef CONFIG_KVM_MMIO
 	struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
 	spinlock_t ring_lock;
@@ -1070,8 +1072,6 @@ static inline void kvm_irqfd_exit(void)
 int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module);
 void kvm_exit(void);
 
-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);
 void kvm_put_kvm_no_destroy(struct kvm *kvm);
@@ -1081,7 +1081,7 @@ static inline struct kvm_memslots *__kvm_memslots(struct kvm *kvm, int as_id)
 	as_id = array_index_nospec(as_id, KVM_MAX_NR_ADDRESS_SPACES);
 	return srcu_dereference_check(kvm->memslots[as_id], &kvm->srcu,
 			lockdep_is_held(&kvm->slots_lock) ||
-			!refcount_read(&kvm->users_count));
+			!refcount_read(&kvm->rc.users_count));
 }
 
 static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm)
include/linux/kvm_types.h +24 -0
index a568d8e6f4e8..add7cc2016e8 100644
@@ -33,6 +33,7 @@
 
 #include <linux/mutex.h>
 #include <linux/spinlock_types.h>
+#include <linux/refcount.h>
 
 struct kvm;
 struct kvm_async_pf;
@@ -140,6 +141,29 @@ struct kvm_vcpu_stat_generic {
 };
 
 #define KVM_STATS_NAME_SIZE	48
+
+struct kvm_refcount {
+	refcount_t users_count;
+};
+
+static inline void kvm_get_kvm(struct kvm *kvm)
+{
+	struct kvm_refcount *rc = (struct kvm_refcount *)kvm;
+
+	refcount_inc(&rc->users_count);
+}
+
+/*
+ * A safe version of kvm_get_kvm(), making sure the vm is not being destroyed.
+ * Return true if kvm referenced successfully, false otherwise.
+ */
+static inline bool kvm_get_kvm_safe(struct kvm *kvm)
+{
+	struct kvm_refcount *rc = (struct kvm_refcount *)kvm;
+
+	return refcount_inc_not_zero(&rc->users_count);
+}
+
 #endif /* !__ASSEMBLER__ */
 
 #endif /* __KVM_TYPES_H__ */
virt/kvm/kvm_main.c +7 -22
index e44c20c04961..7dec697ce35a 100644
@@ -1089,7 +1089,7 @@ static inline struct kvm_io_bus *kvm_get_bus_for_destruction(struct kvm *kvm,
 							     enum kvm_bus idx)
 {
 	return rcu_dereference_protected(kvm->buses[idx],
-					 !refcount_read(&kvm->users_count));
+					 !refcount_read(&kvm->rc.users_count));
 }
 
 static int kvm_enable_virtualization(void);
@@ -1146,7 +1146,8 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
 	if (r)
 		goto out_err_no_irq_routing;
 
-	refcount_set(&kvm->users_count, 1);
+	BUILD_BUG_ON(offsetof(struct kvm, rc) != 0);
+	refcount_set(&kvm->rc.users_count, 1);
 
 	for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
 		for (j = 0; j < 2; j++) {
@@ -1216,7 +1217,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
 out_err_no_disable:
 	kvm_arch_destroy_vm(kvm);
 out_err_no_arch_destroy_vm:
-	WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count));
+	WARN_ON_ONCE(!refcount_dec_and_test(&kvm->rc.users_count));
 	for (i = 0; i < KVM_NR_BUSES; i++)
 		kfree(kvm_get_bus_for_destruction(kvm, i));
 	kvm_free_irq_routing(kvm);
@@ -1309,25 +1310,9 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	mmdrop(mm);
 }
 
-void kvm_get_kvm(struct kvm *kvm)
-{
-	refcount_inc(&kvm->users_count);
-}
-EXPORT_SYMBOL_GPL(kvm_get_kvm);
-
-/*
- * Make sure the vm is not during destruction, which is a safe version of
- * kvm_get_kvm().  Return true if kvm referenced successfully, false otherwise.
- */
-bool kvm_get_kvm_safe(struct kvm *kvm)
-{
-	return refcount_inc_not_zero(&kvm->users_count);
-}
-EXPORT_SYMBOL_GPL(kvm_get_kvm_safe);
-
 void kvm_put_kvm(struct kvm *kvm)
 {
-	if (refcount_dec_and_test(&kvm->users_count))
+	if (refcount_dec_and_test(&kvm->rc.users_count))
 		kvm_destroy_vm(kvm);
 }
 EXPORT_SYMBOL_GPL(kvm_put_kvm);
@@ -1341,7 +1326,7 @@ EXPORT_SYMBOL_GPL(kvm_put_kvm);
  */
 void kvm_put_kvm_no_destroy(struct kvm *kvm)
 {
-	WARN_ON(refcount_dec_and_test(&kvm->users_count));
+	WARN_ON(refcount_dec_and_test(&kvm->rc.users_count));
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_put_kvm_no_destroy);
 
@@ -3520,7 +3505,7 @@ void mark_page_dirty_in_slot(struct kvm *kvm,
 	if (WARN_ON_ONCE(vcpu && vcpu->kvm != kvm))
 		return;
 
-	WARN_ON_ONCE(!vcpu && refcount_read(&kvm->users_count) &&
+	WARN_ON_ONCE(!vcpu && refcount_read(&kvm->rc.users_count) &&
 		     !kvm_arch_allow_write_without_running_vcpu(kvm));
 #endif
 
Signature · 1 line
2.53.0
author reviewer
#7 lore raw

PATCH v4 3/27 KVM, vfio: remove symbol_get(kvm_put_kvm) from vfio

In reply to Steffen Eiden

From: Paolo Bonzini <[email protected]>

Right now, KVM and VFIO are using symbol_get to access each other's symbols because of a circular reference between the modules, as well as to avoid loading them unnecessarily.

The remaining use in VFIO is for kvm_put_kvm, which is not inline because it needs to call kvm_destroy_vm. However, storing the address of kvm_destroy_vm in the "struct kvm" is enough to remove the dependency from VFIO.

This also makes it possible to direct kvm_put_kvm to either the arm64 or the s390 implementation of kvm_destroy_vm.

---
 drivers/vfio/vfio_main.c  | 24 +++---------------------
 include/linux/kvm_host.h  |  1 -
 include/linux/kvm_types.h |  9 +++++++++
 include/linux/vfio.h      |  1 -
 virt/kvm/kvm_main.c       |  9 ++-------
 5 files changed, 14 insertions(+), 30 deletions(-)
drivers/vfio/vfio_main.c +3 -21
index b7b8b45f1cec..6bddea4d384f 100644
@@ -17,7 +17,7 @@
 #include <linux/idr.h>
 #include <linux/iommu.h>
 #if IS_ENABLED(CONFIG_KVM)
-#include <linux/kvm_host.h>
+#include <linux/kvm_types.h>
 #endif
 #include <linux/list.h>
 #include <linux/miscdevice.h>
@@ -451,9 +451,6 @@ EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);
 void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm,
 			      struct module *kvm_module)
 {
-	void (*pfn)(struct kvm *kvm);
-	bool ret;
-
 	lockdep_assert_held(&device->dev_set->lock);
 
 	if (!kvm)
@@ -462,21 +459,13 @@ void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm,
 	if (!try_module_get(kvm_module))
 		return;
 
-	pfn = symbol_get(kvm_put_kvm);
-	if (WARN_ON(!pfn))
+	if (!kvm_get_kvm_safe(kvm))
 		goto out_put_mod;
 
-	ret = kvm_get_kvm_safe(kvm);
-	if (!ret)
-		goto out_put_sym;
-
-	device->put_kvm = pfn;
 	device->kvm = kvm;
 	device->kvm_module = kvm_module;
 	return;
 
-out_put_sym:
-	symbol_put(kvm_put_kvm);
 out_put_mod:
 	module_put(kvm_module);
 }
@@ -488,14 +477,7 @@ void vfio_device_put_kvm(struct vfio_device *device)
 	if (!device->kvm)
 		return;
 
-	if (WARN_ON(!device->put_kvm))
-		goto clear;
-
-	device->put_kvm(device->kvm);
-	device->put_kvm = NULL;
-	symbol_put(kvm_put_kvm);
-
-clear:
+	kvm_put_kvm(device->kvm);
 	device->kvm = NULL;
 	module_put(device->kvm_module);
 	device->kvm_module = NULL;
include/linux/kvm_host.h +0 -1
index 015d02018109..1f967b1d68b9 100644
@@ -1072,7 +1072,6 @@ static inline void kvm_irqfd_exit(void)
 int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module);
 void kvm_exit(void);
 
-void kvm_put_kvm(struct kvm *kvm);
 bool file_is_kvm(struct file *file);
 void kvm_put_kvm_no_destroy(struct kvm *kvm);
 
include/linux/kvm_types.h +9 -0
index add7cc2016e8..aadee536771a 100644
@@ -144,6 +144,7 @@ struct kvm_vcpu_stat_generic {
 
 struct kvm_refcount {
 	refcount_t users_count;
+	void (*destroy)(struct kvm *kvm);
 };
 
 static inline void kvm_get_kvm(struct kvm *kvm)
@@ -164,6 +165,14 @@ static inline bool kvm_get_kvm_safe(struct kvm *kvm)
 	return refcount_inc_not_zero(&rc->users_count);
 }
 
+static inline void kvm_put_kvm(struct kvm *kvm)
+{
+	struct kvm_refcount *rc = (struct kvm_refcount *)kvm;
+
+	if (refcount_dec_and_test(&rc->users_count))
+		rc->destroy(kvm);
+}
+
 #endif /* !__ASSEMBLER__ */
 
 #endif /* __KVM_TYPES_H__ */
include/linux/vfio.h +0 -1
index 5bfec0339804..81f04e60ebfd 100644
@@ -67,7 +67,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;
virt/kvm/kvm_main.c +2 -7
index 7dec697ce35a..271884089f58 100644
@@ -119,6 +119,7 @@ static struct dentry *kvm_debugfs_dir;
 
 static const struct file_operations stat_fops_per_vm;
 
+static void kvm_destroy_vm(struct kvm *kvm);
 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
 			   unsigned long arg);
 #ifdef CONFIG_KVM_COMPAT
@@ -1148,6 +1149,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
 
 	BUILD_BUG_ON(offsetof(struct kvm, rc) != 0);
 	refcount_set(&kvm->rc.users_count, 1);
+	kvm->rc.destroy = kvm_destroy_vm;
 
 	for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
 		for (j = 0; j < 2; j++) {
@@ -1310,13 +1312,6 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	mmdrop(mm);
 }
 
-void kvm_put_kvm(struct kvm *kvm)
-{
-	if (refcount_dec_and_test(&kvm->rc.users_count))
-		kvm_destroy_vm(kvm);
-}
-EXPORT_SYMBOL_GPL(kvm_put_kvm);
-
 /*
  * Used to put a reference that was taken on behalf of an object associated
  * with a user-visible file descriptor, e.g. a vcpu or device, if installation
Signature · 1 line
2.53.0
#8 lore raw

Re: [PATCH v4 03/27] KVM, vfio: remove symbol_get(kvm_put_kvm) from vfio

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

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

Pre-existing issues:
- [High] Concurrent or subsequent bindings of cdev file descriptors to the same VFIO device overwrite the device->kvm and device->kvm_module pointers without dropping the previous reference, causing a KVM instance memory leak and premature KVM reference drop.
- [High] The architecture-specific KVM module (e.g., kvm-intel.ko) can be unloaded while VFIO still holds a reference to a KVM instance, leading to execution of freed memory when the instance is eventually destroyed.
--

commit 31b847131145e41ad6d50358b1c90ea462e38022
Author: Paolo Bonzini <[email protected]>

KVM, vfio: remove symbol_get(kvm_put_kvm) from vfio

Right now, KVM and VFIO are using symbol_get to access each other's symbols because of a circular reference between the modules, as well as to avoid loading them unnecessarily.

The remaining use in VFIO is for kvm_put_kvm, which is not inline because it needs to call kvm_destroy_vm. However, storing the address of kvm_destroy_vm in the "struct kvm" is enough to remove the dependency from VFIO.

Quoted 4 lines
drivers/vfio/vfio_main.c +0 -0
index b7b8b45f1cec2..6bddea4d384ff 100644

[ ... ]

drivers/vfio/vfio_main.c void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm, · line 460

Quoted 13 lines
drivers/vfio/vfio_main.c +0 -3
@@ -451,9 +451,6 @@ EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);
 void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm,
 			      struct module *kvm_module)
 {
-	void (*pfn)(struct kvm *kvm);
-	bool ret;
-
 	lockdep_assert_held(&device->dev_set->lock);
 
 	if (!kvm)
@@ -462,21 +459,13 @@ void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm,
 	if (!try_module_get(kvm_module))
 		return;

[Severity: High]
This isn't a bug introduced by this patch, but does pinning only the generic kvm.ko module leave us vulnerable to the architecture-specific module being unloaded?

When VFIO acquires a reference via try_module_get(kvm_module), it doesn't pin the architecture module (e.g., kvm-intel.ko). If userspace closes the KVM fds, the VFS reference count drops to 0, allowing an administrator to unload the arch module.

Since VFIO still holds a reference to the struct kvm, when VFIO later drops its reference, KVM destruction proceeds:

kvm_destroy_vm()

    kvm_arch_pre_destroy_vm(kvm);
    ...
    kvm_arch_destroy_vm(kvm);

Could these calls into the architecture-specific teardown execute unmapped memory if the arch module has already been unloaded?

drivers/vfio/vfio_main.c void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm, · line 466

Quoted 12 lines
drivers/vfio/vfio_main.c +1 -7
-	pfn = symbol_get(kvm_put_kvm);
-	if (WARN_ON(!pfn))
+	if (!kvm_get_kvm_safe(kvm))
 		goto out_put_mod;
 
-	ret = kvm_get_kvm_safe(kvm);
-	if (!ret)
-		goto out_put_sym;
-
-	device->put_kvm = pfn;
 	device->kvm = kvm;
 	device->kvm_module = kvm_module;

[Severity: High]
This is a pre-existing issue, but if multiple cdev file descriptors are opened for the same VFIO device, does this overwrite device->kvm without dropping the previous reference?

For instance, if vfio_df_get_kvm_safe() is called successfully on a second file descriptor, but then vfio_df_open() fails because device->open_count != 0:

drivers/vfio/device_cdev.c:vfio_df_ioctl_bind_iommufd() {
    ...
    vfio_df_get_kvm_safe(df);
    
    ret = vfio_df_open(df);
    if (ret)
        goto out_put_kvm;
    ...
}

Would this leak the first KVM instance reference since device->kvm is unconditionally overwritten here?

Additionally, if the second fd has no KVM instance associated, wouldn't the error path's call to vfio_device_put_kvm() prematurely drop the original reference and clear device->kvm?

Quoted 1 line
drivers/vfio/vfio_main.c +0 -0
 	return;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author reviewer
#9 lore raw

PATCH v4 4/27 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.

---
 include/linux/kvm_host.h |  4 +++-
 virt/kvm/kvm_main.c      | 31 +++++++++++++++++++++++--------
 2 files changed, 26 insertions(+), 9 deletions(-)
include/linux/kvm_host.h +3 -1
index 1f967b1d68b9..aa573e2530f7 100644
@@ -1069,7 +1069,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);
 
 bool file_is_kvm(struct file *file);
virt/kvm/kvm_main.c +23 -8
index 271884089f58..dcad33cc2f25 100644
@@ -5542,9 +5542,7 @@ static struct file_operations kvm_chardev_ops = {
 };
 
 static struct miscdevice kvm_dev = {
-	KVM_MINOR,
-	"kvm",
-	&kvm_chardev_ops,
+	.fops = &kvm_chardev_ops,
 };
 
 #ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING
@@ -6322,13 +6320,13 @@ static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)
 	kfree(env);
 }
 
-static void kvm_init_debug(void)
+static void kvm_init_debug(const char *dev_name)
 {
 	const struct file_operations *fops;
 	const struct kvm_stats_desc *pdesc;
 	int i;
 
-	kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
+	kvm_debugfs_dir = debugfs_create_dir(dev_name, NULL);
 
 	for (i = 0; i < kvm_vm_stats_header.num_desc; ++i) {
 		pdesc = &kvm_vm_stats_desc[i];
@@ -6464,11 +6462,22 @@ void kvm_unregister_perf_callbacks(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)
+{
+	return kvm_init_with_dev(vcpu_size, vcpu_align, module, "kvm", KVM_MINOR);
+}
+
+int kvm_init_with_dev(unsigned int vcpu_size, unsigned int vcpu_align,
+		      struct module *module, const char *dev_name, int minor)
 {
 	int r;
 	int cpu;
 
+	kvm_dev.minor = minor;
+	kvm_dev.name = kstrdup(dev_name, GFP_KERNEL);
+	if (!kvm_dev.name)
+		return -ENOMEM;
+
 	/* A kmem cache lets us meet the alignment requirements of fx_save. */
 	if (!vcpu_align)
 		vcpu_align = __alignof__(struct kvm_vcpu);
@@ -6479,8 +6488,9 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)
 					   offsetofend(struct kvm_vcpu, stats_id)
 					   - offsetof(struct kvm_vcpu, arch),
 					   NULL);
+	r = -ENOMEM;
 	if (!kvm_vcpu_cache)
-		return -ENOMEM;
+		goto err_dev_name;
 
 	for_each_possible_cpu(cpu) {
 		if (!alloc_cpumask_var_node(&per_cpu(cpu_kick_mask, cpu),
@@ -6506,7 +6516,7 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)
 	kvm_preempt_ops.sched_in = kvm_sched_in;
 	kvm_preempt_ops.sched_out = kvm_sched_out;
 
-	kvm_init_debug();
+	kvm_init_debug(kvm_dev.name);
 
 	r = kvm_vfio_ops_init();
 	if (WARN_ON_ONCE(r))
@@ -6547,6 +6557,8 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)
 	for_each_possible_cpu(cpu)
 		free_cpumask_var(per_cpu(cpu_kick_mask, cpu));
 	kmem_cache_destroy(kvm_vcpu_cache);
+err_dev_name:
+	kfree(kvm_dev.name);
 	return r;
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_init);
@@ -6572,5 +6584,8 @@ void kvm_exit(void)
 	kvm_vfio_ops_exit();
 	kvm_async_pf_deinit();
 	kvm_irqfd_exit();
+
+	kfree(kvm_dev.name);
+	kvm_dev.name = NULL;
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_exit);
Signature · 1 line
2.53.0
#10 lore raw

Re: [PATCH v4 04/27] KVM: Make device name configurable

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

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

New issues:
- [Medium] Simultaneous KVM implementations will trigger sysfs duplicate name warnings due to hardcoded kmem cache names.

Pre-existing issues:
- [High] Missing cleanup of kvm_debugfs_dir on module initialization error paths leads to debugfs memory leaks and Use-After-Free vulnerabilities. --

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

KVM: Make device name configurable

Allow KVM implementations to choose alternative device names. This is especially useful for architectures providing multiple KVM implementations simultaneously.

Quoted 4 lines
virt/kvm/kvm_main.c +0 -0
index 271884089f58d..dcad33cc2f255 100644

[ ... ]

Quoted 25 lines
virt/kvm/kvm_main.c +12 -1
@@ -6464,11 +6462,22 @@ void kvm_unregister_perf_callbacks(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)
+{
+	return kvm_init_with_dev(vcpu_size, vcpu_align, module, "kvm", KVM_MINOR);
+}
+
+int kvm_init_with_dev(unsigned int vcpu_size, unsigned int vcpu_align,
+		      struct module *module, const char *dev_name, int minor)
 {
 	int r;
 	int cpu;
 
+	kvm_dev.minor = minor;
+	kvm_dev.name = kstrdup(dev_name, GFP_KERNEL);
+	if (!kvm_dev.name)
+		return -ENOMEM;
+
 	/* A kmem cache lets us meet the alignment requirements of fx_save. */
 	if (!vcpu_align)
 		vcpu_align = __alignof__(struct kvm_vcpu);

virt/kvm/kvm_main.c int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) · line 6490

Quoted 3 lines
virt/kvm/kvm_main.c +0 -0
@@ -6479,8 +6488,9 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)
 					   offsetofend(struct kvm_vcpu, stats_id)
 					   - offsetof(struct kvm_vcpu, arch),
 					   NULL);

[Severity: Medium]
Since the goal is to allow multiple KVM implementations simultaneously, will the hardcoded "kvm_vcpu" string passed to kmem_cache_create_usercopy() in kvm_init_with_dev() cause a sysfs duplicate name warning?

If a second KVM implementation is loaded, SLUB might attempt to create a duplicate /sys/kernel/slab/kvm_vcpu sysfs directory, triggering a warning that could crash systems with panic_on_warn enabled. Does this need to use the configurable dev_name instead?

Quoted 4 lines
virt/kvm/kvm_main.c +2 -1
+	r = -ENOMEM;
 	if (!kvm_vcpu_cache)
-		return -ENOMEM;
+		goto err_dev_name;

[ ... ]

virt/kvm/kvm_main.c int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) · line 6563

Quoted 8 lines
virt/kvm/kvm_main.c +2 -0
@@ -6547,6 +6557,8 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)
 	for_each_possible_cpu(cpu)
 		free_cpumask_var(per_cpu(cpu_kick_mask, cpu));
 	kmem_cache_destroy(kvm_vcpu_cache);
+err_dev_name:
+	kfree(kvm_dev.name);
 	return r;
 }

[Severity: High]
This is a pre-existing issue, but is there a missing cleanup of kvm_debugfs_dir in the error handling path of kvm_init_with_dev()?

If initialization fails during a late step like misc_register, the error unwinding labels are executed, but debugfs_remove_recursive() is never called for the debugfs directory created earlier by kvm_init_debug().

Since the initialization fails and the module memory is freed, could accessing the abandoned debugfs files later cause a use-after-free when the kernel tries to dereference invalid fops pointers?

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

PATCH v4 5/27 KVM: Remove KVM_MMIO as config option

In reply to Steffen Eiden

Defining KVM_MMIO is not flexible enough for multi-KVM systems with different Kconfig options regarding KVM_MMIO. Therefore, remove KVM_MMIO from the config space and use the macro HAVE_KVM_MMIO instead.

---
 arch/arm64/include/asm/kvm_host.h     | 2 ++
 arch/arm64/kvm/Kconfig                | 1 -
 arch/loongarch/include/asm/kvm_host.h | 2 ++
 arch/loongarch/kvm/Kconfig            | 1 -
 arch/mips/include/asm/kvm_host.h      | 2 ++
 arch/mips/kvm/Kconfig                 | 1 -
 arch/powerpc/include/asm/kvm_host.h   | 7 +++++++
 arch/powerpc/kvm/Kconfig              | 4 ----
 arch/riscv/include/asm/kvm_host.h     | 2 ++
 arch/riscv/kvm/Kconfig                | 1 -
 arch/x86/include/asm/kvm_host.h       | 2 ++
 arch/x86/kvm/Kconfig                  | 1 -
 include/linux/kvm_host.h              | 2 +-
 virt/kvm/Kconfig                      | 3 ---
 virt/kvm/Makefile.kvm                 | 3 +--
 virt/kvm/coalesced_mmio.c             | 3 +++
 virt/kvm/coalesced_mmio.h             | 2 +-
 virt/kvm/kvm_main.c                   | 8 ++++----
 18 files changed, 27 insertions(+), 20 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -0
index bae2c4f92ef5..245bda0fb571 100644
@@ -33,6 +33,8 @@
 
 #define KVM_HALT_POLL_NS_DEFAULT 500000
 
+#define HAVE_KVM_MMIO
+
 #include <kvm/arm_vgic.h>
 #include <kvm/arm_arch_timer.h>
 #include <kvm/arm_pmu.h>
arch/arm64/kvm/Kconfig +0 -1
index 449154f9a485..dd138b730217 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/include/asm/kvm_host.h +2 -0
index 23cfbecebbd7..47003a85b35a 100644
@@ -27,6 +27,8 @@
 
 #define __KVM_HAVE_ARCH_INTC_INITIALIZED
 
+#define HAVE_KVM_MMIO
+
 /* Loongarch KVM register ids */
 #define KVM_GET_IOC_CSR_IDX(id)		((id & KVM_CSR_IDX_MASK) >> LOONGARCH_REG_SHIFT)
 #define KVM_GET_IOC_CPUCFG_IDX(id)	((id & KVM_CPUCFG_IDX_MASK) >> LOONGARCH_REG_SHIFT)
arch/loongarch/kvm/Kconfig +0 -1
index 15da2d88c0c1..f03872ba0ba8 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/include/asm/kvm_host.h +2 -0
index c14b10821817..e3cb7c8c9461 100644
@@ -26,6 +26,8 @@
 
 #include <kvm/iodev.h>
 
+#define HAVE_KVM_MMIO
+
 /* MIPS KVM register ids */
 #define MIPS_CP0_32(_R, _S)					\
 	(KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U32 | (8 * (_R) + (_S)))
arch/mips/kvm/Kconfig +0 -1
index b1b9a1d67758..c65596fdf54f 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/include/asm/kvm_host.h +7 -0
index 2d139c807577..92d14438e31b 100644
@@ -29,6 +29,13 @@
 
 #define __KVM_HAVE_ARCH_VCPU_DEBUGFS
 
+#if defined(CONFIG_KVM_BOOK3S_32_HANDLER) || \
+	defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE) || \
+	defined(CONFIG_KVM_E500V2) || \
+	defined(CONFIG_KVM_E500MC)
+#define HAVE_KVM_MMIO
+#endif
+
 #define KVM_MAX_VCPUS		NR_CPUS
 #define KVM_MAX_VCORES		NR_CPUS
 
arch/powerpc/kvm/Kconfig +0 -4
index 9a0d1c1aca6c..d97a4a51ec55 100644
@@ -29,7 +29,6 @@ config KVM_BOOK3S_HANDLER
 config KVM_BOOK3S_32_HANDLER
 	bool
 	select KVM_BOOK3S_HANDLER
-	select KVM_MMIO
 
 config KVM_BOOK3S_64_HANDLER
 	bool
@@ -37,7 +36,6 @@ config KVM_BOOK3S_64_HANDLER
 
 config KVM_BOOK3S_PR_POSSIBLE
 	bool
-	select KVM_MMIO
 
 config KVM_BOOK3S_HV_POSSIBLE
 	bool
@@ -200,7 +198,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.
@@ -215,7 +212,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/include/asm/kvm_host.h +2 -0
index 60017ceec9d2..38333a01e5bb 100644
@@ -26,6 +26,8 @@
 #include <asm/kvm_vcpu_timer.h>
 #include <asm/kvm_vcpu_pmu.h>
 
+#define HAVE_KVM_MMIO
+
 #define KVM_MAX_VCPUS			1024
 
 #define KVM_HALT_POLL_NS_DEFAULT	500000
arch/riscv/kvm/Kconfig +0 -1
index ec2cee0a39e0..3aea8d4939d2 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/x86/include/asm/kvm_host.h +2 -0
index 5f6c1ce9673b..69a374e48fe7 100644
@@ -46,6 +46,8 @@
 
 #define __KVM_HAVE_ARCH_VCPU_DEBUGFS
 
+#define HAVE_KVM_MMIO
+
 /*
  * CONFIG_KVM_MAX_NR_VCPUS is defined iff CONFIG_KVM!=n, provide a dummy max if
  * KVM is disabled (arbitrarily use the default from CONFIG_KVM_MAX_NR_VCPUS).
arch/x86/kvm/Kconfig +0 -1
index 801bf9e520db..da5d9fa8a11c 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 aa573e2530f7..f179a9230e30 100644
@@ -834,7 +834,7 @@ struct kvm {
 	struct list_head ioeventfds;
 	struct kvm_vm_stat stat;
 	struct kvm_arch arch;
-#ifdef CONFIG_KVM_MMIO
+#ifdef HAVE_KVM_MMIO
 	struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
 	spinlock_t ring_lock;
 	struct list_head coalesced_zones;
virt/kvm/Kconfig +0 -3
index 794976b88c6f..c4a983e41765 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 d047d4cf58c9..3c27c18b49a0 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)/binary_stats.o $(KVM)/coalesced_mmio.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 6b1d90161099..070eba3c6534 100644
@@ -12,6 +12,8 @@
 #include <kvm/iodev.h>
 
 #include <linux/kvm_host.h>
+
+#ifdef HAVE_KVM_MMIO
 #include <linux/slab.h>
 #include <linux/kvm.h>
 
@@ -188,3 +190,4 @@ int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
 	 */
 	return 0;
 }
+#endif /* ifdef HAVE_KVM_MMIO */
virt/kvm/coalesced_mmio.h +1 -1
index 36f84264ed25..a8430d41945b 100644
@@ -11,7 +11,7 @@
  *
  */
 
-#ifdef CONFIG_KVM_MMIO
+#ifdef HAVE_KVM_MMIO
 
 #include <linux/list.h>
 
virt/kvm/kvm_main.c +4 -4
index dcad33cc2f25..6ac79d80e7ec 100644
@@ -4035,7 +4035,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
+#ifdef HAVE_KVM_MMIO
 	else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
 		page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
 #endif
@@ -4861,7 +4861,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
+#ifdef HAVE_KVM_MMIO
 	case KVM_CAP_COALESCED_MMIO:
 		return KVM_COALESCED_MMIO_PAGE_OFFSET;
 	case KVM_CAP_COALESCED_PIO:
@@ -5202,7 +5202,7 @@ static long kvm_vm_ioctl(struct file *filp,
 		break;
 	}
 #endif
-#ifdef CONFIG_KVM_MMIO
+#ifdef HAVE_KVM_MMIO
 	case KVM_REGISTER_COALESCED_MMIO: {
 		struct kvm_coalesced_mmio_zone zone;
 
@@ -5524,7 +5524,7 @@ static long kvm_dev_ioctl(struct file *filp,
 #ifdef CONFIG_X86
 		r += PAGE_SIZE;    /* pio data page */
 #endif
-#ifdef CONFIG_KVM_MMIO
+#ifdef HAVE_KVM_MMIO
 		r += PAGE_SIZE;    /* coalesced mmio ring page */
 #endif
 		break;
Signature · 1 line
2.53.0
maintainer
#12 lore raw

Re: [PATCH v4 05/27] KVM: Remove KVM_MMIO as config option

Quoted 5 lines · Steffen Eiden

Defining KVM_MMIO is not flexible enough for multi-KVM systems with different Kconfig options regarding KVM_MMIO. Therefore, remove KVM_MMIO from the config space and use the macro HAVE_KVM_MMIO instead.

Given that there is only one KVM architecture that doesn't use KVM_MMIO, it feels like the polarity of this symbol is the wrong way around.

Something like KVM_NO_MMIO, only exposed by S390 for its native support, should significantly reduce the churn.

Thanks,

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

Re: [PATCH v4 05/27] KVM: Remove KVM_MMIO as config option

Quoted 8 lines · Marc Zyngier
Quoted 5 lines · Steffen Eiden

Defining KVM_MMIO is not flexible enough for multi-KVM systems with different Kconfig options regarding KVM_MMIO. Therefore, remove KVM_MMIO from the config space and use the macro HAVE_KVM_MMIO instead.

Quoted 6 lines

Given that there is only one KVM architecture that doesn't use KVM_MMIO, it feels like the polarity of this symbol is the wrong way around.

Something like KVM_NO_MMIO, only exposed by S390 for its native support, should significantly reduce the churn.

Makes sense. I'll add KVM_NO_MMIO and conitionally compile based on the config and not havng this symbol

Steffen

author reviewer
#14 lore raw

PATCH v4 6/27 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
author reviewer
#15 lore raw

PATCH v4 7/27 arm64: Prepare sharing arm64 headers with s390

In reply to Steffen Eiden

Prepare the sharing of arm64 headers with s390 by marking the shared regions or add a comment that the whole file is shared. The regions are marked with:

#define ARM64_S390_COMMON

/* insert shared definitions here */

#endif /* ARM64_S390_COMMON */

The preprocessor symbol ARM64_S390_COMMON is always defined for arm64. Reduce the include scope where possible and beneficial for s390. No functional change.

---
 arch/arm64/Makefile                  |  2 ++
 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 | 19 ++++++++++++++++++
 arch/arm64/include/asm/kvm_host.h    | 28 +++++++++++++++++++++++++++
 arch/arm64/include/asm/kvm_mmu.h     |  6 ++++++
 arch/arm64/include/asm/ptrace.h      |  9 +++++++++
 arch/arm64/include/asm/sysreg.h      | 29 +++++++++++++++++++++++++---
 9 files changed, 98 insertions(+), 7 deletions(-)
arch/arm64/Makefile +2 -0
index 6b005c8fef70..1cd7cd94a855 100644
@@ -43,7 +43,9 @@ CC_FLAGS_NO_FPU	:= -mgeneral-regs-only
 KBUILD_CFLAGS	+= $(CC_FLAGS_NO_FPU) \
 		   $(compat_vdso) $(cc_has_k_constraint)
 KBUILD_CFLAGS	+= $(call cc-disable-warning, psabi)
+KBUILD_CFLAGS	+= -DARM64_S390_COMMON
 KBUILD_AFLAGS	+= $(compat_vdso)
+KBUILD_AFLAGS	+= -DARM64_S390_COMMON
 
 ifeq ($(call rustc-min-version, 108500),y)
 KBUILD_RUSTFLAGS += --target=aarch64-unknown-none-softfloat
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 +3 -2
index 81c17320a588..e25b742b09ac 100644
@@ -3,11 +3,12 @@
  * Copyright (C) 2013 - ARM Ltd
  * Author: Marc Zyngier <[email protected]>
  */
+/* Whole file is shared with s390 */
 
 #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 +436,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 +4 -2
index 3f9233b5a130..cc4175176f94 100644
@@ -3,14 +3,16 @@
  * 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__
 
+#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 +19 -0
index 5bf3d7e1d92c..a1c92d2436ae 100644
@@ -44,13 +44,18 @@ enum exception_type {
 bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
 void kvm_skip_instr32(struct kvm_vcpu *vcpu);
 
+#ifdef ARM64_S390_COMMON
 void kvm_inject_undefined(struct kvm_vcpu *vcpu);
+#endif /* ARM64_S390_COMMON */
 void kvm_inject_sync(struct kvm_vcpu *vcpu, u64 esr);
 int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr);
+#ifdef ARM64_S390_COMMON
 int kvm_inject_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr);
+#endif /* ARM64_S390_COMMON */
 int kvm_inject_dabt_excl_atomic(struct kvm_vcpu *vcpu, u64 addr);
 void kvm_inject_size_fault(struct kvm_vcpu *vcpu);
 
+#ifdef ARM64_S390_COMMON
 static inline int kvm_inject_sea_dabt(struct kvm_vcpu *vcpu, u64 addr)
 {
 	return kvm_inject_sea(vcpu, false, addr);
@@ -61,6 +66,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)
 {
 	/*
@@ -159,6 +166,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
@@ -177,6 +185,8 @@ static __always_inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
 		vcpu_gp_regs(vcpu)->regs[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)) {
@@ -360,6 +370,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;
@@ -437,6 +448,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)
 {
@@ -477,6 +490,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)) {
@@ -501,6 +515,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;
@@ -536,6 +552,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)
@@ -611,6 +628,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.
arch/arm64/include/asm/kvm_host.h +28 -0
index 245bda0fb571..ae9f76378218 100644
@@ -41,6 +41,7 @@
 
 #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS
 
+#ifdef ARM64_S390_COMMON
 #define KVM_VCPU_MAX_FEATURES 9
 #define KVM_VCPU_VALID_FEATURES	(BIT(KVM_VCPU_MAX_FEATURES) - 1)
 
@@ -58,6 +59,8 @@
 #define KVM_REQ_MAP_L1_VNCR_EL2		KVM_ARCH_REQ(10)
 #define KVM_REQ_VGIC_PROCESS_UPDATE	KVM_ARCH_REQ(11)
 
+#endif /* ARM64_S390_COMMON */
+
 #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
 				     KVM_DIRTY_LOG_INITIALLY_SET)
 
@@ -340,6 +343,7 @@ struct kvm_arch {
 	/* Protects VM-scoped configuration data */
 	struct mutex config_lock;
 
+#ifdef ARM64_S390_COMMON
 	/*
 	 * If we encounter a data abort without valid instruction syndrome
 	 * information, report this to user space.  User space can (and
@@ -369,6 +373,8 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS		10
 	/* Unhandled SEAs are taken to userspace */
 #define KVM_ARCH_FLAG_EXIT_SEA				11
+
+#endif /* ARM64_S390_COMMON */
 	unsigned long flags;
 
 	/* VM-wide vCPU feature set */
@@ -833,6 +839,8 @@ extern s64 kvm_nvhe_sym(hyp_physvirt_offset);
 extern u64 kvm_nvhe_sym(hyp_cpu_logical_map)[NR_CPUS];
 #define hyp_cpu_logical_map CHOOSE_NVHE_SYM(hyp_cpu_logical_map)
 
+#ifdef ARM64_S390_COMMON
+
 struct vcpu_reset_state {
 	unsigned long	pc;
 	unsigned long	r0;
@@ -840,6 +848,8 @@ struct vcpu_reset_state {
 	bool		reset;
 };
 
+#endif /* ARM64_S390_COMMON */
+
 struct vncr_tlb;
 
 struct kvm_vcpu_arch {
@@ -949,6 +959,7 @@ struct kvm_vcpu_arch {
 	pid_t pid;
 };
 
+#ifdef ARM64_S390_COMMON
 /*
  * Each 'flag' is composed of a comma-separated triplet:
  *
@@ -982,6 +993,8 @@ struct kvm_vcpu_arch {
 		READ_ONCE(v->arch.flagset) & (m);		\
 	})
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * Note that the set/clear accessors must be preempt-safe in order to
  * avoid nesting them with load/put which also manipulate flags...
@@ -995,6 +1008,7 @@ struct kvm_vcpu_arch {
 #define __vcpu_flags_preempt_enable()	preempt_enable()
 #endif
 
+#ifdef ARM64_S390_COMMON
 #define __vcpu_set_flag(v, flagset, f, m)			\
 	do {							\
 		typeof(v->arch.flagset) *fset;			\
@@ -1079,6 +1093,8 @@ struct kvm_vcpu_arch {
 #define EXCEPT_AA64_EL2_FIQ	__vcpu_except_flags(6)
 #define EXCEPT_AA64_EL2_SERR	__vcpu_except_flags(7)
 
+#endif /* ARM64_S390_COMMON */
+
 /* Physical CPU not in supported_cpus */
 #define ON_UNSUPPORTED_CPU	__vcpu_single_flag(sflags, BIT(0))
 /* WFIT instruction trapped */
@@ -1236,7 +1252,10 @@ struct kvm_vcpu_stat {
 };
 
 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
+#ifdef ARM64_S390_COMMON
 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
+
+#endif /* ARM64_S390_COMMON */
 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
 int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
 
@@ -1321,6 +1340,7 @@ int __init populate_nv_trap_config(void);
 
 void kvm_calculate_traps(struct kvm_vcpu *vcpu);
 
+#ifdef ARM64_S390_COMMON
 /* MMIO helpers */
 void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data);
 unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len);
@@ -1328,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
@@ -1502,8 +1524,11 @@ struct kvm *kvm_arch_alloc_vm(void);
 
 #define kvm_vm_is_protected(kvm)	(is_protected_kvm_enabled() && (kvm)->arch.pkvm.is_protected)
 
+#ifdef ARM64_S390_COMMON
 #define vcpu_is_protected(vcpu)		kvm_vm_is_protected((vcpu)->kvm)
 
+#endif /* ARM64_S390_COMMON */
+
 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
 bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
 
@@ -1528,8 +1553,11 @@ static inline bool __vcpu_has_feature(const struct kvm_arch *ka, int feature)
 #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))
 
+#ifdef ARM64_S390_COMMON
 #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/kvm_mmu.h +6 -0
index 6eae7e7e2a68..b782cae771fe 100644
@@ -142,12 +142,15 @@ static __always_inline unsigned long __kern_hyp_va(unsigned long v)
 
 extern u32 __hyp_va_bits;
 
+#ifdef ARM64_S390_COMMON
 /*
  * We currently support using a VM-specified IPA size. For backward
  * compatibility, the default IPA size is fixed to 40bits.
  */
 #define KVM_PHYS_SHIFT	(40)
 
+#endif /* ARM64_S390_COMMON */
+
 #define kvm_phys_shift(mmu)		VTCR_EL2_IPA((mmu)->vtcr)
 #define kvm_phys_size(mmu)		(_AC(1, ULL) << kvm_phys_shift(mmu))
 #define kvm_phys_mask(mmu)		(kvm_phys_size(mmu) - _AC(1, ULL))
@@ -178,9 +181,12 @@ void stage2_unmap_vm(struct kvm *kvm);
 int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long type);
 void kvm_uninit_stage2_mmu(struct kvm *kvm);
 void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu);
+#ifdef ARM64_S390_COMMON
 int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 			  phys_addr_t pa, unsigned long size, bool writable);
 
+#endif /* ARM64_S390_COMMON */
+
 int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
 int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
 
arch/arm64/include/asm/ptrace.h +9 -0
index 39582511ad72..6eb122eb5fa6 100644
@@ -10,8 +10,13 @@
 
 #include <asm/cpufeature.h>
 
+#ifdef ARM64_S390_COMMON
+#include <asm/sysreg.h>
+
 #include <uapi/asm/ptrace.h>
 
+#endif /* ARM64_S390_COMMON */
+
 /* Current Exception Level values, as contained in CurrentEL */
 #define CurrentEL_EL1		(1 << 2)
 #define CurrentEL_EL2		(2 << 2)
@@ -28,6 +33,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)
@@ -68,6 +75,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 +26 -3
index 4bfdac9401bd..58912a6b6fcd 100644
@@ -9,13 +9,17 @@
 #ifndef __ASM_SYSREG_H
 #define __ASM_SYSREG_H
 
+#ifdef ARM64_S390_COMMON
 #include <linux/bits.h>
+#endif /* ARM64_S390_COMMON */
 #include <linux/stringify.h>
 #include <linux/kasan-tags.h>
 #include <linux/kconfig.h>
 
 #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 +54,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__
@@ -79,6 +85,8 @@
 
 #endif	/* CONFIG_BROKEN_GAS_INST */
 
+#ifdef ARM64_S390_COMMON
+
 /*
  * Instructions for modifying PSTATE fields.
  * As per Arm ARM for v8-A, Section "C.5.1.3 op0 == 0b00, architectural hints,
@@ -91,8 +99,6 @@
  */
 #define pstate_field(op1, op2)		((op1) << Op1_shift | (op2) << Op2_shift)
 #define PSTATE_Imm_shift		CRm_shift
-#define ENCODE_PSTATE(x, r)		(0xd500401f | PSTATE_ ## r | ((!!x) << PSTATE_Imm_shift))
-#define SET_PSTATE(x, r)		__emit_inst(ENCODE_PSTATE(x, r))
 
 #define PSTATE_PAN			pstate_field(0, 4)
 #define PSTATE_UAO			pstate_field(0, 3)
@@ -100,6 +106,11 @@
 #define PSTATE_DIT			pstate_field(3, 2)
 #define PSTATE_TCO			pstate_field(3, 4)
 
+#endif /* ARM64_S390_COMMON */
+
+#define ENCODE_PSTATE(x, r)		(0xd500401f | PSTATE_ ## r | ((!!x) << PSTATE_Imm_shift))
+#define SET_PSTATE(x, r)		__emit_inst(ENCODE_PSTATE(x, r))
+
 #define SET_PSTATE_PAN(x)		SET_PSTATE((x), PAN)
 #define SET_PSTATE_UAO(x)		SET_PSTATE((x), UAO)
 #define SET_PSTATE_SSBS(x)		SET_PSTATE((x), SSBS)
@@ -123,6 +134,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)
@@ -835,6 +848,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
@@ -869,6 +884,8 @@
 	 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)
@@ -1105,6 +1122,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)
 
@@ -1254,6 +1273,8 @@
 	par;								\
 })
 
+#ifdef ARM64_S390_COMMON
+
 #define SYS_FIELD_VALUE(reg, field, val)	reg##_##field##_##val
 
 #define SYS_FIELD_GET(reg, field, val)		\
@@ -1266,6 +1287,8 @@
 		 FIELD_PREP(reg##_##field##_MASK,	\
 			    SYS_FIELD_VALUE(reg, field, val))
 
-#endif
+#endif /* ARM64_S390_COMMON */
+
+#endif /* __ASSEMBLER__ */
 
 #endif	/* __ASM_SYSREG_H */
Signature · 1 line
2.53.0
maintainer
#16 lore raw

Re: [PATCH v4 07/27] arm64: Prepare sharing arm64 headers with s390

Quoted 196 lines · Steffen Eiden

Prepare the sharing of arm64 headers with s390 by marking the shared regions or add a comment that the whole file is shared. The regions are marked with:

#define ARM64_S390_COMMON

/* insert shared definitions here */

#endif /* ARM64_S390_COMMON */

The preprocessor symbol ARM64_S390_COMMON is always defined for arm64. Reduce the include scope where possible and beneficial for s390. No functional change.

---
 arch/arm64/Makefile                  |  2 ++
 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 | 19 ++++++++++++++++++
 arch/arm64/include/asm/kvm_host.h    | 28 +++++++++++++++++++++++++++
 arch/arm64/include/asm/kvm_mmu.h     |  6 ++++++
 arch/arm64/include/asm/ptrace.h      |  9 +++++++++
 arch/arm64/include/asm/sysreg.h      | 29 +++++++++++++++++++++++++---
 9 files changed, 98 insertions(+), 7 deletions(-)
arch/arm64/Makefile +2 -0
index 6b005c8fef70..1cd7cd94a855 100644
@@ -43,7 +43,9 @@ CC_FLAGS_NO_FPU	:= -mgeneral-regs-only
 KBUILD_CFLAGS	+= $(CC_FLAGS_NO_FPU) \
 		   $(compat_vdso) $(cc_has_k_constraint)
 KBUILD_CFLAGS	+= $(call cc-disable-warning, psabi)
+KBUILD_CFLAGS	+= -DARM64_S390_COMMON
 KBUILD_AFLAGS	+= $(compat_vdso)
+KBUILD_AFLAGS	+= -DARM64_S390_COMMON
 
 ifeq ($(call rustc-min-version, 108500),y)
 KBUILD_RUSTFLAGS += --target=aarch64-unknown-none-softfloat
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 +3 -2
index 81c17320a588..e25b742b09ac 100644
@@ -3,11 +3,12 @@
  * Copyright (C) 2013 - ARM Ltd
  * Author: Marc Zyngier <[email protected]>
  */
+/* Whole file is shared with s390 */
 
 #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 +436,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 +4 -2
index 3f9233b5a130..cc4175176f94 100644
@@ -3,14 +3,16 @@
  * 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__
 
+#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 +17 -0
index 5bf3d7e1d92c..a1c92d2436ae 100644
@@ -44,13 +44,18 @@ enum exception_type {
 bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
 void kvm_skip_instr32(struct kvm_vcpu *vcpu);
 
+#ifdef ARM64_S390_COMMON
 void kvm_inject_undefined(struct kvm_vcpu *vcpu);
+#endif /* ARM64_S390_COMMON */
 void kvm_inject_sync(struct kvm_vcpu *vcpu, u64 esr);
 int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr);
+#ifdef ARM64_S390_COMMON
 int kvm_inject_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr);
+#endif /* ARM64_S390_COMMON */
 int kvm_inject_dabt_excl_atomic(struct kvm_vcpu *vcpu, u64 addr);
 void kvm_inject_size_fault(struct kvm_vcpu *vcpu);
 
+#ifdef ARM64_S390_COMMON
 static inline int kvm_inject_sea_dabt(struct kvm_vcpu *vcpu, u64 addr)
 {
 	return kvm_inject_sea(vcpu, false, addr);
@@ -61,6 +66,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)
 {
 	/*
@@ -159,6 +166,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
@@ -177,6 +185,8 @@ static __always_inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
 		vcpu_gp_regs(vcpu)->regs[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)) {
@@ -360,6 +370,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;
@@ -437,6 +448,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)
 {
@@ -477,6 +490,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)) {
@@ -501,6 +515,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;
@@ -536,6 +552,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)

arch/arm64/include/asm/kvm_emulate.h static __always_inline void kvm_incr_pc(struct kvm_vcpu *vcpu) · line 632

Quoted 5 lines
arch/arm64/include/asm/kvm_emulate.h +2 -0
@@ -611,6 +628,8 @@ static __always_inline void kvm_incr_pc(struct kvm_vcpu *vcpu)
 		vcpu_set_flag((v), e);					\
 	} while (0)
 
+#endif /* ARM64_S390_COMMON */
+

It'd be worth looking at an initial patch reorganising this file to minimise the number of such markers. This is equally valid for other files in this patch.

[...]

Quoted 21 lines
arch/arm64/include/asm/kvm_mmu.h +3 -0
index 6eae7e7e2a68..b782cae771fe 100644
@@ -142,12 +142,15 @@ static __always_inline unsigned long __kern_hyp_va(unsigned long v)
 
 extern u32 __hyp_va_bits;
 
+#ifdef ARM64_S390_COMMON
 /*
  * We currently support using a VM-specified IPA size. For backward
  * compatibility, the default IPA size is fixed to 40bits.
  */
 #define KVM_PHYS_SHIFT	(40)
 
+#endif /* ARM64_S390_COMMON */
+
 #define kvm_phys_shift(mmu)		VTCR_EL2_IPA((mmu)->vtcr)
 #define kvm_phys_size(mmu)		(_AC(1, ULL) << kvm_phys_shift(mmu))
 #define kvm_phys_mask(mmu)		(kvm_phys_size(mmu) - _AC(1, ULL))

arch/arm64/include/asm/kvm_mmu.h void stage2_unmap_vm(struct kvm *kvm); · line 186

Quoted 6 lines
arch/arm64/include/asm/kvm_mmu.h +1 -0
@@ -178,9 +181,12 @@ void stage2_unmap_vm(struct kvm *kvm);
 int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long type);
 void kvm_uninit_stage2_mmu(struct kvm *kvm);
 void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu);
+#ifdef ARM64_S390_COMMON
 int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 			  phys_addr_t pa, unsigned long size, bool writable);

This only serves a purpose for GICv2, and I really hope your HW will not implement this. You should probably find a way to avoid dragging stuff that doesn't make sense on s390.

Quoted 104 lines
arch/arm64/include/asm/kvm_mmu.h +2 -0
+#endif /* ARM64_S390_COMMON */
+
 int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
 int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
 
arch/arm64/include/asm/ptrace.h +9 -0
index 39582511ad72..6eb122eb5fa6 100644
@@ -10,8 +10,13 @@
 
 #include <asm/cpufeature.h>
 
+#ifdef ARM64_S390_COMMON
+#include <asm/sysreg.h>
+
 #include <uapi/asm/ptrace.h>
 
+#endif /* ARM64_S390_COMMON */
+
 /* Current Exception Level values, as contained in CurrentEL */
 #define CurrentEL_EL1		(1 << 2)
 #define CurrentEL_EL2		(2 << 2)
@@ -28,6 +33,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)
@@ -68,6 +75,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 +13 -2
index 4bfdac9401bd..58912a6b6fcd 100644
@@ -9,13 +9,17 @@
 #ifndef __ASM_SYSREG_H
 #define __ASM_SYSREG_H
 
+#ifdef ARM64_S390_COMMON
 #include <linux/bits.h>
+#endif /* ARM64_S390_COMMON */
 #include <linux/stringify.h>
 #include <linux/kasan-tags.h>
 #include <linux/kconfig.h>
 
 #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 +54,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__
@@ -79,6 +85,8 @@
 
 #endif	/* CONFIG_BROKEN_GAS_INST */
 
+#ifdef ARM64_S390_COMMON
+
 /*
  * Instructions for modifying PSTATE fields.
  * As per Arm ARM for v8-A, Section "C.5.1.3 op0 == 0b00, architectural hints,
@@ -91,8 +99,6 @@
  */
 #define pstate_field(op1, op2)		((op1) << Op1_shift | (op2) << Op2_shift)
 #define PSTATE_Imm_shift		CRm_shift
-#define ENCODE_PSTATE(x, r)		(0xd500401f | PSTATE_ ## r | ((!!x) << PSTATE_Imm_shift))
-#define SET_PSTATE(x, r)		__emit_inst(ENCODE_PSTATE(x, r))
 
 #define PSTATE_PAN			pstate_field(0, 4)
 #define PSTATE_UAO			pstate_field(0, 3)
@@ -100,6 +106,11 @@
 #define PSTATE_DIT			pstate_field(3, 2)
 #define PSTATE_TCO			pstate_field(3, 4)
 
+#endif /* ARM64_S390_COMMON */
+
+#define ENCODE_PSTATE(x, r)		(0xd500401f | PSTATE_ ## r | ((!!x) << PSTATE_Imm_shift))
+#define SET_PSTATE(x, r)		__emit_inst(ENCODE_PSTATE(x, r))
+
 #define SET_PSTATE_PAN(x)		SET_PSTATE((x), PAN)
 #define SET_PSTATE_UAO(x)		SET_PSTATE((x), UAO)
 #define SET_PSTATE_SSBS(x)		SET_PSTATE((x), SSBS)

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

Quoted 2 lines
arch/arm64/include/asm/sysreg.h +0 -0
@@ -123,6 +134,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)

What is the reason to avoid dragging these definitions? They seem extremely similar to the rest.

Quoted 43 lines
arch/arm64/include/asm/sysreg.h +10 -0
+#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)
@@ -835,6 +848,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
@@ -869,6 +884,8 @@
 	 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)
@@ -1105,6 +1122,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)
 
@@ -1254,6 +1273,8 @@
 	par;								\
 })
 
+#ifdef ARM64_S390_COMMON
+
 #define SYS_FIELD_VALUE(reg, field, val)	reg##_##field##_##val
 
 #define SYS_FIELD_GET(reg, field, val)		\

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

Quoted 9 lines
arch/arm64/include/asm/sysreg.h +3 -1
@@ -1266,6 +1287,8 @@
 		 FIELD_PREP(reg##_##field##_MASK,	\
 			    SYS_FIELD_VALUE(reg, field, val))
 
-#endif
+#endif /* ARM64_S390_COMMON */
+
+#endif /* __ASSEMBLER__ */
 
 #endif	/* __ASM_SYSREG_H */

Overall, the fine-grained tagging is a bit hard to read, and there is no obvious rationale on why some bits are needed, and not others are not.

It definitely looks more manageable than the previous iteration, but as I said above, it would benefit from a less scattergun approach.

Thanks,

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

Re: [PATCH v4 07/27] arm64: Prepare sharing arm64 headers with s390

Quoted 30 lines · Marc Zyngier
Quoted 28 lines · Steffen Eiden

Prepare the sharing of arm64 headers with s390 by marking the shared regions or add a comment that the whole file is shared. The regions are marked with:

#define ARM64_S390_COMMON

/* insert shared definitions here */

#endif /* ARM64_S390_COMMON */

The preprocessor symbol ARM64_S390_COMMON is always defined for arm64. Reduce the include scope where possible and beneficial for s390. No functional change.

---
 arch/arm64/Makefile                  |  2 ++
 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 | 19 ++++++++++++++++++
 arch/arm64/include/asm/kvm_host.h    | 28 +++++++++++++++++++++++++++
 arch/arm64/include/asm/kvm_mmu.h     |  6 ++++++
 arch/arm64/include/asm/ptrace.h      |  9 +++++++++
 arch/arm64/include/asm/sysreg.h      | 29 +++++++++++++++++++++++++---
 9 files changed, 98 insertions(+), 7 deletions(-)

...

Quoted 12 lines

arch/arm64/include/asm/kvm_emulate.h static __always_inline void kvm_incr_pc(struct kvm_vcpu *vcpu) · line 632

Quoted 5 lines
arch/arm64/include/asm/kvm_emulate.h +2 -0
 		vcpu_set_flag((v), e);					\
 	} while (0)
 
+#endif /* ARM64_S390_COMMON */
+

It'd be worth looking at an initial patch reorganising this file to minimise the number of such markers. This is equally valid for other files in this patch.

[...]

So it is fine if I reorganize files a bit to reduce the number of markes?
I feared it would not. I will reorganize the files a bit then if this reduces the number of sections. Also if some thiings are required in a later series I might alreaddy share the stuff if possible if that reduces the number of regions.

Quoted 15 lines
Quoted 15 lines
arch/arm64/include/asm/kvm_mmu.h +1 -0
index 6eae7e7e2a68..b782cae771fe 100644
@@ -142,12 +142,15 @@ static __always_inline unsigned long __kern_hyp_va(unsigned long v)
 
 extern u32 __hyp_va_bits;
 
+#ifdef ARM64_S390_COMMON
 /*
  * We currently support using a VM-specified IPA size. For backward
  * compatibility, the default IPA size is fixed to 40bits.
  */
 #define KVM_PHYS_SHIFT	(40)
 
Quoted 17 lines

arch/arm64/include/asm/kvm_mmu.h void stage2_unmap_vm(struct kvm *kvm); · line 186

Quoted 12 lines
arch/arm64/include/asm/kvm_mmu.h +3 -0
+#endif /* ARM64_S390_COMMON */
+
 #define kvm_phys_shift(mmu)		VTCR_EL2_IPA((mmu)->vtcr)
 #define kvm_phys_size(mmu)		(_AC(1, ULL) << kvm_phys_shift(mmu))
 #define kvm_phys_mask(mmu)		(kvm_phys_size(mmu) - _AC(1, ULL))
@@ -178,9 +181,12 @@ void stage2_unmap_vm(struct kvm *kvm);
 int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long type);
 void kvm_uninit_stage2_mmu(struct kvm *kvm);
 void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu);
+#ifdef ARM64_S390_COMMON
 int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 			  phys_addr_t pa, unsigned long size, bool writable);

This only serves a purpose for GICv2, and I really hope your HW will not implement this. You should probably find a way to avoid dragging stuff that doesn't make sense on s390.

good catch, I added this as a mistake. It was a leftover from the early days.

...

Quoted 18 lines
Quoted 18 lines
arch/arm64/include/asm/kvm_mmu.h +2 -2
+#ifdef ARM64_S390_COMMON
+
 /*
  * Instructions for modifying PSTATE fields.
  * As per Arm ARM for v8-A, Section "C.5.1.3 op0 == 0b00, architectural hints,
@@ -91,8 +99,6 @@
  */
 #define pstate_field(op1, op2)		((op1) << Op1_shift | (op2) << Op2_shift)
 #define PSTATE_Imm_shift		CRm_shift
-#define ENCODE_PSTATE(x, r)		(0xd500401f | PSTATE_ ## r | ((!!x) << PSTATE_Imm_shift))
-#define SET_PSTATE(x, r)		__emit_inst(ENCODE_PSTATE(x, r))
 
 #define PSTATE_PAN			pstate_field(0, 4)
 #define PSTATE_UAO			pstate_field(0, 3)
@@ -100,6 +106,11 @@
 #define PSTATE_DIT			pstate_field(3, 2)
 #define PSTATE_TCO			pstate_field(3, 4)
 
Quoted 15 lines

arch/arm64/include/asm/kvm_mmu.h line 135

Quoted 11 lines
arch/arm64/include/asm/kvm_mmu.h +5 -0
+#endif /* ARM64_S390_COMMON */
+
+#define ENCODE_PSTATE(x, r)		(0xd500401f | PSTATE_ ## r | ((!!x) << PSTATE_Imm_shift))
+#define SET_PSTATE(x, r)		__emit_inst(ENCODE_PSTATE(x, r))
+
 #define SET_PSTATE_PAN(x)		SET_PSTATE((x), PAN)
 #define SET_PSTATE_UAO(x)		SET_PSTATE((x), UAO)
 #define SET_PSTATE_SSBS(x)		SET_PSTATE((x), SSBS)
@@ -123,6 +134,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)

What is the reason to avoid dragging these definitions? They seem extremely similar to the rest.

They emit arm64 instructions, dont they?

Quoted 51 lines
Quoted 51 lines
arch/arm64/include/asm/kvm_mmu.h +13 -1
+#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)
@@ -835,6 +848,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
@@ -869,6 +884,8 @@
 	 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)
@@ -1105,6 +1122,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)
 
@@ -1254,6 +1273,8 @@
 	par;								\
 })
 
+#ifdef ARM64_S390_COMMON
+
 #define SYS_FIELD_VALUE(reg, field, val)	reg##_##field##_##val
 
 #define SYS_FIELD_GET(reg, field, val)		\
@@ -1266,6 +1287,8 @@
 		 FIELD_PREP(reg##_##field##_MASK,	\
 			    SYS_FIELD_VALUE(reg, field, val))
 
-#endif
+#endif /* ARM64_S390_COMMON */
+
+#endif /* __ASSEMBLER__ */
 
Quoted 6 lines

arch/arm64/include/asm/kvm_mmu.h line 1294

Quoted 1 line
arch/arm64/include/asm/kvm_mmu.h +0 -0
 #endif	/* __ASM_SYSREG_H */

Overall, the fine-grained tagging is a bit hard to read, and there is no obvious rationale on why some bits are needed, and not others are not.

Sometimes the reason that it is not used is that they are not used yet but will in a later series. I defenitely can add a few more function that we need right now to reduce the scatter.

Quoted 2 lines

It definitely looks more manageable than the previous iteration, but as I said above, it would benefit from a less scattergun approach.

That is great to hear!

I tried to move as little arm code as possible. This is probably the other extreme compared to the first attempts where I moved everything. I will try out moving a few functions/defenitions inside the headers/functions to resuce the scattering.

Steffen

maintainer
#18 lore raw

Re: [PATCH v4 07/27] arm64: Prepare sharing arm64 headers with s390

Quoted 45 lines · Steffen Eiden
Quoted 30 lines · Marc Zyngier
Quoted 28 lines · Steffen Eiden

Prepare the sharing of arm64 headers with s390 by marking the shared regions or add a comment that the whole file is shared. The regions are marked with:

#define ARM64_S390_COMMON

/* insert shared definitions here */

#endif /* ARM64_S390_COMMON */

The preprocessor symbol ARM64_S390_COMMON is always defined for arm64. Reduce the include scope where possible and beneficial for s390. No functional change.

---
 arch/arm64/Makefile                  |  2 ++
 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 | 19 ++++++++++++++++++
 arch/arm64/include/asm/kvm_host.h    | 28 +++++++++++++++++++++++++++
 arch/arm64/include/asm/kvm_mmu.h     |  6 ++++++
 arch/arm64/include/asm/ptrace.h      |  9 +++++++++
 arch/arm64/include/asm/sysreg.h      | 29 +++++++++++++++++++++++++---
 9 files changed, 98 insertions(+), 7 deletions(-)

...

Quoted 10 lines

arch/arm64/include/asm/kvm_emulate.h static __always_inline void kvm_incr_pc(struct kvm_vcpu *vcpu) · line 632

Quoted 5 lines
arch/arm64/include/asm/kvm_emulate.h +2 -0
 		vcpu_set_flag((v), e);					\
 	} while (0)
 
+#endif /* ARM64_S390_COMMON */
+

It'd be worth looking at an initial patch reorganising this file to minimise the number of such markers. This is equally valid for other files in this patch.

Quoted 5 lines
Quoted 2 lines

[...]

So it is fine if I reorganize files a bit to reduce the number of markes?

Within reason, yes.

Quoted 4 lines

I feared it would not. I will reorganize the files a bit then if this reduces the number of sections. Also if some thiings are required in a later series I might alreaddy share the stuff if possible if that reduces the number of regions.

We'll have to evaluate the amount of churn this generates, but overall it would makes sense to keep related things together, and apply the markers sensibly.

[...]

Quoted 34 lines
Quoted 18 lines
Quoted 18 lines
arch/arm64/include/asm/sysreg.h +2 -2
+#ifdef ARM64_S390_COMMON
+
 /*
  * Instructions for modifying PSTATE fields.
  * As per Arm ARM for v8-A, Section "C.5.1.3 op0 == 0b00, architectural hints,
@@ -91,8 +99,6 @@
  */
 #define pstate_field(op1, op2)		((op1) << Op1_shift | (op2) << Op2_shift)
 #define PSTATE_Imm_shift		CRm_shift
-#define ENCODE_PSTATE(x, r)		(0xd500401f | PSTATE_ ## r | ((!!x) << PSTATE_Imm_shift))
-#define SET_PSTATE(x, r)		__emit_inst(ENCODE_PSTATE(x, r))
 
 #define PSTATE_PAN			pstate_field(0, 4)
 #define PSTATE_UAO			pstate_field(0, 3)
@@ -100,6 +106,11 @@
 #define PSTATE_DIT			pstate_field(3, 2)
 #define PSTATE_TCO			pstate_field(3, 4)
 
Quoted 15 lines

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

Quoted 11 lines
arch/arm64/include/asm/sysreg.h +5 -0
+#endif /* ARM64_S390_COMMON */
+
+#define ENCODE_PSTATE(x, r)		(0xd500401f | PSTATE_ ## r | ((!!x) << PSTATE_Imm_shift))
+#define SET_PSTATE(x, r)		__emit_inst(ENCODE_PSTATE(x, r))
+
 #define SET_PSTATE_PAN(x)		SET_PSTATE((x), PAN)
 #define SET_PSTATE_UAO(x)		SET_PSTATE((x), UAO)
 #define SET_PSTATE_SSBS(x)		SET_PSTATE((x), SSBS)
@@ -123,6 +134,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)

What is the reason to avoid dragging these definitions? They seem extremely similar to the rest.

Quoted 1 line

They emit arm64 instructions, dont they?

They emit instructions if you make use of them. And so do all the PSTATE macros. Why the different treatment?

Thanks,

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

PATCH v4 8/27 KVM: arm64: Prepare sharing 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       |  3 +++
 arch/arm64/kvm/handle_exit.c |  6 ++++++
 arch/arm64/kvm/mmio.c        | 14 ++++++++++++++
 arch/arm64/kvm/reset.c       |  3 +++
 5 files changed, 29 insertions(+)
arch/arm64/kvm/arm.c +3 -0
index 50adfff75be8..c992308b2dce 100644
@@ -1580,6 +1580,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)
 {
@@ -1628,6 +1629,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 +3 -0
index b01d6622b872..5a202cfd27bc 100644
@@ -543,6 +543,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 +592,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 54aedf93c78b..d511af145ee6 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 +14 -0
index e2285ed8c91d..876d56dd5b64 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;
@@ -135,8 +136,11 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)
 		if (!kvm_vcpu_dabt_issf(vcpu))
 			data = data & 0xffffffff;
 
+#endif /* ARM64_S390_COMMON */
 		trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
 			       &data);
+		//TODO  tracing
+#ifdef ARM64_S390_COMMON
 		data = vcpu_data_host_to_guest(vcpu, data, len);
 		vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data);
 	}
@@ -171,8 +175,11 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
 	 * though, so directly deliver an exception to the guest.
 	 */
 	if (!kvm_vcpu_dabt_isvalid(vcpu)) {
+#endif /* ARM64_S390_COMMON */
 		trace_kvm_mmio_nisv(*vcpu_pc(vcpu), esr,
 				    kvm_vcpu_get_hfar(vcpu), fault_ipa);
+		//TODO tracing
+#ifdef ARM64_S390_COMMON
 
 		if (vcpu_is_protected(vcpu))
 			return kvm_inject_sea_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
@@ -223,14 +230,20 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
 		data = vcpu_data_guest_to_host(vcpu, vcpu_get_reg(vcpu, rt),
 					       len);
 
+#endif /* ARM64_S390_COMMON */
 		trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, len, fault_ipa, &data);
+	//TODO tracing
+#ifdef ARM64_S390_COMMON
 		kvm_mmio_write_buf(data_buf, len, data);
 
 		ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_ipa, len,
 				       data_buf);
 	} else {
+#endif /* ARM64_S390_COMMON */
 		trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, len,
 			       fault_ipa, NULL);
+	//TODO tracing
+#ifdef ARM64_S390_COMMON
 
 		ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_ipa, len,
 				      data_buf);
@@ -257,3 +270,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 */
arch/arm64/kvm/reset.c +3 -0
index b963fd975aac..faab6f03f365 100644
@@ -34,6 +34,7 @@
 static u32 __ro_after_init kvm_ipa_limit;
 unsigned int __ro_after_init kvm_host_sve_max_vl;
 
+#ifdef ARM64_S390_COMMON
 /*
  * ARMv8 Reset Values
  */
@@ -46,6 +47,8 @@ unsigned int __ro_after_init kvm_host_sve_max_vl;
 #define VCPU_RESET_PSTATE_SVC	(PSR_AA32_MODE_SVC | PSR_AA32_A_BIT | \
 				 PSR_AA32_I_BIT | PSR_AA32_F_BIT)
 
+#endif /* ARM64_S390_COMMON */
+
 unsigned int __ro_after_init kvm_sve_max_vl;
 
 int __init kvm_arm_init_sve(void)
Signature · 1 line
2.53.0
#20 lore raw

Re: [PATCH v4 08/27] KVM: arm64: Prepare sharing arm64 code with s390

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] The patch misuses C preprocessor `#ifdef` directives to exclude tracepoints, resulting in…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] The patch misuses C preprocessor #ifdef directives to exclude tracepoints, resulting in catastrophic syntax errors because it leaves statements in the global scope.
- [Low] Critical ARM64 functions and definitions are wrapped in #ifdef ARM64_S390_COMMON, stripping them from standard ARM64 builds and causing undefined reference errors because their callers remain unconditionally compiled. --

commit 651f0f9137d42f2814e4612c746de86e829e6666
Author: Steffen Eiden <[email protected]>

KVM: arm64: Prepare sharing arm64 code with s390

This commit marks functions that s390 can use to implement arm on s390 as shared functions, without introducing functional changes.

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

Quoted 12 lines
arch/arm64/kvm/arm.c +1 -0
index 50adfff75be82..c992308b2dceb 100644
@@ -1580,6 +1580,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)
 {

[Severity: Low]
Will this cause undefined reference errors on standard ARM64 builds?

The caller of kvm_vcpu_init_check_features() remains unconditionally compiled, so the symbol will be missing when ARM64_S390_COMMON is not defined.

arch/arm64/kvm/guest.c int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) · line 549

Quoted 12 lines
arch/arm64/kvm/guest.c +1 -0
index b01d6622b8720..5a202cfd27bcb 100644
@@ -543,6 +543,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)
 {

[Severity: Low]
Does this missing symbol cause a link failure for kvm_arm_copy_reg_indices()? The function is compiled out here, but the caller isn't.

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

Quoted 11 lines
arch/arm64/kvm/handle_exit.c +1 -0
index 54aedf93c78b6..d511af145ee6a 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)
 {

[Severity: Low]
Will this lead to an undefined reference?

kvm_handle_unknown_ec() is conditionally removed here, but it remains referenced in the unconditionally compiled arm_exit_handlers array.

[ ... ]

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

Quoted 7 lines
arch/arm64/kvm/handle_exit.c +1 -0
@@ -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)
 {

[Severity: Low]
Is it intentional to compile out kvm_get_exit_handler() and handle_trap_exceptions()?

The handle_exit() caller relies on them unconditionally, which may cause a build failure.

Quoted 4 lines
arch/arm64/kvm/mmio.c +0 -0
index e2285ed8c91de..876d56dd5b640 100644

[ ... ]

arch/arm64/kvm/mmio.c int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) · line 146

Quoted 12 lines
arch/arm64/kvm/mmio.c +3 -0
@@ -135,8 +136,11 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)
 		if (!kvm_vcpu_dabt_issf(vcpu))
 			data = data & 0xffffffff;
 
+#endif /* ARM64_S390_COMMON */
 		trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
 			       &data);
+		//TODO  tracing
+#ifdef ARM64_S390_COMMON
 		data = vcpu_data_host_to_guest(vcpu, data, len);
 		vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data);
 	}

[Severity: Low]
Does this inverted preprocessor logic break the build on standard ARM64?

Since kvm_handle_mmio_return() starts inside an ifdef ARM64_S390_COMMON block, placing an endif here exposes the trace_kvm_mmio() call to the global scope when the flag is not set, resulting in invalid C syntax.

[ ... ]

arch/arm64/kvm/mmio.c int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa) · line 185

Quoted 12 lines
arch/arm64/kvm/mmio.c +3 -0
@@ -171,8 +175,11 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
 	 * though, so directly deliver an exception to the guest.
 	 */
 	if (!kvm_vcpu_dabt_isvalid(vcpu)) {
+#endif /* ARM64_S390_COMMON */
 		trace_kvm_mmio_nisv(*vcpu_pc(vcpu), esr,
 				    kvm_vcpu_get_hfar(vcpu), fault_ipa);
+		//TODO tracing
+#ifdef ARM64_S390_COMMON
 
 		if (vcpu_is_protected(vcpu))
 			return kvm_inject_sea_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));

[Severity: Low]
Similar to the issue above, does this leave trace_kvm_mmio_nisv() in the global scope and cause syntax errors when ARM64_S390_COMMON is undefined?

Quoted 13 lines
arch/arm64/kvm/reset.c +1 -0
index b963fd975aaca..faab6f03f3655 100644
@@ -34,6 +34,7 @@
 static u32 __ro_after_init kvm_ipa_limit;
 unsigned int __ro_after_init kvm_host_sve_max_vl;
 
+#ifdef ARM64_S390_COMMON
 /*
  * ARMv8 Reset Values
  */

arch/arm64/kvm/reset.c unsigned int __ro_after_init kvm_host_sve_max_vl; · line 50

Quoted 4 lines
arch/arm64/kvm/reset.c +1 -0
@@ -46,6 +47,8 @@ unsigned int __ro_after_init kvm_host_sve_max_vl;
 #define VCPU_RESET_PSTATE_SVC	(PSR_AA32_MODE_SVC | PSR_AA32_A_BIT | \
 				 PSR_AA32_I_BIT | PSR_AA32_F_BIT)
 
+#endif /* ARM64_S390_COMMON */

[Severity: Low]
Could stripping out these reset value macros cause compilation to fail?

They appear to be used in unconditional kvm_reset_vcpu() initialization logic.

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

Re: [PATCH v4 08/27] KVM: arm64: Prepare sharing arm64 code with s390

In reply to Steffen Eiden
Quoted 108 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       |  3 +++
 arch/arm64/kvm/handle_exit.c |  6 ++++++
 arch/arm64/kvm/mmio.c        | 14 ++++++++++++++
 arch/arm64/kvm/reset.c       |  3 +++
 5 files changed, 29 insertions(+)
arch/arm64/kvm/arm.c +3 -0
index 50adfff75be8..c992308b2dce 100644
@@ -1580,6 +1580,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)
 {
@@ -1628,6 +1629,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 +3 -0
index b01d6622b872..5a202cfd27bc 100644
@@ -543,6 +543,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 +592,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 54aedf93c78b..d511af145ee6 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 +1 -0
index e2285ed8c91d..876d56dd5b64 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;

arch/arm64/kvm/mmio.c int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) · line 143

Quoted 8 lines
arch/arm64/kvm/mmio.c +3 -0
@@ -135,8 +136,11 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)
 		if (!kvm_vcpu_dabt_issf(vcpu))
 			data = data & 0xffffffff;
 
+#endif /* ARM64_S390_COMMON */
 		trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
 			       &data);
+		//TODO  tracing
+#ifdef ARM64_S390_COMMON

Irk. The deal is that you lift full functions, not cherry-pick which lines you want. Just define empty stubs for the trace nonsense, and be done with it. But not this, please!

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

Re: [PATCH v4 08/27] KVM: arm64: Prepare sharing arm64 code with s390

Quoted 2 lines · Marc Zyngier

On Mon, 06 Jul 2026 09:52:08 +0100,
Steffen Eiden <[email protected]> wrote:

...

Quoted 19 lines
Quoted 19 lines

/*
* Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
* proper exit to userspace.

arch/arm64/kvm/mmio.c +1 -0
index e2285ed8c91d..876d56dd5b64 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;
@@ -135,8 +136,11 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)
 		if (!kvm_vcpu_dabt_issf(vcpu))
 			data = data & 0xffffffff;
 
Quoted 10 lines

arch/arm64/kvm/mmio.c int kvm_handle_mmio_return(struct kvm_vcpu *vcpu) · line 143

Quoted 5 lines
arch/arm64/kvm/mmio.c +3 -0
+#endif /* ARM64_S390_COMMON */
 		trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
 			       &data);
+		//TODO  tracing
+#ifdef ARM64_S390_COMMON

Irk. The deal is that you lift full functions, not cherry-pick which lines you want. Just define empty stubs for the trace nonsense, and be done with it. But not this, please!

Sorry, this dirty hack is a leftover I forgot to clean up prior to sending. Hence the TODO.

I definitely second you full functions or not at all should be the approach. Everything else is not maintainable.

I will add the tracing stuff for the next round.

Steffen

author reviewer
#23 lore raw

PATCH v4 9/27 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.

Share the gp register functions with s390.

No functional change.

---
 arch/arm64/include/asm/kvm_emulate.h       | 13 +++++++++----
 arch/arm64/include/asm/kvm_host.h          |  8 +++++++-
 arch/arm64/kvm/guest.c                     | 19 +++++++++++--------
 arch/arm64/kvm/hyp/exception.c             |  7 +++++--
 arch/arm64/kvm/hyp/include/hyp/adjust_pc.h |  4 ++--
 arch/arm64/kvm/hyp/include/hyp/switch.h    |  6 +++---
 arch/arm64/kvm/reset.c                     |  6 ++++--
 7 files changed, 41 insertions(+), 22 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +9 -4
index a1c92d2436ae..15d6d6a08d37 100644
@@ -140,12 +140,17 @@ 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 unsigned long *vcpu_sp_el0(const struct kvm_vcpu *vcpu)
+{
+	return (unsigned long *)&vcpu->arch.ctxt.regs.sp;
 }
 
 static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
@@ -175,14 +180,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;
 }
 
 #endif /* ARM64_S390_COMMON */
arch/arm64/include/asm/kvm_host.h +7 -1
index ae9f76378218..2fce38fd9152 100644
@@ -1170,7 +1170,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
@@ -1201,6 +1201,12 @@ static inline u64 *___ctxt_sys_reg(const struct kvm_cpu_context *ctxt, int r)
 
 #define ctxt_sys_reg(c,r)	(*__ctxt_sys_reg(c,r))
 
+#define kvm_vcpu_get_sp_el1(__vcpu) (__ctxt_sys_reg(&(__vcpu)->arch.ctxt, SP_EL1))
+#define kvm_vcpu_get_vreg(__vcpu, _n) (&(__vcpu)->arch.ctxt.fp_regs.vregs[_n])
+#define kvm_vcpu_get_vregs(__vcpu) (&(__vcpu)->arch.ctxt.fp_regs.vregs)
+#define kvm_vcpu_get_fpsr(__vcpu) (&(__vcpu)->arch.ctxt.fp_regs.fpsr)
+#define kvm_vcpu_get_fpcr(__vcpu) (&(__vcpu)->arch.ctxt.fp_regs.fpcr)
+
 u64 kvm_vcpu_apply_reg_masks(const struct kvm_vcpu *, enum vcpu_sysreg, u64);
 
 #define __vcpu_assign_sys_reg(v, r, val)				\
arch/arm64/kvm/guest.c +11 -8
index 5a202cfd27bc..5e1e1faa98c2 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) &&
@@ -134,19 +135,19 @@ 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;
+		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  kvm_vcpu_get_sp_el1(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(elr_el1):
 		return __ctxt_sys_reg(&vcpu->arch.ctxt, ELR_EL1);
@@ -170,13 +171,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 kvm_vcpu_get_vreg(vcpu, off);
 
 	case KVM_REG_ARM_CORE_REG(fp_regs.fpsr):
-		return &vcpu->arch.ctxt.fp_regs.fpsr;
+		return  kvm_vcpu_get_fpsr(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(fp_regs.fpcr):
-		return &vcpu->arch.ctxt.fp_regs.fpcr;
+		return  kvm_vcpu_get_fpcr(vcpu);
 
 	default:
 		return NULL;
@@ -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)))
arch/arm64/kvm/hyp/exception.c +5 -2
index bef40ddb16db..82611442a2d1 100644
@@ -277,6 +277,9 @@ static const u8 return_offsets[8][2] = {
 	[7] = { 4, 4 },		/* FIQ, unused */
 };
 
+#define OFFSETOF_PT_REG(__r) offsetof(struct user_pt_regs, __r)
+#define COMPAT_IDX(__c) ((OFFSETOF_PT_REG(__c) - OFFSETOF_PT_REG(regs[0])) / sizeof(u64))
+
 static void enter_exception32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset)
 {
 	unsigned long spsr = *vcpu_cpsr(vcpu);
@@ -292,12 +295,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_IDX(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_IDX(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 +3 -3
index 18131e395e24..a57aec6dd854 100644
@@ -449,7 +449,7 @@ static inline bool __populate_fault_info(struct kvm_vcpu *vcpu)
 static inline bool kvm_hyp_handle_mops(struct kvm_vcpu *vcpu, u64 *exit_code)
 {
 	*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);
 
 	/*
@@ -895,7 +895,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?
 	 */
@@ -905,7 +905,7 @@ static inline void synchronize_vcpu_pstate(struct kvm_vcpu *vcpu)
 	    ESR_ELx_EC(read_sysreg_el2(SYS_ESR)) == ESR_ELx_EC_PAC)
 		write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
 
-	vcpu->arch.ctxt.regs.pstate = read_sysreg_el2(SYS_SPSR);
+	*vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
 }
 
 /*
arch/arm64/kvm/reset.c +4 -2
index faab6f03f365..1ce3b26cc1ae 100644
@@ -221,13 +221,15 @@ void kvm_reset_vcpu(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_sp_el0(vcpu) = 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;
 
 	/* Reset system registers */
 	kvm_reset_sys_regs(vcpu);
Signature · 1 line
2.53.0
maintainer
#24 lore raw

Re: [PATCH v4 09/27] KVM: arm64: Access elements of vcpu_gp_regs individually

Quoted 7 lines · 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.

That's an interesting change of semantics, because this excludes PC from the GPRs. This is valid on AArch64, but wrong for AArch32 (PC really is R15, and is just another GPR).

It isn't a huge deal, and nothing breaks, but that's something that you may want to capture.

Quoted 90 lines

Share the gp register functions with s390.

No functional change.

---
 arch/arm64/include/asm/kvm_emulate.h       | 13 +++++++++----
 arch/arm64/include/asm/kvm_host.h          |  8 +++++++-
 arch/arm64/kvm/guest.c                     | 19 +++++++++++--------
 arch/arm64/kvm/hyp/exception.c             |  7 +++++--
 arch/arm64/kvm/hyp/include/hyp/adjust_pc.h |  4 ++--
 arch/arm64/kvm/hyp/include/hyp/switch.h    |  6 +++---
 arch/arm64/kvm/reset.c                     |  6 ++++--
 7 files changed, 41 insertions(+), 22 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +9 -4
index a1c92d2436ae..15d6d6a08d37 100644
@@ -140,12 +140,17 @@ 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 unsigned long *vcpu_sp_el0(const struct kvm_vcpu *vcpu)
+{
+	return (unsigned long *)&vcpu->arch.ctxt.regs.sp;
 }
 
 static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
@@ -175,14 +180,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;
 }
 
 #endif /* ARM64_S390_COMMON */
arch/arm64/include/asm/kvm_host.h +7 -1
index ae9f76378218..2fce38fd9152 100644
@@ -1170,7 +1170,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
@@ -1201,6 +1201,12 @@ static inline u64 *___ctxt_sys_reg(const struct kvm_cpu_context *ctxt, int r)
 
 #define ctxt_sys_reg(c,r)	(*__ctxt_sys_reg(c,r))
 
+#define kvm_vcpu_get_sp_el1(__vcpu) (__ctxt_sys_reg(&(__vcpu)->arch.ctxt, SP_EL1))
+#define kvm_vcpu_get_vreg(__vcpu, _n) (&(__vcpu)->arch.ctxt.fp_regs.vregs[_n])
+#define kvm_vcpu_get_vregs(__vcpu) (&(__vcpu)->arch.ctxt.fp_regs.vregs)
+#define kvm_vcpu_get_fpsr(__vcpu) (&(__vcpu)->arch.ctxt.fp_regs.fpsr)
+#define kvm_vcpu_get_fpcr(__vcpu) (&(__vcpu)->arch.ctxt.fp_regs.fpcr)
+
 u64 kvm_vcpu_apply_reg_masks(const struct kvm_vcpu *, enum vcpu_sysreg, u64);
 
 #define __vcpu_assign_sys_reg(v, r, val)				\
arch/arm64/kvm/guest.c +0 -0
index 5a202cfd27bc..5e1e1faa98c2 100644

arch/arm64/kvm/guest.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 65

Quoted 4 lines
arch/arm64/kvm/guest.c +1 -0
@@ -62,6 +62,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {
 		       sizeof(kvm_vcpu_stats_desc),
 };
 
+#ifdef ARM64_S390_COMMON

I really think this patch (and a few others) needs splitting. What I'd like to see is a prefix to this series adding the required arm64 rework, and only in a subsequent patch add the "make this shared" attributes.

Also, quite a lot of this patch is about using the existing accessors instead of an open-coded version. These changes should be standalone.

Quoted 32 lines
arch/arm64/kvm/guest.c +5 -5
 static bool core_reg_offset_is_vreg(u64 off)
 {
 	return off >= KVM_REG_ARM_CORE_REG(fp_regs.vregs) &&
@@ -134,19 +135,19 @@ 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;
+		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  kvm_vcpu_get_sp_el1(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(elr_el1):
 		return __ctxt_sys_reg(&vcpu->arch.ctxt, ELR_EL1);
@@ -170,13 +171,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;

arch/arm64/kvm/guest.c static void *core_reg_addr(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 180

Quoted 10 lines
arch/arm64/kvm/guest.c +3 -3
-		return &vcpu->arch.ctxt.fp_regs.vregs[off];
+		return kvm_vcpu_get_vreg(vcpu, off);
 
 	case KVM_REG_ARM_CORE_REG(fp_regs.fpsr):
-		return &vcpu->arch.ctxt.fp_regs.fpsr;
+		return  kvm_vcpu_get_fpsr(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(fp_regs.fpcr):
-		return &vcpu->arch.ctxt.fp_regs.fpcr;
+		return  kvm_vcpu_get_fpcr(vcpu);

Odd additional spaces (3 instances).

Quoted 17 lines
arch/arm64/kvm/guest.c +2 -0
 	default:
 		return NULL;
@@ -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)))
arch/arm64/kvm/hyp/exception.c +0 -0
index bef40ddb16db..82611442a2d1 100644

arch/arm64/kvm/hyp/exception.c static const u8 return_offsets[8][2] = { · line 282

Quoted 6 lines
arch/arm64/kvm/hyp/exception.c +3 -0
@@ -277,6 +277,9 @@ static const u8 return_offsets[8][2] = {
 	[7] = { 4, 4 },		/* FIQ, unused */
 };
 
+#define OFFSETOF_PT_REG(__r) offsetof(struct user_pt_regs, __r)
+#define COMPAT_IDX(__c) ((OFFSETOF_PT_REG(__c) - OFFSETOF_PT_REG(regs[0])) / sizeof(u64))
+

Oh $gawd, this is... awful.

arch/arm64/kvm/hyp/exception.c static void enter_exception32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset) · line 299

Quoted 10 lines
arch/arm64/kvm/hyp/exception.c +1 -1
 static void enter_exception32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset)
 {
 	unsigned long spsr = *vcpu_cpsr(vcpu);
@@ -292,12 +295,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_IDX(compat_lr_abt)] = return_address;
 		break;

Stupid idea: why don't you simply have new #defines that make the register number standalone, and make ptrace.h use that? Something line this (which can obviously be extended to all the compat registers):

arch/arm64/include/asm/ptrace.h +5 -1
index 39582511ad72f..2d3d324d2598e 100644
@@ -104,6 +104,10 @@
 #define COMPAT_USER_SZ	296
 
 /* Architecturally defined mapping between AArch32 and AArch64 registers */
+enum aarch32_reg_mapping {
+	__compat_lr_und = 22,
+};
+
 #define compat_usr(x)	regs[(x)]
 #define compat_fp	regs[11]
 #define compat_sp	regs[13]
@@ -115,7 +119,7 @@
 #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_lr_und	regs[__compat_lr_und]
 #define compat_sp_und	regs[23]
 #define compat_r8_fiq	regs[24]
 #define compat_r9_fiq	regs[25]

and then your #defines from hell can go?

Thanks,

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

Re: [PATCH v4 09/27] KVM: arm64: Access elements of vcpu_gp_regs individually

Quoted 6 lines · Marc Zyngier
Quoted 4 lines · Steffen Eiden

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

Quoted 8 lines
Quoted 3 lines

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.

That's an interesting change of semantics, because this excludes PC from the GPRs. This is valid on AArch64, but wrong for AArch32 (PC really is R15, and is just another GPR).

I never looked into AArch32 thanks for pointing this out.

Quoted 3 lines

It isn't a huge deal, and nothing breaks, but that's something that you may want to capture.

Shall I add a comment into the commit message that this splits pc from the gprs for arm32?

...

Quoted 18 lines
Quoted 18 lines
(quoted diff) +6 -0
+#define kvm_vcpu_get_sp_el1(__vcpu) (__ctxt_sys_reg(&(__vcpu)->arch.ctxt, SP_EL1))
+#define kvm_vcpu_get_vreg(__vcpu, _n) (&(__vcpu)->arch.ctxt.fp_regs.vregs[_n])
+#define kvm_vcpu_get_vregs(__vcpu) (&(__vcpu)->arch.ctxt.fp_regs.vregs)
+#define kvm_vcpu_get_fpsr(__vcpu) (&(__vcpu)->arch.ctxt.fp_regs.fpsr)
+#define kvm_vcpu_get_fpcr(__vcpu) (&(__vcpu)->arch.ctxt.fp_regs.fpcr)
+
 u64 kvm_vcpu_apply_reg_masks(const struct kvm_vcpu *, enum vcpu_sysreg, u64);
 
 #define __vcpu_assign_sys_reg(v, r, val)				\
arch/arm64/kvm/guest.c +0 -0
index 5a202cfd27bc..5e1e1faa98c2 100644
@@ -62,6 +62,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {
 		       sizeof(kvm_vcpu_stats_desc),
 };
 
Quoted 6 lines

arch/arm64/kvm/guest.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 65

Quoted 1 line
arch/arm64/kvm/guest.c +1 -0
+#ifdef ARM64_S390_COMMON

I really think this patch (and a few others) needs splitting. What I'd like to see is a prefix to this series adding the required arm64 rework, and only in a subsequent patch add the "make this shared" attributes.

Ok, makes sense. I'll do the refactroings first and then add these markers.

Quoted 3 lines

Also, quite a lot of this patch is about using the existing accessors instead of an open-coded version. These changes should be standalone.

Ok I'llsplit this up:

1. vcpu_gp_regs change
2. accessor use

Quoted 40 lines
Quoted 40 lines
arch/arm64/kvm/guest.c +7 -7
 static bool core_reg_offset_is_vreg(u64 off)
 {
 	return off >= KVM_REG_ARM_CORE_REG(fp_regs.vregs) &&
@@ -134,19 +135,19 @@ 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;
+		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  kvm_vcpu_get_sp_el1(vcpu);
 
 	case KVM_REG_ARM_CORE_REG(elr_el1):
 		return __ctxt_sys_reg(&vcpu->arch.ctxt, ELR_EL1);
@@ -170,13 +171,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 kvm_vcpu_get_vreg(vcpu, off);
 
 	case KVM_REG_ARM_CORE_REG(fp_regs.fpsr):
-		return &vcpu->arch.ctxt.fp_regs.fpsr;
+		return  kvm_vcpu_get_fpsr(vcpu);
 
Quoted 5 lines

arch/arm64/kvm/guest.c static void *core_reg_addr(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 180

Quoted 3 lines
arch/arm64/kvm/guest.c +1 -1
 	case KVM_REG_ARM_CORE_REG(fp_regs.fpcr):
-		return &vcpu->arch.ctxt.fp_regs.fpcr;
+		return  kvm_vcpu_get_fpcr(vcpu);

Odd additional spaces (3 instances).

thanks.

Quoted 67 lines
Quoted 18 lines
arch/arm64/kvm/guest.c +2 -0
 	default:
 		return NULL;
@@ -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)))
arch/arm64/kvm/hyp/exception.c +0 -0
index bef40ddb16db..82611442a2d1 100644

arch/arm64/kvm/hyp/exception.c static const u8 return_offsets[8][2] = { · line 282

Quoted 6 lines
arch/arm64/kvm/hyp/exception.c +3 -0
@@ -277,6 +277,9 @@ static const u8 return_offsets[8][2] = {
 	[7] = { 4, 4 },		/* FIQ, unused */
 };
 
+#define OFFSETOF_PT_REG(__r) offsetof(struct user_pt_regs, __r)
+#define COMPAT_IDX(__c) ((OFFSETOF_PT_REG(__c) - OFFSETOF_PT_REG(regs[0])) / sizeof(u64))
+

Oh $gawd, this is... awful.

arch/arm64/kvm/hyp/exception.c static void enter_exception32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset) · line 299

Quoted 10 lines
arch/arm64/kvm/hyp/exception.c +1 -1
 static void enter_exception32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset)
 {
 	unsigned long spsr = *vcpu_cpsr(vcpu);
@@ -292,12 +295,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_IDX(compat_lr_abt)] = return_address;
 		break;

Stupid idea: why don't you simply have new #defines that make the register number standalone, and make ptrace.h use that? Something line this (which can obviously be extended to all the compat registers):

arch/arm64/include/asm/ptrace.h +5 -1
index 39582511ad72f..2d3d324d2598e 100644
@@ -104,6 +104,10 @@
 #define COMPAT_USER_SZ	296
 
 /* Architecturally defined mapping between AArch32 and AArch64 registers */
+enum aarch32_reg_mapping {
+	__compat_lr_und = 22,
+};
+
 #define compat_usr(x)	regs[(x)]
 #define compat_fp	regs[11]
 #define compat_sp	regs[13]
@@ -115,7 +119,7 @@
 #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_lr_und	regs[__compat_lr_und]
 #define compat_sp_und	regs[23]
 #define compat_r8_fiq	regs[24]
 #define compat_r9_fiq	regs[25]
Quoted 1 line

and then your #defines from hell can go?

Great suggestion I do not like this macro hell myself. I'll implement it that way. Should I convert all compat_* regs to this concept or just the ones I need?

Steffen

maintainer
#26 lore raw

Re: [PATCH v4 09/27] KVM: arm64: Access elements of vcpu_gp_regs individually

Quoted 17 lines · Steffen Eiden
Quoted 6 lines · Marc Zyngier
Quoted 4 lines · Steffen Eiden

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

Quoted 8 lines
Quoted 3 lines

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.

That's an interesting change of semantics, because this excludes PC from the GPRs. This is valid on AArch64, but wrong for AArch32 (PC really is R15, and is just another GPR).

Quoted 8 lines

I never looked into AArch32 thanks for pointing this out.

Quoted 3 lines

It isn't a huge deal, and nothing breaks, but that's something that you may want to capture.

Shall I add a comment into the commit message that this splits pc from the gprs for arm32?

Yeah, I really want this stuff to be correctly documented, just in case we break something and have to go back in time to find out why. That's overall something that could be improved in this series.

[...]

Quoted 54 lines
Quoted 54 lines

arch/arm64/kvm/hyp/exception.c static const u8 return_offsets[8][2] = { · line 282

Quoted 11 lines
arch/arm64/kvm/hyp/exception.c +3 -0
index bef40ddb16db..82611442a2d1 100644
@@ -277,6 +277,9 @@ static const u8 return_offsets[8][2] = {
 	[7] = { 4, 4 },		/* FIQ, unused */
 };
 
+#define OFFSETOF_PT_REG(__r) offsetof(struct user_pt_regs, __r)
+#define COMPAT_IDX(__c) ((OFFSETOF_PT_REG(__c) - OFFSETOF_PT_REG(regs[0])) / sizeof(u64))
+

Oh $gawd, this is... awful.

arch/arm64/kvm/hyp/exception.c static void enter_exception32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset) · line 299

Quoted 10 lines
arch/arm64/kvm/hyp/exception.c +1 -1
 static void enter_exception32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset)
 {
 	unsigned long spsr = *vcpu_cpsr(vcpu);
@@ -292,12 +295,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_IDX(compat_lr_abt)] = return_address;
 		break;

Stupid idea: why don't you simply have new #defines that make the register number standalone, and make ptrace.h use that? Something line this (which can obviously be extended to all the compat registers):

arch/arm64/include/asm/ptrace.h +5 -1
index 39582511ad72f..2d3d324d2598e 100644
@@ -104,6 +104,10 @@
 #define COMPAT_USER_SZ	296
 
 /* Architecturally defined mapping between AArch32 and AArch64 registers */
+enum aarch32_reg_mapping {
+	__compat_lr_und = 22,
+};
+
 #define compat_usr(x)	regs[(x)]
 #define compat_fp	regs[11]
 #define compat_sp	regs[13]
@@ -115,7 +119,7 @@
 #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_lr_und	regs[__compat_lr_und]
 #define compat_sp_und	regs[23]
 #define compat_r8_fiq	regs[24]
 #define compat_r9_fiq	regs[25]
Quoted 5 lines
Quoted 1 line

and then your #defines from hell can go?

Great suggestion I do not like this macro hell myself. I'll implement it that way. Should I convert all compat_* regs to this concept or just the ones I need?

All of them, please., I value consistency. As you are doing this, consider adding a reference to I_PYKVS, which describes the mapping in the ARM ARM.

Thanks,

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

PATCH v4 10/27 KVM: arm64: Refactor core-reset into a separate function

In reply to Steffen Eiden

Move the reset of the arm core register into a separate function and share it with s390.

No functional change.

---
 arch/arm64/kvm/reset.c | 45 ++++++++++++++++++++++++++----------------
 1 file changed, 28 insertions(+), 17 deletions(-)
arch/arm64/kvm/reset.c +28 -17
index 1ce3b26cc1ae..81d9f0faede2 100644
@@ -172,6 +172,33 @@ static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
 		memset(vcpu->arch.sve_state, 0, vcpu_sve_state_size(vcpu));
 }
 
+#ifdef ARM64_S390_COMMON
+static void kvm_reset_vcpu_core_regs(struct kvm_vcpu *vcpu)
+{
+	u32 pstate;
+
+	if (vcpu_el1_is_32bit(vcpu))
+		pstate = VCPU_RESET_PSTATE_SVC;
+	else if (vcpu_has_nv(vcpu))
+		pstate = VCPU_RESET_PSTATE_EL2;
+	else
+		pstate = VCPU_RESET_PSTATE_EL1;
+
+	memset(vcpu_gp_regs(vcpu), 0, sizeof(vcpu_gp_regs(vcpu)));
+	*vcpu_pc(vcpu) = 0;
+	*vcpu_sp_el0(vcpu) = 0;
+	memset(kvm_vcpu_get_vregs(vcpu), 0, sizeof(*kvm_vcpu_get_vregs(vcpu)));
+	memset(kvm_vcpu_get_fpsr(vcpu), 0, sizeof(*kvm_vcpu_get_fpsr(vcpu)));
+	memset(kvm_vcpu_get_fpcr(vcpu), 0, sizeof(*kvm_vcpu_get_fpcr(vcpu)));
+	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_cpsr(vcpu) = pstate;
+}
+
+#endif /* ARM64_S390_COMMON */
+
 /**
  * kvm_reset_vcpu - sets core registers and sys_regs to reset value
  * @vcpu: The VCPU pointer
@@ -194,7 +221,6 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_reset_state reset_state;
 	bool loaded;
-	u32 pstate;
 
 	spin_lock(&vcpu->arch.mp_state_lock);
 	reset_state = vcpu->arch.reset_state;
@@ -213,23 +239,8 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 		kvm_vcpu_reset_sve(vcpu);
 	}
 
-	if (vcpu_el1_is_32bit(vcpu))
-		pstate = VCPU_RESET_PSTATE_SVC;
-	else if (vcpu_has_nv(vcpu))
-		pstate = VCPU_RESET_PSTATE_EL2;
-	else
-		pstate = VCPU_RESET_PSTATE_EL1;
-
 	/* Reset core registers */
-	memset(vcpu_gp_regs(vcpu), 0, sizeof(vcpu_gp_regs(vcpu)));
-	*vcpu_pc(vcpu) = 0;
-	*vcpu_sp_el0(vcpu) = 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_cpsr(vcpu) = pstate;
+	kvm_reset_vcpu_core_regs(vcpu);
 
 	/* Reset system registers */
 	kvm_reset_sys_regs(vcpu);
Signature · 1 line
2.53.0
author reviewer
#28 lore raw

PATCH v4 11/27 s390: 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/asm/...>' 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_S390_ARM64.

---
 arch/s390/tools/Makefile             |  5 ++
 arch/s390/tools/Makefile.arm64h      | 58 +++++++++++++++++++++++
 arch/s390/tools/copy-arm64h-full.awk | 43 +++++++++++++++++
 arch/s390/tools/copy-arm64h.awk      | 71 ++++++++++++++++++++++++++++
 4 files changed, 177 insertions(+)
 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 +5 -0
index f2862364fb42..bd2a00d5ce2c 100644
@@ -3,8 +3,13 @@
 # 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
+kapi-hdrs-$(CONFIG_KVM_ARM64) += $(ARM64_TARGETS)
+
+targets += $(addprefix ../../../,$(kapi-hdrs-y))
 
 PHONY += kapi
 
arch/s390/tools/Makefile.arm64h new file +58 -0

58 lines not on this page · raw message

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

43 lines not on this page · raw message

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

71 lines not on this page · raw message

Signature · 1 line
2.53.0
#29 lore raw

Re: [PATCH v4 11/27] s390: Use arm64 headers

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Double `$(objtree)` prefix in `arm64-dst` causes out-of-tree builds to fail. - [Medium] The…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Double $(objtree) prefix in arm64-dst causes out-of-tree builds to fail.
- [Medium] The sysreg-defs.h target will be unnecessarily rebuilt on every make invocation during out-of-tree builds due to a targets array mismatch.
- [Low] The include guard regex in copy-arm64h-full.awk fails to match standard ARM64 KVM headers that contain numbers.
- [Low] The commit message incorrectly states that included headers are rewritten to #include <arm64/asm/...>, but the implementation correctly rewrites them to #include <arm64/...> instead. --

commit 622a9e9498e4f7fc1aed85088dd8016c40e7fc8b
Author: Steffen Eiden <[email protected]>

s390: Use arm64 headers

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.

Quoted 3 lines

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

[Severity: Low]
This isn't a bug, but the commit message states the headers are rewritten to '#include <arm64/asm/...>', while the awk scripts in this patch correctly rewrite them to '#include <arm64/...>' without the 'asm/' component.

Quoted 4 lines
arch/s390/tools/Makefile +0 -0
index f2862364fb422..bd2a00d5ce2c9 100644

[ ... ]

arch/s390/tools/Makefile line 12

Quoted 11 lines
arch/s390/tools/Makefile +5 -0
@@ -3,8 +3,13 @@
 # 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
+kapi-hdrs-$(CONFIG_KVM_ARM64) += $(ARM64_TARGETS)
+
+targets += $(addprefix ../../../,$(kapi-hdrs-y))

[Severity: Medium]
Will this unnecessarily rebuild sysreg-defs.h on every make invocation during out-of-tree builds?

In Makefile.arm64h, sysreg-defs.h is added to ARM64_TARGETS using an absolute path because arm64api includes $(objtree).

Then here, it gets appended to the targets array with a relative directory traversal using $(addprefix ../../../,$(kapi-hdrs-y)).

Prepending a relative traversal to an absolute path
(like ../../..//build_dir/...) creates a malformed path. This prevents Kbuild from locating the .cmd file and breaks the if_changed cache.

Quoted 6 lines
arch/s390/tools/Makefile.arm64h new file +0 -0
new file mode 100644
index 0000000000000..0b32c124707d9
@@ -0,0 +1,58 @@

[ ... ]

Quoted 2 lines
arch/s390/tools/Makefile.arm64h new file +2 -0
+arm64api := $(objtree)/arch/$(ARCH)/include/generated/arm64
+arm64uapi := $(objtree)/arch/$(ARCH)/include/generated/uapi/arm64

[ ... ]

arch/s390/tools/Makefile.arm64h line 42

Quoted 2 lines
arch/s390/tools/Makefile.arm64h new file +2 -0
+arm64-src = $(srctree)/arch/arm64/include/$(if $(filter uapi,$(call arm64-type,$(1))),uapi/)asm/$(call arm64-src-base,$(1))
+arm64-dst = $(objtree)/$(if $(filter uapi,$(call arm64-type,$(1))),$(arm64uapi),$(arm64api))/$(call arm64-base,$(1))

[Severity: High]
Does this double prefix of $(objtree) break out-of-tree builds?

Since arm64api and arm64uapi are defined above with the $(objtree) prefix already, arm64-dst prepends $(objtree)/ to them again.

This causes paths to resolve to a nested build_dir/build_dir/... structure during out-of-tree compilation.

Quoted 6 lines
arch/s390/tools/copy-arm64h-full.awk new file +0 -0
new file mode 100644
index 0000000000000..a9b85b347f780
@@ -0,0 +1,43 @@

[ ... ]

arch/s390/tools/copy-arm64h-full.awk line 17

Quoted 2 lines
arch/s390/tools/copy-arm64h-full.awk new file +2 -0
+# Find and process the include guard in the first few lines
+NR <= max_guard_line && !guard_found && /^#ifndef [A-Z_]+$/ {

[Severity: Low]
Does this strict regex fail to match include guards containing numbers?

For standard ARM64 KVM headers that contain numbers in their guards (e.g., __ARM64_KVM_ARM_H__), this fails to match.

This silently omits the 'This header was copied from...' preamble in the generated files.

Quoted 3 lines
arch/s390/tools/copy-arm64h-full.awk new file +3 -0
+	guard_name = $2
+	guard_found = 1
+	print "/* This header was copied from " srcfile " */"
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
author reviewer
#30 lore raw

PATCH v4 12/27 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/Makefile.gen    | 30 +++++++++++++
 arch/s390/kvm/arm64/copy-arm64c.awk | 69 +++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+)
 create mode 100644 arch/s390/kvm/arm64/Makefile.gen
 create mode 100644 arch/s390/kvm/arm64/copy-arm64c.awk
arch/s390/kvm/arm64/Makefile.gen new file +30 -0
new file mode 100644
index 000000000000..2e8838bc6921
@@ -0,0 +1,30 @@
+# 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 \
+	reset.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 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
+
+$(addprefix $(obj)/,$(kvm-arm64-obj)): $(addprefix $(obj)/,$(ARM64_INC_FILES))
+
+$(foreach cfile,$(basename $(ARM64_CFILES)),\
+	$(eval $(obj)/$(cfile).o: $(obj)/generated/$(cfile).inc))
arch/s390/kvm/arm64/copy-arm64c.awk new file +69 -0

69 lines not on this page · raw message

Signature · 1 line
2.53.0
#31 lore raw

Re: [PATCH v4 12/27] KVM: s390: Use arm64 code

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Low] Sharing C code between architectures via textual extraction using awk and architecture-specific…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Low] Sharing C code between architectures via textual extraction using awk and architecture-specific markers is highly irregular and fragile.
- [Low] The #undef __INCL_GEN_ARM_FILE directive emitted by the awk script is placed inside the #ifndef block, rendering it unreachable and useless.
- [Low] The awk script silently succeeds and extracts the entire remainder of the file if an end marker is missing.
--

commit 984c79bce287186aa6e2e94e2238ab50e9164186
Author: Steffen Eiden <[email protected]>

KVM: s390: Use arm64 code

This commit adds the infrastructure to extract KVM/arm64 code into s390 at build time. It introduces an awk script that detects ARM64_S390_COMMON markers and extracts marked sections into an .inc file for consumption.

Quoted 6 lines
arch/s390/kvm/arm64/copy-arm64c.awk new file +0 -0
new file mode 100644
index 0000000000000..45d9c7034c177

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

Quoted 8 lines
arch/s390/kvm/arm64/copy-arm64c.awk new file +8 -0
@@ -0,0 +1,69 @@
+#!/usr/bin/awk -f
+# SPDX-License-Identifier: GPL-2.0
+#
+# Extract marked sections from ARM64 C files for sharing with s390 KVM
+#
+# Usage: share-arm64-cfile.awk <input_file>
+#
+# Extracts all sections between start/end markers. If no markers found, signals failure.

[Severity: Low]
Is this code sharing approach via build-time regex extraction the intended long-term design? Sharing C code between architectures by using awk to extract textual blocks based on markers seems unusual and could obscure dependencies or complicate maintenance compared to traditional code sharing via common files or headers.

[ ... ]

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

Quoted 4 lines
arch/s390/kvm/arm64/copy-arm64c.awk new file +4 -0
+	print "#ifndef __INCL_GEN_ARM_FILE"
+	print "#error included .inc file w/o proper guard definition"
+	print "#undef __INCL_GEN_ARM_FILE"
+	print "#endif /* __INCL_GEN_ARM_FILE */"

[Severity: Low]
Is the placement of the #undef directive correct here?

If a caller correctly defines __INCL_GEN_ARM_FILE before inclusion, the preprocessor evaluates the #ifndef to false and skips the entire block. If it is not defined, the #error halts compilation. In either case, the #undef inside the #ifndef block appears to be unreachable.

[ ... ]

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

Quoted 10 lines
arch/s390/kvm/arm64/copy-arm64c.awk new file +10 -0
+copying {
+	gsub(/#include <asm\//, "#include <arm64/")
+	gsub(/#include <uapi\/asm\//, "#include <uapi/arm64/")
+	print
+	next
+}
+
+END {
+	exit !found_marker
+}

[Severity: Low]
Does this END block handle a missing end marker properly?

If an ARM64_S390_COMMON end marker is accidentally omitted or malformed in the source file, the awk script leaves the copying flag set to 1 and dumps the remainder of the file. Since found_marker is true, the script will exit with status 0 (success). Could we check if copying == 0 to catch unbalanced markers during extraction instead of having it fail later during C compilation?

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

PATCH v4 13/27 KVM: s390: Prepare KVM/s390 for a second KVM module

In reply to Steffen Eiden

Lay the groundwork for a second KVM module. Guard KVM/s390 behind the KVM_S390 config and turn CONFIG_KVM on if selected. Move all the kvm_host definitions to _s390 postfixed headers. Prepare local header include guards for a new location. Move gamp related tracing into a separate file. While at it, rename the main KVM/s390 files from s390-kvm.{c,h} to just s390.{c,h} to match the naming scheme of other architectures.

No functional change.

---
 arch/s390/Kconfig                             |   2 +-
 arch/s390/boot/ipl_parm.c                     |   2 +-
 arch/s390/boot/uv.c                           |   2 +-
 arch/s390/configs/debug_defconfig             |   2 +-
 arch/s390/configs/defconfig                   |   2 +-
 arch/s390/include/asm/kvm_host.h              | 774 +-----------------
 .../asm/{kvm_host.h => kvm_host_s390.h}       |   6 +-
 ...kvm_host_types.h => kvm_host_s390_types.h} |   6 +-
 arch/s390/include/asm/kvm_host_types.h        | 347 +-------
 arch/s390/kernel/early.c                      |   2 +-
 arch/s390/kernel/entry.S                      |  10 +-
 arch/s390/kernel/perf_event.c                 |   2 +-
 arch/s390/kvm/Kconfig                         |  10 +-
 arch/s390/kvm/Makefile                        |   2 +-
 arch/s390/kvm/dat.h                           |   6 +-
 arch/s390/kvm/diag.c                          |   2 +-
 arch/s390/kvm/faultin.c                       |   3 +-
 arch/s390/kvm/faultin.h                       |   6 +-
 arch/s390/kvm/gaccess.c                       |   2 +-
 arch/s390/kvm/gaccess.h                       |   2 +-
 arch/s390/kvm/gmap.c                          |   2 +-
 arch/s390/kvm/gmap.h                          |   6 +-
 arch/s390/kvm/guestdbg.c                      |   2 +-
 arch/s390/kvm/intercept.c                     |   2 +-
 arch/s390/kvm/interrupt.c                     |   2 +-
 arch/s390/kvm/pci.c                           |   2 +-
 arch/s390/kvm/priv.c                          |   2 +-
 arch/s390/kvm/pv.c                            |   2 +-
 arch/s390/kvm/{kvm-s390.c => s390.c}          |   2 +-
 arch/s390/kvm/{kvm-s390.h => s390.h}          |   2 +-
 arch/s390/kvm/sigp.c                          |   2 +-
 arch/s390/kvm/trace-gmap.h                    |  45 +
 arch/s390/kvm/trace.h                         |  14 -
 arch/s390/kvm/vsie.c                          |   2 +-
 include/linux/kvm_host.h                      |   2 +-
 35 files changed, 103 insertions(+), 1176 deletions(-)
 copy arch/s390/include/asm/{kvm_host.h => kvm_host_s390.h} (99%)
 copy arch/s390/include/asm/{kvm_host_types.h => kvm_host_s390_types.h} (98%)
 rename arch/s390/kvm/{kvm-s390.c => s390.c} (99%)
 rename arch/s390/kvm/{kvm-s390.h => s390.h} (99%)
 create mode 100644 arch/s390/kvm/trace-gmap.h
arch/s390/Kconfig +1 -1
index 84404e6778d5..b2eaa2000b10 100644
@@ -834,7 +834,7 @@ config VFIO_CCW
 config VFIO_AP
 	def_tristate n
 	prompt "VFIO support for AP devices"
-	depends on KVM
+	depends on KVM_S390
 	depends on VFIO
 	depends on AP
 	select VFIO_MDEV
arch/s390/boot/ipl_parm.c +1 -1
index 6bc950b92be7..b8906ba7e5e8 100644
@@ -300,7 +300,7 @@ void parse_boot_command_line(void)
 			stack_protector_debug = 1;
 #endif
 
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 		if (!strcmp(param, "prot_virt")) {
 			rc = kstrtobool(val, &enabled);
 			if (!rc && enabled)
arch/s390/boot/uv.c +1 -1
index 4568e8f81dac..f7b68bdcedc1 100644
@@ -26,7 +26,7 @@ void uv_query_info(void)
 	if (uv_call(0, (uint64_t)&uvcb) && uvcb.header.rc != UVC_RC_MORE_DATA)
 		return;
 
-	if (IS_ENABLED(CONFIG_KVM)) {
+	if (IS_ENABLED(CONFIG_KVM_S390)) {
 		memcpy(uv_info.inst_calls_list, uvcb.inst_calls_list, sizeof(uv_info.inst_calls_list));
 		uv_info.uv_base_stor_len = uvcb.uv_base_stor_len;
 		uv_info.guest_base_stor_len = uvcb.conf_base_phys_stor_len;
arch/s390/configs/debug_defconfig +1 -1
index 54637be87fb7..e9ad718a9408 100644
@@ -63,7 +63,7 @@ CONFIG_IOMMUFD=y
 CONFIG_CMM=m
 CONFIG_APPLDATA_BASE=y
 CONFIG_S390_HYPFS_FS=y
-CONFIG_KVM=m
+CONFIG_KVM_S390=m
 CONFIG_KVM_S390_UCONTROL=y
 CONFIG_S390_UNWIND_SELFTEST=m
 CONFIG_S390_KPROBES_SANITY_TEST=m
arch/s390/configs/defconfig +1 -1
index 5f5114a253cf..327ab4a3933c 100644
@@ -61,7 +61,7 @@ CONFIG_IOMMUFD=y
 CONFIG_CMM=m
 CONFIG_APPLDATA_BASE=y
 CONFIG_S390_HYPFS_FS=y
-CONFIG_KVM=m
+CONFIG_KVM_S390=m
 CONFIG_S390_UNWIND_SELFTEST=m
 CONFIG_S390_KPROBES_SANITY_TEST=m
 CONFIG_S390_MODULES_SANITY_TEST=m
arch/s390/include/asm/kvm_host.h +2 -772

780 lines not on this page · raw message

arch/s390/include/asm/kvm_host_s390.h renamed from arch/s390/include/asm/kvm_host.h +3 -3
similarity index 99%
copy from arch/s390/include/asm/kvm_host.h
copy to arch/s390/include/asm/kvm_host_s390.h
index eaa34c5bd3c1..5293b0067422 100644
@@ -8,8 +8,8 @@
  */
 
 
-#ifndef ASM_KVM_HOST_H
-#define ASM_KVM_HOST_H
+#ifndef ASM_KVM_HOST_S390_H
+#define ASM_KVM_HOST_S390_H
 
 #include <linux/types.h>
 #include <linux/hrtimer.h>
@@ -775,4 +775,4 @@ struct zpci_kvm_hook {
 
 extern struct zpci_kvm_hook zpci_kvm_hook;
 
-#endif
+#endif /* ASM_KVM_HOST_S390_H */
arch/s390/include/asm/kvm_host_s390_types.h renamed from arch/s390/include/asm/kvm_host_types.h +3 -3
similarity index 98%
copy from arch/s390/include/asm/kvm_host_types.h
copy to arch/s390/include/asm/kvm_host_s390_types.h
index 3f50942bdfe6..5cf746095bfd 100644
@@ -1,7 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 
-#ifndef _ASM_KVM_HOST_TYPES_H
-#define _ASM_KVM_HOST_TYPES_H
+#ifndef _ASM_KVM_HOST_S390_TYPES_H
+#define _ASM_KVM_HOST_S390_TYPES_H
 
 #include <linux/atomic.h>
 #include <linux/types.h>
@@ -344,4 +344,4 @@ struct sie_page {
 	__u8 reserved700[2304];		/* 0x0700 */
 };
 
-#endif /* _ASM_KVM_HOST_TYPES_H */
+#endif /* _ASM_KVM_HOST_S390_TYPES_H */
arch/s390/include/asm/kvm_host_types.h +4 -343

351 lines not on this page · raw message

arch/s390/kernel/early.c +1 -1
index b27239c03d79..464f8918d447 100644
@@ -57,7 +57,7 @@ decompressor_handled_param(cmma);
 decompressor_handled_param(relocate_lowcore);
 decompressor_handled_param(bootdebug);
 __decompressor_handled_param(debug_alternative, debug-alternative);
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 decompressor_handled_param(prot_virt);
 #endif
 
arch/s390/kernel/entry.S +5 -5
index 79a45efae23d..9572620fcaa6 100644
@@ -113,7 +113,7 @@ _LPP_OFFSET	= __LC_LPP
 		    "jnz .+8; .insn rrf,0xb2e80000,0,0,13,0", ALT_SPEC(82)
 	.endm
 
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 	.macro SIEEXIT sie_control,lowcore
 	lg	%r9,\sie_control			# get control block pointer
 	ni	__SIE_PROG0C+3(%r9),0xfe		# no longer in SIE
@@ -184,7 +184,7 @@ EXPORT_SYMBOL(__WARN_trap)
 
 #endif /* CONFIG_BUG && CONFIG_CC_HAS_ASM_IMMEDIATE_STRINGS */
 
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 /*
  * __sie64a calling convention:
  * %r2 pointer to sie control block phys
@@ -315,7 +315,7 @@ SYM_CODE_START(pgm_check_handler)
 	xgr	%r10,%r10
 	tmhh	%r8,0x0001		# coming from user space?
 	jo	3f			# -> fault in user space
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 	lg	%r11,__LC_CURRENT(%r13)
 	tm	__TI_sie(%r11),0xff
 	jz	1f
@@ -397,7 +397,7 @@ SYM_CODE_START(\name)
 	lmg	%r8,%r9,\lc_old_psw(%r13)
 	tmhh	%r8,0x0001			# interrupting from user ?
 	jnz	1f
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 	lg	%r10,__LC_CURRENT(%r13)
 	tm	__TI_sie(%r10),0xff
 	jz	0f
@@ -475,7 +475,7 @@ SYM_CODE_START(mcck_int_handler)
 	jnz	.Lmcck_user
 	TSTMSK	__LC_MCCK_CODE(%r13),MCCK_CODE_PSW_IA_VALID
 	jno	.Lmcck_panic
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 	lg	%r10,__LC_CURRENT(%r13)
 	tm	__TI_sie(%r10),0xff
 	jz	.Lmcck_user
arch/s390/kernel/perf_event.c +1 -1
index 606750bae508..6441746b8e72 100644
@@ -37,7 +37,7 @@ static bool is_in_guest(struct pt_regs *regs)
 {
 	if (user_mode(regs))
 		return false;
-#if IS_ENABLED(CONFIG_KVM)
+#if IS_ENABLED(CONFIG_KVM_S390)
 	return instruction_pointer(regs) == (unsigned long) &sie_exit;
 #else
 	return false;
arch/s390/kvm/Kconfig +7 -3
index 8d3ee17a1bcb..fbf4162d6f2d 100644
@@ -17,8 +17,12 @@ menuconfig VIRTUALIZATION
 if VIRTUALIZATION
 
 config KVM
+	tristate
+
+config KVM_S390
 	def_tristate y
-	prompt "Kernel-based Virtual Machine (KVM) support"
+	prompt "Kernel-based Virtual Machine (KVM) support (s390)"
+	select KVM
 	select HAVE_KVM_CPU_RELAX_INTERCEPT
 	select KVM_ASYNC_PF
 	select KVM_ASYNC_PF_SYNC
@@ -32,7 +36,7 @@ config KVM
 	select KVM_MMU_LOCKLESS_AGING
 	select KVM_GENERIC_PRE_FAULT_MEMORY
 	help
-	  Support hosting paravirtualized guest machines using the SIE
+	  Support hosting paravirtualized s390 guest machines using the SIE
 	  virtualization capability on the mainframe. This should work
 	  on any 64bit machine.
 
@@ -46,7 +50,7 @@ config KVM
 
 config KVM_S390_UCONTROL
 	bool "Userspace controlled virtual machines"
-	depends on KVM
+	depends on KVM_S390
 	help
 	  Allow CAP_SYS_ADMIN users to create KVM virtual machines that are
 	  controlled by userspace.
arch/s390/kvm/Makefile +1 -1
index dac9d53b23d8..df10063ae042 100644
@@ -7,7 +7,7 @@ include $(srctree)/virt/kvm/Makefile.kvm
 
 ccflags-y := -Ivirt/kvm -Iarch/s390/kvm
 
-kvm-y += kvm-s390.o intercept.o interrupt.o priv.o sigp.o
+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 += dat.o gmap.o faultin.o
 
arch/s390/kvm/dat.h +3 -3
index fad605305e05..711ae2f96107 100644
@@ -6,8 +6,8 @@
  *    Author(s): Claudio Imbrenda <[email protected]>
  */
 
-#ifndef __KVM_S390_DAT_H
-#define __KVM_S390_DAT_H
+#ifndef ARCH_KVM_GMAP_DAT_H
+#define ARCH_KVM_GMAP_DAT_H
 
 #include <linux/radix-tree.h>
 #include <linux/refcount.h>
@@ -975,4 +975,4 @@ static inline bool crste_is_ucas(union crste crste)
 	return is_pmd(crste) && crste.h.i && crste.h.fc0.tl == 1 && crste.h.fc == 0;
 }
 
-#endif /* __KVM_S390_DAT_H */
+#endif /* ARCH_KVM_GMAP_DAT_H */
arch/s390/kvm/diag.c +1 -1
index d89d1c381522..700d9b7b68bc 100644
@@ -12,7 +12,7 @@
 #include <linux/kvm_host.h>
 #include <asm/gmap_helpers.h>
 #include <asm/virtio-ccw.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "trace.h"
 #include "trace-s390.h"
 #include "gaccess.h"
arch/s390/kvm/faultin.c +2 -1
index fee80047bd94..740415d494de 100644
@@ -9,10 +9,11 @@
 #include <linux/kvm_host.h>
 
 #include "gmap.h"
-#include "trace.h"
 #include "faultin.h"
 
 bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu);
+#define CREATE_TRACE_POINTS
+#include "trace-gmap.h"
 
 /*
  * kvm_s390_faultin_gfn() - handle a dat fault.
arch/s390/kvm/faultin.h +3 -3
index f86176d2769c..f343b6fb6f16 100644
@@ -6,8 +6,8 @@
  *    Author(s): Claudio Imbrenda <[email protected]>
  */
 
-#ifndef __KVM_S390_FAULTIN_H
-#define __KVM_S390_FAULTIN_H
+#ifndef ARCH_KVM_GMAP_FAULTIN_H
+#define ARCH_KVM_GMAP_FAULTIN_H
 
 #include <linux/kvm_host.h>
 
@@ -89,4 +89,4 @@ static inline int kvm_s390_get_guest_pages(struct kvm *kvm, struct guest_fault *
 #define kvm_s390_array_needs_retry_safe(kvm, seq, array) \
 	kvm_s390_multiple_faults_need_retry(kvm, seq, array, ARRAY_SIZE(array), false)
 
-#endif /* __KVM_S390_FAULTIN_H */
+#endif /* ARCH_KVM_GMAP_FAULTIN_H */
arch/s390/kvm/gaccess.c +1 -1
index 36102b2727fb..2a4b2525329a 100644
@@ -17,7 +17,7 @@
 #include <asm/access-regs.h>
 #include <asm/fault.h>
 #include <asm/dat-bits.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "dat.h"
 #include "gmap.h"
 #include "gaccess.h"
arch/s390/kvm/gaccess.h +1 -1
index b5385cec60f4..ef922b3b4990 100644
@@ -14,7 +14,7 @@
 #include <linux/kvm_host.h>
 #include <linux/uaccess.h>
 #include <linux/ptrace.h>
-#include "kvm-s390.h"
+#include "s390.h"
 
 /**
  * kvm_s390_real_to_abs - convert guest real address to guest absolute address
arch/s390/kvm/gmap.c +1 -1
index 298fbaecec28..8773aa34f107 100644
@@ -21,7 +21,7 @@
 
 #include "dat.h"
 #include "gmap.h"
-#include "kvm-s390.h"
+#include "s390.h"
 #include "faultin.h"
 
 static inline bool kvm_s390_is_in_sie(struct kvm_vcpu *vcpu)
arch/s390/kvm/gmap.h +3 -3
index 39938d363ec9..c54c35e47d6d 100644
@@ -7,8 +7,8 @@
  *               Claudio Imbrenda <[email protected]>
  */
 
-#ifndef ARCH_KVM_S390_GMAP_H
-#define ARCH_KVM_S390_GMAP_H
+#ifndef ARCH_KVM_GMAP_GMAP_H
+#define ARCH_KVM_GMAP_GMAP_H
 
 #include "dat.h"
 
@@ -330,4 +330,4 @@ static inline bool gmap_is_shadow_valid(struct gmap *sg, union asce asce, int ed
 	return sg->guest_asce.val == asce.val && sg->edat_level == edat_level;
 }
 
-#endif /* ARCH_KVM_S390_GMAP_H */
+#endif /* ARCH_KVM_GMAP_GMAP_H */
arch/s390/kvm/guestdbg.c +1 -1
index 69835e1d4f20..c880ebbdef2f 100644
@@ -8,7 +8,7 @@
  */
 #include <linux/kvm_host.h>
 #include <linux/errno.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "gaccess.h"
 
 /*
arch/s390/kvm/intercept.c +1 -1
index 1980df61ef30..ca1205dfac8b 100644
@@ -17,7 +17,7 @@
 #include <asm/sysinfo.h>
 #include <asm/uv.h>
 
-#include "kvm-s390.h"
+#include "s390.h"
 #include "gaccess.h"
 #include "trace.h"
 #include "trace-s390.h"
arch/s390/kvm/interrupt.c +1 -1
index 9e3e6b0d72ad..24c41c7a4c3d 100644
@@ -29,7 +29,7 @@
 #include <asm/nmi.h>
 #include <asm/airq.h>
 #include <asm/tpi.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "gaccess.h"
 #include "trace-s390.h"
 #include "pci.h"
arch/s390/kvm/pci.c +1 -1
index 5b075c38998e..efdc1ddf6ba8 100644
@@ -14,7 +14,7 @@
 #include <asm/pci_io.h>
 #include <asm/sclp.h>
 #include "pci.h"
-#include "kvm-s390.h"
+#include "s390.h"
 
 struct zpci_aift *aift;
 
arch/s390/kvm/priv.c +1 -1
index ad0ddc433a73..21479f0a5e5a 100644
@@ -26,7 +26,7 @@
 #include <asm/ap.h>
 #include <asm/gmap_helpers.h>
 #include "gaccess.h"
-#include "kvm-s390.h"
+#include "s390.h"
 #include "trace.h"
 #include "gmap.h"
 
arch/s390/kvm/pv.c +1 -1
index 1beacc841ca8..f6e03e9d371a 100644
@@ -18,7 +18,7 @@
 #include <linux/sched/mm.h>
 #include <linux/mmu_notifier.h>
 #include <asm/gmap_helpers.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "dat.h"
 #include "gaccess.h"
 #include "gmap.h"
arch/s390/kvm/s390.c renamed from arch/s390/kvm/kvm-s390.c +1 -1
similarity index 99%
rename from arch/s390/kvm/kvm-s390.c
rename to arch/s390/kvm/s390.c
index 23c817595e28..26720ffcf5db 100644
@@ -50,7 +50,7 @@
 #include <asm/fpu.h>
 #include <asm/ap.h>
 #include <asm/uv.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "gaccess.h"
 #include "gmap.h"
 #include "faultin.h"
arch/s390/kvm/s390.h renamed from arch/s390/kvm/kvm-s390.h +1 -1
similarity index 99%
rename from arch/s390/kvm/kvm-s390.h
rename to arch/s390/kvm/s390.h
index 6d2842fb71a3..e144ca2e6d5e 100644
@@ -452,7 +452,7 @@ void kvm_s390_vsie_destroy(struct kvm *kvm);
 int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu);
 int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu);
 
-/* implemented in kvm-s390.c */
+/* implemented in s390.c */
 int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod);
 int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long addr);
 int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr);
arch/s390/kvm/sigp.c +1 -1
index 55c34cb35428..131b3371ef4f 100644
@@ -14,7 +14,7 @@
 #include <linux/slab.h>
 #include <asm/sigp.h>
 #include "gaccess.h"
-#include "kvm-s390.h"
+#include "s390.h"
 #include "trace.h"
 
 static int __sigp_sense(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
arch/s390/kvm/trace-gmap.h new file +45 -0

45 lines not on this page · raw message

arch/s390/kvm/trace.h +0 -14
index aa419eb6a0c8..97774fe09a85 100644
@@ -45,20 +45,6 @@ TRACE_EVENT(kvm_s390_skey_related_inst,
 	    VCPU_TP_PRINTK("%s", "storage key related instruction")
 	);
 
-TRACE_EVENT(kvm_s390_major_guest_pfault,
-	    TP_PROTO(VCPU_PROTO_COMMON),
-	    TP_ARGS(VCPU_ARGS_COMMON),
-
-	    TP_STRUCT__entry(
-		    VCPU_FIELD_COMMON
-		    ),
-
-	    TP_fast_assign(
-		    VCPU_ASSIGN_COMMON
-		    ),
-	    VCPU_TP_PRINTK("%s", "major fault, maybe applicable for pfault")
-	);
-
 TRACE_EVENT(kvm_s390_pfault_init,
 	    TP_PROTO(VCPU_PROTO_COMMON, long pfault_token),
 	    TP_ARGS(VCPU_ARGS_COMMON, pfault_token),
arch/s390/kvm/vsie.c +1 -1
index eea24562e7db..c4a03b310fea 100644
@@ -23,7 +23,7 @@
 #include <asm/nmi.h>
 #include <asm/dis.h>
 #include <asm/facility.h>
-#include "kvm-s390.h"
+#include "s390.h"
 #include "gaccess.h"
 #include "gmap.h"
 
include/linux/kvm_host.h +1 -1
index f179a9230e30..bd380a259910 100644
@@ -1538,7 +1538,7 @@ void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu);
 bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu);
 
-#ifndef CONFIG_S390
+#ifndef CONFIG_KVM_S390
 void __kvm_vcpu_kick(struct kvm_vcpu *vcpu, bool wait);
 
 static inline void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
Signature · 1 line
2.53.0
#33 lore raw

Re: [PATCH v4 13/27] KVM: s390: Prepare KVM/s390 for a second KVM module

Am 06.07.26 um 10:52 schrieb Steffen Eiden:

Quoted 9 lines

Lay the groundwork for a second KVM module. Guard KVM/s390 behind the KVM_S390 config and turn CONFIG_KVM on if selected. Move all the kvm_host definitions to _s390 postfixed headers. Prepare local header include guards for a new location. Move gamp related tracing into a separate file. While at it, rename the main KVM/s390 files from s390-kvm.{c,h} to just s390.{c,h} to match the naming scheme of other architectures.

No functional change.

[...]

Quoted 3 lines
  arch/s390/include/asm/kvm_host.h              | 774 +-----------------
  .../asm/{kvm_host.h => kvm_host_s390.h}       |   6 +-
  arch/s390/include/asm/kvm_host_types.h        | 347 +-------

[...]

Quoted 2 lines

copy arch/s390/include/asm/{kvm_host.h => kvm_host_s390.h} (99%) copy arch/s390/include/asm/{kvm_host_types.h => kvm_host_s390_types.h} (98%)

This is just an "almost" rename only in the end. Maybe we can convince git to show a better diffstat to reflect that?

In any case, I guess we can handle patch 13 and patch 14 pretty soon upfront so that the patch series gets smaller and is easier to review.

author reviewer
#34 lore raw

Re: [PATCH v4 13/27] KVM: s390: Prepare KVM/s390 for a second KVM module

Quoted 13 lines · Christian Borntraeger

Am 06.07.26 um 10:52 schrieb Steffen Eiden:

Quoted 9 lines

Lay the groundwork for a second KVM module. Guard KVM/s390 behind the KVM_S390 config and turn CONFIG_KVM on if selected. Move all the kvm_host definitions to _s390 postfixed headers. Prepare local header include guards for a new location. Move gamp related tracing into a separate file. While at it, rename the main KVM/s390 files from s390-kvm.{c,h} to just s390.{c,h} to match the naming scheme of other architectures.

No functional change.

Quoted 9 lines

[...]

Quoted 3 lines
  arch/s390/include/asm/kvm_host.h              | 774 +-----------------
  .../asm/{kvm_host.h => kvm_host_s390.h}       |   6 +-
  arch/s390/include/asm/kvm_host_types.h        | 347 +-------

[...]

Quoted 2 lines

copy arch/s390/include/asm/{kvm_host.h => kvm_host_s390.h} (99%) copy arch/s390/include/asm/{kvm_host_types.h => kvm_host_s390_types.h} (98%)

This is just an "almost" rename only in the end. Maybe we can convince git to show a better diffstat to reflect that?

The rename only part worked quite well (6 lined diff). I used --find-copies / --break-rewrites with format-patch when creating this patchfile to reduce the diff. Otherwhise we get a diff like:

...

 arch/s390/include/asm/kvm_host.h            | 774 +------------------
 arch/s390/include/asm/kvm_host_s390.h       | 778 ++++++++++++++++++++
 arch/s390/include/asm/kvm_host_s390_types.h | 347 +++++++++
 arch/s390/include/asm/kvm_host_types.h      | 347 +--------

...

Which I think is more confusing and bloats the diff unessecary.

But I cannot find an option to convince git that kvm_host.h should be treated as a new file. This problem seems to be out of scope for gits heuristics.

The only way to get this smaller is to split up the patch but then the first of the two patches will not compile.

Quoted 1 line

In any case, I guess we can handle patch 13 and patch 14 pretty soon upfront so that the patch series gets smaller and is easier to review.

yes +1.
I would argue that the gmap refactorings (PATCH 15-17) can get handled upfront as well. They also have no functional changes as well.

FYI: I made some smaller cleanups on those refactrrings since monday.

Steffen

maintainer
#35 lore raw

Re: [PATCH v4 13/27] KVM: s390: Prepare KVM/s390 for a second KVM module

In reply to Steffen Eiden
Quoted 11 lines · On Mon, 6 Jul 2026 10:52:13 +0200 Steffen Eiden <[email protected]> wrote:

Lay the groundwork for a second KVM module. Guard KVM/s390 behind the KVM_S390 config and turn CONFIG_KVM on if selected. Move all the kvm_host definitions to _s390 postfixed headers. Prepare local header include guards for a new location. Move gamp related tracing into a separate file. While at it, rename the main KVM/s390 files from s390-kvm.{c,h} to just s390.{c,h} to match the naming scheme of other architectures.

No functional change.

Quoted 9 lines
---
 arch/s390/Kconfig                             |   2 +-
 arch/s390/boot/ipl_parm.c                     |   2 +-
 arch/s390/boot/uv.c                           |   2 +-
 arch/s390/configs/debug_defconfig             |   2 +-
 arch/s390/configs/defconfig                   |   2 +-
 arch/s390/include/asm/kvm_host.h              | 774 +-----------------
 .../asm/{kvm_host.h => kvm_host_s390.h}       |   6 +-
 ...kvm_host_types.h => kvm_host_s390_types.h} |   6 +-

I really dislike those "almost renames". I'd prefer to have a prior patch with the differences, and a second patch for the move.

Quoted 28 lines
 arch/s390/include/asm/kvm_host_types.h        | 347 +-------
 arch/s390/kernel/early.c                      |   2 +-
 arch/s390/kernel/entry.S                      |  10 +-
 arch/s390/kernel/perf_event.c                 |   2 +-
 arch/s390/kvm/Kconfig                         |  10 +-
 arch/s390/kvm/Makefile                        |   2 +-
 arch/s390/kvm/dat.h                           |   6 +-
 arch/s390/kvm/diag.c                          |   2 +-
 arch/s390/kvm/faultin.c                       |   3 +-
 arch/s390/kvm/faultin.h                       |   6 +-
 arch/s390/kvm/gaccess.c                       |   2 +-
 arch/s390/kvm/gaccess.h                       |   2 +-
 arch/s390/kvm/gmap.c                          |   2 +-
 arch/s390/kvm/gmap.h                          |   6 +-
 arch/s390/kvm/guestdbg.c                      |   2 +-
 arch/s390/kvm/intercept.c                     |   2 +-
 arch/s390/kvm/interrupt.c                     |   2 +-
 arch/s390/kvm/pci.c                           |   2 +-
 arch/s390/kvm/priv.c                          |   2 +-
 arch/s390/kvm/pv.c                            |   2 +-
 arch/s390/kvm/{kvm-s390.c => s390.c}          |   2 +-
 arch/s390/kvm/{kvm-s390.h => s390.h}          |   2 +-
 arch/s390/kvm/sigp.c                          |   2 +-
 arch/s390/kvm/trace-gmap.h                    |  45 +
 arch/s390/kvm/trace.h                         |  14 -
 arch/s390/kvm/vsie.c                          |   2 +-
 include/linux/kvm_host.h                      |   2 +-
 35 files changed, 103 insertions(+), 1176 deletions(-)

[...]

author reviewer
#36 lore raw

Re: [PATCH v4 13/27] KVM: s390: Prepare KVM/s390 for a second KVM module

Quoted 24 lines · Claudio Imbrenda
Quoted 11 lines · On Mon, 6 Jul 2026 10:52:13 +0200 Steffen Eiden <[email protected]> wrote:

Lay the groundwork for a second KVM module. Guard KVM/s390 behind the KVM_S390 config and turn CONFIG_KVM on if selected. Move all the kvm_host definitions to _s390 postfixed headers. Prepare local header include guards for a new location. Move gamp related tracing into a separate file. While at it, rename the main KVM/s390 files from s390-kvm.{c,h} to just s390.{c,h} to match the naming scheme of other architectures.

No functional change.

Quoted 9 lines
---
 arch/s390/Kconfig                             |   2 +-
 arch/s390/boot/ipl_parm.c                     |   2 +-
 arch/s390/boot/uv.c                           |   2 +-
 arch/s390/configs/debug_defconfig             |   2 +-
 arch/s390/configs/defconfig                   |   2 +-
 arch/s390/include/asm/kvm_host.h              | 774 +-----------------
 .../asm/{kvm_host.h => kvm_host_s390.h}       |   6 +-
 ...kvm_host_types.h => kvm_host_s390_types.h} |   6 +-
Quoted 2 lines

I really dislike those "almost renames". I'd prefer to have a prior patch with the differences, and a second patch for the move.

I dislike this to. I tried to reduce it to a minimum.
I could prepend a patch that changes the macro guard before this patch. This is the change in question.

In fact the whole purpose of this patch is to reduce the almost renames and let the next patch be a pure move ( minus one keeping stuff compile change)

Quoted 14 lines
Quoted 14 lines
 arch/s390/include/asm/kvm_host_types.h        | 347 +-------
 arch/s390/kernel/early.c                      |   2 +-
 arch/s390/kernel/entry.S                      |  10 +-
 arch/s390/kernel/perf_event.c                 |   2 +-
 arch/s390/kvm/Kconfig                         |  10 +-
 arch/s390/kvm/Makefile                        |   2 +-
 arch/s390/kvm/dat.h                           |   6 +-
 arch/s390/kvm/diag.c                          |   2 +-
 arch/s390/kvm/faultin.c                       |   3 +-
 arch/s390/kvm/faultin.h                       |   6 +-
 arch/s390/kvm/gaccess.c                       |   2 +-
 arch/s390/kvm/gaccess.h                       |   2 +-
 arch/s390/kvm/gmap.c                          |   2 +-
Quoted 8 lines
Quoted 8 lines
 arch/s390/kvm/gmap.h                          |   6 +-
 arch/s390/kvm/guestdbg.c                      |   2 +-
 arch/s390/kvm/intercept.c                     |   2 +-
 arch/s390/kvm/interrupt.c                     |   2 +-
 arch/s390/kvm/pci.c                           |   2 +-
 arch/s390/kvm/priv.c                          |   2 +-
 arch/s390/kvm/pv.c                            |   2 +-
 arch/s390/kvm/{kvm-s390.c => s390.c}          |   2 +-

this almost move is required by the compiler (change header form kvm-s390.h to s390.h)

Quoted 1 line
Quoted 1 line
 arch/s390/kvm/{kvm-s390.h => s390.h}          |   2 +-

this change in theory could be prepended but would leave the comment in place invalid for one patch.

Quoted 8 lines
Quoted 6 lines
 arch/s390/kvm/sigp.c                          |   2 +-
 arch/s390/kvm/trace-gmap.h                    |  45 +
 arch/s390/kvm/trace.h                         |  14 -
 arch/s390/kvm/vsie.c                          |   2 +-
 include/linux/kvm_host.h                      |   2 +-
 35 files changed, 103 insertions(+), 1176 deletions(-)

[...]

Steffen

author reviewer
#37 lore raw

PATCH v4 14/27 KVM: s390: Move s390 kvm code into a subdirectory

In reply to Steffen Eiden

Move all the code required to run s390 KVM guests on s390 to a s390 subdirectory. Move gmap related code into a gmap directory to later share gmap code between KVM implementations.

No functional change.

---
 arch/s390/kvm/Kconfig                 | 38 +--------------------------
 arch/s390/kvm/Makefile                | 11 +-------
 arch/s390/kvm/gmap/Makefile           |  5 ++++
 arch/s390/kvm/{ => gmap}/dat.c        |  0
 arch/s390/kvm/{ => gmap}/dat.h        |  0
 arch/s390/kvm/{ => gmap}/faultin.c    |  0
 arch/s390/kvm/{ => gmap}/faultin.h    |  0
 arch/s390/kvm/{ => gmap}/gmap.c       |  0
 arch/s390/kvm/{ => gmap}/gmap.h       |  0
 arch/s390/kvm/{ => gmap}/trace-gmap.h |  1 +
 arch/s390/kvm/{ => s390}/Kconfig      | 21 +--------------
 arch/s390/kvm/{ => s390}/Makefile     |  8 +++---
 arch/s390/kvm/{ => s390}/diag.c       |  0
 arch/s390/kvm/{ => s390}/gaccess.c    |  0
 arch/s390/kvm/{ => s390}/gaccess.h    |  0
 arch/s390/kvm/{ => s390}/guestdbg.c   |  0
 arch/s390/kvm/{ => s390}/intercept.c  |  0
 arch/s390/kvm/{ => s390}/interrupt.c  |  0
 arch/s390/kvm/{ => s390}/pci.c        |  0
 arch/s390/kvm/{ => s390}/pci.h        |  0
 arch/s390/kvm/{ => s390}/priv.c       |  0
 arch/s390/kvm/{ => s390}/pv.c         |  0
 arch/s390/kvm/{ => s390}/s390.c       |  0
 arch/s390/kvm/{ => s390}/s390.h       |  0
 arch/s390/kvm/{ => s390}/sigp.c       |  0
 arch/s390/kvm/{ => s390}/trace-s390.h |  0
 arch/s390/kvm/{ => s390}/trace.h      |  0
 arch/s390/kvm/{ => s390}/vsie.c       |  0
 28 files changed, 14 insertions(+), 70 deletions(-)
 create mode 100644 arch/s390/kvm/gmap/Makefile
 rename arch/s390/kvm/{ => gmap}/dat.c (100%)
 rename arch/s390/kvm/{ => gmap}/dat.h (100%)
 rename arch/s390/kvm/{ => gmap}/faultin.c (100%)
 rename arch/s390/kvm/{ => gmap}/faultin.h (100%)
 rename arch/s390/kvm/{ => gmap}/gmap.c (100%)
 rename arch/s390/kvm/{ => gmap}/gmap.h (100%)
 rename arch/s390/kvm/{ => gmap}/trace-gmap.h (96%)
 copy arch/s390/kvm/{ => s390}/Kconfig (71%)
 copy arch/s390/kvm/{ => s390}/Makefile (64%)
 rename arch/s390/kvm/{ => s390}/diag.c (100%)
 rename arch/s390/kvm/{ => s390}/gaccess.c (100%)
 rename arch/s390/kvm/{ => s390}/gaccess.h (100%)
 rename arch/s390/kvm/{ => s390}/guestdbg.c (100%)
 rename arch/s390/kvm/{ => s390}/intercept.c (100%)
 rename arch/s390/kvm/{ => s390}/interrupt.c (100%)
 rename arch/s390/kvm/{ => s390}/pci.c (100%)
 rename arch/s390/kvm/{ => s390}/pci.h (100%)
 rename arch/s390/kvm/{ => s390}/priv.c (100%)
 rename arch/s390/kvm/{ => s390}/pv.c (100%)
 rename arch/s390/kvm/{ => s390}/s390.c (100%)
 rename arch/s390/kvm/{ => s390}/s390.h (100%)
 rename arch/s390/kvm/{ => s390}/sigp.c (100%)
 rename arch/s390/kvm/{ => s390}/trace-s390.h (100%)
 rename arch/s390/kvm/{ => s390}/trace.h (100%)
 rename arch/s390/kvm/{ => s390}/vsie.c (100%)
arch/s390/kvm/Kconfig +1 -37
index fbf4162d6f2d..f8d4a9a38dae 100644
@@ -19,42 +19,6 @@ if VIRTUALIZATION
 config KVM
 	tristate
 
-config KVM_S390
-	def_tristate y
-	prompt "Kernel-based Virtual Machine (KVM) support (s390)"
-	select KVM
-	select HAVE_KVM_CPU_RELAX_INTERCEPT
-	select KVM_ASYNC_PF
-	select KVM_ASYNC_PF_SYNC
-	select KVM_COMMON
-	select HAVE_KVM_IRQCHIP
-	select HAVE_KVM_IRQ_ROUTING
-	select HAVE_KVM_INVALID_WAKEUPS
-	select HAVE_KVM_NO_POLL
-	select KVM_VFIO
-	select VIRT_XFER_TO_GUEST_WORK
-	select KVM_MMU_LOCKLESS_AGING
-	select KVM_GENERIC_PRE_FAULT_MEMORY
-	help
-	  Support hosting paravirtualized s390 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_S390
-	help
-	  Allow CAP_SYS_ADMIN users to create KVM virtual machines that are
-	  controlled by userspace.
-
-	  If unsure, say N.
+source "arch/s390/kvm/s390/Kconfig"
 
 endif # VIRTUALIZATION
arch/s390/kvm/Makefile +1 -10
index df10063ae042..c43d7dffca13 100644
@@ -3,13 +3,4 @@
 #
 # Copyright IBM Corp. 2008
 
-include $(srctree)/virt/kvm/Makefile.kvm
-
-ccflags-y := -Ivirt/kvm -Iarch/s390/kvm
-
-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 += dat.o gmap.o faultin.o
-
-kvm-$(CONFIG_VFIO_PCI_ZDEV_KVM) += pci.o
-obj-$(CONFIG_KVM) += kvm.o
+obj-$(CONFIG_KVM_S390) += s390/
arch/s390/kvm/gmap/Makefile new file +5 -0
new file mode 100644
index 000000000000..21967ed88877
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+
+GMAP ?= ../gmap
+
+kvm-y += $(GMAP)/dat.o $(GMAP)/gmap.o $(GMAP)/faultin.o
arch/s390/kvm/gmap/dat.c renamed from arch/s390/kvm/dat.c +0 -0
similarity index 100%
rename from arch/s390/kvm/dat.c
rename to arch/s390/kvm/gmap/dat.c
arch/s390/kvm/gmap/dat.h renamed from arch/s390/kvm/dat.h +0 -0
similarity index 100%
rename from arch/s390/kvm/dat.h
rename to arch/s390/kvm/gmap/dat.h
arch/s390/kvm/gmap/faultin.c renamed from arch/s390/kvm/faultin.c +0 -0
similarity index 100%
rename from arch/s390/kvm/faultin.c
rename to arch/s390/kvm/gmap/faultin.c
arch/s390/kvm/gmap/faultin.h renamed from arch/s390/kvm/faultin.h +0 -0
similarity index 100%
rename from arch/s390/kvm/faultin.h
rename to arch/s390/kvm/gmap/faultin.h
arch/s390/kvm/gmap/gmap.c renamed from arch/s390/kvm/gmap.c +0 -0
similarity index 100%
rename from arch/s390/kvm/gmap.c
rename to arch/s390/kvm/gmap/gmap.c
arch/s390/kvm/gmap/gmap.h renamed from arch/s390/kvm/gmap.h +0 -0
similarity index 100%
rename from arch/s390/kvm/gmap.h
rename to arch/s390/kvm/gmap/gmap.h
arch/s390/kvm/gmap/trace-gmap.h renamed from arch/s390/kvm/trace-gmap.h +1 -0
similarity index 96%
rename from arch/s390/kvm/trace-gmap.h
rename to arch/s390/kvm/gmap/trace-gmap.h
index b4abe4e385f8..78559298932c 100644
@@ -6,6 +6,7 @@
 
 #undef TRACE_SYSTEM
 #define TRACE_SYSTEM kvm
+#define TRACE_INCLUDE_PATH ../gmap
 #undef TRACE_INCLUDE_FILE
 #define TRACE_INCLUDE_FILE trace-gmap
 
arch/s390/kvm/s390/Kconfig renamed from arch/s390/kvm/Kconfig +1 -20
similarity index 71%
copy from arch/s390/kvm/Kconfig
copy to arch/s390/kvm/s390/Kconfig
index fbf4162d6f2d..4b7a7c2945e5 100644
@@ -1,23 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 #
-# KVM configuration
-#
-source "virt/kvm/Kconfig"
-
-menuconfig VIRTUALIZATION
-	def_bool y
-	prompt "KVM"
-	help
-	  Say Y here to get to see options for using your Linux host to run other
-	  operating systems inside virtual machines (guests).
-	  This option alone does not add any kernel code.
-
-	  If you say N, all options in this submenu will be skipped and disabled.
-
-if VIRTUALIZATION
-
-config KVM
-	tristate
+# KVM_S390 configuration
 
 config KVM_S390
 	def_tristate y
@@ -56,5 +39,3 @@ config KVM_S390_UCONTROL
 	  controlled by userspace.
 
 	  If unsure, say N.
-
-endif # VIRTUALIZATION
arch/s390/kvm/s390/Makefile renamed from arch/s390/kvm/Makefile +5 -3
similarity index 64%
copy from arch/s390/kvm/Makefile
copy to arch/s390/kvm/s390/Makefile
index df10063ae042..51aee874b36f 100644
@@ -3,13 +3,15 @@
 #
 # Copyright IBM Corp. 2008
 
+KVM := ../../../../virt/kvm
 include $(srctree)/virt/kvm/Makefile.kvm
+include $(srctree)/arch/s390/kvm/gmap/Makefile
 
-ccflags-y := -Ivirt/kvm -Iarch/s390/kvm
+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 += dat.o gmap.o faultin.o
 
 kvm-$(CONFIG_VFIO_PCI_ZDEV_KVM) += pci.o
-obj-$(CONFIG_KVM) += kvm.o
+
+obj-$(CONFIG_KVM_S390) += kvm.o
arch/s390/kvm/s390/diag.c renamed from arch/s390/kvm/diag.c +0 -0
similarity index 100%
rename from arch/s390/kvm/diag.c
rename to arch/s390/kvm/s390/diag.c
arch/s390/kvm/s390/gaccess.c renamed from arch/s390/kvm/gaccess.c +0 -0
similarity index 100%
rename from arch/s390/kvm/gaccess.c
rename to arch/s390/kvm/s390/gaccess.c
arch/s390/kvm/s390/gaccess.h renamed from arch/s390/kvm/gaccess.h +0 -0
similarity index 100%
rename from arch/s390/kvm/gaccess.h
rename to arch/s390/kvm/s390/gaccess.h
arch/s390/kvm/s390/guestdbg.c renamed from arch/s390/kvm/guestdbg.c +0 -0
similarity index 100%
rename from arch/s390/kvm/guestdbg.c
rename to arch/s390/kvm/s390/guestdbg.c
arch/s390/kvm/s390/intercept.c renamed from arch/s390/kvm/intercept.c +0 -0
similarity index 100%
rename from arch/s390/kvm/intercept.c
rename to arch/s390/kvm/s390/intercept.c
arch/s390/kvm/s390/interrupt.c renamed from arch/s390/kvm/interrupt.c +0 -0
similarity index 100%
rename from arch/s390/kvm/interrupt.c
rename to arch/s390/kvm/s390/interrupt.c
arch/s390/kvm/s390/pci.c renamed from arch/s390/kvm/pci.c +0 -0
similarity index 100%
rename from arch/s390/kvm/pci.c
rename to arch/s390/kvm/s390/pci.c
arch/s390/kvm/s390/pci.h renamed from arch/s390/kvm/pci.h +0 -0
similarity index 100%
rename from arch/s390/kvm/pci.h
rename to arch/s390/kvm/s390/pci.h
arch/s390/kvm/s390/priv.c renamed from arch/s390/kvm/priv.c +0 -0
similarity index 100%
rename from arch/s390/kvm/priv.c
rename to arch/s390/kvm/s390/priv.c
arch/s390/kvm/s390/pv.c renamed from arch/s390/kvm/pv.c +0 -0
similarity index 100%
rename from arch/s390/kvm/pv.c
rename to arch/s390/kvm/s390/pv.c
arch/s390/kvm/s390/s390.c renamed from arch/s390/kvm/s390.c +0 -0
similarity index 100%
rename from arch/s390/kvm/s390.c
rename to arch/s390/kvm/s390/s390.c
arch/s390/kvm/s390/s390.h renamed from arch/s390/kvm/s390.h +0 -0
similarity index 100%
rename from arch/s390/kvm/s390.h
rename to arch/s390/kvm/s390/s390.h
arch/s390/kvm/s390/sigp.c renamed from arch/s390/kvm/sigp.c +0 -0
similarity index 100%
rename from arch/s390/kvm/sigp.c
rename to arch/s390/kvm/s390/sigp.c
arch/s390/kvm/s390/trace-s390.h renamed from arch/s390/kvm/trace-s390.h +0 -0
similarity index 100%
rename from arch/s390/kvm/trace-s390.h
rename to arch/s390/kvm/s390/trace-s390.h
arch/s390/kvm/s390/trace.h renamed from arch/s390/kvm/trace.h +0 -0
similarity index 100%
rename from arch/s390/kvm/trace.h
rename to arch/s390/kvm/s390/trace.h
arch/s390/kvm/s390/vsie.c renamed from arch/s390/kvm/vsie.c +0 -0
similarity index 100%
rename from arch/s390/kvm/vsie.c
rename to arch/s390/kvm/s390/vsie.c
Signature · 1 line
2.53.0
#38 lore raw

Re: [PATCH v4 14/27] KVM: s390: Move s390 kvm code into a subdirectory

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The files `pci.c` and `pci.h` were moved from `arch/s390/kvm/` to the `s390/` subdirectory…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The files pci.c and pci.h were moved from arch/s390/kvm/ to the s390/ subdirectory (arch/s390/kvm/s390/), but their specific entry in the MAINTAINERS file was not updated to reflect this change. --

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

KVM: s390: Move s390 kvm code into a subdirectory

Move all the code required to run s390 KVM guests on s390 to a s390 subdirectory. Move gmap related code into a gmap directory to later share gmap code between KVM implementations.

No functional change.

[ ... ]

arch/s390/kvm/s390/pci.c

Quoted 4 lines
arch/s390/kvm/s390/pci.c renamed from arch/s390/kvm/pci.c +0 -0
similarity index 100%
rename from arch/s390/kvm/pci.c
rename to arch/s390/kvm/s390/pci.c

[Severity: Low]
Since these files are moving to the new s390/ subdirectory, should the MAINTAINERS file also be updated?

The current maintainers entry tracks this path:
F: arch/s390/kvm/pci*

Because the path has changed, the get_maintainer script might fail to include the subsystem reviewers on future patches for these files. Is it worth updating the pattern to arch/s390/kvm/s390/pci* in this commit?

Quoted 4 lines
arch/s390/kvm/s390/pci.h renamed from arch/s390/kvm/pci.h +0 -0
similarity index 100%
rename from arch/s390/kvm/pci.h
rename to arch/s390/kvm/s390/pci.h
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
author reviewer
#39 lore raw

PATCH v4 15/27 KVM: S390: Prepare gmap for a second KVM implementation

In reply to Steffen Eiden

Refactor gmap code such that a second s390 (host) KVM implementation can use the gmap code as well. Move relevant definitions into the shared kvm_host.h. Move mmu code from s390 to gmap so the other KVM implementation can use it as well.

No functional change.

---
 arch/s390/include/asm/kvm_host.h      |   9 ++
 arch/s390/include/asm/kvm_host_s390.h |  11 +-
 arch/s390/kvm/gmap/Makefile           |   2 +-
 arch/s390/kvm/gmap/faultin.c          |   1 -
 arch/s390/kvm/gmap/gmap.c             |   5 -
 arch/s390/kvm/gmap/gmap.h             |  15 ++
 arch/s390/kvm/gmap/mmu.c              | 193 ++++++++++++++++++++++++++
 arch/s390/kvm/s390/s390.c             | 134 ++----------------
 arch/s390/kvm/s390/s390.h             |  16 +++
 9 files changed, 245 insertions(+), 141 deletions(-)
 create mode 100644 arch/s390/kvm/gmap/mmu.c
arch/s390/include/asm/kvm_host.h +9 -0
index e76ceee11ef5..33af8842a71c 100644
@@ -5,4 +5,13 @@
 
 #include <asm/kvm_host_s390.h>
 
+#define PGM_PROTECTION			0x04
+#define PGM_ADDRESSING			0x05
+#define PGM_SEGMENT_TRANSLATION		0x10
+#define PGM_PAGE_TRANSLATION		0x11
+#define PGM_ASCE_TYPE			0x38
+#define PGM_REGION_FIRST_TRANS		0x39
+#define PGM_REGION_SECOND_TRANS		0x3a
+#define PGM_REGION_THIRD_TRANS		0x3b
+
 #endif /* ASM_KVM_HOST_H */
arch/s390/include/asm/kvm_host_s390.h +3 -8
index 5293b0067422..7b7aa166cff7 100644
@@ -153,8 +153,7 @@ struct kvm_vcpu_stat {
 #define PGM_OPERATION			0x01
 #define PGM_PRIVILEGED_OP		0x02
 #define PGM_EXECUTE			0x03
-#define PGM_PROTECTION			0x04
-#define PGM_ADDRESSING			0x05
+/* 0x04 & 0x05 defined in kvm_host.h */
 #define PGM_SPECIFICATION		0x06
 #define PGM_DATA			0x07
 #define PGM_FIXED_POINT_OVERFLOW	0x08
@@ -165,8 +164,7 @@ struct kvm_vcpu_stat {
 #define PGM_HFP_EXPONENT_UNDERFLOW	0x0d
 #define PGM_HFP_SIGNIFICANCE		0x0e
 #define PGM_HFP_DIVIDE			0x0f
-#define PGM_SEGMENT_TRANSLATION		0x10
-#define PGM_PAGE_TRANSLATION		0x11
+/* 0x10 & 0x11 defined in kvm_host.h */
 #define PGM_TRANSLATION_SPEC		0x12
 #define PGM_SPECIAL_OPERATION		0x13
 #define PGM_OPERAND			0x15
@@ -196,10 +194,7 @@ struct kvm_vcpu_stat {
 #define PGM_STACK_SPECIFICATION		0x32
 #define PGM_STACK_TYPE			0x33
 #define PGM_STACK_OPERATION		0x34
-#define PGM_ASCE_TYPE			0x38
-#define PGM_REGION_FIRST_TRANS		0x39
-#define PGM_REGION_SECOND_TRANS		0x3a
-#define PGM_REGION_THIRD_TRANS		0x3b
+/* 0x38 - 0x3b defined in kvm_host.h */
 #define PGM_SECURE_STORAGE_ACCESS	0x3d
 #define PGM_NON_SECURE_STORAGE_ACCESS	0x3e
 #define PGM_SECURE_STORAGE_VIOLATION	0x3f
arch/s390/kvm/gmap/Makefile +1 -1
index 21967ed88877..140914c5c14f 100644
@@ -2,4 +2,4 @@
 
 GMAP ?= ../gmap
 
-kvm-y += $(GMAP)/dat.o $(GMAP)/gmap.o $(GMAP)/faultin.o
+kvm-y += $(GMAP)/dat.o $(GMAP)/gmap.o $(GMAP)/faultin.o $(GMAP)/mmu.o
arch/s390/kvm/gmap/faultin.c +0 -1
index 740415d494de..713914dc8eb2 100644
@@ -11,7 +11,6 @@
 #include "gmap.h"
 #include "faultin.h"
 
-bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu);
 #define CREATE_TRACE_POINTS
 #include "trace-gmap.h"
 
arch/s390/kvm/gmap/gmap.c +0 -5
index 8773aa34f107..a04e0a4355a0 100644
@@ -24,11 +24,6 @@
 #include "s390.h"
 #include "faultin.h"
 
-static inline bool kvm_s390_is_in_sie(struct kvm_vcpu *vcpu)
-{
-	return vcpu->arch.sie_block->prog0c & PROG_IN_SIE;
-}
-
 static int gmap_limit_to_type(gfn_t limit)
 {
 	if (!limit)
arch/s390/kvm/gmap/gmap.h +15 -0
index c54c35e47d6d..4ab7208f50ff 100644
@@ -10,6 +10,8 @@
 #ifndef ARCH_KVM_GMAP_GMAP_H
 #define ARCH_KVM_GMAP_GMAP_H
 
+#include <linux/kvm_host.h>
+
 #include "dat.h"
 
 /**
@@ -330,4 +332,17 @@ static inline bool gmap_is_shadow_valid(struct gmap *sg, union asce asce, int ed
 	return sg->guest_asce.val == asce.val && sg->edat_level == edat_level;
 }
 
+int gmap_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log);
+int gmap_prepare_memory_region(struct kvm *kvm,
+			       const struct kvm_memory_slot *old,
+			       struct kvm_memory_slot *new,
+			       enum kvm_mr_change change);
+void gmap_commit_memory_region(struct kvm *kvm,
+			       struct kvm_memory_slot *old,
+			       const struct kvm_memory_slot *new,
+			       enum kvm_mr_change change);
+bool gmap_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range);
+long gmap_pre_fault_memory(struct kvm_vcpu *vcpu,
+			   struct kvm_pre_fault_memory *range);
+
 #endif /* ARCH_KVM_GMAP_GMAP_H */
arch/s390/kvm/gmap/mmu.c new file +193 -0

193 lines not on this page · raw message

arch/s390/kvm/s390/s390.c +8 -126

171 lines not on this page · raw message

arch/s390/kvm/s390/s390.h +16 -0
index e144ca2e6d5e..aa2061007230 100644
@@ -32,6 +32,11 @@ union kvm_s390_quad {
 	unsigned char one;
 };
 
+static inline bool kvm_s390_is_in_sie(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sie_block->prog0c & PROG_IN_SIE;
+}
+
 static inline void kvm_s390_fpu_store(struct kvm_run *run)
 {
 	fpu_stfpc(&run->s.regs.fpc);
@@ -594,6 +599,11 @@ static inline bool kvm_s390_cur_gmap_fault_is_write(void)
 	return test_facility(75) && (current->thread.gmap_teid.fsi == TEID_FSI_STORE);
 }
 
+static __always_inline int kvm_s390_is_migration_mode(struct kvm *kvm)
+{
+	return kvm->arch.migration_mode;
+}
+
 /**
  * kvm_s390_vcpu_crypto_reset_all
  *
@@ -624,4 +634,10 @@ void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm);
  */
 extern unsigned int diag9c_forwarding_hz;
 
+/*
+ * Must be called with kvm->slots_lock to avoid races with ourselves and
+ * kvm_s390_vm_start_migration.
+ */
+int kvm_s390_vm_stop_migration(struct kvm *kvm);
+
 #endif
Signature · 1 line
2.53.0
#40 lore raw

Re: [PATCH v4 15/27] KVM: S390: Prepare gmap for a second KVM implementation

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

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

Pre-existing issues:
- [Critical] Fallible memory allocation during the infallible memslot commit phase causes silent Use-After-Free and guest crashes.
- [High] Non-atomic clearing of the memory slot dirty bitmap via memset can race with concurrent atomic set_bit operations, leading to lost dirty bits. --

commit 2aa486852c957946a99960f65fceb0012466db06
Author: Steffen Eiden <[email protected]>

KVM: S390: Prepare gmap for a second KVM implementation

Refactor gmap code such that a second s390 (host) KVM implementation can use the gmap code as well. Move relevant definitions into the shared kvm_host.h. Move mmu code from s390 to gmap so the other KVM implementation can use it as well.

Quoted 5 lines
arch/s390/kvm/gmap/mmu.c new file +0 -0
new file mode 100644
index 0000000000000..5bb6609b974b2

[ ... ]

Quoted 2 lines
arch/s390/kvm/gmap/mmu.c new file +2 -0
+int gmap_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
+{

[ ... ]

arch/s390/kvm/gmap/mmu.c line 42

Quoted 5 lines
arch/s390/kvm/gmap/mmu.c new file +5 -0
+	/* Clear the dirty log */
+	if (is_dirty) {
+		n = kvm_dirty_bitmap_bytes(memslot);
+		memset(memslot->dirty_bitmap, 0, n);
+	}

[Severity: High]
This isn't a bug introduced by this patch, but since the code is being moved here, I noticed this clearing logic. Can this non-atomic memset() race with concurrent atomic bit setting operations?

If KVM concurrently calls mark_page_dirty() (for example, from kvm_s390_sync_request() for virtio indicators), which sets bits in this same bitmap using set_bit_le(), it seems we could lose dirty bits in the gap between the copy and this clear, or have the atomic set overwritten by the memset().

Could this lead to lost dirty bits and memory corruption during live migration?

[ ... ]

Quoted 6 lines
arch/s390/kvm/gmap/mmu.c new file +6 -0
+void gmap_commit_memory_region(struct kvm *kvm,
+			       struct kvm_memory_slot *old,
+			       const struct kvm_memory_slot *new,
+			       enum kvm_mr_change change)
+{
+	struct kvm_s390_mmu_cache *mc = NULL;

arch/s390/kvm/gmap/mmu.c line 110

Quoted 10 lines
arch/s390/kvm/gmap/mmu.c new file +10 -0
+	int rc = 0;
+
+	if (change == KVM_MR_FLAGS_ONLY)
+		return;
+
+	mc = kvm_s390_new_mmu_cache();
+	if (!mc) {
+		rc = -ENOMEM;
+		goto out;
+	}

[Severity: Critical]
This is a pre-existing issue, but is it safe for this memory allocation to fail during the memslot commit phase?

If kvm_s390_new_mmu_cache() fails under memory pressure, the error is only printed at the goto out label and the required gmap table updates are skipped:

[ ... ]

arch/s390/kvm/gmap/mmu.c line 135

Quoted 5 lines
arch/s390/kvm/gmap/mmu.c new file +5 -0
+out:
+	if (rc)
+		pr_warn("failed to commit memory region\n");
+	kvm_s390_free_mmu_cache(mc);
+}

For KVM_MR_DELETE and KVM_MR_MOVE, skipping dat_delete_slot() could leave the guest with a stale, usable hardware mapping to host memory.

Does this lead to a guest-to-host use-after-free if userspace issues KVM_SET_USER_MEMORY_REGION under high memory pressure?

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

Re: [PATCH v4 15/27] KVM: S390: Prepare gmap for a second KVM implementation

In reply to Steffen Eiden
Quoted 27 lines · Steffen Eiden

Refactor gmap code such that a second s390 (host) KVM implementation can use the gmap code as well. Move relevant definitions into the shared kvm_host.h. Move mmu code from s390 to gmap so the other KVM implementation can use it as well.

No functional change.

---
  arch/s390/include/asm/kvm_host.h      |   9 ++
  arch/s390/include/asm/kvm_host_s390.h |  11 +-
  arch/s390/kvm/gmap/Makefile           |   2 +-
  arch/s390/kvm/gmap/faultin.c          |   1 -
  arch/s390/kvm/gmap/gmap.c             |   5 -
  arch/s390/kvm/gmap/gmap.h             |  15 ++
  arch/s390/kvm/gmap/mmu.c              | 193 ++++++++++++++++++++++++++
  arch/s390/kvm/s390/s390.c             | 134 ++----------------
  arch/s390/kvm/s390/s390.h             |  16 +++
  9 files changed, 245 insertions(+), 141 deletions(-)

create mode 100644 arch/s390/kvm/gmap/mmu.c

arch/s390/include/asm/kvm_host.h +0 -0
index e76ceee11ef5..33af8842a71c 100644
@@ -5,4 +5,13 @@
 

arch/s390/include/asm/kvm_host.h line 15

Quoted 10 lines
arch/s390/include/asm/kvm_host.h +8 -0
  #include <asm/kvm_host_s390.h>
 
+#define PGM_PROTECTION			0x04
+#define PGM_ADDRESSING			0x05
+#define PGM_SEGMENT_TRANSLATION		0x10
+#define PGM_PAGE_TRANSLATION		0x11
+#define PGM_ASCE_TYPE			0x38
+#define PGM_REGION_FIRST_TRANS		0x39
+#define PGM_REGION_SECOND_TRANS		0x3a
+#define PGM_REGION_THIRD_TRANS		0x3b

Before we go out and do funny stuff like this I'd much rather have it all in one file even though the arm code won't use most of the PGMs.

Either we keep all of it in kvm_host.h or we move it out entirely into ptrace.h if the s390 kernel team doesn't complain.

I don't think this change belongs in this patch anyway.

[...]

Quoted 9 lines
arch/s390/include/asm/kvm_host.h +0 -1
-bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu);
  #define CREATE_TRACE_POINTS
  #include "trace-gmap.h"
arch/s390/kvm/gmap/gmap.c +0 -0
index 8773aa34f107..a04e0a4355a0 100644

arch/s390/kvm/gmap/gmap.c line 31

Quoted 8 lines
arch/s390/kvm/gmap/gmap.c +0 -5
@@ -24,11 +24,6 @@
  #include "s390.h"
  #include "faultin.h"
 
-static inline bool kvm_s390_is_in_sie(struct kvm_vcpu *vcpu)
-{
-	return vcpu->arch.sie_block->prog0c & PROG_IN_SIE;
-}
-

With a small modification we can re-use this in vsie.c and throw it into kvm_host.h before we move it into kvm_host_s390.h. Make it work on sblks instead of vcpus and name it something like kvm_s390_sblk_is_in_sie().

#42 lore raw

Re: [PATCH v4 15/27] KVM: S390: Prepare gmap for a second KVM implementation

In reply to Steffen Eiden

Am 06.07.26 um 10:52 schrieb Steffen Eiden:
[...]

Quoted 21 lines
arch/s390/include/asm/kvm_host.h +9 -0
@@ -5,4 +5,13 @@
 
  #include <asm/kvm_host_s390.h>
 
+#define PGM_PROTECTION			0x04
+#define PGM_ADDRESSING			0x05
+#define PGM_SEGMENT_TRANSLATION		0x10
+#define PGM_PAGE_TRANSLATION		0x11
+#define PGM_ASCE_TYPE			0x38
+#define PGM_REGION_FIRST_TRANS		0x39
+#define PGM_REGION_SECOND_TRANS		0x3a
+#define PGM_REGION_THIRD_TRANS		0x3b
+
  #endif /* ASM_KVM_HOST_H */
arch/s390/include/asm/kvm_host_s390.h +0 -0
index 5293b0067422..7b7aa166cff7 100644

arch/s390/include/asm/kvm_host_s390.h struct kvm_vcpu_stat { · line 156

Quoted 6 lines
arch/s390/include/asm/kvm_host_s390.h +1 -2
@@ -153,8 +153,7 @@ struct kvm_vcpu_stat {
  #define PGM_OPERATION			0x01
  #define PGM_PRIVILEGED_OP		0x02
  #define PGM_EXECUTE			0x03
-#define PGM_PROTECTION			0x04
-#define PGM_ADDRESSING			0x05
+/* 0x04 & 0x05 defined in kvm_host.h */

I think I would move ALL PGM defines into kvm_host.h. Those are certainly valid from a host point of view, even for the arm64 kvm module. Yes, its unused but it will be much cleaner than having everything in one place and doing these cross-reference comments.

[...]

author reviewer
#43 lore raw

Re: [PATCH v4 15/27] KVM: S390: Prepare gmap for a second KVM implementation

Quoted 35 lines · Christian Borntraeger

Am 06.07.26 um 10:52 schrieb Steffen Eiden:
[...]

Quoted 19 lines
arch/s390/include/asm/kvm_host.h +9 -0
@@ -5,4 +5,13 @@
  #include <asm/kvm_host_s390.h>
+#define PGM_PROTECTION			0x04
+#define PGM_ADDRESSING			0x05
+#define PGM_SEGMENT_TRANSLATION		0x10
+#define PGM_PAGE_TRANSLATION		0x11
+#define PGM_ASCE_TYPE			0x38
+#define PGM_REGION_FIRST_TRANS		0x39
+#define PGM_REGION_SECOND_TRANS		0x3a
+#define PGM_REGION_THIRD_TRANS		0x3b
+
  #endif /* ASM_KVM_HOST_H */
arch/s390/include/asm/kvm_host_s390.h +0 -0
index 5293b0067422..7b7aa166cff7 100644

arch/s390/include/asm/kvm_host_s390.h struct kvm_vcpu_stat { · line 156

Quoted 6 lines
arch/s390/include/asm/kvm_host_s390.h +1 -2
@@ -153,8 +153,7 @@ struct kvm_vcpu_stat {
  #define PGM_OPERATION			0x01
  #define PGM_PRIVILEGED_OP		0x02
  #define PGM_EXECUTE			0x03
-#define PGM_PROTECTION			0x04
-#define PGM_ADDRESSING			0x05
+/* 0x04 & 0x05 defined in kvm_host.h */

I think I would move ALL PGM defines into kvm_host.h. Those are certainly valid from a host point of view, even for the arm64 kvm module. Yes, its unused but it will be much cleaner than having everything in one place and doing these cross-reference comments.

Quoted 1 line

[...]

Malkes sense. I'll move all.

Steffen

author reviewer
#44 lore raw

PATCH v4 16/27 KVM: s390: gmap: Refactor storage key and CMMA code into separate files

In reply to Steffen Eiden

Extract storage key and CMMA functionality from the core gmap code into dedicated compilation units This improves code organization and enables reuse of the gmap core by other KVM implementations that don't require s390-specific features.

The gmap core (dat.c, gmap.c, faultin.c, mmu.c) now compiles independently without storage key or CMMA dependencies, making it suitable for reuse by other KVM implementations. By default sk and cmma support is compiled into gmap. If a KVM implementation wishes to not use those features it has to define KVM_MANAGES_S390_GUEST = n prior to including the gmap Makefile.

No functional changes.

---
 arch/s390/kvm/gmap/Makefile  |   5 +
 arch/s390/kvm/gmap/cmma.c    | 313 ++++++++++++++++++++++
 arch/s390/kvm/gmap/cmma.h    |  36 +++
 arch/s390/kvm/gmap/dat.c     | 504 +----------------------------------
 arch/s390/kvm/gmap/dat.h     |  14 -
 arch/s390/kvm/gmap/gmap.c    |  80 +-----
 arch/s390/kvm/gmap/gmap.h    |  12 -
 arch/s390/kvm/gmap/sk.c      | 264 ++++++++++++++++++
 arch/s390/kvm/gmap/sk.h      |  18 ++
 arch/s390/kvm/s390/gaccess.c |   1 +
 arch/s390/kvm/s390/priv.c    |   2 +
 arch/s390/kvm/s390/s390.c    |   2 +
 12 files changed, 647 insertions(+), 604 deletions(-)
 create mode 100644 arch/s390/kvm/gmap/cmma.c
 create mode 100644 arch/s390/kvm/gmap/cmma.h
 create mode 100644 arch/s390/kvm/gmap/sk.c
 create mode 100644 arch/s390/kvm/gmap/sk.h
arch/s390/kvm/gmap/Makefile +5 -0
index 140914c5c14f..a49c7e36b71c 100644
@@ -2,4 +2,9 @@
 
 GMAP ?= ../gmap
 
+# Enable s390-specific guest management features (storage keys and CMMA)
+KVM_MANAGES_S390_GUEST ?= y
+
 kvm-y += $(GMAP)/dat.o $(GMAP)/gmap.o $(GMAP)/faultin.o $(GMAP)/mmu.o
+
+kvm-$(KVM_MANAGES_S390_GUEST) += $(GMAP)/sk.o $(GMAP)/cmma.o
arch/s390/kvm/gmap/cmma.c new file +313 -0

313 lines not on this page · raw message

arch/s390/kvm/gmap/cmma.h new file +36 -0
new file mode 100644
index 000000000000..12b574ecf711
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef ARCH_KVM_GMAP_CMMA_H
+#define ARCH_KVM_GMAP_CMMA_H
+
+#include "dat.h"
+
+#define ESSA_GET_STATE              0
+#define ESSA_SET_STABLE             1
+#define ESSA_SET_UNUSED             2
+#define ESSA_SET_VOLATILE           3
+#define ESSA_SET_POT_VOLATILE       4
+#define ESSA_SET_STABLE_RESIDENT    5
+#define ESSA_SET_STABLE_IF_RESIDENT 6
+#define ESSA_SET_STABLE_NODAT       7
+
+int dat_perform_essa(union asce asce, gfn_t gfn, int orc, union essa_state *state, bool *dirty);
+long dat_reset_cmma(union asce asce, gfn_t start_gfn);
+int dat_peek_cmma(gfn_t start, union asce asce, unsigned int *count, u8 *values);
+int dat_get_cmma(union asce asce, gfn_t *start, unsigned int *count, u8 *values, atomic64_t *rem);
+int dat_set_cmma_bits(struct kvm_s390_mmu_cache *mc, union asce asce, gfn_t gfn,
+		      unsigned long count, unsigned long mask, const uint8_t *bits);
+
+void _gmap_set_cmma_all(struct gmap *gmap, bool dirty);
+
+static inline void gmap_set_cmma_all_dirty(struct gmap *gmap)
+{
+	_gmap_set_cmma_all(gmap, true);
+}
+
+static inline void gmap_set_cmma_all_clean(struct gmap *gmap)
+{
+	_gmap_set_cmma_all(gmap, false);
+}
+
+#endif /* ARCH_KVM_GMAP_CMMA_H */
arch/s390/kvm/gmap/dat.c +3 -501

519 lines not on this page · raw message

arch/s390/kvm/gmap/dat.h +0 -14
index 711ae2f96107..12153a74815f 100644
@@ -532,13 +532,6 @@ int dat_entry_walk(struct kvm_s390_mmu_cache *mc, gfn_t gfn, union asce asce, in
 void dat_free_level(struct crst_table *table, bool owns_ptes);
 struct crst_table *dat_alloc_crst_sleepable(unsigned long init);
 int dat_set_asce_limit(struct kvm_s390_mmu_cache *mc, union asce *asce, int newtype);
-int dat_get_storage_key(union asce asce, gfn_t gfn, union skey *skey);
-int dat_set_storage_key(struct kvm_s390_mmu_cache *mc, union asce asce, gfn_t gfn,
-			union skey skey, bool nq);
-int dat_cond_set_storage_key(struct kvm_s390_mmu_cache *mmc, union asce asce, gfn_t gfn,
-			     union skey skey, union skey *oldkey, bool nq, bool mr, bool mc);
-int dat_reset_reference_bit(union asce asce, gfn_t gfn);
-long dat_reset_skeys(union asce asce, gfn_t start);
 
 unsigned long dat_get_ptval(struct page_table *table, struct ptval_param param);
 void dat_set_ptval(struct page_table *table, struct ptval_param param, unsigned long val);
@@ -548,13 +541,6 @@ int dat_set_slot(struct kvm_s390_mmu_cache *mc, union asce asce, gfn_t start, gf
 int dat_set_prefix_notif_bit(union asce asce, gfn_t gfn);
 bool dat_test_age_gfn(union asce asce, gfn_t start, gfn_t end);
 
-int dat_perform_essa(union asce asce, gfn_t gfn, int orc, union essa_state *state, bool *dirty);
-long dat_reset_cmma(union asce asce, gfn_t start_gfn);
-int dat_peek_cmma(gfn_t start, union asce asce, unsigned int *count, u8 *values);
-int dat_get_cmma(union asce asce, gfn_t *start, unsigned int *count, u8 *values, atomic64_t *rem);
-int dat_set_cmma_bits(struct kvm_s390_mmu_cache *mc, union asce asce, gfn_t gfn,
-		      unsigned long count, unsigned long mask, const uint8_t *bits);
-
 int kvm_s390_mmu_cache_topup(struct kvm_s390_mmu_cache *mc);
 
 #define GFP_KVM_S390_MMU_CACHE (GFP_ATOMIC | __GFP_ACCOUNT | __GFP_NOWARN)
arch/s390/kvm/gmap/gmap.c +3 -77

98 lines not on this page · raw message

arch/s390/kvm/gmap/gmap.h +0 -12
index 4ab7208f50ff..aa65fe724fdb 100644
@@ -100,13 +100,11 @@ int gmap_set_limit(struct gmap *gmap, gfn_t limit);
 int gmap_ucas_translate(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, gpa_t *gaddr);
 int gmap_ucas_map(struct gmap *gmap, gfn_t p_gfn, gfn_t c_gfn, unsigned long count);
 void gmap_ucas_unmap(struct gmap *gmap, gfn_t c_gfn, unsigned long count);
-int gmap_enable_skeys(struct gmap *gmap);
 int gmap_pv_destroy_range(struct gmap *gmap, gfn_t start, gfn_t end, bool interruptible);
 int gmap_insert_rmap(struct kvm_s390_mmu_cache *mc, struct gmap *sg, gfn_t p_gfn,
 		     gfn_t r_gfn, int level);
 int gmap_protect_rmap(struct kvm_s390_mmu_cache *mc, struct gmap *sg, gfn_t p_gfn, gfn_t r_gfn,
 		      kvm_pfn_t pfn, int level, bool wr);
-void _gmap_set_cmma_all(struct gmap *gmap, bool dirty);
 void _gmap_handle_vsie_unshadow_event(struct gmap *parent, gfn_t gfn);
 struct gmap *gmap_create_shadow(struct kvm_s390_mmu_cache *mc, struct gmap *gmap,
 				union asce asce, int edat_level);
@@ -200,16 +198,6 @@ static inline bool pte_needs_unshadow(union pte oldpte, union pte newpte, union
 	return !newpte.h.p || !newpte.s.pr;
 }
 
-static inline void gmap_set_cmma_all_dirty(struct gmap *gmap)
-{
-	_gmap_set_cmma_all(gmap, true);
-}
-
-static inline void gmap_set_cmma_all_clean(struct gmap *gmap)
-{
-	_gmap_set_cmma_all(gmap, false);
-}
-
 static inline union pgste _gmap_ptep_xchg(struct gmap *gmap, union pte *ptep, union pte newpte,
 					  union pgste pgste, gfn_t gfn, bool needs_lock)
 {
arch/s390/kvm/gmap/sk.c new file +264 -0

264 lines not on this page · raw message

arch/s390/kvm/gmap/sk.h new file +18 -0
new file mode 100644
index 000000000000..113ff8d0b0ab
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef ARCH_KVM_GMAP_SK_H
+#define ARCH_KVM_GMAP_SK_H
+
+#include "dat.h"
+
+int dat_get_storage_key(union asce asce, gfn_t gfn, union skey *skey);
+int dat_set_storage_key(struct kvm_s390_mmu_cache *mc, union asce asce, gfn_t gfn,
+			union skey skey, bool nq);
+int dat_cond_set_storage_key(struct kvm_s390_mmu_cache *mmc, union asce asce, gfn_t gfn,
+			     union skey skey, union skey *oldkey, bool nq, bool mr, bool mc);
+int dat_reset_reference_bit(union asce asce, gfn_t gfn);
+long dat_reset_skeys(union asce asce, gfn_t start);
+
+int gmap_enable_skeys(struct gmap *gmap);
+
+#endif /* ARCH_KVM_GMAP_SK_H */
arch/s390/kvm/s390/gaccess.c +1 -0
index 2a4b2525329a..2d82c9317597 100644
@@ -20,6 +20,7 @@
 #include "s390.h"
 #include "dat.h"
 #include "gmap.h"
+#include "sk.h"
 #include "gaccess.h"
 #include "faultin.h"
 
arch/s390/kvm/s390/priv.c +2 -0
index 21479f0a5e5a..9bd3c07285ba 100644
@@ -29,6 +29,8 @@
 #include "s390.h"
 #include "trace.h"
 #include "gmap.h"
+#include "sk.h"
+#include "cmma.h"
 
 static int handle_ri(struct kvm_vcpu *vcpu)
 {
arch/s390/kvm/s390/s390.c +2 -0
index f89bad4e9e04..30c273f5377e 100644
@@ -55,6 +55,8 @@
 #include "gmap.h"
 #include "faultin.h"
 #include "pci.h"
+#include "sk.h"
+#include "cmma.h"
 
 #define CREATE_TRACE_POINTS
 #include "trace.h"
Signature · 1 line
2.53.0
author reviewer
#45 lore raw

PATCH v4 17/27 KVM: s390: Refactor prefix handling into a separate file

In reply to Steffen Eiden

Split prefix page tracking into prefix.c/h with conditional compilation via prefix-stubs.c for KVM for non-s390 guests. Enables gmap reuse by other KVM implementations that don't use prefix pages. The prefix tracking is conditional on the Makefile variable KVM_MANAGES_S390_GUEST.

No functional changes.

---
 arch/s390/kvm/gmap/Makefile       |  4 +-
 arch/s390/kvm/gmap/dat.c          | 48 -------------------
 arch/s390/kvm/gmap/gmap.c         | 23 ---------
 arch/s390/kvm/gmap/gmap.h         | 11 +----
 arch/s390/kvm/gmap/prefix-stubs.c | 21 ++++++++
 arch/s390/kvm/gmap/prefix.c       | 80 +++++++++++++++++++++++++++++++
 arch/s390/kvm/gmap/prefix.h       | 68 ++++++++++++++++++++++++++
 7 files changed, 173 insertions(+), 82 deletions(-)
 create mode 100644 arch/s390/kvm/gmap/prefix-stubs.c
 create mode 100644 arch/s390/kvm/gmap/prefix.c
 create mode 100644 arch/s390/kvm/gmap/prefix.h
arch/s390/kvm/gmap/Makefile +3 -1
index a49c7e36b71c..4ca7635d1d0e 100644
@@ -4,7 +4,9 @@ GMAP ?= ../gmap
 
 # Enable s390-specific guest management features (storage keys and CMMA)
 KVM_MANAGES_S390_GUEST ?= y
+USE_PREFIX_STUB := $(if $(filter y,$(KVM_MANAGES_S390_GUEST)),n,y)
 
 kvm-y += $(GMAP)/dat.o $(GMAP)/gmap.o $(GMAP)/faultin.o $(GMAP)/mmu.o
 
-kvm-$(KVM_MANAGES_S390_GUEST) += $(GMAP)/sk.o $(GMAP)/cmma.o
+kvm-$(KVM_MANAGES_S390_GUEST) += $(GMAP)/sk.o $(GMAP)/cmma.o $(GMAP)/prefix.o
+kvm-$(USE_PREFIX_STUB) += $(GMAP)/prefix-stubs.o
arch/s390/kvm/gmap/dat.c +0 -48

51 lines not on this page · raw message

arch/s390/kvm/gmap/gmap.c +0 -23
index 4ade7794e990..3dcd1d4c6a29 100644
@@ -254,29 +254,6 @@ int s390_replace_asce(struct gmap *gmap)
 	return 0;
 }
 
-bool _gmap_unmap_prefix(struct gmap *gmap, gfn_t gfn, gfn_t end, bool hint)
-{
-	struct kvm *kvm = gmap->kvm;
-	struct kvm_vcpu *vcpu;
-	gfn_t prefix_gfn;
-	unsigned long i;
-
-	if (is_shadow(gmap))
-		return false;
-	kvm_for_each_vcpu(i, vcpu, kvm) {
-		/* Match against both prefix pages */
-		prefix_gfn = gpa_to_gfn(kvm_s390_get_prefix(vcpu));
-		if (prefix_gfn < end && gfn <= prefix_gfn + 1) {
-			if (hint && kvm_s390_is_in_sie(vcpu))
-				return false;
-			VCPU_EVENT(vcpu, 2, "gmap notifier for %llx-%llx",
-				   gfn_to_gpa(gfn), gfn_to_gpa(end));
-			kvm_s390_sync_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu);
-		}
-	}
-	return true;
-}
-
 struct clear_young_pte_priv {
 	struct gmap *gmap;
 	bool young;
arch/s390/kvm/gmap/gmap.h +1 -10
index aa65fe724fdb..0500c8c6c9ca 100644
@@ -13,6 +13,7 @@
 #include <linux/kvm_host.h>
 
 #include "dat.h"
+#include "prefix.h"
 
 /**
  * enum gmap_flags - Flags of a gmap.
@@ -158,16 +159,6 @@ static inline void gmap_handle_vsie_unshadow_event(struct gmap *parent, gfn_t gf
 		_gmap_handle_vsie_unshadow_event(parent, gfn);
 }
 
-static inline bool gmap_mkold_prefix(struct gmap *gmap, gfn_t gfn, gfn_t end)
-{
-	return _gmap_unmap_prefix(gmap, gfn, end, true);
-}
-
-static inline bool gmap_unmap_prefix(struct gmap *gmap, gfn_t gfn, gfn_t end)
-{
-	return _gmap_unmap_prefix(gmap, gfn, end, false);
-}
-
 /**
  * pte_needs_unshadow() -- Check if the pte operations triggers unshadowing.
  * @oldpte: the previous value for the guest pte.
arch/s390/kvm/gmap/prefix-stubs.c new file +21 -0
new file mode 100644
index 000000000000..d1d6286f454a
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * These stubs are used when building gmap for non-s390 guests
+ * that don't need prefix page tracking.
+ */
+
+#include <linux/kvm_host.h>
+#include <linux/kvm_types.h>
+
+#include "dat.h"
+#include "prefix.h"
+
+bool _gmap_unmap_prefix(struct gmap *gmap, gfn_t gfn, gfn_t end, bool hint)
+{
+	return true;
+}
+
+int dat_set_prefix_notif_bit(union asce asce, gfn_t gfn)
+{
+	return 0;
+}
arch/s390/kvm/gmap/prefix.c new file +80 -0

80 lines not on this page · raw message

arch/s390/kvm/gmap/prefix.h new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#46 lore raw

PATCH v4 18/27 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.

---
 arch/s390/kvm/s390/s390.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/s390/kvm/s390/s390.c +8 -0
index 30c273f5377e..b560c1e57926 100644
@@ -5808,6 +5808,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
#47 lore raw

PATCH v4 19/27 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 | 129 +++++++++++++++++++
 arch/s390/include/asm/kvm_host_types.h       |   4 +
 arch/s390/include/asm/sae.h                  |  56 ++++++++
 arch/s390/include/asm/stacktrace.h           |   5 +
 arch/s390/kernel/asm-offsets.c               |   1 +
 arch/s390/kernel/entry.S                     |  25 ++++
 arch/s390/tools/opcodes.txt                  |   3 +
 8 files changed, 224 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 +129 -0

129 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 +56 -0

56 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 +25 -0
index 9572620fcaa6..248bea26921b 100644
@@ -244,6 +244,31 @@ EXPORT_SYMBOL(__sie64a)
 EXPORT_SYMBOL(sie_exit)
 #endif
 
+#if IS_ENABLED(CONFIG_KVM_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
+# Let the next instruction be NOP to avoid triggering a machine check
+# and handling it in a guest as result of the instruction execution.
+	nopr	7
+	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	%r3,%r3
+	xgr	%r2,%r2
+	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
maintainer
#48 lore raw

Re: [PATCH v4 19/27] s390: Introduce Start Arm Execution instruction

Quoted 23 lines · 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.

[...]

Quoted 7 lines
arch/s390/include/asm/kvm_host_arm64_types.h new file +7 -0
+	u64	gpr[31];		/* 0x0300  */
+	u64	_03f8;			/* 0x03f8  */
+
+	union {
+		u64	icptd[8];		/* 0x0400 */
+		/* validity-interception reason; icptr 0x01 */
+#define SAE_VIR_UNKNOWN		0x00

arch/s390/include/asm/kvm_host_arm64_types.h line 87

Quoted 10 lines
arch/s390/include/asm/kvm_host_arm64_types.h new file +10 -0
+#define SAE_VIR_UNSUPP_FORMAT	0x01
+#define SAE_VIR_MSO_BOUNDS	0x02
+#define SAE_VIR_MSLA		0x03
+#define SAE_VIR_MGPAS		0x04
+#define SAE_VIR_INVAL_SYSREG	0x05
+#define SAE_VIR_HOST_CONTROL	0x06
+#define SAE_VIR_SCA		0x07
+#define SAE_VIR_MSO_ALIGN	0x08
+#define SAE_VIR_HLC		0x09
+#define SAE_VIR_IRPTC		0x0a

Will these ever be used for something?

Quoted 29 lines
arch/s390/include/asm/kvm_host_arm64_types.h new file +29 -0
+		u16 vir;			/* 0x0400 */
+		/* host access interception details; icptr 0x02 */
+		struct {
+			u64		esr_elz;	/* 0x0400 */
+			u8		_0408[6];	/* 0x0408 */
+			u16		pic;		/* 0x040e */
+			union teid	teid;		/* 0x0410 */
+			gva_t		far_elz;	/* 0x0418 */
+			gva_t		vaddr;		/* 0x0420 */
+			u64		suppl;		/* 0x0428 */
+			u8		gltl;		/* 0x0430 */
+			u8		_0431[7];	/* 0x0431 */
+			u64		_0438;		/* 0x0438 */
+		} hai;
+		/* exception-interception details; icptr 0x03 */
+		struct {
+			u64	esr_elz;		/* 0x0400 */
+			u64	_0408[2];		/* 0x0408 */
+			gva_t	far_elz;		/* 0x0418 */
+		} trap;
+		/* timer-interception reason; icptr 0x04 */
+#define SAE_IR_TIMER_ID_VIRT		BIT(6)
+#define SAE_IR_TIMER_ID_PHYS		BIT(7)
+		u8	tir;			/* 0x0400 */
+	};
+	u64	_0440[376];			/* 0x0440 */
+} __packed __aligned(PAGE_SIZE);
+static_assert(sizeof(struct kvm_sae_block) == PAGE_SIZE);
+

[...]

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

Quoted 11 lines
arch/s390/kernel/entry.S +11 -0
+#if IS_ENABLED(CONFIG_KVM_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
+# Let the next instruction be NOP to avoid triggering a machine check
+# and handling it in a guest as result of the instruction execution.

@Christian:
I have the feeling things got lost in translation when this comment was written.
How would we trigger a MCHECK by having an instruction after SIE/SAE?
AFAIU this allows us to have a label which we can check in the mcheck handler to decide who triggered the mcheck.

Without the label the comment and NOP are useless as far as I can see.
So we need to decide if this still applies and then either remove it or add sae handling to mcck_int_handler and maybe other mcheck functions.

Quoted 31 lines
arch/s390/kernel/entry.S +14 -0
+	nopr	7
+	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	%r3,%r3
+	xgr	%r2,%r2
+	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
#49 lore raw

Re: [PATCH v4 19/27] s390: Introduce Start Arm Execution instruction

Am 06.07.26 um 13:17 schrieb Janosch Frank:

Quoted 11 lines

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

Quoted 6 lines
arch/s390/kernel/entry.S +6 -0
+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
+# Let the next instruction be NOP to avoid triggering a machine check
+# and handling it in a guest as result of the instruction execution.

@Christian:
I have the feeling things got lost in translation when this comment was written.
How would we trigger a MCHECK by having an instruction after SIE/SAE?
AFAIU this allows us to have a label which we can check in the mcheck handler to decide who triggered the mcheck.

the original rationale was that some machine checks are nullifying some are terminating. So the PSW might point to SAE or the instruction afterwards for a guest machine check.
In SIE, we use that nop to account any machine check on that nop as a guest machine check.

Quoted 3 lines

Without the label the comment and NOP are useless as far as I can see.
So we need to decide if this still applies and then either remove it or add sae handling to mcck_int_handler and maybe other mcheck functions.

We plan to get more information on the machine checks for sae so we might no longer need that. But this series does not yet contain the machine check handling. Maybe its better to remove comment and nop and introduce that later when we add machine check handling and if still needed.

author reviewer
#50 lore raw

Re: [PATCH v4 19/27] s390: Introduce Start Arm Execution instruction

Quoted 21 lines · Christian Borntraeger

Am 06.07.26 um 13:17 schrieb Janosch Frank:

Quoted 11 lines

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

Quoted 6 lines
arch/s390/kernel/entry.S +6 -0
+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
+# Let the next instruction be NOP to avoid triggering a machine check
+# and handling it in a guest as result of the instruction execution.

@Christian:
I have the feeling things got lost in translation when this comment was written.
How would we trigger a MCHECK by having an instruction after SIE/SAE?
AFAIU this allows us to have a label which we can check in the mcheck handler to decide who triggered the mcheck.

the original rationale was that some machine checks are nullifying some are terminating. So the PSW might point to SAE or the instruction afterwards for a guest machine check.
In SIE, we use that nop to account any machine check on that nop as a guest machine check.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Without the label the comment and NOP are useless as far as I can see.
So we need to decide if this still applies and then either remove it or add sae handling to mcck_int_handler and maybe other mcheck functions.

We plan to get more information on the machine checks for sae so we might no longer need that. But this series does not yet contain the machine check handling. Maybe its better to remove comment and nop and introduce that later when we add machine check handling and if still needed.

Ok I'll remove the comment and the nop for the next round.

author reviewer
#51 lore raw

Re: [PATCH v4 19/27] s390: Introduce Start Arm Execution instruction

In reply to Janosch Frank
Quoted 38 lines · Janosch Frank
Quoted 23 lines · 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.

[...]

Quoted 11 lines
arch/s390/include/asm/kvm_host_arm64_types.h new file +11 -0
+	u64	gpr[31];		/* 0x0300  */
+	u64	_03f8;			/* 0x03f8  */
+
+	union {
+		u64	icptd[8];		/* 0x0400 */
+		/* validity-interception reason; icptr 0x01 */
+#define SAE_VIR_UNKNOWN		0x00
+#define SAE_VIR_UNSUPP_FORMAT	0x01
+#define SAE_VIR_MSO_BOUNDS	0x02
+#define SAE_VIR_MSLA		0x03
+#define SAE_VIR_MGPAS		0x04
Quoted 8 lines

arch/s390/include/asm/kvm_host_arm64_types.h line 87

Quoted 6 lines
arch/s390/include/asm/kvm_host_arm64_types.h new file +6 -0
+#define SAE_VIR_INVAL_SYSREG	0x05
+#define SAE_VIR_HOST_CONTROL	0x06
+#define SAE_VIR_SCA		0x07
+#define SAE_VIR_MSO_ALIGN	0x08
+#define SAE_VIR_HLC		0x09
+#define SAE_VIR_IRPTC		0x0a

Will these ever be used for something?

I'll remove them.

author reviewer
#52 lore raw

PATCH v4 20/27 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 | 210 +++++++++++++++++++++++++
 3 files changed, 225 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 33af8842a71c..0c22cc42be7e 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_PROTECTION			0x04
 #define PGM_ADDRESSING			0x05
arch/s390/include/asm/kvm_host_arm64.h new file +210 -0

210 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#53 lore raw

PATCH v4 21/27 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 f2d490558054..f669569cf78d 100644
@@ -53,7 +53,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;
@@ -106,6 +108,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
#55 lore raw

PATCH v4 22/27 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.

---
 arch/s390/kvm/arm64/arm.c   | 192 ++++++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/arm.h   |   7 ++
 arch/s390/kvm/arm64/guest.c | 102 +++++++++++++++++++
 arch/s390/kvm/arm64/guest.h |   9 ++
 4 files changed, 310 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/guest.h
arch/s390/kvm/arm64/arm.c new file +192 -0
new file mode 100644
index 000000000000..19249ec5c539
@@ -0,0 +1,192 @@
+// 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"
+
+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:
+	case KVM_CAP_ARM_USER_IRQ:
+		ret = 1;
+		break;
+	default:
+		ret = 0;
+	}
+
+	return ret;
+}
+
+static u64 kvm_max_guest_address(void)
+{
+	u64 max_addr;
+
+	if (sclp.hamax == U64_MAX)
+		max_addr = TASK_SIZE_MAX;
+	else
+		max_addr = min_t(u64, TASK_SIZE_MAX, sclp.hamax);
+	return ALIGN_DOWN(max_addr + 1, 1 << 30) - 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)
+{
+}
+
+void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
+{
+}
+
+void kvm_arch_vcpu_unblocking(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 *v)
+{
+	return 0;
+}
+
+static unsigned long system_supported_vcpu_features(void)
+{
+	return KVM_VCPU_VALID_FEATURES;
+}
+
+int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
+			  bool line_status)
+{
+	return 0;
+}
+
+void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
+					     struct kvm_memory_slot *slot,
+					     gfn_t gfn_offset,
+					     unsigned long mask)
+{
+}
+
+bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
+{
+	return false;
+}
+
+void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
+{
+}
+
+void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
+{
+}
+
+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;
+}
+
+void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
+				   struct kvm_memory_slot *slot)
+{
+}
+
+void kvm_arch_flush_shadow_all(struct kvm *kvm)
+{
+}
+
+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 +102 -0

102 lines not on this page · raw message

arch/s390/kvm/arm64/guest.h new file +9 -0
new file mode 100644
index 000000000000..31c8bc799dbe
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef KVM_ARM_GUEST_H
+#define KVM_ARM_GUEST_H
+
+#include <linux/kvm_host.h>
+
+unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
+
+#endif /* KVM_ARM_GUEST_H */
Signature · 1 line
2.53.0
#56 lore raw

Re: [PATCH v4 22/27] KVM: s390: Add basic arm64 kvm module

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Integer underflow in `kvm_max_guest_address()` allows bypassing IPA limit checks, leading to…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Integer underflow in kvm_max_guest_address() allows bypassing IPA limit checks, leading to shift-out-of-bounds undefined behavior.
- [Medium] Falsely advertises support for KVM_CAP_ARM_USER_IRQ while silently dropping interrupt injections.
--

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

KVM: s390: Add basic arm64 kvm module

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

Quoted 5 lines
arch/s390/kvm/arm64/arm.c new file +0 -0
new file mode 100644
index 0000000000000..19249ec5c539f

[ ... ]

Quoted 6 lines
arch/s390/kvm/arm64/arm.c new file +0 -0
@@ -20,172 +20,192 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 {
 	int ret;
 
 	switch (ext) {
 	case KVM_CAP_NR_VCPUS:

arch/s390/kvm/arm64/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 34

Quoted 10 lines
arch/s390/kvm/arm64/arm.c new file +0 -0
 	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:
 	case KVM_CAP_ARM_USER_IRQ:
 		ret = 1;

[Severity: Medium]
Since kvm_vm_ioctl_irq_line() silently drops interrupts and returns success, does advertising support for KVM_CAP_ARM_USER_IRQ here cause userspace to believe the capability works, leading to lost interrupts and guest hangs?

Quoted 7 lines
arch/s390/kvm/arm64/arm.c new file +0 -0
 		break;
 	default:
 		ret = 0;
 	}
 
 	return ret;
 }

arch/s390/kvm/arm64/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 51

Quoted 10 lines
arch/s390/kvm/arm64/arm.c new file +0 -0
 
 static u64 kvm_max_guest_address(void)
 {
 	u64 max_addr;
 
 	if (sclp.hamax == U64_MAX)
 		max_addr = TASK_SIZE_MAX;
 	else
 		max_addr = min_t(u64, TASK_SIZE_MAX, sclp.hamax);
 	return ALIGN_DOWN(max_addr + 1, 1 << 30) - 1;

[Severity: Medium]
Can this underflow if max_addr + 1 is less than 1GB?

If sclp.hamax restricts the maximum address to less than 1GB, ALIGN_DOWN(max_addr + 1, 1 << 30) will evaluate to 0. Subtracting 1 would then underflow to U64_MAX.

Quoted 6 lines
arch/s390/kvm/arm64/arm.c new file +0 -0
 }
 
 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
 {
 	return VM_FAULT_SIGBUS;
 }

arch/s390/kvm/arm64/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 67

Quoted 10 lines
arch/s390/kvm/arm64/arm.c new file +0 -0
 
 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;

[Severity: Medium]
If kvm_max_guest_address() returns U64_MAX due to the underflow above, this will evaluate to U32_MAX.

Could this large value cause the IPA limit validation checks to be bypassed, allowing untrusted userspace to provide a large shift value and trigger a shift-out-of-bounds undefined behavior?

Quoted 1 line
arch/s390/kvm/arm64/arm.c new file +0 -0
 }

[ ... ]

arch/s390/kvm/arm64/arm.c

Quoted 4 lines

int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,

arch/s390/kvm/arm64/arm.c new file +0 -0
 			  bool line_status)
 {
 	return 0;

[Severity: Medium]
This isn't a bug in the implementation of the stub, but since KVM_CAP_ARM_USER_IRQ is advertised as supported, will returning success here without injecting the interrupt deceive userspace and silently drop the interrupts?

Quoted 1 line

}

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

PATCH v4 23/27 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.

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

---
 arch/s390/include/arm64/kvm_emulate.h | 135 ++++++++++++++++++++++++++
 arch/s390/include/arm64/kvm_nested.h  |  11 +++
 arch/s390/kvm/arm64/handle_exit.c     | 110 +++++++++++++++++++++
 arch/s390/kvm/arm64/inject_fault.c    |  21 ++++
 4 files changed, 277 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/kvm/arm64/handle_exit.c
 create mode 100644 arch/s390/kvm/arm64/inject_fault.c
arch/s390/include/arm64/kvm_emulate.h new file +135 -0
new file mode 100644
index 000000000000..6ad21398f86f
@@ -0,0 +1,135 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __S390_ARM64_KVM_EMULATE_H__
+#define __S390_ARM64_KVM_EMULATE_H__
+
+#include <asm/fault.h>
+#include <asm/ptrace.h>
+#include <linux/kvm_host.h>
+
+#include <arm64/kvm_arm.h>
+#include <arm64/sysreg.h>
+
+static __always_inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu)
+{
+	return (unsigned long *)&vcpu->arch.sae_block.pc;
+}
+
+static __always_inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
+{
+	return (unsigned long *)&vcpu->arch.sae_block.pstate;
+}
+
+static __always_inline unsigned long *vcpu_sp_el0(const struct kvm_vcpu *vcpu)
+{
+	return (unsigned long *)&vcpu->arch.sae_block.sp_el0;
+}
+
+static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static __always_inline u64 kvm_vcpu_get_esr(const struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.esr_elz;
+}
+
+static __always_inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.far_elz;
+}
+
+static __always_inline phys_addr_t kvm_vcpu_get_fault_ipa(const struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.teid.addr * PAGE_SIZE;
+}
+
+static inline u16 kvm_vcpu_fault_pic(const struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.pic & PGM_INT_CODE_MASK;
+}
+
+static __always_inline
+bool kvm_vcpu_trap_is_permission_fault(const struct kvm_vcpu *vcpu)
+{
+	return kvm_vcpu_fault_pic(vcpu) == PGM_PROTECTION;
+}
+
+static __always_inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
+{
+	return true;
+}
+
+static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
+{
+	vcpu->arch.hcr_elz = HCR_E2H | HCR_RW | HCR_AMO | HCR_IMO | HCR_FMO |
+			     HCR_PTW;
+	/* traps */
+	vcpu->arch.hcr_elz |= HCR_TSC | HCR_TID1 | HCR_TID2 | HCR_TID3 |
+			      HCR_TID4 | HCR_TID5 | HCR_TIDCP;
+}
+
+static inline unsigned long vcpu_get_vsesr(struct kvm_vcpu *vcpu)
+{
+	WARN(true, "not implemented, just feat RAS");
+
+	return 0L;
+}
+
+static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr)
+{
+	WARN(true, "not implemented, just feat RAS");
+}
+
+static inline bool vcpu_el2_tge_is_set(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static inline int kvm_vcpu_abt_gltl(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.gltl;
+}
+
+static inline bool is_hyp_ctxt(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static inline bool is_nested_ctxt(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu)
+{
+	u32 mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK;
+
+	return mode != PSR_MODE_EL0t;
+}
+
+#define SPSR_SS BIT(21)
+
+static inline void kvm_skip_instr(struct kvm_vcpu *vcpu)
+{
+	*vcpu_pc(vcpu) += 4;
+	*vcpu_cpsr(vcpu) &= ~PSR_BTYPE_MASK;
+
+	/* advance the singlestep state machine */
+	*vcpu_cpsr(vcpu) &= ~SPSR_SS;
+}
+
+#include <arm64/kvm_emulate-part.h>
+
+#endif /* __S390_ARM64_KVM_EMULATE_H__ */
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/kvm/arm64/handle_exit.c new file +110 -0

110 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
#58 lore raw

Re: [PATCH v4 23/27] KVM: s390: arm64: Implement required functions

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] PSCI SYSTEM_OFF handler fails to stop other vCPUs, leading to potential guest corruption during…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] PSCI SYSTEM_OFF handler fails to stop other vCPUs, leading to potential guest corruption during shutdown.
- [Medium] Empty stubs for exception injection cause guest vCPUs to hang in an infinite loop. --

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

KVM: s390: arm64: Implement required functions

This commit implements the required functions for s390 to support shared arm64 KVM code. It also introduces a basic SMCCC handler to stop a vCPU.

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

[ ... ]

arch/s390/kvm/arm64/handle_exit.c line 32

Quoted 11 lines
arch/s390/kvm/arm64/handle_exit.c new file +11 -0
+static int kvm_smccc_call_handler(struct kvm_vcpu *vcpu)
+{
+	u32 func_id = vcpu_get_reg(vcpu, 0);
+	u64 val = PSCI_RET_NOT_SUPPORTED;
+	int ret = 1;
+
+	if (func_id == PSCI_0_2_FN_SYSTEM_OFF) {
+		spin_lock(&vcpu->arch.mp_state_lock);
+		WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
+		spin_unlock(&vcpu->arch.mp_state_lock);
+		kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);

[Severity: High]
Does this actually stop all other vCPUs in the system?

Because the mp_state is only updated for the calling vCPU, broadcasting KVM_REQ_SLEEP will leave the other vCPUs in their current state. When they process the sleep request, won't they evaluate their own mp_state, see they aren't STOPPED, and continue executing guest code while the VM is supposed to be shutting down?

Quoted 13 lines
arch/s390/kvm/arm64/handle_exit.c new file +13 -0
+		memset(&vcpu->run->system_event, 0,
+		       sizeof(vcpu->run->system_event));
+		vcpu->run->system_event.type = KVM_SYSTEM_EVENT_SHUTDOWN;
+		vcpu->run->system_event.ndata = 1;
+		vcpu->run->system_event.data[0] = 0;
+		vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
+		val = PSCI_RET_INTERNAL_FAILURE;
+		ret = 0;
+	}
+	vcpu_set_reg(vcpu, 0, val);
+
+	return ret;
+}

[ ... ]

Quoted 11 lines
arch/s390/kvm/arm64/inject_fault.c new file +5 -0
new file mode 100644
index 0000000000000..425dbeaa421c6
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <arm64/kvm_emulate.h>
+
+/**

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

Quoted 10 lines
arch/s390/kvm/arm64/inject_fault.c new file +10 -0
+ * 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*/
+}

[Severity: Medium]
Will this empty stub cause the guest to get stuck in an infinite exit loop?

Since the function does nothing, it doesn't advance the guest's Program Counter or update exception state. When handle_exit() resumes the guest, won't it immediately re-execute the same trapping instruction over and over, hanging the vCPU and burning host CPU cycles?

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;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
maintainer
#59 lore raw

Re: [PATCH v4 23/27] KVM: s390: arm64: Implement required functions

In reply to Steffen Eiden
Quoted 18 lines · Steffen Eiden

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

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

---
  arch/s390/include/arm64/kvm_emulate.h | 135 ++++++++++++++++++++++++++
  arch/s390/include/arm64/kvm_nested.h  |  11 +++
  arch/s390/kvm/arm64/handle_exit.c     | 110 +++++++++++++++++++++
  arch/s390/kvm/arm64/inject_fault.c    |  21 ++++
  4 files changed, 277 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/kvm/arm64/handle_exit.c create mode 100644 arch/s390/kvm/arm64/inject_fault.c

[...]

Quoted 39 lines
arch/s390/kvm/arm64/handle_exit.c new file +39 -0
+/* manually copied from arch/arm64/kernel/traps.c */
+static const char * const esr_class_str[] = {
+	[0 ... ESR_ELx_EC_MAX]		= "UNRECOGNIZED EC",
+	[ESR_ELx_EC_UNKNOWN]		= "Unknown/Uncategorized",
+	[ESR_ELx_EC_WFx]		= "WFI/WFE",
+	[ESR_ELx_EC_CP15_32]		= "CP15 MCR/MRC",
+	[ESR_ELx_EC_CP15_64]		= "CP15 MCRR/MRRC",
+	[ESR_ELx_EC_CP14_MR]		= "CP14 MCR/MRC",
+	[ESR_ELx_EC_CP14_LS]		= "CP14 LDC/STC",
+	[ESR_ELx_EC_FP_ASIMD]		= "ASIMD",
+	[ESR_ELx_EC_CP10_ID]		= "CP10 MRC/VMRS",
+	[ESR_ELx_EC_PAC]		= "PAC",
+	[ESR_ELx_EC_CP14_64]		= "CP14 MCRR/MRRC",
+	[ESR_ELx_EC_BTI]		= "BTI",
+	[ESR_ELx_EC_ILL]		= "PSTATE.IL",
+	[ESR_ELx_EC_SVC32]		= "SVC (AArch32)",
+	[ESR_ELx_EC_HVC32]		= "HVC (AArch32)",
+	[ESR_ELx_EC_SMC32]		= "SMC (AArch32)",
+	[ESR_ELx_EC_SVC64]		= "SVC (AArch64)",
+	[ESR_ELx_EC_HVC64]		= "HVC (AArch64)",
+	[ESR_ELx_EC_SMC64]		= "SMC (AArch64)",
+	[ESR_ELx_EC_SYS64]		= "MSR/MRS (AArch64)",
+	[ESR_ELx_EC_SVE]		= "SVE",
+	[ESR_ELx_EC_ERET]		= "ERET/ERETAA/ERETAB",
+	[ESR_ELx_EC_FPAC]		= "FPAC",
+	[ESR_ELx_EC_SME]		= "SME",
+	[ESR_ELx_EC_IMP_DEF]		= "EL3 IMP DEF",
+	[ESR_ELx_EC_IABT_LOW]		= "IABT (lower EL)",
+	[ESR_ELx_EC_IABT_CUR]		= "IABT (current EL)",
+	[ESR_ELx_EC_PC_ALIGN]		= "PC Alignment",
+	[ESR_ELx_EC_DABT_LOW]		= "DABT (lower EL)",
+	[ESR_ELx_EC_DABT_CUR]		= "DABT (current EL)",
+	[ESR_ELx_EC_SP_ALIGN]		= "SP Alignment",
+	[ESR_ELx_EC_MOPS]		= "MOPS",
+	[ESR_ELx_EC_FP_EXC32]		= "FP (AArch32)",
+	[ESR_ELx_EC_FP_EXC64]		= "FP (AArch64)",
+	[ESR_ELx_EC_GCS]		= "Guarded Control Stack",
+	[ESR_ELx_EC_SERROR]		= "SError",
+	[ESR_ELx_EC_BREAKPT_LOW]	= "Breakpoint (lower EL)",

arch/s390/kvm/arm64/handle_exit.c line 106

Quoted 10 lines
arch/s390/kvm/arm64/handle_exit.c new file +10 -0
+	[ESR_ELx_EC_BREAKPT_CUR]	= "Breakpoint (current EL)",
+	[ESR_ELx_EC_SOFTSTP_LOW]	= "Software Step (lower EL)",
+	[ESR_ELx_EC_SOFTSTP_CUR]	= "Software Step (current EL)",
+	[ESR_ELx_EC_WATCHPT_LOW]	= "Watchpoint (lower EL)",
+	[ESR_ELx_EC_WATCHPT_CUR]	= "Watchpoint (current EL)",
+	[ESR_ELx_EC_BKPT32]		= "BKPT (AArch32)",
+	[ESR_ELx_EC_VECTOR32]		= "Vector catch (AArch32)",
+	[ESR_ELx_EC_BRK64]		= "BRK (AArch64)",
+};
+

We could rip out the 32bit ones.
Not necessarily right now as it's easier to compare to the aarch64 implementation like this but at least at a later time.

I was also wondering if it would make sense to have all the feature checks for unimplemented features in one file.

maintainer
#60 lore raw

Re: [PATCH v4 23/27] KVM: s390: arm64: Implement required functions

In reply to Steffen Eiden
Quoted 90 lines · Steffen Eiden

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

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

---
 arch/s390/include/arm64/kvm_emulate.h | 135 ++++++++++++++++++++++++++
 arch/s390/include/arm64/kvm_nested.h  |  11 +++
 arch/s390/kvm/arm64/handle_exit.c     | 110 +++++++++++++++++++++
 arch/s390/kvm/arm64/inject_fault.c    |  21 ++++
 4 files changed, 277 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/kvm/arm64/handle_exit.c
 create mode 100644 arch/s390/kvm/arm64/inject_fault.c
arch/s390/include/arm64/kvm_emulate.h new file +65 -0
new file mode 100644
index 000000000000..6ad21398f86f
@@ -0,0 +1,135 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __S390_ARM64_KVM_EMULATE_H__
+#define __S390_ARM64_KVM_EMULATE_H__
+
+#include <asm/fault.h>
+#include <asm/ptrace.h>
+#include <linux/kvm_host.h>
+
+#include <arm64/kvm_arm.h>
+#include <arm64/sysreg.h>
+
+static __always_inline unsigned long *vcpu_pc(const struct kvm_vcpu *vcpu)
+{
+	return (unsigned long *)&vcpu->arch.sae_block.pc;
+}
+
+static __always_inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
+{
+	return (unsigned long *)&vcpu->arch.sae_block.pstate;
+}
+
+static __always_inline unsigned long *vcpu_sp_el0(const struct kvm_vcpu *vcpu)
+{
+	return (unsigned long *)&vcpu->arch.sae_block.sp_el0;
+}
+
+static __always_inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static __always_inline u64 kvm_vcpu_get_esr(const struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.esr_elz;
+}
+
+static __always_inline unsigned long kvm_vcpu_get_hfar(const struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.far_elz;
+}
+
+static __always_inline phys_addr_t kvm_vcpu_get_fault_ipa(const struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.teid.addr * PAGE_SIZE;
+}
+
+static inline u16 kvm_vcpu_fault_pic(const struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.pic & PGM_INT_CODE_MASK;
+}
+
+static __always_inline
+bool kvm_vcpu_trap_is_permission_fault(const struct kvm_vcpu *vcpu)
+{
+	return kvm_vcpu_fault_pic(vcpu) == PGM_PROTECTION;
+}
+
+static __always_inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
+{
+	return true;
+}
+
+static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)
+{

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

Quoted 10 lines
arch/s390/include/arm64/kvm_emulate.h new file +10 -0
+	return false;
+}
+
+static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
+{
+	vcpu->arch.hcr_elz = HCR_E2H | HCR_RW | HCR_AMO | HCR_IMO | HCR_FMO |
+			     HCR_PTW;
+	/* traps */
+	vcpu->arch.hcr_elz |= HCR_TSC | HCR_TID1 | HCR_TID2 | HCR_TID3 |
+			      HCR_TID4 | HCR_TID5 | HCR_TIDCP;

Since this is new code, consider using the generated symbols (HCR_EL2_*). I intend to get rid of the compat symbols at some point.

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

Quoted 5 lines
arch/s390/include/arm64/kvm_emulate.h new file +5 -0
+}
+
+static inline unsigned long vcpu_get_vsesr(struct kvm_vcpu *vcpu)
+{
+	WARN(true, "not implemented, just feat RAS");

WARN_ONCE() should be enough. And really, this code shouldn't be reachable at all.

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

Quoted 7 lines
arch/s390/include/arm64/kvm_emulate.h new file +7 -0
+
+	return 0L;
+}
+
+static inline void vcpu_set_vsesr(struct kvm_vcpu *vcpu, u64 vsesr)
+{
+	WARN(true, "not implemented, just feat RAS");

Same thing.

Quoted 25 lines
arch/s390/include/arm64/kvm_emulate.h new file +25 -0
+}
+
+static inline bool vcpu_el2_tge_is_set(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static inline int kvm_vcpu_abt_gltl(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.sae_block.hai.gltl;
+}
+
+static inline bool is_hyp_ctxt(const struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static inline bool is_nested_ctxt(struct kvm_vcpu *vcpu)
+{
+	return false;

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

Quoted 10 lines
arch/s390/include/arm64/kvm_emulate.h new file +10 -0
+}
+
+static inline bool vcpu_mode_priv(const struct kvm_vcpu *vcpu)
+{
+	u32 mode = *vcpu_cpsr(vcpu) & PSR_MODE_MASK;
+
+	return mode != PSR_MODE_EL0t;
+}
+
+#define SPSR_SS BIT(21)

We may be better off defining SPSR_ELx in the sysreg file and use that all over the place.

Quoted 43 lines
arch/s390/include/arm64/kvm_emulate.h new file +13 -0
+
+static inline void kvm_skip_instr(struct kvm_vcpu *vcpu)
+{
+	*vcpu_pc(vcpu) += 4;
+	*vcpu_cpsr(vcpu) &= ~PSR_BTYPE_MASK;
+
+	/* advance the singlestep state machine */
+	*vcpu_cpsr(vcpu) &= ~SPSR_SS;
+}
+
+#include <arm64/kvm_emulate-part.h>
+
+#endif /* __S390_ARM64_KVM_EMULATE_H__ */
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/kvm/arm64/handle_exit.c new file +7 -0
new file mode 100644
index 000000000000..a0ebe5ffa19a
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/kvm_host.h>
+
+#include <arm64/esr.h>
+#include <arm64/kvm_emulate.h>
+

arch/s390/kvm/arm64/handle_exit.c line 17

Quoted 10 lines
arch/s390/kvm/arm64/handle_exit.c new file +10 -0
+typedef int (*exit_handle_fn)(struct kvm_vcpu *);
+exit_handle_fn arm_exit_handlers[ESR_ELx_EC_MAX + 1];
+
+#define __INCL_GEN_ARM_FILE
+#include "generated/handle_exit.inc"
+#undef __INCL_GEN_ARM_FILE
+
+#define PSCI_0_2_FN_SYSTEM_OFF		0x84000008
+#define PSCI_RET_NOT_SUPPORTED		-1
+#define PSCI_RET_INTERNAL_FAILURE	-6

All of this exists in include/uapi/linux/psci.h.

Quoted 4 lines
arch/s390/kvm/arm64/handle_exit.c new file +4 -0
+/*
+ * Temporary smc/hvc handler. Non-compliant implementation (features missing).
+ * Implements only system off so that test programs are able to end their execution
+ */

arch/s390/kvm/arm64/handle_exit.c line 31

Quoted 10 lines
arch/s390/kvm/arm64/handle_exit.c new file +10 -0
+static int kvm_smccc_call_handler(struct kvm_vcpu *vcpu)
+{
+	u32 func_id = vcpu_get_reg(vcpu, 0);
+	u64 val = PSCI_RET_NOT_SUPPORTED;
+	int ret = 1;
+
+	if (func_id == PSCI_0_2_FN_SYSTEM_OFF) {
+		spin_lock(&vcpu->arch.mp_state_lock);
+		WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
+		spin_unlock(&vcpu->arch.mp_state_lock);

scoped_guard()?

Quoted 64 lines
arch/s390/kvm/arm64/handle_exit.c new file +64 -0
+		kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
+		memset(&vcpu->run->system_event, 0,
+		       sizeof(vcpu->run->system_event));
+		vcpu->run->system_event.type = KVM_SYSTEM_EVENT_SHUTDOWN;
+		vcpu->run->system_event.ndata = 1;
+		vcpu->run->system_event.data[0] = 0;
+		vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
+		val = PSCI_RET_INTERNAL_FAILURE;
+		ret = 0;
+	}
+	vcpu_set_reg(vcpu, 0, val);
+
+	return ret;
+}
+
+static int handle_hvc(struct kvm_vcpu *vcpu)
+{
+	vcpu->stat.hvc_exit_stat++;
+	return kvm_smccc_call_handler(vcpu);
+}
+
+exit_handle_fn arm_exit_handlers[] = {
+	[0 ... ESR_ELx_EC_MAX]	= kvm_handle_unknown_ec,
+	[ESR_ELx_EC_HVC64]	= handle_hvc,
+};
+
+/* manually copied from arch/arm64/kernel/traps.c */
+static const char * const esr_class_str[] = {
+	[0 ... ESR_ELx_EC_MAX]		= "UNRECOGNIZED EC",
+	[ESR_ELx_EC_UNKNOWN]		= "Unknown/Uncategorized",
+	[ESR_ELx_EC_WFx]		= "WFI/WFE",
+	[ESR_ELx_EC_CP15_32]		= "CP15 MCR/MRC",
+	[ESR_ELx_EC_CP15_64]		= "CP15 MCRR/MRRC",
+	[ESR_ELx_EC_CP14_MR]		= "CP14 MCR/MRC",
+	[ESR_ELx_EC_CP14_LS]		= "CP14 LDC/STC",
+	[ESR_ELx_EC_FP_ASIMD]		= "ASIMD",
+	[ESR_ELx_EC_CP10_ID]		= "CP10 MRC/VMRS",
+	[ESR_ELx_EC_PAC]		= "PAC",
+	[ESR_ELx_EC_CP14_64]		= "CP14 MCRR/MRRC",
+	[ESR_ELx_EC_BTI]		= "BTI",
+	[ESR_ELx_EC_ILL]		= "PSTATE.IL",
+	[ESR_ELx_EC_SVC32]		= "SVC (AArch32)",
+	[ESR_ELx_EC_HVC32]		= "HVC (AArch32)",
+	[ESR_ELx_EC_SMC32]		= "SMC (AArch32)",
+	[ESR_ELx_EC_SVC64]		= "SVC (AArch64)",
+	[ESR_ELx_EC_HVC64]		= "HVC (AArch64)",
+	[ESR_ELx_EC_SMC64]		= "SMC (AArch64)",
+	[ESR_ELx_EC_SYS64]		= "MSR/MRS (AArch64)",
+	[ESR_ELx_EC_SVE]		= "SVE",
+	[ESR_ELx_EC_ERET]		= "ERET/ERETAA/ERETAB",
+	[ESR_ELx_EC_FPAC]		= "FPAC",
+	[ESR_ELx_EC_SME]		= "SME",
+	[ESR_ELx_EC_IMP_DEF]		= "EL3 IMP DEF",
+	[ESR_ELx_EC_IABT_LOW]		= "IABT (lower EL)",
+	[ESR_ELx_EC_IABT_CUR]		= "IABT (current EL)",
+	[ESR_ELx_EC_PC_ALIGN]		= "PC Alignment",
+	[ESR_ELx_EC_DABT_LOW]		= "DABT (lower EL)",
+	[ESR_ELx_EC_DABT_CUR]		= "DABT (current EL)",
+	[ESR_ELx_EC_SP_ALIGN]		= "SP Alignment",
+	[ESR_ELx_EC_MOPS]		= "MOPS",
+	[ESR_ELx_EC_FP_EXC32]		= "FP (AArch32)",
+	[ESR_ELx_EC_FP_EXC64]		= "FP (AArch64)",
+	[ESR_ELx_EC_GCS]		= "Guarded Control Stack",
+	[ESR_ELx_EC_SERROR]		= "SError",

arch/s390/kvm/arm64/handle_exit.c line 105

Quoted 10 lines
arch/s390/kvm/arm64/handle_exit.c new file +10 -0
+	[ESR_ELx_EC_BREAKPT_LOW]	= "Breakpoint (lower EL)",
+	[ESR_ELx_EC_BREAKPT_CUR]	= "Breakpoint (current EL)",
+	[ESR_ELx_EC_SOFTSTP_LOW]	= "Software Step (lower EL)",
+	[ESR_ELx_EC_SOFTSTP_CUR]	= "Software Step (current EL)",
+	[ESR_ELx_EC_WATCHPT_LOW]	= "Watchpoint (lower EL)",
+	[ESR_ELx_EC_WATCHPT_CUR]	= "Watchpoint (current EL)",
+	[ESR_ELx_EC_BKPT32]		= "BKPT (AArch32)",
+	[ESR_ELx_EC_VECTOR32]		= "Vector catch (AArch32)",
+	[ESR_ELx_EC_BRK64]		= "BRK (AArch64)",
+};

There are a lot of AArch32-specific ECs here that you may not need.

Thanks,

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

Re: [PATCH v4 23/27] KVM: s390: arm64: Implement required functions

Quoted 20 lines · Marc Zyngier
Quoted 18 lines · Steffen Eiden

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

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

---
 arch/s390/include/arm64/kvm_emulate.h | 135 ++++++++++++++++++++++++++
 arch/s390/include/arm64/kvm_nested.h  |  11 +++
 arch/s390/kvm/arm64/handle_exit.c     | 110 +++++++++++++++++++++
 arch/s390/kvm/arm64/inject_fault.c    |  21 ++++
 4 files changed, 277 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/kvm/arm64/handle_exit.c
 create mode 100644 arch/s390/kvm/arm64/inject_fault.c

...

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+#define SPSR_SS BIT(21)

We may be better off defining SPSR_ELx in the sysreg file and use that all over the place.

make sense. I'll create a patch for this.

...

Quoted 10 lines

arch/s390/kvm/arm64/handle_exit.c line 17

Quoted 8 lines
arch/s390/kvm/arm64/handle_exit.c new file +8 -0
+
+#define __INCL_GEN_ARM_FILE
+#include "generated/handle_exit.inc"
+#undef __INCL_GEN_ARM_FILE
+
+#define PSCI_0_2_FN_SYSTEM_OFF		0x84000008
+#define PSCI_RET_NOT_SUPPORTED		-1
+#define PSCI_RET_INTERNAL_FAILURE	-6

All of this exists in include/uapi/linux/psci.h.

Yes. We'll use them in a later series as well.
this is just a placeholder implememtation so guests do not have to run idefenitely.

...

Quoted 9 lines

arch/s390/kvm/arm64/handle_exit.c line 105

Quoted 7 lines
arch/s390/kvm/arm64/handle_exit.c new file +7 -0
+	[ESR_ELx_EC_SOFTSTP_CUR]	= "Software Step (current EL)",
+	[ESR_ELx_EC_WATCHPT_LOW]	= "Watchpoint (lower EL)",
+	[ESR_ELx_EC_WATCHPT_CUR]	= "Watchpoint (current EL)",
+	[ESR_ELx_EC_BKPT32]		= "BKPT (AArch32)",
+	[ESR_ELx_EC_VECTOR32]		= "Vector catch (AArch32)",
+	[ESR_ELx_EC_BRK64]		= "BRK (AArch64)",
+};

There are a lot of AArch32-specific ECs here that you may not need.

you are absolutely right. This was just a blind 1to1 copy. IMO they do not hurt but I can reduce 32 specific ones.

FYI
The code is not shared as I would have to spin up a whole sharing infrastructure to share this one defenition from arm kernel code.

Steffen

author reviewer
#62 lore raw

PATCH v4 24/27 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.

---
 arch/s390/kvm/arm64/arm.c       | 190 ++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/arm.h       |  56 ++++++++++
 arch/s390/kvm/gmap/faultin.c    |   5 +
 arch/s390/kvm/gmap/gmap.c       |   4 +
 arch/s390/kvm/gmap/trace-gmap.h |  13 +++
 5 files changed, 268 insertions(+)
arch/s390/kvm/arm64/arm.c +190 -0
index 19249ec5c539..3a4092698fae 100644
@@ -8,6 +8,10 @@
 #include <linux/kvm_types.h>
 #include <linux/kvm_host.h>
 
+#include <arm64/kvm_mmu.h>
+
+#include <gmap.h>
+
 #include "arm.h"
 
 static unsigned long system_supported_vcpu_features(void);
@@ -31,6 +35,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		break;
 	case KVM_CAP_IOEVENTFD:
 	case KVM_CAP_ARM_USER_IRQ:
+	case KVM_CAP_PRE_FAULT_MEMORY:
 		ret = 1;
 		break;
 	default:
@@ -51,6 +56,82 @@ static u64 kvm_max_guest_address(void)
 	return ALIGN_DOWN(max_addr + 1, 1 << 30) - 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", current->pid);
+	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_max_guest_address();
+
+	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 +143,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 +160,39 @@ 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;
+
+	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 = kvm_max_guest_address();
+
+	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");
+}
+
 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
 {
 }
@@ -113,6 +230,58 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
 	return 0;
 }
 
+int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
+			       struct kvm_dirty_log *log)
+{
+	return gmap_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 gmap_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)
+{
+	gmap_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)
+{
+	return gmap_test_age_gfn(kvm, range);
+}
+
+long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
+				    struct kvm_pre_fault_memory *range)
+{
+	return gmap_pre_fault_memory(vcpu, range);
+}
+
 void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
 					     struct kvm_memory_slot *slot,
 					     gfn_t gfn_offset,
@@ -120,6 +289,27 @@ void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
 {
 }
 
+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 +56 -0

62 lines not on this page · raw message

arch/s390/kvm/gmap/faultin.c +5 -0
index 713914dc8eb2..b417ba575e6e 100644
@@ -10,6 +10,11 @@
 
 #include "gmap.h"
 #include "faultin.h"
+#ifdef KVM_S390_ARM64
+#include "arm.h"
+#else
+#include "s390.h"
+#endif
 
 #define CREATE_TRACE_POINTS
 #include "trace-gmap.h"
arch/s390/kvm/gmap/gmap.c +4 -0
index 3dcd1d4c6a29..b2ba6ee85fef 100644
@@ -24,7 +24,11 @@
 #include "cmma.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/trace-gmap.h +13 -0
index 78559298932c..25f8a527fdde 100644
@@ -10,6 +10,18 @@
 #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)
@@ -20,6 +32,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
#63 lore raw

Re: [PATCH v4 24/27] KVM: s390: arm64: Implement vm/vcpu create destroy.

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

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

New issues:
- [High] Mismatch between kvm->arch.mem_limit and gmap bounds allows an infinite page fault loop.
- [High] kvm_max_guest_address() underflows to U64_MAX when the host's maximum memory address is less than 1GB.
- [Medium] Off-by-one boundary check error due to mismatched mem_limit semantics.

Pre-existing issues:
- [Critical] Lost dirty pages during live migration due to non-atomic memset() in gmap_get_dirty_log().
- [High] Memory allocation in kvm_arch_commit_memory_region allows host Use-After-Free on OOM. --

commit 07502b29add56a1b469b806719c1e501111b58a8
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Implement vm/vcpu create destroy.

Implement init and destroy IOCTLS for vcpu and vm. Implement arch vm IOCTL. Use s390 gmap.

Quoted 4 lines
arch/s390/kvm/arm64/arm.c +0 -0
index 19249ec5c539f..3a4092698fae2 100644

[ ... ]

arch/s390/kvm/arm64/arm.c static u64 kvm_max_guest_address(void) · line 56

Quoted 2 lines
arch/s390/kvm/arm64/arm.c +0 -0
@@ -51,6 +56,82 @@ static u64 kvm_max_guest_address(void)
 	return ALIGN_DOWN(max_addr + 1, 1 << 30) - 1;

[Severity: High]
Does this ALIGN_DOWN() calculation underflow to U64_MAX if the host's maximum memory address is less than 1GB? On hosts with low memory configurations (e.g. sclp.hamax < 1GB), ALIGN_DOWN(max_addr + 1, 1 << 30) evaluates to 0, causing the subtraction to underflow.

[Severity: Medium]
Will this inclusive boundary cause an off-by-one rejection of valid memory slots? By subtracting 1 here, kvm_max_guest_address() returns an inclusive boundary which is later assigned to mem_limit. If a memslot's exclusive end address perfectly matches the maximum permitted address, gmap_prepare_memory_region() could reject it:

if ((new->base_gfn + new->npages) * PAGE_SIZE > kvm->arch.mem_limit)

        return -EINVAL;
Quoted 4 lines
arch/s390/kvm/arm64/arm.c +2 -0
 }
 
+static int kvm_gmap_init(struct kvm *kvm)
+{

[ ... ]

Quoted 1 line
arch/s390/kvm/arm64/arm.c +1 -0
+	kvm->arch.gmap = gmap_new(kvm, gpa_to_gfn(kvm->arch.guest_phys_size));

[ ... ]

Quoted 2 lines
arch/s390/kvm/arm64/arm.c +2 -0
+int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
+{

[ ... ]

arch/s390/kvm/arm64/arm.c static u64 kvm_max_guest_address(void) · line 124

Quoted 4 lines
arch/s390/kvm/arm64/arm.c +4 -0
+	ret = kvm_gmap_init(kvm);
+	if (ret)
+		goto out_err;
+	kvm->arch.mem_limit = kvm_max_guest_address();

[Severity: High]
Can this mismatch between gmap bounds and mem_limit lead to an infinite page fault loop? The gmap is limited to guest_phys_size (the requested IPA limit), but mem_limit is set unconditionally to the maximum host-supported address.

If userspace creates a memslot beyond guest_phys_size but below mem_limit, KVM resolving the PFN and calling gmap_link() would reject the address via dat_entry_walk(), returning -EAGAIN, causing kvm_s390_faultin_gfn() to retry endlessly.

[ ... ]

arch/s390/kvm/arm64/arm.c int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, · line 236

Quoted 8 lines
arch/s390/kvm/arm64/arm.c +4 -0
@@ -113,6 +230,58 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
 	return 0;
 }
 
+int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
+			       struct kvm_dirty_log *log)
+{
+	return gmap_get_dirty_log(kvm, log);

[Severity: Critical]
This is a pre-existing issue, but does gmap_get_dirty_log() lose dirty pages during live migration? It uses a non-atomic memset() to clear the dirty bitmap after copying it:

gmap_get_dirty_log() {
    ...
    memset(memslot->dirty_bitmap, 0, n);
    ...
}

If a guest VCPU concurrently faults and marks a page dirty, the atomic bit set operation in mark_page_dirty() could be blindly overwritten by the memset().

[ ... ]

arch/s390/kvm/arm64/arm.c int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, · line 266

Quoted 6 lines
arch/s390/kvm/arm64/arm.c +6 -0
+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)
+{
+	gmap_commit_memory_region(kvm, old, new, change);

[Severity: High]
This isn't a bug introduced by this patch, but does the memory allocation in gmap_commit_memory_region() allow a host use-after-free on OOM? When host userspace removes a memory slot, KVM calls commit_memory_region(), which cannot return an error. However, gmap_commit_memory_region() dynamically allocates memory via kvm_s390_new_mmu_cache():

gmap_commit_memory_region() {
    ...
    mc = kvm_s390_new_mmu_cache();
    if (!mc) {
        rc = -ENOMEM;
        goto out;
    }
    ...
}

If this fails under memory pressure, the function returns early and skips dat_delete_slot(). The memslot deletion proceeds globally and the host memory is freed, but the guest retains hardware page table entries pointing to it.

Quoted 1 line
arch/s390/kvm/arm64/arm.c +1 -0
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
maintainer
#64 lore raw

Re: [PATCH v4 24/27] KVM: s390: arm64: Implement vm/vcpu create destroy.

In reply to Steffen Eiden
Quoted 35 lines · Steffen Eiden

Implement init and destroy IOCTLS for vcpu and vm. Implement arch vm IOCTL. Use s390 gmap.

---
 arch/s390/kvm/arm64/arm.c       | 190 ++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/arm.h       |  56 ++++++++++
 arch/s390/kvm/gmap/faultin.c    |   5 +
 arch/s390/kvm/gmap/gmap.c       |   4 +
 arch/s390/kvm/gmap/trace-gmap.h |  13 +++
 5 files changed, 268 insertions(+)
arch/s390/kvm/arm64/arm.c +4 -0
index 19249ec5c539..3a4092698fae 100644
@@ -8,6 +8,10 @@
 #include <linux/kvm_types.h>
 #include <linux/kvm_host.h>
 
+#include <arm64/kvm_mmu.h>
+
+#include <gmap.h>
+
 #include "arm.h"
 
 static unsigned long system_supported_vcpu_features(void);

arch/s390/kvm/arm64/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 38

Quoted 4 lines
arch/s390/kvm/arm64/arm.c +1 -0
@@ -31,6 +35,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		break;
 	case KVM_CAP_IOEVENTFD:
 	case KVM_CAP_ARM_USER_IRQ:
+	case KVM_CAP_PRE_FAULT_MEMORY:

arm64 *doesn't* support this yet. Please make sure you don't deviate from what arm64 currently supports.

Thanks,

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

Re: [PATCH v4 24/27] KVM: s390: arm64: Implement vm/vcpu create destroy.

Quoted 35 lines · Marc Zyngier
Quoted 33 lines · Steffen Eiden

Implement init and destroy IOCTLS for vcpu and vm. Implement arch vm IOCTL. Use s390 gmap.

---
 arch/s390/kvm/arm64/arm.c       | 190 ++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/arm.h       |  56 ++++++++++
 arch/s390/kvm/gmap/faultin.c    |   5 +
 arch/s390/kvm/gmap/gmap.c       |   4 +
 arch/s390/kvm/gmap/trace-gmap.h |  13 +++
 5 files changed, 268 insertions(+)
arch/s390/kvm/arm64/arm.c +4 -0
index 19249ec5c539..3a4092698fae 100644
@@ -8,6 +8,10 @@
 #include <linux/kvm_types.h>
 #include <linux/kvm_host.h>
 
+#include <arm64/kvm_mmu.h>
+
+#include <gmap.h>
+
 #include "arm.h"
 
Quoted 10 lines

arch/s390/kvm/arm64/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 38

Quoted 6 lines
arch/s390/kvm/arm64/arm.c +1 -0
 static unsigned long system_supported_vcpu_features(void);
@@ -31,6 +35,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		break;
 	case KVM_CAP_IOEVENTFD:
 	case KVM_CAP_ARM_USER_IRQ:
+	case KVM_CAP_PRE_FAULT_MEMORY:

arm64 *doesn't* support this yet. Please make sure you don't deviate from what arm64 currently supports.

I hoped the corresponding arm64 series is merged when we got this :)

I will remove this advertisement. I'll keep the implementation as the compiler expect that function to be present anyways, as native s390 KVM now implements this CAP. OK?

Quoted 6 lines

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer
#66 lore raw

Re: [PATCH v4 24/27] KVM: s390: arm64: Implement vm/vcpu create destroy.

Quoted 48 lines · Steffen Eiden
Quoted 35 lines · Marc Zyngier
Quoted 33 lines · Steffen Eiden

Implement init and destroy IOCTLS for vcpu and vm. Implement arch vm IOCTL. Use s390 gmap.

---
 arch/s390/kvm/arm64/arm.c       | 190 ++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/arm.h       |  56 ++++++++++
 arch/s390/kvm/gmap/faultin.c    |   5 +
 arch/s390/kvm/gmap/gmap.c       |   4 +
 arch/s390/kvm/gmap/trace-gmap.h |  13 +++
 5 files changed, 268 insertions(+)
arch/s390/kvm/arm64/arm.c +4 -0
index 19249ec5c539..3a4092698fae 100644
@@ -8,6 +8,10 @@
 #include <linux/kvm_types.h>
 #include <linux/kvm_host.h>
 
+#include <arm64/kvm_mmu.h>
+
+#include <gmap.h>
+
 #include "arm.h"
 
Quoted 10 lines

arch/s390/kvm/arm64/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 38

Quoted 6 lines
arch/s390/kvm/arm64/arm.c +1 -0
 static unsigned long system_supported_vcpu_features(void);
@@ -31,6 +35,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		break;
 	case KVM_CAP_IOEVENTFD:
 	case KVM_CAP_ARM_USER_IRQ:
+	case KVM_CAP_PRE_FAULT_MEMORY:

arm64 *doesn't* support this yet. Please make sure you don't deviate from what arm64 currently supports.

Quoted 1 line

I hoped the corresponding arm64 series is merged when we got this :)

If the quality of the previous postings of that series is anything to go by, I doubt this will be merged on this side of the death of the universe.

Quoted 3 lines

I will remove this advertisement. I'll keep the implementation as the compiler expect that function to be present anyways, as native s390 KVM now implements this CAP. OK?

As long as the code isn't reachable from the arm64 side, that's fine by me.

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

PATCH v4 25/27 KVM: s390: arm64: Implement vCPU IOCTLs

In reply to Steffen Eiden

Implement all vCPU IOCTLs.

---
 arch/s390/kvm/arm64/arm.c         | 344 ++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/guest.c       |  67 ++++++
 arch/s390/kvm/arm64/guest.h       |   5 +
 arch/s390/kvm/arm64/handle_exit.c |  31 +++
 arch/s390/kvm/arm64/handle_exit.h |   9 +
 arch/s390/kvm/arm64/reset.c       |  62 ++++++
 arch/s390/kvm/arm64/reset.h       |  11 +
 7 files changed, 529 insertions(+)
 create mode 100644 arch/s390/kvm/arm64/handle_exit.h
 create mode 100644 arch/s390/kvm/arm64/reset.c
 create mode 100644 arch/s390/kvm/arm64/reset.h
arch/s390/kvm/arm64/arm.c +344 -0
index 3a4092698fae..6ef47f7010be 100644
@@ -7,12 +7,20 @@
 #include <linux/kvm.h>
 #include <linux/kvm_types.h>
 #include <linux/kvm_host.h>
+#include <linux/fpu.h>
 
+#include <asm/access-regs.h>
+
+#include <arm64/kvm_emulate.h>
 #include <arm64/kvm_mmu.h>
+#include <arm64/sysreg.h>
 
 #include <gmap.h>
 
 #include "arm.h"
+#include "guest.h"
+#include "handle_exit.h"
+#include "reset.h"
 
 static unsigned long system_supported_vcpu_features(void);
 
@@ -201,6 +209,22 @@ void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
 {
 }
 
+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)
 {
@@ -224,12 +248,332 @@ static unsigned long system_supported_vcpu_features(void)
 	return KVM_VCPU_VALID_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;
+
+	mutex_lock(&kvm->arch.config_lock);
+	set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
+	mutex_unlock(&kvm->arch.config_lock);
+
+	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;
+	int ret = -EINVAL;
+
+	mutex_lock(&kvm->arch.config_lock);
+
+	if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) &&
+	    kvm_vcpu_init_changed(vcpu, init))
+		goto out_unlock;
+
+	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);
+
+	ret = 0;
+out_unlock:
+	mutex_unlock(&kvm->arch.config_lock);
+	return ret;
+}
+
+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;
+
+	spin_lock(&vcpu->arch.mp_state_lock);
+	WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
+	spin_unlock(&vcpu->arch.mp_state_lock);
+
+	return 0;
+}
+
 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
 			  bool line_status)
 {
 	return 0;
 }
 
+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_enter_irqoff();
+	local_irq_enable();
+
+	sae_block->icptr = 0;
+
+	sae64a(sae_block);
+
+	local_irq_disable();
+	guest_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();
+
+	ret = 1;
+	do {
+		if (signal_pending(current)) {
+			kvm_run->exit_reason = KVM_EXIT_INTR;
+			ret = -EINTR;
+			continue;
+		}
+
+		if (need_resched())
+			schedule();
+
+		if (ret > 0)
+			ret = check_vcpu_requests(vcpu);
+
+		vcpu->arch.sae_block.icptr = 0;
+
+		arm_vcpu_run(vcpu);
+
+		ret = handle_exit(vcpu);
+
+	} while (ret > 0);
+
+	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;
+}
+
+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 +67 -0

73 lines not on this page · raw message

arch/s390/kvm/arm64/guest.h +5 -0
index 31c8bc799dbe..be367df99e31 100644
@@ -5,5 +5,10 @@
 #include <linux/kvm_host.h>
 
 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
+int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
+int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
+int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
+int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
+int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
 
 #endif /* KVM_ARM_GUEST_H */
arch/s390/kvm/arm64/handle_exit.c +31 -0
index a0ebe5ffa19a..1cd19506b133 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 *);
 exit_handle_fn arm_exit_handlers[ESR_ELx_EC_MAX + 1];
 
@@ -55,6 +57,35 @@ 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 +62 -0

62 lines not on this page · raw message

arch/s390/kvm/arm64/reset.h new file +11 -0
new file mode 100644
index 000000000000..a5c5304e47bc
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef KVM_ARM_RESET_H
+#define KVM_ARM_RESET_H
+
+#include <linux/kvm_host.h>
+
+bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
+void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
+int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
+
+#endif /* KVM_ARM_RESET_H */
Signature · 1 line
2.53.0
#68 lore raw

Re: [PATCH v4 25/27] KVM: s390: arm64: Implement vCPU IOCTLs

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [High] Guest vector registers are not properly reset when KVM_REQ_VCPU_RESET is processed during the…

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] Guest vector registers are not properly reset when KVM_REQ_VCPU_RESET is processed during the KVM_RUN loop.
- [High] Userspace-reachable WARN_ONCE during validity intercept handling allows unprivileged processes to taint or panic the kernel.
- [High] Executing local_irq_enable() while in an RCU extended quiescent state (EQS) triggers lockdep warnings and system crashes.
- [High] Guest PC increment is overwritten and lost due to calling adjust_pc before vcpu_put.
- [High] Missing vcpu_load() and vcpu_put() around KVM IOCTLs modifying architectural state.
- [Medium] KVM_GET_REG_LIST ioctl returns a positive integer instead of 0 on success.
- [Medium] KVM_ARM_VCPU_FINALIZE ioctl unconditionally returns 0, failing to reject unrecognized features.
--

commit 13eb2c521ea373c254d3f7904b5979dc114e1d21
Author: Steffen Eiden <[email protected]>
Subject: KVM: s390: arm64: Implement vCPU IOCTLs

Implement all vCPU IOCTLs.

Quoted 3 lines
arch/s390/kvm/arm64/arm.c +0 -0

[ ... ]

arch/s390/kvm/arm64/arm.c static void arm_vcpu_run(struct kvm_vcpu *vcpu)

Quoted 9 lines
arch/s390/kvm/arm64/arm.c +0 -0
@@ -375,6 +375,18 @@ 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_enter_irqoff();
 	local_irq_enable();

[Severity: High]
Could enabling interrupts immediately after entering the guest context trigger an RCU-idle splat?

guest_enter_irqoff() places the CPU into an RCU extended quiescent state (EQS). Calling local_irq_enable() inside this state violates the API contract since it makes the kernel preemptible while RCU is not watching, which can lead to lockdep warnings and system stalls.

[ ... ]

Quoted 5 lines
arch/s390/kvm/arm64/arm.c +0 -0
@@ -396,6 +408,61 @@ 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;

[ ... ]

Quoted 14 lines
arch/s390/kvm/arm64/arm.c +0 -0
 	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();
 
 	ret = 1;
 	do {

[ ... ]

arch/s390/kvm/arm64/arm.c

Quoted 2 lines
arch/s390/kvm/arm64/arm.c +0 -0
 		if (ret > 0)
 			ret = check_vcpu_requests(vcpu);

[Severity: High]
If a KVM_REQ_VCPU_RESET is pending and processed by check_vcpu_requests(), the memory copy of the core registers is zeroed out by kvm_reset_vcpu(). However, the hardware CPU vector registers were already loaded before the loop via load_vx_regs().

When the loop exits, won't save_vx_regs() copy the stale hardware state back into memory, completely overriding the reset we just performed?

[ ... ]

Quoted 4 lines
arch/s390/kvm/arm64/arm.c +0 -0
 		arm_vcpu_run(vcpu);
 
 		ret = handle_exit(vcpu);
 

arch/s390/kvm/arm64/arm.c

Quoted 10 lines
arch/s390/kvm/arm64/arm.c +0 -0
 	} while (ret > 0);
 
 	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);

[Severity: High]
Does this sequence overwrite the PC increment?

adjust_pc() modifies the PC in the vCPU's memory state, but the immediate subsequent call to vcpu_put() executes the stiasrm hardware instruction. This dumps the current internal hardware register state (with the un-incremented PC) into the exact same memory save area.

Could this permanently overwrite the software increment and cause infinite guest execution loops?

[ ... ]

arch/s390/kvm/arm64/arm.c long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long ar

Quoted 8 lines
arch/s390/kvm/arm64/arm.c +0 -0
@@ -463,6 +530,95 @@ long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long ar
 {
 	struct kvm_vcpu *vcpu = filp->private_data;
 	void __user *argp = (void __user *)arg;
 	struct kvm_device_attr attr;
 	int ret;
 
 	switch (ioctl) {

[Severity: High]
Are we missing calls to vcpu_load() and vcpu_put() around these IOCTLs?

Operations like KVM_SET_ONE_REG and KVM_ARM_VCPU_INIT directly modify or read the vCPU architectural state. The KVM subsystem mandates that IOCTLs modifying architectural state must use these wrappers to manage vCPU attachment.

Without them, could this lead to hardware state synchronization issues or leaked preempt notifiers?

[ ... ]

Quoted 4 lines
arch/s390/kvm/arm64/arm.c +0 -0
 	case KVM_GET_REG_LIST: {
 		struct kvm_reg_list __user *user_list = argp;
 		struct kvm_reg_list reg_list;
 		unsigned int n;

[ ... ]

arch/s390/kvm/arm64/arm.c

Quoted 10 lines
arch/s390/kvm/arm64/arm.c +0 -0
 		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;
 	}

[Severity: Medium]
Should this explicitly return 0 on success?

kvm_arm_copy_reg_indices() returns the positive number of registers copied. Passing this positive count directly back to userspace goes against the KVM ABI requirement that this ioctl returns 0 on success.

Could this cause userspace programs like QEMU to misinterpret a successful call as an error?

[ ... ]

Quoted 3 lines
arch/s390/kvm/arm64/handle_exit.c +0 -0

[ ... ]

Quoted 7 lines
arch/s390/kvm/arm64/handle_exit.c +3 -0
@@ -55,6 +57,35 @@ exit_handle_fn arm_exit_handlers[] = {
 	[ESR_ELx_EC_HVC64]	= handle_hvc,
 };
 
+int handle_exit(struct kvm_vcpu *vcpu)
+{
+	u8 icptr = vcpu->arch.sae_block.icptr;

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

Quoted 10 lines
arch/s390/kvm/arm64/handle_exit.c +10 -0
+	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;

[Severity: High]
Is it safe to have a WARN_ONCE here?

Unprivileged host userspace can set arbitrary guest register states via KVM_SET_ONE_REG prior to calling KVM_RUN. This allows a malicious userspace process to intentionally construct an invalid CPU state that hardware rejects.

Would this allow an unprivileged process to drive this warning condition and create a host-side denial of service vector if panic_on_warn is enabled?

[ ... ]

Quoted 3 lines
arch/s390/kvm/arm64/reset.c new file +0 -0

[ ... ]

arch/s390/kvm/arm64/reset.c line 62

Quoted 4 lines
arch/s390/kvm/arm64/reset.c new file +4 -0
+int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)
+{
+	return 0;
+}

[Severity: Medium]
Should this validate the feature parameter instead of unconditionally returning 0?

The KVM API specifies that finalizing an unsupported or invalid feature must fail with -EINVAL or -EPERM. Returning 0 silently acknowledges invalid feature finalizations.

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

Re: [PATCH v4 25/27] KVM: s390: arm64: Implement vCPU IOCTLs

In reply to Steffen Eiden

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

Quoted 8 lines · Steffen Eiden
arch/s390/kvm/arm64/arm.c +7 -0
+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);
+	}
+}

Can you clarify the semantics of adjust_pc() here? arm64 also deals with exceptions in the same code, and I wonder how you deal with this.

Quoted 73 lines
arch/s390/kvm/arm64/arm.c +73 -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_enter_irqoff();
+	local_irq_enable();
+
+	sae_block->icptr = 0;
+
+	sae64a(sae_block);
+
+	local_irq_disable();
+	guest_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();
+
+	ret = 1;
+	do {
+		if (signal_pending(current)) {
+			kvm_run->exit_reason = KVM_EXIT_INTR;
+			ret = -EINTR;
+			continue;
+		}
+
+		if (need_resched())
+			schedule();
+
+		if (ret > 0)
+			ret = check_vcpu_requests(vcpu);
+
+		vcpu->arch.sae_block.icptr = 0;
+

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

Quoted 10 lines
arch/s390/kvm/arm64/arm.c +10 -0
+		arm_vcpu_run(vcpu);
+
+		ret = handle_exit(vcpu);
+
+	} while (ret > 0);
+
+	kvm_sigset_deactivate(vcpu);
+out:
+	if (unlikely(vcpu_get_flag(vcpu, INCREMENT_PC)))
+		adjust_pc(vcpu);

arm64 has the following statements:

	if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
		     vcpu_get_flag(vcpu, INCREMENT_PC)))
		kvm_call_hyp(__kvm_adjust_pc, vcpu);

It isn't clear to me why you can afford not to deal with pending exceptions when returning to userspace.

Thanks,

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

Re: [PATCH v4 25/27] KVM: s390: arm64: Implement vCPU IOCTLs

Quoted 14 lines · Marc Zyngier

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

Quoted 8 lines · Steffen Eiden
arch/s390/kvm/arm64/arm.c +7 -0
+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);
+	}
+}

Can you clarify the semantics of adjust_pc() here? arm64 also deals with exceptions in the same code, and I wonder how you deal with this.

We will do the same thing as native arm64 does. The KVM in this series has no clue what an exception is. Thus I skipped the handling here. The exception stuff is added in the second series anlongside with the sysreg handling.

...

Quoted 8 lines

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

Quoted 5 lines
arch/s390/kvm/arm64/arm.c +5 -0
+
+	kvm_sigset_deactivate(vcpu);
+out:
+	if (unlikely(vcpu_get_flag(vcpu, INCREMENT_PC)))
+		adjust_pc(vcpu);

arm64 has the following statements:

Quoted 7 lines
	if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
		     vcpu_get_flag(vcpu, INCREMENT_PC)))
		kvm_call_hyp(__kvm_adjust_pc, vcpu);

It isn't clear to me why you can afford not to deal with pending exceptions when returning to userspace.

Same reasoning like above. We'll do the exact same thing as native arm64 with the next series.

Thanks for looking into s390 code :)

Steffen

maintainer
#71 lore raw

Re: [PATCH v4 25/27] KVM: s390: arm64: Implement vCPU IOCTLs

Quoted 17 lines · Steffen Eiden
Quoted 14 lines · Marc Zyngier

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

Quoted 8 lines · Steffen Eiden
arch/s390/kvm/arm64/arm.c +7 -0
+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);
+	}
+}

Can you clarify the semantics of adjust_pc() here? arm64 also deals with exceptions in the same code, and I wonder how you deal with this.

Quoted 4 lines

We will do the same thing as native arm64 does. The KVM in this series has no clue what an exception is. Thus I skipped the handling here. The exception stuff is added in the second series anlongside with the sysreg handling.

Hmm, OK. Maybe I tend to view these things as tied together:

- updating PC on the back of taking an exception

- injecting an exception on the back of taking another exception

These two are the basic primitives to affect the guest's execution flow, and the sysreg stuff builds on top of that.

But if that's the other way around makes more sense to you, fair enough.

Quoted 15 lines

...

Quoted 12 lines

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

Quoted 5 lines
arch/s390/kvm/arm64/arm.c +5 -0
+
+	kvm_sigset_deactivate(vcpu);
+out:
+	if (unlikely(vcpu_get_flag(vcpu, INCREMENT_PC)))
+		adjust_pc(vcpu);

arm64 has the following statements:

	if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
		     vcpu_get_flag(vcpu, INCREMENT_PC)))
		kvm_call_hyp(__kvm_adjust_pc, vcpu);
Quoted 8 lines
Quoted 3 lines

It isn't clear to me why you can afford not to deal with pending exceptions when returning to userspace.

Same reasoning like above. We'll do the exact same thing as native arm64 with the next series.

Thanks for looking into s390 code :)

I have the ugly feeling that this is going to come with the territory... ;-)

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

PATCH v4 26/27 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        |  12 ++
 arch/s390/kvm/arm64/mmu.c         | 178 ++++++++++++++++++++++++++++++
 3 files changed, 192 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 1cd19506b133..96a45483e5b6 100644
@@ -55,6 +55,8 @@ static int handle_hvc(struct kvm_vcpu *vcpu)
 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 +12 -0
new file mode 100644
index 000000000000..cfddfe322196
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/kvm_host.h>
+#include <linux/bitfield.h>
+
+#include <arm64/kvm_emulate.h>
+#include <arm64/kvm_mmu.h>
+#include <arm64/sysreg.h>
+
+#define __INCL_GEN_ARM_FILE
+#include "generated/mmio.inc"
+#undef __INCL_GEN_ARM_FILE
arch/s390/kvm/arm64/mmu.c new file +178 -0

178 lines not on this page · raw message

Signature · 1 line
2.53.0
#73 lore raw

Re: [PATCH v4 26/27] KVM: s390: arm64: Implement basic page fault handler

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Guest-triggerable WARN_ONCE via KVM_BUG in kvm_handle_pic leading to host DoS - [Low] Unused…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Guest-triggerable WARN_ONCE via KVM_BUG in kvm_handle_pic leading to host DoS
- [Low] Unused parameters in user_mem_abort function signature
- [Low] Type mismatch: Assigning macro FOLL_WRITE to a boolean variable
--

commit d14fe466e599e9b62bd9a5838ffa35d7c0a359d1
Author: Steffen Eiden <[email protected]>
Subject: KVM: s390: arm64: Implement basic page fault handler

This patch adds host functionality to page in guest memory and aborts guest execution if the guest does something unexpected or illegal.

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

[ ... ]

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

Quoted 3 lines
arch/s390/kvm/arm64/mmu.c new file +3 -0
+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 here? They appear to be unused in the function body.

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

Quoted 5 lines
arch/s390/kvm/arm64/mmu.c new file +5 -0
+	struct guest_fault f = { };
+	int ret;
+
+	if (kvm_s390_cur_gmap_fault_is_write(vcpu))
+		f.write_attempt = FOLL_WRITE;

[Severity: Low]
Is it intentional to assign the FOLL_WRITE integer macro directly to the write_attempt boolean field?

Would it be clearer to assign this to true instead?

Quoted 1 line
arch/s390/kvm/arm64/mmu.c new file +1 -0
+	f.gfn = gpa_to_gfn(fault_ipa);

[ ... ]

Quoted 5 lines
arch/s390/kvm/arm64/mmu.c new file +5 -0
+static int kvm_handle_pic(struct kvm_vcpu *vcpu, bool *translation)
+{
+	switch (kvm_vcpu_fault_pic(vcpu)) {
+	/* expected cases: */
+	case PGM_ASCE_TYPE:

[ ... ]

Quoted 5 lines
arch/s390/kvm/arm64/mmu.c new file +5 -0
+	case PGM_PROTECTION:
+		break;
+	/* unexpected cases: */
+	case 0:
+		KVM_BUG(1, vcpu->kvm, "On MMU fault path but no fault occurred");

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

Quoted 10 lines
arch/s390/kvm/arm64/mmu.c new file +10 -0
+		return -EFAULT;
+	default:
+		KVM_BUG(1, vcpu->kvm, "Unexpected program interrupt 0x%x, TEID 0x%016lx",
+			vcpu->arch.sae_block.hai.pic, vcpu->arch.sae_block.hai.teid.val);
+		send_sig(SIGSEGV, current, 0);
+		return -EFAULT;
+	}
+
+	return 0;
+}

[Severity: High]
Does this allow a guest to trigger a host WARN_ONCE and potentially a kernel panic?

For faults like Alignment Faults or Synchronous External Aborts, S390 hardware might not report a DAT exception, meaning hai.pic could be 0 or an unrelated code.

Can an unprivileged guest performing an unaligned memory access hit these KVM_BUG() paths and crash the host if panic_on_warn is enabled?

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

PATCH v4 27/27 KVM: s390: arm64: Enable KVM_ARM64 config and Kbuild

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 the parameter is introduced.

---
 arch/s390/configs/defconfig  |  1 +
 arch/s390/include/asm/Kbuild |  3 ++
 arch/s390/kvm/Kconfig        |  1 +
 arch/s390/kvm/Makefile       |  1 +
 arch/s390/kvm/arm64/Kconfig  | 23 +++++++++
 arch/s390/kvm/arm64/Makefile | 94 ++++++++++++++++++++++++++++++++++++
 6 files changed, 123 insertions(+)
 create mode 100644 arch/s390/kvm/arm64/Kconfig
 create mode 100644 arch/s390/kvm/arm64/Makefile
arch/s390/configs/defconfig +1 -0
index 327ab4a3933c..20de85c96e9e 100644
@@ -62,6 +62,7 @@ CONFIG_CMM=m
 CONFIG_APPLDATA_BASE=y
 CONFIG_S390_HYPFS_FS=y
 CONFIG_KVM_S390=m
+CONFIG_KVM_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..f6fd67d1c651 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-gen-defs.h
+generated-y += kvm_arm64_modname.h
arch/s390/kvm/Kconfig +1 -0
index f8d4a9a38dae..bbae58aa8bc4 100644
@@ -20,5 +20,6 @@ config KVM
 	tristate
 
 source "arch/s390/kvm/s390/Kconfig"
+source "arch/s390/kvm/arm64/Kconfig"
 
 endif # VIRTUALIZATION
arch/s390/kvm/Makefile +1 -0
index c43d7dffca13..38bdd7c9b42d 100644
@@ -4,3 +4,4 @@
 # Copyright IBM Corp. 2008
 
 obj-$(CONFIG_KVM_S390) += s390/
+obj-$(CONFIG_KVM_ARM64) += arm64/
arch/s390/kvm/arm64/Kconfig new file +23 -0
new file mode 100644
index 000000000000..99a7fc3b71c0
@@ -0,0 +1,23 @@
+# SPDX-License-Identifier: GPL-2.0
+
+config KVM_ARM64
+	def_tristate y
+	prompt "Kernel-based Virtual Machine (KVM) support for arm64 guests"
+	depends on HAS_IOMEM
+	select KVM
+	select KVM_VFIO
+	select IRQ_BYPASS_MANAGER
+	select SCHED_INFO
+	select XARRAY_MULTI
+	select KVM_COMMON
+	select HAVE_KVM_CPU_RELAX_INTERCEPT
+	select HAVE_KVM_MSI
+	select HAVE_KVM_IRQCHIP
+	select HAVE_KVM_IRQ_ROUTING
+	select HAVE_KVM_VCPU_RUN_PID_CHANGE
+	select KVM_GENERIC_PRE_FAULT_MEMORY
+	select GUEST_PERF_EVENTS if PERF_EVENTS
+	help
+	  Support hosting virtualized arm64 guest machines on s390 host machines.
+
+	  If unsure, say N.
arch/s390/kvm/arm64/Makefile new file +94 -0

94 lines not on this page · raw message

Signature · 1 line
2.53.0
#75 lore raw

Re: [PATCH v4 27/27] KVM: s390: arm64: Enable KVM_ARM64 config and Kbuild

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Recompiling virt/kvm core code creates duplicate exported symbols, causing modpost failures or…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Recompiling virt/kvm core code creates duplicate exported symbols, causing modpost failures or type confusion.
- [High] Concurrent execution of generic KVM initialization by two separate modules will clash on singleton resources like /dev/kvm.
- [High] The Makefile uses nm -jU and objcopy -O default, which are invalid for standard GNU binutils, breaking the build.
- [High] Core KVM files compiled for the arm64 module will incorrectly include the s390 architecture headers instead of arm64 headers. --

commit 2fa0a606e2ea5038c2f6e6df6ba70d3165729ce4
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Enable KVM_ARM64 config and Kbuild

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.

Quoted 6 lines
arch/s390/kvm/arm64/Makefile new file +0 -0
new file mode 100644
index 0000000000000..566040725ab57
@@ -0,0 +1,94 @@

[ ... ]

arch/s390/kvm/arm64/Makefile line 9

Quoted 3 lines
arch/s390/kvm/arm64/Makefile new file +3 -0
+include $(src)/Makefile.gen
+
+ccflags-y += -I $(src) -I $(objtree)/$(obj) -I$(srctree)/arch/s390/kvm/gmap -I$(srctree)/arch/arm64/include -I$(srctree)/arch/arm64/kvm -DKVM_S390_ARM64

[Severity: High]
Does this successfully override the architecture include path?

Kbuild places LINUXINCLUDE (which contains -I$(srctree)/arch/$(SRCARCH)/include pointing to s390) before ccflags-y in the compiler invocation.

When the generic KVM code includes <asm/kvm_host.h>, will it incorrectly resolve to the s390 header instead of the arm64 header, leading to compilation errors or ABI mismatches?

arch/s390/kvm/arm64/Makefile line 12

Quoted 3 lines
arch/s390/kvm/arm64/Makefile new file +3 -0
+
+kvm-arm64-obj := \
+	arm.o \

[Severity: High]
Will compiling a second copy of the core KVM logic into the system duplicate static resource registrations?

The generic KVM initialization code in kvm_init() assumes it is a singleton per kernel and attempts to register the /dev/kvm misc device with a fixed minor number. If both kvm-s390 and kvm-arm64 are loaded, will the second module's initialization fail with -EBUSY when registering the misc device?

arch/s390/kvm/arm64/Makefile line 20

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

[Severity: High]
Does recompiling the virt/kvm core code here create duplicate exported symbols?

When configured as modules, compiling the generic KVM core a second time results in modpost fatal errors for exported twice symbols. When configured as built-in, objcopy --localize-symbols modifies the symbol table but fails to strip __ksymtab data sections, leaving duplicate KVM exports in vmlinux.

Could this lead to unpredictable symbol resolution for modules like vfio?

[ ... ]

arch/s390/kvm/arm64/Makefile line 73

Quoted 9 lines
arch/s390/kvm/arm64/Makefile new file +9 -0
+LDFLAGS_kvm-unnamespaced.o := -r --whole-archive
+$(obj)/kvm-unnamespaced.o: $(addprefix $(obj)/,$(kvm-arm64-obj)) FORCE
+	$(call if_changed_rule,ld_o_o)
+
+# Make list of symbols to localize.
+# Collect normal/exported symbols. Use dict as set for deduplication.
+# TODO extract do script
+quiet_cmd_nm_filter = NMFLTR  $@
+	cmd_nm_filter = $(NM) -jU $< | sort -u > $@

[Severity: High]
Are the nm flags -jU supported by standard GNU binutils?

GNU nm lacks -j and -U, which are widely used in kernel builds. Will this cause build failures when CONFIG_KVM_ARM64=y?

arch/s390/kvm/arm64/Makefile line 84

Quoted 11 lines
arch/s390/kvm/arm64/Makefile new file +11 -0
+
+$(obj)/kvm_symbol_list: $(obj)/kvm-unnamespaced.o FORCE
+	$(call if_changed,nm_filter)
+
+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 default --localize-symbols=$(obj)/kvm_symbol_list

[Severity: High]
Is the -O default flag valid for GNU objcopy?

GNU objcopy rejects default as an invalid BFD target, which may break the build when localizing these symbols.

Quoted 2 lines

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

arch/s390/kvm/arm64/Makefile new file +1 -0
+	$(call if_changed_rule,oc_o_o)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=27
#76 lore raw
In reply to Steffen Eiden

Am 06.07.26 um 10:52 schrieb Steffen Eiden:

Quoted 5 lines

KVM/s390 reorganization (patches 12-17):
Prepare s390 for a second KVM module. Move existing s390 KVM code into dedicated s390 and gmap subdirectories to make room for a second KVM implementation. Refactor gmap to better integrate with the second KVM implementation.

In the interest of making this patch set smaller, I would be willing to take these mechanical things in kvms390->next. Needs some review and polish but should be straighforward enough.