Skip to content

Commit

Permalink
bpftool: Mount bpffs on provided dir instead of parent dir
Browse files Browse the repository at this point in the history
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/[email protected]/T/#t

Closes: libbpf/bpftool#100

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

Signed-off-by: Sahil Siddiq <[email protected]>
  • Loading branch information
valdaarhun authored and Kernel Patches Daemon committed Mar 9, 2024
1 parent dbc26f1 commit b38ef6c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 15 deletions.
78 changes: 67 additions & 11 deletions tools/bpf/bpftool/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,24 +244,80 @@ 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 mount_bpffs_on_dir(const char *dir_name)
{
char err_str[ERR_MAX_LEN];
char *file;
char *dir;
int err = 0;

if (is_dir && is_bpffs(name))
if (is_bpffs(dir_name))
return err;

file = malloc(strlen(name) + 1);
if (!file) {
if (access(dir_name, F_OK) == -1) {
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;
}

free(temp_name);

if (block_mount) {
p_err("no BPF file system found, not mounting it due to --nomount option");
return -1;
}

err = mkdir(dir_name, 0700);
if (err) {
p_err("failed to create dir (%s): %s", dir_name, strerror(errno));
return err;
}
} else if (block_mount) {
p_err("no BPF file system found, not mounting it due to --nomount option");
return -1;
}

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);
}

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 */
Expand All @@ -277,19 +333,19 @@ 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;
}

int do_pin_fd(int fd, const char *name)
{
int err;

err = mount_bpffs_for_pin(name, false);
err = mount_bpffs_given_file(name);
if (err)
return err;

Expand Down
2 changes: 1 addition & 1 deletion tools/bpf/bpftool/iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_given_file(path);
if (err)
goto close_link;

Expand Down
3 changes: 2 additions & 1 deletion tools/bpf/bpftool/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_given_file(const char *file_name);
int mount_bpffs_on_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);

Expand Down
5 changes: 4 additions & 1 deletion tools/bpf/bpftool/prog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_given_file(pinfile);
else
err = mount_bpffs_on_dir(pinfile);
if (err)
goto err_close_obj;

Expand Down
2 changes: 1 addition & 1 deletion tools/bpf/bpftool/struct_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 && mount_bpffs_on_dir(linkdir)) {
p_err("can't mount bpffs for pinning");
return -1;
}
Expand Down

0 comments on commit b38ef6c

Please sign in to comment.