Re: [PATCH net-next v2] net: dsa: Fix skb ownership in taggers
From: Vladimir Oltean <olteanv@gmail.com>
Date: 2026-06-16 21:37:34
On Tue, Jun 16, 2026 at 11:36:22AM +0200, Linus Walleij wrote:
The tag_8021q.c tagger calls vlan_insert_tag() in dsa_8021q_xmit(). vlan_insert_tag() will consume the skb with kfree_skb() on failure and return NULL. When NULL is returned as error code to ->xmit() in dsa_user_xmit() it will free the same skb again leading to a double-free. The idea of dsa_user_xmit() and dsa_switch_rcv() dropping the skb they held before the call to ->xmit() and ->rcv() is conceptually wrong: the pattern elsewhere in the networking code is that consumers drop their skb:s on failure. Modify the ->xmit() and ->rcv() call sites to not drop the SKB if the taggers return NULL from any of these calls. Move those drops into the taggers so every callback error path that retains ownership consumes the skb before returning NULL. Keep the existing helper ownership rules: VLAN insertion helpers already free on failure (this is the case in tag_8021q.c), while deferred transmit paths either transfer the skb reference to worker context or hold a worker reference with skb_get() and drop the caller's reference. For SJA1105 meta RX, transfer the buffered stampable skb under the meta lock and return NULL while the skb is waiting for its meta frame: the skb is not dropped in this case. Reported-by: Sashiko AI Review <sashiko-bot@kernel.org> Closes: https://lore.kernel.org/r/20260610153952.1685895-1-kuba@kernel.org/ (local) Suggested-by: Jakub Kicinski <kuba@kernel.org> Assisted-by: Codex:gpt-5-5 Acked-by: David Yang <mmyangfl@gmail.com> # yt921x Acked-by: Kurt Kanzenbach <kurt@linutronix.de> # hellcreek Signed-off-by: Linus Walleij <linusw@kernel.org> --- Changes in v2: - In some instances __skb_pad() and __skb_put_padto() followed by a kfree_skb() could be simplified to just call skb_pad() and skb_put_padto() which will free the skb on failure. - Use a label and goto for the kfree_skb(); return NULL; in the netc_rcv() callback in tag_netc.c as requested. - Collect ACKs. - Retag for net-next. - Link to v1: https://patch.msgid.link/20260616-dsa-fix-free-skb-v1-1-fd30b35dcf66@kernel.org ---
From my perspective, the tradeoff between pros and cons is not so well explained. Consider the following not mentioned in the commit message: - Changing the kfree_skb() convention, without any mechanical obstacle preventing the backporting of patches that are written assuming one convention down to trees expecting the other (obstacle like a failure to compile, for example, which would warn people of their otherwise silent incompatibility), is an avoidable experience (at best) from a maintainance perspective. - Has anyone proven that a real problem exists? Because dsa_user_xmit() -> skb_ensure_writable_head_tail() has run successfully at this stage, so we know that dev->needed_headroom bytes are available for writing. Because DSA uses VLAN as a tag, dsa_user_setup_tagger() will increase dev->needed_headroom by VLAN_HLEN for the tag_8021q protocols, so vlan_insert_tag() should not fail. I've looked at this function at it seems not to be coded up to fail for any other reason. Otherwise, sure, it seems cleaner this way, but the way I see it, it risks introducing more issues than it fixes. If maintainers feel different about this please go ahead, but given the fact that I don't really have a lot of time to do proper review during this period, I'm more on the pragmatic side on this one.