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 3/4] tap: do not hold RTNL in open(), release() and queue ioctls
Date: Thu, 19 Nov 2026 08:22:57 +0100 [thread overview]
Message-ID: <20261119072258.3157902-4-edumazet@kernel.org> (raw)
In-Reply-To: <20261119072258.3157902-1-edumazet@kernel.org>
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
next prev parent reply other threads:[~2026-11-19 7:23 UTC|newest]
Thread overview: 5+ 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 7:22 ` [PATCH net-next 2/4] tap: add a mutex to protect queue management Eric Dumazet
2026-11-19 7:22 ` Eric Dumazet [this message]
2026-11-19 7:22 ` [PATCH net-next 4/4] tap: remove tap_get_tap_dev() and tap_put_tap_dev() Eric Dumazet
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-4-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox