Skip to contentSkip to conversation

net_sched: no longer use qdisc_tree_flush_backlog()

PATCH Applied Eric Dumazet 2 participants 7 messages Jun 11, 2025 – Jun 12, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover net_sched: no longer use qdisc_tree_flush_backlog()

This series is based on a report from Gerrard Tai.

Essentially, all users of qdisc_tree_flush_backlog() are racy.

We must instead use qdisc_purge_queue().

Eric Dumazet (5):
net_sched: prio: fix a race in prio_tune()
net_sched: red: fix a race in __red_change()
net_sched: tbf: fix a race in tbf_change()
net_sched: ets: fix a race in ets_qdisc_change()
net_sched: remove qdisc_tree_flush_backlog()

 include/net/sch_generic.h | 8 --------
 net/sched/sch_ets.c       | 2 +-
 net/sched/sch_prio.c      | 2 +-
 net/sched/sch_red.c       | 2 +-
 net/sched/sch_tbf.c       | 2 +-
 5 files changed, 4 insertions(+), 12 deletions(-)
Signature · 1 line
2.50.0.rc0.642.g800a2b2222-goog
#1 lore raw

PATCH 1/5 net_sched: prio: fix a race in prio_tune()

Gerrard Tai reported a race condition in PRIO, whenever SFQ perturb timer fires at the wrong time.

The race is as follows:

CPU 0 CPU 1
[1]: lock root
[2]: qdisc_tree_flush_backlog()
[3]: unlock root

 |
 |                                    [5]: lock root
 |                                    [6]: rehash
 |                                    [7]: qdisc_tree_reduce_backlog()
 |

[4]: qdisc_put()

This can be abused to underflow a parent's qlen.

Calling qdisc_purge_queue() instead of qdisc_tree_flush_backlog() should fix the race, because all packets will be purged from the qdisc before releasing the lock.

Fixes 7b8e0b6e6599 ("net: sched: prio: delay destroying child qdiscs on change")
---
 net/sched/sch_prio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/sch_prio.c +1 -1
index cc30f7a32f1a786fa1c3937a1b9d5c96a52e56e7..9e2b9a490db23d858b27b7fc073b05a06535b05e 100644
@@ -211,7 +211,7 @@ static int prio_tune(struct Qdisc *sch, struct nlattr *opt,
 	memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
 
 	for (i = q->bands; i < oldbands; i++)
-		qdisc_tree_flush_backlog(q->queues[i]);
+		qdisc_purge_queue(q->queues[i]);
 
 	for (i = oldbands; i < q->bands; i++) {
 		q->queues[i] = queues[i];
Signature · 1 line
2.50.0.rc0.642.g800a2b2222-goog
#2 lore raw

PATCH 2/5 net_sched: red: fix a race in __red_change()

In reply to Eric Dumazet

Gerrard Tai reported a race condition in RED, whenever SFQ perturb timer fires at the wrong time.

The race is as follows:

CPU 0 CPU 1
[1]: lock root
[2]: qdisc_tree_flush_backlog()
[3]: unlock root

 |
 |                                    [5]: lock root
 |                                    [6]: rehash
 |                                    [7]: qdisc_tree_reduce_backlog()
 |

[4]: qdisc_put()

This can be abused to underflow a parent's qlen.

Calling qdisc_purge_queue() instead of qdisc_tree_flush_backlog() should fix the race, because all packets will be purged from the qdisc before releasing the lock.

Fixes 0c8d13ac9607 ("net: sched: red: delay destroying child qdisc on replace")
---
 net/sched/sch_red.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/sch_red.c +1 -1
index 1ba3e0bba54f0cc8d797ab9a7ffab529481b2d4e..4696c893cf553c4c6ecd2da75b47b3d40c80e812 100644
@@ -285,7 +285,7 @@ static int __red_change(struct Qdisc *sch, struct nlattr **tb,
 	q->userbits = userbits;
 	q->limit = ctl->limit;
 	if (child) {
-		qdisc_tree_flush_backlog(q->qdisc);
+		qdisc_purge_queue(q->qdisc);
 		old_child = q->qdisc;
 		q->qdisc = child;
 	}
Signature · 1 line
2.50.0.rc0.642.g800a2b2222-goog
#3 lore raw

PATCH 3/5 net_sched: tbf: fix a race in tbf_change()

In reply to Eric Dumazet

Gerrard Tai reported a race condition in TBF, whenever SFQ perturb timer fires at the wrong time.

The race is as follows:

CPU 0 CPU 1
[1]: lock root
[2]: qdisc_tree_flush_backlog()
[3]: unlock root

 |
 |                                    [5]: lock root
 |                                    [6]: rehash
 |                                    [7]: qdisc_tree_reduce_backlog()
 |

[4]: qdisc_put()

This can be abused to underflow a parent's qlen.

Calling qdisc_purge_queue() instead of qdisc_tree_flush_backlog() should fix the race, because all packets will be purged from the qdisc before releasing the lock.

Fixes b05972f01e7d ("net: sched: tbf: don't call qdisc_put() while holding tree lock")
Cc Zhengchao Shao <[email protected]>
---
 net/sched/sch_tbf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/sch_tbf.c +1 -1
index dc26b22d53c73461c6f6d64f768136ad3f334143..4c977f049670a600eafd219c898e5f29597be2c1 100644
@@ -452,7 +452,7 @@ static int tbf_change(struct Qdisc *sch, struct nlattr *opt,
 
 	sch_tree_lock(sch);
 	if (child) {
-		qdisc_tree_flush_backlog(q->qdisc);
+		qdisc_purge_queue(q->qdisc);
 		old = q->qdisc;
 		q->qdisc = child;
 	}
Signature · 1 line
2.50.0.rc0.642.g800a2b2222-goog
#4 lore raw

PATCH 4/5 net_sched: ets: fix a race in ets_qdisc_change()

In reply to Eric Dumazet

Gerrard Tai reported a race condition in ETS, whenever SFQ perturb timer fires at the wrong time.

The race is as follows:

CPU 0 CPU 1
[1]: lock root
[2]: qdisc_tree_flush_backlog()
[3]: unlock root

 |
 |                                    [5]: lock root
 |                                    [6]: rehash
 |                                    [7]: qdisc_tree_reduce_backlog()
 |

[4]: qdisc_put()

This can be abused to underflow a parent's qlen.

Calling qdisc_purge_queue() instead of qdisc_tree_flush_backlog() should fix the race, because all packets will be purged from the qdisc before releasing the lock.

Fixes b05972f01e7d ("net: sched: tbf: don't call qdisc_put() while holding tree lock")
---
 net/sched/sch_ets.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/sch_ets.c +1 -1
index 2c069f0181c62b5387118822dfb7a51fc1b3033a..037f764822b96526329a4715bfcc22c502495f14 100644
@@ -661,7 +661,7 @@ static int ets_qdisc_change(struct Qdisc *sch, struct nlattr *opt,
 	for (i = q->nbands; i < oldbands; i++) {
 		if (i >= q->nstrict && q->classes[i].qdisc->q.qlen)
 			list_del_init(&q->classes[i].alist);
-		qdisc_tree_flush_backlog(q->classes[i].qdisc);
+		qdisc_purge_queue(q->classes[i].qdisc);
 	}
 	WRITE_ONCE(q->nstrict, nstrict);
 	memcpy(q->prio2band, priomap, sizeof(priomap));
Signature · 1 line
2.50.0.rc0.642.g800a2b2222-goog
#5 lore raw

PATCH 5/5 net_sched: remove qdisc_tree_flush_backlog()

In reply to Eric Dumazet

This function is no longer used after the four prior fixes.

Given all prior uses were wrong, it seems better to remove it.

---
 include/net/sch_generic.h | 8 --------
 1 file changed, 8 deletions(-)
include/net/sch_generic.h +0 -8
index 629368ab2787f9ddeff8dbb0a825d9d30e5b0f0c..638948be4c50e2e6f5899ded17943de55d4f94f5 100644
@@ -973,14 +973,6 @@ static inline void qdisc_qstats_qlen_backlog(struct Qdisc *sch,  __u32 *qlen,
 	*backlog = qstats.backlog;
 }
 
-static inline void qdisc_tree_flush_backlog(struct Qdisc *sch)
-{
-	__u32 qlen, backlog;
-
-	qdisc_qstats_qlen_backlog(sch, &qlen, &backlog);
-	qdisc_tree_reduce_backlog(sch, qlen, backlog);
-}
-
 static inline void qdisc_purge_queue(struct Qdisc *sch)
 {
 	__u32 qlen, backlog;
Signature · 1 line
2.50.0.rc0.642.g800a2b2222-goog
#6 lore raw
In reply to Eric Dumazet
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 6 lines · On Wed, 11 Jun 2025 11:15:10 +0000 you wrote:

This series is based on a report from Gerrard Tai.

Essentially, all users of qdisc_tree_flush_backlog() are racy.

We must instead use qdisc_purge_queue().

Quoted 8 lines

Eric Dumazet (5):
net_sched: prio: fix a race in prio_tune()
net_sched: red: fix a race in __red_change()
net_sched: tbf: fix a race in tbf_change()
net_sched: ets: fix a race in ets_qdisc_change()
net_sched: remove qdisc_tree_flush_backlog()

[...]

Here is the summary with links:
- [net,1/5] net_sched: prio: fix a race in prio_tune()

    https://git.kernel.org/netdev/net/c/d35acc1be348
  - [net,2/5] net_sched: red: fix a race in __red_change()
    https://git.kernel.org/netdev/net/c/85a3e0ede384
  - [net,3/5] net_sched: tbf: fix a race in tbf_change()
    https://git.kernel.org/netdev/net/c/43eb46604121
  - [net,4/5] net_sched: ets: fix a race in ets_qdisc_change()
    https://git.kernel.org/netdev/net/c/d92adacdd8c2
  - [net,5/5] net_sched: remove qdisc_tree_flush_backlog()
    https://git.kernel.org/netdev/net/c/adcaa890c7a4

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html