bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCHv3 bpf-next] libbpf: Add names for auxiliary maps
@ 2022-08-11  3:40 Hangbin Liu
  2022-08-11 22:15 ` Andrii Nakryiko
  2022-08-11 22:20 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 4+ messages in thread
From: Hangbin Liu @ 2022-08-11  3:40 UTC (permalink / raw)
  To: netdev
  Cc: Quentin Monnet, Andrii Nakryiko, Alexei Starovoitov,
	Daniel Borkmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	bpf, Hangbin Liu

The bpftool self-created maps can appear in final map show output due to
deferred removal in kernel. These maps don't have a name, which would make
users confused about where it comes from.

With a libbpf_ prefix name, users could know who created these maps.
It also could make some tests (like test_offload.py, which skip base maps
without names as a workaround) filter them out.

Kernel adds bpf prog/map name support in the same merge
commit fadad670a8ab ("Merge branch 'bpf-extend-info'"). So we can also use
kernel_supports(NULL, FEAT_PROG_NAME) to check if kernel supports map name.

As disscussed[1], Let's make bpf_map_create accept non-null
name string, and silently ignore the name if kernel doesn't support.

[1] https://lore.kernel.org/bpf/CAEf4BzYL1TQwo1231s83pjTdFPk9XWWhfZC5=KzkU-VO0k=0Ug@mail.gmail.com/

Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
---
v3: let bpf_map_create ignore the name if kernel doesn't support
v2: rename the wrapper with proper name
---
 tools/lib/bpf/bpf.c    | 2 +-
 tools/lib/bpf/libbpf.c | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index efcc06dafbd9..6a96e665dc5d 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -183,7 +183,7 @@ int bpf_map_create(enum bpf_map_type map_type,
 		return libbpf_err(-EINVAL);
 
 	attr.map_type = map_type;
-	if (map_name)
+	if (map_name && kernel_supports(NULL, FEAT_PROG_NAME))
 		libbpf_strlcpy(attr.map_name, map_name, sizeof(attr.map_name));
 	attr.key_size = key_size;
 	attr.value_size = value_size;
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index f7364ea82ac1..a075211b3730 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4432,7 +4432,7 @@ static int probe_kern_global_data(void)
 	};
 	int ret, map, insn_cnt = ARRAY_SIZE(insns);
 
-	map = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), 32, 1, NULL);
+	map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_global", sizeof(int), 32, 1, NULL);
 	if (map < 0) {
 		ret = -errno;
 		cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
@@ -4565,7 +4565,7 @@ static int probe_kern_array_mmap(void)
 	LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE);
 	int fd;
 
-	fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), sizeof(int), 1, &opts);
+	fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_mmap", sizeof(int), sizeof(int), 1, &opts);
 	return probe_fd(fd);
 }
 
@@ -4612,7 +4612,7 @@ static int probe_prog_bind_map(void)
 	};
 	int ret, map, prog, insn_cnt = ARRAY_SIZE(insns);
 
-	map = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), 32, 1, NULL);
+	map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_det_bind", sizeof(int), 32, 1, NULL);
 	if (map < 0) {
 		ret = -errno;
 		cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
-- 
2.35.3


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

* Re: [PATCHv3 bpf-next] libbpf: Add names for auxiliary maps
  2022-08-11  3:40 [PATCHv3 bpf-next] libbpf: Add names for auxiliary maps Hangbin Liu
@ 2022-08-11 22:15 ` Andrii Nakryiko
  2022-08-12  1:59   ` Hangbin Liu
  2022-08-11 22:20 ` patchwork-bot+netdevbpf
  1 sibling, 1 reply; 4+ messages in thread
From: Andrii Nakryiko @ 2022-08-11 22:15 UTC (permalink / raw)
  To: Hangbin Liu
  Cc: netdev, Quentin Monnet, Andrii Nakryiko, Alexei Starovoitov,
	Daniel Borkmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	bpf

On Wed, Aug 10, 2022 at 8:40 PM Hangbin Liu <liuhangbin@gmail.com> wrote:
>
> The bpftool self-created maps can appear in final map show output due to
> deferred removal in kernel. These maps don't have a name, which would make
> users confused about where it comes from.
>
> With a libbpf_ prefix name, users could know who created these maps.
> It also could make some tests (like test_offload.py, which skip base maps
> without names as a workaround) filter them out.
>
> Kernel adds bpf prog/map name support in the same merge
> commit fadad670a8ab ("Merge branch 'bpf-extend-info'"). So we can also use
> kernel_supports(NULL, FEAT_PROG_NAME) to check if kernel supports map name.
>
> As disscussed[1], Let's make bpf_map_create accept non-null
> name string, and silently ignore the name if kernel doesn't support.
>
> [1] https://lore.kernel.org/bpf/CAEf4BzYL1TQwo1231s83pjTdFPk9XWWhfZC5=KzkU-VO0k=0Ug@mail.gmail.com/
>
> Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
> ---
> v3: let bpf_map_create ignore the name if kernel doesn't support
> v2: rename the wrapper with proper name
> ---

Applied to bpf-next. But let's also do the same for bpf_prog_load().
We can make probe_kern_prog_name() use sys_bpf_prog_load() directly
and avoid calling bpf_prog_load() and thus avoiding circular
dependency.

Will you be able to do this change?


>  tools/lib/bpf/bpf.c    | 2 +-
>  tools/lib/bpf/libbpf.c | 6 +++---
>  2 files changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> index efcc06dafbd9..6a96e665dc5d 100644
> --- a/tools/lib/bpf/bpf.c
> +++ b/tools/lib/bpf/bpf.c
> @@ -183,7 +183,7 @@ int bpf_map_create(enum bpf_map_type map_type,
>                 return libbpf_err(-EINVAL);
>
>         attr.map_type = map_type;
> -       if (map_name)
> +       if (map_name && kernel_supports(NULL, FEAT_PROG_NAME))
>                 libbpf_strlcpy(attr.map_name, map_name, sizeof(attr.map_name));
>         attr.key_size = key_size;
>         attr.value_size = value_size;
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index f7364ea82ac1..a075211b3730 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -4432,7 +4432,7 @@ static int probe_kern_global_data(void)
>         };
>         int ret, map, insn_cnt = ARRAY_SIZE(insns);
>
> -       map = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), 32, 1, NULL);
> +       map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_global", sizeof(int), 32, 1, NULL);
>         if (map < 0) {
>                 ret = -errno;
>                 cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
> @@ -4565,7 +4565,7 @@ static int probe_kern_array_mmap(void)
>         LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE);
>         int fd;
>
> -       fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), sizeof(int), 1, &opts);
> +       fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_mmap", sizeof(int), sizeof(int), 1, &opts);
>         return probe_fd(fd);
>  }
>
> @@ -4612,7 +4612,7 @@ static int probe_prog_bind_map(void)
>         };
>         int ret, map, prog, insn_cnt = ARRAY_SIZE(insns);
>
> -       map = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), 32, 1, NULL);
> +       map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_det_bind", sizeof(int), 32, 1, NULL);
>         if (map < 0) {
>                 ret = -errno;
>                 cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
> --
> 2.35.3
>

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

* Re: [PATCHv3 bpf-next] libbpf: Add names for auxiliary maps
  2022-08-11  3:40 [PATCHv3 bpf-next] libbpf: Add names for auxiliary maps Hangbin Liu
  2022-08-11 22:15 ` Andrii Nakryiko
@ 2022-08-11 22:20 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-08-11 22:20 UTC (permalink / raw)
  To: Hangbin Liu
  Cc: netdev, quentin, andrii, ast, daniel, martin.lau, song, yhs,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, bpf

Hello:

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

On Thu, 11 Aug 2022 11:40:20 +0800 you wrote:
> The bpftool self-created maps can appear in final map show output due to
> deferred removal in kernel. These maps don't have a name, which would make
> users confused about where it comes from.
> 
> With a libbpf_ prefix name, users could know who created these maps.
> It also could make some tests (like test_offload.py, which skip base maps
> without names as a workaround) filter them out.
> 
> [...]

Here is the summary with links:
  - [PATCHv3,bpf-next] libbpf: Add names for auxiliary maps
    https://git.kernel.org/bpf/bpf-next/c/10b62d6a38f7

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] 4+ messages in thread

* Re: [PATCHv3 bpf-next] libbpf: Add names for auxiliary maps
  2022-08-11 22:15 ` Andrii Nakryiko
@ 2022-08-12  1:59   ` Hangbin Liu
  0 siblings, 0 replies; 4+ messages in thread
From: Hangbin Liu @ 2022-08-12  1:59 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: netdev, Quentin Monnet, Andrii Nakryiko, Alexei Starovoitov,
	Daniel Borkmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	bpf

On Fri, Aug 12, 2022 at 6:15 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> Applied to bpf-next. But let's also do the same for bpf_prog_load().
> We can make probe_kern_prog_name() use sys_bpf_prog_load() directly
> and avoid calling bpf_prog_load() and thus avoiding circular
> dependency.

Ah, yes, we can do it this way. I will post the patch today.

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

end of thread, other threads:[~2022-08-12  1:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-11  3:40 [PATCHv3 bpf-next] libbpf: Add names for auxiliary maps Hangbin Liu
2022-08-11 22:15 ` Andrii Nakryiko
2022-08-12  1:59   ` Hangbin Liu
2022-08-11 22: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).