[libc-commits] [PATCH] D149790: [libc][rpc] Fix the memory ordering in lock.

Jon Chesterfield via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed May 3 14:57:38 PDT 2023


JonChesterfield created this revision.
JonChesterfield added reviewers: jhuber6, jdoerfert, tianshilei1992, michaelrj, sivachandra, tra, gchatelet.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
JonChesterfield requested review of this revision.

Prevent operation reordering with fence instead of a comment.

The mailboxes are in shared memory and the locks structure in device memory.
If the mailboxes are read and then the lock taken, the lock says nothing
about the current or future state of those mail boxes. The relaxed atomic
fetch_or can be reordered before the relaxed atomic loads of unrelated
variables unless there is a fence preventing this.

Patches both Client::try_open and Server::try_open, one of which is missing
an optimisation and the other is missing the comment, but which otherwise
could be Process::try_open followed by buffer->opcode = opcode in Client.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149790

Files:
  libc/src/__support/RPC/rpc.h


Index: libc/src/__support/RPC/rpc.h
===================================================================
--- libc/src/__support/RPC/rpc.h
+++ libc/src/__support/RPC/rpc.h
@@ -248,6 +248,9 @@
   if (lock->fetch_or(1, cpp::MemoryOrder::RELAXED))
     return cpp::nullopt;
 
+  // The mailbox state must be read with the lock held.
+  atomic_thread_fence(cpp::MemoryOrder::ACQUIRE);
+
   uint32_t in = inbox->load(cpp::MemoryOrder::RELAXED);
   uint32_t out = outbox->load(cpp::MemoryOrder::RELAXED);
 
@@ -285,6 +288,9 @@
   if (lock->fetch_or(1, cpp::MemoryOrder::RELAXED))
     return cpp::nullopt;
 
+  // The mailbox state must be read with the lock held.
+  atomic_thread_fence(cpp::MemoryOrder::ACQUIRE);
+
   in = inbox->load(cpp::MemoryOrder::RELAXED);
   out = outbox->load(cpp::MemoryOrder::RELAXED);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149790.519269.patch
Type: text/x-patch
Size: 810 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230503/074aadf0/attachment.bin>


More information about the libc-commits mailing list