* [PATCH net-next v13 0/4] net/sched: cap the accounted backlog before it can wrap
@ 2026-11-27 11:57 Jamal Hadi Salim
2026-11-27 11:57 ` [PATCH net-next v13 1/4] net/sched: cake: return the classifier result to the caller Jamal Hadi Salim
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Jamal Hadi Salim @ 2026-11-27 11:57 UTC (permalink / raw)
To: netdev
Cc: Jamal Hadi Salim, David S . Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jiri Pirko, Stephen Hemminger,
Vinicius Costa Gomes, Toke Høiland-Jørgensen, cake,
Victor Nogueira, Björn Töpel, Sashiko
This series bounds the accounted backlog of the AQM qdiscs so that it
cannot wrap mod 2^32.
1/4 cake: classifier refactor: cake_tcf_classify() returns its result
to the caller.
2/4 cake: use a dedicated split-GSO list iterator.
3/4 the fix: drop at enqueue once the stored backlog cannot represent
the packet's accounted length, and saturate every subtraction from
the counter.
4/4 cake: split cake_hash() into key extraction (on the unsegmented
skb) and resolution (after admission), move the split-GSO path
ahead of the resolution, and refuse a split an ancestor cannot
absorb.
For the sashikos: net-next, no Fixes: (the aggregate wrap predates every
touched qdisc).
1/4 and 2/4 are unchanged from v12 and carry no revision-history block.
Changes in v13 (Sashiko nipa and gemini reviews of v12):
- every core subtraction saturates. An offloaded qdisc is exempt from
the ceiling: its counter carries hardware bytes, so wrong statistics
there beats a permanent drop.
- taprio mirrors the child's net change at dequeue too, and books only
the outermost handoff to a child.
- htb bumps q->overlimits, which htb_dump() copies over the qstats
field.
- 4/4 refuses a split an ancestor cannot absorb, and its Conditions
section states the shape that reaches the check.
- Toke's Acked-by is dropped for the substantial change; he stays in
the Cc.
- rebased onto net-next at ccb81d5e317b.
Jamal Hadi Salim (4):
net/sched: cake: return the classifier result to the caller
net/sched: cake: use a dedicated split-GSO list iterator
net/sched: cap the accounted backlog before it can wrap
net/sched: cake: hash before segmenting and reject an overlong split
include/net/pkt_sched.h | 1 -
include/net/sch_generic.h | 70 +++++++++++++-
net/sched/sch_api.c | 37 +++++++-
net/sched/sch_cake.c | 190 +++++++++++++++++++++++++++-----------
net/sched/sch_cbs.c | 6 ++
net/sched/sch_choke.c | 6 ++
net/sched/sch_codel.c | 16 ++--
net/sched/sch_drr.c | 6 ++
net/sched/sch_dualpi2.c | 1 +
net/sched/sch_etf.c | 6 ++
net/sched/sch_ets.c | 6 ++
net/sched/sch_fifo.c | 12 +++
net/sched/sch_fq.c | 6 ++
net/sched/sch_fq_codel.c | 9 ++
net/sched/sch_fq_pie.c | 3 +-
net/sched/sch_gred.c | 9 ++
net/sched/sch_hfsc.c | 6 ++
net/sched/sch_hhf.c | 18 +++-
net/sched/sch_htb.c | 16 +++-
net/sched/sch_netem.c | 18 ++++
net/sched/sch_pie.c | 3 +-
net/sched/sch_plug.c | 3 +-
net/sched/sch_prio.c | 6 ++
net/sched/sch_qfq.c | 6 ++
net/sched/sch_red.c | 10 ++
net/sched/sch_sfb.c | 6 ++
net/sched/sch_sfq.c | 8 +-
net/sched/sch_skbprio.c | 17 +++-
net/sched/sch_taprio.c | 98 ++++++++++++++++----
net/sched/sch_tbf.c | 6 ++
30 files changed, 509 insertions(+), 96 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH net-next v13 1/4] net/sched: cake: return the classifier result to the caller
2026-11-27 11:57 [PATCH net-next v13 0/4] net/sched: cap the accounted backlog before it can wrap Jamal Hadi Salim
@ 2026-11-27 11:57 ` Jamal Hadi Salim
2026-11-27 11:57 ` [PATCH net-next v13 2/4] net/sched: cake: use a dedicated split-GSO list iterator Jamal Hadi Salim
` (2 subsequent siblings)
3 siblings, 0 replies; 8+ messages in thread
From: Jamal Hadi Salim @ 2026-11-27 11:57 UTC (permalink / raw)
To: netdev
Cc: Jamal Hadi Salim, David S . Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jiri Pirko, Stephen Hemminger,
Vinicius Costa Gomes, Toke Høiland-Jørgensen, cake,
Victor Nogueira, Björn Töpel, Sashiko
Preparation:
cake runs an optional external classifier from its enqueue path and then
computes the flow hash inside the helper. Have the helper return the
filter's overrides instead, so the caller owns the flow selection.
This is preparation for the accounting changes that follow. Packet
classification and flow selection are unchanged.
Link: https://lore.kernel.org/netdev/179269577009.3135.5221412623031392098@kernel.org/
Reviewed-by: Victor Nogueira <victor@mojatatu.com>
Acked-by: Toke Høiland-Jørgensen <toke@toke.dk>
Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
---
net/sched/sch_cake.c | 36 +++++++++++++++++++++++-------------
1 file changed, 23 insertions(+), 13 deletions(-)
diff --git a/net/sched/sch_cake.c b/net/sched/sch_cake.c
index f97134522727..2480ee4b9575 100644
--- a/net/sched/sch_cake.c
+++ b/net/sched/sch_cake.c
@@ -1717,18 +1717,25 @@ static struct cake_tin_data *cake_select_tin(struct Qdisc *sch,
return &qd->tins[tin];
}
-static u32 cake_classify(struct Qdisc *sch, struct cake_tin_data **t,
- struct sk_buff *skb, int flow_mode, int *qerr)
+/* Run the qdisc's optional external classifier. Returns true when a
+ * terminal TC action wants the packet dropped, with the reason in *qerr;
+ * the caller must drop the skb. Otherwise *flow and *host hold the
+ * filter's overrides (0 if none).
+ */
+static bool cake_tcf_classify(struct Qdisc *sch, struct sk_buff *skb,
+ u16 *flow, u16 *host, int *qerr)
{
struct cake_sched_data *q = qdisc_priv(sch);
struct tcf_proto *filter;
struct tcf_result res;
- u16 flow = 0, host = 0;
int result;
+ *flow = 0;
+ *host = 0;
+
filter = rcu_dereference_bh(q->filter_list);
if (!filter)
- goto hash;
+ return false;
*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
result = tcf_classify_qdisc(skb, filter, &res, false);
@@ -1742,17 +1749,15 @@ static u32 cake_classify(struct Qdisc *sch, struct cake_tin_data **t,
*qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
fallthrough;
case TC_ACT_SHOT:
- return 0;
+ return true;
}
#endif
if (TC_H_MIN(res.classid) <= CAKE_QUEUES)
- flow = TC_H_MIN(res.classid);
+ *flow = TC_H_MIN(res.classid);
if (TC_H_MAJ(res.classid) <= (CAKE_QUEUES << 16))
- host = TC_H_MAJ(res.classid) >> 16;
+ *host = TC_H_MAJ(res.classid) >> 16;
}
-hash:
- *t = cake_select_tin(sch, skb);
- return cake_hash(*t, skb, flow_mode, flow, host) + 1;
+ return false;
}
static void cake_reconfigure(struct Qdisc *sch);
@@ -1763,20 +1768,25 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
u32 idx, tin, prev_qlen, prev_backlog, drop_id;
struct cake_sched_data *q = qdisc_priv(sch);
int len = qdisc_pkt_len(skb), ret;
+ u16 flow_override, host_override;
struct sk_buff *ack = NULL;
ktime_t now = ktime_get();
struct cake_tin_data *b;
struct cake_flow *flow;
bool same_flow = false;
- /* choose flow to insert into */
- idx = cake_classify(sch, &b, skb, q->config->flow_mode, &ret);
- if (idx == 0) {
+ /* Classify first: a terminal verdict means do not enqueue. */
+ if (cake_tcf_classify(sch, skb, &flow_override, &host_override, &ret)) {
if (ret & __NET_XMIT_BYPASS)
qdisc_qstats_drop(sch);
__qdisc_drop(skb, to_free);
return ret;
}
+
+ /* choose flow to insert into */
+ b = cake_select_tin(sch, skb);
+ idx = cake_hash(b, skb, q->config->flow_mode, flow_override,
+ host_override) + 1;
tin = (u32)(b - q->tins);
idx--;
flow = &b->flows[idx];
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH net-next v13 2/4] net/sched: cake: use a dedicated split-GSO list iterator
2026-11-27 11:57 [PATCH net-next v13 0/4] net/sched: cap the accounted backlog before it can wrap Jamal Hadi Salim
2026-11-27 11:57 ` [PATCH net-next v13 1/4] net/sched: cake: return the classifier result to the caller Jamal Hadi Salim
@ 2026-11-27 11:57 ` Jamal Hadi Salim
2026-11-27 11:57 ` [PATCH net-next v13 3/4] net/sched: cap the accounted backlog before it can wrap Jamal Hadi Salim
2026-11-27 11:57 ` [PATCH net-next v13 4/4] net/sched: cake: hash before segmenting and reject an overlong split Jamal Hadi Salim
3 siblings, 0 replies; 8+ messages in thread
From: Jamal Hadi Salim @ 2026-11-27 11:57 UTC (permalink / raw)
To: netdev
Cc: Jamal Hadi Salim, David S . Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jiri Pirko, Stephen Hemminger,
Vinicius Costa Gomes, Toke Høiland-Jørgensen, cake,
Victor Nogueira, Björn Töpel, Sashiko
skb_list_walk_safe(segs, segs, nskb) reuses the list head as the walk
cursor, so segs no longer names the segment list once the walk finishes.
Use a separate seg cursor to keep the head and the walker distinct. No
behaviour change.
Reviewed-by: Victor Nogueira <victor@mojatatu.com>
Acked-by: Toke Høiland-Jørgensen <toke@toke.dk>
Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
---
net/sched/sch_cake.c | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/net/sched/sch_cake.c b/net/sched/sch_cake.c
index 2480ee4b9575..2bc0dac9123a 100644
--- a/net/sched/sch_cake.c
+++ b/net/sched/sch_cake.c
@@ -1816,7 +1816,7 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
WRITE_ONCE(b->max_skblen, len);
if (qdisc_pkt_segs(skb) > 1 && q->config->rate_flags & CAKE_FLAG_SPLIT_GSO) {
- struct sk_buff *segs, *nskb;
+ struct sk_buff *segs, *nskb, *seg;
netdev_features_t features = netif_skb_features(skb);
unsigned int slen = 0, numsegs = 0;
@@ -1824,19 +1824,19 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
if (IS_ERR_OR_NULL(segs))
return qdisc_drop(skb, sch, to_free);
- skb_list_walk_safe(segs, segs, nskb) {
- skb_mark_not_on_list(segs);
- qdisc_skb_cb(segs)->pkt_len = segs->len;
- qdisc_skb_cb(segs)->pkt_segs = 1;
- cobalt_set_enqueue_time(segs, now);
- get_cobalt_cb(segs)->adjusted_len = cake_overhead(q,
- segs);
- flow_queue_add(flow, segs);
+ skb_list_walk_safe(segs, seg, nskb) {
+ skb_mark_not_on_list(seg);
+ qdisc_skb_cb(seg)->pkt_len = seg->len;
+ qdisc_skb_cb(seg)->pkt_segs = 1;
+ cobalt_set_enqueue_time(seg, now);
+ get_cobalt_cb(seg)->adjusted_len = cake_overhead(q,
+ seg);
+ flow_queue_add(flow, seg);
qdisc_qlen_inc(sch);
numsegs++;
- slen += segs->len;
- q->buffer_used += segs->truesize;
+ q->buffer_used += seg->truesize;
+ slen += seg->len;
WRITE_ONCE(b->packets, b->packets + 1);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH net-next v13 3/4] net/sched: cap the accounted backlog before it can wrap
2026-11-27 11:57 [PATCH net-next v13 0/4] net/sched: cap the accounted backlog before it can wrap Jamal Hadi Salim
2026-11-27 11:57 ` [PATCH net-next v13 1/4] net/sched: cake: return the classifier result to the caller Jamal Hadi Salim
2026-11-27 11:57 ` [PATCH net-next v13 2/4] net/sched: cake: use a dedicated split-GSO list iterator Jamal Hadi Salim
@ 2026-11-27 11:57 ` Jamal Hadi Salim
2026-11-28 12:08 ` netdev-bot+sashiko
2026-11-27 11:57 ` [PATCH net-next v13 4/4] net/sched: cake: hash before segmenting and reject an overlong split Jamal Hadi Salim
3 siblings, 1 reply; 8+ messages in thread
From: Jamal Hadi Salim @ 2026-11-27 11:57 UTC (permalink / raw)
To: netdev
Cc: Jamal Hadi Salim, David S . Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jiri Pirko, Stephen Hemminger,
Vinicius Costa Gomes, Toke Høiland-Jørgensen, cake,
Victor Nogueira, Björn Töpel, Sashiko
This is a followup of commit 709f34f7c28d ("net/sched: fq: add overflow
bounds to quantum and initial quantum") as reported by Sashiko.
The issue: every AQM qdisc accounts each enqueued packet's stab-adjusted
length into the 32-bit sch->qstats.backlog. A crafted size table inflates
qdisc_pkt_len() to QDISC_PKT_LEN_MAX (1 MiB), and a BIG TCP aggregate adds
the headers replicated per segment, so a few thousand packets wrap the
counter mod 2^32. The AQM then reads a small backlog and drops from the
wrong flow; in fq_codel a wrapped per-flow backlog can make
fq_codel_drop() pick an empty flow.
The Fix: drop at enqueue once the stored backlog cannot represent the
incoming packet's accounted length. Packets up to 1 MiB are covered by a
standing ceiling, QDISC_MAX_BACKLOG == U32_MAX - 2 * QDISC_PKT_LEN_MAX
(one packet plus the duplicate a netem child may book); a larger wire
estimate is checked against the room that actually remains.
The same ceiling covers every qdisc in net/sched that adds the incoming
qdisc_pkt_len() to its own backlog, and the writers below it saturate so
its precondition holds: qstats_backlog_sub() stops at zero and
qdisc_qstats_backlog_apply() takes the signed delta that sfq's head drop
and the tree reconciliation report. An offloaded qdisc is exempt, because
its counter carries driver-maintained hardware bytes. taprio mirrors its
child's net change at dequeue as well and books only the outermost
handoff, and htb bumps q->overlimits, which htb_dump() copies over the
qstats field.
Some context: qdisc_pkt_len() stays truthful for every input, so BPF
wire_len, act_police and the HTB/TBF shapers read the real length. RED
books the packet on success again, as the tree did before v11.
Conditions to recreate the bug:
ip tuntap add tun0 mode tun
ip link set tun0 txqueuelen 32 up
ip addr add 10.99.0.1/24 dev tun0
tc qdisc add dev tun0 root handle 1: stab overhead 2000000000 \
fq_codel flows 1 limit 20000 ecn
# hold the tun fd open without reading so the backlog persists, then
# send 6000 packets: the counter wraps and the unfixed kernel reports
# a wrapped backlog, while the fixed one drops at the ceiling. Setup
# needs CAP_NET_ADMIN in a user namespace of its own (unshare -Urn); the
# payload itself does not.
Reported-by: Sashiko (nipa) <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/netdev/20260818101130.16203-1-jhs@mojatatu.com/
Link: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260818101130.16203-1-jhs@mojatatu.com
Link: https://lore.kernel.org/netdev/CANn89iLfMJV7ancKH1Gjzzm7ZUG-gKcrczkJjJEWN5sCuWd-ug@mail.gmail.com/
Link: https://lore.kernel.org/netdev/CANn89i+GOFH_8g+vSV0jUj0aBqLYVWuFOrHuDggZu9kPC_m6tQ@mail.gmail.com/
Link: https://lore.kernel.org/netdev/87a4oottcj.fsf@toke.dk/
Link: https://lore.kernel.org/netdev/87pkxks68f.fsf@toke.dk/
Link: https://lore.kernel.org/netdev/179231932946.3135.7497478990186385688@kernel.org/
Link: https://lore.kernel.org/netdev/179269577131.3135.3967915390403138015@kernel.org/
Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
---
v12 -> v13:
- saturate every core subtraction: qstats_backlog_sub() stops at zero, and
qdisc_qstats_backlog_apply() takes the signed deltas. (Sashiko nipa)
- do not apply the ceiling to an offloaded qdisc, whose counter carries
driver-maintained hardware bytes. (Sashiko nipa)
- taprio: mirror the child's net change at dequeue too, and book only the
outermost handoff to a child. (Sashiko nipa and gemini)
- htb: bump q->overlimits, which htb_dump() copies over the qstats field.
(Sashiko gemini)
- shorten the commit log; the v12 body read like a changelog.
(Note to the sashikos: reduced because humans complained.)
v11 -> v12:
- taprio: read the child's backlog and queue length before the handoff
and mirror the net change afterwards, instead of booking the packet when
the child returns NET_XMIT_SUCCESS. Some children free the skb and still
report success (tbf_segment(), cake's split path), so the v11 code read
the packet after it was gone; and because taprio_graft() sets
TCQ_F_NOPARENT, a child that evicts on NET_XMIT_CN never reaches taprio
through qdisc_tree_reduce_backlog(), so booking only on success let the
dequeue subtraction run ahead of the booking and the counter wrapped.
The segmented path reads the segment length before the handoff and no
longer counts a segment drop twice.
(Sashiko nipa and gemini reviews of v11)
- RED: book the packet on success again, as the tree did before v11. The
v11 delta arithmetic re-derived the aggregate from the snapshots and so
dropped any offload-driver update that landed while the child ran, and it
passed that same amount on to the ancestors. With the child and the
netem duplicate propagating their own changes, the plain accounting is
correct and cannot wrap.
(Sashiko nipa review of v11)
- qdisc_qstats_backlog_dec() stops at zero. An offload driver updates
sch->qstats.backlog from the dump path while holding only RTNL, so a
datapath increment can be lost; the matching decrement then wrapped the
counter and the new enqueue guard turned the wrap into a permanent drop.
(Sashiko nipa review of v11)
- leave room for two full-size packets in the standing ceiling, because
netem books a duplicate before it re-enters the tree and one enqueue can
then add twice the packet length.
- pfifo's backlog drops report QDISC_DROP_OVERLIMIT and count an
overlimit, like the other guards added here.
(Sashiko nipa review of v11)
- drop the Reviewed-by; the taprio and RED accounting it covered was
replaced in this version.
---
include/net/pkt_sched.h | 1 -
include/net/sch_generic.h | 71 +++++++++++++++++++++++++++-
net/sched/sch_api.c | 44 +++++++++++++++++-
net/sched/sch_cake.c | 7 +++
net/sched/sch_cbs.c | 6 +++
net/sched/sch_choke.c | 6 +++
net/sched/sch_codel.c | 16 ++++---
net/sched/sch_drr.c | 6 +++
net/sched/sch_dualpi2.c | 1 +
net/sched/sch_etf.c | 6 +++
net/sched/sch_ets.c | 6 +++
net/sched/sch_fifo.c | 12 +++++
net/sched/sch_fq.c | 6 +++
net/sched/sch_fq_codel.c | 9 ++++
net/sched/sch_fq_pie.c | 3 +-
net/sched/sch_generic.c | 9 ++++
net/sched/sch_gred.c | 9 ++++
net/sched/sch_hfsc.c | 6 +++
net/sched/sch_hhf.c | 18 ++++++-
net/sched/sch_htb.c | 16 +++++--
net/sched/sch_netem.c | 18 +++++++
net/sched/sch_pie.c | 3 +-
net/sched/sch_plug.c | 3 +-
net/sched/sch_prio.c | 6 +++
net/sched/sch_qfq.c | 6 +++
net/sched/sch_red.c | 10 ++++
net/sched/sch_sfb.c | 6 +++
net/sched/sch_sfq.c | 8 +++-
net/sched/sch_skbprio.c | 17 ++++++-
net/sched/sch_taprio.c | 98 +++++++++++++++++++++++++++++++--------
net/sched/sch_tbf.c | 6 +++
31 files changed, 398 insertions(+), 41 deletions(-)
diff --git a/include/net/pkt_sched.h b/include/net/pkt_sched.h
index 90d3e7943b19..18a419cd9d94 100644
--- a/include/net/pkt_sched.h
+++ b/include/net/pkt_sched.h
@@ -12,7 +12,6 @@
#define DEFAULT_TX_QUEUE_LEN 1000
#define STAB_SIZE_LOG_MAX 30
-#define QDISC_PKT_LEN_MAX (1 << 20) /* 1 MiB */
struct qdisc_walker {
int stop;
diff --git a/include/net/sch_generic.h b/include/net/sch_generic.h
index 1acaadb3e2cf..677cd089ad5d 100644
--- a/include/net/sch_generic.h
+++ b/include/net/sch_generic.h
@@ -735,6 +735,12 @@ void qdisc_destroy(struct Qdisc *qdisc);
void qdisc_put(struct Qdisc *qdisc);
void qdisc_put_unlocked(struct Qdisc *qdisc);
void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
+/* Does every ancestor of @sch have room for @extra more bytes below its
+ * enqueue ceiling? A child that turns one skb into several reconciles the
+ * difference through qdisc_tree_reduce_backlog(), growth that never passes
+ * the ancestors' own enqueue guard.
+ */
+bool qdisc_ancestors_backlog_room(const struct Qdisc *sch, u64 extra);
static inline void dev_reset_queue(struct net_device *dev,
struct netdev_queue *dev_queue,
@@ -915,6 +921,37 @@ static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
return qdisc_skb_cb(skb)->pkt_len;
}
+#define QDISC_PKT_LEN_MAX (1 << 20) /* 1 MiB */
+
+/* Largest backlog that still has room for a 1 MiB packet and a netem
+ * duplicate of it, so that a single enqueue can never run out of
+ * headroom before it is accounted.
+ */
+#define QDISC_MAX_BACKLOG (U32_MAX - 2 * QDISC_PKT_LEN_MAX)
+
+/* Would this packet's accounted length wrap sch->qstats.backlog?
+ * Packets up to QDISC_PKT_LEN_MAX fit below QDISC_MAX_BACKLOG; GSO
+ * can make the length larger, so compare against the real headroom.
+ *
+ * An offloaded qdisc's counter also carries driver-maintained hardware
+ * bytes, so the ceiling does not describe the queue it protects.
+ */
+static inline bool qdisc_backlog_overflow(const struct Qdisc *sch,
+ const struct sk_buff *skb)
+{
+ u32 len = qdisc_pkt_len(skb);
+ u32 backlog;
+
+ if (unlikely(READ_ONCE(sch->flags) & TCQ_F_OFFLOADED))
+ return false;
+
+ backlog = READ_ONCE(sch->qstats.backlog);
+ if (likely(len <= QDISC_PKT_LEN_MAX))
+ return backlog > QDISC_MAX_BACKLOG;
+ /* The duplicate a netem child books means one enqueue can add twice. */
+ return (u64)backlog + 2 * (u64)len > U32_MAX;
+}
+
static inline unsigned int qdisc_pkt_segs(const struct sk_buff *skb)
{
u32 pkt_segs = qdisc_skb_cb(skb)->pkt_segs;
@@ -991,13 +1028,43 @@ static inline void qdisc_bstats_update(struct Qdisc *sch,
static inline void qstats_backlog_sub(struct Qdisc *sch, u32 val)
{
- WRITE_ONCE(sch->qstats.backlog, sch->qstats.backlog - val);
+ u32 backlog = READ_ONCE(sch->qstats.backlog);
+
+ /* Saturate: an earlier clamp or offload update can leave the
+ * counter below the amount being removed.
+ */
+ WRITE_ONCE(sch->qstats.backlog, backlog > val ? backlog - val : 0);
+}
+
+/* Apply a signed net change: positive removes, negative grows. The
+ * negative form is a contract of sfq's head drop and of the tree
+ * reconciliation.
+ */
+static inline void qdisc_qstats_backlog_apply(struct Qdisc *sch, int delta)
+{
+ u32 backlog = READ_ONCE(sch->qstats.backlog);
+ u64 next;
+
+ if (delta >= 0)
+ next = backlog > (u32)delta ? backlog - (u32)delta : 0;
+ else
+ next = min_t(u64, (u64)backlog + (u32)(-(s64)delta), U32_MAX);
+
+ WRITE_ONCE(sch->qstats.backlog, (u32)next);
}
static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
const struct sk_buff *skb)
{
- qstats_backlog_sub(sch, qdisc_pkt_len(skb));
+ u32 backlog = READ_ONCE(sch->qstats.backlog);
+ u32 len = qdisc_pkt_len(skb);
+
+ /* An offload driver can update the counter from the dump path
+ * while the packet is in flight, so a lost increment can leave
+ * the backlog below the length being removed. Do not let that
+ * wrap: the enqueue guard would then drop every packet for good.
+ */
+ WRITE_ONCE(sch->qstats.backlog, backlog > len ? backlog - len : 0);
}
static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
diff --git a/net/sched/sch_api.c b/net/sched/sch_api.c
index f36c41224bcc..062e5d6f158e 100644
--- a/net/sched/sch_api.c
+++ b/net/sched/sch_api.c
@@ -877,13 +877,55 @@ void qdisc_tree_reduce_backlog(struct Qdisc *sch, int n, int len)
cops->qlen_notify(sch, cl);
}
WRITE_ONCE(sch->q.qlen, sch->q.qlen - n);
- qstats_backlog_sub(sch, len);
+ qdisc_qstats_backlog_apply(sch, len);
__qdisc_qstats_drop(sch, drops);
}
rcu_read_unlock();
}
EXPORT_SYMBOL(qdisc_tree_reduce_backlog);
+/* Does every ancestor have room for a growth that qdisc_tree_reduce_backlog()
+ * would apply without its own enqueue guard seeing it?
+ */
+bool qdisc_ancestors_backlog_room(const struct Qdisc *sch, u64 extra)
+{
+ const struct Qdisc *p = sch;
+ u32 parentid;
+
+ if (!extra)
+ return true;
+
+ rcu_read_lock();
+ while ((parentid = p->parent)) {
+ if (parentid == TC_H_ROOT)
+ break;
+
+ if (p->flags & TCQ_F_NOPARENT)
+ break;
+
+ p = qdisc_lookup_rcu(qdisc_dev(p), TC_H_MAJ(parentid));
+ if (!p)
+ break;
+
+ /* An offloaded ancestor is exempt from the ceiling for the same
+ * reason the enqueue guard is: its counter carries hardware
+ * bytes the core does not own.
+ */
+ if (unlikely(READ_ONCE(p->flags) & TCQ_F_OFFLOADED))
+ continue;
+
+ if (unlikely((u64)READ_ONCE(p->qstats.backlog) + extra >
+ QDISC_MAX_BACKLOG)) {
+ rcu_read_unlock();
+ return false;
+ }
+ }
+ rcu_read_unlock();
+
+ return true;
+}
+EXPORT_SYMBOL(qdisc_ancestors_backlog_room);
+
int qdisc_offload_dump_helper(struct Qdisc *sch, enum tc_setup_type type,
void *type_data)
{
diff --git a/net/sched/sch_cake.c b/net/sched/sch_cake.c
index 2bc0dac9123a..25c7e20ec123 100644
--- a/net/sched/sch_cake.c
+++ b/net/sched/sch_cake.c
@@ -1783,6 +1783,13 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
return ret;
}
+ /* Drop before touching the hash or shaper state. */
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
/* choose flow to insert into */
b = cake_select_tin(sch, skb);
idx = cake_hash(b, skb, q->config->flow_mode, flow_override,
diff --git a/net/sched/sch_cbs.c b/net/sched/sch_cbs.c
index 1c93469c56e3..3c018ef70e23 100644
--- a/net/sched/sch_cbs.c
+++ b/net/sched/sch_cbs.c
@@ -92,6 +92,12 @@ static int cbs_child_enqueue(struct sk_buff *skb, struct Qdisc *sch,
unsigned int len = qdisc_pkt_len(skb);
int err;
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
err = child->ops->enqueue(skb, child, to_free);
if (err != NET_XMIT_SUCCESS)
return err;
diff --git a/net/sched/sch_choke.c b/net/sched/sch_choke.c
index 73d3e673dc7b..9ce02273b624 100644
--- a/net/sched/sch_choke.c
+++ b/net/sched/sch_choke.c
@@ -215,6 +215,12 @@ static int choke_enqueue(struct sk_buff *skb, struct Qdisc *sch,
struct choke_sched_data *q = qdisc_priv(sch);
const struct red_parms *p = &q->parms;
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
choke_skb_cb(skb)->keys_valid = 0;
/* Compute average queue usage (see RED) */
q->vars.qavg = red_calc_qavg(p, &q->vars, sch->q.qlen);
diff --git a/net/sched/sch_codel.c b/net/sched/sch_codel.c
index a1ba2e355f2d..78c5d72d6406 100644
--- a/net/sched/sch_codel.c
+++ b/net/sched/sch_codel.c
@@ -116,15 +116,17 @@ static struct sk_buff *codel_peek(struct Qdisc *sch)
static int codel_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
struct sk_buff **to_free)
{
- struct codel_sched_data *q;
+ struct codel_sched_data *q = qdisc_priv(sch);
- if (likely(qdisc_qlen(sch) < sch->limit)) {
- codel_set_enqueue_time(skb);
- return qdisc_enqueue_tail(skb, sch);
+ if (unlikely(qdisc_qlen(sch) >= sch->limit) ||
+ unlikely(qdisc_backlog_overflow(sch, skb))) {
+ WRITE_ONCE(q->drop_overlimit, q->drop_overlimit + 1);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
}
- q = qdisc_priv(sch);
- WRITE_ONCE(q->drop_overlimit, q->drop_overlimit + 1);
- return qdisc_drop_reason(skb, sch, to_free, QDISC_DROP_OVERLIMIT);
+
+ codel_set_enqueue_time(skb);
+ return qdisc_enqueue_tail(skb, sch);
}
static const struct nla_policy codel_policy[TCA_CODEL_MAX + 1] = {
diff --git a/net/sched/sch_drr.c b/net/sched/sch_drr.c
index 8621d057edd9..eeef304f76a7 100644
--- a/net/sched/sch_drr.c
+++ b/net/sched/sch_drr.c
@@ -342,6 +342,12 @@ static int drr_enqueue(struct sk_buff *skb, struct Qdisc *sch,
struct drr_class *cl;
int err = 0;
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
cl = drr_classify(skb, sch, &err);
if (cl == NULL) {
if (err & __NET_XMIT_BYPASS)
diff --git a/net/sched/sch_dualpi2.c b/net/sched/sch_dualpi2.c
index 4947def7c49e..7331f0ec1c8f 100644
--- a/net/sched/sch_dualpi2.c
+++ b/net/sched/sch_dualpi2.c
@@ -392,6 +392,7 @@ static int dualpi2_enqueue_skb(struct sk_buff *skb, struct Qdisc *sch,
struct dualpi2_skb_cb *cb;
if (unlikely(qdisc_qlen(sch) >= sch->limit) ||
+ unlikely(qdisc_backlog_overflow(sch, skb)) ||
unlikely((u64)q->memory_used + skb->truesize > q->memory_limit)) {
qdisc_qstats_overlimit(sch);
if (skb_in_l_queue(skb))
diff --git a/net/sched/sch_etf.c b/net/sched/sch_etf.c
index aaafcf640bc7..a534b11eadd0 100644
--- a/net/sched/sch_etf.c
+++ b/net/sched/sch_etf.c
@@ -168,6 +168,12 @@ static int etf_enqueue_timesortedlist(struct sk_buff *nskb, struct Qdisc *sch,
ktime_t txtime = nskb->tstamp;
bool leftmost = true;
+ if (unlikely(qdisc_backlog_overflow(sch, nskb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(nskb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
if (!is_packet_valid(sch, nskb)) {
report_sock_error(nskb, EINVAL,
SO_EE_CODE_TXTIME_INVALID_PARAM);
diff --git a/net/sched/sch_ets.c b/net/sched/sch_ets.c
index 6cc902a03838..7f74f79ea6b6 100644
--- a/net/sched/sch_ets.c
+++ b/net/sched/sch_ets.c
@@ -420,6 +420,12 @@ static int ets_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
struct ets_class *cl;
int err = 0;
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
cl = ets_classify(skb, sch, &err);
if (!cl) {
if (err & __NET_XMIT_BYPASS)
diff --git a/net/sched/sch_fifo.c b/net/sched/sch_fifo.c
index 1b6388d50967..53f159d5b723 100644
--- a/net/sched/sch_fifo.c
+++ b/net/sched/sch_fifo.c
@@ -29,6 +29,12 @@ static int bfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch,
static int pfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch,
struct sk_buff **to_free)
{
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
if (likely(sch->q.qlen < READ_ONCE(sch->limit)))
return qdisc_enqueue_tail(skb, sch);
@@ -43,6 +49,12 @@ static int pfifo_tail_enqueue(struct sk_buff *skb, struct Qdisc *sch,
if (unlikely(READ_ONCE(sch->limit) == 0))
return qdisc_drop(skb, sch, to_free);
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
if (likely(sch->q.qlen < READ_ONCE(sch->limit)))
return qdisc_enqueue_tail(skb, sch);
diff --git a/net/sched/sch_fq.c b/net/sched/sch_fq.c
index a282812c192e..03d06b65ed03 100644
--- a/net/sched/sch_fq.c
+++ b/net/sched/sch_fq.c
@@ -628,6 +628,12 @@ static int fq_enqueue(struct sk_buff *skb, struct Qdisc *sch,
u64 now;
u8 band;
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
band = fq_prio2band(q->prio2band, skb->priority & TC_PRIO_MAX);
if (unlikely(q->band_pkt_count[band] >= sch->limit)) {
q->stat_band_drops[band]++;
diff --git a/net/sched/sch_fq_codel.c b/net/sched/sch_fq_codel.c
index e0fcda26ff62..5feb50db41e8 100644
--- a/net/sched/sch_fq_codel.c
+++ b/net/sched/sch_fq_codel.c
@@ -201,6 +201,15 @@ static int fq_codel_enqueue(struct sk_buff *skb, struct Qdisc *sch,
}
idx--;
+ /* Drop before the flow is touched, so a dropped packet cannot
+ * create or update flow state.
+ */
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ q->drop_overlimit++;
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
codel_set_enqueue_time(skb);
flow = &q->flows[idx];
flow_queue_add(flow, skb);
diff --git a/net/sched/sch_fq_pie.c b/net/sched/sch_fq_pie.c
index 5982847df8f8..9f927a08b7bc 100644
--- a/net/sched/sch_fq_pie.c
+++ b/net/sched/sch_fq_pie.c
@@ -155,7 +155,8 @@ static int fq_pie_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
memory_limited = q->memory_usage > q->memory_limit + skb->truesize;
/* Checks if the qdisc is full */
- if (unlikely(qdisc_qlen(sch) >= sch->limit)) {
+ if (unlikely(qdisc_qlen(sch) >= sch->limit) ||
+ unlikely(qdisc_backlog_overflow(sch, skb))) {
q->stats.overlimit++;
goto out;
} else if (unlikely(memory_limited)) {
diff --git a/net/sched/sch_generic.c b/net/sched/sch_generic.c
index d2e0d20f3ba1..dcf7dc7d20a3 100644
--- a/net/sched/sch_generic.c
+++ b/net/sched/sch_generic.c
@@ -824,6 +824,15 @@ static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
unsigned int pkt_len = qdisc_pkt_len(skb);
int err;
+ /* qdisc_clear_nolock() can strip TCQ_F_CPUSTATS on graft, after which
+ * this books the shared (u32) backlog; guard it like the others.
+ */
+ if (unlikely(qdisc_backlog_overflow(qdisc, skb))) {
+ qdisc_qstats_overlimit(qdisc);
+ return qdisc_drop_reason(skb, qdisc, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
err = skb_array_produce(q, skb);
if (unlikely(err)) {
diff --git a/net/sched/sch_gred.c b/net/sched/sch_gred.c
index f3d9a7252949..de96badff654 100644
--- a/net/sched/sch_gred.c
+++ b/net/sched/sch_gred.c
@@ -170,6 +170,15 @@ static int gred_enqueue(struct sk_buff *skb, struct Qdisc *sch,
unsigned long qavg = 0;
u16 dp = tc_index_to_dp(skb);
+ /* Per-VQ limits don't bound the total: every VQ adds into the
+ * same u32 backlog, so check it before picking a VQ.
+ */
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
dp = t->def;
diff --git a/net/sched/sch_hfsc.c b/net/sched/sch_hfsc.c
index 284490fd6ca9..c11457e6cc24 100644
--- a/net/sched/sch_hfsc.c
+++ b/net/sched/sch_hfsc.c
@@ -1564,6 +1564,12 @@ hfsc_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
int err;
bool first;
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
cl = hfsc_classify(skb, sch, &err);
if (cl == NULL) {
if (err & __NET_XMIT_BYPASS)
diff --git a/net/sched/sch_hhf.c b/net/sched/sch_hhf.c
index fca20c50c97c..a3d1e7c850e6 100644
--- a/net/sched/sch_hhf.c
+++ b/net/sched/sch_hhf.c
@@ -373,9 +373,16 @@ static int hhf_enqueue(struct sk_buff *skb, struct Qdisc *sch,
struct sk_buff **to_free)
{
struct hhf_sched_data *q = qdisc_priv(sch);
- enum wdrr_bucket_idx idx;
+ unsigned int len = qdisc_pkt_len(skb);
struct wdrr_bucket *bucket;
unsigned int prev_backlog;
+ enum wdrr_bucket_idx idx;
+
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
idx = hhf_classify(skb, sch);
@@ -409,8 +416,15 @@ static int hhf_enqueue(struct sk_buff *skb, struct Qdisc *sch,
/* Return Congestion Notification only if we dropped a packet from this
* bucket.
*/
- if (hhf_drop(sch, to_free) == idx)
+ if (hhf_drop(sch, to_free) == idx) {
+ /* The parent does not book this packet on CN, so pass the net
+ * change on instead.
+ */
+ qdisc_tree_reduce_backlog(sch, 0,
+ prev_backlog - sch->qstats.backlog -
+ len);
return NET_XMIT_CN;
+ }
/* As we dropped a packet, better let upper stack know this. */
qdisc_tree_reduce_backlog(sch, 1, prev_backlog - sch->qstats.backlog);
diff --git a/net/sched/sch_htb.c b/net/sched/sch_htb.c
index 1ba67b121de4..6d62feaaa218 100644
--- a/net/sched/sch_htb.c
+++ b/net/sched/sch_htb.c
@@ -625,10 +625,20 @@ static inline void htb_deactivate(struct htb_sched *q, struct htb_class *cl)
static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch,
struct sk_buff **to_free)
{
- int ret;
- unsigned int len = qdisc_pkt_len(skb);
struct htb_sched *q = qdisc_priv(sch);
- struct htb_class *cl = htb_classify(skb, sch, &ret);
+ unsigned int len = qdisc_pkt_len(skb);
+ struct htb_class *cl;
+ int ret;
+
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ /* htb_dump() copies this over sch->qstats.overlimits. */
+ WRITE_ONCE(q->overlimits, q->overlimits + 1);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
+ cl = htb_classify(skb, sch, &ret);
if (cl == HTB_DIRECT) {
/* enqueue to helper queue */
diff --git a/net/sched/sch_netem.c b/net/sched/sch_netem.c
index 1dc2924890eb..c25ce4c3175e 100644
--- a/net/sched/sch_netem.c
+++ b/net/sched/sch_netem.c
@@ -467,6 +467,12 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
unsigned int prev_len = qdisc_pkt_len(skb);
int count = 1;
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
/* Do not fool qdisc_drop_all() */
skb->prev = NULL;
@@ -567,7 +573,19 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
struct Qdisc *rootq = qdisc_root_bh(sch);
skb2->tc_depth++; /* prevent duplicating a dup... */
+ /* The clone re-enters the tree before this packet is booked,
+ * so every guard it reaches would compare against a backlog
+ * that does not hold it yet and admit two packets where it
+ * has room for one. Account it here and undo, because the
+ * normal path below books it again.
+ */
+ qstats_backlog_add(sch, prev_len);
+ qdisc_qlen_inc(sch);
+ qdisc_tree_reduce_backlog(sch, 0, -(int)prev_len);
rootq->enqueue(skb2, rootq, to_free);
+ qdisc_tree_reduce_backlog(sch, 0, prev_len);
+ qstats_backlog_sub(sch, prev_len);
+ qdisc_qlen_dec(sch);
skb2 = NULL;
}
diff --git a/net/sched/sch_pie.c b/net/sched/sch_pie.c
index 3b7863ffd284..2a5fc493a7e5 100644
--- a/net/sched/sch_pie.c
+++ b/net/sched/sch_pie.c
@@ -89,7 +89,8 @@ static int pie_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
struct pie_sched_data *q = qdisc_priv(sch);
bool enqueue = false;
- if (unlikely(qdisc_qlen(sch) >= sch->limit)) {
+ if (unlikely(qdisc_qlen(sch) >= sch->limit) ||
+ unlikely(qdisc_backlog_overflow(sch, skb))) {
WRITE_ONCE(q->stats.overlimit, q->stats.overlimit + 1);
goto out;
}
diff --git a/net/sched/sch_plug.c b/net/sched/sch_plug.c
index b60ddfee6a68..5654e5310fb5 100644
--- a/net/sched/sch_plug.c
+++ b/net/sched/sch_plug.c
@@ -89,7 +89,8 @@ static int plug_enqueue(struct sk_buff *skb, struct Qdisc *sch,
{
struct plug_sched_data *q = qdisc_priv(sch);
- if (likely((u64)sch->qstats.backlog + skb->len <= q->limit)) {
+ if (likely((u64)sch->qstats.backlog + qdisc_pkt_len(skb) <=
+ q->limit)) {
if (!q->unplug_indefinite)
q->pkts_current_epoch++;
return qdisc_enqueue_tail(skb, sch);
diff --git a/net/sched/sch_prio.c b/net/sched/sch_prio.c
index 79437c587e7e..11f9354f57c2 100644
--- a/net/sched/sch_prio.c
+++ b/net/sched/sch_prio.c
@@ -72,6 +72,12 @@ prio_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
struct Qdisc *qdisc;
int ret;
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
qdisc = prio_classify(skb, sch, &ret);
#ifdef CONFIG_NET_CLS_ACT
if (qdisc == NULL) {
diff --git a/net/sched/sch_qfq.c b/net/sched/sch_qfq.c
index e900890e9c54..8e0b2702669e 100644
--- a/net/sched/sch_qfq.c
+++ b/net/sched/sch_qfq.c
@@ -1236,6 +1236,12 @@ static int qfq_enqueue(struct sk_buff *skb, struct Qdisc *sch,
struct qfq_aggregate *agg;
int err = 0;
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
cl = qfq_classify(skb, sch, &err);
if (cl == NULL) {
if (err & __NET_XMIT_BYPASS)
diff --git a/net/sched/sch_red.c b/net/sched/sch_red.c
index d7598214270b..738868f08a2e 100644
--- a/net/sched/sch_red.c
+++ b/net/sched/sch_red.c
@@ -76,6 +76,16 @@ static int red_enqueue(struct sk_buff *skb, struct Qdisc *sch,
unsigned int len;
int ret;
+ /* RED feeds the child backlog into red_calc_qavg() and books the
+ * packet on its own counter, so both must have room for it.
+ */
+ if (unlikely(qdisc_backlog_overflow(sch, skb) ||
+ qdisc_backlog_overflow(child, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
q->vars.qavg = red_calc_qavg(&q->parms,
&q->vars,
child->qstats.backlog);
diff --git a/net/sched/sch_sfb.c b/net/sched/sch_sfb.c
index ed39869199c0..7e8edb481edc 100644
--- a/net/sched/sch_sfb.c
+++ b/net/sched/sch_sfb.c
@@ -296,6 +296,12 @@ static int sfb_enqueue(struct sk_buff *skb, struct Qdisc *sch,
u32 slot = q->slot;
int ret = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
if (unlikely(sch->q.qlen >= q->limit)) {
qdisc_qstats_overlimit(sch);
WRITE_ONCE(q->stats.queuedrop,
diff --git a/net/sched/sch_sfq.c b/net/sched/sch_sfq.c
index 8bbcfc9e85d9..ef16ca17bd11 100644
--- a/net/sched/sch_sfq.c
+++ b/net/sched/sch_sfq.c
@@ -351,6 +351,12 @@ sfq_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
struct sk_buff *head;
int delta;
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
hash = sfq_classify(skb, sch, &ret);
if (hash == 0) {
if (ret & __NET_XMIT_BYPASS)
@@ -427,7 +433,7 @@ sfq_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
/* We know we have at least one packet in queue */
head = slot_dequeue_head(slot);
delta = qdisc_pkt_len(head) - qdisc_pkt_len(skb);
- qstats_backlog_sub(sch, delta);
+ qdisc_qstats_backlog_apply(sch, delta);
WRITE_ONCE(slot->backlog, slot->backlog - delta);
qdisc_drop_reason(head, sch, to_free, QDISC_DROP_FLOW_LIMIT);
diff --git a/net/sched/sch_skbprio.c b/net/sched/sch_skbprio.c
index 52abfb4015a3..688a786364a9 100644
--- a/net/sched/sch_skbprio.c
+++ b/net/sched/sch_skbprio.c
@@ -70,11 +70,18 @@ static int skbprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
{
const unsigned int max_priority = SKBPRIO_MAX_PRIORITY - 1;
struct skbprio_sched_data *q = qdisc_priv(sch);
- struct sk_buff_head *qdisc;
struct sk_buff_head *lp_qdisc;
+ struct sk_buff_head *qdisc;
+ unsigned int to_drop_len;
struct sk_buff *to_drop;
u16 prio, lp;
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
/* Obtain the priority of @skb. */
prio = min(skb->priority, max_priority);
@@ -113,10 +120,11 @@ static int skbprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
lp_qdisc = &q->qdiscs[lp];
to_drop = __skb_dequeue_tail(lp_qdisc);
BUG_ON(!to_drop);
+ to_drop_len = qdisc_pkt_len(to_drop);
qdisc_qstats_backlog_dec(sch, to_drop);
qdisc_drop(to_drop, sch, to_free);
- q->qstats[lp].backlog -= qdisc_pkt_len(to_drop);
+ q->qstats[lp].backlog -= to_drop_len;
q->qstats[lp].drops++;
q->qstats[lp].overlimits++;
@@ -133,6 +141,11 @@ static int skbprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
if (prio > q->highest_prio)
q->highest_prio = prio;
+ /* The parent does not book this packet on CN, so pass the net
+ * change on instead.
+ */
+ qdisc_tree_reduce_backlog(sch, 0, to_drop_len - qdisc_pkt_len(skb));
+
return NET_XMIT_CN;
}
diff --git a/net/sched/sch_taprio.c b/net/sched/sch_taprio.c
index 5a1dca9d5e9d..42364c785c83 100644
--- a/net/sched/sch_taprio.c
+++ b/net/sched/sch_taprio.c
@@ -123,6 +123,10 @@ struct taprio_sched {
u32 max_sdu[TC_MAX_QUEUE]; /* save info from the user */
u32 fp[TC_QOPT_MAX_QUEUE]; /* only for dump and offloading */
u32 txtime_delay;
+ /* Child whose net change the in-flight enqueue mirror is
+ * accounting; see taprio_enqueue_one().
+ */
+ struct Qdisc *mirror_child;
};
struct __tc_taprio_qopt_offload {
@@ -585,10 +589,36 @@ static bool taprio_skb_exceeds_queue_max_sdu(struct Qdisc *sch,
return exceeds;
}
+/* The child cannot correct us: taprio_graft() marks it TCQ_F_NOPARENT, so
+ * its qdisc_tree_reduce_backlog() stops there. Mirror its net change.
+ */
+static void taprio_mirror_child(struct Qdisc *sch, struct Qdisc *child,
+ u32 backlog_before, int qlen_before)
+{
+ /* apply() takes the amount to remove, so a child growth is negated
+ * and a child eviction adds.
+ */
+ qdisc_qstats_backlog_apply(sch,
+ -(int)(READ_ONCE(child->qstats.backlog) -
+ backlog_before));
+ WRITE_ONCE(sch->q.qlen,
+ sch->q.qlen + (int)(qdisc_qlen(child) - qlen_before));
+}
+
static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
struct Qdisc *child, struct sk_buff **to_free)
{
struct taprio_sched *q = qdisc_priv(sch);
+ struct Qdisc *mirror_prev;
+ u32 backlog;
+ int qlen;
+ int ret;
+
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
/* sk_flags are only safe to use on full sockets. */
if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) {
@@ -600,10 +630,23 @@ static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
return qdisc_drop(skb, sch, to_free);
}
- qdisc_qstats_backlog_inc(sch, skb);
- qdisc_qlen_inc(sch);
+ /* Snapshot before the handoff: the child may free skb (tbf_segment(),
+ * cake) or evict another packet. A netem duplicate re-enters this
+ * qdisc on the same child and books there, so only the outermost
+ * handoff to a child accounts.
+ */
+ if (q->mirror_child == child)
+ return qdisc_enqueue(skb, child, to_free);
+
+ mirror_prev = q->mirror_child;
+ q->mirror_child = child;
+ backlog = READ_ONCE(child->qstats.backlog);
+ qlen = qdisc_qlen(child);
+ ret = qdisc_enqueue(skb, child, to_free);
+ taprio_mirror_child(sch, child, backlog, qlen);
+ q->mirror_child = mirror_prev;
- return qdisc_enqueue(skb, child, to_free);
+ return ret;
}
static int taprio_enqueue_segmented(struct sk_buff *skb, struct Qdisc *sch,
@@ -620,10 +663,11 @@ static int taprio_enqueue_segmented(struct sk_buff *skb, struct Qdisc *sch,
return qdisc_drop(skb, sch, to_free);
skb_list_walk_safe(segs, segs, nskb) {
+ unsigned int seg_len = segs->len;
+
skb_mark_not_on_list(segs);
- qdisc_skb_cb(segs)->pkt_len = segs->len;
+ qdisc_skb_cb(segs)->pkt_len = seg_len;
qdisc_skb_cb(segs)->pkt_segs = 1;
- slen += segs->len;
/* FIXME: we should be segmenting to a smaller size
* rather than dropping these
@@ -633,10 +677,12 @@ static int taprio_enqueue_segmented(struct sk_buff *skb, struct Qdisc *sch,
else
ret = taprio_enqueue_one(segs, sch, child, to_free);
- if (ret != NET_XMIT_SUCCESS) {
- if (net_xmit_drop_count(ret))
- qdisc_qstats_drop(sch);
- } else {
+ /* Every drop above is already counted by the site that
+ * performs it; only keep track of the admitted segments
+ * here.
+ */
+ if (ret == NET_XMIT_SUCCESS) {
+ slen += seg_len;
numsegs++;
}
}
@@ -743,8 +789,10 @@ static struct sk_buff *taprio_dequeue_from_txq(struct Qdisc *sch, int txq,
struct net_device *dev = qdisc_dev(sch);
struct Qdisc *child = q->qdiscs[txq];
int num_tc = netdev_get_num_tc(dev);
- struct sk_buff *skb;
+ struct sk_buff *skb = NULL;
ktime_t guard;
+ u32 backlog;
+ int qlen;
int prio;
int len;
u8 tc;
@@ -752,18 +800,26 @@ static struct sk_buff *taprio_dequeue_from_txq(struct Qdisc *sch, int txq,
if (unlikely(child == &noop_qdisc))
return NULL;
+ /* Mirror the child's net change, not just the skb handed up: it can
+ * drop inside peek() and dequeue(), and cannot correct us itself.
+ */
+ backlog = READ_ONCE(child->qstats.backlog);
+ qlen = qdisc_qlen(child);
+
if (TXTIME_ASSIST_IS_ENABLED(q->flags))
goto skip_peek_checks;
skb = child->ops->peek(child);
if (!skb)
- return NULL;
+ goto out;
prio = skb->priority;
tc = netdev_get_prio_tc_map(dev, prio);
- if (!(gate_mask & BIT(tc)))
- return NULL;
+ if (!(gate_mask & BIT(tc))) {
+ skb = NULL;
+ goto out;
+ }
len = qdisc_pkt_len(skb);
guard = ktime_add_ns(taprio_get_time(q), length_to_duration(q, len));
@@ -772,22 +828,26 @@ static struct sk_buff *taprio_dequeue_from_txq(struct Qdisc *sch, int txq,
* guard band ...
*/
if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
- !taprio_entry_allows_tx(guard, entry, tc))
- return NULL;
+ !taprio_entry_allows_tx(guard, entry, tc)) {
+ skb = NULL;
+ goto out;
+ }
/* ... and no budget. */
if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
- taprio_update_budgets(entry, len, tc, num_tc) < 0)
- return NULL;
+ taprio_update_budgets(entry, len, tc, num_tc) < 0) {
+ skb = NULL;
+ goto out;
+ }
skip_peek_checks:
skb = qdisc_dequeue_peeked(child);
+out:
+ taprio_mirror_child(sch, child, backlog, qlen);
if (unlikely(!skb))
return NULL;
qdisc_bstats_update(sch, skb);
- qdisc_qstats_backlog_dec(sch, skb);
- qdisc_qlen_dec(sch);
return skb;
}
diff --git a/net/sched/sch_tbf.c b/net/sched/sch_tbf.c
index 58c9687efb0f..5a23b6749758 100644
--- a/net/sched/sch_tbf.c
+++ b/net/sched/sch_tbf.c
@@ -255,6 +255,12 @@ static int tbf_enqueue(struct sk_buff *skb, struct Qdisc *sch,
unsigned int len = qdisc_pkt_len(skb);
int ret;
+ if (unlikely(qdisc_backlog_overflow(sch, skb))) {
+ qdisc_qstats_overlimit(sch);
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_OVERLIMIT);
+ }
+
if (qdisc_pkt_len(skb) > q->max_size) {
if (skb_is_gso(skb) &&
skb_gso_validate_mac_len(skb, q->max_size))
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH net-next v13 4/4] net/sched: cake: hash before segmenting and reject an overlong split
2026-11-27 11:57 [PATCH net-next v13 0/4] net/sched: cap the accounted backlog before it can wrap Jamal Hadi Salim
` (2 preceding siblings ...)
2026-11-27 11:57 ` [PATCH net-next v13 3/4] net/sched: cap the accounted backlog before it can wrap Jamal Hadi Salim
@ 2026-11-27 11:57 ` Jamal Hadi Salim
2026-11-28 12:08 ` netdev-bot+sashiko
3 siblings, 1 reply; 8+ messages in thread
From: Jamal Hadi Salim @ 2026-11-27 11:57 UTC (permalink / raw)
To: netdev
Cc: Jamal Hadi Salim, David S . Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Jiri Pirko, Stephen Hemminger,
Vinicius Costa Gomes, Toke Høiland-Jørgensen, cake,
Victor Nogueira, Björn Töpel, Sashiko
The Fix: with CAKE_FLAG_SPLIT_GSO, cake computes the flow keys and then
splits a GSO skb into segments it accounts individually. Key extraction
must run on the unsegmented skb -- skb_gso_segment() can rewrite
encapsulated headers to the inner ones -- and resolution must wait until
admission, since it commits set-associative state a later rejection would
leave mutated.
Split cake_hash() into a side-effect-free cake_hash_keys() and a
cake_hash_resolve() that runs only on admission, move the split-GSO path
ahead of the resolution, and sum the list against the ceiling before
linking any segment. A split an ancestor cannot absorb is refused, because
qdisc_tree_reduce_backlog() would grow it without its enqueue guard seeing
the sum. The list is deferred to free so the tail segment's sock_free()
never runs under the qdisc lock.
Some context: tag every deferred segment with QDISC_DROP_OVERLIMIT --
skb_gso_segment() keeps the control block at cb offset 32, aliasing the
qdisc drop reason into every segment, so tagging only the head left the
rest with a headroom-sized value. Read the flow mode once with READ_ONCE()
so a concurrent cake_mq reconfiguration cannot extract and resolve in
different modes.
Conditions to recreate the bug:
ip tuntap add tun0 mode tun
ip link set tun0 txqueuelen 32 up
ip addr add 10.99.0.1/24 dev tun0
# hold the tun fd open without reading so the backlog persists; a stab
# overhead of 1048468 accounts each 92-byte packet at 1048560 bytes.
# 4095 queued packets are already above QDISC_MAX_BACKLOG, so the
# enqueue guard drops the GSO skb before the split is tried; 4094
# packets leave 2162655 bytes below U32_MAX, so only a segment sum above
# that reaches the split check and cake drops the whole list. A
# 65000-byte UDP_SEGMENT payload with a 1400-byte segment size sums to
# 66316 bytes and passes. Setup needs CAP_NET_ADMIN in a user namespace
# of its own (unshare -Urn); the payload itself does not.
tc qdisc add dev tun0 root handle 1: stab overhead 1048468 \
cake split-gso memlimit 512Mb
# Fill the queue to the window with 4094 92-byte frames (1048560
# accounted bytes each) and then send the split:
python3 -c 'import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 33554432)
for _ in range(4094):
s.sendto(b"A" * 64, ("10.99.0.2", 9))'
# A 65000-byte UDP_SEGMENT datagram with a 1400-byte segment size sums
# to 66316 accounted bytes and is admitted; the refusal needs a segment
# sum above the 2162655 bytes left below U32_MAX, which this example
# does not reach. The clause is a defensive bound, not a proven one:
# the shape that can reach it is an encapsulated trusted TSO with a small
# inner MSS, where the initializer replicates outer and inner headers on
# every segment.
python3 -c 'import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.IPPROTO_UDP, socket.UDP_SEGMENT, 1400)
s.sendto(b"A" * 65000, ("10.99.0.2", 9))'
Link: https://lore.kernel.org/netdev/179231933068.3135.80340792633077986@kernel.org/
Link: https://lore.kernel.org/netdev/179269577270.3135.16072065138845192450@kernel.org/
Reviewed-by: Victor Nogueira <victor@mojatatu.com>
Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
---
v12 -> v13:
- refuse a split an ancestor cannot absorb.
- state the shape that reaches the check in Conditions.
- drop Toke's Acked-by: the split path and its ceiling check changed
substantially; he stays in the Cc.
- shorten the commit log; the v12 body read like a changelog.
(Note to the sashikos: reduced because humans complained.)
v11 -> v12:
- compare the split's segment sum against U32_MAX, the wrap boundary that
qdisc_backlog_overflow() uses, instead of the post-add ceiling, which
rejected a split up to two packet lengths before it could wrap.
(Sashiko nipa review of v11)
---
net/sched/sch_cake.c | 135 +++++++++++++++++++++++++++++++------------
1 file changed, 99 insertions(+), 36 deletions(-)
diff --git a/net/sched/sch_cake.c b/net/sched/sch_cake.c
index 25c7e20ec123..f22d9bcad077 100644
--- a/net/sched/sch_cake.c
+++ b/net/sched/sch_cake.c
@@ -709,19 +709,28 @@ static u16 cake_get_flow_quantum(struct cake_tin_data *q,
get_random_u16()) >> 16;
}
-static u32 cake_hash(struct cake_tin_data *q, const struct sk_buff *skb,
- int flow_mode, u16 flow_override, u16 host_override)
+struct cake_hash_keys {
+ u32 srchost_hash;
+ u32 dsthost_hash;
+ u32 flow_hash;
+};
+
+static void cake_hash_keys(const struct sk_buff *skb, int flow_mode,
+ u16 flow_override, u16 host_override,
+ struct cake_hash_keys *out)
{
bool hash_flows = (!flow_override && !!(flow_mode & CAKE_FLOW_FLOWS));
bool hash_hosts = (!host_override && !!(flow_mode & CAKE_FLOW_HOSTS));
bool nat_enabled = !!(flow_mode & CAKE_FLOW_NAT_FLAG);
- u32 flow_hash = 0, srchost_hash = 0, dsthost_hash = 0;
- u16 reduced_hash, srchost_idx, dsthost_idx;
struct flow_keys keys, host_keys;
bool use_skbhash = skb->l4_hash;
+ out->flow_hash = 0;
+ out->srchost_hash = 0;
+ out->dsthost_hash = 0;
+
if (unlikely(flow_mode == CAKE_FLOW_NONE))
- return 0;
+ return;
/* If both overrides are set, or we can use the SKB hash and nat mode is
* disabled, we can skip packet dissection entirely. If nat mode is
@@ -756,50 +765,64 @@ static u32 cake_hash(struct cake_tin_data *q, const struct sk_buff *skb,
switch (host_keys.control.addr_type) {
case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
host_keys.addrs.v4addrs.src = 0;
- dsthost_hash = flow_hash_from_keys(&host_keys);
+ out->dsthost_hash = flow_hash_from_keys(&host_keys);
host_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
host_keys.addrs.v4addrs.dst = 0;
- srchost_hash = flow_hash_from_keys(&host_keys);
+ out->srchost_hash = flow_hash_from_keys(&host_keys);
break;
case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
memset(&host_keys.addrs.v6addrs.src, 0,
sizeof(host_keys.addrs.v6addrs.src));
- dsthost_hash = flow_hash_from_keys(&host_keys);
+ out->dsthost_hash = flow_hash_from_keys(&host_keys);
host_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
memset(&host_keys.addrs.v6addrs.dst, 0,
sizeof(host_keys.addrs.v6addrs.dst));
- srchost_hash = flow_hash_from_keys(&host_keys);
+ out->srchost_hash = flow_hash_from_keys(&host_keys);
break;
default:
- dsthost_hash = 0;
- srchost_hash = 0;
+ out->dsthost_hash = 0;
+ out->srchost_hash = 0;
}
/* This *must* be after the above switch, since as a
* side-effect it sorts the src and dst addresses.
*/
if (hash_flows && !use_skbhash)
- flow_hash = flow_hash_from_keys(&keys);
+ out->flow_hash = flow_hash_from_keys(&keys);
skip_hash:
if (flow_override)
- flow_hash = flow_override - 1;
+ out->flow_hash = flow_override - 1;
else if (use_skbhash && (flow_mode & CAKE_FLOW_FLOWS))
- flow_hash = skb->hash;
+ out->flow_hash = skb->hash;
if (host_override) {
- dsthost_hash = host_override - 1;
- srchost_hash = host_override - 1;
+ out->dsthost_hash = host_override - 1;
+ out->srchost_hash = host_override - 1;
}
if (!(flow_mode & CAKE_FLOW_FLOWS)) {
if (flow_mode & CAKE_FLOW_SRC_IP)
- flow_hash ^= srchost_hash;
+ out->flow_hash ^= out->srchost_hash;
if (flow_mode & CAKE_FLOW_DST_IP)
- flow_hash ^= dsthost_hash;
+ out->flow_hash ^= out->dsthost_hash;
}
+}
+
+/* Commit the flow into the set-associative table (q->tags[], host
+ * indices, bulk counters). Only after the packet is admitted.
+ */
+static u32 cake_hash_resolve(struct cake_tin_data *q,
+ const struct cake_hash_keys *keys, int flow_mode)
+{
+ u32 flow_hash = keys->flow_hash, srchost_hash = keys->srchost_hash,
+ dsthost_hash = keys->dsthost_hash;
+ u16 reduced_hash, srchost_idx, dsthost_idx;
+
+ if (unlikely(flow_mode == CAKE_FLOW_NONE))
+ return 0;
reduced_hash = flow_hash % CAKE_QUEUES;
@@ -1767,13 +1790,17 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
{
u32 idx, tin, prev_qlen, prev_backlog, drop_id;
struct cake_sched_data *q = qdisc_priv(sch);
+ int flow_mode = READ_ONCE(q->config->flow_mode);
int len = qdisc_pkt_len(skb), ret;
u16 flow_override, host_override;
+ struct sk_buff *segs = NULL;
+ struct cake_hash_keys keys;
struct sk_buff *ack = NULL;
ktime_t now = ktime_get();
struct cake_tin_data *b;
struct cake_flow *flow;
bool same_flow = false;
+ u64 slen = 0;
/* Classify first: a terminal verdict means do not enqueue. */
if (cake_tcf_classify(sch, skb, &flow_override, &host_override, &ret)) {
@@ -1790,12 +1817,54 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
QDISC_DROP_OVERLIMIT);
}
- /* choose flow to insert into */
+ /* Select the tin and extract the keys before segmenting:
+ * skb_gso_segment() rewrites encapsulated headers, which
+ * changes the keys.
+ */
b = cake_select_tin(sch, skb);
- idx = cake_hash(b, skb, q->config->flow_mode, flow_override,
- host_override) + 1;
+ cake_hash_keys(skb, flow_mode, flow_override, host_override, &keys);
+
+ if (qdisc_pkt_segs(skb) > 1 &&
+ q->config->rate_flags & CAKE_FLAG_SPLIT_GSO) {
+ netdev_features_t features = netif_skb_features(skb);
+ struct sk_buff *nskb, *seg;
+
+ segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
+ if (IS_ERR_OR_NULL(segs))
+ return qdisc_drop_reason(skb, sch, to_free,
+ QDISC_DROP_GENERIC);
+
+ /* we never use the original skb after splitting */
+ consume_skb(skb);
+ skb = NULL;
+
+ /* The split path accounts the segment sum, not the
+ * stab-adjusted qdisc_pkt_len(), so check that sum
+ * against the same wrap boundary as the enqueue guard.
+ */
+ skb_list_walk_safe(segs, seg, nskb)
+ slen += seg->len;
+
+ if (unlikely((u64)sch->qstats.backlog + slen > U32_MAX ||
+ (slen > (u64)len &&
+ !qdisc_ancestors_backlog_room(sch, slen - len)))) {
+ qdisc_qstats_overlimit(sch);
+ /* __skb_gso_segment() puts SKB_GSO_CB at cb
+ * offset 32, over tc_skb_cb.drop_reason; every
+ * segment inherits it. Tag each one with the
+ * qdisc drop reason.
+ */
+ skb_list_walk_safe(segs, seg, nskb)
+ tcf_set_qdisc_drop_reason(seg,
+ QDISC_DROP_OVERLIMIT);
+
+ return qdisc_drop_all(segs, sch, to_free);
+ }
+ }
+
+ /* Admitted: resolve the flow. */
+ idx = cake_hash_resolve(b, &keys, flow_mode);
tin = (u32)(b - q->tins);
- idx--;
flow = &b->flows[idx];
/* ensure shaper state isn't stale */
@@ -1822,14 +1891,9 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
if (unlikely(len > b->max_skblen))
WRITE_ONCE(b->max_skblen, len);
- if (qdisc_pkt_segs(skb) > 1 && q->config->rate_flags & CAKE_FLAG_SPLIT_GSO) {
- struct sk_buff *segs, *nskb, *seg;
- netdev_features_t features = netif_skb_features(skb);
- unsigned int slen = 0, numsegs = 0;
-
- segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
- if (IS_ERR_OR_NULL(segs))
- return qdisc_drop(skb, sch, to_free);
+ if (segs) {
+ struct sk_buff *nskb, *seg;
+ unsigned int numsegs = 0;
skb_list_walk_safe(segs, seg, nskb) {
skb_mark_not_on_list(seg);
@@ -1843,7 +1907,6 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
qdisc_qlen_inc(sch);
numsegs++;
q->buffer_used += seg->truesize;
- slen += seg->len;
WRITE_ONCE(b->packets, b->packets + 1);
}
@@ -1855,9 +1918,8 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
WRITE_ONCE(b->backlogs[idx], b->backlogs[idx] + slen);
qdisc_tree_reduce_backlog(sch, 1-numsegs, len-slen);
- consume_skb(skb);
} else {
- /* not splitting */
+ /* not splitting - skb pointer still valid */
int ack_pkt_len = 0;
cobalt_set_enqueue_time(skb, now);
@@ -1951,7 +2013,8 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
flow->set = CAKE_SET_SPARSE;
WRITE_ONCE(b->sparse_flow_count, b->sparse_flow_count + 1);
- WRITE_ONCE(flow->deficit, cake_get_flow_quantum(b, flow, q->config->flow_mode));
+ WRITE_ONCE(flow->deficit,
+ cake_get_flow_quantum(b, flow, flow_mode));
} else if (flow->set == CAKE_SET_SPARSE_WAIT) {
/* this flow was empty, accounted as a sparse flow, but actually
* in the bulk rotation.
@@ -1960,8 +2023,8 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
WRITE_ONCE(b->sparse_flow_count, b->sparse_flow_count - 1);
WRITE_ONCE(b->bulk_flow_count, b->bulk_flow_count + 1);
- cake_inc_srchost_bulk_flow_count(b, flow, q->config->flow_mode);
- cake_inc_dsthost_bulk_flow_count(b, flow, q->config->flow_mode);
+ cake_inc_srchost_bulk_flow_count(b, flow, flow_mode);
+ cake_inc_dsthost_bulk_flow_count(b, flow, flow_mode);
}
if (q->buffer_used > u64_stats_read(&q->buffer_max_used)) {
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH net-next v13 3/4] net/sched: cap the accounted backlog before it can wrap
2026-11-27 11:57 ` [PATCH net-next v13 3/4] net/sched: cap the accounted backlog before it can wrap Jamal Hadi Salim
@ 2026-11-28 12:08 ` netdev-bot+sashiko
2026-11-28 16:26 ` Jamal Hadi Salim
0 siblings, 1 reply; 8+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 12:08 UTC (permalink / raw)
To: jhs
Cc: netdev, davem, edumazet, kuba, pabeni, horms, jiri, stephen,
vinicius.gomes, toke, victor, bjorn, sashiko-bot
Thank you for your contribution! Sashiko AI review found 8 potential
issue(s) to consider.
Critical: 0 · High: 2 · Medium: 5 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] Signed reinterpretation in qdisc_tree_reduce_backlog() can
blackhole the parent qdisc for good.
- [High] taprio's backlog/qlen can leak upward through control-path child
drops, and the new guard then drops every packet.
- [Medium] The saturating reconciliation breaks reduce-before-book
accounting and leaves a phantom backlog.
- [Medium] The offload exemption depends on a flag that is cleared on
every stats dump, while the driver still writes non-saturating…
- [Medium] The core saturation also runs on offloaded ancestors and
breaks the drivers' signed-delta hardware accounting.
- [Medium] Segmenting children other than cake still grow ancestors
without the room check.
- [Medium] The commit message misdescribes some changes and leaves out
several separate behavioural changes.
- [Low] No Fixes: tag.
--
net/sched: cap the accounted backlog before it can wrap
This patch adds an enqueue-time guard, qdisc_backlog_overflow(), across
the net/sched qdiscs. The guard drops a packet once the 32-bit
sch->qstats.backlog can no longer hold its accounted length. It also makes
the core backlog subtraction saturate, routes qdisc_tree_reduce_backlog()
and sfq through a new signed qdisc_qstats_backlog_apply(), and reworks
taprio to mirror its children's backlog changes.
Closes: https://lore.kernel.org/netdev/20260818101130.16203-1-jhs@mojatatu.com/
Link: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260818101130.16203-1-jhs@mojatatu.com
Link: https://lore.kernel.org/netdev/CANn89iLfMJV7ancKH1Gjzzm7ZUG-gKcrczkJjJEWN5sCuWd-ug@mail.gmail.com/
Link: https://lore.kernel.org/netdev/CANn89i+GOFH_8g+vSV0jUj0aBqLYVWuFOrHuDggZu9kPC_m6tQ@mail.gmail.com/
Link: https://lore.kernel.org/netdev/87a4oottcj.fsf@toke.dk/
Link: https://lore.kernel.org/netdev/87pkxks68f.fsf@toke.dk/
Link: https://lore.kernel.org/netdev/179231932946.3135.7497478990186385688@kernel.org/
Link: https://lore.kernel.org/netdev/179269577131.3135.3967915390403138015@kernel.org/
> This is a followup of commit 709f34f7c28d ("net/sched: fq: add overflow
> bounds to quantum and initial quantum") as reported by Sashiko.
[Severity: Low]
This isn't a bug, but should this have a Fixes: tag for the commit that
introduced the backlog wrap, and possibly a stable Cc?
709f34f7c28d is an fq quantum change, not the source of the wrap. The tag
block only has Reported-by, Closes, Link and Signed-off-by. The separate
hhf/skbprio and plug fixes bundled here don't have Fixes: tags either.
[ ... ]
> Some context: qdisc_pkt_len() stays truthful for every input, so BPF
> wire_len, act_police and the HTB/TBF shapers read the real length. RED
> books the packet on success again, as the tree did before v11.
[Severity: Medium]
Is the RED sentence accurate for the tree this applies to? The baseline
red_enqueue() already books the packet on success:
ret = qdisc_enqueue(skb, child, to_free);
if (likely(ret == NET_XMIT_SUCCESS)) {
qstats_backlog_add(sch, len);
qdisc_qlen_inc(sch);
The RED hunk in this patch only adds the new guard. The references to v11,
"stays truthful" and "at dequeue as well" only make sense relative to
earlier postings of the series.
The message also leaves out several behavioural changes in the diff:
hhf_enqueue() and skbprio_enqueue() now call qdisc_tree_reduce_backlog()
on the NET_XMIT_CN path. This fixes a separate, pre-existing drift in
the parent backlog.
plug_enqueue() now checks its limit against qdisc_pkt_len() instead of
skb->len, which changes admission for stab and GSO users.
taprio_enqueue_segmented() no longer adds child drops to taprio's own
drop counter.
netem_enqueue() now pre-books qlen before it re-injects the duplicate,
so a duplicate near sch->limit can now be dropped where it used to be
admitted.
Could these be described in the commit message, or split out?
> diff --git a/include/net/sch_generic.h b/include/net/sch_generic.h
> index 1acaadb3e2cf0..677cd089ad5d3 100644
> --- a/include/net/sch_generic.h
> +++ b/include/net/sch_generic.h
> @@ -735,6 +735,12 @@ void qdisc_destroy(struct Qdisc *qdisc);
> void qdisc_put(struct Qdisc *qdisc);
> void qdisc_put_unlocked(struct Qdisc *qdisc);
> void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
> +/* Does every ancestor of @sch have room for @extra more bytes below its
> + * enqueue ceiling? A child that turns one skb into several reconciles the
> + * difference through qdisc_tree_reduce_backlog(), growth that never passes
> + * the ancestors' own enqueue guard.
> + */
> +bool qdisc_ancestors_backlog_room(const struct Qdisc *sch, u64 extra);
[Severity: Medium]
Who calls this helper at this commit? It is exported but has no caller
here. Later in the series, "net/sched: cake: hash before segmenting and
reject an overlong split" adds a call in cake_enqueue(), and that is the
only caller.
tbf_segment() and the netem segment path also turn one skb into several,
and they grow the ancestors without this room check:
net/sched/sch_tbf.c:tbf_segment() {
...
qstats_backlog_add(sch, len);
if (nb > 0) {
qdisc_tree_reduce_backlog(sch, 1 - nb, prev_len - len);
...
}
net/sched/sch_netem.c:netem_enqueue() {
...
qdisc_tree_reduce_backlog(sch, -(nb - 1), -(len - prev_len));
...
}
The QDISC_MAX_BACKLOG comment says "a single enqueue can never run out of
headroom before it is accounted". That only holds if this growth stays
under about 2 MiB.
Could a stab table that maps a large encapsulated BIG TCP aggregate to
just above tbf's max_size push the segment sum past that headroom? That
would wrap tbf's counter or an ancestor's.
The later cake patch calls this bound defensive rather than proven, so
this may be hard to reach. It also doesn't match the commit message claim
that the ceiling covers "every qdisc in net/sched that adds the incoming
qdisc_pkt_len() to its own backlog".
> @@ -915,6 +921,37 @@ static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
> return qdisc_skb_cb(skb)->pkt_len;
> }
>
> +#define QDISC_PKT_LEN_MAX (1 << 20) /* 1 MiB */
> +
> +/* Largest backlog that still has room for a 1 MiB packet and a netem
> + * duplicate of it, so that a single enqueue can never run out of
> + * headroom before it is accounted.
> + */
> +#define QDISC_MAX_BACKLOG (U32_MAX - 2 * QDISC_PKT_LEN_MAX)
[ ... ]
> +static inline bool qdisc_backlog_overflow(const struct Qdisc *sch,
> + const struct sk_buff *skb)
> +{
> + u32 len = qdisc_pkt_len(skb);
> + u32 backlog;
> +
> + if (unlikely(READ_ONCE(sch->flags) & TCQ_F_OFFLOADED))
> + return false;
[Severity: Medium]
Can the qdisc lose this exemption while its counter still carries
hardware bytes? qdisc_offload_dump_helper() clears the flag around every
stats query and sets it again only on success:
net/sched/sch_api.c:qdisc_offload_dump_helper() {
...
sch->flags &= ~TCQ_F_OFFLOADED;
if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
return 0;
err = dev->netdev_ops->ndo_setup_tc(dev, type, type_data);
...
if (!err)
sch->flags |= TCQ_F_OFFLOADED;
...
}
This runs from dump callbacks such as red_dump()->red_dump_offload_stats()
with only RTNL held, because tc_fill_qdisc() calls q->ops->dump() without
the qdisc root lock.
Inside that window, mlxsw updates the counter with plain u32 arithmetic.
That races with the datapath's saturating update:
mlxsw_sp_qdisc_update_stats():
stats_ptr->qstats->backlog += mlxsw_sp_cells_bytes(mlxsw_sp, backlog);
mlxsw_sp_qdisc_leaf_unoffload():
qstats->backlog -= backlog;
nfp_abm_stats_propagate() also writes hardware deltas without saturating.
The new comment in qdisc_qstats_backlog_dec() says a datapath update can
be lost here. If that happens and the hardware then drains, could the
driver's delta wrap the counter to near U32_MAX?
If so, enqueues during later dump windows would hit the ceiling. After an
unoffload, where the flag stays cleared, every enqueue would be dropped.
The TCQ_F_OFFLOADED check in qdisc_ancestors_backlog_room() has the same
window.
[ ... ]
> @@ -991,13 +1028,43 @@ static inline void qdisc_bstats_update(struct Qdisc *sch,
[ ... ]
> +static inline void qdisc_qstats_backlog_apply(struct Qdisc *sch, int delta)
> +{
> + u32 backlog = READ_ONCE(sch->qstats.backlog);
> + u64 next;
> +
> + if (delta >= 0)
> + next = backlog > (u32)delta ? backlog - (u32)delta : 0;
[Severity: Medium]
Should this clamp also apply to offloaded ancestors? Their counter
includes hardware deltas that the driver maintains.
mlxsw keeps the hardware share as a modular delta against a per-qdisc
baseline in mlxsw_sp_qdisc_update_stats(). When an offloaded child is
destroyed, it rebases the parent on the assumption that the core
subtracted exactly:
mlxsw_sp_qdisc_reduce_parent_backlog():
tmp->stats_base.backlog -= mlxsw_sp_qdisc->stats_base.backlog;
The parent and child baselines come from different dumps. The parent's
counter can therefore be lower than the child's, for example if the parent
was dumped while the tclass was empty and the child while it was full.
prio_graft()->qdisc_replace()->qdisc_purge_queue()->
qdisc_tree_reduce_backlog() then subtracts the child's backlog. The clamp
stops at 0, but the next parent dump still adds back the full hardware
share of the child.
Wouldn't the parent then stay too high by that amount for good?
mlxsw_sp_qdisc_leaf_unoffload() only removes stats_base, so the excess
would also carry over into the software-only qdisc.
> diff --git a/net/sched/sch_api.c b/net/sched/sch_api.c
> index f36c41224bccf..062e5d6f158eb 100644
> --- a/net/sched/sch_api.c
> +++ b/net/sched/sch_api.c
> @@ -877,13 +877,55 @@ void qdisc_tree_reduce_backlog(struct Qdisc *sch, int n, int len)
> cops->qlen_notify(sch, cl);
> }
> WRITE_ONCE(sch->q.qlen, sch->q.qlen - n);
> - qstats_backlog_sub(sch, len);
> + qdisc_qstats_backlog_apply(sch, len);
> __qdisc_qstats_drop(sch, drops);
[Severity: High]
Can a large but legitimate removal now be treated as growth here? len is
an int. qstats_backlog_sub() took it as a u32, so modular arithmetic got
removals of up to 4 GiB right. qdisc_qstats_backlog_apply() treats a
negative int as growth and saturates at U32_MAX.
Control-path callers pass unsigned byte counts through this int:
include/net/sch_generic.h:qdisc_purge_queue():
qdisc_tree_reduce_backlog(sch, qlen, backlog);
net/sched/sch_fq_codel.c:fq_codel_change():
unsigned int dropped_pkts = 0, dropped_bytes = 0;
...
qdisc_tree_reduce_backlog(sch, dropped_pkts, dropped_bytes);
The codel, fq, fq_pie, hhf, pie, sfq and cake ->change() handlers do the
same.
With the new ceiling, a child backlog between 2 GiB and about 4 GiB is a
reachable state. One setup: a root prio/htb/drr/ets/tbf/red, a stab
overhead as in the reproducer, and an fq_codel child filled to about
3 GiB. Then "tc qdisc change ... fq_codel limit 1", a class delete or a
child replace passes len > INT_MAX.
Would every ancestor then grow by 2^32 - len and stay pinned at U32_MAX?
>From then on, qdisc_backlog_overflow() sees backlog > QDISC_MAX_BACKLOG
and drops every packet with QDISC_DROP_OVERLIMIT. The children are empty,
so no dequeue ever brings the counter down. That is the "drop every packet
for good" outcome the new qdisc_qstats_backlog_dec() comment tries to
prevent.
[Severity: Medium]
Moving from modular subtraction to a clamp has a second effect. Some
children report a reduction to their ancestors before the parent has
booked the packet. tbf_segment() calls:
qdisc_tree_reduce_backlog(sch, 1 - nb, prev_len - len);
and then returns NET_XMIT_SUCCESS, after which the parent does
qstats_backlog_add(sch, prev_len). The netem_enqueue() segment path and
the cake_enqueue() split-GSO path use the same order.
With modular arithmetic, the parent ended up correct once it booked
prev_len. With the clamp, take B as the parent's current backlog and
d = prev_len - len. If B < d, the reduction stops at 0, the parent then
adds prev_len, and later dequeues remove only len.
Doesn't that leave a phantom backlog of d - B bytes with qlen 0?
This can happen with an idle htb/prio/drr parent over a tbf leaf, when
stab inflation (for example linklayer atm overhead) makes prev_len larger
than the segment sum, or when some segments are dropped.
red_calc_qavg(child->qstats.backlog) over such a classful child would
read the inflated value. The commit message presents the saturation as
purely protective.
[ ... ]
> diff --git a/net/sched/sch_taprio.c b/net/sched/sch_taprio.c
> index 5a1dca9d5e9de..42364c785c838 100644
> --- a/net/sched/sch_taprio.c
> +++ b/net/sched/sch_taprio.c
[ ... ]
> @@ -585,10 +589,36 @@ static bool taprio_skb_exceeds_queue_max_sdu(struct Qdisc *sch,
> return exceeds;
> }
>
> +/* The child cannot correct us: taprio_graft() marks it TCQ_F_NOPARENT, so
> + * its qdisc_tree_reduce_backlog() stops there. Mirror its net change.
> + */
> +static void taprio_mirror_child(struct Qdisc *sch, struct Qdisc *child,
> + u32 backlog_before, int qlen_before)
> +{
> + /* apply() takes the amount to remove, so a child growth is negated
> + * and a child eviction adds.
> + */
> + qdisc_qstats_backlog_apply(sch,
> + -(int)(READ_ONCE(child->qstats.backlog) -
> + backlog_before));
> + WRITE_ONCE(sch->q.qlen,
> + sch->q.qlen + (int)(qdisc_qlen(child) - qlen_before));
> +}
> +
> static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
> struct Qdisc *child, struct sk_buff **to_free)
> {
> struct taprio_sched *q = qdisc_priv(sch);
> + struct Qdisc *mirror_prev;
> + u32 backlog;
> + int qlen;
> + int ret;
> +
> + if (unlikely(qdisc_backlog_overflow(sch, skb))) {
> + qdisc_qstats_overlimit(sch);
> + return qdisc_drop_reason(skb, sch, to_free,
> + QDISC_DROP_OVERLIMIT);
> + }
[Severity: High]
What happens to taprio's counters when a child drops packets outside the
enqueue and dequeue windows?
taprio's backlog and qlen are now updated only from child deltas measured
around enqueue and dequeue. taprio_graft() and taprio_attach() set
TCQ_F_NOPARENT on every child, so qdisc_tree_reduce_backlog() stops at the
child:
net/sched/sch_api.c:qdisc_tree_reduce_backlog() {
...
if (sch->flags & TCQ_F_NOPARENT)
break;
...
}
A child's ->change() can drop queued packets without taprio seeing it.
Examples are fq_codel_change() and the codel, pie, fq_pie, hhf, fq, sfq
and cake equivalents, or a class delete inside a classful child.
Nothing removes those packets from taprio's sch->qstats.backlog or
sch->q.qlen. taprio_dump() does not resync from the children, and only a
qdisc_reset() of the root clears the counters.
Take a software-mode taprio with an fq_codel child on a txq and a stab
overhead that accounts each packet at about 1 MiB. Filling the child and
running "tc qdisc change dev X parent <taprio>:N fq_codel limit 1", over
and over, would keep adding leaked bytes. A graft goes through
dev_deactivate(), which resets the root, so a graft does not leak.
Once the leaked total passes QDISC_MAX_BACKLOG, won't this new guard drop
every packet on the device with QDISC_DROP_OVERLIMIT? The children are
empty, so nothing brings the counter back down.
Before this patch the drift only made the stats wrong. The guard now turns
it into a permanent drop.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/QDISC-BA27.v13.r0.20261127065026%40mojatatu.com
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH net-next v13 4/4] net/sched: cake: hash before segmenting and reject an overlong split
2026-11-27 11:57 ` [PATCH net-next v13 4/4] net/sched: cake: hash before segmenting and reject an overlong split Jamal Hadi Salim
@ 2026-11-28 12:08 ` netdev-bot+sashiko
0 siblings, 0 replies; 8+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 12:08 UTC (permalink / raw)
To: jhs
Cc: netdev, davem, edumazet, kuba, pabeni, horms, jiri, stephen,
vinicius.gomes, toke, victor, bjorn, sashiko-bot
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The subject "hash before segmenting" and the "The Fix:" paragraph
("Key extraction must run on the unsegmented skb -- skb_gso_segment()…
--
net/sched: cake: hash before segmenting and reject an overlong split
This patch splits cake_hash() into a side-effect-free cake_hash_keys()
and a cake_hash_resolve() that runs only after admission. It moves the
split-GSO segmentation ahead of the resolution and refuses a split whose
segment sum would push the backlog past U32_MAX, or which an ancestor
cannot absorb, tagging every dropped segment with QDISC_DROP_OVERLIMIT.
The flow mode is now read once with READ_ONCE().
Link: https://lore.kernel.org/netdev/179231933068.3135.80340792633077986@kernel.org/
Link: https://lore.kernel.org/netdev/179269577270.3135.16072065138845192450@kernel.org/
> net/sched: cake: hash before segmenting and reject an overlong split
>
> The Fix: with CAKE_FLAG_SPLIT_GSO, cake computes the flow keys and then
> splits a GSO skb into segments it accounts individually. Key extraction
> must run on the unsegmented skb -- skb_gso_segment() can rewrite
> encapsulated headers to the inner ones -- and resolution must wait until
> admission, since it commits set-associative state a later rejection would
> leave mutated.
[Severity: Low]
Is "hash before segmenting" accurate as a description of what this
patch changes? Before this patch, cake_enqueue() already hashed the
unsegmented skb, ahead of the split block:
b = cake_select_tin(sch, skb);
idx = cake_hash(b, skb, q->config->flow_mode, flow_override,
host_override) + 1;
...
if (qdisc_pkt_segs(skb) > 1 && q->config->rate_flags & CAKE_FLAG_SPLIT_GSO) {
...
segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
The earlier patches in the series ("return the classifier result to the
caller" and "use a dedicated split-GSO list iterator") keep this order.
So keys were always taken from the unsegmented skb.
The change to hashing goes the other way. The stateful set-associative
part (q->tags[], host tags, bulk counters, now cake_hash_resolve()) moves
after skb_gso_segment() and after the overlimit / ancestor refusal that
this same patch adds.
In the old code, the only rejection that came after the state-committing
cake_hash() was a skb_gso_segment() failure. The message doesn't mention
that case.
The body sentence "move the split-GSO path ahead of the resolution" is
accurate. The subject and the "The Fix:" paragraph, though, could make a
reader such as a stable backporter think cake used to hash segmented
skbs with rewritten inner headers.
Would a subject such as "resolve the flow only after the split is
admitted" describe the change more precisely?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/QDISC-BA27.v13.r0.20261127065026%40mojatatu.com
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH net-next v13 3/4] net/sched: cap the accounted backlog before it can wrap
2026-11-28 12:08 ` netdev-bot+sashiko
@ 2026-11-28 16:26 ` Jamal Hadi Salim
0 siblings, 0 replies; 8+ messages in thread
From: Jamal Hadi Salim @ 2026-11-28 16:26 UTC (permalink / raw)
To: netdev-bot+sashiko
Cc: netdev, davem, edumazet, kuba, pabeni, horms, jiri, stephen,
vinicius.gomes, toke, victor, bjorn, sashiko-bot
On Sat, Nov 28, 2026 at 7:08 AM <netdev-bot+sashiko@kernel.org> wrote:
>
> Thank you for your contribution! Sashiko AI review found 8 potential
> issue(s) to consider.
>
> Critical: 0 · High: 2 · Medium: 5 · Low: 1
>
> If you believe none of the issues are legitimate, please reply to each
> issue of Medium severity or higher and explain why the AI is wrong.
> If any of them are legitimate and you plan to respin, please let patchwork
> know by including "pw-bot: cr" as a separate line at the end of your reply
> (one such reply per series is enough).
>
So version 13 on a friday and still no luck?
> - [High] Signed reinterpretation in qdisc_tree_reduce_backlog() can
> blackhole the parent qdisc for good.
> - [High] taprio's backlog/qlen can leak upward through control-path child
> drops, and the new guard then drops every packet.
So version 13 on a friday and still no luck? The taprio issue is my
fault in v13. So v14 coming up.
cheers,
jamal
> - [Medium] The saturating reconciliation breaks reduce-before-book
> accounting and leaves a phantom backlog.
> - [Medium] The offload exemption depends on a flag that is cleared on
> every stats dump, while the driver still writes non-saturating…
> - [Medium] The core saturation also runs on offloaded ancestors and
> breaks the drivers' signed-delta hardware accounting.
> - [Medium] Segmenting children other than cake still grow ancestors
> without the room check.
> - [Medium] The commit message misdescribes some changes and leaves out
> several separate behavioural changes.
> - [Low] No Fixes: tag.
>
> --
>
> net/sched: cap the accounted backlog before it can wrap
>
> This patch adds an enqueue-time guard, qdisc_backlog_overflow(), across
> the net/sched qdiscs. The guard drops a packet once the 32-bit
> sch->qstats.backlog can no longer hold its accounted length. It also makes
> the core backlog subtraction saturate, routes qdisc_tree_reduce_backlog()
> and sfq through a new signed qdisc_qstats_backlog_apply(), and reworks
> taprio to mirror its children's backlog changes.
>
> Closes: https://lore.kernel.org/netdev/20260818101130.16203-1-jhs@mojatatu.com/
> Link: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260818101130.16203-1-jhs@mojatatu.com
> Link: https://lore.kernel.org/netdev/CANn89iLfMJV7ancKH1Gjzzm7ZUG-gKcrczkJjJEWN5sCuWd-ug@mail.gmail.com/
> Link: https://lore.kernel.org/netdev/CANn89i+GOFH_8g+vSV0jUj0aBqLYVWuFOrHuDggZu9kPC_m6tQ@mail.gmail.com/
> Link: https://lore.kernel.org/netdev/87a4oottcj.fsf@toke.dk/
> Link: https://lore.kernel.org/netdev/87pkxks68f.fsf@toke.dk/
> Link: https://lore.kernel.org/netdev/179231932946.3135.7497478990186385688@kernel.org/
> Link: https://lore.kernel.org/netdev/179269577131.3135.3967915390403138015@kernel.org/
>
> > This is a followup of commit 709f34f7c28d ("net/sched: fq: add overflow
> > bounds to quantum and initial quantum") as reported by Sashiko.
>
> [Severity: Low]
> This isn't a bug, but should this have a Fixes: tag for the commit that
> introduced the backlog wrap, and possibly a stable Cc?
>
> 709f34f7c28d is an fq quantum change, not the source of the wrap. The tag
> block only has Reported-by, Closes, Link and Signed-off-by. The separate
> hhf/skbprio and plug fixes bundled here don't have Fixes: tags either.
>
> [ ... ]
>
> > Some context: qdisc_pkt_len() stays truthful for every input, so BPF
> > wire_len, act_police and the HTB/TBF shapers read the real length. RED
> > books the packet on success again, as the tree did before v11.
>
> [Severity: Medium]
> Is the RED sentence accurate for the tree this applies to? The baseline
> red_enqueue() already books the packet on success:
>
> ret = qdisc_enqueue(skb, child, to_free);
> if (likely(ret == NET_XMIT_SUCCESS)) {
> qstats_backlog_add(sch, len);
> qdisc_qlen_inc(sch);
>
> The RED hunk in this patch only adds the new guard. The references to v11,
> "stays truthful" and "at dequeue as well" only make sense relative to
> earlier postings of the series.
>
> The message also leaves out several behavioural changes in the diff:
>
> hhf_enqueue() and skbprio_enqueue() now call qdisc_tree_reduce_backlog()
> on the NET_XMIT_CN path. This fixes a separate, pre-existing drift in
> the parent backlog.
>
> plug_enqueue() now checks its limit against qdisc_pkt_len() instead of
> skb->len, which changes admission for stab and GSO users.
>
> taprio_enqueue_segmented() no longer adds child drops to taprio's own
> drop counter.
>
> netem_enqueue() now pre-books qlen before it re-injects the duplicate,
> so a duplicate near sch->limit can now be dropped where it used to be
> admitted.
>
> Could these be described in the commit message, or split out?
>
> > diff --git a/include/net/sch_generic.h b/include/net/sch_generic.h
> > index 1acaadb3e2cf0..677cd089ad5d3 100644
> > --- a/include/net/sch_generic.h
> > +++ b/include/net/sch_generic.h
> > @@ -735,6 +735,12 @@ void qdisc_destroy(struct Qdisc *qdisc);
> > void qdisc_put(struct Qdisc *qdisc);
> > void qdisc_put_unlocked(struct Qdisc *qdisc);
> > void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len);
> > +/* Does every ancestor of @sch have room for @extra more bytes below its
> > + * enqueue ceiling? A child that turns one skb into several reconciles the
> > + * difference through qdisc_tree_reduce_backlog(), growth that never passes
> > + * the ancestors' own enqueue guard.
> > + */
> > +bool qdisc_ancestors_backlog_room(const struct Qdisc *sch, u64 extra);
>
> [Severity: Medium]
> Who calls this helper at this commit? It is exported but has no caller
> here. Later in the series, "net/sched: cake: hash before segmenting and
> reject an overlong split" adds a call in cake_enqueue(), and that is the
> only caller.
>
> tbf_segment() and the netem segment path also turn one skb into several,
> and they grow the ancestors without this room check:
>
> net/sched/sch_tbf.c:tbf_segment() {
> ...
> qstats_backlog_add(sch, len);
> if (nb > 0) {
> qdisc_tree_reduce_backlog(sch, 1 - nb, prev_len - len);
> ...
> }
>
> net/sched/sch_netem.c:netem_enqueue() {
> ...
> qdisc_tree_reduce_backlog(sch, -(nb - 1), -(len - prev_len));
> ...
> }
>
> The QDISC_MAX_BACKLOG comment says "a single enqueue can never run out of
> headroom before it is accounted". That only holds if this growth stays
> under about 2 MiB.
>
> Could a stab table that maps a large encapsulated BIG TCP aggregate to
> just above tbf's max_size push the segment sum past that headroom? That
> would wrap tbf's counter or an ancestor's.
>
> The later cake patch calls this bound defensive rather than proven, so
> this may be hard to reach. It also doesn't match the commit message claim
> that the ceiling covers "every qdisc in net/sched that adds the incoming
> qdisc_pkt_len() to its own backlog".
>
> > @@ -915,6 +921,37 @@ static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
> > return qdisc_skb_cb(skb)->pkt_len;
> > }
> >
> > +#define QDISC_PKT_LEN_MAX (1 << 20) /* 1 MiB */
> > +
> > +/* Largest backlog that still has room for a 1 MiB packet and a netem
> > + * duplicate of it, so that a single enqueue can never run out of
> > + * headroom before it is accounted.
> > + */
> > +#define QDISC_MAX_BACKLOG (U32_MAX - 2 * QDISC_PKT_LEN_MAX)
>
> [ ... ]
>
> > +static inline bool qdisc_backlog_overflow(const struct Qdisc *sch,
> > + const struct sk_buff *skb)
> > +{
> > + u32 len = qdisc_pkt_len(skb);
> > + u32 backlog;
> > +
> > + if (unlikely(READ_ONCE(sch->flags) & TCQ_F_OFFLOADED))
> > + return false;
>
> [Severity: Medium]
> Can the qdisc lose this exemption while its counter still carries
> hardware bytes? qdisc_offload_dump_helper() clears the flag around every
> stats query and sets it again only on success:
>
> net/sched/sch_api.c:qdisc_offload_dump_helper() {
> ...
> sch->flags &= ~TCQ_F_OFFLOADED;
> if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
> return 0;
>
> err = dev->netdev_ops->ndo_setup_tc(dev, type, type_data);
> ...
> if (!err)
> sch->flags |= TCQ_F_OFFLOADED;
> ...
> }
>
> This runs from dump callbacks such as red_dump()->red_dump_offload_stats()
> with only RTNL held, because tc_fill_qdisc() calls q->ops->dump() without
> the qdisc root lock.
>
> Inside that window, mlxsw updates the counter with plain u32 arithmetic.
> That races with the datapath's saturating update:
>
> mlxsw_sp_qdisc_update_stats():
> stats_ptr->qstats->backlog += mlxsw_sp_cells_bytes(mlxsw_sp, backlog);
>
> mlxsw_sp_qdisc_leaf_unoffload():
> qstats->backlog -= backlog;
>
> nfp_abm_stats_propagate() also writes hardware deltas without saturating.
>
> The new comment in qdisc_qstats_backlog_dec() says a datapath update can
> be lost here. If that happens and the hardware then drains, could the
> driver's delta wrap the counter to near U32_MAX?
>
> If so, enqueues during later dump windows would hit the ceiling. After an
> unoffload, where the flag stays cleared, every enqueue would be dropped.
>
> The TCQ_F_OFFLOADED check in qdisc_ancestors_backlog_room() has the same
> window.
>
> [ ... ]
>
> > @@ -991,13 +1028,43 @@ static inline void qdisc_bstats_update(struct Qdisc *sch,
>
> [ ... ]
>
> > +static inline void qdisc_qstats_backlog_apply(struct Qdisc *sch, int delta)
> > +{
> > + u32 backlog = READ_ONCE(sch->qstats.backlog);
> > + u64 next;
> > +
> > + if (delta >= 0)
> > + next = backlog > (u32)delta ? backlog - (u32)delta : 0;
>
> [Severity: Medium]
> Should this clamp also apply to offloaded ancestors? Their counter
> includes hardware deltas that the driver maintains.
>
> mlxsw keeps the hardware share as a modular delta against a per-qdisc
> baseline in mlxsw_sp_qdisc_update_stats(). When an offloaded child is
> destroyed, it rebases the parent on the assumption that the core
> subtracted exactly:
>
> mlxsw_sp_qdisc_reduce_parent_backlog():
> tmp->stats_base.backlog -= mlxsw_sp_qdisc->stats_base.backlog;
>
> The parent and child baselines come from different dumps. The parent's
> counter can therefore be lower than the child's, for example if the parent
> was dumped while the tclass was empty and the child while it was full.
>
> prio_graft()->qdisc_replace()->qdisc_purge_queue()->
> qdisc_tree_reduce_backlog() then subtracts the child's backlog. The clamp
> stops at 0, but the next parent dump still adds back the full hardware
> share of the child.
>
> Wouldn't the parent then stay too high by that amount for good?
> mlxsw_sp_qdisc_leaf_unoffload() only removes stats_base, so the excess
> would also carry over into the software-only qdisc.
>
> > diff --git a/net/sched/sch_api.c b/net/sched/sch_api.c
> > index f36c41224bccf..062e5d6f158eb 100644
> > --- a/net/sched/sch_api.c
> > +++ b/net/sched/sch_api.c
> > @@ -877,13 +877,55 @@ void qdisc_tree_reduce_backlog(struct Qdisc *sch, int n, int len)
> > cops->qlen_notify(sch, cl);
> > }
> > WRITE_ONCE(sch->q.qlen, sch->q.qlen - n);
> > - qstats_backlog_sub(sch, len);
> > + qdisc_qstats_backlog_apply(sch, len);
> > __qdisc_qstats_drop(sch, drops);
>
> [Severity: High]
> Can a large but legitimate removal now be treated as growth here? len is
> an int. qstats_backlog_sub() took it as a u32, so modular arithmetic got
> removals of up to 4 GiB right. qdisc_qstats_backlog_apply() treats a
> negative int as growth and saturates at U32_MAX.
>
> Control-path callers pass unsigned byte counts through this int:
>
> include/net/sch_generic.h:qdisc_purge_queue():
> qdisc_tree_reduce_backlog(sch, qlen, backlog);
>
> net/sched/sch_fq_codel.c:fq_codel_change():
> unsigned int dropped_pkts = 0, dropped_bytes = 0;
> ...
> qdisc_tree_reduce_backlog(sch, dropped_pkts, dropped_bytes);
>
> The codel, fq, fq_pie, hhf, pie, sfq and cake ->change() handlers do the
> same.
>
> With the new ceiling, a child backlog between 2 GiB and about 4 GiB is a
> reachable state. One setup: a root prio/htb/drr/ets/tbf/red, a stab
> overhead as in the reproducer, and an fq_codel child filled to about
> 3 GiB. Then "tc qdisc change ... fq_codel limit 1", a class delete or a
> child replace passes len > INT_MAX.
>
> Would every ancestor then grow by 2^32 - len and stay pinned at U32_MAX?
>
> >From then on, qdisc_backlog_overflow() sees backlog > QDISC_MAX_BACKLOG
> and drops every packet with QDISC_DROP_OVERLIMIT. The children are empty,
> so no dequeue ever brings the counter down. That is the "drop every packet
> for good" outcome the new qdisc_qstats_backlog_dec() comment tries to
> prevent.
>
> [Severity: Medium]
> Moving from modular subtraction to a clamp has a second effect. Some
> children report a reduction to their ancestors before the parent has
> booked the packet. tbf_segment() calls:
>
> qdisc_tree_reduce_backlog(sch, 1 - nb, prev_len - len);
>
> and then returns NET_XMIT_SUCCESS, after which the parent does
> qstats_backlog_add(sch, prev_len). The netem_enqueue() segment path and
> the cake_enqueue() split-GSO path use the same order.
>
> With modular arithmetic, the parent ended up correct once it booked
> prev_len. With the clamp, take B as the parent's current backlog and
> d = prev_len - len. If B < d, the reduction stops at 0, the parent then
> adds prev_len, and later dequeues remove only len.
>
> Doesn't that leave a phantom backlog of d - B bytes with qlen 0?
>
> This can happen with an idle htb/prio/drr parent over a tbf leaf, when
> stab inflation (for example linklayer atm overhead) makes prev_len larger
> than the segment sum, or when some segments are dropped.
> red_calc_qavg(child->qstats.backlog) over such a classful child would
> read the inflated value. The commit message presents the saturation as
> purely protective.
>
> [ ... ]
>
> > diff --git a/net/sched/sch_taprio.c b/net/sched/sch_taprio.c
> > index 5a1dca9d5e9de..42364c785c838 100644
> > --- a/net/sched/sch_taprio.c
> > +++ b/net/sched/sch_taprio.c
>
> [ ... ]
>
> > @@ -585,10 +589,36 @@ static bool taprio_skb_exceeds_queue_max_sdu(struct Qdisc *sch,
> > return exceeds;
> > }
> >
> > +/* The child cannot correct us: taprio_graft() marks it TCQ_F_NOPARENT, so
> > + * its qdisc_tree_reduce_backlog() stops there. Mirror its net change.
> > + */
> > +static void taprio_mirror_child(struct Qdisc *sch, struct Qdisc *child,
> > + u32 backlog_before, int qlen_before)
> > +{
> > + /* apply() takes the amount to remove, so a child growth is negated
> > + * and a child eviction adds.
> > + */
> > + qdisc_qstats_backlog_apply(sch,
> > + -(int)(READ_ONCE(child->qstats.backlog) -
> > + backlog_before));
> > + WRITE_ONCE(sch->q.qlen,
> > + sch->q.qlen + (int)(qdisc_qlen(child) - qlen_before));
> > +}
> > +
> > static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
> > struct Qdisc *child, struct sk_buff **to_free)
> > {
> > struct taprio_sched *q = qdisc_priv(sch);
> > + struct Qdisc *mirror_prev;
> > + u32 backlog;
> > + int qlen;
> > + int ret;
> > +
> > + if (unlikely(qdisc_backlog_overflow(sch, skb))) {
> > + qdisc_qstats_overlimit(sch);
> > + return qdisc_drop_reason(skb, sch, to_free,
> > + QDISC_DROP_OVERLIMIT);
> > + }
>
> [Severity: High]
> What happens to taprio's counters when a child drops packets outside the
> enqueue and dequeue windows?
>
> taprio's backlog and qlen are now updated only from child deltas measured
> around enqueue and dequeue. taprio_graft() and taprio_attach() set
> TCQ_F_NOPARENT on every child, so qdisc_tree_reduce_backlog() stops at the
> child:
>
> net/sched/sch_api.c:qdisc_tree_reduce_backlog() {
> ...
> if (sch->flags & TCQ_F_NOPARENT)
> break;
> ...
> }
>
> A child's ->change() can drop queued packets without taprio seeing it.
> Examples are fq_codel_change() and the codel, pie, fq_pie, hhf, fq, sfq
> and cake equivalents, or a class delete inside a classful child.
>
> Nothing removes those packets from taprio's sch->qstats.backlog or
> sch->q.qlen. taprio_dump() does not resync from the children, and only a
> qdisc_reset() of the root clears the counters.
>
> Take a software-mode taprio with an fq_codel child on a txq and a stab
> overhead that accounts each packet at about 1 MiB. Filling the child and
> running "tc qdisc change dev X parent <taprio>:N fq_codel limit 1", over
> and over, would keep adding leaked bytes. A graft goes through
> dev_deactivate(), which resets the root, so a graft does not leak.
>
> Once the leaked total passes QDISC_MAX_BACKLOG, won't this new guard drop
> every packet on the device with QDISC_DROP_OVERLIMIT? The children are
> empty, so nothing brings the counter back down.
>
> Before this patch the drift only made the stats wrong. The guard now turns
> it into a permanent drop.
>
> [ ... ]
>
> --
> Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/QDISC-BA27.v13.r0.20261127065026%40mojatatu.com
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-28 16:26 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 11:57 [PATCH net-next v13 0/4] net/sched: cap the accounted backlog before it can wrap Jamal Hadi Salim
2026-11-27 11:57 ` [PATCH net-next v13 1/4] net/sched: cake: return the classifier result to the caller Jamal Hadi Salim
2026-11-27 11:57 ` [PATCH net-next v13 2/4] net/sched: cake: use a dedicated split-GSO list iterator Jamal Hadi Salim
2026-11-27 11:57 ` [PATCH net-next v13 3/4] net/sched: cap the accounted backlog before it can wrap Jamal Hadi Salim
2026-11-28 12:08 ` netdev-bot+sashiko
2026-11-28 16:26 ` Jamal Hadi Salim
2026-11-27 11:57 ` [PATCH net-next v13 4/4] net/sched: cake: hash before segmenting and reject an overlong split Jamal Hadi Salim
2026-11-28 12:08 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox