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