Skip to contentSkip to conversation

s390/mm: Deduplicate machine_has_tlb_guest() usage

PATCH New Heiko Carstens 2 participants 7 messages Nov 24

Subsystems (3)
lore
Share & tools
lore

Heiko Carstens

author maintainer
#0 lore raw

cover s390/mm: Deduplicate machine_has_tlb_guest() usage

While working on something different there was the need to add yet another machine_has_tlb_guest() check. The machine_has_tlb_guest() check pattern is duplicated all over the place. It seems to make sense to add common helpers instead of duplicating the code again.

This also revealed that the gmap_asce member in mm_context_t is always zero since Claudio's gmap rework. Removing it allows to simplify quite a bit of code.

Code applies on top of linux-next 20261123.

Thanks,
Heiko

Heiko Carstens (5):
s390/mm: Remove gmap_asce from mm_context_t
s390/mm: Remove unused nodat parameter from ptep flush helpers s390/mm: Add ptep_ipte() helper
s390/mm: Add pmdp_idte() helper
s390/mm: Add pudp_idte() helper

 arch/s390/include/asm/mmu.h         |  1 -
 arch/s390/include/asm/mmu_context.h |  1 -
 arch/s390/include/asm/pgtable.h     | 31 ++++++++++
 arch/s390/include/asm/tlbflush.h    | 12 +---
 arch/s390/kvm/gmap/dat.c            |  5 +-
 arch/s390/mm/pgtable.c              | 94 ++++++-----------------------
 6 files changed, 53 insertions(+), 91 deletions(-)

base-commit: 3191063649dbf7c0d0b768e1f3655e4c5fc6edb5

Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/5 s390/mm: Remove gmap_asce from mm_context_t

After the gmap rework mm->context.gmap_asce is always 0. Remove it, and simplify the mm code accordingly.

---
 arch/s390/include/asm/mmu.h         |  1 -
 arch/s390/include/asm/mmu_context.h |  1 -
 arch/s390/include/asm/tlbflush.h    | 12 +---------
 arch/s390/mm/pgtable.c              | 34 +++++------------------------
 4 files changed, 7 insertions(+), 41 deletions(-)
arch/s390/include/asm/mmu.h +0 -1
index d4fd7bf3692e..3bf06dcba050 100644
@@ -12,7 +12,6 @@ typedef struct {
 	atomic_t flush_count;
 	unsigned int flush_mm;
 	struct list_head gmap_list;
-	unsigned long gmap_asce;
 	unsigned long asce;
 	unsigned long asce_limit;
 	unsigned long vdso_base;
arch/s390/include/asm/mmu_context.h +0 -1
index bd1ef5e2d2eb..4e5da0017c15 100644
@@ -27,7 +27,6 @@ static inline int init_new_context(struct task_struct *tsk,
 	cpumask_clear(&mm->context.cpu_attach_mask);
 	atomic_set(&mm->context.flush_count, 0);
 	atomic_set(&mm->context.protected_count, 0);
-	mm->context.gmap_asce = 0;
 	mm->context.flush_mm = 0;
 #if IS_ENABLED(CONFIG_KVM)
 	mm->context.allow_cow_sharing = 1;
arch/s390/include/asm/tlbflush.h +1 -11
index 163ccbbe8c47..ca517013b5d0 100644
@@ -46,22 +46,12 @@ static inline void __tlb_flush_global(void)
  */
 static inline void __tlb_flush_mm(struct mm_struct *mm)
 {
-	unsigned long gmap_asce;
-
 	preempt_disable();
 	atomic_inc(&mm->context.flush_count);
 	/* Reset TLB flush mask */
 	cpumask_copy(mm_cpumask(mm), &mm->context.cpu_attach_mask);
 	barrier();
-	gmap_asce = READ_ONCE(mm->context.gmap_asce);
-	if (gmap_asce != -1UL) {
-		if (gmap_asce)
-			__tlb_flush_idte(gmap_asce);
-		__tlb_flush_idte(mm->context.asce);
-	} else {
-		/* Global TLB flush */
-		__tlb_flush_global();
-	}
+	__tlb_flush_idte(mm->context.asce);
 	atomic_dec(&mm->context.flush_count);
 	preempt_enable();
 }
arch/s390/mm/pgtable.c +6 -28
index da076928b63b..561d280d485f 100644
@@ -39,41 +39,19 @@ EXPORT_SYMBOL_GPL(pgprot_writecombine);
 static inline void ptep_ipte_local(struct mm_struct *mm, unsigned long addr,
 				   pte_t *ptep, int nodat)
 {
-	unsigned long opt, asce;
-
-	if (machine_has_tlb_guest()) {
-		opt = 0;
-		asce = READ_ONCE(mm->context.gmap_asce);
-		if (asce == 0UL || nodat)
-			opt |= IPTE_NODAT;
-		if (asce != -1UL) {
-			asce = asce ? : mm->context.asce;
-			opt |= IPTE_GUEST_ASCE;
-		}
-		__ptep_ipte(addr, ptep, opt, asce, IPTE_LOCAL);
-	} else {
+	if (machine_has_tlb_guest())
+		__ptep_ipte(addr, ptep, IPTE_NODAT | IPTE_GUEST_ASCE, mm->context.asce, IPTE_LOCAL);
+	else
 		__ptep_ipte(addr, ptep, 0, 0, IPTE_LOCAL);
-	}
 }
 
 static inline void ptep_ipte_global(struct mm_struct *mm, unsigned long addr,
 				    pte_t *ptep, int nodat)
 {
-	unsigned long opt, asce;
-
-	if (machine_has_tlb_guest()) {
-		opt = 0;
-		asce = READ_ONCE(mm->context.gmap_asce);
-		if (asce == 0UL || nodat)
-			opt |= IPTE_NODAT;
-		if (asce != -1UL) {
-			asce = asce ? : mm->context.asce;
-			opt |= IPTE_GUEST_ASCE;
-		}
-		__ptep_ipte(addr, ptep, opt, asce, IPTE_GLOBAL);
-	} else {
+	if (machine_has_tlb_guest())
+		__ptep_ipte(addr, ptep, IPTE_NODAT | IPTE_GUEST_ASCE, mm->context.asce, IPTE_GLOBAL);
+	else
 		__ptep_ipte(addr, ptep, 0, 0, IPTE_GLOBAL);
-	}
 }
 
 static inline pte_t ptep_flush_direct(struct mm_struct *mm,
Signature · 1 line
2.53.0
author maintainer
#2 lore raw

PATCH 2/5 s390/mm: Remove unused nodat parameter from ptep flush helpers

In reply to Heiko Carstens

The nodat parameter of ptep_ipte_local() and ptep_ipte_global() is not used anymore. Remove it and update its callers.

---
 arch/s390/mm/pgtable.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)
arch/s390/mm/pgtable.c +10 -16
index 561d280d485f..1aa5fe0b30d8 100644
@@ -36,8 +36,7 @@ pgprot_t pgprot_writecombine(pgprot_t prot)
 }
 EXPORT_SYMBOL_GPL(pgprot_writecombine);
 
-static inline void ptep_ipte_local(struct mm_struct *mm, unsigned long addr,
-				   pte_t *ptep, int nodat)
+static inline void ptep_ipte_local(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
 {
 	if (machine_has_tlb_guest())
 		__ptep_ipte(addr, ptep, IPTE_NODAT | IPTE_GUEST_ASCE, mm->context.asce, IPTE_LOCAL);
@@ -45,8 +44,7 @@ static inline void ptep_ipte_local(struct mm_struct *mm, unsigned long addr,
 		__ptep_ipte(addr, ptep, 0, 0, IPTE_LOCAL);
 }
 
-static inline void ptep_ipte_global(struct mm_struct *mm, unsigned long addr,
-				    pte_t *ptep, int nodat)
+static inline void ptep_ipte_global(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
 {
 	if (machine_has_tlb_guest())
 		__ptep_ipte(addr, ptep, IPTE_NODAT | IPTE_GUEST_ASCE, mm->context.asce, IPTE_GLOBAL);
@@ -54,9 +52,7 @@ static inline void ptep_ipte_global(struct mm_struct *mm, unsigned long addr,
 		__ptep_ipte(addr, ptep, 0, 0, IPTE_GLOBAL);
 }
 
-static inline pte_t ptep_flush_direct(struct mm_struct *mm,
-				      unsigned long addr, pte_t *ptep,
-				      int nodat)
+static inline pte_t ptep_flush_direct(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
 {
 	pte_t old;
 
@@ -66,16 +62,14 @@ static inline pte_t ptep_flush_direct(struct mm_struct *mm,
 	atomic_inc(&mm->context.flush_count);
 	if (cpu_has_tlb_lc() &&
 	    cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
-		ptep_ipte_local(mm, addr, ptep, nodat);
+		ptep_ipte_local(mm, addr, ptep);
 	else
-		ptep_ipte_global(mm, addr, ptep, nodat);
+		ptep_ipte_global(mm, addr, ptep);
 	atomic_dec(&mm->context.flush_count);
 	return old;
 }
 
-static inline pte_t ptep_flush_lazy(struct mm_struct *mm,
-				    unsigned long addr, pte_t *ptep,
-				    int nodat)
+static inline pte_t ptep_flush_lazy(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
 {
 	pte_t old;
 
@@ -88,7 +82,7 @@ static inline pte_t ptep_flush_lazy(struct mm_struct *mm,
 		set_pte(ptep, set_pte_bit(old, __pgprot(_PAGE_INVALID)));
 		mm->context.flush_mm = 1;
 	} else
-		ptep_ipte_global(mm, addr, ptep, nodat);
+		ptep_ipte_global(mm, addr, ptep);
 	atomic_dec(&mm->context.flush_count);
 	return old;
 }
@@ -99,7 +93,7 @@ pte_t ptep_xchg_direct(struct mm_struct *mm, unsigned long addr,
 	pte_t old;
 
 	preempt_disable();
-	old = ptep_flush_direct(mm, addr, ptep, 1);
+	old = ptep_flush_direct(mm, addr, ptep);
 	set_pte(ptep, new);
 	preempt_enable();
 	return old;
@@ -137,7 +131,7 @@ pte_t ptep_xchg_lazy(struct mm_struct *mm, unsigned long addr,
 	pte_t old;
 
 	preempt_disable();
-	old = ptep_flush_lazy(mm, addr, ptep, 1);
+	old = ptep_flush_lazy(mm, addr, ptep);
 	set_pte(ptep, new);
 	preempt_enable();
 	return old;
@@ -147,7 +141,7 @@ EXPORT_SYMBOL(ptep_xchg_lazy);
 pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr,
 			     pte_t *ptep)
 {
-	return ptep_flush_lazy(vma->vm_mm, addr, ptep, 1);
+	return ptep_flush_lazy(vma->vm_mm, addr, ptep);
 }
 
 void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr,
Signature · 1 line
2.53.0
author maintainer
#3 lore raw

PATCH 3/5 s390/mm: Add ptep_ipte() helper

In reply to Heiko Carstens

Add a common ptep_ipte() helper in order to avoid the repeated machine_has_tlb_guest() checks.

---
 arch/s390/include/asm/pgtable.h | 11 +++++++++++
 arch/s390/kvm/gmap/dat.c        |  5 +----
 arch/s390/mm/pgtable.c          | 10 ++--------
 3 files changed, 14 insertions(+), 12 deletions(-)
arch/s390/include/asm/pgtable.h +11 -0
index 2d5c2ab06de9..378d2db6e6be 100644
@@ -21,6 +21,7 @@
 #include <linux/atomic.h>
 #include <linux/mmap_lock.h>
 #include <asm/ctlreg.h>
+#include <asm/machine.h>
 #include <asm/bug.h>
 #include <asm/page.h>
 #include <asm/uv.h>
@@ -1156,6 +1157,16 @@ static __always_inline void __ptep_ipte(unsigned long address, pte_t *ptep,
 		: [r1] "a" (pto), [m4] "i" (local) : "memory");
 }
 
+static __always_inline void ptep_ipte(unsigned long address, pte_t *ptep,
+				      unsigned long opt, unsigned long asce,
+				      int local)
+{
+	if (machine_has_tlb_guest())
+		__ptep_ipte(address, ptep, opt, asce, local);
+	else
+		__ptep_ipte(address, ptep, 0, 0, local);
+}
+
 static __always_inline void __ptep_ipte_range(unsigned long address, int nr,
 					      pte_t *ptep, int local)
 {
arch/s390/kvm/gmap/dat.c +1 -4
index 7a25e41a1423..909d665a67a9 100644
@@ -173,10 +173,7 @@ union pgste __dat_ptep_xchg(union pte *ptep, union pgste pgste, union pte new, g
 	if (!old.h.i) {
 		unsigned long opts = IPTE_GUEST_ASCE | (pgste.nodat ? IPTE_NODAT : 0);
 
-		if (machine_has_tlb_guest())
-			__ptep_ipte(gfn_to_gpa(gfn), (void *)ptep, opts, asce.val, IPTE_GLOBAL);
-		else
-			__ptep_ipte(gfn_to_gpa(gfn), (void *)ptep, 0, 0, IPTE_GLOBAL);
+		ptep_ipte(gfn_to_gpa(gfn), (void *)ptep, opts, asce.val, IPTE_GLOBAL);
 	}
 
 	if (uses_skeys) {
arch/s390/mm/pgtable.c +2 -8
index 1aa5fe0b30d8..f6a960f39abf 100644
@@ -38,18 +38,12 @@ EXPORT_SYMBOL_GPL(pgprot_writecombine);
 
 static inline void ptep_ipte_local(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
 {
-	if (machine_has_tlb_guest())
-		__ptep_ipte(addr, ptep, IPTE_NODAT | IPTE_GUEST_ASCE, mm->context.asce, IPTE_LOCAL);
-	else
-		__ptep_ipte(addr, ptep, 0, 0, IPTE_LOCAL);
+	ptep_ipte(addr, ptep, IPTE_NODAT | IPTE_GUEST_ASCE, mm->context.asce, IPTE_LOCAL);
 }
 
 static inline void ptep_ipte_global(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
 {
-	if (machine_has_tlb_guest())
-		__ptep_ipte(addr, ptep, IPTE_NODAT | IPTE_GUEST_ASCE, mm->context.asce, IPTE_GLOBAL);
-	else
-		__ptep_ipte(addr, ptep, 0, 0, IPTE_GLOBAL);
+	ptep_ipte(addr, ptep, IPTE_NODAT | IPTE_GUEST_ASCE, mm->context.asce, IPTE_GLOBAL);
 }
 
 static inline pte_t ptep_flush_direct(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
Signature · 1 line
2.53.0
author maintainer
#4 lore raw

PATCH 4/5 s390/mm: Add pmdp_idte() helper

In reply to Heiko Carstens

Add a common pmdp_idte() helper in order to avoid the repeated machine_has_tlb_guest() checks.

---
 arch/s390/include/asm/pgtable.h | 10 ++++++++++
 arch/s390/mm/pgtable.c          | 18 ++++--------------
 2 files changed, 14 insertions(+), 14 deletions(-)
arch/s390/include/asm/pgtable.h +10 -0
index 378d2db6e6be..0d5ea2dca786 100644
@@ -1661,6 +1661,16 @@ static __always_inline void __pmdp_idte(unsigned long addr, pmd_t *pmdp,
 	}
 }
 
+static __always_inline void pmdp_idte(unsigned long addr, pmd_t *pmdp,
+				      unsigned long opt, unsigned long asce,
+				      int local)
+{
+	if (machine_has_tlb_guest())
+		__pmdp_idte(addr, pmdp, opt, asce, local);
+	else
+		__pmdp_idte(addr, pmdp, 0, 0, local);
+}
+
 static __always_inline void __pudp_idte(unsigned long addr, pud_t *pudp,
 					unsigned long opt, unsigned long asce,
 					int local)
arch/s390/mm/pgtable.c +4 -14
index f6a960f39abf..72b62ad480bd 100644
@@ -144,24 +144,14 @@ void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr,
 	set_pte(ptep, pte);
 }
 
-static inline void pmdp_idte_local(struct mm_struct *mm,
-				   unsigned long addr, pmd_t *pmdp)
+static inline void pmdp_idte_local(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp)
 {
-	if (machine_has_tlb_guest())
-		__pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE, mm->context.asce, IDTE_LOCAL);
-	else
-		__pmdp_idte(addr, pmdp, 0, 0, IDTE_LOCAL);
+	pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE, mm->context.asce, IDTE_LOCAL);
 }
 
-static inline void pmdp_idte_global(struct mm_struct *mm,
-				    unsigned long addr, pmd_t *pmdp)
+static inline void pmdp_idte_global(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp)
 {
-	if (machine_has_tlb_guest()) {
-		__pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE,
-			    mm->context.asce, IDTE_GLOBAL);
-	} else {
-		__pmdp_idte(addr, pmdp, 0, 0, IDTE_GLOBAL);
-	}
+	pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE, mm->context.asce, IDTE_GLOBAL);
 }
 
 static inline pmd_t pmdp_flush_direct(struct mm_struct *mm,
Signature · 1 line
2.53.0
author maintainer
#5 lore raw

PATCH 5/5 s390/mm: Add pudp_idte() helper

In reply to Heiko Carstens

Add a common pudp_idte() helper in order to avoid the repeated machine_has_tlb_guest() checks.

---
 arch/s390/include/asm/pgtable.h | 10 ++++++++++
 arch/s390/mm/pgtable.c          | 18 ++++--------------
 2 files changed, 14 insertions(+), 14 deletions(-)
arch/s390/include/asm/pgtable.h +10 -0
index 0d5ea2dca786..e8bb46071306 100644
@@ -1698,6 +1698,16 @@ static __always_inline void __pudp_idte(unsigned long addr, pud_t *pudp,
 	}
 }
 
+static __always_inline void pudp_idte(unsigned long addr, pud_t *pudp,
+				      unsigned long opt, unsigned long asce,
+				      int local)
+{
+	if (machine_has_tlb_guest())
+		__pudp_idte(addr, pudp, opt, asce, local);
+	else
+		__pudp_idte(addr, pudp, 0, 0, local);
+}
+
 pmd_t pmdp_xchg_direct(struct mm_struct *, unsigned long, pmd_t *, pmd_t);
 pmd_t pmdp_xchg_lazy(struct mm_struct *, unsigned long, pmd_t *, pmd_t);
 pud_t pudp_xchg_direct(struct mm_struct *, unsigned long, pud_t *, pud_t);
arch/s390/mm/pgtable.c +4 -14
index 72b62ad480bd..23125e88d17a 100644
@@ -218,24 +218,14 @@ pmd_t pmdp_xchg_lazy(struct mm_struct *mm, unsigned long addr,
 }
 EXPORT_SYMBOL(pmdp_xchg_lazy);
 
-static inline void pudp_idte_local(struct mm_struct *mm,
-				   unsigned long addr, pud_t *pudp)
+static inline void pudp_idte_local(struct mm_struct *mm, unsigned long addr, pud_t *pudp)
 {
-	if (machine_has_tlb_guest())
-		__pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
-			    mm->context.asce, IDTE_LOCAL);
-	else
-		__pudp_idte(addr, pudp, 0, 0, IDTE_LOCAL);
+	pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE, mm->context.asce, IDTE_LOCAL);
 }
 
-static inline void pudp_idte_global(struct mm_struct *mm,
-				    unsigned long addr, pud_t *pudp)
+static inline void pudp_idte_global(struct mm_struct *mm, unsigned long addr, pud_t *pudp)
 {
-	if (machine_has_tlb_guest())
-		__pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
-			    mm->context.asce, IDTE_GLOBAL);
-	else
-		__pudp_idte(addr, pudp, 0, 0, IDTE_GLOBAL);
+	pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE, mm->context.asce, IDTE_GLOBAL);
 }
 
 static inline pud_t pudp_flush_direct(struct mm_struct *mm,
Signature · 1 line
2.53.0