linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Feng Zhou <zhoufeng.zf@bytedance.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: 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>, Yonghong Song <yhs@fb.com>,
	john fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ingo Molnar <mingo@redhat.com>, Jiri Olsa <jolsa@kernel.org>,
	Dave Marchevsky <davemarchevsky@fb.com>,
	Joanne Koong <joannekoong@fb.com>,
	Geliang Tang <geliang.tang@suse.com>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	open list <linux-kernel@vger.kernel.org>,
	duanxiongchun@bytedance.com,
	Muchun Song <songmuchun@bytedance.com>,
	Dongdong Wang <wangdongdong.6@bytedance.com>,
	Cong Wang <cong.wang@bytedance.com>,
	zhouchengming@bytedance.com, yosryahmed@google.com
Subject: Re: [External] Re: [PATCH bpf-next v2 2/2] selftests/bpf: add test case for bpf_map_lookup_percpu_elem
Date: Thu, 12 May 2022 11:58:38 +0800	[thread overview]
Message-ID: <731c281a-9911-fa86-fec2-a3c1a3954461@bytedance.com> (raw)
In-Reply-To: <CAEf4BzZL85C7KUwKv9i5cdLSDzM175cLjiW4EDjOqNfcxbLO+A@mail.gmail.com>

在 2022/5/12 上午11:34, Andrii Nakryiko 写道:
> On Wed, May 11, 2022 at 2:39 AM Feng zhou <zhoufeng.zf@bytedance.com> wrote:
>> From: Feng Zhou <zhoufeng.zf@bytedance.com>
>>
>> test_progs:
>> Tests new ebpf helpers bpf_map_lookup_percpu_elem.
>>
>> Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
>> ---
>>   .../bpf/prog_tests/map_lookup_percpu_elem.c   | 46 ++++++++++++++++
>>   .../bpf/progs/test_map_lookup_percpu_elem.c   | 54 +++++++++++++++++++
>>   2 files changed, 100 insertions(+)
>>   create mode 100644 tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c
>>   create mode 100644 tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c b/tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c
>> new file mode 100644
>> index 000000000000..58b24c2112b0
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c
>> @@ -0,0 +1,46 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +// Copyright (c) 2022 Bytedance
> /* */ instead of //

Ok, I will do. Thanks.


>
>> +
>> +#include <test_progs.h>
>> +
>> +#include "test_map_lookup_percpu_elem.skel.h"
>> +
>> +#define TEST_VALUE  1
>> +
>> +void test_map_lookup_percpu_elem(void)
>> +{
>> +       struct test_map_lookup_percpu_elem *skel;
>> +       int key = 0, ret;
>> +       int nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
> I think this is actually wrong and will break selftests on systems
> with offline CPUs. Please use libbpf_num_possible_cpus() instead.


Ok, I will do. Thanks.


>
>> +       int *buf;
>> +
>> +       buf = (int *)malloc(nr_cpus*sizeof(int));
>> +       if (!ASSERT_OK_PTR(buf, "malloc"))
>> +               return;
>> +       memset(buf, 0, nr_cpus*sizeof(int));
> this is wrong, kernel expects to have roundup(sz, 8) per each CPU,
> while you have just 4 bytes per each element
>
> please also have spaces around multiplication operator here and above


Ok, I will use 8 bytes for key and val. Thanks.


>> +       buf[0] = TEST_VALUE;
>> +
>> +       skel = test_map_lookup_percpu_elem__open_and_load();
>> +       if (!ASSERT_OK_PTR(skel, "test_map_lookup_percpu_elem__open_and_load"))
>> +               return;
> buf leaking here


Yes, sorry for my negligence.


>
>> +       ret = test_map_lookup_percpu_elem__attach(skel);
>> +       ASSERT_OK(ret, "test_map_lookup_percpu_elem__attach");
>> +
>> +       ret = bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_array_map), &key, buf, 0);
>> +       ASSERT_OK(ret, "percpu_array_map update");
>> +
>> +       ret = bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_hash_map), &key, buf, 0);
>> +       ASSERT_OK(ret, "percpu_hash_map update");
>> +
>> +       ret = bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_lru_hash_map), &key, buf, 0);
>> +       ASSERT_OK(ret, "percpu_lru_hash_map update");
>> +
>> +       syscall(__NR_getuid);
>> +
>> +       ret = skel->bss->percpu_array_elem_val == TEST_VALUE &&
>> +             skel->bss->percpu_hash_elem_val == TEST_VALUE &&
>> +             skel->bss->percpu_lru_hash_elem_val == TEST_VALUE;
>> +       ASSERT_OK(!ret, "bpf_map_lookup_percpu_elem success");
> this would be better done as three separate ASSERT_EQ(), combining
> into opaque true/false isn't helpful if something breaks


Good suggestion.


>
>> +
>> +       test_map_lookup_percpu_elem__destroy(skel);
>> +}
>> diff --git a/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c b/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c
>> new file mode 100644
>> index 000000000000..5d4ef86cbf48
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c
>> @@ -0,0 +1,54 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +// Copyright (c) 2022 Bytedance
> /* */ instead of //


Ok, I will do. Thanks.


>
>> +
>> +#include "vmlinux.h"
>> +#include <bpf/bpf_helpers.h>
>> +
>> +int percpu_array_elem_val = 0;
>> +int percpu_hash_elem_val = 0;
>> +int percpu_lru_hash_elem_val = 0;
>> +
>> +struct {
>> +       __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
>> +       __uint(max_entries, 1);
>> +       __type(key, __u32);
>> +       __type(value, __u32);
>> +} percpu_array_map SEC(".maps");
>> +
>> +struct {
>> +       __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
>> +       __uint(max_entries, 1);
>> +       __type(key, __u32);
>> +       __type(value, __u32);
>> +} percpu_hash_map SEC(".maps");
>> +
>> +struct {
>> +       __uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH);
>> +       __uint(max_entries, 1);
>> +       __type(key, __u32);
>> +       __type(value, __u32);
>> +} percpu_lru_hash_map SEC(".maps");
>> +
>> +SEC("tp/syscalls/sys_enter_getuid")
>> +int sysenter_getuid(const void *ctx)
>> +{
>> +       __u32 key = 0;
>> +       __u32 cpu = 0;
>> +       __u32 *value;
>> +
>> +       value = bpf_map_lookup_percpu_elem(&percpu_array_map, &key, cpu);
>> +       if (value)
>> +               percpu_array_elem_val = *value;
>> +
>> +       value = bpf_map_lookup_percpu_elem(&percpu_hash_map, &key, cpu);
>> +       if (value)
>> +               percpu_hash_elem_val = *value;
>> +
>> +       value = bpf_map_lookup_percpu_elem(&percpu_lru_hash_map, &key, cpu);
>> +       if (value)
>> +               percpu_lru_hash_elem_val = *value;
>> +
> if the test happens to run on CPU 0 then the test doesn't really test
> much. It would be more interesting to have a bpf_loop() iteration that
> would fetch values on each possible CPU instead and do something with
> it.


Good suggestion. I check the code and find no bpf helper function to get 
possible CPU nums.

I think for the test function, read cpu0 elem value correctly should be 
considered to be no problem.

Or is it necessary to add a new helper function to get num_possible_cpus ?


>
>> +       return 0;
>> +}
>> +
>> +char _license[] SEC("license") = "GPL";
>> --
>> 2.20.1
>>


  reply	other threads:[~2022-05-12  3:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-11  9:38 [PATCH bpf-next v2 0/2] Introduce access remote cpu elem support in BPF percpu map Feng zhou
2022-05-11  9:38 ` [PATCH bpf-next v2 1/2] bpf: add bpf_map_lookup_percpu_elem for " Feng zhou
2022-05-11  9:38 ` [PATCH bpf-next v2 2/2] selftests/bpf: add test case for bpf_map_lookup_percpu_elem Feng zhou
2022-05-12  3:34   ` Andrii Nakryiko
2022-05-12  3:58     ` Feng Zhou [this message]
2022-05-12 16:43       ` [External] " Andrii Nakryiko
2022-05-13  1:49         ` Feng Zhou
2022-05-12  1:30 ` [PATCH bpf-next v2 0/2] Introduce access remote cpu elem support in BPF percpu map 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=731c281a-9911-fa86-fec2-a3c1a3954461@bytedance.com \
    --to=zhoufeng.zf@bytedance.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=cong.wang@bytedance.com \
    --cc=daniel@iogearbox.net \
    --cc=davemarchevsky@fb.com \
    --cc=duanxiongchun@bytedance.com \
    --cc=geliang.tang@suse.com \
    --cc=joannekoong@fb.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=songliubraving@fb.com \
    --cc=songmuchun@bytedance.com \
    --cc=wangdongdong.6@bytedance.com \
    --cc=yhs@fb.com \
    --cc=yosryahmed@google.com \
    --cc=zhouchengming@bytedance.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 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).