linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id()
@ 2022-10-06 11:07 Roberto Sassu
  2022-10-06 11:07 ` [PATCH v2 1/6] libbpf: Fix LIBBPF_1.0.0 declaration in libbpf.map Roberto Sassu
                   ` (7 more replies)
  0 siblings, 8 replies; 10+ messages in thread
From: Roberto Sassu @ 2022-10-06 11:07 UTC (permalink / raw)
  To: ast, daniel, andrii, martin.lau, song, yhs, john.fastabend,
	kpsingh, sdf, haoluo, jolsa, mykolal, shuah
  Cc: bpf, linux-kselftest, linux-kernel, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

Add the _opts variant for bpf_*_get_fd_by_id() functions, to be able to
pass to the kernel more options, when requesting a fd of an eBPF object.

Pass the options through a newly introduced structure,
bpf_get_fd_by_id_opts, which currently contains open_flags (the other two
members are for compatibility and for padding).

open_flags allows the caller to request specific permissions to access a
map (e.g. read-only). This is useful for example in the situation where a
map is write-protected.

Besides patches 2-6, which introduce the new variants and the data
structure, patch 1 fixes the LIBBPF_1.0.0 declaration in libbpf.map.

Changelog

v1:
 - Don't CC stable kernel mailing list for patch 1 (suggested by Andrii)
 - Rename bpf_get_fd_opts struct to bpf_get_fd_by_id_opts (suggested by
   Andrii)
 - Move declaration of _opts variants after non-opts variants (suggested by
   Andrii)
 - Correctly initialize bpf_map_info, fix style issues, use map from
   skeleton, check valid fd in the test (suggested by Andrii)
 - Rename libbpf_get_fd_opts test to libbpf_get_fd_by_id_opts

Roberto Sassu (6):
  libbpf: Fix LIBBPF_1.0.0 declaration in libbpf.map
  libbpf: Introduce bpf_get_fd_by_id_opts and
    bpf_map_get_fd_by_id_opts()
  libbpf: Introduce bpf_prog_get_fd_by_id_opts()
  libbpf: Introduce bpf_btf_get_fd_by_id_opts()
  libbpf: Introduce bpf_link_get_fd_by_id_opts()
  selftests/bpf: Add tests for _opts variants of bpf_*_get_fd_by_id()

 tools/lib/bpf/bpf.c                           | 48 +++++++++-
 tools/lib/bpf/bpf.h                           | 16 ++++
 tools/lib/bpf/libbpf.map                      |  6 +-
 tools/testing/selftests/bpf/DENYLIST.s390x    |  1 +
 .../bpf/prog_tests/libbpf_get_fd_by_id_opts.c | 87 +++++++++++++++++++
 .../bpf/progs/test_libbpf_get_fd_by_id_opts.c | 36 ++++++++
 6 files changed, 189 insertions(+), 5 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_libbpf_get_fd_by_id_opts.c

-- 
2.25.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v2 1/6] libbpf: Fix LIBBPF_1.0.0 declaration in libbpf.map
  2022-10-06 11:07 [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id() Roberto Sassu
@ 2022-10-06 11:07 ` Roberto Sassu
  2022-10-06 11:07 ` [PATCH v2 2/6] libbpf: Introduce bpf_get_fd_by_id_opts and bpf_map_get_fd_by_id_opts() Roberto Sassu
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Roberto Sassu @ 2022-10-06 11:07 UTC (permalink / raw)
  To: ast, daniel, andrii, martin.lau, song, yhs, john.fastabend,
	kpsingh, sdf, haoluo, jolsa, mykolal, shuah
  Cc: bpf, linux-kselftest, linux-kernel, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

Add the missing LIBBPF_0.8.0 at the end of the LIBBPF_1.0.0 declaration,
similarly to other version declarations.

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 tools/lib/bpf/libbpf.map | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index c1d6aa7c82b6..04fab9f1fdd7 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -367,7 +367,7 @@ LIBBPF_1.0.0 {
 		libbpf_bpf_map_type_str;
 		libbpf_bpf_prog_type_str;
 		perf_buffer__buffer;
-};
+} LIBBPF_0.8.0;
 
 LIBBPF_1.1.0 {
 	global:
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 2/6] libbpf: Introduce bpf_get_fd_by_id_opts and bpf_map_get_fd_by_id_opts()
  2022-10-06 11:07 [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id() Roberto Sassu
  2022-10-06 11:07 ` [PATCH v2 1/6] libbpf: Fix LIBBPF_1.0.0 declaration in libbpf.map Roberto Sassu
@ 2022-10-06 11:07 ` Roberto Sassu
  2022-10-06 11:07 ` [PATCH v2 3/6] libbpf: Introduce bpf_prog_get_fd_by_id_opts() Roberto Sassu
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Roberto Sassu @ 2022-10-06 11:07 UTC (permalink / raw)
  To: ast, daniel, andrii, martin.lau, song, yhs, john.fastabend,
	kpsingh, sdf, haoluo, jolsa, mykolal, shuah
  Cc: bpf, linux-kselftest, linux-kernel, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

Define a new data structure called bpf_get_fd_by_id_opts, with the member
open_flags, to be used by callers of the _opts variants of
bpf_*_get_fd_by_id() to specify the permissions needed for the file
descriptor to be obtained.

Also, introduce bpf_map_get_fd_by_id_opts(), to let the caller pass a
bpf_get_fd_by_id_opts structure.

Finally, keep the existing bpf_map_get_fd_by_id(), and call
bpf_map_get_fd_by_id_opts() with NULL as opts argument, to request
read-write permissions (current behavior).

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 tools/lib/bpf/bpf.c      | 12 +++++++++++-
 tools/lib/bpf/bpf.h      | 10 ++++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 1d49a0352836..c08d7509553d 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -948,19 +948,29 @@ int bpf_prog_get_fd_by_id(__u32 id)
 	return libbpf_err_errno(fd);
 }
 
-int bpf_map_get_fd_by_id(__u32 id)
+int bpf_map_get_fd_by_id_opts(__u32 id,
+			      const struct bpf_get_fd_by_id_opts *opts)
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
 	union bpf_attr attr;
 	int fd;
 
+	if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
+		return libbpf_err(-EINVAL);
+
 	memset(&attr, 0, attr_sz);
 	attr.map_id = id;
+	attr.open_flags = OPTS_GET(opts, open_flags, 0);
 
 	fd = sys_bpf_fd(BPF_MAP_GET_FD_BY_ID, &attr, attr_sz);
 	return libbpf_err_errno(fd);
 }
 
+int bpf_map_get_fd_by_id(__u32 id)
+{
+	return bpf_map_get_fd_by_id_opts(id, NULL);
+}
+
 int bpf_btf_get_fd_by_id(__u32 id)
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 9c50beabdd14..10ce38f0a9ef 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -365,8 +365,18 @@ LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
 LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
 LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
 LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
+
+struct bpf_get_fd_by_id_opts {
+	size_t sz; /* size of this struct for forward/backward compatibility */
+	__u32 open_flags; /* permissions requested for the operation on fd */
+	size_t :0;
+};
+#define bpf_get_fd_by_id_opts__last_field open_flags
+
 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
 LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
+LIBBPF_API int bpf_map_get_fd_by_id_opts(__u32 id,
+				const struct bpf_get_fd_by_id_opts *opts);
 LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
 LIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
 LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index 04fab9f1fdd7..2e665b21d84f 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -371,6 +371,7 @@ LIBBPF_1.0.0 {
 
 LIBBPF_1.1.0 {
 	global:
+		bpf_map_get_fd_by_id_opts;
 		user_ring_buffer__discard;
 		user_ring_buffer__free;
 		user_ring_buffer__new;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 3/6] libbpf: Introduce bpf_prog_get_fd_by_id_opts()
  2022-10-06 11:07 [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id() Roberto Sassu
  2022-10-06 11:07 ` [PATCH v2 1/6] libbpf: Fix LIBBPF_1.0.0 declaration in libbpf.map Roberto Sassu
  2022-10-06 11:07 ` [PATCH v2 2/6] libbpf: Introduce bpf_get_fd_by_id_opts and bpf_map_get_fd_by_id_opts() Roberto Sassu
@ 2022-10-06 11:07 ` Roberto Sassu
  2022-10-06 11:07 ` [PATCH v2 4/6] libbpf: Introduce bpf_btf_get_fd_by_id_opts() Roberto Sassu
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Roberto Sassu @ 2022-10-06 11:07 UTC (permalink / raw)
  To: ast, daniel, andrii, martin.lau, song, yhs, john.fastabend,
	kpsingh, sdf, haoluo, jolsa, mykolal, shuah
  Cc: bpf, linux-kselftest, linux-kernel, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

Introduce bpf_prog_get_fd_by_id_opts(), for symmetry with
bpf_map_get_fd_by_id_opts(), to let the caller pass the newly introduced
data structure bpf_get_fd_by_id_opts. Keep the existing
bpf_prog_get_fd_by_id(), and call bpf_prog_get_fd_by_id_opts() with NULL as
opts argument, to prevent setting open_flags.

Currently, the kernel does not support non-zero open_flags for
bpf_prog_get_fd_by_id_opts(), and a call with them will result in an error
returned by the bpf() system call. The caller should always pass zero
open_flags.

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 tools/lib/bpf/bpf.c      | 12 +++++++++++-
 tools/lib/bpf/bpf.h      |  2 ++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index c08d7509553d..03be8c96bbac 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -935,19 +935,29 @@ int bpf_link_get_next_id(__u32 start_id, __u32 *next_id)
 	return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID);
 }
 
-int bpf_prog_get_fd_by_id(__u32 id)
+int bpf_prog_get_fd_by_id_opts(__u32 id,
+			       const struct bpf_get_fd_by_id_opts *opts)
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
 	union bpf_attr attr;
 	int fd;
 
+	if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
+		return libbpf_err(-EINVAL);
+
 	memset(&attr, 0, attr_sz);
 	attr.prog_id = id;
+	attr.open_flags = OPTS_GET(opts, open_flags, 0);
 
 	fd = sys_bpf_fd(BPF_PROG_GET_FD_BY_ID, &attr, attr_sz);
 	return libbpf_err_errno(fd);
 }
 
+int bpf_prog_get_fd_by_id(__u32 id)
+{
+	return bpf_prog_get_fd_by_id_opts(id, NULL);
+}
+
 int bpf_map_get_fd_by_id_opts(__u32 id,
 			      const struct bpf_get_fd_by_id_opts *opts)
 {
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 10ce38f0a9ef..4558bafbce14 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -374,6 +374,8 @@ struct bpf_get_fd_by_id_opts {
 #define bpf_get_fd_by_id_opts__last_field open_flags
 
 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
+LIBBPF_API int bpf_prog_get_fd_by_id_opts(__u32 id,
+				const struct bpf_get_fd_by_id_opts *opts);
 LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
 LIBBPF_API int bpf_map_get_fd_by_id_opts(__u32 id,
 				const struct bpf_get_fd_by_id_opts *opts);
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index 2e665b21d84f..c3604eaa220d 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -372,6 +372,7 @@ LIBBPF_1.0.0 {
 LIBBPF_1.1.0 {
 	global:
 		bpf_map_get_fd_by_id_opts;
+		bpf_prog_get_fd_by_id_opts;
 		user_ring_buffer__discard;
 		user_ring_buffer__free;
 		user_ring_buffer__new;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 4/6] libbpf: Introduce bpf_btf_get_fd_by_id_opts()
  2022-10-06 11:07 [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id() Roberto Sassu
                   ` (2 preceding siblings ...)
  2022-10-06 11:07 ` [PATCH v2 3/6] libbpf: Introduce bpf_prog_get_fd_by_id_opts() Roberto Sassu
@ 2022-10-06 11:07 ` Roberto Sassu
  2022-10-06 11:07 ` [PATCH v2 5/6] libbpf: Introduce bpf_link_get_fd_by_id_opts() Roberto Sassu
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Roberto Sassu @ 2022-10-06 11:07 UTC (permalink / raw)
  To: ast, daniel, andrii, martin.lau, song, yhs, john.fastabend,
	kpsingh, sdf, haoluo, jolsa, mykolal, shuah
  Cc: bpf, linux-kselftest, linux-kernel, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

Introduce bpf_btf_get_fd_by_id_opts(), for symmetry with
bpf_map_get_fd_by_id_opts(), to let the caller pass the newly introduced
data structure bpf_get_fd_by_id_opts. Keep the existing
bpf_btf_get_fd_by_id(), and call bpf_btf_get_fd_by_id_opts() with NULL as
opts argument, to prevent setting open_flags.

Currently, the kernel does not support non-zero open_flags for
bpf_btf_get_fd_by_id_opts(), and a call with them will result in an error
returned by the bpf() system call. The caller should always pass zero
open_flags.

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 tools/lib/bpf/bpf.c      | 12 +++++++++++-
 tools/lib/bpf/bpf.h      |  2 ++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 03be8c96bbac..b95fed0c1644 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -981,19 +981,29 @@ int bpf_map_get_fd_by_id(__u32 id)
 	return bpf_map_get_fd_by_id_opts(id, NULL);
 }
 
-int bpf_btf_get_fd_by_id(__u32 id)
+int bpf_btf_get_fd_by_id_opts(__u32 id,
+			      const struct bpf_get_fd_by_id_opts *opts)
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
 	union bpf_attr attr;
 	int fd;
 
+	if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
+		return libbpf_err(-EINVAL);
+
 	memset(&attr, 0, attr_sz);
 	attr.btf_id = id;
+	attr.open_flags = OPTS_GET(opts, open_flags, 0);
 
 	fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, attr_sz);
 	return libbpf_err_errno(fd);
 }
 
+int bpf_btf_get_fd_by_id(__u32 id)
+{
+	return bpf_btf_get_fd_by_id_opts(id, NULL);
+}
+
 int bpf_link_get_fd_by_id(__u32 id)
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 4558bafbce14..4b487305eeb8 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -380,6 +380,8 @@ LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
 LIBBPF_API int bpf_map_get_fd_by_id_opts(__u32 id,
 				const struct bpf_get_fd_by_id_opts *opts);
 LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
+LIBBPF_API int bpf_btf_get_fd_by_id_opts(__u32 id,
+				const struct bpf_get_fd_by_id_opts *opts);
 LIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
 LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
 
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index c3604eaa220d..7011d5eec67b 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -371,6 +371,7 @@ LIBBPF_1.0.0 {
 
 LIBBPF_1.1.0 {
 	global:
+		bpf_btf_get_fd_by_id_opts;
 		bpf_map_get_fd_by_id_opts;
 		bpf_prog_get_fd_by_id_opts;
 		user_ring_buffer__discard;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 5/6] libbpf: Introduce bpf_link_get_fd_by_id_opts()
  2022-10-06 11:07 [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id() Roberto Sassu
                   ` (3 preceding siblings ...)
  2022-10-06 11:07 ` [PATCH v2 4/6] libbpf: Introduce bpf_btf_get_fd_by_id_opts() Roberto Sassu
@ 2022-10-06 11:07 ` Roberto Sassu
  2022-10-06 11:07 ` [PATCH v2 6/6] selftests/bpf: Add tests for _opts variants of bpf_*_get_fd_by_id() Roberto Sassu
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Roberto Sassu @ 2022-10-06 11:07 UTC (permalink / raw)
  To: ast, daniel, andrii, martin.lau, song, yhs, john.fastabend,
	kpsingh, sdf, haoluo, jolsa, mykolal, shuah
  Cc: bpf, linux-kselftest, linux-kernel, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

Introduce bpf_link_get_fd_by_id_opts(), for symmetry with
bpf_map_get_fd_by_id_opts(), to let the caller pass the newly introduced
data structure bpf_get_fd_by_id_opts. Keep the existing
bpf_link_get_fd_by_id(), and call bpf_link_get_fd_by_id_opts() with NULL as
opts argument, to prevent setting open_flags.

Currently, the kernel does not support non-zero open_flags for
bpf_link_get_fd_by_id_opts(), and a call with them will result in an error
returned by the bpf() system call. The caller should always pass zero
open_flags.

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 tools/lib/bpf/bpf.c      | 12 +++++++++++-
 tools/lib/bpf/bpf.h      |  2 ++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index b95fed0c1644..9aff98f42a3d 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -1004,19 +1004,29 @@ int bpf_btf_get_fd_by_id(__u32 id)
 	return bpf_btf_get_fd_by_id_opts(id, NULL);
 }
 
-int bpf_link_get_fd_by_id(__u32 id)
+int bpf_link_get_fd_by_id_opts(__u32 id,
+			       const struct bpf_get_fd_by_id_opts *opts)
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
 	union bpf_attr attr;
 	int fd;
 
+	if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
+		return libbpf_err(-EINVAL);
+
 	memset(&attr, 0, attr_sz);
 	attr.link_id = id;
+	attr.open_flags = OPTS_GET(opts, open_flags, 0);
 
 	fd = sys_bpf_fd(BPF_LINK_GET_FD_BY_ID, &attr, attr_sz);
 	return libbpf_err_errno(fd);
 }
 
+int bpf_link_get_fd_by_id(__u32 id)
+{
+	return bpf_link_get_fd_by_id_opts(id, NULL);
+}
+
 int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len)
 {
 	const size_t attr_sz = offsetofend(union bpf_attr, info);
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 4b487305eeb8..a112e0ed1b19 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -383,6 +383,8 @@ LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
 LIBBPF_API int bpf_btf_get_fd_by_id_opts(__u32 id,
 				const struct bpf_get_fd_by_id_opts *opts);
 LIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
+LIBBPF_API int bpf_link_get_fd_by_id_opts(__u32 id,
+				const struct bpf_get_fd_by_id_opts *opts);
 LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
 
 struct bpf_prog_query_opts {
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index 7011d5eec67b..71bf5691a689 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -372,6 +372,7 @@ LIBBPF_1.0.0 {
 LIBBPF_1.1.0 {
 	global:
 		bpf_btf_get_fd_by_id_opts;
+		bpf_link_get_fd_by_id_opts;
 		bpf_map_get_fd_by_id_opts;
 		bpf_prog_get_fd_by_id_opts;
 		user_ring_buffer__discard;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 6/6] selftests/bpf: Add tests for _opts variants of bpf_*_get_fd_by_id()
  2022-10-06 11:07 [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id() Roberto Sassu
                   ` (4 preceding siblings ...)
  2022-10-06 11:07 ` [PATCH v2 5/6] libbpf: Introduce bpf_link_get_fd_by_id_opts() Roberto Sassu
@ 2022-10-06 11:07 ` Roberto Sassu
  2022-10-07 10:59 ` [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id() Jiri Olsa
  2022-10-11  0:20 ` patchwork-bot+netdevbpf
  7 siblings, 0 replies; 10+ messages in thread
From: Roberto Sassu @ 2022-10-06 11:07 UTC (permalink / raw)
  To: ast, daniel, andrii, martin.lau, song, yhs, john.fastabend,
	kpsingh, sdf, haoluo, jolsa, mykolal, shuah
  Cc: bpf, linux-kselftest, linux-kernel, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

Introduce the data_input map, write-protected with a small eBPF program
implementing the lsm/bpf_map hook.

Then, ensure that bpf_map_get_fd_by_id() and bpf_map_get_fd_by_id_opts()
with NULL opts don't succeed due to requesting read-write access to the
write-protected map. Also, ensure that bpf_map_get_fd_by_id_opts() with
open_flags in opts set to BPF_F_RDONLY instead succeeds.

After obtaining a read-only fd, ensure that only map lookup succeeds and
not update. Ensure that update works only with the read-write fd obtained
at program loading time, when the write protection was not yet enabled.

Finally, ensure that the other _opts variants of bpf_*_get_fd_by_id() don't
work if the BPF_F_RDONLY flag is set in opts (due to the kernel not
handling the open_flags member of bpf_attr).

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 tools/testing/selftests/bpf/DENYLIST.s390x    |  1 +
 .../bpf/prog_tests/libbpf_get_fd_by_id_opts.c | 87 +++++++++++++++++++
 .../bpf/progs/test_libbpf_get_fd_by_id_opts.c | 36 ++++++++
 3 files changed, 124 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_libbpf_get_fd_by_id_opts.c

diff --git a/tools/testing/selftests/bpf/DENYLIST.s390x b/tools/testing/selftests/bpf/DENYLIST.s390x
index 17e074eb42b8..f3a56dcc4eec 100644
--- a/tools/testing/selftests/bpf/DENYLIST.s390x
+++ b/tools/testing/selftests/bpf/DENYLIST.s390x
@@ -75,3 +75,4 @@ user_ringbuf                             # failed to find kernel BTF type ID of
 lookup_key                               # JIT does not support calling kernel function                                (kfunc)
 verify_pkcs7_sig                         # JIT does not support calling kernel function                                (kfunc)
 kfunc_dynptr_param                       # JIT does not support calling kernel function                                (kfunc)
+libbpf_get_fd_by_id_opts                 # failed to attach: ERROR: strerror_r(-524)=22                                (trampoline)
diff --git a/tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c b/tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c
new file mode 100644
index 000000000000..25e5dfa9c315
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (C) 2022 Huawei Technologies Duesseldorf GmbH
+ *
+ * Author: Roberto Sassu <roberto.sassu@huawei.com>
+ */
+
+#include <test_progs.h>
+
+#include "test_libbpf_get_fd_by_id_opts.skel.h"
+
+void test_libbpf_get_fd_by_id_opts(void)
+{
+	struct test_libbpf_get_fd_by_id_opts *skel;
+	struct bpf_map_info info_m = {};
+	__u32 len = sizeof(info_m), value;
+	int ret, zero = 0, fd = -1;
+	LIBBPF_OPTS(bpf_get_fd_by_id_opts, fd_opts_rdonly,
+		.open_flags = BPF_F_RDONLY,
+	);
+
+	skel = test_libbpf_get_fd_by_id_opts__open_and_load();
+	if (!ASSERT_OK_PTR(skel,
+			   "test_libbpf_get_fd_by_id_opts__open_and_load"))
+		return;
+
+	ret = test_libbpf_get_fd_by_id_opts__attach(skel);
+	if (!ASSERT_OK(ret, "test_libbpf_get_fd_by_id_opts__attach"))
+		goto close_prog;
+
+	ret = bpf_obj_get_info_by_fd(bpf_map__fd(skel->maps.data_input),
+				     &info_m, &len);
+	if (!ASSERT_OK(ret, "bpf_obj_get_info_by_fd"))
+		goto close_prog;
+
+	fd = bpf_map_get_fd_by_id(info_m.id);
+	if (!ASSERT_LT(fd, 0, "bpf_map_get_fd_by_id"))
+		goto close_prog;
+
+	fd = bpf_map_get_fd_by_id_opts(info_m.id, NULL);
+	if (!ASSERT_LT(fd, 0, "bpf_map_get_fd_by_id_opts"))
+		goto close_prog;
+
+	fd = bpf_map_get_fd_by_id_opts(info_m.id, &fd_opts_rdonly);
+	if (!ASSERT_GE(fd, 0, "bpf_map_get_fd_by_id_opts"))
+		goto close_prog;
+
+	/* Map lookup should work with read-only fd. */
+	ret = bpf_map_lookup_elem(fd, &zero, &value);
+	if (!ASSERT_OK(ret, "bpf_map_lookup_elem"))
+		goto close_prog;
+
+	if (!ASSERT_EQ(value, 0, "map value mismatch"))
+		goto close_prog;
+
+	/* Map update should not work with read-only fd. */
+	ret = bpf_map_update_elem(fd, &zero, &len, BPF_ANY);
+	if (!ASSERT_LT(ret, 0, "bpf_map_update_elem"))
+		goto close_prog;
+
+	/* Map update should work with read-write fd. */
+	ret = bpf_map_update_elem(bpf_map__fd(skel->maps.data_input), &zero,
+				  &len, BPF_ANY);
+	if (!ASSERT_OK(ret, "bpf_map_update_elem"))
+		goto close_prog;
+
+	/* Prog get fd with opts set should not work (no kernel support). */
+	ret = bpf_prog_get_fd_by_id_opts(0, &fd_opts_rdonly);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_prog_get_fd_by_id_opts"))
+		goto close_prog;
+
+	/* Link get fd with opts set should not work (no kernel support). */
+	ret = bpf_link_get_fd_by_id_opts(0, &fd_opts_rdonly);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_link_get_fd_by_id_opts"))
+		goto close_prog;
+
+	/* BTF get fd with opts set should not work (no kernel support). */
+	ret = bpf_btf_get_fd_by_id_opts(0, &fd_opts_rdonly);
+	ASSERT_EQ(ret, -EINVAL, "bpf_btf_get_fd_by_id_opts");
+
+close_prog:
+	if (fd >= 0)
+		close(fd);
+
+	test_libbpf_get_fd_by_id_opts__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_libbpf_get_fd_by_id_opts.c b/tools/testing/selftests/bpf/progs/test_libbpf_get_fd_by_id_opts.c
new file mode 100644
index 000000000000..f5ac5f3e8919
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_libbpf_get_fd_by_id_opts.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (C) 2022 Huawei Technologies Duesseldorf GmbH
+ *
+ * Author: Roberto Sassu <roberto.sassu@huawei.com>
+ */
+
+#include "vmlinux.h"
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+/* From include/linux/mm.h. */
+#define FMODE_WRITE	0x2
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u32);
+} data_input SEC(".maps");
+
+char _license[] SEC("license") = "GPL";
+
+SEC("lsm/bpf_map")
+int BPF_PROG(check_access, struct bpf_map *map, fmode_t fmode)
+{
+	if (map != (struct bpf_map *)&data_input)
+		return 0;
+
+	if (fmode & FMODE_WRITE)
+		return -EACCES;
+
+	return 0;
+}
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id()
  2022-10-06 11:07 [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id() Roberto Sassu
                   ` (5 preceding siblings ...)
  2022-10-06 11:07 ` [PATCH v2 6/6] selftests/bpf: Add tests for _opts variants of bpf_*_get_fd_by_id() Roberto Sassu
@ 2022-10-07 10:59 ` Jiri Olsa
  2022-10-07 11:31   ` Roberto Sassu
  2022-10-11  0:20 ` patchwork-bot+netdevbpf
  7 siblings, 1 reply; 10+ messages in thread
From: Jiri Olsa @ 2022-10-07 10:59 UTC (permalink / raw)
  To: Roberto Sassu
  Cc: ast, daniel, andrii, martin.lau, song, yhs, john.fastabend,
	kpsingh, sdf, haoluo, mykolal, shuah, bpf, linux-kselftest,
	linux-kernel, Roberto Sassu

On Thu, Oct 06, 2022 at 01:07:30PM +0200, Roberto Sassu wrote:
> From: Roberto Sassu <roberto.sassu@huawei.com>
> 
> Add the _opts variant for bpf_*_get_fd_by_id() functions, to be able to
> pass to the kernel more options, when requesting a fd of an eBPF object.
> 
> Pass the options through a newly introduced structure,
> bpf_get_fd_by_id_opts, which currently contains open_flags (the other two
> members are for compatibility and for padding).
> 
> open_flags allows the caller to request specific permissions to access a
> map (e.g. read-only). This is useful for example in the situation where a
> map is write-protected.
> 
> Besides patches 2-6, which introduce the new variants and the data
> structure, patch 1 fixes the LIBBPF_1.0.0 declaration in libbpf.map.
> 
> Changelog
> 
> v1:
>  - Don't CC stable kernel mailing list for patch 1 (suggested by Andrii)
>  - Rename bpf_get_fd_opts struct to bpf_get_fd_by_id_opts (suggested by
>    Andrii)
>  - Move declaration of _opts variants after non-opts variants (suggested by
>    Andrii)
>  - Correctly initialize bpf_map_info, fix style issues, use map from
>    skeleton, check valid fd in the test (suggested by Andrii)
>  - Rename libbpf_get_fd_opts test to libbpf_get_fd_by_id_opts
> 
> Roberto Sassu (6):
>   libbpf: Fix LIBBPF_1.0.0 declaration in libbpf.map
>   libbpf: Introduce bpf_get_fd_by_id_opts and
>     bpf_map_get_fd_by_id_opts()
>   libbpf: Introduce bpf_prog_get_fd_by_id_opts()
>   libbpf: Introduce bpf_btf_get_fd_by_id_opts()
>   libbpf: Introduce bpf_link_get_fd_by_id_opts()
>   selftests/bpf: Add tests for _opts variants of bpf_*_get_fd_by_id()

it's not marked, but I'm assuming this goes to bpf-next, right?

anyway, lgtm

Acked-by: Jiri Olsa <jolsa@kernel.org>

thanks,
jirka

> 
>  tools/lib/bpf/bpf.c                           | 48 +++++++++-
>  tools/lib/bpf/bpf.h                           | 16 ++++
>  tools/lib/bpf/libbpf.map                      |  6 +-
>  tools/testing/selftests/bpf/DENYLIST.s390x    |  1 +
>  .../bpf/prog_tests/libbpf_get_fd_by_id_opts.c | 87 +++++++++++++++++++
>  .../bpf/progs/test_libbpf_get_fd_by_id_opts.c | 36 ++++++++
>  6 files changed, 189 insertions(+), 5 deletions(-)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c
>  create mode 100644 tools/testing/selftests/bpf/progs/test_libbpf_get_fd_by_id_opts.c
> 
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id()
  2022-10-07 10:59 ` [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id() Jiri Olsa
@ 2022-10-07 11:31   ` Roberto Sassu
  0 siblings, 0 replies; 10+ messages in thread
From: Roberto Sassu @ 2022-10-07 11:31 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: ast, daniel, andrii, martin.lau, song, yhs, john.fastabend,
	kpsingh, sdf, haoluo, mykolal, shuah, bpf, linux-kselftest,
	linux-kernel, Roberto Sassu

On Fri, 2022-10-07 at 12:59 +0200, Jiri Olsa wrote:
> On Thu, Oct 06, 2022 at 01:07:30PM +0200, Roberto Sassu wrote:
> > From: Roberto Sassu <roberto.sassu@huawei.com>
> > 
> > Add the _opts variant for bpf_*_get_fd_by_id() functions, to be
> > able to
> > pass to the kernel more options, when requesting a fd of an eBPF
> > object.
> > 
> > Pass the options through a newly introduced structure,
> > bpf_get_fd_by_id_opts, which currently contains open_flags (the
> > other two
> > members are for compatibility and for padding).
> > 
> > open_flags allows the caller to request specific permissions to
> > access a
> > map (e.g. read-only). This is useful for example in the situation
> > where a
> > map is write-protected.
> > 
> > Besides patches 2-6, which introduce the new variants and the data
> > structure, patch 1 fixes the LIBBPF_1.0.0 declaration in
> > libbpf.map.
> > 
> > Changelog
> > 
> > v1:
> >  - Don't CC stable kernel mailing list for patch 1 (suggested by
> > Andrii)
> >  - Rename bpf_get_fd_opts struct to bpf_get_fd_by_id_opts
> > (suggested by
> >    Andrii)
> >  - Move declaration of _opts variants after non-opts variants
> > (suggested by
> >    Andrii)
> >  - Correctly initialize bpf_map_info, fix style issues, use map
> > from
> >    skeleton, check valid fd in the test (suggested by Andrii)
> >  - Rename libbpf_get_fd_opts test to libbpf_get_fd_by_id_opts
> > 
> > Roberto Sassu (6):
> >   libbpf: Fix LIBBPF_1.0.0 declaration in libbpf.map
> >   libbpf: Introduce bpf_get_fd_by_id_opts and
> >     bpf_map_get_fd_by_id_opts()
> >   libbpf: Introduce bpf_prog_get_fd_by_id_opts()
> >   libbpf: Introduce bpf_btf_get_fd_by_id_opts()
> >   libbpf: Introduce bpf_link_get_fd_by_id_opts()
> >   selftests/bpf: Add tests for _opts variants of
> > bpf_*_get_fd_by_id()
> 
> it's not marked, but I'm assuming this goes to bpf-next, right?

At the next possible chance would be perfect.

> anyway, lgtm
> 
> Acked-by: Jiri Olsa <jolsa@kernel.org>

Thanks

Roberto

> thanks,
> jirka
> 
> >  tools/lib/bpf/bpf.c                           | 48 +++++++++-
> >  tools/lib/bpf/bpf.h                           | 16 ++++
> >  tools/lib/bpf/libbpf.map                      |  6 +-
> >  tools/testing/selftests/bpf/DENYLIST.s390x    |  1 +
> >  .../bpf/prog_tests/libbpf_get_fd_by_id_opts.c | 87
> > +++++++++++++++++++
> >  .../bpf/progs/test_libbpf_get_fd_by_id_opts.c | 36 ++++++++
> >  6 files changed, 189 insertions(+), 5 deletions(-)
> >  create mode 100644
> > tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c
> >  create mode 100644
> > tools/testing/selftests/bpf/progs/test_libbpf_get_fd_by_id_opts.c
> > 
> > -- 
> > 2.25.1
> > 


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id()
  2022-10-06 11:07 [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id() Roberto Sassu
                   ` (6 preceding siblings ...)
  2022-10-07 10:59 ` [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id() Jiri Olsa
@ 2022-10-11  0:20 ` patchwork-bot+netdevbpf
  7 siblings, 0 replies; 10+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-10-11  0:20 UTC (permalink / raw)
  To: Roberto Sassu
  Cc: ast, daniel, andrii, martin.lau, song, yhs, john.fastabend,
	kpsingh, sdf, haoluo, jolsa, mykolal, shuah, bpf,
	linux-kselftest, linux-kernel, roberto.sassu

Hello:

This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Thu,  6 Oct 2022 13:07:30 +0200 you wrote:
> From: Roberto Sassu <roberto.sassu@huawei.com>
> 
> Add the _opts variant for bpf_*_get_fd_by_id() functions, to be able to
> pass to the kernel more options, when requesting a fd of an eBPF object.
> 
> Pass the options through a newly introduced structure,
> bpf_get_fd_by_id_opts, which currently contains open_flags (the other two
> members are for compatibility and for padding).
> 
> [...]

Here is the summary with links:
  - [v2,1/6] libbpf: Fix LIBBPF_1.0.0 declaration in libbpf.map
    https://git.kernel.org/bpf/bpf-next/c/7a366da2d2ba
  - [v2,2/6] libbpf: Introduce bpf_get_fd_by_id_opts and bpf_map_get_fd_by_id_opts()
    https://git.kernel.org/bpf/bpf-next/c/243e300563b1
  - [v2,3/6] libbpf: Introduce bpf_prog_get_fd_by_id_opts()
    https://git.kernel.org/bpf/bpf-next/c/8f13f168ea14
  - [v2,4/6] libbpf: Introduce bpf_btf_get_fd_by_id_opts()
    https://git.kernel.org/bpf/bpf-next/c/2ce7cbf2ba71
  - [v2,5/6] libbpf: Introduce bpf_link_get_fd_by_id_opts()
    https://git.kernel.org/bpf/bpf-next/c/97c8f9dd5db8
  - [v2,6/6] selftests/bpf: Add tests for _opts variants of bpf_*_get_fd_by_id()
    https://git.kernel.org/bpf/bpf-next/c/a9c7c18b5759

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-10-11  0:20 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-06 11:07 [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id() Roberto Sassu
2022-10-06 11:07 ` [PATCH v2 1/6] libbpf: Fix LIBBPF_1.0.0 declaration in libbpf.map Roberto Sassu
2022-10-06 11:07 ` [PATCH v2 2/6] libbpf: Introduce bpf_get_fd_by_id_opts and bpf_map_get_fd_by_id_opts() Roberto Sassu
2022-10-06 11:07 ` [PATCH v2 3/6] libbpf: Introduce bpf_prog_get_fd_by_id_opts() Roberto Sassu
2022-10-06 11:07 ` [PATCH v2 4/6] libbpf: Introduce bpf_btf_get_fd_by_id_opts() Roberto Sassu
2022-10-06 11:07 ` [PATCH v2 5/6] libbpf: Introduce bpf_link_get_fd_by_id_opts() Roberto Sassu
2022-10-06 11:07 ` [PATCH v2 6/6] selftests/bpf: Add tests for _opts variants of bpf_*_get_fd_by_id() Roberto Sassu
2022-10-07 10:59 ` [PATCH v2 0/6] Add _opts variant for bpf_*_get_fd_by_id() Jiri Olsa
2022-10-07 11:31   ` Roberto Sassu
2022-10-11  0:20 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).