Fix teardown deadlock
[lttng-modules.git] / ltt-debugfs-abi.c
CommitLineData
baf20995
MD
1/*
2 * ltt-debugfs-abi.c
3 *
4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng debugfs ABI
7 *
8 * Mimic system calls for:
9 * - session creation, returns a file descriptor or failure.
ad1c05e1
MD
10 * - channel creation, returns a file descriptor or failure.
11 * - Operates on a session file descriptor
12 * - Takes all channel options as parameters.
13 * - stream get, returns a file descriptor or failure.
14 * - Operates on a channel file descriptor.
15 * - stream notifier get, returns a file descriptor or failure.
16 * - Operates on a channel file descriptor.
17 * - event creation, returns a file descriptor or failure.
18 * - Operates on a channel file descriptor
19 * - Takes an event name as parameter
20 * - Takes an instrumentation source as parameter
21 * - e.g. tracepoints, dynamic_probes...
22 * - Takes instrumentation source specific arguments.
baf20995
MD
23 */
24
11b5a3c2 25#include <linux/module.h>
baf20995 26#include <linux/debugfs.h>
11b5a3c2
MD
27#include <linux/anon_inodes.h>
28#include <linux/file.h>
29#include <linux/uaccess.h>
30#include <linux/slab.h>
b13f3ebe 31#include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
f3bc08c5 32#include "wrapper/ringbuffer/vfs.h"
11b5a3c2 33#include "ltt-debugfs-abi.h"
ad1c05e1 34#include "ltt-events.h"
baf20995
MD
35
36/*
37 * This is LTTng's own personal way to create a system call as an external
38 * module. We use ioctl() on /sys/kernel/debug/lttng.
39 */
40
41static struct dentry *lttng_dentry;
ad1c05e1
MD
42static const struct file_operations lttng_fops;
43static const struct file_operations lttng_session_fops;
44static const struct file_operations lttng_channel_fops;
5dbbdb43 45static const struct file_operations lttng_metadata_fops;
305d3e42 46static const struct file_operations lttng_event_fops;
baf20995 47
5dbbdb43
MD
48enum channel_type {
49 PER_CPU_CHANNEL,
5dbbdb43
MD
50 METADATA_CHANNEL,
51};
52
ad1c05e1 53static
baf20995
MD
54int lttng_abi_create_session(void)
55{
56 struct ltt_session *session;
c0e31d2e 57 struct file *session_file;
11b5a3c2 58 int session_fd, ret;
baf20995 59
653fe716 60 session = ltt_session_create();
baf20995
MD
61 if (!session)
62 return -ENOMEM;
1c25284c 63 session_fd = get_unused_fd();
baf20995
MD
64 if (session_fd < 0) {
65 ret = session_fd;
66 goto fd_error;
67 }
c0e31d2e 68 session_file = anon_inode_getfile("[lttng_session]",
ad1c05e1 69 &lttng_session_fops,
baf20995 70 session, O_RDWR);
c0e31d2e
MD
71 if (IS_ERR(session_file)) {
72 ret = PTR_ERR(session_file);
baf20995
MD
73 goto file_error;
74 }
c0e31d2e
MD
75 session->file = session_file;
76 fd_install(session_fd, session_file);
baf20995
MD
77 return session_fd;
78
79file_error:
80 put_unused_fd(session_fd);
81fd_error:
82 ltt_session_destroy(session);
83 return ret;
84}
85
ad1c05e1
MD
86/**
87 * lttng_ioctl - lttng syscall through ioctl
88 *
c0e31d2e 89 * @file: the file
ad1c05e1
MD
90 * @cmd: the command
91 * @arg: command arg
92 *
93 * This ioctl implements lttng commands:
94 * LTTNG_SESSION
95 * Returns a LTTng trace session file descriptor
96 *
97 * The returned session will be deleted when its file descriptor is closed.
98 */
99static
c0e31d2e 100long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1
MD
101{
102 switch (cmd) {
103 case LTTNG_SESSION:
104 return lttng_abi_create_session();
105 default:
106 return -ENOIOCTLCMD;
107 }
108}
109
ad1c05e1
MD
110static const struct file_operations lttng_fops = {
111 .unlocked_ioctl = lttng_ioctl,
112#ifdef CONFIG_COMPAT
03037b98 113 .compat_ioctl = lttng_ioctl,
ad1c05e1 114#endif
11b5a3c2 115};
ad1c05e1 116
5dbbdb43
MD
117/*
118 * We tolerate no failure in this function (if one happens, we print a dmesg
119 * error, but cannot return any error, because the channel information is
120 * invariant.
121 */
122static
123void lttng_metadata_create_events(struct file *channel_file)
124{
125 struct ltt_channel *channel = channel_file->private_data;
126 char *event_name = "lttng-metadata";
85a9ca7f 127 const struct lttng_event_desc *event_desc;
5dbbdb43
MD
128 struct ltt_event *event;
129 int ret;
5dbbdb43 130
85a9ca7f
MD
131 event_desc = ltt_event_get(event_name);
132 if (!event_desc) {
5dbbdb43 133 ret = -ENOENT;
85a9ca7f 134 goto get_error;
5dbbdb43
MD
135 }
136 /*
137 * We tolerate no failure path after event creation. It will stay
138 * invariant for the rest of the session.
139 */
140 event = ltt_event_create(channel, event_name, INSTRUM_TRACEPOINTS,
85a9ca7f 141 event_desc, NULL);
5dbbdb43 142 if (!event) {
85a9ca7f 143 goto create_error;
5dbbdb43
MD
144 ret = -EEXIST;
145 }
146 return;
147
85a9ca7f
MD
148create_error:
149 ltt_event_put(event_desc);
150get_error:
5dbbdb43
MD
151 WARN_ON(1);
152 return; /* not allowed to return error */
153}
154
155static
c0e31d2e 156int lttng_abi_create_channel(struct file *session_file,
5dbbdb43
MD
157 struct lttng_channel __user *uchan_param,
158 enum channel_type channel_type)
baf20995 159{
c0e31d2e 160 struct ltt_session *session = session_file->private_data;
5dbbdb43
MD
161 const struct file_operations *fops;
162 const char *transport_name;
baf20995 163 struct ltt_channel *chan;
c0e31d2e 164 struct file *chan_file;
baf20995
MD
165 struct lttng_channel chan_param;
166 int chan_fd;
ad1c05e1 167 int ret = 0;
baf20995 168
653fe716 169 if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param)))
baf20995 170 return -EFAULT;
1c25284c 171 chan_fd = get_unused_fd();
baf20995
MD
172 if (chan_fd < 0) {
173 ret = chan_fd;
174 goto fd_error;
175 }
c0e31d2e 176 chan_file = anon_inode_getfile("[lttng_channel]",
ad1c05e1 177 &lttng_channel_fops,
03037b98 178 NULL, O_RDWR);
c0e31d2e
MD
179 if (IS_ERR(chan_file)) {
180 ret = PTR_ERR(chan_file);
baf20995
MD
181 goto file_error;
182 }
5dbbdb43
MD
183 switch (channel_type) {
184 case PER_CPU_CHANNEL:
185 transport_name = chan_param.overwrite ?
186 "relay-overwrite" : "relay-discard";
187 fops = &lttng_channel_fops;
188 break;
5dbbdb43 189 case METADATA_CHANNEL:
881833e3 190 transport_name = "relay-metadata";
5dbbdb43
MD
191 fops = &lttng_metadata_fops;
192 break;
193 default:
194 transport_name = "<unknown>";
195 break;
196 }
03037b98
MD
197 /*
198 * We tolerate no failure path after channel creation. It will stay
199 * invariant for the rest of the session.
200 */
5dbbdb43 201 chan = ltt_channel_create(session, transport_name, NULL,
11b5a3c2
MD
202 chan_param.subbuf_size,
203 chan_param.num_subbuf,
204 chan_param.switch_timer_interval,
205 chan_param.read_timer_interval);
03037b98 206 if (!chan) {
f3d01b96 207 ret = -EINVAL;
03037b98
MD
208 goto chan_error;
209 }
11b5a3c2 210 chan->file = chan_file;
c0e31d2e
MD
211 chan_file->private_data = chan;
212 fd_install(chan_fd, chan_file);
5dbbdb43
MD
213 if (channel_type == METADATA_CHANNEL)
214 lttng_metadata_create_events(chan_file);
215
ad1c05e1 216 /* The channel created holds a reference on the session */
b0caa15a 217 atomic_long_inc(&session_file->f_count);
ad1c05e1 218
baf20995
MD
219 return chan_fd;
220
03037b98 221chan_error:
c0e31d2e 222 fput(chan_file);
baf20995
MD
223file_error:
224 put_unused_fd(chan_fd);
225fd_error:
baf20995
MD
226 return ret;
227}
228
229/**
ad1c05e1 230 * lttng_session_ioctl - lttng session fd ioctl
baf20995 231 *
c0e31d2e 232 * @file: the file
baf20995
MD
233 * @cmd: the command
234 * @arg: command arg
235 *
236 * This ioctl implements lttng commands:
baf20995
MD
237 * LTTNG_CHANNEL
238 * Returns a LTTng channel file descriptor
ad1c05e1
MD
239 *
240 * The returned channel will be deleted when its file descriptor is closed.
241 */
242static
c0e31d2e 243long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1 244{
c0e31d2e
MD
245 struct ltt_session *session = file->private_data;
246
ad1c05e1
MD
247 switch (cmd) {
248 case LTTNG_CHANNEL:
5dbbdb43
MD
249 return lttng_abi_create_channel(file,
250 (struct lttng_channel __user *)arg,
251 PER_CPU_CHANNEL);
c0e31d2e
MD
252 case LTTNG_SESSION_START:
253 return ltt_session_start(session);
254 case LTTNG_SESSION_STOP:
255 return ltt_session_stop(session);
5dbbdb43
MD
256 case LTTNG_METADATA:
257 return lttng_abi_create_channel(file,
258 (struct lttng_channel __user *)arg,
259 METADATA_CHANNEL);
ad1c05e1
MD
260 default:
261 return -ENOIOCTLCMD;
262 }
263}
264
03037b98
MD
265/*
266 * Called when the last file reference is dropped.
267 *
268 * Big fat note: channels and events are invariant for the whole session after
269 * their creation. So this session destruction also destroys all channel and
270 * event structures specific to this session (they are not destroyed when their
271 * individual file is released).
272 */
ad1c05e1 273static
03037b98 274int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 275{
03037b98 276 struct ltt_session *session = file->private_data;
c269fff4
MD
277
278 if (session)
279 ltt_session_destroy(session);
11b5a3c2 280 return 0;
ad1c05e1 281}
ad1c05e1
MD
282
283static const struct file_operations lttng_session_fops = {
03037b98 284 .release = lttng_session_release,
ad1c05e1
MD
285 .unlocked_ioctl = lttng_session_ioctl,
286#ifdef CONFIG_COMPAT
03037b98 287 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 288#endif
11b5a3c2 289};
ad1c05e1
MD
290
291static
c0e31d2e 292int lttng_abi_open_stream(struct file *channel_file)
ad1c05e1 293{
c0e31d2e 294 struct ltt_channel *channel = channel_file->private_data;
ad1c05e1
MD
295 struct lib_ring_buffer *buf;
296 int stream_fd, ret;
11b5a3c2 297 struct file *stream_file;
ad1c05e1 298
11b5a3c2 299 buf = channel->ops->buffer_read_open(channel->chan);
ad1c05e1
MD
300 if (!buf)
301 return -ENOENT;
302
1c25284c 303 stream_fd = get_unused_fd();
ad1c05e1
MD
304 if (stream_fd < 0) {
305 ret = stream_fd;
306 goto fd_error;
307 }
c0e31d2e 308 stream_file = anon_inode_getfile("[lttng_stream]",
7f57c73c 309 &lib_ring_buffer_file_operations,
ad1c05e1 310 buf, O_RDWR);
c0e31d2e
MD
311 if (IS_ERR(stream_file)) {
312 ret = PTR_ERR(stream_file);
ad1c05e1
MD
313 goto file_error;
314 }
409453cb
MD
315 /*
316 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
317 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
318 * file descriptor, so we set FMODE_PREAD here.
319 */
d7b6f197 320 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 321 fd_install(stream_fd, stream_file);
dda6a249
MD
322 /*
323 * The stream holds a reference to the channel within the generic ring
324 * buffer library, so no need to hold a refcount on the channel and
325 * session files here.
326 */
ad1c05e1
MD
327 return stream_fd;
328
329file_error:
330 put_unused_fd(stream_fd);
331fd_error:
11b5a3c2 332 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
333 return ret;
334}
335
653fe716 336static
c0e31d2e 337int lttng_abi_create_event(struct file *channel_file,
653fe716
MD
338 struct lttng_event __user *uevent_param)
339{
c0e31d2e 340 struct ltt_channel *channel = channel_file->private_data;
85a9ca7f 341 const struct lttng_event_desc *event_desc;
653fe716
MD
342 struct ltt_event *event;
343 char *event_name;
344 struct lttng_event event_param;
345 int event_fd, ret;
11b5a3c2 346 struct file *event_file;
653fe716
MD
347
348 if (copy_from_user(&event_param, uevent_param, sizeof(event_param)))
349 return -EFAULT;
350 event_name = kmalloc(PATH_MAX, GFP_KERNEL);
351 if (!event_name)
352 return -ENOMEM;
981616e8 353 if (strncpy_from_user(event_name, uevent_param->name, PATH_MAX) < 0) {
653fe716
MD
354 ret = -EFAULT;
355 goto name_error;
356 }
357 event_name[PATH_MAX - 1] = '\0';
dda6a249 358
85a9ca7f
MD
359 event_desc = ltt_event_get(event_name);
360 if (!event_desc) {
dda6a249 361 ret = -ENOENT;
85a9ca7f 362 goto get_error;
dda6a249 363 }
1c25284c 364 event_fd = get_unused_fd();
653fe716
MD
365 if (event_fd < 0) {
366 ret = event_fd;
367 goto fd_error;
368 }
c0e31d2e 369 event_file = anon_inode_getfile("[lttng_event]",
3b923e5b 370 &lttng_event_fops,
03037b98 371 NULL, O_RDWR);
c0e31d2e
MD
372 if (IS_ERR(event_file)) {
373 ret = PTR_ERR(event_file);
653fe716
MD
374 goto file_error;
375 }
03037b98
MD
376 /*
377 * We tolerate no failure path after event creation. It will stay
378 * invariant for the rest of the session.
379 */
11b5a3c2 380 event = ltt_event_create(channel, event_name, event_param.itype,
85a9ca7f 381 event_desc, NULL);
03037b98
MD
382 if (!event) {
383 goto event_error;
384 ret = -EEXIST;
385 }
c0e31d2e
MD
386 event_file->private_data = event;
387 fd_install(event_fd, event_file);
653fe716 388 /* The event holds a reference on the channel */
b0caa15a 389 atomic_long_inc(&channel_file->f_count);
03037b98 390 kfree(event_name);
653fe716
MD
391 return event_fd;
392
03037b98 393event_error:
c0e31d2e 394 fput(event_file);
653fe716
MD
395file_error:
396 put_unused_fd(event_fd);
397fd_error:
85a9ca7f
MD
398 ltt_event_put(event_desc);
399get_error:
653fe716
MD
400name_error:
401 kfree(event_name);
402 return ret;
403}
ad1c05e1
MD
404
405/**
406 * lttng_channel_ioctl - lttng syscall through ioctl
407 *
c0e31d2e 408 * @file: the file
ad1c05e1
MD
409 * @cmd: the command
410 * @arg: command arg
411 *
412 * This ioctl implements lttng commands:
413 * LTTNG_STREAM
414 * Returns an event stream file descriptor or failure.
415 * (typically, one event stream records events from one CPU)
baf20995 416 * LTTNG_EVENT
ad1c05e1 417 * Returns an event file descriptor or failure.
baf20995 418 *
baf20995
MD
419 * Channel and event file descriptors also hold a reference on the session.
420 */
ad1c05e1 421static
c0e31d2e 422long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 423{
baf20995 424 switch (cmd) {
ad1c05e1 425 case LTTNG_STREAM:
c0e31d2e 426 return lttng_abi_open_stream(file);
baf20995 427 case LTTNG_EVENT:
c0e31d2e 428 return lttng_abi_create_event(file, (struct lttng_event __user *)arg);
baf20995
MD
429 default:
430 return -ENOIOCTLCMD;
431 }
432}
433
5dbbdb43
MD
434/**
435 * lttng_metadata_ioctl - lttng syscall through ioctl
436 *
437 * @file: the file
438 * @cmd: the command
439 * @arg: command arg
440 *
441 * This ioctl implements lttng commands:
442 * LTTNG_STREAM
443 * Returns an event stream file descriptor or failure.
444 *
445 * Channel and event file descriptors also hold a reference on the session.
446 */
447static
448long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
449{
450 switch (cmd) {
451 case LTTNG_STREAM:
452 return lttng_abi_open_stream(file);
453 default:
454 return -ENOIOCTLCMD;
455 }
456}
457
3b923e5b
MD
458/* TODO: poll */
459#if 0
653fe716
MD
460/**
461 * lttng_channel_poll - lttng stream addition/removal monitoring
462 *
c0e31d2e 463 * @file: the file
653fe716
MD
464 * @wait: poll table
465 */
c0e31d2e 466unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 467{
c0e31d2e 468 struct ltt_channel *channel = file->private_data;
653fe716
MD
469 unsigned int mask = 0;
470
c0e31d2e 471 if (file->f_mode & FMODE_READ) {
653fe716 472 poll_wait_set_exclusive(wait);
c0e31d2e 473 poll_wait(file, &channel->notify_wait, wait);
653fe716
MD
474
475 /* TODO: identify when the channel is being finalized. */
476 if (finalized)
477 return POLLHUP;
478 else
479 return POLLIN | POLLRDNORM;
480 }
481 return mask;
482
483}
3b923e5b 484#endif //0
653fe716 485
0a84a57f
MD
486static
487int lttng_channel_release(struct inode *inode, struct file *file)
488{
489 struct ltt_channel *channel = file->private_data;
c269fff4
MD
490
491 if (channel)
492 fput(channel->session->file);
0a84a57f
MD
493 return 0;
494}
495
ad1c05e1 496static const struct file_operations lttng_channel_fops = {
03037b98 497 .release = lttng_channel_release,
3b923e5b
MD
498/* TODO */
499#if 0
653fe716 500 .poll = lttng_channel_poll,
3b923e5b 501#endif //0
ad1c05e1 502 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 503#ifdef CONFIG_COMPAT
03037b98 504 .compat_ioctl = lttng_channel_ioctl,
baf20995 505#endif
11b5a3c2 506};
baf20995 507
5dbbdb43
MD
508static const struct file_operations lttng_metadata_fops = {
509 .release = lttng_channel_release,
510 .unlocked_ioctl = lttng_metadata_ioctl,
511#ifdef CONFIG_COMPAT
512 .compat_ioctl = lttng_metadata_ioctl,
513#endif
514};
515
0a84a57f
MD
516static
517int lttng_event_release(struct inode *inode, struct file *file)
518{
519 struct ltt_event *event = file->private_data;
c269fff4
MD
520
521 if (event)
522 fput(event->chan->file);
0a84a57f
MD
523 return 0;
524}
525
3b923e5b 526/* TODO: filter control ioctl */
0a84a57f
MD
527static const struct file_operations lttng_event_fops = {
528 .release = lttng_event_release,
11b5a3c2 529};
0a84a57f 530
1c25284c 531int __init ltt_debugfs_abi_init(void)
baf20995
MD
532{
533 int ret = 0;
534
6d2a620c 535 wrapper_vmalloc_sync_all();
11b5a3c2 536 lttng_dentry = debugfs_create_file("lttng", S_IWUSR, NULL, NULL,
f3d01b96 537 &lttng_fops);
11b5a3c2 538 if (IS_ERR(lttng_dentry) || !lttng_dentry) {
baf20995
MD
539 printk(KERN_ERR "Error creating LTTng control file\n");
540 ret = -ENOMEM;
541 goto error;
542 }
543error:
544 return ret;
545}
546
1c25284c 547void __exit ltt_debugfs_abi_exit(void)
baf20995 548{
11b5a3c2 549 debugfs_remove(lttng_dentry);
baf20995 550}
This page took 0.065449 seconds and 4 git commands to generate.