Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp537691pxb; Wed, 3 Mar 2021 09:06:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJxQvFXJZWUOq/OobIsRTrgJfY6smMY5uGxzM8iTZBK1rW4W/NpIatKyStN3Eu3U3TxvlejW X-Received: by 2002:a05:6402:b41:: with SMTP id bx1mr234591edb.69.1614791207181; Wed, 03 Mar 2021 09:06:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614791207; cv=none; d=google.com; s=arc-20160816; b=qhkND3nUDT+GzKgQsBCO+3ur7SFLfsq5xf1di2Un86nKxNI6b8KxyzKeMCDKU0MUJ0 NCFkc9F8SkqAQAOJktpe0TaeFde4t9uDlwR8IDUeyMPMp72M+kMSsa+jGEth7TUomF3j bAAbj78xenCm2zQ8VlS3sFVYPh7QQQRWjMWIroWLSxzjJMzhwoghknNte5QHplg73W0D m4QAuXrreFA+jlh6MOr6B4X8DKHZdTpyLquHJ9PRr7dvKiWKWa4KGTsCNRdb7+Mr6UUt Tls0QmXI2fBsO/N+imZZfUSr4O1q7lYhHx7KjE31YLtr2rqhaZLrzNDp6E9WjFcsba6L 2fPw== 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=wR4DHNrErrO1Fo85rdUCNjrezuTwvkC+xLIRVv968aU=; b=Zfs6llKWtYUe5p+apXwHOvoEIOVRbbivYZEQDkKGsLMnjU23wy2V+bcK6np0Gi9DaH KtVM/g1y3T8Ih93QNqP4B7zM5zrV9Kv2UT098dFE53FiEGkaXfnzxGXjauYz/8uyIi0K 48UB1VRewvpIfIP3IhEZRC4n4XBsqdV+uZChGYYXnZINoz+wM7phyr2Jk8S32JPGbzjp tX+sEkt0HpmHMYux89gtaeRf9zcWuUZQHPx+BVwnDI7+L7FDvz57R3cUIlNyjsB/CMdO vkmhyztLRcJ1xNQsyD2wqwXGVvGtu8bijxanKFnNV3IilwPxkwZK2pmeo2KWI03tIBJj RmgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aYFX4Xy2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dk18si7647195edb.178.2021.03.03.09.06.23; Wed, 03 Mar 2021 09:06:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aYFX4Xy2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348181AbhCBAmJ (ORCPT + 99 others); Mon, 1 Mar 2021 19:42:09 -0500 Received: from mail.kernel.org ([198.145.29.99]:49644 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240153AbhCASgX (ORCPT ); Mon, 1 Mar 2021 13:36:23 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id E3E9664F92; Mon, 1 Mar 2021 17:20:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614619249; bh=XD8VFxHfoZEaFY/3b+6BLe+5TYx9IDfxIZXxgQbNcuo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aYFX4Xy2Q4qg+47Iuo8nqB5gBvoHB6+Z1KgJ9RkW7LImo+LtI456tX7+clRJpw8Nq 79WY9k9fEvy3g1NIPxsqs0LmF4XzYK8ZApIA2ij8VKFBdZxbJf7I/hP9ySkSZdlIqy l9kPV9y+3IL+rIyuoeANJ3ce5CiMg2YV4v7GAJZs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Adrian Hunter , Andi Kleen , Jiri Olsa , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 5.10 386/663] perf intel-pt: Fix premature IPC Date: Mon, 1 Mar 2021 17:10:34 +0100 Message-Id: <20210301161200.964502560@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210301161141.760350206@linuxfoundation.org> References: <20210301161141.760350206@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Adrian Hunter [ Upstream commit 20aa39708a5999b7921b27482a756766272286ac ] The code assumed a change in cycle count means accurate IPC. That is not correct, for example when sampling both branches and instructions, or at a FUP packet (which is not CYC-eligible) address. Fix by using an explicit flag to indicate when IPC can be sampled. Fixes: 5b1dc0fd1da06 ("perf intel-pt: Add support for samples to contain IPC ratio") Signed-off-by: Adrian Hunter Reviewed-by: Andi Kleen Cc: Jiri Olsa Cc: linux-kernel@vger.kernel.org Link: https://lore.kernel.org/r/20210205175350.23817-3-adrian.hunter@intel.com Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- .../util/intel-pt-decoder/intel-pt-decoder.c | 11 ++++++++++- .../util/intel-pt-decoder/intel-pt-decoder.h | 1 + tools/perf/util/intel-pt.c | 16 ++++++---------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c b/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c index 91cba05827369..ef29f6b25e60a 100644 --- a/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c +++ b/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c @@ -2814,9 +2814,18 @@ const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder) } if (intel_pt_sample_time(decoder->pkt_state)) { intel_pt_update_sample_time(decoder); - if (decoder->sample_cyc) + if (decoder->sample_cyc) { decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt; + decoder->state.flags |= INTEL_PT_SAMPLE_IPC; + decoder->sample_cyc = false; + } } + /* + * When using only TSC/MTC to compute cycles, IPC can be + * sampled as soon as the cycle count changes. + */ + if (!decoder->have_cyc) + decoder->state.flags |= INTEL_PT_SAMPLE_IPC; } decoder->state.timestamp = decoder->sample_timestamp; diff --git a/tools/perf/util/intel-pt-decoder/intel-pt-decoder.h b/tools/perf/util/intel-pt-decoder/intel-pt-decoder.h index 8645fc2654811..b52937b03c8c8 100644 --- a/tools/perf/util/intel-pt-decoder/intel-pt-decoder.h +++ b/tools/perf/util/intel-pt-decoder/intel-pt-decoder.h @@ -17,6 +17,7 @@ #define INTEL_PT_ABORT_TX (1 << 1) #define INTEL_PT_ASYNC (1 << 2) #define INTEL_PT_FUP_IP (1 << 3) +#define INTEL_PT_SAMPLE_IPC (1 << 4) enum intel_pt_sample_type { INTEL_PT_BRANCH = 1 << 0, diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c index 3a0348caec7d6..710ce798a2686 100644 --- a/tools/perf/util/intel-pt.c +++ b/tools/perf/util/intel-pt.c @@ -1381,7 +1381,8 @@ static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq) sample.branch_stack = (struct branch_stack *)&dummy_bs; } - sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_br_cyc_cnt; + if (ptq->state->flags & INTEL_PT_SAMPLE_IPC) + sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_br_cyc_cnt; if (sample.cyc_cnt) { sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_br_insn_cnt; ptq->last_br_insn_cnt = ptq->ipc_insn_cnt; @@ -1431,7 +1432,8 @@ static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq) else sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt; - sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_in_cyc_cnt; + if (ptq->state->flags & INTEL_PT_SAMPLE_IPC) + sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_in_cyc_cnt; if (sample.cyc_cnt) { sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_in_insn_cnt; ptq->last_in_insn_cnt = ptq->ipc_insn_cnt; @@ -1966,14 +1968,8 @@ static int intel_pt_sample(struct intel_pt_queue *ptq) ptq->have_sample = false; - if (ptq->state->tot_cyc_cnt > ptq->ipc_cyc_cnt) { - /* - * Cycle count and instruction count only go together to create - * a valid IPC ratio when the cycle count changes. - */ - ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt; - ptq->ipc_cyc_cnt = ptq->state->tot_cyc_cnt; - } + ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt; + ptq->ipc_cyc_cnt = ptq->state->tot_cyc_cnt; /* * Do PEBS first to allow for the possibility that the PEBS timestamp -- 2.27.0