* [PATCH RESEND 1/2] riscv: mm: Apply svinval in update_mmu_cache()
@ 2025-09-01 11:41 ` Xu Lu
0 siblings, 0 replies; 16+ messages in thread
From: Xu Lu @ 2025-09-01 11:41 UTC (permalink / raw)
To: paul.walmsley, palmer, aou, alex
Cc: linux-riscv, linux-kernel, apw, joe, 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
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH RESEND 1/2] riscv: mm: Apply svinval in update_mmu_cache()
2025-09-01 11:41 ` Xu Lu
@ 2026-07-11 0:04 ` Paul Walmsley
-1 siblings, 0 replies; 16+ messages in thread
From: Paul Walmsley @ 2026-07-11 0:04 UTC (permalink / raw)
To: Xu Lu; +Cc: paul.walmsley, palmer, aou, alex, linux-riscv, linux-kernel, apw,
joe
Hi,
On Mon, 1 Sep 2025, Xu Lu wrote:
> Only flush tlb of the specified mm, and apply svinval if available.
>
> Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
I wound up splitting this into two separate patches, rather than one
patch, since there seem to be two orthogonal changes. The first change
restricts the sfence.vma to a particular ASID (below). The second change
involves the Svinval path, and is sent in a subsequent message.
What do you think?
- Paul
From: Xu Lu <luxu.kernel@bytedance.com>
Date: Mon, 1 Sep 2025 19:41:40 +0800
Subject: [PATCH 1/2] riscv: mm: flush TLB by ASID in update_mmu_cache_range()
Only flush the TLB of the specified mm (via local_flush_tlb_page_asid)
Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
Link: https://patch.msgid.link/20250901114141.5438-2-luxu.kernel@bytedance.com
[pjw@kernel.org: split the non-Svinval code in the original into this patch; update description]
Signed-off-by: Paul Walmsley <pjw@kernel.org>
---
arch/riscv/include/asm/pgtable.h | 7 +++++--
arch/riscv/include/asm/tlbflush.h | 5 +++++
arch/riscv/mm/tlbflush.c | 5 -----
3 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
index 5d5756bda82e..755495a542cc 100644
--- a/arch/riscv/include/asm/pgtable.h
+++ b/arch/riscv/include/asm/pgtable.h
@@ -568,6 +568,8 @@ 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)
{
+ unsigned long asid;
+
/*
* Svvptc guarantees that the new valid pte will be visible within
* a bounded timeframe, so when the uarch does not cache invalid
@@ -583,10 +585,11 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf,
* Relying on flush_tlb_fix_spurious_fault would suffice, but
* the extra traps reduce performance. So, eagerly SFENCE.VMA.
*/
+ asid = get_mm_asid(vma->vm_mm);
while (nr--)
- local_flush_tlb_page(address + nr * PAGE_SIZE);
-
+ local_flush_tlb_page_asid(address + nr * PAGE_SIZE, asid);
}
+
#define update_mmu_cache(vma, addr, ptep) \
update_mmu_cache_range(NULL, vma, addr, ptep, 1)
diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h
index eed0abc40514..7c2cd5cc92d3 100644
--- a/arch/riscv/include/asm/tlbflush.h
+++ b/arch/riscv/include/asm/tlbflush.h
@@ -15,6 +15,11 @@
#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_flush_tlb_all(void)
{
__asm__ __volatile__ ("sfence.vma" : : : "memory");
diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c
index 8404530ec00f..73c226f719c7 100644
--- a/arch/riscv/mm/tlbflush.c
+++ b/arch/riscv/mm/tlbflush.c
@@ -110,11 +110,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.53.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH RESEND 1/2] riscv: mm: Apply svinval in update_mmu_cache()
@ 2026-07-11 0:04 ` Paul Walmsley
0 siblings, 0 replies; 16+ messages in thread
From: Paul Walmsley @ 2026-07-11 0:04 UTC (permalink / raw)
To: Xu Lu; +Cc: paul.walmsley, palmer, aou, alex, linux-riscv, linux-kernel, apw,
joe
Hi,
On Mon, 1 Sep 2025, Xu Lu wrote:
> Only flush tlb of the specified mm, and apply svinval if available.
>
> Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
I wound up splitting this into two separate patches, rather than one
patch, since there seem to be two orthogonal changes. The first change
restricts the sfence.vma to a particular ASID (below). The second change
involves the Svinval path, and is sent in a subsequent message.
What do you think?
- Paul
From: Xu Lu <luxu.kernel@bytedance.com>
Date: Mon, 1 Sep 2025 19:41:40 +0800
Subject: [PATCH 1/2] riscv: mm: flush TLB by ASID in update_mmu_cache_range()
Only flush the TLB of the specified mm (via local_flush_tlb_page_asid)
Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
Link: https://patch.msgid.link/20250901114141.5438-2-luxu.kernel@bytedance.com
[pjw@kernel.org: split the non-Svinval code in the original into this patch; update description]
Signed-off-by: Paul Walmsley <pjw@kernel.org>
---
arch/riscv/include/asm/pgtable.h | 7 +++++--
arch/riscv/include/asm/tlbflush.h | 5 +++++
arch/riscv/mm/tlbflush.c | 5 -----
3 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
index 5d5756bda82e..755495a542cc 100644
--- a/arch/riscv/include/asm/pgtable.h
+++ b/arch/riscv/include/asm/pgtable.h
@@ -568,6 +568,8 @@ 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)
{
+ unsigned long asid;
+
/*
* Svvptc guarantees that the new valid pte will be visible within
* a bounded timeframe, so when the uarch does not cache invalid
@@ -583,10 +585,11 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf,
* Relying on flush_tlb_fix_spurious_fault would suffice, but
* the extra traps reduce performance. So, eagerly SFENCE.VMA.
*/
+ asid = get_mm_asid(vma->vm_mm);
while (nr--)
- local_flush_tlb_page(address + nr * PAGE_SIZE);
-
+ local_flush_tlb_page_asid(address + nr * PAGE_SIZE, asid);
}
+
#define update_mmu_cache(vma, addr, ptep) \
update_mmu_cache_range(NULL, vma, addr, ptep, 1)
diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h
index eed0abc40514..7c2cd5cc92d3 100644
--- a/arch/riscv/include/asm/tlbflush.h
+++ b/arch/riscv/include/asm/tlbflush.h
@@ -15,6 +15,11 @@
#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_flush_tlb_all(void)
{
__asm__ __volatile__ ("sfence.vma" : : : "memory");
diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c
index 8404530ec00f..73c226f719c7 100644
--- a/arch/riscv/mm/tlbflush.c
+++ b/arch/riscv/mm/tlbflush.c
@@ -110,11 +110,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.53.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [External] Re: [PATCH RESEND 1/2] riscv: mm: Apply svinval in update_mmu_cache()
2026-07-11 0:04 ` Paul Walmsley
@ 2026-07-13 6:29 ` Xu Lu
-1 siblings, 0 replies; 16+ messages in thread
From: Xu Lu @ 2026-07-13 6:29 UTC (permalink / raw)
To: Paul Walmsley
Cc: paul.walmsley, palmer, aou, alex, linux-riscv, linux-kernel, apw,
joe
Hi Paul,
Yes, it makes more sense. Go ahead please.
Best regards.
Xu Lu
On Sat, Jul 11, 2026 at 8:04 AM Paul Walmsley <pjw@kernel.org> wrote:
>
> Hi,
>
> On Mon, 1 Sep 2025, Xu Lu wrote:
>
> > Only flush tlb of the specified mm, and apply svinval if available.
> >
> > Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
>
> I wound up splitting this into two separate patches, rather than one
> patch, since there seem to be two orthogonal changes. The first change
> restricts the sfence.vma to a particular ASID (below). The second change
> involves the Svinval path, and is sent in a subsequent message.
> What do you think?
>
>
> - Paul
>
>
> From: Xu Lu <luxu.kernel@bytedance.com>
> Date: Mon, 1 Sep 2025 19:41:40 +0800
> Subject: [PATCH 1/2] riscv: mm: flush TLB by ASID in update_mmu_cache_range()
>
> Only flush the TLB of the specified mm (via local_flush_tlb_page_asid)
>
> Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
> Link: https://patch.msgid.link/20250901114141.5438-2-luxu.kernel@bytedance.com
> [pjw@kernel.org: split the non-Svinval code in the original into this patch; update description]
> Signed-off-by: Paul Walmsley <pjw@kernel.org>
> ---
> arch/riscv/include/asm/pgtable.h | 7 +++++--
> arch/riscv/include/asm/tlbflush.h | 5 +++++
> arch/riscv/mm/tlbflush.c | 5 -----
> 3 files changed, 10 insertions(+), 7 deletions(-)
>
> diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
> index 5d5756bda82e..755495a542cc 100644
> --- a/arch/riscv/include/asm/pgtable.h
> +++ b/arch/riscv/include/asm/pgtable.h
> @@ -568,6 +568,8 @@ 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)
> {
> + unsigned long asid;
> +
> /*
> * Svvptc guarantees that the new valid pte will be visible within
> * a bounded timeframe, so when the uarch does not cache invalid
> @@ -583,10 +585,11 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf,
> * Relying on flush_tlb_fix_spurious_fault would suffice, but
> * the extra traps reduce performance. So, eagerly SFENCE.VMA.
> */
> + asid = get_mm_asid(vma->vm_mm);
> while (nr--)
> - local_flush_tlb_page(address + nr * PAGE_SIZE);
> -
> + local_flush_tlb_page_asid(address + nr * PAGE_SIZE, asid);
> }
> +
> #define update_mmu_cache(vma, addr, ptep) \
> update_mmu_cache_range(NULL, vma, addr, ptep, 1)
>
> diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h
> index eed0abc40514..7c2cd5cc92d3 100644
> --- a/arch/riscv/include/asm/tlbflush.h
> +++ b/arch/riscv/include/asm/tlbflush.h
> @@ -15,6 +15,11 @@
> #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_flush_tlb_all(void)
> {
> __asm__ __volatile__ ("sfence.vma" : : : "memory");
> diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c
> index 8404530ec00f..73c226f719c7 100644
> --- a/arch/riscv/mm/tlbflush.c
> +++ b/arch/riscv/mm/tlbflush.c
> @@ -110,11 +110,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.53.0
>
>
>
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [External] Re: [PATCH RESEND 1/2] riscv: mm: Apply svinval in update_mmu_cache()
@ 2026-07-13 6:29 ` Xu Lu
0 siblings, 0 replies; 16+ messages in thread
From: Xu Lu @ 2026-07-13 6:29 UTC (permalink / raw)
To: Paul Walmsley
Cc: paul.walmsley, palmer, aou, alex, linux-riscv, linux-kernel, apw,
joe
Hi Paul,
Yes, it makes more sense. Go ahead please.
Best regards.
Xu Lu
On Sat, Jul 11, 2026 at 8:04 AM Paul Walmsley <pjw@kernel.org> wrote:
>
> Hi,
>
> On Mon, 1 Sep 2025, Xu Lu wrote:
>
> > Only flush tlb of the specified mm, and apply svinval if available.
> >
> > Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
>
> I wound up splitting this into two separate patches, rather than one
> patch, since there seem to be two orthogonal changes. The first change
> restricts the sfence.vma to a particular ASID (below). The second change
> involves the Svinval path, and is sent in a subsequent message.
> What do you think?
>
>
> - Paul
>
>
> From: Xu Lu <luxu.kernel@bytedance.com>
> Date: Mon, 1 Sep 2025 19:41:40 +0800
> Subject: [PATCH 1/2] riscv: mm: flush TLB by ASID in update_mmu_cache_range()
>
> Only flush the TLB of the specified mm (via local_flush_tlb_page_asid)
>
> Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
> Link: https://patch.msgid.link/20250901114141.5438-2-luxu.kernel@bytedance.com
> [pjw@kernel.org: split the non-Svinval code in the original into this patch; update description]
> Signed-off-by: Paul Walmsley <pjw@kernel.org>
> ---
> arch/riscv/include/asm/pgtable.h | 7 +++++--
> arch/riscv/include/asm/tlbflush.h | 5 +++++
> arch/riscv/mm/tlbflush.c | 5 -----
> 3 files changed, 10 insertions(+), 7 deletions(-)
>
> diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
> index 5d5756bda82e..755495a542cc 100644
> --- a/arch/riscv/include/asm/pgtable.h
> +++ b/arch/riscv/include/asm/pgtable.h
> @@ -568,6 +568,8 @@ 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)
> {
> + unsigned long asid;
> +
> /*
> * Svvptc guarantees that the new valid pte will be visible within
> * a bounded timeframe, so when the uarch does not cache invalid
> @@ -583,10 +585,11 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf,
> * Relying on flush_tlb_fix_spurious_fault would suffice, but
> * the extra traps reduce performance. So, eagerly SFENCE.VMA.
> */
> + asid = get_mm_asid(vma->vm_mm);
> while (nr--)
> - local_flush_tlb_page(address + nr * PAGE_SIZE);
> -
> + local_flush_tlb_page_asid(address + nr * PAGE_SIZE, asid);
> }
> +
> #define update_mmu_cache(vma, addr, ptep) \
> update_mmu_cache_range(NULL, vma, addr, ptep, 1)
>
> diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h
> index eed0abc40514..7c2cd5cc92d3 100644
> --- a/arch/riscv/include/asm/tlbflush.h
> +++ b/arch/riscv/include/asm/tlbflush.h
> @@ -15,6 +15,11 @@
> #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_flush_tlb_all(void)
> {
> __asm__ __volatile__ ("sfence.vma" : : : "memory");
> diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c
> index 8404530ec00f..73c226f719c7 100644
> --- a/arch/riscv/mm/tlbflush.c
> +++ b/arch/riscv/mm/tlbflush.c
> @@ -110,11 +110,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.53.0
>
>
>
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [External] Re: [PATCH RESEND 1/2] riscv: mm: Apply svinval in update_mmu_cache()
2026-07-13 6:29 ` Xu Lu
@ 2026-07-13 17:49 ` Paul Walmsley
-1 siblings, 0 replies; 16+ messages in thread
From: Paul Walmsley @ 2026-07-13 17:49 UTC (permalink / raw)
To: Xu Lu
Cc: Paul Walmsley, paul.walmsley, palmer, aou, alex, linux-riscv,
linux-kernel, apw, joe
On Mon, 13 Jul 2026, Xu Lu wrote:
> Yes, it makes more sense. Go ahead please.
Great, thanks, I queued the two split patches for v7.3.
- Paul
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [External] Re: [PATCH RESEND 1/2] riscv: mm: Apply svinval in update_mmu_cache()
@ 2026-07-13 17:49 ` Paul Walmsley
0 siblings, 0 replies; 16+ messages in thread
From: Paul Walmsley @ 2026-07-13 17:49 UTC (permalink / raw)
To: Xu Lu
Cc: Paul Walmsley, paul.walmsley, palmer, aou, alex, linux-riscv,
linux-kernel, apw, joe
On Mon, 13 Jul 2026, Xu Lu wrote:
> Yes, it makes more sense. Go ahead please.
Great, thanks, I queued the two split patches for v7.3.
- Paul
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH RESEND 1/2] riscv: mm: Apply svinval in update_mmu_cache()
2025-09-01 11:41 ` Xu Lu
@ 2026-07-11 0:07 ` Paul Walmsley
-1 siblings, 0 replies; 16+ messages in thread
From: Paul Walmsley @ 2026-07-11 0:07 UTC (permalink / raw)
To: Xu Lu; +Cc: paul.walmsley, palmer, aou, alex, linux-riscv, linux-kernel, apw,
joe
On Mon, 1 Sep 2025, Xu Lu wrote:
> 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(-)
And here's the second patch, separated out from your original, which adds
the Svinval path. Thoughts?
- Paul
From: Xu Lu <luxu.kernel@bytedance.com>
Date: Mon, 1 Sep 2025 19:41:40 +0800
Subject: [PATCH] riscv: mm: Use Svinval if present in update_mmu_cache_range()
If the Svinval extension is present, use it in
update_mmu_cache_range(), rather than using the standard sfence.vma.
Svinval should be faster.
Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
Link: https://patch.msgid.link/20250901114141.5438-2-luxu.kernel@bytedance.com
[pjw@kernel.org: separate the non-Svinval path into an earlier patch; update to apply and improve the description]
Signed-off-by: Paul Walmsley <pjw@kernel.org>
---
arch/riscv/include/asm/pgtable.h | 16 ++++++++++++++++
arch/riscv/include/asm/tlbflush.h | 18 ++++++++++++++++++
arch/riscv/mm/tlbflush.c | 18 ------------------
3 files changed, 34 insertions(+), 18 deletions(-)
diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
index 755495a542cc..899d38614bb3 100644
--- a/arch/riscv/include/asm/pgtable.h
+++ b/arch/riscv/include/asm/pgtable.h
@@ -562,6 +562,17 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
#define pgd_ERROR(e) \
pr_err("%s:%d: bad pgd " PTE_FMT ".\n", __FILE__, __LINE__, pgd_val(e))
+static inline void __update_mmu_cache_range_svinval(struct vm_area_struct *vma,
+ unsigned long address, unsigned int nr)
+{
+ int i;
+ unsigned long asid = get_mm_asid(vma->vm_mm);
+
+ local_sfence_w_inval();
+ for (i = 0; i < nr; i++)
+ local_sinval_vma(address + nr * PAGE_SIZE, asid);
+ local_sfence_inval_ir();
+}
/* Commit new configuration to MMU hardware */
static inline void update_mmu_cache_range(struct vm_fault *vmf,
@@ -578,6 +589,11 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf,
if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SVVPTC))
return;
+ if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SVINVAL)) {
+ __update_mmu_cache_range_svinval(vma, address, nr);
+ return;
+ }
+
/*
* The kernel assumes that TLBs don't cache invalid entries, but
* in RISC-V, SFENCE.VMA specifies an ordering constraint, not a
diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h
index 7c2cd5cc92d3..9636d07fe9ee 100644
--- a/arch/riscv/include/asm/tlbflush.h
+++ b/arch/riscv/include/asm/tlbflush.h
@@ -20,6 +20,24 @@ 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 73c226f719c7..962db300a166 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.
--
2.53.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH RESEND 1/2] riscv: mm: Apply svinval in update_mmu_cache()
@ 2026-07-11 0:07 ` Paul Walmsley
0 siblings, 0 replies; 16+ messages in thread
From: Paul Walmsley @ 2026-07-11 0:07 UTC (permalink / raw)
To: Xu Lu; +Cc: paul.walmsley, palmer, aou, alex, linux-riscv, linux-kernel, apw,
joe
On Mon, 1 Sep 2025, Xu Lu wrote:
> 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(-)
And here's the second patch, separated out from your original, which adds
the Svinval path. Thoughts?
- Paul
From: Xu Lu <luxu.kernel@bytedance.com>
Date: Mon, 1 Sep 2025 19:41:40 +0800
Subject: [PATCH] riscv: mm: Use Svinval if present in update_mmu_cache_range()
If the Svinval extension is present, use it in
update_mmu_cache_range(), rather than using the standard sfence.vma.
Svinval should be faster.
Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
Link: https://patch.msgid.link/20250901114141.5438-2-luxu.kernel@bytedance.com
[pjw@kernel.org: separate the non-Svinval path into an earlier patch; update to apply and improve the description]
Signed-off-by: Paul Walmsley <pjw@kernel.org>
---
arch/riscv/include/asm/pgtable.h | 16 ++++++++++++++++
arch/riscv/include/asm/tlbflush.h | 18 ++++++++++++++++++
arch/riscv/mm/tlbflush.c | 18 ------------------
3 files changed, 34 insertions(+), 18 deletions(-)
diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
index 755495a542cc..899d38614bb3 100644
--- a/arch/riscv/include/asm/pgtable.h
+++ b/arch/riscv/include/asm/pgtable.h
@@ -562,6 +562,17 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
#define pgd_ERROR(e) \
pr_err("%s:%d: bad pgd " PTE_FMT ".\n", __FILE__, __LINE__, pgd_val(e))
+static inline void __update_mmu_cache_range_svinval(struct vm_area_struct *vma,
+ unsigned long address, unsigned int nr)
+{
+ int i;
+ unsigned long asid = get_mm_asid(vma->vm_mm);
+
+ local_sfence_w_inval();
+ for (i = 0; i < nr; i++)
+ local_sinval_vma(address + nr * PAGE_SIZE, asid);
+ local_sfence_inval_ir();
+}
/* Commit new configuration to MMU hardware */
static inline void update_mmu_cache_range(struct vm_fault *vmf,
@@ -578,6 +589,11 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf,
if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SVVPTC))
return;
+ if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SVINVAL)) {
+ __update_mmu_cache_range_svinval(vma, address, nr);
+ return;
+ }
+
/*
* The kernel assumes that TLBs don't cache invalid entries, but
* in RISC-V, SFENCE.VMA specifies an ordering constraint, not a
diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h
index 7c2cd5cc92d3..9636d07fe9ee 100644
--- a/arch/riscv/include/asm/tlbflush.h
+++ b/arch/riscv/include/asm/tlbflush.h
@@ -20,6 +20,24 @@ 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 73c226f719c7..962db300a166 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.
--
2.53.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH RESEND 1/2] riscv: mm: Apply svinval in update_mmu_cache()
2025-09-01 11:41 ` Xu Lu
@ 2026-07-15 1:14 ` Klara Modin
-1 siblings, 0 replies; 16+ messages in thread
From: Klara Modin @ 2026-07-15 1:14 UTC (permalink / raw)
To: Xu Lu; +Cc: paul.walmsley, palmer, aou, alex, linux-riscv, linux-kernel, apw,
joe
Hi,
On 2025-09-01 19:41:40 +0800, Xu Lu wrote:
> 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);
Probably should be
+ for (i = 0; i < nr; i++)
+ local_sinval_vma(address + i * PAGE_SIZE, asid);
instead. My BPI-f3 hangs when entering the initramfs with the original
patch. Reverting also fixes it.
> + local_sfence_inval_ir();
> + return;
>
> svvptc:;
> /*
Regards,
Klara Modin
...
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [PATCH RESEND 1/2] riscv: mm: Apply svinval in update_mmu_cache()
@ 2026-07-15 1:14 ` Klara Modin
0 siblings, 0 replies; 16+ messages in thread
From: Klara Modin @ 2026-07-15 1:14 UTC (permalink / raw)
To: Xu Lu; +Cc: paul.walmsley, palmer, aou, alex, linux-riscv, linux-kernel, apw,
joe
Hi,
On 2025-09-01 19:41:40 +0800, Xu Lu wrote:
> 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);
Probably should be
+ for (i = 0; i < nr; i++)
+ local_sinval_vma(address + i * PAGE_SIZE, asid);
instead. My BPI-f3 hangs when entering the initramfs with the original
patch. Reverting also fixes it.
> + local_sfence_inval_ir();
> + return;
>
> svvptc:;
> /*
Regards,
Klara Modin
...
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 16+ messages in thread