blob: 5de084dab4fa65dc30a663d57af86d73ef971581 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Kprobes-based tracing events
4 *
5 * Created by Masami Hiramatsu <mhiramat@redhat.com>
6 *
7 */
8#define pr_fmt(fmt) "trace_kprobe: " fmt
9
David Brazdil0f672f62019-12-10 10:32:29 +000010#include <linux/security.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011#include <linux/module.h>
12#include <linux/uaccess.h>
13#include <linux/rculist.h>
14#include <linux/error-injection.h>
15
David Brazdil0f672f62019-12-10 10:32:29 +000016#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
17
18#include "trace_dynevent.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019#include "trace_kprobe_selftest.h"
20#include "trace_probe.h"
David Brazdil0f672f62019-12-10 10:32:29 +000021#include "trace_probe_tmpl.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000022
23#define KPROBE_EVENT_SYSTEM "kprobes"
24#define KRETPROBE_MAXACTIVE_MAX 4096
David Brazdil0f672f62019-12-10 10:32:29 +000025#define MAX_KPROBE_CMDLINE_SIZE 1024
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026
David Brazdil0f672f62019-12-10 10:32:29 +000027/* Kprobe early definition from command line */
28static char kprobe_boot_events_buf[COMMAND_LINE_SIZE] __initdata;
29static bool kprobe_boot_events_enabled __initdata;
30
31static int __init set_kprobe_boot_events(char *str)
32{
33 strlcpy(kprobe_boot_events_buf, str, COMMAND_LINE_SIZE);
34 return 0;
35}
36__setup("kprobe_event=", set_kprobe_boot_events);
37
38static int trace_kprobe_create(int argc, const char **argv);
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,
43 int argc, const char **argv, struct dyn_event *ev);
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
53/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054 * Kprobe event core functions
55 */
56struct trace_kprobe {
David Brazdil0f672f62019-12-10 10:32:29 +000057 struct dyn_event devent;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000058 struct kretprobe rp; /* Use rp.kp for kprobe use */
59 unsigned long __percpu *nhit;
60 const char *symbol; /* symbol name */
61 struct trace_probe tp;
62};
63
David Brazdil0f672f62019-12-10 10:32:29 +000064static 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083#define SIZEOF_TRACE_KPROBE(n) \
84 (offsetof(struct trace_kprobe, tp.args) + \
85 (sizeof(struct probe_arg) * (n)))
86
87static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
88{
89 return tk->rp.handler != NULL;
90}
91
92static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
93{
94 return tk->symbol ? tk->symbol : "unknown";
95}
96
97static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
98{
99 return tk->rp.kp.offset;
100}
101
102static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
103{
104 return !!(kprobe_gone(&tk->rp.kp));
105}
106
107static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
108 struct module *mod)
109{
110 int len = strlen(mod->name);
111 const char *name = trace_kprobe_symbol(tk);
112 return strncmp(mod->name, name, len) == 0 && name[len] == ':';
113}
114
115static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
116{
117 char *p;
118 bool ret;
119
120 if (!tk->symbol)
121 return false;
122 p = strchr(tk->symbol, ':');
123 if (!p)
124 return true;
125 *p = '\0';
126 mutex_lock(&module_mutex);
127 ret = !!find_module(tk->symbol);
128 mutex_unlock(&module_mutex);
129 *p = ':';
130
131 return ret;
132}
133
David Brazdil0f672f62019-12-10 10:32:29 +0000134static bool trace_kprobe_is_busy(struct dyn_event *ev)
135{
136 struct trace_kprobe *tk = to_trace_kprobe(ev);
137
138 return trace_probe_is_enabled(&tk->tp);
139}
140
141static bool trace_kprobe_match_command_head(struct trace_kprobe *tk,
142 int argc, const char **argv)
143{
144 char buf[MAX_ARGSTR_LEN + 1];
145
146 if (!argc)
147 return true;
148
149 if (!tk->symbol)
150 snprintf(buf, sizeof(buf), "0x%p", tk->rp.kp.addr);
151 else if (tk->rp.kp.offset)
152 snprintf(buf, sizeof(buf), "%s+%u",
153 trace_kprobe_symbol(tk), tk->rp.kp.offset);
154 else
155 snprintf(buf, sizeof(buf), "%s", trace_kprobe_symbol(tk));
156 if (strcmp(buf, argv[0]))
157 return false;
158 argc--; argv++;
159
160 return trace_probe_match_command_args(&tk->tp, argc, argv);
161}
162
163static bool trace_kprobe_match(const char *system, const char *event,
164 int argc, const char **argv, struct dyn_event *ev)
165{
166 struct trace_kprobe *tk = to_trace_kprobe(ev);
167
168 return strcmp(trace_probe_name(&tk->tp), event) == 0 &&
169 (!system || strcmp(trace_probe_group_name(&tk->tp), system) == 0) &&
170 trace_kprobe_match_command_head(tk, argc, argv);
171}
172
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000173static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
174{
175 unsigned long nhit = 0;
176 int cpu;
177
178 for_each_possible_cpu(cpu)
179 nhit += *per_cpu_ptr(tk->nhit, cpu);
180
181 return nhit;
182}
183
David Brazdil0f672f62019-12-10 10:32:29 +0000184static nokprobe_inline bool trace_kprobe_is_registered(struct trace_kprobe *tk)
185{
186 return !(list_empty(&tk->rp.kp.list) &&
187 hlist_unhashed(&tk->rp.kp.hlist));
188}
189
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000190/* Return 0 if it fails to find the symbol address */
191static nokprobe_inline
192unsigned long trace_kprobe_address(struct trace_kprobe *tk)
193{
194 unsigned long addr;
195
196 if (tk->symbol) {
197 addr = (unsigned long)
198 kallsyms_lookup_name(trace_kprobe_symbol(tk));
199 if (addr)
200 addr += tk->rp.kp.offset;
201 } else {
202 addr = (unsigned long)tk->rp.kp.addr;
203 }
204 return addr;
205}
206
David Brazdil0f672f62019-12-10 10:32:29 +0000207static nokprobe_inline struct trace_kprobe *
208trace_kprobe_primary_from_call(struct trace_event_call *call)
209{
210 struct trace_probe *tp;
211
212 tp = trace_probe_primary_from_call(call);
213 if (WARN_ON_ONCE(!tp))
214 return NULL;
215
216 return container_of(tp, struct trace_kprobe, tp);
217}
218
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000219bool trace_kprobe_on_func_entry(struct trace_event_call *call)
220{
David Brazdil0f672f62019-12-10 10:32:29 +0000221 struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000222
Olivier Deprez0e641232021-09-23 10:07:05 +0200223 return tk ? (kprobe_on_func_entry(tk->rp.kp.addr,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000224 tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
Olivier Deprez0e641232021-09-23 10:07:05 +0200225 tk->rp.kp.addr ? 0 : tk->rp.kp.offset) == 0) : false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000226}
227
228bool trace_kprobe_error_injectable(struct trace_event_call *call)
229{
David Brazdil0f672f62019-12-10 10:32:29 +0000230 struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000231
David Brazdil0f672f62019-12-10 10:32:29 +0000232 return tk ? within_error_injection_list(trace_kprobe_address(tk)) :
233 false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000234}
235
236static int register_kprobe_event(struct trace_kprobe *tk);
237static int unregister_kprobe_event(struct trace_kprobe *tk);
238
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000239static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
240static int kretprobe_dispatcher(struct kretprobe_instance *ri,
241 struct pt_regs *regs);
242
David Brazdil0f672f62019-12-10 10:32:29 +0000243static void free_trace_kprobe(struct trace_kprobe *tk)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000244{
David Brazdil0f672f62019-12-10 10:32:29 +0000245 if (tk) {
246 trace_probe_cleanup(&tk->tp);
247 kfree(tk->symbol);
248 free_percpu(tk->nhit);
249 kfree(tk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000250 }
251}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000252
253/*
254 * Allocate new trace_probe and initialize it (including kprobes).
255 */
256static struct trace_kprobe *alloc_trace_kprobe(const char *group,
257 const char *event,
258 void *addr,
259 const char *symbol,
260 unsigned long offs,
261 int maxactive,
262 int nargs, bool is_return)
263{
264 struct trace_kprobe *tk;
265 int ret = -ENOMEM;
266
267 tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
268 if (!tk)
269 return ERR_PTR(ret);
270
271 tk->nhit = alloc_percpu(unsigned long);
272 if (!tk->nhit)
273 goto error;
274
275 if (symbol) {
276 tk->symbol = kstrdup(symbol, GFP_KERNEL);
277 if (!tk->symbol)
278 goto error;
279 tk->rp.kp.symbol_name = tk->symbol;
280 tk->rp.kp.offset = offs;
281 } else
282 tk->rp.kp.addr = addr;
283
284 if (is_return)
285 tk->rp.handler = kretprobe_dispatcher;
286 else
287 tk->rp.kp.pre_handler = kprobe_dispatcher;
288
289 tk->rp.maxactive = maxactive;
David Brazdil0f672f62019-12-10 10:32:29 +0000290 INIT_HLIST_NODE(&tk->rp.kp.hlist);
291 INIT_LIST_HEAD(&tk->rp.kp.list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000292
Olivier Deprez0e641232021-09-23 10:07:05 +0200293 ret = trace_probe_init(&tk->tp, event, group, false);
David Brazdil0f672f62019-12-10 10:32:29 +0000294 if (ret < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000295 goto error;
296
David Brazdil0f672f62019-12-10 10:32:29 +0000297 dyn_event_init(&tk->devent, &trace_kprobe_ops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000298 return tk;
299error:
David Brazdil0f672f62019-12-10 10:32:29 +0000300 free_trace_kprobe(tk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000301 return ERR_PTR(ret);
302}
303
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000304static struct trace_kprobe *find_trace_kprobe(const char *event,
305 const char *group)
306{
David Brazdil0f672f62019-12-10 10:32:29 +0000307 struct dyn_event *pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000308 struct trace_kprobe *tk;
309
David Brazdil0f672f62019-12-10 10:32:29 +0000310 for_each_trace_kprobe(tk, pos)
311 if (strcmp(trace_probe_name(&tk->tp), event) == 0 &&
312 strcmp(trace_probe_group_name(&tk->tp), group) == 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000313 return tk;
314 return NULL;
315}
316
317static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
318{
319 int ret = 0;
320
David Brazdil0f672f62019-12-10 10:32:29 +0000321 if (trace_kprobe_is_registered(tk) && !trace_kprobe_has_gone(tk)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000322 if (trace_kprobe_is_return(tk))
323 ret = enable_kretprobe(&tk->rp);
324 else
325 ret = enable_kprobe(&tk->rp.kp);
326 }
327
328 return ret;
329}
330
David Brazdil0f672f62019-12-10 10:32:29 +0000331static void __disable_trace_kprobe(struct trace_probe *tp)
332{
333 struct trace_probe *pos;
334 struct trace_kprobe *tk;
335
336 list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
337 tk = container_of(pos, struct trace_kprobe, tp);
338 if (!trace_kprobe_is_registered(tk))
339 continue;
340 if (trace_kprobe_is_return(tk))
341 disable_kretprobe(&tk->rp);
342 else
343 disable_kprobe(&tk->rp.kp);
344 }
345}
346
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000347/*
348 * Enable trace_probe
349 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
350 */
David Brazdil0f672f62019-12-10 10:32:29 +0000351static int enable_trace_kprobe(struct trace_event_call *call,
352 struct trace_event_file *file)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000353{
David Brazdil0f672f62019-12-10 10:32:29 +0000354 struct trace_probe *pos, *tp;
355 struct trace_kprobe *tk;
356 bool enabled;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000357 int ret = 0;
358
David Brazdil0f672f62019-12-10 10:32:29 +0000359 tp = trace_probe_primary_from_call(call);
360 if (WARN_ON_ONCE(!tp))
361 return -ENODEV;
362 enabled = trace_probe_is_enabled(tp);
363
364 /* This also changes "enabled" state */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000365 if (file) {
David Brazdil0f672f62019-12-10 10:32:29 +0000366 ret = trace_probe_add_file(tp, file);
367 if (ret)
368 return ret;
369 } else
370 trace_probe_set_flag(tp, TP_FLAG_PROFILE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000371
David Brazdil0f672f62019-12-10 10:32:29 +0000372 if (enabled)
373 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000374
David Brazdil0f672f62019-12-10 10:32:29 +0000375 list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
376 tk = container_of(pos, struct trace_kprobe, tp);
377 if (trace_kprobe_has_gone(tk))
378 continue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000379 ret = __enable_trace_kprobe(tk);
380 if (ret)
David Brazdil0f672f62019-12-10 10:32:29 +0000381 break;
382 enabled = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000383 }
David Brazdil0f672f62019-12-10 10:32:29 +0000384
385 if (ret) {
386 /* Failed to enable one of them. Roll back all */
387 if (enabled)
388 __disable_trace_kprobe(tp);
389 if (file)
390 trace_probe_remove_file(tp, file);
391 else
392 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
393 }
394
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000395 return ret;
396}
397
398/*
399 * Disable trace_probe
400 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
401 */
David Brazdil0f672f62019-12-10 10:32:29 +0000402static int disable_trace_kprobe(struct trace_event_call *call,
403 struct trace_event_file *file)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000404{
David Brazdil0f672f62019-12-10 10:32:29 +0000405 struct trace_probe *tp;
406
407 tp = trace_probe_primary_from_call(call);
408 if (WARN_ON_ONCE(!tp))
409 return -ENODEV;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000410
411 if (file) {
David Brazdil0f672f62019-12-10 10:32:29 +0000412 if (!trace_probe_get_file_link(tp, file))
413 return -ENOENT;
414 if (!trace_probe_has_single_file(tp))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000415 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +0000416 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000417 } else
David Brazdil0f672f62019-12-10 10:32:29 +0000418 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000419
David Brazdil0f672f62019-12-10 10:32:29 +0000420 if (!trace_probe_is_enabled(tp))
421 __disable_trace_kprobe(tp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000422
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000423 out:
David Brazdil0f672f62019-12-10 10:32:29 +0000424 if (file)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000425 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000426 * Synchronization is done in below function. For perf event,
427 * file == NULL and perf_trace_event_unreg() calls
428 * tracepoint_synchronize_unregister() to ensure synchronize
429 * event. We don't need to care about it.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000430 */
David Brazdil0f672f62019-12-10 10:32:29 +0000431 trace_probe_remove_file(tp, file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000432
David Brazdil0f672f62019-12-10 10:32:29 +0000433 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000434}
435
Olivier Deprez0e641232021-09-23 10:07:05 +0200436#if defined(CONFIG_DYNAMIC_FTRACE) && \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000437 !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
Olivier Deprez0e641232021-09-23 10:07:05 +0200438static bool __within_notrace_func(unsigned long addr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000439{
Olivier Deprez0e641232021-09-23 10:07:05 +0200440 unsigned long offset, size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000441
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000442 if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
443 return false;
444
445 /* Get the entry address of the target function */
446 addr -= offset;
447
448 /*
449 * Since ftrace_location_range() does inclusive range check, we need
450 * to subtract 1 byte from the end address.
451 */
452 return !ftrace_location_range(addr, addr + size - 1);
453}
Olivier Deprez0e641232021-09-23 10:07:05 +0200454
455static bool within_notrace_func(struct trace_kprobe *tk)
456{
457 unsigned long addr = trace_kprobe_address(tk);
458 char symname[KSYM_NAME_LEN], *p;
459
460 if (!__within_notrace_func(addr))
461 return false;
462
463 /* Check if the address is on a suffixed-symbol */
464 if (!lookup_symbol_name(addr, symname)) {
465 p = strchr(symname, '.');
466 if (!p)
467 return true;
468 *p = '\0';
469 addr = (unsigned long)kprobe_lookup_name(symname, 0);
470 if (addr)
471 return __within_notrace_func(addr);
472 }
473
474 return true;
475}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000476#else
477#define within_notrace_func(tk) (false)
478#endif
479
480/* Internal register function - just handle k*probes and flags */
481static int __register_trace_kprobe(struct trace_kprobe *tk)
482{
483 int i, ret;
484
David Brazdil0f672f62019-12-10 10:32:29 +0000485 ret = security_locked_down(LOCKDOWN_KPROBES);
486 if (ret)
487 return ret;
488
489 if (trace_kprobe_is_registered(tk))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000490 return -EINVAL;
491
492 if (within_notrace_func(tk)) {
493 pr_warn("Could not probe notrace function %s\n",
494 trace_kprobe_symbol(tk));
495 return -EINVAL;
496 }
497
David Brazdil0f672f62019-12-10 10:32:29 +0000498 for (i = 0; i < tk->tp.nr_args; i++) {
499 ret = traceprobe_update_arg(&tk->tp.args[i]);
500 if (ret)
501 return ret;
502 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000503
504 /* Set/clear disabled flag according to tp->flag */
505 if (trace_probe_is_enabled(&tk->tp))
506 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
507 else
508 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
509
510 if (trace_kprobe_is_return(tk))
511 ret = register_kretprobe(&tk->rp);
512 else
513 ret = register_kprobe(&tk->rp.kp);
514
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000515 return ret;
516}
517
518/* Internal unregister function - just handle k*probes and flags */
519static void __unregister_trace_kprobe(struct trace_kprobe *tk)
520{
David Brazdil0f672f62019-12-10 10:32:29 +0000521 if (trace_kprobe_is_registered(tk)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000522 if (trace_kprobe_is_return(tk))
523 unregister_kretprobe(&tk->rp);
524 else
525 unregister_kprobe(&tk->rp.kp);
David Brazdil0f672f62019-12-10 10:32:29 +0000526 /* Cleanup kprobe for reuse and mark it unregistered */
527 INIT_HLIST_NODE(&tk->rp.kp.hlist);
528 INIT_LIST_HEAD(&tk->rp.kp.list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000529 if (tk->rp.kp.symbol_name)
530 tk->rp.kp.addr = NULL;
531 }
532}
533
David Brazdil0f672f62019-12-10 10:32:29 +0000534/* Unregister a trace_probe and probe_event */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000535static int unregister_trace_kprobe(struct trace_kprobe *tk)
536{
David Brazdil0f672f62019-12-10 10:32:29 +0000537 /* If other probes are on the event, just unregister kprobe */
538 if (trace_probe_has_sibling(&tk->tp))
539 goto unreg;
540
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000541 /* Enabled event can not be unregistered */
542 if (trace_probe_is_enabled(&tk->tp))
543 return -EBUSY;
544
545 /* Will fail if probe is being used by ftrace or perf */
546 if (unregister_kprobe_event(tk))
547 return -EBUSY;
548
David Brazdil0f672f62019-12-10 10:32:29 +0000549unreg:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000550 __unregister_trace_kprobe(tk);
David Brazdil0f672f62019-12-10 10:32:29 +0000551 dyn_event_remove(&tk->devent);
552 trace_probe_unlink(&tk->tp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000553
554 return 0;
555}
556
David Brazdil0f672f62019-12-10 10:32:29 +0000557static bool trace_kprobe_has_same_kprobe(struct trace_kprobe *orig,
558 struct trace_kprobe *comp)
559{
560 struct trace_probe_event *tpe = orig->tp.event;
561 struct trace_probe *pos;
562 int i;
563
564 list_for_each_entry(pos, &tpe->probes, list) {
565 orig = container_of(pos, struct trace_kprobe, tp);
566 if (strcmp(trace_kprobe_symbol(orig),
567 trace_kprobe_symbol(comp)) ||
568 trace_kprobe_offset(orig) != trace_kprobe_offset(comp))
569 continue;
570
571 /*
572 * trace_probe_compare_arg_type() ensured that nr_args and
573 * each argument name and type are same. Let's compare comm.
574 */
575 for (i = 0; i < orig->tp.nr_args; i++) {
576 if (strcmp(orig->tp.args[i].comm,
577 comp->tp.args[i].comm))
578 break;
579 }
580
581 if (i == orig->tp.nr_args)
582 return true;
583 }
584
585 return false;
586}
587
588static int append_trace_kprobe(struct trace_kprobe *tk, struct trace_kprobe *to)
589{
590 int ret;
591
592 ret = trace_probe_compare_arg_type(&tk->tp, &to->tp);
593 if (ret) {
594 /* Note that argument starts index = 2 */
595 trace_probe_log_set_index(ret + 1);
596 trace_probe_log_err(0, DIFF_ARG_TYPE);
597 return -EEXIST;
598 }
599 if (trace_kprobe_has_same_kprobe(to, tk)) {
600 trace_probe_log_set_index(0);
601 trace_probe_log_err(0, SAME_PROBE);
602 return -EEXIST;
603 }
604
605 /* Append to existing event */
606 ret = trace_probe_append(&tk->tp, &to->tp);
607 if (ret)
608 return ret;
609
610 /* Register k*probe */
611 ret = __register_trace_kprobe(tk);
612 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
613 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
614 ret = 0;
615 }
616
617 if (ret)
618 trace_probe_unlink(&tk->tp);
619 else
620 dyn_event_add(&tk->devent);
621
622 return ret;
623}
624
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000625/* Register a trace_probe and probe_event */
626static int register_trace_kprobe(struct trace_kprobe *tk)
627{
628 struct trace_kprobe *old_tk;
629 int ret;
630
David Brazdil0f672f62019-12-10 10:32:29 +0000631 mutex_lock(&event_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000632
David Brazdil0f672f62019-12-10 10:32:29 +0000633 old_tk = find_trace_kprobe(trace_probe_name(&tk->tp),
634 trace_probe_group_name(&tk->tp));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000635 if (old_tk) {
David Brazdil0f672f62019-12-10 10:32:29 +0000636 if (trace_kprobe_is_return(tk) != trace_kprobe_is_return(old_tk)) {
637 trace_probe_log_set_index(0);
638 trace_probe_log_err(0, DIFF_PROBE_TYPE);
639 ret = -EEXIST;
640 } else {
641 ret = append_trace_kprobe(tk, old_tk);
642 }
643 goto end;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000644 }
645
646 /* Register new event */
647 ret = register_kprobe_event(tk);
648 if (ret) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200649 if (ret == -EEXIST) {
650 trace_probe_log_set_index(0);
651 trace_probe_log_err(0, EVENT_EXIST);
652 } else
653 pr_warn("Failed to register probe event(%d)\n", ret);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000654 goto end;
655 }
656
657 /* Register k*probe */
658 ret = __register_trace_kprobe(tk);
659 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
660 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
661 ret = 0;
662 }
663
664 if (ret < 0)
665 unregister_kprobe_event(tk);
666 else
David Brazdil0f672f62019-12-10 10:32:29 +0000667 dyn_event_add(&tk->devent);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000668
669end:
David Brazdil0f672f62019-12-10 10:32:29 +0000670 mutex_unlock(&event_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000671 return ret;
672}
673
674/* Module notifier call back, checking event on the module */
675static int trace_kprobe_module_callback(struct notifier_block *nb,
676 unsigned long val, void *data)
677{
678 struct module *mod = data;
David Brazdil0f672f62019-12-10 10:32:29 +0000679 struct dyn_event *pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000680 struct trace_kprobe *tk;
681 int ret;
682
683 if (val != MODULE_STATE_COMING)
684 return NOTIFY_DONE;
685
686 /* Update probes on coming module */
David Brazdil0f672f62019-12-10 10:32:29 +0000687 mutex_lock(&event_mutex);
688 for_each_trace_kprobe(tk, pos) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000689 if (trace_kprobe_within_module(tk, mod)) {
690 /* Don't need to check busy - this should have gone. */
691 __unregister_trace_kprobe(tk);
692 ret = __register_trace_kprobe(tk);
693 if (ret)
694 pr_warn("Failed to re-register probe %s on %s: %d\n",
David Brazdil0f672f62019-12-10 10:32:29 +0000695 trace_probe_name(&tk->tp),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000696 mod->name, ret);
697 }
698 }
David Brazdil0f672f62019-12-10 10:32:29 +0000699 mutex_unlock(&event_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000700
701 return NOTIFY_DONE;
702}
703
704static struct notifier_block trace_kprobe_module_nb = {
705 .notifier_call = trace_kprobe_module_callback,
706 .priority = 1 /* Invoked after kprobe module callback */
707};
708
709/* Convert certain expected symbols into '_' when generating event names */
710static inline void sanitize_event_name(char *name)
711{
712 while (*name++ != '\0')
713 if (*name == ':' || *name == '.')
714 *name = '_';
715}
716
David Brazdil0f672f62019-12-10 10:32:29 +0000717static int trace_kprobe_create(int argc, const char *argv[])
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000718{
719 /*
720 * Argument syntax:
721 * - Add kprobe:
722 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
723 * - Add kretprobe:
724 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
725 * Fetch args:
726 * $retval : fetch return value
727 * $stack : fetch stack address
728 * $stackN : fetch Nth of stack (N:0-)
729 * $comm : fetch current task comm
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
733 * Dereferencing memory fetch:
734 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
735 * Alias name of args:
736 * NAME=FETCHARG : set NAME as alias of FETCHARG.
737 * Type of args:
738 * FETCHARG:TYPE : use TYPE instead of unsigned long.
739 */
David Brazdil0f672f62019-12-10 10:32:29 +0000740 struct trace_kprobe *tk = NULL;
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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000745 int maxactive = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000746 long offset = 0;
747 void *addr = NULL;
748 char buf[MAX_EVENT_NAME_LEN];
David Brazdil0f672f62019-12-10 10:32:29 +0000749 unsigned int flags = TPARG_FL_KERNEL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000750
David Brazdil0f672f62019-12-10 10:32:29 +0000751 switch (argv[0][0]) {
752 case 'r':
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000753 is_return = true;
David Brazdil0f672f62019-12-10 10:32:29 +0000754 flags |= TPARG_FL_RETURN;
755 break;
756 case 'p':
757 break;
758 default:
759 return -ECANCELED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000760 }
David Brazdil0f672f62019-12-10 10:32:29 +0000761 if (argc < 2)
762 return -ECANCELED;
763
764 trace_probe_log_init("trace_kprobe", argc, argv);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000765
766 event = strchr(&argv[0][1], ':');
David Brazdil0f672f62019-12-10 10:32:29 +0000767 if (event)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000768 event++;
David Brazdil0f672f62019-12-10 10:32:29 +0000769
770 if (isdigit(argv[0][1])) {
771 if (!is_return) {
772 trace_probe_log_err(1, MAXACT_NO_KPROBE);
773 goto parse_error;
774 }
775 if (event)
776 len = event - &argv[0][1] - 1;
777 else
778 len = strlen(&argv[0][1]);
779 if (len > MAX_EVENT_NAME_LEN - 1) {
780 trace_probe_log_err(1, BAD_MAXACT);
781 goto parse_error;
782 }
783 memcpy(buf, &argv[0][1], len);
784 buf[len] = '\0';
785 ret = kstrtouint(buf, 0, &maxactive);
786 if (ret || !maxactive) {
787 trace_probe_log_err(1, BAD_MAXACT);
788 goto parse_error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000789 }
790 /* kretprobes instances are iterated over via a list. The
791 * maximum should stay reasonable.
792 */
793 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
David Brazdil0f672f62019-12-10 10:32:29 +0000794 trace_probe_log_err(1, MAXACT_TOO_BIG);
795 goto parse_error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000796 }
797 }
798
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000799 /* try to parse an address. if that fails, try to read the
800 * input as a symbol. */
801 if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000802 trace_probe_log_set_index(1);
803 /* Check whether uprobe event specified */
804 if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
805 ret = -ECANCELED;
806 goto error;
807 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000808 /* a symbol specified */
David Brazdil0f672f62019-12-10 10:32:29 +0000809 symbol = kstrdup(argv[1], GFP_KERNEL);
810 if (!symbol)
811 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000812 /* TODO: support .init module functions */
813 ret = traceprobe_split_symbol_offset(symbol, &offset);
814 if (ret || offset < 0 || offset > UINT_MAX) {
David Brazdil0f672f62019-12-10 10:32:29 +0000815 trace_probe_log_err(0, BAD_PROBE_ADDR);
816 goto parse_error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000817 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200818 ret = kprobe_on_func_entry(NULL, symbol, offset);
819 if (ret == 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000820 flags |= TPARG_FL_FENTRY;
Olivier Deprez0e641232021-09-23 10:07:05 +0200821 /* Defer the ENOENT case until register kprobe */
822 if (ret == -EINVAL && is_return) {
David Brazdil0f672f62019-12-10 10:32:29 +0000823 trace_probe_log_err(0, BAD_RETPROBE);
824 goto parse_error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000825 }
826 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000827
David Brazdil0f672f62019-12-10 10:32:29 +0000828 trace_probe_log_set_index(0);
829 if (event) {
830 ret = traceprobe_parse_event_name(&event, &group, buf,
831 event - argv[0]);
832 if (ret)
833 goto parse_error;
834 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000835 /* Make a new event name */
836 if (symbol)
837 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
838 is_return ? 'r' : 'p', symbol, offset);
839 else
840 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
841 is_return ? 'r' : 'p', addr);
842 sanitize_event_name(buf);
843 event = buf;
844 }
David Brazdil0f672f62019-12-10 10:32:29 +0000845
846 /* setup a probe */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000847 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
David Brazdil0f672f62019-12-10 10:32:29 +0000848 argc - 2, is_return);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000849 if (IS_ERR(tk)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000850 ret = PTR_ERR(tk);
851 /* This must return -ENOMEM, else there is a bug */
852 WARN_ON_ONCE(ret != -ENOMEM);
853 goto out; /* We know tk is not allocated */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000854 }
David Brazdil0f672f62019-12-10 10:32:29 +0000855 argc -= 2; argv += 2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000856
857 /* parse arguments */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000858 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
David Brazdil0f672f62019-12-10 10:32:29 +0000859 tmp = kstrdup(argv[i], GFP_KERNEL);
860 if (!tmp) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000861 ret = -ENOMEM;
862 goto error;
863 }
864
David Brazdil0f672f62019-12-10 10:32:29 +0000865 trace_probe_log_set_index(i + 2);
866 ret = traceprobe_parse_probe_arg(&tk->tp, i, tmp, flags);
867 kfree(tmp);
868 if (ret)
869 goto error; /* This can be -ENOMEM */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000870 }
871
David Brazdil0f672f62019-12-10 10:32:29 +0000872 ret = traceprobe_set_print_fmt(&tk->tp, is_return);
873 if (ret < 0)
874 goto error;
875
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000876 ret = register_trace_kprobe(tk);
David Brazdil0f672f62019-12-10 10:32:29 +0000877 if (ret) {
878 trace_probe_log_set_index(1);
879 if (ret == -EILSEQ)
880 trace_probe_log_err(0, BAD_INSN_BNDRY);
881 else if (ret == -ENOENT)
882 trace_probe_log_err(0, BAD_PROBE_ADDR);
883 else if (ret != -ENOMEM && ret != -EEXIST)
884 trace_probe_log_err(0, FAIL_REG_PROBE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000885 goto error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000886 }
887
David Brazdil0f672f62019-12-10 10:32:29 +0000888out:
889 trace_probe_log_clear();
890 kfree(symbol);
891 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000892
David Brazdil0f672f62019-12-10 10:32:29 +0000893parse_error:
894 ret = -EINVAL;
895error:
896 free_trace_kprobe(tk);
897 goto out;
898}
899
900static int create_or_delete_trace_kprobe(int argc, char **argv)
901{
902 int ret;
903
904 if (argv[0][0] == '-')
905 return dyn_event_release(argc, argv, &trace_kprobe_ops);
906
907 ret = trace_kprobe_create(argc, (const char **)argv);
908 return ret == -ECANCELED ? -EINVAL : ret;
909}
910
911static int trace_kprobe_release(struct dyn_event *ev)
912{
913 struct trace_kprobe *tk = to_trace_kprobe(ev);
914 int ret = unregister_trace_kprobe(tk);
915
916 if (!ret)
917 free_trace_kprobe(tk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000918 return ret;
919}
920
David Brazdil0f672f62019-12-10 10:32:29 +0000921static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000922{
David Brazdil0f672f62019-12-10 10:32:29 +0000923 struct trace_kprobe *tk = to_trace_kprobe(ev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000924 int i;
925
926 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
Olivier Deprez0e641232021-09-23 10:07:05 +0200927 if (trace_kprobe_is_return(tk) && tk->rp.maxactive)
928 seq_printf(m, "%d", tk->rp.maxactive);
David Brazdil0f672f62019-12-10 10:32:29 +0000929 seq_printf(m, ":%s/%s", trace_probe_group_name(&tk->tp),
930 trace_probe_name(&tk->tp));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000931
932 if (!tk->symbol)
933 seq_printf(m, " 0x%p", tk->rp.kp.addr);
934 else if (tk->rp.kp.offset)
935 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
936 tk->rp.kp.offset);
937 else
938 seq_printf(m, " %s", trace_kprobe_symbol(tk));
939
940 for (i = 0; i < tk->tp.nr_args; i++)
941 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
942 seq_putc(m, '\n');
943
944 return 0;
945}
946
David Brazdil0f672f62019-12-10 10:32:29 +0000947static int probes_seq_show(struct seq_file *m, void *v)
948{
949 struct dyn_event *ev = v;
950
951 if (!is_trace_kprobe(ev))
952 return 0;
953
954 return trace_kprobe_show(m, ev);
955}
956
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000957static const struct seq_operations probes_seq_op = {
David Brazdil0f672f62019-12-10 10:32:29 +0000958 .start = dyn_event_seq_start,
959 .next = dyn_event_seq_next,
960 .stop = dyn_event_seq_stop,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000961 .show = probes_seq_show
962};
963
964static int probes_open(struct inode *inode, struct file *file)
965{
966 int ret;
967
David Brazdil0f672f62019-12-10 10:32:29 +0000968 ret = security_locked_down(LOCKDOWN_TRACEFS);
969 if (ret)
970 return ret;
971
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000972 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000973 ret = dyn_events_release_all(&trace_kprobe_ops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000974 if (ret < 0)
975 return ret;
976 }
977
978 return seq_open(file, &probes_seq_op);
979}
980
981static ssize_t probes_write(struct file *file, const char __user *buffer,
982 size_t count, loff_t *ppos)
983{
984 return trace_parse_run_command(file, buffer, count, ppos,
David Brazdil0f672f62019-12-10 10:32:29 +0000985 create_or_delete_trace_kprobe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000986}
987
988static const struct file_operations kprobe_events_ops = {
989 .owner = THIS_MODULE,
990 .open = probes_open,
991 .read = seq_read,
992 .llseek = seq_lseek,
993 .release = seq_release,
994 .write = probes_write,
995};
996
997/* Probes profiling interfaces */
998static int probes_profile_seq_show(struct seq_file *m, void *v)
999{
David Brazdil0f672f62019-12-10 10:32:29 +00001000 struct dyn_event *ev = v;
1001 struct trace_kprobe *tk;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001002
David Brazdil0f672f62019-12-10 10:32:29 +00001003 if (!is_trace_kprobe(ev))
1004 return 0;
1005
1006 tk = to_trace_kprobe(ev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001007 seq_printf(m, " %-44s %15lu %15lu\n",
David Brazdil0f672f62019-12-10 10:32:29 +00001008 trace_probe_name(&tk->tp),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001009 trace_kprobe_nhit(tk),
1010 tk->rp.kp.nmissed);
1011
1012 return 0;
1013}
1014
1015static const struct seq_operations profile_seq_op = {
David Brazdil0f672f62019-12-10 10:32:29 +00001016 .start = dyn_event_seq_start,
1017 .next = dyn_event_seq_next,
1018 .stop = dyn_event_seq_stop,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001019 .show = probes_profile_seq_show
1020};
1021
1022static int profile_open(struct inode *inode, struct file *file)
1023{
David Brazdil0f672f62019-12-10 10:32:29 +00001024 int ret;
1025
1026 ret = security_locked_down(LOCKDOWN_TRACEFS);
1027 if (ret)
1028 return ret;
1029
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001030 return seq_open(file, &profile_seq_op);
1031}
1032
1033static const struct file_operations kprobe_profile_ops = {
1034 .owner = THIS_MODULE,
1035 .open = profile_open,
1036 .read = seq_read,
1037 .llseek = seq_lseek,
1038 .release = seq_release,
1039};
1040
David Brazdil0f672f62019-12-10 10:32:29 +00001041/* Kprobe specific fetch functions */
1042
1043/* Return the length of string -- including null terminal byte */
1044static nokprobe_inline int
1045fetch_store_strlen(unsigned long addr)
1046{
1047 int ret, len = 0;
1048 u8 c;
1049
1050 do {
1051 ret = probe_kernel_read(&c, (u8 *)addr + len, 1);
1052 len++;
1053 } while (c && ret == 0 && len < MAX_STRING_SIZE);
1054
1055 return (ret < 0) ? ret : len;
1056}
1057
1058/* Return the length of string -- including null terminal byte */
1059static nokprobe_inline int
1060fetch_store_strlen_user(unsigned long addr)
1061{
1062 const void __user *uaddr = (__force const void __user *)addr;
1063
1064 return strnlen_unsafe_user(uaddr, MAX_STRING_SIZE);
1065}
1066
1067/*
1068 * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
1069 * length and relative data location.
1070 */
1071static nokprobe_inline int
1072fetch_store_string(unsigned long addr, void *dest, void *base)
1073{
1074 int maxlen = get_loc_len(*(u32 *)dest);
1075 void *__dest;
1076 long ret;
1077
1078 if (unlikely(!maxlen))
1079 return -ENOMEM;
1080
1081 __dest = get_loc_data(dest, base);
1082
1083 /*
1084 * Try to get string again, since the string can be changed while
1085 * probing.
1086 */
1087 ret = strncpy_from_unsafe(__dest, (void *)addr, maxlen);
1088 if (ret >= 0)
1089 *(u32 *)dest = make_data_loc(ret, __dest - base);
1090
1091 return ret;
1092}
1093
1094/*
1095 * Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf
1096 * with max length and relative data location.
1097 */
1098static nokprobe_inline int
1099fetch_store_string_user(unsigned long addr, void *dest, void *base)
1100{
1101 const void __user *uaddr = (__force const void __user *)addr;
1102 int maxlen = get_loc_len(*(u32 *)dest);
1103 void *__dest;
1104 long ret;
1105
1106 if (unlikely(!maxlen))
1107 return -ENOMEM;
1108
1109 __dest = get_loc_data(dest, base);
1110
1111 ret = strncpy_from_unsafe_user(__dest, uaddr, maxlen);
1112 if (ret >= 0)
1113 *(u32 *)dest = make_data_loc(ret, __dest - base);
1114
1115 return ret;
1116}
1117
1118static nokprobe_inline int
1119probe_mem_read(void *dest, void *src, size_t size)
1120{
1121 return probe_kernel_read(dest, src, size);
1122}
1123
1124static nokprobe_inline int
1125probe_mem_read_user(void *dest, void *src, size_t size)
1126{
1127 const void __user *uaddr = (__force const void __user *)src;
1128
1129 return probe_user_read(dest, uaddr, size);
1130}
1131
1132/* Note that we don't verify it, since the code does not come from user space */
1133static int
1134process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
1135 void *base)
1136{
1137 unsigned long val;
1138
1139retry:
1140 /* 1st stage: get value from context */
1141 switch (code->op) {
1142 case FETCH_OP_REG:
1143 val = regs_get_register(regs, code->param);
1144 break;
1145 case FETCH_OP_STACK:
1146 val = regs_get_kernel_stack_nth(regs, code->param);
1147 break;
1148 case FETCH_OP_STACKP:
1149 val = kernel_stack_pointer(regs);
1150 break;
1151 case FETCH_OP_RETVAL:
1152 val = regs_return_value(regs);
1153 break;
1154 case FETCH_OP_IMM:
1155 val = code->immediate;
1156 break;
1157 case FETCH_OP_COMM:
1158 val = (unsigned long)current->comm;
1159 break;
1160 case FETCH_OP_DATA:
1161 val = (unsigned long)code->data;
1162 break;
1163#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
1164 case FETCH_OP_ARG:
1165 val = regs_get_kernel_argument(regs, code->param);
1166 break;
1167#endif
1168 case FETCH_NOP_SYMBOL: /* Ignore a place holder */
1169 code++;
1170 goto retry;
1171 default:
1172 return -EILSEQ;
1173 }
1174 code++;
1175
1176 return process_fetch_insn_bottom(code, val, dest, base);
1177}
1178NOKPROBE_SYMBOL(process_fetch_insn)
1179
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001180/* Kprobe handler */
1181static nokprobe_inline void
1182__kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
1183 struct trace_event_file *trace_file)
1184{
1185 struct kprobe_trace_entry_head *entry;
1186 struct ring_buffer_event *event;
1187 struct ring_buffer *buffer;
1188 int size, dsize, pc;
1189 unsigned long irq_flags;
David Brazdil0f672f62019-12-10 10:32:29 +00001190 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001191
1192 WARN_ON(call != trace_file->event_call);
1193
1194 if (trace_trigger_soft_disabled(trace_file))
1195 return;
1196
1197 local_save_flags(irq_flags);
1198 pc = preempt_count();
1199
1200 dsize = __get_data_size(&tk->tp, regs);
1201 size = sizeof(*entry) + tk->tp.size + dsize;
1202
1203 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1204 call->event.type,
1205 size, irq_flags, pc);
1206 if (!event)
1207 return;
1208
1209 entry = ring_buffer_event_data(event);
1210 entry->ip = (unsigned long)tk->rp.kp.addr;
David Brazdil0f672f62019-12-10 10:32:29 +00001211 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001212
1213 event_trigger_unlock_commit_regs(trace_file, buffer, event,
1214 entry, irq_flags, pc, regs);
1215}
1216
1217static void
1218kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
1219{
1220 struct event_file_link *link;
1221
David Brazdil0f672f62019-12-10 10:32:29 +00001222 trace_probe_for_each_link_rcu(link, &tk->tp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001223 __kprobe_trace_func(tk, regs, link->file);
1224}
1225NOKPROBE_SYMBOL(kprobe_trace_func);
1226
1227/* Kretprobe handler */
1228static nokprobe_inline void
1229__kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1230 struct pt_regs *regs,
1231 struct trace_event_file *trace_file)
1232{
1233 struct kretprobe_trace_entry_head *entry;
1234 struct ring_buffer_event *event;
1235 struct ring_buffer *buffer;
1236 int size, pc, dsize;
1237 unsigned long irq_flags;
David Brazdil0f672f62019-12-10 10:32:29 +00001238 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001239
1240 WARN_ON(call != trace_file->event_call);
1241
1242 if (trace_trigger_soft_disabled(trace_file))
1243 return;
1244
1245 local_save_flags(irq_flags);
1246 pc = preempt_count();
1247
1248 dsize = __get_data_size(&tk->tp, regs);
1249 size = sizeof(*entry) + tk->tp.size + dsize;
1250
1251 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1252 call->event.type,
1253 size, irq_flags, pc);
1254 if (!event)
1255 return;
1256
1257 entry = ring_buffer_event_data(event);
1258 entry->func = (unsigned long)tk->rp.kp.addr;
1259 entry->ret_ip = (unsigned long)ri->ret_addr;
David Brazdil0f672f62019-12-10 10:32:29 +00001260 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001261
1262 event_trigger_unlock_commit_regs(trace_file, buffer, event,
1263 entry, irq_flags, pc, regs);
1264}
1265
1266static void
1267kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1268 struct pt_regs *regs)
1269{
1270 struct event_file_link *link;
1271
David Brazdil0f672f62019-12-10 10:32:29 +00001272 trace_probe_for_each_link_rcu(link, &tk->tp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001273 __kretprobe_trace_func(tk, ri, regs, link->file);
1274}
1275NOKPROBE_SYMBOL(kretprobe_trace_func);
1276
1277/* Event entry printers */
1278static enum print_line_t
1279print_kprobe_event(struct trace_iterator *iter, int flags,
1280 struct trace_event *event)
1281{
1282 struct kprobe_trace_entry_head *field;
1283 struct trace_seq *s = &iter->seq;
1284 struct trace_probe *tp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001285
1286 field = (struct kprobe_trace_entry_head *)iter->ent;
David Brazdil0f672f62019-12-10 10:32:29 +00001287 tp = trace_probe_primary_from_call(
1288 container_of(event, struct trace_event_call, event));
1289 if (WARN_ON_ONCE(!tp))
1290 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001291
David Brazdil0f672f62019-12-10 10:32:29 +00001292 trace_seq_printf(s, "%s: (", trace_probe_name(tp));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001293
1294 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1295 goto out;
1296
1297 trace_seq_putc(s, ')');
1298
David Brazdil0f672f62019-12-10 10:32:29 +00001299 if (print_probe_args(s, tp->args, tp->nr_args,
1300 (u8 *)&field[1], field) < 0)
1301 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001302
1303 trace_seq_putc(s, '\n');
1304 out:
1305 return trace_handle_return(s);
1306}
1307
1308static enum print_line_t
1309print_kretprobe_event(struct trace_iterator *iter, int flags,
1310 struct trace_event *event)
1311{
1312 struct kretprobe_trace_entry_head *field;
1313 struct trace_seq *s = &iter->seq;
1314 struct trace_probe *tp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001315
1316 field = (struct kretprobe_trace_entry_head *)iter->ent;
David Brazdil0f672f62019-12-10 10:32:29 +00001317 tp = trace_probe_primary_from_call(
1318 container_of(event, struct trace_event_call, event));
1319 if (WARN_ON_ONCE(!tp))
1320 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001321
David Brazdil0f672f62019-12-10 10:32:29 +00001322 trace_seq_printf(s, "%s: (", trace_probe_name(tp));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001323
1324 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1325 goto out;
1326
1327 trace_seq_puts(s, " <- ");
1328
1329 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1330 goto out;
1331
1332 trace_seq_putc(s, ')');
1333
David Brazdil0f672f62019-12-10 10:32:29 +00001334 if (print_probe_args(s, tp->args, tp->nr_args,
1335 (u8 *)&field[1], field) < 0)
1336 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001337
1338 trace_seq_putc(s, '\n');
1339
1340 out:
1341 return trace_handle_return(s);
1342}
1343
1344
1345static int kprobe_event_define_fields(struct trace_event_call *event_call)
1346{
David Brazdil0f672f62019-12-10 10:32:29 +00001347 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001348 struct kprobe_trace_entry_head field;
David Brazdil0f672f62019-12-10 10:32:29 +00001349 struct trace_probe *tp;
1350
1351 tp = trace_probe_primary_from_call(event_call);
1352 if (WARN_ON_ONCE(!tp))
1353 return -ENOENT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001354
1355 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001356
David Brazdil0f672f62019-12-10 10:32:29 +00001357 return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001358}
1359
1360static int kretprobe_event_define_fields(struct trace_event_call *event_call)
1361{
David Brazdil0f672f62019-12-10 10:32:29 +00001362 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001363 struct kretprobe_trace_entry_head field;
David Brazdil0f672f62019-12-10 10:32:29 +00001364 struct trace_probe *tp;
1365
1366 tp = trace_probe_primary_from_call(event_call);
1367 if (WARN_ON_ONCE(!tp))
1368 return -ENOENT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001369
1370 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1371 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001372
David Brazdil0f672f62019-12-10 10:32:29 +00001373 return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001374}
1375
1376#ifdef CONFIG_PERF_EVENTS
1377
1378/* Kprobe profile handler */
1379static int
1380kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
1381{
David Brazdil0f672f62019-12-10 10:32:29 +00001382 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001383 struct kprobe_trace_entry_head *entry;
1384 struct hlist_head *head;
1385 int size, __size, dsize;
1386 int rctx;
1387
1388 if (bpf_prog_array_valid(call)) {
1389 unsigned long orig_ip = instruction_pointer(regs);
1390 int ret;
1391
1392 ret = trace_call_bpf(call, regs);
1393
1394 /*
1395 * We need to check and see if we modified the pc of the
1396 * pt_regs, and if so return 1 so that we don't do the
1397 * single stepping.
1398 */
1399 if (orig_ip != instruction_pointer(regs))
1400 return 1;
1401 if (!ret)
1402 return 0;
1403 }
1404
1405 head = this_cpu_ptr(call->perf_events);
1406 if (hlist_empty(head))
1407 return 0;
1408
1409 dsize = __get_data_size(&tk->tp, regs);
1410 __size = sizeof(*entry) + tk->tp.size + dsize;
1411 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1412 size -= sizeof(u32);
1413
1414 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1415 if (!entry)
1416 return 0;
1417
1418 entry->ip = (unsigned long)tk->rp.kp.addr;
1419 memset(&entry[1], 0, dsize);
David Brazdil0f672f62019-12-10 10:32:29 +00001420 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001421 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1422 head, NULL);
1423 return 0;
1424}
1425NOKPROBE_SYMBOL(kprobe_perf_func);
1426
1427/* Kretprobe profile handler */
1428static void
1429kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1430 struct pt_regs *regs)
1431{
David Brazdil0f672f62019-12-10 10:32:29 +00001432 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001433 struct kretprobe_trace_entry_head *entry;
1434 struct hlist_head *head;
1435 int size, __size, dsize;
1436 int rctx;
1437
1438 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1439 return;
1440
1441 head = this_cpu_ptr(call->perf_events);
1442 if (hlist_empty(head))
1443 return;
1444
1445 dsize = __get_data_size(&tk->tp, regs);
1446 __size = sizeof(*entry) + tk->tp.size + dsize;
1447 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1448 size -= sizeof(u32);
1449
1450 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1451 if (!entry)
1452 return;
1453
1454 entry->func = (unsigned long)tk->rp.kp.addr;
1455 entry->ret_ip = (unsigned long)ri->ret_addr;
David Brazdil0f672f62019-12-10 10:32:29 +00001456 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001457 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1458 head, NULL);
1459}
1460NOKPROBE_SYMBOL(kretprobe_perf_func);
1461
1462int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1463 const char **symbol, u64 *probe_offset,
1464 u64 *probe_addr, bool perf_type_tracepoint)
1465{
1466 const char *pevent = trace_event_name(event->tp_event);
1467 const char *group = event->tp_event->class->system;
1468 struct trace_kprobe *tk;
1469
1470 if (perf_type_tracepoint)
1471 tk = find_trace_kprobe(pevent, group);
1472 else
Olivier Deprez0e641232021-09-23 10:07:05 +02001473 tk = trace_kprobe_primary_from_call(event->tp_event);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001474 if (!tk)
1475 return -EINVAL;
1476
1477 *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1478 : BPF_FD_TYPE_KPROBE;
1479 if (tk->symbol) {
1480 *symbol = tk->symbol;
1481 *probe_offset = tk->rp.kp.offset;
1482 *probe_addr = 0;
1483 } else {
1484 *symbol = NULL;
1485 *probe_offset = 0;
1486 *probe_addr = (unsigned long)tk->rp.kp.addr;
1487 }
1488 return 0;
1489}
1490#endif /* CONFIG_PERF_EVENTS */
1491
1492/*
1493 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1494 *
1495 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1496 * lockless, but we can't race with this __init function.
1497 */
1498static int kprobe_register(struct trace_event_call *event,
1499 enum trace_reg type, void *data)
1500{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001501 struct trace_event_file *file = data;
1502
1503 switch (type) {
1504 case TRACE_REG_REGISTER:
David Brazdil0f672f62019-12-10 10:32:29 +00001505 return enable_trace_kprobe(event, file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001506 case TRACE_REG_UNREGISTER:
David Brazdil0f672f62019-12-10 10:32:29 +00001507 return disable_trace_kprobe(event, file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001508
1509#ifdef CONFIG_PERF_EVENTS
1510 case TRACE_REG_PERF_REGISTER:
David Brazdil0f672f62019-12-10 10:32:29 +00001511 return enable_trace_kprobe(event, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001512 case TRACE_REG_PERF_UNREGISTER:
David Brazdil0f672f62019-12-10 10:32:29 +00001513 return disable_trace_kprobe(event, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001514 case TRACE_REG_PERF_OPEN:
1515 case TRACE_REG_PERF_CLOSE:
1516 case TRACE_REG_PERF_ADD:
1517 case TRACE_REG_PERF_DEL:
1518 return 0;
1519#endif
1520 }
1521 return 0;
1522}
1523
1524static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1525{
1526 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
1527 int ret = 0;
1528
1529 raw_cpu_inc(*tk->nhit);
1530
David Brazdil0f672f62019-12-10 10:32:29 +00001531 if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001532 kprobe_trace_func(tk, regs);
1533#ifdef CONFIG_PERF_EVENTS
David Brazdil0f672f62019-12-10 10:32:29 +00001534 if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001535 ret = kprobe_perf_func(tk, regs);
1536#endif
1537 return ret;
1538}
1539NOKPROBE_SYMBOL(kprobe_dispatcher);
1540
1541static int
1542kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1543{
1544 struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
1545
1546 raw_cpu_inc(*tk->nhit);
1547
David Brazdil0f672f62019-12-10 10:32:29 +00001548 if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001549 kretprobe_trace_func(tk, ri, regs);
1550#ifdef CONFIG_PERF_EVENTS
David Brazdil0f672f62019-12-10 10:32:29 +00001551 if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001552 kretprobe_perf_func(tk, ri, regs);
1553#endif
1554 return 0; /* We don't tweek kernel, so just return 0 */
1555}
1556NOKPROBE_SYMBOL(kretprobe_dispatcher);
1557
1558static struct trace_event_functions kretprobe_funcs = {
1559 .trace = print_kretprobe_event
1560};
1561
1562static struct trace_event_functions kprobe_funcs = {
1563 .trace = print_kprobe_event
1564};
1565
David Brazdil0f672f62019-12-10 10:32:29 +00001566static inline void init_trace_event_call(struct trace_kprobe *tk)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001567{
David Brazdil0f672f62019-12-10 10:32:29 +00001568 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1569
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001570 if (trace_kprobe_is_return(tk)) {
1571 call->event.funcs = &kretprobe_funcs;
1572 call->class->define_fields = kretprobe_event_define_fields;
1573 } else {
1574 call->event.funcs = &kprobe_funcs;
1575 call->class->define_fields = kprobe_event_define_fields;
1576 }
1577
1578 call->flags = TRACE_EVENT_FL_KPROBE;
1579 call->class->reg = kprobe_register;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001580}
1581
1582static int register_kprobe_event(struct trace_kprobe *tk)
1583{
David Brazdil0f672f62019-12-10 10:32:29 +00001584 init_trace_event_call(tk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001585
David Brazdil0f672f62019-12-10 10:32:29 +00001586 return trace_probe_register_event_call(&tk->tp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001587}
1588
1589static int unregister_kprobe_event(struct trace_kprobe *tk)
1590{
David Brazdil0f672f62019-12-10 10:32:29 +00001591 return trace_probe_unregister_event_call(&tk->tp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001592}
1593
1594#ifdef CONFIG_PERF_EVENTS
1595/* create a trace_kprobe, but don't add it to global lists */
1596struct trace_event_call *
1597create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1598 bool is_return)
1599{
1600 struct trace_kprobe *tk;
1601 int ret;
1602 char *event;
1603
1604 /*
David Brazdil0f672f62019-12-10 10:32:29 +00001605 * local trace_kprobes are not added to dyn_event, so they are never
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001606 * searched in find_trace_kprobe(). Therefore, there is no concern of
1607 * duplicated name here.
1608 */
1609 event = func ? func : "DUMMY_EVENT";
1610
1611 tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1612 offs, 0 /* maxactive */, 0 /* nargs */,
1613 is_return);
1614
1615 if (IS_ERR(tk)) {
1616 pr_info("Failed to allocate trace_probe.(%d)\n",
1617 (int)PTR_ERR(tk));
1618 return ERR_CAST(tk);
1619 }
1620
David Brazdil0f672f62019-12-10 10:32:29 +00001621 init_trace_event_call(tk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001622
David Brazdil0f672f62019-12-10 10:32:29 +00001623 if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001624 ret = -ENOMEM;
1625 goto error;
1626 }
1627
1628 ret = __register_trace_kprobe(tk);
David Brazdil0f672f62019-12-10 10:32:29 +00001629 if (ret < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001630 goto error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001631
David Brazdil0f672f62019-12-10 10:32:29 +00001632 return trace_probe_event_call(&tk->tp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001633error:
1634 free_trace_kprobe(tk);
1635 return ERR_PTR(ret);
1636}
1637
1638void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1639{
1640 struct trace_kprobe *tk;
1641
David Brazdil0f672f62019-12-10 10:32:29 +00001642 tk = trace_kprobe_primary_from_call(event_call);
1643 if (unlikely(!tk))
1644 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001645
1646 if (trace_probe_is_enabled(&tk->tp)) {
1647 WARN_ON(1);
1648 return;
1649 }
1650
1651 __unregister_trace_kprobe(tk);
1652
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001653 free_trace_kprobe(tk);
1654}
1655#endif /* CONFIG_PERF_EVENTS */
1656
David Brazdil0f672f62019-12-10 10:32:29 +00001657static __init void enable_boot_kprobe_events(void)
1658{
1659 struct trace_array *tr = top_trace_array();
1660 struct trace_event_file *file;
1661 struct trace_kprobe *tk;
1662 struct dyn_event *pos;
1663
1664 mutex_lock(&event_mutex);
1665 for_each_trace_kprobe(tk, pos) {
1666 list_for_each_entry(file, &tr->events, list)
1667 if (file->event_call == trace_probe_event_call(&tk->tp))
1668 trace_event_enable_disable(file, 1, 0);
1669 }
1670 mutex_unlock(&event_mutex);
1671}
1672
1673static __init void setup_boot_kprobe_events(void)
1674{
1675 char *p, *cmd = kprobe_boot_events_buf;
1676 int ret;
1677
1678 strreplace(kprobe_boot_events_buf, ',', ' ');
1679
1680 while (cmd && *cmd != '\0') {
1681 p = strchr(cmd, ';');
1682 if (p)
1683 *p++ = '\0';
1684
1685 ret = trace_run_command(cmd, create_or_delete_trace_kprobe);
1686 if (ret)
1687 pr_warn("Failed to add event(%d): %s\n", ret, cmd);
1688 else
1689 kprobe_boot_events_enabled = true;
1690
1691 cmd = p;
1692 }
1693
1694 enable_boot_kprobe_events();
1695}
1696
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001697/* Make a tracefs interface for controlling probe points */
1698static __init int init_kprobe_trace(void)
1699{
1700 struct dentry *d_tracer;
1701 struct dentry *entry;
David Brazdil0f672f62019-12-10 10:32:29 +00001702 int ret;
1703
1704 ret = dyn_event_register(&trace_kprobe_ops);
1705 if (ret)
1706 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001707
1708 if (register_module_notifier(&trace_kprobe_module_nb))
1709 return -EINVAL;
1710
1711 d_tracer = tracing_init_dentry();
1712 if (IS_ERR(d_tracer))
1713 return 0;
1714
1715 entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
1716 NULL, &kprobe_events_ops);
1717
1718 /* Event list interface */
1719 if (!entry)
1720 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
1721
1722 /* Profile interface */
1723 entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
1724 NULL, &kprobe_profile_ops);
1725
1726 if (!entry)
1727 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
David Brazdil0f672f62019-12-10 10:32:29 +00001728
1729 setup_boot_kprobe_events();
1730
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001731 return 0;
1732}
1733fs_initcall(init_kprobe_trace);
1734
1735
1736#ifdef CONFIG_FTRACE_STARTUP_TEST
1737static __init struct trace_event_file *
1738find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
1739{
1740 struct trace_event_file *file;
1741
1742 list_for_each_entry(file, &tr->events, list)
David Brazdil0f672f62019-12-10 10:32:29 +00001743 if (file->event_call == trace_probe_event_call(&tk->tp))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001744 return file;
1745
1746 return NULL;
1747}
1748
1749/*
1750 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
1751 * stage, we can do this lockless.
1752 */
1753static __init int kprobe_trace_self_tests_init(void)
1754{
1755 int ret, warn = 0;
1756 int (*target)(int, int, int, int, int, int);
1757 struct trace_kprobe *tk;
1758 struct trace_event_file *file;
1759
1760 if (tracing_is_disabled())
1761 return -ENODEV;
1762
David Brazdil0f672f62019-12-10 10:32:29 +00001763 if (kprobe_boot_events_enabled) {
1764 pr_info("Skipping kprobe tests due to kprobe_event on cmdline\n");
1765 return 0;
1766 }
1767
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001768 target = kprobe_trace_selftest_target;
1769
1770 pr_info("Testing kprobe tracing: ");
1771
David Brazdil0f672f62019-12-10 10:32:29 +00001772 ret = trace_run_command("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)",
1773 create_or_delete_trace_kprobe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001774 if (WARN_ON_ONCE(ret)) {
1775 pr_warn("error on probing function entry.\n");
1776 warn++;
1777 } else {
1778 /* Enable trace point */
1779 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1780 if (WARN_ON_ONCE(tk == NULL)) {
1781 pr_warn("error on getting new probe.\n");
1782 warn++;
1783 } else {
1784 file = find_trace_probe_file(tk, top_trace_array());
1785 if (WARN_ON_ONCE(file == NULL)) {
1786 pr_warn("error on getting probe file.\n");
1787 warn++;
1788 } else
David Brazdil0f672f62019-12-10 10:32:29 +00001789 enable_trace_kprobe(
1790 trace_probe_event_call(&tk->tp), file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001791 }
1792 }
1793
David Brazdil0f672f62019-12-10 10:32:29 +00001794 ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target $retval",
1795 create_or_delete_trace_kprobe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001796 if (WARN_ON_ONCE(ret)) {
1797 pr_warn("error on probing function return.\n");
1798 warn++;
1799 } else {
1800 /* Enable trace point */
1801 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1802 if (WARN_ON_ONCE(tk == NULL)) {
1803 pr_warn("error on getting 2nd new probe.\n");
1804 warn++;
1805 } else {
1806 file = find_trace_probe_file(tk, top_trace_array());
1807 if (WARN_ON_ONCE(file == NULL)) {
1808 pr_warn("error on getting probe file.\n");
1809 warn++;
1810 } else
David Brazdil0f672f62019-12-10 10:32:29 +00001811 enable_trace_kprobe(
1812 trace_probe_event_call(&tk->tp), file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001813 }
1814 }
1815
1816 if (warn)
1817 goto end;
1818
1819 ret = target(1, 2, 3, 4, 5, 6);
1820
1821 /*
1822 * Not expecting an error here, the check is only to prevent the
1823 * optimizer from removing the call to target() as otherwise there
1824 * are no side-effects and the call is never performed.
1825 */
1826 if (ret != 21)
1827 warn++;
1828
1829 /* Disable trace points before removing it */
1830 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1831 if (WARN_ON_ONCE(tk == NULL)) {
1832 pr_warn("error on getting test probe.\n");
1833 warn++;
1834 } else {
1835 if (trace_kprobe_nhit(tk) != 1) {
1836 pr_warn("incorrect number of testprobe hits\n");
1837 warn++;
1838 }
1839
1840 file = find_trace_probe_file(tk, top_trace_array());
1841 if (WARN_ON_ONCE(file == NULL)) {
1842 pr_warn("error on getting probe file.\n");
1843 warn++;
1844 } else
David Brazdil0f672f62019-12-10 10:32:29 +00001845 disable_trace_kprobe(
1846 trace_probe_event_call(&tk->tp), file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001847 }
1848
1849 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1850 if (WARN_ON_ONCE(tk == NULL)) {
1851 pr_warn("error on getting 2nd test probe.\n");
1852 warn++;
1853 } else {
1854 if (trace_kprobe_nhit(tk) != 1) {
1855 pr_warn("incorrect number of testprobe2 hits\n");
1856 warn++;
1857 }
1858
1859 file = find_trace_probe_file(tk, top_trace_array());
1860 if (WARN_ON_ONCE(file == NULL)) {
1861 pr_warn("error on getting probe file.\n");
1862 warn++;
1863 } else
David Brazdil0f672f62019-12-10 10:32:29 +00001864 disable_trace_kprobe(
1865 trace_probe_event_call(&tk->tp), file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001866 }
1867
David Brazdil0f672f62019-12-10 10:32:29 +00001868 ret = trace_run_command("-:testprobe", create_or_delete_trace_kprobe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001869 if (WARN_ON_ONCE(ret)) {
1870 pr_warn("error on deleting a probe.\n");
1871 warn++;
1872 }
1873
David Brazdil0f672f62019-12-10 10:32:29 +00001874 ret = trace_run_command("-:testprobe2", create_or_delete_trace_kprobe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001875 if (WARN_ON_ONCE(ret)) {
1876 pr_warn("error on deleting a probe.\n");
1877 warn++;
1878 }
1879
1880end:
David Brazdil0f672f62019-12-10 10:32:29 +00001881 ret = dyn_events_release_all(&trace_kprobe_ops);
1882 if (WARN_ON_ONCE(ret)) {
1883 pr_warn("error on cleaning up probes.\n");
1884 warn++;
1885 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001886 /*
1887 * Wait for the optimizer work to finish. Otherwise it might fiddle
1888 * with probes in already freed __init text.
1889 */
1890 wait_for_kprobe_optimizer();
1891 if (warn)
1892 pr_cont("NG: Some tests are failed. Please check them.\n");
1893 else
1894 pr_cont("OK\n");
1895 return 0;
1896}
1897
1898late_initcall(kprobe_trace_self_tests_init);
1899
1900#endif