[v2,2/9] bpf/btf: tracing: Move finding func-proto API and getting func-param API to BTF
Message ID | 168960741686.34107.6330273416064011062.stgit@devnote2 |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1187835vqt; Mon, 17 Jul 2023 08:39:18 -0700 (PDT) X-Google-Smtp-Source: APBJJlGQHH0yHcSJMpe2L0Ji33YmRevgNbgXku1kI7RiLAoGiqtO2Pbmoc6C8xxPFSy4Anx1gypj X-Received: by 2002:a05:6402:150b:b0:521:728f:d84e with SMTP id f11-20020a056402150b00b00521728fd84emr8117363edw.0.1689608357858; Mon, 17 Jul 2023 08:39:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689608357; cv=none; d=google.com; s=arc-20160816; b=o+QNnp6NWTRS4E3uzxYb3Kq0k/DBx8LIU0TowDj9RR4HN0HJuJun1l69eKIk5DK+RN znPHq+xVhAs4FTLL0FU7/oEdqq26CN0eF0mLunXQ4epLraWWiDl2dOfYpmtutOkPHwrU 7c3OrWhPVQenBWiZ9sOC2qsk4rrAXEvxjZG1F4R9ZyoIdz15jX5L5Pw174ds/lQLGjww oiOVh+YaihwEZ2i389q3jIGg2x8ipqyLSiojOb9+b5LVGO+LAynkNXMyy/upAHoIFQhz GWvDkzU/ekH9w9CXi/lamJlXBwWZjRkg23Rb1OXAHxF0NOP5OWsEE4ooBIwaPni9IleX 3z8g== 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=qvmLfDLzbW71v3d9IB7+eVUminhQnSactNGLrDwMRfs=; fh=raxTTJvC+dSJYbgfX5JG5gvrVjxTZXczN0HF8iDayLY=; b=Qexj38n18voOER2cV0lGwvuJQEqzWquAVm3c5H+hiBpB/sb13ijj4TTs02DtcZtJdy L07sauQhkUU2lA+38NMPFC/gZJ3jLiWLdpprU3WFb7LSE9aydYyZu531AwvP2dFXYw/o suwTJtK4U0Zi+MlC6vhVx9oGKvA24rYqqIEAS3WYNR5FyPYKH7spSw43sxibxGCLXYrj ZM8Ie/6MwWFJ8ourAHdjC6TkTwH8tllPqmv7aSNufNmHll3iG5lexFxK7cSQ2C+XPCPM vtCjHU1ebVj0UQ9xqIC7zmab19zSHeCukLWkjdhBm6W1CpW8xz6HvcyZlyC/Wq5jFDoH RvtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JazJuJxI; 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 b9-20020aa7d489000000b0051dd2d2cfb8si15167739edr.644.2023.07.17.08.38.54; Mon, 17 Jul 2023 08:39:17 -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=JazJuJxI; 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 S230274AbjGQPYN (ORCPT <rfc822;hadasmailinglist@gmail.com> + 99 others); Mon, 17 Jul 2023 11:24:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231779AbjGQPYE (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 17 Jul 2023 11:24:04 -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 E55CB10D3; Mon, 17 Jul 2023 08:23:47 -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 59BC16112C; Mon, 17 Jul 2023 15:23:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2FC70C433C8; Mon, 17 Jul 2023 15:23:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689607421; bh=BWbI9yDKpcKn7YyhyFkObG82wGckmUlkttodK+RJwNE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JazJuJxIrqcELqrY8aqDYDkCX3z/7enpoNbNDNmbCPPL/49Sj0j4RZ80+cClSQGTx mLZb3QLIx/htHp2zBmnnoLg9zk0iWMTfx61wjzK9tm0qM0ddGuTHaAH9XrGfTDJEyx Wp20YIntLt6HSR6uxMGXRf/GYsjNjrIjlkUnxi23J1deUBCmdwvRu29nnJgMn2cG56 IDLI0XeMZkesBuAacvkEIFUUNw/TgDaeIF2kmj279DFe4FukOlfHgSkCKM7m29fBGZ Khp+fBAVjVjNL/xibMBlNNnvS2pmJRl8edyZNrZ0bOAieh6YHv0LD+Dcb6BpN8VdxG yCza+MzlUFcug== From: "Masami Hiramatsu (Google)" <mhiramat@kernel.org> To: linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Steven Rostedt <rostedt@goodmis.org>, mhiramat@kernel.org, Martin KaFai Lau <martin.lau@linux.dev>, bpf@vger.kernel.org, Sven Schnelle <svens@linux.ibm.com>, Alexei Starovoitov <ast@kernel.org> Subject: [PATCH v2 2/9] bpf/btf: tracing: Move finding func-proto API and getting func-param API to BTF Date: Tue, 18 Jul 2023 00:23:37 +0900 Message-Id: <168960741686.34107.6330273416064011062.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 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771682773387989840 X-GMAIL-MSGID: 1771682773387989840 |
Series |
tracing: Improbe BTF support on probe events
|
|
Commit Message
Masami Hiramatsu (Google)
July 17, 2023, 3:23 p.m. UTC
From: Masami Hiramatsu (Google) <mhiramat@kernel.org> 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) <mhiramat@kernel.org> --- include/linux/btf.h | 4 ++++ kernel/bpf/btf.c | 45 ++++++++++++++++++++++++++++++++++++++++ kernel/trace/trace_probe.c | 50 +++++++++++++------------------------------- 3 files changed, 64 insertions(+), 35 deletions(-)
Comments
On Tue, 18 Jul 2023 00:23:37 +0900 "Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote: > From: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > 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. "different features." > > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> > --- > include/linux/btf.h | 4 ++++ > kernel/bpf/btf.c | 45 ++++++++++++++++++++++++++++++++++++++++ > kernel/trace/trace_probe.c | 50 +++++++++++++------------------------------- > 3 files changed, 64 insertions(+), 35 deletions(-) > > diff --git a/include/linux/btf.h b/include/linux/btf.h > index cac9f304e27a..98fbbcdd72ec 100644 > --- a/include/linux/btf.h > +++ b/include/linux/btf.h > @@ -221,6 +221,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(struct btf *btf, > + const char *func_name); > +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 817204d53372..e015b52956cb 100644 > --- a/kernel/bpf/btf.c > +++ b/kernel/bpf/btf.c > @@ -1947,6 +1947,51 @@ btf_resolve_size(const struct btf *btf, const struct btf_type *type, > return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL); > } > > +/* > + * Find a functio proto type by name, and return it. "function" > + * Return NULL if not found, or return -EINVAL if parameter is invalid. > + */ > +const struct btf_type *btf_find_func_proto(struct btf *btf, const char *func_name) > +{ > + const struct btf_type *t; > + s32 id; > + > + if (!btf || !func_name) > + return ERR_PTR(-EINVAL); > + > + id = btf_find_by_name_kind(btf, func_name, BTF_KIND_FUNC); > + if (id <= 0) > + return NULL; > + > + /* Get BTF_KIND_FUNC type */ > + t = btf_type_by_id(btf, id); > + if (!t || !btf_type_is_func(t)) > + return NULL; > + > + /* 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)) > + return NULL; > + > + return t; > +} > + > +/* > + * Get function parameter with the number of parameters. > + * This can return NULL if the function has no parameters. " It can return EINVAL if this function's parameters are NULL." -- Steve > + */ > +const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr) > +{ > + if (!func_proto || !nr) > + return ERR_PTR(-EINVAL); > + > + *nr = btf_type_vlen(func_proto); > + if (*nr > 0) > + return (const struct btf_param *)(func_proto + 1); > + else > + return NULL; > +} > + > static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id) > { > while (type_id < btf->start_id) > diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c > index c68a72707852..cd89fc1ebb42 100644 > --- a/kernel/trace/trace_probe.c > +++ b/kernel/trace/trace_probe.c > @@ -371,47 +371,23 @@ 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 = traceprobe_get_btf(); > - const struct btf_type *t; > - s32 id; > - > - if (!btf || !funcname) > - return ERR_PTR(-EINVAL); > - > - id = btf_find_by_name_kind(btf, funcname, BTF_KIND_FUNC); > - 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)) > - return ERR_PTR(-ENOENT); > - > - /* 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)) > - return ERR_PTR(-ENOENT); > - > - return t; > -} > - > static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, > bool tracepoint) > { > + struct btf *btf = traceprobe_get_btf(); > const struct btf_param *param; > const struct btf_type *t; > > - if (!funcname || !nr) > + if (!funcname || !nr || !btf) > return ERR_PTR(-EINVAL); > > - t = find_btf_func_proto(funcname); > - if (IS_ERR(t)) > + t = btf_find_func_proto(btf, funcname); > + if (IS_ERR_OR_NULL(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)) > + return param; > > /* Hide the first 'data' argument of tracepoint */ > if (tracepoint) { > @@ -490,8 +466,8 @@ static const struct fetch_type *parse_btf_retval_type( > const struct btf_type *t; > > if (btf && ctx->funcname) { > - t = find_btf_func_proto(ctx->funcname); > - if (!IS_ERR(t)) > + t = btf_find_func_proto(btf, ctx->funcname); > + if (!IS_ERR_OR_NULL(t)) > typestr = type_from_btf_id(btf, t->type); > } > > @@ -500,10 +476,14 @@ static const struct fetch_type *parse_btf_retval_type( > > static bool is_btf_retval_void(const char *funcname) > { > + struct btf *btf = traceprobe_get_btf(); > const struct btf_type *t; > > - t = find_btf_func_proto(funcname); > - if (IS_ERR(t)) > + if (!btf) > + return false; > + > + t = btf_find_func_proto(btf, funcname); > + if (IS_ERR_OR_NULL(t)) > return false; > > return t->type == 0;
On Mon, 17 Jul 2023 14:39:14 -0400 Steven Rostedt <rostedt@goodmis.org> wrote: > On Tue, 18 Jul 2023 00:23:37 +0900 > "Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote: > > > From: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > > > 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. > > "different features." Thanks! > > > > > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > --- > > include/linux/btf.h | 4 ++++ > > kernel/bpf/btf.c | 45 ++++++++++++++++++++++++++++++++++++++++ > > kernel/trace/trace_probe.c | 50 +++++++++++++------------------------------- > > 3 files changed, 64 insertions(+), 35 deletions(-) > > > > diff --git a/include/linux/btf.h b/include/linux/btf.h > > index cac9f304e27a..98fbbcdd72ec 100644 > > --- a/include/linux/btf.h > > +++ b/include/linux/btf.h > > @@ -221,6 +221,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(struct btf *btf, > > + const char *func_name); > > +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 817204d53372..e015b52956cb 100644 > > --- a/kernel/bpf/btf.c > > +++ b/kernel/bpf/btf.c > > @@ -1947,6 +1947,51 @@ btf_resolve_size(const struct btf *btf, const struct btf_type *type, > > return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL); > > } > > > > +/* > > + * Find a functio proto type by name, and return it. > > "function" Oops. > > > + * Return NULL if not found, or return -EINVAL if parameter is invalid. > > + */ > > +const struct btf_type *btf_find_func_proto(struct btf *btf, const char *func_name) > > +{ > > + const struct btf_type *t; > > + s32 id; > > + > > + if (!btf || !func_name) > > + return ERR_PTR(-EINVAL); > > + > > + id = btf_find_by_name_kind(btf, func_name, BTF_KIND_FUNC); > > + if (id <= 0) > > + return NULL; > > + > > + /* Get BTF_KIND_FUNC type */ > > + t = btf_type_by_id(btf, id); > > + if (!t || !btf_type_is_func(t)) > > + return NULL; > > + > > + /* 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)) > > + return NULL; > > + > > + return t; > > +} > > + > > +/* > > + * Get function parameter with the number of parameters. > > + * This can return NULL if the function has no parameters. > > " It can return EINVAL if this function's parameters are NULL." No, as you can see the code, if btf_type_vlen(func_proto) returns 0 (means the function proto type has no parameters), btf_get_func_param() returns NULL. This is important point because user needs to use IS_ERR_OR_NULL(ret) instead of IS_ERR(ret). Thank you, > > -- Steve > > > > + */ > > +const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr) > > +{ > > + if (!func_proto || !nr) > > + return ERR_PTR(-EINVAL); > > + > > + *nr = btf_type_vlen(func_proto); > > + if (*nr > 0) > > + return (const struct btf_param *)(func_proto + 1); > > + else > > + return NULL; > > +} > > + > > static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id) > > { > > while (type_id < btf->start_id) > > diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c > > index c68a72707852..cd89fc1ebb42 100644 > > --- a/kernel/trace/trace_probe.c > > +++ b/kernel/trace/trace_probe.c > > @@ -371,47 +371,23 @@ 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 = traceprobe_get_btf(); > > - const struct btf_type *t; > > - s32 id; > > - > > - if (!btf || !funcname) > > - return ERR_PTR(-EINVAL); > > - > > - id = btf_find_by_name_kind(btf, funcname, BTF_KIND_FUNC); > > - 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)) > > - return ERR_PTR(-ENOENT); > > - > > - /* 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)) > > - return ERR_PTR(-ENOENT); > > - > > - return t; > > -} > > - > > static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, > > bool tracepoint) > > { > > + struct btf *btf = traceprobe_get_btf(); > > const struct btf_param *param; > > const struct btf_type *t; > > > > - if (!funcname || !nr) > > + if (!funcname || !nr || !btf) > > return ERR_PTR(-EINVAL); > > > > - t = find_btf_func_proto(funcname); > > - if (IS_ERR(t)) > > + t = btf_find_func_proto(btf, funcname); > > + if (IS_ERR_OR_NULL(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)) > > + return param; > > > > /* Hide the first 'data' argument of tracepoint */ > > if (tracepoint) { > > @@ -490,8 +466,8 @@ static const struct fetch_type *parse_btf_retval_type( > > const struct btf_type *t; > > > > if (btf && ctx->funcname) { > > - t = find_btf_func_proto(ctx->funcname); > > - if (!IS_ERR(t)) > > + t = btf_find_func_proto(btf, ctx->funcname); > > + if (!IS_ERR_OR_NULL(t)) > > typestr = type_from_btf_id(btf, t->type); > > } > > > > @@ -500,10 +476,14 @@ static const struct fetch_type *parse_btf_retval_type( > > > > static bool is_btf_retval_void(const char *funcname) > > { > > + struct btf *btf = traceprobe_get_btf(); > > const struct btf_type *t; > > > > - t = find_btf_func_proto(funcname); > > - if (IS_ERR(t)) > > + if (!btf) > > + return false; > > + > > + t = btf_find_func_proto(btf, funcname); > > + if (IS_ERR_OR_NULL(t)) > > return false; > > > > return t->type == 0; >
On Tue, 18 Jul 2023 08:46:34 +0900 Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote: > > > + * Get function parameter with the number of parameters. > > > + * This can return NULL if the function has no parameters. > > > > " It can return EINVAL if this function's parameters are NULL." > > No, as you can see the code, if btf_type_vlen(func_proto) returns 0 (means > the function proto type has no parameters), btf_get_func_param() returns > NULL. This is important point because user needs to use IS_ERR_OR_NULL(ret) > instead of IS_ERR(ret). I didn't mean to replace what you had, I meant you left that part out. In other words, you have to check for IS_ERR_OR_NULL(ret), not just "!ret". -- Steve > > > > > + */ > > > +const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr) > > > +{ > > > + if (!func_proto || !nr) > > > + return ERR_PTR(-EINVAL); > > > +
On Mon, Jul 17, 2023 at 4:46 PM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > > > > > + * Return NULL if not found, or return -EINVAL if parameter is invalid. > > > + */ > > > +const struct btf_type *btf_find_func_proto(struct btf *btf, const char *func_name) > > > +{ > > > + const struct btf_type *t; > > > + s32 id; > > > + > > > + if (!btf || !func_name) > > > + return ERR_PTR(-EINVAL); Please remove these checks. We don't do defensive programming in the BPF subsystem. Don't pass NULL pointers to such functions.
On Mon, 17 Jul 2023 19:51:29 -0400 Steven Rostedt <rostedt@goodmis.org> wrote: > On Tue, 18 Jul 2023 08:46:34 +0900 > Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote: > > > > > + * Get function parameter with the number of parameters. > > > > + * This can return NULL if the function has no parameters. > > > > > > " It can return EINVAL if this function's parameters are NULL." > > > > No, as you can see the code, if btf_type_vlen(func_proto) returns 0 (means > > the function proto type has no parameters), btf_get_func_param() returns > > NULL. This is important point because user needs to use IS_ERR_OR_NULL(ret) > > instead of IS_ERR(ret). > > I didn't mean to replace what you had, I meant you left that part out. In > other words, you have to check for IS_ERR_OR_NULL(ret), not just "!ret". Ah, got it! Let me update it. Thank you! > > -- Steve > > > > > > > > + */ > > > > +const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr) > > > > +{ > > > > + if (!func_proto || !nr) > > > > + return ERR_PTR(-EINVAL); > > > > +
On Mon, 17 Jul 2023 16:51:29 -0700 Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote: > On Mon, Jul 17, 2023 at 4:46 PM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > > > > > > > > + * Return NULL if not found, or return -EINVAL if parameter is invalid. > > > > + */ > > > > +const struct btf_type *btf_find_func_proto(struct btf *btf, const char *func_name) > > > > +{ > > > > + const struct btf_type *t; > > > > + s32 id; > > > > + > > > > + if (!btf || !func_name) > > > > + return ERR_PTR(-EINVAL); > > Please remove these checks. > We don't do defensive programming in the BPF subsystem. > Don't pass NULL pointers to such functions. OK, we will trust API user to pass a non-NULL parameters. Thank you!
On Mon, Jul 17, 2023 at 11:24 PM Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote: > > From: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > 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) <mhiramat@kernel.org> > --- > include/linux/btf.h | 4 ++++ > kernel/bpf/btf.c | 45 ++++++++++++++++++++++++++++++++++++++++ > kernel/trace/trace_probe.c | 50 +++++++++++++------------------------------- > 3 files changed, 64 insertions(+), 35 deletions(-) > > diff --git a/include/linux/btf.h b/include/linux/btf.h > index cac9f304e27a..98fbbcdd72ec 100644 > --- a/include/linux/btf.h > +++ b/include/linux/btf.h > @@ -221,6 +221,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(struct btf *btf, > + const char *func_name); > +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 817204d53372..e015b52956cb 100644 > --- a/kernel/bpf/btf.c > +++ b/kernel/bpf/btf.c > @@ -1947,6 +1947,51 @@ btf_resolve_size(const struct btf *btf, const struct btf_type *type, > return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL); > } > > +/* > + * Find a functio proto type by name, and return it. > + * Return NULL if not found, or return -EINVAL if parameter is invalid. > + */ > +const struct btf_type *btf_find_func_proto(struct btf *btf, const char *func_name) > +{ > + const struct btf_type *t; > + s32 id; > + > + if (!btf || !func_name) > + return ERR_PTR(-EINVAL); > + > + id = btf_find_by_name_kind(btf, func_name, BTF_KIND_FUNC); > + if (id <= 0) > + return NULL; > + > + /* Get BTF_KIND_FUNC type */ > + t = btf_type_by_id(btf, id); > + if (!t || !btf_type_is_func(t)) > + return NULL; > + > + /* 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)) > + return NULL; > + > + return t; > +} > + > +/* > + * Get function parameter with the number of parameters. > + * This can return NULL if the function has no parameters. > + */ > +const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr) > +{ > + if (!func_proto || !nr) > + return ERR_PTR(-EINVAL); > + > + *nr = btf_type_vlen(func_proto); > + if (*nr > 0) > + return (const struct btf_param *)(func_proto + 1); > + else > + return NULL; > +} > + > static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id) > { > while (type_id < btf->start_id) > diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c > index c68a72707852..cd89fc1ebb42 100644 > --- a/kernel/trace/trace_probe.c > +++ b/kernel/trace/trace_probe.c > @@ -371,47 +371,23 @@ 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 = traceprobe_get_btf(); > - const struct btf_type *t; > - s32 id; > - > - if (!btf || !funcname) > - return ERR_PTR(-EINVAL); > - > - id = btf_find_by_name_kind(btf, funcname, BTF_KIND_FUNC); > - 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)) > - return ERR_PTR(-ENOENT); > - > - /* 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)) > - return ERR_PTR(-ENOENT); > - > - return t; > -} > - > static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, > bool tracepoint) > { > + struct btf *btf = traceprobe_get_btf(); I found that traceprobe_get_btf() only returns the vmlinux's btf. But if the function is defined in a kernel module, we should get the module's btf. -- Donglin > const struct btf_param *param; > const struct btf_type *t; > > - if (!funcname || !nr) > + if (!funcname || !nr || !btf) > return ERR_PTR(-EINVAL); > > - t = find_btf_func_proto(funcname); > - if (IS_ERR(t)) > + t = btf_find_func_proto(btf, funcname); > + if (IS_ERR_OR_NULL(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)) > + return param; > > /* Hide the first 'data' argument of tracepoint */ > if (tracepoint) { > @@ -490,8 +466,8 @@ static const struct fetch_type *parse_btf_retval_type( > const struct btf_type *t; > > if (btf && ctx->funcname) { > - t = find_btf_func_proto(ctx->funcname); > - if (!IS_ERR(t)) > + t = btf_find_func_proto(btf, ctx->funcname); > + if (!IS_ERR_OR_NULL(t)) > typestr = type_from_btf_id(btf, t->type); > } > > @@ -500,10 +476,14 @@ static const struct fetch_type *parse_btf_retval_type( > > static bool is_btf_retval_void(const char *funcname) > { > + struct btf *btf = traceprobe_get_btf(); > const struct btf_type *t; > > - t = find_btf_func_proto(funcname); > - if (IS_ERR(t)) > + if (!btf) > + return false; > + > + t = btf_find_func_proto(btf, funcname); > + if (IS_ERR_OR_NULL(t)) > return false; > > return t->type == 0; > >
On Tue, 18 Jul 2023 10:40:09 +0800 Donglin Peng <dolinux.peng@gmail.com> wrote: > On Mon, Jul 17, 2023 at 11:24 PM Masami Hiramatsu (Google) > <mhiramat@kernel.org> wrote: > > > > From: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > > > 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) <mhiramat@kernel.org> > > --- > > include/linux/btf.h | 4 ++++ > > kernel/bpf/btf.c | 45 ++++++++++++++++++++++++++++++++++++++++ > > kernel/trace/trace_probe.c | 50 +++++++++++++------------------------------- > > 3 files changed, 64 insertions(+), 35 deletions(-) > > > > diff --git a/include/linux/btf.h b/include/linux/btf.h > > index cac9f304e27a..98fbbcdd72ec 100644 > > --- a/include/linux/btf.h > > +++ b/include/linux/btf.h > > @@ -221,6 +221,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(struct btf *btf, > > + const char *func_name); > > +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 817204d53372..e015b52956cb 100644 > > --- a/kernel/bpf/btf.c > > +++ b/kernel/bpf/btf.c > > @@ -1947,6 +1947,51 @@ btf_resolve_size(const struct btf *btf, const struct btf_type *type, > > return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL); > > } > > > > +/* > > + * Find a functio proto type by name, and return it. > > + * Return NULL if not found, or return -EINVAL if parameter is invalid. > > + */ > > +const struct btf_type *btf_find_func_proto(struct btf *btf, const char *func_name) > > +{ > > + const struct btf_type *t; > > + s32 id; > > + > > + if (!btf || !func_name) > > + return ERR_PTR(-EINVAL); > > + > > + id = btf_find_by_name_kind(btf, func_name, BTF_KIND_FUNC); > > + if (id <= 0) > > + return NULL; > > + > > + /* Get BTF_KIND_FUNC type */ > > + t = btf_type_by_id(btf, id); > > + if (!t || !btf_type_is_func(t)) > > + return NULL; > > + > > + /* 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)) > > + return NULL; > > + > > + return t; > > +} > > + > > +/* > > + * Get function parameter with the number of parameters. > > + * This can return NULL if the function has no parameters. > > + */ > > +const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr) > > +{ > > + if (!func_proto || !nr) > > + return ERR_PTR(-EINVAL); > > + > > + *nr = btf_type_vlen(func_proto); > > + if (*nr > 0) > > + return (const struct btf_param *)(func_proto + 1); > > + else > > + return NULL; > > +} > > + > > static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id) > > { > > while (type_id < btf->start_id) > > diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c > > index c68a72707852..cd89fc1ebb42 100644 > > --- a/kernel/trace/trace_probe.c > > +++ b/kernel/trace/trace_probe.c > > @@ -371,47 +371,23 @@ 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 = traceprobe_get_btf(); > > - const struct btf_type *t; > > - s32 id; > > - > > - if (!btf || !funcname) > > - return ERR_PTR(-EINVAL); > > - > > - id = btf_find_by_name_kind(btf, funcname, BTF_KIND_FUNC); > > - 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)) > > - return ERR_PTR(-ENOENT); > > - > > - /* 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)) > > - return ERR_PTR(-ENOENT); > > - > > - return t; > > -} > > - > > static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, > > bool tracepoint) > > { > > + struct btf *btf = traceprobe_get_btf(); > > I found that traceprobe_get_btf() only returns the vmlinux's btf. But > if the function is > defined in a kernel module, we should get the module's btf. > Good catch! That should be a separated fix (or improvement?) I think it's better to use btf_get() and btf_put(), and pass btf via traceprobe_parse_context. Thank you! > -- Donglin > > > const struct btf_param *param; > > const struct btf_type *t; > > > > - if (!funcname || !nr) > > + if (!funcname || !nr || !btf) > > return ERR_PTR(-EINVAL); > > > > - t = find_btf_func_proto(funcname); > > - if (IS_ERR(t)) > > + t = btf_find_func_proto(btf, funcname); > > + if (IS_ERR_OR_NULL(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)) > > + return param; > > > > /* Hide the first 'data' argument of tracepoint */ > > if (tracepoint) { > > @@ -490,8 +466,8 @@ static const struct fetch_type *parse_btf_retval_type( > > const struct btf_type *t; > > > > if (btf && ctx->funcname) { > > - t = find_btf_func_proto(ctx->funcname); > > - if (!IS_ERR(t)) > > + t = btf_find_func_proto(btf, ctx->funcname); > > + if (!IS_ERR_OR_NULL(t)) > > typestr = type_from_btf_id(btf, t->type); > > } > > > > @@ -500,10 +476,14 @@ static const struct fetch_type *parse_btf_retval_type( > > > > static bool is_btf_retval_void(const char *funcname) > > { > > + struct btf *btf = traceprobe_get_btf(); > > const struct btf_type *t; > > > > - t = find_btf_func_proto(funcname); > > - if (IS_ERR(t)) > > + if (!btf) > > + return false; > > + > > + t = btf_find_func_proto(btf, funcname); > > + if (IS_ERR_OR_NULL(t)) > > return false; > > > > return t->type == 0; > > > >
On Tue, 18 Jul 2023 19:44:31 +0900 Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote: > > > static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, > > > bool tracepoint) > > > { > > > + struct btf *btf = traceprobe_get_btf(); > > > > I found that traceprobe_get_btf() only returns the vmlinux's btf. But > > if the function is > > defined in a kernel module, we should get the module's btf. > > > > Good catch! That should be a separated fix (or improvement?) > I think it's better to use btf_get() and btf_put(), and pass btf via > traceprobe_parse_context. Hmm, it seems that there is no exposed API to get the module's btf. Should I use btf_idr and btf_idr_lock directly to find the corresponding btf? If there isn't yet, I will add it too. Thank you,
On Tue, Jul 18, 2023 at 6:56 AM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > On Tue, 18 Jul 2023 19:44:31 +0900 > Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote: > > > > > static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, > > > > bool tracepoint) > > > > { > > > > + struct btf *btf = traceprobe_get_btf(); > > > > > > I found that traceprobe_get_btf() only returns the vmlinux's btf. But > > > if the function is > > > defined in a kernel module, we should get the module's btf. > > > > > > > Good catch! That should be a separated fix (or improvement?) > > I think it's better to use btf_get() and btf_put(), and pass btf via > > traceprobe_parse_context. > > Hmm, it seems that there is no exposed API to get the module's btf. > Should I use btf_idr and btf_idr_lock directly to find the corresponding > btf? If there isn't yet, I will add it too. There is bpf_find_btf_id. Probably drop 'static' from it and use it.
On Tue, 18 Jul 2023 10:11:01 -0700 Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote: > On Tue, Jul 18, 2023 at 6:56 AM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > > > On Tue, 18 Jul 2023 19:44:31 +0900 > > Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote: > > > > > > > static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, > > > > > bool tracepoint) > > > > > { > > > > > + struct btf *btf = traceprobe_get_btf(); > > > > > > > > I found that traceprobe_get_btf() only returns the vmlinux's btf. But > > > > if the function is > > > > defined in a kernel module, we should get the module's btf. > > > > > > > > > > Good catch! That should be a separated fix (or improvement?) > > > I think it's better to use btf_get() and btf_put(), and pass btf via > > > traceprobe_parse_context. > > > > Hmm, it seems that there is no exposed API to get the module's btf. > > Should I use btf_idr and btf_idr_lock directly to find the corresponding > > btf? If there isn't yet, I will add it too. > > There is bpf_find_btf_id. > Probably drop 'static' from it and use it. Thanks! BTW, that API seems to search BTF type info by name. If user want to specify a module name, do we need a new API? (Or expand the function to parse a module name in given name?) Thank you,
On Tue, Jul 18, 2023 at 4:03 PM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > On Tue, 18 Jul 2023 10:11:01 -0700 > Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote: > > > On Tue, Jul 18, 2023 at 6:56 AM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > > > > > On Tue, 18 Jul 2023 19:44:31 +0900 > > > Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote: > > > > > > > > > static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, > > > > > > bool tracepoint) > > > > > > { > > > > > > + struct btf *btf = traceprobe_get_btf(); > > > > > > > > > > I found that traceprobe_get_btf() only returns the vmlinux's btf. But > > > > > if the function is > > > > > defined in a kernel module, we should get the module's btf. > > > > > > > > > > > > > Good catch! That should be a separated fix (or improvement?) > > > > I think it's better to use btf_get() and btf_put(), and pass btf via > > > > traceprobe_parse_context. > > > > > > Hmm, it seems that there is no exposed API to get the module's btf. > > > Should I use btf_idr and btf_idr_lock directly to find the corresponding > > > btf? If there isn't yet, I will add it too. > > > > There is bpf_find_btf_id. > > Probably drop 'static' from it and use it. > > Thanks! BTW, that API seems to search BTF type info by name. If user want to > specify a module name, do we need a new API? (Or expand the function to parse > a module name in given name?) We can allow users specify module name, but how would it help? Do you want to allow static func names ? But module name won't help. There can be many statics with the same name in the module. Currently pahole filters out all ambiguous things in BTF. Alan is working on better representation of statics in BTF. The work is still in progress. For now I don't see a need for an api to specify module, since it's not a modifier that can be relied upon to disambiguate. Hence bpf_find_btf_id that transparently searches across all should be enough. At least it was enough for all of bpf use cases.
On Wed, Jul 19, 2023 at 7:03 AM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > On Tue, 18 Jul 2023 10:11:01 -0700 > Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote: > > > On Tue, Jul 18, 2023 at 6:56 AM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > > > > > On Tue, 18 Jul 2023 19:44:31 +0900 > > > Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote: > > > > > > > > > static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, > > > > > > bool tracepoint) > > > > > > { > > > > > > + struct btf *btf = traceprobe_get_btf(); > > > > > > > > > > I found that traceprobe_get_btf() only returns the vmlinux's btf. But > > > > > if the function is > > > > > defined in a kernel module, we should get the module's btf. > > > > > > > > > > > > > Good catch! That should be a separated fix (or improvement?) > > > > I think it's better to use btf_get() and btf_put(), and pass btf via > > > > traceprobe_parse_context. > > > > > > Hmm, it seems that there is no exposed API to get the module's btf. > > > Should I use btf_idr and btf_idr_lock directly to find the corresponding > > > btf? If there isn't yet, I will add it too. > > > > There is bpf_find_btf_id. > > Probably drop 'static' from it and use it. > > Thanks! BTW, that API seems to search BTF type info by name. If user want to > specify a module name, do we need a new API? (Or expand the function to parse > a module name in given name?) btf_get_module_btf can be used to get a module's btf, but we have to use find_module to get the module by its name firstly. > > Thank you, > -- > Masami Hiramatsu (Google) <mhiramat@kernel.org>
On 17/07/2023 16:23, Masami Hiramatsu (Google) wrote: > From: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > 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) <mhiramat@kernel.org> > --- > include/linux/btf.h | 4 ++++ > kernel/bpf/btf.c | 45 ++++++++++++++++++++++++++++++++++++++++ > kernel/trace/trace_probe.c | 50 +++++++++++++------------------------------- > 3 files changed, 64 insertions(+), 35 deletions(-) > > diff --git a/include/linux/btf.h b/include/linux/btf.h > index cac9f304e27a..98fbbcdd72ec 100644 > --- a/include/linux/btf.h > +++ b/include/linux/btf.h > @@ -221,6 +221,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(struct btf *btf, > + const char *func_name); > +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 817204d53372..e015b52956cb 100644 > --- a/kernel/bpf/btf.c > +++ b/kernel/bpf/btf.c > @@ -1947,6 +1947,51 @@ btf_resolve_size(const struct btf *btf, const struct btf_type *type, > return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL); > } > > +/* > + * Find a functio proto type by name, and return it. > + * Return NULL if not found, or return -EINVAL if parameter is invalid. > + */ > +const struct btf_type *btf_find_func_proto(struct btf *btf, const char *func_name) > +{ > + const struct btf_type *t; > + s32 id; > + > + if (!btf || !func_name) > + return ERR_PTR(-EINVAL); > + > + id = btf_find_by_name_kind(btf, func_name, BTF_KIND_FUNC); as mentioned in my other mail, there are cases where the function name may have a .isra.0 suffix, but the BTF representation will not. I looked at this and it seems like symbol names are validated via traceprobe_parse_event_name() - will this validation allow a "."-suffix name? I tried the following (with pahole v1.25 that emits BTF for schedule_work.isra.0): [45454] FUNC 'schedule_work' type_id=45453 linkage=static $ echo 'f schedule_work.isra.0 $arg*' >> dynamic_events bash: echo: write error: No such file or directory So presuming that such "."-suffixed names are allowed, would it make sense to fall back to search BTF for the prefix ("schedule_work") instead of the full name ("schedule_work.isra.0"), as the former is what makes it into the BTF representation? Thanks! Alan > + if (id <= 0) > + return NULL; > + > + /* Get BTF_KIND_FUNC type */ > + t = btf_type_by_id(btf, id); > + if (!t || !btf_type_is_func(t)) > + return NULL; > + > + /* 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)) > + return NULL; > + > + return t; > +} > + > +/* > + * Get function parameter with the number of parameters. > + * This can return NULL if the function has no parameters. > + */ > +const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr) > +{ > + if (!func_proto || !nr) > + return ERR_PTR(-EINVAL); > + > + *nr = btf_type_vlen(func_proto); > + if (*nr > 0) > + return (const struct btf_param *)(func_proto + 1); > + else > + return NULL; > +} > + > static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id) > { > while (type_id < btf->start_id) > diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c > index c68a72707852..cd89fc1ebb42 100644 > --- a/kernel/trace/trace_probe.c > +++ b/kernel/trace/trace_probe.c > @@ -371,47 +371,23 @@ 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 = traceprobe_get_btf(); > - const struct btf_type *t; > - s32 id; > - > - if (!btf || !funcname) > - return ERR_PTR(-EINVAL); > - > - id = btf_find_by_name_kind(btf, funcname, BTF_KIND_FUNC); > - 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)) > - return ERR_PTR(-ENOENT); > - > - /* 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)) > - return ERR_PTR(-ENOENT); > - > - return t; > -} > - > static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, > bool tracepoint) > { > + struct btf *btf = traceprobe_get_btf(); > const struct btf_param *param; > const struct btf_type *t; > > - if (!funcname || !nr) > + if (!funcname || !nr || !btf) > return ERR_PTR(-EINVAL); > > - t = find_btf_func_proto(funcname); > - if (IS_ERR(t)) > + t = btf_find_func_proto(btf, funcname); > + if (IS_ERR_OR_NULL(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)) > + return param; > > /* Hide the first 'data' argument of tracepoint */ > if (tracepoint) { > @@ -490,8 +466,8 @@ static const struct fetch_type *parse_btf_retval_type( > const struct btf_type *t; > > if (btf && ctx->funcname) { > - t = find_btf_func_proto(ctx->funcname); > - if (!IS_ERR(t)) > + t = btf_find_func_proto(btf, ctx->funcname); > + if (!IS_ERR_OR_NULL(t)) > typestr = type_from_btf_id(btf, t->type); > } > > @@ -500,10 +476,14 @@ static const struct fetch_type *parse_btf_retval_type( > > static bool is_btf_retval_void(const char *funcname) > { > + struct btf *btf = traceprobe_get_btf(); > const struct btf_type *t; > > - t = find_btf_func_proto(funcname); > - if (IS_ERR(t)) > + if (!btf) > + return false; > + > + t = btf_find_func_proto(btf, funcname); > + if (IS_ERR_OR_NULL(t)) > return false; > > return t->type == 0; > >
On Wed, 19 Jul 2023 10:09:48 +0800 Donglin Peng <dolinux.peng@gmail.com> wrote: > On Wed, Jul 19, 2023 at 7:03 AM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > > > On Tue, 18 Jul 2023 10:11:01 -0700 > > Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote: > > > > > On Tue, Jul 18, 2023 at 6:56 AM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > > > > > > > On Tue, 18 Jul 2023 19:44:31 +0900 > > > > Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote: > > > > > > > > > > > static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, > > > > > > > bool tracepoint) > > > > > > > { > > > > > > > + struct btf *btf = traceprobe_get_btf(); > > > > > > > > > > > > I found that traceprobe_get_btf() only returns the vmlinux's btf. But > > > > > > if the function is > > > > > > defined in a kernel module, we should get the module's btf. > > > > > > > > > > > > > > > > Good catch! That should be a separated fix (or improvement?) > > > > > I think it's better to use btf_get() and btf_put(), and pass btf via > > > > > traceprobe_parse_context. > > > > > > > > Hmm, it seems that there is no exposed API to get the module's btf. > > > > Should I use btf_idr and btf_idr_lock directly to find the corresponding > > > > btf? If there isn't yet, I will add it too. > > > > > > There is bpf_find_btf_id. > > > Probably drop 'static' from it and use it. > > > > Thanks! BTW, that API seems to search BTF type info by name. If user want to > > specify a module name, do we need a new API? (Or expand the function to parse > > a module name in given name?) > > btf_get_module_btf can be used to get a module's btf, but we have to use > find_module to get the module by its name firstly. Thanks for the info! OK, this will be useful. > > > > > Thank you, > > -- > > Masami Hiramatsu (Google) <mhiramat@kernel.org>
On Tue, 18 Jul 2023 16:12:55 -0700 Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote: > On Tue, Jul 18, 2023 at 4:03 PM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > > > On Tue, 18 Jul 2023 10:11:01 -0700 > > Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote: > > > > > On Tue, Jul 18, 2023 at 6:56 AM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > > > > > > > On Tue, 18 Jul 2023 19:44:31 +0900 > > > > Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote: > > > > > > > > > > > static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, > > > > > > > bool tracepoint) > > > > > > > { > > > > > > > + struct btf *btf = traceprobe_get_btf(); > > > > > > > > > > > > I found that traceprobe_get_btf() only returns the vmlinux's btf. But > > > > > > if the function is > > > > > > defined in a kernel module, we should get the module's btf. > > > > > > > > > > > > > > > > Good catch! That should be a separated fix (or improvement?) > > > > > I think it's better to use btf_get() and btf_put(), and pass btf via > > > > > traceprobe_parse_context. > > > > > > > > Hmm, it seems that there is no exposed API to get the module's btf. > > > > Should I use btf_idr and btf_idr_lock directly to find the corresponding > > > > btf? If there isn't yet, I will add it too. > > > > > > There is bpf_find_btf_id. > > > Probably drop 'static' from it and use it. > > > > Thanks! BTW, that API seems to search BTF type info by name. If user want to > > specify a module name, do we need a new API? (Or expand the function to parse > > a module name in given name?) > > We can allow users specify module name, but how would it help? > Do you want to allow static func names ? > But module name won't help. There can be many statics with the same name > in the module. Currently pahole filters out all ambiguous things in BTF. > Alan is working on better representation of statics in BTF. > The work is still in progress. Ah, got it. So currently we don't have to worry about that case. > > For now I don't see a need for an api to specify module, since it's not > a modifier that can be relied upon to disambiguate. > Hence bpf_find_btf_id that transparently searches across all should be enough. > At least it was enough for all of bpf use cases. OK. After updating the BTF I will revisit here. Thank you!
On Wed, 19 Jul 2023 13:36:48 +0100 Alan Maguire <alan.maguire@oracle.com> wrote: > On 17/07/2023 16:23, Masami Hiramatsu (Google) wrote: > > From: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > > > 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) <mhiramat@kernel.org> > > --- > > include/linux/btf.h | 4 ++++ > > kernel/bpf/btf.c | 45 ++++++++++++++++++++++++++++++++++++++++ > > kernel/trace/trace_probe.c | 50 +++++++++++++------------------------------- > > 3 files changed, 64 insertions(+), 35 deletions(-) > > > > diff --git a/include/linux/btf.h b/include/linux/btf.h > > index cac9f304e27a..98fbbcdd72ec 100644 > > --- a/include/linux/btf.h > > +++ b/include/linux/btf.h > > @@ -221,6 +221,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(struct btf *btf, > > + const char *func_name); > > +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 817204d53372..e015b52956cb 100644 > > --- a/kernel/bpf/btf.c > > +++ b/kernel/bpf/btf.c > > @@ -1947,6 +1947,51 @@ btf_resolve_size(const struct btf *btf, const struct btf_type *type, > > return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL); > > } > > > > +/* > > + * Find a functio proto type by name, and return it. > > + * Return NULL if not found, or return -EINVAL if parameter is invalid. > > + */ > > +const struct btf_type *btf_find_func_proto(struct btf *btf, const char *func_name) > > +{ > > + const struct btf_type *t; > > + s32 id; > > + > > + if (!btf || !func_name) > > + return ERR_PTR(-EINVAL); > > + > > + id = btf_find_by_name_kind(btf, func_name, BTF_KIND_FUNC); > > as mentioned in my other mail, there are cases where the function name > may have a .isra.0 suffix, but the BTF representation will not. I looked > at this and it seems like symbol names are validated via > traceprobe_parse_event_name() - will this validation allow a "."-suffix > name? I tried the following (with pahole v1.25 that emits BTF for > schedule_work.isra.0): > > [45454] FUNC 'schedule_work' type_id=45453 linkage=static That's a good point! I'm checking fprobe, but kprobes actually uses those suffixed names. > > $ echo 'f schedule_work.isra.0 $arg*' >> dynamic_events > bash: echo: write error: No such file or directory So maybe fprobe doesn't accept that. > So presuming that such "."-suffixed names are allowed, would it make > sense to fall back to search BTF for the prefix ("schedule_work") > instead of the full name ("schedule_work.isra.0"), as the former is what > makes it into the BTF representation? Thanks! OK, that's not a problem. My concern is that some "constprop" functions will replace a part of parameter with constant value (so it can skip passing some arguments). BTF argument may not work in such case. Let me check it. Thank you, > > Alan > > > + if (id <= 0) > > + return NULL; > > + > > + /* Get BTF_KIND_FUNC type */ > > + t = btf_type_by_id(btf, id); > > + if (!t || !btf_type_is_func(t)) > > + return NULL; > > + > > + /* 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)) > > + return NULL; > > + > > + return t; > > +} > > + > > +/* > > + * Get function parameter with the number of parameters. > > + * This can return NULL if the function has no parameters. > > + */ > > +const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr) > > +{ > > + if (!func_proto || !nr) > > + return ERR_PTR(-EINVAL); > > + > > + *nr = btf_type_vlen(func_proto); > > + if (*nr > 0) > > + return (const struct btf_param *)(func_proto + 1); > > + else > > + return NULL; > > +} > > + > > static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id) > > { > > while (type_id < btf->start_id) > > diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c > > index c68a72707852..cd89fc1ebb42 100644 > > --- a/kernel/trace/trace_probe.c > > +++ b/kernel/trace/trace_probe.c > > @@ -371,47 +371,23 @@ 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 = traceprobe_get_btf(); > > - const struct btf_type *t; > > - s32 id; > > - > > - if (!btf || !funcname) > > - return ERR_PTR(-EINVAL); > > - > > - id = btf_find_by_name_kind(btf, funcname, BTF_KIND_FUNC); > > - 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)) > > - return ERR_PTR(-ENOENT); > > - > > - /* 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)) > > - return ERR_PTR(-ENOENT); > > - > > - return t; > > -} > > - > > static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, > > bool tracepoint) > > { > > + struct btf *btf = traceprobe_get_btf(); > > const struct btf_param *param; > > const struct btf_type *t; > > > > - if (!funcname || !nr) > > + if (!funcname || !nr || !btf) > > return ERR_PTR(-EINVAL); > > > > - t = find_btf_func_proto(funcname); > > - if (IS_ERR(t)) > > + t = btf_find_func_proto(btf, funcname); > > + if (IS_ERR_OR_NULL(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)) > > + return param; > > > > /* Hide the first 'data' argument of tracepoint */ > > if (tracepoint) { > > @@ -490,8 +466,8 @@ static const struct fetch_type *parse_btf_retval_type( > > const struct btf_type *t; > > > > if (btf && ctx->funcname) { > > - t = find_btf_func_proto(ctx->funcname); > > - if (!IS_ERR(t)) > > + t = btf_find_func_proto(btf, ctx->funcname); > > + if (!IS_ERR_OR_NULL(t)) > > typestr = type_from_btf_id(btf, t->type); > > } > > > > @@ -500,10 +476,14 @@ static const struct fetch_type *parse_btf_retval_type( > > > > static bool is_btf_retval_void(const char *funcname) > > { > > + struct btf *btf = traceprobe_get_btf(); > > const struct btf_type *t; > > > > - t = find_btf_func_proto(funcname); > > - if (IS_ERR(t)) > > + if (!btf) > > + return false; > > + > > + t = btf_find_func_proto(btf, funcname); > > + if (IS_ERR_OR_NULL(t)) > > return false; > > > > return t->type == 0; > > > >
On 19/07/2023 16:24, Masami Hiramatsu (Google) wrote: > On Wed, 19 Jul 2023 13:36:48 +0100 > Alan Maguire <alan.maguire@oracle.com> wrote: > >> On 17/07/2023 16:23, Masami Hiramatsu (Google) wrote: >>> From: Masami Hiramatsu (Google) <mhiramat@kernel.org> >>> >>> 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) <mhiramat@kernel.org> >>> --- >>> include/linux/btf.h | 4 ++++ >>> kernel/bpf/btf.c | 45 ++++++++++++++++++++++++++++++++++++++++ >>> kernel/trace/trace_probe.c | 50 +++++++++++++------------------------------- >>> 3 files changed, 64 insertions(+), 35 deletions(-) >>> >>> diff --git a/include/linux/btf.h b/include/linux/btf.h >>> index cac9f304e27a..98fbbcdd72ec 100644 >>> --- a/include/linux/btf.h >>> +++ b/include/linux/btf.h >>> @@ -221,6 +221,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(struct btf *btf, >>> + const char *func_name); >>> +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 817204d53372..e015b52956cb 100644 >>> --- a/kernel/bpf/btf.c >>> +++ b/kernel/bpf/btf.c >>> @@ -1947,6 +1947,51 @@ btf_resolve_size(const struct btf *btf, const struct btf_type *type, >>> return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL); >>> } >>> >>> +/* >>> + * Find a functio proto type by name, and return it. >>> + * Return NULL if not found, or return -EINVAL if parameter is invalid. >>> + */ >>> +const struct btf_type *btf_find_func_proto(struct btf *btf, const char *func_name) >>> +{ >>> + const struct btf_type *t; >>> + s32 id; >>> + >>> + if (!btf || !func_name) >>> + return ERR_PTR(-EINVAL); >>> + >>> + id = btf_find_by_name_kind(btf, func_name, BTF_KIND_FUNC); >> >> as mentioned in my other mail, there are cases where the function name >> may have a .isra.0 suffix, but the BTF representation will not. I looked >> at this and it seems like symbol names are validated via >> traceprobe_parse_event_name() - will this validation allow a "."-suffix >> name? I tried the following (with pahole v1.25 that emits BTF for >> schedule_work.isra.0): >> >> [45454] FUNC 'schedule_work' type_id=45453 linkage=static > > That's a good point! I'm checking fprobe, but kprobes actually > uses those suffixed names. > >> >> $ echo 'f schedule_work.isra.0 $arg*' >> dynamic_events >> bash: echo: write error: No such file or directory > > So maybe fprobe doesn't accept that. > >> So presuming that such "."-suffixed names are allowed, would it make >> sense to fall back to search BTF for the prefix ("schedule_work") >> instead of the full name ("schedule_work.isra.0"), as the former is what >> makes it into the BTF representation? Thanks! > > OK, that's not a problem. My concern is that some "constprop" functions > will replace a part of parameter with constant value (so it can skip > passing some arguments). BTF argument may not work in such case. > Let me check it. > If the --btf_skip_inconsistent_proto option (also specified for pahole v1.25) is working as expected, any such cases shouldn't make it into BTF. The idea is we skip representing any static functions in BTF that 1. have multiple different function prototypes (since we don't yet have good mechanisms to choose which one the user was referring to); or 2. use an unexpected register for a parameter. We gather that info from DWARF and then make choices on whether to skip adding the function to BTF or not. See dwarf_loader.c in https://github.com/acmel/dwarves for more details on the process used if needed. The only exception for case 2 - where we allow unexpected registers - is where multiple registers are used to represent a >64 bit structure passed as a parameter by value. It might make sense to exclude such functions from fprobe support (there's only a few of these functions in the kernel if I remember, so it's no huge loss). So long story short, if a function made it into in BTF, it is likely using the registers you expect it to, unless it has a struct parameter. It might be worth excluding such functions if you're worried about getting unreliable data. Thanks! Alan > Thank you, > >> >> Alan >> >>> + if (id <= 0) >>> + return NULL; >>> + >>> + /* Get BTF_KIND_FUNC type */ >>> + t = btf_type_by_id(btf, id); >>> + if (!t || !btf_type_is_func(t)) >>> + return NULL; >>> + >>> + /* 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)) >>> + return NULL; >>> + >>> + return t; >>> +} >>> + >>> +/* >>> + * Get function parameter with the number of parameters. >>> + * This can return NULL if the function has no parameters. >>> + */ >>> +const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr) >>> +{ >>> + if (!func_proto || !nr) >>> + return ERR_PTR(-EINVAL); >>> + >>> + *nr = btf_type_vlen(func_proto); >>> + if (*nr > 0) >>> + return (const struct btf_param *)(func_proto + 1); >>> + else >>> + return NULL; >>> +} >>> + >>> static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id) >>> { >>> while (type_id < btf->start_id) >>> diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c >>> index c68a72707852..cd89fc1ebb42 100644 >>> --- a/kernel/trace/trace_probe.c >>> +++ b/kernel/trace/trace_probe.c >>> @@ -371,47 +371,23 @@ 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 = traceprobe_get_btf(); >>> - const struct btf_type *t; >>> - s32 id; >>> - >>> - if (!btf || !funcname) >>> - return ERR_PTR(-EINVAL); >>> - >>> - id = btf_find_by_name_kind(btf, funcname, BTF_KIND_FUNC); >>> - 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)) >>> - return ERR_PTR(-ENOENT); >>> - >>> - /* 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)) >>> - return ERR_PTR(-ENOENT); >>> - >>> - return t; >>> -} >>> - >>> static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, >>> bool tracepoint) >>> { >>> + struct btf *btf = traceprobe_get_btf(); >>> const struct btf_param *param; >>> const struct btf_type *t; >>> >>> - if (!funcname || !nr) >>> + if (!funcname || !nr || !btf) >>> return ERR_PTR(-EINVAL); >>> >>> - t = find_btf_func_proto(funcname); >>> - if (IS_ERR(t)) >>> + t = btf_find_func_proto(btf, funcname); >>> + if (IS_ERR_OR_NULL(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)) >>> + return param; >>> >>> /* Hide the first 'data' argument of tracepoint */ >>> if (tracepoint) { >>> @@ -490,8 +466,8 @@ static const struct fetch_type *parse_btf_retval_type( >>> const struct btf_type *t; >>> >>> if (btf && ctx->funcname) { >>> - t = find_btf_func_proto(ctx->funcname); >>> - if (!IS_ERR(t)) >>> + t = btf_find_func_proto(btf, ctx->funcname); >>> + if (!IS_ERR_OR_NULL(t)) >>> typestr = type_from_btf_id(btf, t->type); >>> } >>> >>> @@ -500,10 +476,14 @@ static const struct fetch_type *parse_btf_retval_type( >>> >>> static bool is_btf_retval_void(const char *funcname) >>> { >>> + struct btf *btf = traceprobe_get_btf(); >>> const struct btf_type *t; >>> >>> - t = find_btf_func_proto(funcname); >>> - if (IS_ERR(t)) >>> + if (!btf) >>> + return false; >>> + >>> + t = btf_find_func_proto(btf, funcname); >>> + if (IS_ERR_OR_NULL(t)) >>> return false; >>> >>> return t->type == 0; >>> >>> > >
diff --git a/include/linux/btf.h b/include/linux/btf.h index cac9f304e27a..98fbbcdd72ec 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -221,6 +221,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(struct btf *btf, + const char *func_name); +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 817204d53372..e015b52956cb 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -1947,6 +1947,51 @@ btf_resolve_size(const struct btf *btf, const struct btf_type *type, return __btf_resolve_size(btf, type, type_size, NULL, NULL, NULL, NULL); } +/* + * Find a functio proto type by name, and return it. + * Return NULL if not found, or return -EINVAL if parameter is invalid. + */ +const struct btf_type *btf_find_func_proto(struct btf *btf, const char *func_name) +{ + const struct btf_type *t; + s32 id; + + if (!btf || !func_name) + return ERR_PTR(-EINVAL); + + id = btf_find_by_name_kind(btf, func_name, BTF_KIND_FUNC); + if (id <= 0) + return NULL; + + /* Get BTF_KIND_FUNC type */ + t = btf_type_by_id(btf, id); + if (!t || !btf_type_is_func(t)) + return NULL; + + /* 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)) + return NULL; + + return t; +} + +/* + * Get function parameter with the number of parameters. + * This can return NULL if the function has no parameters. + */ +const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr) +{ + if (!func_proto || !nr) + return ERR_PTR(-EINVAL); + + *nr = btf_type_vlen(func_proto); + if (*nr > 0) + return (const struct btf_param *)(func_proto + 1); + else + return NULL; +} + static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id) { while (type_id < btf->start_id) diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c index c68a72707852..cd89fc1ebb42 100644 --- a/kernel/trace/trace_probe.c +++ b/kernel/trace/trace_probe.c @@ -371,47 +371,23 @@ 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 = traceprobe_get_btf(); - const struct btf_type *t; - s32 id; - - if (!btf || !funcname) - return ERR_PTR(-EINVAL); - - id = btf_find_by_name_kind(btf, funcname, BTF_KIND_FUNC); - 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)) - return ERR_PTR(-ENOENT); - - /* 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)) - return ERR_PTR(-ENOENT); - - return t; -} - static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, bool tracepoint) { + struct btf *btf = traceprobe_get_btf(); const struct btf_param *param; const struct btf_type *t; - if (!funcname || !nr) + if (!funcname || !nr || !btf) return ERR_PTR(-EINVAL); - t = find_btf_func_proto(funcname); - if (IS_ERR(t)) + t = btf_find_func_proto(btf, funcname); + if (IS_ERR_OR_NULL(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)) + return param; /* Hide the first 'data' argument of tracepoint */ if (tracepoint) { @@ -490,8 +466,8 @@ static const struct fetch_type *parse_btf_retval_type( const struct btf_type *t; if (btf && ctx->funcname) { - t = find_btf_func_proto(ctx->funcname); - if (!IS_ERR(t)) + t = btf_find_func_proto(btf, ctx->funcname); + if (!IS_ERR_OR_NULL(t)) typestr = type_from_btf_id(btf, t->type); } @@ -500,10 +476,14 @@ static const struct fetch_type *parse_btf_retval_type( static bool is_btf_retval_void(const char *funcname) { + struct btf *btf = traceprobe_get_btf(); const struct btf_type *t; - t = find_btf_func_proto(funcname); - if (IS_ERR(t)) + if (!btf) + return false; + + t = btf_find_func_proto(btf, funcname); + if (IS_ERR_OR_NULL(t)) return false; return t->type == 0;