xref: /OK3568_Linux_fs/kernel/net/sctp/objcnt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* SCTP kernel implementation
3*4882a593Smuzhiyun  * (C) Copyright IBM Corp. 2001, 2004
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This file is part of the SCTP kernel implementation
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Support for memory object debugging.  This allows one to monitor the
8*4882a593Smuzhiyun  * object allocations/deallocations for types instrumented for this
9*4882a593Smuzhiyun  * via the proc fs.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * Please send any bug reports or fixes you make to the
12*4882a593Smuzhiyun  * email address(es):
13*4882a593Smuzhiyun  *    lksctp developers <linux-sctp@vger.kernel.org>
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * Written or modified by:
16*4882a593Smuzhiyun  *    Jon Grimm             <jgrimm@us.ibm.com>
17*4882a593Smuzhiyun  */
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include <linux/kernel.h>
22*4882a593Smuzhiyun #include <net/sctp/sctp.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * Global counters to count raw object allocation counts.
26*4882a593Smuzhiyun  * To add new counters, choose a unique suffix for the variable
27*4882a593Smuzhiyun  * name as the helper macros key off this suffix to make
28*4882a593Smuzhiyun  * life easier for the programmer.
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun SCTP_DBG_OBJCNT(sock);
32*4882a593Smuzhiyun SCTP_DBG_OBJCNT(ep);
33*4882a593Smuzhiyun SCTP_DBG_OBJCNT(transport);
34*4882a593Smuzhiyun SCTP_DBG_OBJCNT(assoc);
35*4882a593Smuzhiyun SCTP_DBG_OBJCNT(bind_addr);
36*4882a593Smuzhiyun SCTP_DBG_OBJCNT(bind_bucket);
37*4882a593Smuzhiyun SCTP_DBG_OBJCNT(chunk);
38*4882a593Smuzhiyun SCTP_DBG_OBJCNT(addr);
39*4882a593Smuzhiyun SCTP_DBG_OBJCNT(datamsg);
40*4882a593Smuzhiyun SCTP_DBG_OBJCNT(keys);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /* An array to make it easy to pretty print the debug information
43*4882a593Smuzhiyun  * to the proc fs.
44*4882a593Smuzhiyun  */
45*4882a593Smuzhiyun static struct sctp_dbg_objcnt_entry sctp_dbg_objcnt[] = {
46*4882a593Smuzhiyun 	SCTP_DBG_OBJCNT_ENTRY(sock),
47*4882a593Smuzhiyun 	SCTP_DBG_OBJCNT_ENTRY(ep),
48*4882a593Smuzhiyun 	SCTP_DBG_OBJCNT_ENTRY(assoc),
49*4882a593Smuzhiyun 	SCTP_DBG_OBJCNT_ENTRY(transport),
50*4882a593Smuzhiyun 	SCTP_DBG_OBJCNT_ENTRY(chunk),
51*4882a593Smuzhiyun 	SCTP_DBG_OBJCNT_ENTRY(bind_addr),
52*4882a593Smuzhiyun 	SCTP_DBG_OBJCNT_ENTRY(bind_bucket),
53*4882a593Smuzhiyun 	SCTP_DBG_OBJCNT_ENTRY(addr),
54*4882a593Smuzhiyun 	SCTP_DBG_OBJCNT_ENTRY(datamsg),
55*4882a593Smuzhiyun 	SCTP_DBG_OBJCNT_ENTRY(keys),
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /* Callback from procfs to read out objcount information.
59*4882a593Smuzhiyun  * Walk through the entries in the sctp_dbg_objcnt array, dumping
60*4882a593Smuzhiyun  * the raw object counts for each monitored type.
61*4882a593Smuzhiyun  */
sctp_objcnt_seq_show(struct seq_file * seq,void * v)62*4882a593Smuzhiyun static int sctp_objcnt_seq_show(struct seq_file *seq, void *v)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	int i;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	i = (int)*(loff_t *)v;
67*4882a593Smuzhiyun 	seq_setwidth(seq, 127);
68*4882a593Smuzhiyun 	seq_printf(seq, "%s: %d", sctp_dbg_objcnt[i].label,
69*4882a593Smuzhiyun 				atomic_read(sctp_dbg_objcnt[i].counter));
70*4882a593Smuzhiyun 	seq_pad(seq, '\n');
71*4882a593Smuzhiyun 	return 0;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
sctp_objcnt_seq_start(struct seq_file * seq,loff_t * pos)74*4882a593Smuzhiyun static void *sctp_objcnt_seq_start(struct seq_file *seq, loff_t *pos)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	return (*pos >= ARRAY_SIZE(sctp_dbg_objcnt)) ? NULL : (void *)pos;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
sctp_objcnt_seq_stop(struct seq_file * seq,void * v)79*4882a593Smuzhiyun static void sctp_objcnt_seq_stop(struct seq_file *seq, void *v)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
sctp_objcnt_seq_next(struct seq_file * seq,void * v,loff_t * pos)83*4882a593Smuzhiyun static void *sctp_objcnt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	++*pos;
86*4882a593Smuzhiyun 	return (*pos >= ARRAY_SIZE(sctp_dbg_objcnt)) ? NULL : (void *)pos;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun static const struct seq_operations sctp_objcnt_seq_ops = {
90*4882a593Smuzhiyun 	.start = sctp_objcnt_seq_start,
91*4882a593Smuzhiyun 	.next  = sctp_objcnt_seq_next,
92*4882a593Smuzhiyun 	.stop  = sctp_objcnt_seq_stop,
93*4882a593Smuzhiyun 	.show  = sctp_objcnt_seq_show,
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /* Initialize the objcount in the proc filesystem.  */
sctp_dbg_objcnt_init(struct net * net)97*4882a593Smuzhiyun void sctp_dbg_objcnt_init(struct net *net)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	struct proc_dir_entry *ent;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	ent = proc_create_seq("sctp_dbg_objcnt", 0,
102*4882a593Smuzhiyun 			  net->sctp.proc_net_sctp, &sctp_objcnt_seq_ops);
103*4882a593Smuzhiyun 	if (!ent)
104*4882a593Smuzhiyun 		pr_warn("sctp_dbg_objcnt: Unable to create /proc entry.\n");
105*4882a593Smuzhiyun }
106