Received: by 2002:a25:b323:0:0:0:0:0 with SMTP id l35csp1179173ybj; Fri, 20 Sep 2019 06:26:21 -0700 (PDT) X-Google-Smtp-Source: APXvYqw1yDpzBJoVjmhD5h1KG4cNxSWkrUQYDzr/sM2v6v2orlPWbZRbqKfm/rPtSL85F2IAsDLp X-Received: by 2002:a17:906:d797:: with SMTP id pj23mr19320440ejb.70.1568985981569; Fri, 20 Sep 2019 06:26:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568985981; cv=none; d=google.com; s=arc-20160816; b=wsrw9DC/srA/mvARofIrVdQfhzmkfOXdj0pMRv9MFMSTSl7d28l6cnDaVdIvZSkuHv RSBHbHMWeI3GVTKUGS6SXVjdN/4MWPhnCjHV8joT8c4wpG4EFbopU8FS/3y/d39DXhRy 4pJTGkamAH0yytEHy374yTtXUn2fDhmdmze3Vt61FXs9MndAaTkaDalKzFs3EsJDBbl5 FvE/P+doBFhVHjO91PB7ME1mXt0NYALHZ8XLdMYnXxTuGl9kzTXJpe3hNNZe1x97LgIK PsHK/4lpqN6VFR50wa0btdzRrz7WQ/ZZVEC7IZyFJ/BJgav6qzWCkhRS3JQPenhYSAnQ lqKQ== 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=NkR0pXt0eI0/RrEfKY/zZ3Y6V6IlpLLFV1ncmPylkP0=; b=Jjkxt45Vi11Qd9bRtjn0QLdYtkBdxhYgjoaPisg2Qyv5drTwUZtHl7Re0AwW/cUZnt 7X0N10iy+xSM7k0A/xGH3F8RsS5PiNFM6ueDm1q/1WOoTAW4ElvRV/xpUIBlZgP7qkAm dh6Dfk8M0SAxluBna9uGEWhLQvxUQBm4KATBRI/hTZfX1svEX/IK7GdDeSw1vgrWWZB7 5Z5MoMG0zkAIVGeGelT2O1x50C1v6p+aXoiUiGKLYqW/DDe8CaRUQl43uITMRwJy9X+4 Oh3JkwxOxNjIj7JWF4Q3sxWOWpzf9cJEmpNkim0UQgN4VqYBw9ZH+rgodZsA1jugiPBV 4BHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=WCa970AP; 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 g3si1000098ejk.387.2019.09.20.06.25.58; Fri, 20 Sep 2019 06:26:21 -0700 (PDT) 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=WCa970AP; 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 S2393893AbfISWSS (ORCPT + 99 others); Thu, 19 Sep 2019 18:18:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:59680 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391977AbfISWSO (ORCPT ); Thu, 19 Sep 2019 18:18:14 -0400 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 9B87721924; Thu, 19 Sep 2019 22:18:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1568931493; bh=7lJqaK5sP32QNVz9dKxIQ1V9PU1uSFT/pKqmryKuYAw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WCa970AP06HblvoYpgFMklleAVEO2sjXf4CxiCRIk8tjryG/Ba41i0Q2rolJsRXQg rzOKGcVe2OHf/3fHcFFBz6bKsy+yw/R/1n9rr2WbCWvZQbeiorgbiDSsT9/vMAtZfV EDEdt1wQy8/T8r7JY8HmAcH/PlgyEWtAUg+TD0JQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kim Phillips , "Peter Zijlstra (Intel)" , "Arnaldo Carvalho de Melo" , x86@kernel.org, Ingo Molnar , Ingo Molnar , Jiri Olsa , Thomas Gleixner , "Borislav Petkov" , Stephane Eranian , Alexander Shishkin , "Namhyung Kim" , "H. Peter Anvin" , Sasha Levin Subject: [PATCH 4.14 41/59] perf/x86/amd/ibs: Fix sample bias for dispatched micro-ops Date: Fri, 20 Sep 2019 00:03:56 +0200 Message-Id: <20190919214806.987687439@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190919214755.852282682@linuxfoundation.org> References: <20190919214755.852282682@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: Kim Phillips [ Upstream commit 0f4cd769c410e2285a4e9873a684d90423f03090 ] When counting dispatched micro-ops with cnt_ctl=1, in order to prevent sample bias, IBS hardware preloads the least significant 7 bits of current count (IbsOpCurCnt) with random values, such that, after the interrupt is handled and counting resumes, the next sample taken will be slightly perturbed. The current count bitfield is in the IBS execution control h/w register, alongside the maximum count field. Currently, the IBS driver writes that register with the maximum count, leaving zeroes to fill the current count field, thereby overwriting the random bits the hardware preloaded for itself. Fix the driver to actually retain and carry those random bits from the read of the IBS control register, through to its write, instead of overwriting the lower current count bits with zeroes. Tested with: perf record -c 100001 -e ibs_op/cnt_ctl=1/pp -a -C 0 taskset -c 0 'perf annotate' output before: 15.70 65: addsd %xmm0,%xmm1 17.30 add $0x1,%rax 15.88 cmp %rdx,%rax je 82 17.32 72: test $0x1,%al jne 7c 7.52 movapd %xmm1,%xmm0 5.90 jmp 65 8.23 7c: sqrtsd %xmm1,%xmm0 12.15 jmp 65 'perf annotate' output after: 16.63 65: addsd %xmm0,%xmm1 16.82 add $0x1,%rax 16.81 cmp %rdx,%rax je 82 16.69 72: test $0x1,%al jne 7c 8.30 movapd %xmm1,%xmm0 8.13 jmp 65 8.24 7c: sqrtsd %xmm1,%xmm0 8.39 jmp 65 Tested on Family 15h and 17h machines. Machines prior to family 10h Rev. C don't have the RDWROPCNT capability, and have the IbsOpCurCnt bitfield reserved, so this patch shouldn't affect their operation. It is unknown why commit db98c5faf8cb ("perf/x86: Implement 64-bit counter support for IBS") ignored the lower 4 bits of the IbsOpCurCnt field; the number of preloaded random bits has always been 7, AFAICT. Signed-off-by: Kim Phillips Signed-off-by: Peter Zijlstra (Intel) Cc: "Arnaldo Carvalho de Melo" Cc: Cc: Ingo Molnar Cc: Ingo Molnar Cc: Jiri Olsa Cc: Thomas Gleixner Cc: "Borislav Petkov" Cc: Stephane Eranian Cc: Alexander Shishkin Cc: "Namhyung Kim" Cc: "H. Peter Anvin" Link: https://lkml.kernel.org/r/20190826195730.30614-1-kim.phillips@amd.com Signed-off-by: Sasha Levin --- arch/x86/events/amd/ibs.c | 13 ++++++++++--- arch/x86/include/asm/perf_event.h | 12 ++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/arch/x86/events/amd/ibs.c b/arch/x86/events/amd/ibs.c index 8c51844694e2f..7a86fbc07ddc1 100644 --- a/arch/x86/events/amd/ibs.c +++ b/arch/x86/events/amd/ibs.c @@ -672,10 +672,17 @@ fail: throttle = perf_event_overflow(event, &data, ®s); out: - if (throttle) + if (throttle) { perf_ibs_stop(event, 0); - else - perf_ibs_enable_event(perf_ibs, hwc, period >> 4); + } else { + period >>= 4; + + if ((ibs_caps & IBS_CAPS_RDWROPCNT) && + (*config & IBS_OP_CNT_CTL)) + period |= *config & IBS_OP_CUR_CNT_RAND; + + perf_ibs_enable_event(perf_ibs, hwc, period); + } perf_event_update_userpage(event); diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h index 78241b736f2a0..f6c4915a863e0 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -209,16 +209,20 @@ struct x86_pmu_capability { #define IBSCTL_LVT_OFFSET_VALID (1ULL<<8) #define IBSCTL_LVT_OFFSET_MASK 0x0F -/* ibs fetch bits/masks */ +/* IBS fetch bits/masks */ #define IBS_FETCH_RAND_EN (1ULL<<57) #define IBS_FETCH_VAL (1ULL<<49) #define IBS_FETCH_ENABLE (1ULL<<48) #define IBS_FETCH_CNT 0xFFFF0000ULL #define IBS_FETCH_MAX_CNT 0x0000FFFFULL -/* ibs op bits/masks */ -/* lower 4 bits of the current count are ignored: */ -#define IBS_OP_CUR_CNT (0xFFFF0ULL<<32) +/* + * IBS op bits/masks + * The lower 7 bits of the current count are random bits + * preloaded by hardware and ignored in software + */ +#define IBS_OP_CUR_CNT (0xFFF80ULL<<32) +#define IBS_OP_CUR_CNT_RAND (0x0007FULL<<32) #define IBS_OP_CNT_CTL (1ULL<<19) #define IBS_OP_VAL (1ULL<<18) #define IBS_OP_ENABLE (1ULL<<17) -- 2.20.1