🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #27434

[PATCH net-next v16 3/4] net/sched: cap the accounted backlog before it can wrap

#27434
From: Jamal Hadi Salim
Date: Sat, 05 Dec 2026 17:20
1576 lines
66347 bytes
This is a followup of commit 8f735d64382d ("net/sched: bound qdisc_pkt_len
to prevent qdisc soft lockup") as reported by Sashiko.

The issue: every qdisc that books into the shared 32-bit
sch->qstats.backlog can be made to wrap it.  A size table inflates
qdisc_pkt_len() to QDISC_PKT_LEN_MAX, and a BIG TCP aggregate adds the
headers replicated per segment, so a few thousand packets wrap the
counter.  An AQM then reads a small backlog and drops the wrong flow; in
fq_codel it can pick an empty flow and dereference NULL.

The Fix: drop at enqueue once the stored backlog cannot represent the
packet's accounted length.  QDISC_MAX_BACKLOG leaves room for one packet
and a netem duplicate of it; a larger wire estimate is tested against the
room that remains.  The writers below the ceiling saturate so its
precondition holds, and the tree reconciliation stays modular, so a child
that compensates before its parent has booked still cancels.

An offloaded qdisc is exempt, because its counter carries hardware bytes
the core does not own.  The test is coarse on purpose: it keys on
TCQ_F_OFFLOADED, not on whether the counter holds hardware bytes right
now.  An exact latch would have to follow the driver's own writes to the
counter, and the stats path still makes those; that is a separate change.

Qdiscs that book into per-CPU statistics are outside the ceiling too: the
shared counter stays zero there, the per-CPU counters are stats-only, and
no AQM reads them.

htb_init() reaches the same state through htb_offload(), so it needs no
second setter.

taprio mirrors its child's net change, so it rebuilds from the children
plus the packets it parks on itself, in ->dump() so the reported qlen and
backlog are one sample.  htb bumps q->overlimits, which htb_dump() copies
over the qstats field.

Full-offload taprio is deliberately not marked TCQ_F_NO_BACKLOG: unlike
mq and mqprio it does not keep a zero counter.  Its qlen and backlog
mirror its children's, the mirror saturates at both ends, and it is
re-synced in taprio's own guard and at dump and graft time.

A splitting child that crosses it therefore reads a snapshot that can lag
the last update by one window: that can mis-admit or wrongly refuse a
split within the window, but it cannot read a counter wrapped low and
refuse a split that has room.

The ancestor-room check covers a split whose segments would grow an
ancestor past the room: tbf, dualpi2 and netem's corrupt split here; cake
gets it in the next patch.  It reserves two copies, because a duplicate
books its clone twice.

Some context: qdisc_pkt_len() stays truthful, so BPF wire_len, act_police
and the HTB/TBF shapers read the real length.

Five user-visible changes come with it.  netem books the original before
its duplicate clone re-enters the root, and plug's limit test counts
qdisc_pkt_len(), so it follows a size table.  hhf and skbprio reconcile
the packet they evict on the CN path.

taprio stops counting a child's drops as its own and reports qlen and
backlog from the child mirror.  red tests its child's counter as well as
its own, since it feeds that backlog into its average.

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.

Fixes: 175f9c1bba9b ("net_sched: Add size table for qdiscs")
Reported-by: Sashiko (nipa) <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/netdev/20260818101130.16203-1-jhs@mojatatu.com/
Link: https://sashiko.dev/#/patchset/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/
Reviewed-by: Victor Nogueira <victor@mojatatu.com>
Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>

---
v15 -> v16:
- A new TCQ_F_NO_BACKLOG flag marks the pass-through parents (multiq, mq,
mqprio); the ancestor-room walk skips them and the tree reconciliation no
longer debits them, so a child drop cannot wrap their dump-only counter low
and make a split that has room be refused.  (Sashiko nipa main-1-0,
gemini)
- netem: pre-book the original before the duplicate clone re-enters the root;
carry the duplicate depth onto the re-entering trailing segments of a
corrupt split (only when duplication is armed, and only from zero, so
act_mirred's two-bit tc_depth budget is not spent); reserve twice the larger
of the segment sum and the original length on netem and every ancestor.
(Sashiko nipa and gemini High)
- taprio: saturate the child mirror at both ends and rebuild it from the
children plus parked packets at graft, at dump and inside the guard before
a refusal.  (Sashiko nipa main-2-8, gemini)
- Reserve twice the larger of the segment sum and the original length in the
split headroom at tbf and dualpi2, so an original above QDISC_PKT_LEN_MAX
cannot fill an ancestor to the ceiling before it is booked.  (Sashiko
gemini and nipa)
- Tag every segment of a refused split list with its drop reason before it is
freed at tbf, dualpi2, taprio's SDU drop and netem's limit and linearize
arms, so __skb_gso_segment()'s header offset is not read as a core skb
reason.  (Sashiko nipa main-2-7, main-1-2)
- Set and clear the offload and non-work-conserving qdisc flags with a
compare-and-swap, so a dump and a parent's dequeue cannot lose each other's
write (Sashiko nipa main-5-18); keep the coarse TCQ_F_OFFLOADED exemption
and clear it only on a failed setup (Sashiko nipa main-7-25, main-2-8).
---
 include/net/pkt_sched.h   |   3 +-
 include/net/sch_generic.h |  75 ++++++++++++++++-
 net/sched/sch_api.c       |  66 ++++++++++++---
 net/sched/sch_cake.c      |   9 ++-
 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   |  30 ++++++-
 net/sched/sch_etf.c       |   6 ++
 net/sched/sch_ets.c       |   6 ++
 net/sched/sch_fifo.c      |  15 +++-
 net/sched/sch_fq.c        |   6 ++
 net/sched/sch_fq_codel.c  |  11 ++-
 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       |  23 +++++-
 net/sched/sch_htb.c       |  16 +++-
 net/sched/sch_mq.c        |   2 +-
 net/sched/sch_mqprio.c    |   2 +-
 net/sched/sch_multiq.c    |   2 +
 net/sched/sch_netem.c     |  85 +++++++++++++++++++-
 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       |  11 ++-
 net/sched/sch_skbprio.c   |  19 ++++-
 net/sched/sch_taprio.c    | 165 +++++++++++++++++++++++++++++++++-----
 net/sched/sch_tbf.c       |  32 +++++++-
 34 files changed, 610 insertions(+), 69 deletions(-)

diff --git a/include/net/pkt_sched.h b/include/net/pkt_sched.h
index 90d3e7943b19..d24e52ef1f43 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;
@@ -290,7 +289,7 @@ static inline void qdisc_warn_nonwc(const char *txt, struct Qdisc *qdisc)
 	if (!(qdisc->flags & TCQ_F_WARN_NONWC)) {
 		pr_warn("%s: %s qdisc %X: is non-work-conserving?\n",
 			txt, qdisc->ops->id, qdisc->handle >> 16);
-		qdisc->flags |= TCQ_F_WARN_NONWC;
+		qdisc_flag_set(qdisc, TCQ_F_WARN_NONWC);
 	}
 }
 
diff --git a/include/net/sch_generic.h b/include/net/sch_generic.h
index 1acaadb3e2cf..b62d9b5d8c8b 100644
--- a/include/net/sch_generic.h
+++ b/include/net/sch_generic.h
@@ -92,6 +92,7 @@ struct Qdisc {
 #define TCQ_F_NOLOCK		0x100 /* qdisc does not require locking */
 #define TCQ_F_OFFLOADED		0x200 /* qdisc is offloaded to HW */
 #define TCQ_F_DEQUEUE_DROPS	0x400 /* ->dequeue() can drop packets in q->to_free */
+#define TCQ_F_NO_BACKLOG	0x800 /* never books qstats.backlog on the data path */
 
 	u32			limit;
 	const struct Qdisc_ops	*ops;
@@ -142,6 +143,30 @@ struct Qdisc {
 	long privdata[] ____cacheline_aligned;
 };
 
+/* flags is read and written from different contexts (a dump, a parent's
+ * dequeue); a plain read-modify-write can lose a bit to an interleaved
+ * writer, so the offload/ceiling bits go through a compare-and-swap.
+ */
+static inline void qdisc_flag_set(struct Qdisc *q, unsigned int flag)
+{
+	unsigned int old, new;
+
+	do {
+		old = READ_ONCE(q->flags);
+		new = old | flag;
+	} while (cmpxchg(&q->flags, old, new) != old);
+}
+
+static inline void qdisc_flag_clear(struct Qdisc *q, unsigned int flag)
+{
+	unsigned int old, new;
+
+	do {
+		old = READ_ONCE(q->flags);
+		new = old & ~flag;
+	} while (cmpxchg(&q->flags, old, new) != old);
+}
+
 static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
 {
 	if (qdisc->flags & TCQ_F_BUILTIN)
@@ -734,7 +759,9 @@ void qdisc_reset(struct Qdisc *qdisc);
 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);
+void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, s64 len);
+/* Is there room for @extra more bytes on every ancestor of @sch? */
+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,
@@ -775,7 +802,7 @@ static inline int
 qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type,
 			  void *type_data)
 {
-	q->flags &= ~TCQ_F_OFFLOADED;
+	qdisc_flag_clear(q, TCQ_F_OFFLOADED);
 	return 0;
 }
 
@@ -915,6 +942,31 @@ 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 */
+
+/* Room for a 1 MiB packet and a netem duplicate of it. */
+#define QDISC_MAX_BACKLOG	(U32_MAX - 2 * QDISC_PKT_LEN_MAX)
+
+/* Would this packet's accounted length wrap sch->qstats.backlog?  GSO can
+ * exceed QDISC_PKT_LEN_MAX, so compare against the real headroom; an
+ * offloaded counter carries hardware bytes the core does not own.
+ */
+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 +1043,28 @@ 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 offload update can leave the counter short. */
+	WRITE_ONCE(sch->qstats.backlog, backlog > val ? backlog - val : 0);
+}
+
+/* Apply a signed net change: positive removes, negative grows. */
+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));
 }
 
 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 update can leave the counter short; saturate. */
+	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..1dbeb4263d5a 100644
--- a/net/sched/sch_api.c
+++ b/net/sched/sch_api.c
@@ -844,7 +844,7 @@ static int qdisc_alloc_handle(struct net_device *dev, u32 *handlep)
 	return 0;
 }
 
-void qdisc_tree_reduce_backlog(struct Qdisc *sch, int n, int len)
+void qdisc_tree_reduce_backlog(struct Qdisc *sch, int n, s64 len)
 {
 	const struct Qdisc_class_ops *cops;
 	unsigned long cl;
@@ -877,31 +877,79 @@ 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);
+		/* A pass-through parent never booked these bytes. */
+		if (!(sch->flags & TCQ_F_NO_BACKLOG))
+			qdisc_qstats_backlog_apply(sch, len);
 		__qdisc_qstats_drop(sch, drops);
 	}
 	rcu_read_unlock();
 }
 EXPORT_SYMBOL(qdisc_tree_reduce_backlog);
 
+/* Room left for @extra bytes on every ancestor of @sch; a split's growth
+ * reaches them without passing their own enqueue guard.
+ */
+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;
+
+		/* Cross NOPARENT: a mirror parent still books the child's
+		 * growth.  A pass-through parent never books it, so its
+		 * counter cannot be pushed over by this delta.
+		 */
+		p = qdisc_lookup_rcu(qdisc_dev(p), TC_H_MAJ(parentid));
+		if (!p)
+			break;
+
+		/* Hardware bytes are not ours to bound. */
+		if (unlikely(READ_ONCE(p->flags) &
+			     (TCQ_F_OFFLOADED | TCQ_F_NO_BACKLOG)))
+			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)
 {
 	struct net_device *dev = qdisc_dev(sch);
 	int err;
 
-	sch->flags &= ~TCQ_F_OFFLOADED;
-	if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
+	if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc) {
+		qdisc_flag_clear(sch, TCQ_F_OFFLOADED);
 		return 0;
+	}
 
 	err = dev->netdev_ops->ndo_setup_tc(dev, type, type_data);
-	if (err == -EOPNOTSUPP)
+	if (!err) {
+		qdisc_flag_set(sch, TCQ_F_OFFLOADED);
 		return 0;
+	}
 
-	if (!err)
-		sch->flags |= TCQ_F_OFFLOADED;
-
-	return err;
+	/* Also the ceiling's offload exemption: clear only on failure, so a
+	 * dump in progress keeps it.
+	 */
+	qdisc_flag_clear(sch, TCQ_F_OFFLOADED);
+	/* -EOPNOTSUPP means "no hardware stats", not a dump failure. */
+	return err == -EOPNOTSUPP ? 0 : err;
 }
 EXPORT_SYMBOL(qdisc_offload_dump_helper);
 
diff --git a/net/sched/sch_cake.c b/net/sched/sch_cake.c
index 2bc0dac9123a..f354ad1467db 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,
@@ -1982,7 +1989,7 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 
 	if (same_flow) {
 		qdisc_tree_reduce_backlog(sch, prev_qlen - 1,
-					  prev_backlog - len);
+					  (s64)prev_backlog - (s64)len);
 		return NET_XMIT_CN;
 	}
 	qdisc_tree_reduce_backlog(sch, prev_qlen, prev_backlog);
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..18a4e1949dde 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))
@@ -467,6 +468,28 @@ static int dualpi2_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 		cnt = 0;
 		byte_len = 0;
 		orig_len = qdisc_pkt_len(skb);
+		{
+			struct sk_buff *seg;
+			u64 slen = 0;
+			u64 reserve;
+
+			skb_list_walk_safe(nskb, seg, next)
+				slen += seg->len;
+
+			/* The original was not booked on the ancestors yet,
+			 * and a duplicate can book a second copy.
+			 */
+			reserve = 2 * max_t(u64, slen, orig_len);
+			if ((u64)sch->qstats.backlog + reserve > U32_MAX ||
+			    !qdisc_ancestors_backlog_room(sch, reserve)) {
+				consume_skb(skb);
+				qdisc_qstats_overlimit(sch);
+				skb_list_walk_safe(nskb, seg, next)
+					tcf_set_qdisc_drop_reason(seg,
+								  QDISC_DROP_OVERLIMIT);
+				return qdisc_drop_all(nskb, sch, to_free);
+			}
+		}
 		skb_list_walk_safe(nskb, nskb, next) {
 			skb_mark_not_on_list(nskb);
 
@@ -496,7 +519,8 @@ static int dualpi2_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 			 * backlog, compensate this if any nskb is enqueued.
 			 */
 			qdisc_tree_reduce_backlog(sch, 1 - cnt,
-						  orig_len - byte_len);
+						  (s64)orig_len -
+						  (s64)byte_len);
 		}
 		consume_skb(skb);
 		return cnt > 0 ? NET_XMIT_SUCCESS : err;
@@ -797,7 +821,7 @@ static int dualpi2_change(struct Qdisc *sch, struct nlattr *opt,
 {
 	struct nlattr *tb[TCA_DUALPI2_MAX + 1];
 	struct dualpi2_sched_data *q;
-	int old_backlog;
+	u32 old_backlog;
 	int old_qlen;
 	int err;
 
@@ -939,7 +963,7 @@ static int dualpi2_change(struct Qdisc *sch, struct nlattr *opt,
 		}
 	}
 	qdisc_tree_reduce_backlog(sch, old_qlen - qdisc_qlen(sch),
-				  old_backlog - sch->qstats.backlog);
+				  (s64)old_backlog - (s64)sch->qstats.backlog);
 
 	sch_tree_unlock(sch);
 	return 0;
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..85c97e60032f 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);
 
@@ -52,7 +64,8 @@ static int pfifo_tail_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 	qdisc_qstats_drop(sch);
 	qdisc_enqueue_tail(skb, sch);
 
-	qdisc_tree_reduce_backlog(sch, 0, prev_backlog - sch->qstats.backlog);
+	qdisc_tree_reduce_backlog(sch, 0, (s64)prev_backlog -
+				  (s64)sch->qstats.backlog);
 	return NET_XMIT_CN;
 }
 
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..68d508d622c1 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);
@@ -243,7 +252,7 @@ static int fq_codel_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 	 */
 	if (ret == idx) {
 		qdisc_tree_reduce_backlog(sch, prev_qlen - 1,
-					  prev_backlog - pkt_len);
+					  (s64)prev_backlog - (s64)pkt_len);
 		return NET_XMIT_CN;
 	}
 	qdisc_tree_reduce_backlog(sch, prev_qlen, prev_backlog);
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..6706b33ccadc 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,11 +416,21 @@ 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,
+					  (s64)prev_backlog -
+					  (s64)sch->qstats.backlog -
+					  (s64)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);
+	qdisc_tree_reduce_backlog(sch, 1,
+				  (s64)prev_backlog -
+				  (s64)sch->qstats.backlog);
 	return NET_XMIT_SUCCESS;
 }
 
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_mq.c b/net/sched/sch_mq.c
index 5a6b14557d85..d0756e4b1f31 100644
--- a/net/sched/sch_mq.c
+++ b/net/sched/sch_mq.c
@@ -99,7 +99,7 @@ int mq_init_common(struct Qdisc *sch, struct nlattr *opt,
 		qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
 	}
 
-	sch->flags |= TCQ_F_MQROOT;
+	sch->flags |= TCQ_F_MQROOT | TCQ_F_NO_BACKLOG;
 	return 0;
 }
 EXPORT_SYMBOL_NS_GPL(mq_init_common, "NET_SCHED_INTERNAL");
diff --git a/net/sched/sch_mqprio.c b/net/sched/sch_mqprio.c
index 6ced7008ef5c..1deec020ecca 100644
--- a/net/sched/sch_mqprio.c
+++ b/net/sched/sch_mqprio.c
@@ -424,7 +424,7 @@ static int mqprio_init(struct Qdisc *sch, struct nlattr *opt,
 	for (i = 0; i < TC_BITMASK + 1; i++)
 		netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]);
 
-	sch->flags |= TCQ_F_MQROOT;
+	sch->flags |= TCQ_F_MQROOT | TCQ_F_NO_BACKLOG;
 	return 0;
 }
 
diff --git a/net/sched/sch_multiq.c b/net/sched/sch_multiq.c
index 66df30939aa5..b58e9454345b 100644
--- a/net/sched/sch_multiq.c
+++ b/net/sched/sch_multiq.c
@@ -255,6 +255,8 @@ static int multiq_init(struct Qdisc *sch, struct nlattr *opt,
 	for (i = 0; i < q->max_bands; i++)
 		q->queues[i] = &noop_qdisc;
 
+	sch->flags |= TCQ_F_NO_BACKLOG;
+
 	return multiq_tune(sch, opt, extack);
 }
 
diff --git a/net/sched/sch_netem.c b/net/sched/sch_netem.c
index 1dc2924890eb..c0e539125f03 100644
--- a/net/sched/sch_netem.c
+++ b/net/sched/sch_netem.c
@@ -446,10 +446,34 @@ static struct sk_buff *netem_segment(struct sk_buff *skb, struct Qdisc *sch,
 		qdisc_drop(skb, sch, to_free);
 		return NULL;
 	}
+
 	consume_skb(skb);
 	return segs;
 }
 
+/* The split replicates headers per segment, so the sum outgrows what the
+ * guard admitted; refuse it if an ancestor has no room.
+ */
+static bool netem_split_over_ceiling(struct Qdisc *sch,
+				     const struct sk_buff *skb,
+				     const struct sk_buff *segs,
+				     unsigned int prev_len)
+{
+	const struct sk_buff *seg;
+	u64 slen, reserve;
+
+	slen = skb ? skb->len : 0;
+	for (seg = segs; seg; seg = seg->next)
+		slen += seg->len;
+
+	/* The original was not booked on the ancestors yet, and a
+	 * duplicate can book a second copy of the segment sum.
+	 */
+	reserve = 2 * max_t(u64, slen, prev_len);
+	return (u64)READ_ONCE(sch->qstats.backlog) + reserve > U32_MAX ||
+	       !qdisc_ancestors_backlog_room(sch, reserve);
+}
+
 /*
  * Insert one skb into qdisc.
  * Note: parent depends on return value to account for queue length.
@@ -467,6 +491,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;
 
@@ -517,6 +547,8 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 	 */
 	if (q->corrupt && q->corrupt >= get_crandom(&q->corrupt_cor, &q->prng)) {
 		if (skb_is_gso(skb)) {
+			struct sk_buff *seg;
+
 			skb = netem_segment(skb, sch, to_free);
 			if (!skb) {
 				WRITE_ONCE(q->allocation_errors, q->allocation_errors + 1);
@@ -526,6 +558,26 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 			segs = skb->next;
 			skb_mark_not_on_list(skb);
 			qdisc_skb_cb(skb)->pkt_len = skb->len;
+
+			if (unlikely(netem_split_over_ceiling(sch, skb, segs,
+							      prev_len))) {
+				if (skb2) {
+					tcf_set_qdisc_drop_reason(skb2,
+								  QDISC_DROP_OVERLIMIT);
+					__qdisc_drop(skb2, to_free);
+				}
+				qdisc_qstats_overlimit(sch);
+				qdisc_drop_reason(skb, sch, to_free,
+						  QDISC_DROP_OVERLIMIT);
+				while (segs) {
+					seg = segs->next;
+					skb_mark_not_on_list(segs);
+					qdisc_drop_reason(segs, sch, to_free,
+							  QDISC_DROP_OVERLIMIT);
+					segs = seg;
+				}
+				return NET_XMIT_DROP;
+			}
 		}
 
 		skb = skb_unclone_full(skb, GFP_ATOMIC);
@@ -537,7 +589,7 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 		if (skb_linearize(skb) ||
 		    (skb->ip_summed == CHECKSUM_PARTIAL && skb_checksum_help(skb))) {
 			WRITE_ONCE(q->allocation_errors, q->allocation_errors + 1);
-			qdisc_drop(skb, sch, to_free);
+			qdisc_drop_reason(skb, sch, to_free, QDISC_DROP_GENERIC);
 			skb = NULL;
 			goto finish_segs;
 		}
@@ -550,11 +602,17 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 	}
 
 	if (unlikely(sch->q.qlen >= sch->limit)) {
+		struct sk_buff *seg;
+
 		/* re-link segs, so that qdisc_drop_all() frees them all */
 		skb->next = segs;
+		for (seg = skb; seg; seg = seg->next)
+			tcf_set_qdisc_drop_reason(seg, QDISC_DROP_OVERLIMIT);
 		qdisc_drop_all(skb, sch, to_free);
-		if (skb2)
+		if (skb2) {
+			tcf_set_qdisc_drop_reason(skb2, QDISC_DROP_OVERLIMIT);
 			__qdisc_drop(skb2, to_free);
+		}
 		return NET_XMIT_DROP;
 	}
 
@@ -567,7 +625,16 @@ 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 before this packet is booked; pre-book
+		 * it so a guard cannot admit two, and undo below.
+		 */
+		qstats_backlog_add(sch, prev_len);
+		qdisc_qlen_inc(sch);
+		qdisc_tree_reduce_backlog(sch, 0, -(s64)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;
 	}
 
@@ -643,8 +710,10 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 	}
 
 finish_segs:
-	if (skb2)
+	if (skb2) {
+		tcf_set_qdisc_drop_reason(skb2, QDISC_DROP_GENERIC);
 		__qdisc_drop(skb2, to_free);
+	}
 
 	if (segs) {
 		unsigned int len, last_len;
@@ -656,6 +725,13 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 		while (segs) {
 			skb2 = segs->next;
 			skb_mark_not_on_list(segs);
+			/* Only a re-entering segment has to be marked a
+			 * duplicate, and tc_depth is shared with
+			 * act_mirred's recursion budget, so spend just one
+			 * and only when duplication is armed.
+			 */
+			if (q->duplicate && !segs->tc_depth)
+				segs->tc_depth = 1;
 			qdisc_skb_cb(segs)->pkt_len = segs->len;
 			last_len = segs->len;
 			rc = qdisc_enqueue(segs, sch, to_free);
@@ -669,7 +745,8 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 			segs = skb2;
 		}
 		/* Parent qdiscs accounted for 1 skb of size @prev_len */
-		qdisc_tree_reduce_backlog(sch, -(nb - 1), -(len - prev_len));
+		qdisc_tree_reduce_backlog(sch, -(nb - 1),
+					  -((s64)len - (s64)prev_len));
 	} else if (!skb) {
 		return NET_XMIT_DROP;
 	}
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..1cde38d5957b 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);
 
@@ -466,7 +472,8 @@ sfq_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
 	 * from this flow.
 	 */
 	if (qlen != slot->qlen) {
-		qdisc_tree_reduce_backlog(sch, 0, dropped - qdisc_pkt_len(skb));
+		qdisc_tree_reduce_backlog(sch, 0,
+					  (s64)dropped - (s64)qdisc_pkt_len(skb));
 		return NET_XMIT_CN;
 	}
 
diff --git a/net/sched/sch_skbprio.c b/net/sched/sch_skbprio.c
index 52abfb4015a3..c321df3b1d40 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,13 @@ 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,
+				  (s64)to_drop_len -
+				  (s64)qdisc_pkt_len(skb));
+
 	return NET_XMIT_CN;
 }
 
diff --git a/net/sched/sch_taprio.c b/net/sched/sch_taprio.c
index 5a1dca9d5e9d..62ac32d095ce 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,90 @@ static bool taprio_skb_exceeds_queue_max_sdu(struct Qdisc *sch,
 	return exceeds;
 }
 
+/* Rebuild from the children plus what we parked on ourselves; a child's
+ * ->change() drop never reaches us.  Callers hold qdisc_lock(sch).
+ */
+static void taprio_resync_backlog(struct Qdisc *sch)
+{
+	struct taprio_sched *q = qdisc_priv(sch);
+	struct net_device *dev = qdisc_dev(sch);
+	struct sk_buff *skb;
+	u64 backlog = 0;
+	int 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);
+	}
+
+	skb_queue_walk(&sch->gso_skb, skb) {
+		backlog += qdisc_pkt_len(skb);
+		qlen++;
+	}
+	skb_queue_walk(&sch->skb_bad_txq, skb) {
+		backlog += qdisc_pkt_len(skb);
+		qlen++;
+	}
+
+	/* Saturate: a wrapped-low value would tell the guard there is room. */
+	WRITE_ONCE(sch->qstats.backlog, (u32)min_t(u64, backlog, U32_MAX));
+	WRITE_ONCE(sch->q.qlen, qlen);
+}
+
+/* Resync only when the guard would refuse; the queue count can shrink. */
+static bool taprio_backlog_overflow(struct Qdisc *sch,
+				    const struct sk_buff *skb)
+{
+	struct taprio_sched *q = qdisc_priv(sch);
+
+	if (!qdisc_backlog_overflow(sch, skb))
+		return false;
+
+	if (!q->mirror_child)
+		taprio_resync_backlog(sch);
+	return qdisc_backlog_overflow(sch, skb);
+}
+
+/* The child cannot correct us (TCQ_F_NOPARENT); mirror its net change. */
+static void taprio_mirror_child(struct Qdisc *sch, struct Qdisc *child,
+				u32 backlog_before, int qlen_before)
+{
+	s64 backlog;
+
+	/* Saturate instead of wrapping: a peek() can grow the child by
+	 * more than there is room for, and a wrapped-low value would tell
+	 * the guard there is room.
+	 */
+	backlog = (s64)READ_ONCE(sch->qstats.backlog) +
+		  (s64)READ_ONCE(child->qstats.backlog) - (s64)backlog_before;
+	if (backlog < 0)
+		backlog = 0;
+	else if (backlog > (s64)U32_MAX)
+		backlog = U32_MAX;
+	WRITE_ONCE(sch->qstats.backlog, (u32)backlog);
+	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(taprio_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 +684,21 @@ 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 the skb or evict
+	 * another packet, and a netem duplicate re-enters this qdisc.
+	 */
+	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,29 +715,31 @@ 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
 		 */
 		if (taprio_skb_exceeds_queue_max_sdu(sch, segs))
-			ret = qdisc_drop(segs, sch, to_free);
+			ret = qdisc_drop_reason(segs, sch, to_free,
+						QDISC_DROP_GENERIC);
 		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 {
+		/* SDU drops count taprio's; child drops count the child's. */
+		if (ret == NET_XMIT_SUCCESS) {
+			slen += seg_len;
 			numsegs++;
 		}
 	}
 
 	if (numsegs > 1)
-		qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
+		qdisc_tree_reduce_backlog(sch, 1 - numsegs,
+					  (s64)len - (s64)slen);
 	consume_skb(skb);
 
 	return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
@@ -743,8 +840,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 +851,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 +879,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;
 }
@@ -2286,6 +2397,8 @@ static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
 		if (i < dev->real_num_tx_queues)
 			qdisc_hash_add(qdisc, false);
 
+		/* Same contract as a grafted child (see taprio_graft()). */
+		qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
 		q->qdiscs[i] = qdisc;
 	}
 
@@ -2382,6 +2495,11 @@ static int taprio_graft(struct Qdisc *sch, unsigned long cl,
 	if (new != &noop_qdisc)
 		new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
 
+	/* A replaced child can park or free packets the mirror counted. */
+	spin_lock_bh(qdisc_lock(sch));
+	taprio_resync_backlog(sch);
+	spin_unlock_bh(qdisc_lock(sch));
+
 	if (dev->flags & IFF_UP)
 		dev_activate(dev);
 
@@ -2562,6 +2680,11 @@ static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
 	struct nlattr *nest, *sched_nest;
 	u32 txtime_delay;
 
+	/* Resync here: tc_fill_qdisc() samples qlen before ->dump_stats(). */
+	spin_lock_bh(qdisc_lock(sch));
+	taprio_resync_backlog(sch);
+	spin_unlock_bh(qdisc_lock(sch));
+
 	mqprio_qopt_reconstruct(dev, &opt);
 
 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
diff --git a/net/sched/sch_tbf.c b/net/sched/sch_tbf.c
index 58c9687efb0f..6dc07cd55b6b 100644
--- a/net/sched/sch_tbf.c
+++ b/net/sched/sch_tbf.c
@@ -221,6 +221,29 @@ 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;
+		u64 reserve;
+
+		skb_list_walk_safe(segs, seg, nskb)
+			slen += seg->len;
+
+		/* The original was not booked on the ancestors yet, and a
+		 * duplicate can book a second copy of the segment sum.
+		 */
+		reserve = 2 * max_t(u64, slen, prev_len);
+		if (unlikely((u64)sch->qstats.backlog + reserve > U32_MAX ||
+			     !qdisc_ancestors_backlog_room(sch, reserve))) {
+			consume_skb(skb);
+			qdisc_qstats_overlimit(sch);
+			skb_list_walk_safe(segs, seg, nskb)
+				tcf_set_qdisc_drop_reason(seg,
+							  QDISC_DROP_OVERLIMIT);
+			return qdisc_drop_all(segs, sch, to_free);
+		}
+	}
+
 	nb = 0;
 	skb_list_walk_safe(segs, segs, nskb) {
 		skb_mark_not_on_list(segs);
@@ -239,7 +262,8 @@ static int tbf_segment(struct sk_buff *skb, struct Qdisc *sch,
 	WRITE_ONCE(sch->q.qlen, sch->q.qlen + nb);
 	qstats_backlog_add(sch, len);
 	if (nb > 0) {
-		qdisc_tree_reduce_backlog(sch, 1 - nb, prev_len - len);
+		qdisc_tree_reduce_backlog(sch, 1 - nb,
+					  (s64)prev_len - (s64)len);
 		consume_skb(skb);
 		return NET_XMIT_SUCCESS;
 	}
@@ -255,6 +279,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
Technical Details
Message-ID: <QDISC-BA27.v16.r0.20261205113823-3@mojatatu.com>
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-378058-linux-kernel-netdev=dewy.ru
References: <QDISC-BA27.v16.r0.20261205113823@mojatatu.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-378058-linux-kernel-netdev=dewy.ru
From: jhs@mojatatu.com (Jamal Hadi Salim)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next v16 3/4] net/sched: cap the accounted backlog before it can wrap
Date: Sat,  5 Dec 2026 12:20:18 -0500
Organization: Usenet Network
Message-ID: <QDISC-BA27.v16.r0.20261205113823-3@mojatatu.com>
References: <QDISC-BA27.v16.r0.20261205113823@mojatatu.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3077640"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:cf+gwUgFjK8TLnIHS1PU6K6dy6Y= sha256:k7D8uFL/PZS986ij6DNZdS0uoL1TqVkaq8ST+pyQue0=
	sha1:lFk2Ev2k0ibKqAIHVg+BqWu5FKA= sha256:IcXoypS2bseZF4pkmcCHBKCZTmW1HYxc1jLAMC/DKJo=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 28676117E6B; Sat,  5 Dec 2026 20:21:51 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.298
X-Spam-Status: No, score=-3.298 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.001,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key)
	header.d=mojatatu.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id yVYbjqBL5kUv;
	Sat,  5 Dec 2026 20:21:46 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-378058-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=mojatatu.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 846C0117E39; Sat,  5 Dec 2026 20:21:37 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 9A6C42ED07; Sat,  5 Dec 2026 17:21:34 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796491294;
	b=SeVSQaXCdOf5125+vIR4dFXOwYc79ndjM/j+0kNyiti/p4VrVvfRFmed0vr+Xtcw7d8w
	 l/jdkCFKogjjnxbrDRNhSVKwLK8orcdnTAbTOrn81/Os5AMxhXO4xwDgsxn8EiHOcvNN2
	 3eNsX2QgT86Oc0ZZ+o5tnbGNu1EdkSuElBCfODic0pEdMlXcXMpK+Xo44RXsGeAx5bTZE
	 sa1ffML50g4KV2/WiM4xfHZv5QS/X5tTTAtiVP0YvyukzosyK7GXkNlqrSCNT2JLDtKrp
	 5P1C7eNf7ZUI3OHZJymG4uBIF5ednGxTjndF6gMI4nqCepF/sZh9xuJL0i4w30V5djQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796491294;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
	 X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
	 Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:
	 References:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=5wJKv2Hix0YjYM/u74pj2Drv19bmwSoOs5gwQQoaAfg=;
	b=guxe7HVKy9d+u04W6Xb1419HQ0z3hPJgidJjhqFxJDHgwL+5gbRDaDez2fSQaaExev5g
	 lTsC+tJ7lrHVPPWoB7jAOQTiteLpo+Avla5jPpv9ud+Bq8h8Fqr51v0LiNrmwicdxKLC/
	 SPxOJoKZ5Epw8jBLS0sVJPEBFI6meMG3dOGM4lTqWolH0KX/iMUnwleW/r03cjwmQSzWu
	 56r7yqp70g3NXJ96fKBX30F3UcnyQ2QrSQD2SIRWaztDAVDCl2/HMPQ1EPgdfpsQSiuDJ
	 msGhysgfOAdB/fTikjE8ChDe1AUPWoA85LzWQs0UU30Mj3/fq+nA453irbOd8NZE0Vg==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=mojatatu.com header.i=@mojatatu.com header.a=rsa-sha256 header.s=google header.b=jS2fv7ye;
	dmarc=none header.from=mojatatu.com;
	spf=none smtp.mailfrom=mojatatu.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 25570606A334;
	Sat,  5 Dec 2026 17:21:34 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=mojatatu.com header.i=@mojatatu.com header.a=rsa-sha256 header.s=google header.b=jS2fv7ye
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.170
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796491293;
	b=ZUXTd3rv4yl7E1Jui4jjN7yTWniepEdN1BPPZRMe+SSTOjIijBnH72GUCZTes3FSI18C
	 kh2EtcdG3Pw6NOYQl8pZvSavJbu41gAsVgZ0tlr5QAQFK+z+/aVaLbPd+IJNMj/Z9S+u8
	 IO9qdC7EN/d8p76hNk6y53YuyMGZKusxbxr9+Cftm9R12K4vO1fqSER+1m2tRkDcK19Tw
	 RfOjJuaojeyVmFPyJ5rjMIg/VH50AeIgRr8S/JAqRYjJRwCPR+cyNIn/XUGkuG3Avqqov
	 L+gFkuRGI6KowdodL8h+kdAjcOObwknTItehq1LFp03A61uHbrOTdJg/4Mk4kWpU3qg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796491293;
	h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
	 X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:
	 Content-Transfer-Encoding;
	bh=5wJKv2Hix0YjYM/u74pj2Drv19bmwSoOs5gwQQoaAfg=;
	b=jNRHlmQHELmustZqrjcaJmuDMBKEOMApLgQJbAceHVXHJ48ctkpbBAj4UB5VL4+Y+nRY
	 h0PvjQMHDgfctLc6U2V+TcQnjc6vbSh0lsEYkrkL5vjAb5qlsrRYQXZr4g8CU2Ljw7zXO
	 TAswUd0fjf/Kj7+lQd6YYEEA60SuyKlv9IjXyTRjfVXBJAtwtB62KFY6gZj6ShJor89kv
	 t6j8tjw20wiF5azEXqQn3YYO3Fqh5oUQilsghzruCjNwNVZsuJNu917RinEZgPpNqSC1e
	 FJ6i7atcTV4h9eFtN4BZ8tGs4ULMMPpfC2Bi9yV04xSkc7gwm5Dg8Z0s3J40WnclPlg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=none header.from=mojatatu.com;
	spf=none smtp.mailfrom=mojatatu.com;
	dkim=pass header.d=mojatatu.com header.i=@mojatatu.com header.a=rsa-sha256 header.s=google header.b=jS2fv7ye;
	arc=none smtp.remote-ip=209.85.221.170
X-Received: from mail-vk1-f170.google.com (mail-vk1-f170.google.com [209.85.221.170])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0E5C6606A31F
	for <netdev@vger.kernel.org>; Sat,  5 Dec 2026 17:21:31 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0E5C6606A31F
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mojatatu.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=mojatatu.com
X-Received: by mail-vk1-f170.google.com with SMTP id 71dfb90a1353d-5f028e58d8cso162763e0c.2
        for <netdev@vger.kernel.org>; Sat, 05 Dec 2026 09:21:31 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=mojatatu.com; s=google; t=1796491291; x=1797096091; darn=vger.kernel.org;
        h=content-transfer-encoding:mime-version:references:in-reply-to
         :message-id:date:subject:cc:to:from:from:to:cc:subject:date
         :message-id:reply-to:content-type;
        bh=5wJKv2Hix0YjYM/u74pj2Drv19bmwSoOs5gwQQoaAfg=;
        b=jS2fv7yeiX+GagNwM6I0EOzXZSDgHAm4iOW7PK90Q9b1kVdQfNFtVy4t79HclQy9nJ
         Kg6NYsRLln4eCSLM21MF1Or5Xj1Nqh5Ca3EOvIFYZtHaGToLtaWBl4udqaqYqd2wrWUz
         DCxWlA+lXtKMd6gxXqMT4su+3eReAO9U0zMBQ=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1796491291; x=1797096091;
        h=content-transfer-encoding:mime-version:references:in-reply-to
         :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from
         :to:cc:subject:date:message-id:reply-to:content-type;
        bh=5wJKv2Hix0YjYM/u74pj2Drv19bmwSoOs5gwQQoaAfg=;
        b=tYz5gRUwNgcYjjL/xEUq2KdNGaAkjAhsXEk4QCHBfR1Qhxsyum1+Yd+hVTvm1P+WjE
         7dYr9nALab+SxtnPEOZefOkMJ8DEMGf2S/gvUtu3xM5zBiA8/GKaLkV2Qt9XmKzeT1vC
         8yVGyd3pesCFeIefjOlGGoSl9L9nfILB+cyuISarKfNJ5PJNcaPbzkOxP8EvwUqMRo7t
         g9cIHwIqh9DjPK02D4hEbgJ6JNjSKXdNsMYUGlN75WqiL8ZiS57lvCYRzFmVsqV09hvF
         nVXfK3YoPdJDVkkBKAPDKarBk5asHzDVu9Tyg69Wm35PCJBl2aIxGBsjc0BSFiqcYuKP
         hlzA==
X-Gm-Message-State: AB4q+uHzRUVCswGRP82ASZ6l106hRuXMFaRg6fVUAfF7G5zqI/QC1nGb
	iMocbIlxB3ZafGZ/DstqbL3XrGyFuzZ4znGYB6khzrspiXoefczAxCPDmDr27R0Ut6FUDLrA2DD
	AlQYWLYuA
X-Gm-Gg: AZFdTZcYZEDGe6cV7WGg0FnEbUz7rnO8v7qwfiIkfBEjJ36MQaA8Pgap33YLdcZ4NHA
	inCrgY5Mdg8jDhglVbSzb9e1nm3u2Ti7DKqmgKQ3Nt5Mm7hB0qAhFNvfID5GcBVOpbT5YCVRdBH
	KWWICQK6hOTT1S1fxBjb5jQ9+j+/a52F2m/RiWehutyrQ7hMbYe98MRLTLcFHuxZDWB+7pw+sy9
	PN1xMYdqmLUIw1+FtchhC5V4Gd1dkn2JLtQSlN9/k+rjFgRlSbZ8+Ir7rl/8OjITGniVcJABMUS
	9GFcuD+GpEnDjWkVY6G78OhkTJLQyO7HN5iOSJSIaB6ItXazuceH/PBhUH5bW4slOB0SZ6tuD7j
	OS+W+je1YDAlUjxbqF3Qyt/0AXNNPKXV+9vEkDsT8nmIMoN86kGF6pHRA18SVadS4oPHaniQcW4
	hiu188jatopm5RTWo9a5vwEwY5ZIxE+Te8EEUXgq/ZQDUfhVUM4VA4q+lzaS1ohx/5bdcuupsyV
	7mL/WLb6wSo2fsEssChUFMhUUWO++teasr3sJw1wSkbnTtslaFbsCkktL981ex2K9ajzcRwUGB4
	3vPKjBiMR2IDmXDjj8FrOqq4hG7Os2vS6l70ta+H2QgnBY+QJU8E1cYzs3EZnGoQdXbeD7lmA6e
	Uom9TyWmkd+LXt5cvxT6fs9py1juxpnNUfdOfb0dClqF9HHa9N2YtI47vLQUDiY5QfyFDtVtJ49
	lUqqn+6s+JgqxG2VR28w==
X-Received: by 2002:a05:6122:1d51:b0:5ea:e7aa:acb9 with SMTP id 71dfb90a1353d-5f01cf92614mr4934988e0c.6.1796491290381;
        Sat, 05 Dec 2026 09:21:30 -0800 (PST)
X-Received: from tg.tail33bf8.ts.net ([174.94.23.9])
        by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-5f01d3ecd41sm3852997e0c.12.2026.12.05.09.21.27
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 05 Dec 2026 09:21:29 -0800 (PST)
X-Cc: Jamal Hadi Salim <jhs@mojatatu.com>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>,
	Jiri Pirko <jiri@resnulli.us>,
	Stephen Hemminger <stephen@networkplumber.org>,
	Vinicius Costa Gomes <vinicius.gomes@intel.com>,
	Toke Høiland-Jørgensen <toke@toke.dk>,
	cake@lists.bufferbloat.net,
	Jussi Kivilinna <jussi.kivilinna@mbnet.fi>,
	Victor Nogueira <victor@mojatatu.com>,
	Björn Töpel <bjorn@kernel.org>,
	Sashiko <sashiko-bot@kernel.org>
X-Mailer: git-send-email 2.43.0
X-In-Reply-To: <QDISC-BA27.v16.r0.20261205113823@mojatatu.com>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list