All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Jiri Olsa <jolsa@redhat.com>,
	Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Yinjun Zhang <yinjun.zhang@corigine.com>,
	bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>, Martin Lau <kafai@fb.com>,
	Song Liu <songliubraving@fb.com>,
	john fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>, <niklas.soderlund@corigine.com>,
	Simon Horman <simon.horman@corigine.com>
Subject: Re: [PATCH bpf] bpftool: fix the error when lookup in no-btf maps
Date: Mon, 7 Feb 2022 12:34:47 -0800	[thread overview]
Message-ID: <ab1e74e8-ce5a-44b8-47cb-3f326973bb59@fb.com> (raw)
In-Reply-To: <YgFkVXggmihEpO/o@krava>



On 2/7/22 10:26 AM, Jiri Olsa wrote:
> On Mon, Feb 07, 2022 at 06:57:20PM +0100, Jiri Olsa wrote:
>> On Mon, Feb 07, 2022 at 09:42:25AM -0800, Andrii Nakryiko wrote:
>>> On Mon, Feb 7, 2022 at 8:00 AM Yinjun Zhang <yinjun.zhang@corigine.com> wrote:
>>>>
>>>> When reworking btf__get_from_id() in commit a19f93cfafdf the error
>>>> handling when calling bpf_btf_get_fd_by_id() changed. Before the rework
>>>> if bpf_btf_get_fd_by_id() failed the error would not be propagated to
>>>> callers of btf__get_from_id(), after the rework it is. This lead to a
>>>> change in behavior in print_key_value() that now prints an error when
>>>> trying to lookup keys in maps with no btf available.
>>>>
>>>> Fix this by following the way used in dumping maps to allow to look up
>>>> keys in no-btf maps, by which it decides whether and where to get the
>>>> btf info according to the btf value type.
>>>>
>>>> Fixes: a19f93cfafdf ("libbpf: Add internal helper to load BTF data by FD")
>>>> Signed-off-by: Yinjun Zhang <yinjun.zhang@corigine.com>
>>>> Reviewed-by: Niklas Söderlund <niklas.soderlund@corigine.com>
>>>> Signed-off-by: Simon Horman <simon.horman@corigine.com>
>>>> ---
>>>>   tools/bpf/bpftool/map.c | 6 ++----
>>>>   1 file changed, 2 insertions(+), 4 deletions(-)
>>>>
>>>> diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
>>>> index cc530a229812..4fc772d66e3a 100644
>>>> --- a/tools/bpf/bpftool/map.c
>>>> +++ b/tools/bpf/bpftool/map.c
>>>> @@ -1054,11 +1054,9 @@ static void print_key_value(struct bpf_map_info *info, void *key,
>>>>          json_writer_t *btf_wtr;
>>>>          struct btf *btf;
>>>>
>>>> -       btf = btf__load_from_kernel_by_id(info->btf_id);
>>>> -       if (libbpf_get_error(btf)) {
>>>> -               p_err("failed to get btf");
>>>> +       btf = get_map_kv_btf(info);
>>>> +       if (libbpf_get_error(btf))
>>>
>>> See discussion in [0], it seems relevant.
>>>
>>>    [0] https://lore.kernel.org/bpf/20220204225823.339548-3-jolsa@kernel.org/

For the patch in the above link:

diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
index c66a3c979b7a..2ccf85042e75 100644
--- a/tools/bpf/bpftool/map.c
+++ b/tools/bpf/bpftool/map.c
@@ -862,6 +862,7 @@ map_dump(int fd, struct bpf_map_info *info, 
json_writer_t *wtr,
  	prev_key = NULL;

  	if (wtr) {
+		errno = 0;
  		btf = get_map_kv_btf(info);
  		err = libbpf_get_error(btf);
  		if (err) {
-- 

Do we know who sets non-zero errno in the above?
Maybe we can fix the issue in that place?

>>
>> I checked and this patch does not fix the problem for me,
>> but looks like similar issue, do you have test case for this?
>>
>> mine is to dump any no-btf map with -p option
> 
> anyway I think your change should go in separately,
> I can send change below (v2 for [0] above) on top of yours
> 
> thanks,
> jirka
> 
> 
> ---
>   tools/bpf/bpftool/map.c | 31 +++++++++++++------------------
>   1 file changed, 13 insertions(+), 18 deletions(-)
> 
> diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
> index c66a3c979b7a..8562add7417d 100644
> --- a/tools/bpf/bpftool/map.c
> +++ b/tools/bpf/bpftool/map.c
> @@ -805,29 +805,28 @@ static int maps_have_btf(int *fds, int nb_fds)
>   
>   static struct btf *btf_vmlinux;
>   
> -static struct btf *get_map_kv_btf(const struct bpf_map_info *info)
> +static int get_map_kv_btf(const struct bpf_map_info *info, struct btf **btf)
>   {
> -	struct btf *btf = NULL;
> +	int err = 0;
>   
>   	if (info->btf_vmlinux_value_type_id) {
>   		if (!btf_vmlinux) {
>   			btf_vmlinux = libbpf_find_kernel_btf();
> -			if (libbpf_get_error(btf_vmlinux))
> +			err = libbpf_get_error(btf_vmlinux);
> +			if (err) {
>   				p_err("failed to get kernel btf");
> +				return err;
> +			}
>   		}
> -		return btf_vmlinux;
> +		*btf = btf_vmlinux;
>   	} else if (info->btf_value_type_id) {
> -		int err;
> -
> -		btf = btf__load_from_kernel_by_id(info->btf_id);
> -		err = libbpf_get_error(btf);
> -		if (err) {
> +		*btf = btf__load_from_kernel_by_id(info->btf_id);
> +		err = libbpf_get_error(*btf);
> +		if (err)
>   			p_err("failed to get btf");
> -			btf = ERR_PTR(err);
> -		}
>   	}
>   
> -	return btf;
> +	return err;
>   }
>   
>   static void free_map_kv_btf(struct btf *btf)
> @@ -862,8 +861,7 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
>   	prev_key = NULL;
>   
>   	if (wtr) {
> -		btf = get_map_kv_btf(info);
> -		err = libbpf_get_error(btf);
> +		err = get_map_kv_btf(info, &btf);
>   		if (err) {
>   			goto exit_free;
>   		}
> @@ -1054,11 +1052,8 @@ static void print_key_value(struct bpf_map_info *info, void *key,
>   	json_writer_t *btf_wtr;
>   	struct btf *btf;
>   
> -	btf = btf__load_from_kernel_by_id(info->btf_id);
> -	if (libbpf_get_error(btf)) {
> -		p_err("failed to get btf");
> +	if (get_map_kv_btf(info, &btf))
>   		return;
> -	}
>   
>   	if (json_output) {
>   		print_entry_json(info, key, value, btf);

  reply	other threads:[~2022-02-07 20:48 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-07 16:00 [PATCH bpf] bpftool: fix the error when lookup in no-btf maps Yinjun Zhang
2022-02-07 17:42 ` Andrii Nakryiko
2022-02-07 17:57   ` Jiri Olsa
2022-02-07 18:26     ` Jiri Olsa
2022-02-07 20:34       ` Yonghong Song [this message]
2022-02-08  4:53       ` Yinjun Zhang
2022-02-15  9:47 ` Jiri Olsa
2022-02-15 17:10 ` patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ab1e74e8-ce5a-44b8-47cb-3f326973bb59@fb.com \
    --to=yhs@fb.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@redhat.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=niklas.soderlund@corigine.com \
    --cc=simon.horman@corigine.com \
    --cc=songliubraving@fb.com \
    --cc=yinjun.zhang@corigine.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.