riscv: mm: Trace TLB flush path selection
PATCH v2 1/1 riscv: mm: Trace TLB flush path selection
Make RISC-V TLB flush path selection observable. Record whether Linux handles an invalidation locally, delegates it to SBI RFENCE, or executes it through a cross-CPU call, so MM activity can be correlated with the RISC-V, firmware, or Linux cross-CPU path carrying the request.
The generic tlb:tlb_flush event describes TLB flush activity using architecture-independent reason and page-count information. The RISC-V implementation subsequently selects between local invalidation, SBI RFENCE, and Linux cross-CPU coordination, with additional architecture-specific request context available at that point.
Making this selection observable is useful when debugging RISC-V TLB shootdowns. When a remote invalidation is observed to be slow, the selected path determines whether to investigate SBI firmware and platform handling or Linux cross-CPU and IPI handling. An unexpectedly broad target mask can reveal an unintended address-space CPU footprint, while the range and stride distinguish invalidation requests with different mapping granularities.
Place the event in the RISC-V implementation because the local, SBI RFENCE, or cross-CPU choice is made there, and SBI RFENCE and the invalidation stride are RISC-V-specific semantics rather than properties of the generic MM flush request.
Add riscv_tlb:riscv_tlb_flush_path in flush_tlb_all() and __flush_tlb_range(). Record start, size, stride, the hardware-visible ASID, whether a specific mm is associated with the request, the target CPU mask and its weight, the requested scope, and the selected path.
Record the complete target mask in addition to its weight because CPU identity cannot be reconstructed from a count and is needed to correlate the request with per-CPU scheduler, IPI, and firmware activity.
The event records the invalidation request and the path selected by Linux before the operation is dispatched. In particular, selecting the SBI RFENCE path means that Linux delegated the request to firmware; the event does not describe the implementation or outcome of that delegated operation.
Tested on QEMU virt with OpenSBI using local and shared-mm mprotect()/munmap() workloads. Local requests reported path=local, while remote requests reported path=sbi-rfence and were followed by the existing riscv:sbi_call RFENCE event.
The cross-CPU-call path was tested with QEMU virt using APLIC+IMSIC. A MADV_PAGEOUT reclaim workload was used to exercise mm-independent global flushes. All reported path values (local, sbi-rfence and cross-cpu-call) and scope values (single, range, address-space and all) were observed.
---
Add a RISC-V tracepoint for observing the path selected by Linux for TLB invalidation requests: local invalidation, SBI RFENCE, or Linux cross-CPU coordination.
The tracepoint is intended to make RISC-V TLB shootdown behavior easier to correlate with MM activity, CPU targeting, SBI calls, and IPI handling. The patch records the invalidation request context and the Linux path-selection decision before the operation is dispatched.
The patch was tested on QEMU virt with both the SBI RFENCE path and an APLIC+IMSIC configuration. Local, SBI RFENCE, and cross-CPU-call paths
were exercised. All reported scope values -- single, range,
address-space, and all -- were also observed.
---
Changes in v2:
- Use trace_call__riscv_tlb_flush_path() after the explicit trace_riscv_tlb_flush_path_enabled() check to avoid a second
tracepoint static-key test, as suggested by Steven Rostedt.
- Link to v1: https://lore.kernel.org/r/[email protected]
--- arch/riscv/mm/tlbflush.c | 60 +++++++++++++++++++-- include/trace/events/riscv_tlb.h | 113 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+), 4 deletions(-)
arch/riscv/mm/tlbflush.c +56 -4
@@ -9,6 +9,9 @@#include <asm/mmu_context.h> #include <asm/cpufeature.h> +#define CREATE_TRACE_POINTS +#include <trace/events/riscv_tlb.h> +#define has_svinval () riscv_has_extension_unlikely(RISCV_ISA_EXT_SVINVAL)/*
@@ -63,6 +66,33 @@ 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 ); } +static enum riscv_tlb_flush_scope +riscv_tlb_get_flush_scope (unsigned long size,unsigned long stride,bool has_mm) +{ +if (size ==FLUSH_TLB_MAX_SIZE ) +return has_mm ?RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE : +RISCV_TLB_FLUSH_SCOPE_ALL ; + +return size <= stride ?RISCV_TLB_FLUSH_SCOPE_SINGLE : +RISCV_TLB_FLUSH_SCOPE_RANGE ; +} + +static __always_inline void +riscv_tlb_trace_flush_path (const struct cpumask *cmask,unsigned long start, +unsigned long size,unsigned long stride, +unsigned long asid,bool has_mm, +enum riscv_tlb_flush_path path) +{ +enum riscv_tlb_flush_scope scope; + +if (!trace_riscv_tlb_flush_path_enabled ()) +return ; + + scope =riscv_tlb_get_flush_scope (size, stride, has_mm); +trace_call__riscv_tlb_flush_path (start, size, stride, asid, has_mm, + cmask, scope, path); +} +static void __ipi_flush_tlb_all (void *info) {local_flush_tlb_all ();
@@ -70,12 +100,26 @@ static void __ipi_flush_tlb_all(void *info)void flush_tlb_all (void ) { -if (num_online_cpus () <2 ) +if (num_online_cpus () <2 ) { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_LOCAL );local_flush_tlb_all (); -else if (riscv_use_sbi_for_rfence ()) -sbi_remote_sfence_vma_asid (NULL ,0 ,FLUSH_TLB_MAX_SIZE ,FLUSH_TLB_NO_ASID ); -else + }else if (riscv_use_sbi_for_rfence ()) { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_SBI_RFENCE ); +sbi_remote_sfence_vma_asid (NULL ,0 ,FLUSH_TLB_MAX_SIZE , +FLUSH_TLB_NO_ASID ); + }else { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL );on_each_cpu (__ipi_flush_tlb_all,NULL ,1 ); + } }struct flush_tlb_range_data {
@@ -107,12 +151,20 @@ 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) { +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm,RISCV_TLB_FLUSH_PATH_LOCAL );local_flush_tlb_range_asid (start, size, stride, asid); }else if (riscv_use_sbi_for_rfence ()) { +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm,RISCV_TLB_FLUSH_PATH_SBI_RFENCE );sbi_remote_sfence_vma_asid (cmask, start, size, asid); }else {struct flush_tlb_range_data ftd; +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm, +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL ); + ftd.asid = asid; ftd.start = start; ftd.size = size;
include/trace/events/riscv_tlb.h new file +113 -0
113 lines not on this page · raw message
---
base-commit: 77ae27fd98f3b548797c9f22c10ab5cf1c4ada53
change-id: 20260829-tlb_tracepoint-844105ab5092
Best regards,
Signature · 1 line
Roman 'Hedin' Storozhenko <[email protected]>
Hi,
A gentle ping on this patch. v2 incorporates Steven's suggestion to use trace_call__riscv_tlb_flush_path() after the explicit enabled check.
The patch has passed the RISC-V Patchwork CI, and the local, SBI RFENCE, and cross-CPU-call paths have been exercised in QEMU.
Any further comments would be appreciated.
Thanks,
Roman
Quoted 305 lines · Roman 'Hedin' Storozhenko
Make RISC-V TLB flush path selection observable. Record whether Linux handles an invalidation locally, delegates it to SBI RFENCE, or executes it through a cross-CPU call, so MM activity can be correlated with the RISC-V, firmware, or Linux cross-CPU path carrying the request.
The generic tlb:tlb_flush event describes TLB flush activity using architecture-independent reason and page-count information. The RISC-V implementation subsequently selects between local invalidation, SBI RFENCE, and Linux cross-CPU coordination, with additional architecture-specific request context available at that point.
Making this selection observable is useful when debugging RISC-V TLB shootdowns. When a remote invalidation is observed to be slow, the selected path determines whether to investigate SBI firmware and platform handling or Linux cross-CPU and IPI handling. An unexpectedly broad target mask can reveal an unintended address-space CPU footprint, while the range and stride distinguish invalidation requests with different mapping granularities.
Place the event in the RISC-V implementation because the local, SBI RFENCE, or cross-CPU choice is made there, and SBI RFENCE and the invalidation stride are RISC-V-specific semantics rather than properties of the generic MM flush request.
Add riscv_tlb:riscv_tlb_flush_path in
flush_tlb_all()and__flush_tlb_range(). Record start, size, stride, the hardware-visible ASID, whether a specific mm is associated with the request, the target CPU mask and its weight, the requested scope, and the selected path.Record the complete target mask in addition to its weight because CPU identity cannot be reconstructed from a count and is needed to correlate the request with per-CPU scheduler, IPI, and firmware activity.
The event records the invalidation request and the path selected by Linux before the operation is dispatched. In particular, selecting the SBI RFENCE path means that Linux delegated the request to firmware; the event does not describe the implementation or outcome of that delegated operation.
Tested on QEMU virt with OpenSBI using local and shared-mm mprotect()/munmap() workloads. Local requests reported path=local, while remote requests reported path=sbi-rfence and were followed by the existing riscv:sbi_call RFENCE event.
The cross-CPU-call path was tested with QEMU virt using APLIC+IMSIC. A
MADV_PAGEOUTreclaim workload was used to exercise mm-independent global flushes. All reported path values (local, sbi-rfence and cross-cpu-call) and scope values (single, range, address-space and all) were observed.Signed-off-by Roman 'Hedin' Storozhenko <[email protected]>---
Add a RISC-V tracepoint for observing the path selected by Linux for TLB invalidation requests: local invalidation, SBI RFENCE, or Linux cross-CPU coordination.The tracepoint is intended to make RISC-V TLB shootdown behavior easier to correlate with MM activity, CPU targeting, SBI calls, and IPI handling. The patch records the invalidation request context and the Linux path-selection decision before the operation is dispatched.
The patch was tested on QEMU virt with both the SBI RFENCE path and an APLIC+IMSIC configuration. Local, SBI RFENCE, and cross-CPU-call paths
were exercised. All reported scope values -- single, range,
address-space, and all -- were also observed.
---
Changes in v2:
- Usetrace_call__riscv_tlb_flush_path()after the explicittrace_riscv_tlb_flush_path_enabled()check to avoid a second
tracepoint static-key test, as suggested by Steven Rostedt.
- Link to v1: https://lore.kernel.org/r/[email protected]--- arch/riscv/mm/tlbflush.c | 60 +++++++++++++++++++-- include/trace/events/riscv_tlb.h | 113 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+), 4 deletions(-)arch/riscv/mm/tlbflush.c +56
-4@@ -9,6 +9,9 @@#include <asm/mmu_context.h> #include <asm/cpufeature.h> +#define CREATE_TRACE_POINTS +#include <trace/events/riscv_tlb.h> +#define has_svinval () riscv_has_extension_unlikely(RISCV_ISA_EXT_SVINVAL)/* @@ -63,6 +66,33 @@ 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 ); } +static enum riscv_tlb_flush_scope +riscv_tlb_get_flush_scope (unsigned long size,unsigned long stride,bool has_mm) +{ +if (size ==FLUSH_TLB_MAX_SIZE ) +return has_mm ?RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE : +RISCV_TLB_FLUSH_SCOPE_ALL ; + +return size <= stride ?RISCV_TLB_FLUSH_SCOPE_SINGLE : +RISCV_TLB_FLUSH_SCOPE_RANGE ; +} + +static __always_inline void +riscv_tlb_trace_flush_path (const struct cpumask *cmask,unsigned long start, +unsigned long size,unsigned long stride, +unsigned long asid,bool has_mm, +enum riscv_tlb_flush_path path) +{ +enum riscv_tlb_flush_scope scope; + +if (!trace_riscv_tlb_flush_path_enabled ()) +return ; + + scope =riscv_tlb_get_flush_scope (size, stride, has_mm); +trace_call__riscv_tlb_flush_path (start, size, stride, asid, has_mm, + cmask, scope, path); +} +static void __ipi_flush_tlb_all (void *info) {local_flush_tlb_all ();@@ -70,12 +100,26 @@ static void __ipi_flush_tlb_all(void *info)void flush_tlb_all (void ) { -if (num_online_cpus () <2 ) +if (num_online_cpus () <2 ) { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_LOCAL );local_flush_tlb_all (); -else if (riscv_use_sbi_for_rfence ()) -sbi_remote_sfence_vma_asid (NULL ,0 ,FLUSH_TLB_MAX_SIZE ,FLUSH_TLB_NO_ASID ); -else + }else if (riscv_use_sbi_for_rfence ()) { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_SBI_RFENCE ); +sbi_remote_sfence_vma_asid (NULL ,0 ,FLUSH_TLB_MAX_SIZE , +FLUSH_TLB_NO_ASID ); + }else { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL );on_each_cpu (__ipi_flush_tlb_all,NULL ,1 ); + } }struct flush_tlb_range_data {@@ -107,12 +151,20 @@ 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) { +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm,RISCV_TLB_FLUSH_PATH_LOCAL );local_flush_tlb_range_asid (start, size, stride, asid); }else if (riscv_use_sbi_for_rfence ()) { +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm,RISCV_TLB_FLUSH_PATH_SBI_RFENCE );sbi_remote_sfence_vma_asid (cmask, start, size, asid); }else {struct flush_tlb_range_data ftd; +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm, +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL ); + ftd.asid = asid; ftd.start = start; ftd.size = size;include/trace/events/riscv_tlb.h new file +113
-1@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM riscv_tlb + +#if !defined(_TRACE_RISCV_TLB_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_RISCV_TLB_H + +#include <linux/cpumask.h> +#include <linux/tracepoint.h> + +#ifndef _TRACE_RISCV_TLB_ENUMS +#define _TRACE_RISCV_TLB_ENUMS + +enum riscv_tlb_flush_scope { +RISCV_TLB_FLUSH_SCOPE_SINGLE , +RISCV_TLB_FLUSH_SCOPE_RANGE , +RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE , +RISCV_TLB_FLUSH_SCOPE_ALL , +}; + +enum riscv_tlb_flush_path { +RISCV_TLB_FLUSH_PATH_LOCAL , +RISCV_TLB_FLUSH_PATH_SBI_RFENCE , +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL , +}; + +#endif /* _TRACE_RISCV_TLB_ENUMS */ + +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_SINGLE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_RANGE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_ALL ); + +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_LOCAL ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_SBI_RFENCE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL ); + +#define show_riscv_tlb_flush_scope (scope) \ + __print_symbolic(scope, \ + { RISCV_TLB_FLUSH_SCOPE_SINGLE, "single" }, \ + { RISCV_TLB_FLUSH_SCOPE_RANGE, "range" }, \ + { RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE, "address-space" }, \ + { RISCV_TLB_FLUSH_SCOPE_ALL, "all" }) + +#define show_riscv_tlb_flush_path (path) \ + __print_symbolic(path, \ + { RISCV_TLB_FLUSH_PATH_LOCAL, "local" }, \ + { RISCV_TLB_FLUSH_PATH_SBI_RFENCE, "sbi-rfence" }, \ + { RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL, "cross-cpu-call" }) + +/* +* Record the invalidation request received by the RISC-V architecture code +* and the path selected by Linux. +* +* The target CPU mask represents the CPUs Linux intends to cover for the +* request. It can be correlated with per-CPU activity, but does not describe +* which harts ultimately performed an invalidation. +* +* The ASID is hardware-visible and may be reused. It must not be treated as a +* persistent identifier for an mm. +* +* The stride describes the invalidation granularity supplied to the RISC-V +* implementation. SBI RFENCE receives start, size and ASID, but not stride. +* +* The event is emitted at path selection time. For SBI RFENCE, it records +* delegation of the request to firmware; firmware processing after that +* point is outside the event's scope. +*/ +TRACE_EVENT (riscv_tlb_flush_path, + TP_PROTO(unsigned long start, unsigned long size, + unsigned long stride, unsigned long asid, bool has_mm, + const struct cpumask *cmask, + enum riscv_tlb_flush_scope scope, + enum riscv_tlb_flush_path path), + +TP_ARGS (start, size, stride, asid, has_mm, cmask, scope, path), + +TP_STRUCT__entry ( + __field(unsigned long, start) + __field(unsigned long, size) + __field(unsigned long, stride) + __field(unsigned long, asid) + __field(bool, has_mm) + __field(unsigned int, target_mask_weight) + __cpumask(target_cpus) + __field(u8, scope) +__field (u8, path) + ), + +TP_fast_assign ( + __entry->start = start; + __entry->size = size; + __entry->stride = stride; + __entry->asid = asid; + __entry->has_mm = has_mm; + __entry->target_mask_weight = cpumask_weight(cmask); + __assign_cpumask(target_cpus, cpumask_bits(cmask)); + __entry->scope = scope; + __entry->path = path; + ), + +TP_printk ("start=%#lx size=%#lx stride=%#lx asid=%#lx has_mm=%d target_mask_weight=%u target_cpus=%s scope=%s path=%s" , + __entry->start, __entry->size, __entry->stride, + __entry->asid, __entry->has_mm, + __entry->target_mask_weight,__get_cpumask (target_cpus), +show_riscv_tlb_flush_scope (__entry->scope), +show_riscv_tlb_flush_path (__entry->path)) +); + +#endif /* _TRACE_RISCV_TLB_H */ + +/* This part must be outside protection. */ +#include <trace/define_trace.h> ---base-commit:
77ae27fd98f3b548797c9f22c10ab5cf1c4ada53
change-id: 20260829-tlb_tracepoint-844105ab5092Best regards,
--
Roman 'Hedin' Storozhenko <[email protected]>
Signature · 2 lines
Kind regards, Roman 'Hedin' Storozhenko
Quoted 4 lines · Roman 'Hedin' Storozhenko
Make RISC-V TLB flush path selection observable. Record whether Linux handles an invalidation locally, delegates it to SBI RFENCE, or executes it through a cross-CPU call, so MM activity can be correlated with the RISC-V, firmware, or Linux cross-CPU path carrying the request.
I didn't see too much usefullness of the trace here. why not use kprobe instead? can you plz show a real usage example of the trace? Which bug/performance problem can be solved conveniently with this trace only?
From another side, except x86, other architectures don't have the trace in the TLB path.
Quoted 308 lines
The generic tlb:tlb_flush event describes TLB flush activity using architecture-independent reason and page-count information. The RISC-V implementation subsequently selects between local invalidation, SBI RFENCE, and Linux cross-CPU coordination, with additional architecture-specific request context available at that point.
Making this selection observable is useful when debugging RISC-V TLB shootdowns. When a remote invalidation is observed to be slow, the selected path determines whether to investigate SBI firmware and platform handling or Linux cross-CPU and IPI handling. An unexpectedly broad target mask can reveal an unintended address-space CPU footprint, while the range and stride distinguish invalidation requests with
different mapping granularities.
Place the event in the RISC-V implementation because the local, SBI RFENCE, or cross-CPU choice is made there, and SBI RFENCE and the invalidation stride are RISC-V-specific semantics rather than properties of the generic MM flush request.
Add riscv_tlb:riscv_tlb_flush_path in
flush_tlb_all()and__flush_tlb_range(). Record start, size, stride, the hardware-visible ASID, whether a specific mm is associated with the request, the target CPU mask and its weight, the requested scope, and the selected path.Record the complete target mask in addition to its weight because CPU identity cannot be reconstructed from a count and is needed to correlate the request with per-CPU scheduler, IPI, and firmware activity.
The event records the invalidation request and the path selected by Linux before the operation is dispatched. In particular, selecting the SBI RFENCE path means that Linux delegated the request to firmware; the event does not describe the implementation or outcome of that delegated operation.
Tested on QEMU virt with OpenSBI using local and shared-mm mprotect()/munmap() workloads. Local requests reported path=local, while remote requests reported path=sbi-rfence and were followed by the existing riscv:sbi_call RFENCE event.
The cross-CPU-call path was tested with QEMU virt using APLIC+IMSIC. A
MADV_PAGEOUTreclaim workload was used to exercise mm-independent global flushes. All reported path values (local, sbi-rfence and cross-cpu-call) and scope values (single, range, address-space and all) were observed.Signed-off-by Roman 'Hedin' Storozhenko <[email protected]>---
Add a RISC-V tracepoint for observing the path selected by Linux for TLB invalidation requests: local invalidation, SBI RFENCE, or Linux cross-CPU coordination.The tracepoint is intended to make RISC-V TLB shootdown behavior easier to correlate with MM activity, CPU targeting, SBI calls, and IPI handling. The patch records the invalidation request context and the Linux path-selection decision before the operation is dispatched.
The patch was tested on QEMU virt with both the SBI RFENCE path and an APLIC+IMSIC configuration. Local, SBI RFENCE, and cross-CPU-call paths
were exercised. All reported scope values -- single, range,
address-space, and all -- were also observed.
---
Changes in v2:
- Usetrace_call__riscv_tlb_flush_path()after the explicittrace_riscv_tlb_flush_path_enabled()check to avoid a second
tracepoint static-key test, as suggested by Steven Rostedt.
- Link to v1: https://lore.kernel.org/r/[email protected]--- arch/riscv/mm/tlbflush.c | 60 +++++++++++++++++++-- include/trace/events/riscv_tlb.h | 113 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+), 4 deletions(-)arch/riscv/mm/tlbflush.c +56
-4@@ -9,6 +9,9 @@#include <asm/mmu_context.h> #include <asm/cpufeature.h> +#define CREATE_TRACE_POINTS +#include <trace/events/riscv_tlb.h> +#define has_svinval () riscv_has_extension_unlikely(RISCV_ISA_EXT_SVINVAL)/* @@ -63,6 +66,33 @@ 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 ); } +static enum riscv_tlb_flush_scope +riscv_tlb_get_flush_scope (unsigned long size,unsigned long stride,bool has_mm) +{ +if (size ==FLUSH_TLB_MAX_SIZE ) +return has_mm ?RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE : +RISCV_TLB_FLUSH_SCOPE_ALL ; + +return size <= stride ?RISCV_TLB_FLUSH_SCOPE_SINGLE : +RISCV_TLB_FLUSH_SCOPE_RANGE ; +} + +static __always_inline void +riscv_tlb_trace_flush_path (const struct cpumask *cmask,unsigned long start, +unsigned long size,unsigned long stride, +unsigned long asid,bool has_mm, +enum riscv_tlb_flush_path path) +{ +enum riscv_tlb_flush_scope scope; + +if (!trace_riscv_tlb_flush_path_enabled ()) +return ; + + scope =riscv_tlb_get_flush_scope (size, stride, has_mm); +trace_call__riscv_tlb_flush_path (start, size, stride, asid, has_mm, + cmask, scope, path); +} +static void __ipi_flush_tlb_all (void *info) {local_flush_tlb_all ();@@ -70,12 +100,26 @@ static void __ipi_flush_tlb_all(void *info)void flush_tlb_all (void ) { -if (num_online_cpus () <2 ) +if (num_online_cpus () <2 ) { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_LOCAL );local_flush_tlb_all (); -else if (riscv_use_sbi_for_rfence ()) -sbi_remote_sfence_vma_asid (NULL ,0 ,FLUSH_TLB_MAX_SIZE ,FLUSH_TLB_NO_ASID ); -else + }else if (riscv_use_sbi_for_rfence ()) { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_SBI_RFENCE ); +sbi_remote_sfence_vma_asid (NULL ,0 ,FLUSH_TLB_MAX_SIZE , +FLUSH_TLB_NO_ASID ); + }else { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL );on_each_cpu (__ipi_flush_tlb_all,NULL ,1 ); + } }struct flush_tlb_range_data {@@ -107,12 +151,20 @@ 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) { +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm,RISCV_TLB_FLUSH_PATH_LOCAL );local_flush_tlb_range_asid (start, size, stride, asid); }else if (riscv_use_sbi_for_rfence ()) { +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm,RISCV_TLB_FLUSH_PATH_SBI_RFENCE );sbi_remote_sfence_vma_asid (cmask, start, size, asid); }else {struct flush_tlb_range_data ftd; +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm, +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL ); + ftd.asid = asid; ftd.start = start; ftd.size = size;include/trace/events/riscv_tlb.h new file +113
-1@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM riscv_tlb + +#if !defined(_TRACE_RISCV_TLB_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_RISCV_TLB_H + +#include <linux/cpumask.h> +#include <linux/tracepoint.h> + +#ifndef _TRACE_RISCV_TLB_ENUMS +#define _TRACE_RISCV_TLB_ENUMS + +enum riscv_tlb_flush_scope { +RISCV_TLB_FLUSH_SCOPE_SINGLE , +RISCV_TLB_FLUSH_SCOPE_RANGE , +RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE , +RISCV_TLB_FLUSH_SCOPE_ALL , +}; + +enum riscv_tlb_flush_path { +RISCV_TLB_FLUSH_PATH_LOCAL , +RISCV_TLB_FLUSH_PATH_SBI_RFENCE , +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL , +}; + +#endif /* _TRACE_RISCV_TLB_ENUMS */ + +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_SINGLE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_RANGE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_ALL ); + +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_LOCAL ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_SBI_RFENCE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL ); + +#define show_riscv_tlb_flush_scope (scope) \ + __print_symbolic(scope, \ + { RISCV_TLB_FLUSH_SCOPE_SINGLE, "single" }, \ + { RISCV_TLB_FLUSH_SCOPE_RANGE, "range" }, \ + { RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE, "address-space" }, \ + { RISCV_TLB_FLUSH_SCOPE_ALL, "all" }) + +#define show_riscv_tlb_flush_path (path) \ + __print_symbolic(path, \ + { RISCV_TLB_FLUSH_PATH_LOCAL, "local" }, \ + { RISCV_TLB_FLUSH_PATH_SBI_RFENCE, "sbi-rfence" }, \ + { RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL, "cross-cpu-call" }) + +/* +* Record the invalidation request received by the RISC-V architecture code +* and the path selected by Linux. +* +* The target CPU mask represents the CPUs Linux intends to cover for the +* request. It can be correlated with per-CPU activity, but does not describe +* which harts ultimately performed an invalidation. +* +* The ASID is hardware-visible and may be reused. It must not be treated as a +* persistent identifier for an mm. +* +* The stride describes the invalidation granularity supplied to the RISC-V +* implementation. SBI RFENCE receives start, size and ASID, but not stride. +* +* The event is emitted at path selection time. For SBI RFENCE, it records +* delegation of the request to firmware; firmware processing after that +* point is outside the event's scope. +*/ +TRACE_EVENT (riscv_tlb_flush_path, + TP_PROTO(unsigned long start, unsigned long size, + unsigned long stride, unsigned long asid, bool has_mm, + const struct cpumask *cmask, + enum riscv_tlb_flush_scope scope, + enum riscv_tlb_flush_path path), + +TP_ARGS (start, size, stride, asid, has_mm, cmask, scope, path), + +TP_STRUCT__entry ( + __field(unsigned long, start) + __field(unsigned long, size) + __field(unsigned long, stride) + __field(unsigned long, asid) + __field(bool, has_mm) + __field(unsigned int, target_mask_weight) + __cpumask(target_cpus) + __field(u8, scope) +__field (u8, path) + ), + +TP_fast_assign ( + __entry->start = start; + __entry->size = size; + __entry->stride = stride; + __entry->asid = asid; + __entry->has_mm = has_mm; + __entry->target_mask_weight = cpumask_weight(cmask); + __assign_cpumask(target_cpus, cpumask_bits(cmask)); + __entry->scope = scope; + __entry->path = path; + ), + +TP_printk ("start=%#lx size=%#lx stride=%#lx asid=%#lx has_mm=%d target_mask_weight=%u target_cpus=%s scope=%s path=%s" , + __entry->start, __entry->size, __entry->stride, + __entry->asid, __entry->has_mm, + __entry->target_mask_weight,__get_cpumask (target_cpus), +show_riscv_tlb_flush_scope (__entry->scope), +show_riscv_tlb_flush_path (__entry->path)) +); + +#endif /* _TRACE_RISCV_TLB_H */ + +/* This part must be outside protection. */ +#include <trace/define_trace.h> ---base-commit:
77ae27fd98f3b548797c9f22c10ab5cf1c4ada53
change-id: 20260829-tlb_tracepoint-844105ab5092Best regards,
Signature · 7 lines
Roman 'Hedin' Storozhenko <[email protected]> _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 7 lines · Jisheng Zhang
Quoted 4 lines · Roman 'Hedin' Storozhenko
Make RISC-V TLB flush path selection observable. Record whether Linux handles an invalidation locally, delegates it to SBI RFENCE, or executes it through a cross-CPU call, so MM activity can be correlated with the RISC-V, firmware, or Linux cross-CPU path carrying the request.
Quoted 7 lines
I didn't see too much usefullness of the trace here. why not use kprobe instead? can you plz show a real usage example of the trace? Which bug/performance problem can be solved conveniently with this trace only?
From another side, except x86, other architectures don't have the trace in the TLB path.
Hi Jisheng,
Thanks for the review. I agree that the current commit message describes the intended use rather abstractly.
A kprobe can certainly be used to investigate this. My motivation for a tracepoint is not that dynamic tracing cannot obtain the information, but that doing so requires knowledge of the current implementation and reconstruction of a semantic decision made inside __flush_tlb_range() / flush_tlb_all().
For example, probing __flush_tlb_range() gives the request context, but not directly the path subsequently selected. To determine whether the request was handled locally, delegated through SBI RFENCE, or executed through Linux cross-CPU coordination, a tracing tool needs to probe/correlate implementation-specific call sites, or reproduce the selection logic itself.
The intent of the proposed event is to expose that semantic decision directly, together with the target CPU mask and request context, rather than make users depend on the current helper names and call graph.
One concrete RISC-V example is commit ae9e9f3d67dc:
"RISC-V: clear hot-unplugged cores from all task mm_cpumasks
to avoid rfence errors"
In that case an offline CPU could remain in mm_cpumask(), which was then used as the target of an SBI RFENCE request. OpenSBI checks the requested hart mask against online harts, so such a request could fail.
With this event, such a situation could expose, for example:
target_cpus=0,3 target_mask_weight=2 scope=range path=sbi-rfence
while CPU3 is already offline.
That shows that Linux constructed an unexpected target mask before the request was handed to firmware, so the investigation can first focus on Linux's mm CPU tracking rather than starting inside the SBI implementation.
There is also a performance-debugging use case. Real-world latency investigations have found TLB shootdowns responsible for significant latency spikes; for example:
https://www.jabperf.com/how-to-deter-or-disarm-tlb-shootdowns/
That particular case eventually identified automatic NUMA balancing as the source of excessive shootdowns. I am not claiming that this proposed event alone would identify that root cause. On RISC-V, once TLB shootdowns have been identified as relevant, the event provides the architecture-specific next step: which CPUs Linux targeted, and whether Linux selected local handling, SBI RFENCE, or Linux cross-CPU coordination.
This distinction also helps separate where to investigate next. For an otherwise similar MM request:
path=sbi-rfence
points toward firmware/platform handling after Linux delegates the request, while:
path=cross-cpu-call
points toward Linux cross-CPU/IPI handling and target-CPU activity.
An unexpectedly broad target_cpus mask can similarly indicate that the cost comes from Linux targeting more CPUs than expected rather than from the execution time of the selected mechanism itself.
I also put the workloads used to exercise the event here:
https://github.com/Romeus/tlb_workloads
They provide reproducible examples using the same 64-page mprotect() request with different CPU footprints/configurations. I used them to exercise local, SBI RFENCE, and cross-CPU-call paths.
I am not claiming that a tracepoint by itself solves these bugs, or that the same information is impossible to obtain with kprobes. The intended value is to expose the RISC-V TLB path-selection decision and its target context in one semantic record, without requiring each tracing tool to reconstruct that decision from implementation-specific probes.
I also take your point that, apart from x86, architectures generally do not currently have permanent tracing in the TLB path. If the use case is considered sufficient for a tracepoint, I will rework the commit message to make the concrete motivation and the distinction from dynamic probing clearer.
For context, I also posted an unchanged v2 RESEND on Sep 24; this reply is on the original v2 thread where your review landed.
https://lore.kernel.org/lkml/[email protected]/
Thanks,
Roman
Quoted 308 lines
Quoted 308 lines
The generic tlb:tlb_flush event describes TLB flush activity using architecture-independent reason and page-count information. The RISC-V implementation subsequently selects between local invalidation, SBI RFENCE, and Linux cross-CPU coordination, with additional architecture-specific request context available at that point.
Making this selection observable is useful when debugging RISC-V TLB shootdowns. When a remote invalidation is observed to be slow, the selected path determines whether to investigate SBI firmware and platform handling or Linux cross-CPU and IPI handling. An unexpectedly broad target mask can reveal an unintended address-space CPU footprint, while the range and stride distinguish invalidation requests with
different mapping granularities.
Place the event in the RISC-V implementation because the local, SBI RFENCE, or cross-CPU choice is made there, and SBI RFENCE and the invalidation stride are RISC-V-specific semantics rather than properties of the generic MM flush request.
Add riscv_tlb:riscv_tlb_flush_path in
flush_tlb_all()and__flush_tlb_range(). Record start, size, stride, the hardware-visible ASID, whether a specific mm is associated with the request, the target CPU mask and its weight, the requested scope, and the selected path.Record the complete target mask in addition to its weight because CPU identity cannot be reconstructed from a count and is needed to correlate the request with per-CPU scheduler, IPI, and firmware activity.
The event records the invalidation request and the path selected by Linux before the operation is dispatched. In particular, selecting the SBI RFENCE path means that Linux delegated the request to firmware; the event does not describe the implementation or outcome of that delegated operation.
Tested on QEMU virt with OpenSBI using local and shared-mm mprotect()/munmap() workloads. Local requests reported path=local, while remote requests reported path=sbi-rfence and were followed by the existing riscv:sbi_call RFENCE event.
The cross-CPU-call path was tested with QEMU virt using APLIC+IMSIC. A
MADV_PAGEOUTreclaim workload was used to exercise mm-independent global flushes. All reported path values (local, sbi-rfence and cross-cpu-call) and scope values (single, range, address-space and all) were observed.Signed-off-by Roman 'Hedin' Storozhenko <[email protected]>---
Add a RISC-V tracepoint for observing the path selected by Linux for TLB invalidation requests: local invalidation, SBI RFENCE, or Linux cross-CPU coordination.The tracepoint is intended to make RISC-V TLB shootdown behavior easier to correlate with MM activity, CPU targeting, SBI calls, and IPI handling. The patch records the invalidation request context and the Linux path-selection decision before the operation is dispatched.
The patch was tested on QEMU virt with both the SBI RFENCE path and an APLIC+IMSIC configuration. Local, SBI RFENCE, and cross-CPU-call paths
were exercised. All reported scope values -- single, range,
address-space, and all -- were also observed.
---
Changes in v2:
- Usetrace_call__riscv_tlb_flush_path()after the explicittrace_riscv_tlb_flush_path_enabled()check to avoid a second
tracepoint static-key test, as suggested by Steven Rostedt.
- Link to v1: https://lore.kernel.org/r/[email protected]--- arch/riscv/mm/tlbflush.c | 60 +++++++++++++++++++-- include/trace/events/riscv_tlb.h | 113 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+), 4 deletions(-)arch/riscv/mm/tlbflush.c +56
-4@@ -9,6 +9,9 @@#include <asm/mmu_context.h> #include <asm/cpufeature.h> +#define CREATE_TRACE_POINTS +#include <trace/events/riscv_tlb.h> +#define has_svinval () riscv_has_extension_unlikely(RISCV_ISA_EXT_SVINVAL)/* @@ -63,6 +66,33 @@ 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 ); } +static enum riscv_tlb_flush_scope +riscv_tlb_get_flush_scope (unsigned long size,unsigned long stride,bool has_mm) +{ +if (size ==FLUSH_TLB_MAX_SIZE ) +return has_mm ?RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE : +RISCV_TLB_FLUSH_SCOPE_ALL ; + +return size <= stride ?RISCV_TLB_FLUSH_SCOPE_SINGLE : +RISCV_TLB_FLUSH_SCOPE_RANGE ; +} + +static __always_inline void +riscv_tlb_trace_flush_path (const struct cpumask *cmask,unsigned long start, +unsigned long size,unsigned long stride, +unsigned long asid,bool has_mm, +enum riscv_tlb_flush_path path) +{ +enum riscv_tlb_flush_scope scope; + +if (!trace_riscv_tlb_flush_path_enabled ()) +return ; + + scope =riscv_tlb_get_flush_scope (size, stride, has_mm); +trace_call__riscv_tlb_flush_path (start, size, stride, asid, has_mm, + cmask, scope, path); +} +static void __ipi_flush_tlb_all (void *info) {local_flush_tlb_all ();@@ -70,12 +100,26 @@ static void __ipi_flush_tlb_all(void *info)void flush_tlb_all (void ) { -if (num_online_cpus () <2 ) +if (num_online_cpus () <2 ) { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_LOCAL );local_flush_tlb_all (); -else if (riscv_use_sbi_for_rfence ()) -sbi_remote_sfence_vma_asid (NULL ,0 ,FLUSH_TLB_MAX_SIZE ,FLUSH_TLB_NO_ASID ); -else + }else if (riscv_use_sbi_for_rfence ()) { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_SBI_RFENCE ); +sbi_remote_sfence_vma_asid (NULL ,0 ,FLUSH_TLB_MAX_SIZE , +FLUSH_TLB_NO_ASID ); + }else { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL );on_each_cpu (__ipi_flush_tlb_all,NULL ,1 ); + } }struct flush_tlb_range_data {@@ -107,12 +151,20 @@ 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) { +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm,RISCV_TLB_FLUSH_PATH_LOCAL );local_flush_tlb_range_asid (start, size, stride, asid); }else if (riscv_use_sbi_for_rfence ()) { +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm,RISCV_TLB_FLUSH_PATH_SBI_RFENCE );sbi_remote_sfence_vma_asid (cmask, start, size, asid); }else {struct flush_tlb_range_data ftd; +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm, +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL ); + ftd.asid = asid; ftd.start = start; ftd.size = size;include/trace/events/riscv_tlb.h new file +113
-1@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM riscv_tlb + +#if !defined(_TRACE_RISCV_TLB_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_RISCV_TLB_H + +#include <linux/cpumask.h> +#include <linux/tracepoint.h> + +#ifndef _TRACE_RISCV_TLB_ENUMS +#define _TRACE_RISCV_TLB_ENUMS + +enum riscv_tlb_flush_scope { +RISCV_TLB_FLUSH_SCOPE_SINGLE , +RISCV_TLB_FLUSH_SCOPE_RANGE , +RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE , +RISCV_TLB_FLUSH_SCOPE_ALL , +}; + +enum riscv_tlb_flush_path { +RISCV_TLB_FLUSH_PATH_LOCAL , +RISCV_TLB_FLUSH_PATH_SBI_RFENCE , +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL , +}; + +#endif /* _TRACE_RISCV_TLB_ENUMS */ + +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_SINGLE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_RANGE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_ALL ); + +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_LOCAL ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_SBI_RFENCE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL ); + +#define show_riscv_tlb_flush_scope (scope) \ + __print_symbolic(scope, \ + { RISCV_TLB_FLUSH_SCOPE_SINGLE, "single" }, \ + { RISCV_TLB_FLUSH_SCOPE_RANGE, "range" }, \ + { RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE, "address-space" }, \ + { RISCV_TLB_FLUSH_SCOPE_ALL, "all" }) + +#define show_riscv_tlb_flush_path (path) \ + __print_symbolic(path, \ + { RISCV_TLB_FLUSH_PATH_LOCAL, "local" }, \ + { RISCV_TLB_FLUSH_PATH_SBI_RFENCE, "sbi-rfence" }, \ + { RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL, "cross-cpu-call" }) + +/* +* Record the invalidation request received by the RISC-V architecture code +* and the path selected by Linux. +* +* The target CPU mask represents the CPUs Linux intends to cover for the +* request. It can be correlated with per-CPU activity, but does not describe +* which harts ultimately performed an invalidation. +* +* The ASID is hardware-visible and may be reused. It must not be treated as a +* persistent identifier for an mm. +* +* The stride describes the invalidation granularity supplied to the RISC-V +* implementation. SBI RFENCE receives start, size and ASID, but not stride. +* +* The event is emitted at path selection time. For SBI RFENCE, it records +* delegation of the request to firmware; firmware processing after that +* point is outside the event's scope. +*/ +TRACE_EVENT (riscv_tlb_flush_path, + TP_PROTO(unsigned long start, unsigned long size, + unsigned long stride, unsigned long asid, bool has_mm, + const struct cpumask *cmask, + enum riscv_tlb_flush_scope scope, + enum riscv_tlb_flush_path path), + +TP_ARGS (start, size, stride, asid, has_mm, cmask, scope, path), + +TP_STRUCT__entry ( + __field(unsigned long, start) + __field(unsigned long, size) + __field(unsigned long, stride) + __field(unsigned long, asid) + __field(bool, has_mm) + __field(unsigned int, target_mask_weight) + __cpumask(target_cpus) + __field(u8, scope) +__field (u8, path) + ), + +TP_fast_assign ( + __entry->start = start; + __entry->size = size; + __entry->stride = stride; + __entry->asid = asid; + __entry->has_mm = has_mm; + __entry->target_mask_weight = cpumask_weight(cmask); + __assign_cpumask(target_cpus, cpumask_bits(cmask)); + __entry->scope = scope; + __entry->path = path; + ), + +TP_printk ("start=%#lx size=%#lx stride=%#lx asid=%#lx has_mm=%d target_mask_weight=%u target_cpus=%s scope=%s path=%s" , + __entry->start, __entry->size, __entry->stride, + __entry->asid, __entry->has_mm, + __entry->target_mask_weight,__get_cpumask (target_cpus), +show_riscv_tlb_flush_scope (__entry->scope), +show_riscv_tlb_flush_path (__entry->path)) +); + +#endif /* _TRACE_RISCV_TLB_H */ + +/* This part must be outside protection. */ +#include <trace/define_trace.h> ---base-commit:
77ae27fd98f3b548797c9f22c10ab5cf1c4ada53
change-id: 20260829-tlb_tracepoint-844105ab5092Best regards,
--
Roman 'Hedin' Storozhenko <[email protected]>_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Signature · 2 lines
Kind regards, Roman 'Hedin' Storozhenko
Quoted 18 lines · Roman Storozhenko
Quoted 7 lines · Jisheng Zhang
Quoted 4 lines · Roman 'Hedin' Storozhenko
Make RISC-V TLB flush path selection observable. Record whether Linux handles an invalidation locally, delegates it to SBI RFENCE, or executes it through a cross-CPU call, so MM activity can be correlated with the RISC-V, firmware, or Linux cross-CPU path carrying the request.
Quoted 7 lines
I didn't see too much usefullness of the trace here. why not use kprobe instead? can you plz show a real usage example of the trace? Which bug/performance problem can be solved conveniently with this trace only?
From another side, except x86, other architectures don't have the trace in the TLB path.
Hi Jisheng,
Quoted 8 lines
Thanks for the review. I agree that the current commit message describes the intended use rather abstractly.
A kprobe can certainly be used to investigate this. My motivation for a tracepoint is not that dynamic tracing cannot obtain the information, but that doing so requires knowledge of the current implementation and reconstruction of a semantic decision made inside
__flush_tlb_range()/flush_tlb_all().
I prefer aligning with other architectures(except x86), I.E not add this kind of trace. However, I would leave the decision to riscv maintainers.
FYI, you may notice that the riscv trace overhead is not trivial per
https://lore.kernel.org/linux-riscv/[email protected]/
Quoted 403 lines
For example, probing
__flush_tlb_range()gives the request context, but not directly the path subsequently selected. To determine whether the request was handled locally, delegated through SBI RFENCE, or executed through Linux cross-CPU coordination, a tracing tool needs to probe/correlate implementation-specific call sites, or reproduce the selection logic itself.The intent of the proposed event is to expose that semantic decision directly, together with the target CPU mask and request context, rather than make users depend on the current helper names and call graph.
One concrete RISC-V example is commit
ae9e9f3d67dc:"RISC-V: clear hot-unplugged cores from all task
mm_cpumasks
to avoid rfence errors"In that case an offline CPU could remain in
mm_cpumask(), which was then used as the target of an SBI RFENCE request. OpenSBI checks the requested hart mask against online harts, so such a request could fail.With this event, such a situation could expose, for example:
target_cpus=0,3 target_mask_weight=2 scope=range path=sbi-rfence
while CPU3 is already offline.
That shows that Linux constructed an unexpected target mask before the request was handed to firmware, so the investigation can first focus on Linux's mm CPU tracking rather than starting inside the SBI implementation.
There is also a performance-debugging use case. Real-world latency investigations have found TLB shootdowns responsible for significant latency spikes; for example:
https://www.jabperf.com/how-to-deter-or-disarm-tlb-shootdowns/
That particular case eventually identified automatic NUMA balancing as the source of excessive shootdowns. I am not claiming that this proposed event alone would identify that root cause. On RISC-V, once TLB shootdowns have been identified as relevant, the event provides the architecture-specific next step: which CPUs Linux targeted, and whether Linux selected local handling, SBI RFENCE, or Linux cross-CPU coordination.
This distinction also helps separate where to investigate next. For an otherwise similar MM request:
path=sbi-rfence
points toward
firmware/platformhandling after Linux delegates the request, while:path=cross-cpu-call
points toward Linux cross-CPU/IPI handling and target-CPU activity.
An unexpectedly broad
target_cpusmask can similarly indicate that the cost comes from Linux targeting more CPUs than expected rather than from the execution time of the selected mechanism itself.I also put the workloads used to exercise the event here:
https://github.com/Romeus/tlb_workloads
They provide reproducible examples using the same 64-page
mprotect()request with different CPU footprints/configurations. I used them to exercise local, SBI RFENCE, and cross-CPU-call paths.I am not claiming that a tracepoint by itself solves these bugs, or that the same information is impossible to obtain with kprobes. The intended value is to expose the RISC-V TLB path-selection decision and its target context in one semantic record, without requiring each tracing tool to reconstruct that decision from implementation-specific probes.
I also take your point that, apart from x86, architectures generally do not currently have permanent tracing in the TLB path. If the use case is considered sufficient for a tracepoint, I will rework the commit message to make the concrete motivation and the distinction from dynamic probing clearer.
For context, I also posted an unchanged v2 RESEND on Sep 24; this reply is on the original v2 thread where your review landed.
https://lore.kernel.org/lkml/[email protected]/Thanks,
RomanQuoted 308 lines
Quoted 308 lines
The generic tlb:tlb_flush event describes TLB flush activity using architecture-independent reason and page-count information. The RISC-V implementation subsequently selects between local invalidation, SBI RFENCE, and Linux cross-CPU coordination, with additional architecture-specific request context available at that point.
Making this selection observable is useful when debugging RISC-V TLB shootdowns. When a remote invalidation is observed to be slow, the selected path determines whether to investigate SBI firmware and platform handling or Linux cross-CPU and IPI handling. An unexpectedly broad target mask can reveal an unintended address-space CPU footprint, while the range and stride distinguish invalidation requests with
different mapping granularities.
Place the event in the RISC-V implementation because the local, SBI RFENCE, or cross-CPU choice is made there, and SBI RFENCE and the invalidation stride are RISC-V-specific semantics rather than properties of the generic MM flush request.
Add riscv_tlb:riscv_tlb_flush_path in
flush_tlb_all()and__flush_tlb_range(). Record start, size, stride, the hardware-visible ASID, whether a specific mm is associated with the request, the target CPU mask and its weight, the requested scope, and the selected path.Record the complete target mask in addition to its weight because CPU identity cannot be reconstructed from a count and is needed to correlate the request with per-CPU scheduler, IPI, and firmware activity.
The event records the invalidation request and the path selected by Linux before the operation is dispatched. In particular, selecting the SBI RFENCE path means that Linux delegated the request to firmware; the event does not describe the implementation or outcome of that delegated operation.
Tested on QEMU virt with OpenSBI using local and shared-mm mprotect()/munmap() workloads. Local requests reported path=local, while remote requests reported path=sbi-rfence and were followed by the existing riscv:sbi_call RFENCE event.
The cross-CPU-call path was tested with QEMU virt using APLIC+IMSIC. A
MADV_PAGEOUTreclaim workload was used to exercise mm-independent global flushes. All reported path values (local, sbi-rfence and cross-cpu-call) and scope values (single, range, address-space and all) were observed.Signed-off-by Roman 'Hedin' Storozhenko <[email protected]>---
Add a RISC-V tracepoint for observing the path selected by Linux for TLB invalidation requests: local invalidation, SBI RFENCE, or Linux cross-CPU coordination.The tracepoint is intended to make RISC-V TLB shootdown behavior easier to correlate with MM activity, CPU targeting, SBI calls, and IPI handling. The patch records the invalidation request context and the Linux path-selection decision before the operation is dispatched.
The patch was tested on QEMU virt with both the SBI RFENCE path and an APLIC+IMSIC configuration. Local, SBI RFENCE, and cross-CPU-call paths
were exercised. All reported scope values -- single, range,
address-space, and all -- were also observed.
---
Changes in v2:
- Usetrace_call__riscv_tlb_flush_path()after the explicittrace_riscv_tlb_flush_path_enabled()check to avoid a second
tracepoint static-key test, as suggested by Steven Rostedt.
- Link to v1: https://lore.kernel.org/r/[email protected]--- arch/riscv/mm/tlbflush.c | 60 +++++++++++++++++++-- include/trace/events/riscv_tlb.h | 113 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+), 4 deletions(-)arch/riscv/mm/tlbflush.c +56
-4@@ -9,6 +9,9 @@#include <asm/mmu_context.h> #include <asm/cpufeature.h> +#define CREATE_TRACE_POINTS +#include <trace/events/riscv_tlb.h> +#define has_svinval () riscv_has_extension_unlikely(RISCV_ISA_EXT_SVINVAL)/* @@ -63,6 +66,33 @@ 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 ); } +static enum riscv_tlb_flush_scope +riscv_tlb_get_flush_scope (unsigned long size,unsigned long stride,bool has_mm) +{ +if (size ==FLUSH_TLB_MAX_SIZE ) +return has_mm ?RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE : +RISCV_TLB_FLUSH_SCOPE_ALL ; + +return size <= stride ?RISCV_TLB_FLUSH_SCOPE_SINGLE : +RISCV_TLB_FLUSH_SCOPE_RANGE ; +} + +static __always_inline void +riscv_tlb_trace_flush_path (const struct cpumask *cmask,unsigned long start, +unsigned long size,unsigned long stride, +unsigned long asid,bool has_mm, +enum riscv_tlb_flush_path path) +{ +enum riscv_tlb_flush_scope scope; + +if (!trace_riscv_tlb_flush_path_enabled ()) +return ; + + scope =riscv_tlb_get_flush_scope (size, stride, has_mm); +trace_call__riscv_tlb_flush_path (start, size, stride, asid, has_mm, + cmask, scope, path); +} +static void __ipi_flush_tlb_all (void *info) {local_flush_tlb_all ();@@ -70,12 +100,26 @@ static void __ipi_flush_tlb_all(void *info)void flush_tlb_all (void ) { -if (num_online_cpus () <2 ) +if (num_online_cpus () <2 ) { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_LOCAL );local_flush_tlb_all (); -else if (riscv_use_sbi_for_rfence ()) -sbi_remote_sfence_vma_asid (NULL ,0 ,FLUSH_TLB_MAX_SIZE ,FLUSH_TLB_NO_ASID ); -else + }else if (riscv_use_sbi_for_rfence ()) { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_SBI_RFENCE ); +sbi_remote_sfence_vma_asid (NULL ,0 ,FLUSH_TLB_MAX_SIZE , +FLUSH_TLB_NO_ASID ); + }else { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL );on_each_cpu (__ipi_flush_tlb_all,NULL ,1 ); + } }struct flush_tlb_range_data {@@ -107,12 +151,20 @@ 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) { +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm,RISCV_TLB_FLUSH_PATH_LOCAL );local_flush_tlb_range_asid (start, size, stride, asid); }else if (riscv_use_sbi_for_rfence ()) { +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm,RISCV_TLB_FLUSH_PATH_SBI_RFENCE );sbi_remote_sfence_vma_asid (cmask, start, size, asid); }else {struct flush_tlb_range_data ftd; +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm, +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL ); + ftd.asid = asid; ftd.start = start; ftd.size = size;include/trace/events/riscv_tlb.h new file +113
-1@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM riscv_tlb + +#if !defined(_TRACE_RISCV_TLB_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_RISCV_TLB_H + +#include <linux/cpumask.h> +#include <linux/tracepoint.h> + +#ifndef _TRACE_RISCV_TLB_ENUMS +#define _TRACE_RISCV_TLB_ENUMS + +enum riscv_tlb_flush_scope { +RISCV_TLB_FLUSH_SCOPE_SINGLE , +RISCV_TLB_FLUSH_SCOPE_RANGE , +RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE , +RISCV_TLB_FLUSH_SCOPE_ALL , +}; + +enum riscv_tlb_flush_path { +RISCV_TLB_FLUSH_PATH_LOCAL , +RISCV_TLB_FLUSH_PATH_SBI_RFENCE , +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL , +}; + +#endif /* _TRACE_RISCV_TLB_ENUMS */ + +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_SINGLE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_RANGE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_ALL ); + +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_LOCAL ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_SBI_RFENCE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL ); + +#define show_riscv_tlb_flush_scope (scope) \ + __print_symbolic(scope, \ + { RISCV_TLB_FLUSH_SCOPE_SINGLE, "single" }, \ + { RISCV_TLB_FLUSH_SCOPE_RANGE, "range" }, \ + { RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE, "address-space" }, \ + { RISCV_TLB_FLUSH_SCOPE_ALL, "all" }) + +#define show_riscv_tlb_flush_path (path) \ + __print_symbolic(path, \ + { RISCV_TLB_FLUSH_PATH_LOCAL, "local" }, \ + { RISCV_TLB_FLUSH_PATH_SBI_RFENCE, "sbi-rfence" }, \ + { RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL, "cross-cpu-call" }) + +/* +* Record the invalidation request received by the RISC-V architecture code +* and the path selected by Linux. +* +* The target CPU mask represents the CPUs Linux intends to cover for the +* request. It can be correlated with per-CPU activity, but does not describe +* which harts ultimately performed an invalidation. +* +* The ASID is hardware-visible and may be reused. It must not be treated as a +* persistent identifier for an mm. +* +* The stride describes the invalidation granularity supplied to the RISC-V +* implementation. SBI RFENCE receives start, size and ASID, but not stride. +* +* The event is emitted at path selection time. For SBI RFENCE, it records +* delegation of the request to firmware; firmware processing after that +* point is outside the event's scope. +*/ +TRACE_EVENT (riscv_tlb_flush_path, + TP_PROTO(unsigned long start, unsigned long size, + unsigned long stride, unsigned long asid, bool has_mm, + const struct cpumask *cmask, + enum riscv_tlb_flush_scope scope, + enum riscv_tlb_flush_path path), + +TP_ARGS (start, size, stride, asid, has_mm, cmask, scope, path), + +TP_STRUCT__entry ( + __field(unsigned long, start) + __field(unsigned long, size) + __field(unsigned long, stride) + __field(unsigned long, asid) + __field(bool, has_mm) + __field(unsigned int, target_mask_weight) + __cpumask(target_cpus) + __field(u8, scope) +__field (u8, path) + ), + +TP_fast_assign ( + __entry->start = start; + __entry->size = size; + __entry->stride = stride; + __entry->asid = asid; + __entry->has_mm = has_mm; + __entry->target_mask_weight = cpumask_weight(cmask); + __assign_cpumask(target_cpus, cpumask_bits(cmask)); + __entry->scope = scope; + __entry->path = path; + ), + +TP_printk ("start=%#lx size=%#lx stride=%#lx asid=%#lx has_mm=%d target_mask_weight=%u target_cpus=%s scope=%s path=%s" , + __entry->start, __entry->size, __entry->stride, + __entry->asid, __entry->has_mm, + __entry->target_mask_weight,__get_cpumask (target_cpus), +show_riscv_tlb_flush_scope (__entry->scope), +show_riscv_tlb_flush_path (__entry->path)) +); + +#endif /* _TRACE_RISCV_TLB_H */ + +/* This part must be outside protection. */ +#include <trace/define_trace.h> ---base-commit:
77ae27fd98f3b548797c9f22c10ab5cf1c4ada53
change-id: 20260829-tlb_tracepoint-844105ab5092Best regards,
--
Roman 'Hedin' Storozhenko <[email protected]>_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscvSignature · 2 lines
Kind regards, Roman 'Hedin' Storozhenko
Quoted 29 lines · Jisheng Zhang
Quoted 18 lines · Roman Storozhenko
Quoted 7 lines · Jisheng Zhang
Quoted 4 lines · Roman 'Hedin' Storozhenko
Make RISC-V TLB flush path selection observable. Record whether Linux handles an invalidation locally, delegates it to SBI RFENCE, or executes it through a cross-CPU call, so MM activity can be correlated with the RISC-V, firmware, or Linux cross-CPU path carrying the request.
Quoted 7 lines
I didn't see too much usefullness of the trace here. why not use kprobe instead? can you plz show a real usage example of the trace? Which bug/performance problem can be solved conveniently with this trace only?
From another side, except x86, other architectures don't have the trace in the TLB path.
Hi Jisheng,
Quoted 8 lines
Thanks for the review. I agree that the current commit message describes the intended use rather abstractly.
A kprobe can certainly be used to investigate this. My motivation for a tracepoint is not that dynamic tracing cannot obtain the information, but that doing so requires knowledge of the current implementation and reconstruction of a semantic decision made inside
__flush_tlb_range()/flush_tlb_all().
Quoted 6 lines
I prefer aligning with other architectures(except x86), I.E not add this kind of trace. However, I would leave the decision to riscv maintainers.
FYI, you may notice that the riscv trace overhead is not trivial per
https://lore.kernel.org/linux-riscv/[email protected]/
Hi Jisheng,
Thanks for the feedback.
Regarding the comparison with other architectures, thanks to Michael Ellerman for pointing me to the PowerPC TLB tracing when we happened to discuss this at LPC 2026.
PowerPC defines the powerpc:tlbie and powerpc:tlbia trace events in arch/powerpc/include/asm/trace.h, and the corresponding trace calls are used in the PowerPC TLB invalidation paths.
These events are not identical to the proposed RISC-V event: the PowerPC events primarily describe architecture invalidation operations, whereas this patch records the path selected by Linux before dispatch. However, I think they provide useful precedent for architecture-specific TLB observability beyond x86.
Regarding kprobes, I agree that dynamic tracing can be used to investigate this.
My concern is that obtaining the same information requires knowledge of the current implementation and reconstruction of the decision made inside __flush_tlb_range() / flush_tlb_all().
For example, probing __flush_tlb_range() gives us the incoming request, but does not directly tell us whether Linux selected the local flush, SBI RFENCE, or cross-CPU-call path. A tracing tool has to probe and correlate implementation-specific downstream functions, or reproduce the selection logic itself.
The proposed event records this semantic decision directly at the point where Linux makes it. Users therefore do not need to depend on the current helper names, call graph, or exact implementation of the selection logic.
Regarding the tracepoint overhead issue, I agree that disabled-path overhead is important, especially in the TLB flush path.
However, I see the referenced SBI tracepoint case primarily as a RISC-V compiler/code-generation issue rather than as an argument against providing this kind of observability.
The referenced discussion itself shows that the generated code depends significantly on compiler behavior, and that this is an area being improved. The undesirable overhead is not an inherent semantic requirement of the tracepoint.
Even if the same code-generation problem currently affects this tracepoint, I don't think a temporary compiler limitation should by itself determine whether the tracepoint is useful or whether this observability belongs in the kernel.
If the compiler currently generates unacceptable disabled-path overhead, that is certainly something that should be improved or mitigated in the implementation. But I would separate that issue from the question of whether exposing the RISC-V TLB path-selection decision is useful.
In other words, I see the code-generation issue as an implementation concern, while the tracepoint itself provides information that otherwise has to be reconstructed from implementation details using dynamic tracing.
Thanks,
Roman
Quoted 404 lines
Quoted 404 lines
For example, probing
__flush_tlb_range()gives the request context, but not directly the path subsequently selected. To determine whether the request was handled locally, delegated through SBI RFENCE, or executed through Linux cross-CPU coordination, a tracing tool needs to probe/correlate implementation-specific call sites, or reproduce the selection logic itself.The intent of the proposed event is to expose that semantic decision directly, together with the target CPU mask and request context, rather than make users depend on the current helper names and call graph.
One concrete RISC-V example is commit
ae9e9f3d67dc:"RISC-V: clear hot-unplugged cores from all task
mm_cpumasks
to avoid rfence errors"In that case an offline CPU could remain in
mm_cpumask(), which was then used as the target of an SBI RFENCE request. OpenSBI checks the requested hart mask against online harts, so such a request could fail.With this event, such a situation could expose, for example:
target_cpus=0,3 target_mask_weight=2 scope=range path=sbi-rfence
while CPU3 is already offline.
That shows that Linux constructed an unexpected target mask before the request was handed to firmware, so the investigation can first focus on Linux's mm CPU tracking rather than starting inside the SBI implementation.
There is also a performance-debugging use case. Real-world latency investigations have found TLB shootdowns responsible for significant latency spikes; for example:
https://www.jabperf.com/how-to-deter-or-disarm-tlb-shootdowns/
That particular case eventually identified automatic NUMA balancing as the source of excessive shootdowns. I am not claiming that this proposed event alone would identify that root cause. On RISC-V, once TLB shootdowns have been identified as relevant, the event provides the architecture-specific next step: which CPUs Linux targeted, and whether Linux selected local handling, SBI RFENCE, or Linux cross-CPU coordination.
This distinction also helps separate where to investigate next. For an otherwise similar MM request:
path=sbi-rfence
points toward
firmware/platformhandling after Linux delegates the request, while:path=cross-cpu-call
points toward Linux cross-CPU/IPI handling and target-CPU activity.
An unexpectedly broad
target_cpusmask can similarly indicate that the cost comes from Linux targeting more CPUs than expected rather than from the execution time of the selected mechanism itself.I also put the workloads used to exercise the event here:
https://github.com/Romeus/tlb_workloads
They provide reproducible examples using the same 64-page
mprotect()request with different CPU footprints/configurations. I used them to exercise local, SBI RFENCE, and cross-CPU-call paths.I am not claiming that a tracepoint by itself solves these bugs, or that the same information is impossible to obtain with kprobes. The intended value is to expose the RISC-V TLB path-selection decision and its target context in one semantic record, without requiring each tracing tool to reconstruct that decision from implementation-specific probes.
I also take your point that, apart from x86, architectures generally do not currently have permanent tracing in the TLB path. If the use case is considered sufficient for a tracepoint, I will rework the commit message to make the concrete motivation and the distinction from dynamic probing clearer.
For context, I also posted an unchanged v2 RESEND on Sep 24; this reply is on the original v2 thread where your review landed.
https://lore.kernel.org/lkml/[email protected]/Thanks,
RomanQuoted 304 lines
Quoted 304 lines
The generic tlb:tlb_flush event describes TLB flush activity using architecture-independent reason and page-count information. The RISC-V implementation subsequently selects between local invalidation, SBI RFENCE, and Linux cross-CPU coordination, with additional architecture-specific request context available at that point.
Making this selection observable is useful when debugging RISC-V TLB shootdowns. When a remote invalidation is observed to be slow, the selected path determines whether to investigate SBI firmware and platform handling or Linux cross-CPU and IPI handling. An unexpectedly broad target mask can reveal an unintended address-space CPU footprint, while the range and stride distinguish invalidation requests with
different mapping granularities.
Place the event in the RISC-V implementation because the local, SBI RFENCE, or cross-CPU choice is made there, and SBI RFENCE and the invalidation stride are RISC-V-specific semantics rather than properties of the generic MM flush request.
Add riscv_tlb:riscv_tlb_flush_path in
flush_tlb_all()and__flush_tlb_range(). Record start, size, stride, the hardware-visible ASID, whether a specific mm is associated with the request, the target CPU mask and its weight, the requested scope, and the selected path.Record the complete target mask in addition to its weight because CPU identity cannot be reconstructed from a count and is needed to correlate the request with per-CPU scheduler, IPI, and firmware activity.
The event records the invalidation request and the path selected by Linux before the operation is dispatched. In particular, selecting the SBI RFENCE path means that Linux delegated the request to firmware; the event does not describe the implementation or outcome of that delegated operation.
Tested on QEMU virt with OpenSBI using local and shared-mm mprotect()/munmap() workloads. Local requests reported path=local, while remote requests reported path=sbi-rfence and were followed by the existing riscv:sbi_call RFENCE event.
The cross-CPU-call path was tested with QEMU virt using APLIC+IMSIC. A
MADV_PAGEOUTreclaim workload was used to exercise mm-independent global flushes. All reported path values (local, sbi-rfence and cross-cpu-call) and scope values (single, range, address-space and all) were observed.Signed-off-by Roman 'Hedin' Storozhenko <[email protected]>---
Add a RISC-V tracepoint for observing the path selected by Linux for TLB invalidation requests: local invalidation, SBI RFENCE, or Linux cross-CPU coordination.The tracepoint is intended to make RISC-V TLB shootdown behavior easier to correlate with MM activity, CPU targeting, SBI calls, and IPI handling. The patch records the invalidation request context and the Linux path-selection decision before the operation is dispatched.
The patch was tested on QEMU virt with both the SBI RFENCE path and an APLIC+IMSIC configuration. Local, SBI RFENCE, and cross-CPU-call paths
were exercised. All reported scope values -- single, range,
address-space, and all -- were also observed.
---
Changes in v2:
- Usetrace_call__riscv_tlb_flush_path()after the explicittrace_riscv_tlb_flush_path_enabled()check to avoid a second
tracepoint static-key test, as suggested by Steven Rostedt.
- Link to v1: https://lore.kernel.org/r/[email protected]--- arch/riscv/mm/tlbflush.c | 60 +++++++++++++++++++-- include/trace/events/riscv_tlb.h | 113 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+), 4 deletions(-)arch/riscv/mm/tlbflush.c +56
-4@@ -9,6 +9,9 @@#include <asm/mmu_context.h> #include <asm/cpufeature.h> +#define CREATE_TRACE_POINTS +#include <trace/events/riscv_tlb.h> +#define has_svinval () riscv_has_extension_unlikely(RISCV_ISA_EXT_SVINVAL)/* @@ -63,6 +66,33 @@ 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 ); } +static enum riscv_tlb_flush_scope +riscv_tlb_get_flush_scope (unsigned long size,unsigned long stride,bool has_mm) +{ +if (size ==FLUSH_TLB_MAX_SIZE ) +return has_mm ?RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE : +RISCV_TLB_FLUSH_SCOPE_ALL ; + +return size <= stride ?RISCV_TLB_FLUSH_SCOPE_SINGLE : +RISCV_TLB_FLUSH_SCOPE_RANGE ; +} + +static __always_inline void +riscv_tlb_trace_flush_path (const struct cpumask *cmask,unsigned long start, +unsigned long size,unsigned long stride, +unsigned long asid,bool has_mm, +enum riscv_tlb_flush_path path) +{ +enum riscv_tlb_flush_scope scope; + +if (!trace_riscv_tlb_flush_path_enabled ()) +return ; + + scope =riscv_tlb_get_flush_scope (size, stride, has_mm); +trace_call__riscv_tlb_flush_path (start, size, stride, asid, has_mm, + cmask, scope, path); +} +static void __ipi_flush_tlb_all (void *info) {local_flush_tlb_all ();@@ -70,12 +100,26 @@ static void __ipi_flush_tlb_all(void *info)void flush_tlb_all (void ) { -if (num_online_cpus () <2 ) +if (num_online_cpus () <2 ) { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_LOCAL );local_flush_tlb_all (); -else if (riscv_use_sbi_for_rfence ()) -sbi_remote_sfence_vma_asid (NULL ,0 ,FLUSH_TLB_MAX_SIZE ,FLUSH_TLB_NO_ASID ); -else + }else if (riscv_use_sbi_for_rfence ()) { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_SBI_RFENCE ); +sbi_remote_sfence_vma_asid (NULL ,0 ,FLUSH_TLB_MAX_SIZE , +FLUSH_TLB_NO_ASID ); + }else { +riscv_tlb_trace_flush_path (cpu_online_mask,0 , +FLUSH_TLB_MAX_SIZE ,0 , +FLUSH_TLB_NO_ASID , false, +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL );on_each_cpu (__ipi_flush_tlb_all,NULL ,1 ); + } }struct flush_tlb_range_data {@@ -107,12 +151,20 @@ 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) { +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm,RISCV_TLB_FLUSH_PATH_LOCAL );local_flush_tlb_range_asid (start, size, stride, asid); }else if (riscv_use_sbi_for_rfence ()) { +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm,RISCV_TLB_FLUSH_PATH_SBI_RFENCE );sbi_remote_sfence_vma_asid (cmask, start, size, asid); }else {struct flush_tlb_range_data ftd; +riscv_tlb_trace_flush_path (cmask, start, size, stride, asid, + !!mm, +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL ); + ftd.asid = asid; ftd.start = start; ftd.size = size;include/trace/events/riscv_tlb.h new file +113
-1@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM riscv_tlb + +#if !defined(_TRACE_RISCV_TLB_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_RISCV_TLB_H + +#include <linux/cpumask.h> +#include <linux/tracepoint.h> + +#ifndef _TRACE_RISCV_TLB_ENUMS +#define _TRACE_RISCV_TLB_ENUMS + +enum riscv_tlb_flush_scope { +RISCV_TLB_FLUSH_SCOPE_SINGLE , +RISCV_TLB_FLUSH_SCOPE_RANGE , +RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE , +RISCV_TLB_FLUSH_SCOPE_ALL , +}; + +enum riscv_tlb_flush_path { +RISCV_TLB_FLUSH_PATH_LOCAL , +RISCV_TLB_FLUSH_PATH_SBI_RFENCE , +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL , +}; + +#endif /* _TRACE_RISCV_TLB_ENUMS */ + +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_SINGLE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_RANGE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_ALL ); + +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_LOCAL ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_SBI_RFENCE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL ); + +#define show_riscv_tlb_flush_scope (scope) \ + __print_symbolic(scope, \ + { RISCV_TLB_FLUSH_SCOPE_SINGLE, "single" }, \ + { RISCV_TLB_FLUSH_SCOPE_RANGE, "range" }, \ + { RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE, "address-space" }, \ + { RISCV_TLB_FLUSH_SCOPE_ALL, "all" }) + +#define show_riscv_tlb_flush_path (path) \ + __print_symbolic(path, \ + { RISCV_TLB_FLUSH_PATH_LOCAL, "local" }, \ + { RISCV_TLB_FLUSH_PATH_SBI_RFENCE, "sbi-rfence" }, \ + { RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL, "cross-cpu-call" }) + +/* +* Record the invalidation request received by the RISC-V architecture code +* and the path selected by Linux. +* +* The target CPU mask represents the CPUs Linux intends to cover for the +* request. It can be correlated with per-CPU activity, but does not describe +* which harts ultimately performed an invalidation. +* +* The ASID is hardware-visible and may be reused. It must not be treated as a +* persistent identifier for an mm. +* +* The stride describes the invalidation granularity supplied to the RISC-V +* implementation. SBI RFENCE receives start, size and ASID, but not stride. +* +* The event is emitted at path selection time. For SBI RFENCE, it records +* delegation of the request to firmware; firmware processing after that +* point is outside the event's scope. +*/ +TRACE_EVENT (riscv_tlb_flush_path, + TP_PROTO(unsigned long start, unsigned long size, + unsigned long stride, unsigned long asid, bool has_mm, + const struct cpumask *cmask, + enum riscv_tlb_flush_scope scope, + enum riscv_tlb_flush_path path), + +TP_ARGS (start, size, stride, asid, has_mm, cmask, scope, path), + +TP_STRUCT__entry ( + __field(unsigned long, start) + __field(unsigned long, size) + __field(unsigned long, stride) + __field(unsigned long, asid) + __field(bool, has_mm) + __field(unsigned int, target_mask_weight) + __cpumask(target_cpus) + __field(u8, scope) +__field (u8, path) + ), + +TP_fast_assign ( + __entry->start = start; + __entry->size = size; + __entry->stride = stride; + __entry->asid = asid; + __entry->has_mm = has_mm; + __entry->target_mask_weight = cpumask_weight(cmask); + __assign_cpumask(target_cpus, cpumask_bits(cmask)); + __entry->scope = scope; + __entry->path = path; + ), + +TP_printk ("start=%#lx size=%#lx stride=%#lx asid=%#lx has_mm=%d target_mask_weight=%u target_cpus=%s scope=%s path=%s" , + __entry->start, __entry->size, __entry->stride, + __entry->asid, __entry->has_mm, + __entry->target_mask_weight,__get_cpumask (target_cpus), +show_riscv_tlb_flush_scope (__entry->scope), +show_riscv_tlb_flush_path (__entry->path)) +); + +#endif /* _TRACE_RISCV_TLB_H */ + +/* This part must be outside protection. */ +#include <trace/define_trace.h> ---base-commit:
77ae27fd98f3b548797c9f22c10ab5cf1c4ada53
change-id: 20260829-tlb_tracepoint-844105ab5092Best regards,
--
Roman 'Hedin' Storozhenko <[email protected]>Quoted 4 lines
Quoted 4 lines
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv--
Kind regards,
Roman 'Hedin' Storozhenko
Signature · 2 lines
Kind regards, Roman 'Hedin' Storozhenko
Quoted 61 lines · Roman Storozhenko
Quoted 29 lines · Jisheng Zhang
Quoted 18 lines · Roman Storozhenko
Quoted 7 lines · Jisheng Zhang
Quoted 4 lines · Roman 'Hedin' Storozhenko
Make RISC-V TLB flush path selection observable. Record whether Linux handles an invalidation locally, delegates it to SBI RFENCE, or executes it through a cross-CPU call, so MM activity can be correlated with the RISC-V, firmware, or Linux cross-CPU path carrying the request.
Quoted 7 lines
I didn't see too much usefullness of the trace here. why not use kprobe instead? can you plz show a real usage example of the trace? Which bug/performance problem can be solved conveniently with this trace only?
From another side, except x86, other architectures don't have the trace in the TLB path.
Hi Jisheng,
Quoted 8 lines
Thanks for the review. I agree that the current commit message describes the intended use rather abstractly.
A kprobe can certainly be used to investigate this. My motivation for a tracepoint is not that dynamic tracing cannot obtain the information, but that doing so requires knowledge of the current implementation and reconstruction of a semantic decision made inside
__flush_tlb_range()/flush_tlb_all().Quoted 6 lines
I prefer aligning with other architectures(except x86), I.E not add this kind of trace. However, I would leave the decision to riscv maintainers.
FYI, you may notice that the riscv trace overhead is not trivial per
https://lore.kernel.org/linux-riscv/[email protected]/Hi Jisheng,
Thanks for the feedback.
Regarding the comparison with other architectures, thanks to Michael Ellerman for pointing me to the PowerPC TLB tracing when we happened to discuss this at LPC 2026.
PowerPC defines the powerpc:tlbie and powerpc:tlbia trace events in
arch/powerpc/include/asm/trace.h, and the corresponding trace calls are used in the PowerPC TLB invalidation paths.These events are not identical to the proposed RISC-V event: the PowerPC events primarily describe architecture invalidation operations, whereas this patch records the path selected by Linux before dispatch. However, I think they provide useful precedent for architecture-specific TLB observability beyond x86.
Regarding kprobes, I agree that dynamic tracing can be used to investigate this.
My concern is that obtaining the same information requires knowledge of the current implementation and reconstruction of the decision made inside
__flush_tlb_range()/flush_tlb_all().
Quoted 5 lines
For example, probing
__flush_tlb_range()gives us the incoming request, but does not directly tell us whether Linux selected the local flush, SBI RFENCE, or cross-CPU-call path. A tracing tool has to probe and correlate implementation-specific downstream functions, or reproduce the selection logic itself.
Hmm, let's look at where the tracepoints added:
void __flush_tlb_range(struct mm_struct *mm,
const struct cpumask *cmask, unsigned long start, unsigned long size, unsigned long stride)
void flush_tlb_all(void)
most of the information you exported with the tracepoints are just parameters of flush_tlb_range.
So kprobe or function trace can also get most of them, while whether local or not needs to calcuate the cmask, but getting this info is also easy via. adding one LoC to the kprobe routine.
The tlb invalidation is frequently executed, so we need to carefully evaluate the convience VS maitanince offort and performance overhead when adding permanent tracepoints, given current riscv tracepoint overhead isn't trivial.
BTW: whether SBI RFENCE or cross-cpu-call is much easier. Just search IPI in /proc/interrupts. No need to kprobe/function trace/tracepoint
Quoted 23 lines
The proposed event records this semantic decision directly at the point where Linux makes it. Users therefore do not need to depend on the current helper names, call graph, or exact implementation of the selection logic.
Regarding the tracepoint overhead issue, I agree that disabled-path overhead is important, especially in the TLB flush path.
However, I see the referenced SBI tracepoint case primarily as a RISC-V compiler/code-generation issue rather than as an argument against providing this kind of observability.
The referenced discussion itself shows that the generated code depends significantly on compiler behavior, and that this is an area being improved. The undesirable overhead is not an inherent semantic requirement of the tracepoint.
Even if the same code-generation problem currently affects this tracepoint, I don't think a temporary compiler limitation should by itself determine whether the tracepoint is useful or whether this observability belongs in the kernel.
Quoted 6 lines
If the compiler currently generates unacceptable disabled-path overhead, that is certainly something that should be improved or mitigated in the implementation. But I would separate that issue from the question of whether exposing the RISC-V TLB path-selection decision is useful.
But we need to live with it. Per the Radim email, gcc14 can still see the issue, given the minimum gcc version of kernel building requirement is gcc8 now, gcc14 has a long time to bailout.
And another tricky point here is
one of your motivations of adding the tracepoint is to profile then improve the tlb related code path performance, but adding the tracepoint brings non-trivial overhead to tlb code path.
Hi Jisheng,
Thanks for the clarification.
Since Radim's tracepoint code-generation results are now part of this discussion, I've added him to Cc in case I misinterpret any aspect of that issue.
I agree with you on one point regarding the compiler overhead. Since the kernel supports older GCC versions, we have to take the code generated by those supported compilers into account today. So describing the issue simply as temporary was too strong.
However, I think there are two separate questions here: whether the event exposes useful information, and whether the disabled-path cost of its current implementation is acceptable.
Regarding kprobes, most of the raw fields in this event are indeed already arguments of __flush_tlb_range(). My point is not that these values are impossible to obtain dynamically.
Kprobes can be used to observe a very large part of the kernel. If the ability to reconstruct an event with kprobes were by itself sufficient reason not to add a tracepoint, the same reasoning would apply to many existing tracepoints.
The distinction I see here is between observing implementation details and exposing the semantic event directly.
For this case, the interesting information is not only start, size, stride, cmask, etc., but their correlation with the path Linux selected for that particular request:
local
SBI RFENCE
cross-CPU call
Calculating the local case from cmask may be simple with the current implementation, but the tracing program still has to reproduce knowledge of the selection logic. Likewise, observing downstream functions ties the tracing program to the current call graph.
I also don't think /proc/interrupts provides equivalent information here.
The RISC-V IPI statistics are aggregate counters. In particular, Linux accounts IPI_CALL_FUNC as "Function call interrupts", while SBI RFENCE is handled below Linux by firmware; for example, OpenSBI implements the remote TLB operation using its own IPI_TLB event.
So checking /proc/interrupts can certainly be useful as a sanity check in a controlled workload: an increase in function-call IPIs can indicate use of the Linux cross-CPU path.
But it cannot associate that IPI activity with a particular TLB invalidation request, and it cannot correlate it with the range, ASID, stride and target cpumask. Other users of the call-function IPI also contribute to the same counters. In a concurrent workload this becomes an inference rather than an observation of the TLB path decision.
That per-request correlation is what I am trying to expose with the tracepoint.
Regarding the performance argument, I agree that the disabled path must be cheap and that the current RISC-V tracepoint code-generation issue is relevant when evaluating the patch.
I would not claim that all tracepoints have exactly the same overhead. My narrower point is that the issue referenced in Radim's thread is a general interaction between RISC-V tracepoints and compiler code generation rather than something specific to the semantics of this event.
If the generated disabled path is considered too expensive, that is a valid implementation concern and may require improvement or mitigation. But I don't think it shows that the TLB path-selection event itself is redundant because the information can be reconstructed with kprobes.
There is also no contradiction, in my view, between using the event for performance analysis and the event having overhead when enabled. Tracing normally perturbs the workload when it is enabled. The intended use here is primarily to understand things such as the distribution of local/SBI/IPI decisions and the request context associated with those decisions, rather than to use the tracepoint itself for cycle-accurate measurement of the cost of a TLB flush.
The important requirement is therefore that the disabled path be sufficiently cheap.
I think the PowerPC TLB tracepoints also remain a useful precedent
here: they show that permanent architecture-specific TLB observability can be worthwhile when the architecture-specific information is useful.
So I agree that convenience, maintenance cost and disabled-path overhead all need to be considered. I just don't think that kprobes or /proc/interrupts provide the same abstraction as recording the path-selection decision directly.
Thanks,
Roman
Quoted 128 lines · Jisheng Zhang
Quoted 61 lines · Roman Storozhenko
Quoted 29 lines · Jisheng Zhang
Quoted 18 lines · Roman Storozhenko
Quoted 7 lines · Jisheng Zhang
Quoted 4 lines · Roman 'Hedin' Storozhenko
Make RISC-V TLB flush path selection observable. Record whether Linux handles an invalidation locally, delegates it to SBI RFENCE, or executes it through a cross-CPU call, so MM activity can be correlated with the RISC-V, firmware, or Linux cross-CPU path carrying the request.
Quoted 7 lines
I didn't see too much usefullness of the trace here. why not use kprobe instead? can you plz show a real usage example of the trace? Which bug/performance problem can be solved conveniently with this trace only?
From another side, except x86, other architectures don't have the trace in the TLB path.
Hi Jisheng,
Quoted 8 lines
Thanks for the review. I agree that the current commit message describes the intended use rather abstractly.
A kprobe can certainly be used to investigate this. My motivation for a tracepoint is not that dynamic tracing cannot obtain the information, but that doing so requires knowledge of the current implementation and reconstruction of a semantic decision made inside
__flush_tlb_range()/flush_tlb_all().Quoted 6 lines
I prefer aligning with other architectures(except x86), I.E not add this kind of trace. However, I would leave the decision to riscv maintainers.
FYI, you may notice that the riscv trace overhead is not trivial per
https://lore.kernel.org/linux-riscv/[email protected]/Hi Jisheng,
Thanks for the feedback.
Regarding the comparison with other architectures, thanks to Michael Ellerman for pointing me to the PowerPC TLB tracing when we happened to discuss this at LPC 2026.
PowerPC defines the powerpc:tlbie and powerpc:tlbia trace events in
arch/powerpc/include/asm/trace.h, and the corresponding trace calls are used in the PowerPC TLB invalidation paths.These events are not identical to the proposed RISC-V event: the PowerPC events primarily describe architecture invalidation operations, whereas this patch records the path selected by Linux before dispatch. However, I think they provide useful precedent for architecture-specific TLB observability beyond x86.
Regarding kprobes, I agree that dynamic tracing can be used to investigate this.
My concern is that obtaining the same information requires knowledge of the current implementation and reconstruction of the decision made inside
__flush_tlb_range()/flush_tlb_all().Quoted 5 lines
For example, probing
__flush_tlb_range()gives us the incoming request, but does not directly tell us whether Linux selected the local flush, SBI RFENCE, or cross-CPU-call path. A tracing tool has to probe and correlate implementation-specific downstream functions, or reproduce the selection logic itself.Hmm, let's look at where the tracepoints added:
void __flush_tlb_range(structmm_struct*mm,
const struct cpumask *cmask, unsigned long start, unsigned long size, unsigned long stride)
voidflush_tlb_all(void)most of the information you exported with the tracepoints are just parameters of
flush_tlb_range.
So kprobe or function trace can also get most of them, while whether local or not needs to calcuate the cmask, but getting this info is also easy via. adding one LoC to the kprobe routine.The tlb invalidation is frequently executed, so we need to carefully evaluate the convience VS maitanince offort and performance overhead when adding permanent tracepoints, given current riscv tracepoint overhead isn't trivial.
BTW: whether SBI RFENCE or cross-cpu-call is much easier. Just search IPI in
/proc/interrupts. No need to kprobe/function trace/tracepointQuoted 23 lines
The proposed event records this semantic decision directly at the point where Linux makes it. Users therefore do not need to depend on the current helper names, call graph, or exact implementation of the selection logic.
Regarding the tracepoint overhead issue, I agree that disabled-path overhead is important, especially in the TLB flush path.
However, I see the referenced SBI tracepoint case primarily as a RISC-V compiler/code-generation issue rather than as an argument against providing this kind of observability.
The referenced discussion itself shows that the generated code depends significantly on compiler behavior, and that this is an area being improved. The undesirable overhead is not an inherent semantic requirement of the tracepoint.
Even if the same code-generation problem currently affects this tracepoint, I don't think a temporary compiler limitation should by itself determine whether the tracepoint is useful or whether this observability belongs in the kernel.
Quoted 6 lines
If the compiler currently generates unacceptable disabled-path overhead, that is certainly something that should be improved or mitigated in the implementation. But I would separate that issue from the question of whether exposing the RISC-V TLB path-selection decision is useful.
But we need to live with it. Per the Radim email, gcc14 can still see the issue, given the minimum gcc version of kernel building requirement is gcc8 now, gcc14 has a long time to bailout.
And another tricky point here is
one of your motivations of adding the tracepoint is to profile then improve the tlb related code path performance, but adding the tracepoint brings non-trivial overhead to tlb code path.
Signature · 2 lines
Kind regards, Roman 'Hedin' Storozhenko
Quoted 42 lines · Roman Storozhenko
Hi Jisheng,
Thanks for the clarification.
Since Radim's tracepoint code-generation results are now part of this discussion, I've added him to Cc in case I misinterpret any aspect of that issue.
I agree with you on one point regarding the compiler overhead. Since the kernel supports older GCC versions, we have to take the code generated by those supported compilers into account today. So describing the issue simply as temporary was too strong.
However, I think there are two separate questions here: whether the event exposes useful information, and whether the disabled-path cost of its current implementation is acceptable.
Regarding kprobes, most of the raw fields in this event are indeed already arguments of
__flush_tlb_range(). My point is not that these values are impossible to obtain dynamically.Kprobes can be used to observe a very large part of the kernel. If the ability to reconstruct an event with kprobes were by itself sufficient reason not to add a tracepoint, the same reasoning would apply to many existing tracepoints.
The distinction I see here is between observing implementation details and exposing the semantic event directly.
For this case, the interesting information is not only
start,size,stride,cmask, etc., but their correlation with the path Linux selected for that particular request:local
SBI RFENCE
cross-CPU callCalculating the local case from
cmaskmay be simple with the current implementation, but the tracing program still has to reproduce knowledge of the selection logic. Likewise, observing downstream functions ties the tracing program to the current call graph.
Quoted 6 lines
I also don't think
/proc/interruptsprovides equivalent information here.The RISC-V IPI statistics are aggregate counters. In particular, Linux accounts
IPI_CALL_FUNCas "Function call interrupts", while SBI RFENCE is handled below Linux by firmware; for example, OpenSBI implements the remote TLB operation using its ownIPI_TLBevent.
I think you misunderstood something. A riscv platform uses either SBI RFENCE or IPI for tlb shootdown, and won't change. So you can know whether the system is SBI RFENCE powered or IPI powered by cat /proc/interrupts. It's much simpler.
Quoted 188 lines
So checking
/proc/interruptscan certainly be useful as a sanity check in a controlled workload: an increase in function-call IPIs can indicate use of the Linux cross-CPU path.But it cannot associate that IPI activity with a particular TLB invalidation request, and it cannot correlate it with the range, ASID, stride and target cpumask. Other users of the call-function IPI also contribute to the same counters. In a concurrent workload this becomes an inference rather than an observation of the TLB path decision.
That per-request correlation is what I am trying to expose with the tracepoint.
Regarding the performance argument, I agree that the disabled path must be cheap and that the current RISC-V tracepoint code-generation issue is relevant when evaluating the patch.
I would not claim that all tracepoints have exactly the same overhead. My narrower point is that the issue referenced in Radim's thread is a general interaction between RISC-V tracepoints and compiler code generation rather than something specific to the semantics of this event.
If the generated disabled path is considered too expensive, that is a valid implementation concern and may require improvement or mitigation. But I don't think it shows that the TLB path-selection event itself is redundant because the information can be reconstructed with kprobes.
There is also no contradiction, in my view, between using the event for performance analysis and the event having overhead when enabled. Tracing normally perturbs the workload when it is enabled. The intended use here is primarily to understand things such as the distribution of local/SBI/IPI decisions and the request context associated with those decisions, rather than to use the tracepoint itself for cycle-accurate measurement of the cost of a TLB flush.
The important requirement is therefore that the disabled path be sufficiently cheap.
I think the PowerPC TLB tracepoints also remain a useful precedent
here: they show that permanent architecture-specific TLB observability can be worthwhile when the architecture-specific information is useful.So I agree that convenience, maintenance cost and disabled-path overhead all need to be considered. I just don't think that kprobes or
/proc/interruptsprovide the same abstraction as recording the path-selection decision directly.Thanks,
RomanQuoted 128 lines · Jisheng Zhang
Quoted 61 lines · Roman Storozhenko
Quoted 29 lines · Jisheng Zhang
Quoted 18 lines · Roman Storozhenko
Quoted 7 lines · Jisheng Zhang
Quoted 4 lines · Roman 'Hedin' Storozhenko
Make RISC-V TLB flush path selection observable. Record whether Linux handles an invalidation locally, delegates it to SBI RFENCE, or executes it through a cross-CPU call, so MM activity can be correlated with the RISC-V, firmware, or Linux cross-CPU path carrying the request.
Quoted 7 lines
I didn't see too much usefullness of the trace here. why not use kprobe instead? can you plz show a real usage example of the trace? Which bug/performance problem can be solved conveniently with this trace only?
From another side, except x86, other architectures don't have the trace in the TLB path.
Hi Jisheng,
Quoted 8 lines
Thanks for the review. I agree that the current commit message describes the intended use rather abstractly.
A kprobe can certainly be used to investigate this. My motivation for a tracepoint is not that dynamic tracing cannot obtain the information, but that doing so requires knowledge of the current implementation and reconstruction of a semantic decision made inside
__flush_tlb_range()/flush_tlb_all().Quoted 6 lines
I prefer aligning with other architectures(except x86), I.E not add this kind of trace. However, I would leave the decision to riscv maintainers.
FYI, you may notice that the riscv trace overhead is not trivial per
https://lore.kernel.org/linux-riscv/[email protected]/Hi Jisheng,
Thanks for the feedback.
Regarding the comparison with other architectures, thanks to Michael Ellerman for pointing me to the PowerPC TLB tracing when we happened to discuss this at LPC 2026.
PowerPC defines the powerpc:tlbie and powerpc:tlbia trace events in
arch/powerpc/include/asm/trace.h, and the corresponding trace calls are used in the PowerPC TLB invalidation paths.These events are not identical to the proposed RISC-V event: the PowerPC events primarily describe architecture invalidation operations, whereas this patch records the path selected by Linux before dispatch. However, I think they provide useful precedent for architecture-specific TLB observability beyond x86.
Regarding kprobes, I agree that dynamic tracing can be used to investigate this.
My concern is that obtaining the same information requires knowledge of the current implementation and reconstruction of the decision made inside
__flush_tlb_range()/flush_tlb_all().Quoted 5 lines
For example, probing
__flush_tlb_range()gives us the incoming request, but does not directly tell us whether Linux selected the local flush, SBI RFENCE, or cross-CPU-call path. A tracing tool has to probe and correlate implementation-specific downstream functions, or reproduce the selection logic itself.Hmm, let's look at where the tracepoints added:
void __flush_tlb_range(structmm_struct*mm,
const struct cpumask *cmask, unsigned long start, unsigned long size, unsigned long stride)
voidflush_tlb_all(void)most of the information you exported with the tracepoints are just parameters of
flush_tlb_range.
So kprobe or function trace can also get most of them, while whether local or not needs to calcuate the cmask, but getting this info is also easy via. adding one LoC to the kprobe routine.The tlb invalidation is frequently executed, so we need to carefully evaluate the convience VS maitanince offort and performance overhead when adding permanent tracepoints, given current riscv tracepoint overhead isn't trivial.
BTW: whether SBI RFENCE or cross-cpu-call is much easier. Just search IPI in
/proc/interrupts. No need to kprobe/function trace/tracepointQuoted 23 lines
The proposed event records this semantic decision directly at the point where Linux makes it. Users therefore do not need to depend on the current helper names, call graph, or exact implementation of the selection logic.
Regarding the tracepoint overhead issue, I agree that disabled-path overhead is important, especially in the TLB flush path.
However, I see the referenced SBI tracepoint case primarily as a RISC-V compiler/code-generation issue rather than as an argument against providing this kind of observability.
The referenced discussion itself shows that the generated code depends significantly on compiler behavior, and that this is an area being improved. The undesirable overhead is not an inherent semantic requirement of the tracepoint.
Even if the same code-generation problem currently affects this tracepoint, I don't think a temporary compiler limitation should by itself determine whether the tracepoint is useful or whether this observability belongs in the kernel.
Quoted 6 lines
If the compiler currently generates unacceptable disabled-path overhead, that is certainly something that should be improved or mitigated in the implementation. But I would separate that issue from the question of whether exposing the RISC-V TLB path-selection decision is useful.
But we need to live with it. Per the Radim email, gcc14 can still see the issue, given the minimum gcc version of kernel building requirement is gcc8 now, gcc14 has a long time to bailout.
And another tricky point here is
one of your motivations of adding the tracepoint is to profile then improve the tlb related code path performance, but adding the tracepoint brings non-trivial overhead to tlb code path.Signature · 2 lines
Kind regards, Roman 'Hedin' Storozhenko
Quoted 50 lines · Jisheng Zhang
Quoted 42 lines · Roman Storozhenko
Hi Jisheng,
Thanks for the clarification.
Since Radim's tracepoint code-generation results are now part of this discussion, I've added him to Cc in case I misinterpret any aspect of that issue.
I agree with you on one point regarding the compiler overhead. Since the kernel supports older GCC versions, we have to take the code generated by those supported compilers into account today. So describing the issue simply as temporary was too strong.
However, I think there are two separate questions here: whether the event exposes useful information, and whether the disabled-path cost of its current implementation is acceptable.
Regarding kprobes, most of the raw fields in this event are indeed already arguments of
__flush_tlb_range(). My point is not that these values are impossible to obtain dynamically.Kprobes can be used to observe a very large part of the kernel. If the ability to reconstruct an event with kprobes were by itself sufficient reason not to add a tracepoint, the same reasoning would apply to many existing tracepoints.
The distinction I see here is between observing implementation details and exposing the semantic event directly.
For this case, the interesting information is not only
start,size,stride,cmask, etc., but their correlation with the path Linux selected for that particular request:local
SBI RFENCE
cross-CPU callCalculating the local case from
cmaskmay be simple with the current implementation, but the tracing program still has to reproduce knowledge of the selection logic. Likewise, observing downstream functions ties the tracing program to the current call graph.Quoted 6 lines
I also don't think
/proc/interruptsprovides equivalent information here.The RISC-V IPI statistics are aggregate counters. In particular, Linux accounts
IPI_CALL_FUNCas "Function call interrupts", while SBI RFENCE is handled below Linux by firmware; for example, OpenSBI implements the remote TLB operation using its ownIPI_TLBevent.
Quoted 4 lines
I think you misunderstood something. A riscv platform uses either SBI RFENCE or IPI for tlb shootdown, and won't change. So you can know whether the system is SBI RFENCE powered or IPI powered by cat
/proc/interrupts. It's much simpler.
Aha, I missed aclint sswi case, if sswi, no explict /proc/interrupts difference between sswi and traditonal plic. However, whether TLB is powered by SBI RFENCE or IPI can still be known by checking "providing IPIs using SBI IPI extension" in dmesg.
Quoted 8 lines · Jisheng Zhang
Quoted 5 lines · Jisheng Zhang
On Wed, Oct 14, 2026 at 05:58:42PM +0200, Roman Storozhenko wrote:
I think you misunderstood something. A riscv platform uses either SBI RFENCE or IPI for tlb shootdown, and won't change. So you can know whether the system is SBI RFENCE powered or IPI powered by cat/proc/interrupts. It's much simpler.
Quoted 4 lines
Aha, I missed aclint sswi case, if sswi, no explict
/proc/interruptsdifference between sswi and traditonal plic. However, whether TLB is powered by SBI RFENCE or IPI can still be known by checking "providing IPIs using SBI IPI extension" in dmesg.
Thanks, I see your point now.
I was treating SBI RFENCE vs the Linux cross-CPU path as part of the per-request selection, but with the current implementation riscv_sbi_for_rfence is effectively a system-wide choice established during initialization.
I reworked the patch in v3 based on your feedback. The tracepoint no longer reports the local/SBI RFENCE/cross-CPU implementation path. Instead, it describes the semantic TLB invalidation request itself, including the range, stride, ASID, target CPU mask and request scope.
I still think there is value in exposing this information as one semantic event. The individual pieces can be reconstructed with dynamic tracing, but having them together makes it straightforward to analyze the distribution and characteristics of TLB invalidations across a workload without depending on the current implementation details.
The v3 patch is here:
https://lore.kernel.org/all/[email protected]/
Thanks for pointing out the distinction. It helped simplify the event and make its abstraction clearer.
Signature · 2 lines
Kind regards, Roman 'Hedin' Storozhenko
Quoted 33 lines · Roman Storozhenko
Quoted 8 lines · Jisheng Zhang
Quoted 5 lines · Jisheng Zhang
On Wed, Oct 14, 2026 at 05:58:42PM +0200, Roman Storozhenko wrote:
I think you misunderstood something. A riscv platform uses either SBI RFENCE or IPI for tlb shootdown, and won't change. So you can know whether the system is SBI RFENCE powered or IPI powered by cat/proc/interrupts. It's much simpler.Quoted 4 lines
Aha, I missed aclint sswi case, if sswi, no explict
/proc/interruptsdifference between sswi and traditonal plic. However, whether TLB is powered by SBI RFENCE or IPI can still be known by checking "providing IPIs using SBI IPI extension" in dmesg.Thanks, I see your point now.
I was treating SBI RFENCE vs the Linux cross-CPU path as part of the per-request selection, but with the current implementation
riscv_sbi_for_rfenceis effectively a system-wide choice established during initialization.I reworked the patch in v3 based on your feedback. The tracepoint no longer reports the local/SBI RFENCE/cross-CPU implementation path. Instead, it describes the semantic TLB invalidation request itself, including the range, stride, ASID, target CPU mask and request scope.
I still think there is value in exposing this information as one semantic event. The individual pieces can be reconstructed with dynamic tracing, but having them together makes it straightforward to analyze the distribution and characteristics of TLB invalidations across a workload without depending on the current implementation details.
Quoted 6 lines
The v3 patch is here:
https://lore.kernel.org/all/[email protected]/
Thanks for pointing out the distinction. It helped simplify the event and make its abstraction clearer.
And quick v4, where I removed unnecessary cleaning for checkpatch:
https://lore.kernel.org/all/[email protected]/
Quoted 4 lines
--
Kind regards,
Roman 'Hedin' Storozhenko
Signature · 2 lines
Kind regards, Roman 'Hedin' Storozhenko
Steven Rostedt
maintainerinclude/trace/events/riscv_tlb.h
Quoted 1 line · On Sun, 30 Aug 2026 16:36:37 +0200 Roman 'Hedin' Storozhenko…
include/trace/events/riscv_tlb.h +0
-0
Please do not put architecture specific events in include/trace.
They belong in the architecture.
See the example code in samples/trace_events/trace-events-sample.[ch] as well as the Makefile there on how to put trace events in directories outside of include/trace/
Thanks,
-- Steve
Quoted 118 lines
new file mode 100644
index 000000000000..3eff171ec54finclude/trace/events/riscv_tlb.h new file +113
-0@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM riscv_tlb + +#if !defined(_TRACE_RISCV_TLB_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_RISCV_TLB_H + +#include <linux/cpumask.h> +#include <linux/tracepoint.h> + +#ifndef _TRACE_RISCV_TLB_ENUMS +#define _TRACE_RISCV_TLB_ENUMS + +enum riscv_tlb_flush_scope { +RISCV_TLB_FLUSH_SCOPE_SINGLE , +RISCV_TLB_FLUSH_SCOPE_RANGE , +RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE , +RISCV_TLB_FLUSH_SCOPE_ALL , +}; + +enum riscv_tlb_flush_path { +RISCV_TLB_FLUSH_PATH_LOCAL , +RISCV_TLB_FLUSH_PATH_SBI_RFENCE , +RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL , +}; + +#endif /* _TRACE_RISCV_TLB_ENUMS */ + +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_SINGLE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_RANGE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_SCOPE_ALL ); + +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_LOCAL ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_SBI_RFENCE ); +TRACE_DEFINE_ENUM (RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL ); + +#define show_riscv_tlb_flush_scope (scope) \ + __print_symbolic(scope, \ + { RISCV_TLB_FLUSH_SCOPE_SINGLE, "single" }, \ + { RISCV_TLB_FLUSH_SCOPE_RANGE, "range" }, \ + { RISCV_TLB_FLUSH_SCOPE_ADDRESS_SPACE, "address-space" }, \ + { RISCV_TLB_FLUSH_SCOPE_ALL, "all" }) + +#define show_riscv_tlb_flush_path (path) \ + __print_symbolic(path, \ + { RISCV_TLB_FLUSH_PATH_LOCAL, "local" }, \ + { RISCV_TLB_FLUSH_PATH_SBI_RFENCE, "sbi-rfence" }, \ + { RISCV_TLB_FLUSH_PATH_CROSS_CPU_CALL, "cross-cpu-call" }) + +/* +* Record the invalidation request received by the RISC-V architecture code +* and the path selected by Linux. +* +* The target CPU mask represents the CPUs Linux intends to cover for the +* request. It can be correlated with per-CPU activity, but does not describe +* which harts ultimately performed an invalidation. +* +* The ASID is hardware-visible and may be reused. It must not be treated as a +* persistent identifier for an mm. +* +* The stride describes the invalidation granularity supplied to the RISC-V +* implementation. SBI RFENCE receives start, size and ASID, but not stride. +* +* The event is emitted at path selection time. For SBI RFENCE, it records +* delegation of the request to firmware; firmware processing after that +* point is outside the event's scope. +*/ +TRACE_EVENT (riscv_tlb_flush_path, + TP_PROTO(unsigned long start, unsigned long size, + unsigned long stride, unsigned long asid, bool has_mm, + const struct cpumask *cmask, + enum riscv_tlb_flush_scope scope, + enum riscv_tlb_flush_path path), + +TP_ARGS (start, size, stride, asid, has_mm, cmask, scope, path), + +TP_STRUCT__entry ( + __field(unsigned long, start) + __field(unsigned long, size) + __field(unsigned long, stride) + __field(unsigned long, asid) + __field(bool, has_mm) + __field(unsigned int, target_mask_weight) + __cpumask(target_cpus) + __field(u8, scope) +__field (u8, path) + ), + +TP_fast_assign ( + __entry->start = start; + __entry->size = size; + __entry->stride = stride; + __entry->asid = asid; + __entry->has_mm = has_mm; + __entry->target_mask_weight = cpumask_weight(cmask); + __assign_cpumask(target_cpus, cpumask_bits(cmask)); + __entry->scope = scope; + __entry->path = path; + ), + +TP_printk ("start=%#lx size=%#lx stride=%#lx asid=%#lx has_mm=%d target_mask_weight=%u target_cpus=%s scope=%s path=%s" , + __entry->start, __entry->size, __entry->stride, + __entry->asid, __entry->has_mm, + __entry->target_mask_weight,__get_cpumask (target_cpus), +show_riscv_tlb_flush_scope (__entry->scope), +show_riscv_tlb_flush_path (__entry->path)) +); + +#endif /* _TRACE_RISCV_TLB_H */ + +/* This part must be outside protection. */ +#include <trace/define_trace.h>
Quoted 10 lines · Steven Rostedt
include/trace/events/riscv_tlb.h
Quoted 1 line · On Sun, 30 Aug 2026 16:36:37 +0200 Roman 'Hedin' Storozhenko…
include/trace/events/riscv_tlb.h +0
-0Please do not put architecture specific events in
include/trace.They belong in the architecture.
Quoted 8 lines
See the example code in samples/trace_events/trace-events-sample.[ch] as well as the
Makefilethere on how to put trace events in directories outside ofinclude/trace/Thanks,
-- Steve
Hi Steve,
Thanks for the review.
I've moved the trace event definition to arch/riscv/include/asm/riscv_tlb.h in v5, following the existing RISC-V tracepoint convention.
The updated patch is available here:
https://lore.kernel.org/linux-riscv/[email protected]/
Thanks for pointing this out.
Signature · 2 lines
Kind regards, Roman 'Hedin' Storozhenko
Quoted 25 lines · Roman Storozhenko
Quoted 10 lines · Steven Rostedt
include/trace/events/riscv_tlb.h
Quoted 1 line · On Sun, 30 Aug 2026 16:36:37 +0200 Roman 'Hedin' Storozhenko…
include/trace/events/riscv_tlb.h +0
-0Please do not put architecture specific events in
include/trace.They belong in the architecture.
Quoted 8 lines
See the example code in samples/trace_events/trace-events-sample.[ch] as well as the
Makefilethere on how to put trace events in directories outside ofinclude/trace/Thanks,
-- Steve
Hi Steve,
Thanks for the review.
Quoted 8 lines
I've moved the trace event definition to
arch/riscv/include/asm/riscv_tlb.hin v5, following the existing RISC-V tracepoint convention.The updated patch is available here:
https://lore.kernel.org/linux-riscv/[email protected]/Thanks for pointing this out.
Hi Steve,
I've posted v6 with an additional fix for
the alignment of the dynamically stored CPU mask.
The trace event definition remains underarch/riscv/include/asm/riscv_tlb.h, as in v5.
The updated patch is available here:
https://lore.kernel.org/linux-riscv/[email protected]/T/#u
Quoted 4 lines
--
Kind regards,
Roman 'Hedin' Storozhenko
Signature · 2 lines
Kind regards, Roman 'Hedin' Storozhenko