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

   ## Summary
   
   Commit 7f04498f157 ("drivers/net/vlan: strip vlan devices from their 
reliance on real device lock") removed netdev_lock() from around the receive 
loop in netdev_upper_rxpoll_work(), so
   that a VLAN-tagged frame could be dispatched to the VLAN device without 
holding the real device lock. The non-VLAN branch of the same loop lost the 
lock as well.
   
   That branch still needs it. netdev_upper_input() documents "Called with the 
network device locked", and netdev_upper_txavail_work() continues to take the 
lock. With only one side locked,
   receive and transmit race on dev->d_iob. devif_send() prepares dev->d_iob 
through netdev_iob_prepare() and then copies into it with iob_trycopyin(); an 
unlocked receive can replace or
   release that IOB in between, and the copy gets a NULL chain:
   
   Assertion failed : at file: iob/iob_copyin.c:69 task: hpwork
   
   
   This takes the lock around each non-VLAN frame instead of around the whole 
loop. netdev_upper_input() gets its lock back and is mutually exclusive with 
the TX path again, while the VLAN
   dispatch still runs without the real device lock, so the real-time 
improvement of the original change is kept.
   
   Reported-by: raiden00pl in 
https://github.com/apache/nuttx/pull/21931#issuecomment-6977937414
   
   ## Impact
   
   Fixes a crash on the receive path of any driver using the netdev upper half, 
whenever TX runs concurrently with RX. CONFIG_NET_VLAN does not have to be 
enabled to be affected.
   
   The lock is now taken and released once per received frame rather than once 
per poll batch. This is the rmutex fast path when uncontended, and the previous 
code held the lock for the wholebatch anyway, so throughput is not expected to 
change. Measured 4 x 256 KiB of concurrent TCP echo with no regression.
   
   No API or configuration change.
   
   ## Testing
   
   Host: x86_64 Linux. Configuration: sim:dynconns (flat build) with the TAP 
device bridged to the host bridge nuttx0 (10.0.1.1/24); the device is 
10.0.1.2/24. For the test I enabled
   DEBUG_ASSERTIONS, EXAMPLES_TCPECHO, SIM_NET_BRIDGE, FS_PROCFS and 
NETINIT_THREAD, disabled NETDEV_LATEINIT, and raised the IOB pool to 
IOB_NBUFFERS=256 / IOB_BUFSIZE=1536. CONFIG_NET_VLAN was left disabled, which 
shows the bug is not specific to VLAN.
   
   The reported case, 4 concurrent TCP echo connections of 256 KiB each.
   
   Before, on 7f04498f157:
   
   conn0: sent=2048 recv=0 err=TimeoutError('timed out')
   conn1: sent=2048 recv=64 err=TimeoutError('timed out')
   conn2: sent=2048 recv=0 err=TimeoutError('timed out')
   conn3: sent=2048 recv=0 err=TimeoutError('timed out')
   dump_assert_info: Assertion failed : at file: iob/iob_copyin.c:69 task: 
hpwork process: Kernel 0x400034e0
   
   
   After:
   
   conn0: sent=262144 recv=262144 err=None
   conn1: sent=262144 recv=262144 err=None
   conn2: sent=262144 recv=262144 err=None
   conn3: sent=262144 recv=262144 err=None
   
   
   The crash needs far less than that. A single 64-byte TCP connection is 
enough, while ICMP alone never triggers it, which matches the analysis: TCP has 
a send path that can sit between
   netdev_iob_prepare() and iob_trycopyin(), and a ping does not.
   
   Before, on 7f04498f157:
   
   === only ping, no TCP ===
   assert_count_after_ping=0
   === one TCP echo of 64 bytes ===
   sent=64 recv=64 match=True
   assert_count_after_tcp=1
   
   
   After, same two steps:
   
   assert_count_after_ping=0
   sent=64 recv=64 match=True
   assert_count_after_tcp=0
   
   
   Note that the echo itself succeeds before the assertion fires, so the crash 
is not a data path failure.
   
   ./tools/checkpatch.sh -c -u -f drivers/net/netdev_upperhalf.c passes. Build 
and link succeed.


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