-
Notifications
You must be signed in to change notification settings - Fork 0
/
0001-kmalloc-accounting.patch
executable file
·371 lines (358 loc) · 11.7 KB
/
0001-kmalloc-accounting.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
From b0d595c2e087e083b7f1404ffdc0d84cf6421082 Mon Sep 17 00:00:00 2001
From: "vishnu.ps" <[email protected]>
Date: Tue, 24 Dec 2013 18:06:57 +0530
Subject: [PATCH] kmalloc accounting
Change-Id: Ic5a7888f19c7c83a19b43a227d57631d388e38f0
Signed-off-by: vishnu.ps <[email protected]>
---
arch/arm/configs/mint-vlx-rev03_defconfig | 3 +-
include/linux/slub_def.h | 24 +++-
init/Kconfig | 8 ++
mm/Makefile | 1 +
mm/kmallocacct.c | 184 ++++++++++++++++++++++++++++++
mm/slub.c | 3 +-
mm/vmstat.c | 22 ++++
7 files changed, 242 insertions(+), 3 deletions(-)
create mode 100644 mm/kmallocacct.c
diff --git a/arch/arm/configs/mint-vlx-rev03_defconfig b/arch/arm/configs/mint-vlx-rev03_defconfig
index 307024b..c5d969b 100755
--- a/arch/arm/configs/mint-vlx-rev03_defconfig
+++ b/arch/arm/configs/mint-vlx-rev03_defconfig
@@ -142,7 +142,8 @@ CONFIG_PERF_EVENTS=y
CONFIG_VM_EVENT_COUNTERS=y
CONFIG_COMPAT_BRK=y
CONFIG_SLAB=y
-# CONFIG_SLUB is not set
+CONFIG_SLUB=y
+CONFIG_KMALLOC_ACCOUNTING=y
# CONFIG_SLOB is not set
# CONFIG_PROFILING is not set
# CONFIG_KPROBES is not set
diff --git a/include/linux/slub_def.h b/include/linux/slub_def.h
index c8668d1..df875c7 100644
--- a/include/linux/slub_def.h
+++ b/include/linux/slub_def.h
@@ -143,6 +143,23 @@ struct kmem_cache {
#define SLUB_DMA (__force gfp_t)0
#endif
+#ifdef CONFIG_KMALLOC_ACCOUNTING
+void __kmalloc_account(const void *, const void *, int, int);
+
+static void inline kmalloc_account(const void *addr, int size, int req)
+{
+ __kmalloc_account(__builtin_return_address(0), addr, size, req);
+}
+
+static void inline kfree_account(const void *addr, int size)
+{
+ __kmalloc_account(__builtin_return_address(0), addr, size, -1);
+}
+#else
+#define kmalloc_account(a, b, c)
+#define kfree_account(a, b)
+#endif
+
/*
* We keep the general caches in an array of slab caches that are used for
* 2^x bytes of allocations.
@@ -248,12 +265,16 @@ kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
static __always_inline void *kmalloc_large(size_t size, gfp_t flags)
{
+ void *ret;
unsigned int order = get_order(size);
- return kmalloc_order_trace(size, flags, order);
+ ret = kmalloc_order_trace(size, flags, order);
+ kmalloc_account(ret, size, size);
+ return ret;
}
static __always_inline void *kmalloc(size_t size, gfp_t flags)
{
+#ifndef CONFIG_KMALLOC_ACCOUNTING
if (__builtin_constant_p(size)) {
if (size > SLUB_MAX_SIZE)
return kmalloc_large(size, flags);
@@ -267,6 +288,7 @@ static __always_inline void *kmalloc(size_t size, gfp_t flags)
return kmem_cache_alloc_trace(s, flags, size);
}
}
+#endif
return __kmalloc(size, flags);
}
diff --git a/init/Kconfig b/init/Kconfig
index ad0a30f..f28d649 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1039,6 +1039,14 @@ config PCSPKR_PLATFORM
This option allows to disable the internal PC-Speaker
support, saving some memory.
+config KMALLOC_ACCOUNTING
+ default n
+ bool "Enabled accounting of kmalloc/kfree allocations" if EMBEDDED
+ help
+ This option records kmalloc and kfree activity and reports it via
+ /proc/kmalloc.
+
+
config BASE_FULL
default y
bool "Enable full-sized data structures for core" if EXPERT
diff --git a/mm/Makefile b/mm/Makefile
index 70e47dc..43cae0d 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -24,6 +24,7 @@ endif
obj-$(CONFIG_HAVE_MEMBLOCK) += memblock.o
obj-$(CONFIG_BOUNCE) += bounce.o
+obj-$(CONFIG_KMALLOC_ACCOUNTING) += kmallocacct.o
obj-$(CONFIG_SWAP) += page_io.o swap_state.o swapfile.o thrash.o
obj-$(CONFIG_HAS_DMA) += dmapool.o
obj-$(CONFIG_HUGETLBFS) += hugetlb.o
diff --git a/mm/kmallocacct.c b/mm/kmallocacct.c
new file mode 100644
index 0000000..3a65bb7
--- /dev/null
+++ b/mm/kmallocacct.c
@@ -0,0 +1,184 @@
+//#include <linux/Kconfig.h>
+#include <linux/seq_file.h>
+#include <linux/kallsyms.h>
+#include <linux/spinlock.h>
+
+struct kma_caller {
+ const void *caller;
+ long int total, net, slack, allocs, frees;
+};
+
+struct kma_list {
+ int callerhash;
+ const void *address;
+};
+
+#define MAX_CALLER_TABLE 512
+#define MAX_ALLOC_TRACK 2048
+
+#define kma_hash(address, size) (((u32)address / (u32)size) % size)
+
+static struct kma_list kma_alloc[MAX_ALLOC_TRACK];
+static struct kma_caller kma_caller[MAX_CALLER_TABLE];
+
+static int kma_callers;
+static int kma_lost_callers, kma_lost_allocs, kma_unknown_frees;
+static long int kma_total, kma_net, kma_slack, kma_allocs, kma_frees;
+static DEFINE_SPINLOCK(kma_lock);
+
+void __kmalloc_account(const void *caller, const void *addr, int size, int req)
+{
+ int i, hasha, hashc;
+ unsigned long flags;
+
+ spin_lock_irqsave(&kma_lock, flags);
+ if(req >= 0) /* kmalloc */
+ {
+ /* find callers slot */
+ hashc = kma_hash(caller, MAX_CALLER_TABLE);
+ for (i = 0; i < MAX_CALLER_TABLE; i++) {
+ if (!kma_caller[hashc].caller ||
+ kma_caller[hashc].caller == caller)
+ break;
+ hashc = (hashc + 1) % MAX_CALLER_TABLE;
+ }
+
+ if (!kma_caller[hashc].caller)
+ kma_callers++;
+
+ if (i < MAX_CALLER_TABLE) {
+ /* update callers stats */
+ kma_caller[hashc].caller = caller;
+ kma_caller[hashc].total += size;
+ kma_caller[hashc].net += size;
+ kma_caller[hashc].slack += size - req;
+ kma_caller[hashc].allocs++;
+
+ /* add malloc to list */
+ hasha = kma_hash(addr, MAX_ALLOC_TRACK);
+ for (i = 0; i < MAX_ALLOC_TRACK; i++) {
+ if (!kma_alloc[hasha].callerhash)
+ break;
+ hasha = (hasha + 1) % MAX_ALLOC_TRACK;
+ }
+
+ if(i < MAX_ALLOC_TRACK) {
+ kma_alloc[hasha].callerhash = hashc;
+ kma_alloc[hasha].address = addr;
+ }
+ else
+ kma_lost_allocs++;
+ }
+ else {
+ kma_lost_callers++;
+ kma_lost_allocs++;
+ }
+
+ kma_total += size;
+ kma_net += size;
+ kma_slack += size - req;
+ kma_allocs++;
+ }
+ else { /* kfree */
+ hasha = kma_hash(addr, MAX_ALLOC_TRACK);
+ for (i = 0; i < MAX_ALLOC_TRACK ; i++) {
+ if (kma_alloc[hasha].address == addr)
+ break;
+ hasha = (hasha + 1) % MAX_ALLOC_TRACK;
+ }
+
+ if (i < MAX_ALLOC_TRACK) {
+ hashc = kma_alloc[hasha].callerhash;
+ kma_alloc[hasha].callerhash = 0;
+ kma_caller[hashc].net -= size;
+ kma_caller[hashc].frees++;
+ }
+ else
+ kma_unknown_frees++;
+
+ kma_net -= size;
+ kma_frees++;
+ }
+ spin_unlock_irqrestore(&kma_lock, flags);
+}
+
+static void *as_start(struct seq_file *m, loff_t *pos)
+{
+ int i;
+ loff_t n = *pos;
+
+ if (!n) {
+ seq_printf(m, "total bytes allocated: %16ld\n", kma_total);
+ seq_printf(m, "slack bytes allocated: %16ld\n", kma_slack);
+ seq_printf(m, "net bytes allocated: %16ld\n", kma_net);
+ seq_printf(m, "number of allocs: %16ld\n", kma_allocs);
+ seq_printf(m, "number of frees: %16ld\n", kma_frees);
+ seq_printf(m, "number of callers: %8d\n", kma_callers);
+ seq_printf(m, "lost callers: %8d\n",
+ kma_lost_callers);
+ seq_printf(m, "lost allocs: %8d\n",
+ kma_lost_allocs);
+ seq_printf(m, "unknown frees: %8d\n",
+ kma_unknown_frees);
+ seq_puts(m, "\n total slack net count-alloc/free caller\n");
+ }
+
+ for (i = 0; i < MAX_CALLER_TABLE; i++) {
+ if(kma_caller[i].caller)
+ n--;
+ if(n < 0)
+ return (void *)(i+1);
+ }
+
+ return 0;
+}
+
+
+static void *as_next(struct seq_file *m, void *p, loff_t *pos)
+{
+ int n = (int)p-1, i;
+ ++*pos;
+
+ for (i = n + 1; i < MAX_CALLER_TABLE; i++)
+ if(kma_caller[i].caller)
+ return (void *)(i+1);
+
+ return 0;
+}
+
+static void as_stop(struct seq_file *m, void *p)
+{
+}
+
+static int as_show(struct seq_file *m, void *p)
+{
+ int n = (int)p-1;
+ struct kma_caller *c;
+#ifdef CONFIG_KALLSYMS
+ char *modname;
+ const char *name;
+ unsigned long offset = 0, size;
+ char namebuf[128];
+
+ c = &kma_caller[n];
+ name = kallsyms_lookup((int)c->caller, &size, &offset, &modname,
+ namebuf);
+ seq_printf(m, "%16ld %16ld %16ld %10ld/%-10ld %s+0x%lx\n",
+ c->total, c->slack, c->net, c->allocs, c->frees,
+ name, offset);
+#else
+ c = &kma_caller[n];
+ seq_printf(m, "%16ld %16ld %16ld %5d/%-5d %p\n",
+ c->total, c->slack, c->net, c->allocs, c->frees, c->caller);
+#endif
+
+ return 0;
+}
+
+struct seq_operations kmalloc_account_op = {
+ .start = as_start,
+ .next = as_next,
+ .stop = as_stop,
+ .show = as_show,
+};
+
diff --git a/mm/slub.c b/mm/slub.c
index 35f351f..43816cf 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2861,7 +2861,7 @@ void *__kmalloc(size_t size, gfp_t flags)
ret = slab_alloc(s, flags, NUMA_NO_NODE, _RET_IP_);
trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
-
+ kmalloc_account(ret, s->size, size);
return ret;
}
EXPORT_SYMBOL(__kmalloc);
@@ -2933,6 +2933,7 @@ void kfree(const void *x)
struct page *page;
void *object = (void *)x;
+ kfree_account(object, ksize(object));
trace_kfree(_RET_IP_, x);
if (unlikely(ZERO_OR_NULL_PTR(x)))
diff --git a/mm/vmstat.c b/mm/vmstat.c
index ad1c213..dc458fa 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1138,6 +1138,25 @@ static const struct file_operations proc_vmstat_file_operations = {
};
#endif /* CONFIG_PROC_FS */
+#ifdef CONFIG_KMALLOC_ACCOUNTING
+
+extern struct seq_operations kmalloc_account_op;
+
+
+static int kmalloc_account_open(struct inode *inode, struct file *file)
+{
+ return seq_open(file, &kmalloc_account_op);
+}
+
+static const struct file_operations proc_kmalloc_account_operations = {
+ .open = kmalloc_account_open,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = seq_release,
+};
+#endif
+
+
#ifdef CONFIG_SMP
static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
int sysctl_stat_interval __read_mostly = HZ;
@@ -1213,6 +1232,9 @@ static int __init setup_vmstat(void)
proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
#endif
+#ifdef CONFIG_KMALLOC_ACCOUNTING
+ proc_create("kmalloc", S_IRUGO, NULL, &proc_kmalloc_account_operations);
+#endif
return 0;
}
module_init(setup_vmstat)
--
1.8.3.2