Re: [PATCH v2 07/13] powerpc: add support for folded p4d page tables

2020-02-26 Thread Christophe Leroy




Le 26/02/2020 à 11:56, Mike Rapoport a écrit :

On Wed, Feb 26, 2020 at 10:46:13AM +0100, Christophe Leroy wrote:



Le 26/02/2020 à 10:13, Mike Rapoport a écrit :

On Tue, Feb 18, 2020 at 12:54:40PM +0200, Mike Rapoport wrote:

On Sun, Feb 16, 2020 at 11:41:07AM +0100, Christophe Leroy wrote:



Le 16/02/2020 à 09:18, Mike Rapoport a écrit :

From: Mike Rapoport 

Implement primitives necessary for the 4th level folding, add walks of p4d
level where appropriate and replace 5level-fixup.h with pgtable-nop4d.h.


I don't think it is worth adding all this additionnals walks of p4d, this
patch could be limited to changes like:

-   pud = pud_offset(pgd, gpa);
+   pud = pud_offset(p4d_offset(pgd, gpa), gpa);

The additionnal walks should be added through another patch the day powerpc
need them.


Ok, I'll update the patch to reduce walking the p4d.


Here's what I have with more direct acceses from pgd to pud.


I went quickly through. This looks promising.

Do we need the walk_p4d() in arch/powerpc/mm/ptdump/hashpagetable.c ?
Can't we just do

@@ -445,7 +459,7 @@ static void walk_pagetables(struct pg_state *st)
addr = KERN_VIRT_START + i * PGDIR_SIZE;
if (!pgd_none(*pgd))
/* pgd exists */
-   walk_pud(st, pgd, addr);
+   walk_pud(st, p4d_offset(pgd, addr), addr);


We can do

addr = KERN_VIRT_START + i * PGDIR_SIZE;
p4d = p4d_offset(pgd, addr);
if (!p4d_none(*pgd))
walk_pud()

But I don't think this is really essential. Again, we are trading off code
consistency vs line count. I don't think line count is that important.


Ok.

Christophe


Re: [PATCH v2 07/13] powerpc: add support for folded p4d page tables

2020-02-26 Thread Mike Rapoport
On Wed, Feb 26, 2020 at 10:46:13AM +0100, Christophe Leroy wrote:
> 
> 
> Le 26/02/2020 à 10:13, Mike Rapoport a écrit :
> > On Tue, Feb 18, 2020 at 12:54:40PM +0200, Mike Rapoport wrote:
> > > On Sun, Feb 16, 2020 at 11:41:07AM +0100, Christophe Leroy wrote:
> > > > 
> > > > 
> > > > Le 16/02/2020 à 09:18, Mike Rapoport a écrit :
> > > > > From: Mike Rapoport 
> > > > > 
> > > > > Implement primitives necessary for the 4th level folding, add walks 
> > > > > of p4d
> > > > > level where appropriate and replace 5level-fixup.h with 
> > > > > pgtable-nop4d.h.
> > > > 
> > > > I don't think it is worth adding all this additionnals walks of p4d, 
> > > > this
> > > > patch could be limited to changes like:
> > > > 
> > > > -   pud = pud_offset(pgd, gpa);
> > > > +   pud = pud_offset(p4d_offset(pgd, gpa), gpa);
> > > > 
> > > > The additionnal walks should be added through another patch the day 
> > > > powerpc
> > > > need them.
> > > 
> > > Ok, I'll update the patch to reduce walking the p4d.
> > 
> > Here's what I have with more direct acceses from pgd to pud.
> 
> I went quickly through. This looks promising.
> 
> Do we need the walk_p4d() in arch/powerpc/mm/ptdump/hashpagetable.c ?
> Can't we just do
> 
> @@ -445,7 +459,7 @@ static void walk_pagetables(struct pg_state *st)
>   addr = KERN_VIRT_START + i * PGDIR_SIZE;
>   if (!pgd_none(*pgd))
>   /* pgd exists */
> - walk_pud(st, pgd, addr);
> + walk_pud(st, p4d_offset(pgd, addr), addr);

We can do

addr = KERN_VIRT_START + i * PGDIR_SIZE;
p4d = p4d_offset(pgd, addr);
if (!p4d_none(*pgd))
walk_pud()

But I don't think this is really essential. Again, we are trading off code
consistency vs line count. I don't think line count is that important.

>   }
>  }
> 
> 
> 
> Also, I think the removal of get_pteptr() should be a patch by itself.
> 
> You could include my patches in your series. See
> https://patchwork.ozlabs.org/project/linuxppc-dev/list/?series=152102
> 
> Christophe
> 
> 
> 
> > 
> >  From 6c59a86ce8394fb6100e9b6ced2e346981fb0ce9 Mon Sep 17 00:00:00 2001
> > From: Mike Rapoport 
> > Date: Sun, 24 Nov 2019 15:38:00 +0200
> > Subject: [PATCH v3] powerpc: add support for folded p4d page tables
> > 
> > Implement primitives necessary for the 4th level folding, add walks of p4d
> > level where appropriate and replace 5level-fixup.h with pgtable-nop4d.h.
> > 
> > Signed-off-by: Mike Rapoport 
> > Tested-by: Christophe Leroy  # 8xx and 83xx
> > ---
> > v3:
> > * reduce amount of added p4d walks
> > * kill pgtable_32::get_pteptr and traverse page table in
> >pgtable_32::__change_page_attr_noflush
> > 
> > 
> >   arch/powerpc/include/asm/book3s/32/pgtable.h  |  1 -
> >   arch/powerpc/include/asm/book3s/64/hash.h |  4 +-
> >   arch/powerpc/include/asm/book3s/64/pgalloc.h  |  4 +-
> >   arch/powerpc/include/asm/book3s/64/pgtable.h  | 60 ++-
> >   arch/powerpc/include/asm/book3s/64/radix.h|  6 +-
> >   arch/powerpc/include/asm/nohash/32/pgtable.h  |  1 -
> >   arch/powerpc/include/asm/nohash/64/pgalloc.h  |  2 +-
> >   .../include/asm/nohash/64/pgtable-4k.h| 32 +-
> >   arch/powerpc/include/asm/nohash/64/pgtable.h  |  6 +-
> >   arch/powerpc/include/asm/pgtable.h|  6 +-
> >   arch/powerpc/kvm/book3s_64_mmu_radix.c| 30 ++
> >   arch/powerpc/lib/code-patching.c  |  7 ++-
> >   arch/powerpc/mm/book3s32/mmu.c|  2 +-
> >   arch/powerpc/mm/book3s32/tlb.c|  4 +-
> >   arch/powerpc/mm/book3s64/hash_pgtable.c   |  4 +-
> >   arch/powerpc/mm/book3s64/radix_pgtable.c  | 26 +---
> >   arch/powerpc/mm/book3s64/subpage_prot.c   |  6 +-
> >   arch/powerpc/mm/hugetlbpage.c | 28 +
> >   arch/powerpc/mm/kasan/kasan_init_32.c |  8 +--
> >   arch/powerpc/mm/mem.c |  4 +-
> >   arch/powerpc/mm/nohash/40x.c  |  4 +-
> >   arch/powerpc/mm/nohash/book3e_pgtable.c   | 15 ++---
> >   arch/powerpc/mm/pgtable.c | 30 ++
> >   arch/powerpc/mm/pgtable_32.c  | 45 +++---
> >   arch/powerpc/mm/pgtable_64.c  | 10 ++--
> >   arch/powerpc/mm/ptdump/hashpagetable.c| 20 ++-
> >   arch/powerpc/mm/ptdump/ptdump.c   | 14 +++--
> >   arch/powerpc/xmon/xmon.c  | 18 +++---
> >   28 files changed, 213 insertions(+), 184 deletions(-)
> > 
> > diff --git a/arch/powerpc/include/asm/book3s/32/pgtable.h 
> > b/arch/powerpc/include/asm/book3s/32/pgtable.h
> > index 5b39c11e884a..39ec11371be0 100644
> > --- a/arch/powerpc/include/asm/book3s/32/pgtable.h
> > +++ b/arch/powerpc/include/asm/book3s/32/pgtable.h
> > @@ -2,7 +2,6 @@
> >   #ifndef _ASM_POWERPC_BOOK3S_32_PGTABLE_H
> >   #define _ASM_POWERPC_BOOK3S_32_PGTABLE_H
> > -#define 

Re: [PATCH v2 07/13] powerpc: add support for folded p4d page tables

2020-02-26 Thread Christophe Leroy




Le 26/02/2020 à 10:13, Mike Rapoport a écrit :

On Tue, Feb 18, 2020 at 12:54:40PM +0200, Mike Rapoport wrote:

On Sun, Feb 16, 2020 at 11:41:07AM +0100, Christophe Leroy wrote:



Le 16/02/2020 à 09:18, Mike Rapoport a écrit :

From: Mike Rapoport 

Implement primitives necessary for the 4th level folding, add walks of p4d
level where appropriate and replace 5level-fixup.h with pgtable-nop4d.h.


I don't think it is worth adding all this additionnals walks of p4d, this
patch could be limited to changes like:

-   pud = pud_offset(pgd, gpa);
+   pud = pud_offset(p4d_offset(pgd, gpa), gpa);

The additionnal walks should be added through another patch the day powerpc
need them.


Ok, I'll update the patch to reduce walking the p4d.


Here's what I have with more direct acceses from pgd to pud.


I went quickly through. This looks promising.

Do we need the walk_p4d() in arch/powerpc/mm/ptdump/hashpagetable.c ?
Can't we just do

@@ -445,7 +459,7 @@ static void walk_pagetables(struct pg_state *st)
addr = KERN_VIRT_START + i * PGDIR_SIZE;
if (!pgd_none(*pgd))
/* pgd exists */
-   walk_pud(st, pgd, addr);
+   walk_pud(st, p4d_offset(pgd, addr), addr);
}
 }



Also, I think the removal of get_pteptr() should be a patch by itself.

You could include my patches in your series. See 
https://patchwork.ozlabs.org/project/linuxppc-dev/list/?series=152102


Christophe





 From 6c59a86ce8394fb6100e9b6ced2e346981fb0ce9 Mon Sep 17 00:00:00 2001
From: Mike Rapoport 
Date: Sun, 24 Nov 2019 15:38:00 +0200
Subject: [PATCH v3] powerpc: add support for folded p4d page tables

Implement primitives necessary for the 4th level folding, add walks of p4d
level where appropriate and replace 5level-fixup.h with pgtable-nop4d.h.

Signed-off-by: Mike Rapoport 
Tested-by: Christophe Leroy  # 8xx and 83xx
---
v3:
* reduce amount of added p4d walks
* kill pgtable_32::get_pteptr and traverse page table in
   pgtable_32::__change_page_attr_noflush


  arch/powerpc/include/asm/book3s/32/pgtable.h  |  1 -
  arch/powerpc/include/asm/book3s/64/hash.h |  4 +-
  arch/powerpc/include/asm/book3s/64/pgalloc.h  |  4 +-
  arch/powerpc/include/asm/book3s/64/pgtable.h  | 60 ++-
  arch/powerpc/include/asm/book3s/64/radix.h|  6 +-
  arch/powerpc/include/asm/nohash/32/pgtable.h  |  1 -
  arch/powerpc/include/asm/nohash/64/pgalloc.h  |  2 +-
  .../include/asm/nohash/64/pgtable-4k.h| 32 +-
  arch/powerpc/include/asm/nohash/64/pgtable.h  |  6 +-
  arch/powerpc/include/asm/pgtable.h|  6 +-
  arch/powerpc/kvm/book3s_64_mmu_radix.c| 30 ++
  arch/powerpc/lib/code-patching.c  |  7 ++-
  arch/powerpc/mm/book3s32/mmu.c|  2 +-
  arch/powerpc/mm/book3s32/tlb.c|  4 +-
  arch/powerpc/mm/book3s64/hash_pgtable.c   |  4 +-
  arch/powerpc/mm/book3s64/radix_pgtable.c  | 26 +---
  arch/powerpc/mm/book3s64/subpage_prot.c   |  6 +-
  arch/powerpc/mm/hugetlbpage.c | 28 +
  arch/powerpc/mm/kasan/kasan_init_32.c |  8 +--
  arch/powerpc/mm/mem.c |  4 +-
  arch/powerpc/mm/nohash/40x.c  |  4 +-
  arch/powerpc/mm/nohash/book3e_pgtable.c   | 15 ++---
  arch/powerpc/mm/pgtable.c | 30 ++
  arch/powerpc/mm/pgtable_32.c  | 45 +++---
  arch/powerpc/mm/pgtable_64.c  | 10 ++--
  arch/powerpc/mm/ptdump/hashpagetable.c| 20 ++-
  arch/powerpc/mm/ptdump/ptdump.c   | 14 +++--
  arch/powerpc/xmon/xmon.c  | 18 +++---
  28 files changed, 213 insertions(+), 184 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/32/pgtable.h 
b/arch/powerpc/include/asm/book3s/32/pgtable.h
index 5b39c11e884a..39ec11371be0 100644
--- a/arch/powerpc/include/asm/book3s/32/pgtable.h
+++ b/arch/powerpc/include/asm/book3s/32/pgtable.h
@@ -2,7 +2,6 @@
  #ifndef _ASM_POWERPC_BOOK3S_32_PGTABLE_H
  #define _ASM_POWERPC_BOOK3S_32_PGTABLE_H
  
-#define __ARCH_USE_5LEVEL_HACK

  #include 
  
  #include 

diff --git a/arch/powerpc/include/asm/book3s/64/hash.h 
b/arch/powerpc/include/asm/book3s/64/hash.h
index 2781ebf6add4..876d1528c2cf 100644
--- a/arch/powerpc/include/asm/book3s/64/hash.h
+++ b/arch/powerpc/include/asm/book3s/64/hash.h
@@ -134,9 +134,9 @@ static inline int get_region_id(unsigned long ea)
  
  #define	hash__pmd_bad(pmd)		(pmd_val(pmd) & H_PMD_BAD_BITS)

  #define   hash__pud_bad(pud)  (pud_val(pud) & H_PUD_BAD_BITS)
-static inline int hash__pgd_bad(pgd_t pgd)
+static inline int hash__p4d_bad(p4d_t p4d)
  {
-   return (pgd_val(pgd) == 0);
+   return (p4d_val(p4d) == 0);
  }
  #ifdef CONFIG_STRICT_KERNEL_RWX
  extern void hash__mark_rodata_ro(void);
diff --git a/arch/powerpc/include/asm/book3s/64/pgalloc.h 

Re: [PATCH v2 07/13] powerpc: add support for folded p4d page tables

2020-02-26 Thread Mike Rapoport
On Tue, Feb 18, 2020 at 12:54:40PM +0200, Mike Rapoport wrote:
> On Sun, Feb 16, 2020 at 11:41:07AM +0100, Christophe Leroy wrote:
> > 
> > 
> > Le 16/02/2020 à 09:18, Mike Rapoport a écrit :
> > > From: Mike Rapoport 
> > > 
> > > Implement primitives necessary for the 4th level folding, add walks of p4d
> > > level where appropriate and replace 5level-fixup.h with pgtable-nop4d.h.
> > 
> > I don't think it is worth adding all this additionnals walks of p4d, this
> > patch could be limited to changes like:
> > 
> > -   pud = pud_offset(pgd, gpa);
> > +   pud = pud_offset(p4d_offset(pgd, gpa), gpa);
> > 
> > The additionnal walks should be added through another patch the day powerpc
> > need them.
> 
> Ok, I'll update the patch to reduce walking the p4d.

Here's what I have with more direct acceses from pgd to pud.

>From 6c59a86ce8394fb6100e9b6ced2e346981fb0ce9 Mon Sep 17 00:00:00 2001
From: Mike Rapoport 
Date: Sun, 24 Nov 2019 15:38:00 +0200
Subject: [PATCH v3] powerpc: add support for folded p4d page tables

Implement primitives necessary for the 4th level folding, add walks of p4d
level where appropriate and replace 5level-fixup.h with pgtable-nop4d.h.

Signed-off-by: Mike Rapoport 
Tested-by: Christophe Leroy  # 8xx and 83xx
---
v3:
* reduce amount of added p4d walks
* kill pgtable_32::get_pteptr and traverse page table in
  pgtable_32::__change_page_attr_noflush


 arch/powerpc/include/asm/book3s/32/pgtable.h  |  1 -
 arch/powerpc/include/asm/book3s/64/hash.h |  4 +-
 arch/powerpc/include/asm/book3s/64/pgalloc.h  |  4 +-
 arch/powerpc/include/asm/book3s/64/pgtable.h  | 60 ++-
 arch/powerpc/include/asm/book3s/64/radix.h|  6 +-
 arch/powerpc/include/asm/nohash/32/pgtable.h  |  1 -
 arch/powerpc/include/asm/nohash/64/pgalloc.h  |  2 +-
 .../include/asm/nohash/64/pgtable-4k.h| 32 +-
 arch/powerpc/include/asm/nohash/64/pgtable.h  |  6 +-
 arch/powerpc/include/asm/pgtable.h|  6 +-
 arch/powerpc/kvm/book3s_64_mmu_radix.c| 30 ++
 arch/powerpc/lib/code-patching.c  |  7 ++-
 arch/powerpc/mm/book3s32/mmu.c|  2 +-
 arch/powerpc/mm/book3s32/tlb.c|  4 +-
 arch/powerpc/mm/book3s64/hash_pgtable.c   |  4 +-
 arch/powerpc/mm/book3s64/radix_pgtable.c  | 26 +---
 arch/powerpc/mm/book3s64/subpage_prot.c   |  6 +-
 arch/powerpc/mm/hugetlbpage.c | 28 +
 arch/powerpc/mm/kasan/kasan_init_32.c |  8 +--
 arch/powerpc/mm/mem.c |  4 +-
 arch/powerpc/mm/nohash/40x.c  |  4 +-
 arch/powerpc/mm/nohash/book3e_pgtable.c   | 15 ++---
 arch/powerpc/mm/pgtable.c | 30 ++
 arch/powerpc/mm/pgtable_32.c  | 45 +++---
 arch/powerpc/mm/pgtable_64.c  | 10 ++--
 arch/powerpc/mm/ptdump/hashpagetable.c| 20 ++-
 arch/powerpc/mm/ptdump/ptdump.c   | 14 +++--
 arch/powerpc/xmon/xmon.c  | 18 +++---
 28 files changed, 213 insertions(+), 184 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/32/pgtable.h 
b/arch/powerpc/include/asm/book3s/32/pgtable.h
index 5b39c11e884a..39ec11371be0 100644
--- a/arch/powerpc/include/asm/book3s/32/pgtable.h
+++ b/arch/powerpc/include/asm/book3s/32/pgtable.h
@@ -2,7 +2,6 @@
 #ifndef _ASM_POWERPC_BOOK3S_32_PGTABLE_H
 #define _ASM_POWERPC_BOOK3S_32_PGTABLE_H
 
-#define __ARCH_USE_5LEVEL_HACK
 #include 
 
 #include 
diff --git a/arch/powerpc/include/asm/book3s/64/hash.h 
b/arch/powerpc/include/asm/book3s/64/hash.h
index 2781ebf6add4..876d1528c2cf 100644
--- a/arch/powerpc/include/asm/book3s/64/hash.h
+++ b/arch/powerpc/include/asm/book3s/64/hash.h
@@ -134,9 +134,9 @@ static inline int get_region_id(unsigned long ea)
 
 #definehash__pmd_bad(pmd)  (pmd_val(pmd) & H_PMD_BAD_BITS)
 #definehash__pud_bad(pud)  (pud_val(pud) & H_PUD_BAD_BITS)
-static inline int hash__pgd_bad(pgd_t pgd)
+static inline int hash__p4d_bad(p4d_t p4d)
 {
-   return (pgd_val(pgd) == 0);
+   return (p4d_val(p4d) == 0);
 }
 #ifdef CONFIG_STRICT_KERNEL_RWX
 extern void hash__mark_rodata_ro(void);
diff --git a/arch/powerpc/include/asm/book3s/64/pgalloc.h 
b/arch/powerpc/include/asm/book3s/64/pgalloc.h
index a41e91bd0580..69c5b051734f 100644
--- a/arch/powerpc/include/asm/book3s/64/pgalloc.h
+++ b/arch/powerpc/include/asm/book3s/64/pgalloc.h
@@ -85,9 +85,9 @@ static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
kmem_cache_free(PGT_CACHE(PGD_INDEX_SIZE), pgd);
 }
 
-static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, pud_t *pud)
+static inline void p4d_populate(struct mm_struct *mm, p4d_t *pgd, pud_t *pud)
 {
-   *pgd =  __pgd(__pgtable_ptr_val(pud) | PGD_VAL_BITS);
+   *pgd =  __p4d(__pgtable_ptr_val(pud) | PGD_VAL_BITS);
 }
 
 static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
diff --git 

Re: [PATCH v2 07/13] powerpc: add support for folded p4d page tables

2020-02-19 Thread Mike Rapoport
On Wed, Feb 19, 2020 at 01:07:55PM +0100, Christophe Leroy wrote:
> 
> Le 16/02/2020 à 09:18, Mike Rapoport a écrit :
> > diff --git a/arch/powerpc/mm/ptdump/ptdump.c 
> > b/arch/powerpc/mm/ptdump/ptdump.c
> > index 206156255247..7bd4b81d5b5d 100644
> > --- a/arch/powerpc/mm/ptdump/ptdump.c
> > +++ b/arch/powerpc/mm/ptdump/ptdump.c
> > @@ -277,9 +277,9 @@ static void walk_pmd(struct pg_state *st, pud_t *pud, 
> > unsigned long start)
> > }
> >   }
> > -static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
> > +static void walk_pud(struct pg_state *st, p4d_t *p4d, unsigned long start)
> >   {
> > -   pud_t *pud = pud_offset(pgd, 0);
> > +   pud_t *pud = pud_offset(p4d, 0);
> > unsigned long addr;
> > unsigned int i;
> > @@ -293,6 +293,22 @@ static void walk_pud(struct pg_state *st, pgd_t *pgd, 
> > unsigned long start)
> > }
> >   }
> > +static void walk_p4d(struct pg_state *st, pgd_t *pgd, unsigned long start)
> > +{
> > +   p4d_t *p4d = p4d_offset(pgd, 0);
> > +   unsigned long addr;
> > +   unsigned int i;
> > +
> > +   for (i = 0; i < PTRS_PER_P4D; i++, p4d++) {
> > +   addr = start + i * P4D_SIZE;
> > +   if (!p4d_none(*p4d) && !p4d_is_leaf(*p4d))
> > +   /* p4d exists */
> > +   walk_pud(st, p4d, addr);
> > +   else
> > +   note_page(st, addr, 2, p4d_val(*p4d));
> 
> Level 2 is already used by walk_pud().
> 
> I think you have to increment the level used in walk_pud() and walk_pmd()
> and walk_pte()

Thanks for catching this!
I'll fix the numbers in the next version.
 
> > +   }
> > +}
> > +
> >   static void walk_pagetables(struct pg_state *st)
> >   {
> > unsigned int i;
> > @@ -306,7 +322,7 @@ static void walk_pagetables(struct pg_state *st)
> > for (i = pgd_index(addr); i < PTRS_PER_PGD; i++, pgd++, addr += 
> > PGDIR_SIZE) {
> > if (!pgd_none(*pgd) && !pgd_is_leaf(*pgd))
> > /* pgd exists */
> > -   walk_pud(st, pgd, addr);
> > +   walk_p4d(st, pgd, addr);
> > else
> > note_page(st, addr, 1, pgd_val(*pgd));
> > }
> 
> Christophe

-- 
Sincerely yours,
Mike.


Re: [PATCH v2 07/13] powerpc: add support for folded p4d page tables

2020-02-19 Thread Christophe Leroy




Le 16/02/2020 à 09:18, Mike Rapoport a écrit :

From: Mike Rapoport 

Implement primitives necessary for the 4th level folding, add walks of p4d
level where appropriate and replace 5level-fixup.h with pgtable-nop4d.h.

Signed-off-by: Mike Rapoport 
Tested-by: Christophe Leroy  # 8xx and 83xx
---
  arch/powerpc/include/asm/book3s/32/pgtable.h  |  1 -
  arch/powerpc/include/asm/book3s/64/hash.h |  4 +-
  arch/powerpc/include/asm/book3s/64/pgalloc.h  |  4 +-
  arch/powerpc/include/asm/book3s/64/pgtable.h  | 58 ++
  arch/powerpc/include/asm/book3s/64/radix.h|  6 +-
  arch/powerpc/include/asm/nohash/32/pgtable.h  |  1 -
  arch/powerpc/include/asm/nohash/64/pgalloc.h  |  2 +-
  .../include/asm/nohash/64/pgtable-4k.h| 32 +-
  arch/powerpc/include/asm/nohash/64/pgtable.h  |  6 +-
  arch/powerpc/include/asm/pgtable.h|  8 +++
  arch/powerpc/kvm/book3s_64_mmu_radix.c| 59 ---
  arch/powerpc/lib/code-patching.c  |  7 ++-
  arch/powerpc/mm/book3s32/mmu.c|  2 +-
  arch/powerpc/mm/book3s32/tlb.c|  4 +-
  arch/powerpc/mm/book3s64/hash_pgtable.c   |  4 +-
  arch/powerpc/mm/book3s64/radix_pgtable.c  | 19 --
  arch/powerpc/mm/book3s64/subpage_prot.c   |  6 +-
  arch/powerpc/mm/hugetlbpage.c | 28 +
  arch/powerpc/mm/kasan/kasan_init_32.c |  8 +--
  arch/powerpc/mm/mem.c |  4 +-
  arch/powerpc/mm/nohash/40x.c  |  4 +-
  arch/powerpc/mm/nohash/book3e_pgtable.c   | 15 +++--
  arch/powerpc/mm/pgtable.c | 25 +++-
  arch/powerpc/mm/pgtable_32.c  | 28 +
  arch/powerpc/mm/pgtable_64.c  | 10 ++--
  arch/powerpc/mm/ptdump/hashpagetable.c| 20 ++-
  arch/powerpc/mm/ptdump/ptdump.c   | 22 ++-
  arch/powerpc/xmon/xmon.c  | 17 +-
  28 files changed, 284 insertions(+), 120 deletions(-)

diff --git a/arch/powerpc/mm/ptdump/ptdump.c b/arch/powerpc/mm/ptdump/ptdump.c
index 206156255247..7bd4b81d5b5d 100644
--- a/arch/powerpc/mm/ptdump/ptdump.c
+++ b/arch/powerpc/mm/ptdump/ptdump.c
@@ -277,9 +277,9 @@ static void walk_pmd(struct pg_state *st, pud_t *pud, 
unsigned long start)
}
  }
  
-static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)

+static void walk_pud(struct pg_state *st, p4d_t *p4d, unsigned long start)
  {
-   pud_t *pud = pud_offset(pgd, 0);
+   pud_t *pud = pud_offset(p4d, 0);
unsigned long addr;
unsigned int i;
  
@@ -293,6 +293,22 @@ static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)

}
  }
  
+static void walk_p4d(struct pg_state *st, pgd_t *pgd, unsigned long start)

+{
+   p4d_t *p4d = p4d_offset(pgd, 0);
+   unsigned long addr;
+   unsigned int i;
+
+   for (i = 0; i < PTRS_PER_P4D; i++, p4d++) {
+   addr = start + i * P4D_SIZE;
+   if (!p4d_none(*p4d) && !p4d_is_leaf(*p4d))
+   /* p4d exists */
+   walk_pud(st, p4d, addr);
+   else
+   note_page(st, addr, 2, p4d_val(*p4d));


Level 2 is already used by walk_pud().

I think you have to increment the level used in walk_pud() and 
walk_pmd() and walk_pte()



+   }
+}
+
  static void walk_pagetables(struct pg_state *st)
  {
unsigned int i;
@@ -306,7 +322,7 @@ static void walk_pagetables(struct pg_state *st)
for (i = pgd_index(addr); i < PTRS_PER_PGD; i++, pgd++, addr += 
PGDIR_SIZE) {
if (!pgd_none(*pgd) && !pgd_is_leaf(*pgd))
/* pgd exists */
-   walk_pud(st, pgd, addr);
+   walk_p4d(st, pgd, addr);
else
note_page(st, addr, 1, pgd_val(*pgd));
}


Christophe


Re: [PATCH v2 07/13] powerpc: add support for folded p4d page tables

2020-02-18 Thread Mike Rapoport
On Sun, Feb 16, 2020 at 11:41:07AM +0100, Christophe Leroy wrote:
> 
> 
> Le 16/02/2020 à 09:18, Mike Rapoport a écrit :
> > From: Mike Rapoport 
> > 
> > Implement primitives necessary for the 4th level folding, add walks of p4d
> > level where appropriate and replace 5level-fixup.h with pgtable-nop4d.h.
> 
> I don't think it is worth adding all this additionnals walks of p4d, this
> patch could be limited to changes like:
> 
> - pud = pud_offset(pgd, gpa);
> + pud = pud_offset(p4d_offset(pgd, gpa), gpa);
> 
> The additionnal walks should be added through another patch the day powerpc
> need them.

Ok, I'll update the patch to reduce walking the p4d.
 
> See below for more comments.
> 
> > 
> > Signed-off-by: Mike Rapoport 
> > Tested-by: Christophe Leroy  # 8xx and 83xx
> > ---

...

> > diff --git a/arch/powerpc/include/asm/book3s/64/pgtable.h 
> > b/arch/powerpc/include/asm/book3s/64/pgtable.h
> > index 201a69e6a355..bafff0ab 100644
> > --- a/arch/powerpc/include/asm/book3s/64/pgtable.h
> > +++ b/arch/powerpc/include/asm/book3s/64/pgtable.h
> > @@ -2,7 +2,7 @@
> >   #ifndef _ASM_POWERPC_BOOK3S_64_PGTABLE_H_
> >   #define _ASM_POWERPC_BOOK3S_64_PGTABLE_H_
> > -#include 
> > +#include 
> >   #ifndef __ASSEMBLY__
> >   #include 
> > @@ -251,7 +251,7 @@ extern unsigned long __pmd_frag_size_shift;
> >   /* Bits to mask out from a PUD to get to the PMD page */
> >   #define PUD_MASKED_BITS   0xc0ffUL
> >   /* Bits to mask out from a PGD to get to the PUD page */
> > -#define PGD_MASKED_BITS0xc0ffUL
> > +#define P4D_MASKED_BITS0xc0ffUL
> >   /*
> >* Used as an indicator for rcu callback functions
> > @@ -949,54 +949,60 @@ static inline bool pud_access_permitted(pud_t pud, 
> > bool write)
> > return pte_access_permitted(pud_pte(pud), write);
> >   }
> > -#define pgd_write(pgd) pte_write(pgd_pte(pgd))
> > +#define __p4d_raw(x)   ((p4d_t) { __pgd_raw(x) })
> > +static inline __be64 p4d_raw(p4d_t x)
> > +{
> > +   return pgd_raw(x.pgd);
> > +}
> > +
> 
> Shouldn't this be defined in asm/pgtable-be-types.h, just like other
> __pxx_raw() ?

Ideally yes, but this creates weird header file dependencies and untangling
them would generate way too much churn.
 
> > +#define p4d_write(p4d) pte_write(p4d_pte(p4d))
> > -static inline void pgd_clear(pgd_t *pgdp)
> > +static inline void p4d_clear(p4d_t *p4dp)
> >   {
> > -   *pgdp = __pgd(0);
> > +   *p4dp = __p4d(0);
> >   }

...

> > @@ -573,9 +596,15 @@ int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, 
> > pte_t pte,
> > /* Traverse the guest's 2nd-level tree, allocate new levels needed */
> > pgd = pgtable + pgd_index(gpa);
> > -   pud = NULL;
> > +   p4d = NULL;
> > if (pgd_present(*pgd))
> > -   pud = pud_offset(pgd, gpa);
> > +   p4d = p4d_offset(pgd, gpa);
> > +   else
> > +   new_p4d = p4d_alloc_one(kvm->mm, gpa);
> > +
> > +   pud = NULL;
> > +   if (p4d_present(*p4d))
> > +   pud = pud_offset(p4d, gpa);
> 
> Is it worth adding all this new code ?
> 
> My understanding is that the series objective is to get rid of
> __ARCH_HAS_5LEVEL_HACK, to to add support for 5 levels to an architecture
> that not need it (at least for now).
> If we want to add support for 5 levels, it can be done later in another
> patch.
> 
> Here I think your change could be limited to:
> 
> - pud = pud_offset(pgd, gpa);
> + pud = pud_offset(p4d_offset(pgd, gpa), gpa);

This won't work. Without __ARCH_USE_5LEVEL_HACK defined pgd_present() is
hardwired to 1 and the actual check for the top level is performed with
p4d_present(). The 'else' clause that allocates p4d will never be taken and
it could be removed, but I prefer to keep it for consistency.
 
> > else
> > new_pud = pud_alloc_one(kvm->mm, gpa);
> > @@ -597,12 +626,18 @@ int kvmppc_create_pte(struct kvm *kvm, pgd_t 
> > *pgtable, pte_t pte,
> > /* Now traverse again under the lock and change the tree */
> > ret = -ENOMEM;
> > if (pgd_none(*pgd)) {
> > +   if (!new_p4d)
> > +   goto out_unlock;
> > +   pgd_populate(kvm->mm, pgd, new_p4d);
> > +   new_p4d = NULL;
> > +   }
> > +   if (p4d_none(*p4d)) {
> > if (!new_pud)
> > goto out_unlock;
> > -   pgd_populate(kvm->mm, pgd, new_pud);
> > +   p4d_populate(kvm->mm, p4d, new_pud);
> > new_pud = NULL;
> > }
> > -   pud = pud_offset(pgd, gpa);
> > +   pud = pud_offset(p4d, gpa);
> > if (pud_is_leaf(*pud)) {
> > unsigned long hgpa = gpa & PUD_MASK;
> > @@ -1220,6 +1255,7 @@ static ssize_t debugfs_radix_read(struct file *file, 
> > char __user *buf,
> > pgd_t *pgt;
> > struct kvm_nested_guest *nested;
> > pgd_t pgd, *pgdp;
> > +   p4d_t p4d, *p4dp;
> > pud_t pud, *pudp;
> > pmd_t pmd, *pmdp;
> > pte_t *ptep;
> > @@ -1298,7 

Re: [PATCH v2 07/13] powerpc: add support for folded p4d page tables

2020-02-16 Thread Christophe Leroy




Le 16/02/2020 à 09:18, Mike Rapoport a écrit :

From: Mike Rapoport 

Implement primitives necessary for the 4th level folding, add walks of p4d
level where appropriate and replace 5level-fixup.h with pgtable-nop4d.h.


I don't think it is worth adding all this additionnals walks of p4d, 
this patch could be limited to changes like:


-   pud = pud_offset(pgd, gpa);
+   pud = pud_offset(p4d_offset(pgd, gpa), gpa);

The additionnal walks should be added through another patch the day 
powerpc need them.


See below for more comments.



Signed-off-by: Mike Rapoport 
Tested-by: Christophe Leroy  # 8xx and 83xx
---
  arch/powerpc/include/asm/book3s/32/pgtable.h  |  1 -
  arch/powerpc/include/asm/book3s/64/hash.h |  4 +-
  arch/powerpc/include/asm/book3s/64/pgalloc.h  |  4 +-
  arch/powerpc/include/asm/book3s/64/pgtable.h  | 58 ++
  arch/powerpc/include/asm/book3s/64/radix.h|  6 +-
  arch/powerpc/include/asm/nohash/32/pgtable.h  |  1 -
  arch/powerpc/include/asm/nohash/64/pgalloc.h  |  2 +-
  .../include/asm/nohash/64/pgtable-4k.h| 32 +-
  arch/powerpc/include/asm/nohash/64/pgtable.h  |  6 +-
  arch/powerpc/include/asm/pgtable.h|  8 +++
  arch/powerpc/kvm/book3s_64_mmu_radix.c| 59 ---
  arch/powerpc/lib/code-patching.c  |  7 ++-
  arch/powerpc/mm/book3s32/mmu.c|  2 +-
  arch/powerpc/mm/book3s32/tlb.c|  4 +-
  arch/powerpc/mm/book3s64/hash_pgtable.c   |  4 +-
  arch/powerpc/mm/book3s64/radix_pgtable.c  | 19 --
  arch/powerpc/mm/book3s64/subpage_prot.c   |  6 +-
  arch/powerpc/mm/hugetlbpage.c | 28 +
  arch/powerpc/mm/kasan/kasan_init_32.c |  8 +--
  arch/powerpc/mm/mem.c |  4 +-
  arch/powerpc/mm/nohash/40x.c  |  4 +-
  arch/powerpc/mm/nohash/book3e_pgtable.c   | 15 +++--
  arch/powerpc/mm/pgtable.c | 25 +++-
  arch/powerpc/mm/pgtable_32.c  | 28 +
  arch/powerpc/mm/pgtable_64.c  | 10 ++--
  arch/powerpc/mm/ptdump/hashpagetable.c| 20 ++-
  arch/powerpc/mm/ptdump/ptdump.c   | 22 ++-
  arch/powerpc/xmon/xmon.c  | 17 +-
  28 files changed, 284 insertions(+), 120 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/32/pgtable.h 
b/arch/powerpc/include/asm/book3s/32/pgtable.h
index 5b39c11e884a..39ec11371be0 100644
--- a/arch/powerpc/include/asm/book3s/32/pgtable.h
+++ b/arch/powerpc/include/asm/book3s/32/pgtable.h
@@ -2,7 +2,6 @@
  #ifndef _ASM_POWERPC_BOOK3S_32_PGTABLE_H
  #define _ASM_POWERPC_BOOK3S_32_PGTABLE_H
  
-#define __ARCH_USE_5LEVEL_HACK

  #include 
  
  #include 

diff --git a/arch/powerpc/include/asm/book3s/64/hash.h 
b/arch/powerpc/include/asm/book3s/64/hash.h
index 2781ebf6add4..876d1528c2cf 100644
--- a/arch/powerpc/include/asm/book3s/64/hash.h
+++ b/arch/powerpc/include/asm/book3s/64/hash.h
@@ -134,9 +134,9 @@ static inline int get_region_id(unsigned long ea)
  
  #define	hash__pmd_bad(pmd)		(pmd_val(pmd) & H_PMD_BAD_BITS)

  #define   hash__pud_bad(pud)  (pud_val(pud) & H_PUD_BAD_BITS)
-static inline int hash__pgd_bad(pgd_t pgd)
+static inline int hash__p4d_bad(p4d_t p4d)
  {
-   return (pgd_val(pgd) == 0);
+   return (p4d_val(p4d) == 0);
  }
  #ifdef CONFIG_STRICT_KERNEL_RWX
  extern void hash__mark_rodata_ro(void);
diff --git a/arch/powerpc/include/asm/book3s/64/pgalloc.h 
b/arch/powerpc/include/asm/book3s/64/pgalloc.h
index a41e91bd0580..69c5b051734f 100644
--- a/arch/powerpc/include/asm/book3s/64/pgalloc.h
+++ b/arch/powerpc/include/asm/book3s/64/pgalloc.h
@@ -85,9 +85,9 @@ static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
kmem_cache_free(PGT_CACHE(PGD_INDEX_SIZE), pgd);
  }
  
-static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, pud_t *pud)

+static inline void p4d_populate(struct mm_struct *mm, p4d_t *pgd, pud_t *pud)
  {
-   *pgd =  __pgd(__pgtable_ptr_val(pud) | PGD_VAL_BITS);
+   *pgd =  __p4d(__pgtable_ptr_val(pud) | PGD_VAL_BITS);
  }
  
  static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)

diff --git a/arch/powerpc/include/asm/book3s/64/pgtable.h 
b/arch/powerpc/include/asm/book3s/64/pgtable.h
index 201a69e6a355..bafff0ab 100644
--- a/arch/powerpc/include/asm/book3s/64/pgtable.h
+++ b/arch/powerpc/include/asm/book3s/64/pgtable.h
@@ -2,7 +2,7 @@
  #ifndef _ASM_POWERPC_BOOK3S_64_PGTABLE_H_
  #define _ASM_POWERPC_BOOK3S_64_PGTABLE_H_
  
-#include 

+#include 
  
  #ifndef __ASSEMBLY__

  #include 
@@ -251,7 +251,7 @@ extern unsigned long __pmd_frag_size_shift;
  /* Bits to mask out from a PUD to get to the PMD page */
  #define PUD_MASKED_BITS   0xc0ffUL
  /* Bits to mask out from a PGD to get to the PUD page */
-#define PGD_MASKED_BITS0xc0ffUL
+#define P4D_MASKED_BITS

[PATCH v2 07/13] powerpc: add support for folded p4d page tables

2020-02-16 Thread Mike Rapoport
From: Mike Rapoport 

Implement primitives necessary for the 4th level folding, add walks of p4d
level where appropriate and replace 5level-fixup.h with pgtable-nop4d.h.

Signed-off-by: Mike Rapoport 
Tested-by: Christophe Leroy  # 8xx and 83xx
---
 arch/powerpc/include/asm/book3s/32/pgtable.h  |  1 -
 arch/powerpc/include/asm/book3s/64/hash.h |  4 +-
 arch/powerpc/include/asm/book3s/64/pgalloc.h  |  4 +-
 arch/powerpc/include/asm/book3s/64/pgtable.h  | 58 ++
 arch/powerpc/include/asm/book3s/64/radix.h|  6 +-
 arch/powerpc/include/asm/nohash/32/pgtable.h  |  1 -
 arch/powerpc/include/asm/nohash/64/pgalloc.h  |  2 +-
 .../include/asm/nohash/64/pgtable-4k.h| 32 +-
 arch/powerpc/include/asm/nohash/64/pgtable.h  |  6 +-
 arch/powerpc/include/asm/pgtable.h|  8 +++
 arch/powerpc/kvm/book3s_64_mmu_radix.c| 59 ---
 arch/powerpc/lib/code-patching.c  |  7 ++-
 arch/powerpc/mm/book3s32/mmu.c|  2 +-
 arch/powerpc/mm/book3s32/tlb.c|  4 +-
 arch/powerpc/mm/book3s64/hash_pgtable.c   |  4 +-
 arch/powerpc/mm/book3s64/radix_pgtable.c  | 19 --
 arch/powerpc/mm/book3s64/subpage_prot.c   |  6 +-
 arch/powerpc/mm/hugetlbpage.c | 28 +
 arch/powerpc/mm/kasan/kasan_init_32.c |  8 +--
 arch/powerpc/mm/mem.c |  4 +-
 arch/powerpc/mm/nohash/40x.c  |  4 +-
 arch/powerpc/mm/nohash/book3e_pgtable.c   | 15 +++--
 arch/powerpc/mm/pgtable.c | 25 +++-
 arch/powerpc/mm/pgtable_32.c  | 28 +
 arch/powerpc/mm/pgtable_64.c  | 10 ++--
 arch/powerpc/mm/ptdump/hashpagetable.c| 20 ++-
 arch/powerpc/mm/ptdump/ptdump.c   | 22 ++-
 arch/powerpc/xmon/xmon.c  | 17 +-
 28 files changed, 284 insertions(+), 120 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/32/pgtable.h 
b/arch/powerpc/include/asm/book3s/32/pgtable.h
index 5b39c11e884a..39ec11371be0 100644
--- a/arch/powerpc/include/asm/book3s/32/pgtable.h
+++ b/arch/powerpc/include/asm/book3s/32/pgtable.h
@@ -2,7 +2,6 @@
 #ifndef _ASM_POWERPC_BOOK3S_32_PGTABLE_H
 #define _ASM_POWERPC_BOOK3S_32_PGTABLE_H
 
-#define __ARCH_USE_5LEVEL_HACK
 #include 
 
 #include 
diff --git a/arch/powerpc/include/asm/book3s/64/hash.h 
b/arch/powerpc/include/asm/book3s/64/hash.h
index 2781ebf6add4..876d1528c2cf 100644
--- a/arch/powerpc/include/asm/book3s/64/hash.h
+++ b/arch/powerpc/include/asm/book3s/64/hash.h
@@ -134,9 +134,9 @@ static inline int get_region_id(unsigned long ea)
 
 #definehash__pmd_bad(pmd)  (pmd_val(pmd) & H_PMD_BAD_BITS)
 #definehash__pud_bad(pud)  (pud_val(pud) & H_PUD_BAD_BITS)
-static inline int hash__pgd_bad(pgd_t pgd)
+static inline int hash__p4d_bad(p4d_t p4d)
 {
-   return (pgd_val(pgd) == 0);
+   return (p4d_val(p4d) == 0);
 }
 #ifdef CONFIG_STRICT_KERNEL_RWX
 extern void hash__mark_rodata_ro(void);
diff --git a/arch/powerpc/include/asm/book3s/64/pgalloc.h 
b/arch/powerpc/include/asm/book3s/64/pgalloc.h
index a41e91bd0580..69c5b051734f 100644
--- a/arch/powerpc/include/asm/book3s/64/pgalloc.h
+++ b/arch/powerpc/include/asm/book3s/64/pgalloc.h
@@ -85,9 +85,9 @@ static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
kmem_cache_free(PGT_CACHE(PGD_INDEX_SIZE), pgd);
 }
 
-static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, pud_t *pud)
+static inline void p4d_populate(struct mm_struct *mm, p4d_t *pgd, pud_t *pud)
 {
-   *pgd =  __pgd(__pgtable_ptr_val(pud) | PGD_VAL_BITS);
+   *pgd =  __p4d(__pgtable_ptr_val(pud) | PGD_VAL_BITS);
 }
 
 static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
diff --git a/arch/powerpc/include/asm/book3s/64/pgtable.h 
b/arch/powerpc/include/asm/book3s/64/pgtable.h
index 201a69e6a355..bafff0ab 100644
--- a/arch/powerpc/include/asm/book3s/64/pgtable.h
+++ b/arch/powerpc/include/asm/book3s/64/pgtable.h
@@ -2,7 +2,7 @@
 #ifndef _ASM_POWERPC_BOOK3S_64_PGTABLE_H_
 #define _ASM_POWERPC_BOOK3S_64_PGTABLE_H_
 
-#include 
+#include 
 
 #ifndef __ASSEMBLY__
 #include 
@@ -251,7 +251,7 @@ extern unsigned long __pmd_frag_size_shift;
 /* Bits to mask out from a PUD to get to the PMD page */
 #define PUD_MASKED_BITS0xc0ffUL
 /* Bits to mask out from a PGD to get to the PUD page */
-#define PGD_MASKED_BITS0xc0ffUL
+#define P4D_MASKED_BITS0xc0ffUL
 
 /*
  * Used as an indicator for rcu callback functions
@@ -949,54 +949,60 @@ static inline bool pud_access_permitted(pud_t pud, bool 
write)
return pte_access_permitted(pud_pte(pud), write);
 }
 
-#define pgd_write(pgd) pte_write(pgd_pte(pgd))
+#define __p4d_raw(x)   ((p4d_t) { __pgd_raw(x) })
+static inline __be64 p4d_raw(p4d_t x)
+{
+   return pgd_raw(x.pgd);
+}
+
+#define p4d_write(p4d)