BPF List
 help / color / mirror / Atom feed
From: Hou Tao <houtao1@huawei.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Yonghong Song <yhs@fb.com>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf-next 5/5] selftests/bpf: add test cases for bpf_strncmp()
Date: Wed, 8 Dec 2021 21:50:45 +0800	[thread overview]
Message-ID: <f39b4017-8f7e-474c-6497-7f6448c44911@huawei.com> (raw)
In-Reply-To: <CAEf4BzaZR84VXUSh-SkA32yTYXhz5vUxK7ysoGMgWsa0+d54vQ@mail.gmail.com>

Hi,
On 12/7/2021 11:09 AM, Andrii Nakryiko wrote:
>> +static struct strncmp_test *strncmp_test_open_and_disable_autoload(void)
>> +{
>> +       struct strncmp_test *skel;
>> +       struct bpf_program *prog;
>> +
>> +       skel = strncmp_test__open();
>> +       if (libbpf_get_error(skel))
>> +               return skel;
>> +
>> +       bpf_object__for_each_program(prog, skel->obj)
>> +               bpf_program__set_autoload(prog, false);
> I think this is a wrong "code economy". You save few lines of code,
> but make tests harder to follow. Just do 4 lines of code for each
> subtest:
>
> skel = strncmp_test__open();
> if (!ASSERT_OK_PTR(skel, "skel_open"))
>     return;
>
> bpf_object__for_each_program(prog, skel->obj)
>     bpf_program__set_autoload(prog, false);
>
>
> It makes tests more self-contained and easier to follow. Also if some
> tests need to do something slightly different it's easier to modify
> them, as they are not coupled to some common helper. DRY is good where
> it makes sense, but it also increases code coupling and more "jumping
> around" in code, so it shouldn't be applied blindly.
Thanks for your suggestion on DRY topic. Will do in v2.
> +
> +static int trigger_strncmp(const struct strncmp_test *skel)
> +{
> +       struct timespec wait = {.tv_sec = 0, .tv_nsec = 1};
> +
> +       nanosleep(&wait, NULL);
> all the other tests are just doing usleep(1), why using this more verbose way?
Will do in v2.
>> +
>> +static __always_inline bool called_by_target_pid(void)
>> +{
>> +       __u32 pid = bpf_get_current_pid_tgid() >> 32;
>> +
>> +       return pid == target_pid;
>> +}
> again, what's the point of this helper? it's used once and you'd
> actually save the code by doing the following inline:
>
> if ((bpf_get_current_pid_tgid() >> 32) != target_pid)
>     return 0;
Will do in v2.
>> +
>> +SEC("tp/syscalls/sys_enter_nanosleep")
>> +int do_strncmp(void *ctx)
>> +{
>> +       if (!called_by_target_pid())
>> +               return 0;
>> +
>> +       cmp_ret = bpf_strncmp(str, STRNCMP_STR_SZ, target);
>> +
>> +       return 0;
>> +}
>> +
>> +SEC("tp/syscalls/sys_enter_nanosleep")
>> +int strncmp_bad_not_const_str_size(void *ctx)
>> +{
> probably worth leaving a short comment explaining that this program
> should fail because ...
OK. Will do in v2.

Regards,
Tao

  reply	other threads:[~2021-12-08 13:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-30 14:22 [PATCH bpf-next 0/5] introduce bpf_strncmp() helper Hou Tao
2021-11-30 14:22 ` [PATCH bpf-next 1/5] bpf: add bpf_strncmp helper Hou Tao
2021-11-30 14:22 ` [PATCH bpf-next 2/5] selftests/bpf: fix checkpatch error on empty function parameter Hou Tao
2021-11-30 14:22 ` [PATCH bpf-next 3/5] selftests/bpf: factor out common helpers for benchmarks Hou Tao
2021-12-07  2:55   ` Andrii Nakryiko
2021-12-08 13:41     ` Hou Tao
2021-11-30 14:22 ` [PATCH bpf-next 4/5] selftests/bpf: add benchmark for bpf_strncmp() helper Hou Tao
2021-12-07  3:01   ` Andrii Nakryiko
2021-12-08 13:47     ` Hou Tao
2021-12-08 20:08       ` Andrii Nakryiko
2021-11-30 14:22 ` [PATCH bpf-next 5/5] selftests/bpf: add test cases for bpf_strncmp() Hou Tao
2021-12-07  3:09   ` Andrii Nakryiko
2021-12-08 13:50     ` Hou Tao [this message]
2021-12-03  2:09 ` [PATCH bpf-next 0/5] introduce bpf_strncmp() helper Alexei Starovoitov

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=f39b4017-8f7e-474c-6497-7f6448c44911@huawei.com \
    --to=houtao1@huawei.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kafai@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=yhs@fb.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