BPF List
 help / color / mirror / Atom feed
From: Kui-Feng Lee <sinquersw@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	Kui-Feng Lee <thinker.li@gmail.com>
Cc: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev,
	song@kernel.org, kernel-team@meta.com, andrii@kernel.org,
	kuifeng@meta.com
Subject: Re: [PATCH bpf-next 2/2] selftests/bpf: Ensure libbpf skip all-zeros fields of struct_ops maps.
Date: Tue, 12 Mar 2024 17:56:28 -0700	[thread overview]
Message-ID: <cc07c8ad-d07c-4314-8f4f-ec1fe913b005@gmail.com> (raw)
In-Reply-To: <CAEf4BzYyDV5MAjB+0Jj64Fr6AqPg+JS_mHc2rGNovFPAHjxM2Q@mail.gmail.com>



On 3/12/24 16:10, Andrii Nakryiko wrote:
> On Tue, Mar 12, 2024 at 11:32 AM Kui-Feng Lee <thinker.li@gmail.com> wrote:
>>
>> A new version of a type may have additional fields that do not exist in
>> older versions. Previously, libbpf would reject struct_ops maps with a new
>> version containing extra fields when running on a machine with an old
>> kernel. However, we have updated libbpf to ignore these fields if their
>> values are all zeros or null in order to provide backward compatibility.
>>
>> Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
>> ---
>>   .../bpf/prog_tests/test_struct_ops_module.c   | 35 +++++++++++++++++++
>>   .../selftests/bpf/progs/struct_ops_module.c   | 13 +++++++
>>   2 files changed, 48 insertions(+)
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c
>> index ee5372c7f2c7..e0d9ff75121b 100644
>> --- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c
>> @@ -93,9 +93,44 @@ static void test_struct_ops_load(void)
>>          struct_ops_module__destroy(skel);
>>   }
>>
>> +static void test_struct_ops_not_zeroed(void)
>> +{
>> +       struct struct_ops_module *skel;
>> +       int err;
>> +
>> +       skel = struct_ops_module__open();
>> +       if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
>> +               return;
>> +
>> +       bpf_program__set_autoload(skel->progs.test_3, false);
> 
> maybe mark test_3 program in progs/struct_ops_module.c as default
> not-autoload (SEC("?struct_ops/test_3")), so you don't have to set
> this to false everywhere?

Sure! I forgot we have this new feature.

> 
>> +
>> +       err = struct_ops_module__load(skel);
>> +       struct_ops_module__destroy(skel);
>> +
>> +       if (!ASSERT_OK(err, "struct_ops_module_load"))
>> +               return;
>> +
>> +       skel = struct_ops_module__open();
>> +       if (!ASSERT_OK_PTR(skel, "struct_ops_module_open_not_zeroed"))
>> +               return;
>> +
>> +       bpf_program__set_autoload(skel->progs.test_3, false);
>> +
>> +       /* libbpf should reject the testmod_zeroed since the value of its
>> +        * "zeroed" is non-zero.
>> +        */
>> +       skel->struct_ops.testmod_zeroed->zeroed = 0xdeadbeef;
>> +       err = struct_ops_module__load(skel);
>> +       ASSERT_ERR(err, "struct_ops_module_load_not_zeroed");
>> +
>> +       struct_ops_module__destroy(skel);
>> +}
>> +
>>   void serial_test_struct_ops_module(void)
>>   {
>>          if (test__start_subtest("test_struct_ops_load"))
>>                  test_struct_ops_load();
>> +       if (test__start_subtest("test_struct_ops_not_zeroed"))
>> +               test_struct_ops_not_zeroed();
>>   }
>>
>> diff --git a/tools/testing/selftests/bpf/progs/struct_ops_module.c b/tools/testing/selftests/bpf/progs/struct_ops_module.c
>> index 026cabfa7f1f..d7d7606f639c 100644
>> --- a/tools/testing/selftests/bpf/progs/struct_ops_module.c
>> +++ b/tools/testing/selftests/bpf/progs/struct_ops_module.c
>> @@ -54,3 +54,16 @@ struct bpf_testmod_ops___v2 testmod_2 = {
>>          .test_1 = (void *)test_1,
>>          .test_2 = (void *)test_2_v2,
>>   };
>> +
>> +struct bpf_testmod_ops___zeroed {
>> +       int (*test_1)(void);
>> +       void (*test_2)(int a, int b);
>> +       int (*test_maybe_null)(int dummy, struct task_struct *task);
>> +       int zeroed;
>> +};
>> +
>> +SEC(".struct_ops.link")
>> +struct bpf_testmod_ops___zeroed testmod_zeroed = {
>> +       .test_1 = (void *)test_1,
>> +       .test_2 = (void *)test_2_v2,
>> +};
> 
> We should also test the case where we have local ops definition with
> incompatible callback function signature (e.g., extra argument or
> something). Test then would update the program pointer (through
> skeleton's shadow type) to a compatible implementation.
> 
> Can you please add a test like that? Because the goal is to have a
> single struct_ops definition, if possible, and adjust it at runtime to
> make it compatible with the old kernel, let's have a small demo of
> this working.

Do you want to check signatures at libbpf? Or you just want a small
demo?

For extra arguments, IIRC, the verifier should reject the program if the 
program did access these arguments since it accesses memory beyond the 
last byte of the context. Doing extra checking at libbpf can provide 
better error messages if that is what you want.

If a program never accesses an extra argument, it should be allowed.(?)
WDYT?


> 
>> --
>> 2.34.1
>>

  reply	other threads:[~2024-03-13  0:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-12 18:32 [PATCH bpf-next 0/2] Ignore additional fields in the struct_ops maps in an updated version Kui-Feng Lee
2024-03-12 18:32 ` [PATCH bpf-next 1/2] libbpf: Skip zeroed or null fields if not found in the kernel type Kui-Feng Lee
2024-03-12 23:01   ` Andrii Nakryiko
2024-03-13  0:22     ` Kui-Feng Lee
2024-03-12 18:32 ` [PATCH bpf-next 2/2] selftests/bpf: Ensure libbpf skip all-zeros fields of struct_ops maps Kui-Feng Lee
2024-03-12 23:10   ` Andrii Nakryiko
2024-03-13  0:56     ` Kui-Feng Lee [this message]
2024-03-13 15:52       ` Andrii Nakryiko

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=cc07c8ad-d07c-4314-8f4f-ec1fe913b005@gmail.com \
    --to=sinquersw@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=kernel-team@meta.com \
    --cc=kuifeng@meta.com \
    --cc=martin.lau@linux.dev \
    --cc=song@kernel.org \
    --cc=thinker.li@gmail.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