Skip to content

Commit 1dcaea8

Browse files
llyyrkasper93
authored andcommitted
vulkan/malloc: fix data race on vk_malloc.age
TSAN reports a data race between vk_malloc_garbage_collect and vk_malloc_slice, the former increments vk_malloc.age while holding the vk_malloc.lock while the latter reads it after dropping the lock. Fix this by setting slab->age inside pool_get_page when returning already allocated slab. slab_alloc already sets it for new slabs. TSAN trace: WARNING: ThreadSanitizer: data race (pid=847921) Write of size 8 at 0x725400013878 by thread T15 (mutexes: write M0, write M1): \#0 vk_malloc_garbage_collect /mnt/f/workspace/libplacebo/build/../src/vulkan/malloc.c:608:12 \#1 vk_gpu_flush /mnt/f/workspace/libplacebo/build/../src/vulkan/gpu.c:756:5 \#2 pl_gpu_flush /mnt/f/workspace/libplacebo/build/../src/gpu.c:1267:9 Previous read of size 8 at 0x725400013878 by main thread (mutexes: write M2, write M3): \#0 vk_malloc_slice /mnt/f/workspace/libplacebo/build/../src/vulkan/malloc.c:1063:25 \#1 vk_buf_create /mnt/f/workspace/libplacebo/build/../src/vulkan/gpu_buf.c:221:10 \#2 pl_buf_create /mnt/f/workspace/libplacebo/build/../src/gpu.c:609:18
1 parent eb719e4 commit 1dcaea8

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/vulkan/malloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ static struct vk_slab *pool_get_page(struct vk_malloc *ma, struct vk_pool *pool,
742742

743743
slab->spacemap ^= 0x1LLU << page_idx;
744744
*offset = page_idx * slab->pagesize;
745+
slab->age = ma->age;
745746
return slab;
746747
}
747748

@@ -1060,7 +1061,6 @@ bool vk_malloc_slice(struct vk_malloc *ma, struct vk_memslice *out,
10601061
// consumers are always aligned properly.
10611062
size = PL_ALIGN(size, align);
10621063
slab->used += size;
1063-
slab->age = ma->age;
10641064
if (params->debug_tag)
10651065
slab->debug_tag = params->debug_tag;
10661066
pl_mutex_unlock(&slab->lock);

0 commit comments

Comments
 (0)