Add vmalloc sync all calls in ftrace and kprobes modules
[lttng-modules.git] / probes / lttng-ftrace.c
1 /*
2 * (C) Copyright 2009-2011 -
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * LTTng function tracer integration module.
6 *
7 * Dual LGPL v2.1/GPL v2 license.
8 */
9
10 /*
11 * Ftrace function tracer does not seem to provide synchronization between probe
12 * teardown and callback execution. Therefore, we make this module permanently
13 * loaded (unloadable).
14 */
15
16 #include <linux/module.h>
17 #include <linux/ftrace.h>
18 #include <linux/slab.h>
19 #include "../ltt-events.h"
20 #include "../wrapper/ringbuffer/frontend_types.h"
21 #include "../wrapper/ftrace.h"
22 #include "../wrapper/vmalloc.h"
23 #include "../ltt-tracer.h"
24
25 static
26 void lttng_ftrace_handler(unsigned long ip, unsigned long parent_ip, void **data)
27 {
28 struct ltt_event *event = *data;
29 struct ltt_channel *chan = event->chan;
30 struct lib_ring_buffer_ctx ctx;
31 struct {
32 unsigned long ip;
33 unsigned long parent_ip;
34 } payload;
35 int ret;
36
37 if (!ACCESS_ONCE(chan->session->active))
38 return;
39 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL,
40 sizeof(payload), ltt_alignof(payload), -1);
41 ret = chan->ops->event_reserve(&ctx);
42 if (ret < 0)
43 return;
44 payload.ip = ip;
45 payload.parent_ip = parent_ip;
46 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(payload));
47 chan->ops->event_write(&ctx, &payload, sizeof(payload));
48 chan->ops->event_commit(&ctx);
49 return;
50 }
51
52 /*
53 * Create event description
54 */
55 static
56 int lttng_create_ftrace_event(const char *name, struct ltt_event *event)
57 {
58 struct lttng_event_field *fields;
59 struct lttng_event_desc *desc;
60 int ret;
61
62 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
63 if (!desc)
64 return -ENOMEM;
65 desc->name = kstrdup(name, GFP_KERNEL);
66 if (!desc->name) {
67 ret = -ENOMEM;
68 goto error_str;
69 }
70 desc->nr_fields = 2;
71 desc->fields = fields =
72 kzalloc(2 * sizeof(struct lttng_event_field), GFP_KERNEL);
73 fields[0].name = "ip";
74 fields[0].type.atype = atype_integer;
75 fields[0].type.u.basic.integer.size = sizeof(unsigned long);
76 fields[0].type.u.basic.integer.alignment = ltt_alignof(unsigned long);
77 fields[0].type.u.basic.integer.signedness = 0;
78 fields[0].type.u.basic.integer.reverse_byte_order = 0;
79 fields[0].type.u.basic.integer.base = 16;
80 fields[0].type.u.basic.integer.encoding = lttng_encode_none;
81
82 fields[1].name = "parent_ip";
83 fields[1].type.atype = atype_integer;
84 fields[1].type.u.basic.integer.size = sizeof(unsigned long);
85 fields[1].type.u.basic.integer.alignment = ltt_alignof(unsigned long);
86 fields[1].type.u.basic.integer.signedness = 0;
87 fields[1].type.u.basic.integer.reverse_byte_order = 0;
88 fields[1].type.u.basic.integer.base = 16;
89 fields[1].type.u.basic.integer.encoding = lttng_encode_none;
90
91 event->desc = desc;
92
93 return 0;
94
95 error_str:
96 kfree(desc);
97 return ret;
98 }
99
100 static
101 struct ftrace_probe_ops lttng_ftrace_ops = {
102 .func = lttng_ftrace_handler,
103 };
104
105 int lttng_ftrace_register(const char *name,
106 const char *symbol_name,
107 struct ltt_event *event)
108 {
109 int ret;
110
111 ret = lttng_create_ftrace_event(name, event);
112 if (ret)
113 goto error;
114
115 event->u.ftrace.symbol_name = kstrdup(name, GFP_KERNEL);
116 if (!event->u.ftrace.symbol_name)
117 goto name_error;
118
119 /* Ensure the memory we just allocated don't trigger page faults */
120 wrapper_vmalloc_sync_all();
121
122 ret = wrapper_register_ftrace_function_probe(event->u.ftrace.symbol_name,
123 &lttng_ftrace_ops, event);
124 if (ret)
125 goto register_error;
126 return 0;
127
128 register_error:
129 kfree(event->u.ftrace.symbol_name);
130 name_error:
131 kfree(event->desc->name);
132 kfree(event->desc);
133 error:
134 return ret;
135 }
136 EXPORT_SYMBOL_GPL(lttng_ftrace_register);
137
138 void lttng_ftrace_unregister(struct ltt_event *event)
139 {
140 wrapper_unregister_ftrace_function_probe(event->u.ftrace.symbol_name,
141 &lttng_ftrace_ops, event);
142 kfree(event->u.ftrace.symbol_name);
143 kfree(event->desc->name);
144 kfree(event->desc);
145 }
146 EXPORT_SYMBOL_GPL(lttng_ftrace_unregister);
147
148 /* This module is permanent. */
149 int lttng_ftrace_init(void)
150 {
151 wrapper_vmalloc_sync_all();
152 return 0;
153 }
154 module_init(lttng_ftrace_init)
155
156 MODULE_LICENSE("GPL and additional rights");
157 MODULE_AUTHOR("Mathieu Desnoyers");
158 MODULE_DESCRIPTION("Linux Trace Toolkit Ftrace Support");
This page took 0.032513 seconds and 5 git commands to generate.