casaroli opened a new pull request, #22033:
URL: https://github.com/apache/nuttx/pull/22033

   ## Summary
   
   Since bc862acb935 (#19849), `tcp_stop_timer()` cancels the timer of a 
connection with `work_cancel_sync()`: `tcp_free()` waits for a 
`tcp_timer_expiry()` that runs already. `tcp_timer_expiry()` takes the 
connection list lock and then the device lock. Many callers of `tcp_free()` 
hold one of these locks, so if the timer work of the connection has started, it 
waits for that lock, `tcp_free()` waits for the work, and the network stops.
   
   Callers of `tcp_free()` that hold a lock that the timer work takes:
   
   - `tcp_input()` with the device lock: a connection in `TCP_SYN_RCVD` (a 
duplicate SYN, a RST, the ACK of the handshake with no listener or a full 
backlog), a RST on a connection in a backlog.
   - `tcp_timer()` with the device lock and the list lock 
(`devif_poll_tcp_connections()` holds the list lock for the whole poll): the 
retries of a SYN-ACK are exhausted. Here the timing comes by itself: 
`tcp_timer_expiry()` sets `conn->timeout` and then waits for the device lock of 
the poll that runs; that poll sees the timeout, frees the connection and waits 
for the same work.
   - `tcp_unlisten()` with the list lock: the `TCP_SYN_RCVD` connections of a 
listener that closes, whose SYN-ACK timer is queued.
   - `tcp_alloc()` with the list lock, and from `tcp_alloc_accept()` also with 
the device lock: the reuse of a closing connection when the pool is empty.
   
   A packet to a local address is received inside the poll, so over the 
loopback device the input path holds the device lock and the list lock.
   
   This PR cancels the timer without waiting. It is safe because of the other 
change of bc862acb935: `tcp_timer_expiry()` uses the connection only when it 
finds it in the active list, under the list lock, and `tcp_free()` removes the 
connection from that list before it stops the timer. A timer work that runs 
already does not use the connection after that, and the work queue does not use 
the `work_s` after it starts the work, so the structure can be freed and used 
again at once. `tcp_stop_timer()` has no other caller.
   
   ## Impact
   
   `net/tcp` only: one call, comments. No API, ABI or Kconfig change. The same 
in flat, protected and kernel builds, on every architecture.
   
   A timer work that runs late can set `timeout` of a new connection that uses 
the same structure again and is in the active list already: that connection 
then runs `tcp_timer()` once early. This was also so before bc862acb935; it is 
no memory error.
   
   ## Testing
   
   qemu-armv8a:knsh under HVF (Apple M-series), 4 CPUs (`CONFIG_SMP`), 4 
low-priority work threads, TCP over the loopback device, a fixed pool of 8 
connections (`NET_TCP_PREALLOC_CONNS=8`, `NET_TCP_ALLOC_CONNS=0`), 
`NET_TCP_WAIT_TIMEOUT=1`; a test program in which client threads connect to a 
listener on 127.0.0.1 and close first, so that new connections must reuse 
closing ones.
   
   The runs use the branch of #22025 as the base: on master, other faults of 
the reuse path, which #22025 fixes, stop the test first. `tcp_free()`, 
`tcp_stop_timer()` and `tcp_timer_expiry()` are the same in both.
   
   The window is short: no run without help showed the deadlock. To show it, a 
change for the test only (not in this PR) waits, before `tcp_free()` of a 
reused connection, until the timer work of that connection starts.
   
   | Tree (with the test change) | Result |
   |---|---|
   | without this PR | the network stops at the first time the timer work 
starts in the window (2 of 2 boots); gdb: hpwork holds the device lock and the 
list lock and waits in `work_qcancel()` from `tcp_free()`; a low-priority 
worker runs `tcp_timer_expiry()` of the same connection and waits for the list 
lock |
   | with this PR | 4 of 4 runs pass; in one run the timer work started 14 
times in the window |
   
   `tools/checkpatch.sh -c -u -m -g` passes. Documentation: no change (no page 
describes `tcp_stop_timer()` or `tcp_free()`).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to