* [PATCH net-next 1/4] tap: detach queues from the NETDEV_UNREGISTER notifier
2026-11-19 7:22 [PATCH net-next 0/4] tap: do not hold RTNL in open(), release() and queue ioctls Eric Dumazet
@ 2026-11-19 7:22 ` Eric Dumazet
2026-11-19 7:22 ` [PATCH net-next 2/4] tap: add a mutex to protect queue management Eric Dumazet
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Eric Dumazet @ 2026-11-19 7:22 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Paolo Abeni, Willem de Bruijn
Cc: Kuniyuki Iwashima, Simon Horman, netdev, Eric Dumazet
tap_del_queues() is currently called from the macvtap and ipvtap
dellink handlers. Two things in this area only work because
tap_open() runs under RTNL, which a following patch removes:
1) Not all unregistrations go through dellink. If register_netdevice()
fails after the NETDEV_REGISTER notifier created the char device,
or if macvlan_common_newlink() / ipvlan_link_new() fail after
register_netdevice(), the device is unregistered without
tap_del_queues() being called. Nothing can open the char device in
this window today, because RTNL is held from the minor allocation
to its release.
2) tap_del_queues() sets tap->numvtaps to MAX_TAP_QUEUES to "guarantee
that any future tap_set_queue will fail", but since commit
815f236d6227 ("macvtap: add TUNSETQUEUE ioctl") tap_set_queue()
tests tap->numqueues, which is zero at this point. A later
tap_set_queue() would succeed and write to tap->taps[MAX_TAP_QUEUES],
one element past the array. Again, this can not happen today since
the minor is released in the same RTNL section.
Call tap_del_queues() from the NETDEV_UNREGISTER notifier instead, so
that every unregistration path is covered. The notifier is also invoked
when the device moves to another netns, in which case the queues must
stay attached: only act when the device is really being unregistered.
Replace the numvtaps trick with an explicit tap->dead flag, tested in
tap_set_queue().
No functional change intended: none of the above is reachable as long
as tap_open() holds RTNL. These issues were found by code inspection,
with the help of an AI coding assistant, while looking at RTNL removal
in tap. They were not triggered.
Signed-off-by: Eric Dumazet <edumazet@kernel.org>
---
drivers/net/ipvlan/ipvtap.c | 8 +++++---
drivers/net/macvtap.c | 8 +++++---
drivers/net/tap.c | 5 ++++-
include/linux/if_tap.h | 2 ++
4 files changed, 16 insertions(+), 7 deletions(-)
diff --git a/drivers/net/ipvlan/ipvtap.c b/drivers/net/ipvlan/ipvtap.c
index 66c949d9426103edea10b2580f096f399f139f4f..d17daa1040967a1d80cede5d740d957204fe1186 100644
--- a/drivers/net/ipvlan/ipvtap.c
+++ b/drivers/net/ipvlan/ipvtap.c
@@ -112,10 +112,7 @@ static int ipvtap_newlink(struct net_device *dev,
static void __ipvtap_dellink(struct net *net, struct net_device *dev,
struct list_head *head)
{
- struct ipvtap_dev *vlantap = netdev_priv(dev);
-
netdev_rx_handler_unregister(dev);
- tap_del_queues(&vlantap->tap);
__ipvlan_link_delete(net, dev, head);
}
@@ -185,6 +182,11 @@ static int ipvtap_device_event(struct notifier_block *unused,
return notifier_from_errno(err);
break;
case NETDEV_UNREGISTER:
+ /* NETDEV_UNREGISTER is also sent when the device moves to
+ * another netns, queues must survive this.
+ */
+ if (dev->reg_state == NETREG_UNREGISTERING)
+ tap_del_queues(&vlantap->tap);
/* vlan->minor == 0 if NETDEV_REGISTER above failed */
if (vlantap->tap.minor == 0)
break;
diff --git a/drivers/net/macvtap.c b/drivers/net/macvtap.c
index cc975dfb738051cd43d70e6b95d7499400e345aa..3e88ca35cff2f9c755f8aff694e1c4ca83566741 100644
--- a/drivers/net/macvtap.c
+++ b/drivers/net/macvtap.c
@@ -120,10 +120,7 @@ static int macvtap_newlink(struct net_device *dev,
static void macvtap_dellink(struct net_device *dev,
struct list_head *head)
{
- struct macvtap_dev *vlantap = netdev_priv(dev);
-
netdev_rx_handler_unregister(dev);
- tap_del_queues(&vlantap->tap);
macvlan_dellink(dev, head);
}
@@ -186,6 +183,11 @@ static int macvtap_device_event(struct notifier_block *unused,
return notifier_from_errno(err);
break;
case NETDEV_UNREGISTER:
+ /* NETDEV_UNREGISTER is also sent when the device moves to
+ * another netns, queues must survive this.
+ */
+ if (dev->reg_state == NETREG_UNREGISTERING)
+ tap_del_queues(&vlantap->tap);
/* vlan->minor == 0 if NETDEV_REGISTER above failed */
if (vlantap->tap.minor == 0)
break;
diff --git a/drivers/net/tap.c b/drivers/net/tap.c
index 0f3ad8fd6ab66003093d2aedd4d7e7a6c4f882e9..ded0be1dfdd1e3123acb08e75aa9a874c9e949b5 100644
--- a/drivers/net/tap.c
+++ b/drivers/net/tap.c
@@ -102,6 +102,9 @@ static int tap_enable_queue(struct tap_dev *tap, struct file *file,
static int tap_set_queue(struct tap_dev *tap, struct file *file,
struct tap_queue *q)
{
+ if (tap->dead)
+ return -ENODEV;
+
if (tap->numqueues == MAX_TAP_QUEUES)
return -EBUSY;
@@ -248,7 +251,7 @@ void tap_del_queues(struct tap_dev *tap)
BUG_ON(tap->numvtaps);
BUG_ON(tap->numqueues);
/* guarantee that any future tap_set_queue will fail */
- tap->numvtaps = MAX_TAP_QUEUES;
+ tap->dead = true;
}
EXPORT_SYMBOL_GPL(tap_del_queues);
diff --git a/include/linux/if_tap.h b/include/linux/if_tap.h
index 553552fa635c3e1e53d1a63c203d32e4c4fd5a4f..e9c4c9272585cdb14cf0ea7ea7d96acc3e8964b7 100644
--- a/include/linux/if_tap.h
+++ b/include/linux/if_tap.h
@@ -43,6 +43,8 @@ struct tap_dev {
int numqueues;
netdev_features_t tap_features;
int minor;
+ /* Set once the net_device is unregistered, no queue can be added. */
+ bool dead;
void (*update_features)(struct tap_dev *tap, netdev_features_t features);
void (*count_tx_dropped)(struct tap_dev *tap);
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH net-next 2/4] tap: add a mutex to protect queue management
2026-11-19 7:22 [PATCH net-next 0/4] tap: do not hold RTNL in open(), release() and queue ioctls Eric Dumazet
2026-11-19 7:22 ` [PATCH net-next 1/4] tap: detach queues from the NETDEV_UNREGISTER notifier Eric Dumazet
@ 2026-11-19 7:22 ` Eric Dumazet
2026-11-19 7:22 ` [PATCH net-next 3/4] tap: do not hold RTNL in open(), release() and queue ioctls Eric Dumazet
2026-11-19 7:22 ` [PATCH net-next 4/4] tap: remove tap_get_tap_dev() and tap_put_tap_dev() Eric Dumazet
3 siblings, 0 replies; 5+ messages in thread
From: Eric Dumazet @ 2026-11-19 7:22 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Paolo Abeni, Willem de Bruijn
Cc: Kuniyuki Iwashima, Simon Horman, netdev, Eric Dumazet
The list of queues attached to a tap device (tap->taps[],
tap->queue_list, tap->numvtaps, tap->numqueues) and the back pointer
from each queue (q->tap, q->enabled, q->queue_index) are all private
to tap, but are protected by RTNL.
Add tap->lock, a per-device mutex nesting inside RTNL, and hold it in
every function reading or changing this state: tap_open(),
tap_put_queue(), the TUNSETQUEUE ioctl, tap_del_queues() and
tap_queue_resize(). A mutex is needed because tap_open() and
tap_queue_resize() allocate ptr_ring storage with GFP_KERNEL.
Add tap->ring_size, the ptr_ring size shared by all the queues of a
device. It is updated under the mutex when tap_queue_resize() succeeds
and is used by tap_open() instead of dev->tx_queue_len. Once RTNL is
gone, tap_open() can thus not size a new ring from a tx_queue_len
which is being changed, or rolled back by netif_change_tx_queue_len()
after a failure.
tap_handle_frame() reads tap->numvtaps locklessly, add the missing
WRITE_ONCE() annotations while touching these lines.
The mutex is initialized in the macvtap and ipvtap ->setup() handlers
and destroyed from dev->priv_destructor, once all references to the
device are gone. macvtap has to chain to the macvlan destructor,
export macvlan_dev_free() for this purpose.
Set tap->dev before the netdevice is registered instead of after,
since the char device can be opened as soon as the NETDEV_REGISTER
notifier has run. Like the mutex itself this has no effect yet: RTNL
is still held by all callers, and is removed from the paths that do
not need it in the following patch.
Signed-off-by: Eric Dumazet <edumazet@kernel.org>
---
drivers/net/ipvlan/ipvtap.c | 15 +++++++-
drivers/net/macvlan.c | 3 +-
drivers/net/macvtap.c | 16 +++++++-
drivers/net/tap.c | 74 +++++++++++++++++++++++--------------
include/linux/if_macvlan.h | 2 +
include/linux/if_tap.h | 10 +++++
6 files changed, 87 insertions(+), 33 deletions(-)
diff --git a/drivers/net/ipvlan/ipvtap.c b/drivers/net/ipvlan/ipvtap.c
index d17daa1040967a1d80cede5d740d957204fe1186..901697ff2169dce11dc90733dd9375af9b47e24e 100644
--- a/drivers/net/ipvlan/ipvtap.c
+++ b/drivers/net/ipvlan/ipvtap.c
@@ -82,6 +82,8 @@ static int ipvtap_newlink(struct net_device *dev,
int err;
INIT_LIST_HEAD(&vlantap->tap.queue_list);
+ vlantap->tap.dev = dev;
+ vlantap->tap.ring_size = dev->tx_queue_len;
/* Since macvlan supports all offloads by default, make
* tap support all offloads also.
@@ -104,8 +106,6 @@ static int ipvtap_newlink(struct net_device *dev,
return err;
}
- vlantap->tap.dev = vlantap->vlan.dev;
-
return err;
}
@@ -128,11 +128,22 @@ static void ipvtap_dellink(struct net_device *dev,
mutex_unlock(&port->pnodes_lock);
}
+static void ipvtap_dev_free(struct net_device *dev)
+{
+ struct ipvtap_dev *vlantap = netdev_priv(dev);
+
+ mutex_destroy(&vlantap->tap.lock);
+}
+
static void ipvtap_setup(struct net_device *dev)
{
+ struct ipvtap_dev *vlantap = netdev_priv(dev);
+
ipvlan_link_setup(dev);
dev->tx_queue_len = TUN_READQ_SIZE;
dev->priv_flags &= ~IFF_NO_QUEUE;
+ dev->priv_destructor = ipvtap_dev_free;
+ mutex_init(&vlantap->tap.lock);
}
static struct rtnl_link_ops ipvtap_link_ops __read_mostly = {
diff --git a/drivers/net/macvlan.c b/drivers/net/macvlan.c
index 96ec9bafa525f37d38839ea32d6ee419c2d841a7..c7afa736c472bcfaa7d3065132e633b58968cdd4 100644
--- a/drivers/net/macvlan.c
+++ b/drivers/net/macvlan.c
@@ -1230,13 +1230,14 @@ static const struct net_device_ops macvlan_netdev_ops = {
.ndo_hwtstamp_set = macvlan_hwtstamp_set,
};
-static void macvlan_dev_free(struct net_device *dev)
+void macvlan_dev_free(struct net_device *dev)
{
struct macvlan_dev *vlan = netdev_priv(dev);
/* Get rid of the macvlan's reference to lowerdev */
netdev_put(vlan->lowerdev, &vlan->dev_tracker);
}
+EXPORT_SYMBOL_GPL(macvlan_dev_free);
void macvlan_common_setup(struct net_device *dev)
{
diff --git a/drivers/net/macvtap.c b/drivers/net/macvtap.c
index 3e88ca35cff2f9c755f8aff694e1c4ca83566741..32e6797014d6925745d5e99c8be6f5cf2ddf1832 100644
--- a/drivers/net/macvtap.c
+++ b/drivers/net/macvtap.c
@@ -86,6 +86,8 @@ static int macvtap_newlink(struct net_device *dev,
int err;
INIT_LIST_HEAD(&vlantap->tap.queue_list);
+ vlantap->tap.dev = dev;
+ vlantap->tap.ring_size = dev->tx_queue_len;
/* Since macvlan supports all offloads by default, make
* tap support all offloads also.
@@ -112,8 +114,6 @@ static int macvtap_newlink(struct net_device *dev,
return err;
}
- vlantap->tap.dev = vlantap->vlan.dev;
-
return 0;
}
@@ -124,10 +124,22 @@ static void macvtap_dellink(struct net_device *dev,
macvlan_dellink(dev, head);
}
+static void macvtap_dev_free(struct net_device *dev)
+{
+ struct macvtap_dev *vlantap = netdev_priv(dev);
+
+ mutex_destroy(&vlantap->tap.lock);
+ macvlan_dev_free(dev);
+}
+
static void macvtap_setup(struct net_device *dev)
{
+ struct macvtap_dev *vlantap = netdev_priv(dev);
+
macvlan_common_setup(dev);
dev->tx_queue_len = TUN_READQ_SIZE;
+ dev->priv_destructor = macvtap_dev_free;
+ mutex_init(&vlantap->tap.lock);
}
static struct net *macvtap_link_net(const struct net_device *dev)
diff --git a/drivers/net/tap.c b/drivers/net/tap.c
index ded0be1dfdd1e3123acb08e75aa9a874c9e949b5..e8250857bce12f140b35f2b0b47f603e53a292d5 100644
--- a/drivers/net/tap.c
+++ b/drivers/net/tap.c
@@ -65,7 +65,7 @@ static struct tap_dev *tap_dev_get_rcu(const struct net_device *dev)
* RCU usage:
* The tap_queue and the macvlan_dev are loosely coupled, the
* pointers from one to the other can only be read while rcu_read_lock
- * or rtnl is held.
+ * or tap->lock is held.
*
* Both the file and the macvlan_dev hold a reference on the tap_queue
* through sock_hold(&q->sk). When the macvlan_dev goes away first,
@@ -83,7 +83,7 @@ static int tap_enable_queue(struct tap_dev *tap, struct file *file,
{
int err = -EINVAL;
- ASSERT_RTNL();
+ lockdep_assert_held(&tap->lock);
if (q->enabled)
goto out;
@@ -93,15 +93,16 @@ static int tap_enable_queue(struct tap_dev *tap, struct file *file,
q->queue_index = tap->numvtaps;
q->enabled = true;
- tap->numvtaps++;
+ WRITE_ONCE(tap->numvtaps, tap->numvtaps + 1);
out:
return err;
}
-/* Requires RTNL */
static int tap_set_queue(struct tap_dev *tap, struct file *file,
struct tap_queue *q)
{
+ lockdep_assert_held(&tap->lock);
+
if (tap->dead)
return -ENODEV;
@@ -118,35 +119,32 @@ static int tap_set_queue(struct tap_dev *tap, struct file *file,
file->private_data = q;
list_add_tail(&q->next, &tap->queue_list);
- tap->numvtaps++;
+ WRITE_ONCE(tap->numvtaps, tap->numvtaps + 1);
tap->numqueues++;
return 0;
}
-static int tap_disable_queue(struct tap_queue *q)
+static int tap_disable_queue(struct tap_dev *tap, struct tap_queue *q)
{
- struct tap_dev *tap;
struct tap_queue *nq;
+ int index;
- ASSERT_RTNL();
+ lockdep_assert_held(&tap->lock);
if (!q->enabled)
return -EINVAL;
- tap = rtnl_dereference(q->tap);
+ index = q->queue_index;
+ BUG_ON(index >= tap->numvtaps);
+ nq = rcu_dereference_protected(tap->taps[tap->numvtaps - 1],
+ lockdep_is_held(&tap->lock));
+ nq->queue_index = index;
- if (tap) {
- int index = q->queue_index;
- BUG_ON(index >= tap->numvtaps);
- nq = rtnl_dereference(tap->taps[tap->numvtaps - 1]);
- nq->queue_index = index;
+ rcu_assign_pointer(tap->taps[index], nq);
+ RCU_INIT_POINTER(tap->taps[tap->numvtaps - 1], NULL);
+ q->enabled = false;
- rcu_assign_pointer(tap->taps[index], nq);
- RCU_INIT_POINTER(tap->taps[tap->numvtaps - 1], NULL);
- q->enabled = false;
-
- tap->numvtaps--;
- }
+ WRITE_ONCE(tap->numvtaps, tap->numvtaps - 1);
return 0;
}
@@ -156,7 +154,7 @@ static int tap_disable_queue(struct tap_queue *q)
* the reference that the files holds as well as the
* one from the macvlan_dev if that still exists.
*
- * Using the spinlock makes sure that we don't get
+ * Using tap->lock makes sure that we don't get
* to the queue again after destroying it.
*/
static void tap_put_queue(struct tap_queue *q)
@@ -167,13 +165,15 @@ static void tap_put_queue(struct tap_queue *q)
tap = rtnl_dereference(q->tap);
if (tap) {
+ mutex_lock(&tap->lock);
if (q->enabled)
- BUG_ON(tap_disable_queue(q));
+ BUG_ON(tap_disable_queue(tap, q));
tap->numqueues--;
RCU_INIT_POINTER(q->tap, NULL);
sock_put(&q->sk);
list_del_init(&q->next);
+ mutex_unlock(&tap->lock);
}
rtnl_unlock();
@@ -240,11 +240,12 @@ void tap_del_queues(struct tap_dev *tap)
struct tap_queue *q, *tmp;
ASSERT_RTNL();
+ mutex_lock(&tap->lock);
list_for_each_entry_safe(q, tmp, &tap->queue_list, next) {
list_del_init(&q->next);
RCU_INIT_POINTER(q->tap, NULL);
if (q->enabled)
- tap->numvtaps--;
+ WRITE_ONCE(tap->numvtaps, tap->numvtaps - 1);
tap->numqueues--;
sock_put(&q->sk);
}
@@ -252,6 +253,7 @@ void tap_del_queues(struct tap_dev *tap)
BUG_ON(tap->numqueues);
/* guarantee that any future tap_set_queue will fail */
tap->dead = true;
+ mutex_unlock(&tap->lock);
}
EXPORT_SYMBOL_GPL(tap_del_queues);
@@ -464,7 +466,14 @@ static int tap_open(struct inode *inode, struct file *file)
&tap_proto, 0);
if (!q)
goto err;
- if (ptr_ring_init(&q->ring, tap->dev->tx_queue_len, GFP_KERNEL)) {
+
+ /* Use tap->ring_size instead of dev->tx_queue_len, which might be
+ * changing: a concurrent tap_queue_resize() either sees this queue
+ * or has already updated tap->ring_size.
+ */
+ mutex_lock(&tap->lock);
+ if (ptr_ring_init(&q->ring, tap->ring_size, GFP_KERNEL)) {
+ mutex_unlock(&tap->lock);
sk_free(&q->sk);
goto err;
}
@@ -491,6 +500,7 @@ static int tap_open(struct inode *inode, struct file *file)
sock_set_flag(&q->sk, SOCK_ZEROCOPY);
err = tap_set_queue(tap, file, q);
+ mutex_unlock(&tap->lock);
if (err) {
/* tap_sock_destruct() will take care of freeing ptr_ring */
goto err_put;
@@ -848,12 +858,14 @@ static int tap_ioctl_set_queue(struct file *file, unsigned int flags)
if (!tap)
return -EINVAL;
+ mutex_lock(&tap->lock);
if (flags & IFF_ATTACH_QUEUE)
ret = tap_enable_queue(tap, file, q);
else if (flags & IFF_DETACH_QUEUE)
- ret = tap_disable_queue(q);
+ ret = tap_disable_queue(tap, q);
else
ret = -EINVAL;
+ mutex_unlock(&tap->lock);
tap_put_tap_dev(tap);
return ret;
@@ -1207,12 +1219,14 @@ int tap_queue_resize(struct tap_dev *tap)
struct net_device *dev = tap->dev;
struct tap_queue *q;
struct ptr_ring **rings;
- int n = tap->numqueues;
- int ret, i = 0;
+ int ret = -ENOMEM;
+ int n, i = 0;
+ mutex_lock(&tap->lock);
+ n = tap->numqueues;
rings = kmalloc_objs(*rings, n);
if (!rings)
- return -ENOMEM;
+ goto unlock;
list_for_each_entry(q, &tap->queue_list, next)
rings[i++] = &q->ring;
@@ -1220,8 +1234,12 @@ int tap_queue_resize(struct tap_dev *tap)
ret = ptr_ring_resize_multiple_bh(rings, n,
dev->tx_queue_len, GFP_KERNEL,
__skb_array_destroy_skb);
+ if (!ret)
+ tap->ring_size = dev->tx_queue_len;
kfree(rings);
+unlock:
+ mutex_unlock(&tap->lock);
return ret;
}
EXPORT_SYMBOL_GPL(tap_queue_resize);
diff --git a/include/linux/if_macvlan.h b/include/linux/if_macvlan.h
index 0f7281e3e448891a78c24b096862a2de790b7687..7a2bfbcfea1f95b01c17d38e30725a5a2f0f6616 100644
--- a/include/linux/if_macvlan.h
+++ b/include/linux/if_macvlan.h
@@ -59,6 +59,8 @@ static inline void macvlan_count_rx(const struct macvlan_dev *vlan,
extern void macvlan_common_setup(struct net_device *dev);
+void macvlan_dev_free(struct net_device *dev);
+
struct rtnl_newlink_params;
extern int macvlan_common_newlink(struct net_device *dev,
diff --git a/include/linux/if_tap.h b/include/linux/if_tap.h
index e9c4c9272585cdb14cf0ea7ea7d96acc3e8964b7..f2507fd41b9c9bf37687d17d06a2a9821b934b50 100644
--- a/include/linux/if_tap.h
+++ b/include/linux/if_tap.h
@@ -35,12 +35,22 @@ struct tap_queue;
struct tap_dev {
struct net_device *dev;
u16 flags;
+ /* Protects taps[], queue_list, numvtaps, numqueues, ring_size, dead,
+ * and the tap, enabled and queue_index fields of the attached queues.
+ * Nests inside RTNL.
+ */
+ struct mutex lock;
/* This array tracks active taps. */
struct tap_queue __rcu *taps[MAX_TAP_QUEUES];
/* This list tracks all taps (both enabled and disabled) */
struct list_head queue_list;
int numvtaps;
int numqueues;
+ /* Size of the ptr_ring of all queues, as last set by
+ * tap_queue_resize(). Can differ from dev->tx_queue_len
+ * if a change of the latter failed after our notifier ran.
+ */
+ unsigned int ring_size;
netdev_features_t tap_features;
int minor;
/* Set once the net_device is unregistered, no queue can be added. */
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH net-next 3/4] tap: do not hold RTNL in open(), release() and queue ioctls
2026-11-19 7:22 [PATCH net-next 0/4] tap: do not hold RTNL in open(), release() and queue ioctls Eric Dumazet
2026-11-19 7:22 ` [PATCH net-next 1/4] tap: detach queues from the NETDEV_UNREGISTER notifier Eric Dumazet
2026-11-19 7:22 ` [PATCH net-next 2/4] tap: add a mutex to protect queue management Eric Dumazet
@ 2026-11-19 7:22 ` Eric Dumazet
2026-11-19 7:22 ` [PATCH net-next 4/4] tap: remove tap_get_tap_dev() and tap_put_tap_dev() Eric Dumazet
3 siblings, 0 replies; 5+ messages in thread
From: Eric Dumazet @ 2026-11-19 7:22 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Paolo Abeni, Willem de Bruijn
Cc: Kuniyuki Iwashima, Simon Horman, netdev, Eric Dumazet
Opening and closing a macvtap or ipvtap char device, as well as the
TUNSETQUEUE and TUNGETIFF ioctls, only touch state now protected by
tap->lock. They nevertheless acquire RTNL, so starting or stopping
virtual machines contends with every other networking configuration
change on the host.
Stop taking RTNL in these four paths.
Without RTNL, the tap_dev a queue is attached to is found with
tap_queue_lock_dev(): q->tap is read under rcu_read_lock() and a
reference on tap->dev is taken, which is safe because q->tap is
cleared before the synchronize_net() done while the device is
unregistered. tap->lock, which lives in the netdev private area, can
then be acquired and q->tap checked again.
tap_open() relies on the two previous patches: a queue can no longer
be added once tap_del_queues() has run, and tap_del_queues() runs for
every unregistered device.
TUNGETIFF uses netdev_copy_name() to get a consistent device name,
export it for modular builds.
tap_open() now reads dev->features once, with READ_ONCE(), since it
can change while the SOCK_ZEROCOPY decision is made.
TUNSETOFFLOAD, SIOCGIFHWADDR and SIOCSIFHWADDR call into the core
stack (netdev_update_features(), dev_set_mac_address_user(), device
lookup by name) and still hold RTNL. They keep using
rtnl_dereference(q->tap), which remains valid because tap_del_queues()
is only called under RTNL.
Signed-off-by: Eric Dumazet <edumazet@kernel.org>
---
drivers/net/tap.c | 90 +++++++++++++++++++++++++++++++----------------
net/core/dev.c | 1 +
2 files changed, 60 insertions(+), 31 deletions(-)
diff --git a/drivers/net/tap.c b/drivers/net/tap.c
index e8250857bce12f140b35f2b0b47f603e53a292d5..a240609d2a8a6b643216135d4b80540c18cf11db 100644
--- a/drivers/net/tap.c
+++ b/drivers/net/tap.c
@@ -76,7 +76,48 @@ static struct tap_dev *tap_dev_get_rcu(const struct net_device *dev)
* queue from outbound SKBs, but these never reference back to the
* file or the dev. The data structure is freed through __sk_free
* when both our references and any pending SKBs are gone.
+ *
+ * Locking:
+ * tap->lock protects the queues attached to a tap_dev, see the comment
+ * in struct tap_dev. RTNL is only needed by the ioctls calling into
+ * the core networking stack. They can use rtnl_dereference(q->tap),
+ * because tap_del_queues() is called with RTNL held and the only other
+ * place clearing q->tap is the release of the file they operate on.
+ */
+
+/* Returns the tap_dev @q is attached to, with tap->lock held and a
+ * reference on tap->dev keeping the mutex alive, or NULL if the queue
+ * is detached.
*/
+static struct tap_dev *tap_queue_lock_dev(struct tap_queue *q)
+{
+ struct tap_dev *tap;
+
+ /* q->tap is cleared before the synchronize_net() calls done at
+ * device unregistration, tap->dev can not be freed under us.
+ */
+ rcu_read_lock();
+ tap = rcu_dereference(q->tap);
+ if (tap)
+ dev_hold(tap->dev);
+ rcu_read_unlock();
+ if (!tap)
+ return NULL;
+
+ mutex_lock(&tap->lock);
+ if (unlikely(rcu_access_pointer(q->tap) != tap)) {
+ mutex_unlock(&tap->lock);
+ dev_put(tap->dev);
+ return NULL;
+ }
+ return tap;
+}
+
+static void tap_queue_unlock_dev(struct tap_dev *tap)
+{
+ mutex_unlock(&tap->lock);
+ dev_put(tap->dev);
+}
static int tap_enable_queue(struct tap_dev *tap, struct file *file,
struct tap_queue *q)
@@ -159,13 +200,9 @@ static int tap_disable_queue(struct tap_dev *tap, struct tap_queue *q)
*/
static void tap_put_queue(struct tap_queue *q)
{
- struct tap_dev *tap;
-
- rtnl_lock();
- tap = rtnl_dereference(q->tap);
+ struct tap_dev *tap = tap_queue_lock_dev(q);
if (tap) {
- mutex_lock(&tap->lock);
if (q->enabled)
BUG_ON(tap_disable_queue(tap, q));
@@ -173,11 +210,9 @@ static void tap_put_queue(struct tap_queue *q)
RCU_INIT_POINTER(q->tap, NULL);
sock_put(&q->sk);
list_del_init(&q->next);
- mutex_unlock(&tap->lock);
+ tap_queue_unlock_dev(tap);
}
- rtnl_unlock();
-
synchronize_rcu();
sock_put(&q->sk);
}
@@ -239,6 +274,7 @@ void tap_del_queues(struct tap_dev *tap)
{
struct tap_queue *q, *tmp;
+ /* The ioctls using rtnl_dereference(q->tap) depend on this. */
ASSERT_RTNL();
mutex_lock(&tap->lock);
list_for_each_entry_safe(q, tmp, &tap->queue_list, next) {
@@ -452,11 +488,11 @@ static void tap_sock_destruct(struct sock *sk)
static int tap_open(struct inode *inode, struct file *file)
{
struct net *net = current->nsproxy->net_ns;
+ netdev_features_t features;
struct tap_dev *tap;
struct tap_queue *q;
int err = -ENODEV;
- rtnl_lock();
tap = dev_get_by_tap_file(imajor(inode), iminor(inode));
if (!tap)
goto err;
@@ -496,7 +532,8 @@ static int tap_open(struct inode *inode, struct file *file)
* The macvlan supports zerocopy iff the lower device supports zero
* copy so we don't have to look at the lower device directly.
*/
- if ((tap->dev->features & NETIF_F_HIGHDMA) && (tap->dev->features & NETIF_F_SG))
+ features = READ_ONCE(tap->dev->features);
+ if ((features & NETIF_F_HIGHDMA) && (features & NETIF_F_SG))
sock_set_flag(&q->sk, SOCK_ZEROCOPY);
err = tap_set_queue(tap, file, q);
@@ -511,7 +548,6 @@ static int tap_open(struct inode *inode, struct file *file)
dev_put(tap->dev);
- rtnl_unlock();
return err;
err_put:
@@ -520,7 +556,6 @@ static int tap_open(struct inode *inode, struct file *file)
if (tap)
dev_put(tap->dev);
- rtnl_unlock();
return err;
}
@@ -854,20 +889,18 @@ static int tap_ioctl_set_queue(struct file *file, unsigned int flags)
struct tap_dev *tap;
int ret;
- tap = tap_get_tap_dev(q);
+ tap = tap_queue_lock_dev(q);
if (!tap)
return -EINVAL;
- mutex_lock(&tap->lock);
if (flags & IFF_ATTACH_QUEUE)
ret = tap_enable_queue(tap, file, q);
else if (flags & IFF_DETACH_QUEUE)
ret = tap_disable_queue(tap, q);
else
ret = -EINVAL;
- mutex_unlock(&tap->lock);
- tap_put_tap_dev(tap);
+ tap_queue_unlock_dev(tap);
return ret;
}
@@ -935,6 +968,7 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
struct sockaddr_storage ss = {};
struct ifreq __user *ifr = argp;
unsigned int __user *up = argp;
+ char name[IFNAMSIZ] = {};
unsigned short u;
int __user *sp = argp;
int s;
@@ -958,31 +992,25 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
return ret;
case TUNGETIFF:
- rtnl_lock();
- tap = tap_get_tap_dev(q);
- if (!tap) {
- rtnl_unlock();
+ tap = tap_queue_lock_dev(q);
+ if (!tap)
return -ENOLINK;
- }
- ret = 0;
u = q->flags;
if (!q->enabled)
u |= IFF_DETACH_QUEUE;
- if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
+ netdev_copy_name(tap->dev, name);
+ tap_queue_unlock_dev(tap);
+
+ if (copy_to_user(&ifr->ifr_name, name, IFNAMSIZ) ||
put_user(u, &ifr->ifr_flags))
- ret = -EFAULT;
- tap_put_tap_dev(tap);
- rtnl_unlock();
- return ret;
+ return -EFAULT;
+ return 0;
case TUNSETQUEUE:
if (get_user(u, &ifr->ifr_flags))
return -EFAULT;
- rtnl_lock();
- ret = tap_ioctl_set_queue(file, u);
- rtnl_unlock();
- return ret;
+ return tap_ioctl_set_queue(file, u);
case TUNGETFEATURES:
if (put_user(IFF_TAP | IFF_NO_PI | TAP_IFFEATURES, up))
diff --git a/net/core/dev.c b/net/core/dev.c
index d88af07aea29004f78a47576ea04a7c5a1c6d411..5efa26fcb207f31d7975272a9fc0f861f5fcc923 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -1244,6 +1244,7 @@ void netdev_copy_name(struct net_device *dev, char *name)
strscpy(name, dev->name, IFNAMSIZ);
} while (read_seqretry(&netdev_rename_lock, seq));
}
+EXPORT_SYMBOL_GPL(netdev_copy_name);
/**
* netdev_get_name - get a netdevice name, knowing its ifindex.
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH net-next 4/4] tap: remove tap_get_tap_dev() and tap_put_tap_dev()
2026-11-19 7:22 [PATCH net-next 0/4] tap: do not hold RTNL in open(), release() and queue ioctls Eric Dumazet
` (2 preceding siblings ...)
2026-11-19 7:22 ` [PATCH net-next 3/4] tap: do not hold RTNL in open(), release() and queue ioctls Eric Dumazet
@ 2026-11-19 7:22 ` Eric Dumazet
3 siblings, 0 replies; 5+ messages in thread
From: Eric Dumazet @ 2026-11-19 7:22 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Paolo Abeni, Willem de Bruijn
Cc: Kuniyuki Iwashima, Simon Horman, netdev, Eric Dumazet
tap_get_tap_dev() takes a reference on tap->dev, released by
tap_put_tap_dev().
This comes from commit 8f475a318aaa ("macvtap: introduce
macvtap_get_vlan()"), when q->vlan was read under rcu_read_lock_bh()
and the reference was needed to use the device after
rcu_read_unlock_bh().
Both remaining callers, SIOCGIFHWADDR and SIOCSIFHWADDR, hold RTNL
for the whole time the device is used. tap_del_queues() clears q->tap
under RTNL, before the device can be freed, so a non NULL q->tap
guarantees the device stays around until rtnl_unlock(). TUNSETOFFLOAD
already relies on this.
Use rtnl_dereference(q->tap) directly.
Signed-off-by: Eric Dumazet <edumazet@kernel.org>
---
drivers/net/tap.c | 23 ++---------------------
1 file changed, 2 insertions(+), 21 deletions(-)
diff --git a/drivers/net/tap.c b/drivers/net/tap.c
index a240609d2a8a6b643216135d4b80540c18cf11db..2d6af83d97b38e5dd9137c02e5bf002a1d12f678 100644
--- a/drivers/net/tap.c
+++ b/drivers/net/tap.c
@@ -866,23 +866,6 @@ static ssize_t tap_read_iter(struct kiocb *iocb, struct iov_iter *to)
return ret;
}
-static struct tap_dev *tap_get_tap_dev(struct tap_queue *q)
-{
- struct tap_dev *tap;
-
- ASSERT_RTNL();
- tap = rtnl_dereference(q->tap);
- if (tap)
- dev_hold(tap->dev);
-
- return tap;
-}
-
-static void tap_put_tap_dev(struct tap_dev *tap)
-{
- dev_put(tap->dev);
-}
-
static int tap_ioctl_set_queue(struct file *file, unsigned int flags)
{
struct tap_queue *q = file->private_data;
@@ -1040,7 +1023,7 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
case SIOCGIFHWADDR:
rtnl_lock();
- tap = tap_get_tap_dev(q);
+ tap = rtnl_dereference(q->tap);
if (!tap) {
rtnl_unlock();
return -ENOLINK;
@@ -1051,7 +1034,6 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
copy_to_user(&ifr->ifr_hwaddr, &ss, sizeof(ifr->ifr_hwaddr)))
ret = -EFAULT;
- tap_put_tap_dev(tap);
rtnl_unlock();
return ret;
@@ -1059,7 +1041,7 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
if (copy_from_user(&ss, &ifr->ifr_hwaddr, sizeof(ifr->ifr_hwaddr)))
return -EFAULT;
rtnl_lock();
- tap = tap_get_tap_dev(q);
+ tap = rtnl_dereference(q->tap);
if (!tap) {
rtnl_unlock();
return -ENOLINK;
@@ -1068,7 +1050,6 @@ static long tap_ioctl(struct file *file, unsigned int cmd,
ret = -EINVAL;
else
ret = dev_set_mac_address_user(tap->dev, &ss, NULL);
- tap_put_tap_dev(tap);
rtnl_unlock();
return ret;
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread