Skip to contentSkip to conversation

tap: do not hold RTNL in open(), release() and queue ioctls

PATCH Applied Eric Dumazet 3 participants 9 messages Nov 19 – Nov 21

Subsystems (3)
lore
Share & tools
lore

Eric Dumazet

author maintainer
#0 lore raw

cover tap: do not hold RTNL in open(), release() and queue ioctls

Opening or closing a macvtap/ipvtap char device acquires RTNL, as do the TUNSETQUEUE and TUNGETIFF ioctls, although they only manipulate the list of queues attached to the device, which is private to tap. Hosts starting and stopping many virtual machines thus add contention on RTNL for no good reason.

This series protects the queue list with a per-device mutex, and stops taking RTNL in these four paths.

Patch 1 makes sure tap_del_queues() is called for every unregistered device, and that no queue can be attached afterwards. Both properties are currently provided by RTNL being held in tap_open().

Patch 2 adds tap->lock and takes it wherever the queue list is used, still under RTNL.

Patch 3 removes RTNL from open(), release(), TUNSETQUEUE and TUNGETIFF.

Patch 4 removes a device reference that is not needed by the ioctls still running under RTNL.

TUNSETOFFLOAD, SIOCGIFHWADDR and SIOCSIFHWADDR still take RTNL, since they call into the core stack. The datapath is unchanged.

Testing: selftests net/tap and net/ipvtap_test.sh pass. A test program (not part of this series) checking the queue limit, attach/detach, txqueuelen change, rename, netns move and device or lower device removal with open queues gives the same results before and after the series. The same program was used to run open/ioctl/read/write/close loops against concurrent link add/del/rename/netns moves for macvtap and ipvtap, on a kernel with lockdep, PROVE_RCU and KASAN enabled, without any splat. No test was done with vhost-net or real VM.

Eric Dumazet (4):
tap: detach queues from the NETDEV_UNREGISTER notifier
tap: add a mutex to protect queue management
tap: do not hold RTNL in open(), release() and queue ioctls
tap: remove tap_get_tap_dev() and tap_put_tap_dev()

 drivers/net/ipvlan/ipvtap.c |  23 ++++--
 drivers/net/macvlan.c       |   3 +-
 drivers/net/macvtap.c       |  24 ++++--
 drivers/net/tap.c           | 184 ++++++++++++++++++++++++++------------------
 include/linux/if_macvlan.h  |   2 +
 include/linux/if_tap.h      |  12 +++
 net/core/dev.c              |   1 +
 7 files changed, 161 insertions(+), 88 deletions(-)

base-commit: 66726ba54a01a2951688a56f5eb4d13d063acbea

Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/4 tap: detach queues from the NETDEV_UNREGISTER notifier

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.

---
 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(-)
drivers/net/ipvlan/ipvtap.c +5 -3
index 66c949d9426103edea10b2580f096f399f139f4f..d17daa1040967a1d80cede5d740d957204fe1186 100644
@@ -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;
drivers/net/macvtap.c +5 -3
index cc975dfb738051cd43d70e6b95d7499400e345aa..3e88ca35cff2f9c755f8aff694e1c4ca83566741 100644
@@ -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;
drivers/net/tap.c +4 -1
index 0f3ad8fd6ab66003093d2aedd4d7e7a6c4f882e9..ded0be1dfdd1e3123acb08e75aa9a874c9e949b5 100644
@@ -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);
 
include/linux/if_tap.h +2 -0
index 553552fa635c3e1e53d1a63c203d32e4c4fd5a4f..e9c4c9272585cdb14cf0ea7ea7d96acc3e8964b7 100644
@@ -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);
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH net-next 1/4] tap: detach queues from the NETDEV_UNREGISTER notifier

Quoted 28 lines · 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().

Quoted 6 lines

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.

author maintainer
#3 lore raw

PATCH 2/4 tap: add a mutex to protect queue management

In reply to 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.

---
 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(-)
drivers/net/ipvlan/ipvtap.c +13 -2
index d17daa1040967a1d80cede5d740d957204fe1186..901697ff2169dce11dc90733dd9375af9b47e24e 100644
@@ -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 = {
drivers/net/macvlan.c +2 -1
index 96ec9bafa525f37d38839ea32d6ee419c2d841a7..c7afa736c472bcfaa7d3065132e633b58968cdd4 100644
@@ -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)
 {
drivers/net/macvtap.c +14 -2
index 3e88ca35cff2f9c755f8aff694e1c4ca83566741..32e6797014d6925745d5e99c8be6f5cf2ddf1832 100644
@@ -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)
drivers/net/tap.c +46 -28

192 lines not on this page · raw message

include/linux/if_macvlan.h +2 -0
index 0f7281e3e448891a78c24b096862a2de790b7687..7a2bfbcfea1f95b01c17d38e30725a5a2f0f6616 100644
@@ -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,
include/linux/if_tap.h +10 -0
index e9c4c9272585cdb14cf0ea7ea7d96acc3e8964b7..f2507fd41b9c9bf37687d17d06a2a9821b934b50 100644
@@ -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. */
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH net-next 2/4] tap: add a mutex to protect queue management

Quoted 26 lines · 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.

Quoted 7 lines

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.

include/linux/if_tap.h struct tap_queue; · line 42

Quoted 13 lines
include/linux/if_tap.h +5 -0
index e9c4c9272585cdb14cf0ea7ea7d96acc3e8964b7..f2507fd41b9c9bf37687d17d06a2a9821b934b50 100644
@@ -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;

It's very helpful that the exact fields guarded by the lock are explicitly listed.

(not a blocker) could this use even more mechanically verifiable instrumentation, such as __guarded_by field annotations and __must_hold for helper functions that access these fields?

It is not possible for every such example, as e.g., lockless READ_ONCE numvtaps.

author maintainer
#5 lore raw

PATCH 3/4 tap: do not hold RTNL in open(), release() and queue ioctls

In reply to 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.

---
 drivers/net/tap.c | 90 +++++++++++++++++++++++++++++++----------------
 net/core/dev.c    |  1 +
 2 files changed, 60 insertions(+), 31 deletions(-)
drivers/net/tap.c +59 -31
index e8250857bce12f140b35f2b0b47f603e53a292d5..a240609d2a8a6b643216135d4b80540c18cf11db 100644
@@ -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))
net/core/dev.c +1 -0
index d88af07aea29004f78a47576ea04a7c5a1c6d411..5efa26fcb207f31d7975272a9fc0f861f5fcc923 100644
@@ -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.
Signature · 1 line
2.53.0
author maintainer
#6 lore raw

PATCH 4/4 tap: remove tap_get_tap_dev() and tap_put_tap_dev()

In reply to 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.

---
 drivers/net/tap.c | 23 ++---------------------
 1 file changed, 2 insertions(+), 21 deletions(-)
drivers/net/tap.c +2 -21
index a240609d2a8a6b643216135d4b80540c18cf11db..2d6af83d97b38e5dd9137c02e5bf002a1d12f678 100644
@@ -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;
 
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 4/4
#7 lore raw

Re: [PATCH net-next 4/4] tap: remove tap_get_tap_dev() and tap_put_tap_dev()

Quoted 9 lines · 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

Iff respinning would be worthwhile to add when the condition that required the dev_hold went away.

Apparently very soon after the introduction in commit 441ac0fcaadc ("macvtap: Convert to using rtnl lock").

Quoted 8 lines

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.

#8 lore raw
In reply to Eric Dumazet
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Thu, 19 Nov 2026 08:22:54 +0100 you wrote:

Opening or closing a macvtap/ipvtap char device acquires RTNL, as do the TUNSETQUEUE and TUNGETIFF ioctls, although they only manipulate the list of queues attached to the device, which is private to tap. Hosts starting and stopping many virtual machines thus add contention on RTNL for no good reason.

This series protects the queue list with a per-device mutex, and stops taking RTNL in these four paths.

[...]

Here is the summary with links:
- [net-next,1/4] tap: detach queues from the NETDEV_UNREGISTER notifier

    https://git.kernel.org/netdev/net-next/c/5a4431ff4fa7
  - [net-next,2/4] tap: add a mutex to protect queue management
    https://git.kernel.org/netdev/net-next/c/c70849e0b16d
  - [net-next,3/4] tap: do not hold RTNL in open(), release() and queue ioctls
    https://git.kernel.org/netdev/net-next/c/ef7b12deec78
  - [net-next,4/4] tap: remove tap_get_tap_dev() and tap_put_tap_dev()
    https://git.kernel.org/netdev/net-next/c/3d5aa58bec4f

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html