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

   ## Summary
   
   In a kernel build, the network can complete a receive or a send in another 
task, for example in the work queue of the network device. The memory of the 
calling process is not mapped there. `recvfrom()` and `sendto()` copy the data 
through a kernel buffer for this reason, but `read()`, `write()`, `readv()`, 
`writev()`, `recvmsg()` and `sendmsg()` of a socket give the user buffers to 
the network. Then a receive handler can write the data while another address 
environment is active: the data goes to the wrong address space, and the reader 
gets old bytes. The unbuffered TCP and UDP sends can read from the wrong 
address space in the same way.
   
   All these calls go through `psock_recvmsg()` and `psock_sendmsg()`. The 
first commit makes these two functions copy the data, the address and the 
control data through kernel memory in a kernel build. A stream socket moves the 
data in parts of up to 16 KiB, so a large `read()` or `write()` needs no large 
kernel buffer. A datagram is copied whole.
   
   The second commit removes the copy of `recvfrom()` and `sendto()`, because 
it is now done twice. That copy also allocated the full request length, so a 
large `send()` failed with `ENOMEM` when the kernel heap had no block of that 
size.
   
   ## Impact
   
   Kernel builds only: the new code is under `CONFIG_BUILD_KERNEL`, the same 
guard as the old copy in `recvfrom()` and `sendto()`. Flat and protected builds 
have one address space and do not compile it. In a flat build, 
`psock_recvmsg()` is 4 bytes smaller (the result no longer goes through an 
`int`); all other code is the same.
   
   In a kernel build, a blocking `recv()` or `read()` of a stream socket 
returns at most 16 KiB per call without `MSG_WAITALL`. POSIX lets a stream 
receive return less than the request. Kernel callers of `psock_recvmsg()` and 
`psock_sendmsg()` (for example NFS and usrsock) also get the copy.
   
   | text (bytes) | master | this PR |
   |---|---|---|
   | `qemu-armv8a:netnsh` (flat) | 559204 | 559200 (-4) |
   | `qemu-armv8a:knsh` + TCP, UDP, local sockets | 316920 | 317640 (+720) |
   | `rv-virt:knetnsh64` | 231495 | 232051 (+556) |
   
   Data and bss do not change. No API, option or header changes. No 
documentation describes this path.
   
   ## Testing
   
   The test is the new `test_tcp_process_data` case of `cmocka_net_tcp` 
(apache/nuttx-apps#@@APPSPR@@). It sends 1 MiB over TCP on 127.0.0.1 to a 
forked child with `write()` and `writev()`. The child reads with `read()` and 
`readv()` and checks every byte. Master d729a200002, 3 boots on each side, 1 
CPU:
   
   | Configuration | master | this PR |
   |---|---|---|
   | `qemu-armv8a:knsh` (kernel, arm64, QEMU TCG) | FAIL 3/3: the child gets 
wrong bytes | PASS 3/3 |
   | `qemu-intel64:knsh_romfs` (kernel, x86_64, QEMU KVM) | FAIL 3/3: the child 
gets wrong bytes | PASS 3/3 |
   
   The other cases give the same result on both sides: the other 
`cmocka_net_tcp` cases, `cmocka_net_local` (6, with `SCM_RIGHTS`), 
`cmocka_net_local_dgram` (91) and, on x86_64, `cmocka_net_udp` (24). 
`test_tcp_close_timewait`, and on x86_64 `test_tcp_reset_send`, also fail on 
master; this PR does not change them. The arm64 kernel runs also have 
apache/nuttx#22329 on both sides, so that the programs start.
   
   Flat `qemu-armv8a:nsh` with the same options: `cmocka_net_tcp` and 
`cmocka_net_local` give the same results as on master.
   
   Builds with `-Werror` (CI image): `qemu-armv8a:netnsh`, `qemu-armv8a:knsh`, 
`qemu-intel64:knsh_romfs`, `rv-virt:knetnsh64`, `rv-virt:netnsh64`, 
`qemu-armv7a:citest_knsh`, and `rv-virt:pnsh64` (protected) with TCP, UDP and 
local sockets (same text size as master). `tools/checkpatch.sh -c -u -m -g` 
passes.
   


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

  • [PR] net/socket: Copy socket ... casaroli (via GitHub) via commits

Reply via email to