Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1723004rda; Tue, 24 Oct 2023 00:52:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH6fxbsaDV32UKQEY79RjjbYJ7oNhIRZaF7IByKgqxvqw0vPV5LLdoIs2B4000b/RW6ExLx X-Received: by 2002:a05:6a00:180c:b0:6b8:3375:4885 with SMTP id y12-20020a056a00180c00b006b833754885mr10309973pfa.1.1698133944083; Tue, 24 Oct 2023 00:52:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698133944; cv=none; d=google.com; s=arc-20160816; b=Jdd2u6IsZQwgHv4MvxJ/ftkhksSCr5UdKgcwumZM06HJx2f48ZnjKVQLg5tUokCn9B UqeDWf/s4I+5eSb7CPfa//oaeoyWlynr3Np6yb+A7Laxc7l1de/tvK3u5k0fLbcf2dKy px/8uhSJjZ5IOVs1E7RLiU8NXTApUhb/0ESyocgoK82GFyf4Bg3LbxNqdFbR3rW45NQn t35DiTJj+g/iVcN11apaBMXAUQ94lxnhkJft/l8Bx5VlzPDpuFs6AFEeL1iwz4bWh5cY kvmgeyewtsNzbO8KddZ760ZjUUbDocKtnQUnNfPHg38wq9IyU0QaAXCgMn2GjW3L7IjA KNMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=p4UQUT2cU3JOZfm3LbL3EhL2KO4kHrMGHSyOrtfXAAk=; fh=E3ilibYCxplRFz20OpOkjMj1lMKBPGRzcx1abGX98y8=; b=QCSRkO73Lf9Mqu+DXvsxXYKQ4XDJM0hwHhqArpCq2tX7sS1PDUeCy3hfWD9VCOXEsD WWQwUEi92/aGQPECxXt1crM1x+8BmDqUgZhGafmJM0dA/9Lb1NcKfGIzWgOfoHa0gmXN wv31un69kxks9ZSc4KfH6UC6qPvxlNyBwbTDlorHYXz4NDCqt5PMoQGkao/XGou79CPJ HuhqnZsLlBovPUIlh5kOWpIdDSrPgzsCF5h4mRSUwCIf8AVeEl/kJYKKuYewxUNAOk2T LBarK0pxEKiqveaLrLenEYqcNeNwDpRQS86Mygq0O0I52mVbliHtTS+0TxqUW35SIa6a /dzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=N8rwUd8V; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id by22-20020a056a02059600b0056fbf85c743si8168403pgb.790.2023.10.24.00.52.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 00:52:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=N8rwUd8V; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 70F4F804D8FF; Tue, 24 Oct 2023 00:52:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233910AbjJXHwA (ORCPT + 99 others); Tue, 24 Oct 2023 03:52:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233906AbjJXHvp (ORCPT ); Tue, 24 Oct 2023 03:51:45 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 940BC19B2; Tue, 24 Oct 2023 00:51:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698133892; x=1729669892; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BxG6wVyvyosYY4Ew029SXkTfwIymBHDUnYzPxlYLnc0=; b=N8rwUd8Vfz10b3+yfXbpyIklM80BHmv79H79LfbKILrctxGFFb/eZZcY iYB8DzKzwrtkUORxiCYNLkl4geEdz7WmHhqqiT7pYJHBgYso28oVsxeDD dzC0Id5JSclHJqf1gx/MfPyGvGTscJ8y8GublJ3PhKZncKDtFqaxr3+HV 7a/TbvMQWoanzonou9d4Z9YdIPlYkfhj/bp9ogqYkQaLEt57eyfxVeLOi AkS0WEL+00MChAKpMrofpPtNFMnB9V2eOWmS3uzOtir3ssluSsxrcXjTJ KaxteSJb/NKZuytvZaorfy6eVrU9CUK35jl8tu8PinurugJOpiTMDLwak g==; X-IronPort-AV: E=McAfee;i="6600,9927,10872"; a="367235231" X-IronPort-AV: E=Sophos;i="6.03,247,1694761200"; d="scan'208";a="367235231" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2023 00:51:13 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10872"; a="1089766330" X-IronPort-AV: E=Sophos;i="6.03,247,1694761200"; d="scan'208";a="1089766330" Received: from dmi-pnp-i7.sh.intel.com ([10.239.159.155]) by fmsmga005.fm.intel.com with ESMTP; 24 Oct 2023 00:51:10 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Zhenyu Wang , Zhang Xiong , Jim Mattson , Mingwei Zhang , Like Xu , Dapeng Mi , Dapeng Mi Subject: [kvm-unit-tests Patch 5/5] x86: pmu: Add asserts to warn inconsistent fixed events and counters Date: Tue, 24 Oct 2023 15:57:48 +0800 Message-Id: <20231024075748.1675382-6-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231024075748.1675382-1-dapeng1.mi@linux.intel.com> References: <20231024075748.1675382-1-dapeng1.mi@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 24 Oct 2023 00:52:21 -0700 (PDT) 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 41165e168d8e..a1d615fbab3d 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -112,8 +112,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; } @@ -246,6 +250,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); @@ -267,6 +272,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