Skip to content

Commit 6e22ab9

Browse files
olsajiriAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Add d_path helper
Adding d_path helper function that returns full path for given 'struct path' object, which needs to be the kernel BTF 'path' object. The path is returned in buffer provided 'buf' of size 'sz' and is zero terminated. bpf_d_path(&file->f_path, buf, size); The helper calls directly d_path function, so there's only limited set of function it can be called from. Adding just very modest set for the start. Updating also bpf.h tools uapi header and adding 'path' to bpf_helpers_doc.py script. Signed-off-by: Jiri Olsa <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Acked-by: KP Singh <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent eae2e83 commit 6e22ab9

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

Diff for: include/uapi/linux/bpf.h

+14
Original file line numberDiff line numberDiff line change
@@ -3513,6 +3513,7 @@ union bpf_attr {
35133513
*
35143514
* **-EPERM** This helper cannot be used under the
35153515
* current sock_ops->op.
3516+
*
35163517
* void *bpf_inode_storage_get(struct bpf_map *map, void *inode, void *value, u64 flags)
35173518
* Description
35183519
* Get a bpf_local_storage from an *inode*.
@@ -3548,6 +3549,18 @@ union bpf_attr {
35483549
* 0 on success.
35493550
*
35503551
* **-ENOENT** if the bpf_local_storage cannot be found.
3552+
*
3553+
* long bpf_d_path(struct path *path, char *buf, u32 sz)
3554+
* Description
3555+
* Return full path for given 'struct path' object, which
3556+
* needs to be the kernel BTF 'path' object. The path is
3557+
* returned in the provided buffer 'buf' of size 'sz' and
3558+
* is zero terminated.
3559+
*
3560+
* Return
3561+
* On success, the strictly positive length of the string,
3562+
* including the trailing NUL character. On error, a negative
3563+
* value.
35513564
*/
35523565
#define __BPF_FUNC_MAPPER(FN) \
35533566
FN(unspec), \
@@ -3697,6 +3710,7 @@ union bpf_attr {
36973710
FN(reserve_hdr_opt), \
36983711
FN(inode_storage_get), \
36993712
FN(inode_storage_delete), \
3713+
FN(d_path), \
37003714
/* */
37013715

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

Diff for: kernel/trace/bpf_trace.c

+48
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,52 @@ static const struct bpf_func_proto bpf_send_signal_thread_proto = {
10981098
.arg1_type = ARG_ANYTHING,
10991099
};
11001100

1101+
BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
1102+
{
1103+
long len;
1104+
char *p;
1105+
1106+
if (!sz)
1107+
return 0;
1108+
1109+
p = d_path(path, buf, sz);
1110+
if (IS_ERR(p)) {
1111+
len = PTR_ERR(p);
1112+
} else {
1113+
len = buf + sz - p;
1114+
memmove(buf, p, len);
1115+
}
1116+
1117+
return len;
1118+
}
1119+
1120+
BTF_SET_START(btf_allowlist_d_path)
1121+
BTF_ID(func, vfs_truncate)
1122+
BTF_ID(func, vfs_fallocate)
1123+
BTF_ID(func, dentry_open)
1124+
BTF_ID(func, vfs_getattr)
1125+
BTF_ID(func, filp_close)
1126+
BTF_SET_END(btf_allowlist_d_path)
1127+
1128+
static bool bpf_d_path_allowed(const struct bpf_prog *prog)
1129+
{
1130+
return btf_id_set_contains(&btf_allowlist_d_path, prog->aux->attach_btf_id);
1131+
}
1132+
1133+
BTF_ID_LIST(bpf_d_path_btf_ids)
1134+
BTF_ID(struct, path)
1135+
1136+
static const struct bpf_func_proto bpf_d_path_proto = {
1137+
.func = bpf_d_path,
1138+
.gpl_only = false,
1139+
.ret_type = RET_INTEGER,
1140+
.arg1_type = ARG_PTR_TO_BTF_ID,
1141+
.arg2_type = ARG_PTR_TO_MEM,
1142+
.arg3_type = ARG_CONST_SIZE_OR_ZERO,
1143+
.btf_id = bpf_d_path_btf_ids,
1144+
.allowed = bpf_d_path_allowed,
1145+
};
1146+
11011147
const struct bpf_func_proto *
11021148
bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
11031149
{
@@ -1579,6 +1625,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
15791625
return prog->expected_attach_type == BPF_TRACE_ITER ?
15801626
&bpf_seq_write_proto :
15811627
NULL;
1628+
case BPF_FUNC_d_path:
1629+
return &bpf_d_path_proto;
15821630
default:
15831631
return raw_tp_prog_func_proto(func_id, prog);
15841632
}

Diff for: scripts/bpf_helpers_doc.py

+2
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ class PrinterHelpers(Printer):
432432
'struct __sk_buff',
433433
'struct sk_msg_md',
434434
'struct xdp_md',
435+
'struct path',
435436
]
436437
known_types = {
437438
'...',
@@ -472,6 +473,7 @@ class PrinterHelpers(Printer):
472473
'struct tcp_request_sock',
473474
'struct udp6_sock',
474475
'struct task_struct',
476+
'struct path',
475477
}
476478
mapped_types = {
477479
'u8': '__u8',

Diff for: tools/include/uapi/linux/bpf.h

+14
Original file line numberDiff line numberDiff line change
@@ -3513,6 +3513,7 @@ union bpf_attr {
35133513
*
35143514
* **-EPERM** This helper cannot be used under the
35153515
* current sock_ops->op.
3516+
*
35163517
* void *bpf_inode_storage_get(struct bpf_map *map, void *inode, void *value, u64 flags)
35173518
* Description
35183519
* Get a bpf_local_storage from an *inode*.
@@ -3548,6 +3549,18 @@ union bpf_attr {
35483549
* 0 on success.
35493550
*
35503551
* **-ENOENT** if the bpf_local_storage cannot be found.
3552+
*
3553+
* long bpf_d_path(struct path *path, char *buf, u32 sz)
3554+
* Description
3555+
* Return full path for given 'struct path' object, which
3556+
* needs to be the kernel BTF 'path' object. The path is
3557+
* returned in the provided buffer 'buf' of size 'sz' and
3558+
* is zero terminated.
3559+
*
3560+
* Return
3561+
* On success, the strictly positive length of the string,
3562+
* including the trailing NUL character. On error, a negative
3563+
* value.
35513564
*/
35523565
#define __BPF_FUNC_MAPPER(FN) \
35533566
FN(unspec), \
@@ -3697,6 +3710,7 @@ union bpf_attr {
36973710
FN(reserve_hdr_opt), \
36983711
FN(inode_storage_get), \
36993712
FN(inode_storage_delete), \
3713+
FN(d_path), \
37003714
/* */
37013715

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

0 commit comments

Comments
 (0)