From patchwork Tue Aug 22 16:26:02 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: 136588 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp3807819vqi; Tue, 22 Aug 2023 11:19:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHYtWeyNUJR/84X4cqES7/bh8/bDGI+ys6uKr+Y8m1P9E8ptzx4AAUr6Tbi4ENoVHdQaO+V X-Received: by 2002:a17:90b:90b:b0:269:2195:c322 with SMTP id bo11-20020a17090b090b00b002692195c322mr7593192pjb.41.1692728361060; Tue, 22 Aug 2023 11:19:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692728361; cv=none; d=google.com; s=arc-20160816; b=WeP+kaagFITs+sRxy4Ky3CY/4W5wCyErESHa+yPhloD0r2SNLgf02GK/X16fO9keE7 8LljRc5k7UD5qgUTVpX8XaSTouKtHZaX9dFpSL2GGHE3D8sw86Q6h7LBFekyYmZNgx37 O735ZbT0qyAa027FVqIp33mIulLYCuH/CwFPBt2t2lAR7hd/vc1we5H8bJwjSEdIO1hx 3DEJRq8yXEMbqigO11pPWJISoJWyV1YsmnUM/OPf1XuMazWGOJ7FpdbQ2hrtFes4eE0X xRUoOpZaPlPWwrlCnklQj1HwnXBvCgsu8+EjdO6xUqdHgnTPCqTf8LXRHfPNKn1ii0oZ nJvg== 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=gFvAH2/zRcC8XxlydGIqvKVXy4WGkvh/pRWOlxTg24Y=; fh=raxTTJvC+dSJYbgfX5JG5gvrVjxTZXczN0HF8iDayLY=; b=1IDQClIV/pu9oGVwWtPfQDuSzl5guxvM0fx6G+LhZgYdW09fH7Dgf4XvnFvxutP1Wv uNXgDlvtY5DUZs8Id9bgpnrTMqMFBBVaUYV8vZ+NUYRBCqtySx6l6oBmKd18fdQxU3bc CB1k7jDAbJoMurOlxfUjyS/I+Gly2oJLgLhVgzjXlMWw/WXcyF96Yxqcbldmdg589++S BfA03s1qEX4dbVXhbpuNmssKd1p15REcEk2fgkmEIPcLVIeHuILTmPQCun/BDAMrz6jz 6S8knXnr0ejW5IWIR2XG8NP4PzIIl458dXVF2gohzSCjL6kBoBkGOkzq2UtlDKNZlc8X f6PQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Cnr1S/Sy"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id z16-20020a656650000000b005694a49d062si5534404pgv.314.2023.08.22.11.19.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 11:19:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Cnr1S/Sy"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E0F5F36488; Tue, 22 Aug 2023 10:22:50 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237709AbjHVQ0M (ORCPT + 99 others); Tue, 22 Aug 2023 12:26:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237665AbjHVQ0M (ORCPT ); Tue, 22 Aug 2023 12:26:12 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2798FCEF; Tue, 22 Aug 2023 09:26:08 -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 B44B265A54; Tue, 22 Aug 2023 16:26:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 37C10C433C8; Tue, 22 Aug 2023 16:26:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692721567; bh=M0yvt9XhZlKteG4rq6911+5kqkZiiyrV8Vdh/k3iy5w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cnr1S/Sy+0F8FIgeXlSiDjs0zBxioLjRU8WEycgszF7j8sH3oAV19R6G/LCSqP5MX bq0bNyiiZInyYvTzj5cbMQ358j/IzLo6XaImnUW71BV0tyknHqFTG8NTczk9gwr7hH QZKVkYVzg5dHAySTy6lpfkkb1gtqYhEA2eo8UoggUwy8+4LuQ8x647hWn+EyMG9iIp AWql+EFY8AmMU2NM520W3qAfpzxcnnu+jM8h5mGqkS0DuGNpXjNoOIDfJbSd2iawVB ee8y5q1hQQKAJlnLtP6wKxsjVzsUnhnesHIO8WwFm+etWOrfhyMGrY+9VmsFiUajZM ShenasIwP0G7g== 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 v6 3/9] tracing/probes: Add a function to search a member of a struct/union Date: Wed, 23 Aug 2023 01:26:02 +0900 Message-Id: <169272156248.160970.8868479822371129043.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <169272153143.160970.15584603734373446082.stgit@devnote2> References: <169272153143.160970.15584603734373446082.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS 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: 1774954334246606944 X-GMAIL-MSGID: 1774954334246606944 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 Acked-by: Steven Rostedt (Google) --- 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. Changes in v5: - Move the generic functions into a new file, trace_btf.c, for avoiding tree-merge conflict. - Return anon_offset for calculating correct offset from root struct. Changes in v6: - Allocate anon_stack from heap instead of stack. --- kernel/trace/trace_btf.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++ kernel/trace/trace_btf.h | 4 +++ 2 files changed, 73 insertions(+) diff --git a/kernel/trace/trace_btf.c b/kernel/trace/trace_btf.c index d70b8ee9af37..ca224d53bfdc 100644 --- a/kernel/trace/trace_btf.c +++ b/kernel/trace/trace_btf.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include #include +#include #include "trace_btf.h" @@ -51,3 +52,71 @@ const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s3 return NULL; } +#define BTF_ANON_STACK_MAX 16 + +struct btf_anon_stack { + u32 tid; + u32 offset; +}; + +/* + * Find a member of data structure/union by name and return it. + * Return NULL if not found, or -EINVAL if parameter is invalid. + * If the member is an member of anonymous union/structure, the offset + * of that anonymous union/structure is stored into @anon_offset. Caller + * can calculate the correct offset from the root data structure by + * adding anon_offset to the member's offset. + */ +const struct btf_member *btf_find_struct_member(struct btf *btf, + const struct btf_type *type, + const char *member_name, + u32 *anon_offset) +{ + struct btf_anon_stack *anon_stack; + const struct btf_member *member; + u32 tid, cur_offset = 0; + const char *name; + int i, top = 0; + + anon_stack = kcalloc(BTF_ANON_STACK_MAX, sizeof(*anon_stack), GFP_KERNEL); + if (!anon_stack) + return ERR_PTR(-ENOMEM); + +retry: + if (!btf_type_is_struct(type)) { + member = ERR_PTR(-EINVAL); + goto out; + } + + 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, &tid); + if (type && top < BTF_ANON_STACK_MAX) { + anon_stack[top].tid = tid; + anon_stack[top++].offset = + cur_offset + member->offset; + } + } else { + name = btf_name_by_offset(btf, member->name_off); + if (name && !strcmp(member_name, name)) { + if (anon_offset) + *anon_offset = cur_offset; + goto out; + } + } + } + if (top > 0) { + /* Pop from the anonymous stack and retry */ + tid = anon_stack[--top].tid; + cur_offset = anon_stack[top].offset; + type = btf_type_by_id(btf, tid); + goto retry; + } + member = NULL; + +out: + kfree(anon_stack); + return member; +} + diff --git a/kernel/trace/trace_btf.h b/kernel/trace/trace_btf.h index 98685e9a556c..4bc44bc261e6 100644 --- a/kernel/trace/trace_btf.h +++ b/kernel/trace/trace_btf.h @@ -5,3 +5,7 @@ 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, + u32 *anon_offset);