From patchwork Thu May 30 17:47:18 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 2638471 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id AB12ADF2A1 for ; Thu, 30 May 2013 17:47:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965881Ab3E3Rrc (ORCPT ); Thu, 30 May 2013 13:47:32 -0400 Received: from mx1.redhat.com ([209.132.183.28]:29769 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965718Ab3E3Rrb (ORCPT ); Thu, 30 May 2013 13:47:31 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r4UHlUWA028286 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 30 May 2013 13:47:31 -0400 Received: from yakj.usersys.redhat.com (ovpn-112-38.ams2.redhat.com [10.36.112.38]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r4UHlSPR015422 for ; Thu, 30 May 2013 13:47:29 -0400 From: Paolo Bonzini To: kvm@vger.kernel.org Subject: [PATCH kvm-unit-tests v2] pmu: fixes for Sandy Bridge hosts Date: Thu, 30 May 2013 19:47:18 +0200 Message-Id: <1369936038-19242-1-git-send-email-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This patch includes two fixes for SB: * the 3rd fixed counter ("ref cpu cycles") can sometimes report less than the number of iterations * there is an 8th counter which causes out of bounds accesses to gp_event or check_counters_many's cnt array There is still a bug in KVM, because the "pmu all counters-0" test fails. (It passes if you use any 6 of the 8 gp counters, fails if you use 7 or 8). Signed-off-by: Paolo Bonzini --- x86/pmu.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/x86/pmu.c b/x86/pmu.c index 2c46f31..dca753a 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -88,9 +88,10 @@ struct pmu_event { }, fixed_events[] = { {"fixed 1", MSR_CORE_PERF_FIXED_CTR0, 10*N, 10.2*N}, {"fixed 2", MSR_CORE_PERF_FIXED_CTR0 + 1, 1*N, 30*N}, - {"fixed 3", MSR_CORE_PERF_FIXED_CTR0 + 2, 1*N, 30*N} + {"fixed 3", MSR_CORE_PERF_FIXED_CTR0 + 2, 0.1*N, 30*N} }; +static int num_counters; static int tests, failures; char *buf; @@ -237,7 +238,7 @@ static void check_gp_counter(struct pmu_event *evt) }; int i; - for (i = 0; i < eax.split.num_counters; i++, cnt.ctr++) { + for (i = 0; i < num_counters; i++, cnt.ctr++) { cnt.count = 0; measure(&cnt, 1); report(evt->name, i, verify_event(cnt.count, evt)); @@ -276,7 +277,7 @@ static void check_counters_many(void) pmu_counter_t cnt[10]; int i, n; - for (i = 0, n = 0; n < eax.split.num_counters; i++) { + for (i = 0, n = 0; n < num_counters; i++) { if (ebx.full & (1 << i)) continue; @@ -316,10 +317,10 @@ static void check_counter_overflow(void) /* clear status before test */ wrmsr(MSR_CORE_PERF_GLOBAL_OVF_CTRL, rdmsr(MSR_CORE_PERF_GLOBAL_STATUS)); - for (i = 0; i < eax.split.num_counters + 1; i++, cnt.ctr++) { + for (i = 0; i < num_counters + 1; i++, cnt.ctr++) { uint64_t status; int idx; - if (i == eax.split.num_counters) + if (i == num_counters) cnt.ctr = fixed_events[0].unit_sel; if (i % 2) cnt.config |= EVNTSEL_INT; @@ -355,7 +356,7 @@ static void check_rdpmc(void) uint64_t val = 0x1f3456789ull; int i; - for (i = 0; i < eax.split.num_counters; i++) { + for (i = 0; i < num_counters; i++) { uint64_t x = (val & 0xffffffff) | ((1ull << (eax.split.bit_width - 32)) - 1) << 32; wrmsr(MSR_IA32_PERFCTR0 + i, val); @@ -395,6 +396,10 @@ int main(int ac, char **av) printf("Fixed counters: %d\n", edx.split.num_counters_fixed); printf("Fixed counter width: %d\n", edx.split.bit_width_fixed); + num_counters = eax.split.num_counters; + if (num_counters > ARRAY_SIZE(gp_events)) + num_counters = ARRAY_SIZE(gp_events); + apic_write(APIC_LVTPC, PC_VECTOR); check_gp_counters();