Skip to content

Commit 52bf272

Browse files
BitsByWillkuba-moo
authored andcommitted
net/sched: Fix backlog accounting in qdisc_dequeue_internal
This issue applies for the following qdiscs: hhf, fq, fq_codel, and fq_pie, and occurs in their change handlers when adjusting to the new limit. The problem is the following in the values passed to the subsequent qdisc_tree_reduce_backlog call given a tbf parent: When the tbf parent runs out of tokens, skbs of these qdiscs will be placed in gso_skb. Their peek handlers are qdisc_peek_dequeued, which accounts for both qlen and backlog. However, in the case of qdisc_dequeue_internal, ONLY qlen is accounted for when pulling from gso_skb. This means that these qdiscs are missing a qdisc_qstats_backlog_dec when dropping packets to satisfy the new limit in their change handlers. One can observe this issue with the following (with tc patched to support a limit of 0): export TARGET=fq tc qdisc del dev lo root tc qdisc add dev lo root handle 1: tbf rate 8bit burst 100b latency 1ms tc qdisc replace dev lo handle 3: parent 1:1 $TARGET limit 1000 echo ''; echo 'add child'; tc -s -d qdisc show dev lo ping -I lo -f -c2 -s32 -W0.001 127.0.0.1 2>&1 >/dev/null echo ''; echo 'after ping'; tc -s -d qdisc show dev lo tc qdisc change dev lo handle 3: parent 1:1 $TARGET limit 0 echo ''; echo 'after limit drop'; tc -s -d qdisc show dev lo tc qdisc replace dev lo handle 2: parent 1:1 sfq echo ''; echo 'post graft'; tc -s -d qdisc show dev lo The second to last show command shows 0 packets but a positive number (74) of backlog bytes. The problem becomes clearer in the last show command, where qdisc_purge_queue triggers qdisc_tree_reduce_backlog with the positive backlog and causes an underflow in the tbf parent's backlog (4096 Mb instead of 0). To fix this issue, the codepath for all clients of qdisc_dequeue_internal has been simplified: codel, pie, hhf, fq, fq_pie, and fq_codel. qdisc_dequeue_internal handles the backlog adjustments for all cases that do not directly use the dequeue handler. The old fq_codel_change limit adjustment loop accumulated the arguments to the subsequent qdisc_tree_reduce_backlog call through the cstats field. However, this is confusing and error prone as fq_codel_dequeue could also potentially mutate this field (which qdisc_dequeue_internal calls in the non gso_skb case), so we have unified the code here with other qdiscs. Fixes: 2d3cbfd ("net_sched: Flush gso_skb list too during ->change()") Fixes: 4b549a2 ("fq_codel: Fair Queue Codel AQM") Fixes: 10239ed ("net-qdisc-hhf: Heavy-Hitter Filter (HHF) qdisc") Signed-off-by: William Liu <[email protected]> Reviewed-by: Savino Dicanosa <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent d1547bf commit 52bf272

File tree

7 files changed

+50
-33
lines changed

7 files changed

+50
-33
lines changed

include/net/sch_generic.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,12 +1038,17 @@ static inline struct sk_buff *qdisc_dequeue_internal(struct Qdisc *sch, bool dir
10381038
skb = __skb_dequeue(&sch->gso_skb);
10391039
if (skb) {
10401040
sch->q.qlen--;
1041+
qdisc_qstats_backlog_dec(sch, skb);
10411042
return skb;
10421043
}
1043-
if (direct)
1044-
return __qdisc_dequeue_head(&sch->q);
1045-
else
1044+
if (direct) {
1045+
skb = __qdisc_dequeue_head(&sch->q);
1046+
if (skb)
1047+
qdisc_qstats_backlog_dec(sch, skb);
1048+
return skb;
1049+
} else {
10461050
return sch->dequeue(sch);
1051+
}
10471052
}
10481053

10491054
static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)

net/sched/sch_codel.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ static const struct nla_policy codel_policy[TCA_CODEL_MAX + 1] = {
101101
static int codel_change(struct Qdisc *sch, struct nlattr *opt,
102102
struct netlink_ext_ack *extack)
103103
{
104+
unsigned int dropped_pkts = 0, dropped_bytes = 0;
104105
struct codel_sched_data *q = qdisc_priv(sch);
105106
struct nlattr *tb[TCA_CODEL_MAX + 1];
106-
unsigned int qlen, dropped = 0;
107107
int err;
108108

109109
err = nla_parse_nested_deprecated(tb, TCA_CODEL_MAX, opt,
@@ -142,15 +142,17 @@ static int codel_change(struct Qdisc *sch, struct nlattr *opt,
142142
WRITE_ONCE(q->params.ecn,
143143
!!nla_get_u32(tb[TCA_CODEL_ECN]));
144144

145-
qlen = sch->q.qlen;
146145
while (sch->q.qlen > sch->limit) {
147146
struct sk_buff *skb = qdisc_dequeue_internal(sch, true);
148147

149-
dropped += qdisc_pkt_len(skb);
150-
qdisc_qstats_backlog_dec(sch, skb);
148+
if (!skb)
149+
break;
150+
151+
dropped_pkts++;
152+
dropped_bytes += qdisc_pkt_len(skb);
151153
rtnl_qdisc_drop(skb, sch);
152154
}
153-
qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped);
155+
qdisc_tree_reduce_backlog(sch, dropped_pkts, dropped_bytes);
154156

155157
sch_tree_unlock(sch);
156158
return 0;

net/sched/sch_fq.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,11 +1013,11 @@ static int fq_load_priomap(struct fq_sched_data *q,
10131013
static int fq_change(struct Qdisc *sch, struct nlattr *opt,
10141014
struct netlink_ext_ack *extack)
10151015
{
1016+
unsigned int dropped_pkts = 0, dropped_bytes = 0;
10161017
struct fq_sched_data *q = qdisc_priv(sch);
10171018
struct nlattr *tb[TCA_FQ_MAX + 1];
1018-
int err, drop_count = 0;
1019-
unsigned drop_len = 0;
10201019
u32 fq_log;
1020+
int err;
10211021

10221022
err = nla_parse_nested_deprecated(tb, TCA_FQ_MAX, opt, fq_policy,
10231023
NULL);
@@ -1135,16 +1135,18 @@ static int fq_change(struct Qdisc *sch, struct nlattr *opt,
11351135
err = fq_resize(sch, fq_log);
11361136
sch_tree_lock(sch);
11371137
}
1138+
11381139
while (sch->q.qlen > sch->limit) {
11391140
struct sk_buff *skb = qdisc_dequeue_internal(sch, false);
11401141

11411142
if (!skb)
11421143
break;
1143-
drop_len += qdisc_pkt_len(skb);
1144+
1145+
dropped_pkts++;
1146+
dropped_bytes += qdisc_pkt_len(skb);
11441147
rtnl_kfree_skbs(skb, skb);
1145-
drop_count++;
11461148
}
1147-
qdisc_tree_reduce_backlog(sch, drop_count, drop_len);
1149+
qdisc_tree_reduce_backlog(sch, dropped_pkts, dropped_bytes);
11481150

11491151
sch_tree_unlock(sch);
11501152
return err;

net/sched/sch_fq_codel.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ static const struct nla_policy fq_codel_policy[TCA_FQ_CODEL_MAX + 1] = {
366366
static int fq_codel_change(struct Qdisc *sch, struct nlattr *opt,
367367
struct netlink_ext_ack *extack)
368368
{
369+
unsigned int dropped_pkts = 0, dropped_bytes = 0;
369370
struct fq_codel_sched_data *q = qdisc_priv(sch);
370371
struct nlattr *tb[TCA_FQ_CODEL_MAX + 1];
371372
u32 quantum = 0;
@@ -443,13 +444,14 @@ static int fq_codel_change(struct Qdisc *sch, struct nlattr *opt,
443444
q->memory_usage > q->memory_limit) {
444445
struct sk_buff *skb = qdisc_dequeue_internal(sch, false);
445446

446-
q->cstats.drop_len += qdisc_pkt_len(skb);
447+
if (!skb)
448+
break;
449+
450+
dropped_pkts++;
451+
dropped_bytes += qdisc_pkt_len(skb);
447452
rtnl_kfree_skbs(skb, skb);
448-
q->cstats.drop_count++;
449453
}
450-
qdisc_tree_reduce_backlog(sch, q->cstats.drop_count, q->cstats.drop_len);
451-
q->cstats.drop_count = 0;
452-
q->cstats.drop_len = 0;
454+
qdisc_tree_reduce_backlog(sch, dropped_pkts, dropped_bytes);
453455

454456
sch_tree_unlock(sch);
455457
return 0;

net/sched/sch_fq_pie.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,9 @@ static struct sk_buff *fq_pie_qdisc_dequeue(struct Qdisc *sch)
287287
static int fq_pie_change(struct Qdisc *sch, struct nlattr *opt,
288288
struct netlink_ext_ack *extack)
289289
{
290+
unsigned int dropped_pkts = 0, dropped_bytes = 0;
290291
struct fq_pie_sched_data *q = qdisc_priv(sch);
291292
struct nlattr *tb[TCA_FQ_PIE_MAX + 1];
292-
unsigned int len_dropped = 0;
293-
unsigned int num_dropped = 0;
294293
int err;
295294

296295
err = nla_parse_nested(tb, TCA_FQ_PIE_MAX, opt, fq_pie_policy, extack);
@@ -368,11 +367,14 @@ static int fq_pie_change(struct Qdisc *sch, struct nlattr *opt,
368367
while (sch->q.qlen > sch->limit) {
369368
struct sk_buff *skb = qdisc_dequeue_internal(sch, false);
370369

371-
len_dropped += qdisc_pkt_len(skb);
372-
num_dropped += 1;
370+
if (!skb)
371+
break;
372+
373+
dropped_pkts++;
374+
dropped_bytes += qdisc_pkt_len(skb);
373375
rtnl_kfree_skbs(skb, skb);
374376
}
375-
qdisc_tree_reduce_backlog(sch, num_dropped, len_dropped);
377+
qdisc_tree_reduce_backlog(sch, dropped_pkts, dropped_bytes);
376378

377379
sch_tree_unlock(sch);
378380
return 0;

net/sched/sch_hhf.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,9 @@ static const struct nla_policy hhf_policy[TCA_HHF_MAX + 1] = {
508508
static int hhf_change(struct Qdisc *sch, struct nlattr *opt,
509509
struct netlink_ext_ack *extack)
510510
{
511+
unsigned int dropped_pkts = 0, dropped_bytes = 0;
511512
struct hhf_sched_data *q = qdisc_priv(sch);
512513
struct nlattr *tb[TCA_HHF_MAX + 1];
513-
unsigned int qlen, prev_backlog;
514514
int err;
515515
u64 non_hh_quantum;
516516
u32 new_quantum = q->quantum;
@@ -561,15 +561,17 @@ static int hhf_change(struct Qdisc *sch, struct nlattr *opt,
561561
usecs_to_jiffies(us));
562562
}
563563

564-
qlen = sch->q.qlen;
565-
prev_backlog = sch->qstats.backlog;
566564
while (sch->q.qlen > sch->limit) {
567565
struct sk_buff *skb = qdisc_dequeue_internal(sch, false);
568566

567+
if (!skb)
568+
break;
569+
570+
dropped_pkts++;
571+
dropped_bytes += qdisc_pkt_len(skb);
569572
rtnl_kfree_skbs(skb, skb);
570573
}
571-
qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen,
572-
prev_backlog - sch->qstats.backlog);
574+
qdisc_tree_reduce_backlog(sch, dropped_pkts, dropped_bytes);
573575

574576
sch_tree_unlock(sch);
575577
return 0;

net/sched/sch_pie.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ static const struct nla_policy pie_policy[TCA_PIE_MAX + 1] = {
141141
static int pie_change(struct Qdisc *sch, struct nlattr *opt,
142142
struct netlink_ext_ack *extack)
143143
{
144+
unsigned int dropped_pkts = 0, dropped_bytes = 0;
144145
struct pie_sched_data *q = qdisc_priv(sch);
145146
struct nlattr *tb[TCA_PIE_MAX + 1];
146-
unsigned int qlen, dropped = 0;
147147
int err;
148148

149149
err = nla_parse_nested_deprecated(tb, TCA_PIE_MAX, opt, pie_policy,
@@ -193,15 +193,17 @@ static int pie_change(struct Qdisc *sch, struct nlattr *opt,
193193
nla_get_u32(tb[TCA_PIE_DQ_RATE_ESTIMATOR]));
194194

195195
/* Drop excess packets if new limit is lower */
196-
qlen = sch->q.qlen;
197196
while (sch->q.qlen > sch->limit) {
198197
struct sk_buff *skb = qdisc_dequeue_internal(sch, true);
199198

200-
dropped += qdisc_pkt_len(skb);
201-
qdisc_qstats_backlog_dec(sch, skb);
199+
if (!skb)
200+
break;
201+
202+
dropped_pkts++;
203+
dropped_bytes += qdisc_pkt_len(skb);
202204
rtnl_qdisc_drop(skb, sch);
203205
}
204-
qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped);
206+
qdisc_tree_reduce_backlog(sch, dropped_pkts, dropped_bytes);
205207

206208
sch_tree_unlock(sch);
207209
return 0;

0 commit comments

Comments
 (0)