Skip to content

Commit d9847d3

Browse files
Alexei StarovoitovIngo Molnar
Alexei Starovoitov
authored and
Ingo Molnar
committed
tracing: Allow BPF programs to call bpf_ktime_get_ns()
bpf_ktime_get_ns() is used by programs to compute time delta between events or as a timestamp Signed-off-by: Alexei Starovoitov <[email protected]> Reviewed-by: Steven Rostedt <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: David S. Miller <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 2541517 commit d9847d3

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

include/uapi/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ enum bpf_func_id {
165165
BPF_FUNC_map_update_elem, /* int map_update_elem(&map, &key, &value, flags) */
166166
BPF_FUNC_map_delete_elem, /* int map_delete_elem(&map, &key) */
167167
BPF_FUNC_probe_read, /* int bpf_probe_read(void *dst, int size, void *src) */
168+
BPF_FUNC_ktime_get_ns, /* u64 bpf_ktime_get_ns(void) */
168169
__BPF_FUNC_MAX_ID,
169170
};
170171

kernel/trace/bpf_trace.c

+14
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ static const struct bpf_func_proto bpf_probe_read_proto = {
7878
.arg3_type = ARG_ANYTHING,
7979
};
8080

81+
static u64 bpf_ktime_get_ns(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
82+
{
83+
/* NMI safe access to clock monotonic */
84+
return ktime_get_mono_fast_ns();
85+
}
86+
87+
static const struct bpf_func_proto bpf_ktime_get_ns_proto = {
88+
.func = bpf_ktime_get_ns,
89+
.gpl_only = true,
90+
.ret_type = RET_INTEGER,
91+
};
92+
8193
static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
8294
{
8395
switch (func_id) {
@@ -89,6 +101,8 @@ static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func
89101
return &bpf_map_delete_elem_proto;
90102
case BPF_FUNC_probe_read:
91103
return &bpf_probe_read_proto;
104+
case BPF_FUNC_ktime_get_ns:
105+
return &bpf_ktime_get_ns_proto;
92106
default:
93107
return NULL;
94108
}

0 commit comments

Comments
 (0)