diff mbox series

btf_loader: support BTF_KIND_ENUM64 was Re: [PATCH dwarves v2 0/2] btf: support BTF_KIND_ENUM64

Message ID YryELT6OadpiJki/@kernel.org (mailing list archive)
State Not Applicable
Delegated to: BPF
Headers show
Series btf_loader: support BTF_KIND_ENUM64 was Re: [PATCH dwarves v2 0/2] btf: support BTF_KIND_ENUM64 | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch
bpf/vmtest-bpf-PR fail merge-conflict

Commit Message

Arnaldo Carvalho de Melo June 29, 2022, 4:56 p.m. UTC
Em Wed, Jun 29, 2022 at 01:30:10PM -0300, Arnaldo Carvalho de Melo escreveu:
> ⬢[acme@toolbox pahole]$ pdwtags -F btf vmlinux-v5.18-rc7+ | grep -B10 -A5 BPF_F_CTXLEN_MASK
> BTF: idx: 4173, Unknown kind 19
> BTF: idx: 4975, Unknown kind 19
> BTF: idx: 6673, Unknown kind 19
> BTF: idx: 27413, Unknown kind 19
> BTF: idx: 30626, Unknown kind 19
> BTF: idx: 30829, Unknown kind 19
> BTF: idx: 38040, Unknown kind 19
> BTF: idx: 56969, Unknown kind 19
> BTF: idx: 83004, Unknown kind 19
> ⬢[acme@toolbox pahole]$
> 
> Ok, I need to update pahole's BTF loader to support:
> 
> lib/bpf/src/btf.h:#define BTF_KIND_ENUM64		19	/* Enum for up-to 64bit values */
> 
> 
> Working on it now.

⬢[acme@toolbox pahole]$ pdwtags -F btf vmlinux-v5.18-rc7+ | grep -B5 -A5 BPF_F_CTXLEN_MASK

/* 27413 */
enum {
	BPF_F_INDEX_MASK  = 4294967295,
	BPF_F_CURRENT_CPU = 4294967295,
	BPF_F_CTXLEN_MASK = 4503595332403200,
} __attribute__((__packed__)); /* size: 8 */

/* 27414 */
enum {
	BPF_F_GET_BRANCH_RECORDS_SIZE = 1,
⬢[acme@toolbox pahole]$

Quick patch here, please Ack, if possible:

Comments

Andrii Nakryiko July 6, 2022, 4:28 a.m. UTC | #1
On Wed, Jun 29, 2022 at 9:56 AM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> Em Wed, Jun 29, 2022 at 01:30:10PM -0300, Arnaldo Carvalho de Melo escreveu:
> > ⬢[acme@toolbox pahole]$ pdwtags -F btf vmlinux-v5.18-rc7+ | grep -B10 -A5 BPF_F_CTXLEN_MASK
> > BTF: idx: 4173, Unknown kind 19
> > BTF: idx: 4975, Unknown kind 19
> > BTF: idx: 6673, Unknown kind 19
> > BTF: idx: 27413, Unknown kind 19
> > BTF: idx: 30626, Unknown kind 19
> > BTF: idx: 30829, Unknown kind 19
> > BTF: idx: 38040, Unknown kind 19
> > BTF: idx: 56969, Unknown kind 19
> > BTF: idx: 83004, Unknown kind 19
> > ⬢[acme@toolbox pahole]$
> >
> > Ok, I need to update pahole's BTF loader to support:
> >
> > lib/bpf/src/btf.h:#define BTF_KIND_ENUM64             19      /* Enum for up-to 64bit values */
> >
> >
> > Working on it now.
>
> ⬢[acme@toolbox pahole]$ pdwtags -F btf vmlinux-v5.18-rc7+ | grep -B5 -A5 BPF_F_CTXLEN_MASK
>
> /* 27413 */
> enum {
>         BPF_F_INDEX_MASK  = 4294967295,
>         BPF_F_CURRENT_CPU = 4294967295,
>         BPF_F_CTXLEN_MASK = 4503595332403200,
> } __attribute__((__packed__)); /* size: 8 */
>
> /* 27414 */
> enum {
>         BPF_F_GET_BRANCH_RECORDS_SIZE = 1,
> ⬢[acme@toolbox pahole]$
>
> Quick patch here, please Ack, if possible:
>

two minor nits, but looks good overall:

Acked-by: Andrii Nakryiko <andrii@kernel.org>

> diff --git a/btf_loader.c b/btf_loader.c
> index b5d444643adf30b1..e57ecce2cde26e4e 100644
> --- a/btf_loader.c
> +++ b/btf_loader.c
> @@ -312,6 +312,49 @@ out_free:
>         return -ENOMEM;
>  }
>
> +static struct enumerator *enumerator__new64(const char *name, uint64_t value)
> +{
> +       struct enumerator *en = tag__alloc(sizeof(*en));
> +
> +       if (en != NULL) {
> +               en->name = name;
> +               en->value = value; // Value is already 64-bit, as this is used with DWARF as well
> +               en->tag.tag = DW_TAG_enumerator;
> +       }
> +
> +       return en;
> +}
> +
> +static int create_new_enumeration64(struct cu *cu, const struct btf_type *tp, uint32_t id)
> +{
> +       struct btf_enum64 *ep = btf_enum64(tp);
> +       uint16_t i, vlen = btf_vlen(tp);
> +       struct type *enumeration = type__new(DW_TAG_enumeration_type,
> +                                            cu__btf_str(cu, tp->name_off),
> +                                            tp->size ? tp->size * 8 : (sizeof(int) * 8));

tp->size should always be valid, so this fall back to sizeof(int)
isn't necessary

> +
> +       if (enumeration == NULL)
> +               return -ENOMEM;
> +
> +       for (i = 0; i < vlen; i++) {
> +               const char *name = cu__btf_str(cu, ep[i].name_off);
> +               uint64_t value = ((uint64_t)ep[i].val_hi32) << 32 | ep[i].val_lo32;

use btf_enum64_value() defined in libbpf's btf.h header

> +               struct enumerator *enumerator = enumerator__new64(name, value);
> +
> +               if (enumerator == NULL)
> +                       goto out_free;
> +
> +               enumeration__add(enumeration, enumerator);
> +       }
> +
> +       cu__add_tag_with_id(cu, &enumeration->namespace.tag, id);
> +
> +       return 0;
> +out_free:
> +       enumeration__delete(enumeration);
> +       return -ENOMEM;
> +}
> +
>  static int create_new_subroutine_type(struct cu *cu, const struct btf_type *tp, uint32_t id)
>  {
>         struct ftype *proto = tag__alloc(sizeof(*proto));
> @@ -419,6 +462,9 @@ static int btf__load_types(struct btf *btf, struct cu *cu)
>                 case BTF_KIND_ENUM:
>                         err = create_new_enumeration(cu, type_ptr, type_index);
>                         break;
> +               case BTF_KIND_ENUM64:
> +                       err = create_new_enumeration64(cu, type_ptr, type_index);
> +                       break;
>                 case BTF_KIND_FWD:
>                         err = create_new_forward_decl(cu, type_ptr, type_index);
>                         break;
Arnaldo Carvalho de Melo July 6, 2022, 3:51 p.m. UTC | #2
Em Tue, Jul 05, 2022 at 09:28:05PM -0700, Andrii Nakryiko escreveu:
> On Wed, Jun 29, 2022 at 9:56 AM Arnaldo Carvalho de Melo
> <acme@kernel.org> wrote:
> >
> > Em Wed, Jun 29, 2022 at 01:30:10PM -0300, Arnaldo Carvalho de Melo escreveu:
> > > ⬢[acme@toolbox pahole]$ pdwtags -F btf vmlinux-v5.18-rc7+ | grep -B10 -A5 BPF_F_CTXLEN_MASK
> > > BTF: idx: 4173, Unknown kind 19
> > > BTF: idx: 4975, Unknown kind 19
> > > BTF: idx: 6673, Unknown kind 19
> > > BTF: idx: 27413, Unknown kind 19
> > > BTF: idx: 30626, Unknown kind 19
> > > BTF: idx: 30829, Unknown kind 19
> > > BTF: idx: 38040, Unknown kind 19
> > > BTF: idx: 56969, Unknown kind 19
> > > BTF: idx: 83004, Unknown kind 19
> > > ⬢[acme@toolbox pahole]$
> > >
> > > Ok, I need to update pahole's BTF loader to support:
> > >
> > > lib/bpf/src/btf.h:#define BTF_KIND_ENUM64             19      /* Enum for up-to 64bit values */
> > >
> > >
> > > Working on it now.
> >
> > ⬢[acme@toolbox pahole]$ pdwtags -F btf vmlinux-v5.18-rc7+ | grep -B5 -A5 BPF_F_CTXLEN_MASK
> >
> > /* 27413 */
> > enum {
> >         BPF_F_INDEX_MASK  = 4294967295,
> >         BPF_F_CURRENT_CPU = 4294967295,
> >         BPF_F_CTXLEN_MASK = 4503595332403200,
> > } __attribute__((__packed__)); /* size: 8 */
> >
> > /* 27414 */
> > enum {
> >         BPF_F_GET_BRANCH_RECORDS_SIZE = 1,
> > ⬢[acme@toolbox pahole]$
> >
> > Quick patch here, please Ack, if possible:
> >
> 
> two minor nits, but looks good overall:
> 
> Acked-by: Andrii Nakryiko <andrii@kernel.org>

Thanks for reviewing it!
 
> > diff --git a/btf_loader.c b/btf_loader.c
> > index b5d444643adf30b1..e57ecce2cde26e4e 100644
> > --- a/btf_loader.c
> > +++ b/btf_loader.c
> > @@ -312,6 +312,49 @@ out_free:
> >         return -ENOMEM;
> >  }
> >
> > +static struct enumerator *enumerator__new64(const char *name, uint64_t value)
> > +{
> > +       struct enumerator *en = tag__alloc(sizeof(*en));
> > +
> > +       if (en != NULL) {
> > +               en->name = name;
> > +               en->value = value; // Value is already 64-bit, as this is used with DWARF as well
> > +               en->tag.tag = DW_TAG_enumerator;
> > +       }
> > +
> > +       return en;
> > +}
> > +
> > +static int create_new_enumeration64(struct cu *cu, const struct btf_type *tp, uint32_t id)
> > +{
> > +       struct btf_enum64 *ep = btf_enum64(tp);
> > +       uint16_t i, vlen = btf_vlen(tp);
> > +       struct type *enumeration = type__new(DW_TAG_enumeration_type,
> > +                                            cu__btf_str(cu, tp->name_off),
> > +                                            tp->size ? tp->size * 8 : (sizeof(int) * 8));
> 
> tp->size should always be valid, so this fall back to sizeof(int)
> isn't necessary

This was just a copy from the create_new_enumeration() one, will remove
it there as well as a prep patch, then not have it in the 64-bit
version.
 
> > +
> > +       if (enumeration == NULL)
> > +               return -ENOMEM;
> > +
> > +       for (i = 0; i < vlen; i++) {
> > +               const char *name = cu__btf_str(cu, ep[i].name_off);
> > +               uint64_t value = ((uint64_t)ep[i].val_hi32) << 32 | ep[i].val_lo32;
> 
> use btf_enum64_value() defined in libbpf's btf.h header

Great, will pick that
 
> > +               struct enumerator *enumerator = enumerator__new64(name, value);
> > +
> > +               if (enumerator == NULL)
> > +                       goto out_free;
> > +
> > +               enumeration__add(enumeration, enumerator);
> > +       }
> > +
> > +       cu__add_tag_with_id(cu, &enumeration->namespace.tag, id);
> > +
> > +       return 0;
> > +out_free:
> > +       enumeration__delete(enumeration);
> > +       return -ENOMEM;
> > +}
> > +
> >  static int create_new_subroutine_type(struct cu *cu, const struct btf_type *tp, uint32_t id)
> >  {
> >         struct ftype *proto = tag__alloc(sizeof(*proto));
> > @@ -419,6 +462,9 @@ static int btf__load_types(struct btf *btf, struct cu *cu)
> >                 case BTF_KIND_ENUM:
> >                         err = create_new_enumeration(cu, type_ptr, type_index);
> >                         break;
> > +               case BTF_KIND_ENUM64:
> > +                       err = create_new_enumeration64(cu, type_ptr, type_index);
> > +                       break;
> >                 case BTF_KIND_FWD:
> >                         err = create_new_forward_decl(cu, type_ptr, type_index);
> >                         break;
diff mbox series

Patch

diff --git a/btf_loader.c b/btf_loader.c
index b5d444643adf30b1..e57ecce2cde26e4e 100644
--- a/btf_loader.c
+++ b/btf_loader.c
@@ -312,6 +312,49 @@  out_free:
 	return -ENOMEM;
 }
 
+static struct enumerator *enumerator__new64(const char *name, uint64_t value)
+{
+	struct enumerator *en = tag__alloc(sizeof(*en));
+
+	if (en != NULL) {
+		en->name = name;
+		en->value = value; // Value is already 64-bit, as this is used with DWARF as well
+		en->tag.tag = DW_TAG_enumerator;
+	}
+
+	return en;
+}
+
+static int create_new_enumeration64(struct cu *cu, const struct btf_type *tp, uint32_t id)
+{
+	struct btf_enum64 *ep = btf_enum64(tp);
+	uint16_t i, vlen = btf_vlen(tp);
+	struct type *enumeration = type__new(DW_TAG_enumeration_type,
+					     cu__btf_str(cu, tp->name_off),
+					     tp->size ? tp->size * 8 : (sizeof(int) * 8));
+
+	if (enumeration == NULL)
+		return -ENOMEM;
+
+	for (i = 0; i < vlen; i++) {
+		const char *name = cu__btf_str(cu, ep[i].name_off);
+		uint64_t value = ((uint64_t)ep[i].val_hi32) << 32 | ep[i].val_lo32;
+		struct enumerator *enumerator = enumerator__new64(name, value);
+
+		if (enumerator == NULL)
+			goto out_free;
+
+		enumeration__add(enumeration, enumerator);
+	}
+
+	cu__add_tag_with_id(cu, &enumeration->namespace.tag, id);
+
+	return 0;
+out_free:
+	enumeration__delete(enumeration);
+	return -ENOMEM;
+}
+
 static int create_new_subroutine_type(struct cu *cu, const struct btf_type *tp, uint32_t id)
 {
 	struct ftype *proto = tag__alloc(sizeof(*proto));
@@ -419,6 +462,9 @@  static int btf__load_types(struct btf *btf, struct cu *cu)
 		case BTF_KIND_ENUM:
 			err = create_new_enumeration(cu, type_ptr, type_index);
 			break;
+		case BTF_KIND_ENUM64:
+			err = create_new_enumeration64(cu, type_ptr, type_index);
+			break;
 		case BTF_KIND_FWD:
 			err = create_new_forward_decl(cu, type_ptr, type_index);
 			break;