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 C0810C369AB for ; Thu, 24 Apr 2025 07:07:18 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3EBBD10E73C; Thu, 24 Apr 2025 07:07:13 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="aEDdRCNb"; dkim-atps=neutral Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id 236FE10E73C for ; Thu, 24 Apr 2025 07:07:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:In-Reply-To:From: References:Cc:To:Subject:MIME-Version:Date:Message-ID:Sender:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=P6dxRnCEmFme6g/XnGOCpWVei/EzbFB1aGY/BAh4xYA=; b=aEDdRCNbvhMJPzYeJqnbcIVWfV RqPrtz5v4Y85OjLy1RWhjcyJG4BcSIeSkCDPdDuzZQbVlxsKoje3C9mybzcn3zRz3hLLHcls2VnUw AAVLRAzM8ADXstBS+GHsKQDeWl+utUCN7jXv8HlSZE1JIo+R8oebP4Px3Y2XHBTLTC9wZ7MMdoOre VqppNQFDecBg62+O0QBarYlfclFaW81SvdQYmx4rmdDvqD2qA2s7KiETawNuLA+wJ5jUQp7lnzkHD jH3Tx0g4ZayQpU86e4PUDeb7+coFN8gd3bwzV/08TtjXBk3ZaPW4finJKUotCksxgN4gKkjyMNmwZ V+w9p27Q==; Received: from [81.79.92.254] (helo=[192.168.0.101]) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_128_GCM:128) (Exim) id 1u7qfr-007i8U-Cm; Thu, 24 Apr 2025 09:07:07 +0200 Message-ID: Date: Thu, 24 Apr 2025 08:07:06 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC 0/4] Some (drm_sched_|dma_)fence lifetime issues To: =?UTF-8?Q?Christian_K=C3=B6nig?= , dri-devel@lists.freedesktop.org Cc: kernel-dev@igalia.com, Danilo Krummrich , Lucas De Marchi , Matthew Brost , Philipp Stanner , Rodrigo Vivi References: <20250418164246.72426-1-tvrtko.ursulin@igalia.com> <29b27ae3-de56-475d-ae85-fd593a011ea3@amd.com> Content-Language: en-GB From: Tvrtko Ursulin In-Reply-To: <29b27ae3-de56-475d-ae85-fd593a011ea3@amd.com> Content-Type: text/plain; charset=UTF-8; format=flowed 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" On 23/04/2025 14:12, Christian König wrote: > On 4/18/25 18:42, Tvrtko Ursulin wrote: >> Hi all, >> >> Recently I mentioned to Danilo about some fence lifetime issues so here is a >> rough series, more than anything intended to start the discussion. >> >> Most of the problem statement can be found in the first patch but to briefly >> summarise - because sched fence can outlive the scheduler, we can trivially >> engineer an use after free with xe and possibly other drivers. All that is >> needed is to convert a syncobj into a sync file behind drivers back, and I don't >> see what the driver can do about it. > > > Yeah that topic again :) The problem here is that this is not a bug, it is a feature! > > IIRC it was Alex who pointed that issue out on the very first fence patch set, and we already discussed what to do back then. > > The problem with grabbing module references for fences is that you get trivially into circle references and so basically always preventing the module from unloading. Where "always" is only "while there are active objects from that module", no? > The decision was made to postpone this and live with the potential use after free on module unload until somebody has time to fix it. Well that was +10 years ago :) > > I discussed this with Sima again last year and we came to the conclusion that the easiest way forward would be to decouple the dma_fence implementation from the driver or component issuing the fence. > > I then came up with the following steps to allow this: > 1. Decouple the lock used for protecting the dma_fence callback list from the caller. > 2. Stop calling enable_signaling with the lock held. > 3. Nuke all those kmem_cache implementations and force drivers to always allocate fences using kvmalloc(). > 4. Nuke the release callback (or maybe move it directly after signaling) and set fence->ops to NULL after signaling the fence. > > I already send patches out for #1 and #2, but don't have enough time to actually finish the work. > > If you want take a look at nuking all those kmem_cache implementations for allocating the fence memory. I think that can be completed completely separate to everything else. So enabling dma fence "revoke" so to say. Just to check we are on the same page, it is not just about the module references, but also use after frees which can happen even if module is still loaded but any memory reachable via dma fence entry points has been freed. In that case, as Matt has already asked, if you could dig up your unfinished work it would be interesting to see. Regards, Tvrtko >> IGT that exploits the problem: >> https://patchwork.freedesktop.org/patch/642709/?series=146211&rev=2 >> >> Different flavour of the problem space is if we had a close(drm_fd) in that test >> before the sleep. In that case we can even unload xe.ko and gpu-sched.ko for >> even more fun. Last two patches in the series close that gap. >> >> But first two patches are just shrinking the race window. They are not proper >> fixes. This is what I want to discuss since I understand reference counting all >> the involved objects has been rejected in the past. And since the problem >> probably expands to all dma fences it certainly isn't easy. >> >> To be clear once more - lets not focus on how this does not fix it fully - I am >> primarily trying to start the conversation. >> >> Cc: Christian König >> Cc: Danilo Krummrich >> Cc: Lucas De Marchi >> Cc: Matthew Brost >> Cc: Philipp Stanner >> Cc: Rodrigo Vivi >> >> Tvrtko Ursulin (4): >> sync_file: Weakly paper over one use-after-free resulting race >> dma-fence: Slightly safer dma_fence_set_deadline >> drm/sched: Keep module reference while there are active fences >> drm/xe: Keep module reference while there are active fences >> >> drivers/dma-buf/dma-fence.c | 2 +- >> drivers/dma-buf/sync_file.c | 29 ++++++++++++++++++++----- >> drivers/gpu/drm/scheduler/sched_fence.c | 12 ++++++++-- >> drivers/gpu/drm/xe/xe_hw_fence.c | 13 ++++++++++- >> 4 files changed, 47 insertions(+), 9 deletions(-) >> >