diff mbox series

[RFC,bpf-next,05/12] libbpf: Header guards for selected data structures in vmlinux.h

Message ID 20221025222802.2295103-6-eddyz87@gmail.com (mailing list archive)
State RFC
Delegated to: BPF
Headers show
Series Use uapi kernel headers with vmlinux.h | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-1 pending Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain }}
bpf/vmtest-bpf-next-VM_Test-2 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-3 fail Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 fail Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix
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: 0 this patch: 0
netdev/cc_maintainers warning 7 maintainers not CCed: sdf@google.com john.fastabend@gmail.com haoluo@google.com jolsa@kernel.org kpsingh@kernel.org song@kernel.org martin.lau@linux.dev
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning CHECK: Please use a blank line after function/struct/union/enum declarations WARNING: line length of 93 exceeds 80 columns WARNING: line length of 94 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 12 this patch: 12
netdev/source_inline success Was 0 now: 0

Commit Message

Eduard Zingerman Oct. 25, 2022, 10:27 p.m. UTC
The goal of the patch is to allow usage of header files from
`include/uapi` alongside with `vmlinux.h`. E.g. as follows:

  #include <uapi/linux/tcp.h>
  #include "vmlinux.h"

This goal is achieved by adding #ifndef / #endif guards in vmlinux.h
around definitions that originate from the `include/uapi` headers. The
guards emitted match the guards used in the original headers.
E.g. as follows:

include/uapi/linux/tcp.h:

  #ifndef _UAPI_LINUX_TCP_H
  #define _UAPI_LINUX_TCP_H
  ...
  union tcp_word_hdr {
	struct tcphdr hdr;
	__be32        words[5];
  };
  ...
  #endif /* _UAPI_LINUX_TCP_H */

vmlinux.h:

  ...
  #ifndef _UAPI_LINUX_TCP_H

  union tcp_word_hdr {
	struct tcphdr hdr;
	__be32 words[5];
  };

  #endif /* _UAPI_LINUX_TCP_H */
  ...

The problem of identifying data structures from uapi and selecting
proper guard names is delegated to pahole. When configured pahole
generates fake `BTF_DECL_TAG` records with header guards information.
The fake tag is distinguished from a real tag by a prefix
"header_guard:" in its value. These tags could be present for unions,
structures, enums and typedefs, e.g.:

[24139] STRUCT 'tcphdr' size=20 vlen=17
  ...
[24296] DECL_TAG 'header_guard:_UAPI_LINUX_TCP_H' type_id=24139 ...

This patch adds An option `emit_header_guards` to `struct btf_dump_opts`.
When this option is present the function `btf_dump__dump_type` emits
header guards for top-level declarations. The header guards are
identified by inspecting fake `BTF_DECL_TAG` records described above.

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
---
 tools/lib/bpf/btf.h      |  7 +++-
 tools/lib/bpf/btf_dump.c | 89 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 94 insertions(+), 2 deletions(-)

Comments

Andrii Nakryiko Oct. 27, 2022, 10:44 p.m. UTC | #1
On Tue, Oct 25, 2022 at 3:28 PM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> The goal of the patch is to allow usage of header files from
> `include/uapi` alongside with `vmlinux.h`. E.g. as follows:
>
>   #include <uapi/linux/tcp.h>
>   #include "vmlinux.h"
>
> This goal is achieved by adding #ifndef / #endif guards in vmlinux.h
> around definitions that originate from the `include/uapi` headers. The
> guards emitted match the guards used in the original headers.
> E.g. as follows:
>
> include/uapi/linux/tcp.h:
>
>   #ifndef _UAPI_LINUX_TCP_H
>   #define _UAPI_LINUX_TCP_H
>   ...
>   union tcp_word_hdr {
>         struct tcphdr hdr;
>         __be32        words[5];
>   };
>   ...
>   #endif /* _UAPI_LINUX_TCP_H */
>
> vmlinux.h:
>
>   ...
>   #ifndef _UAPI_LINUX_TCP_H
>
>   union tcp_word_hdr {
>         struct tcphdr hdr;
>         __be32 words[5];
>   };
>
>   #endif /* _UAPI_LINUX_TCP_H */
>   ...
>
> The problem of identifying data structures from uapi and selecting
> proper guard names is delegated to pahole. When configured pahole
> generates fake `BTF_DECL_TAG` records with header guards information.
> The fake tag is distinguished from a real tag by a prefix
> "header_guard:" in its value. These tags could be present for unions,
> structures, enums and typedefs, e.g.:
>
> [24139] STRUCT 'tcphdr' size=20 vlen=17
>   ...
> [24296] DECL_TAG 'header_guard:_UAPI_LINUX_TCP_H' type_id=24139 ...
>
> This patch adds An option `emit_header_guards` to `struct btf_dump_opts`.
> When this option is present the function `btf_dump__dump_type` emits
> header guards for top-level declarations. The header guards are
> identified by inspecting fake `BTF_DECL_TAG` records described above.

This looks like a completely arbitrary convention that libbpf has no
business knowing or caring about. I think bpftool should be emitting
these guards when generating vmlinux.h. Let's solve this somehow
differently.

>
> Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
> ---
>  tools/lib/bpf/btf.h      |  7 +++-
>  tools/lib/bpf/btf_dump.c | 89 +++++++++++++++++++++++++++++++++++++++-
>  2 files changed, 94 insertions(+), 2 deletions(-)
>

[...]
diff mbox series

Patch

diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
index 8e6880d91c84..dcfb3f168750 100644
--- a/tools/lib/bpf/btf.h
+++ b/tools/lib/bpf/btf.h
@@ -235,8 +235,13 @@  struct btf_dump;
 
 struct btf_dump_opts {
 	size_t sz;
+	/*
+	 * if set to true #ifndef X ... #endif brackets would be printed
+	 * for types marked by BTF_DECL_TAG with "header_guard:" prefix
+	 */
+	bool emit_header_guards;
 };
-#define btf_dump_opts__last_field sz
+#define btf_dump_opts__last_field emit_header_guards
 
 typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args);
 
diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 9bfe2a4ae277..30b995d209c0 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -113,6 +113,8 @@  struct btf_dump {
 	int decl_stack_cap;
 	int decl_stack_cnt;
 
+	bool emit_header_guards;
+
 	/* maps struct/union/enum name to a number of name occurrences */
 	struct hashmap *type_names;
 	/*
@@ -202,6 +204,8 @@  struct btf_dump *btf_dump__new(const struct btf *btf,
 	d->cb_ctx = ctx;
 	d->ptr_sz = btf__pointer_size(btf) ? : sizeof(void *);
 
+	d->emit_header_guards = OPTS_GET(opts, emit_header_guards, false);
+
 	d->type_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
 	if (IS_ERR(d->type_names)) {
 		err = PTR_ERR(d->type_names);
@@ -347,6 +351,8 @@  int btf_dump__dump_type(struct btf_dump *d, __u32 id)
 	return 0;
 }
 
+static const char *btf_dump_is_header_guard_tag(struct btf_dump *d, const struct btf_type *t);
+
 /*
  * Mark all types that are referenced from any other type. This is used to
  * determine top-level anonymous enums that need to be emitted as an
@@ -384,11 +390,15 @@  static int btf_dump_mark_referenced(struct btf_dump *d)
 		case BTF_KIND_TYPEDEF:
 		case BTF_KIND_FUNC:
 		case BTF_KIND_VAR:
-		case BTF_KIND_DECL_TAG:
 		case BTF_KIND_TYPE_TAG:
 			d->type_states[t->type].referenced = 1;
 			break;
 
+		case BTF_KIND_DECL_TAG:
+			if (!btf_dump_is_header_guard_tag(d, t))
+				d->type_states[t->type].referenced = 1;
+			break;
+
 		case BTF_KIND_ARRAY: {
 			const struct btf_array *a = btf_array(t);
 
@@ -449,6 +459,40 @@  static struct decl_tag_array *realloc_decl_tags(struct decl_tag_array *tags, __u
 	return new_tags;
 }
 
+#define HEADER_GUARD_TAG "header_guard:"
+
+static const char *btf_dump_is_header_guard_tag(struct btf_dump *d, const struct btf_type *t)
+{
+	const char *tag_value;
+	int tag_len = strlen(HEADER_GUARD_TAG);
+
+	tag_value = btf__str_by_offset(d->btf, t->name_off);
+	if (strncmp(tag_value, HEADER_GUARD_TAG, tag_len))
+		return NULL;
+
+	return &tag_value[tag_len];
+}
+
+static const char *btf_dump_find_header_guard(struct btf_dump *d, __u32 id)
+{
+	struct decl_tag_array *decl_tags = btf_dump_find_decl_tags(d, id);
+	const struct btf_type *t;
+	const char *guard;
+	int i;
+
+	if (!decl_tags)
+		return NULL;
+
+	for (i = 0; i < decl_tags->cnt; ++i) {
+		t = btf__type_by_id(d->btf, decl_tags->tag_ids[i]);
+		guard = btf_dump_is_header_guard_tag(d, t);
+		if (guard)
+			return guard;
+	}
+
+	return NULL;
+}
+
 /*
  * Scans all BTF objects looking for BTF_KIND_DECL_TAG entries.
  * The id's of the entries are stored in the `btf_dump.decl_tags` table,
@@ -770,6 +814,8 @@  static const char *btf_dump_type_name(struct btf_dump *d, __u32 id);
 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id);
 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
 				 const char *orig_name);
+static void btf_dump_emit_guard_start(struct btf_dump *d, __u32 id);
+static void btf_dump_emit_guard_end(struct btf_dump *d, __u32 id);
 
 static bool btf_dump_is_blacklisted(struct btf_dump *d, __u32 id)
 {
@@ -835,8 +881,10 @@  static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
 					id);
 				return;
 			}
+			btf_dump_emit_guard_start(d, id);
 			btf_dump_emit_struct_fwd(d, id, t);
 			btf_dump_printf(d, ";\n\n");
+			btf_dump_emit_guard_end(d, id);
 			tstate->fwd_emitted = 1;
 			break;
 		case BTF_KIND_TYPEDEF:
@@ -846,8 +894,10 @@  static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
 			 * references through pointer only, not for embedding
 			 */
 			if (!btf_dump_is_blacklisted(d, id)) {
+				btf_dump_emit_guard_start(d, id);
 				btf_dump_emit_typedef_def(d, id, t, 0);
 				btf_dump_printf(d, ";\n\n");
+				btf_dump_emit_guard_end(d, id);
 			}
 			tstate->fwd_emitted = 1;
 			break;
@@ -868,8 +918,10 @@  static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
 	case BTF_KIND_ENUM:
 	case BTF_KIND_ENUM64:
 		if (top_level_def) {
+			btf_dump_emit_guard_start(d, id);
 			btf_dump_emit_enum_def(d, id, t, 0);
 			btf_dump_printf(d, ";\n\n");
+			btf_dump_emit_guard_end(d, id);
 		}
 		tstate->emit_state = EMITTED;
 		break;
@@ -884,8 +936,10 @@  static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
 		btf_dump_emit_type(d, btf_array(t)->type, cont_id);
 		break;
 	case BTF_KIND_FWD:
+		btf_dump_emit_guard_start(d, id);
 		btf_dump_emit_fwd_def(d, id, t);
 		btf_dump_printf(d, ";\n\n");
+		btf_dump_emit_guard_end(d, id);
 		tstate->emit_state = EMITTED;
 		break;
 	case BTF_KIND_TYPEDEF:
@@ -899,8 +953,10 @@  static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
 		 * emit typedef as a forward declaration
 		 */
 		if (!tstate->fwd_emitted && !btf_dump_is_blacklisted(d, id)) {
+			btf_dump_emit_guard_start(d, id);
 			btf_dump_emit_typedef_def(d, id, t, 0);
 			btf_dump_printf(d, ";\n\n");
+			btf_dump_emit_guard_end(d, id);
 		}
 		tstate->emit_state = EMITTED;
 		break;
@@ -923,14 +979,18 @@  static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
 			for (i = 0; i < vlen; i++, m++)
 				btf_dump_emit_type(d, m->type, new_cont_id);
 		} else if (!tstate->fwd_emitted && id != cont_id) {
+			btf_dump_emit_guard_start(d, id);
 			btf_dump_emit_struct_fwd(d, id, t);
 			btf_dump_printf(d, ";\n\n");
+			btf_dump_emit_guard_end(d, id);
 			tstate->fwd_emitted = 1;
 		}
 
 		if (top_level_def) {
+			btf_dump_emit_guard_start(d, id);
 			btf_dump_emit_struct_def(d, id, t, 0);
 			btf_dump_printf(d, ";\n\n");
+			btf_dump_emit_guard_end(d, id);
 			tstate->emit_state = EMITTED;
 		} else {
 			tstate->emit_state = NOT_EMITTED;
@@ -1034,6 +1094,8 @@  static void btf_dump_emit_decl_tags(struct btf_dump *d, __u32 id)
 
 	for (i = 0; i < decl_tags->cnt; ++i) {
 		decl_tag_t = btf_type_by_id(d->btf, decl_tags->tag_ids[i]);
+		if (btf_dump_is_header_guard_tag(d, decl_tag_t))
+			continue;
 		decl_tag_text = btf__name_by_offset(d->btf, decl_tag_t->name_off);
 		btf_dump_printf(d, " __attribute__((btf_decl_tag(\"%s\")))", decl_tag_text);
 	}
@@ -1672,6 +1734,31 @@  static void btf_dump_emit_type_cast(struct btf_dump *d, __u32 id,
 		btf_dump_printf(d, ")");
 }
 
+static void btf_dump_emit_guard_start(struct btf_dump *d, __u32 id)
+{
+	const char *header_guard;
+
+	if (!d->emit_header_guards)
+		return;
+
+	header_guard = btf_dump_find_header_guard(d, id);
+	if (!header_guard)
+		return;
+
+	btf_dump_printf(d, "#ifndef %s\n\n", header_guard);
+}
+
+static void btf_dump_emit_guard_end(struct btf_dump *d, __u32 id)
+{
+	if (!d->emit_header_guards)
+		return;
+
+	if (!btf_dump_find_header_guard(d, id))
+		return;
+
+	btf_dump_printf(d, "#endif\n\n");
+}
+
 /* return number of duplicates (occurrences) of a given name */
 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
 				 const char *orig_name)