Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <yangfeng@kylinos.cn>

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/20250427063821.207263-1-yangfeng59949@163.com/

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/20250425080032.327477-1-yangfeng59949@163.com/
====================

Link: https://patch.msgid.link/20250506061434.94277-1-yangfeng59949@163.com
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

+47 -105
-8
include/linux/bpf-cgroup.h
··· 427 427 428 428 const struct bpf_func_proto * 429 429 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); 432 430 #else 433 431 434 432 static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; } ··· 459 461 460 462 static inline const struct bpf_func_proto * 461 463 cgroup_common_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 462 - { 463 - return NULL; 464 - } 465 - 466 - static inline const struct bpf_func_proto * 467 - cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 468 464 { 469 465 return NULL; 470 466 }
-32
kernel/bpf/cgroup.c
··· 1653 1653 if (func_proto) 1654 1654 return func_proto; 1655 1655 1656 - func_proto = cgroup_current_func_proto(func_id, prog); 1657 - if (func_proto) 1658 - return func_proto; 1659 - 1660 1656 switch (func_id) { 1661 1657 case BPF_FUNC_perf_event_output: 1662 1658 return &bpf_event_output_data_proto; ··· 2200 2204 if (func_proto) 2201 2205 return func_proto; 2202 2206 2203 - func_proto = cgroup_current_func_proto(func_id, prog); 2204 - if (func_proto) 2205 - return func_proto; 2206 - 2207 2207 switch (func_id) { 2208 2208 case BPF_FUNC_sysctl_get_name: 2209 2209 return &bpf_sysctl_get_name_proto; ··· 2340 2348 const struct bpf_func_proto *func_proto; 2341 2349 2342 2350 func_proto = cgroup_common_func_proto(func_id, prog); 2343 - if (func_proto) 2344 - return func_proto; 2345 - 2346 - func_proto = cgroup_current_func_proto(func_id, prog); 2347 2351 if (func_proto) 2348 2352 return func_proto; 2349 2353 ··· 2585 2597 default: 2586 2598 return &bpf_set_retval_proto; 2587 2599 } 2588 - default: 2589 - return NULL; 2590 - } 2591 - } 2592 - 2593 - /* Common helpers for cgroup hooks with valid process context. */ 2594 - const struct bpf_func_proto * 2595 - cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 2596 - { 2597 - switch (func_id) { 2598 - case BPF_FUNC_get_current_uid_gid: 2599 - return &bpf_get_current_uid_gid_proto; 2600 - case BPF_FUNC_get_current_comm: 2601 - return &bpf_get_current_comm_proto; 2602 - #ifdef CONFIG_CGROUP_NET_CLASSID 2603 - case BPF_FUNC_get_cgroup_classid: 2604 - return &bpf_get_cgroup_classid_curr_proto; 2605 - #endif 2606 - case BPF_FUNC_current_task_under_cgroup: 2607 - return &bpf_current_task_under_cgroup_proto; 2608 2600 default: 2609 2601 return NULL; 2610 2602 }
+42
kernel/bpf/helpers.c
··· 23 23 #include <linux/btf_ids.h> 24 24 #include <linux/bpf_mem_alloc.h> 25 25 #include <linux/kasan.h> 26 + #include <linux/bpf_verifier.h> 26 27 27 28 #include "../../lib/kstrtox.h" 28 29 ··· 1913 1912 const struct bpf_func_proto bpf_probe_read_kernel_proto __weak; 1914 1913 const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak; 1915 1914 const struct bpf_func_proto bpf_task_pt_regs_proto __weak; 1915 + const struct bpf_func_proto bpf_perf_event_read_proto __weak; 1916 + const struct bpf_func_proto bpf_send_signal_proto __weak; 1917 + const struct bpf_func_proto bpf_send_signal_thread_proto __weak; 1918 + const struct bpf_func_proto bpf_get_task_stack_sleepable_proto __weak; 1919 + const struct bpf_func_proto bpf_get_task_stack_proto __weak; 1920 + const struct bpf_func_proto bpf_get_branch_snapshot_proto __weak; 1916 1921 1917 1922 const struct bpf_func_proto * 1918 1923 bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ··· 1972 1965 return &bpf_get_current_pid_tgid_proto; 1973 1966 case BPF_FUNC_get_ns_current_pid_tgid: 1974 1967 return &bpf_get_ns_current_pid_tgid_proto; 1968 + case BPF_FUNC_get_current_uid_gid: 1969 + return &bpf_get_current_uid_gid_proto; 1975 1970 default: 1976 1971 break; 1977 1972 } ··· 2031 2022 return &bpf_get_current_cgroup_id_proto; 2032 2023 case BPF_FUNC_get_current_ancestor_cgroup_id: 2033 2024 return &bpf_get_current_ancestor_cgroup_id_proto; 2025 + case BPF_FUNC_current_task_under_cgroup: 2026 + return &bpf_current_task_under_cgroup_proto; 2034 2027 #endif 2028 + #ifdef CONFIG_CGROUP_NET_CLASSID 2029 + case BPF_FUNC_get_cgroup_classid: 2030 + return &bpf_get_cgroup_classid_curr_proto; 2031 + #endif 2032 + case BPF_FUNC_task_storage_get: 2033 + if (bpf_prog_check_recur(prog)) 2034 + return &bpf_task_storage_get_recur_proto; 2035 + return &bpf_task_storage_get_proto; 2036 + case BPF_FUNC_task_storage_delete: 2037 + if (bpf_prog_check_recur(prog)) 2038 + return &bpf_task_storage_delete_recur_proto; 2039 + return &bpf_task_storage_delete_proto; 2035 2040 default: 2036 2041 break; 2037 2042 } ··· 2060 2037 return &bpf_get_current_task_proto; 2061 2038 case BPF_FUNC_get_current_task_btf: 2062 2039 return &bpf_get_current_task_btf_proto; 2040 + case BPF_FUNC_get_current_comm: 2041 + return &bpf_get_current_comm_proto; 2063 2042 case BPF_FUNC_probe_read_user: 2064 2043 return &bpf_probe_read_user_proto; 2065 2044 case BPF_FUNC_probe_read_kernel: ··· 2072 2047 case BPF_FUNC_probe_read_kernel_str: 2073 2048 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? 2074 2049 NULL : &bpf_probe_read_kernel_str_proto; 2050 + case BPF_FUNC_copy_from_user: 2051 + return &bpf_copy_from_user_proto; 2052 + case BPF_FUNC_copy_from_user_task: 2053 + return &bpf_copy_from_user_task_proto; 2075 2054 case BPF_FUNC_snprintf_btf: 2076 2055 return &bpf_snprintf_btf_proto; 2077 2056 case BPF_FUNC_snprintf: ··· 2086 2057 return bpf_get_trace_vprintk_proto(); 2087 2058 case BPF_FUNC_perf_event_read_value: 2088 2059 return bpf_get_perf_event_read_value_proto(); 2060 + case BPF_FUNC_perf_event_read: 2061 + return &bpf_perf_event_read_proto; 2062 + case BPF_FUNC_send_signal: 2063 + return &bpf_send_signal_proto; 2064 + case BPF_FUNC_send_signal_thread: 2065 + return &bpf_send_signal_thread_proto; 2066 + case BPF_FUNC_get_task_stack: 2067 + return prog->sleepable ? &bpf_get_task_stack_sleepable_proto 2068 + : &bpf_get_task_stack_proto; 2069 + case BPF_FUNC_get_branch_snapshot: 2070 + return &bpf_get_branch_snapshot_proto; 2071 + case BPF_FUNC_find_vma: 2072 + return &bpf_find_vma_proto; 2089 2073 default: 2090 2074 return NULL; 2091 2075 }
+1 -14
kernel/sched/ext.c
··· 5586 5586 return -EACCES; 5587 5587 } 5588 5588 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 - 5602 5589 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, 5604 5591 .is_valid_access = bpf_scx_is_valid_access, 5605 5592 .btf_struct_access = bpf_scx_btf_struct_access, 5606 5593 };
+4 -37
kernel/trace/bpf_trace.c
··· 572 572 return value; 573 573 } 574 574 575 - static const struct bpf_func_proto bpf_perf_event_read_proto = { 575 + const struct bpf_func_proto bpf_perf_event_read_proto = { 576 576 .func = bpf_perf_event_read, 577 577 .gpl_only = true, 578 578 .ret_type = RET_INTEGER, ··· 882 882 return bpf_send_signal_common(sig, PIDTYPE_TGID, NULL, 0); 883 883 } 884 884 885 - static const struct bpf_func_proto bpf_send_signal_proto = { 885 + const struct bpf_func_proto bpf_send_signal_proto = { 886 886 .func = bpf_send_signal, 887 887 .gpl_only = false, 888 888 .ret_type = RET_INTEGER, ··· 894 894 return bpf_send_signal_common(sig, PIDTYPE_PID, NULL, 0); 895 895 } 896 896 897 - static const struct bpf_func_proto bpf_send_signal_thread_proto = { 897 + const struct bpf_func_proto bpf_send_signal_thread_proto = { 898 898 .func = bpf_send_signal_thread, 899 899 .gpl_only = false, 900 900 .ret_type = RET_INTEGER, ··· 1185 1185 return entry_cnt * br_entry_size; 1186 1186 } 1187 1187 1188 - static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { 1188 + const struct bpf_func_proto bpf_get_branch_snapshot_proto = { 1189 1189 .func = bpf_get_branch_snapshot, 1190 1190 .gpl_only = true, 1191 1191 .ret_type = RET_INTEGER, ··· 1430 1430 const struct bpf_func_proto *func_proto; 1431 1431 1432 1432 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; 1437 1433 case BPF_FUNC_get_smp_processor_id: 1438 1434 return &bpf_get_smp_processor_id_proto; 1439 - case BPF_FUNC_perf_event_read: 1440 - return &bpf_perf_event_read_proto; 1441 1435 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE 1442 1436 case BPF_FUNC_probe_read: 1443 1437 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? ··· 1440 1446 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? 1441 1447 NULL : &bpf_probe_read_compat_str_proto; 1442 1448 #endif 1443 - #ifdef CONFIG_CGROUPS 1444 - case BPF_FUNC_current_task_under_cgroup: 1445 - return &bpf_current_task_under_cgroup_proto; 1446 - #endif 1447 - case BPF_FUNC_send_signal: 1448 - return &bpf_send_signal_proto; 1449 - case BPF_FUNC_send_signal_thread: 1450 - return &bpf_send_signal_thread_proto; 1451 - case BPF_FUNC_get_task_stack: 1452 - return prog->sleepable ? &bpf_get_task_stack_sleepable_proto 1453 - : &bpf_get_task_stack_proto; 1454 - case BPF_FUNC_copy_from_user: 1455 - return &bpf_copy_from_user_proto; 1456 - case BPF_FUNC_copy_from_user_task: 1457 - return &bpf_copy_from_user_task_proto; 1458 - case BPF_FUNC_task_storage_get: 1459 - if (bpf_prog_check_recur(prog)) 1460 - return &bpf_task_storage_get_recur_proto; 1461 - return &bpf_task_storage_get_proto; 1462 - case BPF_FUNC_task_storage_delete: 1463 - if (bpf_prog_check_recur(prog)) 1464 - return &bpf_task_storage_delete_recur_proto; 1465 - return &bpf_task_storage_delete_proto; 1466 1449 case BPF_FUNC_get_func_ip: 1467 1450 return &bpf_get_func_ip_proto_tracing; 1468 - case BPF_FUNC_get_branch_snapshot: 1469 - return &bpf_get_branch_snapshot_proto; 1470 - case BPF_FUNC_find_vma: 1471 - return &bpf_find_vma_proto; 1472 1451 default: 1473 1452 break; 1474 1453 }
-14
net/core/filter.c
··· 8022 8022 if (func_proto) 8023 8023 return func_proto; 8024 8024 8025 - func_proto = cgroup_current_func_proto(func_id, prog); 8026 - if (func_proto) 8027 - return func_proto; 8028 - 8029 8025 switch (func_id) { 8030 8026 case BPF_FUNC_get_socket_cookie: 8031 8027 return &bpf_get_socket_cookie_sock_proto; ··· 8044 8048 const struct bpf_func_proto *func_proto; 8045 8049 8046 8050 func_proto = cgroup_common_func_proto(func_id, prog); 8047 - if (func_proto) 8048 - return func_proto; 8049 - 8050 - func_proto = cgroup_current_func_proto(func_id, prog); 8051 8051 if (func_proto) 8052 8052 return func_proto; 8053 8053 ··· 8480 8488 return &bpf_msg_pop_data_proto; 8481 8489 case BPF_FUNC_perf_event_output: 8482 8490 return &bpf_event_output_data_proto; 8483 - case BPF_FUNC_get_current_uid_gid: 8484 - return &bpf_get_current_uid_gid_proto; 8485 8491 case BPF_FUNC_sk_storage_get: 8486 8492 return &bpf_sk_storage_get_proto; 8487 8493 case BPF_FUNC_sk_storage_delete: 8488 8494 return &bpf_sk_storage_delete_proto; 8489 8495 case BPF_FUNC_get_netns_cookie: 8490 8496 return &bpf_get_netns_cookie_sk_msg_proto; 8491 - #ifdef CONFIG_CGROUP_NET_CLASSID 8492 - case BPF_FUNC_get_cgroup_classid: 8493 - return &bpf_get_cgroup_classid_curr_proto; 8494 - #endif 8495 8497 default: 8496 8498 return bpf_sk_base_func_proto(func_id, prog); 8497 8499 }