bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Lorenz Bauer <lmb@cloudflare.com>
Cc: Andrii Nakryiko <andriin@fb.com>, bpf <bpf@vger.kernel.org>
Subject: Re: BTF CO-RE bitfield relocation: why is the load size rounded?
Date: Fri, 2 Oct 2020 11:41:59 -0700	[thread overview]
Message-ID: <CAEf4BzZst8uF-zf_o-mJvo53UG6WgTXHWyz5NgFFO1DbK22Khw@mail.gmail.com> (raw)
In-Reply-To: <CACAyw9_R4_ib0KvcuQC4nSOy5+Hn8-Xq-G8geDdLsNztX=0Fsw@mail.gmail.com>

On Fri, Oct 2, 2020 at 4:25 AM Lorenz Bauer <lmb@cloudflare.com> wrote:
>
> Hi Andrii,
>
> I'm trying to understand the following code from
> bpf_core_calc_field_relo in libbpf.c:
>
>     if (bitfield) {
>         byte_sz = mt->size;
>         byte_off = bit_off / 8 / byte_sz * byte_sz;
>         /* figure out smallest int size necessary for bitfield load */
>         while (bit_off + bit_sz - byte_off * 8 > byte_sz * 8) {
>             if (byte_sz >= 8) {
>                 ...
>                 return -E2BIG;
>             }
>             byte_sz *= 2;
>             byte_off = bit_off / 8 / byte_sz * byte_sz;
>         }
>     }
>
> It's used to calculate the load size (byte_sz) and load offset
> (byte_off) for a bitfield member of a struct. Can you explain to me
> why byte_off is rounded to byte_sz? Is it to preserve alignment?
>
> It seems like the rounding can lead to reading past the end of a
> struct. An example:
>
>     struct foo {
>         unsigned short boo:4;
>     } __attribute__((packed));
>
>     [2] STRUCT 'foo' size=1 vlen=1
>         'boo' type_id=3 bits_offset=0 bitfield_size=4
>     [3] INT 'unsigned short' size=2 bits_offset=0 nr_bits=16 encoding=(none)
>
> The result of the calculation for this is byte_sz = 2 and byte_off =
> 0, but the structure is only 1 byte in length.
>
> Would it be possible to replace the calculation with the following?
>
>     byte_off = bit_off / 8
>     byte_sz = ((bit_off + bit_sz + 7) - byte_off*8) / 8

Yes, it's for alignment. See BPF_CORE_READ_BITFIELD(), which reads the
underlying integer directly, which means that it has to be a
naturally-aligned memory read. Yes, it technically could be a problem
that we'll read 2 bytes in this case, but I don't think the kernel is
using that many packed structs and bitfields of interest are in the
very last byte of a packed struct... Packed structs are special and
annoying, but luckily not all that popular.

In this particular case reading one byte would be enough, so we could
change the logic to always start with a byte and see what's the
smallest int type we can get away with, ignoring what's the underlying
integer type (unsigned short in this case) was declared. But I'm not
sure that's a real problem worth solving.

But first, do you see any real issues in production with the current logic?

>
> Thanks
> Lorenz
>
> --
> Lorenz Bauer  |  Systems Engineer
> 6th Floor, County Hall/The Riverside Building, SE1 7PB, UK
>
> www.cloudflare.com

      reply	other threads:[~2020-10-02 18:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-02 11:24 BTF CO-RE bitfield relocation: why is the load size rounded? Lorenz Bauer
2020-10-02 18:41 ` Andrii Nakryiko [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=CAEf4BzZst8uF-zf_o-mJvo53UG6WgTXHWyz5NgFFO1DbK22Khw@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=andriin@fb.com \
    --cc=bpf@vger.kernel.org \
    --cc=lmb@cloudflare.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).