Skip to content

Commit

Permalink
Give DescriptorSetAllocator a mutex
Browse files Browse the repository at this point in the history
There are memory corruption crashes inside DescriptorSetAllocator with
Android Graphite/Dawn/Vulkan. The class is accessed with MutexProtected
from BindGroupLayoutVk but not from DeviceVk so concurrent access to
member variables on different threads seems possible. Since the class is
reference counted move the mutex inside the class so that calling a
method on any reference is thread safe.

Bug: 368362677
Change-Id: I7ca400555261c75995431d4a4023a571747961ee
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/212774
Reviewed-by: Corentin Wallez <[email protected]>
Reviewed-by: Geoff Lang <[email protected]>
Commit-Queue: Kyle Charbonneau <[email protected]>
  • Loading branch information
Kyle Charbonneau authored and Dawn LUCI CQ committed Oct 31, 2024
1 parent efed88f commit e249898
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/dawn/native/vulkan/BindGroupLayoutVk.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class BindGroupLayout final : public BindGroupLayoutInternalBase {
VkDescriptorSetLayout mHandle = VK_NULL_HANDLE;

MutexProtected<SlabAllocator<BindGroup>> mBindGroupAllocator;
MutexProtected<Ref<DescriptorSetAllocator>> mDescriptorSetAllocator;
Ref<DescriptorSetAllocator> mDescriptorSetAllocator;
};

} // namespace dawn::native::vulkan
Expand Down
6 changes: 6 additions & 0 deletions src/dawn/native/vulkan/DescriptorSetAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ DescriptorSetAllocator::~DescriptorSetAllocator() {
}

ResultOrError<DescriptorSetAllocation> DescriptorSetAllocator::Allocate(BindGroupLayout* layout) {
Mutex::AutoLock lock(&mMutex);

if (mAvailableDescriptorPoolIndices.empty()) {
DAWN_TRY(AllocateDescriptorPool(layout));
}
Expand All @@ -116,6 +118,8 @@ ResultOrError<DescriptorSetAllocation> DescriptorSetAllocator::Allocate(BindGrou
}

void DescriptorSetAllocator::Deallocate(DescriptorSetAllocation* allocationInfo) {
Mutex::AutoLock lock(&mMutex);

DAWN_ASSERT(allocationInfo != nullptr);
DAWN_ASSERT(allocationInfo->set != VK_NULL_HANDLE);

Expand All @@ -136,6 +140,8 @@ void DescriptorSetAllocator::Deallocate(DescriptorSetAllocation* allocationInfo)
}

void DescriptorSetAllocator::FinishDeallocation(ExecutionSerial completedSerial) {
Mutex::AutoLock lock(&mMutex);

for (const Deallocation& dealloc : mPendingDeallocations.IterateUpTo(completedSerial)) {
DAWN_ASSERT(dealloc.poolIndex < mDescriptorPools.size());

Expand Down
4 changes: 4 additions & 0 deletions src/dawn/native/vulkan/DescriptorSetAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "dawn/common/Mutex.h"
#include "dawn/common/SerialQueue.h"
#include "dawn/common/vulkan_platform.h"
#include "dawn/native/Error.h"
Expand Down Expand Up @@ -81,6 +82,9 @@ class DescriptorSetAllocator : public ObjectBase {
};
SerialQueue<ExecutionSerial, Deallocation> mPendingDeallocations;
ExecutionSerial mLastDeallocationSerial = ExecutionSerial(0);

// Used to guard all public member functions.
Mutex mMutex;
};

} // namespace dawn::native::vulkan
Expand Down

0 comments on commit e249898

Please sign in to comment.