From patchwork Wed Jan 8 01:24:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 13929923 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26A1015B99E; Wed, 8 Jan 2025 01:24:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736299464; cv=none; b=MZCPBcUxK3K6ZVilsK30HbiG1+ub90l5a1UrpiIvJu3QT1DETKIVSFUpepFiJsJCUchpMOGsk2CW3K+3wjB9LY9a05oMO+UbCVa77lGHOBiP68xcKtps3FdMO5h84qlqe8GYHknm23H2kQtN3v+JH3Fx3PxFxJfr9tMfpu+Cq+w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736299464; c=relaxed/simple; bh=3H7oE86zBoBdo+u3xFj1t2xGAOZ0foXAprKGMF4Eoto=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ZTZDu1JE3Vepd4+z0MmajWLKihN7qfkfN0behPLxg07WPhRDeaeF9nINzK4YlzQRjxn3qfJ05H5/Q/69+HzHxlZ/56sbp8/NKsiochJRF2XwTP/t9PRbZjQjLPnolXnkFXXyT9qeNYCmLoSJYgYi7ijfOA6rv7V5QX+Xji6z4E4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TbQISRKl; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TbQISRKl" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E9DCC4CED6; Wed, 8 Jan 2025 01:24:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1736299463; bh=3H7oE86zBoBdo+u3xFj1t2xGAOZ0foXAprKGMF4Eoto=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TbQISRKlWbXgfnt44ixB842grS/PWNmGV2tc5TePJZqb2v+ZFQqOTJ0qtPTht/7hB rOmWxcPGski2bBFMUTw6spL6Uq41nmP1aUfqGsPTR9ynKNqE9XaoOI9ruxC+v3c6OT gV/nhPBODfg9KEyWnmsOVKB88MNlHC95En9eHOp6FYwRrqsH2npXFg8ylG3WhN/wcY aXsIvshDSCzaRhBrMcfFDuKoas8C3sfEQ5DcfH2KlReXeJyFgcJ8FbIXjCLPFHfYD5 PR6/uQKqIRzB31mUg5DspEvsGdlfZffc4cziWV14n6rdNode4rKMtvXOhXZ/L42u1Q FiK0vQje+L62A== From: "Masami Hiramatsu (Google)" To: Steven Rostedt , Peter Zijlstra Cc: Anil S Keshavamurthy , Masami Hiramatsu , "David S . Miller" , Mathieu Desnoyers , Oleg Nesterov , Tzvetomir Stoyanov , Naveen N Rao , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v4 2/5] tracing: Use __free() in trace_probe for cleanup Date: Wed, 8 Jan 2025 10:24:18 +0900 Message-ID: <173629945814.1450013.2614648818084217685.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173629943548.1450013.12412761424554510119.stgit@devnote2> References: <173629943548.1450013.12412761424554510119.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Masami Hiramatsu (Google) Use __free() in trace_probe to cleanup some gotos. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v4: - Run kfree() right before kstrdup() in a loop since __free(kfree) works only when exiting the loop, not each iteration. --- kernel/trace/trace_probe.c | 53 ++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c index 16a5e368e7b7..0a705add2206 100644 --- a/kernel/trace/trace_probe.c +++ b/kernel/trace/trace_probe.c @@ -1409,7 +1409,7 @@ static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, struct traceprobe_parse_context *ctx) { struct fetch_insn *code, *tmp = NULL; - char *type, *arg; + char *type, *arg __free(kfree) = NULL; int ret, len; len = strlen(argv); @@ -1426,22 +1426,16 @@ static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, return -ENOMEM; parg->comm = kstrdup(arg, GFP_KERNEL); - if (!parg->comm) { - ret = -ENOMEM; - goto out; - } + if (!parg->comm) + return -ENOMEM; type = parse_probe_arg_type(arg, parg, ctx); - if (IS_ERR(type)) { - ret = PTR_ERR(type); - goto out; - } + if (IS_ERR(type)) + return PTR_ERR(type); code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL); - if (!code) { - ret = -ENOMEM; - goto out; - } + if (!code) + return -ENOMEM; code[FETCH_INSN_MAX - 1].op = FETCH_OP_END; ctx->last_type = NULL; @@ -1497,8 +1491,6 @@ static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, kfree(code->data); } kfree(tmp); -out: - kfree(arg); return ret; } @@ -1668,7 +1660,7 @@ const char **traceprobe_expand_meta_args(int argc, const char *argv[], { const struct btf_param *params = NULL; int i, j, n, used, ret, args_idx = -1; - const char **new_argv = NULL; + const char **new_argv __free(kfree) = NULL; ret = argv_has_var_arg(argc, argv, &args_idx, ctx); if (ret < 0) @@ -1707,7 +1699,7 @@ const char **traceprobe_expand_meta_args(int argc, const char *argv[], ret = sprint_nth_btf_arg(n, "", buf + used, bufsize - used, ctx); if (ret < 0) - goto error; + return ERR_PTR(ret); new_argv[j++] = buf + used; used += ret + 1; @@ -1721,25 +1713,20 @@ const char **traceprobe_expand_meta_args(int argc, const char *argv[], n = simple_strtoul(argv[i] + 4, &type, 10); if (type && !(*type == ':' || *type == '\0')) { trace_probe_log_err(0, BAD_VAR); - ret = -ENOENT; - goto error; + return ERR_PTR(-ENOENT); } /* Note: $argN starts from $arg1 */ ret = sprint_nth_btf_arg(n - 1, type, buf + used, bufsize - used, ctx); if (ret < 0) - goto error; + return ERR_PTR(ret); new_argv[j++] = buf + used; used += ret + 1; } else new_argv[j++] = argv[i]; } - return new_argv; - -error: - kfree(new_argv); - return ERR_PTR(ret); + return_ptr(new_argv); } /* @buf: *buf must be equal to NULL. Caller must to free *buf */ @@ -1747,14 +1734,14 @@ int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf) { int i, used, ret; const int bufsize = MAX_DENTRY_ARGS_LEN; - char *tmpbuf = NULL; + char *tmpbuf __free(kfree) = NULL; if (*buf) return -EINVAL; used = 0; for (i = 0; i < argc; i++) { - char *tmp; + char *tmp __free(kfree) = NULL; char *equal; size_t arg_len; @@ -1767,9 +1754,11 @@ int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf) return -ENOMEM; } + /* Note: __free(kfree) on 'tmp' works only when exiting this loop. */ + kfree(tmp); tmp = kstrdup(argv[i], GFP_KERNEL); if (!tmp) - goto nomem; + return -ENOMEM; equal = strchr(tmp, '='); if (equal) @@ -1790,18 +1779,14 @@ int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf) offsetof(struct file, f_path.dentry), equal ? equal + 1 : tmp); - kfree(tmp); if (ret >= bufsize - used) - goto nomem; + return -ENOMEM; argv[i] = tmpbuf + used; used += ret + 1; } - *buf = tmpbuf; + *buf = no_free_ptr(tmpbuf); return 0; -nomem: - kfree(tmpbuf); - return -ENOMEM; } void traceprobe_finish_parse(struct traceprobe_parse_context *ctx)