Skip to contentSkip to conversation

libbpf: Add bpf_obj_get_opts()

PATCH Applied Joe Burton 5 participants 18 messages Jul 19, 2022 – Jul 29, 2022

Subsystems (2)

v2 of 3 Applied Latest is v3 →

lore
Share & tools
lore

Joe Burton

author
#0 lore raw

PATCH v2 1/1 libbpf: Add bpf_obj_get_opts()

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF maps. Without a method like this, users must manually make the syscall.

---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
tools/lib/bpf/bpf.c +10 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char *pathname)
 }
 
 int bpf_obj_get(const char *pathname)
+{
+	LIBBPF_OPTS(bpf_obj_get_opts, opts);
+	return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
 {
 	union bpf_attr attr;
 	int fd;
 
+	if (!OPTS_VALID(opts, bpf_obj_get_opts))
+		return libbpf_err(-EINVAL);
+
 	memset(&attr, 0, sizeof(attr));
 	attr.pathname = ptr_to_u64((void *)pathname);
+	attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
 	fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
 	return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +9 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values
 				    __u32 *count,
 				    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+	size_t sz; /* size of this struct for forward/backward compatibility */
+
+	__u32 file_flags;
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+				const struct bpf_obj_get_opts *opts);
 
 struct bpf_prog_attach_opts {
 	size_t sz; /* size of this struct for forward/backward compatibility */
tools/lib/bpf/libbpf.map +1 -0
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
 	global:
+		bpf_obj_get_opts;
 		bpf_prog_query_opts;
 		bpf_program__attach_ksyscall;
 		btf__add_enum64;
Signature · 1 line
2.37.0.170.g444d1eabd0-goog
Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Joe Burton

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF maps. Without a method like this, users must manually make the syscall.

For context:
We've found this out while we were trying to add support for unpriv processes to open pinned r-x maps.
Maybe this deserves a test as well? Not sure.

Quoted 70 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
tools/lib/bpf/bpf.c +10 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char *pathname)
 }
 
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +9 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward compatibility */
+
+       __u32 file_flags;
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward compatibility */
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#2 lore raw
Quoted 13 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Tuesday, July 19, 2022 10:40 PM

Quoted 9 lines · Joe Burton

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF maps. Without a method like this, users must manually make the syscall.

Quoted 8 lines
Quoted 1 line

For context:
We've found this out while we were trying to add support for unpriv processes to open pinned r-x maps.
Maybe this deserves a test as well? Not sure.

Hi Stanislav, Joe

I noticed now this patch. I'm doing a broader work to add opts to bpf_*_get_fd_by_id(). I also adjusted permissions of bpftool depending on the operation type (e.g. show, dump: BPF_F_RDONLY).

Will send it soon (I'm trying to solve an issue with the CI, where libbfd is not available in the VM doing actual tests).

Roberto

Quoted 72 lines
Quoted 11 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
tools/lib/bpf/bpf.c +0 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char *pathname) · line 586

Quoted 9 lines
tools/lib/bpf/bpf.c +6 -0
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char *pathname)
 }
 
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts

*opts)

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd, const

Quoted 18 lines
tools/lib/bpf/bpf.c +4 -0
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int fd, const

void *keys, const void *values

Quoted 32 lines
tools/lib/bpf/bpf.h +9 -0
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward compatibility */
+
+       __u32 file_flags;
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward compatibility */
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#3 lore raw
Quoted 23 lines · Roberto Sassu
Quoted 14 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Tuesday, July 19, 2022 10:40 PM

Quoted 9 lines · Joe Burton

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF maps. Without a method like this, users must manually make the syscall.

Quoted 8 lines
Quoted 1 line

For context:
We've found this out while we were trying to add support for unpriv processes to open pinned r-x maps.
Maybe this deserves a test as well? Not sure.

Quoted 8 lines

Hi Stanislav, Joe

I noticed now this patch. I'm doing a broader work to add opts to bpf_*_get_fd_by_id(). I also adjusted permissions of bpftool depending on the operation type (e.g. show, dump: BPF_F_RDONLY).

Will send it soon (I'm trying to solve an issue with the CI, where libbfd is not available in the VM doing actual tests).

Is something like this patch included in your series as well? Can you use this new interface or do you need something different?

Quoted 74 lines

Roberto

Quoted 72 lines
Quoted 11 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
tools/lib/bpf/bpf.c +0 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char *pathname) · line 586

Quoted 9 lines
tools/lib/bpf/bpf.c +6 -0
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char *pathname)
 }
 
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts

*opts)

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd, const

Quoted 18 lines
tools/lib/bpf/bpf.c +4 -0
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int fd, const

void *keys, const void *values

Quoted 32 lines
tools/lib/bpf/bpf.h +9 -0
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward compatibility */
+
+       __u32 file_flags;
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward compatibility */
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#4 lore raw
Quoted 33 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Wednesday, July 20, 2022 5:57 PM

Quoted 29 lines · Roberto Sassu
Quoted 14 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Tuesday, July 19, 2022 10:40 PM

Quoted 9 lines · Joe Burton

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF maps. Without a method like this, users must manually make the syscall.

Quoted 8 lines
Quoted 1 line

For context:
We've found this out while we were trying to add support for unpriv processes to open pinned r-x maps.
Maybe this deserves a test as well? Not sure.

Hi Stanislav, Joe

I noticed now this patch. I'm doing a broader work to add opts to bpf_*_get_fd_by_id(). I also adjusted permissions of bpftool depending on the operation type (e.g. show, dump: BPF_F_RDONLY).

Quoted 5 lines
Quoted 2 lines

Will send it soon (I'm trying to solve an issue with the CI, where libbfd is not available in the VM doing actual tests).

Is something like this patch included in your series as well? Can you use this new interface or do you need something different?

It is very similar. Except that I called it bpf_get_fd_opts, as it is shared with the bpf_*_get_fd_by_id() functions. The member name is just flags, plus an extra u32 for alignment.

It needs to be shared, as there are functions in bpftool calling both. Since the meaning of flags is the same, seems ok sharing.

Roberto

Quoted 76 lines
Quoted 15 lines

Roberto

Quoted 13 lines
Quoted 13 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
tools/lib/bpf/bpf.c +0 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char *pathname)
 }
 
Quoted 7 lines
Quoted 7 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char *pathname)

Quoted 7 lines
tools/lib/bpf/bpf.c +6 -0
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct

bpf_obj_get_opts

Quoted 12 lines
Quoted 12 lines

*opts)

Quoted 11 lines
tools/lib/bpf/bpf.c +4 -0
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
Quoted 7 lines
Quoted 7 lines

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd,

Quoted 7 lines
tools/lib/bpf/bpf.c +0 -0
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int fd,

const

Quoted 33 lines
Quoted 33 lines

void *keys, const void *values

Quoted 32 lines
tools/lib/bpf/bpf.h +9 -0
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward compatibility */
+
+       __u32 file_flags;
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward compatibility */
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#5 lore raw
Quoted 37 lines · Roberto Sassu
Quoted 37 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Wednesday, July 20, 2022 5:57 PM

Quoted 31 lines · Roberto Sassu
Quoted 14 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Tuesday, July 19, 2022 10:40 PM

Quoted 9 lines · Joe Burton

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF maps. Without a method like this, users must manually make the syscall.

Quoted 8 lines
Quoted 1 line

For context:
We've found this out while we were trying to add support for unpriv processes to open pinned r-x maps.
Maybe this deserves a test as well? Not sure.

Hi Stanislav, Joe

I noticed now this patch. I'm doing a broader work to add opts to bpf_*_get_fd_by_id(). I also adjusted permissions of bpftool depending on the operation type (e.g. show, dump: BPF_F_RDONLY).

Will send it soon (I'm trying to solve an issue with the CI, where libbfd is not available in the VM doing actual tests).

Quoted 6 lines
Quoted 2 lines

Is something like this patch included in your series as well? Can you use this new interface or do you need something different?

It is very similar. Except that I called it bpf_get_fd_opts, as it is shared with the bpf_*_get_fd_by_id() functions. The member name is just flags, plus an extra u32 for alignment.

We can bikeshed the naming, but we've been using existing conventions where opts fields match syscall fields, that seems like a sensible thing to do?

Quoted 2 lines

It needs to be shared, as there are functions in bpftool calling both. Since the meaning of flags is the same, seems ok sharing.

So I guess there are no objections to the current patch? If it gets accepted, you should be able to drop some of your code and use this new bpf_obj_get_opts..

Quoted 78 lines

Roberto

Quoted 76 lines
Quoted 15 lines

Roberto

Quoted 13 lines
Quoted 13 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
tools/lib/bpf/bpf.c +0 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char *pathname)
 }
 
Quoted 7 lines
Quoted 7 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char *pathname)

Quoted 7 lines
tools/lib/bpf/bpf.c +6 -0
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct

bpf_obj_get_opts

Quoted 12 lines
Quoted 12 lines

*opts)

Quoted 11 lines
tools/lib/bpf/bpf.c +4 -0
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
Quoted 7 lines
Quoted 7 lines

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd,

Quoted 7 lines
tools/lib/bpf/bpf.c +0 -0
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int fd,

const

Quoted 33 lines
Quoted 33 lines

void *keys, const void *values

Quoted 32 lines
tools/lib/bpf/bpf.h +9 -0
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward compatibility */
+
+       __u32 file_flags;
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward compatibility */
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#6 lore raw
Quoted 46 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:38 AM

Quoted 4 lines · Roberto Sassu
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Wednesday, July 20, 2022 5:57 PM
On Wed, Jul 20, 2022 at 1:02 AM Roberto Sassu

<[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Tuesday, July 19, 2022 10:40 PM
On Tue, Jul 19, 2022 at 12:40 PM Joe Burton

<[email protected]>

Quoted 31 lines
Quoted 30 lines
Quoted 19 lines
Quoted 10 lines
Quoted 9 lines · wrote:

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF maps. Without a method like this, users must manually make the syscall.

Quoted 8 lines
Quoted 1 line

For context:
We've found this out while we were trying to add support for unpriv processes to open pinned r-x maps.
Maybe this deserves a test as well? Not sure.

Quoted 8 lines

Hi Stanislav, Joe

I noticed now this patch. I'm doing a broader work to add opts to bpf_*_get_fd_by_id(). I also adjusted permissions of bpftool depending on the operation type (e.g. show, dump: BPF_F_RDONLY).

Will send it soon (I'm trying to solve an issue with the CI, where libbfd is not available in the VM doing actual tests).

Is something like this patch included in your series as well? Can you use this new interface or do you need something different?

Quoted 7 lines
Quoted 3 lines

It is very similar. Except that I called it bpf_get_fd_opts, as it is shared with the bpf_*_get_fd_by_id() functions. The member name is just flags, plus an extra u32 for alignment.

We can bikeshed the naming, but we've been using existing conventions where opts fields match syscall fields, that seems like a sensible thing to do?

The only problem is that bpf_*_get_fd_by_id() functions would set the open_flags member of bpf_attr.

Flags would be good for both, even if not exact. Believe me, duplicating the opts would just create more confusion.

Quoted 6 lines
Quoted 2 lines

It needs to be shared, as there are functions in bpftool calling both. Since the meaning of flags is the same, seems ok sharing.

So I guess there are no objections to the current patch? If it gets accepted, you should be able to drop some of your code and use this new bpf_obj_get_opts..

If you use a name good also for bpf_*_get_fd_by_id() and flags as structure member name, that would be ok.

Roberto

Quoted 81 lines
Quoted 10 lines

Roberto

Quoted 8 lines
Quoted 8 lines

Roberto

Quoted 6 lines
Quoted 6 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char

Quoted 5 lines
tools/lib/bpf/bpf.c +0 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char

*pathname)

Quoted 30 lines
Quoted 30 lines
Quoted 9 lines
Quoted 9 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char *pathname)

Quoted 9 lines
tools/lib/bpf/bpf.c +6 -0
 }
 
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct

bpf_obj_get_opts

Quoted 12 lines
Quoted 12 lines

*opts)

Quoted 11 lines
tools/lib/bpf/bpf.c +4 -0
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
Quoted 7 lines
Quoted 7 lines

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd,

Quoted 7 lines
tools/lib/bpf/bpf.c +0 -0
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int fd,

const

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

void *keys, const void *values

Quoted 5 lines
tools/lib/bpf/bpf.h +2 -0
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward compatibility

*/

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
tools/lib/bpf/bpf.h +7 -0
+
+       __u32 file_flags;
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd, · line 286

Quoted 2 lines
tools/lib/bpf/bpf.h +0 -0
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward compatibility

*/

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#7 lore raw
Quoted 55 lines · Roberto Sassu
Quoted 47 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:38 AM

Quoted 4 lines · Roberto Sassu
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Wednesday, July 20, 2022 5:57 PM
On Wed, Jul 20, 2022 at 1:02 AM Roberto Sassu

<[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Tuesday, July 19, 2022 10:40 PM
On Tue, Jul 19, 2022 at 12:40 PM Joe Burton

<[email protected]>

Quoted 31 lines
Quoted 30 lines
Quoted 19 lines
Quoted 10 lines
Quoted 9 lines · wrote:

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF maps. Without a method like this, users must manually make the syscall.

Quoted 8 lines
Quoted 1 line

For context:
We've found this out while we were trying to add support for unpriv processes to open pinned r-x maps.
Maybe this deserves a test as well? Not sure.

Quoted 8 lines

Hi Stanislav, Joe

I noticed now this patch. I'm doing a broader work to add opts to bpf_*_get_fd_by_id(). I also adjusted permissions of bpftool depending on the operation type (e.g. show, dump: BPF_F_RDONLY).

Will send it soon (I'm trying to solve an issue with the CI, where libbfd is not available in the VM doing actual tests).

Is something like this patch included in your series as well? Can you use this new interface or do you need something different?

Quoted 7 lines
Quoted 3 lines

It is very similar. Except that I called it bpf_get_fd_opts, as it is shared with the bpf_*_get_fd_by_id() functions. The member name is just flags, plus an extra u32 for alignment.

We can bikeshed the naming, but we've been using existing conventions where opts fields match syscall fields, that seems like a sensible thing to do?

Quoted 5 lines

The only problem is that bpf_*_get_fd_by_id() functions would set the open_flags member of bpf_attr.

Flags would be good for both, even if not exact. Believe me, duplicating the opts would just create more confusion.

Wait, that's completely different, right? We are talking here about BPF_OBJ_GET (which has related BPF_OBJ_PIN).
Your GET_XXX_BY_ID are different so you'll still have to have another wrapper with opts?

Quoted 93 lines
Quoted 6 lines
Quoted 2 lines

It needs to be shared, as there are functions in bpftool calling both. Since the meaning of flags is the same, seems ok sharing.

So I guess there are no objections to the current patch? If it gets accepted, you should be able to drop some of your code and use this new bpf_obj_get_opts..

If you use a name good also for bpf_*_get_fd_by_id() and flags as structure member name, that would be ok.

Roberto

Quoted 81 lines
Quoted 10 lines

Roberto

Quoted 8 lines
Quoted 8 lines

Roberto

Quoted 6 lines
Quoted 6 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char

Quoted 5 lines
tools/lib/bpf/bpf.c +0 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char

*pathname)

Quoted 30 lines
Quoted 30 lines
Quoted 9 lines
Quoted 9 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char · line 586

Quoted 9 lines
tools/lib/bpf/bpf.c +6 -0
 }
 
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct

bpf_obj_get_opts

Quoted 12 lines
Quoted 12 lines

*opts)

Quoted 11 lines
tools/lib/bpf/bpf.c +4 -0
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
Quoted 7 lines
Quoted 7 lines

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd,

Quoted 7 lines
tools/lib/bpf/bpf.c +0 -0
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int fd,

const

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

void *keys, const void *values

Quoted 5 lines
tools/lib/bpf/bpf.h +2 -0
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward compatibility

*/

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
tools/lib/bpf/bpf.h +7 -0
+
+       __u32 file_flags;
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd, · line 286

Quoted 2 lines
tools/lib/bpf/bpf.h +0 -0
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward compatibility

*/

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#8 lore raw
Quoted 64 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:48 AM

Quoted 4 lines · Roberto Sassu
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:38 AM
On Wed, Jul 20, 2022 at 3:30 PM Roberto Sassu

<[email protected]>

Quoted 12 lines
Quoted 12 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Wednesday, July 20, 2022 5:57 PM
On Wed, Jul 20, 2022 at 1:02 AM Roberto Sassu

<[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Tuesday, July 19, 2022 10:40 PM
On Tue, Jul 19, 2022 at 12:40 PM Joe Burton

<[email protected]>

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines · wrote:

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF

maps.

Quoted 34 lines
Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
Quoted 11 lines
Quoted 10 lines
Quoted 3 lines

Without a method like this, users must manually make the syscall.

For context:
We've found this out while we were trying to add support for unpriv processes to open pinned r-x maps.
Maybe this deserves a test as well? Not sure.

Quoted 8 lines

Hi Stanislav, Joe

I noticed now this patch. I'm doing a broader work to add opts to bpf_*_get_fd_by_id(). I also adjusted permissions of bpftool depending on the operation type (e.g. show, dump: BPF_F_RDONLY).

Will send it soon (I'm trying to solve an issue with the CI, where libbfd is not available in the VM doing actual tests).

Is something like this patch included in your series as well? Can you use this new interface or do you need something different?

Quoted 7 lines
Quoted 3 lines

It is very similar. Except that I called it bpf_get_fd_opts, as it is shared with the bpf_*_get_fd_by_id() functions. The member name is just flags, plus an extra u32 for alignment.

We can bikeshed the naming, but we've been using existing conventions where opts fields match syscall fields, that seems like a sensible thing to do?

The only problem is that bpf_*_get_fd_by_id() functions would set the open_flags member of bpf_attr.

Quoted 7 lines
Quoted 2 lines

Flags would be good for both, even if not exact. Believe me, duplicating the opts would just create more confusion.

Wait, that's completely different, right? We are talking here about BPF_OBJ_GET (which has related BPF_OBJ_PIN).
Your GET_XXX_BY_ID are different so you'll still have to have another wrapper with opts?

Yes, they have different wrappers, just accept the same opts as obj_get(). From bpftool subcommands you want to set the correct permission, and propagate it uniformly to bpf_*_get_fd_by_id() or obj_get(). See map_parse_fds().

Roberto

Quoted 96 lines
Quoted 50 lines
Quoted 6 lines
Quoted 2 lines

It needs to be shared, as there are functions in bpftool calling both. Since the meaning of flags is the same, seems ok sharing.

So I guess there are no objections to the current patch? If it gets accepted, you should be able to drop some of your code and use this new bpf_obj_get_opts..

If you use a name good also for bpf_*_get_fd_by_id() and flags as structure member name, that would be ok.

Roberto

Quoted 38 lines
Quoted 10 lines

Roberto

Quoted 8 lines
Quoted 8 lines

Roberto

Quoted 6 lines
Quoted 6 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char

Quoted 5 lines
tools/lib/bpf/bpf.c +0 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char

*pathname)

Quoted 22 lines
Quoted 22 lines
Quoted 9 lines
Quoted 9 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char · line 586

Quoted 9 lines
tools/lib/bpf/bpf.c +6 -0
 }
 
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct

bpf_obj_get_opts

Quoted 12 lines
Quoted 12 lines

*opts)

Quoted 11 lines
tools/lib/bpf/bpf.c +4 -0
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int

Quoted 7 lines
tools/lib/bpf/bpf.c +0 -0
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int

fd,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

const

Quoted 6 lines
Quoted 6 lines

void *keys, const void *values

Quoted 5 lines
tools/lib/bpf/bpf.h +2 -0
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward

compatibility

Quoted 11 lines
Quoted 11 lines

*/

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
tools/lib/bpf/bpf.h +7 -0
+
+       __u32 file_flags;
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int · line 286

Quoted 2 lines
tools/lib/bpf/bpf.h +0 -0
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward

compatibility

Quoted 16 lines
Quoted 16 lines

*/

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#9 lore raw
Quoted 73 lines · Roberto Sassu
Quoted 65 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:48 AM

Quoted 4 lines · Roberto Sassu
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:38 AM
On Wed, Jul 20, 2022 at 3:30 PM Roberto Sassu

<[email protected]>

Quoted 12 lines
Quoted 12 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Wednesday, July 20, 2022 5:57 PM
On Wed, Jul 20, 2022 at 1:02 AM Roberto Sassu

<[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Tuesday, July 19, 2022 10:40 PM
On Tue, Jul 19, 2022 at 12:40 PM Joe Burton

<[email protected]>

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines · wrote:

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF

maps.

Quoted 34 lines
Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
Quoted 11 lines
Quoted 10 lines
Quoted 3 lines

Without a method like this, users must manually make the syscall.

For context:
We've found this out while we were trying to add support for unpriv processes to open pinned r-x maps.
Maybe this deserves a test as well? Not sure.

Quoted 8 lines

Hi Stanislav, Joe

I noticed now this patch. I'm doing a broader work to add opts to bpf_*_get_fd_by_id(). I also adjusted permissions of bpftool depending on the operation type (e.g. show, dump: BPF_F_RDONLY).

Will send it soon (I'm trying to solve an issue with the CI, where libbfd is not available in the VM doing actual tests).

Is something like this patch included in your series as well? Can you use this new interface or do you need something different?

Quoted 7 lines
Quoted 3 lines

It is very similar. Except that I called it bpf_get_fd_opts, as it is shared with the bpf_*_get_fd_by_id() functions. The member name is just flags, plus an extra u32 for alignment.

We can bikeshed the naming, but we've been using existing conventions where opts fields match syscall fields, that seems like a sensible thing to do?

The only problem is that bpf_*_get_fd_by_id() functions would set the open_flags member of bpf_attr.

Quoted 7 lines
Quoted 2 lines

Flags would be good for both, even if not exact. Believe me, duplicating the opts would just create more confusion.

Wait, that's completely different, right? We are talking here about BPF_OBJ_GET (which has related BPF_OBJ_PIN).
Your GET_XXX_BY_ID are different so you'll still have to have another wrapper with opts?

Quoted 4 lines

Yes, they have different wrappers, just accept the same opts as obj_get(). From bpftool subcommands you want to set the correct permission, and propagate it uniformly to bpf_*_get_fd_by_id() or obj_get(). See map_parse_fds().

I don't think they are accepting the same opts.

For our case, we care about:

        struct { /* anonymous struct used by BPF_OBJ_* commands */
                __aligned_u64   pathname;
                __u32           bpf_fd;
                __u32           file_flags;
        };

For your case, you care about:

        struct { /* anonymous struct used by BPF_*_GET_*_ID */
                union {
                        __u32           start_id;
                        __u32           prog_id;
                        __u32           map_id;
                        __u32           btf_id;
                        __u32           link_id;
                };
                __u32           next_id;
                __u32           open_flags;
        };

So your new _opts libbpf routine should be independent of what Joe is doing here.

Quoted 98 lines

Roberto

Quoted 96 lines
Quoted 50 lines
Quoted 6 lines
Quoted 2 lines

It needs to be shared, as there are functions in bpftool calling both. Since the meaning of flags is the same, seems ok sharing.

So I guess there are no objections to the current patch? If it gets accepted, you should be able to drop some of your code and use this new bpf_obj_get_opts..

If you use a name good also for bpf_*_get_fd_by_id() and flags as structure member name, that would be ok.

Roberto

Quoted 38 lines
Quoted 10 lines

Roberto

Quoted 8 lines
Quoted 8 lines

Roberto

Quoted 6 lines
Quoted 6 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char

Quoted 5 lines
tools/lib/bpf/bpf.c +0 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char

*pathname)

Quoted 22 lines
Quoted 22 lines
Quoted 9 lines
Quoted 9 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char · line 586

Quoted 9 lines
tools/lib/bpf/bpf.c +6 -0
 }
 
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct

bpf_obj_get_opts

Quoted 12 lines
Quoted 12 lines

*opts)

Quoted 11 lines
tools/lib/bpf/bpf.c +4 -0
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int

Quoted 7 lines
tools/lib/bpf/bpf.c +0 -0
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int

fd,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

const

Quoted 6 lines
Quoted 6 lines

void *keys, const void *values

Quoted 5 lines
tools/lib/bpf/bpf.h +2 -0
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward

compatibility

Quoted 11 lines
Quoted 11 lines

*/

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
tools/lib/bpf/bpf.h +7 -0
+
+       __u32 file_flags;
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int · line 286

Quoted 2 lines
tools/lib/bpf/bpf.h +0 -0
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward

compatibility

Quoted 16 lines
Quoted 16 lines

*/

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#10 lore raw
Quoted 110 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 1:09 AM

Quoted 4 lines · Roberto Sassu
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:48 AM
On Wed, Jul 20, 2022 at 3:44 PM Roberto Sassu

<[email protected]>

Quoted 20 lines
Quoted 20 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:38 AM
On Wed, Jul 20, 2022 at 3:30 PM Roberto Sassu

<[email protected]>

Quoted 14 lines
Quoted 14 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Wednesday, July 20, 2022 5:57 PM
On Wed, Jul 20, 2022 at 1:02 AM Roberto Sassu

<[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Tuesday, July 19, 2022 10:40 PM
On Tue, Jul 19, 2022 at 12:40 PM Joe Burton

<[email protected]>

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line · wrote:
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF

maps.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Without a method like this, users must manually make the

syscall.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

For context:
We've found this out while we were trying to add support for

unpriv

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

processes to open pinned r-x maps.
Maybe this deserves a test as well? Not sure.

Hi Stanislav, Joe

I noticed now this patch. I'm doing a broader work to add opts to bpf_*_get_fd_by_id(). I also adjusted permissions of bpftool depending on the operation type (e.g. show, dump:

BPF_F_RDONLY).

Quoted 26 lines
Quoted 18 lines
Quoted 18 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

Will send it soon (I'm trying to solve an issue with the CI, where libbfd is not available in the VM doing actual tests).

Is something like this patch included in your series as well? Can you use this new interface or do you need something different?

Quoted 7 lines
Quoted 3 lines

It is very similar. Except that I called it bpf_get_fd_opts, as it is shared with the bpf_*_get_fd_by_id() functions. The member name is just flags, plus an extra u32 for alignment.

We can bikeshed the naming, but we've been using existing conventions where opts fields match syscall fields, that seems like a sensible thing to do?

The only problem is that bpf_*_get_fd_by_id() functions would set the open_flags member of bpf_attr.

Quoted 7 lines
Quoted 2 lines

Flags would be good for both, even if not exact. Believe me, duplicating the opts would just create more confusion.

Wait, that's completely different, right? We are talking here about BPF_OBJ_GET (which has related BPF_OBJ_PIN).
Your GET_XXX_BY_ID are different so you'll still have to have another wrapper with opts?

Quoted 4 lines

Yes, they have different wrappers, just accept the same opts as obj_get(). From bpftool subcommands you want to set the correct permission, and propagate it uniformly to bpf_*_get_fd_by_id() or obj_get(). See map_parse_fds().

I don't think they are accepting the same opts.

For our case, we care about:

        struct { /* anonymous struct used by BPF_OBJ_* commands */
                __aligned_u64   pathname;
                __u32           bpf_fd;
                __u32           file_flags;
        };

For your case, you care about:

        struct { /* anonymous struct used by BPF_*_GET_*_ID */
                union {
                        __u32           start_id;
                        __u32           prog_id;
                        __u32           map_id;
                        __u32           btf_id;
                        __u32           link_id;
                };
                __u32           next_id;
                __u32           open_flags;
        };
Quoted 2 lines

So your new _opts libbpf routine should be independent of what Joe is doing here.

It is. Just I use the same opts to set file_flags or open_flags.

Roberto

Quoted 99 lines
Quoted 52 lines

Roberto

Quoted 50 lines
Quoted 50 lines
Quoted 6 lines
Quoted 2 lines

It needs to be shared, as there are functions in bpftool calling both. Since the meaning of flags is the same, seems ok sharing.

So I guess there are no objections to the current patch? If it gets accepted, you should be able to drop some of your code and use this new bpf_obj_get_opts..

If you use a name good also for bpf_*_get_fd_by_id() and flags as structure member name, that would be ok.

Roberto

Quoted 38 lines
Quoted 10 lines

Roberto

Quoted 8 lines
Quoted 8 lines

Roberto

Quoted 6 lines
Quoted 6 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char

Quoted 5 lines
tools/lib/bpf/bpf.c +0 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char

*pathname)

Quoted 22 lines
Quoted 22 lines
Quoted 9 lines
Quoted 9 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char · line 586

Quoted 9 lines
tools/lib/bpf/bpf.c +6 -0
 }
 
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct

bpf_obj_get_opts

Quoted 12 lines
Quoted 12 lines

*opts)

Quoted 11 lines
tools/lib/bpf/bpf.c +4 -0
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

tools/lib/bpf/bpf.h LIBBPF_API int

Quoted 7 lines
tools/lib/bpf/bpf.c +0 -0
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int

bpf_map_update_batch(int

Quoted 39 lines
Quoted 39 lines

fd,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

const

Quoted 6 lines
Quoted 6 lines

void *keys, const void *values

Quoted 5 lines
tools/lib/bpf/bpf.h +2 -0
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward

compatibility

Quoted 11 lines
Quoted 11 lines

*/

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
tools/lib/bpf/bpf.h +7 -0
+
+       __u32 file_flags;
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

tools/lib/bpf/bpf.h LIBBPF_API int · line 286

Quoted 2 lines
tools/lib/bpf/bpf.h +0 -0
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward

compatibility

Quoted 16 lines
Quoted 16 lines

*/

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#11 lore raw
Quoted 111 lines · Roberto Sassu
Quoted 111 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 1:09 AM

Quoted 4 lines · Roberto Sassu
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:48 AM
On Wed, Jul 20, 2022 at 3:44 PM Roberto Sassu

<[email protected]>

Quoted 20 lines
Quoted 20 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:38 AM
On Wed, Jul 20, 2022 at 3:30 PM Roberto Sassu

<[email protected]>

Quoted 14 lines
Quoted 14 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Wednesday, July 20, 2022 5:57 PM
On Wed, Jul 20, 2022 at 1:02 AM Roberto Sassu

<[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Tuesday, July 19, 2022 10:40 PM
On Tue, Jul 19, 2022 at 12:40 PM Joe Burton

<[email protected]>

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line · wrote:
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF

maps.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Without a method like this, users must manually make the

syscall.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

For context:
We've found this out while we were trying to add support for

unpriv

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

processes to open pinned r-x maps.
Maybe this deserves a test as well? Not sure.

Hi Stanislav, Joe

I noticed now this patch. I'm doing a broader work to add opts to bpf_*_get_fd_by_id(). I also adjusted permissions of bpftool depending on the operation type (e.g. show, dump:

BPF_F_RDONLY).

Quoted 26 lines
Quoted 18 lines
Quoted 18 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

Will send it soon (I'm trying to solve an issue with the CI, where libbfd is not available in the VM doing actual tests).

Is something like this patch included in your series as well? Can you use this new interface or do you need something different?

Quoted 7 lines
Quoted 3 lines

It is very similar. Except that I called it bpf_get_fd_opts, as it is shared with the bpf_*_get_fd_by_id() functions. The member name is just flags, plus an extra u32 for alignment.

We can bikeshed the naming, but we've been using existing conventions where opts fields match syscall fields, that seems like a sensible thing to do?

The only problem is that bpf_*_get_fd_by_id() functions would set the open_flags member of bpf_attr.

Quoted 7 lines
Quoted 2 lines

Flags would be good for both, even if not exact. Believe me, duplicating the opts would just create more confusion.

Wait, that's completely different, right? We are talking here about BPF_OBJ_GET (which has related BPF_OBJ_PIN).
Your GET_XXX_BY_ID are different so you'll still have to have another wrapper with opts?

Quoted 4 lines

Yes, they have different wrappers, just accept the same opts as obj_get(). From bpftool subcommands you want to set the correct permission, and propagate it uniformly to bpf_*_get_fd_by_id() or obj_get(). See map_parse_fds().

I don't think they are accepting the same opts.

For our case, we care about:

        struct { /* anonymous struct used by BPF_OBJ_* commands */
                __aligned_u64   pathname;
                __u32           bpf_fd;
                __u32           file_flags;
        };

For your case, you care about:

        struct { /* anonymous struct used by BPF_*_GET_*_ID */
                union {
                        __u32           start_id;
                        __u32           prog_id;
                        __u32           map_id;
                        __u32           btf_id;
                        __u32           link_id;
                };
                __u32           next_id;
                __u32           open_flags;
        };
Quoted 4 lines
Quoted 2 lines

So your new _opts libbpf routine should be independent of what Joe is doing here.

It is. Just I use the same opts to set file_flags or open_flags.

That seems confusing. Let's have separate calls for separate syscall commands as we do already?

Quoted 101 lines

Roberto

Quoted 99 lines
Quoted 52 lines

Roberto

Quoted 50 lines
Quoted 50 lines
Quoted 6 lines
Quoted 2 lines

It needs to be shared, as there are functions in bpftool calling both. Since the meaning of flags is the same, seems ok sharing.

So I guess there are no objections to the current patch? If it gets accepted, you should be able to drop some of your code and use this new bpf_obj_get_opts..

If you use a name good also for bpf_*_get_fd_by_id() and flags as structure member name, that would be ok.

Roberto

Quoted 38 lines
Quoted 10 lines

Roberto

Quoted 8 lines
Quoted 8 lines

Roberto

Quoted 6 lines
Quoted 6 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char

Quoted 5 lines
tools/lib/bpf/bpf.c +0 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char

*pathname)

Quoted 22 lines
Quoted 22 lines
Quoted 9 lines
Quoted 9 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char · line 586

Quoted 9 lines
tools/lib/bpf/bpf.c +6 -0
 }
 
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct

bpf_obj_get_opts

Quoted 12 lines
Quoted 12 lines

*opts)

Quoted 11 lines
tools/lib/bpf/bpf.c +4 -0
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

tools/lib/bpf/bpf.h LIBBPF_API int

Quoted 7 lines
tools/lib/bpf/bpf.c +0 -0
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int

bpf_map_update_batch(int

Quoted 39 lines
Quoted 39 lines

fd,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

const

Quoted 6 lines
Quoted 6 lines

void *keys, const void *values

Quoted 5 lines
tools/lib/bpf/bpf.h +2 -0
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward

compatibility

Quoted 11 lines
Quoted 11 lines

*/

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
tools/lib/bpf/bpf.h +7 -0
+
+       __u32 file_flags;
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

tools/lib/bpf/bpf.h LIBBPF_API int · line 286

Quoted 2 lines
tools/lib/bpf/bpf.h +0 -0
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward

compatibility

Quoted 16 lines
Quoted 16 lines

*/

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#12 lore raw
Quoted 120 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 1:15 AM

Quoted 4 lines · Roberto Sassu
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 1:09 AM
On Wed, Jul 20, 2022 at 4:02 PM Roberto Sassu

<[email protected]>

Quoted 23 lines
Quoted 23 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:48 AM
On Wed, Jul 20, 2022 at 3:44 PM Roberto Sassu

<[email protected]>

Quoted 17 lines
Quoted 17 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:38 AM
On Wed, Jul 20, 2022 at 3:30 PM Roberto Sassu

<[email protected]>

Quoted 11 lines
Quoted 11 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Wednesday, July 20, 2022 5:57 PM
On Wed, Jul 20, 2022 at 1:02 AM Roberto Sassu

<[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Tuesday, July 19, 2022 10:40 PM
On Tue, Jul 19, 2022 at 12:40 PM Joe Burton

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

<[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines · wrote:

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting

the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

file_flags parameter.

This parameter is needed to enable unprivileged access to

BPF

Quoted 19 lines
Quoted 19 lines
Quoted 2 lines
Quoted 2 lines

maps.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Without a method like this, users must manually make the

syscall.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

For context:
We've found this out while we were trying to add support for

unpriv

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

processes to open pinned r-x maps.
Maybe this deserves a test as well? Not sure.

Hi Stanislav, Joe

I noticed now this patch. I'm doing a broader work to add opts to bpf_*_get_fd_by_id(). I also adjusted permissions of bpftool depending on the operation type (e.g. show, dump:

Quoted 6 lines
Quoted 6 lines

BPF_F_RDONLY).

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

Will send it soon (I'm trying to solve an issue with the CI, where libbfd is not available in the VM doing actual tests).

Is something like this patch included in your series as well? Can

you

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

use this new interface or do you need something different?

It is very similar. Except that I called it bpf_get_fd_opts, as it is shared with the bpf_*_get_fd_by_id() functions. The member name is just flags, plus an extra u32 for alignment.

We can bikeshed the naming, but we've been using existing

conventions

Quoted 43 lines
Quoted 43 lines
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

where opts fields match syscall fields, that seems like a sensible thing to do?

The only problem is that bpf_*_get_fd_by_id() functions would set the open_flags member of bpf_attr.

Quoted 7 lines
Quoted 2 lines

Flags would be good for both, even if not exact. Believe me, duplicating the opts would just create more confusion.

Wait, that's completely different, right? We are talking here about BPF_OBJ_GET (which has related BPF_OBJ_PIN).
Your GET_XXX_BY_ID are different so you'll still have to have another wrapper with opts?

Quoted 4 lines

Yes, they have different wrappers, just accept the same opts as obj_get(). From bpftool subcommands you want to set the correct permission, and propagate it uniformly to bpf_*_get_fd_by_id() or obj_get(). See map_parse_fds().

I don't think they are accepting the same opts.

For our case, we care about:

        struct { /* anonymous struct used by BPF_OBJ_* commands */
                __aligned_u64   pathname;
                __u32           bpf_fd;
                __u32           file_flags;
        };

For your case, you care about:

        struct { /* anonymous struct used by BPF_*_GET_*_ID */
                union {
                        __u32           start_id;
                        __u32           prog_id;
                        __u32           map_id;
                        __u32           btf_id;
                        __u32           link_id;
                };
                __u32           next_id;
                __u32           open_flags;
        };
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

So your new _opts libbpf routine should be independent of what Joe is doing here.

It is. Just I use the same opts to set file_flags or open_flags.

That seems confusing. Let's have separate calls for separate syscall commands as we do already?

Can you wait one day, I send what I have, so that we see everything together?

Thanks

Roberto

Quoted 102 lines
Quoted 82 lines

Roberto

Quoted 80 lines
Quoted 52 lines

Roberto

Quoted 50 lines
Quoted 50 lines
Quoted 6 lines
Quoted 2 lines

It needs to be shared, as there are functions in bpftool calling both. Since the meaning of flags is the same, seems ok sharing.

So I guess there are no objections to the current patch? If it gets accepted, you should be able to drop some of your code and use this new bpf_obj_get_opts..

If you use a name good also for bpf_*_get_fd_by_id() and flags as structure member name, that would be ok.

Roberto

Quoted 38 lines
Quoted 10 lines

Roberto

Quoted 8 lines
Quoted 8 lines

Roberto

Quoted 6 lines
Quoted 6 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char

Quoted 5 lines
tools/lib/bpf/bpf.c +0 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char

*pathname)

Quoted 22 lines
Quoted 22 lines
Quoted 9 lines
Quoted 9 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char · line 586

Quoted 9 lines
tools/lib/bpf/bpf.c +6 -0
 }
 
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct

bpf_obj_get_opts

Quoted 12 lines
Quoted 12 lines

*opts)

Quoted 11 lines
tools/lib/bpf/bpf.c +4 -0
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

tools/lib/bpf/bpf.h LIBBPF_API int

Quoted 7 lines
tools/lib/bpf/bpf.c +0 -0
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int

bpf_map_update_batch(int

Quoted 20 lines
Quoted 20 lines

fd,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

const

Quoted 6 lines
Quoted 6 lines

void *keys, const void *values

Quoted 5 lines
tools/lib/bpf/bpf.h +2 -0
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward

compatibility

Quoted 11 lines
Quoted 11 lines

*/

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
tools/lib/bpf/bpf.h +7 -0
+
+       __u32 file_flags;
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

tools/lib/bpf/bpf.h LIBBPF_API int · line 286

Quoted 2 lines
tools/lib/bpf/bpf.h +0 -0
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward

compatibility

Quoted 2 lines
Quoted 2 lines

*/

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/tools/lib/bpf/libbpf.map

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

index 0625adb9e888..119e6e1ea7f1 100644

tools/lib/bpf/libbpf.map +1 -1
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#13 lore raw
Quoted 124 lines · Roberto Sassu
Quoted 124 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 1:15 AM

Quoted 4 lines · Roberto Sassu
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 1:09 AM
On Wed, Jul 20, 2022 at 4:02 PM Roberto Sassu

<[email protected]>

Quoted 23 lines
Quoted 23 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:48 AM
On Wed, Jul 20, 2022 at 3:44 PM Roberto Sassu

<[email protected]>

Quoted 17 lines
Quoted 17 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Thursday, July 21, 2022 12:38 AM
On Wed, Jul 20, 2022 at 3:30 PM Roberto Sassu

<[email protected]>

Quoted 11 lines
Quoted 11 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Wednesday, July 20, 2022 5:57 PM
On Wed, Jul 20, 2022 at 1:02 AM Roberto Sassu

<[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines · wrote:
Quoted 4 lines

From: Stanislav Fomichev [mailto:[email protected]]
Sent: Tuesday, July 19, 2022 10:40 PM
On Tue, Jul 19, 2022 at 12:40 PM Joe Burton

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

<[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines · wrote:

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting

the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

`file_flags` parameter.

This parameter is needed to enable unprivileged access to

BPF

Quoted 19 lines
Quoted 19 lines
Quoted 2 lines
Quoted 2 lines

maps.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Without a method like this, users must manually make the

syscall.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

Signed-off-by: Joe Burton <[email protected]>

For context:
We've found this out while we were trying to add support for

unpriv

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

processes to open pinned r-x maps.
Maybe this deserves a test as well? Not sure.

Hi Stanislav, Joe

I noticed now this patch. I'm doing a broader work to add opts to bpf_*_get_fd_by_id(). I also adjusted permissions of bpftool depending on the operation type (e.g. show, dump:

Quoted 6 lines
Quoted 6 lines

BPF_F_RDONLY).

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

Will send it soon (I'm trying to solve an issue with the CI, where libbfd is not available in the VM doing actual tests).

Is something like this patch included in your series as well? Can

you

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

use this new interface or do you need something different?

It is very similar. Except that I called it bpf_get_fd_opts, as it is shared with the bpf_*_get_fd_by_id() functions. The member name is just flags, plus an extra u32 for alignment.

We can bikeshed the naming, but we've been using existing

conventions

Quoted 46 lines
Quoted 45 lines
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

where opts fields match syscall fields, that seems like a sensible thing to do?

The only problem is that bpf_*_get_fd_by_id() functions would set the open_flags member of bpf_attr.

Quoted 7 lines
Quoted 2 lines

Flags would be good for both, even if not exact. Believe me, duplicating the opts would just create more confusion.

Wait, that's completely different, right? We are talking here about BPF_OBJ_GET (which has related BPF_OBJ_PIN).
Your GET_XXX_BY_ID are different so you'll still have to have another wrapper with opts?

Quoted 4 lines

Yes, they have different wrappers, just accept the same opts as obj_get(). From bpftool subcommands you want to set the correct permission, and propagate it uniformly to bpf_*_get_fd_by_id() or obj_get(). See map_parse_fds().

I don't think they are accepting the same opts.

For our case, we care about:

        struct { /* anonymous struct used by BPF_OBJ_* commands */
                __aligned_u64   pathname;
                __u32           bpf_fd;
                __u32           file_flags;
        };

For your case, you care about:

        struct { /* anonymous struct used by BPF_*_GET_*_ID */
                union {
                        __u32           start_id;
                        __u32           prog_id;
                        __u32           map_id;
                        __u32           btf_id;
                        __u32           link_id;
                };
                __u32           next_id;
                __u32           open_flags;
        };

So your new _opts libbpf routine should be independent of what Joe is doing here.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

It is. Just I use the same opts to set file_flags or open_flags.

That seems confusing. Let's have separate calls for separate syscall commands as we do already?

Can you wait one day, I send what I have, so that we see everything together?

Sure, CC us both on the patches.

#14 lore raw
In reply to Joe Burton
Quoted 9 lines · Joe Burton

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF maps. Without a method like this, users must manually make the syscall.

Quoted 7 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)

I agree that bpf_obj_get_opts should be separate from bpf_get_fd_opts. Just because both currently have file_flags in them doesn't mean that they should/will always stay in sync. So two separate opts for two separate APIs makes sense to me.

So I'd accept this patch, but please see a few small things below and send v3. Thanks!

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char *pathname) · line 582

Quoted 10 lines
tools/lib/bpf/bpf.c +2 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char *pathname)
 }
 
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);

if you were doing it this way, here should be an empty line. But really you can/should just pass NULL instead of opts in this case.

Quoted 22 lines
tools/lib/bpf/bpf.c +8 -0
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values · line 276

Quoted 7 lines
tools/lib/bpf/bpf.h +4 -0
@@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward compatibility */
+
+       __u32 file_flags;

please add size_t :0; to avoid non-zero-initialized padding (we do it in a lot of other opts structs)

Quoted 25 lines
tools/lib/bpf/bpf.h +5 -0
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward compatibility */
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#15 lore raw
Quoted 21 lines

From: Andrii Nakryiko [mailto:[email protected]]
Sent: Thursday, July 28, 2022 1:03 AM

Quoted 9 lines · Joe Burton

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF maps. Without a method like this, users must manually make the syscall.

Quoted 7 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)
Quoted 7 lines

I agree that bpf_obj_get_opts should be separate from bpf_get_fd_opts. Just because both currently have file_flags in them doesn't mean that they should/will always stay in sync. So two separate opts for two separate APIs makes sense to me.

So I'd accept this patch, but please see a few small things below and send v3. Thanks!

Should map_parse_fds() accept two opts, or just the flags to be set on locally-defined variables?

Thanks

Roberto

Quoted 75 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char *pathname) · line 582

Quoted 10 lines
tools/lib/bpf/bpf.c +2 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char *pathname)
 }
 
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);

if you were doing it this way, here should be an empty line. But really you can/should just pass NULL instead of opts in this case.

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char *pathname)

Quoted 4 lines
tools/lib/bpf/bpf.c +4 -0
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts

*opts)

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd, const

Quoted 18 lines
tools/lib/bpf/bpf.c +4 -0
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int fd, const

void *keys, const void *values

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values · line 276

Quoted 7 lines
tools/lib/bpf/bpf.h +4 -0
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward compatibility */
+
+       __u32 file_flags;

please add size_t :0; to avoid non-zero-initialized padding (we do it in a lot of other opts structs)

Quoted 25 lines
tools/lib/bpf/bpf.h +5 -0
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward compatibility */
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

#16 lore raw
Quoted 27 lines · Roberto Sassu
Quoted 27 lines

From: Andrii Nakryiko [mailto:[email protected]]
Sent: Thursday, July 28, 2022 1:03 AM

Quoted 9 lines · Joe Burton

From: Joe Burton <[email protected]>

Add an extensible variant of bpf_obj_get() capable of setting the file_flags parameter.

This parameter is needed to enable unprivileged access to BPF maps. Without a method like this, users must manually make the syscall.

Quoted 7 lines
---
 tools/lib/bpf/bpf.c      | 10 ++++++++++
 tools/lib/bpf/bpf.h      |  9 +++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 20 insertions(+)

I agree that bpf_obj_get_opts should be separate from bpf_get_fd_opts. Just because both currently have file_flags in them doesn't mean that they should/will always stay in sync. So two separate opts for two separate APIs makes sense to me.

Quoted 5 lines
Quoted 2 lines

So I'd accept this patch, but please see a few small things below and send v3. Thanks!

Should map_parse_fds() accept two opts, or just the flags to be set on locally-defined variables?

it's because map_parse_fds() is used with both get_fd_by_id() and bpf_obj_get(), right? I'd pass flags and construct correct set of options internally, based on which BPF command you need to use to get FD

Quoted 80 lines

Thanks

Roberto

Quoted 75 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char *pathname) · line 582

Quoted 10 lines
tools/lib/bpf/bpf.c +2 -0
index 5eb0df90eb2b..5acb0e8bd13c 100644
@@ -578,12 +578,22 @@ int bpf_obj_pin(int fd, const char *pathname)
 }
 
 int bpf_obj_get(const char *pathname)
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);

if you were doing it this way, here should be an empty line. But really you can/should just pass NULL instead of opts in this case.

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char *pathname)

Quoted 4 lines
tools/lib/bpf/bpf.c +4 -0
+       return bpf_obj_get_opts(pathname, &opts);
+}
+
+int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts

*opts)

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd, const

Quoted 18 lines
tools/lib/bpf/bpf.c +4 -0
 {
        union bpf_attr attr;
        int fd;
 
+       if (!OPTS_VALID(opts, bpf_obj_get_opts))
+               return libbpf_err(-EINVAL);
+
        memset(&attr, 0, sizeof(attr));
        attr.pathname = ptr_to_u64((void *)pathname);
+       attr.file_flags = OPTS_GET(opts, file_flags, 0);
 
        fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
        return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +0 -0
index 88a7cc4bd76f..f31b493b5f9a 100644
@@ -270,8 +270,17 @@ LIBBPF_API int bpf_map_update_batch(int fd, const

void *keys, const void *values

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values · line 276

Quoted 7 lines
tools/lib/bpf/bpf.h +4 -0
                                    __u32 *count,
                                    const struct bpf_map_batch_opts *opts);
 
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward compatibility */
+
+       __u32 file_flags;

please add size_t :0; to avoid non-zero-initialized padding (we do it in a lot of other opts structs)

Quoted 25 lines
tools/lib/bpf/bpf.h +5 -0
+};
+#define bpf_obj_get_opts__last_field file_flags
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
+LIBBPF_API int bpf_obj_get_opts(const char *pathname,
+                               const struct bpf_obj_get_opts *opts);
 
 struct bpf_prog_attach_opts {
        size_t sz; /* size of this struct for forward/backward compatibility */
tools/lib/bpf/libbpf.map +1 -1
index 0625adb9e888..119e6e1ea7f1 100644
@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               bpf_obj_get_opts;
                bpf_prog_query_opts;
                bpf_program__attach_ksyscall;
                btf__add_enum64;
--

2.37.0.170.g444d1eabd0-goog

Applied
#17 lore raw
In reply to Andrii Nakryiko

On Wed, Jul 27, 2022 at 04:02:43PM -0700, Andrii Nakryiko wrote:

Applied both of these in v3, which I'll send out in a moment. Thanks for the feedback!

Quoted 7 lines

tools/lib/bpf/bpf.c int bpf_obj_pin(int fd, const char *pathname) · line 582

Quoted 3 lines

int bpf_obj_get(const char *pathname)

tools/lib/bpf/bpf.c +2 -0
+{
+       LIBBPF_OPTS(bpf_obj_get_opts, opts);

if you were doing it this way, here should be an empty line. But really you can/should just pass NULL instead of opts in this case.

Quoted 8 lines

tools/lib/bpf/bpf.h LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values · line 276

Quoted 4 lines
tools/lib/bpf/bpf.h +4 -0
+struct bpf_obj_get_opts {
+       size_t sz; /* size of this struct for forward/backward compatibility */
+
+       __u32 file_flags;

please add size_t :0; to avoid non-zero-initialized padding (we do it in a lot of other opts structs)

TIL about this trick. Very clever.