* [PATCH] [PATCH bpf-next] libbpf: Add bpf_obj_get_opts()
@ 2022-07-18 21:46 Joe Burton
2022-07-18 21:50 ` Stanislav Fomichev
0 siblings, 1 reply; 3+ messages in thread
From: Joe Burton @ 2022-07-18 21:46 UTC (permalink / raw)
To: Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann,
Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf,
linux-kernel
Cc: Joe Burton
From: Joe Burton <jevburton@google.com>
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.
Signed-off-by: Joe Burton <jevburton@google.com>
---
tools/lib/bpf/bpf.c | 10 ++++++++++
tools/lib/bpf/bpf.h | 9 +++++++++
2 files changed, 19 insertions(+)
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 5eb0df90eb2b..5acb0e8bd13c 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -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);
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 88a7cc4bd76f..f31b493b5f9a 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -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 */
--
2.37.0.170.g444d1eabd0-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] [PATCH bpf-next] libbpf: Add bpf_obj_get_opts()
2022-07-18 21:46 [PATCH] [PATCH bpf-next] libbpf: Add bpf_obj_get_opts() Joe Burton
@ 2022-07-18 21:50 ` Stanislav Fomichev
2022-07-19 19:31 ` Joe Burton
0 siblings, 1 reply; 3+ messages in thread
From: Stanislav Fomichev @ 2022-07-18 21:50 UTC (permalink / raw)
To: Joe Burton
Cc: Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann,
Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
KP Singh, Hao Luo, Jiri Olsa, bpf, linux-kernel, Joe Burton
On Mon, Jul 18, 2022 at 2:46 PM Joe Burton <jevburton.kernel@gmail.com> wrote:
>
> From: Joe Burton <jevburton@google.com>
>
> 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.
>
> Signed-off-by: Joe Burton <jevburton@google.com>
> ---
> tools/lib/bpf/bpf.c | 10 ++++++++++
> tools/lib/bpf/bpf.h | 9 +++++++++
Needs a libbpf.map change as well?
> 2 files changed, 19 insertions(+)
>
> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> index 5eb0df90eb2b..5acb0e8bd13c 100644
> --- a/tools/lib/bpf/bpf.c
> +++ b/tools/lib/bpf/bpf.c
> @@ -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);
> diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
> index 88a7cc4bd76f..f31b493b5f9a 100644
> --- a/tools/lib/bpf/bpf.h
> +++ b/tools/lib/bpf/bpf.h
> @@ -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 */
> --
> 2.37.0.170.g444d1eabd0-goog
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] [PATCH bpf-next] libbpf: Add bpf_obj_get_opts()
2022-07-18 21:50 ` Stanislav Fomichev
@ 2022-07-19 19:31 ` Joe Burton
0 siblings, 0 replies; 3+ messages in thread
From: Joe Burton @ 2022-07-19 19:31 UTC (permalink / raw)
To: Stanislav Fomichev
Cc: Joe Burton, Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann,
Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
KP Singh, Hao Luo, Jiri Olsa, bpf, linux-kernel
On Mon, Jul 18, 2022 at 02:50:26PM -0700, Stanislav Fomichev wrote:
> Needs a libbpf.map change as well?
Good catch. Done in v2. Sending the patch out in a moment.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-07-19 19:31 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-18 21:46 [PATCH] [PATCH bpf-next] libbpf: Add bpf_obj_get_opts() Joe Burton
2022-07-18 21:50 ` Stanislav Fomichev
2022-07-19 19:31 ` Joe Burton
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.