* [PATCH net-next 0/4] net/sched: sch_cake: per-packet cost of a shaping CAKE
@ 2026-11-29 15:11 Julius Bairaktaris
2026-11-29 15:11 ` [PATCH net-next 1/4] net/sched: sch_cake: give the shaper timer slack Julius Bairaktaris
` (3 more replies)
0 siblings, 4 replies; 9+ messages in thread
From: Julius Bairaktaris @ 2026-11-29 15:11 UTC (permalink / raw)
To: Toke Høiland-Jørgensen
Cc: netdev, cake, Jamal Hadi Salim, Jiri Pirko, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
Simon Horman, Victor Nogueira
These patches cut the work a shaping CAKE does per packet.
Patch 1 lets the shaper's watchdog fire up to four packet times late,
capped at 100 us, as sch_fq does with its timer_slack.
Patch 2 draws the dither of the flow quantum from a per-tin prandom
state instead of get_random_u16().
Patch 3 keeps a GSO aggregate whole in its flow queue while its wire
time is short against the AQM target, and splits it at dequeue when
COBALT may act on it. Patch 4 skips the flow dissection and the host
hashes for a packet whose flow already holds an active queue.
The series applies to net-next on its own. Jamal's "net/sched: cap the
accounted backlog before it can wrap" [1] rewrites the classification
and the split-GSO path of cake_enqueue() that patches 3 and 4 change, so
whichever lands second is rebased onto the other. The split that patch 3
adds at dequeue refuses only to wrap CAKE's own backlog here; with both
applied it needs the ancestor check that [1] adds to the split at
enqueue as well, which goes in with that rebase.
Tested under QEMU (KVM) on net-next: the cake and cake_mq tc-testing
cases pass with the series (46 of 46, 1 run). For patch 3, forwarded TCP
that veth GRO turns into frame lists goes through cake with split-gso at
900 Mbit/s and a 300 KB memory limit: the aggregates are queued whole,
the memory limit splits and drops segments, and the backlog returns to
zero when the traffic stops, with no warnings in 1 run.
Measured on a Xiaomi AX3600 router (IPQ8074, four Cortex-A53) with the
patches on OpenWrt's 6.18 kernel:
- Patch 1, CAKE at 900 Mbit/s on the host path with 8 TCP streams and
four busy loops on the CPUs: 850/851/850 Mbit/s and an RTT p99 of
7.1/8.3/21.0 ms, against 807/851/849 Mbit/s and 43.3/15.4/36.5 ms
without the slack (3 runs each).
- Patch 2, shaping at 330/168 Mbit/s with 8 TCP streams: 6.4 % fewer
cycles per packet on the download and 4.3 % on the upload (3 runs
each; the noise between identical arms was about 3 %).
- Patch 3, a TCP download shaped at 320 Mbit/s: 10.5 % and 9.9 % fewer
cycles per received packet than with every aggregate split (69505
against 77660 and 62298 against 69153, one run each on two test
images, so only the ratio counts), with the probe's p99.9 delay rise
at 1.8 and 4.3 ms against 3.0 and 3.2 ms.
- Patch 4: in a perf profile of a shaped download before it, the
dissection and the two host hashes took 736 of 11380 cycles per
received packet (one profile).
I used Claude (Anthropic) to help write these patches.
[1] https://lore.kernel.org/all/QDISC-BA27.v13.r0.20261127065026@mojatatu.com/
Julius Bairaktaris (4):
net/sched: sch_cake: give the shaper timer slack
net/sched: sch_cake: dither the flow quantum from a per-tin generator
net/sched: sch_cake: keep short GSO aggregates whole
net/sched: sch_cake: skip the dissection for a packet of an active
flow
net/sched/sch_cake.c | 329 +++++++++++++++++++++++++++++++++++++++----
1 file changed, 303 insertions(+), 26 deletions(-)
base-commit: f89726ffabef1b08c52e6163b7ab94b0d5f15503
--
2.53.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH net-next 1/4] net/sched: sch_cake: give the shaper timer slack 2026-11-29 15:11 [PATCH net-next 0/4] net/sched: sch_cake: per-packet cost of a shaping CAKE Julius Bairaktaris @ 2026-11-29 15:11 ` Julius Bairaktaris 2026-11-30 15:46 ` netdev-bot+sashiko 2026-11-29 15:11 ` [PATCH net-next 2/4] net/sched: sch_cake: dither the flow quantum from a per-tin generator Julius Bairaktaris ` (2 subsequent siblings) 3 siblings, 1 reply; 9+ messages in thread From: Julius Bairaktaris @ 2026-11-29 15:11 UTC (permalink / raw) To: Toke Høiland-Jørgensen Cc: netdev, cake, Jamal Hadi Salim, Jiri Pirko, David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman, Victor Nogueira When the shaper holds the next packet back, CAKE arms its watchdog for the exact time that packet may leave. At a few hundred Mbit/s that is a timer interrupt, a softirq and a qdisc run for nearly every packet. Let the timer fire up to four packet times late, capped at 100 us, as sch_fq does with its timer_slack. The shaper keeps its rate, since it sends what it owes once the timer runs. Four packet times of 1514 bytes are 48 us at 1 Gbit/s and 5 us at 10 Gbit/s; below about 480 Mbit/s the cap applies. Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de> --- net/sched/sch_cake.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/net/sched/sch_cake.c b/net/sched/sch_cake.c index f97134522727..89f3c5cbb3de 100644 --- a/net/sched/sch_cake.c +++ b/net/sched/sch_cake.c @@ -229,6 +229,7 @@ struct cake_sched_data { /* time_next = time_this + ((len * rate_ns) >> rate_shft) */ ktime_t time_next_packet; ktime_t failsafe_next_packet; + u64 timer_slack; u64 rate_ns; u16 rate_shft; u16 overflow_timeout; @@ -1618,6 +1619,11 @@ static unsigned int cake_drop(struct Qdisc *sch, struct sk_buff **to_free) return idx + (tin << 16); } +static void cake_watchdog_schedule(struct cake_sched_data *q, u64 next) +{ + qdisc_watchdog_schedule_range_ns(&q->watchdog, next, q->timer_slack); +} + static u8 cake_handle_diffserv(struct sk_buff *skb, bool wash) { const int offset = skb_network_offset(skb); @@ -1797,7 +1803,7 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, ktime_to_ns( q->failsafe_next_packet)); sch->qstats.overlimits++; - qdisc_watchdog_schedule_ns(&q->watchdog, next); + cake_watchdog_schedule(q, next); } } } @@ -2071,7 +2077,7 @@ static struct sk_buff *cake_dequeue(struct Qdisc *sch) ktime_to_ns(q->failsafe_next_packet)); sch->qstats.overlimits++; - qdisc_watchdog_schedule_ns(&q->watchdog, next); + cake_watchdog_schedule(q, next); return NULL; } @@ -2281,7 +2287,7 @@ static struct sk_buff *cake_dequeue(struct Qdisc *sch) u64 next = min(ktime_to_ns(q->time_next_packet), ktime_to_ns(q->failsafe_next_packet)); - qdisc_watchdog_schedule_ns(&q->watchdog, next); + cake_watchdog_schedule(q, next); } else if (!sch->q.qlen) { int i; @@ -2636,6 +2642,16 @@ static void cake_configure_rates(struct Qdisc *sch, u64 rate, bool rate_adjust) qd->rate_ns = qd->tins[ft].tin_rate_ns; qd->rate_shft = qd->tins[ft].tin_rate_shft; + + /* Waking the shaper for every packet costs a timer interrupt and a + * softirq each. Letting the timer fire up to four packet times late + * lets it send a few at once, as sch_fq's timer_slack does; the shaper + * still meets its rate, since it catches up on what it owes. Capped so + * that a slow link waits no longer than a fast one would. + */ + qd->timer_slack = min_t(u64, 100 * NSEC_PER_USEC, + (4ULL * psched_mtu(qdisc_dev(sch)) * + qd->rate_ns) >> qd->rate_shft); } static void cake_reconfigure(struct Qdisc *sch) -- 2.53.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH net-next 1/4] net/sched: sch_cake: give the shaper timer slack 2026-11-29 15:11 ` [PATCH net-next 1/4] net/sched: sch_cake: give the shaper timer slack Julius Bairaktaris @ 2026-11-30 15:46 ` netdev-bot+sashiko 0 siblings, 0 replies; 9+ messages in thread From: netdev-bot+sashiko @ 2026-11-30 15:46 UTC (permalink / raw) To: julius Cc: toke, netdev, jhs, jiri, davem, edumazet, kuba, pabeni, willemb, horms, victor Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider. Critical: 0 · High: 0 · Medium: 2 · Low: 2 If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough). - [Medium] In net/sched/sch_cake.c, cake_configure_rates() sets the slack to qd->timer_slack = min(100 us, 4 * psched_mtu * rate_ns >>… - [Medium] The commit message presents the new slack as matching sch_fq ('as sch_fq does with its timer_slack'). - [Low] This is a pure performance change to a latency-focused qdisc. - [Low] The comment added above the slack calculation in cake_configure_rates() (net/sched/sch_cake.c:2646-2651) says the cap… -- net/sched: sch_cake: give the shaper timer slack This patch changes how CAKE arms its shaper watchdog. Instead of an exact expiry, it now uses qdisc_watchdog_schedule_range_ns() with a slack computed in cake_configure_rates(). The slack is four MTU packet times at the configured rate, capped at 100 us, and the goal is fewer timer interrupts and softirqs at a few hundred Mbit/s. > Let the timer fire up to four packet times late, capped at 100 us, as > sch_fq does with its timer_slack. The shaper keeps its rate, since it > sends what it owes once the timer runs. [Severity: Low] This isn't a bug, but this change trades up to 100 us of extra timer lateness for fewer timer interrupts and softirqs, in a qdisc that is focused on latency. Could the commit message include some measurements? Before and after figures for CPU use, interrupt or softirq counts, throughput, and latency or jitter would help. Going by the slack formula, the batching benefit looks limited to a middle band of rates. Below about 121 Mbit/s the slack is less than one packet time, and at 10 Gbit/s it is only about 5 us. > diff --git a/net/sched/sch_cake.c b/net/sched/sch_cake.c > index f97134522727e..89f3c5cbb3dee 100644 > --- a/net/sched/sch_cake.c > +++ b/net/sched/sch_cake.c [ ... ] > @@ -1618,6 +1619,11 @@ static unsigned int cake_drop(struct Qdisc *sch, struct sk_buff **to_free) > return idx + (tin << 16); > } > > +static void cake_watchdog_schedule(struct cake_sched_data *q, u64 next) > +{ > + qdisc_watchdog_schedule_range_ns(&q->watchdog, next, q->timer_slack); > +} [Severity: Medium] Below about 121 Mbit/s, is this slack shorter than one packet time? The slack is min(100 us, four MTU times). A 1514 byte frame takes about 121 us at 100 Mbit/s and about 1.2 ms at 10 Mbit/s, so the 100 us cap applies while each wait on the shaper is longer than the slack. qdisc_watchdog_schedule_range_ns() ends up in hrtimer_start_range_ns(). There, hrtimer_set_expires_range_ns() sets the hard expiry to next + slack. Unless some other interrupt lands inside that window, the watchdog fires at next + 100 us. cake_advance_shaper() moves the departure time on from its previous scheduled value, not from now: q->time_next_packet = ktime_add_ns(q->time_next_packet, global_dur); So a wakeup at T + S, where S is smaller than the packet time P, still releases only one packet. The watchdog is then re-armed for T + P plus the slack, from the global hard shaper branch in cake_dequeue() or the re-arm at its end. In that rate range, isn't the number of watchdog firings and qdisc runs unchanged? Meanwhile, each packet that waits on the shaper would leave up to about 100 us after its scheduled time. Many CAKE setups (home, CPE and ingress shaping) run below that rate. The commit message only says "below about 480 Mbit/s the cap applies". It doesn't mention this effect below about 121 Mbit/s. As far as I can tell, this behaviour is also unchanged later in the series. [ ... ] > @@ -2636,6 +2642,16 @@ static void cake_configure_rates(struct Qdisc *sch, u64 rate, bool rate_adjust) > > qd->rate_ns = qd->tins[ft].tin_rate_ns; > qd->rate_shft = qd->tins[ft].tin_rate_shft; > + > + /* Waking the shaper for every packet costs a timer interrupt and a > + * softirq each. Letting the timer fire up to four packet times late > + * lets it send a few at once, as sch_fq's timer_slack does; the shaper > + * still meets its rate, since it catches up on what it owes. Capped so > + * that a slow link waits no longer than a fast one would. [Severity: Low] This isn't a bug, but does this comment match the min_t() below it? rate_ns >> rate_shft is nanoseconds per byte, so the second argument is the time to send four MTU sized frames. On slow links that term is large and the 100 us cap wins: 4.8 ms becomes 100 us at 10 Mbit/s. On fast links the term is smaller, about 48 us at 1 Gbit/s and 5 us at 10 Gbit/s. So a slow link waits longer than a fast one (100 us against 5 to 48 us), not "no longer than" one. Was the intent that the extra wait on a slow link is bounded? > + */ > + qd->timer_slack = min_t(u64, 100 * NSEC_PER_USEC, > + (4ULL * psched_mtu(qdisc_dev(sch)) * > + qd->rate_ns) >> qd->rate_shft); [Severity: Medium] The commit message says this is done "as sch_fq does with its timer_slack". In sch_fq the slack is a tunable with a 10 us default, set in fq_init(): q->timer_slack = 10 * NSEC_PER_USEC; /* 10 usec of hrtimer slack */ Userspace can change it through TCA_FQ_TIMER_SLACK in fq_change(), and fq_dump() reports it. Here the slack is hard-coded in cake_configure_rates(), and it reaches 100 us on any link below about 480 Mbit/s. There is no TCA_CAKE_* attribute for it, no handling in cake_change(), and nothing in cake_dump(). If a user sees added latency or jitter from this, is there a way to turn it off or to see the value in use? The same slack also applies when cake_enqueue() arms the watchdog while the shaper is still paying off earlier traffic. > } -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129151144.2194312-1-julius%40bairaktaris.de ^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH net-next 2/4] net/sched: sch_cake: dither the flow quantum from a per-tin generator 2026-11-29 15:11 [PATCH net-next 0/4] net/sched: sch_cake: per-packet cost of a shaping CAKE Julius Bairaktaris 2026-11-29 15:11 ` [PATCH net-next 1/4] net/sched: sch_cake: give the shaper timer slack Julius Bairaktaris @ 2026-11-29 15:11 ` Julius Bairaktaris 2026-11-30 15:46 ` netdev-bot+sashiko 2026-11-29 15:11 ` [PATCH net-next 3/4] net/sched: sch_cake: keep short GSO aggregates whole Julius Bairaktaris 2026-11-29 15:11 ` [PATCH net-next 4/4] net/sched: sch_cake: skip the dissection for a packet of an active flow Julius Bairaktaris 3 siblings, 1 reply; 9+ messages in thread From: Julius Bairaktaris @ 2026-11-29 15:11 UTC (permalink / raw) To: Toke Høiland-Jørgensen Cc: netdev, cake, Jamal Hadi Salim, Jiri Pirko, David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman, Victor Nogueira CAKE adds a random 16-bit value to every flow quantum it hands out, so that the scaled quantum does not accumulate a rounding error. That is a get_random_u16() for nearly every packet dequeued, while the dithering needs no more than a uniform value. Draw the value from a prandom state in the tin instead, seeded from get_random_u64() when the qdisc is created. The tin is only touched under the qdisc lock, so the state needs no locking of its own. Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de> --- net/sched/sch_cake.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/net/sched/sch_cake.c b/net/sched/sch_cake.c index 89f3c5cbb3de..281c72ac530d 100644 --- a/net/sched/sch_cake.c +++ b/net/sched/sch_cake.c @@ -60,6 +60,7 @@ #include <linux/init.h> #include <linux/skbuff.h> #include <linux/jhash.h> +#include <linux/prandom.h> #include <linux/slab.h> #include <linux/vmalloc.h> #include <linux/reciprocal_div.h> @@ -156,6 +157,7 @@ struct cake_tin_data { u16 overflow_idx[CAKE_QUEUES]; struct cake_host hosts[CAKE_QUEUES]; /* for triple isolation */ u16 flow_quantum; + struct rnd_state dither; struct cobalt_params cparams; u32 drop_overlimit; @@ -703,11 +705,12 @@ static u16 cake_get_flow_quantum(struct cake_tin_data *q, host_load = max(host_load, q->hosts[flow->dsthost].dsthost_bulk_flow_count); - /* The get_random_u16() is a way to apply dithering to avoid - * accumulating roundoff errors + /* The random addend is a way to apply dithering to avoid + * accumulating roundoff errors. It only has to be uniform, so it + * comes from the tin's own generator rather than the system's. */ return (q->flow_quantum * quantum_div[host_load] + - get_random_u16()) >> 16; + (u16)prandom_u32_state(&q->dither)) >> 16; } static u32 cake_hash(struct cake_tin_data *q, const struct sk_buff *skb, @@ -2915,6 +2918,7 @@ static int cake_init(struct Qdisc *sch, struct nlattr *opt, INIT_LIST_HEAD(&b->new_flows); INIT_LIST_HEAD(&b->old_flows); INIT_LIST_HEAD(&b->decaying_flows); + prandom_seed_state(&b->dither, get_random_u64()); b->sparse_flow_count = 0; b->bulk_flow_count = 0; b->decaying_flow_count = 0; -- 2.53.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH net-next 2/4] net/sched: sch_cake: dither the flow quantum from a per-tin generator 2026-11-29 15:11 ` [PATCH net-next 2/4] net/sched: sch_cake: dither the flow quantum from a per-tin generator Julius Bairaktaris @ 2026-11-30 15:46 ` netdev-bot+sashiko 0 siblings, 0 replies; 9+ messages in thread From: netdev-bot+sashiko @ 2026-11-30 15:46 UTC (permalink / raw) To: julius Cc: toke, netdev, jhs, jiri, davem, edumazet, kuba, pabeni, willemb, horms, victor Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin. Low priority / nit picks for your consideration: - [Low] The commit message for "net/sched: sch_cake: dither the flow quantum from a per-tin generator" gives a performance motive but no… -- net/sched: sch_cake: dither the flow quantum from a per-tin generator This patch replaces the get_random_u16() dithering addend in cake_get_flow_quantum() with a value taken from a per-tin struct rnd_state. cake_init() seeds that state from get_random_u64() for each tin. > CAKE adds a random 16-bit value to every flow quantum it hands out, so > that the scaled quantum does not accumulate a rounding error. That is a > get_random_u16() for nearly every packet dequeued, while the dithering > needs no more than a uniform value. [Severity: Low] This isn't a bug, but are there any measurements behind the performance motivation here? The commit message has no cycle counts, pps figures or perf profile showing that get_random_u16() costs anything noticeable in cake_get_flow_quantum(). get_random_u16() already draws from a batched per-CPU buffer. The patch also adds a struct rnd_state to each struct cake_tin_data and moves this call from the system CSPRNG to prandom. Without numbers it is hard to tell what the change gains. Is "nearly every packet dequeued" accurate? In net/sched/sch_cake.c, cake_get_flow_quantum() has only two callers. In cake_dequeue() it runs only when the deficit is refilled: cake_dequeue() { ... if (flow->deficit <= 0) { ... WRITE_ONCE(flow->deficit, flow->deficit + cake_get_flow_quantum(b, flow, q->config->flow_mode)); ... } The second caller is cake_enqueue(), when a flow is activated as sparse. The commit message does not mention this one: cake_enqueue() { ... WRITE_ONCE(flow->deficit, cake_get_flow_quantum(b, flow, q->config->flow_mode)); ... } So the number of calls per dequeued packet is roughly pkt_len divided by the scaled quantum. cake_set_rate() clamps flow_quantum to 300..1514 bytes, and the quantum is then divided by host_load. Bulk MTU-sized or GSO traffic gets one or more calls per packet. Small packets such as ACKs get far fewer than one. Could the commit message describe the call frequency more precisely and include numbers that show the benefit? -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129151144.2194312-1-julius%40bairaktaris.de ^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH net-next 3/4] net/sched: sch_cake: keep short GSO aggregates whole 2026-11-29 15:11 [PATCH net-next 0/4] net/sched: sch_cake: per-packet cost of a shaping CAKE Julius Bairaktaris 2026-11-29 15:11 ` [PATCH net-next 1/4] net/sched: sch_cake: give the shaper timer slack Julius Bairaktaris 2026-11-29 15:11 ` [PATCH net-next 2/4] net/sched: sch_cake: dither the flow quantum from a per-tin generator Julius Bairaktaris @ 2026-11-29 15:11 ` Julius Bairaktaris 2026-11-30 15:46 ` netdev-bot+sashiko 2026-11-29 15:11 ` [PATCH net-next 4/4] net/sched: sch_cake: skip the dissection for a packet of an active flow Julius Bairaktaris 3 siblings, 1 reply; 9+ messages in thread From: Julius Bairaktaris @ 2026-11-29 15:11 UTC (permalink / raw) To: Toke Høiland-Jørgensen Cc: netdev, cake, Jamal Hadi Salim, Jiri Pirko, David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman, Victor Nogueira With split-gso, CAKE segments every GSO aggregate at enqueue. That is one skb_gso_segment() per aggregate, then a queue operation, a COBALT pass, a shaper step and a transmit per segment, and the device receives single frames even when it segments TCP itself. All segments of an aggregate share one flow, one host pair and one tin, so what splitting buys is the grain at which the flow is served and signalled, not a different share. Queue an aggregate unsplit when the time its bytes need at the tin's or the global shaper rate, whichever is slower, is at most 1/8 of the tin's COBALT target: 625 us with the default 5 ms, about 25 kB at 320 Mbit/s. cake_overhead() already sums the overhead of every segment of a GSO skb, so the shaper and the flow's deficit are charged what the split packets would have been charged, and the rate and the shares between flows and hosts are unchanged. The tin counts every segment as a packet. The decision is taken where the aggregate would be split, once its tin is known. At dequeue the head aggregate leaves whole only while it is still short at the current rate and COBALT has nothing to signal for any of its segments: no BLUE probability, not dropping, and its sojourn plus its own wire time within the target. COBALT signals only in the dropping state and enters it only for a packet whose sojourn exceeds the target, which then holds for no segment; a drop schedule still decaying from an earlier episode decays with the time, not with the packets, so it does not change the verdict either. A bulk flow at a standing queue near the target carries such a schedule almost all the time, so also requiring none would split nearly every aggregate: in one 12 s download on the router, 16329 of 133705 aggregates left whole with that test and 69969 of 130425 without it. Otherwise it is replaced by its segments first, with its enqueue time kept, and they are judged one at a time as before. The memory limit splits the same way, so that one segment is dropped and not the aggregate; drop_overlimit then counts the drops, not the change of the queue length. The aggregate is taken off its queue before it is segmented: skb_segment_list() returns a frame list's head as the first segment and links the others through its next pointer. The segments take its place in front of the packet that followed it. They are accounted by their own length, as at enqueue, so the backlog changes by their sum against the length the aggregate was queued with, and the split is refused where that would wrap the backlog. An aggregate that cannot be split stays at the head and is dropped by the path that wanted it split, with that path's counters: the memory limit's drop, or a drop at dequeue in place of COBALT's verdict. A frame list that fails half way has lost the segments it had cut, so it cannot be sent. The ACK filter does not run on a whole aggregate, as it does not run on segments split at enqueue. What differs: a flow's bytes of up to one aggregate leave in one step, at most target/8 ahead of where the shaper would have spread them, so a packet of another flow can wait that long behind it instead of one MTU. qlen counts a whole aggregate as one packet. Aggregates that need longer and everything on a qdisc with no rate are split as before, and no-split-gso is unchanged. Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de> --- net/sched/sch_cake.c | 225 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 217 insertions(+), 8 deletions(-) diff --git a/net/sched/sch_cake.c b/net/sched/sch_cake.c index 281c72ac530d..e7bf7bc97d9f 100644 --- a/net/sched/sch_cake.c +++ b/net/sched/sch_cake.c @@ -1566,7 +1566,178 @@ static int cake_advance_shaper(struct cake_sched_data *q, return len; } -static unsigned int cake_drop(struct Qdisc *sch, struct sk_buff **to_free) +/* A GSO aggregate may stay whole in a flow queue while the time its bytes + * need on the wire is small against the tin's target. Splitting it costs a + * segmentation, a queue operation and a shaper step per segment, and hands + * the device single frames. The aggregate is charged to the shaper and to the + * flow's deficit with the overhead of every segment it carries, so the rate + * and the shares between flows are those of the split packets. What changes + * is the grain: the flow's bytes leave in one step, at most 1/8 of the AQM + * target ahead of where the shaper would have spread them. Without a rate + * there is no wire time to bound, so those aggregates are split. + */ +#define CAKE_GSO_WHOLE_SHIFT 3 + +/* the time @adjlen bytes take at the slower of the tin's and the global rate */ +static u64 cake_wire_ns(const struct cake_sched_data *q, + const struct cake_tin_data *b, u32 adjlen) +{ + return max(((u64)adjlen * q->rate_ns) >> q->rate_shft, + ((u64)adjlen * b->tin_rate_ns) >> b->tin_rate_shft); +} + +static bool cake_gso_keep_whole(const struct cake_sched_data *q, + const struct cake_tin_data *b, u32 adjlen) +{ + if (!q->rate_ns) + return false; + + return cake_wire_ns(q, b, adjlen) <= + b->cparams.target >> CAKE_GSO_WHOLE_SHIFT; +} + +/* Whether the whole aggregate @skb at the head of @flow may leave as one + * packet now: it is still short at the current rate, and COBALT has nothing + * to signal for any of its segments, so that judging it once is the same as + * judging each of them. That holds with no BLUE probability, outside the + * dropping state, and when even its last byte, sent behind all the others, + * leaves within the target: no segment is then over the target when it leaves, + * so none enters the dropping state, and a drop schedule still decaying from an + * earlier one (a non-zero count) decays with the time alone. + */ +static bool cake_gso_dequeue_whole(const struct cake_sched_data *q, + const struct cake_tin_data *b, + const struct cake_flow *flow, ktime_t now, + const struct sk_buff *skb) +{ + const struct cobalt_vars *vars = &flow->cvars; + u32 adjlen = get_cobalt_cb(skb)->adjusted_len; + u64 sojourn; + + if (vars->p_drop || vars->dropping || + !cake_gso_keep_whole(q, b, adjlen)) + return false; + + sojourn = ktime_to_ns(ktime_sub(now, cobalt_get_enqueue_time(skb))); + return sojourn + cake_wire_ns(q, b, adjlen) <= b->cparams.target; +} + +/* Replace the aggregate at the head of flow @idx by its segments, so that the + * queue is what splitting at enqueue would have left: each segment with its + * own length and overhead, the enqueue time of the aggregate, and the backlog, + * memory and byte counts of the segments. The tin's packet count already holds + * every segment. @notify tells the parents about the change; cake_drop() + * passes false, since its caller reports the change of the queue it measures + * around it, and its @to_free, where cake_dequeue() passes NULL. + * + * Returns the number of packets added to the queue, or -1 when the aggregate + * could not be split: it is then left at the head, charged its current + * truesize, for the caller to drop through its own drop path, with the reason + * in @reason when that is not NULL. That happens when the segmentation fails, + * since a frame list that fails half way has lost the segments it had cut, and + * when the segments would account more than the backlog can hold: they are + * counted by their own length, which a size table can make larger than the + * length the aggregate was queued with. + * + * The aggregate leaves the queue before it is segmented: skb_segment_list() + * returns the aggregate itself as the first segment and links the others + * through its next pointer, so the packet behind it is kept here, and the + * memory it was charged at enqueue too, which the segmentation may change. + */ +static int cake_split_head(struct cake_sched_data *q, struct Qdisc *sch, + struct cake_tin_data *b, u32 idx, bool notify, + struct sk_buff **to_free, + enum qdisc_drop_reason *reason) +{ + struct cake_flow *flow = &b->flows[idx]; + struct sk_buff *skb = flow->head, *next = skb->next; + struct sk_buff *segs, *seg, *nskb, *last = NULL; + netdev_features_t features = netif_skb_features(skb); + ktime_t enqueue_time = cobalt_get_enqueue_time(skb); + unsigned int truesize = skb->truesize, segsize = 0; + u32 len = qdisc_pkt_len(skb), numsegs = 0; + enum qdisc_drop_reason why; + u64 slen = 0; + s64 dlen; + + skb_mark_not_on_list(skb); + segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK); + if (IS_ERR_OR_NULL(segs)) { + why = QDISC_DROP_GENERIC; + goto unsplit; + } + + skb_list_walk_safe(segs, seg, nskb) { + numsegs++; + slen += seg->len; + segsize += seg->truesize; + last = seg; + } + + if (unlikely((u64)READ_ONCE(sch->qstats.backlog) + slen > + (u64)U32_MAX + len)) { + qdisc_qstats_overlimit(sch); + why = QDISC_DROP_OVERLIMIT; + /* @skb stays, as the first segment of a frame list */ + skb_list_walk_safe(segs, seg, nskb) { + if (seg == skb) + continue; + skb_mark_not_on_list(seg); + if (to_free) { + tcf_set_qdisc_drop_reason(seg, why); + __qdisc_drop(seg, to_free); + } else { + qdisc_dequeue_drop(sch, seg, why); + } + } + /* the reference skb_segment_list() took on it */ + if (segs == skb) + consume_skb(skb); + goto unsplit; + } + + skb_list_walk_safe(segs, seg, nskb) { + qdisc_skb_cb(seg)->pkt_len = seg->len; + qdisc_skb_cb(seg)->pkt_segs = 1; + cobalt_set_enqueue_time(seg, enqueue_time); + get_cobalt_cb(seg)->adjusted_len = cake_overhead(q, seg); + } + + /* the list ends at the packet behind the aggregate */ + last->next = next; + flow->head = segs; + if (flow->tail == skb) + flow->tail = last; + + dlen = (s64)slen - len; + WRITE_ONCE(sch->q.qlen, sch->q.qlen + numsegs - 1); + qstats_backlog_add(sch, dlen); + WRITE_ONCE(b->backlogs[idx], b->backlogs[idx] + dlen); + WRITE_ONCE(b->tin_backlog, b->tin_backlog + dlen); + WRITE_ONCE(b->bytes, b->bytes + dlen); + q->avg_window_bytes += dlen; + q->buffer_used += segsize - truesize; + if (q->overflow_timeout) + cake_heapify_up(q, b->overflow_idx[idx]); + if (notify) + qdisc_tree_reduce_backlog(sch, 1 - numsegs, -dlen); + + /* the aggregate, or the reference skb_segment_list() took on it */ + consume_skb(skb); + return numsegs - 1; + +unsplit: + /* the drop paths uncharge the skb's own truesize */ + q->buffer_used += skb->truesize - truesize; + skb->next = next; + if (reason) + *reason = why; + return -1; +} + +/* @grown counts the packets that splitting an aggregate added to the queue */ +static unsigned int cake_drop(struct Qdisc *sch, struct sk_buff **to_free, + unsigned int *grown) { struct cake_sched_data *q = qdisc_priv(sch); ktime_t now = ktime_get(); @@ -1591,6 +1762,16 @@ static unsigned int cake_drop(struct Qdisc *sch, struct sk_buff **to_free) b = &q->tins[tin]; flow = &b->flows[idx]; + /* drop one segment, as with split packets, not the whole aggregate */ + if (flow->head && qdisc_pkt_segs(flow->head) > 1 && + (q->config->rate_flags & CAKE_FLAG_SPLIT_GSO)) { + int added = cake_split_head(q, sch, b, idx, false, to_free, + NULL); + + /* else the aggregate is dropped below */ + if (added > 0) + *grown += added; + } skb = dequeue_head(flow); if (unlikely(!skb)) { /* heap has gone wrong, rebuild it next time */ @@ -1774,9 +1955,11 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, int len = qdisc_pkt_len(skb), ret; struct sk_buff *ack = NULL; ktime_t now = ktime_get(); + bool same_flow = false, whole = false; struct cake_tin_data *b; struct cake_flow *flow; - bool same_flow = false; + unsigned int grown = 0; + u32 adjlen = 0; /* choose flow to insert into */ idx = cake_classify(sch, &b, skb, q->config->flow_mode, &ret); @@ -1814,7 +1997,14 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, if (unlikely(len > b->max_skblen)) WRITE_ONCE(b->max_skblen, len); - if (qdisc_pkt_segs(skb) > 1 && q->config->rate_flags & CAKE_FLAG_SPLIT_GSO) { + if (qdisc_pkt_segs(skb) > 1 && + q->config->rate_flags & CAKE_FLAG_SPLIT_GSO) { + adjlen = cake_overhead(q, skb); + whole = cake_gso_keep_whole(q, b, adjlen); + } + + if (qdisc_pkt_segs(skb) > 1 && !whole && + q->config->rate_flags & CAKE_FLAG_SPLIT_GSO) { struct sk_buff *segs, *nskb; netdev_features_t features = netif_skb_features(skb); unsigned int slen = 0, numsegs = 0; @@ -1853,10 +2043,12 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, int ack_pkt_len = 0; cobalt_set_enqueue_time(skb, now); - get_cobalt_cb(skb)->adjusted_len = cake_overhead(q, skb); + get_cobalt_cb(skb)->adjusted_len = whole ? adjlen : + cake_overhead(q, skb); flow_queue_add(flow, skb); - if (q->config->ack_filter) + /* the filter does not run on segments split above either */ + if (q->config->ack_filter && !whole) ack = cake_ack_filter(q, flow); if (ack) { @@ -1877,7 +2069,8 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, } /* stats */ - WRITE_ONCE(b->packets, b->packets + 1); + WRITE_ONCE(b->packets, b->packets + + (whole ? qdisc_pkt_segs(skb) : 1)); qstats_backlog_add(sch, len - ack_pkt_len); q->avg_window_bytes += len - ack_pkt_len; WRITE_ONCE(b->bytes, b->bytes + len - ack_pkt_len); @@ -1969,7 +2162,7 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, prev_backlog = sch->qstats.backlog; while (q->buffer_used > q->buffer_limit) { - drop_id = cake_drop(sch, to_free); + drop_id = cake_drop(sch, to_free, &grown); if ((drop_id >> 16) == tin && (drop_id & 0xFFFF) == idx) same_flow = true; @@ -1977,7 +2170,7 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, prev_qlen -= sch->q.qlen; prev_backlog -= sch->qstats.backlog; - b->drop_overlimit += prev_qlen; + b->drop_overlimit += prev_qlen + grown; if (same_flow) { qdisc_tree_reduce_backlog(sch, prev_qlen - 1, @@ -2198,6 +2391,15 @@ static struct sk_buff *cake_dequeue(struct Qdisc *sch) /* Retrieve a packet via the AQM */ while (1) { + enum qdisc_drop_reason unsplit = QDISC_DROP_UNSPEC; + + /* an aggregate that COBALT may act on is judged per segment */ + if (flow->head && qdisc_pkt_segs(flow->head) > 1 && + (q->config->rate_flags & CAKE_FLAG_SPLIT_GSO) && + !cake_gso_dequeue_whole(q, b, flow, now, flow->head)) + cake_split_head(q, sch, b, q->cur_flow, true, NULL, + &unsplit); + skb = cake_dequeue_one(sch); if (!skb) { /* this queue was actually empty */ @@ -2244,6 +2446,12 @@ static struct sk_buff *cake_dequeue(struct Qdisc *sch) goto begin; } + /* an aggregate that could not be split is not sent */ + if (unsplit) { + reason = unsplit; + goto drop_skb; + } + reason = cobalt_should_drop(&flow->cvars, &b->cparams, now, skb, (b->bulk_flow_count * !!(q->config->rate_flags & @@ -2252,6 +2460,7 @@ static struct sk_buff *cake_dequeue(struct Qdisc *sch) if (reason == QDISC_DROP_UNSPEC || !flow->head) break; +drop_skb: /* drop this packet, get another one */ if (q->config->rate_flags & CAKE_FLAG_INGRESS) { len = cake_advance_shaper(q, b, skb, -- 2.53.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH net-next 3/4] net/sched: sch_cake: keep short GSO aggregates whole 2026-11-29 15:11 ` [PATCH net-next 3/4] net/sched: sch_cake: keep short GSO aggregates whole Julius Bairaktaris @ 2026-11-30 15:46 ` netdev-bot+sashiko 0 siblings, 0 replies; 9+ messages in thread From: netdev-bot+sashiko @ 2026-11-30 15:46 UTC (permalink / raw) To: julius Cc: toke, netdev, jhs, jiri, davem, edumazet, kuba, pabeni, willemb, horms, victor Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider. Critical: 1 · High: 0 · Medium: 2 · Low: 3 If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough). - [Critical] In net/sched/sch_cake.c, cake_split_head() changes the u64 memory counter by a difference computed in unsigned int, which can… - [Medium] When cake_drop() splits an aggregate on the memory-limit path (cake_split_head() with notify=false), sch->q.qlen grows by numsegs-1… - [Medium] net/sched: sch_cake: avg_window_bytes underflow in cake_split_head() - [Low] The commit message and the comment on cake_gso_dequeue_whole() (net/sched/sch_cake.c) treat the test `sojourn + cake_wire_ns(q, b,… - [Low] The kernel-doc of cake_split_head() (net/sched/sch_cake.c) says "The tin's packet count already holds every segment." That is only… - [Low] cake_split_head() publishes the new flow head with a plain store, `flow->head = segs;` (net/sched/sch_cake.c:1708). -- net/sched: sch_cake: keep short GSO aggregates whole With split-gso set, this commit lets CAKE keep a GSO aggregate unsplit at enqueue when its wire time at the shaper rate is at most 1/8 of the tin's COBALT target. At dequeue, or when the memory limit is hit, the new cake_split_head() splits such an aggregate in place, unless COBALT has nothing to signal for it. The ACK filter is skipped for whole aggregates. > diff --git a/net/sched/sch_cake.c b/net/sched/sch_cake.c > index 281c72ac530d3..e7bf7bc97d9f1 100644 > --- a/net/sched/sch_cake.c > +++ b/net/sched/sch_cake.c > @@ -1566,7 +1566,178 @@ static int cake_advance_shaper(struct cake_sched_data *q, [ ... ] > +/* Whether the whole aggregate @skb at the head of @flow may leave as one > + * packet now: it is still short at the current rate, and COBALT has nothing > + * to signal for any of its segments, so that judging it once is the same as > + * judging each of them. That holds with no BLUE probability, outside the > + * dropping state, and when even its last byte, sent behind all the others, > + * leaves within the target: no segment is then over the target when it leaves, > + * so none enters the dropping state, and a drop schedule still decaying from an > + * earlier one (a non-zero count) decays with the time alone. > + */ > +static bool cake_gso_dequeue_whole(const struct cake_sched_data *q, > + const struct cake_tin_data *b, > + const struct cake_flow *flow, ktime_t now, > + const struct sk_buff *skb) > +{ > + const struct cobalt_vars *vars = &flow->cvars; > + u32 adjlen = get_cobalt_cb(skb)->adjusted_len; > + u64 sojourn; > + > + if (vars->p_drop || vars->dropping || > + !cake_gso_keep_whole(q, b, adjlen)) > + return false; > + > + sojourn = ktime_to_ns(ktime_sub(now, cobalt_get_enqueue_time(skb))); > + return sojourn + cake_wire_ns(q, b, adjlen) <= b->cparams.target; > +} [Severity: Low] The commit message says: At dequeue the head aggregate leaves whole only while it is still short at the current rate and COBALT has nothing to signal for any of its segments The comment above also says "judging it once is the same as judging each of them". Does that still hold when other flows are active in the same tin? cake_wire_ns() uses the tin or global shaper rate, so it assumes the segments would leave back to back. When the aggregate is split, cake_dequeue() serves the flow only until its deficit is used up. It then moves the flow to the tail of old_flows, behind the other active flows. With M competing flows, segment k leaves roughly k*M packet times after the first, not k. In that split schedule the later segments can go over the target and put COBALT into the dropping state. With this patch the aggregate leaves whole with no signal, and the mark or drop only lands on a later packet of the flow. The extra delay is bounded by about one aggregate. Could the commit message and the comment call this a difference rather than an equivalence? The single download measured in the message doesn't cover the multi-flow case. > + > +/* Replace the aggregate at the head of flow @idx by its segments, so that the > + * queue is what splitting at enqueue would have left: each segment with its > + * own length and overhead, the enqueue time of the aggregate, and the backlog, > + * memory and byte counts of the segments. The tin's packet count already holds > + * every segment. @notify tells the parents about the change; cake_drop() [Severity: Low] Is "The tin's packet count already holds every segment" always true? cake_enqueue() only counts every segment when whole is set: WRITE_ONCE(b->packets, b->packets + (whole ? qdisc_pkt_segs(skb) : 1)); whole is only computed while CAKE_FLAG_SPLIT_GSO is set, so an aggregate queued under no-split-gso adds 1 to b->packets. TCA_CAKE_SPLIT_GSO can be switched on at runtime through cake_change(). The split checks in cake_dequeue() and cake_drop() look only at the current rate_flags and at qdisc_pkt_segs(flow->head) > 1, so such an aggregate can be split later. cake_split_head() never adjusts b->packets, which leaves the per-tin packet count short by those segments. [ ... ] > +static int cake_split_head(struct cake_sched_data *q, struct Qdisc *sch, > + struct cake_tin_data *b, u32 idx, bool notify, > + struct sk_buff **to_free, > + enum qdisc_drop_reason *reason) > +{ > + struct cake_flow *flow = &b->flows[idx]; > + struct sk_buff *skb = flow->head, *next = skb->next; > + struct sk_buff *segs, *seg, *nskb, *last = NULL; > + netdev_features_t features = netif_skb_features(skb); > + ktime_t enqueue_time = cobalt_get_enqueue_time(skb); > + unsigned int truesize = skb->truesize, segsize = 0; [ ... ] > + /* the list ends at the packet behind the aggregate */ > + last->next = next; > + flow->head = segs; [Severity: Low] Should this be WRITE_ONCE(flow->head, segs)? cake_dump_class_stats() reads flow->head without the qdisc lock: if (READ_ONCE(flow->head)) { sch_tree_lock(sch); All the other writers (dequeue_head(), flow_queue_add() and cake_ack_filter()) use WRITE_ONCE(). KCSAN will probably report this plain store. > + if (flow->tail == skb) > + flow->tail = last; > + > + dlen = (s64)slen - len; > + WRITE_ONCE(sch->q.qlen, sch->q.qlen + numsegs - 1); > + qstats_backlog_add(sch, dlen); > + WRITE_ONCE(b->backlogs[idx], b->backlogs[idx] + dlen); > + WRITE_ONCE(b->tin_backlog, b->tin_backlog + dlen); > + WRITE_ONCE(b->bytes, b->bytes + dlen); > + q->avg_window_bytes += dlen; > + q->buffer_used += segsize - truesize; [Severity: Medium] Can this wrap q->avg_window_bytes? It is a u64, and dlen is negative whenever the length the aggregate was queued with is larger than the sum of its segments' lengths. That is the case the backlog check above was written for. With a size table, qdisc_pkt_len() includes the stab overhead, cell alignment or size_log rounding of the whole aggregate, while each segment is accounted here with plain seg->len. The backlog counters still hold len for the queued aggregate, so they are fine. avg_window_bytes is different: it only holds the bytes that arrived in the current autorate-ingress window. cake_enqueue() resets it to 0 when a window closes, and that happens after it has added the aggregate's len and before the cake_drop() loop runs. A split later in cake_dequeue(), or in cake_drop() on that same enqueue, then subtracts -dlen from a counter that never held those bytes. With a small or zero value it wraps to near U64_MAX. The next window close in cake_enqueue() computes: u64 b = q->avg_window_bytes * (u64)NSEC_PER_SEC; b = div64_u64(b, window_interval); from that value. The result goes into avg_peak_bandwidth with the fast EWMA weight when it is larger, and cake_reconfigure() then sets rate_bps and buffer_limit from it. With autorate-ingress and a size table, the shaper rate can end up set to an arbitrary value. The autorate setup is also where whole GRO aggregates are most likely, since autorate gives the qdisc a rate. Even without the wrap, those bytes were already counted in the window the aggregate arrived in, so changing the current window's count attributes them to the wrong window. Is there a reason for cake_split_head() to touch avg_window_bytes at all? Would you rather drop this update, or clamp it at zero? [Severity: Critical] Can this subtraction wrap? segsize and truesize are both unsigned int. If the segments' total truesize is lower than the aggregate's, segsize - truesize becomes 2^32 - d. That value is zero-extended into the u64 q->buffer_used, so the counter goes up by almost 4 GiB instead of down by d. The segments can have a smaller total. skb_segment() gives each new segment a fresh head plus its data_len: net/core/skbuff.c:skb_segment() { ... nskb->truesize += nskb->data_len; ... } A GRO aggregate, by contrast, carries the full rx buffer truesize of every frame merged into it. tcp_gso_segment() notes the same thing: "In some pathological cases, delta can be negative". The enqueue time split only ever added segs->truesize, so it could not wrap. The unsplit path has the same pattern: unsplit: /* the drop paths uncharge the skb's own truesize */ q->buffer_used += skb->truesize - truesize; There, skb_segment() can lower the head skb's truesize when it swaps head and tail truesize for sock_wfree. Each segment later uncharges only its own truesize, so once buffer_used is above the u32 buffer_limit nothing brings it back down. Every cake_enqueue() then runs: while (q->buffer_used > q->buffer_limit) { drop_id = cake_drop(sch, to_free, &grown); and drops every queued packet. Once all flows are empty, cake_drop() reaches: if (unlikely(!skb)) { /* heap has gone wrong, rebuild it next time */ q->overflow_timeout = 0; return idx + (tin << 16); } and returns without lowering buffer_used. Wouldn't the enqueue loop then spin forever, with the root qdisc lock held and BH disabled? This looks reachable with the default split-gso plus a shaper rate. A forwarded bulk TCP flow arrives as GRO aggregates, which are queued whole. They are split later, either in cake_dequeue() once sojourn plus wire time exceeds the target, or in cake_drop() on the memory limit path. Would a signed delta, for example (s64)segsize - truesize, avoid this? [ ... ] > @@ -1591,6 +1762,16 @@ static unsigned int cake_drop(struct Qdisc *sch, struct sk_buff **to_free) > > b = &q->tins[tin]; > flow = &b->flows[idx]; > + /* drop one segment, as with split packets, not the whole aggregate */ > + if (flow->head && qdisc_pkt_segs(flow->head) > 1 && > + (q->config->rate_flags & CAKE_FLAG_SPLIT_GSO)) { > + int added = cake_split_head(q, sch, b, idx, false, to_free, > + NULL); > + > + /* else the aggregate is dropped below */ > + if (added > 0) > + *grown += added; > + } [ ... ] > @@ -1969,7 +2162,7 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, > prev_backlog = sch->qstats.backlog; > > while (q->buffer_used > q->buffer_limit) { > - drop_id = cake_drop(sch, to_free); > + drop_id = cake_drop(sch, to_free, &grown); > if ((drop_id >> 16) == tin && > (drop_id & 0xFFFF) == idx) > same_flow = true; > @@ -1977,7 +2170,7 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, > > prev_qlen -= sch->q.qlen; > prev_backlog -= sch->qstats.backlog; > - b->drop_overlimit += prev_qlen; > + b->drop_overlimit += prev_qlen + grown; > > if (same_flow) { > qdisc_tree_reduce_backlog(sch, prev_qlen - 1, [Severity: Medium] drop_overlimit is corrected for the growth from a split, but prev_qlen still mixes growth and drops when it is passed to the parents. Would parent qdiscs undercount drops here? When cake_drop() splits an aggregate through cake_split_head() with notify set to false, sch->q.qlen grows by numsegs - 1 and the parents are not told. prev_qlen then equals drops - grown, which can be negative, and it is passed on as n: qdisc_tree_reduce_backlog(sch, prev_qlen - 1, prev_backlog - len); ... qdisc_tree_reduce_backlog(sch, prev_qlen, prev_backlog); qdisc_tree_reduce_backlog() uses n both as the qlen change and as the drop count: drops = max_t(int, n, 0); ... __qdisc_qstats_drop(sch, drops); For example, splitting a 44 segment aggregate and dropping one segment gives n = -42, so a parent such as an HTB class counts 0 drops. Parent qlen and backlog stay correct; only the parents' drop counters are wrong. -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129151144.2194312-1-julius%40bairaktaris.de ^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH net-next 4/4] net/sched: sch_cake: skip the dissection for a packet of an active flow 2026-11-29 15:11 [PATCH net-next 0/4] net/sched: sch_cake: per-packet cost of a shaping CAKE Julius Bairaktaris ` (2 preceding siblings ...) 2026-11-29 15:11 ` [PATCH net-next 3/4] net/sched: sch_cake: keep short GSO aggregates whole Julius Bairaktaris @ 2026-11-29 15:11 ` Julius Bairaktaris 2026-11-30 15:46 ` netdev-bot+sashiko 3 siblings, 1 reply; 9+ messages in thread From: Julius Bairaktaris @ 2026-11-29 15:11 UTC (permalink / raw) To: Toke Høiland-Jørgensen Cc: netdev, cake, Jamal Hadi Salim, Jiri Pirko, David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman, Victor Nogueira When the flow mode hashes flows and the skb carries an L4 hash, cake_hash() takes skb->hash as the flow hash. In the dual-srchost, dual-dsthost and triple-isolate modes it still dissects every packet and runs flow_hash_from_keys() twice for the host hashes, although its set lookup only reads those when the flow is given a queue it does not hold yet. Once the flow's tag is in its set and that queue is still in use, the lookup ends at the queue and leaves the host indices and counts as they are. Check for such a queue first and skip the dissection when the flow has one. In nat mode a conntrack tuple rewrites the keys, and the flow hash is then a hash of the rewritten keys rather than skb->hash, so the tuple is now looked up ahead of the dissection, once per packet as before, and a packet that has one takes the full path. A packet nat mode leaves alone, IPv6 or an IPv4 packet without a conntrack entry, can take the shortcut. Each packet ends up in the queue, with the host indices and the counters, that the full path gives it: per-flow and per-host isolation are unchanged, and two flows with the same skb->hash share one queue as they did before. Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de> --- net/sched/sch_cake.c | 72 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/net/sched/sch_cake.c b/net/sched/sch_cake.c index e7bf7bc97d9f..aee9bf963a29 100644 --- a/net/sched/sch_cake.c +++ b/net/sched/sch_cake.c @@ -596,26 +596,41 @@ static enum qdisc_drop_reason cobalt_should_drop(struct cobalt_vars *vars, return reason; } +/* The conntrack tuple nat mode takes a packet's addresses and ports from */ +struct cake_nat_tuple { +#if IS_ENABLED(CONFIG_NF_CONNTRACK) + struct nf_conntrack_tuple tuple; +#endif +}; + +static bool cake_nat_lookup(struct cake_nat_tuple *nat, + const struct sk_buff *skb) +{ +#if IS_ENABLED(CONFIG_NF_CONNTRACK) + if (skb_protocol(skb, true) != htons(ETH_P_IP)) + return false; + + return nf_ct_get_tuple_skb(&nat->tuple, skb); +#else + return false; +#endif +} + static bool cake_update_flowkeys(struct flow_keys *keys, + const struct cake_nat_tuple *nat, const struct sk_buff *skb) { #if IS_ENABLED(CONFIG_NF_CONNTRACK) - struct nf_conntrack_tuple tuple = {}; + const struct nf_conntrack_tuple *tuple = &nat->tuple; bool rev = !skb->_nfct, upd = false; __be32 ip; - if (skb_protocol(skb, true) != htons(ETH_P_IP)) - return false; - - if (!nf_ct_get_tuple_skb(&tuple, skb)) - return false; - - ip = rev ? tuple.dst.u3.ip : tuple.src.u3.ip; + ip = rev ? tuple->dst.u3.ip : tuple->src.u3.ip; if (ip != keys->addrs.v4addrs.src) { keys->addrs.v4addrs.src = ip; upd = true; } - ip = rev ? tuple.src.u3.ip : tuple.dst.u3.ip; + ip = rev ? tuple->src.u3.ip : tuple->dst.u3.ip; if (ip != keys->addrs.v4addrs.dst) { keys->addrs.v4addrs.dst = ip; upd = true; @@ -624,12 +639,12 @@ static bool cake_update_flowkeys(struct flow_keys *keys, if (keys->ports.ports) { __be16 port; - port = rev ? tuple.dst.u.all : tuple.src.u.all; + port = rev ? tuple->dst.u.all : tuple->src.u.all; if (port != keys->ports.src) { keys->ports.src = port; upd = true; } - port = rev ? tuple.src.u.all : tuple.dst.u.all; + port = rev ? tuple->src.u.all : tuple->dst.u.all; if (port != keys->ports.dst) { keys->ports.dst = port; upd = true; @@ -713,6 +728,25 @@ static u16 cake_get_flow_quantum(struct cake_tin_data *q, (u16)prandom_u32_state(&q->dither)) >> 16; } +/* Whether the flow with @hash holds a queue of @q that is still in use. Its + * tag is then in that queue's set once, and cake_hash() resolves to that + * queue without allocating, so it reads no host hash. + */ +static bool cake_flow_active(const struct cake_tin_data *q, u32 hash) +{ + u32 reduced_hash = hash % CAKE_QUEUES; + u32 inner_hash = reduced_hash % CAKE_SET_WAYS; + u32 outer_hash = reduced_hash - inner_hash; + u32 i, k; + + for (i = 0, k = inner_hash; i < CAKE_SET_WAYS; + i++, k = (k + 1) % CAKE_SET_WAYS) + if (q->tags[outer_hash + k] == hash) + return q->flows[outer_hash + k].set != CAKE_SET_NONE; + + return false; +} + static u32 cake_hash(struct cake_tin_data *q, const struct sk_buff *skb, int flow_mode, u16 flow_override, u16 host_override) { @@ -723,6 +757,8 @@ static u32 cake_hash(struct cake_tin_data *q, const struct sk_buff *skb, u16 reduced_hash, srchost_idx, dsthost_idx; struct flow_keys keys, host_keys; bool use_skbhash = skb->l4_hash; + struct cake_nat_tuple nat; + bool nat_found = false; if (unlikely(flow_mode == CAKE_FLOW_NONE)) return 0; @@ -734,11 +770,23 @@ static u32 cake_hash(struct cake_tin_data *q, const struct sk_buff *skb, if ((!hash_flows || (use_skbhash && !nat_enabled)) && !hash_hosts) goto skip_hash; + if (nat_enabled) + nat_found = cake_nat_lookup(&nat, skb); + + /* Without a conntrack tuple to rewrite the keys from, the SKB hash is + * the flow hash. The host hashes are only read when a queue is newly + * allocated to the flow, so a packet of a flow that already holds an + * active queue needs neither them nor the dissection. + */ + if (hash_flows && use_skbhash && !nat_found && + (!hash_hosts || cake_flow_active(q, skb->hash))) + goto skip_hash; + skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); /* Don't use the SKB hash if we change the lookup keys from conntrack */ - if (nat_enabled && cake_update_flowkeys(&keys, skb)) + if (nat_found && cake_update_flowkeys(&keys, &nat, skb)) use_skbhash = false; /* If we can still use the SKB hash and don't need the host hash, we can -- 2.53.0 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH net-next 4/4] net/sched: sch_cake: skip the dissection for a packet of an active flow 2026-11-29 15:11 ` [PATCH net-next 4/4] net/sched: sch_cake: skip the dissection for a packet of an active flow Julius Bairaktaris @ 2026-11-30 15:46 ` netdev-bot+sashiko 0 siblings, 0 replies; 9+ messages in thread From: netdev-bot+sashiko @ 2026-11-30 15:46 UTC (permalink / raw) To: julius Cc: toke, netdev, jhs, jiri, davem, edumazet, kuba, pabeni, willemb, horms, victor Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin. Low priority / nit picks for your consideration: - [Low] The commit message of this performance-only patch has no measurement, does not mention a small extra cost, and does not cover… -- net/sched: sch_cake: skip the dissection for a packet of an active flow In cake_hash(), this patch skips skb_flow_dissect_flow_keys() and the host hash computation when three things hold: the skb carries an L4 hash, nat mode has no conntrack tuple to rewrite the keys, and the flow already holds an active queue. The conntrack tuple lookup moves ahead of the dissection into a new cake_nat_lookup(). A new helper, cake_flow_active(), scans the flow's set for an in-use queue. > diff --git a/net/sched/sch_cake.c b/net/sched/sch_cake.c > index e7bf7bc97d9f1..aee9bf963a291 100644 > --- a/net/sched/sch_cake.c > +++ b/net/sched/sch_cake.c [ ... ] > @@ -713,6 +728,25 @@ static u16 cake_get_flow_quantum(struct cake_tin_data *q, > (u16)prandom_u32_state(&q->dither)) >> 16; > } > > +/* Whether the flow with @hash holds a queue of @q that is still in use. Its > + * tag is then in that queue's set once, and cake_hash() resolves to that > + * queue without allocating, so it reads no host hash. > + */ > +static bool cake_flow_active(const struct cake_tin_data *q, u32 hash) > +{ > + u32 reduced_hash = hash % CAKE_QUEUES; > + u32 inner_hash = reduced_hash % CAKE_SET_WAYS; > + u32 outer_hash = reduced_hash - inner_hash; > + u32 i, k; > + > + for (i = 0, k = inner_hash; i < CAKE_SET_WAYS; > + i++, k = (k + 1) % CAKE_SET_WAYS) > + if (q->tags[outer_hash + k] == hash) > + return q->flows[outer_hash + k].set != CAKE_SET_NONE; > + > + return false; > +} > + [ ... ] > @@ -734,11 +770,23 @@ static u32 cake_hash(struct cake_tin_data *q, const struct sk_buff *skb, > if ((!hash_flows || (use_skbhash && !nat_enabled)) && !hash_hosts) > goto skip_hash; > > + if (nat_enabled) > + nat_found = cake_nat_lookup(&nat, skb); > + > + /* Without a conntrack tuple to rewrite the keys from, the SKB hash is > + * the flow hash. The host hashes are only read when a queue is newly > + * allocated to the flow, so a packet of a flow that already holds an > + * active queue needs neither them nor the dissection. > + */ > + if (hash_flows && use_skbhash && !nat_found && > + (!hash_hosts || cake_flow_active(q, skb->hash))) > + goto skip_hash; [Severity: Low] This isn't a bug, but could the commit message cover a few more details? The patch is only a performance change. Are there any numbers showing how much skipping skb_flow_dissect_flow_keys() and the two flow_hash_from_keys() calls saves per packet? Sometimes hash_hosts is set and cake_flow_active() returns false. That happens for a new flow, a flow whose queue is back at CAKE_SET_NONE, or a tag collision. Such a packet still takes the full dissection path. The set-associative lookup later in cake_hash() then scans the same CAKE_SET_WAYS entries of q->tags[] a second time: net/sched/sch_cake.c:cake_hash() { ... for (i = 0, k = inner_hash; i < CAKE_SET_WAYS; i++, k = (k + 1) % CAKE_SET_WAYS) { if (q->tags[outer_hash + k] == flow_hash) { ... } Could the message mention this extra scan for packets that miss? The message also says: Check for such a queue first and skip the dissection when the flow has one. The !hash_hosts arm of the new condition also covers nat mode without host hashing, that is, flows plus nat or host_override set. There, any packet without a conntrack tuple now skips dissection with no queue check at all. Before the patch, such packets were dissected and cake_update_flowkeys() returned false, so the use_skbhash && !hash_hosts exit was taken. The selected queue is the same in both cases. Could the message describe this branch as well? > skb_flow_dissect_flow_keys(skb, &keys, > FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); > > /* Don't use the SKB hash if we change the lookup keys from conntrack */ > - if (nat_enabled && cake_update_flowkeys(&keys, skb)) > + if (nat_found && cake_update_flowkeys(&keys, &nat, skb)) > use_skbhash = false; -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129151144.2194312-1-julius%40bairaktaris.de ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-11-30 15:46 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-29 15:11 [PATCH net-next 0/4] net/sched: sch_cake: per-packet cost of a shaping CAKE Julius Bairaktaris 2026-11-29 15:11 ` [PATCH net-next 1/4] net/sched: sch_cake: give the shaper timer slack Julius Bairaktaris 2026-11-30 15:46 ` netdev-bot+sashiko 2026-11-29 15:11 ` [PATCH net-next 2/4] net/sched: sch_cake: dither the flow quantum from a per-tin generator Julius Bairaktaris 2026-11-30 15:46 ` netdev-bot+sashiko 2026-11-29 15:11 ` [PATCH net-next 3/4] net/sched: sch_cake: keep short GSO aggregates whole Julius Bairaktaris 2026-11-30 15:46 ` netdev-bot+sashiko 2026-11-29 15:11 ` [PATCH net-next 4/4] net/sched: sch_cake: skip the dissection for a packet of an active flow Julius Bairaktaris 2026-11-30 15:46 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox