net/sched: cap the accounted backlog before it can wrap
Subsystems (4)
Jamal Hadi Salim
maintainerv7 cover net/sched: cap the accounted backlog before it can wrap
This series bounds the accounted backlog of the AQM qdiscs so that it cannot wrap mod 2^32.
v1 widened the counters to u64 and was rejected; from v2 on the fix drops at enqueue instead. The v5 posting was a single patch; at the reviewer's request it is split into four:
1/4 classifier refactor: cake_tcf_classify() and fq_codel_classify()
return their result to the caller.
2/4 cake: use a dedicated split-GSO list iterator.
3/4 the fix: drop at enqueue once the stored backlog cannot
represent the packet's accounted length -- the standing QDISC_MAX_BACKLOG ceiling for ordinary packets, a per-packet representability check for the larger wire estimates. Covers fq_codel, cake, codel, pie, fq_pie, dualpi2, RED, pfifo, plug and gred.
4/4 cake: split cake_hash() into side-effect-free key extraction (on
the unsegmented skb) and set-associative resolution (after admission), move the split-GSO path ahead of the resolution, and tag every deferred split segment.
The commit logs and the in-code comments are written for readability.
The guard half is a net fix, but the CAKE hash split is new-code net-next material, so the series targets net-next with no Fixes: (the aggregate wrap predates every touched qdisc).
Changes in v7:
- 1/4: complete the fq_codel_classify() comment (Sashiko nipa v6 main-2-1, Low). The comment listed only the terminal-action and no-class-ID cases; the function also returns true when no filter matched or when the selected class minor is above q->flows_cnt, and those packets are BYPASS drops, not a fallback to hashing as in cake. Comment-only, no code change; folded into this resend since the series is revised anyway.
- 3/4: do not clamp the shared qdisc_pkt_len() (Sashiko nipa v6 main-2-6, Medium). The clamp silently undercharged BPF __sk_buff.wire_len, act_police and the HTB/TBF shapers for a valid large GSO estimate. The initializer now stays truthful for every input and the overflow protection is enqueue admission: each affected qdisc drops a packet once the stored backlog cannot represent its accounted length -- the standing QDISC_MAX_BACKLOG ceiling covers ordinary packets, and a per-packet representability check covers the larger wire estimates, which a supported small-MSS BIG TCP aggregate can legitimately carry above 1 MiB while its raw length stays within GSO_MAX_SIZE. The consumers read the real length, and sch_handle_ingress() keeps complete pkt_segs metadata.
- 4/4: tag every deferred split-GSO segment with QDISC_DROP_OVERLIMIT (Sashiko nipa v6 gpt-6-astra-2-5, Medium). __skb_gso_segment() stores SKB_GSO_CB at cb offset 32, aliasing tc_skb_cb.drop_reason, and the cb is copied into every segment, so tagging only the head left the rest with GSO scratch as their drop reason.
- 4/4: snapshot q->config->flow_mode once with READ_ONCE() and pass it to both hash helpers (Sashiko nipa v6 gpt-6-astra-5-9, Low). The split introduced two loads with the segmentation work between them.
- Not folded: the 3/4 intermediate-commit split-GSO limitation (4/4 closes it; the pre-series path was already unbounded) and the Fixes:/stable suggestions (intentional net-next target). The gemini v6 TC_ACT_CONSUMED High is the v5 finding already rebutted as unreachable from a qdisc classify switch.
v6 rewrite as a four-patch series per Toke Høiland-Jørgensen's review of
v5 (split the classifier refactor, the split-GSO iterator, the overflow guard and the cake hash split; drop the machine-written prose). Considered the Sashiko gemini v5 TC_ACT_CONSUMED report and did not fold it (unreachable; see below). Reworded the cake block comment that had an embedded "/*" (Simon Horman's W=1 warning) and reworded a comment that overstated the classifier's return cases. The TC_ACT_CONSUMED switch case was dropped from the final 0001 because it still reached __qdisc_drop(); no in-tree producer can deliver TC_ACT_CONSUMED to a qdisc classify switch (act_ct returns it only on a clsact/ingress qdisc, act_mirred only for a non-cloning ingress redirect, and cls_bpf/act_bpf remap an unknown verdict to UNSPEC).
v5/v4/v3/v2 history: see the v6 posting. In short, v3 adopted the stored
backlog compare qdisc_backlog_at_max() (no double-count of the 1 MiB headroom, no per-enqueue 64-bit add), moved the classifier and the ceiling check ahead of the hash/shaper state, and added pfifo; v4 split the hash keys from the set-associative resolution and handled the fraglist-GSO self-link; v2 replaced the rejected u64 widening with the enqueue drop; v1 widened the counters.
Jamal Hadi Salim (4):
net/sched: cake, fq_codel: return the classifier result to the caller net/sched: cake: use a dedicated split-GSO list iterator net/sched: cap the accounted backlog before it can wrap net/sched: cake: hash before segmenting and reject an overlong split
include/net/pkt_sched.h | 1 - include/net/sch_generic.h | 20 +++++ net/sched/sch_cake.c | 184 +++++++++++++++++++++++++++----------- net/sched/sch_codel.c | 16 ++-- net/sched/sch_dualpi2.c | 1 + net/sched/sch_fifo.c | 6 ++ net/sched/sch_fq_codel.c | 55 +++++++++--- net/sched/sch_fq_pie.c | 3 +- net/sched/sch_gred.c | 9 ++ net/sched/sch_pie.c | 3 +- net/sched/sch_plug.c | 9 +- net/sched/sch_red.c | 23 +++-- 12 files changed, 251 insertions(+), 79 deletions(-)
Signature · 1 line
2.43.0
Jamal Hadi Salim
maintainerPATCH v7 1/4 net/sched: cake, fq_codel: return the classifier result to the caller
The Fix:
cake and fq_codel run an optional external classifier from their enqueue path and then compute the flow hash inside the helper. Have the helper return the filter's overrides (cake) or a 1-based flow index or a request to hash (fq_codel) instead, so the caller owns the flow selection.
This is preparation for the accounting changes that follow. Packet classification and flow selection are unchanged.
--- net/sched/sch_cake.c | 35 +++++++++++++++++++----------- net/sched/sch_fq_codel.c | 47 +++++++++++++++++++++++++++++----------- 2 files changed, 56 insertions(+), 26 deletions(-)
net/sched/sch_cake.c +22 -13
@@ -1712,18 +1712,24 @@ static struct cake_tin_data *cake_select_tin(struct Qdisc *sch,return &qd->tins[tin]; } -static u32 cake_classify (struct Qdisc *sch,struct cake_tin_data **t, -struct sk_buff *skb,int flow_mode,int *qerr) +/* Run the qdisc's optional external classifier. Returns true when a +* terminal TC action consumed the packet, with the reason in *qerr; +* otherwise *flow and *host hold the filter's overrides (0 if none). +*/ +static bool cake_tcf_classify (struct Qdisc *sch,struct sk_buff *skb, +u16 *flow,u16 *host,int *qerr) {struct cake_sched_data *q =qdisc_priv (sch);struct tcf_proto *filter;struct tcf_result res; -u16 flow =0 , host =0 ;int result; + *flow =0 ; + *host =0 ; + filter =rcu_dereference_bh (q->filter_list);if (!filter) - goto hash; +return false; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; result =tcf_classify_qdisc (skb, filter, &res, false);
@@ -1737,17 +1743,15 @@ static u32 cake_classify(struct Qdisc *sch, struct cake_tin_data **t, *qerr =NET_XMIT_SUCCESS | __NET_XMIT_STOLEN; fallthrough;case TC_ACT_SHOT : - return 0; + return true; }#endif if (TC_H_MIN (res.classid) <=CAKE_QUEUES ) - flow =TC_H_MIN (res.classid); + *flow =TC_H_MIN (res.classid);if (TC_H_MAJ (res.classid) <= (CAKE_QUEUES <<16 )) - host =TC_H_MAJ (res.classid) >>16 ; + *host =TC_H_MAJ (res.classid) >>16 ; } -hash: - *t =cake_select_tin (sch, skb); -return cake_hash (*t, skb, flow_mode, flow, host) +1 ; +return false; }static void cake_reconfigure (struct Qdisc *sch);
@@ -1758,20 +1762,25 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,u32 idx, tin, prev_qlen, prev_backlog, drop_id;struct cake_sched_data *q =qdisc_priv (sch);int len =qdisc_pkt_len (skb), ret; +u16 flow_override, host_override;struct sk_buff *ack =NULL ;ktime_t now =ktime_get ();struct cake_tin_data *b;struct cake_flow *flow;bool same_flow = false; -/* choose flow to insert into */ - idx =cake_classify (sch, &b, skb, q->config->flow_mode, &ret); -if (idx ==0 ) { +/* Classify first: a terminal TC action consumes the packet. */ +if (cake_tcf_classify (sch, skb, &flow_override, &host_override, &ret)) {if (ret & __NET_XMIT_BYPASS)qdisc_qstats_drop (sch);__qdisc_drop (skb, to_free);return ret; } + +/* choose flow to insert into */ + b =cake_select_tin (sch, skb); + idx =cake_hash (b, skb, q->config->flow_mode, flow_override, + host_override) +1 ; tin = (u32)(b - q->tins); idx--; flow = &b->flows[idx];
net/sched/sch_fq_codel.c +34 -13
86 lines not on this page · raw message
Signature · 1 line
2.43.0
Toke Høiland-Jørgensen
maintainerRe: [PATCH net-next v7 1/4] net/sched: cake, fq_codel: return the classifier result to the caller
Quoted 8 lines · Jamal Hadi Salim
The Fix:
cake andfq_codelrun an optional external classifier from their enqueue path and then compute the flow hash inside the helper. Have the helper return the filter's overrides (cake) or a 1-based flow index or a request to hash (fq_codel) instead, so the caller owns the flow selection.This is preparation for the accounting changes that follow. Packet classification and flow selection are unchanged.
It's not clear to me why this change is needed at all for fq_codel? fq_codel_hash just does a reciprocal_scale() of the skb hash, it doesn't mutate any qdisc state. So why can't fq_codel_classify not just stay as it is?
Quoted 12 lines
Reviewed-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>--- net/sched/sch_cake.c | 35 +++++++++++++++++++----------- net/sched/sch_fq_codel.c | 47 +++++++++++++++++++++++++++++----------- 2 files changed, 56 insertions(+), 26 deletions(-)net/sched/sch_cake.c +0
-0
net/sched/sch_cake.c static struct cake_tin_data *cake_select_tin(struct Qdisc *sch, · line 1718
Quoted 9 lines
net/sched/sch_cake.c +4
-2@@ -1712,18 +1712,24 @@ static struct cake_tin_data *cake_select_tin(struct Qdisc *sch,return &qd->tins[tin]; } -static u32 cake_classify (struct Qdisc *sch,struct cake_tin_data **t, -struct sk_buff *skb,int flow_mode,int *qerr) +/* Run the qdisc's optional external classifier. Returns true when a +* terminal TC action consumed the packet, with the reason in *qerr; +* otherwise *flow and *host hold the filter's overrides (0 if none). +*/
This comment is unnecessary, and just repeating what the code does. Please drop.
Quoted 165 lines
net/sched/sch_cake.c +18
-11+static bool cake_tcf_classify (struct Qdisc *sch,struct sk_buff *skb, +u16 *flow,u16 *host,int *qerr) {struct cake_sched_data *q =qdisc_priv (sch);struct tcf_proto *filter;struct tcf_result res; -u16 flow =0 , host =0 ;int result; + *flow =0 ; + *host =0 ; + filter =rcu_dereference_bh (q->filter_list);if (!filter) - goto hash; +return false; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; result =tcf_classify_qdisc (skb, filter, &res, false);@@ -1737,17 +1743,15 @@ static u32 cake_classify(struct Qdisc *sch, struct cake_tin_data **t, *qerr =NET_XMIT_SUCCESS | __NET_XMIT_STOLEN; fallthrough;case TC_ACT_SHOT : - return 0; + return true; }#endif if (TC_H_MIN (res.classid) <=CAKE_QUEUES ) - flow =TC_H_MIN (res.classid); + *flow =TC_H_MIN (res.classid);if (TC_H_MAJ (res.classid) <= (CAKE_QUEUES <<16 )) - host =TC_H_MAJ (res.classid) >>16 ; + *host =TC_H_MAJ (res.classid) >>16 ; } -hash: - *t =cake_select_tin (sch, skb); -return cake_hash (*t, skb, flow_mode, flow, host) +1 ; +return false; }static void cake_reconfigure (struct Qdisc *sch);@@ -1758,20 +1762,25 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,u32 idx, tin, prev_qlen, prev_backlog, drop_id;struct cake_sched_data *q =qdisc_priv (sch);int len =qdisc_pkt_len (skb), ret; +u16 flow_override, host_override;struct sk_buff *ack =NULL ;ktime_t now =ktime_get ();struct cake_tin_data *b;struct cake_flow *flow;bool same_flow = false; -/* choose flow to insert into */ - idx =cake_classify (sch, &b, skb, q->config->flow_mode, &ret); -if (idx ==0 ) { +/* Classify first: a terminal TC action consumes the packet. */ +if (cake_tcf_classify (sch, skb, &flow_override, &host_override, &ret)) {if (ret & __NET_XMIT_BYPASS)qdisc_qstats_drop (sch);__qdisc_drop (skb, to_free);return ret; } + +/* choose flow to insert into */ + b =cake_select_tin (sch, skb); + idx =cake_hash (b, skb, q->config->flow_mode, flow_override, + host_override) +1 ; tin = (u32)(b - q->tins); idx--; flow = &b->flows[idx];net/sched/sch_fq_codel.c +34
-13@@ -73,22 +73,38 @@ static unsigned int fq_codel_hash(const struct fq_codel_sched_data *q,return reciprocal_scale (skb_get_hash (skb), q->flows_cnt); } -static unsigned int fq_codel_classify (struct sk_buff *skb,struct Qdisc *sch, -int *qerr) +/* Run the qdisc's optional external classifier. Returns true when the +* caller must drop the packet, with the reason in *qerr: a terminal TC +* action consumed it, or no valid class in 1..q->flows_cnt was +* selected (no filter matched, the result carried no class ID, or its +* minor was above q->flows_cnt). Unlike cake, a packet with no valid +* class is dropped here (a BYPASS drop) rather than falling back to +* hashing. Otherwise *idx is the 1-based flow index, or *do_hash asks +* the caller to use fq_codel_hash(). +*/ +static bool fq_codel_classify (struct sk_buff *skb,struct Qdisc *sch, +unsigned int *idx,bool *do_hash,int *qerr) {struct fq_codel_sched_data *q =qdisc_priv (sch);struct tcf_proto *filter;struct tcf_result res;int result; + *idx =0 ; + *do_hash = false; +if (TC_H_MAJ (skb->priority) == sch->handle &&TC_H_MIN (skb->priority) >0 && -TC_H_MIN (skb->priority) <= q->flows_cnt) -return TC_H_MIN (skb->priority); +TC_H_MIN (skb->priority) <= q->flows_cnt) { + *idx =TC_H_MIN (skb->priority); +return false; + } filter =rcu_dereference_bh (q->filter_list); -if (!filter) -return fq_codel_hash (q, skb) +1 ; +if (!filter) { + *do_hash = true; +return false; + } *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; result =tcf_classify_qdisc (skb, filter, &res, false);@@ -101,13 +117,16 @@ static unsigned int fq_codel_classify(struct sk_buff *skb, struct Qdisc *sch, *qerr =NET_XMIT_SUCCESS | __NET_XMIT_STOLEN; fallthrough;case TC_ACT_SHOT : - return 0; + return true; }#endif -if (TC_H_MIN (res.classid) <= q->flows_cnt) -return TC_H_MIN (res.classid); +if (TC_H_MIN (res.classid) >0 && +TC_H_MIN (res.classid) <= q->flows_cnt) { + *idx =TC_H_MIN (res.classid); +return false; + } } -return 0 ; +return true; }/* helper functions : might be changed when/if skb use a standard list_head */ @@ -188,17 +207,19 @@ static int fq_codel_enqueue(struct sk_buff *skb, struct Qdisc *sch,struct fq_codel_sched_data *q =qdisc_priv (sch);unsigned int idx, prev_backlog, prev_qlen;struct fq_codel_flow *flow; -int ret; +bool do_hash = false;unsigned int pkt_len;bool memory_limited; +int ret; - idx =fq_codel_classify (skb, sch, &ret); -if (idx ==0 ) { +if (fq_codel_classify (skb, sch, &idx, &do_hash, &ret)) {if (ret & __NET_XMIT_BYPASS)qdisc_qstats_drop (sch);__qdisc_drop (skb, to_free);return ret; } +if (do_hash) + idx =fq_codel_hash (q, skb) +1 ; idx--;codel_set_enqueue_time (skb);Signature · 1 line
2.43.0
Jamal Hadi Salim
maintainerRe: [PATCH net-next v7 1/4] net/sched: cake, fq_codel: return the classifier result to the caller
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 6 lines · Jamal Hadi Salim
The Fix:
cake andfq_codelrun an optional external classifier from their enqueue path and then compute the flow hash inside the helper. Have the helper return the filter's overrides (cake) or a 1-based flow index or a request to hash (fq_codel) instead, so the caller owns the flow selection.
Quoted 7 lines
Quoted 2 lines
This is preparation for the accounting changes that follow. Packet classification and flow selection are unchanged.
It's not clear to me why this change is needed at all for
fq_codel?fq_codel_hashjust does areciprocal_scale()of the skb hash, it doesn't mutate any qdisc state. So why can'tfq_codel_classifynot just stay as it is?
It is not functionaly needed for fq_codel, it is more because these two are cousins and simplifies subsequent patches.
Note in patch 4 we force the same semantics on cake as in fq_codel (classifier decides the drop, the caller owns flow selection). Keeping fq_codel's classify the same makes patch 3 hunks in the two files similar. There is another reason: the old helper encoded "drop" as a magic return and carried the 1-based +1 arithmetic on every return path.
In any case, unless you feel strongly i would rather keep it.
cheers,
jamal
Toke Høiland-Jørgensen
maintainerRe: [PATCH net-next v7 1/4] net/sched: cake, fq_codel: return the classifier result to the caller
Quoted 18 lines · Jamal Hadi Salim
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 6 lines · Jamal Hadi Salim
The Fix:
cake andfq_codelrun an optional external classifier from their enqueue path and then compute the flow hash inside the helper. Have the helper return the filter's overrides (cake) or a 1-based flow index or a request to hash (fq_codel) instead, so the caller owns the flow selection.Quoted 7 lines
Quoted 2 lines
This is preparation for the accounting changes that follow. Packet classification and flow selection are unchanged.
It's not clear to me why this change is needed at all for
fq_codel?fq_codel_hashjust does areciprocal_scale()of the skb hash, it doesn't mutate any qdisc state. So why can'tfq_codel_classifynot just stay as it is?
Quoted 8 lines
It is not functionaly needed for
fq_codel, it is more because these two are cousins and simplifies subsequent patches.
Note in patch 4 we force the same semantics on cake as infq_codel(classifier decides the drop, the caller owns flow selection). Keeping fq_codel's classify the same makes patch 3 hunks in the two files similar. There is another reason: the old helper encoded "drop" as a magic return and carried the 1-based +1 arithmetic on every return path.
But you're not making the same change to fq_pie, although its classify function is line-for-line the same as fq_codel. I think it's better to keep those two the same; you can add the new check to fq_codel between the 'idx--' and 'codel_set_enqueue_time()' lines just fine.
Quoted 1 line
In any case, unless you feel strongly i would rather keep it.
Well, see above :)
-Toke
Jamal Hadi Salim
maintainerRe: [PATCH net-next v7 1/4] net/sched: cake, fq_codel: return the classifier result to the caller
Quoted 30 lines · Toke Høiland-Jørgensen
Quoted 18 lines · Jamal Hadi Salim
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 6 lines · Jamal Hadi Salim
The Fix:
cake andfq_codelrun an optional external classifier from their enqueue path and then compute the flow hash inside the helper. Have the helper return the filter's overrides (cake) or a 1-based flow index or a request to hash (fq_codel) instead, so the caller owns the flow selection.Quoted 7 lines
Quoted 2 lines
This is preparation for the accounting changes that follow. Packet classification and flow selection are unchanged.
It's not clear to me why this change is needed at all for
fq_codel?fq_codel_hashjust does areciprocal_scale()of the skb hash, it doesn't mutate any qdisc state. So why can'tfq_codel_classifynot just stay as it is?Quoted 8 lines
It is not functionaly needed for
fq_codel, it is more because these two are cousins and simplifies subsequent patches.
Note in patch 4 we force the same semantics on cake as infq_codel(classifier decides the drop, the caller owns flow selection). Keeping fq_codel's classify the same makes patch 3 hunks in the two files similar. There is another reason: the old helper encoded "drop" as a magic return and carried the 1-based +1 arithmetic on every return path.
Quoted 8 lines
But you're not making the same change to
fq_pie, although its classify function is line-for-line the same asfq_codel. I think it's better to keep those two the same; you can add the new check tofq_codelbetween the 'idx--' and 'codel_set_enqueue_time()' lines just fine.Quoted 1 line
In any case, unless you feel strongly i would rather keep it.
Well, see above :)
Ok, i will drop it ;->
cheers,
jamal
Quoted 1 line
-Toke
[email protected]
AI reviewRe: [PATCH net-next v7 1/4] net/sched: cake, fq_codel: return the classifier result to the caller
In reply to Jamal Hadi SalimThank 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…
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 body opens with the heading "The Fix:", but the patch fixes nothing.
--
net/sched: cake, fq_codel: return the classifier result to the caller
This commit changes the external classifier helpers in cake and fq_codel so they no longer compute the flow hash. The cake helper now returns the filter's flow/host overrides, and the fq_codel helper returns a 1-based flow index or a request to hash. The enqueue callers then do the flow selection themselves, in preparation for later accounting changes.
Quoted 3 lines
The Fix:
cake andfq_codelrun an optional external classifier from their enqueue path and then compute the flow hash inside the helper.
[Severity: Low]
This isn't a bug, but is the "The Fix:" heading accurate here? Later in the same message it says:
Quoted 4 lines
This is preparation for the accounting changes that follow. Packet classification and flow selection are unchanged.
From the diff, behaviour looks the same as before.
In cake, cake_tcf_classify() returns true for exactly the actions on which the old cake_classify() returned 0 (SHOT, STOLEN, QUEUED, TRAP). cake_enqueue() then calls cake_select_tin() and cake_hash() with the same overrides that were used before.
In fq_codel, the old fq_codel_classify() returned 0 (drop) in three cases:
a terminal action, no filter match, and TC_H_MIN(res.classid) == 0. The new version returns true in those same three cases. When it does not drop, it hands back the same index or hash request as before.
Since this is a refactor and not a fix, could the heading be dropped or reworded, for example to "Preparation:"? As written, backporters and stable selection tooling might take it for a bug fix.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/QDISC-BA27.v7.r0.20261019115505%40mojatatu.com
Jamal Hadi Salim
maintainerPATCH v7 2/4 net/sched: cake: use a dedicated split-GSO list iterator
In reply to Jamal Hadi Salimskb_list_walk_safe(segs, segs, nskb) reuses the list head as the walk cursor, so segs no longer names the segment list once the walk finishes. Use a separate seg cursor to keep the head and the walker distinct. No behaviour change.
--- net/sched/sch_cake.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
net/sched/sch_cake.c +11 -11
@@ -1810,7 +1810,7 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,WRITE_ONCE (b->max_skblen, len);if (qdisc_pkt_segs (skb) >1 && q->config->rate_flags &CAKE_FLAG_SPLIT_GSO ) { -struct sk_buff *segs, *nskb; +struct sk_buff *segs, *nskb, *seg;netdev_features_t features =netif_skb_features (skb);unsigned int slen =0 , numsegs =0 ;
@@ -1818,19 +1818,19 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,if (IS_ERR_OR_NULL (segs))return qdisc_drop (skb, sch, to_free); - skb_list_walk_safe(segs, segs, nskb) { -skb_mark_not_on_list (segs); -qdisc_skb_cb (segs)->pkt_len = segs->len; -qdisc_skb_cb (segs)->pkt_segs =1 ; -cobalt_set_enqueue_time (segs, now); -get_cobalt_cb (segs)->adjusted_len =cake_overhead (q, - segs); -flow_queue_add (flow, segs); + skb_list_walk_safe(segs, seg, nskb) { +skb_mark_not_on_list (seg); +qdisc_skb_cb (seg)->pkt_len = seg->len; +qdisc_skb_cb (seg)->pkt_segs =1 ; +cobalt_set_enqueue_time (seg, now); +get_cobalt_cb (seg)->adjusted_len =cake_overhead (q, + seg); +flow_queue_add (flow, seg);qdisc_qlen_inc (sch); numsegs++; - slen += segs->len; - q->buffer_used += segs->truesize; + q->buffer_used += seg->truesize; + slen += seg->len;WRITE_ONCE (b->packets, b->packets +1 ); }
Signature · 1 line
2.43.0
Re: [PATCH net-next v7 2/4] net/sched: cake: use a dedicated split-GSO list iterator
Quoted 7 lines · Jamal Hadi Salim
skb_list_walk_safe(segs, segs, nskb)reuses the list head as the walk cursor, so segs no longer names the segment list once the walk finishes. Use a separate seg cursor to keep the head and the walker distinct. No behaviour change.Reviewed-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>
Jamal Hadi Salim
maintainerPATCH v7 3/4 net/sched: cap the accounted backlog before it can wrap
In reply to Jamal Hadi SalimThis is a followup of commit 709f34f7c28d ("net/sched: fq: add overflow bounds to quantum and initial quantum") as reported by Sashiko.
The issue: fq_codel, cake, codel, pie, fq_pie, dualpi2, RED, pfifo, plug and (non-WRED) gred account every enqueued packet's stab-adjusted length into the 32-bit sch->qstats.backlog, which the AQM logic reads back (for non-WRED gred it is the reported stats and the dequeue-side subtractions that go wrong instead). A crafted tc size table inflates qdisc_pkt_len() up to QDISC_PKT_LEN_MAX (1 MiB), and a dodgy GSO skb can account even more through qdisc_pkt_len_segs_init(), so a few thousand packets wrap the counter mod 2^32. The AQM then reads a small backlog and makes the wrong drop decision, and the dequeue-side subtractions leave it corrupt. In fq_codel a wrapped per-flow backlog can also make fq_codel_drop()'s scan pick an empty flow, dereferencing a NULL flow->head.
The Fix: drop at enqueue once the stored backlog cannot represent the incoming packet's accounted length. Ordinary packets (qdisc_pkt_len() at most QDISC_PKT_LEN_MAX) are covered by a standing ceiling: drop once the backlog is within QDISC_MAX_BACKLOG (U32_MAX - QDISC_PKT_LEN_MAX) of the wrap point, the largest backlog one more 1 MiB packet cannot wrap, so there is no per-enqueue 64-bit add. A larger wire estimate is checked against the space that actually remains (len > U32_MAX - backlog):qdisc_pkt_len_segs_init() adds the headers replicated on every output segment, so a supported small-MSS BIG TCP aggregate can carry a wire estimate above 1 MiB while its raw length stays within GSO_MAX_SIZE, and such traffic must pass on an empty queue. qdisc_pkt_len() itself stays truthful for every input: it is not clamped and the initializer keeps succeeding, so BPF __sk_buff.wire_len, act_police and the HTB/TBF shapers read the real length, and sch_handle_ingress() -- which ignores the initializer's return value and reads pkt_segs for packet statistics -- keeps complete metadata. Cover sch_plug and non-WRED sch_gred too: plug admits on skb->len while qdisc_enqueue_tail() adds qdisc_pkt_len(), and gred bounds each virtual queue but adds every VQ into the one aggregate. pfifo is included because RED reads a grafted child's backlog into red_calc_qavg(); it can be reached standalone or as the default child of SFB. In RED, account the packet to the aggregate before handing it to the child, because a duplicating netem child re-enqueues its clone at the root while the stored backlog is still un-updated -- one guard check must not authorize two additions.
The guard bounds each qdisc's own enqueue path: a child that segments a GSO skb (tbf, netem) can still grow a parent's aggregate through qdisc_tree_reduce_backlog()'s unclamped byte delta, which is left to a separate change.
Conditions to recreate the bug:
ip tuntap add tun0 mode tun
ip link set tun0 txqueuelen 32 up
ip addr add 10.99.0.1/24 dev tun0
tc qdisc add dev tun0 root handle 1: stab overhead 2000000000 \
fq_codel flows 1 limit 20000 ecn
# hold the tun fd open without reading so the backlog persists, then # send 6000 packets: the counter wraps and the unfixed kernel reports # a wrapped backlog, while the fixed one drops at the ceiling.
---
v6 -> v7: qdisc_pkt_len_segs_init() no longer clamps the shared qdisc_pkt_len(); it stays truthful for every input, including the wire estimates above QDISC_PKT_LEN_MAX that a small-MSS BIG TCP aggregate legitimately carries. The overflow protection is enqueue admission
instead: each affected qdisc drops a packet once the stored backlog cannot represent its accounted length -- the standing QDISC_MAX_BACKLOG ceiling covers ordinary packets, and a per-packet representability check covers the larger wire estimates -- so BPF __sk_buff.wire_len, act_police and the HTB/TBF shapers read the real length and the ingress path keeps complete pkt_segs metadata. (Sashiko nipa v6 review, main-2-6)
--- include/net/pkt_sched.h | 1 - include/net/sch_generic.h | 20 ++++++++++++++++++++ net/sched/sch_cake.c | 7 +++++++ net/sched/sch_codel.c | 16 +++++++++------- net/sched/sch_dualpi2.c | 1 + net/sched/sch_fifo.c | 6 ++++++ net/sched/sch_fq_codel.c | 8 ++++++++ net/sched/sch_fq_pie.c | 3 ++- net/sched/sch_gred.c | 9 +++++++++ net/sched/sch_pie.c | 3 ++- net/sched/sch_plug.c | 9 ++++++++- net/sched/sch_red.c | 23 ++++++++++++++++++----- 12 files changed, 90 insertions(+), 16 deletions(-)
include/net/pkt_sched.h +0 -1
@@ -12,7 +12,6 @@#define DEFAULT_TX_QUEUE_LEN 1000#define STAB_SIZE_LOG_MAX 30 -#define QDISC_PKT_LEN_MAX (1 << 20)/* 1 MiB */ struct qdisc_walker {int stop;
include/net/sch_generic.h +20 -0
@@ -915,6 +915,26 @@ static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)return qdisc_skb_cb (skb)->pkt_len; } +#define QDISC_PKT_LEN_MAX (1 << 20)/* 1 MiB */ + +/* Largest backlog that still has room for one more 1 MiB packet. */ +#define QDISC_MAX_BACKLOG (U32_MAX - QDISC_PKT_LEN_MAX) + +/* Would this packet's accounted length wrap sch->qstats.backlog? +* Packets up to QDISC_PKT_LEN_MAX fit below QDISC_MAX_BACKLOG; GSO +* can make the length larger, so compare against the real headroom. +*/ +static inline bool qdisc_backlog_overflow (const struct Qdisc *sch, +const struct sk_buff *skb) +{ +u32 backlog =READ_ONCE (sch->qstats.backlog); +u32 len =qdisc_pkt_len (skb); + +if (likely (len <=QDISC_PKT_LEN_MAX )) +return backlog >QDISC_MAX_BACKLOG ; +return len >U32_MAX - backlog; +} +static inline unsigned int qdisc_pkt_segs (const struct sk_buff *skb) {u32 pkt_segs =qdisc_skb_cb (skb)->pkt_segs;
net/sched/sch_cake.c +7 -0
@@ -1777,6 +1777,13 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,return ret; } +/* Drop before touching the hash or shaper state. */ +if (unlikely (qdisc_backlog_overflow (sch, skb))) { +qdisc_qstats_overlimit (sch); +return qdisc_drop_reason (skb, sch, to_free, +QDISC_DROP_OVERLIMIT ); + } +/* choose flow to insert into */ b =cake_select_tin (sch, skb); idx = cake_hash(b, skb, q->config->flow_mode, flow_override,
net/sched/sch_codel.c +9 -7
@@ -116,15 +116,17 @@ static struct sk_buff *codel_peek(struct Qdisc *sch)static int codel_qdisc_enqueue (struct sk_buff *skb,struct Qdisc *sch,struct sk_buff **to_free) { -struct codel_sched_data *q; +struct codel_sched_data *q =qdisc_priv (sch); -if (likely (qdisc_qlen (sch) < sch->limit)) { -codel_set_enqueue_time (skb); -return qdisc_enqueue_tail (skb, sch); +if (unlikely (qdisc_qlen (sch) >= sch->limit) || +unlikely (qdisc_backlog_overflow (sch, skb))) { +WRITE_ONCE (q->drop_overlimit, q->drop_overlimit +1 ); +return qdisc_drop_reason (skb, sch, to_free, +QDISC_DROP_OVERLIMIT ); } - q =qdisc_priv (sch); -WRITE_ONCE (q->drop_overlimit, q->drop_overlimit +1 ); -return qdisc_drop_reason (skb, sch, to_free,QDISC_DROP_OVERLIMIT ); + +codel_set_enqueue_time (skb); +return qdisc_enqueue_tail (skb, sch); }static const struct nla_policy codel_policy[TCA_CODEL_MAX +1 ] = {
net/sched/sch_dualpi2.c +1 -0
@@ -392,6 +392,7 @@ static int dualpi2_enqueue_skb(struct sk_buff *skb, struct Qdisc *sch,struct dualpi2_skb_cb *cb;if (unlikely (qdisc_qlen (sch) >= sch->limit) || +unlikely (qdisc_backlog_overflow (sch, skb)) ||unlikely ((u64 )q->memory_used + skb->truesize > q->memory_limit)) {qdisc_qstats_overlimit (sch);if (skb_in_l_queue (skb))
net/sched/sch_fifo.c +6 -0
@@ -29,6 +29,9 @@ static int bfifo_enqueue(struct sk_buff *skb, struct Qdisc *sch,static int pfifo_enqueue (struct sk_buff *skb,struct Qdisc *sch,struct sk_buff **to_free) { +if (unlikely (qdisc_backlog_overflow (sch, skb))) +return qdisc_drop (skb, sch, to_free); +if (likely (sch->q.qlen <READ_ONCE (sch->limit)))return qdisc_enqueue_tail (skb, sch);
@@ -43,6 +46,9 @@ static int pfifo_tail_enqueue(struct sk_buff *skb, struct Qdisc *sch,if (unlikely (READ_ONCE (sch->limit) ==0 ))return qdisc_drop (skb, sch, to_free); +if (unlikely (qdisc_backlog_overflow (sch, skb))) +return qdisc_drop (skb, sch, to_free); +if (likely (sch->q.qlen <READ_ONCE (sch->limit)))return qdisc_enqueue_tail (skb, sch);
net/sched/sch_fq_codel.c +8 -0
@@ -218,6 +218,14 @@ static int fq_codel_enqueue(struct sk_buff *skb, struct Qdisc *sch,__qdisc_drop (skb, to_free);return ret; } + +/* Drop before hashing so a dropped packet cannot create a flow. */ +if (unlikely (qdisc_backlog_overflow (sch, skb))) { + q->drop_overlimit++; +return qdisc_drop_reason (skb, sch, to_free, +QDISC_DROP_OVERLIMIT ); + } +if (do_hash) idx =fq_codel_hash (q, skb) +1 ; idx--;
net/sched/sch_fq_pie.c +2 -1
@@ -155,7 +155,8 @@ static int fq_pie_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch, memory_limited = q->memory_usage > q->memory_limit + skb->truesize;/* Checks if the qdisc is full */ -if (unlikely (qdisc_qlen (sch) >= sch->limit)) { +if (unlikely (qdisc_qlen (sch) >= sch->limit) || +unlikely (qdisc_backlog_overflow (sch, skb))) { q->stats.overlimit++; goto out; }else if (unlikely (memory_limited)) {
net/sched/sch_gred.c +9 -0
@@ -170,6 +170,15 @@ static int gred_enqueue(struct sk_buff *skb, struct Qdisc *sch,unsigned long qavg =0 ;u16 dp =tc_index_to_dp (skb); +/* Per-VQ limits don't bound the total: every VQ adds into the +* same u32 backlog, so check it before picking a VQ. +*/ +if (unlikely (qdisc_backlog_overflow (sch, skb))) { +qdisc_qstats_overlimit (sch); +return qdisc_drop_reason (skb, sch, to_free, +QDISC_DROP_OVERLIMIT ); + } +if (dp >= t->DPs || (q = t->tab[dp]) ==NULL ) { dp = t->def;
net/sched/sch_pie.c +2 -1
@@ -89,7 +89,8 @@ static int pie_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,struct pie_sched_data *q =qdisc_priv (sch);bool enqueue = false; -if (unlikely (qdisc_qlen (sch) >= sch->limit)) { +if (unlikely (qdisc_qlen (sch) >= sch->limit) || +unlikely (qdisc_backlog_overflow (sch, skb))) {WRITE_ONCE (q->stats.overlimit, q->stats.overlimit +1 ); goto out; }
net/sched/sch_plug.c +8 -1
@@ -89,7 +89,14 @@ static int plug_enqueue(struct sk_buff *skb, struct Qdisc *sch, {struct plug_sched_data *q =qdisc_priv (sch); -if (likely ((u64 )sch->qstats.backlog + skb->len <= q->limit)) { +if (unlikely (qdisc_backlog_overflow (sch, skb))) { +qdisc_qstats_overlimit (sch); +return qdisc_drop_reason (skb, sch, to_free, +QDISC_DROP_OVERLIMIT ); + } + +if (likely ((u64 )sch->qstats.backlog +qdisc_pkt_len (skb) <= + q->limit)) {if (!q->unplug_indefinite) q->pkts_current_epoch++;return qdisc_enqueue_tail (skb, sch);
net/sched/sch_red.c +18 -5
@@ -76,6 +76,12 @@ static int red_enqueue(struct sk_buff *skb, struct Qdisc *sch,unsigned int len;int ret; +if (unlikely (qdisc_backlog_overflow (sch, skb))) { +qdisc_qstats_overlimit (sch); +return qdisc_drop_reason (skb, sch, to_free, +QDISC_DROP_OVERLIMIT ); + } + q->vars.qavg =red_calc_qavg (&q->parms, &q->vars, child->qstats.backlog);
@@ -136,14 +142,21 @@ static int red_enqueue(struct sk_buff *skb, struct Qdisc *sch, } len =qdisc_pkt_len (skb); +/* Reserve the length before the child sees the packet: netem +* can re-enqueue a clone here before the backlog is updated. +* Undo it if the child drops the packet. +*/ +qstats_backlog_add (sch, len); ret =qdisc_enqueue (skb, child, to_free);if (likely (ret ==NET_XMIT_SUCCESS )) { -qstats_backlog_add (sch, len);qdisc_qlen_inc (sch); - }else if (net_xmit_drop_count (ret)) { -WRITE_ONCE (q->stats.pdrop, - q->stats.pdrop +1 ); -qdisc_qstats_drop (sch); + }else { +qstats_backlog_sub (sch, len); +if (net_xmit_drop_count (ret)) { +WRITE_ONCE (q->stats.pdrop, + q->stats.pdrop +1 ); +qdisc_qstats_drop (sch); + } }return ret;
Signature · 1 line
2.43.0
Quoted 2 lines · Jamal Hadi Salim
This is a followup of commit
709f34f7c28d("net/sched: fq: add overflow bounds to quantum and initial quantum") as reported by Sashiko.
The rest of this commit message is somewhat obtuse - too much run-on information. I think it can be pretty easily fixed by a couple of rearrangements and paragraph breaks, like this:
Quoted 62 lines
The issue:
fq_codel, cake, codel, pie,fq_pie, dualpi2, RED, pfifo, plug and (non-WRED) gred account every enqueued packet's stab-adjusted length into the 32-bitsch->qstats.backlog. A crafted tc size table inflatesqdisc_pkt_len()up toQDISC_PKT_LEN_MAX(1 MiB), and a dodgy GSO skb can account even more throughqdisc_pkt_len_segs_init(), so a few thousand packets wrap the counter mod 2^32.The AQM then reads a small backlog and makes the wrong drop decision, and the dequeue-side subtractions leave it corrupt (for non-WRED gred it is the reported stats and the dequeue-side subtractions that go wrong instead). In
fq_codela wrapped per-flow backlog can also make fq_codel_drop()'s scan pick an empty flow, dereferencing a NULLflow->head.The Fix: drop at enqueue once the stored backlog cannot represent the incoming packet's accounted length.
Ordinary packets (
qdisc_pkt_len()at mostQDISC_PKT_LEN_MAX) are covered by a standing ceiling: drop once the backlog is withinQDISC_MAX_BACKLOG(U32_MAX-QDISC_PKT_LEN_MAX) of the wrap point, the largest backlog one more 1 MiB packet cannot wrap, so there is no per-enqueue 64-bit add.A larger wire estimate is checked against the space that actually remains (len >
U32_MAX- backlog):qdisc_pkt_len_segs_init()adds the headers replicated on every output segment, so a supported small-MSS BIG TCP aggregate can carry a wire estimate above 1 MiB while its raw length stays withinGSO_MAX_SIZE, and such traffic must pass on an empty queue.
qdisc_pkt_len()itself stays truthful for every input: it is not clamped and the initializer keeps succeeding, so BPF __sk_buff.wire_len,act_policeand the HTB/TBF shapers read the real length, andsch_handle_ingress()-- which ignores the initializer's return value and readspkt_segsfor packet statistics -- keeps complete metadata.Cover
sch_plugand non-WREDsch_gredtoo: plug admits onskb->lenwhileqdisc_enqueue_tail()addsqdisc_pkt_len(), and gred bounds each virtual queue but adds every VQ into the one aggregate. pfifo is included because RED reads a grafted child's backlog intored_calc_qavg(); it can be reached standalone or as the default child of SFB. In RED, account the packet to the aggregate before handing it to the child, because a duplicating netem child re-enqueues its clone at the root while the stored backlog is still un-updated -- one guard check must not authorize two additions.The guard bounds each qdisc's own enqueue path: a child that segments a GSO skb (tbf, netem) can still grow a parent's aggregate through qdisc_tree_reduce_backlog()'s unclamped byte delta, which is left to a separate change.
Conditions to recreate the bug:
ip tuntap add tun0 mode tun
ip link set tun0 txqueuelen 32 up
ip addr add 10.99.0.1/24 dev tun0
tc qdisc add dev tun0 root handle 1: stab overhead 2000000000 \
fq_codelflows 1 limit 20000 ecn# hold the tun fd open without reading so the backlog persists, then # send 6000 packets: the counter wraps and the unfixed kernel reports # a wrapped backlog, while the fixed one drops at the ceiling.
Quoted 10 lines
Reported-by Sashiko (nipa) <[email protected]>Link https://lore.kernel.org/netdev/CANn89iLfMJV7ancKH1Gjzzm7ZUG-gKcrczkJjJEWN5sCuWd-ug@mail.gmail.com/ open hereLink https://lore.kernel.org/netdev/CANn89i+GOFH_8g+vSV0jUj0aBqLYVWuFOrHuDggZu9kPC_m6tQ@mail.gmail.com/ open hereReviewed-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>
With the above rewording:
Jamal Hadi Salim
maintainerQuoted 10 lines · Toke Høiland-Jørgensen
Quoted 2 lines · Jamal Hadi Salim
This is a followup of commit
709f34f7c28d("net/sched: fq: add overflow bounds to quantum and initial quantum") as reported by Sashiko.The rest of this commit message is somewhat obtuse - too much run-on information. I think it can be pretty easily fixed by a couple of rearrangements and paragraph breaks, like this:
Sigh - i thought i took care of this (iirc, Stephen preferred the shorter form as well)
Thanks for the reviewed by.
I guess i have to send a v8.
cheers,
jamal
Quoted 76 lines
Quoted 62 lines
The issue:
fq_codel, cake, codel, pie,fq_pie, dualpi2, RED, pfifo, plug and (non-WRED) gred account every enqueued packet's stab-adjusted length into the 32-bitsch->qstats.backlog. A crafted tc size table inflatesqdisc_pkt_len()up toQDISC_PKT_LEN_MAX(1 MiB), and a dodgy GSO skb can account even more throughqdisc_pkt_len_segs_init(), so a few thousand packets wrap the counter mod 2^32.The AQM then reads a small backlog and makes the wrong drop decision, and the dequeue-side subtractions leave it corrupt (for non-WRED gred it is the reported stats and the dequeue-side subtractions that go wrong instead). In
fq_codela wrapped per-flow backlog can also make fq_codel_drop()'s scan pick an empty flow, dereferencing a NULLflow->head.The Fix: drop at enqueue once the stored backlog cannot represent the incoming packet's accounted length.
Ordinary packets (
qdisc_pkt_len()at mostQDISC_PKT_LEN_MAX) are covered by a standing ceiling: drop once the backlog is withinQDISC_MAX_BACKLOG(U32_MAX-QDISC_PKT_LEN_MAX) of the wrap point, the largest backlog one more 1 MiB packet cannot wrap, so there is no per-enqueue 64-bit add.A larger wire estimate is checked against the space that actually remains (len >
U32_MAX- backlog):qdisc_pkt_len_segs_init()adds the headers replicated on every output segment, so a supported small-MSS BIG TCP aggregate can carry a wire estimate above 1 MiB while its raw length stays withinGSO_MAX_SIZE, and such traffic must pass on an empty queue.
qdisc_pkt_len()itself stays truthful for every input: it is not clamped and the initializer keeps succeeding, so BPF __sk_buff.wire_len,act_policeand the HTB/TBF shapers read the real length, andsch_handle_ingress()-- which ignores the initializer's return value and readspkt_segsfor packet statistics -- keeps complete metadata.Cover
sch_plugand non-WREDsch_gredtoo: plug admits onskb->lenwhileqdisc_enqueue_tail()addsqdisc_pkt_len(), and gred bounds each virtual queue but adds every VQ into the one aggregate. pfifo is included because RED reads a grafted child's backlog intored_calc_qavg(); it can be reached standalone or as the default child of SFB. In RED, account the packet to the aggregate before handing it to the child, because a duplicating netem child re-enqueues its clone at the root while the stored backlog is still un-updated -- one guard check must not authorize two additions.The guard bounds each qdisc's own enqueue path: a child that segments a GSO skb (tbf, netem) can still grow a parent's aggregate through qdisc_tree_reduce_backlog()'s unclamped byte delta, which is left to a separate change.
Conditions to recreate the bug:
ip tuntap add tun0 mode tun
ip link set tun0 txqueuelen 32 up
ip addr add 10.99.0.1/24 dev tun0
tc qdisc add dev tun0 root handle 1: stab overhead 2000000000 \
fq_codelflows 1 limit 20000 ecn# hold the tun fd open without reading so the backlog persists, then # send 6000 packets: the counter wraps and the unfixed kernel reports # a wrapped backlog, while the fixed one drops at the ceiling.Quoted 10 lines
Reported-by Sashiko (nipa) <[email protected]>Link https://lore.kernel.org/netdev/CANn89iLfMJV7ancKH1Gjzzm7ZUG-gKcrczkJjJEWN5sCuWd-ug@mail.gmail.com/ open hereLink https://lore.kernel.org/netdev/CANn89i+GOFH_8g+vSV0jUj0aBqLYVWuFOrHuDggZu9kPC_m6tQ@mail.gmail.com/ open hereReviewed-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>With the above rewording:
Reviewed-by Toke Høiland-Jørgensen <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 (1) · Low: 1
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 this patch, cake_enqueue() (net/sched/sch_cake.c) runs the new qdisc_backlog_overflow() check on the original skb, using its…
- [Medium] net/sched: red_enqueue() fails to bound child backlog on NET_XMIT_CN
- [Low] The commit "net/sched: cap the accounted backlog before it can wrap" fixes a user-triggerable bug in about ten qdiscs: u32…
Pre-existing issues:
- [Medium] red_enqueue() (net/sched/sch_red.c:79) checks RED's own backlog once and reserves only the original qdisc_pkt_len() L (line…
--
net/sched: cap the accounted backlog before it can wrap
This patch adds qdisc_backlog_overflow() and calls it at enqueue in fq_codel, cake, codel, pie, fq_pie, dualpi2, RED, pfifo, plug and gred. These qdiscs now drop a packet once the u32 sch->qstats.backlog can no longer represent its accounted length. RED also reserves the packet length in its aggregate backlog before handing the packet to its child.
Quoted 2 lines
[Severity: Low]
Should this have a Fixes: tag?
The commit message describes a backlog wrap that users can trigger in about ten qdiscs, including a NULL flow->head dereference in fq_codel_drop(). The trailers only carry Reported-by, Closes and Link.
The commit this patch follows up on, 709f34f7c28d ("net/sched: fq: add overflow bounds to quantum and initial quantum"), carries "Fixes: afe4fd062416". Without a Fixes: tag here, it isn't clear which stable releases need this change.
Quoted 5 lines
include/net/sch_generic.h +0
-0@@ -915,6 +915,26 @@ static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
[ ... ]
Quoted 10 lines
include/net/sch_generic.h +10
-0+static inline bool qdisc_backlog_overflow (const struct Qdisc *sch, +const struct sk_buff *skb) +{ +u32 backlog =READ_ONCE (sch->qstats.backlog); +u32 len =qdisc_pkt_len (skb); + +if (likely (len <=QDISC_PKT_LEN_MAX )) +return backlog >QDISC_MAX_BACKLOG ; +return len >U32_MAX - backlog; +}
[ ... ]
Quoted 5 lines
net/sched/sch_cake.c +0
-0
net/sched/sch_cake.c static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 1786
Quoted 10 lines
net/sched/sch_cake.c +7
-0@@ -1777,6 +1777,13 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,return ret; } +/* Drop before touching the hash or shaper state. */ +if (unlikely (qdisc_backlog_overflow (sch, skb))) { +qdisc_qstats_overlimit (sch); +return qdisc_drop_reason (skb, sch, to_free, +QDISC_DROP_OVERLIMIT ); + } +
[Severity: Medium]
On NET_XMIT_CN, red_enqueue() now undoes the reservation with qstats_backlog_sub(sch, len). That leaves RED's backlog where the old code left it. Both versions assume the child has already told its ancestors about anything it dropped. Does that hold for every qdisc red_graft() accepts?
hhf_enqueue() queues the new skb, then hhf_drop() drops the head of the heaviest bucket. If that is the bucket it just queued to, it returns NET_XMIT_CN without calling qdisc_tree_reduce_backlog():
if (hhf_drop(sch, to_free) == idx) return NET_XMIT_CN;
skbprio_enqueue() does the same when it evicts the tail of a lower priority band. In both cases child->qstats.backlog moves by qdisc_pkt_len(skb) minus the evicted packet's length, while RED's backlog does not move.
Two things follow.
First, the new guard reads RED's own backlog, but red_calc_qavg() reads child->qstats.backlog. Neither hhf nor skbprio checks qdisc_backlog_overflow(). Suppose a stab inflates the arriving packets to 1 MiB while the queued ones stay small. Each eviction of a small packet for a 1 MiB one then grows the child backlog by about 1 MiB and leaves RED's untouched. Can the child backlog wrap mod 2^32 and hand red_calc_qavg() a small value?
Second, red_dequeue() later subtracts the full qdisc_pkt_len() of a packet RED never counted. Once the queue drains, RED's backlog sits at 2^32 minus the net drift. Any drift from 1 byte to 1 MiB leaves it above QDISC_MAX_BACKLOG. Evicting one 64 byte packet to admit a 1500 byte one is enough, and no stab is needed.
Before this patch a wrapped RED backlog mostly affected the reported stats. Now it gates admission. Wouldn't red_enqueue() drop every packet from then on, with nothing left queued to bring the counter back?
The commit message adds pfifo because RED reads a grafted child's backlog. Doesn't the same reasoning apply to hhf and skbprio? Should those children correct their ancestors on NET_XMIT_CN, or should RED check the child's backlog as well as its own?
[Severity: Medium]
Does this check also bound what the CAKE_FLAG_SPLIT_GSO branch adds to the backlog later in cake_enqueue()?
The check uses the original skb's qdisc_pkt_len(). When a size table is attached, that length is recomputed from skb->len and capped:
net/sched/sch_api.c:__qdisc_calculate_pkt_len() {
...
pkt_len = clamp_t(int, pkt_len, 1, QDISC_PKT_LEN_MAX);
qdisc_skb_cb(skb)->pkt_len = pkt_len;
}So qdisc_backlog_overflow() takes the len <= QDISC_PKT_LEN_MAX branch and only guarantees 1 MiB of headroom.
The split path then adds the raw segment sum, and nothing compares that sum against the remaining headroom:
net/sched/sch_cake.c:cake_enqueue() {
...
skb_list_walk_safe(segs, seg, nskb) {
...
slen += seg->len;
...
}
/* stats */
qstats_backlog_add(sch, slen);
...
WRITE_ONCE(b->tin_backlog, b->tin_backlog + slen);
WRITE_ONCE(b->backlogs[idx], b->backlogs[idx] + slen);
...
}Take cake with split_gso and a stab that inflates small packets to 1 MiB. About 4095 queued packets bring the backlog near QDISC_MAX_BACKLOG.
Could one small-MSS GSO skb with slen above the remaining headroom then wrap sch->qstats.backlog, b->tin_backlog and b->backlogs[idx] mod 2^32?
If b->backlogs[idx] happens to wrap to exactly zero, could cake_drop() keep selecting an empty flow from the overflow heap? Nothing is freed in that case:
skb = dequeue_head(flow);
if (unlikely(!skb)) {
/* heap has gone wrong, rebuild it next time */
q->overflow_timeout = 0;
return idx + (tin << 16);
}The buffer_limit drop loop in cake_enqueue() would then make no progress.
The last patch in the series, "net/sched: cake: hash before segmenting and reject an overlong split", sums slen before any segment is linked. It rejects the split when (u64)sch->qstats.backlog + slen > QDISC_MAX_BACKLOG, so this only affects this intermediate commit.
[ ... ]
Quoted 4 lines
net/sched/sch_red.c +0
-0
[ ... ]
net/sched/sch_red.c static int red_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 150
Quoted 10 lines
net/sched/sch_red.c +5
-0@@ -136,14 +142,21 @@ static int red_enqueue(struct sk_buff *skb, struct Qdisc *sch, } len =qdisc_pkt_len (skb); +/* Reserve the length before the child sees the packet: netem +* can re-enqueue a clone here before the backlog is updated. +* Undo it if the child drops the packet. +*/ +qstats_backlog_add (sch, len); ret =qdisc_enqueue (skb, child, to_free);
[Severity: Medium]
This is a pre-existing issue and was not introduced by this patch. The commit message already says this path is left to a separate change.
red_enqueue() runs qdisc_backlog_overflow() once and reserves only the original qdisc_pkt_len(). A child that segments the skb can raise the queued bytes after that point. With corruption enabled, netem re-enqueues each segment with pkt_len = seg->len and then does:
net/sched/sch_netem.c:netem_enqueue() {
...
/* Parent qdiscs accounted for 1 skb of size @prev_len */
qdisc_tree_reduce_backlog(sch, -(nb - 1), -(len - prev_len));
...
}qdisc_tree_reduce_backlog() applies that negative delta to every ancestor and never checks the headroom:
qstats_backlog_sub(sch, len);
Suppose RED's backlog is near QDISC_MAX_BACKLOG, for example because a stab inflates packets to 1 MiB, and a GSO skb's segment sum is above 1 MiB.
Can RED's backlog and the child backlog that red_calc_qavg() reads both still wrap mod 2^32? RED would then base its mark and drop decisions on the wrapped value until the counters drain. tbf_segment() appears to grow the parent backlog the same way.
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/QDISC-BA27.v7.r0.20261019115505%40mojatatu.com
Jamal Hadi Salim
maintainerPATCH v7 4/4 net/sched: cake: hash before segmenting and reject an overlong split
In reply to Jamal Hadi SalimThe Fix:
with CAKE_FLAG_SPLIT_GSO, cake computes the flow/host hash keys, then splits a GSO skb into segments it accounts individually. Key extraction must run on the unsegmented skb -- skb_gso_segment() can rewrite an encapsulated packet's headers to the inner ones -- and the resolution must wait until the packet is admitted, since it commits the set-associative tags, host indices and way counters that a later rejection would leave mutated (a GSO skb whose segmentation fails was dropped with that state already committed).
Split cake_hash() into a side-effect-free cake_hash_keys(), run on the unsegmented skb, and cake_hash_resolve(), run only once the packet is admitted. Move the split-GSO path ahead of the resolution: sum the segment list, test the total against the ceiling before linking any segment, and drop the whole list on rejection so no persistent state is committed. The list is deferred to to_free so the tail segment's sock_wfree() never runs under the qdisc lock; a fraglist GSO, where skb_segment_list() returns the original skb as the head, drops the caller's reference and queues the list once instead of self-linking it.
Tag every deferred segment with QDISC_DROP_OVERLIMIT: __skb_gso_segment() keeps SKB_GSO_CB at cb offset 32, aliasing tc_skb_cb.drop_reason, and __copy_skb_header() copies it into every segment, so tagging only the head left the rest with GSO scratch (a headroom-sized value) as their drop reason, which __tcf_kfree_skb_list() would report as an unrelated core reason.
Read q->config->flow_mode once with READ_ONCE() in cake_enqueue() and pass that value to both cake_hash_keys() and cake_hash_resolve(). cake_config_change() writes the mode before cake_change() takes sch_tree_lock(), and the split introduced two separate loads with the segmentation work between them; a concurrent reconfiguration could extract keys in one mode and resolve them in another.
---
v6 -> v7: the split-GSO reject path now sets QDISC_DROP_OVERLIMIT on every deferred segment, not only on the head/original skb; the GSO cb aliases tc_skb_cb.drop_reason and is copied into every segment, so untagged segments carried GSO scratch as their drop reason. The flow_mode is likewise snapshotted once with READ_ONCE() and passed to both cake_hash_keys() and cake_hash_resolve(). (Sashiko nipa v6 review, gpt-6-astra-2-5, gpt-6-astra-5-9)
--- net/sched/sch_cake.c | 130 ++++++++++++++++++++++++++++++++----------- 1 file changed, 99 insertions(+), 31 deletions(-)
net/sched/sch_cake.c +99 -31
@@ -706,19 +706,28 @@ static u16 cake_get_flow_quantum(struct cake_tin_data *q, get_random_u16()) >> 16; } -static u32 cake_hash (struct cake_tin_data *q,const struct sk_buff *skb, -int flow_mode,u16 flow_override,u16 host_override) +struct cake_hash_keys { +u32 srchost_hash; +u32 dsthost_hash; +u32 flow_hash; +}; + +static void cake_hash_keys (const struct sk_buff *skb,int flow_mode, +u16 flow_override,u16 host_override, +struct cake_hash_keys *out) {bool hash_flows = (!flow_override && !!(flow_mode &CAKE_FLOW_FLOWS ));bool hash_hosts = (!host_override && !!(flow_mode &CAKE_FLOW_HOSTS ));bool nat_enabled = !!(flow_mode &CAKE_FLOW_NAT_FLAG ); -u32 flow_hash =0 , srchost_hash =0 , dsthost_hash =0 ; -u16 reduced_hash, srchost_idx, dsthost_idx;struct flow_keys keys, host_keys;bool use_skbhash = skb->l4_hash; + out->flow_hash =0 ; + out->srchost_hash =0 ; + out->dsthost_hash =0 ; +if (unlikely (flow_mode ==CAKE_FLOW_NONE )) -return 0 ; +return ;/* If both overrides are set, or we can use the SKB hash and nat mode is * disabled, we can skip packet dissection entirely. If nat mode is
@@ -753,50 +762,64 @@ static u32 cake_hash(struct cake_tin_data *q, const struct sk_buff *skb,switch (host_keys.control.addr_type) {case FLOW_DISSECTOR_KEY_IPV4_ADDRS : host_keys.addrs.v4addrs.src =0 ; - dsthost_hash =flow_hash_from_keys (&host_keys); + out->dsthost_hash =flow_hash_from_keys (&host_keys); host_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src; host_keys.addrs.v4addrs.dst =0 ; - srchost_hash =flow_hash_from_keys (&host_keys); + out->srchost_hash =flow_hash_from_keys (&host_keys);break ;case FLOW_DISSECTOR_KEY_IPV6_ADDRS :memset (&host_keys.addrs.v6addrs.src,0 ,sizeof (host_keys.addrs.v6addrs.src)); - dsthost_hash =flow_hash_from_keys (&host_keys); + out->dsthost_hash =flow_hash_from_keys (&host_keys); host_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;memset (&host_keys.addrs.v6addrs.dst,0 ,sizeof (host_keys.addrs.v6addrs.dst)); - srchost_hash =flow_hash_from_keys (&host_keys); + out->srchost_hash =flow_hash_from_keys (&host_keys);break ;default : - dsthost_hash =0 ; - srchost_hash =0 ; + out->dsthost_hash =0 ; + out->srchost_hash =0 ; }/* This *must* be after the above switch, since as a * side-effect it sorts the src and dst addresses. */ if (hash_flows && !use_skbhash) - flow_hash =flow_hash_from_keys (&keys); + out->flow_hash =flow_hash_from_keys (&keys); skip_hash:if (flow_override) - flow_hash = flow_override -1 ; + out->flow_hash = flow_override -1 ;else if (use_skbhash && (flow_mode &CAKE_FLOW_FLOWS )) - flow_hash = skb->hash; + out->flow_hash = skb->hash;if (host_override) { - dsthost_hash = host_override -1 ; - srchost_hash = host_override -1 ; + out->dsthost_hash = host_override -1 ; + out->srchost_hash = host_override -1 ; }if (!(flow_mode &CAKE_FLOW_FLOWS )) {if (flow_mode &CAKE_FLOW_SRC_IP ) - flow_hash ^= srchost_hash; + out->flow_hash ^= out->srchost_hash;if (flow_mode &CAKE_FLOW_DST_IP ) - flow_hash ^= dsthost_hash; + out->flow_hash ^= out->dsthost_hash; } +} + +/* Commit the flow into the set-associative table (q->tags[], host +* indices, bulk counters). Only after the packet is admitted. +*/ +static u32 cake_hash_resolve (struct cake_tin_data *q, +const struct cake_hash_keys *keys,int flow_mode) +{ +u32 flow_hash = keys->flow_hash, srchost_hash = keys->srchost_hash, + dsthost_hash = keys->dsthost_hash; +u16 reduced_hash, srchost_idx, dsthost_idx; + +if (unlikely (flow_mode ==CAKE_FLOW_NONE )) +return 0 ; reduced_hash = flow_hash %CAKE_QUEUES ;
@@ -1761,13 +1784,17 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, {u32 idx, tin, prev_qlen, prev_backlog, drop_id;struct cake_sched_data *q =qdisc_priv (sch); +int flow_mode =READ_ONCE (q->config->flow_mode);int len =qdisc_pkt_len (skb), ret;u16 flow_override, host_override; +struct sk_buff *segs =NULL ; +struct cake_hash_keys keys;struct sk_buff *ack =NULL ;ktime_t now =ktime_get ();struct cake_tin_data *b;struct cake_flow *flow;bool same_flow = false; +u64 slen =0 ;/* Classify first: a terminal TC action consumes the packet. */ if (cake_tcf_classify (sch, skb, &flow_override, &host_override, &ret)) {
@@ -1784,12 +1811,59 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, QDISC_DROP_OVERLIMIT); } -/* choose flow to insert into */ +/* Select the tin and extract the keys before segmenting: +* skb_gso_segment() rewrites encapsulated headers, which +* changes the keys. +*/ b =cake_select_tin (sch, skb); - idx =cake_hash (b, skb, q->config->flow_mode, flow_override, - host_override) +1 ; +cake_hash_keys (skb, flow_mode, flow_override, host_override, &keys); + +if (qdisc_pkt_segs (skb) >1 && q->config->rate_flags &CAKE_FLAG_SPLIT_GSO ) { +struct sk_buff *nskb, *seg; +netdev_features_t features =netif_skb_features (skb); + + segs =skb_gso_segment (skb, features & ~NETIF_F_GSO_MASK ); +if (IS_ERR_OR_NULL (segs)) +return qdisc_drop (skb, sch, to_free); + +/* The split path accounts the segment sum, not the +* stab-adjusted qdisc_pkt_len(), so check it here. +*/ +skb_list_walk_safe (segs, seg, nskb) + slen += seg->len; + +if (unlikely ((u64 )sch->qstats.backlog + slen >QDISC_MAX_BACKLOG )) { +qdisc_qstats_overlimit (sch); +/* __skb_gso_segment() puts SKB_GSO_CB at cb +* offset 32, over tc_skb_cb.drop_reason; every +* segment inherits it. Tag each one with the +* qdisc drop reason. +*/ + skb_list_walk_safe(segs, seg, nskb) +tcf_set_qdisc_drop_reason (seg, +QDISC_DROP_OVERLIMIT ); +/* skb_gso_segment() moved sock_wfree to the tail +* segment, so defer the list to to_free rather +* than free it under the qdisc lock. +* +* Fraglist GSO returns the original skb as head +* (segs == skb), so drop our ref first. +*/ +if (unlikely (segs == skb)) { +consume_skb (skb); +return qdisc_drop_all (segs, sch, to_free); + } +__qdisc_drop_all (segs, to_free); +return qdisc_drop_reason (skb, sch, to_free, +QDISC_DROP_OVERLIMIT ); + } + } + +/* Admitted: resolve the flow. The mode is read once above, so +* extraction and resolution agree if cake_change() intervenes. +*/ + idx =cake_hash_resolve (b, &keys, flow_mode); tin = (u32)(b - q->tins); - idx--; flow = &b->flows[idx];/* ensure shaper state isn't stale */
@@ -1816,14 +1890,9 @@ 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 ) { -struct sk_buff *segs, *nskb, *seg; -netdev_features_t features =netif_skb_features (skb); -unsigned int slen =0 , numsegs =0 ; - - segs =skb_gso_segment (skb, features & ~NETIF_F_GSO_MASK ); -if (IS_ERR_OR_NULL (segs)) -return qdisc_drop (skb, sch, to_free); +if (segs) { +struct sk_buff *nskb, *seg; +unsigned int numsegs =0 ; skb_list_walk_safe(segs, seg, nskb) {skb_mark_not_on_list (seg);
@@ -1837,7 +1906,6 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,qdisc_qlen_inc (sch); numsegs++; q->buffer_used += seg->truesize; - slen += seg->len;WRITE_ONCE (b->packets, b->packets +1 ); }
Signature · 1 line
2.43.0
Toke Høiland-Jørgensen
maintainerRe: [PATCH net-next v7 4/4] net/sched: cake: hash before segmenting and reject an overlong split
Quoted 166 lines · Jamal Hadi Salim
The Fix:
withCAKE_FLAG_SPLIT_GSO, cake computes the flow/host hash keys, then splits a GSO skb into segments it accounts individually. Key extraction must run on the unsegmented skb --skb_gso_segment()can rewrite an encapsulated packet's headers to the inner ones -- and the resolution must wait until the packet is admitted, since it commits the set-associative tags, host indices and way counters that a later rejection would leave mutated (a GSO skb whose segmentation fails was dropped with that state already committed).Split
cake_hash()into a side-effect-freecake_hash_keys(), run on the unsegmented skb, andcake_hash_resolve(), run only once the packet is admitted. Move the split-GSO path ahead of the resolution: sum the segment list, test the total against the ceiling before linking any segment, and drop the whole list on rejection so no persistent state is committed. The list is deferred toto_freeso the tail segment'ssock_wfree()never runs under the qdisc lock; a fraglist GSO, whereskb_segment_list()returns the original skb as the head, drops the caller's reference and queues the list once instead of self-linking it.Tag every deferred segment with
QDISC_DROP_OVERLIMIT:__skb_gso_segment()keepsSKB_GSO_CBat cb offset 32, aliasing tc_skb_cb.drop_reason, and__copy_skb_header()copies it into every segment, so tagging only the head left the rest with GSO scratch (a headroom-sized value) as their drop reason, which__tcf_kfree_skb_list()would report as an unrelated core reason.Read
q->config->flow_modeonce withREAD_ONCE()incake_enqueue()and pass that value to bothcake_hash_keys()andcake_hash_resolve().cake_config_change()writes the mode beforecake_change()takessch_tree_lock(), and the split introduced two separate loads with the segmentation work between them; a concurrent reconfiguration could extract keys in one mode and resolve them in another.Reviewed-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>---
v6 -> v7: the split-GSO reject path now setsQDISC_DROP_OVERLIMITon every deferred segment, not only on the head/original skb; the GSO cb aliases tc_skb_cb.drop_reason and is copied into every segment, so untagged segments carried GSO scratch as their drop reason. Theflow_modeis likewise snapshotted once withREAD_ONCE()and passed to bothcake_hash_keys()andcake_hash_resolve(). (Sashiko nipa v6 review, gpt-6-astra-2-5, gpt-6-astra-5-9)--- net/sched/sch_cake.c | 130 ++++++++++++++++++++++++++++++++----------- 1 file changed, 99 insertions(+), 31 deletions(-)net/sched/sch_cake.c +41
-18@@ -706,19 +706,28 @@ static u16 cake_get_flow_quantum(struct cake_tin_data *q, get_random_u16()) >> 16; } -static u32 cake_hash (struct cake_tin_data *q,const struct sk_buff *skb, -int flow_mode,u16 flow_override,u16 host_override) +struct cake_hash_keys { +u32 srchost_hash; +u32 dsthost_hash; +u32 flow_hash; +}; + +static void cake_hash_keys (const struct sk_buff *skb,int flow_mode, +u16 flow_override,u16 host_override, +struct cake_hash_keys *out) {bool hash_flows = (!flow_override && !!(flow_mode &CAKE_FLOW_FLOWS ));bool hash_hosts = (!host_override && !!(flow_mode &CAKE_FLOW_HOSTS ));bool nat_enabled = !!(flow_mode &CAKE_FLOW_NAT_FLAG ); -u32 flow_hash =0 , srchost_hash =0 , dsthost_hash =0 ; -u16 reduced_hash, srchost_idx, dsthost_idx;struct flow_keys keys, host_keys;bool use_skbhash = skb->l4_hash; + out->flow_hash =0 ; + out->srchost_hash =0 ; + out->dsthost_hash =0 ; +if (unlikely (flow_mode ==CAKE_FLOW_NONE )) -return 0 ; +return ;/* If both overrides are set, or we can use the SKB hash and nat mode is * disabled, we can skip packet dissection entirely. If nat mode is @@ -753,50 +762,64 @@ static u32 cake_hash(struct cake_tin_data *q, const struct sk_buff *skb,switch (host_keys.control.addr_type) {case FLOW_DISSECTOR_KEY_IPV4_ADDRS : host_keys.addrs.v4addrs.src =0 ; - dsthost_hash =flow_hash_from_keys (&host_keys); + out->dsthost_hash =flow_hash_from_keys (&host_keys); host_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src; host_keys.addrs.v4addrs.dst =0 ; - srchost_hash =flow_hash_from_keys (&host_keys); + out->srchost_hash =flow_hash_from_keys (&host_keys);break ;case FLOW_DISSECTOR_KEY_IPV6_ADDRS :memset (&host_keys.addrs.v6addrs.src,0 ,sizeof (host_keys.addrs.v6addrs.src)); - dsthost_hash =flow_hash_from_keys (&host_keys); + out->dsthost_hash =flow_hash_from_keys (&host_keys); host_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;memset (&host_keys.addrs.v6addrs.dst,0 ,sizeof (host_keys.addrs.v6addrs.dst)); - srchost_hash =flow_hash_from_keys (&host_keys); + out->srchost_hash =flow_hash_from_keys (&host_keys);break ;default : - dsthost_hash =0 ; - srchost_hash =0 ; + out->dsthost_hash =0 ; + out->srchost_hash =0 ; }/* This *must* be after the above switch, since as a * side-effect it sorts the src and dst addresses. */ if (hash_flows && !use_skbhash) - flow_hash =flow_hash_from_keys (&keys); + out->flow_hash =flow_hash_from_keys (&keys); skip_hash:if (flow_override) - flow_hash = flow_override -1 ; + out->flow_hash = flow_override -1 ;else if (use_skbhash && (flow_mode &CAKE_FLOW_FLOWS )) - flow_hash = skb->hash; + out->flow_hash = skb->hash;if (host_override) { - dsthost_hash = host_override -1 ; - srchost_hash = host_override -1 ; + out->dsthost_hash = host_override -1 ; + out->srchost_hash = host_override -1 ; }if (!(flow_mode &CAKE_FLOW_FLOWS )) {if (flow_mode &CAKE_FLOW_SRC_IP ) - flow_hash ^= srchost_hash; + out->flow_hash ^= out->srchost_hash;if (flow_mode &CAKE_FLOW_DST_IP ) - flow_hash ^= dsthost_hash; + out->flow_hash ^= out->dsthost_hash; } +} + +/* Commit the flow into the set-associative table (q->tags[], host +* indices, bulk counters). Only after the packet is admitted. +*/ +static u32 cake_hash_resolve (struct cake_tin_data *q, +const struct cake_hash_keys *keys,int flow_mode) +{ +u32 flow_hash = keys->flow_hash, srchost_hash = keys->srchost_hash, + dsthost_hash = keys->dsthost_hash; +u16 reduced_hash, srchost_idx, dsthost_idx; + +if (unlikely (flow_mode ==CAKE_FLOW_NONE )) +return 0 ; reduced_hash = flow_hash %CAKE_QUEUES ;
net/sched/sch_cake.c static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 1787
Quoted 4 lines
net/sched/sch_cake.c +1
-0@@ -1761,13 +1784,17 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, {u32 idx, tin, prev_qlen, prev_backlog, drop_id;struct cake_sched_data *q =qdisc_priv (sch); +int flow_mode =READ_ONCE (q->config->flow_mode);
This breaks the reverse x-mas tree ordering. Move the u32 defs below, and keep an empty line after the qdisc_priv() resolution?
Quoted 20 lines
net/sched/sch_cake.c +5
-1int len =qdisc_pkt_len (skb), ret;u16 flow_override, host_override; +struct sk_buff *segs =NULL ; +struct cake_hash_keys keys;struct sk_buff *ack =NULL ;ktime_t now =ktime_get ();struct cake_tin_data *b;struct cake_flow *flow;bool same_flow = false; +u64 slen =0 ;/* Classify first: a terminal TC action consumes the packet. */ if (cake_tcf_classify (sch, skb, &flow_override, &host_override, &ret)) {@@ -1784,12 +1811,59 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, QDISC_DROP_OVERLIMIT); } -/* choose flow to insert into */ +/* Select the tin and extract the keys before segmenting: +* skb_gso_segment() rewrites encapsulated headers, which
net/sched/sch_cake.c static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 1823
Quoted 10 lines
net/sched/sch_cake.c +7
-2+* changes the keys. +*/ b =cake_select_tin (sch, skb); - idx =cake_hash (b, skb, q->config->flow_mode, flow_override, - host_override) +1 ; +cake_hash_keys (skb, flow_mode, flow_override, host_override, &keys); + +if (qdisc_pkt_segs (skb) >1 && q->config->rate_flags &CAKE_FLAG_SPLIT_GSO ) { +struct sk_buff *nskb, *seg; +netdev_features_t features =netif_skb_features (skb);
Reverse x-mas tree, please.
Quoted 14 lines
net/sched/sch_cake.c +14
-0+ + segs =skb_gso_segment (skb, features & ~NETIF_F_GSO_MASK ); +if (IS_ERR_OR_NULL (segs)) +return qdisc_drop (skb, sch, to_free); + +/* The split path accounts the segment sum, not the +* stab-adjusted qdisc_pkt_len(), so check it here. +*/ +skb_list_walk_safe (segs, seg, nskb) + slen += seg->len; + +if (unlikely ((u64 )sch->qstats.backlog + slen >QDISC_MAX_BACKLOG )) { +qdisc_qstats_overlimit (sch); +/* __skb_gso_segment() puts SKB_GSO_CB at cb
net/sched/sch_cake.c static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 1847
Quoted 10 lines
net/sched/sch_cake.c +10
-0+* offset 32, over tc_skb_cb.drop_reason; every +* segment inherits it. Tag each one with the +* qdisc drop reason. +*/ + skb_list_walk_safe(segs, seg, nskb) +tcf_set_qdisc_drop_reason (seg, +QDISC_DROP_OVERLIMIT ); +/* skb_gso_segment() moved sock_wfree to the tail +* segment, so defer the list to to_free rather +* than free it under the qdisc lock.
I don't think this part of the comment is needed - we do all the drops using to_free, so the sock_wfree thing is irrelevant.
net/sched/sch_cake.c static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 1858
Quoted 10 lines
net/sched/sch_cake.c +10
-0+* Fraglist GSO returns the original skb as head +* (segs == skb), so drop our ref first. +*/ +if (unlikely (segs == skb)) { +consume_skb (skb); +return qdisc_drop_all (segs, sch, to_free); + } +__qdisc_drop_all (segs, to_free); +return qdisc_drop_reason (skb, sch, to_free, +QDISC_DROP_OVERLIMIT );
Since you already set the drop reason above, there's not really any reason to use qdisc_drop_reason() anymore; so this logic can be simplified to simply:
if (unlikely(segs == skb)) consume_skb(skb); return qdisc_drop_all(segs, sch, to_free);
net/sched/sch_cake.c static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 1864
Quoted 6 lines
net/sched/sch_cake.c +6
-0+ } + } + +/* Admitted: resolve the flow. The mode is read once above, so +* extraction and resolution agree if cake_change() intervenes. +*/
The last sentence here is just distracting. Please drop.
Quoted 34 lines
net/sched/sch_cake.c +4
-10+ idx =cake_hash_resolve (b, &keys, flow_mode); tin = (u32)(b - q->tins); - idx--; flow = &b->flows[idx];/* ensure shaper state isn't stale */ @@ -1816,14 +1890,9 @@ 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 ) { -struct sk_buff *segs, *nskb, *seg; -netdev_features_t features =netif_skb_features (skb); -unsigned int slen =0 , numsegs =0 ; - - segs =skb_gso_segment (skb, features & ~NETIF_F_GSO_MASK ); -if (IS_ERR_OR_NULL (segs)) -return qdisc_drop (skb, sch, to_free); +if (segs) { +struct sk_buff *nskb, *seg; +unsigned int numsegs =0 ; skb_list_walk_safe(segs, seg, nskb) {skb_mark_not_on_list (seg);@@ -1837,7 +1906,6 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,qdisc_qlen_inc (sch); numsegs++; q->buffer_used += seg->truesize; - slen += seg->len;WRITE_ONCE (b->packets, b->packets +1 ); }Signature · 1 line
2.43.0
Jamal Hadi Salim
maintainerRe: [PATCH net-next v7 4/4] net/sched: cake: hash before segmenting and reject an overlong split
Quoted 194 lines · Toke Høiland-Jørgensen
Quoted 166 lines · Jamal Hadi Salim
The Fix:
withCAKE_FLAG_SPLIT_GSO, cake computes the flow/host hash keys, then splits a GSO skb into segments it accounts individually. Key extraction must run on the unsegmented skb --skb_gso_segment()can rewrite an encapsulated packet's headers to the inner ones -- and the resolution must wait until the packet is admitted, since it commits the set-associative tags, host indices and way counters that a later rejection would leave mutated (a GSO skb whose segmentation fails was dropped with that state already committed).Split
cake_hash()into a side-effect-freecake_hash_keys(), run on the unsegmented skb, andcake_hash_resolve(), run only once the packet is admitted. Move the split-GSO path ahead of the resolution: sum the segment list, test the total against the ceiling before linking any segment, and drop the whole list on rejection so no persistent state is committed. The list is deferred toto_freeso the tail segment'ssock_wfree()never runs under the qdisc lock; a fraglist GSO, whereskb_segment_list()returns the original skb as the head, drops the caller's reference and queues the list once instead of self-linking it.Tag every deferred segment with
QDISC_DROP_OVERLIMIT:__skb_gso_segment()keepsSKB_GSO_CBat cb offset 32, aliasing tc_skb_cb.drop_reason, and__copy_skb_header()copies it into every segment, so tagging only the head left the rest with GSO scratch (a headroom-sized value) as their drop reason, which__tcf_kfree_skb_list()would report as an unrelated core reason.Read
q->config->flow_modeonce withREAD_ONCE()incake_enqueue()and pass that value to bothcake_hash_keys()andcake_hash_resolve().cake_config_change()writes the mode beforecake_change()takessch_tree_lock(), and the split introduced two separate loads with the segmentation work between them; a concurrent reconfiguration could extract keys in one mode and resolve them in another.Reviewed-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>---
v6 -> v7: the split-GSO reject path now setsQDISC_DROP_OVERLIMITon every deferred segment, not only on the head/original skb; the GSO cb aliases tc_skb_cb.drop_reason and is copied into every segment, so untagged segments carried GSO scratch as their drop reason. Theflow_modeis likewise snapshotted once withREAD_ONCE()and passed to bothcake_hash_keys()andcake_hash_resolve(). (Sashiko nipa v6 review, gpt-6-astra-2-5, gpt-6-astra-5-9)--- net/sched/sch_cake.c | 130 ++++++++++++++++++++++++++++++++----------- 1 file changed, 99 insertions(+), 31 deletions(-)net/sched/sch_cake.c +41
-18@@ -706,19 +706,28 @@ static u16 cake_get_flow_quantum(struct cake_tin_data *q, get_random_u16()) >> 16; } -static u32 cake_hash (struct cake_tin_data *q,const struct sk_buff *skb, -int flow_mode,u16 flow_override,u16 host_override) +struct cake_hash_keys { +u32 srchost_hash; +u32 dsthost_hash; +u32 flow_hash; +}; + +static void cake_hash_keys (const struct sk_buff *skb,int flow_mode, +u16 flow_override,u16 host_override, +struct cake_hash_keys *out) {bool hash_flows = (!flow_override && !!(flow_mode &CAKE_FLOW_FLOWS ));bool hash_hosts = (!host_override && !!(flow_mode &CAKE_FLOW_HOSTS ));bool nat_enabled = !!(flow_mode &CAKE_FLOW_NAT_FLAG ); -u32 flow_hash =0 , srchost_hash =0 , dsthost_hash =0 ; -u16 reduced_hash, srchost_idx, dsthost_idx;struct flow_keys keys, host_keys;bool use_skbhash = skb->l4_hash; + out->flow_hash =0 ; + out->srchost_hash =0 ; + out->dsthost_hash =0 ; +if (unlikely (flow_mode ==CAKE_FLOW_NONE )) -return 0 ; +return ;/* If both overrides are set, or we can use the SKB hash and nat mode is * disabled, we can skip packet dissection entirely. If nat mode is @@ -753,50 +762,64 @@ static u32 cake_hash(struct cake_tin_data *q, const struct sk_buff *skb,switch (host_keys.control.addr_type) {case FLOW_DISSECTOR_KEY_IPV4_ADDRS : host_keys.addrs.v4addrs.src =0 ; - dsthost_hash =flow_hash_from_keys (&host_keys); + out->dsthost_hash =flow_hash_from_keys (&host_keys); host_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src; host_keys.addrs.v4addrs.dst =0 ; - srchost_hash =flow_hash_from_keys (&host_keys); + out->srchost_hash =flow_hash_from_keys (&host_keys);break ;case FLOW_DISSECTOR_KEY_IPV6_ADDRS :memset (&host_keys.addrs.v6addrs.src,0 ,sizeof (host_keys.addrs.v6addrs.src)); - dsthost_hash =flow_hash_from_keys (&host_keys); + out->dsthost_hash =flow_hash_from_keys (&host_keys); host_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;memset (&host_keys.addrs.v6addrs.dst,0 ,sizeof (host_keys.addrs.v6addrs.dst)); - srchost_hash =flow_hash_from_keys (&host_keys); + out->srchost_hash =flow_hash_from_keys (&host_keys);break ;default : - dsthost_hash =0 ; - srchost_hash =0 ; + out->dsthost_hash =0 ; + out->srchost_hash =0 ; }/* This *must* be after the above switch, since as a * side-effect it sorts the src and dst addresses. */ if (hash_flows && !use_skbhash) - flow_hash =flow_hash_from_keys (&keys); + out->flow_hash =flow_hash_from_keys (&keys); skip_hash:if (flow_override) - flow_hash = flow_override -1 ; + out->flow_hash = flow_override -1 ;else if (use_skbhash && (flow_mode &CAKE_FLOW_FLOWS )) - flow_hash = skb->hash; + out->flow_hash = skb->hash;if (host_override) { - dsthost_hash = host_override -1 ; - srchost_hash = host_override -1 ; + out->dsthost_hash = host_override -1 ; + out->srchost_hash = host_override -1 ; }if (!(flow_mode &CAKE_FLOW_FLOWS )) {if (flow_mode &CAKE_FLOW_SRC_IP ) - flow_hash ^= srchost_hash; + out->flow_hash ^= out->srchost_hash;if (flow_mode &CAKE_FLOW_DST_IP ) - flow_hash ^= dsthost_hash; + out->flow_hash ^= out->dsthost_hash; } +} + +/* Commit the flow into the set-associative table (q->tags[], host +* indices, bulk counters). Only after the packet is admitted. +*/ +static u32 cake_hash_resolve (struct cake_tin_data *q, +const struct cake_hash_keys *keys,int flow_mode) +{ +u32 flow_hash = keys->flow_hash, srchost_hash = keys->srchost_hash, + dsthost_hash = keys->dsthost_hash; +u16 reduced_hash, srchost_idx, dsthost_idx; + +if (unlikely (flow_mode ==CAKE_FLOW_NONE )) +return 0 ; reduced_hash = flow_hash %CAKE_QUEUES ;net/sched/sch_cake.c static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 1787
Quoted 4 lines
net/sched/sch_cake.c +1
-0@@ -1761,13 +1784,17 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, {u32 idx, tin, prev_qlen, prev_backlog, drop_id;struct cake_sched_data *q =qdisc_priv (sch); +int flow_mode =READ_ONCE (q->config->flow_mode);This breaks the reverse x-mas tree ordering. Move the u32 defs below, and keep an empty line after the
qdisc_priv()resolution?Quoted 17 lines
net/sched/sch_cake.c +3
-0int len =qdisc_pkt_len (skb), ret;u16 flow_override, host_override; +struct sk_buff *segs =NULL ; +struct cake_hash_keys keys;struct sk_buff *ack =NULL ;ktime_t now =ktime_get ();struct cake_tin_data *b;struct cake_flow *flow;bool same_flow = false; +u64 slen =0 ;/* Classify first: a terminal TC action consumes the packet. */ if (cake_tcf_classify (sch, skb, &flow_override, &host_override, &ret)) {@@ -1784,12 +1811,59 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, QDISC_DROP_OVERLIMIT); }
Quoted 15 lines
net/sched/sch_cake.c static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 1823
Quoted 13 lines
net/sched/sch_cake.c +9
-3-/* choose flow to insert into */ +/* Select the tin and extract the keys before segmenting: +* skb_gso_segment() rewrites encapsulated headers, which +* changes the keys. +*/ b =cake_select_tin (sch, skb); - idx =cake_hash (b, skb, q->config->flow_mode, flow_override, - host_override) +1 ; +cake_hash_keys (skb, flow_mode, flow_override, host_override, &keys); + +if (qdisc_pkt_segs (skb) >1 && q->config->rate_flags &CAKE_FLAG_SPLIT_GSO ) { +struct sk_buff *nskb, *seg; +netdev_features_t features =netif_skb_features (skb);Reverse x-mas tree, please.
Grrr...
Quoted 19 lines
Quoted 19 lines
net/sched/sch_cake.c +19
-0+ + segs =skb_gso_segment (skb, features & ~NETIF_F_GSO_MASK ); +if (IS_ERR_OR_NULL (segs)) +return qdisc_drop (skb, sch, to_free); + +/* The split path accounts the segment sum, not the +* stab-adjusted qdisc_pkt_len(), so check it here. +*/ +skb_list_walk_safe (segs, seg, nskb) + slen += seg->len; + +if (unlikely ((u64 )sch->qstats.backlog + slen >QDISC_MAX_BACKLOG )) { +qdisc_qstats_overlimit (sch); +/* __skb_gso_segment() puts SKB_GSO_CB at cb +* offset 32, over tc_skb_cb.drop_reason; every +* segment inherits it. Tag each one with the +* qdisc drop reason. +*/ +skb_list_walk_safe (segs, seg, nskb)
Quoted 8 lines
net/sched/sch_cake.c static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 1847
Quoted 5 lines
net/sched/sch_cake.c +5
-0+tcf_set_qdisc_drop_reason (seg, +QDISC_DROP_OVERLIMIT ); +/* skb_gso_segment() moved sock_wfree to the tail +* segment, so defer the list to to_free rather +* than free it under the qdisc lock. I don't think this part of the comment is needed - we do all the drops using
to_free, so thesock_wfreething is irrelevant.
I was trying to rationalize "the segs == skb fraglist". How about:
/* Fraglist GSO returns the original skb as head (segs == skb), so drop our ref first */
I can drop it if it seems obvious
Quoted 11 lines
net/sched/sch_cake.c static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 1858
Quoted 10 lines
net/sched/sch_cake.c +10
-0+* Fraglist GSO returns the original skb as head +* (segs == skb), so drop our ref first. +*/ +if (unlikely (segs == skb)) { +consume_skb (skb); +return qdisc_drop_all (segs, sch, to_free); + } +__qdisc_drop_all (segs, to_free); +return qdisc_drop_reason (skb, sch, to_free, +QDISC_DROP_OVERLIMIT );
Quoted 8 lines
Since you already set the drop reason above, there's not really any reason to use
qdisc_drop_reason()anymore; so this logic can be simplified to simply:if (unlikely(segs == skb)) consume_skb(skb); return qdisc_drop_all(segs, sch, to_free);
How does this look ? tcf_set_qdisc_drop_reason(skb, QDISC_DROP_OVERLIMIT); skb_list_walk_safe(segs, seg, nskb)
tcf_set_qdisc_drop_reason(seg, QDISC_DROP_OVERLIMIT);
if (unlikely(segs == skb)) {
consume_skb(skb);
return qdisc_drop_all(segs, sch, to_free);
}__qdisc_drop_all(segs, to_free);
return qdisc_drop(skb, sch, to_free);
Quoted 9 lines
net/sched/sch_cake.c static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 1864
Quoted 6 lines
net/sched/sch_cake.c +6
-0+ } + } + +/* Admitted: resolve the flow. The mode is read once above, so +* extraction and resolution agree if cake_change() intervenes. +*/ The last sentence here is just distracting. Please drop.
Ok.Will reword to: /* Admitted: resolve the flow. */
I wont wait for the 7 days for sashiko to respond if we can reach a quick consensus.
cheers,
jamal
Quoted 34 lines
Quoted 34 lines
net/sched/sch_cake.c +4
-11+ idx =cake_hash_resolve (b, &keys, flow_mode); tin = (u32)(b - q->tins); - idx--; flow = &b->flows[idx];/* ensure shaper state isn't stale */ @@ -1816,14 +1890,9 @@ 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 ) { -struct sk_buff *segs, *nskb, *seg; -netdev_features_t features =netif_skb_features (skb); -unsigned int slen =0 , numsegs =0 ; - - segs =skb_gso_segment (skb, features & ~NETIF_F_GSO_MASK ); -if (IS_ERR_OR_NULL (segs)) -return qdisc_drop (skb, sch, to_free); +if (segs) { +struct sk_buff *nskb, *seg; +unsigned int numsegs =0 ; skb_list_walk_safe(segs, seg, nskb) {skb_mark_not_on_list (seg);@@ -1837,7 +1906,6 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,qdisc_qlen_inc (sch); numsegs++; q->buffer_used += seg->truesize; - slen += seg->len;WRITE_ONCE (b->packets, b->packets +1 ); } --2.43.0
Toke Høiland-Jørgensen
maintainerRe: [PATCH net-next v7 4/4] net/sched: cake: hash before segmenting and reject an overlong split
Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
(quoted diff) +19
-0+ + segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK); + if (IS_ERR_OR_NULL(segs)) + return qdisc_drop(skb, sch, to_free); + + /* The split path accounts the segment sum, not the + * stab-adjusted qdisc_pkt_len(), so check it here. + */ + skb_list_walk_safe(segs, seg, nskb) + slen += seg->len; + + if (unlikely((u64)sch->qstats.backlog + slen > QDISC_MAX_BACKLOG)) { + qdisc_qstats_overlimit(sch); + /* __skb_gso_segment() puts SKB_GSO_CB at cb + * offset 32, over tc_skb_cb.drop_reason; every + * segment inherits it. Tag each one with the + * qdisc drop reason. + */ + skb_list_walk_safe(segs, seg, nskb)Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ tcf_set_qdisc_drop_reason(seg, + QDISC_DROP_OVERLIMIT); + /* skb_gso_segment() moved sock_wfree to the tail + * segment, so defer the list to to_free rather + * than free it under the qdisc lock.I don't think this part of the comment is needed - we do all the drops using
to_free, so thesock_wfreething is irrelevant.
Quoted 4 lines
I was trying to rationalize "the segs == skb fraglist". How about:
/* Fraglist GSO returns the original skb as head (segs == skb), so drop our ref first */
I can drop it if it seems obvious
Yeah, that bit is fine to keep :)
Quoted 23 lines
Quoted 11 lines
Review
Quoted 10 lines
(quoted diff) +10
-0+ * Fraglist GSO returns the original skb as head + * (segs == skb), so drop our ref first. + */ + if (unlikely(segs == skb)) { + consume_skb(skb); + return qdisc_drop_all(segs, sch, to_free); + } + __qdisc_drop_all(segs, to_free); + return qdisc_drop_reason(skb, sch, to_free, + QDISC_DROP_OVERLIMIT);Quoted 8 lines
Since you already set the drop reason above, there's not really any reason to use
qdisc_drop_reason()anymore; so this logic can be simplified to simply:if (unlikely(segs == skb)) consume_skb(skb); return qdisc_drop_all(segs, sch, to_free);How does this look ?
tcf_set_qdisc_drop_reason(skb, QDISC_DROP_OVERLIMIT);skb_list_walk_safe(segs, seg, nskb)
Quoted 8 lines
tcf_set_qdisc_drop_reason(seg, QDISC_DROP_OVERLIMIT);if (unlikely(segs == skb)) { consume_skb(skb); return qdisc_drop_all(segs, sch, to_free); }
__qdisc_drop_all(segs, to_free);
returnqdisc_drop(skb, sch, to_free);
Ah, right, got the 'segs' and 'skb' variables a bit confused, sorry about that.
But, thinking about it some more:
In both branches of your code above, we have a drop of the skb, and a drop_all of the segs. And the existing code already has an unconditional consume_skb() after segmentation, where none of the post-segmentation uses the 'skb' pointer.
So wouldn't it be simpler to just add the consume_skb() unconditionally immediately after the skb_gso_segment() call? Something like this, on top of your patch:
net/sched/sch_cake.c +8 -17
@@ -1819,13 +1819,17 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,cake_hash_keys (skb, flow_mode, flow_override, host_override, &keys);if (qdisc_pkt_segs (skb) >1 && q->config->rate_flags &CAKE_FLAG_SPLIT_GSO ) { -struct sk_buff *nskb, *seg;netdev_features_t features =netif_skb_features (skb); +struct sk_buff *nskb, *seg; segs =skb_gso_segment (skb, features & ~NETIF_F_GSO_MASK );if (IS_ERR_OR_NULL (segs))return qdisc_drop (skb, sch, to_free); +/* we never use the original skb after splitting */ +consume_skb (skb); + skb =NULL ; +/* The split path accounts the segment sum, not the * stab-adjusted qdisc_pkt_len(), so check it here. */
@@ -1842,20 +1846,8 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, skb_list_walk_safe(segs, seg, nskb)tcf_set_qdisc_drop_reason (seg,QDISC_DROP_OVERLIMIT ); -/* skb_gso_segment() moved sock_wfree to the tail -* segment, so defer the list to to_free rather -* than free it under the qdisc lock. -* -* Fraglist GSO returns the original skb as head -* (segs == skb), so drop our ref first. -*/ -if (unlikely (segs == skb)) { -consume_skb (skb); -return qdisc_drop_all (segs, sch, to_free); - } -__qdisc_drop_all (segs, to_free); -return qdisc_drop_reason (skb, sch, to_free, -QDISC_DROP_OVERLIMIT ); + +return qdisc_drop_all (segs, sch, to_free); } }
@@ -1917,9 +1909,8 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,WRITE_ONCE (b->backlogs[idx], b->backlogs[idx] + slen);qdisc_tree_reduce_backlog (sch,1 -numsegs, len-slen); -consume_skb (skb); }else { -/* not splitting */ +/* not splitting - skb pointer still valid */ int ack_pkt_len =0 ;cobalt_set_enqueue_time (skb, now);
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
(quoted diff) +6
-0+ } + } + + /* Admitted: resolve the flow. The mode is read once above, so + * extraction and resolution agree if cake_change() intervenes. + */
Quoted 7 lines
Quoted 2 lines
The last sentence here is just distracting. Please drop.
Ok.Will reword to: /* Admitted: resolve the flow. */
I wont wait for the 7 days for sashiko to respond if we can reach a quick consensus.
Fine with me :)
-Toke
Jamal Hadi Salim
maintainerRe: [PATCH net-next v7 4/4] net/sched: cake: hash before segmenting and reject an overlong split
Quoted 150 lines · Toke Høiland-Jørgensen
Quoted 29 lines
Quoted 20 lines
Quoted 20 lines
(quoted diff) +19
-0+ + segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK); + if (IS_ERR_OR_NULL(segs)) + return qdisc_drop(skb, sch, to_free); + + /* The split path accounts the segment sum, not the + * stab-adjusted qdisc_pkt_len(), so check it here. + */ + skb_list_walk_safe(segs, seg, nskb) + slen += seg->len; + + if (unlikely((u64)sch->qstats.backlog + slen > QDISC_MAX_BACKLOG)) { + qdisc_qstats_overlimit(sch); + /* __skb_gso_segment() puts SKB_GSO_CB at cb + * offset 32, over tc_skb_cb.drop_reason; every + * segment inherits it. Tag each one with the + * qdisc drop reason. + */ + skb_list_walk_safe(segs, seg, nskb)Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ tcf_set_qdisc_drop_reason(seg, + QDISC_DROP_OVERLIMIT); + /* skb_gso_segment() moved sock_wfree to the tail + * segment, so defer the list to to_free rather + * than free it under the qdisc lock.I don't think this part of the comment is needed - we do all the drops using
to_free, so thesock_wfreething is irrelevant.Quoted 4 lines
I was trying to rationalize "the segs == skb fraglist". How about:
/* Fraglist GSO returns the original skb as head (segs == skb), so drop our ref first */
I can drop it if it seems obviousYeah, that bit is fine to keep :)
Quoted 23 lines
Quoted 11 lines
Review
Quoted 10 lines
(quoted diff) +10
-0+ * Fraglist GSO returns the original skb as head + * (segs == skb), so drop our ref first. + */ + if (unlikely(segs == skb)) { + consume_skb(skb); + return qdisc_drop_all(segs, sch, to_free); + } + __qdisc_drop_all(segs, to_free); + return qdisc_drop_reason(skb, sch, to_free, + QDISC_DROP_OVERLIMIT);Quoted 8 lines
Since you already set the drop reason above, there's not really any reason to use
qdisc_drop_reason()anymore; so this logic can be simplified to simply:if (unlikely(segs == skb)) consume_skb(skb); return qdisc_drop_all(segs, sch, to_free);How does this look ?
tcf_set_qdisc_drop_reason(skb, QDISC_DROP_OVERLIMIT);skb_list_walk_safe(segs, seg, nskb)Quoted 8 lines
tcf_set_qdisc_drop_reason(seg, QDISC_DROP_OVERLIMIT);if (unlikely(segs == skb)) { consume_skb(skb); return qdisc_drop_all(segs, sch, to_free); }
__qdisc_drop_all(segs, to_free);
returnqdisc_drop(skb, sch, to_free);Ah, right, got the 'segs' and 'skb' variables a bit confused, sorry about that.
But, thinking about it some more:
In both branches of your code above, we have a drop of the skb, and a
drop_allof the segs. And the existing code already has an unconditionalconsume_skb()after segmentation, where none of the post-segmentation uses the 'skb' pointer.So wouldn't it be simpler to just add the
consume_skb()unconditionally immediately after theskb_gso_segment()call? Something like this, on top of your patch:net/sched/sch_cake.c +8
-17@@ -1819,13 +1819,17 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,cake_hash_keys (skb, flow_mode, flow_override, host_override, &keys);if (qdisc_pkt_segs (skb) >1 && q->config->rate_flags &CAKE_FLAG_SPLIT_GSO ) { -struct sk_buff *nskb, *seg;netdev_features_t features =netif_skb_features (skb); +struct sk_buff *nskb, *seg; segs =skb_gso_segment (skb, features & ~NETIF_F_GSO_MASK );if (IS_ERR_OR_NULL (segs))return qdisc_drop (skb, sch, to_free); +/* we never use the original skb after splitting */ +consume_skb (skb); + skb =NULL ; +/* The split path accounts the segment sum, not the * stab-adjusted qdisc_pkt_len(), so check it here. */ @@ -1842,20 +1846,8 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, skb_list_walk_safe(segs, seg, nskb)tcf_set_qdisc_drop_reason (seg,QDISC_DROP_OVERLIMIT ); -/* skb_gso_segment() moved sock_wfree to the tail -* segment, so defer the list to to_free rather -* than free it under the qdisc lock. -* -* Fraglist GSO returns the original skb as head -* (segs == skb), so drop our ref first. -*/ -if (unlikely (segs == skb)) { -consume_skb (skb); -return qdisc_drop_all (segs, sch, to_free); - } -__qdisc_drop_all (segs, to_free); -return qdisc_drop_reason (skb, sch, to_free, -QDISC_DROP_OVERLIMIT ); + +return qdisc_drop_all (segs, sch, to_free); } }@@ -1917,9 +1909,8 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch,WRITE_ONCE (b->backlogs[idx], b->backlogs[idx] + slen);qdisc_tree_reduce_backlog (sch,1 -numsegs, len-slen); -consume_skb (skb); }else { -/* not splitting */ +/* not splitting - skb pointer still valid */ int ack_pkt_len =0 ;cobalt_set_enqueue_time (skb, now);Quoted 11 lines
Quoted 10 lines
Review
Quoted 7 lines
(quoted diff) +6
-0+ } + } + + /* Admitted: resolve the flow. The mode is read once above, so + * extraction and resolution agree if cake_change() intervenes. + */The last sentence here is just distracting. Please drop.
Quoted 6 lines
Quoted 4 lines
Ok.Will reword to: /* Admitted: resolve the flow. */
I wont wait for the 7 days for sashiko to respond if we can reach a quick consensus.
Fine with me :)
I will resend with your suggestions.
pw-bot: cr
cheers,
jamal
Quoted 1 line
-Toke
[email protected]
AI reviewRe: [PATCH net-next v7 4/4] net/sched: cake: hash before segmenting and reject an overlong split
In reply to Jamal Hadi SalimThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · 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] cake_enqueue() (net/sched/sch_cake.c:1825-1827): when skb_gso_segment() fails with IS_ERR_OR_NULL(segs), the original skb is…
- [Low] The commit presents itself as a bug fix ('The Fix:') with user- visible effects but has no Fixes: trailer.
- [Low] The commit message says cake_enqueue() reads q->config->flow_mode 'once with READ_ONCE()'.
--
net/sched: cake: hash before segmenting and reject an overlong split
This commit splits cake_hash() into a side-effect-free cake_hash_keys(), which runs on the unsegmented skb, and cake_hash_resolve(), which runs only once the packet is admitted. The split-GSO path moves ahead of the
resolution: it sums the segment list, checks the total against QDISC_MAX_BACKLOG and, on rejection, defers the whole list to to_free with every segment tagged QDISC_DROP_OVERLIMIT. flow_mode is read once with READ_ONCE() and passed to both hash helpers.
Quoted 5 lines
net/sched: cake: hash before segmenting and reject an overlong split
The Fix:
withCAKE_FLAG_SPLIT_GSO, cake computes the flow/host hash keys, then splits a GSO skb into segments it accounts individually.
[ ... ]
Quoted 3 lines
[Severity: Low]
This isn't a bug, but the commit is presented as a fix and has no Fixes:
tag.
It adds the missing check of the segment sum against QDISC_MAX_BACKLOG on the CAKE_FLAG_SPLIT_GSO path. It also stops cake_hash() state from being committed for packets that are then dropped. The split-GSO accounting dates back to when CAKE_FLAG_SPLIT_GSO was added to sch_cake.
Could a Fixes: tag be added so backporters know which kernels need this?
Quoted 4 lines
net/sched/sch_cake.c +0
-0
[ ... ]
net/sched/sch_cake.c static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 1787
Quoted 5 lines
net/sched/sch_cake.c +1
-0@@ -1761,13 +1784,17 @@ static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, {u32 idx, tin, prev_qlen, prev_backlog, drop_id;struct cake_sched_data *q =qdisc_priv (sch); +int flow_mode =READ_ONCE (q->config->flow_mode);
[Severity: Low]
The commit message says:
Read q->config->flow_mode once with READ_ONCE() in cake_enqueue() and pass that value to both cake_hash_keys() and cake_hash_resolve().
Is the snapshot complete? Further down in the same cake_enqueue() call, the flowchain code still reloads the live config with plain loads:
net/sched/sch_cake.c:cake_enqueue() {
...
WRITE_ONCE(flow->deficit, cake_get_flow_quantum(b, flow, q->config->flow_mode));
} else if (flow->set == CAKE_SET_SPARSE_WAIT) {
...
cake_inc_srchost_bulk_flow_count(b, flow, q->config->flow_mode);
cake_inc_dsthost_bulk_flow_count(b, flow, q->config->flow_mode);
}
...
}cake_config_change() publishes the new mode with WRITE_ONCE() before cake_change() takes sch_tree_lock(). A concurrent tc change can therefore let cake_hash_resolve() run with a mode that lacks the dual src/dst bits, so flow->srchost and flow->dsthost are not updated. The later reload can then see the dual mode and charge hosts[] bulk counts at stale indices.
These plain loads also race with the WRITE_ONCE() store, which KCSAN can report.
Would passing the local flow_mode to these three calls as well keep one mode for the whole enqueue?
Quoted 3 lines
net/sched/sch_cake.c +1
-0int len =qdisc_pkt_len (skb), ret;u16 flow_override, host_override; +struct sk_buff *segs =NULL ;
[ ... ]
net/sched/sch_cake.c static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, · line 1827
Quoted 7 lines
net/sched/sch_cake.c +7
-0+if (qdisc_pkt_segs (skb) >1 && q->config->rate_flags &CAKE_FLAG_SPLIT_GSO ) { +struct sk_buff *nskb, *seg; +netdev_features_t features =netif_skb_features (skb); + + segs =skb_gso_segment (skb, features & ~NETIF_F_GSO_MASK ); +if (IS_ERR_OR_NULL (segs)) +return qdisc_drop (skb, sch, to_free);
[Severity: Medium]
The comment a few lines below says SKB_GSO_CB sits over tc_skb_cb.drop_reason. Doesn't the same aliasing affect this failure return?
__skb_gso_segment() writes the headroom into that slot before calling skb_mac_gso_segment(), and that call can still fail. Examples are -EINVAL on malformed SKB_GSO_DODGY headers from tun/virtio, or -ENOMEM in skb_segment():
net/core/gso.c:__skb_gso_segment() {
...
SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
...
segs = skb_mac_gso_segment(skb, features);
...
}This overwrites the QDISC_DROP_GENERIC that __dev_xmit_skb() set before enqueue. qdisc_drop() does not write a reason back. __tcf_kfree_skb_list() then reads the headroom value, finds no qdisc subsystem bits, and passes it to kfree_skb_reason() as a core skb_drop_reason:
net/sched/sch_generic.c:__tcf_kfree_skb_list() {
...
if ((reason & SKB_DROP_REASON_SUBSYS_MASK) == __QDISC_DROP_REASON) {
trace_qdisc_drop(q, txq, dev, skb, (enum qdisc_drop_reason)reason);
skb_reason = SKB_DROP_REASON_QDISC_DROP;
} else {
skb_reason = (enum skb_drop_reason)reason;
}
kfree_skb_reason(skb, skb_reason);
...
}As a result, the drop gets an unrelated core reason and trace_qdisc_drop is not emitted.
This return is unchanged from the old code. Since the patch moves this block and re-tags the reject path for this exact aliasing, could this path use qdisc_drop_reason(skb, sch, to_free, QDISC_DROP_GENERIC) or a more specific reason?
Quoted 17 lines
net/sched/sch_cake.c +17
-0+ +/* The split path accounts the segment sum, not the +* stab-adjusted qdisc_pkt_len(), so check it here. +*/ +skb_list_walk_safe (segs, seg, nskb) + slen += seg->len; + +if (unlikely ((u64 )sch->qstats.backlog + slen >QDISC_MAX_BACKLOG )) { +qdisc_qstats_overlimit (sch); +/* __skb_gso_segment() puts SKB_GSO_CB at cb +* offset 32, over tc_skb_cb.drop_reason; every +* segment inherits it. Tag each one with the +* qdisc drop reason. +*/ + skb_list_walk_safe(segs, seg, nskb) +tcf_set_qdisc_drop_reason (seg, +QDISC_DROP_OVERLIMIT );
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/QDISC-BA27.v7.r0.20261019115505%40mojatatu.com