From: "Christian König" <christian.koenig@amd.com>
To: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>,
dri-devel@lists.freedesktop.org
Cc: kernel-dev@igalia.com, Danilo Krummrich <dakr@kernel.org>,
Lucas De Marchi <lucas.demarchi@intel.com>,
Matthew Brost <matthew.brost@intel.com>,
Philipp Stanner <phasta@kernel.org>,
Rodrigo Vivi <rodrigo.vivi@intel.com>
Subject: Re: [RFC 0/4] Some (drm_sched_|dma_)fence lifetime issues
Date: Wed, 7 May 2025 16:50:27 +0200 [thread overview]
Message-ID: <cab8b34a-cc21-433e-a4c1-0a25db6df798@amd.com> (raw)
In-Reply-To: <33cf57f2-230d-4bab-bab9-848b5ace9dfc@igalia.com>
On 5/7/25 16:07, Tvrtko Ursulin wrote:
>>> Are you thinking truly never or for as long someone has a reference?
>>
>> Truly never. It's simply a circle dependency you can never break up.
>>
>> In other words the module references the fence and the fence references the module.
>
> Past fences being signaled? How?
See how for example amdgpu manages it's VMIDs. Basically the driver keeps an array of all the fence which every used the VMID.
When a VMID is needed the driver checks those fences and eventually frees the signaled ones until an idle VMID is found.
The problem is that freeing the old signaled fences is a lazy operation and only done when a new request comes in.
As far as I know we have tons of those use cases spread all around in different drivers.
>> When the module unloads it drops the reference to the fences ultimately freeing them.
>>
>> The only issue is that modules can both reference their own as well a foreign fences. So what can happen is that you have module A which references fences A1, A2 and B1 and module B which references B1, B2 and A2.
>>
>> Now you can't unload either module first because they cross reference their fences and unloading one would leave the other module with fences which can't be released without crashing.
>>
>> So what we need to have is that the dma_fence framework guarantees that you don't need the fence->ops nor the fence->lock pointer any more after the fence signaled.
>
> With this option it would mean guarding all entry points with the embedded lock or you had in mind something different? Going simply by the signaled bit looks impossible to be safe.
The module can't unload until all it's fences are signaled, that's obvious.
I think it's save to assume that module unload doesn't happens right after signaling a fence, so we should be save to assume that nobody is inside the callbacks any more after some grace period.
We could use some SRCU or similar to enforce that but my gut feeling is that this would hurt more than help, especially since the code is really performance critical.
>>> It is also a question how to "revoke" fences safely (race free). It sounds hard to me. It does not seem you got to this last problem in the above branches so I don't know if you had some elegant ideas for that.
>>>
>>> Maybe first to ask if anyone is aware of a precedent where something in the kernel already uses this design pattern?
>>
>> Of hand I don't know of any, but the problem sounds rather common to me.
>
> Uf I don't know. Feels very atypical to me but I would be very glad to be told otherwise.
I briefly remember that Greg once kicked me because I accidentally violated some rule in the FS layer which implemented something similar for signal/polling.
E.g. you can unmount and unload a file system even if userspace is still sleeping and waiting on something.
I need to look that up again, maybe it provides a good pattern on how to solve this.
Regards,
Christian.
>
> Regards,
>
> Tvrtko
>
prev parent reply other threads:[~2025-05-07 14:50 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-18 16:42 [RFC 0/4] Some (drm_sched_|dma_)fence lifetime issues Tvrtko Ursulin
2025-04-18 16:42 ` [RFC 1/4] sync_file: Weakly paper over one use-after-free resulting race Tvrtko Ursulin
2025-04-18 16:42 ` [RFC 2/4] dma-fence: Slightly safer dma_fence_set_deadline Tvrtko Ursulin
2025-04-18 16:42 ` [RFC 3/4] drm/sched: Keep module reference while there are active fences Tvrtko Ursulin
2025-04-18 16:42 ` [RFC 4/4] drm/xe: " Tvrtko Ursulin
2025-04-23 13:12 ` [RFC 0/4] Some (drm_sched_|dma_)fence lifetime issues Christian König
2025-04-24 6:11 ` Matthew Brost
2025-04-24 7:07 ` Tvrtko Ursulin
2025-04-28 13:15 ` Christian König
2025-05-07 12:28 ` Tvrtko Ursulin
2025-05-07 12:54 ` Christian König
2025-05-07 14:07 ` Tvrtko Ursulin
2025-05-07 14:50 ` Christian König [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cab8b34a-cc21-433e-a4c1-0a25db6df798@amd.com \
--to=christian.koenig@amd.com \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=kernel-dev@igalia.com \
--cc=lucas.demarchi@intel.com \
--cc=matthew.brost@intel.com \
--cc=phasta@kernel.org \
--cc=rodrigo.vivi@intel.com \
--cc=tvrtko.ursulin@igalia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox