From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.21 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796171284; b=SRLFdmpLwP8VLCkcbkGOQq7GtBsI/nksg3oI7xjF7oAt1ILntQwAC7nMK1c17wW4bu8v k+RSHczA4yBgBK3h83BLApsZpB/FVwB3zxLSpPrXYI8v/sd9KcYG0fwQYTnTuh7HbY3/G 8ERL0qhWoOpznCPOj4XB9P9o6PmxAhE4P9rek8kP7927fhUt34w4OsdJJRetkyim19snQ /Nim+fv0gfFuXSCKt/MNf/tRfraLzqRNw1whF5+ITwxmdKOES2vMpWx1CX9WYsMMt4EE6 Uv4rc4lj14P0NsK/5gXFp/QKOFMqYEICqsuTnkz5m2Y+M9kMBw5ZWrclWjDji+yxqCA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796171284; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Message-ID:Date: MIME-Version:User-Agent:Subject:To:Cc:References:Content-Language: From:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=j8Ch+Pbo9NcVqUB8YkX6LybBrRb53F69RVBsVMIAb/g=; b=GVZFdTuMRPS61xjODeR7QH2/5sq4datsATqDPWGNTNq3KCoqjzp1MODZgXUdVPlMODlA LRQh28CGNfcwtzQbAYSmBm18aCGWDM7o+B3FWPGEqbnTLo+Yhgsvj0joiCSisjrp647wC WycwEyy+aPGaHWS6uTMEOwyNvRBZSnEW5NikiMns0DwSbix4MHaTmJmcxqUDw7c0tTgXa b8zydgG0M+da2WnOTaEXcDlLKix1BFvEI79uPcrXQwDZbfJ9fqlmqWj3tEdw4ed4Zbkjh lMIL3VyENI4VqxlYK2vA2P4kzUAch0uROPDhLPtsROykhyeFsxP+t9BeWl9yWly2JBw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=ALC51Dni; arc=none smtp.remote-ip=198.175.65.21 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2F52606A31A; Wed, 2 Dec 2026 00:28:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D2F52606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=ALC51Dni DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796171284; x=1827707284; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=j8Ch+Pbo9NcVqUB8YkX6LybBrRb53F69RVBsVMIAb/g=; b=ALC51DniMn65OQrSYEBiYjYduHljWjm+wKquVQBlxIc+m8SAIttrklgO rdxay43uvuFA1bb0BcWAFg0+gnopN5lSvMvLNMiA0alT/dGLdDHVmhOhn NOnuvRUVWR6rbrgiNwAAGcC00SQmF50YyawxRxCvbiP2lZ6e/7CcUaePy RUTz4Ys4SVxGluLFoulndtFHoxuPvIGXEd5lepZgzrt/yKGL8nWj+tDHm 5hx2hqEvUZCfezom0ETwbtxGUA3xpwKbiCQ+TXPjSdNISjqgnssnQy241 dSsHrlDCk/hJRFBpHdN++0Gap62/Hff8Z614XOCTegKWvhb8ptDAEnzC9 w==; X-CSE-ConnectionGUID: e9YgHpaSTwKMCH1foLJFcA== X-CSE-MsgGUID: kyAlmJdtTQGa54L1s6k8VQ== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="5176218" X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="5176218" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 16:27:24 -0800 X-CSE-ConnectionGUID: HsZvOsXMRTupFo8gmRgUqA== X-CSE-MsgGUID: EFWxPd7SRYmgUiclkjjFUA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="13602633" Received: from dapengmi-mobl1.ccr.corp.intel.com (HELO [10.124.241.239]) ([10.124.241.239]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 16:27:20 -0800 Message-ID: <5ec75901-7128-44f4-bc3f-0c4bba677dc8@linux.intel.com> Date: Wed, 2 Dec 2026 08:27:17 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v10 18/18] KVM: selftests: Add Topdown PERF_METRICS test To: "Chen, Zide" , Sean Christopherson , Paolo Bonzini , Peter Zijlstra Cc: kvm@vger.kernel.org, Jim Mattson , Yosry Ahmed , Andi Kleen , Stephane Eranian , linux-kernel@vger.kernel.org, Mingwei Zhang , Das Sandipan , Shukla Manali , Xudong Hao References: <20261124213901.120572-1-zide.chen@intel.com> <20261124213901.120572-19-zide.chen@intel.com> <52cf1381-d759-490e-aff1-b824372a77fe@linux.intel.com> Content-Language: en-US From: "Mi, Dapeng" In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 12/2/2026 2:21 AM, Chen, Zide wrote: > > On 12/1/2026 3:35 AM, Mi, Dapeng wrote: > >>> diff --git a/tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c b/tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c >>> new file mode 100644 >>> index 000000000000..07a6c948e4cd >>> --- /dev/null >>> +++ b/tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c >>> @@ -0,0 +1,391 @@ >>> +// SPDX-License-Identifier: GPL-2.0-only >>> +/* >>> + * Test for KVM Topdown Microarchitecture Analysis (TMA) and PERF_METRICS. >>> + * >>> + * Copyright (C) 2026, Intel Corporation >>> + */ >>> + >>> +#include >>> + >>> +#include "processor.h" >>> +#include "pmu.h" >>> + >>> +/* Number of iterations of the loop for the guest measurement payload. */ >>> +#define NUM_LOOPS 10 >>> +#define PERF_METRICS_TEST_VAL 0x12345678ULL >> The sum of 4 metric fields is 0x114, which is far away from 0xff. Could we >> change it to an available value? > Nice catch! > > While WRMSR does not enforce the arithmetic sum of the Level-1 > metric fields (only validating reserved bits), using an architecturally > valid value whose Level-1 metrics sum to 0xff avoids confusion. It's true that the PERF_METRICS MSR allows to write any data, but the invalid starting data could lead to the subsequent calculation is corrupted. > > I will update it to 0x12345663ULL (0x12 + 0x34 + 0x56 + 0x63 == 0xff). > >>> +#define SLOTS_TEST_VAL 0x12345ULL >>> + >>> +static void guest_test_rdpmc(u32 rdpmc_idx, bool expect_success, u64 expected_val) >>> +{ >>> + u8 vector; >>> + u64 val; >>> + >>> + vector = rdpmc_safe(rdpmc_idx, &val); >>> + __GUEST_ASSERT(expect_success ? !vector : vector == GP_VECTOR, >>> + "Expected %s on RDPMC(0x%x), got %s", >>> + expect_success ? "no fault" : "#GP", rdpmc_idx, ex_str(vector)); >>> + if (expect_success) >>> + GUEST_ASSERT_EQ(val, expected_val); >>> + >>> + if (!is_forced_emulation_enabled) >>> + return; >>> + >>> + vector = rdpmc_safe_fep(rdpmc_idx, &val); >>> + __GUEST_ASSERT(expect_success ? !vector : vector == GP_VECTOR, >>> + "Expected %s on FEP RDPMC(0x%x), got %s", >>> + expect_success ? "no fault" : "#GP", rdpmc_idx, ex_str(vector)); >>> + if (expect_success) >>> + GUEST_ASSERT_EQ(val, expected_val); >>> +} >> There is a similar guest_test_rdpmc() helper in pmu_counters_test.c. Better >> move the helper into a common header and then avoid the duplication. > guest_test_rdpmc() is just ~15 lines of test assertion glue. Moving it > into pmu.h requires exporting ad-hoc assertion macros like > GUEST_ASSERT_PMC_MSR_ACCESS and GUEST_ASSERT_PMC_VALUE, and it encodes > test-specific expectations (#GP vs exact match) into an architectural > header. > > I guess in selftests, a little duplicated assertion is often preferred > over polluting common headers. This is unlike GUEST_MEASURE_EVENT(), > which is a complex inline assembly primitive where duplication would be > error-prone and a maintenance burden. > > That said, if you still feel strongly about deduplicating it, I am open > to moving it. guest_test_rdpmc() is a general function and could be used again in the new test cases. I suppose we'd better move it into a common header. Thanks. >> Others look good to me. Thanks.