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 5/5] net: change tx_queue_len only after all allocations succeeded
Date: Sun, 06 Dec 2026 23:44:46 +0900 [thread overview]
Message-ID: <20261206-tc1-txqlen-commit-v1-5-ec9f34dce973@gmail.com> (raw)
In-Reply-To: <20261206-tc1-txqlen-commit-v1-0-ec9f34dce973@gmail.com>
netif_change_tx_queue_len() sets the new length, resizes the device
rings, notifies and then resizes the qdiscs. If the qdiscs fail, it
restores dev->tx_queue_len, but the device rings keep the new length
and RTM_NEWLINK has already reported it. Resizing the rings back on
the way out does not help, as that would need to allocate again.
Do it in this order instead:
prepare the qdiscs may fail, leaves nothing behind
resize the device rings may fail, changes all or none
set the new length
swap the qdisc rings in
notify
Whichever step fails, nothing has been changed yet, so there is
nothing to revert.
No in-tree listener of NETDEV_CHANGE_TX_QUEUE_LEN fails any more.
Its return value is now ignored, as for NETDEV_UP and
NETDEV_FEAT_CHANGE.
Found with a prototype static analysis tool I am developing. The
analysis, the reproducer and the fix were written with Claude Code
(Claude Opus 5.5).
Signed-off-by: SeungJong Ha <engineer.jjhama@gmail.com>
---
include/linux/if_tap.h | 2 +-
include/net/sch_generic.h | 5 ++++-
net/core/dev.c | 22 +++++++++++-----------
net/sched/sch_generic.c | 19 ++++---------------
4 files changed, 20 insertions(+), 28 deletions(-)
diff --git a/include/linux/if_tap.h b/include/linux/if_tap.h
index 5b8d59e19c1c..2a8b1084122f 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 tap_queue_resize() ran.
+ * while the latter is being changed.
*/
unsigned int ring_size;
netdev_features_t tap_features;
diff --git a/include/net/sch_generic.h b/include/net/sch_generic.h
index 8e0e774844c2..e406b32dfab9 100644
--- a/include/net/sch_generic.h
+++ b/include/net/sch_generic.h
@@ -723,7 +723,10 @@ void qdisc_class_hash_remove(struct Qdisc_class_hash *,
void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
-int dev_qdisc_change_tx_queue_len(struct net_device *dev);
+int dev_qdisc_prepare_tx_queue_len(struct net_device *dev,
+ unsigned int new_len);
+void dev_qdisc_commit_tx_queue_len(struct net_device *dev);
+void dev_qdisc_abort_tx_queue_len(struct net_device *dev);
void dev_qdisc_change_real_num_tx(struct net_device *dev,
unsigned int new_real_tx);
void dev_init_scheduler(struct net_device *dev);
diff --git a/net/core/dev.c b/net/core/dev.c
index 9a9bfc4815e6..4a1ddafca036 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -10269,26 +10269,26 @@ int netif_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
return -ERANGE;
if (new_len != orig_len) {
- WRITE_ONCE(dev->tx_queue_len, new_len);
+ /* Nothing may fail once the new length is set. */
+ res = dev_qdisc_prepare_tx_queue_len(dev, new_len);
+ if (res)
+ goto err;
if (ops && ops->change_tx_queue_len) {
res = ops->change_tx_queue_len(dev, new_len);
if (res)
- goto err_rollback;
+ goto err_abort;
}
- res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
- res = notifier_to_errno(res);
- if (res)
- goto err_rollback;
- res = dev_qdisc_change_tx_queue_len(dev);
- if (res)
- goto err_rollback;
+ WRITE_ONCE(dev->tx_queue_len, new_len);
+ dev_qdisc_commit_tx_queue_len(dev);
+ call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
}
return 0;
-err_rollback:
+err_abort:
+ dev_qdisc_abort_tx_queue_len(dev);
+err:
netdev_err(dev, "refused to change device tx_queue_len\n");
- WRITE_ONCE(dev->tx_queue_len, orig_len);
return res;
}
diff --git a/net/sched/sch_generic.c b/net/sched/sch_generic.c
index c7eb4ef5411f..b72d535729d0 100644
--- a/net/sched/sch_generic.c
+++ b/net/sched/sch_generic.c
@@ -1534,7 +1534,7 @@ void mq_change_real_num_tx(struct Qdisc *sch, unsigned int new_real_tx)
}
EXPORT_SYMBOL(mq_change_real_num_tx);
-static void dev_qdisc_abort_tx_queue_len(struct net_device *dev)
+void dev_qdisc_abort_tx_queue_len(struct net_device *dev)
{
struct Qdisc *qdisc;
unsigned int i;
@@ -1547,8 +1547,8 @@ static void dev_qdisc_abort_tx_queue_len(struct net_device *dev)
}
/* Allocate everything a tx_queue_len change needs, or nothing. */
-static int dev_qdisc_prepare_tx_queue_len(struct net_device *dev,
- unsigned int new_len)
+int dev_qdisc_prepare_tx_queue_len(struct net_device *dev,
+ unsigned int new_len)
{
struct Qdisc *qdisc;
unsigned int i;
@@ -1569,7 +1569,7 @@ static int dev_qdisc_prepare_tx_queue_len(struct net_device *dev,
return 0;
}
-static void dev_qdisc_commit_tx_queue_len(struct net_device *dev)
+void dev_qdisc_commit_tx_queue_len(struct net_device *dev)
{
bool up = dev->flags & IFF_UP;
struct Qdisc *qdisc;
@@ -1588,17 +1588,6 @@ static void dev_qdisc_commit_tx_queue_len(struct net_device *dev)
dev_activate(dev);
}
-int dev_qdisc_change_tx_queue_len(struct net_device *dev)
-{
- int ret;
-
- ret = dev_qdisc_prepare_tx_queue_len(dev, dev->tx_queue_len);
- if (!ret)
- dev_qdisc_commit_tx_queue_len(dev);
-
- return ret;
-}
-
static void dev_init_scheduler_queue(struct net_device *dev,
struct netdev_queue *dev_queue,
void *_qdisc)
--
2.54.0
prev 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 ` [PATCH RFC net-next 4/5] net: resize tun and tap rings from rtnl_link_ops SeungJong Ha
2026-12-06 14:44 ` SeungJong Ha [this message]
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-5-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®