12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2*9a36a6bcSDavid Howells /* Socket buffer accounting 38c3e34a4SDavid Howells * 48c3e34a4SDavid Howells * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 58c3e34a4SDavid Howells * Written by David Howells (dhowells@redhat.com) 68c3e34a4SDavid Howells */ 78c3e34a4SDavid Howells 88c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 98c3e34a4SDavid Howells 108c3e34a4SDavid Howells #include <linux/module.h> 118c3e34a4SDavid Howells #include <linux/net.h> 128c3e34a4SDavid Howells #include <linux/skbuff.h> 138c3e34a4SDavid Howells #include <net/sock.h> 148c3e34a4SDavid Howells #include <net/af_rxrpc.h> 158c3e34a4SDavid Howells #include "ar-internal.h" 168c3e34a4SDavid Howells 1727f699ccSDavid Howells #define select_skb_count(skb) (&rxrpc_n_rx_skbs) 1871f3ca40SDavid Howells 198c3e34a4SDavid Howells /* 2071f3ca40SDavid Howells * Note the allocation or reception of a socket buffer. 21df844fd4SDavid Howells */ 22*9a36a6bcSDavid Howells void rxrpc_new_skb(struct sk_buff *skb, enum rxrpc_skb_trace why) 23df844fd4SDavid Howells { 24987db9f7SDavid Howells int n = atomic_inc_return(select_skb_count(skb)); 25*9a36a6bcSDavid Howells trace_rxrpc_skb(skb, refcount_read(&skb->users), n, why); 26df844fd4SDavid Howells } 27df844fd4SDavid Howells 28df844fd4SDavid Howells /* 29df844fd4SDavid Howells * Note the re-emergence of a socket buffer from a queue or buffer. 30df844fd4SDavid Howells */ 31*9a36a6bcSDavid Howells void rxrpc_see_skb(struct sk_buff *skb, enum rxrpc_skb_trace why) 32df844fd4SDavid Howells { 33df844fd4SDavid Howells if (skb) { 34987db9f7SDavid Howells int n = atomic_read(select_skb_count(skb)); 35*9a36a6bcSDavid Howells trace_rxrpc_skb(skb, refcount_read(&skb->users), n, why); 36df844fd4SDavid Howells } 37df844fd4SDavid Howells } 38df844fd4SDavid Howells 39df844fd4SDavid Howells /* 40df844fd4SDavid Howells * Note the addition of a ref on a socket buffer. 41df844fd4SDavid Howells */ 42*9a36a6bcSDavid Howells void rxrpc_get_skb(struct sk_buff *skb, enum rxrpc_skb_trace why) 43df844fd4SDavid Howells { 44987db9f7SDavid Howells int n = atomic_inc_return(select_skb_count(skb)); 45*9a36a6bcSDavid Howells trace_rxrpc_skb(skb, refcount_read(&skb->users), n, why); 46df844fd4SDavid Howells skb_get(skb); 47df844fd4SDavid Howells } 48df844fd4SDavid Howells 49df844fd4SDavid Howells /* 50d0d5c0cdSDavid Howells * Note the dropping of a ref on a socket buffer by the core. 51d0d5c0cdSDavid Howells */ 52*9a36a6bcSDavid Howells void rxrpc_eaten_skb(struct sk_buff *skb, enum rxrpc_skb_trace why) 53d0d5c0cdSDavid Howells { 54d0d5c0cdSDavid Howells int n = atomic_inc_return(&rxrpc_n_rx_skbs); 55*9a36a6bcSDavid Howells trace_rxrpc_skb(skb, 0, n, why); 56d0d5c0cdSDavid Howells } 57d0d5c0cdSDavid Howells 58d0d5c0cdSDavid Howells /* 59df844fd4SDavid Howells * Note the destruction of a socket buffer. 60df844fd4SDavid Howells */ 61*9a36a6bcSDavid Howells void rxrpc_free_skb(struct sk_buff *skb, enum rxrpc_skb_trace why) 62df844fd4SDavid Howells { 63df844fd4SDavid Howells if (skb) { 64*9a36a6bcSDavid Howells int n = atomic_dec_return(select_skb_count(skb)); 65*9a36a6bcSDavid Howells trace_rxrpc_skb(skb, refcount_read(&skb->users), n, why); 66df844fd4SDavid Howells kfree_skb(skb); 67df844fd4SDavid Howells } 68df844fd4SDavid Howells } 69df844fd4SDavid Howells 70df844fd4SDavid Howells /* 71df844fd4SDavid Howells * Clear a queue of socket buffers. 72df844fd4SDavid Howells */ 73df844fd4SDavid Howells void rxrpc_purge_queue(struct sk_buff_head *list) 74df844fd4SDavid Howells { 75df844fd4SDavid Howells struct sk_buff *skb; 76*9a36a6bcSDavid Howells 77df844fd4SDavid Howells while ((skb = skb_dequeue((list))) != NULL) { 78987db9f7SDavid Howells int n = atomic_dec_return(select_skb_count(skb)); 79*9a36a6bcSDavid Howells trace_rxrpc_skb(skb, refcount_read(&skb->users), n, 80*9a36a6bcSDavid Howells rxrpc_skb_put_purge); 81df844fd4SDavid Howells kfree_skb(skb); 82df844fd4SDavid Howells } 83df844fd4SDavid Howells } 84