[PATCH v3 1/7] rtla/timerlat: Support tail call from BPF program

Tomas Glozar posted 7 patches 3 months, 2 weeks ago
[PATCH v3 1/7] rtla/timerlat: Support tail call from BPF program
Posted by Tomas Glozar 3 months, 2 weeks ago
Add a map to the rtla-timerlat BPF program that holds a file descriptor
of another BPF program, to be executed on threshold overflow.

timerlat_bpf_set_action() is added as an interface to set the program.

Signed-off-by: Tomas Glozar <tglozar@redhat.com>
---
 tools/tracing/rtla/src/timerlat.bpf.c | 23 ++++++++++++++++++++---
 tools/tracing/rtla/src/timerlat_bpf.c | 13 +++++++++++++
 tools/tracing/rtla/src/timerlat_bpf.h |  1 +
 3 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/tools/tracing/rtla/src/timerlat.bpf.c b/tools/tracing/rtla/src/timerlat.bpf.c
index 084cd10c21fc..19ccd9abf8d4 100644
--- a/tools/tracing/rtla/src/timerlat.bpf.c
+++ b/tools/tracing/rtla/src/timerlat.bpf.c
@@ -40,6 +40,17 @@ struct {
 	__uint(max_entries, 1);
 } signal_stop_tracing SEC(".maps");
 
+struct {
+	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+	__uint(key_size, sizeof(unsigned int));
+	__uint(max_entries, 1);
+	__array(values, unsigned int (void *));
+} bpf_action SEC(".maps") = {
+	.values = {
+		[0] = 0
+	},
+};
+
 /* Params to be set by rtla */
 const volatile int bucket_size = 1;
 const volatile int output_divisor = 1000;
@@ -109,7 +120,7 @@ nosubprog void update_summary(void *map,
 	map_set(map, SUMMARY_SUM, map_get(map, SUMMARY_SUM) + latency);
 }
 
-nosubprog void set_stop_tracing(void)
+nosubprog void set_stop_tracing(struct trace_event_raw_timerlat_sample *tp_args)
 {
 	int value = 0;
 
@@ -118,6 +129,12 @@ nosubprog void set_stop_tracing(void)
 
 	/* Signal to userspace */
 	bpf_ringbuf_output(&signal_stop_tracing, &value, sizeof(value), 0);
+
+	/*
+	 * Call into BPF action program, if attached.
+	 * Otherwise, just silently fail.
+	 */
+	bpf_tail_call(tp_args, &bpf_action, 0);
 }
 
 SEC("tp/osnoise/timerlat_sample")
@@ -138,13 +155,13 @@ int handle_timerlat_sample(struct trace_event_raw_timerlat_sample *tp_args)
 		update_summary(&summary_irq, latency, bucket);
 
 		if (irq_threshold != 0 && latency_us >= irq_threshold)
-			set_stop_tracing();
+			set_stop_tracing(tp_args);
 	} else if (tp_args->context == 1) {
 		update_main_hist(&hist_thread, bucket);
 		update_summary(&summary_thread, latency, bucket);
 
 		if (thread_threshold != 0 && latency_us >= thread_threshold)
-			set_stop_tracing();
+			set_stop_tracing(tp_args);
 	} else {
 		update_main_hist(&hist_user, bucket);
 		update_summary(&summary_user, latency, bucket);
diff --git a/tools/tracing/rtla/src/timerlat_bpf.c b/tools/tracing/rtla/src/timerlat_bpf.c
index e97d16646bcd..1d619e502c65 100644
--- a/tools/tracing/rtla/src/timerlat_bpf.c
+++ b/tools/tracing/rtla/src/timerlat_bpf.c
@@ -59,6 +59,19 @@ int timerlat_bpf_init(struct timerlat_params *params)
 	return 0;
 }
 
+/*
+ * timerlat_bpf_set_action - set action on threshold executed on BPF side
+ */
+static int timerlat_bpf_set_action(struct bpf_program *prog)
+{
+	unsigned int key = 0, value = bpf_program__fd(prog);
+
+	return bpf_map__update_elem(bpf->maps.bpf_action,
+				    &key, sizeof(key),
+				    &value, sizeof(value),
+				    BPF_ANY);
+}
+
 /*
  * timerlat_bpf_attach - attach BPF program to collect timerlat data
  */
diff --git a/tools/tracing/rtla/src/timerlat_bpf.h b/tools/tracing/rtla/src/timerlat_bpf.h
index 118487436d30..b5009092c7a3 100644
--- a/tools/tracing/rtla/src/timerlat_bpf.h
+++ b/tools/tracing/rtla/src/timerlat_bpf.h
@@ -12,6 +12,7 @@ enum summary_field {
 };
 
 #ifndef __bpf__
+#include <bpf/libbpf.h>
 #ifdef HAVE_BPF_SKEL
 int timerlat_bpf_init(struct timerlat_params *params);
 int timerlat_bpf_attach(void);
-- 
2.51.0
Re: [PATCH v3 1/7] rtla/timerlat: Support tail call from BPF program
Posted by Wander Lairson Costa 3 months, 1 week ago
On Mon, Oct 27, 2025 at 04:33:55PM +0100, Tomas Glozar wrote:
> Add a map to the rtla-timerlat BPF program that holds a file descriptor
> of another BPF program, to be executed on threshold overflow.
> 
> timerlat_bpf_set_action() is added as an interface to set the program.
> 
> Signed-off-by: Tomas Glozar <tglozar@redhat.com>
> ---
>  tools/tracing/rtla/src/timerlat.bpf.c | 23 ++++++++++++++++++++---
>  tools/tracing/rtla/src/timerlat_bpf.c | 13 +++++++++++++
>  tools/tracing/rtla/src/timerlat_bpf.h |  1 +
>  3 files changed, 34 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/tracing/rtla/src/timerlat.bpf.c b/tools/tracing/rtla/src/timerlat.bpf.c
> index 084cd10c21fc..19ccd9abf8d4 100644
> --- a/tools/tracing/rtla/src/timerlat.bpf.c
> +++ b/tools/tracing/rtla/src/timerlat.bpf.c
> @@ -40,6 +40,17 @@ struct {
>  	__uint(max_entries, 1);
>  } signal_stop_tracing SEC(".maps");
>  
> +struct {
> +	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
> +	__uint(key_size, sizeof(unsigned int));
> +	__uint(max_entries, 1);
> +	__array(values, unsigned int (void *));
> +} bpf_action SEC(".maps") = {
> +	.values = {
> +		[0] = 0
> +	},
> +};
> +
>  /* Params to be set by rtla */
>  const volatile int bucket_size = 1;
>  const volatile int output_divisor = 1000;
> @@ -109,7 +120,7 @@ nosubprog void update_summary(void *map,
>  	map_set(map, SUMMARY_SUM, map_get(map, SUMMARY_SUM) + latency);
>  }
>  
> -nosubprog void set_stop_tracing(void)
> +nosubprog void set_stop_tracing(struct trace_event_raw_timerlat_sample *tp_args)
>  {
>  	int value = 0;
>  
> @@ -118,6 +129,12 @@ nosubprog void set_stop_tracing(void)
>  
>  	/* Signal to userspace */
>  	bpf_ringbuf_output(&signal_stop_tracing, &value, sizeof(value), 0);
> +
> +	/*
> +	 * Call into BPF action program, if attached.
> +	 * Otherwise, just silently fail.
> +	 */
> +	bpf_tail_call(tp_args, &bpf_action, 0);
>  }
>  
>  SEC("tp/osnoise/timerlat_sample")
> @@ -138,13 +155,13 @@ int handle_timerlat_sample(struct trace_event_raw_timerlat_sample *tp_args)
>  		update_summary(&summary_irq, latency, bucket);
>  
>  		if (irq_threshold != 0 && latency_us >= irq_threshold)
> -			set_stop_tracing();
> +			set_stop_tracing(tp_args);
>  	} else if (tp_args->context == 1) {
>  		update_main_hist(&hist_thread, bucket);
>  		update_summary(&summary_thread, latency, bucket);
>  
>  		if (thread_threshold != 0 && latency_us >= thread_threshold)
> -			set_stop_tracing();
> +			set_stop_tracing(tp_args);
>  	} else {
>  		update_main_hist(&hist_user, bucket);
>  		update_summary(&summary_user, latency, bucket);
> diff --git a/tools/tracing/rtla/src/timerlat_bpf.c b/tools/tracing/rtla/src/timerlat_bpf.c
> index e97d16646bcd..1d619e502c65 100644
> --- a/tools/tracing/rtla/src/timerlat_bpf.c
> +++ b/tools/tracing/rtla/src/timerlat_bpf.c
> @@ -59,6 +59,19 @@ int timerlat_bpf_init(struct timerlat_params *params)
>  	return 0;
>  }
>  
> +/*
> + * timerlat_bpf_set_action - set action on threshold executed on BPF side
> + */
> +static int timerlat_bpf_set_action(struct bpf_program *prog)
> +{
> +	unsigned int key = 0, value = bpf_program__fd(prog);
> +
> +	return bpf_map__update_elem(bpf->maps.bpf_action,
> +				    &key, sizeof(key),
> +				    &value, sizeof(value),
> +				    BPF_ANY);
> +}
> +

I believe it makes more sense to add the definition of this function to
the patch where it is called.

>  /*
>   * timerlat_bpf_attach - attach BPF program to collect timerlat data
>   */
> diff --git a/tools/tracing/rtla/src/timerlat_bpf.h b/tools/tracing/rtla/src/timerlat_bpf.h
> index 118487436d30..b5009092c7a3 100644
> --- a/tools/tracing/rtla/src/timerlat_bpf.h
> +++ b/tools/tracing/rtla/src/timerlat_bpf.h
> @@ -12,6 +12,7 @@ enum summary_field {
>  };
>  
>  #ifndef __bpf__
> +#include <bpf/libbpf.h>
>  #ifdef HAVE_BPF_SKEL
>  int timerlat_bpf_init(struct timerlat_params *params);
>  int timerlat_bpf_attach(void);
> -- 
> 2.51.0
>
Re: [PATCH v3 1/7] rtla/timerlat: Support tail call from BPF program
Posted by Tomas Glozar 2 months, 2 weeks ago
po 3. 11. 2025 v 15:24 odesílatel Wander Lairson Costa
<wander@redhat.com> napsal:
>> +/*
>> + * timerlat_bpf_set_action - set action on threshold executed on BPF side
>> + */
>> +static int timerlat_bpf_set_action(struct bpf_program *prog)
>> +{
>> +     unsigned int key = 0, value = bpf_program__fd(prog);
>> +
>> +     return bpf_map__update_elem(bpf->maps.bpf_action,
>> +                                 &key, sizeof(key),
>> +                                 &value, sizeof(value),
>> +                                 BPF_ANY);
>> +}
>> +
>
> I believe it makes more sense to add the definition of this function to
> the patch where it is called.
>

Maybe, but I wrote this patch first, the rest of the patchset some
time after that. That is why it is this way; either way, it doesn't
hurt to define something before using it, the other way is worse.

Tomas