Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp115001rdb; Tue, 31 Oct 2023 02:22:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGHbuR5ZK2h+z+hanfFeDZQrnP+m7vHy1Qdoe/XA0+7gx/WXGgQnMhZOH8iXAYDFblX+5uY X-Received: by 2002:a05:6e02:1d06:b0:349:7832:5d8 with SMTP id i6-20020a056e021d0600b00349783205d8mr16976717ila.5.1698744150083; Tue, 31 Oct 2023 02:22:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698744150; cv=none; d=google.com; s=arc-20160816; b=GKPd8qoJCubaESOoi4G3X/X/D6qojjQZvm6kEBQGuGLwWHxPHGh3yq66O1TuJpxYuL T0jqJjKCmDRFqRgtz2+hT1J0Ef69zW0FrntMEKYHrwd8KzDws8UpWqM8PX5J3/RPURaC ZiAzOvQ9FfyxyQQ9iXtVdFFe5Pqn1IXu2paO40UheHaQwkjQsG3YS5ha57u2W1/r6YCm x8nZawhpCZ3QVP6wCbNBzxFGrMbp2QBCX7spzhOJSXPRB3Msae7wdY1XhLs3WjWavqQv pHeM4WO1vgCfLM/+UNfMGnVlDHE7atDlsjUpGZ5v0CeYzUkw9diynF8embVr8P69rBJk Ukkg== 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=uB5jcPqU60w2I9rfaezCeVkReEPIv1pcC3nWkarvyfw=; fh=E3ilibYCxplRFz20OpOkjMj1lMKBPGRzcx1abGX98y8=; b=AzP5+NfLv1OLNSzYiik+aqOJgHWzoK1sEyPjRtoiv3HAvSJ4K2uSGUSnDus0ew8dVm oL50b8o6iGbxfqWlL0ZSupQxBsVzU9reGOIS0wUr4uYjEAW1ceJPgbcj68LiLkyxnGOq +t7CJmcOHCjdgc5+mFIro8JLPVA0y4DW2g3jS5xLsGQxLcQwO41w93Er0BUGSZ6F0Zp5 bR2WJ6qyyzvoTIYYM65ApW1mOEPxICv58M5EmRlqPEycAW7HRIl0yqUZeWArzRx2acUc LJ+0fkuiY7bHuUKO8RMC5+Gqo66uXD893H6ZewbA8HjXeqKIGHVvRFlndu3w9Godhw3x OZCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KNM+3pQn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id bq19-20020a056a02045300b005b95f60b9a6si762047pgb.274.2023.10.31.02.22.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 02:22:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KNM+3pQn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (Postfix) with ESMTP id AF3A5803512E; Tue, 31 Oct 2023 02:22:27 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343875AbjJaJWQ (ORCPT + 99 others); Tue, 31 Oct 2023 05:22:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343890AbjJaJWH (ORCPT ); Tue, 31 Oct 2023 05:22:07 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E16ACB7; Tue, 31 Oct 2023 02:22:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698744121; x=1730280121; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PuCRcyvXo/0paorC4geEOaVKWI0b7CqKhedXiIXoy0A=; b=KNM+3pQnGpmUUNQuHOdJcxhEJDaYVJgfWRz9AWWnmosWzV4A0ehXWcI/ lTcQ7n0+M5pnMKJBkf7mAKGCvzPElMZkNpf5A9oZyhQsxywtri3MP8CXR k+90mWDkWu3TC4irj/W8bzCV+I2k/DHt5GPwcdHwI79hTSZAfusPp/HyN PW/VWy8IGJjj6uj0A3mLEhTsllOP8qkA/F5oCa4zIVLB4aeMhtaNtTJMv ER38hCNC5tuW4J9Vl35gLh/DUYqMexeTcluHlsSHkjBrOuOgz7f7D9ZQX u5wKjKuiKo+1MBhBytx85ub/awqH/MP194ZIgD+flMns1Ibf4ouBRS9Rl Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10879"; a="385435997" X-IronPort-AV: E=Sophos;i="6.03,265,1694761200"; d="scan'208";a="385435997" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Oct 2023 02:22:01 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10879"; a="877445536" X-IronPort-AV: E=Sophos;i="6.03,265,1694761200"; d="scan'208";a="877445536" Received: from dmi-pnp-i7.sh.intel.com ([10.239.159.155]) by fmsmga002.fm.intel.com with ESMTP; 31 Oct 2023 02:21:58 -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 v2 5/5] x86: pmu: Add asserts to warn inconsistent fixed events and counters Date: Tue, 31 Oct 2023 17:29:21 +0800 Message-Id: <20231031092921.2885109-6-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231031092921.2885109-1-dapeng1.mi@linux.intel.com> References: <20231031092921.2885109-1-dapeng1.mi@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Tue, 31 Oct 2023 02:22:27 -0700 (PDT) Current PMU code doesn't check whether the number of fixed counters is larger than pre-defined fixed events. If so, it would cause out of range memory access. So add asserts 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 404dc7b62ac2..3ce05f0a1d38 100644 --- a/x86/pmu.c +++ b/x86/pmu.c @@ -117,8 +117,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; } @@ -251,6 +255,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); @@ -272,6 +277,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