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

   ## Summary
   
   This PR fixes the locks of the TCP backlog, of `tcp_ioctl()` and of 
`/proc/net/tcp`, and the line count of `/proc/net/tcp` and `udp`.
   
   The input path holds the device lock, then takes the TCP connection list 
lock (`tcp_active()`, `tcp_alloc_accept()`), then the lock of a listener. Code 
that read the queues of a connection without the device lock, or that held the 
list lock or a listener's lock and then took a device lock, stopped the network 
or read freed write buffers.
   
   1. **`net/tcp: Lock the device in tcp_ioctl().`** `FIONREAD`, `FIONSPACE` 
and `FIOC_FILEPATH` (also `fcntl(F_GETPATH)`) read the read-ahead and write 
queues with only the connection lock, while an ACK releases write buffers. With 
`CONFIG_NET_SEND_BUFSIZE` > 0 the kernel faults in 
`tcp_wrbuffer_inqueue_size()`. Now `tcp_ioctl()` takes the device lock, then 
the connection lock, as `psock_tcp_send()` does.
   2. **`net/tcp: Keep the parent of a pending connection that listen() 
moves.`** `listen()` again moves the pending connections to the new backlog but 
clears their `blparent`. A moved connection that the peer resets then holds a 
backlog slot, and the next clients' SYNs are dropped. Now the move sets 
`blparent` again.
   3. **`net/tcp: Keep the list lock while a listener takes a new 
connection.`** `tcp_accept_connection()` used the listener after 
`tcp_findlistener()` released the list lock; a `close()` in between freed it. 
Now the list lock stays held until the listener has the connection.
   4. **`net/tcp: Free the pending connections of a backlog without a lock.`** 
The teardown of a backlog freed each pending connection, which takes its device 
lock, with the list lock or the listener's lock held. Now the backlog is taken 
away under the lock and freed after it.
   5. **`net/tcp: Free a listener's half-open connections without the list 
lock.`** `tcp_unlisten()` freed the connections in `TCP_SYN_RCVD` with the list 
lock held. Now it frees each one with its device locked first.
   6. **`net/procfs: Count the lines of /proc/net/tcp and udp in full.`** The 
tables counted their lines in a `uint8_t`: with 255 connections or more 
(TIME_WAIT counts) a read started again at the head and never ended. Now the 
count is an `unsigned int`, and the list is walked once for both address 
families.
   7. **`net/procfs: Count the lines of a device file in 16 bits.`** 
`/proc/net/<device>` has one line generator per IPv6 address with 
`CONFIG_NETDEV_MULTIPLE_IPv6`, more than 255 near the limit of 
`CONFIG_NETDEV_MAX_IPv6_ADDR`. Now the count is a `uint16_t`.
   8. **`net/procfs: Lock the device of each connection in /proc/net/tcp.`** 
The TCP table walks the write queues with only the list lock, as `tcp_ioctl()` 
did before commit 1. The device lock cannot be waited for under the list lock, 
so the walk tries it, and when another thread holds it, unlocks the list, locks 
the device, locks the list again and finds its line again by its count.
   
   ## Impact
   
   `net/tcp` (`tcp_ioctl.c`, `tcp_backlog.c`, `tcp_conn.c`, `tcp_listen.c`, 
`tcp.h`) and `net/procfs` (`net_tcp.c`, `net_udp.c`, `procfs.h`); `tcp.h` and 
`procfs.h` are internal headers. No API, ABI or Kconfig change. Common network 
code: the same in flat, protected and kernel builds and on every architecture; 
commits 2 and 4 change only `CONFIG_NET_TCPBACKLOG` code. The format of the 
`/proc/net` files does not change; the IPv4 and IPv6 lines of a table now come 
in list order, not IPv4 first.
   
   #22077 also changes `tcp_accept_connection()`; the two merge without a 
conflict. In the kernel build the backlog race test also needs #22025: without 
it `tcp_alloc()` frees an old TIME_WAIT connection with the list lock held and 
stops the network the same way (`tcp_reclaim()`, fixed there).
   
   ## Testing
   
   qemu-armv8a:knsh (kernel build) and qemu-armv8a:nsh (flat build), 4 CPUs 
(`CONFIG_SMP`), HVF (Apple M-series); TCP over the loopback device, 
`NET_TCPBACKLOG`, `NET_SOLINGER`, `NET_TCP_ALLOC_CONNS=1`, 
`NET_ALLOC_DEVIF_CALLBACKS=1`, `NET_SEND_BUFSIZE` 16384, `DEBUG_ASSERTIONS`.
   
   | Commit | Test | master | this PR |
   |---|---|---|---|
   | 1 | `tcppathrace path 20`: two threads send over 127.0.0.1, a third calls 
`fcntl(F_GETPATH)` in a loop | kernel panic in the first run, 4 of 4 boots 
(data abort at 0xc in `tcp_wrbuffer_inqueue_size()`) | 12 of 12 runs pass |
   | 2 | `test_tcp_backlog_relisten_rst` (apache/nuttx-apps#APPS) | the next 
client's `connect()` times out | PASS |
   | 3, 4 | `test_tcp_backlog_relisten_race`, `test_tcp_backlog_close_race` 
(`listen()` again and `close()` while clients connect) | the network stops in 
round 1 (list lock waits for the device lock) | PASS (knsh with #22025) |
   | 6 | `test_tcp_procfs_many`: 300 connections in TIME_WAIT, read in 1 KiB 
parts | the read passes 1200 lines (it starts again) | PASS |
   | 6 | `procnettcp count 400 10` / `udp 400` / `churn 400 10` | the read 
passes 1 MiB | 420 / 400 / 4430 to 4464 lines |
   | 8 | `tcppathrace proc`: the same as for commit 1, with reads of 
`/proc/net/tcp` | kernel panic in 1 of 4 runs (data abort in 
`tcp_wrbuffer_inqueue_size()` from `netprocfs_tcpstats()`) | every run passes |
   
   All of the above again on this branch, on master 5814b404c95, knsh and flat 
nsh with 4 CPUs: `cmocka_net_tcp` passes 17 of 17 on knsh and 16 of 17 on flat 
nsh. In flat nsh `test_tcp_reset_send` fails on master and with this PR alike: 
the `SIGPIPE` goes to another thread of the process.
   
   In 2 of 5 knsh boots of one round the system stopped later, at the exit of a 
process, on a deadlock between the critical section and the lock of the 
low-priority work queue (`_exit()` closes the sockets inside the critical 
section; `work_qcancel()` holds the queue lock and enters the critical section 
in `wd_start()`). That code is the same on master, which stops earlier in these 
tests; it needs a separate fix.
   
   Builds: sabre-6quad:netknsh (ARMv7-A kernel build) and rv-virt:netnsh (RV32 
flat), also with IPv6, backlog, write buffers and procfs: 0 warnings in `net/`.
   
   Commits 5 and 7 have no failing test of their own: commit 5 and the use 
after free of commit 3 were found by reading; commit 7 needs more than 255 IPv6 
addresses on one device. Text size: +192 (commits 2 to 5), +32 (commit 1), +320 
(commits 6 to 8) bytes on qemu-armv8a:knsh. Builds: qemu-armv8a:nsh with 
`NET_TCPBACKLOG=n`, with IPv6, without write buffers; rv-virt:knetnsh64; 
qemu-armv7a:nsh: no new warning.
   
   `tools/checkpatch.sh -c -u -m -g` passes. Documentation: no change.
   


-- 
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