radv: fix the error code when allocating a fresh imported syncobj fails
[mesa.git] / src / amd / vulkan / radv_device.c
index d16235f7ce1e1d6a309a955f83506938148cf282..5a1a67288d3d9e0474fb4016a18e7b6cc30916d1 100644 (file)
@@ -230,7 +230,7 @@ radv_physical_device_init_mem_types(struct radv_physical_device *device)
 static const char *
 radv_get_compiler_string(struct radv_physical_device *pdevice)
 {
-       if (pdevice->use_aco) {
+       if (!pdevice->use_llvm) {
                /* Some games like SotTR apply shader workarounds if the LLVM
                 * version is too old or if the LLVM version string is
                 * missing. This gives 2-5% performance with SotTR and ACO.
@@ -338,7 +338,7 @@ radv_physical_device_try_create(struct radv_instance *instance,
        device->local_fd = fd;
        device->ws->query_info(device->ws, &device->rad_info);
 
-       device->use_aco = instance->perftest_flags & RADV_PERFTEST_ACO;
+       device->use_llvm = instance->debug_flags & RADV_DEBUG_LLVM;
 
        snprintf(device->name, sizeof(device->name),
                 "AMD RADV %s (%s)",
@@ -351,7 +351,7 @@ radv_physical_device_try_create(struct radv_instance *instance,
        }
 
        /* These flags affect shader compilation. */
-       uint64_t shader_env_flags = (device->use_aco ? 0x2 : 0);
+       uint64_t shader_env_flags = (device->use_llvm ? 0 : 0x2);
 
        /* The gpu id is already embedded in the uuid so we just pass "radv"
         * when creating the cache.
@@ -360,7 +360,7 @@ radv_physical_device_try_create(struct radv_instance *instance,
        disk_cache_format_hex_id(buf, device->cache_uuid, VK_UUID_SIZE * 2);
        device->disk_cache = disk_cache_create(device->name, buf, shader_env_flags);
 
-       if (device->rad_info.chip_class < GFX8)
+       if (device->rad_info.chip_class < GFX8 || !device->use_llvm)
                fprintf(stderr, "WARNING: radv is not a conformant vulkan implementation, testing use only.\n");
 
        radv_get_driver_uuid(&device->driver_uuid);
@@ -372,15 +372,12 @@ radv_physical_device_try_create(struct radv_instance *instance,
        device->dcc_msaa_allowed =
                (device->instance->perftest_flags & RADV_PERFTEST_DCC_MSAA);
 
-       device->use_shader_ballot = (device->use_aco && device->rad_info.chip_class >= GFX8) ||
-                                   (device->instance->perftest_flags & RADV_PERFTEST_SHADER_BALLOT);
-
        device->use_ngg = device->rad_info.chip_class >= GFX10 &&
                          device->rad_info.family != CHIP_NAVI14 &&
                          !(device->instance->debug_flags & RADV_DEBUG_NO_NGG);
 
        /* TODO: Implement NGG GS with ACO. */
-       device->use_ngg_gs = device->use_ngg && !device->use_aco;
+       device->use_ngg_gs = device->use_ngg && device->use_llvm;
        device->use_ngg_streamout = false;
 
        /* Determine the number of threads per wave for all stages. */
@@ -501,12 +498,11 @@ static const struct debug_control radv_debug_options[] = {
        {"checkir", RADV_DEBUG_CHECKIR},
        {"nothreadllvm", RADV_DEBUG_NOTHREADLLVM},
        {"nobinning", RADV_DEBUG_NOBINNING},
-       {"noloadstoreopt", RADV_DEBUG_NO_LOAD_STORE_OPT},
        {"nongg", RADV_DEBUG_NO_NGG},
-       {"noshaderballot", RADV_DEBUG_NO_SHADER_BALLOT},
        {"allentrypoints", RADV_DEBUG_ALL_ENTRYPOINTS},
        {"metashaders", RADV_DEBUG_DUMP_META_SHADERS},
        {"nomemorycache", RADV_DEBUG_NO_MEMORY_CACHE},
+       {"llvm", RADV_DEBUG_LLVM},
        {NULL, 0}
 };
 
@@ -521,13 +517,11 @@ static const struct debug_control radv_perftest_options[] = {
        {"localbos", RADV_PERFTEST_LOCAL_BOS},
        {"dccmsaa", RADV_PERFTEST_DCC_MSAA},
        {"bolist", RADV_PERFTEST_BO_LIST},
-       {"shader_ballot", RADV_PERFTEST_SHADER_BALLOT},
        {"tccompatcmask", RADV_PERFTEST_TC_COMPAT_CMASK},
        {"cswave32", RADV_PERFTEST_CS_WAVE_32},
        {"pswave32", RADV_PERFTEST_PS_WAVE_32},
        {"gewave32", RADV_PERFTEST_GE_WAVE_32},
        {"dfsm", RADV_PERFTEST_DFSM},
-       {"aco", RADV_PERFTEST_ACO},
        {NULL, 0}
 };
 
@@ -549,22 +543,6 @@ radv_handle_per_app_options(struct radv_instance *instance,
                if (!strcmp(name, "DOOM_VFR")) {
                        /* Work around a Doom VFR game bug */
                        instance->debug_flags |= RADV_DEBUG_NO_DYNAMIC_BOUNDS;
-               } else if (!strcmp(name, "MonsterHunterWorld.exe")) {
-                       /* Workaround for a WaW hazard when LLVM moves/merges
-                        * load/store memory operations.
-                        * See https://reviews.llvm.org/D61313
-                        */
-                       if (LLVM_VERSION_MAJOR < 9)
-                               instance->debug_flags |= RADV_DEBUG_NO_LOAD_STORE_OPT;
-               } else if (!strcmp(name, "Wolfenstein: Youngblood")) {
-                       if (!(instance->debug_flags & RADV_DEBUG_NO_SHADER_BALLOT) &&
-                           !(instance->perftest_flags & RADV_PERFTEST_ACO)) {
-                               /* Force enable VK_AMD_shader_ballot because it looks
-                                * safe and it gives a nice boost (+20% on Vega 56 at
-                                * this time). It also prevents corruption on LLVM.
-                                */
-                               instance->perftest_flags |= RADV_PERFTEST_SHADER_BALLOT;
-                       }
                } else if (!strcmp(name, "Fledge")) {
                        /*
                         * Zero VRAM for "The Surge 2"
@@ -591,6 +569,9 @@ radv_handle_per_app_options(struct radv_instance *instance,
                         * rendering issues.
                         */
                        instance->debug_flags |= RADV_DEBUG_ZERO_VRAM;
+               } else if (!strcmp(engine_name, "Quantic Dream Engine")) {
+                       /* Fix various artifacts in Detroit: Become Human */
+                       instance->debug_flags |= RADV_DEBUG_ZERO_VRAM;
                }
        }
 
@@ -608,6 +589,7 @@ DRI_CONF_BEGIN
                DRI_CONF_ADAPTIVE_SYNC("true")
                DRI_CONF_VK_X11_OVERRIDE_MIN_IMAGE_COUNT(0)
                DRI_CONF_VK_X11_STRICT_IMAGE_COUNT("false")
+               DRI_CONF_VK_X11_ENSURE_MIN_IMAGE_COUNT("false")
                DRI_CONF_RADV_REPORT_LLVM9_VERSION_STRING("false")
                DRI_CONF_RADV_ENABLE_MRT_OUTPUT_NAN_FIXUP("false")
                DRI_CONF_RADV_NO_DYNAMIC_BOUNDS("false")
@@ -661,25 +643,12 @@ VkResult radv_CreateInstance(
        if (instance->apiVersion == 0)
                instance->apiVersion = VK_API_VERSION_1_0;
 
-       /* Get secure compile thread count. NOTE: We cap this at 32 */
-#define MAX_SC_PROCS 32
-       char *num_sc_threads = getenv("RADV_SECURE_COMPILE_THREADS");
-       if (num_sc_threads)
-               instance->num_sc_threads = MIN2(strtoul(num_sc_threads, NULL, 10), MAX_SC_PROCS);
-
        instance->debug_flags = parse_debug_string(getenv("RADV_DEBUG"),
                                                   radv_debug_options);
 
-       /* Disable memory cache when secure compile is set */
-       if (radv_device_use_secure_compile(instance))
-               instance->debug_flags |= RADV_DEBUG_NO_MEMORY_CACHE;
-
        instance->perftest_flags = parse_debug_string(getenv("RADV_PERFTEST"),
                                                   radv_perftest_options);
 
-       if (instance->perftest_flags & RADV_PERFTEST_ACO)
-               fprintf(stderr, "WARNING: Experimental compiler backend enabled. Here be dragons! Incorrect rendering, GPU hangs and/or resets are likely\n");
-
        if (instance->debug_flags & RADV_DEBUG_STARTUP)
                radv_logi("Created an instance");
 
@@ -693,6 +662,7 @@ VkResult radv_CreateInstance(
 
                if (idx >= RADV_INSTANCE_EXTENSION_COUNT ||
                    !radv_instance_extensions_supported.extensions[idx]) {
+                       vk_object_base_finish(&instance->base);
                        vk_free2(&default_alloc, pAllocator, instance);
                        return vk_error(instance, VK_ERROR_EXTENSION_NOT_PRESENT);
                }
@@ -749,6 +719,7 @@ VkResult radv_CreateInstance(
 
        result = vk_debug_report_instance_init(&instance->debug_report_callbacks);
        if (result != VK_SUCCESS) {
+               vk_object_base_finish(&instance->base);
                vk_free2(&default_alloc, pAllocator, instance);
                return vk_error(instance, result);
        }
@@ -971,7 +942,7 @@ radv_get_physical_device_features_1_1(struct radv_physical_device *pdevice,
        f->storageBuffer16BitAccess            = true;
        f->uniformAndStorageBuffer16BitAccess  = true;
        f->storagePushConstant16               = true;
-       f->storageInputOutput16                = pdevice->rad_info.has_packed_math_16bit && (LLVM_VERSION_MAJOR >= 9 || pdevice->use_aco);
+       f->storageInputOutput16                = pdevice->rad_info.has_packed_math_16bit && (LLVM_VERSION_MAJOR >= 9 || !pdevice->use_llvm);
        f->multiview                           = true;
        f->multiviewGeometryShader             = true;
        f->multiviewTessellationShader         = true;
@@ -993,8 +964,8 @@ radv_get_physical_device_features_1_2(struct radv_physical_device *pdevice,
        f->storageBuffer8BitAccess = true;
        f->uniformAndStorageBuffer8BitAccess = true;
        f->storagePushConstant8 = true;
-       f->shaderBufferInt64Atomics = LLVM_VERSION_MAJOR >= 9 || pdevice->use_aco;
-       f->shaderSharedInt64Atomics = LLVM_VERSION_MAJOR >= 9 || pdevice->use_aco;
+       f->shaderBufferInt64Atomics = LLVM_VERSION_MAJOR >= 9 || !pdevice->use_llvm;
+       f->shaderSharedInt64Atomics = LLVM_VERSION_MAJOR >= 9 || !pdevice->use_llvm;
        f->shaderFloat16 = pdevice->rad_info.has_packed_math_16bit;
        f->shaderInt8 = true;
 
@@ -1212,7 +1183,7 @@ void radv_GetPhysicalDeviceFeatures2(
                case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DEMOTE_TO_HELPER_INVOCATION_FEATURES_EXT: {
                        VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT *features =
                                (VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT *)ext;
-                       features->shaderDemoteToHelperInvocation = LLVM_VERSION_MAJOR >= 9 || pdevice->use_aco;
+                       features->shaderDemoteToHelperInvocation = LLVM_VERSION_MAJOR >= 9 || !pdevice->use_llvm;
                        break;
                }
                case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_FEATURES_EXT: {
@@ -1356,6 +1327,12 @@ void radv_GetPhysicalDeviceFeatures2(
                        features-> pipelineCreationCacheControl = true;
                        break;
                }
+               case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT: {
+                       VkPhysicalDeviceExtendedDynamicStateFeaturesEXT *features =
+                               (VkPhysicalDeviceExtendedDynamicStateFeaturesEXT *) ext;
+                       features->extendedDynamicState = true;
+                       break;
+               }
                default:
                        break;
                }
@@ -1492,8 +1469,8 @@ void radv_GetPhysicalDeviceProperties(
                .maxCullDistances                         = 8,
                .maxCombinedClipAndCullDistances          = 8,
                .discreteQueuePriorities                  = 2,
-               .pointSizeRange                           = { 0.0, 8192.0 },
-               .lineWidthRange                           = { 0.0, 8192.0 },
+               .pointSizeRange                           = { 0.0, 8191.875 },
+               .lineWidthRange                           = { 0.0, 8191.875 },
                .pointSizeGranularity                     = (1.0 / 8.0),
                .lineWidthGranularity                     = (1.0 / 8.0),
                .strictLines                              = false, /* FINISHME */
@@ -2393,537 +2370,6 @@ radv_get_int_debug_option(const char *name, int default_value)
        return result;
 }
 
-static int install_seccomp_filter() {
-
-       struct sock_filter filter[] = {
-               /* Check arch is 64bit x86 */
-               BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, arch))),
-               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, AUDIT_ARCH_X86_64, 0, 12),
-
-               /* Futex is required for mutex locks */
-               #if defined __NR__newselect
-               BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, nr))),
-               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR__newselect, 11, 0),
-               #elif defined __NR_select
-               BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, nr))),
-               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR_select, 11, 0),
-               #else
-               BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, nr))),
-               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR_pselect6, 11, 0),
-               #endif
-
-               /* Allow system exit calls for the forked process */
-               BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, nr))),
-               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR_exit_group, 9, 0),
-
-               /* Allow system read calls */
-               BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, nr))),
-               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR_read, 7, 0),
-
-               /* Allow system write calls */
-               BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, nr))),
-               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR_write, 5, 0),
-
-               /* Allow system brk calls (we need this for malloc) */
-               BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, nr))),
-               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR_brk, 3, 0),
-
-               /* Futex is required for mutex locks */
-               BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, nr))),
-               BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR_futex, 1, 0),
-
-               /* Return error if we hit a system call not on the whitelist */
-               BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ERRNO | (EPERM & SECCOMP_RET_DATA)),
-
-               /* Allow whitelisted system calls */
-               BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW),
-       };
-
-       struct sock_fprog prog = {
-               .len = (unsigned short)(sizeof(filter) / sizeof(filter[0])),
-               .filter = filter,
-       };
-
-       if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
-               return -1;
-
-       if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog))
-               return -1;
-
-       return 0;
-}
-
-/* Helper function with timeout support for reading from the pipe between
- * processes used for secure compile.
- */
-bool radv_sc_read(int fd, void *buf, size_t size, bool timeout)
-{
-       fd_set fds;
-       struct timeval tv;
-
-       FD_ZERO(&fds);
-       FD_SET(fd, &fds);
-
-       while (true) {
-               /* We can't rely on the value of tv after calling select() so
-                * we must reset it on each iteration of the loop.
-                */
-               tv.tv_sec = 5;
-               tv.tv_usec = 0;
-
-               int rval = select(fd + 1, &fds, NULL, NULL, timeout ? &tv : NULL);
-
-               if (rval == -1) {
-                       /* select error */
-                       return false;
-               } else if (rval) {
-                       ssize_t bytes_read = read(fd, buf, size);
-                       if (bytes_read < 0)
-                               return false;
-
-                       buf += bytes_read;
-                       size -= bytes_read;
-                       if (size == 0)
-                               return true;
-               } else {
-                       /* select timeout */
-                       return false;
-               }
-       }
-}
-
-static bool radv_close_all_fds(const int *keep_fds, int keep_fd_count)
-{
-       DIR *d;
-       struct dirent *dir;
-       d = opendir("/proc/self/fd");
-       if (!d)
-               return false;
-       int dir_fd = dirfd(d);
-
-       while ((dir = readdir(d)) != NULL) {
-               if (dir->d_name[0] == '.')
-                       continue;
-
-               int fd = atoi(dir->d_name);
-               if (fd == dir_fd)
-                       continue;
-
-               bool keep = false;
-               for (int i = 0; !keep && i < keep_fd_count; ++i)
-                       if (keep_fds[i] == fd)
-                               keep = true;
-
-               if (keep)
-                       continue;
-
-               close(fd);
-       }
-       closedir(d);
-       return true;
-}
-
-static bool secure_compile_open_fifo_fds(struct radv_secure_compile_state *sc,
-                                        int *fd_server, int *fd_client,
-                                        unsigned process, bool make_fifo)
-{
-       bool result = false;
-       char *fifo_server_path = NULL;
-       char *fifo_client_path = NULL;
-
-       if (asprintf(&fifo_server_path, "/tmp/radv_server_%s_%u", sc->uid, process) == -1)
-               goto open_fifo_exit;
-
-       if (asprintf(&fifo_client_path, "/tmp/radv_client_%s_%u", sc->uid, process) == -1)
-               goto open_fifo_exit;
-
-       if (make_fifo) {
-               int file1 = mkfifo(fifo_server_path, 0666);
-               if(file1 < 0)
-                       goto open_fifo_exit;
-
-               int file2 = mkfifo(fifo_client_path, 0666);
-               if(file2 < 0)
-                       goto open_fifo_exit;
-       }
-
-       *fd_server = open(fifo_server_path, O_RDWR);
-       if(*fd_server < 1)
-               goto open_fifo_exit;
-
-       *fd_client = open(fifo_client_path, O_RDWR);
-       if(*fd_client < 1) {
-               close(*fd_server);
-               goto open_fifo_exit;
-       }
-
-       result = true;
-
-open_fifo_exit:
-       free(fifo_server_path);
-       free(fifo_client_path);
-
-       return result;
-}
-
-static void run_secure_compile_device(struct radv_device *device, unsigned process,
-                                     int fd_idle_device_output)
-{
-       int fd_secure_input;
-       int fd_secure_output;
-       bool fifo_result = secure_compile_open_fifo_fds(device->sc_state,
-                                                       &fd_secure_input,
-                                                       &fd_secure_output,
-                                                       process, false);
-
-       enum radv_secure_compile_type sc_type;
-
-       const int needed_fds[] = {
-               fd_secure_input,
-               fd_secure_output,
-               fd_idle_device_output,
-       };
-
-       if (!fifo_result || !radv_close_all_fds(needed_fds, ARRAY_SIZE(needed_fds)) ||
-           install_seccomp_filter() == -1) {
-               sc_type = RADV_SC_TYPE_INIT_FAILURE;
-       } else {
-               sc_type = RADV_SC_TYPE_INIT_SUCCESS;
-               device->sc_state->secure_compile_processes[process].fd_secure_input = fd_secure_input;
-               device->sc_state->secure_compile_processes[process].fd_secure_output = fd_secure_output;
-       }
-
-       write(fd_idle_device_output, &sc_type, sizeof(sc_type));
-
-       if (sc_type == RADV_SC_TYPE_INIT_FAILURE)
-               goto secure_compile_exit;
-
-       while (true) {
-               radv_sc_read(fd_secure_input, &sc_type, sizeof(sc_type), false);
-
-               if (sc_type == RADV_SC_TYPE_COMPILE_PIPELINE) {
-                       struct radv_pipeline *pipeline;
-                       bool sc_read = true;
-
-                       pipeline = vk_zalloc2(&device->vk.alloc, NULL, sizeof(*pipeline), 8,
-                                             VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
-
-                       pipeline->device = device;
-
-                       /* Read pipeline layout */
-                       struct radv_pipeline_layout layout;
-                       sc_read = radv_sc_read(fd_secure_input, &layout, sizeof(struct radv_pipeline_layout), true);
-                       sc_read &= radv_sc_read(fd_secure_input, &layout.num_sets, sizeof(uint32_t), true);
-                       if (!sc_read)
-                               goto secure_compile_exit;
-
-                       for (uint32_t set = 0; set < layout.num_sets; set++) {
-                               uint32_t layout_size;
-                               sc_read &= radv_sc_read(fd_secure_input, &layout_size, sizeof(uint32_t), true);
-                               if (!sc_read)
-                                       goto secure_compile_exit;
-
-                               layout.set[set].layout = malloc(layout_size);
-                               layout.set[set].layout->layout_size = layout_size;
-                               sc_read &= radv_sc_read(fd_secure_input, layout.set[set].layout,
-                                                       layout.set[set].layout->layout_size, true);
-                       }
-
-                       pipeline->layout = &layout;
-
-                       /* Read pipeline key */
-                       struct radv_pipeline_key key;
-                       sc_read &= radv_sc_read(fd_secure_input, &key, sizeof(struct radv_pipeline_key), true);
-
-                       /* Read pipeline create flags */
-                       VkPipelineCreateFlags flags;
-                       sc_read &= radv_sc_read(fd_secure_input, &flags, sizeof(VkPipelineCreateFlags), true);
-
-                       /* Read stage and shader information */
-                       uint32_t num_stages;
-                       const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = { 0, };
-                       sc_read &= radv_sc_read(fd_secure_input, &num_stages, sizeof(uint32_t), true);
-                       if (!sc_read)
-                               goto secure_compile_exit;
-
-                       for (uint32_t i = 0; i < num_stages; i++) {
-
-                               /* Read stage */
-                               gl_shader_stage stage;
-                               sc_read &= radv_sc_read(fd_secure_input, &stage, sizeof(gl_shader_stage), true);
-
-                               VkPipelineShaderStageCreateInfo *pStage = calloc(1, sizeof(VkPipelineShaderStageCreateInfo));
-
-                               /* Read entry point name */
-                               size_t name_size;
-                               sc_read &= radv_sc_read(fd_secure_input, &name_size, sizeof(size_t), true);
-                               if (!sc_read)
-                                       goto secure_compile_exit;
-
-                               char *ep_name = malloc(name_size);
-                               sc_read &= radv_sc_read(fd_secure_input, ep_name, name_size, true);
-                               pStage->pName = ep_name;
-
-                               /* Read shader module */
-                               size_t module_size;
-                               sc_read &= radv_sc_read(fd_secure_input, &module_size, sizeof(size_t), true);
-                               if (!sc_read)
-                                       goto secure_compile_exit;
-
-                               struct radv_shader_module *module = malloc(module_size);
-                               sc_read &= radv_sc_read(fd_secure_input, module, module_size, true);
-                               pStage->module = radv_shader_module_to_handle(module);
-
-                               /* Read specialization info */
-                               bool has_spec_info;
-                               sc_read &= radv_sc_read(fd_secure_input, &has_spec_info, sizeof(bool), true);
-                               if (!sc_read)
-                                       goto secure_compile_exit;
-
-                               if (has_spec_info) {
-                                       VkSpecializationInfo *specInfo = malloc(sizeof(VkSpecializationInfo));
-                                       pStage->pSpecializationInfo = specInfo;
-
-                                       sc_read &= radv_sc_read(fd_secure_input, &specInfo->dataSize, sizeof(size_t), true);
-                                       if (!sc_read)
-                                               goto secure_compile_exit;
-
-                                       void *si_data = malloc(specInfo->dataSize);
-                                       sc_read &= radv_sc_read(fd_secure_input, si_data, specInfo->dataSize, true);
-                                       specInfo->pData = si_data;
-
-                                       sc_read &= radv_sc_read(fd_secure_input, &specInfo->mapEntryCount, sizeof(uint32_t), true);
-                                       if (!sc_read)
-                                               goto secure_compile_exit;
-
-                                       VkSpecializationMapEntry *mapEntries = malloc(sizeof(VkSpecializationMapEntry) * specInfo->mapEntryCount);
-                                       for (uint32_t j = 0; j < specInfo->mapEntryCount; j++) {
-                                               sc_read &= radv_sc_read(fd_secure_input, &mapEntries[j], sizeof(VkSpecializationMapEntry), true);
-                                               if (!sc_read)
-                                                       goto secure_compile_exit;
-                                       }
-
-                                       specInfo->pMapEntries = mapEntries;
-                               }
-
-                               pStages[stage] = pStage;
-                       }
-
-                       /* Compile the shaders */
-                       VkPipelineCreationFeedbackEXT *stage_feedbacks[MESA_SHADER_STAGES] = { 0 };
-
-                       /* Not fully to spec but if we're doing sandboxed compilations already this doesn't matter. */
-                       flags &= ~VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT_EXT;
-
-                       radv_create_shaders(pipeline, device, NULL, &key, pStages, flags, NULL, stage_feedbacks);
-
-                       /* free memory allocated above */
-                       for (uint32_t set = 0; set < layout.num_sets; set++)
-                               free(layout.set[set].layout);
-
-                       for (uint32_t i = 0; i < MESA_SHADER_STAGES; i++) {
-                               if (!pStages[i])
-                                       continue;
-
-                               free((void *) pStages[i]->pName);
-                               free(radv_shader_module_from_handle(pStages[i]->module));
-                               if (pStages[i]->pSpecializationInfo) {
-                                       free((void *) pStages[i]->pSpecializationInfo->pData);
-                                       free((void *) pStages[i]->pSpecializationInfo->pMapEntries);
-                                       free((void *) pStages[i]->pSpecializationInfo);
-                               }
-                               free((void *) pStages[i]);
-                       }
-
-                       vk_free(&device->vk.alloc, pipeline);
-
-                       sc_type = RADV_SC_TYPE_COMPILE_PIPELINE_FINISHED;
-                       write(fd_secure_output, &sc_type, sizeof(sc_type));
-
-               } else if (sc_type == RADV_SC_TYPE_DESTROY_DEVICE) {
-                       goto secure_compile_exit;
-               }
-       }
-
-secure_compile_exit:
-       close(fd_secure_input);
-       close(fd_secure_output);
-       close(fd_idle_device_output);
-       _exit(0);
-}
-
-static enum radv_secure_compile_type fork_secure_compile_device(struct radv_device *device, unsigned process)
-{
-       int fd_secure_input[2];
-       int fd_secure_output[2];
-
-       /* create pipe descriptors (used to communicate between processes) */
-       if (pipe(fd_secure_input) == -1 || pipe(fd_secure_output) == -1)
-               return RADV_SC_TYPE_INIT_FAILURE;
-
-
-       int sc_pid;
-       if ((sc_pid = fork()) == 0) {
-               device->sc_state->secure_compile_thread_counter = process;
-               run_secure_compile_device(device, process, fd_secure_output[1]);
-       } else {
-               if (sc_pid == -1)
-                       return RADV_SC_TYPE_INIT_FAILURE;
-
-               /* Read the init result returned from the secure process */
-               enum radv_secure_compile_type sc_type;
-               bool sc_read = radv_sc_read(fd_secure_output[0], &sc_type, sizeof(sc_type), true);
-
-               if (sc_type == RADV_SC_TYPE_INIT_FAILURE || !sc_read) {
-                       close(fd_secure_input[0]);
-                       close(fd_secure_input[1]);
-                       close(fd_secure_output[1]);
-                       close(fd_secure_output[0]);
-                       int status;
-                       waitpid(sc_pid, &status, 0);
-
-                       return RADV_SC_TYPE_INIT_FAILURE;
-               } else {
-                       assert(sc_type == RADV_SC_TYPE_INIT_SUCCESS);
-                       write(device->sc_state->secure_compile_processes[process].fd_secure_output, &sc_type, sizeof(sc_type));
-
-                       close(fd_secure_input[0]);
-                       close(fd_secure_input[1]);
-                       close(fd_secure_output[1]);
-                       close(fd_secure_output[0]);
-
-                       int status;
-                       waitpid(sc_pid, &status, 0);
-               }
-       }
-
-       return RADV_SC_TYPE_INIT_SUCCESS;
-}
-
-/* Run a bare bones fork of a device that was forked right after its creation.
- * This device will have low overhead when it is forked again before each
- * pipeline compilation. This device sits idle and its only job is to fork
- * itself.
- */
-static void run_secure_compile_idle_device(struct radv_device *device, unsigned process,
-                                           int fd_secure_input, int fd_secure_output)
-{
-       enum radv_secure_compile_type sc_type = RADV_SC_TYPE_INIT_SUCCESS;
-       device->sc_state->secure_compile_processes[process].fd_secure_input = fd_secure_input;
-       device->sc_state->secure_compile_processes[process].fd_secure_output = fd_secure_output;
-
-       write(fd_secure_output, &sc_type, sizeof(sc_type));
-
-       while (true) {
-               radv_sc_read(fd_secure_input, &sc_type, sizeof(sc_type), false);
-
-               if (sc_type == RADV_SC_TYPE_FORK_DEVICE) {
-                       sc_type = fork_secure_compile_device(device, process);
-
-                       if (sc_type == RADV_SC_TYPE_INIT_FAILURE)
-                               goto secure_compile_exit;
-
-               } else if (sc_type == RADV_SC_TYPE_DESTROY_DEVICE) {
-                       goto secure_compile_exit;
-               }
-       }
-
-secure_compile_exit:
-       close(fd_secure_input);
-       close(fd_secure_output);
-       _exit(0);
-}
-
-static void destroy_secure_compile_device(struct radv_device *device, unsigned process)
-{
-       int fd_secure_input = device->sc_state->secure_compile_processes[process].fd_secure_input;
-
-       enum radv_secure_compile_type sc_type = RADV_SC_TYPE_DESTROY_DEVICE;
-       write(fd_secure_input, &sc_type, sizeof(sc_type));
-
-       close(device->sc_state->secure_compile_processes[process].fd_secure_input);
-       close(device->sc_state->secure_compile_processes[process].fd_secure_output);
-
-       int status;
-       waitpid(device->sc_state->secure_compile_processes[process].sc_pid, &status, 0);
-}
-
-static VkResult fork_secure_compile_idle_device(struct radv_device *device)
-{
-       device->sc_state = vk_zalloc(&device->vk.alloc,
-                                    sizeof(struct radv_secure_compile_state),
-                                    8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
-
-       mtx_init(&device->sc_state->secure_compile_mutex, mtx_plain);
-
-       pid_t upid = getpid();
-       time_t seconds = time(NULL);
-
-       char *uid;
-       if (asprintf(&uid, "%ld_%ld", (long) upid, (long) seconds) == -1)
-               return VK_ERROR_INITIALIZATION_FAILED;
-
-       device->sc_state->uid = uid;
-
-       uint8_t sc_threads = device->instance->num_sc_threads;
-       int fd_secure_input[MAX_SC_PROCS][2];
-       int fd_secure_output[MAX_SC_PROCS][2];
-
-       /* create pipe descriptors (used to communicate between processes) */
-       for (unsigned i = 0; i < sc_threads; i++) {
-               if (pipe(fd_secure_input[i]) == -1 ||
-                   pipe(fd_secure_output[i]) == -1) {
-                       return VK_ERROR_INITIALIZATION_FAILED;
-               }
-       }
-
-       device->sc_state->secure_compile_processes = vk_zalloc(&device->vk.alloc,
-                                                               sizeof(struct radv_secure_compile_process) * sc_threads, 8,
-                                                               VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
-
-       for (unsigned process = 0; process < sc_threads; process++) {
-               if ((device->sc_state->secure_compile_processes[process].sc_pid = fork()) == 0) {
-                       device->sc_state->secure_compile_thread_counter = process;
-                       run_secure_compile_idle_device(device, process, fd_secure_input[process][0], fd_secure_output[process][1]);
-               } else {
-                       if (device->sc_state->secure_compile_processes[process].sc_pid == -1)
-                               return VK_ERROR_INITIALIZATION_FAILED;
-
-                       /* Read the init result returned from the secure process */
-                       enum radv_secure_compile_type sc_type;
-                       bool sc_read = radv_sc_read(fd_secure_output[process][0], &sc_type, sizeof(sc_type), true);
-
-                       bool fifo_result;
-                       if (sc_read && sc_type == RADV_SC_TYPE_INIT_SUCCESS) {
-                               fifo_result = secure_compile_open_fifo_fds(device->sc_state,
-                                                                          &device->sc_state->secure_compile_processes[process].fd_server,
-                                                                          &device->sc_state->secure_compile_processes[process].fd_client,
-                                                                          process, true);
-
-                               device->sc_state->secure_compile_processes[process].fd_secure_input = fd_secure_input[process][1];
-                               device->sc_state->secure_compile_processes[process].fd_secure_output = fd_secure_output[process][0];
-                       }
-
-                       if (sc_type == RADV_SC_TYPE_INIT_FAILURE || !sc_read || !fifo_result) {
-                               close(fd_secure_input[process][0]);
-                               close(fd_secure_input[process][1]);
-                               close(fd_secure_output[process][1]);
-                               close(fd_secure_output[process][0]);
-                               int status;
-                               waitpid(device->sc_state->secure_compile_processes[process].sc_pid, &status, 0);
-
-                               /* Destroy any forks that were created sucessfully */
-                               for (unsigned i = 0; i < process; i++) {
-                                       destroy_secure_compile_device(device, i);
-                               }
-
-                               return VK_ERROR_INITIALIZATION_FAILED;
-                       }
-               }
-       }
-       return VK_SUCCESS;
-}
-
 static void
 radv_device_init_dispatch(struct radv_device *device)
 {
@@ -3236,11 +2682,6 @@ VkResult radv_CreateDevice(
                        goto fail;
        }
 
-       /* Temporarily disable secure compile while we create meta shaders, etc */
-       uint8_t sc_threads = device->instance->num_sc_threads;
-       if (sc_threads)
-               device->instance->num_sc_threads = 0;
-
        device->keep_shader_info = keep_shader_info;
        result = radv_device_init_meta(device);
        if (result != VK_SUCCESS)
@@ -3299,15 +2740,6 @@ VkResult radv_CreateDevice(
                        1 << util_logbase2(device->force_aniso));
        }
 
-       /* Fork device for secure compile as required */
-       device->instance->num_sc_threads = sc_threads;
-       if (radv_device_use_secure_compile(device->instance)) {
-
-               result = fork_secure_compile_idle_device(device);
-               if (result != VK_SUCCESS)
-                       goto fail_meta;
-       }
-
        *pDevice = radv_device_to_handle(device);
        return VK_SUCCESS;
 
@@ -3376,17 +2808,6 @@ void radv_DestroyDevice(
 
        radv_thread_trace_finish(device);
 
-       if (radv_device_use_secure_compile(device->instance)) {
-               for (unsigned i = 0; i < device->instance->num_sc_threads; i++ ) {
-                       destroy_secure_compile_device(device, i);
-               }
-       }
-
-       if (device->sc_state) {
-               free(device->sc_state->uid);
-               vk_free(&device->vk.alloc, device->sc_state->secure_compile_processes);
-       }
-       vk_free(&device->vk.alloc, device->sc_state);
        vk_free(&device->vk.alloc, device);
 }
 
@@ -4172,6 +3593,10 @@ radv_get_preamble_cs(struct radv_queue *queue,
                if (queue->device->trace_bo)
                        radv_cs_add_buffer(queue->device->ws, cs, queue->device->trace_bo);
 
+               if (queue->device->border_color_data.bo)
+                       radv_cs_add_buffer(queue->device->ws, cs,
+                                          queue->device->border_color_data.bo);
+
                if (i == 0) {
                        si_cs_emit_cache_flush(cs,
                                               queue->device->physical_device->rad_info.chip_class,
@@ -4197,7 +3622,7 @@ radv_get_preamble_cs(struct radv_queue *queue,
                                               RADV_CMD_FLAG_START_PIPELINE_STATS, 0);
                }
 
-               if (!queue->device->ws->cs_finalize(cs))
+               if (queue->device->ws->cs_finalize(cs) != VK_SUCCESS)
                        goto fail;
        }
 
@@ -5045,9 +4470,11 @@ VkResult radv_QueueWaitIdle(
        }
        pthread_mutex_unlock(&queue->pending_mutex);
 
-       queue->device->ws->ctx_wait_idle(queue->hw_ctx,
-                                        radv_queue_family_to_ring(queue->queue_family_index),
-                                        queue->queue_idx);
+       if (!queue->device->ws->ctx_wait_idle(queue->hw_ctx,
+                                             radv_queue_family_to_ring(queue->queue_family_index),
+                                             queue->queue_idx))
+               return VK_ERROR_DEVICE_LOST;
+
        return VK_SUCCESS;
 }
 
@@ -5058,7 +4485,11 @@ VkResult radv_DeviceWaitIdle(
 
        for (unsigned i = 0; i < RADV_MAX_QUEUE_FAMILIES; i++) {
                for (unsigned q = 0; q < device->queue_count[i]; q++) {
-                       radv_QueueWaitIdle(radv_queue_to_handle(&device->queues[i][q]));
+                       VkResult result =
+                               radv_QueueWaitIdle(radv_queue_to_handle(&device->queues[i][q]));
+
+                       if (result != VK_SUCCESS)
+                               return result;
                }
        }
        return VK_SUCCESS;
@@ -5220,9 +4651,10 @@ bool radv_get_memory_fd(struct radv_device *device,
 }
 
 
-static void radv_free_memory(struct radv_device *device,
-                            const VkAllocationCallbacks* pAllocator,
-                            struct radv_device_memory *mem)
+void
+radv_free_memory(struct radv_device *device,
+                const VkAllocationCallbacks* pAllocator,
+                struct radv_device_memory *mem)
 {
        if (mem == NULL)
                return;
@@ -5730,6 +5162,24 @@ static bool radv_sparse_bind_has_effects(const VkBindSparseInfo *info)
        return VK_SUCCESS;
 }
 
+static void
+radv_destroy_fence(struct radv_device *device,
+                  const VkAllocationCallbacks *pAllocator,
+                  struct radv_fence *fence)
+{
+       if (fence->temp_syncobj)
+               device->ws->destroy_syncobj(device->ws, fence->temp_syncobj);
+       if (fence->syncobj)
+               device->ws->destroy_syncobj(device->ws, fence->syncobj);
+       if (fence->fence)
+               device->ws->destroy_fence(fence->fence);
+       if (fence->fence_wsi)
+               fence->fence_wsi->destroy(fence->fence_wsi);
+
+       vk_object_base_finish(&fence->base);
+       vk_free2(&device->vk.alloc, pAllocator, fence);
+}
+
 VkResult radv_CreateFence(
        VkDevice                                    _device,
        const VkFenceCreateInfo*                    pCreateInfo,
@@ -5756,7 +5206,7 @@ VkResult radv_CreateFence(
        if (device->always_use_syncobj || handleTypes) {
                int ret = device->ws->create_syncobj(device->ws, &fence->syncobj);
                if (ret) {
-                       vk_free2(&device->vk.alloc, pAllocator, fence);
+                       radv_destroy_fence(device, pAllocator, fence);
                        return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
                }
                if (pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT) {
@@ -5766,7 +5216,7 @@ VkResult radv_CreateFence(
        } else {
                fence->fence = device->ws->create_fence();
                if (!fence->fence) {
-                       vk_free2(&device->vk.alloc, pAllocator, fence);
+                       radv_destroy_fence(device, pAllocator, fence);
                        return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
                }
                fence->syncobj = 0;
@@ -5790,17 +5240,7 @@ void radv_DestroyFence(
        if (!fence)
                return;
 
-       if (fence->temp_syncobj)
-               device->ws->destroy_syncobj(device->ws, fence->temp_syncobj);
-       if (fence->syncobj)
-               device->ws->destroy_syncobj(device->ws, fence->syncobj);
-       if (fence->fence)
-               device->ws->destroy_fence(fence->fence);
-       if (fence->fence_wsi)
-               fence->fence_wsi->destroy(fence->fence_wsi);
-
-       vk_object_base_finish(&fence->base);
-       vk_free2(&device->vk.alloc, pAllocator, fence);
+       radv_destroy_fence(device, pAllocator, fence);
 }
 
 
@@ -6198,6 +5638,17 @@ radv_get_semaphore_type(const void *pNext, uint64_t *initial_value)
        return type_info->semaphoreType;
 }
 
+static void
+radv_destroy_semaphore(struct radv_device *device,
+                      const VkAllocationCallbacks *pAllocator,
+                      struct radv_semaphore *sem)
+{
+       radv_destroy_semaphore_part(device, &sem->temporary);
+       radv_destroy_semaphore_part(device, &sem->permanent);
+       vk_object_base_finish(&sem->base);
+       vk_free2(&device->vk.alloc, pAllocator, sem);
+}
+
 VkResult radv_CreateSemaphore(
        VkDevice                                    _device,
        const VkSemaphoreCreateInfo*                pCreateInfo,
@@ -6231,14 +5682,14 @@ VkResult radv_CreateSemaphore(
                assert (device->physical_device->rad_info.has_syncobj);
                int ret = device->ws->create_syncobj(device->ws, &sem->permanent.syncobj);
                if (ret) {
-                       vk_free2(&device->vk.alloc, pAllocator, sem);
+                       radv_destroy_semaphore(device, pAllocator, sem);
                        return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
                }
                sem->permanent.kind = RADV_SEMAPHORE_SYNCOBJ;
        } else {
                sem->permanent.ws_sem = device->ws->create_sem(device->ws);
                if (!sem->permanent.ws_sem) {
-                       vk_free2(&device->vk.alloc, pAllocator, sem);
+                       radv_destroy_semaphore(device, pAllocator, sem);
                        return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
                }
                sem->permanent.kind = RADV_SEMAPHORE_WINSYS;
@@ -6258,10 +5709,7 @@ void radv_DestroySemaphore(
        if (!_semaphore)
                return;
 
-       radv_destroy_semaphore_part(device, &sem->temporary);
-       radv_destroy_semaphore_part(device, &sem->permanent);
-       vk_object_base_finish(&sem->base);
-       vk_free2(&device->vk.alloc, pAllocator, sem);
+       radv_destroy_semaphore(device, pAllocator, sem);
 }
 
 VkResult
@@ -6370,7 +5818,9 @@ static void radv_destroy_event(struct radv_device *device,
                                const VkAllocationCallbacks* pAllocator,
                                struct radv_event *event)
 {
-       device->ws->buffer_destroy(event->bo);
+       if (event->bo)
+               device->ws->buffer_destroy(event->bo);
+
        vk_object_base_finish(&event->base);
        vk_free2(&device->vk.alloc, pAllocator, event);
 }
@@ -6396,7 +5846,7 @@ VkResult radv_CreateEvent(
                                              RADEON_FLAG_VA_UNCACHED | RADEON_FLAG_CPU_ACCESS | RADEON_FLAG_NO_INTERPROCESS_SHARING,
                                              RADV_BO_PRIORITY_FENCE);
        if (!event->bo) {
-               vk_free2(&device->vk.alloc, pAllocator, event);
+               radv_destroy_event(device, pAllocator, event);
                return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
        }
 
@@ -6456,6 +5906,18 @@ VkResult radv_ResetEvent(
        return VK_SUCCESS;
 }
 
+static void
+radv_destroy_buffer(struct radv_device *device,
+                   const VkAllocationCallbacks *pAllocator,
+                   struct radv_buffer *buffer)
+{
+       if ((buffer->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) && buffer->bo)
+               device->ws->buffer_destroy(buffer->bo);
+
+       vk_object_base_finish(&buffer->base);
+       vk_free2(&device->vk.alloc, pAllocator, buffer);
+}
+
 VkResult radv_CreateBuffer(
        VkDevice                                    _device,
        const VkBufferCreateInfo*                   pCreateInfo,
@@ -6492,7 +5954,7 @@ VkResult radv_CreateBuffer(
                                                       4096, 0, RADEON_FLAG_VIRTUAL,
                                                       RADV_BO_PRIORITY_VIRTUAL);
                if (!buffer->bo) {
-                       vk_free2(&device->vk.alloc, pAllocator, buffer);
+                       radv_destroy_buffer(device, pAllocator, buffer);
                        return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
                }
        }
@@ -6513,11 +5975,7 @@ void radv_DestroyBuffer(
        if (!buffer)
                return;
 
-       if (buffer->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT)
-               device->ws->buffer_destroy(buffer->bo);
-
-       vk_object_base_finish(&buffer->base);
-       vk_free2(&device->vk.alloc, pAllocator, buffer);
+       radv_destroy_buffer(device, pAllocator, buffer);
 }
 
 VkDeviceAddress radv_GetBufferDeviceAddress(
@@ -7606,7 +7064,7 @@ static VkResult radv_import_sync_fd(struct radv_device *device,
        if (!syncobj_handle) {
                int ret = device->ws->create_syncobj(device->ws, &syncobj_handle);
                if (ret) {
-                       return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
+                       return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
                }
        }
 
@@ -7680,23 +7138,24 @@ VkResult radv_GetSemaphoreFdKHR(VkDevice _device,
        switch(pGetFdInfo->handleType) {
        case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT:
                ret = device->ws->export_syncobj(device->ws, syncobj_handle, pFd);
+               if (ret)
+                       return vk_error(device->instance, VK_ERROR_TOO_MANY_OBJECTS);
                break;
        case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT:
                ret = device->ws->export_syncobj_to_sync_file(device->ws, syncobj_handle, pFd);
-               if (!ret) {
-                       if (sem->temporary.kind != RADV_SEMAPHORE_NONE) {
-                               radv_destroy_semaphore_part(device, &sem->temporary);
-                       } else {
-                               device->ws->reset_syncobj(device->ws, syncobj_handle);
-                       }
+               if (ret)
+                       return vk_error(device->instance, VK_ERROR_TOO_MANY_OBJECTS);
+
+               if (sem->temporary.kind != RADV_SEMAPHORE_NONE) {
+                       radv_destroy_semaphore_part(device, &sem->temporary);
+               } else {
+                       device->ws->reset_syncobj(device->ws, syncobj_handle);
                }
                break;
        default:
                unreachable("Unhandled semaphore handle type");
        }
 
-       if (ret)
-               return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
        return VK_SUCCESS;
 }
 
@@ -7774,24 +7233,25 @@ VkResult radv_GetFenceFdKHR(VkDevice _device,
        switch(pGetFdInfo->handleType) {
        case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT:
                ret = device->ws->export_syncobj(device->ws, syncobj_handle, pFd);
+               if (ret)
+                       return vk_error(device->instance, VK_ERROR_TOO_MANY_OBJECTS);
                break;
        case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT:
                ret = device->ws->export_syncobj_to_sync_file(device->ws, syncobj_handle, pFd);
-               if (!ret) {
-                       if (fence->temp_syncobj) {
-                               close (fence->temp_syncobj);
-                               fence->temp_syncobj = 0;
-                       } else {
-                               device->ws->reset_syncobj(device->ws, syncobj_handle);
-                       }
+               if (ret)
+                       return vk_error(device->instance, VK_ERROR_TOO_MANY_OBJECTS);
+
+               if (fence->temp_syncobj) {
+                       device->ws->destroy_syncobj(device->ws, fence->temp_syncobj);
+                       fence->temp_syncobj = 0;
+               } else {
+                       device->ws->reset_syncobj(device->ws, syncobj_handle);
                }
                break;
        default:
                unreachable("Unhandled fence handle type");
        }
 
-       if (ret)
-               return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
        return VK_SUCCESS;
 }