Netdev List
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: dsahern@kernel.org,
	"Stephen Hemminger" <stephen@networkplumber.org>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	"Hangbin Liu" <haliu@redhat.com>
Subject: [PATCH iproute2-next v2 6/7] lib/bpf: check for missing bpf fs mount point
Date: Thu, 19 Nov 2026 11:02:41 -0800	[thread overview]
Message-ID: <20261119190312.578885-7-stephen@networkplumber.org> (raw)
In-Reply-To: <20261119190312.578885-1-stephen@networkplumber.org>

bpf_get_work_dir() returns NULL when it cannot provide a usable
work directory. It does so even after finding a valid bpffs mount,
because bpf_gen_hierarchy() fails if the tc/ subdirectory cannot be
created: running unprivileged against the default mode=700 mount,
or as root against a read-only one.

iproute2_get_root_path() passed that NULL straight to snprintf("%s"),
which glibc renders as "(null)":

  $ tc filter add dev lo parent ffff: bpf obj prog.o sec egress
  mkdir /sys/fs/bpf/tc/ failed: Permission denied
  Continuing without mounted eBPF fs. Too old kernel?
  mkdir (null)/globals failed: No such file or directory
  Unable to load program

Check for NULL and report it instead. Also check the snprintf()
return, since root_path is handed to libbpf as pin_root_path and a
silently truncated pin root is worse than a hard failure.

The "Continuing without mounted eBPF fs" warning in bpf_init_env()
goes away with it. The legacy loader could carry on without bpffs,
which is what that message described, but the only remaining caller
needs the pin root and now fails, so the warning no longer matched
what happens next. The "Too old kernel?" guess was misleading as
well: the mount is usually present and simply not writable.

Removing the warning also removes the bpf_get_work_dir() call that
primed its cache, and the per-program-type subdirectory was only
appended on the cached path. Apply it on the first call too, so a
cls program keeps pinning under tc/ instead of at the mount root.

Fixes: 6d61a2b55799 ("lib: add libbpf support")
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/bpf_util.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/lib/bpf_util.c b/lib/bpf_util.c
index e58e54be..7ce8f52f 100644
--- a/lib/bpf_util.c
+++ b/lib/bpf_util.c
@@ -767,6 +767,11 @@ static const char *bpf_get_work_dir(enum bpf_prog_type type)
 	mnt = bpf_wrk_dir;
 out:
 	bpf_mnt_cached = true;
+	if (mnt && type) {
+		snprintf(bpf_tmp, sizeof(bpf_tmp), "%s%s/",
+			 mnt, bpf_prog_to_subdir(type));
+		return bpf_tmp;
+	}
 	return mnt;
 }
 
@@ -1068,20 +1073,27 @@ static void bpf_init_env(void)
 
 	/* Don't bother in case we fail! */
 	setrlimit(RLIMIT_MEMLOCK, &limit);
-
-	if (!bpf_get_work_dir(BPF_PROG_TYPE_UNSPEC))
-		fprintf(stderr, "Continuing without mounted eBPF fs. Too old kernel?\n");
 }
 
 int iproute2_get_root_path(enum bpf_prog_type type, char *root_path,
 			   size_t len)
 {
+	const char *wrk_dir;
 	int ret;
 
 	bpf_init_env();
 
-	snprintf(root_path, len, "%s/%s",
-		 bpf_get_work_dir(type), BPF_DIR_GLOBALS);
+	wrk_dir = bpf_get_work_dir(type);
+	if (!wrk_dir) {
+		fprintf(stderr, "No mounted eBPF fs to pin maps under\n");
+		return -1;
+	}
+
+	if ((size_t)snprintf(root_path, len, "%s/%s",
+			     wrk_dir, BPF_DIR_GLOBALS) >= len) {
+		fprintf(stderr, "Pin root path for %s too long\n", wrk_dir);
+		return -1;
+	}
 
 	ret = mkdir(root_path, S_IRWXU);
 	if (ret && errno != EEXIST) {
-- 
2.53.0


  parent reply	other threads:[~2026-11-19 19:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 19:02 [PATCH iproute2-next 0/7] bpf: fix mount point parsing and drop the legacy loader Stephen Hemminger
2026-11-19 19:02 ` [PATCH iproute2-next v2 1/7] lib/bpf: parse /proc/mounts with getmntent and check fs magic Stephen Hemminger
2026-11-19 19:02 ` [PATCH iproute2-next v2 2/7] lib/fs: " Stephen Hemminger
2026-11-19 19:02 ` [PATCH iproute2-next v2 3/7] tc: remove eBPF map export and exec bpf import Stephen Hemminger
2026-11-19 19:02 ` [PATCH iproute2-next v2 4/7] man: tc-bpf: drop references to the removed eBPF agent Stephen Hemminger
2026-11-19 19:02 ` [PATCH iproute2-next v2 5/7] lib/bpf: require libbpf 1.0 and remove legacy ELF loader Stephen Hemminger
2026-11-19 19:02 ` Stephen Hemminger [this message]
2026-11-19 19:02 ` [PATCH iproute2-next v2 7/7] testsuite: add tc bpf test Stephen Hemminger

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=20261119190312.578885-7-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dsahern@kernel.org \
    --cc=haliu@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=toke@redhat.com \
    /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