Skip to contentSkip to conversation

arm64: tlb: fix the TTL value of tlb_get_level

PATCH Applied Zhenyu Ye 3 participants 6 messages Jun 23, 2021 – Jun 24, 2021

Subsystems (4)
lore
Share & tools
lore

Zhenyu Ye

author
#0 lore raw

PATCH 1/1 arm64: tlb: fix the TTL value of tlb_get_level

The TTL field indicates the level of page table walk holding the *leaf* entry for the address being invalidated. But currently, the TTL field may be set to an incorrent value in the following stack:

pte_free_tlb

    __pte_free_tlb
        tlb_remove_table
            tlb_table_invalidate
                tlb_flush_mmu_tlbonly
                    tlb_flush

In this case, we just want to flush a PTE page, but the tlb->cleared_pmds is set and we get tlb_level = 2 in the tlb_get_level() function. This may cause some unexpected problems.

This patch set the TTL field to 0 if tlb->freed_tables is set. The tlb->freed_tables indicates page table pages are freed, not the leaf entry.

Fixes c4ab2cbc1d87 ("arm64: tlb: Set the TTL field in flush_tlb_range")
---
 arch/arm64/include/asm/tlb.h | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm64/include/asm/tlb.h +4 -0
index 61c97d3b58c7..c995d1f4594f 100644
@@ -28,6 +28,10 @@ static void tlb_flush(struct mmu_gather *tlb);
  */
 static inline int tlb_get_level(struct mmu_gather *tlb)
 {
+	/* The TTL field is only valid for the leaf entry. */
+	if (tlb->freed_tables)
+		return 0;
+
 	if (tlb->cleared_ptes && !(tlb->cleared_pmds ||
 				   tlb->cleared_puds ||
 				   tlb->cleared_p4ds))
Signature · 1 line
2.26.2
maintainer
#1 lore raw
Quoted 31 lines · Zhenyu Ye

The TTL field indicates the level of page table walk holding the *leaf* entry for the address being invalidated. But currently, the TTL field may be set to an incorrent value in the following stack:

pte_free_tlb

    __pte_free_tlb
        tlb_remove_table
            tlb_table_invalidate
                tlb_flush_mmu_tlbonly
                    tlb_flush

In this case, we just want to flush a PTE page, but the tlb->cleared_pmds is set and we get tlb_level = 2 in the tlb_get_level() function. This may cause some unexpected problems.

This patch set the TTL field to 0 if tlb->freed_tables is set. The tlb->freed_tables indicates page table pages are freed, not the leaf entry.

Fixes c4ab2cbc1d87 ("arm64: tlb: Set the TTL field in flush_tlb_range")
---
 arch/arm64/include/asm/tlb.h | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm64/include/asm/tlb.h +0 -0
index 61c97d3b58c7..c995d1f4594f 100644

arch/arm64/include/asm/tlb.h static void tlb_flush(struct mmu_gather *tlb); · line 37

Quoted 10 lines
arch/arm64/include/asm/tlb.h +4 -0
@@ -28,6 +28,10 @@ static void tlb_flush(struct mmu_gather *tlb);
  */
 static inline int tlb_get_level(struct mmu_gather *tlb)
 {
+	/* The TTL field is only valid for the leaf entry. */
+	if (tlb->freed_tables)
+		return 0;
+
 	if (tlb->cleared_ptes && !(tlb->cleared_pmds ||
 				   tlb->cleared_puds ||
 				   tlb->cleared_p4ds))

Thanks. I can't see a better way around this, so I'll queue the patch. The stage-2 page-table code looks ok afaict, but please can you check it too?

Cheers,

Will

maintainer Acked-by
#2 lore raw
Quoted 36 lines · Will Deacon
Quoted 35 lines · Zhenyu Ye

The TTL field indicates the level of page table walk holding the *leaf* entry for the address being invalidated. But currently, the TTL field may be set to an incorrent value in the following stack:

pte_free_tlb

    __pte_free_tlb
        tlb_remove_table
            tlb_table_invalidate
                tlb_flush_mmu_tlbonly
                    tlb_flush

In this case, we just want to flush a PTE page, but the tlb->cleared_pmds is set and we get tlb_level = 2 in the tlb_get_level() function. This may cause some unexpected problems.

This patch set the TTL field to 0 if tlb->freed_tables is set. The tlb->freed_tables indicates page table pages are freed, not the leaf entry.

Fixes c4ab2cbc1d87 ("arm64: tlb: Set the TTL field in flush_tlb_range")
---
 arch/arm64/include/asm/tlb.h | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm64/include/asm/tlb.h +1 -0
index 61c97d3b58c7..c995d1f4594f 100644
@@ -28,6 +28,10 @@ static void tlb_flush(struct mmu_gather *tlb);
  */
 static inline int tlb_get_level(struct mmu_gather *tlb)
 {
+	/* The TTL field is only valid for the leaf entry. */
Quoted 8 lines

arch/arm64/include/asm/tlb.h static void tlb_flush(struct mmu_gather *tlb); · line 37

Quoted 6 lines
arch/arm64/include/asm/tlb.h +3 -0
+	if (tlb->freed_tables)
+		return 0;
+
 	if (tlb->cleared_ptes && !(tlb->cleared_pmds ||
 				   tlb->cleared_puds ||
 				   tlb->cleared_p4ds))

Thanks. I can't see a better way around this, so I'll queue the patch.

If you haven't queued it already, feel free to add:

I'd also add:

Cc <[email protected]> # 5.9.x
Signature · 1 line
Catalin
author
#3 lore raw
In reply to Will Deacon
Quoted 12 lines · Will Deacon
Quoted 11 lines · Zhenyu Ye
arch/arm64/include/asm/tlb.h +3 -0
index 61c97d3b58c7..c995d1f4594f 100644
@@ -28,6 +28,10 @@ static void tlb_flush(struct mmu_gather *tlb);
  */
 static inline int tlb_get_level(struct mmu_gather *tlb)
 {
+	/* The TTL field is only valid for the leaf entry. */
+	if (tlb->freed_tables)
+		return 0;
Quoted 8 lines

arch/arm64/include/asm/tlb.h static void tlb_flush(struct mmu_gather *tlb); · line 37

Quoted 4 lines
arch/arm64/include/asm/tlb.h +1 -0
+
 	if (tlb->cleared_ptes && !(tlb->cleared_pmds ||
 				   tlb->cleared_puds ||
 				   tlb->cleared_p4ds))

Thanks. I can't see a better way around this, so I'll queue the patch. The stage-2 page-table code looks ok afaict, but please can you check it too?

The stage-2 page-table codes seem to be correct to me.

Thanks,
Zhenyu

maintainer
#4 lore raw
Quoted 22 lines · Zhenyu Ye
Quoted 12 lines · Will Deacon
Quoted 11 lines · Zhenyu Ye
arch/arm64/include/asm/tlb.h +3 -0
index 61c97d3b58c7..c995d1f4594f 100644
@@ -28,6 +28,10 @@ static void tlb_flush(struct mmu_gather *tlb);
  */
 static inline int tlb_get_level(struct mmu_gather *tlb)
 {
+	/* The TTL field is only valid for the leaf entry. */
+	if (tlb->freed_tables)
+		return 0;
Quoted 8 lines

arch/arm64/include/asm/tlb.h static void tlb_flush(struct mmu_gather *tlb); · line 37

Quoted 4 lines
arch/arm64/include/asm/tlb.h +1 -0
+
 	if (tlb->cleared_ptes && !(tlb->cleared_pmds ||
 				   tlb->cleared_puds ||
 				   tlb->cleared_p4ds))

Thanks. I can't see a better way around this, so I'll queue the patch. The stage-2 page-table code looks ok afaict, but please can you check it too?

Quoted 1 line

The stage-2 page-table codes seem to be correct to me.

Thanks for having a look.

Will

maintainer Applied
#5 lore raw
In reply to Zhenyu Ye
Quoted 4 lines · Zhenyu Ye

The TTL field indicates the level of page table walk holding the *leaf* entry for the address being invalidated. But currently, the TTL field may be set to an incorrent value in the following stack:

Quoted 8 lines

pte_free_tlb

    __pte_free_tlb
        tlb_remove_table
            tlb_table_invalidate
                tlb_flush_mmu_tlbonly
                    tlb_flush

[...]

Applied to arm64 (for-next/mm), thanks!

[1/1] arm64: tlb: fix the TTL value of tlb_get_level

      https://git.kernel.org/arm64/c/52218fcd61cb

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev