Skip to contentSkip to conversation

powerpc/64s/hash: add torture_slb kernel boot option to increase SLB faults

PATCH Superseded Nicholas Piggin 2 participants 6 messages May 3, 2020 – May 4, 2020

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Nicholas Piggin

author reviewer
#0 lore raw

PATCH 1/2 powerpc/64s/hash: add torture_slb kernel boot option to increase SLB faults

This option increases the number of SLB misses by limiting the number of kernel SLB entries, and increased flushing of cached lookaside information. This helps stress test difficult to hit paths in the kernel.

---
 .../admin-guide/kernel-parameters.txt         |   5 +
 arch/powerpc/include/asm/book3s/64/mmu-hash.h |   7 +
 arch/powerpc/mm/book3s64/hash_utils.c         |  13 ++
 arch/powerpc/mm/book3s64/slb.c                | 145 ++++++++++++------
 4 files changed, 125 insertions(+), 45 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -0
index f2a93c8679e8..5a34b7dd9ebe 100644
@@ -871,6 +871,11 @@
 			can be useful when debugging issues that require an SLB
 			miss to occur.
 
+	torture_slb	[PPC]
+			Limits the number of kernel SLB entries, and flushes
+			them frequently to increase the rate of SLB faults
+			on kernel addresses.
+
 	disable=	[IPV6]
 			See Documentation/networking/ipv6.txt.
 
arch/powerpc/include/asm/book3s/64/mmu-hash.h +7 -0
index 3fa1b962dc27..758de1e0f676 100644
@@ -317,6 +317,13 @@ extern unsigned long tce_alloc_start, tce_alloc_end;
  */
 extern int mmu_ci_restrictions;
 
+extern bool torture_slb_enabled;
+DECLARE_STATIC_KEY_FALSE(torture_slb_key);
+static inline bool torture_slb(void)
+{
+	return static_branch_unlikely(&torture_slb_key);
+}
+
 /*
  * This computes the AVPN and B fields of the first dword of a HPTE,
  * for use when we want to match an existing PTE.  The bottom 7 bits
arch/powerpc/mm/book3s64/hash_utils.c +13 -0
index 8ed2411c3f39..9c487b5782ef 100644
@@ -354,6 +354,7 @@ int htab_remove_mapping(unsigned long vstart, unsigned long vend,
 }
 
 static bool disable_1tb_segments = false;
+bool torture_slb_enabled __read_mostly = false;
 
 static int __init parse_disable_1tb_segments(char *p)
 {
@@ -362,6 +363,13 @@ static int __init parse_disable_1tb_segments(char *p)
 }
 early_param("disable_1tb_segments", parse_disable_1tb_segments);
 
+static int __init parse_torture_slb(char *p)
+{
+	torture_slb_enabled = true;
+	return 0;
+}
+early_param("torture_slb", parse_torture_slb);
+
 static int __init htab_dt_scan_seg_sizes(unsigned long node,
 					 const char *uname, int depth,
 					 void *data)
@@ -854,6 +862,8 @@ static void __init hash_init_partition_table(phys_addr_t hash_table,
 	pr_info("Partition table %p\n", partition_tb);
 }
 
+DEFINE_STATIC_KEY_FALSE(torture_slb_key);
+
 static void __init htab_initialize(void)
 {
 	unsigned long table;
@@ -870,6 +880,9 @@ static void __init htab_initialize(void)
 		printk(KERN_INFO "Using 1TB segments\n");
 	}
 
+	if (torture_slb_enabled)
+		static_branch_enable(&torture_slb_key);
+
 	/*
 	 * Calculate the required size of the htab.  We want the number of
 	 * PTEGs to equal one half the number of real pages.
arch/powerpc/mm/book3s64/slb.c +100 -45

221 lines not on this page · raw message

Signature · 1 line
2.23.0
author reviewer
#1 lore raw

PATCH 2/2 powerpc/64s/hash: add torture_hpt kernel boot option to increase hash faults

This option increases the number of hash misses by limiting the number of kernel HPT entries. This helps stress test difficult to hit paths in the kernel.

---
 .../admin-guide/kernel-parameters.txt         |  9 +++
 arch/powerpc/include/asm/book3s/64/mmu-hash.h | 10 +++
 arch/powerpc/mm/book3s64/hash_4k.c            |  3 +
 arch/powerpc/mm/book3s64/hash_64k.c           |  8 +++
 arch/powerpc/mm/book3s64/hash_utils.c         | 66 ++++++++++++++++++-
 5 files changed, 95 insertions(+), 1 deletion(-)
Documentation/admin-guide/kernel-parameters.txt +9 -0
index 5a34b7dd9ebe..1ec6a32a717a 100644
@@ -876,6 +876,15 @@
 			them frequently to increase the rate of SLB faults
 			on kernel addresses.
 
+	torture_hpt	[PPC]
+			Limits the number of kernel HPT entries in the hash
+			page table to increase the rate of hash page table
+			faults on kernel addresses.
+
+			This may hang when run on processors / emulators which
+			do not have a TLB, or flush it more often than
+			required, QEMU seems to have problems.
+
 	disable=	[IPV6]
 			See Documentation/networking/ipv6.txt.
 
arch/powerpc/include/asm/book3s/64/mmu-hash.h +10 -0
index 758de1e0f676..539e3d91eac4 100644
@@ -324,6 +324,16 @@ static inline bool torture_slb(void)
 	return static_branch_unlikely(&torture_slb_key);
 }
 
+extern bool torture_hpt_enabled;
+DECLARE_STATIC_KEY_FALSE(torture_hpt_key);
+static inline bool torture_hpt(void)
+{
+	return static_branch_unlikely(&torture_hpt_key);
+}
+
+void hpt_do_torture(unsigned long ea, unsigned long access,
+		    unsigned long rflags, unsigned long hpte_group);
+
 /*
  * This computes the AVPN and B fields of the first dword of a HPTE,
  * for use when we want to match an existing PTE.  The bottom 7 bits
arch/powerpc/mm/book3s64/hash_4k.c +3 -0
index 22e787123cdf..54e4ff8c558d 100644
@@ -118,6 +118,9 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
 		}
 		new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
 		new_pte |= pte_set_hidx(ptep, rpte, 0, slot, PTRS_PER_PTE);
+
+		if (torture_hpt())
+			hpt_do_torture(ea, access, rflags, hpte_group);
 	}
 	*ptep = __pte(new_pte & ~H_PAGE_BUSY);
 	return 0;
arch/powerpc/mm/book3s64/hash_64k.c +8 -0
index 7084ce2951e6..19ea0fc145a9 100644
@@ -216,6 +216,9 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
 	new_pte |= pte_set_hidx(ptep, rpte, subpg_index, slot, PTRS_PER_PTE);
 	new_pte |= H_PAGE_HASHPTE;
 
+	if (torture_hpt())
+		hpt_do_torture(ea, access, rflags, hpte_group);
+
 	*ptep = __pte(new_pte & ~H_PAGE_BUSY);
 	return 0;
 }
@@ -327,7 +330,12 @@ int __hash_page_64K(unsigned long ea, unsigned long access,
 
 		new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
 		new_pte |= pte_set_hidx(ptep, rpte, 0, slot, PTRS_PER_PTE);
+
+		if (torture_hpt())
+			hpt_do_torture(ea, access, rflags, hpte_group);
 	}
+
 	*ptep = __pte(new_pte & ~H_PAGE_BUSY);
+
 	return 0;
 }
arch/powerpc/mm/book3s64/hash_utils.c +65 -1

97 lines not on this page · raw message

Signature · 1 line
2.23.0
#4 lore raw
In reply to Nicholas Piggin
Quoted 5 lines · Nicholas Piggin

This option increases the number of SLB misses by limiting the number of kernel SLB entries, and increased flushing of cached lookaside information. This helps stress test difficult to hit paths in the kernel.

....

Quoted 4 lines
arch/powerpc/mm/book3s64/slb.c +4 -0
+{
+	unsigned long slbie_data = get_paca()->slb_cache[index];
+	unsigned long ksp = get_paca()->kstack;
+

arch/powerpc/mm/book3s64/slb.c void preload_new_slb_context(unsigned long start, unsigned long sp) · line 428

Quoted 10 lines
arch/powerpc/mm/book3s64/slb.c +10 -0
+	slbie_data <<= SID_SHIFT;
+	slbie_data |= 0xc000000000000000ULL;
+	if ((ksp & slb_esid_mask(mmu_kernel_ssize)) == slbie_data)
+		return;
+	slbie_data |= mmu_kernel_ssize << SLBIE_SSIZE_SHIFT;
+
+	asm volatile("slbie %0" : : "r" (slbie_data));
+}
+
+static void slb_cache_slbie(unsigned int index)

May be slb_cache_slbie_user()? Similar to _kernel above?

Quoted 13 lines
arch/powerpc/mm/book3s64/slb.c +9 -0
+{
+	unsigned long slbie_data = get_paca()->slb_cache[index];
+
+	slbie_data <<= SID_SHIFT;
+	slbie_data |= user_segment_size(slbie_data) << SLBIE_SSIZE_SHIFT;
+	slbie_data |= SLBIE_C; /* user slbs have C=1 */
+
+	asm volatile("slbie %0" : : "r" (slbie_data));
+}
 
 /* Flush all user entries from the segment table of the current processor. */
 void switch_slb(struct task_struct *tsk, struct mm_struct *mm)

arch/powerpc/mm/book3s64/slb.c void switch_slb(struct task_struct *tsk, struct mm_struct *mm) · line 454

Quoted 8 lines
arch/powerpc/mm/book3s64/slb.c +3 -2
@@ -414,8 +449,14 @@ void switch_slb(struct task_struct *tsk, struct mm_struct *mm)
 	 * which would update the slb_cache/slb_cache_ptr fields in the PACA.
 	 */
 	hard_irq_disable();
-	asm volatile("isync" : : : "memory");
-	if (cpu_has_feature(CPU_FTR_ARCH_300)) {
+	isync();
+	if (torture_slb()) {
+		__slb_flush_and_restore_bolted(0);

s/0/SLIBA_IH_ALL or something like that?

arch/powerpc/mm/book3s64/slb.c void switch_slb(struct task_struct *tsk, struct mm_struct *mm) · line 466

Quoted 12 lines
arch/powerpc/mm/book3s64/slb.c +5 -0
+		isync();
+		get_paca()->slb_cache_ptr = 0;
+		get_paca()->slb_kern_bitmap = (1U << SLB_NUM_BOLTED) - 1;
+
+	} else if (cpu_has_feature(CPU_FTR_ARCH_300)) {
 		/*
 		 * SLBIA IH=3 invalidates all Class=1 SLBEs and their
 		 * associated lookaside structures, which matches what
@@ -423,47 +464,36 @@ void switch_slb(struct task_struct *tsk, struct mm_struct *mm)
 		 * cache.
 		 */
 		asm volatile(PPC_SLBIA(3));

-aneesh

#2 lore raw

Re: [PATCH 2/2] powerpc/64s/hash: add torture_hpt kernel boot option to increase hash faults

In reply to Nicholas Piggin
Quoted 4 lines · Nicholas Piggin

This option increases the number of hash misses by limiting the number of kernel HPT entries. This helps stress test difficult to hit paths in the kernel.

It would nice if we can explain in commit message how we are limiting the number of HPT entries.

Quoted 8 lines
---
 .../admin-guide/kernel-parameters.txt         |  9 +++
 arch/powerpc/include/asm/book3s/64/mmu-hash.h | 10 +++
 arch/powerpc/mm/book3s64/hash_4k.c            |  3 +
 arch/powerpc/mm/book3s64/hash_64k.c           |  8 +++
 arch/powerpc/mm/book3s64/hash_utils.c         | 66 ++++++++++++++++++-
 5 files changed, 95 insertions(+), 1 deletion(-)

....

Quoted 25 lines
arch/powerpc/mm/book3s64/hash_utils.c +25 -0
+void hpt_do_torture(unsigned long ea, unsigned long access,
+		    unsigned long rflags, unsigned long hpte_group)
+{
+	unsigned long last_group;
+	int cpu = raw_smp_processor_id();
+
+	last_group = torture_hpt_last_group[cpu];
+	if (last_group != -1UL) {
+		while (mmu_hash_ops.hpte_remove(last_group) != -1)
+			;
+		torture_hpt_last_group[cpu] = -1UL;
+	}
+
+#define QEMU_WORKAROUND	0
+
+	if (ea >= PAGE_OFFSET) {
+		if (!QEMU_WORKAROUND && (access & (_PAGE_READ|_PAGE_WRITE)) &&
+		    !(rflags & (HPTE_R_I|HPTE_R_G))) {
+			/* prefetch / prefetchw does not seem to set up a TLB
+			 * entry with the powerpc systemsim (mambo) emulator,
+			 * though it works with real hardware. An alternative
+			 * approach that would work more reliably on quirky
+			 * emulators like QEMU may be to remember the last
+			 * insertion and remove that, rather than removing the
+			 * current insertion. Then no prefetch is required.

arch/powerpc/mm/book3s64/hash_utils.c long hpte_insert_repeating(unsigned long hash, unsigned long vpn, · line 1959

Quoted 10 lines
arch/powerpc/mm/book3s64/hash_utils.c +10 -0
+			 */
+			if ((access & _PAGE_WRITE) && (access & _PAGE_READ))
+				atomic_add(0, (atomic_t *)(ea & ~0x3));
+			else if (access & _PAGE_READ)
+				*(volatile char *)ea;
+
+			mb();
+
+			while (mmu_hash_ops.hpte_remove(hpte_group) != -1)
+				;

Do we get similar hpte faults rate, if we remove everything except the current inserted entry?. If so that would largely simplify the code.

Quoted 13 lines
arch/powerpc/mm/book3s64/hash_utils.c +8 -0
+		} else {
+			/* Can't prefetch cache-inhibited so clear next time. */
+			torture_hpt_last_group[cpu] = hpte_group;
+		}
+	}
+}
+
+
 #ifdef CONFIG_DEBUG_PAGEALLOC
 static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
 {
Signature · 1 line
2.23.0

-aneesh

author reviewer
#3 lore raw

Re: [PATCH 2/2] powerpc/64s/hash: add torture_hpt kernel boot option to increase hash faults

Excerpts from Aneesh Kumar K.V's message of May 4, 2020 5:36 pm:

Quoted 9 lines
Quoted 4 lines · Nicholas Piggin

This option increases the number of hash misses by limiting the number of kernel HPT entries. This helps stress test difficult to hit paths in the kernel.

It would nice if we can explain in commit message how we are limiting the number of HPT entries.

"limiting the number of kernel HPT entries by removing them as soon as possible after they are installed"?

Quoted 43 lines
Quoted 9 lines
---
 .../admin-guide/kernel-parameters.txt         |  9 +++
 arch/powerpc/include/asm/book3s/64/mmu-hash.h | 10 +++
 arch/powerpc/mm/book3s64/hash_4k.c            |  3 +
 arch/powerpc/mm/book3s64/hash_64k.c           |  8 +++
 arch/powerpc/mm/book3s64/hash_utils.c         | 66 ++++++++++++++++++-
 5 files changed, 95 insertions(+), 1 deletion(-)

....

Quoted 30 lines
arch/powerpc/mm/book3s64/hash_utils.c +30 -0
+void hpt_do_torture(unsigned long ea, unsigned long access,
+		    unsigned long rflags, unsigned long hpte_group)
+{
+	unsigned long last_group;
+	int cpu = raw_smp_processor_id();
+
+	last_group = torture_hpt_last_group[cpu];
+	if (last_group != -1UL) {
+		while (mmu_hash_ops.hpte_remove(last_group) != -1)
+			;
+		torture_hpt_last_group[cpu] = -1UL;
+	}
+
+#define QEMU_WORKAROUND	0
+
+	if (ea >= PAGE_OFFSET) {
+		if (!QEMU_WORKAROUND && (access & (_PAGE_READ|_PAGE_WRITE)) &&
+		    !(rflags & (HPTE_R_I|HPTE_R_G))) {
+			/* prefetch / prefetchw does not seem to set up a TLB
+			 * entry with the powerpc systemsim (mambo) emulator,
+			 * though it works with real hardware. An alternative
+			 * approach that would work more reliably on quirky
+			 * emulators like QEMU may be to remember the last
+			 * insertion and remove that, rather than removing the
+			 * current insertion. Then no prefetch is required.
+			 */
+			if ((access & _PAGE_WRITE) && (access & _PAGE_READ))
+				atomic_add(0, (atomic_t *)(ea & ~0x3));
+			else if (access & _PAGE_READ)
+				*(volatile char *)ea;
Quoted 8 lines

arch/powerpc/mm/book3s64/hash_utils.c long hpte_insert_repeating(unsigned long hash, unsigned long vpn, · line 1959

Quoted 5 lines
arch/powerpc/mm/book3s64/hash_utils.c +5 -0
+
+			mb();
+
+			while (mmu_hash_ops.hpte_remove(hpte_group) != -1)
+				;

Do we get similar hpte faults rate, if we remove everything except the current inserted entry?. If so that would largely simplify the code.

Well it would remove this one branch at least. It does actually help cause more faults and helps (in theory) irritate cases where you have two accesses to a vmalloc page in the kernel, where the first is okay to take a fault but the second is buggy.

I actually like the interesting behaviour it exposes in emulators too. We should really fix mambo to have prefetches bring in TLBs, and fix qemu to bring in TLBs more like hardware, and it could have TLB vs PTE consistency checks to catch bugs like mambo does.

So I prefer leaving this in.

Thanks,
Nick

author reviewer
#5 lore raw
In reply to Aneesh Kumar K.V

Excerpts from Aneesh Kumar K.V's message of May 4, 2020 5:27 pm:

Quoted 10 lines
Quoted 5 lines · Nicholas Piggin

This option increases the number of SLB misses by limiting the number of kernel SLB entries, and increased flushing of cached lookaside information. This helps stress test difficult to hit paths in the kernel.

....

Quoted 16 lines
Quoted 4 lines
arch/powerpc/mm/book3s64/slb.c +4 -0
+{
+	unsigned long slbie_data = get_paca()->slb_cache[index];
+	unsigned long ksp = get_paca()->kstack;
+

arch/powerpc/mm/book3s64/slb.c void preload_new_slb_context(unsigned long start, unsigned long sp) · line 428

Quoted 10 lines
arch/powerpc/mm/book3s64/slb.c +10 -0
+	slbie_data <<= SID_SHIFT;
+	slbie_data |= 0xc000000000000000ULL;
+	if ((ksp & slb_esid_mask(mmu_kernel_ssize)) == slbie_data)
+		return;
+	slbie_data |= mmu_kernel_ssize << SLBIE_SSIZE_SHIFT;
+
+	asm volatile("slbie %0" : : "r" (slbie_data));
+}
+
+static void slb_cache_slbie(unsigned int index)

May be slb_cache_slbie_user()? Similar to _kernel above?

Yeah that'd help.

Quoted 10 lines
Quoted 10 lines
arch/powerpc/mm/book3s64/slb.c +9 -0
+{
+	unsigned long slbie_data = get_paca()->slb_cache[index];
+
+	slbie_data <<= SID_SHIFT;
+	slbie_data |= user_segment_size(slbie_data) << SLBIE_SSIZE_SHIFT;
+	slbie_data |= SLBIE_C; /* user slbs have C=1 */
+
+	asm volatile("slbie %0" : : "r" (slbie_data));
+}
Quoted 13 lines

arch/powerpc/mm/book3s64/slb.c void switch_slb(struct task_struct *tsk, struct mm_struct *mm) · line 454

Quoted 11 lines

/* Flush all user entries from the segment table of the current processor. */ void switch_slb(struct task_struct *tsk, struct mm_struct *mm)

arch/powerpc/mm/book3s64/slb.c +3 -2
@@ -414,8 +449,14 @@ void switch_slb(struct task_struct *tsk, struct mm_struct *mm)
 	 * which would update the slb_cache/slb_cache_ptr fields in the PACA.
 	 */
 	hard_irq_disable();
-	asm volatile("isync" : : : "memory");
-	if (cpu_has_feature(CPU_FTR_ARCH_300)) {
+	isync();
+	if (torture_slb()) {
+		__slb_flush_and_restore_bolted(0);

s/0/SLIBA_IH_ALL or something like that?

IH isn't so simple. 0 isn't all, it's clear all SLBE except index zero, and flush all EA lookaside data.

Maybe callers should use a bool parameter though to flush kernel lookaside data.

Thanks,
Nick