summaryrefslogtreecommitdiffstats
path: root/mm/kmemtrace.c
blob: f7704f52c4b137ceb41540d6497695189d0e044a (plain)
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
/*
 * Copyright (C) 2008 Pekka Enberg, Eduard - Gabriel Munteanu
 *
 * This file is released under GPL version 2.
 */

#include <linux/string.h>
#include <linux/debugfs.h>
#include <linux/relay.h>
#include <linux/module.h>
#include <linux/marker.h>
#include <linux/gfp.h>
#include <linux/kmemtrace.h>

#define KMEMTRACE_SUBBUF_SIZE		524288
#define KMEMTRACE_DEF_N_SUBBUFS		20

static struct rchan *kmemtrace_chan;
static u32 kmemtrace_buf_overruns;

static unsigned int kmemtrace_n_subbufs;

/* disabled by default */
static unsigned int kmemtrace_enabled;

/*
 * The sequence number is used for reordering kmemtrace packets
 * in userspace, since they are logged as per-CPU data.
 *
 * atomic_t should always be a 32-bit signed integer. Wraparound is not
 * likely to occur, but userspace can deal with it by expecting a certain
 * sequence number in the next packet that will be read.
 */
static atomic_t kmemtrace_seq_num;

#define KMEMTRACE_ABI_VERSION		1

static u32 kmemtrace_abi_version __read_mostly = KMEMTRACE_ABI_VERSION;

enum kmemtrace_event_id {
	KMEMTRACE_EVENT_ALLOC = 0,
	KMEMTRACE_EVENT_FREE,
};

struct kmemtrace_event {
	u8		event_id;
	u8		type_id;
	u16		event_size;
	s32		seq_num;
	u64		call_site;
	u64		ptr;
} __attribute__ ((__packed__));

struct kmemtrace_stats_alloc {
	u64		bytes_req;
	u64		bytes_alloc;
	u32		gfp_flags;
	s32		numa_node;
} __attribute__ ((__packed__));

static void kmemtrace_probe_alloc(void *probe_data, void *call_data,
				  const char *format, va_list *args)
{
	unsigned long flags;
	struct kmemtrace_event *ev;
	struct kmemtrace_stats_alloc *stats;
	void *buf;

	local_irq_save(flags);

	buf = relay_reserve(kmemtrace_chan,
			    sizeof(struct kmemtrace_event) +
			    sizeof(struct kmemtrace_stats_alloc));
	if (!buf)
		goto failed;

	/*
	 * Don't convert this to use structure initializers,
	 * C99 does not guarantee the rvalues evaluation order.
	 */

	ev = buf;
	ev->event_id = KMEMTRACE_EVENT_ALLOC;
	ev->type_id = va_arg(*args, int);
	ev->event_size = sizeof(struct kmemtrace_event) +
			 sizeof(struct kmemtrace_stats_alloc);
	ev->seq_num = atomic_add_return(1, &kmemtrace_seq_num);
	ev->call_site = va_arg(*args, unsigned long);
	ev->ptr = va_arg(*args, unsigned long);

	stats = buf + sizeof(struct kmemtrace_event);
	stats->bytes_req = va_arg(*args, unsigned long);
	stats->bytes_alloc = va_arg(*args, unsigned long);
	stats->gfp_flags = va_arg(*args, unsigned long);
	stats->numa_node = va_arg(*args, int);

failed:
	local_irq_restore(flags);
}

static void kmemtrace_probe_free(void *probe_data, void *call_data,
				 const char *format, va_list *args)
{
	unsigned long flags;
	struct kmemtrace_event *ev;

	local_irq_save(flags);

	ev = relay_reserve(kmemtrace_chan, sizeof(struct kmemtrace_event));
	if (!ev)
		goto failed;

	/*
	 * Don't convert this to use structure initializers,
	 * C99 does not guarantee the rvalues evaluation order.
	 */
	ev->event_id = KMEMTRACE_EVENT_FREE;
	ev->type_id = va_arg(*args, int);
	ev->event_size = sizeof(struct kmemtrace_event);
	ev->seq_num = atomic_add_return(1, &kmemtrace_seq_num);
	ev->call_site = va_arg(*args, unsigned long);
	ev->ptr = va_arg(*args, unsigned long);

failed:
	local_irq_restore(flags);
}

static struct dentry *
kmemtrace_create_buf_file(const char *filename, struct dentry *parent,
			  int mode, struct rchan_buf *buf, int *is_global)
{
	return debugfs_create_file(filename, mode, parent, buf,
				   &relay_file_operations);
}

static int kmemtrace_remove_buf_file(struct dentry *dentry)
{
	debugfs_remove(dentry);

	return 0;
}

static int kmemtrace_subbuf_start(struct rchan_buf *buf,
				  void *subbuf,
				  void *prev_subbuf,
				  size_t prev_padding)
{
	if (relay_buf_full(buf)) {
		/*
		 * We know it's not SMP-safe, but neither
		 * debugfs_create_u32() is.
		 */
		kmemtrace_buf_overruns++;
		return 0;
	}

	return 1;
}

static struct rchan_callbacks relay_callbacks = {
	.create_buf_file = kmemtrace_create_buf_file,
	.remove_buf_file = kmemtrace_remove_buf_file,
	.subbuf_start = kmemtrace_subbuf_start,
};

static struct dentry *kmemtrace_dir;
static struct dentry *kmemtrace_overruns_dentry;
static struct dentry *kmemtrace_abi_version_dentry;

static struct dentry *kmemtrace_enabled_dentry;

static int kmemtrace_start_probes(void)
{
	int err;

	err = marker_probe_register("kmemtrace_alloc", "type_id %d "
				    "call_site %lu ptr %lu "
				    "bytes_req %lu bytes_alloc %lu "
				    "gfp_flags %lu node %d",
				    kmemtrace_probe_alloc, NULL);
	if (err)
		return err;
	err = marker_probe_register("kmemtrace_free", "type_id %d "
				    "call_site %lu ptr %lu",
				    kmemtrace_probe_free, NULL);

	return err;
}

static void kmemtrace_stop_probes(void)
{
	marker_probe_unregister("kmemtrace_alloc",
				kmemtrace_probe_alloc, NULL);
	marker_probe_unregister("kmemtrace_free",
				kmemtrace_probe_free, NULL);
}

static int kmemtrace_enabled_get(void *data, u64 *val)
{
	*val = *((int *) data);

	return 0;
}

static int kmemtrace_enabled_set(void *data, u64 val)
{
	u64 old_val = kmemtrace_enabled;

	*((int *) data) = !!val;

	if (old_val == val)
		return 0;
	if (val)
		kmemtrace_start_probes();
	else
		kmemtrace_stop_probes();

	return 0;
}

DEFINE_SIMPLE_ATTRIBUTE(kmemtrace_enabled_fops,
			kmemtrace_enabled_get,
			kmemtrace_enabled_set, "%llu\n");

static void kmemtrace_cleanup(void)
{
	if (kmemtrace_enabled_dentry)
		debugfs_remove(kmemtrace_enabled_dentry);

	kmemtrace_stop_probes();

	if (kmemtrace_abi_version_dentry)
		debugfs_remove(kmemtrace_abi_version_dentry);
	if (kmemtrace_overruns_dentry)
		debugfs_remove(kmemtrace_overruns_dentry);

	relay_close(kmemtrace_chan);
	kmemtrace_chan = NULL;

	if (kmemtrace_dir)
		debugfs_remove(kmemtrace_dir);
}

static int __init kmemtrace_setup_late(void)
{
	if (!kmemtrace_chan)
		goto failed;

	kmemtrace_dir = debugfs_create_dir("kmemtrace", NULL);
	if (!kmemtrace_dir)
		goto cleanup;

	kmemtrace_abi_version_dentry =
		debugfs_create_u32("abi_version", S_IRUSR,
				   kmemtrace_dir, &kmemtrace_abi_version);
	kmemtrace_overruns_dentry =
		debugfs_create_u32("total_overruns", S_IRUSR,
				   kmemtrace_dir, &kmemtrace_buf_overruns);
	if (!kmemtrace_overruns_dentry || !kmemtrace_abi_version_dentry)
		goto cleanup;

	kmemtrace_enabled_dentry =
		debugfs_create_file("enabled", S_IRUSR | S_IWUSR,
				    kmemtrace_dir, &kmemtrace_enabled,
				    &kmemtrace_enabled_fops);
	if (!kmemtrace_enabled_dentry)
		goto cleanup;

	if (relay_late_setup_files(kmemtrace_chan, "cpu", kmemtrace_dir))
		goto cleanup;

	printk(KERN_INFO "kmemtrace: fully up.\n");

	return 0;

cleanup:
	kmemtrace_cleanup();
failed:
	return 1;
}
late_initcall(kmemtrace_setup_late);

static int __init kmemtrace_set_boot_enabled(char *str)
{
	if (!str)
		return -EINVAL;

	if (!strcmp(str, "yes"))
		kmemtrace_enabled = 1;
	else if (!strcmp(str, "no"))
		kmemtrace_enabled = 0;
	else
		return -EINVAL;

	return 0;
}
early_param("kmemtrace.enable", kmemtrace_set_boot_enabled);

static int __init kmemtrace_set_subbufs(char *str)
{
	get_option(&str, &kmemtrace_n_subbufs);
	return 0;
}
early_param("kmemtrace.subbufs", kmemtrace_set_subbufs);

void kmemtrace_init(void)
{
	if (!kmemtrace_n_subbufs)
		kmemtrace_n_subbufs = KMEMTRACE_DEF_N_SUBBUFS;

	kmemtrace_chan = relay_open(NULL, NULL, KMEMTRACE_SUBBUF_SIZE,
				    kmemtrace_n_subbufs, &relay_callbacks,
				    NULL);
	if (!kmemtrace_chan) {
		printk(KERN_ERR "kmemtrace: could not open relay channel.\n");
		return;
	}

	if (!kmemtrace_enabled) {
		printk(KERN_INFO "kmemtrace: disabled. Pass "
			"kemtrace.enable=yes as kernel parameter for "
			"boot-time tracing.");
		return;
	}
	if (kmemtrace_start_probes()) {
		printk(KERN_ERR "kmemtrace: could not register marker probes!\n");
		kmemtrace_cleanup();
		return;
	}

	printk(KERN_INFO "kmemtrace: enabled.\n");
}

OpenPOWER on IntegriCloud