Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp445312ybl; Tue, 28 Jan 2020 06:07:25 -0800 (PST) X-Google-Smtp-Source: APXvYqyb93Sf8Bwue1DKeGETt90nqCkSq96l/ikJ0eA+L3qrTxVfq/bDjfMTixsrEhjYRw3GncoW X-Received: by 2002:aca:fc0c:: with SMTP id a12mr2769989oii.118.1580220445620; Tue, 28 Jan 2020 06:07:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580220445; cv=none; d=google.com; s=arc-20160816; b=eaI4zlJ+f4N4/VNlFTH+H5gqOAXfeKavsz50KLJcjfPJJGjFJNuqnELAcZidgBrvjQ 92YN2LIrh2F6rfADfu+EWG8eCXa2jjCbmF6w3lSlHWRjdzRDlSTYbQPL1tMgO1llBJtG zcWlMdJ3D3xhh+BiKtGHJax5MQK9LStg+4hz8bR22GaSjgRDgsEarJFRVAywpAkZAyaE t8G2bK6uzCArwLALtyMP6cPOuxVYJk7a5YciTQGr1vI+DR2XVHsqlbnn1eCE72vwKDZH sXQ7hAGlkuo7a2eh7W9sUFAJaLEgymh13rpWWIwMWFXFg8VUY68fs8CzoMgjH98moM8/ US1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=HEho2celac/5E71AKaisYXw4QYhvmZqgMdgDJk67Wi4=; b=iX7OvD7tRAEuCqT8VKUT3HEaEJlVjuWuWcj/tdn5YkkSldEPqNgzbnTaUgBsKaPJ+/ Adp7UyhK/bPVsz0e+iNzct14LVMA8omVl6E6noGKzEe8zIVUUavp5dxp4OOwyWs2N1C7 4HpxjcH2O/xuxUE5l8DtSJ6Fky7U/JnZeCHlS9eAuq0hR8LZJBwyqsZzL5qtO2/dnfP3 5ilSjYBQk3D6sbOLVZrBQys7fqG10j8+GHeNqlAT3X2xvjg1JY1N8+VS93hMz9uVANGj mAg6ahC2SqM4/SkE4UiCW5E6HAM4pDvxjLuZUUXLyrlmcmS3jHX88D/kMRPM1uM6IrfQ Speg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=sIZPrjKQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u1si1905782otg.113.2020.01.28.06.07.05; Tue, 28 Jan 2020 06:07:25 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=sIZPrjKQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728092AbgA1OEa (ORCPT + 99 others); Tue, 28 Jan 2020 09:04:30 -0500 Received: from mail.kernel.org ([198.145.29.99]:51606 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726836AbgA1OE0 (ORCPT ); Tue, 28 Jan 2020 09:04:26 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 727A624690; Tue, 28 Jan 2020 14:04:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1580220264; bh=Xg+MncbSQ7xSIR3VkMqzdI8dxYAyz+HZo+mfxNYbx04=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sIZPrjKQmuLHoK6BqzsPpS/qYp65YHOGg9GccRGiGrkBEazaGKaZmiqiXtbHiCztl +2dksp33lud0xNgPA/TXpw3NUzFrPr0fv/jgp2s7yg4d7yzV9tCiuVQCQz6JPZq59a O7gyKbDTSMBvQeZoECuzBAIWgXiEIX65XbB4LU6A= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tom Zanuss , "Steven Rostedt (VMware)" Subject: [PATCH 5.4 045/104] tracing: Fix histogram code when expression has same var as value Date: Tue, 28 Jan 2020 15:00:06 +0100 Message-Id: <20200128135823.945269940@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200128135817.238524998@linuxfoundation.org> References: <20200128135817.238524998@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Steven Rostedt (VMware) commit 8bcebc77e85f3d7536f96845a0fe94b1dddb6af0 upstream. While working on a tool to convert SQL syntex into the histogram language of the kernel, I discovered the following bug: # echo 'first u64 start_time u64 end_time pid_t pid u64 delta' >> synthetic_events # echo 'hist:keys=pid:start=common_timestamp' > events/sched/sched_waking/trigger # echo 'hist:keys=next_pid:delta=common_timestamp-$start,start2=$start:onmatch(sched.sched_waking).trace(first,$start2,common_timestamp,next_pid,$delta)' > events/sched/sched_switch/trigger Would not display any histograms in the sched_switch histogram side. But if I were to swap the location of "delta=common_timestamp-$start" with "start2=$start" Such that the last line had: # echo 'hist:keys=next_pid:start2=$start,delta=common_timestamp-$start:onmatch(sched.sched_waking).trace(first,$start2,common_timestamp,next_pid,$delta)' > events/sched/sched_switch/trigger The histogram works as expected. What I found out is that the expressions clear out the value once it is resolved. As the variables are resolved in the order listed, when processing: delta=common_timestamp-$start The $start is cleared. When it gets to "start2=$start", it errors out with "unresolved symbol" (which is silent as this happens at the location of the trace), and the histogram is dropped. When processing the histogram for variable references, instead of adding a new reference for a variable used twice, use the same reference. That way, not only is it more efficient, but the order will no longer matter in processing of the variables. >From Tom Zanussi: "Just to clarify some more about what the problem was is that without your patch, we would have two separate references to the same variable, and during resolve_var_refs(), they'd both want to be resolved separately, so in this case, since the first reference to start wasn't part of an expression, it wouldn't get the read-once flag set, so would be read normally, and then the second reference would do the read-once read and also be read but using read-once. So everything worked and you didn't see a problem: from: start2=$start,delta=common_timestamp-$start In the second case, when you switched them around, the first reference would be resolved by doing the read-once, and following that the second reference would try to resolve and see that the variable had already been read, so failed as unset, which caused it to short-circuit out and not do the trigger action to generate the synthetic event: to: delta=common_timestamp-$start,start2=$start With your patch, we only have the single resolution which happens correctly the one time it's resolved, so this can't happen." Link: https://lore.kernel.org/r/20200116154216.58ca08eb@gandalf.local.home Cc: stable@vger.kernel.org Fixes: 067fe038e70f6 ("tracing: Add variable reference handling to hist triggers") Reviewed-by: Tom Zanuss Tested-by: Tom Zanussi Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Greg Kroah-Hartman --- kernel/trace/trace_events_hist.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -116,6 +116,7 @@ struct hist_field { struct ftrace_event_field *field; unsigned long flags; hist_field_fn_t fn; + unsigned int ref; unsigned int size; unsigned int offset; unsigned int is_signed; @@ -2427,8 +2428,16 @@ static int contains_operator(char *str) return field_op; } +static void get_hist_field(struct hist_field *hist_field) +{ + hist_field->ref++; +} + static void __destroy_hist_field(struct hist_field *hist_field) { + if (--hist_field->ref > 1) + return; + kfree(hist_field->var.name); kfree(hist_field->name); kfree(hist_field->type); @@ -2470,6 +2479,8 @@ static struct hist_field *create_hist_fi if (!hist_field) return NULL; + hist_field->ref = 1; + hist_field->hist_data = hist_data; if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS) @@ -2665,6 +2676,17 @@ static struct hist_field *create_var_ref { unsigned long flags = HIST_FIELD_FL_VAR_REF; struct hist_field *ref_field; + int i; + + /* Check if the variable already exists */ + for (i = 0; i < hist_data->n_var_refs; i++) { + ref_field = hist_data->var_refs[i]; + if (ref_field->var.idx == var_field->var.idx && + ref_field->var.hist_data == var_field->hist_data) { + get_hist_field(ref_field); + return ref_field; + } + } ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL); if (ref_field) {