Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp875666ybe; Wed, 4 Sep 2019 09:02:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqwpDu6u4uOzARZpp8v8x6YwxFxK775oDu3hOqDjkMiJZQEGfgygvKR1DaVIrRzqnzFon0Pb X-Received: by 2002:a17:90a:9f4a:: with SMTP id q10mr5512994pjv.83.1567612954652; Wed, 04 Sep 2019 09:02:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567612954; cv=none; d=google.com; s=arc-20160816; b=Bt3xyqTKFLPkNV2TV5KBZS1Ru0soEpbQwRofZMTJaTlxLLq2HbmTYkzIvoIQRoSYCn rBRNhEuvOKD8RF9A3QkELl3e/vBR7pt8cWtoWYhbppYaqsyDvsjjtMMzbaxDaKJF4qdG NInS7xqIkmrxXLuQvZB/rTyL3L2pofI7c+17yF8FyRrSiKSVtJzUvWwdbaLpCMlAxrL3 td5KlYwAocrFyyXmMV9orCgihwNqibbP/MHy2ww54XxGpPCVxY04bxhTUIlAJYHmlK6K pVs3yFFj1XaZyoxK37lC8el1Zg2ycRI/NvLg322VnaonFS12vIGmaHK43rKry8PSRj5O /Tug== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2Btf9wmpijB7jROtKTEDKRlfiEiKwTnZ9D4xOVSsX9E=; b=Fr1J0zbnpB76GvRZXzy7N9/krA/1OY5BHuvbrk8i5qvLJ0fm0kPhI5yvktx+7KofiC KZnHkIsUlu2LtOLboM2qQmIVLG4BWKK/dumcy8fYKa8UIIJkxzxVsbWZ+iW3x9yNEq3P mlK4hfQkCljyWMd8g/cwC2RStOO1m1zrYyB0jhnKgHTQfL2YD1u94RjdMHdBskrYn9+q X/CDBUotylCOlttEwRwVMJLKlreHyLCc1DHApKKonKQEUrFdU1QKektCPJiKGfrCWXye 6LASmJcmfr2pG3DlKN/A61oMoW/oYdKY3RZfDXWsduC/2ky3L1/tMPDB8L3QtTUNco/T gc9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Zqpxw0bl; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k65si17286823pge.422.2019.09.04.09.02.18; Wed, 04 Sep 2019 09:02:34 -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=Zqpxw0bl; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732425AbfIDP7f (ORCPT + 99 others); Wed, 4 Sep 2019 11:59:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:34090 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731650AbfIDP7c (ORCPT ); Wed, 4 Sep 2019 11:59:32 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6543622CED; Wed, 4 Sep 2019 15:59:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567612771; bh=Z+KQzhdzjhI9w2EO9udNs4Ojp3yxUcr7VScQuI1wTeM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zqpxw0blLtMnDbmBbZOoynw9EUm9K80M59viv4Vbzb6mqfcNlpzoQHD155QTPs1jM kOzEJSjfwH1r5Z8JmtWlZD8Y8kExZfp7VQ+yGAm0y+19xs8G6J3xuFUMjEdGu5em31 lpSecPg20SCnbuZJwC5tF1AiO/b+j38eqkc5eRho= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Kim Phillips , Peter Zijlstra , "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 AUTOSEL 5.2 72/94] perf/x86/amd/ibs: Fix sample bias for dispatched micro-ops Date: Wed, 4 Sep 2019 11:57:17 -0400 Message-Id: <20190904155739.2816-72-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190904155739.2816-1-sashal@kernel.org> References: <20190904155739.2816-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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 62f317c9113af..5b35b7ea5d728 100644 --- a/arch/x86/events/amd/ibs.c +++ b/arch/x86/events/amd/ibs.c @@ -661,10 +661,17 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs) 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 1392d5e6e8d67..ee26e9215f187 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -252,16 +252,20 @@ struct pebs_lbr { #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