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