Skip to content

Commit

Permalink
net: factor out a helper to decrement the skb refcount
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Paolo Abeni authored and David S. Miller committed Jun 12, 2017
1 parent 78d6102 commit 3889a80
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
13 changes: 13 additions & 0 deletions include/linux/skbuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,19 @@ static inline unsigned int skb_napi_id(const struct sk_buff *skb)
#endif
}

/* decrement the reference count and return true if we can free the skb */
static inline bool skb_unref(struct sk_buff *skb)
{
if (unlikely(!skb))
return false;
if (likely(atomic_read(&skb->users) == 1))
smp_rmb();
else if (likely(!atomic_dec_and_test(&skb->users)))
return false;

return true;
}

void kfree_skb(struct sk_buff *skb);
void kfree_skb_list(struct sk_buff *segs);
void skb_tx_error(struct sk_buff *skb);
Expand Down
4 changes: 1 addition & 3 deletions net/core/datagram.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len)
{
bool slow;

if (likely(atomic_read(&skb->users) == 1))
smp_rmb();
else if (likely(!atomic_dec_and_test(&skb->users))) {
if (!skb_unref(skb)) {
sk_peek_offset_bwd(sk, len);
return;
}
Expand Down
14 changes: 4 additions & 10 deletions net/core/skbuff.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,12 +694,9 @@ EXPORT_SYMBOL(__kfree_skb);
*/
void kfree_skb(struct sk_buff *skb)
{
if (unlikely(!skb))
return;
if (likely(atomic_read(&skb->users) == 1))
smp_rmb();
else if (likely(!atomic_dec_and_test(&skb->users)))
if (!skb_unref(skb))
return;

trace_kfree_skb(skb, __builtin_return_address(0));
__kfree_skb(skb);
}
Expand Down Expand Up @@ -746,12 +743,9 @@ EXPORT_SYMBOL(skb_tx_error);
*/
void consume_skb(struct sk_buff *skb)
{
if (unlikely(!skb))
return;
if (likely(atomic_read(&skb->users) == 1))
smp_rmb();
else if (likely(!atomic_dec_and_test(&skb->users)))
if (!skb_unref(skb))
return;

trace_consume_skb(skb);
__kfree_skb(skb);
}
Expand Down

0 comments on commit 3889a80

Please sign in to comment.