Merge tag 'for-linus' of git://git.armlinux.org.uk/~rmk/linux-arm
[linux-block.git] / kernel / trace / trace_kprobe.c
CommitLineData
bcea3f96 1// SPDX-License-Identifier: GPL-2.0
413d37d1 2/*
77b44d1b 3 * Kprobes-based tracing events
413d37d1
MH
4 *
5 * Created by Masami Hiramatsu <mhiramat@redhat.com>
6 *
413d37d1 7 */
72576341 8#define pr_fmt(fmt) "trace_kprobe: " fmt
413d37d1 9
17911ff3 10#include <linux/security.h>
413d37d1
MH
11#include <linux/module.h>
12#include <linux/uaccess.h>
b2d09103 13#include <linux/rculist.h>
540adea3 14#include <linux/error-injection.h>
413d37d1 15
970988e1
MH
16#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
17
6212dd29 18#include "trace_dynevent.h"
d899926f 19#include "trace_kprobe_selftest.h"
8ab83f56 20#include "trace_probe.h"
53305928 21#include "trace_probe_tmpl.h"
1ff511e3 22
8ab83f56 23#define KPROBE_EVENT_SYSTEM "kprobes"
696ced4f 24#define KRETPROBE_MAXACTIVE_MAX 4096
970988e1
MH
25
26/* Kprobe early definition from command line */
27static char kprobe_boot_events_buf[COMMAND_LINE_SIZE] __initdata;
28
29static int __init set_kprobe_boot_events(char *str)
30{
31 strlcpy(kprobe_boot_events_buf, str, COMMAND_LINE_SIZE);
60efe21e
MH
32 disable_tracing_selftest("running kprobe events");
33
970988e1
MH
34 return 0;
35}
36__setup("kprobe_event=", set_kprobe_boot_events);
e09c8614 37
d262271d 38static int trace_kprobe_create(const char *raw_command);
6212dd29
MH
39static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev);
40static int trace_kprobe_release(struct dyn_event *ev);
41static bool trace_kprobe_is_busy(struct dyn_event *ev);
42static bool trace_kprobe_match(const char *system, const char *event,
30199137 43 int argc, const char **argv, struct dyn_event *ev);
6212dd29
MH
44
45static struct dyn_event_operations trace_kprobe_ops = {
46 .create = trace_kprobe_create,
47 .show = trace_kprobe_show,
48 .is_busy = trace_kprobe_is_busy,
49 .free = trace_kprobe_release,
50 .match = trace_kprobe_match,
51};
52
cede666e 53/*
77b44d1b 54 * Kprobe event core functions
413d37d1 55 */
c31ffb3f 56struct trace_kprobe {
6212dd29 57 struct dyn_event devent;
4a846b44 58 struct kretprobe rp; /* Use rp.kp for kprobe use */
a7636d9e 59 unsigned long __percpu *nhit;
413d37d1 60 const char *symbol; /* symbol name */
c31ffb3f 61 struct trace_probe tp;
413d37d1
MH
62};
63
6212dd29
MH
64static bool is_trace_kprobe(struct dyn_event *ev)
65{
66 return ev->ops == &trace_kprobe_ops;
67}
68
69static struct trace_kprobe *to_trace_kprobe(struct dyn_event *ev)
70{
71 return container_of(ev, struct trace_kprobe, devent);
72}
73
74/**
75 * for_each_trace_kprobe - iterate over the trace_kprobe list
76 * @pos: the struct trace_kprobe * for each entry
77 * @dpos: the struct dyn_event * to use as a loop cursor
78 */
79#define for_each_trace_kprobe(pos, dpos) \
80 for_each_dyn_event(dpos) \
81 if (is_trace_kprobe(dpos) && (pos = to_trace_kprobe(dpos)))
82
c31ffb3f
NK
83#define SIZEOF_TRACE_KPROBE(n) \
84 (offsetof(struct trace_kprobe, tp.args) + \
eca0d916 85 (sizeof(struct probe_arg) * (n)))
a82378d8 86
3da0f180 87static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
413d37d1 88{
c31ffb3f 89 return tk->rp.handler != NULL;
413d37d1
MH
90}
91
3da0f180 92static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
413d37d1 93{
c31ffb3f 94 return tk->symbol ? tk->symbol : "unknown";
413d37d1
MH
95}
96
3da0f180 97static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
61424318 98{
c31ffb3f 99 return tk->rp.kp.offset;
61424318
MH
100}
101
3da0f180 102static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
61424318 103{
c31ffb3f 104 return !!(kprobe_gone(&tk->rp.kp));
61424318
MH
105}
106
3da0f180 107static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
c31ffb3f 108 struct module *mod)
61424318 109{
f3d36426 110 int len = strlen(module_name(mod));
c31ffb3f 111 const char *name = trace_kprobe_symbol(tk);
f3d36426
JS
112
113 return strncmp(module_name(mod), name, len) == 0 && name[len] == ':';
61424318
MH
114}
115
59158ec4 116static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
61424318 117{
59158ec4
MH
118 char *p;
119 bool ret;
120
121 if (!tk->symbol)
122 return false;
123 p = strchr(tk->symbol, ':');
124 if (!p)
125 return true;
126 *p = '\0';
a0060505 127 rcu_read_lock_sched();
59158ec4 128 ret = !!find_module(tk->symbol);
a0060505 129 rcu_read_unlock_sched();
59158ec4
MH
130 *p = ':';
131
132 return ret;
61424318
MH
133}
134
6212dd29
MH
135static bool trace_kprobe_is_busy(struct dyn_event *ev)
136{
137 struct trace_kprobe *tk = to_trace_kprobe(ev);
138
139 return trace_probe_is_enabled(&tk->tp);
140}
141
eb5bf813
MH
142static bool trace_kprobe_match_command_head(struct trace_kprobe *tk,
143 int argc, const char **argv)
144{
145 char buf[MAX_ARGSTR_LEN + 1];
146
147 if (!argc)
148 return true;
149
150 if (!tk->symbol)
151 snprintf(buf, sizeof(buf), "0x%p", tk->rp.kp.addr);
152 else if (tk->rp.kp.offset)
153 snprintf(buf, sizeof(buf), "%s+%u",
154 trace_kprobe_symbol(tk), tk->rp.kp.offset);
155 else
156 snprintf(buf, sizeof(buf), "%s", trace_kprobe_symbol(tk));
157 if (strcmp(buf, argv[0]))
158 return false;
159 argc--; argv++;
160
161 return trace_probe_match_command_args(&tk->tp, argc, argv);
162}
163
6212dd29 164static bool trace_kprobe_match(const char *system, const char *event,
30199137 165 int argc, const char **argv, struct dyn_event *ev)
6212dd29
MH
166{
167 struct trace_kprobe *tk = to_trace_kprobe(ev);
168
b55ce203 169 return strcmp(trace_probe_name(&tk->tp), event) == 0 &&
eb5bf813
MH
170 (!system || strcmp(trace_probe_group_name(&tk->tp), system) == 0) &&
171 trace_kprobe_match_command_head(tk, argc, argv);
6212dd29
MH
172}
173
f18f97ac
MN
174static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
175{
176 unsigned long nhit = 0;
177 int cpu;
178
179 for_each_possible_cpu(cpu)
180 nhit += *per_cpu_ptr(tk->nhit, cpu);
181
182 return nhit;
183}
184
715fa2fd
MH
185static nokprobe_inline bool trace_kprobe_is_registered(struct trace_kprobe *tk)
186{
187 return !(list_empty(&tk->rp.kp.list) &&
188 hlist_unhashed(&tk->rp.kp.hlist));
189}
190
6bc6c77c 191/* Return 0 if it fails to find the symbol address */
45408c4f
MH
192static nokprobe_inline
193unsigned long trace_kprobe_address(struct trace_kprobe *tk)
194{
195 unsigned long addr;
196
197 if (tk->symbol) {
198 addr = (unsigned long)
199 kallsyms_lookup_name(trace_kprobe_symbol(tk));
6bc6c77c
MH
200 if (addr)
201 addr += tk->rp.kp.offset;
45408c4f
MH
202 } else {
203 addr = (unsigned long)tk->rp.kp.addr;
204 }
205 return addr;
206}
207
60d53e2c
MH
208static nokprobe_inline struct trace_kprobe *
209trace_kprobe_primary_from_call(struct trace_event_call *call)
210{
211 struct trace_probe *tp;
212
213 tp = trace_probe_primary_from_call(call);
214 if (WARN_ON_ONCE(!tp))
215 return NULL;
216
217 return container_of(tp, struct trace_kprobe, tp);
218}
219
b4da3340 220bool trace_kprobe_on_func_entry(struct trace_event_call *call)
9802d865 221{
60d53e2c 222 struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
b4da3340 223
97c753e6 224 return tk ? (kprobe_on_func_entry(tk->rp.kp.addr,
b4da3340 225 tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
97c753e6 226 tk->rp.kp.addr ? 0 : tk->rp.kp.offset) == 0) : false;
9802d865
JB
227}
228
b4da3340 229bool trace_kprobe_error_injectable(struct trace_event_call *call)
9802d865 230{
60d53e2c 231 struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
9802d865 232
60d53e2c
MH
233 return tk ? within_error_injection_list(trace_kprobe_address(tk)) :
234 false;
9802d865
JB
235}
236
c31ffb3f
NK
237static int register_kprobe_event(struct trace_kprobe *tk);
238static int unregister_kprobe_event(struct trace_kprobe *tk);
413d37d1 239
50d78056
MH
240static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
241static int kretprobe_dispatcher(struct kretprobe_instance *ri,
242 struct pt_regs *regs);
243
455b2899
MH
244static void free_trace_kprobe(struct trace_kprobe *tk)
245{
246 if (tk) {
247 trace_probe_cleanup(&tk->tp);
248 kfree(tk->symbol);
249 free_percpu(tk->nhit);
250 kfree(tk);
251 }
252}
253
4a846b44
MH
254/*
255 * Allocate new trace_probe and initialize it (including kprobes).
256 */
c31ffb3f 257static struct trace_kprobe *alloc_trace_kprobe(const char *group,
f52487e9 258 const char *event,
4a846b44
MH
259 void *addr,
260 const char *symbol,
261 unsigned long offs,
696ced4f 262 int maxactive,
3a6b7666 263 int nargs, bool is_return)
413d37d1 264{
c31ffb3f 265 struct trace_kprobe *tk;
6f3cf440 266 int ret = -ENOMEM;
413d37d1 267
c31ffb3f
NK
268 tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
269 if (!tk)
6f3cf440 270 return ERR_PTR(ret);
413d37d1 271
a7636d9e
MKL
272 tk->nhit = alloc_percpu(unsigned long);
273 if (!tk->nhit)
274 goto error;
275
413d37d1 276 if (symbol) {
c31ffb3f
NK
277 tk->symbol = kstrdup(symbol, GFP_KERNEL);
278 if (!tk->symbol)
413d37d1 279 goto error;
c31ffb3f
NK
280 tk->rp.kp.symbol_name = tk->symbol;
281 tk->rp.kp.offset = offs;
4a846b44 282 } else
c31ffb3f 283 tk->rp.kp.addr = addr;
4a846b44
MH
284
285 if (is_return)
c31ffb3f 286 tk->rp.handler = kretprobe_dispatcher;
4a846b44 287 else
c31ffb3f 288 tk->rp.kp.pre_handler = kprobe_dispatcher;
4a846b44 289
696ced4f 290 tk->rp.maxactive = maxactive;
715fa2fd
MH
291 INIT_HLIST_NODE(&tk->rp.kp.hlist);
292 INIT_LIST_HEAD(&tk->rp.kp.list);
696ced4f 293
b61387cb 294 ret = trace_probe_init(&tk->tp, event, group, false);
455b2899 295 if (ret < 0)
f52487e9
MH
296 goto error;
297
6212dd29 298 dyn_event_init(&tk->devent, &trace_kprobe_ops);
c31ffb3f 299 return tk;
413d37d1 300error:
455b2899 301 free_trace_kprobe(tk);
6f3cf440 302 return ERR_PTR(ret);
413d37d1
MH
303}
304
c31ffb3f
NK
305static struct trace_kprobe *find_trace_kprobe(const char *event,
306 const char *group)
413d37d1 307{
6212dd29 308 struct dyn_event *pos;
c31ffb3f 309 struct trace_kprobe *tk;
413d37d1 310
6212dd29 311 for_each_trace_kprobe(tk, pos)
b55ce203
MH
312 if (strcmp(trace_probe_name(&tk->tp), event) == 0 &&
313 strcmp(trace_probe_group_name(&tk->tp), group) == 0)
c31ffb3f 314 return tk;
413d37d1
MH
315 return NULL;
316}
317
87107a25
SRV
318static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
319{
320 int ret = 0;
321
715fa2fd 322 if (trace_kprobe_is_registered(tk) && !trace_kprobe_has_gone(tk)) {
87107a25
SRV
323 if (trace_kprobe_is_return(tk))
324 ret = enable_kretprobe(&tk->rp);
325 else
326 ret = enable_kprobe(&tk->rp.kp);
327 }
328
329 return ret;
330}
331
60d53e2c
MH
332static void __disable_trace_kprobe(struct trace_probe *tp)
333{
334 struct trace_probe *pos;
335 struct trace_kprobe *tk;
336
337 list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
338 tk = container_of(pos, struct trace_kprobe, tp);
339 if (!trace_kprobe_is_registered(tk))
340 continue;
341 if (trace_kprobe_is_return(tk))
342 disable_kretprobe(&tk->rp);
343 else
344 disable_kprobe(&tk->rp.kp);
345 }
346}
347
41a7dd42
MH
348/*
349 * Enable trace_probe
350 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
351 */
60d53e2c
MH
352static int enable_trace_kprobe(struct trace_event_call *call,
353 struct trace_event_file *file)
1538f888 354{
60d53e2c
MH
355 struct trace_probe *pos, *tp;
356 struct trace_kprobe *tk;
357 bool enabled;
1538f888
MH
358 int ret = 0;
359
60d53e2c
MH
360 tp = trace_probe_primary_from_call(call);
361 if (WARN_ON_ONCE(!tp))
362 return -ENODEV;
363 enabled = trace_probe_is_enabled(tp);
364
365 /* This also changes "enabled" state */
41a7dd42 366 if (file) {
60d53e2c 367 ret = trace_probe_add_file(tp, file);
b5f935ee
MH
368 if (ret)
369 return ret;
370 } else
60d53e2c 371 trace_probe_set_flag(tp, TP_FLAG_PROFILE);
41a7dd42 372
b5f935ee
MH
373 if (enabled)
374 return 0;
87107a25 375
60d53e2c
MH
376 list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
377 tk = container_of(pos, struct trace_kprobe, tp);
378 if (trace_kprobe_has_gone(tk))
379 continue;
380 ret = __enable_trace_kprobe(tk);
44d00dc7 381 if (ret)
60d53e2c 382 break;
60d53e2c
MH
383 enabled = true;
384 }
385
44d00dc7
MH
386 if (ret) {
387 /* Failed to enable one of them. Roll back all */
388 if (enabled)
389 __disable_trace_kprobe(tp);
b5f935ee 390 if (file)
60d53e2c 391 trace_probe_remove_file(tp, file);
b5f935ee 392 else
60d53e2c 393 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
57ea2a34 394 }
b5f935ee 395
1538f888
MH
396 return ret;
397}
398
41a7dd42
MH
399/*
400 * Disable trace_probe
401 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
402 */
60d53e2c
MH
403static int disable_trace_kprobe(struct trace_event_call *call,
404 struct trace_event_file *file)
1538f888 405{
60d53e2c
MH
406 struct trace_probe *tp;
407
408 tp = trace_probe_primary_from_call(call);
409 if (WARN_ON_ONCE(!tp))
410 return -ENODEV;
41a7dd42 411
41a7dd42 412 if (file) {
b5f935ee
MH
413 if (!trace_probe_get_file_link(tp, file))
414 return -ENOENT;
415 if (!trace_probe_has_single_file(tp))
b04d52e3 416 goto out;
747774d6 417 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
41a7dd42 418 } else
747774d6 419 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
41a7dd42 420
60d53e2c
MH
421 if (!trace_probe_is_enabled(tp))
422 __disable_trace_kprobe(tp);
e12f03d7 423
3fe3d619 424 out:
b5f935ee 425 if (file)
a232e270 426 /*
b5f935ee
MH
427 * Synchronization is done in below function. For perf event,
428 * file == NULL and perf_trace_event_unreg() calls
429 * tracepoint_synchronize_unregister() to ensure synchronize
430 * event. We don't need to care about it.
a232e270 431 */
b5f935ee 432 trace_probe_remove_file(tp, file);
a232e270 433
60d53e2c 434 return 0;
1538f888
MH
435}
436
7bb83f6f 437#if defined(CONFIG_DYNAMIC_FTRACE) && \
45408c4f 438 !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
c7411a1a 439static bool __within_notrace_func(unsigned long addr)
45408c4f 440{
c7411a1a 441 unsigned long offset, size;
45408c4f 442
6bc6c77c
MH
443 if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
444 return false;
45408c4f 445
9161a864
MH
446 /* Get the entry address of the target function */
447 addr -= offset;
448
449 /*
450 * Since ftrace_location_range() does inclusive range check, we need
451 * to subtract 1 byte from the end address.
452 */
453 return !ftrace_location_range(addr, addr + size - 1);
45408c4f 454}
c7411a1a
MH
455
456static bool within_notrace_func(struct trace_kprobe *tk)
457{
dcbd21c9 458 unsigned long addr = trace_kprobe_address(tk);
c7411a1a
MH
459 char symname[KSYM_NAME_LEN], *p;
460
461 if (!__within_notrace_func(addr))
462 return false;
463
464 /* Check if the address is on a suffixed-symbol */
465 if (!lookup_symbol_name(addr, symname)) {
466 p = strchr(symname, '.');
467 if (!p)
468 return true;
469 *p = '\0';
470 addr = (unsigned long)kprobe_lookup_name(symname, 0);
471 if (addr)
472 return __within_notrace_func(addr);
473 }
474
475 return true;
476}
45408c4f
MH
477#else
478#define within_notrace_func(tk) (false)
479#endif
480
61424318 481/* Internal register function - just handle k*probes and flags */
c31ffb3f 482static int __register_trace_kprobe(struct trace_kprobe *tk)
413d37d1 483{
a6682814 484 int i, ret;
61424318 485
a94549dd
DH
486 ret = security_locked_down(LOCKDOWN_KPROBES);
487 if (ret)
488 return ret;
489
715fa2fd 490 if (trace_kprobe_is_registered(tk))
61424318
MH
491 return -EINVAL;
492
45408c4f
MH
493 if (within_notrace_func(tk)) {
494 pr_warn("Could not probe notrace function %s\n",
495 trace_kprobe_symbol(tk));
496 return -EINVAL;
497 }
498
a6682814
MH
499 for (i = 0; i < tk->tp.nr_args; i++) {
500 ret = traceprobe_update_arg(&tk->tp.args[i]);
501 if (ret)
502 return ret;
503 }
504
61424318 505 /* Set/clear disabled flag according to tp->flag */
c31ffb3f
NK
506 if (trace_probe_is_enabled(&tk->tp))
507 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
61424318 508 else
c31ffb3f 509 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
61424318 510
c31ffb3f
NK
511 if (trace_kprobe_is_return(tk))
512 ret = register_kretprobe(&tk->rp);
413d37d1 513 else
c31ffb3f 514 ret = register_kprobe(&tk->rp.kp);
61424318 515
61424318
MH
516 return ret;
517}
518
519/* Internal unregister function - just handle k*probes and flags */
c31ffb3f 520static void __unregister_trace_kprobe(struct trace_kprobe *tk)
61424318 521{
715fa2fd 522 if (trace_kprobe_is_registered(tk)) {
c31ffb3f
NK
523 if (trace_kprobe_is_return(tk))
524 unregister_kretprobe(&tk->rp);
61424318 525 else
c31ffb3f 526 unregister_kprobe(&tk->rp.kp);
715fa2fd
MH
527 /* Cleanup kprobe for reuse and mark it unregistered */
528 INIT_HLIST_NODE(&tk->rp.kp.hlist);
529 INIT_LIST_HEAD(&tk->rp.kp.list);
c31ffb3f
NK
530 if (tk->rp.kp.symbol_name)
531 tk->rp.kp.addr = NULL;
61424318
MH
532 }
533}
534
6212dd29 535/* Unregister a trace_probe and probe_event */
c31ffb3f 536static int unregister_trace_kprobe(struct trace_kprobe *tk)
61424318 537{
ca89bc07
MH
538 /* If other probes are on the event, just unregister kprobe */
539 if (trace_probe_has_sibling(&tk->tp))
540 goto unreg;
541
02ca1521 542 /* Enabled event can not be unregistered */
c31ffb3f 543 if (trace_probe_is_enabled(&tk->tp))
02ca1521
MH
544 return -EBUSY;
545
40c32592 546 /* Will fail if probe is being used by ftrace or perf */
c31ffb3f 547 if (unregister_kprobe_event(tk))
40c32592
SRRH
548 return -EBUSY;
549
ca89bc07 550unreg:
c31ffb3f 551 __unregister_trace_kprobe(tk);
6212dd29 552 dyn_event_remove(&tk->devent);
ca89bc07 553 trace_probe_unlink(&tk->tp);
02ca1521
MH
554
555 return 0;
413d37d1
MH
556}
557
fe60b0ce
MH
558static bool trace_kprobe_has_same_kprobe(struct trace_kprobe *orig,
559 struct trace_kprobe *comp)
560{
561 struct trace_probe_event *tpe = orig->tp.event;
562 struct trace_probe *pos;
563 int i;
564
565 list_for_each_entry(pos, &tpe->probes, list) {
566 orig = container_of(pos, struct trace_kprobe, tp);
567 if (strcmp(trace_kprobe_symbol(orig),
568 trace_kprobe_symbol(comp)) ||
569 trace_kprobe_offset(orig) != trace_kprobe_offset(comp))
570 continue;
571
572 /*
573 * trace_probe_compare_arg_type() ensured that nr_args and
574 * each argument name and type are same. Let's compare comm.
575 */
576 for (i = 0; i < orig->tp.nr_args; i++) {
577 if (strcmp(orig->tp.args[i].comm,
578 comp->tp.args[i].comm))
f8d7ab2b 579 break;
fe60b0ce
MH
580 }
581
f8d7ab2b
SD
582 if (i == orig->tp.nr_args)
583 return true;
fe60b0ce
MH
584 }
585
586 return false;
587}
588
ca89bc07
MH
589static int append_trace_kprobe(struct trace_kprobe *tk, struct trace_kprobe *to)
590{
591 int ret;
592
fe60b0ce
MH
593 ret = trace_probe_compare_arg_type(&tk->tp, &to->tp);
594 if (ret) {
595 /* Note that argument starts index = 2 */
596 trace_probe_log_set_index(ret + 1);
597 trace_probe_log_err(0, DIFF_ARG_TYPE);
598 return -EEXIST;
599 }
600 if (trace_kprobe_has_same_kprobe(to, tk)) {
601 trace_probe_log_set_index(0);
602 trace_probe_log_err(0, SAME_PROBE);
603 return -EEXIST;
604 }
605
ca89bc07
MH
606 /* Append to existing event */
607 ret = trace_probe_append(&tk->tp, &to->tp);
608 if (ret)
609 return ret;
610
611 /* Register k*probe */
612 ret = __register_trace_kprobe(tk);
613 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
614 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
615 ret = 0;
616 }
617
618 if (ret)
619 trace_probe_unlink(&tk->tp);
620 else
621 dyn_event_add(&tk->devent);
622
623 return ret;
624}
625
413d37d1 626/* Register a trace_probe and probe_event */
c31ffb3f 627static int register_trace_kprobe(struct trace_kprobe *tk)
413d37d1 628{
c31ffb3f 629 struct trace_kprobe *old_tk;
413d37d1
MH
630 int ret;
631
6212dd29 632 mutex_lock(&event_mutex);
413d37d1 633
b55ce203
MH
634 old_tk = find_trace_kprobe(trace_probe_name(&tk->tp),
635 trace_probe_group_name(&tk->tp));
c31ffb3f 636 if (old_tk) {
ca89bc07
MH
637 if (trace_kprobe_is_return(tk) != trace_kprobe_is_return(old_tk)) {
638 trace_probe_log_set_index(0);
639 trace_probe_log_err(0, DIFF_PROBE_TYPE);
640 ret = -EEXIST;
641 } else {
fe60b0ce 642 ret = append_trace_kprobe(tk, old_tk);
ca89bc07
MH
643 }
644 goto end;
2d5e067e 645 }
61424318
MH
646
647 /* Register new event */
c31ffb3f 648 ret = register_kprobe_event(tk);
2d5e067e 649 if (ret) {
a395d6a7 650 pr_warn("Failed to register probe event(%d)\n", ret);
2d5e067e
MH
651 goto end;
652 }
653
61424318 654 /* Register k*probe */
c31ffb3f 655 ret = __register_trace_kprobe(tk);
59158ec4
MH
656 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
657 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
658 ret = 0;
659 }
660
61424318 661 if (ret < 0)
c31ffb3f 662 unregister_kprobe_event(tk);
61424318 663 else
6212dd29 664 dyn_event_add(&tk->devent);
61424318 665
413d37d1 666end:
6212dd29 667 mutex_unlock(&event_mutex);
413d37d1
MH
668 return ret;
669}
670
61424318 671/* Module notifier call back, checking event on the module */
c31ffb3f 672static int trace_kprobe_module_callback(struct notifier_block *nb,
61424318
MH
673 unsigned long val, void *data)
674{
675 struct module *mod = data;
6212dd29 676 struct dyn_event *pos;
c31ffb3f 677 struct trace_kprobe *tk;
61424318
MH
678 int ret;
679
680 if (val != MODULE_STATE_COMING)
681 return NOTIFY_DONE;
682
683 /* Update probes on coming module */
6212dd29
MH
684 mutex_lock(&event_mutex);
685 for_each_trace_kprobe(tk, pos) {
c31ffb3f 686 if (trace_kprobe_within_module(tk, mod)) {
02ca1521 687 /* Don't need to check busy - this should have gone. */
c31ffb3f
NK
688 __unregister_trace_kprobe(tk);
689 ret = __register_trace_kprobe(tk);
61424318 690 if (ret)
a395d6a7 691 pr_warn("Failed to re-register probe %s on %s: %d\n",
b55ce203 692 trace_probe_name(&tk->tp),
f3d36426 693 module_name(mod), ret);
61424318
MH
694 }
695 }
6212dd29 696 mutex_unlock(&event_mutex);
61424318
MH
697
698 return NOTIFY_DONE;
699}
700
c31ffb3f
NK
701static struct notifier_block trace_kprobe_module_nb = {
702 .notifier_call = trace_kprobe_module_callback,
61424318
MH
703 .priority = 1 /* Invoked after kprobe module callback */
704};
705
fca18a47
NR
706/* Convert certain expected symbols into '_' when generating event names */
707static inline void sanitize_event_name(char *name)
708{
709 while (*name++ != '\0')
710 if (*name == ':' || *name == '.')
711 *name = '_';
712}
713
d262271d 714static int __trace_kprobe_create(int argc, const char *argv[])
413d37d1
MH
715{
716 /*
717 * Argument syntax:
696ced4f
AC
718 * - Add kprobe:
719 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
720 * - Add kretprobe:
721 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
4725cd89
MH
722 * Or
723 * p:[GRP/]EVENT] [MOD:]KSYM[+0]%return [FETCHARGS]
724 *
413d37d1 725 * Fetch args:
2e06ff63
MH
726 * $retval : fetch return value
727 * $stack : fetch stack address
728 * $stackN : fetch Nth of stack (N:0-)
35abb67d 729 * $comm : fetch current task comm
413d37d1
MH
730 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
731 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
732 * %REG : fetch register REG
93ccae7a 733 * Dereferencing memory fetch:
413d37d1 734 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
eca0d916
MH
735 * Alias name of args:
736 * NAME=FETCHARG : set NAME as alias of FETCHARG.
93ccae7a
MH
737 * Type of args:
738 * FETCHARG:TYPE : use TYPE instead of unsigned long.
413d37d1 739 */
ab105a4f 740 struct trace_kprobe *tk = NULL;
6212dd29
MH
741 int i, len, ret = 0;
742 bool is_return = false;
743 char *symbol = NULL, *tmp = NULL;
744 const char *event = NULL, *group = KPROBE_EVENT_SYSTEM;
696ced4f 745 int maxactive = 0;
c5d343b6 746 long offset = 0;
413d37d1 747 void *addr = NULL;
4a846b44 748 char buf[MAX_EVENT_NAME_LEN];
a1303af5 749 unsigned int flags = TPARG_FL_KERNEL;
413d37d1 750
8b05a3a7
AR
751 switch (argv[0][0]) {
752 case 'r':
3a6b7666 753 is_return = true;
8b05a3a7
AR
754 break;
755 case 'p':
756 break;
757 default:
758 return -ECANCELED;
759 }
760 if (argc < 2)
6212dd29 761 return -ECANCELED;
413d37d1 762
ab105a4f
MH
763 trace_probe_log_init("trace_kprobe", argc, argv);
764
696ced4f 765 event = strchr(&argv[0][1], ':');
6212dd29 766 if (event)
696ced4f 767 event++;
6212dd29 768
287c038c
MH
769 if (isdigit(argv[0][1])) {
770 if (!is_return) {
ab105a4f
MH
771 trace_probe_log_err(1, MAXACT_NO_KPROBE);
772 goto parse_error;
287c038c 773 }
6212dd29
MH
774 if (event)
775 len = event - &argv[0][1] - 1;
776 else
777 len = strlen(&argv[0][1]);
ab105a4f
MH
778 if (len > MAX_EVENT_NAME_LEN - 1) {
779 trace_probe_log_err(1, BAD_MAXACT);
780 goto parse_error;
781 }
6212dd29
MH
782 memcpy(buf, &argv[0][1], len);
783 buf[len] = '\0';
784 ret = kstrtouint(buf, 0, &maxactive);
287c038c 785 if (ret || !maxactive) {
ab105a4f
MH
786 trace_probe_log_err(1, BAD_MAXACT);
787 goto parse_error;
696ced4f
AC
788 }
789 /* kretprobes instances are iterated over via a list. The
790 * maximum should stay reasonable.
791 */
792 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
ab105a4f
MH
793 trace_probe_log_err(1, MAXACT_TOO_BIG);
794 goto parse_error;
696ced4f
AC
795 }
796 }
797
9e52b325
SD
798 /* try to parse an address. if that fails, try to read the
799 * input as a symbol. */
800 if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
ab105a4f 801 trace_probe_log_set_index(1);
6212dd29 802 /* Check whether uprobe event specified */
ab105a4f
MH
803 if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
804 ret = -ECANCELED;
805 goto error;
806 }
413d37d1 807 /* a symbol specified */
6212dd29
MH
808 symbol = kstrdup(argv[1], GFP_KERNEL);
809 if (!symbol)
810 return -ENOMEM;
4725cd89
MH
811
812 tmp = strchr(symbol, '%');
813 if (tmp) {
814 if (!strcmp(tmp, "%return")) {
815 *tmp = '\0';
816 is_return = true;
817 } else {
818 trace_probe_log_err(tmp - symbol, BAD_ADDR_SUFFIX);
819 goto parse_error;
820 }
821 }
822
413d37d1 823 /* TODO: support .init module functions */
8ab83f56 824 ret = traceprobe_split_symbol_offset(symbol, &offset);
c5d343b6 825 if (ret || offset < 0 || offset > UINT_MAX) {
ab105a4f
MH
826 trace_probe_log_err(0, BAD_PROBE_ADDR);
827 goto parse_error;
e63cc239 828 }
4725cd89
MH
829 if (is_return)
830 flags |= TPARG_FL_RETURN;
97c753e6
MH
831 ret = kprobe_on_func_entry(NULL, symbol, offset);
832 if (ret == 0)
a1303af5 833 flags |= TPARG_FL_FENTRY;
97c753e6
MH
834 /* Defer the ENOENT case until register kprobe */
835 if (ret == -EINVAL && is_return) {
ab105a4f
MH
836 trace_probe_log_err(0, BAD_RETPROBE);
837 goto parse_error;
e63cc239 838 }
413d37d1
MH
839 }
840
ab105a4f 841 trace_probe_log_set_index(0);
6212dd29 842 if (event) {
ab105a4f
MH
843 ret = traceprobe_parse_event_name(&event, &group, buf,
844 event - argv[0]);
6212dd29 845 if (ret)
ab105a4f 846 goto parse_error;
6212dd29 847 } else {
4263565d 848 /* Make a new event name */
4263565d 849 if (symbol)
6f3cf440 850 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
4263565d
MH
851 is_return ? 'r' : 'p', symbol, offset);
852 else
6f3cf440 853 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
4263565d 854 is_return ? 'r' : 'p', addr);
fca18a47 855 sanitize_event_name(buf);
4a846b44
MH
856 event = buf;
857 }
6212dd29
MH
858
859 /* setup a probe */
696ced4f 860 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
ab105a4f 861 argc - 2, is_return);
c31ffb3f 862 if (IS_ERR(tk)) {
6212dd29 863 ret = PTR_ERR(tk);
ab105a4f 864 /* This must return -ENOMEM, else there is a bug */
a039480e 865 WARN_ON_ONCE(ret != -ENOMEM);
ab105a4f 866 goto out; /* We know tk is not allocated */
e63cc239 867 }
ab105a4f 868 argc -= 2; argv += 2;
413d37d1 869
413d37d1 870 /* parse arguments */
a82378d8 871 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
6212dd29
MH
872 tmp = kstrdup(argv[i], GFP_KERNEL);
873 if (!tmp) {
ba8665d7 874 ret = -ENOMEM;
413d37d1
MH
875 goto error;
876 }
da34634f 877
ab105a4f 878 trace_probe_log_set_index(i + 2);
6212dd29
MH
879 ret = traceprobe_parse_probe_arg(&tk->tp, i, tmp, flags);
880 kfree(tmp);
d00bbea9 881 if (ret)
ab105a4f 882 goto error; /* This can be -ENOMEM */
413d37d1 883 }
413d37d1 884
f730e0f2
MH
885 ret = traceprobe_set_print_fmt(&tk->tp, is_return);
886 if (ret < 0)
887 goto error;
888
c31ffb3f 889 ret = register_trace_kprobe(tk);
ab105a4f
MH
890 if (ret) {
891 trace_probe_log_set_index(1);
892 if (ret == -EILSEQ)
893 trace_probe_log_err(0, BAD_INSN_BNDRY);
894 else if (ret == -ENOENT)
895 trace_probe_log_err(0, BAD_PROBE_ADDR);
ca89bc07 896 else if (ret != -ENOMEM && ret != -EEXIST)
ab105a4f 897 trace_probe_log_err(0, FAIL_REG_PROBE);
413d37d1 898 goto error;
ab105a4f
MH
899 }
900
6212dd29 901out:
ab105a4f 902 trace_probe_log_clear();
6212dd29
MH
903 kfree(symbol);
904 return ret;
413d37d1 905
ab105a4f
MH
906parse_error:
907 ret = -EINVAL;
413d37d1 908error:
c31ffb3f 909 free_trace_kprobe(tk);
6212dd29 910 goto out;
413d37d1
MH
911}
912
d262271d
MH
913static int trace_kprobe_create(const char *raw_command)
914{
915 return trace_probe_create(raw_command, __trace_kprobe_create);
916}
917
918static int create_or_delete_trace_kprobe(const char *raw_command)
413d37d1 919{
6212dd29 920 int ret;
02ca1521 921
d262271d
MH
922 if (raw_command[0] == '-')
923 return dyn_event_release(raw_command, &trace_kprobe_ops);
413d37d1 924
d262271d 925 ret = trace_kprobe_create(raw_command);
6212dd29 926 return ret == -ECANCELED ? -EINVAL : ret;
413d37d1
MH
927}
928
29a15481 929static int trace_kprobe_run_command(struct dynevent_cmd *cmd)
2a588dd1 930{
d262271d 931 return create_or_delete_trace_kprobe(cmd->seq.buffer);
2a588dd1
TZ
932}
933
934/**
935 * kprobe_event_cmd_init - Initialize a kprobe event command object
936 * @cmd: A pointer to the dynevent_cmd struct representing the new event
937 * @buf: A pointer to the buffer used to build the command
938 * @maxlen: The length of the buffer passed in @buf
939 *
940 * Initialize a synthetic event command object. Use this before
941 * calling any of the other kprobe_event functions.
942 */
943void kprobe_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
944{
945 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_KPROBE,
29a15481 946 trace_kprobe_run_command);
2a588dd1
TZ
947}
948EXPORT_SYMBOL_GPL(kprobe_event_cmd_init);
949
950/**
951 * __kprobe_event_gen_cmd_start - Generate a kprobe event command from arg list
952 * @cmd: A pointer to the dynevent_cmd struct representing the new event
953 * @name: The name of the kprobe event
954 * @loc: The location of the kprobe event
955 * @kretprobe: Is this a return probe?
956 * @args: Variable number of arg (pairs), one pair for each field
957 *
958 * NOTE: Users normally won't want to call this function directly, but
959 * rather use the kprobe_event_gen_cmd_start() wrapper, which automatically
960 * adds a NULL to the end of the arg list. If this function is used
961 * directly, make sure the last arg in the variable arg list is NULL.
962 *
963 * Generate a kprobe event command to be executed by
964 * kprobe_event_gen_cmd_end(). This function can be used to generate the
965 * complete command or only the first part of it; in the latter case,
966 * kprobe_event_add_fields() can be used to add more fields following this.
967 *
5b4dcd2d
MH
968 * Unlikely the synth_event_gen_cmd_start(), @loc must be specified. This
969 * returns -EINVAL if @loc == NULL.
970 *
2a588dd1
TZ
971 * Return: 0 if successful, error otherwise.
972 */
973int __kprobe_event_gen_cmd_start(struct dynevent_cmd *cmd, bool kretprobe,
974 const char *name, const char *loc, ...)
975{
976 char buf[MAX_EVENT_NAME_LEN];
977 struct dynevent_arg arg;
978 va_list args;
979 int ret;
980
981 if (cmd->type != DYNEVENT_TYPE_KPROBE)
982 return -EINVAL;
983
5b4dcd2d
MH
984 if (!loc)
985 return -EINVAL;
986
2a588dd1
TZ
987 if (kretprobe)
988 snprintf(buf, MAX_EVENT_NAME_LEN, "r:kprobes/%s", name);
989 else
990 snprintf(buf, MAX_EVENT_NAME_LEN, "p:kprobes/%s", name);
991
992 ret = dynevent_str_add(cmd, buf);
993 if (ret)
994 return ret;
995
74403b6c 996 dynevent_arg_init(&arg, 0);
2a588dd1 997 arg.str = loc;
74403b6c 998 ret = dynevent_arg_add(cmd, &arg, NULL);
2a588dd1
TZ
999 if (ret)
1000 return ret;
1001
1002 va_start(args, loc);
1003 for (;;) {
1004 const char *field;
1005
1006 field = va_arg(args, const char *);
1007 if (!field)
1008 break;
1009
1010 if (++cmd->n_fields > MAX_TRACE_ARGS) {
1011 ret = -EINVAL;
1012 break;
1013 }
1014
1015 arg.str = field;
74403b6c 1016 ret = dynevent_arg_add(cmd, &arg, NULL);
2a588dd1
TZ
1017 if (ret)
1018 break;
1019 }
1020 va_end(args);
1021
1022 return ret;
1023}
1024EXPORT_SYMBOL_GPL(__kprobe_event_gen_cmd_start);
1025
1026/**
1027 * __kprobe_event_add_fields - Add probe fields to a kprobe command from arg list
1028 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1029 * @args: Variable number of arg (pairs), one pair for each field
1030 *
1031 * NOTE: Users normally won't want to call this function directly, but
1032 * rather use the kprobe_event_add_fields() wrapper, which
1033 * automatically adds a NULL to the end of the arg list. If this
1034 * function is used directly, make sure the last arg in the variable
1035 * arg list is NULL.
1036 *
1037 * Add probe fields to an existing kprobe command using a variable
1038 * list of args. Fields are added in the same order they're listed.
1039 *
1040 * Return: 0 if successful, error otherwise.
1041 */
1042int __kprobe_event_add_fields(struct dynevent_cmd *cmd, ...)
1043{
1044 struct dynevent_arg arg;
1045 va_list args;
10f129cb 1046 int ret = 0;
2a588dd1
TZ
1047
1048 if (cmd->type != DYNEVENT_TYPE_KPROBE)
1049 return -EINVAL;
1050
74403b6c 1051 dynevent_arg_init(&arg, 0);
2a588dd1
TZ
1052
1053 va_start(args, cmd);
1054 for (;;) {
1055 const char *field;
1056
1057 field = va_arg(args, const char *);
1058 if (!field)
1059 break;
1060
1061 if (++cmd->n_fields > MAX_TRACE_ARGS) {
1062 ret = -EINVAL;
1063 break;
1064 }
1065
1066 arg.str = field;
74403b6c 1067 ret = dynevent_arg_add(cmd, &arg, NULL);
2a588dd1
TZ
1068 if (ret)
1069 break;
1070 }
1071 va_end(args);
1072
1073 return ret;
1074}
1075EXPORT_SYMBOL_GPL(__kprobe_event_add_fields);
1076
1077/**
1078 * kprobe_event_delete - Delete a kprobe event
1079 * @name: The name of the kprobe event to delete
1080 *
1081 * Delete a kprobe event with the give @name from kernel code rather
1082 * than directly from the command line.
1083 *
1084 * Return: 0 if successful, error otherwise.
1085 */
1086int kprobe_event_delete(const char *name)
1087{
1088 char buf[MAX_EVENT_NAME_LEN];
1089
1090 snprintf(buf, MAX_EVENT_NAME_LEN, "-:%s", name);
1091
d262271d 1092 return create_or_delete_trace_kprobe(buf);
2a588dd1
TZ
1093}
1094EXPORT_SYMBOL_GPL(kprobe_event_delete);
1095
6212dd29 1096static int trace_kprobe_release(struct dyn_event *ev)
413d37d1 1097{
6212dd29
MH
1098 struct trace_kprobe *tk = to_trace_kprobe(ev);
1099 int ret = unregister_trace_kprobe(tk);
413d37d1 1100
6212dd29
MH
1101 if (!ret)
1102 free_trace_kprobe(tk);
1103 return ret;
413d37d1
MH
1104}
1105
6212dd29 1106static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev)
413d37d1 1107{
6212dd29 1108 struct trace_kprobe *tk = to_trace_kprobe(ev);
93ccae7a 1109 int i;
413d37d1 1110
fa6f0cc7 1111 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
6a13a0d7
MH
1112 if (trace_kprobe_is_return(tk) && tk->rp.maxactive)
1113 seq_printf(m, "%d", tk->rp.maxactive);
b55ce203
MH
1114 seq_printf(m, ":%s/%s", trace_probe_group_name(&tk->tp),
1115 trace_probe_name(&tk->tp));
413d37d1 1116
c31ffb3f
NK
1117 if (!tk->symbol)
1118 seq_printf(m, " 0x%p", tk->rp.kp.addr);
1119 else if (tk->rp.kp.offset)
1120 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
1121 tk->rp.kp.offset);
413d37d1 1122 else
c31ffb3f 1123 seq_printf(m, " %s", trace_kprobe_symbol(tk));
413d37d1 1124
c31ffb3f
NK
1125 for (i = 0; i < tk->tp.nr_args; i++)
1126 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
fa6f0cc7 1127 seq_putc(m, '\n');
93ccae7a 1128
413d37d1
MH
1129 return 0;
1130}
1131
6212dd29
MH
1132static int probes_seq_show(struct seq_file *m, void *v)
1133{
1134 struct dyn_event *ev = v;
1135
1136 if (!is_trace_kprobe(ev))
1137 return 0;
1138
1139 return trace_kprobe_show(m, ev);
1140}
1141
413d37d1 1142static const struct seq_operations probes_seq_op = {
6212dd29
MH
1143 .start = dyn_event_seq_start,
1144 .next = dyn_event_seq_next,
1145 .stop = dyn_event_seq_stop,
413d37d1
MH
1146 .show = probes_seq_show
1147};
1148
1149static int probes_open(struct inode *inode, struct file *file)
1150{
02ca1521
MH
1151 int ret;
1152
17911ff3
SRV
1153 ret = security_locked_down(LOCKDOWN_TRACEFS);
1154 if (ret)
1155 return ret;
1156
02ca1521 1157 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
6212dd29 1158 ret = dyn_events_release_all(&trace_kprobe_ops);
02ca1521
MH
1159 if (ret < 0)
1160 return ret;
1161 }
413d37d1
MH
1162
1163 return seq_open(file, &probes_seq_op);
1164}
1165
413d37d1
MH
1166static ssize_t probes_write(struct file *file, const char __user *buffer,
1167 size_t count, loff_t *ppos)
1168{
7e465baa 1169 return trace_parse_run_command(file, buffer, count, ppos,
6212dd29 1170 create_or_delete_trace_kprobe);
413d37d1
MH
1171}
1172
1173static const struct file_operations kprobe_events_ops = {
1174 .owner = THIS_MODULE,
1175 .open = probes_open,
1176 .read = seq_read,
1177 .llseek = seq_lseek,
1178 .release = seq_release,
1179 .write = probes_write,
1180};
1181
cd7e7bd5
MH
1182/* Probes profiling interfaces */
1183static int probes_profile_seq_show(struct seq_file *m, void *v)
1184{
6212dd29
MH
1185 struct dyn_event *ev = v;
1186 struct trace_kprobe *tk;
cd7e7bd5 1187
6212dd29
MH
1188 if (!is_trace_kprobe(ev))
1189 return 0;
cd7e7bd5 1190
6212dd29 1191 tk = to_trace_kprobe(ev);
de7b2973 1192 seq_printf(m, " %-44s %15lu %15lu\n",
b55ce203 1193 trace_probe_name(&tk->tp),
f18f97ac 1194 trace_kprobe_nhit(tk),
c31ffb3f 1195 tk->rp.kp.nmissed);
cd7e7bd5
MH
1196
1197 return 0;
1198}
1199
1200static const struct seq_operations profile_seq_op = {
6212dd29
MH
1201 .start = dyn_event_seq_start,
1202 .next = dyn_event_seq_next,
1203 .stop = dyn_event_seq_stop,
cd7e7bd5
MH
1204 .show = probes_profile_seq_show
1205};
1206
1207static int profile_open(struct inode *inode, struct file *file)
1208{
17911ff3
SRV
1209 int ret;
1210
1211 ret = security_locked_down(LOCKDOWN_TRACEFS);
1212 if (ret)
1213 return ret;
1214
cd7e7bd5
MH
1215 return seq_open(file, &profile_seq_op);
1216}
1217
1218static const struct file_operations kprobe_profile_ops = {
1219 .owner = THIS_MODULE,
1220 .open = profile_open,
1221 .read = seq_read,
1222 .llseek = seq_lseek,
1223 .release = seq_release,
1224};
1225
53305928
MH
1226/* Kprobe specific fetch functions */
1227
9de1fec5
CH
1228/* Return the length of string -- including null terminal byte */
1229static nokprobe_inline int
1230fetch_store_strlen_user(unsigned long addr)
1231{
1232 const void __user *uaddr = (__force const void __user *)addr;
1233
1234 return strnlen_user_nofault(uaddr, MAX_STRING_SIZE);
1235}
1236
53305928 1237/* Return the length of string -- including null terminal byte */
9178412d
MH
1238static nokprobe_inline int
1239fetch_store_strlen(unsigned long addr)
53305928 1240{
53305928
MH
1241 int ret, len = 0;
1242 u8 c;
1243
9de1fec5
CH
1244#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1245 if (addr < TASK_SIZE)
1246 return fetch_store_strlen_user(addr);
1247#endif
1248
53305928 1249 do {
fe557319 1250 ret = copy_from_kernel_nofault(&c, (u8 *)addr + len, 1);
53305928
MH
1251 len++;
1252 } while (c && ret == 0 && len < MAX_STRING_SIZE);
1253
9178412d 1254 return (ret < 0) ? ret : len;
53305928
MH
1255}
1256
1257/*
9de1fec5
CH
1258 * Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf
1259 * with max length and relative data location.
53305928 1260 */
9178412d 1261static nokprobe_inline int
9de1fec5 1262fetch_store_string_user(unsigned long addr, void *dest, void *base)
53305928 1263{
9de1fec5 1264 const void __user *uaddr = (__force const void __user *)addr;
9178412d 1265 int maxlen = get_loc_len(*(u32 *)dest);
88903c46 1266 void *__dest;
53305928
MH
1267 long ret;
1268
9178412d
MH
1269 if (unlikely(!maxlen))
1270 return -ENOMEM;
88903c46
MH
1271
1272 __dest = get_loc_data(dest, base);
1273
9de1fec5 1274 ret = strncpy_from_user_nofault(__dest, uaddr, maxlen);
88903c46
MH
1275 if (ret >= 0)
1276 *(u32 *)dest = make_data_loc(ret, __dest - base);
1277
1278 return ret;
1279}
53305928 1280
88903c46 1281/*
9de1fec5
CH
1282 * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
1283 * length and relative data location.
88903c46
MH
1284 */
1285static nokprobe_inline int
9de1fec5 1286fetch_store_string(unsigned long addr, void *dest, void *base)
88903c46 1287{
88903c46
MH
1288 int maxlen = get_loc_len(*(u32 *)dest);
1289 void *__dest;
1290 long ret;
1291
9de1fec5
CH
1292#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1293 if ((unsigned long)addr < TASK_SIZE)
1294 return fetch_store_string_user(addr, dest, base);
1295#endif
1296
88903c46
MH
1297 if (unlikely(!maxlen))
1298 return -ENOMEM;
1299
1300 __dest = get_loc_data(dest, base);
1301
9de1fec5
CH
1302 /*
1303 * Try to get string again, since the string can be changed while
1304 * probing.
1305 */
1306 ret = strncpy_from_kernel_nofault(__dest, (void *)addr, maxlen);
9178412d 1307 if (ret >= 0)
88903c46
MH
1308 *(u32 *)dest = make_data_loc(ret, __dest - base);
1309
9178412d 1310 return ret;
53305928
MH
1311}
1312
e65f7ae7
MH
1313static nokprobe_inline int
1314probe_mem_read_user(void *dest, void *src, size_t size)
1315{
539b75b2
MH
1316 const void __user *uaddr = (__force const void __user *)src;
1317
c0ee37e8 1318 return copy_from_user_nofault(dest, uaddr, size);
e65f7ae7
MH
1319}
1320
9de1fec5
CH
1321static nokprobe_inline int
1322probe_mem_read(void *dest, void *src, size_t size)
1323{
1324#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1325 if ((unsigned long)src < TASK_SIZE)
1326 return probe_mem_read_user(dest, src, size);
1327#endif
fe557319 1328 return copy_from_kernel_nofault(dest, src, size);
9de1fec5
CH
1329}
1330
53305928
MH
1331/* Note that we don't verify it, since the code does not come from user space */
1332static int
1333process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
9178412d 1334 void *base)
53305928
MH
1335{
1336 unsigned long val;
53305928 1337
a6682814 1338retry:
53305928
MH
1339 /* 1st stage: get value from context */
1340 switch (code->op) {
1341 case FETCH_OP_REG:
1342 val = regs_get_register(regs, code->param);
1343 break;
1344 case FETCH_OP_STACK:
1345 val = regs_get_kernel_stack_nth(regs, code->param);
1346 break;
1347 case FETCH_OP_STACKP:
1348 val = kernel_stack_pointer(regs);
1349 break;
1350 case FETCH_OP_RETVAL:
1351 val = regs_return_value(regs);
1352 break;
1353 case FETCH_OP_IMM:
1354 val = code->immediate;
1355 break;
1356 case FETCH_OP_COMM:
1357 val = (unsigned long)current->comm;
1358 break;
a42e3c4d
MH
1359 case FETCH_OP_DATA:
1360 val = (unsigned long)code->data;
1361 break;
a1303af5
MH
1362#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
1363 case FETCH_OP_ARG:
1364 val = regs_get_kernel_argument(regs, code->param);
1365 break;
1366#endif
a6682814
MH
1367 case FETCH_NOP_SYMBOL: /* Ignore a place holder */
1368 code++;
1369 goto retry;
53305928
MH
1370 default:
1371 return -EILSEQ;
1372 }
1373 code++;
1374
9b960a38 1375 return process_fetch_insn_bottom(code, val, dest, base);
53305928
MH
1376}
1377NOKPROBE_SYMBOL(process_fetch_insn)
1378
413d37d1 1379/* Kprobe handler */
3da0f180 1380static nokprobe_inline void
c31ffb3f 1381__kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
7f1d2f82 1382 struct trace_event_file *trace_file)
413d37d1 1383{
93ccae7a 1384 struct kprobe_trace_entry_head *entry;
e3dc9f89 1385 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
8cfcf155
MH
1386 struct trace_event_buffer fbuffer;
1387 int dsize;
413d37d1 1388
7f1d2f82 1389 WARN_ON(call != trace_file->event_call);
41a7dd42 1390
09a5059a 1391 if (trace_trigger_soft_disabled(trace_file))
13a1e4ae 1392 return;
b8820084 1393
36590c50 1394 fbuffer.trace_ctx = tracing_gen_ctx();
8cfcf155 1395 fbuffer.trace_file = trace_file;
413d37d1 1396
c31ffb3f 1397 dsize = __get_data_size(&tk->tp, regs);
413d37d1 1398
8cfcf155
MH
1399 fbuffer.event =
1400 trace_event_buffer_lock_reserve(&fbuffer.buffer, trace_file,
1401 call->event.type,
1402 sizeof(*entry) + tk->tp.size + dsize,
36590c50 1403 fbuffer.trace_ctx);
8cfcf155 1404 if (!fbuffer.event)
1e12a4a7 1405 return;
413d37d1 1406
8cfcf155
MH
1407 fbuffer.regs = regs;
1408 entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
c31ffb3f 1409 entry->ip = (unsigned long)tk->rp.kp.addr;
9178412d 1410 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
413d37d1 1411
8cfcf155 1412 trace_event_buffer_commit(&fbuffer);
413d37d1
MH
1413}
1414
3da0f180 1415static void
c31ffb3f 1416kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
41a7dd42 1417{
b04d52e3 1418 struct event_file_link *link;
41a7dd42 1419
b5f935ee 1420 trace_probe_for_each_link_rcu(link, &tk->tp)
c31ffb3f 1421 __kprobe_trace_func(tk, regs, link->file);
41a7dd42 1422}
3da0f180 1423NOKPROBE_SYMBOL(kprobe_trace_func);
41a7dd42 1424
413d37d1 1425/* Kretprobe handler */
3da0f180 1426static nokprobe_inline void
c31ffb3f 1427__kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
41a7dd42 1428 struct pt_regs *regs,
7f1d2f82 1429 struct trace_event_file *trace_file)
413d37d1 1430{
93ccae7a 1431 struct kretprobe_trace_entry_head *entry;
8cfcf155 1432 struct trace_event_buffer fbuffer;
e3dc9f89 1433 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
8cfcf155 1434 int dsize;
413d37d1 1435
7f1d2f82 1436 WARN_ON(call != trace_file->event_call);
41a7dd42 1437
09a5059a 1438 if (trace_trigger_soft_disabled(trace_file))
13a1e4ae 1439 return;
b8820084 1440
36590c50 1441 fbuffer.trace_ctx = tracing_gen_ctx();
8cfcf155 1442 fbuffer.trace_file = trace_file;
413d37d1 1443
c31ffb3f 1444 dsize = __get_data_size(&tk->tp, regs);
8cfcf155
MH
1445 fbuffer.event =
1446 trace_event_buffer_lock_reserve(&fbuffer.buffer, trace_file,
1447 call->event.type,
1448 sizeof(*entry) + tk->tp.size + dsize,
36590c50 1449 fbuffer.trace_ctx);
8cfcf155 1450 if (!fbuffer.event)
1e12a4a7 1451 return;
413d37d1 1452
8cfcf155
MH
1453 fbuffer.regs = regs;
1454 entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
c31ffb3f 1455 entry->func = (unsigned long)tk->rp.kp.addr;
413d37d1 1456 entry->ret_ip = (unsigned long)ri->ret_addr;
9178412d 1457 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
413d37d1 1458
8cfcf155 1459 trace_event_buffer_commit(&fbuffer);
413d37d1
MH
1460}
1461
3da0f180 1462static void
c31ffb3f 1463kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
41a7dd42
MH
1464 struct pt_regs *regs)
1465{
b04d52e3 1466 struct event_file_link *link;
41a7dd42 1467
b5f935ee 1468 trace_probe_for_each_link_rcu(link, &tk->tp)
c31ffb3f 1469 __kretprobe_trace_func(tk, ri, regs, link->file);
41a7dd42 1470}
3da0f180 1471NOKPROBE_SYMBOL(kretprobe_trace_func);
41a7dd42 1472
413d37d1 1473/* Event entry printers */
b62fdd97 1474static enum print_line_t
a9a57763
SR
1475print_kprobe_event(struct trace_iterator *iter, int flags,
1476 struct trace_event *event)
413d37d1 1477{
93ccae7a 1478 struct kprobe_trace_entry_head *field;
413d37d1 1479 struct trace_seq *s = &iter->seq;
eca0d916 1480 struct trace_probe *tp;
413d37d1 1481
93ccae7a 1482 field = (struct kprobe_trace_entry_head *)iter->ent;
60d53e2c
MH
1483 tp = trace_probe_primary_from_call(
1484 container_of(event, struct trace_event_call, event));
1485 if (WARN_ON_ONCE(!tp))
1486 goto out;
413d37d1 1487
b55ce203 1488 trace_seq_printf(s, "%s: (", trace_probe_name(tp));
6e9f23d1 1489
413d37d1 1490 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
85224da0 1491 goto out;
413d37d1 1492
85224da0 1493 trace_seq_putc(s, ')');
413d37d1 1494
56de7630
MH
1495 if (print_probe_args(s, tp->args, tp->nr_args,
1496 (u8 *)&field[1], field) < 0)
1497 goto out;
413d37d1 1498
85224da0
SRRH
1499 trace_seq_putc(s, '\n');
1500 out:
1501 return trace_handle_return(s);
413d37d1
MH
1502}
1503
b62fdd97 1504static enum print_line_t
a9a57763
SR
1505print_kretprobe_event(struct trace_iterator *iter, int flags,
1506 struct trace_event *event)
413d37d1 1507{
93ccae7a 1508 struct kretprobe_trace_entry_head *field;
413d37d1 1509 struct trace_seq *s = &iter->seq;
eca0d916 1510 struct trace_probe *tp;
413d37d1 1511
93ccae7a 1512 field = (struct kretprobe_trace_entry_head *)iter->ent;
60d53e2c
MH
1513 tp = trace_probe_primary_from_call(
1514 container_of(event, struct trace_event_call, event));
1515 if (WARN_ON_ONCE(!tp))
1516 goto out;
413d37d1 1517
b55ce203 1518 trace_seq_printf(s, "%s: (", trace_probe_name(tp));
6e9f23d1 1519
413d37d1 1520 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
85224da0 1521 goto out;
413d37d1 1522
85224da0 1523 trace_seq_puts(s, " <- ");
413d37d1
MH
1524
1525 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
85224da0 1526 goto out;
413d37d1 1527
85224da0 1528 trace_seq_putc(s, ')');
413d37d1 1529
56de7630
MH
1530 if (print_probe_args(s, tp->args, tp->nr_args,
1531 (u8 *)&field[1], field) < 0)
1532 goto out;
413d37d1 1533
85224da0 1534 trace_seq_putc(s, '\n');
413d37d1 1535
85224da0
SRRH
1536 out:
1537 return trace_handle_return(s);
413d37d1
MH
1538}
1539
413d37d1 1540
2425bcb9 1541static int kprobe_event_define_fields(struct trace_event_call *event_call)
413d37d1 1542{
eeb07b06 1543 int ret;
93ccae7a 1544 struct kprobe_trace_entry_head field;
60d53e2c
MH
1545 struct trace_probe *tp;
1546
1547 tp = trace_probe_primary_from_call(event_call);
1548 if (WARN_ON_ONCE(!tp))
1549 return -ENOENT;
413d37d1 1550
a703d946 1551 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
c31ffb3f 1552
60d53e2c 1553 return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
413d37d1
MH
1554}
1555
2425bcb9 1556static int kretprobe_event_define_fields(struct trace_event_call *event_call)
413d37d1 1557{
eeb07b06 1558 int ret;
93ccae7a 1559 struct kretprobe_trace_entry_head field;
60d53e2c
MH
1560 struct trace_probe *tp;
1561
1562 tp = trace_probe_primary_from_call(event_call);
1563 if (WARN_ON_ONCE(!tp))
1564 return -ENOENT;
413d37d1 1565
a703d946
MH
1566 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1567 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
c31ffb3f 1568
60d53e2c 1569 return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
413d37d1
MH
1570}
1571
07b139c8 1572#ifdef CONFIG_PERF_EVENTS
e08d1c65
MH
1573
1574/* Kprobe profile handler */
9802d865 1575static int
c31ffb3f 1576kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
e08d1c65 1577{
e3dc9f89 1578 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
93ccae7a 1579 struct kprobe_trace_entry_head *entry;
1c024eca 1580 struct hlist_head *head;
e09c8614 1581 int size, __size, dsize;
4ed7c92d 1582 int rctx;
e08d1c65 1583
9802d865 1584 if (bpf_prog_array_valid(call)) {
66665ad2 1585 unsigned long orig_ip = instruction_pointer(regs);
9802d865
JB
1586 int ret;
1587
1588 ret = trace_call_bpf(call, regs);
1589
1590 /*
1591 * We need to check and see if we modified the pc of the
cce188bd
MH
1592 * pt_regs, and if so return 1 so that we don't do the
1593 * single stepping.
9802d865 1594 */
cce188bd 1595 if (orig_ip != instruction_pointer(regs))
9802d865 1596 return 1;
9802d865
JB
1597 if (!ret)
1598 return 0;
1599 }
2541517c 1600
288e984e
ON
1601 head = this_cpu_ptr(call->perf_events);
1602 if (hlist_empty(head))
9802d865 1603 return 0;
288e984e 1604
c31ffb3f
NK
1605 dsize = __get_data_size(&tk->tp, regs);
1606 __size = sizeof(*entry) + tk->tp.size + dsize;
74ebb63e
MH
1607 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1608 size -= sizeof(u32);
ce71b9df 1609
1e1dcd93 1610 entry = perf_trace_buf_alloc(size, NULL, &rctx);
430ad5a6 1611 if (!entry)
9802d865 1612 return 0;
a1a138d0 1613
c31ffb3f 1614 entry->ip = (unsigned long)tk->rp.kp.addr;
e09c8614 1615 memset(&entry[1], 0, dsize);
9178412d 1616 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1e1dcd93 1617 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
8fd0fbbe 1618 head, NULL);
9802d865 1619 return 0;
e08d1c65 1620}
3da0f180 1621NOKPROBE_SYMBOL(kprobe_perf_func);
e08d1c65
MH
1622
1623/* Kretprobe profile handler */
3da0f180 1624static void
c31ffb3f 1625kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
2b106aab 1626 struct pt_regs *regs)
e08d1c65 1627{
e3dc9f89 1628 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
93ccae7a 1629 struct kretprobe_trace_entry_head *entry;
1c024eca 1630 struct hlist_head *head;
e09c8614 1631 int size, __size, dsize;
4ed7c92d 1632 int rctx;
e08d1c65 1633
e87c6bc3 1634 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
2541517c
AS
1635 return;
1636
288e984e
ON
1637 head = this_cpu_ptr(call->perf_events);
1638 if (hlist_empty(head))
1639 return;
1640
c31ffb3f
NK
1641 dsize = __get_data_size(&tk->tp, regs);
1642 __size = sizeof(*entry) + tk->tp.size + dsize;
74ebb63e
MH
1643 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1644 size -= sizeof(u32);
444a2a3b 1645
1e1dcd93 1646 entry = perf_trace_buf_alloc(size, NULL, &rctx);
430ad5a6 1647 if (!entry)
1e12a4a7 1648 return;
e08d1c65 1649
c31ffb3f 1650 entry->func = (unsigned long)tk->rp.kp.addr;
a1a138d0 1651 entry->ret_ip = (unsigned long)ri->ret_addr;
9178412d 1652 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1e1dcd93 1653 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
8fd0fbbe 1654 head, NULL);
e08d1c65 1655}
3da0f180 1656NOKPROBE_SYMBOL(kretprobe_perf_func);
41bdc4b4
YS
1657
1658int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1659 const char **symbol, u64 *probe_offset,
1660 u64 *probe_addr, bool perf_type_tracepoint)
1661{
1662 const char *pevent = trace_event_name(event->tp_event);
1663 const char *group = event->tp_event->class->system;
1664 struct trace_kprobe *tk;
1665
1666 if (perf_type_tracepoint)
1667 tk = find_trace_kprobe(pevent, group);
1668 else
22d5bd68 1669 tk = trace_kprobe_primary_from_call(event->tp_event);
41bdc4b4
YS
1670 if (!tk)
1671 return -EINVAL;
1672
1673 *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1674 : BPF_FD_TYPE_KPROBE;
1675 if (tk->symbol) {
1676 *symbol = tk->symbol;
1677 *probe_offset = tk->rp.kp.offset;
1678 *probe_addr = 0;
1679 } else {
1680 *symbol = NULL;
1681 *probe_offset = 0;
1682 *probe_addr = (unsigned long)tk->rp.kp.addr;
1683 }
1684 return 0;
1685}
07b139c8 1686#endif /* CONFIG_PERF_EVENTS */
50d78056 1687
3fe3d619
ON
1688/*
1689 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1690 *
1691 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1692 * lockless, but we can't race with this __init function.
1693 */
2425bcb9 1694static int kprobe_register(struct trace_event_call *event,
fbc1963d 1695 enum trace_reg type, void *data)
2239291a 1696{
7f1d2f82 1697 struct trace_event_file *file = data;
1538f888 1698
2239291a
SR
1699 switch (type) {
1700 case TRACE_REG_REGISTER:
60d53e2c 1701 return enable_trace_kprobe(event, file);
2239291a 1702 case TRACE_REG_UNREGISTER:
60d53e2c 1703 return disable_trace_kprobe(event, file);
2239291a
SR
1704
1705#ifdef CONFIG_PERF_EVENTS
1706 case TRACE_REG_PERF_REGISTER:
60d53e2c 1707 return enable_trace_kprobe(event, NULL);
2239291a 1708 case TRACE_REG_PERF_UNREGISTER:
60d53e2c 1709 return disable_trace_kprobe(event, NULL);
ceec0b6f
JO
1710 case TRACE_REG_PERF_OPEN:
1711 case TRACE_REG_PERF_CLOSE:
489c75c3
JO
1712 case TRACE_REG_PERF_ADD:
1713 case TRACE_REG_PERF_DEL:
ceec0b6f 1714 return 0;
2239291a
SR
1715#endif
1716 }
1717 return 0;
1718}
50d78056 1719
3da0f180 1720static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
50d78056 1721{
c31ffb3f 1722 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
9802d865 1723 int ret = 0;
e08d1c65 1724
a7636d9e 1725 raw_cpu_inc(*tk->nhit);
48182bd2 1726
747774d6 1727 if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
c31ffb3f 1728 kprobe_trace_func(tk, regs);
07b139c8 1729#ifdef CONFIG_PERF_EVENTS
747774d6 1730 if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
9802d865 1731 ret = kprobe_perf_func(tk, regs);
07b139c8 1732#endif
9802d865 1733 return ret;
50d78056 1734}
3da0f180 1735NOKPROBE_SYMBOL(kprobe_dispatcher);
50d78056 1736
3da0f180
MH
1737static int
1738kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
50d78056 1739{
d741bf41
PZ
1740 struct kretprobe *rp = get_kretprobe(ri);
1741 struct trace_kprobe *tk = container_of(rp, struct trace_kprobe, rp);
50d78056 1742
a7636d9e 1743 raw_cpu_inc(*tk->nhit);
48182bd2 1744
747774d6 1745 if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
c31ffb3f 1746 kretprobe_trace_func(tk, ri, regs);
07b139c8 1747#ifdef CONFIG_PERF_EVENTS
747774d6 1748 if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
c31ffb3f 1749 kretprobe_perf_func(tk, ri, regs);
07b139c8 1750#endif
f2cc020d 1751 return 0; /* We don't tweak kernel, so just return 0 */
50d78056 1752}
3da0f180 1753NOKPROBE_SYMBOL(kretprobe_dispatcher);
e08d1c65 1754
a9a57763
SR
1755static struct trace_event_functions kretprobe_funcs = {
1756 .trace = print_kretprobe_event
1757};
1758
1759static struct trace_event_functions kprobe_funcs = {
1760 .trace = print_kprobe_event
1761};
1762
04ae87a5
PZ
1763static struct trace_event_fields kretprobe_fields_array[] = {
1764 { .type = TRACE_FUNCTION_TYPE,
1765 .define_fields = kretprobe_event_define_fields },
1766 {}
1767};
1768
1769static struct trace_event_fields kprobe_fields_array[] = {
1770 { .type = TRACE_FUNCTION_TYPE,
1771 .define_fields = kprobe_event_define_fields },
1772 {}
1773};
1774
e3dc9f89 1775static inline void init_trace_event_call(struct trace_kprobe *tk)
413d37d1 1776{
e3dc9f89
MH
1777 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1778
c31ffb3f 1779 if (trace_kprobe_is_return(tk)) {
80decc70 1780 call->event.funcs = &kretprobe_funcs;
04ae87a5 1781 call->class->fields_array = kretprobe_fields_array;
413d37d1 1782 } else {
80decc70 1783 call->event.funcs = &kprobe_funcs;
04ae87a5 1784 call->class->fields_array = kprobe_fields_array;
413d37d1 1785 }
e12f03d7
SL
1786
1787 call->flags = TRACE_EVENT_FL_KPROBE;
1788 call->class->reg = kprobe_register;
e12f03d7
SL
1789}
1790
1791static int register_kprobe_event(struct trace_kprobe *tk)
1792{
e3dc9f89 1793 init_trace_event_call(tk);
f730e0f2 1794
46e5376d 1795 return trace_probe_register_event_call(&tk->tp);
413d37d1
MH
1796}
1797
c31ffb3f 1798static int unregister_kprobe_event(struct trace_kprobe *tk)
413d37d1 1799{
46e5376d 1800 return trace_probe_unregister_event_call(&tk->tp);
413d37d1
MH
1801}
1802
e12f03d7
SL
1803#ifdef CONFIG_PERF_EVENTS
1804/* create a trace_kprobe, but don't add it to global lists */
1805struct trace_event_call *
1806create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1807 bool is_return)
1808{
1809 struct trace_kprobe *tk;
1810 int ret;
1811 char *event;
1812
1813 /*
6212dd29 1814 * local trace_kprobes are not added to dyn_event, so they are never
e12f03d7
SL
1815 * searched in find_trace_kprobe(). Therefore, there is no concern of
1816 * duplicated name here.
1817 */
1818 event = func ? func : "DUMMY_EVENT";
1819
1820 tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1821 offs, 0 /* maxactive */, 0 /* nargs */,
1822 is_return);
1823
1824 if (IS_ERR(tk)) {
1825 pr_info("Failed to allocate trace_probe.(%d)\n",
1826 (int)PTR_ERR(tk));
1827 return ERR_CAST(tk);
1828 }
1829
e3dc9f89 1830 init_trace_event_call(tk);
e12f03d7 1831
0a46c854 1832 if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
e12f03d7
SL
1833 ret = -ENOMEM;
1834 goto error;
1835 }
1836
1837 ret = __register_trace_kprobe(tk);
f730e0f2 1838 if (ret < 0)
e12f03d7
SL
1839 goto error;
1840
e3dc9f89 1841 return trace_probe_event_call(&tk->tp);
e12f03d7
SL
1842error:
1843 free_trace_kprobe(tk);
1844 return ERR_PTR(ret);
1845}
1846
1847void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1848{
1849 struct trace_kprobe *tk;
1850
60d53e2c
MH
1851 tk = trace_kprobe_primary_from_call(event_call);
1852 if (unlikely(!tk))
1853 return;
e12f03d7
SL
1854
1855 if (trace_probe_is_enabled(&tk->tp)) {
1856 WARN_ON(1);
1857 return;
1858 }
1859
1860 __unregister_trace_kprobe(tk);
0fc8c358 1861
e12f03d7
SL
1862 free_trace_kprobe(tk);
1863}
1864#endif /* CONFIG_PERF_EVENTS */
1865
970988e1
MH
1866static __init void enable_boot_kprobe_events(void)
1867{
1868 struct trace_array *tr = top_trace_array();
1869 struct trace_event_file *file;
1870 struct trace_kprobe *tk;
1871 struct dyn_event *pos;
1872
1873 mutex_lock(&event_mutex);
1874 for_each_trace_kprobe(tk, pos) {
1875 list_for_each_entry(file, &tr->events, list)
e3dc9f89 1876 if (file->event_call == trace_probe_event_call(&tk->tp))
970988e1
MH
1877 trace_event_enable_disable(file, 1, 0);
1878 }
1879 mutex_unlock(&event_mutex);
1880}
1881
1882static __init void setup_boot_kprobe_events(void)
1883{
1884 char *p, *cmd = kprobe_boot_events_buf;
1885 int ret;
1886
1887 strreplace(kprobe_boot_events_buf, ',', ' ');
1888
1889 while (cmd && *cmd != '\0') {
1890 p = strchr(cmd, ';');
1891 if (p)
1892 *p++ = '\0';
1893
d262271d 1894 ret = create_or_delete_trace_kprobe(cmd);
970988e1
MH
1895 if (ret)
1896 pr_warn("Failed to add event(%d): %s\n", ret, cmd);
1897
1898 cmd = p;
1899 }
1900
1901 enable_boot_kprobe_events();
1902}
1903
d8d4c6d0 1904/*
ba0fbfbb
MH
1905 * Register dynevent at core_initcall. This allows kernel to setup kprobe
1906 * events in postcore_initcall without tracefs.
d8d4c6d0
MH
1907 */
1908static __init int init_kprobe_trace_early(void)
413d37d1 1909{
6212dd29
MH
1910 int ret;
1911
1912 ret = dyn_event_register(&trace_kprobe_ops);
1913 if (ret)
1914 return ret;
413d37d1 1915
c31ffb3f 1916 if (register_module_notifier(&trace_kprobe_module_nb))
61424318
MH
1917 return -EINVAL;
1918
d8d4c6d0
MH
1919 return 0;
1920}
ba0fbfbb 1921core_initcall(init_kprobe_trace_early);
d8d4c6d0
MH
1922
1923/* Make a tracefs interface for controlling probe points */
1924static __init int init_kprobe_trace(void)
1925{
22c36b18 1926 int ret;
d8d4c6d0
MH
1927 struct dentry *entry;
1928
22c36b18
WY
1929 ret = tracing_init_dentry();
1930 if (ret)
413d37d1
MH
1931 return 0;
1932
22c36b18 1933 entry = tracefs_create_file("kprobe_events", 0644, NULL,
413d37d1
MH
1934 NULL, &kprobe_events_ops);
1935
cd7e7bd5 1936 /* Event list interface */
413d37d1 1937 if (!entry)
a395d6a7 1938 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
cd7e7bd5
MH
1939
1940 /* Profile interface */
22c36b18 1941 entry = tracefs_create_file("kprobe_profile", 0444, NULL,
cd7e7bd5
MH
1942 NULL, &kprobe_profile_ops);
1943
1944 if (!entry)
a395d6a7 1945 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
970988e1
MH
1946
1947 setup_boot_kprobe_events();
1948
413d37d1
MH
1949 return 0;
1950}
1951fs_initcall(init_kprobe_trace);
1952
1953
1954#ifdef CONFIG_FTRACE_STARTUP_TEST
26a346f2 1955static __init struct trace_event_file *
c31ffb3f 1956find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
41a7dd42 1957{
7f1d2f82 1958 struct trace_event_file *file;
41a7dd42
MH
1959
1960 list_for_each_entry(file, &tr->events, list)
e3dc9f89 1961 if (file->event_call == trace_probe_event_call(&tk->tp))
41a7dd42
MH
1962 return file;
1963
1964 return NULL;
1965}
1966
3fe3d619 1967/*
c31ffb3f 1968 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
3fe3d619
ON
1969 * stage, we can do this lockless.
1970 */
413d37d1
MH
1971static __init int kprobe_trace_self_tests_init(void)
1972{
231e36f4 1973 int ret, warn = 0;
413d37d1 1974 int (*target)(int, int, int, int, int, int);
c31ffb3f 1975 struct trace_kprobe *tk;
7f1d2f82 1976 struct trace_event_file *file;
413d37d1 1977
748ec3a2
YY
1978 if (tracing_is_disabled())
1979 return -ENODEV;
1980
60efe21e 1981 if (tracing_selftest_disabled)
b6399cc7 1982 return 0;
b6399cc7 1983
413d37d1
MH
1984 target = kprobe_trace_selftest_target;
1985
1986 pr_info("Testing kprobe tracing: ");
1987
d262271d 1988 ret = create_or_delete_trace_kprobe("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)");
231e36f4 1989 if (WARN_ON_ONCE(ret)) {
41a7dd42 1990 pr_warn("error on probing function entry.\n");
231e36f4
MH
1991 warn++;
1992 } else {
1993 /* Enable trace point */
c31ffb3f
NK
1994 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1995 if (WARN_ON_ONCE(tk == NULL)) {
41a7dd42 1996 pr_warn("error on getting new probe.\n");
231e36f4 1997 warn++;
41a7dd42 1998 } else {
c31ffb3f 1999 file = find_trace_probe_file(tk, top_trace_array());
41a7dd42
MH
2000 if (WARN_ON_ONCE(file == NULL)) {
2001 pr_warn("error on getting probe file.\n");
2002 warn++;
2003 } else
60d53e2c
MH
2004 enable_trace_kprobe(
2005 trace_probe_event_call(&tk->tp), file);
41a7dd42 2006 }
231e36f4 2007 }
413d37d1 2008
d262271d 2009 ret = create_or_delete_trace_kprobe("r:testprobe2 kprobe_trace_selftest_target $retval");
231e36f4 2010 if (WARN_ON_ONCE(ret)) {
41a7dd42 2011 pr_warn("error on probing function return.\n");
231e36f4
MH
2012 warn++;
2013 } else {
2014 /* Enable trace point */
c31ffb3f
NK
2015 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
2016 if (WARN_ON_ONCE(tk == NULL)) {
41a7dd42 2017 pr_warn("error on getting 2nd new probe.\n");
231e36f4 2018 warn++;
41a7dd42 2019 } else {
c31ffb3f 2020 file = find_trace_probe_file(tk, top_trace_array());
41a7dd42
MH
2021 if (WARN_ON_ONCE(file == NULL)) {
2022 pr_warn("error on getting probe file.\n");
2023 warn++;
2024 } else
60d53e2c
MH
2025 enable_trace_kprobe(
2026 trace_probe_event_call(&tk->tp), file);
41a7dd42 2027 }
231e36f4
MH
2028 }
2029
2030 if (warn)
2031 goto end;
413d37d1
MH
2032
2033 ret = target(1, 2, 3, 4, 5, 6);
2034
d4d7ccc8
MN
2035 /*
2036 * Not expecting an error here, the check is only to prevent the
2037 * optimizer from removing the call to target() as otherwise there
2038 * are no side-effects and the call is never performed.
2039 */
2040 if (ret != 21)
2041 warn++;
2042
02ca1521 2043 /* Disable trace points before removing it */
c31ffb3f
NK
2044 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
2045 if (WARN_ON_ONCE(tk == NULL)) {
41a7dd42 2046 pr_warn("error on getting test probe.\n");
02ca1521 2047 warn++;
41a7dd42 2048 } else {
d4d7ccc8
MN
2049 if (trace_kprobe_nhit(tk) != 1) {
2050 pr_warn("incorrect number of testprobe hits\n");
2051 warn++;
2052 }
2053
c31ffb3f 2054 file = find_trace_probe_file(tk, top_trace_array());
41a7dd42
MH
2055 if (WARN_ON_ONCE(file == NULL)) {
2056 pr_warn("error on getting probe file.\n");
2057 warn++;
2058 } else
60d53e2c
MH
2059 disable_trace_kprobe(
2060 trace_probe_event_call(&tk->tp), file);
41a7dd42 2061 }
02ca1521 2062
c31ffb3f
NK
2063 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
2064 if (WARN_ON_ONCE(tk == NULL)) {
41a7dd42 2065 pr_warn("error on getting 2nd test probe.\n");
02ca1521 2066 warn++;
41a7dd42 2067 } else {
d4d7ccc8
MN
2068 if (trace_kprobe_nhit(tk) != 1) {
2069 pr_warn("incorrect number of testprobe2 hits\n");
2070 warn++;
2071 }
2072
c31ffb3f 2073 file = find_trace_probe_file(tk, top_trace_array());
41a7dd42
MH
2074 if (WARN_ON_ONCE(file == NULL)) {
2075 pr_warn("error on getting probe file.\n");
2076 warn++;
2077 } else
60d53e2c
MH
2078 disable_trace_kprobe(
2079 trace_probe_event_call(&tk->tp), file);
41a7dd42 2080 }
02ca1521 2081
d262271d 2082 ret = create_or_delete_trace_kprobe("-:testprobe");
231e36f4 2083 if (WARN_ON_ONCE(ret)) {
41a7dd42 2084 pr_warn("error on deleting a probe.\n");
231e36f4
MH
2085 warn++;
2086 }
2087
d262271d 2088 ret = create_or_delete_trace_kprobe("-:testprobe2");
231e36f4 2089 if (WARN_ON_ONCE(ret)) {
41a7dd42 2090 pr_warn("error on deleting a probe.\n");
231e36f4
MH
2091 warn++;
2092 }
413d37d1 2093
231e36f4 2094end:
6212dd29
MH
2095 ret = dyn_events_release_all(&trace_kprobe_ops);
2096 if (WARN_ON_ONCE(ret)) {
2097 pr_warn("error on cleaning up probes.\n");
2098 warn++;
2099 }
30e7d894
TG
2100 /*
2101 * Wait for the optimizer work to finish. Otherwise it might fiddle
2102 * with probes in already freed __init text.
2103 */
2104 wait_for_kprobe_optimizer();
231e36f4
MH
2105 if (warn)
2106 pr_cont("NG: Some tests are failed. Please check them.\n");
2107 else
2108 pr_cont("OK\n");
413d37d1
MH
2109 return 0;
2110}
2111
2112late_initcall(kprobe_trace_self_tests_init);
2113
2114#endif