From patchwork Tue Oct 24 07:57:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 157271 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp1780558vqx; Tue, 24 Oct 2023 00:52:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF/mkB4F2K0ICTMOj7e91c/OCnN9UhxPQ15paAniwW05bzdVYKn2KK8cTbnCk42+qTnqy6y X-Received: by 2002:a05:6e02:1bac:b0:352:6251:2364 with SMTP id n12-20020a056e021bac00b0035262512364mr16435738ili.11.1698133931592; Tue, 24 Oct 2023 00:52:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698133931; cv=none; d=google.com; s=arc-20160816; b=N9YW/ihHX7w5um9fPMfgITkOfV5Wr3riEIp/lzxtkujaXR8Wj/+pK1rz0XNm6dBYoH F4O/ZlyqVVy1Gi+WZdiYdBY6H+GYDQLN/jszdDnNubgaJvsMzGYxyHQ9ttVRbiOlZLvT 024wJThbrWJW4WGOvb9vlNJIeioFVehA8TOqAiByMOovM/mHhVMgQwnkLrcuKKFfiiNN drW/KuGiIYd8Kxy2OghOV3AYJLkXHJBXPu2yyka9mVwmxEO4J90EGICk0kV+o03wvIWm 7uxB7+/NV4QEWqR80/rh5ev9xzei1pvGQnbWhmcU7ayji6VbLDaHb5HhptVR46FU4VAf 0SHw== 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 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id f4-20020aa79684000000b00690c1e51cdfsi7891531pfk.188.2023.10.24.00.52.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 00:52:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; 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 23.128.96.32 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 agentk.vger.email (Postfix) with ESMTP id 87E7A805E47B; Tue, 24 Oct 2023 00:52:09 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233912AbjJXHwA (ORCPT + 26 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 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 agentk.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 (agentk.vger.email [0.0.0.0]); Tue, 24 Oct 2023 00:52:09 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780622485639784669 X-GMAIL-MSGID: 1780622485639784669 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;