bpftool: Support bpffs mountpoint as pin path for prog loadall
authorPengcheng Yang <yangpc@wangsu.com>
Sat, 6 May 2023 03:07:19 +0000 (11:07 +0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 17 May 2023 13:58:18 +0000 (15:58 +0200)
Currently, when using prog loadall and the pin path is a bpffs mountpoint,
bpffs will be repeatedly mounted to the parent directory of the bpffs
mountpoint path. For example, a `bpftool prog loadall test.o /sys/fs/bpf`
will trigger this.

Signed-off-by: Pengcheng Yang <yangpc@wangsu.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/1683342439-3677-1-git-send-email-yangpc@wangsu.com
tools/bpf/bpftool/common.c
tools/bpf/bpftool/iter.c
tools/bpf/bpftool/main.h
tools/bpf/bpftool/prog.c
tools/bpf/bpftool/struct_ops.c

index 1360c82..cc6e6aa 100644 (file)
@@ -68,7 +68,7 @@ void p_info(const char *fmt, ...)
        va_end(ap);
 }
 
-static bool is_bpffs(char *path)
+static bool is_bpffs(const char *path)
 {
        struct statfs st_fs;
 
@@ -244,13 +244,16 @@ int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type)
        return fd;
 }
 
-int mount_bpffs_for_pin(const char *name)
+int mount_bpffs_for_pin(const char *name, bool is_dir)
 {
        char err_str[ERR_MAX_LEN];
        char *file;
        char *dir;
        int err = 0;
 
+       if (is_dir && is_bpffs(name))
+               return err;
+
        file = malloc(strlen(name) + 1);
        if (!file) {
                p_err("mem alloc failed");
@@ -286,7 +289,7 @@ int do_pin_fd(int fd, const char *name)
 {
        int err;
 
-       err = mount_bpffs_for_pin(name);
+       err = mount_bpffs_for_pin(name, false);
        if (err)
                return err;
 
index 9a1d236..6b0e520 100644 (file)
@@ -76,7 +76,7 @@ static int do_pin(int argc, char **argv)
                goto close_obj;
        }
 
-       err = mount_bpffs_for_pin(path);
+       err = mount_bpffs_for_pin(path, false);
        if (err)
                goto close_link;
 
index a49534d..b8bb08d 100644 (file)
@@ -142,7 +142,7 @@ 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);
+int mount_bpffs_for_pin(const char *name, bool is_dir);
 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(int *, char ***));
 int do_pin_fd(int fd, const char *name);
 
index 91b6075..1f736dc 100644 (file)
@@ -1739,7 +1739,7 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
                goto err_close_obj;
        }
 
-       err = mount_bpffs_for_pin(pinfile);
+       err = mount_bpffs_for_pin(pinfile, !first_prog_only);
        if (err)
                goto err_close_obj;
 
index 57c3da7..3ebc9fe 100644 (file)
@@ -509,7 +509,7 @@ static int do_register(int argc, char **argv)
        if (argc == 1)
                linkdir = GET_ARG();
 
-       if (linkdir && mount_bpffs_for_pin(linkdir)) {
+       if (linkdir && mount_bpffs_for_pin(linkdir, true)) {
                p_err("can't mount bpffs for pinning");
                return -1;
        }