* [PATCH 0/2] riscv: mm: Some optimizations for tlb flush
@ 2025-08-27 13:14 Xu Lu
2025-08-27 13:14 ` [PATCH 1/2] riscv: mm: Apply svinval in update_mmu_cache() Xu Lu
2025-08-27 13:14 ` [PATCH 2/2] riscv: mm: Clear cpu in mm_cpumask after local_flush_tlb_all_asid Xu Lu
0 siblings, 2 replies; 3+ messages in thread
From: Xu Lu @ 2025-08-27 13:14 UTC (permalink / raw)
To: paul.walmsley, palmer, aou, alex; +Cc: linux-riscv, linux-kernel, Xu Lu
Some optimizations for tlb flush on RISC-V smp:
1. Apply Svinval in update_mmu_cache().
2. Clear bit of current cpu in mm_cpumask after
local_flush_tlb_all_asid().
Xu Lu (2):
riscv: mm: Apply svinval in update_mmu_cache()
riscv: mm: Clear cpu in mm_cpumask after local_flush_tlb_all_asid
arch/riscv/include/asm/pgtable.h | 16 +++++++-
arch/riscv/include/asm/tlbflush.h | 23 +++++++++++
arch/riscv/mm/tlbflush.c | 64 ++++++++++++-------------------
3 files changed, 63 insertions(+), 40 deletions(-)
--
2.20.1
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 1/2] riscv: mm: Apply svinval in update_mmu_cache()
2025-08-27 13:14 [PATCH 0/2] riscv: mm: Some optimizations for tlb flush Xu Lu
@ 2025-08-27 13:14 ` Xu Lu
2025-08-27 13:14 ` [PATCH 2/2] riscv: mm: Clear cpu in mm_cpumask after local_flush_tlb_all_asid Xu Lu
1 sibling, 0 replies; 3+ messages in thread
From: Xu Lu @ 2025-08-27 13:14 UTC (permalink / raw)
To: paul.walmsley, palmer, aou, alex; +Cc: linux-riscv, linux-kernel, Xu Lu
Only flush tlb of the specified mm, and apply svinval if available.
Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
---
arch/riscv/include/asm/pgtable.h | 16 +++++++++++++++-
arch/riscv/include/asm/tlbflush.h | 23 +++++++++++++++++++++++
arch/riscv/mm/tlbflush.c | 23 -----------------------
3 files changed, 38 insertions(+), 24 deletions(-)
diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
index 91697fbf1f901..165cd02d51629 100644
--- a/arch/riscv/include/asm/pgtable.h
+++ b/arch/riscv/include/asm/pgtable.h
@@ -495,9 +495,15 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf,
struct vm_area_struct *vma, unsigned long address,
pte_t *ptep, unsigned int nr)
{
+ int i;
+ unsigned long asid = get_mm_asid(vma->vm_mm);
+
asm goto(ALTERNATIVE("nop", "j %l[svvptc]", 0, RISCV_ISA_EXT_SVVPTC, 1)
: : : : svvptc);
+ asm goto(ALTERNATIVE("nop", "j %l[svinval]", 0, RISCV_ISA_EXT_SVINVAL, 1)
+ : : : : svinval);
+
/*
* The kernel assumes that TLBs don't cache invalid entries, but
* in RISC-V, SFENCE.VMA specifies an ordering constraint, not a
@@ -506,7 +512,15 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf,
* the extra traps reduce performance. So, eagerly SFENCE.VMA.
*/
while (nr--)
- local_flush_tlb_page(address + nr * PAGE_SIZE);
+ local_flush_tlb_page_asid(address + nr * PAGE_SIZE, asid);
+ return;
+
+svinval:
+ local_sfence_w_inval();
+ for (i = 0; i < nr; i++)
+ local_sinval_vma(address + nr * PAGE_SIZE, asid);
+ local_sfence_inval_ir();
+ return;
svvptc:;
/*
diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h
index eed0abc405143..9636d07fe9eed 100644
--- a/arch/riscv/include/asm/tlbflush.h
+++ b/arch/riscv/include/asm/tlbflush.h
@@ -15,6 +15,29 @@
#define FLUSH_TLB_NO_ASID ((unsigned long)-1)
#ifdef CONFIG_MMU
+static inline unsigned long get_mm_asid(struct mm_struct *mm)
+{
+ return mm ? cntx2asid(atomic_long_read(&mm->context.id)) : FLUSH_TLB_NO_ASID;
+}
+
+static inline void local_sfence_inval_ir(void)
+{
+ asm volatile(SFENCE_INVAL_IR() ::: "memory");
+}
+
+static inline void local_sfence_w_inval(void)
+{
+ asm volatile(SFENCE_W_INVAL() ::: "memory");
+}
+
+static inline void local_sinval_vma(unsigned long vma, unsigned long asid)
+{
+ if (asid != FLUSH_TLB_NO_ASID)
+ asm volatile(SINVAL_VMA(%0, %1) : : "r" (vma), "r" (asid) : "memory");
+ else
+ asm volatile(SINVAL_VMA(%0, zero) : : "r" (vma) : "memory");
+}
+
static inline void local_flush_tlb_all(void)
{
__asm__ __volatile__ ("sfence.vma" : : : "memory");
diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c
index 8404530ec00f9..962db300a1665 100644
--- a/arch/riscv/mm/tlbflush.c
+++ b/arch/riscv/mm/tlbflush.c
@@ -11,24 +11,6 @@
#define has_svinval() riscv_has_extension_unlikely(RISCV_ISA_EXT_SVINVAL)
-static inline void local_sfence_inval_ir(void)
-{
- asm volatile(SFENCE_INVAL_IR() ::: "memory");
-}
-
-static inline void local_sfence_w_inval(void)
-{
- asm volatile(SFENCE_W_INVAL() ::: "memory");
-}
-
-static inline void local_sinval_vma(unsigned long vma, unsigned long asid)
-{
- if (asid != FLUSH_TLB_NO_ASID)
- asm volatile(SINVAL_VMA(%0, %1) : : "r" (vma), "r" (asid) : "memory");
- else
- asm volatile(SINVAL_VMA(%0, zero) : : "r" (vma) : "memory");
-}
-
/*
* Flush entire TLB if number of entries to be flushed is greater
* than the threshold below.
@@ -110,11 +92,6 @@ static void __ipi_flush_tlb_range_asid(void *info)
local_flush_tlb_range_asid(d->start, d->size, d->stride, d->asid);
}
-static inline unsigned long get_mm_asid(struct mm_struct *mm)
-{
- return mm ? cntx2asid(atomic_long_read(&mm->context.id)) : FLUSH_TLB_NO_ASID;
-}
-
static void __flush_tlb_range(struct mm_struct *mm,
const struct cpumask *cmask,
unsigned long start, unsigned long size,
--
2.20.1
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] riscv: mm: Clear cpu in mm_cpumask after local_flush_tlb_all_asid
2025-08-27 13:14 [PATCH 0/2] riscv: mm: Some optimizations for tlb flush Xu Lu
2025-08-27 13:14 ` [PATCH 1/2] riscv: mm: Apply svinval in update_mmu_cache() Xu Lu
@ 2025-08-27 13:14 ` Xu Lu
1 sibling, 0 replies; 3+ messages in thread
From: Xu Lu @ 2025-08-27 13:14 UTC (permalink / raw)
To: paul.walmsley, palmer, aou, alex; +Cc: linux-riscv, linux-kernel, Xu Lu
Clear corresponding bit of current cpu in mm_cpumask after executing
local_flush_tlb_all_asid().
This reduces the number of IPI due to tlb flush:
* ltp - mmapstress01
Before: ~98k
After: 268
Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
---
arch/riscv/mm/tlbflush.c | 41 ++++++++++++++++++++++++----------------
1 file changed, 25 insertions(+), 16 deletions(-)
diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c
index 962db300a1665..571358f385879 100644
--- a/arch/riscv/mm/tlbflush.c
+++ b/arch/riscv/mm/tlbflush.c
@@ -17,7 +17,8 @@
*/
unsigned long tlb_flush_all_threshold __read_mostly = 64;
-static void local_flush_tlb_range_threshold_asid(unsigned long start,
+static void local_flush_tlb_range_threshold_asid(struct mm_struct *mm,
+ unsigned long start,
unsigned long size,
unsigned long stride,
unsigned long asid)
@@ -27,6 +28,8 @@ static void local_flush_tlb_range_threshold_asid(unsigned long start,
if (nr_ptes_in_range > tlb_flush_all_threshold) {
local_flush_tlb_all_asid(asid);
+ if (mm && mm != current->active_mm)
+ cpumask_clear_cpu(raw_smp_processor_id(), mm_cpumask(mm));
return;
}
@@ -46,21 +49,28 @@ static void local_flush_tlb_range_threshold_asid(unsigned long start,
}
}
-static inline void local_flush_tlb_range_asid(unsigned long start,
- unsigned long size, unsigned long stride, unsigned long asid)
+static inline void local_flush_tlb_range_mm(struct mm_struct *mm,
+ unsigned long start,
+ unsigned long size,
+ unsigned long stride)
{
- if (size <= stride)
+ unsigned long asid = get_mm_asid(mm);
+
+ if (size <= stride) {
local_flush_tlb_page_asid(start, asid);
- else if (size == FLUSH_TLB_MAX_SIZE)
+ } else if (size == FLUSH_TLB_MAX_SIZE) {
local_flush_tlb_all_asid(asid);
- else
- local_flush_tlb_range_threshold_asid(start, size, stride, asid);
+ if (mm && mm != current->active_mm)
+ cpumask_clear_cpu(raw_smp_processor_id(), mm_cpumask(mm));
+ } else {
+ local_flush_tlb_range_threshold_asid(mm, start, size, stride, asid);
+ }
}
/* Flush a range of kernel pages without broadcasting */
void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
{
- local_flush_tlb_range_asid(start, end - start, PAGE_SIZE, FLUSH_TLB_NO_ASID);
+ local_flush_tlb_range_mm(NULL, start, end - start, PAGE_SIZE);
}
static void __ipi_flush_tlb_all(void *info)
@@ -79,17 +89,17 @@ void flush_tlb_all(void)
}
struct flush_tlb_range_data {
- unsigned long asid;
+ struct mm_struct *mm;
unsigned long start;
unsigned long size;
unsigned long stride;
};
-static void __ipi_flush_tlb_range_asid(void *info)
+static void __ipi_flush_tlb_range_mm(void *info)
{
struct flush_tlb_range_data *d = info;
- local_flush_tlb_range_asid(d->start, d->size, d->stride, d->asid);
+ local_flush_tlb_range_mm(d->mm, d->start, d->size, d->stride);
}
static void __flush_tlb_range(struct mm_struct *mm,
@@ -97,7 +107,6 @@ static void __flush_tlb_range(struct mm_struct *mm,
unsigned long start, unsigned long size,
unsigned long stride)
{
- unsigned long asid = get_mm_asid(mm);
unsigned int cpu;
if (cpumask_empty(cmask))
@@ -107,17 +116,17 @@ static void __flush_tlb_range(struct mm_struct *mm,
/* Check if the TLB flush needs to be sent to other CPUs. */
if (cpumask_any_but(cmask, cpu) >= nr_cpu_ids) {
- local_flush_tlb_range_asid(start, size, stride, asid);
+ local_flush_tlb_range_mm(mm, start, size, stride);
} else if (riscv_use_sbi_for_rfence()) {
- sbi_remote_sfence_vma_asid(cmask, start, size, asid);
+ sbi_remote_sfence_vma_asid(cmask, start, size, get_mm_asid(mm));
} else {
struct flush_tlb_range_data ftd;
- ftd.asid = asid;
+ ftd.mm = mm;
ftd.start = start;
ftd.size = size;
ftd.stride = stride;
- on_each_cpu_mask(cmask, __ipi_flush_tlb_range_asid, &ftd, 1);
+ on_each_cpu_mask(cmask, __ipi_flush_tlb_range_mm, &ftd, 1);
}
put_cpu();
--
2.20.1
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-08-27 15:54 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-27 13:14 [PATCH 0/2] riscv: mm: Some optimizations for tlb flush Xu Lu
2025-08-27 13:14 ` [PATCH 1/2] riscv: mm: Apply svinval in update_mmu_cache() Xu Lu
2025-08-27 13:14 ` [PATCH 2/2] riscv: mm: Clear cpu in mm_cpumask after local_flush_tlb_all_asid Xu Lu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).