Skip to content

Commit 8cd0f08

Browse files
rtla/timerlat: Support tail call from BPF program
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. Link: https://lore.kernel.org/r/20251126144205.331954-2-tglozar@redhat.com Signed-off-by: Tomas Glozar <tglozar@redhat.com>
1 parent a08e012 commit 8cd0f08

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

tools/tracing/rtla/src/timerlat.bpf.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ struct {
4040
__uint(max_entries, 1);
4141
} signal_stop_tracing SEC(".maps");
4242

43+
struct {
44+
__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
45+
__uint(key_size, sizeof(unsigned int));
46+
__uint(max_entries, 1);
47+
__array(values, unsigned int (void *));
48+
} bpf_action SEC(".maps") = {
49+
.values = {
50+
[0] = 0
51+
},
52+
};
53+
4354
/* Params to be set by rtla */
4455
const volatile int bucket_size = 1;
4556
const volatile int output_divisor = 1000;
@@ -109,7 +120,7 @@ nosubprog void update_summary(void *map,
109120
map_set(map, SUMMARY_SUM, map_get(map, SUMMARY_SUM) + latency);
110121
}
111122

112-
nosubprog void set_stop_tracing(void)
123+
nosubprog void set_stop_tracing(struct trace_event_raw_timerlat_sample *tp_args)
113124
{
114125
int value = 0;
115126

@@ -118,6 +129,12 @@ nosubprog void set_stop_tracing(void)
118129

119130
/* Signal to userspace */
120131
bpf_ringbuf_output(&signal_stop_tracing, &value, sizeof(value), 0);
132+
133+
/*
134+
* Call into BPF action program, if attached.
135+
* Otherwise, just silently fail.
136+
*/
137+
bpf_tail_call(tp_args, &bpf_action, 0);
121138
}
122139

123140
SEC("tp/osnoise/timerlat_sample")
@@ -138,19 +155,19 @@ int handle_timerlat_sample(struct trace_event_raw_timerlat_sample *tp_args)
138155
update_summary(&summary_irq, latency, bucket);
139156

140157
if (irq_threshold != 0 && latency_us >= irq_threshold)
141-
set_stop_tracing();
158+
set_stop_tracing(tp_args);
142159
} else if (tp_args->context == 1) {
143160
update_main_hist(&hist_thread, bucket);
144161
update_summary(&summary_thread, latency, bucket);
145162

146163
if (thread_threshold != 0 && latency_us >= thread_threshold)
147-
set_stop_tracing();
164+
set_stop_tracing(tp_args);
148165
} else {
149166
update_main_hist(&hist_user, bucket);
150167
update_summary(&summary_user, latency, bucket);
151168

152169
if (thread_threshold != 0 && latency_us >= thread_threshold)
153-
set_stop_tracing();
170+
set_stop_tracing(tp_args);
154171
}
155172

156173
return 0;

tools/tracing/rtla/src/timerlat_bpf.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ int timerlat_bpf_init(struct timerlat_params *params)
5959
return 0;
6060
}
6161

62+
/*
63+
* timerlat_bpf_set_action - set action on threshold executed on BPF side
64+
*/
65+
static int timerlat_bpf_set_action(struct bpf_program *prog)
66+
{
67+
unsigned int key = 0, value = bpf_program__fd(prog);
68+
69+
return bpf_map__update_elem(bpf->maps.bpf_action,
70+
&key, sizeof(key),
71+
&value, sizeof(value),
72+
BPF_ANY);
73+
}
74+
6275
/*
6376
* timerlat_bpf_attach - attach BPF program to collect timerlat data
6477
*/

tools/tracing/rtla/src/timerlat_bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ enum summary_field {
1212
};
1313

1414
#ifndef __bpf__
15+
#include <bpf/libbpf.h>
1516
#ifdef HAVE_BPF_SKEL
1617
int timerlat_bpf_init(struct timerlat_params *params);
1718
int timerlat_bpf_attach(void);

0 commit comments

Comments
 (0)