net: factor out a helper to decrement the skb refcount
authorPaolo Abeni <pabeni@redhat.com>
Mon, 12 Jun 2017 09:23:41 +0000 (11:23 +0200)
committerDavid S. Miller <davem@davemloft.net>
Mon, 12 Jun 2017 14:01:29 +0000 (10:01 -0400)
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>
include/linux/skbuff.h
net/core/datagram.c
net/core/skbuff.c

index d460a4cbda1c8b65e5e0b2a643e29c122055df1f..decce3655a48e0f94d3fc93a0e91dd418de04725 100644 (file)
@@ -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);
index bc46118486fe8ffaae37f688467faa6494e3549c..e5311a7c70daf1d6c60d67fc6cf383e85cacc82e 100644 (file)
@@ -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;
        }
index e508c1eae67fb433ae165875876450fd4307a7b2..747263c498388cfdf49e6891869dadee62242ddf 100644 (file)
@@ -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);
 }
@@ -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);
 }