This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 5019a8878bef165d28242d16e9a5c33057601258 Author: wangjinjing1 <[email protected]> AuthorDate: Wed Nov 18 10:22:34 2026 +0800 net/netforward: set up the forward callback under the outgoing device lock netfwd_forward() runs in the context of the device a packet came in on, but it links a forward callback into the callback lists of the device the packet goes out on. Those lists are also walked and changed by the worker of the outgoing device, so the two could race: the callback could run before it was fully set up, and fwd could be freed by the other worker while it was still used here. Do the work with the lock of the outgoing device held. To avoid an AB-BA deadlock when two devices forward packets to each other, release the lock of the incoming device first with netdev_breaklock() and restore it with netdev_restorelock() afterwards. The fwd pointer is not used any more once the callback has been set up, and the incoming device's IOB is released after the lock is restored. When the packet goes out on the device it came in on, the device lock is already held, so skip the lock reordering and the device state save. Signed-off-by: wangjinjing <[email protected]> --- net/netforward/netfwd_forward.c | 106 ++++++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 15 deletions(-) diff --git a/net/netforward/netfwd_forward.c b/net/netforward/netfwd_forward.c index eacbc50956a..fdf380161bc 100644 --- a/net/netforward/netfwd_forward.c +++ b/net/netforward/netfwd_forward.c @@ -201,6 +201,50 @@ static uint32_t netfwd_eventhandler(FAR struct net_driver_s *dev, return flags; } +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netfwd_forward_notify + * + * Description: + * Set up the forward callback on the outgoing device and notify its + * driver that TX data is available. + * + * Input Parameters: + * fwd - An initialized instance of the common forwarding structure that + * includes everything needed to perform the forwarding operation. + * + * Returned Value: + * Zero is returned on success; a negated errno value is returned if the + * callback could not be allocated. + * + * Assumptions: + * The lock of the outgoing device (fwd->f_dev) is held. + * + ****************************************************************************/ + +static inline_function int netfwd_forward_notify(FAR struct forward_s *fwd) +{ + /* Set up the callback in the connection */ + + fwd->f_cb = netfwd_callback_alloc(fwd->f_dev); + if (fwd->f_cb != NULL) + { + fwd->f_cb->flags = (IPFWD_POLL | NETDEV_DOWN); + fwd->f_cb->priv = (FAR void *)fwd; + fwd->f_cb->event = netfwd_eventhandler; + + /* Notify the device driver of the availability of TX data */ + + netdev_txnotify_dev(fwd->f_dev, IPFWD_POLL); + return OK; + } + + return -EBUSY; +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -217,7 +261,14 @@ static uint32_t netfwd_eventhandler(FAR struct net_driver_s *dev, * asynchronously and must return without waiting for the send to * complete. * + * The callback lists of the outgoing device are also changed by the + * worker of that device, so they are only touched with the lock of the + * outgoing device held. The lock of the incoming device is released + * meanwhile, to avoid an AB-BA deadlock when two devices forward packets + * to each other. + * * Input Parameters: + * dev - The device on which the packet was received. Its lock is held. * fwd - An initialized instance of the common forwarding structure that * includes everything needed to perform the forwarding operation. * @@ -230,39 +281,64 @@ static uint32_t netfwd_eventhandler(FAR struct net_driver_s *dev, int netfwd_forward(FAR struct net_driver_s *dev, FAR struct forward_s *fwd) { + FAR struct net_driver_s *fwddev; + FAR struct iob_s *iob; + FAR uint8_t *buf; + unsigned int count; + int len; + int ret; + DEBUGASSERT(fwd != NULL && fwd->f_iob != NULL && fwd->f_dev != NULL); - /* Set up the callback in the connection */ + /* The worker of the outgoing device may free fwd as soon as the callback + * is set up, so do not touch fwd after netfwd_forward_notify(). + */ - fwd->f_cb = netfwd_callback_alloc(fwd->f_dev); - if (fwd->f_cb != NULL) - { - FAR struct iob_s *iob = dev->d_iob; - FAR uint8_t *buf = dev->d_buf; - int len = dev->d_len; + fwddev = fwd->f_dev; - fwd->f_cb->flags = (IPFWD_POLL | NETDEV_DOWN); - fwd->f_cb->priv = (FAR void *)fwd; - fwd->f_cb->event = netfwd_eventhandler; + if (fwddev == dev) + { + /* The packet goes out on the device it came in on: the lock of that + * device is already held, so no lock reordering is needed, and the + * device state does not have to be saved. + */ + ret = netfwd_forward_notify(fwd); + } + else + { /* Save the current state of the device, avoid damaging the current * d_iob and other resources when forwarding recursion */ + iob = dev->d_iob; + buf = dev->d_buf; + len = dev->d_len; + dev->d_iob = NULL; dev->d_buf = NULL; dev->d_len = 0; - /* Notify the device driver of the availability of TX data */ + /* Release the lock of the incoming device before taking the lock of + * the outgoing one. + */ - netdev_txnotify_dev(fwd->f_dev, IPFWD_POLL); + netdev_breaklock(dev, &count); + netdev_lock(fwddev); + + ret = netfwd_forward_notify(fwd); + + netdev_unlock(fwddev); + netdev_restorelock(dev, count); + + /* Release the IOB that the outgoing device may have left in dev */ + + netdev_iob_release(dev); dev->d_iob = iob; dev->d_buf = buf; dev->d_len = len; - - return OK; } - return -EBUSY; + return ret; }
