Skip to content

Commit 2f23d5b

Browse files
Yunsheng Lingregkh
authored andcommitted
net: sched: fix tx action rescheduling issue during deactivation
[ Upstream commit 102b55e ] Currently qdisc_run() checks the STATE_DEACTIVATED of lockless qdisc before calling __qdisc_run(), which ultimately clear the STATE_MISSED when all the skb is dequeued. If STATE_DEACTIVATED is set before clearing STATE_MISSED, there may be rescheduling of net_tx_action() at the end of qdisc_run_end(), see below: CPU0(net_tx_atcion) CPU1(__dev_xmit_skb) CPU2(dev_deactivate) . . . . set STATE_MISSED . . __netif_schedule() . . . set STATE_DEACTIVATED . . qdisc_reset() . . . .<--------------- . synchronize_net() clear __QDISC_STATE_SCHED | . . . | . . . | . some_qdisc_is_busy() . | . return *false* . | . . test STATE_DEACTIVATED | . . __qdisc_run() *not* called | . . . | . . test STATE_MISS | . . __netif_schedule()--------| . . . . . . . . __qdisc_run() is not called by net_tx_atcion() in CPU0 because CPU2 has set STATE_DEACTIVATED flag during dev_deactivate(), and STATE_MISSED is only cleared in __qdisc_run(), __netif_schedule is called at the end of qdisc_run_end(), causing tx action rescheduling problem. qdisc_run() called by net_tx_action() runs in the softirq context, which should has the same semantic as the qdisc_run() called by __dev_xmit_skb() protected by rcu_read_lock_bh(). And there is a synchronize_net() between STATE_DEACTIVATED flag being set and qdisc_reset()/some_qdisc_is_busy in dev_deactivate(), we can safely bail out for the deactived lockless qdisc in net_tx_action(), and qdisc_reset() will reset all skb not dequeued yet. So add the rcu_read_lock() explicitly to protect the qdisc_run() and do the STATE_DEACTIVATED checking in net_tx_action() before calling qdisc_run_begin(). Another option is to do the checking in the qdisc_run_end(), but it will add unnecessary overhead for non-tx_action case, because __dev_queue_xmit() will not see qdisc with STATE_DEACTIVATED after synchronize_net(), the qdisc with STATE_DEACTIVATED can only be seen by net_tx_action() because of __netif_schedule(). The STATE_DEACTIVATED checking in qdisc_run() is to avoid race between net_tx_action() and qdisc_reset(), see: commit d518d2e ("net/sched: fix race between deactivation and dequeue for NOLOCK qdisc"). As the bailout added above for deactived lockless qdisc in net_tx_action() provides better protection for the race without calling qdisc_run() at all, so remove the STATE_DEACTIVATED checking in qdisc_run(). After qdisc_reset(), there is no skb in qdisc to be dequeued, so clear the STATE_MISSED in dev_reset_queue() too. Fixes: 6b3ba91 ("net: sched: allow qdiscs to handle locking") Acked-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Yunsheng Lin <linyunsheng@huawei.com> V8: Clearing STATE_MISSED before calling __netif_schedule() has avoid the endless rescheduling problem, but there may still be a unnecessary rescheduling, so adjust the commit log. Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 21c7151 commit 2f23d5b

3 files changed

Lines changed: 26 additions & 11 deletions

File tree

‎include/net/pkt_sched.h‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,7 @@ void __qdisc_run(struct Qdisc *q);
123123
static inline void qdisc_run(struct Qdisc *q)
124124
{
125125
if (qdisc_run_begin(q)) {
126-
/* NOLOCK qdisc must check 'state' under the qdisc seqlock
127-
* to avoid racing with dev_qdisc_reset()
128-
*/
129-
if (!(q->flags & TCQ_F_NOLOCK) ||
130-
likely(!test_bit(__QDISC_STATE_DEACTIVATED, &q->state)))
131-
__qdisc_run(q);
126+
__qdisc_run(q);
132127
qdisc_run_end(q);
133128
}
134129
}

‎net/core/dev.c‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4910,25 +4910,43 @@ static __latent_entropy void net_tx_action(struct softirq_action *h)
49104910
sd->output_queue_tailp = &sd->output_queue;
49114911
local_irq_enable();
49124912

4913+
rcu_read_lock();
4914+
49134915
while (head) {
49144916
struct Qdisc *q = head;
49154917
spinlock_t *root_lock = NULL;
49164918

49174919
head = head->next_sched;
49184920

4919-
if (!(q->flags & TCQ_F_NOLOCK)) {
4920-
root_lock = qdisc_lock(q);
4921-
spin_lock(root_lock);
4922-
}
49234921
/* We need to make sure head->next_sched is read
49244922
* before clearing __QDISC_STATE_SCHED
49254923
*/
49264924
smp_mb__before_atomic();
4925+
4926+
if (!(q->flags & TCQ_F_NOLOCK)) {
4927+
root_lock = qdisc_lock(q);
4928+
spin_lock(root_lock);
4929+
} else if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED,
4930+
&q->state))) {
4931+
/* There is a synchronize_net() between
4932+
* STATE_DEACTIVATED flag being set and
4933+
* qdisc_reset()/some_qdisc_is_busy() in
4934+
* dev_deactivate(), so we can safely bail out
4935+
* early here to avoid data race between
4936+
* qdisc_deactivate() and some_qdisc_is_busy()
4937+
* for lockless qdisc.
4938+
*/
4939+
clear_bit(__QDISC_STATE_SCHED, &q->state);
4940+
continue;
4941+
}
4942+
49274943
clear_bit(__QDISC_STATE_SCHED, &q->state);
49284944
qdisc_run(q);
49294945
if (root_lock)
49304946
spin_unlock(root_lock);
49314947
}
4948+
4949+
rcu_read_unlock();
49324950
}
49334951

49344952
xfrm_dev_backlog(sd);

‎net/sched/sch_generic.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,8 +1177,10 @@ static void dev_reset_queue(struct net_device *dev,
11771177
qdisc_reset(qdisc);
11781178

11791179
spin_unlock_bh(qdisc_lock(qdisc));
1180-
if (nolock)
1180+
if (nolock) {
1181+
clear_bit(__QDISC_STATE_MISSED, &qdisc->state);
11811182
spin_unlock_bh(&qdisc->seqlock);
1183+
}
11821184
}
11831185

11841186
static bool some_qdisc_is_busy(struct net_device *dev)

0 commit comments

Comments
 (0)