Skip to contentSkip to conversation

KVM: s390: some cleanup and small fixes

PATCH Superseded Claudio Imbrenda 5 participants 14 messages May 20, 2025 – May 27, 2025

Subsystems (5)
KVM s390 s390 MM KVM s390 +1 more subsystems

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Claudio Imbrenda

author maintainer
#0 lore raw

v2 cover KVM: s390: some cleanup and small fixes

This series has some cleanups and small fixes in preparation of the upcoming series that will finally completely move all guest page table handling into kvm. The cleaups and fixes in this series are good enough on their own, hence why they are being sent now.

v1->v2
* remove uneeded "gmap.h" include from gaccess.c (thanks Christph)
* use a custom helper instead of u64_replace_bits() (thanks Nina)
* new helper functions in priv.c to increase readability (thanks Nina)
* add lockdep assertion in handle_essa() (thanks Nina)
* gmap_helper_disable_cow_sharing() will not take the mmap lock, and must now be called while already holding the mmap lock in write mode

Claudio Imbrenda (5):
s390: remove unneeded includes
KVM: s390: remove unneeded srcu lock
KVM: s390: refactor some functions in priv.c
KVM: s390: refactor and split some gmap helpers
KVM: s390: simplify and move pv code

 MAINTAINERS                          |   2 +
 arch/s390/include/asm/gmap_helpers.h |  18 ++
 arch/s390/include/asm/tlb.h          |   1 +
 arch/s390/include/asm/uv.h           |   1 -
 arch/s390/kernel/uv.c                |  12 +-
 arch/s390/kvm/Makefile               |   2 +-
 arch/s390/kvm/diag.c                 |  11 +-
 arch/s390/kvm/gaccess.c              |   3 +-
 arch/s390/kvm/gmap-vsie.c            |   1 -
 arch/s390/kvm/gmap.c                 | 121 -----------
 arch/s390/kvm/gmap.h                 |  39 ----
 arch/s390/kvm/intercept.c            |   9 +-
 arch/s390/kvm/kvm-s390.c             |  10 +-
 arch/s390/kvm/kvm-s390.h             |  57 ++++++
 arch/s390/kvm/priv.c                 | 287 +++++++++++++--------------
 arch/s390/kvm/pv.c                   |  61 +++++-
 arch/s390/kvm/vsie.c                 |  19 +-
 arch/s390/mm/Makefile                |   2 +
 arch/s390/mm/fault.c                 |   1 -
 arch/s390/mm/gmap.c                  |  47 +----
 arch/s390/mm/gmap_helpers.c          | 259 ++++++++++++++++++++++++
 arch/s390/mm/init.c                  |   1 -
 arch/s390/mm/pgalloc.c               |   2 -
 arch/s390/mm/pgtable.c               |   1 -
 24 files changed, 591 insertions(+), 376 deletions(-)
 create mode 100644 arch/s390/include/asm/gmap_helpers.h
 delete mode 100644 arch/s390/kvm/gmap.c
 delete mode 100644 arch/s390/kvm/gmap.h
 create mode 100644 arch/s390/mm/gmap_helpers.c
Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH v2 1/5 s390: remove unneeded includes

Many files don't need to include asm/tlb.h or asm/gmap.h. On the other hand, asm/tlb.h does need to include asm/gmap.h.

Remove all unneeded includes so that asm/tlb.h is not directly used by s390 arch code anymore. Remove asm/gmap.h from a few other files as well, so that now only KVM code, mm/gmap.c, and asm/tlb.h include it.

---
 arch/s390/include/asm/tlb.h | 1 +
 arch/s390/include/asm/uv.h  | 1 -
 arch/s390/kvm/intercept.c   | 1 +
 arch/s390/mm/fault.c        | 1 -
 arch/s390/mm/gmap.c         | 1 -
 arch/s390/mm/init.c         | 1 -
 arch/s390/mm/pgalloc.c      | 2 --
 arch/s390/mm/pgtable.c      | 1 -
 8 files changed, 2 insertions(+), 7 deletions(-)
arch/s390/include/asm/tlb.h +1 -0
index f20601995bb0..56d5f9e0eb2e 100644
@@ -36,6 +36,7 @@ static inline bool __tlb_remove_folio_pages(struct mmu_gather *tlb,
 
 #include <asm/tlbflush.h>
 #include <asm-generic/tlb.h>
+#include <asm/gmap.h>
 
 /*
  * Release the page cache reference for a pte removed by
arch/s390/include/asm/uv.h +0 -1
index 46fb0ef6f984..eeb2db4783e6 100644
@@ -16,7 +16,6 @@
 #include <linux/bug.h>
 #include <linux/sched.h>
 #include <asm/page.h>
-#include <asm/gmap.h>
 #include <asm/asm.h>
 
 #define UVC_CC_OK	0
arch/s390/kvm/intercept.c +1 -0
index a06a000f196c..b4834bd4d216 100644
@@ -16,6 +16,7 @@
 #include <asm/irq.h>
 #include <asm/sysinfo.h>
 #include <asm/uv.h>
+#include <asm/gmap.h>
 
 #include "kvm-s390.h"
 #include "gaccess.h"
arch/s390/mm/fault.c +0 -1
index da84ff6770de..3829521450dd 100644
@@ -40,7 +40,6 @@
 #include <asm/ptrace.h>
 #include <asm/fault.h>
 #include <asm/diag.h>
-#include <asm/gmap.h>
 #include <asm/irq.h>
 #include <asm/facility.h>
 #include <asm/uv.h>
arch/s390/mm/gmap.c +0 -1
index a94bd4870c65..4869555ff403 100644
@@ -24,7 +24,6 @@
 #include <asm/machine.h>
 #include <asm/gmap.h>
 #include <asm/page.h>
-#include <asm/tlb.h>
 
 /*
  * The address is saved in a radix tree directly; NULL would be ambiguous,
arch/s390/mm/init.c +0 -1
index afa085e8186c..074bf4fb4ce2 100644
@@ -40,7 +40,6 @@
 #include <asm/kfence.h>
 #include <asm/dma.h>
 #include <asm/abs_lowcore.h>
-#include <asm/tlb.h>
 #include <asm/tlbflush.h>
 #include <asm/sections.h>
 #include <asm/sclp.h>
arch/s390/mm/pgalloc.c +0 -2
index e3a6f8ae156c..ddab36875370 100644
@@ -12,8 +12,6 @@
 #include <asm/mmu_context.h>
 #include <asm/page-states.h>
 #include <asm/pgalloc.h>
-#include <asm/gmap.h>
-#include <asm/tlb.h>
 #include <asm/tlbflush.h>
 
 unsigned long *crst_table_alloc(struct mm_struct *mm)
arch/s390/mm/pgtable.c +0 -1
index 9901934284ec..7df70cd8f739 100644
@@ -20,7 +20,6 @@
 #include <linux/ksm.h>
 #include <linux/mman.h>
 
-#include <asm/tlb.h>
 #include <asm/tlbflush.h>
 #include <asm/mmu_context.h>
 #include <asm/page-states.h>
Signature · 1 line
2.49.0
author maintainer
#2 lore raw

PATCH v2 2/5 KVM: s390: remove unneeded srcu lock

In reply to Claudio Imbrenda

All paths leading to handle_essa() already hold the kvm->srcu. Remove unneeded srcu locking from handle_essa().
Add lockdep assertion to make sure we will always be holding kvm->srcu when entering handle_essa().

---
 arch/s390/kvm/priv.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/s390/kvm/priv.c +2 -4
index 1a49b89706f8..9253c70897a8 100644
@@ -1248,6 +1248,8 @@ static inline int __do_essa(struct kvm_vcpu *vcpu, const int orc)
 
 static int handle_essa(struct kvm_vcpu *vcpu)
 {
+	lockdep_assert_held(&vcpu->kvm->srcu);
+
 	/* entries expected to be 1FF */
 	int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
 	unsigned long *cbrlo;
@@ -1297,12 +1299,8 @@ static int handle_essa(struct kvm_vcpu *vcpu)
 		/* Retry the ESSA instruction */
 		kvm_s390_retry_instr(vcpu);
 	} else {
-		int srcu_idx;
-
 		mmap_read_lock(vcpu->kvm->mm);
-		srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
 		i = __do_essa(vcpu, orc);
-		srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
 		mmap_read_unlock(vcpu->kvm->mm);
 		if (i < 0)
 			return i;
Signature · 1 line
2.49.0
Reviewed-by on v2 2/5
#3 lore raw

Re: [PATCH v2 2/5] KVM: s390: remove unneeded srcu lock

Quoted 8 lines · Claudio Imbrenda

All paths leading to handle_essa() already hold the kvm->srcu. Remove unneeded srcu locking from handle_essa().
Add lockdep assertion to make sure we will always be holding kvm->srcu when entering handle_essa().

Quoted 30 lines
---
 arch/s390/kvm/priv.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/s390/kvm/priv.c +2 -4
index 1a49b89706f8..9253c70897a8 100644
@@ -1248,6 +1248,8 @@ static inline int __do_essa(struct kvm_vcpu *vcpu, const int orc)
 
 static int handle_essa(struct kvm_vcpu *vcpu)
 {
+	lockdep_assert_held(&vcpu->kvm->srcu);
+
 	/* entries expected to be 1FF */
 	int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
 	unsigned long *cbrlo;
@@ -1297,12 +1299,8 @@ static int handle_essa(struct kvm_vcpu *vcpu)
 		/* Retry the ESSA instruction */
 		kvm_s390_retry_instr(vcpu);
 	} else {
-		int srcu_idx;
-
 		mmap_read_lock(vcpu->kvm->mm);
-		srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
 		i = __do_essa(vcpu, orc);
-		srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
 		mmap_read_unlock(vcpu->kvm->mm);
 		if (i < 0)
 			return i;
author maintainer
#4 lore raw

PATCH v2 3/5 KVM: s390: refactor some functions in priv.c

In reply to Claudio Imbrenda

Refactor some functions in priv.c to make them more readable.

handle_{iske,rrbe,sske}: move duplicated checks into a single function. handle{pfmf,epsw}: improve readability.
handle_lpswe{,y}: merge implementations since they are almost the same.

Use a helper function to replace open-coded bit twiddling operations.

---
 arch/s390/kvm/kvm-s390.h |  15 +++
 arch/s390/kvm/priv.c     | 281 +++++++++++++++++++--------------------
 2 files changed, 153 insertions(+), 143 deletions(-)
arch/s390/kvm/kvm-s390.h +15 -0
index 8d3bbb2dd8d2..f8c32527c4e4 100644
@@ -22,6 +22,21 @@
 
 #define KVM_S390_UCONTROL_MEMSLOT (KVM_USER_MEM_SLOTS + 0)
 
+static inline bool kvm_s390_is_amode_24(struct kvm_vcpu *vcpu)
+{
+	return psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_24BIT;
+}
+
+static inline bool kvm_s390_is_amode_31(struct kvm_vcpu *vcpu)
+{
+	return psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_31BIT;
+}
+
+static inline bool kvm_s390_is_amode_64(struct kvm_vcpu *vcpu)
+{
+	return psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT;
+}
+
 static inline void kvm_s390_fpu_store(struct kvm_run *run)
 {
 	fpu_stfpc(&run->s.regs.fpc);
arch/s390/kvm/priv.c +138 -143

452 lines not on this page · raw message

Signature · 1 line
2.49.0
maintainer
#5 lore raw

Re: [PATCH v2 3/5] KVM: s390: refactor some functions in priv.c

Quoted 9 lines · Claudio Imbrenda

Refactor some functions in priv.c to make them more readable.

handle_{iske,rrbe,sske}: move duplicated checks into a single function. handle{pfmf,epsw}: improve readability.
handle_lpswe{,y}: merge implementations since they are almost the same.

Use a helper function to replace open-coded bit twiddling operations.

...since you asked me to look into this... :)

For the sake of reviewability: I guess this really should be split into separate patches which address one function each.

Quoted 10 lines
arch/s390/kvm/priv.c +10 -0
+static inline void replace_selected_bits(u64 *w, unsigned long mask, unsigned long val)
+{
+	*w = (*w & ~mask) | (val & mask);
+}
+
+struct skeys_ops_state {
+	int reg1;
+	int reg2;
+	u64 *r1;
+	u64 *r2;

arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 275

Quoted 10 lines
arch/s390/kvm/priv.c +10 -0
+	unsigned long effective;
+	unsigned long absolute;
+};
+
+static void get_regs_rre_ptr(struct kvm_vcpu *vcpu, int *reg1, int *reg2, u64 **r1, u64 **r2)
+{
+	kvm_s390_get_regs_rre(vcpu, reg1, reg2);
+	*r1 = vcpu->run->s.regs.gprs + *reg1;
+	*r2 = vcpu->run->s.regs.gprs + *reg2;
+}

Ewww...

arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 284

Quoted 8 lines
arch/s390/kvm/priv.c +8 -0
+static int skeys_common_checks(struct kvm_vcpu *vcpu, struct skeys_ops_state *state)
+{
+	int rc;
+
+	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) {
+		rc = kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
+		return rc ? rc : -EAGAIN;
+	}

Hm.. first you introduce helper functions which use psw_bits() and now this is open-coded again?

arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 297

Quoted 12 lines
arch/s390/kvm/priv.c +12 -0
+	rc = try_handle_skey(vcpu);
+	if (rc)
+		return rc;
+
+	get_regs_rre_ptr(vcpu, &state->reg1, &state->reg2, &state->r1, &state->r2);
+
+	state->effective = vcpu->run->s.regs.gprs[state->reg2] & PAGE_MASK;
+	state->effective = kvm_s390_logical_to_effective(vcpu, state->effective);
+	state->absolute = kvm_s390_real_to_abs(vcpu, state->effective);
+
+	return 0;
+}

So a function which is called "*common_checks" actually may or may not set up a state which is later used. This is anything but obvious.

Quoted 2 lines

static int handle_iske(struct kvm_vcpu *vcpu)
{

...

arch/s390/kvm/priv.c static int handle_iske(struct kvm_vcpu *vcpu) · line 334

Quoted 3 lines
arch/s390/kvm/priv.c +1 -2
-	vcpu->run->s.regs.gprs[reg1] &= ~0xff;
-	vcpu->run->s.regs.gprs[reg1] |= key;
+	replace_selected_bits(state.r1, 0xff, key);

Who is supposed to understand that this replace_selected_bits() call actually changes vcpu->run->s.regs.gprs[reg1]? To me this obfuscates the code and makes it much less understandable.

From my point of view this state structure and passing it back and forth is a mistake, since it hides way too much what is actually going on.

Anyway, just my 0.02. :)

#6 lore raw

Re: [PATCH v2 3/5] KVM: s390: refactor some functions in priv.c

In reply to Claudio Imbrenda

On Tue May 20, 2025 at 8:26 PM CEST, Claudio Imbrenda wrote:
[...]

Quoted 4 lines
arch/s390/kvm/priv.c +0 -0
index 9253c70897a8..15843e7e57e6 100644

[...]

arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 283

Quoted 7 lines
arch/s390/kvm/priv.c +7 -0
+static int skeys_common_checks(struct kvm_vcpu *vcpu, struct skeys_ops_state *state)
+{
+	int rc;
+
+	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) {
+		rc = kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
+		return rc ? rc : -EAGAIN;

What has changed that

return kvm_s390_inject_program_int()

is not sufficient any more?

[...]

arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 292

Quoted 3 lines
arch/s390/kvm/priv.c +3 -0
+	get_regs_rre_ptr(vcpu, &state->reg1, &state->reg2, &state->r1, &state->r2);
+
+	state->effective = vcpu->run->s.regs.gprs[state->reg2] & PAGE_MASK;

*state->r2?

[...]

Quoted 2 lines

static int handle_pfmf(struct kvm_vcpu *vcpu)
{

[...]

arch/s390/kvm/priv.c static int handle_pfmf(struct kvm_vcpu *vcpu) · line 1173

Quoted 6 lines
arch/s390/kvm/priv.c +6 -0
+	if (r1.fsc) {
+		end = kvm_s390_logical_to_effective(vcpu, end);
+		if (kvm_s390_is_amode_64(vcpu))
+			replace_selected_bits(r2, PAGE_MASK, end);
+		else
+			replace_selected_bits(r2, 0xfffff000, end);

Maybe I'm missing something, but I don't get why you need replace_selected_bits here. kvm_s390_logical_to_effective() already does the neccesary masking, no?

author maintainer
#7 lore raw

Re: [PATCH v2 3/5] KVM: s390: refactor some functions in priv.c

On Tue, 27 May 2025 09:18:01 +0200
"Nico Boehr" <[email protected]> wrote:

this patch had so many issues, and brought no real advantages, so I dropped it from v3 onwards

Quoted 41 lines · Claudio Imbrenda

[...]

Quoted 4 lines
arch/s390/kvm/priv.c +0 -0
index 9253c70897a8..15843e7e57e6 100644

[...]

arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 283

Quoted 7 lines
arch/s390/kvm/priv.c +7 -0
+static int skeys_common_checks(struct kvm_vcpu *vcpu, struct skeys_ops_state *state)
+{
+	int rc;
+
+	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) {
+		rc = kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
+		return rc ? rc : -EAGAIN;  

What has changed that

return kvm_s390_inject_program_int()

is not sufficient any more?

[...]

arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 292

Quoted 3 lines
arch/s390/kvm/priv.c +3 -0
+	get_regs_rre_ptr(vcpu, &state->reg1, &state->reg2, &state->r1, &state->r2);
+
+	state->effective = vcpu->run->s.regs.gprs[state->reg2] & PAGE_MASK;  

*state->r2?

[...]

Quoted 2 lines

static int handle_pfmf(struct kvm_vcpu *vcpu)
{

[...]

arch/s390/kvm/priv.c static int handle_pfmf(struct kvm_vcpu *vcpu) · line 1173

Quoted 6 lines
arch/s390/kvm/priv.c +6 -0
+	if (r1.fsc) {
+		end = kvm_s390_logical_to_effective(vcpu, end);
+		if (kvm_s390_is_amode_64(vcpu))
+			replace_selected_bits(r2, PAGE_MASK, end);
+		else
+			replace_selected_bits(r2, 0xfffff000, end);  

Maybe I'm missing something, but I don't get why you need replace_selected_bits here. kvm_s390_logical_to_effective() already does the neccesary masking, no?

author maintainer
#8 lore raw

PATCH v2 4/5 KVM: s390: refactor and split some gmap helpers

In reply to Claudio Imbrenda

Refactor some gmap functions; move the implementation into a separate file with only helper functions. The new helper functions work on vm addresses, leaving all gmap logic in the gmap functions, which mostly become just wrappers.

The whole gmap handling is going to be moved inside KVM soon, but the helper functions need to touch core mm functions, and thus need to stay in the core of kernel.

---
 MAINTAINERS                          |   2 +
 arch/s390/include/asm/gmap_helpers.h |  18 ++
 arch/s390/kvm/diag.c                 |  11 +-
 arch/s390/kvm/kvm-s390.c             |   5 +-
 arch/s390/mm/Makefile                |   2 +
 arch/s390/mm/gmap.c                  |  46 ++---
 arch/s390/mm/gmap_helpers.c          | 259 +++++++++++++++++++++++++++
 7 files changed, 302 insertions(+), 41 deletions(-)
 create mode 100644 arch/s390/include/asm/gmap_helpers.h
 create mode 100644 arch/s390/mm/gmap_helpers.c
MAINTAINERS +2 -0
index f21f1dabb5fe..b0a8fb5a254c 100644
@@ -13093,12 +13093,14 @@ S:	Supported
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/kvms390/linux.git
 F:	Documentation/virt/kvm/s390*
 F:	arch/s390/include/asm/gmap.h
+F:	arch/s390/include/asm/gmap_helpers.h
 F:	arch/s390/include/asm/kvm*
 F:	arch/s390/include/uapi/asm/kvm*
 F:	arch/s390/include/uapi/asm/uvdevice.h
 F:	arch/s390/kernel/uv.c
 F:	arch/s390/kvm/
 F:	arch/s390/mm/gmap.c
+F:	arch/s390/mm/gmap_helpers.c
 F:	drivers/s390/char/uvdevice.c
 F:	tools/testing/selftests/drivers/s390x/uvdevice/
 F:	tools/testing/selftests/kvm/*/s390/
arch/s390/include/asm/gmap_helpers.h new file +18 -0
new file mode 100644
index 000000000000..1854fe6a8c33
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ *  Helper functions for KVM guest address space mapping code
+ *
+ *    Copyright IBM Corp. 2007, 2025
+ *    Author(s): Claudio Imbrenda <[email protected]>
+ */
+
+#ifndef _ASM_S390_GMAP_HELPERS_H
+#define _ASM_S390_GMAP_HELPERS_H
+
+void __gmap_helper_zap_one(struct mm_struct *mm, unsigned long vmaddr);
+void __gmap_helper_discard(struct mm_struct *mm, unsigned long vmaddr, unsigned long end);
+void gmap_helper_discard(struct mm_struct *mm, unsigned long vmaddr, unsigned long end);
+void __gmap_helper_set_unused(struct mm_struct *mm, unsigned long vmaddr);
+int gmap_helper_disable_cow_sharing(void);
+
+#endif /* _ASM_S390_GMAP_HELPERS_H */
arch/s390/kvm/diag.c +6 -5
index 74f73141f9b9..ce8b2f8125c4 100644
@@ -11,6 +11,7 @@
 #include <linux/kvm.h>
 #include <linux/kvm_host.h>
 #include <asm/gmap.h>
+#include <asm/gmap_helpers.h>
 #include <asm/virtio-ccw.h>
 #include "kvm-s390.h"
 #include "trace.h"
@@ -37,7 +38,7 @@ static int diag_release_pages(struct kvm_vcpu *vcpu)
 	 * fast path (no prefix swap page involved)
 	 */
 	if (end <= prefix || start >= prefix + 2 * PAGE_SIZE) {
-		gmap_discard(vcpu->arch.gmap, start, end);
+		gmap_helper_discard(vcpu->kvm->mm, start, end);
 	} else {
 		/*
 		 * This is slow path.  gmap_discard will check for start
@@ -45,12 +46,12 @@ static int diag_release_pages(struct kvm_vcpu *vcpu)
 		 * prefix and let gmap_discard make some of these calls
 		 * NOPs.
 		 */
-		gmap_discard(vcpu->arch.gmap, start, prefix);
+		gmap_helper_discard(vcpu->kvm->mm, start, prefix);
 		if (start <= prefix)
-			gmap_discard(vcpu->arch.gmap, 0, PAGE_SIZE);
+			gmap_helper_discard(vcpu->kvm->mm, 0, PAGE_SIZE);
 		if (end > prefix + PAGE_SIZE)
-			gmap_discard(vcpu->arch.gmap, PAGE_SIZE, 2 * PAGE_SIZE);
-		gmap_discard(vcpu->arch.gmap, prefix + 2 * PAGE_SIZE, end);
+			gmap_helper_discard(vcpu->kvm->mm, PAGE_SIZE, 2 * PAGE_SIZE);
+		gmap_helper_discard(vcpu->kvm->mm, prefix + 2 * PAGE_SIZE, end);
 	}
 	return 0;
 }
arch/s390/kvm/kvm-s390.c +4 -1
index 3f3175193fd7..10cfc047525d 100644
@@ -40,6 +40,7 @@
 #include <asm/machine.h>
 #include <asm/stp.h>
 #include <asm/gmap.h>
+#include <asm/gmap_helpers.h>
 #include <asm/nmi.h>
 #include <asm/isc.h>
 #include <asm/sclp.h>
@@ -2674,7 +2675,9 @@ static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)
 		if (r)
 			break;
 
-		r = s390_disable_cow_sharing();
+		mmap_write_lock(kvm->mm);
+		r = gmap_helper_disable_cow_sharing();
+		mmap_write_unlock(kvm->mm);
 		if (r)
 			break;
 
arch/s390/mm/Makefile +2 -0
index 9726b91fe7e4..bd0401cc7ca5 100644
@@ -12,3 +12,5 @@ obj-$(CONFIG_HUGETLB_PAGE)	+= hugetlbpage.o
 obj-$(CONFIG_PTDUMP)		+= dump_pagetables.o
 obj-$(CONFIG_PGSTE)		+= gmap.o
 obj-$(CONFIG_PFAULT)		+= pfault.o
+
+obj-$(subst m,y,$(CONFIG_KVM))	+= gmap_helpers.o
arch/s390/mm/gmap.c +11 -35

75 lines not on this page · raw message

arch/s390/mm/gmap_helpers.c new file +259 -0

259 lines not on this page · raw message

Signature · 1 line
2.49.0
maintainer
#9 lore raw

Re: [PATCH v2 4/5] KVM: s390: refactor and split some gmap helpers

Quoted 15 lines · Claudio Imbrenda
arch/s390/mm/gmap_helpers.c new file +15 -0
+void __gmap_helper_set_unused(struct mm_struct *mm, unsigned long vmaddr)
+{
+	spinlock_t *ptl;
+	pmd_t *pmdp;
+	pte_t *ptep;
+
+	mmap_assert_locked(mm);
+
+	if (pmd_lookup(mm, vmaddr, &pmdp))
+		return;
+	ptl = pmd_lock(mm, pmdp);
+	if (!pmd_present(*pmdp) || pmd_leaf(*pmdp)) {
+		spin_unlock(ptl);
+		return;
+	}

arch/s390/mm/gmap_helpers.c line 134

Quoted 10 lines
arch/s390/mm/gmap_helpers.c new file +10 -0
+	spin_unlock(ptl);
+
+	ptep = pte_offset_map_lock(mm, pmdp, vmaddr, &ptl);
+	if (!ptep)
+		return;
+	/* The last byte of a pte can be changed freely without ipte */
+	__atomic64_or(_PAGE_UNUSED, (long *)ptep);
+	pte_unmap_unlock(ptep, ptl);
+}
+EXPORT_SYMBOL_GPL(__gmap_helper_set_unused);

This is unused, as far as I can tell. I'm not sure if it is a good approach to do all this code movements / refactorings now. Especially if you also add (now?) dead code. I guess that e.g. this function is required for your rework that will come later?

Imho this series causes quite a bit confusion and is not about "cleanups and small fixes" like advertised.

maintainer
#10 lore raw

Re: [PATCH v2 4/5] KVM: s390: refactor and split some gmap helpers

In reply to Claudio Imbrenda
Quoted 21 lines · Claudio Imbrenda

Refactor some gmap functions; move the implementation into a separate file with only helper functions. The new helper functions work on vm addresses, leaving all gmap logic in the gmap functions, which mostly become just wrappers.

The whole gmap handling is going to be moved inside KVM soon, but the helper functions need to touch core mm functions, and thus need to stay in the core of kernel.

---
  MAINTAINERS                          |   2 +
  arch/s390/include/asm/gmap_helpers.h |  18 ++
  arch/s390/kvm/diag.c                 |  11 +-
  arch/s390/kvm/kvm-s390.c             |   5 +-
  arch/s390/mm/Makefile                |   2 +
  arch/s390/mm/gmap.c                  |  46 ++---
  arch/s390/mm/gmap_helpers.c          | 259 +++++++++++++++++++++++++++
  7 files changed, 302 insertions(+), 41 deletions(-)

create mode 100644 arch/s390/include/asm/gmap_helpers.h create mode 100644 arch/s390/mm/gmap_helpers.c

[...]

arch/s390/mm/Makefile obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o · line 16

Quoted 10 lines
arch/s390/mm/Makefile +2 -0
index 9726b91fe7e4..bd0401cc7ca5 100644
@@ -12,3 +12,5 @@ obj-$(CONFIG_HUGETLB_PAGE)	+= hugetlbpage.o
  obj-$(CONFIG_PTDUMP)		+= dump_pagetables.o
  obj-$(CONFIG_PGSTE)		+= gmap.o
  obj-$(CONFIG_PFAULT)		+= pfault.o
+
+obj-$(subst m,y,$(CONFIG_KVM))	+= gmap_helpers.o

So gmap.o depends on PGSTE but gmap_helpers.o depends on KVM. Yes, PGSTE is Y if KVM is set, but this looks really strange.

@Heiko:
Can we move away from CONFIG_PGSTE and start using CONFIG_KVM instead? Well, maybe this goes away with Claudio's rework anyway.

maintainer
#11 lore raw

Re: [PATCH v2 4/5] KVM: s390: refactor and split some gmap helpers

Quoted 10 lines · Janosch Frank

arch/s390/mm/Makefile obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o · line 16

Quoted 2 lines · Claudio Imbrenda
arch/s390/mm/Makefile +2 -0
+
+obj-$(subst m,y,$(CONFIG_KVM))	+= gmap_helpers.o

So gmap.o depends on PGSTE but gmap_helpers.o depends on KVM. Yes, PGSTE is Y if KVM is set, but this looks really strange.

@Heiko:
Can we move away from CONFIG_PGSTE and start using CONFIG_KVM instead? Well, maybe this goes away with Claudio's rework anyway.

Sure, I'd appreciate if we can get rid of config options.

author maintainer
#12 lore raw

Re: [PATCH v2 4/5] KVM: s390: refactor and split some gmap helpers

In reply to Janosch Frank
Quoted 25 lines · On Mon, 26 May 2025 13:17:35 +0200 Janosch Frank <[email protected]> wrote:
Quoted 21 lines · Claudio Imbrenda

Refactor some gmap functions; move the implementation into a separate file with only helper functions. The new helper functions work on vm addresses, leaving all gmap logic in the gmap functions, which mostly become just wrappers.

The whole gmap handling is going to be moved inside KVM soon, but the helper functions need to touch core mm functions, and thus need to stay in the core of kernel.

---
  MAINTAINERS                          |   2 +
  arch/s390/include/asm/gmap_helpers.h |  18 ++
  arch/s390/kvm/diag.c                 |  11 +-
  arch/s390/kvm/kvm-s390.c             |   5 +-
  arch/s390/mm/Makefile                |   2 +
  arch/s390/mm/gmap.c                  |  46 ++---
  arch/s390/mm/gmap_helpers.c          | 259 +++++++++++++++++++++++++++
  7 files changed, 302 insertions(+), 41 deletions(-)

create mode 100644 arch/s390/include/asm/gmap_helpers.h create mode 100644 arch/s390/mm/gmap_helpers.c

[...]

Quoted 13 lines

arch/s390/mm/Makefile obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o · line 16

Quoted 10 lines
arch/s390/mm/Makefile +2 -0
index 9726b91fe7e4..bd0401cc7ca5 100644
@@ -12,3 +12,5 @@ obj-$(CONFIG_HUGETLB_PAGE)	+= hugetlbpage.o
  obj-$(CONFIG_PTDUMP)		+= dump_pagetables.o
  obj-$(CONFIG_PGSTE)		+= gmap.o
  obj-$(CONFIG_PFAULT)		+= pfault.o
+
+obj-$(subst m,y,$(CONFIG_KVM))	+= gmap_helpers.o  

So gmap.o depends on PGSTE but gmap_helpers.o depends on KVM. Yes, PGSTE is Y if KVM is set, but this looks really strange.

yes, CONFIG_PGSTE will go away in the final series

No point in using CONFIG_PGSTE here knowing it will go away soon anyway

Quoted 5 lines

@Heiko:
Can we move away from CONFIG_PGSTE and start using CONFIG_KVM instead? Well, maybe this goes away with Claudio's rework anyway.

author maintainer
#13 lore raw

PATCH v2 5/5 KVM: s390: simplify and move pv code

In reply to Claudio Imbrenda

All functions in kvm/gmap.c fit better in kvm/pv.c instead. Move and rename them appropriately, then delete the now empty kvm/gmap.c and kvm/gmap.h.

---
 arch/s390/kernel/uv.c     |  12 ++--
 arch/s390/kvm/Makefile    |   2 +-
 arch/s390/kvm/gaccess.c   |   3 +-
 arch/s390/kvm/gmap-vsie.c |   1 -
 arch/s390/kvm/gmap.c      | 121 --------------------------------------
 arch/s390/kvm/gmap.h      |  39 ------------
 arch/s390/kvm/intercept.c |  10 +---
 arch/s390/kvm/kvm-s390.c  |   5 +-
 arch/s390/kvm/kvm-s390.h  |  42 +++++++++++++
 arch/s390/kvm/pv.c        |  61 ++++++++++++++++++-
 arch/s390/kvm/vsie.c      |  19 +++++-
 11 files changed, 133 insertions(+), 182 deletions(-)
 delete mode 100644 arch/s390/kvm/gmap.c
 delete mode 100644 arch/s390/kvm/gmap.h
arch/s390/kernel/uv.c +6 -6
index 9a5d5be8acf4..644c110287c4 100644
@@ -135,7 +135,7 @@ int uv_destroy_folio(struct folio *folio)
 {
 	int rc;
 
-	/* See gmap_make_secure(): large folios cannot be secure */
+	/* Large folios cannot be secure */
 	if (unlikely(folio_test_large(folio)))
 		return 0;
 
@@ -184,7 +184,7 @@ int uv_convert_from_secure_folio(struct folio *folio)
 {
 	int rc;
 
-	/* See gmap_make_secure(): large folios cannot be secure */
+	/* Large folios cannot be secure */
 	if (unlikely(folio_test_large(folio)))
 		return 0;
 
@@ -403,15 +403,15 @@ EXPORT_SYMBOL_GPL(make_hva_secure);
 
 /*
  * To be called with the folio locked or with an extra reference! This will
- * prevent gmap_make_secure from touching the folio concurrently. Having 2
- * parallel arch_make_folio_accessible is fine, as the UV calls will become a
- * no-op if the folio is already exported.
+ * prevent kvm_s390_pv_make_secure() from touching the folio concurrently.
+ * Having 2 parallel arch_make_folio_accessible is fine, as the UV calls will
+ * become a no-op if the folio is already exported.
  */
 int arch_make_folio_accessible(struct folio *folio)
 {
 	int rc = 0;
 
-	/* See gmap_make_secure(): large folios cannot be secure */
+	/* Large folios cannot be secure */
 	if (unlikely(folio_test_large(folio)))
 		return 0;
 
arch/s390/kvm/Makefile +1 -1
index f0ffe874adc2..9a723c48b05a 100644
@@ -8,7 +8,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 += diag.o gaccess.o guestdbg.o vsie.o pv.o gmap.o gmap-vsie.o
+kvm-y += diag.o gaccess.o guestdbg.o vsie.o pv.o gmap-vsie.o
 
 kvm-$(CONFIG_VFIO_PCI_ZDEV_KVM) += pci.o
 obj-$(CONFIG_KVM) += kvm.o
arch/s390/kvm/gaccess.c +2 -1
index f6fded15633a..e23670e1949c 100644
@@ -16,9 +16,10 @@
 #include <asm/gmap.h>
 #include <asm/dat-bits.h>
 #include "kvm-s390.h"
-#include "gmap.h"
 #include "gaccess.h"
 
+#define GMAP_SHADOW_FAKE_TABLE 1ULL
+
 /*
  * vaddress union in order to easily decode a virtual address into its
  * region first index, region second index etc. parts.
arch/s390/kvm/gmap-vsie.c +0 -1
index a6d1dbb04c97..56ef153eb8fe 100644
@@ -22,7 +22,6 @@
 #include <asm/uv.h>
 
 #include "kvm-s390.h"
-#include "gmap.h"
 
 /**
  * gmap_find_shadow - find a specific asce in the list of shadow tables
arch/s390/kvm/gmap.c deleted +0 -121

121 lines not on this page · raw message

arch/s390/kvm/gmap.h deleted +0 -39
deleted file mode 100644
index c8f031c9ea5f..000000000000
@@ -1,39 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-/*
- *  KVM guest address space mapping code
- *
- *    Copyright IBM Corp. 2007, 2016, 2025
- *    Author(s): Martin Schwidefsky <[email protected]>
- *               Claudio Imbrenda <[email protected]>
- */
-
-#ifndef ARCH_KVM_S390_GMAP_H
-#define ARCH_KVM_S390_GMAP_H
-
-#define GMAP_SHADOW_FAKE_TABLE 1ULL
-
-int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb);
-int gmap_convert_to_secure(struct gmap *gmap, unsigned long gaddr);
-int gmap_destroy_page(struct gmap *gmap, unsigned long gaddr);
-struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce, int edat_level);
-
-/**
- * gmap_shadow_valid - check if a shadow guest address space matches the
- *                     given properties and is still valid
- * @sg: pointer to the shadow guest address space structure
- * @asce: ASCE for which the shadow table is requested
- * @edat_level: edat level to be used for the shadow translation
- *
- * Returns 1 if the gmap shadow is still valid and matches the given
- * properties, the caller can continue using it. Returns 0 otherwise, the
- * caller has to request a new shadow gmap in this case.
- *
- */
-static inline int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
-{
-	if (sg->removed)
-		return 0;
-	return sg->orig_asce == asce && sg->edat_level == edat_level;
-}
-
-#endif
arch/s390/kvm/intercept.c +3 -7
index b4834bd4d216..c7908950c1f4 100644
@@ -16,13 +16,11 @@
 #include <asm/irq.h>
 #include <asm/sysinfo.h>
 #include <asm/uv.h>
-#include <asm/gmap.h>
 
 #include "kvm-s390.h"
 #include "gaccess.h"
 #include "trace.h"
 #include "trace-s390.h"
-#include "gmap.h"
 
 u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu)
 {
@@ -546,7 +544,7 @@ static int handle_pv_uvc(struct kvm_vcpu *vcpu)
 			  guest_uvcb->header.cmd);
 		return 0;
 	}
-	rc = gmap_make_secure(vcpu->arch.gmap, uvcb.gaddr, &uvcb);
+	rc = kvm_s390_pv_make_secure(vcpu->kvm, uvcb.gaddr, &uvcb);
 	/*
 	 * If the unpin did not succeed, the guest will exit again for the UVC
 	 * and we will retry the unpin.
@@ -654,10 +652,8 @@ int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
 		break;
 	case ICPT_PV_PREF:
 		rc = 0;
-		gmap_convert_to_secure(vcpu->arch.gmap,
-				       kvm_s390_get_prefix(vcpu));
-		gmap_convert_to_secure(vcpu->arch.gmap,
-				       kvm_s390_get_prefix(vcpu) + PAGE_SIZE);
+		kvm_s390_pv_convert_to_secure(vcpu->kvm, kvm_s390_get_prefix(vcpu));
+		kvm_s390_pv_convert_to_secure(vcpu->kvm, kvm_s390_get_prefix(vcpu) + PAGE_SIZE);
 		break;
 	default:
 		return -EOPNOTSUPP;
arch/s390/kvm/kvm-s390.c +2 -3
index 10cfc047525d..d5ad10791c25 100644
@@ -53,7 +53,6 @@
 #include "kvm-s390.h"
 #include "gaccess.h"
 #include "pci.h"
-#include "gmap.h"
 
 #define CREATE_TRACE_POINTS
 #include "trace.h"
@@ -4976,7 +4975,7 @@ static int vcpu_post_run_handle_fault(struct kvm_vcpu *vcpu)
 		 * previous protected guest. The old pages need to be destroyed
 		 * so the new guest can use them.
 		 */
-		if (gmap_destroy_page(vcpu->arch.gmap, gaddr)) {
+		if (kvm_s390_pv_destroy_page(vcpu->kvm, gaddr)) {
 			/*
 			 * Either KVM messed up the secure guest mapping or the
 			 * same page is mapped into multiple secure guests.
@@ -4998,7 +4997,7 @@ static int vcpu_post_run_handle_fault(struct kvm_vcpu *vcpu)
 		 * guest has not been imported yet. Try to import the page into
 		 * the protected guest.
 		 */
-		rc = gmap_convert_to_secure(vcpu->arch.gmap, gaddr);
+		rc = kvm_s390_pv_convert_to_secure(vcpu->kvm, gaddr);
 		if (rc == -EINVAL)
 			send_sig(SIGSEGV, current, 0);
 		if (rc != -ENXIO)
arch/s390/kvm/kvm-s390.h +42 -0

60 lines not on this page · raw message

arch/s390/kvm/pv.c +59 -2

79 lines not on this page · raw message

arch/s390/kvm/vsie.c +18 -1
index a78df3a4f353..13a9661d2b28 100644
@@ -23,7 +23,6 @@
 #include <asm/facility.h>
 #include "kvm-s390.h"
 #include "gaccess.h"
-#include "gmap.h"
 
 enum vsie_page_flags {
 	VSIE_PAGE_IN_USE = 0,
@@ -68,6 +67,24 @@ struct vsie_page {
 	__u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE];	/* 0x0800 */
 };
 
+/**
+ * gmap_shadow_valid() - check if a shadow guest address space matches the
+ *                       given properties and is still valid
+ * @sg: pointer to the shadow guest address space structure
+ * @asce: ASCE for which the shadow table is requested
+ * @edat_level: edat level to be used for the shadow translation
+ *
+ * Returns 1 if the gmap shadow is still valid and matches the given
+ * properties, the caller can continue using it. Returns 0 otherwise; the
+ * caller has to request a new shadow gmap in this case.
+ */
+int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
+{
+	if (sg->removed)
+		return 0;
+	return sg->orig_asce == asce && sg->edat_level == edat_level;
+}
+
 /* trigger a validity icpt for the given scb */
 static int set_validity_icpt(struct kvm_s390_sie_block *scb,
 			     __u16 reason_code)
Signature · 1 line
2.49.0