BPF List
 help / color / mirror / Atom feed
From: Sahil Siddiq <icegambit91@gmail.com>
To: quentin@isovalent.com, ast@kernel.org, daniel@iogearbox.net,
	andrii@kernel.org
Cc: martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org,
	yonghong.song@linux.dev, john.fastabend@gmail.com,
	kpsingh@kernel.org, sdf@google.com, haoluo@google.com,
	jolsa@kernel.org, bpf@vger.kernel.org,
	Sahil Siddiq <icegambit91@gmail.com>
Subject: [PATCH bpf-next] bpftool: Mount bpffs on provided dir instead of parent dir
Date: Thu, 29 Feb 2024 18:35:42 +0530	[thread overview]
Message-ID: <20240229130543.17491-1-icegambit91@gmail.com> (raw)

When pinning programs/objects under PATH (eg: during "bpftool prog
loadall") the bpffs is mounted on the parent dir of PATH in the
following situations:
- the given dir exists but it is not bpffs.
- the given dir doesn't exist and the parent dir is not bpffs.

Mounting on the parent dir can also have the unintentional side-
effect of hiding other files located under the parent dir.

If the given dir exists but is not bpffs, then the bpffs should
be mounted on the given dir and not its parent dir.

Similarly, if the given dir doesn't exist and its parent dir is not
bpffs, then the given dir should be created and the bpffs should be
mounted on this new dir.

Link: https://lore.kernel.org/bpf/2da44d24-74ae-a564-1764-afccf395eeec@isovalent.com/T/#t

Closes: https://github.com/libbpf/bpftool/issues/100

Signed-off-by: Sahil Siddiq <icegambit91@gmail.com>
---
 tools/bpf/bpftool/common.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c
index cc6e6aae2447..6b2c3e82c19e 100644
--- a/tools/bpf/bpftool/common.c
+++ b/tools/bpf/bpftool/common.c
@@ -254,6 +254,17 @@ int mount_bpffs_for_pin(const char *name, bool is_dir)
 	if (is_dir && is_bpffs(name))
 		return err;
 
+	if (is_dir && access(name, F_OK) != -1) {
+		err = mnt_fs(name, "bpf", err_str, ERR_MAX_LEN);
+		if (err) {
+			err_str[ERR_MAX_LEN - 1] = '\0';
+			p_err("can't mount BPF file system to pin the object (%s): %s",
+				name, err_str);
+		}
+
+		return err;
+	}
+
 	file = malloc(strlen(name) + 1);
 	if (!file) {
 		p_err("mem alloc failed");
@@ -273,7 +284,17 @@ int mount_bpffs_for_pin(const char *name, bool is_dir)
 		goto out_free;
 	}
 
-	err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
+	if (is_dir) {
+		err = mkdir(name, 0700);
+		if (err) {
+			err_str[ERR_MAX_LEN - 1] = '\0';
+			p_err("failed to mkdir (%s): %s",
+				name, err_str);
+			goto out_free;
+		}
+	}
+
+	err = mnt_fs(is_dir ? name : dir, "bpf", err_str, ERR_MAX_LEN);
 	if (err) {
 		err_str[ERR_MAX_LEN - 1] = '\0';
 		p_err("can't mount BPF file system to pin the object (%s): %s",
-- 
2.44.0


             reply	other threads:[~2024-02-29 13:05 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-29 13:05 Sahil Siddiq [this message]
2024-02-29 14:59 ` [PATCH bpf-next] bpftool: Mount bpffs on provided dir instead of parent dir Quentin Monnet
2024-02-29 19:50   ` Sahil
2024-03-01 20:28   ` Sahil
2024-03-04 13:50     ` Quentin Monnet
2024-03-04 20:34       ` Sahil
2024-03-05 10:35         ` Quentin Monnet
2024-03-07 20:12           ` Sahil

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=20240229130543.17491-1-icegambit91@gmail.com \
    --to=icegambit91@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=quentin@isovalent.com \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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