Received: by 2002:ab2:23c8:0:b0:1f2:fdbc:cb93 with SMTP id a8csp203037lqe; Wed, 27 Mar 2024 03:21:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXxfpS0EB1zU+8uTtUa1mf+WqqbgNxqe/wN5nbhjqsQhqGJdZjRX2BVMrTUmsKQEdr5fZbJv6FeCPSAu1sH/NR1OmmjlxfK7j48F6nc5w== X-Google-Smtp-Source: AGHT+IEaYsUikHydZlkVeO4srC4nxt+7PCAA48zWj9ewQIoU0TnKOFaIvs+3xMCmaS4n2bUhFvad X-Received: by 2002:a50:d652:0:b0:56c:17be:5b03 with SMTP id c18-20020a50d652000000b0056c17be5b03mr677639edj.36.1711534917837; Wed, 27 Mar 2024 03:21:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711534917; cv=pass; d=google.com; s=arc-20160816; b=kRWONj6fBw/wYjphLkgn5gtRxhjZ07VhWjC3BxkZqesWiPD6VTstzjTm71NXxC0TWI 1q78cQ7tHbEc1wE8NYhs0m9UL3xuKSdpnPbQqvTllQWVLBkP5icdC1XrgUtXmwRzqlFf 27Ki/Ps2qoP3RX66jmIuNi2HdC9p1ZSx+Op8Z7YDGC2FildXJSNqwuSAZLoqy2SvUVWr MPWppsVCJ1QkR3GK7t2WkWMReenXeX+jw8Ky81wznnOSldW2RTB+/rn4j3lLRdIXeskL GyEJgLX4eWw0VACm+6zwDptzr77agPYrfhJiPsjL00hI19N7WWbflTcumT5dSCQgrpLB cxTQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=n344crze8KyvptQ1SrQvddMcAikS5ZSKi88jaE8KQzQ=; fh=Fgosouhnnb0Tg+B4zt0jPW2SYN0fzXrOuiZQ6VFtW8w=; b=xBzLN9v9qIt5FjS0nz98ngE6aE3hZFXuE4qeBdmAzl/2tF8QZqSqXoBGa1YYjNtA2J SNr07aFcAS0UiQR7toMc2FjdJOFgtXeXYygANbBAPCnTvZX80mh/UsAEQZvhif4fMjrm sveeTKSB30IoEke44C5WTxoiRSo7B0brybensGbTpZ5gj5p+3ZLmRayfeG99b9bqeH6v 8iI6hHesepHCZqM3H6lWc5/sk1x/r9Ul0FC+dlXn8kdjH2E788fyvKI++iKtccNf+SQe JDJloIX0kcMkW7rleT86gXrGTGrayPodIiLAa3N3TsR9CAcQf/9vTmjahaqQsZt4YYSm dTHQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=M0F6D7d8; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-120396-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-120396-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 j14-20020a508a8e000000b00568cec5b614si4369780edj.600.2024.03.27.03.21.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 03:21:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-120396-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=M0F6D7d8; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-120396-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-120396-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 7678B1F2B5C1 for ; Wed, 27 Mar 2024 06:44:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1574A224CE; Wed, 27 Mar 2024 06:44:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="M0F6D7d8" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (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 A7C974C9D; Wed, 27 Mar 2024 06:43:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711521841; cv=none; b=QLBXSn4I5D5KBipdk9qmvCOu9MSnYx2fu+D+3ED6hFOfPMtbMRo+pl/nySolvVN7/N4PJHbuUQcDMqD/4Ixvl+opZknm67l/99X5t4uVTgpcTMqRyExP3apUEgDyORAIMYRfE2kelqZVMJgAMfFN2aQAWCRHX88MbPSuxsXx43E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711521841; c=relaxed/simple; bh=gjxD+4XwapFfO+Ut0KmYydbgFTnH0aPydPXYf435b9o=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=rdIbXgA9R4vIOAgpp4QqWrpA4UGOxJ0BOdERHHf8KuXBoGo7HHbA+wqaT930szkhJDdSZhlvWUE8Nbe1ncnUz/UKuKHqclPgo67op6zrXx2BHlFn3u5s64Liwu2/2t2uKijYPC4giaxWLGE0BtSKCmckXk6xGFlK8b9EalFzDvA= 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=M0F6D7d8; arc=none smtp.client-ip=198.175.65.18 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=1711521840; x=1743057840; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=gjxD+4XwapFfO+Ut0KmYydbgFTnH0aPydPXYf435b9o=; b=M0F6D7d8kfGuBzYatjKPF+uZ0rWCmkRjfEZSh6pj/Q6+NtSMqfLTetCk nUv2ZZ5rqkEoL5C+3BdntCN73H8phJANG8QRcGHpigYKLeaCoIzRnMQ9G bs+/PRN9yLNtoqnKkThFHVeW4eKL7sSzUBNq8ErvJMA0AV36USA3p55U/ qxw7x8v7UVdADoIYvQo31Or9J0DNZpvOYjvi9Fq25zGRQdcc722GzqyDe 7k5npCNdgZ0jPVzsfXAXYS1lB+JmYopXFxEQuTRi9/RTeYyhcIDgZdwIZ dFZz3BiyOhclqolBiQJtn5Ob0XWF+POTIwGKlJnBduGWMXnBiCC7rBhc+ w==; X-CSE-ConnectionGUID: pwNbSCW7Tt2t2El/7gYn0A== X-CSE-MsgGUID: UN6wFzF2R0Oh1FN8W1q1dw== X-IronPort-AV: E=McAfee;i="6600,9927,11025"; a="6735529" X-IronPort-AV: E=Sophos;i="6.07,158,1708416000"; d="scan'208";a="6735529" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Mar 2024 23:43:59 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,158,1708416000"; d="scan'208";a="16577421" Received: from dapengmi-mobl1.ccr.corp.intel.com (HELO [10.125.242.198]) ([10.125.242.198]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Mar 2024 23:43:56 -0700 Message-ID: <894d480c-5785-4896-bb79-9560611347cb@linux.intel.com> Date: Wed, 27 Mar 2024 14:43:54 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [kvm-unit-tests Patch v3 03/11] x86: pmu: Add asserts to warn inconsistent fixed events and counters Content-Language: en-US To: Mingwei Zhang Cc: Sean Christopherson , Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Zhenyu Wang , Zhang Xiong , Like Xu , Jinrong Liang , Dapeng Mi References: <20240103031409.2504051-1-dapeng1.mi@linux.intel.com> <20240103031409.2504051-4-dapeng1.mi@linux.intel.com> From: "Mi, Dapeng" In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 3/27/2024 1:30 PM, Mingwei Zhang wrote: > On Wed, Jan 03, 2024, Dapeng Mi wrote: >> 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 > Reviewed-by: Mingwei Zhang >> --- >> 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; > maybe unsigned int is better? Make sense. Thanks for review. >> + >> + 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 >>