diff mbox series

[bpf-next,04/12] libbpf: Add btf enum64 support

Message ID 20220501190023.2578209-1-yhs@fb.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: Add 64bit enum value support | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR fail PR summary
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 2 this patch: 2
netdev/cc_maintainers warning 8 maintainers not CCed: songliubraving@fb.com shuah@kernel.org netdev@vger.kernel.org kafai@fb.com linux-kselftest@vger.kernel.org alan.maguire@oracle.com john.fastabend@gmail.com kpsingh@kernel.org
netdev/build_clang success Errors and warnings before: 9 this patch: 9
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 2 this patch: 2
netdev/checkpatch fail ERROR: open brace '{' following function definitions go on the next line ERROR: trailing statements should be on next line WARNING: braces {} are not necessary for single statement blocks WARNING: line length of 100 exceeds 80 columns WARNING: line length of 103 exceeds 80 columns WARNING: line length of 116 exceeds 80 columns WARNING: line length of 83 exceeds 80 columns WARNING: line length of 84 exceeds 80 columns WARNING: line length of 85 exceeds 80 columns WARNING: line length of 86 exceeds 80 columns WARNING: line length of 89 exceeds 80 columns WARNING: line length of 93 exceeds 80 columns WARNING: line length of 95 exceeds 80 columns WARNING: line length of 96 exceeds 80 columns WARNING: line length of 97 exceeds 80 columns WARNING: space prohibited before semicolon
netdev/kdoc success Errors and warnings before: 12 this patch: 12
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-1 fail Logs for Kernel LATEST on ubuntu-latest + selftests
bpf/vmtest-bpf-next-VM_Test-2 fail Logs for Kernel LATEST on z15 + selftests

Commit Message

Yonghong Song May 1, 2022, 7 p.m. UTC
Add BTF_KIND_ENUM64 support. Deprecated btf__add_enum() and
btf__add_enum_value() and introduced the following new APIs
  btf__add_enum32()
  btf__add_enum32_value()
  btf__add_enum64()
  btf__add_enum64_value()
due to new kind and introduction of kflag.

To support old kernel with enum64, the sanitization is
added to replace BTF_KIND_ENUM64 with a bunch of
pointer-to-void types.

The enum64 value relocation is also supported. The enum64
forward resolution, with enum type as forward declaration
and enum64 as the actual definition, is also supported.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/lib/bpf/btf.c                           | 226 +++++++++++++++++-
 tools/lib/bpf/btf.h                           |  21 ++
 tools/lib/bpf/btf_dump.c                      |  94 ++++++--
 tools/lib/bpf/libbpf.c                        |  64 ++++-
 tools/lib/bpf/libbpf.map                      |   4 +
 tools/lib/bpf/libbpf_internal.h               |   2 +
 tools/lib/bpf/linker.c                        |   2 +
 tools/lib/bpf/relo_core.c                     |  93 ++++---
 .../selftests/bpf/prog_tests/btf_dump.c       |  10 +-
 .../selftests/bpf/prog_tests/btf_write.c      |   6 +-
 10 files changed, 450 insertions(+), 72 deletions(-)

Comments

kernel test robot May 3, 2022, 5:22 p.m. UTC | #1
Hi Yonghong,

I love your patch! Perhaps something to improve:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Yonghong-Song/bpf-Add-64bit-enum-value-support/20220502-030301
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: i386-randconfig-m021 (https://download.01.org/0day-ci/archive/20220504/202205040133.jd7yTwg5-lkp@intel.com/config)
compiler: gcc-11 (Debian 11.2.0-20) 11.2.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

New smatch warnings:
tools/lib/bpf/relo_core.c:348 bpf_core_fields_are_compat() warn: if();

Old smatch warnings:
tools/lib/bpf/relo_core.c:349 bpf_core_fields_are_compat() warn: if();

vim +348 tools/lib/bpf/relo_core.c

   314	
   315	/* Check two types for compatibility for the purpose of field access
   316	 * relocation. const/volatile/restrict and typedefs are skipped to ensure we
   317	 * are relocating semantically compatible entities:
   318	 *   - any two STRUCTs/UNIONs are compatible and can be mixed;
   319	 *   - any two FWDs are compatible, if their names match (modulo flavor suffix);
   320	 *   - any two PTRs are always compatible;
   321	 *   - for ENUMs, names should be the same (ignoring flavor suffix) or at
   322	 *     least one of enums should be anonymous;
   323	 *   - for ENUMs, check sizes, names are ignored;
   324	 *   - for INT, size and signedness are ignored;
   325	 *   - any two FLOATs are always compatible;
   326	 *   - for ARRAY, dimensionality is ignored, element types are checked for
   327	 *     compatibility recursively;
   328	 *   - everything else shouldn't be ever a target of relocation.
   329	 * These rules are not set in stone and probably will be adjusted as we get
   330	 * more experience with using BPF CO-RE relocations.
   331	 */
   332	static int bpf_core_fields_are_compat(const struct btf *local_btf,
   333					      __u32 local_id,
   334					      const struct btf *targ_btf,
   335					      __u32 targ_id)
   336	{
   337		const struct btf_type *local_type, *targ_type;
   338	
   339	recur:
   340		local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
   341		targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
   342		if (!local_type || !targ_type)
   343			return -EINVAL;
   344	
   345		if (btf_is_composite(local_type) && btf_is_composite(targ_type))
   346			return 1;
   347		if (btf_kind(local_type) != btf_kind(targ_type)) {
 > 348			if (btf_is_enum(local_type) && btf_is_enum64(targ_type)) ;
   349			else if (btf_is_enum64(local_type) && btf_is_enum(targ_type)) ;
   350			else return 0;
   351		}
   352	
   353		switch (btf_kind(local_type)) {
   354		case BTF_KIND_PTR:
   355		case BTF_KIND_FLOAT:
   356			return 1;
   357		case BTF_KIND_FWD:
   358		case BTF_KIND_ENUM:
   359		case BTF_KIND_ENUM64: {
   360			const char *local_name, *targ_name;
   361			size_t local_len, targ_len;
   362	
   363			local_name = btf__name_by_offset(local_btf,
   364							 local_type->name_off);
   365			targ_name = btf__name_by_offset(targ_btf, targ_type->name_off);
   366			local_len = bpf_core_essential_name_len(local_name);
   367			targ_len = bpf_core_essential_name_len(targ_name);
   368			/* one of them is anonymous or both w/ same flavor-less names */
   369			return local_len == 0 || targ_len == 0 ||
   370			       (local_len == targ_len &&
   371				strncmp(local_name, targ_name, local_len) == 0);
   372		}
   373		case BTF_KIND_INT:
   374			/* just reject deprecated bitfield-like integers; all other
   375			 * integers are by default compatible between each other
   376			 */
   377			return btf_int_offset(local_type) == 0 &&
   378			       btf_int_offset(targ_type) == 0;
   379		case BTF_KIND_ARRAY:
   380			local_id = btf_array(local_type)->type;
   381			targ_id = btf_array(targ_type)->type;
   382			goto recur;
   383		default:
   384			return 0;
   385		}
   386	}
   387
Yonghong Song May 5, 2022, 10:44 p.m. UTC | #2
On 5/3/22 10:22 AM, kernel test robot wrote:
> Hi Yonghong,
> 
> I love your patch! Perhaps something to improve:
> 
> [auto build test WARNING on bpf-next/master]
> 
> url:    https://github.com/intel-lab-lkp/linux/commits/Yonghong-Song/bpf-Add-64bit-enum-value-support/20220502-030301
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
> config: i386-randconfig-m021 (https://download.01.org/0day-ci/archive/20220504/202205040133.jd7yTwg5-lkp@intel.com/config )
> compiler: gcc-11 (Debian 11.2.0-20) 11.2.0
> 
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kernel test robot <lkp@intel.com>
> 
> New smatch warnings:
> tools/lib/bpf/relo_core.c:348 bpf_core_fields_are_compat() warn: if();
> 
> Old smatch warnings:
> tools/lib/bpf/relo_core.c:349 bpf_core_fields_are_compat() warn: if();

The following change should work:

diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
index 1e751400427b..2c8d5292e946 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -345,9 +345,8 @@ static int bpf_core_fields_are_compat(const struct 
btf *local_btf,
         if (btf_is_composite(local_type) && btf_is_composite(targ_type))
                 return 1;
         if (btf_kind(local_type) != btf_kind(targ_type)) {
-               if (btf_is_enum(local_type) && btf_is_enum64(targ_type)) ;
-               else if (btf_is_enum64(local_type) && 
btf_is_enum(targ_type)) ;
-               else return 0;
+               if (!btf_is_enum(local_type) || !btf_is_enum64(targ_type))
+                       return 0;
         }

         switch (btf_kind(local_type)) {

I will wait for more comments before submitting version 2.

> 
> vim +348 tools/lib/bpf/relo_core.c
> 
>     314	
>     315	/* Check two types for compatibility for the purpose of field access
>     316	 * relocation. const/volatile/restrict and typedefs are skipped to ensure we
>     317	 * are relocating semantically compatible entities:
>     318	 *   - any two STRUCTs/UNIONs are compatible and can be mixed;
>     319	 *   - any two FWDs are compatible, if their names match (modulo flavor suffix);
>     320	 *   - any two PTRs are always compatible;
>     321	 *   - for ENUMs, names should be the same (ignoring flavor suffix) or at
>     322	 *     least one of enums should be anonymous;
>     323	 *   - for ENUMs, check sizes, names are ignored;
>     324	 *   - for INT, size and signedness are ignored;
>     325	 *   - any two FLOATs are always compatible;
>     326	 *   - for ARRAY, dimensionality is ignored, element types are checked for
>     327	 *     compatibility recursively;
>     328	 *   - everything else shouldn't be ever a target of relocation.
>     329	 * These rules are not set in stone and probably will be adjusted as we get
>     330	 * more experience with using BPF CO-RE relocations.
>     331	 */
>     332	static int bpf_core_fields_are_compat(const struct btf *local_btf,
>     333					      __u32 local_id,
>     334					      const struct btf *targ_btf,
>     335					      __u32 targ_id)
>     336	{
>     337		const struct btf_type *local_type, *targ_type;
>     338	
>     339	recur:
>     340		local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
>     341		targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
>     342		if (!local_type || !targ_type)
>     343			return -EINVAL;
>     344	
>     345		if (btf_is_composite(local_type) && btf_is_composite(targ_type))
>     346			return 1;
>     347		if (btf_kind(local_type) != btf_kind(targ_type)) {
>   > 348			if (btf_is_enum(local_type) && btf_is_enum64(targ_type)) ;
>     349			else if (btf_is_enum64(local_type) && btf_is_enum(targ_type)) ;
>     350			else return 0;
>     351		}
>     352	
>     353		switch (btf_kind(local_type)) {
>     354		case BTF_KIND_PTR:
>     355		case BTF_KIND_FLOAT:
>     356			return 1;
>     357		case BTF_KIND_FWD:
>     358		case BTF_KIND_ENUM:
>     359		case BTF_KIND_ENUM64: {
>     360			const char *local_name, *targ_name;
>     361			size_t local_len, targ_len;
>     362	
>     363			local_name = btf__name_by_offset(local_btf,
>     364							 local_type->name_off);
>     365			targ_name = btf__name_by_offset(targ_btf, targ_type->name_off);
>     366			local_len = bpf_core_essential_name_len(local_name);
>     367			targ_len = bpf_core_essential_name_len(targ_name);
>     368			/* one of them is anonymous or both w/ same flavor-less names */
>     369			return local_len == 0 || targ_len == 0 ||
>     370			       (local_len == targ_len &&
>     371				strncmp(local_name, targ_name, local_len) == 0);
>     372		}
>     373		case BTF_KIND_INT:
>     374			/* just reject deprecated bitfield-like integers; all other
>     375			 * integers are by default compatible between each other
>     376			 */
>     377			return btf_int_offset(local_type) == 0 &&
>     378			       btf_int_offset(targ_type) == 0;
>     379		case BTF_KIND_ARRAY:
>     380			local_id = btf_array(local_type)->type;
>     381			targ_id = btf_array(targ_type)->type;
>     382			goto recur;
>     383		default:
>     384			return 0;
>     385		}
>     386	}
>     387	
>
Andrii Nakryiko May 9, 2022, 11:25 p.m. UTC | #3
On Sun, May 1, 2022 at 12:00 PM Yonghong Song <yhs@fb.com> wrote:
>
> Add BTF_KIND_ENUM64 support. Deprecated btf__add_enum() and
> btf__add_enum_value() and introduced the following new APIs
>   btf__add_enum32()
>   btf__add_enum32_value()
>   btf__add_enum64()
>   btf__add_enum64_value()
> due to new kind and introduction of kflag.
>
> To support old kernel with enum64, the sanitization is
> added to replace BTF_KIND_ENUM64 with a bunch of
> pointer-to-void types.
>
> The enum64 value relocation is also supported. The enum64
> forward resolution, with enum type as forward declaration
> and enum64 as the actual definition, is also supported.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---
>  tools/lib/bpf/btf.c                           | 226 +++++++++++++++++-
>  tools/lib/bpf/btf.h                           |  21 ++
>  tools/lib/bpf/btf_dump.c                      |  94 ++++++--
>  tools/lib/bpf/libbpf.c                        |  64 ++++-
>  tools/lib/bpf/libbpf.map                      |   4 +
>  tools/lib/bpf/libbpf_internal.h               |   2 +
>  tools/lib/bpf/linker.c                        |   2 +
>  tools/lib/bpf/relo_core.c                     |  93 ++++---
>  .../selftests/bpf/prog_tests/btf_dump.c       |  10 +-
>  .../selftests/bpf/prog_tests/btf_write.c      |   6 +-
>  10 files changed, 450 insertions(+), 72 deletions(-)
>

This is a huge patch touching very different and logically independent
parts of libbpf. Please split it into smaller parts, e.g.:
  - libbpf.c changes (sanitization and kcfg);
  - BTF public API helpers (btf_is_enum64, btf__add_enum64);
  - btf_dump changes;
  - btf__dedup changes;
  - CO-RE relocations.

It will be easier to discuss each in a separate patch.

[...]

> +static int btf_add_enum_common(struct btf *btf, const char *name,
> +                              bool is_unsigned, __u8 kind, __u32 tsize)
> +{
> +       struct btf_type *t;
> +       int sz, name_off = 0;
> +
> +       if (btf_ensure_modifiable(btf))
> +               return libbpf_err(-ENOMEM);
> +
> +       sz = sizeof(struct btf_type);
> +       t = btf_add_type_mem(btf, sz);
> +       if (!t)
> +               return libbpf_err(-ENOMEM);
> +
> +       if (name && name[0]) {
> +               name_off = btf__add_str(btf, name);
> +               if (name_off < 0)
> +                       return name_off;
> +       }
> +
> +       /* start out with vlen=0; it will be adjusted when adding enum values */
> +       t->name_off = name_off;
> +       t->info = btf_type_info(kind, 0, is_unsigned);

As mentioned on another patch, I think unsigned should be default
(despite UAPI having s32 as type for enum's val), because that's what
we assume in practice. It makes backwards compatibility easier in more
than one place


> +       t->size = tsize;
> +
> +       return btf_commit_type(btf, sz);
> +}
> +
> +/*
> + * Append new BTF_KIND_ENUM type with:
> + *   - *name* - name of the enum, can be NULL or empty for anonymous enums;
> + *   - *is_unsigned* - whether the enum values are unsigned or not;
> + *
> + * Enum initially has no enum values in it (and corresponds to enum forward
> + * declaration). Enumerator values can be added by btf__add_enum64_value()
> + * immediately after btf__add_enum() succeeds.
> + *
> + * Returns:
> + *   - >0, type ID of newly added BTF type;
> + *   - <0, on error.
> + */
> +int btf__add_enum32(struct btf *btf, const char *name, bool is_unsigned)

given it's still BTF_KIND_ENUM in UAPI, let's keep 32-bit ones as just
btf__add_enum()/btf__add_enum_value() and not deprecate anything.
ENUM64 can be thought about as more of a special case, so I think it's
ok.

> +{
> +       return btf_add_enum_common(btf, name, is_unsigned, BTF_KIND_ENUM, 4);
> +}
> +

[...]

>  /*
>   * Append new BTF_KIND_FWD type with:
>   *   - *name*, non-empty/non-NULL name;
> @@ -2242,7 +2419,7 @@ int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind)
>                 /* enum forward in BTF currently is just an enum with no enum
>                  * values; we also assume a standard 4-byte size for it
>                  */
> -               return btf__add_enum(btf, name, sizeof(int));
> +               return btf__add_enum32(btf, name, false);
>         default:
>                 return libbpf_err(-EINVAL);
>         }
> @@ -3485,6 +3662,7 @@ static long btf_hash_enum(struct btf_type *t)
>  /* Check structural equality of two ENUMs. */
>  static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
>  {
> +       const struct btf_enum64 *n1, *n2;
>         const struct btf_enum *m1, *m2;
>         __u16 vlen;
>         int i;
> @@ -3493,26 +3671,40 @@ static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)

they are so different that I think separate btf_equal_enum64() and
similar approaches everywhere makes sense. Yes, it's enum, but in
practice two very different kinds and should be handled differently

>                 return false;
>
>         vlen = btf_vlen(t1);
> -       m1 = btf_enum(t1);
> -       m2 = btf_enum(t2);
> -       for (i = 0; i < vlen; i++) {
> -               if (m1->name_off != m2->name_off || m1->val != m2->val)
> -                       return false;
> -               m1++;
> -               m2++;

[...]

>  enum btf_fwd_kind {
>         BTF_FWD_STRUCT = 0,
> @@ -454,6 +460,11 @@ static inline bool btf_is_enum(const struct btf_type *t)
>         return btf_kind(t) == BTF_KIND_ENUM;
>  }
>
> +static inline bool btf_is_enum64(const struct btf_type *t)
> +{
> +       return btf_kind(t) == BTF_KIND_ENUM64;

please also add #define BTF_KIND_ENUM64 19 to avoid user breakage if
they don't have very latest kernel UAPI header, same as we did for
TYPE_TAG and others

> +}
> +
>  static inline bool btf_is_fwd(const struct btf_type *t)
>  {
>         return btf_kind(t) == BTF_KIND_FWD;

[...]

> @@ -993,8 +996,11 @@ static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
>                                    const struct btf_type *t,
>                                    int lvl)
>  {
> -       const struct btf_enum *v = btf_enum(t);
> +       bool is_unsigned = btf_kflag(t);
> +       const struct btf_enum64 *v64;
> +       const struct btf_enum *v;
>         __u16 vlen = btf_vlen(t);
> +       const char *fmt_str;
>         const char *name;
>         size_t dup_cnt;
>         int i;
> @@ -1005,18 +1011,47 @@ static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
>
>         if (vlen) {
>                 btf_dump_printf(d, " {");
> -               for (i = 0; i < vlen; i++, v++) {
> -                       name = btf_name_of(d, v->name_off);
> -                       /* enumerators share namespace with typedef idents */
> -                       dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
> -                       if (dup_cnt > 1) {
> -                               btf_dump_printf(d, "\n%s%s___%zu = %u,",
> -                                               pfx(lvl + 1), name, dup_cnt,
> -                                               (__u32)v->val);
> -                       } else {
> -                               btf_dump_printf(d, "\n%s%s = %u,",
> -                                               pfx(lvl + 1), name,
> -                                               (__u32)v->val);
> +               if (btf_is_enum(t)) {
> +                       v = btf_enum(t);
> +                       for (i = 0; i < vlen; i++, v++) {
> +                               name = btf_name_of(d, v->name_off);
> +                               /* enumerators share namespace with typedef idents */
> +                               dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
> +                               if (dup_cnt > 1) {
> +                                       fmt_str = is_unsigned ? "\n%s%s___%zu = %u,"
> +                                                             : "\n%s%s___%zu = %d,";
> +                                       btf_dump_printf(d, fmt_str,
> +                                                       pfx(lvl + 1), name, dup_cnt,
> +                                                       v->val);
> +                               } else {
> +                                       fmt_str = is_unsigned ? "\n%s%s = %u,"
> +                                                             : "\n%s%s = %d,";
> +                                       btf_dump_printf(d, fmt_str,
> +                                                       pfx(lvl + 1), name,
> +                                                       v->val);
> +                               }
> +                       }
> +               } else {
> +                       v64 = btf_enum64(t);
> +                       for (i = 0; i < vlen; i++, v64++) {
> +                               __u64 val = btf_enum64_value(v64);
> +
> +                               name = btf_name_of(d, v64->name_off);
> +                               /* enumerators share namespace with typedef idents */
> +                               dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
> +                               if (dup_cnt > 1) {
> +                                       fmt_str = is_unsigned ? "\n%s%s___%zu = %lluULL,"
> +                                                             : "\n%s%s___%zu = %lldLL,";
> +                                       btf_dump_printf(d, fmt_str,
> +                                                       pfx(lvl + 1), name, dup_cnt,
> +                                                       val);
> +                               } else {
> +                                       fmt_str = is_unsigned ? "\n%s%s = %lluULL,"
> +                                                             : "\n%s%s = %lldLL,";
> +                                       btf_dump_printf(d, fmt_str,
> +                                                       pfx(lvl + 1), name,
> +                                                       val);
> +                               }
>                         }

yeah, let's just have btf_dump_emit_enum64_def(), there is very little
that can be reused, I think it will be cleaning to keep enum and
enum64 separate everywhere where we actually need to iterate
enumerators and do something about them

>                 }
>                 btf_dump_printf(d, "\n%s}", pfx(lvl));
> @@ -1183,6 +1218,7 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
>                 case BTF_KIND_UNION:
>                 case BTF_KIND_TYPEDEF:
>                 case BTF_KIND_FLOAT:

[...]

> -       btf_dump_type_values(d, "%d", value);
> +               btf_dump_type_values(d, is_unsigned ? "%u" : "%d", value);
> +       } else {
> +               for (i = 0, e64 = btf_enum64(t); i < btf_vlen(t); i++, e64++) {
> +                       if (value != btf_enum64_value(e64))
> +                               continue;
> +                       btf_dump_type_values(d, "%s", btf_name_of(d, e64->name_off));
> +                       return 0;
> +               }
> +
> +               btf_dump_type_values(d, is_unsigned ? "%lluULL" : "%lldLL", value);
> +       }

ditto, also beware of %lld/%llu use with __u64/__s64, it gives
compilation warnings without cast on some architectures

>         return 0;
>  }
>

[...]

> @@ -2717,6 +2720,17 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
>                         /* replace TYPE_TAG with a CONST */
>                         t->name_off = 0;
>                         t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0);
> +               } else if (!has_enum64 && btf_is_enum(t)) {
> +                       /* clear the kflag */
> +                       t->info &= 0x7fffffff;

please use btf_type_info() helper (defined in libbpf_internal.h) or
just plain BTF_INFO_ENC() like all other cases around instead of
hard-coding magic masks

> +               } else if (!has_enum64 && btf_is_enum64(t)) {
> +                       /* replace ENUM64 with pointer->void's */
> +                       vlen = btf_vlen(t);
> +                       for (j = 0; j <= vlen; j++, t++) {
> +                               t->name_off = 0;
> +                               t->info = BTF_INFO_ENC(BTF_KIND_PTR, 0, 0);
> +                               t->type = 0;
> +                       }

I don't think we can replace each enumerator with a new kind, it
breaks type ID numbering. struct btf_member has matching layout, so we
can replace ENUM64 with UNION (easier to keep offsets as zeroes),
WDYT?

>                 }
>         }
>  }
> @@ -3563,6 +3577,12 @@ static enum kcfg_type find_kcfg_type(const struct btf *btf, int id,
>                 if (strcmp(name, "libbpf_tristate"))
>                         return KCFG_UNKNOWN;
>                 return KCFG_TRISTATE;
> +       case BTF_KIND_ENUM64:
> +               if (t->size != 8)
> +                       return KCFG_UNKNOWN;

I think I don't like this t->size == 8 more and more. At some we'll
decide it's ok and then we'll have to go and adjust everything again.
It requires pretty much zero effort to support from the very beginning
and makes tons of sense to allow that, let's allow it.

> +               if (strcmp(name, "libbpf_tristate"))
> +                       return KCFG_UNKNOWN;
> +               return KCFG_TRISTATE;
>         case BTF_KIND_ARRAY:
>                 if (btf_array(t)->nelems == 0)
>                         return KCFG_UNKNOWN;
> @@ -4746,6 +4766,17 @@ static int probe_kern_bpf_cookie(void)
>         return probe_fd(ret);
>  }
>
> +static int probe_kern_btf_enum64(void)
> +{
> +       static const char strs[] = "\0enum64";
> +       __u32 types[] = {
> +               BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
> +       };
> +
> +       return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
> +                                            strs, sizeof(strs)));
> +}
> +
>  enum kern_feature_result {
>         FEAT_UNKNOWN = 0,
>         FEAT_SUPPORTED = 1,
> @@ -4811,6 +4842,9 @@ static struct kern_feature_desc {
>         [FEAT_BPF_COOKIE] = {
>                 "BPF cookie support", probe_kern_bpf_cookie,
>         },
> +       [FEAT_BTF_ENUM64] = {
> +               "BTF_KIND_ENUM64 support", probe_kern_btf_enum64,
> +       },
>  };
>
>  bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)
> @@ -5296,6 +5330,15 @@ void bpf_core_free_cands(struct bpf_core_cand_list *cands)
>         free(cands);
>  }
>
> +static bool btf_is_enum_enum64(const struct btf_type *t1,
> +                              const struct btf_type *t2) {
> +       if (btf_is_enum(t1) && btf_is_enum64(t2))
> +               return true;
> +       if (btf_is_enum(t2) && btf_is_enum64(t1))
> +               return true;
> +       return false;
> +}
> +

maybe simplify and rename to

static bool btf_are_enums(...) {
    return (btf_is_enum(t1) || btf_is_enum64(t1)) && (same for t2)?
}

>  int bpf_core_add_cands(struct bpf_core_cand *local_cand,
>                        size_t local_essent_len,
>                        const struct btf *targ_btf,
> @@ -5315,8 +5358,10 @@ int bpf_core_add_cands(struct bpf_core_cand *local_cand,
>         n = btf__type_cnt(targ_btf);
>         for (i = targ_start_id; i < n; i++) {
>                 t = btf__type_by_id(targ_btf, i);
> -               if (btf_kind(t) != btf_kind(local_t))
> -                       continue;
> +               if (btf_kind(t) != btf_kind(local_t)) {
> +                       if (!btf_is_enum_enum64(t, local_t))
> +                               continue;
> +               }

let's extract this into a helper and call it btf_kinds_are_compat() or
something along those lines?

>
>                 targ_name = btf__name_by_offset(targ_btf, t->name_off);
>                 if (str_is_empty(targ_name))
> @@ -5529,8 +5574,10 @@ int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
>         /* caller made sure that names match (ignoring flavor suffix) */
>         local_type = btf__type_by_id(local_btf, local_id);
>         targ_type = btf__type_by_id(targ_btf, targ_id);
> -       if (btf_kind(local_type) != btf_kind(targ_type))
> -               return 0;
> +       if (btf_kind(local_type) != btf_kind(targ_type)) {
> +               if (!btf_is_enum_enum64(local_type, targ_type))
> +                       return 0;
> +       }
>
>  recur:
>         depth--;
> @@ -5542,8 +5589,10 @@ int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
>         if (!local_type || !targ_type)
>                 return -EINVAL;
>
> -       if (btf_kind(local_type) != btf_kind(targ_type))
> -               return 0;
> +       if (btf_kind(local_type) != btf_kind(targ_type)) {
> +               if (!btf_is_enum_enum64(local_type, targ_type))
> +                       return 0;
> +       }

and reuse it in many places like here and above

>
>         switch (btf_kind(local_type)) {
>         case BTF_KIND_UNKN:
> @@ -5551,6 +5600,7 @@ int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
>         case BTF_KIND_UNION:
>         case BTF_KIND_ENUM:
>         case BTF_KIND_FWD:
> +       case BTF_KIND_ENUM64:
>                 return 1;
>         case BTF_KIND_INT:
>                 /* just reject deprecated bitfield-like integers; all other
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index b5bc84039407..acde13bd48c8 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -448,6 +448,10 @@ LIBBPF_0.8.0 {
>                 bpf_object__open_subskeleton;
>                 bpf_program__attach_kprobe_multi_opts;
>                 bpf_program__attach_usdt;
> +               btf__add_enum32;
> +               btf__add_enum32_value;
> +               btf__add_enum64;
> +               btf__add_enum64_value;
>                 libbpf_register_prog_handler;
>                 libbpf_unregister_prog_handler;
>  } LIBBPF_0.7.0;
> diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
> index 4abdbe2fea9d..10c16acfa8ae 100644
> --- a/tools/lib/bpf/libbpf_internal.h
> +++ b/tools/lib/bpf/libbpf_internal.h
> @@ -351,6 +351,8 @@ enum kern_feature_id {
>         FEAT_MEMCG_ACCOUNT,
>         /* BPF cookie (bpf_get_attach_cookie() BPF helper) support */
>         FEAT_BPF_COOKIE,
> +       /* BTF_KIND_ENUM64 support and BTF_KIND_ENUM kflag support */
> +       FEAT_BTF_ENUM64,
>         __FEAT_CNT,
>  };
>
> diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
> index 9aa016fb55aa..1e1ef3302921 100644
> --- a/tools/lib/bpf/linker.c
> +++ b/tools/lib/bpf/linker.c
> @@ -1343,6 +1343,7 @@ static bool glob_sym_btf_matches(const char *sym_name, bool exact,
>         case BTF_KIND_FWD:
>         case BTF_KIND_FUNC:
>         case BTF_KIND_VAR:
> +       case BTF_KIND_ENUM64:
>                 n1 = btf__str_by_offset(btf1, t1->name_off);
>                 n2 = btf__str_by_offset(btf2, t2->name_off);
>                 if (strcmp(n1, n2) != 0) {
> @@ -1358,6 +1359,7 @@ static bool glob_sym_btf_matches(const char *sym_name, bool exact,
>         switch (btf_kind(t1)) {
>         case BTF_KIND_UNKN: /* void */
>         case BTF_KIND_FWD:
> +       case BTF_KIND_ENUM64:

this should be lower, along with BTF_KIND_ENUM (btw, maybe keep it
next to BTF_KIND_ENUM64 in switches like this, e.g. in the one right
above in the patch)

>                 return true;
>         case BTF_KIND_INT:
>         case BTF_KIND_FLOAT:
> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
> index f25ffd03c3b1..1e751400427b 100644
> --- a/tools/lib/bpf/relo_core.c
> +++ b/tools/lib/bpf/relo_core.c
> @@ -231,11 +231,15 @@ int bpf_core_parse_spec(const char *prog_name, const struct btf *btf,
>         spec->len++;
>
>         if (core_relo_is_enumval_based(relo->kind)) {
> -               if (!btf_is_enum(t) || spec->raw_len > 1 || access_idx >= btf_vlen(t))
> +               if (!(btf_is_enum(t) || btf_is_enum64(t)) ||
> +                   spec->raw_len > 1 || access_idx >= btf_vlen(t))
>                         return -EINVAL;
>
>                 /* record enumerator name in a first accessor */
> -               acc->name = btf__name_by_offset(btf, btf_enum(t)[access_idx].name_off);
> +               if (btf_is_enum(t))
> +                       acc->name = btf__name_by_offset(btf, btf_enum(t)[access_idx].name_off);
> +               else
> +                       acc->name = btf__name_by_offset(btf, btf_enum64(t)[access_idx].name_off);

mild nit: it seems like extracting name_off into a variable (based on
btf_is_enum(t)) would be a bit cleaner, then just one
btf__name_by_offset() call with that name_off?

>                 return 0;
>         }
>
> @@ -340,15 +344,19 @@ static int bpf_core_fields_are_compat(const struct btf *local_btf,
>
>         if (btf_is_composite(local_type) && btf_is_composite(targ_type))
>                 return 1;
> -       if (btf_kind(local_type) != btf_kind(targ_type))
> -               return 0;
> +       if (btf_kind(local_type) != btf_kind(targ_type)) {
> +               if (btf_is_enum(local_type) && btf_is_enum64(targ_type)) ;
> +               else if (btf_is_enum64(local_type) && btf_is_enum(targ_type)) ;
> +               else return 0;
> +       }

use proposed btf_kinds_are_compat() here?

>
>         switch (btf_kind(local_type)) {
>         case BTF_KIND_PTR:
>         case BTF_KIND_FLOAT:
>                 return 1;
>         case BTF_KIND_FWD:
> -       case BTF_KIND_ENUM: {
> +       case BTF_KIND_ENUM:
> +       case BTF_KIND_ENUM64: {
>                 const char *local_name, *targ_name;
>                 size_t local_len, targ_len;
>
> @@ -494,29 +502,48 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
>
>         if (core_relo_is_enumval_based(local_spec->relo_kind)) {
>                 size_t local_essent_len, targ_essent_len;
> +               const struct btf_enum64 *e64;
>                 const struct btf_enum *e;
>                 const char *targ_name;
>
>                 /* has to resolve to an enum */
>                 targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id, &targ_id);
> -               if (!btf_is_enum(targ_type))
> +               if (!btf_is_enum(targ_type) && !btf_is_enum64(targ_type))
>                         return 0;
>
>                 local_essent_len = bpf_core_essential_name_len(local_acc->name);
>
> -               for (i = 0, e = btf_enum(targ_type); i < btf_vlen(targ_type); i++, e++) {
> -                       targ_name = btf__name_by_offset(targ_spec->btf, e->name_off);
> -                       targ_essent_len = bpf_core_essential_name_len(targ_name);
> -                       if (targ_essent_len != local_essent_len)
> -                               continue;
> -                       if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) {


so idea here is to find enumerator with matching name and record its
name and position, let's extract that part of the logic into a helper
and keep the targ_acc/targ_spec initialization in one piece. It will
be easier to follow the intent and less opportunity to get out of
sync.

> -                               targ_acc->type_id = targ_id;
> -                               targ_acc->idx = i;
> -                               targ_acc->name = targ_name;
> -                               targ_spec->len++;
> -                               targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
> -                               targ_spec->raw_len++;
> -                               return 1;
> +               if (btf_is_enum(targ_type)) {
> +                       for (i = 0, e = btf_enum(targ_type); i < btf_vlen(targ_type); i++, e++) {
> +                               targ_name = btf__name_by_offset(targ_spec->btf, e->name_off);
> +                               targ_essent_len = bpf_core_essential_name_len(targ_name);
> +                               if (targ_essent_len != local_essent_len)
> +                                       continue;
> +                               if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) {
> +                                       targ_acc->type_id = targ_id;
> +                                       targ_acc->idx = i;
> +                                       targ_acc->name = targ_name;
> +                                       targ_spec->len++;
> +                                       targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
> +                                       targ_spec->raw_len++;
> +                                       return 1;
> +                               }
> +                       }
> +               } else {
> +                       for (i = 0, e64 = btf_enum64(targ_type); i < btf_vlen(targ_type); i++, e64++) {
> +                               targ_name = btf__name_by_offset(targ_spec->btf, e64->name_off);
> +                               targ_essent_len = bpf_core_essential_name_len(targ_name);
> +                               if (targ_essent_len != local_essent_len)
> +                                       continue;
> +                               if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) {
> +                                       targ_acc->type_id = targ_id;
> +                                       targ_acc->idx = i;
> +                                       targ_acc->name = targ_name;
> +                                       targ_spec->len++;
> +                                       targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
> +                                       targ_spec->raw_len++;
> +                                       return 1;
> +                               }
>                         }
>                 }
>                 return 0;
> @@ -681,7 +708,7 @@ static int bpf_core_calc_field_relo(const char *prog_name,
>                 break;
>         case BPF_CORE_FIELD_SIGNED:
>                 /* enums will be assumed unsigned */
> -               *val = btf_is_enum(mt) ||
> +               *val = btf_is_enum(mt) || btf_is_enum64(mt) ||
>                        (btf_int_encoding(mt) & BTF_INT_SIGNED);
>                 if (validate)
>                         *validate = true; /* signedness is never ambiguous */
> @@ -753,6 +780,7 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
>                                       const struct bpf_core_spec *spec,
>                                       __u64 *val)
>  {
> +       const struct btf_enum64 *e64;
>         const struct btf_type *t;
>         const struct btf_enum *e;
>
> @@ -764,8 +792,13 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
>                 if (!spec)
>                         return -EUCLEAN; /* request instruction poisoning */
>                 t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
> -               e = btf_enum(t) + spec->spec[0].idx;
> -               *val = e->val;
> +               if (btf_is_enum(t)) {
> +                       e = btf_enum(t) + spec->spec[0].idx;
> +                       *val = e->val;
> +               } else {
> +                       e64 = btf_enum64(t) + spec->spec[0].idx;
> +                       *val = btf_enum64_value(e64);
> +               }

I think with sign bit we now have further complication: for 32-bit
enums we need to sign extend 32-bit values to s64 and then cast as
u64, no? Seems like a helper to abstract that is good to have here.
Otherwise relocating enum ABC { D = -1 } will produce invalid ldimm64
instruction, right?

Also keep in mind that you can use btf_enum()/btf_enum64() as an
array, so above you can write just as

*val = btf_is_enum(t)
    ? btf_enum(t)[spec->spec[0].idx]
    : btf_enum64(t)[spec->spec[0].idx];

But we need sign check and extension, so better to have a separate helper.

>                 break;
>         default:
>                 return -EOPNOTSUPP;
> @@ -1034,7 +1067,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
>                 }
>
>                 insn[0].imm = new_val;
> -               insn[1].imm = 0; /* currently only 32-bit values are supported */
> +               insn[1].imm = new_val >> 32;

for 32-bit instructions (ALU/ALU32, etc) we need to make sure that
new_val fits in 32 bits. And we need to be careful about
signed/unsigned, because for signed case all-zero or all-one upper 32
bits are ok (sign extension). Can we know the expected signed/unsigned
operation from bpf_insn itself? We should be, right?

>                 pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %llu\n",
>                          prog_name, relo_idx, insn_idx,
>                          (unsigned long long)imm, new_val);
> @@ -1056,6 +1089,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
>   */
>  int bpf_core_format_spec(char *buf, size_t buf_sz, const struct bpf_core_spec *spec)
>  {
> +       const struct btf_enum64 *e64;
>         const struct btf_type *t;
>         const struct btf_enum *e;
>         const char *s;
> @@ -1086,10 +1120,15 @@ int bpf_core_format_spec(char *buf, size_t buf_sz, const struct bpf_core_spec *s
>
>         if (core_relo_is_enumval_based(spec->relo_kind)) {
>                 t = skip_mods_and_typedefs(spec->btf, type_id, NULL);
> -               e = btf_enum(t) + spec->raw_spec[0];
> -               s = btf__name_by_offset(spec->btf, e->name_off);
> -
> -               append_buf("::%s = %u", s, e->val);
> +               if (btf_is_enum(t)) {
> +                       e = btf_enum(t) + spec->raw_spec[0];
> +                       s = btf__name_by_offset(spec->btf, e->name_off);
> +                       append_buf("::%s = %u", s, e->val);
> +               } else {
> +                       e64 = btf_enum64(t) + spec->raw_spec[0];
> +                       s = btf__name_by_offset(spec->btf, e64->name_off);
> +                       append_buf("::%s = %llu", s, btf_enum64_value(e64));

%llu problem here again

> +               }
>                 return len;
>         }
>

[...]
Yonghong Song May 10, 2022, 10:40 p.m. UTC | #4
On 5/9/22 4:25 PM, Andrii Nakryiko wrote:
> On Sun, May 1, 2022 at 12:00 PM Yonghong Song <yhs@fb.com> wrote:
>>
>> Add BTF_KIND_ENUM64 support. Deprecated btf__add_enum() and
>> btf__add_enum_value() and introduced the following new APIs
>>    btf__add_enum32()
>>    btf__add_enum32_value()
>>    btf__add_enum64()
>>    btf__add_enum64_value()
>> due to new kind and introduction of kflag.
>>
>> To support old kernel with enum64, the sanitization is
>> added to replace BTF_KIND_ENUM64 with a bunch of
>> pointer-to-void types.
>>
>> The enum64 value relocation is also supported. The enum64
>> forward resolution, with enum type as forward declaration
>> and enum64 as the actual definition, is also supported.
>>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
>>   tools/lib/bpf/btf.c                           | 226 +++++++++++++++++-
>>   tools/lib/bpf/btf.h                           |  21 ++
>>   tools/lib/bpf/btf_dump.c                      |  94 ++++++--
>>   tools/lib/bpf/libbpf.c                        |  64 ++++-
>>   tools/lib/bpf/libbpf.map                      |   4 +
>>   tools/lib/bpf/libbpf_internal.h               |   2 +
>>   tools/lib/bpf/linker.c                        |   2 +
>>   tools/lib/bpf/relo_core.c                     |  93 ++++---
>>   .../selftests/bpf/prog_tests/btf_dump.c       |  10 +-
>>   .../selftests/bpf/prog_tests/btf_write.c      |   6 +-
>>   10 files changed, 450 insertions(+), 72 deletions(-)
>>
> 
> This is a huge patch touching very different and logically independent
> parts of libbpf. Please split it into smaller parts, e.g.:
>    - libbpf.c changes (sanitization and kcfg);
>    - BTF public API helpers (btf_is_enum64, btf__add_enum64);
>    - btf_dump changes;
>    - btf__dedup changes;
>    - CO-RE relocations.
> 
> It will be easier to discuss each in a separate patch.

okay.

> 
> [...]
> 
>> +static int btf_add_enum_common(struct btf *btf, const char *name,
>> +                              bool is_unsigned, __u8 kind, __u32 tsize)
>> +{
>> +       struct btf_type *t;
>> +       int sz, name_off = 0;
>> +
>> +       if (btf_ensure_modifiable(btf))
>> +               return libbpf_err(-ENOMEM);
>> +
>> +       sz = sizeof(struct btf_type);
>> +       t = btf_add_type_mem(btf, sz);
>> +       if (!t)
>> +               return libbpf_err(-ENOMEM);
>> +
>> +       if (name && name[0]) {
>> +               name_off = btf__add_str(btf, name);
>> +               if (name_off < 0)
>> +                       return name_off;
>> +       }
>> +
>> +       /* start out with vlen=0; it will be adjusted when adding enum values */
>> +       t->name_off = name_off;
>> +       t->info = btf_type_info(kind, 0, is_unsigned);
> 
> As mentioned on another patch, I think unsigned should be default
> (despite UAPI having s32 as type for enum's val), because that's what
> we assume in practice. It makes backwards compatibility easier in more
> than one place

okay.

> 
> 
>> +       t->size = tsize;
>> +
>> +       return btf_commit_type(btf, sz);
>> +}
>> +
>> +/*
>> + * Append new BTF_KIND_ENUM type with:
>> + *   - *name* - name of the enum, can be NULL or empty for anonymous enums;
>> + *   - *is_unsigned* - whether the enum values are unsigned or not;
>> + *
>> + * Enum initially has no enum values in it (and corresponds to enum forward
>> + * declaration). Enumerator values can be added by btf__add_enum64_value()
>> + * immediately after btf__add_enum() succeeds.
>> + *
>> + * Returns:
>> + *   - >0, type ID of newly added BTF type;
>> + *   - <0, on error.
>> + */
>> +int btf__add_enum32(struct btf *btf, const char *name, bool is_unsigned)
> 
> given it's still BTF_KIND_ENUM in UAPI, let's keep 32-bit ones as just
> btf__add_enum()/btf__add_enum_value() and not deprecate anything.
> ENUM64 can be thought about as more of a special case, so I think it's
> ok.

The current btf__add_enum api:
LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32 
bytes_sz);

The issue is it doesn't have signedness parameter. if the user input
is
    enum { A = -1, B = 0, C = 1 };
the actual printout btf format will be
    enum { A 4294967295, B = 0, C = 1}
does not match the original source.

> 
>> +{
>> +       return btf_add_enum_common(btf, name, is_unsigned, BTF_KIND_ENUM, 4);
>> +}
>> +
> 
> [...]
> 
>>   /*
>>    * Append new BTF_KIND_FWD type with:
>>    *   - *name*, non-empty/non-NULL name;
>> @@ -2242,7 +2419,7 @@ int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind)
>>                  /* enum forward in BTF currently is just an enum with no enum
>>                   * values; we also assume a standard 4-byte size for it
>>                   */
>> -               return btf__add_enum(btf, name, sizeof(int));
>> +               return btf__add_enum32(btf, name, false);
>>          default:
>>                  return libbpf_err(-EINVAL);
>>          }
>> @@ -3485,6 +3662,7 @@ static long btf_hash_enum(struct btf_type *t)
>>   /* Check structural equality of two ENUMs. */
>>   static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
>>   {
>> +       const struct btf_enum64 *n1, *n2;
>>          const struct btf_enum *m1, *m2;
>>          __u16 vlen;
>>          int i;
>> @@ -3493,26 +3671,40 @@ static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
> 
> they are so different that I think separate btf_equal_enum64() and
> similar approaches everywhere makes sense. Yes, it's enum, but in
> practice two very different kinds and should be handled differently

okay.

> 
>>                  return false;
>>
>>          vlen = btf_vlen(t1);
>> -       m1 = btf_enum(t1);
>> -       m2 = btf_enum(t2);
>> -       for (i = 0; i < vlen; i++) {
>> -               if (m1->name_off != m2->name_off || m1->val != m2->val)
>> -                       return false;
>> -               m1++;
>> -               m2++;
> 
> [...]
> 
>>   enum btf_fwd_kind {
>>          BTF_FWD_STRUCT = 0,
>> @@ -454,6 +460,11 @@ static inline bool btf_is_enum(const struct btf_type *t)
>>          return btf_kind(t) == BTF_KIND_ENUM;
>>   }
>>
>> +static inline bool btf_is_enum64(const struct btf_type *t)
>> +{
>> +       return btf_kind(t) == BTF_KIND_ENUM64;
> 
> please also add #define BTF_KIND_ENUM64 19 to avoid user breakage if
> they don't have very latest kernel UAPI header, same as we did for
> TYPE_TAG and others

okay.

> 
>> +}
>> +
>>   static inline bool btf_is_fwd(const struct btf_type *t)
>>   {
>>          return btf_kind(t) == BTF_KIND_FWD;
> 
> [...]
> 
>> @@ -993,8 +996,11 @@ static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
>>                                     const struct btf_type *t,
>>                                     int lvl)
>>   {
>> -       const struct btf_enum *v = btf_enum(t);
>> +       bool is_unsigned = btf_kflag(t);
>> +       const struct btf_enum64 *v64;
>> +       const struct btf_enum *v;
>>          __u16 vlen = btf_vlen(t);
>> +       const char *fmt_str;
>>          const char *name;
>>          size_t dup_cnt;
>>          int i;
>> @@ -1005,18 +1011,47 @@ static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
>>
>>          if (vlen) {
>>                  btf_dump_printf(d, " {");
>> -               for (i = 0; i < vlen; i++, v++) {
>> -                       name = btf_name_of(d, v->name_off);
>> -                       /* enumerators share namespace with typedef idents */
>> -                       dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
>> -                       if (dup_cnt > 1) {
>> -                               btf_dump_printf(d, "\n%s%s___%zu = %u,",
>> -                                               pfx(lvl + 1), name, dup_cnt,
>> -                                               (__u32)v->val);
>> -                       } else {
>> -                               btf_dump_printf(d, "\n%s%s = %u,",
>> -                                               pfx(lvl + 1), name,
>> -                                               (__u32)v->val);
>> +               if (btf_is_enum(t)) {
>> +                       v = btf_enum(t);
>> +                       for (i = 0; i < vlen; i++, v++) {
>> +                               name = btf_name_of(d, v->name_off);
>> +                               /* enumerators share namespace with typedef idents */
>> +                               dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
>> +                               if (dup_cnt > 1) {
>> +                                       fmt_str = is_unsigned ? "\n%s%s___%zu = %u,"
>> +                                                             : "\n%s%s___%zu = %d,";
>> +                                       btf_dump_printf(d, fmt_str,
>> +                                                       pfx(lvl + 1), name, dup_cnt,
>> +                                                       v->val);
>> +                               } else {
>> +                                       fmt_str = is_unsigned ? "\n%s%s = %u,"
>> +                                                             : "\n%s%s = %d,";
>> +                                       btf_dump_printf(d, fmt_str,
>> +                                                       pfx(lvl + 1), name,
>> +                                                       v->val);
>> +                               }
>> +                       }
>> +               } else {
>> +                       v64 = btf_enum64(t);
>> +                       for (i = 0; i < vlen; i++, v64++) {
>> +                               __u64 val = btf_enum64_value(v64);
>> +
>> +                               name = btf_name_of(d, v64->name_off);
>> +                               /* enumerators share namespace with typedef idents */
>> +                               dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
>> +                               if (dup_cnt > 1) {
>> +                                       fmt_str = is_unsigned ? "\n%s%s___%zu = %lluULL,"
>> +                                                             : "\n%s%s___%zu = %lldLL,";
>> +                                       btf_dump_printf(d, fmt_str,
>> +                                                       pfx(lvl + 1), name, dup_cnt,
>> +                                                       val);
>> +                               } else {
>> +                                       fmt_str = is_unsigned ? "\n%s%s = %lluULL,"
>> +                                                             : "\n%s%s = %lldLL,";
>> +                                       btf_dump_printf(d, fmt_str,
>> +                                                       pfx(lvl + 1), name,
>> +                                                       val);
>> +                               }
>>                          }
> 
> yeah, let's just have btf_dump_emit_enum64_def(), there is very little
> that can be reused, I think it will be cleaning to keep enum and
> enum64 separate everywhere where we actually need to iterate
> enumerators and do something about them

okay.

> 
>>                  }
>>                  btf_dump_printf(d, "\n%s}", pfx(lvl));
>> @@ -1183,6 +1218,7 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
>>                  case BTF_KIND_UNION:
>>                  case BTF_KIND_TYPEDEF:
>>                  case BTF_KIND_FLOAT:
> 
> [...]
> 
>> -       btf_dump_type_values(d, "%d", value);
>> +               btf_dump_type_values(d, is_unsigned ? "%u" : "%d", value);
>> +       } else {
>> +               for (i = 0, e64 = btf_enum64(t); i < btf_vlen(t); i++, e64++) {
>> +                       if (value != btf_enum64_value(e64))
>> +                               continue;
>> +                       btf_dump_type_values(d, "%s", btf_name_of(d, e64->name_off));
>> +                       return 0;
>> +               }
>> +
>> +               btf_dump_type_values(d, is_unsigned ? "%lluULL" : "%lldLL", value);
>> +       }
> 
> ditto, also beware of %lld/%llu use with __u64/__s64, it gives
> compilation warnings without cast on some architectures

okay.

> 
>>          return 0;
>>   }
>>
> 
> [...]
> 
>> @@ -2717,6 +2720,17 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
>>                          /* replace TYPE_TAG with a CONST */
>>                          t->name_off = 0;
>>                          t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0);
>> +               } else if (!has_enum64 && btf_is_enum(t)) {
>> +                       /* clear the kflag */
>> +                       t->info &= 0x7fffffff;
> 
> please use btf_type_info() helper (defined in libbpf_internal.h) or
> just plain BTF_INFO_ENC() like all other cases around instead of
> hard-coding magic masks

okay.

> 
>> +               } else if (!has_enum64 && btf_is_enum64(t)) {
>> +                       /* replace ENUM64 with pointer->void's */
>> +                       vlen = btf_vlen(t);
>> +                       for (j = 0; j <= vlen; j++, t++) {
>> +                               t->name_off = 0;
>> +                               t->info = BTF_INFO_ENC(BTF_KIND_PTR, 0, 0);
>> +                               t->type = 0;
>> +                       }
> 
> I don't think we can replace each enumerator with a new kind, it
> breaks type ID numbering. struct btf_member has matching layout, so we
> can replace ENUM64 with UNION (easier to keep offsets as zeroes),
> WDYT?

Yes, my above approach won't work. I will replace it with UNION with
members be int/ptr types.

> 
>>                  }
>>          }
>>   }
>> @@ -3563,6 +3577,12 @@ static enum kcfg_type find_kcfg_type(const struct btf *btf, int id,
>>                  if (strcmp(name, "libbpf_tristate"))
>>                          return KCFG_UNKNOWN;
>>                  return KCFG_TRISTATE;
>> +       case BTF_KIND_ENUM64:
>> +               if (t->size != 8)
>> +                       return KCFG_UNKNOWN;
> 
> I think I don't like this t->size == 8 more and more. At some we'll
> decide it's ok and then we'll have to go and adjust everything again.
> It requires pretty much zero effort to support from the very beginning
> and makes tons of sense to allow that, let's allow it.

Will remove this.

> 
>> +               if (strcmp(name, "libbpf_tristate"))
>> +                       return KCFG_UNKNOWN;
>> +               return KCFG_TRISTATE;
>>          case BTF_KIND_ARRAY:
>>                  if (btf_array(t)->nelems == 0)
>>                          return KCFG_UNKNOWN;
>> @@ -4746,6 +4766,17 @@ static int probe_kern_bpf_cookie(void)
>>          return probe_fd(ret);
>>   }
>>
>> +static int probe_kern_btf_enum64(void)
>> +{
>> +       static const char strs[] = "\0enum64";
>> +       __u32 types[] = {
>> +               BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
>> +       };
>> +
>> +       return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
>> +                                            strs, sizeof(strs)));
>> +}
>> +
>>   enum kern_feature_result {
>>          FEAT_UNKNOWN = 0,
>>          FEAT_SUPPORTED = 1,
>> @@ -4811,6 +4842,9 @@ static struct kern_feature_desc {
>>          [FEAT_BPF_COOKIE] = {
>>                  "BPF cookie support", probe_kern_bpf_cookie,
>>          },
>> +       [FEAT_BTF_ENUM64] = {
>> +               "BTF_KIND_ENUM64 support", probe_kern_btf_enum64,
>> +       },
>>   };
>>
>>   bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)
>> @@ -5296,6 +5330,15 @@ void bpf_core_free_cands(struct bpf_core_cand_list *cands)
>>          free(cands);
>>   }
>>
>> +static bool btf_is_enum_enum64(const struct btf_type *t1,
>> +                              const struct btf_type *t2) {
>> +       if (btf_is_enum(t1) && btf_is_enum64(t2))
>> +               return true;
>> +       if (btf_is_enum(t2) && btf_is_enum64(t1))
>> +               return true;
>> +       return false;
>> +}
>> +
> 
> maybe simplify and rename to
> 
> static bool btf_are_enums(...) {
>      return (btf_is_enum(t1) || btf_is_enum64(t1)) && (same for t2)?
> }

Right this can be simplified.

> 
>>   int bpf_core_add_cands(struct bpf_core_cand *local_cand,
>>                         size_t local_essent_len,
>>                         const struct btf *targ_btf,
>> @@ -5315,8 +5358,10 @@ int bpf_core_add_cands(struct bpf_core_cand *local_cand,
>>          n = btf__type_cnt(targ_btf);
>>          for (i = targ_start_id; i < n; i++) {
>>                  t = btf__type_by_id(targ_btf, i);
>> -               if (btf_kind(t) != btf_kind(local_t))
>> -                       continue;
>> +               if (btf_kind(t) != btf_kind(local_t)) {
>> +                       if (!btf_is_enum_enum64(t, local_t))
>> +                               continue;
>> +               }
> 
> let's extract this into a helper and call it btf_kinds_are_compat() or
> something along those lines?

okay.

> 
>>
>>                  targ_name = btf__name_by_offset(targ_btf, t->name_off);
>>                  if (str_is_empty(targ_name))
>> @@ -5529,8 +5574,10 @@ int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
>>          /* caller made sure that names match (ignoring flavor suffix) */
>>          local_type = btf__type_by_id(local_btf, local_id);
>>          targ_type = btf__type_by_id(targ_btf, targ_id);
>> -       if (btf_kind(local_type) != btf_kind(targ_type))
>> -               return 0;
>> +       if (btf_kind(local_type) != btf_kind(targ_type)) {
>> +               if (!btf_is_enum_enum64(local_type, targ_type))
>> +                       return 0;
>> +       }
>>
>>   recur:
>>          depth--;
>> @@ -5542,8 +5589,10 @@ int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
>>          if (!local_type || !targ_type)
>>                  return -EINVAL;
>>
>> -       if (btf_kind(local_type) != btf_kind(targ_type))
>> -               return 0;
>> +       if (btf_kind(local_type) != btf_kind(targ_type)) {
>> +               if (!btf_is_enum_enum64(local_type, targ_type))
>> +                       return 0;
>> +       }
> 
> and reuse it in many places like here and above

ditto.

> 
>>
>>          switch (btf_kind(local_type)) {
>>          case BTF_KIND_UNKN:
>> @@ -5551,6 +5600,7 @@ int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
>>          case BTF_KIND_UNION:
>>          case BTF_KIND_ENUM:
>>          case BTF_KIND_FWD:
>> +       case BTF_KIND_ENUM64:
>>                  return 1;
>>          case BTF_KIND_INT:
>>                  /* just reject deprecated bitfield-like integers; all other
>> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
>> index b5bc84039407..acde13bd48c8 100644
>> --- a/tools/lib/bpf/libbpf.map
>> +++ b/tools/lib/bpf/libbpf.map
>> @@ -448,6 +448,10 @@ LIBBPF_0.8.0 {
>>                  bpf_object__open_subskeleton;
>>                  bpf_program__attach_kprobe_multi_opts;
>>                  bpf_program__attach_usdt;
>> +               btf__add_enum32;
>> +               btf__add_enum32_value;
>> +               btf__add_enum64;
>> +               btf__add_enum64_value;
>>                  libbpf_register_prog_handler;
>>                  libbpf_unregister_prog_handler;
>>   } LIBBPF_0.7.0;
>> diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
>> index 4abdbe2fea9d..10c16acfa8ae 100644
>> --- a/tools/lib/bpf/libbpf_internal.h
>> +++ b/tools/lib/bpf/libbpf_internal.h
>> @@ -351,6 +351,8 @@ enum kern_feature_id {
>>          FEAT_MEMCG_ACCOUNT,
>>          /* BPF cookie (bpf_get_attach_cookie() BPF helper) support */
>>          FEAT_BPF_COOKIE,
>> +       /* BTF_KIND_ENUM64 support and BTF_KIND_ENUM kflag support */
>> +       FEAT_BTF_ENUM64,
>>          __FEAT_CNT,
>>   };
>>
>> diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
>> index 9aa016fb55aa..1e1ef3302921 100644
>> --- a/tools/lib/bpf/linker.c
>> +++ b/tools/lib/bpf/linker.c
>> @@ -1343,6 +1343,7 @@ static bool glob_sym_btf_matches(const char *sym_name, bool exact,
>>          case BTF_KIND_FWD:
>>          case BTF_KIND_FUNC:
>>          case BTF_KIND_VAR:
>> +       case BTF_KIND_ENUM64:
>>                  n1 = btf__str_by_offset(btf1, t1->name_off);
>>                  n2 = btf__str_by_offset(btf2, t2->name_off);
>>                  if (strcmp(n1, n2) != 0) {
>> @@ -1358,6 +1359,7 @@ static bool glob_sym_btf_matches(const char *sym_name, bool exact,
>>          switch (btf_kind(t1)) {
>>          case BTF_KIND_UNKN: /* void */
>>          case BTF_KIND_FWD:
>> +       case BTF_KIND_ENUM64:
> 
> this should be lower, along with BTF_KIND_ENUM (btw, maybe keep it
> next to BTF_KIND_ENUM64 in switches like this, e.g. in the one right
> above in the patch)

My mistake. Will fix.

> 
>>                  return true;
>>          case BTF_KIND_INT:
>>          case BTF_KIND_FLOAT:
>> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
>> index f25ffd03c3b1..1e751400427b 100644
>> --- a/tools/lib/bpf/relo_core.c
>> +++ b/tools/lib/bpf/relo_core.c
>> @@ -231,11 +231,15 @@ int bpf_core_parse_spec(const char *prog_name, const struct btf *btf,
>>          spec->len++;
>>
>>          if (core_relo_is_enumval_based(relo->kind)) {
>> -               if (!btf_is_enum(t) || spec->raw_len > 1 || access_idx >= btf_vlen(t))
>> +               if (!(btf_is_enum(t) || btf_is_enum64(t)) ||
>> +                   spec->raw_len > 1 || access_idx >= btf_vlen(t))
>>                          return -EINVAL;
>>
>>                  /* record enumerator name in a first accessor */
>> -               acc->name = btf__name_by_offset(btf, btf_enum(t)[access_idx].name_off);
>> +               if (btf_is_enum(t))
>> +                       acc->name = btf__name_by_offset(btf, btf_enum(t)[access_idx].name_off);
>> +               else
>> +                       acc->name = btf__name_by_offset(btf, btf_enum64(t)[access_idx].name_off);
> 
> mild nit: it seems like extracting name_off into a variable (based on
> btf_is_enum(t)) would be a bit cleaner, then just one
> btf__name_by_offset() call with that name_off?

Will do.

> 
>>                  return 0;
>>          }
>>
>> @@ -340,15 +344,19 @@ static int bpf_core_fields_are_compat(const struct btf *local_btf,
>>
>>          if (btf_is_composite(local_type) && btf_is_composite(targ_type))
>>                  return 1;
>> -       if (btf_kind(local_type) != btf_kind(targ_type))
>> -               return 0;
>> +       if (btf_kind(local_type) != btf_kind(targ_type)) {
>> +               if (btf_is_enum(local_type) && btf_is_enum64(targ_type)) ;
>> +               else if (btf_is_enum64(local_type) && btf_is_enum(targ_type)) ;
>> +               else return 0;
>> +       }
> 
> use proposed btf_kinds_are_compat() here?

Right. Can do this.

> 
>>
>>          switch (btf_kind(local_type)) {
>>          case BTF_KIND_PTR:
>>          case BTF_KIND_FLOAT:
>>                  return 1;
>>          case BTF_KIND_FWD:
>> -       case BTF_KIND_ENUM: {
>> +       case BTF_KIND_ENUM:
>> +       case BTF_KIND_ENUM64: {
>>                  const char *local_name, *targ_name;
>>                  size_t local_len, targ_len;
>>
>> @@ -494,29 +502,48 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
>>
>>          if (core_relo_is_enumval_based(local_spec->relo_kind)) {
>>                  size_t local_essent_len, targ_essent_len;
>> +               const struct btf_enum64 *e64;
>>                  const struct btf_enum *e;
>>                  const char *targ_name;
>>
>>                  /* has to resolve to an enum */
>>                  targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id, &targ_id);
>> -               if (!btf_is_enum(targ_type))
>> +               if (!btf_is_enum(targ_type) && !btf_is_enum64(targ_type))
>>                          return 0;
>>
>>                  local_essent_len = bpf_core_essential_name_len(local_acc->name);
>>
>> -               for (i = 0, e = btf_enum(targ_type); i < btf_vlen(targ_type); i++, e++) {
>> -                       targ_name = btf__name_by_offset(targ_spec->btf, e->name_off);
>> -                       targ_essent_len = bpf_core_essential_name_len(targ_name);
>> -                       if (targ_essent_len != local_essent_len)
>> -                               continue;
>> -                       if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) {
> 
> 
> so idea here is to find enumerator with matching name and record its
> name and position, let's extract that part of the logic into a helper
> and keep the targ_acc/targ_spec initialization in one piece. It will
> be easier to follow the intent and less opportunity to get out of
> sync.

Will do.

> 
>> -                               targ_acc->type_id = targ_id;
>> -                               targ_acc->idx = i;
>> -                               targ_acc->name = targ_name;
>> -                               targ_spec->len++;
>> -                               targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
>> -                               targ_spec->raw_len++;
>> -                               return 1;
>> +               if (btf_is_enum(targ_type)) {
>> +                       for (i = 0, e = btf_enum(targ_type); i < btf_vlen(targ_type); i++, e++) {
>> +                               targ_name = btf__name_by_offset(targ_spec->btf, e->name_off);
>> +                               targ_essent_len = bpf_core_essential_name_len(targ_name);
>> +                               if (targ_essent_len != local_essent_len)
>> +                                       continue;
>> +                               if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) {
>> +                                       targ_acc->type_id = targ_id;
>> +                                       targ_acc->idx = i;
>> +                                       targ_acc->name = targ_name;
>> +                                       targ_spec->len++;
>> +                                       targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
>> +                                       targ_spec->raw_len++;
>> +                                       return 1;
>> +                               }
>> +                       }
>> +               } else {
>> +                       for (i = 0, e64 = btf_enum64(targ_type); i < btf_vlen(targ_type); i++, e64++) {
>> +                               targ_name = btf__name_by_offset(targ_spec->btf, e64->name_off);
>> +                               targ_essent_len = bpf_core_essential_name_len(targ_name);
>> +                               if (targ_essent_len != local_essent_len)
>> +                                       continue;
>> +                               if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) {
>> +                                       targ_acc->type_id = targ_id;
>> +                                       targ_acc->idx = i;
>> +                                       targ_acc->name = targ_name;
>> +                                       targ_spec->len++;
>> +                                       targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
>> +                                       targ_spec->raw_len++;
>> +                                       return 1;
>> +                               }
>>                          }
>>                  }
>>                  return 0;
>> @@ -681,7 +708,7 @@ static int bpf_core_calc_field_relo(const char *prog_name,
>>                  break;
>>          case BPF_CORE_FIELD_SIGNED:
>>                  /* enums will be assumed unsigned */
>> -               *val = btf_is_enum(mt) ||
>> +               *val = btf_is_enum(mt) || btf_is_enum64(mt) ||
>>                         (btf_int_encoding(mt) & BTF_INT_SIGNED);
>>                  if (validate)
>>                          *validate = true; /* signedness is never ambiguous */
>> @@ -753,6 +780,7 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
>>                                        const struct bpf_core_spec *spec,
>>                                        __u64 *val)
>>   {
>> +       const struct btf_enum64 *e64;
>>          const struct btf_type *t;
>>          const struct btf_enum *e;
>>
>> @@ -764,8 +792,13 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
>>                  if (!spec)
>>                          return -EUCLEAN; /* request instruction poisoning */
>>                  t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
>> -               e = btf_enum(t) + spec->spec[0].idx;
>> -               *val = e->val;
>> +               if (btf_is_enum(t)) {
>> +                       e = btf_enum(t) + spec->spec[0].idx;
>> +                       *val = e->val;
>> +               } else {
>> +                       e64 = btf_enum64(t) + spec->spec[0].idx;
>> +                       *val = btf_enum64_value(e64);
>> +               }
> 
> I think with sign bit we now have further complication: for 32-bit
> enums we need to sign extend 32-bit values to s64 and then cast as
> u64, no? Seems like a helper to abstract that is good to have here.
> Otherwise relocating enum ABC { D = -1 } will produce invalid ldimm64
> instruction, right?

We should be fine here. For enum32, we have
struct btf_enum {
         __u32   name_off;
         __s32   val;
};
So above *val = e->val will first sign extend from __s32 to __s64
and then the __u64. Let me have a helper with additional comments
to make it clear.

> 
> Also keep in mind that you can use btf_enum()/btf_enum64() as an
> array, so above you can write just as
> 
> *val = btf_is_enum(t)
>      ? btf_enum(t)[spec->spec[0].idx]
>      : btf_enum64(t)[spec->spec[0].idx];
> 
> But we need sign check and extension, so better to have a separate helper.
> 
>>                  break;
>>          default:
>>                  return -EOPNOTSUPP;
>> @@ -1034,7 +1067,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
>>                  }
>>
>>                  insn[0].imm = new_val;
>> -               insn[1].imm = 0; /* currently only 32-bit values are supported */
>> +               insn[1].imm = new_val >> 32;
> 
> for 32-bit instructions (ALU/ALU32, etc) we need to make sure that
> new_val fits in 32 bits. And we need to be careful about
> signed/unsigned, because for signed case all-zero or all-one upper 32
> bits are ok (sign extension). Can we know the expected signed/unsigned
> operation from bpf_insn itself? We should be, right?

The core relocation insn for constant is
   move r1, <32bit value>
or
   ldimm_64 r1, <64bit value>
and there are no signedness information.
So the 64bit value (except sign extension) can only from
ldimm_64. We should be okay here, but I can double check.

> 
>>                  pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %llu\n",
>>                           prog_name, relo_idx, insn_idx,
>>                           (unsigned long long)imm, new_val);
>> @@ -1056,6 +1089,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
>>    */
>>   int bpf_core_format_spec(char *buf, size_t buf_sz, const struct bpf_core_spec *spec)
>>   {
>> +       const struct btf_enum64 *e64;
>>          const struct btf_type *t;
>>          const struct btf_enum *e;
>>          const char *s;
>> @@ -1086,10 +1120,15 @@ int bpf_core_format_spec(char *buf, size_t buf_sz, const struct bpf_core_spec *s
>>
>>          if (core_relo_is_enumval_based(spec->relo_kind)) {
>>                  t = skip_mods_and_typedefs(spec->btf, type_id, NULL);
>> -               e = btf_enum(t) + spec->raw_spec[0];
>> -               s = btf__name_by_offset(spec->btf, e->name_off);
>> -
>> -               append_buf("::%s = %u", s, e->val);
>> +               if (btf_is_enum(t)) {
>> +                       e = btf_enum(t) + spec->raw_spec[0];
>> +                       s = btf__name_by_offset(spec->btf, e->name_off);
>> +                       append_buf("::%s = %u", s, e->val);
>> +               } else {
>> +                       e64 = btf_enum64(t) + spec->raw_spec[0];
>> +                       s = btf__name_by_offset(spec->btf, e64->name_off);
>> +                       append_buf("::%s = %llu", s, btf_enum64_value(e64));
> 
> %llu problem here again

okay.

> 
>> +               }
>>                  return len;
>>          }
>>
> 
> [...]
Yonghong Song May 10, 2022, 11:02 p.m. UTC | #5
On 5/10/22 3:40 PM, Yonghong Song wrote:
> 
> 
> On 5/9/22 4:25 PM, Andrii Nakryiko wrote:
>> On Sun, May 1, 2022 at 12:00 PM Yonghong Song <yhs@fb.com> wrote:
>>>
>>> Add BTF_KIND_ENUM64 support. Deprecated btf__add_enum() and
>>> btf__add_enum_value() and introduced the following new APIs
>>>    btf__add_enum32()
>>>    btf__add_enum32_value()
>>>    btf__add_enum64()
>>>    btf__add_enum64_value()
>>> due to new kind and introduction of kflag.
>>>
>>> To support old kernel with enum64, the sanitization is
>>> added to replace BTF_KIND_ENUM64 with a bunch of
>>> pointer-to-void types.
>>>
>>> The enum64 value relocation is also supported. The enum64
>>> forward resolution, with enum type as forward declaration
>>> and enum64 as the actual definition, is also supported.
>>>
>>> Signed-off-by: Yonghong Song <yhs@fb.com>
>>> ---
>>>   tools/lib/bpf/btf.c                           | 226 +++++++++++++++++-
>>>   tools/lib/bpf/btf.h                           |  21 ++
>>>   tools/lib/bpf/btf_dump.c                      |  94 ++++++--
>>>   tools/lib/bpf/libbpf.c                        |  64 ++++-
>>>   tools/lib/bpf/libbpf.map                      |   4 +
>>>   tools/lib/bpf/libbpf_internal.h               |   2 +
>>>   tools/lib/bpf/linker.c                        |   2 +
>>>   tools/lib/bpf/relo_core.c                     |  93 ++++---
>>>   .../selftests/bpf/prog_tests/btf_dump.c       |  10 +-
>>>   .../selftests/bpf/prog_tests/btf_write.c      |   6 +-
>>>   10 files changed, 450 insertions(+), 72 deletions(-)
>>>
>>
[...]
>>
>>
>>> +       t->size = tsize;
>>> +
>>> +       return btf_commit_type(btf, sz);
>>> +}
>>> +
>>> +/*
>>> + * Append new BTF_KIND_ENUM type with:
>>> + *   - *name* - name of the enum, can be NULL or empty for anonymous 
>>> enums;
>>> + *   - *is_unsigned* - whether the enum values are unsigned or not;
>>> + *
>>> + * Enum initially has no enum values in it (and corresponds to enum 
>>> forward
>>> + * declaration). Enumerator values can be added by 
>>> btf__add_enum64_value()
>>> + * immediately after btf__add_enum() succeeds.
>>> + *
>>> + * Returns:
>>> + *   - >0, type ID of newly added BTF type;
>>> + *   - <0, on error.
>>> + */
>>> +int btf__add_enum32(struct btf *btf, const char *name, bool 
>>> is_unsigned)
>>
>> given it's still BTF_KIND_ENUM in UAPI, let's keep 32-bit ones as just
>> btf__add_enum()/btf__add_enum_value() and not deprecate anything.
>> ENUM64 can be thought about as more of a special case, so I think it's
>> ok.
> 
> The current btf__add_enum api:
> LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32 
> bytes_sz);
> 
> The issue is it doesn't have signedness parameter. if the user input
> is
>     enum { A = -1, B = 0, C = 1 };
> the actual printout btf format will be
>     enum { A 4294967295, B = 0, C = 1}
> does not match the original source.

I think I found a way to keep the current btf__add_enum() API.
Initially, the signedness will be unsigned. But during
btf__add_enum_value() api calls, if any negative value
is found, the signedness will change to signed. I think
this should work.

> 
>>
>>> +{
>>> +       return btf_add_enum_common(btf, name, is_unsigned, 
>>> BTF_KIND_ENUM, 4);
>>> +}
>>> +
>>
>> [...]
>>
[...]
Andrii Nakryiko May 10, 2022, 11:38 p.m. UTC | #6
On Tue, May 10, 2022 at 3:40 PM Yonghong Song <yhs@fb.com> wrote:
>
>
>
> On 5/9/22 4:25 PM, Andrii Nakryiko wrote:
> > On Sun, May 1, 2022 at 12:00 PM Yonghong Song <yhs@fb.com> wrote:
> >>
> >> Add BTF_KIND_ENUM64 support. Deprecated btf__add_enum() and
> >> btf__add_enum_value() and introduced the following new APIs
> >>    btf__add_enum32()
> >>    btf__add_enum32_value()
> >>    btf__add_enum64()
> >>    btf__add_enum64_value()
> >> due to new kind and introduction of kflag.
> >>
> >> To support old kernel with enum64, the sanitization is
> >> added to replace BTF_KIND_ENUM64 with a bunch of
> >> pointer-to-void types.
> >>
> >> The enum64 value relocation is also supported. The enum64
> >> forward resolution, with enum type as forward declaration
> >> and enum64 as the actual definition, is also supported.
> >>
> >> Signed-off-by: Yonghong Song <yhs@fb.com>
> >> ---
> >>   tools/lib/bpf/btf.c                           | 226 +++++++++++++++++-
> >>   tools/lib/bpf/btf.h                           |  21 ++
> >>   tools/lib/bpf/btf_dump.c                      |  94 ++++++--
> >>   tools/lib/bpf/libbpf.c                        |  64 ++++-
> >>   tools/lib/bpf/libbpf.map                      |   4 +
> >>   tools/lib/bpf/libbpf_internal.h               |   2 +
> >>   tools/lib/bpf/linker.c                        |   2 +
> >>   tools/lib/bpf/relo_core.c                     |  93 ++++---
> >>   .../selftests/bpf/prog_tests/btf_dump.c       |  10 +-
> >>   .../selftests/bpf/prog_tests/btf_write.c      |   6 +-
> >>   10 files changed, 450 insertions(+), 72 deletions(-)
> >>
> >

[...]

> >
> >
> >> +       t->size = tsize;
> >> +
> >> +       return btf_commit_type(btf, sz);
> >> +}
> >> +
> >> +/*
> >> + * Append new BTF_KIND_ENUM type with:
> >> + *   - *name* - name of the enum, can be NULL or empty for anonymous enums;
> >> + *   - *is_unsigned* - whether the enum values are unsigned or not;
> >> + *
> >> + * Enum initially has no enum values in it (and corresponds to enum forward
> >> + * declaration). Enumerator values can be added by btf__add_enum64_value()
> >> + * immediately after btf__add_enum() succeeds.
> >> + *
> >> + * Returns:
> >> + *   - >0, type ID of newly added BTF type;
> >> + *   - <0, on error.
> >> + */
> >> +int btf__add_enum32(struct btf *btf, const char *name, bool is_unsigned)
> >
> > given it's still BTF_KIND_ENUM in UAPI, let's keep 32-bit ones as just
> > btf__add_enum()/btf__add_enum_value() and not deprecate anything.
> > ENUM64 can be thought about as more of a special case, so I think it's
> > ok.
>
> The current btf__add_enum api:
> LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32
> bytes_sz);
>
> The issue is it doesn't have signedness parameter. if the user input
> is
>     enum { A = -1, B = 0, C = 1 };
> the actual printout btf format will be
>     enum { A 4294967295, B = 0, C = 1}
> does not match the original source.

Oh, I didn't realize that's the reason. I still like btf__add_enum()
name much better, can you please do the same macro trick that I did
for bpf_prog_load() based on the number of arguments? We'll be able to
preserve good API name and add extra argument. Once this lands we'll
need to update pahole to added signedness bit, but otherwise I don't
think there are many other users of these APIs currently (I might be
wrong, but macro magic gives us backwards compat anyway).

>
> >
> >> +{
> >> +       return btf_add_enum_common(btf, name, is_unsigned, BTF_KIND_ENUM, 4);
> >> +}
> >> +
> >
> > [...]
> >
> >>   /*

[...]

> >> @@ -764,8 +792,13 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
> >>                  if (!spec)
> >>                          return -EUCLEAN; /* request instruction poisoning */
> >>                  t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
> >> -               e = btf_enum(t) + spec->spec[0].idx;
> >> -               *val = e->val;
> >> +               if (btf_is_enum(t)) {
> >> +                       e = btf_enum(t) + spec->spec[0].idx;
> >> +                       *val = e->val;
> >> +               } else {
> >> +                       e64 = btf_enum64(t) + spec->spec[0].idx;
> >> +                       *val = btf_enum64_value(e64);
> >> +               }
> >
> > I think with sign bit we now have further complication: for 32-bit
> > enums we need to sign extend 32-bit values to s64 and then cast as
> > u64, no? Seems like a helper to abstract that is good to have here.
> > Otherwise relocating enum ABC { D = -1 } will produce invalid ldimm64
> > instruction, right?
>
> We should be fine here. For enum32, we have
> struct btf_enum {
>          __u32   name_off;
>          __s32   val;
> };
> So above *val = e->val will first sign extend from __s32 to __s64
> and then the __u64. Let me have a helper with additional comments
> to make it clear.
>

Ok, great! Let's just shorten this as I suggested below?

> >
> > Also keep in mind that you can use btf_enum()/btf_enum64() as an
> > array, so above you can write just as
> >
> > *val = btf_is_enum(t)
> >      ? btf_enum(t)[spec->spec[0].idx]
> >      : btf_enum64(t)[spec->spec[0].idx];
> >
> > But we need sign check and extension, so better to have a separate helper.
> >
> >>                  break;
> >>          default:
> >>                  return -EOPNOTSUPP;
> >> @@ -1034,7 +1067,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
> >>                  }
> >>
> >>                  insn[0].imm = new_val;
> >> -               insn[1].imm = 0; /* currently only 32-bit values are supported */
> >> +               insn[1].imm = new_val >> 32;
> >
> > for 32-bit instructions (ALU/ALU32, etc) we need to make sure that
> > new_val fits in 32 bits. And we need to be careful about
> > signed/unsigned, because for signed case all-zero or all-one upper 32
> > bits are ok (sign extension). Can we know the expected signed/unsigned
> > operation from bpf_insn itself? We should be, right?
>
> The core relocation insn for constant is
>    move r1, <32bit value>
> or
>    ldimm_64 r1, <64bit value>
> and there are no signedness information.
> So the 64bit value (except sign extension) can only from
> ldimm_64. We should be okay here, but I can double check.

not sure how full 64-bit -1 should be loaded into register then. Does
compiler generate extra sign-extending bit shifts or embedded constant
is considered to be a signed constant always?

>
> >
> >>                  pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %llu\n",
> >>                           prog_name, relo_idx, insn_idx,
> >>                           (unsigned long long)imm, new_val);
> >> @@ -1056,6 +1089,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
> >>    */

[...]
Andrii Nakryiko May 10, 2022, 11:40 p.m. UTC | #7
On Tue, May 10, 2022 at 4:02 PM Yonghong Song <yhs@fb.com> wrote:
>
>
>
> On 5/10/22 3:40 PM, Yonghong Song wrote:
> >
> >
> > On 5/9/22 4:25 PM, Andrii Nakryiko wrote:
> >> On Sun, May 1, 2022 at 12:00 PM Yonghong Song <yhs@fb.com> wrote:
> >>>
> >>> Add BTF_KIND_ENUM64 support. Deprecated btf__add_enum() and
> >>> btf__add_enum_value() and introduced the following new APIs
> >>>    btf__add_enum32()
> >>>    btf__add_enum32_value()
> >>>    btf__add_enum64()
> >>>    btf__add_enum64_value()
> >>> due to new kind and introduction of kflag.
> >>>
> >>> To support old kernel with enum64, the sanitization is
> >>> added to replace BTF_KIND_ENUM64 with a bunch of
> >>> pointer-to-void types.
> >>>
> >>> The enum64 value relocation is also supported. The enum64
> >>> forward resolution, with enum type as forward declaration
> >>> and enum64 as the actual definition, is also supported.
> >>>
> >>> Signed-off-by: Yonghong Song <yhs@fb.com>
> >>> ---
> >>>   tools/lib/bpf/btf.c                           | 226 +++++++++++++++++-
> >>>   tools/lib/bpf/btf.h                           |  21 ++
> >>>   tools/lib/bpf/btf_dump.c                      |  94 ++++++--
> >>>   tools/lib/bpf/libbpf.c                        |  64 ++++-
> >>>   tools/lib/bpf/libbpf.map                      |   4 +
> >>>   tools/lib/bpf/libbpf_internal.h               |   2 +
> >>>   tools/lib/bpf/linker.c                        |   2 +
> >>>   tools/lib/bpf/relo_core.c                     |  93 ++++---
> >>>   .../selftests/bpf/prog_tests/btf_dump.c       |  10 +-
> >>>   .../selftests/bpf/prog_tests/btf_write.c      |   6 +-
> >>>   10 files changed, 450 insertions(+), 72 deletions(-)
> >>>
> >>
> [...]
> >>
> >>
> >>> +       t->size = tsize;
> >>> +
> >>> +       return btf_commit_type(btf, sz);
> >>> +}
> >>> +
> >>> +/*
> >>> + * Append new BTF_KIND_ENUM type with:
> >>> + *   - *name* - name of the enum, can be NULL or empty for anonymous
> >>> enums;
> >>> + *   - *is_unsigned* - whether the enum values are unsigned or not;
> >>> + *
> >>> + * Enum initially has no enum values in it (and corresponds to enum
> >>> forward
> >>> + * declaration). Enumerator values can be added by
> >>> btf__add_enum64_value()
> >>> + * immediately after btf__add_enum() succeeds.
> >>> + *
> >>> + * Returns:
> >>> + *   - >0, type ID of newly added BTF type;
> >>> + *   - <0, on error.
> >>> + */
> >>> +int btf__add_enum32(struct btf *btf, const char *name, bool
> >>> is_unsigned)
> >>
> >> given it's still BTF_KIND_ENUM in UAPI, let's keep 32-bit ones as just
> >> btf__add_enum()/btf__add_enum_value() and not deprecate anything.
> >> ENUM64 can be thought about as more of a special case, so I think it's
> >> ok.
> >
> > The current btf__add_enum api:
> > LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32
> > bytes_sz);
> >
> > The issue is it doesn't have signedness parameter. if the user input
> > is
> >     enum { A = -1, B = 0, C = 1 };
> > the actual printout btf format will be
> >     enum { A 4294967295, B = 0, C = 1}
> > does not match the original source.
>
> I think I found a way to keep the current btf__add_enum() API.
> Initially, the signedness will be unsigned. But during
> btf__add_enum_value() api calls, if any negative value
> is found, the signedness will change to signed. I think
> this should work.
>

Oops, didn't see this email when replying. Yeah, I guess this approach
will work for 32-bit enum. For 64-bit one we probably better specify
signedness explicitly and then accept __u64 as the value (which can be
negative value casted to __u64, in practice).

> >
> >>
> >>> +{
> >>> +       return btf_add_enum_common(btf, name, is_unsigned,
> >>> BTF_KIND_ENUM, 4);
> >>> +}
> >>> +
> >>
> >> [...]
> >>
> [...]
Yonghong Song May 11, 2022, 12:39 a.m. UTC | #8
On 5/10/22 4:38 PM, Andrii Nakryiko wrote:
> On Tue, May 10, 2022 at 3:40 PM Yonghong Song <yhs@fb.com> wrote:
>>
>>
>>
>> On 5/9/22 4:25 PM, Andrii Nakryiko wrote:
>>> On Sun, May 1, 2022 at 12:00 PM Yonghong Song <yhs@fb.com> wrote:
>>>>
>>>> Add BTF_KIND_ENUM64 support. Deprecated btf__add_enum() and
>>>> btf__add_enum_value() and introduced the following new APIs
>>>>     btf__add_enum32()
>>>>     btf__add_enum32_value()
>>>>     btf__add_enum64()
>>>>     btf__add_enum64_value()
>>>> due to new kind and introduction of kflag.
>>>>
>>>> To support old kernel with enum64, the sanitization is
>>>> added to replace BTF_KIND_ENUM64 with a bunch of
>>>> pointer-to-void types.
>>>>
>>>> The enum64 value relocation is also supported. The enum64
>>>> forward resolution, with enum type as forward declaration
>>>> and enum64 as the actual definition, is also supported.
>>>>
>>>> Signed-off-by: Yonghong Song <yhs@fb.com>
>>>> ---
>>>>    tools/lib/bpf/btf.c                           | 226 +++++++++++++++++-
>>>>    tools/lib/bpf/btf.h                           |  21 ++
>>>>    tools/lib/bpf/btf_dump.c                      |  94 ++++++--
>>>>    tools/lib/bpf/libbpf.c                        |  64 ++++-
>>>>    tools/lib/bpf/libbpf.map                      |   4 +
>>>>    tools/lib/bpf/libbpf_internal.h               |   2 +
>>>>    tools/lib/bpf/linker.c                        |   2 +
>>>>    tools/lib/bpf/relo_core.c                     |  93 ++++---
>>>>    .../selftests/bpf/prog_tests/btf_dump.c       |  10 +-
>>>>    .../selftests/bpf/prog_tests/btf_write.c      |   6 +-
>>>>    10 files changed, 450 insertions(+), 72 deletions(-)
>>>>
>>>
> 
> [...]
> 
>>>
>>>
>>>> +       t->size = tsize;
>>>> +
>>>> +       return btf_commit_type(btf, sz);
>>>> +}
>>>> +
>>>> +/*
>>>> + * Append new BTF_KIND_ENUM type with:
>>>> + *   - *name* - name of the enum, can be NULL or empty for anonymous enums;
>>>> + *   - *is_unsigned* - whether the enum values are unsigned or not;
>>>> + *
>>>> + * Enum initially has no enum values in it (and corresponds to enum forward
>>>> + * declaration). Enumerator values can be added by btf__add_enum64_value()
>>>> + * immediately after btf__add_enum() succeeds.
>>>> + *
>>>> + * Returns:
>>>> + *   - >0, type ID of newly added BTF type;
>>>> + *   - <0, on error.
>>>> + */
>>>> +int btf__add_enum32(struct btf *btf, const char *name, bool is_unsigned)
>>>
>>> given it's still BTF_KIND_ENUM in UAPI, let's keep 32-bit ones as just
>>> btf__add_enum()/btf__add_enum_value() and not deprecate anything.
>>> ENUM64 can be thought about as more of a special case, so I think it's
>>> ok.
>>
>> The current btf__add_enum api:
>> LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32
>> bytes_sz);
>>
>> The issue is it doesn't have signedness parameter. if the user input
>> is
>>      enum { A = -1, B = 0, C = 1 };
>> the actual printout btf format will be
>>      enum { A 4294967295, B = 0, C = 1}
>> does not match the original source.
> 
> Oh, I didn't realize that's the reason. I still like btf__add_enum()
> name much better, can you please do the same macro trick that I did
> for bpf_prog_load() based on the number of arguments? We'll be able to
> preserve good API name and add extra argument. Once this lands we'll
> need to update pahole to added signedness bit, but otherwise I don't
> think there are many other users of these APIs currently (I might be
> wrong, but macro magic gives us backwards compat anyway).
> 
>>
>>>
>>>> +{
>>>> +       return btf_add_enum_common(btf, name, is_unsigned, BTF_KIND_ENUM, 4);
>>>> +}
>>>> +
>>>
>>> [...]
>>>
>>>>    /*
> 
> [...]
> 
>>>> @@ -764,8 +792,13 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
>>>>                   if (!spec)
>>>>                           return -EUCLEAN; /* request instruction poisoning */
>>>>                   t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
>>>> -               e = btf_enum(t) + spec->spec[0].idx;
>>>> -               *val = e->val;
>>>> +               if (btf_is_enum(t)) {
>>>> +                       e = btf_enum(t) + spec->spec[0].idx;
>>>> +                       *val = e->val;
>>>> +               } else {
>>>> +                       e64 = btf_enum64(t) + spec->spec[0].idx;
>>>> +                       *val = btf_enum64_value(e64);
>>>> +               }
>>>
>>> I think with sign bit we now have further complication: for 32-bit
>>> enums we need to sign extend 32-bit values to s64 and then cast as
>>> u64, no? Seems like a helper to abstract that is good to have here.
>>> Otherwise relocating enum ABC { D = -1 } will produce invalid ldimm64
>>> instruction, right?
>>
>> We should be fine here. For enum32, we have
>> struct btf_enum {
>>           __u32   name_off;
>>           __s32   val;
>> };
>> So above *val = e->val will first sign extend from __s32 to __s64
>> and then the __u64. Let me have a helper with additional comments
>> to make it clear.
>>
> 
> Ok, great! Let's just shorten this as I suggested below?

The
 >>> *val = btf_is_enum(t)
 >>>       ? btf_enum(t)[spec->spec[0].idx]
 >>>       : btf_enum64(t)[spec->spec[0].idx];
won't work, but the following should work:
    *val = btf_is_enum(t)
	? btf_enum(t)[spec->spec[0].idx].val
	: btf_enum64_value(btf_enum64(t) + spec->spec[0].idx);
> 
>>>
>>> Also keep in mind that you can use btf_enum()/btf_enum64() as an
>>> array, so above you can write just as
>>>
>>> *val = btf_is_enum(t)
>>>       ? btf_enum(t)[spec->spec[0].idx]
>>>       : btf_enum64(t)[spec->spec[0].idx];
>>>
>>> But we need sign check and extension, so better to have a separate helper.
>>>
>>>>                   break;
>>>>           default:
>>>>                   return -EOPNOTSUPP;
>>>> @@ -1034,7 +1067,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
>>>>                   }
>>>>
>>>>                   insn[0].imm = new_val;
>>>> -               insn[1].imm = 0; /* currently only 32-bit values are supported */
>>>> +               insn[1].imm = new_val >> 32;
>>>
>>> for 32-bit instructions (ALU/ALU32, etc) we need to make sure that
>>> new_val fits in 32 bits. And we need to be careful about
>>> signed/unsigned, because for signed case all-zero or all-one upper 32
>>> bits are ok (sign extension). Can we know the expected signed/unsigned
>>> operation from bpf_insn itself? We should be, right?
>>
>> The core relocation insn for constant is
>>     move r1, <32bit value>
>> or
>>     ldimm_64 r1, <64bit value>
>> and there are no signedness information.
>> So the 64bit value (except sign extension) can only from
>> ldimm_64. We should be okay here, but I can double check.
> 
> not sure how full 64-bit -1 should be loaded into register then. Does
> compiler generate extra sign-extending bit shifts or embedded constant
> is considered to be a signed constant always?

For ldimm64 r1, -1,
the first insn imm will be 0xffffffff, and the second insn will also be 
0xffffffff. The final value will be
   ((u64)(u32)0xffffffff << 32) | (u32)0xffffffff


> 
>>
>>>
>>>>                   pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %llu\n",
>>>>                            prog_name, relo_idx, insn_idx,
>>>>                            (unsigned long long)imm, new_val);
>>>> @@ -1056,6 +1089,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
>>>>     */
> 
> [...]
Andrii Nakryiko May 11, 2022, 5:43 p.m. UTC | #9
On Tue, May 10, 2022 at 5:39 PM Yonghong Song <yhs@fb.com> wrote:
>
>
>
> On 5/10/22 4:38 PM, Andrii Nakryiko wrote:
> > On Tue, May 10, 2022 at 3:40 PM Yonghong Song <yhs@fb.com> wrote:
> >>
> >>
> >>
> >> On 5/9/22 4:25 PM, Andrii Nakryiko wrote:
> >>> On Sun, May 1, 2022 at 12:00 PM Yonghong Song <yhs@fb.com> wrote:
> >>>>
> >>>> Add BTF_KIND_ENUM64 support. Deprecated btf__add_enum() and
> >>>> btf__add_enum_value() and introduced the following new APIs
> >>>>     btf__add_enum32()
> >>>>     btf__add_enum32_value()
> >>>>     btf__add_enum64()
> >>>>     btf__add_enum64_value()
> >>>> due to new kind and introduction of kflag.
> >>>>
> >>>> To support old kernel with enum64, the sanitization is
> >>>> added to replace BTF_KIND_ENUM64 with a bunch of
> >>>> pointer-to-void types.
> >>>>
> >>>> The enum64 value relocation is also supported. The enum64
> >>>> forward resolution, with enum type as forward declaration
> >>>> and enum64 as the actual definition, is also supported.
> >>>>
> >>>> Signed-off-by: Yonghong Song <yhs@fb.com>
> >>>> ---
> >>>>    tools/lib/bpf/btf.c                           | 226 +++++++++++++++++-
> >>>>    tools/lib/bpf/btf.h                           |  21 ++
> >>>>    tools/lib/bpf/btf_dump.c                      |  94 ++++++--
> >>>>    tools/lib/bpf/libbpf.c                        |  64 ++++-
> >>>>    tools/lib/bpf/libbpf.map                      |   4 +
> >>>>    tools/lib/bpf/libbpf_internal.h               |   2 +
> >>>>    tools/lib/bpf/linker.c                        |   2 +
> >>>>    tools/lib/bpf/relo_core.c                     |  93 ++++---
> >>>>    .../selftests/bpf/prog_tests/btf_dump.c       |  10 +-
> >>>>    .../selftests/bpf/prog_tests/btf_write.c      |   6 +-
> >>>>    10 files changed, 450 insertions(+), 72 deletions(-)
> >>>>
> >>>
> >
> > [...]
> >
> >>>
> >>>
> >>>> +       t->size = tsize;
> >>>> +
> >>>> +       return btf_commit_type(btf, sz);
> >>>> +}
> >>>> +
> >>>> +/*
> >>>> + * Append new BTF_KIND_ENUM type with:
> >>>> + *   - *name* - name of the enum, can be NULL or empty for anonymous enums;
> >>>> + *   - *is_unsigned* - whether the enum values are unsigned or not;
> >>>> + *
> >>>> + * Enum initially has no enum values in it (and corresponds to enum forward
> >>>> + * declaration). Enumerator values can be added by btf__add_enum64_value()
> >>>> + * immediately after btf__add_enum() succeeds.
> >>>> + *
> >>>> + * Returns:
> >>>> + *   - >0, type ID of newly added BTF type;
> >>>> + *   - <0, on error.
> >>>> + */
> >>>> +int btf__add_enum32(struct btf *btf, const char *name, bool is_unsigned)
> >>>
> >>> given it's still BTF_KIND_ENUM in UAPI, let's keep 32-bit ones as just
> >>> btf__add_enum()/btf__add_enum_value() and not deprecate anything.
> >>> ENUM64 can be thought about as more of a special case, so I think it's
> >>> ok.
> >>
> >> The current btf__add_enum api:
> >> LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32
> >> bytes_sz);
> >>
> >> The issue is it doesn't have signedness parameter. if the user input
> >> is
> >>      enum { A = -1, B = 0, C = 1 };
> >> the actual printout btf format will be
> >>      enum { A 4294967295, B = 0, C = 1}
> >> does not match the original source.
> >
> > Oh, I didn't realize that's the reason. I still like btf__add_enum()
> > name much better, can you please do the same macro trick that I did
> > for bpf_prog_load() based on the number of arguments? We'll be able to
> > preserve good API name and add extra argument. Once this lands we'll
> > need to update pahole to added signedness bit, but otherwise I don't
> > think there are many other users of these APIs currently (I might be
> > wrong, but macro magic gives us backwards compat anyway).
> >
> >>
> >>>
> >>>> +{
> >>>> +       return btf_add_enum_common(btf, name, is_unsigned, BTF_KIND_ENUM, 4);
> >>>> +}
> >>>> +
> >>>
> >>> [...]
> >>>
> >>>>    /*
> >
> > [...]
> >
> >>>> @@ -764,8 +792,13 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
> >>>>                   if (!spec)
> >>>>                           return -EUCLEAN; /* request instruction poisoning */
> >>>>                   t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
> >>>> -               e = btf_enum(t) + spec->spec[0].idx;
> >>>> -               *val = e->val;
> >>>> +               if (btf_is_enum(t)) {
> >>>> +                       e = btf_enum(t) + spec->spec[0].idx;
> >>>> +                       *val = e->val;
> >>>> +               } else {
> >>>> +                       e64 = btf_enum64(t) + spec->spec[0].idx;
> >>>> +                       *val = btf_enum64_value(e64);
> >>>> +               }
> >>>
> >>> I think with sign bit we now have further complication: for 32-bit
> >>> enums we need to sign extend 32-bit values to s64 and then cast as
> >>> u64, no? Seems like a helper to abstract that is good to have here.
> >>> Otherwise relocating enum ABC { D = -1 } will produce invalid ldimm64
> >>> instruction, right?
> >>
> >> We should be fine here. For enum32, we have
> >> struct btf_enum {
> >>           __u32   name_off;
> >>           __s32   val;
> >> };
> >> So above *val = e->val will first sign extend from __s32 to __s64
> >> and then the __u64. Let me have a helper with additional comments
> >> to make it clear.
> >>
> >
> > Ok, great! Let's just shorten this as I suggested below?
>
> The
>  >>> *val = btf_is_enum(t)
>  >>>       ? btf_enum(t)[spec->spec[0].idx]
>  >>>       : btf_enum64(t)[spec->spec[0].idx];
> won't work, but the following should work:
>     *val = btf_is_enum(t)
>         ? btf_enum(t)[spec->spec[0].idx].val
>         : btf_enum64_value(btf_enum64(t) + spec->spec[0].idx);

yep, for consistency it should be btf_enum64(t)[spec->spec[0].idx],
but it's very minor, of course

> >
> >>>
> >>> Also keep in mind that you can use btf_enum()/btf_enum64() as an
> >>> array, so above you can write just as
> >>>
> >>> *val = btf_is_enum(t)
> >>>       ? btf_enum(t)[spec->spec[0].idx]
> >>>       : btf_enum64(t)[spec->spec[0].idx];
> >>>
> >>> But we need sign check and extension, so better to have a separate helper.
> >>>
> >>>>                   break;
> >>>>           default:
> >>>>                   return -EOPNOTSUPP;
> >>>> @@ -1034,7 +1067,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
> >>>>                   }
> >>>>
> >>>>                   insn[0].imm = new_val;
> >>>> -               insn[1].imm = 0; /* currently only 32-bit values are supported */
> >>>> +               insn[1].imm = new_val >> 32;
> >>>
> >>> for 32-bit instructions (ALU/ALU32, etc) we need to make sure that
> >>> new_val fits in 32 bits. And we need to be careful about
> >>> signed/unsigned, because for signed case all-zero or all-one upper 32
> >>> bits are ok (sign extension). Can we know the expected signed/unsigned
> >>> operation from bpf_insn itself? We should be, right?
> >>
> >> The core relocation insn for constant is
> >>     move r1, <32bit value>
> >> or
> >>     ldimm_64 r1, <64bit value>
> >> and there are no signedness information.
> >> So the 64bit value (except sign extension) can only from
> >> ldimm_64. We should be okay here, but I can double check.
> >
> > not sure how full 64-bit -1 should be loaded into register then. Does
> > compiler generate extra sign-extending bit shifts or embedded constant
> > is considered to be a signed constant always?
>
> For ldimm64 r1, -1,
> the first insn imm will be 0xffffffff, and the second insn will also be
> 0xffffffff. The final value will be
>    ((u64)(u32)0xffffffff << 32) | (u32)0xffffffff

yeah, I get it for ldimm64, but I was specifically curious about move
instruction that only has 32-bit immediate value but assigns to full
64-bit r1? Is it treated as signed unconditionally?

>
>
> >
> >>
> >>>
> >>>>                   pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %llu\n",
> >>>>                            prog_name, relo_idx, insn_idx,
> >>>>                            (unsigned long long)imm, new_val);
> >>>> @@ -1056,6 +1089,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
> >>>>     */
> >
> > [...]
Yonghong Song May 11, 2022, 6:56 p.m. UTC | #10
On 5/11/22 10:43 AM, Andrii Nakryiko wrote:
> On Tue, May 10, 2022 at 5:39 PM Yonghong Song <yhs@fb.com> wrote:
>>
>>
>>
>> On 5/10/22 4:38 PM, Andrii Nakryiko wrote:
>>> On Tue, May 10, 2022 at 3:40 PM Yonghong Song <yhs@fb.com> wrote:
>>>>
>>>>
>>>>
>>>> On 5/9/22 4:25 PM, Andrii Nakryiko wrote:
>>>>> On Sun, May 1, 2022 at 12:00 PM Yonghong Song <yhs@fb.com> wrote:
>>>>>>
>>>>>> Add BTF_KIND_ENUM64 support. Deprecated btf__add_enum() and
>>>>>> btf__add_enum_value() and introduced the following new APIs
>>>>>>      btf__add_enum32()
>>>>>>      btf__add_enum32_value()
>>>>>>      btf__add_enum64()
>>>>>>      btf__add_enum64_value()
>>>>>> due to new kind and introduction of kflag.
>>>>>>
>>>>>> To support old kernel with enum64, the sanitization is
>>>>>> added to replace BTF_KIND_ENUM64 with a bunch of
>>>>>> pointer-to-void types.
>>>>>>
>>>>>> The enum64 value relocation is also supported. The enum64
>>>>>> forward resolution, with enum type as forward declaration
>>>>>> and enum64 as the actual definition, is also supported.
>>>>>>
>>>>>> Signed-off-by: Yonghong Song <yhs@fb.com>
>>>>>> ---
>>>>>>     tools/lib/bpf/btf.c                           | 226 +++++++++++++++++-
>>>>>>     tools/lib/bpf/btf.h                           |  21 ++
>>>>>>     tools/lib/bpf/btf_dump.c                      |  94 ++++++--
>>>>>>     tools/lib/bpf/libbpf.c                        |  64 ++++-
>>>>>>     tools/lib/bpf/libbpf.map                      |   4 +
>>>>>>     tools/lib/bpf/libbpf_internal.h               |   2 +
>>>>>>     tools/lib/bpf/linker.c                        |   2 +
>>>>>>     tools/lib/bpf/relo_core.c                     |  93 ++++---
>>>>>>     .../selftests/bpf/prog_tests/btf_dump.c       |  10 +-
>>>>>>     .../selftests/bpf/prog_tests/btf_write.c      |   6 +-
>>>>>>     10 files changed, 450 insertions(+), 72 deletions(-)
>>>>>>
>>>>>
>>>
>>> [...]
>>>
>>>>>
>>>>>
>>>>>> +       t->size = tsize;
>>>>>> +
>>>>>> +       return btf_commit_type(btf, sz);
>>>>>> +}
>>>>>> +
>>>>>> +/*
>>>>>> + * Append new BTF_KIND_ENUM type with:
>>>>>> + *   - *name* - name of the enum, can be NULL or empty for anonymous enums;
>>>>>> + *   - *is_unsigned* - whether the enum values are unsigned or not;
>>>>>> + *
>>>>>> + * Enum initially has no enum values in it (and corresponds to enum forward
>>>>>> + * declaration). Enumerator values can be added by btf__add_enum64_value()
>>>>>> + * immediately after btf__add_enum() succeeds.
>>>>>> + *
>>>>>> + * Returns:
>>>>>> + *   - >0, type ID of newly added BTF type;
>>>>>> + *   - <0, on error.
>>>>>> + */
>>>>>> +int btf__add_enum32(struct btf *btf, const char *name, bool is_unsigned)
>>>>>
>>>>> given it's still BTF_KIND_ENUM in UAPI, let's keep 32-bit ones as just
>>>>> btf__add_enum()/btf__add_enum_value() and not deprecate anything.
>>>>> ENUM64 can be thought about as more of a special case, so I think it's
>>>>> ok.
>>>>
>>>> The current btf__add_enum api:
>>>> LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32
>>>> bytes_sz);
>>>>
>>>> The issue is it doesn't have signedness parameter. if the user input
>>>> is
>>>>       enum { A = -1, B = 0, C = 1 };
>>>> the actual printout btf format will be
>>>>       enum { A 4294967295, B = 0, C = 1}
>>>> does not match the original source.
>>>
>>> Oh, I didn't realize that's the reason. I still like btf__add_enum()
>>> name much better, can you please do the same macro trick that I did
>>> for bpf_prog_load() based on the number of arguments? We'll be able to
>>> preserve good API name and add extra argument. Once this lands we'll
>>> need to update pahole to added signedness bit, but otherwise I don't
>>> think there are many other users of these APIs currently (I might be
>>> wrong, but macro magic gives us backwards compat anyway).
>>>
>>>>
>>>>>
>>>>>> +{
>>>>>> +       return btf_add_enum_common(btf, name, is_unsigned, BTF_KIND_ENUM, 4);
>>>>>> +}
>>>>>> +
>>>>>
>>>>> [...]
>>>>>
>>>>>>     /*
>>>
>>> [...]
>>>
>>>>>> @@ -764,8 +792,13 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
>>>>>>                    if (!spec)
>>>>>>                            return -EUCLEAN; /* request instruction poisoning */
>>>>>>                    t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
>>>>>> -               e = btf_enum(t) + spec->spec[0].idx;
>>>>>> -               *val = e->val;
>>>>>> +               if (btf_is_enum(t)) {
>>>>>> +                       e = btf_enum(t) + spec->spec[0].idx;
>>>>>> +                       *val = e->val;
>>>>>> +               } else {
>>>>>> +                       e64 = btf_enum64(t) + spec->spec[0].idx;
>>>>>> +                       *val = btf_enum64_value(e64);
>>>>>> +               }
>>>>>
>>>>> I think with sign bit we now have further complication: for 32-bit
>>>>> enums we need to sign extend 32-bit values to s64 and then cast as
>>>>> u64, no? Seems like a helper to abstract that is good to have here.
>>>>> Otherwise relocating enum ABC { D = -1 } will produce invalid ldimm64
>>>>> instruction, right?
>>>>
>>>> We should be fine here. For enum32, we have
>>>> struct btf_enum {
>>>>            __u32   name_off;
>>>>            __s32   val;
>>>> };
>>>> So above *val = e->val will first sign extend from __s32 to __s64
>>>> and then the __u64. Let me have a helper with additional comments
>>>> to make it clear.
>>>>
>>>
>>> Ok, great! Let's just shorten this as I suggested below?
>>
>> The
>>   >>> *val = btf_is_enum(t)
>>   >>>       ? btf_enum(t)[spec->spec[0].idx]
>>   >>>       : btf_enum64(t)[spec->spec[0].idx];
>> won't work, but the following should work:
>>      *val = btf_is_enum(t)
>>          ? btf_enum(t)[spec->spec[0].idx].val
>>          : btf_enum64_value(btf_enum64(t) + spec->spec[0].idx);
> 
> yep, for consistency it should be btf_enum64(t)[spec->spec[0].idx],
> but it's very minor, of course
> 
>>>
>>>>>
>>>>> Also keep in mind that you can use btf_enum()/btf_enum64() as an
>>>>> array, so above you can write just as
>>>>>
>>>>> *val = btf_is_enum(t)
>>>>>        ? btf_enum(t)[spec->spec[0].idx]
>>>>>        : btf_enum64(t)[spec->spec[0].idx];
>>>>>
>>>>> But we need sign check and extension, so better to have a separate helper.
>>>>>
>>>>>>                    break;
>>>>>>            default:
>>>>>>                    return -EOPNOTSUPP;
>>>>>> @@ -1034,7 +1067,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
>>>>>>                    }
>>>>>>
>>>>>>                    insn[0].imm = new_val;
>>>>>> -               insn[1].imm = 0; /* currently only 32-bit values are supported */
>>>>>> +               insn[1].imm = new_val >> 32;
>>>>>
>>>>> for 32-bit instructions (ALU/ALU32, etc) we need to make sure that
>>>>> new_val fits in 32 bits. And we need to be careful about
>>>>> signed/unsigned, because for signed case all-zero or all-one upper 32
>>>>> bits are ok (sign extension). Can we know the expected signed/unsigned
>>>>> operation from bpf_insn itself? We should be, right?
>>>>
>>>> The core relocation insn for constant is
>>>>      move r1, <32bit value>
>>>> or
>>>>      ldimm_64 r1, <64bit value>
>>>> and there are no signedness information.
>>>> So the 64bit value (except sign extension) can only from
>>>> ldimm_64. We should be okay here, but I can double check.
>>>
>>> not sure how full 64-bit -1 should be loaded into register then. Does
>>> compiler generate extra sign-extending bit shifts or embedded constant
>>> is considered to be a signed constant always?
>>
>> For ldimm64 r1, -1,
>> the first insn imm will be 0xffffffff, and the second insn will also be
>> 0xffffffff. The final value will be
>>     ((u64)(u32)0xffffffff << 32) | (u32)0xffffffff
> 
> yeah, I get it for ldimm64, but I was specifically curious about move
> instruction that only has 32-bit immediate value but assigns to full
> 64-bit r1? Is it treated as signed unconditionally?

Yes, it is treated as 32-bit signed int and will do sign extension
if needed.

> 
>>
>>
>>>
>>>>
>>>>>
>>>>>>                    pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %llu\n",
>>>>>>                             prog_name, relo_idx, insn_idx,
>>>>>>                             (unsigned long long)imm, new_val);
>>>>>> @@ -1056,6 +1089,7 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
>>>>>>      */
>>>
>>> [...]
diff mbox series

Patch

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index bb1e06eb1eca..77fe14e99eeb 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -318,6 +318,8 @@  static int btf_type_size(const struct btf_type *t)
 		return base_size + vlen * sizeof(struct btf_var_secinfo);
 	case BTF_KIND_DECL_TAG:
 		return base_size + sizeof(struct btf_decl_tag);
+	case BTF_KIND_ENUM64:
+		return base_size + vlen * sizeof(struct btf_enum64);
 	default:
 		pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
 		return -EINVAL;
@@ -334,6 +336,7 @@  static void btf_bswap_type_base(struct btf_type *t)
 static int btf_bswap_type_rest(struct btf_type *t)
 {
 	struct btf_var_secinfo *v;
+	struct btf_enum64 *e64;
 	struct btf_member *m;
 	struct btf_array *a;
 	struct btf_param *p;
@@ -394,6 +397,13 @@  static int btf_bswap_type_rest(struct btf_type *t)
 	case BTF_KIND_DECL_TAG:
 		btf_decl_tag(t)->component_idx = bswap_32(btf_decl_tag(t)->component_idx);
 		return 0;
+	case BTF_KIND_ENUM64:
+		for (i = 0, e64 = btf_enum64(t); i < vlen; i++, e64++) {
+			e64->name_off = bswap_32(e64->name_off);
+			e64->hi32 = bswap_32(e64->hi32);
+			e64->lo32 = bswap_32(e64->lo32);
+		}
+		return 0;
 	default:
 		pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
 		return -EINVAL;
@@ -599,6 +609,7 @@  __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
 		case BTF_KIND_ENUM:
 		case BTF_KIND_DATASEC:
 		case BTF_KIND_FLOAT:
+		case BTF_KIND_ENUM64:
 			size = t->size;
 			goto done;
 		case BTF_KIND_PTR:
@@ -645,6 +656,7 @@  int btf__align_of(const struct btf *btf, __u32 id)
 	case BTF_KIND_INT:
 	case BTF_KIND_ENUM:
 	case BTF_KIND_FLOAT:
+	case BTF_KIND_ENUM64:
 		return min(btf_ptr_sz(btf), (size_t)t->size);
 	case BTF_KIND_PTR:
 		return btf_ptr_sz(btf);
@@ -2211,6 +2223,171 @@  int btf__add_enum_value(struct btf *btf, const char *name, __s64 value)
 	return 0;
 }
 
+static int btf_add_enum_common(struct btf *btf, const char *name,
+			       bool is_unsigned, __u8 kind, __u32 tsize)
+{
+	struct btf_type *t;
+	int sz, name_off = 0;
+
+	if (btf_ensure_modifiable(btf))
+		return libbpf_err(-ENOMEM);
+
+	sz = sizeof(struct btf_type);
+	t = btf_add_type_mem(btf, sz);
+	if (!t)
+		return libbpf_err(-ENOMEM);
+
+	if (name && name[0]) {
+		name_off = btf__add_str(btf, name);
+		if (name_off < 0)
+			return name_off;
+	}
+
+	/* start out with vlen=0; it will be adjusted when adding enum values */
+	t->name_off = name_off;
+	t->info = btf_type_info(kind, 0, is_unsigned);
+	t->size = tsize;
+
+	return btf_commit_type(btf, sz);
+}
+
+/*
+ * Append new BTF_KIND_ENUM type with:
+ *   - *name* - name of the enum, can be NULL or empty for anonymous enums;
+ *   - *is_unsigned* - whether the enum values are unsigned or not;
+ *
+ * Enum initially has no enum values in it (and corresponds to enum forward
+ * declaration). Enumerator values can be added by btf__add_enum64_value()
+ * immediately after btf__add_enum() succeeds.
+ *
+ * Returns:
+ *   - >0, type ID of newly added BTF type;
+ *   - <0, on error.
+ */
+int btf__add_enum32(struct btf *btf, const char *name, bool is_unsigned)
+{
+	return btf_add_enum_common(btf, name, is_unsigned, BTF_KIND_ENUM, 4);
+}
+
+/*
+ * Append new enum value for the current ENUM type with:
+ *   - *name* - name of the enumerator value, can't be NULL or empty;
+ *   - *value* - integer value corresponding to enum value *name*;
+ * Returns:
+ *   -  0, on success;
+ *   - <0, on error.
+ */
+int btf__add_enum32_value(struct btf *btf, const char *name, __s32 value)
+{
+	struct btf_enum *v;
+	struct btf_type *t;
+	int sz, name_off;
+
+	/* last type should be BTF_KIND_ENUM */
+	if (btf->nr_types == 0)
+		return libbpf_err(-EINVAL);
+	t = btf_last_type(btf);
+	if (!btf_is_enum(t))
+		return libbpf_err(-EINVAL);
+
+	/* non-empty name */
+	if (!name || !name[0])
+		return libbpf_err(-EINVAL);
+
+	/* decompose and invalidate raw data */
+	if (btf_ensure_modifiable(btf))
+		return libbpf_err(-ENOMEM);
+
+	sz = sizeof(struct btf_enum);
+	v = btf_add_type_mem(btf, sz);
+	if (!v)
+		return libbpf_err(-ENOMEM);
+
+	name_off = btf__add_str(btf, name);
+	if (name_off < 0)
+		return name_off;
+
+	v->name_off = name_off;
+	v->val = value;
+
+	/* update parent type's vlen */
+	t = btf_last_type(btf);
+	btf_type_inc_vlen(t);
+
+	btf->hdr->type_len += sz;
+	btf->hdr->str_off += sz;
+	return 0;
+}
+
+/*
+ * Append new BTF_KIND_ENUM64 type with:
+ *   - *name* - name of the enum, can be NULL or empty for anonymous enums;
+ *   - *is_unsigned* - whether the enum values are unsigned or not;
+ *
+ * Enum64 initially has no enum values in it (and corresponds to enum forward
+ * declaration). Enumerator values can be added by btf__add_enum64_value()
+ * immediately after btf__add_enum64() succeeds.
+ *
+ * Returns:
+ *   - >0, type ID of newly added BTF type;
+ *   - <0, on error.
+ */
+int btf__add_enum64(struct btf *btf, const char *name, bool is_unsigned)
+{
+	return btf_add_enum_common(btf, name, is_unsigned, BTF_KIND_ENUM64, 8);
+}
+
+/*
+ * Append new enum value for the current ENUM64 type with:
+ *   - *name* - name of the enumerator value, can't be NULL or empty;
+ *   - *value* - integer value corresponding to enum value *name*;
+ * Returns:
+ *   -  0, on success;
+ *   - <0, on error.
+ */
+int btf__add_enum64_value(struct btf *btf, const char *name, __u64 value)
+{
+	struct btf_enum64 *v;
+	struct btf_type *t;
+	int sz, name_off;
+
+	/* last type should be BTF_KIND_ENUM64 */
+	if (btf->nr_types == 0)
+		return libbpf_err(-EINVAL);
+	t = btf_last_type(btf);
+	if (!btf_is_enum64(t))
+		return libbpf_err(-EINVAL);
+
+	/* non-empty name */
+	if (!name || !name[0])
+		return libbpf_err(-EINVAL);
+
+	/* decompose and invalidate raw data */
+	if (btf_ensure_modifiable(btf))
+		return libbpf_err(-ENOMEM);
+
+	sz = sizeof(struct btf_enum64);
+	v = btf_add_type_mem(btf, sz);
+	if (!v)
+		return libbpf_err(-ENOMEM);
+
+	name_off = btf__add_str(btf, name);
+	if (name_off < 0)
+		return name_off;
+
+	v->name_off = name_off;
+	v->hi32 = value >> 32;
+	v->lo32 = (__u32)value;
+
+	/* update parent type's vlen */
+	t = btf_last_type(btf);
+	btf_type_inc_vlen(t);
+
+	btf->hdr->type_len += sz;
+	btf->hdr->str_off += sz;
+	return 0;
+}
+
 /*
  * Append new BTF_KIND_FWD type with:
  *   - *name*, non-empty/non-NULL name;
@@ -2242,7 +2419,7 @@  int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind)
 		/* enum forward in BTF currently is just an enum with no enum
 		 * values; we also assume a standard 4-byte size for it
 		 */
-		return btf__add_enum(btf, name, sizeof(int));
+		return btf__add_enum32(btf, name, false);
 	default:
 		return libbpf_err(-EINVAL);
 	}
@@ -3485,6 +3662,7 @@  static long btf_hash_enum(struct btf_type *t)
 /* Check structural equality of two ENUMs. */
 static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
 {
+	const struct btf_enum64 *n1, *n2;
 	const struct btf_enum *m1, *m2;
 	__u16 vlen;
 	int i;
@@ -3493,26 +3671,40 @@  static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
 		return false;
 
 	vlen = btf_vlen(t1);
-	m1 = btf_enum(t1);
-	m2 = btf_enum(t2);
-	for (i = 0; i < vlen; i++) {
-		if (m1->name_off != m2->name_off || m1->val != m2->val)
-			return false;
-		m1++;
-		m2++;
+	if (btf_is_enum(t1)) {
+		m1 = btf_enum(t1);
+		m2 = btf_enum(t2);
+		for (i = 0; i < vlen; i++) {
+			if (m1->name_off != m2->name_off || m1->val != m2->val)
+				return false;
+			m1++;
+			m2++;
+		}
+	} else {
+		n1 = btf_enum64(t1);
+		n2 = btf_enum64(t2);
+		for (i = 0; i < vlen; i++) {
+			if (n1->name_off != n2->name_off || n1->hi32 != n2->hi32 ||
+			    n1->lo32 != n2->lo32)
+				return false;
+			n1++;
+			n2++;
+		}
 	}
 	return true;
 }
 
 static inline bool btf_is_enum_fwd(struct btf_type *t)
 {
-	return btf_is_enum(t) && btf_vlen(t) == 0;
+	return (btf_is_enum(t) || btf_is_enum64(t)) && btf_vlen(t) == 0;
 }
 
 static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
 {
-	if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
+	if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2)) {
 		return btf_equal_enum(t1, t2);
+	}
+
 	/* ignore vlen when comparing */
 	return t1->name_off == t2->name_off &&
 	       (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
@@ -3731,6 +3923,7 @@  static int btf_dedup_prep(struct btf_dedup *d)
 			h = btf_hash_int_decl_tag(t);
 			break;
 		case BTF_KIND_ENUM:
+		case BTF_KIND_ENUM64:
 			h = btf_hash_enum(t);
 			break;
 		case BTF_KIND_STRUCT:
@@ -3800,6 +3993,7 @@  static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
 		break;
 
 	case BTF_KIND_ENUM:
+	case BTF_KIND_ENUM64:
 		h = btf_hash_enum(t);
 		for_each_dedup_cand(d, hash_entry, h) {
 			cand_id = (__u32)(long)hash_entry->value;
@@ -4113,6 +4307,7 @@  static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
 		return btf_equal_int_tag(cand_type, canon_type);
 
 	case BTF_KIND_ENUM:
+	case BTF_KIND_ENUM64:
 		return btf_compat_enum(cand_type, canon_type);
 
 	case BTF_KIND_FWD:
@@ -4717,6 +4912,7 @@  int btf_type_visit_type_ids(struct btf_type *t, type_id_visit_fn visit, void *ct
 	case BTF_KIND_INT:
 	case BTF_KIND_FLOAT:
 	case BTF_KIND_ENUM:
+	case BTF_KIND_ENUM64:
 		return 0;
 
 	case BTF_KIND_FWD:
@@ -4811,6 +5007,16 @@  int btf_type_visit_str_offs(struct btf_type *t, str_off_visit_fn visit, void *ct
 		}
 		break;
 	}
+	case BTF_KIND_ENUM64: {
+		struct btf_enum64 *m = btf_enum64(t);
+
+		for (i = 0, n = btf_vlen(t); i < n; i++, m++) {
+			err = visit(&m->name_off, ctx);
+			if (err)
+				return err;
+		}
+		break;
+	}
 	case BTF_KIND_FUNC_PROTO: {
 		struct btf_param *m = btf_params(t);
 
diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
index 951ac7475794..90f35bc00038 100644
--- a/tools/lib/bpf/btf.h
+++ b/tools/lib/bpf/btf.h
@@ -213,8 +213,14 @@  LIBBPF_API int btf__add_field(struct btf *btf, const char *name, int field_type_
 			      __u32 bit_offset, __u32 bit_size);
 
 /* enum construction APIs */
+LIBBPF_DEPRECATED_SINCE(0, 8, "btf__add_enum is deprecated; use btf__add_enum32 or btf__add_enum64")
 LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32 bytes_sz);
+LIBBPF_DEPRECATED_SINCE(0, 8, "btf__add_enum_value is deprecated; use btf_add_enum32_value or btf_add_enum64_value")
 LIBBPF_API int btf__add_enum_value(struct btf *btf, const char *name, __s64 value);
+LIBBPF_API int btf__add_enum32(struct btf *btf, const char *name, bool is_unsigned);
+LIBBPF_API int btf__add_enum32_value(struct btf *btf, const char *name, __s32 value);
+LIBBPF_API int btf__add_enum64(struct btf *btf, const char *name, bool is_unsigned);
+LIBBPF_API int btf__add_enum64_value(struct btf *btf, const char *name, __u64 value);
 
 enum btf_fwd_kind {
 	BTF_FWD_STRUCT = 0,
@@ -454,6 +460,11 @@  static inline bool btf_is_enum(const struct btf_type *t)
 	return btf_kind(t) == BTF_KIND_ENUM;
 }
 
+static inline bool btf_is_enum64(const struct btf_type *t)
+{
+	return btf_kind(t) == BTF_KIND_ENUM64;
+}
+
 static inline bool btf_is_fwd(const struct btf_type *t)
 {
 	return btf_kind(t) == BTF_KIND_FWD;
@@ -549,6 +560,16 @@  static inline struct btf_enum *btf_enum(const struct btf_type *t)
 	return (struct btf_enum *)(t + 1);
 }
 
+static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
+{
+	return (struct btf_enum64 *)(t + 1);
+}
+
+static inline __u64 btf_enum64_value(const struct btf_enum64 *e)
+{
+	return (__u64)e->hi32 << 32 | e->lo32;
+}
+
 static inline struct btf_member *btf_members(const struct btf_type *t)
 {
 	return (struct btf_member *)(t + 1);
diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 6b1bc1f43728..c3f99ca29426 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -320,6 +320,7 @@  static int btf_dump_mark_referenced(struct btf_dump *d)
 		case BTF_KIND_ENUM:
 		case BTF_KIND_FWD:
 		case BTF_KIND_FLOAT:
+		case BTF_KIND_ENUM64:
 			break;
 
 		case BTF_KIND_VOLATILE:
@@ -539,6 +540,7 @@  static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
 	}
 	case BTF_KIND_ENUM:
 	case BTF_KIND_FWD:
+	case BTF_KIND_ENUM64:
 		/*
 		 * non-anonymous or non-referenced enums are top-level
 		 * declarations and should be emitted. Same logic can be
@@ -739,6 +741,7 @@  static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
 		tstate->emit_state = EMITTED;
 		break;
 	case BTF_KIND_ENUM:
+	case BTF_KIND_ENUM64:
 		if (top_level_def) {
 			btf_dump_emit_enum_def(d, id, t, 0);
 			btf_dump_printf(d, ";\n\n");
@@ -993,8 +996,11 @@  static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
 				   const struct btf_type *t,
 				   int lvl)
 {
-	const struct btf_enum *v = btf_enum(t);
+	bool is_unsigned = btf_kflag(t);
+	const struct btf_enum64 *v64;
+	const struct btf_enum *v;
 	__u16 vlen = btf_vlen(t);
+	const char *fmt_str;
 	const char *name;
 	size_t dup_cnt;
 	int i;
@@ -1005,18 +1011,47 @@  static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
 
 	if (vlen) {
 		btf_dump_printf(d, " {");
-		for (i = 0; i < vlen; i++, v++) {
-			name = btf_name_of(d, v->name_off);
-			/* enumerators share namespace with typedef idents */
-			dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
-			if (dup_cnt > 1) {
-				btf_dump_printf(d, "\n%s%s___%zu = %u,",
-						pfx(lvl + 1), name, dup_cnt,
-						(__u32)v->val);
-			} else {
-				btf_dump_printf(d, "\n%s%s = %u,",
-						pfx(lvl + 1), name,
-						(__u32)v->val);
+		if (btf_is_enum(t)) {
+			v = btf_enum(t);
+			for (i = 0; i < vlen; i++, v++) {
+				name = btf_name_of(d, v->name_off);
+				/* enumerators share namespace with typedef idents */
+				dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
+				if (dup_cnt > 1) {
+					fmt_str = is_unsigned ? "\n%s%s___%zu = %u,"
+							      : "\n%s%s___%zu = %d,";
+					btf_dump_printf(d, fmt_str,
+							pfx(lvl + 1), name, dup_cnt,
+							v->val);
+				} else {
+					fmt_str = is_unsigned ? "\n%s%s = %u,"
+							      : "\n%s%s = %d,";
+					btf_dump_printf(d, fmt_str,
+							pfx(lvl + 1), name,
+							v->val);
+				}
+			}
+		} else {
+			v64 = btf_enum64(t);
+			for (i = 0; i < vlen; i++, v64++) {
+				__u64 val = btf_enum64_value(v64);
+
+				name = btf_name_of(d, v64->name_off);
+				/* enumerators share namespace with typedef idents */
+				dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
+				if (dup_cnt > 1) {
+					fmt_str = is_unsigned ? "\n%s%s___%zu = %lluULL,"
+							      : "\n%s%s___%zu = %lldLL,";
+					btf_dump_printf(d, fmt_str,
+							pfx(lvl + 1), name, dup_cnt,
+							val);
+				} else {
+					fmt_str = is_unsigned ? "\n%s%s = %lluULL,"
+							      : "\n%s%s = %lldLL,";
+					btf_dump_printf(d, fmt_str,
+							pfx(lvl + 1), name,
+							val);
+				}
 			}
 		}
 		btf_dump_printf(d, "\n%s}", pfx(lvl));
@@ -1183,6 +1218,7 @@  static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
 		case BTF_KIND_UNION:
 		case BTF_KIND_TYPEDEF:
 		case BTF_KIND_FLOAT:
+		case BTF_KIND_ENUM64:
 			goto done;
 		default:
 			pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
@@ -1312,6 +1348,7 @@  static void btf_dump_emit_type_chain(struct btf_dump *d,
 				btf_dump_emit_struct_fwd(d, id, t);
 			break;
 		case BTF_KIND_ENUM:
+		case BTF_KIND_ENUM64:
 			btf_dump_emit_mods(d, decls);
 			/* inline anonymous enum */
 			if (t->name_off == 0 && !d->skip_anon_defs)
@@ -2024,7 +2061,9 @@  static int btf_dump_enum_data(struct btf_dump *d,
 			      __u32 id,
 			      const void *data)
 {
+	const struct btf_enum64 *e64;
 	const struct btf_enum *e;
+	bool is_unsigned;
 	__s64 value;
 	int i, err;
 
@@ -2032,14 +2071,26 @@  static int btf_dump_enum_data(struct btf_dump *d,
 	if (err)
 		return err;
 
-	for (i = 0, e = btf_enum(t); i < btf_vlen(t); i++, e++) {
-		if (value != e->val)
-			continue;
-		btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off));
-		return 0;
-	}
+	is_unsigned = btf_kflag(t);
+	if (btf_is_enum(t)) {
+		for (i = 0, e = btf_enum(t); i < btf_vlen(t); i++, e++) {
+			if (value != e->val)
+				continue;
+			btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off));
+			return 0;
+		}
 
-	btf_dump_type_values(d, "%d", value);
+		btf_dump_type_values(d, is_unsigned ? "%u" : "%d", value);
+	} else {
+		for (i = 0, e64 = btf_enum64(t); i < btf_vlen(t); i++, e64++) {
+			if (value != btf_enum64_value(e64))
+				continue;
+			btf_dump_type_values(d, "%s", btf_name_of(d, e64->name_off));
+			return 0;
+		}
+
+		btf_dump_type_values(d, is_unsigned ? "%lluULL" : "%lldLL", value);
+	}
 	return 0;
 }
 
@@ -2099,6 +2150,7 @@  static int btf_dump_type_data_check_overflow(struct btf_dump *d,
 	case BTF_KIND_FLOAT:
 	case BTF_KIND_PTR:
 	case BTF_KIND_ENUM:
+	case BTF_KIND_ENUM64:
 		if (data + bits_offset / 8 + size > d->typed_dump->data_end)
 			return -E2BIG;
 		break;
@@ -2203,6 +2255,7 @@  static int btf_dump_type_data_check_zero(struct btf_dump *d,
 		return -ENODATA;
 	}
 	case BTF_KIND_ENUM:
+	case BTF_KIND_ENUM64:
 		err = btf_dump_get_enum_value(d, t, data, id, &value);
 		if (err)
 			return err;
@@ -2275,6 +2328,7 @@  static int btf_dump_dump_type_data(struct btf_dump *d,
 		err = btf_dump_struct_data(d, t, id, data);
 		break;
 	case BTF_KIND_ENUM:
+	case BTF_KIND_ENUM64:
 		/* handle bitfield and int enum values */
 		if (bit_sz) {
 			__u64 print_num;
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 63c0f412266c..2e8b843ff5ef 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -2114,6 +2114,7 @@  static const char *__btf_kind_str(__u16 kind)
 	case BTF_KIND_FLOAT: return "float";
 	case BTF_KIND_DECL_TAG: return "decl_tag";
 	case BTF_KIND_TYPE_TAG: return "type_tag";
+	case BTF_KIND_ENUM64: return "enum64";
 	default: return "unknown";
 	}
 }
@@ -2642,9 +2643,10 @@  static bool btf_needs_sanitization(struct bpf_object *obj)
 	bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
 	bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
 	bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG);
+	bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64);
 
 	return !has_func || !has_datasec || !has_func_global || !has_float ||
-	       !has_decl_tag || !has_type_tag;
+	       !has_decl_tag || !has_type_tag || !has_enum64;
 }
 
 static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
@@ -2655,6 +2657,7 @@  static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
 	bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
 	bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
 	bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG);
+	bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64);
 	struct btf_type *t;
 	int i, j, vlen;
 
@@ -2717,6 +2720,17 @@  static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
 			/* replace TYPE_TAG with a CONST */
 			t->name_off = 0;
 			t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0);
+		} else if (!has_enum64 && btf_is_enum(t)) {
+			/* clear the kflag */
+			t->info &= 0x7fffffff;
+		} else if (!has_enum64 && btf_is_enum64(t)) {
+			/* replace ENUM64 with pointer->void's */
+			vlen = btf_vlen(t);
+			for (j = 0; j <= vlen; j++, t++) {
+				t->name_off = 0;
+				t->info = BTF_INFO_ENC(BTF_KIND_PTR, 0, 0);
+				t->type = 0;
+			}
 		}
 	}
 }
@@ -3563,6 +3577,12 @@  static enum kcfg_type find_kcfg_type(const struct btf *btf, int id,
 		if (strcmp(name, "libbpf_tristate"))
 			return KCFG_UNKNOWN;
 		return KCFG_TRISTATE;
+	case BTF_KIND_ENUM64:
+		if (t->size != 8)
+			return KCFG_UNKNOWN;
+		if (strcmp(name, "libbpf_tristate"))
+			return KCFG_UNKNOWN;
+		return KCFG_TRISTATE;
 	case BTF_KIND_ARRAY:
 		if (btf_array(t)->nelems == 0)
 			return KCFG_UNKNOWN;
@@ -4746,6 +4766,17 @@  static int probe_kern_bpf_cookie(void)
 	return probe_fd(ret);
 }
 
+static int probe_kern_btf_enum64(void)
+{
+	static const char strs[] = "\0enum64";
+	__u32 types[] = {
+		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
+	};
+
+	return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
+					     strs, sizeof(strs)));
+}
+
 enum kern_feature_result {
 	FEAT_UNKNOWN = 0,
 	FEAT_SUPPORTED = 1,
@@ -4811,6 +4842,9 @@  static struct kern_feature_desc {
 	[FEAT_BPF_COOKIE] = {
 		"BPF cookie support", probe_kern_bpf_cookie,
 	},
+	[FEAT_BTF_ENUM64] = {
+		"BTF_KIND_ENUM64 support", probe_kern_btf_enum64,
+	},
 };
 
 bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)
@@ -5296,6 +5330,15 @@  void bpf_core_free_cands(struct bpf_core_cand_list *cands)
 	free(cands);
 }
 
+static bool btf_is_enum_enum64(const struct btf_type *t1,
+			       const struct btf_type *t2) {
+	if (btf_is_enum(t1) && btf_is_enum64(t2))
+		return true;
+	if (btf_is_enum(t2) && btf_is_enum64(t1))
+		return true;
+	return false;
+}
+
 int bpf_core_add_cands(struct bpf_core_cand *local_cand,
 		       size_t local_essent_len,
 		       const struct btf *targ_btf,
@@ -5315,8 +5358,10 @@  int bpf_core_add_cands(struct bpf_core_cand *local_cand,
 	n = btf__type_cnt(targ_btf);
 	for (i = targ_start_id; i < n; i++) {
 		t = btf__type_by_id(targ_btf, i);
-		if (btf_kind(t) != btf_kind(local_t))
-			continue;
+		if (btf_kind(t) != btf_kind(local_t)) {
+			if (!btf_is_enum_enum64(t, local_t))
+				continue;
+		}
 
 		targ_name = btf__name_by_offset(targ_btf, t->name_off);
 		if (str_is_empty(targ_name))
@@ -5529,8 +5574,10 @@  int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
 	/* caller made sure that names match (ignoring flavor suffix) */
 	local_type = btf__type_by_id(local_btf, local_id);
 	targ_type = btf__type_by_id(targ_btf, targ_id);
-	if (btf_kind(local_type) != btf_kind(targ_type))
-		return 0;
+	if (btf_kind(local_type) != btf_kind(targ_type)) {
+		if (!btf_is_enum_enum64(local_type, targ_type))
+			return 0;
+	}
 
 recur:
 	depth--;
@@ -5542,8 +5589,10 @@  int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
 	if (!local_type || !targ_type)
 		return -EINVAL;
 
-	if (btf_kind(local_type) != btf_kind(targ_type))
-		return 0;
+	if (btf_kind(local_type) != btf_kind(targ_type)) {
+		if (!btf_is_enum_enum64(local_type, targ_type))
+			return 0;
+	}
 
 	switch (btf_kind(local_type)) {
 	case BTF_KIND_UNKN:
@@ -5551,6 +5600,7 @@  int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
 	case BTF_KIND_UNION:
 	case BTF_KIND_ENUM:
 	case BTF_KIND_FWD:
+	case BTF_KIND_ENUM64:
 		return 1;
 	case BTF_KIND_INT:
 		/* just reject deprecated bitfield-like integers; all other
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index b5bc84039407..acde13bd48c8 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -448,6 +448,10 @@  LIBBPF_0.8.0 {
 		bpf_object__open_subskeleton;
 		bpf_program__attach_kprobe_multi_opts;
 		bpf_program__attach_usdt;
+		btf__add_enum32;
+		btf__add_enum32_value;
+		btf__add_enum64;
+		btf__add_enum64_value;
 		libbpf_register_prog_handler;
 		libbpf_unregister_prog_handler;
 } LIBBPF_0.7.0;
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index 4abdbe2fea9d..10c16acfa8ae 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -351,6 +351,8 @@  enum kern_feature_id {
 	FEAT_MEMCG_ACCOUNT,
 	/* BPF cookie (bpf_get_attach_cookie() BPF helper) support */
 	FEAT_BPF_COOKIE,
+	/* BTF_KIND_ENUM64 support and BTF_KIND_ENUM kflag support */
+	FEAT_BTF_ENUM64,
 	__FEAT_CNT,
 };
 
diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
index 9aa016fb55aa..1e1ef3302921 100644
--- a/tools/lib/bpf/linker.c
+++ b/tools/lib/bpf/linker.c
@@ -1343,6 +1343,7 @@  static bool glob_sym_btf_matches(const char *sym_name, bool exact,
 	case BTF_KIND_FWD:
 	case BTF_KIND_FUNC:
 	case BTF_KIND_VAR:
+	case BTF_KIND_ENUM64:
 		n1 = btf__str_by_offset(btf1, t1->name_off);
 		n2 = btf__str_by_offset(btf2, t2->name_off);
 		if (strcmp(n1, n2) != 0) {
@@ -1358,6 +1359,7 @@  static bool glob_sym_btf_matches(const char *sym_name, bool exact,
 	switch (btf_kind(t1)) {
 	case BTF_KIND_UNKN: /* void */
 	case BTF_KIND_FWD:
+	case BTF_KIND_ENUM64:
 		return true;
 	case BTF_KIND_INT:
 	case BTF_KIND_FLOAT:
diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
index f25ffd03c3b1..1e751400427b 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -231,11 +231,15 @@  int bpf_core_parse_spec(const char *prog_name, const struct btf *btf,
 	spec->len++;
 
 	if (core_relo_is_enumval_based(relo->kind)) {
-		if (!btf_is_enum(t) || spec->raw_len > 1 || access_idx >= btf_vlen(t))
+		if (!(btf_is_enum(t) || btf_is_enum64(t)) ||
+		    spec->raw_len > 1 || access_idx >= btf_vlen(t))
 			return -EINVAL;
 
 		/* record enumerator name in a first accessor */
-		acc->name = btf__name_by_offset(btf, btf_enum(t)[access_idx].name_off);
+		if (btf_is_enum(t))
+			acc->name = btf__name_by_offset(btf, btf_enum(t)[access_idx].name_off);
+		else
+			acc->name = btf__name_by_offset(btf, btf_enum64(t)[access_idx].name_off);
 		return 0;
 	}
 
@@ -340,15 +344,19 @@  static int bpf_core_fields_are_compat(const struct btf *local_btf,
 
 	if (btf_is_composite(local_type) && btf_is_composite(targ_type))
 		return 1;
-	if (btf_kind(local_type) != btf_kind(targ_type))
-		return 0;
+	if (btf_kind(local_type) != btf_kind(targ_type)) {
+		if (btf_is_enum(local_type) && btf_is_enum64(targ_type)) ;
+		else if (btf_is_enum64(local_type) && btf_is_enum(targ_type)) ;
+		else return 0;
+	}
 
 	switch (btf_kind(local_type)) {
 	case BTF_KIND_PTR:
 	case BTF_KIND_FLOAT:
 		return 1;
 	case BTF_KIND_FWD:
-	case BTF_KIND_ENUM: {
+	case BTF_KIND_ENUM:
+	case BTF_KIND_ENUM64: {
 		const char *local_name, *targ_name;
 		size_t local_len, targ_len;
 
@@ -494,29 +502,48 @@  static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
 
 	if (core_relo_is_enumval_based(local_spec->relo_kind)) {
 		size_t local_essent_len, targ_essent_len;
+		const struct btf_enum64 *e64;
 		const struct btf_enum *e;
 		const char *targ_name;
 
 		/* has to resolve to an enum */
 		targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id, &targ_id);
-		if (!btf_is_enum(targ_type))
+		if (!btf_is_enum(targ_type) && !btf_is_enum64(targ_type))
 			return 0;
 
 		local_essent_len = bpf_core_essential_name_len(local_acc->name);
 
-		for (i = 0, e = btf_enum(targ_type); i < btf_vlen(targ_type); i++, e++) {
-			targ_name = btf__name_by_offset(targ_spec->btf, e->name_off);
-			targ_essent_len = bpf_core_essential_name_len(targ_name);
-			if (targ_essent_len != local_essent_len)
-				continue;
-			if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) {
-				targ_acc->type_id = targ_id;
-				targ_acc->idx = i;
-				targ_acc->name = targ_name;
-				targ_spec->len++;
-				targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
-				targ_spec->raw_len++;
-				return 1;
+		if (btf_is_enum(targ_type)) {
+			for (i = 0, e = btf_enum(targ_type); i < btf_vlen(targ_type); i++, e++) {
+				targ_name = btf__name_by_offset(targ_spec->btf, e->name_off);
+				targ_essent_len = bpf_core_essential_name_len(targ_name);
+				if (targ_essent_len != local_essent_len)
+					continue;
+				if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) {
+					targ_acc->type_id = targ_id;
+					targ_acc->idx = i;
+					targ_acc->name = targ_name;
+					targ_spec->len++;
+					targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
+					targ_spec->raw_len++;
+					return 1;
+				}
+			}
+		} else {
+			for (i = 0, e64 = btf_enum64(targ_type); i < btf_vlen(targ_type); i++, e64++) {
+				targ_name = btf__name_by_offset(targ_spec->btf, e64->name_off);
+				targ_essent_len = bpf_core_essential_name_len(targ_name);
+				if (targ_essent_len != local_essent_len)
+					continue;
+				if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) {
+					targ_acc->type_id = targ_id;
+					targ_acc->idx = i;
+					targ_acc->name = targ_name;
+					targ_spec->len++;
+					targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
+					targ_spec->raw_len++;
+					return 1;
+				}
 			}
 		}
 		return 0;
@@ -681,7 +708,7 @@  static int bpf_core_calc_field_relo(const char *prog_name,
 		break;
 	case BPF_CORE_FIELD_SIGNED:
 		/* enums will be assumed unsigned */
-		*val = btf_is_enum(mt) ||
+		*val = btf_is_enum(mt) || btf_is_enum64(mt) ||
 		       (btf_int_encoding(mt) & BTF_INT_SIGNED);
 		if (validate)
 			*validate = true; /* signedness is never ambiguous */
@@ -753,6 +780,7 @@  static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
 				      const struct bpf_core_spec *spec,
 				      __u64 *val)
 {
+	const struct btf_enum64 *e64;
 	const struct btf_type *t;
 	const struct btf_enum *e;
 
@@ -764,8 +792,13 @@  static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
 		if (!spec)
 			return -EUCLEAN; /* request instruction poisoning */
 		t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
-		e = btf_enum(t) + spec->spec[0].idx;
-		*val = e->val;
+		if (btf_is_enum(t)) {
+			e = btf_enum(t) + spec->spec[0].idx;
+			*val = e->val;
+		} else {
+			e64 = btf_enum64(t) + spec->spec[0].idx;
+			*val = btf_enum64_value(e64);
+		}
 		break;
 	default:
 		return -EOPNOTSUPP;
@@ -1034,7 +1067,7 @@  int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
 		}
 
 		insn[0].imm = new_val;
-		insn[1].imm = 0; /* currently only 32-bit values are supported */
+		insn[1].imm = new_val >> 32;
 		pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %llu\n",
 			 prog_name, relo_idx, insn_idx,
 			 (unsigned long long)imm, new_val);
@@ -1056,6 +1089,7 @@  int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
  */
 int bpf_core_format_spec(char *buf, size_t buf_sz, const struct bpf_core_spec *spec)
 {
+	const struct btf_enum64 *e64;
 	const struct btf_type *t;
 	const struct btf_enum *e;
 	const char *s;
@@ -1086,10 +1120,15 @@  int bpf_core_format_spec(char *buf, size_t buf_sz, const struct bpf_core_spec *s
 
 	if (core_relo_is_enumval_based(spec->relo_kind)) {
 		t = skip_mods_and_typedefs(spec->btf, type_id, NULL);
-		e = btf_enum(t) + spec->raw_spec[0];
-		s = btf__name_by_offset(spec->btf, e->name_off);
-
-		append_buf("::%s = %u", s, e->val);
+		if (btf_is_enum(t)) {
+			e = btf_enum(t) + spec->raw_spec[0];
+			s = btf__name_by_offset(spec->btf, e->name_off);
+			append_buf("::%s = %u", s, e->val);
+		} else {
+			e64 = btf_enum64(t) + spec->raw_spec[0];
+			s = btf__name_by_offset(spec->btf, e64->name_off);
+			append_buf("::%s = %llu", s, btf_enum64_value(e64));
+		}
 		return len;
 	}
 
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dump.c b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
index 5fce7008d1ff..1d3ac4496e7b 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_dump.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
@@ -159,16 +159,16 @@  static void test_btf_dump_incremental(void)
 	 * struct s { int x; };
 	 *
 	 */
-	id = btf__add_enum(btf, "x", 4);
+	id = btf__add_enum32(btf, "x", false);
 	ASSERT_EQ(id, 1, "enum_declaration_id");
-	id = btf__add_enum(btf, "x", 4);
+	id = btf__add_enum32(btf, "x", true);
 	ASSERT_EQ(id, 2, "named_enum_id");
-	err = btf__add_enum_value(btf, "X", 1);
+	err = btf__add_enum32_value(btf, "X", 1);
 	ASSERT_OK(err, "named_enum_val_ok");
 
-	id = btf__add_enum(btf, NULL, 4);
+	id = btf__add_enum32(btf, NULL, true);
 	ASSERT_EQ(id, 3, "anon_enum_id");
-	err = btf__add_enum_value(btf, "Y", 1);
+	err = btf__add_enum32_value(btf, "Y", 1);
 	ASSERT_OK(err, "anon_enum_val_ok");
 
 	id = btf__add_int(btf, "int", 4, BTF_INT_SIGNED);
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_write.c b/tools/testing/selftests/bpf/prog_tests/btf_write.c
index addf99c05896..be958ab26ebd 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_write.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_write.c
@@ -152,11 +152,11 @@  static void gen_btf(struct btf *btf)
 		     "\t'f1' type_id=1 bits_offset=0 bitfield_size=16", "raw_dump");
 
 	/* ENUM */
-	id = btf__add_enum(btf, "e1", 4);
+	id = btf__add_enum32(btf, "e1", true);
 	ASSERT_EQ(id, 9, "enum_id");
-	err = btf__add_enum_value(btf, "v1", 1);
+	err = btf__add_enum32_value(btf, "v1", 1);
 	ASSERT_OK(err, "v1_res");
-	err = btf__add_enum_value(btf, "v2", 2);
+	err = btf__add_enum32_value(btf, "v2", 2);
 	ASSERT_OK(err, "v2_res");
 
 	t = btf__type_by_id(btf, 9);