Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EC1DBC25B6B for ; Thu, 26 Oct 2023 16:39:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To:Subject: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=eG/2X2j2dcDh6xgV0G0r+v5zJTaqiOW1nuktg+LsFbI=; b=eRQETi4pQtzkSP AAj/RXeg9DN/gxhH9EDtJ3aLbMngvfLQSVLhKfnVqgTZs0VCQzXubAuWOI/zA/dh588YmONZw2jAf AxEf6qjmWFDUrla8LZyDAAaaL44xoHgYBc9hAMQ2J2aq9uNcjrYLaMIknI3GujPHl1mtHDfCrOS1a tuOTVHxd6pR4Tw7BeEZtKwnd3mjzaH7EDus4qIeDcLMGcLvfZAZacbxFgkLKqiOfNgrNgtfE+WGdO MVJRpAG9SH6bGlme0hJ5W6dbW0/KMrQW+sK2+rVDa7PcNyvlPk7G1ryivbZtk8W4bN8tNSr+wqVvp d1EL/C3M6DTWZXoD28xg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qw3OO-00EsPd-0N; Thu, 26 Oct 2023 16:39:32 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qw3OJ-00EsPE-31 for linux-arm-kernel@lists.infradead.org; Thu, 26 Oct 2023 16:39:29 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E74FD2F4; Thu, 26 Oct 2023 09:40:06 -0700 (PDT) Received: from [10.1.196.40] (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 24E053F64C; Thu, 26 Oct 2023 09:39:23 -0700 (PDT) Message-ID: <1727ca78-ca1f-472d-875a-642c8c249a44@arm.com> Date: Thu, 26 Oct 2023 17:39:22 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/1] drivers/perf: Fix kernel panic due to the invalid mon_ctx pointer Content-Language: en-GB To: Shanker Donthineni , James Morse Cc: Catalin Marinas , Mark Rutland , Will Deacon , linux-arm-kernel@lists.infradead.org, Rohit Mathew , Vikram Sethi References: <20231026142930.2670705-1-sdonthineni@nvidia.com> From: Robin Murphy In-Reply-To: <20231026142930.2670705-1-sdonthineni@nvidia.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231026_093928_087959_50F436F3 X-CRM114-Status: GOOD ( 21.43 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org [ Since I made the mistake of looking... ] On 26/10/2023 3:29 pm, Shanker Donthineni wrote: > The return pointer from the resctrl_arch_mon_ctx_alloc_no_wait() function > is saved in a 32-bit variable 'hwc->idx,' which results in the loss of > the upper 32 bits. This, in turn, triggers a kernel panic when attempting > to access a corrupted pointer. > > This patch addresses the issue by utilizing the IDR data structure to > keep track of a 32-bit value associated with a pointer (64-bit value). This seems unnecessaraily complicated - there's plenty of space in that anonymous hw_perf_event "driver data" union (I know I cram quite a lot in there for arm-cmn) so it should just be a case of picking a different appropriately-sized field to (ab)use. Thanks, Robin. > Signed-off-by: Shanker Donthineni > --- > drivers/perf/resctrl_pmu.c | 27 ++++++++++++++++++++++++--- > 1 file changed, 24 insertions(+), 3 deletions(-) > > diff --git a/drivers/perf/resctrl_pmu.c b/drivers/perf/resctrl_pmu.c > index 99a2b90b5d83..68cda619da48 100644 > --- a/drivers/perf/resctrl_pmu.c > +++ b/drivers/perf/resctrl_pmu.c > @@ -6,6 +6,8 @@ > #include > #include > #include > +#include > +#include > > struct resctrl_pmu { > struct pmu pmu; > @@ -25,6 +27,9 @@ RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(event, config, 0, 7); > RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(domain, config, 16, 23); > RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(resctrl_id, config1, 0, 63); > > +static DEFINE_MUTEX(resctrl_pmu_lock); > +static DEFINE_IDR(resctrl_pmu_idr); > + > static void resctrl_pmu_do_nothing(struct pmu *pmu) > { > } > @@ -69,12 +74,17 @@ static void resctrl_pmu_event_destroy(struct perf_event *event) > struct hw_perf_event *hwc = &event->hw; > u16 event_num = get_event(event); > struct rdt_resource *r; > + void *mon_ctx; > > r = resctrl_event_get_resource(event_num); > if (!r) > return; > > - resctrl_arch_mon_ctx_free(r, event_num, hwc->idx); > + mutex_lock(&resctrl_pmu_lock); > + mon_ctx = idr_remove(&resctrl_pmu_idr, hwc->idx); > + mutex_unlock(&resctrl_pmu_lock); > + > + resctrl_arch_mon_ctx_free(r, event_num, mon_ctx); > } > > static int resctrl_pmu_event_init(struct perf_event *event) > @@ -87,6 +97,7 @@ static int resctrl_pmu_event_init(struct perf_event *event) > struct rdt_domain *d; > u16 event_num, domain_id; > u32 closid, rmid; > + void *mon_ctx; > int err; > u64 id; > > @@ -144,7 +155,12 @@ static int resctrl_pmu_event_init(struct perf_event *event) > return -EINVAL; > } > > - hwc->idx = resctrl_arch_mon_ctx_alloc_no_wait(r, event_num); > + mon_ctx = resctrl_arch_mon_ctx_alloc_no_wait(r, event_num); > + > + mutex_lock(&resctrl_pmu_lock); > + hwc->idx = idr_alloc(&resctrl_pmu_idr, mon_ctx, 0, INT_MAX, GFP_KERNEL); > + mutex_unlock(&resctrl_pmu_lock); > + > if (hwc->idx == -ENOSPC) > return -ENOSPC; > event->destroy = resctrl_pmu_event_destroy; > @@ -162,6 +178,7 @@ static void resctrl_pmu_event_update(struct perf_event *event) > struct rdt_resource *r; > struct rdt_domain *d; > u32 closid, rmid; > + void *mon_ctx; > int err; > > event_num = get_event(event); > @@ -179,11 +196,15 @@ static void resctrl_pmu_event_update(struct perf_event *event) > if (!cpumask_test_cpu(smp_processor_id(), &d->cpu_mask)) > return; > > + mutex_lock(&resctrl_pmu_lock); > + mon_ctx = idr_find(&resctrl_pmu_idr, hwc->idx); > + mutex_unlock(&resctrl_pmu_lock); > + > do { > prev = local64_read(&hwc->prev_count); > > err = resctrl_arch_rmid_read(r, d, closid, rmid, > - event_num, &now, hwc->idx); > + event_num, &now, mon_ctx); > if (err) > return; > } while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev); _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel