All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf] libbpf: define BTF_KIND_* constants in btf.h to avoid compilation errors
@ 2022-01-18 14:13 Toke Høiland-Jørgensen
  2022-01-18 15:35 ` Arnaldo Carvalho de Melo
  2022-01-19  4:00 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 4+ messages in thread
From: Toke Høiland-Jørgensen @ 2022-01-18 14:13 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh
  Cc: Toke Høiland-Jørgensen, netdev, bpf

The btf.h header included with libbpf contains inline helper functions to
check for various BTF kinds. These helpers directly reference the
BTF_KIND_* constants defined in the kernel header, and because the header
file is included in user applications, this happens in the user application
compile units.

This presents a problem if a user application is compiled on a system with
older kernel headers because the constants are not available. To avoid
this, add #defines of the constants directly in btf.h before using them.

Since the kernel header moved to an enum for BTF_KIND_*, the #defines can
shadow the enum values without any errors, so we only need #ifndef guards
for the constants that predates the conversion to enum. We group these so
there's only one guard for groups of values that were added together.

  [0] Closes: https://github.com/libbpf/libbpf/issues/436

Fixes: 223f903e9c83 ("bpf: Rename BTF_KIND_TAG to BTF_KIND_DECL_TAG")
Fixes: 5b84bd10363e ("libbpf: Add support for BTF_KIND_TAG")
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
---
 tools/lib/bpf/btf.h | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
index 061839f04525..51862fdee850 100644
--- a/tools/lib/bpf/btf.h
+++ b/tools/lib/bpf/btf.h
@@ -375,8 +375,28 @@ btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
 			 const struct btf_dump_type_data_opts *opts);
 
 /*
- * A set of helpers for easier BTF types handling
+ * A set of helpers for easier BTF types handling.
+ *
+ * The inline functions below rely on constants from the kernel headers which
+ * may not be available for applications including this header file. To avoid
+ * compilation errors, we define all the constants here that were added after
+ * the initial introduction of the BTF_KIND* constants.
  */
+#ifndef BTF_KIND_FUNC
+#define BTF_KIND_FUNC		12	/* Function	*/
+#define BTF_KIND_FUNC_PROTO	13	/* Function Proto	*/
+#endif
+#ifndef BTF_KIND_VAR
+#define BTF_KIND_VAR		14	/* Variable	*/
+#define BTF_KIND_DATASEC	15	/* Section	*/
+#endif
+#ifndef BTF_KIND_FLOAT
+#define BTF_KIND_FLOAT		16	/* Floating point	*/
+#endif
+/* The kernel header switched to enums, so these two were never #defined */
+#define BTF_KIND_DECL_TAG	17	/* Decl Tag */
+#define BTF_KIND_TYPE_TAG	18	/* Type Tag */
+
 static inline __u16 btf_kind(const struct btf_type *t)
 {
 	return BTF_INFO_KIND(t->info);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] libbpf: define BTF_KIND_* constants in btf.h to avoid compilation errors
  2022-01-18 14:13 [PATCH bpf] libbpf: define BTF_KIND_* constants in btf.h to avoid compilation errors Toke Høiland-Jørgensen
@ 2022-01-18 15:35 ` Arnaldo Carvalho de Melo
  2022-01-18 16:17   ` Toke Høiland-Jørgensen
  2022-01-19  4:00 ` patchwork-bot+netdevbpf
  1 sibling, 1 reply; 4+ messages in thread
From: Arnaldo Carvalho de Melo @ 2022-01-18 15:35 UTC (permalink / raw)
  To: Toke Høiland-Jørgensen
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, netdev, bpf

Em Tue, Jan 18, 2022 at 03:13:27PM +0100, Toke Høiland-Jørgensen escreveu:
> The btf.h header included with libbpf contains inline helper functions to
> check for various BTF kinds. These helpers directly reference the
> BTF_KIND_* constants defined in the kernel header, and because the header
> file is included in user applications, this happens in the user application
> compile units.
> 
> This presents a problem if a user application is compiled on a system with
> older kernel headers because the constants are not available. To avoid
> this, add #defines of the constants directly in btf.h before using them.
> 
> Since the kernel header moved to an enum for BTF_KIND_*, the #defines can
> shadow the enum values without any errors, so we only need #ifndef guards
> for the constants that predates the conversion to enum. We group these so
> there's only one guard for groups of values that were added together.
> 
>   [0] Closes: https://github.com/libbpf/libbpf/issues/436

The coexistence of enums with the defines (in turn #ifndef guarded) as
something I hadn't considered, clever.

Should fix lots of build errors in my test containers :-)

FWIW:

Acked-by: Arnaldo Carvalho de Melo <acme@redhat.com>
 
> Fixes: 223f903e9c83 ("bpf: Rename BTF_KIND_TAG to BTF_KIND_DECL_TAG")
> Fixes: 5b84bd10363e ("libbpf: Add support for BTF_KIND_TAG")
> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
> ---
>  tools/lib/bpf/btf.h | 22 +++++++++++++++++++++-
>  1 file changed, 21 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
> index 061839f04525..51862fdee850 100644
> --- a/tools/lib/bpf/btf.h
> +++ b/tools/lib/bpf/btf.h
> @@ -375,8 +375,28 @@ btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
>  			 const struct btf_dump_type_data_opts *opts);
>  
>  /*
> - * A set of helpers for easier BTF types handling
> + * A set of helpers for easier BTF types handling.
> + *
> + * The inline functions below rely on constants from the kernel headers which
> + * may not be available for applications including this header file. To avoid
> + * compilation errors, we define all the constants here that were added after
> + * the initial introduction of the BTF_KIND* constants.
>   */
> +#ifndef BTF_KIND_FUNC
> +#define BTF_KIND_FUNC		12	/* Function	*/
> +#define BTF_KIND_FUNC_PROTO	13	/* Function Proto	*/
> +#endif
> +#ifndef BTF_KIND_VAR
> +#define BTF_KIND_VAR		14	/* Variable	*/
> +#define BTF_KIND_DATASEC	15	/* Section	*/
> +#endif
> +#ifndef BTF_KIND_FLOAT
> +#define BTF_KIND_FLOAT		16	/* Floating point	*/
> +#endif
> +/* The kernel header switched to enums, so these two were never #defined */
> +#define BTF_KIND_DECL_TAG	17	/* Decl Tag */
> +#define BTF_KIND_TYPE_TAG	18	/* Type Tag */
> +
>  static inline __u16 btf_kind(const struct btf_type *t)
>  {
>  	return BTF_INFO_KIND(t->info);
> -- 
> 2.34.1

-- 

- Arnaldo

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] libbpf: define BTF_KIND_* constants in btf.h to avoid compilation errors
  2022-01-18 15:35 ` Arnaldo Carvalho de Melo
@ 2022-01-18 16:17   ` Toke Høiland-Jørgensen
  0 siblings, 0 replies; 4+ messages in thread
From: Toke Høiland-Jørgensen @ 2022-01-18 16:17 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, netdev, bpf

Arnaldo Carvalho de Melo <acme@kernel.org> writes:

> Em Tue, Jan 18, 2022 at 03:13:27PM +0100, Toke Høiland-Jørgensen escreveu:
>> The btf.h header included with libbpf contains inline helper functions to
>> check for various BTF kinds. These helpers directly reference the
>> BTF_KIND_* constants defined in the kernel header, and because the header
>> file is included in user applications, this happens in the user application
>> compile units.
>> 
>> This presents a problem if a user application is compiled on a system with
>> older kernel headers because the constants are not available. To avoid
>> this, add #defines of the constants directly in btf.h before using them.
>> 
>> Since the kernel header moved to an enum for BTF_KIND_*, the #defines can
>> shadow the enum values without any errors, so we only need #ifndef guards
>> for the constants that predates the conversion to enum. We group these so
>> there's only one guard for groups of values that were added together.
>> 
>>   [0] Closes: https://github.com/libbpf/libbpf/issues/436
>
> The coexistence of enums with the defines (in turn #ifndef guarded) as
> something I hadn't considered, clever.

Me neither - that bit was Andrii's idea :)

> Should fix lots of build errors in my test containers :-)
>
> FWIW:
>
> Acked-by: Arnaldo Carvalho de Melo <acme@redhat.com>

Thanks!

-Toke


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] libbpf: define BTF_KIND_* constants in btf.h to avoid compilation errors
  2022-01-18 14:13 [PATCH bpf] libbpf: define BTF_KIND_* constants in btf.h to avoid compilation errors Toke Høiland-Jørgensen
  2022-01-18 15:35 ` Arnaldo Carvalho de Melo
@ 2022-01-19  4:00 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-01-19  4:00 UTC (permalink / raw)
  To: =?utf-8?b?VG9rZSBIw7hpbGFuZC1Kw7hyZ2Vuc2VuIDx0b2tlQHJlZGhhdC5jb20+?=
  Cc: ast, daniel, andrii, kafai, songliubraving, yhs, john.fastabend,
	kpsingh, netdev, bpf

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Tue, 18 Jan 2022 15:13:27 +0100 you wrote:
> The btf.h header included with libbpf contains inline helper functions to
> check for various BTF kinds. These helpers directly reference the
> BTF_KIND_* constants defined in the kernel header, and because the header
> file is included in user applications, this happens in the user application
> compile units.
> 
> This presents a problem if a user application is compiled on a system with
> older kernel headers because the constants are not available. To avoid
> this, add #defines of the constants directly in btf.h before using them.
> 
> [...]

Here is the summary with links:
  - [bpf] libbpf: define BTF_KIND_* constants in btf.h to avoid compilation errors
    https://git.kernel.org/bpf/bpf-next/c/eaa266d83a37

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-01-19  4:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-18 14:13 [PATCH bpf] libbpf: define BTF_KIND_* constants in btf.h to avoid compilation errors Toke Høiland-Jørgensen
2022-01-18 15:35 ` Arnaldo Carvalho de Melo
2022-01-18 16:17   ` Toke Høiland-Jørgensen
2022-01-19  4:00 ` patchwork-bot+netdevbpf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.