Fix: use after free in ring buffer clients
[lttng-modules.git] / lttng-ring-buffer-metadata-client.h
1 /*
2 * lttng-ring-buffer-client.h
3 *
4 * LTTng lib ring buffer client template.
5 *
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
26 #include "lttng-events.h"
27 #include "lttng-tracer.h"
28
29 static struct lttng_transport lttng_relay_transport;
30
31 struct metadata_packet_header {
32 uint32_t magic; /* 0x75D11D57 */
33 uint8_t uuid[16]; /* Unique Universal Identifier */
34 uint32_t checksum; /* 0 if unused */
35 uint32_t content_size; /* in bits */
36 uint32_t packet_size; /* in bits */
37 uint8_t compression_scheme; /* 0 if unused */
38 uint8_t encryption_scheme; /* 0 if unused */
39 uint8_t checksum_scheme; /* 0 if unused */
40 uint8_t major; /* CTF spec major version number */
41 uint8_t minor; /* CTF spec minor version number */
42 uint8_t header_end[0];
43 };
44
45 struct metadata_record_header {
46 uint8_t header_end[0]; /* End of header */
47 };
48
49 static const struct lib_ring_buffer_config client_config;
50
51 static inline
52 u64 lib_ring_buffer_clock_read(struct channel *chan)
53 {
54 return 0;
55 }
56
57 static inline
58 unsigned char record_header_size(const struct lib_ring_buffer_config *config,
59 struct channel *chan, size_t offset,
60 size_t *pre_header_padding,
61 struct lib_ring_buffer_ctx *ctx)
62 {
63 return 0;
64 }
65
66 #include "wrapper/ringbuffer/api.h"
67
68 static u64 client_ring_buffer_clock_read(struct channel *chan)
69 {
70 return 0;
71 }
72
73 static
74 size_t client_record_header_size(const struct lib_ring_buffer_config *config,
75 struct channel *chan, size_t offset,
76 size_t *pre_header_padding,
77 struct lib_ring_buffer_ctx *ctx)
78 {
79 return 0;
80 }
81
82 /**
83 * client_packet_header_size - called on buffer-switch to a new sub-buffer
84 *
85 * Return header size without padding after the structure. Don't use packed
86 * structure because gcc generates inefficient code on some architectures
87 * (powerpc, mips..)
88 */
89 static size_t client_packet_header_size(void)
90 {
91 return offsetof(struct metadata_packet_header, header_end);
92 }
93
94 static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc,
95 unsigned int subbuf_idx)
96 {
97 struct channel *chan = buf->backend.chan;
98 struct metadata_packet_header *header =
99 (struct metadata_packet_header *)
100 lib_ring_buffer_offset_address(&buf->backend,
101 subbuf_idx * chan->backend.subbuf_size);
102 struct lttng_channel *lttng_chan = channel_get_private(chan);
103 struct lttng_session *session = lttng_chan->session;
104
105 header->magic = TSDL_MAGIC_NUMBER;
106 memcpy(header->uuid, session->uuid.b, sizeof(session->uuid));
107 header->checksum = 0; /* 0 if unused */
108 header->content_size = 0xFFFFFFFF; /* in bits, for debugging */
109 header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */
110 header->compression_scheme = 0; /* 0 if unused */
111 header->encryption_scheme = 0; /* 0 if unused */
112 header->checksum_scheme = 0; /* 0 if unused */
113 header->major = CTF_SPEC_MAJOR;
114 header->minor = CTF_SPEC_MINOR;
115 }
116
117 /*
118 * offset is assumed to never be 0 here : never deliver a completely empty
119 * subbuffer. data_size is between 1 and subbuf_size.
120 */
121 static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc,
122 unsigned int subbuf_idx, unsigned long data_size)
123 {
124 struct channel *chan = buf->backend.chan;
125 struct metadata_packet_header *header =
126 (struct metadata_packet_header *)
127 lib_ring_buffer_offset_address(&buf->backend,
128 subbuf_idx * chan->backend.subbuf_size);
129 unsigned long records_lost = 0;
130
131 header->content_size = data_size * CHAR_BIT; /* in bits */
132 header->packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
133 /*
134 * We do not care about the records lost count, because the metadata
135 * channel waits and retry.
136 */
137 (void) lib_ring_buffer_get_records_lost_full(&client_config, buf);
138 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
139 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
140 WARN_ON_ONCE(records_lost != 0);
141 }
142
143 static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
144 int cpu, const char *name)
145 {
146 return 0;
147 }
148
149 static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
150 {
151 }
152
153 static int client_timestamp_begin(const struct lib_ring_buffer_config *config,
154 struct lib_ring_buffer *buf, uint64_t *timestamp_begin)
155 {
156 return -ENOSYS;
157 }
158
159 static int client_timestamp_end(const struct lib_ring_buffer_config *config,
160 struct lib_ring_buffer *bufb,
161 uint64_t *timestamp_end)
162 {
163 return -ENOSYS;
164 }
165
166 static int client_events_discarded(const struct lib_ring_buffer_config *config,
167 struct lib_ring_buffer *bufb,
168 uint64_t *events_discarded)
169 {
170 return -ENOSYS;
171 }
172
173 static int client_current_timestamp(const struct lib_ring_buffer_config *config,
174 struct lib_ring_buffer *bufb,
175 uint64_t *ts)
176 {
177 return -ENOSYS;
178 }
179
180 static int client_content_size(const struct lib_ring_buffer_config *config,
181 struct lib_ring_buffer *bufb,
182 uint64_t *content_size)
183 {
184 return -ENOSYS;
185 }
186
187 static int client_packet_size(const struct lib_ring_buffer_config *config,
188 struct lib_ring_buffer *bufb,
189 uint64_t *packet_size)
190 {
191 return -ENOSYS;
192 }
193
194 static int client_stream_id(const struct lib_ring_buffer_config *config,
195 struct lib_ring_buffer *bufb,
196 uint64_t *stream_id)
197 {
198 return -ENOSYS;
199 }
200
201 static const struct lib_ring_buffer_config client_config = {
202 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
203 .cb.record_header_size = client_record_header_size,
204 .cb.subbuffer_header_size = client_packet_header_size,
205 .cb.buffer_begin = client_buffer_begin,
206 .cb.buffer_end = client_buffer_end,
207 .cb.buffer_create = client_buffer_create,
208 .cb.buffer_finalize = client_buffer_finalize,
209
210 .tsc_bits = 0,
211 .alloc = RING_BUFFER_ALLOC_GLOBAL,
212 .sync = RING_BUFFER_SYNC_GLOBAL,
213 .mode = RING_BUFFER_MODE_TEMPLATE,
214 .backend = RING_BUFFER_PAGE,
215 .output = RING_BUFFER_OUTPUT_TEMPLATE,
216 .oops = RING_BUFFER_OOPS_CONSISTENCY,
217 .ipi = RING_BUFFER_IPI_BARRIER,
218 .wakeup = RING_BUFFER_WAKEUP_BY_TIMER,
219 };
220
221 static
222 void release_priv_ops(void *priv_ops)
223 {
224 module_put(THIS_MODULE);
225 }
226
227 static
228 void lttng_channel_destroy(struct channel *chan)
229 {
230 channel_destroy(chan);
231 }
232
233 static
234 struct channel *_channel_create(const char *name,
235 struct lttng_channel *lttng_chan, void *buf_addr,
236 size_t subbuf_size, size_t num_subbuf,
237 unsigned int switch_timer_interval,
238 unsigned int read_timer_interval)
239 {
240 struct channel *chan;
241
242 chan = channel_create(&client_config, name, lttng_chan, buf_addr,
243 subbuf_size, num_subbuf, switch_timer_interval,
244 read_timer_interval);
245 if (chan) {
246 /*
247 * Ensure this module is not unloaded before we finish
248 * using lttng_relay_transport.ops.
249 */
250 if (!try_module_get(THIS_MODULE)) {
251 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
252 goto error;
253 }
254 chan->backend.priv_ops = &lttng_relay_transport.ops;
255 chan->backend.release_priv_ops = release_priv_ops;
256 }
257 return chan;
258
259 error:
260 lttng_channel_destroy(chan);
261 return NULL;
262 }
263
264 static
265 struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan)
266 {
267 struct lib_ring_buffer *buf;
268
269 buf = channel_get_ring_buffer(&client_config, chan, 0);
270 if (!lib_ring_buffer_open_read(buf))
271 return buf;
272 return NULL;
273 }
274
275 static
276 int lttng_buffer_has_read_closed_stream(struct channel *chan)
277 {
278 struct lib_ring_buffer *buf;
279 int cpu;
280
281 for_each_channel_cpu(cpu, chan) {
282 buf = channel_get_ring_buffer(&client_config, chan, cpu);
283 if (!atomic_long_read(&buf->active_readers))
284 return 1;
285 }
286 return 0;
287 }
288
289 static
290 void lttng_buffer_read_close(struct lib_ring_buffer *buf)
291 {
292 lib_ring_buffer_release_read(buf);
293 }
294
295 static
296 int lttng_event_reserve(struct lib_ring_buffer_ctx *ctx, uint32_t event_id)
297 {
298 return lib_ring_buffer_reserve(&client_config, ctx);
299 }
300
301 static
302 void lttng_event_commit(struct lib_ring_buffer_ctx *ctx)
303 {
304 lib_ring_buffer_commit(&client_config, ctx);
305 }
306
307 static
308 void lttng_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
309 size_t len)
310 {
311 lib_ring_buffer_write(&client_config, ctx, src, len);
312 }
313
314 static
315 void lttng_event_write_from_user(struct lib_ring_buffer_ctx *ctx,
316 const void __user *src, size_t len)
317 {
318 lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len);
319 }
320
321 static
322 void lttng_event_memset(struct lib_ring_buffer_ctx *ctx,
323 int c, size_t len)
324 {
325 lib_ring_buffer_memset(&client_config, ctx, c, len);
326 }
327
328 static
329 size_t lttng_packet_avail_size(struct channel *chan)
330
331 {
332 unsigned long o_begin;
333 struct lib_ring_buffer *buf;
334
335 buf = chan->backend.buf; /* Only for global buffer ! */
336 o_begin = v_read(&client_config, &buf->offset);
337 if (subbuf_offset(o_begin, chan) != 0) {
338 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan);
339 } else {
340 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan)
341 - sizeof(struct metadata_packet_header);
342 }
343 }
344
345 static
346 wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu)
347 {
348 struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config,
349 chan, cpu);
350 return &buf->write_wait;
351 }
352
353 static
354 wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan)
355 {
356 return &chan->hp_wait;
357 }
358
359 static
360 int lttng_is_finalized(struct channel *chan)
361 {
362 return lib_ring_buffer_channel_is_finalized(chan);
363 }
364
365 static
366 int lttng_is_disabled(struct channel *chan)
367 {
368 return lib_ring_buffer_channel_is_disabled(chan);
369 }
370
371 static struct lttng_transport lttng_relay_transport = {
372 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
373 .owner = THIS_MODULE,
374 .ops = {
375 .channel_create = _channel_create,
376 .channel_destroy = lttng_channel_destroy,
377 .buffer_read_open = lttng_buffer_read_open,
378 .buffer_has_read_closed_stream =
379 lttng_buffer_has_read_closed_stream,
380 .buffer_read_close = lttng_buffer_read_close,
381 .event_reserve = lttng_event_reserve,
382 .event_commit = lttng_event_commit,
383 .event_write_from_user = lttng_event_write_from_user,
384 .event_memset = lttng_event_memset,
385 .event_write = lttng_event_write,
386 .packet_avail_size = lttng_packet_avail_size,
387 .get_writer_buf_wait_queue = lttng_get_writer_buf_wait_queue,
388 .get_hp_wait_queue = lttng_get_hp_wait_queue,
389 .is_finalized = lttng_is_finalized,
390 .is_disabled = lttng_is_disabled,
391 .timestamp_begin = client_timestamp_begin,
392 .timestamp_end = client_timestamp_end,
393 .events_discarded = client_events_discarded,
394 .content_size = client_content_size,
395 .packet_size = client_packet_size,
396 .stream_id = client_stream_id,
397 .current_timestamp = client_current_timestamp,
398 },
399 };
400
401 static int __init lttng_ring_buffer_client_init(void)
402 {
403 /*
404 * This vmalloc sync all also takes care of the lib ring buffer
405 * vmalloc'd module pages when it is built as a module into LTTng.
406 */
407 wrapper_vmalloc_sync_all();
408 lttng_transport_register(&lttng_relay_transport);
409 return 0;
410 }
411
412 module_init(lttng_ring_buffer_client_init);
413
414 static void __exit lttng_ring_buffer_client_exit(void)
415 {
416 lttng_transport_unregister(&lttng_relay_transport);
417 }
418
419 module_exit(lttng_ring_buffer_client_exit);
420
421 MODULE_LICENSE("GPL and additional rights");
422 MODULE_AUTHOR("Mathieu Desnoyers");
423 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
424 " client");
This page took 0.039651 seconds and 5 git commands to generate.