KVM: s390: some cleanup and small fixes
Claudio Imbrenda
maintainercover 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.
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 | 10 +- arch/s390/kvm/kvm-s390.c | 8 +- arch/s390/kvm/kvm-s390.h | 57 ++++++ arch/s390/kvm/priv.c | 292 ++++++++++++--------------- 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 | 266 ++++++++++++++++++++++++ arch/s390/mm/init.c | 1 - arch/s390/mm/pgalloc.c | 2 - arch/s390/mm/pgtable.c | 1 - 24 files changed, 590 insertions(+), 388 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
Claudio Imbrenda
maintainerPATCH 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [PATCH v1 1/5] s390: remove unneeded includes
You added one unnecessary import to arch/s390/kvm/intercept.c
With that removed:
Quoted 48 lines · Claudio Imbrenda
Many files don't need to include
asm/tlb.horasm/gmap.h. On the other hand,asm/tlb.hdoes need to includeasm/gmap.h.Remove all unneeded includes so that
asm/tlb.his not directly used by s390 arch code anymore. Removeasm/gmap.hfrom a few other files as well, so that now only KVM code,mm/gmap.c, andasm/tlb.hinclude it.Signed-off-by Claudio Imbrenda <[email protected]>--- 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@@ -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@@ -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 0arch/s390/kvm/intercept.c +0
-0
arch/s390/kvm/intercept.c line 19
Quoted 4 lines
arch/s390/kvm/intercept.c +1
-0@@ -16,6 +16,7 @@#include <asm/irq.h> #include <asm/sysinfo.h> #include <asm/uv.h> +#include <asm/gmap.h>
This import is not needed.
Quoted 64 lines
arch/s390/kvm/intercept.c +0
-0#include "kvm-s390.h" #include "gaccess.h" arch/s390/mm/fault.c +0
-1@@ -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@@ -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@@ -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@@ -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@@ -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>
Claudio Imbrenda
maintainerRe: [PATCH v1 1/5] s390: remove unneeded includes
Quoted 49 lines · On Thu, 15 May 2025 15:56:44 +0200 "Christoph Schlameuss"…
You added one unnecessary import to
arch/s390/kvm/intercept.cWith that removed:
Reviewed-by Christoph Schlameuss <[email protected]>Quoted 42 lines · Claudio Imbrenda
Many files don't need to include
asm/tlb.horasm/gmap.h. On the other hand,asm/tlb.hdoes need to includeasm/gmap.h.Remove all unneeded includes so that
asm/tlb.his not directly used by s390 arch code anymore. Removeasm/gmap.hfrom a few other files as well, so that now only KVM code,mm/gmap.c, andasm/tlb.hinclude it.Signed-off-by Claudio Imbrenda <[email protected]>--- 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@@ -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@@ -16,7 +16,6 @@#include <linux/bug.h> #include <linux/sched.h> #include <asm/page.h> -#include <asm/gmap.h> #include <asm/asm.h>
Quoted 12 lines
arch/s390/kvm/intercept.c line 19
Quoted 10 lines
arch/s390/include/asm/uv.h +0
-0#define UVC_CC_OK 0arch/s390/kvm/intercept.c +1
-0@@ -16,6 +16,7 @@#include <asm/irq.h> #include <asm/sysinfo.h> #include <asm/uv.h> +#include <asm/gmap.h> This import is not needed.
it is needed here, but it can be removed in patch 5 (I'll fix that for v2)
Quoted 66 lines
Quoted 65 lines
arch/s390/kvm/intercept.c +0
-0#include "kvm-s390.h" #include "gaccess.h" arch/s390/mm/fault.c +0
-1@@ -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@@ -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@@ -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@@ -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@@ -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>
Claudio Imbrenda
maintainerPATCH 2/5 KVM: s390: remove unneeded srcu lock
In reply to Claudio ImbrendaAll paths leading to handle_essa() already hold the kvm->srcu. Remove unneeded srcu locking from handle_essa().
--- arch/s390/kvm/priv.c | 4 ---- 1 file changed, 4 deletions(-)
arch/s390/kvm/priv.c +0 -4
@@ -1297,12 +1297,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
Re: [PATCH v1 2/5] KVM: s390: remove unneeded srcu lock
Am 14.05.25 um 18:38 schrieb Claudio Imbrenda:
Quoted 1 line
All paths leading to
handle_essa()already hold thekvm->srcu.
You could add
since commit 800c1065c320 ("KVM: s390: Lock kvm->srcu at the appropriate places").
Quoted 3 lines
Remove unneeded srcu locking from
handle_essa().Signed-off-by Claudio Imbrenda <[email protected]>
Quoted 21 lines
--- arch/s390/kvm/priv.c | 4 ---- 1 file changed, 4 deletions(-)arch/s390/kvm/priv.c +0
-4@@ -1297,12 +1297,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;
Re: [PATCH v1 2/5] KVM: s390: remove unneeded srcu lock
In reply to Claudio ImbrendaQuoted 4 lines · Claudio Imbrenda
All paths leading to
handle_essa()already hold thekvm->srcu. Remove unneeded srcu locking fromhandle_essa().Signed-off-by Claudio Imbrenda <[email protected]>
Why are you removing it tho?
It should be very low cost and it makes the code more robust, since handle_essa itself ensures that it has the lock.
It is also easier to understand which synchronization the function does. You could of course add a comment stating that the kvm srcu read side needs to be held. I think this would be good to have if you really don't want the srcu_read_lock here.
But then you might also want that documented up the call chain.
Quoted 21 lines
--- arch/s390/kvm/priv.c | 4 ---- 1 file changed, 4 deletions(-)arch/s390/kvm/priv.c +0
-4@@ -1297,12 +1297,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 · 4 lines
IBM Deutschland Research & Development GmbH Vorsitzender des Aufsichtsrats: Wolfgang Wendt Geschäftsführung: David Faller Sitz der Gesellschaft: Böblingen / Registergericht: Amtsgericht Stuttgart, HRB 243294
Re: [PATCH v1 2/5] KVM: s390: remove unneeded srcu lock
Quoted 8 lines · Nina Schoetterl-Glausch
Quoted 4 lines · Claudio Imbrenda
All paths leading to
handle_essa()already hold thekvm->srcu. Remove unneeded srcu locking fromhandle_essa().Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Nina Schoetterl-Glausch <[email protected]>
Quoted 8 lines
Why are you removing it tho?
It should be very low cost and it makes the code more robust, sincehandle_essaitself ensures that it has the lock.
It is also easier to understand which synchronization the function does. You could of course add a comment stating that the kvm srcu read side needs to be held. I think this would be good to have if you really don't want thesrcu_read_lockhere.
But then you might also want that documented up the call chain.
Actually, can we use __must_hold or have some assert?
Quoted 22 lines
Quoted 22 lines
--- arch/s390/kvm/priv.c | 4 ---- 1 file changed, 4 deletions(-)arch/s390/kvm/priv.c +0
-4@@ -1297,12 +1297,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 · 4 lines
IBM Deutschland Research & Development GmbH Vorsitzender des Aufsichtsrats: Wolfgang Wendt Geschäftsführung: David Faller Sitz der Gesellschaft: Böblingen / Registergericht: Amtsgericht Stuttgart, HRB 243294
Re: [PATCH v1 2/5] KVM: s390: remove unneeded srcu lock
Am 20.05.25 um 16:34 schrieb Nina Schoetterl-Glausch:
Quoted 18 lines · Nina Schoetterl-Glausch
Quoted 8 lines · Claudio Imbrenda
Quoted 4 lines
All paths leading to
handle_essa()already hold thekvm->srcu. Remove unneeded srcu locking fromhandle_essa().Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Nina Schoetterl-Glausch <[email protected]>Quoted 8 lines
Why are you removing it tho?
It should be very low cost and it makes the code more robust, sincehandle_essaitself ensures that it has the lock.
It is also easier to understand which synchronization the function does. You could of course add a comment stating that the kvm srcu read side needs to be held. I think this would be good to have if you really don't want thesrcu_read_lockhere.
But then you might also want that documented up the call chain.
Quoted 1 line
Actually, can we use
__must_holdor have some assert?
Yes, that might be the best way.
Claudio Imbrenda
maintainerPATCH 3/5 KVM: s390: refactor some functions in priv.c
In reply to Claudio ImbrendaRefactor 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 u64_replace_bits() where it makes sense.
--- arch/s390/kvm/kvm-s390.h | 15 ++ arch/s390/kvm/priv.c | 288 ++++++++++++++++++--------------------- 2 files changed, 148 insertions(+), 155 deletions(-)
arch/s390/kvm/kvm-s390.h +15 -0
@@ -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 +133 -155
463 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v1 3/5] KVM: s390: refactor some functions in priv.c
Quoted 14 lines · Claudio Imbrenda
Refactor some functions in
priv.cto 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
u64_replace_bits()where it makes sense.Signed-off-by Claudio Imbrenda <[email protected]>--- arch/s390/kvm/kvm-s390.h | 15 ++ arch/s390/kvm/priv.c | 288 ++++++++++++++++++--------------------- 2 files changed, 148 insertions(+), 155 deletions(-)
[...]
Quoted 33 lines
arch/s390/kvm/priv.c +18
-0@@ -14,6 +14,7 @@#include <linux/mm_types.h> #include <linux/pgtable.h> #include <linux/io.h> +#include <linux/bitfield.h> #include <asm/asm-offsets.h> #include <asm/facility.h> #include <asm/current.h> @@ -253,29 +254,50 @@ static int try_handle_skey(struct kvm_vcpu *vcpu)return 0 ; } +struct skeys_ops_state { +int reg1; +int reg2; +int rc; +unsigned long gaddr; +}; + +static bool skeys_common_checks (struct kvm_vcpu *vcpu,struct skeys_ops_state *state,bool abs) +{ +if (vcpu->arch.sie_block->gpsw.mask &PSW_MASK_PSTATE ) { + state->rc =kvm_s390_inject_program_int (vcpu,PGM_PRIVILEGED_OP ); +return true; + } + + state->rc =try_handle_skey (vcpu); +if (state->rc) +return true;
arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 283
Quoted 10 lines
arch/s390/kvm/priv.c +10
-0+ +kvm_s390_get_regs_rre (vcpu, &state->reg1, &state->reg2); + + state->gaddr = vcpu->run->s.regs.gprs[state->reg2] &PAGE_MASK ; + state->gaddr =kvm_s390_logical_to_effective (vcpu, state->gaddr); +if (!abs) + state->gaddr =kvm_s390_real_to_abs (vcpu, state->gaddr); + +return false; +}
I don't really like this function, IMO it makes the calling functions harder to read. If it was just a chain of checks it be fine, but with the differing control flow base on the abs parameter and the complex return value it becomes too complicated.
Quoted 6 lines
arch/s390/kvm/priv.c +3
-1+static int handle_iske (struct kvm_vcpu *vcpu) { -unsigned long gaddr, vmaddr; +struct skeys_ops_state state; +unsigned long vmaddr;
arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 267
Quoted 10 lines
arch/s390/kvm/priv.c +1
-3unsigned char key; -int reg1, reg2;bool unlocked; +u64 *r1;int rc; vcpu->stat.instruction_iske++; -if (vcpu->arch.sie_block->gpsw.mask &PSW_MASK_PSTATE ) -return kvm_s390_inject_program_int (vcpu,PGM_PRIVILEGED_OP );
How about a macro INJECT_PGM_ON: INJECT_PGM_ON(kvm_s390_problem_state(vcpu), PGM_PRIVILEGED_OP)
arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 271
Quoted 4 lines
arch/s390/kvm/priv.c +0
-4- - rc =try_handle_skey (vcpu); -if (rc) -return rc != -EAGAIN ? rc :0 ;
You are not replicating this behavior, are you?
arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 273
Quoted 2 lines
arch/s390/kvm/priv.c +0
-2- -kvm_s390_get_regs_rre (vcpu, ®1, ®2);
You could introduce a helper
void _kvm_s390_get_gpr_ptrs_rre(vcpu, u64 **reg1, u64 **reg2)
{
int r1, r2;
kvm_s390_get_regs_rre(vcpu, &r1, &r2);
*reg1 = &vcpu->run->s.regs.gprs[r1];
*reg2 = &vcpu->run->s.regs.gprs[r2];
}which would remove some clutter from the original function implementations.
Quoted 23 lines
arch/s390/kvm/priv.c +5
-6+if (skeys_common_checks (vcpu, &state, false)) +return state.rc; + r1 = vcpu->run->s.regs.gprs + state.reg1; - gaddr = vcpu->run->s.regs.gprs[reg2] &PAGE_MASK ; - gaddr =kvm_s390_logical_to_effective (vcpu, gaddr); - gaddr =kvm_s390_real_to_abs (vcpu, gaddr); - vmaddr =gfn_to_hva (vcpu->kvm,gpa_to_gfn (gaddr)); + vmaddr =gfn_to_hva (vcpu->kvm,gpa_to_gfn (state.gaddr));if (kvm_is_error_hva (vmaddr))return kvm_s390_inject_program_int (vcpu,PGM_ADDRESSING ); retry:@@ -296,33 +318,23 @@ static int handle_iske(struct kvm_vcpu *vcpu)return kvm_s390_inject_program_int (vcpu,PGM_ADDRESSING );if (rc <0 )return rc; - vcpu->run->s.regs.gprs[reg1] &= ~0xff ; - vcpu->run->s.regs.gprs[reg1] |= key; + *r1 =u64_replace_bits (*r1, key,0xff );return 0 ; }
[...]
Quoted 23 lines
retry:
arch/s390/kvm/priv.c +3
-8@@ -353,40 +365,30 @@ static int handle_rrbe(struct kvm_vcpu *vcpu)static int handle_sske (struct kvm_vcpu *vcpu) {unsigned char m3 = vcpu->arch.sie_block->ipb >>28 ; +struct skeys_ops_state state;unsigned long start, end;unsigned char key, oldkey; -int reg1, reg2; +bool nq, mr, mc, mb;bool unlocked; +u64 *r1, *r2;int rc; vcpu->stat.instruction_sske++; -if (vcpu->arch.sie_block->gpsw.mask &PSW_MASK_PSTATE ) -return kvm_s390_inject_program_int (vcpu,PGM_PRIVILEGED_OP ); - - rc =try_handle_skey (vcpu); -if (rc) -return rc != -EAGAIN ? rc :0 ; -
arch/s390/kvm/priv.c static int handle_rrbe(struct kvm_vcpu *vcpu) · line 381
Quoted 10 lines
arch/s390/kvm/priv.c +4
-6-if (!test_kvm_facility (vcpu->kvm,8 )) - m3 &= ~SSKE_MB ; -if (!test_kvm_facility (vcpu->kvm,10 )) - m3 &= ~(SSKE_MC |SSKE_MR ); -if (!test_kvm_facility (vcpu->kvm,14 )) - m3 &= ~SSKE_NQ ; + mb =test_kvm_facility (vcpu->kvm,8 ) && (m3 &SSKE_MB ); + mr =test_kvm_facility (vcpu->kvm,10 ) && (m3 &SSKE_MR ); + mc =test_kvm_facility (vcpu->kvm,10 ) && (m3 &SSKE_MC ); + nq =test_kvm_facility (vcpu->kvm,14 ) && (m3 &SSKE_NQ );
That is indeed much nicer.
Quoted 8 lines
arch/s390/kvm/priv.c +3
-3-kvm_s390_get_regs_rre (vcpu, ®1, ®2); +/* start already designates an absolute address if MB is set */ +if (skeys_common_checks (vcpu, &state, mb)) +return state.rc; - key = vcpu->run->s.regs.gprs[reg1] &0xfe ; - start = vcpu->run->s.regs.gprs[reg2] &PAGE_MASK ;
arch/s390/kvm/priv.c static int handle_rrbe(struct kvm_vcpu *vcpu) · line 388
Quoted 10 lines
arch/s390/kvm/priv.c +2
-8- start =kvm_s390_logical_to_effective (vcpu, start); -if (m3 &SSKE_MB ) { -/* start already designates an absolute address */ - end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE -1 ); - }else { - start =kvm_s390_real_to_abs (vcpu, start); - end = start +PAGE_SIZE ; - } + start = state.gaddr; + end = mb ?ALIGN (start +1 , _SEGMENT_SIZE) : start +PAGE_SIZE ;
Alternatively you could do ALIGN_DOWN(start, _SEGMENT_SIZE) + _SEGMENT_SIZE, which seems a bit easier to read, but it's really minor.
Quoted 22 lines
arch/s390/kvm/priv.c +4
-4+ r1 = vcpu->run->s.regs.gprs + state.reg1; + r2 = vcpu->run->s.regs.gprs + state.reg2; + key = *r1 &0xfe ;while (start != end) {unsigned long vmaddr =gfn_to_hva (vcpu->kvm,gpa_to_gfn (start));@@ -396,9 +398,7 @@ static int handle_sske(struct kvm_vcpu *vcpu)return kvm_s390_inject_program_int (vcpu,PGM_ADDRESSING );mmap_read_lock (current->mm); - rc =cond_set_guest_storage_key (current->mm, vmaddr, key, &oldkey, - m3 &SSKE_NQ , m3 &SSKE_MR , - m3 &SSKE_MC ); + rc =cond_set_guest_storage_key (current->mm, vmaddr, key, &oldkey, nq, mr, mc);if (rc <0 ) { rc = fixup_user_fault(current->mm, vmaddr,@@ -415,23 +415,21 @@ static int handle_sske(struct kvm_vcpu *vcpu) start +=PAGE_SIZE ; } -if (m3 & (SSKE_MC |SSKE_MR )) {
arch/s390/kvm/priv.c static int handle_sske(struct kvm_vcpu *vcpu) · line 424
Quoted 10 lines
arch/s390/kvm/priv.c +3
-3-if (m3 &SSKE_MB ) { +if (mc || mr) { +if (mb) {/* skey in reg1 is unpredictable */ kvm_s390_set_psw_cc (vcpu,3 ); }else {kvm_s390_set_psw_cc (vcpu, rc); - vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL ; - vcpu->run->s.regs.gprs[reg1] |= (u64 ) oldkey <<8 ; + *r1 =u64_replace_bits (*r1, oldkey <<8 ,0xff00 );
Uh, u64_replace_bits does the shift for you, no? So it should be u64_replace_bits(*r1, oldkey, 0xff00)
You could also do u64p_replace_bits(r1, oldkey, 0xff00) but I'd actually prefer the assignment as you do it.
Quoted 4 lines
arch/s390/kvm/priv.c +0
-2} } -if (m3 &SSKE_MB ) { -if (psw_bits (vcpu->arch.sie_block->gpsw).eaba ==PSW_BITS_AMODE_64BIT )
arch/s390/kvm/priv.c static int handle_sske(struct kvm_vcpu *vcpu) · line 432
Quoted 10 lines
arch/s390/kvm/priv.c +5
-4- vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK ; - else - vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL; +if (mb) { end =kvm_s390_logical_to_effective (vcpu, end); - vcpu->run->s.regs.gprs[reg2] |= end; +if (kvm_s390_is_amode_64 (vcpu)) + *r2 =u64_replace_bits (*r2, end,PAGE_MASK ); +else + *r2 =u64_replace_bits (*r2, end,0xfffff000 );
This does not work because of the implicit shift.
So you need to use gpa_to_gfn(end) instead.
(I think I would prefer using start instead of end, since it better shows the interruptible nature of the instruction, but start == end if we get here so ...)
Quoted 49 lines
arch/s390/kvm/priv.c +6
-27}return 0 ; }@@ -773,46 +771,28 @@ int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)return 0 ; } -static int handle_lpswe (struct kvm_vcpu *vcpu) +static int handle_lpswe_y (struct kvm_vcpu *vcpu,bool lpswey) {psw_t new_psw;u64 addr;int rc;u8 ar; - vcpu->stat.instruction_lpswe++; - -if (vcpu->arch.sie_block->gpsw.mask &PSW_MASK_PSTATE ) -return kvm_s390_inject_program_int (vcpu,PGM_PRIVILEGED_OP ); - - addr =kvm_s390_get_base_disp_s (vcpu, &ar); -if (addr &7 ) -return kvm_s390_inject_program_int (vcpu,PGM_SPECIFICATION ); - rc =read_guest (vcpu, addr, ar, &new_psw,sizeof (new_psw)); -if (rc) -return kvm_s390_inject_prog_cond (vcpu, rc); - vcpu->arch.sie_block->gpsw = new_psw; -if (!is_valid_psw (&vcpu->arch.sie_block->gpsw)) -return kvm_s390_inject_program_int (vcpu,PGM_SPECIFICATION ); -return 0 ; -} - -static int handle_lpswey (struct kvm_vcpu *vcpu) -{ -psw_t new_psw; -u64 addr; -int rc; -u8 ar; - - vcpu->stat.instruction_lpswey++; +if (lpswey) + vcpu->stat.instruction_lpswey++; +else + vcpu->stat.instruction_lpswe++; -if (!test_kvm_facility (vcpu->kvm,193 )) +if (lpswey && !test_kvm_facility (vcpu->kvm,193 ))return kvm_s390_inject_program_int (vcpu,PGM_OPERATION );
arch/s390/kvm/priv.c int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu) · line 797
Quoted 10 lines
arch/s390/kvm/priv.c +4
-1if (vcpu->arch.sie_block->gpsw.mask &PSW_MASK_PSTATE )return kvm_s390_inject_program_int (vcpu,PGM_PRIVILEGED_OP ); - addr =kvm_s390_get_base_disp_siy (vcpu, &ar); +if (!lpswey) + addr =kvm_s390_get_base_disp_s (vcpu, &ar); +else + addr =kvm_s390_get_base_disp_siy (vcpu, &ar);if (addr &7 )return kvm_s390_inject_program_int (vcpu,PGM_SPECIFICATION );
I'd prefer a helper function _do_lpswe_y_swap(struct kvm_vcpu *vcpu, gpa_t addr)
and then just
static int handle_lpswey(struct kvm_vcpu *vcpu)
{
u64 addr;
u8 ar;
vcpu->stat.instruction_lpswey++;
if (!test_kvm_facility(vcpu->kvm, 193))
return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
addr = kvm_s390_get_base_disp_siy(vcpu, &ar);
return _do_lpswe_y_swap(vcpu, addr);
}Makes it easier to read IMO because of the simpler control flow.
Quoted 23 lines
arch/s390/kvm/priv.c +4
-1@@ -1034,7 +1014,7 @@ int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)case 0xb1 :return handle_stfl (vcpu);case 0xb2 : -return handle_lpswe (vcpu); +return handle_lpswe_y (vcpu, false);default :return -EOPNOTSUPP ; }@@ -1043,42 +1023,50 @@ int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)static int handle_epsw (struct kvm_vcpu *vcpu) {int reg1, reg2; +u64 *r1, *r2; vcpu->stat.instruction_epsw++;kvm_s390_get_regs_rre (vcpu, ®1, ®2); + r1 = vcpu->run->s.regs.gprs + reg1; + r2 = vcpu->run->s.regs.gprs + reg2;/* This basically extracts the mask half of the psw. */
arch/s390/kvm/priv.c int kvm_s390_handle_b2(struct kvm_vcpu *vcpu) · line 1037
Quoted 10 lines
arch/s390/kvm/priv.c +3
-7- vcpu->run->s.regs.gprs[reg1] &=0xffffffff00000000UL ; - vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >>32 ; -if (reg2) { - vcpu->run->s.regs.gprs[reg2] &=0xffffffff00000000UL ; - vcpu->run->s.regs.gprs[reg2] |= - vcpu->arch.sie_block->gpsw.mask &0x00000000ffffffffUL ; - } + *r1 =u64_replace_bits (*r1, vcpu->arch.sie_block->gpsw.mask >>32 ,0xffffffff ); +if (reg2) + *r2 =u64_replace_bits (*r2, vcpu->arch.sie_block->gpsw.mask,0xffffffff );
LGTM although I don't hate the original implementation, which is very easy to understand compared to u64_replace_bits whose implementation is anything but.
It would be nice to make gprs a union, which I think should be fine from a backwards compatibility point of view. So:
struct kvm_sync_regs {
__u64 prefix; /* prefix register */
union {
__u64 gprs[16]; /* general purpose registers */
struct { __u32 h; __u32 l} gprs32[16];
struct { __u16 hh; __u16 hl; ...} gprs16[16];
... ...
But I don't expect you to do the refactor.
You could of course also contribute documentation to bitfield.h :)
Quoted 2 lines
arch/s390/kvm/priv.c +0
-0return 0 ; }
[...]
Quoted 2 lines
static int
handle_pfmf(struct kvm_vcpu *vcpu)
{
[...]
Quoted 6 lines
arch/s390/kvm/priv.c +0
-6-if (vcpu->run->s.regs.gprs[reg1] &PFMF_FSC ) { -if (psw_bits (vcpu->arch.sie_block->gpsw).eaba ==PSW_BITS_AMODE_64BIT ) { - vcpu->run->s.regs.gprs[reg2] = end; - }else { - vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL ; - end =kvm_s390_logical_to_effective (vcpu, end);
arch/s390/kvm/priv.c static int handle_pfmf(struct kvm_vcpu *vcpu) · line 1156
Quoted 10 lines
arch/s390/kvm/priv.c +8
-2- vcpu->run->s.regs.gprs[reg2] |= end; - } +if (r1.fsc) { +u64 *r2 = vcpu->run->s.regs.gprs + reg2; + + end =kvm_s390_logical_to_effective (vcpu, end); +if (kvm_s390_is_amode_64 (vcpu)) + *r2 =u64_replace_bits (*r2, end,PAGE_MASK ); +else + *r2 =u64_replace_bits (*r2, end,0xfffff000 );
Same issue as above regarding the shift.
Quoted 24 lines
arch/s390/kvm/priv.c +4
-3}return 0 ; }@@ -1361,8 +1338,9 @@ int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu) reg = reg1; nr_regs =0 ; do { - vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul; - vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++]; + u64 *cr = vcpu->arch.sie_block->gcr + reg; + + *cr = u64_replace_bits(*cr, ctl_array[nr_regs++], 0xffffffff); if (reg == reg3) break; reg = (reg + 1) % 16;@@ -1489,7 +1467,7 @@ int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)case 0x62 :return handle_ri (vcpu);case 0x71 : -return handle_lpswey (vcpu); +return handle_lpswe_y (vcpu, true);default :return -EOPNOTSUPP ; }
Signature · 4 lines
IBM Deutschland Research & Development GmbH Vorsitzender des Aufsichtsrats: Wolfgang Wendt Geschäftsführung: David Faller Sitz der Gesellschaft: Böblingen / Registergericht: Amtsgericht Stuttgart, HRB 243294
Claudio Imbrenda
maintainerRe: [PATCH v1 3/5] KVM: s390: refactor some functions in priv.c
Quoted 56 lines · On Tue, 20 May 2025 14:49:55 +0200 Nina Schoetterl-Glausch <[email protected]>…
Quoted 14 lines · Claudio Imbrenda
Refactor some functions in
priv.cto 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
u64_replace_bits()where it makes sense.Signed-off-by Claudio Imbrenda <[email protected]>--- arch/s390/kvm/kvm-s390.h | 15 ++ arch/s390/kvm/priv.c | 288 ++++++++++++++++++--------------------- 2 files changed, 148 insertions(+), 155 deletions(-)[...]
Quoted 39 lines
arch/s390/kvm/priv.c +24
-0@@ -14,6 +14,7 @@#include <linux/mm_types.h> #include <linux/pgtable.h> #include <linux/io.h> +#include <linux/bitfield.h> #include <asm/asm-offsets.h> #include <asm/facility.h> #include <asm/current.h> @@ -253,29 +254,50 @@ static int try_handle_skey(struct kvm_vcpu *vcpu)return 0 ; } +struct skeys_ops_state { +int reg1; +int reg2; +int rc; +unsigned long gaddr; +}; + +static bool skeys_common_checks (struct kvm_vcpu *vcpu,struct skeys_ops_state *state,bool abs) +{ +if (vcpu->arch.sie_block->gpsw.mask &PSW_MASK_PSTATE ) { + state->rc =kvm_s390_inject_program_int (vcpu,PGM_PRIVILEGED_OP ); +return true; + } + + state->rc =try_handle_skey (vcpu); +if (state->rc) +return true; + +kvm_s390_get_regs_rre (vcpu, &state->reg1, &state->reg2); + + state->gaddr = vcpu->run->s.regs.gprs[state->reg2] &PAGE_MASK ; + state->gaddr =kvm_s390_logical_to_effective (vcpu, state->gaddr); +if (!abs)
Quoted 8 lines
arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 283
Quoted 4 lines
arch/s390/kvm/priv.c +4
-0+ state->gaddr =kvm_s390_real_to_abs (vcpu, state->gaddr); + +return false; +}I don't really like this function, IMO it makes the calling functions harder to read. If it was just a chain of checks it be fine, but with the differing control flow base on the abs parameter and the complex return value it becomes too complicated.
I'll try to improve it
Quoted 13 lines
Quoted 13 lines
arch/s390/kvm/priv.c +4
-2+static int handle_iske (struct kvm_vcpu *vcpu) { -unsigned long gaddr, vmaddr; +struct skeys_ops_state state; +unsigned long vmaddr;unsigned char key; -int reg1, reg2;bool unlocked; +u64 *r1;int rc;
Quoted 6 lines
arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 267
Quoted 4 lines
arch/s390/kvm/priv.c +0
-2vcpu->stat.instruction_iske++; -if (vcpu->arch.sie_block->gpsw.mask &PSW_MASK_PSTATE ) -return kvm_s390_inject_program_int (vcpu,PGM_PRIVILEGED_OP );How about a macro
INJECT_PGM_ON:INJECT_PGM_ON(kvm_s390_problem_state(vcpu), PGM_PRIVILEGED_OP)
no, I would like to avoid hiding control flow in a macro
Quoted 8 lines
arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 271
Quoted 6 lines
arch/s390/kvm/priv.c +0
-4- - rc =try_handle_skey (vcpu); -if (rc) -return rc != -EAGAIN ? rc :0 ;You are not replicating this behavior, are you?
no, but it's fine, we can afford a useless trip to userspace literally once in the lifetime of the guest
Quoted 135 lines
arch/s390/kvm/priv.c static int try_handle_skey(struct kvm_vcpu *vcpu) · line 273
Quoted 2 lines
arch/s390/kvm/priv.c +0
-2- -kvm_s390_get_regs_rre (vcpu, ®1, ®2);You could introduce a helper
void
_kvm_s390_get_gpr_ptrs_rre(vcpu, u64 **reg1, u64 **reg2){ int r1, r2; kvm_s390_get_regs_rre(vcpu, &r1, &r2); *reg1 = &vcpu->run->s.regs.gprs[r1]; *reg2 = &vcpu->run->s.regs.gprs[r2]; }which would remove some clutter from the original function implementations.
Quoted 23 lines
arch/s390/kvm/priv.c +5
-6+if (skeys_common_checks (vcpu, &state, false)) +return state.rc; + r1 = vcpu->run->s.regs.gprs + state.reg1; - gaddr = vcpu->run->s.regs.gprs[reg2] &PAGE_MASK ; - gaddr =kvm_s390_logical_to_effective (vcpu, gaddr); - gaddr =kvm_s390_real_to_abs (vcpu, gaddr); - vmaddr =gfn_to_hva (vcpu->kvm,gpa_to_gfn (gaddr)); + vmaddr =gfn_to_hva (vcpu->kvm,gpa_to_gfn (state.gaddr));if (kvm_is_error_hva (vmaddr))return kvm_s390_inject_program_int (vcpu,PGM_ADDRESSING ); retry:@@ -296,33 +318,23 @@ static int handle_iske(struct kvm_vcpu *vcpu)return kvm_s390_inject_program_int (vcpu,PGM_ADDRESSING );if (rc <0 )return rc; - vcpu->run->s.regs.gprs[reg1] &= ~0xff ; - vcpu->run->s.regs.gprs[reg1] |= key; + *r1 =u64_replace_bits (*r1, key,0xff );return 0 ; }[...]
Quoted 23 lines
retry:
arch/s390/kvm/priv.c +3
-8@@ -353,40 +365,30 @@ static int handle_rrbe(struct kvm_vcpu *vcpu)static int handle_sske (struct kvm_vcpu *vcpu) {unsigned char m3 = vcpu->arch.sie_block->ipb >>28 ; +struct skeys_ops_state state;unsigned long start, end;unsigned char key, oldkey; -int reg1, reg2; +bool nq, mr, mc, mb;bool unlocked; +u64 *r1, *r2;int rc; vcpu->stat.instruction_sske++; -if (vcpu->arch.sie_block->gpsw.mask &PSW_MASK_PSTATE ) -return kvm_s390_inject_program_int (vcpu,PGM_PRIVILEGED_OP ); - - rc =try_handle_skey (vcpu); -if (rc) -return rc != -EAGAIN ? rc :0 ; -arch/s390/kvm/priv.c static int handle_rrbe(struct kvm_vcpu *vcpu) · line 381
Quoted 10 lines
arch/s390/kvm/priv.c +4
-6-if (!test_kvm_facility (vcpu->kvm,8 )) - m3 &= ~SSKE_MB ; -if (!test_kvm_facility (vcpu->kvm,10 )) - m3 &= ~(SSKE_MC |SSKE_MR ); -if (!test_kvm_facility (vcpu->kvm,14 )) - m3 &= ~SSKE_NQ ; + mb =test_kvm_facility (vcpu->kvm,8 ) && (m3 &SSKE_MB ); + mr =test_kvm_facility (vcpu->kvm,10 ) && (m3 &SSKE_MR ); + mc =test_kvm_facility (vcpu->kvm,10 ) && (m3 &SSKE_MC ); + nq =test_kvm_facility (vcpu->kvm,14 ) && (m3 &SSKE_NQ );That is indeed much nicer.
Quoted 8 lines
arch/s390/kvm/priv.c +3
-3-kvm_s390_get_regs_rre (vcpu, ®1, ®2); +/* start already designates an absolute address if MB is set */ +if (skeys_common_checks (vcpu, &state, mb)) +return state.rc; - key = vcpu->run->s.regs.gprs[reg1] &0xfe ; - start = vcpu->run->s.regs.gprs[reg2] &PAGE_MASK ;arch/s390/kvm/priv.c static int handle_rrbe(struct kvm_vcpu *vcpu) · line 388
Quoted 10 lines
arch/s390/kvm/priv.c +2
-8- start =kvm_s390_logical_to_effective (vcpu, start); -if (m3 &SSKE_MB ) { -/* start already designates an absolute address */ - end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE -1 ); - }else { - start =kvm_s390_real_to_abs (vcpu, start); - end = start +PAGE_SIZE ; - } + start = state.gaddr; + end = mb ?ALIGN (start +1 , _SEGMENT_SIZE) : start +PAGE_SIZE ;Alternatively you could do
ALIGN_DOWN(start, _SEGMENT_SIZE)+_SEGMENT_SIZE, which seems a bit easier to read, but it's really minor.Quoted 22 lines
arch/s390/kvm/priv.c +4
-4+ r1 = vcpu->run->s.regs.gprs + state.reg1; + r2 = vcpu->run->s.regs.gprs + state.reg2; + key = *r1 &0xfe ;while (start != end) {unsigned long vmaddr =gfn_to_hva (vcpu->kvm,gpa_to_gfn (start));@@ -396,9 +398,7 @@ static int handle_sske(struct kvm_vcpu *vcpu)return kvm_s390_inject_program_int (vcpu,PGM_ADDRESSING );mmap_read_lock (current->mm); - rc =cond_set_guest_storage_key (current->mm, vmaddr, key, &oldkey, - m3 &SSKE_NQ , m3 &SSKE_MR , - m3 &SSKE_MC ); + rc =cond_set_guest_storage_key (current->mm, vmaddr, key, &oldkey, nq, mr, mc);if (rc <0 ) { rc = fixup_user_fault(current->mm, vmaddr,@@ -415,23 +415,21 @@ static int handle_sske(struct kvm_vcpu *vcpu) start +=PAGE_SIZE ; } -if (m3 & (SSKE_MC |SSKE_MR )) {arch/s390/kvm/priv.c static int handle_sske(struct kvm_vcpu *vcpu) · line 424
Quoted 10 lines
arch/s390/kvm/priv.c +3
-3-if (m3 &SSKE_MB ) { +if (mc || mr) { +if (mb) {/* skey in reg1 is unpredictable */ kvm_s390_set_psw_cc (vcpu,3 ); }else {kvm_s390_set_psw_cc (vcpu, rc); - vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL ; - vcpu->run->s.regs.gprs[reg1] |= (u64 ) oldkey <<8 ; + *r1 =u64_replace_bits (*r1, oldkey <<8 ,0xff00 );Uh,
u64_replace_bitsdoes the shift for you, no? So it should beu64_replace_bits(*r1, oldkey, 0xff00)
Quoted 2 lines
You could also do
u64p_replace_bits(r1, oldkey, 0xff00)but I'd actually prefer the assignment as you do it.
yeahhhhhh I think I'll completely rewrite those parts using bitfields and structs / unions
Quoted 100 lines
Quoted 5 lines
arch/s390/kvm/priv.c +0
-2} } -if (m3 &SSKE_MB ) { -if (psw_bits (vcpu->arch.sie_block->gpsw).eaba ==PSW_BITS_AMODE_64BIT )arch/s390/kvm/priv.c static int handle_sske(struct kvm_vcpu *vcpu) · line 432
Quoted 10 lines
arch/s390/kvm/priv.c +5
-4- vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK ; - else - vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL; +if (mb) { end =kvm_s390_logical_to_effective (vcpu, end); - vcpu->run->s.regs.gprs[reg2] |= end; +if (kvm_s390_is_amode_64 (vcpu)) + *r2 =u64_replace_bits (*r2, end,PAGE_MASK ); +else + *r2 =u64_replace_bits (*r2, end,0xfffff000 );This does not work because of the implicit shift.
So you need to usegpa_to_gfn(end)instead.
(I think I would prefer using start instead of end, since it better shows the interruptible nature of the instruction, but start == end if we get here so ...)Quoted 49 lines
arch/s390/kvm/priv.c +6
-27}return 0 ; }@@ -773,46 +771,28 @@ int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)return 0 ; } -static int handle_lpswe (struct kvm_vcpu *vcpu) +static int handle_lpswe_y (struct kvm_vcpu *vcpu,bool lpswey) {psw_t new_psw;u64 addr;int rc;u8 ar; - vcpu->stat.instruction_lpswe++; - -if (vcpu->arch.sie_block->gpsw.mask &PSW_MASK_PSTATE ) -return kvm_s390_inject_program_int (vcpu,PGM_PRIVILEGED_OP ); - - addr =kvm_s390_get_base_disp_s (vcpu, &ar); -if (addr &7 ) -return kvm_s390_inject_program_int (vcpu,PGM_SPECIFICATION ); - rc =read_guest (vcpu, addr, ar, &new_psw,sizeof (new_psw)); -if (rc) -return kvm_s390_inject_prog_cond (vcpu, rc); - vcpu->arch.sie_block->gpsw = new_psw; -if (!is_valid_psw (&vcpu->arch.sie_block->gpsw)) -return kvm_s390_inject_program_int (vcpu,PGM_SPECIFICATION ); -return 0 ; -} - -static int handle_lpswey (struct kvm_vcpu *vcpu) -{ -psw_t new_psw; -u64 addr; -int rc; -u8 ar; - - vcpu->stat.instruction_lpswey++; +if (lpswey) + vcpu->stat.instruction_lpswey++; +else + vcpu->stat.instruction_lpswe++; -if (!test_kvm_facility (vcpu->kvm,193 )) +if (lpswey && !test_kvm_facility (vcpu->kvm,193 ))return kvm_s390_inject_program_int (vcpu,PGM_OPERATION );arch/s390/kvm/priv.c int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu) · line 797
Quoted 10 lines
arch/s390/kvm/priv.c +4
-1if (vcpu->arch.sie_block->gpsw.mask &PSW_MASK_PSTATE )return kvm_s390_inject_program_int (vcpu,PGM_PRIVILEGED_OP ); - addr =kvm_s390_get_base_disp_siy (vcpu, &ar); +if (!lpswey) + addr =kvm_s390_get_base_disp_s (vcpu, &ar); +else + addr =kvm_s390_get_base_disp_siy (vcpu, &ar);if (addr &7 )return kvm_s390_inject_program_int (vcpu,PGM_SPECIFICATION );I'd prefer a helper function
_do_lpswe_y_swap(struct kvm_vcpu *vcpu, gpa_t addr)and then just
static int
handle_lpswey(struct kvm_vcpu *vcpu){ u64 addr; u8 ar; vcpu->stat.instruction_lpswey++; if (!test_kvm_facility(vcpu->kvm, 193)) return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); addr = kvm_s390_get_base_disp_siy(vcpu, &ar); return _do_lpswe_y_swap(vcpu, addr); }
Quoted 1 line
Makes it easier to read IMO because of the simpler control flow.
hmmm you have a point
Quoted 22 lines
Quoted 22 lines
arch/s390/kvm/priv.c +4
-1@@ -1034,7 +1014,7 @@ int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)case 0xb1 :return handle_stfl (vcpu);case 0xb2 : -return handle_lpswe (vcpu); +return handle_lpswe_y (vcpu, false);default :return -EOPNOTSUPP ; }@@ -1043,42 +1023,50 @@ int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)static int handle_epsw (struct kvm_vcpu *vcpu) {int reg1, reg2; +u64 *r1, *r2; vcpu->stat.instruction_epsw++;kvm_s390_get_regs_rre (vcpu, ®1, ®2); + r1 = vcpu->run->s.regs.gprs + reg1; + r2 = vcpu->run->s.regs.gprs + reg2;
Quoted 14 lines
arch/s390/kvm/priv.c int kvm_s390_handle_b2(struct kvm_vcpu *vcpu) · line 1037
Quoted 11 lines
arch/s390/kvm/priv.c +3
-7/* This basically extracts the mask half of the psw. */ - vcpu->run->s.regs.gprs[reg1] &=0xffffffff00000000UL ; - vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >>32 ; -if (reg2) { - vcpu->run->s.regs.gprs[reg2] &=0xffffffff00000000UL ; - vcpu->run->s.regs.gprs[reg2] |= - vcpu->arch.sie_block->gpsw.mask &0x00000000ffffffffUL ; - } + *r1 =u64_replace_bits (*r1, vcpu->arch.sie_block->gpsw.mask >>32 ,0xffffffff ); +if (reg2) + *r2 =u64_replace_bits (*r2, vcpu->arch.sie_block->gpsw.mask,0xffffffff );LGTM although I don't hate the original implementation, which is very easy to understand compared to
u64_replace_bitswhose implementation is anything but.
yeah I agree
Quoted 10 lines
It would be nice to make gprs a union, which I think should be fine from a backwards compatibility point of view. So:
struct kvm_sync_regs { __u64 prefix; /* prefix register */ union { __u64 gprs[16]; /* general purpose registers */ struct { __u32 h; __u32 l} gprs32[16]; struct { __u16 hh; __u16 hl; ...} gprs16[16]; ...
Quoted 4 lines
...
But I don't expect you to do the refactor.
You could of course also contribute documentation tobitfield.h:)
ehhhhhhh
Quoted 55 lines
Quoted 3 lines
arch/s390/kvm/priv.c +0
-0return 0 ; }[...]
Quoted 2 lines
static int
handle_pfmf(struct kvm_vcpu *vcpu)
{[...]
Quoted 6 lines
arch/s390/kvm/priv.c +0
-6-if (vcpu->run->s.regs.gprs[reg1] &PFMF_FSC ) { -if (psw_bits (vcpu->arch.sie_block->gpsw).eaba ==PSW_BITS_AMODE_64BIT ) { - vcpu->run->s.regs.gprs[reg2] = end; - }else { - vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL ; - end =kvm_s390_logical_to_effective (vcpu, end);arch/s390/kvm/priv.c static int handle_pfmf(struct kvm_vcpu *vcpu) · line 1156
Quoted 10 lines
arch/s390/kvm/priv.c +8
-2- vcpu->run->s.regs.gprs[reg2] |= end; - } +if (r1.fsc) { +u64 *r2 = vcpu->run->s.regs.gprs + reg2; + + end =kvm_s390_logical_to_effective (vcpu, end); +if (kvm_s390_is_amode_64 (vcpu)) + *r2 =u64_replace_bits (*r2, end,PAGE_MASK ); +else + *r2 =u64_replace_bits (*r2, end,0xfffff000 );Same issue as above regarding the shift.
Quoted 24 lines
arch/s390/kvm/priv.c +4
-3}return 0 ; }@@ -1361,8 +1338,9 @@ int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu) reg = reg1; nr_regs =0 ; do { - vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul; - vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++]; + u64 *cr = vcpu->arch.sie_block->gcr + reg; + + *cr = u64_replace_bits(*cr, ctl_array[nr_regs++], 0xffffffff); if (reg == reg3) break; reg = (reg + 1) % 16;@@ -1489,7 +1467,7 @@ int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)case 0x62 :return handle_ri (vcpu);case 0x71 : -return handle_lpswey (vcpu); +return handle_lpswe_y (vcpu, true);default :return -EOPNOTSUPP ; }
Claudio Imbrenda
maintainerPATCH 4/5 KVM: s390: refactor and split some gmap helpers
In reply to Claudio ImbrendaRefactor 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 | 3 +- arch/s390/mm/Makefile | 2 + arch/s390/mm/gmap.c | 46 ++--- arch/s390/mm/gmap_helpers.c | 266 +++++++++++++++++++++++++++ 7 files changed, 307 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
@@ -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
@@ -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
@@ -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 +2 -1
@@ -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,7 @@ static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)if (r)break ; - r =s390_disable_cow_sharing (); + r =gmap_helper_disable_cow_sharing ();if (r)break ;
arch/s390/mm/Makefile +2 -0
@@ -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 +266 -0
266 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v1 4/5] KVM: s390: refactor and split some gmap helpers
Quoted 22 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.
Signed-off-by Claudio Imbrenda <[email protected]>--- MAINTAINERS | 2 + arch/s390/include/asm/gmap_helpers.h | 18 ++ arch/s390/kvm/diag.c | 11 +- arch/s390/kvm/kvm-s390.c | 3 +- arch/s390/mm/Makefile | 2 + arch/s390/mm/gmap.c | 46 ++--- arch/s390/mm/gmap_helpers.c | 266 +++++++++++++++++++++++++++ 7 files changed, 307 insertions(+), 41 deletions(-) create mode 100644 arch/s390/include/asm/gmap_helpers.h create mode 100644 arch/s390/mm/gmap_helpers.c
[...]
Quoted 5 lines
arch/s390/mm/gmap.c +0
-0
[...]
Quoted 5 lines
void
gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to){arch/s390/mm/gmap.c +1
-2-unsigned long gaddr, vmaddr, size; -struct vm_area_struct *vma; +unsigned long vmaddr, next, start, end;
arch/s390/mm/gmap.c EXPORT_SYMBOL(__gmap_link); · line 643
Quoted 10 lines
arch/s390/mm/gmap.c +2
-6mmap_read_lock (gmap->mm); -for (gaddr = from; gaddr < to; - gaddr = (gaddr +PMD_SIZE ) &PMD_MASK ) { -/* Find the vm address for the guest address */ - vmaddr = (unsigned long ) -radix_tree_lookup (&gmap->guest_to_host, - gaddr >>PMD_SHIFT ); +for ( ; from < to; from = next) { + next =ALIGN (from +1 ,PMD_SIZE );
I think you can use pmd_addr_end(from, to) here...
Quoted 8 lines
arch/s390/mm/gmap.c +1
-5+ vmaddr = (unsigned long )radix_tree_lookup (&gmap->guest_to_host, from >>PMD_SHIFT );if (!vmaddr)continue ; - vmaddr |= gaddr & ~PMD_MASK ; -/* Find vma in the parent mm */ - vma =find_vma (gmap->mm, vmaddr); -if (!vma) -continue ;
arch/s390/mm/gmap.c EXPORT_SYMBOL(__gmap_link); · line 648
Quoted 10 lines
arch/s390/mm/gmap.c +2
-8-/* -* We do not discard pages that are backed by -* hugetlbfs, so we don't have to refault them. -*/ -if (is_vm_hugetlb_page (vma)) -continue ; - size =min (to - gaddr,PMD_SIZE - (gaddr & ~PMD_MASK )); -zap_page_range_single (vma, vmaddr, size,NULL ); + start = vmaddr | (from & ~PMD_MASK ); + end = (vmaddr | (min (to -1 , next -1 ) & ~PMD_MASK )) +1 ;
... then simply do:
end = vmaddr + (next - from);
Quoted 17 lines
arch/s390/mm/gmap.c +1
-0+__gmap_helper_discard (gmap->mm, start, end); }mmap_read_unlock (gmap->mm); }arch/s390/mm/gmap_helpers.c new file +7
-0@@ -0,0 +1,266 @@ +// 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]> +* Martin Schwidefsky <[email protected]>
arch/s390/mm/gmap_helpers.c line 17
Quoted 10 lines
arch/s390/mm/gmap_helpers.c new file +10
-0+* David Hildenbrand <[email protected]> +* Janosch Frank <[email protected]> +*/ +#include <linux/mm_types.h> +#include <linux/mmap_lock.h> +#include <linux/mm.h> +#include <linux/hugetlb.h> +#include <linux/pagewalk.h> +#include <linux/ksm.h> +#include <asm/gmap_helpers.h>
Please add documentation to all these functions for those of us that don't live and breath mm code :)
arch/s390/mm/gmap_helpers.c line 28
Quoted 11 lines
arch/s390/mm/gmap_helpers.c new file +11
-0+ +static void ptep_zap_swap_entry (struct mm_struct *mm,swp_entry_t entry) +{ +if (!non_swap_entry (entry)) +dec_mm_counter (mm,MM_SWAPENTS ); +else if (is_migration_entry (entry)) +dec_mm_counter (mm,mm_counter (pfn_swap_entry_folio (entry))); +free_swap_and_cache (entry); +} + +void __gmap_helper_zap_one (struct mm_struct *mm,unsigned long vmaddr)
__gmap_helper_zap_mapping_pte ?
Quoted 5 lines
arch/s390/mm/gmap_helpers.c new file +5
-0+{ +struct vm_area_struct *vma; +spinlock_t *ptl; +pte_t *ptep; +
arch/s390/mm/gmap_helpers.c line 43
Quoted 10 lines
arch/s390/mm/gmap_helpers.c new file +10
-0+mmap_assert_locked (mm); + +/* Find the vm address for the guest address */ + vma =vma_lookup (mm, vmaddr); +if (!vma ||is_vm_hugetlb_page (vma)) +return ; + +/* Get pointer to the page table entry */ + ptep =get_locked_pte (mm, vmaddr, &ptl); +if (!likely (ptep))
if (unlikely(!ptep)) reads nicer to me.
arch/s390/mm/gmap_helpers.c line 49
Quoted 6 lines
arch/s390/mm/gmap_helpers.c new file +6
-0+return ; +if (pte_swap (*ptep)) +ptep_zap_swap_entry (mm,pte_to_swp_entry (*ptep)); +pte_unmap_unlock (ptep, ptl); +} +EXPORT_SYMBOL_GPL (__gmap_helper_zap_one);
Looks reasonable, but I'm not well versed enough in mm code to evaluate that with confidence.
arch/s390/mm/gmap_helpers.c line 51
Quoted 2 lines
arch/s390/mm/gmap_helpers.c new file +2
-0+ +void __gmap_helper_discard (struct mm_struct *mm,unsigned long vmaddr,unsigned long end)
Maybe call this gmap_helper_discard_nolock or something.
Quoted 31 lines
arch/s390/mm/gmap_helpers.c new file +31
-0+{ +struct vm_area_struct *vma; +unsigned long next; + +mmap_assert_locked (mm); + +while (vmaddr < end) { + vma =find_vma_intersection (mm, vmaddr, end); +if (!vma) +break ; + vmaddr =max (vmaddr, vma->vm_start); + next =min (end, vma->vm_end); +if (!is_vm_hugetlb_page (vma)) +zap_page_range_single (vma, vmaddr, next - vmaddr,NULL ); + vmaddr = next; + } +} + +void gmap_helper_discard (struct mm_struct *mm,unsigned long vmaddr,unsigned long end) +{ +mmap_read_lock (mm); +__gmap_helper_discard (mm, vmaddr, end); +mmap_read_unlock (mm); +} +EXPORT_SYMBOL_GPL (gmap_helper_discard); + +static int pmd_lookup (struct mm_struct *mm,unsigned long addr,pmd_t **pmdp) +{ +struct vm_area_struct *vma; +pgd_t *pgd; +p4d_t *p4d;
arch/s390/mm/gmap_helpers.c line 92
Quoted 10 lines
arch/s390/mm/gmap_helpers.c new file +10
-0+pud_t *pud; + +/* We need a valid VMA, otherwise this is clearly a fault. */ + vma =vma_lookup (mm, addr); +if (!vma) +return -EFAULT ; + + pgd =pgd_offset (mm, addr); +if (!pgd_present (*pgd)) +return -ENOENT ;
What about pgd_bad?
Quoted 8 lines
arch/s390/mm/gmap_helpers.c new file +8
-0+ + p4d =p4d_offset (pgd, addr); +if (!p4d_present (*p4d)) +return -ENOENT ; + + pud =pud_offset (p4d, addr); +if (!pud_present (*pud)) +return -ENOENT ;
arch/s390/mm/gmap_helpers.c line 110
Quoted 10 lines
arch/s390/mm/gmap_helpers.c new file +10
-0+ +/* Large PUDs are not supported yet. */ +if (pud_leaf (*pud)) +return -EFAULT ; + + *pmdp =pmd_offset (pud, addr); +return 0 ; +} + +void __gmap_helper_set_unused (struct mm_struct *mm,unsigned long vmaddr)
What is this function for? Why do you introduce it now?
Quoted 14 lines
arch/s390/mm/gmap_helpers.c new file +14
-0+{ +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);
The stuff below is from arch/s390/mm/gmap.c right? Are you going to delete it from there?
Quoted 3 lines
arch/s390/mm/gmap_helpers.c new file +3
-0+static int find_zeropage_pte_entry (pte_t *pte,unsigned long addr, +unsigned long end,struct mm_walk *walk) +{
[...]
Quoted 23 lines
arch/s390/mm/gmap_helpers.c new file +23
-0+} + +static const struct mm_walk_ops find_zeropage_ops = { + .pte_entry = find_zeropage_pte_entry, + .walk_lock =PGWALK_WRLOCK , +}; + +/* +* Unshare all shared zeropages, replacing them by anonymous pages. Note that +* we cannot simply zap all shared zeropages, because this could later +* trigger unexpected userfaultfd missing events. +* +* This must be called after mm->context.allow_cow_sharing was +* set to 0, to avoid future mappings of shared zeropages. +* +* mm contracts with s390, that even if mm were to remove a page table, +* and racing with walk_page_range_vma() calling pte_offset_map_lock() +* would fail, it will never insert a page table containing empty zero +* pages once mm_forbids_zeropage(mm) i.e. +* mm->context.allow_cow_sharing is set to 0. +*/ +static int __gmap_helper_unshare_zeropages (struct mm_struct *mm) +{
[...]
Quoted 4 lines
arch/s390/mm/gmap_helpers.c new file +4
-0+} + +static int __gmap_helper_disable_cow_sharing (struct mm_struct *mm) +{
[...]
Quoted 12 lines
arch/s390/mm/gmap_helpers.c new file +12
-0+} + +/* +* Disable most COW-sharing of memory pages for the whole process: +* (1) Disable KSM and unmerge/unshare any KSM pages. +* (2) Disallow shared zeropages and unshare any zerpages that are mapped. +* +* Not that we currently don't bother with COW-shared pages that are shared +* with parent/child processes due to fork(). +*/ +int gmap_helper_disable_cow_sharing (void ) +{
[...]
Quoted 2 lines
arch/s390/mm/gmap_helpers.c new file +2
-0+} +EXPORT_SYMBOL_GPL (gmap_helper_disable_cow_sharing);
Signature · 4 lines
IBM Deutschland Research & Development GmbH Vorsitzender des Aufsichtsrats: Wolfgang Wendt Geschäftsführung: David Faller Sitz der Gesellschaft: Böblingen / Registergericht: Amtsgericht Stuttgart, HRB 243294
Claudio Imbrenda
maintainerRe: [PATCH v1 4/5] KVM: s390: refactor and split some gmap helpers
Quoted 38 lines · On Wed, 21 May 2025 16:55:18 +0200 Nina Schoetterl-Glausch <[email protected]>…
Quoted 22 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.
Signed-off-by Claudio Imbrenda <[email protected]>--- MAINTAINERS | 2 + arch/s390/include/asm/gmap_helpers.h | 18 ++ arch/s390/kvm/diag.c | 11 +- arch/s390/kvm/kvm-s390.c | 3 +- arch/s390/mm/Makefile | 2 + arch/s390/mm/gmap.c | 46 ++--- arch/s390/mm/gmap_helpers.c | 266 +++++++++++++++++++++++++++ 7 files changed, 307 insertions(+), 41 deletions(-) create mode 100644 arch/s390/include/asm/gmap_helpers.h create mode 100644 arch/s390/mm/gmap_helpers.c[...]
Quoted 5 lines
arch/s390/mm/gmap.c +0
-0[...]
Quoted 6 lines
void
gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to){arch/s390/mm/gmap.c +1
-2-unsigned long gaddr, vmaddr, size; -struct vm_area_struct *vma; +unsigned long vmaddr, next, start, end;
Quoted 11 lines
arch/s390/mm/gmap.c EXPORT_SYMBOL(__gmap_link); · line 643
Quoted 9 lines
arch/s390/mm/gmap.c +2
-6mmap_read_lock (gmap->mm); -for (gaddr = from; gaddr < to; - gaddr = (gaddr +PMD_SIZE ) &PMD_MASK ) { -/* Find the vm address for the guest address */ - vmaddr = (unsigned long ) -radix_tree_lookup (&gmap->guest_to_host, - gaddr >>PMD_SHIFT ); +for ( ; from < to; from = next) { + next =ALIGN (from +1 ,PMD_SIZE );I think you can use
pmd_addr_end(from, to)here...
I guess
Quoted 21 lines
Quoted 9 lines
arch/s390/mm/gmap.c +1
-5+ vmaddr = (unsigned long )radix_tree_lookup (&gmap->guest_to_host, from >>PMD_SHIFT );if (!vmaddr)continue ; - vmaddr |= gaddr & ~PMD_MASK ; -/* Find vma in the parent mm */ - vma =find_vma (gmap->mm, vmaddr); -if (!vma) -continue ;arch/s390/mm/gmap.c EXPORT_SYMBOL(__gmap_link); · line 648
Quoted 10 lines
arch/s390/mm/gmap.c +2
-8-/* -* We do not discard pages that are backed by -* hugetlbfs, so we don't have to refault them. -*/ -if (is_vm_hugetlb_page (vma)) -continue ; - size =min (to - gaddr,PMD_SIZE - (gaddr & ~PMD_MASK )); -zap_page_range_single (vma, vmaddr, size,NULL ); + start = vmaddr | (from & ~PMD_MASK ); + end = (vmaddr | (min (to -1 , next -1 ) & ~PMD_MASK )) +1 ;... then simply do:
Quoted 1 line
end = vmaddr + (next - from);
looks cleaner, yes
Quoted 23 lines
Quoted 23 lines
arch/s390/mm/gmap.c +1
-0+__gmap_helper_discard (gmap->mm, start, end); }mmap_read_unlock (gmap->mm); }arch/s390/mm/gmap_helpers.c new file +12
-0@@ -0,0 +1,266 @@ +// 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]> +* Martin Schwidefsky <[email protected]> +* David Hildenbrand <[email protected]> +* Janosch Frank <[email protected]> +*/ +#include <linux/mm_types.h> +#include <linux/mmap_lock.h>
Quoted 8 lines
arch/s390/mm/gmap_helpers.c line 17
Quoted 5 lines
arch/s390/mm/gmap_helpers.c new file +5
-0+#include <linux/mm.h> +#include <linux/hugetlb.h> +#include <linux/pagewalk.h> +#include <linux/ksm.h> +#include <asm/gmap_helpers.h> Please add documentation to all these functions for those of us that don't live and breath mm code :)
eh... yes I think it's a good idea
Quoted 14 lines
arch/s390/mm/gmap_helpers.c line 28
Quoted 12 lines
arch/s390/mm/gmap_helpers.c new file +11
-0+ +static void ptep_zap_swap_entry (struct mm_struct *mm,swp_entry_t entry) +{ +if (!non_swap_entry (entry)) +dec_mm_counter (mm,MM_SWAPENTS ); +else if (is_migration_entry (entry)) +dec_mm_counter (mm,mm_counter (pfn_swap_entry_folio (entry))); +free_swap_and_cache (entry); +} + +void __gmap_helper_zap_one (struct mm_struct *mm,unsigned long vmaddr)
__gmap_helper_zap_mapping_pte?
but I'm not taking a pte as parameter
Quoted 10 lines
Quoted 10 lines
arch/s390/mm/gmap_helpers.c new file +9
-0+{ +struct vm_area_struct *vma; +spinlock_t *ptl; +pte_t *ptep; + +mmap_assert_locked (mm); + +/* Find the vm address for the guest address */ + vma =vma_lookup (mm, vmaddr);
Quoted 8 lines
arch/s390/mm/gmap_helpers.c line 43
Quoted 6 lines
arch/s390/mm/gmap_helpers.c new file +6
-0+if (!vma ||is_vm_hugetlb_page (vma)) +return ; + +/* Get pointer to the page table entry */ + ptep =get_locked_pte (mm, vmaddr, &ptl); +if (!likely (ptep))if (unlikely(!ptep)) reads nicer to me.
ok
Quoted 11 lines
arch/s390/mm/gmap_helpers.c line 49
Quoted 7 lines
arch/s390/mm/gmap_helpers.c new file +6
-0+return ; +if (pte_swap (*ptep)) +ptep_zap_swap_entry (mm,pte_to_swp_entry (*ptep)); +pte_unmap_unlock (ptep, ptl); +} +EXPORT_SYMBOL_GPL (__gmap_helper_zap_one);Looks reasonable, but I'm not well versed enough in mm code to evaluate that with confidence.
Quoted 4 lines
arch/s390/mm/gmap_helpers.c line 51
Quoted 2 lines
arch/s390/mm/gmap_helpers.c new file +2
-0+ +void __gmap_helper_discard (struct mm_struct *mm,unsigned long vmaddr,unsigned long end)Maybe call this
gmap_helper_discard_nolockor something.
maybe __gmap_helper_discard_unlocked?
the __ prefix often implies lack of locking
Quoted 36 lines
Quoted 36 lines
arch/s390/mm/gmap_helpers.c new file +35
-0+{ +struct vm_area_struct *vma; +unsigned long next; + +mmap_assert_locked (mm); + +while (vmaddr < end) { + vma =find_vma_intersection (mm, vmaddr, end); +if (!vma) +break ; + vmaddr =max (vmaddr, vma->vm_start); + next =min (end, vma->vm_end); +if (!is_vm_hugetlb_page (vma)) +zap_page_range_single (vma, vmaddr, next - vmaddr,NULL ); + vmaddr = next; + } +} + +void gmap_helper_discard (struct mm_struct *mm,unsigned long vmaddr,unsigned long end) +{ +mmap_read_lock (mm); +__gmap_helper_discard (mm, vmaddr, end); +mmap_read_unlock (mm); +} +EXPORT_SYMBOL_GPL (gmap_helper_discard); + +static int pmd_lookup (struct mm_struct *mm,unsigned long addr,pmd_t **pmdp) +{ +struct vm_area_struct *vma; +pgd_t *pgd; +p4d_t *p4d; +pud_t *pud; + +/* We need a valid VMA, otherwise this is clearly a fault. */ + vma =vma_lookup (mm, addr);
Quoted 8 lines
arch/s390/mm/gmap_helpers.c line 92
Quoted 6 lines
arch/s390/mm/gmap_helpers.c new file +6
-0+if (!vma) +return -EFAULT ; + + pgd =pgd_offset (mm, addr); +if (!pgd_present (*pgd)) +return -ENOENT ;What about
pgd_bad?
I don't know, this code is copied verbatim from mm/gmap.c
Quoted 13 lines
Quoted 13 lines
arch/s390/mm/gmap_helpers.c new file +12
-0+ + p4d =p4d_offset (pgd, addr); +if (!p4d_present (*p4d)) +return -ENOENT ; + + pud =pud_offset (p4d, addr); +if (!pud_present (*pud)) +return -ENOENT ; + +/* Large PUDs are not supported yet. */ +if (pud_leaf (*pud)) +return -EFAULT ;
Quoted 8 lines
arch/s390/mm/gmap_helpers.c line 110
Quoted 6 lines
arch/s390/mm/gmap_helpers.c new file +6
-0+ + *pmdp =pmd_offset (pud, addr); +return 0 ; +} + +void __gmap_helper_set_unused (struct mm_struct *mm,unsigned long vmaddr)What is this function for? Why do you introduce it now?
this is for cmma handling, I'm introducing it now because it needs to be in this file and I would like to put all the stuff in at once. I will not need to touch this file anymore if I get this in now.
Quoted 20 lines
Quoted 20 lines
arch/s390/mm/gmap_helpers.c new file +19
-0+{ +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 ; + } +spin_unlock (ptl); + + ptep =pte_offset_map_lock (mm, pmdp, vmaddr, &ptl); +if (!ptep) +return ;
Quoted 8 lines
arch/s390/mm/gmap_helpers.c line 134
Quoted 5 lines
arch/s390/mm/gmap_helpers.c new file +5
-0+/* 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);The stuff below is from
arch/s390/mm/gmap.cright? Are you going to delete it from there?
not in this series, but the next series will remove mm/gmap.c altogether
Quoted 58 lines
Quoted 4 lines
arch/s390/mm/gmap_helpers.c new file +3
-0+static int find_zeropage_pte_entry (pte_t *pte,unsigned long addr, +unsigned long end,struct mm_walk *walk) +{[...]
Quoted 23 lines
arch/s390/mm/gmap_helpers.c new file +23
-0+} + +static const struct mm_walk_ops find_zeropage_ops = { + .pte_entry = find_zeropage_pte_entry, + .walk_lock =PGWALK_WRLOCK , +}; + +/* +* Unshare all shared zeropages, replacing them by anonymous pages. Note that +* we cannot simply zap all shared zeropages, because this could later +* trigger unexpected userfaultfd missing events. +* +* This must be called after mm->context.allow_cow_sharing was +* set to 0, to avoid future mappings of shared zeropages. +* +* mm contracts with s390, that even if mm were to remove a page table, +* and racing with walk_page_range_vma() calling pte_offset_map_lock() +* would fail, it will never insert a page table containing empty zero +* pages once mm_forbids_zeropage(mm) i.e. +* mm->context.allow_cow_sharing is set to 0. +*/ +static int __gmap_helper_unshare_zeropages (struct mm_struct *mm) +{[...]
Quoted 4 lines
arch/s390/mm/gmap_helpers.c new file +4
-0+} + +static int __gmap_helper_disable_cow_sharing (struct mm_struct *mm) +{[...]
Quoted 12 lines
arch/s390/mm/gmap_helpers.c new file +12
-0+} + +/* +* Disable most COW-sharing of memory pages for the whole process: +* (1) Disable KSM and unmerge/unshare any KSM pages. +* (2) Disallow shared zeropages and unshare any zerpages that are mapped. +* +* Not that we currently don't bother with COW-shared pages that are shared +* with parent/child processes due to fork(). +*/ +int gmap_helper_disable_cow_sharing (void ) +{[...]
Quoted 2 lines
arch/s390/mm/gmap_helpers.c new file +2
-0+} +EXPORT_SYMBOL_GPL (gmap_helper_disable_cow_sharing);
Re: [PATCH v1 4/5] KVM: s390: refactor and split some gmap helpers
Quoted 26 lines · Claudio Imbrenda
Quoted 23 lines · On Wed, 21 May 2025 16:55:18 +0200 Nina Schoetterl-Glausch <[email protected]>…
Quoted 22 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.
Signed-off-by Claudio Imbrenda <[email protected]>--- MAINTAINERS | 2 + arch/s390/include/asm/gmap_helpers.h | 18 ++ arch/s390/kvm/diag.c | 11 +- arch/s390/kvm/kvm-s390.c | 3 +- arch/s390/mm/Makefile | 2 + arch/s390/mm/gmap.c | 46 ++--- arch/s390/mm/gmap_helpers.c | 266 +++++++++++++++++++++++++++ 7 files changed, 307 insertions(+), 41 deletions(-) create mode 100644 arch/s390/include/asm/gmap_helpers.h create mode 100644 arch/s390/mm/gmap_helpers.c
[...]
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
+void
__gmap_helper_zap_one(struct mm_struct *mm, unsigned long vmaddr)
__gmap_helper_zap_mapping_pte?but I'm not taking a pte as parameter
The pte being zapped is the one mapping vmaddr, right?
Quoted 33 lines
Quoted 11 lines
Quoted 11 lines
arch/s390/mm/gmap_helpers.c new file +9
-0+{ +struct vm_area_struct *vma; +spinlock_t *ptl; +pte_t *ptep; + +mmap_assert_locked (mm); + +/* Find the vm address for the guest address */ + vma =vma_lookup (mm, vmaddr);Quoted 8 lines
arch/s390/mm/gmap_helpers.c line 43
Quoted 6 lines
arch/s390/mm/gmap_helpers.c new file +6
-0+if (!vma ||is_vm_hugetlb_page (vma)) +return ; + +/* Get pointer to the page table entry */ + ptep =get_locked_pte (mm, vmaddr, &ptl); +if (!likely (ptep))if (unlikely(!ptep)) reads nicer to me.
ok
Quoted 11 lines
arch/s390/mm/gmap_helpers.c line 49
Quoted 7 lines
arch/s390/mm/gmap_helpers.c new file +6
-0+return ; +if (pte_swap (*ptep)) +ptep_zap_swap_entry (mm,pte_to_swp_entry (*ptep)); +pte_unmap_unlock (ptep, ptl); +} +EXPORT_SYMBOL_GPL (__gmap_helper_zap_one);Looks reasonable, but I'm not well versed enough in mm code to evaluate that with confidence.
Quoted 8 lines
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ +void __gmap_helper_discard(struct mm_struct *mm, unsigned long vmaddr, unsigned long end)Maybe call this
gmap_helper_discard_nolockor something.maybe
__gmap_helper_discard_unlocked?the __ prefix often implies lack of locking
_nolock *definitely* implies it :P
[...]
Quoted 5 lines
Quoted 3 lines
The stuff below is from
arch/s390/mm/gmap.cright? Are you going to delete it from there?not in this series, but the next series will remove
mm/gmap.caltogether
Can't you do it with this one?
[...]
Signature · 4 lines
IBM Deutschland Research & Development GmbH Vorsitzender des Aufsichtsrats: Wolfgang Wendt Geschäftsführung: David Faller Sitz der Gesellschaft: Böblingen / Registergericht: Amtsgericht Stuttgart, HRB 243294
Claudio Imbrenda
maintainerRe: [PATCH v1 4/5] KVM: s390: refactor and split some gmap helpers
Quoted 29 lines · On Wed, 21 May 2025 17:30:00 +0200 Nina Schoetterl-Glausch <[email protected]>…
Quoted 26 lines · Claudio Imbrenda
Quoted 23 lines · On Wed, 21 May 2025 16:55:18 +0200 Nina Schoetterl-Glausch <[email protected]>…
Quoted 22 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.
Signed-off-by Claudio Imbrenda <[email protected]>--- MAINTAINERS | 2 + arch/s390/include/asm/gmap_helpers.h | 18 ++ arch/s390/kvm/diag.c | 11 +- arch/s390/kvm/kvm-s390.c | 3 +- arch/s390/mm/Makefile | 2 + arch/s390/mm/gmap.c | 46 ++--- arch/s390/mm/gmap_helpers.c | 266 +++++++++++++++++++++++++++ 7 files changed, 307 insertions(+), 41 deletions(-) create mode 100644 arch/s390/include/asm/gmap_helpers.h create mode 100644 arch/s390/mm/gmap_helpers.c[...]
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
+void
__gmap_helper_zap_one(struct mm_struct *mm, unsigned long vmaddr)
__gmap_helper_zap_mapping_pte?but I'm not taking a pte as parameter
The pte being zapped is the one mapping vmaddr, right?
I don't know, _pte kinda sounds to me as the function would be taking a pte as parameter
Quoted 47 lines
Quoted 33 lines
Quoted 11 lines
Quoted 11 lines
arch/s390/mm/gmap_helpers.c new file +9
-0+{ +struct vm_area_struct *vma; +spinlock_t *ptl; +pte_t *ptep; + +mmap_assert_locked (mm); + +/* Find the vm address for the guest address */ + vma =vma_lookup (mm, vmaddr);Quoted 8 lines
arch/s390/mm/gmap_helpers.c line 43
Quoted 6 lines
arch/s390/mm/gmap_helpers.c new file +6
-0+if (!vma ||is_vm_hugetlb_page (vma)) +return ; + +/* Get pointer to the page table entry */ + ptep =get_locked_pte (mm, vmaddr, &ptl); +if (!likely (ptep))if (unlikely(!ptep)) reads nicer to me.
ok
Quoted 11 lines
arch/s390/mm/gmap_helpers.c line 49
Quoted 7 lines
arch/s390/mm/gmap_helpers.c new file +6
-0+return ; +if (pte_swap (*ptep)) +ptep_zap_swap_entry (mm,pte_to_swp_entry (*ptep)); +pte_unmap_unlock (ptep, ptl); +} +EXPORT_SYMBOL_GPL (__gmap_helper_zap_one);Looks reasonable, but I'm not well versed enough in mm code to evaluate that with confidence.
Quoted 8 lines
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ +void __gmap_helper_discard(struct mm_struct *mm, unsigned long vmaddr, unsigned long end)Maybe call this
gmap_helper_discard_nolockor something.maybe
__gmap_helper_discard_unlocked?the __ prefix often implies lack of locking
_nolock*definitely* implies it :P[...]
Quoted 1 line
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
The stuff below is from
arch/s390/mm/gmap.cright? Are you going to delete it from there?not in this series, but the next series will remove
mm/gmap.caltogetherCan't you do it with this one?
if you mean removing mm/gmap.c, no. I would need to push the whole gmap rewrite series, which is not ready yet.
if you mean removing the redundant functions... I guess I could
Quoted 3 lines
[...]
Re: [PATCH v1 4/5] KVM: s390: refactor and split some gmap helpers
Quoted 34 lines · Claudio Imbrenda
Quoted 31 lines · On Wed, 21 May 2025 17:30:00 +0200 Nina Schoetterl-Glausch <[email protected]>…
Quoted 26 lines · Claudio Imbrenda
Quoted 23 lines · On Wed, 21 May 2025 16:55:18 +0200 Nina Schoetterl-Glausch <[email protected]>…
Quoted 22 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.
Signed-off-by Claudio Imbrenda <[email protected]>--- MAINTAINERS | 2 + arch/s390/include/asm/gmap_helpers.h | 18 ++ arch/s390/kvm/diag.c | 11 +- arch/s390/kvm/kvm-s390.c | 3 +- arch/s390/mm/Makefile | 2 + arch/s390/mm/gmap.c | 46 ++--- arch/s390/mm/gmap_helpers.c | 266 +++++++++++++++++++++++++++ 7 files changed, 307 insertions(+), 41 deletions(-) create mode 100644 arch/s390/include/asm/gmap_helpers.h create mode 100644 arch/s390/mm/gmap_helpers.c[...]
Quoted 2 lines
Quoted 2 lines
Quoted 1 line
+void
__gmap_helper_zap_one(struct mm_struct *mm, unsigned long vmaddr)
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
__gmap_helper_zap_mapping_pte?but I'm not taking a pte as parameter
The pte being zapped is the one mapping vmaddr, right?
I don't know,
_ptekinda sounds to me as the function would be taking a pte as parameter
__gmap_helper_zap_pte_mapping_addr ?
IMO __gmap_helper_zap_one is rather vague. Zap one? Which one?
[...]
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
The stuff below is from
arch/s390/mm/gmap.cright? Are you going to delete it from there?
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
not in this series, but the next series will remove
mm/gmap.caltogetherCan't you do it with this one?
if you mean removing
mm/gmap.c, no. I would need to push the whole gmap rewrite series, which is not ready yet.if you mean removing the redundant functions... I guess I could
The latter. I think that would be cleaner.
Quoted 4 lines
Quoted 4 lines
[...]
Signature · 4 lines
IBM Deutschland Research & Development GmbH Vorsitzender des Aufsichtsrats: Wolfgang Wendt Geschäftsführung: David Faller Sitz der Gesellschaft: Böblingen / Registergericht: Amtsgericht Stuttgart, HRB 243294
Claudio Imbrenda
maintainerPATCH 5/5 KVM: s390: simplify and move pv code
In reply to Claudio ImbrendaAll 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 | 9 +-- 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(+), 181 deletions(-) delete mode 100644 arch/s390/kvm/gmap.c delete mode 100644 arch/s390/kvm/gmap.h
arch/s390/kernel/uv.c +6 -6
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 -6
@@ -22,7 +22,6 @@#include "gaccess.h" #include "trace.h" #include "trace-s390.h" -#include "gmap.h" u8 kvm_s390_get_ilen (struct kvm_vcpu *vcpu) {
@@ -546,7 +545,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 +653,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
@@ -53,7 +53,6 @@#include "kvm-s390.h" #include "gaccess.h" #include "pci.h" -#include "gmap.h" #define CREATE_TRACE_POINTS #include "trace.h"
@@ -4974,7 +4973,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.
@@ -4996,7 +4995,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
@@ -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
Re: [PATCH v1 5/5] KVM: s390: simplify and move pv code
Quoted 5 lines · Claudio Imbrenda
All functions in
kvm/gmap.cfit better inkvm/pv.cinstead. Move and rename them appropriately, then delete the now emptykvm/gmap.candkvm/gmap.h.Signed-off-by Claudio Imbrenda <[email protected]>
[...]
Signature · 4 lines
IBM Deutschland Research & Development GmbH Vorsitzender des Aufsichtsrats: Wolfgang Wendt Geschäftsführung: David Faller Sitz der Gesellschaft: Böblingen / Registergericht: Amtsgericht Stuttgart, HRB 243294
Quoted 29 lines · Claudio Imbrenda
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.
Claudio Imbrenda (5):
s390: remove unneeded includes
KVM: s390: remove unneeded srcu lock
KVM: s390: refactor some functions inpriv.c
KVM: s390: refactor and split some gmap helpers
KVM: s390: simplify and move pv codeMAINTAINERS | 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 | 10 +- arch/s390/kvm/kvm-s390.c | 8 +- arch/s390/kvm/kvm-s390.h | 57 ++++++ arch/s390/kvm/priv.c | 292 ++++++++++++--------------- arch/s390/kvm/pv.c | 61 +++++- arch/s390/kvm/vsie.c | 19 +-
Quoted 8 lines
arch/s390/mm/Makefile | 2 + arch/s390/mm/fault.c | 1 - arch/s390/mm/gmap.c | 47 +---- arch/s390/mm/gmap_helpers.c | 266 ++++++++++++++++++++++++ arch/s390/mm/init.c | 1 - arch/s390/mm/pgalloc.c | 2 - arch/s390/mm/pgtable.c | 1 - 24 files changed, 590 insertions(+), 388 deletions(-)
Hehe, that's not what I expected from a cleanup. I assume it's mostly the "factor out stuff" and new file that adds these LOC?
Signature · 3 lines
Cheers, David / dhildenb