From patchwork Fri Apr 19 03:52:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635629 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 892DEB65C; Fri, 19 Apr 2024 03:45:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498338; cv=none; b=WtCLPpBAGy39ESxkWAKpOVSVbXDQIyxxN+QcfLUP/QAor+hZxeGl3LM+vxhtdSQKQ52x6BAZtj7frHe0YLENxlEnBSacAG+c4d/U0V/TStt1x+icGfcbIEbzadALlQkzt/uRBFqmdfhPMcx89jVGP7D3dvLW+GzjeNDjhQSC2Po= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498338; c=relaxed/simple; bh=mvnhKObMsf4oRtDBDUyLyjk+9cER6IEHnWTcGVhIjQ4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Zdapvqp5HJ1QZHIUM3dKI2YU/7+L/1jPrXqYRFuqVctRGWn692soxwlqLr7kmwyF/d20WTQzQV7q0CJFL4roTRGspr7T8/rjVNYu+IPRSokKn8jBXMIFfGq3wLHlj3RgSqR4pgSJyr6iRDRrrD4PEq1zCpZvYCclyjDucEMVvYs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=DfAQN9a5; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="DfAQN9a5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498337; x=1745034337; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mvnhKObMsf4oRtDBDUyLyjk+9cER6IEHnWTcGVhIjQ4=; b=DfAQN9a50I6zmy9+pCHN/pAB5zRtg9IS4VX1dF0J8ewJmDVGudbj+8Hb txmsgc3XIZuF8SS1vVl7rBDL/zLMTD280BSEvRoB2n+Xb/6PNgYi7OLuy d4Dro8eebUWxft/4Nq65qtO1vRqpoJL1lvCYoplz9OjMQIAHgY9nVYd/X eclnAy7nPwjO2WCO45AVmMrOMm7cJ3oq3MPV1MGCEP57kUxS+2OJxi4Mf 9B+lEIZ9yhQO4hfperUTGLl/I4d4LytXzxHYL4iq1DYDKs0b860TR1Wc/ He3+4KGX7DUFB+tFYhVIFoEpz/8LLvj3O34gMnY6qsB9UzOFxJZNP3sgD w==; X-CSE-ConnectionGUID: pLD1nsigQqecdRheOF6H8w== X-CSE-MsgGUID: jnXLVH8kRJWkn4YM5HOFTQ== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565397" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565397" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:45:37 -0700 X-CSE-ConnectionGUID: Eb2gFHIwSxiZ3DyaOoO7qQ== X-CSE-MsgGUID: uyD1Lh62QJi8fxWmPqloFA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410102" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:45:34 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 01/17] x86: pmu: Remove duplicate code in pmu_init() Date: Fri, 19 Apr 2024 11:52:17 +0800 Message-Id: <20240419035233.3837621-2-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Xiong Zhang There are totally same code in pmu_init() helper, remove the duplicate code. Reviewed-by: Jim Mattson Signed-off-by: Xiong Zhang Signed-off-by: Dapeng Mi --- lib/x86/pmu.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/x86/pmu.c b/lib/x86/pmu.c index 0f2afd650bc9..d06e94553024 100644 --- a/lib/x86/pmu.c +++ b/lib/x86/pmu.c @@ -16,11 +16,6 @@ void pmu_init(void) pmu.fixed_counter_width = (cpuid_10.d >> 5) & 0xff; } - if (pmu.version > 1) { - pmu.nr_fixed_counters = cpuid_10.d & 0x1f; - pmu.fixed_counter_width = (cpuid_10.d >> 5) & 0xff; - } - pmu.nr_gp_counters = (cpuid_10.a >> 8) & 0xff; pmu.gp_counter_width = (cpuid_10.a >> 16) & 0xff; pmu.gp_counter_mask_length = (cpuid_10.a >> 24) & 0xff; From patchwork Fri Apr 19 03:52:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635630 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 513A9DDD9; Fri, 19 Apr 2024 03:45:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498341; cv=none; b=T8e9XPIRZo6dgxOjwvgT+peIBUiayXQAnq419lp2mCZnCpe7Ebh8pHi0EddCW6ZzfCJztcHgdLBEMg2hVAo1L4M2fZswCo4DsxmDVrWE3fxMEs4e4FKLKUIwpT5usbtEV6tvAaTD6OcqGqayO+pc3Yi36KqPzLw7jzQhb7jxsQE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498341; c=relaxed/simple; bh=LxFSjPHK2sa2aihQ51xpIpnx2+ypI2jC3yBIVvG0fmI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aW4XgJnS7cfOFozrheML1dAkOKTmuio3yqvmp4Epi4vCkVC4bBbDcm9AsWGMq+hOEzhA0Hz9DsN6hVokIRY/y/Omqvu9T4CwBG3B8SoZI+TM3xoaIQjE58mlCdG/+f5fMHZJbqkQEWmvHWqdBcQwVY7G39Q8eWD3UdDtTPok8Kg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=RVL+eOL9; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="RVL+eOL9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498340; x=1745034340; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LxFSjPHK2sa2aihQ51xpIpnx2+ypI2jC3yBIVvG0fmI=; b=RVL+eOL9Xs8tis0BQU5msN6ChDcpLSWWJ8gkY4qQnTHjlsSvEWDmbx8A 2AUKCvafcUK1smlIbiAnhFIN7Arwx5ke8u/LRib7tP1zyF+nqmQ8R2Rv1 E2aHK4YiarCgV/oW+WPH/wLKr9tAS+lO4LYl55hq9ZGc8Tjv/e30nXAVj TSXucDcmY1DnFmL/qmAa1RiSnPWAmwEYuXzYLoaCyTPZu6ktR0ZYzZ3LJ YaIMM7g4e2VKYjY+UiHbkNbgYpwBa8s/UVhNmrr4thmugtWvlZFIZ3xYc 6ZKRg6AKHEbPJIak8ul4dSnLRl8KgSvey9IkRZkT85mA1zlRREK4IWR9H w==; X-CSE-ConnectionGUID: 0+LRJzFsSIKmhLOGHqW+XQ== X-CSE-MsgGUID: 9ca9CsGWRf2xJQM+/Bc8rA== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565404" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565404" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:45:40 -0700 X-CSE-ConnectionGUID: +1gYnQlRSTKa25/eYYd33g== X-CSE-MsgGUID: zl6nGItwS+mIbwFsyK0fkg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410114" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:45:37 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 02/17] x86: pmu: Remove blank line and redundant space Date: Fri, 19 Apr 2024 11:52:18 +0800 Message-Id: <20240419035233.3837621-3-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 code style changes. Reviewed-by: Mingwei Zhang Signed-off-by: Dapeng Mi --- x86/pmu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x86/pmu.c b/x86/pmu.c index 47a1a602ade2..4847a424f572 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -203,8 +203,7 @@ static noinline void __measure(pmu_counter_t *evt, uint64_t count) static bool verify_event(uint64_t count, struct pmu_event *e) { // printf("%d <= %ld <= %d\n", e->min, count, e->max); - return count >= e->min && count <= e->max; - + return count >= e->min && count <= e->max; } static bool verify_counter(pmu_counter_t *cnt) From patchwork Fri Apr 19 03:52:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635631 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3875811713; Fri, 19 Apr 2024 03:45:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498344; cv=none; b=QsWesnx0BDcNhZb5nU5l3KgXG1mJdfkiMco4WNqdznx1Pv+RcW992Zy22bPxLboJxKbUAJmQbZaNko56VZN9bANIN5MxNr58CDcwDltImCLc8FogZTV9gEaZWYSnzs7PVtj2gY98aS7/2ff9U3BnRDY8P2Pt6LfpPhlFvUvJd5E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498344; c=relaxed/simple; bh=CEzCgDWBVKLE5GxE7FSPfrK8LT8jtbQYUwpmr+e1jXY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=teUIuGvfp45oK1BreYC8UiQd3Q+YVnLhEilcyID4Qg2mUmhhDKU4+5gK7yWiKiik2Zzng6xi0PW+KNM3qV4nD8xOSeXnfzm0eAz51q7X1PnvluVuEC275nnU8EZk0S4bajta7yVc+YPE5DvIoVZX8YWj7iFGANCbaNmMNjE/GmE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=PhLnN9fr; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="PhLnN9fr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498343; x=1745034343; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CEzCgDWBVKLE5GxE7FSPfrK8LT8jtbQYUwpmr+e1jXY=; b=PhLnN9fr4oeKM/1ufEkyR8C3cTkEAlLpZxNPD0evtb6NwEtmy4NBH7ce O5Ro3tV1Cd5eqwRwoRNczgLqft7PcCBPmXmXSrfSfmww3lg9LSpOdZ1/5 20/IK8y5kl+7jxDXCzvmPpfIuSqm99CciIoumYfQix0CT+7G1Nkw5w0eq bFpk4BxZTBVmkOqJSC1dh93y3ny7H7Dr1q0nz8USg00vW/jhrTdZBkkUW rQ7T6RTHNc73suCTjOnhlxU/V5hAowmJpFDUN+UeQysBpxrt063Cu/VLu ZDvNhaGiMWSJy4QbrazjNPdsc596xHnIsAoPYkNY7rQ+yl3Oy5QJwWfPg w==; X-CSE-ConnectionGUID: /0b2OimVTVqL9CdPCiQaiw== X-CSE-MsgGUID: W2Z0LTEyQEyuNydHbWiXjg== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565411" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565411" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:45:43 -0700 X-CSE-ConnectionGUID: hRWPN1lBS0ygcWzxNrhHLA== X-CSE-MsgGUID: c9gF6VFfSL+LfZbss1Zn5g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410121" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:45:40 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 03/17] x86: pmu: Refine fixed_events[] names Date: Fri, 19 Apr 2024 11:52:19 +0800 Message-Id: <20240419035233.3837621-4-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In SDM the fixed counter is numbered from 0 but currently the fixed_events names are numbered from 1. It would cause confusion for users. So Change the fixed_events[] names to number from 0 as well and keep identical with SDM. Reviewed-by: Mingwei Zhang Signed-off-by: Dapeng Mi --- x86/pmu.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x86/pmu.c b/x86/pmu.c index 4847a424f572..c971386db4e6 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -44,9 +44,9 @@ struct pmu_event { {"branches", 0x00c2, 1*N, 1.1*N}, {"branch misses", 0x00c3, 0, 0.1*N}, }, 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, 0.1*N, 30*N} + {"fixed 0", MSR_CORE_PERF_FIXED_CTR0, 10*N, 10.2*N}, + {"fixed 1", MSR_CORE_PERF_FIXED_CTR0 + 1, 1*N, 30*N}, + {"fixed 2", MSR_CORE_PERF_FIXED_CTR0 + 2, 0.1*N, 30*N} }; char *buf; From patchwork Fri Apr 19 03:52:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635632 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 169E179F9; Fri, 19 Apr 2024 03:45:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498347; cv=none; b=DMPw1wINQyTkHwWqYnUNL0y9tUt95HI90TfnPDFy75ZgvvJTKEDc1YgGESUl37qwnn7Hs+wbt/PoI0g0BK6TUL/zNx18Ph01e9mRgApnWKXGU5a+H+H1lT+/9SfzlPK0/X6eBlGjpjn+6U3SMApcoWlj4gnwtzOuJca4deM3JHc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498347; c=relaxed/simple; bh=NZsLuZu/5ti+G6Ydn1XN7FYIKHLkE4jtT7DJv0eWE1o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oVrBS1c3B2AK960mS3NGmEu6MYsItS7/OpqDoMzG3gAqoAZdhv0PP3hTPLuVrBiQNwgmYt++mcWOp2R3gqimKCV6Ijt/OaqBYqyRH6Np8F7MbueGzQLsnCgE6OdkaKTD0+0/Y8makBhRbUW7wGMWbrSE+7UUYgKny7pchAqnxLg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=NLjHJ1dy; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="NLjHJ1dy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498346; x=1745034346; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NZsLuZu/5ti+G6Ydn1XN7FYIKHLkE4jtT7DJv0eWE1o=; b=NLjHJ1dyH685VLeE0ORMQnDtkaSBCsUIaWjT2iO8dM3KZQknaPd5p/cV hDOH8mlr8Skx+8qNa03ITyvFblgwFvhYFLquEcS+kz9jEMjGNmRfqa/Yz UD76qyax+qnTpSDHN3KBAA8I8OaTMzzsfnnp8SW0oq2To15ntzGQRJ18l itKJAH64y33RrxswYbOt6C5gwkjMgQVuOunG3ysDYw3HwwT/2k+uRvZQj WdHj/906vKr/KtbC+BlDUXoiWsASdRMasOGQke+CGFSDdgCj6Cl7h/z9n s4fm3YmZVVE+S0j6MMPzmMcSK7BjxQXXFIL9qHdI665MEoXQsbwZ7HM7+ Q==; X-CSE-ConnectionGUID: h61CPVycR66Sh/IuareZXw== X-CSE-MsgGUID: Rou8wjJtTZu41nWypDXSzw== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565418" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565418" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:45:46 -0700 X-CSE-ConnectionGUID: KKNFX8YoReOhSWUr/EmbxA== X-CSE-MsgGUID: 1yc7TGEMQXObYBVkNLm2iA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410140" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:45:43 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 04/17] x86: pmu: Fix the issue that pmu_counter_t.config crosses cache line Date: Fri, 19 Apr 2024 11:52:20 +0800 Message-Id: <20240419035233.3837621-5-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When running pmu test on SPR, the following #GP fault is reported. Unhandled exception 13 #GP at ip 000000000040771f error_code=0000 rflags=00010046 cs=00000008 rax=00000000004031ad rcx=0000000000000186 rdx=0000000000000000 rbx=00000000005142f0 rbp=0000000000514260 rsi=0000000000000020 rdi=0000000000000340 r8=0000000000513a65 r9=00000000000003f8 r10=000000000000000d r11=00000000ffffffff r12=000000000043003c r13=0000000000514450 r14=000000000000000b r15=0000000000000001 cr0=0000000080010011 cr2=0000000000000000 cr3=0000000001007000 cr4=0000000000000020 cr8=0000000000000000 STACK: @40771f 40040e 400976 400aef 40148d 401da9 4001ad FAIL pmu It looks EVENTSEL0 MSR (0x186) is written a invalid value (0x4031ad) and cause a #GP. Further investigation shows the #GP is caused by below code in __start_event(). rmsr(MSR_GP_EVENT_SELECTx(event_to_global_idx(evt)), evt->config | EVNTSEL_EN); The evt->config is correctly initialized but seems corrupted before writing to MSR. The original pmu_counter_t layout looks as below. typedef struct { uint32_t ctr; uint64_t config; uint64_t count; int idx; } pmu_counter_t; Obviously the config filed crosses two cache lines. When the two cache lines are not updated simultaneously, the config value is corrupted. Adjust pmu_counter_t fields order and ensure config field is cache-line aligned. Signeduoff-by: Dapeng Mi --- x86/pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x86/pmu.c b/x86/pmu.c index c971386db4e6..5fd7439a0eba 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -20,9 +20,9 @@ typedef struct { uint32_t ctr; + uint32_t idx; uint64_t config; uint64_t count; - int idx; } pmu_counter_t; struct pmu_event { From patchwork Fri Apr 19 03:52:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635633 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02DA3225CF; Fri, 19 Apr 2024 03:45:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498350; cv=none; b=NEhyMeCjJ9yPAhtIrkfLRVVO6ev1uVoJ6FKgBmWhEXCOn7j0n+QX3eK6RhfGEvhpqtu9BdyuxL9bflJyO/bC76uogCfPpBIqos1DwxaMA1Dl1CfIQrgpmRv8THWXC5+h9VrBEFTdyjS2wXutHTCmx1eeh3l5gorbBtEl40LHjoo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498350; c=relaxed/simple; bh=/Fg7XH4wVHMgm+RVDjt7bI8o2hvzs79zF/aqW2rqFOs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rndHUmRPpisWbceUnXAgS/B0tgfvZ5Vu4DuB+4zTlJUEGGzQGSqPh7rUS5NtQDRWltUpUcSOPdgDcEoZ5WWOg3smOj3ljeFndnF8UXfxXNvLaZHDqUZACtnO3WwWRJTdCokrvM+ZP+DI120Kr8K3wC4m3iPqDom1J6bbQod3C4A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=X0Ako2w9; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="X0Ako2w9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498349; x=1745034349; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/Fg7XH4wVHMgm+RVDjt7bI8o2hvzs79zF/aqW2rqFOs=; b=X0Ako2w9GToRrL8lwDDjGzvMpPq1kxXdDRjNoXUvoO4DpZ7USVmLr2a+ 2sWdRLdhBjaNXwgIUEJ8OwgGcAKNpTsBmK0v7dwjcEImp//HIZCRZxhKS 0GnpBTfNqNMtbj5w+KAVmEABzxF0JHNTWqqfHL5KQLN7khUuXsGyldZfs CKm7wT/gWU/P4vjACULS++En4EnJXtzu1HwkJFgifTGbiLWf3AYebg0Iq D9rw0Ce9FvrGcV6Y4Oqbdxyy0ZS89QVnZBZybwbb0x8HThX3gRs0xQ3dS fdOBI7ENiGHTeHCk790LpDcaQ3s0ft7474YYlukwf9zsiWLbd1Qv1tXbq Q==; X-CSE-ConnectionGUID: VV9R6Us/QAWJ3+2DFoGUzQ== X-CSE-MsgGUID: GmqykvU7S3+4ssAPAxAIUA== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565428" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565428" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:45:49 -0700 X-CSE-ConnectionGUID: w7V3nyGWTiyx0qlxwfnqpQ== X-CSE-MsgGUID: lnADloPQQ7SFjPsPSyAGGQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410148" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:45:46 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 05/17] x86: pmu: Enlarge cnt[] length to 48 in check_counters_many() Date: Fri, 19 Apr 2024 11:52:21 +0800 Message-Id: <20240419035233.3837621-6-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Considering there are already 8 GP counters and 4 fixed counters on latest Intel processors, like Sapphire Rapids. The original cnt[] array length 10 is definitely not enough to cover all supported PMU counters on these new processors even through currently KVM only supports 3 fixed counters at most. This would cause out of bound memory access and may trigger false alarm on PMU counter validation It's probably more and more GP and fixed counters are introduced in the future and then directly extends the cnt[] array length to 48 once and for all. Base on the layout of IA32_PERF_GLOBAL_CTRL and IA32_PERF_GLOBAL_STATUS, 48 looks enough in near feature. Reviewed-by: Jim Mattson Signed-off-by: Dapeng Mi --- x86/pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x86/pmu.c b/x86/pmu.c index 5fd7439a0eba..494af4012e84 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -253,7 +253,7 @@ static void check_fixed_counters(void) static void check_counters_many(void) { - pmu_counter_t cnt[10]; + pmu_counter_t cnt[48]; int i, n; for (i = 0, n = 0; n < pmu.nr_gp_counters; i++) { From patchwork Fri Apr 19 03:52:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635634 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D709F2837B; Fri, 19 Apr 2024 03:45:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498353; cv=none; b=hr7/bkSvg3zkYmKApfQAUMM9u15FLq7Nn/GaVoliT3VMcnDEDi2KLQeObt2liu5v8h9v+3OgiM5B89pTQDdiqr+rttm6aSH4OmePFzxd3s0BcOfZTC4XtiG3e4g6jakw2CDVkmwSkqtCovzWRU4rgiGnl80urfNLXz8yvFO9ZLk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498353; c=relaxed/simple; bh=xJIfGxQUiFH95XrF4Oof2nG2cWZnTQoaCXLtcOAi5sY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YDaOZm0AW56w4ULzlrXFqDM6ZpmHgSQClP7i7MFn0hMH7C42vxmmVzCHOUEvEoFKXmt+dQMo8FC1YSubA/f74uy13q4afPMjU101XhfIjqnrFeAl2ziJgihM0XDTfdt1546u6J18zRO/WaC5Wxc1g8tYJPaI4Ic3CEbGM5b+Zno= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=BZRjxdVc; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="BZRjxdVc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498352; x=1745034352; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xJIfGxQUiFH95XrF4Oof2nG2cWZnTQoaCXLtcOAi5sY=; b=BZRjxdVccrWgQ984QoY3IPt4imRQJvnatuDVrXV0Jh3mo712crxyH5aD pj/osOAPaXTcf6lLkhMetvxHKVLnxjAz6dBOwX50bxVztGlX/lRX4a134 MmRu+8I/yAuD0gwIYwz59zokeynaiuBL7OdOVNMkUK4SFkBfJUTXovETb qprm5Hjf05M/kSSt3X7OjaJUre4+7sDYJ9VB3nr4UDNNixPZt0NzkIeJp O4mKWVFioCwau6QS40rTXsgWxELeg1258ESGV6h2pzF8rwppvENttwDAf 99JePXQBKJ45v4fARASHPms/39Zw+/PQ3x0ElKTGYCCFbxwG5R+nqhgVM Q==; X-CSE-ConnectionGUID: vP5B/sC0QleaS0vU4lBJ2w== X-CSE-MsgGUID: P/ecdxjbRciDkxkrFxElRQ== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565443" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565443" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:45:51 -0700 X-CSE-ConnectionGUID: nzqB9zqrR4iITSAUJjbcjw== X-CSE-MsgGUID: WY5nL+qURCGpqKn6hd6Y1g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410156" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:45:48 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 06/17] x86: pmu: Add asserts to warn inconsistent fixed events and counters Date: Fri, 19 Apr 2024 11:52:22 +0800 Message-Id: <20240419035233.3837621-7-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Current PMU code deosn't check whether PMU fixed counter number is larger than pre-defined fixed events. If so, it would cause memory access out of range. So add assert to warn this invalid case. Reviewed-by: Mingwei Zhang Signed-off-by: Dapeng Mi --- x86/pmu.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/x86/pmu.c b/x86/pmu.c index 494af4012e84..461a4090d475 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -111,8 +111,12 @@ static struct pmu_event* get_counter_event(pmu_counter_t *cnt) for (i = 0; i < gp_events_size; i++) if (gp_events[i].unit_sel == (cnt->config & 0xffff)) return &gp_events[i]; - } else - return &fixed_events[cnt->ctr - MSR_CORE_PERF_FIXED_CTR0]; + } else { + unsigned int idx = cnt->ctr - MSR_CORE_PERF_FIXED_CTR0; + + assert(idx < ARRAY_SIZE(fixed_events)); + return &fixed_events[idx]; + } return (void*)0; } @@ -738,6 +742,8 @@ int main(int ac, char **av) printf("Fixed counters: %d\n", pmu.nr_fixed_counters); printf("Fixed counter width: %d\n", pmu.fixed_counter_width); + assert(pmu.nr_fixed_counters <= ARRAY_SIZE(fixed_events)); + apic_write(APIC_LVTPC, PMI_VECTOR); check_counters(); From patchwork Fri Apr 19 03:52:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635635 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BA29E2AF17; Fri, 19 Apr 2024 03:45:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498356; cv=none; b=QnJG9oS8fRiVDMbTOErJbkpOcs6zVbD6CRnePKHjc8T7sbgUUV0jdZn7BxuKgG/otz3HHl9FT/x0okNzv7bfuc60y/PpO/U7F0e51bHkPKq1mzmNfeJY3HrnhcaHSN3039zg1gWB+B1VnfZUwzhjWrpSMC6LZgJddgsAt7Wq0SY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498356; c=relaxed/simple; bh=Qsmg2ffUGXqjYUoa6KnIqYzBA5c7umCrK37g6zMvxy8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GFFZK8/dqTdMWuYMyhagb4cr5yI9ehQQ6AJqwFgOitrUBVm0wkb+3ITa6Lk2/cQm+Fjuih42doP2QUIJJdac/B+gw80fTMstHkJUPwN80mXUZiq7tr1hQyJwYjhcKk0ThLrAByKihTorzP6vNt0L5r7nkHgJV+XlPgZYYlrYgLs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ikeXhYwS; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ikeXhYwS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498355; x=1745034355; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Qsmg2ffUGXqjYUoa6KnIqYzBA5c7umCrK37g6zMvxy8=; b=ikeXhYwSMJwKP+dmHi/JT9S0OtDHbuy++AdzSxeGr3zLJ93NfWBb97vb 4DO2pjVPFB/YzTQGbz9zStxFImeMmxA7tdFGRYvZFclzp43EO3kKQm/EX EkPdCOiJaum+a6nYLyjvNirOE1m70xgC7XyFcY3kap9HIBPT+GCWfDtb/ TtXpXP7+rb9mDVL1zDLEUiVZN2fUJTcdMnmCyl6J49GQ4TzjdwsbnaZ8/ Wuv+ZRuoY03hLzjEKFCs+q7crFavS3+dYKK2xoqwqUz8VHMLHrazJd1Yl mvPLKqY+DDYJzczsXbMh1UXNltx+YblCrSt4gkABVOMuvA4vDMUnZp5Jf g==; X-CSE-ConnectionGUID: qAc2q0AHQpq9B8Ifo+vSxA== X-CSE-MsgGUID: Myia6YNlSXusML20p7DLpA== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565450" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565450" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:45:54 -0700 X-CSE-ConnectionGUID: W1VyNyMfRJe7HdPoKvzNzw== X-CSE-MsgGUID: 5O11IwgkTKuehBxMZyagnw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410163" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:45:51 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 07/17] x86: pmu: Fix cycles event validation failure Date: Fri, 19 Apr 2024 11:52:23 +0800 Message-Id: <20240419035233.3837621-8-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When running pmu test on SPR, sometimes the following failure is reported. PMU version: 2 GP counters: 8 GP counter width: 48 Mask length: 8 Fixed counters: 3 Fixed counter width: 48 1000000 <= 55109398 <= 50000000 FAIL: Intel: core cycles-0 1000000 <= 18279571 <= 50000000 PASS: Intel: core cycles-1 1000000 <= 12238092 <= 50000000 PASS: Intel: core cycles-2 1000000 <= 7981727 <= 50000000 PASS: Intel: core cycles-3 1000000 <= 6984711 <= 50000000 PASS: Intel: core cycles-4 1000000 <= 6773673 <= 50000000 PASS: Intel: core cycles-5 1000000 <= 6697842 <= 50000000 PASS: Intel: core cycles-6 1000000 <= 6747947 <= 50000000 PASS: Intel: core cycles-7 The count of the "core cycles" on first counter would exceed the upper boundary and leads to a failure, and then the "core cycles" count would drop gradually and reach a stable state. That looks reasonable. The "core cycles" event is defined as the 1st event in xxx_gp_events[] array and it is always verified at first. when the program loop() is executed at the first time it needs to warm up the pipeline and cache, such as it has to wait for cache is filled. All these warm-up work leads to a quite large core cycles count which may exceeds the verification range. To avoid the false positive of cycles event caused by warm-up, explicitly introduce a warm-up state before really starting verification. Signed-off-by: Dapeng Mi --- x86/pmu.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/x86/pmu.c b/x86/pmu.c index 461a4090d475..43ef7ecbcaea 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -591,11 +591,27 @@ static void check_tsx_cycles(void) report_prefix_pop(); } +static void warm_up(void) +{ + int i = 8; + + /* + * Since cycles event is always run as the first event, there would be + * a warm-up state to warm up the cache, it leads to the measured cycles + * value may exceed the pre-defined cycles upper boundary and cause + * false positive. To avoid this, introduce an warm-up state before + * the real verification. + */ + while (i--) + loop(); +} + static void check_counters(void) { if (is_fep_available()) check_emulated_instr(); + warm_up(); check_gp_counters(); check_fixed_counters(); check_rdpmc(); From patchwork Fri Apr 19 03:52:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635636 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A03102E41C; Fri, 19 Apr 2024 03:45:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498359; cv=none; b=Vuq/bnD14QOQl++oiv+7w7Q+j2YqWVoF/xjNqKYRE8sJ8Kx4vwg69yXYfparz7D0w2G5nzYs22AuoRETp2tiTm0zVWW5/SMgsONeNSiQcFq706ZX2ttZ6ytbisUjsj2nRuS3vY6kDro/89e+VQW3GJZMimUm21IT+jBAvBwl4QM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498359; c=relaxed/simple; bh=bBwGEz/zSa/hCAv0OjbB+yO6hHyuL/TvJodBdVmlGns=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fjCa67LKJLrcnBB3AzmDI4rui5wd2Kkp5k3GfzHKMluHSktQ9AuBHoSFQy/EkTLH7lt6Dh1GiBONPLlZmQ1btbze42VwBiXfP8i2JfZtbITsSLP+62rvdxUYI6G9h/wubT83YhL0WodFO1FyJHfjPFMF1ZcCiN/C8dCsN9jw3lE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=OwaoMflC; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="OwaoMflC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498357; x=1745034357; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bBwGEz/zSa/hCAv0OjbB+yO6hHyuL/TvJodBdVmlGns=; b=OwaoMflC68rPwC2OH0zfCh919dhL4EmkP8zYPIPo7AJPlCMLvQXB0NUx sG0XxndfzOqOj2uZoNiA0Hk1ScsLa8kX7Jdeo15GFZKqIjG+gAt3N+emI 5X3NlucI82DJX41IpeHMaquQqqnYSZnZYtja9iBKyH+LdL/bjFt2qh+cb LmUYgaKy1zWFecrkwNrh7D47rrc5tQKgqgdRmVGSyDgHnXHjErjYhQ2bq e86XeQfqNnjV4WgqSZE3okmssN2JhWm9F54lf8mld5ZlRUH+HfbZPA2Pa PYu7i/kzFuC9lKDJbyn+ZZUiekaPc7vmYTNYJrrvyX4mRbl50uaCXnJIY g==; X-CSE-ConnectionGUID: d1MLEUJxTni4KxwK1+qysw== X-CSE-MsgGUID: l1isJZncSe6c94aYBwn16A== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565457" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565457" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:45:57 -0700 X-CSE-ConnectionGUID: SmE3ZuSZR2W1MvQMgHjH9w== X-CSE-MsgGUID: GswCXT3bTPqdfBgfgyD5eQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410171" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:45:54 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 08/17] x86: pmu: Use macro to replace hard-coded branches event index Date: Fri, 19 Apr 2024 11:52:24 +0800 Message-Id: <20240419035233.3837621-9-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently the branches event index is a hard-coded number. User could add new events and cause the branches event index changes in the future, but don't notice the hard-coded event index and forget to update the event index synchronously, then the issue comes. Thus, replace the hard-coded index to a macro. Signed-off-by: Dapeng Mi --- x86/pmu.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/x86/pmu.c b/x86/pmu.c index 43ef7ecbcaea..fd1b22104fc4 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -49,6 +49,22 @@ struct pmu_event { {"fixed 2", MSR_CORE_PERF_FIXED_CTR0 + 2, 0.1*N, 30*N} }; +/* + * Events index in intel_gp_events[], ensure consistent with + * intel_gp_events[]. + */ +enum { + INTEL_BRANCHES_IDX = 5, +}; + +/* + * Events index in amd_gp_events[], ensure consistent with + * amd_gp_events[]. + */ +enum { + AMD_BRANCHES_IDX = 2, +}; + char *buf; static struct pmu_event *gp_events; @@ -481,7 +497,8 @@ static void check_emulated_instr(void) { uint64_t status, instr_start, brnch_start; uint64_t gp_counter_width = (1ull << pmu.gp_counter_width) - 1; - unsigned int branch_idx = pmu.is_intel ? 5 : 2; + unsigned int branch_idx = pmu.is_intel ? + INTEL_BRANCHES_IDX : AMD_BRANCHES_IDX; pmu_counter_t brnch_cnt = { .ctr = MSR_GP_COUNTERx(0), /* branch instructions */ From patchwork Fri Apr 19 03:52:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635637 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8BAAC3E485; Fri, 19 Apr 2024 03:46:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498361; cv=none; b=pSUvwHLeNooNqihy7B0g+xkpBDh+DATl7hyJkfPd5Xp1aAX0qU77i+fPIx+tN0SwRQkwS3IZK/livNNo4li5TArPdpoNrNtGL8nxfHfgzheTyJjB9jHpSGSkwQAxUlkZWr9iFoCO+PpPdrq1qGtkfO7nyqPdcSvWlU49Iep3LhM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498361; c=relaxed/simple; bh=5/QEb1aKZvreLoUXtF0uNhYLsiRGyvEfapJ7il+jZgM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=INtOocGN7P+Q5gJbGg9tADbXCBXfnUGQaErdIhFYjEMBDT4dEmzDpOx5F+IH3oG788Cvi+VFZtAP2w175Ec7zdYKW0B+xtDs3t3tjj1pXFqWMSx159uNUWxOJrbxdM28zCvbV4IUApsSitTBu/Mn9tNBjlJhEoCj674exVosNIM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=OJcrAJl/; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="OJcrAJl/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498360; x=1745034360; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5/QEb1aKZvreLoUXtF0uNhYLsiRGyvEfapJ7il+jZgM=; b=OJcrAJl/n9RN5vX+2KhRr6pe09D6MWR2oJqmgACAEFksvcfZoiBdZtMa wfx4X4j3NYZSLXwXr+osbtYNYA8gD03+zIip5qIbqtm9XKYMwlpA8Uk5o twJ3Lvfniju74P95bvDK/k3Bbrs9UxODYGYKALny/A9R8v3ijmX2guiZ/ Otu8+oIj55ZoJy7mFJYjtKO9t+LKv2JqCuHyYujOVjmdH5kO4hTa9V6kr 3Z7UdQCYajFdUDLODt99AEMzXzNtF1MZppsae3SZAm3qrKTB2AOtixft+ Jc6V2Mvrfb5xwV2jyEY1vF5ZIn79i9TcnrejJH6QJH31G8U3JtF9yB33y Q==; X-CSE-ConnectionGUID: 7SByHVvVQr2GgV0z14Yhzg== X-CSE-MsgGUID: fdvePZ+nRHqBS+EA2jsSjQ== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565463" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565463" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:46:00 -0700 X-CSE-ConnectionGUID: wtrIdJe7TvmMzrRFSpU+JQ== X-CSE-MsgGUID: xj6HBejRTIKIRZ4yRuSWcA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410183" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:45:57 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 09/17] x86: pmu: Use macro to replace hard-coded ref-cycles event index Date: Fri, 19 Apr 2024 11:52:25 +0800 Message-Id: <20240419035233.3837621-10-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Replace hard-coded ref-cycles event index with macro to avoid possible mismatch issue if new event is added in the future and cause ref-cycles event index changed, but forget to update the hard-coded ref-cycles event index. Signed-off-by: Dapeng Mi --- x86/pmu.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/x86/pmu.c b/x86/pmu.c index fd1b22104fc4..6ae46398d84b 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -54,6 +54,7 @@ struct pmu_event { * intel_gp_events[]. */ enum { + INTEL_REF_CYCLES_IDX = 2, INTEL_BRANCHES_IDX = 5, }; @@ -697,7 +698,8 @@ static void set_ref_cycle_expectations(void) { pmu_counter_t cnt = { .ctr = MSR_IA32_PERFCTR0, - .config = EVNTSEL_OS | EVNTSEL_USR | intel_gp_events[2].unit_sel, + .config = EVNTSEL_OS | EVNTSEL_USR | + intel_gp_events[INTEL_REF_CYCLES_IDX].unit_sel, }; uint64_t tsc_delta; uint64_t t0, t1, t2, t3; @@ -733,8 +735,10 @@ static void set_ref_cycle_expectations(void) if (!tsc_delta) return; - intel_gp_events[2].min = (intel_gp_events[2].min * cnt.count) / tsc_delta; - intel_gp_events[2].max = (intel_gp_events[2].max * cnt.count) / tsc_delta; + intel_gp_events[INTEL_REF_CYCLES_IDX].min = + (intel_gp_events[INTEL_REF_CYCLES_IDX].min * cnt.count) / tsc_delta; + intel_gp_events[INTEL_REF_CYCLES_IDX].max = + (intel_gp_events[INTEL_REF_CYCLES_IDX].max * cnt.count) / tsc_delta; } static void check_invalid_rdpmc_gp(void) From patchwork Fri Apr 19 03:52:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635638 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6A9994317A; Fri, 19 Apr 2024 03:46:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498365; cv=none; b=OHxS0HYLn9xXjfsmy87INGmGR3TyaDnREIdvaqS6G01hSbeDRYvDMlQxL6tdUYVZZtbGPe3EC4TvivgWkloiSBbdOOV3IrFEJM2EuGhKFsQOtwqAX3RUFC/dDBIi3oUleIQT2UUX2HXX8a0uKr70nwFlrt8NSSq2ny2pBWaoIC8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498365; c=relaxed/simple; bh=/eDNOrT42kpAxYyGh97YDD4hXbu+mRJnDOxAdDrrLl4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=adDPtdClBmh06mMry9F/PoC2htaKlgl9Fwt/8llTOE2Q8gJFjWhs9GjwC5fB98nByreJ+fVoIfev5Oxq7JAoc44QK2NIwD5B+FxaKGyP953IDX6+yQ5A5p0VWO12NMXKD0grCol1N4+j5exS0QrffOyXFdX8HkOd4x3n5jY2Blc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=UqvJdMNe; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="UqvJdMNe" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498363; x=1745034363; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/eDNOrT42kpAxYyGh97YDD4hXbu+mRJnDOxAdDrrLl4=; b=UqvJdMNexQBN6bfgz0Dm1R4r6L+ZBuGLmUVi5uw+b/U2cJH3EELRgHdk PEbsE5OFm2UVYccQ5aPK0RGvGUXIfOO/KZ6URXTuQPQ5CEwku+uvFYzb8 Q73fkeSFny19e+qpNSix+8lchmYD6O8Y3+4mvKIYgssuu6IoJoVdLtAQJ 3bNDOJMOcFXrkmvY+4YYQGINDN+sZEOPtrqXDsOUcl6WmicC+fAccgKtZ uIrdlAmResTV5eKLOXSP7ROnPaAarf9fQWlJctb1NEryZovc6Xhfmsb8v b/KE4LoMf2hCllfcxpkYuNGnp4I4REMi+pg3ZyZB0LiEZyPhE+chje1wJ g==; X-CSE-ConnectionGUID: IzUasVH4Q9Kaz0p8ghtRBA== X-CSE-MsgGUID: j9MjMzYbRUGHjxj+W2ZDQg== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565472" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565472" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:46:03 -0700 X-CSE-ConnectionGUID: siuQYcQ/QDWc4frLCM2/Ng== X-CSE-MsgGUID: 2cU109VxTN2Nzwn0o5Qopw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410231" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:46:00 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 10/17] x86: pmu: Use macro to replace hard-coded instructions event index Date: Fri, 19 Apr 2024 11:52:26 +0800 Message-Id: <20240419035233.3837621-11-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Replace hard-coded instruction event index with macro to avoid possible mismatch issue if new event is added in the future and cause instructions event index changed, but forget to update the hard-coded event index. Signed-off-by: Dapeng Mi --- x86/pmu.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/x86/pmu.c b/x86/pmu.c index 6ae46398d84b..20bc6de9c936 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -54,6 +54,7 @@ struct pmu_event { * intel_gp_events[]. */ enum { + INTEL_INSTRUCTIONS_IDX = 1, INTEL_REF_CYCLES_IDX = 2, INTEL_BRANCHES_IDX = 5, }; @@ -63,6 +64,7 @@ enum { * amd_gp_events[]. */ enum { + AMD_INSTRUCTIONS_IDX = 1, AMD_BRANCHES_IDX = 2, }; @@ -317,11 +319,16 @@ static uint64_t measure_for_overflow(pmu_counter_t *cnt) static void check_counter_overflow(void) { - uint64_t overflow_preset; int i; + uint64_t overflow_preset; + int instruction_idx = pmu.is_intel ? + INTEL_INSTRUCTIONS_IDX : + AMD_INSTRUCTIONS_IDX; + pmu_counter_t cnt = { .ctr = MSR_GP_COUNTERx(0), - .config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel /* instructions */, + .config = EVNTSEL_OS | EVNTSEL_USR | + gp_events[instruction_idx].unit_sel /* instructions */, }; overflow_preset = measure_for_overflow(&cnt); @@ -377,13 +384,18 @@ static void check_counter_overflow(void) static void check_gp_counter_cmask(void) { + int instruction_idx = pmu.is_intel ? + INTEL_INSTRUCTIONS_IDX : + AMD_INSTRUCTIONS_IDX; + pmu_counter_t cnt = { .ctr = MSR_GP_COUNTERx(0), - .config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel /* instructions */, + .config = EVNTSEL_OS | EVNTSEL_USR | + gp_events[instruction_idx].unit_sel /* instructions */, }; cnt.config |= (0x2 << EVNTSEL_CMASK_SHIFT); measure_one(&cnt); - report(cnt.count < gp_events[1].min, "cmask"); + report(cnt.count < gp_events[instruction_idx].min, "cmask"); } static void do_rdpmc_fast(void *ptr) @@ -458,9 +470,14 @@ static void check_running_counter_wrmsr(void) { uint64_t status; uint64_t count; + unsigned int instruction_idx = pmu.is_intel ? + INTEL_INSTRUCTIONS_IDX : + AMD_INSTRUCTIONS_IDX; + pmu_counter_t evt = { .ctr = MSR_GP_COUNTERx(0), - .config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel, + .config = EVNTSEL_OS | EVNTSEL_USR | + gp_events[instruction_idx].unit_sel, }; report_prefix_push("running counter wrmsr"); @@ -469,7 +486,7 @@ static void check_running_counter_wrmsr(void) loop(); wrmsr(MSR_GP_COUNTERx(0), 0); stop_event(&evt); - report(evt.count < gp_events[1].min, "cntr"); + report(evt.count < gp_events[instruction_idx].min, "cntr"); /* clear status before overflow test */ if (this_cpu_has_perf_global_status()) @@ -500,6 +517,9 @@ static void check_emulated_instr(void) uint64_t gp_counter_width = (1ull << pmu.gp_counter_width) - 1; unsigned int branch_idx = pmu.is_intel ? INTEL_BRANCHES_IDX : AMD_BRANCHES_IDX; + unsigned int instruction_idx = pmu.is_intel ? + INTEL_INSTRUCTIONS_IDX : + AMD_INSTRUCTIONS_IDX; pmu_counter_t brnch_cnt = { .ctr = MSR_GP_COUNTERx(0), /* branch instructions */ @@ -508,7 +528,7 @@ static void check_emulated_instr(void) pmu_counter_t instr_cnt = { .ctr = MSR_GP_COUNTERx(1), /* instructions */ - .config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel, + .config = EVNTSEL_OS | EVNTSEL_USR | gp_events[instruction_idx].unit_sel, }; report_prefix_push("emulated instruction"); From patchwork Fri Apr 19 03:52:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635639 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6A061537E4; Fri, 19 Apr 2024 03:46:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498367; cv=none; b=VjbrkOcFd/QZrMT9tJn8T2WTDUVYxMZGEyyTQeLxVB5Rs+M18YibVO8/87GIlni+yn5ivW6ZTHU9pJTH6YT5To5wrCBJlKWFQD97d5EkjrrZx95aw4Bl1o0XTbxTJgExgf6wSZLZqBKdYV6sk6+1nckX6NCAUWb+4K0kWsRxFqc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498367; c=relaxed/simple; bh=k5yeC6To8qfzgH7nrLV5GXePfcq/WaufQeG/owbIi/Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hZtp8t6DjLlfaDPhJtCF+sBYzLMEiI0LXUfmYEU+BGYJLRZlIf6IyA2AjN4goYX5TvpaCVc2l6MpfChX0s0jFI1Mh5r2A4jiyHJ689TkUGUa9NqujpN+rUcmPEocyFw1SfM8lMh/Jw9zpgzr+N5/mS1ssddM2SOyfTxQ6ip2KYQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=dQ8yRFLE; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="dQ8yRFLE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498366; x=1745034366; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=k5yeC6To8qfzgH7nrLV5GXePfcq/WaufQeG/owbIi/Y=; b=dQ8yRFLE6KokBCL+8JKdjxzXXHvkGSaF1VtQ1TdyxQAJOTgSmKA6sjPV J8W8/Q/SzA4fNvbWmWsh2aXFXlMwJnIKrX84ICU9XWpc6rrPwtoxeBpRc jcqqs/4s/kyTOKwv/E2DYVcdNaC8hDYb4/hZ/MuTFm1VtqukedMGtjfbW gfleSSwaRoSzGDciua0x5RVnWzVgQBw2zKCDvqyGIA1SaUhZmuhgHnrr7 mau6pqHl+2CR/ZegzcdkL74l72ev1OMXaI37g9YF+y1GSUEk7lyn+VkbU QT/pGpii1dKxKY7NWsUG4gAq3B1+xe5f6z02RAmQWrv9fmDYAnusNnaNU A==; X-CSE-ConnectionGUID: ITV8UmcXTeO/zQIiov2uWg== X-CSE-MsgGUID: BEYQhsIrTL2W0a+D3jZy7A== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565480" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565480" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:46:06 -0700 X-CSE-ConnectionGUID: j4Rg0L6JSqaVG0N/ODX8sQ== X-CSE-MsgGUID: yxu2/dNGRK6UJDax2KWosw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410249" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:46:03 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 11/17] x86: pmu: Enable and disable PMCs in loop() asm blob Date: Fri, 19 Apr 2024 11:52:27 +0800 Message-Id: <20240419035233.3837621-12-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently enabling PMCs, executing loop() and disabling PMCs are divided 3 separated functions. So there could be other instructions executed between enabling PMCS and running loop() or running loop() and disabling PMCs, e.g. if there are multiple counters enabled in measure_many() function, the instructions which enabling the 2nd and more counters would be counted in by the 1st counter. So current implementation can only verify the correctness of count by an rough range rather than a precise count even for instructions and branches events. Strictly speaking, this verification is meaningless as the test could still pass even though KVM vPMU has something wrong and reports an incorrect instructions or branches count which is in the rough range. Thus, move the PMCs enabling and disabling into the loop() asm blob and ensure only the loop asm instructions would be counted, then the instructions or branches events can be verified with an precise count instead of an rough range. Signed-off-by: Dapeng Mi --- x86/pmu.c | 80 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/x86/pmu.c b/x86/pmu.c index 20bc6de9c936..d97309d7b8a3 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -18,6 +18,15 @@ #define EXPECTED_INSTR 17 #define EXPECTED_BRNCH 5 +#define LOOP_ASM(_wrmsr) \ + _wrmsr "\n\t" \ + "mov %%ecx, %%edi; mov %%ebx, %%ecx;\n\t" \ + "1: mov (%1), %2; add $64, %1;\n\t" \ + "nop; nop; nop; nop; nop; nop; nop;\n\t" \ + "loop 1b;\n\t" \ + "mov %%edi, %%ecx; xor %%eax, %%eax; xor %%edx, %%edx;\n\t" \ + _wrmsr "\n\t" + typedef struct { uint32_t ctr; uint32_t idx; @@ -73,13 +82,43 @@ char *buf; static struct pmu_event *gp_events; static unsigned int gp_events_size; -static inline void loop(void) + +static inline void __loop(void) +{ + unsigned long tmp, tmp2, tmp3; + + asm volatile(LOOP_ASM("nop") + : "=c"(tmp), "=r"(tmp2), "=r"(tmp3) + : "0"(N), "1"(buf)); +} + +/* + * Enable and disable counters in a whole asm blob to ensure + * no other instructions are counted in the window between + * counters enabling and really LOOP_ASM code executing. + * Thus counters can verify instructions and branches events + * against precise counts instead of a rough valid count range. + */ +static inline void __precise_loop(u64 cntrs) { unsigned long tmp, tmp2, tmp3; + unsigned int global_ctl = pmu.msr_global_ctl; + u32 eax = cntrs & (BIT_ULL(32) - 1); + u32 edx = cntrs >> 32; - asm volatile("1: mov (%1), %2; add $64, %1; nop; nop; nop; nop; nop; nop; nop; loop 1b" - : "=c"(tmp), "=r"(tmp2), "=r"(tmp3): "0"(N), "1"(buf)); + asm volatile(LOOP_ASM("wrmsr") + : "=b"(tmp), "=r"(tmp2), "=r"(tmp3) + : "a"(eax), "d"(edx), "c"(global_ctl), + "0"(N), "1"(buf) + : "edi"); +} +static inline void loop(u64 cntrs) +{ + if (!this_cpu_has_perf_global_ctrl()) + __loop(); + else + __precise_loop(cntrs); } volatile uint64_t irq_received; @@ -178,18 +217,17 @@ static void __start_event(pmu_counter_t *evt, uint64_t count) ctrl = (ctrl & ~(0xf << shift)) | (usrospmi << shift); wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, ctrl); } - global_enable(evt); apic_write(APIC_LVTPC, PMI_VECTOR); } static void start_event(pmu_counter_t *evt) { __start_event(evt, 0); + global_enable(evt); } -static void stop_event(pmu_counter_t *evt) +static void __stop_event(pmu_counter_t *evt) { - global_disable(evt); if (is_gp(evt)) { wrmsr(MSR_GP_EVENT_SELECTx(event_to_global_idx(evt)), evt->config & ~EVNTSEL_EN); @@ -201,14 +239,24 @@ static void stop_event(pmu_counter_t *evt) evt->count = rdmsr(evt->ctr); } +static void stop_event(pmu_counter_t *evt) +{ + global_disable(evt); + __stop_event(evt); +} + static noinline void measure_many(pmu_counter_t *evt, int count) { int i; + u64 cntrs = 0; + + for (i = 0; i < count; i++) { + __start_event(&evt[i], 0); + cntrs |= BIT_ULL(event_to_global_idx(&evt[i])); + } + loop(cntrs); for (i = 0; i < count; i++) - start_event(&evt[i]); - loop(); - for (i = 0; i < count; i++) - stop_event(&evt[i]); + __stop_event(&evt[i]); } static void measure_one(pmu_counter_t *evt) @@ -218,9 +266,11 @@ static void measure_one(pmu_counter_t *evt) static noinline void __measure(pmu_counter_t *evt, uint64_t count) { + u64 cntrs = BIT_ULL(event_to_global_idx(evt)); + __start_event(evt, count); - loop(); - stop_event(evt); + loop(cntrs); + __stop_event(evt); } static bool verify_event(uint64_t count, struct pmu_event *e) @@ -483,7 +533,7 @@ static void check_running_counter_wrmsr(void) report_prefix_push("running counter wrmsr"); start_event(&evt); - loop(); + __loop(); wrmsr(MSR_GP_COUNTERx(0), 0); stop_event(&evt); report(evt.count < gp_events[instruction_idx].min, "cntr"); @@ -500,7 +550,7 @@ static void check_running_counter_wrmsr(void) wrmsr(MSR_GP_COUNTERx(0), count); - loop(); + __loop(); stop_event(&evt); if (this_cpu_has_perf_global_status()) { @@ -641,7 +691,7 @@ static void warm_up(void) * the real verification. */ while (i--) - loop(); + loop(0); } static void check_counters(void) From patchwork Fri Apr 19 03:52:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635640 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 706A85FEE4; Fri, 19 Apr 2024 03:46:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498370; cv=none; b=riBMXo5TmMYS/eBN7h5dtKutRUrbV4mzeeFyjI/FU6BDudzJXIFjXFoq77Akn4ZzSG2f+85DUqXEl5TkDMMNmEUI5rD2bZYCUfBJn1BeGXGvqYoGB4fttup+Rib7b2lxoP/coJv5PzpY6f2gdnb7rueSALZSktUW/pTHiG0vB3c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498370; c=relaxed/simple; bh=lwt15SLJk+6I+/6HTQgdnMGKM74ORGjHD0Jay1bNsxQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Lz5eXU6evINLw6rCKJ5OvYpFYGLxJSm53oLaTjgXC9ftjUTxpXlrURJDNIkPkBHH0Mxnz5I63F6JUzi9wiV98wcgraKdz0v1R0RrwDpNCRaq+HO30YF+0cZhXSWqMXDI9I1z8wN7iR2iyqGJS8J+vxGXfsuy99609OMva9EzjKY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=hr59Z/tX; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hr59Z/tX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498369; x=1745034369; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=lwt15SLJk+6I+/6HTQgdnMGKM74ORGjHD0Jay1bNsxQ=; b=hr59Z/tXJsiezzpFh3GOMJZWH+4V10m80xlnb7ARyHSmvNEZWfFyc15v ctqpGDnfS6JO9znM8AxNh2pA3MzXW7R+gITFXuWSaCM8LkOoLizko35ZD iClIe9Vr6H6x7DWIOLfEcTc8lxaJsRuuGYuSX+0pb4JiiYKy1d8cw0195 hqWsrfF1kwAzF7TVUoPbyHFB3KpTE00AbwxcZh+KwCzFsADGHAMTWuLKq /a3RlG+B6MB6lus1wDKGoFXIuqyIA501NYZMHDNFn3bu+kqSRK9JsiYLN 9szqUSdD0l1FUTt06B0mb6B02wnwltXw81n8UT7iUqhSFpGutJUM1BmYR Q==; X-CSE-ConnectionGUID: Lsvedh9nSq6Qa/x9yMQaWg== X-CSE-MsgGUID: vDGMFyedS16MZY+FSk2fAA== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565487" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565487" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:46:09 -0700 X-CSE-ConnectionGUID: 7vf4fdEgTYeiF5B0rGy5BQ== X-CSE-MsgGUID: NXLQgSTKTquuoWE5QROKug== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410280" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:46:06 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 12/17] x86: pmu: Improve instruction and branches events verification Date: Fri, 19 Apr 2024 11:52:28 +0800 Message-Id: <20240419035233.3837621-13-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 If HW supports GLOBAL_CTRL MSR, enabling and disabling PMCs are moved in __precise_count_loop(). Thus, instructions and branches events can be verified against a precise count instead of a rough range. Signed-off-by: Dapeng Mi --- x86/pmu.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/x86/pmu.c b/x86/pmu.c index d97309d7b8a3..1f81d96030e4 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -18,6 +18,11 @@ #define EXPECTED_INSTR 17 #define EXPECTED_BRNCH 5 + +/* Enable GLOBAL_CTRL + disable GLOBAL_CTRL instructions */ +#define EXTRA_INSTRNS (3 + 3) +#define LOOP_INSTRNS (N * 10 + EXTRA_INSTRNS) +#define LOOP_BRANCHES (N) #define LOOP_ASM(_wrmsr) \ _wrmsr "\n\t" \ "mov %%ecx, %%edi; mov %%ebx, %%ecx;\n\t" \ @@ -121,6 +126,24 @@ static inline void loop(u64 cntrs) __precise_loop(cntrs); } +static void adjust_events_range(struct pmu_event *gp_events, + int instruction_idx, int branch_idx) +{ + /* + * If HW supports GLOBAL_CTRL MSR, enabling and disabling PMCs are + * moved in __precise_loop(). Thus, instructions and branches events + * can be verified against a precise count instead of a rough range. + */ + if (this_cpu_has_perf_global_ctrl()) { + /* instructions event */ + gp_events[instruction_idx].min = LOOP_INSTRNS; + gp_events[instruction_idx].max = LOOP_INSTRNS; + /* branches event */ + gp_events[branch_idx].min = LOOP_BRANCHES; + gp_events[branch_idx].max = LOOP_BRANCHES; + } +} + volatile uint64_t irq_received; static void cnt_overflow(isr_regs_t *regs) @@ -821,6 +844,9 @@ static void check_invalid_rdpmc_gp(void) int main(int ac, char **av) { + int instruction_idx; + int branch_idx; + setup_vm(); handle_irq(PMI_VECTOR, cnt_overflow); buf = malloc(N*64); @@ -834,13 +860,18 @@ int main(int ac, char **av) } gp_events = (struct pmu_event *)intel_gp_events; gp_events_size = sizeof(intel_gp_events)/sizeof(intel_gp_events[0]); + instruction_idx = INTEL_INSTRUCTIONS_IDX; + branch_idx = INTEL_BRANCHES_IDX; report_prefix_push("Intel"); set_ref_cycle_expectations(); } else { gp_events_size = sizeof(amd_gp_events)/sizeof(amd_gp_events[0]); gp_events = (struct pmu_event *)amd_gp_events; + instruction_idx = AMD_INSTRUCTIONS_IDX; + branch_idx = AMD_BRANCHES_IDX; report_prefix_push("AMD"); } + adjust_events_range(gp_events, instruction_idx, branch_idx); printf("PMU version: %d\n", pmu.version); printf("GP counters: %d\n", pmu.nr_gp_counters); From patchwork Fri Apr 19 03:52:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635641 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5ECC07C6C8; Fri, 19 Apr 2024 03:46:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498373; cv=none; b=V2rAptZRhocJx5Xn97UCQHZL/tkdZlyxpRYkFWKk65gruHWTt8N7fAnDveSpGIUgZqLdtdBur7I/GCa4m7MCB2EBUBmCejklmPQgCGvjzM9N266EAyy9Ye0f8hJXpf7JqAq13Ail7TXAZ6Mcosr7GuG7K8wq+Fik7L2vZXD0SGc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498373; c=relaxed/simple; bh=IYpNCAzV0Bjh1hMoxtoJMYlAI9fDHjiRenI3de/8ODs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=h5niA9el2ZEg+oVgitdE7eD4504VInZF53s4wshf5BPMjm4STNQmQPjGRYJvHFQVL7MbEh5RFYIPfKBWnWNXyRo2jAlVKnqoJrcpgZt9Mn6YMhXkLl6pnpTNYsYsveYLEqLdefKSdsMQLv+HOa7sB7xb3k0e3x5BqbhNlIIrOtI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=LwLrAjP7; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="LwLrAjP7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498372; x=1745034372; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IYpNCAzV0Bjh1hMoxtoJMYlAI9fDHjiRenI3de/8ODs=; b=LwLrAjP7R92eSbb//81VznQ1aLYooOca2OJgmPS7+2YoYyFZeq/Lw0+m ePCGlzHhJWY2en5yPgZefrNoWz99iUuEjc3gVFIKR7HoaSrJ+lDh3Kkbd EPVNgE8qlP8SXjX1bwsSqXrWvMBUKwgC86Ug/++Q3j4nYlCUdAHEJgvJK +PP21w4TuieLh8plGhBORh+34kTQyX9/9zhqFLrF50/yI0CBBuE/gaHyO wKokPwEmx4Z2LKolUsayMkYzQTLvnKTR+1SgHugdn+2kunSQC+QtCrwoy 4PO46tEX+SRFLDqryZBh+0CjQvwz9AiuDhXQ5A+ZFuR7XYRvgdVyT9Cc1 w==; X-CSE-ConnectionGUID: 27aaRiNCRZau9CuxTOufQw== X-CSE-MsgGUID: 1cZp3zSfRWmyIbFMijmw5g== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565493" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565493" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:46:12 -0700 X-CSE-ConnectionGUID: wc1/7mxXSP+TRHi8pOUmtQ== X-CSE-MsgGUID: SE7hlfXoSumW+tWR1aFrsg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410288" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:46:09 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 13/17] x86: pmu: Improve LLC misses event verification Date: Fri, 19 Apr 2024 11:52:29 +0800 Message-Id: <20240419035233.3837621-14-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When running pmu test on SPR, sometimes the following failure is reported. 1 <= 0 <= 1000000 FAIL: Intel: llc misses-4 Currently The LLC misses occurring only depends on probability. It's possible that there is no LLC misses happened in the whole loop(), especially along with processors have larger and larger cache size just like what we observed on SPR. Thus, add clflush instruction into the loop() asm blob and ensure once LLC miss is triggered at least. Suggested-by: Jim Mattson Signed-off-by: Dapeng Mi --- x86/pmu.c | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/x86/pmu.c b/x86/pmu.c index 1f81d96030e4..fcae60d33966 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -19,19 +19,30 @@ #define EXPECTED_BRNCH 5 -/* Enable GLOBAL_CTRL + disable GLOBAL_CTRL instructions */ -#define EXTRA_INSTRNS (3 + 3) +/* Enable GLOBAL_CTRL + disable GLOBAL_CTRL + clflush/mfence instructions */ +#define EXTRA_INSTRNS (3 + 3 + 2) #define LOOP_INSTRNS (N * 10 + EXTRA_INSTRNS) #define LOOP_BRANCHES (N) -#define LOOP_ASM(_wrmsr) \ +#define LOOP_ASM(_wrmsr, _clflush) \ _wrmsr "\n\t" \ "mov %%ecx, %%edi; mov %%ebx, %%ecx;\n\t" \ + _clflush "\n\t" \ + "mfence;\n\t" \ "1: mov (%1), %2; add $64, %1;\n\t" \ "nop; nop; nop; nop; nop; nop; nop;\n\t" \ "loop 1b;\n\t" \ "mov %%edi, %%ecx; xor %%eax, %%eax; xor %%edx, %%edx;\n\t" \ _wrmsr "\n\t" +#define _loop_asm(_wrmsr, _clflush) \ +do { \ + asm volatile(LOOP_ASM(_wrmsr, _clflush) \ + : "=b"(tmp), "=r"(tmp2), "=r"(tmp3) \ + : "a"(eax), "d"(edx), "c"(global_ctl), \ + "0"(N), "1"(buf) \ + : "edi"); \ +} while (0) + typedef struct { uint32_t ctr; uint32_t idx; @@ -87,14 +98,17 @@ char *buf; static struct pmu_event *gp_events; static unsigned int gp_events_size; - static inline void __loop(void) { unsigned long tmp, tmp2, tmp3; + u32 global_ctl = 0; + u32 eax = 0; + u32 edx = 0; - asm volatile(LOOP_ASM("nop") - : "=c"(tmp), "=r"(tmp2), "=r"(tmp3) - : "0"(N), "1"(buf)); + if (this_cpu_has(X86_FEATURE_CLFLUSH)) + _loop_asm("nop", "clflush (%1)"); + else + _loop_asm("nop", "nop"); } /* @@ -107,15 +121,14 @@ static inline void __loop(void) static inline void __precise_loop(u64 cntrs) { unsigned long tmp, tmp2, tmp3; - unsigned int global_ctl = pmu.msr_global_ctl; + u32 global_ctl = pmu.msr_global_ctl; u32 eax = cntrs & (BIT_ULL(32) - 1); u32 edx = cntrs >> 32; - asm volatile(LOOP_ASM("wrmsr") - : "=b"(tmp), "=r"(tmp2), "=r"(tmp3) - : "a"(eax), "d"(edx), "c"(global_ctl), - "0"(N), "1"(buf) - : "edi"); + if (this_cpu_has(X86_FEATURE_CLFLUSH)) + _loop_asm("wrmsr", "clflush (%1)"); + else + _loop_asm("wrmsr", "nop"); } static inline void loop(u64 cntrs) From patchwork Fri Apr 19 03:52:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635642 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 499B47E103; Fri, 19 Apr 2024 03:46:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498376; cv=none; b=I8jmLbYBCErlX5KXrTdyR0hkhuvjO4Qw70tu/N10QWi98dPif3k2MGlX5R4OSEKRz91boQWS1QJmSrkqs/k/nQIWwkxCKLl+Igct+A8qzjQlzUhwqITKRTSJXvB8sOUmUD2ZWbapxVu1+LVgqnHDzeeLYLABB0OI506w0O+vGh4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498376; c=relaxed/simple; bh=cEod3+83Wa+vP4Dtlze3LzHehXl/2U9UGWLeIZ52yNQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=C+EOjfY/I0GiGlx03NVxgChYaigCkiMNPc3lVnOA8xTAC0PU9ULJyWskjN1/06nactVk9pklEe39gFA5Po9RsQS/HxHFJsj4Ecxn3ULUBRCW/nv+P7ugZoUQcMwvEmq+OaQJKluL7GAUkhmHLsCOu3AtIA2MlOyadowcHuYObHI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=V4Fq1mZ6; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="V4Fq1mZ6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498375; x=1745034375; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cEod3+83Wa+vP4Dtlze3LzHehXl/2U9UGWLeIZ52yNQ=; b=V4Fq1mZ6wkcLzs1e9rX2PM70Pf7uS6D9AIgTYvZZ8b3hbnmazM7P6j+T dB3vToasmXXgaw34f1lb2k1jZ7h8aQItVMy/Lx7iYWNNvtqc7GrXo2479 3Bba0cyrFKmnBJ3rksZyyOyxEBGhwj1eW4IX6F6qEmnY5B/HHPQbRDwZn 4uJ3O/LhB284TIddUrmVkyo4xGsrXB52pkKFVyqNth4bR8TcGszVN1rYc KXpIpZi+tZIJoktoWjY6m/7NiXle0pqYJqGHZmHOTbAPvfUIRGeIK39q1 DABoAmpXaf0At57KgEIRoGFmyuWAINBrZL0+cyHWGlmSa4p4uik08/Yg2 A==; X-CSE-ConnectionGUID: tUYQ2KyyTz+jHZ/PU1mf5A== X-CSE-MsgGUID: v21ZTCm/SXi5gKH4cpdOoA== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565501" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565501" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:46:15 -0700 X-CSE-ConnectionGUID: 5GtYuA+0ROGNMihn9IToDw== X-CSE-MsgGUID: zO/LMXMtSyWnoNlWDngTpA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410299" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:46:12 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 14/17] x86: pmu: Adjust lower boundary of llc-misses event to 0 for legacy CPUs Date: Fri, 19 Apr 2024 11:52:30 +0800 Message-Id: <20240419035233.3837621-15-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For these legacy Intel CPUs without clflush/clflushopt support, there is on way to force to trigger a LLC miss and the measured llc misses is possible to be 0. Thus adjust the lower boundary of llc-misses event to 0 to avoid possible false positive. Signed-off-by: Dapeng Mi --- x86/pmu.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/x86/pmu.c b/x86/pmu.c index fcae60d33966..adc7e6c640c1 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -81,6 +81,7 @@ struct pmu_event { enum { INTEL_INSTRUCTIONS_IDX = 1, INTEL_REF_CYCLES_IDX = 2, + INTEL_LLC_MISSES_IDX = 4, INTEL_BRANCHES_IDX = 5, }; @@ -875,6 +876,15 @@ int main(int ac, char **av) gp_events_size = sizeof(intel_gp_events)/sizeof(intel_gp_events[0]); instruction_idx = INTEL_INSTRUCTIONS_IDX; branch_idx = INTEL_BRANCHES_IDX; + + /* + * For legacy Intel CPUS without clflush/clflushopt support, + * there is no way to force to trigger a LLC miss, thus set + * the minimum value to 0 to avoid false positives. + */ + if (!this_cpu_has(X86_FEATURE_CLFLUSH)) + gp_events[INTEL_LLC_MISSES_IDX].min = 0; + report_prefix_push("Intel"); set_ref_cycle_expectations(); } else { From patchwork Fri Apr 19 03:52:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635643 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2821E7E794; Fri, 19 Apr 2024 03:46:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498379; cv=none; b=R0HAd4+E3cKMi062jpYWC5vJKKiVx/IEF0IlDBpJ9bSML9csmrbv5dnPSKfklFcOK6EL6J1+TUKvjoT/u8QbbKoBb9by01scgvDRUybxwThiTqND92rPQqNWDbQVysdY6uct4X390B1e51GzlxAn/74HHzYGcCfoOo2CcawMYKo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498379; c=relaxed/simple; bh=TQbV1T1MhLXK57NxsUL2tKSbMLenSbvBlMwDGIbHruw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MzKdjZbdgdpqeWUkNUUzqX2q2BLRGBWwcW72QsHnKwv0GnR+G1rUPl2VQUg1ayXnkVNF8thL8Fb3znIMjrpE7KmVl2kU4h+Ll1Ha8HEEX9DktsYq5jMzB4DcZidSy0TEOmrxsBeV4n8nv9x2+BQvMIw/oyMDDkfPHBwxJzPHlA8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ci7pn+oT; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ci7pn+oT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498378; x=1745034378; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TQbV1T1MhLXK57NxsUL2tKSbMLenSbvBlMwDGIbHruw=; b=ci7pn+oTcK3yLRwCr1zMlrHaDmRHGTbiAsq3dmNSFl4Vk9X2atfNgckn KjuE5E5k3ACqvehjnS1xQNyliwMxtzjXrXmQZ6onUjUqLX4WTHg+9UY7h RQfpO+KQieeUyG35hAnjXcVZZzg8RkIvynZ03tqwpV59Asj6R/ikjm4/E jQc5P8z6gjziI2pT/7mswD8dTWj7sK0LyKXvJeUxwQr7Eu4WZCEzaTvbX H+lGIk46OE0S+HJamCnYoYtQRPyz3d77Qu+1uhap+rl071Yuu8AlZCrgt tjCI7Sgvrw9uXhuMNx+9aKyWMfB2RnryQTT6i6H93sP49juLWdd/GjPJY g==; X-CSE-ConnectionGUID: /sQtFKRQSzezr/wQ/jKn1Q== X-CSE-MsgGUID: R83UI8JfRCqA48Aa//7R7A== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565506" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565506" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:46:18 -0700 X-CSE-ConnectionGUID: L8CpPe8FRx60ZEcMD0VbNA== X-CSE-MsgGUID: b0YqDMtOQe+KMniaNCZwQQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410310" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:46:15 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 15/17] x86: pmu: Add IBPB indirect jump asm blob Date: Fri, 19 Apr 2024 11:52:31 +0800 Message-Id: <20240419035233.3837621-16-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently the lower boundary of branch misses event is set to 0. Strictly speaking 0 shouldn't be a valid count since it can't tell us if branch misses event counter works correctly or even disabled. Whereas it's also possible and reasonable that branch misses event count is 0 especailly for such simple loop() program with advanced branch predictor. To eliminate such ambiguity and make branch misses event verification more acccurately, an extra IBPB indirect jump asm blob is appended and IBPB command is leveraged to clear the branch target buffer and force to cause a branch miss for the indirect jump. Suggested-by: Jim Mattson Signed-off-by: Dapeng Mi --- x86/pmu.c | 55 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/x86/pmu.c b/x86/pmu.c index adc7e6c640c1..0b3dd1ba1766 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -18,25 +18,36 @@ #define EXPECTED_INSTR 17 #define EXPECTED_BRNCH 5 - -/* Enable GLOBAL_CTRL + disable GLOBAL_CTRL + clflush/mfence instructions */ -#define EXTRA_INSTRNS (3 + 3 + 2) +#define IBPB_JMP_INSTRNS 7 +#define IBPB_JMP_BRANCHES 1 +#define IBPB_JMP_ASM(_wrmsr) \ + "mov $1, %%eax; xor %%edx, %%edx;\n\t" \ + "mov $73, %%ecx;\n\t" \ + _wrmsr "\n\t" \ + "lea 2f, %%rax;\n\t" \ + "jmp *%%rax;\n\t" \ + "nop;\n\t" \ + "2: nop;\n\t" + +/* GLOBAL_CTRL enable + disable + clflush/mfence + IBPB_JMP */ +#define EXTRA_INSTRNS (3 + 3 + 2 + IBPB_JMP_INSTRNS) #define LOOP_INSTRNS (N * 10 + EXTRA_INSTRNS) -#define LOOP_BRANCHES (N) -#define LOOP_ASM(_wrmsr, _clflush) \ - _wrmsr "\n\t" \ +#define LOOP_BRANCHES (N + IBPB_JMP_BRANCHES) +#define LOOP_ASM(_wrmsr1, _clflush, _wrmsr2) \ + _wrmsr1 "\n\t" \ "mov %%ecx, %%edi; mov %%ebx, %%ecx;\n\t" \ _clflush "\n\t" \ "mfence;\n\t" \ "1: mov (%1), %2; add $64, %1;\n\t" \ "nop; nop; nop; nop; nop; nop; nop;\n\t" \ "loop 1b;\n\t" \ + IBPB_JMP_ASM(_wrmsr2) \ "mov %%edi, %%ecx; xor %%eax, %%eax; xor %%edx, %%edx;\n\t" \ - _wrmsr "\n\t" + _wrmsr1 "\n\t" -#define _loop_asm(_wrmsr, _clflush) \ +#define _loop_asm(_wrmsr1, _clflush, _wrmsr2) \ do { \ - asm volatile(LOOP_ASM(_wrmsr, _clflush) \ + asm volatile(LOOP_ASM(_wrmsr1, _clflush, _wrmsr2) \ : "=b"(tmp), "=r"(tmp2), "=r"(tmp3) \ : "a"(eax), "d"(edx), "c"(global_ctl), \ "0"(N), "1"(buf) \ @@ -99,6 +110,12 @@ char *buf; static struct pmu_event *gp_events; static unsigned int gp_events_size; +static int has_ibpb(void) +{ + return this_cpu_has(X86_FEATURE_SPEC_CTRL) || + this_cpu_has(X86_FEATURE_AMD_IBPB); +} + static inline void __loop(void) { unsigned long tmp, tmp2, tmp3; @@ -106,10 +123,14 @@ static inline void __loop(void) u32 eax = 0; u32 edx = 0; - if (this_cpu_has(X86_FEATURE_CLFLUSH)) - _loop_asm("nop", "clflush (%1)"); + if (this_cpu_has(X86_FEATURE_CLFLUSH) && has_ibpb()) + _loop_asm("nop", "clflush (%1)", "wrmsr"); + else if (this_cpu_has(X86_FEATURE_CLFLUSH)) + _loop_asm("nop", "clflush (%1)", "nop"); + else if (has_ibpb()) + _loop_asm("nop", "nop", "wrmsr"); else - _loop_asm("nop", "nop"); + _loop_asm("nop", "nop", "nop"); } /* @@ -126,10 +147,14 @@ static inline void __precise_loop(u64 cntrs) u32 eax = cntrs & (BIT_ULL(32) - 1); u32 edx = cntrs >> 32; - if (this_cpu_has(X86_FEATURE_CLFLUSH)) - _loop_asm("wrmsr", "clflush (%1)"); + if (this_cpu_has(X86_FEATURE_CLFLUSH) && has_ibpb()) + _loop_asm("wrmsr", "clflush (%1)", "wrmsr"); + else if (this_cpu_has(X86_FEATURE_CLFLUSH)) + _loop_asm("wrmsr", "clflush (%1)", "nop"); + else if (has_ibpb()) + _loop_asm("wrmsr", "nop", "wrmsr"); else - _loop_asm("wrmsr", "nop"); + _loop_asm("wrmsr", "nop", "nop"); } static inline void loop(u64 cntrs) From patchwork Fri Apr 19 03:52:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635644 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3190810A1D; Fri, 19 Apr 2024 03:46:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498382; cv=none; b=utW51BilPB/6kT1hGkaSiRIopxY4CnZJ5qvmWi/vsNd7AtVo8V2oAuX/06p/X4YWMBg9KNqn1fkpSZri+YXhgktdCloPF2YWBbZv8vD5mEea9g2Q4YAv24d5RPCg8t4LCm6Y4T5ZpVpbZqUt2GxY1SYPIFChd2RhUcu7/kvFMos= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498382; c=relaxed/simple; bh=mh1oCzu7/w0SZXmYy/roGGDEmggIS6f3yEtq/Sb39JI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jPn7P74uWfYlmSR3jZxzTKAYG8QkXmJ9+LP5H9EoY9BciMO3kCYF5bc1npfyxS1L0knTmI1a4CUfFK/+6kkbNhrLmo9dfk6VY3AZRrNM51AlfmBCdTGQZ16cIpev46ETRL60W0et/jLPAkYMxBPP7eO5JiI1QJ1UeuGLVDeItW8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=MOgLMGag; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="MOgLMGag" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498381; x=1745034381; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mh1oCzu7/w0SZXmYy/roGGDEmggIS6f3yEtq/Sb39JI=; b=MOgLMGag/8YTxi2Ag1Ax6LL/bAfPmAxPt+ap/YzqEPxC6/Ljou0qJycj 4WuZKB8sLhgU5SSp150ZAADsvocGpTlV4luLvsmZ20Hk9P3+B8MfOvmBo IzekD50TgVVOovKFVVkVUmCmNKlrdzzlRvO5RX9wzQdUV6ed9/yx2+DbZ RHbRsomEx3I/8Yd2j+mrdRdXbCTXjAZPdPn3+yd+uZY29f1vnGzRk+UK+ ArwtDvRoszTZx5Ljfk4QXUNyi//MXLiCwZwtOhDu0nGA1MC3cUKZ2BPc3 vLKbqm7NnmOTueeZI4vaBFuXJer91UURkc7QZ9ERO5xSvsume61C1onmL g==; X-CSE-ConnectionGUID: CePLvaFITY69WAn2iOBJsw== X-CSE-MsgGUID: +Y9Pf1thSr6+w2petu086w== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565511" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565511" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:46:21 -0700 X-CSE-ConnectionGUID: iLnSHLUHTkmCCHQVQu//rQ== X-CSE-MsgGUID: XBQvYtMDTBSmvkEiGqvnZQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410323" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:46:18 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 16/17] x86: pmu: Adjust lower boundary of branch-misses event Date: Fri, 19 Apr 2024 11:52:32 +0800 Message-Id: <20240419035233.3837621-17-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Since the IBPB command is added to force to trigger a branch miss at least, the lower boundary of branch misses event is increased to 1 by default. For these CPUs without IBPB support, adjust dynamically the lower boundary to 0 to avoid false positive. Signed-off-by: Dapeng Mi --- x86/pmu.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/x86/pmu.c b/x86/pmu.c index 0b3dd1ba1766..e0da522c004b 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -73,12 +73,12 @@ struct pmu_event { {"llc references", 0x4f2e, 1, 2*N}, {"llc misses", 0x412e, 1, 1*N}, {"branches", 0x00c4, 1*N, 1.1*N}, - {"branch misses", 0x00c5, 0, 0.1*N}, + {"branch misses", 0x00c5, 1, 0.1*N}, }, amd_gp_events[] = { {"core cycles", 0x0076, 1*N, 50*N}, {"instructions", 0x00c0, 10*N, 10.2*N}, {"branches", 0x00c2, 1*N, 1.1*N}, - {"branch misses", 0x00c3, 0, 0.1*N}, + {"branch misses", 0x00c3, 1, 0.1*N}, }, fixed_events[] = { {"fixed 0", MSR_CORE_PERF_FIXED_CTR0, 10*N, 10.2*N}, {"fixed 1", MSR_CORE_PERF_FIXED_CTR0 + 1, 1*N, 30*N}, @@ -94,6 +94,7 @@ enum { INTEL_REF_CYCLES_IDX = 2, INTEL_LLC_MISSES_IDX = 4, INTEL_BRANCHES_IDX = 5, + INTEL_BRANCH_MISS_IDX = 6, }; /* @@ -103,6 +104,7 @@ enum { enum { AMD_INSTRUCTIONS_IDX = 1, AMD_BRANCHES_IDX = 2, + AMD_BRANCH_MISS_IDX = 3, }; char *buf; @@ -166,7 +168,8 @@ static inline void loop(u64 cntrs) } static void adjust_events_range(struct pmu_event *gp_events, - int instruction_idx, int branch_idx) + int instruction_idx, int branch_idx, + int branch_miss_idx) { /* * If HW supports GLOBAL_CTRL MSR, enabling and disabling PMCs are @@ -181,6 +184,17 @@ static void adjust_events_range(struct pmu_event *gp_events, gp_events[branch_idx].min = LOOP_BRANCHES; gp_events[branch_idx].max = LOOP_BRANCHES; } + + /* + * For CPUs without IBPB support, no way to force to trigger a + * branch miss and the measured branch misses is possible to be + * 0. Thus overwrite the lower boundary of branch misses event + * to 0 to avoid false positive. + */ + if (!has_ibpb()) { + /* branch misses event */ + gp_events[branch_miss_idx].min = 0; + } } volatile uint64_t irq_received; @@ -885,6 +899,7 @@ int main(int ac, char **av) { int instruction_idx; int branch_idx; + int branch_miss_idx; setup_vm(); handle_irq(PMI_VECTOR, cnt_overflow); @@ -901,6 +916,7 @@ int main(int ac, char **av) gp_events_size = sizeof(intel_gp_events)/sizeof(intel_gp_events[0]); instruction_idx = INTEL_INSTRUCTIONS_IDX; branch_idx = INTEL_BRANCHES_IDX; + branch_miss_idx = INTEL_BRANCH_MISS_IDX; /* * For legacy Intel CPUS without clflush/clflushopt support, @@ -917,9 +933,10 @@ int main(int ac, char **av) gp_events = (struct pmu_event *)amd_gp_events; instruction_idx = AMD_INSTRUCTIONS_IDX; branch_idx = AMD_BRANCHES_IDX; + branch_miss_idx = AMD_BRANCH_MISS_IDX; report_prefix_push("AMD"); } - adjust_events_range(gp_events, instruction_idx, branch_idx); + adjust_events_range(gp_events, instruction_idx, branch_idx, branch_miss_idx); printf("PMU version: %d\n", pmu.version); printf("GP counters: %d\n", pmu.nr_gp_counters); From patchwork Fri Apr 19 03:52:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 13635645 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 348B67F7F3; Fri, 19 Apr 2024 03:46:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498385; cv=none; b=gReezH3LIZNu5jzaKw0LC5/Y0ZFqbTS7ZAMih1Ooz12Qri5K3osiPoqIIJqrfggygazCJHv4Z1uFVt74DJJwdKDSAUf61z99Wtv2w74d69aWu/nV/D+EvBsoaIkgb4z4AzQCUGOs/S74KBJk0X5fXMHDe/12be2N0qnDY8WeQsw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713498385; c=relaxed/simple; bh=BOCLUCvoA9G68ezGtgdPML+FLT+IPlvWgEm4jrl3wZ0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FFShIcOKZcCzAKjlKw/st5u7ReQ5gh2xOCJ/XXjnn9SO9VMHNmSuVwCp2dzt8it9knpsuE/gC+GnocuRKn4dC+G+DOGmSzIYO7IUHDkqzgru4DsBprXIaVf16nxZSSHfu6vx1PTOR+UVI1Ye1qOxeQFAuBkvT/KGYIclCeDuIBU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=OIaSYVNR; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="OIaSYVNR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713498384; x=1745034384; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BOCLUCvoA9G68ezGtgdPML+FLT+IPlvWgEm4jrl3wZ0=; b=OIaSYVNReAEV4ErJWbnBoj1irinwA7QDKPsE8NLhtGJfQ+MaqjuOgEm9 8JFyTB7WrOXaWU13sfP3BiYRpdfXRCoYxldMFa2yIKljkI14Y4f9G3afx hTZRSYBIrgUUvNIAo+SSJPHS70UmMB4y44B7rR+ITuNxMeMIG17/FbBuY cceo2ihB169k5T3jSio9sNpJREBzJplnc5tmxcd61DGxHUX7qw6LVUIia 0/Ui2O2fKJLoHOXpPE0wwDF1TW/JhOSmbbSOG+G5R6Fz6uazvNSy3OtXj 8BSKMSGXfHQihjQIPLG7mM73rA19eA3/Vex5jvDy2sBRSBJUslpKAdKgv g==; X-CSE-ConnectionGUID: T6PqFqejRJypdV6FLXpTMw== X-CSE-MsgGUID: hULK0nvkQ9OLmC61io0snw== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="31565516" X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="31565516" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 20:46:24 -0700 X-CSE-ConnectionGUID: jlJk7cBbRPer8HCEfwmGnQ== X-CSE-MsgGUID: WVXnpKhDTVOGVm/e0gRkGA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,213,1708416000"; d="scan'208";a="54410340" Received: from unknown (HELO dmi-pnp-i7.sh.intel.com) ([10.239.159.155]) by fmviesa001.fm.intel.com with ESMTP; 18 Apr 2024 20:46:21 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson , Mingwei Zhang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiong Zhang , Zhenyu Wang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v4 17/17] x86: pmu: Optimize emulated instruction validation Date: Fri, 19 Apr 2024 11:52:33 +0800 Message-Id: <20240419035233.3837621-18-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> References: <20240419035233.3837621-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For support CPUs supporting PERF_GLOBAL_CTRL MSR, the validation for emulated instruction can be improved to check against precise counts for instructions and branches events instead of a rough range. Move enabling and disabling PERF_GLOBAL_CTRL MSR into kvm_fep_asm blob, thus instructions and branches events can be verified against precise counts. Signed-off-by: Dapeng Mi --- x86/pmu.c | 108 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 65 insertions(+), 43 deletions(-) diff --git a/x86/pmu.c b/x86/pmu.c index e0da522c004b..dd83f157b35c 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -13,11 +13,6 @@ #define N 1000000 -// These values match the number of instructions and branches in the -// assembly block in check_emulated_instr(). -#define EXPECTED_INSTR 17 -#define EXPECTED_BRNCH 5 - #define IBPB_JMP_INSTRNS 7 #define IBPB_JMP_BRANCHES 1 #define IBPB_JMP_ASM(_wrmsr) \ @@ -54,6 +49,40 @@ do { \ : "edi"); \ } while (0) +/* the number of instructions and branches of the kvm_fep_asm() blob */ +#define KVM_FEP_INSTR 22 +#define KVM_FEP_BRNCH 5 + +/* + * KVM_FEP is a magic prefix that forces emulation so + * 'KVM_FEP "jne label\n"' just counts as a single instruction. + */ +#define kvm_fep_asm(_wrmsr) \ +do { \ + asm volatile( \ + _wrmsr "\n\t" \ + "mov %%ecx, %%edi;\n\t" \ + "mov $0x0, %%eax;\n\t" \ + "cmp $0x0, %%eax;\n\t" \ + KVM_FEP "jne 1f\n\t" \ + KVM_FEP "jne 1f\n\t" \ + KVM_FEP "jne 1f\n\t" \ + KVM_FEP "jne 1f\n\t" \ + KVM_FEP "jne 1f\n\t" \ + "mov $0xa, %%eax; cpuid;\n\t" \ + "mov $0xa, %%eax; cpuid;\n\t" \ + "mov $0xa, %%eax; cpuid;\n\t" \ + "mov $0xa, %%eax; cpuid;\n\t" \ + "mov $0xa, %%eax; cpuid;\n\t" \ + "1: mov %%edi, %%ecx; \n\t" \ + "xor %%eax, %%eax; \n\t" \ + "xor %%edx, %%edx;\n\t" \ + _wrmsr "\n\t" \ + : \ + : "a"(eax), "d"(edx), "c"(ecx) \ + : "ebx", "edi"); \ +} while (0) + typedef struct { uint32_t ctr; uint32_t idx; @@ -639,6 +668,7 @@ static void check_running_counter_wrmsr(void) static void check_emulated_instr(void) { + u32 eax, edx, ecx; uint64_t status, instr_start, brnch_start; uint64_t gp_counter_width = (1ull << pmu.gp_counter_width) - 1; unsigned int branch_idx = pmu.is_intel ? @@ -646,6 +676,7 @@ static void check_emulated_instr(void) unsigned int instruction_idx = pmu.is_intel ? INTEL_INSTRUCTIONS_IDX : AMD_INSTRUCTIONS_IDX; + pmu_counter_t brnch_cnt = { .ctr = MSR_GP_COUNTERx(0), /* branch instructions */ @@ -661,55 +692,46 @@ static void check_emulated_instr(void) if (this_cpu_has_perf_global_status()) pmu_clear_global_status(); - start_event(&brnch_cnt); - start_event(&instr_cnt); + __start_event(&brnch_cnt, 0); + __start_event(&instr_cnt, 0); - brnch_start = -EXPECTED_BRNCH; - instr_start = -EXPECTED_INSTR; + brnch_start = -KVM_FEP_BRNCH; + instr_start = -KVM_FEP_INSTR; wrmsr(MSR_GP_COUNTERx(0), brnch_start & gp_counter_width); wrmsr(MSR_GP_COUNTERx(1), instr_start & gp_counter_width); - // KVM_FEP is a magic prefix that forces emulation so - // 'KVM_FEP "jne label\n"' just counts as a single instruction. - asm volatile( - "mov $0x0, %%eax\n" - "cmp $0x0, %%eax\n" - KVM_FEP "jne label\n" - KVM_FEP "jne label\n" - KVM_FEP "jne label\n" - KVM_FEP "jne label\n" - KVM_FEP "jne label\n" - "mov $0xa, %%eax\n" - "cpuid\n" - "mov $0xa, %%eax\n" - "cpuid\n" - "mov $0xa, %%eax\n" - "cpuid\n" - "mov $0xa, %%eax\n" - "cpuid\n" - "mov $0xa, %%eax\n" - "cpuid\n" - "label:\n" - : - : - : "eax", "ebx", "ecx", "edx"); - if (this_cpu_has_perf_global_ctrl()) - wrmsr(pmu.msr_global_ctl, 0); + if (this_cpu_has_perf_global_ctrl()) { + eax = BIT(0) | BIT(1); + ecx = pmu.msr_global_ctl; + edx = 0; + kvm_fep_asm("wrmsr"); + } else { + eax = ecx = edx = 0; + kvm_fep_asm("nop"); + } - stop_event(&brnch_cnt); - stop_event(&instr_cnt); + __stop_event(&brnch_cnt); + __stop_event(&instr_cnt); // Check that the end count - start count is at least the expected // number of instructions and branches. - report(instr_cnt.count - instr_start >= EXPECTED_INSTR, - "instruction count"); - report(brnch_cnt.count - brnch_start >= EXPECTED_BRNCH, - "branch count"); + if (this_cpu_has_perf_global_ctrl()) { + report(instr_cnt.count - instr_start == KVM_FEP_INSTR, + "instruction count"); + report(brnch_cnt.count - brnch_start == KVM_FEP_BRNCH, + "branch count"); + } else { + report(instr_cnt.count - instr_start >= KVM_FEP_INSTR, + "instruction count"); + report(brnch_cnt.count - brnch_start >= KVM_FEP_BRNCH, + "branch count"); + } + if (this_cpu_has_perf_global_status()) { // Additionally check that those counters overflowed properly. status = rdmsr(pmu.msr_global_status); - report(status & 1, "branch counter overflow"); - report(status & 2, "instruction counter overflow"); + report(status & BIT_ULL(0), "branch counter overflow"); + report(status & BIT_ULL(1), "instruction counter overflow"); } report_prefix_pop();