public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Eduard Zingerman <eddyz87@gmail.com>,
	Andrii Nakryiko <andrii@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Sasha Levin <sashal@kernel.org>,
	daniel@iogearbox.net, shuah@kernel.org, bpf@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: [PATCH AUTOSEL 6.1 14/25] selftests/bpf: fix bpf_loop_bench for new callback verification scheme
Date: Tue, 28 Nov 2023 16:07:30 -0500	[thread overview]
Message-ID: <20231128210750.875945-14-sashal@kernel.org> (raw)
In-Reply-To: <20231128210750.875945-1-sashal@kernel.org>

From: Eduard Zingerman <eddyz87@gmail.com>

[ Upstream commit f40bfd1679446b22d321e64a1fa98b7d07d2be08 ]

This is a preparatory change. A follow-up patch "bpf: verify callbacks
as if they are called unknown number of times" changes logic for
callbacks handling. While previously callbacks were verified as a
single function call, new scheme takes into account that callbacks
could be executed unknown number of times.

This has dire implications for bpf_loop_bench:

    SEC("fentry/" SYS_PREFIX "sys_getpgid")
    int benchmark(void *ctx)
    {
            for (int i = 0; i < 1000; i++) {
                    bpf_loop(nr_loops, empty_callback, NULL, 0);
                    __sync_add_and_fetch(&hits, nr_loops);
            }
            return 0;
    }

W/o callbacks change verifier sees it as a 1000 calls to
empty_callback(). However, with callbacks change things become
exponential:
- i=0: state exploring empty_callback is scheduled with i=0 (a);
- i=1: state exploring empty_callback is scheduled with i=1;
  ...
- i=999: state exploring empty_callback is scheduled with i=999;
- state (a) is popped from stack;
- i=1: state exploring empty_callback is scheduled with i=1;
  ...

Avoid this issue by rewriting outer loop as bpf_loop().
Unfortunately, this adds a function call to a loop at runtime, which
negatively affects performance:

            throughput               latency
   before:  149.919 ± 0.168 M ops/s, 6.670 ns/op
   after :  137.040 ± 0.187 M ops/s, 7.297 ns/op

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20231121020701.26440-4-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 tools/testing/selftests/bpf/progs/bpf_loop_bench.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/bpf/progs/bpf_loop_bench.c b/tools/testing/selftests/bpf/progs/bpf_loop_bench.c
index 4ce76eb064c41..d461746fd3c1e 100644
--- a/tools/testing/selftests/bpf/progs/bpf_loop_bench.c
+++ b/tools/testing/selftests/bpf/progs/bpf_loop_bench.c
@@ -15,13 +15,16 @@ static int empty_callback(__u32 index, void *data)
 	return 0;
 }
 
+static int outer_loop(__u32 index, void *data)
+{
+	bpf_loop(nr_loops, empty_callback, NULL, 0);
+	__sync_add_and_fetch(&hits, nr_loops);
+	return 0;
+}
+
 SEC("fentry/" SYS_PREFIX "sys_getpgid")
 int benchmark(void *ctx)
 {
-	for (int i = 0; i < 1000; i++) {
-		bpf_loop(nr_loops, empty_callback, NULL, 0);
-
-		__sync_add_and_fetch(&hits, nr_loops);
-	}
+	bpf_loop(1000, outer_loop, NULL, 0);
 	return 0;
 }
-- 
2.42.0


  parent reply	other threads:[~2023-11-28 21:08 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-28 21:07 [PATCH AUTOSEL 6.1 01/25] x86/hyperv: Fix the detection of E820_TYPE_PRAM in a Gen2 VM Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 02/25] usb: aqc111: check packet for fixup for true limit Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 03/25] stmmac: dwmac-loongson: Add architecture dependency Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 04/25] blk-throttle: fix lockdep warning of "cgroup_mutex or RCU read lock required!" Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 05/25] blk-cgroup: bypass blkcg_deactivate_policy after destroying Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 06/25] bcache: avoid oversize memory allocation by small stripe_size Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 07/25] bcache: remove redundant assignment to variable cur_idx Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 08/25] bcache: add code comments for bch_btree_node_get() and __bch_btree_node_alloc() Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 09/25] bcache: avoid NULL checking to c->root in run_cache_set() Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 10/25] nbd: fold nbd config initialization into nbd_alloc_config() Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 11/25] nvme-auth: unlock mutex in one place only Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 12/25] nvme-auth: set explanation code for failure2 msgs Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 13/25] nvme: catch errors from nvme_configure_metadata() Sasha Levin
2023-11-28 21:07 ` Sasha Levin [this message]
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 15/25] LoongArch: Add dependency between vmlinuz.efi and vmlinux.efi Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 16/25] LoongArch: Implement constant timer shutdown interface Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 17/25] platform/x86: intel_telemetry: Fix kernel doc descriptions Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 18/25] HID: glorious: fix Glorious Model I HID report Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 19/25] HID: add ALWAYS_POLL quirk for Apple kb Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 20/25] nbd: pass nbd_sock to nbd_read_reply() instead of index Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 21/25] HID: hid-asus: reset the backlight brightness level on resume Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 22/25] HID: multitouch: Add quirk for HONOR GLO-GXXX touchpad Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 23/25] asm-generic: qspinlock: fix queued_spin_value_unlocked() implementation Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 24/25] net: usb: qmi_wwan: claim interface 4 for ZTE MF290 Sasha Levin
2023-11-28 21:07 ` [PATCH AUTOSEL 6.1 25/25] arm64: add dependency between vmlinuz.efi and Image Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231128210750.875945-14-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox