Commit 3889a803 authored by Paolo Abeni's avatar Paolo Abeni Committed by David S. Miller

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: default avatarPaolo Abeni <pabeni@redhat.com>
Acked-by: default avatarEric Dumazet <edumazet@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 78d61022
...@@ -867,6 +867,19 @@ static inline unsigned int skb_napi_id(const struct sk_buff *skb) ...@@ -867,6 +867,19 @@ static inline unsigned int skb_napi_id(const struct sk_buff *skb)
#endif #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(struct sk_buff *skb);
void kfree_skb_list(struct sk_buff *segs); void kfree_skb_list(struct sk_buff *segs);
void skb_tx_error(struct sk_buff *skb); void skb_tx_error(struct sk_buff *skb);
......
...@@ -330,9 +330,7 @@ void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len) ...@@ -330,9 +330,7 @@ void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len)
{ {
bool slow; bool slow;
if (likely(atomic_read(&skb->users) == 1)) if (!skb_unref(skb)) {
smp_rmb();
else if (likely(!atomic_dec_and_test(&skb->users))) {
sk_peek_offset_bwd(sk, len); sk_peek_offset_bwd(sk, len);
return; return;
} }
......
...@@ -694,12 +694,9 @@ EXPORT_SYMBOL(__kfree_skb); ...@@ -694,12 +694,9 @@ EXPORT_SYMBOL(__kfree_skb);
*/ */
void kfree_skb(struct sk_buff *skb) void kfree_skb(struct sk_buff *skb)
{ {
if (unlikely(!skb)) if (!skb_unref(skb))
return;
if (likely(atomic_read(&skb->users) == 1))
smp_rmb();
else if (likely(!atomic_dec_and_test(&skb->users)))
return; return;
trace_kfree_skb(skb, __builtin_return_address(0)); trace_kfree_skb(skb, __builtin_return_address(0));
__kfree_skb(skb); __kfree_skb(skb);
} }
...@@ -746,12 +743,9 @@ EXPORT_SYMBOL(skb_tx_error); ...@@ -746,12 +743,9 @@ EXPORT_SYMBOL(skb_tx_error);
*/ */
void consume_skb(struct sk_buff *skb) void consume_skb(struct sk_buff *skb)
{ {
if (unlikely(!skb)) if (!skb_unref(skb))
return;
if (likely(atomic_read(&skb->users) == 1))
smp_rmb();
else if (likely(!atomic_dec_and_test(&skb->users)))
return; return;
trace_consume_skb(skb); trace_consume_skb(skb);
__kfree_skb(skb); __kfree_skb(skb);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment