Skip to contentSkip to conversation

net/sched: cap the accounted backlog before it can wrap

PATCH Superseded Jamal Hadi Salim 2 participants 4 messages Oct 2 – Oct 9

Subsystems (4)

v4 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Jamal Hadi Salim

author maintainer
#0 lore raw

PATCH v4 1/1 net/sched: cap the accounted backlog before it can wrap

This is a follow-up to an issue found by Sashiko (nipa) during review of commit d9ebd8f9aa8b ("net/sched: fq_codel: clamp default quantum and mtu"), part of the quantum/mtu overflow series merged as a687f2ae995f. Clamping the per-flow quantum and the CoDel mtu does not bound the accounted backlog, so the per-flow backlog wrap the review flagged remained.

fq_codel, cake, codel, pie, fq_pie and dualpi2 account every enqueued packet's stab-adjusted length into a 32-bit sch->qstats.backlog. fq_codel/codel use it to decide if they should drop a packet at dequeue; cake uses it to prune the longest-flow heap from per-flow backlogs; pie and fq_pie use it to make early drop decisions and dualpi2 decides must_drop() on it. RED can decide on a child's backlog based on it. A crafted tc size table (TC_STAB) inflates qdisc_pkt_len() up to QDISC_PKT_LEN_MAX (1 MiB), so a few thousand packets wrap the counter mod 2^32. The AQM then reads a small backlog and makes the wrong drop decision, and the dequeue-side subtractions keep the counter corrupt.

In fq_codel the wrapped value is worse than a wrong drop: a wrapped q->backlogs[idx] can leave fq_codel_drop()'s strict '>' scan selecting idx 0 while flow 0 is empty, so dequeue_head() dereferences a NULL flow->head.

Fix:
Drop at enqueue once the stored backlog is within QDISC_MAX_BACKLOG (U32_MAX - QDISC_PKT_LEN_MAX) of the wrap point, the largest backlog one more maximum-size packet cannot wrap. Test the stored backlog itself via qdisc_backlog_at_max() rather than adding the incoming packet's length, so there is no per-enqueue 64-bit add and the 1 MiB headroom already reserved by QDISC_MAX_BACKLOG is not counted twice. This follows the existing bfifo approach, which bounds bytes against the accounted length; gred is a precedent only in WRED mode, where it bounds the aggregate, while non-WRED gred bounds each virtual queue. The fixed qdiscs' limits are packet counts or otherwise do not bound the aggregate bytes. pfifo, reachable standalone or grafted as RED's or SFB's child, is fixed too because RED reads a child's backlog into red_calc_qavg(). RED's guard bounds only the primary enqueue: a child that segments a GSO skb (tbf, netem) still grows the parent aggregate through qdisc_tree_reduce_backlog()'s unclamped byte delta, left to a separate change.

fq_codel and cake both expose an optional external classifier whose terminal TC actions (STOLEN/QUEUED/TRAP/SHOT) consume the packet without enqueuing it. The ceiling check runs after that classifier and its terminal result, but before flow-hash/shaper state, so a redirect, trap or police action keeps working while the backlog sits at the ceiling, and a packet about to be dropped still does not disturb the flow hash. Only a packet admitted past the check mutates a flow: for fq_codel the guard precedes fq_codel_hash(); for cake the guard precedes both the key extraction and the split-GSO segmentation, so the flow/host keys are taken from the packet as received rather than from a segment whose encapsulated headers skb_gso_segment() may have rewritten. CAKE's set-associative resolution, which writes q->tags[], the flow host indices, the way counters and the host bulk-flow counts, runs only once the packet is admitted. fq_codel's external filter selects a flow only when the result's minor is nonzero and no larger than flows_cnt; a matched filter that supplied no class ID (classid 0) is the no-flow result and the packet is dropped, as before.

CAKE's SPLIT_GSO path accounts the sum of the segment lengths, which a stab recomputes from skb->len and can exceed the pre-split qdisc_pkt_len(). The list is summed before any segment is linked and rejected as a whole before any flow state is resolved: the flow/host keys are computed from the unsegmented packet, but the set-associative resolution that commits q->tags[], the flow host indices, the way counters and the host bulk-flow counts runs only after the whole segment-sum admission test passes. A packet rejected by that test therefore leaves no persistent state behind. The whole list is dropped via to_free so the tail segment's sock_wfree() never runs under the qdisc lock. A fraglist GSO is the exception: skb_segment_list() returns the original skb as the list head carrying an extra reference, so segs == skb; dropping the caller's reference and queueing the list once avoids self-linking *to_free, which would otherwise free the same skb repeatedly in the deferred teardown.

Conditions to recreate the bug: CAP_NET_ADMIN in a user namespace; CONFIG_NET_SCH_FQ_CODEL=y.

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 (IFF_BACKPRESSURE) so the qdisc
  # backlog persists, then send 6000 packets. At 4096 resident 1 MiB
  # packets the counter wraps; the unfixed kernel then reports a wrapped
  # backlog for 5969 resident packets, the fixed one drops at the ceiling.

# RED with a grafted packet-limited child reads the child's backlog the

  # same way; the default bfifo child is byte-limited and safe, pfifo is
  # packet-limited:
  tc qdisc add dev tun0 root handle 1: stab overhead 2000000000 \
      red limit 1000000000 min 5000 max 10000 avpkt 1000 burst 32 ecn
  tc qdisc add dev tun0 parent 1:1 handle 30: pfifo limit 100000
  # a terminal action at the ceiling: fill the queue as above, then
  #   tc filter add dev tun0 parent 1: protocol ip prio 1 matchall \
  #       action trap
  # and send more packets; the trap action's stats must advance.

---
v4 fold the Sashiko nipa v3 review:
- cake: compute the flow/host hash keys from the packet as received (before skb_gso_segment(), which can rewrite an encapsulated GSO packet's headers to the inner ones), but defer the set-associative resolution that writes q->tags[], the flow host indices, the way counters and the host bulk-flow counts until after the split-GSO segment-sum admission check, so a packet rejected by that check commits no flow state.
- cake: in the split-GSO rejection, handle a fraglist GSO (whose skb_segment_list() returns the original skb as the list head with an extra reference, segs == skb) by dropping the caller's reference and queueing the list once, instead of calling __qdisc_drop_all() and qdisc_drop_reason() on the same skb, which self-linked *to_free.

v3 fold the v2 review (Eric Dumazet) and Sashiko gemini v2:
- Test the stored backlog via qdisc_backlog_at_max() instead of (u64)backlog + qdisc_pkt_len() > QDISC_MAX_BACKLOG: no double-count of the reserved 1 MiB headroom, no per-enqueue 64-bit add.
- cake: run the external classifier first, honor its terminal TC actions, then the ceiling check, then the hash and the shaper state; the SPLIT_GSO segment sum is checked before any flow/host state is committed and the list is deferred to to_free rather than freed under the qdisc lock.
- fq_codel: run the external classifier (terminal actions honored)

    first, then the ceiling check before fq_codel_hash(); document the
    fq_codel_drop() NULL flow->head consequence.

- pfifo: same ceiling guard, since RED reads a (graftable) child's backlog and SFB's default child is pfifo.

v2 approach rewrite per list discussion of v1:
v1 approached the wrap by widening the per-flow backlog counters to u64. Eric Dumazet rejected the framing ("Storing 4GB in a qdisc is absolutely insane") and asked for a drop at enqueue once the backlog approaches the wrap point. Sebastian Moeller noted tc-stab is the generic overhead-accounting mechanism, so the fix must not penalize normal stab use.
v2 replaces the u64 widening with an enqueue-side pre-check against QDISC_MAX_BACKLOG and drops with QDISC_DROP_OVERLIMIT. No counter is widened, so the 32-bit sch->qstats.backlog the AQM reads can no longer wrap. Same-pattern siblings fixed in one patch: fq_codel, cake, codel, pie, fq_pie, dualpi2, RED.

 include/net/pkt_sched.h   |   1 -
 include/net/sch_generic.h |  15 +++
 net/sched/sch_cake.c      | 188 ++++++++++++++++++++++++++++----------
 net/sched/sch_codel.c     |  16 ++--
 net/sched/sch_dualpi2.c   |   1 +
 net/sched/sch_fifo.c      |   6 ++
 net/sched/sch_fq_codel.c  |  54 ++++++++---
 net/sched/sch_fq_pie.c    |   3 +-
 net/sched/sch_pie.c       |   3 +-
 net/sched/sch_red.c       |   6 ++
 10 files changed, 221 insertions(+), 72 deletions(-)
include/net/pkt_sched.h +0 -1
index 90d3e7943b19..18a419cd9d94 100644
@@ -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;
include/net/sch_generic.h +15 -0
index f35bd06a6bad..b396a1606d62 100644
@@ -906,6 +906,21 @@ 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 accounted backlog one more maximum-size packet cannot wrap
+ * the 32-bit sch->qstats.backlog past.
+ */
+#define QDISC_MAX_BACKLOG	(U32_MAX - QDISC_PKT_LEN_MAX)
+
+/* Backlog close enough to U32_MAX that one more packet could wrap it.
+ * A packet-limited qdisc must drop at enqueue when this holds.
+ */
+static inline bool qdisc_backlog_at_max(const struct Qdisc *sch)
+{
+	return sch->qstats.backlog > QDISC_MAX_BACKLOG;
+}
+
 static inline unsigned int qdisc_pkt_segs(const struct sk_buff *skb)
 {
 	u32 pkt_segs = qdisc_skb_cb(skb)->pkt_segs;
net/sched/sch_cake.c +138 -50

292 lines not on this page · raw message

net/sched/sch_codel.c +9 -7
index 6aa5829d6961..9aa18438413a 100644
@@ -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_at_max(sch))) {
+		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] = {
net/sched/sch_dualpi2.c +1 -0
index 4947def7c49e..5c746e9b60cd 100644
@@ -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_at_max(sch)) ||
 	    unlikely((u64)q->memory_used + skb->truesize > q->memory_limit)) {
 		qdisc_qstats_overlimit(sch);
 		if (skb_in_l_queue(skb))
net/sched/sch_fifo.c +6 -0
index 1b6388d50967..d5e5cd49377d 100644
@@ -29,6 +29,9 @@ 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_at_max(sch)))
+		return qdisc_drop(skb, sch, to_free);
+
 	if (likely(sch->q.qlen < READ_ONCE(sch->limit)))
 		return qdisc_enqueue_tail(skb, sch);
 
@@ -43,6 +46,9 @@ 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_at_max(sch)))
+		return qdisc_drop(skb, sch, to_free);
+
 	if (likely(sch->q.qlen < READ_ONCE(sch->limit)))
 		return qdisc_enqueue_tail(skb, sch);
 
net/sched/sch_fq_codel.c +42 -12

95 lines not on this page · raw message

net/sched/sch_fq_pie.c +2 -1
index 5982847df8f8..394f015cec36 100644
@@ -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_at_max(sch))) {
 		q->stats.overlimit++;
 		goto out;
 	} else if (unlikely(memory_limited)) {
net/sched/sch_pie.c +2 -1
index 3b7863ffd284..9e38f38ce923 100644
@@ -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_at_max(sch))) {
 		WRITE_ONCE(q->stats.overlimit, q->stats.overlimit + 1);
 		goto out;
 	}
net/sched/sch_red.c +6 -0
index d7598214270b..e3a254f9dbdf 100644
@@ -76,6 +76,12 @@ static int red_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 	unsigned int len;
 	int ret;
 
+	if (unlikely(qdisc_backlog_at_max(sch))) {
+		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);
Signature · 1 line
2.43.0
reviewer
#1 lore raw

On Fri, Oct 02, 2026 at 03:44:31AM -0400, Jamal Hadi Salim wrote:

...

Quoted 1 line
net/sched/sch_cake.c +0 -0

...

net/sched/sch_cake.c static struct cake_tin_data *cake_select_tin(struct Qdisc *sch, · line 1742

Quoted 8 lines
net/sched/sch_cake.c +2 -2
@@ -1712,18 +1738,24 @@ 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 optional external classifier. Returns true when the packet was
+ * consumed by a terminal TC action (reason in *qerr); else *flow/*host

FYI:

W=1 builds complain that the line above has a '/*' embedded in a comment. This is likely why Sashiko NIPA didn't run for this series.

...

author maintainer
#2 lore raw
Quoted 20 lines · Simon Horman

On Fri, Oct 02, 2026 at 03:44:31AM -0400, Jamal Hadi Salim wrote:

...

Quoted 1 line
net/sched/sch_cake.c +0 -0

...

net/sched/sch_cake.c static struct cake_tin_data *cake_select_tin(struct Qdisc *sch, · line 1742

Quoted 8 lines
net/sched/sch_cake.c +2 -2
@@ -1712,18 +1738,24 @@ 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 optional external classifier. Returns true when the packet was
+ * consumed by a terminal TC action (reason in *qerr); else *flow/*host

FYI:

Quoted 2 lines

W=1 builds complain that the line above has a '/*' embedded in a comment. This is likely why Sashiko NIPA didn't run for this series.

Thanks for catching this Simon. We don't run W=1 - already overwhelmed as it is with the flood. Do you think we should?

cheers,
jamal

Quoted 1 line

...

reviewer
#3 lore raw
Quoted 21 lines · Jamal Hadi Salim
Quoted 20 lines · Simon Horman

On Fri, Oct 02, 2026 at 03:44:31AM -0400, Jamal Hadi Salim wrote:

...

Quoted 1 line
net/sched/sch_cake.c +0 -0

...

net/sched/sch_cake.c static struct cake_tin_data *cake_select_tin(struct Qdisc *sch, · line 1742

Quoted 8 lines
net/sched/sch_cake.c +2 -2
@@ -1712,18 +1738,24 @@ 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 optional external classifier. Returns true when the packet was
+ * consumed by a terminal TC action (reason in *qerr); else *flow/*host

FYI:

Quoted 5 lines
Quoted 2 lines

W=1 builds complain that the line above has a '/*' embedded in a comment. This is likely why Sashiko NIPA didn't run for this series.

Thanks for catching this Simon. We don't run W=1 - already overwhelmed as it is with the flood. Do you think we should?

Yes.

It's important that patches don't add new warnings.
You can use some sort of diff mechanism to spot those amongst any noise.