* [PATCH RFC net-next 0/5] net: make tx_queue_len changes all-or-nothing
@ 2026-12-06 14:44 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
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: SeungJong Ha @ 2026-12-06 14:44 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Willem de Bruijn, Simon Horman, Jamal Hadi Salim, Jiri Pirko,
Andrew Lunn, Willem de Bruijn, Jason Wang
Cc: Michael S. Tsirkin, John Fastabend, linux-kernel, netdev, SeungJong Ha
This follows the discussion of v1 [1], a different fix that sent
NETDEV_CHANGE_TX_QUEUE_LEN again after a failed change. I proposed
preallocating instead [2].
The only listener that can fail is the driver of the device itself
(tun, macvtap or ipvtap), and its resize already changes all rings or
none. So only the qdiscs need a separate allocation step:
prepare qdiscs -> resize device rings -> set tx_queue_len
-> swap qdisc rings -> notify
Patches 1-3 do not depend on patches 4-5.
Questions:
- Is rtnl_link_ops the right place for the callback? A ndo does not
work because macvtap and ipvtap use the net_device_ops of macvlan
and ipvlan.
- Patch 5 ignores the return value of the notifier, so an out-of-tree
listener can no longer veto the change. No in-tree one does after
patch 4. The only in-tree listener left is rtnetlink, for
RTM_NEWLINK. I kept the event like the ones of other link
attributes, but it could become a direct rtmsg_ifinfo() call.
Should it be removed?
- The three new Qdisc_ops are not documented, like the other ones.
Their contract: all three run once per tx queue, so more than once
for a root qdisc shared by them, and commit and abort must do
nothing when nothing is pending. Should it be written down next to
them?
All new qdisc rings are allocated before any old one is freed, so a
change needs more memory at once.
I found this with a prototype static analysis tool I am developing.
It flags error paths that return after a change was already made
visible to other code.
The analysis, the reproducer and the fix were written with Claude Code
(Claude Opus 5.5). I reviewed every line of the patches and the commit
messages, and re-ran the reproducer myself.
Tested in a QEMU VM on net-next d5cf954cebe4 with KASAN, lockdep and
kmemleak. failslab failed each allocation of the resize in turn.
Before, 37 of 64 runs left some qdiscs at the new length; after, none.
tun, macvtap and ipvtap no longer announce a refused length in
RTM_NEWLINK. All rings follow on growing and shrinking. tc-testing
pfifo_fast.json and fifo.json give the same results before and after.
Not tested on hardware.
[1] https://lore.kernel.org/netdev/20261125-tc1-txqlen-renotify-v1-1-6c8e9c4d0503@gmail.com/
[2] https://lore.kernel.org/netdev/CAGAB666kgzt9SF+QgAQ3ZnwhP3624P64Z89e5+uUrW0Pkkkdzg@mail.gmail.com/
Signed-off-by: SeungJong Ha <engineer.jjhama@gmail.com>
---
SeungJong Ha (5):
ptr_ring: drop the unused gfp argument of __ptr_ring_swap_queue()
ptr_ring: split ptr_ring_resize_multiple_bh() into alloc and swap
net_sched: change tx_queue_len on all tx queues or on none
net: resize tun and tap rings from rtnl_link_ops
net: change tx_queue_len only after all allocations succeeded
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/linux/ptr_ring.h | 86 +++++++++++++++++++++++++----------
include/linux/skb_array.h | 14 +++---
include/net/rtnetlink.h | 5 +++
include/net/sch_generic.h | 10 ++++-
net/core/dev.c | 24 ++++++----
net/sched/sch_generic.c | 106 ++++++++++++++++++++++++++++++++++----------
11 files changed, 235 insertions(+), 120 deletions(-)
---
base-commit: d5cf954cebe4f175dfd834e1dc471a3aa203ec7d
change-id: 20261205-tc1-txqlen-commit-42c8f03df652
Best regards,
--
SeungJong Ha <engineer.jjhama@gmail.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH RFC net-next 1/5] ptr_ring: drop the unused gfp argument of __ptr_ring_swap_queue()
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 ` 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
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: SeungJong Ha @ 2026-12-06 14:44 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Willem de Bruijn, Simon Horman, Jamal Hadi Salim, Jiri Pirko,
Andrew Lunn, Willem de Bruijn, Jason Wang
Cc: Michael S. Tsirkin, John Fastabend, linux-kernel, netdev, SeungJong Ha
__ptr_ring_swap_queue() does not allocate. It has not used its gfp
argument since it was split out of ptr_ring_resize() in commit
59e6ae53248a ("ptr_ring: support resizing multiple queues"). Drop it.
No functional change.
The patch was written with Claude Code (Claude Opus 5.5).
Signed-off-by: SeungJong Ha <engineer.jjhama@gmail.com>
---
include/linux/ptr_ring.h | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h
index b851a9c170bf..4e710397903b 100644
--- a/include/linux/ptr_ring.h
+++ b/include/linux/ptr_ring.h
@@ -590,8 +590,7 @@ static inline void ptr_ring_unconsume(struct ptr_ring *r, void **batch, int n,
}
static inline void **__ptr_ring_swap_queue(struct ptr_ring *r, void **queue,
- int size, gfp_t gfp,
- void (*destroy)(void *))
+ int size, void (*destroy)(void *))
{
int producer = 0;
void **old;
@@ -634,7 +633,7 @@ static inline int ptr_ring_resize_noprof(struct ptr_ring *r, int size, gfp_t gfp
spin_lock_irqsave(&(r)->consumer_lock, flags);
spin_lock(&(r)->producer_lock);
- old = __ptr_ring_swap_queue(r, queue, size, gfp, destroy);
+ old = __ptr_ring_swap_queue(r, queue, size, destroy);
spin_unlock(&(r)->producer_lock);
spin_unlock_irqrestore(&(r)->consumer_lock, flags);
@@ -673,7 +672,7 @@ static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings,
spin_lock_bh(&(rings[i])->consumer_lock);
spin_lock(&(rings[i])->producer_lock);
queues[i] = __ptr_ring_swap_queue(rings[i], queues[i],
- size, gfp, destroy);
+ size, destroy);
spin_unlock(&(rings[i])->producer_lock);
spin_unlock_bh(&(rings[i])->consumer_lock);
}
--
2.54.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH RFC net-next 2/5] ptr_ring: split ptr_ring_resize_multiple_bh() into alloc and swap
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 ` 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
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: SeungJong Ha @ 2026-12-06 14:44 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Willem de Bruijn, Simon Horman, Jamal Hadi Salim, Jiri Pirko,
Andrew Lunn, Willem de Bruijn, Jason Wang
Cc: Michael S. Tsirkin, John Fastabend, linux-kernel, netdev, SeungJong Ha
ptr_ring_resize_multiple_bh() allocates all the new queues before it
swaps any of them in, so it either resizes every ring or none. A
caller that has to do other work that can fail between allocating
and swapping cannot use it. pfifo_fast needs this to change the
rings of all tx queues of a device together.
Split it into ptr_ring_alloc_queues(), which can fail, and
ptr_ring_swap_queues_bh(), which cannot. ptr_ring_free_queues() frees
an allocation that is not swapped in. ptr_ring_resize_multiple_bh()
now calls the first two in turn; no functional change.
The patch was written with Claude Code (Claude Opus 5.5).
Signed-off-by: SeungJong Ha <engineer.jjhama@gmail.com>
---
include/linux/ptr_ring.h | 79 ++++++++++++++++++++++++++++++++++++------------
1 file changed, 59 insertions(+), 20 deletions(-)
diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h
index 4e710397903b..539063e29a55 100644
--- a/include/linux/ptr_ring.h
+++ b/include/linux/ptr_ring.h
@@ -645,22 +645,18 @@ static inline int ptr_ring_resize_noprof(struct ptr_ring *r, int size, gfp_t gfp
#define ptr_ring_resize(...) alloc_hooks(ptr_ring_resize_noprof(__VA_ARGS__))
/*
- * Note: producer lock is nested within consumer lock, so if you
- * resize you must make sure all uses nest correctly.
- * In particular if you consume ring in BH context, you must
- * disable BH when doing so.
+ * Allocate @nrings queues of @size entries for ptr_ring_swap_queues_bh().
+ * On failure, return NULL with nothing allocated.
*/
-static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings,
- unsigned int nrings,
- int size, gfp_t gfp,
- void (*destroy)(void *))
+static inline void ***ptr_ring_alloc_queues_noprof(unsigned int nrings,
+ int size, gfp_t gfp)
{
void ***queues;
int i;
queues = kmalloc_array_noprof(nrings, sizeof(*queues), gfp);
if (!queues)
- goto noqueues;
+ return NULL;
for (i = 0; i < nrings; ++i) {
queues[i] = __ptr_ring_init_queue_alloc_noprof(size, gfp);
@@ -668,6 +664,41 @@ static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings,
goto nomem;
}
+ return queues;
+
+nomem:
+ while (--i >= 0)
+ kvfree(queues[i]);
+
+ kfree(queues);
+
+ return NULL;
+}
+#define ptr_ring_alloc_queues(...) \
+ alloc_hooks(ptr_ring_alloc_queues_noprof(__VA_ARGS__))
+
+/* Free @queues from ptr_ring_alloc_queues() without swapping them in. */
+static inline void ptr_ring_free_queues(void ***queues, unsigned int nrings)
+{
+ int i;
+
+ for (i = 0; i < nrings; ++i)
+ kvfree(queues[i]);
+
+ kfree(queues);
+}
+
+/*
+ * Swap in @queues from ptr_ring_alloc_queues() with the same @nrings and
+ * @size, then free the old queues and @queues. Locking is as for
+ * ptr_ring_resize_multiple_bh().
+ */
+static inline void ptr_ring_swap_queues_bh(struct ptr_ring **rings,
+ void ***queues, unsigned int nrings,
+ int size, void (*destroy)(void *))
+{
+ int i;
+
for (i = 0; i < nrings; ++i) {
spin_lock_bh(&(rings[i])->consumer_lock);
spin_lock(&(rings[i])->producer_lock);
@@ -677,21 +708,29 @@ static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings,
spin_unlock_bh(&(rings[i])->consumer_lock);
}
- for (i = 0; i < nrings; ++i)
- kvfree(queues[i]);
-
- kfree(queues);
+ ptr_ring_free_queues(queues, nrings);
+}
- return 0;
+/*
+ * Note: producer lock is nested within consumer lock, so if you
+ * resize you must make sure all uses nest correctly.
+ * In particular if you consume ring in BH context, you must
+ * disable BH when doing so.
+ */
+static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings,
+ unsigned int nrings,
+ int size, gfp_t gfp,
+ void (*destroy)(void *))
+{
+ void ***queues;
-nomem:
- while (--i >= 0)
- kvfree(queues[i]);
+ queues = ptr_ring_alloc_queues_noprof(nrings, size, gfp);
+ if (!queues)
+ return -ENOMEM;
- kfree(queues);
+ ptr_ring_swap_queues_bh(rings, queues, nrings, size, destroy);
-noqueues:
- return -ENOMEM;
+ return 0;
}
#define ptr_ring_resize_multiple_bh(...) \
alloc_hooks(ptr_ring_resize_multiple_bh_noprof(__VA_ARGS__))
--
2.54.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH RFC net-next 3/5] net_sched: change tx_queue_len on all tx queues or on none
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 ` 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 ` [PATCH RFC net-next 5/5] net: change tx_queue_len only after all allocations succeeded SeungJong Ha
4 siblings, 0 replies; 6+ messages in thread
From: SeungJong Ha @ 2026-12-06 14:44 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Willem de Bruijn, Simon Horman, Jamal Hadi Salim, Jiri Pirko,
Andrew Lunn, Willem de Bruijn, Jason Wang
Cc: Michael S. Tsirkin, John Fastabend, linux-kernel, netdev, SeungJong Ha
dev_qdisc_change_tx_queue_len() resizes the qdisc of each tx queue in
turn and stops at the first failure, leaving the earlier queues with
the new length:
/* TODO: revert changes on a partial failure */
Reverting would need new allocations again. Instead, split
->change_tx_queue_len() in three:
->prepare_tx_queue_len() allocates the new rings and may fail
->commit_tx_queue_len() swaps them in
->abort_tx_queue_len() frees them
Only prepare can fail. All tx queues are prepared before any is
changed.
The allocations now happen before the device is deactivated, so a
failure no longer stops the queues at all.
All tx queues hold their new rings at the same time until the commit,
so a change needs more memory at once than before.
pfifo_fast is the only qdisc implementing these callbacks.
The patch was written with Claude Code (Claude Opus 5.5).
Signed-off-by: SeungJong Ha <engineer.jjhama@gmail.com>
---
include/linux/skb_array.h | 14 ++----
include/net/sch_generic.h | 5 +-
net/sched/sch_generic.c | 115 ++++++++++++++++++++++++++++++++++++----------
3 files changed, 101 insertions(+), 33 deletions(-)
diff --git a/include/linux/skb_array.h b/include/linux/skb_array.h
index bf178238a308..91fd1fab87f6 100644
--- a/include/linux/skb_array.h
+++ b/include/linux/skb_array.h
@@ -199,18 +199,14 @@ static inline int skb_array_resize(struct skb_array *a, int size, gfp_t gfp)
return ptr_ring_resize(&a->ring, size, gfp, __skb_array_destroy_skb);
}
-static inline int skb_array_resize_multiple_bh_noprof(struct skb_array **rings,
- int nrings,
- unsigned int size,
- gfp_t gfp)
+static inline void skb_array_swap_queues_bh(struct skb_array **rings,
+ void ***queues, int nrings,
+ unsigned int size)
{
BUILD_BUG_ON(offsetof(struct skb_array, ring));
- return ptr_ring_resize_multiple_bh_noprof((struct ptr_ring **)rings,
- nrings, size, gfp,
- __skb_array_destroy_skb);
+ ptr_ring_swap_queues_bh((struct ptr_ring **)rings, queues, nrings,
+ size, __skb_array_destroy_skb);
}
-#define skb_array_resize_multiple_bh(...) \
- alloc_hooks(skb_array_resize_multiple_bh_noprof(__VA_ARGS__))
static inline void skb_array_cleanup(struct skb_array *a)
{
diff --git a/include/net/sch_generic.h b/include/net/sch_generic.h
index 1acaadb3e2cf..8e0e774844c2 100644
--- a/include/net/sch_generic.h
+++ b/include/net/sch_generic.h
@@ -326,7 +326,10 @@ struct Qdisc_ops {
struct nlattr *arg,
struct netlink_ext_ack *extack);
void (*attach)(struct Qdisc *sch);
- int (*change_tx_queue_len)(struct Qdisc *, unsigned int);
+ int (*prepare_tx_queue_len)(struct Qdisc *sch,
+ unsigned int new_len);
+ void (*commit_tx_queue_len)(struct Qdisc *sch);
+ void (*abort_tx_queue_len)(struct Qdisc *sch);
void (*change_real_num_tx)(struct Qdisc *sch,
unsigned int new_real_tx);
diff --git a/net/sched/sch_generic.c b/net/sched/sch_generic.c
index d2e0d20f3ba1..c7eb4ef5411f 100644
--- a/net/sched/sch_generic.c
+++ b/net/sched/sch_generic.c
@@ -804,9 +804,12 @@ EXPORT_SYMBOL(sch_default_prio2band);
/*
* Private data for a pfifo_fast scheduler containing:
* - rings for priority bands
+ * - new rings for a tx_queue_len change, between prepare and commit
*/
struct pfifo_fast_priv {
struct skb_array q[PFIFO_FAST_BANDS];
+ void ***pending;
+ unsigned int pending_len;
};
static inline struct skb_array *band2list(struct pfifo_fast_priv *priv,
@@ -983,21 +986,53 @@ static void pfifo_fast_destroy(struct Qdisc *sch)
}
}
-static int pfifo_fast_change_tx_queue_len(struct Qdisc *sch,
- unsigned int new_len)
+static int pfifo_fast_prepare_tx_queue_len(struct Qdisc *sch,
+ unsigned int new_len)
+{
+ struct pfifo_fast_priv *priv = qdisc_priv(sch);
+
+ /* A root qdisc is attached to every tx queue of the device. */
+ if (priv->pending)
+ return 0;
+
+ priv->pending = ptr_ring_alloc_queues(PFIFO_FAST_BANDS, new_len,
+ GFP_KERNEL);
+ if (!priv->pending)
+ return -ENOMEM;
+
+ priv->pending_len = new_len;
+ return 0;
+}
+
+static void pfifo_fast_commit_tx_queue_len(struct Qdisc *sch)
{
struct pfifo_fast_priv *priv = qdisc_priv(sch);
struct skb_array *bands[PFIFO_FAST_BANDS];
int prio;
+ if (!priv->pending)
+ return;
+
for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
struct skb_array *q = band2list(priv, prio);
bands[prio] = q;
}
- return skb_array_resize_multiple_bh(bands, PFIFO_FAST_BANDS, new_len,
- GFP_KERNEL);
+ skb_array_swap_queues_bh(bands, priv->pending, PFIFO_FAST_BANDS,
+ priv->pending_len);
+ priv->pending = NULL;
+}
+
+static void pfifo_fast_abort_tx_queue_len(struct Qdisc *sch)
+{
+ struct pfifo_fast_priv *priv = qdisc_priv(sch);
+
+ if (!priv->pending)
+ return;
+
+ ptr_ring_free_queues(priv->pending, PFIFO_FAST_BANDS);
+ priv->pending = NULL;
}
struct Qdisc_ops pfifo_fast_ops __read_mostly = {
@@ -1010,7 +1045,9 @@ struct Qdisc_ops pfifo_fast_ops __read_mostly = {
.destroy = pfifo_fast_destroy,
.reset = pfifo_fast_reset,
.dump = pfifo_fast_dump,
- .change_tx_queue_len = pfifo_fast_change_tx_queue_len,
+ .prepare_tx_queue_len = pfifo_fast_prepare_tx_queue_len,
+ .commit_tx_queue_len = pfifo_fast_commit_tx_queue_len,
+ .abort_tx_queue_len = pfifo_fast_abort_tx_queue_len,
.owner = THIS_MODULE,
.static_flags = TCQ_F_NOLOCK | TCQ_F_CPUSTATS,
};
@@ -1464,17 +1501,6 @@ void dev_deactivate(struct net_device *dev, bool reset_needed)
}
EXPORT_SYMBOL(dev_deactivate);
-static int qdisc_change_tx_queue_len(struct net_device *dev,
- struct netdev_queue *dev_queue)
-{
- struct Qdisc *qdisc = rtnl_dereference(dev_queue->qdisc_sleeping);
- const struct Qdisc_ops *ops = qdisc->ops;
-
- if (ops->change_tx_queue_len)
- return ops->change_tx_queue_len(qdisc, dev->tx_queue_len);
- return 0;
-}
-
void dev_qdisc_change_real_num_tx(struct net_device *dev,
unsigned int new_real_tx)
{
@@ -1508,25 +1534,68 @@ void mq_change_real_num_tx(struct Qdisc *sch, unsigned int new_real_tx)
}
EXPORT_SYMBOL(mq_change_real_num_tx);
-int dev_qdisc_change_tx_queue_len(struct net_device *dev)
+static void dev_qdisc_abort_tx_queue_len(struct net_device *dev)
+{
+ struct Qdisc *qdisc;
+ unsigned int i;
+
+ for (i = 0; i < dev->num_tx_queues; i++) {
+ qdisc = rtnl_dereference(dev->_tx[i].qdisc_sleeping);
+ if (qdisc->ops->abort_tx_queue_len)
+ qdisc->ops->abort_tx_queue_len(qdisc);
+ }
+}
+
+/* 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)
+{
+ struct Qdisc *qdisc;
+ unsigned int i;
+ int ret;
+
+ for (i = 0; i < dev->num_tx_queues; i++) {
+ qdisc = rtnl_dereference(dev->_tx[i].qdisc_sleeping);
+ if (!qdisc->ops->prepare_tx_queue_len)
+ continue;
+
+ ret = qdisc->ops->prepare_tx_queue_len(qdisc, new_len);
+ if (ret) {
+ dev_qdisc_abort_tx_queue_len(dev);
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+static void dev_qdisc_commit_tx_queue_len(struct net_device *dev)
{
bool up = dev->flags & IFF_UP;
+ struct Qdisc *qdisc;
unsigned int i;
- int ret = 0;
if (up)
dev_deactivate(dev, false);
for (i = 0; i < dev->num_tx_queues; i++) {
- ret = qdisc_change_tx_queue_len(dev, &dev->_tx[i]);
-
- /* TODO: revert changes on a partial failure */
- if (ret)
- break;
+ qdisc = rtnl_dereference(dev->_tx[i].qdisc_sleeping);
+ if (qdisc->ops->commit_tx_queue_len)
+ qdisc->ops->commit_tx_queue_len(qdisc);
}
if (up)
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;
}
--
2.54.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH RFC net-next 4/5] net: resize tun and tap rings from rtnl_link_ops
2026-12-06 14:44 [PATCH RFC net-next 0/5] net: make tx_queue_len changes all-or-nothing SeungJong Ha
` (2 preceding siblings ...)
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
2026-12-06 14:44 ` [PATCH RFC net-next 5/5] net: change tx_queue_len only after all allocations succeeded SeungJong Ha
4 siblings, 0 replies; 6+ messages in thread
From: SeungJong Ha @ 2026-12-06 14:44 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Willem de Bruijn, Simon Horman, Jamal Hadi Salim, Jiri Pirko,
Andrew Lunn, Willem de Bruijn, Jason Wang
Cc: Michael S. Tsirkin, John Fastabend, linux-kernel, netdev, SeungJong Ha
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH RFC net-next 5/5] net: change tx_queue_len only after all allocations succeeded
2026-12-06 14:44 [PATCH RFC net-next 0/5] net: make tx_queue_len changes all-or-nothing SeungJong Ha
` (3 preceding siblings ...)
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
4 siblings, 0 replies; 6+ messages in thread
From: SeungJong Ha @ 2026-12-06 14:44 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Willem de Bruijn, Simon Horman, Jamal Hadi Salim, Jiri Pirko,
Andrew Lunn, Willem de Bruijn, Jason Wang
Cc: Michael S. Tsirkin, John Fastabend, linux-kernel, netdev, SeungJong Ha
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
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-06 14:45 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH RFC net-next 5/5] net: change tx_queue_len only after all allocations succeeded SeungJong Ha
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®