OSDN Git Service

libdrm/amdgpu: Use private fd for amdgpu_device and winsys hash table to fix ZaphodHe...
[android-x86/external-libdrm.git] / amdgpu / amdgpu_device.c
index 7a997cb..5f21b32 100644 (file)
@@ -34,6 +34,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include "xf86drm.h"
 #include "amdgpu_drm.h"
@@ -110,7 +111,7 @@ static int fd_compare(void *key1, void *key2)
 static int amdgpu_get_auth(int fd, int *auth)
 {
        int r = 0;
-       drm_client_t client;
+       drm_client_t client = {};
 
        if (drmGetNodeTypeFromFd(fd) == DRM_NODE_RENDER)
                *auth = 0;
@@ -153,7 +154,7 @@ int amdgpu_device_initialize(int fd,
                        return r;
                }
                if ((flag_auth) && (!flag_authexist)) {
-                       dev->flink_fd = fd;
+                       dev->flink_fd = dup(fd);
                }
                *major_version = dev->major_version;
                *minor_version = dev->minor_version;
@@ -168,6 +169,9 @@ int amdgpu_device_initialize(int fd,
                return -ENOMEM;
        }
 
+       dev->fd = -1;
+       dev->flink_fd = -1;
+
        atomic_set(&dev->refcount, 1);
 
        version = drmGetVersion(fd);
@@ -183,8 +187,8 @@ int amdgpu_device_initialize(int fd,
                goto cleanup;
        }
 
-       dev->fd = fd;
-       dev->flink_fd = fd;
+       dev->fd = dup(fd);
+       dev->flink_fd = dev->fd;
        dev->major_version = version->version_major;
        dev->minor_version = version->version_minor;
        drmFreeVersion(version);
@@ -207,17 +211,19 @@ int amdgpu_device_initialize(int fd,
        if (r)
                goto cleanup;
 
-       amdgpu_vamgr_init(dev);
+       dev->vamgr = amdgpu_vamgr_get_global(dev);
 
        *major_version = dev->major_version;
        *minor_version = dev->minor_version;
        *device_handle = dev;
-       util_hash_table_set(fd_tab, UINT_TO_PTR(fd), dev);
+       util_hash_table_set(fd_tab, UINT_TO_PTR(dev->fd), dev);
        pthread_mutex_unlock(&fd_mutex);
 
        return 0;
 
 cleanup:
+       if (dev->fd >= 0)
+               close(dev->fd);
        free(dev);
        pthread_mutex_unlock(&fd_mutex);
        return r;
@@ -225,11 +231,14 @@ cleanup:
 
 void amdgpu_device_free_internal(amdgpu_device_handle dev)
 {
+       amdgpu_vamgr_reference(&dev->vamgr, NULL);
        util_hash_table_destroy(dev->bo_flink_names);
        util_hash_table_destroy(dev->bo_handles);
        pthread_mutex_destroy(&dev->bo_table_mutex);
-       pthread_mutex_destroy(&(dev->vamgr.bo_va_mutex));
        util_hash_table_remove(fd_tab, UINT_TO_PTR(dev->fd));
+       close(dev->fd);
+       if ((dev->flink_fd >= 0) && (dev->fd != dev->flink_fd))
+               close(dev->flink_fd);
        free(dev);
 }