* [Cake] [PATCH net v7 1/2] net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop
@ 2025-11-26 19:45 Xiang Mei
2025-11-26 19:45 ` [Cake] [PATCH net v7 2/2] selftests/tc-testing: Test CAKE scheduler when enqueue drops packets Xiang Mei
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Xiang Mei @ 2025-11-26 19:45 UTC (permalink / raw)
To: security; +Cc: netdev, toke, xiyou.wangcong, cake, bestswngs, Xiang Mei
In cake_drop(), qdisc_tree_reduce_backlog() is used to update the qlen
and backlog of the qdisc hierarchy. Its caller, cake_enqueue(), assumes
that the parent qdisc will enqueue the current packet. However, this
assumption breaks when cake_enqueue() returns NET_XMIT_CN: the parent
qdisc stops enqueuing current packet, leaving the tree qlen/backlog
accounting inconsistent. This mismatch can lead to a NULL dereference
(e.g., when the parent Qdisc is qfq_qdisc).
This patch computes the qlen/backlog delta in a more robust way by
observing the difference before and after the series of cake_drop()
calls, and then compensates the qdisc tree accounting if cake_enqueue()
returns NET_XMIT_CN.
To ensure correct compensation when ACK thinning is enabled, a new
variable is introduced to keep qlen unchanged.
Fixes: 15de71d06a40 ("net/sched: Make cake_enqueue return NET_XMIT_CN when past buffer_limit")
Signed-off-by: Xiang Mei <xmei5@asu.edu>
---
v2: add missing cc
v3: move qdisc_tree_reduce_backlog out of cake_drop
v4: remove redundant variable and handle ack branch correctly
v5: add the PoC as a test case
v6: split test and patch; fix the wrong drop count
v7: remove redundant comments
---
net/sched/sch_cake.c | 58 ++++++++++++++++++++++++--------------------
1 file changed, 32 insertions(+), 26 deletions(-)
diff --git a/net/sched/sch_cake.c b/net/sched/sch_cake.c
index 32bacfc314c2..d325a90cde9e 100644
--- a/net/sched/sch_cake.c
+++ b/net/sched/sch_cake.c
@@ -1597,7 +1597,6 @@ static unsigned int cake_drop(struct Qdisc *sch, struct sk_buff **to_free)
qdisc_drop_reason(skb, sch, to_free, SKB_DROP_REASON_QDISC_OVERLIMIT);
sch->q.qlen--;
- qdisc_tree_reduce_backlog(sch, 1, len);
cake_heapify(q, 0);
@@ -1743,14 +1742,14 @@ static void cake_reconfigure(struct Qdisc *sch);
static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
struct sk_buff **to_free)
{
+ u32 idx, tin, prev_qlen, prev_backlog, drop_id;
struct cake_sched_data *q = qdisc_priv(sch);
- int len = qdisc_pkt_len(skb);
- int ret;
+ int len = qdisc_pkt_len(skb), ret;
struct sk_buff *ack = NULL;
ktime_t now = ktime_get();
struct cake_tin_data *b;
struct cake_flow *flow;
- u32 idx, tin;
+ bool same_flow = false;
/* choose flow to insert into */
idx = cake_classify(sch, &b, skb, q->flow_mode, &ret);
@@ -1823,6 +1822,8 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
consume_skb(skb);
} else {
/* not splitting */
+ int ack_pkt_len = 0;
+
cobalt_set_enqueue_time(skb, now);
get_cobalt_cb(skb)->adjusted_len = cake_overhead(q, skb);
flow_queue_add(flow, skb);
@@ -1833,13 +1834,13 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
if (ack) {
b->ack_drops++;
sch->qstats.drops++;
- b->bytes += qdisc_pkt_len(ack);
- len -= qdisc_pkt_len(ack);
+ ack_pkt_len = qdisc_pkt_len(ack);
+ b->bytes += ack_pkt_len;
q->buffer_used += skb->truesize - ack->truesize;
if (q->rate_flags & CAKE_FLAG_INGRESS)
cake_advance_shaper(q, b, ack, now, true);
- qdisc_tree_reduce_backlog(sch, 1, qdisc_pkt_len(ack));
+ qdisc_tree_reduce_backlog(sch, 1, ack_pkt_len);
consume_skb(ack);
} else {
sch->q.qlen++;
@@ -1848,11 +1849,11 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
/* stats */
b->packets++;
- b->bytes += len;
- b->backlogs[idx] += len;
- b->tin_backlog += len;
- sch->qstats.backlog += len;
- q->avg_window_bytes += len;
+ b->bytes += len - ack_pkt_len;
+ b->backlogs[idx] += len - ack_pkt_len;
+ b->tin_backlog += len - ack_pkt_len;
+ sch->qstats.backlog += len - ack_pkt_len;
+ q->avg_window_bytes += len - ack_pkt_len;
}
if (q->overflow_timeout)
@@ -1927,24 +1928,29 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,
if (q->buffer_used > q->buffer_max_used)
q->buffer_max_used = q->buffer_used;
- if (q->buffer_used > q->buffer_limit) {
- bool same_flow = false;
- u32 dropped = 0;
- u32 drop_id;
+ if (q->buffer_used <= q->buffer_limit)
+ return NET_XMIT_SUCCESS;
- while (q->buffer_used > q->buffer_limit) {
- dropped++;
- drop_id = cake_drop(sch, to_free);
+ prev_qlen = sch->q.qlen;
+ prev_backlog = sch->qstats.backlog;
- if ((drop_id >> 16) == tin &&
- (drop_id & 0xFFFF) == idx)
- same_flow = true;
- }
- b->drop_overlimit += dropped;
+ while (q->buffer_used > q->buffer_limit) {
+ drop_id = cake_drop(sch, to_free);
+ if ((drop_id >> 16) == tin &&
+ (drop_id & 0xFFFF) == idx)
+ same_flow = true;
+ }
+
+ prev_qlen -= sch->q.qlen;
+ prev_backlog -= sch->qstats.backlog;
+ b->drop_overlimit += prev_qlen;
- if (same_flow)
- return NET_XMIT_CN;
+ if (same_flow) {
+ qdisc_tree_reduce_backlog(sch, prev_qlen - 1,
+ prev_backlog - len);
+ return NET_XMIT_CN;
}
+ qdisc_tree_reduce_backlog(sch, prev_qlen, prev_backlog);
return NET_XMIT_SUCCESS;
}
--
2.43.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Cake] [PATCH net v7 2/2] selftests/tc-testing: Test CAKE scheduler when enqueue drops packets
2025-11-26 19:45 [Cake] [PATCH net v7 1/2] net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop Xiang Mei
@ 2025-11-26 19:45 ` Xiang Mei
2025-11-27 9:14 ` [Cake] " Toke Høiland-Jørgensen
2025-11-27 18:22 ` Cong Wang
2025-11-27 9:14 ` [Cake] Re: [PATCH net v7 1/2] net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop Toke Høiland-Jørgensen
` (2 subsequent siblings)
3 siblings, 2 replies; 8+ messages in thread
From: Xiang Mei @ 2025-11-26 19:45 UTC (permalink / raw)
To: security; +Cc: netdev, toke, xiyou.wangcong, cake, bestswngs, Xiang Mei
Add tests that trigger packet drops in cake_enqueue(): "CAKE with QFQ
Parent - CAKE enqueue with packets dropping". It forces CAKE_enqueue to
return NET_XMIT_CN after dropping the packets when it has a QFQ parent.
Signed-off-by: Xiang Mei <xmei5@asu.edu>
---
v2: place the test in qdiscs.json
---
.../tc-testing/tc-tests/infra/qdiscs.json | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/tools/testing/selftests/tc-testing/tc-tests/infra/qdiscs.json b/tools/testing/selftests/tc-testing/tc-tests/infra/qdiscs.json
index 998e5a2f4579..e99ae8f81cf6 100644
--- a/tools/testing/selftests/tc-testing/tc-tests/infra/qdiscs.json
+++ b/tools/testing/selftests/tc-testing/tc-tests/infra/qdiscs.json
@@ -961,5 +961,33 @@
"teardown": [
"$TC qdisc del dev $DUMMY root"
]
+ },
+ {
+ "id": "4366",
+ "name": "CAKE with QFQ Parent - CAKE enqueue with packets dropping",
+ "category": [
+ "qdisc",
+ "cake",
+ "netem"
+ ],
+ "plugins": {
+ "requires": "nsPlugin"
+ },
+ "setup":[
+ "$TC qdisc add dev $DUMMY handle 1: root qfq",
+ "$TC class add dev $DUMMY parent 1: classid 1:1 qfq maxpkt 1024",
+ "$TC qdisc add dev $DUMMY parent 1:1 handle 2: cake memlimit 9",
+ "$TC filter add dev $DUMMY protocol ip parent 1: prio 1 u32 match ip protocol 1 0xff flowid 1:1",
+ "ping -I$DUMMY -f -c1 -s64 -W1 10.10.10.1 || true",
+ "$TC qdisc replace dev $DUMMY parent 1:1 handle 3: netem delay 0ms"
+ ],
+ "cmdUnderTest": "ping -I$DUMMY -f -c1 -s64 -W1 10.10.10.1 || true",
+ "expExitCode": "0",
+ "verifyCmd": "$TC -s qdisc show dev $DUMMY",
+ "matchPattern": "qdisc qfq 1:",
+ "matchCount": "1",
+ "teardown": [
+ "$TC qdisc del dev $DUMMY handle 1: root"
+ ]
}
]
--
2.43.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Cake] Re: [PATCH net v7 1/2] net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop
2025-11-26 19:45 [Cake] [PATCH net v7 1/2] net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop Xiang Mei
2025-11-26 19:45 ` [Cake] [PATCH net v7 2/2] selftests/tc-testing: Test CAKE scheduler when enqueue drops packets Xiang Mei
@ 2025-11-27 9:14 ` Toke Høiland-Jørgensen
2025-11-27 18:21 ` Cong Wang
2025-11-27 23:36 ` Jakub Kicinski
3 siblings, 0 replies; 8+ messages in thread
From: Toke Høiland-Jørgensen @ 2025-11-27 9:14 UTC (permalink / raw)
To: Xiang Mei, security; +Cc: netdev, xiyou.wangcong, cake, bestswngs, Xiang Mei
Xiang Mei <xmei5@asu.edu> writes:
> In cake_drop(), qdisc_tree_reduce_backlog() is used to update the qlen
> and backlog of the qdisc hierarchy. Its caller, cake_enqueue(), assumes
> that the parent qdisc will enqueue the current packet. However, this
> assumption breaks when cake_enqueue() returns NET_XMIT_CN: the parent
> qdisc stops enqueuing current packet, leaving the tree qlen/backlog
> accounting inconsistent. This mismatch can lead to a NULL dereference
> (e.g., when the parent Qdisc is qfq_qdisc).
>
> This patch computes the qlen/backlog delta in a more robust way by
> observing the difference before and after the series of cake_drop()
> calls, and then compensates the qdisc tree accounting if cake_enqueue()
> returns NET_XMIT_CN.
>
> To ensure correct compensation when ACK thinning is enabled, a new
> variable is introduced to keep qlen unchanged.
>
> Fixes: 15de71d06a40 ("net/sched: Make cake_enqueue return NET_XMIT_CN when past buffer_limit")
> Signed-off-by: Xiang Mei <xmei5@asu.edu>
Reviewed-by: Toke Høiland-Jørgensen <toke@toke.dk>
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Cake] Re: [PATCH net v7 2/2] selftests/tc-testing: Test CAKE scheduler when enqueue drops packets
2025-11-26 19:45 ` [Cake] [PATCH net v7 2/2] selftests/tc-testing: Test CAKE scheduler when enqueue drops packets Xiang Mei
@ 2025-11-27 9:14 ` Toke Høiland-Jørgensen
2025-11-27 18:22 ` Cong Wang
1 sibling, 0 replies; 8+ messages in thread
From: Toke Høiland-Jørgensen @ 2025-11-27 9:14 UTC (permalink / raw)
To: Xiang Mei, security; +Cc: netdev, xiyou.wangcong, cake, bestswngs, Xiang Mei
Xiang Mei <xmei5@asu.edu> writes:
> Add tests that trigger packet drops in cake_enqueue(): "CAKE with QFQ
> Parent - CAKE enqueue with packets dropping". It forces CAKE_enqueue to
> return NET_XMIT_CN after dropping the packets when it has a QFQ parent.
>
> Signed-off-by: Xiang Mei <xmei5@asu.edu>
Reviewed-by: Toke Høiland-Jørgensen <toke@toke.dk>
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Cake] Re: [PATCH net v7 1/2] net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop
2025-11-26 19:45 [Cake] [PATCH net v7 1/2] net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop Xiang Mei
2025-11-26 19:45 ` [Cake] [PATCH net v7 2/2] selftests/tc-testing: Test CAKE scheduler when enqueue drops packets Xiang Mei
2025-11-27 9:14 ` [Cake] Re: [PATCH net v7 1/2] net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop Toke Høiland-Jørgensen
@ 2025-11-27 18:21 ` Cong Wang
2025-11-27 23:36 ` Jakub Kicinski
3 siblings, 0 replies; 8+ messages in thread
From: Cong Wang @ 2025-11-27 18:21 UTC (permalink / raw)
To: Xiang Mei; +Cc: security, netdev, toke, cake, bestswngs
On Wed, Nov 26, 2025 at 12:45:12PM -0700, Xiang Mei wrote:
> In cake_drop(), qdisc_tree_reduce_backlog() is used to update the qlen
> and backlog of the qdisc hierarchy. Its caller, cake_enqueue(), assumes
> that the parent qdisc will enqueue the current packet. However, this
> assumption breaks when cake_enqueue() returns NET_XMIT_CN: the parent
> qdisc stops enqueuing current packet, leaving the tree qlen/backlog
> accounting inconsistent. This mismatch can lead to a NULL dereference
> (e.g., when the parent Qdisc is qfq_qdisc).
>
> This patch computes the qlen/backlog delta in a more robust way by
> observing the difference before and after the series of cake_drop()
> calls, and then compensates the qdisc tree accounting if cake_enqueue()
> returns NET_XMIT_CN.
>
> To ensure correct compensation when ACK thinning is enabled, a new
> variable is introduced to keep qlen unchanged.
>
> Fixes: 15de71d06a40 ("net/sched: Make cake_enqueue return NET_XMIT_CN when past buffer_limit")
> Signed-off-by: Xiang Mei <xmei5@asu.edu>
Acked-by: Cong Wang <cwang@multikernel.io>
Thanks for your patience!
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Cake] Re: [PATCH net v7 2/2] selftests/tc-testing: Test CAKE scheduler when enqueue drops packets
2025-11-26 19:45 ` [Cake] [PATCH net v7 2/2] selftests/tc-testing: Test CAKE scheduler when enqueue drops packets Xiang Mei
2025-11-27 9:14 ` [Cake] " Toke Høiland-Jørgensen
@ 2025-11-27 18:22 ` Cong Wang
1 sibling, 0 replies; 8+ messages in thread
From: Cong Wang @ 2025-11-27 18:22 UTC (permalink / raw)
To: Xiang Mei; +Cc: security, netdev, toke, cake, bestswngs
On Wed, Nov 26, 2025 at 12:45:13PM -0700, Xiang Mei wrote:
> Add tests that trigger packet drops in cake_enqueue(): "CAKE with QFQ
> Parent - CAKE enqueue with packets dropping". It forces CAKE_enqueue to
> return NET_XMIT_CN after dropping the packets when it has a QFQ parent.
>
> Signed-off-by: Xiang Mei <xmei5@asu.edu>
> ---
> v2: place the test in qdiscs.json
Acked-by: Cong Wang <cwang@multikernel.io>
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Cake] Re: [PATCH net v7 1/2] net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop
2025-11-26 19:45 [Cake] [PATCH net v7 1/2] net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop Xiang Mei
` (2 preceding siblings ...)
2025-11-27 18:21 ` Cong Wang
@ 2025-11-27 23:36 ` Jakub Kicinski
2025-11-28 0:16 ` Xiang Mei
3 siblings, 1 reply; 8+ messages in thread
From: Jakub Kicinski @ 2025-11-27 23:36 UTC (permalink / raw)
To: Xiang Mei; +Cc: security, netdev, toke, xiyou.wangcong, cake, bestswngs
On Wed, 26 Nov 2025 12:45:12 -0700 Xiang Mei wrote:
> In cake_drop(), qdisc_tree_reduce_backlog() is used to update the qlen
> and backlog of the qdisc hierarchy. Its caller, cake_enqueue(), assumes
> that the parent qdisc will enqueue the current packet. However, this
> assumption breaks when cake_enqueue() returns NET_XMIT_CN: the parent
> qdisc stops enqueuing current packet, leaving the tree qlen/backlog
> accounting inconsistent. This mismatch can lead to a NULL dereference
> (e.g., when the parent Qdisc is qfq_qdisc).
This series does not apply, please rebase on netdev/net/main.
--
pw-bot: cr
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Cake] Re: [PATCH net v7 1/2] net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop
2025-11-27 23:36 ` Jakub Kicinski
@ 2025-11-28 0:16 ` Xiang Mei
0 siblings, 0 replies; 8+ messages in thread
From: Xiang Mei @ 2025-11-28 0:16 UTC (permalink / raw)
To: Jakub Kicinski; +Cc: security, netdev, toke, xiyou.wangcong, cake, bestswngs
Thanks for the reminder. The conflict has been resolved in v8.
On Thu, Nov 27, 2025 at 4:36 PM Jakub Kicinski <kuba@kernel.org> wrote:
>
> On Wed, 26 Nov 2025 12:45:12 -0700 Xiang Mei wrote:
> > In cake_drop(), qdisc_tree_reduce_backlog() is used to update the qlen
> > and backlog of the qdisc hierarchy. Its caller, cake_enqueue(), assumes
> > that the parent qdisc will enqueue the current packet. However, this
> > assumption breaks when cake_enqueue() returns NET_XMIT_CN: the parent
> > qdisc stops enqueuing current packet, leaving the tree qlen/backlog
> > accounting inconsistent. This mismatch can lead to a NULL dereference
> > (e.g., when the parent Qdisc is qfq_qdisc).
>
> This series does not apply, please rebase on netdev/net/main.
> --
> pw-bot: cr
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2025-11-28 0:16 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-26 19:45 [Cake] [PATCH net v7 1/2] net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop Xiang Mei
2025-11-26 19:45 ` [Cake] [PATCH net v7 2/2] selftests/tc-testing: Test CAKE scheduler when enqueue drops packets Xiang Mei
2025-11-27 9:14 ` [Cake] " Toke Høiland-Jørgensen
2025-11-27 18:22 ` Cong Wang
2025-11-27 9:14 ` [Cake] Re: [PATCH net v7 1/2] net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop Toke Høiland-Jørgensen
2025-11-27 18:21 ` Cong Wang
2025-11-27 23:36 ` Jakub Kicinski
2025-11-28 0:16 ` Xiang Mei
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox