Skip to contentSkip to conversation

Add update_mmu_tlb_range() to simplify code

PATCH Superseded Bang Li 2 participants 8 messages Apr 29, 2024 – May 5, 2024

Subsystems (6)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Bang Li

author
#0 lore raw

cover Add update_mmu_tlb_range() to simplify code

This series of commits mainly adds the update_mmu_tlb_range() to batch update tlb in an address range.

After the commit 19eaf44954df ("mm: thp: support allocation of anonymous multi-size THP"), We may need to batch update tlb of a certain address range by calling update_mmu_tlb() in a loop. Using the update_mmu_tlb_range(), we can simplify the code and possibly reduce the execution of some unnecessary code in some architectures.

Bang Li (5):
LoongArch: Add update_mmu_tlb_range()
mips: Add update_mmu_tlb_range()
riscv: Add update_mmu_tlb_range()
xtensa: Add update_mmu_tlb_range()
mm: Add update_mmu_tlb_range()

 arch/loongarch/include/asm/pgtable.h | 2 ++
 arch/mips/include/asm/pgtable.h      | 2 ++
 arch/riscv/include/asm/pgtable.h     | 2 ++
 arch/xtensa/include/asm/pgtable.h    | 2 ++
 arch/xtensa/mm/tlb.c                 | 6 ++++++
 include/linux/pgtable.h              | 5 +++++
 mm/memory.c                          | 4 +---
 7 files changed, 20 insertions(+), 3 deletions(-)
Signature · 1 line
2.19.1.6.gb485710b
author
#1 lore raw

PATCH 1/5 LoongArch: Add update_mmu_tlb_range()

Added update_mmu_tlb_range function, we can batch update tlb of an address range.

---
 arch/loongarch/include/asm/pgtable.h | 2 ++
 1 file changed, 2 insertions(+)
arch/loongarch/include/asm/pgtable.h +2 -0
index af3acdf3481a..5ccc2a3a6f7a 100644
@@ -469,6 +469,8 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf,
 
 #define __HAVE_ARCH_UPDATE_MMU_TLB
 #define update_mmu_tlb	update_mmu_cache
+#define update_mmu_tlb_range(vma, addr, ptep, nr) \
+	update_mmu_cache_range(NULL, vma, addr, ptep, nr)
 
 static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
 			unsigned long address, pmd_t *pmdp)
Signature · 1 line
2.19.1.6.gb485710b
author
#2 lore raw

PATCH 2/5 mips: Add update_mmu_tlb_range()

In reply to Bang Li

Added update_mmu_tlb_range function, we can batch update tlb of an address range.

---
 arch/mips/include/asm/pgtable.h | 2 ++
 1 file changed, 2 insertions(+)
arch/mips/include/asm/pgtable.h +2 -0
index e27a4c83c548..0891ad7d43b6 100644
@@ -596,6 +596,8 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf,
 
 #define	__HAVE_ARCH_UPDATE_MMU_TLB
 #define update_mmu_tlb	update_mmu_cache
+#define update_mmu_tlb_range(vma, address, ptep, nr) \
+	update_mmu_cache_range(NULL, vma, address, ptep, nr)
 
 static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
 	unsigned long address, pmd_t *pmdp)
Signature · 1 line
2.19.1.6.gb485710b
author
#3 lore raw

PATCH 3/5 riscv: Add update_mmu_tlb_range()

In reply to Bang Li

Added update_mmu_tlb_range function, we can batch update tlb of an address range.

---
 arch/riscv/include/asm/pgtable.h | 2 ++
 1 file changed, 2 insertions(+)
arch/riscv/include/asm/pgtable.h +2 -0
index f2d5973a011b..d515a11a52cd 100644
@@ -488,6 +488,8 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf,
 
 #define __HAVE_ARCH_UPDATE_MMU_TLB
 #define update_mmu_tlb update_mmu_cache
+#define update_mmu_tlb_range(vma, addr, ptep, nr) \
+	update_mmu_cache_range(NULL, vma, addr, ptep, nr)
 
 static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
 		unsigned long address, pmd_t *pmdp)
Signature · 1 line
2.19.1.6.gb485710b
author
#4 lore raw

PATCH 4/5 xtensa: Add update_mmu_tlb_range()

In reply to Bang Li

Added update_mmu_tlb_range function, we can batch update tlb of an address range.

---
 arch/xtensa/include/asm/pgtable.h | 2 ++
 arch/xtensa/mm/tlb.c              | 6 ++++++
 2 files changed, 8 insertions(+)
arch/xtensa/include/asm/pgtable.h +2 -0
index 9a7e5e57ee9a..50ccfc988256 100644
@@ -412,6 +412,8 @@ typedef pte_t *pte_addr_t;
 
 void update_mmu_tlb(struct vm_area_struct *vma,
 		    unsigned long address, pte_t *ptep);
+void update_mmu_tlb_range(struct vm_area_struct *vma,
+			unsigned long address, pte_t *ptep, unsigned int nr);
 #define __HAVE_ARCH_UPDATE_MMU_TLB
 
 #endif /* !defined (__ASSEMBLY__) */
arch/xtensa/mm/tlb.c +6 -0
index d8b60d6e50a8..05efba86b870 100644
@@ -169,6 +169,12 @@ void update_mmu_tlb(struct vm_area_struct *vma,
 	local_flush_tlb_page(vma, address);
 }
 
+void update_mmu_tlb_range(struct vm_area_struct *vma,
+			unsigned long address, pte_t *ptep, unsigned int nr)
+{
+	local_flush_tlb_range(vma, address, address + PAGE_SIZE * nr);
+}
+
 #ifdef CONFIG_DEBUG_TLB_SANITY
 
 static unsigned get_pte_for_vaddr(unsigned vaddr)
Signature · 1 line
2.19.1.6.gb485710b
author
#5 lore raw

PATCH 5/5 mm: Add update_mmu_tlb_range()

In reply to Bang Li

After the commit 19eaf44954df ("mm: thp: support allocation of anonymous multi-size THP"), it may need to batch update tlb of an address range through the update_mmu_tlb function. We can simplify this operation by adding the update_mmu_tlb_range function, which may also reduce the execution of some unnecessary code in some architectures.

---
 include/linux/pgtable.h | 5 +++++
 mm/memory.c             | 4 +---
 2 files changed, 6 insertions(+), 3 deletions(-)
include/linux/pgtable.h +5 -0
index 18019f037bae..73411dfebf7a 100644
@@ -734,6 +734,11 @@ static inline void update_mmu_tlb(struct vm_area_struct *vma,
 				unsigned long address, pte_t *ptep)
 {
 }
+
+static inline void update_mmu_tlb_range(struct vm_area_struct *vma,
+				unsigned long address, pte_t *ptep, unsigned int nr)
+{
+}
 #define __HAVE_ARCH_UPDATE_MMU_TLB
 #endif
 
mm/memory.c +1 -3
index 6647685fd3c4..1f0ca362b82a 100644
@@ -4396,7 +4396,6 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
 	vm_fault_t ret = 0;
 	int nr_pages = 1;
 	pte_t entry;
-	int i;
 
 	/* File mapping without ->vm_ops ? */
 	if (vma->vm_flags & VM_SHARED)
@@ -4465,8 +4464,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
 		update_mmu_tlb(vma, addr, vmf->pte);
 		goto release;
 	} else if (nr_pages > 1 && !pte_range_none(vmf->pte, nr_pages)) {
-		for (i = 0; i < nr_pages; i++)
-			update_mmu_tlb(vma, addr + PAGE_SIZE * i, vmf->pte + i);
+		update_mmu_tlb_range(vma, addr, vmf->pte, nr_pages);
 		goto release;
 	}
 
Signature · 1 line
2.19.1.6.gb485710b
#6 lore raw

Re: [PATCH v1 5/5] mm: Add update_mmu_tlb_range()

Hey Bang,

Quoted 18 lines · Bang Li

After the commit 19eaf44954df ("mm: thp: support allocation of anonymous multi-size THP"), it may need to batch update tlb of an address range through the update_mmu_tlb function. We can simplify this operation by adding the update_mmu_tlb_range function, which may also reduce the execution of some unnecessary code in some architectures.

---
 include/linux/pgtable.h | 5 +++++
 mm/memory.c             | 4 +---
 2 files changed, 6 insertions(+), 3 deletions(-)
include/linux/pgtable.h +0 -0
index 18019f037bae..73411dfebf7a 100644

include/linux/pgtable.h static inline void update_mmu_tlb(struct vm_area_struct *vma, · line 743

Quoted 10 lines
include/linux/pgtable.h +5 -0
@@ -734,6 +734,11 @@ static inline void update_mmu_tlb(struct vm_area_struct *vma,
                                unsigned long address, pte_t *ptep)
 {
 }
+
+static inline void update_mmu_tlb_range(struct vm_area_struct *vma,
+                               unsigned long address, pte_t *ptep, unsigned int nr)
+{
+}
 #define __HAVE_ARCH_UPDATE_MMU_TLB
 #endif

IMO, it might be better to use a separate definition to determine whether update_mmu_tlb_range() is overridden by a specific architecture.

Thanks,
Lance

Quoted 26 lines
mm/memory.c +1 -4
index 6647685fd3c4..1f0ca362b82a 100644
@@ -4396,7 +4396,6 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
        vm_fault_t ret = 0;
        int nr_pages = 1;
        pte_t entry;
-       int i;
 
        /* File mapping without ->vm_ops ? */
        if (vma->vm_flags & VM_SHARED)
@@ -4465,8 +4464,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
                update_mmu_tlb(vma, addr, vmf->pte);
                goto release;
        } else if (nr_pages > 1 && !pte_range_none(vmf->pte, nr_pages)) {
-               for (i = 0; i < nr_pages; i++)
-                       update_mmu_tlb(vma, addr + PAGE_SIZE * i, vmf->pte + i);
+               update_mmu_tlb_range(vma, addr, vmf->pte, nr_pages);
                goto release;
        }
 
--

2.19.1.6.gb485710b

author
#7 lore raw

Re: [PATCH v1 5/5] mm: Add update_mmu_tlb_range()

Hey Lance,

Thanks for taking time to review!

Quoted 26 lines · Lance Yang

Hey Bang,

Quoted 23 lines · Bang Li

After the commit 19eaf44954df ("mm: thp: support allocation of anonymous multi-size THP"), it may need to batch update tlb of an address range through the update_mmu_tlb function. We can simplify this operation by adding the update_mmu_tlb_range function, which may also reduce the execution of some unnecessary code in some architectures.

---
  include/linux/pgtable.h | 5 +++++
  mm/memory.c             | 4 +---
  2 files changed, 6 insertions(+), 3 deletions(-)
include/linux/pgtable.h +2 -0
index 18019f037bae..73411dfebf7a 100644
@@ -734,6 +734,11 @@ static inline void update_mmu_tlb(struct vm_area_struct *vma,
                                 unsigned long address, pte_t *ptep)
  {
  }
+
+static inline void update_mmu_tlb_range(struct vm_area_struct *vma,
Quoted 8 lines

include/linux/pgtable.h static inline void update_mmu_tlb(struct vm_area_struct *vma, · line 743

Quoted 5 lines
include/linux/pgtable.h +3 -0
+                               unsigned long address, pte_t *ptep, unsigned int nr)
+{
+}
  #define __HAVE_ARCH_UPDATE_MMU_TLB
  #endif

IMO, it might be better to use a separate definition to determine whether update_mmu_tlb_range() is overridden by a specific architecture.

I have also considered this, and I will modify it in the next version. thank you again for your review!

Thanks,
Bang

Quoted 30 lines

Thanks,
Lance

Quoted 26 lines
mm/memory.c +1 -4
index 6647685fd3c4..1f0ca362b82a 100644
@@ -4396,7 +4396,6 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
         vm_fault_t ret = 0;
         int nr_pages = 1;
         pte_t entry;
-       int i;
 
         /* File mapping without ->vm_ops ? */
         if (vma->vm_flags & VM_SHARED)
@@ -4465,8 +4464,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
                 update_mmu_tlb(vma, addr, vmf->pte);
                 goto release;
         } else if (nr_pages > 1 && !pte_range_none(vmf->pte, nr_pages)) {
-               for (i = 0; i < nr_pages; i++)
-                       update_mmu_tlb(vma, addr + PAGE_SIZE * i, vmf->pte + i);
+               update_mmu_tlb_range(vma, addr, vmf->pte, nr_pages);
                 goto release;
         }
 
--

2.19.1.6.gb485710b