Skip to content

Commit 606274c

Browse files
4astdavem330
authored andcommitted
bpf: introduce bpf_get_current_task() helper
over time there were multiple requests to access different data structures and fields of task_struct current, so finally add the helper to access 'current' as-is. Tracing bpf programs will do the rest of walking the pointers via bpf_probe_read(). Note that current can be null and bpf program has to deal it with, but even dumb passing null into bpf_probe_read() is still safe. Suggested-by: Brendan Gregg <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent d390238 commit 606274c

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

include/uapi/linux/bpf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,13 @@ enum bpf_func_id {
357357
*/
358358
BPF_FUNC_get_hash_recalc,
359359

360+
/**
361+
* u64 bpf_get_current_task(void)
362+
* Returns current task_struct
363+
* Return: current
364+
*/
365+
BPF_FUNC_get_current_task,
366+
360367
__BPF_FUNC_MAX_ID,
361368
};
362369

kernel/trace/bpf_trace.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,17 @@ const struct bpf_func_proto *bpf_get_event_output_proto(void)
312312
return &bpf_event_output_proto;
313313
}
314314

315+
static u64 bpf_get_current_task(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
316+
{
317+
return (long) current;
318+
}
319+
320+
static const struct bpf_func_proto bpf_get_current_task_proto = {
321+
.func = bpf_get_current_task,
322+
.gpl_only = true,
323+
.ret_type = RET_INTEGER,
324+
};
325+
315326
static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
316327
{
317328
switch (func_id) {
@@ -329,6 +340,8 @@ static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
329340
return &bpf_tail_call_proto;
330341
case BPF_FUNC_get_current_pid_tgid:
331342
return &bpf_get_current_pid_tgid_proto;
343+
case BPF_FUNC_get_current_task:
344+
return &bpf_get_current_task_proto;
332345
case BPF_FUNC_get_current_uid_gid:
333346
return &bpf_get_current_uid_gid_proto;
334347
case BPF_FUNC_get_current_comm:

0 commit comments

Comments
 (0)