Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: jhs@mojatatu.com
Cc: netdev@vger.kernel.org, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com, willemb@google.com,
	horms@kernel.org, jiri@resnulli.us, stephen@networkplumber.org,
	vinicius.gomes@intel.com, toke@toke.dk, victor@mojatatu.com,
	bjorn@kernel.org, sashiko-bot@kernel.org
Subject: Re: [PATCH net-next v14 3/4] net/sched: cap the accounted backlog before it can wrap
Date: Mon, 30 Nov 2026 10:01:12 +0000	[thread overview]
Message-ID: <179603287226.3146.11753213620888850974@kernel.org> (raw)
In-Reply-To: <QDISC-BA27.v14.r0.20261128181551-3@mojatatu.com>

Thank you for your contribution! Sashiko AI review found 11 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 5 · Low: 6

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

- [Medium] The patch fixes a long-standing, user-namespace-reachable wrap
  of the u32 sch->qstats.backlog, which leads to wrong AQM decisions and…
- [Medium] netem's GSO corruption split grows every ancestor's u32
  qstats.backlog, and nothing checks that growth against the ceiling.
- [Medium] taprio's u32 counter can still wrap when a child splits a GSO
  skb, for example a tbf grafted under taprio running tbf_segment().
- [Medium] qdisc_offload_dump_helper() clears the offload exemption latch
  before calling into the driver and only sets it again after a…
- [Medium] net/sched: taprio_resync_backlog() omits gso_skb and
  skb_bad_txq packets
- [Low] The commit message understates what this patch fixes.
- [Low] The commit message has an inaccurate statement and leaves out
  several behaviour changes.
- [Low] The new split-refusal paths in tbf_segment() and
  dualpi2_qdisc_enqueue() drop freshly segmented skbs with…
- [Low] The comments behind taprio's new mirror accounting say every
  child is TCQ_F_NOPARENT.
- [Low] taprio_dump_stats() now calls taprio_resync_backlog() under
  qdisc_lock(sch), which rewrites both sch->qstats.backlog and…
- [Low] qdisc_qstats_backlog_apply() is documented as taking a signed net
  change ("positive removes, negative grows"), and…

--

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

This patch adds qdisc_backlog_overflow() and calls it at enqueue in most
qdiscs. A packet is dropped once the u32 sch->qstats.backlog can no longer
hold its accounted length. The patch also makes the backlog removal helpers
saturate, changes qdisc_tree_reduce_backlog() to take an s64, adds
qdisc_ancestors_backlog_room() for the tbf and dualpi2 GSO split paths, and
reworks taprio so it mirrors its children's net change.

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/

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

[Severity: Low]
Is "pick an empty flow" the full consequence? It looks like this ends in a
NULL pointer dereference.

q->backlogs[] is u32. 4096 packets clamped to 1 MiB add up to exactly 2^32,
so that flow's backlog reads 0. In fq_codel_drop():

	unsigned int maxbacklog = 0, idx = 0, i, len;
	...
	for (i = 0; i < q->flows_cnt; i++) {
		if (q->backlogs[i] > maxbacklog) {
	...
	flow = &q->flows[idx];
	...
	do {
		skb = dequeue_head(flow);
		len += qdisc_pkt_len(skb);

If the wrapped flow is not flow 0 and flow 0 is empty, idx stays 0.
dequeue_head() then reads flow->head (NULL) and dereferences skb->next.

The reproducer in this message only needs CAP_NET_ADMIN in a user
namespace. Should the message say that the unfixed kernel can oops?

> This is a followup of commit 709f34f7c28d ("net/sched: fq: add overflow
> bounds to quantum and initial quantum") as reported by Sashiko.

[Severity: Medium]
Should this carry a Fixes: tag and a stable Cc?

The wrap does not come from 709f34f7c28d. __qdisc_calculate_pkt_len() has
inflated the length this way since size tables were added:

	pkt_len = skb->len + stab->szopts.overhead;

Blame points at 175f9c1bba9b ("net_sched: Add size table for qdiscs"). That
hash was not re-checked here.

Without a Fixes: tag, stable trees may not reliably pick up a fix for a
wrap that is reachable from a user namespace.

> 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, matching upstream RED's accounting
> at the base of this series.

[Severity: Low]
Is the RED sentence still accurate? At the baseline, red_enqueue() already
does qstats_backlog_add(sch, len) on NET_XMIT_SUCCESS. This patch only adds
the guard there, so the sentence looks left over from an earlier revision.

The body also leaves out a few behaviour changes that are in the diff:

- plug_enqueue() now checks its byte limit against qdisc_pkt_len(skb)
  instead of skb->len. With GSO or a size table that makes the limit
  tighter.

- hhf_enqueue() and skbprio_enqueue() now call
  qdisc_tree_reduce_backlog() on their NET_XMIT_CN paths. That is a
  separate fix to parent accounting.

- taprio_enqueue_segmented() no longer adds child drops to taprio's drop
  counter, which changes the tc -s output. Only the version changelog
  mentions this, and that is dropped on commit.

Could the commit message describe these as well?

> diff --git a/include/net/sch_generic.h b/include/net/sch_generic.h
> index 1acaadb3e2cf0..e73de3c011411 100644
> --- a/include/net/sch_generic.h
> +++ b/include/net/sch_generic.h

[ ... ]

> @@ -991,13 +1029,37 @@ static inline void qdisc_bstats_update(struct Qdisc *sch,
>  
>  static inline void qstats_backlog_sub(struct Qdisc *sch, u32 val)

[ ... ]

> +/* 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, s64 delta)
> +{
> +	u32 backlog = READ_ONCE(sch->qstats.backlog);
> +
> +	WRITE_ONCE(sch->qstats.backlog, (u32)(backlog - delta));
> +}

[Severity: Low]
This isn't a bug, but the comment describes a signed contract. Several
callers this patch leaves unchanged only satisfy it because of the (u32)
truncation. They still pass growth as a wrapped unsigned difference, which
zero-extends to a large positive s64:

sfq_enqueue():
	qdisc_tree_reduce_backlog(sch, 0, dropped - qdisc_pkt_len(skb));

pfifo_tail_enqueue():
	qdisc_tree_reduce_backlog(sch, 0, prev_backlog - sch->qstats.backlog);

fq_codel_enqueue() (prev_backlog - pkt_len) and taprio_enqueue_segmented()
(len - slen) do the same.

Read literally, those values are multi-gigabyte removals. The v13 clamp got
this same ambiguity wrong.

Should these callers get the s64 casts that hhf, skbprio, tbf and dualpi2
got? Or should the comment say the change is applied modulo 2^32?

> diff --git a/net/sched/sch_api.c b/net/sched/sch_api.c
> index f36c41224bccf..2f314414bb5d5 100644
> --- a/net/sched/sch_api.c
> +++ b/net/sched/sch_api.c

[ ... ]

> @@ -891,6 +933,8 @@ int qdisc_offload_dump_helper(struct Qdisc *sch, enum tc_setup_type type,
>  	int err;
>  
>  	sch->flags &= ~TCQ_F_OFFLOADED;
> +	if (!READ_ONCE(sch->qstats.backlog))
> +		sch->flags &= ~TCQ_F_HW_BACKLOG;
>  	if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
>  		return 0;
>  
> @@ -899,7 +943,7 @@ int qdisc_offload_dump_helper(struct Qdisc *sch, enum tc_setup_type type,
>  		return 0;
>  
>  	if (!err)
> -		sch->flags |= TCQ_F_OFFLOADED;
> +		sch->flags |= TCQ_F_OFFLOADED | TCQ_F_HW_BACKLOG;
>  
>  	return err;
>  }

[Severity: Medium]
Can the data path see TCQ_F_HW_BACKLOG clear while the driver is writing
hardware bytes into the counter?

This runs on the dump path with RTNL held but not the qdisc lock. The latch
is cleared before ndo_setup_tc() and only set again when that returns 0.
Inside ndo_setup_tc(), drivers add signed occupancy deltas into the same
counter:

mlxsw_sp_qdisc_update_stats():
	stats_ptr->qstats->backlog += mlxsw_sp_cells_bytes(mlxsw_sp, backlog);

nfp_abm_stats_calculate():
	qstats->backlog += new->backlog_bytes - old->backlog_bytes

Suppose the counter is 0 while the driver's base still holds B bytes. That
can happen after qdisc_reset() on a link flap, which keeps the latch
because TCQ_F_OFFLOADED is set. A negative delta then wraps the counter to
about 2^32 - B.

qdisc_backlog_overflow() would then see the latch clear and a counter above
QDISC_MAX_BACKLOG. It would drop every enqueue until the latch is set
again.

The nfp red/gred/mq stats paths adjust the backlog and then do:

	return qdisc->offloaded ? 0 : -EOPNOTSUPP;

Once offload has stopped, the helper turns -EOPNOTSUPP into 0 and never
sets the latch again. Could the qdisc then drop all software traffic until
it is reset?

The plain &= and |= here can also race with read-modify-writes of
qdisc->flags on the data path under the root lock, for example
qdisc_warn_nonwc() setting TCQ_F_WARN_NONWC. Can that race lose
TCQ_F_HW_BACKLOG?

[ ... ]

> diff --git a/net/sched/sch_netem.c b/net/sched/sch_netem.c
> index 1dc2924890eb5..c25ce4c3175e9 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);
> +	}
> +

[Severity: Medium]
Does netem's GSO corruption split also need the
qdisc_ancestors_backlog_room() pre-check that tbf_segment() and dualpi2
got?

The commit message says "A split whose segments would grow an ancestor past
the room is refused before any segment is enqueued". When q->corrupt fires
on a GSO skb, netem_segment() splits it. The finish_segs block re-enqueues
each segment with pkt_len = segs->len and then does:

		/* Parent qdiscs accounted for 1 skb of size @prev_len */
		qdisc_tree_reduce_backlog(sch, -(nb - 1), -(len - prev_len));

Each ancestor applies +(segment_sum - prev_len) through the modular
qdisc_qstats_backlog_apply(). Their own enqueue guards only saw prev_len.
With a size table, prev_len leaves out the headers replicated per segment.
An ancestor sitting near QDISC_MAX_BACKLOG would wrap once the segment sum
goes past about 2 MiB, if a segment sum that large is reachable.

The duplicate re-enters at the root with tc_depth = 1. The corrupt check
does not look at tc_depth, so the duplicate can be split as well, which
lowers the threshold to about 1 MiB. The later cake patch in the series
does not change netem.

[ ... ]

> diff --git a/net/sched/sch_taprio.c b/net/sched/sch_taprio.c
> index 5a1dca9d5e9de..67bd2f8b56a71 100644
> --- a/net/sched/sch_taprio.c
> +++ b/net/sched/sch_taprio.c

[ ... ]

> @@ -585,10 +589,79 @@ static bool taprio_skb_exceeds_queue_max_sdu(struct Qdisc *sch,
>  	return exceeds;
>  }
>  
> +/* taprio's counters mirror its children, but a child's ->change() can drop
> + * queued packets without an enqueue or dequeue to measure (the child is
> + * TCQ_F_NOPARENT, so its own reconciliation stops there).  Recompute the
> + * aggregate from the children when it matters.
> + */

[Severity: Low]
Do the default software-mode children actually have TCQ_F_NOPARENT?

taprio_init() creates one pfifo per tx queue with qdisc_create_dflt() and
does not set the flag. taprio_attach() only sets it under
FULL_OFFLOAD_IS_ENABLED(). Only children grafted later through
taprio_graft() get it.

For a default child, qdisc_tree_reduce_backlog() would walk up to taprio.
taprio_mirror_child() would then apply the same change again, so it would
be counted twice.

pfifo never reconciles with its parent, so this is harmless today. Still,
the mirror depends on an invariant that nothing enforces.

> +static void taprio_resync_backlog(struct Qdisc *sch)
> +{
> +	struct taprio_sched *q = qdisc_priv(sch);
> +	struct net_device *dev = qdisc_dev(sch);
> +	u32 backlog = 0;
> +	u32 qlen = 0;
> +	int i;
> +
> +	for (i = 0; i < dev->num_tx_queues; i++) {
> +		struct Qdisc *child = q->qdiscs[i];
> +
> +		if (!child || child == &noop_qdisc)
> +			continue;
> +		backlog += READ_ONCE(child->qstats.backlog);
> +		qlen += qdisc_qlen(child);
> +	}
> +
> +	WRITE_ONCE(sch->qstats.backlog, backlog);
> +	WRITE_ONCE(sch->q.qlen, qlen);
> +}

[Severity: Medium]
Does this resync drop the packets that taprio itself is holding in
sch->gso_skb and sch->skb_bad_txq?

In software mode taprio is the root qdisc of a multi-queue device and
does not have TCQ_F_ONETXQUEUE, so dequeue_skb() goes through
try_bulk_dequeue_skb_slow(). Any packet dequeued for a different tx
queue than the head is parked with qdisc_enqueue_skb_bad_txq(). A
driver requeue goes back through dev_requeue_skb() into sch->gso_skb.
Both paths do:

	qdisc_qstats_backlog_inc(q, skb);
	qdisc_qlen_inc(q);

That increment lands on taprio's own counters. By then
taprio_dequeue_from_txq() has already removed the packet from the
child, and taprio_mirror_child() has already removed it from taprio.

taprio_resync_backlog() overwrites sch->qstats.backlog and sch->q.qlen
with the sum over q->qdiscs[] only, so the parked packets drop out of
the totals. When __skb_dequeue_bad_txq() or dequeue_skb() later hands
them out, qdisc_qstats_backlog_dec() and qdisc_qlen_dec() remove them
a second time. The backlog saturates at 0 under the new helper.
q.qlen is a plain u32, though, and wraps to 0xffffffff, so
qdisc_is_empty() reads false until the next resync.

This does not need the rare overflow path. taprio_dump_stats() now
resyncs on every dump, so an ordinary tc -s qdisc show while packets
are parked is enough.

Should the resync also add skb_queue_len() and the qdisc_pkt_len() sum
of sch->gso_skb and sch->skb_bad_txq? Or would it be simpler to fold
only the children's drift into the mirror, instead of replacing the
totals outright?

[ ... ]

> +/* 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,
> +				   -((s64)READ_ONCE(child->qstats.backlog) -
> +				     (s64)backlog_before));
> +	WRITE_ONCE(sch->q.qlen,
> +		   sch->q.qlen + (int)(qdisc_qlen(child) - qlen_before));
> +}

[Severity: Medium]
Can taprio's own counter still wrap when a grafted child splits a GSO skb,
for example a tbf child going through tbf_segment()?

taprio_graft() does:

		new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;

qdisc_ancestors_backlog_room() tests the child's own flags before it looks
up any parent:

		if (READ_ONCE(p->flags) & TCQ_F_NOPARENT)
			break;

So the room check in tbf_segment() returns true without ever looking at
taprio.

taprio_enqueue_one() only checks qdisc_pkt_len() of the original skb.
taprio_mirror_child() then applies the child's whole segment sum through
the modular qdisc_qstats_backlog_apply().

If the segment sum goes past about 2 MiB, taprio's backlog wraps to a small
value. taprio_backlog_overflow() then never triggers a resync. Even if it
did, taprio_resync_backlog() adds into a u32 backlog, so it would write
back a total that has already wrapped.

[ ... ]

> @@ -2550,6 +2653,10 @@ static int taprio_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
>  		.cmd = TAPRIO_CMD_STATS,
>  	};
>  
> +	spin_lock_bh(qdisc_lock(sch));
> +	taprio_resync_backlog(sch);
> +	spin_unlock_bh(qdisc_lock(sch));
> +
>  	return taprio_dump_xstats(sch, d, &offload, &offload.stats);
>  }

[Severity: Low]
Will the qlen reported in the same dump match the resynced backlog?
tc_fill_qdisc() samples qlen before it calls ->dump_stats():

	qlen = qdisc_qlen_sum(q);
	...
	if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0)
	...
	    gnet_stats_copy_queue(&d, cpu_qstats, &q->qstats, qlen) < 0)

When the mirror is stale, one dump shows the resynced backlog next to the
old qlen. The corrected qlen only shows up in the next dump.

> diff --git a/net/sched/sch_tbf.c b/net/sched/sch_tbf.c
> index 58c9687efb0fd..76ae871cbe400 100644
> --- a/net/sched/sch_tbf.c
> +++ b/net/sched/sch_tbf.c
> @@ -221,6 +221,24 @@ static int tbf_segment(struct sk_buff *skb, struct Qdisc *sch,
>  	if (IS_ERR_OR_NULL(segs))
>  		return qdisc_drop(skb, sch, to_free);
>  
> +	{
> +		struct sk_buff *seg;
> +		u64 slen = 0;
> +
> +		skb_list_walk_safe(segs, seg, nskb)
> +			slen += seg->len;
> +
> +		if (unlikely((u64)sch->qstats.backlog + slen > U32_MAX ||
> +			     (slen > prev_len &&
> +			      !qdisc_ancestors_backlog_room(sch,
> +							    slen -
> +							    prev_len)))) {
> +			consume_skb(skb);
> +			qdisc_qstats_overlimit(sch);
> +			return qdisc_drop_all(segs, sch, to_free);
> +		}
> +	}

[Severity: Low]
What drop reason do these segments end up with?

qdisc_drop_all() never calls tcf_set_qdisc_drop_reason().
__tcf_kfree_skb_list() reads tc_skb_cb(skb)->drop_reason. That field is at
cb offset 32, which overlaps SKB_GSO_CB, so each fresh segment carries
whatever skb_gso_segment() wrote there.

With CONFIG_DEBUG_NET, a value of 0 is SKB_NOT_DROPPED_YET, and that
triggers the DEBUG_NET warning in kfree_skb_reason().

The split refusal in dualpi2_qdisc_enqueue() has the same pattern. Every
other refusal this patch adds uses QDISC_DROP_OVERLIMIT, and the later cake
patch in the series tags each segment for exactly this reason. Should tbf
and dualpi2 tag their segments too?

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/QDISC-BA27.v14.r0.20261128181551%40mojatatu.com

  reply	other threads:[~2026-11-30 10:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29  9:06 [PATCH net-next v14 0/4] net/sched: cap the accounted backlog before it can wrap Jamal Hadi Salim
2026-11-29  9:06 ` [PATCH net-next v14 1/4] net/sched: cake: return the classifier result to the caller Jamal Hadi Salim
2026-11-29  9:06 ` [PATCH net-next v14 2/4] net/sched: cake: use a dedicated split-GSO list iterator Jamal Hadi Salim
2026-11-29  9:06 ` [PATCH net-next v14 3/4] net/sched: cap the accounted backlog before it can wrap Jamal Hadi Salim
2026-11-30 10:01   ` netdev-bot+sashiko [this message]
2026-11-29  9:06 ` [PATCH net-next v14 4/4] net/sched: cake: hash before segmenting and reject an overlong split Jamal Hadi Salim
2026-11-30 10:01   ` netdev-bot+sashiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179603287226.3146.11753213620888850974@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=bjorn@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=jhs@mojatatu.com \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sashiko-bot@kernel.org \
    --cc=stephen@networkplumber.org \
    --cc=toke@toke.dk \
    --cc=victor@mojatatu.com \
    --cc=vinicius.gomes@intel.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox