Bug 108381

Summary: amdgpu_bo_import and amdgpu_bo_free are not paired which leaks amdgpu_bo
Product: Mesa Reporter: Yong Zhang <zhangyong>
Component: Drivers/Gallium/radeonsiAssignee: Default DRI bug account <dri-devel>
Status: RESOLVED FIXED QA Contact: Default DRI bug account <dri-devel>
Severity: major    
Priority: medium    
Version: 18.2   
Hardware: All   
OS: Linux (All)   
Whiteboard:
i915 platform: i915 features:
Attachments: sample code to reproduce the issue
possible fix to this issue

Description Yong Zhang 2018-10-16 05:11:55 UTC
Created attachment 142039 [details]
sample code to reproduce the issue

amdgpu_bo is reference counted, libdrm expects the number of calls to amdgpu_bo_import must be equal to amdgpu_bo_free.

When importing prime fd into mesa, amdgpu_bo_from_handle will be called, which calls amdgpu_bo_import to get the unique handle (which in fact is a pointer to amdgpu_bo), then wrapped into amdgpu_winsys_bo object.

The problem is amdgpu_winsys_bo is also reference counted, if one tries to import the same prime fd for multiple times, amdgpu_bo_import will be called for multiple times, but only one amdgpu_winsys_bo will be created, so amdgpu_bo_free will be called for only once, eventually leaks memory.

I have attached a sample code to reproduce this issue.

a possible solution is to call amdgpu_bo_free after amdgpu_bo_import if result.buf_handle can be found in ws->bo_export_table, which will balance the number of calls to amdgpu_bo_import and amdgpu_bo_free.
Comment 1 Yong Zhang 2018-10-16 05:12:58 UTC
Created attachment 142040 [details] [review]
possible fix to this issue

attached possible fix to this issue
Comment 2 Michel Dänzer 2018-10-16 09:17:21 UTC
Please send the patch (with a commit log, generated by git format-patch) to the mesa-dev mailing list for review.
Comment 3 Pierre-Eric Pelloux-Prayer 2019-06-07 08:30:00 UTC
Was fixed by https://gitlab.freedesktop.org/mesa/mesa/commit/82aa07f81fcc5ed696eea16f48cec7e39c3cd3d1

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.