driver/net: replace net_lock with netdev_lock

modify the code of the adapted protocol stack to avoid deadlocks and the
logic that cannot be protected by locks after modification.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
This commit is contained in:
zhanghongyu
2026-01-04 16:50:01 +08:00
committed by Donny(董九柱)
parent 5e1410ecab
commit fa6c8579dd
17 changed files with 90 additions and 90 deletions
+4 -4
View File
@@ -134,13 +134,13 @@ static int sim_can_txavail(struct net_driver_s *dev)
/* Ignore the notification if the interface is not yet up */
net_lock();
netdev_lock(dev);
if (IFF_IS_UP(priv->dev.d_flags))
{
devif_poll(&priv->dev, sim_can_txpoll);
}
net_unlock();
netdev_unlock(dev);
return OK;
}
@@ -189,7 +189,7 @@ static void sim_can_work(void *arg)
* in priv->dev.d_len
*/
net_lock();
netdev_lock(&priv->dev);
priv->dev.d_len = ret;
priv->dev.d_buf = (FAR uint8_t *)&hframe;
@@ -198,7 +198,7 @@ static void sim_can_work(void *arg)
NETDEV_RXPACKETS(&priv->dev);
can_input(&priv->dev);
net_unlock();
netdev_unlock(&priv->dev);
}
nodata:
+4 -4
View File
@@ -1545,7 +1545,7 @@ static void enc_irqworker(FAR void *arg)
/* Get exclusive access to both the network and the SPI bus. */
net_lock();
netdev_lock(&priv->dev);
enc_lock(priv);
/* Disable further interrupts by clearing the global interrupt enable bit.
@@ -1739,7 +1739,7 @@ static void enc_irqworker(FAR void *arg)
/* Release lock on the SPI bus and the network */
enc_unlock(priv);
net_unlock();
netdev_unlock(&priv->dev);
}
/****************************************************************************
@@ -1812,7 +1812,7 @@ static void enc_toworker(FAR void *arg)
/* Get exclusive access to the network */
net_lock();
netdev_lock(&priv->dev);
/* Increment statistics and dump debug info */
@@ -1834,7 +1834,7 @@ static void enc_toworker(FAR void *arg)
/* Release lock on the network */
net_unlock();
netdev_unlock(&priv->dev);
}
/****************************************************************************
+4 -4
View File
@@ -1735,7 +1735,7 @@ static void enc_irqworker(FAR void *arg)
/* Get exclusive access to both the network and the SPI bus. */
net_lock();
netdev_lock(&priv->dev);
enc_lock(priv);
/* A good practice is for the host controller to clear the Global Interrupt
@@ -1884,7 +1884,7 @@ static void enc_irqworker(FAR void *arg)
/* Release lock on the SPI bus and the network */
enc_unlock(priv);
net_unlock();
netdev_unlock(&priv->dev);
}
/****************************************************************************
@@ -1957,7 +1957,7 @@ static void enc_toworker(FAR void *arg)
/* Get exclusive access to the network. */
net_lock();
netdev_lock(&priv->dev);
/* Increment statistics and dump debug info */
@@ -1979,7 +1979,7 @@ static void enc_toworker(FAR void *arg)
/* Release the network */
net_unlock();
netdev_unlock(&priv->dev);
}
/****************************************************************************
+6 -6
View File
@@ -809,7 +809,7 @@ static void ftmac100_interrupt_work(FAR void *arg)
/* Process pending Ethernet interrupts */
net_lock();
netdev_lock(&priv->ft_dev);
status = priv->status;
ninfo("status=%08x(%08x) BASE=%p ISR=%p PHYCR=%p\n",
@@ -883,7 +883,7 @@ out:
putreg32 (INT_MASK_ALL_ENABLED, &iobase->imr);
ninfo("ISR-done\n");
net_unlock();
netdev_unlock(&priv->ft_dev);
/* Re-enable Ethernet interrupts */
@@ -972,12 +972,12 @@ static void ftmac100_txtimeout_work(FAR void *arg)
/* Process pending Ethernet interrupts */
net_lock();
netdev_lock(&priv->ft_dev);
/* Then poll the network for new XMIT data */
devif_poll(&priv->ft_dev, ftmac100_txpoll);
net_unlock();
netdev_unlock(&priv->ft_dev);
}
/****************************************************************************
@@ -1137,7 +1137,7 @@ static void ftmac100_txavail_work(FAR void *arg)
/* Perform the poll */
net_lock();
netdev_lock(&priv->ft_dev);
/* Ignore the notification if the interface is not yet up */
@@ -1152,7 +1152,7 @@ static void ftmac100_txavail_work(FAR void *arg)
devif_poll(&priv->ft_dev, ftmac100_txpoll);
}
net_unlock();
netdev_unlock(&priv->ft_dev);
}
/****************************************************************************
+12 -12
View File
@@ -812,7 +812,7 @@ static void lan91c111_interrupt_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(dev);
/* Process pending Ethernet interrupts */
@@ -873,7 +873,7 @@ static void lan91c111_interrupt_work(FAR void *arg)
}
}
net_unlock();
netdev_unlock(dev);
/* Re-enable Ethernet interrupts */
@@ -949,7 +949,7 @@ static int lan91c111_ifup(FAR struct net_driver_s *dev)
dev->d_ipv6addr[6], dev->d_ipv6addr[7]);
#endif
net_lock();
netdev_lock(dev);
/* Initialize PHYs, Ethernet interface, and setup up Ethernet interrupts */
@@ -974,7 +974,7 @@ static int lan91c111_ifup(FAR struct net_driver_s *dev)
copyto16(priv, ADDR0_REG, &dev->d_mac.ether, sizeof(dev->d_mac.ether));
net_unlock();
netdev_unlock(dev);
/* Enable the Ethernet interrupt */
@@ -1056,7 +1056,7 @@ static void lan91c111_txavail_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(dev);
/* Ignore the notification if the interface is not yet up */
@@ -1074,7 +1074,7 @@ static void lan91c111_txavail_work(FAR void *arg)
}
}
net_unlock();
netdev_unlock(dev);
}
/****************************************************************************
@@ -1187,9 +1187,9 @@ static int lan91c111_addmac(FAR struct net_driver_s *dev,
/* Add the MAC address to the hardware multicast routing table */
net_lock();
netdev_lock(dev);
modifyreg16(priv, MCAST_REG1 + off, 0, 1 << bit);
net_unlock();
netdev_unlock(dev);
return OK;
}
@@ -1234,9 +1234,9 @@ static int lan91c111_rmmac(FAR struct net_driver_s *dev,
/* Remove the MAC address from the hardware multicast routing table */
net_lock();
netdev_lock(dev);
modifyreg16(priv, MCAST_REG1 + off, 1 << bit, 0);
net_unlock();
netdev_unlock(dev);
return OK;
}
@@ -1269,7 +1269,7 @@ static int lan91c111_ioctl(FAR struct net_driver_s *dev, int cmd,
FAR struct mii_ioctl_data_s *req = (FAR void *)arg;
int ret = OK;
net_lock();
netdev_lock(dev);
/* Decode and dispatch the driver-specific IOCTL command */
@@ -1292,7 +1292,7 @@ static int lan91c111_ioctl(FAR struct net_driver_s *dev, int cmd,
ret = -ENOTTY; /* Special return value for this case */
}
net_unlock();
netdev_unlock(dev);
return ret;
}
#endif
+6 -6
View File
@@ -1525,7 +1525,7 @@ static void lan9250_txavail_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(dev);
lan9250_lock_spi(priv);
/* Ignore the notification if the interface is not yet up */
@@ -1544,7 +1544,7 @@ static void lan9250_txavail_work(FAR void *arg)
/* Release lock on the SPI bus and the network */
lan9250_unlock_spi(priv);
net_unlock();
netdev_unlock(dev);
}
/****************************************************************************
@@ -1798,7 +1798,7 @@ static void lan9250_int_worker(FAR void *arg)
/* Get exclusive access to both the network and the SPI bus. */
net_lock();
netdev_lock(&priv->dev);
lan9250_lock_spi(priv);
/* There is no infinite loop check... if there are always pending
@@ -1985,7 +1985,7 @@ static void lan9250_int_worker(FAR void *arg)
/* Release lock on the SPI bus and the network */
lan9250_unlock_spi(priv);
net_unlock();
netdev_unlock(&priv->dev);
/* Enable ISR_GPIO interrupts after unlocking net so that application
* could have chance to process Ethernet packet and free iob.
@@ -2061,7 +2061,7 @@ static void lan9250_txtout_worker(FAR void *arg)
/* Get exclusive access to the network */
net_lock();
netdev_lock(&priv->dev);
/* Increment statistics and dump debug info */
@@ -2083,7 +2083,7 @@ static void lan9250_txtout_worker(FAR void *arg)
/* Release lock on the network */
net_unlock();
netdev_unlock(&priv->dev);
}
/****************************************************************************
+2 -2
View File
@@ -194,7 +194,7 @@ static void lo_txavail_work(FAR void *arg)
/* Ignore the notification if the interface is not yet up */
net_lock();
netdev_lock(&priv->lo_dev);
if (priv->lo_bifup)
{
/* Reuse the devif_loopback() logic, Polling all pending events until
@@ -204,7 +204,7 @@ static void lo_txavail_work(FAR void *arg)
while (devif_poll(&priv->lo_dev, NULL));
}
net_unlock();
netdev_unlock(&priv->lo_dev);
}
/****************************************************************************
+6 -6
View File
@@ -466,7 +466,7 @@ static void skel_interrupt_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(&priv->sk_dev);
/* Process pending Ethernet interrupts */
@@ -484,7 +484,7 @@ static void skel_interrupt_work(FAR void *arg)
*/
skel_txdone(priv);
net_unlock();
netdev_unlock(&priv->sk_dev);
/* Re-enable Ethernet interrupts */
@@ -564,7 +564,7 @@ static void skel_txtimeout_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(&priv->sk_dev);
/* Increment statistics and dump debug info */
@@ -575,7 +575,7 @@ static void skel_txtimeout_work(FAR void *arg)
/* Then poll the network for new XMIT data */
devif_poll(&priv->sk_dev, skel_txpoll);
net_unlock();
netdev_unlock(&priv->sk_dev);
}
/****************************************************************************
@@ -731,7 +731,7 @@ static void skel_txavail_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(&priv->sk_dev);
/* Ignore the notification if the interface is not yet up */
@@ -744,7 +744,7 @@ static void skel_txavail_work(FAR void *arg)
devif_poll(&priv->sk_dev, skel_txpoll);
}
net_unlock();
netdev_unlock(&priv->sk_dev);
}
/****************************************************************************
+4 -4
View File
@@ -750,7 +750,7 @@ static void slip_interrupt_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(&self->dev);
/* Process pending Ethernet interrupts */
@@ -798,7 +798,7 @@ static void slip_interrupt_work(FAR void *arg)
slip_txdone(self);
}
net_unlock();
netdev_unlock(&self->dev);
}
/****************************************************************************
@@ -899,7 +899,7 @@ static void slip_txavail_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(&self->dev);
/* Ignore the notification if the interface is not yet up */
@@ -917,7 +917,7 @@ static void slip_txavail_work(FAR void *arg)
}
}
net_unlock();
netdev_unlock(&self->dev);
}
/****************************************************************************
+8 -8
View File
@@ -748,7 +748,7 @@ static void tun_txavail_work(FAR void *arg)
return;
}
net_lock();
netdev_lock(&priv->dev);
if (priv->bifup)
{
/* Poll the network for new XMIT data */
@@ -756,7 +756,7 @@ static void tun_txavail_work(FAR void *arg)
devif_poll(&priv->dev, tun_txpoll);
}
net_unlock();
netdev_unlock(&priv->dev);
nxmutex_unlock(&priv->lock);
}
@@ -1007,13 +1007,13 @@ static ssize_t tun_write(FAR struct file *filep, FAR const char *buffer,
if (priv->write_d_len == 0)
{
net_lock();
netdev_lock(&priv->dev);
netdev_iob_release(&priv->dev);
ret = netdev_iob_prepare(&priv->dev, false, 0);
priv->dev.d_buf = NULL;
if (ret < 0)
{
net_unlock();
netdev_unlock(&priv->dev);
break;
}
@@ -1021,14 +1021,14 @@ static ssize_t tun_write(FAR struct file *filep, FAR const char *buffer,
buflen, -llhdrlen, false);
if (ret < 0)
{
net_unlock();
netdev_unlock(&priv->dev);
break;
}
priv->dev.d_len = buflen;
tun_net_receive(priv);
net_unlock();
netdev_unlock(&priv->dev);
ret = buflen;
break;
@@ -1128,9 +1128,9 @@ static ssize_t tun_read(FAR struct file *filep, FAR char *buffer,
priv->read_buf = NULL;
priv->read_d_len = 0;
net_lock();
netdev_lock(&priv->dev);
tun_txdone(priv);
net_unlock();
netdev_unlock(&priv->dev);
break;
}
+7 -7
View File
@@ -1513,7 +1513,7 @@ static void w5500_interrupt_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(&self->w_dev);
/* Process pending Ethernet interrupts. Read IR, MIR and SIR in one shot
* to optimize latency, although MIR is not actually used.
@@ -1592,7 +1592,7 @@ static void w5500_interrupt_work(FAR void *arg)
}
done:
net_unlock();
netdev_unlock(&self->w_dev);
/* Re-enable Ethernet interrupts */
@@ -1602,7 +1602,7 @@ done:
error:
w5500_fence(self);
net_unlock();
netdev_unlock(&self->w_dev);
}
/****************************************************************************
@@ -1667,7 +1667,7 @@ static void w5500_txtimeout_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(&self->w_dev);
/* Increment statistics and dump debug info */
@@ -1684,7 +1684,7 @@ static void w5500_txtimeout_work(FAR void *arg)
devif_poll(&self->w_dev, w5500_txpoll);
}
net_unlock();
netdev_unlock(&self->w_dev);
}
/****************************************************************************
@@ -1848,7 +1848,7 @@ static void w5500_txavail_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(&priv->w_dev);
/* Ignore the notification if the interface is not yet up */
@@ -1861,7 +1861,7 @@ static void w5500_txavail_work(FAR void *arg)
devif_poll(&priv->w_dev, w5500_txpoll);
}
net_unlock();
netdev_unlock(&priv->w_dev);
}
/****************************************************************************
+4 -4
View File
@@ -530,7 +530,7 @@ static void cdcecm_interrupt_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(&self->dev);
/* Check if we received an incoming packet, if so, call cdcecm_receive() */
@@ -558,7 +558,7 @@ static void cdcecm_interrupt_work(FAR void *arg)
cdcecm_txdone(self);
}
net_unlock();
netdev_unlock(&self->dev);
}
/****************************************************************************
@@ -666,7 +666,7 @@ static void cdcecm_txavail_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(&self->dev);
/* Ignore the notification if the interface is not yet up */
@@ -675,7 +675,7 @@ static void cdcecm_txavail_work(FAR void *arg)
devif_poll(&self->dev, cdcecm_txpoll);
}
net_unlock();
netdev_unlock(&self->dev);
}
/****************************************************************************
+4 -4
View File
@@ -1022,7 +1022,7 @@ static void rndis_rxdispatch(FAR void *arg)
FAR struct eth_hdr_s *hdr;
irqstate_t flags;
net_lock();
netdev_lock(&priv->netdev);
flags = enter_critical_section();
rndis_giverxreq(priv);
priv->netdev.d_len = priv->current_rx_datagram_size;
@@ -1099,7 +1099,7 @@ static void rndis_rxdispatch(FAR void *arg)
rndis_freenetreq(priv);
}
net_unlock();
netdev_unlock(&priv->netdev);
}
/****************************************************************************
@@ -1192,7 +1192,7 @@ static void rndis_txavail_work(FAR void *arg)
{
FAR struct rndis_dev_s *priv = (FAR struct rndis_dev_s *)arg;
net_lock();
netdev_lock(&priv->netdev);
if (rndis_allocnetreq(priv))
{
@@ -1203,7 +1203,7 @@ static void rndis_txavail_work(FAR void *arg)
}
}
net_unlock();
netdev_unlock(&priv->netdev);
}
/****************************************************************************
+4 -4
View File
@@ -2209,7 +2209,7 @@ static void cdcmbim_receive(FAR struct usbhost_cdcmbim_s *priv,
{
uinfo("received packet: %d len\n", len);
net_lock();
netdev_lock(&priv->netdev);
NETDEV_RXPACKETS(&priv->netdev);
@@ -2249,7 +2249,7 @@ static void cdcmbim_receive(FAR struct usbhost_cdcmbim_s *priv,
NETDEV_RXERRORS(dev);
}
net_unlock();
netdev_unlock(&priv->netdev);
}
/****************************************************************************
@@ -2405,7 +2405,7 @@ static void cdcmbim_txavail_work(FAR void *arg)
{
FAR struct usbhost_cdcmbim_s *priv = (FAR struct usbhost_cdcmbim_s *)arg;
net_lock();
netdev_lock(&priv->netdev);
priv->netdev.d_buf = (FAR uint8_t *)priv->txpktbuf;
@@ -2414,7 +2414,7 @@ static void cdcmbim_txavail_work(FAR void *arg)
devif_poll(&priv->netdev, cdcmbim_txpoll);
}
net_unlock();
netdev_unlock(&priv->netdev);
}
/****************************************************************************
@@ -437,7 +437,7 @@ static void bcmf_tx_poll_work(FAR void *arg)
{
FAR struct bcmf_dev_s *priv = (FAR struct bcmf_dev_s *)arg;
net_lock();
netdev_lock(&priv->bc_dev);
/* Ignore the notification if the interface is not yet up */
@@ -462,7 +462,7 @@ static void bcmf_tx_poll_work(FAR void *arg)
}
}
net_unlock();
netdev_unlock(&priv->bc_dev);
}
/****************************************************************************
@@ -492,7 +492,7 @@ static void bcmf_rxpoll_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(&priv->bc_dev);
/* Tx work will hold the d_buf until there is data to send,
* replace and cache the d_buf temporarily
@@ -512,7 +512,7 @@ static void bcmf_rxpoll_work(FAR void *arg)
#if 0
bcmf_txdone(priv);
#endif
net_unlock();
netdev_unlock(&priv->bc_dev);
}
/****************************************************************************
@@ -464,7 +464,7 @@ static int xbeenet_rxframe(FAR struct xbeenet_driver_s *priv,
ind->frame = NULL;
net_lock();
netdev_lock(&priv->xd_dev.r_dev);
/* Transfer the frame to the network logic */
@@ -506,7 +506,7 @@ static int xbeenet_rxframe(FAR struct xbeenet_driver_s *priv,
if (ret < 0)
#endif
{
net_unlock();
netdev_unlock(&priv->xd_dev.r_dev);
ind->frame = iob;
return ret;
}
@@ -516,7 +516,7 @@ static int xbeenet_rxframe(FAR struct xbeenet_driver_s *priv,
NETDEV_RXPACKETS(&priv->xd_dev.r_dev);
NETDEV_RXIPV6(&priv->xd_dev.r_dev);
net_unlock();
netdev_unlock(&priv->xd_dev.r_dev);
/* sixlowpan_input() will free the IOB, but we must free the struct
* ieee802154_primitive_s container here.
@@ -752,7 +752,7 @@ static void xbeenet_txavail_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(&priv->xd_dev.r_dev);
/* Ignore the notification if the interface is not yet up */
@@ -769,7 +769,7 @@ static void xbeenet_txavail_work(FAR void *arg)
devif_poll(&priv->xd_dev.r_dev, xbeenet_txpoll_callback);
}
net_unlock();
netdev_unlock(&priv->xd_dev.r_dev);
}
/****************************************************************************
@@ -955,7 +955,7 @@ static void spirit_receive_work(FAR void *arg)
wlinfo("Send frame %p to the network: Offset=%u Length=%u\n",
iob, iob->io_offset, iob->io_len);
net_lock();
netdev_lock(&priv->radio.r_dev);
ret = sixlowpan_input(&priv->radio, iob, (FAR void *)pktmeta);
if (ret < 0)
{
@@ -964,7 +964,7 @@ static void spirit_receive_work(FAR void *arg)
NETDEV_ERRORS(&priv->radio.r_dev);
}
net_unlock();
netdev_unlock(&priv->radio.r_dev);
/* sixlowpan_input() will free the IOB, but we must free the struct
* pktradio_metadata_s container here.
@@ -1567,7 +1567,7 @@ static void spirit_txtimeout_work(FAR void *arg)
* worker thread has been configured.
*/
net_lock();
netdev_lock(&priv->radio.r_dev);
/* Increment statistics and dump debug info */
@@ -1583,7 +1583,7 @@ static void spirit_txtimeout_work(FAR void *arg)
*/
work_queue(LPWORK, &priv->pollwork, spirit_txpoll_work, priv, 0);
net_unlock();
netdev_unlock(&priv->radio.r_dev);
}
}
@@ -1653,7 +1653,7 @@ static void spirit_txpoll_work(FAR void *arg)
* thread has been configured.
*/
net_lock();
netdev_lock(&priv->radio.r_dev);
#ifdef CONFIG_NET_6LOWPAN
/* Make sure the our single packet buffer is attached */
@@ -1670,7 +1670,7 @@ static void spirit_txpoll_work(FAR void *arg)
devif_poll(&priv->radio.r_dev, spirit_txpoll_callback);
}
net_unlock();
netdev_unlock(&priv->radio.r_dev);
}
/****************************************************************************