All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v2 1/2] libbpf: Probe for bounded loop support
@ 2021-12-17 13:52 Paul Chaignon
  2021-12-17 13:52 ` [PATCH bpf-next v2 2/2] bpftool: " Paul Chaignon
  0 siblings, 1 reply; 2+ messages in thread
From: Paul Chaignon @ 2021-12-17 13:52 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko; +Cc: bpf, Quentin Monnet

This patch introduces a new probe to check whether the verifier supports
bounded loops as introduced in commit 2589726d12a1 ("bpf: introduce
bounded loops"). This patch will allow BPF users such as Cilium to probe
for loop support on startup and only unconditionally unroll loops on
older kernels.

Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Paul Chaignon <paul@isovalent.com>
---
Changes in v2:
  - Resending to fix a format error. I had the good idea of saving the
    patch in Gmail as a draft before sending with mutt, which caused
    Gmail to remove the trailing space in the Git version signature.
    That then caused pathwork to generate an incorrect mbox file.

 tools/lib/bpf/libbpf.h        |  1 +
 tools/lib/bpf/libbpf.map      |  1 +
 tools/lib/bpf/libbpf_probes.c | 20 ++++++++++++++++++++
 3 files changed, 22 insertions(+)

diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 42b2f36fd9f0..3621aaaff67c 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -1058,6 +1058,7 @@ LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex);
 LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id,
 				 enum bpf_prog_type prog_type, __u32 ifindex);
 LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex);
+LIBBPF_API bool bpf_probe_bounded_loops(__u32 ifindex);
 
 /*
  * Get bpf_prog_info in continuous memory
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index b3938b3f8fc9..059d168452d7 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -423,6 +423,7 @@ LIBBPF_0.6.0 {
 LIBBPF_0.7.0 {
 	global:
 		bpf_btf_load;
+		bpf_probe_bounded_loops;
 		bpf_program__log_buf;
 		bpf_program__log_level;
 		bpf_program__set_log_buf;
diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
index 4bdec69523a7..e5bd691059e4 100644
--- a/tools/lib/bpf/libbpf_probes.c
+++ b/tools/lib/bpf/libbpf_probes.c
@@ -356,3 +356,23 @@ bool bpf_probe_large_insn_limit(__u32 ifindex)
 
 	return errno != E2BIG && errno != EINVAL;
 }
+
+/*
+ * Probe for bounded loop support introduced in commit 2589726d12a1
+ * ("bpf: introduce bounded loops").
+ */
+bool bpf_probe_bounded_loops(__u32 ifindex)
+{
+	struct bpf_insn insns[4] = {
+		BPF_MOV64_IMM(BPF_REG_0, 10),
+		BPF_ALU64_IMM(BPF_SUB, BPF_REG_0, 1),
+		BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, -2),
+		BPF_EXIT_INSN()
+	};
+
+	errno = 0;
+	probe_load(BPF_PROG_TYPE_SOCKET_FILTER, insns, ARRAY_SIZE(insns), NULL,
+		   0, ifindex);
+
+	return !errno;
+}
-- 
2.25.1


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

* [PATCH bpf-next v2 2/2] bpftool: Probe for bounded loop support
  2021-12-17 13:52 [PATCH bpf-next v2 1/2] libbpf: Probe for bounded loop support Paul Chaignon
@ 2021-12-17 13:52 ` Paul Chaignon
  0 siblings, 0 replies; 2+ messages in thread
From: Paul Chaignon @ 2021-12-17 13:52 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko; +Cc: bpf, Quentin Monnet

This patch probes for bounded loop support and displays the results as
part of the miscellaneous section, as shown below.

  $ bpftool feature probe | grep loops
  Bounded loop support is available
  $ bpftool feature probe macro | grep LOOPS
  #define HAVE_BOUNDED_LOOPS
  $ bpftool feature probe -j | jq .misc
  {
    "have_large_insn_limit": true,
    "have_bounded_loops": true
  }

Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Paul Chaignon <paul@isovalent.com>
---
Changes in v2:
  - Resending to fix a format error.

 tools/bpf/bpftool/feature.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/tools/bpf/bpftool/feature.c b/tools/bpf/bpftool/feature.c
index 5397077d0d9e..7aee920162e5 100644
--- a/tools/bpf/bpftool/feature.c
+++ b/tools/bpf/bpftool/feature.c
@@ -654,6 +654,18 @@ probe_large_insn_limit(const char *define_prefix, __u32 ifindex)
 			   res, define_prefix);
 }
 
+static void
+probe_bounded_loops(const char *define_prefix, __u32 ifindex)
+{
+	bool res;
+
+	res = bpf_probe_bounded_loops(ifindex);
+	print_bool_feature("have_bounded_loops",
+			   "Bounded loop support",
+			   "BOUNDED_LOOPS",
+			   res, define_prefix);
+}
+
 static void
 section_system_config(enum probe_component target, const char *define_prefix)
 {
@@ -768,6 +780,7 @@ static void section_misc(const char *define_prefix, __u32 ifindex)
 			    "/*** eBPF misc features ***/",
 			    define_prefix);
 	probe_large_insn_limit(define_prefix, ifindex);
+	probe_bounded_loops(define_prefix, ifindex);
 	print_end_section();
 }
 
-- 
2.25.1


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

end of thread, other threads:[~2021-12-17 13:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-17 13:52 [PATCH bpf-next v2 1/2] libbpf: Probe for bounded loop support Paul Chaignon
2021-12-17 13:52 ` [PATCH bpf-next v2 2/2] bpftool: " Paul Chaignon

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.