bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH bpf-next/net v1 00/13] BPF Standard Streams
@ 2025-04-14 16:14 Kumar Kartikeya Dwivedi
  2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 01/13] bpf: Tie dynptrs to referenced source objects Kumar Kartikeya Dwivedi
                   ` (12 more replies)
  0 siblings, 13 replies; 44+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2025-04-14 16:14 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Martin KaFai Lau, Eduard Zingerman, Emil Tsalapatis,
	Barret Rhoden, kkd, kernel-team

NOTE: Temporarily based off of bpf-next/net due to Qdisc related fixes.

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 the
bpf_mem_alloc() API, but in case it fails, we use try_alloc_pages() to
construct a bespoke bump allocator to carve out elements. If this fails,
we finally give up and drop the message.

Three scenarios (rqspinlock deadlocks, arena page faults, and cond_break
timeouts) are covered for reporting errors to user space, the format
looks as follows (with file:line info for BPF programs to pin point
source of error). The output is as follows:

ERROR: AA or ABBA deadlock detected for bpf_res_spin_lock
Attempted lock   = 0xff11000104a875e0
Total held locks = 1
Held lock[ 0] = 0xff11000104a875e0
CPU: 5 UID: 0 PID: 764 Comm: test_progs
Call trace:
 bpf_prog_stderr_dump_stack+0x83/0xc0
 bpf_res_spin_lock+0x8c/0xa0
 bpf_prog_0f369a1fb6eb23d7_foo+0xe5/0x140: [stream_bpftool.c:62]
 bpf_prog_986cfd896eb75e8c_stream_bpftool_dump_prog_stream+0x7e/0x2e1: [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: Arena READ access at unmapped address 0xdeadbeef
CPU: 5 UID: 0 PID: 764 Comm: test_progs
Call trace:
 bpf_prog_stderr_dump_stack+0x83/0xc0
 ex_handler_bpf+0x4a/0xa0
 fixup_exception+0xde/0x310
 kernelmode_fixup_or_oops.constprop.0+0x2f/0x70
 exc_page_fault+0xdd/0x1d0
 asm_exc_page_fault+0x26/0x30
 bpf_prog_0f369a1fb6eb23d7_foo+0x10c/0x140: [stream_bpftool.c:67]
 bpf_prog_986cfd896eb75e8c_stream_bpftool_dump_prog_stream+0x7e/0x2e1: [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: Arena WRITE access at unmapped address 0xfaceb00c
CPU: 5 UID: 0 PID: 764 Comm: test_progs
Call trace:
 bpf_prog_stderr_dump_stack+0x83/0xc0
 ex_handler_bpf+0x4a/0xa0
 fixup_exception+0xde/0x310
 kernelmode_fixup_or_oops.constprop.0+0x2f/0x70
 exc_page_fault+0xdd/0x1d0
 asm_exc_page_fault+0x26/0x30
 bpf_prog_0f369a1fb6eb23d7_foo+0x111/0x140: [stream_bpftool.c:67]
 bpf_prog_986cfd896eb75e8c_stream_bpftool_dump_prog_stream+0x7e/0x2e1: [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: 5 UID: 0 PID: 764 Comm: test_progs
Call trace:
 bpf_prog_stderr_dump_stack+0x83/0xc0
 bpf_check_timed_may_goto+0x4d/0xa0
 arch_bpf_timed_may_goto+0x21/0x40
 bpf_prog_0f369a1fb6eb23d7_foo+0x12f/0x140: [stream_bpftool.c:70]
 bpf_prog_986cfd896eb75e8c_stream_bpftool_dump_prog_stream+0x7e/0x2e1: [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

There is a lot of minor things to address before this is mergeable, but
the main logic and approach should be set in stone.

TODO (in order of importance)
-----------------------------
 * Add an API for kernel writers to stderr streams to allow them to
   stage messages to a llist_head and then atomically splice in a bunch
   of messages into the stream to avoid interleaving from multiple
   threads. Each "message" consisisting of multiple printk statements
   then appears as a contiguous sequence to the user space.
   This will simply be a stage + commit API on top of an object pinning
   the llist_head that will be del_all'd and then add_batch'd to the
   stream->log.
 * Add bpf_printk() macros for streams.
 * Add kdoc comments for kfuncs.
 * Add more corner case tests, and negative cases.
 * Enforcing memory consumption limits on the log usage, so that
   in-flight log memory is bounded at runtime.
 * Support bpftool filepath printing and JSON output.

Alexei Starovoitov (1):
  locking/local_lock, mm: Replace localtry_ helpers with local_trylock_t
    type

Kumar Kartikeya Dwivedi (12):
  bpf: Tie dynptrs to referenced source objects
  bpf: Compare dynptr_id in regsafe
  selftests/bpf: Convert dynptr_fail to use vmlinux.h
  selftests/bpf: Add tests for dynptr source object interaction
  bpf: Introduce bpf_dynptr_from_mem_slice
  bpf: Introduce per-prog stdout/stderr streams
  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
  bpf: Report arena faults to BPF stderr
  bpftool: Add support for dumping streams
  selftests/bpf: Add tests for prog streams

 arch/x86/net/bpf_jit_comp.c                   |  21 +-
 include/linux/bpf.h                           |  63 +-
 include/linux/local_lock.h                    |  58 +-
 include/linux/local_lock_internal.h           | 207 +++---
 kernel/bpf/Makefile                           |   2 +-
 kernel/bpf/arena.c                            |  14 +
 kernel/bpf/core.c                             |  29 +-
 kernel/bpf/helpers.c                          |  58 +-
 kernel/bpf/rqspinlock.c                       |  22 +
 kernel/bpf/stream.c                           | 595 ++++++++++++++++++
 kernel/bpf/syscall.c                          |   2 +-
 kernel/bpf/verifier.c                         | 104 ++-
 mm/memcontrol.c                               |  39 +-
 tools/bpf/bpftool/Makefile                    |   2 +-
 tools/bpf/bpftool/prog.c                      |  71 ++-
 tools/bpf/bpftool/skeleton/stream.bpf.c       |  96 +++
 .../testing/selftests/bpf/prog_tests/dynptr.c |   2 +
 .../testing/selftests/bpf/prog_tests/stream.c |  57 ++
 .../testing/selftests/bpf/progs/dynptr_fail.c |  43 +-
 .../selftests/bpf/progs/dynptr_fail_qdisc.c   |  38 ++
 tools/testing/selftests/bpf/progs/stream.c    | 150 +++++
 .../selftests/bpf/progs/stream_bpftool.c      | 142 +++++
 .../testing/selftests/bpf/progs/stream_fail.c |  38 ++
 23 files changed, 1597 insertions(+), 256 deletions(-)
 create mode 100644 kernel/bpf/stream.c
 create mode 100644 tools/bpf/bpftool/skeleton/stream.bpf.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/stream.c
 create mode 100644 tools/testing/selftests/bpf/progs/dynptr_fail_qdisc.c
 create mode 100644 tools/testing/selftests/bpf/progs/stream.c
 create mode 100644 tools/testing/selftests/bpf/progs/stream_bpftool.c
 create mode 100644 tools/testing/selftests/bpf/progs/stream_fail.c


base-commit: 93cb5375fb16693273144e7627e4fe27b811003a
-- 
2.47.1


^ permalink raw reply	[flat|nested] 44+ messages in thread

end of thread, other threads:[~2025-05-07 17:14 UTC | newest]

Thread overview: 44+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-14 16:14 [RFC PATCH bpf-next/net v1 00/13] BPF Standard Streams Kumar Kartikeya Dwivedi
2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 01/13] bpf: Tie dynptrs to referenced source objects Kumar Kartikeya Dwivedi
2025-04-14 19:58   ` Amery Hung
2025-04-17 19:53     ` Kumar Kartikeya Dwivedi
2025-04-16  5:52   ` Eduard Zingerman
2025-04-17 19:52     ` Kumar Kartikeya Dwivedi
2025-04-16 21:04   ` Andrii Nakryiko
2025-04-17 19:51     ` Kumar Kartikeya Dwivedi
2025-04-22 21:09       ` Andrii Nakryiko
2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 02/13] bpf: Compare dynptr_id in regsafe Kumar Kartikeya Dwivedi
2025-04-16  7:58   ` Eduard Zingerman
2025-04-16 21:04   ` Andrii Nakryiko
2025-04-17 19:54     ` Kumar Kartikeya Dwivedi
2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 03/13] selftests/bpf: Convert dynptr_fail to use vmlinux.h Kumar Kartikeya Dwivedi
2025-04-16  8:41   ` Eduard Zingerman
2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 04/13] selftests/bpf: Add tests for dynptr source object interaction Kumar Kartikeya Dwivedi
2025-04-16  8:40   ` Eduard Zingerman
2025-04-17 20:09     ` Kumar Kartikeya Dwivedi
2025-04-17 20:44       ` Eduard Zingerman
2025-04-17 21:10         ` Kumar Kartikeya Dwivedi
2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 05/13] locking/local_lock, mm: Replace localtry_ helpers with local_trylock_t type Kumar Kartikeya Dwivedi
2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 06/13] bpf: Introduce bpf_dynptr_from_mem_slice Kumar Kartikeya Dwivedi
2025-04-16 17:36   ` Eduard Zingerman
2025-04-17 20:07     ` Kumar Kartikeya Dwivedi
2025-04-16 21:04   ` Andrii Nakryiko
2025-04-17 20:07     ` Kumar Kartikeya Dwivedi
2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 07/13] bpf: Introduce per-prog stdout/stderr streams Kumar Kartikeya Dwivedi
2025-04-16 21:49   ` Eduard Zingerman
2025-04-17 20:06     ` Kumar Kartikeya Dwivedi
2025-04-22  1:42       ` Alexei Starovoitov
2025-04-22  2:30         ` Kumar Kartikeya Dwivedi
2025-04-22 17:44           ` Alexei Starovoitov
2025-04-22 22:09       ` Eduard Zingerman
2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 08/13] bpf: Add dump_stack() analogue to print to BPF stderr Kumar Kartikeya Dwivedi
2025-04-22 21:07   ` Eduard Zingerman
2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 09/13] bpf: Report may_goto timeout " Kumar Kartikeya Dwivedi
2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 10/13] bpf: Report rqspinlock deadlocks/timeout " Kumar Kartikeya Dwivedi
2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 11/13] bpf: Report arena faults " Kumar Kartikeya Dwivedi
2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 12/13] bpftool: Add support for dumping streams Kumar Kartikeya Dwivedi
2025-04-22 20:30   ` Eduard Zingerman
2025-04-22 22:10   ` Quentin Monnet
2025-05-07 17:13     ` Kumar Kartikeya Dwivedi
2025-04-14 16:14 ` [RFC PATCH bpf-next/net v1 13/13] selftests/bpf: Add tests for prog streams Kumar Kartikeya Dwivedi
2025-04-22 19:12   ` Eduard Zingerman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).