BPF Standard Streams
Subsystems (8)
Kumar Kartikeya Dwivedi
maintainerv2 cover BPF Standard Streams
This set introduces a standard output interface with two streams, namely stdout and stderr, for BPF programs. The idea is that these streams will be written to by BPF programs and the kernel, and serve as standard interfaces for informing user space of any BPF runtime violations. Users can also utilize them for printing normal messages for debugging usage, as is the case with bpf_printk() and trace pipe interface.
BPF programs and the kernel can use these streams to output messages. User space can dump these messages using bpftool.
The stream interface itself is implemented using a lockless list, so that we can queue messages from any context. Every printk statement into the stream leads to memory allocation. Allocation itself relies on try_alloc_pages() to construct a bespoke bump allocator to carve out elements. If this fails, we finally give up and drop the message.
See commit logs for more details.
Two scenarios are covered:
- Deadlocks and timeouts in rqspinlock.
- Timeouts for may_goto.
In each we provide the stack trace and source information for the offending BPF programs. Both the C source line and the file and line numbers are printed. The output format is as follows:
ERROR: AA or ABBA deadlock detected for bpf_res_spin_lock
Attempted lock = 0xff11000108f3a5e0
Total held locks = 1
Held lock[ 0] = 0xff11000108f3a5e0
CPU: 48 UID: 0 PID: 786 Comm: test_progs Call trace: bpf_stream_stage_dump_stack+0xb0/0xd0 bpf_prog_report_rqspinlock_violation+0x10b/0x130 bpf_res_spin_lock+0x8c/0xa0 bpf_prog_3699ea119d1f6ed8_foo+0xe5/0x140 if (!bpf_res_spin_lock(&v2->lock)) @ stream_bpftool.c:62 bpf_prog_9b324ec4a1b2a5c0_stream_bpftool_dump_prog_stream+0x7e/0x2d0 foo(stream); @ stream_bpftool.c:93 bpf_prog_test_run_syscall+0x102/0x240 __sys_bpf+0xd68/0x2bf0 __x64_sys_bpf+0x1e/0x30 do_syscall_64+0x68/0x140 entry_SYSCALL_64_after_hwframe+0x76/0x7e
ERROR: Timeout detected for may_goto instruction
CPU: 48 UID: 0 PID: 786 Comm: test_progs Call trace: bpf_stream_stage_dump_stack+0xb0/0xd0 bpf_prog_report_may_goto_violation+0x6a/0x90 bpf_check_timed_may_goto+0x4d/0xa0 arch_bpf_timed_may_goto+0x21/0x40 bpf_prog_3699ea119d1f6ed8_foo+0x12f/0x140 while (can_loop) @ stream_bpftool.c:71 bpf_prog_9b324ec4a1b2a5c0_stream_bpftool_dump_prog_stream+0x7e/0x2d0 foo(stream); @ stream_bpftool.c:93 bpf_prog_test_run_syscall+0x102/0x240 __sys_bpf+0xd68/0x2bf0 __x64_sys_bpf+0x1e/0x30 do_syscall_64+0x68/0x140 entry_SYSCALL_64_after_hwframe+0x76/0x7e
Changelog:
----------
v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]
* Drop arena page fault prints, will be done as follow up. (Alexei)
* Defer Andrii's request to reuse code and Alan's suggestion of error counts to follow up.
* Drop bpf_dynptr_from_mem_slice patch.
* Drop some acks due to heavy reworking.
* Fix KASAN splat in bpf_prog_get_file_line. (Eduard)
* Collapse bpf_prog_ksym_find and is_bpf_text_address into single call. (Eduard)
* Add missing RCU read lock in bpf_prog_ksym_find.
* Fix incorrect error handling in dump_stack_cb.
* Simplify libbpf macro. (Eduard, Andrii)
* Introduce bpf_prog_stream_read() libbpf API. (Eduard, Alexei, Andrii)
* Drop BPF prog from the bpftool, use libbpf API.
* Rework selftests.
RFC v1 -> v1
RFC v1: https://lore.kernel.org/bpf/[email protected]
* Rebase on bpf-next/master.
* Change output in dump_stack to also print source line. (Alexei)
* Simplify API to single pop() operation. (Eduard, Alexei)
* Add kdoc for bpf_dynptr_from_mem_slice.
* Fix -EINVAL returned from prog_dump_stream. (Eduard)
* Split dump_stack() patch into multiple commits.
* Add macro wrapping stream staging API.
* Change bpftool command from dump to tracelog. (Quentin)
* Add bpftool documentation and bash completion. (Quentin)
* Change license of bpftool to Dual BSD/GPL.
* Simplify memory allocator. (Alexei)
* No overflow into second page.
* Remove bpf_mem_alloc() fallback.
* Symlink bpftool BPF program and exercise as selftest. (Eduard)
* Verify output after dumping from ringbuf. (Eduard)
* More failure cases to check API invariants.
* Remove patches for dynptr lifetime fixes (split into separate set).
* Limit maximum error messages, and add stream capacity. (Eduard)
Kumar Kartikeya Dwivedi (11):
bpf: Introduce BPF standard streams
bpf: Add function to extract program source info
bpf: Add function to find program from stack trace
bpf: Hold RCU read lock in bpf_prog_ksym_find
bpf: Add dump_stack() analogue to print to BPF stderr
bpf: Report may_goto timeout to BPF stderr
bpf: Report rqspinlock deadlocks/timeout to BPF stderr
libbpf: Add bpf_stream_printk() macro
libbpf: Introduce bpf_prog_stream_read() API
bpftool: Add support for dumping streams selftests/bpf: Add tests for prog streams
arch/x86/net/bpf_jit_comp.c | 1 - include/linux/bpf.h | 85 ++- include/uapi/linux/bpf.h | 19 + kernel/bpf/Makefile | 2 +- kernel/bpf/core.c | 112 +++- kernel/bpf/helpers.c | 26 +- kernel/bpf/rqspinlock.c | 22 + kernel/bpf/stream.c | 544 ++++++++++++++++++ kernel/bpf/syscall.c | 27 +- kernel/bpf/verifier.c | 5 +- .../bpftool/Documentation/bpftool-prog.rst | 7 + tools/bpf/bpftool/bash-completion/bpftool | 16 +- tools/bpf/bpftool/prog.c | 50 +- tools/include/uapi/linux/bpf.h | 19 + tools/lib/bpf/bpf.c | 16 + tools/lib/bpf/bpf.h | 15 + tools/lib/bpf/bpf_helpers.h | 16 + tools/lib/bpf/libbpf.map | 1 + .../testing/selftests/bpf/prog_tests/stream.c | 110 ++++ tools/testing/selftests/bpf/progs/stream.c | 75 +++ .../testing/selftests/bpf/progs/stream_fail.c | 17 + 21 files changed, 1159 insertions(+), 26 deletions(-) create mode 100644 kernel/bpf/stream.c create mode 100644 tools/testing/selftests/bpf/prog_tests/stream.c create mode 100644 tools/testing/selftests/bpf/progs/stream.c create mode 100644 tools/testing/selftests/bpf/progs/stream_fail.c
base-commit: 079e5c56a5c41d285068939ff7b0041ab10386fa
Signature · 1 line
2.47.1
Kumar Kartikeya Dwivedi
maintainerPATCH v2 1/11 bpf: Introduce BPF standard streams
Add support for a stream API to the kernel and expose related kfuncs to BPF programs. Two streams are exposed, BPF_STDOUT and BPF_STDERR. These can be used for printing messages that can be consumed from user space, thus it's similar in spirit to existing trace_pipe interface.
The kernel will use the BPF_STDERR stream to notify the program of any errors encountered at runtime. BPF programs themselves may use both streams for writing debug messages. BPF library-like code may use BPF_STDERR to print warnings or errors on misuse at runtime.
The implementation of a stream is as follows. Everytime a message is emitted from the kernel (directly, or through a BPF program), a record is allocated by bump allocating from per-cpu region backed by a page obtained using try_alloc_pages. This ensures that we can allocate memory from any context. The eventual plan is to discard this scheme in favor of Alexei's kmalloc_nolock() [0].
This record is then locklessly inserted into a list (llist_add()) so that the printing side doesn't require holding any locks, and works in any context. Each stream has a maximum capacity of 4MB of text, and each printed message is accounted against this limit.
Messages from a program are emitted using the bpf_stream_vprintk kfunc, which takes a stream_id argument in addition to working otherwise similar to bpf_trace_vprintk.
The bprintf buffer helpers are extracted out to be reused for printing the string into them before copying it into the stream, so that we can (with the defined max limit) format a string and know its true length before performing allocations of the stream element.
For consuming elements from a stream, we expose a bpf(2) syscall command named BPF_PROG_STREAM_READ_BY_FD, which allows reading data from the stream of a given prog_fd into a user space buffer. The main logic is implemented in bpf_stream_read(). The log messages are queued in bpf_stream::log by the bpf_stream_vprintk kfunc, and then pulled and ordered correctly in the stream backlog.
For this purpose, we hold a lock around bpf_stream_backlog_peek(), as llist_del_first() (if we maintained a second lockless list for the backlog) wouldn't be safe from multiple threads anyway. Then, if we fail to find something in the backlog log, we splice out everything from the lockless log, and place it in the backlog log, and then return the head of the backlog. Once the full length of the element is consumed, we will pop it and free it.
The lockless list bpf_stream::log is a LIFO stack. Elements obtained using a llist_del_all() operation are in LIFO order, thus would break the chronological ordering if printed directly. Hence, this batch of messages is first reversed. Then, it is stashed into a separate list in the stream, i.e. the backlog_log. The head of this list is the actual message that should always be returned to the caller. All of this is done in bpf_stream_backlog_fill().
From the kernel side, the writing into the stream will be a bit more involved than the typical printk. First, the kernel typically may print a collection of messages into the stream, and parallel writers into the stream may suffer from interleaving of messages. To ensure each group of messages is visible atomically, we can lift the advantage of using a lockless list for pushing in messages.
To enable this, we add a bpf_stream_stage() macro, and require kernel users to use bpf_stream_printk statements for the passed expression to write into the stream. Underneath the macro, we have a message staging API, where a bpf_stream_stage object on the stack accumulates the messages being printed into a local llist_head, and then a commit operation splices the whole batch into the stream's lockless log list.
This is especially pertinent for rqspinlock deadlock messages printed to program streams. After this change, we see each deadlock invocation as a non-interleaving contiguous message without any confusion on the reader's part, improving their user experience in debugging the fault.
While programs cannot benefit from this staged stream writing API, they could just as well hold an rqspinlock around their print statements to serialize messages, hence this is kept kernel-internal for now.
Overall, this infrastructure provides NMI-safe any context printing of messages to two dedicated streams.
Later patches will add support for printing splats in case of BPF arena page faults, rqspinlock deadlocks, and cond_break timeouts, and integration of this facility into bpftool for dumping messages to user space.
[0]: https://lore.kernel.org/bpf/[email protected]
--- include/linux/bpf.h | 73 ++++- include/uapi/linux/bpf.h | 19 ++ kernel/bpf/Makefile | 2 +- kernel/bpf/core.c | 12 + kernel/bpf/helpers.c | 26 +- kernel/bpf/stream.c | 497 +++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 27 +- kernel/bpf/verifier.c | 5 +- tools/include/uapi/linux/bpf.h | 19 ++ 9 files changed, 659 insertions(+), 21 deletions(-) create mode 100644 kernel/bpf/stream.c
include/linux/bpf.h +72 -1
@@ -1538,6 +1538,41 @@ struct btf_mod_pair {struct bpf_kfunc_desc_tab ; +enum bpf_stream_id { +BPF_STDOUT =1 , +BPF_STDERR =2 , +}; + +struct bpf_stream_elem { +struct llist_node node; +int total_len; +int consumed_len; +char str[]; +}; + +struct bpf_stream_elem_batch { +struct llist_node *node; +}; + +enum { +BPF_STREAM_MAX_CAPACITY = (4 *1024U *1024U ), +}; + +struct bpf_stream { +enum bpf_stream_id stream_id; +atomic_t capacity; +struct llist_head log; + +rqspinlock_t lock; +struct llist_node *backlog_head; +struct llist_node *backlog_tail; +}; + +struct bpf_stream_stage { +struct llist_head log; +int len; +}; +struct bpf_prog_aux {atomic64_t refcnt;u32 used_map_cnt;
@@ -1646,6 +1681,7 @@ struct bpf_prog_aux {struct work_struct work;struct rcu_head rcu; }; +struct bpf_stream stream[2 ]; };struct bpf_prog {
@@ -2405,6 +2441,8 @@ int generic_map_delete_batch(struct bpf_map *map,struct bpf_map *bpf_map_get_curr_or_next (u32 *id);struct bpf_prog *bpf_prog_get_curr_or_next (u32 *id); + +struct page *__bpf_alloc_page (int nid);int bpf_map_alloc_pages (const struct bpf_map *map,int nid,unsigned long nr_pages,struct page **page_array);#ifdef CONFIG_MEMCG
@@ -3543,6 +3581,16 @@ bool btf_id_set_contains(const struct btf_id_set *set, u32 id);#define MAX_BPRINTF_VARARGS 12#define MAX_BPRINTF_BUF 1024 +/* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary +* arguments representation. +*/ +#define MAX_BPRINTF_BIN_ARGS 512 + +struct bpf_bprintf_buffers { +char bin_args[MAX_BPRINTF_BIN_ARGS ]; +char buf[MAX_BPRINTF_BUF ]; +}; +struct bpf_bprintf_data {u32 *bin_args;char *buf;
@@ -3550,9 +3598,32 @@ struct bpf_bprintf_data {bool get_buf; }; -int bpf_bprintf_prepare (char *fmt,u32 fmt_size,const u64 *raw_args, +int bpf_bprintf_prepare (const char *fmt,u32 fmt_size,const u64 *raw_args,u32 num_args,struct bpf_bprintf_data *data);void bpf_bprintf_cleanup (struct bpf_bprintf_data *data); +int bpf_try_get_buffers (struct bpf_bprintf_buffers **bufs); +void bpf_put_buffers (void ); + +void bpf_prog_stream_init (struct bpf_prog *prog); +void bpf_prog_stream_free (struct bpf_prog *prog); +int bpf_prog_stream_read (struct bpf_prog *prog,enum bpf_stream_id stream_id,void __user *buf,int len); +void bpf_stream_stage_init (struct bpf_stream_stage *ss); +void bpf_stream_stage_free (struct bpf_stream_stage *ss); +__printf(2, 3) +int bpf_stream_stage_printk (struct bpf_stream_stage *ss,const char *fmt, ...); +int bpf_stream_stage_commit (struct bpf_stream_stage *ss,struct bpf_prog *prog, +enum bpf_stream_id stream_id); + +#define bpf_stream_printk (...) bpf_stream_stage_printk(&__ss, __VA_ARGS__) + +#define bpf_stream_stage (prog, stream_id, expr) \ + ({ \ + struct bpf_stream_stage __ss; \ + bpf_stream_stage_init(&__ss); \ + (expr); \ + bpf_stream_stage_commit(&__ss, prog, stream_id); \ + bpf_stream_stage_free(&__ss); \ + })#ifdef CONFIG_BPF_LSM void bpf_cgroup_atype_get (u32 attach_btf_id,int cgroup_atype);
include/uapi/linux/bpf.h +19 -0
@@ -906,6 +906,17 @@ union bpf_iter_link_info {* A new file descriptor (a nonnegative integer), or -1 if an * error occurred (in which case, *errno* is set appropriately). * +* BPF_PROG_STREAM_READ_BY_FD +* Description +* Read data of a program's BPF stream. The program is identified +* by *prog_fd*, and the stream is identified by the *stream_id*. +* The data is copied to a buffer pointed to by *stream_buf*, and +* filled less than or equal to *stream_buf_len* bytes. +* +* Return +* Number of bytes read from the stream on success, or -1 if an +* error occurred (in which case, *errno* is set appropriately). +* * NOTES * eBPF objects (maps and programs) can be shared between processes. *
@@ -961,6 +972,7 @@ enum bpf_cmd {BPF_LINK_DETACH ,BPF_PROG_BIND_MAP ,BPF_TOKEN_CREATE , +BPF_PROG_STREAM_READ_BY_FD , __MAX_BPF_CMD, };
@@ -1842,6 +1854,13 @@ union bpf_attr {__u32 bpffs_fd; } token_create; +struct { +__aligned_u64 stream_buf; +__u32 stream_buf_len; +__u32 stream_id; +__u32 prog_fd; + } prog_stream_read; + } __attribute__((aligned(8)));/* The description below is an attempt at providing documentation to eBPF
kernel/bpf/Makefile +1 -1
@@ -14,7 +14,7 @@ obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o obj-${CONFIG_BPF_LSM} += bpf_inode_storage.o obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o obj-$(CONFIG_BPF_JIT) += trampoline.o -obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o +obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o ifeq ($(CONFIG_MMU)$(CONFIG_64BIT),yy) obj-$(CONFIG_BPF_SYSCALL) += arena.o range_tree.o endif
kernel/bpf/core.c +12 -0
@@ -134,6 +134,10 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flagmutex_init (&fp->aux->ext_mutex);mutex_init (&fp->aux->dst_mutex); +#ifdef CONFIG_BPF_SYSCALL +bpf_prog_stream_init (fp); +#endif +return fp; }
@@ -2861,6 +2865,7 @@ static void bpf_prog_free_deferred(struct work_struct *work) aux = container_of(work, struct bpf_prog_aux, work);#ifdef CONFIG_BPF_SYSCALL bpf_free_kfunc_btf_tab (aux->kfunc_btf_tab); +bpf_prog_stream_free (aux->prog);#endif #ifdef CONFIG_CGROUP_BPF if (aux->cgroup_atype !=CGROUP_BPF_ATTACH_TYPE_INVALID )
@@ -2877,6 +2882,13 @@ static void bpf_prog_free_deferred(struct work_struct *work)if (aux->dst_trampoline)bpf_trampoline_put (aux->dst_trampoline);for (i =0 ; i < aux->real_func_cnt; i++) { +#ifdef CONFIG_BPF_SYSCALL +/* Ensure we don't push to subprog lists. */ +if (bpf_is_subprog (aux->func[i])) { +WARN_ON_ONCE (!llist_empty (&aux->func[i]->aux->stream[0 ].log)); +WARN_ON_ONCE (!llist_empty (&aux->func[i]->aux->stream[1 ].log)); + } +#endif /* We can just unlink the subprog poke descriptor table as * it was originally linked to the main program and is also * released along with it.
kernel/bpf/helpers.c +11 -15
@@ -762,22 +762,13 @@ static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,return -EINVAL ; } -/* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary -* arguments representation. -*/ -#define MAX_BPRINTF_BIN_ARGS 512 -/* Support executing three nested bprintf helper calls on a given CPU */ #define MAX_BPRINTF_NEST_LEVEL 3 -struct bpf_bprintf_buffers { -char bin_args[MAX_BPRINTF_BIN_ARGS ]; -char buf[MAX_BPRINTF_BUF ]; -}; static DEFINE_PER_CPU(struct bpf_bprintf_buffers[MAX_BPRINTF_NEST_LEVEL], bpf_bprintf_bufs); static DEFINE_PER_CPU(int, bpf_bprintf_nest_level); -static int try_get_buffers (struct bpf_bprintf_buffers **bufs) +int bpf_try_get_buffers (struct bpf_bprintf_buffers **bufs) {int nest_level;
@@ -793,16 +784,21 @@ static int try_get_buffers(struct bpf_bprintf_buffers **bufs)return 0 ; } -void bpf_bprintf_cleanup (struct bpf_bprintf_data *data) +void bpf_put_buffers (void ) { -if (!data->bin_args && !data->buf) -return ;if (WARN_ON_ONCE (this_cpu_read (bpf_bprintf_nest_level) ==0 ))return ;this_cpu_dec (bpf_bprintf_nest_level);preempt_enable (); } +void bpf_bprintf_cleanup (struct bpf_bprintf_data *data) +{ +if (!data->bin_args && !data->buf) +return ; +bpf_put_buffers (); +} +/* * bpf_bprintf_prepare - Generic pass on format strings for bprintf-like helpers *
@@ -817,7 +813,7 @@ void bpf_bprintf_cleanup(struct bpf_bprintf_data *data)* In argument preparation mode, if 0 is returned, safe temporary buffers are * allocated and bpf_bprintf_cleanup should be called to free them after use. */ -int bpf_bprintf_prepare (char *fmt,u32 fmt_size,const u64 *raw_args, +int bpf_bprintf_prepare (const char *fmt,u32 fmt_size,const u64 *raw_args,u32 num_args,struct bpf_bprintf_data *data) {bool get_buffers = (data->get_bin_args && num_args) || data->get_buf;
@@ -833,7 +829,7 @@ int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,return -EINVAL ; fmt_size = fmt_end - fmt; -if (get_buffers &&try_get_buffers (&buffers)) +if (get_buffers &&bpf_try_get_buffers (&buffers))return -EBUSY ;if (data->get_bin_args) {
kernel/bpf/stream.c new file +497 -0
497 lines not on this page · raw message
kernel/bpf/syscall.c +26 -1
@@ -576,7 +576,7 @@ static bool can_alloc_pages(void) !IS_ENABLED (CONFIG_PREEMPT_RT ); } -static struct page *__bpf_alloc_page (int nid) +struct page *__bpf_alloc_page (int nid) {if (!can_alloc_pages ())return try_alloc_pages (nid,0 );
@@ -5794,6 +5794,28 @@ static int token_create(union bpf_attr *attr)return bpf_token_create (attr); } +#define BPF_PROG_STREAM_READ_BY_FD_LAST_FIELD prog_stream_read.prog_fd + +static int prog_stream_read (union bpf_attr *attr) +{ +char __user *buf =u64_to_user_ptr (attr->prog_stream_read.stream_buf); +u32 len = attr->prog_stream_read.stream_buf_len; +struct bpf_prog *prog; +int ret; + +if (CHECK_ATTR (BPF_PROG_STREAM_READ_BY_FD )) +return -EINVAL ; + + prog =bpf_prog_get (attr->prog_stream_read.prog_fd); +if (IS_ERR (prog)) +return PTR_ERR (prog); + + ret =bpf_prog_stream_read (prog, attr->prog_stream_read.stream_id, buf, len); +bpf_prog_put (prog); + +return ret; +} +static int __sys_bpf (enum bpf_cmd cmd,bpfptr_t uattr,unsigned int size) {union bpf_attr attr;
@@ -5930,6 +5952,9 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)case BPF_TOKEN_CREATE : err =token_create (&attr);break ; +case BPF_PROG_STREAM_READ_BY_FD : + err =prog_stream_read (&attr); +break ;default : err = -EINVAL ;break ;
kernel/bpf/verifier.c +2 -3
@@ -13882,10 +13882,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, regs[BPF_REG_0 ].type =PTR_TO_BTF_ID ; regs[BPF_REG_0 ].btf_id = ptr_type_id; -if (meta.func_id == special_kfunc_list[KF_bpf_get_kmem_cache]) +if (meta.func_id == special_kfunc_list[KF_bpf_get_kmem_cache]) { regs[BPF_REG_0 ].type |=PTR_UNTRUSTED ; - -if (is_iter_next_kfunc (&meta)) { + }else if (is_iter_next_kfunc (&meta)) {struct bpf_reg_state *cur_iter; cur_iter =get_iter_from_state (env->cur_state, &meta);
tools/include/uapi/linux/bpf.h +19 -0
@@ -906,6 +906,17 @@ union bpf_iter_link_info {* A new file descriptor (a nonnegative integer), or -1 if an * error occurred (in which case, *errno* is set appropriately). * +* BPF_PROG_STREAM_READ_BY_FD +* Description +* Read data of a program's BPF stream. The program is identified +* by *prog_fd*, and the stream is identified by the *stream_id*. +* The data is copied to a buffer pointed to by *stream_buf*, and +* filled less than or equal to *stream_buf_len* bytes. +* +* Return +* Number of bytes read from the stream on success, or -1 if an +* error occurred (in which case, *errno* is set appropriately). +* * NOTES * eBPF objects (maps and programs) can be shared between processes. *
@@ -961,6 +972,7 @@ enum bpf_cmd {BPF_LINK_DETACH ,BPF_PROG_BIND_MAP ,BPF_TOKEN_CREATE , +BPF_PROG_STREAM_READ_BY_FD , __MAX_BPF_CMD, };
@@ -1842,6 +1854,13 @@ union bpf_attr {__u32 bpffs_fd; } token_create; +struct { +__aligned_u64 stream_buf; +__u32 stream_buf_len; +__u32 stream_id; +__u32 prog_fd; + } prog_stream_read; + } __attribute__((aligned(8)));/* The description below is an attempt at providing documentation to eBPF
Signature · 1 line
2.47.1
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 01/11] bpf: Introduce BPF standard streams
Kumar Kartikeya Dwivedi <[email protected]> writes:
Overall logic seems right to me, a few comments below.
[...]
Quoted 13 lines
include/linux/bpf.h +5
-0@@ -1538,6 +1538,41 @@ struct btf_mod_pair {struct bpf_kfunc_desc_tab ; +enum bpf_stream_id { +BPF_STDOUT =1 , +BPF_STDERR =2 , +}; +
include/linux/bpf.h struct btf_mod_pair { · line 1555
Quoted 10 lines
include/linux/bpf.h +10
-0+struct bpf_stream_elem { +struct llist_node node; +int total_len; +int consumed_len; +char str[]; +}; + +struct bpf_stream_elem_batch { +struct llist_node *node; +};
This type is not used anymore.
include/linux/bpf.h struct btf_mod_pair { · line 1562
Quoted 7 lines
include/linux/bpf.h +7
-0+ +enum { +BPF_STREAM_MAX_CAPACITY = (4 *1024U *1024U ), +}; + +struct bpf_stream { +enum bpf_stream_id stream_id;
Nit: stream_id is never read, as streams are identified by a position
in the bpf_prog_aux->stream.
include/linux/bpf.h struct btf_mod_pair { · line 1568
Quoted 6 lines
include/linux/bpf.h +6
-0+atomic_t capacity; +struct llist_head log; + +rqspinlock_t lock; +struct llist_node *backlog_head; +struct llist_node *backlog_tail;
Nit: maybe add comments describing what kind of data is in the llist_{head,node}? E.g.:
atomic_t capacity;
struct llist_head log; /* list of struct bpf_stream_elem in LIFO order */
rqspinlock_t lock; /* backlog_{head,tail} lock */
struct llist_node *backlog_head; /* list of struct bpf_stream_elem in FIFO order */ struct llist_node *backlog_tail;
Quoted 7 lines
include/linux/bpf.h +7
-0+}; + +struct bpf_stream_stage { +struct llist_head log; +int len; +}; +
[...]
Quoted 5 lines
kernel/bpf/stream.c new file +0
-0
[...]
Quoted 15 lines
kernel/bpf/stream.c new file +15
-0+int bpf_stream_stage_commit (struct bpf_stream_stage *ss,struct bpf_prog *prog, +enum bpf_stream_id stream_id) +{ +struct llist_node *list, *head, *tail; +struct bpf_stream *stream; +int ret; + + stream =bpf_stream_get (stream_id, prog->aux); +if (!stream) +return -EINVAL ; + + ret =bpf_stream_consume_capacity (stream, ss->len); +if (ret) +return ret; +
kernel/bpf/stream.c line 495
Quoted 10 lines
kernel/bpf/stream.c new file +10
-0+ list =llist_del_all (&ss->log); + head = list; + +if (!list) +return 0 ; +while (llist_next (list)) { + tail =llist_next (list); + list = tail; + } +llist_add_batch (head, tail, &stream->log);
If llist_next(list) == NULL at entry tail is never assigned?
Quoted 2 lines
kernel/bpf/stream.c new file +2
-0+return 0 ; +}
[...]
Quoted 5 lines
kernel/bpf/verifier.c +0
-0
kernel/bpf/verifier.c static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 13887
Quoted 9 lines
kernel/bpf/verifier.c +2
-3@@ -13882,10 +13882,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, regs[BPF_REG_0 ].type =PTR_TO_BTF_ID ; regs[BPF_REG_0 ].btf_id = ptr_type_id; -if (meta.func_id == special_kfunc_list[KF_bpf_get_kmem_cache]) +if (meta.func_id == special_kfunc_list[KF_bpf_get_kmem_cache]) { regs[BPF_REG_0 ].type |=PTR_UNTRUSTED ; - -if (is_iter_next_kfunc (&meta)) { + }else if (is_iter_next_kfunc (&meta)) {
Nit: unrelated change?
Quoted 3 lines
kernel/bpf/verifier.c +0
-0struct bpf_reg_state *cur_iter; cur_iter =get_iter_from_state (env->cur_state, &meta);
[...]
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 01/11] bpf: Introduce BPF standard streams
Quoted 24 lines · Eduard Zingerman
Kumar Kartikeya Dwivedi <[email protected]> writes:
Overall logic seems right to me, a few comments below.
[...]
Quoted 17 lines
include/linux/bpf.h +9
-0@@ -1538,6 +1538,41 @@ struct btf_mod_pair {struct bpf_kfunc_desc_tab ; +enum bpf_stream_id { +BPF_STDOUT =1 , +BPF_STDERR =2 , +}; + +struct bpf_stream_elem { +struct llist_node node; +int total_len; +int consumed_len;
Quoted 9 lines
include/linux/bpf.h struct btf_mod_pair { · line 1555
Quoted 6 lines
include/linux/bpf.h +6
-0+char str[]; +}; + +struct bpf_stream_elem_batch { +struct llist_node *node; +};This type is not used anymore.
Will drop.
Quoted 10 lines
include/linux/bpf.h struct btf_mod_pair { · line 1562
Quoted 7 lines
include/linux/bpf.h +7
-0+ +enum { +BPF_STREAM_MAX_CAPACITY = (4 *1024U *1024U ), +}; + +struct bpf_stream { +enum bpf_stream_id stream_id;Nit:
stream_idis never read, as streams are identified by a positionin the bpf_prog_aux->stream.
Ack.
Quoted 8 lines
include/linux/bpf.h struct btf_mod_pair { · line 1568
Quoted 7 lines
include/linux/bpf.h +6
-0+atomic_t capacity; +struct llist_head log; + +rqspinlock_t lock; +struct llist_node *backlog_head; +struct llist_node *backlog_tail;
Quoted 10 lines
Nit: maybe add comments describing what kind of data is in the llist_{head,node}? E.g.:
atomic_tcapacity;
structllist_headlog; /* list of structbpf_stream_elemin LIFO order */
rqspinlock_tlock; /* backlog_{head,tail} lock */
structllist_node*backlog_head; /* list of structbpf_stream_elemin FIFO order */ structllist_node*backlog_tail;
Will do.
Quoted 37 lines
Quoted 7 lines
include/linux/bpf.h +7
-0+}; + +struct bpf_stream_stage { +struct llist_head log; +int len; +}; +[...]
Quoted 5 lines
kernel/bpf/stream.c new file +0
-0[...]
Quoted 19 lines
kernel/bpf/stream.c new file +19
-0+int bpf_stream_stage_commit (struct bpf_stream_stage *ss,struct bpf_prog *prog, +enum bpf_stream_id stream_id) +{ +struct llist_node *list, *head, *tail; +struct bpf_stream *stream; +int ret; + + stream =bpf_stream_get (stream_id, prog->aux); +if (!stream) +return -EINVAL ; + + ret =bpf_stream_consume_capacity (stream, ss->len); +if (ret) +return ret; + + list =llist_del_all (&ss->log); + head = list; + +if (!list)
Quoted 8 lines
kernel/bpf/stream.c line 495
Quoted 6 lines
kernel/bpf/stream.c new file +6
-0+return 0 ; +while (llist_next (list)) { + tail =llist_next (list); + list = tail; + } +llist_add_batch (head, tail, &stream->log);If
llist_next(list) == NULLat entrytailis never assigned?
The assumption is llist_del_all being non-NULL means llist_next is going to return a non-NULL value at least once.
Does that address your concern?
Quoted 14 lines
Quoted 3 lines
kernel/bpf/stream.c new file +2
-0+return 0 ; +}[...]
Quoted 8 lines
kernel/bpf/verifier.c +0
-0@@ -13882,10 +13882,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, regs[BPF_REG_0 ].type =PTR_TO_BTF_ID ; regs[BPF_REG_0 ].btf_id = ptr_type_id;
Quoted 8 lines
kernel/bpf/verifier.c static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 13887
Quoted 6 lines
kernel/bpf/verifier.c +2
-3-if (meta.func_id == special_kfunc_list[KF_bpf_get_kmem_cache]) +if (meta.func_id == special_kfunc_list[KF_bpf_get_kmem_cache]) { regs[BPF_REG_0 ].type |=PTR_UNTRUSTED ; - -if (is_iter_next_kfunc (&meta)) { + }else if (is_iter_next_kfunc (&meta)) {Nit: unrelated change?
Yeah, will drop.
Quoted 6 lines
Quoted 4 lines
kernel/bpf/verifier.c +0
-0struct bpf_reg_state *cur_iter; cur_iter =get_iter_from_state (env->cur_state, &meta);[...]
Thanks!
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 01/11] bpf: Introduce BPF standard streams
Kumar Kartikeya Dwivedi <[email protected]> writes:
[...]
Quoted 36 lines
Quoted 27 lines
Quoted 5 lines
kernel/bpf/stream.c new file +0
-0[...]
Quoted 19 lines
kernel/bpf/stream.c new file +19
-0+int bpf_stream_stage_commit (struct bpf_stream_stage *ss,struct bpf_prog *prog, +enum bpf_stream_id stream_id) +{ +struct llist_node *list, *head, *tail; +struct bpf_stream *stream; +int ret; + + stream =bpf_stream_get (stream_id, prog->aux); +if (!stream) +return -EINVAL ; + + ret =bpf_stream_consume_capacity (stream, ss->len); +if (ret) +return ret; + + list =llist_del_all (&ss->log); + head = list; + +if (!list)Quoted 8 lines
kernel/bpf/stream.c line 495
Quoted 6 lines
kernel/bpf/stream.c new file +6
-0+return 0 ; +while (llist_next (list)) { + tail =llist_next (list); + list = tail; + } +llist_add_batch (head, tail, &stream->log);If
llist_next(list) == NULLat entrytailis never assigned?
Quoted 3 lines
The assumption is
llist_del_allbeing non-NULL meansllist_nextis going to return a non-NULL value at least once.
Does that address your concern?
Sorry, maybe I don't understand something.
Suppose that at entry ss->log is a list with a single element:
ss->log -> 0xAA: { .next = NULL; ... payload ... }
then:
- list == 0xAA;
- llist_next(list) == 0x0;
- loop body never executes.
What do I miss?
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
kernel/bpf/stream.c new file +2
-0+return 0 ; +}
[...]
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 01/11] bpf: Introduce BPF standard streams
Quoted 48 lines · Eduard Zingerman
Kumar Kartikeya Dwivedi <[email protected]> writes:
[...]
Quoted 36 lines
Quoted 27 lines
Quoted 5 lines
kernel/bpf/stream.c new file +0
-0[...]
Quoted 19 lines
kernel/bpf/stream.c new file +19
-0+int bpf_stream_stage_commit (struct bpf_stream_stage *ss,struct bpf_prog *prog, +enum bpf_stream_id stream_id) +{ +struct llist_node *list, *head, *tail; +struct bpf_stream *stream; +int ret; + + stream =bpf_stream_get (stream_id, prog->aux); +if (!stream) +return -EINVAL ; + + ret =bpf_stream_consume_capacity (stream, ss->len); +if (ret) +return ret; + + list =llist_del_all (&ss->log); + head = list; + +if (!list)Quoted 8 lines
kernel/bpf/stream.c line 495
Quoted 6 lines
kernel/bpf/stream.c new file +6
-0+return 0 ; +while (llist_next (list)) { + tail =llist_next (list); + list = tail; + } +llist_add_batch (head, tail, &stream->log);If
llist_next(list) == NULLat entrytailis never assigned?Quoted 3 lines
The assumption is
llist_del_allbeing non-NULL meansllist_nextis going to return a non-NULL value at least once.
Does that address your concern?Sorry, maybe I don't understand something.
Suppose that at entryss->logis a list with a single element:
Quoted 8 lines
ss->log->0xAA: { .next = NULL; ... payload ... }then:
- list ==0xAA;
-llist_next(list)==0x0;
- loop body never executes.What do I miss?
Right, I see.
We need to do head = tail = list above. Then it's equivalent to a single element llist_add.
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
kernel/bpf/stream.c new file +2
-0+return 0 ; +}[...]
Re: [PATCH bpf-next v2 01/11] bpf: Introduce BPF standard streams
In reply to Kumar Kartikeya Dwivedikernel/bpf/core.c static void bpf_prog_free_deferred(struct work_struct *work) · line 2890
Quoted 8 lines · Kumar Kartikeya Dwivedi
for (i = 0; i < aux->real_func_cnt; i++) {kernel/bpf/core.c +6
-0+#ifdef CONFIG_BPF_SYSCALL +/* Ensure we don't push to subprog lists. */ +if (bpf_is_subprog (aux->func[i])) { +WARN_ON_ONCE (!llist_empty (&aux->func[i]->aux->stream[0 ].log)); +WARN_ON_ONCE (!llist_empty (&aux->func[i]->aux->stream[1 ].log)); + }
Why check for subprogs only ?
kernel/bpf/helpers.c static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype, · line 775
Quoted 11 lines
kernel/bpf/helpers.c +0
-9-/* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary -* arguments representation. -*/ -#define MAX_BPRINTF_BIN_ARGS 512 -/* Support executing three nested bprintf helper calls on a given CPU */ #define MAX_BPRINTF_NEST_LEVEL 3 -struct bpf_bprintf_buffers { -char bin_args[MAX_BPRINTF_BIN_ARGS ]; -char buf[MAX_BPRINTF_BUF ]; -};
Pls split the refactor into another patch.
Quoted 18 lines
kernel/bpf/stream.c new file +18
-0+static int bpf_stream_read (struct bpf_stream *stream,void __user *buf,int len) +{ +int rem_len = len, cons_len, ret =0 ; +struct bpf_stream_elem *elem =NULL ; +struct llist_node *node; +unsigned long flags; + +if (raw_res_spin_lock_irqsave (&stream->lock, flags)) +return -EDEADLK ; + +while (rem_len) { +int pos = len - rem_len; +bool cont; + + node =bpf_stream_backlog_peek (stream); +if (!node) { +bpf_stream_backlog_fill (stream); + node =bpf_stream_backlog_peek (stream);
kernel/bpf/stream.c line 324
Quoted 10 lines
kernel/bpf/stream.c new file +10
-0+ } +if (!node) +break ; + elem =container_of (node,typeof (*elem), node); + + cons_len = elem->consumed_len; + cont =bpf_stream_consume_elem (elem, &rem_len) == false; + + ret =copy_to_user_nofault (buf + pos, elem->str + cons_len, + elem->consumed_len - cons_len);
_nofault() because res_spin_lock() is held, right?
But this is only called from sys_bpf. It's sleepable and faultable. In v1 bpf_prog_stream_read() was callable from bpf prog, iirc, but not anymore.
Let's use proper mutex and copy_to_user() ?
Quoted 73 lines
kernel/bpf/stream.c new file +73
-0+/* Restore in case of error. */ +if (ret) { + elem->consumed_len = cons_len; +break ; + } + +if (cont) +continue ; +bpf_stream_backlog_pop (stream); +bpf_stream_release_capacity (stream, elem); +bpf_stream_free_elem (elem); + } + +raw_res_spin_unlock_irqrestore (&stream->lock, flags); +return ret ? ret : len - rem_len; +} + +int bpf_prog_stream_read (struct bpf_prog *prog,enum bpf_stream_id stream_id,void __user *buf,int len) +{ +struct bpf_stream *stream; + + stream =bpf_stream_get (stream_id, prog->aux); +if (!stream) +return -ENOENT ; +return bpf_stream_read (stream, buf, len); +} + +__bpf_kfunc_start_defs (); + +/* +* Avoid using enum bpf_stream_id so that kfunc users don't have to pull in the +* enum in headers. +*/ +__bpf_kfunc int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *args, u32 len__sz, void *aux__prog) +{ +struct bpf_bprintf_data data = { + .get_bin_args = true, + .get_buf = true, + }; +struct bpf_prog_aux *aux = aux__prog; +u32 fmt_size =strlen (fmt__str) +1 ; +struct bpf_stream *stream; +u32 data_len = len__sz; +int ret, num_args; + + stream =bpf_stream_get (stream_id, aux); +if (!stream) +return -ENOENT ; + +if (data_len &7 || data_len >MAX_BPRINTF_VARARGS *8 || + (data_len && !args)) +return -EINVAL ; + num_args = data_len /8 ; + + ret =bpf_bprintf_prepare (fmt__str, fmt_size, args, num_args, &data); +if (ret <0 ) +return ret; + + ret =bstr_printf (data.buf,MAX_BPRINTF_BUF , fmt__str, data.bin_args); +/* If the string was truncated, we only wrote until the size of buffer. */ + ret =min_t (u32, ret +1 ,MAX_BPRINTF_BUF ); + ret =bpf_stream_push_str (stream, data.buf, ret); +bpf_bprintf_cleanup (&data); + +return ret; +} + +__bpf_kfunc_end_defs (); + +BTF_KFUNCS_START(stream_kfunc_set) +BTF_ID_FLAGS(func, bpf_stream_vprintk, KF_TRUSTED_ARGS) +BTF_KFUNCS_END(stream_kfunc_set) +
kernel/bpf/stream.c line 407
Quoted 10 lines
kernel/bpf/stream.c new file +10
-0+static const struct btf_kfunc_id_set bpf_stream_kfunc_set = { + .owner =THIS_MODULE , + .set = &stream_kfunc_set, +}; + +static int __init bpf_stream_kfunc_init (void ) +{ +return register_btf_kfunc_id_set (BPF_PROG_TYPE_UNSPEC , &bpf_stream_kfunc_set); +} +late_initcall (bpf_stream_kfunc_init);
let's avoid all these little initcall-s and individual stream_kfunc_set-s.
Add bpf_stream_vprintk() to common_btf_ids[].
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 01/11] bpf: Introduce BPF standard streams
Quoted 12 lines · Alexei Starovoitov
kernel/bpf/core.c static void bpf_prog_free_deferred(struct work_struct *work) · line 2890
Quoted 8 lines · Kumar Kartikeya Dwivedi
for (i = 0; i < aux->real_func_cnt; i++) {kernel/bpf/core.c +6
-0+#ifdef CONFIG_BPF_SYSCALL +/* Ensure we don't push to subprog lists. */ +if (bpf_is_subprog (aux->func[i])) { +WARN_ON_ONCE (!llist_empty (&aux->func[i]->aux->stream[0 ].log)); +WARN_ON_ONCE (!llist_empty (&aux->func[i]->aux->stream[1 ].log)); + }
Quoted 2 lines
Why check for subprogs only ?
Stream elements should only be populated for main progs, so it would be a bug if we did it for subprogs.
I guess I can drop it.
Quoted 14 lines
kernel/bpf/helpers.c static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype, · line 775
Quoted 12 lines
kernel/bpf/helpers.c +0
-9-/* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary -* arguments representation. -*/ -#define MAX_BPRINTF_BIN_ARGS 512 -/* Support executing three nested bprintf helper calls on a given CPU */ #define MAX_BPRINTF_NEST_LEVEL 3 -struct bpf_bprintf_buffers { -char bin_args[MAX_BPRINTF_BIN_ARGS ]; -char buf[MAX_BPRINTF_BUF ]; -};Pls split the refactor into another patch.
Ack.
Quoted 27 lines
Quoted 27 lines
kernel/bpf/stream.c new file +26
-0+static int bpf_stream_read (struct bpf_stream *stream,void __user *buf,int len) +{ +int rem_len = len, cons_len, ret =0 ; +struct bpf_stream_elem *elem =NULL ; +struct llist_node *node; +unsigned long flags; + +if (raw_res_spin_lock_irqsave (&stream->lock, flags)) +return -EDEADLK ; + +while (rem_len) { +int pos = len - rem_len; +bool cont; + + node =bpf_stream_backlog_peek (stream); +if (!node) { +bpf_stream_backlog_fill (stream); + node =bpf_stream_backlog_peek (stream); + } +if (!node) +break ; + elem =container_of (node,typeof (*elem), node); + + cons_len = elem->consumed_len; + cont =bpf_stream_consume_elem (elem, &rem_len) == false; +
Quoted 8 lines
kernel/bpf/stream.c line 324
Quoted 2 lines
kernel/bpf/stream.c new file +2
-0+ ret =copy_to_user_nofault (buf + pos, elem->str + cons_len, + elem->consumed_len - cons_len);
_nofault()becauseres_spin_lock()is held, right?
But this is only called fromsys_bpf. It's sleepable and faultable. In v1bpf_prog_stream_read()was callable from bpf prog, iirc, but not anymore.
Let's use proper mutex andcopy_to_user()?
Yes, will do.
Quoted 80 lines
Quoted 80 lines
kernel/bpf/stream.c new file +79
-0+/* Restore in case of error. */ +if (ret) { + elem->consumed_len = cons_len; +break ; + } + +if (cont) +continue ; +bpf_stream_backlog_pop (stream); +bpf_stream_release_capacity (stream, elem); +bpf_stream_free_elem (elem); + } + +raw_res_spin_unlock_irqrestore (&stream->lock, flags); +return ret ? ret : len - rem_len; +} + +int bpf_prog_stream_read (struct bpf_prog *prog,enum bpf_stream_id stream_id,void __user *buf,int len) +{ +struct bpf_stream *stream; + + stream =bpf_stream_get (stream_id, prog->aux); +if (!stream) +return -ENOENT ; +return bpf_stream_read (stream, buf, len); +} + +__bpf_kfunc_start_defs (); + +/* +* Avoid using enum bpf_stream_id so that kfunc users don't have to pull in the +* enum in headers. +*/ +__bpf_kfunc int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *args, u32 len__sz, void *aux__prog) +{ +struct bpf_bprintf_data data = { + .get_bin_args = true, + .get_buf = true, + }; +struct bpf_prog_aux *aux = aux__prog; +u32 fmt_size =strlen (fmt__str) +1 ; +struct bpf_stream *stream; +u32 data_len = len__sz; +int ret, num_args; + + stream =bpf_stream_get (stream_id, aux); +if (!stream) +return -ENOENT ; + +if (data_len &7 || data_len >MAX_BPRINTF_VARARGS *8 || + (data_len && !args)) +return -EINVAL ; + num_args = data_len /8 ; + + ret =bpf_bprintf_prepare (fmt__str, fmt_size, args, num_args, &data); +if (ret <0 ) +return ret; + + ret =bstr_printf (data.buf,MAX_BPRINTF_BUF , fmt__str, data.bin_args); +/* If the string was truncated, we only wrote until the size of buffer. */ + ret =min_t (u32, ret +1 ,MAX_BPRINTF_BUF ); + ret =bpf_stream_push_str (stream, data.buf, ret); +bpf_bprintf_cleanup (&data); + +return ret; +} + +__bpf_kfunc_end_defs (); + +BTF_KFUNCS_START (stream_kfunc_set) +BTF_ID_FLAGS (func ,bpf_stream_vprintk ,KF_TRUSTED_ARGS ) +BTF_KFUNCS_END (stream_kfunc_set) + +static const struct btf_kfunc_id_set bpf_stream_kfunc_set = { + .owner =THIS_MODULE , + .set = &stream_kfunc_set, +}; + +static int __init bpf_stream_kfunc_init (void )
Quoted 8 lines
kernel/bpf/stream.c line 407
Quoted 4 lines
kernel/bpf/stream.c new file +4
-0+{ +return register_btf_kfunc_id_set (BPF_PROG_TYPE_UNSPEC , &bpf_stream_kfunc_set); +} +late_initcall (bpf_stream_kfunc_init);let's avoid all these little initcall-s and individual stream_kfunc_set-s.
Addbpf_stream_vprintk()to common_btf_ids[].
Ok, I will move it there.
Kumar Kartikeya Dwivedi
maintainerPATCH v2 2/11 bpf: Add function to extract program source info
In reply to Kumar Kartikeya DwivediPrepare a function for use in future patches that can extract the file info, line info, and the source line number for a given BPF program provided it's program counter.
Only the basename of the file path is provided, given it can be excessively long in some cases.
This will be used in later patches to print source info to the BPF stream. The source line number is indicated by the return value, and the file and line info are provided through out parameters.
--- include/linux/bpf.h | 2 ++ kernel/bpf/core.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+)
include/linux/bpf.h +2 -0
@@ -3659,4 +3659,6 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog) return prog->aux->func_idx != 0; } +int bpf_prog_get_file_line (struct bpf_prog *prog,unsigned long ip,const char **filep,const char **linep); +#endif /* _LINUX_BPF_H */
kernel/bpf/core.c +49 -0
52 lines not on this page · raw message
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v2 02/11] bpf: Add function to extract program source info
Quoted 72 lines · Kumar Kartikeya Dwivedi
Prepare a function for use in future patches that can extract the file info, line info, and the source line number for a given BPF program provided it's program counter.
Only the basename of the file path is provided, given it can be excessively long in some cases.
This will be used in later patches to print source info to the BPF stream. The source line number is indicated by the return value, and the file and line info are provided through out parameters.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/bpf.h | 2 ++ kernel/bpf/core.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+)include/linux/bpf.h +2
-0@@ -3659,4 +3659,6 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog) return prog->aux->func_idx != 0; } +int bpf_prog_get_file_line (struct bpf_prog *prog,unsigned long ip,const char **filep,const char **linep); +#endif /* _LINUX_BPF_H */ kernel/bpf/core.c +35
-0@@ -3204,3 +3204,52 @@ EXPORT_SYMBOL(bpf_stats_enabled_key);EXPORT_TRACEPOINT_SYMBOL_GPL (xdp_exception);EXPORT_TRACEPOINT_SYMBOL_GPL (xdp_bulk_tx); + +#ifdef CONFIG_BPF_SYSCALL + +int bpf_prog_get_file_line (struct bpf_prog *prog,unsigned long ip,const char **filep,const char **linep) +{ +int idx =-1 , insn_start, insn_end, len; +struct bpf_line_info *linfo; +void **jited_linfo; +struct btf *btf; + + btf = prog->aux->btf; + linfo = prog->aux->linfo; + jited_linfo = prog->aux->jited_linfo; + +if (!btf || !linfo || !prog->aux->jited_linfo) +return -EINVAL ; + len = prog->aux->func ? prog->aux->func[prog->aux->func_idx]->len : prog->len; + + linfo = &prog->aux->linfo[prog->aux->linfo_idx]; + jited_linfo = &prog->aux->jited_linfo[prog->aux->linfo_idx]; + + insn_start = linfo[0 ].insn_off; + insn_end = insn_start + len; + +for (int i =0 ; i < prog->aux->nr_linfo && + linfo[i].insn_off >= insn_start && linfo[i].insn_off < insn_end; i++) { +if (jited_linfo[i] >= (void *)ip) +break ; + idx = i; + } + +if (idx ==-1 ) +return -ENOENT ; + +/* Get base component of the file path. */
kernel/bpf/core.c EXPORT_SYMBOL(bpf_stats_enabled_key); · line 3251
Quoted 10 lines
kernel/bpf/core.c +10
-0+ *filep =btf_name_by_offset (btf, linfo[idx].file_name_off); +if (!*filep) +return -ENOENT ; + *filep =kbasename (*filep); +/* Obtain the source line, and strip whitespace in prefix. */ + *linep =btf_name_by_offset (btf, linfo[idx].line_off); +if (!*linep) +return -ENOENT ; +while (isspace (**linep)) + *linep +=1 ;
The check_btf_line() in the verifier does:
if (!btf_name_by_offset(btf, linfo[i].line_off) ||
!btf_name_by_offset(btf, linfo[i].file_name_off)) {
verbose(env, "Invalid line_info[%u].line_off
or .file_name_off\n", i);
err = -EINVAL;
goto err_free;
}and later in the verifier we do:
s = ltrim(btf_name_by_offset(btf, linfo->line_off));
verbose(env, "%s", s); /* source code line */so please drop these two checks.
kernel/bpf/core.c EXPORT_SYMBOL(bpf_stats_enabled_key); · line 3252
Quoted 1 line
kernel/bpf/core.c +1
-0+return BPF_LINE_INFO_LINE_NUM (linfo[idx].line_col);
I would return it by reference as well.
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 02/11] bpf: Add function to extract program source info
Quoted 95 lines · Alexei Starovoitov
Quoted 72 lines · Kumar Kartikeya Dwivedi
Prepare a function for use in future patches that can extract the file info, line info, and the source line number for a given BPF program provided it's program counter.
Only the basename of the file path is provided, given it can be excessively long in some cases.
This will be used in later patches to print source info to the BPF stream. The source line number is indicated by the return value, and the file and line info are provided through out parameters.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/bpf.h | 2 ++ kernel/bpf/core.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+)include/linux/bpf.h +2
-0@@ -3659,4 +3659,6 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog) return prog->aux->func_idx != 0; } +int bpf_prog_get_file_line (struct bpf_prog *prog,unsigned long ip,const char **filep,const char **linep); +#endif /* _LINUX_BPF_H */ kernel/bpf/core.c +35
-0@@ -3204,3 +3204,52 @@ EXPORT_SYMBOL(bpf_stats_enabled_key);EXPORT_TRACEPOINT_SYMBOL_GPL (xdp_exception);EXPORT_TRACEPOINT_SYMBOL_GPL (xdp_bulk_tx); + +#ifdef CONFIG_BPF_SYSCALL + +int bpf_prog_get_file_line (struct bpf_prog *prog,unsigned long ip,const char **filep,const char **linep) +{ +int idx =-1 , insn_start, insn_end, len; +struct bpf_line_info *linfo; +void **jited_linfo; +struct btf *btf; + + btf = prog->aux->btf; + linfo = prog->aux->linfo; + jited_linfo = prog->aux->jited_linfo; + +if (!btf || !linfo || !prog->aux->jited_linfo) +return -EINVAL ; + len = prog->aux->func ? prog->aux->func[prog->aux->func_idx]->len : prog->len; + + linfo = &prog->aux->linfo[prog->aux->linfo_idx]; + jited_linfo = &prog->aux->jited_linfo[prog->aux->linfo_idx]; + + insn_start = linfo[0 ].insn_off; + insn_end = insn_start + len; + +for (int i =0 ; i < prog->aux->nr_linfo && + linfo[i].insn_off >= insn_start && linfo[i].insn_off < insn_end; i++) { +if (jited_linfo[i] >= (void *)ip) +break ; + idx = i; + } + +if (idx ==-1 ) +return -ENOENT ; + +/* Get base component of the file path. */ kernel/bpf/core.c EXPORT_SYMBOL(bpf_stats_enabled_key); · line 3251
Quoted 10 lines
kernel/bpf/core.c +10
-0+ *filep =btf_name_by_offset (btf, linfo[idx].file_name_off); +if (!*filep) +return -ENOENT ; + *filep =kbasename (*filep); +/* Obtain the source line, and strip whitespace in prefix. */ + *linep =btf_name_by_offset (btf, linfo[idx].line_off); +if (!*linep) +return -ENOENT ; +while (isspace (**linep)) + *linep +=1 ;The
check_btf_line()in the verifier does:if (!btf_name_by_offset(btf, linfo[i].line_off) || !btf_name_by_offset(btf, linfo[i].file_name_off)) { verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); err = -EINVAL; goto err_free; }
Quoted 5 lines
and later in the verifier we do:
s = ltrim(btf_name_by_offset(btf, linfo->line_off)); verbose(env, "%s", s); /* source code line */so please drop these two checks.
There was a kernel crash in CI when these NULL checks were missing.
Quoted 4 lines
kernel/bpf/core.c EXPORT_SYMBOL(bpf_stats_enabled_key); · line 3252
Quoted 2 lines
kernel/bpf/core.c +1
-0+return BPF_LINE_INFO_LINE_NUM (linfo[idx].line_col);I would return it by reference as well.
Ok.
Re: [PATCH bpf-next v2 02/11] bpf: Add function to extract program source info
Quoted 98 lines · Kumar Kartikeya Dwivedi
Quoted 95 lines · Alexei Starovoitov
Quoted 72 lines · Kumar Kartikeya Dwivedi
Prepare a function for use in future patches that can extract the file info, line info, and the source line number for a given BPF program provided it's program counter.
Only the basename of the file path is provided, given it can be excessively long in some cases.
This will be used in later patches to print source info to the BPF stream. The source line number is indicated by the return value, and the file and line info are provided through out parameters.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/bpf.h | 2 ++ kernel/bpf/core.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+)include/linux/bpf.h +2
-0@@ -3659,4 +3659,6 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog) return prog->aux->func_idx != 0; } +int bpf_prog_get_file_line (struct bpf_prog *prog,unsigned long ip,const char **filep,const char **linep); +#endif /* _LINUX_BPF_H */ kernel/bpf/core.c +35
-0@@ -3204,3 +3204,52 @@ EXPORT_SYMBOL(bpf_stats_enabled_key);EXPORT_TRACEPOINT_SYMBOL_GPL (xdp_exception);EXPORT_TRACEPOINT_SYMBOL_GPL (xdp_bulk_tx); + +#ifdef CONFIG_BPF_SYSCALL + +int bpf_prog_get_file_line (struct bpf_prog *prog,unsigned long ip,const char **filep,const char **linep) +{ +int idx =-1 , insn_start, insn_end, len; +struct bpf_line_info *linfo; +void **jited_linfo; +struct btf *btf; + + btf = prog->aux->btf; + linfo = prog->aux->linfo; + jited_linfo = prog->aux->jited_linfo; + +if (!btf || !linfo || !prog->aux->jited_linfo) +return -EINVAL ; + len = prog->aux->func ? prog->aux->func[prog->aux->func_idx]->len : prog->len; + + linfo = &prog->aux->linfo[prog->aux->linfo_idx]; + jited_linfo = &prog->aux->jited_linfo[prog->aux->linfo_idx]; + + insn_start = linfo[0 ].insn_off; + insn_end = insn_start + len; + +for (int i =0 ; i < prog->aux->nr_linfo && + linfo[i].insn_off >= insn_start && linfo[i].insn_off < insn_end; i++) { +if (jited_linfo[i] >= (void *)ip) +break ; + idx = i; + } + +if (idx ==-1 ) +return -ENOENT ; + +/* Get base component of the file path. */ kernel/bpf/core.c EXPORT_SYMBOL(bpf_stats_enabled_key); · line 3251
Quoted 10 lines
kernel/bpf/core.c +10
-0+ *filep =btf_name_by_offset (btf, linfo[idx].file_name_off); +if (!*filep) +return -ENOENT ; + *filep =kbasename (*filep); +/* Obtain the source line, and strip whitespace in prefix. */ + *linep =btf_name_by_offset (btf, linfo[idx].line_off); +if (!*linep) +return -ENOENT ; +while (isspace (**linep)) + *linep +=1 ;The
check_btf_line()in the verifier does:if (!btf_name_by_offset(btf, linfo[i].line_off) || !btf_name_by_offset(btf, linfo[i].file_name_off)) { verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); err = -EINVAL; goto err_free; }
Quoted 7 lines
Quoted 5 lines
and later in the verifier we do:
s = ltrim(btf_name_by_offset(btf, linfo->line_off)); verbose(env, "%s", s); /* source code line */so please drop these two checks.
There was a kernel crash in CI when these NULL checks were missing.
The math before is probably wrong then.
idx is invalid ?
This needs more debugging.
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 02/11] bpf: Add function to extract program source info
Quoted 104 lines · Alexei Starovoitov
Quoted 102 lines · Kumar Kartikeya Dwivedi
Quoted 99 lines · Alexei Starovoitov
Quoted 72 lines · Kumar Kartikeya Dwivedi
Prepare a function for use in future patches that can extract the file info, line info, and the source line number for a given BPF program provided it's program counter.
Only the basename of the file path is provided, given it can be excessively long in some cases.
This will be used in later patches to print source info to the BPF stream. The source line number is indicated by the return value, and the file and line info are provided through out parameters.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/bpf.h | 2 ++ kernel/bpf/core.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+)include/linux/bpf.h +2
-0@@ -3659,4 +3659,6 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog) return prog->aux->func_idx != 0; } +int bpf_prog_get_file_line (struct bpf_prog *prog,unsigned long ip,const char **filep,const char **linep); +#endif /* _LINUX_BPF_H */ kernel/bpf/core.c +35
-0@@ -3204,3 +3204,52 @@ EXPORT_SYMBOL(bpf_stats_enabled_key);EXPORT_TRACEPOINT_SYMBOL_GPL (xdp_exception);EXPORT_TRACEPOINT_SYMBOL_GPL (xdp_bulk_tx); + +#ifdef CONFIG_BPF_SYSCALL + +int bpf_prog_get_file_line (struct bpf_prog *prog,unsigned long ip,const char **filep,const char **linep) +{ +int idx =-1 , insn_start, insn_end, len; +struct bpf_line_info *linfo; +void **jited_linfo; +struct btf *btf; + + btf = prog->aux->btf; + linfo = prog->aux->linfo; + jited_linfo = prog->aux->jited_linfo; + +if (!btf || !linfo || !prog->aux->jited_linfo) +return -EINVAL ; + len = prog->aux->func ? prog->aux->func[prog->aux->func_idx]->len : prog->len; + + linfo = &prog->aux->linfo[prog->aux->linfo_idx]; + jited_linfo = &prog->aux->jited_linfo[prog->aux->linfo_idx]; + + insn_start = linfo[0 ].insn_off; + insn_end = insn_start + len; + +for (int i =0 ; i < prog->aux->nr_linfo && + linfo[i].insn_off >= insn_start && linfo[i].insn_off < insn_end; i++) { +if (jited_linfo[i] >= (void *)ip) +break ; + idx = i; + } + +if (idx ==-1 ) +return -ENOENT ; + +/* Get base component of the file path. */ kernel/bpf/core.c EXPORT_SYMBOL(bpf_stats_enabled_key); · line 3251
Quoted 10 lines
kernel/bpf/core.c +10
-0+ *filep =btf_name_by_offset (btf, linfo[idx].file_name_off); +if (!*filep) +return -ENOENT ; + *filep =kbasename (*filep); +/* Obtain the source line, and strip whitespace in prefix. */ + *linep =btf_name_by_offset (btf, linfo[idx].line_off); +if (!*linep) +return -ENOENT ; +while (isspace (**linep)) + *linep +=1 ;The
check_btf_line()in the verifier does:if (!btf_name_by_offset(btf, linfo[i].line_off) || !btf_name_by_offset(btf, linfo[i].file_name_off)) { verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); err = -EINVAL; goto err_free; }and later in the verifier we do:
s = ltrim(btf_name_by_offset(btf, linfo->line_off)); verbose(env, "%s", s); /* source code line */
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
so please drop these two checks.
There was a kernel crash in CI when these NULL checks were missing.
The math before is probably wrong then.
idx is invalid ?
This needs more debugging.
I will take a look.
Kumar Kartikeya Dwivedi
maintainerPATCH v2 3/11 bpf: Add function to find program from stack trace
In reply to Kumar Kartikeya DwivediIn preparation of figuring out the closest program that led to the current point in the kernel, implement a function that scans through the stack trace and finds out the closest BPF program when walking down the stack trace.
Special care needs to be taken to skip over kernel and BPF subprog frames. We basically scan until we find a BPF main prog frame. The assumption is that if a program calls into us transitively, we'll hit it along the way. If not, we end up returning NULL.
Contextually the function will be used in places where we know the program may have called into us.
Due to reliance on arch_bpf_stack_walk(), this function only works on x86 with CONFIG_UNWINDER_ORC, arm64, and s390. Remove the warning from arch_bpf_stack_walk as well since we call it outside bpf_throw() context.
--- arch/x86/net/bpf_jit_comp.c | 1 - include/linux/bpf.h | 1 + kernel/bpf/core.c | 26 ++++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-)
arch/x86/net/bpf_jit_comp.c +0 -1
@@ -3791,7 +3791,6 @@ void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp }return ;#endif -WARN (1 ,"verification of programs using bpf_throw should have failed\n" ); } void bpf_arch_poke_desc_update(struct bpf_jit_poke_descriptor *poke,
include/linux/bpf.h +1 -0
@@ -3660,5 +3660,6 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog) }int bpf_prog_get_file_line (struct bpf_prog *prog,unsigned long ip,const char **filep,const char **linep); +struct bpf_prog *bpf_prog_find_from_stack (void );#endif /* _LINUX_BPF_H */
kernel/bpf/core.c +26 -0
@@ -3252,4 +3252,30 @@ int bpf_prog_get_file_line(struct bpf_prog *prog, unsigned long ip, const char * return BPF_LINE_INFO_LINE_NUM(linfo[idx].line_col); } +struct walk_stack_ctx { +struct bpf_prog *prog; +}; + +static bool find_from_stack_cb (void *cookie,u64 ip,u64 sp,u64 bp) +{ +struct walk_stack_ctx *ctxp = cookie; +struct bpf_prog *prog; + + prog =bpf_prog_ksym_find (ip); +if (!prog) +return true; +if (bpf_is_subprog (prog)) +return true; + ctxp->prog = prog; +return false; +} + +struct bpf_prog *bpf_prog_find_from_stack (void ) +{ +struct walk_stack_ctx ctx = {}; + +arch_bpf_stack_walk (find_from_stack_cb, &ctx); +return ctx.prog; +} +#endif
Signature · 1 line
2.47.1
Kumar Kartikeya Dwivedi
maintainerPATCH v2 4/11 bpf: Hold RCU read lock in bpf_prog_ksym_find
In reply to Kumar Kartikeya DwivediThe bpf_ksym_find must be called with RCU read protection, wrap the call to bpf_ksym_find in bpf_prog_ksym_find with RCU read lock so that callers do not have to care about holding it specifically.
--- kernel/bpf/core.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/bpf/core.c +5 -1
@@ -782,7 +782,11 @@ bool is_bpf_text_address(unsigned long addr)struct bpf_prog *bpf_prog_ksym_find (unsigned long addr) { -struct bpf_ksym *ksym =bpf_ksym_find (addr); +struct bpf_ksym *ksym; + +rcu_read_lock (); + ksym =bpf_ksym_find (addr); +rcu_read_unlock ();return ksym && ksym->prog ? container_of(ksym, struct bpf_prog_aux, ksym)->prog :
Signature · 1 line
2.47.1
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 04/11] bpf: Hold RCU read lock in bpf_prog_ksym_find
Quoted 17 lines · Kumar Kartikeya Dwivedi
The
bpf_ksym_findmust be called with RCU read protection, wrap the call tobpf_ksym_findinbpf_prog_ksym_findwith RCU read lock so that callers do not have to care about holding it specifically.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- kernel/bpf/core.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)kernel/bpf/core.c +0
-0@@ -782,7 +782,11 @@ bool is_bpf_text_address(unsigned long addr)struct bpf_prog *bpf_prog_ksym_find (unsigned long addr)
kernel/bpf/core.c bool is_bpf_text_address(unsigned long addr) · line 792
Quoted 10 lines
kernel/bpf/core.c +5
-1{ -struct bpf_ksym *ksym =bpf_ksym_find (addr); +struct bpf_ksym *ksym; + +rcu_read_lock (); + ksym =bpf_ksym_find (addr); +rcu_read_unlock ();return ksym && ksym->prog ? container_of(ksym, struct bpf_prog_aux, ksym)->prog :
This isn't right, we need to have the read section open around ksym access as well.
We can end the section and return the prog pointer.
The caller is responsible to ensure prog outlives RCU protection, or otherwise hold it if necessary for prog's lifetime.
We're using this to pick programs who have an active stack frame, so they aren't going away.
But the ksym access itself needs to happen under correct protection.
I can fix it in a respin, whatever is best.
Quoted 3 lines
--
2.47.1
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 04/11] bpf: Hold RCU read lock in bpf_prog_ksym_find
Kumar Kartikeya Dwivedi <[email protected]> writes:
[...]
Quoted 22 lines
Quoted 5 lines
kernel/bpf/core.c +0
-0@@ -782,7 +782,11 @@ bool is_bpf_text_address(unsigned long addr)struct bpf_prog *bpf_prog_ksym_find (unsigned long addr)kernel/bpf/core.c bool is_bpf_text_address(unsigned long addr) · line 792
Quoted 10 lines
kernel/bpf/core.c +5
-1{ -struct bpf_ksym *ksym =bpf_ksym_find (addr); +struct bpf_ksym *ksym; + +rcu_read_lock (); + ksym =bpf_ksym_find (addr); +rcu_read_unlock ();return ksym && ksym->prog ? container_of(ksym, struct bpf_prog_aux, ksym)->prog :This isn't right, we need to have the read section open around ksym access as well.
We can end the section and return the prog pointer.
The caller is responsible to ensure prog outlives RCU protection, or otherwise hold it if necessary for prog's lifetime.
Quoted 5 lines
We're using this to pick programs who have an active stack frame, so they aren't going away.
But the ksym access itself needs to happen under correct protection.I can fix it in a respin, whatever is best.
Are rcu_read_{lock,unlock} necessary in core.c:search_bpf_extables() after this change?
Also, helpers.c:bpf_stack_walker.c does not have lock/unlock in it,
this patch needs a fixes tag for commit f18b03fabaa9 ("bpf: Implement BPF exceptions")?
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 04/11] bpf: Hold RCU read lock in bpf_prog_ksym_find
Quoted 27 lines · Eduard Zingerman
Kumar Kartikeya Dwivedi <[email protected]> writes:
[...]
Quoted 22 lines
Quoted 5 lines
kernel/bpf/core.c +0
-0@@ -782,7 +782,11 @@ bool is_bpf_text_address(unsigned long addr)struct bpf_prog *bpf_prog_ksym_find (unsigned long addr)kernel/bpf/core.c bool is_bpf_text_address(unsigned long addr) · line 792
Quoted 10 lines
kernel/bpf/core.c +5
-1{ -struct bpf_ksym *ksym =bpf_ksym_find (addr); +struct bpf_ksym *ksym; + +rcu_read_lock (); + ksym =bpf_ksym_find (addr); +rcu_read_unlock ();return ksym && ksym->prog ? container_of(ksym, struct bpf_prog_aux, ksym)->prog :This isn't right, we need to have the read section open around ksym access as well.
We can end the section and return the prog pointer.
The caller is responsible to ensure prog outlives RCU protection, or otherwise hold it if necessary for prog's lifetime.
Quoted 8 lines
Quoted 5 lines
We're using this to pick programs who have an active stack frame, so they aren't going away.
But the ksym access itself needs to happen under correct protection.I can fix it in a respin, whatever is best.
Are rcu_read_{lock,unlock} necessary in core.c:search_bpf_extables() after this change?
Don't think so, in general, it would be necessary to hold the RCU read lock to make sure the program does not go away.
There is a requirement to hold it to traverse the latch tree itself, and then for accesses on the prog require its lifetime to be valid. Except in case of search_bpf_extables (and all other current users), the prog being found should have an active frame on that CPU, so it's not going away.
Alternatively, we can add WARN_ON_ONCE(rcu_read_lock_held()) as a requirement.
Quoted 2 lines
Also, helpers.c:bpf_stack_walker.c does not have lock/unlock in it,
this patch needs a fixes tag for commitf18b03fabaa9("bpf: Implement BPF exceptions")?
So far this is probably not a bug because programs run in an RCU read section already (perhaps it is a problem in case of sleepable ones). It felt odd to me while looking at the code.
I feel like the rcu_read_lock being dropped and then returning a prog pointer will be a bit confusing and a footgun, so it might be better to go with WARN_ON_ONCE.
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 04/11] bpf: Hold RCU read lock in bpf_prog_ksym_find
Kumar Kartikeya Dwivedi <[email protected]> writes:
[...]
Quoted 1 line
Alternatively, we can add WARN_ON_ONCE(rcu_read_lock_held()) as a requirement.
I think this is a good option.
[...]
Kumar Kartikeya Dwivedi
maintainerPATCH v2 5/11 bpf: Add dump_stack() analogue to print to BPF stderr
In reply to Kumar Kartikeya DwivediIntroduce a kernel function which is the analogue of dump_stack() printing some useful information and the stack trace. This is not exposed to BPF programs yet, but can be made available in the future.
When we have a program counter for a BPF program in the stack trace, also additionally output the filename and line number to make the trace helpful. The rest of the trace can be passed into ./decode_stacktrace.sh to obtain the line numbers for kernel symbols.
--- include/linux/bpf.h | 2 ++ kernel/bpf/stream.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+)
include/linux/bpf.h +2 -0
@@ -3613,8 +3613,10 @@ __printf(2, 3)int bpf_stream_stage_printk (struct bpf_stream_stage *ss,const char *fmt, ...);int bpf_stream_stage_commit (struct bpf_stream_stage *ss,struct bpf_prog *prog,enum bpf_stream_id stream_id); +int bpf_stream_stage_dump_stack (struct bpf_stream_stage *ss);#define bpf_stream_printk (...) bpf_stream_stage_printk(&__ss, __VA_ARGS__) +#define bpf_stream_dump_stack () bpf_stream_stage_dump_stack(&__ss)#define bpf_stream_stage (prog, stream_id, expr) \ ({ \
kernel/bpf/stream.c +42 -0
51 lines not on this page · raw message
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v2 05/11] bpf: Add dump_stack() analogue to print to BPF stderr
Kumar Kartikeya Dwivedi <[email protected]> writes:
Could you please modify one of the selftests to check lines reported by dump stack?
[...]
include/linux/bpf.h __printf(2, 3) · line 3619
Quoted 12 lines
include/linux/bpf.h +2
-0@@ -3613,8 +3613,10 @@ __printf(2, 3)int bpf_stream_stage_printk (struct bpf_stream_stage *ss,const char *fmt, ...);int bpf_stream_stage_commit (struct bpf_stream_stage *ss,struct bpf_prog *prog,enum bpf_stream_id stream_id); +int bpf_stream_stage_dump_stack (struct bpf_stream_stage *ss);#define bpf_stream_printk (...) bpf_stream_stage_printk(&__ss, __VA_ARGS__) +#define bpf_stream_dump_stack () bpf_stream_stage_dump_stack(&__ss)
I don't think we should add macro with hard-coded variable names (__ss) in common headers.
[...]
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 05/11] bpf: Add dump_stack() analogue to print to BPF stderr
Quoted 5 lines · Eduard Zingerman
Kumar Kartikeya Dwivedi <[email protected]> writes:
Could you please modify one of the selftests to check lines reported by dump stack?
Ok, will try to do it with a regex pattern to match the overall layout.
Quoted 15 lines
Acked-by Eduard Zingerman <[email protected]>[...]
Quoted 10 lines
include/linux/bpf.h +1
-0@@ -3613,8 +3613,10 @@ __printf(2, 3)int bpf_stream_stage_printk (struct bpf_stream_stage *ss,const char *fmt, ...);int bpf_stream_stage_commit (struct bpf_stream_stage *ss,struct bpf_prog *prog,enum bpf_stream_id stream_id); +int bpf_stream_stage_dump_stack (struct bpf_stream_stage *ss);
Quoted 5 lines
include/linux/bpf.h __printf(2, 3) · line 3619
Quoted 2 lines
include/linux/bpf.h +1
-0#define bpf_stream_printk (...) bpf_stream_stage_printk(&__ss, __VA_ARGS__) +#define bpf_stream_dump_stack () bpf_stream_stage_dump_stack(&__ss)I don't think we should add macro with hard-coded variable names (
__ss) in common headers.
Hm, right. But this is supposed to be used within the stream stage block, and we have __i variables in macros that wrap around loops etc., hence the double / triple underscore to not conflict. Anyhow, I'm open to other suggestions.
Quoted 2 lines
[...]
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 05/11] bpf: Add dump_stack() analogue to print to BPF stderr
Kumar Kartikeya Dwivedi <[email protected]> writes:
[...]
Quoted 16 lines
Quoted 10 lines
Quoted 10 lines
include/linux/bpf.h +1
-0@@ -3613,8 +3613,10 @@ __printf(2, 3)int bpf_stream_stage_printk (struct bpf_stream_stage *ss,const char *fmt, ...);int bpf_stream_stage_commit (struct bpf_stream_stage *ss,struct bpf_prog *prog,enum bpf_stream_id stream_id); +int bpf_stream_stage_dump_stack (struct bpf_stream_stage *ss);Quoted 5 lines
include/linux/bpf.h __printf(2, 3) · line 3619
Quoted 2 lines
include/linux/bpf.h +1
-0#define bpf_stream_printk (...) bpf_stream_stage_printk(&__ss, __VA_ARGS__) +#define bpf_stream_dump_stack () bpf_stream_stage_dump_stack(&__ss)I don't think we should add macro with hard-coded variable names (
__ss) in common headers.
Quoted 4 lines
Hm, right. But this is supposed to be used within the stream stage block, and we have
__ivariables in macros that wrap around loops etc., hence the double / triple underscore to not conflict. Anyhow, I'm open to other suggestions.
I missed that this macro is supposed to be used only inside bpf_stream_stage. Nothing pretty comes to mind, I thought about using cleanup.h:DEFINE_CLASS, but that thing is ugly in its own way.
Kumar Kartikeya Dwivedi
maintainerPATCH v2 6/11 bpf: Report may_goto timeout to BPF stderr
In reply to Kumar Kartikeya DwivediBegin reporting may_goto timeouts to BPF program's stderr stream. Make sure that we don't end up spamming too many errors if the program keeps failing repeatedly and filling up the stream, hence emit at most 512 error messages from the kernel for a given stream.
--- include/linux/bpf.h | 21 ++++++++++++++------- kernel/bpf/core.c | 19 ++++++++++++++++++- kernel/bpf/stream.c | 5 +++++ 3 files changed, 37 insertions(+), 8 deletions(-)
include/linux/bpf.h +14 -7
@@ -1682,6 +1682,7 @@ struct bpf_prog_aux {struct rcu_head rcu; };struct bpf_stream stream[2 ]; +atomic_t stream_error_cnt; };struct bpf_prog {
@@ -3604,6 +3605,8 @@ void bpf_bprintf_cleanup(struct bpf_bprintf_data *data);int bpf_try_get_buffers (struct bpf_bprintf_buffers **bufs);void bpf_put_buffers (void ); +#define BPF_PROG_STREAM_ERROR_CNT 512 +void bpf_prog_stream_init (struct bpf_prog *prog);void bpf_prog_stream_free (struct bpf_prog *prog);int bpf_prog_stream_read (struct bpf_prog *prog,enum bpf_stream_id stream_id,void __user *buf,int len);
@@ -3615,16 +3618,20 @@ int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog, enum bpf_stream_id stream_id);int bpf_stream_stage_dump_stack (struct bpf_stream_stage *ss); +bool bpf_prog_stream_error_limit (struct bpf_prog *prog); +#define bpf_stream_printk (...) bpf_stream_stage_printk(&__ss, __VA_ARGS__)#define bpf_stream_dump_stack () bpf_stream_stage_dump_stack(&__ss) -#define bpf_stream_stage (prog, stream_id, expr) \ - ({ \ - struct bpf_stream_stage __ss; \ - bpf_stream_stage_init(&__ss); \ - (expr); \ - bpf_stream_stage_commit(&__ss, prog, stream_id); \ - bpf_stream_stage_free(&__ss); \ +#define bpf_stream_stage (prog, stream_id, expr) \ + ({ \ + struct bpf_stream_stage __ss; \ + if (!bpf_prog_stream_error_limit(prog)) { \ + bpf_stream_stage_init(&__ss); \ + (expr); \ + bpf_stream_stage_commit(&__ss, prog, stream_id); \ + bpf_stream_stage_free(&__ss); \ + } \ })#ifdef CONFIG_BPF_LSM
kernel/bpf/core.c +18 -1
@@ -3160,6 +3160,21 @@ u64 __weak arch_bpf_timed_may_goto(void)return 0 ; } +static noinline void bpf_prog_report_may_goto_violation(void) +{ +#ifdef CONFIG_BPF_SYSCALL +struct bpf_prog *prog; + + prog =bpf_prog_find_from_stack (); +if (!prog) +return ; +bpf_stream_stage (prog,BPF_STDERR , ({ +bpf_stream_printk ("ERROR: Timeout detected for may_goto instruction\n" ); +bpf_stream_dump_stack (); + })); +#endif +} +u64 bpf_check_timed_may_goto (struct bpf_timed_may_goto *p) {u64 time =ktime_get_mono_fast_ns ();
@@ -3170,8 +3185,10 @@ u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *p)return BPF_MAX_TIMED_LOOPS ; }/* Check if we've exhausted our time slice, and zero count. */ -if (time - p->timestamp >= (NSEC_PER_SEC /4 )) +if (unlikely (time - p->timestamp >= (NSEC_PER_SEC /4 ))) { +bpf_prog_report_may_goto_violation ();return 0 ; + }/* Refresh the count for the stack frame. */ return BPF_MAX_TIMED_LOOPS ; }
kernel/bpf/stream.c +5 -0
@@ -537,3 +537,8 @@ int bpf_stream_stage_dump_stack(struct bpf_stream_stage *ss) ret = ret ?: ctx.err;return ret ?:bpf_stream_stage_printk (ss,"\n" ); } + +bool bpf_prog_stream_error_limit (struct bpf_prog *prog) +{ +return atomic_fetch_add (1 , &prog->aux->stream_error_cnt) >=BPF_PROG_STREAM_ERROR_CNT ; +}
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v2 06/11] bpf: Report may_goto timeout to BPF stderr
Quoted 51 lines · Kumar Kartikeya Dwivedi
Begin reporting
may_gototimeouts to BPF program's stderr stream. Make sure that we don't end up spamming too many errors if the program keeps failing repeatedly and filling up the stream, hence emit at most 512 error messages from the kernel for a given stream.Acked-by Eduard Zingerman <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/bpf.h | 21 ++++++++++++++------- kernel/bpf/core.c | 19 ++++++++++++++++++- kernel/bpf/stream.c | 5 +++++ 3 files changed, 37 insertions(+), 8 deletions(-)include/linux/bpf.h +5
-7@@ -1682,6 +1682,7 @@ struct bpf_prog_aux {struct rcu_head rcu; };struct bpf_stream stream[2 ]; +atomic_t stream_error_cnt; };struct bpf_prog {@@ -3604,6 +3605,8 @@ void bpf_bprintf_cleanup(struct bpf_bprintf_data *data);int bpf_try_get_buffers (struct bpf_bprintf_buffers **bufs);void bpf_put_buffers (void ); +#define BPF_PROG_STREAM_ERROR_CNT 512 +void bpf_prog_stream_init (struct bpf_prog *prog);void bpf_prog_stream_free (struct bpf_prog *prog);int bpf_prog_stream_read (struct bpf_prog *prog,enum bpf_stream_id stream_id,void __user *buf,int len);@@ -3615,16 +3618,20 @@ int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog, enum bpf_stream_id stream_id);int bpf_stream_stage_dump_stack (struct bpf_stream_stage *ss); +bool bpf_prog_stream_error_limit (struct bpf_prog *prog); +#define bpf_stream_printk (...) bpf_stream_stage_printk(&__ss, __VA_ARGS__)#define bpf_stream_dump_stack () bpf_stream_stage_dump_stack(&__ss) -#define bpf_stream_stage (prog, stream_id, expr) \ - ({ \ - struct bpf_stream_stage __ss; \ - bpf_stream_stage_init(&__ss); \ - (expr); \ - bpf_stream_stage_commit(&__ss, prog, stream_id); \ - bpf_stream_stage_free(&__ss); \
include/linux/bpf.h int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog, · line 3635
Quoted 10 lines
include/linux/bpf.h +9
-0+#define bpf_stream_stage (prog, stream_id, expr) \ + ({ \ + struct bpf_stream_stage __ss; \ + if (!bpf_prog_stream_error_limit(prog)) { \ + bpf_stream_stage_init(&__ss); \ + (expr); \ + bpf_stream_stage_commit(&__ss, prog, stream_id); \ + bpf_stream_stage_free(&__ss); \ + } \ })
I think this part can be in the macro in some prior patch from the start to avoid the churn.
To me it's easier to review all key things about it in one go.
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 06/11] bpf: Report may_goto timeout to BPF stderr
Quoted 60 lines · Alexei Starovoitov
Quoted 57 lines · Kumar Kartikeya Dwivedi
Begin reporting
may_gototimeouts to BPF program's stderr stream. Make sure that we don't end up spamming too many errors if the program keeps failing repeatedly and filling up the stream, hence emit at most 512 error messages from the kernel for a given stream.Acked-by Eduard Zingerman <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/bpf.h | 21 ++++++++++++++------- kernel/bpf/core.c | 19 ++++++++++++++++++- kernel/bpf/stream.c | 5 +++++ 3 files changed, 37 insertions(+), 8 deletions(-)include/linux/bpf.h +11
-7@@ -1682,6 +1682,7 @@ struct bpf_prog_aux {struct rcu_head rcu; };struct bpf_stream stream[2 ]; +atomic_t stream_error_cnt; };struct bpf_prog {@@ -3604,6 +3605,8 @@ void bpf_bprintf_cleanup(struct bpf_bprintf_data *data);int bpf_try_get_buffers (struct bpf_bprintf_buffers **bufs);void bpf_put_buffers (void ); +#define BPF_PROG_STREAM_ERROR_CNT 512 +void bpf_prog_stream_init (struct bpf_prog *prog);void bpf_prog_stream_free (struct bpf_prog *prog);int bpf_prog_stream_read (struct bpf_prog *prog,enum bpf_stream_id stream_id,void __user *buf,int len);@@ -3615,16 +3618,20 @@ int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog, enum bpf_stream_id stream_id);int bpf_stream_stage_dump_stack (struct bpf_stream_stage *ss); +bool bpf_prog_stream_error_limit (struct bpf_prog *prog); +#define bpf_stream_printk (...) bpf_stream_stage_printk(&__ss, __VA_ARGS__)#define bpf_stream_dump_stack () bpf_stream_stage_dump_stack(&__ss) -#define bpf_stream_stage (prog, stream_id, expr) \ - ({ \ - struct bpf_stream_stage __ss; \ - bpf_stream_stage_init(&__ss); \ - (expr); \ - bpf_stream_stage_commit(&__ss, prog, stream_id); \ - bpf_stream_stage_free(&__ss); \ +#define bpf_stream_stage (prog, stream_id, expr) \ + ({ \ + struct bpf_stream_stage __ss; \ + if (!bpf_prog_stream_error_limit(prog)) { \ + bpf_stream_stage_init(&__ss); \ + (expr); \
Quoted 8 lines
include/linux/bpf.h int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog, · line 3635
Quoted 4 lines
include/linux/bpf.h +3
-0+bpf_stream_stage_commit (&__ss, prog, stream_id); \ +bpf_stream_stage_free (&__ss); \ + } \ })I think this part can be in the macro in some prior patch from the start to avoid the churn.
To me it's easier to review all key things about it in one go.
Ok, will move it.
Kumar Kartikeya Dwivedi
maintainerPATCH v2 7/11 bpf: Report rqspinlock deadlocks/timeout to BPF stderr
In reply to Kumar Kartikeya DwivediBegin reporting rqspinlock deadlocks and timeout to BPF program's stderr.
--- kernel/bpf/rqspinlock.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
kernel/bpf/rqspinlock.c +22 -0
@@ -666,6 +666,26 @@ EXPORT_SYMBOL_GPL(resilient_queued_spin_lock_slowpath);__bpf_kfunc_start_defs (); +static void bpf_prog_report_rqspinlock_violation (const char *str,void *lock,bool irqsave) +{ +struct rqspinlock_held *rqh =this_cpu_ptr (&rqspinlock_held_locks); +struct bpf_prog *prog; + + prog =bpf_prog_find_from_stack (); +if (!prog) +return ; +bpf_stream_stage (prog,BPF_STDERR , ({ +bpf_stream_printk ("ERROR: %s for bpf_res_spin_lock%s\n" , str, irqsave ?"_irqsave" :"" ); +bpf_stream_printk ("Attempted lock = 0x%px\n" , lock); +bpf_stream_printk ("Total held locks = %d\n" , rqh->cnt); +for (int i =0 ; i <min (RES_NR_HELD , rqh->cnt); i++) +bpf_stream_printk ("Held lock[%2d] = 0x%px\n" , i, rqh->locks[i]); +bpf_stream_dump_stack (); + })); +} + +#define REPORT_STR (ret) ({ (ret) == -ETIMEDOUT ? "Timeout detected" : "AA or ABBA deadlock detected"; }) + __bpf_kfunc int bpf_res_spin_lock(struct bpf_res_spin_lock *lock) {int ret;
@@ -676,6 +696,7 @@ __bpf_kfunc int bpf_res_spin_lock(struct bpf_res_spin_lock *lock)preempt_disable (); ret =res_spin_lock ((rqspinlock_t *)lock);if (unlikely (ret)) { +bpf_prog_report_rqspinlock_violation (REPORT_STR (ret), lock, false);preempt_enable ();return ret; }
@@ -698,6 +719,7 @@ __bpf_kfunc int bpf_res_spin_lock_irqsave(struct bpf_res_spin_lock *lock, unsignlocal_irq_save (flags); ret =res_spin_lock ((rqspinlock_t *)lock);if (unlikely (ret)) { +bpf_prog_report_rqspinlock_violation (REPORT_STR (ret), lock, true);local_irq_restore (flags);preempt_enable ();return ret;
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v2 07/11] bpf: Report rqspinlock deadlocks/timeout to BPF stderr
Quoted 23 lines · Kumar Kartikeya Dwivedi
Begin reporting rqspinlock deadlocks and timeout to BPF program's stderr.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- kernel/bpf/rqspinlock.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)kernel/bpf/rqspinlock.c +6
-0@@ -666,6 +666,26 @@ EXPORT_SYMBOL_GPL(resilient_queued_spin_lock_slowpath);__bpf_kfunc_start_defs (); +static void bpf_prog_report_rqspinlock_violation (const char *str,void *lock,bool irqsave) +{ +struct rqspinlock_held *rqh =this_cpu_ptr (&rqspinlock_held_locks); +struct bpf_prog *prog; + + prog =bpf_prog_find_from_stack ();
kernel/bpf/rqspinlock.c EXPORT_SYMBOL_GPL(resilient_queued_spin_lock_slowpath); · line 684
Quoted 10 lines
kernel/bpf/rqspinlock.c +10
-0+if (!prog) +return ; +bpf_stream_stage (prog,BPF_STDERR , ({ +bpf_stream_printk ("ERROR: %s for bpf_res_spin_lock%s\n" , str, irqsave ?"_irqsave" :"" ); +bpf_stream_printk ("Attempted lock = 0x%px\n" , lock); +bpf_stream_printk ("Total held locks = %d\n" , rqh->cnt); +for (int i =0 ; i <min (RES_NR_HELD , rqh->cnt); i++) +bpf_stream_printk ("Held lock[%2d] = 0x%px\n" , i, rqh->locks[i]); +bpf_stream_dump_stack (); + }));
I agree with Eduard that hiding __ss is kinda meh. How about the following:
struct bpf_stream_stage ss;
bpf_stream_stage(ss, prog, BPF_STDERR, ({
bpf_stream_printk(ss, "ERROR: ...);
...
bpf_stream_dump_stack(ss);
}));Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 07/11] bpf: Report rqspinlock deadlocks/timeout to BPF stderr
Quoted 40 lines · Alexei Starovoitov
Quoted 23 lines · Kumar Kartikeya Dwivedi
Begin reporting rqspinlock deadlocks and timeout to BPF program's stderr.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- kernel/bpf/rqspinlock.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)kernel/bpf/rqspinlock.c +6
-0@@ -666,6 +666,26 @@ EXPORT_SYMBOL_GPL(resilient_queued_spin_lock_slowpath);__bpf_kfunc_start_defs (); +static void bpf_prog_report_rqspinlock_violation (const char *str,void *lock,bool irqsave) +{ +struct rqspinlock_held *rqh =this_cpu_ptr (&rqspinlock_held_locks); +struct bpf_prog *prog; + + prog =bpf_prog_find_from_stack ();kernel/bpf/rqspinlock.c EXPORT_SYMBOL_GPL(resilient_queued_spin_lock_slowpath); · line 684
Quoted 10 lines
kernel/bpf/rqspinlock.c +10
-0+if (!prog) +return ; +bpf_stream_stage (prog,BPF_STDERR , ({ +bpf_stream_printk ("ERROR: %s for bpf_res_spin_lock%s\n" , str, irqsave ?"_irqsave" :"" ); +bpf_stream_printk ("Attempted lock = 0x%px\n" , lock); +bpf_stream_printk ("Total held locks = %d\n" , rqh->cnt); +for (int i =0 ; i <min (RES_NR_HELD , rqh->cnt); i++) +bpf_stream_printk ("Held lock[%2d] = 0x%px\n" , i, rqh->locks[i]); +bpf_stream_dump_stack (); + }));I agree with Eduard that hiding
__ssis kinda meh. How about the following:
Quoted 7 lines
struct
bpf_stream_stagess;bpf_stream_stage(ss, prog, BPF_STDERR, ({ bpf_stream_printk(ss, "ERROR: ...); ... bpf_stream_dump_stack(ss); }));
Ok.
Kumar Kartikeya Dwivedi
maintainerPATCH v2 8/11 libbpf: Add bpf_stream_printk() macro
In reply to Kumar Kartikeya DwivediAdd a convenience macro to print data to the BPF streams. BPF_STDOUT and BPF_STDERR stream IDs in the vmlinux.h can be passed to the macro to print to the respective streams.
--- tools/lib/bpf/bpf_helpers.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
tools/lib/bpf/bpf_helpers.h +16 -0
@@ -314,6 +314,22 @@ enum libbpf_tristate { ___param, sizeof(___param)); \ }) +extern int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *args, + __u32 len__sz, void *aux__prog) __weak __ksym; + +#define bpf_stream_printk(stream_id, fmt, args...) \ +({ \ + static const char ___fmt[] = fmt; \ + unsigned long long ___param[___bpf_narg(args)]; \ + \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ + ___bpf_fill(___param, args); \ + _Pragma("GCC diagnostic pop") \ + \ + bpf_stream_vprintk(stream_id, ___fmt, ___param, sizeof(___param), NULL);\ +}) +/* Use __bpf_printk when bpf_printk call has 3 or fewer fmt args * Otherwise use __bpf_vprintk */
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v2 08/11] libbpf: Add bpf_stream_printk() macro
Quoted 6 lines · Kumar Kartikeya Dwivedi
Add a convenience macro to print data to the BPF streams.
BPF_STDOUTandBPF_STDERRstream IDs in thevmlinux.hcan be passed to the macro to print to the respective streams.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
(Sorry if I'm being repetative, could you please extend
one of the tests so that output of the bpf_stream_printk is compared with some reference values).
[...]
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 08/11] libbpf: Add bpf_stream_printk() macro
Quoted 7 lines · Eduard Zingerman
Quoted 4 lines · Kumar Kartikeya Dwivedi
Add a convenience macro to print data to the BPF streams.
BPF_STDOUTandBPF_STDERRstream IDs in thevmlinux.hcan be passed to the macro to print to the respective streams.
Quoted 8 lines
Quoted 2 lines
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
(Sorry if I'm being repetative, could you please extend
one of the tests so that output of thebpf_stream_printkis compared with some reference values).Acked-by Eduard Zingerman <[email protected]>
IDK whether you saw the final selftest patch, that does some output matching. I was matching arena fault addresses until I dropped the patch.
I will add something to match on the backtrace output as well.
Quoted 2 lines
[...]
Kumar Kartikeya Dwivedi
maintainerPATCH v2 9/11 libbpf: Introduce bpf_prog_stream_read() API
In reply to Kumar Kartikeya DwivediIntroduce a libbpf API so that users can read data from a given BPF stream for a BPF prog fd. For now, only the low-level syscall wrapper is provided, we can add a bpf_program__* accessor as a follow up if needed.
--- tools/lib/bpf/bpf.c | 16 ++++++++++++++++ tools/lib/bpf/bpf.h | 15 +++++++++++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 32 insertions(+)
tools/lib/bpf/bpf.c +16 -0
@@ -1331,3 +1331,19 @@ int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts) fd =sys_bpf_fd (BPF_TOKEN_CREATE , &attr, attr_sz);return libbpf_err_errno (fd); } + +int bpf_prog_stream_read (int prog_fd,__u32 stream_id,void *stream_buf,__u32 stream_buf_len) +{ + const size_t attr_sz = offsetofend(union bpf_attr, prog_stream_read); +union bpf_attr attr; +int err; + +memset (&attr,0 , attr_sz); + attr.prog_stream_read.stream_buf =ptr_to_u64 (stream_buf); + attr.prog_stream_read.stream_buf_len = stream_buf_len; + attr.prog_stream_read.stream_id = stream_id; + attr.prog_stream_read.prog_fd = prog_fd; + + err =sys_bpf (BPF_PROG_STREAM_READ_BY_FD , &attr, attr_sz); +return libbpf_err_errno (err); +}
tools/lib/bpf/bpf.h +15 -0
@@ -704,6 +704,21 @@ struct bpf_token_create_opts { LIBBPF_API int bpf_token_create(int bpffs_fd,struct bpf_token_create_opts *opts); +/** +* @brief **bpf_prog_stream_read** reads data from the BPF stream of a given BPF +* program. +* +* @param prog_fd FD for the BPF program whose BPF stream is to be read. +* @param stream_id ID of the BPF stream to be read. +* @param stream_buf Buffer to read data into from the BPF stream. +* @param stream_buf_len Maximum number of bytes to read from the BPF stream. +* +* @return The number of bytes read, on success; negative error code, otherwise +* (errno is also set to the error code) +*/ +LIBBPF_API int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *stream_buf, +__u32 stream_buf_len); + #ifdef __cplusplus }/* extern "C" */ #endif
tools/lib/bpf/libbpf.map +1 -0
@@ -437,6 +437,7 @@ LIBBPF_1.6.0 { bpf_linker__add_fd; bpf_linker__new_fd; bpf_object__prepare; + bpf_prog_stream_read; bpf_program__func_info; bpf_program__func_info_cnt; bpf_program__line_info;
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v2 09/11] libbpf: Introduce bpf_prog_stream_read() API
Quoted 7 lines · Kumar Kartikeya Dwivedi
Introduce a libbpf API so that users can read data from a given BPF stream for a BPF prog fd. For now, only the low-level syscall wrapper is provided, we can add a bpf_program__* accessor as a follow up if needed.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
[...]
Quoted 1 line
+int
bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *stream_buf, __u32 stream_buf_len)
Note: many of such utility functions have _opts parameter for future
extensibility. Imo in this case it would hinder usability a bit. If need be bpf_prog_stream_read_opts can be added later.
[...]
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 09/11] libbpf: Introduce bpf_prog_stream_read() API
Quoted 13 lines · Eduard Zingerman
Quoted 7 lines · Kumar Kartikeya Dwivedi
Introduce a libbpf API so that users can read data from a given BPF stream for a BPF prog fd. For now, only the low-level syscall wrapper is provided, we can add a bpf_program__* accessor as a follow up if needed.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
Acked-by Eduard Zingerman <[email protected]>
Quoted 8 lines
[...]
Quoted 1 line
+int
bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *stream_buf, __u32 stream_buf_len)Note: many of such utility functions have
_optsparameter for future
extensibility. Imo in this case it would hinder usability a bit. If need bebpf_prog_stream_read_optscan be added later.
We may have more arguments in the future (like count of errors, as Alan requested), so it may be a good idea to add the opts variant directly now.
Quoted 1 line
[...]
Re: [PATCH bpf-next v2 09/11] libbpf: Introduce bpf_prog_stream_read() API
In reply to Kumar Kartikeya DwivediQuoted 6 lines · Kumar Kartikeya Dwivedi
Introduce a libbpf API so that users can read data from a given BPF stream for a BPF prog fd. For now, only the low-level syscall wrapper is provided, we can add a bpf_program__* accessor as a follow up if needed.
Quoted 7 lines
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- tools/lib/bpf/bpf.c | 16 ++++++++++++++++ tools/lib/bpf/bpf.h | 15 +++++++++++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 32 insertions(+)
LGTM from libbpf side of things, but please see nits and questions below.
tools/lib/bpf/bpf.c int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts) · line 1335
Quoted 10 lines
tools/lib/bpf/bpf.c +2
-0@@ -1331,3 +1331,19 @@ int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts) fd =sys_bpf_fd (BPF_TOKEN_CREATE , &attr, attr_sz);return libbpf_err_errno (fd); } + +int bpf_prog_stream_read (int prog_fd,__u32 stream_id,void *stream_buf,__u32 stream_buf_len)
nit: I'd use shorter and no less clear "buf" and "buf_len". If anything, the buffer itself isn't really "a stream buffer", it's just a buffer into which we copy stream data..
Quoted 19 lines
tools/lib/bpf/bpf.c +14
-0+{ + const size_t attr_sz = offsetofend(union bpf_attr, prog_stream_read); +union bpf_attr attr; +int err; + +memset (&attr,0 , attr_sz); + attr.prog_stream_read.stream_buf =ptr_to_u64 (stream_buf); + attr.prog_stream_read.stream_buf_len = stream_buf_len; + attr.prog_stream_read.stream_id = stream_id; + attr.prog_stream_read.prog_fd = prog_fd; + + err =sys_bpf (BPF_PROG_STREAM_READ_BY_FD , &attr, attr_sz); +return libbpf_err_errno (err); +}tools/lib/bpf/bpf.h +0
-0
tools/lib/bpf/bpf.h struct bpf_token_create_opts { · line 712
Quoted 9 lines
tools/lib/bpf/bpf.h +6
-0@@ -704,6 +704,21 @@ struct bpf_token_create_opts { LIBBPF_API int bpf_token_create(int bpffs_fd,struct bpf_token_create_opts *opts); +/** +* @brief **bpf_prog_stream_read** reads data from the BPF stream of a given BPF +* program. +* +* @param prog_fd FD for the BPF program whose BPF stream is to be read. +* @param stream_id ID of the BPF stream to be read.
Is it documented anywhere that 1 is BPF_STDOUT and 2 is BPF_STDERR? That seems like an important user-visible thing, no? In patch #1 these constants are kernel-internal, is that intentional? If yes, how are users going to know which value to pass here?
tools/lib/bpf/bpf.h struct bpf_token_create_opts { · line 720
Quoted 8 lines
tools/lib/bpf/bpf.h +8
-0+* @param stream_buf Buffer to read data into from the BPF stream. +* @param stream_buf_len Maximum number of bytes to read from the BPF stream. +* +* @return The number of bytes read, on success; negative error code, otherwise +* (errno is also set to the error code) +*/ +LIBBPF_API int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *stream_buf, +__u32 stream_buf_len);
Do you anticipate adding some extra parameters, flags, etc? Should we add empty opts struct from the very beginning instead of later having bpf_prog_stream_read() and bpf_prog_stream_read_opts() APIs?
Quoted 19 lines
tools/lib/bpf/bpf.h +1
-0+ #ifdef __cplusplus }/* extern "C" */ #endif tools/lib/bpf/libbpf.map +1
-1@@ -437,6 +437,7 @@ LIBBPF_1.6.0 { bpf_linker__add_fd; bpf_linker__new_fd; bpf_object__prepare; + bpf_prog_stream_read; bpf_program__func_info; bpf_program__func_info_cnt; bpf_program__line_info; --2.47.1
Kumar Kartikeya Dwivedi
maintainerPATCH v2 10/11 bpftool: Add support for dumping streams
In reply to Kumar Kartikeya DwivediAdd support for printing the BPF stream contents of a program in bpftool. The new bpftool prog tracelog command is extended to take stdout and stderr arguments, and then the prog specification.
The bpf_prog_stream_read() API added in previous patch is simply reused to grab data and then it is dumped to the respective file. The stdout data is sent to stdout, and stderr is printed to stderr.
--- .../bpftool/Documentation/bpftool-prog.rst | 7 +++ tools/bpf/bpftool/bash-completion/bpftool | 16 +++++- tools/bpf/bpftool/prog.c | 50 ++++++++++++++++++- 3 files changed, 71 insertions(+), 2 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-prog.rst +7 -0
@@ -35,6 +35,7 @@ PROG COMMANDS | **bpftool** **prog attach** *PROG* *ATTACH_TYPE* [*MAP*] | **bpftool** **prog detach** *PROG* *ATTACH_TYPE* [*MAP*] | **bpftool** **prog tracelog** +| **bpftool** **prog tracelog** [ { **stdout** | **stderr** } *PROG* ] | **bpftool** **prog run** *PROG* **data_in** *FILE* [**data_out** *FILE* [**data_size_out** *L*]] [**ctx_in** *FILE* [**ctx_out** *FILE* [**ctx_size_out** *M*]]] [**repeat** *N*] | **bpftool** **prog profile** *PROG* [**duration** *DURATION*] *METRICs* | **bpftool** **prog help**
@@ -173,6 +174,12 @@ bpftool prog tracelog purposes. For streaming data from BPF programs to user space, one can use perf events (see also **bpftool-map**\ (8)). +bpftool prog tracelog { stdout | stderr } *PROG* + Dump the BPF stream of the program. BPF programs can write to these streams + at runtime with the **bpf_stream_vprintk**\ () kfunc. The kernel may write + error messages to the standard error stream. This facility should be used + only for debugging purposes. + bpftool prog run *PROG* data_in *FILE* [data_out *FILE* [data_size_out *L*]] [ctx_in *FILE* [ctx_out *FILE* [ctx_size_out *M*]]] [repeat *N*] Run BPF program *PROG* in the kernel testing infrastructure for BPF, meaning that the program works on the data and context provided by the
tools/bpf/bpftool/bash-completion/bpftool +15 -1
@@ -518,7 +518,21 @@ _bpftool() esac ;; tracelog) - return 0 + case $prev in + $command) + COMPREPLY+=( $( compgen -W "stdout stderr" -- \ + "$cur" ) ) + return 0 + ;; + stdout|stderr) + COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \ + "$cur" ) ) + return 0 + ;; + *) + return 0 + ;; + esac ;; profile) case $cword in
tools/bpf/bpftool/prog.c +49 -1
68 lines not on this page · raw message
Signature · 1 line
2.47.1
Quentin Monnet
maintainerRe: [PATCH bpf-next v2 10/11] bpftool: Add support for dumping streams
2025-05-23 18:18 UTC-0700 ~ Kumar Kartikeya Dwivedi <[email protected]>
Quoted 22 lines
Add support for printing the BPF stream contents of a program in bpftool. The new bpftool prog tracelog command is extended to take stdout and stderr arguments, and then the prog specification.
The
bpf_prog_stream_read()API added in previous patch is simply reused to grab data and then it is dumped to the respective file. The stdout data is sent to stdout, and stderr is printed to stderr.Cc Quentin Monnet <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- .../bpftool/Documentation/bpftool-prog.rst | 7 +++ tools/bpf/bpftool/bash-completion/bpftool | 16 +++++- tools/bpf/bpftool/prog.c | 50 ++++++++++++++++++- 3 files changed, 71 insertions(+), 2 deletions(-)tools/bpf/bpftool/prog.c +0
-0
tools/bpf/bpftool/prog.c static int do_detach(int argc, char **argv) · line 1119
Quoted 7 lines
tools/bpf/bpftool/prog.c +4
-0@@ -1113,6 +1113,53 @@ static int do_detach(int argc, char **argv)return 0 ; } +enum prog_tracelog_mode { +TRACE_STDOUT , +TRACE_STDERR , +};
You could have TRACE_STDOUT = 1 and TRACE_STDERR = 2 in this enum, and later do "stream_id = mode". This would avoid passing "1" or "2" inside of the prog_tracelog_stream() function. Although thinking again, it's maybe confusing to use the same enum for the mode and the stream_id? Your call.
tools/bpf/bpftool/prog.c static int do_detach(int argc, char **argv) · line 1126
Quoted 7 lines
tools/bpf/bpftool/prog.c +7
-0+ +static int +prog_tracelog_stream (int prog_fd,enum prog_tracelog_mode mode) +{ +FILE *file = mode ==TRACE_STDOUT ? stdout : stderr; +int stream_id = mode ==TRACE_STDOUT ?1 :2 ; +static char buf[512 ];
Why static?
tools/bpf/bpftool/prog.c static int do_detach(int argc, char **argv) · line 1134
Quoted 8 lines
tools/bpf/bpftool/prog.c +8
-0+int ret; + + ret =0 ; + do { + ret = bpf_prog_stream_read(prog_fd, stream_id, buf, sizeof(buf)); + if (ret > 0) { + fwrite(buf, sizeof(buf[0]), ret, file); + }
Nit: No brackets needed around fwrite()
Otherwise looks good, thanks!
Quentin
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 10/11] bpftool: Add support for dumping streams
Quoted 27 lines · Quentin Monnet
2025-05-23 18:18 UTC-0700 ~ Kumar Kartikeya Dwivedi <[email protected]>
Quoted 25 lines
Add support for printing the BPF stream contents of a program in bpftool. The new bpftool prog tracelog command is extended to take stdout and stderr arguments, and then the prog specification.
The
bpf_prog_stream_read()API added in previous patch is simply reused to grab data and then it is dumped to the respective file. The stdout data is sent to stdout, and stderr is printed to stderr.Cc Quentin Monnet <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- .../bpftool/Documentation/bpftool-prog.rst | 7 +++ tools/bpf/bpftool/bash-completion/bpftool | 16 +++++- tools/bpf/bpftool/prog.c | 50 ++++++++++++++++++- 3 files changed, 71 insertions(+), 2 deletions(-)tools/bpf/bpftool/prog.c +0
-0@@ -1113,6 +1113,53 @@ static int do_detach(int argc, char **argv)return 0 ; }
Quoted 11 lines
tools/bpf/bpftool/prog.c static int do_detach(int argc, char **argv) · line 1119
Quoted 4 lines
tools/bpf/bpftool/prog.c +4
-0+enum prog_tracelog_mode { +TRACE_STDOUT , +TRACE_STDERR , +};You could have
TRACE_STDOUT= 1 andTRACE_STDERR= 2 in this enum, and later do "stream_id= mode". This would avoid passing "1" or "2" inside of theprog_tracelog_stream()function. Although thinking again, it's maybe confusing to use the same enum for the mode and thestream_id? Your call.
Yeah, that's why I kept them separate.
Quoted 13 lines
tools/bpf/bpftool/prog.c static int do_detach(int argc, char **argv) · line 1126
Quoted 9 lines
tools/bpf/bpftool/prog.c +7
-0+ +static int +prog_tracelog_stream (int prog_fd,enum prog_tracelog_mode mode) +{ +FILE *file = mode ==TRACE_STDOUT ? stdout : stderr; +int stream_id = mode ==TRACE_STDOUT ?1 :2 ; +static char buf[512 ];Why static?
Will change, not really needed.
Quoted 11 lines
tools/bpf/bpftool/prog.c static int do_detach(int argc, char **argv) · line 1134
Quoted 9 lines
tools/bpf/bpftool/prog.c +8
-0+int ret; + + ret =0 ; + do { + ret = bpf_prog_stream_read(prog_fd, stream_id, buf, sizeof(buf)); + if (ret > 0) { + fwrite(buf, sizeof(buf[0]), ret, file); + }
Quoted 1 line
Nit: No brackets needed around
fwrite()
Ack.
Quoted 4 lines
Otherwise looks good, thanks!
Quentin
Thanks!
Kumar Kartikeya Dwivedi
maintainerPATCH v2 11/11 selftests/bpf: Add tests for prog streams
In reply to Kumar Kartikeya DwivediAdd selftests to stress test the various facets of the stream API, memory allocation pattern, and ensuring dumping support is tested and functional.
--- .../testing/selftests/bpf/prog_tests/stream.c | 110 ++++++++++++++++++ tools/testing/selftests/bpf/progs/stream.c | 75 ++++++++++++ .../testing/selftests/bpf/progs/stream_fail.c | 17 +++ 3 files changed, 202 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/stream.c create mode 100644 tools/testing/selftests/bpf/progs/stream.c create mode 100644 tools/testing/selftests/bpf/progs/stream_fail.c
tools/testing/selftests/bpf/prog_tests/stream.c new file +110 -0
@@ -0,0 +1,110 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ +#include <test_progs.h> +#include <sys/mman.h> + +#include "stream.skel.h" +#include "stream_fail.skel.h" + +void test_stream_failure (void ) +{ +RUN_TESTS (stream_fail); +} + +void test_stream_success (void ) +{ +RUN_TESTS (stream); +return ; +} + +struct { +int prog_off; +const char *errstr; +} stream_error_arr[] = { + { + offsetof(struct stream, progs.stream_cond_break), +"ERROR: Timeout detected for may_goto instruction" , + }, + { + offsetof(struct stream, progs.stream_deadlock), +"ERROR: AA or ABBA deadlock detected" , + }, +}; + +void test_stream_errors (void ) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts); +struct stream *skel; +int ret, prog_fd; +char buf[64 ]; + + skel =stream__open_and_load (); +if (!ASSERT_OK_PTR (skel,"stream__open_and_load" )) +return ; + +for (int i =0 ; i <ARRAY_SIZE (stream_error_arr); i++) { +struct bpf_program **prog; + + prog = (struct bpf_program **)(((char *)skel) + stream_error_arr[i].prog_off); + prog_fd =bpf_program__fd (*prog); + ret =bpf_prog_test_run_opts (prog_fd, &opts); +ASSERT_OK (ret,"ret" ); +ASSERT_OK (opts.retval,"retval" ); + +#if !defined(__x86_64__) +ASSERT_TRUE (1 ,"Timed may_goto unsupported, skip." ); +if (i ==0 ) { + ret =bpf_prog_stream_read (prog_fd,2 , buf,sizeof (buf)); +ASSERT_EQ (ret,0 ,"stream read" ); +continue ; + } +#endif + + ret =bpf_prog_stream_read (prog_fd,2 , buf,sizeof (buf)); +ASSERT_EQ (ret,sizeof (buf),"stream read" ); +ASSERT_STRNEQ (stream_error_arr[i].errstr, buf,strlen (stream_error_arr[i].errstr), +"compare error msg" ); + } + +stream__destroy (skel); +} + +void test_stream_syscall (void ) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts); +struct stream *skel; +int ret, prog_fd; +char buf[64 ]; + + skel =stream__open_and_load (); +if (!ASSERT_OK_PTR (skel,"stream__open_and_load" )) +return ; + + prog_fd =bpf_program__fd (skel->progs.stream_syscall); + ret =bpf_prog_test_run_opts (prog_fd, &opts); +ASSERT_OK (ret,"ret" ); +ASSERT_OK (opts.retval,"retval" ); + +bpf_prog_stream_read (0 ,1 , buf,sizeof (buf)); + ret = -errno; +ASSERT_EQ (ret, -EINVAL ,"bad prog_fd" ); + +bpf_prog_stream_read (prog_fd,0 , buf,sizeof (buf)); + ret = -errno; +ASSERT_EQ (ret, -ENOENT ,"bad stream id" ); + +bpf_prog_stream_read (prog_fd,1 ,NULL ,sizeof (buf)); + ret = -errno; +ASSERT_EQ (ret, -EFAULT ,"bad stream buf" ); + + ret =bpf_prog_stream_read (prog_fd,1 , buf,2 ); +ASSERT_EQ (ret,2 ,"bytes" ); + ret =bpf_prog_stream_read (prog_fd,1 , buf,2 ); +ASSERT_EQ (ret,2 ,"bytes" ); + ret =bpf_prog_stream_read (prog_fd,1 , buf,1 ); +ASSERT_EQ (ret,0 ,"no bytes stdout" ); + ret =bpf_prog_stream_read (prog_fd,2 , buf,1 ); +ASSERT_EQ (ret,0 ,"no bytes stderr" ); + +stream__destroy (skel); +}
tools/testing/selftests/bpf/progs/stream.c new file +75 -0
75 lines not on this page · raw message
tools/testing/selftests/bpf/progs/stream_fail.c new file +17 -0
@@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ +#include <vmlinux.h> +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_core_read.h> +#include "bpf_misc.h" + +SEC("syscall") +__failure __msg("Possibly NULL pointer passed") +int stream_vprintk_null_arg(void *ctx) +{ +bpf_stream_vprintk (BPF_STDOUT ,"" ,NULL ,0 ,NULL ); +return 0 ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.47.1
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 11/11] selftests/bpf: Add tests for prog streams
Kumar Kartikeya Dwivedi <[email protected]> writes:
[...]
tools/testing/selftests/bpf/prog_tests/stream.c line 32
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/stream.c new file +13
-0+struct { +int prog_off; +const char *errstr; +} stream_error_arr[] = { + { + offsetof(struct stream, progs.stream_cond_break), +"ERROR: Timeout detected for may_goto instruction" , + }, + { + offsetof(struct stream, progs.stream_deadlock), +"ERROR: AA or ABBA deadlock detected" , + }, +};
Wild idea: instead of hand-coding this for each test, maybe add __bpf_stderr, __bpf_stdout annotations to test_loader.c? With intent for them to operate like __msg.
[...]
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 11/11] selftests/bpf: Add tests for prog streams
Quoted 14 lines · Eduard Zingerman
Kumar Kartikeya Dwivedi <[email protected]> writes:
[...]
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/stream.c new file +9
-0+struct { +int prog_off; +const char *errstr; +} stream_error_arr[] = { + { + offsetof(struct stream, progs.stream_cond_break), +"ERROR: Timeout detected for may_goto instruction" , + }, + {
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/stream.c line 32
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/stream.c new file +4
-0+ offsetof(struct stream, progs.stream_deadlock), + "ERROR: AA or ABBA deadlock detected", + }, +};Wild idea: instead of hand-coding this for each test, maybe add
__bpf_stderr,__bpf_stdoutannotations totest_loader.c? With intent for them to operate like__msg.
Good idea. But we'll have to run the program, which is slightly different for each type.
I guess I can just support tc, syscall etc. for now with some default setup to support this.
Quoted 2 lines
[...]
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 11/11] selftests/bpf: Add tests for prog streams
Quoted 24 lines · Kumar Kartikeya Dwivedi
Quoted 14 lines · Eduard Zingerman
Kumar Kartikeya Dwivedi <[email protected]> writes:
[...]
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/stream.c new file +9
-0+struct { +int prog_off; +const char *errstr; +} stream_error_arr[] = { + { + offsetof(struct stream, progs.stream_cond_break), +"ERROR: Timeout detected for may_goto instruction" , + }, + {Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/stream.c line 32
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/stream.c new file +4
-0+ offsetof(struct stream, progs.stream_deadlock), + "ERROR: AA or ABBA deadlock detected", + }, +};Wild idea: instead of hand-coding this for each test, maybe add
__bpf_stderr,__bpf_stdoutannotations totest_loader.c? With intent for them to operate like__msg.
Quoted 4 lines
Good idea. But we'll have to run the program, which is slightly different for each type.
I guess I can just support tc, syscall etc. for now with some default setup to support this.
We currently have a __retval annotation which does BPF_PROG_TEST_RUN. BPF_PROG_TEST_RUN is supported for "syscall" program type that you use in the tests, so that should be covered.