misc: tegra-profiler: reduce size of sample
[linux-2.6.git] / drivers / misc / tegra-profiler / comm.c
1 /*
2  * drivers/misc/tegra-profiler/comm.c
3  *
4  * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  */
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <linux/module.h>
20 #include <linux/fs.h>
21 #include <linux/slab.h>
22 #include <linux/vmalloc.h>
23 #include <linux/miscdevice.h>
24 #include <linux/sched.h>
25 #include <linux/poll.h>
26 #include <linux/bitops.h>
27 #include <linux/interrupt.h>
28 #include <linux/err.h>
29
30 #include <asm/uaccess.h>
31
32 #include <linux/tegra_profiler.h>
33
34 #include "comm.h"
35 #include "version.h"
36
37 struct quadd_comm_ctx comm_ctx;
38
39 static inline void *rb_alloc(unsigned long size)
40 {
41         return vmalloc(size);
42 }
43
44 static inline void rb_free(void *addr)
45 {
46         vfree(addr);
47 }
48
49 static void rb_reset(struct quadd_ring_buffer *rb)
50 {
51         rb->pos_read = 0;
52         rb->pos_write = 0;
53         rb->fill_count = 0;
54         rb->max_fill_count = 0;
55 }
56
57 static int rb_init(struct quadd_ring_buffer *rb, size_t size)
58 {
59         spin_lock_init(&rb->lock);
60
61         rb->size = size;
62         rb->buf = NULL;
63
64         rb->buf = (char *) rb_alloc(rb->size);
65         if (!rb->buf) {
66                 pr_err("Ring buffer alloc error\n");
67                 return -ENOMEM;
68         }
69         pr_info("rb: data buffer size: %u\n", (unsigned int)rb->size);
70
71         rb_reset(rb);
72
73         return 0;
74 }
75
76 static void rb_deinit(struct quadd_ring_buffer *rb)
77 {
78         unsigned long flags;
79
80         spin_lock_irqsave(&rb->lock, flags);
81         if (rb->buf) {
82                 rb_reset(rb);
83
84                 rb_free(rb->buf);
85                 rb->buf = NULL;
86         }
87         spin_unlock_irqrestore(&rb->lock, flags);
88 }
89
90 static __attribute__((unused)) int rb_is_full(struct quadd_ring_buffer *rb)
91 {
92         return rb->fill_count == rb->size;
93 }
94
95 static int rb_is_empty(struct quadd_ring_buffer *rb)
96 {
97         return rb->fill_count == 0;
98 }
99
100 static int rb_is_empty_lock(struct quadd_ring_buffer *rb)
101 {
102         int res;
103         unsigned long flags;
104
105         spin_lock_irqsave(&rb->lock, flags);
106         res = rb->fill_count == 0;
107         spin_unlock_irqrestore(&rb->lock, flags);
108
109         return res;
110 }
111
112 static size_t
113 rb_get_free_space(struct quadd_ring_buffer *rb)
114 {
115         return rb->size - rb->fill_count;
116 }
117
118 static size_t
119 rb_write(struct quadd_ring_buffer *rb, char *data, size_t length)
120 {
121         size_t new_pos_write, chunk1;
122
123         if (length > rb_get_free_space(rb))
124                 return 0;
125
126         new_pos_write = (rb->pos_write + length) % rb->size;
127
128         if (new_pos_write < rb->pos_write) {
129                 chunk1 = rb->size - rb->pos_write;
130                 memcpy(rb->buf + rb->pos_write, data, chunk1);
131                 if (new_pos_write > 0)
132                         memcpy(rb->buf, data + chunk1, new_pos_write);
133         } else {
134                 memcpy(rb->buf + rb->pos_write, data, length);
135         }
136
137         rb->pos_write = new_pos_write;
138         rb->fill_count += length;
139
140         return length;
141 }
142
143 static ssize_t rb_read_undo(struct quadd_ring_buffer *rb, size_t length)
144 {
145         if (rb_get_free_space(rb) < length)
146                 return -EIO;
147
148         if (rb->pos_read > length)
149                 rb->pos_read -= length;
150         else
151                 rb->pos_read += rb->size - length;
152
153         rb->fill_count += sizeof(struct quadd_record_data);
154         return length;
155 }
156
157 static size_t rb_read(struct quadd_ring_buffer *rb, char *data, size_t length)
158 {
159         unsigned int new_pos_read, chunk1;
160
161         if (length > rb->fill_count)
162                 return 0;
163
164         new_pos_read = (rb->pos_read + length) % rb->size;
165
166         if (new_pos_read < rb->pos_read) {
167                 chunk1 = rb->size - rb->pos_read;
168                 memcpy(data, rb->buf + rb->pos_read, chunk1);
169                 if (new_pos_read > 0)
170                         memcpy(data + chunk1, rb->buf, new_pos_read);
171         } else {
172                 memcpy(data, rb->buf + rb->pos_read, length);
173         }
174
175         rb->pos_read = new_pos_read;
176         rb->fill_count -= length;
177
178         return length;
179 }
180
181 static ssize_t
182 rb_read_user(struct quadd_ring_buffer *rb, char __user *data, size_t length)
183 {
184         size_t new_pos_read, chunk1;
185
186         if (length > rb->fill_count)
187                 return 0;
188
189         new_pos_read = (rb->pos_read + length) % rb->size;
190
191         if (new_pos_read < rb->pos_read) {
192                 chunk1 = rb->size - rb->pos_read;
193                 if (copy_to_user(data, rb->buf + rb->pos_read, chunk1))
194                         return -EFAULT;
195
196                 if (new_pos_read > 0) {
197                         if (copy_to_user(data + chunk1, rb->buf,
198                                          new_pos_read))
199                                 return -EFAULT;
200                 }
201         } else {
202                 if (copy_to_user(data, rb->buf + rb->pos_read, length))
203                         return -EFAULT;
204         }
205
206         rb->pos_read = new_pos_read;
207         rb->fill_count -= length;
208
209         return length;
210 }
211
212 static void
213 write_sample(struct quadd_record_data *sample,
214              struct quadd_iovec *vec, int vec_count)
215 {
216         int i;
217         unsigned long flags;
218         struct quadd_ring_buffer *rb = &comm_ctx.rb;
219         size_t length_sample;
220
221         length_sample = sizeof(struct quadd_record_data);
222         for (i = 0; i < vec_count; i++)
223                 length_sample += vec[i].len;
224
225         spin_lock_irqsave(&rb->lock, flags);
226
227         if (length_sample > rb_get_free_space(rb)) {
228                 pr_err_once("Error: Buffer has been overflowed\n");
229                 spin_unlock_irqrestore(&rb->lock, flags);
230                 return;
231         }
232
233         if (!rb_write(rb, (char *)sample, sizeof(struct quadd_record_data))) {
234                 spin_unlock_irqrestore(&rb->lock, flags);
235                 return;
236         }
237
238         for (i = 0; i < vec_count; i++) {
239                 if (!rb_write(rb, vec[i].base, vec[i].len)) {
240                         spin_unlock_irqrestore(&rb->lock, flags);
241                         pr_err_once("%s: error: ring buffer\n", __func__);
242                         return;
243                 }
244         }
245
246         if (rb->fill_count > rb->max_fill_count)
247                 rb->max_fill_count = rb->fill_count;
248
249         spin_unlock_irqrestore(&rb->lock, flags);
250
251         wake_up_interruptible(&comm_ctx.read_wait);
252 }
253
254 static ssize_t read_sample(char __user *buffer, size_t max_length)
255 {
256         int retval = -EIO;
257         unsigned long flags;
258         struct quadd_ring_buffer *rb = &comm_ctx.rb;
259         struct quadd_record_data record;
260         size_t length_extra = 0, nr_events;
261         struct quadd_sample_data *sample;
262
263         spin_lock_irqsave(&rb->lock, flags);
264
265         if (rb_is_empty(rb)) {
266                 retval = 0;
267                 goto out;
268         }
269
270         if (rb->fill_count < sizeof(record))
271                 goto out;
272
273         if (!rb_read(rb, (char *)&record, sizeof(record)))
274                 goto out;
275
276         switch (record.record_type) {
277         case QUADD_RECORD_TYPE_SAMPLE:
278                 sample = &record.sample;
279                 length_extra = sample->callchain_nr * sizeof(quadd_bt_addr_t);
280
281                 nr_events = __sw_hweight32(sample->events_flags);
282                 length_extra += nr_events * sizeof(u32);
283
284                 length_extra += sample->state ? sizeof(u32) : 0;
285                 break;
286
287         case QUADD_RECORD_TYPE_MMAP:
288                 length_extra = sizeof(u64);
289
290                 if (record.mmap.filename_length > 0) {
291                         length_extra += record.mmap.filename_length;
292                 } else {
293                         pr_err("Error: filename is empty\n");
294                         goto out;
295                 }
296                 break;
297
298         case QUADD_RECORD_TYPE_HEADER:
299                 length_extra = record.hdr.nr_events * sizeof(u32);
300                 break;
301
302         case QUADD_RECORD_TYPE_DEBUG:
303                 length_extra = record.debug.extra_length;
304                 break;
305
306         case QUADD_RECORD_TYPE_MA:
307                 length_extra = 0;
308                 break;
309
310         case QUADD_RECORD_TYPE_POWER_RATE:
311                 length_extra = record.power_rate.nr_cpus * sizeof(u32);
312                 break;
313
314         case QUADD_RECORD_TYPE_ADDITIONAL_SAMPLE:
315                 length_extra = record.additional_sample.extra_length;
316                 break;
317
318         default:
319                 goto out;
320         }
321
322         if (sizeof(record) + length_extra > max_length) {
323                 retval = rb_read_undo(rb, sizeof(record));
324                 if (retval < 0)
325                         goto out;
326
327                 retval = 0;
328                 goto out;
329         }
330
331         if (length_extra > rb->fill_count)
332                 goto out;
333
334         if (copy_to_user(buffer, &record, sizeof(record)))
335                 goto out_fault_error;
336
337         if (length_extra > 0) {
338                 retval = rb_read_user(rb, buffer + sizeof(record),
339                                       length_extra);
340                 if (retval <= 0)
341                         goto out;
342         }
343
344         spin_unlock_irqrestore(&rb->lock, flags);
345         return sizeof(record) + length_extra;
346
347 out_fault_error:
348         retval = -EFAULT;
349
350 out:
351         spin_unlock_irqrestore(&rb->lock, flags);
352         return retval;
353 }
354
355 static void put_sample(struct quadd_record_data *data,
356                        struct quadd_iovec *vec, int vec_count)
357 {
358         if (!atomic_read(&comm_ctx.active))
359                 return;
360
361         write_sample(data, vec, vec_count);
362 }
363
364 static void comm_reset(void)
365 {
366         unsigned long flags;
367
368         pr_debug("Comm reset\n");
369         spin_lock_irqsave(&comm_ctx.rb.lock, flags);
370         rb_reset(&comm_ctx.rb);
371         spin_unlock_irqrestore(&comm_ctx.rb.lock, flags);
372 }
373
374 static int is_active(void)
375 {
376         return atomic_read(&comm_ctx.active) != 0;
377 }
378
379 static struct quadd_comm_data_interface comm_data = {
380         .put_sample = put_sample,
381         .reset = comm_reset,
382         .is_active = is_active,
383 };
384
385 static int check_access_permission(void)
386 {
387         struct task_struct *task;
388
389         if (capable(CAP_SYS_ADMIN))
390                 return 0;
391
392         if (!comm_ctx.params_ok || comm_ctx.process_pid == 0)
393                 return -EACCES;
394
395         rcu_read_lock();
396         task = pid_task(find_vpid(comm_ctx.process_pid), PIDTYPE_PID);
397         rcu_read_unlock();
398         if (!task)
399                 return -EACCES;
400
401         if (current_fsuid() != task_uid(task) &&
402             task_uid(task) != comm_ctx.debug_app_uid) {
403                 pr_err("Permission denied, owner/task uids: %u/%u\n",
404                            current_fsuid(), task_uid(task));
405                 return -EACCES;
406         }
407         return 0;
408 }
409
410 static int device_open(struct inode *inode, struct file *file)
411 {
412         mutex_lock(&comm_ctx.io_mutex);
413         comm_ctx.nr_users++;
414         mutex_unlock(&comm_ctx.io_mutex);
415         return 0;
416 }
417
418 static int device_release(struct inode *inode, struct file *file)
419 {
420         mutex_lock(&comm_ctx.io_mutex);
421         comm_ctx.nr_users--;
422
423         if (comm_ctx.nr_users == 0) {
424                 if (atomic_cmpxchg(&comm_ctx.active, 1, 0)) {
425                         comm_ctx.control->stop();
426                         pr_info("Stop profiling: daemon is closed\n");
427                 }
428         }
429         mutex_unlock(&comm_ctx.io_mutex);
430
431         return 0;
432 }
433
434 static unsigned int
435 device_poll(struct file *file, poll_table *wait)
436 {
437         unsigned int mask = 0;
438         struct quadd_ring_buffer *rb = &comm_ctx.rb;
439
440         poll_wait(file, &comm_ctx.read_wait, wait);
441
442         if (!rb_is_empty_lock(rb))
443                 mask |= POLLIN | POLLRDNORM;
444
445         if (!atomic_read(&comm_ctx.active))
446                 mask |= POLLHUP;
447
448         return mask;
449 }
450
451 static ssize_t
452 device_read(struct file *filp,
453             char __user *buffer,
454             size_t length,
455             loff_t *offset)
456 {
457         int err;
458         size_t was_read = 0, res, samples_counter = 0;
459
460         err = check_access_permission();
461         if (err)
462                 return err;
463
464         mutex_lock(&comm_ctx.io_mutex);
465
466         if (!atomic_read(&comm_ctx.active)) {
467                 mutex_unlock(&comm_ctx.io_mutex);
468                 return -EPIPE;
469         }
470
471         while (was_read + sizeof(struct quadd_record_data) < length) {
472                 res = read_sample(buffer + was_read, length - was_read);
473                 if (res < 0) {
474                         mutex_unlock(&comm_ctx.io_mutex);
475                         pr_err("Error: data is corrupted\n");
476                         return res;
477                 }
478
479                 if (res == 0)
480                         break;
481
482                 was_read += res;
483                 samples_counter++;
484
485                 if (!atomic_read(&comm_ctx.active))
486                         break;
487         }
488
489         mutex_unlock(&comm_ctx.io_mutex);
490         return was_read;
491 }
492
493 static long
494 device_ioctl(struct file *file,
495              unsigned int ioctl_num,
496              unsigned long ioctl_param)
497 {
498         int err;
499         struct quadd_parameters *user_params;
500         struct quadd_comm_cap cap;
501         struct quadd_module_state state;
502         struct quadd_module_version versions;
503         unsigned long flags;
504         struct quadd_ring_buffer *rb = &comm_ctx.rb;
505
506         if (ioctl_num != IOCTL_SETUP &&
507             ioctl_num != IOCTL_GET_CAP &&
508             ioctl_num != IOCTL_GET_STATE &&
509             ioctl_num != IOCTL_GET_VERSION) {
510                 err = check_access_permission();
511                 if (err)
512                         return err;
513         }
514
515         mutex_lock(&comm_ctx.io_mutex);
516
517         switch (ioctl_num) {
518         case IOCTL_SETUP:
519                 if (atomic_read(&comm_ctx.active)) {
520                         pr_err("error: tegra profiler is active\n");
521                         mutex_unlock(&comm_ctx.io_mutex);
522                         return -EBUSY;
523                 }
524
525                 user_params = vmalloc(sizeof(*user_params));
526                 if (!user_params)
527                         return -ENOMEM;
528
529                 if (copy_from_user(user_params, (void __user *)ioctl_param,
530                                    sizeof(struct quadd_parameters))) {
531                         pr_err("setup failed\n");
532                         vfree(user_params);
533                         mutex_unlock(&comm_ctx.io_mutex);
534                         return -EFAULT;
535                 }
536
537                 err = comm_ctx.control->set_parameters(user_params,
538                                                        &comm_ctx.debug_app_uid);
539                 if (err) {
540                         pr_err("error: setup failed\n");
541                         vfree(user_params);
542                         mutex_unlock(&comm_ctx.io_mutex);
543                         return err;
544                 }
545                 comm_ctx.params_ok = 1;
546                 comm_ctx.process_pid = user_params->pids[0];
547
548                 if (user_params->reserved[QUADD_PARAM_IDX_SIZE_OF_RB] == 0) {
549                         pr_err("error: too old version of daemon\n");
550                         vfree(user_params);
551                         mutex_unlock(&comm_ctx.io_mutex);
552                         return -EINVAL;
553                 }
554                 comm_ctx.rb_size = user_params->reserved[0];
555
556                 pr_info("setup success: freq/mafreq: %u/%u, backtrace: %d, pid: %d\n",
557                         user_params->freq,
558                         user_params->ma_freq,
559                         user_params->backtrace,
560                         user_params->pids[0]);
561
562                 vfree(user_params);
563                 break;
564
565         case IOCTL_GET_CAP:
566                 comm_ctx.control->get_capabilities(&cap);
567                 if (copy_to_user((void __user *)ioctl_param, &cap,
568                                  sizeof(struct quadd_comm_cap))) {
569                         pr_err("error: get_capabilities failed\n");
570                         mutex_unlock(&comm_ctx.io_mutex);
571                         return -EFAULT;
572                 }
573                 break;
574
575         case IOCTL_GET_VERSION:
576                 strcpy(versions.branch, QUADD_MODULE_BRANCH);
577                 strcpy(versions.version, QUADD_MODULE_VERSION);
578
579                 versions.samples_version = QUADD_SAMPLES_VERSION;
580                 versions.io_version = QUADD_IO_VERSION;
581
582                 if (copy_to_user((void __user *)ioctl_param, &versions,
583                                  sizeof(struct quadd_module_version))) {
584                         pr_err("error: get version failed\n");
585                         mutex_unlock(&comm_ctx.io_mutex);
586                         return -EFAULT;
587                 }
588                 break;
589
590         case IOCTL_GET_STATE:
591                 comm_ctx.control->get_state(&state);
592
593                 state.buffer_size = comm_ctx.rb_size;
594
595                 spin_lock_irqsave(&rb->lock, flags);
596                 state.buffer_fill_size =
597                         comm_ctx.rb_size - rb_get_free_space(rb);
598                 state.reserved[QUADD_MOD_STATE_IDX_RB_MAX_FILL_COUNT] =
599                         rb->max_fill_count;
600                 spin_unlock_irqrestore(&rb->lock, flags);
601
602                 if (copy_to_user((void __user *)ioctl_param, &state,
603                                  sizeof(struct quadd_module_state))) {
604                         pr_err("error: get_state failed\n");
605                         mutex_unlock(&comm_ctx.io_mutex);
606                         return -EFAULT;
607                 }
608                 break;
609
610         case IOCTL_START:
611                 if (!atomic_cmpxchg(&comm_ctx.active, 0, 1)) {
612                         if (!comm_ctx.params_ok) {
613                                 pr_err("error: params failed\n");
614                                 atomic_set(&comm_ctx.active, 0);
615                                 mutex_unlock(&comm_ctx.io_mutex);
616                                 return -EFAULT;
617                         }
618
619                         err = rb_init(rb, comm_ctx.rb_size);
620                         if (err) {
621                                 pr_err("error: rb_init failed\n");
622                                 atomic_set(&comm_ctx.active, 0);
623                                 mutex_unlock(&comm_ctx.io_mutex);
624                                 return err;
625                         }
626
627                         if (comm_ctx.control->start()) {
628                                 pr_err("error: start failed\n");
629                                 atomic_set(&comm_ctx.active, 0);
630                                 mutex_unlock(&comm_ctx.io_mutex);
631                                 return -EFAULT;
632                         }
633                         pr_info("Start profiling success\n");
634                 }
635                 break;
636
637         case IOCTL_STOP:
638                 if (atomic_cmpxchg(&comm_ctx.active, 1, 0)) {
639                         comm_ctx.control->stop();
640                         wake_up_interruptible(&comm_ctx.read_wait);
641                         rb_deinit(&comm_ctx.rb);
642                         pr_info("Stop profiling success\n");
643                 }
644                 break;
645
646         default:
647                 pr_err("error: ioctl %u is unsupported in this version of module\n",
648                        ioctl_num);
649                 mutex_unlock(&comm_ctx.io_mutex);
650                 return -EFAULT;
651         }
652         mutex_unlock(&comm_ctx.io_mutex);
653
654         return 0;
655 }
656
657 static void unregister(void)
658 {
659         misc_deregister(comm_ctx.misc_dev);
660         kfree(comm_ctx.misc_dev);
661 }
662
663 static void free_ctx(void)
664 {
665         rb_deinit(&comm_ctx.rb);
666 }
667
668 static const struct file_operations qm_fops = {
669         .read           = device_read,
670         .poll           = device_poll,
671         .open           = device_open,
672         .release        = device_release,
673         .unlocked_ioctl = device_ioctl
674 };
675
676 static int comm_init(void)
677 {
678         int res;
679         struct miscdevice *misc_dev;
680
681         misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
682         if (!misc_dev) {
683                 pr_err("Error: alloc error\n");
684                 return -ENOMEM;
685         }
686
687         misc_dev->minor = MISC_DYNAMIC_MINOR;
688         misc_dev->name = QUADD_DEVICE_NAME;
689         misc_dev->fops = &qm_fops;
690
691         res = misc_register(misc_dev);
692         if (res < 0) {
693                 pr_err("Error: misc_register %d\n", res);
694                 return res;
695         }
696         comm_ctx.misc_dev = misc_dev;
697
698         mutex_init(&comm_ctx.io_mutex);
699         atomic_set(&comm_ctx.active, 0);
700
701         comm_ctx.params_ok = 0;
702         comm_ctx.process_pid = 0;
703         comm_ctx.nr_users = 0;
704
705         init_waitqueue_head(&comm_ctx.read_wait);
706
707         return 0;
708 }
709
710 struct quadd_comm_data_interface *
711 quadd_comm_events_init(struct quadd_comm_control_interface *control)
712 {
713         int err;
714
715         err = comm_init();
716         if (err < 0)
717                 return ERR_PTR(err);
718
719         comm_ctx.control = control;
720         return &comm_data;
721 }
722
723 void quadd_comm_events_exit(void)
724 {
725         mutex_lock(&comm_ctx.io_mutex);
726         unregister();
727         free_ctx();
728         mutex_unlock(&comm_ctx.io_mutex);
729 }