mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: SeungJong Ha <engineer.jjhama@gmail.com>
To: "David S. Miller" <davem@davemloft.net>,
	 Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>,
	 Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	 Simon Horman <horms@kernel.org>,
	Jamal Hadi Salim <jhs@mojatatu.com>,
	 Jiri Pirko <jiri@resnulli.us>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	 Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
	 Jason Wang <jasowangio@gmail.com>
Cc: "Michael S. Tsirkin" <mst@redhat.com>,
	 John Fastabend <john.fastabend@gmail.com>,
	linux-kernel@vger.kernel.org,  netdev@vger.kernel.org,
	SeungJong Ha <engineer.jjhama@gmail.com>
Subject: [PATCH RFC net-next 4/5] net: resize tun and tap rings from rtnl_link_ops
Date: Sun, 06 Dec 2026 23:44:45 +0900	[thread overview]
Message-ID: <20261206-tc1-txqlen-commit-v1-4-ec9f34dce973@gmail.com> (raw)
In-Reply-To: <20261206-tc1-txqlen-commit-v1-0-ec9f34dce973@gmail.com>

tun, macvtap and ipvtap resize their rings when they get
NETDEV_CHANGE_TX_QUEUE_LEN, and refuse the change if the resize fails.
This has two problems:

- The event goes to every listener, but each of these handlers
  returns early unless the device is its own. The notifier is only
  used as a way to call the driver of the device.
- rtnetlink listens to the same event and runs before them. When
  the driver fails, RTM_NEWLINK has already announced the length
  that is then refused.

Add rtnl_link_ops->change_tx_queue_len() and call it from
netif_change_tx_queue_len() before the notifier. The three drivers
move their resize there. A ndo would not do: macvtap and ipvtap use
the net_device_ops of macvlan and ipvlan.

The rings are still resized after dev->tx_queue_len is set and
before the qdiscs are. When the resize fails, userspace sees two
changes: no RTM_NEWLINK with the refused length is sent, and the
error is -ENOMEM instead of the -EPERM that NOTIFY_BAD turned into.

The patch was written with Claude Code (Claude Opus 5.5).

Signed-off-by: SeungJong Ha <engineer.jjhama@gmail.com>
---
 drivers/net/ipvlan/ipvtap.c | 13 +++++---
 drivers/net/macvtap.c       | 13 +++++---
 drivers/net/tap.c           |  7 ++---
 drivers/net/tun.c           | 73 ++++++++++++++++++++++-----------------------
 include/linux/if_tap.h      |  4 +--
 include/net/rtnetlink.h     |  5 ++++
 net/core/dev.c              |  6 ++++
 7 files changed, 69 insertions(+), 52 deletions(-)

diff --git a/drivers/net/ipvlan/ipvtap.c b/drivers/net/ipvlan/ipvtap.c
index 901697ff2169..41b538ff26f1 100644
--- a/drivers/net/ipvlan/ipvtap.c
+++ b/drivers/net/ipvlan/ipvtap.c
@@ -146,11 +146,20 @@ static void ipvtap_setup(struct net_device *dev)
 	mutex_init(&vlantap->tap.lock);
 }
 
+static int ipvtap_change_tx_queue_len(struct net_device *dev,
+				      unsigned int new_len)
+{
+	struct ipvtap_dev *vlantap = netdev_priv(dev);
+
+	return tap_queue_resize(&vlantap->tap, new_len);
+}
+
 static struct rtnl_link_ops ipvtap_link_ops __read_mostly = {
 	.kind		= "ipvtap",
 	.setup		= ipvtap_setup,
 	.newlink	= ipvtap_newlink,
 	.dellink	= ipvtap_dellink,
+	.change_tx_queue_len = ipvtap_change_tx_queue_len,
 	.priv_size	= sizeof(struct ipvtap_dev),
 };
 
@@ -206,10 +215,6 @@ static int ipvtap_device_event(struct notifier_block *unused,
 		device_destroy(&ipvtap_class, devt);
 		tap_free_minor(ipvtap_major, &vlantap->tap);
 		break;
-	case NETDEV_CHANGE_TX_QUEUE_LEN:
-		if (tap_queue_resize(&vlantap->tap))
-			return NOTIFY_BAD;
-		break;
 	}
 
 	return NOTIFY_DONE;
diff --git a/drivers/net/macvtap.c b/drivers/net/macvtap.c
index 32e6797014d6..0f851edfa455 100644
--- a/drivers/net/macvtap.c
+++ b/drivers/net/macvtap.c
@@ -147,12 +147,21 @@ static struct net *macvtap_link_net(const struct net_device *dev)
 	return dev_net(macvlan_dev_real_dev(dev));
 }
 
+static int macvtap_change_tx_queue_len(struct net_device *dev,
+				       unsigned int new_len)
+{
+	struct macvtap_dev *vlantap = netdev_priv(dev);
+
+	return tap_queue_resize(&vlantap->tap, new_len);
+}
+
 static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
 	.kind		= "macvtap",
 	.setup		= macvtap_setup,
 	.newlink	= macvtap_newlink,
 	.dellink	= macvtap_dellink,
 	.get_link_net	= macvtap_link_net,
+	.change_tx_queue_len = macvtap_change_tx_queue_len,
 	.priv_size      = sizeof(struct macvtap_dev),
 };
 
@@ -208,10 +217,6 @@ static int macvtap_device_event(struct notifier_block *unused,
 		device_destroy(&macvtap_class, devt);
 		tap_free_minor(macvtap_major, &vlantap->tap);
 		break;
-	case NETDEV_CHANGE_TX_QUEUE_LEN:
-		if (tap_queue_resize(&vlantap->tap))
-			return NOTIFY_BAD;
-		break;
 	}
 
 	return NOTIFY_DONE;
diff --git a/drivers/net/tap.c b/drivers/net/tap.c
index 3028bf90c6b4..be82458ae7da 100644
--- a/drivers/net/tap.c
+++ b/drivers/net/tap.c
@@ -1227,9 +1227,8 @@ struct ptr_ring *tap_get_ptr_ring(struct file *file)
 }
 EXPORT_SYMBOL_GPL(tap_get_ptr_ring);
 
-int tap_queue_resize(struct tap_dev *tap)
+int tap_queue_resize(struct tap_dev *tap, unsigned int new_len)
 {
-	struct net_device *dev = tap->dev;
 	struct tap_queue *q;
 	struct ptr_ring **rings;
 	int ret = -ENOMEM;
@@ -1245,10 +1244,10 @@ int tap_queue_resize(struct tap_dev *tap)
 		rings[i++] = &q->ring;
 
 	ret = ptr_ring_resize_multiple_bh(rings, n,
-					  dev->tx_queue_len, GFP_KERNEL,
+					  new_len, GFP_KERNEL,
 					  __skb_array_destroy_skb);
 	if (!ret)
-		tap->ring_size = dev->tx_queue_len;
+		tap->ring_size = new_len;
 
 	kfree(rings);
 unlock:
diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index a644fab4321f..a4d419cfbf16 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -2544,6 +2544,40 @@ static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev)
 	return -EMSGSIZE;
 }
 
+static int tun_queue_resize(struct net_device *dev, unsigned int new_len)
+{
+	struct tun_struct *tun = netdev_priv(dev);
+	struct tun_file *tfile;
+	struct ptr_ring **rings;
+	int n = tun->numqueues + tun->numdisabled;
+	int ret, i;
+
+	rings = kmalloc_objs(*rings, n);
+	if (!rings)
+		return -ENOMEM;
+
+	for (i = 0; i < tun->numqueues; i++) {
+		tfile = rtnl_dereference(tun->tfiles[i]);
+		rings[i] = &tfile->tx_ring;
+	}
+	list_for_each_entry(tfile, &tun->disabled, next)
+		rings[i++] = &tfile->tx_ring;
+
+	ret = ptr_ring_resize_multiple_bh(rings, n,
+					  new_len, GFP_KERNEL,
+					  tun_ptr_free);
+
+	if (!ret) {
+		for (i = 0; i < tun->numqueues; i++) {
+			tfile = rtnl_dereference(tun->tfiles[i]);
+			tun_force_wake_queue(tun, tfile);
+		}
+	}
+
+	kfree(rings);
+	return ret;
+}
+
 static struct rtnl_link_ops tun_link_ops __read_mostly = {
 	.kind		= DRV_NAME,
 	.priv_size	= sizeof(struct tun_struct),
@@ -2551,6 +2585,7 @@ static struct rtnl_link_ops tun_link_ops __read_mostly = {
 	.validate	= tun_validate,
 	.get_size       = tun_get_size,
 	.fill_info      = tun_fill_info,
+	.change_tx_queue_len = tun_queue_resize,
 };
 
 static void tun_sock_write_space(struct sock *sk)
@@ -3844,40 +3879,6 @@ static const struct ethtool_ops tun_ethtool_ops = {
 	.set_link_ksettings = tun_set_link_ksettings,
 };
 
-static int tun_queue_resize(struct tun_struct *tun)
-{
-	struct net_device *dev = tun->dev;
-	struct tun_file *tfile;
-	struct ptr_ring **rings;
-	int n = tun->numqueues + tun->numdisabled;
-	int ret, i;
-
-	rings = kmalloc_objs(*rings, n);
-	if (!rings)
-		return -ENOMEM;
-
-	for (i = 0; i < tun->numqueues; i++) {
-		tfile = rtnl_dereference(tun->tfiles[i]);
-		rings[i] = &tfile->tx_ring;
-	}
-	list_for_each_entry(tfile, &tun->disabled, next)
-		rings[i++] = &tfile->tx_ring;
-
-	ret = ptr_ring_resize_multiple_bh(rings, n,
-					  dev->tx_queue_len, GFP_KERNEL,
-					  tun_ptr_free);
-
-	if (!ret) {
-		for (i = 0; i < tun->numqueues; i++) {
-			tfile = rtnl_dereference(tun->tfiles[i]);
-			tun_force_wake_queue(tun, tfile);
-		}
-	}
-
-	kfree(rings);
-	return ret;
-}
-
 static int tun_device_event(struct notifier_block *unused,
 			    unsigned long event, void *ptr)
 {
@@ -3889,10 +3890,6 @@ static int tun_device_event(struct notifier_block *unused,
 		return NOTIFY_DONE;
 
 	switch (event) {
-	case NETDEV_CHANGE_TX_QUEUE_LEN:
-		if (tun_queue_resize(tun))
-			return NOTIFY_BAD;
-		break;
 	case NETDEV_UP:
 		for (i = 0; i < tun->numqueues; i++) {
 			struct tun_file *tfile;
diff --git a/include/linux/if_tap.h b/include/linux/if_tap.h
index f2507fd41b9c..5b8d59e19c1c 100644
--- a/include/linux/if_tap.h
+++ b/include/linux/if_tap.h
@@ -48,7 +48,7 @@ struct tap_dev {
 	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.
+	 * if a change of the latter failed after tap_queue_resize() ran.
 	 */
 	unsigned int		ring_size;
 	netdev_features_t	tap_features;
@@ -89,7 +89,7 @@ rx_handler_result_t tap_handle_frame(struct sk_buff **pskb);
 void tap_del_queues(struct tap_dev *tap);
 int tap_get_minor(dev_t major, struct tap_dev *tap);
 void tap_free_minor(dev_t major, struct tap_dev *tap);
-int tap_queue_resize(struct tap_dev *tap);
+int tap_queue_resize(struct tap_dev *tap, unsigned int new_len);
 int tap_create_cdev(struct cdev *tap_cdev, dev_t *tap_major,
 		    const char *device_name, struct module *module);
 void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev);
diff --git a/include/net/rtnetlink.h b/include/net/rtnetlink.h
index 2bff41aacc98..1a4e001df7a9 100644
--- a/include/net/rtnetlink.h
+++ b/include/net/rtnetlink.h
@@ -132,6 +132,9 @@ static inline struct net *rtnl_newlink_peer_net(struct rtnl_newlink_params *p)
  *			    to create when creating a new device.
  *	@get_num_rx_queues: Function to determine number of receive queues
  *			    to create when creating a new device.
+ *	@change_tx_queue_len: Function to resize device rings to @new_len
+ *			      when dev->tx_queue_len changes. Called under
+ *			      RTNL, may sleep. Must change all of them or none.
  *	@get_link_net: Function to get the i/o netns of the device
  *	@get_linkxstats_size: Function to calculate the required room for
  *			      dumping device-specific extended link stats
@@ -178,6 +181,8 @@ struct rtnl_link_ops {
 					       const struct net_device *dev);
 	unsigned int		(*get_num_tx_queues)(void);
 	unsigned int		(*get_num_rx_queues)(void);
+	int			(*change_tx_queue_len)(struct net_device *dev,
+						       unsigned int new_len);
 
 	unsigned int		slave_maxtype;
 	const struct nla_policy	*slave_policy;
diff --git a/net/core/dev.c b/net/core/dev.c
index b7757f91fdf9..9a9bfc4815e6 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -10261,6 +10261,7 @@ EXPORT_SYMBOL(netif_set_mtu);
 
 int netif_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
 {
+	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
 	unsigned int orig_len = dev->tx_queue_len;
 	int res;
 
@@ -10269,6 +10270,11 @@ int netif_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
 
 	if (new_len != orig_len) {
 		WRITE_ONCE(dev->tx_queue_len, new_len);
+		if (ops && ops->change_tx_queue_len) {
+			res = ops->change_tx_queue_len(dev, new_len);
+			if (res)
+				goto err_rollback;
+		}
 		res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
 		res = notifier_to_errno(res);
 		if (res)

-- 
2.54.0


  parent reply	other threads:[~2026-12-06 14:45 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06 14:44 [PATCH RFC net-next 0/5] net: make tx_queue_len changes all-or-nothing SeungJong Ha
2026-12-06 14:44 ` [PATCH RFC net-next 1/5] ptr_ring: drop the unused gfp argument of __ptr_ring_swap_queue() SeungJong Ha
2026-12-06 14:44 ` [PATCH RFC net-next 2/5] ptr_ring: split ptr_ring_resize_multiple_bh() into alloc and swap SeungJong Ha
2026-12-06 14:44 ` [PATCH RFC net-next 3/5] net_sched: change tx_queue_len on all tx queues or on none SeungJong Ha
2026-12-06 14:44 ` SeungJong Ha [this message]
2026-12-06 14:44 ` [PATCH RFC net-next 5/5] net: change tx_queue_len only after all allocations succeeded SeungJong Ha

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=20261206-tc1-txqlen-commit-v1-4-ec9f34dce973@gmail.com \
    --to=engineer.jjhama@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=jasowangio@gmail.com \
    --cc=jhs@mojatatu.com \
    --cc=jiri@resnulli.us \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.com \
    --cc=willemdebruijn.kernel@gmail.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

all inboxes | Powered by JetHome®