blob: 3bcd6ee803960ba3280baf0f77508905c57d6c42 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells9a36a6b2022-10-21 15:31:21 +01002/* Socket buffer accounting
David Howells17926a72007-04-26 15:48:28 -07003 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells17926a72007-04-26 15:48:28 -07006 */
7
Joe Perches9b6d5392016-06-02 12:08:52 -07008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
David Howells17926a72007-04-26 15:48:28 -070010#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 Howells27f699c2022-10-07 13:52:06 +010017#define select_skb_count(skb) (&rxrpc_n_rx_skbs)
David Howells71f3ca42016-09-17 10:49:14 +010018
David Howells17926a72007-04-26 15:48:28 -070019/*
David Howells71f3ca42016-09-17 10:49:14 +010020 * Note the allocation or reception of a socket buffer.
David Howellsdf844fd2016-08-23 15:27:24 +010021 */
David Howells9a36a6b2022-10-21 15:31:21 +010022void rxrpc_new_skb(struct sk_buff *skb, enum rxrpc_skb_trace why)
David Howellsdf844fd2016-08-23 15:27:24 +010023{
David Howells987db9f2019-08-19 09:25:38 +010024 int n = atomic_inc_return(select_skb_count(skb));
David Howells9a36a6b2022-10-21 15:31:21 +010025 trace_rxrpc_skb(skb, refcount_read(&skb->users), n, why);
David Howellsdf844fd2016-08-23 15:27:24 +010026}
27
28/*
29 * Note the re-emergence of a socket buffer from a queue or buffer.
30 */
David Howells9a36a6b2022-10-21 15:31:21 +010031void rxrpc_see_skb(struct sk_buff *skb, enum rxrpc_skb_trace why)
David Howellsdf844fd2016-08-23 15:27:24 +010032{
David Howellsdf844fd2016-08-23 15:27:24 +010033 if (skb) {
David Howells987db9f2019-08-19 09:25:38 +010034 int n = atomic_read(select_skb_count(skb));
David Howells9a36a6b2022-10-21 15:31:21 +010035 trace_rxrpc_skb(skb, refcount_read(&skb->users), n, why);
David Howellsdf844fd2016-08-23 15:27:24 +010036 }
37}
38
39/*
40 * Note the addition of a ref on a socket buffer.
41 */
David Howells9a36a6b2022-10-21 15:31:21 +010042void rxrpc_get_skb(struct sk_buff *skb, enum rxrpc_skb_trace why)
David Howellsdf844fd2016-08-23 15:27:24 +010043{
David Howells987db9f2019-08-19 09:25:38 +010044 int n = atomic_inc_return(select_skb_count(skb));
David Howells9a36a6b2022-10-21 15:31:21 +010045 trace_rxrpc_skb(skb, refcount_read(&skb->users), n, why);
David Howellsdf844fd2016-08-23 15:27:24 +010046 skb_get(skb);
47}
48
49/*
David Howellsd0d5c0c2019-08-27 10:13:46 +010050 * Note the dropping of a ref on a socket buffer by the core.
51 */
David Howells9a36a6b2022-10-21 15:31:21 +010052void rxrpc_eaten_skb(struct sk_buff *skb, enum rxrpc_skb_trace why)
David Howellsd0d5c0c2019-08-27 10:13:46 +010053{
David Howellsd0d5c0c2019-08-27 10:13:46 +010054 int n = atomic_inc_return(&rxrpc_n_rx_skbs);
David Howells9a36a6b2022-10-21 15:31:21 +010055 trace_rxrpc_skb(skb, 0, n, why);
David Howellsd0d5c0c2019-08-27 10:13:46 +010056}
57
58/*
David Howellsdf844fd2016-08-23 15:27:24 +010059 * Note the destruction of a socket buffer.
60 */
David Howells9a36a6b2022-10-21 15:31:21 +010061void rxrpc_free_skb(struct sk_buff *skb, enum rxrpc_skb_trace why)
David Howellsdf844fd2016-08-23 15:27:24 +010062{
David Howellsdf844fd2016-08-23 15:27:24 +010063 if (skb) {
David Howells9a36a6b2022-10-21 15:31:21 +010064 int n = atomic_dec_return(select_skb_count(skb));
65 trace_rxrpc_skb(skb, refcount_read(&skb->users), n, why);
David Howells16d56772023-02-07 22:11:30 +000066 consume_skb(skb);
David Howellsdf844fd2016-08-23 15:27:24 +010067 }
68}
69
70/*
71 * Clear a queue of socket buffers.
72 */
73void rxrpc_purge_queue(struct sk_buff_head *list)
74{
David Howellsdf844fd2016-08-23 15:27:24 +010075 struct sk_buff *skb;
David Howells9a36a6b2022-10-21 15:31:21 +010076
David Howellsdf844fd2016-08-23 15:27:24 +010077 while ((skb = skb_dequeue((list))) != NULL) {
David Howells987db9f2019-08-19 09:25:38 +010078 int n = atomic_dec_return(select_skb_count(skb));
David Howells9a36a6b2022-10-21 15:31:21 +010079 trace_rxrpc_skb(skb, refcount_read(&skb->users), n,
80 rxrpc_skb_put_purge);
David Howells16d56772023-02-07 22:11:30 +000081 consume_skb(skb);
David Howellsdf844fd2016-08-23 15:27:24 +010082 }
83}