Message ID | 8eaba4b675cba9035121121bba6618c9f8f65610.1724976539.git.tony.ambardar@gmail.com (mailing list archive) |
---|---|
State | Superseded |
Delegated to: | BPF |
Headers | show |
Series | libbpf, selftests/bpf: Support cross-endian usage | expand |
On Fri, Aug 30, 2024 at 12:30 AM Tony Ambardar <tony.ambardar@gmail.com> wrote: > > Support for handling BTF data of either endianness was added in [1], but > did not include BTF.ext data for lack of use cases. Later, support for > static linking [2] provided a use case, but this feature and later ones > were restricted to native-endian usage. > > Add support for BTF.ext handling in either endianness. Convert BTF.ext data > to native endianness when read into memory for further processing, and > support raw data access that restores the original byte-order for output. > Add internal header functions for byte-swapping func, line, and core info > records. > > Add new API functions btf_ext__endianness() and btf_ext__set_endianness() > for query and setting byte-order, as already exist for BTF data. > > [1] 3289959b97ca ("libbpf: Support BTF loading and raw data output in both endianness") > [2] 8fd27bf69b86 ("libbpf: Add BPF static linker BTF and BTF.ext support") > > Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com> > --- > tools/lib/bpf/btf.c | 192 +++++++++++++++++++++++++++++--- > tools/lib/bpf/btf.h | 3 + > tools/lib/bpf/libbpf.map | 2 + > tools/lib/bpf/libbpf_internal.h | 33 ++++++ > 4 files changed, 214 insertions(+), 16 deletions(-) > > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c > index f5081de86ee0..064cfe126c09 100644 > --- a/tools/lib/bpf/btf.c > +++ b/tools/lib/bpf/btf.c > @@ -3022,25 +3022,102 @@ static int btf_ext_setup_core_relos(struct btf_ext *btf_ext) > return btf_ext_setup_info(btf_ext, ¶m); > } > > -static int btf_ext_parse_hdr(__u8 *data, __u32 data_size) > +/* Swap byte-order of BTF.ext header with any endianness */ > +static void btf_ext_bswap_hdr(struct btf_ext *btf_ext, __u32 hdr_len) > { > - const struct btf_ext_header *hdr = (struct btf_ext_header *)data; > + struct btf_ext_header *h = btf_ext->hdr; > > - if (data_size < offsetofend(struct btf_ext_header, hdr_len) || > - data_size < hdr->hdr_len) { > - pr_debug("BTF.ext header not found\n"); > + h->magic = bswap_16(h->magic); > + h->hdr_len = bswap_32(h->hdr_len); > + h->func_info_off = bswap_32(h->func_info_off); > + h->func_info_len = bswap_32(h->func_info_len); > + h->line_info_off = bswap_32(h->line_info_off); > + h->line_info_len = bswap_32(h->line_info_len); > + > + if (hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) > + return; > + > + h->core_relo_off = bswap_32(h->core_relo_off); > + h->core_relo_len = bswap_32(h->core_relo_len); > +} > + > +/* Swap byte-order of a generic info subsection */ > +static void info_subsec_bswap(const struct btf_ext_header *hdr, bool native, > + __u32 off, __u32 len, anon_info_bswap_fn_t bswap) ok, so I'm not a fan of this bswap callback, tbh. Also, we don't really enforce that each kind of record has exact size we expect (i.e., bpf_line_info_min and bpf_func_info_min shouldn't be "min" for byte-swapped case, it should be exact). How about this slight modification: split byte swapping of sections/subsection metadata, so we adjust record size, sec_name_off and num_info separately from adjusting each record. Once this swapping is done we: a) validate record size for each section is expected (according to its type, of course) b) we can then use for_each_btf_ext_sec() and for_each_btf_ext_rec() macro (which assume proper in-memory metadata byte order) and then hard-code swapping of each record fields No callbacks. This has also a benefit of not needing this annoying "bool native" flag when producing raw bytes. We just ensure proper order of operation: a) swap records b) swap metadata (so just mirrored order from initialization) WDYT? pw-bot: cr > +{ > + __u32 left, i, *rs, rec_size, num_info; > + struct btf_ext_info_sec *si; > + void *p; > + > + if (len == 0) > + return; > + > + rs = (void *)hdr + hdr->hdr_len + off; /* record size */ > + si = (void *)rs + sizeof(__u32); /* sec info #1 */ > + rec_size = native ? *rs : bswap_32(*rs); > + *rs = bswap_32(*rs); > + left = len - sizeof(__u32); > + while (left > 0) { > + num_info = native ? si->num_info : bswap_32(si->num_info); > + si->sec_name_off = bswap_32(si->sec_name_off); > + si->num_info = bswap_32(si->num_info); > + left -= offsetof(struct btf_ext_info_sec, data); > + p = si->data; > + for (i = 0; i < num_info; i++) /* list of records */ > + p += bswap(p); > + si = p; > + left -= rec_size * num_info; nit: extra space here > + } > +} > + [...]
On Fri, 2024-08-30 at 00:29 -0700, Tony Ambardar wrote: [...] > @@ -3050,11 +3127,42 @@ static int btf_ext_parse_hdr(__u8 *data, __u32 data_size) > return -ENOTSUP; > } > > - if (data_size == hdr->hdr_len) { > + if (data_size < hdr_len) { > + pr_debug("BTF.ext header not found\n"); > + return -EINVAL; > + } else if (data_size == hdr_len) { > pr_debug("BTF.ext has no data\n"); > return -EINVAL; > } > > + /* Verify mandatory hdr info details present */ > + if (hdr_len < offsetofend(struct btf_ext_header, line_info_len)) { > + pr_warn("BTF.ext header missing func_info, line_info\n"); > + return -EINVAL; > + } > + > + /* Keep hdr native byte-order in memory for introspection */ > + if (btf_ext->swapped_endian) > + btf_ext_bswap_hdr(btf_ext, hdr_len); > + > + /* Basic info section consistency checks*/ > + info_size = btf_ext->data_size - hdr_len; > + if (info_size & 0x03) { > + pr_warn("BTF.ext info size not 4-byte multiple\n"); > + return -EINVAL; > + } > + info_size -= hdr->func_info_len + hdr->line_info_len; > + if (hdr_len >= offsetofend(struct btf_ext_header, core_relo_len)) > + info_size -= hdr->core_relo_len; nit: Since we are checking this, maybe also check that sections do not overlap? Also, why disallowing gaps between sections? > + if (info_size) { > + pr_warn("BTF.ext info size mismatch with header data\n"); > + return -EINVAL; > + } > + > + /* Keep infos native byte-order in memory for introspection */ > + if (btf_ext->swapped_endian) > + btf_ext_bswap_info(btf_ext, !btf_ext->swapped_endian); > + > return 0; > } [...] > @@ -3119,15 +3223,71 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size) > return btf_ext; > } > > +static void *btf_ext_raw_data(const struct btf_ext *btf_ext_ro, __u32 *size, > + bool swap_endian) > +{ > + struct btf_ext *btf_ext = (struct btf_ext *)btf_ext_ro; > + const __u32 data_sz = btf_ext->data_size; > + void *data; > + > + data = swap_endian ? btf_ext->data_swapped : btf_ext->data; > + if (data) { > + *size = data_sz; > + return data; > + } > + > + data = calloc(1, data_sz); > + if (!data) > + return NULL; > + memcpy(data, btf_ext->data, data_sz); > + > + if (swap_endian) { > + btf_ext_bswap_info(btf_ext, true); > + btf_ext_bswap_hdr(btf_ext, btf_ext->hdr->hdr_len); > + btf_ext->data_swapped = data; > + } Nit: I don't like how this function is organized: - if btf_ext->data can't be NULL swap_endian == true at this point; - if btf_ext->data can be NULL and swap_endian == false pointer to `data` would be lost. I assume that btf_ext->data can't be null, basing on the btf_ext__new(), but function body is a bit confusing. > + > + *size = data_sz; > + return data; > +} > + [...]
On Fri, Aug 30, 2024 at 02:14:19PM -0700, Andrii Nakryiko wrote: > On Fri, Aug 30, 2024 at 12:30 AM Tony Ambardar <tony.ambardar@gmail.com> wrote: > > > > Support for handling BTF data of either endianness was added in [1], but > > did not include BTF.ext data for lack of use cases. Later, support for > > static linking [2] provided a use case, but this feature and later ones > > were restricted to native-endian usage. > > > > Add support for BTF.ext handling in either endianness. Convert BTF.ext data > > to native endianness when read into memory for further processing, and > > support raw data access that restores the original byte-order for output. > > Add internal header functions for byte-swapping func, line, and core info > > records. > > > > Add new API functions btf_ext__endianness() and btf_ext__set_endianness() > > for query and setting byte-order, as already exist for BTF data. > > > > [1] 3289959b97ca ("libbpf: Support BTF loading and raw data output in both endianness") > > [2] 8fd27bf69b86 ("libbpf: Add BPF static linker BTF and BTF.ext support") > > > > Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com> > > --- > > tools/lib/bpf/btf.c | 192 +++++++++++++++++++++++++++++--- > > tools/lib/bpf/btf.h | 3 + > > tools/lib/bpf/libbpf.map | 2 + > > tools/lib/bpf/libbpf_internal.h | 33 ++++++ > > 4 files changed, 214 insertions(+), 16 deletions(-) > > > > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c > > index f5081de86ee0..064cfe126c09 100644 > > --- a/tools/lib/bpf/btf.c > > +++ b/tools/lib/bpf/btf.c > > @@ -3022,25 +3022,102 @@ static int btf_ext_setup_core_relos(struct btf_ext *btf_ext) > > return btf_ext_setup_info(btf_ext, ¶m); > > } > > > > -static int btf_ext_parse_hdr(__u8 *data, __u32 data_size) > > +/* Swap byte-order of BTF.ext header with any endianness */ > > +static void btf_ext_bswap_hdr(struct btf_ext *btf_ext, __u32 hdr_len) > > { > > - const struct btf_ext_header *hdr = (struct btf_ext_header *)data; > > + struct btf_ext_header *h = btf_ext->hdr; > > > > - if (data_size < offsetofend(struct btf_ext_header, hdr_len) || > > - data_size < hdr->hdr_len) { > > - pr_debug("BTF.ext header not found\n"); > > + h->magic = bswap_16(h->magic); > > + h->hdr_len = bswap_32(h->hdr_len); > > + h->func_info_off = bswap_32(h->func_info_off); > > + h->func_info_len = bswap_32(h->func_info_len); > > + h->line_info_off = bswap_32(h->line_info_off); > > + h->line_info_len = bswap_32(h->line_info_len); > > + > > + if (hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) > > + return; > > + > > + h->core_relo_off = bswap_32(h->core_relo_off); > > + h->core_relo_len = bswap_32(h->core_relo_len); > > +} > > + > > +/* Swap byte-order of a generic info subsection */ > > +static void info_subsec_bswap(const struct btf_ext_header *hdr, bool native, > > + __u32 off, __u32 len, anon_info_bswap_fn_t bswap) > > ok, so I'm not a fan of this bswap callback, tbh. Also, we don't > really enforce that each kind of record has exact size we expect > (i.e., bpf_line_info_min and bpf_func_info_min shouldn't be "min" for > byte-swapped case, it should be exact). > > How about this slight modification: split byte swapping of > sections/subsection metadata, so we adjust record size, sec_name_off > and num_info separately from adjusting each record. Hmmm, the bulk of code needed is to parse the metadata, with only 2 lines used to go through records. Splitting per above would add unnecessary duplication it seems, no? > > Once this swapping is done we: > > a) validate record size for each section is expected (according to its > type, of course) This is a good point I overlooked, and needs doing in any case. > b) we can then use for_each_btf_ext_sec() and for_each_btf_ext_rec() > macro (which assume proper in-memory metadata byte order) and then > hard-code swapping of each record fields How easily can we use these macros? Consider the current call chain: btf_ext__new btf_ext_parse btf_ext_bswap_hdr (1) btf_ext_bswap_info (2) btf_ext_setup_func_info btf_ext_setup_line_info btf_ext_setup_core_relos (3) btf_ext__raw_data btf_ext_bswap_info (4) btf_ext_bswap_hdr The macros iterate on 'struct btf_ext_info' instances in 'struct btf_ext' but these are only set up after (3) it seems and unavailable at (2). I suppose they could be used with some sort of kludge but unsure how well they'll work. > > No callbacks. > > This has also a benefit of not needing this annoying "bool native" > flag when producing raw bytes. We just ensure proper order of > operation: > > a) swap records > b) swap metadata (so just mirrored order from initialization) How does that work? If we split up btf_ext_bswap_info(), after (1) btf_ext->swapped_endian is set and btf_ext->hdr->magic is swapped, so at (2) it's not possible to tell the current info data byte order without some hinting. But maybe if we defer setting btf_ext->swapped_endian until after (b) we can drop the "bool native" thanks to symmetry breaking. Let me check. > > WDYT? Adding a record_size check is definitely needed. But I have trouble seeing how splitting bswap of info metadata/records would yield something simpler and cleaner than the callbacks. What if they were passed via a descriptor, as in btf_ext_setup_func_info()? I think I need to play around with this a while and see.. It would also help me if you'd elaborate on the drawbacks you see of using callbacks, given I see then in other parts of libbpf. > > pw-bot: cr > > > +{ > > + __u32 left, i, *rs, rec_size, num_info; > > + struct btf_ext_info_sec *si; > > + void *p; > > + > > + if (len == 0) > > + return; > > + > > + rs = (void *)hdr + hdr->hdr_len + off; /* record size */ > > + si = (void *)rs + sizeof(__u32); /* sec info #1 */ > > + rec_size = native ? *rs : bswap_32(*rs); > > + *rs = bswap_32(*rs); > > + left = len - sizeof(__u32); > > + while (left > 0) { > > + num_info = native ? si->num_info : bswap_32(si->num_info); > > + si->sec_name_off = bswap_32(si->sec_name_off); > > + si->num_info = bswap_32(si->num_info); > > + left -= offsetof(struct btf_ext_info_sec, data); > > + p = si->data; > > + for (i = 0; i < num_info; i++) /* list of records */ > > + p += bswap(p); > > + si = p; > > + left -= rec_size * num_info; > > nit: extra space here Fixed, thanks. > > > + } > > +} > > + > > [...]
On Mon, Sep 2, 2024 at 1:19 AM Tony Ambardar <tony.ambardar@gmail.com> wrote: > > On Fri, Aug 30, 2024 at 02:14:19PM -0700, Andrii Nakryiko wrote: > > On Fri, Aug 30, 2024 at 12:30 AM Tony Ambardar <tony.ambardar@gmail.com> wrote: > > > > > > Support for handling BTF data of either endianness was added in [1], but > > > did not include BTF.ext data for lack of use cases. Later, support for > > > static linking [2] provided a use case, but this feature and later ones > > > were restricted to native-endian usage. > > > > > > Add support for BTF.ext handling in either endianness. Convert BTF.ext data > > > to native endianness when read into memory for further processing, and > > > support raw data access that restores the original byte-order for output. > > > Add internal header functions for byte-swapping func, line, and core info > > > records. > > > > > > Add new API functions btf_ext__endianness() and btf_ext__set_endianness() > > > for query and setting byte-order, as already exist for BTF data. > > > > > > [1] 3289959b97ca ("libbpf: Support BTF loading and raw data output in both endianness") > > > [2] 8fd27bf69b86 ("libbpf: Add BPF static linker BTF and BTF.ext support") > > > > > > Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com> > > > --- > > > tools/lib/bpf/btf.c | 192 +++++++++++++++++++++++++++++--- > > > tools/lib/bpf/btf.h | 3 + > > > tools/lib/bpf/libbpf.map | 2 + > > > tools/lib/bpf/libbpf_internal.h | 33 ++++++ > > > 4 files changed, 214 insertions(+), 16 deletions(-) > > > > > > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c > > > index f5081de86ee0..064cfe126c09 100644 > > > --- a/tools/lib/bpf/btf.c > > > +++ b/tools/lib/bpf/btf.c > > > @@ -3022,25 +3022,102 @@ static int btf_ext_setup_core_relos(struct btf_ext *btf_ext) > > > return btf_ext_setup_info(btf_ext, ¶m); > > > } > > > > > > -static int btf_ext_parse_hdr(__u8 *data, __u32 data_size) > > > +/* Swap byte-order of BTF.ext header with any endianness */ > > > +static void btf_ext_bswap_hdr(struct btf_ext *btf_ext, __u32 hdr_len) > > > { > > > - const struct btf_ext_header *hdr = (struct btf_ext_header *)data; > > > + struct btf_ext_header *h = btf_ext->hdr; > > > > > > - if (data_size < offsetofend(struct btf_ext_header, hdr_len) || > > > - data_size < hdr->hdr_len) { > > > - pr_debug("BTF.ext header not found\n"); > > > + h->magic = bswap_16(h->magic); > > > + h->hdr_len = bswap_32(h->hdr_len); > > > + h->func_info_off = bswap_32(h->func_info_off); > > > + h->func_info_len = bswap_32(h->func_info_len); > > > + h->line_info_off = bswap_32(h->line_info_off); > > > + h->line_info_len = bswap_32(h->line_info_len); > > > + > > > + if (hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) > > > + return; > > > + > > > + h->core_relo_off = bswap_32(h->core_relo_off); > > > + h->core_relo_len = bswap_32(h->core_relo_len); > > > +} > > > + > > > +/* Swap byte-order of a generic info subsection */ > > > +static void info_subsec_bswap(const struct btf_ext_header *hdr, bool native, > > > + __u32 off, __u32 len, anon_info_bswap_fn_t bswap) > > > > ok, so I'm not a fan of this bswap callback, tbh. Also, we don't > > really enforce that each kind of record has exact size we expect > > (i.e., bpf_line_info_min and bpf_func_info_min shouldn't be "min" for > > byte-swapped case, it should be exact). > > > > How about this slight modification: split byte swapping of > > sections/subsection metadata, so we adjust record size, sec_name_off > > and num_info separately from adjusting each record. > > Hmmm, the bulk of code needed is to parse the metadata, with only 2 lines > used to go through records. Splitting per above would add unnecessary > duplication it seems, no? > > > > > Once this swapping is done we: > > > > a) validate record size for each section is expected (according to its > > type, of course) > > This is a good point I overlooked, and needs doing in any case. > > > b) we can then use for_each_btf_ext_sec() and for_each_btf_ext_rec() > > macro (which assume proper in-memory metadata byte order) and then > > hard-code swapping of each record fields > > How easily can we use these macros? Consider the current call chain: Not that easily, turns out, because a) it acquires data pointer implicitly, which makes it hard for btf_ext_raw_data() and b) it accesses sec->num_info and doesn't cache it, so we'd need an extra local variable to keep it if we were to swap it in raw data. > > btf_ext__new > btf_ext_parse > btf_ext_bswap_hdr (1) > btf_ext_bswap_info (2) > btf_ext_setup_func_info > btf_ext_setup_line_info > btf_ext_setup_core_relos (3) > > btf_ext__raw_data > btf_ext_bswap_info (4) > btf_ext_bswap_hdr > > The macros iterate on 'struct btf_ext_info' instances in 'struct btf_ext' > but these are only set up after (3) it seems and unavailable at (2). I > suppose they could be used with some sort of kludge but unsure how well > they'll work. > > > > > No callbacks. > > > > This has also a benefit of not needing this annoying "bool native" > > flag when producing raw bytes. We just ensure proper order of > > operation: > > > > a) swap records > > b) swap metadata (so just mirrored order from initialization) > > How does that work? If we split up btf_ext_bswap_info(), after (1) > btf_ext->swapped_endian is set and btf_ext->hdr->magic is swapped, so at > (2) it's not possible to tell the current info data byte order without > some hinting. > > But maybe if we defer setting btf_ext->swapped_endian until after (b) we > can drop the "bool native" thanks to symmetry breaking. Let me check. > > > > > WDYT? > > Adding a record_size check is definitely needed. > > But I have trouble seeing how splitting bswap of info metadata/records > would yield something simpler and cleaner than the callbacks. What if > they were passed via a descriptor, as in btf_ext_setup_func_info()? I > think I need to play around with this a while and see.. > > It would also help me if you'd elaborate on the drawbacks you see of > using callbacks, given I see then in other parts of libbpf. I replied to your latest patches. I generally dislike callbacks as they make following the code harder. If it's possible to not use callbacks with reasonable simplicity, I'll always go for that. But it's ok, given those existing iteration macros are a bit assuming about data and its endianness, it's hard to use them. > > > > > pw-bot: cr > > > > > +{ > > > + __u32 left, i, *rs, rec_size, num_info; > > > + struct btf_ext_info_sec *si; > > > + void *p; > > > + > > > + if (len == 0) > > > + return; > > > + > > > + rs = (void *)hdr + hdr->hdr_len + off; /* record size */ > > > + si = (void *)rs + sizeof(__u32); /* sec info #1 */ > > > + rec_size = native ? *rs : bswap_32(*rs); > > > + *rs = bswap_32(*rs); > > > + left = len - sizeof(__u32); > > > + while (left > 0) { > > > + num_info = native ? si->num_info : bswap_32(si->num_info); > > > + si->sec_name_off = bswap_32(si->sec_name_off); > > > + si->num_info = bswap_32(si->num_info); > > > + left -= offsetof(struct btf_ext_info_sec, data); > > > + p = si->data; > > > + for (i = 0; i < num_info; i++) /* list of records */ > > > + p += bswap(p); > > > + si = p; > > > + left -= rec_size * num_info; > > > > nit: extra space here > > Fixed, thanks. > > > > > > + } > > > +} > > > + > > > > [...]
On Fri, Aug 30, 2024 at 05:15:06PM -0700, Eduard Zingerman wrote: > On Fri, 2024-08-30 at 00:29 -0700, Tony Ambardar wrote: > > [...] > > > @@ -3050,11 +3127,42 @@ static int btf_ext_parse_hdr(__u8 *data, __u32 data_size) > > return -ENOTSUP; > > } > > > > - if (data_size == hdr->hdr_len) { > > + if (data_size < hdr_len) { > > + pr_debug("BTF.ext header not found\n"); > > + return -EINVAL; > > + } else if (data_size == hdr_len) { > > pr_debug("BTF.ext has no data\n"); > > return -EINVAL; > > } > > > > + /* Verify mandatory hdr info details present */ > > + if (hdr_len < offsetofend(struct btf_ext_header, line_info_len)) { > > + pr_warn("BTF.ext header missing func_info, line_info\n"); > > + return -EINVAL; > > + } > > + > > + /* Keep hdr native byte-order in memory for introspection */ > > + if (btf_ext->swapped_endian) > > + btf_ext_bswap_hdr(btf_ext, hdr_len); > > + > > + /* Basic info section consistency checks*/ > > + info_size = btf_ext->data_size - hdr_len; > > + if (info_size & 0x03) { > > + pr_warn("BTF.ext info size not 4-byte multiple\n"); > > + return -EINVAL; > > + } > > + info_size -= hdr->func_info_len + hdr->line_info_len; > > + if (hdr_len >= offsetofend(struct btf_ext_header, core_relo_len)) > > + info_size -= hdr->core_relo_len; > > nit: Since we are checking this, maybe also check that sections do not overlap? > Also, why disallowing gaps between sections? > > > + if (info_size) { > > + pr_warn("BTF.ext info size mismatch with header data\n"); > > + return -EINVAL; > > + } > > + > > + /* Keep infos native byte-order in memory for introspection */ > > + if (btf_ext->swapped_endian) > > + btf_ext_bswap_info(btf_ext, !btf_ext->swapped_endian); > > + > > return 0; > > } > > [...] > > > @@ -3119,15 +3223,71 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size) > > return btf_ext; > > } > > > > +static void *btf_ext_raw_data(const struct btf_ext *btf_ext_ro, __u32 *size, > > + bool swap_endian) > > +{ > > + struct btf_ext *btf_ext = (struct btf_ext *)btf_ext_ro; > > + const __u32 data_sz = btf_ext->data_size; > > + void *data; > > + > > + data = swap_endian ? btf_ext->data_swapped : btf_ext->data; > > + if (data) { > > + *size = data_sz; > > + return data; > > + } > > + > > + data = calloc(1, data_sz); > > + if (!data) > > + return NULL; > > + memcpy(data, btf_ext->data, data_sz); > > + > > + if (swap_endian) { > > + btf_ext_bswap_info(btf_ext, true); > > + btf_ext_bswap_hdr(btf_ext, btf_ext->hdr->hdr_len); > > + btf_ext->data_swapped = data; > > + } > > Nit: I don't like how this function is organized: > - if btf_ext->data can't be NULL swap_endian == true at this point; > - if btf_ext->data can be NULL and swap_endian == false > pointer to `data` would be lost. > > I assume that btf_ext->data can't be null, basing on the > btf_ext__new(), but function body is a bit confusing. Hi Eduard, Sorry, I saw this earlier but dropped my reply by mistake I think. You're right that btf_ext->data can't be null, and the awkwardness above is a holdover from trying to use the btf_raw_data() code, where it _can_ be null. I've rewritten it to be clearer for the next v6 series, which also reuses existing info sec validation and drops the extra code you referred to further above. Thanks, Tony > > > + > > + *size = data_sz; > > + return data; > > +} > > + > > [...] >
diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index f5081de86ee0..064cfe126c09 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -3022,25 +3022,102 @@ static int btf_ext_setup_core_relos(struct btf_ext *btf_ext) return btf_ext_setup_info(btf_ext, ¶m); } -static int btf_ext_parse_hdr(__u8 *data, __u32 data_size) +/* Swap byte-order of BTF.ext header with any endianness */ +static void btf_ext_bswap_hdr(struct btf_ext *btf_ext, __u32 hdr_len) { - const struct btf_ext_header *hdr = (struct btf_ext_header *)data; + struct btf_ext_header *h = btf_ext->hdr; - if (data_size < offsetofend(struct btf_ext_header, hdr_len) || - data_size < hdr->hdr_len) { - pr_debug("BTF.ext header not found\n"); + h->magic = bswap_16(h->magic); + h->hdr_len = bswap_32(h->hdr_len); + h->func_info_off = bswap_32(h->func_info_off); + h->func_info_len = bswap_32(h->func_info_len); + h->line_info_off = bswap_32(h->line_info_off); + h->line_info_len = bswap_32(h->line_info_len); + + if (hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) + return; + + h->core_relo_off = bswap_32(h->core_relo_off); + h->core_relo_len = bswap_32(h->core_relo_len); +} + +/* Swap byte-order of a generic info subsection */ +static void info_subsec_bswap(const struct btf_ext_header *hdr, bool native, + __u32 off, __u32 len, anon_info_bswap_fn_t bswap) +{ + __u32 left, i, *rs, rec_size, num_info; + struct btf_ext_info_sec *si; + void *p; + + if (len == 0) + return; + + rs = (void *)hdr + hdr->hdr_len + off; /* record size */ + si = (void *)rs + sizeof(__u32); /* sec info #1 */ + rec_size = native ? *rs : bswap_32(*rs); + *rs = bswap_32(*rs); + left = len - sizeof(__u32); + while (left > 0) { + num_info = native ? si->num_info : bswap_32(si->num_info); + si->sec_name_off = bswap_32(si->sec_name_off); + si->num_info = bswap_32(si->num_info); + left -= offsetof(struct btf_ext_info_sec, data); + p = si->data; + for (i = 0; i < num_info; i++) /* list of records */ + p += bswap(p); + si = p; + left -= rec_size * num_info; + } +} + +/* + * Swap endianness of the whole info segment in a BTF.ext data section: + * - requires BTF.ext header data in native byte order + * - only support info structs from BTF version 1 + * - native: current info data is native endianness + */ +static void btf_ext_bswap_info(struct btf_ext *btf_ext, bool native) +{ + const struct btf_ext_header *hdr = btf_ext->hdr; + + /* Swap func_info subsection byte-order */ + info_subsec_bswap(hdr, native, hdr->func_info_off, hdr->func_info_len, + (anon_info_bswap_fn_t)bpf_func_info_bswap); + + /* Swap line_info subsection byte-order */ + info_subsec_bswap(hdr, native, hdr->line_info_off, hdr->line_info_len, + (anon_info_bswap_fn_t)bpf_line_info_bswap); + + /* Swap core_relo subsection byte-order (if present) */ + if (hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) + return; + + info_subsec_bswap(hdr, native, hdr->core_relo_off, hdr->core_relo_len, + (anon_info_bswap_fn_t)bpf_core_relo_bswap); +} + +/* Validate hdr data & info sections, convert to native endianness */ +static int btf_ext_parse(struct btf_ext *btf_ext) +{ + struct btf_ext_header *hdr = btf_ext->hdr; + __u32 hdr_len, info_size, data_size = btf_ext->data_size; + + if (data_size < offsetofend(struct btf_ext_header, hdr_len)) { + pr_debug("BTF.ext header too short\n"); return -EINVAL; } + hdr_len = hdr->hdr_len; if (hdr->magic == bswap_16(BTF_MAGIC)) { - pr_warn("BTF.ext in non-native endianness is not supported\n"); - return -ENOTSUP; + btf_ext->swapped_endian = true; + hdr_len = bswap_32(hdr_len); } else if (hdr->magic != BTF_MAGIC) { pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic); return -EINVAL; } - if (hdr->version != BTF_VERSION) { + /* Ensure known version of structs, current BTF_VERSION == 1 */ + if (hdr->version != 1) { pr_debug("Unsupported BTF.ext version:%u\n", hdr->version); return -ENOTSUP; } @@ -3050,11 +3127,42 @@ static int btf_ext_parse_hdr(__u8 *data, __u32 data_size) return -ENOTSUP; } - if (data_size == hdr->hdr_len) { + if (data_size < hdr_len) { + pr_debug("BTF.ext header not found\n"); + return -EINVAL; + } else if (data_size == hdr_len) { pr_debug("BTF.ext has no data\n"); return -EINVAL; } + /* Verify mandatory hdr info details present */ + if (hdr_len < offsetofend(struct btf_ext_header, line_info_len)) { + pr_warn("BTF.ext header missing func_info, line_info\n"); + return -EINVAL; + } + + /* Keep hdr native byte-order in memory for introspection */ + if (btf_ext->swapped_endian) + btf_ext_bswap_hdr(btf_ext, hdr_len); + + /* Basic info section consistency checks*/ + info_size = btf_ext->data_size - hdr_len; + if (info_size & 0x03) { + pr_warn("BTF.ext info size not 4-byte multiple\n"); + return -EINVAL; + } + info_size -= hdr->func_info_len + hdr->line_info_len; + if (hdr_len >= offsetofend(struct btf_ext_header, core_relo_len)) + info_size -= hdr->core_relo_len; + if (info_size) { + pr_warn("BTF.ext info size mismatch with header data\n"); + return -EINVAL; + } + + /* Keep infos native byte-order in memory for introspection */ + if (btf_ext->swapped_endian) + btf_ext_bswap_info(btf_ext, !btf_ext->swapped_endian); + return 0; } @@ -3066,6 +3174,7 @@ void btf_ext__free(struct btf_ext *btf_ext) free(btf_ext->line_info.sec_idxs); free(btf_ext->core_relo_info.sec_idxs); free(btf_ext->data); + free(btf_ext->data_swapped); free(btf_ext); } @@ -3086,15 +3195,10 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size) } memcpy(btf_ext->data, data, size); - err = btf_ext_parse_hdr(btf_ext->data, size); + err = btf_ext_parse(btf_ext); if (err) goto done; - if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, line_info_len)) { - err = -EINVAL; - goto done; - } - err = btf_ext_setup_func_info(btf_ext); if (err) goto done; @@ -3119,15 +3223,71 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size) return btf_ext; } +static void *btf_ext_raw_data(const struct btf_ext *btf_ext_ro, __u32 *size, + bool swap_endian) +{ + struct btf_ext *btf_ext = (struct btf_ext *)btf_ext_ro; + const __u32 data_sz = btf_ext->data_size; + void *data; + + data = swap_endian ? btf_ext->data_swapped : btf_ext->data; + if (data) { + *size = data_sz; + return data; + } + + data = calloc(1, data_sz); + if (!data) + return NULL; + memcpy(data, btf_ext->data, data_sz); + + if (swap_endian) { + btf_ext_bswap_info(btf_ext, true); + btf_ext_bswap_hdr(btf_ext, btf_ext->hdr->hdr_len); + btf_ext->data_swapped = data; + } + + *size = data_sz; + return data; +} + const void *btf_ext__raw_data(const struct btf_ext *btf_ext, __u32 *size) { + __u32 data_sz; + void *data; + + data = btf_ext_raw_data(btf_ext, &data_sz, btf_ext->swapped_endian); + if (!data) + return errno = ENOMEM, NULL; + *size = btf_ext->data_size; - return btf_ext->data; + return data; } __attribute__((alias("btf_ext__raw_data"))) const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size); +enum btf_endianness btf_ext__endianness(const struct btf_ext *btf_ext) +{ + if (is_host_big_endian()) + return btf_ext->swapped_endian ? BTF_LITTLE_ENDIAN : BTF_BIG_ENDIAN; + else + return btf_ext->swapped_endian ? BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; +} + +int btf_ext__set_endianness(struct btf_ext *btf_ext, enum btf_endianness endian) +{ + if (endian != BTF_LITTLE_ENDIAN && endian != BTF_BIG_ENDIAN) + return libbpf_err(-EINVAL); + + btf_ext->swapped_endian = is_host_big_endian() != (endian == BTF_BIG_ENDIAN); + + if (!btf_ext->swapped_endian) { + free(btf_ext->data_swapped); + btf_ext->data_swapped = NULL; + } + return 0; +} struct btf_dedup; diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index b68d216837a9..e3cf91687c78 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -167,6 +167,9 @@ LIBBPF_API const char *btf__str_by_offset(const struct btf *btf, __u32 offset); LIBBPF_API struct btf_ext *btf_ext__new(const __u8 *data, __u32 size); LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext); LIBBPF_API const void *btf_ext__raw_data(const struct btf_ext *btf_ext, __u32 *size); +LIBBPF_API enum btf_endianness btf_ext__endianness(const struct btf_ext *btf_ext); +LIBBPF_API int btf_ext__set_endianness(struct btf_ext *btf_ext, + enum btf_endianness endian); LIBBPF_API int btf__find_str(struct btf *btf, const char *s); LIBBPF_API int btf__add_str(struct btf *btf, const char *s); diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 8f0d9ea3b1b4..5c17632807b6 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -421,6 +421,8 @@ LIBBPF_1.5.0 { global: btf__distill_base; btf__relocate; + btf_ext__endianness; + btf_ext__set_endianness; bpf_map__autoattach; bpf_map__set_autoattach; bpf_program__attach_sockmap; diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index 8cda511a1982..81d375015c2b 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -484,6 +484,8 @@ struct btf_ext { struct btf_ext_header *hdr; void *data; }; + void *data_swapped; + bool swapped_endian; struct btf_ext_info func_info; struct btf_ext_info line_info; struct btf_ext_info core_relo_info; @@ -511,6 +513,37 @@ struct bpf_line_info_min { __u32 line_col; }; +/* Functions/typedef to help byte-swap info records, returning their size */ + +typedef int (*anon_info_bswap_fn_t)(void *); + +static inline int bpf_func_info_bswap(struct bpf_func_info *i) +{ + i->insn_off = bswap_32(i->insn_off); + i->type_id = bswap_32(i->type_id); + return sizeof(*i); +} + +static inline int bpf_line_info_bswap(struct bpf_line_info *i) +{ + i->insn_off = bswap_32(i->insn_off); + i->file_name_off = bswap_32(i->file_name_off); + i->line_off = bswap_32(i->line_off); + i->line_col = bswap_32(i->line_col); + return sizeof(*i); +} + +static inline int bpf_core_relo_bswap(struct bpf_core_relo *i) +{ + _Static_assert(sizeof(i->kind) == sizeof(__u32), + "enum bpf_core_relo_kind is not 32-bit\n"); + i->insn_off = bswap_32(i->insn_off); + i->type_id = bswap_32(i->type_id); + i->access_str_off = bswap_32(i->access_str_off); + i->kind = bswap_32(i->kind); + return sizeof(*i); +} + enum btf_field_iter_kind { BTF_FIELD_ITER_IDS, BTF_FIELD_ITER_STRS,
Support for handling BTF data of either endianness was added in [1], but did not include BTF.ext data for lack of use cases. Later, support for static linking [2] provided a use case, but this feature and later ones were restricted to native-endian usage. Add support for BTF.ext handling in either endianness. Convert BTF.ext data to native endianness when read into memory for further processing, and support raw data access that restores the original byte-order for output. Add internal header functions for byte-swapping func, line, and core info records. Add new API functions btf_ext__endianness() and btf_ext__set_endianness() for query and setting byte-order, as already exist for BTF data. [1] 3289959b97ca ("libbpf: Support BTF loading and raw data output in both endianness") [2] 8fd27bf69b86 ("libbpf: Add BPF static linker BTF and BTF.ext support") Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com> --- tools/lib/bpf/btf.c | 192 +++++++++++++++++++++++++++++--- tools/lib/bpf/btf.h | 3 + tools/lib/bpf/libbpf.map | 2 + tools/lib/bpf/libbpf_internal.h | 33 ++++++ 4 files changed, 214 insertions(+), 16 deletions(-)