Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp998340rwb; Wed, 26 Jul 2023 06:14:57 -0700 (PDT) X-Google-Smtp-Source: APBJJlHzcNyzgRoMFE7w4SA3NRqCl0ILDeP3dM+otDxx7LLzqT7es5/zfjhRq7khHb2sKkYi0bmr X-Received: by 2002:a17:906:30c7:b0:992:3ac7:a2da with SMTP id b7-20020a17090630c700b009923ac7a2damr1674702ejb.30.1690377297551; Wed, 26 Jul 2023 06:14:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690377297; cv=none; d=google.com; s=arc-20160816; b=ooXQd6BOLffJ9t2x/Xb8OIUVVLM2pcu92t53XsbaNymMr6cSd8zkTdo/bSBmoib869 YC+zf6wEP7JwZreYy71nSRs6YGWUSGsVQ9HNro4jNLPuLYuLHkEK5oHrwfTY2CWx8kp6 BqhfD/eHiITNUpxiRXujvOu/ARvT/NmqYDw32x8wdwzzykREEN+Rn1Xvq3vLl5gfokTo xIkcA3NgoiFrTAH0Bh3xcMCfOYB9K563vfA2x5Moac8MZThPJTfSn4PzZblD2y2M8Ngr 3O/gaqfpL7Jdz7tW11vDPJeg7qu6rpjHs0NRJdbmfmCqad++qpByICCgO6mG4tlrOn0u jFmw== 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=AqItGZxNm5vBReUt1IrarI9tDkPp/4TxJjzBqByVk0c=; fh=raxTTJvC+dSJYbgfX5JG5gvrVjxTZXczN0HF8iDayLY=; b=YcOb/XMYSmmg4nVkw6yAeOgB5pcsDfKz5f+wTSUMiDW1cYm6Wk2ztS86QcRDcx9BDC UZFD2cqQOhbhYvf0QUbdH2WWzJMBIA6QTYE9D/Fu0RtUZ3QYYeI/aQxCZjZ7DVMm6oEV FUb8Dkh6yxJENuYsVdAUmLmQm3wwDNzRp8XNP9R5D+V92zqcTtWKEAqvZno3KnPk2QdM zGer+Es87E7KgdZ4aaPBCv92re/OcK1N8VWuNR6sNCfxlU8b1KkbmjOpnD5XoaoFeDd4 LT6ePMS51t+eKp092mWK0119aJFjwFp8hVoogcs7001pp5bTQj+TpT9AcuT3LVeYW2jB +fjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WUPMFPxe; 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 q27-20020a1709060e5b00b009883ee7519fsi8985639eji.884.2023.07.26.06.14.31; Wed, 26 Jul 2023 06:14:57 -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=WUPMFPxe; 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 S233014AbjGZNA0 (ORCPT + 99 others); Wed, 26 Jul 2023 09:00:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233236AbjGZNAY (ORCPT ); Wed, 26 Jul 2023 09:00:24 -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 1BBDF2129; Wed, 26 Jul 2023 06:00:19 -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 A37CC613CA; Wed, 26 Jul 2023 13:00:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0EA64C433C8; Wed, 26 Jul 2023 13:00:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690376418; bh=SVaKMsm5wi9MtePH615CD5h2bQfFDb6xVagKrZjtk0U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WUPMFPxeQ243AvvEKB6yR048E2BwbVC2zYeNyQp0MhufnS8soHGi14Yh8wM1dans9 jFh5g5cBlzc5BsVYAq08KU6yMOcJ4mHyg5pxSKWu1M6jXXdGtUdthBvg8H2/nPb7qM J3bLVYkBURe7UL1Elew2+HhnMbguVKY7tgY+X/VkD6Xof0J7pHaFETcpXnmxLY8QeE Ty/Gp7oPQhqfxjteluKERbDnq/oHIlhTrPV0r+/GvGXau42/GAe70Fyj+TVehSMtO3 xIJt66WwEYbJD2WKlfbXVhfN4fi2eQ0ticmjhmWLH3aC94X5vEhWkbgdZBmq8wzkdv IvfS1yQeTAQhQ== 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 v3 2/9] bpf/btf: tracing: Move finding func-proto API and getting func-param API to BTF Date: Wed, 26 Jul 2023 22:00:13 +0900 Message-Id: <169037641366.607919.6661445077432894528.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <169037639315.607919.2613476171148037242.stgit@devnote2> References: <169037639315.607919.2613476171148037242.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=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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) Move generic function-proto find API and getting function parameter API to BTF library code from trace_probe.c. This will avoid redundant efforts on different feature. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v3: - Remove perameter check. - Fix a typo. - Add a type check for btf_get_func_param() and add comment for that. - Use bpf_find_btf_id() and add bpf_put(). - Move the code before btf_show() related code. --- include/linux/btf.h | 4 ++++ kernel/bpf/btf.c | 47 +++++++++++++++++++++++++++++++++++++++++ kernel/trace/trace_probe.c | 50 +++++++++----------------------------------- 3 files changed, 61 insertions(+), 40 deletions(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index dbfe41a09c4b..20e3a07eef8f 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -222,6 +222,10 @@ const struct btf_type * btf_resolve_size(const struct btf *btf, const struct btf_type *type, u32 *type_size); const char *btf_type_str(const struct btf_type *t); +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); #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 b9b0eb1189bb..f7b25c615269 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -911,6 +911,53 @@ static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf, return t; } +/* + * Find a function proto type by name, and return the btf_type with its btf + * in *@btf_p. Return NULL if not found. + * Note that caller has to call btf_put(*@btf_p) after using the btf_type. + */ +const struct btf_type *btf_find_func_proto(const char *func_name, struct btf **btf_p) +{ + const struct btf_type *t; + s32 id; + + id = bpf_find_btf_id(func_name, BTF_KIND_FUNC, btf_p); + if (id < 0) + return NULL; + + /* Get BTF_KIND_FUNC type */ + t = btf_type_by_id(*btf_p, id); + if (!t || !btf_type_is_func(t)) + goto err; + + /* The type of BTF_KIND_FUNC is BTF_KIND_FUNC_PROTO */ + t = btf_type_by_id(*btf_p, t->type); + if (!t || !btf_type_is_func_proto(t)) + goto err; + + return t; +err: + btf_put(*btf_p); + return NULL; +} + +/* + * Get function parameter with the number of parameters. + * This can return NULL if the function has no parameters. + * It can return -EINVAL if the @func_proto is not a function proto type. + */ +const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr) +{ + if (!btf_type_is_func_proto(func_proto)) + return ERR_PTR(-EINVAL); + + *nr = btf_type_vlen(func_proto); + if (*nr > 0) + return (const struct btf_param *)(func_proto + 1); + else + return NULL; +} + #define BTF_SHOW_MAX_ITER 10 #define BTF_KIND_BIT(kind) (1ULL << kind) diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c index ecbe28f8d676..21a228d88ebb 100644 --- a/kernel/trace/trace_probe.c +++ b/kernel/trace/trace_probe.c @@ -361,38 +361,6 @@ static const char *type_from_btf_id(struct btf *btf, s32 id) return NULL; } -static const struct btf_type *find_btf_func_proto(const char *funcname, - struct btf **btf_p) -{ - const struct btf_type *t; - struct btf *btf = NULL; - s32 id; - - if (!funcname) - return ERR_PTR(-EINVAL); - - id = bpf_find_btf_id(funcname, BTF_KIND_FUNC, &btf); - if (id <= 0) - return ERR_PTR(-ENOENT); - - /* Get BTF_KIND_FUNC type */ - t = btf_type_by_id(btf, id); - if (!t || !btf_type_is_func(t)) - goto err; - - /* The type of BTF_KIND_FUNC is BTF_KIND_FUNC_PROTO */ - t = btf_type_by_id(btf, t->type); - if (!t || !btf_type_is_func_proto(t)) - goto err; - - *btf_p = btf; - return t; - -err: - btf_put(btf); - return ERR_PTR(-ENOENT); -} - static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, struct btf **btf_p, bool tracepoint) { @@ -403,12 +371,13 @@ static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr if (!funcname || !nr) return ERR_PTR(-EINVAL); - t = find_btf_func_proto(funcname, &btf); - if (IS_ERR(t)) + t = btf_find_func_proto(funcname, &btf); + if (!t) return (const struct btf_param *)t; - *nr = btf_type_vlen(t); - param = (const struct btf_param *)(t + 1); + param = btf_get_func_param(t, nr); + if (IS_ERR_OR_NULL(param)) + goto err; /* Hide the first 'data' argument of tracepoint */ if (tracepoint) { @@ -421,6 +390,7 @@ static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr return param; } +err: btf_put(btf); return NULL; } @@ -496,8 +466,8 @@ static const struct fetch_type *parse_btf_retval_type( if (ctx->funcname) { /* Do not use ctx->btf, because it must be used with ctx->param */ - t = find_btf_func_proto(ctx->funcname, &btf); - if (!IS_ERR(t)) { + t = btf_find_func_proto(ctx->funcname, &btf); + if (t) { typestr = type_from_btf_id(btf, t->type); btf_put(btf); } @@ -512,8 +482,8 @@ static bool is_btf_retval_void(const char *funcname) struct btf *btf; bool ret; - t = find_btf_func_proto(funcname, &btf); - if (IS_ERR(t)) + t = btf_find_func_proto(funcname, &btf); + if (!t) return false; ret = (t->type == 0);