mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: "Paul E. McKenney" <paulmck@kernel.org>, rcu@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, kernel-team@meta.com,
	rostedt@goodmis.org,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	bpf@vger.kernel.org
Subject: Re: [PATCH v4 4/6] tracing: Guard __DECLARE_TRACE() use of __DO_TRACE_CALL() with SRCU-fast
Date: Wed, 23 Jul 2025 17:40:20 -0400	[thread overview]
Message-ID: <020d22f0-a95b-4204-a611-eb3953c33f32@efficios.com> (raw)
In-Reply-To: <20250723202800.2094614-4-paulmck@kernel.org>

On 2025-07-23 16:27, Paul E. McKenney wrote:
> The current use of guard(preempt_notrace)() within __DECLARE_TRACE()
> to protect invocation of __DO_TRACE_CALL() means that BPF programs
> attached to tracepoints are non-preemptible.  This is unhelpful in
> real-time systems, whose users apparently wish to use BPF while also
> achieving low latencies.  (Who knew?)
> 
> One option would be to use preemptible RCU, but this introduces
> many opportunities for infinite recursion, which many consider to
> be counterproductive, especially given the relatively small stacks
> provided by the Linux kernel.  These opportunities could be shut down
> by sufficiently energetic duplication of code, but this sort of thing
> is considered impolite in some circles.
> 
> Therefore, use the shiny new SRCU-fast API, which provides somewhat faster
> readers than those of preemptible RCU, at least on my laptop, where
> task_struct access is more expensive than access to per-CPU variables.
> And SRCU fast provides way faster readers than does SRCU, courtesy of
> being able to avoid the read-side use of smp_mb().  Also, it is quite
> straightforward to create srcu_read_{,un}lock_fast_notrace() functions.

As-is this will break the tracer callbacks, because some tracers expect
the tracepoint callback to be called with preemption-off for various
reasons, including preventing migration.

We'd need to add preempt off guards in the tracer callbacks that require
it initially before doing this change.

I've done something similar for the syscall tracepoints when introducing
faultable syscall tracepoints:

4aadde89d81f tracing/bpf: disable preemption in syscall probe
65e7462a16ce tracing/perf: disable preemption in syscall probe
13d750c2c03e tracing/ftrace: disable preemption in syscall probe

Thanks,

Mathieu

> 
> While in the area, SRCU now supports early boot call_srcu().  Therefore,
> remove the checks that used to avoid such use from rcu_free_old_probes()
> before this commit was applied:
> 
> e53244e2c893 ("tracepoint: Remove SRCU protection")
> 
> The current commit can be thought of as an approximate revert of that
> commit.
> 
> Link: https://lore.kernel.org/all/20250613152218.1924093-1-bigeasy@linutronix.de/
> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> Cc: Steven Rostedt <rostedt@goodmis.org>
> Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> Cc: <bpf@vger.kernel.org>
> ---
>   include/linux/tracepoint.h |  6 ++++--
>   kernel/tracepoint.c        | 21 ++++++++++++++++++++-
>   2 files changed, 24 insertions(+), 3 deletions(-)
> 
> diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h
> index 826ce3f8e1f85..a22c1ab88560b 100644
> --- a/include/linux/tracepoint.h
> +++ b/include/linux/tracepoint.h
> @@ -33,6 +33,8 @@ struct trace_eval_map {
>   
>   #define TRACEPOINT_DEFAULT_PRIO	10
>   
> +extern struct srcu_struct tracepoint_srcu;
> +
>   extern int
>   tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data);
>   extern int
> @@ -115,7 +117,7 @@ void for_each_tracepoint_in_module(struct module *mod,
>   static inline void tracepoint_synchronize_unregister(void)
>   {
>   	synchronize_rcu_tasks_trace();
> -	synchronize_rcu();
> +	synchronize_srcu(&tracepoint_srcu);
>   }
>   static inline bool tracepoint_is_faultable(struct tracepoint *tp)
>   {
> @@ -271,7 +273,7 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
>   	static inline void __do_trace_##name(proto)			\
>   	{								\
>   		if (cond) {						\
> -			guard(preempt_notrace)();			\
> +			guard(srcu_fast_notrace)(&tracepoint_srcu);	\
>   			__DO_TRACE_CALL(name, TP_ARGS(args));		\
>   		}							\
>   	}								\
> diff --git a/kernel/tracepoint.c b/kernel/tracepoint.c
> index 62719d2941c90..e19973015cbd7 100644
> --- a/kernel/tracepoint.c
> +++ b/kernel/tracepoint.c
> @@ -25,6 +25,9 @@ enum tp_func_state {
>   extern tracepoint_ptr_t __start___tracepoints_ptrs[];
>   extern tracepoint_ptr_t __stop___tracepoints_ptrs[];
>   
> +DEFINE_SRCU(tracepoint_srcu);
> +EXPORT_SYMBOL_GPL(tracepoint_srcu);
> +
>   enum tp_transition_sync {
>   	TP_TRANSITION_SYNC_1_0_1,
>   	TP_TRANSITION_SYNC_N_2_1,
> @@ -34,6 +37,7 @@ enum tp_transition_sync {
>   
>   struct tp_transition_snapshot {
>   	unsigned long rcu;
> +	unsigned long srcu_gp;
>   	bool ongoing;
>   };
>   
> @@ -46,6 +50,7 @@ static void tp_rcu_get_state(enum tp_transition_sync sync)
>   
>   	/* Keep the latest get_state snapshot. */
>   	snapshot->rcu = get_state_synchronize_rcu();
> +	snapshot->srcu_gp = start_poll_synchronize_srcu(&tracepoint_srcu);
>   	snapshot->ongoing = true;
>   }
>   
> @@ -56,6 +61,8 @@ static void tp_rcu_cond_sync(enum tp_transition_sync sync)
>   	if (!snapshot->ongoing)
>   		return;
>   	cond_synchronize_rcu(snapshot->rcu);
> +	if (!poll_state_synchronize_srcu(&tracepoint_srcu, snapshot->srcu_gp))
> +		synchronize_srcu(&tracepoint_srcu);
>   	snapshot->ongoing = false;
>   }
>   
> @@ -101,17 +108,29 @@ static inline void *allocate_probes(int count)
>   	return p == NULL ? NULL : p->probes;
>   }
>   
> -static void rcu_free_old_probes(struct rcu_head *head)
> +static void srcu_free_old_probes(struct rcu_head *head)
>   {
>   	kfree(container_of(head, struct tp_probes, rcu));
>   }
>   
> +static void rcu_free_old_probes(struct rcu_head *head)
> +{
> +	call_srcu(&tracepoint_srcu, head, srcu_free_old_probes);
> +}
> +
>   static inline void release_probes(struct tracepoint *tp, struct tracepoint_func *old)
>   {
>   	if (old) {
>   		struct tp_probes *tp_probes = container_of(old,
>   			struct tp_probes, probes[0]);
>   
> +		/*
> +		 * Tracepoint probes are protected by either RCU or
> +		 * Tasks Trace RCU and also by SRCU.  By calling the SRCU
> +		 * callback in the [Tasks Trace] RCU callback we cover
> +		 * both cases. So let us chain the SRCU and [Tasks Trace]
> +		 * RCU callbacks to wait for both grace periods.
> +		 */
>   		if (tracepoint_is_faultable(tp))
>   			call_rcu_tasks_trace(&tp_probes->rcu, rcu_free_old_probes);
>   		else


-- 
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com

  reply	other threads:[~2025-07-23 21:40 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-23 20:27 [PATCH 0/6] Switch __DECLARE_TRACE() to new notrace variant of SRCU-fast Paul E. McKenney
2025-07-23 20:27 ` [PATCH v4 1/6] srcu: Move rcu_is_watching() checks to srcu_read_{,un}lock_fast() Paul E. McKenney
2025-07-23 20:27 ` [PATCH v4 2/6] srcu: Add srcu_read_lock_fast_notrace() and srcu_read_unlock_fast_notrace() Paul E. McKenney
2025-07-23 20:50   ` Boqun Feng
2025-07-23 21:21     ` Paul E. McKenney
2025-07-23 20:27 ` [PATCH v4 3/6] srcu: Add guards for notrace variants of SRCU-fast readers Paul E. McKenney
2025-07-23 20:27 ` [PATCH v4 4/6] tracing: Guard __DECLARE_TRACE() use of __DO_TRACE_CALL() with SRCU-fast Paul E. McKenney
2025-07-23 21:40   ` Mathieu Desnoyers [this message]
2025-07-23 22:17     ` Paul E. McKenney
2025-07-23 22:29       ` Steven Rostedt
2025-07-23 22:51         ` Paul E. McKenney
2025-07-23 20:27 ` [PATCH v4 5/6] srcu: Document __srcu_read_{,un}lock_fast() implicit RCU readers Paul E. McKenney
2025-07-23 20:28 ` [PATCH v4 6/6] srcu: Document srcu_flip() memory-barrier D relation to SRCU-fast Paul E. McKenney
2025-07-23 20:34 ` [PATCH 0/6] Switch __DECLARE_TRACE() to new notrace variant of SRCU-fast Steven Rostedt
2025-07-23 20:54   ` Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=020d22f0-a95b-4204-a611-eb3953c33f32@efficios.com \
    --to=mathieu.desnoyers@efficios.com \
    --cc=bigeasy@linutronix.de \
    --cc=bpf@vger.kernel.org \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paulmck@kernel.org \
    --cc=rcu@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®