powerpc/pmac: Add missing unlocks in error path
[linux-2.6/next.git] / kernel / trace / kmemtrace.c
bloba91da69f153ad0c859997356d53db6548006db7c
1 /*
2 * Memory allocator tracing
4 * Copyright (C) 2008 Eduard - Gabriel Munteanu
5 * Copyright (C) 2008 Pekka Enberg <penberg@cs.helsinki.fi>
6 * Copyright (C) 2008 Frederic Weisbecker <fweisbec@gmail.com>
7 */
9 #include <linux/tracepoint.h>
10 #include <linux/seq_file.h>
11 #include <linux/debugfs.h>
12 #include <linux/dcache.h>
13 #include <linux/fs.h>
15 #include <linux/kmemtrace.h>
17 #include "trace_output.h"
18 #include "trace.h"
20 /* Select an alternative, minimalistic output than the original one */
21 #define TRACE_KMEM_OPT_MINIMAL 0x1
23 static struct tracer_opt kmem_opts[] = {
24 /* Default disable the minimalistic output */
25 { TRACER_OPT(kmem_minimalistic, TRACE_KMEM_OPT_MINIMAL) },
26 { }
29 static struct tracer_flags kmem_tracer_flags = {
30 .val = 0,
31 .opts = kmem_opts
34 static struct trace_array *kmemtrace_array;
36 /* Trace allocations */
37 static inline void kmemtrace_alloc(enum kmemtrace_type_id type_id,
38 unsigned long call_site,
39 const void *ptr,
40 size_t bytes_req,
41 size_t bytes_alloc,
42 gfp_t gfp_flags,
43 int node)
45 struct ftrace_event_call *call = &event_kmem_alloc;
46 struct trace_array *tr = kmemtrace_array;
47 struct kmemtrace_alloc_entry *entry;
48 struct ring_buffer_event *event;
50 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry));
51 if (!event)
52 return;
54 entry = ring_buffer_event_data(event);
55 tracing_generic_entry_update(&entry->ent, 0, 0);
57 entry->ent.type = TRACE_KMEM_ALLOC;
58 entry->type_id = type_id;
59 entry->call_site = call_site;
60 entry->ptr = ptr;
61 entry->bytes_req = bytes_req;
62 entry->bytes_alloc = bytes_alloc;
63 entry->gfp_flags = gfp_flags;
64 entry->node = node;
66 if (!filter_check_discard(call, entry, tr->buffer, event))
67 ring_buffer_unlock_commit(tr->buffer, event);
69 trace_wake_up();
72 static inline void kmemtrace_free(enum kmemtrace_type_id type_id,
73 unsigned long call_site,
74 const void *ptr)
76 struct ftrace_event_call *call = &event_kmem_free;
77 struct trace_array *tr = kmemtrace_array;
78 struct kmemtrace_free_entry *entry;
79 struct ring_buffer_event *event;
81 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry));
82 if (!event)
83 return;
84 entry = ring_buffer_event_data(event);
85 tracing_generic_entry_update(&entry->ent, 0, 0);
87 entry->ent.type = TRACE_KMEM_FREE;
88 entry->type_id = type_id;
89 entry->call_site = call_site;
90 entry->ptr = ptr;
92 if (!filter_check_discard(call, entry, tr->buffer, event))
93 ring_buffer_unlock_commit(tr->buffer, event);
95 trace_wake_up();
98 static void kmemtrace_kmalloc(unsigned long call_site,
99 const void *ptr,
100 size_t bytes_req,
101 size_t bytes_alloc,
102 gfp_t gfp_flags)
104 kmemtrace_alloc(KMEMTRACE_TYPE_KMALLOC, call_site, ptr,
105 bytes_req, bytes_alloc, gfp_flags, -1);
108 static void kmemtrace_kmem_cache_alloc(unsigned long call_site,
109 const void *ptr,
110 size_t bytes_req,
111 size_t bytes_alloc,
112 gfp_t gfp_flags)
114 kmemtrace_alloc(KMEMTRACE_TYPE_CACHE, call_site, ptr,
115 bytes_req, bytes_alloc, gfp_flags, -1);
118 static void kmemtrace_kmalloc_node(unsigned long call_site,
119 const void *ptr,
120 size_t bytes_req,
121 size_t bytes_alloc,
122 gfp_t gfp_flags,
123 int node)
125 kmemtrace_alloc(KMEMTRACE_TYPE_KMALLOC, call_site, ptr,
126 bytes_req, bytes_alloc, gfp_flags, node);
129 static void kmemtrace_kmem_cache_alloc_node(unsigned long call_site,
130 const void *ptr,
131 size_t bytes_req,
132 size_t bytes_alloc,
133 gfp_t gfp_flags,
134 int node)
136 kmemtrace_alloc(KMEMTRACE_TYPE_CACHE, call_site, ptr,
137 bytes_req, bytes_alloc, gfp_flags, node);
140 static void kmemtrace_kfree(unsigned long call_site, const void *ptr)
142 kmemtrace_free(KMEMTRACE_TYPE_KMALLOC, call_site, ptr);
145 static void kmemtrace_kmem_cache_free(unsigned long call_site, const void *ptr)
147 kmemtrace_free(KMEMTRACE_TYPE_CACHE, call_site, ptr);
150 static int kmemtrace_start_probes(void)
152 int err;
154 err = register_trace_kmalloc(kmemtrace_kmalloc);
155 if (err)
156 return err;
157 err = register_trace_kmem_cache_alloc(kmemtrace_kmem_cache_alloc);
158 if (err)
159 return err;
160 err = register_trace_kmalloc_node(kmemtrace_kmalloc_node);
161 if (err)
162 return err;
163 err = register_trace_kmem_cache_alloc_node(kmemtrace_kmem_cache_alloc_node);
164 if (err)
165 return err;
166 err = register_trace_kfree(kmemtrace_kfree);
167 if (err)
168 return err;
169 err = register_trace_kmem_cache_free(kmemtrace_kmem_cache_free);
171 return err;
174 static void kmemtrace_stop_probes(void)
176 unregister_trace_kmalloc(kmemtrace_kmalloc);
177 unregister_trace_kmem_cache_alloc(kmemtrace_kmem_cache_alloc);
178 unregister_trace_kmalloc_node(kmemtrace_kmalloc_node);
179 unregister_trace_kmem_cache_alloc_node(kmemtrace_kmem_cache_alloc_node);
180 unregister_trace_kfree(kmemtrace_kfree);
181 unregister_trace_kmem_cache_free(kmemtrace_kmem_cache_free);
184 static int kmem_trace_init(struct trace_array *tr)
186 kmemtrace_array = tr;
188 tracing_reset_online_cpus(tr);
190 kmemtrace_start_probes();
192 return 0;
195 static void kmem_trace_reset(struct trace_array *tr)
197 kmemtrace_stop_probes();
200 static void kmemtrace_headers(struct seq_file *s)
202 /* Don't need headers for the original kmemtrace output */
203 if (!(kmem_tracer_flags.val & TRACE_KMEM_OPT_MINIMAL))
204 return;
206 seq_printf(s, "#\n");
207 seq_printf(s, "# ALLOC TYPE REQ GIVEN FLAGS "
208 " POINTER NODE CALLER\n");
209 seq_printf(s, "# FREE | | | | "
210 " | | | |\n");
211 seq_printf(s, "# |\n\n");
215 * The following functions give the original output from kmemtrace,
216 * plus the origin CPU, since reordering occurs in-kernel now.
219 #define KMEMTRACE_USER_ALLOC 0
220 #define KMEMTRACE_USER_FREE 1
222 struct kmemtrace_user_event {
223 u8 event_id;
224 u8 type_id;
225 u16 event_size;
226 u32 cpu;
227 u64 timestamp;
228 unsigned long call_site;
229 unsigned long ptr;
232 struct kmemtrace_user_event_alloc {
233 size_t bytes_req;
234 size_t bytes_alloc;
235 unsigned gfp_flags;
236 int node;
239 static enum print_line_t
240 kmemtrace_print_alloc(struct trace_iterator *iter, int flags)
242 struct trace_seq *s = &iter->seq;
243 struct kmemtrace_alloc_entry *entry;
244 int ret;
246 trace_assign_type(entry, iter->ent);
248 ret = trace_seq_printf(s, "type_id %d call_site %pF ptr %lu "
249 "bytes_req %lu bytes_alloc %lu gfp_flags %lu node %d\n",
250 entry->type_id, (void *)entry->call_site, (unsigned long)entry->ptr,
251 (unsigned long)entry->bytes_req, (unsigned long)entry->bytes_alloc,
252 (unsigned long)entry->gfp_flags, entry->node);
254 if (!ret)
255 return TRACE_TYPE_PARTIAL_LINE;
256 return TRACE_TYPE_HANDLED;
259 static enum print_line_t
260 kmemtrace_print_free(struct trace_iterator *iter, int flags)
262 struct trace_seq *s = &iter->seq;
263 struct kmemtrace_free_entry *entry;
264 int ret;
266 trace_assign_type(entry, iter->ent);
268 ret = trace_seq_printf(s, "type_id %d call_site %pF ptr %lu\n",
269 entry->type_id, (void *)entry->call_site,
270 (unsigned long)entry->ptr);
272 if (!ret)
273 return TRACE_TYPE_PARTIAL_LINE;
274 return TRACE_TYPE_HANDLED;
277 static enum print_line_t
278 kmemtrace_print_alloc_user(struct trace_iterator *iter, int flags)
280 struct trace_seq *s = &iter->seq;
281 struct kmemtrace_alloc_entry *entry;
282 struct kmemtrace_user_event *ev;
283 struct kmemtrace_user_event_alloc *ev_alloc;
285 trace_assign_type(entry, iter->ent);
287 ev = trace_seq_reserve(s, sizeof(*ev));
288 if (!ev)
289 return TRACE_TYPE_PARTIAL_LINE;
291 ev->event_id = KMEMTRACE_USER_ALLOC;
292 ev->type_id = entry->type_id;
293 ev->event_size = sizeof(*ev) + sizeof(*ev_alloc);
294 ev->cpu = iter->cpu;
295 ev->timestamp = iter->ts;
296 ev->call_site = entry->call_site;
297 ev->ptr = (unsigned long)entry->ptr;
299 ev_alloc = trace_seq_reserve(s, sizeof(*ev_alloc));
300 if (!ev_alloc)
301 return TRACE_TYPE_PARTIAL_LINE;
303 ev_alloc->bytes_req = entry->bytes_req;
304 ev_alloc->bytes_alloc = entry->bytes_alloc;
305 ev_alloc->gfp_flags = entry->gfp_flags;
306 ev_alloc->node = entry->node;
308 return TRACE_TYPE_HANDLED;
311 static enum print_line_t
312 kmemtrace_print_free_user(struct trace_iterator *iter, int flags)
314 struct trace_seq *s = &iter->seq;
315 struct kmemtrace_free_entry *entry;
316 struct kmemtrace_user_event *ev;
318 trace_assign_type(entry, iter->ent);
320 ev = trace_seq_reserve(s, sizeof(*ev));
321 if (!ev)
322 return TRACE_TYPE_PARTIAL_LINE;
324 ev->event_id = KMEMTRACE_USER_FREE;
325 ev->type_id = entry->type_id;
326 ev->event_size = sizeof(*ev);
327 ev->cpu = iter->cpu;
328 ev->timestamp = iter->ts;
329 ev->call_site = entry->call_site;
330 ev->ptr = (unsigned long)entry->ptr;
332 return TRACE_TYPE_HANDLED;
335 /* The two other following provide a more minimalistic output */
336 static enum print_line_t
337 kmemtrace_print_alloc_compress(struct trace_iterator *iter)
339 struct kmemtrace_alloc_entry *entry;
340 struct trace_seq *s = &iter->seq;
341 int ret;
343 trace_assign_type(entry, iter->ent);
345 /* Alloc entry */
346 ret = trace_seq_printf(s, " + ");
347 if (!ret)
348 return TRACE_TYPE_PARTIAL_LINE;
350 /* Type */
351 switch (entry->type_id) {
352 case KMEMTRACE_TYPE_KMALLOC:
353 ret = trace_seq_printf(s, "K ");
354 break;
355 case KMEMTRACE_TYPE_CACHE:
356 ret = trace_seq_printf(s, "C ");
357 break;
358 case KMEMTRACE_TYPE_PAGES:
359 ret = trace_seq_printf(s, "P ");
360 break;
361 default:
362 ret = trace_seq_printf(s, "? ");
365 if (!ret)
366 return TRACE_TYPE_PARTIAL_LINE;
368 /* Requested */
369 ret = trace_seq_printf(s, "%4zu ", entry->bytes_req);
370 if (!ret)
371 return TRACE_TYPE_PARTIAL_LINE;
373 /* Allocated */
374 ret = trace_seq_printf(s, "%4zu ", entry->bytes_alloc);
375 if (!ret)
376 return TRACE_TYPE_PARTIAL_LINE;
378 /* Flags
379 * TODO: would be better to see the name of the GFP flag names
381 ret = trace_seq_printf(s, "%08x ", entry->gfp_flags);
382 if (!ret)
383 return TRACE_TYPE_PARTIAL_LINE;
385 /* Pointer to allocated */
386 ret = trace_seq_printf(s, "0x%tx ", (ptrdiff_t)entry->ptr);
387 if (!ret)
388 return TRACE_TYPE_PARTIAL_LINE;
390 /* Node and call site*/
391 ret = trace_seq_printf(s, "%4d %pf\n", entry->node,
392 (void *)entry->call_site);
393 if (!ret)
394 return TRACE_TYPE_PARTIAL_LINE;
396 return TRACE_TYPE_HANDLED;
399 static enum print_line_t
400 kmemtrace_print_free_compress(struct trace_iterator *iter)
402 struct kmemtrace_free_entry *entry;
403 struct trace_seq *s = &iter->seq;
404 int ret;
406 trace_assign_type(entry, iter->ent);
408 /* Free entry */
409 ret = trace_seq_printf(s, " - ");
410 if (!ret)
411 return TRACE_TYPE_PARTIAL_LINE;
413 /* Type */
414 switch (entry->type_id) {
415 case KMEMTRACE_TYPE_KMALLOC:
416 ret = trace_seq_printf(s, "K ");
417 break;
418 case KMEMTRACE_TYPE_CACHE:
419 ret = trace_seq_printf(s, "C ");
420 break;
421 case KMEMTRACE_TYPE_PAGES:
422 ret = trace_seq_printf(s, "P ");
423 break;
424 default:
425 ret = trace_seq_printf(s, "? ");
428 if (!ret)
429 return TRACE_TYPE_PARTIAL_LINE;
431 /* Skip requested/allocated/flags */
432 ret = trace_seq_printf(s, " ");
433 if (!ret)
434 return TRACE_TYPE_PARTIAL_LINE;
436 /* Pointer to allocated */
437 ret = trace_seq_printf(s, "0x%tx ", (ptrdiff_t)entry->ptr);
438 if (!ret)
439 return TRACE_TYPE_PARTIAL_LINE;
441 /* Skip node and print call site*/
442 ret = trace_seq_printf(s, " %pf\n", (void *)entry->call_site);
443 if (!ret)
444 return TRACE_TYPE_PARTIAL_LINE;
446 return TRACE_TYPE_HANDLED;
449 static enum print_line_t kmemtrace_print_line(struct trace_iterator *iter)
451 struct trace_entry *entry = iter->ent;
453 if (!(kmem_tracer_flags.val & TRACE_KMEM_OPT_MINIMAL))
454 return TRACE_TYPE_UNHANDLED;
456 switch (entry->type) {
457 case TRACE_KMEM_ALLOC:
458 return kmemtrace_print_alloc_compress(iter);
459 case TRACE_KMEM_FREE:
460 return kmemtrace_print_free_compress(iter);
461 default:
462 return TRACE_TYPE_UNHANDLED;
466 static struct trace_event kmem_trace_alloc = {
467 .type = TRACE_KMEM_ALLOC,
468 .trace = kmemtrace_print_alloc,
469 .binary = kmemtrace_print_alloc_user,
472 static struct trace_event kmem_trace_free = {
473 .type = TRACE_KMEM_FREE,
474 .trace = kmemtrace_print_free,
475 .binary = kmemtrace_print_free_user,
478 static struct tracer kmem_tracer __read_mostly = {
479 .name = "kmemtrace",
480 .init = kmem_trace_init,
481 .reset = kmem_trace_reset,
482 .print_line = kmemtrace_print_line,
483 .print_header = kmemtrace_headers,
484 .flags = &kmem_tracer_flags
487 void kmemtrace_init(void)
489 /* earliest opportunity to start kmem tracing */
492 static int __init init_kmem_tracer(void)
494 if (!register_ftrace_event(&kmem_trace_alloc)) {
495 pr_warning("Warning: could not register kmem events\n");
496 return 1;
499 if (!register_ftrace_event(&kmem_trace_free)) {
500 pr_warning("Warning: could not register kmem events\n");
501 return 1;
504 if (register_tracer(&kmem_tracer) != 0) {
505 pr_warning("Warning: could not register the kmem tracer\n");
506 return 1;
509 return 0;
511 device_initcall(init_kmem_tracer);