blob: ed1e3c2087abc5410dc6550856e096118566fd95 [file] [log] [blame]
Steven Rostedt (VMware)bcea3f92018-08-16 11:23:53 -04001// SPDX-License-Identifier: GPL-2.0
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04002/*
Masami Hiramatsu77b44d12009-11-03 19:12:47 -05003 * Kprobes-based tracing events
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04004 *
5 * Created by Masami Hiramatsu <mhiramat@redhat.com>
6 *
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04007 */
Masami Hiramatsu72576342017-02-07 20:21:28 +09008#define pr_fmt(fmt) "trace_kprobe: " fmt
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04009
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -040010#include <linux/security.h>
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040011#include <linux/module.h>
12#include <linux/uaccess.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010013#include <linux/rculist.h>
Masami Hiramatsu540adea2018-01-13 02:55:03 +090014#include <linux/error-injection.h>
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040015
Masami Hiramatsu970988e2019-05-22 17:32:35 +090016#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
17
Masami Hiramatsu6212dd22018-11-05 18:02:36 +090018#include "trace_dynevent.h"
Francis Deslauriersd8999262018-07-30 19:20:42 +090019#include "trace_kprobe_selftest.h"
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053020#include "trace_probe.h"
Masami Hiramatsu53305922018-04-25 21:18:03 +090021#include "trace_probe_tmpl.h"
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040022
Masami Hiramatsuf52487e2009-09-10 19:53:53 -040023#define KPROBE_EVENT_SYSTEM "kprobes"
Alban Crequy696ced42017-04-03 12:36:22 +020024#define KRETPROBE_MAXACTIVE_MAX 4096
Masami Hiramatsu970988e2019-05-22 17:32:35 +090025
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);
Masami Hiramatsu60efe212020-12-08 17:54:09 +090032 disable_tracing_selftest("running kprobe events");
33
Masami Hiramatsu970988e2019-05-22 17:32:35 +090034 return 0;
35}
36__setup("kprobe_event=", set_kprobe_boot_events);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040037
Masami Hiramatsud2622712021-02-01 13:48:11 -060038static int trace_kprobe_create(const char *raw_command);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +090039static 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,
Masami Hiramatsu30199132019-06-20 00:07:39 +090043 int argc, const char **argv, struct dyn_event *ev);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +090044
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
Valdis Klētniekscede6662019-03-12 04:58:32 -040053/*
Masami Hiramatsu77b44d12009-11-03 19:12:47 -050054 * Kprobe event core functions
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040055 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +090056struct trace_kprobe {
Masami Hiramatsu6212dd22018-11-05 18:02:36 +090057 struct dyn_event devent;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +020058 struct kretprobe rp; /* Use rp.kp for kprobe use */
Martin KaFai Laua7636d92016-02-03 12:28:28 -080059 unsigned long __percpu *nhit;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040060 const char *symbol; /* symbol name */
Namhyung Kimc31ffb32013-07-03 13:50:51 +090061 struct trace_probe tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040062};
63
Masami Hiramatsu6212dd22018-11-05 18:02:36 +090064static 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
Namhyung Kimc31ffb32013-07-03 13:50:51 +090083#define SIZEOF_TRACE_KPROBE(n) \
84 (offsetof(struct trace_kprobe, tp.args) + \
Masami Hiramatsueca0d912009-09-10 19:53:38 -040085 (sizeof(struct probe_arg) * (n)))
Masami Hiramatsua82378d2009-08-13 16:35:18 -040086
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090087static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040088{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090089 return tk->rp.handler != NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040090}
91
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090092static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040093{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090094 return tk->symbol ? tk->symbol : "unknown";
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040095}
96
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090097static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +090098{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090099 return tk->rp.kp.offset;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900100}
101
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900102static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900103{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900104 return !!(kprobe_gone(&tk->rp.kp));
Masami Hiramatsu61424312011-06-27 16:26:56 +0900105}
106
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900107static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900108 struct module *mod)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900109{
Jarkko Sakkinenf3d36422020-08-18 08:08:57 +0300110 int len = strlen(module_name(mod));
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900111 const char *name = trace_kprobe_symbol(tk);
Jarkko Sakkinenf3d36422020-08-18 08:08:57 +0300112
113 return strncmp(module_name(mod), name, len) == 0 && name[len] == ':';
Masami Hiramatsu61424312011-06-27 16:26:56 +0900114}
115
Masami Hiramatsu59158ec2018-08-29 01:18:15 +0900116static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900117{
Masami Hiramatsu59158ec2018-08-29 01:18:15 +0900118 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';
Christoph Hellwiga0060502021-02-02 13:13:25 +0100127 rcu_read_lock_sched();
Masami Hiramatsu59158ec2018-08-29 01:18:15 +0900128 ret = !!find_module(tk->symbol);
Christoph Hellwiga0060502021-02-02 13:13:25 +0100129 rcu_read_unlock_sched();
Masami Hiramatsu59158ec2018-08-29 01:18:15 +0900130 *p = ':';
131
132 return ret;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900133}
134
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900135static 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
Masami Hiramatsueb5bf812019-06-20 00:08:08 +0900142static 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
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900164static bool trace_kprobe_match(const char *system, const char *event,
Masami Hiramatsu30199132019-06-20 00:07:39 +0900165 int argc, const char **argv, struct dyn_event *ev)
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900166{
167 struct trace_kprobe *tk = to_trace_kprobe(ev);
168
Masami Hiramatsub55ce2032019-06-01 00:17:47 +0900169 return strcmp(trace_probe_name(&tk->tp), event) == 0 &&
Masami Hiramatsueb5bf812019-06-20 00:08:08 +0900170 (!system || strcmp(trace_probe_group_name(&tk->tp), system) == 0) &&
171 trace_kprobe_match_command_head(tk, argc, argv);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900172}
173
Marcin Nowakowskif18f97a2016-12-09 15:19:37 +0100174static 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
Masami Hiramatsu715fa2f2019-06-01 00:18:07 +0900185static 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
Masami Hiramatsu6bc6c772018-08-02 16:50:48 +0900191/* Return 0 if it fails to find the symbol address */
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900192static 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));
Masami Hiramatsu6bc6c772018-08-02 16:50:48 +0900200 if (addr)
201 addr += tk->rp.kp.offset;
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900202 } else {
203 addr = (unsigned long)tk->rp.kp.addr;
204 }
205 return addr;
206}
207
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900208static 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
Masami Hiramatsub4da3342018-01-13 02:54:04 +0900220bool trace_kprobe_on_func_entry(struct trace_event_call *call)
Josef Bacik9802d862017-12-11 11:36:48 -0500221{
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900222 struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
Masami Hiramatsub4da3342018-01-13 02:54:04 +0900223
Masami Hiramatsu97c753e2021-01-28 00:37:51 +0900224 return tk ? (kprobe_on_func_entry(tk->rp.kp.addr,
Masami Hiramatsub4da3342018-01-13 02:54:04 +0900225 tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
Masami Hiramatsu97c753e2021-01-28 00:37:51 +0900226 tk->rp.kp.addr ? 0 : tk->rp.kp.offset) == 0) : false;
Josef Bacik9802d862017-12-11 11:36:48 -0500227}
228
Masami Hiramatsub4da3342018-01-13 02:54:04 +0900229bool trace_kprobe_error_injectable(struct trace_event_call *call)
Josef Bacik9802d862017-12-11 11:36:48 -0500230{
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900231 struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
Josef Bacik9802d862017-12-11 11:36:48 -0500232
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900233 return tk ? within_error_injection_list(trace_kprobe_address(tk)) :
234 false;
Josef Bacik9802d862017-12-11 11:36:48 -0500235}
236
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900237static int register_kprobe_event(struct trace_kprobe *tk);
238static int unregister_kprobe_event(struct trace_kprobe *tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400239
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400240static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
241static int kretprobe_dispatcher(struct kretprobe_instance *ri,
242 struct pt_regs *regs);
243
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900244static 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
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200254/*
255 * Allocate new trace_probe and initialize it (including kprobes).
256 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900257static struct trace_kprobe *alloc_trace_kprobe(const char *group,
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400258 const char *event,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200259 void *addr,
260 const char *symbol,
261 unsigned long offs,
Alban Crequy696ced42017-04-03 12:36:22 +0200262 int maxactive,
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530263 int nargs, bool is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400264{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900265 struct trace_kprobe *tk;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500266 int ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400267
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900268 tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
269 if (!tk)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500270 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400271
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800272 tk->nhit = alloc_percpu(unsigned long);
273 if (!tk->nhit)
274 goto error;
275
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400276 if (symbol) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900277 tk->symbol = kstrdup(symbol, GFP_KERNEL);
278 if (!tk->symbol)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400279 goto error;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900280 tk->rp.kp.symbol_name = tk->symbol;
281 tk->rp.kp.offset = offs;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200282 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900283 tk->rp.kp.addr = addr;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200284
285 if (is_return)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900286 tk->rp.handler = kretprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200287 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900288 tk->rp.kp.pre_handler = kprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200289
Alban Crequy696ced42017-04-03 12:36:22 +0200290 tk->rp.maxactive = maxactive;
Masami Hiramatsu715fa2f2019-06-01 00:18:07 +0900291 INIT_HLIST_NODE(&tk->rp.kp.hlist);
292 INIT_LIST_HEAD(&tk->rp.kp.list);
Alban Crequy696ced42017-04-03 12:36:22 +0200293
Masami Hiramatsub61387c2020-01-22 12:23:25 +0900294 ret = trace_probe_init(&tk->tp, event, group, false);
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900295 if (ret < 0)
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400296 goto error;
297
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900298 dyn_event_init(&tk->devent, &trace_kprobe_ops);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900299 return tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400300error:
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900301 free_trace_kprobe(tk);
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500302 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400303}
304
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900305static struct trace_kprobe *find_trace_kprobe(const char *event,
306 const char *group)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400307{
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900308 struct dyn_event *pos;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900309 struct trace_kprobe *tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400310
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900311 for_each_trace_kprobe(tk, pos)
Masami Hiramatsub55ce2032019-06-01 00:17:47 +0900312 if (strcmp(trace_probe_name(&tk->tp), event) == 0 &&
313 strcmp(trace_probe_group_name(&tk->tp), group) == 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900314 return tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400315 return NULL;
316}
317
Steven Rostedt (VMware)87107a22018-07-26 12:07:32 -0400318static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
319{
320 int ret = 0;
321
Masami Hiramatsu715fa2f2019-06-01 00:18:07 +0900322 if (trace_kprobe_is_registered(tk) && !trace_kprobe_has_gone(tk)) {
Steven Rostedt (VMware)87107a22018-07-26 12:07:32 -0400323 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
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900332static 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
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200348/*
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900349 * Enable trace_probe
350 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
351 */
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900352static int enable_trace_kprobe(struct trace_event_call *call,
353 struct trace_event_file *file)
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900354{
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900355 struct trace_probe *pos, *tp;
356 struct trace_kprobe *tk;
357 bool enabled;
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900358 int ret = 0;
359
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900360 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 */
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900366 if (file) {
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900367 ret = trace_probe_add_file(tp, file);
Steven Rostedt (VMware)87107a22018-07-26 12:07:32 -0400368 if (ret)
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900369 return ret;
370 } else
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900371 trace_probe_set_flag(tp, TP_FLAG_PROFILE);
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900372
373 if (enabled)
374 return 0;
375
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900376 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);
Masami Hiramatsu44d00dc2019-09-18 17:55:37 +0900381 if (ret)
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900382 break;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900383 enabled = true;
384 }
385
Masami Hiramatsu44d00dc2019-09-18 17:55:37 +0900386 if (ret) {
387 /* Failed to enable one of them. Roll back all */
388 if (enabled)
389 __disable_trace_kprobe(tp);
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900390 if (file)
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900391 trace_probe_remove_file(tp, file);
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900392 else
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900393 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
Artem Savkov57ea2a32018-07-25 16:20:38 +0200394 }
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900395
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900396 return ret;
397}
398
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900399/*
400 * Disable trace_probe
401 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
402 */
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900403static int disable_trace_kprobe(struct trace_event_call *call,
404 struct trace_event_file *file)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900405{
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900406 struct trace_probe *tp;
407
408 tp = trace_probe_primary_from_call(call);
409 if (WARN_ON_ONCE(!tp))
410 return -ENODEV;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900411
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900412 if (file) {
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900413 if (!trace_probe_get_file_link(tp, file))
414 return -ENOENT;
415 if (!trace_probe_has_single_file(tp))
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200416 goto out;
Masami Hiramatsu747774d2019-06-01 00:17:37 +0900417 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900418 } else
Masami Hiramatsu747774d2019-06-01 00:17:37 +0900419 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900420
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900421 if (!trace_probe_is_enabled(tp))
422 __disable_trace_kprobe(tp);
Song Liue12f03d2017-12-06 14:45:15 -0800423
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200424 out:
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900425 if (file)
Masami Hiramatsua232e272013-07-09 18:35:26 +0900426 /*
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900427 * 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.
Masami Hiramatsua232e272013-07-09 18:35:26 +0900431 */
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900432 trace_probe_remove_file(tp, file);
Masami Hiramatsua232e272013-07-09 18:35:26 +0900433
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900434 return 0;
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900435}
436
Masami Hiramatsu7bb83f6f2021-01-08 13:19:38 +0900437#if defined(CONFIG_DYNAMIC_FTRACE) && \
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900438 !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
Masami Hiramatsuc7411a12019-10-29 17:31:44 +0900439static bool __within_notrace_func(unsigned long addr)
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900440{
Masami Hiramatsuc7411a12019-10-29 17:31:44 +0900441 unsigned long offset, size;
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900442
Masami Hiramatsu6bc6c772018-08-02 16:50:48 +0900443 if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
444 return false;
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900445
Masami Hiramatsu9161a862018-08-21 22:04:57 +0900446 /* 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);
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900454}
Masami Hiramatsuc7411a12019-10-29 17:31:44 +0900455
456static bool within_notrace_func(struct trace_kprobe *tk)
457{
Masami Hiramatsudcbd21c2020-04-25 14:49:09 +0900458 unsigned long addr = trace_kprobe_address(tk);
Masami Hiramatsuc7411a12019-10-29 17:31:44 +0900459 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}
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900477#else
478#define within_notrace_func(tk) (false)
479#endif
480
Masami Hiramatsu61424312011-06-27 16:26:56 +0900481/* Internal register function - just handle k*probes and flags */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900482static int __register_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900483{
Masami Hiramatsua6682812018-08-29 01:18:43 +0900484 int i, ret;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900485
David Howellsa94549d2019-08-19 17:17:58 -0700486 ret = security_locked_down(LOCKDOWN_KPROBES);
487 if (ret)
488 return ret;
489
Masami Hiramatsu715fa2f2019-06-01 00:18:07 +0900490 if (trace_kprobe_is_registered(tk))
Masami Hiramatsu61424312011-06-27 16:26:56 +0900491 return -EINVAL;
492
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900493 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
Masami Hiramatsua6682812018-08-29 01:18:43 +0900499 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
Masami Hiramatsu61424312011-06-27 16:26:56 +0900505 /* Set/clear disabled flag according to tp->flag */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900506 if (trace_probe_is_enabled(&tk->tp))
507 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900508 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900509 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900510
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900511 if (trace_kprobe_is_return(tk))
512 ret = register_kretprobe(&tk->rp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900513 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900514 ret = register_kprobe(&tk->rp.kp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900515
Masami Hiramatsu61424312011-06-27 16:26:56 +0900516 return ret;
517}
518
519/* Internal unregister function - just handle k*probes and flags */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900520static void __unregister_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900521{
Masami Hiramatsu715fa2f2019-06-01 00:18:07 +0900522 if (trace_kprobe_is_registered(tk)) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900523 if (trace_kprobe_is_return(tk))
524 unregister_kretprobe(&tk->rp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900525 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900526 unregister_kprobe(&tk->rp.kp);
Masami Hiramatsu715fa2f2019-06-01 00:18:07 +0900527 /* Cleanup kprobe for reuse and mark it unregistered */
528 INIT_HLIST_NODE(&tk->rp.kp.hlist);
529 INIT_LIST_HEAD(&tk->rp.kp.list);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900530 if (tk->rp.kp.symbol_name)
531 tk->rp.kp.addr = NULL;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900532 }
533}
534
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900535/* Unregister a trace_probe and probe_event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900536static int unregister_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400537{
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900538 /* If other probes are on the event, just unregister kprobe */
539 if (trace_probe_has_sibling(&tk->tp))
540 goto unreg;
541
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900542 /* Enabled event can not be unregistered */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900543 if (trace_probe_is_enabled(&tk->tp))
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900544 return -EBUSY;
545
Steven Rostedt (VMware)1d185382021-08-16 23:42:57 -0400546 /* If there's a reference to the dynamic event */
547 if (trace_event_dyn_busy(trace_probe_event_call(&tk->tp)))
548 return -EBUSY;
549
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400550 /* Will fail if probe is being used by ftrace or perf */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900551 if (unregister_kprobe_event(tk))
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400552 return -EBUSY;
553
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900554unreg:
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900555 __unregister_trace_kprobe(tk);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900556 dyn_event_remove(&tk->devent);
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900557 trace_probe_unlink(&tk->tp);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900558
559 return 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400560}
561
Masami Hiramatsufe60b0c2019-09-18 17:55:46 +0900562static bool trace_kprobe_has_same_kprobe(struct trace_kprobe *orig,
563 struct trace_kprobe *comp)
564{
565 struct trace_probe_event *tpe = orig->tp.event;
566 struct trace_probe *pos;
567 int i;
568
569 list_for_each_entry(pos, &tpe->probes, list) {
570 orig = container_of(pos, struct trace_kprobe, tp);
571 if (strcmp(trace_kprobe_symbol(orig),
572 trace_kprobe_symbol(comp)) ||
573 trace_kprobe_offset(orig) != trace_kprobe_offset(comp))
574 continue;
575
576 /*
577 * trace_probe_compare_arg_type() ensured that nr_args and
578 * each argument name and type are same. Let's compare comm.
579 */
580 for (i = 0; i < orig->tp.nr_args; i++) {
581 if (strcmp(orig->tp.args[i].comm,
582 comp->tp.args[i].comm))
Srikar Dronamrajuf8d7ab22019-09-24 17:19:06 +0530583 break;
Masami Hiramatsufe60b0c2019-09-18 17:55:46 +0900584 }
585
Srikar Dronamrajuf8d7ab22019-09-24 17:19:06 +0530586 if (i == orig->tp.nr_args)
587 return true;
Masami Hiramatsufe60b0c2019-09-18 17:55:46 +0900588 }
589
590 return false;
591}
592
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900593static int append_trace_kprobe(struct trace_kprobe *tk, struct trace_kprobe *to)
594{
595 int ret;
596
Masami Hiramatsufe60b0c2019-09-18 17:55:46 +0900597 ret = trace_probe_compare_arg_type(&tk->tp, &to->tp);
598 if (ret) {
599 /* Note that argument starts index = 2 */
600 trace_probe_log_set_index(ret + 1);
601 trace_probe_log_err(0, DIFF_ARG_TYPE);
602 return -EEXIST;
603 }
604 if (trace_kprobe_has_same_kprobe(to, tk)) {
605 trace_probe_log_set_index(0);
606 trace_probe_log_err(0, SAME_PROBE);
607 return -EEXIST;
608 }
609
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900610 /* Append to existing event */
611 ret = trace_probe_append(&tk->tp, &to->tp);
612 if (ret)
613 return ret;
614
615 /* Register k*probe */
616 ret = __register_trace_kprobe(tk);
617 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
618 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
619 ret = 0;
620 }
621
622 if (ret)
623 trace_probe_unlink(&tk->tp);
624 else
Steven Rostedt (VMware)8b0e6c72021-08-16 23:42:56 -0400625 dyn_event_add(&tk->devent, trace_probe_event_call(&tk->tp));
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900626
627 return ret;
628}
629
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400630/* Register a trace_probe and probe_event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900631static int register_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400632{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900633 struct trace_kprobe *old_tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400634 int ret;
635
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900636 mutex_lock(&event_mutex);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400637
Masami Hiramatsub55ce2032019-06-01 00:17:47 +0900638 old_tk = find_trace_kprobe(trace_probe_name(&tk->tp),
639 trace_probe_group_name(&tk->tp));
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900640 if (old_tk) {
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900641 if (trace_kprobe_is_return(tk) != trace_kprobe_is_return(old_tk)) {
642 trace_probe_log_set_index(0);
643 trace_probe_log_err(0, DIFF_PROBE_TYPE);
644 ret = -EEXIST;
645 } else {
Masami Hiramatsufe60b0c2019-09-18 17:55:46 +0900646 ret = append_trace_kprobe(tk, old_tk);
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900647 }
648 goto end;
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400649 }
Masami Hiramatsu61424312011-06-27 16:26:56 +0900650
651 /* Register new event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900652 ret = register_kprobe_event(tk);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400653 if (ret) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700654 pr_warn("Failed to register probe event(%d)\n", ret);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400655 goto end;
656 }
657
Masami Hiramatsu61424312011-06-27 16:26:56 +0900658 /* Register k*probe */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900659 ret = __register_trace_kprobe(tk);
Masami Hiramatsu59158ec2018-08-29 01:18:15 +0900660 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
661 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
662 ret = 0;
663 }
664
Masami Hiramatsu61424312011-06-27 16:26:56 +0900665 if (ret < 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900666 unregister_kprobe_event(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900667 else
Steven Rostedt (VMware)8b0e6c72021-08-16 23:42:56 -0400668 dyn_event_add(&tk->devent, trace_probe_event_call(&tk->tp));
Masami Hiramatsu61424312011-06-27 16:26:56 +0900669
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400670end:
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900671 mutex_unlock(&event_mutex);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400672 return ret;
673}
674
Masami Hiramatsu61424312011-06-27 16:26:56 +0900675/* Module notifier call back, checking event on the module */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900676static int trace_kprobe_module_callback(struct notifier_block *nb,
Masami Hiramatsu61424312011-06-27 16:26:56 +0900677 unsigned long val, void *data)
678{
679 struct module *mod = data;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900680 struct dyn_event *pos;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900681 struct trace_kprobe *tk;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900682 int ret;
683
684 if (val != MODULE_STATE_COMING)
685 return NOTIFY_DONE;
686
687 /* Update probes on coming module */
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900688 mutex_lock(&event_mutex);
689 for_each_trace_kprobe(tk, pos) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900690 if (trace_kprobe_within_module(tk, mod)) {
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900691 /* Don't need to check busy - this should have gone. */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900692 __unregister_trace_kprobe(tk);
693 ret = __register_trace_kprobe(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900694 if (ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700695 pr_warn("Failed to re-register probe %s on %s: %d\n",
Masami Hiramatsub55ce2032019-06-01 00:17:47 +0900696 trace_probe_name(&tk->tp),
Jarkko Sakkinenf3d36422020-08-18 08:08:57 +0300697 module_name(mod), ret);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900698 }
699 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900700 mutex_unlock(&event_mutex);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900701
702 return NOTIFY_DONE;
703}
704
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900705static struct notifier_block trace_kprobe_module_nb = {
706 .notifier_call = trace_kprobe_module_callback,
Masami Hiramatsu61424312011-06-27 16:26:56 +0900707 .priority = 1 /* Invoked after kprobe module callback */
708};
709
Naveen N. Raofca18a42017-07-08 00:27:30 +0530710/* Convert certain expected symbols into '_' when generating event names */
711static inline void sanitize_event_name(char *name)
712{
713 while (*name++ != '\0')
714 if (*name == ':' || *name == '.')
715 *name = '_';
716}
717
Masami Hiramatsud2622712021-02-01 13:48:11 -0600718static int __trace_kprobe_create(int argc, const char *argv[])
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400719{
720 /*
721 * Argument syntax:
Alban Crequy696ced42017-04-03 12:36:22 +0200722 * - Add kprobe:
723 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
724 * - Add kretprobe:
725 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
Masami Hiramatsu4725cd82020-09-10 17:55:35 +0900726 * Or
727 * p:[GRP/]EVENT] [MOD:]KSYM[+0]%return [FETCHARGS]
728 *
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400729 * Fetch args:
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400730 * $retval : fetch return value
731 * $stack : fetch stack address
732 * $stackN : fetch Nth of stack (N:0-)
Omar Sandoval35abb672016-06-08 18:38:02 -0700733 * $comm : fetch current task comm
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400734 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
735 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
736 * %REG : fetch register REG
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400737 * Dereferencing memory fetch:
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400738 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400739 * Alias name of args:
740 * NAME=FETCHARG : set NAME as alias of FETCHARG.
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400741 * Type of args:
742 * FETCHARG:TYPE : use TYPE instead of unsigned long.
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400743 */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500744 struct trace_kprobe *tk = NULL;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900745 int i, len, ret = 0;
746 bool is_return = false;
747 char *symbol = NULL, *tmp = NULL;
748 const char *event = NULL, *group = KPROBE_EVENT_SYSTEM;
Alban Crequy696ced42017-04-03 12:36:22 +0200749 int maxactive = 0;
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900750 long offset = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400751 void *addr = NULL;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200752 char buf[MAX_EVENT_NAME_LEN];
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900753 unsigned int flags = TPARG_FL_KERNEL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400754
Andrea Righi8b05a3a2019-01-11 07:01:13 +0100755 switch (argv[0][0]) {
756 case 'r':
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530757 is_return = true;
Andrea Righi8b05a3a2019-01-11 07:01:13 +0100758 break;
759 case 'p':
760 break;
761 default:
762 return -ECANCELED;
763 }
764 if (argc < 2)
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900765 return -ECANCELED;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400766
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500767 trace_probe_log_init("trace_kprobe", argc, argv);
768
Alban Crequy696ced42017-04-03 12:36:22 +0200769 event = strchr(&argv[0][1], ':');
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900770 if (event)
Alban Crequy696ced42017-04-03 12:36:22 +0200771 event++;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900772
Masami Hiramatsu287c0382019-03-14 13:30:09 +0900773 if (isdigit(argv[0][1])) {
774 if (!is_return) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500775 trace_probe_log_err(1, MAXACT_NO_KPROBE);
776 goto parse_error;
Masami Hiramatsu287c0382019-03-14 13:30:09 +0900777 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900778 if (event)
779 len = event - &argv[0][1] - 1;
780 else
781 len = strlen(&argv[0][1]);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500782 if (len > MAX_EVENT_NAME_LEN - 1) {
783 trace_probe_log_err(1, BAD_MAXACT);
784 goto parse_error;
785 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900786 memcpy(buf, &argv[0][1], len);
787 buf[len] = '\0';
788 ret = kstrtouint(buf, 0, &maxactive);
Masami Hiramatsu287c0382019-03-14 13:30:09 +0900789 if (ret || !maxactive) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500790 trace_probe_log_err(1, BAD_MAXACT);
791 goto parse_error;
Alban Crequy696ced42017-04-03 12:36:22 +0200792 }
793 /* kretprobes instances are iterated over via a list. The
794 * maximum should stay reasonable.
795 */
796 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500797 trace_probe_log_err(1, MAXACT_TOO_BIG);
798 goto parse_error;
Alban Crequy696ced42017-04-03 12:36:22 +0200799 }
800 }
801
Sabrina Dubroca9e52b322017-06-22 11:24:42 +0200802 /* try to parse an address. if that fails, try to read the
803 * input as a symbol. */
804 if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500805 trace_probe_log_set_index(1);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900806 /* Check whether uprobe event specified */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500807 if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
808 ret = -ECANCELED;
809 goto error;
810 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400811 /* a symbol specified */
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900812 symbol = kstrdup(argv[1], GFP_KERNEL);
813 if (!symbol)
814 return -ENOMEM;
Masami Hiramatsu4725cd82020-09-10 17:55:35 +0900815
816 tmp = strchr(symbol, '%');
817 if (tmp) {
818 if (!strcmp(tmp, "%return")) {
819 *tmp = '\0';
820 is_return = true;
821 } else {
822 trace_probe_log_err(tmp - symbol, BAD_ADDR_SUFFIX);
823 goto parse_error;
824 }
825 }
826
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400827 /* TODO: support .init module functions */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530828 ret = traceprobe_split_symbol_offset(symbol, &offset);
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900829 if (ret || offset < 0 || offset > UINT_MAX) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500830 trace_probe_log_err(0, BAD_PROBE_ADDR);
831 goto parse_error;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400832 }
Masami Hiramatsu4725cd82020-09-10 17:55:35 +0900833 if (is_return)
834 flags |= TPARG_FL_RETURN;
Masami Hiramatsu97c753e2021-01-28 00:37:51 +0900835 ret = kprobe_on_func_entry(NULL, symbol, offset);
836 if (ret == 0)
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900837 flags |= TPARG_FL_FENTRY;
Masami Hiramatsu97c753e2021-01-28 00:37:51 +0900838 /* Defer the ENOENT case until register kprobe */
839 if (ret == -EINVAL && is_return) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500840 trace_probe_log_err(0, BAD_RETPROBE);
841 goto parse_error;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400842 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400843 }
844
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500845 trace_probe_log_set_index(0);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900846 if (event) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500847 ret = traceprobe_parse_event_name(&event, &group, buf,
848 event - argv[0]);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900849 if (ret)
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500850 goto parse_error;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900851 } else {
Masami Hiramatsu42635652009-08-13 16:35:26 -0400852 /* Make a new event name */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400853 if (symbol)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500854 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400855 is_return ? 'r' : 'p', symbol, offset);
856 else
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500857 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400858 is_return ? 'r' : 'p', addr);
Naveen N. Raofca18a42017-07-08 00:27:30 +0530859 sanitize_event_name(buf);
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200860 event = buf;
861 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900862
863 /* setup a probe */
Alban Crequy696ced42017-04-03 12:36:22 +0200864 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500865 argc - 2, is_return);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900866 if (IS_ERR(tk)) {
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900867 ret = PTR_ERR(tk);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500868 /* This must return -ENOMEM, else there is a bug */
Masami Hiramatsua0394802019-03-14 13:30:50 +0900869 WARN_ON_ONCE(ret != -ENOMEM);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500870 goto out; /* We know tk is not allocated */
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400871 }
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500872 argc -= 2; argv += 2;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400873
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400874 /* parse arguments */
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400875 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500876 trace_probe_log_set_index(i + 2);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400877 ret = traceprobe_parse_probe_arg(&tk->tp, i, argv[i], flags);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900878 if (ret)
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500879 goto error; /* This can be -ENOMEM */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400880 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400881
Masami Hiramatsuf730e0f2019-06-01 00:16:46 +0900882 ret = traceprobe_set_print_fmt(&tk->tp, is_return);
883 if (ret < 0)
884 goto error;
885
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900886 ret = register_trace_kprobe(tk);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500887 if (ret) {
888 trace_probe_log_set_index(1);
889 if (ret == -EILSEQ)
890 trace_probe_log_err(0, BAD_INSN_BNDRY);
891 else if (ret == -ENOENT)
892 trace_probe_log_err(0, BAD_PROBE_ADDR);
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900893 else if (ret != -ENOMEM && ret != -EEXIST)
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500894 trace_probe_log_err(0, FAIL_REG_PROBE);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400895 goto error;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500896 }
897
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900898out:
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500899 trace_probe_log_clear();
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900900 kfree(symbol);
901 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400902
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500903parse_error:
904 ret = -EINVAL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400905error:
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900906 free_trace_kprobe(tk);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900907 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400908}
909
Masami Hiramatsud2622712021-02-01 13:48:11 -0600910static int trace_kprobe_create(const char *raw_command)
911{
912 return trace_probe_create(raw_command, __trace_kprobe_create);
913}
914
915static int create_or_delete_trace_kprobe(const char *raw_command)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400916{
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900917 int ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400918
Masami Hiramatsud2622712021-02-01 13:48:11 -0600919 if (raw_command[0] == '-')
920 return dyn_event_release(raw_command, &trace_kprobe_ops);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900921
Masami Hiramatsud2622712021-02-01 13:48:11 -0600922 ret = trace_kprobe_create(raw_command);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900923 return ret == -ECANCELED ? -EINVAL : ret;
924}
925
Tom Zanussi29a15482020-01-29 12:59:30 -0600926static int trace_kprobe_run_command(struct dynevent_cmd *cmd)
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600927{
Masami Hiramatsud2622712021-02-01 13:48:11 -0600928 return create_or_delete_trace_kprobe(cmd->seq.buffer);
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600929}
930
931/**
932 * kprobe_event_cmd_init - Initialize a kprobe event command object
933 * @cmd: A pointer to the dynevent_cmd struct representing the new event
934 * @buf: A pointer to the buffer used to build the command
935 * @maxlen: The length of the buffer passed in @buf
936 *
937 * Initialize a synthetic event command object. Use this before
938 * calling any of the other kprobe_event functions.
939 */
940void kprobe_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
941{
942 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_KPROBE,
Tom Zanussi29a15482020-01-29 12:59:30 -0600943 trace_kprobe_run_command);
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600944}
945EXPORT_SYMBOL_GPL(kprobe_event_cmd_init);
946
947/**
948 * __kprobe_event_gen_cmd_start - Generate a kprobe event command from arg list
949 * @cmd: A pointer to the dynevent_cmd struct representing the new event
950 * @name: The name of the kprobe event
951 * @loc: The location of the kprobe event
952 * @kretprobe: Is this a return probe?
953 * @args: Variable number of arg (pairs), one pair for each field
954 *
955 * NOTE: Users normally won't want to call this function directly, but
956 * rather use the kprobe_event_gen_cmd_start() wrapper, which automatically
957 * adds a NULL to the end of the arg list. If this function is used
958 * directly, make sure the last arg in the variable arg list is NULL.
959 *
960 * Generate a kprobe event command to be executed by
961 * kprobe_event_gen_cmd_end(). This function can be used to generate the
962 * complete command or only the first part of it; in the latter case,
963 * kprobe_event_add_fields() can be used to add more fields following this.
964 *
Masami Hiramatsu5b4dcd22020-04-25 14:49:26 +0900965 * Unlikely the synth_event_gen_cmd_start(), @loc must be specified. This
966 * returns -EINVAL if @loc == NULL.
967 *
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600968 * Return: 0 if successful, error otherwise.
969 */
970int __kprobe_event_gen_cmd_start(struct dynevent_cmd *cmd, bool kretprobe,
971 const char *name, const char *loc, ...)
972{
973 char buf[MAX_EVENT_NAME_LEN];
974 struct dynevent_arg arg;
975 va_list args;
976 int ret;
977
978 if (cmd->type != DYNEVENT_TYPE_KPROBE)
979 return -EINVAL;
980
Masami Hiramatsu5b4dcd22020-04-25 14:49:26 +0900981 if (!loc)
982 return -EINVAL;
983
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600984 if (kretprobe)
985 snprintf(buf, MAX_EVENT_NAME_LEN, "r:kprobes/%s", name);
986 else
987 snprintf(buf, MAX_EVENT_NAME_LEN, "p:kprobes/%s", name);
988
989 ret = dynevent_str_add(cmd, buf);
990 if (ret)
991 return ret;
992
Tom Zanussi74403b62020-01-31 15:55:32 -0600993 dynevent_arg_init(&arg, 0);
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600994 arg.str = loc;
Tom Zanussi74403b62020-01-31 15:55:32 -0600995 ret = dynevent_arg_add(cmd, &arg, NULL);
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600996 if (ret)
997 return ret;
998
999 va_start(args, loc);
1000 for (;;) {
1001 const char *field;
1002
1003 field = va_arg(args, const char *);
1004 if (!field)
1005 break;
1006
1007 if (++cmd->n_fields > MAX_TRACE_ARGS) {
1008 ret = -EINVAL;
1009 break;
1010 }
1011
1012 arg.str = field;
Tom Zanussi74403b62020-01-31 15:55:32 -06001013 ret = dynevent_arg_add(cmd, &arg, NULL);
Tom Zanussi2a588dd2020-01-29 12:59:29 -06001014 if (ret)
1015 break;
1016 }
1017 va_end(args);
1018
1019 return ret;
1020}
1021EXPORT_SYMBOL_GPL(__kprobe_event_gen_cmd_start);
1022
1023/**
1024 * __kprobe_event_add_fields - Add probe fields to a kprobe command from arg list
1025 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1026 * @args: Variable number of arg (pairs), one pair for each field
1027 *
1028 * NOTE: Users normally won't want to call this function directly, but
1029 * rather use the kprobe_event_add_fields() wrapper, which
1030 * automatically adds a NULL to the end of the arg list. If this
1031 * function is used directly, make sure the last arg in the variable
1032 * arg list is NULL.
1033 *
1034 * Add probe fields to an existing kprobe command using a variable
1035 * list of args. Fields are added in the same order they're listed.
1036 *
1037 * Return: 0 if successful, error otherwise.
1038 */
1039int __kprobe_event_add_fields(struct dynevent_cmd *cmd, ...)
1040{
1041 struct dynevent_arg arg;
1042 va_list args;
Gustavo A. R. Silva10f129c2020-02-05 16:34:04 -06001043 int ret = 0;
Tom Zanussi2a588dd2020-01-29 12:59:29 -06001044
1045 if (cmd->type != DYNEVENT_TYPE_KPROBE)
1046 return -EINVAL;
1047
Tom Zanussi74403b62020-01-31 15:55:32 -06001048 dynevent_arg_init(&arg, 0);
Tom Zanussi2a588dd2020-01-29 12:59:29 -06001049
1050 va_start(args, cmd);
1051 for (;;) {
1052 const char *field;
1053
1054 field = va_arg(args, const char *);
1055 if (!field)
1056 break;
1057
1058 if (++cmd->n_fields > MAX_TRACE_ARGS) {
1059 ret = -EINVAL;
1060 break;
1061 }
1062
1063 arg.str = field;
Tom Zanussi74403b62020-01-31 15:55:32 -06001064 ret = dynevent_arg_add(cmd, &arg, NULL);
Tom Zanussi2a588dd2020-01-29 12:59:29 -06001065 if (ret)
1066 break;
1067 }
1068 va_end(args);
1069
1070 return ret;
1071}
1072EXPORT_SYMBOL_GPL(__kprobe_event_add_fields);
1073
1074/**
1075 * kprobe_event_delete - Delete a kprobe event
1076 * @name: The name of the kprobe event to delete
1077 *
1078 * Delete a kprobe event with the give @name from kernel code rather
1079 * than directly from the command line.
1080 *
1081 * Return: 0 if successful, error otherwise.
1082 */
1083int kprobe_event_delete(const char *name)
1084{
1085 char buf[MAX_EVENT_NAME_LEN];
1086
1087 snprintf(buf, MAX_EVENT_NAME_LEN, "-:%s", name);
1088
Masami Hiramatsud2622712021-02-01 13:48:11 -06001089 return create_or_delete_trace_kprobe(buf);
Tom Zanussi2a588dd2020-01-29 12:59:29 -06001090}
1091EXPORT_SYMBOL_GPL(kprobe_event_delete);
1092
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001093static int trace_kprobe_release(struct dyn_event *ev)
1094{
1095 struct trace_kprobe *tk = to_trace_kprobe(ev);
1096 int ret = unregister_trace_kprobe(tk);
1097
1098 if (!ret)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001099 free_trace_kprobe(tk);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001100 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001101}
1102
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001103static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001104{
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001105 struct trace_kprobe *tk = to_trace_kprobe(ev);
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001106 int i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001107
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001108 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
Masami Hiramatsu6a13a0d2020-03-24 16:34:48 +09001109 if (trace_kprobe_is_return(tk) && tk->rp.maxactive)
1110 seq_printf(m, "%d", tk->rp.maxactive);
Masami Hiramatsub55ce2032019-06-01 00:17:47 +09001111 seq_printf(m, ":%s/%s", trace_probe_group_name(&tk->tp),
1112 trace_probe_name(&tk->tp));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001113
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001114 if (!tk->symbol)
1115 seq_printf(m, " 0x%p", tk->rp.kp.addr);
1116 else if (tk->rp.kp.offset)
1117 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
1118 tk->rp.kp.offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001119 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001120 seq_printf(m, " %s", trace_kprobe_symbol(tk));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001121
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001122 for (i = 0; i < tk->tp.nr_args; i++)
1123 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001124 seq_putc(m, '\n');
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001125
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001126 return 0;
1127}
1128
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001129static int probes_seq_show(struct seq_file *m, void *v)
1130{
1131 struct dyn_event *ev = v;
1132
1133 if (!is_trace_kprobe(ev))
1134 return 0;
1135
1136 return trace_kprobe_show(m, ev);
1137}
1138
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001139static const struct seq_operations probes_seq_op = {
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001140 .start = dyn_event_seq_start,
1141 .next = dyn_event_seq_next,
1142 .stop = dyn_event_seq_stop,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001143 .show = probes_seq_show
1144};
1145
1146static int probes_open(struct inode *inode, struct file *file)
1147{
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001148 int ret;
1149
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04001150 ret = security_locked_down(LOCKDOWN_TRACEFS);
1151 if (ret)
1152 return ret;
1153
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001154 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001155 ret = dyn_events_release_all(&trace_kprobe_ops);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001156 if (ret < 0)
1157 return ret;
1158 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001159
1160 return seq_open(file, &probes_seq_op);
1161}
1162
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001163static ssize_t probes_write(struct file *file, const char __user *buffer,
1164 size_t count, loff_t *ppos)
1165{
Tom Zanussi7e465ba2017-09-22 14:58:20 -05001166 return trace_parse_run_command(file, buffer, count, ppos,
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001167 create_or_delete_trace_kprobe);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001168}
1169
1170static const struct file_operations kprobe_events_ops = {
1171 .owner = THIS_MODULE,
1172 .open = probes_open,
1173 .read = seq_read,
1174 .llseek = seq_lseek,
1175 .release = seq_release,
1176 .write = probes_write,
1177};
1178
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001179/* Probes profiling interfaces */
1180static int probes_profile_seq_show(struct seq_file *m, void *v)
1181{
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001182 struct dyn_event *ev = v;
1183 struct trace_kprobe *tk;
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001184
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001185 if (!is_trace_kprobe(ev))
1186 return 0;
1187
1188 tk = to_trace_kprobe(ev);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001189 seq_printf(m, " %-44s %15lu %15lu\n",
Masami Hiramatsub55ce2032019-06-01 00:17:47 +09001190 trace_probe_name(&tk->tp),
Marcin Nowakowskif18f97a2016-12-09 15:19:37 +01001191 trace_kprobe_nhit(tk),
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001192 tk->rp.kp.nmissed);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001193
1194 return 0;
1195}
1196
1197static const struct seq_operations profile_seq_op = {
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001198 .start = dyn_event_seq_start,
1199 .next = dyn_event_seq_next,
1200 .stop = dyn_event_seq_stop,
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001201 .show = probes_profile_seq_show
1202};
1203
1204static int profile_open(struct inode *inode, struct file *file)
1205{
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04001206 int ret;
1207
1208 ret = security_locked_down(LOCKDOWN_TRACEFS);
1209 if (ret)
1210 return ret;
1211
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001212 return seq_open(file, &profile_seq_op);
1213}
1214
1215static const struct file_operations kprobe_profile_ops = {
1216 .owner = THIS_MODULE,
1217 .open = profile_open,
1218 .read = seq_read,
1219 .llseek = seq_lseek,
1220 .release = seq_release,
1221};
1222
Masami Hiramatsu53305922018-04-25 21:18:03 +09001223/* Kprobe specific fetch functions */
1224
1225/* Return the length of string -- including null terminal byte */
Masami Hiramatsu91784122018-04-25 21:19:01 +09001226static nokprobe_inline int
Masami Hiramatsu88903c42019-05-15 14:38:30 +09001227fetch_store_strlen_user(unsigned long addr)
1228{
1229 const void __user *uaddr = (__force const void __user *)addr;
1230
Christoph Hellwig02dddb12020-06-08 21:34:20 -07001231 return strnlen_user_nofault(uaddr, MAX_STRING_SIZE);
Masami Hiramatsu88903c42019-05-15 14:38:30 +09001232}
1233
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001234/* Return the length of string -- including null terminal byte */
Masami Hiramatsu91784122018-04-25 21:19:01 +09001235static nokprobe_inline int
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001236fetch_store_strlen(unsigned long addr)
Masami Hiramatsu53305922018-04-25 21:18:03 +09001237{
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001238 int ret, len = 0;
1239 u8 c;
Masami Hiramatsu53305922018-04-25 21:18:03 +09001240
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001241#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1242 if (addr < TASK_SIZE)
1243 return fetch_store_strlen_user(addr);
1244#endif
Masami Hiramatsu88903c42019-05-15 14:38:30 +09001245
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001246 do {
Christoph Hellwigfe557312020-06-17 09:37:53 +02001247 ret = copy_from_kernel_nofault(&c, (u8 *)addr + len, 1);
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001248 len++;
1249 } while (c && ret == 0 && len < MAX_STRING_SIZE);
Masami Hiramatsu88903c42019-05-15 14:38:30 +09001250
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001251 return (ret < 0) ? ret : len;
Masami Hiramatsu88903c42019-05-15 14:38:30 +09001252}
1253
1254/*
1255 * Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf
1256 * with max length and relative data location.
1257 */
1258static nokprobe_inline int
1259fetch_store_string_user(unsigned long addr, void *dest, void *base)
1260{
1261 const void __user *uaddr = (__force const void __user *)addr;
1262 int maxlen = get_loc_len(*(u32 *)dest);
1263 void *__dest;
1264 long ret;
1265
1266 if (unlikely(!maxlen))
1267 return -ENOMEM;
1268
1269 __dest = get_loc_data(dest, base);
1270
Christoph Hellwigbd88bb52020-06-08 21:34:14 -07001271 ret = strncpy_from_user_nofault(__dest, uaddr, maxlen);
Masami Hiramatsu88903c42019-05-15 14:38:30 +09001272 if (ret >= 0)
1273 *(u32 *)dest = make_data_loc(ret, __dest - base);
1274
Masami Hiramatsu91784122018-04-25 21:19:01 +09001275 return ret;
Masami Hiramatsu53305922018-04-25 21:18:03 +09001276}
1277
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001278/*
1279 * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
1280 * length and relative data location.
1281 */
Masami Hiramatsu9b960a32018-04-25 21:19:59 +09001282static nokprobe_inline int
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001283fetch_store_string(unsigned long addr, void *dest, void *base)
Masami Hiramatsu9b960a32018-04-25 21:19:59 +09001284{
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001285 int maxlen = get_loc_len(*(u32 *)dest);
1286 void *__dest;
1287 long ret;
1288
1289#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1290 if ((unsigned long)addr < TASK_SIZE)
1291 return fetch_store_string_user(addr, dest, base);
1292#endif
1293
1294 if (unlikely(!maxlen))
1295 return -ENOMEM;
1296
1297 __dest = get_loc_data(dest, base);
1298
1299 /*
1300 * Try to get string again, since the string can be changed while
1301 * probing.
1302 */
1303 ret = strncpy_from_kernel_nofault(__dest, (void *)addr, maxlen);
1304 if (ret >= 0)
1305 *(u32 *)dest = make_data_loc(ret, __dest - base);
1306
1307 return ret;
Masami Hiramatsu9b960a32018-04-25 21:19:59 +09001308}
1309
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +09001310static nokprobe_inline int
1311probe_mem_read_user(void *dest, void *src, size_t size)
1312{
Masami Hiramatsu539b75b2019-05-22 17:27:52 +09001313 const void __user *uaddr = (__force const void __user *)src;
1314
Christoph Hellwigc0ee37e2020-06-17 09:37:54 +02001315 return copy_from_user_nofault(dest, uaddr, size);
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +09001316}
1317
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001318static nokprobe_inline int
1319probe_mem_read(void *dest, void *src, size_t size)
1320{
1321#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1322 if ((unsigned long)src < TASK_SIZE)
1323 return probe_mem_read_user(dest, src, size);
1324#endif
Christoph Hellwigfe557312020-06-17 09:37:53 +02001325 return copy_from_kernel_nofault(dest, src, size);
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001326}
1327
Masami Hiramatsu53305922018-04-25 21:18:03 +09001328/* Note that we don't verify it, since the code does not come from user space */
1329static int
1330process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
Masami Hiramatsu91784122018-04-25 21:19:01 +09001331 void *base)
Masami Hiramatsu53305922018-04-25 21:18:03 +09001332{
1333 unsigned long val;
Masami Hiramatsu53305922018-04-25 21:18:03 +09001334
Masami Hiramatsua6682812018-08-29 01:18:43 +09001335retry:
Masami Hiramatsu53305922018-04-25 21:18:03 +09001336 /* 1st stage: get value from context */
1337 switch (code->op) {
1338 case FETCH_OP_REG:
1339 val = regs_get_register(regs, code->param);
1340 break;
1341 case FETCH_OP_STACK:
1342 val = regs_get_kernel_stack_nth(regs, code->param);
1343 break;
1344 case FETCH_OP_STACKP:
1345 val = kernel_stack_pointer(regs);
1346 break;
1347 case FETCH_OP_RETVAL:
1348 val = regs_return_value(regs);
1349 break;
1350 case FETCH_OP_IMM:
1351 val = code->immediate;
1352 break;
1353 case FETCH_OP_COMM:
1354 val = (unsigned long)current->comm;
1355 break;
Masami Hiramatsua42e3c42019-06-20 00:08:37 +09001356 case FETCH_OP_DATA:
1357 val = (unsigned long)code->data;
1358 break;
Masami Hiramatsua1303af2018-04-25 21:21:26 +09001359#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
1360 case FETCH_OP_ARG:
1361 val = regs_get_kernel_argument(regs, code->param);
1362 break;
1363#endif
Masami Hiramatsua6682812018-08-29 01:18:43 +09001364 case FETCH_NOP_SYMBOL: /* Ignore a place holder */
1365 code++;
1366 goto retry;
Masami Hiramatsu53305922018-04-25 21:18:03 +09001367 default:
1368 return -EILSEQ;
1369 }
1370 code++;
1371
Masami Hiramatsu9b960a32018-04-25 21:19:59 +09001372 return process_fetch_insn_bottom(code, val, dest, base);
Masami Hiramatsu53305922018-04-25 21:18:03 +09001373}
1374NOKPROBE_SYMBOL(process_fetch_insn)
1375
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001376/* Kprobe handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001377static nokprobe_inline void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001378__kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001379 struct trace_event_file *trace_file)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001380{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001381 struct kprobe_trace_entry_head *entry;
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001382 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001383 struct trace_event_buffer fbuffer;
1384 int dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001385
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001386 WARN_ON(call != trace_file->event_call);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001387
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -04001388 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -05001389 return;
Masami Hiramatsub8820082013-05-09 14:44:54 +09001390
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +01001391 fbuffer.trace_ctx = tracing_gen_ctx();
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001392 fbuffer.trace_file = trace_file;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001393
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001394 dsize = __get_data_size(&tk->tp, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001395
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001396 fbuffer.event =
1397 trace_event_buffer_lock_reserve(&fbuffer.buffer, trace_file,
1398 call->event.type,
1399 sizeof(*entry) + tk->tp.size + dsize,
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +01001400 fbuffer.trace_ctx);
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001401 if (!fbuffer.event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001402 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001403
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001404 fbuffer.regs = regs;
1405 entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001406 entry->ip = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsu91784122018-04-25 21:19:01 +09001407 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001408
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001409 trace_event_buffer_commit(&fbuffer);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001410}
1411
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001412static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001413kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001414{
Oleg Nesterovb04d52e2013-06-20 19:38:14 +02001415 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001416
Masami Hiramatsub5f935e2019-06-01 00:17:26 +09001417 trace_probe_for_each_link_rcu(link, &tk->tp)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001418 __kprobe_trace_func(tk, regs, link->file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001419}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001420NOKPROBE_SYMBOL(kprobe_trace_func);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001421
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001422/* Kretprobe handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001423static nokprobe_inline void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001424__kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001425 struct pt_regs *regs,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001426 struct trace_event_file *trace_file)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001427{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001428 struct kretprobe_trace_entry_head *entry;
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001429 struct trace_event_buffer fbuffer;
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001430 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001431 int dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001432
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001433 WARN_ON(call != trace_file->event_call);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001434
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -04001435 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -05001436 return;
Masami Hiramatsub8820082013-05-09 14:44:54 +09001437
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +01001438 fbuffer.trace_ctx = tracing_gen_ctx();
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001439 fbuffer.trace_file = trace_file;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001440
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001441 dsize = __get_data_size(&tk->tp, regs);
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001442 fbuffer.event =
1443 trace_event_buffer_lock_reserve(&fbuffer.buffer, trace_file,
1444 call->event.type,
1445 sizeof(*entry) + tk->tp.size + dsize,
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +01001446 fbuffer.trace_ctx);
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001447 if (!fbuffer.event)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001448 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001449
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001450 fbuffer.regs = regs;
1451 entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001452 entry->func = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001453 entry->ret_ip = (unsigned long)ri->ret_addr;
Masami Hiramatsu91784122018-04-25 21:19:01 +09001454 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001455
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001456 trace_event_buffer_commit(&fbuffer);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001457}
1458
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001459static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001460kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001461 struct pt_regs *regs)
1462{
Oleg Nesterovb04d52e2013-06-20 19:38:14 +02001463 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001464
Masami Hiramatsub5f935e2019-06-01 00:17:26 +09001465 trace_probe_for_each_link_rcu(link, &tk->tp)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001466 __kretprobe_trace_func(tk, ri, regs, link->file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001467}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001468NOKPROBE_SYMBOL(kretprobe_trace_func);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001469
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001470/* Event entry printers */
Masami Hiramatsub62fdd92013-05-13 20:58:39 +09001471static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001472print_kprobe_event(struct trace_iterator *iter, int flags,
1473 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001474{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001475 struct kprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001476 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001477 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001478
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001479 field = (struct kprobe_trace_entry_head *)iter->ent;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001480 tp = trace_probe_primary_from_call(
1481 container_of(event, struct trace_event_call, event));
1482 if (WARN_ON_ONCE(!tp))
1483 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001484
Masami Hiramatsub55ce2032019-06-01 00:17:47 +09001485 trace_seq_printf(s, "%s: (", trace_probe_name(tp));
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001486
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001487 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001488 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001489
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001490 trace_seq_putc(s, ')');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001491
Masami Hiramatsu56de7632018-04-25 21:16:36 +09001492 if (print_probe_args(s, tp->args, tp->nr_args,
1493 (u8 *)&field[1], field) < 0)
1494 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001495
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001496 trace_seq_putc(s, '\n');
1497 out:
1498 return trace_handle_return(s);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001499}
1500
Masami Hiramatsub62fdd92013-05-13 20:58:39 +09001501static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001502print_kretprobe_event(struct trace_iterator *iter, int flags,
1503 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001504{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001505 struct kretprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001506 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001507 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001508
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001509 field = (struct kretprobe_trace_entry_head *)iter->ent;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001510 tp = trace_probe_primary_from_call(
1511 container_of(event, struct trace_event_call, event));
1512 if (WARN_ON_ONCE(!tp))
1513 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001514
Masami Hiramatsub55ce2032019-06-01 00:17:47 +09001515 trace_seq_printf(s, "%s: (", trace_probe_name(tp));
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001516
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001517 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001518 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001519
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001520 trace_seq_puts(s, " <- ");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001521
1522 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001523 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001524
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001525 trace_seq_putc(s, ')');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001526
Masami Hiramatsu56de7632018-04-25 21:16:36 +09001527 if (print_probe_args(s, tp->args, tp->nr_args,
1528 (u8 *)&field[1], field) < 0)
1529 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001530
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001531 trace_seq_putc(s, '\n');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001532
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001533 out:
1534 return trace_handle_return(s);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001535}
1536
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001537
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001538static int kprobe_event_define_fields(struct trace_event_call *event_call)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001539{
Masami Hiramatsueeb07b02018-04-25 21:17:05 +09001540 int ret;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001541 struct kprobe_trace_entry_head field;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001542 struct trace_probe *tp;
1543
1544 tp = trace_probe_primary_from_call(event_call);
1545 if (WARN_ON_ONCE(!tp))
1546 return -ENOENT;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001547
Masami Hiramatsua703d942009-10-07 18:28:07 -04001548 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001549
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001550 return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001551}
1552
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001553static int kretprobe_event_define_fields(struct trace_event_call *event_call)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001554{
Masami Hiramatsueeb07b02018-04-25 21:17:05 +09001555 int ret;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001556 struct kretprobe_trace_entry_head field;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001557 struct trace_probe *tp;
1558
1559 tp = trace_probe_primary_from_call(event_call);
1560 if (WARN_ON_ONCE(!tp))
1561 return -ENOENT;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001562
Masami Hiramatsua703d942009-10-07 18:28:07 -04001563 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1564 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001565
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001566 return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001567}
1568
Li Zefan07b139c2009-12-21 14:27:35 +08001569#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001570
1571/* Kprobe profile handler */
Josef Bacik9802d862017-12-11 11:36:48 -05001572static int
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001573kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001574{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001575 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001576 struct kprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001577 struct hlist_head *head;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001578 int size, __size, dsize;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001579 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001580
Josef Bacik9802d862017-12-11 11:36:48 -05001581 if (bpf_prog_array_valid(call)) {
Masami Hiramatsu66665ad2018-01-13 02:54:33 +09001582 unsigned long orig_ip = instruction_pointer(regs);
Josef Bacik9802d862017-12-11 11:36:48 -05001583 int ret;
1584
1585 ret = trace_call_bpf(call, regs);
1586
1587 /*
1588 * We need to check and see if we modified the pc of the
Masami Hiramatsucce188b2018-06-20 01:15:45 +09001589 * pt_regs, and if so return 1 so that we don't do the
1590 * single stepping.
Josef Bacik9802d862017-12-11 11:36:48 -05001591 */
Masami Hiramatsucce188b2018-06-20 01:15:45 +09001592 if (orig_ip != instruction_pointer(regs))
Josef Bacik9802d862017-12-11 11:36:48 -05001593 return 1;
Josef Bacik9802d862017-12-11 11:36:48 -05001594 if (!ret)
1595 return 0;
1596 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07001597
Oleg Nesterov288e9842013-06-20 19:38:06 +02001598 head = this_cpu_ptr(call->perf_events);
1599 if (hlist_empty(head))
Josef Bacik9802d862017-12-11 11:36:48 -05001600 return 0;
Oleg Nesterov288e9842013-06-20 19:38:06 +02001601
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001602 dsize = __get_data_size(&tk->tp, regs);
1603 __size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001604 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1605 size -= sizeof(u32);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001606
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001607 entry = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001608 if (!entry)
Josef Bacik9802d862017-12-11 11:36:48 -05001609 return 0;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001610
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001611 entry->ip = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001612 memset(&entry[1], 0, dsize);
Masami Hiramatsu91784122018-04-25 21:19:01 +09001613 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001614 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02001615 head, NULL);
Josef Bacik9802d862017-12-11 11:36:48 -05001616 return 0;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001617}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001618NOKPROBE_SYMBOL(kprobe_perf_func);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001619
1620/* Kretprobe profile handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001621static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001622kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu2b106aa2013-05-09 14:44:41 +09001623 struct pt_regs *regs)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001624{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001625 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001626 struct kretprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001627 struct hlist_head *head;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001628 int size, __size, dsize;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001629 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001630
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001631 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
Alexei Starovoitov25415172015-03-25 12:49:20 -07001632 return;
1633
Oleg Nesterov288e9842013-06-20 19:38:06 +02001634 head = this_cpu_ptr(call->perf_events);
1635 if (hlist_empty(head))
1636 return;
1637
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001638 dsize = __get_data_size(&tk->tp, regs);
1639 __size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001640 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1641 size -= sizeof(u32);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001642
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001643 entry = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001644 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001645 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001646
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001647 entry->func = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001648 entry->ret_ip = (unsigned long)ri->ret_addr;
Masami Hiramatsu91784122018-04-25 21:19:01 +09001649 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001650 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02001651 head, NULL);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001652}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001653NOKPROBE_SYMBOL(kretprobe_perf_func);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07001654
1655int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1656 const char **symbol, u64 *probe_offset,
1657 u64 *probe_addr, bool perf_type_tracepoint)
1658{
1659 const char *pevent = trace_event_name(event->tp_event);
1660 const char *group = event->tp_event->class->system;
1661 struct trace_kprobe *tk;
1662
1663 if (perf_type_tracepoint)
1664 tk = find_trace_kprobe(pevent, group);
1665 else
Jean-Philippe Brucker22d5bd62020-06-08 14:45:32 +02001666 tk = trace_kprobe_primary_from_call(event->tp_event);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07001667 if (!tk)
1668 return -EINVAL;
1669
1670 *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1671 : BPF_FD_TYPE_KPROBE;
1672 if (tk->symbol) {
1673 *symbol = tk->symbol;
1674 *probe_offset = tk->rp.kp.offset;
1675 *probe_addr = 0;
1676 } else {
1677 *symbol = NULL;
1678 *probe_offset = 0;
1679 *probe_addr = (unsigned long)tk->rp.kp.addr;
1680 }
1681 return 0;
1682}
Li Zefan07b139c2009-12-21 14:27:35 +08001683#endif /* CONFIG_PERF_EVENTS */
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001684
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001685/*
1686 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1687 *
1688 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1689 * lockless, but we can't race with this __init function.
1690 */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001691static int kprobe_register(struct trace_event_call *event,
Masami Hiramatsufbc19632014-04-17 17:18:00 +09001692 enum trace_reg type, void *data)
Steven Rostedt22392912010-04-21 12:27:06 -04001693{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001694 struct trace_event_file *file = data;
Masami Hiramatsu1538f882011-06-27 16:26:44 +09001695
Steven Rostedt22392912010-04-21 12:27:06 -04001696 switch (type) {
1697 case TRACE_REG_REGISTER:
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001698 return enable_trace_kprobe(event, file);
Steven Rostedt22392912010-04-21 12:27:06 -04001699 case TRACE_REG_UNREGISTER:
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001700 return disable_trace_kprobe(event, file);
Steven Rostedt22392912010-04-21 12:27:06 -04001701
1702#ifdef CONFIG_PERF_EVENTS
1703 case TRACE_REG_PERF_REGISTER:
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001704 return enable_trace_kprobe(event, NULL);
Steven Rostedt22392912010-04-21 12:27:06 -04001705 case TRACE_REG_PERF_UNREGISTER:
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001706 return disable_trace_kprobe(event, NULL);
Jiri Olsaceec0b62012-02-15 15:51:49 +01001707 case TRACE_REG_PERF_OPEN:
1708 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +01001709 case TRACE_REG_PERF_ADD:
1710 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +01001711 return 0;
Steven Rostedt22392912010-04-21 12:27:06 -04001712#endif
1713 }
1714 return 0;
1715}
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001716
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001717static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001718{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001719 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
Josef Bacik9802d862017-12-11 11:36:48 -05001720 int ret = 0;
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001721
Martin KaFai Laua7636d92016-02-03 12:28:28 -08001722 raw_cpu_inc(*tk->nhit);
Masami Hiramatsu48182bd2013-05-09 14:44:36 +09001723
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001724 if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001725 kprobe_trace_func(tk, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001726#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001727 if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
Josef Bacik9802d862017-12-11 11:36:48 -05001728 ret = kprobe_perf_func(tk, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001729#endif
Josef Bacik9802d862017-12-11 11:36:48 -05001730 return ret;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001731}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001732NOKPROBE_SYMBOL(kprobe_dispatcher);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001733
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001734static int
1735kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001736{
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001737 struct kretprobe *rp = get_kretprobe(ri);
1738 struct trace_kprobe *tk = container_of(rp, struct trace_kprobe, rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001739
Martin KaFai Laua7636d92016-02-03 12:28:28 -08001740 raw_cpu_inc(*tk->nhit);
Masami Hiramatsu48182bd2013-05-09 14:44:36 +09001741
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001742 if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001743 kretprobe_trace_func(tk, ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001744#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001745 if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001746 kretprobe_perf_func(tk, ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001747#endif
Ingo Molnarf2cc0202021-03-23 18:49:35 +01001748 return 0; /* We don't tweak kernel, so just return 0 */
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001749}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001750NOKPROBE_SYMBOL(kretprobe_dispatcher);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001751
Steven Rostedta9a57762010-04-22 18:46:14 -04001752static struct trace_event_functions kretprobe_funcs = {
1753 .trace = print_kretprobe_event
1754};
1755
1756static struct trace_event_functions kprobe_funcs = {
1757 .trace = print_kprobe_event
1758};
1759
Peter Zijlstra04ae87a2019-10-24 22:26:59 +02001760static struct trace_event_fields kretprobe_fields_array[] = {
1761 { .type = TRACE_FUNCTION_TYPE,
1762 .define_fields = kretprobe_event_define_fields },
1763 {}
1764};
1765
1766static struct trace_event_fields kprobe_fields_array[] = {
1767 { .type = TRACE_FUNCTION_TYPE,
1768 .define_fields = kprobe_event_define_fields },
1769 {}
1770};
1771
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001772static inline void init_trace_event_call(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001773{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001774 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1775
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001776 if (trace_kprobe_is_return(tk)) {
Steven Rostedt80decc72010-04-23 10:00:22 -04001777 call->event.funcs = &kretprobe_funcs;
Peter Zijlstra04ae87a2019-10-24 22:26:59 +02001778 call->class->fields_array = kretprobe_fields_array;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001779 } else {
Steven Rostedt80decc72010-04-23 10:00:22 -04001780 call->event.funcs = &kprobe_funcs;
Peter Zijlstra04ae87a2019-10-24 22:26:59 +02001781 call->class->fields_array = kprobe_fields_array;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001782 }
Song Liue12f03d2017-12-06 14:45:15 -08001783
1784 call->flags = TRACE_EVENT_FL_KPROBE;
1785 call->class->reg = kprobe_register;
Song Liue12f03d2017-12-06 14:45:15 -08001786}
1787
1788static int register_kprobe_event(struct trace_kprobe *tk)
1789{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001790 init_trace_event_call(tk);
Song Liue12f03d2017-12-06 14:45:15 -08001791
Masami Hiramatsu46e53762019-06-01 00:17:16 +09001792 return trace_probe_register_event_call(&tk->tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001793}
1794
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001795static int unregister_kprobe_event(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001796{
Masami Hiramatsu46e53762019-06-01 00:17:16 +09001797 return trace_probe_unregister_event_call(&tk->tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001798}
1799
Song Liue12f03d2017-12-06 14:45:15 -08001800#ifdef CONFIG_PERF_EVENTS
1801/* create a trace_kprobe, but don't add it to global lists */
1802struct trace_event_call *
1803create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1804 bool is_return)
1805{
1806 struct trace_kprobe *tk;
1807 int ret;
1808 char *event;
1809
1810 /*
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001811 * local trace_kprobes are not added to dyn_event, so they are never
Song Liue12f03d2017-12-06 14:45:15 -08001812 * searched in find_trace_kprobe(). Therefore, there is no concern of
1813 * duplicated name here.
1814 */
1815 event = func ? func : "DUMMY_EVENT";
1816
1817 tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1818 offs, 0 /* maxactive */, 0 /* nargs */,
1819 is_return);
1820
1821 if (IS_ERR(tk)) {
1822 pr_info("Failed to allocate trace_probe.(%d)\n",
1823 (int)PTR_ERR(tk));
1824 return ERR_CAST(tk);
1825 }
1826
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001827 init_trace_event_call(tk);
Song Liue12f03d2017-12-06 14:45:15 -08001828
Masami Hiramatsu0a46c852018-04-25 21:19:30 +09001829 if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
Song Liue12f03d2017-12-06 14:45:15 -08001830 ret = -ENOMEM;
1831 goto error;
1832 }
1833
1834 ret = __register_trace_kprobe(tk);
Masami Hiramatsuf730e0f2019-06-01 00:16:46 +09001835 if (ret < 0)
Song Liue12f03d2017-12-06 14:45:15 -08001836 goto error;
1837
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001838 return trace_probe_event_call(&tk->tp);
Song Liue12f03d2017-12-06 14:45:15 -08001839error:
1840 free_trace_kprobe(tk);
1841 return ERR_PTR(ret);
1842}
1843
1844void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1845{
1846 struct trace_kprobe *tk;
1847
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001848 tk = trace_kprobe_primary_from_call(event_call);
1849 if (unlikely(!tk))
1850 return;
Song Liue12f03d2017-12-06 14:45:15 -08001851
1852 if (trace_probe_is_enabled(&tk->tp)) {
1853 WARN_ON(1);
1854 return;
1855 }
1856
1857 __unregister_trace_kprobe(tk);
Jiri Olsa0fc8c352018-07-09 16:19:06 +02001858
Song Liue12f03d2017-12-06 14:45:15 -08001859 free_trace_kprobe(tk);
1860}
1861#endif /* CONFIG_PERF_EVENTS */
1862
Masami Hiramatsu970988e2019-05-22 17:32:35 +09001863static __init void enable_boot_kprobe_events(void)
1864{
1865 struct trace_array *tr = top_trace_array();
1866 struct trace_event_file *file;
1867 struct trace_kprobe *tk;
1868 struct dyn_event *pos;
1869
1870 mutex_lock(&event_mutex);
1871 for_each_trace_kprobe(tk, pos) {
1872 list_for_each_entry(file, &tr->events, list)
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001873 if (file->event_call == trace_probe_event_call(&tk->tp))
Masami Hiramatsu970988e2019-05-22 17:32:35 +09001874 trace_event_enable_disable(file, 1, 0);
1875 }
1876 mutex_unlock(&event_mutex);
1877}
1878
1879static __init void setup_boot_kprobe_events(void)
1880{
1881 char *p, *cmd = kprobe_boot_events_buf;
1882 int ret;
1883
1884 strreplace(kprobe_boot_events_buf, ',', ' ');
1885
1886 while (cmd && *cmd != '\0') {
1887 p = strchr(cmd, ';');
1888 if (p)
1889 *p++ = '\0';
1890
Masami Hiramatsud2622712021-02-01 13:48:11 -06001891 ret = create_or_delete_trace_kprobe(cmd);
Masami Hiramatsu970988e2019-05-22 17:32:35 +09001892 if (ret)
1893 pr_warn("Failed to add event(%d): %s\n", ret, cmd);
1894
1895 cmd = p;
1896 }
1897
1898 enable_boot_kprobe_events();
1899}
1900
Masami Hiramatsud8d4c6d2020-01-11 01:05:42 +09001901/*
Masami Hiramatsuba0fbfb2020-09-10 21:39:17 +09001902 * Register dynevent at core_initcall. This allows kernel to setup kprobe
1903 * events in postcore_initcall without tracefs.
Masami Hiramatsud8d4c6d2020-01-11 01:05:42 +09001904 */
1905static __init int init_kprobe_trace_early(void)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001906{
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001907 int ret;
1908
1909 ret = dyn_event_register(&trace_kprobe_ops);
1910 if (ret)
1911 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001912
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001913 if (register_module_notifier(&trace_kprobe_module_nb))
Masami Hiramatsu61424312011-06-27 16:26:56 +09001914 return -EINVAL;
1915
Masami Hiramatsud8d4c6d2020-01-11 01:05:42 +09001916 return 0;
1917}
Masami Hiramatsuba0fbfb2020-09-10 21:39:17 +09001918core_initcall(init_kprobe_trace_early);
Masami Hiramatsud8d4c6d2020-01-11 01:05:42 +09001919
1920/* Make a tracefs interface for controlling probe points */
1921static __init int init_kprobe_trace(void)
1922{
Wei Yang22c36b12020-07-12 09:10:36 +08001923 int ret;
Masami Hiramatsud8d4c6d2020-01-11 01:05:42 +09001924 struct dentry *entry;
1925
Wei Yang22c36b12020-07-12 09:10:36 +08001926 ret = tracing_init_dentry();
1927 if (ret)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001928 return 0;
1929
Wei Yang22c36b12020-07-12 09:10:36 +08001930 entry = tracefs_create_file("kprobe_events", 0644, NULL,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001931 NULL, &kprobe_events_ops);
1932
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001933 /* Event list interface */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001934 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001935 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001936
1937 /* Profile interface */
Wei Yang22c36b12020-07-12 09:10:36 +08001938 entry = tracefs_create_file("kprobe_profile", 0444, NULL,
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001939 NULL, &kprobe_profile_ops);
1940
1941 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001942 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
Masami Hiramatsu970988e2019-05-22 17:32:35 +09001943
1944 setup_boot_kprobe_events();
1945
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001946 return 0;
1947}
1948fs_initcall(init_kprobe_trace);
1949
1950
1951#ifdef CONFIG_FTRACE_STARTUP_TEST
Arnd Bergmann26a346f2017-02-01 17:57:56 +01001952static __init struct trace_event_file *
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001953find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001954{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001955 struct trace_event_file *file;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001956
1957 list_for_each_entry(file, &tr->events, list)
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001958 if (file->event_call == trace_probe_event_call(&tk->tp))
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001959 return file;
1960
1961 return NULL;
1962}
1963
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001964/*
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001965 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001966 * stage, we can do this lockless.
1967 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001968static __init int kprobe_trace_self_tests_init(void)
1969{
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001970 int ret, warn = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001971 int (*target)(int, int, int, int, int, int);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001972 struct trace_kprobe *tk;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001973 struct trace_event_file *file;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001974
Yoshihiro YUNOMAE748ec3a2014-06-06 07:35:20 +09001975 if (tracing_is_disabled())
1976 return -ENODEV;
1977
Masami Hiramatsu60efe212020-12-08 17:54:09 +09001978 if (tracing_selftest_disabled)
Steven Rostedt (VMware)b6399cc2019-05-23 19:50:34 -04001979 return 0;
Steven Rostedt (VMware)b6399cc2019-05-23 19:50:34 -04001980
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001981 target = kprobe_trace_selftest_target;
1982
1983 pr_info("Testing kprobe tracing: ");
1984
Masami Hiramatsud2622712021-02-01 13:48:11 -06001985 ret = create_or_delete_trace_kprobe("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001986 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001987 pr_warn("error on probing function entry.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001988 warn++;
1989 } else {
1990 /* Enable trace point */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001991 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1992 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001993 pr_warn("error on getting new probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001994 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001995 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001996 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001997 if (WARN_ON_ONCE(file == NULL)) {
1998 pr_warn("error on getting probe file.\n");
1999 warn++;
2000 } else
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09002001 enable_trace_kprobe(
2002 trace_probe_event_call(&tk->tp), file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002003 }
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002004 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04002005
Masami Hiramatsud2622712021-02-01 13:48:11 -06002006 ret = create_or_delete_trace_kprobe("r:testprobe2 kprobe_trace_selftest_target $retval");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002007 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002008 pr_warn("error on probing function return.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002009 warn++;
2010 } else {
2011 /* Enable trace point */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09002012 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
2013 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002014 pr_warn("error on getting 2nd new probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002015 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002016 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09002017 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002018 if (WARN_ON_ONCE(file == NULL)) {
2019 pr_warn("error on getting probe file.\n");
2020 warn++;
2021 } else
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09002022 enable_trace_kprobe(
2023 trace_probe_event_call(&tk->tp), file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002024 }
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002025 }
2026
2027 if (warn)
2028 goto end;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04002029
2030 ret = target(1, 2, 3, 4, 5, 6);
2031
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01002032 /*
2033 * Not expecting an error here, the check is only to prevent the
2034 * optimizer from removing the call to target() as otherwise there
2035 * are no side-effects and the call is never performed.
2036 */
2037 if (ret != 21)
2038 warn++;
2039
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09002040 /* Disable trace points before removing it */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09002041 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
2042 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002043 pr_warn("error on getting test probe.\n");
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09002044 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002045 } else {
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01002046 if (trace_kprobe_nhit(tk) != 1) {
2047 pr_warn("incorrect number of testprobe hits\n");
2048 warn++;
2049 }
2050
Namhyung Kimc31ffb32013-07-03 13:50:51 +09002051 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002052 if (WARN_ON_ONCE(file == NULL)) {
2053 pr_warn("error on getting probe file.\n");
2054 warn++;
2055 } else
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09002056 disable_trace_kprobe(
2057 trace_probe_event_call(&tk->tp), file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002058 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09002059
Namhyung Kimc31ffb32013-07-03 13:50:51 +09002060 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
2061 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002062 pr_warn("error on getting 2nd test probe.\n");
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09002063 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002064 } else {
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01002065 if (trace_kprobe_nhit(tk) != 1) {
2066 pr_warn("incorrect number of testprobe2 hits\n");
2067 warn++;
2068 }
2069
Namhyung Kimc31ffb32013-07-03 13:50:51 +09002070 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002071 if (WARN_ON_ONCE(file == NULL)) {
2072 pr_warn("error on getting probe file.\n");
2073 warn++;
2074 } else
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09002075 disable_trace_kprobe(
2076 trace_probe_event_call(&tk->tp), file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002077 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09002078
Masami Hiramatsud2622712021-02-01 13:48:11 -06002079 ret = create_or_delete_trace_kprobe("-:testprobe");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002080 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002081 pr_warn("error on deleting a probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002082 warn++;
2083 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04002084
Masami Hiramatsud2622712021-02-01 13:48:11 -06002085 ret = create_or_delete_trace_kprobe("-:testprobe2");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002086 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002087 pr_warn("error on deleting a probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002088 warn++;
2089 }
2090
2091end:
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09002092 ret = dyn_events_release_all(&trace_kprobe_ops);
2093 if (WARN_ON_ONCE(ret)) {
2094 pr_warn("error on cleaning up probes.\n");
2095 warn++;
2096 }
Thomas Gleixner30e7d8942017-05-17 10:19:49 +02002097 /*
2098 * Wait for the optimizer work to finish. Otherwise it might fiddle
2099 * with probes in already freed __init text.
2100 */
2101 wait_for_kprobe_optimizer();
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002102 if (warn)
2103 pr_cont("NG: Some tests are failed. Please check them.\n");
2104 else
2105 pr_cont("OK\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04002106 return 0;
2107}
2108
2109late_initcall(kprobe_trace_self_tests_init);
2110
2111#endif