Skip to content

Commit 0f2d39f

Browse files
committed
Merge branch 'bpf-allow-some-trace-helpers-for-all-prog-types'
Feng Yang says: ==================== bpf: Allow some trace helpers for all prog types From: Feng Yang <[email protected]> This series 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/ --- Changes in v3: - cgroup_current_func_proto clean. - bpf_scx_get_func_proto clean. Thanks, Andrii Nakryiko. - Link to v2: https://lore.kernel.org/all/[email protected]/ Changes in v2: - not expose compat probe read APIs to more program types. - Remove the prog->sleepable check added for copy_from_user, - or the summarization_freplace/might_sleep_with_might_sleep test will fail with the error "program of this type cannot use helper bpf_copy_from_user" - Link to v1: https://lore.kernel.org/all/[email protected]/ ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents 32c563d + 8c112a4 commit 0f2d39f

File tree

6 files changed

+47
-105
lines changed

6 files changed

+47
-105
lines changed

include/linux/bpf-cgroup.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,6 @@ int cgroup_bpf_prog_query(const union bpf_attr *attr,
427427

428428
const struct bpf_func_proto *
429429
cgroup_common_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
430-
const struct bpf_func_proto *
431-
cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
432430
#else
433431

434432
static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
@@ -465,12 +463,6 @@ cgroup_common_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
465463
return NULL;
466464
}
467465

468-
static inline const struct bpf_func_proto *
469-
cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
470-
{
471-
return NULL;
472-
}
473-
474466
static inline int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux,
475467
struct bpf_map *map) { return 0; }
476468
static inline struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(

kernel/bpf/cgroup.c

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,10 +1653,6 @@ cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
16531653
if (func_proto)
16541654
return func_proto;
16551655

1656-
func_proto = cgroup_current_func_proto(func_id, prog);
1657-
if (func_proto)
1658-
return func_proto;
1659-
16601656
switch (func_id) {
16611657
case BPF_FUNC_perf_event_output:
16621658
return &bpf_event_output_data_proto;
@@ -2204,10 +2200,6 @@ sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
22042200
if (func_proto)
22052201
return func_proto;
22062202

2207-
func_proto = cgroup_current_func_proto(func_id, prog);
2208-
if (func_proto)
2209-
return func_proto;
2210-
22112203
switch (func_id) {
22122204
case BPF_FUNC_sysctl_get_name:
22132205
return &bpf_sysctl_get_name_proto;
@@ -2351,10 +2343,6 @@ cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
23512343
if (func_proto)
23522344
return func_proto;
23532345

2354-
func_proto = cgroup_current_func_proto(func_id, prog);
2355-
if (func_proto)
2356-
return func_proto;
2357-
23582346
switch (func_id) {
23592347
#ifdef CONFIG_NET
23602348
case BPF_FUNC_get_netns_cookie:
@@ -2601,23 +2589,3 @@ cgroup_common_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
26012589
return NULL;
26022590
}
26032591
}
2604-
2605-
/* Common helpers for cgroup hooks with valid process context. */
2606-
const struct bpf_func_proto *
2607-
cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2608-
{
2609-
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;
2614-
#ifdef CONFIG_CGROUP_NET_CLASSID
2615-
case BPF_FUNC_get_cgroup_classid:
2616-
return &bpf_get_cgroup_classid_curr_proto;
2617-
#endif
2618-
case BPF_FUNC_current_task_under_cgroup:
2619-
return &bpf_current_task_under_cgroup_proto;
2620-
default:
2621-
return NULL;
2622-
}
2623-
}

kernel/bpf/helpers.c

Lines changed: 42 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

@@ -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,7 +2031,21 @@ 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
2037+
#ifdef CONFIG_CGROUP_NET_CLASSID
2038+
case BPF_FUNC_get_cgroup_classid:
2039+
return &bpf_get_cgroup_classid_curr_proto;
2040+
#endif
2041+
case BPF_FUNC_task_storage_get:
2042+
if (bpf_prog_check_recur(prog))
2043+
return &bpf_task_storage_get_recur_proto;
2044+
return &bpf_task_storage_get_proto;
2045+
case BPF_FUNC_task_storage_delete:
2046+
if (bpf_prog_check_recur(prog))
2047+
return &bpf_task_storage_delete_recur_proto;
2048+
return &bpf_task_storage_delete_proto;
20262049
default:
20272050
break;
20282051
}
@@ -2037,6 +2060,8 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
20372060
return &bpf_get_current_task_proto;
20382061
case BPF_FUNC_get_current_task_btf:
20392062
return &bpf_get_current_task_btf_proto;
2063+
case BPF_FUNC_get_current_comm:
2064+
return &bpf_get_current_comm_proto;
20402065
case BPF_FUNC_probe_read_user:
20412066
return &bpf_probe_read_user_proto;
20422067
case BPF_FUNC_probe_read_kernel:
@@ -2047,6 +2072,10 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
20472072
case BPF_FUNC_probe_read_kernel_str:
20482073
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
20492074
NULL : &bpf_probe_read_kernel_str_proto;
2075+
case BPF_FUNC_copy_from_user:
2076+
return &bpf_copy_from_user_proto;
2077+
case BPF_FUNC_copy_from_user_task:
2078+
return &bpf_copy_from_user_task_proto;
20502079
case BPF_FUNC_snprintf_btf:
20512080
return &bpf_snprintf_btf_proto;
20522081
case BPF_FUNC_snprintf:
@@ -2057,6 +2086,19 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
20572086
return bpf_get_trace_vprintk_proto();
20582087
case BPF_FUNC_perf_event_read_value:
20592088
return bpf_get_perf_event_read_value_proto();
2089+
case BPF_FUNC_perf_event_read:
2090+
return &bpf_perf_event_read_proto;
2091+
case BPF_FUNC_send_signal:
2092+
return &bpf_send_signal_proto;
2093+
case BPF_FUNC_send_signal_thread:
2094+
return &bpf_send_signal_thread_proto;
2095+
case BPF_FUNC_get_task_stack:
2096+
return prog->sleepable ? &bpf_get_task_stack_sleepable_proto
2097+
: &bpf_get_task_stack_proto;
2098+
case BPF_FUNC_get_branch_snapshot:
2099+
return &bpf_get_branch_snapshot_proto;
2100+
case BPF_FUNC_find_vma:
2101+
return &bpf_find_vma_proto;
20602102
default:
20612103
return NULL;
20622104
}

kernel/sched/ext.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5586,21 +5586,8 @@ static int bpf_scx_btf_struct_access(struct bpf_verifier_log *log,
55865586
return -EACCES;
55875587
}
55885588

5589-
static const struct bpf_func_proto *
5590-
bpf_scx_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5591-
{
5592-
switch (func_id) {
5593-
case BPF_FUNC_task_storage_get:
5594-
return &bpf_task_storage_get_proto;
5595-
case BPF_FUNC_task_storage_delete:
5596-
return &bpf_task_storage_delete_proto;
5597-
default:
5598-
return bpf_base_func_proto(func_id, prog);
5599-
}
5600-
}
5601-
56025589
static const struct bpf_verifier_ops bpf_scx_verifier_ops = {
5603-
.get_func_proto = bpf_scx_get_func_proto,
5590+
.get_func_proto = bpf_base_func_proto,
56045591
.is_valid_access = bpf_scx_is_valid_access,
56055592
.btf_struct_access = bpf_scx_btf_struct_access,
56065593
};

kernel/trace/bpf_trace.c

Lines changed: 4 additions & 37 deletions
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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8022,10 +8022,6 @@ sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
80228022
if (func_proto)
80238023
return func_proto;
80248024

8025-
func_proto = cgroup_current_func_proto(func_id, prog);
8026-
if (func_proto)
8027-
return func_proto;
8028-
80298025
switch (func_id) {
80308026
case BPF_FUNC_get_socket_cookie:
80318027
return &bpf_get_socket_cookie_sock_proto;
@@ -8051,10 +8047,6 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
80518047
if (func_proto)
80528048
return func_proto;
80538049

8054-
func_proto = cgroup_current_func_proto(func_id, prog);
8055-
if (func_proto)
8056-
return func_proto;
8057-
80588050
switch (func_id) {
80598051
case BPF_FUNC_bind:
80608052
switch (prog->expected_attach_type) {
@@ -8488,18 +8480,12 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
84888480
return &bpf_msg_pop_data_proto;
84898481
case BPF_FUNC_perf_event_output:
84908482
return &bpf_event_output_data_proto;
8491-
case BPF_FUNC_get_current_uid_gid:
8492-
return &bpf_get_current_uid_gid_proto;
84938483
case BPF_FUNC_sk_storage_get:
84948484
return &bpf_sk_storage_get_proto;
84958485
case BPF_FUNC_sk_storage_delete:
84968486
return &bpf_sk_storage_delete_proto;
84978487
case BPF_FUNC_get_netns_cookie:
84988488
return &bpf_get_netns_cookie_sk_msg_proto;
8499-
#ifdef CONFIG_CGROUP_NET_CLASSID
8500-
case BPF_FUNC_get_cgroup_classid:
8501-
return &bpf_get_cgroup_classid_curr_proto;
8502-
#endif
85038489
default:
85048490
return bpf_sk_base_func_proto(func_id, prog);
85058491
}

0 commit comments

Comments
 (0)