Skip to contentSkip to conversation

parisc: Optimize per-pagetable spinlocks (v11)

PATCH In review Helge Deller 4 participants 15 messages Jan 27, 2021 – Feb 11, 2021 10 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

Helge Deller

author maintainer
#10 lore raw

PATCH 1/1 parisc: Optimize per-pagetable spinlocks (v11)

On parisc a spinlock is stored in the next page behind the pgd which protects against parallel accesses to the pgd. That's why one additional page (PGD_ALLOC_ORDER) is allocated for the pgd.

Matthew Wilcox suggested that we instead should use a pointer in the struct page table for this spinlock and noted, that the comments for the PGD_ORDER and PMD_ORDER defines were wrong.

Both suggestions are addressed in this patch. The pgd spinlock (parisc_pgd_lock) is stored in the struct page table. In switch_mm_irqs_off() the physical address of this lock is loaded into cr28 (tr4) and the pgd into cr25, so that the fault handlers can directly access the lock.

The currently implemened Hybrid L2/L3 page table scheme (where the pmd is adjacent to the pgd) is dropped now too.

Fixes b37d1c1898b2 ("parisc: Use per-pagetable spinlock")
arch/parisc/include/asm/mmu_context.h +9 -0
index 46f8c22c5977..3e02b1f75e54 100644
@@ -5,6 +5,7 @@
 #include <linux/mm.h>
 #include <linux/sched.h>
 #include <linux/atomic.h>
+#include <linux/spinlock.h>
 #include <asm-generic/mm_hooks.h>
 
 /* on PA-RISC, we actually have enough contexts to justify an allocator
@@ -50,6 +51,14 @@ static inline void switch_mm_irqs_off(struct mm_struct *prev,
 		struct mm_struct *next, struct task_struct *tsk)
 {
 	if (prev != next) {
+#ifdef CONFIG_SMP
+		/* phys address of tlb lock in cr28 (tr4) for TLB faults */
+		struct page *page;
+
+		page = virt_to_page((unsigned long) next->pgd);
+		/* BUG_ON(!page->parisc_pgd_lock); */
+		mtctl(__pa(__ldcw_align(&page->parisc_pgd_lock->rlock.raw_lock)), 28);
+#endif
 		mtctl(__pa(next->pgd), 25);
 		load_context(next->context);
 	}
arch/parisc/include/asm/page.h +1 -1
index 6b3f6740a6a6..d00313d1274e 100644
@@ -112,7 +112,7 @@ extern int npmem_ranges;
 #else
 #define BITS_PER_PTE_ENTRY	2
 #define BITS_PER_PMD_ENTRY	2
-#define BITS_PER_PGD_ENTRY	BITS_PER_PMD_ENTRY
+#define BITS_PER_PGD_ENTRY	2
 #endif
 #define PGD_ENTRY_SIZE	(1UL << BITS_PER_PGD_ENTRY)
 #define PMD_ENTRY_SIZE	(1UL << BITS_PER_PMD_ENTRY)
arch/parisc/include/asm/pgalloc.h +39 -57

129 lines not on this page · raw message

arch/parisc/include/asm/pgtable.h +31 -52

175 lines not on this page · raw message

arch/parisc/kernel/asm-offsets.c +0 -1
index 305768a40773..cd2cc1b1648c 100644
@@ -268,7 +268,6 @@ int main(void)
 	DEFINE(ASM_BITS_PER_PGD, BITS_PER_PGD);
 	DEFINE(ASM_BITS_PER_PMD, BITS_PER_PMD);
 	DEFINE(ASM_BITS_PER_PTE, BITS_PER_PTE);
-	DEFINE(ASM_PGD_PMD_OFFSET, -(PAGE_SIZE << PGD_ORDER));
 	DEFINE(ASM_PMD_ENTRY, ((PAGE_OFFSET & PMD_MASK) >> PMD_SHIFT));
 	DEFINE(ASM_PGD_ENTRY, PAGE_OFFSET >> PGDIR_SHIFT);
 	DEFINE(ASM_PGD_ENTRY_SIZE, PGD_ENTRY_SIZE);
arch/parisc/kernel/entry.S +12 -26
index beba9816cc6c..538114d81760 100644
@@ -35,10 +35,9 @@
 	.level 2.0
 #endif
 
-	.import		pa_tlb_lock,data
-	.macro  load_pa_tlb_lock reg
-	mfctl		%cr25,\reg
-	addil		L%(PAGE_SIZE << (PGD_ALLOC_ORDER - 1)),\reg
+	/* Load parisc_pgd_lock address from cr28/tr4 for this pgd (cr25) */
+	.macro  load_pgd_spinlock reg
+	mfctl	%cr28,\reg
 	.endm
 
 	/* space_to_prot macro creates a prot id from a space id */
@@ -407,7 +406,9 @@
 # endif
 #endif
 	dep             %r0,31,PAGE_SHIFT,\pmd  /* clear offset */
+#if CONFIG_PGTABLE_LEVELS < 3
 	copy		%r0,\pte
+#endif
 	ldw,s		\index(\pmd),\pmd
 	bb,>=,n		\pmd,_PxD_PRESENT_BIT,\fault
 	dep		%r0,31,PxD_FLAG_SHIFT,\pmd /* clear flags */
@@ -417,29 +418,14 @@
 	shladd		\index,BITS_PER_PTE_ENTRY,\pmd,\pmd /* pmd is now pte */
 	.endm
 
-	/* Look up PTE in a 3-Level scheme.
-	 *
-	 * Here we implement a Hybrid L2/L3 scheme: we allocate the
-	 * first pmd adjacent to the pgd.  This means that we can
-	 * subtract a constant offset to get to it.  The pmd and pgd
-	 * sizes are arranged so that a single pmd covers 4GB (giving
-	 * a full LP64 process access to 8TB) so our lookups are
-	 * effectively L2 for the first 4GB of the kernel (i.e. for
-	 * all ILP32 processes and all the kernel for machines with
-	 * under 4GB of memory) */
+	/* Look up PTE in a 3-Level scheme. */
 	.macro		L3_ptep pgd,pte,index,va,fault
-#if CONFIG_PGTABLE_LEVELS == 3 /* we might have a 2-Level scheme, e.g. with 16kb page size */
+#if CONFIG_PGTABLE_LEVELS == 3
+	copy		%r0,\pte
 	extrd,u		\va,63-ASM_PGDIR_SHIFT,ASM_BITS_PER_PGD,\index
-	extrd,u,*=	\va,63-ASM_PGDIR_SHIFT,64-ASM_PGDIR_SHIFT,%r0
 	ldw,s		\index(\pgd),\pgd
-	extrd,u,*=	\va,63-ASM_PGDIR_SHIFT,64-ASM_PGDIR_SHIFT,%r0
 	bb,>=,n		\pgd,_PxD_PRESENT_BIT,\fault
-	extrd,u,*=	\va,63-ASM_PGDIR_SHIFT,64-ASM_PGDIR_SHIFT,%r0
-	shld		\pgd,PxD_VALUE_SHIFT,\index
-	extrd,u,*=	\va,63-ASM_PGDIR_SHIFT,64-ASM_PGDIR_SHIFT,%r0
-	copy		\index,\pgd
-	extrd,u,*<>	\va,63-ASM_PGDIR_SHIFT,64-ASM_PGDIR_SHIFT,%r0
-	ldo		ASM_PGD_PMD_OFFSET(\pgd),\pgd
+	shld		\pgd,PxD_VALUE_SHIFT,\pgd
 #endif
 	L2_ptep		\pgd,\pte,\index,\va,\fault
 	.endm
@@ -448,7 +434,7 @@
 	.macro		tlb_lock	spc,ptp,pte,tmp,tmp1,fault
 #ifdef CONFIG_SMP
 98:	cmpib,COND(=),n	0,\spc,2f
-	load_pa_tlb_lock \tmp
+	load_pgd_spinlock \tmp
 1:	LDCW		0(\tmp),\tmp1
 	cmpib,COND(=)	0,\tmp1,1b
 	nop
@@ -480,9 +466,9 @@
 	/* Release pa_tlb_lock lock. */
 	.macro		tlb_unlock1	spc,tmp
 #ifdef CONFIG_SMP
-98:	load_pa_tlb_lock \tmp
-99:	ALTERNATIVE(98b, 99b, ALT_COND_NO_SMP, INSN_NOP)
+98:	load_pgd_spinlock \tmp
 	tlb_unlock0	\spc,\tmp
+99:	ALTERNATIVE(98b, 99b, ALT_COND_NO_SMP, INSN_NOP)
 #endif
 	.endm
 
arch/parisc/mm/init.c +19 -0
index 3ec633b11b54..4f3f180b0b20 100644
@@ -681,6 +681,24 @@ static void __init parisc_bootmem_free(void)
 	free_area_init(max_zone_pfn);
 }
 
+static void __init parisc_init_pgd_lock(void)
+{
+	struct page *page;
+
+	page = virt_to_page((unsigned long) &swapper_pg_dir);
+	page->parisc_pgd_lock = &pa_swapper_pg_lock;
+}
+
+#ifdef CONFIG_SMP
+spinlock_t *pgd_spinlock(pgd_t *pgd)
+{
+	struct page *page;
+
+	page = virt_to_page((unsigned long) pgd);
+	return page->parisc_pgd_lock;
+}
+#endif
+
 void __init paging_init(void)
 {
 	setup_bootmem();
@@ -691,6 +709,7 @@ void __init paging_init(void)
 
 	sparse_init();
 	parisc_bootmem_free();
+	parisc_init_pgd_lock();
 }
 
 #ifdef CONFIG_PA20
include/linux/mm_types.h +1 -0
index 07d9acb5b19c..d8520af04b7d 100644
@@ -150,6 +150,7 @@ struct page {
 			union {
 				struct mm_struct *pt_mm; /* x86 pgds only */
 				atomic_t pt_frag_refcount; /* powerpc */
+				spinlock_t *parisc_pgd_lock; /* parisc pgds only */
 			};
 #if ALLOC_SPLIT_PTLOCKS
 			spinlock_t *ptl;
#11 lore raw

Am Mittwoch, 27. Januar 2021, 22:18:51 CET schrieb Helge Deller:

Quoted 36 lines

On parisc a spinlock is stored in the next page behind the pgd which protects against parallel accesses to the pgd. That's why one additional page (PGD_ALLOC_ORDER) is allocated for the pgd.

Matthew Wilcox suggested that we instead should use a pointer in the struct page table for this spinlock and noted, that the comments for the PGD_ORDER and PMD_ORDER defines were wrong.

Both suggestions are addressed in this patch. The pgd spinlock (parisc_pgd_lock) is stored in the struct page table. In switch_mm_irqs_off() the physical address of this lock is loaded into cr28 (tr4) and the pgd into cr25, so that the fault handlers can directly access the lock.

The currently implemened Hybrid L2/L3 page table scheme (where the pmd is adjacent to the pgd) is dropped now too.

Fixes b37d1c1898b2 ("parisc: Use per-pagetable spinlock")
(quoted diff) +0 -0

b/arch/parisc/include/asm/mmu_context.h index 46f8c22c5977..3e02b1f75e54 100644

arch/parisc/include/asm/mmu_context.h +1 -0
@@ -5,6 +5,7 @@
 #include <linux/mm.h>
 #include <linux/sched.h>
 #include <linux/atomic.h>
+#include <linux/spinlock.h>
 #include <asm-generic/mm_hooks.h>
 
 /* on PA-RISC, we actually have enough contexts to justify an allocator

arch/parisc/include/asm/mmu_context.h static inline void switch_mm_irqs_off(struct mm_struct · line 55

Quoted 5 lines
arch/parisc/include/asm/mmu_context.h +2 -0
@@ -50,6 +51,14 @@ static inline void switch_mm_irqs_off(struct mm_struct
 *prev, struct mm_struct *next, struct task_struct *tsk)
 {
 	if (prev != next) {
+#ifdef CONFIG_SMP
+		/* phys address of tlb lock in cr28 (tr4) for TLB faults 

*/

arch/parisc/include/asm/mmu_context.h static inline void switch_mm_irqs_off(struct mm_struct *prev, · line 58

Quoted 3 lines
arch/parisc/include/asm/mmu_context.h +3 -0
+		struct page *page;
+
+		page = virt_to_page((unsigned long) next->pgd);

This is one of the few cases you have a space after the cast.

Another one is in pgd_alloc():

Quoted 11 lines
arch/parisc/include/asm/mmu_context.h +1 -0
+       page = virt_to_page((unsigned long) pgd);
 
(quoted diff) +0 -0

b/arch/parisc/include/asm/pgalloc.h index a6482b2ce0ea..9c1a54543c87 100644

arch/parisc/include/asm/pgalloc.h +0 -0
@@ -68,43 +66,27 @@ static inline void pud_populate(struct mm_struct *mm,

pud_t *pud, pmd_t *pmd) (__u32)(__pa((unsigned long)pmd) >> PxD_VALUE_SHIFT)));
}

Quoted 4 lines

-static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address)
+static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr)

Does that change add benefit?

arch/parisc/include/asm/pgalloc.h static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd) · line 74

Quoted 6 lines

{

arch/parisc/include/asm/pgalloc.h +4 -1
-	return (pmd_t *)__get_free_pages(GFP_PGTABLE_KERNEL, PMD_ORDER);
+	pmd_t *pmd;
+
+	pmd = (pmd_t *)__get_free_pages(GFP_PGTABLE_KERNEL, PMD_ORDER);
+	if (pmd)

Maybe annotate that as likely() as it was in pgd_alloc() before?

arch/parisc/include/asm/pgtable.h static inline void purge_tlb_entries(struct mm_struct · line 96

Quoted 10 lines
arch/parisc/include/asm/pgalloc.h +2 -0
+		memset ((void *)pmd, 0, PAGE_SIZE << PMD_ORDER);
+	return pmd;
 }
(quoted diff) +0 -0

b/arch/parisc/include/asm/pgtable.h index 75cf84070fc9..c08c7b37e5f4 100644

arch/parisc/include/asm/pgtable.h +0 -0
@@ -94,10 +96,12 @@ static inline void purge_tlb_entries(struct mm_struct
 *mm, unsigned long addr) #define set_pte_at(mm, addr, ptep, pteval)		
	\

arch/parisc/include/asm/pgtable.h static inline void purge_tlb_entries(struct mm_struct · line 97

Quoted 1 line
arch/parisc/include/asm/pgtable.h +0 -0
 	do {							

\

arch/parisc/include/asm/pgtable.h static inline void purge_tlb_entries(struct mm_struct · line 98

Quoted 1 line
arch/parisc/include/asm/pgtable.h +0 -0
 		unsigned long flags;				

\

arch/parisc/include/asm/pgtable.h static inline void purge_tlb_entries(struct mm_struct · line 99

Quoted 2 lines
arch/parisc/include/asm/pgtable.h +1 -1
-		spin_lock_irqsave(pgd_spinlock((mm)->pgd), flags);\
+		spinlock_t *pgd_lock;				

\

arch/parisc/include/asm/pgtable.h static inline void purge_tlb_entries(struct mm_struct *mm, unsigned long addr) · line 100

Quoted 1 line
arch/parisc/include/asm/pgtable.h +1 -0
+		pgd_lock = pgd_spinlock((mm)->pgd);		\

These should just fit into a single line.

arch/parisc/include/asm/pgtable.h static inline void purge_tlb_entries(struct mm_struct · line 102

Quoted 2 lines
arch/parisc/include/asm/pgtable.h +1 -0
+		spin_lock_irqsave(pgd_lock, flags);		\
 		set_pte(ptep, pteval);				

\

arch/parisc/include/asm/pgtable.h static inline void purge_tlb_entries(struct mm_struct · line 103

Quoted 1 line
arch/parisc/include/asm/pgtable.h +0 -0
 		purge_tlb_entries(mm, addr);			

\

Quoted 11 lines
arch/parisc/include/asm/pgtable.h +1 -1
-		spin_unlock_irqrestore(pgd_spinlock((mm)->pgd), flags);\
+		spin_unlock_irqrestore(pgd_lock, flags);	\
 	} while (0)
 
 #endif /* !__ASSEMBLY__ */
arch/parisc/mm/init.c +0 -0
index 3ec633b11b54..4f3f180b0b20 100644

arch/parisc/mm/init.c static void __init parisc_bootmem_free(void) · line 688

Quoted 8 lines
arch/parisc/mm/init.c +5 -0
@@ -681,6 +681,24 @@ static void __init parisc_bootmem_free(void)
 	free_area_init(max_zone_pfn);
 }
 
+static void __init parisc_init_pgd_lock(void)
+{
+	struct page *page;
+
+	page = virt_to_page((unsigned long) &swapper_pg_dir);

Another space.

arch/parisc/mm/init.c static void __init parisc_bootmem_free(void) · line 700

Quoted 12 lines
arch/parisc/mm/init.c +12 -0
+	page->parisc_pgd_lock = &pa_swapper_pg_lock;
+}
+
+#ifdef CONFIG_SMP
+spinlock_t *pgd_spinlock(pgd_t *pgd)
+{
+	struct page *page;
+
+	page = virt_to_page((unsigned long) pgd);
+	return page->parisc_pgd_lock;
+}
+#endif

This is very simple, and I suspect it being called rather often. Wouldn't it make sense to make it inline?

Eike

author maintainer
#12 lore raw

Re: [PATCH] parisc: Optimize per-pagetable spinlocks (v12)

Quoted 23 lines · Rolf Eike Beer

Am Mittwoch, 27. Januar 2021, 22:18:51 CET schrieb Helge Deller:

Quoted 22 lines

On parisc a spinlock is stored in the next page behind the pgd which protects against parallel accesses to the pgd. That's why one additional page (PGD_ALLOC_ORDER) is allocated for the pgd.

Matthew Wilcox suggested that we instead should use a pointer in the struct page table for this spinlock and noted, that the comments for the PGD_ORDER and PMD_ORDER defines were wrong.

Both suggestions are addressed in this patch. The pgd spinlock (parisc_pgd_lock) is stored in the struct page table. In switch_mm_irqs_off() the physical address of this lock is loaded into cr28 (tr4) and the pgd into cr25, so that the fault handlers can directly access the lock.

The currently implemened Hybrid L2/L3 page table scheme (where the pmd is adjacent to the pgd) is dropped now too.

Fixes b37d1c1898b2 ("parisc: Use per-pagetable spinlock")

[...lots of suggestions by Rolf...]

Rolf, thanks a lot for your review.
I've addressed most of your suggestions and published a v12 patch here:
https://git.kernel.org/pub/scm/linux/kernel/git/deller/parisc-linux.git/log/?h=pgtable_spinlock-v12

Quoted 8 lines
Quoted 8 lines
arch/parisc/mm/init.c +4 -0
index 3ec633b11b54..4f3f180b0b20 100644
+#ifdef CONFIG_SMP
+spinlock_t *pgd_spinlock(pgd_t *pgd)
+{
+	struct page *page;
Quoted 8 lines

arch/parisc/mm/init.c static void __init parisc_bootmem_free(void) · line 700

Quoted 5 lines
arch/parisc/mm/init.c +5 -0
+
+	page = virt_to_page((unsigned long) pgd);
+	return page->parisc_pgd_lock;
+}
+#endif

This is very simple, and I suspect it being called rather often. Wouldn't it make sense to make it inline?

No, it's not simple, that's why I haven't inlined it.
The virt_to_page() expands to many asm instructions based on the selected memory model.

Thanks!
Helge

#13 lore raw
In reply to Helge Deller
Quoted 14 lines · Helge Deller

On parisc a spinlock is stored in the next page behind the pgd which protects against parallel accesses to the pgd. That's why one additional page (PGD_ALLOC_ORDER) is allocated for the pgd.

Matthew Wilcox suggested that we instead should use a pointer in the struct page table for this spinlock and noted, that the comments for the PGD_ORDER and PMD_ORDER defines were wrong.

Both suggestions are addressed in this patch. The pgd spinlock (parisc_pgd_lock) is stored in the struct page table. In switch_mm_irqs_off() the physical address of this lock is loaded into cr28 (tr4) and the pgd into cr25, so that the fault handlers can directly access the lock.

Quoted 7 lines

The currently implemened Hybrid L2/L3 page table scheme (where the pmd is adjacent to the pgd) is dropped now too.

Fixes b37d1c1898b2 ("parisc: Use per-pagetable spinlock")

This patch results in:

BUG: spinlock recursion on CPU#0, swapper/0/1
 lock: 0x12226d14, .magic: dead4ead, .owner: swapper/0/1, .owner_cpu: 0
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.11.0-rc7-next-20210209-32bit #1
Hardware name: 9000/778/B160L
Backtrace:
 [<1019f9bc>] show_stack+0x34/0x48
 [<10a65278>] dump_stack+0x94/0x114
 [<10219f4c>] spin_dump+0x8c/0xb8
 [<1021a0b4>] do_raw_spin_lock+0xdc/0x108
 [<10a7367c>] _raw_spin_lock_irqsave+0x30/0x48
 [<102bf41c>] handle_mm_fault+0x5e8/0xdb0
 [<102b813c>] __get_user_pages.part.0+0x1b0/0x3d4
 [<102b8900>] __get_user_pages_remote+0x134/0x34c
 [<102b8b80>] get_user_pages_remote+0x68/0x90
 [<102fccb0>] get_arg_page+0x94/0xd8
 [<102fdd84>] copy_string_kernel+0xc4/0x234
 [<102fe70c>] kernel_execve+0xcc/0x1a4
 [<10a58d94>] run_init_process+0xbc/0xe0
 [<10a70d50>] kernel_init+0x98/0x13c
 [<1019a01c>] ret_from_kernel_thread+0x1c/0x24

when trying to boot parisc/hppa images in qemu. Reverting this patch fixes the problem.

Bitsect log attached.

Guenter

---
# bad: [a4bfd8d46ac357c12529e4eebb6c89502b03ecc9] Add linux-next specific files for 20210209
# good: [92bf22614b21a2706f4993b278017e437f7785b3] Linux 5.11-rc7
git bisect start 'HEAD' 'v5.11-rc7'
# bad: [a8eb921ba7e8e77d994a1c6c69c8ef08456ecf53] Merge remote-tracking branch 'crypto/master'
git bisect bad a8eb921ba7e8e77d994a1c6c69c8ef08456ecf53
# bad: [b68df186dae8ae890df08059bb068b78252b053a] Merge remote-tracking branch 'hid/for-next'
git bisect bad b68df186dae8ae890df08059bb068b78252b053a
# good: [323c9f6fb99b033883b404ecbc811e7b283a60b3] Merge remote-tracking branch 'sunxi/sunxi/for-next'
git bisect good 323c9f6fb99b033883b404ecbc811e7b283a60b3
# bad: [9d40a7a579a5c51fad0d734a4ed39e39b858fca2] Merge remote-tracking branch 'btrfs/for-next'
git bisect bad 9d40a7a579a5c51fad0d734a4ed39e39b858fca2
# bad: [afe0c3efe88f6c295542fd336d5f604115e9184f] Merge remote-tracking branch 'powerpc/next'
git bisect bad afe0c3efe88f6c295542fd336d5f604115e9184f
# good: [c276186556ed2ce6d30da69ce275234a7df85b09] Merge remote-tracking branch 'mips/mips-next'
git bisect good c276186556ed2ce6d30da69ce275234a7df85b09
# good: [755d664174463791489dddf34c33308b61de68c3] powerpc: DebugException remove args
git bisect good 755d664174463791489dddf34c33308b61de68c3
# good: [26418b36a11f2eaf2556aa8cefe86132907e311f] powerpc/64s/radix: refactor TLB flush type selection
git bisect good 26418b36a11f2eaf2556aa8cefe86132907e311f
# bad: [d7bbb31642d2bd4aa5aad3595061a5e96c32d91d] Merge remote-tracking branch 'parisc-hd/for-next'
git bisect bad d7bbb31642d2bd4aa5aad3595061a5e96c32d91d
# good: [2261352157a932717ec08b9dd18d1bfbb7c37c52] Merge remote-tracking branch 'openrisc/or1k-5.11-fixes' into or1k-5.12-updates git bisect good 2261352157a932717ec08b9dd18d1bfbb7c37c52
# good: [3c92b9eed3ae088ade3688fb356a90926c1c8ee4] Merge remote-tracking branch 'openrisc/for-next'
git bisect good 3c92b9eed3ae088ade3688fb356a90926c1c8ee4
# good: [accb4993d2ee6b644a3d01851cf2fb2c1e0813a6] parisc: Fix IVT checksum calculation wrt HPMC
git bisect good accb4993d2ee6b644a3d01851cf2fb2c1e0813a6
# bad: [4add5f175b1e4e71c06493f9a2c52490d2ea4365] parisc: Optimize per-pagetable spinlocks
git bisect bad 4add5f175b1e4e71c06493f9a2c52490d2ea4365
# good: [0d2d3836dd0a597e514e6231fbf2ae3944f5d38c] parisc: Bump 64-bit IRQ stack size to 64 KB
git bisect good 0d2d3836dd0a597e514e6231fbf2ae3944f5d38c

# first bad commit: [4add5f175b1e4e71c06493f9a2c52490d2ea4365] parisc: Optimize per-pagetable spinlocks
author maintainer
#14 lore raw
Quoted 46 lines · Guenter Roeck
Quoted 14 lines · Helge Deller

On parisc a spinlock is stored in the next page behind the pgd which protects against parallel accesses to the pgd. That's why one additional page (PGD_ALLOC_ORDER) is allocated for the pgd.

Matthew Wilcox suggested that we instead should use a pointer in the struct page table for this spinlock and noted, that the comments for the PGD_ORDER and PMD_ORDER defines were wrong.

Both suggestions are addressed in this patch. The pgd spinlock (parisc_pgd_lock) is stored in the struct page table. In switch_mm_irqs_off() the physical address of this lock is loaded into cr28 (tr4) and the pgd into cr25, so that the fault handlers can directly access the lock.

Quoted 7 lines

The currently implemened Hybrid L2/L3 page table scheme (where the pmd is adjacent to the pgd) is dropped now too.

Fixes b37d1c1898b2 ("parisc: Use per-pagetable spinlock")

This patch results in:

BUG: spinlock recursion on CPU#0, swapper/0/1
  lock: 0x12226d14, .magic: dead4ead, .owner: swapper/0/1, .owner_cpu: 0
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.11.0-rc7-next-20210209-32bit #1
Hardware name: 9000/778/B160L
Backtrace:
  [<1019f9bc>] show_stack+0x34/0x48
  [<10a65278>] dump_stack+0x94/0x114
  [<10219f4c>] spin_dump+0x8c/0xb8
  [<1021a0b4>] do_raw_spin_lock+0xdc/0x108
  [<10a7367c>] _raw_spin_lock_irqsave+0x30/0x48
  [<102bf41c>] handle_mm_fault+0x5e8/0xdb0
  [<102b813c>] __get_user_pages.part.0+0x1b0/0x3d4
  [<102b8900>] __get_user_pages_remote+0x134/0x34c
  [<102b8b80>] get_user_pages_remote+0x68/0x90
  [<102fccb0>] get_arg_page+0x94/0xd8
  [<102fdd84>] copy_string_kernel+0xc4/0x234
  [<102fe70c>] kernel_execve+0xcc/0x1a4
  [<10a58d94>] run_init_process+0xbc/0xe0
  [<10a70d50>] kernel_init+0x98/0x13c
  [<1019a01c>] ret_from_kernel_thread+0x1c/0x24
Quoted 2 lines

when trying to boot parisc/hppa images in qemu. Reverting this patch fixes the problem.

True, I can reproduce the problem.
With CONFIG_DEBUG_SPINLOCK=y you get the backtrace above.
With CONFIG_DEBUG_SPINLOCK=n it just hangs.
Happenes with 32-bit kernel with SMP kernel, even if only one virtual CPU is started.

By the way, the latest version of this patch is in my for-next tree, here:
https://git.kernel.org/pub/scm/linux/kernel/git/deller/parisc-linux.git/commit/?h=for-next&id=4add5f175b1e4e71c06493f9a2c52490d2ea4365
Problem happens with it too.

Helge

#15 lore raw
Quoted 47 lines · Helge Deller
Quoted 46 lines · Guenter Roeck
Quoted 14 lines · Helge Deller

On parisc a spinlock is stored in the next page behind the pgd which protects against parallel accesses to the pgd. That's why one additional page (PGD_ALLOC_ORDER) is allocated for the pgd.

Matthew Wilcox suggested that we instead should use a pointer in the struct page table for this spinlock and noted, that the comments for the PGD_ORDER and PMD_ORDER defines were wrong.

Both suggestions are addressed in this patch. The pgd spinlock (parisc_pgd_lock) is stored in the struct page table. In switch_mm_irqs_off() the physical address of this lock is loaded into cr28 (tr4) and the pgd into cr25, so that the fault handlers can directly access the lock.

Quoted 7 lines

The currently implemened Hybrid L2/L3 page table scheme (where the pmd is adjacent to the pgd) is dropped now too.

Fixes b37d1c1898b2 ("parisc: Use per-pagetable spinlock")

This patch results in:

BUG: spinlock recursion on CPU#0, swapper/0/1
  lock: 0x12226d14, .magic: dead4ead, .owner: swapper/0/1, .owner_cpu: 0
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.11.0-rc7-next-20210209-32bit #1
Hardware name: 9000/778/B160L
Backtrace:
  [<1019f9bc>] show_stack+0x34/0x48
  [<10a65278>] dump_stack+0x94/0x114
  [<10219f4c>] spin_dump+0x8c/0xb8
  [<1021a0b4>] do_raw_spin_lock+0xdc/0x108
  [<10a7367c>] _raw_spin_lock_irqsave+0x30/0x48
  [<102bf41c>] handle_mm_fault+0x5e8/0xdb0
  [<102b813c>] __get_user_pages.part.0+0x1b0/0x3d4
  [<102b8900>] __get_user_pages_remote+0x134/0x34c
  [<102b8b80>] get_user_pages_remote+0x68/0x90
  [<102fccb0>] get_arg_page+0x94/0xd8
  [<102fdd84>] copy_string_kernel+0xc4/0x234
  [<102fe70c>] kernel_execve+0xcc/0x1a4
  [<10a58d94>] run_init_process+0xbc/0xe0
  [<10a70d50>] kernel_init+0x98/0x13c
  [<1019a01c>] ret_from_kernel_thread+0x1c/0x24
Quoted 7 lines
Quoted 2 lines

when trying to boot parisc/hppa images in qemu. Reverting this patch fixes the problem.

True, I can reproduce the problem.
With CONFIG_DEBUG_SPINLOCK=y you get the backtrace above.
With  CONFIG_DEBUG_SPINLOCK=n it just hangs.
Happenes with 32-bit kernel with SMP kernel, even if only one virtual CPU is started.

Which is quite puzzling since most spin locks are optimized in this case case.  Strikes me we have a lock that's not initialized.

It's not obvious how this relates to the patch.  get_arg_page() calls get_user_pages_remote() with locked NULL:

       /*
         * We are doing an exec().  'current' is the process
         * doing the exec and bprm->mm is the new process's mm. */
        ret = get_user_pages_remote(bprm->mm, pos, 1, gup_flags, &page, NULL, NULL);

Dave

Signature · 1 line
John David Anglin  [email protected]
#16 lore raw
Quoted 51 lines · John David Anglin
Quoted 50 lines · Helge Deller
Quoted 48 lines · Guenter Roeck
Quoted 14 lines · Helge Deller

On parisc a spinlock is stored in the next page behind the pgd which protects against parallel accesses to the pgd. That's why one additional page (PGD_ALLOC_ORDER) is allocated for the pgd.

Matthew Wilcox suggested that we instead should use a pointer in the struct page table for this spinlock and noted, that the comments for the PGD_ORDER and PMD_ORDER defines were wrong.

Both suggestions are addressed in this patch. The pgd spinlock (parisc_pgd_lock) is stored in the struct page table. In switch_mm_irqs_off() the physical address of this lock is loaded into cr28 (tr4) and the pgd into cr25, so that the fault handlers can directly access the lock.

Quoted 7 lines

The currently implemened Hybrid L2/L3 page table scheme (where the pmd is adjacent to the pgd) is dropped now too.

Fixes b37d1c1898b2 ("parisc: Use per-pagetable spinlock")

This patch results in:

BUG: spinlock recursion on CPU#0, swapper/0/1
  lock: 0x12226d14, .magic: dead4ead, .owner: swapper/0/1, .owner_cpu: 0
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.11.0-rc7-next-20210209-32bit #1
Hardware name: 9000/778/B160L
Backtrace:
  [<1019f9bc>] show_stack+0x34/0x48
  [<10a65278>] dump_stack+0x94/0x114
  [<10219f4c>] spin_dump+0x8c/0xb8
  [<1021a0b4>] do_raw_spin_lock+0xdc/0x108
  [<10a7367c>] _raw_spin_lock_irqsave+0x30/0x48
  [<102bf41c>] handle_mm_fault+0x5e8/0xdb0
  [<102b813c>] __get_user_pages.part.0+0x1b0/0x3d4
  [<102b8900>] __get_user_pages_remote+0x134/0x34c
  [<102b8b80>] get_user_pages_remote+0x68/0x90
  [<102fccb0>] get_arg_page+0x94/0xd8
  [<102fdd84>] copy_string_kernel+0xc4/0x234
  [<102fe70c>] kernel_execve+0xcc/0x1a4
  [<10a58d94>] run_init_process+0xbc/0xe0
  [<10a70d50>] kernel_init+0x98/0x13c
  [<1019a01c>] ret_from_kernel_thread+0x1c/0x24

when trying to boot parisc/hppa images in qemu. Reverting this patch fixes the problem.

Quoted 8 lines
Quoted 4 lines

True, I can reproduce the problem.
With CONFIG_DEBUG_SPINLOCK=y you get the backtrace above.
With  CONFIG_DEBUG_SPINLOCK=n it just hangs.
Happenes with 32-bit kernel with SMP kernel, even if only one virtual CPU is started.

Which is quite puzzling since most spin locks are optimized in this case case.  Strikes me we have a lock that's not initialized.

It's not obvious how this relates to the patch.  get_arg_page() calls get_user_pages_remote() with

The fact that reverting it fixes the problem is a good indication that the problem does relate to this patch.

As for how - no idea. That is not my area of expertise.

Thanks,
Guenter

#17 lore raw
Quoted 54 lines · Guenter Roeck
Quoted 53 lines · John David Anglin
Quoted 52 lines · Helge Deller
Quoted 45 lines · Guenter Roeck
Quoted 14 lines · Helge Deller

On parisc a spinlock is stored in the next page behind the pgd which protects against parallel accesses to the pgd. That's why one additional page (PGD_ALLOC_ORDER) is allocated for the pgd.

Matthew Wilcox suggested that we instead should use a pointer in the struct page table for this spinlock and noted, that the comments for the PGD_ORDER and PMD_ORDER defines were wrong.

Both suggestions are addressed in this patch. The pgd spinlock (parisc_pgd_lock) is stored in the struct page table. In switch_mm_irqs_off() the physical address of this lock is loaded into cr28 (tr4) and the pgd into cr25, so that the fault handlers can directly access the lock.

Quoted 7 lines

The currently implemened Hybrid L2/L3 page table scheme (where the pmd is adjacent to the pgd) is dropped now too.

Fixes b37d1c1898b2 ("parisc: Use per-pagetable spinlock")

This patch results in:

BUG: spinlock recursion on CPU#0, swapper/0/1
  lock: 0x12226d14, .magic: dead4ead, .owner: swapper/0/1, .owner_cpu: 0
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.11.0-rc7-next-20210209-32bit #1
Hardware name: 9000/778/B160L
Backtrace:
  [<1019f9bc>] show_stack+0x34/0x48
  [<10a65278>] dump_stack+0x94/0x114
  [<10219f4c>] spin_dump+0x8c/0xb8
  [<1021a0b4>] do_raw_spin_lock+0xdc/0x108
  [<10a7367c>] _raw_spin_lock_irqsave+0x30/0x48
  [<102bf41c>] handle_mm_fault+0x5e8/0xdb0
  [<102b813c>] __get_user_pages.part.0+0x1b0/0x3d4
  [<102b8900>] __get_user_pages_remote+0x134/0x34c
  [<102b8b80>] get_user_pages_remote+0x68/0x90
  [<102fccb0>] get_arg_page+0x94/0xd8
  [<102fdd84>] copy_string_kernel+0xc4/0x234
  [<102fe70c>] kernel_execve+0xcc/0x1a4
  [<10a58d94>] run_init_process+0xbc/0xe0
  [<10a70d50>] kernel_init+0x98/0x13c
  [<1019a01c>] ret_from_kernel_thread+0x1c/0x24
Quoted 2 lines

when trying to boot parisc/hppa images in qemu. Reverting this patch fixes the problem.

True, I can reproduce the problem.
With CONFIG_DEBUG_SPINLOCK=y you get the backtrace above.
With  CONFIG_DEBUG_SPINLOCK=n it just hangs.
Happenes with 32-bit kernel with SMP kernel, even if only one virtual CPU is started.

Quoted 8 lines
Quoted 4 lines

Which is quite puzzling since most spin locks are optimized in this case case.  Strikes me we have a lock that's not initialized.

It's not obvious how this relates to the patch.  get_arg_page() calls get_user_pages_remote() with

The fact that reverting it fixes the problem is a good indication that the problem does relate to this patch.

As for how - no idea. That is not my area of expertise.

I built Helge's for-next tree both with CONFIG_DEBUG_SPINLOCK=y and CONFIG_DEBUG_SPINLOCK=n.  Both builds work fine on c8000.

The only thing that might have changed in the patch is the alignment of the lock used for page table updates. Qemu only support PA 1.x instructions.  The ldcw instruction needs 16-byte alignment on real hardware and there is code to dynamically align lock pointers to 16-byte alignment.  The c8000 supports PA 2.0 instructions and the ldcw,co instruction only needs 4-byte alignment.  Perhaps there is an issue with the dynamic alignment of the lock pointer or the lock initialization in the PA 1.x build for qemu.

Signature · 1 line
John David Anglin  [email protected]
#18 lore raw
Quoted 63 lines · John David Anglin
Quoted 54 lines · Guenter Roeck
Quoted 53 lines · John David Anglin
Quoted 52 lines · Helge Deller
Quoted 45 lines · Guenter Roeck
Quoted 14 lines · Helge Deller

On parisc a spinlock is stored in the next page behind the pgd which protects against parallel accesses to the pgd. That's why one additional page (PGD_ALLOC_ORDER) is allocated for the pgd.

Matthew Wilcox suggested that we instead should use a pointer in the struct page table for this spinlock and noted, that the comments for the PGD_ORDER and PMD_ORDER defines were wrong.

Both suggestions are addressed in this patch. The pgd spinlock (parisc_pgd_lock) is stored in the struct page table. In switch_mm_irqs_off() the physical address of this lock is loaded into cr28 (tr4) and the pgd into cr25, so that the fault handlers can directly access the lock.

Quoted 7 lines

The currently implemened Hybrid L2/L3 page table scheme (where the pmd is adjacent to the pgd) is dropped now too.

Fixes b37d1c1898b2 ("parisc: Use per-pagetable spinlock")

This patch results in:

BUG: spinlock recursion on CPU#0, swapper/0/1
  lock: 0x12226d14, .magic: dead4ead, .owner: swapper/0/1, .owner_cpu: 0
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.11.0-rc7-next-20210209-32bit #1
Hardware name: 9000/778/B160L
Backtrace:
  [<1019f9bc>] show_stack+0x34/0x48
  [<10a65278>] dump_stack+0x94/0x114
  [<10219f4c>] spin_dump+0x8c/0xb8
  [<1021a0b4>] do_raw_spin_lock+0xdc/0x108
  [<10a7367c>] _raw_spin_lock_irqsave+0x30/0x48
  [<102bf41c>] handle_mm_fault+0x5e8/0xdb0
  [<102b813c>] __get_user_pages.part.0+0x1b0/0x3d4
  [<102b8900>] __get_user_pages_remote+0x134/0x34c
  [<102b8b80>] get_user_pages_remote+0x68/0x90
  [<102fccb0>] get_arg_page+0x94/0xd8
  [<102fdd84>] copy_string_kernel+0xc4/0x234
  [<102fe70c>] kernel_execve+0xcc/0x1a4
  [<10a58d94>] run_init_process+0xbc/0xe0
  [<10a70d50>] kernel_init+0x98/0x13c
  [<1019a01c>] ret_from_kernel_thread+0x1c/0x24
Quoted 2 lines

when trying to boot parisc/hppa images in qemu. Reverting this patch fixes the problem.

True, I can reproduce the problem.
With CONFIG_DEBUG_SPINLOCK=y you get the backtrace above.
With  CONFIG_DEBUG_SPINLOCK=n it just hangs.
Happenes with 32-bit kernel with SMP kernel, even if only one virtual CPU is started.

Quoted 8 lines
Quoted 4 lines

Which is quite puzzling since most spin locks are optimized in this case case.  Strikes me we have a lock that's not initialized.

It's not obvious how this relates to the patch.  get_arg_page() calls get_user_pages_remote() with

The fact that reverting it fixes the problem is a good indication that the problem does relate to this patch.

As for how - no idea. That is not my area of expertise.

Quoted 9 lines

I built Helge's for-next tree both with CONFIG_DEBUG_SPINLOCK=y and CONFIG_DEBUG_SPINLOCK=n.  Both builds work fine on c8000.

The only thing that might have changed in the patch is the alignment of the lock used for page table updates. Qemu only support PA 1.x instructions.  The ldcw instruction needs 16-byte alignment on real hardware and there is code to dynamically align lock pointers to 16-byte alignment.  The c8000 supports PA 2.0 instructions and the ldcw,co instruction only needs 4-byte alignment.  Perhaps there is an issue with the dynamic alignment of the lock pointer or the lock initialization in the PA 1.x build for qemu.

The first lock is acquired in mm/memory.c from line 3565:

        vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
                        &vmf->ptl);

The second (recursive) lock is acquired from line 3587 in the same function:

        set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);

Source code lines are from next-20210211. I confirmed with debug code that the lock address passed to do_raw_spin_lock() is the same in both calls.

Guenter

#19 lore raw
Quoted 78 lines · Guenter Roeck
Quoted 63 lines · John David Anglin
Quoted 54 lines · Guenter Roeck
Quoted 53 lines · John David Anglin
Quoted 52 lines · Helge Deller
Quoted 45 lines · Guenter Roeck
Quoted 14 lines · Helge Deller

On parisc a spinlock is stored in the next page behind the pgd which protects against parallel accesses to the pgd. That's why one additional page (PGD_ALLOC_ORDER) is allocated for the pgd.

Matthew Wilcox suggested that we instead should use a pointer in the struct page table for this spinlock and noted, that the comments for the PGD_ORDER and PMD_ORDER defines were wrong.

Both suggestions are addressed in this patch. The pgd spinlock (parisc_pgd_lock) is stored in the struct page table. In switch_mm_irqs_off() the physical address of this lock is loaded into cr28 (tr4) and the pgd into cr25, so that the fault handlers can directly access the lock.

Quoted 7 lines

The currently implemened Hybrid L2/L3 page table scheme (where the pmd is adjacent to the pgd) is dropped now too.

Fixes b37d1c1898b2 ("parisc: Use per-pagetable spinlock")

This patch results in:

BUG: spinlock recursion on CPU#0, swapper/0/1
  lock: 0x12226d14, .magic: dead4ead, .owner: swapper/0/1, .owner_cpu: 0
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.11.0-rc7-next-20210209-32bit #1
Hardware name: 9000/778/B160L
Backtrace:
  [<1019f9bc>] show_stack+0x34/0x48
  [<10a65278>] dump_stack+0x94/0x114
  [<10219f4c>] spin_dump+0x8c/0xb8
  [<1021a0b4>] do_raw_spin_lock+0xdc/0x108
  [<10a7367c>] _raw_spin_lock_irqsave+0x30/0x48
  [<102bf41c>] handle_mm_fault+0x5e8/0xdb0
  [<102b813c>] __get_user_pages.part.0+0x1b0/0x3d4
  [<102b8900>] __get_user_pages_remote+0x134/0x34c
  [<102b8b80>] get_user_pages_remote+0x68/0x90
  [<102fccb0>] get_arg_page+0x94/0xd8
  [<102fdd84>] copy_string_kernel+0xc4/0x234
  [<102fe70c>] kernel_execve+0xcc/0x1a4
  [<10a58d94>] run_init_process+0xbc/0xe0
  [<10a70d50>] kernel_init+0x98/0x13c
  [<1019a01c>] ret_from_kernel_thread+0x1c/0x24
Quoted 2 lines

when trying to boot parisc/hppa images in qemu. Reverting this patch fixes the problem.

True, I can reproduce the problem.
With CONFIG_DEBUG_SPINLOCK=y you get the backtrace above.
With  CONFIG_DEBUG_SPINLOCK=n it just hangs.
Happenes with 32-bit kernel with SMP kernel, even if only one virtual CPU is started.

Quoted 8 lines
Quoted 4 lines

Which is quite puzzling since most spin locks are optimized in this case case.  Strikes me we have a lock that's not initialized.

It's not obvious how this relates to the patch.  get_arg_page() calls get_user_pages_remote() with

The fact that reverting it fixes the problem is a good indication that the problem does relate to this patch.

As for how - no idea. That is not my area of expertise.

Quoted 9 lines

I built Helge's for-next tree both with CONFIG_DEBUG_SPINLOCK=y and CONFIG_DEBUG_SPINLOCK=n.  Both builds work fine on c8000.

The only thing that might have changed in the patch is the alignment of the lock used for page table updates. Qemu only support PA 1.x instructions.  The ldcw instruction needs 16-byte alignment on real hardware and there is code to dynamically align lock pointers to 16-byte alignment.  The c8000 supports PA 2.0 instructions and the ldcw,co instruction only needs 4-byte alignment.  Perhaps there is an issue with the dynamic alignment of the lock pointer or the lock initialization in the PA 1.x build for qemu.

The first lock is acquired in mm/memory.c from line 3565:

        vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
                        &vmf->ptl);
Quoted 8 lines

The second (recursive) lock is acquired from line 3587 in the same function:

        set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);

Source code lines are from next-20210211. I confirmed with debug code that the lock address passed to do_raw_spin_lock() is the same in both calls.

Thanks Guenter.  I assume this is with v15 of the patch?

It looks as if we might not need the ptl lock in set_pte_at() and probably elsewhere.

But I'm still puzzled as to why this doesn't happen when different locks are used as in your report with the earlier patch.

Signature · 1 line
John David Anglin  [email protected]
#20 lore raw
Quoted 82 lines · John David Anglin
Quoted 81 lines · Guenter Roeck
Quoted 63 lines · John David Anglin
Quoted 54 lines · Guenter Roeck
Quoted 53 lines · John David Anglin
Quoted 52 lines · Helge Deller
Quoted 45 lines · Guenter Roeck
Quoted 14 lines · Helge Deller

On parisc a spinlock is stored in the next page behind the pgd which protects against parallel accesses to the pgd. That's why one additional page (PGD_ALLOC_ORDER) is allocated for the pgd.

Matthew Wilcox suggested that we instead should use a pointer in the struct page table for this spinlock and noted, that the comments for the PGD_ORDER and PMD_ORDER defines were wrong.

Both suggestions are addressed in this patch. The pgd spinlock (parisc_pgd_lock) is stored in the struct page table. In switch_mm_irqs_off() the physical address of this lock is loaded into cr28 (tr4) and the pgd into cr25, so that the fault handlers can directly access the lock.

Quoted 7 lines

The currently implemened Hybrid L2/L3 page table scheme (where the pmd is adjacent to the pgd) is dropped now too.

Fixes b37d1c1898b2 ("parisc: Use per-pagetable spinlock")

This patch results in:

BUG: spinlock recursion on CPU#0, swapper/0/1
  lock: 0x12226d14, .magic: dead4ead, .owner: swapper/0/1, .owner_cpu: 0
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.11.0-rc7-next-20210209-32bit #1
Hardware name: 9000/778/B160L
Backtrace:
  [<1019f9bc>] show_stack+0x34/0x48
  [<10a65278>] dump_stack+0x94/0x114
  [<10219f4c>] spin_dump+0x8c/0xb8
  [<1021a0b4>] do_raw_spin_lock+0xdc/0x108
  [<10a7367c>] _raw_spin_lock_irqsave+0x30/0x48
  [<102bf41c>] handle_mm_fault+0x5e8/0xdb0
  [<102b813c>] __get_user_pages.part.0+0x1b0/0x3d4
  [<102b8900>] __get_user_pages_remote+0x134/0x34c
  [<102b8b80>] get_user_pages_remote+0x68/0x90
  [<102fccb0>] get_arg_page+0x94/0xd8
  [<102fdd84>] copy_string_kernel+0xc4/0x234
  [<102fe70c>] kernel_execve+0xcc/0x1a4
  [<10a58d94>] run_init_process+0xbc/0xe0
  [<10a70d50>] kernel_init+0x98/0x13c
  [<1019a01c>] ret_from_kernel_thread+0x1c/0x24
Quoted 2 lines

when trying to boot parisc/hppa images in qemu. Reverting this patch fixes the problem.

True, I can reproduce the problem.
With CONFIG_DEBUG_SPINLOCK=y you get the backtrace above.
With  CONFIG_DEBUG_SPINLOCK=n it just hangs.
Happenes with 32-bit kernel with SMP kernel, even if only one virtual CPU is started.

Quoted 8 lines
Quoted 4 lines

Which is quite puzzling since most spin locks are optimized in this case case.  Strikes me we have a lock that's not initialized.

It's not obvious how this relates to the patch.  get_arg_page() calls get_user_pages_remote() with

The fact that reverting it fixes the problem is a good indication that the problem does relate to this patch.

As for how - no idea. That is not my area of expertise.

Quoted 9 lines

I built Helge's for-next tree both with CONFIG_DEBUG_SPINLOCK=y and CONFIG_DEBUG_SPINLOCK=n.  Both builds work fine on c8000.

The only thing that might have changed in the patch is the alignment of the lock used for page table updates. Qemu only support PA 1.x instructions.  The ldcw instruction needs 16-byte alignment on real hardware and there is code to dynamically align lock pointers to 16-byte alignment.  The c8000 supports PA 2.0 instructions and the ldcw,co instruction only needs 4-byte alignment.  Perhaps there is an issue with the dynamic alignment of the lock pointer or the lock initialization in the PA 1.x build for qemu.

The first lock is acquired in mm/memory.c from line 3565:

        vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
                        &vmf->ptl);

The second (recursive) lock is acquired from line 3587 in the same function:

Quoted 7 lines
Quoted 5 lines
        set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);

Source code lines are from next-20210211. I confirmed with deb ug code that the lock address passed to do_raw_spin_lock() is the same in both calls.

Thanks Guenter.  I assume this is with v15 of the patch?

I have no idea what version it is, sorry. It is with the version that is in next-20210211. The problem was first seen with next-20210201.

Quoted 5 lines

It looks as if we might not need the ptl lock in set_pte_at() and probably elsewhere.

But I'm still puzzled as to why this doesn't happen when different locks are used as in your report with the earlier patch.

Maybe I reported it against the wrong version ? If so, sorry, my bad.

Guenter

author maintainer
#21 lore raw
In reply to John David Anglin

* John David Anglin <[email protected]>:

Quoted 82 lines · Guenter Roeck
Quoted 81 lines · John David Anglin
Quoted 63 lines · Guenter Roeck
Quoted 54 lines · John David Anglin
Quoted 53 lines · Helge Deller
Quoted 52 lines · Guenter Roeck
Quoted 45 lines · Helge Deller
Quoted 14 lines

On parisc a spinlock is stored in the next page behind the pgd which protects against parallel accesses to the pgd. That's why one additional page (PGD_ALLOC_ORDER) is allocated for the pgd.

Matthew Wilcox suggested that we instead should use a pointer in the struct page table for this spinlock and noted, that the comments for the PGD_ORDER and PMD_ORDER defines were wrong.

Both suggestions are addressed in this patch. The pgd spinlock (parisc_pgd_lock) is stored in the struct page table. In switch_mm_irqs_off() the physical address of this lock is loaded into cr28 (tr4) and the pgd into cr25, so that the fault handlers can directly access the lock.

Quoted 7 lines

The currently implemened Hybrid L2/L3 page table scheme (where the pmd is adjacent to the pgd) is dropped now too.

Fixes b37d1c1898b2 ("parisc: Use per-pagetable spinlock")

This patch results in:

BUG: spinlock recursion on CPU#0, swapper/0/1
  lock: 0x12226d14, .magic: dead4ead, .owner: swapper/0/1, .owner_cpu: 0
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.11.0-rc7-next-20210209-32bit #1
Hardware name: 9000/778/B160L
Backtrace:
  [<1019f9bc>] show_stack+0x34/0x48
  [<10a65278>] dump_stack+0x94/0x114
  [<10219f4c>] spin_dump+0x8c/0xb8
  [<1021a0b4>] do_raw_spin_lock+0xdc/0x108
  [<10a7367c>] _raw_spin_lock_irqsave+0x30/0x48
  [<102bf41c>] handle_mm_fault+0x5e8/0xdb0
  [<102b813c>] __get_user_pages.part.0+0x1b0/0x3d4
  [<102b8900>] __get_user_pages_remote+0x134/0x34c
  [<102b8b80>] get_user_pages_remote+0x68/0x90
  [<102fccb0>] get_arg_page+0x94/0xd8
  [<102fdd84>] copy_string_kernel+0xc4/0x234
  [<102fe70c>] kernel_execve+0xcc/0x1a4
  [<10a58d94>] run_init_process+0xbc/0xe0
  [<10a70d50>] kernel_init+0x98/0x13c
  [<1019a01c>] ret_from_kernel_thread+0x1c/0x24
Quoted 2 lines

when trying to boot parisc/hppa images in qemu. Reverting this patch fixes the problem.

True, I can reproduce the problem.
With CONFIG_DEBUG_SPINLOCK=y you get the backtrace above.
With  CONFIG_DEBUG_SPINLOCK=n it just hangs.
Happenes with 32-bit kernel with SMP kernel, even if only one virtual CPU is started.

Quoted 8 lines
Quoted 4 lines

Which is quite puzzling since most spin locks are optimized in this case case.  Strikes me we have a lock that's not initialized.

It's not obvious how this relates to the patch.  get_arg_page() calls get_user_pages_remote() with

The fact that reverting it fixes the problem is a good indication that the problem does relate to this patch.

As for how - no idea. That is not my area of expertise.

Quoted 9 lines

I built Helge's for-next tree both with CONFIG_DEBUG_SPINLOCK=y and CONFIG_DEBUG_SPINLOCK=n.  Both builds work fine on c8000.

The only thing that might have changed in the patch is the alignment of the lock used for page table updates. Qemu only support PA 1.x instructions.  The ldcw instruction needs 16-byte alignment on real hardware and there is code to dynamically align lock pointers to 16-byte alignment.  The c8000 supports PA 2.0 instructions and the ldcw,co instruction only needs 4-byte alignment.  Perhaps there is an issue with the dynamic alignment of the lock pointer or the lock initialization in the PA 1.x build for qemu.

The first lock is acquired in mm/memory.c from line 3565:

        vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
                        &vmf->ptl);

The second (recursive) lock is acquired from line 3587 in the same function:

Quoted 6 lines
Quoted 5 lines
        set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);

Source code lines are from next-20210211. I confirmed with debug code that the lock address passed to do_raw_spin_lock() is the same in both calls.

Thanks Guenter.  I assume this is with v15 of the patch?

Yes, happens with latest version too.

Quoted 1 line

It looks as if we might not need the ptl lock in set_pte_at() and probably elsewhere.

Just removing the locks from set_pte_at() didn't solved it. After removing the others too, it works.
Patch is attached below.
I added a memory-barrier to set_pte() too.

Quoted 2 lines

But I'm still puzzled as to why this doesn't happen when different locks are used as in your report with the earlier patch.

I think it happened earlier too. Would need to test though.

Helge

arch/parisc/include/asm/pgtable.h +5 -28
index 2e1873cd4877..2c68010d896a 100644
@@ -82,17 +82,14 @@ static inline void purge_tlb_entries(struct mm_struct *mm, unsigned long addr)
  */
 #define set_pte(pteptr, pteval)                                 \
         do{                                                     \
-                *(pteptr) = (pteval);                           \
+		*(pteptr) = (pteval);				\
+		mb();						\
         } while(0)
 
 #define set_pte_at(mm, addr, ptep, pteval)			\
 	do {							\
-		unsigned long flags;				\
-		spinlock_t *pgd_lock = &(mm)->page_table_lock;	\
-		spin_lock_irqsave(pgd_lock, flags);		\
 		set_pte(ptep, pteval);				\
 		purge_tlb_entries(mm, addr);			\
-		spin_unlock_irqrestore(pgd_lock, flags);	\
 	} while (0)
 
 #endif /* !__ASSEMBLY__ */
@@ -431,22 +428,15 @@ extern void update_mmu_cache(struct vm_area_struct *, unsigned long, pte_t *);
 static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
 {
 	pte_t pte;
-	unsigned long flags;
-	spinlock_t *pgd_lock;
 
 	if (!pte_young(*ptep))
 		return 0;
 
-	pgd_lock = &vma->vm_mm->page_table_lock;
-	spin_lock_irqsave(pgd_lock, flags);
 	pte = *ptep;
 	if (!pte_young(pte)) {
-		spin_unlock_irqrestore(pgd_lock, flags);
 		return 0;
 	}
-	set_pte(ptep, pte_mkold(pte));
-	purge_tlb_entries(vma->vm_mm, addr);
-	spin_unlock_irqrestore(pgd_lock, flags);
+	set_pte_at(vma->vm_mm, addr, ptep, pte_mkold(pte));
 	return 1;
 }
 
@@ -454,29 +444,16 @@ struct mm_struct;
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
 {
 	pte_t old_pte;
-	unsigned long flags;
-	spinlock_t *pgd_lock;
 
-	pgd_lock = &mm->page_table_lock;
-	spin_lock_irqsave(pgd_lock, flags);
 	old_pte = *ptep;
-	set_pte(ptep, __pte(0));
-	purge_tlb_entries(mm, addr);
-	spin_unlock_irqrestore(pgd_lock, flags);
+	set_pte_at(mm, addr, ptep, __pte(0));
 
 	return old_pte;
 }
 
 static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
 {
-	unsigned long flags;
-	spinlock_t *pgd_lock;
-
-	pgd_lock = &mm->page_table_lock;
-	spin_lock_irqsave(pgd_lock, flags);
-	set_pte(ptep, pte_wrprotect(*ptep));
-	purge_tlb_entries(mm, addr);
-	spin_unlock_irqrestore(pgd_lock, flags);
+	set_pte_at(mm, addr, ptep, pte_wrprotect(*ptep));
 }
 
 #define pte_same(A,B)	(pte_val(A) == pte_val(B))
#22 lore raw
Quoted 7 lines · Helge Deller

Yes, happens with latest version too.

Quoted 1 line

It looks as if we might not need the ptl lock in set_pte_at() and probably elsewhere.

Just removing the locks from set_pte_at() didn't solved it. After removing the others too, it works.
Patch is attached below.
I added a memory-barrier to set_pte() too.

You are ahead of me.  Should the memory barrier be a smp_mb()?

Dave

Signature · 1 line
John David Anglin  [email protected]
author maintainer
#23 lore raw
In reply to Helge Deller
Quoted 111 lines · Helge Deller

* John David Anglin <[email protected]>:

Quoted 82 lines · Guenter Roeck
Quoted 81 lines · John David Anglin
Quoted 63 lines · Guenter Roeck
Quoted 54 lines · John David Anglin
Quoted 53 lines · Helge Deller
Quoted 52 lines · Guenter Roeck
Quoted 45 lines · Helge Deller
Quoted 14 lines

On parisc a spinlock is stored in the next page behind the pgd which protects against parallel accesses to the pgd. That's why one additional page (PGD_ALLOC_ORDER) is allocated for the pgd.

Matthew Wilcox suggested that we instead should use a pointer in the struct page table for this spinlock and noted, that the comments for the PGD_ORDER and PMD_ORDER defines were wrong.

Both suggestions are addressed in this patch. The pgd spinlock (parisc_pgd_lock) is stored in the struct page table. In switch_mm_irqs_off() the physical address of this lock is loaded into cr28 (tr4) and the pgd into cr25, so that the fault handlers can directly access the lock.

Quoted 7 lines

The currently implemened Hybrid L2/L3 page table scheme (where the pmd is adjacent to the pgd) is dropped now too.

Fixes b37d1c1898b2 ("parisc: Use per-pagetable spinlock")

This patch results in:

BUG: spinlock recursion on CPU#0, swapper/0/1
   lock: 0x12226d14, .magic: dead4ead, .owner: swapper/0/1, .owner_cpu: 0
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.11.0-rc7-next-20210209-32bit #1
Hardware name: 9000/778/B160L
Backtrace:
   [<1019f9bc>] show_stack+0x34/0x48
   [<10a65278>] dump_stack+0x94/0x114
   [<10219f4c>] spin_dump+0x8c/0xb8
   [<1021a0b4>] do_raw_spin_lock+0xdc/0x108
   [<10a7367c>] _raw_spin_lock_irqsave+0x30/0x48
   [<102bf41c>] handle_mm_fault+0x5e8/0xdb0
   [<102b813c>] __get_user_pages.part.0+0x1b0/0x3d4
   [<102b8900>] __get_user_pages_remote+0x134/0x34c
   [<102b8b80>] get_user_pages_remote+0x68/0x90
   [<102fccb0>] get_arg_page+0x94/0xd8
   [<102fdd84>] copy_string_kernel+0xc4/0x234
   [<102fe70c>] kernel_execve+0xcc/0x1a4
   [<10a58d94>] run_init_process+0xbc/0xe0
   [<10a70d50>] kernel_init+0x98/0x13c
   [<1019a01c>] ret_from_kernel_thread+0x1c/0x24
Quoted 2 lines

when trying to boot parisc/hppa images in qemu. Reverting this patch fixes the problem.

True, I can reproduce the problem.
With CONFIG_DEBUG_SPINLOCK=y you get the backtrace above.
With  CONFIG_DEBUG_SPINLOCK=n it just hangs.
Happenes with 32-bit kernel with SMP kernel, even if only one virtual CPU is started.

Quoted 8 lines
Quoted 4 lines

Which is quite puzzling since most spin locks are optimized in this case case.  Strikes me we have a lock that's not initialized.

It's not obvious how this relates to the patch.  get_arg_page() calls get_user_pages_remote() with

The fact that reverting it fixes the problem is a good indication that the problem does relate to this patch.

As for how - no idea. That is not my area of expertise.

Quoted 9 lines

I built Helge's for-next tree both with CONFIG_DEBUG_SPINLOCK=y and CONFIG_DEBUG_SPINLOCK=n.  Both builds work fine on c8000.

The only thing that might have changed in the patch is the alignment of the lock used for page table updates. Qemu only support PA 1.x instructions.  The ldcw instruction needs 16-byte alignment on real hardware and there is code to dynamically align lock pointers to 16-byte alignment.  The c8000 supports PA 2.0 instructions and the ldcw,co instruction only needs 4-byte alignment.  Perhaps there is an issue with the dynamic alignment of the lock pointer or the lock initialization in the PA 1.x build for qemu.

The first lock is acquired in mm/memory.c from line 3565:

         vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
                         &vmf->ptl);

The second (recursive) lock is acquired from line 3587 in the same function:

Quoted 6 lines
Quoted 5 lines
         set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry);

Source code lines are from next-20210211. I confirmed with debug code that the lock address passed to do_raw_spin_lock() is the same in both calls.

Thanks Guenter.  I assume this is with v15 of the patch?

Yes, happens with latest version too.

Quoted 1 line

It looks as if we might not need the ptl lock in set_pte_at() and probably elsewhere.

Just removing the locks from set_pte_at() didn't solved it. After removing the others too, it works.
Patch is attached below.
I added a memory-barrier to set_pte() too.

Quoted 2 lines

But I'm still puzzled as to why this doesn't happen when different locks are used as in your report with the earlier patch.

I think it happened earlier too. Would need to test though.

Helge

arch/parisc/include/asm/pgtable.h +0 -0
index 2e1873cd4877..2c68010d896a 100644

arch/parisc/include/asm/pgtable.h static inline void purge_tlb_entries(struct mm_struct *mm, unsigned long addr) · line 86

Quoted 6 lines
arch/parisc/include/asm/pgtable.h +2 -1
@@ -82,17 +82,14 @@ static inline void purge_tlb_entries(struct mm_struct *mm, unsigned long addr)
   */
  #define set_pte(pteptr, pteval)                                 \
          do{                                                     \
-                *(pteptr) = (pteval);                           \
+		*(pteptr) = (pteval);				\
+		mb();						\

Actually, this should be barrier() instead of mb(). mb() is overkill on SMP.

Helge

Quoted 70 lines
arch/parisc/include/asm/pgtable.h +3 -27
          } while(0)
 
  #define set_pte_at(mm, addr, ptep, pteval)			\
  	do {							\
-		unsigned long flags;				\
-		spinlock_t *pgd_lock = &(mm)->page_table_lock;	\
-		spin_lock_irqsave(pgd_lock, flags);		\
  		set_pte(ptep, pteval);				\
  		purge_tlb_entries(mm, addr);			\
-		spin_unlock_irqrestore(pgd_lock, flags);	\
  	} while (0)
 
  #endif /* !__ASSEMBLY__ */
@@ -431,22 +428,15 @@ extern void update_mmu_cache(struct vm_area_struct *, unsigned long, pte_t *);
  static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
  {
  	pte_t pte;
-	unsigned long flags;
-	spinlock_t *pgd_lock;
 
  	if (!pte_young(*ptep))
  		return 0;
 
-	pgd_lock = &vma->vm_mm->page_table_lock;
-	spin_lock_irqsave(pgd_lock, flags);
  	pte = *ptep;
  	if (!pte_young(pte)) {
-		spin_unlock_irqrestore(pgd_lock, flags);
  		return 0;
  	}
-	set_pte(ptep, pte_mkold(pte));
-	purge_tlb_entries(vma->vm_mm, addr);
-	spin_unlock_irqrestore(pgd_lock, flags);
+	set_pte_at(vma->vm_mm, addr, ptep, pte_mkold(pte));
  	return 1;
  }
 
@@ -454,29 +444,16 @@ struct mm_struct;
  static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  {
  	pte_t old_pte;
-	unsigned long flags;
-	spinlock_t *pgd_lock;
 
-	pgd_lock = &mm->page_table_lock;
-	spin_lock_irqsave(pgd_lock, flags);
  	old_pte = *ptep;
-	set_pte(ptep, __pte(0));
-	purge_tlb_entries(mm, addr);
-	spin_unlock_irqrestore(pgd_lock, flags);
+	set_pte_at(mm, addr, ptep, __pte(0));
 
  	return old_pte;
  }
 
  static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  {
-	unsigned long flags;
-	spinlock_t *pgd_lock;
-
-	pgd_lock = &mm->page_table_lock;
-	spin_lock_irqsave(pgd_lock, flags);
-	set_pte(ptep, pte_wrprotect(*ptep));
-	purge_tlb_entries(mm, addr);
-	spin_unlock_irqrestore(pgd_lock, flags);
+	set_pte_at(mm, addr, ptep, pte_wrprotect(*ptep));
  }
 
  #define pte_same(A,B)	(pte_val(A) == pte_val(B))