diff mbox

drm/prime: drop reference on imported dma-buf come from gem

Message ID 1348727406-4191-1-git-send-email-sw0312.kim@samsung.com (mailing list archive)
State New, archived
Headers show

Commit Message

Seung-Woo Kim Sept. 27, 2012, 6:30 a.m. UTC
Increasing ref counts of both dma-buf and gem for imported dma-buf come from gem
makes memory leak. release function of dma-buf cannot be called because f_count
of dma-buf increased by importing gem and gem ref count cannot be decrease
because of exported dma-buf.

So I add dma_buf_put() for imported gem come from its own gem into each drivers
having prime_import and prime_export capabilities. With this, only gem ref
count is increased if importing gem exported from gem of same driver.

Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Signed-off-by: Kyungmin.park <kyungmin.park@samsung.com>
Cc: Inki Dae <inki.dae@samsung.com>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: Rob Clark <rob.clark@linaro.org>
Cc: Alex Deucher <alexander.deucher@amd.com>
---
 drivers/gpu/drm/exynos/exynos_drm_dmabuf.c |    5 +++++
 drivers/gpu/drm/i915/i915_gem_dmabuf.c     |    5 +++++
 drivers/gpu/drm/nouveau/nouveau_prime.c    |    1 +
 drivers/gpu/drm/radeon/radeon_prime.c      |    1 +
 drivers/staging/omapdrm/omap_gem_dmabuf.c  |    5 +++++
 5 files changed, 17 insertions(+), 0 deletions(-)

Comments

Rob Clark Sept. 27, 2012, 6:52 a.m. UTC | #1
fwiw, I had a similar patch:

https://patchwork.kernel.org/patch/1229161/

although it was on top of some locking fixes from Daniel (which I
think are also needed):

https://patchwork.kernel.org/patch/1227251/

although that seemed to cause/trigger some explosions which I think
still need to be debugged..

BR,
-R

On Thu, Sep 27, 2012 at 8:30 AM, Seung-Woo Kim <sw0312.kim@samsung.com> wrote:
> Increasing ref counts of both dma-buf and gem for imported dma-buf come from gem
> makes memory leak. release function of dma-buf cannot be called because f_count
> of dma-buf increased by importing gem and gem ref count cannot be decrease
> because of exported dma-buf.
>
> So I add dma_buf_put() for imported gem come from its own gem into each drivers
> having prime_import and prime_export capabilities. With this, only gem ref
> count is increased if importing gem exported from gem of same driver.
>
> Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
> Signed-off-by: Kyungmin.park <kyungmin.park@samsung.com>
> Cc: Inki Dae <inki.dae@samsung.com>
> Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
> Cc: Rob Clark <rob.clark@linaro.org>
> Cc: Alex Deucher <alexander.deucher@amd.com>
> ---
>  drivers/gpu/drm/exynos/exynos_drm_dmabuf.c |    5 +++++
>  drivers/gpu/drm/i915/i915_gem_dmabuf.c     |    5 +++++
>  drivers/gpu/drm/nouveau/nouveau_prime.c    |    1 +
>  drivers/gpu/drm/radeon/radeon_prime.c      |    1 +
>  drivers/staging/omapdrm/omap_gem_dmabuf.c  |    5 +++++
>  5 files changed, 17 insertions(+), 0 deletions(-)
>
> diff --git a/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c b/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
> index ae13feb..b0897c9 100644
> --- a/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
> +++ b/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
> @@ -211,7 +211,12 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
>
>                 /* is it from our device? */
>                 if (obj->dev == drm_dev) {
> +                       /*
> +                        * Importing dmabuf exported from out own gem increases
> +                        * refcount on gem itself instead of f_count of dmabuf.
> +                        */
>                         drm_gem_object_reference(obj);
> +                       dma_buf_put(dma_buf);
>                         return obj;
>                 }
>         }
> diff --git a/drivers/gpu/drm/i915/i915_gem_dmabuf.c b/drivers/gpu/drm/i915/i915_gem_dmabuf.c
> index aa308e1..32e6287 100644
> --- a/drivers/gpu/drm/i915/i915_gem_dmabuf.c
> +++ b/drivers/gpu/drm/i915/i915_gem_dmabuf.c
> @@ -188,7 +188,12 @@ struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
>                 obj = dma_buf->priv;
>                 /* is it from our device? */
>                 if (obj->base.dev == dev) {
> +                       /*
> +                        * Importing dmabuf exported from out own gem increases
> +                        * refcount on gem itself instead of f_count of dmabuf.
> +                        */
>                         drm_gem_object_reference(&obj->base);
> +                       dma_buf_put(dma_buf);
>                         return &obj->base;
>                 }
>         }
> diff --git a/drivers/gpu/drm/nouveau/nouveau_prime.c b/drivers/gpu/drm/nouveau/nouveau_prime.c
> index a25cf2c..bb653c6 100644
> --- a/drivers/gpu/drm/nouveau/nouveau_prime.c
> +++ b/drivers/gpu/drm/nouveau/nouveau_prime.c
> @@ -199,6 +199,7 @@ struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *dev,
>                 if (nvbo->gem) {
>                         if (nvbo->gem->dev == dev) {
>                                 drm_gem_object_reference(nvbo->gem);
> +                               dma_buf_put(dma_buf);
>                                 return nvbo->gem;
>                         }
>                 }
> diff --git a/drivers/gpu/drm/radeon/radeon_prime.c b/drivers/gpu/drm/radeon/radeon_prime.c
> index 6bef46a..d344a3be 100644
> --- a/drivers/gpu/drm/radeon/radeon_prime.c
> +++ b/drivers/gpu/drm/radeon/radeon_prime.c
> @@ -195,6 +195,7 @@ struct drm_gem_object *radeon_gem_prime_import(struct drm_device *dev,
>                 bo = dma_buf->priv;
>                 if (bo->gem_base.dev == dev) {
>                         drm_gem_object_reference(&bo->gem_base);
> +                       dma_buf_put(dma_buf);
>                         return &bo->gem_base;
>                 }
>         }
> diff --git a/drivers/staging/omapdrm/omap_gem_dmabuf.c b/drivers/staging/omapdrm/omap_gem_dmabuf.c
> index 42728e0..5b50eb6 100644
> --- a/drivers/staging/omapdrm/omap_gem_dmabuf.c
> +++ b/drivers/staging/omapdrm/omap_gem_dmabuf.c
> @@ -207,7 +207,12 @@ struct drm_gem_object * omap_gem_prime_import(struct drm_device *dev,
>                 obj = buffer->priv;
>                 /* is it from our device? */
>                 if (obj->dev == dev) {
> +                       /*
> +                        * Importing dmabuf exported from out own gem increases
> +                        * refcount on gem itself instead of f_count of dmabuf.
> +                        */
>                         drm_gem_object_reference(obj);
> +                       dma_buf_put(buffer);
>                         return obj;
>                 }
>         }
> --
> 1.7.4.1
>
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/dri-devel
Seung-Woo Kim Sept. 27, 2012, 7:14 a.m. UTC | #2
Hi Rob,

On 2012? 09? 27? 15:52, Rob Clark wrote:
> fwiw, I had a similar patch:
> 
> https://patchwork.kernel.org/patch/1229161/

Yes, I already check your patch and even my patch's title is a bit from
your patch.

I thought locking issue blocks your patch, so I sent simple fixes on
current state. How do you think merging bug-fixes at first?

Best Regards,
- Seung-Woo Kim

> 
> although it was on top of some locking fixes from Daniel (which I
> think are also needed):
> 
> https://patchwork.kernel.org/patch/1227251/
> 
> although that seemed to cause/trigger some explosions which I think
> still need to be debugged..
> 
> BR,
> -R
> 
> On Thu, Sep 27, 2012 at 8:30 AM, Seung-Woo Kim <sw0312.kim@samsung.com> wrote:
>> Increasing ref counts of both dma-buf and gem for imported dma-buf come from gem
>> makes memory leak. release function of dma-buf cannot be called because f_count
>> of dma-buf increased by importing gem and gem ref count cannot be decrease
>> because of exported dma-buf.
>>
>> So I add dma_buf_put() for imported gem come from its own gem into each drivers
>> having prime_import and prime_export capabilities. With this, only gem ref
>> count is increased if importing gem exported from gem of same driver.
>>
>> Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
>> Signed-off-by: Kyungmin.park <kyungmin.park@samsung.com>
>> Cc: Inki Dae <inki.dae@samsung.com>
>> Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
>> Cc: Rob Clark <rob.clark@linaro.org>
>> Cc: Alex Deucher <alexander.deucher@amd.com>
>> ---
>>  drivers/gpu/drm/exynos/exynos_drm_dmabuf.c |    5 +++++
>>  drivers/gpu/drm/i915/i915_gem_dmabuf.c     |    5 +++++
>>  drivers/gpu/drm/nouveau/nouveau_prime.c    |    1 +
>>  drivers/gpu/drm/radeon/radeon_prime.c      |    1 +
>>  drivers/staging/omapdrm/omap_gem_dmabuf.c  |    5 +++++
>>  5 files changed, 17 insertions(+), 0 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c b/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
>> index ae13feb..b0897c9 100644
>> --- a/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
>> +++ b/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
>> @@ -211,7 +211,12 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
>>
>>                 /* is it from our device? */
>>                 if (obj->dev == drm_dev) {
>> +                       /*
>> +                        * Importing dmabuf exported from out own gem increases
>> +                        * refcount on gem itself instead of f_count of dmabuf.
>> +                        */
>>                         drm_gem_object_reference(obj);
>> +                       dma_buf_put(dma_buf);
>>                         return obj;
>>                 }
>>         }
>> diff --git a/drivers/gpu/drm/i915/i915_gem_dmabuf.c b/drivers/gpu/drm/i915/i915_gem_dmabuf.c
>> index aa308e1..32e6287 100644
>> --- a/drivers/gpu/drm/i915/i915_gem_dmabuf.c
>> +++ b/drivers/gpu/drm/i915/i915_gem_dmabuf.c
>> @@ -188,7 +188,12 @@ struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
>>                 obj = dma_buf->priv;
>>                 /* is it from our device? */
>>                 if (obj->base.dev == dev) {
>> +                       /*
>> +                        * Importing dmabuf exported from out own gem increases
>> +                        * refcount on gem itself instead of f_count of dmabuf.
>> +                        */
>>                         drm_gem_object_reference(&obj->base);
>> +                       dma_buf_put(dma_buf);
>>                         return &obj->base;
>>                 }
>>         }
>> diff --git a/drivers/gpu/drm/nouveau/nouveau_prime.c b/drivers/gpu/drm/nouveau/nouveau_prime.c
>> index a25cf2c..bb653c6 100644
>> --- a/drivers/gpu/drm/nouveau/nouveau_prime.c
>> +++ b/drivers/gpu/drm/nouveau/nouveau_prime.c
>> @@ -199,6 +199,7 @@ struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *dev,
>>                 if (nvbo->gem) {
>>                         if (nvbo->gem->dev == dev) {
>>                                 drm_gem_object_reference(nvbo->gem);
>> +                               dma_buf_put(dma_buf);
>>                                 return nvbo->gem;
>>                         }
>>                 }
>> diff --git a/drivers/gpu/drm/radeon/radeon_prime.c b/drivers/gpu/drm/radeon/radeon_prime.c
>> index 6bef46a..d344a3be 100644
>> --- a/drivers/gpu/drm/radeon/radeon_prime.c
>> +++ b/drivers/gpu/drm/radeon/radeon_prime.c
>> @@ -195,6 +195,7 @@ struct drm_gem_object *radeon_gem_prime_import(struct drm_device *dev,
>>                 bo = dma_buf->priv;
>>                 if (bo->gem_base.dev == dev) {
>>                         drm_gem_object_reference(&bo->gem_base);
>> +                       dma_buf_put(dma_buf);
>>                         return &bo->gem_base;
>>                 }
>>         }
>> diff --git a/drivers/staging/omapdrm/omap_gem_dmabuf.c b/drivers/staging/omapdrm/omap_gem_dmabuf.c
>> index 42728e0..5b50eb6 100644
>> --- a/drivers/staging/omapdrm/omap_gem_dmabuf.c
>> +++ b/drivers/staging/omapdrm/omap_gem_dmabuf.c
>> @@ -207,7 +207,12 @@ struct drm_gem_object * omap_gem_prime_import(struct drm_device *dev,
>>                 obj = buffer->priv;
>>                 /* is it from our device? */
>>                 if (obj->dev == dev) {
>> +                       /*
>> +                        * Importing dmabuf exported from out own gem increases
>> +                        * refcount on gem itself instead of f_count of dmabuf.
>> +                        */
>>                         drm_gem_object_reference(obj);
>> +                       dma_buf_put(buffer);
>>                         return obj;
>>                 }
>>         }
>> --
>> 1.7.4.1
>>
>> _______________________________________________
>> dri-devel mailing list
>> dri-devel@lists.freedesktop.org
>> http://lists.freedesktop.org/mailman/listinfo/dri-devel
>
Rob Clark Sept. 27, 2012, 7:38 a.m. UTC | #3
On Thu, Sep 27, 2012 at 9:14 AM, ??? <sw0312.kim@samsung.com> wrote:
> Hi Rob,
>
> On 2012? 09? 27? 15:52, Rob Clark wrote:
>> fwiw, I had a similar patch:
>>
>> https://patchwork.kernel.org/patch/1229161/
>
> Yes, I already check your patch and even my patch's title is a bit from
> your patch.
>
> I thought locking issue blocks your patch, so I sent simple fixes on
> current state. How do you think merging bug-fixes at first?

well, the lack of locking is a real problem too.. although I didn't
see quite what the locking changes could have to do with the crash
that Dave had seen. But I don't know if anyone has looked at it more,
and I only re-import my own buffers in omapdrm so I don't think I can
trigger the same scenario myself.

I guess if no one else has a chance to take at least a quick look at
the locking crash, maybe we should take this patch.  But I would be
interested get the locking patch too.  We are needing both patches on
our product kernels to fix some issues.

BR,
-R

> Best Regards,
> - Seung-Woo Kim
>
>>
>> although it was on top of some locking fixes from Daniel (which I
>> think are also needed):
>>
>> https://patchwork.kernel.org/patch/1227251/
>>
>> although that seemed to cause/trigger some explosions which I think
>> still need to be debugged..
>>
>> BR,
>> -R
>>
>> On Thu, Sep 27, 2012 at 8:30 AM, Seung-Woo Kim <sw0312.kim@samsung.com> wrote:
>>> Increasing ref counts of both dma-buf and gem for imported dma-buf come from gem
>>> makes memory leak. release function of dma-buf cannot be called because f_count
>>> of dma-buf increased by importing gem and gem ref count cannot be decrease
>>> because of exported dma-buf.
>>>
>>> So I add dma_buf_put() for imported gem come from its own gem into each drivers
>>> having prime_import and prime_export capabilities. With this, only gem ref
>>> count is increased if importing gem exported from gem of same driver.
>>>
>>> Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
>>> Signed-off-by: Kyungmin.park <kyungmin.park@samsung.com>
>>> Cc: Inki Dae <inki.dae@samsung.com>
>>> Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
>>> Cc: Rob Clark <rob.clark@linaro.org>
>>> Cc: Alex Deucher <alexander.deucher@amd.com>
>>> ---
>>>  drivers/gpu/drm/exynos/exynos_drm_dmabuf.c |    5 +++++
>>>  drivers/gpu/drm/i915/i915_gem_dmabuf.c     |    5 +++++
>>>  drivers/gpu/drm/nouveau/nouveau_prime.c    |    1 +
>>>  drivers/gpu/drm/radeon/radeon_prime.c      |    1 +
>>>  drivers/staging/omapdrm/omap_gem_dmabuf.c  |    5 +++++
>>>  5 files changed, 17 insertions(+), 0 deletions(-)
>>>
>>> diff --git a/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c b/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
>>> index ae13feb..b0897c9 100644
>>> --- a/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
>>> +++ b/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
>>> @@ -211,7 +211,12 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
>>>
>>>                 /* is it from our device? */
>>>                 if (obj->dev == drm_dev) {
>>> +                       /*
>>> +                        * Importing dmabuf exported from out own gem increases
>>> +                        * refcount on gem itself instead of f_count of dmabuf.
>>> +                        */
>>>                         drm_gem_object_reference(obj);
>>> +                       dma_buf_put(dma_buf);
>>>                         return obj;
>>>                 }
>>>         }
>>> diff --git a/drivers/gpu/drm/i915/i915_gem_dmabuf.c b/drivers/gpu/drm/i915/i915_gem_dmabuf.c
>>> index aa308e1..32e6287 100644
>>> --- a/drivers/gpu/drm/i915/i915_gem_dmabuf.c
>>> +++ b/drivers/gpu/drm/i915/i915_gem_dmabuf.c
>>> @@ -188,7 +188,12 @@ struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
>>>                 obj = dma_buf->priv;
>>>                 /* is it from our device? */
>>>                 if (obj->base.dev == dev) {
>>> +                       /*
>>> +                        * Importing dmabuf exported from out own gem increases
>>> +                        * refcount on gem itself instead of f_count of dmabuf.
>>> +                        */
>>>                         drm_gem_object_reference(&obj->base);
>>> +                       dma_buf_put(dma_buf);
>>>                         return &obj->base;
>>>                 }
>>>         }
>>> diff --git a/drivers/gpu/drm/nouveau/nouveau_prime.c b/drivers/gpu/drm/nouveau/nouveau_prime.c
>>> index a25cf2c..bb653c6 100644
>>> --- a/drivers/gpu/drm/nouveau/nouveau_prime.c
>>> +++ b/drivers/gpu/drm/nouveau/nouveau_prime.c
>>> @@ -199,6 +199,7 @@ struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *dev,
>>>                 if (nvbo->gem) {
>>>                         if (nvbo->gem->dev == dev) {
>>>                                 drm_gem_object_reference(nvbo->gem);
>>> +                               dma_buf_put(dma_buf);
>>>                                 return nvbo->gem;
>>>                         }
>>>                 }
>>> diff --git a/drivers/gpu/drm/radeon/radeon_prime.c b/drivers/gpu/drm/radeon/radeon_prime.c
>>> index 6bef46a..d344a3be 100644
>>> --- a/drivers/gpu/drm/radeon/radeon_prime.c
>>> +++ b/drivers/gpu/drm/radeon/radeon_prime.c
>>> @@ -195,6 +195,7 @@ struct drm_gem_object *radeon_gem_prime_import(struct drm_device *dev,
>>>                 bo = dma_buf->priv;
>>>                 if (bo->gem_base.dev == dev) {
>>>                         drm_gem_object_reference(&bo->gem_base);
>>> +                       dma_buf_put(dma_buf);
>>>                         return &bo->gem_base;
>>>                 }
>>>         }
>>> diff --git a/drivers/staging/omapdrm/omap_gem_dmabuf.c b/drivers/staging/omapdrm/omap_gem_dmabuf.c
>>> index 42728e0..5b50eb6 100644
>>> --- a/drivers/staging/omapdrm/omap_gem_dmabuf.c
>>> +++ b/drivers/staging/omapdrm/omap_gem_dmabuf.c
>>> @@ -207,7 +207,12 @@ struct drm_gem_object * omap_gem_prime_import(struct drm_device *dev,
>>>                 obj = buffer->priv;
>>>                 /* is it from our device? */
>>>                 if (obj->dev == dev) {
>>> +                       /*
>>> +                        * Importing dmabuf exported from out own gem increases
>>> +                        * refcount on gem itself instead of f_count of dmabuf.
>>> +                        */
>>>                         drm_gem_object_reference(obj);
>>> +                       dma_buf_put(buffer);
>>>                         return obj;
>>>                 }
>>>         }
>>> --
>>> 1.7.4.1
>>>
>>> _______________________________________________
>>> dri-devel mailing list
>>> dri-devel@lists.freedesktop.org
>>> http://lists.freedesktop.org/mailman/listinfo/dri-devel
>>
>
> --
> Seung-Woo Kim
> Samsung Software R&D Center
> --
>
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/dri-devel
Jani Nikula Sept. 27, 2012, 1:43 p.m. UTC | #4
On Thu, 27 Sep 2012, Seung-Woo Kim <sw0312.kim@samsung.com> wrote:
> Increasing ref counts of both dma-buf and gem for imported dma-buf come from gem
> makes memory leak. release function of dma-buf cannot be called because f_count
> of dma-buf increased by importing gem and gem ref count cannot be decrease
> because of exported dma-buf.
>
> So I add dma_buf_put() for imported gem come from its own gem into each drivers
> having prime_import and prime_export capabilities. With this, only gem ref
> count is increased if importing gem exported from gem of same driver.

There's a reference leak bug [1] related to prime self import found by
intel-gpu-tools tests. Without much thinking, I just applied this patch
to see if it makes a difference. Instead, it now fails with:

prime_self_import: prime_self_import.c:61: check_bo: Assertion `ptr1'
failed.

The assert is at [2].

I haven't looked into why this happens at all, so I'm just sharing this
in case you guys find it helpful.

BR,
Jani.



[1] https://bugs.freedesktop.org/show_bug.cgi?id=54111
[2] http://cgit.freedesktop.org/xorg/app/intel-gpu-tools/tree/tests/prime_self_import.c#n61


>
> Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
> Signed-off-by: Kyungmin.park <kyungmin.park@samsung.com>
> Cc: Inki Dae <inki.dae@samsung.com>
> Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
> Cc: Rob Clark <rob.clark@linaro.org>
> Cc: Alex Deucher <alexander.deucher@amd.com>
> ---
>  drivers/gpu/drm/exynos/exynos_drm_dmabuf.c |    5 +++++
>  drivers/gpu/drm/i915/i915_gem_dmabuf.c     |    5 +++++
>  drivers/gpu/drm/nouveau/nouveau_prime.c    |    1 +
>  drivers/gpu/drm/radeon/radeon_prime.c      |    1 +
>  drivers/staging/omapdrm/omap_gem_dmabuf.c  |    5 +++++
>  5 files changed, 17 insertions(+), 0 deletions(-)
>
> diff --git a/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c b/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
> index ae13feb..b0897c9 100644
> --- a/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
> +++ b/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
> @@ -211,7 +211,12 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
>  
>  		/* is it from our device? */
>  		if (obj->dev == drm_dev) {
> +			/*
> +			 * Importing dmabuf exported from out own gem increases
> +			 * refcount on gem itself instead of f_count of dmabuf.
> +			 */
>  			drm_gem_object_reference(obj);
> +			dma_buf_put(dma_buf);
>  			return obj;
>  		}
>  	}
> diff --git a/drivers/gpu/drm/i915/i915_gem_dmabuf.c b/drivers/gpu/drm/i915/i915_gem_dmabuf.c
> index aa308e1..32e6287 100644
> --- a/drivers/gpu/drm/i915/i915_gem_dmabuf.c
> +++ b/drivers/gpu/drm/i915/i915_gem_dmabuf.c
> @@ -188,7 +188,12 @@ struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
>  		obj = dma_buf->priv;
>  		/* is it from our device? */
>  		if (obj->base.dev == dev) {
> +			/*
> +			 * Importing dmabuf exported from out own gem increases
> +			 * refcount on gem itself instead of f_count of dmabuf.
> +			 */
>  			drm_gem_object_reference(&obj->base);
> +			dma_buf_put(dma_buf);
>  			return &obj->base;
>  		}
>  	}
> diff --git a/drivers/gpu/drm/nouveau/nouveau_prime.c b/drivers/gpu/drm/nouveau/nouveau_prime.c
> index a25cf2c..bb653c6 100644
> --- a/drivers/gpu/drm/nouveau/nouveau_prime.c
> +++ b/drivers/gpu/drm/nouveau/nouveau_prime.c
> @@ -199,6 +199,7 @@ struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *dev,
>  		if (nvbo->gem) {
>  			if (nvbo->gem->dev == dev) {
>  				drm_gem_object_reference(nvbo->gem);
> +				dma_buf_put(dma_buf);
>  				return nvbo->gem;
>  			}
>  		}
> diff --git a/drivers/gpu/drm/radeon/radeon_prime.c b/drivers/gpu/drm/radeon/radeon_prime.c
> index 6bef46a..d344a3be 100644
> --- a/drivers/gpu/drm/radeon/radeon_prime.c
> +++ b/drivers/gpu/drm/radeon/radeon_prime.c
> @@ -195,6 +195,7 @@ struct drm_gem_object *radeon_gem_prime_import(struct drm_device *dev,
>  		bo = dma_buf->priv;
>  		if (bo->gem_base.dev == dev) {
>  			drm_gem_object_reference(&bo->gem_base);
> +			dma_buf_put(dma_buf);
>  			return &bo->gem_base;
>  		}
>  	}
> diff --git a/drivers/staging/omapdrm/omap_gem_dmabuf.c b/drivers/staging/omapdrm/omap_gem_dmabuf.c
> index 42728e0..5b50eb6 100644
> --- a/drivers/staging/omapdrm/omap_gem_dmabuf.c
> +++ b/drivers/staging/omapdrm/omap_gem_dmabuf.c
> @@ -207,7 +207,12 @@ struct drm_gem_object * omap_gem_prime_import(struct drm_device *dev,
>  		obj = buffer->priv;
>  		/* is it from our device? */
>  		if (obj->dev == dev) {
> +			/*
> +			 * Importing dmabuf exported from out own gem increases
> +			 * refcount on gem itself instead of f_count of dmabuf.
> +			 */
>  			drm_gem_object_reference(obj);
> +			dma_buf_put(buffer);
>  			return obj;
>  		}
>  	}
> -- 
> 1.7.4.1
>
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/dri-devel
Seung-Woo Kim Nov. 3, 2012, 7:59 a.m. UTC | #5
Hi Jani,

Sorry for late reply.

On 2012? 09? 27? 22:43, Jani Nikula wrote:
> On Thu, 27 Sep 2012, Seung-Woo Kim <sw0312.kim@samsung.com> wrote:
>> Increasing ref counts of both dma-buf and gem for imported dma-buf come from gem
>> makes memory leak. release function of dma-buf cannot be called because f_count
>> of dma-buf increased by importing gem and gem ref count cannot be decrease
>> because of exported dma-buf.
>>
>> So I add dma_buf_put() for imported gem come from its own gem into each drivers
>> having prime_import and prime_export capabilities. With this, only gem ref
>> count is increased if importing gem exported from gem of same driver.
> 
> There's a reference leak bug [1] related to prime self import found by
> intel-gpu-tools tests. Without much thinking, I just applied this patch
> to see if it makes a difference. Instead, it now fails with:
> 
> prime_self_import: prime_self_import.c:61: check_bo: Assertion `ptr1'
> failed.
> 
> The assert is at [2].

I think it was possible to re-use a handle imported_buf list which is
already deleted, and it was fixed after Dave's commit "drm/prime: add
exported buffers to current fprivs imported buffer list (v2)".

> 
> I haven't looked into why this happens at all, so I'm just sharing this
> in case you guys find it helpful.

If you still have same assert, I think it will be very helpful to let me
know exactly where the assert occurs from 5 check_bo() in test
application in your link.

Thanks and Best Regards,
- Seung-Woo Kim

> 
> BR,
> Jani.
> 
> 
> 
> [1] https://bugs.freedesktop.org/show_bug.cgi?id=54111
> [2] http://cgit.freedesktop.org/xorg/app/intel-gpu-tools/tree/tests/prime_self_import.c#n61
> 
> 
>>
>> Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
>> Signed-off-by: Kyungmin.park <kyungmin.park@samsung.com>
>> Cc: Inki Dae <inki.dae@samsung.com>
>> Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
>> Cc: Rob Clark <rob.clark@linaro.org>
>> Cc: Alex Deucher <alexander.deucher@amd.com>
>> ---
>>  drivers/gpu/drm/exynos/exynos_drm_dmabuf.c |    5 +++++
>>  drivers/gpu/drm/i915/i915_gem_dmabuf.c     |    5 +++++
>>  drivers/gpu/drm/nouveau/nouveau_prime.c    |    1 +
>>  drivers/gpu/drm/radeon/radeon_prime.c      |    1 +
>>  drivers/staging/omapdrm/omap_gem_dmabuf.c  |    5 +++++
>>  5 files changed, 17 insertions(+), 0 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c b/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
>> index ae13feb..b0897c9 100644
>> --- a/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
>> +++ b/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
>> @@ -211,7 +211,12 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
>>  
>>  		/* is it from our device? */
>>  		if (obj->dev == drm_dev) {
>> +			/*
>> +			 * Importing dmabuf exported from out own gem increases
>> +			 * refcount on gem itself instead of f_count of dmabuf.
>> +			 */
>>  			drm_gem_object_reference(obj);
>> +			dma_buf_put(dma_buf);
>>  			return obj;
>>  		}
>>  	}
>> diff --git a/drivers/gpu/drm/i915/i915_gem_dmabuf.c b/drivers/gpu/drm/i915/i915_gem_dmabuf.c
>> index aa308e1..32e6287 100644
>> --- a/drivers/gpu/drm/i915/i915_gem_dmabuf.c
>> +++ b/drivers/gpu/drm/i915/i915_gem_dmabuf.c
>> @@ -188,7 +188,12 @@ struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
>>  		obj = dma_buf->priv;
>>  		/* is it from our device? */
>>  		if (obj->base.dev == dev) {
>> +			/*
>> +			 * Importing dmabuf exported from out own gem increases
>> +			 * refcount on gem itself instead of f_count of dmabuf.
>> +			 */
>>  			drm_gem_object_reference(&obj->base);
>> +			dma_buf_put(dma_buf);
>>  			return &obj->base;
>>  		}
>>  	}
>> diff --git a/drivers/gpu/drm/nouveau/nouveau_prime.c b/drivers/gpu/drm/nouveau/nouveau_prime.c
>> index a25cf2c..bb653c6 100644
>> --- a/drivers/gpu/drm/nouveau/nouveau_prime.c
>> +++ b/drivers/gpu/drm/nouveau/nouveau_prime.c
>> @@ -199,6 +199,7 @@ struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *dev,
>>  		if (nvbo->gem) {
>>  			if (nvbo->gem->dev == dev) {
>>  				drm_gem_object_reference(nvbo->gem);
>> +				dma_buf_put(dma_buf);
>>  				return nvbo->gem;
>>  			}
>>  		}
>> diff --git a/drivers/gpu/drm/radeon/radeon_prime.c b/drivers/gpu/drm/radeon/radeon_prime.c
>> index 6bef46a..d344a3be 100644
>> --- a/drivers/gpu/drm/radeon/radeon_prime.c
>> +++ b/drivers/gpu/drm/radeon/radeon_prime.c
>> @@ -195,6 +195,7 @@ struct drm_gem_object *radeon_gem_prime_import(struct drm_device *dev,
>>  		bo = dma_buf->priv;
>>  		if (bo->gem_base.dev == dev) {
>>  			drm_gem_object_reference(&bo->gem_base);
>> +			dma_buf_put(dma_buf);
>>  			return &bo->gem_base;
>>  		}
>>  	}
>> diff --git a/drivers/staging/omapdrm/omap_gem_dmabuf.c b/drivers/staging/omapdrm/omap_gem_dmabuf.c
>> index 42728e0..5b50eb6 100644
>> --- a/drivers/staging/omapdrm/omap_gem_dmabuf.c
>> +++ b/drivers/staging/omapdrm/omap_gem_dmabuf.c
>> @@ -207,7 +207,12 @@ struct drm_gem_object * omap_gem_prime_import(struct drm_device *dev,
>>  		obj = buffer->priv;
>>  		/* is it from our device? */
>>  		if (obj->dev == dev) {
>> +			/*
>> +			 * Importing dmabuf exported from out own gem increases
>> +			 * refcount on gem itself instead of f_count of dmabuf.
>> +			 */
>>  			drm_gem_object_reference(obj);
>> +			dma_buf_put(buffer);
>>  			return obj;
>>  		}
>>  	}
>> -- 
>> 1.7.4.1
>>
>> _______________________________________________
>> dri-devel mailing list
>> dri-devel@lists.freedesktop.org
>> http://lists.freedesktop.org/mailman/listinfo/dri-devel
>
Dave Airlie Dec. 18, 2012, 6:30 a.m. UTC | #6
On Thu, Sep 27, 2012 at 4:30 PM, Seung-Woo Kim <sw0312.kim@samsung.com> wrote:
> Increasing ref counts of both dma-buf and gem for imported dma-buf come from gem
> makes memory leak. release function of dma-buf cannot be called because f_count
> of dma-buf increased by importing gem and gem ref count cannot be decrease
> because of exported dma-buf.
>
> So I add dma_buf_put() for imported gem come from its own gem into each drivers
> having prime_import and prime_export capabilities. With this, only gem ref
> count is increased if importing gem exported from gem of same driver.

Okay its taken me a while to circle around and get back to this, but
yes I admit this is needed, but I hate implementing it like this

But I think I'll push it and work out a cleaner solution, I should
also go back and look at the older patches.

Dave.
Seung-Woo Kim Dec. 18, 2012, 6:59 a.m. UTC | #7
Hi Dave,

On 2012? 12? 18? 15:30, Dave Airlie wrote:
> On Thu, Sep 27, 2012 at 4:30 PM, Seung-Woo Kim <sw0312.kim@samsung.com> wrote:
>> Increasing ref counts of both dma-buf and gem for imported dma-buf come from gem
>> makes memory leak. release function of dma-buf cannot be called because f_count
>> of dma-buf increased by importing gem and gem ref count cannot be decrease
>> because of exported dma-buf.
>>
>> So I add dma_buf_put() for imported gem come from its own gem into each drivers
>> having prime_import and prime_export capabilities. With this, only gem ref
>> count is increased if importing gem exported from gem of same driver.
> 
> Okay its taken me a while to circle around and get back to this, but
> yes I admit this is needed, but I hate implementing it like this
> 
> But I think I'll push it and work out a cleaner solution, I should
> also go back and look at the older patches.

I want to also report some strange thing in dma-buf prime export.

In drm_prime_handle_to_fd_ioctl(), flags is cleared to only support
DRM_CLOEXEC but in gem_prime_export() callbacks of each driver, it uses
0600 as flags for dma_buf_export() like following.

return dma_buf_export(obj, &i915_dmabuf_ops, obj->base.size, 0600);

Best Regards,
- Seung-Woo Kim

> 
> Dave.
>
Dave Airlie Dec. 20, 2012, 12:48 a.m. UTC | #8
> In drm_prime_handle_to_fd_ioctl(), flags is cleared to only support
> DRM_CLOEXEC but in gem_prime_export() callbacks of each driver, it uses
> 0600 as flags for dma_buf_export() like following.
>
> return dma_buf_export(obj, &i915_dmabuf_ops, obj->base.size, 0600);

Oops, nice catch, radeon and nouveau did the correct thing here, I'll
send a patch to fix that.

Dave.
diff mbox

Patch

diff --git a/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c b/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
index ae13feb..b0897c9 100644
--- a/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
+++ b/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c
@@ -211,7 +211,12 @@  struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
 
 		/* is it from our device? */
 		if (obj->dev == drm_dev) {
+			/*
+			 * Importing dmabuf exported from out own gem increases
+			 * refcount on gem itself instead of f_count of dmabuf.
+			 */
 			drm_gem_object_reference(obj);
+			dma_buf_put(dma_buf);
 			return obj;
 		}
 	}
diff --git a/drivers/gpu/drm/i915/i915_gem_dmabuf.c b/drivers/gpu/drm/i915/i915_gem_dmabuf.c
index aa308e1..32e6287 100644
--- a/drivers/gpu/drm/i915/i915_gem_dmabuf.c
+++ b/drivers/gpu/drm/i915/i915_gem_dmabuf.c
@@ -188,7 +188,12 @@  struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
 		obj = dma_buf->priv;
 		/* is it from our device? */
 		if (obj->base.dev == dev) {
+			/*
+			 * Importing dmabuf exported from out own gem increases
+			 * refcount on gem itself instead of f_count of dmabuf.
+			 */
 			drm_gem_object_reference(&obj->base);
+			dma_buf_put(dma_buf);
 			return &obj->base;
 		}
 	}
diff --git a/drivers/gpu/drm/nouveau/nouveau_prime.c b/drivers/gpu/drm/nouveau/nouveau_prime.c
index a25cf2c..bb653c6 100644
--- a/drivers/gpu/drm/nouveau/nouveau_prime.c
+++ b/drivers/gpu/drm/nouveau/nouveau_prime.c
@@ -199,6 +199,7 @@  struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *dev,
 		if (nvbo->gem) {
 			if (nvbo->gem->dev == dev) {
 				drm_gem_object_reference(nvbo->gem);
+				dma_buf_put(dma_buf);
 				return nvbo->gem;
 			}
 		}
diff --git a/drivers/gpu/drm/radeon/radeon_prime.c b/drivers/gpu/drm/radeon/radeon_prime.c
index 6bef46a..d344a3be 100644
--- a/drivers/gpu/drm/radeon/radeon_prime.c
+++ b/drivers/gpu/drm/radeon/radeon_prime.c
@@ -195,6 +195,7 @@  struct drm_gem_object *radeon_gem_prime_import(struct drm_device *dev,
 		bo = dma_buf->priv;
 		if (bo->gem_base.dev == dev) {
 			drm_gem_object_reference(&bo->gem_base);
+			dma_buf_put(dma_buf);
 			return &bo->gem_base;
 		}
 	}
diff --git a/drivers/staging/omapdrm/omap_gem_dmabuf.c b/drivers/staging/omapdrm/omap_gem_dmabuf.c
index 42728e0..5b50eb6 100644
--- a/drivers/staging/omapdrm/omap_gem_dmabuf.c
+++ b/drivers/staging/omapdrm/omap_gem_dmabuf.c
@@ -207,7 +207,12 @@  struct drm_gem_object * omap_gem_prime_import(struct drm_device *dev,
 		obj = buffer->priv;
 		/* is it from our device? */
 		if (obj->dev == dev) {
+			/*
+			 * Importing dmabuf exported from out own gem increases
+			 * refcount on gem itself instead of f_count of dmabuf.
+			 */
 			drm_gem_object_reference(obj);
+			dma_buf_put(buffer);
 			return obj;
 		}
 	}