[llvm] [Offload] Fix self-deadlock on RPC initialization (PR #219012)

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 26 11:46:02 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-offload

Author: Joseph Huber (jhuber6)

<details>
<summary>Changes</summary>

Summary:
Previous change fixed memory leaks, but I neglected the fact that
internally we call a function `initRPCDoorbell` from the AMD plugin that
itself takes the mutex under the mutex, causing a quite obvious
self-deadlock. Do not hold the mutex any longer than necessary.


---
Full diff: https://github.com/llvm/llvm-project/pull/219012.diff


1 Files Affected:

- (modified) offload/plugins-nextgen/common/src/RPC.cpp (+5-2) 


``````````diff
diff --git a/offload/plugins-nextgen/common/src/RPC.cpp b/offload/plugins-nextgen/common/src/RPC.cpp
index 93076d221eda8..c0f6ef0cd361a 100644
--- a/offload/plugins-nextgen/common/src/RPC.cpp
+++ b/offload/plugins-nextgen/common/src/RPC.cpp
@@ -207,10 +207,12 @@ RPCServerTy::isDeviceUsingRPC(plugin::GenericDeviceTy &Device,
 Error RPCServerTy::initDevice(plugin::GenericDeviceTy &Device,
                               plugin::GenericGlobalHandlerTy &Handler,
                               plugin::DeviceImageTy &Image) {
-  std::lock_guard<decltype(BufferMutex)> Lock(BufferMutex);
   uint64_t NumPorts =
       std::min(Device.requestedRPCPortCount(), rpc::MAX_PORT_COUNT);
-  void *RPCBuffer = Buffers[Device.getDeviceId()];
+  {
+    std::lock_guard<decltype(BufferMutex)> Lock(BufferMutex);
+    void *RPCBuffer = Buffers[Device.getDeviceId()];
+  }
   if (!RPCBuffer) {
     auto RPCBufferOrErr = Device.allocate(
         rpc::Server::allocation_size(Device.getRPCNumLanes(), NumPorts),
@@ -236,6 +238,7 @@ Error RPCServerTy::initDevice(plugin::GenericDeviceTy &Device,
         static_cast<uint8_t *>(RPCBuffer) + rpc::Server::doorbell_offset());
     std::memcpy(DoorbellPtr, &Doorbell, sizeof(rpc::Doorbell));
 
+    std::lock_guard<decltype(BufferMutex)> Lock(BufferMutex);
     Buffers[Device.getDeviceId()] = RPCBuffer;
     Devices[Device.getDeviceId()] = &Device;
   }

``````````

</details>


https://github.com/llvm/llvm-project/pull/219012


More information about the llvm-commits mailing list