Skip to contentSkip to conversation

mm: hugetlb: fix mremap tlb flush

PATCH In review Kefeng Wang 3 participants 12 messages Aug 1, 2023 – Aug 2, 2023

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

v2 cover mm: hugetlb: fix mremap tlb flush

The first patch uses correct flush tlb functions when move page tables, and second patch is a small tlb flush optimization for hugepage on arm64.

v2:
- drop uncorrected move_normal_pmd/pud() changes
- add flush_hugetlb_tlb_range() on arm64 instead of changing generic flush_tlb_range()
- collect RB/ACK

Kefeng Wang (2):
mm: hugetlb: use flush_hugetlb_tlb_range() in
move_hugetlb_page_tables()
arm64: hugetlb: enable __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE

 arch/arm64/include/asm/hugetlb.h | 12 ++++++++++++
 mm/hugetlb.c                     |  4 ++--
 2 files changed, 14 insertions(+), 2 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH v2 1/2 mm: hugetlb: use flush_hugetlb_tlb_range() in move_hugetlb_page_tables()

Archs may need to do special things when flushing hugepage tlb, so use the more applicable flush_hugetlb_tlb_range() instead of flush_tlb_range().

Fixes 550a7d60bd5e ("mm, hugepages: add mremap() support for hugepage backed vma")
---
 mm/hugetlb.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/hugetlb.c +2 -2
index 64a3239b6407..ac876bfba340 100644
@@ -5281,9 +5281,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
 	}
 
 	if (shared_pmd)
-		flush_tlb_range(vma, range.start, range.end);
+		flush_hugetlb_tlb_range(vma, range.start, range.end);
 	else
-		flush_tlb_range(vma, old_end - len, old_end);
+		flush_hugetlb_tlb_range(vma, old_end - len, old_end);
 	mmu_notifier_invalidate_range_end(&range);
 	i_mmap_unlock_write(mapping);
 	hugetlb_vma_unlock_write(vma);
Signature · 1 line
2.41.0
#2 lore raw

PATCH v2 2/2 arm64: hugetlb: enable __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE

In reply to Kefeng Wang

It is better to use huge page size instead of PAGE_SIZE for stride when flush hugepage, which reduces the loop in __flush_tlb_range().

Let's support arch's flush_hugetlb_tlb_range(), which is used in hugetlb_unshare_all_pmds(), move_hugetlb_page_tables() and hugetlb_change_protection() for now.

Note, for hugepages based on contiguous bit, it has to be invalidated individually since the contiguous PTE bit is just a hint, the hardware may or may not take it into account.

---
 arch/arm64/include/asm/hugetlb.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/arm64/include/asm/hugetlb.h +12 -0
index 6a4a1ab8eb23..e5c2e3dd9cf0 100644
@@ -60,4 +60,16 @@ extern void huge_ptep_modify_prot_commit(struct vm_area_struct *vma,
 
 #include <asm-generic/hugetlb.h>
 
+#define __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
+static inline void flush_hugetlb_tlb_range(struct vm_area_struct *vma,
+					   unsigned long start,
+					   unsigned long end)
+{
+	unsigned long stride = huge_page_size(hstate_vma(vma));
+
+	if (stride != PMD_SIZE && stride != PUD_SIZE)
+		stride = PAGE_SIZE;
+	__flush_tlb_range(vma, start, end, stride, false, 0);
+}
+
 #endif /* __ASM_HUGETLB_H */
Signature · 1 line
2.41.0
maintainer Reviewed-by on v2 2/2
#3 lore raw

Re: [PATCH v2 2/2] arm64: hugetlb: enable __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE

Quoted 12 lines · Kefeng Wang

It is better to use huge page size instead of PAGE_SIZE for stride when flush hugepage, which reduces the loop in __flush_tlb_range().

Let's support arch's flush_hugetlb_tlb_range(), which is used in hugetlb_unshare_all_pmds(), move_hugetlb_page_tables() and hugetlb_change_protection() for now.

Quoted 5 lines

Note, for hugepages based on contiguous bit, it has to be invalidated individually since the contiguous PTE bit is just a hint, the hardware may or may not take it into account.

maintainer
#4 lore raw

Re: [PATCH v2 2/2] arm64: hugetlb: enable __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE

In reply to Kefeng Wang

arch/arm64/include/asm/hugetlb.h extern void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, · line 72

Quoted 10 lines · Kefeng Wang
arch/arm64/include/asm/hugetlb.h +10 -0
+#define __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
+static inline void flush_hugetlb_tlb_range(struct vm_area_struct *vma,
+					   unsigned long start,
+					   unsigned long end)
+{
+	unsigned long stride = huge_page_size(hstate_vma(vma));
+
+	if (stride != PMD_SIZE && stride != PUD_SIZE)
+		stride = PAGE_SIZE;
+	__flush_tlb_range(vma, start, end, stride, false, 0);

We could use some hints here for the tlb_level (2 for pmd, 1 for pud). Regarding the last_level argument to __flush_tlb_range(), I think it needs to stay false since this function is also called on the hugetlb_unshare_pmds() path where the pud is cleared and needs invalidating.

That said, maybe you can rewrite it as a switch statement and call flush_pmd_tlb_range() or flush_pud_tlb_range() (just make sure these are defined when CONFIG_HUGETLBFS is enabled).

Signature · 1 line
Catalin
#5 lore raw

Re: [PATCH v2 2/2] arm64: hugetlb: enable __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE

Quoted 17 lines · Catalin Marinas

Review

Quoted 10 lines · Kefeng Wang
(quoted diff) +10 -0
+#define __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
+static inline void flush_hugetlb_tlb_range(struct vm_area_struct *vma,
+					   unsigned long start,
+					   unsigned long end)
+{
+	unsigned long stride = huge_page_size(hstate_vma(vma));
+
+	if (stride != PMD_SIZE && stride != PUD_SIZE)
+		stride = PAGE_SIZE;
+	__flush_tlb_range(vma, start, end, stride, false, 0);

We could use some hints here for the tlb_level (2 for pmd, 1 for pud). Regarding the last_level argument to __flush_tlb_range(), I think it needs to stay false since this function is also called on the hugetlb_unshare_pmds() path where the pud is cleared and needs invalidating.

Quoted 4 lines
Quoted 1 line

That said, maybe you can rewrite it as a switch statement and call

flush_pmd_tlb_range() or flush_pud_tlb_range() (just make sure these are defined when CONFIG_HUGETLBFS is enabled).

How about this way, not involved with thp ?

arch/arm64/include/asm/hugetlb.h +14 -2
index e5c2e3dd9cf0..a7ce59d3388e 100644
@@ -66,10 +66,22 @@ static inline void flush_hugetlb_tlb_range(struct vm_area_struct *vma,
                                           unsigned long end)
 {
        unsigned long stride = huge_page_size(hstate_vma(vma));
+       int tlb_level = 0;
 
-       if (stride != PMD_SIZE && stride != PUD_SIZE)
+       switch (stride) {
+#ifndef __PAGETABLE_PMD_FOLDED
+       case PUD_SIZE:
+               tlb_level = 1;
+               break;
+#endif
+       case PMD_SIZE:
+               tlb_level = 2;
+               break;
+       default:
                stride = PAGE_SIZE;
-       __flush_tlb_range(vma, start, end, stride, false, 0);
+       }
+
+       __flush_tlb_range(vma, start, end, stride, false, tlb_level);
 }
 
 
#6 lore raw

Re: [PATCH v2 2/2] arm64: hugetlb: enable __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE

Quoted 15 lines · Kefeng Wang
Quoted 12 lines · Catalin Marinas
Quoted 10 lines · Kefeng Wang
(quoted diff) +10 -0
+#define __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
+static inline void flush_hugetlb_tlb_range(struct vm_area_struct *vma,
+                       unsigned long start,
+                       unsigned long end)
+{
+    unsigned long stride = huge_page_size(hstate_vma(vma));
+
+    if (stride != PMD_SIZE && stride != PUD_SIZE)
+        stride = PAGE_SIZE;
+    __flush_tlb_range(vma, start, end, stride, false, 0);
Quoted 8 lines
Quoted 8 lines

We could use some hints here for the tlb_level (2 for pmd, 1 for pud). Regarding the last_level argument to __flush_tlb_range(), I think it needs to stay false since this function is also called on the hugetlb_unshare_pmds() path where the pud is cleared and needs invalidating.
 > That said, maybe you can rewrite it as a switch statement and call flush_pmd_tlb_range() or flush_pud_tlb_range() (just make sure these are defined when CONFIG_HUGETLBFS is enabled).

I try the way your mentioned, it won't change much, will send v3, thanks.

#7 lore raw

PATCH v3 1/1 arm64: hugetlb: enable __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE

In reply to Kefeng Wang

It is better to use huge page size instead of PAGE_SIZE for stride when flush hugepage, which reduces the loop in __flush_tlb_range().

Let's support arch's flush_hugetlb_tlb_range(), which is used in hugetlb_unshare_all_pmds(), move_hugetlb_page_tables() and hugetlb_change_protection() for now.

Note, for hugepages based on contiguous bit, it has to be invalidated individually since the contiguous PTE bit is just a hint, the hardware may or may not take it into account.

---
v3: add tlb_level hint by using flush_pud/pmd_tlb_range,
suggested by Catalin Marinas

 arch/arm64/include/asm/hugetlb.h | 21 +++++++++++++++++++++
 arch/arm64/include/asm/pgtable.h |  4 ++--
 2 files changed, 23 insertions(+), 2 deletions(-)
arch/arm64/include/asm/hugetlb.h +21 -0
index 6a4a1ab8eb23..0acb1e8b41e9 100644
@@ -60,4 +60,25 @@ extern void huge_ptep_modify_prot_commit(struct vm_area_struct *vma,
 
 #include <asm-generic/hugetlb.h>
 
+#define __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
+static inline void flush_hugetlb_tlb_range(struct vm_area_struct *vma,
+					   unsigned long start,
+					   unsigned long end)
+{
+	unsigned long stride = huge_page_size(hstate_vma(vma));
+
+	switch (stride) {
+#ifndef __PAGETABLE_PMD_FOLDED
+	case PUD_SIZE:
+		flush_pud_tlb_range(vma, start, end);
+		break;
+#endif
+	case PMD_SIZE:
+		flush_pmd_tlb_range(vma, start, end);
+		break;
+	default:
+		__flush_tlb_range(vma, start, end, PAGE_SIZE, false, 0);
+	}
+}
+
 #endif /* __ASM_HUGETLB_H */
arch/arm64/include/asm/pgtable.h +2 -2
index 0bd18de9fd97..def402afcbe9 100644
@@ -35,7 +35,7 @@
 #include <linux/sched.h>
 #include <linux/page_table_check.h>
 
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLB_PAGE)
 #define __HAVE_ARCH_FLUSH_PMD_TLB_RANGE
 
 /* Set stride and tlb_level in flush_*_tlb_range */
@@ -43,7 +43,7 @@
 	__flush_tlb_range(vma, addr, end, PMD_SIZE, false, 2)
 #define flush_pud_tlb_range(vma, addr, end)	\
 	__flush_tlb_range(vma, addr, end, PUD_SIZE, false, 1)
-#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
+#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLB_PAGE */
 
 static inline bool arch_thp_swp_supported(void)
 {
Signature · 1 line
2.41.0
maintainer
#8 lore raw

Re: [PATCH v3] arm64: hugetlb: enable __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE

Quoted 10 lines · Kefeng Wang
arch/arm64/include/asm/hugetlb.h +10 -0
+#define __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
+static inline void flush_hugetlb_tlb_range(struct vm_area_struct *vma,
+					   unsigned long start,
+					   unsigned long end)
+{
+	unsigned long stride = huge_page_size(hstate_vma(vma));
+
+	switch (stride) {
+#ifndef __PAGETABLE_PMD_FOLDED
+	case PUD_SIZE:

arch/arm64/include/asm/hugetlb.h extern void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, · line 82

Quoted 10 lines
arch/arm64/include/asm/hugetlb.h +10 -0
+		flush_pud_tlb_range(vma, start, end);
+		break;
+#endif
+	case PMD_SIZE:
+		flush_pmd_tlb_range(vma, start, end);
+		break;
+	default:
+		__flush_tlb_range(vma, start, end, PAGE_SIZE, false, 0);
+	}
+}

I think we should be consistent and either use __flush_tlb_range() everywhere or flush_p*d_tlb_range() together with flush_tlb_range(). Maybe using __flush_tlb_range() for the pmd/pud is not too bad, smaller patch.

That said, I'd avoid the #ifndef and just go for an if/else statement:

	if (stride == PMD_SIZE)
		__flush_tlb_range(vma, start, end, stride, false, 2);
	else if (stride == PUD_SIZE)
		__flush_tlb_range(vma, start, end, stride, false, 1);
	else
		__flush_tlb_range(vma, start, end, PAGE_SIZE, 0);

With the pmd folded, the P*D_SIZE is the same and the compiler should eliminate the second branch.

Signature · 1 line
Catalin
#9 lore raw

PATCH v4 1/1 arm64: hugetlb: enable __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE

In reply to Kefeng Wang

It is better to use huge page size instead of PAGE_SIZE for stride when flush hugepage, which reduces the loop in __flush_tlb_range().

Let's support arch's flush_hugetlb_tlb_range(), which is used in hugetlb_unshare_all_pmds(), move_hugetlb_page_tables() and hugetlb_change_protection() for now.

Note, for hugepages based on contiguous bit, it has to be invalidated individually since the contiguous PTE bit is just a hint, the hardware may or may not take it into account.

---
v4: directly pass tlb_level to __flush_tlb_range() with PMD/PUD size,
suggested by Catalin
v3: add tlb_level hint by using flush_pud/pmd_tlb_range,
suggested by Catalin

 arch/arm64/include/asm/hugetlb.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/arm64/include/asm/hugetlb.h +15 -0
index 6a4a1ab8eb23..a91d6219aa78 100644
@@ -60,4 +60,19 @@ extern void huge_ptep_modify_prot_commit(struct vm_area_struct *vma,
 
 #include <asm-generic/hugetlb.h>
 
+#define __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
+static inline void flush_hugetlb_tlb_range(struct vm_area_struct *vma,
+					   unsigned long start,
+					   unsigned long end)
+{
+	unsigned long stride = huge_page_size(hstate_vma(vma));
+
+	if (stride == PMD_SIZE)
+		__flush_tlb_range(vma, start, end, stride, false, 2);
+	else if (stride == PUD_SIZE)
+		__flush_tlb_range(vma, start, end, stride, false, 1);
+	else
+		__flush_tlb_range(vma, start, end, PAGE_SIZE, false, 0);
+}
+
 #endif /* __ASM_HUGETLB_H */
Signature · 1 line
2.41.0
maintainer Reviewed-by on v4 1/1
#10 lore raw

Re: [PATCH v4] arm64: hugetlb: enable __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE

Quoted 12 lines · Kefeng Wang

It is better to use huge page size instead of PAGE_SIZE for stride when flush hugepage, which reduces the loop in __flush_tlb_range().

Let's support arch's flush_hugetlb_tlb_range(), which is used in hugetlb_unshare_all_pmds(), move_hugetlb_page_tables() and hugetlb_change_protection() for now.

Quoted 5 lines

Note, for hugepages based on contiguous bit, it has to be invalidated individually since the contiguous PTE bit is just a hint, the hardware may or may not take it into account.

maintainer Reviewed-by on v4 1/1
#11 lore raw

Re: [PATCH v4] arm64: hugetlb: enable __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE

In reply to Kefeng Wang
Quoted 8 lines · Kefeng Wang

It is better to use huge page size instead of PAGE_SIZE for stride when flush hugepage, which reduces the loop in __flush_tlb_range().

Let's support arch's flush_hugetlb_tlb_range(), which is used in hugetlb_unshare_all_pmds(), move_hugetlb_page_tables() and hugetlb_change_protection() for now.

Quoted 5 lines

Note, for hugepages based on contiguous bit, it has to be invalidated individually since the contiguous PTE bit is just a hint, the hardware may or may not take it into account.