Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp421418ybm; Thu, 28 May 2020 06:19:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzutgOvlLuCVAjPUH1+ff73JP7iMvF4kwnXP0FJyw/L65Ybzhw/B5p1FPPVdULYzCvBgvw5 X-Received: by 2002:aa7:cad3:: with SMTP id l19mr3165573edt.335.1590671982632; Thu, 28 May 2020 06:19:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590671982; cv=none; d=google.com; s=arc-20160816; b=UXsTTMwc64tQKX76wioyT09ao0LVI4PaHHUs7eKIS6eJzLbg7oJn4fGcRvHb/HDhf0 9WAS7klu1TM6r3CQRZts723sMLP7O+lJYOQFShRffPTghvSNvuWxb/knqJDNGa1yvJnx VR7a9PvkzwAQeqjmp9eBg/q7azFtobg7BsfUG++U0PUj46N/Y1LmnFGbBmzsxi8pZFKX I8DEOdu7zXTLxVm4gnf2M0ZU/Ve4Mz6BkZNidIQY/k+QhAcH/WdcNUYA3CIPr/n5KTW1 bZNwNr8m70y5YfU+TkT+tPksK70OQS7KkGccKvhRZTB70/Ywr2qrCBBxXvdOaWx3EPY2 X2lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:ironport-sdr:ironport-sdr; bh=l+f9AGBly/WvFxEqS1PJxOyinTYkrR9eeQbu35UqvuE=; b=ZX1IXX9AoooGgDc8iiOvGHiuZS4RWLZy0d8POSjNUE2oVU9T0uaKnAXtrq0cMf2l1u HIU/7uVzfToQUrNHFUqOsJEr4z2N2rl9ZZ5rgLpIFr2vVqgJ08/Uxw4m97BQ8S9/tilQ mit0M6WhywzPDXtSqOsxJ6pqFS6gH7J5xYyzJWx9a98hLv2CT6eG6x3LeacmddmWIx2u Ya39x4evbkhecJHw/bb+cSuANpzDy8H92VBuoNcgvR+h2s+mkDjJ7BWTZkDZ7xMXMeor yWg78YWW/L0DBFJHD6YjOmoxU6ZyuQiFvtrnXJFiQdf1ChMfr0boqZjguOEnGU1C8JhP MKhw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f19si3332558edx.127.2020.05.28.06.19.19; Thu, 28 May 2020 06:19:42 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390276AbgE1NRX (ORCPT + 99 others); Thu, 28 May 2020 09:17:23 -0400 Received: from mga05.intel.com ([192.55.52.43]:17063 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390236AbgE1NRS (ORCPT ); Thu, 28 May 2020 09:17:18 -0400 IronPort-SDR: o21Y4xWHunWbBVXILR2uxebqvl9WBQv8inZudO5I996MJVoSpVuClpkMwftAsXeDuQ/Q2odshE A6mI8dTEhdeQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2020 06:17:18 -0700 IronPort-SDR: hJolKqnq/ZOybWMuU8aaXNJRawGxIy8bX61IH7Nw4CPK9poR7Tsq2NBRlaneZmhaBsLTyb4Jv8 obhlC1gRnAww== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,444,1583222400"; d="scan'208";a="256157003" Received: from otc-lr-04.jf.intel.com ([10.54.39.143]) by orsmga007.jf.intel.com with ESMTP; 28 May 2020 06:17:18 -0700 From: kan.liang@linux.intel.com To: peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: ak@linux.intel.com, David.Laight@ACULAB.COM, Kan Liang Subject: [PATCH V2 3/3] perf/x86/intel/uncore: Validate MMIO address before accessing Date: Thu, 28 May 2020 06:15:27 -0700 Message-Id: <1590671727-99311-3-git-send-email-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1590671727-99311-1-git-send-email-kan.liang@linux.intel.com> References: <1590671727-99311-1-git-send-email-kan.liang@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kan Liang An oops will be triggered, if perf tries to access an invalid address which exceeds the mapped area. Check the address before the actual access to MMIO sapce of an uncore unit. Suggested-by: David Laight Signed-off-by: Kan Liang --- arch/x86/events/intel/uncore.c | 3 +++ arch/x86/events/intel/uncore.h | 12 ++++++++++++ arch/x86/events/intel/uncore_snbep.c | 6 ++++++ 3 files changed, 21 insertions(+) diff --git a/arch/x86/events/intel/uncore.c b/arch/x86/events/intel/uncore.c index cf76d66..284f8e7 100644 --- a/arch/x86/events/intel/uncore.c +++ b/arch/x86/events/intel/uncore.c @@ -132,6 +132,9 @@ u64 uncore_mmio_read_counter(struct intel_uncore_box *box, if (!box->io_addr) return 0; + if (!is_valid_mmio_offset(box, event->hw.event_base)) + return 0; + return readq(box->io_addr + event->hw.event_base); } diff --git a/arch/x86/events/intel/uncore.h b/arch/x86/events/intel/uncore.h index c2e5725..672e9e1 100644 --- a/arch/x86/events/intel/uncore.h +++ b/arch/x86/events/intel/uncore.h @@ -197,6 +197,18 @@ static inline bool uncore_pmc_freerunning(int idx) return idx == UNCORE_PMC_IDX_FREERUNNING; } +static inline bool is_valid_mmio_offset(struct intel_uncore_box *box, + unsigned long offset) +{ + if (box->pmu->type->mmio_map_size > offset) + return true; + + pr_warn_once("perf uncore: Access invalid address of %s.\n", + box->pmu->type->name); + + return false; +} + static inline unsigned int uncore_mmio_box_ctl(struct intel_uncore_box *box) { diff --git a/arch/x86/events/intel/uncore_snbep.c b/arch/x86/events/intel/uncore_snbep.c index 801b662..9c7a641 100644 --- a/arch/x86/events/intel/uncore_snbep.c +++ b/arch/x86/events/intel/uncore_snbep.c @@ -4487,6 +4487,9 @@ static void snr_uncore_mmio_enable_event(struct intel_uncore_box *box, if (!box->io_addr) return; + if (!is_valid_mmio_offset(box, hwc->config_base)) + return; + writel(hwc->config | SNBEP_PMON_CTL_EN, box->io_addr + hwc->config_base); } @@ -4499,6 +4502,9 @@ static void snr_uncore_mmio_disable_event(struct intel_uncore_box *box, if (!box->io_addr) return; + if (!is_valid_mmio_offset(box, hwc->config_base)) + return; + writel(hwc->config, box->io_addr + hwc->config_base); } -- 2.7.4