From: Eric Dumazet <edumazet@kernel.org>
To: "David S . Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Willem de Bruijn <willemb@google.com>
Cc: Kuniyuki Iwashima <kuniyu@google.com>,
Simon Horman <horms@kernel.org>,
netdev@vger.kernel.org, Eric Dumazet <edumazet@kernel.org>
Subject: [PATCH net-next 2/4] tap: add a mutex to protect queue management
Date: Thu, 19 Nov 2026 08:22:56 +0100 [thread overview]
Message-ID: <20261119072258.3157902-3-edumazet@kernel.org> (raw)
In-Reply-To: <20261119072258.3157902-1-edumazet@kernel.org>
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
next prev parent reply other threads:[~2026-11-19 7:23 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
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 23:46 ` Willem de Bruijn
2026-11-19 7:22 ` Eric Dumazet [this message]
2026-11-19 23:49 ` [PATCH net-next 2/4] tap: add a mutex to protect queue management Willem de Bruijn
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
2026-11-20 0:03 ` Willem de Bruijn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261119072258.3157902-3-edumazet@kernel.org \
--to=edumazet@kernel.org \
--cc=davem@davemloft.net \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.