Skip to content

Commit 3889a80

Browse files
Paolo Abenidavem330
authored andcommitted
net: factor out a helper to decrement the skb refcount
The same code is replicated in 3 different places; move it to a common helper. Signed-off-by: Paolo Abeni <pabeni@redhat.com> Acked-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 78d6102 commit 3889a80

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

include/linux/skbuff.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,19 @@ static inline unsigned int skb_napi_id(const struct sk_buff *skb)
867867
#endif
868868
}
869869

870+
/* decrement the reference count and return true if we can free the skb */
871+
static inline bool skb_unref(struct sk_buff *skb)
872+
{
873+
if (unlikely(!skb))
874+
return false;
875+
if (likely(atomic_read(&skb->users) == 1))
876+
smp_rmb();
877+
else if (likely(!atomic_dec_and_test(&skb->users)))
878+
return false;
879+
880+
return true;
881+
}
882+
870883
void kfree_skb(struct sk_buff *skb);
871884
void kfree_skb_list(struct sk_buff *segs);
872885
void skb_tx_error(struct sk_buff *skb);

net/core/datagram.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,7 @@ void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len)
330330
{
331331
bool slow;
332332

333-
if (likely(atomic_read(&skb->users) == 1))
334-
smp_rmb();
335-
else if (likely(!atomic_dec_and_test(&skb->users))) {
333+
if (!skb_unref(skb)) {
336334
sk_peek_offset_bwd(sk, len);
337335
return;
338336
}

net/core/skbuff.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -694,12 +694,9 @@ EXPORT_SYMBOL(__kfree_skb);
694694
*/
695695
void kfree_skb(struct sk_buff *skb)
696696
{
697-
if (unlikely(!skb))
698-
return;
699-
if (likely(atomic_read(&skb->users) == 1))
700-
smp_rmb();
701-
else if (likely(!atomic_dec_and_test(&skb->users)))
697+
if (!skb_unref(skb))
702698
return;
699+
703700
trace_kfree_skb(skb, __builtin_return_address(0));
704701
__kfree_skb(skb);
705702
}
@@ -746,12 +743,9 @@ EXPORT_SYMBOL(skb_tx_error);
746743
*/
747744
void consume_skb(struct sk_buff *skb)
748745
{
749-
if (unlikely(!skb))
750-
return;
751-
if (likely(atomic_read(&skb->users) == 1))
752-
smp_rmb();
753-
else if (likely(!atomic_dec_and_test(&skb->users)))
746+
if (!skb_unref(skb))
754747
return;
748+
755749
trace_consume_skb(skb);
756750
__kfree_skb(skb);
757751
}

0 commit comments

Comments
 (0)