Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp5979706rwp; Mon, 17 Jul 2023 12:40:35 -0700 (PDT) X-Google-Smtp-Source: APBJJlEpUAKVtDsw39UMLKRVwv0AEFBc/j1o95tV5AKnCWdvbXzQ1R1HfWfGRKdeIMjomtEijV4s X-Received: by 2002:a17:906:9d05:b0:991:c67d:87a2 with SMTP id fn5-20020a1709069d0500b00991c67d87a2mr10266534ejc.28.1689622835141; Mon, 17 Jul 2023 12:40:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689622835; cv=none; d=google.com; s=arc-20160816; b=uRgNe1tO1xQdjFMGWs/o5/PYmE6Oh1r5xAMnzx7aVgmveMpw7wYAO5vJjXiWquDckP b9DOJxdd1ShVJooGKdR9oYD8CMv5m/vbe1vBoShfIaMjJIcMERV5LwsdH0q8V0ih0uiR KESesMEnRqj3/Zi9L2IuAnEiJ4FaIZH+xSg5pnXMu63rrENfwDCXX6Ejq1h9a8+kyvkr AV5svOHRjZTN0C64c+gXvmtyhgQ7dsUj3LCQBGZ6V+/l9aGv48bqCrCdDAcX3TeNYYAY kWVP90R5iiHvr2gyuNYRVCv3WPDd4HT0t+ZNo+KRs53erUlU1oLxZR55rP7Cwy05s5yq ELOg== 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 :references:in-reply-to:message-id:subject:cc:to:from:date; bh=SGI18yPJeby0c57BbZgnoEcESiDi9XtoXZegJB4kyYA=; fh=KmucHq6cDCcSBQaLcvPKgtQl3Vh6KE7V4fFYihBHTZ0=; b=mUIBdml/vqjoszF5IKyKKh8QZwfkz2fdtwP+LSC0of1cUQw3t3mlPpCn2+CoBUzW7e QGPn22UEhSJ1Q8OSHXUvUtSaRTWfqT/Gg+fmCn7HkOiDdWu0HbkIx8KgDj5NBVOD1GZC 3wiNcMLazUL+Qwry7mey/gFbRA1yZM//arSSUAsOwlcfMDEL+uOtLwgIRUA8nIGzB4qB h1bEOEjNPiykmetmKQhl7zev8/46z8GiYS4857EXCvbe9kDCDUxlwYwmNnkYSEOsoTTp bx+fAIJfk2IdREu59VR96L7UkSz1FedS2uAaqr+yegNA24CAo4MrHyncu5DfPtyo/cpl Rlyw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e21-20020a170906045500b0099364d9f0e3si72010eja.562.2023.07.17.12.40.10; Mon, 17 Jul 2023 12:40:35 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230518AbjGQSjY (ORCPT + 99 others); Mon, 17 Jul 2023 14:39:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230413AbjGQSjS (ORCPT ); Mon, 17 Jul 2023 14:39:18 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91B4BA1; Mon, 17 Jul 2023 11:39:17 -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 2C8A7611EB; Mon, 17 Jul 2023 18:39:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7012C433C7; Mon, 17 Jul 2023 18:39:15 +0000 (UTC) Date: Mon, 17 Jul 2023 14:39:14 -0400 From: Steven Rostedt To: "Masami Hiramatsu (Google)" Cc: linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, Martin KaFai Lau , bpf@vger.kernel.org, Sven Schnelle , Alexei Starovoitov Subject: Re: [PATCH v2 2/9] bpf/btf: tracing: Move finding func-proto API and getting func-param API to BTF Message-ID: <20230717143914.5399a8e4@gandalf.local.home> In-Reply-To: <168960741686.34107.6330273416064011062.stgit@devnote2> References: <168960739768.34107.15145201749042174448.stgit@devnote2> <168960741686.34107.6330273416064011062.stgit@devnote2> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,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 On Tue, 18 Jul 2023 00:23:37 +0900 "Masami Hiramatsu (Google)" wrote: > 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. "different features." > > Signed-off-by: Masami Hiramatsu (Google) > --- > 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;