Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3124848pxk; Mon, 5 Oct 2020 01:27:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyy/gIdCkAGuhbMd7B3XciaH3REULQBo8pix0VEjmnYgkP3H6XbtxG3Kxt83Hgwm9NI5s/5 X-Received: by 2002:a50:fe82:: with SMTP id d2mr15477598edt.86.1601886475986; Mon, 05 Oct 2020 01:27:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601886475; cv=none; d=google.com; s=arc-20160816; b=Nd5Hf5eUjmTiTlzoO2oVkW08ntJ9QrUoj5Bpb6+52vZcNk2CSSeLCADW7LTDf2XCwa mXtnJNKU1q6Ar1oPdwb613lPi49N1UK02VPEdGZIZHpmtYEWqje50GhsNi8K6e2f2VHK l84WyjPFwYH4D5N1TVwov8HUct8NicJAV/85KGXneAcF8z06tevtuhOhyVzX8QhYSzlf +ICdYrM/fl3eoF+MNShFeCphvirKSfQp7NKsX+b116aRGka1RRWyV17j61Tfm0xHdflI qaVATWUejkE+Uo7ikbr4PjeYGQ/ciJtk/jlmHGGdEUbSwauyoMWo2ubeghmWstOrkp/x 8Fcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=d+YxHpF6mTYuxekjJW/kDThuTR3EAkaMwG8Gs0SnhyY=; b=Mso7IhOMwPrrMarUeSlTM35jyIiIGEMecfRaUpe5igWc4vvo5CxPYS/CQj6/oAlR3N 4pWcMuG9cjEa57xvsJ74s9dfkcV4LOQ2LHVVJ7rUCN5MYCv0/YBQkX0NaQckQSCXjcLG t2e96TXXg3GtRl4crTF1Z9Wrav5DpHh/GP45fNyCYQGx0wUjAlmOOjflkWYJ/vL6ffw4 m2o49VxdXbxnffjNRqaQdxXLBCnGKnUAt/5aE8TTVruGQ4DSfluOy9TMJ7MxJy4wFlOw kd/3v8oFvQzZKcEheno0EyjQl3orrZ6EtYD4Sp1gqKar2uWToETLSsTJrJydhxpquJEx TVrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=svQS837B; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l21si7727438ejg.119.2020.10.05.01.27.33; Mon, 05 Oct 2020 01:27:55 -0700 (PDT) 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=@infradead.org header.s=casper.20170209 header.b=svQS837B; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726006AbgJEI0S (ORCPT + 99 others); Mon, 5 Oct 2020 04:26:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725885AbgJEI0S (ORCPT ); Mon, 5 Oct 2020 04:26:18 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25B71C0613CE for ; Mon, 5 Oct 2020 01:26:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=d+YxHpF6mTYuxekjJW/kDThuTR3EAkaMwG8Gs0SnhyY=; b=svQS837BUTWR/aGPEWGg8PlibA CtCBhZ3/0YzVzNxyHGjr9ICSTfTNCcgRrYGG+tADK+rkPCvXwWPg7cXf2fkHjCjfl/8pNJvl4dg3h 1PQiafCIhTG4z60QmttmmZnkHoEJzpPllSF+dcgc+7TNyW127Gxl6QyRM8Skl4QoHnR+qjSH1hgll jSYtp90dHEczGxunnyVOVMnhWiXt86jPtSh0dSsJ+oC3uErTSvDHHFuYquivRoCSh+rMtHSKbc0jG KdejmTpO3OIXPVMni/l61KLiyYpIYhKpd4W6/bJPUBAXqXRo/Y+KNNw3M3vS/RBmL4VuCrHEHNy6I Qmp0zVCA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1kPLoy-00089k-7l; Mon, 05 Oct 2020 08:26:12 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id A7EC03013E5; Mon, 5 Oct 2020 10:26:11 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 6D83429E6CACF; Mon, 5 Oct 2020 10:26:11 +0200 (CEST) Date: Mon, 5 Oct 2020 10:26:11 +0200 From: Peter Zijlstra To: "Liang, Kan" Cc: mingo@redhat.com, linux-kernel@vger.kernel.org, ak@linux.intel.com, kim.phillips@amd.com Subject: [PATCH] perf/x86: Fix n_metric for cancelled txn Message-ID: <20201005082611.GH2628@hirez.programming.kicks-ass.net> References: <20200930142935.13482-1-kan.liang@linux.intel.com> <20201002110258.GV2628@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 02, 2020 at 09:16:11AM -0400, Liang, Kan wrote: > Tested-by: Kan Liang --- Subject: perf/x86: Fix n_metric for cancelled txn From: Peter Zijlstra Date: Mon Oct 5 10:10:24 CEST 2020 When a group that has TopDown members is failed to be scheduled, any later TopDown groups will not return valid values. Here is an example. A background perf that occupies all the GP counters and the fixed counter 1. $perf stat -e "{cycles,cycles,cycles,cycles,cycles,cycles,cycles, cycles,cycles}:D" -a A user monitors a TopDown group. It works well, because the fixed counter 3 and the PERF_METRICS are available. $perf stat -x, --topdown -- ./workload retiring,bad speculation,frontend bound,backend bound, 18.0,16.1,40.4,25.5, Then the user tries to monitor a group that has TopDown members. Because of the cycles event, the group is failed to be scheduled. $perf stat -x, -e '{slots,topdown-retiring,topdown-be-bound, topdown-fe-bound,topdown-bad-spec,cycles}' -- ./workload ,,slots,0,0.00,, ,,topdown-retiring,0,0.00,, ,,topdown-be-bound,0,0.00,, ,,topdown-fe-bound,0,0.00,, ,,topdown-bad-spec,0,0.00,, ,,cycles,0,0.00,, The user tries to monitor a TopDown group again. It doesn't work anymore. $perf stat -x, --topdown -- ./workload ,,,,, In a txn, cancel_txn() is to truncate the event_list for a canceled group and update the number of events added in this transaction. However, the number of TopDown events added in this transaction is not updated. The kernel will probably fail to add new Topdown events. Fixes: 7b2c05a15d29 ("perf/x86/intel: Generic support for hardware TopDown metrics") Reported-by: Andi Kleen Reported-by: Kan Liang Signed-off-by: Peter Zijlstra (Intel) Tested-by: Kan Liang --- arch/x86/events/core.c | 3 +++ arch/x86/events/perf_event.h | 1 + 2 files changed, 4 insertions(+) --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -1066,6 +1066,7 @@ static int add_nr_metric_event(struct cp if (cpuc->n_metric == INTEL_TD_METRIC_NUM) return -EINVAL; cpuc->n_metric++; + cpuc->n_txn_metric++; } return 0; @@ -2065,6 +2066,7 @@ static void x86_pmu_start_txn(struct pmu perf_pmu_disable(pmu); __this_cpu_write(cpu_hw_events.n_txn, 0); __this_cpu_write(cpu_hw_events.n_txn_pair, 0); + __this_cpu_write(cpu_hw_events.n_txn_metric, 0); } /* @@ -2091,6 +2093,7 @@ static void x86_pmu_cancel_txn(struct pm __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn)); __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn)); __this_cpu_sub(cpu_hw_events.n_pair, __this_cpu_read(cpu_hw_events.n_txn_pair)); + __this_cpu_sub(cpu_hw_events.n_metric, __this_cpu_read(cpu_hw_events.n_txn_metric)); perf_pmu_enable(pmu); } --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -236,6 +236,7 @@ struct cpu_hw_events { int n_txn; /* the # last events in the below arrays; added in the current transaction */ int n_txn_pair; + int n_txn_metric; int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */ u64 tags[X86_PMC_IDX_MAX];