public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v1 0/2] Timed may_goto
@ 2025-03-02 20:13 Kumar Kartikeya Dwivedi
  2025-03-02 20:13 ` [PATCH bpf-next v1 1/2] bpf: Add verifier support for timed may_goto Kumar Kartikeya Dwivedi
  2025-03-02 20:13 ` [PATCH bpf-next v1 2/2] bpf, x86: Add x86 JIT " Kumar Kartikeya Dwivedi
  0 siblings, 2 replies; 6+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2025-03-02 20:13 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Martin KaFai Lau, Eduard Zingerman, Tejun Heo, Emil Tsalapatis,
	Barret Rhoden, Josh Don, Dohyun Kim, kkd, kernel-team

This series replaces the current implementation of cond_break, which
uses the may_goto instruction, and counts 8 million iterations per stack
frame, with an implementation based on sampling time locally on the CPU.

This is done to permit a longer time for a given loop per-program
invocation. The accounting is still done per-stack frame, but the count
is used to instead amortize the cost of the logic to sample and check
the time spent since the start.

This is needed for expressing more complicated algorithms (spin locks,
waiting loops, etc.) in BPF programs without false positive expiration
of the loop. For instance, the plan is to make use of this for
implementing spin locks for BPF arena [0].

For the loop as follows:

for (int i = 0;; i++) {}

Testing on a bare-metal Saphire Rapids Intel server yields the following
table (taking an average of 25 runs).

+-----------------------------+--------------+--------------+------------------+
| Loop type		      |	Iterations   |	Time (ms)   |	Time/iter (ns) |
+-----------------------------|--------------+--------------+------------------+
| may_goto		      |	8388608	     |	3	    |	0.36	       |
| timed_may_goto (count=65535)|	589674932    |	250	    |	0.42	       |
| bpf_for		      |	8388608	     |	10	    |	1.19	       |
+-----------------------------+--------------+--------------+------------------+

Here, count is used to amortize the time sampling and checking logic.

Obviously, this is the limit of an empty loop. Given the complexity of
the loop body, the time spent in the loop can be longer. Cancellations
will address the task of imposing an upper bound on program runtime.

For now, the implementation only supports x86.

  [0]: https://lore.kernel.org/bpf/20250118162238.2621311-1-memxor@gmail.com

Kumar Kartikeya Dwivedi (2):
  bpf: Add verifier support for timed may_goto
  bpf, x86: Add x86 JIT support for timed may_goto

 arch/x86/net/Makefile                         |  2 +-
 arch/x86/net/bpf_jit_comp.c                   |  5 ++
 arch/x86/net/bpf_timed_may_goto.S             | 43 ++++++++++++++
 include/linux/bpf.h                           |  1 +
 include/linux/filter.h                        |  8 +++
 kernel/bpf/core.c                             | 31 ++++++++++
 kernel/bpf/verifier.c                         | 52 ++++++++++++++---
 .../bpf/progs/verifier_bpf_fastcall.c         | 58 +++++++++++++++----
 .../selftests/bpf/progs/verifier_may_goto_1.c | 34 ++++++++++-
 9 files changed, 213 insertions(+), 21 deletions(-)
 create mode 100644 arch/x86/net/bpf_timed_may_goto.S


base-commit: 0b9363131daf4227d5ae11ee677acdcfff06e938
-- 
2.43.5


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

end of thread, other threads:[~2025-03-03 22:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-02 20:13 [PATCH bpf-next v1 0/2] Timed may_goto Kumar Kartikeya Dwivedi
2025-03-02 20:13 ` [PATCH bpf-next v1 1/2] bpf: Add verifier support for timed may_goto Kumar Kartikeya Dwivedi
2025-03-03 21:59   ` Alexei Starovoitov
2025-03-03 22:25     ` Kumar Kartikeya Dwivedi
2025-03-03 22:40       ` Kumar Kartikeya Dwivedi
2025-03-02 20:13 ` [PATCH bpf-next v1 2/2] bpf, x86: Add x86 JIT " Kumar Kartikeya Dwivedi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox