]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - kernel/trace/trace_uprobe.c
tracing: Fix race between deleting buffer and setting events
[mirror_ubuntu-zesty-kernel.git] / kernel / trace / trace_uprobe.c
CommitLineData
f3f096cf
SD
1/*
2 * uprobes-based tracing events
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Copyright (C) IBM Corporation, 2010-2012
18 * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
19 */
20
21#include <linux/module.h>
22#include <linux/uaccess.h>
23#include <linux/uprobes.h>
24#include <linux/namei.h>
b2e902f0 25#include <linux/string.h>
f3f096cf
SD
26
27#include "trace_probe.h"
28
29#define UPROBE_EVENT_SYSTEM "uprobes"
30
457d1772
ON
31struct uprobe_trace_entry_head {
32 struct trace_entry ent;
33 unsigned long vaddr[];
34};
35
36#define SIZEOF_TRACE_ENTRY(is_return) \
37 (sizeof(struct uprobe_trace_entry_head) + \
38 sizeof(unsigned long) * (is_return ? 2 : 1))
39
40#define DATAOF_TRACE_ENTRY(entry, is_return) \
41 ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
42
736288ba
ON
43struct trace_uprobe_filter {
44 rwlock_t rwlock;
45 int nr_systemwide;
46 struct list_head perf_events;
47};
48
f3f096cf
SD
49/*
50 * uprobe event core functions
51 */
f3f096cf
SD
52struct trace_uprobe {
53 struct list_head list;
54 struct ftrace_event_class class;
55 struct ftrace_event_call call;
736288ba 56 struct trace_uprobe_filter filter;
a932b738 57 struct uprobe_consumer consumer;
f3f096cf
SD
58 struct inode *inode;
59 char *filename;
60 unsigned long offset;
61 unsigned long nhit;
62 unsigned int flags; /* For TP_FLAG_* */
63 ssize_t size; /* trace entry size */
64 unsigned int nr_args;
65 struct probe_arg args[];
66};
67
68#define SIZEOF_TRACE_UPROBE(n) \
69 (offsetof(struct trace_uprobe, args) + \
70 (sizeof(struct probe_arg) * (n)))
71
72static int register_uprobe_event(struct trace_uprobe *tu);
73static void unregister_uprobe_event(struct trace_uprobe *tu);
74
75static DEFINE_MUTEX(uprobe_lock);
76static LIST_HEAD(uprobe_list);
77
78static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
c1ae5c75
ON
79static int uretprobe_dispatcher(struct uprobe_consumer *con,
80 unsigned long func, struct pt_regs *regs);
f3f096cf 81
736288ba
ON
82static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
83{
84 rwlock_init(&filter->rwlock);
85 filter->nr_systemwide = 0;
86 INIT_LIST_HEAD(&filter->perf_events);
87}
88
89static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
90{
91 return !filter->nr_systemwide && list_empty(&filter->perf_events);
92}
93
c1ae5c75
ON
94static inline bool is_ret_probe(struct trace_uprobe *tu)
95{
96 return tu->consumer.ret_handler != NULL;
97}
98
f3f096cf
SD
99/*
100 * Allocate new trace_uprobe and initialize it (including uprobes).
101 */
102static struct trace_uprobe *
c1ae5c75 103alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
f3f096cf
SD
104{
105 struct trace_uprobe *tu;
106
107 if (!event || !is_good_name(event))
108 return ERR_PTR(-EINVAL);
109
110 if (!group || !is_good_name(group))
111 return ERR_PTR(-EINVAL);
112
113 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
114 if (!tu)
115 return ERR_PTR(-ENOMEM);
116
117 tu->call.class = &tu->class;
118 tu->call.name = kstrdup(event, GFP_KERNEL);
119 if (!tu->call.name)
120 goto error;
121
122 tu->class.system = kstrdup(group, GFP_KERNEL);
123 if (!tu->class.system)
124 goto error;
125
126 INIT_LIST_HEAD(&tu->list);
a932b738 127 tu->consumer.handler = uprobe_dispatcher;
c1ae5c75
ON
128 if (is_ret)
129 tu->consumer.ret_handler = uretprobe_dispatcher;
736288ba 130 init_trace_uprobe_filter(&tu->filter);
f3f096cf
SD
131 return tu;
132
133error:
134 kfree(tu->call.name);
135 kfree(tu);
136
137 return ERR_PTR(-ENOMEM);
138}
139
140static void free_trace_uprobe(struct trace_uprobe *tu)
141{
142 int i;
143
144 for (i = 0; i < tu->nr_args; i++)
145 traceprobe_free_probe_arg(&tu->args[i]);
146
147 iput(tu->inode);
148 kfree(tu->call.class->system);
149 kfree(tu->call.name);
150 kfree(tu->filename);
151 kfree(tu);
152}
153
154static struct trace_uprobe *find_probe_event(const char *event, const char *group)
155{
156 struct trace_uprobe *tu;
157
158 list_for_each_entry(tu, &uprobe_list, list)
159 if (strcmp(tu->call.name, event) == 0 &&
160 strcmp(tu->call.class->system, group) == 0)
161 return tu;
162
163 return NULL;
164}
165
166/* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
167static void unregister_trace_uprobe(struct trace_uprobe *tu)
168{
169 list_del(&tu->list);
170 unregister_uprobe_event(tu);
171 free_trace_uprobe(tu);
172}
173
174/* Register a trace_uprobe and probe_event */
175static int register_trace_uprobe(struct trace_uprobe *tu)
176{
177 struct trace_uprobe *old_tp;
178 int ret;
179
180 mutex_lock(&uprobe_lock);
181
182 /* register as an event */
183 old_tp = find_probe_event(tu->call.name, tu->call.class->system);
184 if (old_tp)
185 /* delete old event */
186 unregister_trace_uprobe(old_tp);
187
188 ret = register_uprobe_event(tu);
189 if (ret) {
190 pr_warning("Failed to register probe event(%d)\n", ret);
191 goto end;
192 }
193
194 list_add_tail(&tu->list, &uprobe_list);
195
196end:
197 mutex_unlock(&uprobe_lock);
198
199 return ret;
200}
201
202/*
203 * Argument syntax:
4ee5a52e 204 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:SYMBOL [FETCHARGS]
f3f096cf
SD
205 *
206 * - Remove uprobe: -:[GRP/]EVENT
207 */
208static int create_trace_uprobe(int argc, char **argv)
209{
210 struct trace_uprobe *tu;
211 struct inode *inode;
212 char *arg, *event, *group, *filename;
213 char buf[MAX_EVENT_NAME_LEN];
214 struct path path;
215 unsigned long offset;
4ee5a52e 216 bool is_delete, is_return;
f3f096cf
SD
217 int i, ret;
218
219 inode = NULL;
220 ret = 0;
221 is_delete = false;
4ee5a52e 222 is_return = false;
f3f096cf
SD
223 event = NULL;
224 group = NULL;
225
226 /* argc must be >= 1 */
227 if (argv[0][0] == '-')
228 is_delete = true;
4ee5a52e
ON
229 else if (argv[0][0] == 'r')
230 is_return = true;
f3f096cf 231 else if (argv[0][0] != 'p') {
4ee5a52e 232 pr_info("Probe definition must be started with 'p', 'r' or '-'.\n");
f3f096cf
SD
233 return -EINVAL;
234 }
235
236 if (argv[0][1] == ':') {
237 event = &argv[0][2];
238 arg = strchr(event, '/');
239
240 if (arg) {
241 group = event;
242 event = arg + 1;
243 event[-1] = '\0';
244
245 if (strlen(group) == 0) {
246 pr_info("Group name is not specified\n");
247 return -EINVAL;
248 }
249 }
250 if (strlen(event) == 0) {
251 pr_info("Event name is not specified\n");
252 return -EINVAL;
253 }
254 }
255 if (!group)
256 group = UPROBE_EVENT_SYSTEM;
257
258 if (is_delete) {
259 if (!event) {
260 pr_info("Delete command needs an event name.\n");
261 return -EINVAL;
262 }
263 mutex_lock(&uprobe_lock);
264 tu = find_probe_event(event, group);
265
266 if (!tu) {
267 mutex_unlock(&uprobe_lock);
268 pr_info("Event %s/%s doesn't exist.\n", group, event);
269 return -ENOENT;
270 }
271 /* delete an event */
272 unregister_trace_uprobe(tu);
273 mutex_unlock(&uprobe_lock);
274 return 0;
275 }
276
277 if (argc < 2) {
278 pr_info("Probe point is not specified.\n");
279 return -EINVAL;
280 }
281 if (isdigit(argv[1][0])) {
282 pr_info("probe point must be have a filename.\n");
283 return -EINVAL;
284 }
285 arg = strchr(argv[1], ':');
286 if (!arg)
287 goto fail_address_parse;
288
289 *arg++ = '\0';
290 filename = argv[1];
291 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
292 if (ret)
293 goto fail_address_parse;
294
f3f096cf 295 inode = igrab(path.dentry->d_inode);
84d7ed79
ON
296 path_put(&path);
297
7e4e28c5 298 if (!inode || !S_ISREG(inode->i_mode)) {
d24d7dbf
JZ
299 ret = -EINVAL;
300 goto fail_address_parse;
301 }
f3f096cf 302
84d7ed79
ON
303 ret = kstrtoul(arg, 0, &offset);
304 if (ret)
305 goto fail_address_parse;
306
f3f096cf
SD
307 argc -= 2;
308 argv += 2;
309
310 /* setup a probe */
311 if (!event) {
b2e902f0 312 char *tail;
f3f096cf
SD
313 char *ptr;
314
b2e902f0
AS
315 tail = kstrdup(kbasename(filename), GFP_KERNEL);
316 if (!tail) {
f3f096cf
SD
317 ret = -ENOMEM;
318 goto fail_address_parse;
319 }
320
f3f096cf
SD
321 ptr = strpbrk(tail, ".-_");
322 if (ptr)
323 *ptr = '\0';
324
325 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
326 event = buf;
327 kfree(tail);
328 }
329
4ee5a52e 330 tu = alloc_trace_uprobe(group, event, argc, is_return);
f3f096cf
SD
331 if (IS_ERR(tu)) {
332 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
333 ret = PTR_ERR(tu);
334 goto fail_address_parse;
335 }
336 tu->offset = offset;
337 tu->inode = inode;
338 tu->filename = kstrdup(filename, GFP_KERNEL);
339
340 if (!tu->filename) {
341 pr_info("Failed to allocate filename.\n");
342 ret = -ENOMEM;
343 goto error;
344 }
345
346 /* parse arguments */
347 ret = 0;
348 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
349 /* Increment count for freeing args in error case */
350 tu->nr_args++;
351
352 /* Parse argument name */
353 arg = strchr(argv[i], '=');
354 if (arg) {
355 *arg++ = '\0';
356 tu->args[i].name = kstrdup(argv[i], GFP_KERNEL);
357 } else {
358 arg = argv[i];
359 /* If argument name is omitted, set "argN" */
360 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
361 tu->args[i].name = kstrdup(buf, GFP_KERNEL);
362 }
363
364 if (!tu->args[i].name) {
365 pr_info("Failed to allocate argument[%d] name.\n", i);
366 ret = -ENOMEM;
367 goto error;
368 }
369
370 if (!is_good_name(tu->args[i].name)) {
371 pr_info("Invalid argument[%d] name: %s\n", i, tu->args[i].name);
372 ret = -EINVAL;
373 goto error;
374 }
375
376 if (traceprobe_conflict_field_name(tu->args[i].name, tu->args, i)) {
377 pr_info("Argument[%d] name '%s' conflicts with "
378 "another field.\n", i, argv[i]);
379 ret = -EINVAL;
380 goto error;
381 }
382
383 /* Parse fetch argument */
384 ret = traceprobe_parse_probe_arg(arg, &tu->size, &tu->args[i], false, false);
385 if (ret) {
386 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
387 goto error;
388 }
389 }
390
391 ret = register_trace_uprobe(tu);
392 if (ret)
393 goto error;
394 return 0;
395
396error:
397 free_trace_uprobe(tu);
398 return ret;
399
400fail_address_parse:
401 if (inode)
402 iput(inode);
403
d24d7dbf 404 pr_info("Failed to parse address or file.\n");
f3f096cf
SD
405
406 return ret;
407}
408
409static void cleanup_all_probes(void)
410{
411 struct trace_uprobe *tu;
412
413 mutex_lock(&uprobe_lock);
414 while (!list_empty(&uprobe_list)) {
415 tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
416 unregister_trace_uprobe(tu);
417 }
418 mutex_unlock(&uprobe_lock);
419}
420
421/* Probes listing interfaces */
422static void *probes_seq_start(struct seq_file *m, loff_t *pos)
423{
424 mutex_lock(&uprobe_lock);
425 return seq_list_start(&uprobe_list, *pos);
426}
427
428static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
429{
430 return seq_list_next(v, &uprobe_list, pos);
431}
432
433static void probes_seq_stop(struct seq_file *m, void *v)
434{
435 mutex_unlock(&uprobe_lock);
436}
437
438static int probes_seq_show(struct seq_file *m, void *v)
439{
440 struct trace_uprobe *tu = v;
3ede82dd 441 char c = is_ret_probe(tu) ? 'r' : 'p';
f3f096cf
SD
442 int i;
443
3ede82dd 444 seq_printf(m, "%c:%s/%s", c, tu->call.class->system, tu->call.name);
f3f096cf
SD
445 seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset);
446
447 for (i = 0; i < tu->nr_args; i++)
448 seq_printf(m, " %s=%s", tu->args[i].name, tu->args[i].comm);
449
450 seq_printf(m, "\n");
451 return 0;
452}
453
454static const struct seq_operations probes_seq_op = {
455 .start = probes_seq_start,
456 .next = probes_seq_next,
457 .stop = probes_seq_stop,
458 .show = probes_seq_show
459};
460
461static int probes_open(struct inode *inode, struct file *file)
462{
463 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
464 cleanup_all_probes();
465
466 return seq_open(file, &probes_seq_op);
467}
468
469static ssize_t probes_write(struct file *file, const char __user *buffer,
470 size_t count, loff_t *ppos)
471{
472 return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
473}
474
475static const struct file_operations uprobe_events_ops = {
476 .owner = THIS_MODULE,
477 .open = probes_open,
478 .read = seq_read,
479 .llseek = seq_lseek,
480 .release = seq_release,
481 .write = probes_write,
482};
483
484/* Probes profiling interfaces */
485static int probes_profile_seq_show(struct seq_file *m, void *v)
486{
487 struct trace_uprobe *tu = v;
488
489 seq_printf(m, " %s %-44s %15lu\n", tu->filename, tu->call.name, tu->nhit);
490 return 0;
491}
492
493static const struct seq_operations profile_seq_op = {
494 .start = probes_seq_start,
495 .next = probes_seq_next,
496 .stop = probes_seq_stop,
497 .show = probes_profile_seq_show
498};
499
500static int profile_open(struct inode *inode, struct file *file)
501{
502 return seq_open(file, &profile_seq_op);
503}
504
505static const struct file_operations uprobe_profile_ops = {
506 .owner = THIS_MODULE,
507 .open = profile_open,
508 .read = seq_read,
509 .llseek = seq_lseek,
510 .release = seq_release,
511};
512
a51cc604
ON
513static void uprobe_trace_print(struct trace_uprobe *tu,
514 unsigned long func, struct pt_regs *regs)
f3f096cf
SD
515{
516 struct uprobe_trace_entry_head *entry;
517 struct ring_buffer_event *event;
518 struct ring_buffer *buffer;
457d1772 519 void *data;
0e3853d2 520 int size, i;
f3f096cf
SD
521 struct ftrace_event_call *call = &tu->call;
522
393a736c 523 size = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
f3f096cf 524 event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
393a736c 525 size + tu->size, 0, 0);
f3f096cf 526 if (!event)
a51cc604 527 return;
f3f096cf
SD
528
529 entry = ring_buffer_event_data(event);
393a736c
ON
530 if (is_ret_probe(tu)) {
531 entry->vaddr[0] = func;
532 entry->vaddr[1] = instruction_pointer(regs);
533 data = DATAOF_TRACE_ENTRY(entry, true);
534 } else {
535 entry->vaddr[0] = instruction_pointer(regs);
536 data = DATAOF_TRACE_ENTRY(entry, false);
537 }
538
f3f096cf
SD
539 for (i = 0; i < tu->nr_args; i++)
540 call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
541
542 if (!filter_current_check_discard(buffer, call, entry, event))
0e3853d2 543 trace_buffer_unlock_commit(buffer, event, 0, 0);
a51cc604 544}
f42d24a1 545
a51cc604
ON
546/* uprobe handler */
547static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
548{
393a736c
ON
549 if (!is_ret_probe(tu))
550 uprobe_trace_print(tu, 0, regs);
f42d24a1 551 return 0;
f3f096cf
SD
552}
553
c1ae5c75
ON
554static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
555 struct pt_regs *regs)
556{
557 uprobe_trace_print(tu, func, regs);
558}
559
f3f096cf
SD
560/* Event entry printers */
561static enum print_line_t
562print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
563{
457d1772 564 struct uprobe_trace_entry_head *entry;
f3f096cf
SD
565 struct trace_seq *s = &iter->seq;
566 struct trace_uprobe *tu;
567 u8 *data;
568 int i;
569
457d1772 570 entry = (struct uprobe_trace_entry_head *)iter->ent;
f3f096cf
SD
571 tu = container_of(event, struct trace_uprobe, call.event);
572
3ede82dd
ON
573 if (is_ret_probe(tu)) {
574 if (!trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", tu->call.name,
575 entry->vaddr[1], entry->vaddr[0]))
576 goto partial;
577 data = DATAOF_TRACE_ENTRY(entry, true);
578 } else {
579 if (!trace_seq_printf(s, "%s: (0x%lx)", tu->call.name,
580 entry->vaddr[0]))
581 goto partial;
582 data = DATAOF_TRACE_ENTRY(entry, false);
583 }
f3f096cf 584
f3f096cf
SD
585 for (i = 0; i < tu->nr_args; i++) {
586 if (!tu->args[i].type->print(s, tu->args[i].name,
457d1772 587 data + tu->args[i].offset, entry))
f3f096cf
SD
588 goto partial;
589 }
590
591 if (trace_seq_puts(s, "\n"))
592 return TRACE_TYPE_HANDLED;
593
594partial:
595 return TRACE_TYPE_PARTIAL_LINE;
596}
597
b64b0077
ON
598static inline bool is_trace_uprobe_enabled(struct trace_uprobe *tu)
599{
600 return tu->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE);
601}
602
31ba3348
ON
603typedef bool (*filter_func_t)(struct uprobe_consumer *self,
604 enum uprobe_filter_ctx ctx,
605 struct mm_struct *mm);
606
607static int
608probe_event_enable(struct trace_uprobe *tu, int flag, filter_func_t filter)
f3f096cf 609{
f3f096cf
SD
610 int ret = 0;
611
b64b0077 612 if (is_trace_uprobe_enabled(tu))
f3f096cf
SD
613 return -EINTR;
614
736288ba
ON
615 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
616
4161824f 617 tu->flags |= flag;
31ba3348 618 tu->consumer.filter = filter;
a932b738
ON
619 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
620 if (ret)
4161824f 621 tu->flags &= ~flag;
f3f096cf 622
4161824f 623 return ret;
f3f096cf
SD
624}
625
626static void probe_event_disable(struct trace_uprobe *tu, int flag)
627{
b64b0077 628 if (!is_trace_uprobe_enabled(tu))
f3f096cf
SD
629 return;
630
736288ba
ON
631 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
632
a932b738 633 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
f3f096cf 634 tu->flags &= ~flag;
f3f096cf
SD
635}
636
637static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
638{
457d1772 639 int ret, i, size;
f3f096cf 640 struct uprobe_trace_entry_head field;
457d1772 641 struct trace_uprobe *tu = event_call->data;
f3f096cf 642
4d1298e2
ON
643 if (is_ret_probe(tu)) {
644 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
645 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
646 size = SIZEOF_TRACE_ENTRY(true);
647 } else {
648 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
649 size = SIZEOF_TRACE_ENTRY(false);
650 }
f3f096cf
SD
651 /* Set argument names as fields */
652 for (i = 0; i < tu->nr_args; i++) {
653 ret = trace_define_field(event_call, tu->args[i].type->fmttype,
654 tu->args[i].name,
457d1772 655 size + tu->args[i].offset,
f3f096cf
SD
656 tu->args[i].type->size,
657 tu->args[i].type->is_signed,
658 FILTER_OTHER);
659
660 if (ret)
661 return ret;
662 }
663 return 0;
664}
665
666#define LEN_OR_ZERO (len ? len - pos : 0)
667static int __set_print_fmt(struct trace_uprobe *tu, char *buf, int len)
668{
669 const char *fmt, *arg;
670 int i;
671 int pos = 0;
672
4d1298e2
ON
673 if (is_ret_probe(tu)) {
674 fmt = "(%lx <- %lx)";
675 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
676 } else {
677 fmt = "(%lx)";
678 arg = "REC->" FIELD_STRING_IP;
679 }
f3f096cf
SD
680
681 /* When len=0, we just calculate the needed length */
682
683 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
684
685 for (i = 0; i < tu->nr_args; i++) {
686 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
687 tu->args[i].name, tu->args[i].type->fmt);
688 }
689
690 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
691
692 for (i = 0; i < tu->nr_args; i++) {
693 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
694 tu->args[i].name);
695 }
696
697 return pos; /* return the length of print_fmt */
698}
699#undef LEN_OR_ZERO
700
701static int set_print_fmt(struct trace_uprobe *tu)
702{
703 char *print_fmt;
704 int len;
705
706 /* First: called with 0 length to calculate the needed length */
707 len = __set_print_fmt(tu, NULL, 0);
708 print_fmt = kmalloc(len + 1, GFP_KERNEL);
709 if (!print_fmt)
710 return -ENOMEM;
711
712 /* Second: actually write the @print_fmt */
713 __set_print_fmt(tu, print_fmt, len + 1);
714 tu->call.print_fmt = print_fmt;
715
716 return 0;
717}
718
719#ifdef CONFIG_PERF_EVENTS
31ba3348
ON
720static bool
721__uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
722{
723 struct perf_event *event;
724
725 if (filter->nr_systemwide)
726 return true;
727
728 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
729 if (event->hw.tp_target->mm == mm)
730 return true;
731 }
732
733 return false;
734}
735
b2fe8ba6
ON
736static inline bool
737uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
738{
739 return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm);
740}
741
736288ba
ON
742static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
743{
b2fe8ba6
ON
744 bool done;
745
736288ba 746 write_lock(&tu->filter.rwlock);
b2fe8ba6
ON
747 if (event->hw.tp_target) {
748 /*
749 * event->parent != NULL means copy_process(), we can avoid
750 * uprobe_apply(). current->mm must be probed and we can rely
751 * on dup_mmap() which preserves the already installed bp's.
752 *
753 * attr.enable_on_exec means that exec/mmap will install the
754 * breakpoints we need.
755 */
756 done = tu->filter.nr_systemwide ||
757 event->parent || event->attr.enable_on_exec ||
758 uprobe_filter_event(tu, event);
736288ba 759 list_add(&event->hw.tp_list, &tu->filter.perf_events);
b2fe8ba6
ON
760 } else {
761 done = tu->filter.nr_systemwide;
736288ba 762 tu->filter.nr_systemwide++;
b2fe8ba6 763 }
736288ba
ON
764 write_unlock(&tu->filter.rwlock);
765
b2fe8ba6
ON
766 if (!done)
767 uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
31ba3348 768
736288ba
ON
769 return 0;
770}
771
772static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
773{
b2fe8ba6
ON
774 bool done;
775
736288ba 776 write_lock(&tu->filter.rwlock);
b2fe8ba6 777 if (event->hw.tp_target) {
736288ba 778 list_del(&event->hw.tp_list);
b2fe8ba6
ON
779 done = tu->filter.nr_systemwide ||
780 (event->hw.tp_target->flags & PF_EXITING) ||
781 uprobe_filter_event(tu, event);
782 } else {
736288ba 783 tu->filter.nr_systemwide--;
b2fe8ba6
ON
784 done = tu->filter.nr_systemwide;
785 }
736288ba
ON
786 write_unlock(&tu->filter.rwlock);
787
b2fe8ba6
ON
788 if (!done)
789 uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
31ba3348 790
736288ba
ON
791 return 0;
792}
793
31ba3348
ON
794static bool uprobe_perf_filter(struct uprobe_consumer *uc,
795 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
796{
797 struct trace_uprobe *tu;
798 int ret;
799
800 tu = container_of(uc, struct trace_uprobe, consumer);
801 read_lock(&tu->filter.rwlock);
802 ret = __uprobe_perf_filter(&tu->filter, mm);
803 read_unlock(&tu->filter.rwlock);
804
805 return ret;
806}
807
a51cc604
ON
808static void uprobe_perf_print(struct trace_uprobe *tu,
809 unsigned long func, struct pt_regs *regs)
f3f096cf
SD
810{
811 struct ftrace_event_call *call = &tu->call;
812 struct uprobe_trace_entry_head *entry;
813 struct hlist_head *head;
457d1772
ON
814 void *data;
815 int size, rctx, i;
f3f096cf 816
393a736c 817 size = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
457d1772 818 size = ALIGN(size + tu->size + sizeof(u32), sizeof(u64)) - sizeof(u32);
f3f096cf 819 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
a51cc604 820 return;
f3f096cf
SD
821
822 preempt_disable();
515619f2
ON
823 head = this_cpu_ptr(call->perf_events);
824 if (hlist_empty(head))
825 goto out;
826
f3f096cf
SD
827 entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
828 if (!entry)
829 goto out;
830
393a736c
ON
831 if (is_ret_probe(tu)) {
832 entry->vaddr[0] = func;
32520b2c 833 entry->vaddr[1] = instruction_pointer(regs);
393a736c
ON
834 data = DATAOF_TRACE_ENTRY(entry, true);
835 } else {
32520b2c 836 entry->vaddr[0] = instruction_pointer(regs);
393a736c
ON
837 data = DATAOF_TRACE_ENTRY(entry, false);
838 }
839
f3f096cf
SD
840 for (i = 0; i < tu->nr_args; i++)
841 call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
842
32520b2c 843 perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
f3f096cf
SD
844 out:
845 preempt_enable();
a51cc604
ON
846}
847
848/* uprobe profile handler */
849static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
850{
851 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
852 return UPROBE_HANDLER_REMOVE;
853
393a736c
ON
854 if (!is_ret_probe(tu))
855 uprobe_perf_print(tu, 0, regs);
f42d24a1 856 return 0;
f3f096cf 857}
c1ae5c75
ON
858
859static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
860 struct pt_regs *regs)
861{
862 uprobe_perf_print(tu, func, regs);
863}
f3f096cf
SD
864#endif /* CONFIG_PERF_EVENTS */
865
866static
867int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data)
868{
457d1772 869 struct trace_uprobe *tu = event->data;
f3f096cf
SD
870
871 switch (type) {
872 case TRACE_REG_REGISTER:
31ba3348 873 return probe_event_enable(tu, TP_FLAG_TRACE, NULL);
f3f096cf
SD
874
875 case TRACE_REG_UNREGISTER:
876 probe_event_disable(tu, TP_FLAG_TRACE);
877 return 0;
878
879#ifdef CONFIG_PERF_EVENTS
880 case TRACE_REG_PERF_REGISTER:
31ba3348 881 return probe_event_enable(tu, TP_FLAG_PROFILE, uprobe_perf_filter);
f3f096cf
SD
882
883 case TRACE_REG_PERF_UNREGISTER:
884 probe_event_disable(tu, TP_FLAG_PROFILE);
885 return 0;
736288ba
ON
886
887 case TRACE_REG_PERF_OPEN:
888 return uprobe_perf_open(tu, data);
889
890 case TRACE_REG_PERF_CLOSE:
891 return uprobe_perf_close(tu, data);
892
f3f096cf
SD
893#endif
894 default:
895 return 0;
896 }
897 return 0;
898}
899
900static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
901{
f3f096cf 902 struct trace_uprobe *tu;
f42d24a1 903 int ret = 0;
f3f096cf 904
a932b738 905 tu = container_of(con, struct trace_uprobe, consumer);
1b47aefd 906 tu->nhit++;
f3f096cf
SD
907
908 if (tu->flags & TP_FLAG_TRACE)
f42d24a1 909 ret |= uprobe_trace_func(tu, regs);
f3f096cf
SD
910
911#ifdef CONFIG_PERF_EVENTS
912 if (tu->flags & TP_FLAG_PROFILE)
f42d24a1 913 ret |= uprobe_perf_func(tu, regs);
f3f096cf 914#endif
f42d24a1 915 return ret;
f3f096cf
SD
916}
917
c1ae5c75
ON
918static int uretprobe_dispatcher(struct uprobe_consumer *con,
919 unsigned long func, struct pt_regs *regs)
920{
921 struct trace_uprobe *tu;
922
923 tu = container_of(con, struct trace_uprobe, consumer);
924
925 if (tu->flags & TP_FLAG_TRACE)
926 uretprobe_trace_func(tu, func, regs);
927
928#ifdef CONFIG_PERF_EVENTS
929 if (tu->flags & TP_FLAG_PROFILE)
930 uretprobe_perf_func(tu, func, regs);
931#endif
932 return 0;
933}
934
f3f096cf
SD
935static struct trace_event_functions uprobe_funcs = {
936 .trace = print_uprobe_event
937};
938
939static int register_uprobe_event(struct trace_uprobe *tu)
940{
941 struct ftrace_event_call *call = &tu->call;
942 int ret;
943
944 /* Initialize ftrace_event_call */
945 INIT_LIST_HEAD(&call->class->fields);
946 call->event.funcs = &uprobe_funcs;
947 call->class->define_fields = uprobe_event_define_fields;
948
949 if (set_print_fmt(tu) < 0)
950 return -ENOMEM;
951
952 ret = register_ftrace_event(&call->event);
953 if (!ret) {
954 kfree(call->print_fmt);
955 return -ENODEV;
956 }
957 call->flags = 0;
958 call->class->reg = trace_uprobe_register;
959 call->data = tu;
960 ret = trace_add_event_call(call);
961
962 if (ret) {
963 pr_info("Failed to register uprobe event: %s\n", call->name);
964 kfree(call->print_fmt);
965 unregister_ftrace_event(&call->event);
966 }
967
968 return ret;
969}
970
971static void unregister_uprobe_event(struct trace_uprobe *tu)
972{
973 /* tu->event is unregistered in trace_remove_event_call() */
974 trace_remove_event_call(&tu->call);
975 kfree(tu->call.print_fmt);
976 tu->call.print_fmt = NULL;
977}
978
979/* Make a trace interface for controling probe points */
980static __init int init_uprobe_trace(void)
981{
982 struct dentry *d_tracer;
983
984 d_tracer = tracing_init_dentry();
985 if (!d_tracer)
986 return 0;
987
988 trace_create_file("uprobe_events", 0644, d_tracer,
989 NULL, &uprobe_events_ops);
990 /* Profile interface */
991 trace_create_file("uprobe_profile", 0444, d_tracer,
992 NULL, &uprobe_profile_ops);
993 return 0;
994}
995
996fs_initcall(init_uprobe_trace);