bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Florent Revest <revest@chromium.org>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	KP Singh <kpsingh@kernel.org>,
	Brendan Jackman <jackmanb@google.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Steven Rostedt <rostedt@goodmis.org>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf-next v2 2/2] bpf: Implement formatted output helpers with bstr_printf
Date: Wed, 28 Apr 2021 16:52:02 +0200	[thread overview]
Message-ID: <CABRcYmKb0Nft+whG0nv3QkAQszpctdpyXxN=uo2LJdEnzeEZaA@mail.gmail.com> (raw)
In-Reply-To: <CAADnVQK=K2hcrZ7=d=voQ=gxdmC_oqSWodLpck54UncSSgsLuQ@mail.gmail.com>

On Wed, Apr 28, 2021 at 2:51 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Tue, Apr 27, 2021 at 5:20 PM Florent Revest <revest@chromium.org> wrote:
> >
> > On Wed, Apr 28, 2021 at 1:46 AM Alexei Starovoitov
> > <alexei.starovoitov@gmail.com> wrote:
> > >
> > > On Tue, Apr 27, 2021 at 10:43 AM Florent Revest <revest@chromium.org> wrote:
> > > > +                       if (fmt[i + 1] == 'B') {
> > > > +                               if (tmp_buf)  {
> > > > +                                       err = snprintf(tmp_buf,
> > > > +                                                      (tmp_buf_end - tmp_buf),
> > > > +                                                      "%pB",
> > > ...
> > > > +                       if ((tmp_buf_end - tmp_buf) < sizeof_cur_ip) {
> > >
> > > I removed a few redundant () like above
> >
> > Oh, sorry about that.
> >
> > > and applied.
> >
> > Nice! :)
> >
> > > >                 if (fmt[i] == 'l') {
> > > > -                       cur_mod = BPF_PRINTF_LONG;
> > > > +                       sizeof_cur_arg = sizeof(long);
> > > >                         i++;
> > > >                 }
> > > >                 if (fmt[i] == 'l') {
> > > > -                       cur_mod = BPF_PRINTF_LONG_LONG;
> > > > +                       sizeof_cur_arg = sizeof(long long);
> > > >                         i++;
> > > >                 }
> > >
> > > This bit got me thinking.
> > > I understand that this is how bpf_trace_printk behaved
> > > and the sprintf continued the tradition, but I think it will
> > > surprise bpf users.
> > > The bpf progs are always 64-bit. The sizeof(long) == 8
> > > inside any bpf program. So printf("%ld") matches that long.
> >
> > Yes, this also surprised me.
> >
> > > The clang could even do type checking to make sure the prog
> > > is passing the right type into printf() if we add
> > > __attribute__ ((format (printf))) to bpf_helper_defs.h
> > > But this sprintf() implementation will trim the value to 32-bit
> > > to satisfy 'fmt' string on 32-bit archs.
> > > So bpf program behavior would be different on 32 and 64-bit archs.
> > > I think that would be confusing, since the rest of bpf prog is
> > > portable. The progs work the same way on all archs
> > > (except endianess, of course).
> > > I'm not sure how to fix it though.
> > > The sprintf cannot just pass 64-bit unconditionally, since
> > > bstr_printf on 32-bit archs will process %ld incorrectly.
> > > The verifier could replace %ld with %Ld.
> > > The fmt string is a read only string for bpf_snprintf,
> > > but for bpf_trace_printk it's not and messing with it at run-time
> > > is not good. Copying the fmt string is not great either.
> > > Messing with internals of bstr_printf is ugly too.
> >
> > Indeed, none of these solutions are satisfying.
>
> Maybe Daniel has other ideas?
>
> > > Maybe we just have to live with this quirk ?
> >
> > If we were starting from scratch, maybe just banning %ld could have
> > been an option, but now that bpf_trace_printk has been behaving like
> > this for a while, I think it might be best to just keep the behavior
> > as it is.
> >
> > > Just add a doc to uapi/bpf.h to discourage %ld and be done?
> >
> > More doc is always good. Something like "Note: %ld behaves differently
> > depending on the host architecture, it is recommended to avoid it and
> > use %d or %lld instead" in the helper description of the three
> > helpers? If you don't have the time to do it today, I can send a patch
> > tomorrow.
>
> bpf_trace_printk was like this for a long time, so there is no rush.
> Pls wait until everything comes back to bpf tree and send a patch against it.
> bpf_trace_printk comment in uapi/bpf.h is outdated too. Would be good
> to document the latest behavior for them all.

Ok :)

      reply	other threads:[~2021-04-28 14:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-27 17:43 [PATCH bpf-next v2 0/2] Implement formatted output helpers with bstr_printf Florent Revest
2021-04-27 17:43 ` [PATCH bpf-next v2 1/2] seq_file: Add a seq_bprintf function Florent Revest
2021-04-27 17:43 ` [PATCH bpf-next v2 2/2] bpf: Implement formatted output helpers with bstr_printf Florent Revest
2021-04-27 23:46   ` Alexei Starovoitov
2021-04-28  0:20     ` Florent Revest
2021-04-28  0:51       ` Alexei Starovoitov
2021-04-28 14:52         ` Florent Revest [this message]

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='CABRcYmKb0Nft+whG0nv3QkAQszpctdpyXxN=uo2LJdEnzeEZaA@mail.gmail.com' \
    --to=revest@chromium.org \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=jackmanb@google.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=rostedt@goodmis.org \
    /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).