From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5A297EB64DD for ; Fri, 21 Jul 2023 14:23:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231857AbjGUOWq (ORCPT ); Fri, 21 Jul 2023 10:22:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231839AbjGUOWl (ORCPT ); Fri, 21 Jul 2023 10:22:41 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD84530DA; Fri, 21 Jul 2023 07:22:38 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 69B4661CC7; Fri, 21 Jul 2023 14:22:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2A42C433C9; Fri, 21 Jul 2023 14:22:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689949357; bh=cUi296eF2C9WP9mdZeH5Yfe9jXUUyl8wU5SfLNXEuow=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=jslEWqJpZB+dAEEOMcBXFkjAFSWnknZmIMUx78ERpYKIrThT+ORkwLZm1wsAB/B4/ jTyJe+MGfJM6xBmJ/l3omp5knCBjGhJn/8LI9D49Vr+N6vWptgLNRF90vRMaeXcwuJ JwQ5SnZiLw7CubWdtdn0PKd5DS6NTYlQpQ+Evfbjr8MS3lCVWHYMAuYK+f7zrTJ5sl Q8gw6fYaK/MMWM/RWCymcPG+sKhGbvHy+MJXrB9/ywNYVlLQCihnjvl5hAtlgx4Qqu bnqBg0PRXd/xh3c72irxbSCI2qHsQdTH0Xq6a7TUn9C7HlQI5S2JovQ//kJYiTUfYp y/Jo6VJWEqljQ== Date: Fri, 21 Jul 2023 23:22:33 +0900 From: Masami Hiramatsu (Google) To: Alan Maguire Cc: linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, Steven Rostedt , Martin KaFai Lau , bpf@vger.kernel.org, Sven Schnelle , Alexei Starovoitov Subject: Re: [PATCH v2 3/9] bpf/btf: Add a function to search a member of a struct/union Message-Id: <20230721232233.d1e9456d23bbbab88f05f480@kernel.org> In-Reply-To: References: <168960739768.34107.15145201749042174448.stgit@devnote2> <168960742712.34107.9849785489776347376.stgit@devnote2> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 20 Jul 2023 23:34:42 +0100 Alan Maguire wrote: > On 17/07/2023 16:23, Masami Hiramatsu (Google) wrote: > > From: Masami Hiramatsu (Google) > > > > 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) > > A few small things below, but > > Reviewed-by: Alan Maguire 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) > > > > -- Masami Hiramatsu (Google)