Message ID | 20240206220441.38311-13-alexei.starovoitov@gmail.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | bpf: Introduce BPF arena. | expand |
On Tue, Feb 6, 2024 at 2:05 PM Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote: > > From: Alexei Starovoitov <ast@kernel.org> > > __uint() macro that is used to specify map attributes like: > __uint(type, BPF_MAP_TYPE_ARRAY); > __uint(map_flags, BPF_F_MMAPABLE); > is limited to 32-bit, since BTF_KIND_ARRAY has u32 "number of elements" field. > > Introduce __ulong() macro that allows specifying values bigger than 32-bit. > In map definition "map_extra" is the only u64 field. > > Signed-off-by: Alexei Starovoitov <ast@kernel.org> > --- > tools/lib/bpf/bpf_helpers.h | 1 + > tools/lib/bpf/libbpf.c | 44 ++++++++++++++++++++++++++++++++++--- > 2 files changed, 42 insertions(+), 3 deletions(-) > > diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h > index 9c777c21da28..fb909fc6866d 100644 > --- a/tools/lib/bpf/bpf_helpers.h > +++ b/tools/lib/bpf/bpf_helpers.h > @@ -13,6 +13,7 @@ > #define __uint(name, val) int (*name)[val] > #define __type(name, val) typeof(val) *name > #define __array(name, val) typeof(val) *name[] > +#define __ulong(name, val) enum name##__enum { name##__value = val } name Can you try using __ulong() twice in the same file? enum type and value names have global visibility, so I suspect second use with the same field name would cause compilation error > > /* > * Helper macro to place programs, maps, license in > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > index c5ce5946dc6d..a8c89b2315cd 100644 > --- a/tools/lib/bpf/libbpf.c > +++ b/tools/lib/bpf/libbpf.c > @@ -2229,6 +2229,39 @@ static bool get_map_field_int(const char *map_name, const struct btf *btf, > return true; > } > > +static bool get_map_field_long(const char *map_name, const struct btf *btf, > + const struct btf_member *m, __u64 *res) > +{ > + const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); > + const char *name = btf__name_by_offset(btf, m->name_off); > + > + if (btf_is_ptr(t)) > + return false; > + > + if (!btf_is_enum(t) && !btf_is_enum64(t)) { > + pr_warn("map '%s': attr '%s': expected enum or enum64, got %s.\n", > + map_name, name, btf_kind_str(t)); > + return false; > + } > + > + if (btf_vlen(t) != 1) { > + pr_warn("map '%s': attr '%s': invalid __ulong\n", > + map_name, name); > + return false; > + } > + > + if (btf_is_enum(t)) { > + const struct btf_enum *e = btf_enum(t); > + > + *res = e->val; > + } else { > + const struct btf_enum64 *e = btf_enum64(t); > + > + *res = btf_enum64_value(e); > + } > + return true; > +} > + > static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name) > { > int len; > @@ -2462,10 +2495,15 @@ int parse_btf_map_def(const char *map_name, struct btf *btf, > map_def->pinning = val; > map_def->parts |= MAP_DEF_PINNING; > } else if (strcmp(name, "map_extra") == 0) { > - __u32 map_extra; > + __u64 map_extra; > > - if (!get_map_field_int(map_name, btf, m, &map_extra)) > - return -EINVAL; > + if (!get_map_field_long(map_name, btf, m, &map_extra)) { > + __u32 map_extra_u32; > + > + if (!get_map_field_int(map_name, btf, m, &map_extra_u32)) > + return -EINVAL; > + map_extra = map_extra_u32; > + } > map_def->map_extra = map_extra; > map_def->parts |= MAP_DEF_MAP_EXTRA; > } else { > -- > 2.34.1 >
On Wed, Feb 7, 2024 at 5:17 PM Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote: > > On Tue, Feb 6, 2024 at 2:05 PM Alexei Starovoitov > <alexei.starovoitov@gmail.com> wrote: > > > > From: Alexei Starovoitov <ast@kernel.org> > > > > __uint() macro that is used to specify map attributes like: > > __uint(type, BPF_MAP_TYPE_ARRAY); > > __uint(map_flags, BPF_F_MMAPABLE); > > is limited to 32-bit, since BTF_KIND_ARRAY has u32 "number of elements" field. > > > > Introduce __ulong() macro that allows specifying values bigger than 32-bit. > > In map definition "map_extra" is the only u64 field. > > > > Signed-off-by: Alexei Starovoitov <ast@kernel.org> > > --- > > tools/lib/bpf/bpf_helpers.h | 1 + > > tools/lib/bpf/libbpf.c | 44 ++++++++++++++++++++++++++++++++++--- > > 2 files changed, 42 insertions(+), 3 deletions(-) > > > > diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h > > index 9c777c21da28..fb909fc6866d 100644 > > --- a/tools/lib/bpf/bpf_helpers.h > > +++ b/tools/lib/bpf/bpf_helpers.h > > @@ -13,6 +13,7 @@ > > #define __uint(name, val) int (*name)[val] > > #define __type(name, val) typeof(val) *name > > #define __array(name, val) typeof(val) *name[] > > +#define __ulong(name, val) enum name##__enum { name##__value = val } name > > Can you try using __ulong() twice in the same file? enum type and > value names have global visibility, so I suspect second use with the > same field name would cause compilation error Good point will change it to: #define __ulong(name, val) enum { __PASTE(__unique_value,__COUNTER__) = val } name
On Wed, Feb 7, 2024 at 5:59 PM Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote: > > On Wed, Feb 7, 2024 at 5:17 PM Andrii Nakryiko > <andrii.nakryiko@gmail.com> wrote: > > > > On Tue, Feb 6, 2024 at 2:05 PM Alexei Starovoitov > > <alexei.starovoitov@gmail.com> wrote: > > > > > > From: Alexei Starovoitov <ast@kernel.org> > > > > > > __uint() macro that is used to specify map attributes like: > > > __uint(type, BPF_MAP_TYPE_ARRAY); > > > __uint(map_flags, BPF_F_MMAPABLE); > > > is limited to 32-bit, since BTF_KIND_ARRAY has u32 "number of elements" field. > > > > > > Introduce __ulong() macro that allows specifying values bigger than 32-bit. > > > In map definition "map_extra" is the only u64 field. > > > > > > Signed-off-by: Alexei Starovoitov <ast@kernel.org> > > > --- > > > tools/lib/bpf/bpf_helpers.h | 1 + > > > tools/lib/bpf/libbpf.c | 44 ++++++++++++++++++++++++++++++++++--- > > > 2 files changed, 42 insertions(+), 3 deletions(-) > > > > > > diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h > > > index 9c777c21da28..fb909fc6866d 100644 > > > --- a/tools/lib/bpf/bpf_helpers.h > > > +++ b/tools/lib/bpf/bpf_helpers.h > > > @@ -13,6 +13,7 @@ > > > #define __uint(name, val) int (*name)[val] > > > #define __type(name, val) typeof(val) *name > > > #define __array(name, val) typeof(val) *name[] > > > +#define __ulong(name, val) enum name##__enum { name##__value = val } name > > > > Can you try using __ulong() twice in the same file? enum type and > > value names have global visibility, so I suspect second use with the > > same field name would cause compilation error > > Good point will change it to: > > #define __ulong(name, val) enum { __PASTE(__unique_value,__COUNTER__) > = val } name yep, that should work. We still can have name collisions across multiple files, but it doesn't matter when linking two .bpf.o files.
diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h index 9c777c21da28..fb909fc6866d 100644 --- a/tools/lib/bpf/bpf_helpers.h +++ b/tools/lib/bpf/bpf_helpers.h @@ -13,6 +13,7 @@ #define __uint(name, val) int (*name)[val] #define __type(name, val) typeof(val) *name #define __array(name, val) typeof(val) *name[] +#define __ulong(name, val) enum name##__enum { name##__value = val } name /* * Helper macro to place programs, maps, license in diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index c5ce5946dc6d..a8c89b2315cd 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -2229,6 +2229,39 @@ static bool get_map_field_int(const char *map_name, const struct btf *btf, return true; } +static bool get_map_field_long(const char *map_name, const struct btf *btf, + const struct btf_member *m, __u64 *res) +{ + const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); + const char *name = btf__name_by_offset(btf, m->name_off); + + if (btf_is_ptr(t)) + return false; + + if (!btf_is_enum(t) && !btf_is_enum64(t)) { + pr_warn("map '%s': attr '%s': expected enum or enum64, got %s.\n", + map_name, name, btf_kind_str(t)); + return false; + } + + if (btf_vlen(t) != 1) { + pr_warn("map '%s': attr '%s': invalid __ulong\n", + map_name, name); + return false; + } + + if (btf_is_enum(t)) { + const struct btf_enum *e = btf_enum(t); + + *res = e->val; + } else { + const struct btf_enum64 *e = btf_enum64(t); + + *res = btf_enum64_value(e); + } + return true; +} + static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name) { int len; @@ -2462,10 +2495,15 @@ int parse_btf_map_def(const char *map_name, struct btf *btf, map_def->pinning = val; map_def->parts |= MAP_DEF_PINNING; } else if (strcmp(name, "map_extra") == 0) { - __u32 map_extra; + __u64 map_extra; - if (!get_map_field_int(map_name, btf, m, &map_extra)) - return -EINVAL; + if (!get_map_field_long(map_name, btf, m, &map_extra)) { + __u32 map_extra_u32; + + if (!get_map_field_int(map_name, btf, m, &map_extra_u32)) + return -EINVAL; + map_extra = map_extra_u32; + } map_def->map_extra = map_extra; map_def->parts |= MAP_DEF_MAP_EXTRA; } else {