All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Kim Phillips <kim.phillips@amd.com>,
	"Peter Zijlstra (Intel)" <peterz@infradead.org>,
	"Arnaldo Carvalho de Melo" <acme@kernel.org>,
	x86@kernel.org, Ingo Molnar <mingo@kernel.org>,
	Ingo Molnar <mingo@redhat.com>, Jiri Olsa <jolsa@redhat.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	"Borislav Petkov" <bp@alien8.de>,
	Stephane Eranian <eranian@google.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	"Namhyung Kim" <namhyung@kernel.org>,
	"H. Peter Anvin" <hpa@zytor.com>, Sasha Levin <sashal@kernel.org>
Subject: [PATCH 4.19 60/79] perf/x86/amd/ibs: Fix sample bias for dispatched micro-ops
Date: Fri, 20 Sep 2019 00:03:45 +0200	[thread overview]
Message-ID: <20190919214812.890617076@linuxfoundation.org> (raw)
In-Reply-To: <20190919214807.612593061@linuxfoundation.org>

From: Kim Phillips <kim.phillips@amd.com>

[ 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 <workload>

'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 <kim.phillips@amd.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: "Arnaldo Carvalho de Melo" <acme@kernel.org>
Cc: <x86@kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: "Borislav Petkov" <bp@alien8.de>
Cc: Stephane Eranian <eranian@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: "Namhyung Kim" <namhyung@kernel.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Link: https://lkml.kernel.org/r/20190826195730.30614-1-kim.phillips@amd.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 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 d50bb4dc06503..80c6d84cad67b 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, &regs);
 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




  parent reply	other threads:[~2019-09-19 22:14 UTC|newest]

Thread overview: 93+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-19 22:02 [PATCH 4.19 00/79] 4.19.75-stable review Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 01/79] netfilter: nf_flow_table: set default timeout after successful insertion Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 02/79] HID: wacom: generic: read HID_DG_CONTACTMAX from any feature report Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 03/79] RDMA/restrack: Release task struct which was hold by CM_ID object Greg Kroah-Hartman
2019-09-21 20:22   ` Pavel Machek
2019-09-22  8:30     ` Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 04/79] Input: elan_i2c - remove Lenovo Legion Y7000 PnpID Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 05/79] powerpc/mm/radix: Use the right page size for vmemmap mapping Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 06/79] USB: usbcore: Fix slab-out-of-bounds bug during device reset Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 07/79] media: tm6000: double free if usb disconnect while streaming Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 08/79] phy: renesas: rcar-gen3-usb2: Disable clearing VBUS in over-current Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 09/79] ip6_gre: fix a dst leak in ip6erspan_tunnel_xmit Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 10/79] udp: correct reuseport selection with connected sockets Greg Kroah-Hartman
2019-09-21 20:32   ` Pavel Machek
2019-09-19 22:02 ` [PATCH 4.19 11/79] xen-netfront: do not assume sk_buff_head list is empty in error handling Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 12/79] net_sched: let qdisc_put() accept NULL pointer Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 13/79] KVM: coalesced_mmio: add bounds checking Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 14/79] firmware: google: check if size is valid when decoding VPD data Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 15/79] serial: sprd: correct the wrong sequence of arguments Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 16/79] tty/serial: atmel: reschedule TX after RX was started Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 17/79] mwifiex: Fix three heap overflow at parsing element in cfg80211_ap_settings Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 18/79] nl80211: Fix possible Spectre-v1 for CQM RSSI thresholds Greg Kroah-Hartman
2019-09-20 11:28   ` Pavel Machek
2019-09-19 22:03 ` [PATCH 4.19 19/79] ieee802154: hwsim: Fix error handle path in hwsim_init_module Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 20/79] ieee802154: hwsim: unregister hw while hwsim_subscribe_all_others fails Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 21/79] ARM: dts: am57xx: Disable voltage switching for SD card Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 22/79] ARM: OMAP2+: Fix missing SYSC_HAS_RESET_STATUS for dra7 epwmss Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 23/79] bus: ti-sysc: Fix using configured sysc mask value Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 24/79] s390/bpf: fix lcgr instruction encoding Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 25/79] ARM: OMAP2+: Fix omap4 errata warning on other SoCs Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 26/79] ARM: dts: dra74x: Fix iodelay configuration for mmc3 Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 27/79] ARM: OMAP1: ams-delta-fiq: Fix missing irq_ack Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 28/79] bus: ti-sysc: Simplify cleanup upon failures in sysc_probe() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 29/79] s390/bpf: use 32-bit index for tail calls Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 30/79] selftests/bpf: fix "bind{4, 6} deny specific IP & port" on s390 Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 31/79] tools: bpftool: close prog FD before exit on showing a single program Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 32/79] fpga: altera-ps-spi: Fix getting of optional confd gpio Greg Kroah-Hartman
2019-09-21 20:46   ` Pavel Machek
2019-09-23  1:33     ` Phil Reid
2019-09-19 22:03 ` [PATCH 4.19 33/79] netfilter: ebtables: Fix argument order to ADD_COUNTER Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 34/79] netfilter: nft_flow_offload: missing netlink attribute policy Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 35/79] netfilter: xt_nfacct: Fix alignment mismatch in xt_nfacct_match_info Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 36/79] NFSv4: Fix return values for nfs4_file_open() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 37/79] NFSv4: Fix return value in nfs_finish_open() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 38/79] NFS: Fix initialisation of I/O result struct in nfs_pgio_rpcsetup Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 39/79] Kconfig: Fix the reference to the IDT77105 Phy driver in the description of ATM_NICSTAR_USE_IDT77105 Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 40/79] xdp: unpin xdp umem pages in error path Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 41/79] qed: Add cleanup in qed_slowpath_start() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 42/79] ARM: 8874/1: mm: only adjust sections of valid mm structures Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 43/79] batman-adv: Only read OGM2 tvlv_len after buffer len check Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 44/79] bpf: allow narrow loads of some sk_reuseport_md fields with offset > 0 Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 45/79] r8152: Set memory to all 0xFFs on failed reg reads Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 46/79] x86/apic: Fix arch_dynirq_lower_bound() bug for DT enabled machines Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 47/79] netfilter: xt_physdev: Fix spurious error message in physdev_mt_check Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 48/79] netfilter: nf_conntrack_ftp: Fix debug output Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 49/79] NFSv2: Fix eof handling Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 50/79] NFSv2: Fix write regression Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 51/79] kallsyms: Dont let kallsyms_lookup_size_offset() fail on retrieving the first symbol Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 52/79] cifs: set domainName when a domain-key is used in multiuser Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 53/79] cifs: Use kzfree() to zero out the password Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 54/79] usb: host: xhci-tegra: Set DMA mask correctly Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 55/79] ARM: 8901/1: add a criteria for pfn_valid of arm Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 56/79] ibmvnic: Do not process reset during or after device removal Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 57/79] sky2: Disable MSI on yet another ASUS boards (P6Xxxx) Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 58/79] i2c: designware: Synchronize IRQs when unregistering slave client Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 59/79] perf/x86/intel: Restrict period on Nehalem Greg Kroah-Hartman
2019-09-19 22:03 ` Greg Kroah-Hartman [this message]
2019-09-19 22:03 ` [PATCH 4.19 61/79] amd-xgbe: Fix error path in xgbe_mod_init() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 62/79] tools/power x86_energy_perf_policy: Fix "uninitialized variable" warnings at -O2 Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 63/79] tools/power x86_energy_perf_policy: Fix argument parsing Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 64/79] tools/power turbostat: fix buffer overrun Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 65/79] net: aquantia: fix out of memory condition on rx side Greg Kroah-Hartman
2019-09-20 11:20   ` Pavel Machek
2019-09-19 22:03 ` [PATCH 4.19 66/79] net: seeq: Fix the function used to release some memory in an error handling path Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 67/79] dmaengine: ti: dma-crossbar: Fix a memory leak bug Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 68/79] dmaengine: ti: omap-dma: Add cleanup in omap_dma_probe() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 69/79] x86/uaccess: Dont leak the AC flags into __get_user() argument evaluation Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 70/79] x86/hyper-v: Fix overflow bug in fill_gva_list() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 71/79] keys: Fix missing null pointer check in request_key_auth_describe() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 72/79] iommu/amd: Flush old domains in kdump kernel Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 73/79] iommu/amd: Fix race in increase_address_space() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 74/79] PCI: kirin: Fix section mismatch warning Greg Kroah-Hartman
2019-09-19 22:04 ` [PATCH 4.19 75/79] ovl: fix regression caused by overlapping layers detection Greg Kroah-Hartman
2019-09-19 22:04 ` [PATCH 4.19 76/79] floppy: fix usercopy direction Greg Kroah-Hartman
2019-09-19 22:04 ` [PATCH 4.19 77/79] binfmt_elf: move brk out of mmap when doing direct loader exec Greg Kroah-Hartman
2019-09-19 22:04 ` [PATCH 4.19 78/79] arm64: kpti: Whitelist Cortex-A CPUs that dont implement the CSV3 field Greg Kroah-Hartman
2019-09-19 22:04 ` [PATCH 4.19 79/79] media: technisat-usb2: break out of loop at end of buffer Greg Kroah-Hartman
2019-09-20  3:39 ` [PATCH 4.19 00/79] 4.19.75-stable review kernelci.org bot
2019-09-20  8:51 ` Naresh Kamboju
2019-09-20 13:48 ` Jon Hunter
2019-09-20 13:48   ` Jon Hunter
2019-09-20 18:37 ` Guenter Roeck
2019-09-20 21:21 ` shuah

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190919214812.890617076@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=eranian@google.com \
    --cc=hpa@zytor.com \
    --cc=jolsa@redhat.com \
    --cc=kim.phillips@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=sashal@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.