Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1917968rwd; Thu, 25 May 2023 21:46:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4DT2avu5r11V+okygj7EeGGWCPjZQFkwUnivzyZW+7Bt3TaDGrlT8MkQOliHhJq9aWaJgd X-Received: by 2002:a05:6a20:9144:b0:10c:37ed:3e88 with SMTP id x4-20020a056a20914400b0010c37ed3e88mr792714pzc.23.1685076384917; Thu, 25 May 2023 21:46:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685076384; cv=none; d=google.com; s=arc-20160816; b=wNzo0QBb0de9YlioZfdZFEIcFFqlIZiH9vpjwwqRIqLhL4HmrMO3tdiiTmx4FhK2XC nz2rIs7uCX88RD93CgQ09FcopM2wYZcNFpueyx4ZygWhkDjPyn6hBtz8m46esjheyaFr zz2nDlwqLWfsiZHkLxFNRFuy3M0+IFmtxvuQ2GbQdW3YjNF+8+miSIdBFjrxk3Ezgmus 4wgMolQGU4dj868TRmFEe49RC5znp8ZPFWtPdCwWJIYr25wENY+741XdvHrtzudFPPkR YrIFkggPmE7AjCHlHtEmSDQ0uEm5iY55vNbKNFWBWjNK6tbTWwh82cA/lgRDSvvuLSfs L7Fw== 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=vWiNZkYt/i86oq/gDF2bwYeO5ZmCkIW1KEZBa+wG5Qc=; b=d7lNR8ORAMWbYn8Tlcx4S5QE5Ty0SZgoteuxgy+Drnqk+176A+442YdwLlN5x7lB15 aUdVapnuFQf0NZ6t7wn4NlkvzGUOwUapBcT6/PqkXLewIV/5O5Nlq7HXyNTjm2LvNBFf 6tUxVVLylaK3MmuKOLeNHnr0HLfRk/LNBOPEF4A2QrtOU9BK5kZfMogCiHVKmt5UNz/j nBfvI/Fe8Cryq8veAvgq1YdUvkumULwlSA2ekINbHOic/ZCRNBYFAAZhgTDGiHAdQzpw oHstD4zc7MgABSqjyTbk7PZ612ESThr00kKX2Z+1aNyMO5VemMG/94cdQMOUxPxTpRPO IaTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=sw5Q4fZL; 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 h126-20020a636c84000000b0053b8c98d14bsi2669245pgc.859.2023.05.25.21.46.12; Thu, 25 May 2023 21:46:24 -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=sw5Q4fZL; 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 S241569AbjEZEUY (ORCPT + 99 others); Fri, 26 May 2023 00:20:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230039AbjEZEUS (ORCPT ); Fri, 26 May 2023 00:20:18 -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 80B5810CA; Thu, 25 May 2023 21:19:43 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5C58260F0A; Fri, 26 May 2023 04:19:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 460B5C433D2; Fri, 26 May 2023 04:19:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685074766; bh=BQGSP9m2Vq02ML679CHXhApXgd+2v84tBG3Bv+sT5vs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sw5Q4fZLxvlaFCHb6yQ9ML3dbZVxFlzEr4Nn5YtK5soOUYnon4Kx6Kf7zOHqPJB2T +roRpIT0EoZOwGVztXG+LuZVCfHt500GopCmj5qk1HbKOb1JF+WYoa4aqtcK+MbyGS DMoWagCxsakvFesnViqLVzeCfzvxzMgOIksX38hx/pc4eMnYUf4AOGvcvmk7GoP8Gh 6VPvsGpiRArrFKwAJ/QZHPSKePHl89Mv2vHf8Z/vYdxV4wRnQn3TpqR01hIyRynPgc rX+bn+lmATyTn5kv80IrBKf5zK/LXI2r6ZzlLf90z9YuVaYvf0UnKdJus3q4rLPJjA 8+0mgTz/zfNbA== From: "Masami Hiramatsu (Google)" To: linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Steven Rostedt , mhiramat@kernel.org, Florent Revest , Mark Rutland , Will Deacon , Mathieu Desnoyers , Martin KaFai Lau , bpf@vger.kernel.org, Bagas Sanjaya Subject: [PATCH v13 09/12] tracing/probes: Add BTF retval type support Date: Fri, 26 May 2023 12:19:22 +0800 Message-ID: <168507476195.913472.16290308831790216609.stgit@mhiramat.roam.corp.google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog In-Reply-To: <168507466597.913472.10572827237387849017.stgit@mhiramat.roam.corp.google.com> References: <168507466597.913472.10572827237387849017.stgit@mhiramat.roam.corp.google.com> 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) Check the target function has non-void retval type and set the correct fetch type if user doesn't specify it. If the function returns void, $retval is rejected as below; # echo 'f unregister_kprobes%return $retval' >> dynamic_events sh: write error: No such file or directory # cat error_log [ 37.488397] trace_fprobe: error: This function returns 'void' type Command: f unregister_kprobes%return $retval ^ Signed-off-by: Masami Hiramatsu (Google) --- Changes in v8: - Fix wrong indentation. Changes in v7: - Introduce this as a new patch. --- kernel/trace/trace_probe.c | 69 ++++++++++++++++++++++++++++++++++++++++---- kernel/trace/trace_probe.h | 1 + 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c index 7318642aceb3..dfe3e1823eec 100644 --- a/kernel/trace/trace_probe.c +++ b/kernel/trace/trace_probe.c @@ -371,15 +371,13 @@ static const char *type_from_btf_id(struct btf *btf, s32 id) return NULL; } -static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr, - bool tracepoint) +static const struct btf_type *find_btf_func_proto(const char *funcname) { struct btf *btf = traceprobe_get_btf(); - const struct btf_param *param; const struct btf_type *t; s32 id; - if (!btf || !funcname || !nr) + if (!btf || !funcname) return ERR_PTR(-EINVAL); id = btf_find_by_name_kind(btf, funcname, BTF_KIND_FUNC); @@ -396,6 +394,22 @@ static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr if (!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) +{ + const struct btf_param *param; + const struct btf_type *t; + + if (!funcname || !nr) + return ERR_PTR(-EINVAL); + + t = find_btf_func_proto(funcname); + if (IS_ERR(t)) + return (const struct btf_param *)t; + *nr = btf_type_vlen(t); param = (const struct btf_param *)(t + 1); @@ -462,6 +476,32 @@ static const struct fetch_type *parse_btf_arg_type(int arg_idx, return find_fetch_type(typestr, ctx->flags); } +static const struct fetch_type *parse_btf_retval_type( + struct traceprobe_parse_context *ctx) +{ + struct btf *btf = traceprobe_get_btf(); + const char *typestr = NULL; + const struct btf_type *t; + + if (btf && ctx->funcname) { + t = find_btf_func_proto(ctx->funcname); + if (!IS_ERR(t)) + typestr = type_from_btf_id(btf, t->type); + } + + return find_fetch_type(typestr, ctx->flags); +} + +static bool is_btf_retval_void(const char *funcname) +{ + const struct btf_type *t; + + t = find_btf_func_proto(funcname); + if (IS_ERR(t)) + return false; + + return t->type == 0; +} #else static struct btf *traceprobe_get_btf(void) { @@ -480,8 +520,15 @@ static int parse_btf_arg(const char *varname, struct fetch_insn *code, trace_probe_log_err(ctx->offset, NOSUP_BTFARG); return -EOPNOTSUPP; } + #define parse_btf_arg_type(idx, ctx) \ find_fetch_type(NULL, ctx->flags) + +#define parse_btf_retval_type(ctx) \ + find_fetch_type(NULL, ctx->flags) + +#define is_btf_retval_void(funcname) (false) + #endif #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) @@ -512,6 +559,11 @@ static int parse_probe_vars(char *arg, const struct fetch_type *t, if (strcmp(arg, "retval") == 0) { if (ctx->flags & TPARG_FL_RETURN) { + if ((ctx->flags & TPARG_FL_KERNEL) && + is_btf_retval_void(ctx->funcname)) { + err = TP_ERR_NO_RETVAL; + goto inval; + } code->op = FETCH_OP_RETVAL; return 0; } @@ -912,9 +964,12 @@ static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, goto fail; /* Update storing type if BTF is available */ - if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) && - !t && code->op == FETCH_OP_ARG) - parg->type = parse_btf_arg_type(code->param, ctx); + if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) && !t) { + if (code->op == FETCH_OP_ARG) + parg->type = parse_btf_arg_type(code->param, ctx); + else if (code->op == FETCH_OP_RETVAL) + parg->type = parse_btf_retval_type(ctx); + } ret = -EINVAL; /* Store operation */ diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h index c864e6dea10f..eb43bea5c168 100644 --- a/kernel/trace/trace_probe.h +++ b/kernel/trace/trace_probe.h @@ -449,6 +449,7 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call, C(BAD_EVENT_NAME, "Event name must follow the same rules as C identifiers"), \ C(EVENT_EXIST, "Given group/event name is already used by another event"), \ C(RETVAL_ON_PROBE, "$retval is not available on probe"), \ + C(NO_RETVAL, "This function returns 'void' type"), \ C(BAD_STACK_NUM, "Invalid stack number"), \ C(BAD_ARG_NUM, "Invalid argument number"), \ C(BAD_VAR, "Invalid $-valiable specified"), \