[PATCH RFC net-next 5/5] net: change tx_queue_len only after all allocations succeeded

From: SeungJong Ha

Date: Sun Dec 06 2026 - 09:45:47 EST


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@xxxxxxxxx>
---
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