Skip to content

Commit e75ef2f

Browse files
kknjhKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
bpf: Allow some trace helpers for all prog types
if it works under NMI and doesn't use any context-dependent things, should be fine for any program type. The detailed discussion is in [1]. [1] https://lore.kernel.org/all/CAEf4Bza6gK3dsrTosk6k3oZgtHesNDSrDd8sdeQ-GiS6oJixQg@mail.gmail.com/ Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: Feng Yang <[email protected]>
1 parent c7849f3 commit e75ef2f

File tree

4 files changed

+56
-55
lines changed

4 files changed

+56
-55
lines changed

kernel/bpf/cgroup.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,16 +2607,10 @@ const struct bpf_func_proto *
26072607
cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
26082608
{
26092609
switch (func_id) {
2610-
case BPF_FUNC_get_current_uid_gid:
2611-
return &bpf_get_current_uid_gid_proto;
2612-
case BPF_FUNC_get_current_comm:
2613-
return &bpf_get_current_comm_proto;
26142610
#ifdef CONFIG_CGROUP_NET_CLASSID
26152611
case BPF_FUNC_get_cgroup_classid:
26162612
return &bpf_get_cgroup_classid_curr_proto;
26172613
#endif
2618-
case BPF_FUNC_current_task_under_cgroup:
2619-
return &bpf_current_task_under_cgroup_proto;
26202614
default:
26212615
return NULL;
26222616
}

kernel/bpf/helpers.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/btf_ids.h>
2424
#include <linux/bpf_mem_alloc.h>
2525
#include <linux/kasan.h>
26+
#include <linux/bpf_verifier.h>
2627

2728
#include "../../lib/kstrtox.h"
2829

@@ -1907,11 +1908,21 @@ static const struct bpf_func_proto bpf_dynptr_data_proto = {
19071908

19081909
const struct bpf_func_proto bpf_get_current_task_proto __weak;
19091910
const struct bpf_func_proto bpf_get_current_task_btf_proto __weak;
1911+
#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1912+
const struct bpf_func_proto bpf_probe_read_compat_proto __weak;
1913+
const struct bpf_func_proto bpf_probe_read_compat_str_proto __weak;
1914+
#endif
19101915
const struct bpf_func_proto bpf_probe_read_user_proto __weak;
19111916
const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;
19121917
const struct bpf_func_proto bpf_probe_read_kernel_proto __weak;
19131918
const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;
19141919
const struct bpf_func_proto bpf_task_pt_regs_proto __weak;
1920+
const struct bpf_func_proto bpf_perf_event_read_proto __weak;
1921+
const struct bpf_func_proto bpf_send_signal_proto __weak;
1922+
const struct bpf_func_proto bpf_send_signal_thread_proto __weak;
1923+
const struct bpf_func_proto bpf_get_task_stack_sleepable_proto __weak;
1924+
const struct bpf_func_proto bpf_get_task_stack_proto __weak;
1925+
const struct bpf_func_proto bpf_get_branch_snapshot_proto __weak;
19151926

19161927
const struct bpf_func_proto *
19171928
bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
@@ -1965,6 +1976,8 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
19651976
return &bpf_get_current_pid_tgid_proto;
19661977
case BPF_FUNC_get_ns_current_pid_tgid:
19671978
return &bpf_get_ns_current_pid_tgid_proto;
1979+
case BPF_FUNC_get_current_uid_gid:
1980+
return &bpf_get_current_uid_gid_proto;
19681981
default:
19691982
break;
19701983
}
@@ -2022,6 +2035,8 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
20222035
return &bpf_get_current_cgroup_id_proto;
20232036
case BPF_FUNC_get_current_ancestor_cgroup_id:
20242037
return &bpf_get_current_ancestor_cgroup_id_proto;
2038+
case BPF_FUNC_current_task_under_cgroup:
2039+
return &bpf_current_task_under_cgroup_proto;
20252040
#endif
20262041
default:
20272042
break;
@@ -2037,6 +2052,16 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
20372052
return &bpf_get_current_task_proto;
20382053
case BPF_FUNC_get_current_task_btf:
20392054
return &bpf_get_current_task_btf_proto;
2055+
case BPF_FUNC_get_current_comm:
2056+
return &bpf_get_current_comm_proto;
2057+
#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
2058+
case BPF_FUNC_probe_read:
2059+
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
2060+
NULL : &bpf_probe_read_compat_proto;
2061+
case BPF_FUNC_probe_read_str:
2062+
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
2063+
NULL : &bpf_probe_read_compat_str_proto;
2064+
#endif
20402065
case BPF_FUNC_probe_read_user:
20412066
return &bpf_probe_read_user_proto;
20422067
case BPF_FUNC_probe_read_kernel:
@@ -2057,6 +2082,31 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
20572082
return bpf_get_trace_vprintk_proto();
20582083
case BPF_FUNC_perf_event_read_value:
20592084
return bpf_get_perf_event_read_value_proto();
2085+
case BPF_FUNC_perf_event_read:
2086+
return &bpf_perf_event_read_proto;
2087+
case BPF_FUNC_send_signal:
2088+
return &bpf_send_signal_proto;
2089+
case BPF_FUNC_send_signal_thread:
2090+
return &bpf_send_signal_thread_proto;
2091+
case BPF_FUNC_get_task_stack:
2092+
return prog->sleepable ? &bpf_get_task_stack_sleepable_proto
2093+
: &bpf_get_task_stack_proto;
2094+
case BPF_FUNC_copy_from_user:
2095+
return prog->sleepable ? &bpf_copy_from_user_proto : NULL;
2096+
case BPF_FUNC_copy_from_user_task:
2097+
return prog->sleepable ? &bpf_copy_from_user_task_proto : NULL;
2098+
case BPF_FUNC_task_storage_get:
2099+
if (bpf_prog_check_recur(prog))
2100+
return &bpf_task_storage_get_recur_proto;
2101+
return &bpf_task_storage_get_proto;
2102+
case BPF_FUNC_task_storage_delete:
2103+
if (bpf_prog_check_recur(prog))
2104+
return &bpf_task_storage_delete_recur_proto;
2105+
return &bpf_task_storage_delete_proto;
2106+
case BPF_FUNC_get_branch_snapshot:
2107+
return &bpf_get_branch_snapshot_proto;
2108+
case BPF_FUNC_find_vma:
2109+
return &bpf_find_vma_proto;
20602110
default:
20612111
return NULL;
20622112
}

kernel/trace/bpf_trace.c

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
294294
return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
295295
}
296296

297-
static const struct bpf_func_proto bpf_probe_read_compat_proto = {
297+
const struct bpf_func_proto bpf_probe_read_compat_proto = {
298298
.func = bpf_probe_read_compat,
299299
.gpl_only = true,
300300
.ret_type = RET_INTEGER,
@@ -313,7 +313,7 @@ BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
313313
return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
314314
}
315315

316-
static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
316+
const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
317317
.func = bpf_probe_read_compat_str,
318318
.gpl_only = true,
319319
.ret_type = RET_INTEGER,
@@ -572,7 +572,7 @@ BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
572572
return value;
573573
}
574574

575-
static const struct bpf_func_proto bpf_perf_event_read_proto = {
575+
const struct bpf_func_proto bpf_perf_event_read_proto = {
576576
.func = bpf_perf_event_read,
577577
.gpl_only = true,
578578
.ret_type = RET_INTEGER,
@@ -882,7 +882,7 @@ BPF_CALL_1(bpf_send_signal, u32, sig)
882882
return bpf_send_signal_common(sig, PIDTYPE_TGID, NULL, 0);
883883
}
884884

885-
static const struct bpf_func_proto bpf_send_signal_proto = {
885+
const struct bpf_func_proto bpf_send_signal_proto = {
886886
.func = bpf_send_signal,
887887
.gpl_only = false,
888888
.ret_type = RET_INTEGER,
@@ -894,7 +894,7 @@ BPF_CALL_1(bpf_send_signal_thread, u32, sig)
894894
return bpf_send_signal_common(sig, PIDTYPE_PID, NULL, 0);
895895
}
896896

897-
static const struct bpf_func_proto bpf_send_signal_thread_proto = {
897+
const struct bpf_func_proto bpf_send_signal_thread_proto = {
898898
.func = bpf_send_signal_thread,
899899
.gpl_only = false,
900900
.ret_type = RET_INTEGER,
@@ -1185,7 +1185,7 @@ BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
11851185
return entry_cnt * br_entry_size;
11861186
}
11871187

1188-
static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
1188+
const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
11891189
.func = bpf_get_branch_snapshot,
11901190
.gpl_only = true,
11911191
.ret_type = RET_INTEGER,
@@ -1430,51 +1430,10 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
14301430
const struct bpf_func_proto *func_proto;
14311431

14321432
switch (func_id) {
1433-
case BPF_FUNC_get_current_uid_gid:
1434-
return &bpf_get_current_uid_gid_proto;
1435-
case BPF_FUNC_get_current_comm:
1436-
return &bpf_get_current_comm_proto;
14371433
case BPF_FUNC_get_smp_processor_id:
14381434
return &bpf_get_smp_processor_id_proto;
1439-
case BPF_FUNC_perf_event_read:
1440-
return &bpf_perf_event_read_proto;
1441-
#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1442-
case BPF_FUNC_probe_read:
1443-
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1444-
NULL : &bpf_probe_read_compat_proto;
1445-
case BPF_FUNC_probe_read_str:
1446-
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1447-
NULL : &bpf_probe_read_compat_str_proto;
1448-
#endif
1449-
#ifdef CONFIG_CGROUPS
1450-
case BPF_FUNC_current_task_under_cgroup:
1451-
return &bpf_current_task_under_cgroup_proto;
1452-
#endif
1453-
case BPF_FUNC_send_signal:
1454-
return &bpf_send_signal_proto;
1455-
case BPF_FUNC_send_signal_thread:
1456-
return &bpf_send_signal_thread_proto;
1457-
case BPF_FUNC_get_task_stack:
1458-
return prog->sleepable ? &bpf_get_task_stack_sleepable_proto
1459-
: &bpf_get_task_stack_proto;
1460-
case BPF_FUNC_copy_from_user:
1461-
return &bpf_copy_from_user_proto;
1462-
case BPF_FUNC_copy_from_user_task:
1463-
return &bpf_copy_from_user_task_proto;
1464-
case BPF_FUNC_task_storage_get:
1465-
if (bpf_prog_check_recur(prog))
1466-
return &bpf_task_storage_get_recur_proto;
1467-
return &bpf_task_storage_get_proto;
1468-
case BPF_FUNC_task_storage_delete:
1469-
if (bpf_prog_check_recur(prog))
1470-
return &bpf_task_storage_delete_recur_proto;
1471-
return &bpf_task_storage_delete_proto;
14721435
case BPF_FUNC_get_func_ip:
14731436
return &bpf_get_func_ip_proto_tracing;
1474-
case BPF_FUNC_get_branch_snapshot:
1475-
return &bpf_get_branch_snapshot_proto;
1476-
case BPF_FUNC_find_vma:
1477-
return &bpf_find_vma_proto;
14781437
default:
14791438
break;
14801439
}

net/core/filter.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8488,8 +8488,6 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
84888488
return &bpf_msg_pop_data_proto;
84898489
case BPF_FUNC_perf_event_output:
84908490
return &bpf_event_output_data_proto;
8491-
case BPF_FUNC_get_current_uid_gid:
8492-
return &bpf_get_current_uid_gid_proto;
84938491
case BPF_FUNC_sk_storage_get:
84948492
return &bpf_sk_storage_get_proto;
84958493
case BPF_FUNC_sk_storage_delete:

0 commit comments

Comments
 (0)