From 5374af077bf22dd00fedfb8f621d214202c09960 Mon Sep 17 00:00:00 2001 From: zhanghongyu Date: Fri, 20 Oct 2023 15:17:48 +0800 Subject: [PATCH] tun: Fix the error of calling tun_close when tun_txavail or tun_txavail_work is executed When tun_txavail_work is running, switch to tun_close thread and priv->lock will be destroyed, then switch back to tun_txavail_work thread, an error will occur when nxmutex_unlock(&priv->lock) Signed-off-by: zhanghongyu --- drivers/net/tun.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 3f00b0b365..bd7e01567a 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -775,14 +775,21 @@ static void tun_txavail_work(FAR void *arg) static int tun_txavail(FAR struct net_driver_s *dev) { FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private; + irqstate_t flags; - /* Schedule to perform the TX poll on the worker thread. */ + flags = enter_critical_section(); /* No interrupts */ - if (work_available(&priv->work)) + /* Schedule to perform the TX poll on the worker thread when priv->bifup + * is true. + */ + + if (priv->bifup && work_available(&priv->work)) { work_queue(TUNWORK, &priv->work, tun_txavail_work, priv, 0); } + leave_critical_section(flags); + return OK; } @@ -911,6 +918,8 @@ static void tun_dev_uninit(FAR struct tun_device_s *priv) tun_ifdown(&priv->dev); + work_cancel_sync(TUNWORK, &priv->work); + /* Remove the device from the OS */ netdev_unregister(&priv->dev);