Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp520246lqt; Fri, 19 Apr 2024 02:51:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWudDDKHneeYGJgDuMykA5duY729Am88UxVeAyUCIKaeQHsl0sgCPQ/1UR8On9/YpPAKwv1dHN9bkpx8DMjYilE25kD2yEBvFyFM+Dn8g== X-Google-Smtp-Source: AGHT+IGvk70BBN3rnM55Ul/YpXPR1kqM+mvXlBDuYsb2x++tbeALxf+xnG4TUvgpkbV59SoVUoak X-Received: by 2002:a17:90a:a40f:b0:2a2:8ed7:da34 with SMTP id y15-20020a17090aa40f00b002a28ed7da34mr1482215pjp.1.1713520275946; Fri, 19 Apr 2024 02:51:15 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713520275; cv=pass; d=google.com; s=arc-20160816; b=ovF5aTSWAVFvpcrdn+xJBYnjGSZi5QX9rSBL/g1UeXXoR6reP/rd+ahNk4/KRBumZH WdGu7olCxVlPa3c0HW5lCvqx2PX7k3QLLYSw0pey3QXe4/tE129W/QobTELfUXPdiVqN y6rqX3oMaXsTsX3auYrS2EGiTN0hn1nYLmSg57o9QHuejoq7S6w6VXvT7CIvwJ3yTkKz IsPwVpGFKGA9GRkZsUF+JHdvqycIvLdAHrkIz7JXfubK/Hxsa8TVF9saB/ztGCWe50UX aINuc3ahX/LK75CkbUi+M00NhJiBXRYoCl4RuDBr9mJZz1Q2WXPvePTlCaB9sdmScXAS dupw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Tifl/WO+iW1epwqO3lxnS/TTeBJTZwSA/CYFZk+rdJw=; fh=3qX1+dlN8PYECDPP2iJ6RyaKnRhI0MQxvVbehX3gfaw=; b=IJQsyjhwVMOwLCoaHxBmuvthOE/xWxoJbfGh3dmTrSh4QxcU6vAf8tRU9CEFzeCkOd sz+Hpb9QN4rZETYZT7Pw8vFbcr2+vQ8Ite7VIWMOPJ/UDr3QZrcuWJn35U4JMYRBNr+g KsFKliAxJPNpWA2KdLZ+tk0PjMKvPfysdBjFuX9W0+SKF5JbLwcxrZzIhsY8HcQZdPaP K1wF/K26CZ06xccd15cryR0E9sTPNlMjAKf39l2P10swhLFCkOwsgZSj15ZgkEd66iQd wBHpgX1tq5Wmobi/qZqZmHbexq81lPGT1KPWRvZr3AZqxIqEtafCrOP/oq7UKIcn9ScT jl4A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BZRjxdVc; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-150982-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-150982-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id mz20-20020a17090b379400b002a47006b83bsi3053466pjb.141.2024.04.19.02.51.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 02:51:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-150982-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BZRjxdVc; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-150982-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-150982-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id ABC4EB22FF7 for ; Fri, 19 Apr 2024 03:47:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EB9E82AE88; Fri, 19 Apr 2024 03:45:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="BZRjxdVc" 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 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: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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(); -- 2.34.1