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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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 68DD0CCD195 for ; Wed, 22 Oct 2025 10:30:30 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id CFF1110E0C8; Wed, 22 Oct 2025 10:30:29 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by gabe.freedesktop.org (Postfix) with ESMTP id 50B1210E0C8 for ; Wed, 22 Oct 2025 10:30:29 +0000 (UTC) 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 0A11A1063; Wed, 22 Oct 2025 03:30:21 -0700 (PDT) Received: from e120398-lin.trondheim.arm.com (e120398-lin.trondheim.arm.com [10.40.16.110]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 73E363F63F; Wed, 22 Oct 2025 03:30:26 -0700 (PDT) From: Ketil Johnsen To: Boris Brezillon , Steven Price , Liviu Dudau , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Heiko Stuebner Cc: Ketil Johnsen , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] drm/panthor: Fix UAF race between device unplug and FW event processing Date: Wed, 22 Oct 2025 12:30:13 +0200 Message-ID: <20251022103014.1082629-1-ketil.johnsen@arm.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The function panthor_fw_unplug() will free the FW memory sections. The problem is that there could still be pending FW events which are yet not handled at this point. process_fw_events_work() can in this case try to access said freed memory. This fix introduces a destroyed state for the panthor_scheduler object, and we check for this before processing FW events. Signed-off-by: Ketil Johnsen Fixes: de85488138247 ("drm/panthor: Add the scheduler logical block") --- v2: - Followed Boris's advice and handle the race purely within the scheduler block (by adding a destroyed state) --- drivers/gpu/drm/panthor/panthor_sched.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/panthor/panthor_sched.c b/drivers/gpu/drm/panthor/panthor_sched.c index 0cc9055f4ee52..4996f987b8183 100644 --- a/drivers/gpu/drm/panthor/panthor_sched.c +++ b/drivers/gpu/drm/panthor/panthor_sched.c @@ -315,6 +315,13 @@ struct panthor_scheduler { */ struct list_head stopped_groups; } reset; + + /** + * @destroyed: Scheduler object is (being) destroyed + * + * Normal scheduler operations should no longer take place. + */ + bool destroyed; }; /** @@ -1765,7 +1772,10 @@ static void process_fw_events_work(struct work_struct *work) u32 events = atomic_xchg(&sched->fw_events, 0); struct panthor_device *ptdev = sched->ptdev; - mutex_lock(&sched->lock); + guard(mutex)(&sched->lock); + + if (sched->destroyed) + return; if (events & JOB_INT_GLOBAL_IF) { sched_process_global_irq_locked(ptdev); @@ -1778,8 +1788,6 @@ static void process_fw_events_work(struct work_struct *work) sched_process_csg_irq_locked(ptdev, csg_id); events &= ~BIT(csg_id); } - - mutex_unlock(&sched->lock); } /** @@ -3882,6 +3890,7 @@ void panthor_sched_unplug(struct panthor_device *ptdev) cancel_delayed_work_sync(&sched->tick_work); mutex_lock(&sched->lock); + sched->destroyed = true; if (sched->pm.has_ref) { pm_runtime_put(ptdev->base.dev); sched->pm.has_ref = false; -- 2.47.2