Skip to content

Commit 1143042

Browse files
DanielTimLeeAlexei Starovoitov
authored andcommitted
samples/bpf: make tracing programs to be more CO-RE centric
The existing tracing programs have been developed for a considerable period of time and, as a result, do not properly incorporate the features of the current libbpf, such as CO-RE. This is evident in frequent usage of functions like PT_REGS* and the persistence of "hack" methods using underscore-style bpf_probe_read_kernel from the past. These programs are far behind the current level of libbpf and can potentially confuse users. Therefore, this commit aims to convert the outdated BPF programs to be more CO-RE centric. Signed-off-by: Daniel T. Lee <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 02dabc2 commit 1143042

File tree

4 files changed

+20
-40
lines changed

4 files changed

+20
-40
lines changed

samples/bpf/offwaketime.bpf.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,12 @@
88
#include <linux/version.h>
99
#include <bpf/bpf_helpers.h>
1010
#include <bpf/bpf_tracing.h>
11+
#include <bpf/bpf_core_read.h>
1112

1213
#ifndef PERF_MAX_STACK_DEPTH
1314
#define PERF_MAX_STACK_DEPTH 127
1415
#endif
1516

16-
#define _(P) \
17-
({ \
18-
typeof(P) val; \
19-
bpf_probe_read_kernel(&val, sizeof(val), &(P)); \
20-
val; \
21-
})
22-
2317
#define MINBLOCK_US 1
2418
#define MAX_ENTRIES 10000
2519

@@ -68,11 +62,9 @@ struct {
6862
SEC("kprobe/try_to_wake_up")
6963
int waker(struct pt_regs *ctx)
7064
{
71-
struct task_struct *p = (void *) PT_REGS_PARM1(ctx);
65+
struct task_struct *p = (void *)PT_REGS_PARM1_CORE(ctx);
66+
u32 pid = BPF_CORE_READ(p, pid);
7267
struct wokeby_t woke;
73-
u32 pid;
74-
75-
pid = _(p->pid);
7668

7769
bpf_get_current_comm(&woke.name, sizeof(woke.name));
7870
woke.ret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS);
@@ -121,9 +113,9 @@ int oncpu(struct trace_event_raw_sched_switch *ctx)
121113
SEC("kprobe.multi/finish_task_switch*")
122114
int oncpu(struct pt_regs *ctx)
123115
{
124-
struct task_struct *p = (void *) PT_REGS_PARM1(ctx);
116+
struct task_struct *p = (void *)PT_REGS_PARM1_CORE(ctx);
125117
/* record previous thread sleep time */
126-
u32 pid = _(p->pid);
118+
u32 pid = BPF_CORE_READ(p, pid);
127119
#endif
128120
u64 delta, ts, *tsp;
129121

samples/bpf/test_overhead_kprobe.bpf.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88
#include <linux/version.h>
99
#include <bpf/bpf_helpers.h>
1010
#include <bpf/bpf_tracing.h>
11-
12-
#define _(P) \
13-
({ \
14-
typeof(P) val = 0; \
15-
bpf_probe_read_kernel(&val, sizeof(val), &(P)); \
16-
val; \
17-
})
11+
#include <bpf/bpf_core_read.h>
1812

1913
SEC("kprobe/__set_task_comm")
2014
int prog(struct pt_regs *ctx)
@@ -26,14 +20,14 @@ int prog(struct pt_regs *ctx)
2620
u16 oom_score_adj;
2721
u32 pid;
2822

29-
tsk = (void *)PT_REGS_PARM1(ctx);
23+
tsk = (void *)PT_REGS_PARM1_CORE(ctx);
3024

31-
pid = _(tsk->pid);
32-
bpf_probe_read_kernel_str(oldcomm, sizeof(oldcomm), &tsk->comm);
33-
bpf_probe_read_kernel_str(newcomm, sizeof(newcomm),
25+
pid = BPF_CORE_READ(tsk, pid);
26+
bpf_core_read_str(oldcomm, sizeof(oldcomm), &tsk->comm);
27+
bpf_core_read_str(newcomm, sizeof(newcomm),
3428
(void *)PT_REGS_PARM2(ctx));
35-
signal = _(tsk->signal);
36-
oom_score_adj = _(signal->oom_score_adj);
29+
signal = BPF_CORE_READ(tsk, signal);
30+
oom_score_adj = BPF_CORE_READ(signal, oom_score_adj);
3731
return 0;
3832
}
3933

samples/bpf/tracex1.bpf.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@
88
#include "net_shared.h"
99
#include <linux/version.h>
1010
#include <bpf/bpf_helpers.h>
11+
#include <bpf/bpf_core_read.h>
1112
#include <bpf/bpf_tracing.h>
1213

13-
#define _(P) \
14-
({ \
15-
typeof(P) val = 0; \
16-
bpf_probe_read_kernel(&val, sizeof(val), &(P)); \
17-
val; \
18-
})
19-
2014
/* kprobe is NOT a stable ABI
2115
* kernel functions can be removed, renamed or completely change semantics.
2216
* Number of arguments and their positions can change, etc.
@@ -34,12 +28,11 @@ int bpf_prog1(struct pt_regs *ctx)
3428
struct sk_buff *skb;
3529
int len;
3630

37-
/* non-portable! works for the given kernel only */
38-
bpf_probe_read_kernel(&skb, sizeof(skb), (void *)PT_REGS_PARM1(ctx));
39-
dev = _(skb->dev);
40-
len = _(skb->len);
31+
bpf_core_read(&skb, sizeof(skb), (void *)PT_REGS_PARM1(ctx));
32+
dev = BPF_CORE_READ(skb, dev);
33+
len = BPF_CORE_READ(skb, len);
4134

42-
bpf_probe_read_kernel(devname, sizeof(devname), dev->name);
35+
BPF_CORE_READ_STR_INTO(&devname, dev, name);
4336

4437
if (devname[0] == 'l' && devname[1] == 'o') {
4538
char fmt[] = "skb %p len %d\n";

samples/bpf/tracex5.bpf.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <uapi/linux/unistd.h>
1111
#include <bpf/bpf_helpers.h>
1212
#include <bpf/bpf_tracing.h>
13+
#include <bpf/bpf_core_read.h>
1314

1415
#define __stringify(x) #x
1516
#define PROG(F) SEC("kprobe/"__stringify(F)) int bpf_func_##F
@@ -46,7 +47,7 @@ PROG(SYS__NR_write)(struct pt_regs *ctx)
4647
{
4748
struct seccomp_data sd;
4849

49-
bpf_probe_read_kernel(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
50+
bpf_core_read(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
5051
if (sd.args[2] == 512) {
5152
char fmt[] = "write(fd=%d, buf=%p, size=%d)\n";
5253
bpf_trace_printk(fmt, sizeof(fmt),
@@ -59,7 +60,7 @@ PROG(SYS__NR_read)(struct pt_regs *ctx)
5960
{
6061
struct seccomp_data sd;
6162

62-
bpf_probe_read_kernel(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
63+
bpf_core_read(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
6364
if (sd.args[2] > 128 && sd.args[2] <= 1024) {
6465
char fmt[] = "read(fd=%d, buf=%p, size=%d)\n";
6566
bpf_trace_printk(fmt, sizeof(fmt),

0 commit comments

Comments
 (0)