Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1 | #include "aos/ipc_lib/lockless_queue.h" |
| 2 | |
| 3 | #include <linux/futex.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <syscall.h> |
| 6 | #include <unistd.h> |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 7 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 8 | #include <algorithm> |
| 9 | #include <iomanip> |
| 10 | #include <iostream> |
| 11 | #include <sstream> |
| 12 | |
Austin Schuh | be41674 | 2020-10-03 17:24:26 -0700 | [diff] [blame] | 13 | #include "absl/strings/escaping.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 14 | #include "gflags/gflags.h" |
| 15 | #include "glog/logging.h" |
| 16 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 17 | #include "aos/ipc_lib/lockless_queue_memory.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 18 | #include "aos/realtime.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 19 | #include "aos/util/compiler_memory_barrier.h" |
| 20 | |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame] | 21 | DEFINE_bool(dump_lockless_queue_data, false, |
| 22 | "If true, print the data out when dumping the queue."); |
| 23 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 24 | namespace aos { |
| 25 | namespace ipc_lib { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 26 | namespace { |
| 27 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 28 | class GrabQueueSetupLockOrDie { |
| 29 | public: |
| 30 | GrabQueueSetupLockOrDie(LocklessQueueMemory *memory) : memory_(memory) { |
| 31 | const int result = mutex_grab(&(memory->queue_setup_lock)); |
| 32 | CHECK(result == 0 || result == 1) << ": " << result; |
| 33 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 34 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 35 | ~GrabQueueSetupLockOrDie() { mutex_unlock(&(memory_->queue_setup_lock)); } |
| 36 | |
| 37 | GrabQueueSetupLockOrDie(const GrabQueueSetupLockOrDie &) = delete; |
| 38 | GrabQueueSetupLockOrDie &operator=(const GrabQueueSetupLockOrDie &) = delete; |
| 39 | |
| 40 | private: |
| 41 | LocklessQueueMemory *const memory_; |
| 42 | }; |
| 43 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 44 | bool IsPinned(LocklessQueueMemory *memory, Index index) { |
| 45 | DCHECK(index.valid()); |
| 46 | const size_t queue_size = memory->queue_size(); |
| 47 | const QueueIndex message_index = |
| 48 | memory->GetMessage(index)->header.queue_index.Load(queue_size); |
| 49 | if (!message_index.valid()) { |
| 50 | return false; |
| 51 | } |
| 52 | DCHECK(memory->GetQueue(message_index.Wrapped())->Load() != index) |
| 53 | << ": Message is in the queue"; |
| 54 | for (int pinner_index = 0; |
| 55 | pinner_index < static_cast<int>(memory->config.num_pinners); |
| 56 | ++pinner_index) { |
| 57 | ipc_lib::Pinner *const pinner = memory->GetPinner(pinner_index); |
| 58 | |
| 59 | if (pinner->pinned.RelaxedLoad(queue_size) == message_index) { |
| 60 | return true; |
| 61 | } |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | // Ensures sender->scratch_index (which must contain to_replace) is not pinned. |
| 67 | // |
| 68 | // Returns the new scratch_index value. |
| 69 | Index SwapPinnedSenderScratch(LocklessQueueMemory *const memory, |
| 70 | ipc_lib::Sender *const sender, |
| 71 | const Index to_replace) { |
| 72 | // If anybody's trying to pin this message, then grab a message from a pinner |
| 73 | // to write into instead, and leave the message we pulled out of the queue |
| 74 | // (currently in our scratch_index) with a pinner. |
| 75 | // |
| 76 | // This loop will terminate in at most one iteration through the pinners in |
| 77 | // any steady-state configuration of the memory. There are only as many |
| 78 | // Pinner::pinned values to worry about as there are Pinner::scratch_index |
| 79 | // values to check against, plus to_replace, which means there will always be |
| 80 | // a free one. We might have to make multiple passes if things are being |
| 81 | // changed concurrently though, but nobody dying can make this loop fail to |
| 82 | // terminate (because the number of processes that can die is bounded, because |
| 83 | // no new ones can start while we've got the lock). |
| 84 | for (int pinner_index = 0; true; |
| 85 | pinner_index = (pinner_index + 1) % memory->config.num_pinners) { |
| 86 | if (!IsPinned(memory, to_replace)) { |
| 87 | // No pinners on our current scratch_index, so we're fine now. |
| 88 | VLOG(3) << "No pinners: " << to_replace.DebugString(); |
| 89 | return to_replace; |
| 90 | } |
| 91 | |
| 92 | ipc_lib::Pinner *const pinner = memory->GetPinner(pinner_index); |
| 93 | |
| 94 | const Index pinner_scratch = pinner->scratch_index.RelaxedLoad(); |
| 95 | CHECK(pinner_scratch.valid()) |
| 96 | << ": Pinner scratch_index should always be valid"; |
| 97 | if (IsPinned(memory, pinner_scratch)) { |
| 98 | // Wouldn't do us any good to swap with this one, so don't bother, and |
| 99 | // move onto the next one. |
| 100 | VLOG(3) << "Also pinned: " << pinner_scratch.DebugString(); |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | sender->to_replace.RelaxedStore(pinner_scratch); |
| 105 | aos_compiler_memory_barrier(); |
| 106 | // Give the pinner the message (which is currently in |
| 107 | // sender->scratch_index). |
| 108 | if (!pinner->scratch_index.CompareAndExchangeStrong(pinner_scratch, |
| 109 | to_replace)) { |
| 110 | // Somebody swapped into this pinner before us. The new value is probably |
| 111 | // pinned, so we don't want to look at it again immediately. |
| 112 | VLOG(3) << "Pinner " << pinner_index |
| 113 | << " scratch_index changed: " << pinner_scratch.DebugString() |
| 114 | << ", " << to_replace.DebugString(); |
| 115 | sender->to_replace.RelaxedInvalidate(); |
| 116 | continue; |
| 117 | } |
| 118 | aos_compiler_memory_barrier(); |
| 119 | // Now update the sender's scratch space and record that we succeeded. |
| 120 | sender->scratch_index.Store(pinner_scratch); |
| 121 | aos_compiler_memory_barrier(); |
| 122 | // And then record that we succeeded, but definitely after the above |
| 123 | // store. |
| 124 | sender->to_replace.RelaxedInvalidate(); |
| 125 | VLOG(3) << "Got new scratch message: " << pinner_scratch.DebugString(); |
| 126 | |
| 127 | // If it's in a pinner's scratch_index, it should not be in the queue, which |
| 128 | // means nobody new can pin it for real. However, they can still attempt to |
| 129 | // pin it, which means we can't verify !IsPinned down here. |
| 130 | |
| 131 | return pinner_scratch; |
| 132 | } |
| 133 | } |
| 134 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 135 | // Returns true if it succeeded. Returns false if another sender died in the |
| 136 | // middle. |
| 137 | bool DoCleanup(LocklessQueueMemory *memory, const GrabQueueSetupLockOrDie &) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 138 | // Make sure we start looking at shared memory fresh right now. We'll handle |
| 139 | // people dying partway through by either cleaning up after them or not, but |
| 140 | // we want to ensure we clean up after anybody who has already died when we |
| 141 | // start. |
| 142 | aos_compiler_memory_barrier(); |
| 143 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 144 | const size_t num_senders = memory->num_senders(); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 145 | const size_t num_pinners = memory->num_pinners(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 146 | const size_t queue_size = memory->queue_size(); |
| 147 | const size_t num_messages = memory->num_messages(); |
| 148 | |
| 149 | // There are a large number of crazy cases here for how things can go wrong |
| 150 | // and how we have to recover. They either require us to keep extra track of |
| 151 | // what is going on, slowing down the send path, or require a large number of |
| 152 | // cases. |
| 153 | // |
| 154 | // The solution here is to not over-think it. This is running while not real |
| 155 | // time during construction. It is allowed to be slow. It will also very |
| 156 | // rarely trigger. There is a small uS window where process death is |
| 157 | // ambiguous. |
| 158 | // |
| 159 | // So, build up a list N long, where N is the number of messages. Search |
| 160 | // through the entire queue and the sender list (ignoring any dead senders), |
| 161 | // and mark down which ones we have seen. Once we have seen all the messages |
| 162 | // except the N dead senders, we know which messages are dead. Because the |
| 163 | // queue is active while we do this, it may take a couple of go arounds to see |
| 164 | // everything. |
| 165 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 166 | ::std::vector<bool> need_recovery(num_senders, false); |
| 167 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 168 | // Do the easy case. Find all senders who have died. See if they are either |
| 169 | // consistent already, or if they have copied over to_replace to the scratch |
| 170 | // index, but haven't cleared to_replace. Count them. |
| 171 | size_t valid_senders = 0; |
| 172 | for (size_t i = 0; i < num_senders; ++i) { |
| 173 | Sender *sender = memory->GetSender(i); |
| 174 | const uint32_t tid = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 175 | __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE); |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 176 | if (!(tid & FUTEX_OWNER_DIED)) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 177 | // Not dead. |
| 178 | ++valid_senders; |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 179 | continue; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 180 | } |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 181 | VLOG(3) << "Found an easy death for sender " << i; |
| 182 | // We can do a relaxed load here because we're the only person touching |
| 183 | // this sender at this point. |
| 184 | const Index to_replace = sender->to_replace.RelaxedLoad(); |
| 185 | const Index scratch_index = sender->scratch_index.Load(); |
| 186 | |
| 187 | // I find it easiest to think about this in terms of the set of observable |
| 188 | // states. The main code progresses through the following states: |
| 189 | |
| 190 | // 1) scratch_index = xxx |
| 191 | // to_replace = invalid |
| 192 | // This is unambiguous. Already good. |
| 193 | |
| 194 | // 2) scratch_index = xxx |
| 195 | // to_replace = yyy |
| 196 | // Very ambiguous. Is xxx or yyy the correct one? Need to either roll |
| 197 | // this forwards or backwards. |
| 198 | |
| 199 | // 3) scratch_index = yyy |
| 200 | // to_replace = yyy |
| 201 | // We are in the act of moving to_replace to scratch_index, but didn't |
| 202 | // finish. Easy. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 203 | // |
| 204 | // If doing a pinner swap, we've definitely done it. |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 205 | |
| 206 | // 4) scratch_index = yyy |
| 207 | // to_replace = invalid |
| 208 | // Finished, but died. Looks like 1) |
| 209 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 210 | // Swapping with a pinner's scratch_index passes through the same states. |
| 211 | // We just need to ensure the message that ends up in the senders's |
| 212 | // scratch_index isn't pinned, using the same code as sending does. |
| 213 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 214 | // Any cleanup code needs to follow the same set of states to be robust to |
| 215 | // death, so death can be restarted. |
| 216 | |
| 217 | if (!to_replace.valid()) { |
| 218 | // 1) or 4). Make sure we aren't corrupted and declare victory. |
| 219 | CHECK(scratch_index.valid()); |
| 220 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 221 | // If it's in 1) with a pinner, the sender might have a pinned message, |
| 222 | // so fix that. |
| 223 | SwapPinnedSenderScratch(memory, sender, scratch_index); |
| 224 | |
| 225 | // If it's in 4), it may not have completed this step yet. This will |
| 226 | // always be a NOP if it's in 1), verified by a DCHECK. |
| 227 | memory->GetMessage(scratch_index)->header.queue_index.RelaxedInvalidate(); |
| 228 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 229 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
| 230 | ++valid_senders; |
| 231 | continue; |
| 232 | } |
| 233 | |
| 234 | // Could be 2) or 3) at this point. |
| 235 | |
| 236 | if (to_replace == scratch_index) { |
| 237 | // 3) for sure. |
| 238 | // Just need to invalidate to_replace to finish. |
| 239 | sender->to_replace.Invalidate(); |
| 240 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 241 | // Make sure to indicate it's an unused message before a sender gets its |
| 242 | // hands on it. |
| 243 | memory->GetMessage(scratch_index)->header.queue_index.RelaxedInvalidate(); |
| 244 | aos_compiler_memory_barrier(); |
| 245 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 246 | // And mark that we succeeded. |
| 247 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
| 248 | ++valid_senders; |
| 249 | continue; |
| 250 | } |
| 251 | |
| 252 | // Must be 2). Mark it for later. |
| 253 | need_recovery[i] = true; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 256 | // Cleaning up pinners is easy. We don't actually have to do anything, but |
| 257 | // invalidating its pinned field might help catch bugs elsewhere trying to |
| 258 | // read it before it's set. |
| 259 | for (size_t i = 0; i < num_pinners; ++i) { |
| 260 | Pinner *const pinner = memory->GetPinner(i); |
| 261 | const uint32_t tid = |
| 262 | __atomic_load_n(&(pinner->tid.futex), __ATOMIC_ACQUIRE); |
| 263 | if (!(tid & FUTEX_OWNER_DIED)) { |
| 264 | continue; |
| 265 | } |
| 266 | pinner->pinned.Invalidate(); |
| 267 | __atomic_store_n(&(pinner->tid.futex), 0, __ATOMIC_RELEASE); |
| 268 | } |
| 269 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 270 | // If all the senders are (or were made) good, there is no need to do the hard |
| 271 | // case. |
| 272 | if (valid_senders == num_senders) { |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 273 | return true; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 276 | VLOG(3) << "Starting hard cleanup"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 277 | |
| 278 | size_t num_accounted_for = 0; |
| 279 | size_t num_missing = 0; |
| 280 | ::std::vector<bool> accounted_for(num_messages, false); |
| 281 | |
| 282 | while ((num_accounted_for + num_missing) != num_messages) { |
| 283 | num_missing = 0; |
| 284 | for (size_t i = 0; i < num_senders; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 285 | Sender *const sender = memory->GetSender(i); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 286 | const uint32_t tid = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 287 | __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 288 | if (tid & FUTEX_OWNER_DIED) { |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 289 | if (!need_recovery[i]) { |
| 290 | return false; |
| 291 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 292 | ++num_missing; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 293 | continue; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 294 | } |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 295 | CHECK(!need_recovery[i]) << ": Somebody else recovered a sender: " << i; |
| 296 | // We can do a relaxed load here because we're the only person touching |
| 297 | // this sender at this point, if it matters. If it's not a dead sender, |
| 298 | // then any message it ever has will eventually be accounted for if we |
| 299 | // make enough tries through the outer loop. |
| 300 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 301 | if (!accounted_for[scratch_index.message_index()]) { |
| 302 | ++num_accounted_for; |
| 303 | } |
| 304 | accounted_for[scratch_index.message_index()] = true; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | for (size_t i = 0; i < queue_size; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 308 | // Same logic as above for scratch_index applies here too. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 309 | const Index index = memory->GetQueue(i)->RelaxedLoad(); |
| 310 | if (!accounted_for[index.message_index()]) { |
| 311 | ++num_accounted_for; |
| 312 | } |
| 313 | accounted_for[index.message_index()] = true; |
| 314 | } |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 315 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 316 | for (size_t pinner_index = 0; pinner_index < num_pinners; ++pinner_index) { |
| 317 | // Same logic as above for scratch_index applies here too. |
| 318 | const Index index = |
| 319 | memory->GetPinner(pinner_index)->scratch_index.RelaxedLoad(); |
| 320 | if (!accounted_for[index.message_index()]) { |
| 321 | ++num_accounted_for; |
| 322 | } |
| 323 | accounted_for[index.message_index()] = true; |
| 324 | } |
| 325 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 326 | CHECK_LE(num_accounted_for + num_missing, num_messages); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | while (num_missing != 0) { |
| 330 | const size_t starting_num_missing = num_missing; |
| 331 | for (size_t i = 0; i < num_senders; ++i) { |
| 332 | Sender *sender = memory->GetSender(i); |
| 333 | const uint32_t tid = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 334 | __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE); |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 335 | if (!(tid & FUTEX_OWNER_DIED)) { |
| 336 | CHECK(!need_recovery[i]) << ": Somebody else recovered a sender: " << i; |
| 337 | continue; |
| 338 | } |
| 339 | if (!need_recovery[i]) { |
| 340 | return false; |
| 341 | } |
| 342 | // We can do relaxed loads here because we're the only person touching |
| 343 | // this sender at this point. |
| 344 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 345 | const Index to_replace = sender->to_replace.RelaxedLoad(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 346 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 347 | // Candidate. |
| 348 | if (to_replace.valid()) { |
| 349 | CHECK_LE(to_replace.message_index(), accounted_for.size()); |
| 350 | } |
| 351 | if (scratch_index.valid()) { |
| 352 | CHECK_LE(scratch_index.message_index(), accounted_for.size()); |
| 353 | } |
| 354 | if (!to_replace.valid() || accounted_for[to_replace.message_index()]) { |
| 355 | CHECK(scratch_index.valid()); |
| 356 | VLOG(3) << "Sender " << i |
| 357 | << " died, to_replace is already accounted for"; |
| 358 | // If both are accounted for, we are corrupt... |
| 359 | CHECK(!accounted_for[scratch_index.message_index()]); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 360 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 361 | // to_replace is already accounted for. This means that we didn't |
| 362 | // atomically insert scratch_index into the queue yet. So |
| 363 | // invalidate to_replace. |
| 364 | sender->to_replace.Invalidate(); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 365 | // Sender definitely will not have gotten here, so finish for it. |
| 366 | memory->GetMessage(scratch_index) |
| 367 | ->header.queue_index.RelaxedInvalidate(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 368 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 369 | // And then mark this sender clean. |
| 370 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
| 371 | need_recovery[i] = false; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 372 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 373 | // And account for scratch_index. |
| 374 | accounted_for[scratch_index.message_index()] = true; |
| 375 | --num_missing; |
| 376 | ++num_accounted_for; |
| 377 | } else if (!scratch_index.valid() || |
| 378 | accounted_for[scratch_index.message_index()]) { |
| 379 | VLOG(3) << "Sender " << i |
| 380 | << " died, scratch_index is already accounted for"; |
| 381 | // scratch_index is accounted for. That means we did the insert, |
| 382 | // but didn't record it. |
| 383 | CHECK(to_replace.valid()); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 384 | |
| 385 | // Make sure to indicate it's an unused message before a sender gets its |
| 386 | // hands on it. |
| 387 | memory->GetMessage(to_replace)->header.queue_index.RelaxedInvalidate(); |
| 388 | aos_compiler_memory_barrier(); |
| 389 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 390 | // Finish the transaction. Copy to_replace, then clear it. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 391 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 392 | sender->scratch_index.Store(to_replace); |
| 393 | sender->to_replace.Invalidate(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 394 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 395 | // And then mark this sender clean. |
| 396 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
| 397 | need_recovery[i] = false; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 398 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 399 | // And account for to_replace. |
| 400 | accounted_for[to_replace.message_index()] = true; |
| 401 | --num_missing; |
| 402 | ++num_accounted_for; |
| 403 | } else { |
| 404 | VLOG(3) << "Sender " << i << " died, neither is accounted for"; |
| 405 | // Ambiguous. There will be an unambiguous one somewhere that we |
| 406 | // can do first. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | // CHECK that we are making progress. |
| 410 | CHECK_NE(num_missing, starting_num_missing); |
| 411 | } |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 412 | return true; |
| 413 | } |
| 414 | |
| 415 | void Cleanup(LocklessQueueMemory *memory, const GrabQueueSetupLockOrDie &lock) { |
| 416 | // The number of iterations is bounded here because there are only a finite |
| 417 | // number of senders in existence which could die, and no new ones can be |
| 418 | // created while we're in here holding the lock. |
| 419 | while (!DoCleanup(memory, lock)) { |
| 420 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | // Exposes rt_tgsigqueueinfo so we can send the signal *just* to the target |
| 424 | // thread. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 425 | // TODO(Brian): Do directly in assembly for armhf at least for efficiency. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 426 | int rt_tgsigqueueinfo(pid_t tgid, pid_t tid, int sig, siginfo_t *si) { |
| 427 | return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, si); |
| 428 | } |
| 429 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 430 | QueueIndex ZeroOrValid(QueueIndex index) { |
| 431 | if (!index.valid()) { |
| 432 | return index.Clear(); |
| 433 | } |
| 434 | return index; |
| 435 | } |
| 436 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 437 | } // namespace |
| 438 | |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 439 | size_t LocklessQueueConfiguration::message_size() const { |
| 440 | // Round up the message size so following data is aligned appropriately. |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 441 | // Make sure to leave space to align the message data. It will be aligned |
| 442 | // relative to the start of the shared memory region, but that might not be |
| 443 | // aligned for some use cases. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 444 | return LocklessQueueMemory::AlignmentRoundUp(message_data_size + |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 445 | kChannelDataRedzone * 2 + |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 446 | (kChannelDataAlignment - 1)) + |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 447 | sizeof(Message); |
| 448 | } |
| 449 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 450 | size_t LocklessQueueMemorySize(LocklessQueueConfiguration config) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 451 | // Round up the message size so following data is aligned appropriately. |
| 452 | config.message_data_size = |
| 453 | LocklessQueueMemory::AlignmentRoundUp(config.message_data_size); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 454 | |
| 455 | // As we build up the size, confirm that everything is aligned to the |
| 456 | // alignment requirements of the type. |
| 457 | size_t size = sizeof(LocklessQueueMemory); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 458 | CHECK_EQ(size % alignof(LocklessQueueMemory), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 459 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 460 | CHECK_EQ(size % alignof(AtomicIndex), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 461 | size += LocklessQueueMemory::SizeOfQueue(config); |
| 462 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 463 | CHECK_EQ(size % alignof(Message), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 464 | size += LocklessQueueMemory::SizeOfMessages(config); |
| 465 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 466 | CHECK_EQ(size % alignof(Watcher), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 467 | size += LocklessQueueMemory::SizeOfWatchers(config); |
| 468 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 469 | CHECK_EQ(size % alignof(Sender), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 470 | size += LocklessQueueMemory::SizeOfSenders(config); |
| 471 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 472 | CHECK_EQ(size % alignof(Pinner), 0u); |
| 473 | size += LocklessQueueMemory::SizeOfPinners(config); |
| 474 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 475 | return size; |
| 476 | } |
| 477 | |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 478 | // Calculates the starting byte for a redzone in shared memory. This starting |
| 479 | // value is simply incremented for subsequent bytes. |
| 480 | // |
| 481 | // The result is based on the offset of the region in shared memor, to ensure it |
| 482 | // is the same for each region when we generate and verify, but different for |
| 483 | // each region to help catch forms of corruption like copying out-of-bounds data |
| 484 | // from one place to another. |
| 485 | // |
| 486 | // memory is the base pointer to the shared memory. It is used to calculated |
| 487 | // offsets. starting_data is the start of the redzone's data. Each one will |
| 488 | // get a unique pattern. |
| 489 | uint8_t RedzoneStart(const LocklessQueueMemory *memory, |
| 490 | const char *starting_data) { |
| 491 | const auto memory_int = reinterpret_cast<uintptr_t>(memory); |
| 492 | const auto starting_int = reinterpret_cast<uintptr_t>(starting_data); |
| 493 | DCHECK(starting_int >= memory_int); |
| 494 | DCHECK(starting_int < memory_int + LocklessQueueMemorySize(memory->config)); |
| 495 | const uintptr_t starting_offset = starting_int - memory_int; |
| 496 | // Just XOR the lower 2 bytes. They higher-order bytes are probably 0 |
| 497 | // anyways. |
| 498 | return (starting_offset & 0xFF) ^ ((starting_offset >> 8) & 0xFF); |
| 499 | } |
| 500 | |
| 501 | // Returns true if the given redzone has invalid data. |
| 502 | bool CheckRedzone(const LocklessQueueMemory *memory, |
| 503 | absl::Span<const char> redzone) { |
| 504 | uint8_t redzone_value = RedzoneStart(memory, redzone.data()); |
| 505 | |
| 506 | bool bad = false; |
| 507 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 508 | for (size_t i = 0; i < redzone.size() && !bad; ++i) { |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 509 | if (memcmp(&redzone[i], &redzone_value, 1)) { |
| 510 | bad = true; |
| 511 | } |
| 512 | ++redzone_value; |
| 513 | } |
| 514 | |
| 515 | return bad; |
| 516 | } |
| 517 | |
| 518 | // Returns true if either of message's redzones has invalid data. |
| 519 | bool CheckBothRedzones(const LocklessQueueMemory *memory, |
| 520 | const Message *message) { |
| 521 | return CheckRedzone(memory, |
| 522 | message->PreRedzone(memory->message_data_size())) || |
| 523 | CheckRedzone(memory, message->PostRedzone(memory->message_data_size(), |
| 524 | memory->message_size())); |
| 525 | } |
| 526 | |
| 527 | // Fills the given redzone with the expected data. |
| 528 | void FillRedzone(LocklessQueueMemory *memory, absl::Span<char> redzone) { |
| 529 | uint8_t redzone_value = RedzoneStart(memory, redzone.data()); |
| 530 | for (size_t i = 0; i < redzone.size(); ++i) { |
| 531 | memcpy(&redzone[i], &redzone_value, 1); |
| 532 | ++redzone_value; |
| 533 | } |
| 534 | |
| 535 | // Just double check that the implementations match. |
| 536 | CHECK(!CheckRedzone(memory, redzone)); |
| 537 | } |
| 538 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 539 | LocklessQueueMemory *InitializeLocklessQueueMemory( |
| 540 | LocklessQueueMemory *memory, LocklessQueueConfiguration config) { |
| 541 | // Everything should be zero initialized already. So we just need to fill |
| 542 | // everything out properly. |
| 543 | |
Brian Silverman | c57ff0a | 2020-04-28 16:45:13 -0700 | [diff] [blame] | 544 | // This is the UID we will use for checking signal-sending permission |
| 545 | // compatibility. |
| 546 | // |
| 547 | // The manpage says: |
| 548 | // For a process to have permission to send a signal, it must either be |
| 549 | // privileged [...], or the real or effective user ID of the sending process |
| 550 | // must equal the real or saved set-user-ID of the target process. |
| 551 | // |
| 552 | // Processes typically initialize a queue in random order as they start up. |
| 553 | // This means we need an algorithm for verifying all processes have |
| 554 | // permissions to send each other signals which gives the same answer no |
| 555 | // matter what order they attach in. We would also like to avoid maintaining a |
| 556 | // shared list of the UIDs of all processes. |
| 557 | // |
| 558 | // To do this while still giving sufficient flexibility for all current use |
| 559 | // cases, we track a single UID for the queue. All processes with a matching |
| 560 | // euid+suid must have this UID. Any processes with distinct euid/suid must |
| 561 | // instead have a matching ruid. This guarantees signals can be sent between |
| 562 | // all processes attached to the queue. |
| 563 | // |
| 564 | // In particular, this allows a process to change only its euid (to interact |
| 565 | // with a queue) while still maintaining privileges via its ruid. However, it |
| 566 | // can only use privileges in ways that do not require changing the euid back, |
| 567 | // because while the euid is different it will not be able to receive signals. |
| 568 | // We can't actually verify that, but we can sanity check that things are |
| 569 | // valid when the queue is initialized. |
| 570 | |
| 571 | uid_t uid; |
| 572 | { |
| 573 | uid_t ruid, euid, suid; |
| 574 | PCHECK(getresuid(&ruid, &euid, &suid) == 0); |
| 575 | // If these are equal, then use them, even if that's different from the real |
| 576 | // UID. This allows processes to keep a real UID of 0 (to have permissions |
| 577 | // to perform system-level changes) while still being able to communicate |
| 578 | // with processes running unprivileged as a distinct user. |
| 579 | if (euid == suid) { |
| 580 | uid = euid; |
| 581 | VLOG(1) << "Using euid==suid " << uid; |
| 582 | } else { |
| 583 | uid = ruid; |
| 584 | VLOG(1) << "Using ruid " << ruid; |
| 585 | } |
| 586 | } |
| 587 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 588 | // Grab the mutex. We don't care if the previous reader died. We are going |
| 589 | // to check everything anyways. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 590 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 591 | |
| 592 | if (!memory->initialized) { |
| 593 | // TODO(austin): Check these for out of bounds. |
| 594 | memory->config.num_watchers = config.num_watchers; |
| 595 | memory->config.num_senders = config.num_senders; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 596 | memory->config.num_pinners = config.num_pinners; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 597 | memory->config.queue_size = config.queue_size; |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 598 | memory->config.message_data_size = config.message_data_size; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 599 | |
| 600 | const size_t num_messages = memory->num_messages(); |
| 601 | // There need to be at most MaxMessages() messages allocated. |
| 602 | CHECK_LE(num_messages, Index::MaxMessages()); |
| 603 | |
| 604 | for (size_t i = 0; i < num_messages; ++i) { |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 605 | Message *const message = |
| 606 | memory->GetMessage(Index(QueueIndex::Zero(memory->queue_size()), i)); |
| 607 | message->header.queue_index.Invalidate(); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 608 | message->header.monotonic_sent_time = monotonic_clock::min_time; |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 609 | FillRedzone(memory, message->PreRedzone(memory->message_data_size())); |
| 610 | FillRedzone(memory, message->PostRedzone(memory->message_data_size(), |
| 611 | memory->message_size())); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | for (size_t i = 0; i < memory->queue_size(); ++i) { |
| 615 | // Make the initial counter be the furthest away number. That means that |
| 616 | // index 0 should be 0xffff, 1 should be 0, etc. |
| 617 | memory->GetQueue(i)->Store(Index(QueueIndex::Zero(memory->queue_size()) |
| 618 | .IncrementBy(i) |
| 619 | .DecrementBy(memory->queue_size()), |
| 620 | i)); |
| 621 | } |
| 622 | |
| 623 | memory->next_queue_index.Invalidate(); |
Brian Silverman | c57ff0a | 2020-04-28 16:45:13 -0700 | [diff] [blame] | 624 | memory->uid = uid; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 625 | |
| 626 | for (size_t i = 0; i < memory->num_senders(); ++i) { |
| 627 | ::aos::ipc_lib::Sender *s = memory->GetSender(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 628 | // Nobody else can possibly be touching these because we haven't set |
| 629 | // initialized to true yet. |
| 630 | s->scratch_index.RelaxedStore(Index(0xffff, i + memory->queue_size())); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 631 | s->to_replace.RelaxedInvalidate(); |
| 632 | } |
| 633 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 634 | for (size_t i = 0; i < memory->num_pinners(); ++i) { |
| 635 | ::aos::ipc_lib::Pinner *pinner = memory->GetPinner(i); |
| 636 | // Nobody else can possibly be touching these because we haven't set |
| 637 | // initialized to true yet. |
| 638 | pinner->scratch_index.RelaxedStore( |
| 639 | Index(0xffff, i + memory->num_senders() + memory->queue_size())); |
| 640 | pinner->pinned.Invalidate(); |
| 641 | } |
| 642 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 643 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 644 | // Signal everything is done. This needs to be done last, so if we die, we |
| 645 | // redo initialization. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 646 | memory->initialized = true; |
Austin Schuh | 3328d13 | 2020-02-28 13:54:57 -0800 | [diff] [blame] | 647 | } else { |
Brian Silverman | c57ff0a | 2020-04-28 16:45:13 -0700 | [diff] [blame] | 648 | CHECK_EQ(uid, memory->uid) << ": UIDs must match for all processes"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 651 | return memory; |
| 652 | } |
| 653 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 654 | void LocklessQueue::Initialize() { |
| 655 | InitializeLocklessQueueMemory(memory_, config_); |
| 656 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 657 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 658 | LocklessQueueWatcher::~LocklessQueueWatcher() { |
| 659 | if (watcher_index_ == -1) { |
| 660 | return; |
| 661 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 662 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 663 | // Since everything is self consistent, all we need to do is make sure nobody |
| 664 | // else is running. Someone dying will get caught in the generic consistency |
| 665 | // check. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 666 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 667 | |
| 668 | // Make sure we are registered. |
| 669 | CHECK_NE(watcher_index_, -1); |
| 670 | |
| 671 | // Make sure we still own the slot we are supposed to. |
| 672 | CHECK( |
| 673 | death_notification_is_held(&(memory_->GetWatcher(watcher_index_)->tid))); |
| 674 | |
| 675 | // The act of unlocking invalidates the entry. Invalidate it. |
| 676 | death_notification_release(&(memory_->GetWatcher(watcher_index_)->tid)); |
| 677 | // And internally forget the slot. |
| 678 | watcher_index_ = -1; |
| 679 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 680 | // Cleanup is cheap. The next user will do it anyways, so no need for us to do |
| 681 | // anything right now. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 682 | |
| 683 | // And confirm that nothing is owned by us. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 684 | const int num_watchers = memory_->num_watchers(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 685 | for (int i = 0; i < num_watchers; ++i) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 686 | CHECK(!death_notification_is_held(&(memory_->GetWatcher(i)->tid))) |
| 687 | << ": " << i; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 688 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 691 | std::optional<LocklessQueueWatcher> LocklessQueueWatcher::Make( |
| 692 | LocklessQueue queue, int priority) { |
| 693 | queue.Initialize(); |
| 694 | LocklessQueueWatcher result(queue.memory(), priority); |
| 695 | if (result.watcher_index_ != -1) { |
James Kuszmaul | 9776b39 | 2023-01-14 14:08:08 -0800 | [diff] [blame] | 696 | return result; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 697 | } else { |
| 698 | return std::nullopt; |
| 699 | } |
| 700 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 701 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 702 | LocklessQueueWatcher::LocklessQueueWatcher(LocklessQueueMemory *memory, |
| 703 | int priority) |
| 704 | : memory_(memory) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 705 | // TODO(austin): Make sure signal coalescing is turned on. We don't need |
| 706 | // duplicates. That will improve performance under high load. |
| 707 | |
| 708 | // Since everything is self consistent, all we need to do is make sure nobody |
| 709 | // else is running. Someone dying will get caught in the generic consistency |
| 710 | // check. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 711 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 712 | const int num_watchers = memory_->num_watchers(); |
| 713 | |
| 714 | // Now, find the first empty watcher and grab it. |
| 715 | CHECK_EQ(watcher_index_, -1); |
| 716 | for (int i = 0; i < num_watchers; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 717 | // If we see a slot the kernel has marked as dead, everything we do reusing |
| 718 | // it needs to happen-after whatever that process did before dying. |
Brian Silverman | 2484eea | 2019-12-21 16:48:46 -0800 | [diff] [blame] | 719 | auto *const futex = &(memory_->GetWatcher(i)->tid.futex); |
| 720 | const uint32_t tid = __atomic_load_n(futex, __ATOMIC_ACQUIRE); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 721 | if (tid == 0 || (tid & FUTEX_OWNER_DIED)) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 722 | watcher_index_ = i; |
Brian Silverman | 2484eea | 2019-12-21 16:48:46 -0800 | [diff] [blame] | 723 | // Relaxed is OK here because we're the only task going to touch it |
| 724 | // between here and the write in death_notification_init below (other |
| 725 | // recovery is blocked by us holding the setup lock). |
| 726 | __atomic_store_n(futex, 0, __ATOMIC_RELAXED); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 727 | break; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | // Bail if we failed to find an open slot. |
| 732 | if (watcher_index_ == -1) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 733 | return; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 736 | Watcher *const w = memory_->GetWatcher(watcher_index_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 737 | |
| 738 | w->pid = getpid(); |
| 739 | w->priority = priority; |
| 740 | |
| 741 | // Grabbing a mutex is a compiler and memory barrier, so nothing before will |
| 742 | // get rearranged afterwords. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 743 | death_notification_init(&(w->tid)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 746 | LocklessQueueWakeUpper::LocklessQueueWakeUpper(LocklessQueue queue) |
| 747 | : memory_(queue.const_memory()), pid_(getpid()), uid_(getuid()) { |
| 748 | queue.Initialize(); |
| 749 | watcher_copy_.resize(memory_->num_watchers()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 752 | int LocklessQueueWakeUpper::Wakeup(const int current_priority) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 753 | const size_t num_watchers = memory_->num_watchers(); |
| 754 | |
| 755 | CHECK_EQ(watcher_copy_.size(), num_watchers); |
| 756 | |
| 757 | // Grab a copy so it won't change out from underneath us, and we can sort it |
| 758 | // nicely in C++. |
| 759 | // Do note that there is still a window where the process can die *after* we |
| 760 | // read everything. We will still PI boost and send a signal to the thread in |
| 761 | // question. There is no way without pidfd's to close this window, and |
| 762 | // creating a pidfd is likely not RT. |
| 763 | for (size_t i = 0; i < num_watchers; ++i) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 764 | const Watcher *w = memory_->GetWatcher(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 765 | watcher_copy_[i].tid = __atomic_load_n(&(w->tid.futex), __ATOMIC_RELAXED); |
| 766 | // Force the load of the TID to come first. |
| 767 | aos_compiler_memory_barrier(); |
| 768 | watcher_copy_[i].pid = w->pid.load(std::memory_order_relaxed); |
| 769 | watcher_copy_[i].priority = w->priority.load(std::memory_order_relaxed); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 770 | |
| 771 | // Use a priority of -1 to mean an invalid entry to make sorting easier. |
| 772 | if (watcher_copy_[i].tid & FUTEX_OWNER_DIED || watcher_copy_[i].tid == 0) { |
| 773 | watcher_copy_[i].priority = -1; |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 774 | } else { |
| 775 | // Ensure all of this happens after we're done looking at the pid+priority |
| 776 | // in shared memory. |
| 777 | aos_compiler_memory_barrier(); |
| 778 | if (watcher_copy_[i].tid != static_cast<pid_t>(__atomic_load_n( |
| 779 | &(w->tid.futex), __ATOMIC_RELAXED))) { |
| 780 | // Confirm that the watcher hasn't been re-used and modified while we |
| 781 | // read it. If it has, mark it invalid again. |
| 782 | watcher_copy_[i].priority = -1; |
| 783 | watcher_copy_[i].tid = 0; |
| 784 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
| 788 | // Now sort. |
| 789 | ::std::sort(watcher_copy_.begin(), watcher_copy_.end(), |
| 790 | [](const WatcherCopy &a, const WatcherCopy &b) { |
| 791 | return a.priority > b.priority; |
| 792 | }); |
| 793 | |
| 794 | int count = 0; |
| 795 | if (watcher_copy_[0].priority != -1) { |
| 796 | const int max_priority = |
| 797 | ::std::max(current_priority, watcher_copy_[0].priority); |
| 798 | // Boost if we are RT and there is a higher priority sender out there. |
| 799 | // Otherwise we might run into priority inversions. |
| 800 | if (max_priority > current_priority && current_priority > 0) { |
| 801 | SetCurrentThreadRealtimePriority(max_priority); |
| 802 | } |
| 803 | |
| 804 | // Build up the siginfo to send. |
| 805 | siginfo_t uinfo; |
| 806 | memset(&uinfo, 0, sizeof(uinfo)); |
| 807 | |
| 808 | uinfo.si_code = SI_QUEUE; |
| 809 | uinfo.si_pid = pid_; |
| 810 | uinfo.si_uid = uid_; |
| 811 | uinfo.si_value.sival_int = 0; |
| 812 | |
| 813 | for (const WatcherCopy &watcher_copy : watcher_copy_) { |
| 814 | // The first -1 priority means we are at the end of the valid list. |
| 815 | if (watcher_copy.priority == -1) { |
| 816 | break; |
| 817 | } |
| 818 | |
| 819 | // Send the signal. Target just the thread that sent it so that we can |
| 820 | // support multiple watchers in a process (when someone creates multiple |
| 821 | // event loops in different threads). |
| 822 | rt_tgsigqueueinfo(watcher_copy.pid, watcher_copy.tid, kWakeupSignal, |
| 823 | &uinfo); |
| 824 | |
| 825 | ++count; |
| 826 | } |
| 827 | |
| 828 | // Drop back down if we were boosted. |
| 829 | if (max_priority > current_priority && current_priority > 0) { |
| 830 | SetCurrentThreadRealtimePriority(current_priority); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | return count; |
| 835 | } |
| 836 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 837 | std::ostream &operator<<(std::ostream &os, |
| 838 | const LocklessQueueSender::Result r) { |
| 839 | os << static_cast<int>(r); |
| 840 | return os; |
| 841 | } |
| 842 | |
| 843 | LocklessQueueSender::LocklessQueueSender( |
| 844 | LocklessQueueMemory *memory, |
| 845 | monotonic_clock::duration channel_storage_duration) |
| 846 | : memory_(memory), channel_storage_duration_(channel_storage_duration) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 847 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 848 | |
| 849 | // Since we already have the lock, go ahead and try cleaning up. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 850 | Cleanup(memory_, grab_queue_setup_lock); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 851 | |
| 852 | const int num_senders = memory_->num_senders(); |
| 853 | |
| 854 | for (int i = 0; i < num_senders; ++i) { |
| 855 | ::aos::ipc_lib::Sender *s = memory->GetSender(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 856 | // This doesn't need synchronization because we're the only process doing |
| 857 | // initialization right now, and nobody else will be touching senders which |
| 858 | // we're interested in. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 859 | const uint32_t tid = __atomic_load_n(&(s->tid.futex), __ATOMIC_RELAXED); |
| 860 | if (tid == 0) { |
| 861 | sender_index_ = i; |
| 862 | break; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | if (sender_index_ == -1) { |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 867 | VLOG(1) << "Too many senders, starting to bail."; |
| 868 | return; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 871 | ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 872 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 873 | // Indicate that we are now alive by taking over the slot. If the previous |
| 874 | // owner died, we still want to do this. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 875 | death_notification_init(&(sender->tid)); |
| 876 | |
| 877 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 878 | Message *const message = memory_->GetMessage(scratch_index); |
| 879 | CHECK(!message->header.queue_index.RelaxedLoad(memory_->queue_size()).valid()) |
| 880 | << ": " << std::hex << scratch_index.get(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 883 | LocklessQueueSender::~LocklessQueueSender() { |
| 884 | if (sender_index_ != -1) { |
| 885 | CHECK(memory_ != nullptr); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 886 | death_notification_release(&(memory_->GetSender(sender_index_)->tid)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 890 | std::optional<LocklessQueueSender> LocklessQueueSender::Make( |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 891 | LocklessQueue queue, monotonic_clock::duration channel_storage_duration) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 892 | queue.Initialize(); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 893 | LocklessQueueSender result(queue.memory(), channel_storage_duration); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 894 | if (result.sender_index_ != -1) { |
James Kuszmaul | 9776b39 | 2023-01-14 14:08:08 -0800 | [diff] [blame] | 895 | return result; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 896 | } else { |
| 897 | return std::nullopt; |
| 898 | } |
| 899 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 900 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 901 | size_t LocklessQueueSender::size() const { |
| 902 | return memory_->message_data_size(); |
| 903 | } |
| 904 | |
| 905 | void *LocklessQueueSender::Data() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 906 | ::aos::ipc_lib::Sender *sender = memory_->GetSender(sender_index_); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 907 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 908 | Message *const message = memory_->GetMessage(scratch_index); |
| 909 | // We should have invalidated this when we first got the buffer. Verify that |
| 910 | // in debug mode. |
| 911 | DCHECK( |
| 912 | !message->header.queue_index.RelaxedLoad(memory_->queue_size()).valid()) |
| 913 | << ": " << std::hex << scratch_index.get(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 914 | |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 915 | return message->data(memory_->message_data_size()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 918 | LocklessQueueSender::Result LocklessQueueSender::Send( |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 919 | const char *data, size_t length, |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 920 | monotonic_clock::time_point monotonic_remote_time, |
| 921 | realtime_clock::time_point realtime_remote_time, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 922 | uint32_t remote_queue_index, const UUID &source_boot_uuid, |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 923 | monotonic_clock::time_point *monotonic_sent_time, |
| 924 | realtime_clock::time_point *realtime_sent_time, uint32_t *queue_index) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 925 | CHECK_LE(length, size()); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 926 | // Flatbuffers write from the back of the buffer to the front. If we are |
| 927 | // going to write an explicit chunk of memory into the buffer, we need to |
| 928 | // adhere to this convention and place it at the end. |
| 929 | memcpy((reinterpret_cast<char *>(Data()) + size() - length), data, length); |
Austin Schuh | 91ba639 | 2020-10-03 13:27:47 -0700 | [diff] [blame] | 930 | return Send(length, monotonic_remote_time, realtime_remote_time, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 931 | remote_queue_index, source_boot_uuid, monotonic_sent_time, |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 932 | realtime_sent_time, queue_index); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 935 | LocklessQueueSender::Result LocklessQueueSender::Send( |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 936 | size_t length, monotonic_clock::time_point monotonic_remote_time, |
| 937 | realtime_clock::time_point realtime_remote_time, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 938 | uint32_t remote_queue_index, const UUID &source_boot_uuid, |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 939 | monotonic_clock::time_point *monotonic_sent_time, |
| 940 | realtime_clock::time_point *realtime_sent_time, uint32_t *queue_index) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 941 | const size_t queue_size = memory_->queue_size(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 942 | CHECK_LE(length, size()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 943 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 944 | ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_); |
| 945 | // We can do a relaxed load on our sender because we're the only person |
| 946 | // modifying it right now. |
| 947 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 948 | Message *const message = memory_->GetMessage(scratch_index); |
Austin Schuh | 91ba639 | 2020-10-03 13:27:47 -0700 | [diff] [blame] | 949 | if (CheckBothRedzones(memory_, message)) { |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 950 | return Result::INVALID_REDZONE; |
Austin Schuh | 91ba639 | 2020-10-03 13:27:47 -0700 | [diff] [blame] | 951 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 952 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 953 | // We should have invalidated this when we first got the buffer. Verify that |
| 954 | // in debug mode. |
| 955 | DCHECK( |
| 956 | !message->header.queue_index.RelaxedLoad(memory_->queue_size()).valid()) |
| 957 | << ": " << std::hex << scratch_index.get(); |
| 958 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 959 | message->header.length = length; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 960 | // Pass these through. Any alternative behavior can be implemented out a |
| 961 | // layer. |
| 962 | message->header.remote_queue_index = remote_queue_index; |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 963 | message->header.source_boot_uuid = source_boot_uuid; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 964 | message->header.monotonic_remote_time = monotonic_remote_time; |
| 965 | message->header.realtime_remote_time = realtime_remote_time; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 966 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 967 | Index to_replace = Index::Invalid(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 968 | while (true) { |
| 969 | const QueueIndex actual_next_queue_index = |
| 970 | memory_->next_queue_index.Load(queue_size); |
| 971 | const QueueIndex next_queue_index = ZeroOrValid(actual_next_queue_index); |
| 972 | |
| 973 | const QueueIndex incremented_queue_index = next_queue_index.Increment(); |
| 974 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 975 | // This needs to synchronize with whoever the previous writer at this |
| 976 | // location was. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 977 | to_replace = memory_->LoadIndex(next_queue_index); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 978 | |
| 979 | const QueueIndex decremented_queue_index = |
| 980 | next_queue_index.DecrementBy(queue_size); |
| 981 | |
| 982 | // See if we got beat. If we did, try to atomically update |
| 983 | // next_queue_index in case the previous writer failed and retry. |
| 984 | if (!to_replace.IsPlausible(decremented_queue_index)) { |
| 985 | // We don't care about the result. It will either succeed, or we got |
| 986 | // beat in fixing it and just need to give up and try again. If we got |
| 987 | // beat multiple times, the only way progress can be made is if the queue |
| 988 | // is updated as well. This means that if we retry reading |
| 989 | // next_queue_index, we will be at most off by one and can retry. |
| 990 | // |
| 991 | // Both require no further action from us. |
| 992 | // |
| 993 | // TODO(austin): If we are having fairness issues under contention, we |
| 994 | // could have a mode bit in next_queue_index, and could use a lock or some |
| 995 | // other form of PI boosting to let the higher priority task win. |
| 996 | memory_->next_queue_index.CompareAndExchangeStrong( |
| 997 | actual_next_queue_index, incremented_queue_index); |
| 998 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 999 | VLOG(3) << "We were beat. Try again. Was " << std::hex |
| 1000 | << to_replace.get() << ", is " << decremented_queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1001 | continue; |
| 1002 | } |
| 1003 | |
| 1004 | // Confirm that the message is what it should be. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1005 | // |
| 1006 | // This is just a best-effort check to skip reading the clocks if possible. |
| 1007 | // If this fails, then the compare-exchange below definitely would, so we |
| 1008 | // can bail out now. |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 1009 | const Message *message_to_replace = memory_->GetMessage(to_replace); |
| 1010 | bool is_previous_index_valid = false; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1011 | { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1012 | const QueueIndex previous_index = |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 1013 | message_to_replace->header.queue_index.RelaxedLoad(queue_size); |
| 1014 | is_previous_index_valid = previous_index.valid(); |
| 1015 | if (previous_index != decremented_queue_index && |
| 1016 | is_previous_index_valid) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1017 | // Retry. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1018 | VLOG(3) << "Something fishy happened, queue index doesn't match. " |
| 1019 | "Retrying. Previous index was " |
| 1020 | << std::hex << previous_index.index() << ", should be " |
| 1021 | << decremented_queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1022 | continue; |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | message->header.monotonic_sent_time = ::aos::monotonic_clock::now(); |
| 1027 | message->header.realtime_sent_time = ::aos::realtime_clock::now(); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 1028 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1029 | if (monotonic_sent_time != nullptr) { |
| 1030 | *monotonic_sent_time = message->header.monotonic_sent_time; |
| 1031 | } |
| 1032 | if (realtime_sent_time != nullptr) { |
| 1033 | *realtime_sent_time = message->header.realtime_sent_time; |
| 1034 | } |
| 1035 | if (queue_index != nullptr) { |
| 1036 | *queue_index = next_queue_index.index(); |
| 1037 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1038 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 1039 | const auto to_replace_monotonic_sent_time = |
| 1040 | message_to_replace->header.monotonic_sent_time; |
| 1041 | |
| 1042 | // If we are overwriting a message sent in the last |
| 1043 | // channel_storage_duration_, that means that we would be sending more than |
| 1044 | // queue_size messages and would therefore be sending too fast. If the |
| 1045 | // previous index is not valid then the message hasn't been filled out yet |
| 1046 | // so we aren't sending too fast. And, if it is not less than the sent time |
| 1047 | // of the message that we are going to write, someone else beat us and the |
| 1048 | // compare and exchange below will fail. |
| 1049 | if (is_previous_index_valid && |
| 1050 | (to_replace_monotonic_sent_time < |
| 1051 | message->header.monotonic_sent_time) && |
| 1052 | (message->header.monotonic_sent_time - to_replace_monotonic_sent_time < |
| 1053 | channel_storage_duration_)) { |
| 1054 | // There is a possibility that another context beat us to writing out the |
| 1055 | // message in the queue, but we beat that context to acquiring the sent |
| 1056 | // time. In this case our sent time is *greater than* the other context's |
| 1057 | // sent time. Therefore, we can check if we got beat filling out this |
| 1058 | // message *after* doing the above check to determine if we hit this edge |
| 1059 | // case. Otherwise, messages are being sent too fast. |
| 1060 | const QueueIndex previous_index = |
| 1061 | message_to_replace->header.queue_index.Load(queue_size); |
| 1062 | if (previous_index != decremented_queue_index && previous_index.valid()) { |
| 1063 | VLOG(3) << "Got beat during check for messages being sent too fast" |
| 1064 | "Retrying."; |
| 1065 | continue; |
| 1066 | } else { |
Austin Schuh | 71e7214 | 2023-05-03 13:10:07 -0700 | [diff] [blame] | 1067 | VLOG(1) << "Messages sent too fast. Returning. Attempted index: " |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 1068 | << decremented_queue_index.index() |
| 1069 | << " message sent time: " << message->header.monotonic_sent_time |
| 1070 | << " message to replace sent time: " |
| 1071 | << to_replace_monotonic_sent_time; |
Austin Schuh | 71e7214 | 2023-05-03 13:10:07 -0700 | [diff] [blame] | 1072 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 1073 | // Since we are not using the message obtained from scratch_index |
| 1074 | // and we are not retrying, we need to invalidate its queue_index. |
| 1075 | message->header.queue_index.Invalidate(); |
| 1076 | return Result::MESSAGES_SENT_TOO_FAST; |
| 1077 | } |
| 1078 | } |
| 1079 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1080 | // Before we are fully done filling out the message, update the Sender state |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 1081 | // with the new index to write. This re-uses the barrier for the |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1082 | // queue_index store. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1083 | const Index index_to_write(next_queue_index, scratch_index.message_index()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1084 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 1085 | aos_compiler_memory_barrier(); |
| 1086 | // We're the only person who cares about our scratch index, besides somebody |
| 1087 | // cleaning up after us. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1088 | sender->scratch_index.RelaxedStore(index_to_write); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 1089 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1090 | |
| 1091 | message->header.queue_index.Store(next_queue_index); |
| 1092 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 1093 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1094 | // The message is now filled out, and we have a confirmed slot to store |
| 1095 | // into. |
| 1096 | // |
| 1097 | // Start by writing down what we are going to pull out of the queue. This |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 1098 | // was Invalid before now. Only person who will read this is whoever cleans |
| 1099 | // up after us, so no synchronization necessary. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1100 | sender->to_replace.RelaxedStore(to_replace); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 1101 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1102 | |
| 1103 | // Then exchange the next index into the queue. |
| 1104 | if (!memory_->GetQueue(next_queue_index.Wrapped()) |
| 1105 | ->CompareAndExchangeStrong(to_replace, index_to_write)) { |
| 1106 | // Aw, didn't succeed. Retry. |
| 1107 | sender->to_replace.RelaxedInvalidate(); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 1108 | aos_compiler_memory_barrier(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1109 | VLOG(3) << "Failed to wrap into queue"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1110 | continue; |
| 1111 | } |
| 1112 | |
| 1113 | // Then update next_queue_index to save the next user some computation time. |
| 1114 | memory_->next_queue_index.CompareAndExchangeStrong(actual_next_queue_index, |
| 1115 | incremented_queue_index); |
| 1116 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 1117 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1118 | // Now update the scratch space and record that we succeeded. |
| 1119 | sender->scratch_index.Store(to_replace); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 1120 | aos_compiler_memory_barrier(); |
| 1121 | // And then record that we succeeded, but definitely after the above store. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1122 | sender->to_replace.RelaxedInvalidate(); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1123 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1124 | break; |
| 1125 | } |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1126 | |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 1127 | DCHECK(!CheckBothRedzones(memory_, memory_->GetMessage(to_replace))) |
| 1128 | << ": Invalid message found in shared memory"; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1129 | // to_replace is our current scratch_index. It isn't in the queue, which means |
| 1130 | // nobody new can pin it. They can set their `pinned` to it, but they will |
| 1131 | // back it out, so they don't count. This means that we just need to find a |
| 1132 | // message for which no pinner had it in `pinned`, and then we know this |
| 1133 | // message will never be pinned. We'll start with to_replace, and if that is |
| 1134 | // pinned then we'll look for a new one to use instead. |
| 1135 | const Index new_scratch = |
| 1136 | SwapPinnedSenderScratch(memory_, sender, to_replace); |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 1137 | DCHECK(!CheckBothRedzones( |
| 1138 | memory_, memory_->GetMessage(sender->scratch_index.RelaxedLoad()))) |
| 1139 | << ": Invalid message found in shared memory"; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1140 | |
| 1141 | // If anybody is looking at this message (they shouldn't be), then try telling |
| 1142 | // them about it (best-effort). |
| 1143 | memory_->GetMessage(new_scratch)->header.queue_index.RelaxedInvalidate(); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 1144 | return Result::GOOD; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1147 | int LocklessQueueSender::buffer_index() const { |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1148 | ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_); |
| 1149 | // We can do a relaxed load on our sender because we're the only person |
| 1150 | // modifying it right now. |
| 1151 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 1152 | return scratch_index.message_index(); |
| 1153 | } |
| 1154 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1155 | LocklessQueuePinner::LocklessQueuePinner( |
| 1156 | LocklessQueueMemory *memory, const LocklessQueueMemory *const_memory) |
| 1157 | : memory_(memory), const_memory_(const_memory) { |
| 1158 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
| 1159 | |
| 1160 | // Since we already have the lock, go ahead and try cleaning up. |
| 1161 | Cleanup(memory_, grab_queue_setup_lock); |
| 1162 | |
| 1163 | const int num_pinners = memory_->num_pinners(); |
| 1164 | |
| 1165 | for (int i = 0; i < num_pinners; ++i) { |
| 1166 | ::aos::ipc_lib::Pinner *p = memory->GetPinner(i); |
| 1167 | // This doesn't need synchronization because we're the only process doing |
| 1168 | // initialization right now, and nobody else will be touching pinners which |
| 1169 | // we're interested in. |
| 1170 | const uint32_t tid = __atomic_load_n(&(p->tid.futex), __ATOMIC_RELAXED); |
| 1171 | if (tid == 0) { |
| 1172 | pinner_index_ = i; |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | if (pinner_index_ == -1) { |
| 1178 | VLOG(1) << "Too many pinners, starting to bail."; |
| 1179 | return; |
| 1180 | } |
| 1181 | |
| 1182 | ::aos::ipc_lib::Pinner *p = memory_->GetPinner(pinner_index_); |
| 1183 | p->pinned.Invalidate(); |
| 1184 | |
| 1185 | // Indicate that we are now alive by taking over the slot. If the previous |
| 1186 | // owner died, we still want to do this. |
| 1187 | death_notification_init(&(p->tid)); |
| 1188 | } |
| 1189 | |
| 1190 | LocklessQueuePinner::~LocklessQueuePinner() { |
| 1191 | if (pinner_index_ != -1) { |
| 1192 | CHECK(memory_ != nullptr); |
| 1193 | memory_->GetPinner(pinner_index_)->pinned.Invalidate(); |
| 1194 | aos_compiler_memory_barrier(); |
| 1195 | death_notification_release(&(memory_->GetPinner(pinner_index_)->tid)); |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | std::optional<LocklessQueuePinner> LocklessQueuePinner::Make( |
| 1200 | LocklessQueue queue) { |
| 1201 | queue.Initialize(); |
| 1202 | LocklessQueuePinner result(queue.memory(), queue.const_memory()); |
| 1203 | if (result.pinner_index_ != -1) { |
James Kuszmaul | 9776b39 | 2023-01-14 14:08:08 -0800 | [diff] [blame] | 1204 | return result; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1205 | } else { |
| 1206 | return std::nullopt; |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | // This method doesn't mess with any scratch_index, so it doesn't have to worry |
| 1211 | // about message ownership. |
| 1212 | int LocklessQueuePinner::PinIndex(uint32_t uint32_queue_index) { |
| 1213 | const size_t queue_size = memory_->queue_size(); |
| 1214 | const QueueIndex queue_index = |
| 1215 | QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index); |
| 1216 | ipc_lib::Pinner *const pinner = memory_->GetPinner(pinner_index_); |
| 1217 | |
| 1218 | AtomicIndex *const queue_slot = memory_->GetQueue(queue_index.Wrapped()); |
| 1219 | |
| 1220 | // Indicate that we want to pin this message. |
| 1221 | pinner->pinned.Store(queue_index); |
| 1222 | aos_compiler_memory_barrier(); |
| 1223 | |
| 1224 | { |
| 1225 | const Index message_index = queue_slot->Load(); |
| 1226 | Message *const message = memory_->GetMessage(message_index); |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 1227 | DCHECK(!CheckBothRedzones(memory_, message)) |
| 1228 | << ": Invalid message found in shared memory"; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1229 | |
| 1230 | const QueueIndex message_queue_index = |
| 1231 | message->header.queue_index.Load(queue_size); |
| 1232 | if (message_queue_index == queue_index) { |
| 1233 | VLOG(3) << "Eq: " << std::hex << message_queue_index.index(); |
| 1234 | aos_compiler_memory_barrier(); |
| 1235 | return message_index.message_index(); |
| 1236 | } |
| 1237 | VLOG(3) << "Message reused: " << std::hex << message_queue_index.index() |
| 1238 | << ", " << queue_index.index(); |
| 1239 | } |
| 1240 | |
| 1241 | // Being down here means we asked to pin a message before realizing it's no |
| 1242 | // longer in the queue, so back that out now. |
| 1243 | pinner->pinned.Invalidate(); |
| 1244 | VLOG(3) << "Unpinned: " << std::hex << queue_index.index(); |
| 1245 | return -1; |
| 1246 | } |
| 1247 | |
| 1248 | size_t LocklessQueuePinner::size() const { |
| 1249 | return const_memory_->message_data_size(); |
| 1250 | } |
| 1251 | |
| 1252 | const void *LocklessQueuePinner::Data() const { |
| 1253 | const size_t queue_size = const_memory_->queue_size(); |
| 1254 | const ::aos::ipc_lib::Pinner *const pinner = |
| 1255 | const_memory_->GetPinner(pinner_index_); |
| 1256 | QueueIndex pinned = pinner->pinned.RelaxedLoad(queue_size); |
| 1257 | CHECK(pinned.valid()); |
| 1258 | const Message *message = const_memory_->GetMessage(pinned); |
| 1259 | |
| 1260 | return message->data(const_memory_->message_data_size()); |
| 1261 | } |
| 1262 | |
| 1263 | LocklessQueueReader::Result LocklessQueueReader::Read( |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1264 | uint32_t uint32_queue_index, |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 1265 | monotonic_clock::time_point *monotonic_sent_time, |
| 1266 | realtime_clock::time_point *realtime_sent_time, |
| 1267 | monotonic_clock::time_point *monotonic_remote_time, |
| 1268 | realtime_clock::time_point *realtime_remote_time, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1269 | uint32_t *remote_queue_index, UUID *source_boot_uuid, size_t *length, |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 1270 | char *data) const { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1271 | const size_t queue_size = memory_->queue_size(); |
| 1272 | |
| 1273 | // Build up the QueueIndex. |
| 1274 | const QueueIndex queue_index = |
| 1275 | QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index); |
| 1276 | |
| 1277 | // Read the message stored at the requested location. |
| 1278 | Index mi = memory_->LoadIndex(queue_index); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1279 | const Message *m = memory_->GetMessage(mi); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1280 | |
| 1281 | while (true) { |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 1282 | DCHECK(!CheckBothRedzones(memory_, m)) |
| 1283 | << ": Invalid message found in shared memory"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1284 | // We need to confirm that the data doesn't change while we are reading it. |
| 1285 | // Do that by first confirming that the message points to the queue index we |
| 1286 | // want. |
| 1287 | const QueueIndex starting_queue_index = |
| 1288 | m->header.queue_index.Load(queue_size); |
| 1289 | if (starting_queue_index != queue_index) { |
| 1290 | // If we found a message that is exactly 1 loop old, we just wrapped. |
| 1291 | if (starting_queue_index == queue_index.DecrementBy(queue_size)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1292 | VLOG(3) << "Matches: " << std::hex << starting_queue_index.index() |
| 1293 | << ", " << queue_index.DecrementBy(queue_size).index(); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1294 | return Result::NOTHING_NEW; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | // Someone has re-used this message between when we pulled it out of the |
| 1298 | // queue and when we grabbed its index. It is pretty hard to deduce |
| 1299 | // what happened. Just try again. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1300 | const Message *const new_m = memory_->GetMessage(queue_index); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1301 | if (m != new_m) { |
| 1302 | m = new_m; |
| 1303 | VLOG(3) << "Retrying, m doesn't match"; |
| 1304 | continue; |
| 1305 | } |
| 1306 | |
| 1307 | // We have confirmed that message still points to the same message. This |
| 1308 | // means that the message didn't get swapped out from under us, so |
| 1309 | // starting_queue_index is correct. |
| 1310 | // |
| 1311 | // Either we got too far behind (signaled by this being a valid |
| 1312 | // message), or this is one of the initial messages which are invalid. |
| 1313 | if (starting_queue_index.valid()) { |
| 1314 | VLOG(3) << "Too old. Tried for " << std::hex << queue_index.index() |
| 1315 | << ", got " << starting_queue_index.index() << ", behind by " |
| 1316 | << std::dec |
| 1317 | << (starting_queue_index.index() - queue_index.index()); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1318 | return Result::TOO_OLD; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | VLOG(3) << "Initial"; |
| 1322 | |
| 1323 | // There isn't a valid message at this location. |
| 1324 | // |
| 1325 | // If someone asks for one of the messages within the first go around, |
| 1326 | // then they need to wait. They got ahead. Otherwise, they are |
| 1327 | // asking for something crazy, like something before the beginning of |
| 1328 | // the queue. Tell them that they are behind. |
| 1329 | if (uint32_queue_index < memory_->queue_size()) { |
| 1330 | VLOG(3) << "Near zero, " << std::hex << uint32_queue_index; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1331 | return Result::NOTHING_NEW; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1332 | } else { |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1333 | VLOG(3) << "Not near zero, " << std::hex << uint32_queue_index; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1334 | return Result::TOO_OLD; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1335 | } |
| 1336 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1337 | VLOG(3) << "Eq: " << std::hex << starting_queue_index.index() << ", " |
| 1338 | << queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1339 | break; |
| 1340 | } |
| 1341 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1342 | // Then read the data out. Copy it all out to be deterministic and so we can |
| 1343 | // make length be from either end. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1344 | *monotonic_sent_time = m->header.monotonic_sent_time; |
| 1345 | *realtime_sent_time = m->header.realtime_sent_time; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1346 | if (m->header.remote_queue_index == 0xffffffffu) { |
| 1347 | *remote_queue_index = queue_index.index(); |
| 1348 | } else { |
| 1349 | *remote_queue_index = m->header.remote_queue_index; |
| 1350 | } |
| 1351 | *monotonic_remote_time = m->header.monotonic_remote_time; |
| 1352 | *realtime_remote_time = m->header.realtime_remote_time; |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1353 | *source_boot_uuid = m->header.source_boot_uuid; |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 1354 | if (data) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1355 | memcpy(data, m->data(memory_->message_data_size()), |
| 1356 | memory_->message_data_size()); |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 1357 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1358 | *length = m->header.length; |
| 1359 | |
| 1360 | // And finally, confirm that the message *still* points to the queue index we |
| 1361 | // want. This means it didn't change out from under us. |
| 1362 | // If something changed out from under us, we were reading it much too late in |
| 1363 | // it's lifetime. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 1364 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1365 | const QueueIndex final_queue_index = m->header.queue_index.Load(queue_size); |
| 1366 | if (final_queue_index != queue_index) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1367 | VLOG(3) << "Changed out from under us. Reading " << std::hex |
| 1368 | << queue_index.index() << ", finished with " |
| 1369 | << final_queue_index.index() << ", delta: " << std::dec |
| 1370 | << (final_queue_index.index() - queue_index.index()); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1371 | return Result::OVERWROTE; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1374 | return Result::GOOD; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1377 | QueueIndex LocklessQueueReader::LatestIndex() const { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1378 | const size_t queue_size = memory_->queue_size(); |
| 1379 | |
| 1380 | // There is only one interesting case. We need to know if the queue is empty. |
| 1381 | // That is done with a sentinel value. At worst, this will be off by one. |
| 1382 | const QueueIndex next_queue_index = |
| 1383 | memory_->next_queue_index.Load(queue_size); |
| 1384 | if (next_queue_index.valid()) { |
| 1385 | const QueueIndex current_queue_index = next_queue_index.DecrementBy(1u); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1386 | return current_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1387 | } |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 1388 | return QueueIndex::Invalid(); |
| 1389 | } |
| 1390 | |
| 1391 | size_t LocklessQueueSize(const LocklessQueueMemory *memory) { |
| 1392 | return memory->queue_size(); |
| 1393 | } |
| 1394 | |
| 1395 | size_t LocklessQueueMessageDataSize(const LocklessQueueMemory *memory) { |
| 1396 | return memory->message_data_size(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | namespace { |
| 1400 | |
| 1401 | // Prints out the mutex state. Not safe to use while the mutex is being |
| 1402 | // changed. |
| 1403 | ::std::string PrintMutex(aos_mutex *mutex) { |
| 1404 | ::std::stringstream s; |
| 1405 | s << "aos_mutex(" << ::std::hex << mutex->futex; |
| 1406 | |
| 1407 | if (mutex->futex != 0) { |
| 1408 | s << ":"; |
| 1409 | if (mutex->futex & FUTEX_OWNER_DIED) { |
| 1410 | s << "FUTEX_OWNER_DIED|"; |
| 1411 | } |
| 1412 | s << "tid=" << (mutex->futex & FUTEX_TID_MASK); |
| 1413 | } |
| 1414 | |
| 1415 | s << ")"; |
| 1416 | return s.str(); |
| 1417 | } |
| 1418 | |
| 1419 | } // namespace |
| 1420 | |
| 1421 | void PrintLocklessQueueMemory(LocklessQueueMemory *memory) { |
| 1422 | const size_t queue_size = memory->queue_size(); |
| 1423 | ::std::cout << "LocklessQueueMemory (" << memory << ") {" << ::std::endl; |
| 1424 | ::std::cout << " aos_mutex queue_setup_lock = " |
| 1425 | << PrintMutex(&memory->queue_setup_lock) << ::std::endl; |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 1426 | ::std::cout << " bool initialized = " << memory->initialized << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1427 | ::std::cout << " config {" << ::std::endl; |
| 1428 | ::std::cout << " size_t num_watchers = " << memory->config.num_watchers |
| 1429 | << ::std::endl; |
| 1430 | ::std::cout << " size_t num_senders = " << memory->config.num_senders |
| 1431 | << ::std::endl; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1432 | ::std::cout << " size_t num_pinners = " << memory->config.num_pinners |
| 1433 | << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1434 | ::std::cout << " size_t queue_size = " << memory->config.queue_size |
| 1435 | << ::std::endl; |
| 1436 | ::std::cout << " size_t message_data_size = " |
| 1437 | << memory->config.message_data_size << ::std::endl; |
| 1438 | |
| 1439 | ::std::cout << " AtomicQueueIndex next_queue_index = " |
| 1440 | << memory->next_queue_index.Load(queue_size).DebugString() |
| 1441 | << ::std::endl; |
| 1442 | |
Austin Schuh | 3328d13 | 2020-02-28 13:54:57 -0800 | [diff] [blame] | 1443 | ::std::cout << " uid_t uid = " << memory->uid << ::std::endl; |
| 1444 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1445 | ::std::cout << " }" << ::std::endl; |
| 1446 | ::std::cout << " AtomicIndex queue[" << queue_size << "] {" << ::std::endl; |
| 1447 | for (size_t i = 0; i < queue_size; ++i) { |
| 1448 | ::std::cout << " [" << i << "] -> " |
| 1449 | << memory->GetQueue(i)->Load().DebugString() << ::std::endl; |
| 1450 | } |
| 1451 | ::std::cout << " }" << ::std::endl; |
| 1452 | ::std::cout << " Message messages[" << memory->num_messages() << "] {" |
| 1453 | << ::std::endl; |
| 1454 | for (size_t i = 0; i < memory->num_messages(); ++i) { |
| 1455 | Message *m = memory->GetMessage(Index(i, i)); |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame] | 1456 | ::std::cout << " [" << i << "] -> Message 0x" << std::hex |
| 1457 | << (reinterpret_cast<uintptr_t>( |
| 1458 | memory->GetMessage(Index(i, i))) - |
| 1459 | reinterpret_cast<uintptr_t>(memory)) |
| 1460 | << std::dec << " {" << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1461 | ::std::cout << " Header {" << ::std::endl; |
| 1462 | ::std::cout << " AtomicQueueIndex queue_index = " |
| 1463 | << m->header.queue_index.Load(queue_size).DebugString() |
| 1464 | << ::std::endl; |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame] | 1465 | ::std::cout << " monotonic_clock::time_point monotonic_sent_time = " |
| 1466 | << m->header.monotonic_sent_time << " 0x" << std::hex |
| 1467 | << m->header.monotonic_sent_time.time_since_epoch().count() |
| 1468 | << std::dec << ::std::endl; |
| 1469 | ::std::cout << " realtime_clock::time_point realtime_sent_time = " |
| 1470 | << m->header.realtime_sent_time << " 0x" << std::hex |
| 1471 | << m->header.realtime_sent_time.time_since_epoch().count() |
| 1472 | << std::dec << ::std::endl; |
| 1473 | ::std::cout |
| 1474 | << " monotonic_clock::time_point monotonic_remote_time = " |
| 1475 | << m->header.monotonic_remote_time << " 0x" << std::hex |
| 1476 | << m->header.monotonic_remote_time.time_since_epoch().count() |
| 1477 | << std::dec << ::std::endl; |
| 1478 | ::std::cout << " realtime_clock::time_point realtime_remote_time = " |
| 1479 | << m->header.realtime_remote_time << " 0x" << std::hex |
| 1480 | << m->header.realtime_remote_time.time_since_epoch().count() |
| 1481 | << std::dec << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1482 | ::std::cout << " size_t length = " << m->header.length |
| 1483 | << ::std::endl; |
| 1484 | ::std::cout << " }" << ::std::endl; |
Austin Schuh | be41674 | 2020-10-03 17:24:26 -0700 | [diff] [blame] | 1485 | const bool corrupt = CheckBothRedzones(memory, m); |
| 1486 | if (corrupt) { |
| 1487 | absl::Span<char> pre_redzone = m->PreRedzone(memory->message_data_size()); |
| 1488 | absl::Span<char> post_redzone = |
| 1489 | m->PostRedzone(memory->message_data_size(), memory->message_size()); |
| 1490 | |
| 1491 | ::std::cout << " pre-redzone: \"" |
| 1492 | << absl::BytesToHexString(std::string_view( |
| 1493 | pre_redzone.data(), pre_redzone.size())) |
| 1494 | << std::endl; |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 1495 | ::std::cout << " // *** DATA REDZONES ARE CORRUPTED ***" |
| 1496 | << ::std::endl; |
Austin Schuh | be41674 | 2020-10-03 17:24:26 -0700 | [diff] [blame] | 1497 | ::std::cout << " post-redzone: \"" |
| 1498 | << absl::BytesToHexString(std::string_view( |
| 1499 | post_redzone.data(), post_redzone.size())) |
| 1500 | << std::endl; |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 1501 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1502 | ::std::cout << " data: {"; |
| 1503 | |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame] | 1504 | if (FLAGS_dump_lockless_queue_data) { |
| 1505 | const char *const m_data = m->data(memory->message_data_size()); |
Austin Schuh | be41674 | 2020-10-03 17:24:26 -0700 | [diff] [blame] | 1506 | std::cout << absl::BytesToHexString(std::string_view( |
| 1507 | m_data, corrupt ? memory->message_data_size() : m->header.length)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1508 | } |
| 1509 | ::std::cout << ::std::setfill(' ') << ::std::dec << "}" << ::std::endl; |
| 1510 | ::std::cout << " }," << ::std::endl; |
| 1511 | } |
| 1512 | ::std::cout << " }" << ::std::endl; |
| 1513 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1514 | ::std::cout << " Sender senders[" << memory->num_senders() << "] {" |
| 1515 | << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1516 | for (size_t i = 0; i < memory->num_senders(); ++i) { |
| 1517 | Sender *s = memory->GetSender(i); |
| 1518 | ::std::cout << " [" << i << "] -> Sender {" << ::std::endl; |
| 1519 | ::std::cout << " aos_mutex tid = " << PrintMutex(&s->tid) |
| 1520 | << ::std::endl; |
| 1521 | ::std::cout << " AtomicIndex scratch_index = " |
| 1522 | << s->scratch_index.Load().DebugString() << ::std::endl; |
| 1523 | ::std::cout << " AtomicIndex to_replace = " |
| 1524 | << s->to_replace.Load().DebugString() << ::std::endl; |
| 1525 | ::std::cout << " }" << ::std::endl; |
| 1526 | } |
| 1527 | ::std::cout << " }" << ::std::endl; |
| 1528 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1529 | ::std::cout << " Pinner pinners[" << memory->num_pinners() << "] {" |
| 1530 | << ::std::endl; |
| 1531 | for (size_t i = 0; i < memory->num_pinners(); ++i) { |
| 1532 | Pinner *p = memory->GetPinner(i); |
| 1533 | ::std::cout << " [" << i << "] -> Pinner {" << ::std::endl; |
| 1534 | ::std::cout << " aos_mutex tid = " << PrintMutex(&p->tid) |
| 1535 | << ::std::endl; |
| 1536 | ::std::cout << " AtomicIndex scratch_index = " |
| 1537 | << p->scratch_index.Load().DebugString() << ::std::endl; |
| 1538 | ::std::cout << " AtomicIndex pinned = " |
| 1539 | << p->pinned.Load(memory->queue_size()).DebugString() |
| 1540 | << ::std::endl; |
| 1541 | ::std::cout << " }" << ::std::endl; |
| 1542 | } |
| 1543 | ::std::cout << " }" << ::std::endl; |
| 1544 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1545 | ::std::cout << " Watcher watchers[" << memory->num_watchers() << "] {" |
| 1546 | << ::std::endl; |
| 1547 | for (size_t i = 0; i < memory->num_watchers(); ++i) { |
| 1548 | Watcher *w = memory->GetWatcher(i); |
| 1549 | ::std::cout << " [" << i << "] -> Watcher {" << ::std::endl; |
| 1550 | ::std::cout << " aos_mutex tid = " << PrintMutex(&w->tid) |
| 1551 | << ::std::endl; |
| 1552 | ::std::cout << " pid_t pid = " << w->pid << ::std::endl; |
| 1553 | ::std::cout << " int priority = " << w->priority << ::std::endl; |
| 1554 | ::std::cout << " }" << ::std::endl; |
| 1555 | } |
| 1556 | ::std::cout << " }" << ::std::endl; |
| 1557 | |
| 1558 | ::std::cout << "}" << ::std::endl; |
| 1559 | } |
| 1560 | |
| 1561 | } // namespace ipc_lib |
| 1562 | } // namespace aos |