Test struct drm_gem_object.import_attach to detect imported objects.
During object clenanup, the dma_buf field might be NULL. Testing it in
an object's free callback then incorrectly does a cleanup as for native
objects. Happens for calls to drm_mode_destroy_dumb_ioctl() that
clears the dma_buf field in drm_gem_object_exported_dma_buf_free().
v3:
- only test for import_attach (Boris)
v2:
- use import_attach.dmabuf instead of dma_buf (Christian)
Signed-off-by: Thomas Zimmermann <tzimmermann(a)suse.de>
Fixes: b57aa47d39e9 ("drm/gem: Test for imported GEM buffers with helper")
Reported-by: Andy Yan <andyshrk(a)163.com>
Closes: https://lore.kernel.org/dri-devel/38d09d34.4354.196379aa560.Coremail.andysh…
Tested-by: Andy Yan <andyshrk(a)163.com>
Cc: Thomas Zimmermann <tzimmermann(a)suse.de>
Cc: Anusha Srivatsa <asrivats(a)redhat.com>
Cc: Christian König <christian.koenig(a)amd.com>
Cc: Maarten Lankhorst <maarten.lankhorst(a)linux.intel.com>
Cc: Maxime Ripard <mripard(a)kernel.org>
Cc: David Airlie <airlied(a)gmail.com>
Cc: Simona Vetter <simona(a)ffwll.ch>
Cc: Sumit Semwal <sumit.semwal(a)linaro.org>
Cc: "Christian König" <christian.koenig(a)amd.com>
Cc: dri-devel(a)lists.freedesktop.org
Cc: linux-media(a)vger.kernel.org
Cc: linaro-mm-sig(a)lists.linaro.org
---
include/drm/drm_gem.h | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h
index 9b71f7a9f3f8..a3133a08267c 100644
--- a/include/drm/drm_gem.h
+++ b/include/drm/drm_gem.h
@@ -588,8 +588,7 @@ static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_obje
*/
static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
{
- /* The dma-buf's priv field points to the original GEM object. */
- return obj->dma_buf && (obj->dma_buf->priv != obj);
+ return !!obj->import_attach;
}
#ifdef CONFIG_LOCKDEP
--
2.49.0
On Tue, Apr 15, 2025 at 10:20 AM Juan Yescas <jyescas(a)google.com> wrote:
>
> This change sets the allocation orders for the different page sizes
> (4k, 16k, 64k) based on PAGE_SHIFT. Before this change, the orders
> for large page sizes were calculated incorrectly, this caused system
> heap to allocate from 2% to 4% more memory on 16KiB page size kernels.
>
> This change was tested on 4k/16k page size kernels.
>
> Signed-off-by: Juan Yescas <jyescas(a)google.com>
I think "dma-buf: system_heap:" would be better for the subject since
this is specific to the system heap.
Would you mind cleaning up the extra space on line 321 too?
@@ -318,7 +318,7 @@ static struct page
*alloc_largest_available(unsigned long size,
int i;
for (i = 0; i < NUM_ORDERS; i++) {
- if (size < (PAGE_SIZE << orders[i]))
+ if (size < (PAGE_SIZE << orders[i]))
With that,
Reviewed-by: T.J. Mercier <tjmercier(a)google.com>
Fixes: d963ab0f15fb ("dma-buf: system_heap: Allocate higher order
pages if available") is also probably a good idea.
> ---
> drivers/dma-buf/heaps/system_heap.c | 9 ++++++++-
> 1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c
> index 26d5dc89ea16..54674c02dcb4 100644
> --- a/drivers/dma-buf/heaps/system_heap.c
> +++ b/drivers/dma-buf/heaps/system_heap.c
> @@ -50,8 +50,15 @@ static gfp_t order_flags[] = {HIGH_ORDER_GFP, HIGH_ORDER_GFP, LOW_ORDER_GFP};
> * to match with the sizes often found in IOMMUs. Using order 4 pages instead
> * of order 0 pages can significantly improve the performance of many IOMMUs
> * by reducing TLB pressure and time spent updating page tables.
> + *
> + * Note: When the order is 0, the minimum allocation is PAGE_SIZE. The possible
> + * page sizes for ARM devices could be 4K, 16K and 64K.
> */
> -static const unsigned int orders[] = {8, 4, 0};
> +#define ORDER_1M (20 - PAGE_SHIFT)
> +#define ORDER_64K (16 - PAGE_SHIFT)
> +#define ORDER_FOR_PAGE_SIZE (0)
> +static const unsigned int orders[] = {ORDER_1M, ORDER_64K, ORDER_FOR_PAGE_SIZE};
> +
> #define NUM_ORDERS ARRAY_SIZE(orders)
>
> static struct sg_table *dup_sg_table(struct sg_table *table)
> --
> 2.49.0.604.gff1f9ca942-goog
>
On Tue, Apr 15, 2025 at 10:20 AM Juan Yescas <jyescas(a)google.com> wrote:
>
> This change sets the allocation orders for the different page sizes
> (4k, 16k, 64k) based on PAGE_SHIFT. Before this change, the orders
> for large page sizes were calculated incorrectly, this caused system
> heap to allocate from 2% to 4% more memory on 16KiB page size kernels.
>
> This change was tested on 4k/16k page size kernels.
>
> Signed-off-by: Juan Yescas <jyescas(a)google.com>
Seems reasonable to me.
Acked-by: John Stultz <jstultz(a)google.com>
thanks
-john
Hi
Am 15.04.25 um 16:19 schrieb Boris Brezillon:
> On Tue, 15 Apr 2025 16:02:20 +0200
> Thomas Zimmermann <tzimmermann(a)suse.de> wrote:
>
>> Test struct drm_gem_object.import_attach.dmabuf to detect imported
>> objects. Warn if the stored state is inconsistent.
>>
>> During object clenaup, the dma_buf field might be NULL. Testing it in
>> an object's free callback then incorrectly does a cleanup as for native
>> objects. Happens for calls to drm_mode_destroy_dumb_ioctl() that
>> clears the dma_buf field in drm_gem_object_exported_dma_buf_free().
>>
>> v2:
>> - use import_attach.dmabuf instead of dma_buf (Christian)
>>
>> Signed-off-by: Thomas Zimmermann <tzimmermann(a)suse.de>
>> Fixes: b57aa47d39e9 ("drm/gem: Test for imported GEM buffers with helper")
>> Reported-by: Andy Yan <andyshrk(a)163.com>
>> Closes: https://lore.kernel.org/dri-devel/38d09d34.4354.196379aa560.Coremail.andysh…
>> Tested-by: Andy Yan <andyshrk(a)163.com>
>> Cc: Thomas Zimmermann <tzimmermann(a)suse.de>
>> Cc: Anusha Srivatsa <asrivats(a)redhat.com>
>> Cc: Christian König <christian.koenig(a)amd.com>
>> Cc: Maarten Lankhorst <maarten.lankhorst(a)linux.intel.com>
>> Cc: Maxime Ripard <mripard(a)kernel.org>
>> Cc: David Airlie <airlied(a)gmail.com>
>> Cc: Simona Vetter <simona(a)ffwll.ch>
>> Cc: Sumit Semwal <sumit.semwal(a)linaro.org>
>> Cc: "Christian König" <christian.koenig(a)amd.com>
>> Cc: dri-devel(a)lists.freedesktop.org
>> Cc: linux-media(a)vger.kernel.org
>> Cc: linaro-mm-sig(a)lists.linaro.org
>> ---
>> include/drm/drm_gem.h | 26 ++++++++++++++++++++++++--
>> 1 file changed, 24 insertions(+), 2 deletions(-)
>>
>> diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h
>> index 9b71f7a9f3f8..464b9c7feec0 100644
>> --- a/include/drm/drm_gem.h
>> +++ b/include/drm/drm_gem.h
>> @@ -579,6 +579,21 @@ static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_obje
>> return (obj->handle_count > 1) || obj->dma_buf;
>> }
>>
>> +/**
>> + * drm_gem_owns_dma_buf() - Tests if GEM object backs a DMA-buffer object
>> + * @obj: the GEM object
>> + * @obj: the DMA buffer
>> + *
>> + * Returns:
>> + * True if the DMA buffer refers to the GEM object's buffer.
>> + */
>> +static inline bool drm_gem_owns_dma_buf(const struct drm_gem_object *obj,
>> + const struct dma_buf *dma_buf)
>> +{
>> + /* The dma-buf's priv field points to the original GEM object. */
>> + return dma_buf->priv == obj;
>> +}
>> +
>> /**
>> * drm_gem_is_imported() - Tests if GEM object's buffer has been imported
>> * @obj: the GEM object
>> @@ -588,8 +603,15 @@ static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_obje
>> */
>> static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
>> {
>> - /* The dma-buf's priv field points to the original GEM object. */
>> - return obj->dma_buf && (obj->dma_buf->priv != obj);
>> + const struct dma_buf *dma_buf = NULL;
>> +
>> + if (!obj->import_attach)
>> + return false;
>> +
>> + dma_buf = obj->import_attach->dmabuf;
>> +
>> + /* Warn if we somehow reimported our own buffer. */
>> + return !drm_WARN_ON_ONCE(obj->dev, !dma_buf || drm_gem_owns_dma_buf(obj, dma_buf));
> I'm honestly not sure I see the point of checking
> obj->import_attach->dmabuf. If obj->import_attach != NULL, we're sure
> it's a foreign buffer already, otherwise we would get the original GEM
> object which has ->import_attach=NULL. So why not go for a simple
>
> return obj->import_attach != NULL;
>
> check, and extend the check when you get to implement imports without
> import attachments (not sure what those would look like BTW).
I have no strong opinion. I just found it confusing to rely on
import_attach when the dma_buf is what we originally imported.
Best regards
Thomas
--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
Test struct drm_gem_object.import_attach to detect imported objects
during cleanup. At that point, the imported DMA buffer might have
already been released and the dma_buf field is NULL. The object's
free callback then does a cleanup as for native objects.
Happens for calls to drm_mode_destroy_dumb_ioctl() that eventually
clear the dma_buf field in drm_gem_object_exported_dma_buf_free().
Signed-off-by: Thomas Zimmermann <tzimmermann(a)suse.de>
Fixes: b57aa47d39e9 ("drm/gem: Test for imported GEM buffers with helper")
Reported-by: Andy Yan <andyshrk(a)163.com>
Closes: https://lore.kernel.org/dri-devel/38d09d34.4354.196379aa560.Coremail.andysh…
Tested-by: Andy Yan <andyshrk(a)163.com>
Cc: Thomas Zimmermann <tzimmermann(a)suse.de>
Cc: Anusha Srivatsa <asrivats(a)redhat.com>
Cc: Christian König <christian.koenig(a)amd.com>
Cc: Maarten Lankhorst <maarten.lankhorst(a)linux.intel.com>
Cc: Maxime Ripard <mripard(a)kernel.org>
Cc: David Airlie <airlied(a)gmail.com>
Cc: Simona Vetter <simona(a)ffwll.ch>
Cc: Sumit Semwal <sumit.semwal(a)linaro.org>
Cc: "Christian König" <christian.koenig(a)amd.com>
Cc: dri-devel(a)lists.freedesktop.org
Cc: linux-media(a)vger.kernel.org
Cc: linaro-mm-sig(a)lists.linaro.org
---
include/drm/drm_gem.h | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h
index 9b71f7a9f3f8..f09b8afcf86d 100644
--- a/include/drm/drm_gem.h
+++ b/include/drm/drm_gem.h
@@ -589,7 +589,13 @@ static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_obje
static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
{
/* The dma-buf's priv field points to the original GEM object. */
- return obj->dma_buf && (obj->dma_buf->priv != obj);
+ return (obj->dma_buf && (obj->dma_buf->priv != obj)) ||
+ /*
+ * TODO: During object release, the dma-buf might already
+ * be gone. For now keep testing import_attach, but
+ * this should be removed at some point.
+ */
+ obj->import_attach;
}
#ifdef CONFIG_LOCKDEP
--
2.49.0
Am 15.04.25 um 15:10 schrieb Simona Vetter:
>> This is for devices who only want to do a vmap of the buffer, isn't it?
> ... it's for the vmap only case, where you might not even have a struct
> device. Or definitely not a reasonable one, like maybe a faux_bus device
> or some device on a bus that really doesn't do dma (e.g. spi or i2c), and
> where hence dma_buf_map_attachment is just something you never ever want
> to do.
Even in that case I would still suggest to at least create an attachment to let the exporter know that somebody is doing something with it's buffer.
That is also important for move notification since you can't do those without an attachment.
BTW: What is keeping a vmap alive after dropping the reservation lock? There is no pinning whatsoever as far as I can see.
> I think we might want to transform obj->import_attach into a union or
> tagged pointer or something like that, which can cover both cases. And
> maybe a drm_gem_bo_imported_dma_buf() helper that gives you the dma_buf no
> matter what if it's imported, or NULL if it's allocated on that
> drm_device?
Yeah, I had the same idea before as well. Just didn't know if that was something worth looking into.
Regards,
Christian.
>
> Cheers, Sima
Hi,
On Fri, 11 Apr 2025 at 16:05, Adrián Larumbe
<adrian.larumbe(a)collabora.com> wrote:
> +#define PANTHOR_BO_LABEL_MAXLEN PAGE_SIZE
PAGE_SIZE can change between kernel builds with a config setting.
If the thinking here is '4KiB is big enough' (which I agree with),
then just define it to 4096.
Cheers,
Daniel
Am 14.04.25 um 16:27 schrieb Danilo Krummrich:
> On Mon, Apr 14, 2025 at 10:54:25AM +0200, Philipp Stanner wrote:
>> @Danilo:
>> We have now 2 possible solutions for the firing WARN_ON floating.
>>
>> Version A (Christian)
>> Check in nouveau_fence_context_kill() whether a fence is already
>> signaled before setting an error.
>>
>> Version B (Me)
>> This patch series here. Make sure that in Nouveau, only
>> nouveau_fence_signal() signals fences.
>>
>>
>> Both should do the trick. Please share a maintainer-preference so I can
>> move on here.
> Thanks for working on this Philipp.
>
> If you don't want to rework things entirely, A seems to be superior, since it
> also catches the case when someone else would call dma_fence_is_signaled() on a
> nouveau fence (which could happen at any time). This doesn't seem to be caught
> by B, right?
Correct, yes. I would also keep it as simple as possible for backporting this bug fix.
On the other hand a rework is certainly appropriate including both nouveau as well as the DMA-fence calling rules. Especially that the DMA-fence framework calls the signaled callback with inconsistent locking is something we should fix.
Regards,
Christian.