Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 9a36a6b | 2022-10-21 15:31:21 +0100 | [diff] [blame] | 2 | /* Socket buffer accounting |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/net.h> |
| 12 | #include <linux/skbuff.h> |
| 13 | #include <net/sock.h> |
| 14 | #include <net/af_rxrpc.h> |
| 15 | #include "ar-internal.h" |
| 16 | |
David Howells | 27f699c | 2022-10-07 13:52:06 +0100 | [diff] [blame] | 17 | #define select_skb_count(skb) (&rxrpc_n_rx_skbs) |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 18 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 19 | /* |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 20 | * Note the allocation or reception of a socket buffer. |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 21 | */ |
David Howells | 9a36a6b | 2022-10-21 15:31:21 +0100 | [diff] [blame] | 22 | void rxrpc_new_skb(struct sk_buff *skb, enum rxrpc_skb_trace why) |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 23 | { |
David Howells | 987db9f | 2019-08-19 09:25:38 +0100 | [diff] [blame] | 24 | int n = atomic_inc_return(select_skb_count(skb)); |
David Howells | 9a36a6b | 2022-10-21 15:31:21 +0100 | [diff] [blame] | 25 | trace_rxrpc_skb(skb, refcount_read(&skb->users), n, why); |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | /* |
| 29 | * Note the re-emergence of a socket buffer from a queue or buffer. |
| 30 | */ |
David Howells | 9a36a6b | 2022-10-21 15:31:21 +0100 | [diff] [blame] | 31 | void rxrpc_see_skb(struct sk_buff *skb, enum rxrpc_skb_trace why) |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 32 | { |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 33 | if (skb) { |
David Howells | 987db9f | 2019-08-19 09:25:38 +0100 | [diff] [blame] | 34 | int n = atomic_read(select_skb_count(skb)); |
David Howells | 9a36a6b | 2022-10-21 15:31:21 +0100 | [diff] [blame] | 35 | trace_rxrpc_skb(skb, refcount_read(&skb->users), n, why); |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * Note the addition of a ref on a socket buffer. |
| 41 | */ |
David Howells | 9a36a6b | 2022-10-21 15:31:21 +0100 | [diff] [blame] | 42 | void rxrpc_get_skb(struct sk_buff *skb, enum rxrpc_skb_trace why) |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 43 | { |
David Howells | 987db9f | 2019-08-19 09:25:38 +0100 | [diff] [blame] | 44 | int n = atomic_inc_return(select_skb_count(skb)); |
David Howells | 9a36a6b | 2022-10-21 15:31:21 +0100 | [diff] [blame] | 45 | trace_rxrpc_skb(skb, refcount_read(&skb->users), n, why); |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 46 | skb_get(skb); |
| 47 | } |
| 48 | |
| 49 | /* |
David Howells | d0d5c0c | 2019-08-27 10:13:46 +0100 | [diff] [blame] | 50 | * Note the dropping of a ref on a socket buffer by the core. |
| 51 | */ |
David Howells | 9a36a6b | 2022-10-21 15:31:21 +0100 | [diff] [blame] | 52 | void rxrpc_eaten_skb(struct sk_buff *skb, enum rxrpc_skb_trace why) |
David Howells | d0d5c0c | 2019-08-27 10:13:46 +0100 | [diff] [blame] | 53 | { |
David Howells | d0d5c0c | 2019-08-27 10:13:46 +0100 | [diff] [blame] | 54 | int n = atomic_inc_return(&rxrpc_n_rx_skbs); |
David Howells | 9a36a6b | 2022-10-21 15:31:21 +0100 | [diff] [blame] | 55 | trace_rxrpc_skb(skb, 0, n, why); |
David Howells | d0d5c0c | 2019-08-27 10:13:46 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 59 | * Note the destruction of a socket buffer. |
| 60 | */ |
David Howells | 9a36a6b | 2022-10-21 15:31:21 +0100 | [diff] [blame] | 61 | void rxrpc_free_skb(struct sk_buff *skb, enum rxrpc_skb_trace why) |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 62 | { |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 63 | if (skb) { |
David Howells | 9a36a6b | 2022-10-21 15:31:21 +0100 | [diff] [blame] | 64 | int n = atomic_dec_return(select_skb_count(skb)); |
| 65 | trace_rxrpc_skb(skb, refcount_read(&skb->users), n, why); |
David Howells | 16d5677 | 2023-02-07 22:11:30 +0000 | [diff] [blame] | 66 | consume_skb(skb); |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Clear a queue of socket buffers. |
| 72 | */ |
| 73 | void rxrpc_purge_queue(struct sk_buff_head *list) |
| 74 | { |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 75 | struct sk_buff *skb; |
David Howells | 9a36a6b | 2022-10-21 15:31:21 +0100 | [diff] [blame] | 76 | |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 77 | while ((skb = skb_dequeue((list))) != NULL) { |
David Howells | 987db9f | 2019-08-19 09:25:38 +0100 | [diff] [blame] | 78 | int n = atomic_dec_return(select_skb_count(skb)); |
David Howells | 9a36a6b | 2022-10-21 15:31:21 +0100 | [diff] [blame] | 79 | trace_rxrpc_skb(skb, refcount_read(&skb->users), n, |
| 80 | rxrpc_skb_put_purge); |
David Howells | 16d5677 | 2023-02-07 22:11:30 +0000 | [diff] [blame] | 81 | consume_skb(skb); |
David Howells | df844fd | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 82 | } |
| 83 | } |