bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next v5 0/5] Support eliding map lookup nullness
@ 2024-12-12 23:22 Daniel Xu
  2024-12-12 23:22 ` [PATCH bpf-next v5 1/5] bpf: verifier: Add missing newline on verbose() call Daniel Xu
                   ` (4 more replies)
  0 siblings, 5 replies; 27+ messages in thread
From: Daniel Xu @ 2024-12-12 23:22 UTC (permalink / raw)
  To: ast, linux-kernel, netdev, linux-kselftest, memxor, bpf, daniel,
	eddyz87

This patch allows progs to elide a null check on statically known map
lookup keys. In other words, if the verifier can statically prove that
the lookup will be in-bounds, allow the prog to drop the null check.

This is useful for two reasons:

1. Large numbers of nullness checks (especially when they cannot fail)
   unnecessarily pushes prog towards BPF_COMPLEXITY_LIMIT_JMP_SEQ.
2. It forms a tighter contract between programmer and verifier.

For (1), bpftrace is starting to make heavier use of percpu scratch
maps. As a result, for user scripts with large number of unrolled loops,
we are starting to hit jump complexity verification errors.  These
percpu lookups cannot fail anyways, as we only use static key values.
Eliding nullness probably results in less work for verifier as well.

For (2), percpu scratch maps are often used as a larger stack, as the
currrent stack is limited to 512 bytes. In these situations, it is
desirable for the programmer to express: "this lookup should never fail,
and if it does, it means I messed up the code". By omitting the null
check, the programmer can "ask" the verifier to double check the logic.

Changes in v5:
* Dropped all acks
* Use s64 instead of long for const_map_key
* Ensure stack slot contains spilled reg before accessing spilled_ptr
* Ensure spilled reg is a scalar before accessing tnum const value
* Fix verifier selftest for 32-bit write to write at 8 byte alignment
  to ensure spill is tracked
* Introduce more precise tracking of helper stack accesses
* Do constant map key extraction as part of helper argument processing
  and then remove duplicated stack checks
* Use ret_flag instead of regs[BPF_REG_0].type
* Handle STACK_ZERO
* Fix bug in bpf_load_hdr_opt() arg annotation

Changes in v4:
* Only allow for CAP_BPF
* Add test for stack growing upwards
* Improve comment about stack growing upwards

Changes in v3:
* Check if stack is (erroneously) growing upwards
* Mention in commit message why existing tests needed change

Changes in v2:
* Added a check for when R2 is not a ptr to stack
* Added a check for when stack is uninitialized (no stack slot yet)
* Updated existing tests to account for null elision
* Added test case for when R2 can be both const and non-const

Daniel Xu (5):
  bpf: verifier: Add missing newline on verbose() call
  bpf: tcp: Mark bpf_load_hdr_opt() arg2 as read-write
  bpf: verifier: Refactor helper access type tracking
  bpf: verifier: Support eliding map lookup nullness
  bpf: selftests: verifier: Add nullness elision tests

 kernel/bpf/verifier.c                         | 127 ++++++++---
 net/core/filter.c                             |   2 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c |   6 +-
 tools/testing/selftests/bpf/progs/iters.c     |  14 +-
 .../selftests/bpf/progs/map_kptr_fail.c       |   2 +-
 .../selftests/bpf/progs/test_global_func10.c  |   2 +-
 .../selftests/bpf/progs/uninit_stack.c        |  29 ---
 .../bpf/progs/verifier_array_access.c         | 214 ++++++++++++++++++
 .../bpf/progs/verifier_basic_stack.c          |   2 +-
 .../selftests/bpf/progs/verifier_const_or.c   |   4 +-
 .../progs/verifier_helper_access_var_len.c    |  12 +-
 .../selftests/bpf/progs/verifier_int_ptr.c    |   2 +-
 .../selftests/bpf/progs/verifier_map_in_map.c |   2 +-
 .../selftests/bpf/progs/verifier_mtu.c        |   2 +-
 .../selftests/bpf/progs/verifier_raw_stack.c  |   4 +-
 .../selftests/bpf/progs/verifier_unpriv.c     |   2 +-
 .../selftests/bpf/progs/verifier_var_off.c    |   8 +-
 tools/testing/selftests/bpf/verifier/calls.c  |   2 +-
 .../testing/selftests/bpf/verifier/map_kptr.c |   2 +-
 19 files changed, 342 insertions(+), 96 deletions(-)

-- 
2.46.0


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

end of thread, other threads:[~2024-12-20  4:00 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-12 23:22 [PATCH bpf-next v5 0/5] Support eliding map lookup nullness Daniel Xu
2024-12-12 23:22 ` [PATCH bpf-next v5 1/5] bpf: verifier: Add missing newline on verbose() call Daniel Xu
2024-12-12 23:22 ` [PATCH bpf-next v5 2/5] bpf: tcp: Mark bpf_load_hdr_opt() arg2 as read-write Daniel Xu
2024-12-16 18:18   ` Martin KaFai Lau
2024-12-12 23:22 ` [PATCH bpf-next v5 3/5] bpf: verifier: Refactor helper access type tracking Daniel Xu
2024-12-13  4:04   ` Eduard Zingerman
2024-12-13 20:02     ` Daniel Xu
2024-12-12 23:22 ` [PATCH bpf-next v5 4/5] bpf: verifier: Support eliding map lookup nullness Daniel Xu
2024-12-13  4:04   ` Eduard Zingerman
2024-12-13 20:57     ` Daniel Xu
2024-12-13 23:02   ` Andrii Nakryiko
2024-12-14  2:44     ` Daniel Xu
2024-12-14  3:13       ` Eduard Zingerman
2024-12-16 23:24         ` Andrii Nakryiko
2024-12-19  0:09           ` Daniel Xu
2024-12-19 21:41           ` Daniel Xu
2024-12-20  0:04             ` Eduard Zingerman
2024-12-20  0:40               ` Daniel Xu
2024-12-20  0:43                 ` Eduard Zingerman
2024-12-20  0:49                   ` Alexei Starovoitov
2024-12-20  4:00                     ` Daniel Xu
2024-12-13 23:10   ` Kumar Kartikeya Dwivedi
2024-12-13 23:14     ` Eduard Zingerman
2024-12-13 23:18       ` Eduard Zingerman
2024-12-12 23:22 ` [PATCH bpf-next v5 5/5] bpf: selftests: verifier: Add nullness elision tests Daniel Xu
2024-12-14  6:17   ` Eduard Zingerman
2024-12-18  1:57     ` Daniel Xu

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