Linux virtualization list
 help / color / mirror / Atom feed
From: "Christian König" <christian.koenig@amd.com>
To: Tvrtko Ursulin <tursulin@ursulin.net>,
	boris.brezillon@collabora.com, phasta@mailbox.org,
	dakr@kernel.org, erhan.bilgili@gmail.com,
	sumit.semwal@linaro.org, alessio.belle@imgtec.com,
	luigi.santivetti@imgtec.com, thierry.reding@kernel.org,
	mperttunen@nvidia.com, kraxel@redhat.com,
	gurchetansingh@chromium.org
Cc: dri-devel@lists.freedesktop.org,
	imagination@lists.freedesktop.org, linux-tegra@vger.kernel.org,
	virtualization@lists.linux.dev
Subject: Re: [PATCH] dma-buf: improve robustness of dma_fence_unwrap_for_each
Date: Wed, 2 Dec 2026 13:43:15 +0100	[thread overview]
Message-ID: <92c98226-d4d4-427b-9380-46256305ac5e@amd.com> (raw)
In-Reply-To: <2c88704a-6b72-466e-8d82-a25c43219aed@ursulin.net>

On 12/2/26 10:01, Tvrtko Ursulin wrote:
> On 01/12/2026 16:55, Christian König wrote:
...
>> -struct dma_fence *dma_fence_unwrap_next(struct dma_fence_unwrap *cursor)
>> +void dma_fence_unwrap_next(struct dma_fence_unwrap *cursor,
>> +               struct dma_fence **fence)
>>   {
>> -    struct dma_fence *tmp;
>> -
>>       ++cursor->index;
>> -    tmp = dma_fence_array_next(cursor->array, cursor->index);
>> -    if (tmp)
>> -        return tmp;
>> +    *fence = dma_fence_array_next(cursor->array, cursor->index);
>> +    if (*fence)
>> +        return;
>>         cursor->chain = dma_fence_chain_walk(cursor->chain);
>> -    return __dma_fence_unwrap_array(cursor);
>> +    __dma_fence_unwrap_array(cursor, fence);
> 
> Did something require changing the prototype to return via argument pointer? I can't spot it straight away and if really did not, isn't it nicer to keep returning as return value?

In dma_fence_unwrap_next() function it is not a must have, but it is to get the dma_fence_unwrap_first() function right. So I changed all uses for consistency.

>> diff --git a/drivers/gpu/drm/tegra/submit.c b/drivers/gpu/drm/tegra/submit.c
>> index 5db37856db123..3d4db12a8c13f 100644
>> --- a/drivers/gpu/drm/tegra/submit.c
>> +++ b/drivers/gpu/drm/tegra/submit.c
>> @@ -353,14 +353,13 @@ static int submit_get_syncpt(struct tegra_drm_context *context, struct host1x_jo
>>   static int submit_handle_in_fence(struct tegra_drm_context *context, struct host1x *host1x,
>>                     struct dma_fence *fence, u32 *num_waits)
>>   {
>> -    struct dma_fence_unwrap iter;
>>       struct host1x *fence_host1x;
>>       struct dma_fence *f;
>>       int err = 0;
>>         *num_waits = 0;
>>   -    dma_fence_unwrap_for_each(f, &iter, fence) {
>> +    dma_fence_unwrap_for_each(f, fence) {
>>           long wait_err;
>>             if (err)
> 
> This one is if (err) continue which could be the same "keep iterating to avoid reference leaks" workaround as in pvr_sync_add_dep_to_job.

Good point, that can trivially be cleaned up as well.

>> diff --git a/include/linux/dma-fence-unwrap.h b/include/linux/dma-fence-unwrap.h
>> index 7bfacdf79de21..1829e2a655b95 100644
>> --- a/include/linux/dma-fence-unwrap.h
>> +++ b/include/linux/dma-fence-unwrap.h
>> @@ -32,27 +32,33 @@ struct dma_fence_unwrap {
>>       unsigned int index;
>>   };
>>   -struct dma_fence *dma_fence_unwrap_first(struct dma_fence *head,
>> -                     struct dma_fence_unwrap *cursor);
>> -struct dma_fence *dma_fence_unwrap_next(struct dma_fence_unwrap *cursor);
>> +struct dma_fence_unwrap dma_fence_unwrap_first(struct dma_fence *head,
>> +                           struct dma_fence **fence);
>> +void dma_fence_unwrap_next(struct dma_fence_unwrap *cursor,
>> +               struct dma_fence **fence);
>> +void dma_fence_unwrap_cleanup(struct dma_fence_unwrap *cursor);
>> +
>> +/* Helper for dma_fence_unwrap_for_each(). Internal use only. */
>> +#define __dma_fence_unwrap_for_each(fence, cursor, head)    \
>> +    for (struct dma_fence_unwrap cursor            \
>> +         __cleanup(dma_fence_unwrap_cleanup)        \
>> +         = dma_fence_unwrap_first(head, &(fence)); fence;    \
>> +         dma_fence_unwrap_next(&cursor, &(fence)))
>>     /**
>>    * dma_fence_unwrap_for_each - iterate over all fences in containers
>>    * @fence: current fence
>> - * @cursor: current position inside the containers
>>    * @head: starting point for the iterator
>>    *
>>    * Unwrap dma_fence_chain and dma_fence_array containers and deep dive into all
>>    * potential fences in them. If @head is just a normal fence only that one is
>>    * returned.
>>    */
> 
> So far kerneldoc had no mention of the problem with the reference. Assuming some people have learnt about it by now, and may skip that the problem is now gone, it should be worth documenting that the cleanup is automatic.

I will add a sentence about holding a reference which is cleaned up automatically.

>> -#define dma_fence_unwrap_for_each(fence, cursor, head)            \
>> -    for (fence = dma_fence_unwrap_first(head, cursor); fence;    \
>> -         fence = dma_fence_unwrap_next(cursor))
>> +#define dma_fence_unwrap_for_each(fence, head) \
>> +    __dma_fence_unwrap_for_each(fence, __UNIQUE_ID(cursor), head)
>>     struct dma_fence *__dma_fence_unwrap_merge(size_t num_fences,
>> -                       struct dma_fence **fences,
>> -                       struct dma_fence_unwrap *cursors);
>> +                       struct dma_fence **fences);
>>     size_t dma_fence_dedup_array(struct dma_fence **array, size_t num_fences);
>>   @@ -68,12 +74,11 @@ size_t dma_fence_dedup_array(struct dma_fence **array, size_t num_fences);
>>    * Returns NULL on memory allocation failure, a dma_fence object representing
>>    * all the given fences otherwise.
>>    */
>> -#define dma_fence_unwrap_merge(...)                    \
>> -    ({                                \
>> -        struct dma_fence *__f[] = { __VA_ARGS__ };        \
>> -        struct dma_fence_unwrap __c[ARRAY_SIZE(__f)];        \
>> -                                    \
>> -        __dma_fence_unwrap_merge(ARRAY_SIZE(__f), __f, __c);    \
>> +#define dma_fence_unwrap_merge(...)                \
>> +    ({                            \
>> +        struct dma_fence *__f[] = { __VA_ARGS__ };    \
>> +                                \
>> +        __dma_fence_unwrap_merge(ARRAY_SIZE(__f), __f);    \
>>       })
>>     #endif
> 
> Looks mostly okay to me. Just those few small questions to address.

Thanks, going to fix those up.

Regards,
Christian.

> 
> Regards,
> 
> Tvrtko
> 


      reply	other threads:[~2026-12-02 12:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 16:55 [PATCH] dma-buf: improve robustness of dma_fence_unwrap_for_each Christian König
2026-12-01 17:03 ` sashiko-bot
2026-12-02  8:37 ` Boris Brezillon
2026-12-02  9:01 ` Tvrtko Ursulin
2026-12-02 12:43   ` 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=92c98226-d4d4-427b-9380-46256305ac5e@amd.com \
    --to=christian.koenig@amd.com \
    --cc=alessio.belle@imgtec.com \
    --cc=boris.brezillon@collabora.com \
    --cc=dakr@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=erhan.bilgili@gmail.com \
    --cc=gurchetansingh@chromium.org \
    --cc=imagination@lists.freedesktop.org \
    --cc=kraxel@redhat.com \
    --cc=linux-tegra@vger.kernel.org \
    --cc=luigi.santivetti@imgtec.com \
    --cc=mperttunen@nvidia.com \
    --cc=phasta@mailbox.org \
    --cc=sumit.semwal@linaro.org \
    --cc=thierry.reding@kernel.org \
    --cc=tursulin@ursulin.net \
    --cc=virtualization@lists.linux.dev \
    /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