Skip to content

Commit 43bf77d

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 a041a61 commit 43bf77d

File tree

4 files changed

+42
-45
lines changed

4 files changed

+42
-45
lines changed

kernel/bpf/cgroup.c

-6
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

+38
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

@@ -1912,6 +1913,12 @@ const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;
19121913
const struct bpf_func_proto bpf_probe_read_kernel_proto __weak;
19131914
const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;
19141915
const struct bpf_func_proto bpf_task_pt_regs_proto __weak;
1916+
const struct bpf_func_proto bpf_perf_event_read_proto __weak;
1917+
const struct bpf_func_proto bpf_send_signal_proto __weak;
1918+
const struct bpf_func_proto bpf_send_signal_thread_proto __weak;
1919+
const struct bpf_func_proto bpf_get_task_stack_sleepable_proto __weak;
1920+
const struct bpf_func_proto bpf_get_task_stack_proto __weak;
1921+
const struct bpf_func_proto bpf_get_branch_snapshot_proto __weak;
19151922

19161923
const struct bpf_func_proto *
19171924
bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
@@ -1965,6 +1972,8 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
19651972
return &bpf_get_current_pid_tgid_proto;
19661973
case BPF_FUNC_get_ns_current_pid_tgid:
19671974
return &bpf_get_ns_current_pid_tgid_proto;
1975+
case BPF_FUNC_get_current_uid_gid:
1976+
return &bpf_get_current_uid_gid_proto;
19681977
default:
19691978
break;
19701979
}
@@ -2022,6 +2031,8 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
20222031
return &bpf_get_current_cgroup_id_proto;
20232032
case BPF_FUNC_get_current_ancestor_cgroup_id:
20242033
return &bpf_get_current_ancestor_cgroup_id_proto;
2034+
case BPF_FUNC_current_task_under_cgroup:
2035+
return &bpf_current_task_under_cgroup_proto;
20252036
#endif
20262037
default:
20272038
break;
@@ -2037,6 +2048,8 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
20372048
return &bpf_get_current_task_proto;
20382049
case BPF_FUNC_get_current_task_btf:
20392050
return &bpf_get_current_task_btf_proto;
2051+
case BPF_FUNC_get_current_comm:
2052+
return &bpf_get_current_comm_proto;
20402053
case BPF_FUNC_probe_read_user:
20412054
return &bpf_probe_read_user_proto;
20422055
case BPF_FUNC_probe_read_kernel:
@@ -2047,6 +2060,10 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
20472060
case BPF_FUNC_probe_read_kernel_str:
20482061
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
20492062
NULL : &bpf_probe_read_kernel_str_proto;
2063+
case BPF_FUNC_copy_from_user:
2064+
return &bpf_copy_from_user_proto;
2065+
case BPF_FUNC_copy_from_user_task:
2066+
return &bpf_copy_from_user_task_proto;
20502067
case BPF_FUNC_snprintf_btf:
20512068
return &bpf_snprintf_btf_proto;
20522069
case BPF_FUNC_snprintf:
@@ -2057,6 +2074,27 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
20572074
return bpf_get_trace_vprintk_proto();
20582075
case BPF_FUNC_perf_event_read_value:
20592076
return bpf_get_perf_event_read_value_proto();
2077+
case BPF_FUNC_perf_event_read:
2078+
return &bpf_perf_event_read_proto;
2079+
case BPF_FUNC_send_signal:
2080+
return &bpf_send_signal_proto;
2081+
case BPF_FUNC_send_signal_thread:
2082+
return &bpf_send_signal_thread_proto;
2083+
case BPF_FUNC_get_task_stack:
2084+
return prog->sleepable ? &bpf_get_task_stack_sleepable_proto
2085+
: &bpf_get_task_stack_proto;
2086+
case BPF_FUNC_task_storage_get:
2087+
if (bpf_prog_check_recur(prog))
2088+
return &bpf_task_storage_get_recur_proto;
2089+
return &bpf_task_storage_get_proto;
2090+
case BPF_FUNC_task_storage_delete:
2091+
if (bpf_prog_check_recur(prog))
2092+
return &bpf_task_storage_delete_recur_proto;
2093+
return &bpf_task_storage_delete_proto;
2094+
case BPF_FUNC_get_branch_snapshot:
2095+
return &bpf_get_branch_snapshot_proto;
2096+
case BPF_FUNC_find_vma:
2097+
return &bpf_find_vma_proto;
20602098
default:
20612099
return NULL;
20622100
}

kernel/trace/bpf_trace.c

+4-37
Original file line numberDiff line numberDiff line change
@@ -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,14 +1430,8 @@ 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;
14411435
#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
14421436
case BPF_FUNC_probe_read:
14431437
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
@@ -1446,35 +1440,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
14461440
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
14471441
NULL : &bpf_probe_read_compat_str_proto;
14481442
#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;
14721443
case BPF_FUNC_get_func_ip:
14731444
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;
14781445
default:
14791446
break;
14801447
}

net/core/filter.c

-2
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)