Skip to content

Commit 3ca1032

Browse files
sinkapAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Implement get_current_task_btf and RET_PTR_TO_BTF_ID
The currently available bpf_get_current_task returns an unsigned integer which can be used along with BPF_CORE_READ to read data from the task_struct but still cannot be used as an input argument to a helper that accepts an ARG_PTR_TO_BTF_ID of type task_struct. In order to implement this helper a new return type, RET_PTR_TO_BTF_ID, is added. This is similar to RET_PTR_TO_BTF_ID_OR_NULL but does not require checking the nullness of returned pointer. Signed-off-by: KP Singh <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Song Liu <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 864ab06 commit 3ca1032

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

include/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ enum bpf_return_type {
310310
RET_PTR_TO_BTF_ID_OR_NULL, /* returns a pointer to a btf_id or NULL */
311311
RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL, /* returns a pointer to a valid memory or a btf_id or NULL */
312312
RET_PTR_TO_MEM_OR_BTF_ID, /* returns a pointer to a valid memory or a btf_id */
313+
RET_PTR_TO_BTF_ID, /* returns a pointer to a btf_id */
313314
};
314315

315316
/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs

include/uapi/linux/bpf.h

+9
Original file line numberDiff line numberDiff line change
@@ -3779,6 +3779,14 @@ union bpf_attr {
37793779
* 0 on success.
37803780
*
37813781
* **-ENOENT** if the bpf_local_storage cannot be found.
3782+
*
3783+
* struct task_struct *bpf_get_current_task_btf(void)
3784+
* Description
3785+
* Return a BTF pointer to the "current" task.
3786+
* This pointer can also be used in helpers that accept an
3787+
* *ARG_PTR_TO_BTF_ID* of type *task_struct*.
3788+
* Return
3789+
* Pointer to the current task.
37823790
*/
37833791
#define __BPF_FUNC_MAPPER(FN) \
37843792
FN(unspec), \
@@ -3939,6 +3947,7 @@ union bpf_attr {
39393947
FN(redirect_peer), \
39403948
FN(task_storage_get), \
39413949
FN(task_storage_delete), \
3950+
FN(get_current_task_btf), \
39423951
/* */
39433952

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

kernel/bpf/verifier.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -5186,11 +5186,14 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn
51865186
PTR_TO_BTF_ID : PTR_TO_BTF_ID_OR_NULL;
51875187
regs[BPF_REG_0].btf_id = meta.ret_btf_id;
51885188
}
5189-
} else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL) {
5189+
} else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL ||
5190+
fn->ret_type == RET_PTR_TO_BTF_ID) {
51905191
int ret_btf_id;
51915192

51925193
mark_reg_known_zero(env, regs, BPF_REG_0);
5193-
regs[BPF_REG_0].type = PTR_TO_BTF_ID_OR_NULL;
5194+
regs[BPF_REG_0].type = fn->ret_type == RET_PTR_TO_BTF_ID ?
5195+
PTR_TO_BTF_ID :
5196+
PTR_TO_BTF_ID_OR_NULL;
51945197
ret_btf_id = *fn->ret_btf_id;
51955198
if (ret_btf_id == 0) {
51965199
verbose(env, "invalid return type %d of func %s#%d\n",

kernel/trace/bpf_trace.c

+16
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,20 @@ const struct bpf_func_proto bpf_get_current_task_proto = {
10221022
.ret_type = RET_INTEGER,
10231023
};
10241024

1025+
BPF_CALL_0(bpf_get_current_task_btf)
1026+
{
1027+
return (unsigned long) current;
1028+
}
1029+
1030+
BTF_ID_LIST_SINGLE(bpf_get_current_btf_ids, struct, task_struct)
1031+
1032+
static const struct bpf_func_proto bpf_get_current_task_btf_proto = {
1033+
.func = bpf_get_current_task_btf,
1034+
.gpl_only = true,
1035+
.ret_type = RET_PTR_TO_BTF_ID,
1036+
.ret_btf_id = &bpf_get_current_btf_ids[0],
1037+
};
1038+
10251039
BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
10261040
{
10271041
struct bpf_array *array = container_of(map, struct bpf_array, map);
@@ -1265,6 +1279,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
12651279
return &bpf_get_current_pid_tgid_proto;
12661280
case BPF_FUNC_get_current_task:
12671281
return &bpf_get_current_task_proto;
1282+
case BPF_FUNC_get_current_task_btf:
1283+
return &bpf_get_current_task_btf_proto;
12681284
case BPF_FUNC_get_current_uid_gid:
12691285
return &bpf_get_current_uid_gid_proto;
12701286
case BPF_FUNC_get_current_comm:

tools/include/uapi/linux/bpf.h

+9
Original file line numberDiff line numberDiff line change
@@ -3779,6 +3779,14 @@ union bpf_attr {
37793779
* 0 on success.
37803780
*
37813781
* **-ENOENT** if the bpf_local_storage cannot be found.
3782+
*
3783+
* struct task_struct *bpf_get_current_task_btf(void)
3784+
* Description
3785+
* Return a BTF pointer to the "current" task.
3786+
* This pointer can also be used in helpers that accept an
3787+
* *ARG_PTR_TO_BTF_ID* of type *task_struct*.
3788+
* Return
3789+
* Pointer to the current task.
37823790
*/
37833791
#define __BPF_FUNC_MAPPER(FN) \
37843792
FN(unspec), \
@@ -3939,6 +3947,7 @@ union bpf_attr {
39393947
FN(redirect_peer), \
39403948
FN(task_storage_get), \
39413949
FN(task_storage_delete), \
3950+
FN(get_current_task_btf), \
39423951
/* */
39433952

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

0 commit comments

Comments
 (0)