diff mbox series

[v2,3/9] bpf/btf: Add a function to search a member of a struct/union

Message ID 168960742712.34107.9849785489776347376.stgit@devnote2 (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series tracing: Improbe BTF support on probe events | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 2790 this patch: 2790
netdev/cc_maintainers warning 9 maintainers not CCed: daniel@iogearbox.net yhs@fb.com kpsingh@kernel.org john.fastabend@gmail.com song@kernel.org sdf@google.com andrii@kernel.org jolsa@kernel.org haoluo@google.com
netdev/build_clang success Errors and warnings before: 1533 this patch: 1533
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
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: 2808 this patch: 2808
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 53 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on s390x with gcc

Commit Message

Masami Hiramatsu (Google) July 17, 2023, 3:23 p.m. UTC
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Add btf_find_struct_member() API to search a member of a given data structure
or union from the member's name.

Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 include/linux/btf.h |    3 +++
 kernel/bpf/btf.c    |   38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

Comments

Alan Maguire July 20, 2023, 10:34 p.m. UTC | #1
On 17/07/2023 16:23, Masami Hiramatsu (Google) wrote:
> From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> 
> Add btf_find_struct_member() API to search a member of a given data structure
> or union from the member's name.
> 
> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

A few small things below, but

Reviewed-by: Alan Maguire <alan.maguire@oracle.com>

> ---
>  include/linux/btf.h |    3 +++
>  kernel/bpf/btf.c    |   38 ++++++++++++++++++++++++++++++++++++++
>  2 files changed, 41 insertions(+)
> 
> diff --git a/include/linux/btf.h b/include/linux/btf.h
> index 98fbbcdd72ec..097fe9b51562 100644
> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
> @@ -225,6 +225,9 @@ const struct btf_type *btf_find_func_proto(struct btf *btf,
>  					   const char *func_name);
>  const struct btf_param *btf_get_func_param(const struct btf_type *func_proto,
>  					   s32 *nr);
> +const struct btf_member *btf_find_struct_member(struct btf *btf,
> +						const struct btf_type *type,
> +						const char *member_name);
>  
>  #define for_each_member(i, struct_type, member)			\
>  	for (i = 0, member = btf_type_member(struct_type);	\
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index e015b52956cb..452ffb0393d6 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -1992,6 +1992,44 @@ const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s3
>  		return NULL;
>  }
>  
> +/*
> + * Find a member of data structure/union by name and return it.
> + * Return NULL if not found, or -EINVAL if parameter is invalid.
> + */
> +const struct btf_member *btf_find_struct_member(struct btf *btf,
> +						const struct btf_type *type,
> +						const char *member_name)
> +{
> +	const struct btf_member *members, *ret;
> +	const char *name;
> +	int i, vlen;
> +
> +	if (!btf || !member_name || !btf_type_is_struct(type))
> +		return ERR_PTR(-EINVAL);
> +
> +	vlen = btf_type_vlen(type);
> +	members = (const struct btf_member *)(type + 1);
> +
> +	for (i = 0; i < vlen; i++) {

could use for_each_member() here I think, or perhaps use
btf_type_member(type) when getting member pointer above.

> +		if (!members[i].name_off) {
> +			/* unnamed union: dig deeper */
> +			type = btf_type_by_id(btf, members[i].type);
> +			if (!IS_ERR_OR_NULL(type)) {
> +				ret = btf_find_struct_member(btf, type,
> +							     member_name);

You'll need to skip modifiers before calling btf_find_struct_member()
here I think; it's possible to have a const anonymous union for example,
so to get to the union you'd need to skip the modifiers first. Otherwise
you could fail the btf_type_is_struct() test on re-entry.


> +				if (!IS_ERR_OR_NULL(ret))
> +					return ret;
> +			}
> +		} else {
> +			name = btf_name_by_offset(btf, members[i].name_off);
> +			if (name && !strcmp(member_name, name))
> +				return &members[i];
> +		}
> +	}
> +
> +	return NULL;
> +}
> +
>  static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id)
>  {
>  	while (type_id < btf->start_id)
> 
>
Masami Hiramatsu (Google) July 21, 2023, 2:22 p.m. UTC | #2
On Thu, 20 Jul 2023 23:34:42 +0100
Alan Maguire <alan.maguire@oracle.com> wrote:

> On 17/07/2023 16:23, Masami Hiramatsu (Google) wrote:
> > From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> > 
> > Add btf_find_struct_member() API to search a member of a given data structure
> > or union from the member's name.
> > 
> > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> 
> A few small things below, but
> 
> Reviewed-by: Alan Maguire <alan.maguire@oracle.com>

Thanks

> 
> > ---
> >  include/linux/btf.h |    3 +++
> >  kernel/bpf/btf.c    |   38 ++++++++++++++++++++++++++++++++++++++
> >  2 files changed, 41 insertions(+)
> > 
> > diff --git a/include/linux/btf.h b/include/linux/btf.h
> > index 98fbbcdd72ec..097fe9b51562 100644
> > --- a/include/linux/btf.h
> > +++ b/include/linux/btf.h
> > @@ -225,6 +225,9 @@ const struct btf_type *btf_find_func_proto(struct btf *btf,
> >  					   const char *func_name);
> >  const struct btf_param *btf_get_func_param(const struct btf_type *func_proto,
> >  					   s32 *nr);
> > +const struct btf_member *btf_find_struct_member(struct btf *btf,
> > +						const struct btf_type *type,
> > +						const char *member_name);
> >  
> >  #define for_each_member(i, struct_type, member)			\
> >  	for (i = 0, member = btf_type_member(struct_type);	\
> > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> > index e015b52956cb..452ffb0393d6 100644
> > --- a/kernel/bpf/btf.c
> > +++ b/kernel/bpf/btf.c
> > @@ -1992,6 +1992,44 @@ const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s3
> >  		return NULL;
> >  }
> >  
> > +/*
> > + * Find a member of data structure/union by name and return it.
> > + * Return NULL if not found, or -EINVAL if parameter is invalid.
> > + */
> > +const struct btf_member *btf_find_struct_member(struct btf *btf,
> > +						const struct btf_type *type,
> > +						const char *member_name)
> > +{
> > +	const struct btf_member *members, *ret;
> > +	const char *name;
> > +	int i, vlen;
> > +
> > +	if (!btf || !member_name || !btf_type_is_struct(type))
> > +		return ERR_PTR(-EINVAL);
> > +
> > +	vlen = btf_type_vlen(type);
> > +	members = (const struct btf_member *)(type + 1);
> > +
> > +	for (i = 0; i < vlen; i++) {
> 
> could use for_each_member() here I think, or perhaps use
> btf_type_member(type) when getting member pointer above.

Thanks! I missed that macro.

> 
> > +		if (!members[i].name_off) {
> > +			/* unnamed union: dig deeper */
> > +			type = btf_type_by_id(btf, members[i].type);
> > +			if (!IS_ERR_OR_NULL(type)) {
> > +				ret = btf_find_struct_member(btf, type,
> > +							     member_name);
> 
> You'll need to skip modifiers before calling btf_find_struct_member()
> here I think; it's possible to have a const anonymous union for example,

Yeah, it is possible. Let me add it.

> so to get to the union you'd need to skip the modifiers first. Otherwise
> you could fail the btf_type_is_struct() test on re-entry.

Indeed. 

Thank you!

> 
> 
> > +				if (!IS_ERR_OR_NULL(ret))
> > +					return ret;
> > +			}
> > +		} else {
> > +			name = btf_name_by_offset(btf, members[i].name_off);
> > +			if (name && !strcmp(member_name, name))
> > +				return &members[i];
> > +		}
> > +	}
> > +
> > +	return NULL;
> > +}
> > +
> >  static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id)
> >  {
> >  	while (type_id < btf->start_id)
> > 
> >
diff mbox series

Patch

diff --git a/include/linux/btf.h b/include/linux/btf.h
index 98fbbcdd72ec..097fe9b51562 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -225,6 +225,9 @@  const struct btf_type *btf_find_func_proto(struct btf *btf,
 					   const char *func_name);
 const struct btf_param *btf_get_func_param(const struct btf_type *func_proto,
 					   s32 *nr);
+const struct btf_member *btf_find_struct_member(struct btf *btf,
+						const struct btf_type *type,
+						const char *member_name);
 
 #define for_each_member(i, struct_type, member)			\
 	for (i = 0, member = btf_type_member(struct_type);	\
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index e015b52956cb..452ffb0393d6 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -1992,6 +1992,44 @@  const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s3
 		return NULL;
 }
 
+/*
+ * Find a member of data structure/union by name and return it.
+ * Return NULL if not found, or -EINVAL if parameter is invalid.
+ */
+const struct btf_member *btf_find_struct_member(struct btf *btf,
+						const struct btf_type *type,
+						const char *member_name)
+{
+	const struct btf_member *members, *ret;
+	const char *name;
+	int i, vlen;
+
+	if (!btf || !member_name || !btf_type_is_struct(type))
+		return ERR_PTR(-EINVAL);
+
+	vlen = btf_type_vlen(type);
+	members = (const struct btf_member *)(type + 1);
+
+	for (i = 0; i < vlen; i++) {
+		if (!members[i].name_off) {
+			/* unnamed union: dig deeper */
+			type = btf_type_by_id(btf, members[i].type);
+			if (!IS_ERR_OR_NULL(type)) {
+				ret = btf_find_struct_member(btf, type,
+							     member_name);
+				if (!IS_ERR_OR_NULL(ret))
+					return ret;
+			}
+		} else {
+			name = btf_name_by_offset(btf, members[i].name_off);
+			if (name && !strcmp(member_name, name))
+				return &members[i];
+		}
+	}
+
+	return NULL;
+}
+
 static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id)
 {
 	while (type_id < btf->start_id)