| 2021-07-30 - File descriptor migration between threads |
| |
| An FD migration may happen on any idle connection that experiences a takeover() |
| operation by another thread. In this case the acting thread becomes the owner |
| of the connection (and FD) while previous one(s) need to forget about it. |
| |
| File descriptor migration between threads is a fairly complex operation because |
| it is required to maintain a durable consistency between the pollers states and |
| the haproxy's desired state. Indeed, very often the FD is registered within one |
| thread's poller and that thread might be waiting in the system, so there is no |
| way to synchronously update it. This is where thread_mask, polled_mask and per |
| thread updates are used: |
| |
| - a thread knows if it's allowed to manipulate an FD by looking at its bit in |
| the FD's thread_mask ; |
| |
| - each thread knows if it was polling an FD by looking at its bit in the |
| polled_mask field ; a recent migration is usually indicated by a bit being |
| present in polled_mask and absent from thread_mask. |
| |
| - other threads know whether it's safe to take over an FD by looking at the |
| running mask: if it contains any other thread's bit, then other threads are |
| using it and it's not safe to take it over. |
| |
| - sleeping threads are notified about the need to update their polling via |
| local or global updates to the FD. Each thread has its own local update |
| list and its own bit in the update_mask to know whether there are pending |
| updates for it. This allows to reconverge polling with the desired state |
| at the last instant before polling. |
| |
| While the description above could be seen as "progressive" (it technically is) |
| in that there is always a transition and convergence period in a migrated FD's |
| life, functionally speaking it's perfectly atomic thanks to the running bit and |
| to the per-thread idle connections lock: no takeover is permitted without |
| holding the idle_conns lock, and takeover may only happen by atomically picking |
| a connection from the list that is also protected by this lock. In practice, an |
| FD is never taken over by itself, but always in the context of a connection, |
| and by atomically removing a connection from an idle list, it is possible to |
| guarantee that a connection will not be picked, hence that its FD will not be |
| taken over. |
| |
| same thread as list! |
| |
| The possible entry points to a race to use a file descriptor are the following |
| ones, with their respective sequences: |
| |
| 1) takeover: requested by conn_backend_get() on behalf of connect_server() |
| - take the idle_conns_lock, protecting against a parallel access from the |
| I/O tasklet or timeout task |
| - pick the first connection from the list |
| - attempt an fd_takeover() on this connection's fd. Usually it works, |
| unless a late wakeup of the owning thread shows up in the FD's running |
| mask. The operation is performed in fd_takeover() using a DWCAS which |
| tries to switch both running and thread_mask to the caller's tid_bit. A |
| concurrent bit in running is enough to make it fail. This guarantees |
| another thread does not wakeup from I/O in the middle of the takeover. |
| In case of conflict, this FD is skipped and the attempt is tried again |
| with the next connection. |
| - resets the task/tasklet contexts to NULL, as a signal that they are not |
| allowed to run anymore. The tasks retrieve their execution context from |
| the scheduler in the arguments, but will check the tasks' context from |
| the structure under the lock to detect this possible change, and abort. |
| - at this point the takeover succeeded, the idle_conns_lock is released and |
| the connection and its FD are now owned by the caller |
| |
| 2) poll report: happens on late rx, shutdown or error on idle conns |
| - fd_set_running() is called to atomically set the running_mask and check |
| that the caller's tid_bit is still present in the thread_mask. Upon |
| failure the caller arranges itself to stop reporting that FD (e.g. by |
| immediate removal or by an asynchronous update). Upon success, it's |
| guaranteed that any concurrent fd_takeover() will fail the DWCAS and that |
| another connection will need to be picked instead. |
| - FD's state is possibly updated |
| - the iocb is called if needed (almost always) |
| - if the iocb didn't kill the connection, release the bit from running_mask |
| making the connection possibly available to a subsequent fd_takeover(). |
| |
| 3) I/O tasklet, timeout task: timeout or subscribed wakeup |
| - start by taking the idle_conns_lock, ensuring no takeover() will pick the |
| same connection from this point. |
| - check the task/tasklet's context to verify that no recently completed |
| takeover() stole the connection. If it's NULL, the connection was lost, |
| the lock is released and the task/tasklet killed. Otherwise it is |
| guaranteed that no other thread may use that connection (current takeover |
| candidates are waiting on the lock, previous owners waking from poll() |
| lost their bit in the thread_mask and will not touch the FD). |
| - the connection is removed from the idle conns list. From this point on, |
| no other thread will even find it there nor even try fd_takeover() on it. |
| - the idle_conns_lock is now released, the connection is protected and its |
| FD is not reachable by other threads anymore. |
| - the task does what it has to do |
| - if the connection is still usable (i.e. not upon timeout), it's inserted |
| again into the idle conns list, meaning it may instantly be taken over |
| by a competing thread. |
| |
| 4) wake() callback: happens on last user after xfers (may free() the conn) |
| - the connection is still owned by the caller, it's still subscribed to |
| polling but the connection is idle thus inactive. Errors or shutdowns |
| may be reported late, via sock_conn_iocb() and conn_notify_mux(), thus |
| the running bit is set (i.e. a concurrent fd_takeover() will fail). |
| - if the connection is in the list, the idle_conns_lock is grabbed, the |
| connection is removed from the list, and the lock is released. |
| - mux->wake() is called |
| - if the connection previously was in the list, it's reinserted under the |
| idle_conns_lock. |
| |
| |
| With the DWCAS removal between running_mask & thread_mask: |
| |
| fd_takeover: |
| 1 if (!CAS(&running_mask, 0, tid_bit)) |
| 2 return fail; |
| 3 atomic_store(&thread_mask, tid_bit); |
| 4 atomic_and(&running_mask, ~tid_bit); |
| |
| poller: |
| 1 do { |
| 2 /* read consistent running_mask & thread_mask */ |
| 3 do { |
| 4 run = atomic_load(&running_mask); |
| 5 thr = atomic_load(&thread_mask); |
| 6 } while (run & ~thr); |
| 7 |
| 8 if (!(thr & tid_bit)) { |
| 9 /* takeover has started */ |
| 10 goto disable_fd; |
| 11 } |
| 12 } while (!CAS(&running_mask, run, run | tid_bit)); |
| |
| fd_delete: |
| 1 atomic_or(&running_mask, tid_bit); |
| 2 atomic_store(&thread_mask, 0); |
| 3 atomic_and(&running_mask, ~tid_bit); |
| |
| The loop in poller:3-6 is used to make sure the thread_mask we read matches |
| the last updated running_mask. If nobody can give up on fd_takeover(), it |
| might even be possible to spin on thread_mask only. Late pollers will not |
| set running anymore with this. |