BPF List
 help / color / mirror / Atom feed
From: "Jose E. Marchesi" <jose.marchesi@oracle.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Eduard Zingerman <eddyz87@gmail.com>,
	bpf@vger.kernel.org,
	Alexei Starovoitov <alexei.starovoitov@gmail.com>,
	Yonghong Song <yonghong.song@linux.dev>,
	david.faust@oracle.com, cupertino.miranda@oracle.com
Subject: Re: [RFC bpf-next] bpf: avoid clang-specific push/pop attribute pragmas in bpftool
Date: Sat, 04 May 2024 23:09:23 +0200	[thread overview]
Message-ID: <87a5l5jncs.fsf@oracle.com> (raw)
In-Reply-To: <CAEf4Bza5cmJK-+tK1QJ-SVUWmTOTOM_3gZQ=9yhynU5vE_wWyg@mail.gmail.com> (Andrii Nakryiko's message of "Fri, 3 May 2024 15:14:14 -0700")


> On Fri, May 3, 2024 at 2:18 PM Eduard Zingerman <eddyz87@gmail.com> wrote:
>>
>> On Fri, 2024-05-03 at 13:36 -0700, Eduard Zingerman wrote:
>> > On Fri, 2024-05-03 at 13:18 +0200, Jose E. Marchesi wrote:
>> > [...]
>> >
>> > > This patch modifies bpftool in order to, instead of using the pragmas,
>> > > define ATTR_PRESERVE_ACCESS_INDEX to conditionally expand to the CO-RE
>> > > attribute:
>> > >
>> > >   #ifndef __VMLINUX_H__
>> > >   #define __VMLINUX_H__
>> > >
>> > >   #ifndef BPF_NO_PRESERVE_ACCESS_INDEX
>> > >   #define ATTR_PRESERVE_ACCESS_INDEX __attribute__((preserve_access_index))
>> > >   #else
>> > >   #define ATTR_PRESERVE_ACCESS_INDEX
>> > >   #endif
>> >
>> > Nit: maybe swap the branches to avoid double negation?
>> >
>> > >
>> > >   [... type definitions generated from kernel BTF ... ]
>> > >
>> > >   #undef ATTR_PRESERVE_ACCESS_INDEX
>> > >
>> > > and then the new btf_dump__dump_type_with_opts is used with options
>> > > specifying that we wish to have struct type attributes:
>> > >
>> > >   DECLARE_LIBBPF_OPTS(btf_dump_type_opts, opts);
>> > >   [...]
>> > >   opts.record_attrs_str = "ATTR_PRESERVE_ACCESS_INDEX";
>> > >   [...]
>> > >   err = btf_dump__dump_type_with_opts(d, root_type_ids[i], &opts);
>> > >
>> > > This is a RFC because introducing a new libbpf public function
>> > > btf_dump__dump_type_with_opts may not be desirable.
>> > >
>> > > An alternative could be to, instead of passing the record_attrs_str
>> > > option in a btf_dump_type_opts, pass it in the global dumper's option
>> > > btf_dump_opts:
>> > >
>> > >   DECLARE_LIBBPF_OPTS(btf_dump_opts, opts);
>> > >   [...]
>> > >   opts.record_attrs_str = "ATTR_PRESERVE_ACCESS_INDEX";
>> > >   [...]
>> > >   d = btf_dump__new(btf, btf_dump_printf, NULL, &opts);
>> > >   [...]
>> > >   err = btf_dump__dump_type(d, root_type_ids[i]);
>> > >
>> > > This would be less disruptive regarding library API, and an overall
>> > > simpler change.  But it would prevent to use the same btf dumper to
>> > > dump types with and without attribute definitions.  Not sure if that
>> > > matters much in practice.
>> > >
>> > > Thoughts?
>> >
>> > I think that generating attributes explicitly is fine.
>> >
>> > I also think that moving '.record_attrs_str' to 'btf_dump_opts' is preferable,
>> > in order to avoid adding new API functions.
>>
>> On more argument for making it a part of btf_dump_opts is that
>> btf_dump__dump_type() walks the chain of dependent types,
>> so attribute placement control is not per-type anyways.
>
> And that's very unfortunate, which makes this not a good option, IMO.

Indeed.

But for the specific case case of preserve_access_info and vmlinux.h,
having the attribute applied to all types (both directly referred and
indirectly dependent) is actually what is required, isn't it?  That is
what the current implicity push-attribute clang pragma does.

I have sent a tentative patch that adds the `record_attrs_str'
configuration parameter to the btf_dump_opts, incorporating a few
changes after Eduard's suggestions regarding avoiding double negations
and docstrings.

>> I also remembered my stalled attempt to emit preserve_static_offset
>> attribute for certain types [1] (need to finish with it).
>> There I needed to attach attributes to a dozen specific types.
>>
>> [1] https://lore.kernel.org/bpf/20231220133411.22978-3-eddyz87@gmail.com/
>>
>> So, I think that it would be better if '.record_attrs_str' would be a
>> callback accepting the name of the type and it's kind. Wdyt?
>
> I think if we are talking about the current API, then extending it
> with some pre/post type callback would solve this specific problem
> (but even then it feels dirty, because of "this callback is called
> after } but before ," sadness). I really dislike callbacks as part of
> public APIs like this. It feels like the user has to have control and
> the library should provide building blocks.
>
> So how about an alternative view on this problem. What if we add an
> API that will sort types in "C type system" order, i.e., it will
> return a sequence of BTF type ID + a flag whether it's a full BTF type
> definition or forward declaration only for that type. And then,
> separately, instead of btf_dump__dump_type() API that emits type *and*
> all its dependencies (unless they were already emitted, it's very
> stateful), we'll have an analogous API that will emit a full
> definition of one isolated btf_type (and no dependencies).
>
> The user will need to add semicolons after each type (and empty lines
> and stuff like that, probably), but they will also have control over
> appending/prepending any extra attributes and whatnot (or #ifdef
> guards).
>
> Also, when we have this API, we'll have all the necessary building
> blocks to finally be able to emit only types for module BTF without
> duplicated types from vmlinux.h (under assumption that vmlinux.h will
> be included before that). Libbpf will return fully sorted type order,
> including vmlinux BTF types, but bpftool (or whoever is using this
> API) will be smart in ignoring those types and/or emitting just
> forward declaration for them.
>
> With the decomposition into sort + emit string representation, it's
> now trivial to use in this flexible way.
>
> Thoughts?

I am not familiar with the particular use cases, but generally speaking
separating sorting and emission makes sense to me.  I would also prefer
that to iterators.

>
>
>>
>> [...]

  reply	other threads:[~2024-05-04 21:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-03 11:18 [RFC bpf-next] bpf: avoid clang-specific push/pop attribute pragmas in bpftool Jose E. Marchesi
2024-05-03 20:36 ` Eduard Zingerman
2024-05-03 21:18   ` Eduard Zingerman
2024-05-03 22:14     ` Andrii Nakryiko
2024-05-04 21:09       ` Jose E. Marchesi [this message]
2024-05-06 18:55         ` Eduard Zingerman
2024-05-06 19:10           ` Jose E. Marchesi
2024-05-06 21:35             ` Andrii Nakryiko
2024-05-06  6:26       ` Eduard Zingerman

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=87a5l5jncs.fsf@oracle.com \
    --to=jose.marchesi@oracle.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=cupertino.miranda@oracle.com \
    --cc=david.faust@oracle.com \
    --cc=eddyz87@gmail.com \
    --cc=yonghong.song@linux.dev \
    /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