Skip to contentSkip to conversation

arm64: fix pud_huge() for 2-level pagetables

PATCH In review Mark Salter 3 participants 9 messages May 15, 2014 – May 16, 2014 arm64

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: fix pud_huge() for 2-level pagetables

The following happens when trying to run a kvm guest on a kernel configured for 64k pages. This doesn't happen with 4k pages:

  BUG: failure at include/linux/mm.h:297/put_page_testzero()!
  Kernel panic - not syncing: BUG!
  CPU: 2 PID: 4228 Comm: qemu-system-aar Tainted: GF            3.13.0-0.rc7.31.sa2.k32v1.aarch64.debug #1
  Call trace:
  [<fffffe0000096034>] dump_backtrace+0x0/0x16c
  [<fffffe00000961b4>] show_stack+0x14/0x1c
  [<fffffe000066e648>] dump_stack+0x84/0xb0
  [<fffffe0000668678>] panic+0xf4/0x220
  [<fffffe000018ec78>] free_reserved_area+0x0/0x110
  [<fffffe000018edd8>] free_pages+0x50/0x88
  [<fffffe00000a759c>] kvm_free_stage2_pgd+0x30/0x40
  [<fffffe00000a5354>] kvm_arch_destroy_vm+0x18/0x44
  [<fffffe00000a1854>] kvm_put_kvm+0xf0/0x184
  [<fffffe00000a1938>] kvm_vm_release+0x10/0x1c
  [<fffffe00001edc1c>] __fput+0xb0/0x288
  [<fffffe00001ede4c>] ____fput+0xc/0x14
  [<fffffe00000d5a2c>] task_work_run+0xa8/0x11c
  [<fffffe0000095c14>] do_notify_resume+0x54/0x58

In arch/arm/kvm/mmu.c:unmap_range(), we end up doing an extra put_page() on the stage2 pgd which leads to the BUG in put_page_testzero(). This happens because a pud_huge() test in unmap_range() returns true when it should always be false with 2-level pages tables used by 64k pages. This patch removes support for huge puds if 2-level pagetables are being used.

---
 arch/arm64/mm/hugetlbpage.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/arm64/mm/hugetlbpage.c +6 -0
index 5e9aec3..9bed38f 100644
@@ -51,7 +51,11 @@ int pmd_huge(pmd_t pmd)
 
 int pud_huge(pud_t pud)
 {
+#ifndef __PAGETABLE_PMD_FOLDED
 	return !(pud_val(pud) & PUD_TABLE_BIT);
+#else
+	return 0;
+#endif
 }
 
 int pmd_huge_support(void)
@@ -64,8 +68,10 @@ static __init int setup_hugepagesz(char *opt)
 	unsigned long ps = memparse(opt, &opt);
 	if (ps == PMD_SIZE) {
 		hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
+#ifndef __PAGETABLE_PMD_FOLDED
 	} else if (ps == PUD_SIZE) {
 		hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
+#endif
 	} else {
 		pr_err("hugepagesz: Unsupported page size %lu M\n", ps >> 20);
 		return 0;
Signature · 1 line
1.9.0
#1 lore raw
Quoted 22 lines · Mark Salter

The following happens when trying to run a kvm guest on a kernel configured for 64k pages. This doesn't happen with 4k pages:

  BUG: failure at include/linux/mm.h:297/put_page_testzero()!
  Kernel panic - not syncing: BUG!
  CPU: 2 PID: 4228 Comm: qemu-system-aar Tainted: GF            3.13.0-0.rc7.31.sa2.k32v1.aarch64.debug #1
  Call trace:
  [<fffffe0000096034>] dump_backtrace+0x0/0x16c
  [<fffffe00000961b4>] show_stack+0x14/0x1c
  [<fffffe000066e648>] dump_stack+0x84/0xb0
  [<fffffe0000668678>] panic+0xf4/0x220
  [<fffffe000018ec78>] free_reserved_area+0x0/0x110
  [<fffffe000018edd8>] free_pages+0x50/0x88
  [<fffffe00000a759c>] kvm_free_stage2_pgd+0x30/0x40
  [<fffffe00000a5354>] kvm_arch_destroy_vm+0x18/0x44
  [<fffffe00000a1854>] kvm_put_kvm+0xf0/0x184
  [<fffffe00000a1938>] kvm_vm_release+0x10/0x1c
  [<fffffe00001edc1c>] __fput+0xb0/0x288
  [<fffffe00001ede4c>] ____fput+0xc/0x14
  [<fffffe00000d5a2c>] task_work_run+0xa8/0x11c
  [<fffffe0000095c14>] do_notify_resume+0x54/0x58
Quoted 6 lines

In arch/arm/kvm/mmu.c:unmap_range(), we end up doing an extra put_page() on the stage2 pgd which leads to the BUG in put_page_testzero(). This happens because a pud_huge() test in unmap_range() returns true when it should always be false with 2-level pages tables used by 64k pages. This patch removes support for huge puds if 2-level pagetables are being used.

Hi Mark,
I'm still catching up with myself, sorry (was off sick for a couple of days)...

I thought unmap_range was going to be changed?
Does the following help things?
https://lists.cs.columbia.edu/pipermail/kvmarm/2014-May/009388.html

Cheers,

Signature · 1 line
Steve
Quoted 36 lines
---
 arch/arm64/mm/hugetlbpage.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/arm64/mm/hugetlbpage.c +6 -0
index 5e9aec3..9bed38f 100644
@@ -51,7 +51,11 @@ int pmd_huge(pmd_t pmd)
 
 int pud_huge(pud_t pud)
 {
+#ifndef __PAGETABLE_PMD_FOLDED
 	return !(pud_val(pud) & PUD_TABLE_BIT);
+#else
+	return 0;
+#endif
 }
 
 int pmd_huge_support(void)
@@ -64,8 +68,10 @@ static __init int setup_hugepagesz(char *opt)
 	unsigned long ps = memparse(opt, &opt);
 	if (ps == PMD_SIZE) {
 		hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
+#ifndef __PAGETABLE_PMD_FOLDED
 	} else if (ps == PUD_SIZE) {
 		hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
+#endif
 	} else {
 		pr_err("hugepagesz: Unsupported page size %lu M\n", ps >> 20);
 		return 0;
Signature · 1 line
1.9.0
#2 lore raw
Quoted 30 lines · Steve Capper
Quoted 22 lines · Mark Salter

The following happens when trying to run a kvm guest on a kernel configured for 64k pages. This doesn't happen with 4k pages:

  BUG: failure at include/linux/mm.h:297/put_page_testzero()!
  Kernel panic - not syncing: BUG!
  CPU: 2 PID: 4228 Comm: qemu-system-aar Tainted: GF            3.13.0-0.rc7.31.sa2.k32v1.aarch64.debug #1
  Call trace:
  [<fffffe0000096034>] dump_backtrace+0x0/0x16c
  [<fffffe00000961b4>] show_stack+0x14/0x1c
  [<fffffe000066e648>] dump_stack+0x84/0xb0
  [<fffffe0000668678>] panic+0xf4/0x220
  [<fffffe000018ec78>] free_reserved_area+0x0/0x110
  [<fffffe000018edd8>] free_pages+0x50/0x88
  [<fffffe00000a759c>] kvm_free_stage2_pgd+0x30/0x40
  [<fffffe00000a5354>] kvm_arch_destroy_vm+0x18/0x44
  [<fffffe00000a1854>] kvm_put_kvm+0xf0/0x184
  [<fffffe00000a1938>] kvm_vm_release+0x10/0x1c
  [<fffffe00001edc1c>] __fput+0xb0/0x288
  [<fffffe00001ede4c>] ____fput+0xc/0x14
  [<fffffe00000d5a2c>] task_work_run+0xa8/0x11c
  [<fffffe0000095c14>] do_notify_resume+0x54/0x58
Quoted 6 lines

In arch/arm/kvm/mmu.c:unmap_range(), we end up doing an extra put_page() on the stage2 pgd which leads to the BUG in put_page_testzero(). This happens because a pud_huge() test in unmap_range() returns true when it should always be false with 2-level pages tables used by 64k pages. This patch removes support for huge puds if 2-level pagetables are being used.

Quoted 7 lines

Hi Mark,
I'm still catching up with myself, sorry (was off sick for a couple of days)...

I thought unmap_range was going to be changed?
Does the following help things?
https://lists.cs.columbia.edu/pipermail/kvmarm/2014-May/009388.html

No, I get the same BUG. Regardless, pud_huge() should always return false for 2-level pagetables, right?

#3 lore raw
Quoted 35 lines · Mark Salter
Quoted 34 lines · Steve Capper
Quoted 22 lines · Mark Salter

The following happens when trying to run a kvm guest on a kernel configured for 64k pages. This doesn't happen with 4k pages:

  BUG: failure at include/linux/mm.h:297/put_page_testzero()!
  Kernel panic - not syncing: BUG!
  CPU: 2 PID: 4228 Comm: qemu-system-aar Tainted: GF            3.13.0-0.rc7.31.sa2.k32v1.aarch64.debug #1
  Call trace:
  [<fffffe0000096034>] dump_backtrace+0x0/0x16c
  [<fffffe00000961b4>] show_stack+0x14/0x1c
  [<fffffe000066e648>] dump_stack+0x84/0xb0
  [<fffffe0000668678>] panic+0xf4/0x220
  [<fffffe000018ec78>] free_reserved_area+0x0/0x110
  [<fffffe000018edd8>] free_pages+0x50/0x88
  [<fffffe00000a759c>] kvm_free_stage2_pgd+0x30/0x40
  [<fffffe00000a5354>] kvm_arch_destroy_vm+0x18/0x44
  [<fffffe00000a1854>] kvm_put_kvm+0xf0/0x184
  [<fffffe00000a1938>] kvm_vm_release+0x10/0x1c
  [<fffffe00001edc1c>] __fput+0xb0/0x288
  [<fffffe00001ede4c>] ____fput+0xc/0x14
  [<fffffe00000d5a2c>] task_work_run+0xa8/0x11c
  [<fffffe0000095c14>] do_notify_resume+0x54/0x58
Quoted 6 lines

In arch/arm/kvm/mmu.c:unmap_range(), we end up doing an extra put_page() on the stage2 pgd which leads to the BUG in put_page_testzero(). This happens because a pud_huge() test in unmap_range() returns true when it should always be false with 2-level pages tables used by 64k pages. This patch removes support for huge puds if 2-level pagetables are being used.

Hi Mark,
I'm still catching up with myself, sorry (was off sick for a couple of days)...

Quoted 6 lines
Quoted 3 lines

I thought unmap_range was going to be changed?
Does the following help things?
https://lists.cs.columbia.edu/pipermail/kvmarm/2014-May/009388.html

No, I get the same BUG. Regardless, pud_huge() should always return false for 2-level pagetables, right?

Okay, thanks for giving that a go.

Yeah I agree for 64K granule it doesn't make sense to have a huge_pud. The patch looks sound now, but checking for a folded pmd may run into problems if/when we get to 3-levels and 64K pages in future.

Perhaps checking for PAGE_SHIFT==12 (or something similar) would be a bit more robust?

Cheers,

Signature · 1 line
Steve
Quoted 2 lines
#4 lore raw
Quoted 43 lines · Steve Capper
Quoted 35 lines · Mark Salter
Quoted 34 lines · Steve Capper
Quoted 22 lines · Mark Salter

The following happens when trying to run a kvm guest on a kernel configured for 64k pages. This doesn't happen with 4k pages:

  BUG: failure at include/linux/mm.h:297/put_page_testzero()!
  Kernel panic - not syncing: BUG!
  CPU: 2 PID: 4228 Comm: qemu-system-aar Tainted: GF            3.13.0-0.rc7.31.sa2.k32v1.aarch64.debug #1
  Call trace:
  [<fffffe0000096034>] dump_backtrace+0x0/0x16c
  [<fffffe00000961b4>] show_stack+0x14/0x1c
  [<fffffe000066e648>] dump_stack+0x84/0xb0
  [<fffffe0000668678>] panic+0xf4/0x220
  [<fffffe000018ec78>] free_reserved_area+0x0/0x110
  [<fffffe000018edd8>] free_pages+0x50/0x88
  [<fffffe00000a759c>] kvm_free_stage2_pgd+0x30/0x40
  [<fffffe00000a5354>] kvm_arch_destroy_vm+0x18/0x44
  [<fffffe00000a1854>] kvm_put_kvm+0xf0/0x184
  [<fffffe00000a1938>] kvm_vm_release+0x10/0x1c
  [<fffffe00001edc1c>] __fput+0xb0/0x288
  [<fffffe00001ede4c>] ____fput+0xc/0x14
  [<fffffe00000d5a2c>] task_work_run+0xa8/0x11c
  [<fffffe0000095c14>] do_notify_resume+0x54/0x58
Quoted 6 lines

In arch/arm/kvm/mmu.c:unmap_range(), we end up doing an extra put_page() on the stage2 pgd which leads to the BUG in put_page_testzero(). This happens because a pud_huge() test in unmap_range() returns true when it should always be false with 2-level pages tables used by 64k pages. This patch removes support for huge puds if 2-level pagetables are being used.

Hi Mark,
I'm still catching up with myself, sorry (was off sick for a couple of days)...

Quoted 6 lines
Quoted 3 lines

I thought unmap_range was going to be changed?
Does the following help things?
https://lists.cs.columbia.edu/pipermail/kvmarm/2014-May/009388.html

No, I get the same BUG. Regardless, pud_huge() should always return false for 2-level pagetables, right?

Quoted 9 lines

Okay, thanks for giving that a go.

Yeah I agree for 64K granule it doesn't make sense to have a huge_pud. The patch looks sound now, but checking for a folded pmd may run into problems if/when we get to 3-levels and 64K pages in future.

Perhaps checking for PAGE_SHIFT==12 (or something similar) would be a bit more robust?

I don't think testing based on granule size is generally correct either. Maybe support for 3-level page tables with 64k granule gets added as an option. That would break the pagesize based test. With a folded pmd, we know there is no pud, so pud_huge() should always be false.

maintainer
#5 lore raw
Quoted 10 lines · Mark Salter
Quoted 9 lines · Steve Capper
Quoted 8 lines · Mark Salter
Quoted 7 lines · Steve Capper
Quoted 6 lines · Mark Salter

In arch/arm/kvm/mmu.c:unmap_range(), we end up doing an extra put_page() on the stage2 pgd which leads to the BUG in put_page_testzero(). This happens because a pud_huge() test in unmap_range() returns true when it should always be false with 2-level pages tables used by 64k pages. This patch removes support for huge puds if 2-level pagetables are being used.

[...]

Quoted 11 lines
Quoted 6 lines

Yeah I agree for 64K granule it doesn't make sense to have a huge_pud. The patch looks sound now, but checking for a folded pmd may run into problems if/when we get to 3-levels and 64K pages in future.

Perhaps checking for PAGE_SHIFT==12 (or something similar) would be a bit more robust?

I don't think testing based on granule size is generally correct either. Maybe support for 3-level page tables with 64k granule gets added as an option. That would break the pagesize based test. With a folded pmd, we know there is no pud, so pud_huge() should always be false.

I agree, pud_huge() should be false in the same way we define pud_present() to be 1 when __PGTABLE_PMD_FOLDED. The *_huge() macros aren't covered by the generic headers unfortunately (some clean-up would be useful at some point but for now this patch is fine).

Signature · 1 line
Catalin
maintainer
#6 lore raw
In reply to Mark Salter
Quoted 17 lines · Mark Salter
arch/arm64/mm/hugetlbpage.c +4 -0
index 5e9aec3..9bed38f 100644
@@ -51,7 +51,11 @@ int pmd_huge(pmd_t pmd)
 
 int pud_huge(pud_t pud)
 {
+#ifndef __PAGETABLE_PMD_FOLDED
 	return !(pud_val(pud) & PUD_TABLE_BIT);
+#else
+	return 0;
+#endif
 }
 
 int pmd_huge_support(void)

arch/arm64/mm/hugetlbpage.c static __init int setup_hugepagesz(char *opt) · line 74

Quoted 7 lines
arch/arm64/mm/hugetlbpage.c +2 -0
@@ -64,8 +68,10 @@ static __init int setup_hugepagesz(char *opt)
 	unsigned long ps = memparse(opt, &opt);
 	if (ps == PMD_SIZE) {
 		hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
+#ifndef __PAGETABLE_PMD_FOLDED
 	} else if (ps == PUD_SIZE) {
 		hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
+#endif

Since PMD_SIZE == PUD_SIZE when __PAGETABLE_PMD_FOLDED, do we need the #ifndef here? Maybe the compiler is smart enough to remove it but it's not on a critical path anyway, so I wouldn't bother.

Signature · 1 line
Catalin
#7 lore raw
Quoted 16 lines · Catalin Marinas
Quoted 15 lines · Mark Salter
arch/arm64/mm/hugetlbpage.c +4 -0
index 5e9aec3..9bed38f 100644
@@ -51,7 +51,11 @@ int pmd_huge(pmd_t pmd)
 
 int pud_huge(pud_t pud)
 {
+#ifndef __PAGETABLE_PMD_FOLDED
 	return !(pud_val(pud) & PUD_TABLE_BIT);
+#else
+	return 0;
+#endif
 }
 
Quoted 14 lines

arch/arm64/mm/hugetlbpage.c static __init int setup_hugepagesz(char *opt) · line 74

Quoted 9 lines
arch/arm64/mm/hugetlbpage.c +2 -0
 int pmd_huge_support(void)
@@ -64,8 +68,10 @@ static __init int setup_hugepagesz(char *opt)
 	unsigned long ps = memparse(opt, &opt);
 	if (ps == PMD_SIZE) {
 		hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
+#ifndef __PAGETABLE_PMD_FOLDED
 	} else if (ps == PUD_SIZE) {
 		hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
+#endif

Since PMD_SIZE == PUD_SIZE when __PAGETABLE_PMD_FOLDED, do we need the #ifndef here? Maybe the compiler is smart enough to remove it but it's not on a critical path anyway, so I wouldn't bother.

Yes, I think it would remove it. In any case, one less ifdef would be a good thing.

maintainer
#8 lore raw
Quoted 31 lines · Mark Salter
Quoted 16 lines · Catalin Marinas
Quoted 15 lines · Mark Salter
arch/arm64/mm/hugetlbpage.c +4 -0
index 5e9aec3..9bed38f 100644
@@ -51,7 +51,11 @@ int pmd_huge(pmd_t pmd)
 
 int pud_huge(pud_t pud)
 {
+#ifndef __PAGETABLE_PMD_FOLDED
 	return !(pud_val(pud) & PUD_TABLE_BIT);
+#else
+	return 0;
+#endif
 }
 
Quoted 13 lines

arch/arm64/mm/hugetlbpage.c static __init int setup_hugepagesz(char *opt) · line 74

Quoted 9 lines
arch/arm64/mm/hugetlbpage.c +2 -0
 int pmd_huge_support(void)
@@ -64,8 +68,10 @@ static __init int setup_hugepagesz(char *opt)
 	unsigned long ps = memparse(opt, &opt);
 	if (ps == PMD_SIZE) {
 		hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
+#ifndef __PAGETABLE_PMD_FOLDED
 	} else if (ps == PUD_SIZE) {
 		hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
+#endif

Since PMD_SIZE == PUD_SIZE when __PAGETABLE_PMD_FOLDED, do we need the #ifndef here? Maybe the compiler is smart enough to remove it but it's not on a critical path anyway, so I wouldn't bother.

Quoted 2 lines

Yes, I think it would remove it. In any case, one less ifdef would be a good thing.

I merged this patch and dropped the last #ifndef.

I still have doubts about the kvm code calling put_page more than necessary, especially since pud == pmd and the loop continues after pud_huge() returns true, but your patch looks harmless.

Unless Steve has any objection, I'll push it to mainline. I also added
Cc: stable # v3.11+

Thanks.

Signature · 1 line
Catalin