Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2891346pxb; Tue, 24 Aug 2021 09:59:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYkCTteBFc6WYySs7ze1KO6NqF3bDZZ31uxvQqAAX4eZGQOHprNno/BY5gB7S856i+Ycp9 X-Received: by 2002:a17:906:659:: with SMTP id t25mr41576271ejb.372.1629824385318; Tue, 24 Aug 2021 09:59:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629824385; cv=none; d=google.com; s=arc-20160816; b=v8nGncKzJESkAiQqGDb2NCdkJHZ4njxnt5d+b75+xcPx1Rvcftzv1usLhwbHu5i34S BRNvBlFkBvHMM50t7KHegnBucnwAoC7LFxvb3iAM3Y2oNx8wn8oFSydDKsNogqksfMVm par4GLfpXKn2TwGToyqsaRphR83/4VqFnXgmYYDRvs7bpsU7QFpj93dc7C0x/4kqCaIH 2HuXVtnbyEULnfnCFWCN3Losdq5VqcQwP20jxxaRjtJ2wDSznnPQ7u9nEsWrA651Rc2B Ok4/bYxwoRwrVVLCEDpOUwQWxgLlmltWzvi4NwCnI6DlJK8vRy9wtL+UJXhaYQT+dM2C nqrw== 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=qRWvlbYDzOe9hIBVpv/HWfuF6UM2Hb8sJnkECIOUoF4=; b=B6b0973lGGH0ysH7ByPG4jHdZLEmI00HbfdtlKZLnOVg+ZX5xhAVwKJ4bRIUO9xSK2 tgBgBv18U8iJztk3wDVgWxr8s82XMfg4Gm3QnYU4AC7yZ0X8EAwxR7Mhnalv0eOYGCcn hafjfZSfVQYBi3lkewT+LX663WcBYfp5Xmcppdonse4pldJ/BlZdzVUDUpvbYLHdgRxB zOnHkJVESvZ8n8Zx4I0jHtrNpIobOSyPDdrycIRuCUJWEX2atZ+K5S5zkOAfWT+cH8EI GeJLPBPiBL6ch9p1QZ1uh03CImNpwHLYj+PCH3H8aWIi+0ME92ormOAK/61w6fhuWrp1 l6CA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="DpRpy/b4"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h10si19268025edk.552.2021.08.24.09.59.21; Tue, 24 Aug 2021 09:59:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="DpRpy/b4"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234742AbhHXQ55 (ORCPT + 99 others); Tue, 24 Aug 2021 12:57:57 -0400 Received: from mail.kernel.org ([198.145.29.99]:39218 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234343AbhHXQ5O (ORCPT ); Tue, 24 Aug 2021 12:57:14 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 26127613AB; Tue, 24 Aug 2021 16:56:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1629824189; bh=jC6uSCrgt69kXfrpA/d+Zpv7RChIWevKRH+aKooCZ/c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DpRpy/b4mQ15Kcmo73gWIGIvxe5m6zfz4+Cwkr/2T3J/AZg4iIhaq+iuFKI/+O/TQ jwuY1o60QNFDkWqvRPVLEzNXNyiauRAfsZPuRD9UzPtuqFQ0mZEtd8TmXsfILyw78m HP7qT7tDdDfl3T6DqtIuBiK3wwZ0Gqy9Gn+N54xdj8BFQeD+ZEkk5LeG7FQgwaQmvA gv7vmFxhWE5DG+Y5lUdTlluITV493f+Jx+3x19oToP2jp/nLBWfwHn8RlgaO1b7Qsc UMAJ76pVGuleEQP86Ac+F9RB7Fno4bbz3zoJlM0RXnr1ZK1yqtJ2zrB6luQ1uGvSgY P1XinFNV4jhpA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Peter Zijlstra , Vince Weaver , Like Xu , Sasha Levin Subject: [PATCH 5.13 021/127] perf/x86: Fix out of bound MSR access Date: Tue, 24 Aug 2021 12:54:21 -0400 Message-Id: <20210824165607.709387-22-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210824165607.709387-1-sashal@kernel.org> References: <20210824165607.709387-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.13.13-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.13.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.13.13-rc1 X-KernelTest-Deadline: 2021-08-26T16:55+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Zijlstra [ Upstream commit f4b4b45652578357031fbbef7f7a1b04f6fa2dc3 ] On Wed, Jul 28, 2021 at 12:49:43PM -0400, Vince Weaver wrote: > [32694.087403] unchecked MSR access error: WRMSR to 0x318 (tried to write 0x0000000000000000) at rIP: 0xffffffff8106f854 (native_write_msr+0x4/0x20) > [32694.101374] Call Trace: > [32694.103974] perf_clear_dirty_counters+0x86/0x100 The problem being that it doesn't filter out all fake counters, in specific the above (erroneously) tries to use FIXED_BTS. Limit the fixed counters indexes to the hardware supplied number. Reported-by: Vince Weaver Signed-off-by: Peter Zijlstra (Intel) Tested-by: Vince Weaver Tested-by: Like Xu Link: https://lkml.kernel.org/r/YQJxka3dxgdIdebG@hirez.programming.kicks-ass.net Signed-off-by: Sasha Levin --- arch/x86/events/core.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 1eb45139fcc6..3092fbf9dbe4 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -2489,13 +2489,15 @@ void perf_clear_dirty_counters(void) return; for_each_set_bit(i, cpuc->dirty, X86_PMC_IDX_MAX) { - /* Metrics and fake events don't have corresponding HW counters. */ - if (is_metric_idx(i) || (i == INTEL_PMC_IDX_FIXED_VLBR)) - continue; - else if (i >= INTEL_PMC_IDX_FIXED) + if (i >= INTEL_PMC_IDX_FIXED) { + /* Metrics and fake events don't have corresponding HW counters. */ + if ((i - INTEL_PMC_IDX_FIXED) >= hybrid(cpuc->pmu, num_counters_fixed)) + continue; + wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + (i - INTEL_PMC_IDX_FIXED), 0); - else + } else { wrmsrl(x86_pmu_event_addr(i), 0); + } } bitmap_zero(cpuc->dirty, X86_PMC_IDX_MAX); -- 2.30.2