Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp4096927rwb; Mon, 31 Jul 2023 00:52:59 -0700 (PDT) X-Google-Smtp-Source: APBJJlHlveRY8A5FP4qvS6eJ6JQgVAfe7SxYdvc+kmY1ATH8N6dJQ79HEV3CveTmbXEGjKdjJlxM X-Received: by 2002:a19:8c09:0:b0:4f7:6775:2a66 with SMTP id o9-20020a198c09000000b004f767752a66mr4943275lfd.53.1690789979125; Mon, 31 Jul 2023 00:52:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690789979; cv=none; d=google.com; s=arc-20160816; b=jNb0ohGhGeU/wYwRi9QjI+hJ6gQeTdrGSJ0d/bVqQZHxFGBOY4EjW4oQLj8XJd+yf8 q/x/W5TCNIqZcK0FfS6ks8fBKbDPM+AK5nyHHEWsDpDbMj7IfN3iEmrfQgEa/onGaLAP 3kmzVfh4MjJD+FVHkpRwr1K9gXvHDKKTvUJlfLMZtAZ+bFLRipG/Mgw6v4BZ31RD9JXO frRu89dS0MQGNz/Z5DUPtwmHrWfcGBmlpSZWMCRkugxmr2DP22AQLanBCDWY33WruYkZ PgVsFVNcJjZ5+xFQN2+kBLVxG9RkZnFE7KkIyCm4YdBkjzfZfmatvOR5HXax0Cp3NEKN sU/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=uiHJms0ructKwbImvpiC7UGQOSd/ODg388rwUG7IEfg=; fh=raxTTJvC+dSJYbgfX5JG5gvrVjxTZXczN0HF8iDayLY=; b=eAD02rAPp/wTQbJ47MJaD/pXhLh6qZSBciY2keUIjQ8NycHyrnFaXi6bf8clqyfAjg C01dhvZYyRlmaG+DquTGQdVZ++jDF95aTxcSyuP9r/aiQqU9rXzyou2uTHzIv3sn2mAS cjShlWS57lZxpruGfdoQBW8h8nxvI8PoiYnkkLryNKloc0bUzMRlcGP5ZQqKq/x2Ve/S drHZd6VTdz4GNiemLdeVI3K1vkIEAjQNS5pce9oR+92y6PZRJNo1ZcoDn3Z2FJjwzqUY NkV6xkqnTvTji9QAK9MZ7yOEsw93xhk5cqXEkpm5tDW91ZA4n9rp05YKBQjr9u23LNO8 5wrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hpZUtMBG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l6-20020a1709067d4600b00988a5cdbfa7si6926791ejp.889.2023.07.31.00.52.33; Mon, 31 Jul 2023 00:52:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hpZUtMBG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231622AbjGaHcH (ORCPT + 99 others); Mon, 31 Jul 2023 03:32:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231481AbjGaHbf (ORCPT ); Mon, 31 Jul 2023 03:31:35 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2669610C; Mon, 31 Jul 2023 00:30:40 -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 B872760F07; Mon, 31 Jul 2023 07:30:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2BE2DC433C7; Mon, 31 Jul 2023 07:30:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690788639; bh=ZxqLQEDqyrTI+b7ozLCG2peyd8pHZudlPd6y62zwtWQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hpZUtMBGR106BHm0aj2xR7/CzYRk5wdguE7xIfK2kjju17dMC1Ve8PY4G1GNYkn8P 2LV0DgD+0uZ2JB2OfOKjP2TNXSwEacmeNU3iq2kQ+BM124AGnkMCwl8LK9dVJYNTbC eFZeK+jAz/1F9dBubKz5HaIaUNZyIsrk24FSMnmUWFXms6R1GFjXeVNp/oo5KiJ3di U3q5sCGsPnRGezK13vAsoxDPYLDL5D05+ljYET+h3qXjCOkCteT/bH7ZnIA/brGmOm pqUNLdUbGklzV2bIIs7RMX7T6iRZx6uu6BEa2Kx/6E0Ss/2t35rnt8vNq0+atuBnuJ dnNS988aIFc4A== From: "Masami Hiramatsu (Google)" To: linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Steven Rostedt , mhiramat@kernel.org, Martin KaFai Lau , bpf@vger.kernel.org, Sven Schnelle , Alexei Starovoitov Subject: [PATCH v4 3/9] bpf/btf: Add a function to search a member of a struct/union Date: Mon, 31 Jul 2023 16:30:34 +0900 Message-Id: <169078863449.173706.2322042687021909241.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <169078860386.173706.3091034523220945605.stgit@devnote2> References: <169078860386.173706.3091034523220945605.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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) Reviewed-by: Alan Maguire --- Changes in v3: - Remove simple input check. - Fix unneeded IS_ERR_OR_NULL() check for btf_type_by_id(). - Move the code next to btf_get_func_param(). - Use for_each_member() macro instead of for-loop. - Use btf_type_skip_modifiers() instead of btf_type_by_id(). Changes in v4: - Use a stack for searching in anonymous members instead of nested call. --- include/linux/btf.h | 3 +++ kernel/bpf/btf.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/linux/btf.h b/include/linux/btf.h index 20e3a07eef8f..4b10d57ceee0 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -226,6 +226,9 @@ const struct btf_type *btf_find_func_proto(const char *func_name, struct btf **btf_p); 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 f7b25c615269..8d81a4ffa67b 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -958,6 +958,46 @@ const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s3 return NULL; } +#define BTF_ANON_STACK_MAX 16 + +/* + * 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_type *anon_stack[BTF_ANON_STACK_MAX]; + const struct btf_member *member; + const char *name; + int i, top = 0; + +retry: + if (!btf_type_is_struct(type)) + return ERR_PTR(-EINVAL); + + for_each_member(i, type, member) { + if (!member->name_off) { + /* Anonymous union/struct: push it for later use */ + type = btf_type_skip_modifiers(btf, member->type, NULL); + if (type && top < BTF_ANON_STACK_MAX) + anon_stack[top++] = type; + } else { + name = btf_name_by_offset(btf, member->name_off); + if (name && !strcmp(member_name, name)) + return member; + } + } + if (top > 0) { + /* Pop from the anonymous stack and retry */ + type = anon_stack[--top]; + goto retry; + } + + return NULL; +} + #define BTF_SHOW_MAX_ITER 10 #define BTF_KIND_BIT(kind) (1ULL << kind)