Skip to content

Commit 88ab25d

Browse files
sandip4ngregkh
authored andcommitted
perf/x86/amd/uncore: Prevent UMC counters from saturating
[ Upstream commit 2492e5a ] Unlike L3 and DF counters, UMC counters (PERF_CTRs) set the Overflow bit (bit 48) and saturate on overflow. A subsequent pmu->read() of the event reports an incorrect accumulated count as there is no difference between the previous and the current values of the counter. To avoid this, inspect the current counter value and proactively reset the corresponding PERF_CTR register on every pmu->read(). Combined with the periodic reads initiated by the hrtimer, the counters never get a chance saturate but the resolution reduces to 47 bits. Fixes: 25e5684 ("perf/x86/amd/uncore: Add memory controller support") Signed-off-by: Sandipan Das <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Song Liu <[email protected]> Acked-by: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/dee9c8af2c6d66814cf4c6224529c144c620cf2c.1744906694.git.sandipan.das@amd.com Signed-off-by: Sasha Levin <[email protected]>
1 parent 1880719 commit 88ab25d

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

arch/x86/events/amd/uncore.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,39 @@ static void amd_uncore_umc_start(struct perf_event *event, int flags)
889889
perf_event_update_userpage(event);
890890
}
891891

892+
static void amd_uncore_umc_read(struct perf_event *event)
893+
{
894+
struct hw_perf_event *hwc = &event->hw;
895+
u64 prev, new, shift;
896+
s64 delta;
897+
898+
shift = COUNTER_SHIFT + 1;
899+
prev = local64_read(&hwc->prev_count);
900+
901+
/*
902+
* UMC counters do not have RDPMC assignments. Read counts directly
903+
* from the corresponding PERF_CTR.
904+
*/
905+
rdmsrl(hwc->event_base, new);
906+
907+
/*
908+
* Unlike the other uncore counters, UMC counters saturate and set the
909+
* Overflow bit (bit 48) on overflow. Since they do not roll over,
910+
* proactively reset the corresponding PERF_CTR when bit 47 is set so
911+
* that the counter never gets a chance to saturate.
912+
*/
913+
if (new & BIT_ULL(63 - COUNTER_SHIFT)) {
914+
wrmsrl(hwc->event_base, 0);
915+
local64_set(&hwc->prev_count, 0);
916+
} else {
917+
local64_set(&hwc->prev_count, new);
918+
}
919+
920+
delta = (new << shift) - (prev << shift);
921+
delta >>= shift;
922+
local64_add(delta, &event->count);
923+
}
924+
892925
static
893926
void amd_uncore_umc_ctx_scan(struct amd_uncore *uncore, unsigned int cpu)
894927
{
@@ -966,7 +999,7 @@ int amd_uncore_umc_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
966999
.del = amd_uncore_del,
9671000
.start = amd_uncore_umc_start,
9681001
.stop = amd_uncore_stop,
969-
.read = amd_uncore_read,
1002+
.read = amd_uncore_umc_read,
9701003
.capabilities = PERF_PMU_CAP_NO_EXCLUDE | PERF_PMU_CAP_NO_INTERRUPT,
9711004
.module = THIS_MODULE,
9721005
};

0 commit comments

Comments
 (0)