git: 36ed9a84d17b - stable/15 - LinuxKPI: skbuff: add reference counting to the skb

Bjoern A. Zeeb <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src
Message-ID <6a66a200.26e39.35db8a5c__37596.2461855792$1785111184$gmane$org@gitrepo.freebsd.org>
The branch stable/15 has been updated by bz:

URL: https://cgit.FreeBSD.org/src/commit/?id=36ed9a84d17b58b83612bb4ec32bddfc56f991b2

commit 36ed9a84d17b58b83612bb4ec32bddfc56f991b2
Author:     Bjoern A. Zeeb <[email protected]>
AuthorDate: 2026-02-03 22:13:24 +0000
Commit:     Bjoern A. Zeeb <[email protected]>
CommitDate: 2026-07-26 16:48:25 +0000

    LinuxKPI: skbuff: add reference counting to the skb
    
    Sponsored by:   The FreeBSD Foundation
    
    (cherry picked from commit cb13e826ec45e11e964e1921d281e0a06ca5e152)
---
 sys/compat/linuxkpi/common/src/linux_skbuff.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/sys/compat/linuxkpi/common/src/linux_skbuff.c b/sys/compat/linuxkpi/common/src/linux_skbuff.c
index 5af9633691a6..d57c277c3267 100644
--- a/sys/compat/linuxkpi/common/src/linux_skbuff.c
+++ b/sys/compat/linuxkpi/common/src/linux_skbuff.c
@@ -140,6 +140,7 @@ linuxkpi_alloc_skb(size_t size, gfp_t gfp)
 	skb->head = skb->data = (uint8_t *)p;
 	skb_reset_tail_pointer(skb);
 	skb->end = skb->head + size;
+	refcount_set(&skb->refcnt, 1);
 
 	SKB_TRACE_FMT(skb, "data %p size %zu", (skb) ? skb->data : NULL, size);
 	return (skb);
@@ -180,6 +181,7 @@ linuxkpi_build_skb(void *data, size_t fragsz)
 	skb->head = skb->data = data;
 	skb_reset_tail_pointer(skb);
 	skb->end = skb->head + fragsz;
+	refcount_set(&skb->refcnt, 1);
 
 	return (skb);
 }
@@ -288,6 +290,20 @@ lkpi___skb_linearize(struct sk_buff *skb)
 	return (0);
 }
 
+static bool
+lkpi_skb_refcount_release(struct sk_buff *skb)
+{
+	if (skb == NULL)
+		return (false);
+
+	/* Do we need further tests to avoid freeing this one? */
+
+	if (!refcount_dec_and_test(&skb->refcnt))
+		return (false);
+
+	return (true);
+}
+
 void
 linuxkpi_kfree_skb(struct sk_buff *skb)
 {
@@ -298,6 +314,11 @@ linuxkpi_kfree_skb(struct sk_buff *skb)
 	if (skb == NULL)
 		return;
 
+	if (!lkpi_skb_refcount_release(skb)) {
+		SKB_TRACE_FMT(skb, "not freed due to refcnt");
+		return;
+	}
+
 	/*
 	 * XXX TODO this will go away once we have skb backed by mbuf.
 	 * currently we allow the mbuf to stay around and use a private
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.