Skip to contentSkip to conversation

net/sched: revalidate the sleeping-root lock in timers

PATCH In review Kyle Zeng 3 participants 3 messages Oct 30 – Nov 9

Subsystems (3)
lore
Share & tools
lore

Kyle Zeng

author
#0 lore raw

PATCH 1/1 net/sched: revalidate the sleeping-root lock in timers

Autonomous qdisc timers select their lock through qdisc_sleeping. dev_graft_qdisc() changes that pointer while holding the previous root's lock, but a timer may have read the old pointer before the graft and only acquire its lock afterwards. RCU keeps the old qdisc alive; it does not make that lock protect the replacement hierarchy.

For example, a backlogged MQ child retained by an outstanding filter request can be detached and regrafted at the same parent. An SFQ timer that selected the interim root can then rehash its queues under that obsolete lock while tbf_graft() resets the SFQ under the current root's lock. This corrupts skb lists and SFQ slot state.

Add an RCU-side helper which acquires the sleeping-root lock, checks that the root is still current, and retries if it changed. Once the check succeeds, dev_graft_qdisc() cannot replace the root until the callback releases its lock. Use it in the SFQ, RED, PIE, FQ-PIE and DualPI2 timers.

Route default-qdisc attachment and scheduler shutdown through dev_graft_qdisc() as well, so live sleeping-root publications follow the same locking protocol.

---
I have a reproducer for this issue and can share via DM

 include/net/sch_generic.h | 20 ++++++++++++++++++++
 net/sched/sch_dualpi2.c   |  3 +--
 net/sched/sch_fq_pie.c    |  3 +--
 net/sched/sch_generic.c   |  5 +++--
 net/sched/sch_pie.c       |  3 +--
 net/sched/sch_red.c       |  3 +--
 net/sched/sch_sfq.c       |  3 +--
 7 files changed, 28 insertions(+), 12 deletions(-)
include/net/sch_generic.h +20 -0
index 1acaadb3e2cf..e234256e6ae8 100644
@@ -608,6 +608,26 @@ static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
 	return qdisc_lock(root);
 }
 
+/* The caller must hold rcu_read_lock().  dev_graft_qdisc() changes the
+ * sleeping root under the old root's lock, so retry if it changed while
+ * we were waiting for that lock.
+ */
+static inline spinlock_t *qdisc_lock_root_sleeping(const struct Qdisc *qdisc)
+{
+	struct Qdisc *root;
+	/* Lock of the sleeping root observed under RCU. */
+	spinlock_t *lock;
+
+	for (;;) {
+		root = qdisc_root_sleeping(qdisc);
+		lock = qdisc_lock(root);
+		spin_lock(lock);
+		if (root == qdisc_root_sleeping(qdisc))
+			return lock;
+		spin_unlock(lock);
+	}
+}
+
 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
 {
 	return qdisc->dev_queue->dev;
net/sched/sch_dualpi2.c +1 -2
index 4947def7c49e..8a74ceaab29d 100644
@@ -750,8 +750,7 @@ static enum hrtimer_restart dualpi2_timer(struct hrtimer *timer)
 	spinlock_t *root_lock; /* to lock qdisc for probability calculations */
 
 	rcu_read_lock();
-	root_lock = qdisc_lock(qdisc_root_sleeping(sch));
-	spin_lock(root_lock);
+	root_lock = qdisc_lock_root_sleeping(sch);
 
 	WRITE_ONCE(q->pi2_prob, calculate_probability(sch));
 	hrtimer_set_expires(&q->pi2_timer, next_pi2_timeout(q));
net/sched/sch_fq_pie.c +1 -2
index 5982847df8f8..a1b5126db640 100644
@@ -394,8 +394,7 @@ static void fq_pie_timer(struct timer_list *t)
 	int max_cnt, i;
 
 	rcu_read_lock();
-	root_lock = qdisc_lock(qdisc_root_sleeping(sch));
-	spin_lock(root_lock);
+	root_lock = qdisc_lock_root_sleeping(sch);
 
 	/* Limit this expensive loop to 2048 flows per round. */
 	max_cnt = min_t(int, q->flows_cnt - q->flows_cursor, 2048);
net/sched/sch_generic.c +3 -2
index 3e8ee4a691c3..f4ec712be18b 100644
@@ -1182,6 +1182,7 @@ struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
 	struct Qdisc *oqdisc = rtnl_dereference(dev_queue->qdisc_sleeping);
 	spinlock_t *root_lock;
 
+	/* Pairs with qdisc_lock_root_sleeping() in asynchronous callbacks. */
 	root_lock = qdisc_lock(oqdisc);
 	spin_lock_bh(root_lock);
 
@@ -1205,8 +1206,8 @@ static void shutdown_scheduler_queue(struct net_device *dev,
 	struct Qdisc *qdisc_default = _qdisc_default;
 
 	if (qdisc) {
+		qdisc = dev_graft_qdisc(dev_queue, qdisc_default);
 		rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
-		rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc_default);
 
 		qdisc_put(qdisc);
 	}
@@ -1230,7 +1231,7 @@ static void attach_one_default_qdisc(struct net_device *dev,
 
 	if (!netif_is_multiqueue(dev))
 		qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
-	rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc);
+	dev_graft_qdisc(dev_queue, qdisc);
 }
 
 static void attach_default_qdiscs(struct net_device *dev)
net/sched/sch_pie.c +1 -2
index 3b7863ffd284..9367065db4b1 100644
@@ -429,8 +429,7 @@ static void pie_timer(struct timer_list *t)
 	spinlock_t *root_lock;
 
 	rcu_read_lock();
-	root_lock = qdisc_lock(qdisc_root_sleeping(sch));
-	spin_lock(root_lock);
+	root_lock = qdisc_lock_root_sleeping(sch);
 	pie_calculate_probability(&q->params, &q->vars, sch->qstats.backlog);
 
 	/* reset the timer to fire after 'tupdate'. tupdate is in jiffies. */
net/sched/sch_red.c +1 -2
index d7598214270b..a7601b879092 100644
@@ -333,8 +333,7 @@ static inline void red_adaptative_timer(struct timer_list *t)
 	spinlock_t *root_lock;
 
 	rcu_read_lock();
-	root_lock = qdisc_lock(qdisc_root_sleeping(sch));
-	spin_lock(root_lock);
+	root_lock = qdisc_lock_root_sleeping(sch);
 	red_adaptative_algo(&q->parms, &q->vars);
 	mod_timer(&q->adapt_timer, jiffies + HZ/2);
 	spin_unlock(root_lock);
net/sched/sch_sfq.c +1 -2
index 8bbcfc9e85d9..2bd3fc65053a 100644
@@ -611,8 +611,7 @@ static void sfq_perturbation(struct timer_list *t)
 
 	get_random_bytes(&nkey, sizeof(nkey));
 	rcu_read_lock();
-	root_lock = qdisc_lock(qdisc_root_sleeping(sch));
-	spin_lock(root_lock);
+	root_lock = qdisc_lock_root_sleeping(sch);
 	q->perturbation = nkey;
 	if (!q->filter_list && q->tail)
 		sfq_rehash(sch);

base-commit: d89e1d186ec9271bf78e62771779e9fc03bf0fc3

Signature · 1 line
2.56.0
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.

The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.

maintainer
#2 lore raw
In reply to Kyle Zeng
Quoted 18 lines · On Thu, 29 Oct 2026 21:23:12 -0700 Kyle Zeng wrote:

Autonomous qdisc timers select their lock through qdisc_sleeping. dev_graft_qdisc() changes that pointer while holding the previous root's lock, but a timer may have read the old pointer before the graft and only acquire its lock afterwards. RCU keeps the old qdisc alive; it does not make that lock protect the replacement hierarchy.

For example, a backlogged MQ child retained by an outstanding filter request can be detached and regrafted at the same parent. An SFQ timer that selected the interim root can then rehash its queues under that obsolete lock while tbf_graft() resets the SFQ under the current root's lock. This corrupts skb lists and SFQ slot state.

Add an RCU-side helper which acquires the sleeping-root lock, checks that the root is still current, and retries if it changed. Once the check succeeds, dev_graft_qdisc() cannot replace the root until the callback releases its lock. Use it in the SFQ, RED, PIE, FQ-PIE and DualPI2 timers.

Quoted 3 lines

Route default-qdisc attachment and scheduler shutdown through dev_graft_qdisc() as well, so live sleeping-root publications follow the same locking protocol.

Clashiko suggests the fix is incomplete:
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/[email protected]

Signature · 1 line
pw-bot: cr