When a new filter is created with TCA_U32_LINK, u32_set_parms() resolves the linked hash table and increments its reference count, storing it in n->ht_down. If u32_replace_hw_knode() subsequently fails, execution jumps to the errunbind label which frees the node with kfree(n) but never drops the reference on n->ht_down. This leaves the tc_u_hnode refcount permanently elevated, preventing it from being freed when the hash table is later deleted. Drop the ht_down reference at errunbind before freeing the node. This is a forward fix for currently dead code, which will be uncovered by the upcoming patch "net/sched: cls_u32: feed u32_replace_hw_knode() with correct set of flags". Fixes: d34e3e181395 ("net: cls_u32: Add support for skip-sw flag to tc u32 classifier.") Cc: stable@vger.kernel.org Reported-by: Sashiko Closes: https://sashiko.dev/#/patchset/20260807100356.1083774-1-jedrzej.jagielski%40intel.com Signed-off-by: Jedrzej Jagielski --- net/sched/cls_u32.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/net/sched/cls_u32.c b/net/sched/cls_u32.c index 91f4e6458785..c544c8e4e876 100644 --- a/net/sched/cls_u32.c +++ b/net/sched/cls_u32.c @@ -891,7 +891,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, struct netlink_ext_ack *extack) { struct tc_u_common *tp_c = tp->data; - struct tc_u_hnode *ht; + struct tc_u_hnode *ht, *ht_down; struct tc_u_knode *n; struct tc_u32_sel *s; struct nlattr *opt = tca[TCA_OPTIONS]; @@ -1221,6 +1221,9 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, errunbind: u32_unbind_filter(tp, n, tb); + ht_down = rtnl_dereference(n->ht_down); + if (ht_down && refcount_dec_and_test(&ht_down->refcnt)) + kfree(ht_down); #ifdef CONFIG_CLS_U32_MARK free_percpu(n->pcpu_success); -- 2.31.1 Remove the redundant refcount_inc(&ht_old->refcnt) in the u32_replace_hw_knode() error path during filter update. Commit e8d3d78c19be ("net: sched: cls_u32: Undo refcount decrement in case update failed") added this increment to "undo" a decrement performed by u32_set_parms() on the original node's linked hash table but apparently this decrement is already balanced by u32_init_knode(), which increments the same refcount when cloning the original node: u32_init_knode(): refcount_inc() on the old ht_down u32_set_parms(): refcount_dec() on ht_old __u32_destroy_key(new): drops new->ht_down These cancel out, leaving the original hash table's refcount unchanged. The additional refcount_inc in the error path has no matching decrement, leaving the tc_u_hnode refcount permanently elevated by 1. This prevents the hash table from ever being freed, leaking memory. __u32_destroy_key(new) already correctly drops new->ht_down's refcount (the new linked hash table set by u32_set_parms), so no manual refcount fixup is needed. This is a forward fix for currently dead code, which will be uncovered by the upcoming patch "net/sched: cls_u32: feed u32_replace_hw_knode() with correct set of flags". Fixes: e8d3d78c19be ("net: sched: cls_u32: Undo refcount decrement in case update failed") Cc: stable@vger.kernel.org Reported-by: Sashiko Closes: https://sashiko.dev/#/patchset/20260807100356.1083774-1-jedrzej.jagielski%40intel.com Signed-off-by: Jedrzej Jagielski --- net/sched/cls_u32.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/net/sched/cls_u32.c b/net/sched/cls_u32.c index c544c8e4e876..ae1b6476f921 100644 --- a/net/sched/cls_u32.c +++ b/net/sched/cls_u32.c @@ -954,14 +954,6 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, err = u32_replace_hw_knode(tp, new, flags, extack); if (err) { u32_unbind_filter(tp, new, tb); - - if (tb[TCA_U32_LINK]) { - struct tc_u_hnode *ht_old; - - ht_old = rtnl_dereference(n->ht_down); - if (ht_old) - refcount_inc(&ht_old->refcnt); - } __u32_destroy_key(new); return err; } -- 2.31.1 On knode replace, u32_init_knode() copies the handle of the existing knode into the new one. If offloading the new knode fails, u32_replace_hw_knode() rolls back with u32_remove_hw_knode(), which issues TC_CLSU32_DELETE_KNODE for that shared handle and so removes the HW rule of the old knode, which is still installed in software. For a skip_sw filter, u32_classify() skips it, so the filter matches neither in SW nor in HW, while userspace is told the replace failed. Add @new_handle to u32_replace_hw_knode() and roll back only when the handle is not shared with an installed knode, i.e. when adding a new one. On replace, leave the old HW rule in place, in line with tc_setup_cb_add() treating a failed add as non-destructive. This is a trade-off for blocks with multiple offload callbacks. tc_setup_cb_add() stops at the first failing callback, so callbacks which accepted the replace before it keep the new rule, and its link_handle may refer to an hnode no longer referenced in software once the new knode is destroyed. The removed rollback deleted the rule on all callbacks instead, including those which rejected the replace and still held the old rule. Since commit 695176bfe5de ("net_sched: refactor TC action init API") skip_sw is ignored, so the same split HW state is left, but silently and with the new knode installed in software. Unless a netdev sharing the block is not offload-capable let userspace know via extack that the hardware rollback was skipped. Rolling back per callback is not possible, as tc_setup_cb_add() does not report which callbacks succeeded and drivers identify u32 rules by handle only. Re-offloading the old knode is not done either, as drivers which reject an existing handle (e.g. ixgbe) or handle REPLACE_KNODE as an add (e.g. cxgb4) would not be restored by it. Drivers which destroy the old rule themselves when REPLACE_KNODE fails (e.g. stmmac, nfp abm) still lose it, which is not changed by this patch. This is a forward fix for currently dead code, which will be uncovered by the upcoming patch "net/sched: cls_u32: feed u32_replace_hw_knode() with correct set of flags". Fixes: 245dc5121a9b ("net: sched: cls_u32: call block callbacks for offload") Cc: stable@vger.kernel.org Reported-by: Sashiko Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260813122242.1690024-1-jedrzej.jagielski%40intel.com Signed-off-by: Jedrzej Jagielski --- net/sched/cls_u32.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/net/sched/cls_u32.c b/net/sched/cls_u32.c index ae1b6476f921..7ec05629e860 100644 --- a/net/sched/cls_u32.c +++ b/net/sched/cls_u32.c @@ -548,7 +548,8 @@ static void u32_remove_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n, } static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n, - u32 flags, struct netlink_ext_ack *extack) + u32 flags, bool new_handle, + struct netlink_ext_ack *extack) { struct tc_u_hnode *ht = rtnl_dereference(n->ht_down); struct tcf_block *block = tp->chain->block; @@ -576,7 +577,11 @@ static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n, err = tc_setup_cb_add(block, tp, TC_SETUP_CLSU32, &cls_u32, skip_sw, &n->flags, &n->in_hw_count, true); if (err) { - u32_remove_hw_knode(tp, n, NULL); + if (new_handle) + u32_remove_hw_knode(tp, n, NULL); + else if (!block->nooffloaddevcnt) + NL_SET_ERR_MSG_WEAK_MOD(extack, + "Replace failed, hardware rollback was skipped"); return err; } @@ -951,7 +956,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, u32_bind_filter(tp, new, base, tb); - err = u32_replace_hw_knode(tp, new, flags, extack); + err = u32_replace_hw_knode(tp, new, flags, false, extack); if (err) { u32_unbind_filter(tp, new, tb); __u32_destroy_key(new); @@ -1189,7 +1194,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, struct tc_u_knode __rcu **ins; struct tc_u_knode *pins; - err = u32_replace_hw_knode(tp, n, flags, extack); + err = u32_replace_hw_knode(tp, n, flags, true, extack); if (err) goto errunbind; -- 2.31.1 When trying to offload u32 cmd by adding skip_sw attributes it gets ignored and it cannot be correctly processed by HW and tc_cls_u32_offload struct which is then passed to driver lacks skip_sw var enabled. Encountered the issue while developing ixgbe. u32_replace_hw_knode is fed with the TCA_ACT_* flags (bits 16+) instead of TCA_CLS_* flags (bits 0-4) which actually stores the flags parsed by tc, so tc_skip_sw() always returns false. This leads to ignoring some of the attibutes which meant to be configured on filter setup. None of the TCA_ACT_FLAGS_* is actually used within u32_replace_hw_knode(), so theres no point in passing them. Other classifiers do not put TCA_ACT_FLAGS_* during filter setup. Looks like commit 695176bfe5de ("net_sched: refactor TC action init API") shadowed the local flags variable which used to be set with nla_get_u32(tb[TCA_U32_FLAGS]) with the flags as the new function param while not replacing for none of the u32_replace_hw_knode() calls. Because skip_sw is now honored, "tc ... u32 ... skip_sw" add/replace commands that previously succeeded (and were silently skipped by the software path) will now return -EOPNOTSUPP/-EINVAL when the block or driver cannot offload the filter. Fixes: 695176bfe5de ("net_sched: refactor TC action init API") Cc: stable@vger.kernel.org Reviewed-by: Marcin Szycik Reviewed-by: Przemek Kitszel Signed-off-by: Jedrzej Jagielski --- net/sched/cls_u32.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net/sched/cls_u32.c b/net/sched/cls_u32.c index 7ec05629e860..2c1dd926f6c0 100644 --- a/net/sched/cls_u32.c +++ b/net/sched/cls_u32.c @@ -956,7 +956,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, u32_bind_filter(tp, new, base, tb); - err = u32_replace_hw_knode(tp, new, flags, false, extack); + err = u32_replace_hw_knode(tp, new, userflags, false, extack); if (err) { u32_unbind_filter(tp, new, tb); __u32_destroy_key(new); @@ -1194,7 +1194,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, struct tc_u_knode __rcu **ins; struct tc_u_knode *pins; - err = u32_replace_hw_knode(tp, n, flags, true, extack); + err = u32_replace_hw_knode(tp, n, userflags, true, extack); if (err) goto errunbind; -- 2.31.1 Add a tdc test case checking that cls_u32 honors skip_sw. The dummy device has no tc offload, so the block bound to its ingress qdisc counts a non offload-capable device and a skip_sw filter cannot be offloaded anywhere. The filter add is expected to fail with EOPNOTSUPP (exit 2) and leave no u32 filter behind. Before the fix, skip_sw was ignored for u32 knodes, the add silently succeeded and the filter matched neither in software nor in hardware. Signed-off-by: Jedrzej Jagielski --- .../tc-testing/tc-tests/filters/u32.json | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tools/testing/selftests/tc-testing/tc-tests/filters/u32.json b/tools/testing/selftests/tc-testing/tc-tests/filters/u32.json index edc5148a8d97..a97caf9ff85d 100644 --- a/tools/testing/selftests/tc-testing/tc-tests/filters/u32.json +++ b/tools/testing/selftests/tc-testing/tc-tests/filters/u32.json @@ -424,5 +424,27 @@ "teardown": [ "$TC qdisc del dev $DEV1 ingress" ] + }, + { + "id": "a6e9", + "name": "Reject u32 with skip_sw on device without offload support", + "category": [ + "filter", + "u32" + ], + "plugins": { + "requires": "nsPlugin" + }, + "setup": [ + "$TC qdisc add dev $DUMMY ingress" + ], + "cmdUnderTest": "$TC filter add dev $DUMMY ingress u32 match u32 0 0 skip_sw", + "expExitCode": "2", + "verifyCmd": "$TC filter show dev $DUMMY ingress", + "matchPattern": "u32", + "matchCount": "0", + "teardown": [ + "$TC qdisc del dev $DUMMY ingress" + ] } ] -- 2.31.1