From nobody Thu Sep 11 20:29:46 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB21EC61DA4 for ; Fri, 17 Feb 2023 00:51:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229642AbjBQAvL (ORCPT ); Thu, 16 Feb 2023 19:51:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230192AbjBQAu7 (ORCPT ); Thu, 16 Feb 2023 19:50:59 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7D7E5776A for ; Thu, 16 Feb 2023 16:50:42 -0800 (PST) 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 ams.source.kernel.org (Postfix) with ESMTPS id 72610B82ABF for ; Fri, 17 Feb 2023 00:50:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 32C6AC433AC; Fri, 17 Feb 2023 00:50:40 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.96) (envelope-from ) id 1pSoxT-000qOJ-0p; Thu, 16 Feb 2023 19:50:39 -0500 Message-ID: <20230217005039.070788344@goodmis.org> User-Agent: quilt/0.66 Date: Thu, 16 Feb 2023 19:49:58 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Andrew Morton , Tom Zanussi Subject: [for-next][PATCH 4/5] tracing/histogram: Fix stacktrace key References: <20230217004954.719324747@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Tom Zanussi The current code will always use the current stacktrace as a key even if a stacktrace contained in a specific event field was specified. For example, we expect to use the 'unsigned long[] stack' field in the below event in the histogram: # echo 's:block_lat pid_t pid; u64 delta; unsigned long[] stack;' > /sys/= kernel/debug/tracing/dynamic_events # echo 'hist:keys=3Ddelta.buckets=3D100,stack.stacktrace:sort=3Ddelta' > = /sys/kernel/debug/tracing/events/synthetic/block_lat/trigger But in fact, when we type out the trigger, we see that it's using the plain old global 'stacktrace' as the key, which is just the stacktrace when the event was hit and not the stacktrace contained in the event, which is what we want: # cat /sys/kernel/debug/tracing/events/synthetic/block_lat/trigger hist:keys=3Ddelta.buckets=3D100,stacktrace:vals=3Dhitcount:sort=3Ddelta.b= uckets=3D100:size=3D2048 [active] And in fact, there's no code to actually retrieve it from the event, so we need to add HIST_FIELD_FN_STACK and hist_field_stack() to get it and hook it into the trigger code. For now, since the stack is just using dynamic strings, this could just use the dynamic string function, but it seems cleaner to have a dedicated function an be able to tweak independently as necessary. Link: https://lkml.kernel.org/r/11aa614c82976adbfa4ea763dbe885b5fb01d59c.16= 76063532.git.zanussi@kernel.org Signed-off-by: Tom Zanussi [ Fixed 32bit build warning reported by kernel test robot ] Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace_events_hist.c | 34 +++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_h= ist.c index c4f1fe985f6f..89877a18f933 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -135,6 +135,7 @@ enum hist_field_fn { HIST_FIELD_FN_DIV_NOT_POWER2, HIST_FIELD_FN_DIV_MULT_SHIFT, HIST_FIELD_FN_EXECNAME, + HIST_FIELD_FN_STACK, }; =20 /* @@ -1982,7 +1983,10 @@ static struct hist_field *create_hist_field(struct h= ist_trigger_data *hist_data, } =20 if (flags & HIST_FIELD_FL_STACKTRACE) { - hist_field->fn_num =3D HIST_FIELD_FN_NOP; + if (field) + hist_field->fn_num =3D HIST_FIELD_FN_STACK; + else + hist_field->fn_num =3D HIST_FIELD_FN_NOP; hist_field->size =3D HIST_STACKTRACE_SIZE; hist_field->type =3D kstrdup_const("unsigned long[]", GFP_KERNEL); if (!hist_field->type) @@ -4274,6 +4278,19 @@ static u64 hist_field_execname(struct hist_field *hi= st_field, return (u64)(unsigned long)(elt_data->comm); } =20 +static u64 hist_field_stack(struct hist_field *hist_field, + struct tracing_map_elt *elt, + struct trace_buffer *buffer, + struct ring_buffer_event *rbe, + void *event) +{ + u32 str_item =3D *(u32 *)(event + hist_field->field->offset); + int str_loc =3D str_item & 0xffff; + char *addr =3D (char *)(event + str_loc); + + return (u64)(unsigned long)addr; +} + static u64 hist_fn_call(struct hist_field *hist_field, struct tracing_map_elt *elt, struct trace_buffer *buffer, @@ -4337,6 +4354,8 @@ static u64 hist_fn_call(struct hist_field *hist_field, return div_by_mult_and_shift(hist_field, elt, buffer, rbe, event); case HIST_FIELD_FN_EXECNAME: return hist_field_execname(hist_field, elt, buffer, rbe, event); + case HIST_FIELD_FN_STACK: + return hist_field_stack(hist_field, elt, buffer, rbe, event); default: return 0; } @@ -5238,8 +5257,17 @@ static void event_hist_trigger(struct event_trigger_= data *data, =20 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) { memset(entries, 0, HIST_STACKTRACE_SIZE); - stack_trace_save(entries, HIST_STACKTRACE_DEPTH, - HIST_STACKTRACE_SKIP); + if (key_field->field) { + unsigned long *stack, n_entries; + + field_contents =3D hist_fn_call(key_field, elt, buffer, rbe, rec); + stack =3D (unsigned long *)(long)field_contents; + n_entries =3D *stack; + memcpy(entries, ++stack, n_entries * sizeof(unsigned long)); + } else { + stack_trace_save(entries, HIST_STACKTRACE_DEPTH, + HIST_STACKTRACE_SKIP); + } key =3D entries; } else { field_contents =3D hist_fn_call(key_field, elt, buffer, rbe, rec); --=20 2.39.1