Skip to contentSkip to conversation

bpftool: Mount bpffs on provided dir instead of parent dir

PATCH Superseded Sahil Siddiq 2 participants 3 messages Mar 21, 2024 – Apr 1, 2024

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 bpftool: Mount bpffs on provided dir instead of parent dir

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.

Fixes 2a36c26fe3b8 ("bpftool: Support bpffs mountpoint as pin path for prog loadall")

Changes since v1:
- Split "mount_bpffs_for_pin" into two functions. This is done to improve maintainability and readability.

Changes since v2:
- mount_bpffs_for_pin: rename to "create_and_mount_bpffs_dir".
- mount_bpffs_given_file: rename to "mount_bpffs_given_file".
- create_and_mount_bpffs_dir:
- introduce "dir_exists" boolean.
- remove new dir if "mnt_fs" fails.
- improve error handling and error messages.

---
 tools/bpf/bpftool/common.c     | 93 ++++++++++++++++++++++++++++++----
 tools/bpf/bpftool/iter.c       |  2 +-
 tools/bpf/bpftool/main.h       |  3 +-
 tools/bpf/bpftool/prog.c       |  5 +-
 tools/bpf/bpftool/struct_ops.c |  2 +-
 5 files changed, 90 insertions(+), 15 deletions(-)
tools/bpf/bpftool/common.c +82 -11
index cc6e6aae2447..dce73105b0a5 100644
@@ -244,24 +244,95 @@ int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type)
 	return fd;
 }
 
-int mount_bpffs_for_pin(const char *name, bool is_dir)
+int create_and_mount_bpffs_dir(const char *dir_name)
 {
 	char err_str[ERR_MAX_LEN];
-	char *file;
-	char *dir;
 	int err = 0;
+	bool dir_exists;
 
-	if (is_dir && is_bpffs(name))
+	if (is_bpffs(dir_name))
 		return err;
 
-	file = malloc(strlen(name) + 1);
-	if (!file) {
+	dir_exists = (access(dir_name, F_OK) == 0);
+
+	if (!dir_exists) {
+		char *temp_name;
+		char *parent_name;
+
+		temp_name = malloc(strlen(dir_name) + 1);
+		if (!temp_name) {
+			p_err("mem alloc failed");
+			return -1;
+		}
+
+		strcpy(temp_name, dir_name);
+		parent_name = dirname(temp_name);
+
+		if (is_bpffs(parent_name)) {
+			/* nothing to do if already mounted */
+			free(temp_name);
+			return err;
+		}
+
+		if (access(parent_name, F_OK) == -1) {
+			p_err("bpf object can't be pinned since dir (%s) does not exist",
+			      parent_name);
+			free(temp_name);
+			return -1;
+		}
+
+		free(temp_name);
+	}
+
+	if (block_mount) {
+		p_err("no BPF file system found, not mounting it due to --nomount option");
+		return -1;
+	}
+
+	if (!dir_exists) {
+		err = mkdir(dir_name, 0700);
+		if (err) {
+			p_err("failed to create dir (%s): %s", dir_name, strerror(errno));
+			return err;
+		}
+	}
+
+	err = mnt_fs(dir_name, "bpf", err_str, ERR_MAX_LEN);
+	if (err) {
+		err_str[ERR_MAX_LEN - 1] = '\0';
+		p_err("can't mount BPF file system on given dir (%s): %s",
+		      dir_name, err_str);
+
+		if (!dir_exists) {
+			if (rmdir(dir_name) == -1)
+				p_err("failed to remove newly created dir (%s): %s",
+				      dir_name, strerror(errno));
+		}
+	}
+
+	return err;
+}
+
+int mount_bpffs_given_file(const char *file_name)
+{
+	char err_str[ERR_MAX_LEN];
+	char *temp_name;
+	char *dir;
+	int err = 0;
+
+	if (access(file_name, F_OK) != -1) {
+		p_err("bpf object can't be pinned since file (%s) already exists", file_name);
+		return -1;
+	}
+
+	temp_name = malloc(strlen(file_name) + 1);
+	if (!temp_name) {
 		p_err("mem alloc failed");
 		return -1;
 	}
 
-	strcpy(file, name);
-	dir = dirname(file);
+	strcpy(temp_name, file_name);
+	dir = dirname(temp_name);
 
 	if (is_bpffs(dir))
 		/* nothing to do if already mounted */
@@ -277,11 +348,11 @@ int mount_bpffs_for_pin(const char *name, bool is_dir)
 	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);
+		      file_name, err_str);
 	}
 
 out_free:
-	free(file);
+	free(temp_name);
 	return err;
 }
 
@@ -289,7 +360,7 @@ int do_pin_fd(int fd, const char *name)
 {
 	int err;
 
-	err = mount_bpffs_for_pin(name, false);
+	err = mount_bpffs_for_file(name);
 	if (err)
 		return err;
 
tools/bpf/bpftool/iter.c +1 -1
index 6b0e5202ca7a..5c39c2ed36a2 100644
@@ -76,7 +76,7 @@ static int do_pin(int argc, char **argv)
 		goto close_obj;
 	}
 
-	err = mount_bpffs_for_pin(path, false);
+	err = mount_bpffs_for_file(path);
 	if (err)
 		goto close_link;
 
tools/bpf/bpftool/main.h +2 -1
index b8bb08d10dec..9eb764fe4cc8 100644
@@ -142,7 +142,8 @@ const char *get_fd_type_name(enum bpf_obj_type type);
 char *get_fdinfo(int fd, const char *key);
 int open_obj_pinned(const char *path, bool quiet);
 int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type);
-int mount_bpffs_for_pin(const char *name, bool is_dir);
+int mount_bpffs_for_file(const char *file_name);
+int create_and_mount_bpffs_dir(const char *dir_name);
 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(int *, char ***));
 int do_pin_fd(int fd, const char *name);
 
tools/bpf/bpftool/prog.c +4 -1
index 9cb42a3366c0..4c4cf16a40ba 100644
@@ -1778,7 +1778,10 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 		goto err_close_obj;
 	}
 
-	err = mount_bpffs_for_pin(pinfile, !first_prog_only);
+	if (first_prog_only)
+		err = mount_bpffs_for_file(pinfile);
+	else
+		err = create_and_mount_bpffs_dir(pinfile);
 	if (err)
 		goto err_close_obj;
 
tools/bpf/bpftool/struct_ops.c +1 -1
index d573f2640d8e..aa43dead249c 100644
@@ -515,7 +515,7 @@ static int do_register(int argc, char **argv)
 	if (argc == 1)
 		linkdir = GET_ARG();
 
-	if (linkdir && mount_bpffs_for_pin(linkdir, true)) {
+	if (linkdir && create_and_mount_bpffs_dir(linkdir)) {
 		p_err("can't mount bpffs for pinning");
 		return -1;
 	}
Signature · 1 line
2.44.0
maintainer
#1 lore raw

Hi Sahil, sorry for the delay, and thanks for the v3! I've got some minor comments on the error messages, but the rest of the code looks mostly good - although you forgot to rename a function and it currently fails to compile. Please see inline below.

Quoted 20 lines · Sahil Siddiq

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.

Fixes 2a36c26fe3b8 ("bpftool: Support bpffs mountpoint as pin path for prog loadall")
Quoted 7 lines

Changes since v1:
- Split "mount_bpffs_for_pin" into two functions. This is done to improve maintainability and readability.

Changes since v2:
- mount_bpffs_for_pin: rename to "create_and_mount_bpffs_dir".
- mount_bpffs_given_file: rename to "mount_bpffs_given_file".

That's the same!

Quoted 20 lines

- create_and_mount_bpffs_dir:
- introduce "dir_exists" boolean.
- remove new dir if "mnt_fs" fails.
- improve error handling and error messages.

---
  tools/bpf/bpftool/common.c     | 93 ++++++++++++++++++++++++++++++----
  tools/bpf/bpftool/iter.c       |  2 +-
  tools/bpf/bpftool/main.h       |  3 +-
  tools/bpf/bpftool/prog.c       |  5 +-
  tools/bpf/bpftool/struct_ops.c |  2 +-
  5 files changed, 90 insertions(+), 15 deletions(-)
tools/bpf/bpftool/common.c +0 -0
index cc6e6aae2447..dce73105b0a5 100644
@@ -244,24 +244,95 @@ int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type)
  	return fd;

tools/bpf/bpftool/common.c int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type) · line 251

Quoted 10 lines
tools/bpf/bpftool/common.c +2 -3
  }
 
-int mount_bpffs_for_pin(const char *name, bool is_dir)
+int create_and_mount_bpffs_dir(const char *dir_name)
  {
  	char err_str[ERR_MAX_LEN];
-	char *file;
-	char *dir;
  	int err = 0;
+	bool dir_exists;

Nit: "reverse Christmas tree order": In the absence of other criteria, we tend to sort the variable definitions by line length. Please swap dir_exists and err.

Quoted 20 lines
tools/bpf/bpftool/common.c +14 -3
-	if (is_dir && is_bpffs(name))
+	if (is_bpffs(dir_name))
  		return err;
 
-	file = malloc(strlen(name) + 1);
-	if (!file) {
+	dir_exists = (access(dir_name, F_OK) == 0);
+
+	if (!dir_exists) {
+		char *temp_name;
+		char *parent_name;
+
+		temp_name = malloc(strlen(dir_name) + 1);
+		if (!temp_name) {
+			p_err("mem alloc failed");
+			return -1;
+		}
+
+		strcpy(temp_name, dir_name);

tools/bpf/bpftool/common.c int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type) · line 278

Quoted 10 lines
tools/bpf/bpftool/common.c +10 -0
+		parent_name = dirname(temp_name);
+
+		if (is_bpffs(parent_name)) {
+			/* nothing to do if already mounted */
+			free(temp_name);
+			return err;
+		}
+
+		if (access(parent_name, F_OK) == -1) {
+			p_err("bpf object can't be pinned since dir (%s) does not exist",

Maybe "can't create directory '%s' to pin BPF object: parent dir %s does not exist"?

Quoted 7 lines
tools/bpf/bpftool/common.c +7 -0
+			      parent_name);
+			free(temp_name);
+			return -1;
+		}
+
+		free(temp_name);
+	}

tools/bpf/bpftool/common.c int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type) · line 295

Quoted 10 lines
tools/bpf/bpftool/common.c +10 -0
+
+	if (block_mount) {
+		p_err("no BPF file system found, not mounting it due to --nomount option");
+		return -1;
+	}
+
+	if (!dir_exists) {
+		err = mkdir(dir_name, 0700);
+		if (err) {
+			p_err("failed to create dir (%s): %s", dir_name, strerror(errno));

Or "failed to create directory '%s': %s".

Quoted 4 lines
tools/bpf/bpftool/common.c +4 -0
+			return err;
+		}
+	}
+

tools/bpf/bpftool/common.c int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type) · line 309

Quoted 10 lines
tools/bpf/bpftool/common.c +10 -0
+	err = mnt_fs(dir_name, "bpf", err_str, ERR_MAX_LEN);
+	if (err) {
+		err_str[ERR_MAX_LEN - 1] = '\0';
+		p_err("can't mount BPF file system on given dir (%s): %s",
+		      dir_name, err_str);
+
+		if (!dir_exists) {
+			if (rmdir(dir_name) == -1)
+				p_err("failed to remove newly created dir (%s): %s",
+				      dir_name, strerror(errno));

We're already on an error path, I'd maybe drop the check on the return value from rmdir(). It doesn't matter much if it fails, I think.

tools/bpf/bpftool/common.c int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type) · line 316

Quoted 7 lines
tools/bpf/bpftool/common.c +7 -0
+		}
+	}
+
+	return err;
+}
+
+int mount_bpffs_given_file(const char *file_name)

This function must be renamed, as you did at the call sites.

tools/bpf/bpftool/common.c int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type) · line 324

Quoted 8 lines
tools/bpf/bpftool/common.c +8 -0
+{
+	char err_str[ERR_MAX_LEN];
+	char *temp_name;
+	char *dir;
+	int err = 0;
+
+	if (access(file_name, F_OK) != -1) {
+		p_err("bpf object can't be pinned since file (%s) already exists", file_name);

Not sure why the parenthesis, let's use quotes? Can we also replace "since" with "because", or "given that", or simply a colon, please? And I'd drop the passive voice, here's what I would use:

"can't pin BPF object: path '%s' already exists"

Quoted 17 lines
tools/bpf/bpftool/common.c +7 -2
+		return -1;
+	}
+
+	temp_name = malloc(strlen(file_name) + 1);
+	if (!temp_name) {
  		p_err("mem alloc failed");
  		return -1;
  	}
 
-	strcpy(file, name);
-	dir = dirname(file);
+	strcpy(temp_name, file_name);
+	dir = dirname(temp_name);
 
  	if (is_bpffs(dir))
  		/* nothing to do if already mounted */

tools/bpf/bpftool/common.c int mount_bpffs_for_pin(const char *name, bool is_dir) · line 350

Quoted 3 lines
tools/bpf/bpftool/common.c +0 -0
@@ -277,11 +348,11 @@ int mount_bpffs_for_pin(const char *name, bool is_dir)
  	if (err) {
  		err_str[ERR_MAX_LEN - 1] = '\0';
  		p_err("can't mount BPF file system to pin the object (%s): %s",

"object '%s'" instead of "the object (%s)" please.

Quoted 11 lines
tools/bpf/bpftool/common.c +2 -2
-		      name, err_str);
+		      file_name, err_str);
  	}
 
  out_free:
-	free(file);
+	free(temp_name);
  	return err;
  }
 

tools/bpf/bpftool/common.c int do_pin_fd(int fd, const char *name) · line 363

Quoted 5 lines
tools/bpf/bpftool/common.c +1 -1
@@ -289,7 +360,7 @@ int do_pin_fd(int fd, const char *name)
  {
  	int err;
 
-	err = mount_bpffs_for_pin(name, false);
+	err = mount_bpffs_for_file(name);

Compiler says "undefined reference to mount_bpffs_for_file", please build and test your patch. See also how most CI jobs fail:
https://github.com/kernel-patches/bpf/pull/6614

Quentin

author
#2 lore raw

Hi,

Thank you for the review. I have refactored the error messages. I have removed a few more parantheses.

Quoted 3 lines · IST Quentin Monnet

[...]
We're already on an error path, I'd maybe drop the check on the return value from rmdir(). It doesn't matter much if it fails, I think.

I was thinking that if we fail to mount but manage to create the dir successfully, then we should also clean up that new dir and send a notification if we fail to remove that dir so the user can manually remove it.

I guess it's ok to remove that error message though since dealing with the "fail to mount" error will anyway require manual intervention.

Quoted 2 lines

[...]
This function must be renamed, as you did at the call sites.

Sorry, I must have forgotten to copy this when copying the changes from the bpftool repo to my local bpf-next tree.

Thanks,
Sahil