BPF List
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii@kernel.org>
To: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
	martin.lau@kernel.org
Cc: andrii@kernel.org, kernel-team@meta.com, Tejun Heo <tj@kernel.org>
Subject: [PATCH bpf-next 5/7] libbpf: improve early detection of doomed-to-fail BPF program loading
Date: Mon,  6 May 2024 17:13:33 -0700	[thread overview]
Message-ID: <20240507001335.1445325-6-andrii@kernel.org> (raw)
In-Reply-To: <20240507001335.1445325-1-andrii@kernel.org>

Extend libbpf's pre-load checks for BPF programs, detecting more typical
conditions that are destinated to cause BPF program failure. This is an
opportunity to provide more helpful and actionable error message to
users, instead of potentially very confusing BPF verifier log and/or
error.

In this case, we detect struct_ops BPF program that was not referenced
anywhere, but still attempted to be loaded (according to libbpf logic).
Suggest that the program might need to be used in some struct_ops
variable. User will get a message of the following kind:

  libbpf: prog 'test_1_forgotten': SEC("struct_ops") program isn't referenced anywhere, did you forget to use it?

Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 tools/lib/bpf/libbpf.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 04de4fb81785..5401f2df463d 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -7372,7 +7372,11 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
 	__u32 log_level = prog->log_level;
 	int ret, err;
 
-	if (prog->type == BPF_PROG_TYPE_UNSPEC) {
+	/* Be more helpful by rejecting programs that can't be validated early
+	 * with more meaningful and actionable error message.
+	 */
+	switch (prog->type) {
+	case BPF_PROG_TYPE_UNSPEC:
 		/*
 		 * The program type must be set.  Most likely we couldn't find a proper
 		 * section definition at load time, and thus we didn't infer the type.
@@ -7380,6 +7384,15 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
 		pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n",
 			prog->name, prog->sec_name);
 		return -EINVAL;
+	case BPF_PROG_TYPE_STRUCT_OPS:
+		if (prog->attach_btf_id == 0) {
+			pr_warn("prog '%s': SEC(\"struct_ops\") program isn't referenced anywhere, did you forget to use it?\n",
+				prog->name);
+			return -EINVAL;
+		}
+		break;
+	default:
+		break;
 	}
 
 	if (!insns || !insns_cnt)
-- 
2.43.0


  parent reply	other threads:[~2024-05-07  0:13 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-07  0:13 [PATCH bpf-next 0/7] libbpf: further struct_ops fixes and improvements Andrii Nakryiko
2024-05-07  0:13 ` [PATCH bpf-next 1/7] libbpf: remove unnecessary struct_ops prog validity check Andrii Nakryiko
2024-05-07  0:13 ` [PATCH bpf-next 2/7] libbpf: handle yet another corner case of nulling out struct_ops program Andrii Nakryiko
2024-05-07  0:13 ` [PATCH bpf-next 3/7] selftests/bpf: add another struct_ops callback use case test Andrii Nakryiko
2024-05-07  0:13 ` [PATCH bpf-next 4/7] libbpf: fix libbpf_strerror_r() handling unknown errors Andrii Nakryiko
2024-05-07  0:13 ` Andrii Nakryiko [this message]
2024-05-07  0:13 ` [PATCH bpf-next 6/7] selftests/bpf: validate struct_ops early failure detection logic Andrii Nakryiko
2024-05-07  0:13 ` [PATCH bpf-next 7/7] selftests/bpf: shorten subtest names for struct_ops_module test Andrii Nakryiko
2024-05-08  0:30 ` [PATCH bpf-next 0/7] libbpf: further struct_ops fixes and improvements patchwork-bot+netdevbpf

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=20240507001335.1445325-6-andrii@kernel.org \
    --to=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@meta.com \
    --cc=martin.lau@kernel.org \
    --cc=tj@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