[PATCH] tracing/histograms: Simplify last_cmd_set()

Christophe JAILLET posted 1 patch 2 years, 1 month ago
kernel/trace/trace_events_hist.c | 11 ++---------
1 file changed, 2 insertions(+), 9 deletions(-)
[PATCH] tracing/histograms: Simplify last_cmd_set()
Posted by Christophe JAILLET 2 years, 1 month ago
Turn a kzalloc()+strcpy()+strncat() into an equivalent and less verbose
kasprintf().

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
 kernel/trace/trace_events_hist.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)

diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index d06938ae0717..1abc07fba1b9 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -774,23 +774,16 @@ static void last_cmd_set(struct trace_event_file *file, char *str)
 {
 	const char *system = NULL, *name = NULL;
 	struct trace_event_call *call;
-	int len;
 
 	if (!str)
 		return;
 
-	/* sizeof() contains the nul byte */
-	len = sizeof(HIST_PREFIX) + strlen(str);
 	kfree(last_cmd);
-	last_cmd = kzalloc(len, GFP_KERNEL);
+
+	last_cmd = kasprintf(GFP_KERNEL, HIST_PREFIX "%s", str);
 	if (!last_cmd)
 		return;
 
-	strcpy(last_cmd, HIST_PREFIX);
-	/* Again, sizeof() contains the nul byte */
-	len -= sizeof(HIST_PREFIX);
-	strncat(last_cmd, str, len);
-
 	if (file) {
 		call = file->event_call;
 		system = call->class->system;
-- 
2.34.1
Re: [PATCH] tracing/histograms: Simplify last_cmd_set()
Posted by Mukesh Ojha 2 years, 1 month ago

On 10/21/2023 8:12 PM, Christophe JAILLET wrote:
> Turn a kzalloc()+strcpy()+strncat() into an equivalent and less verbose
> kasprintf().
> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
>   kernel/trace/trace_events_hist.c | 11 ++---------
>   1 file changed, 2 insertions(+), 9 deletions(-)
> 
> diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
> index d06938ae0717..1abc07fba1b9 100644
> --- a/kernel/trace/trace_events_hist.c
> +++ b/kernel/trace/trace_events_hist.c
> @@ -774,23 +774,16 @@ static void last_cmd_set(struct trace_event_file *file, char *str)
>   {
>   	const char *system = NULL, *name = NULL;
>   	struct trace_event_call *call;
> -	int len;
>   
>   	if (!str)
>   		return;
>   
> -	/* sizeof() contains the nul byte */
> -	len = sizeof(HIST_PREFIX) + strlen(str);
>   	kfree(last_cmd);
> -	last_cmd = kzalloc(len, GFP_KERNEL);
> +
> +	last_cmd = kasprintf(GFP_KERNEL, HIST_PREFIX "%s", str);
>   	if (!last_cmd)
>   		return;
>   
> -	strcpy(last_cmd, HIST_PREFIX);
> -	/* Again, sizeof() contains the nul byte */
> -	len -= sizeof(HIST_PREFIX);
> -	strncat(last_cmd, str, len);

LGTM, careful optimization., Thanks.
Reviewed-by: Mukesh ojha <quic_mojha@quicinc.com>


-Mukesh

> -
>   	if (file) {
>   		call = file->event_call;
>   		system = call->class->system;