Skip to contentSkip to conversation

sh: get rid of __ARCH_USE_5LEVEL_HACK

PATCH In review Mike Rapoport 3 participants 7 messages Dec 17, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover sh: get rid of __ARCH_USE_5LEVEL_HACK

From: Mike Rapoport <[email protected]>

Hi,

These patches update the SuperH page table folding/unfolding to take into account the 5th level.

Mike Rapoport (2):
sh: drop __pXd_offset() macros that duplicate pXd_index() ones
sh: add support for folded p4d page tables

 arch/sh/include/asm/pgtable-2level.h |  1 -
 arch/sh/include/asm/pgtable-3level.h |  1 -
 arch/sh/include/asm/pgtable_32.h     |  5 ++---
 arch/sh/include/asm/pgtable_64.h     |  5 ++---
 arch/sh/kernel/io_trapped.c          |  7 ++++++-
 arch/sh/mm/cache-sh4.c               |  4 +++-
 arch/sh/mm/cache-sh5.c               |  7 ++++++-
 arch/sh/mm/fault.c                   | 26 +++++++++++++++++++++++---
 arch/sh/mm/hugetlbpage.c             | 28 ++++++++++++++++++----------
 arch/sh/mm/init.c                    | 15 +++++++++++----
 arch/sh/mm/kmap.c                    |  2 +-
 arch/sh/mm/tlbex_32.c                |  6 +++++-
 arch/sh/mm/tlbex_64.c                |  7 ++++++-
 13 files changed, 83 insertions(+), 31 deletions(-)
Signature · 1 line
2.24.0
#1 lore raw

PATCH 1/2 sh: drop __pXd_offset() macros that duplicate pXd_index() ones

From: Mike Rapoport <[email protected]>

The __pXd_offset() macros are identical to the pXd_index() macros and there is no point to keep both of them. All architectures define and use pXd_index() so let's keep only those to make mips consistent with the rest of the kernel.

---
 arch/sh/include/asm/pgtable_32.h | 5 ++---
 arch/sh/include/asm/pgtable_64.h | 5 ++---
 arch/sh/mm/init.c                | 6 +++---
 3 files changed, 7 insertions(+), 9 deletions(-)
arch/sh/include/asm/pgtable_32.h +2 -3
index 29274f0e428e..4acce5f2cbf9 100644
@@ -407,13 +407,12 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
 /* to find an entry in a page-table-directory. */
 #define pgd_index(address)	(((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
 #define pgd_offset(mm, address)	((mm)->pgd + pgd_index(address))
-#define __pgd_offset(address)	pgd_index(address)
 
 /* to find an entry in a kernel page-table-directory */
 #define pgd_offset_k(address)	pgd_offset(&init_mm, address)
 
-#define __pud_offset(address)	(((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
-#define __pmd_offset(address)	(((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
+#define pud_index(address)	(((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
+#define pmd_index(address)	(((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
 
 /* Find an entry in the third-level page table.. */
 #define pte_index(address)	((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
arch/sh/include/asm/pgtable_64.h +2 -3
index 1778bc5971e7..27cc282ec6c0 100644
@@ -46,14 +46,13 @@ static __inline__ void set_pte(pte_t *pteptr, pte_t pteval)
 
 /* To find an entry in a generic PGD. */
 #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
-#define __pgd_offset(address) pgd_index(address)
 #define pgd_offset(mm, address) ((mm)->pgd+pgd_index(address))
 
 /* To find an entry in a kernel PGD. */
 #define pgd_offset_k(address) pgd_offset(&init_mm, address)
 
-#define __pud_offset(address)	(((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
-#define __pmd_offset(address)	(((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
+#define pud_index(address)	(((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
+/* #define pmd_index(address)	(((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1)) */
 
 /*
  * PMD level access routines. Same notes as above.
arch/sh/mm/init.c +3 -3
index dfdbaa50946e..4bce5fc4b63e 100644
@@ -172,9 +172,9 @@ void __init page_table_range_init(unsigned long start, unsigned long end,
 	unsigned long vaddr;
 
 	vaddr = start;
-	i = __pgd_offset(vaddr);
-	j = __pud_offset(vaddr);
-	k = __pmd_offset(vaddr);
+	i = pgd_index(vaddr);
+	j = pud_index(vaddr);
+	k = pmd_index(vaddr);
 	pgd = pgd_base + i;
 
 	for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
Signature · 1 line
2.24.0
#2 lore raw

PATCH 2/2 sh: add support for folded p4d page tables

In reply to Mike Rapoport

From: Mike Rapoport <[email protected]>

Implement primitives necessary for the 4th level folding, add walks of p4d level where appropriate and remove usage of __ARCH_USE_5LEVEL_HACK.

---
 arch/sh/include/asm/pgtable-2level.h |  1 -
 arch/sh/include/asm/pgtable-3level.h |  1 -
 arch/sh/kernel/io_trapped.c          |  7 ++++++-
 arch/sh/mm/cache-sh4.c               |  4 +++-
 arch/sh/mm/cache-sh5.c               |  7 ++++++-
 arch/sh/mm/fault.c                   | 26 +++++++++++++++++++++++---
 arch/sh/mm/hugetlbpage.c             | 28 ++++++++++++++++++----------
 arch/sh/mm/init.c                    |  9 ++++++++-
 arch/sh/mm/kmap.c                    |  2 +-
 arch/sh/mm/tlbex_32.c                |  6 +++++-
 arch/sh/mm/tlbex_64.c                |  7 ++++++-
 11 files changed, 76 insertions(+), 22 deletions(-)
arch/sh/include/asm/pgtable-2level.h +0 -1
index bf1eb51c3ee5..08bff93927ff 100644
@@ -2,7 +2,6 @@
 #ifndef __ASM_SH_PGTABLE_2LEVEL_H
 #define __ASM_SH_PGTABLE_2LEVEL_H
 
-#define __ARCH_USE_5LEVEL_HACK
 #include <asm-generic/pgtable-nopmd.h>
 
 /*
arch/sh/include/asm/pgtable-3level.h +0 -1
index 779260b721ca..0f80097e5c9c 100644
@@ -2,7 +2,6 @@
 #ifndef __ASM_SH_PGTABLE_3LEVEL_H
 #define __ASM_SH_PGTABLE_3LEVEL_H
 
-#define __ARCH_USE_5LEVEL_HACK
 #include <asm-generic/pgtable-nopud.h>
 
 /*
arch/sh/kernel/io_trapped.c +6 -1
index 60c828a2b8a2..037aab2708b7 100644
@@ -136,6 +136,7 @@ EXPORT_SYMBOL_GPL(match_trapped_io_handler);
 static struct trapped_io *lookup_tiop(unsigned long address)
 {
 	pgd_t *pgd_k;
+	p4d_t *p4d_k;
 	pud_t *pud_k;
 	pmd_t *pmd_k;
 	pte_t *pte_k;
@@ -145,7 +146,11 @@ static struct trapped_io *lookup_tiop(unsigned long address)
 	if (!pgd_present(*pgd_k))
 		return NULL;
 
-	pud_k = pud_offset(pgd_k, address);
+	p4d_k = p4d_offset(pgd_k, address);
+	if (!p4d_present(*p4d_k))
+		return NULL;
+
+	pud_k = pud_offset(p4d_k, address);
 	if (!pud_present(*pud_k))
 		return NULL;
 
arch/sh/mm/cache-sh4.c +3 -1
index eee911422cf9..45943bcb7042 100644
@@ -209,6 +209,7 @@ static void sh4_flush_cache_page(void *args)
 	unsigned long address, pfn, phys;
 	int map_coherent = 0;
 	pgd_t *pgd;
+	p4d_t *p4d;
 	pud_t *pud;
 	pmd_t *pmd;
 	pte_t *pte;
@@ -224,7 +225,8 @@ static void sh4_flush_cache_page(void *args)
 		return;
 
 	pgd = pgd_offset(vma->vm_mm, address);
-	pud = pud_offset(pgd, address);
+	p4d = p4d_offset(pgd, address);
+	pud = pud_offset(p4d, address);
 	pmd = pmd_offset(pud, address);
 	pte = pte_offset_kernel(pmd, address);
 
arch/sh/mm/cache-sh5.c +6 -1
index 445b5e69b73c..442a77cc2957 100644
@@ -383,6 +383,7 @@ static void sh64_dcache_purge_user_pages(struct mm_struct *mm,
 				unsigned long addr, unsigned long end)
 {
 	pgd_t *pgd;
+	p4d_t *p4d;
 	pud_t *pud;
 	pmd_t *pmd;
 	pte_t *pte;
@@ -397,7 +398,11 @@ static void sh64_dcache_purge_user_pages(struct mm_struct *mm,
 	if (pgd_bad(*pgd))
 		return;
 
-	pud = pud_offset(pgd, addr);
+	p4d = p4d_offset(pgd, addr);
+	if (p4d_none(*p4d) || p4d_bad(*p4d))
+		return;
+
+	pud = pud_offset(p4d, addr);
 	if (pud_none(*pud) || pud_bad(*pud))
 		return;
 
arch/sh/mm/fault.c +23 -3
index 5f51456f4fc7..001c9c23aa56 100644
@@ -53,6 +53,7 @@ static void show_pte(struct mm_struct *mm, unsigned long addr)
 	       (u32)(sizeof(*pgd) * 2), (u64)pgd_val(*pgd));
 
 	do {
+		p4d_t *p4d;
 		pud_t *pud;
 		pmd_t *pmd;
 		pte_t *pte;
@@ -65,7 +66,20 @@ static void show_pte(struct mm_struct *mm, unsigned long addr)
 			break;
 		}
 
-		pud = pud_offset(pgd, addr);
+		p4d = p4d_offset(pgd, addr);
+		if (PTRS_PER_P4D != 1)
+			printk(", *p4d=%0*Lx", (u32)(sizeof(*p4d) * 2),
+			       (u64)p4d_val(*p4d));
+
+		if (p4d_none(*p4d))
+			break;
+
+		if (p4d_bad(*p4d)) {
+			printk("(bad)");
+			break;
+		}
+
+		pud = pud_offset(p4d, addr);
 		if (PTRS_PER_PUD != 1)
 			printk(", *pud=%0*Lx", (u32)(sizeof(*pud) * 2),
 			       (u64)pud_val(*pud));
@@ -107,6 +121,7 @@ static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
 {
 	unsigned index = pgd_index(address);
 	pgd_t *pgd_k;
+	p4d_t *p4d, *p4d_k;
 	pud_t *pud, *pud_k;
 	pmd_t *pmd, *pmd_k;
 
@@ -116,8 +131,13 @@ static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
 	if (!pgd_present(*pgd_k))
 		return NULL;
 
-	pud = pud_offset(pgd, address);
-	pud_k = pud_offset(pgd_k, address);
+	p4d = p4d_offset(pgd, address);
+	p4d_k = p4d_offset(pgd_k, address);
+	if (!p4d_present(*p4d_k))
+		return NULL;
+
+	pud = pud_offset(p4d, address);
+	pud_k = pud_offset(p4d_k, address);
 	if (!pud_present(*pud_k))
 		return NULL;
 
arch/sh/mm/hugetlbpage.c +18 -10
index 960deb1f24a1..acd5652a0de3 100644
@@ -26,17 +26,21 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
 			unsigned long addr, unsigned long sz)
 {
 	pgd_t *pgd;
+	p4d_t *p4d;
 	pud_t *pud;
 	pmd_t *pmd;
 	pte_t *pte = NULL;
 
 	pgd = pgd_offset(mm, addr);
 	if (pgd) {
-		pud = pud_alloc(mm, pgd, addr);
-		if (pud) {
-			pmd = pmd_alloc(mm, pud, addr);
-			if (pmd)
-				pte = pte_alloc_map(mm, pmd, addr);
+		p4d = p4d_alloc(mm, pgd, addr);
+		if (p4d) {
+			pud = pud_alloc(mm, p4d, addr);
+			if (pud) {
+				pmd = pmd_alloc(mm, pud, addr);
+				if (pmd)
+					pte = pte_alloc_map(mm, pmd, addr);
+			}
 		}
 	}
 
@@ -47,17 +51,21 @@ pte_t *huge_pte_offset(struct mm_struct *mm,
 		       unsigned long addr, unsigned long sz)
 {
 	pgd_t *pgd;
+	p4d_t *p4d;
 	pud_t *pud;
 	pmd_t *pmd;
 	pte_t *pte = NULL;
 
 	pgd = pgd_offset(mm, addr);
 	if (pgd) {
-		pud = pud_offset(pgd, addr);
-		if (pud) {
-			pmd = pmd_offset(pud, addr);
-			if (pmd)
-				pte = pte_offset_map(pmd, addr);
+		p4d = p4d_offset(pgd, addr);
+		if (p4d) {
+			pud = pud_offset(p4d, addr);
+			if (pud) {
+				pmd = pmd_offset(pud, addr);
+				if (pmd)
+					pte = pte_offset_map(pmd, addr);
+			}
 		}
 	}
 
arch/sh/mm/init.c +8 -1
index 4bce5fc4b63e..ed3fc4e5ded7 100644
@@ -45,6 +45,7 @@ void __init __weak plat_mem_setup(void)
 static pte_t *__get_pte_phys(unsigned long addr)
 {
 	pgd_t *pgd;
+	p4d_t *p4d;
 	pud_t *pud;
 	pmd_t *pmd;
 
@@ -54,7 +55,13 @@ static pte_t *__get_pte_phys(unsigned long addr)
 		return NULL;
 	}
 
-	pud = pud_alloc(NULL, pgd, addr);
+	p4d = p4d_alloc(NULL, pgd, addr);
+	if (unlikely(!p4d)) {
+		p4d_ERROR(*p4d);
+		return NULL;
+	}
+
+	pud = pud_alloc(NULL, p4d, addr);
 	if (unlikely(!pud)) {
 		pud_ERROR(*pud);
 		return NULL;
arch/sh/mm/kmap.c +1 -1
index 9e6b38b03cf7..0e7039137f5a 100644
@@ -15,7 +15,7 @@
 #include <asm/cacheflush.h>
 
 #define kmap_get_fixmap_pte(vaddr)                                     \
-	pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr)), (vaddr))
+	pte_offset_kernel(pmd_offset(pud_offset(p4d_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr)), (vaddr)), vaddr)
 
 static pte_t *kmap_coherent_pte;
 
arch/sh/mm/tlbex_32.c +5 -1
index 382262dc0c4b..1c53868632ee 100644
@@ -23,6 +23,7 @@ handle_tlbmiss(struct pt_regs *regs, unsigned long error_code,
 	       unsigned long address)
 {
 	pgd_t *pgd;
+	p4d_t *p4d;
 	pud_t *pud;
 	pmd_t *pmd;
 	pte_t *pte;
@@ -42,7 +43,10 @@ handle_tlbmiss(struct pt_regs *regs, unsigned long error_code,
 		pgd = pgd_offset(current->mm, address);
 	}
 
-	pud = pud_offset(pgd, address);
+	p4d = p4d_offset(pgd, address);
+	if (p4d_none_or_clear_bad(p4d))
+		return 1;
+	pud = pud_offset(p4d, address);
 	if (pud_none_or_clear_bad(pud))
 		return 1;
 	pmd = pmd_offset(pud, address);
arch/sh/mm/tlbex_64.c +6 -1
index 8ff966dd0c74..0d015f7556fa 100644
@@ -44,6 +44,7 @@ static int handle_tlbmiss(unsigned long long protection_flags,
 			  unsigned long address)
 {
 	pgd_t *pgd;
+	p4d_t *p4d;
 	pud_t *pud;
 	pmd_t *pmd;
 	pte_t *pte;
@@ -58,7 +59,11 @@ static int handle_tlbmiss(unsigned long long protection_flags,
 		pgd = pgd_offset(current->mm, address);
 	}
 
-	pud = pud_offset(pgd, address);
+	p4d = p4d_offset(pgd, address);
+	if (p4d_none(*p4d) || !p4d_present(*p4d))
+		return 1;
+
+	pud = pud_offset(p4d, address);
 	if (pud_none(*pud) || !pud_present(*pud))
 		return 1;
 
Signature · 1 line
2.24.0
#3 lore raw

Re: [PATCH 2/2] sh: add support for folded p4d page tables

Hi Mike,

Quoted 6 lines · Mike Rapoport

From: Mike Rapoport <[email protected]>

Implement primitives necessary for the 4th level folding, add walks of p4d level where appropriate and remove usage of __ARCH_USE_5LEVEL_HACK.

Thanks for your patch!

arch/sh/mm/fault.c static void show_pte(struct mm_struct *mm, unsigned long addr) · line 72

Quoted 11 lines
arch/sh/mm/fault.c +4 -1
@@ -65,7 +66,20 @@ static void show_pte(struct mm_struct *mm, unsigned long addr)
                        break;
                }
 
-               pud = pud_offset(pgd, addr);
+               p4d = p4d_offset(pgd, addr);
+               if (PTRS_PER_P4D != 1)
+                       printk(", *p4d=%0*Lx", (u32)(sizeof(*p4d) * 2),
+                              (u64)p4d_val(*p4d));

This (and the prints below) is gonna cause lots of broken output lines. You should use pr_cont() instead, and probably rebase on top of my "[PATCH 7/7] sh: fault: Modernize printing of kernel messages"
(https://lore.kernel.org/lkml/[email protected]/).

arch/sh/mm/fault.c static void show_pte(struct mm_struct *mm, unsigned long addr) · line 85

Quoted 13 lines
arch/sh/mm/fault.c +10 -0
+
+               if (p4d_none(*p4d))
+                       break;
+
+               if (p4d_bad(*p4d)) {
+                       printk("(bad)");
+                       break;
+               }
+
+               pud = pud_offset(p4d, addr);
                if (PTRS_PER_PUD != 1)
                        printk(", *pud=%0*Lx", (u32)(sizeof(*pud) * 2),
                               (u64)pud_val(*pud));

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#4 lore raw

Re: [PATCH 2/2] sh: add support for folded p4d page tables

Quoted 18 lines · Geert Uytterhoeven

Hi Mike,

Quoted 6 lines · Mike Rapoport

From: Mike Rapoport <[email protected]>

Implement primitives necessary for the 4th level folding, add walks of p4d level where appropriate and remove usage of __ARCH_USE_5LEVEL_HACK.

Thanks for your patch!

Quoted 6 lines
arch/sh/mm/fault.c +0 -0
@@ -65,7 +66,20 @@ static void show_pte(struct mm_struct *mm, unsigned long addr)
                        break;
                }
 
Quoted 10 lines

arch/sh/mm/fault.c static void show_pte(struct mm_struct *mm, unsigned long addr) · line 72

Quoted 5 lines
arch/sh/mm/fault.c +4 -1
-               pud = pud_offset(pgd, addr);
+               p4d = p4d_offset(pgd, addr);
+               if (PTRS_PER_P4D != 1)
+                       printk(", *p4d=%0*Lx", (u32)(sizeof(*p4d) * 2),
+                              (u64)p4d_val(*p4d));

This (and the prints below) is gonna cause lots of broken output lines. You should use pr_cont() instead, and probably rebase on top of my "[PATCH 7/7] sh: fault: Modernize printing of kernel messages"
(https://lore.kernel.org/lkml/[email protected]/).

Ok, will fix.

Quoted 24 lines

arch/sh/mm/fault.c static void show_pte(struct mm_struct *mm, unsigned long addr) · line 85

Quoted 13 lines
arch/sh/mm/fault.c +10 -0
+
+               if (p4d_none(*p4d))
+                       break;
+
+               if (p4d_bad(*p4d)) {
+                       printk("(bad)");
+                       break;
+               }
+
+               pud = pud_offset(p4d, addr);
                if (PTRS_PER_PUD != 1)
                        printk(", *pud=%0*Lx", (u32)(sizeof(*pud) * 2),
                               (u64)pud_val(*pud));

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#5 lore raw
In reply to Mike Rapoport
Quoted 6 lines · Mike Rapoport

From: Mike Rapoport <[email protected]>

Hi,

These patches update the SuperH page table folding/unfolding to take into account the 5th level.

Is this only for 64-bit/sh5? If so, I'd really like to avoid having patch churn that nominally keeps it working, since it doesn't; GCC dropped support a long time ago and it was experimental to begin with, never appeared in production. Has anyone looked lately at what it would take to remove it?

Rich

#6 lore raw
Quoted 5 lines · Rich Felker
Quoted 4 lines · Mike Rapoport

From: Mike Rapoport <[email protected]>

Hi,

Quoted 8 lines
Quoted 2 lines

These patches update the SuperH page table folding/unfolding to take into account the 5th level.

Is this only for 64-bit/sh5? If so, I'd really like to avoid having patch churn that nominally keeps it working, since it doesn't; GCC dropped support a long time ago and it was experimental to begin with, never appeared in production. Has anyone looked lately at what it would take to remove it?

This is a part of a generic cleanup of page table manipulations and removal of __ARCH_USE_5LEVEL_HACK and the related code from the kernel. So this is not related to a particular sh variant.

Quoted 1 line

Rich

Signature · 2 lines
Sincerely yours,
Mike.