Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6904040rdb; Tue, 2 Jan 2024 19:10:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IHOgtEmavhK8nCV42BqgJ4RMPybFtNkedKMT2hLvSEC13AxdNHb0pPQNQaCuMdijIfPWyRf X-Received: by 2002:a17:906:701:b0:a23:5cdd:fe31 with SMTP id y1-20020a170906070100b00a235cddfe31mr6774960ejb.117.1704251457617; Tue, 02 Jan 2024 19:10:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704251457; cv=none; d=google.com; s=arc-20160816; b=kcE9sQ6gdCU/gQz9f3Ov+K7KGwL/JP/W+MeBBCczm+hc63mip4jxSXjdp7ncZP17s2 Z9R1OKFG0DtzgIsiAITxXjAOZ1t1eNb0RN2zhPp+irscwI63qjQ7lz0IYqSQNfMeT17t 2Llx7QIIq/Fd58iyy2r4Mr1cqGUG8pq4mJiR4fXe6TcRbd9rL9shjYlWdZ4JQR/eWFwL MzrJGOfNrI3ZXxlK/8dDOngwe5VaxFDkiGHJQppQiQ84cBzuhYzQ9vvNhVOPq+LfxaKs 9vNqja1szZ9Tqofp1hzuNA/55Hn5mtkvxmY7uiCqutjZ4taCb6x49PTUGCVGBYzO4T2f 8/fg== ARC-Message-Signature: i=1; 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=mRVHqXiQs/mwc8unqleB0bjA0K8jN3uEiaReD9ZgM84=; fh=YsfTl7qN5yLiZ/GhYjuBdDgWBv8FNcMbD5266uBeHkY=; b=xUuO9Fai4j9Zanmtc9gxGG/37TkBGLiRetzR+3qLVjsmXKxJa/vTfboMa/0zf9plDU 09v5gyx1JJnRjsrHsExubMezkeCJtX0iPZ/ed28OVUMy0ytGu7C/YD7pOx/eb3Es0u68 2l/x0hLOcc3FBPz51b7zOaJ7gXNOt8yprTfxvvjzOiwwl3+dyizj14XwsDgiQYOBLgCN yF09MDXw/FWk9+oqjoV58oewD3XMVxQ+zhEENeXH4wJBbQqGZvhjz49A6i7K7b7JjXwe gXAoo7Zvi54ZZwHOdgZ1O5R+pMZKvMdQ8UiYfvL6mZZTguyjtPuJPAXF4mHNojExd1qk Kgow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lTK7QezD; spf=pass (google.com: domain of linux-kernel+bounces-15118-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15118-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id zo15-20020a170906ff4f00b00a27b3cfb05asi3085890ejb.386.2024.01.02.19.10.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 19:10:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-15118-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lTK7QezD; spf=pass (google.com: domain of linux-kernel+bounces-15118-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15118-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 83C8C1F238E8 for ; Wed, 3 Jan 2024 03:10:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1B27A18645; Wed, 3 Jan 2024 03:09:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="lTK7QezD" X-Original-To: linux-kernel@vger.kernel.org Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) (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 A97FF182DD; Wed, 3 Jan 2024 03:09:40 +0000 (UTC) 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=1704251381; x=1735787381; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZTgziGb6S+JjPB/4NsS2emVW8oxagsATmMkk72RlKZU=; b=lTK7QezDnVYzUoi2x6HC5fc5GGc7Br8ncihslZTP+uABlNp+2UPF+9D8 Cu2b5/pHVxuW1j57diNjKvwuxL69rOlYokdVs9dNOz09FZkkjkXN4Vscv MJ5bCCTeWn+pk103h3HgeQb7H+JxZL406RxDSR/L3QG1+bWDBEKQUlr7v w7FRo3TNRcEXzxDRyzsx5sqTFNEuJx4LUfedjlq7hEDw2dFTi3wTsgdxX cRcE6j5lQ+RnkSJTixCLrxySrQygodUkNsAXZUDxbxH0/59pZlMBVl+sY GTCEPKY0JwFD+czIZ8RTXwXo9EDli1jMWqQFczIG5yRcOBSPN3y71Etde g==; X-IronPort-AV: E=McAfee;i="6600,9927,10941"; a="10343131" X-IronPort-AV: E=Sophos;i="6.04,326,1695711600"; d="scan'208";a="10343131" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jan 2024 19:09:41 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10941"; a="729665930" X-IronPort-AV: E=Sophos;i="6.04,326,1695711600"; d="scan'208";a="729665930" Received: from dmi-pnp-i7.sh.intel.com ([10.239.159.155]) by orsmga003.jf.intel.com with ESMTP; 02 Jan 2024 19:09:37 -0800 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Jim Mattson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Zhenyu Wang , Zhang Xiong , Mingwei Zhang , Like Xu , Jinrong Liang , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch v3 03/11] x86: pmu: Add asserts to warn inconsistent fixed events and counters Date: Wed, 3 Jan 2024 11:14:01 +0800 Message-Id: <20240103031409.2504051-4-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240103031409.2504051-1-dapeng1.mi@linux.intel.com> References: <20240103031409.2504051-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. 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 a13b8a8398c6..a42fff8d8b36 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 { + int idx = cnt->ctr - MSR_CORE_PERF_FIXED_CTR0; + + assert(idx < ARRAY_SIZE(fixed_events)); + return &fixed_events[idx]; + } return (void*)0; } @@ -245,6 +249,7 @@ static void check_fixed_counters(void) }; int i; + assert(pmu.nr_fixed_counters <= ARRAY_SIZE(fixed_events)); for (i = 0; i < pmu.nr_fixed_counters; i++) { cnt.ctr = fixed_events[i].unit_sel; measure_one(&cnt); @@ -266,6 +271,7 @@ static void check_counters_many(void) gp_events[i % gp_events_size].unit_sel; n++; } + assert(pmu.nr_fixed_counters <= ARRAY_SIZE(fixed_events)); for (i = 0; i < pmu.nr_fixed_counters; i++) { cnt[n].ctr = fixed_events[i].unit_sel; cnt[n].config = EVNTSEL_OS | EVNTSEL_USR; -- 2.34.1