winsys/amdgpu: Close KMS handles for other DRM file descriptions
authorMichel Dänzer <mdaenzer@redhat.com>
Mon, 23 Dec 2019 17:59:10 +0000 (18:59 +0100)
committerMichel Dänzer <michel@daenzer.net>
Thu, 23 Jan 2020 16:39:34 +0000 (17:39 +0100)
When a BO or amdgpu_screen_winsys is destroyed.

Should fix leaking such BOs in other DRM file descriptions.

v2:
* Pass the correct file descriptor to drmIoctl (Pierre-Eric
  Pelloux-Prayer)
* Use _mesa_hash_table_remove
v3:
* Close handles in amdgpu_winsys_unref as well
v4:
* Adapt to amdgpu_winsys::sws_list_lock.

Closes: https://gitlab.freedesktop.org/mesa/mesa/issues/2270
Fixes: 11a3679e3aba "winsys/amdgpu: Make KMS handles valid for original
                     DRM file descriptor"

Reviewed-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Tested-by: Marge Bot <https://gitlab.freedesktop.org/mesa/mesa/merge_requests/3202>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/merge_requests/3202>

src/gallium/winsys/amdgpu/drm/amdgpu_bo.c
src/gallium/winsys/amdgpu/drm/amdgpu_winsys.c

index e6a5a7773e0798139124981a8463a49715c8caa6..5fe1a8379297c0576d8d5332adcecf382b0a2b97 100644 (file)
@@ -183,10 +183,21 @@ void amdgpu_bo_destroy(struct pb_buffer *_buf)
       simple_mtx_unlock(&ws->global_bo_list_lock);
    }
 
+   /* Close all KMS handles retrieved for other DRM file descriptions */
    simple_mtx_lock(&ws->sws_list_lock);
    for (sws_iter = ws->sws_list; sws_iter; sws_iter = sws_iter->next) {
-      if (sws_iter->kms_handles)
-         _mesa_hash_table_remove_key(sws_iter->kms_handles, bo);
+      struct hash_entry *entry;
+
+      if (!sws_iter->kms_handles)
+         continue;
+
+      entry = _mesa_hash_table_search(sws_iter->kms_handles, bo);
+      if (entry) {
+         struct drm_gem_close args = { .handle = (uintptr_t)entry->data };
+
+         drmIoctl(sws_iter->fd, DRM_IOCTL_GEM_CLOSE, &args);
+         _mesa_hash_table_remove(sws_iter->kms_handles, entry);
+      }
    }
    simple_mtx_unlock(&ws->sws_list_lock);
 
index 8fadf584e3fba2ecc6bb758ea6e4847f8bade907..13a4b3ab7f12e38e2566235effeee9eac8b379be 100644 (file)
@@ -304,8 +304,15 @@ static bool amdgpu_winsys_unref(struct radeon_winsys *rws)
 
    simple_mtx_unlock(&aws->sws_list_lock);
 
-   if (ret && sws->kms_handles)
+   if (ret && sws->kms_handles) {
+      struct drm_gem_close args;
+
+      hash_table_foreach(sws->kms_handles, entry) {
+         args.handle = (uintptr_t)entry->data;
+         drmIoctl(sws->fd, DRM_IOCTL_GEM_CLOSE, &args);
+      }
       _mesa_hash_table_destroy(sws->kms_handles, NULL);
+   }
 
    return ret;
 }