Skip to content

Commit 402b407

Browse files
Florent Revestkernel-patches-bot
authored andcommitted
bpf: Expose a bpf_sock_from_file helper to tracing programs
eBPF programs can already check whether a file is a socket using file->f_op == &socket_file_ops but they can not convert file->private_data into a struct socket with BTF information. For that, we need a new helper that is essentially just a wrapper for sock_from_file. sock_from_file can set an err value but this is only set to -ENOTSOCK when the return value is NULL so it's useless superfluous information. Signed-off-by: Florent Revest <[email protected]>
1 parent 6f5c12c commit 402b407

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

include/uapi/linux/bpf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,6 +3787,12 @@ union bpf_attr {
37873787
* *ARG_PTR_TO_BTF_ID* of type *task_struct*.
37883788
* Return
37893789
* Pointer to the current task.
3790+
*
3791+
* struct socket *bpf_sock_from_file(struct file *file)
3792+
* Description
3793+
* If the given file contains a socket, returns the associated socket.
3794+
* Return
3795+
* A pointer to a struct socket on success, or NULL on failure.
37903796
*/
37913797
#define __BPF_FUNC_MAPPER(FN) \
37923798
FN(unspec), \
@@ -3948,6 +3954,7 @@ union bpf_attr {
39483954
FN(task_storage_get), \
39493955
FN(task_storage_delete), \
39503956
FN(get_current_task_btf), \
3957+
FN(sock_from_file), \
39513958
/* */
39523959

39533960
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

kernel/trace/bpf_trace.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,26 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = {
12531253
.arg5_type = ARG_ANYTHING,
12541254
};
12551255

1256+
BPF_CALL_1(bpf_sock_from_file, struct file *, file)
1257+
{
1258+
int err;
1259+
1260+
return (unsigned long) sock_from_file(file, &err);
1261+
}
1262+
1263+
BTF_ID_LIST(bpf_sock_from_file_btf_ids)
1264+
BTF_ID(struct, socket)
1265+
BTF_ID(struct, file)
1266+
1267+
const struct bpf_func_proto bpf_sock_from_file_proto = {
1268+
.func = bpf_sock_from_file,
1269+
.gpl_only = true,
1270+
.ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
1271+
.ret_btf_id = &bpf_sock_from_file_btf_ids[0],
1272+
.arg1_type = ARG_PTR_TO_BTF_ID,
1273+
.arg1_btf_id = &bpf_sock_from_file_btf_ids[1],
1274+
};
1275+
12561276
const struct bpf_func_proto *
12571277
bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
12581278
{
@@ -1347,6 +1367,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
13471367
return &bpf_per_cpu_ptr_proto;
13481368
case BPF_FUNC_bpf_this_cpu_ptr:
13491369
return &bpf_this_cpu_ptr_proto;
1370+
case BPF_FUNC_sock_from_file:
1371+
return &bpf_sock_from_file_proto;
13501372
default:
13511373
return NULL;
13521374
}

scripts/bpf_helpers_doc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ class PrinterHelpers(Printer):
434434
'struct xdp_md',
435435
'struct path',
436436
'struct btf_ptr',
437+
'struct socket',
438+
'struct file',
437439
]
438440
known_types = {
439441
'...',
@@ -477,6 +479,8 @@ class PrinterHelpers(Printer):
477479
'struct task_struct',
478480
'struct path',
479481
'struct btf_ptr',
482+
'struct socket',
483+
'struct file',
480484
}
481485
mapped_types = {
482486
'u8': '__u8',

tools/include/uapi/linux/bpf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,6 +3787,12 @@ union bpf_attr {
37873787
* *ARG_PTR_TO_BTF_ID* of type *task_struct*.
37883788
* Return
37893789
* Pointer to the current task.
3790+
*
3791+
* struct socket *bpf_sock_from_file(struct file *file)
3792+
* Description
3793+
* If the given file contains a socket, returns the associated socket.
3794+
* Return
3795+
* A pointer to a struct socket on success, or NULL on failure.
37903796
*/
37913797
#define __BPF_FUNC_MAPPER(FN) \
37923798
FN(unspec), \
@@ -3948,6 +3954,7 @@ union bpf_attr {
39483954
FN(task_storage_get), \
39493955
FN(task_storage_delete), \
39503956
FN(get_current_task_btf), \
3957+
FN(sock_from_file), \
39513958
/* */
39523959

39533960
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)