From patchwork Mon Jul 17 15:23:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 121421 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1187700vqt; Mon, 17 Jul 2023 08:39:04 -0700 (PDT) X-Google-Smtp-Source: APBJJlGQs6d2I4rth1RWeLErfnOuv9xT1S4bNNnlBVgLdXs2pv+0dm9MnYyqq5jJZaZPPhdp6wcn X-Received: by 2002:a05:6e02:1587:b0:348:8107:a25e with SMTP id m7-20020a056e02158700b003488107a25emr128964ilu.25.1689608343957; Mon, 17 Jul 2023 08:39:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689608343; cv=none; d=google.com; s=arc-20160816; b=BRLnMLMZB+3SjGlZij7S0fmsvzq8aY137kytgnwDXUqVLazAM66drtSIEoXS50tDZh ULH6ZtxF2anRAVMwGs4ZUxF04iPzXm1ajnY2831NZ3RFSoP7WZXdZQ7hQR7f9Ui0C1KB 8tb3ugUFmgkqbLMLmxsIg5u+qbaxXvmDJ5IuyxNjci+KGfcVJZaoznalL7mZY99sMnWb fgOg43lF2efRU5bIgbZ96uxkjPaUib/LZc1EEdyxG1skLhf9o0C8JKHERqlFgDSgUrhU SJ7/gzZyYVqzP3qzEyDZGL11oKSwqonnZ/Dhjaz526ptdEUyyp4+p+6hYI03SZz/NevC 7YSQ== 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=D5ZtDosOqPylMSaePnuWhlRIFxwjHBnlBYi4P71ufX0=; fh=raxTTJvC+dSJYbgfX5JG5gvrVjxTZXczN0HF8iDayLY=; b=Wut9QS2EAcB6+H3Hd7HWA4lI0FZV6EiX1E/MG5rWtMZufkju016+CAitWZmPrcb8a6 tNGHq6xoTmTuxLIilq6M0HL/fiGpq3VNYWPXyQZhk92pOdEoLYw13JdC+6cSlT8WLLft FzLBp3eHdwq3tFU6Myn2BJ5r9UUKtR7Uqq0UjVW4nWH3ZiAt9K6uLbzk2AGj30Ms5QJZ Nl+ZxvhH2fRnc7i26qm1nxIjGxftJFRcHvYfyHUw91g5bV/vkJLNvl0SZ+5hDm0kl5E8 uuhxDLwHRkQUo++W5vLlXd6lfSi1Xe0w+zkdaFPr0zwTSldrcWzDEIC/5BJUOg8sZBhV AK2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tZxXXYzh; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o22-20020a634e56000000b0054fa5f25cfasi11227275pgl.568.2023.07.17.08.38.49; Mon, 17 Jul 2023 08:39:03 -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=tZxXXYzh; 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 S231403AbjGQPY3 (ORCPT + 99 others); Mon, 17 Jul 2023 11:24:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230285AbjGQPYO (ORCPT ); Mon, 17 Jul 2023 11:24:14 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CB26F5; Mon, 17 Jul 2023 08:23:58 -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 5BD9C6104A; Mon, 17 Jul 2023 15:23:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 01632C433C7; Mon, 17 Jul 2023 15:23:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689607431; bh=DHVTx0hal1LvLnlrU3Axy5dJ3lhJGA8GVR5jpKF4jhU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tZxXXYzhpZFBO4M4QHLtv7C4cVPWjYG+Kxb00IplTDMV86B0B6fGIxBzZRmFSo6D1 NMxlH4ildfklCTLfMXMMXlphdUGZtGnd7tNhC8Fz4TNpxHvUabQl8epBQTkfYOb/0z Hd0QDoaUQkV9YYBj3u0+pqgY33fQcPevyU6Ru+K5neL41+cDZDRRi8LDpIcBI4AW7D YxAicfJQoPumhr3nQBUKYYzP0YTX/Qe7y5lAxpAlbR6ModDj6QiVRiwBuSAIYUChA3 k+tFZP1TvFrlYMSfUAp0VAyGdPU8geUq9qFTejBDEQ2WeFaJO6JO7dXUGAC96syKBf qoAES80/9i45A== 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 v2 3/9] bpf/btf: Add a function to search a member of a struct/union Date: Tue, 18 Jul 2023 00:23:47 +0900 Message-Id: <168960742712.34107.9849785489776347376.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <168960739768.34107.15145201749042174448.stgit@devnote2> References: <168960739768.34107.15145201749042174448.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771682758908936604 X-GMAIL-MSGID: 1771682758908936604 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 --- 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++) { + 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)