blob: 508f14af4f2c7eb3ddad503adb2701baf05fb8fb [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
Jakub Kicinskiaef2fed2021-12-15 18:55:37 -080010#include <linux/bpf-cgroup.h>
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -040011#include <linux/security.h>
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040012#include <linux/module.h>
13#include <linux/uaccess.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010014#include <linux/rculist.h>
Masami Hiramatsu540adea2018-01-13 02:55:03 +090015#include <linux/error-injection.h>
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040016
Masami Hiramatsu970988e2019-05-22 17:32:35 +090017#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
18
Masami Hiramatsu6212dd22018-11-05 18:02:36 +090019#include "trace_dynevent.h"
Francis Deslauriersd8999262018-07-30 19:20:42 +090020#include "trace_kprobe_selftest.h"
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053021#include "trace_probe.h"
Masami Hiramatsu53305922018-04-25 21:18:03 +090022#include "trace_probe_tmpl.h"
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040023
Masami Hiramatsuf52487e2009-09-10 19:53:53 -040024#define KPROBE_EVENT_SYSTEM "kprobes"
Alban Crequy696ced42017-04-03 12:36:22 +020025#define KRETPROBE_MAXACTIVE_MAX 4096
Masami Hiramatsu970988e2019-05-22 17:32:35 +090026
27/* Kprobe early definition from command line */
28static char kprobe_boot_events_buf[COMMAND_LINE_SIZE] __initdata;
29
30static int __init set_kprobe_boot_events(char *str)
31{
32 strlcpy(kprobe_boot_events_buf, str, COMMAND_LINE_SIZE);
Masami Hiramatsu60efe212020-12-08 17:54:09 +090033 disable_tracing_selftest("running kprobe events");
34
Masami Hiramatsu970988e2019-05-22 17:32:35 +090035 return 0;
36}
37__setup("kprobe_event=", set_kprobe_boot_events);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040038
Masami Hiramatsud2622712021-02-01 13:48:11 -060039static int trace_kprobe_create(const char *raw_command);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +090040static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev);
41static int trace_kprobe_release(struct dyn_event *ev);
42static bool trace_kprobe_is_busy(struct dyn_event *ev);
43static bool trace_kprobe_match(const char *system, const char *event,
Masami Hiramatsu30199132019-06-20 00:07:39 +090044 int argc, const char **argv, struct dyn_event *ev);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +090045
46static struct dyn_event_operations trace_kprobe_ops = {
47 .create = trace_kprobe_create,
48 .show = trace_kprobe_show,
49 .is_busy = trace_kprobe_is_busy,
50 .free = trace_kprobe_release,
51 .match = trace_kprobe_match,
52};
53
Valdis Klētniekscede6662019-03-12 04:58:32 -040054/*
Masami Hiramatsu77b44d12009-11-03 19:12:47 -050055 * Kprobe event core functions
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040056 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +090057struct trace_kprobe {
Masami Hiramatsu6212dd22018-11-05 18:02:36 +090058 struct dyn_event devent;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +020059 struct kretprobe rp; /* Use rp.kp for kprobe use */
Martin KaFai Laua7636d92016-02-03 12:28:28 -080060 unsigned long __percpu *nhit;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040061 const char *symbol; /* symbol name */
Namhyung Kimc31ffb32013-07-03 13:50:51 +090062 struct trace_probe tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040063};
64
Masami Hiramatsu6212dd22018-11-05 18:02:36 +090065static bool is_trace_kprobe(struct dyn_event *ev)
66{
67 return ev->ops == &trace_kprobe_ops;
68}
69
70static struct trace_kprobe *to_trace_kprobe(struct dyn_event *ev)
71{
72 return container_of(ev, struct trace_kprobe, devent);
73}
74
75/**
76 * for_each_trace_kprobe - iterate over the trace_kprobe list
77 * @pos: the struct trace_kprobe * for each entry
78 * @dpos: the struct dyn_event * to use as a loop cursor
79 */
80#define for_each_trace_kprobe(pos, dpos) \
81 for_each_dyn_event(dpos) \
82 if (is_trace_kprobe(dpos) && (pos = to_trace_kprobe(dpos)))
83
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090084static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040085{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090086 return tk->rp.handler != NULL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040087}
88
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090089static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040090{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090091 return tk->symbol ? tk->symbol : "unknown";
Masami Hiramatsu413d37d2009-08-13 16:35:11 -040092}
93
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090094static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +090095{
Namhyung Kimc31ffb32013-07-03 13:50:51 +090096 return tk->rp.kp.offset;
Masami Hiramatsu61424312011-06-27 16:26:56 +090097}
98
Masami Hiramatsu3da0f182014-04-17 17:18:28 +090099static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900100{
Masami Hiramatsu29e8077a2021-09-14 23:40:16 +0900101 return kprobe_gone(&tk->rp.kp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900102}
103
Masami Hiramatsu3da0f182014-04-17 17:18:28 +0900104static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900105 struct module *mod)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900106{
Jarkko Sakkinenf3d36422020-08-18 08:08:57 +0300107 int len = strlen(module_name(mod));
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900108 const char *name = trace_kprobe_symbol(tk);
Jarkko Sakkinenf3d36422020-08-18 08:08:57 +0300109
110 return strncmp(module_name(mod), name, len) == 0 && name[len] == ':';
Masami Hiramatsu61424312011-06-27 16:26:56 +0900111}
112
Masami Hiramatsu59158ec2018-08-29 01:18:15 +0900113static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900114{
Masami Hiramatsu59158ec2018-08-29 01:18:15 +0900115 char *p;
116 bool ret;
117
118 if (!tk->symbol)
119 return false;
120 p = strchr(tk->symbol, ':');
121 if (!p)
122 return true;
123 *p = '\0';
Christoph Hellwiga0060502021-02-02 13:13:25 +0100124 rcu_read_lock_sched();
Masami Hiramatsu59158ec2018-08-29 01:18:15 +0900125 ret = !!find_module(tk->symbol);
Christoph Hellwiga0060502021-02-02 13:13:25 +0100126 rcu_read_unlock_sched();
Masami Hiramatsu59158ec2018-08-29 01:18:15 +0900127 *p = ':';
128
129 return ret;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900130}
131
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900132static bool trace_kprobe_is_busy(struct dyn_event *ev)
133{
134 struct trace_kprobe *tk = to_trace_kprobe(ev);
135
136 return trace_probe_is_enabled(&tk->tp);
137}
138
Masami Hiramatsueb5bf812019-06-20 00:08:08 +0900139static bool trace_kprobe_match_command_head(struct trace_kprobe *tk,
140 int argc, const char **argv)
141{
142 char buf[MAX_ARGSTR_LEN + 1];
143
144 if (!argc)
145 return true;
146
147 if (!tk->symbol)
148 snprintf(buf, sizeof(buf), "0x%p", tk->rp.kp.addr);
149 else if (tk->rp.kp.offset)
150 snprintf(buf, sizeof(buf), "%s+%u",
151 trace_kprobe_symbol(tk), tk->rp.kp.offset);
152 else
153 snprintf(buf, sizeof(buf), "%s", trace_kprobe_symbol(tk));
154 if (strcmp(buf, argv[0]))
155 return false;
156 argc--; argv++;
157
158 return trace_probe_match_command_args(&tk->tp, argc, argv);
159}
160
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900161static bool trace_kprobe_match(const char *system, const char *event,
Masami Hiramatsu30199132019-06-20 00:07:39 +0900162 int argc, const char **argv, struct dyn_event *ev)
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900163{
164 struct trace_kprobe *tk = to_trace_kprobe(ev);
165
Masami Hiramatsub55ce2032019-06-01 00:17:47 +0900166 return strcmp(trace_probe_name(&tk->tp), event) == 0 &&
Masami Hiramatsueb5bf812019-06-20 00:08:08 +0900167 (!system || strcmp(trace_probe_group_name(&tk->tp), system) == 0) &&
168 trace_kprobe_match_command_head(tk, argc, argv);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900169}
170
Marcin Nowakowskif18f97a2016-12-09 15:19:37 +0100171static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
172{
173 unsigned long nhit = 0;
174 int cpu;
175
176 for_each_possible_cpu(cpu)
177 nhit += *per_cpu_ptr(tk->nhit, cpu);
178
179 return nhit;
180}
181
Masami Hiramatsu715fa2f2019-06-01 00:18:07 +0900182static nokprobe_inline bool trace_kprobe_is_registered(struct trace_kprobe *tk)
183{
184 return !(list_empty(&tk->rp.kp.list) &&
185 hlist_unhashed(&tk->rp.kp.hlist));
186}
187
Masami Hiramatsu6bc6c772018-08-02 16:50:48 +0900188/* Return 0 if it fails to find the symbol address */
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900189static nokprobe_inline
190unsigned long trace_kprobe_address(struct trace_kprobe *tk)
191{
192 unsigned long addr;
193
194 if (tk->symbol) {
195 addr = (unsigned long)
196 kallsyms_lookup_name(trace_kprobe_symbol(tk));
Masami Hiramatsu6bc6c772018-08-02 16:50:48 +0900197 if (addr)
198 addr += tk->rp.kp.offset;
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900199 } else {
200 addr = (unsigned long)tk->rp.kp.addr;
201 }
202 return addr;
203}
204
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900205static nokprobe_inline struct trace_kprobe *
206trace_kprobe_primary_from_call(struct trace_event_call *call)
207{
208 struct trace_probe *tp;
209
210 tp = trace_probe_primary_from_call(call);
211 if (WARN_ON_ONCE(!tp))
212 return NULL;
213
214 return container_of(tp, struct trace_kprobe, tp);
215}
216
Masami Hiramatsub4da3342018-01-13 02:54:04 +0900217bool trace_kprobe_on_func_entry(struct trace_event_call *call)
Josef Bacik9802d862017-12-11 11:36:48 -0500218{
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900219 struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
Masami Hiramatsub4da3342018-01-13 02:54:04 +0900220
Masami Hiramatsu97c753e2021-01-28 00:37:51 +0900221 return tk ? (kprobe_on_func_entry(tk->rp.kp.addr,
Masami Hiramatsub4da3342018-01-13 02:54:04 +0900222 tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
Masami Hiramatsu97c753e2021-01-28 00:37:51 +0900223 tk->rp.kp.addr ? 0 : tk->rp.kp.offset) == 0) : false;
Josef Bacik9802d862017-12-11 11:36:48 -0500224}
225
Masami Hiramatsub4da3342018-01-13 02:54:04 +0900226bool trace_kprobe_error_injectable(struct trace_event_call *call)
Josef Bacik9802d862017-12-11 11:36:48 -0500227{
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900228 struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
Josef Bacik9802d862017-12-11 11:36:48 -0500229
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900230 return tk ? within_error_injection_list(trace_kprobe_address(tk)) :
231 false;
Josef Bacik9802d862017-12-11 11:36:48 -0500232}
233
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900234static int register_kprobe_event(struct trace_kprobe *tk);
235static int unregister_kprobe_event(struct trace_kprobe *tk);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400236
Masami Hiramatsu50d78052009-09-14 16:49:20 -0400237static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
238static int kretprobe_dispatcher(struct kretprobe_instance *ri,
239 struct pt_regs *regs);
240
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900241static void free_trace_kprobe(struct trace_kprobe *tk)
242{
243 if (tk) {
244 trace_probe_cleanup(&tk->tp);
245 kfree(tk->symbol);
246 free_percpu(tk->nhit);
247 kfree(tk);
248 }
249}
250
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200251/*
252 * Allocate new trace_probe and initialize it (including kprobes).
253 */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900254static struct trace_kprobe *alloc_trace_kprobe(const char *group,
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400255 const char *event,
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200256 void *addr,
257 const char *symbol,
258 unsigned long offs,
Alban Crequy696ced42017-04-03 12:36:22 +0200259 int maxactive,
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530260 int nargs, bool is_return)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400261{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900262 struct trace_kprobe *tk;
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500263 int ret = -ENOMEM;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400264
Steven Rostedt (VMware)845cbf32021-08-16 23:43:00 -0400265 tk = kzalloc(struct_size(tk, tp.args, nargs), GFP_KERNEL);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900266 if (!tk)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500267 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400268
Martin KaFai Laua7636d92016-02-03 12:28:28 -0800269 tk->nhit = alloc_percpu(unsigned long);
270 if (!tk->nhit)
271 goto error;
272
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400273 if (symbol) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900274 tk->symbol = kstrdup(symbol, GFP_KERNEL);
275 if (!tk->symbol)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400276 goto error;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900277 tk->rp.kp.symbol_name = tk->symbol;
278 tk->rp.kp.offset = offs;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200279 } else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900280 tk->rp.kp.addr = addr;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200281
282 if (is_return)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900283 tk->rp.handler = kretprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200284 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900285 tk->rp.kp.pre_handler = kprobe_dispatcher;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200286
Alban Crequy696ced42017-04-03 12:36:22 +0200287 tk->rp.maxactive = maxactive;
Masami Hiramatsu715fa2f2019-06-01 00:18:07 +0900288 INIT_HLIST_NODE(&tk->rp.kp.hlist);
289 INIT_LIST_HEAD(&tk->rp.kp.list);
Alban Crequy696ced42017-04-03 12:36:22 +0200290
Masami Hiramatsub61387c2020-01-22 12:23:25 +0900291 ret = trace_probe_init(&tk->tp, event, group, false);
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900292 if (ret < 0)
Masami Hiramatsuf52487e2009-09-10 19:53:53 -0400293 goto error;
294
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900295 dyn_event_init(&tk->devent, &trace_kprobe_ops);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900296 return tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400297error:
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900298 free_trace_kprobe(tk);
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500299 return ERR_PTR(ret);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400300}
301
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900302static struct trace_kprobe *find_trace_kprobe(const char *event,
303 const char *group)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400304{
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900305 struct dyn_event *pos;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900306 struct trace_kprobe *tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400307
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900308 for_each_trace_kprobe(tk, pos)
Masami Hiramatsub55ce2032019-06-01 00:17:47 +0900309 if (strcmp(trace_probe_name(&tk->tp), event) == 0 &&
310 strcmp(trace_probe_group_name(&tk->tp), group) == 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900311 return tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400312 return NULL;
313}
314
Steven Rostedt (VMware)87107a22018-07-26 12:07:32 -0400315static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
316{
317 int ret = 0;
318
Masami Hiramatsu715fa2f2019-06-01 00:18:07 +0900319 if (trace_kprobe_is_registered(tk) && !trace_kprobe_has_gone(tk)) {
Steven Rostedt (VMware)87107a22018-07-26 12:07:32 -0400320 if (trace_kprobe_is_return(tk))
321 ret = enable_kretprobe(&tk->rp);
322 else
323 ret = enable_kprobe(&tk->rp.kp);
324 }
325
326 return ret;
327}
328
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900329static void __disable_trace_kprobe(struct trace_probe *tp)
330{
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900331 struct trace_kprobe *tk;
332
Jiri Olsae161c6b2021-11-25 21:28:52 +0100333 list_for_each_entry(tk, trace_probe_probe_list(tp), tp.list) {
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900334 if (!trace_kprobe_is_registered(tk))
335 continue;
336 if (trace_kprobe_is_return(tk))
337 disable_kretprobe(&tk->rp);
338 else
339 disable_kprobe(&tk->rp.kp);
340 }
341}
342
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200343/*
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900344 * Enable trace_probe
345 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
346 */
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900347static int enable_trace_kprobe(struct trace_event_call *call,
348 struct trace_event_file *file)
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900349{
Jiri Olsae161c6b2021-11-25 21:28:52 +0100350 struct trace_probe *tp;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900351 struct trace_kprobe *tk;
352 bool enabled;
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900353 int ret = 0;
354
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900355 tp = trace_probe_primary_from_call(call);
356 if (WARN_ON_ONCE(!tp))
357 return -ENODEV;
358 enabled = trace_probe_is_enabled(tp);
359
360 /* This also changes "enabled" state */
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900361 if (file) {
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900362 ret = trace_probe_add_file(tp, file);
Steven Rostedt (VMware)87107a22018-07-26 12:07:32 -0400363 if (ret)
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900364 return ret;
365 } else
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900366 trace_probe_set_flag(tp, TP_FLAG_PROFILE);
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900367
368 if (enabled)
369 return 0;
370
Jiri Olsae161c6b2021-11-25 21:28:52 +0100371 list_for_each_entry(tk, trace_probe_probe_list(tp), tp.list) {
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900372 if (trace_kprobe_has_gone(tk))
373 continue;
374 ret = __enable_trace_kprobe(tk);
Masami Hiramatsu44d00dc2019-09-18 17:55:37 +0900375 if (ret)
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900376 break;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900377 enabled = true;
378 }
379
Masami Hiramatsu44d00dc2019-09-18 17:55:37 +0900380 if (ret) {
381 /* Failed to enable one of them. Roll back all */
382 if (enabled)
383 __disable_trace_kprobe(tp);
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900384 if (file)
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900385 trace_probe_remove_file(tp, file);
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900386 else
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900387 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
Artem Savkov57ea2a32018-07-25 16:20:38 +0200388 }
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900389
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900390 return ret;
391}
392
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900393/*
394 * Disable trace_probe
395 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
396 */
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900397static int disable_trace_kprobe(struct trace_event_call *call,
398 struct trace_event_file *file)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900399{
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900400 struct trace_probe *tp;
401
402 tp = trace_probe_primary_from_call(call);
403 if (WARN_ON_ONCE(!tp))
404 return -ENODEV;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900405
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900406 if (file) {
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900407 if (!trace_probe_get_file_link(tp, file))
408 return -ENOENT;
409 if (!trace_probe_has_single_file(tp))
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200410 goto out;
Masami Hiramatsu747774d2019-06-01 00:17:37 +0900411 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900412 } else
Masami Hiramatsu747774d2019-06-01 00:17:37 +0900413 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +0900414
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900415 if (!trace_probe_is_enabled(tp))
416 __disable_trace_kprobe(tp);
Song Liue12f03d2017-12-06 14:45:15 -0800417
Oleg Nesterov3fe3d612013-06-20 19:38:09 +0200418 out:
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900419 if (file)
Masami Hiramatsua232e272013-07-09 18:35:26 +0900420 /*
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900421 * Synchronization is done in below function. For perf event,
422 * file == NULL and perf_trace_event_unreg() calls
423 * tracepoint_synchronize_unregister() to ensure synchronize
424 * event. We don't need to care about it.
Masami Hiramatsua232e272013-07-09 18:35:26 +0900425 */
Masami Hiramatsub5f935e2019-06-01 00:17:26 +0900426 trace_probe_remove_file(tp, file);
Masami Hiramatsua232e272013-07-09 18:35:26 +0900427
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900428 return 0;
Masami Hiramatsu1538f882011-06-27 16:26:44 +0900429}
430
Masami Hiramatsu7bb83f6f2021-01-08 13:19:38 +0900431#if defined(CONFIG_DYNAMIC_FTRACE) && \
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900432 !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
Masami Hiramatsuc7411a12019-10-29 17:31:44 +0900433static bool __within_notrace_func(unsigned long addr)
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900434{
Masami Hiramatsuc7411a12019-10-29 17:31:44 +0900435 unsigned long offset, size;
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900436
Masami Hiramatsu6bc6c772018-08-02 16:50:48 +0900437 if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
438 return false;
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900439
Masami Hiramatsu9161a862018-08-21 22:04:57 +0900440 /* Get the entry address of the target function */
441 addr -= offset;
442
443 /*
444 * Since ftrace_location_range() does inclusive range check, we need
445 * to subtract 1 byte from the end address.
446 */
447 return !ftrace_location_range(addr, addr + size - 1);
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900448}
Masami Hiramatsuc7411a12019-10-29 17:31:44 +0900449
450static bool within_notrace_func(struct trace_kprobe *tk)
451{
Masami Hiramatsudcbd21c2020-04-25 14:49:09 +0900452 unsigned long addr = trace_kprobe_address(tk);
Masami Hiramatsuc7411a12019-10-29 17:31:44 +0900453 char symname[KSYM_NAME_LEN], *p;
454
455 if (!__within_notrace_func(addr))
456 return false;
457
458 /* Check if the address is on a suffixed-symbol */
459 if (!lookup_symbol_name(addr, symname)) {
460 p = strchr(symname, '.');
461 if (!p)
462 return true;
463 *p = '\0';
464 addr = (unsigned long)kprobe_lookup_name(symname, 0);
465 if (addr)
466 return __within_notrace_func(addr);
467 }
468
469 return true;
470}
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900471#else
472#define within_notrace_func(tk) (false)
473#endif
474
Masami Hiramatsu61424312011-06-27 16:26:56 +0900475/* Internal register function - just handle k*probes and flags */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900476static int __register_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900477{
Masami Hiramatsua6682812018-08-29 01:18:43 +0900478 int i, ret;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900479
David Howellsa94549d2019-08-19 17:17:58 -0700480 ret = security_locked_down(LOCKDOWN_KPROBES);
481 if (ret)
482 return ret;
483
Masami Hiramatsu715fa2f2019-06-01 00:18:07 +0900484 if (trace_kprobe_is_registered(tk))
Masami Hiramatsu61424312011-06-27 16:26:56 +0900485 return -EINVAL;
486
Masami Hiramatsu45408c42018-07-30 19:20:14 +0900487 if (within_notrace_func(tk)) {
488 pr_warn("Could not probe notrace function %s\n",
489 trace_kprobe_symbol(tk));
490 return -EINVAL;
491 }
492
Masami Hiramatsua6682812018-08-29 01:18:43 +0900493 for (i = 0; i < tk->tp.nr_args; i++) {
494 ret = traceprobe_update_arg(&tk->tp.args[i]);
495 if (ret)
496 return ret;
497 }
498
Masami Hiramatsu61424312011-06-27 16:26:56 +0900499 /* Set/clear disabled flag according to tp->flag */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900500 if (trace_probe_is_enabled(&tk->tp))
501 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900502 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900503 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900504
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900505 if (trace_kprobe_is_return(tk))
506 ret = register_kretprobe(&tk->rp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900507 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900508 ret = register_kprobe(&tk->rp.kp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900509
Masami Hiramatsu61424312011-06-27 16:26:56 +0900510 return ret;
511}
512
513/* Internal unregister function - just handle k*probes and flags */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900514static void __unregister_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu61424312011-06-27 16:26:56 +0900515{
Masami Hiramatsu715fa2f2019-06-01 00:18:07 +0900516 if (trace_kprobe_is_registered(tk)) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900517 if (trace_kprobe_is_return(tk))
518 unregister_kretprobe(&tk->rp);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900519 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900520 unregister_kprobe(&tk->rp.kp);
Masami Hiramatsu715fa2f2019-06-01 00:18:07 +0900521 /* Cleanup kprobe for reuse and mark it unregistered */
522 INIT_HLIST_NODE(&tk->rp.kp.hlist);
523 INIT_LIST_HEAD(&tk->rp.kp.list);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900524 if (tk->rp.kp.symbol_name)
525 tk->rp.kp.addr = NULL;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900526 }
527}
528
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900529/* Unregister a trace_probe and probe_event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900530static int unregister_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400531{
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900532 /* If other probes are on the event, just unregister kprobe */
533 if (trace_probe_has_sibling(&tk->tp))
534 goto unreg;
535
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900536 /* Enabled event can not be unregistered */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900537 if (trace_probe_is_enabled(&tk->tp))
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900538 return -EBUSY;
539
Steven Rostedt (VMware)1d185382021-08-16 23:42:57 -0400540 /* If there's a reference to the dynamic event */
541 if (trace_event_dyn_busy(trace_probe_event_call(&tk->tp)))
542 return -EBUSY;
543
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400544 /* Will fail if probe is being used by ftrace or perf */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900545 if (unregister_kprobe_event(tk))
Steven Rostedt (Red Hat)40c32592013-07-03 23:33:50 -0400546 return -EBUSY;
547
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900548unreg:
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900549 __unregister_trace_kprobe(tk);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900550 dyn_event_remove(&tk->devent);
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900551 trace_probe_unlink(&tk->tp);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900552
553 return 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400554}
555
Masami Hiramatsufe60b0c2019-09-18 17:55:46 +0900556static bool trace_kprobe_has_same_kprobe(struct trace_kprobe *orig,
557 struct trace_kprobe *comp)
558{
559 struct trace_probe_event *tpe = orig->tp.event;
Masami Hiramatsufe60b0c2019-09-18 17:55:46 +0900560 int i;
561
Jiri Olsae161c6b2021-11-25 21:28:52 +0100562 list_for_each_entry(orig, &tpe->probes, tp.list) {
Masami Hiramatsufe60b0c2019-09-18 17:55:46 +0900563 if (strcmp(trace_kprobe_symbol(orig),
564 trace_kprobe_symbol(comp)) ||
565 trace_kprobe_offset(orig) != trace_kprobe_offset(comp))
566 continue;
567
568 /*
569 * trace_probe_compare_arg_type() ensured that nr_args and
570 * each argument name and type are same. Let's compare comm.
571 */
572 for (i = 0; i < orig->tp.nr_args; i++) {
573 if (strcmp(orig->tp.args[i].comm,
574 comp->tp.args[i].comm))
Srikar Dronamrajuf8d7ab22019-09-24 17:19:06 +0530575 break;
Masami Hiramatsufe60b0c2019-09-18 17:55:46 +0900576 }
577
Srikar Dronamrajuf8d7ab22019-09-24 17:19:06 +0530578 if (i == orig->tp.nr_args)
579 return true;
Masami Hiramatsufe60b0c2019-09-18 17:55:46 +0900580 }
581
582 return false;
583}
584
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900585static int append_trace_kprobe(struct trace_kprobe *tk, struct trace_kprobe *to)
586{
587 int ret;
588
Masami Hiramatsufe60b0c2019-09-18 17:55:46 +0900589 ret = trace_probe_compare_arg_type(&tk->tp, &to->tp);
590 if (ret) {
591 /* Note that argument starts index = 2 */
592 trace_probe_log_set_index(ret + 1);
593 trace_probe_log_err(0, DIFF_ARG_TYPE);
594 return -EEXIST;
595 }
596 if (trace_kprobe_has_same_kprobe(to, tk)) {
597 trace_probe_log_set_index(0);
598 trace_probe_log_err(0, SAME_PROBE);
599 return -EEXIST;
600 }
601
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900602 /* Append to existing event */
603 ret = trace_probe_append(&tk->tp, &to->tp);
604 if (ret)
605 return ret;
606
607 /* Register k*probe */
608 ret = __register_trace_kprobe(tk);
609 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
610 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
611 ret = 0;
612 }
613
614 if (ret)
615 trace_probe_unlink(&tk->tp);
616 else
Steven Rostedt (VMware)8b0e6c72021-08-16 23:42:56 -0400617 dyn_event_add(&tk->devent, trace_probe_event_call(&tk->tp));
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900618
619 return ret;
620}
621
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400622/* Register a trace_probe and probe_event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900623static int register_trace_kprobe(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400624{
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900625 struct trace_kprobe *old_tk;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400626 int ret;
627
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900628 mutex_lock(&event_mutex);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400629
Masami Hiramatsub55ce2032019-06-01 00:17:47 +0900630 old_tk = find_trace_kprobe(trace_probe_name(&tk->tp),
631 trace_probe_group_name(&tk->tp));
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900632 if (old_tk) {
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900633 if (trace_kprobe_is_return(tk) != trace_kprobe_is_return(old_tk)) {
634 trace_probe_log_set_index(0);
635 trace_probe_log_err(0, DIFF_PROBE_TYPE);
636 ret = -EEXIST;
637 } else {
Masami Hiramatsufe60b0c2019-09-18 17:55:46 +0900638 ret = append_trace_kprobe(tk, old_tk);
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900639 }
640 goto end;
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400641 }
Masami Hiramatsu61424312011-06-27 16:26:56 +0900642
643 /* Register new event */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900644 ret = register_kprobe_event(tk);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400645 if (ret) {
Masami Hiramatsu8e242062021-08-19 19:26:02 +0900646 if (ret == -EEXIST) {
647 trace_probe_log_set_index(0);
648 trace_probe_log_err(0, EVENT_EXIST);
649 } else
650 pr_warn("Failed to register probe event(%d)\n", ret);
Masami Hiramatsu2d5e0672009-09-14 16:48:56 -0400651 goto end;
652 }
653
Masami Hiramatsu61424312011-06-27 16:26:56 +0900654 /* Register k*probe */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900655 ret = __register_trace_kprobe(tk);
Masami Hiramatsu59158ec2018-08-29 01:18:15 +0900656 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
657 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
658 ret = 0;
659 }
660
Masami Hiramatsu61424312011-06-27 16:26:56 +0900661 if (ret < 0)
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900662 unregister_kprobe_event(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900663 else
Steven Rostedt (VMware)8b0e6c72021-08-16 23:42:56 -0400664 dyn_event_add(&tk->devent, trace_probe_event_call(&tk->tp));
Masami Hiramatsu61424312011-06-27 16:26:56 +0900665
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400666end:
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900667 mutex_unlock(&event_mutex);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400668 return ret;
669}
670
Masami Hiramatsu61424312011-06-27 16:26:56 +0900671/* Module notifier call back, checking event on the module */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900672static int trace_kprobe_module_callback(struct notifier_block *nb,
Masami Hiramatsu61424312011-06-27 16:26:56 +0900673 unsigned long val, void *data)
674{
675 struct module *mod = data;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900676 struct dyn_event *pos;
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900677 struct trace_kprobe *tk;
Masami Hiramatsu61424312011-06-27 16:26:56 +0900678 int ret;
679
680 if (val != MODULE_STATE_COMING)
681 return NOTIFY_DONE;
682
683 /* Update probes on coming module */
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900684 mutex_lock(&event_mutex);
685 for_each_trace_kprobe(tk, pos) {
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900686 if (trace_kprobe_within_module(tk, mod)) {
Masami Hiramatsu02ca1522011-10-04 19:44:38 +0900687 /* Don't need to check busy - this should have gone. */
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900688 __unregister_trace_kprobe(tk);
689 ret = __register_trace_kprobe(tk);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900690 if (ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700691 pr_warn("Failed to re-register probe %s on %s: %d\n",
Masami Hiramatsub55ce2032019-06-01 00:17:47 +0900692 trace_probe_name(&tk->tp),
Jarkko Sakkinenf3d36422020-08-18 08:08:57 +0300693 module_name(mod), ret);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900694 }
695 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900696 mutex_unlock(&event_mutex);
Masami Hiramatsu61424312011-06-27 16:26:56 +0900697
698 return NOTIFY_DONE;
699}
700
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900701static struct notifier_block trace_kprobe_module_nb = {
702 .notifier_call = trace_kprobe_module_callback,
Masami Hiramatsu61424312011-06-27 16:26:56 +0900703 .priority = 1 /* Invoked after kprobe module callback */
704};
705
Masami Hiramatsud2622712021-02-01 13:48:11 -0600706static int __trace_kprobe_create(int argc, const char *argv[])
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400707{
708 /*
709 * Argument syntax:
Alban Crequy696ced42017-04-03 12:36:22 +0200710 * - Add kprobe:
711 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
712 * - Add kretprobe:
713 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
Masami Hiramatsu4725cd82020-09-10 17:55:35 +0900714 * Or
715 * p:[GRP/]EVENT] [MOD:]KSYM[+0]%return [FETCHARGS]
716 *
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400717 * Fetch args:
Masami Hiramatsu2e06ff62009-10-07 18:27:59 -0400718 * $retval : fetch return value
719 * $stack : fetch stack address
720 * $stackN : fetch Nth of stack (N:0-)
Omar Sandoval35abb672016-06-08 18:38:02 -0700721 * $comm : fetch current task comm
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400722 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
723 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
724 * %REG : fetch register REG
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400725 * Dereferencing memory fetch:
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400726 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
Masami Hiramatsueca0d912009-09-10 19:53:38 -0400727 * Alias name of args:
728 * NAME=FETCHARG : set NAME as alias of FETCHARG.
Masami Hiramatsu93ccae72010-04-12 13:17:08 -0400729 * Type of args:
730 * FETCHARG:TYPE : use TYPE instead of unsigned long.
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400731 */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500732 struct trace_kprobe *tk = NULL;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900733 int i, len, ret = 0;
734 bool is_return = false;
735 char *symbol = NULL, *tmp = NULL;
736 const char *event = NULL, *group = KPROBE_EVENT_SYSTEM;
Steven Rostedt (VMware)007517a2021-08-19 00:13:27 -0400737 enum probe_print_type ptype;
Alban Crequy696ced42017-04-03 12:36:22 +0200738 int maxactive = 0;
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900739 long offset = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400740 void *addr = NULL;
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200741 char buf[MAX_EVENT_NAME_LEN];
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900742 unsigned int flags = TPARG_FL_KERNEL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400743
Andrea Righi8b05a3a2019-01-11 07:01:13 +0100744 switch (argv[0][0]) {
745 case 'r':
Srikar Dronamraju3a6b7662012-04-09 14:41:33 +0530746 is_return = true;
Andrea Righi8b05a3a2019-01-11 07:01:13 +0100747 break;
748 case 'p':
749 break;
750 default:
751 return -ECANCELED;
752 }
753 if (argc < 2)
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900754 return -ECANCELED;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400755
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500756 trace_probe_log_init("trace_kprobe", argc, argv);
757
Alban Crequy696ced42017-04-03 12:36:22 +0200758 event = strchr(&argv[0][1], ':');
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900759 if (event)
Alban Crequy696ced42017-04-03 12:36:22 +0200760 event++;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900761
Masami Hiramatsu287c0382019-03-14 13:30:09 +0900762 if (isdigit(argv[0][1])) {
763 if (!is_return) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500764 trace_probe_log_err(1, MAXACT_NO_KPROBE);
765 goto parse_error;
Masami Hiramatsu287c0382019-03-14 13:30:09 +0900766 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900767 if (event)
768 len = event - &argv[0][1] - 1;
769 else
770 len = strlen(&argv[0][1]);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500771 if (len > MAX_EVENT_NAME_LEN - 1) {
772 trace_probe_log_err(1, BAD_MAXACT);
773 goto parse_error;
774 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900775 memcpy(buf, &argv[0][1], len);
776 buf[len] = '\0';
777 ret = kstrtouint(buf, 0, &maxactive);
Masami Hiramatsu287c0382019-03-14 13:30:09 +0900778 if (ret || !maxactive) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500779 trace_probe_log_err(1, BAD_MAXACT);
780 goto parse_error;
Alban Crequy696ced42017-04-03 12:36:22 +0200781 }
782 /* kretprobes instances are iterated over via a list. The
783 * maximum should stay reasonable.
784 */
785 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500786 trace_probe_log_err(1, MAXACT_TOO_BIG);
787 goto parse_error;
Alban Crequy696ced42017-04-03 12:36:22 +0200788 }
789 }
790
Sabrina Dubroca9e52b322017-06-22 11:24:42 +0200791 /* try to parse an address. if that fails, try to read the
792 * input as a symbol. */
793 if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500794 trace_probe_log_set_index(1);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900795 /* Check whether uprobe event specified */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500796 if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
797 ret = -ECANCELED;
798 goto error;
799 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400800 /* a symbol specified */
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900801 symbol = kstrdup(argv[1], GFP_KERNEL);
802 if (!symbol)
803 return -ENOMEM;
Masami Hiramatsu4725cd82020-09-10 17:55:35 +0900804
805 tmp = strchr(symbol, '%');
806 if (tmp) {
807 if (!strcmp(tmp, "%return")) {
808 *tmp = '\0';
809 is_return = true;
810 } else {
811 trace_probe_log_err(tmp - symbol, BAD_ADDR_SUFFIX);
812 goto parse_error;
813 }
814 }
815
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400816 /* TODO: support .init module functions */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530817 ret = traceprobe_split_symbol_offset(symbol, &offset);
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900818 if (ret || offset < 0 || offset > UINT_MAX) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500819 trace_probe_log_err(0, BAD_PROBE_ADDR);
820 goto parse_error;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400821 }
Masami Hiramatsu4725cd82020-09-10 17:55:35 +0900822 if (is_return)
823 flags |= TPARG_FL_RETURN;
Masami Hiramatsu97c753e2021-01-28 00:37:51 +0900824 ret = kprobe_on_func_entry(NULL, symbol, offset);
825 if (ret == 0)
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900826 flags |= TPARG_FL_FENTRY;
Masami Hiramatsu97c753e2021-01-28 00:37:51 +0900827 /* Defer the ENOENT case until register kprobe */
828 if (ret == -EINVAL && is_return) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500829 trace_probe_log_err(0, BAD_RETPROBE);
830 goto parse_error;
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400831 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400832 }
833
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500834 trace_probe_log_set_index(0);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900835 if (event) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500836 ret = traceprobe_parse_event_name(&event, &group, buf,
837 event - argv[0]);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900838 if (ret)
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500839 goto parse_error;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900840 } else {
Masami Hiramatsu42635652009-08-13 16:35:26 -0400841 /* Make a new event name */
Masami Hiramatsu42635652009-08-13 16:35:26 -0400842 if (symbol)
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500843 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400844 is_return ? 'r' : 'p', symbol, offset);
845 else
Masami Hiramatsu6f3cf442009-12-16 17:24:08 -0500846 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
Masami Hiramatsu42635652009-08-13 16:35:26 -0400847 is_return ? 'r' : 'p', addr);
Naveen N. Raofca18a42017-07-08 00:27:30 +0530848 sanitize_event_name(buf);
Masami Hiramatsu4a846b42009-09-11 05:31:21 +0200849 event = buf;
850 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900851
852 /* setup a probe */
Alban Crequy696ced42017-04-03 12:36:22 +0200853 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500854 argc - 2, is_return);
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900855 if (IS_ERR(tk)) {
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900856 ret = PTR_ERR(tk);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500857 /* This must return -ENOMEM, else there is a bug */
Masami Hiramatsua0394802019-03-14 13:30:50 +0900858 WARN_ON_ONCE(ret != -ENOMEM);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500859 goto out; /* We know tk is not allocated */
Masami Hiramatsue63cc232009-10-16 20:07:28 -0400860 }
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500861 argc -= 2; argv += 2;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400862
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400863 /* parse arguments */
Masami Hiramatsua82378d2009-08-13 16:35:18 -0400864 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500865 trace_probe_log_set_index(i + 2);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400866 ret = traceprobe_parse_probe_arg(&tk->tp, i, argv[i], flags);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900867 if (ret)
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500868 goto error; /* This can be -ENOMEM */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400869 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400870
Steven Rostedt (VMware)007517a2021-08-19 00:13:27 -0400871 ptype = is_return ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
872 ret = traceprobe_set_print_fmt(&tk->tp, ptype);
Masami Hiramatsuf730e0f2019-06-01 00:16:46 +0900873 if (ret < 0)
874 goto error;
875
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900876 ret = register_trace_kprobe(tk);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500877 if (ret) {
878 trace_probe_log_set_index(1);
879 if (ret == -EILSEQ)
880 trace_probe_log_err(0, BAD_INSN_BNDRY);
881 else if (ret == -ENOENT)
882 trace_probe_log_err(0, BAD_PROBE_ADDR);
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900883 else if (ret != -ENOMEM && ret != -EEXIST)
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500884 trace_probe_log_err(0, FAIL_REG_PROBE);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400885 goto error;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500886 }
887
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900888out:
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500889 trace_probe_log_clear();
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900890 kfree(symbol);
891 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400892
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500893parse_error:
894 ret = -EINVAL;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400895error:
Namhyung Kimc31ffb32013-07-03 13:50:51 +0900896 free_trace_kprobe(tk);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900897 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400898}
899
Masami Hiramatsud2622712021-02-01 13:48:11 -0600900static int trace_kprobe_create(const char *raw_command)
901{
902 return trace_probe_create(raw_command, __trace_kprobe_create);
903}
904
905static int create_or_delete_trace_kprobe(const char *raw_command)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400906{
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900907 int ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -0400908
Masami Hiramatsud2622712021-02-01 13:48:11 -0600909 if (raw_command[0] == '-')
910 return dyn_event_release(raw_command, &trace_kprobe_ops);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900911
Masami Hiramatsud2622712021-02-01 13:48:11 -0600912 ret = trace_kprobe_create(raw_command);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900913 return ret == -ECANCELED ? -EINVAL : ret;
914}
915
Tom Zanussi29a15482020-01-29 12:59:30 -0600916static int trace_kprobe_run_command(struct dynevent_cmd *cmd)
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600917{
Masami Hiramatsud2622712021-02-01 13:48:11 -0600918 return create_or_delete_trace_kprobe(cmd->seq.buffer);
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600919}
920
921/**
922 * kprobe_event_cmd_init - Initialize a kprobe event command object
923 * @cmd: A pointer to the dynevent_cmd struct representing the new event
924 * @buf: A pointer to the buffer used to build the command
925 * @maxlen: The length of the buffer passed in @buf
926 *
927 * Initialize a synthetic event command object. Use this before
928 * calling any of the other kprobe_event functions.
929 */
930void kprobe_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
931{
932 dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_KPROBE,
Tom Zanussi29a15482020-01-29 12:59:30 -0600933 trace_kprobe_run_command);
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600934}
935EXPORT_SYMBOL_GPL(kprobe_event_cmd_init);
936
937/**
938 * __kprobe_event_gen_cmd_start - Generate a kprobe event command from arg list
939 * @cmd: A pointer to the dynevent_cmd struct representing the new event
940 * @name: The name of the kprobe event
941 * @loc: The location of the kprobe event
942 * @kretprobe: Is this a return probe?
943 * @args: Variable number of arg (pairs), one pair for each field
944 *
945 * NOTE: Users normally won't want to call this function directly, but
946 * rather use the kprobe_event_gen_cmd_start() wrapper, which automatically
947 * adds a NULL to the end of the arg list. If this function is used
948 * directly, make sure the last arg in the variable arg list is NULL.
949 *
950 * Generate a kprobe event command to be executed by
951 * kprobe_event_gen_cmd_end(). This function can be used to generate the
952 * complete command or only the first part of it; in the latter case,
953 * kprobe_event_add_fields() can be used to add more fields following this.
954 *
Masami Hiramatsu5b4dcd22020-04-25 14:49:26 +0900955 * Unlikely the synth_event_gen_cmd_start(), @loc must be specified. This
956 * returns -EINVAL if @loc == NULL.
957 *
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600958 * Return: 0 if successful, error otherwise.
959 */
960int __kprobe_event_gen_cmd_start(struct dynevent_cmd *cmd, bool kretprobe,
961 const char *name, const char *loc, ...)
962{
963 char buf[MAX_EVENT_NAME_LEN];
964 struct dynevent_arg arg;
965 va_list args;
966 int ret;
967
968 if (cmd->type != DYNEVENT_TYPE_KPROBE)
969 return -EINVAL;
970
Masami Hiramatsu5b4dcd22020-04-25 14:49:26 +0900971 if (!loc)
972 return -EINVAL;
973
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600974 if (kretprobe)
975 snprintf(buf, MAX_EVENT_NAME_LEN, "r:kprobes/%s", name);
976 else
977 snprintf(buf, MAX_EVENT_NAME_LEN, "p:kprobes/%s", name);
978
979 ret = dynevent_str_add(cmd, buf);
980 if (ret)
981 return ret;
982
Tom Zanussi74403b62020-01-31 15:55:32 -0600983 dynevent_arg_init(&arg, 0);
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600984 arg.str = loc;
Tom Zanussi74403b62020-01-31 15:55:32 -0600985 ret = dynevent_arg_add(cmd, &arg, NULL);
Tom Zanussi2a588dd2020-01-29 12:59:29 -0600986 if (ret)
987 return ret;
988
989 va_start(args, loc);
990 for (;;) {
991 const char *field;
992
993 field = va_arg(args, const char *);
994 if (!field)
995 break;
996
997 if (++cmd->n_fields > MAX_TRACE_ARGS) {
998 ret = -EINVAL;
999 break;
1000 }
1001
1002 arg.str = field;
Tom Zanussi74403b62020-01-31 15:55:32 -06001003 ret = dynevent_arg_add(cmd, &arg, NULL);
Tom Zanussi2a588dd2020-01-29 12:59:29 -06001004 if (ret)
1005 break;
1006 }
1007 va_end(args);
1008
1009 return ret;
1010}
1011EXPORT_SYMBOL_GPL(__kprobe_event_gen_cmd_start);
1012
1013/**
1014 * __kprobe_event_add_fields - Add probe fields to a kprobe command from arg list
1015 * @cmd: A pointer to the dynevent_cmd struct representing the new event
1016 * @args: Variable number of arg (pairs), one pair for each field
1017 *
1018 * NOTE: Users normally won't want to call this function directly, but
1019 * rather use the kprobe_event_add_fields() wrapper, which
1020 * automatically adds a NULL to the end of the arg list. If this
1021 * function is used directly, make sure the last arg in the variable
1022 * arg list is NULL.
1023 *
1024 * Add probe fields to an existing kprobe command using a variable
1025 * list of args. Fields are added in the same order they're listed.
1026 *
1027 * Return: 0 if successful, error otherwise.
1028 */
1029int __kprobe_event_add_fields(struct dynevent_cmd *cmd, ...)
1030{
1031 struct dynevent_arg arg;
1032 va_list args;
Gustavo A. R. Silva10f129c2020-02-05 16:34:04 -06001033 int ret = 0;
Tom Zanussi2a588dd2020-01-29 12:59:29 -06001034
1035 if (cmd->type != DYNEVENT_TYPE_KPROBE)
1036 return -EINVAL;
1037
Tom Zanussi74403b62020-01-31 15:55:32 -06001038 dynevent_arg_init(&arg, 0);
Tom Zanussi2a588dd2020-01-29 12:59:29 -06001039
1040 va_start(args, cmd);
1041 for (;;) {
1042 const char *field;
1043
1044 field = va_arg(args, const char *);
1045 if (!field)
1046 break;
1047
1048 if (++cmd->n_fields > MAX_TRACE_ARGS) {
1049 ret = -EINVAL;
1050 break;
1051 }
1052
1053 arg.str = field;
Tom Zanussi74403b62020-01-31 15:55:32 -06001054 ret = dynevent_arg_add(cmd, &arg, NULL);
Tom Zanussi2a588dd2020-01-29 12:59:29 -06001055 if (ret)
1056 break;
1057 }
1058 va_end(args);
1059
1060 return ret;
1061}
1062EXPORT_SYMBOL_GPL(__kprobe_event_add_fields);
1063
1064/**
1065 * kprobe_event_delete - Delete a kprobe event
1066 * @name: The name of the kprobe event to delete
1067 *
1068 * Delete a kprobe event with the give @name from kernel code rather
1069 * than directly from the command line.
1070 *
1071 * Return: 0 if successful, error otherwise.
1072 */
1073int kprobe_event_delete(const char *name)
1074{
1075 char buf[MAX_EVENT_NAME_LEN];
1076
1077 snprintf(buf, MAX_EVENT_NAME_LEN, "-:%s", name);
1078
Masami Hiramatsud2622712021-02-01 13:48:11 -06001079 return create_or_delete_trace_kprobe(buf);
Tom Zanussi2a588dd2020-01-29 12:59:29 -06001080}
1081EXPORT_SYMBOL_GPL(kprobe_event_delete);
1082
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001083static int trace_kprobe_release(struct dyn_event *ev)
1084{
1085 struct trace_kprobe *tk = to_trace_kprobe(ev);
1086 int ret = unregister_trace_kprobe(tk);
1087
1088 if (!ret)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001089 free_trace_kprobe(tk);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001090 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001091}
1092
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001093static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001094{
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001095 struct trace_kprobe *tk = to_trace_kprobe(ev);
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001096 int i;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001097
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001098 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
Masami Hiramatsu6a13a0d2020-03-24 16:34:48 +09001099 if (trace_kprobe_is_return(tk) && tk->rp.maxactive)
1100 seq_printf(m, "%d", tk->rp.maxactive);
Masami Hiramatsub55ce2032019-06-01 00:17:47 +09001101 seq_printf(m, ":%s/%s", trace_probe_group_name(&tk->tp),
1102 trace_probe_name(&tk->tp));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001103
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001104 if (!tk->symbol)
1105 seq_printf(m, " 0x%p", tk->rp.kp.addr);
1106 else if (tk->rp.kp.offset)
1107 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
1108 tk->rp.kp.offset);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001109 else
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001110 seq_printf(m, " %s", trace_kprobe_symbol(tk));
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001111
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001112 for (i = 0; i < tk->tp.nr_args; i++)
1113 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001114 seq_putc(m, '\n');
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001115
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001116 return 0;
1117}
1118
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001119static int probes_seq_show(struct seq_file *m, void *v)
1120{
1121 struct dyn_event *ev = v;
1122
1123 if (!is_trace_kprobe(ev))
1124 return 0;
1125
1126 return trace_kprobe_show(m, ev);
1127}
1128
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001129static const struct seq_operations probes_seq_op = {
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001130 .start = dyn_event_seq_start,
1131 .next = dyn_event_seq_next,
1132 .stop = dyn_event_seq_stop,
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001133 .show = probes_seq_show
1134};
1135
1136static int probes_open(struct inode *inode, struct file *file)
1137{
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001138 int ret;
1139
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04001140 ret = security_locked_down(LOCKDOWN_TRACEFS);
1141 if (ret)
1142 return ret;
1143
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001144 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001145 ret = dyn_events_release_all(&trace_kprobe_ops);
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09001146 if (ret < 0)
1147 return ret;
1148 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001149
1150 return seq_open(file, &probes_seq_op);
1151}
1152
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001153static ssize_t probes_write(struct file *file, const char __user *buffer,
1154 size_t count, loff_t *ppos)
1155{
Tom Zanussi7e465ba2017-09-22 14:58:20 -05001156 return trace_parse_run_command(file, buffer, count, ppos,
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001157 create_or_delete_trace_kprobe);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001158}
1159
1160static const struct file_operations kprobe_events_ops = {
1161 .owner = THIS_MODULE,
1162 .open = probes_open,
1163 .read = seq_read,
1164 .llseek = seq_lseek,
1165 .release = seq_release,
1166 .write = probes_write,
1167};
1168
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001169/* Probes profiling interfaces */
1170static int probes_profile_seq_show(struct seq_file *m, void *v)
1171{
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001172 struct dyn_event *ev = v;
1173 struct trace_kprobe *tk;
Xiangyang Zhangdfea08a2022-01-07 23:02:42 +08001174 unsigned long nmissed;
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001175
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001176 if (!is_trace_kprobe(ev))
1177 return 0;
1178
1179 tk = to_trace_kprobe(ev);
Xiangyang Zhangdfea08a2022-01-07 23:02:42 +08001180 nmissed = trace_kprobe_is_return(tk) ?
1181 tk->rp.kp.nmissed + tk->rp.nmissed : tk->rp.kp.nmissed;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001182 seq_printf(m, " %-44s %15lu %15lu\n",
Masami Hiramatsub55ce2032019-06-01 00:17:47 +09001183 trace_probe_name(&tk->tp),
Marcin Nowakowskif18f97a2016-12-09 15:19:37 +01001184 trace_kprobe_nhit(tk),
Xiangyang Zhangdfea08a2022-01-07 23:02:42 +08001185 nmissed);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001186
1187 return 0;
1188}
1189
1190static const struct seq_operations profile_seq_op = {
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001191 .start = dyn_event_seq_start,
1192 .next = dyn_event_seq_next,
1193 .stop = dyn_event_seq_stop,
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001194 .show = probes_profile_seq_show
1195};
1196
1197static int profile_open(struct inode *inode, struct file *file)
1198{
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04001199 int ret;
1200
1201 ret = security_locked_down(LOCKDOWN_TRACEFS);
1202 if (ret)
1203 return ret;
1204
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001205 return seq_open(file, &profile_seq_op);
1206}
1207
1208static const struct file_operations kprobe_profile_ops = {
1209 .owner = THIS_MODULE,
1210 .open = profile_open,
1211 .read = seq_read,
1212 .llseek = seq_lseek,
1213 .release = seq_release,
1214};
1215
Masami Hiramatsu53305922018-04-25 21:18:03 +09001216/* Kprobe specific fetch functions */
1217
1218/* Return the length of string -- including null terminal byte */
Masami Hiramatsu91784122018-04-25 21:19:01 +09001219static nokprobe_inline int
Masami Hiramatsu88903c42019-05-15 14:38:30 +09001220fetch_store_strlen_user(unsigned long addr)
1221{
1222 const void __user *uaddr = (__force const void __user *)addr;
1223
Christoph Hellwig02dddb12020-06-08 21:34:20 -07001224 return strnlen_user_nofault(uaddr, MAX_STRING_SIZE);
Masami Hiramatsu88903c42019-05-15 14:38:30 +09001225}
1226
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001227/* Return the length of string -- including null terminal byte */
Masami Hiramatsu91784122018-04-25 21:19:01 +09001228static nokprobe_inline int
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001229fetch_store_strlen(unsigned long addr)
Masami Hiramatsu53305922018-04-25 21:18:03 +09001230{
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001231 int ret, len = 0;
1232 u8 c;
Masami Hiramatsu53305922018-04-25 21:18:03 +09001233
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001234#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1235 if (addr < TASK_SIZE)
1236 return fetch_store_strlen_user(addr);
1237#endif
Masami Hiramatsu88903c42019-05-15 14:38:30 +09001238
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001239 do {
Christoph Hellwigfe557312020-06-17 09:37:53 +02001240 ret = copy_from_kernel_nofault(&c, (u8 *)addr + len, 1);
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001241 len++;
1242 } while (c && ret == 0 && len < MAX_STRING_SIZE);
Masami Hiramatsu88903c42019-05-15 14:38:30 +09001243
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001244 return (ret < 0) ? ret : len;
Masami Hiramatsu88903c42019-05-15 14:38:30 +09001245}
1246
1247/*
1248 * Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf
1249 * with max length and relative data location.
1250 */
1251static nokprobe_inline int
1252fetch_store_string_user(unsigned long addr, void *dest, void *base)
1253{
1254 const void __user *uaddr = (__force const void __user *)addr;
1255 int maxlen = get_loc_len(*(u32 *)dest);
1256 void *__dest;
1257 long ret;
1258
1259 if (unlikely(!maxlen))
1260 return -ENOMEM;
1261
1262 __dest = get_loc_data(dest, base);
1263
Christoph Hellwigbd88bb52020-06-08 21:34:14 -07001264 ret = strncpy_from_user_nofault(__dest, uaddr, maxlen);
Masami Hiramatsu88903c42019-05-15 14:38:30 +09001265 if (ret >= 0)
1266 *(u32 *)dest = make_data_loc(ret, __dest - base);
1267
Masami Hiramatsu91784122018-04-25 21:19:01 +09001268 return ret;
Masami Hiramatsu53305922018-04-25 21:18:03 +09001269}
1270
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001271/*
1272 * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
1273 * length and relative data location.
1274 */
Masami Hiramatsu9b960a32018-04-25 21:19:59 +09001275static nokprobe_inline int
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001276fetch_store_string(unsigned long addr, void *dest, void *base)
Masami Hiramatsu9b960a32018-04-25 21:19:59 +09001277{
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001278 int maxlen = get_loc_len(*(u32 *)dest);
1279 void *__dest;
1280 long ret;
1281
1282#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1283 if ((unsigned long)addr < TASK_SIZE)
1284 return fetch_store_string_user(addr, dest, base);
1285#endif
1286
1287 if (unlikely(!maxlen))
1288 return -ENOMEM;
1289
1290 __dest = get_loc_data(dest, base);
1291
1292 /*
1293 * Try to get string again, since the string can be changed while
1294 * probing.
1295 */
1296 ret = strncpy_from_kernel_nofault(__dest, (void *)addr, maxlen);
1297 if (ret >= 0)
1298 *(u32 *)dest = make_data_loc(ret, __dest - base);
1299
1300 return ret;
Masami Hiramatsu9b960a32018-04-25 21:19:59 +09001301}
1302
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +09001303static nokprobe_inline int
1304probe_mem_read_user(void *dest, void *src, size_t size)
1305{
Masami Hiramatsu539b75b2019-05-22 17:27:52 +09001306 const void __user *uaddr = (__force const void __user *)src;
1307
Christoph Hellwigc0ee37e2020-06-17 09:37:54 +02001308 return copy_from_user_nofault(dest, uaddr, size);
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +09001309}
1310
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001311static nokprobe_inline int
1312probe_mem_read(void *dest, void *src, size_t size)
1313{
1314#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1315 if ((unsigned long)src < TASK_SIZE)
1316 return probe_mem_read_user(dest, src, size);
1317#endif
Christoph Hellwigfe557312020-06-17 09:37:53 +02001318 return copy_from_kernel_nofault(dest, src, size);
Christoph Hellwig9de1fec2020-06-08 21:34:44 -07001319}
1320
Masami Hiramatsu53305922018-04-25 21:18:03 +09001321/* Note that we don't verify it, since the code does not come from user space */
1322static int
Steven Rostedt (VMware)8565a452021-08-19 00:13:28 -04001323process_fetch_insn(struct fetch_insn *code, void *rec, void *dest,
Masami Hiramatsu91784122018-04-25 21:19:01 +09001324 void *base)
Masami Hiramatsu53305922018-04-25 21:18:03 +09001325{
Steven Rostedt (VMware)8565a452021-08-19 00:13:28 -04001326 struct pt_regs *regs = rec;
Masami Hiramatsu53305922018-04-25 21:18:03 +09001327 unsigned long val;
Masami Hiramatsu53305922018-04-25 21:18:03 +09001328
Masami Hiramatsua6682812018-08-29 01:18:43 +09001329retry:
Masami Hiramatsu53305922018-04-25 21:18:03 +09001330 /* 1st stage: get value from context */
1331 switch (code->op) {
1332 case FETCH_OP_REG:
1333 val = regs_get_register(regs, code->param);
1334 break;
1335 case FETCH_OP_STACK:
1336 val = regs_get_kernel_stack_nth(regs, code->param);
1337 break;
1338 case FETCH_OP_STACKP:
1339 val = kernel_stack_pointer(regs);
1340 break;
1341 case FETCH_OP_RETVAL:
1342 val = regs_return_value(regs);
1343 break;
1344 case FETCH_OP_IMM:
1345 val = code->immediate;
1346 break;
1347 case FETCH_OP_COMM:
1348 val = (unsigned long)current->comm;
1349 break;
Masami Hiramatsua42e3c42019-06-20 00:08:37 +09001350 case FETCH_OP_DATA:
1351 val = (unsigned long)code->data;
1352 break;
Masami Hiramatsua1303af2018-04-25 21:21:26 +09001353#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
1354 case FETCH_OP_ARG:
1355 val = regs_get_kernel_argument(regs, code->param);
1356 break;
1357#endif
Masami Hiramatsua6682812018-08-29 01:18:43 +09001358 case FETCH_NOP_SYMBOL: /* Ignore a place holder */
1359 code++;
1360 goto retry;
Masami Hiramatsu53305922018-04-25 21:18:03 +09001361 default:
1362 return -EILSEQ;
1363 }
1364 code++;
1365
Masami Hiramatsu9b960a32018-04-25 21:19:59 +09001366 return process_fetch_insn_bottom(code, val, dest, base);
Masami Hiramatsu53305922018-04-25 21:18:03 +09001367}
1368NOKPROBE_SYMBOL(process_fetch_insn)
1369
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001370/* Kprobe handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001371static nokprobe_inline void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001372__kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001373 struct trace_event_file *trace_file)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001374{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001375 struct kprobe_trace_entry_head *entry;
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001376 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001377 struct trace_event_buffer fbuffer;
1378 int dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001379
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001380 WARN_ON(call != trace_file->event_call);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001381
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -04001382 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -05001383 return;
Masami Hiramatsub8820082013-05-09 14:44:54 +09001384
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001385 dsize = __get_data_size(&tk->tp, regs);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001386
Steven Rostedt (VMware)5e6cd842021-11-29 21:39:49 -05001387 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
1388 sizeof(*entry) + tk->tp.size + dsize);
1389 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001390 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001391
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001392 fbuffer.regs = regs;
1393 entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001394 entry->ip = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsu91784122018-04-25 21:19:01 +09001395 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001396
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001397 trace_event_buffer_commit(&fbuffer);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001398}
1399
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001400static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001401kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001402{
Oleg Nesterovb04d52e2013-06-20 19:38:14 +02001403 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001404
Masami Hiramatsub5f935e2019-06-01 00:17:26 +09001405 trace_probe_for_each_link_rcu(link, &tk->tp)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001406 __kprobe_trace_func(tk, regs, link->file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001407}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001408NOKPROBE_SYMBOL(kprobe_trace_func);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001409
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001410/* Kretprobe handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001411static nokprobe_inline void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001412__kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001413 struct pt_regs *regs,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001414 struct trace_event_file *trace_file)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001415{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001416 struct kretprobe_trace_entry_head *entry;
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001417 struct trace_event_buffer fbuffer;
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001418 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001419 int dsize;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001420
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001421 WARN_ON(call != trace_file->event_call);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001422
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -04001423 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -05001424 return;
Masami Hiramatsub8820082013-05-09 14:44:54 +09001425
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001426 dsize = __get_data_size(&tk->tp, regs);
Steven Rostedt (VMware)5e6cd842021-11-29 21:39:49 -05001427
1428 entry = trace_event_buffer_reserve(&fbuffer, trace_file,
1429 sizeof(*entry) + tk->tp.size + dsize);
1430 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001431 return;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001432
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001433 fbuffer.regs = regs;
1434 entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001435 entry->func = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001436 entry->ret_ip = (unsigned long)ri->ret_addr;
Masami Hiramatsu91784122018-04-25 21:19:01 +09001437 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001438
Masami Hiramatsu8cfcf152020-01-11 01:05:31 +09001439 trace_event_buffer_commit(&fbuffer);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001440}
1441
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001442static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001443kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001444 struct pt_regs *regs)
1445{
Oleg Nesterovb04d52e2013-06-20 19:38:14 +02001446 struct event_file_link *link;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001447
Masami Hiramatsub5f935e2019-06-01 00:17:26 +09001448 trace_probe_for_each_link_rcu(link, &tk->tp)
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001449 __kretprobe_trace_func(tk, ri, regs, link->file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001450}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001451NOKPROBE_SYMBOL(kretprobe_trace_func);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001452
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001453/* Event entry printers */
Masami Hiramatsub62fdd92013-05-13 20:58:39 +09001454static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001455print_kprobe_event(struct trace_iterator *iter, int flags,
1456 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001457{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001458 struct kprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001459 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001460 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001461
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001462 field = (struct kprobe_trace_entry_head *)iter->ent;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001463 tp = trace_probe_primary_from_call(
1464 container_of(event, struct trace_event_call, event));
1465 if (WARN_ON_ONCE(!tp))
1466 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001467
Masami Hiramatsub55ce2032019-06-01 00:17:47 +09001468 trace_seq_printf(s, "%s: (", trace_probe_name(tp));
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001469
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001470 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001471 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001472
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001473 trace_seq_putc(s, ')');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001474
Masami Hiramatsu56de7632018-04-25 21:16:36 +09001475 if (print_probe_args(s, tp->args, tp->nr_args,
1476 (u8 *)&field[1], field) < 0)
1477 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001478
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001479 trace_seq_putc(s, '\n');
1480 out:
1481 return trace_handle_return(s);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001482}
1483
Masami Hiramatsub62fdd92013-05-13 20:58:39 +09001484static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001485print_kretprobe_event(struct trace_iterator *iter, int flags,
1486 struct trace_event *event)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001487{
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001488 struct kretprobe_trace_entry_head *field;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001489 struct trace_seq *s = &iter->seq;
Masami Hiramatsueca0d912009-09-10 19:53:38 -04001490 struct trace_probe *tp;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001491
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001492 field = (struct kretprobe_trace_entry_head *)iter->ent;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001493 tp = trace_probe_primary_from_call(
1494 container_of(event, struct trace_event_call, event));
1495 if (WARN_ON_ONCE(!tp))
1496 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001497
Masami Hiramatsub55ce2032019-06-01 00:17:47 +09001498 trace_seq_printf(s, "%s: (", trace_probe_name(tp));
Masami Hiramatsu6e9f23d2009-09-10 19:53:45 -04001499
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001500 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001501 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001502
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001503 trace_seq_puts(s, " <- ");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001504
1505 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001506 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001507
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001508 trace_seq_putc(s, ')');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001509
Masami Hiramatsu56de7632018-04-25 21:16:36 +09001510 if (print_probe_args(s, tp->args, tp->nr_args,
1511 (u8 *)&field[1], field) < 0)
1512 goto out;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001513
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001514 trace_seq_putc(s, '\n');
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001515
Steven Rostedt (Red Hat)85224da2014-11-12 15:18:16 -05001516 out:
1517 return trace_handle_return(s);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001518}
1519
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001520
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001521static int kprobe_event_define_fields(struct trace_event_call *event_call)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001522{
Masami Hiramatsueeb07b02018-04-25 21:17:05 +09001523 int ret;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001524 struct kprobe_trace_entry_head field;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001525 struct trace_probe *tp;
1526
1527 tp = trace_probe_primary_from_call(event_call);
1528 if (WARN_ON_ONCE(!tp))
1529 return -ENOENT;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001530
Masami Hiramatsua703d942009-10-07 18:28:07 -04001531 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001532
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001533 return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001534}
1535
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001536static int kretprobe_event_define_fields(struct trace_event_call *event_call)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001537{
Masami Hiramatsueeb07b02018-04-25 21:17:05 +09001538 int ret;
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001539 struct kretprobe_trace_entry_head field;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001540 struct trace_probe *tp;
1541
1542 tp = trace_probe_primary_from_call(event_call);
1543 if (WARN_ON_ONCE(!tp))
1544 return -ENOENT;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001545
Masami Hiramatsua703d942009-10-07 18:28:07 -04001546 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1547 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001548
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001549 return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001550}
1551
Li Zefan07b139c2009-12-21 14:27:35 +08001552#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001553
1554/* Kprobe profile handler */
Josef Bacik9802d862017-12-11 11:36:48 -05001555static int
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001556kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001557{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001558 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001559 struct kprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001560 struct hlist_head *head;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001561 int size, __size, dsize;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001562 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001563
Josef Bacik9802d862017-12-11 11:36:48 -05001564 if (bpf_prog_array_valid(call)) {
Masami Hiramatsu66665ad2018-01-13 02:54:33 +09001565 unsigned long orig_ip = instruction_pointer(regs);
Josef Bacik9802d862017-12-11 11:36:48 -05001566 int ret;
1567
1568 ret = trace_call_bpf(call, regs);
1569
1570 /*
1571 * We need to check and see if we modified the pc of the
Masami Hiramatsucce188b2018-06-20 01:15:45 +09001572 * pt_regs, and if so return 1 so that we don't do the
1573 * single stepping.
Josef Bacik9802d862017-12-11 11:36:48 -05001574 */
Masami Hiramatsucce188b2018-06-20 01:15:45 +09001575 if (orig_ip != instruction_pointer(regs))
Josef Bacik9802d862017-12-11 11:36:48 -05001576 return 1;
Josef Bacik9802d862017-12-11 11:36:48 -05001577 if (!ret)
1578 return 0;
1579 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07001580
Oleg Nesterov288e9842013-06-20 19:38:06 +02001581 head = this_cpu_ptr(call->perf_events);
1582 if (hlist_empty(head))
Josef Bacik9802d862017-12-11 11:36:48 -05001583 return 0;
Oleg Nesterov288e9842013-06-20 19:38:06 +02001584
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001585 dsize = __get_data_size(&tk->tp, regs);
1586 __size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001587 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1588 size -= sizeof(u32);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001589
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001590 entry = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001591 if (!entry)
Josef Bacik9802d862017-12-11 11:36:48 -05001592 return 0;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001593
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001594 entry->ip = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001595 memset(&entry[1], 0, dsize);
Masami Hiramatsu91784122018-04-25 21:19:01 +09001596 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001597 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02001598 head, NULL);
Josef Bacik9802d862017-12-11 11:36:48 -05001599 return 0;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001600}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001601NOKPROBE_SYMBOL(kprobe_perf_func);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001602
1603/* Kretprobe profile handler */
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001604static void
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001605kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
Masami Hiramatsu2b106aa2013-05-09 14:44:41 +09001606 struct pt_regs *regs)
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001607{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001608 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
Masami Hiramatsu93ccae72010-04-12 13:17:08 -04001609 struct kretprobe_trace_entry_head *entry;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02001610 struct hlist_head *head;
Masami Hiramatsue09c8612010-07-05 15:54:45 -03001611 int size, __size, dsize;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01001612 int rctx;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001613
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001614 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
Alexei Starovoitov25415172015-03-25 12:49:20 -07001615 return;
1616
Oleg Nesterov288e9842013-06-20 19:38:06 +02001617 head = this_cpu_ptr(call->perf_events);
1618 if (hlist_empty(head))
1619 return;
1620
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001621 dsize = __get_data_size(&tk->tp, regs);
1622 __size = sizeof(*entry) + tk->tp.size + dsize;
Masami Hiramatsu74ebb632009-09-14 16:49:28 -04001623 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1624 size -= sizeof(u32);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001625
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001626 entry = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08001627 if (!entry)
Xiao Guangrong1e12a4a2010-01-28 09:34:27 +08001628 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01001629
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001630 entry->func = (unsigned long)tk->rp.kp.addr;
Masami Hiramatsua1a138d2009-09-25 11:20:12 -07001631 entry->ret_ip = (unsigned long)ri->ret_addr;
Masami Hiramatsu91784122018-04-25 21:19:01 +09001632 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001633 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02001634 head, NULL);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001635}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001636NOKPROBE_SYMBOL(kretprobe_perf_func);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07001637
1638int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1639 const char **symbol, u64 *probe_offset,
1640 u64 *probe_addr, bool perf_type_tracepoint)
1641{
1642 const char *pevent = trace_event_name(event->tp_event);
1643 const char *group = event->tp_event->class->system;
1644 struct trace_kprobe *tk;
1645
1646 if (perf_type_tracepoint)
1647 tk = find_trace_kprobe(pevent, group);
1648 else
Jean-Philippe Brucker22d5bd62020-06-08 14:45:32 +02001649 tk = trace_kprobe_primary_from_call(event->tp_event);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07001650 if (!tk)
1651 return -EINVAL;
1652
1653 *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1654 : BPF_FD_TYPE_KPROBE;
1655 if (tk->symbol) {
1656 *symbol = tk->symbol;
1657 *probe_offset = tk->rp.kp.offset;
1658 *probe_addr = 0;
1659 } else {
1660 *symbol = NULL;
1661 *probe_offset = 0;
1662 *probe_addr = (unsigned long)tk->rp.kp.addr;
1663 }
1664 return 0;
1665}
Li Zefan07b139c2009-12-21 14:27:35 +08001666#endif /* CONFIG_PERF_EVENTS */
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001667
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001668/*
1669 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1670 *
1671 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1672 * lockless, but we can't race with this __init function.
1673 */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001674static int kprobe_register(struct trace_event_call *event,
Masami Hiramatsufbc19632014-04-17 17:18:00 +09001675 enum trace_reg type, void *data)
Steven Rostedt22392912010-04-21 12:27:06 -04001676{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001677 struct trace_event_file *file = data;
Masami Hiramatsu1538f882011-06-27 16:26:44 +09001678
Steven Rostedt22392912010-04-21 12:27:06 -04001679 switch (type) {
1680 case TRACE_REG_REGISTER:
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001681 return enable_trace_kprobe(event, file);
Steven Rostedt22392912010-04-21 12:27:06 -04001682 case TRACE_REG_UNREGISTER:
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001683 return disable_trace_kprobe(event, file);
Steven Rostedt22392912010-04-21 12:27:06 -04001684
1685#ifdef CONFIG_PERF_EVENTS
1686 case TRACE_REG_PERF_REGISTER:
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001687 return enable_trace_kprobe(event, NULL);
Steven Rostedt22392912010-04-21 12:27:06 -04001688 case TRACE_REG_PERF_UNREGISTER:
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001689 return disable_trace_kprobe(event, NULL);
Jiri Olsaceec0b62012-02-15 15:51:49 +01001690 case TRACE_REG_PERF_OPEN:
1691 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +01001692 case TRACE_REG_PERF_ADD:
1693 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +01001694 return 0;
Steven Rostedt22392912010-04-21 12:27:06 -04001695#endif
1696 }
1697 return 0;
1698}
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001699
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001700static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001701{
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001702 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
Josef Bacik9802d862017-12-11 11:36:48 -05001703 int ret = 0;
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001704
Martin KaFai Laua7636d92016-02-03 12:28:28 -08001705 raw_cpu_inc(*tk->nhit);
Masami Hiramatsu48182bd2013-05-09 14:44:36 +09001706
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001707 if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001708 kprobe_trace_func(tk, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001709#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001710 if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
Josef Bacik9802d862017-12-11 11:36:48 -05001711 ret = kprobe_perf_func(tk, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001712#endif
Josef Bacik9802d862017-12-11 11:36:48 -05001713 return ret;
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001714}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001715NOKPROBE_SYMBOL(kprobe_dispatcher);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001716
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001717static int
1718kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001719{
Peter Zijlstrad741bf42020-08-29 22:03:24 +09001720 struct kretprobe *rp = get_kretprobe(ri);
1721 struct trace_kprobe *tk = container_of(rp, struct trace_kprobe, rp);
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001722
Martin KaFai Laua7636d92016-02-03 12:28:28 -08001723 raw_cpu_inc(*tk->nhit);
Masami Hiramatsu48182bd2013-05-09 14:44:36 +09001724
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001725 if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001726 kretprobe_trace_func(tk, ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001727#ifdef CONFIG_PERF_EVENTS
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001728 if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001729 kretprobe_perf_func(tk, ri, regs);
Li Zefan07b139c2009-12-21 14:27:35 +08001730#endif
Ingo Molnarf2cc0202021-03-23 18:49:35 +01001731 return 0; /* We don't tweak kernel, so just return 0 */
Masami Hiramatsu50d78052009-09-14 16:49:20 -04001732}
Masami Hiramatsu3da0f182014-04-17 17:18:28 +09001733NOKPROBE_SYMBOL(kretprobe_dispatcher);
Masami Hiramatsue08d1c62009-09-10 19:53:30 -04001734
Steven Rostedta9a57762010-04-22 18:46:14 -04001735static struct trace_event_functions kretprobe_funcs = {
1736 .trace = print_kretprobe_event
1737};
1738
1739static struct trace_event_functions kprobe_funcs = {
1740 .trace = print_kprobe_event
1741};
1742
Peter Zijlstra04ae87a2019-10-24 22:26:59 +02001743static struct trace_event_fields kretprobe_fields_array[] = {
1744 { .type = TRACE_FUNCTION_TYPE,
1745 .define_fields = kretprobe_event_define_fields },
1746 {}
1747};
1748
1749static struct trace_event_fields kprobe_fields_array[] = {
1750 { .type = TRACE_FUNCTION_TYPE,
1751 .define_fields = kprobe_event_define_fields },
1752 {}
1753};
1754
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001755static inline void init_trace_event_call(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001756{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001757 struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1758
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001759 if (trace_kprobe_is_return(tk)) {
Steven Rostedt80decc72010-04-23 10:00:22 -04001760 call->event.funcs = &kretprobe_funcs;
Peter Zijlstra04ae87a2019-10-24 22:26:59 +02001761 call->class->fields_array = kretprobe_fields_array;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001762 } else {
Steven Rostedt80decc72010-04-23 10:00:22 -04001763 call->event.funcs = &kprobe_funcs;
Peter Zijlstra04ae87a2019-10-24 22:26:59 +02001764 call->class->fields_array = kprobe_fields_array;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001765 }
Song Liue12f03d2017-12-06 14:45:15 -08001766
1767 call->flags = TRACE_EVENT_FL_KPROBE;
1768 call->class->reg = kprobe_register;
Song Liue12f03d2017-12-06 14:45:15 -08001769}
1770
1771static int register_kprobe_event(struct trace_kprobe *tk)
1772{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001773 init_trace_event_call(tk);
Song Liue12f03d2017-12-06 14:45:15 -08001774
Masami Hiramatsu46e53762019-06-01 00:17:16 +09001775 return trace_probe_register_event_call(&tk->tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001776}
1777
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001778static int unregister_kprobe_event(struct trace_kprobe *tk)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001779{
Masami Hiramatsu46e53762019-06-01 00:17:16 +09001780 return trace_probe_unregister_event_call(&tk->tp);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001781}
1782
Song Liue12f03d2017-12-06 14:45:15 -08001783#ifdef CONFIG_PERF_EVENTS
1784/* create a trace_kprobe, but don't add it to global lists */
1785struct trace_event_call *
1786create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1787 bool is_return)
1788{
Steven Rostedt (VMware)007517a2021-08-19 00:13:27 -04001789 enum probe_print_type ptype;
Song Liue12f03d2017-12-06 14:45:15 -08001790 struct trace_kprobe *tk;
1791 int ret;
1792 char *event;
1793
1794 /*
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001795 * local trace_kprobes are not added to dyn_event, so they are never
Song Liue12f03d2017-12-06 14:45:15 -08001796 * searched in find_trace_kprobe(). Therefore, there is no concern of
1797 * duplicated name here.
1798 */
1799 event = func ? func : "DUMMY_EVENT";
1800
1801 tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1802 offs, 0 /* maxactive */, 0 /* nargs */,
1803 is_return);
1804
1805 if (IS_ERR(tk)) {
1806 pr_info("Failed to allocate trace_probe.(%d)\n",
1807 (int)PTR_ERR(tk));
1808 return ERR_CAST(tk);
1809 }
1810
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001811 init_trace_event_call(tk);
Song Liue12f03d2017-12-06 14:45:15 -08001812
Steven Rostedt (VMware)007517a2021-08-19 00:13:27 -04001813 ptype = trace_kprobe_is_return(tk) ?
1814 PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
1815 if (traceprobe_set_print_fmt(&tk->tp, ptype) < 0) {
Song Liue12f03d2017-12-06 14:45:15 -08001816 ret = -ENOMEM;
1817 goto error;
1818 }
1819
1820 ret = __register_trace_kprobe(tk);
Masami Hiramatsuf730e0f2019-06-01 00:16:46 +09001821 if (ret < 0)
Song Liue12f03d2017-12-06 14:45:15 -08001822 goto error;
1823
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001824 return trace_probe_event_call(&tk->tp);
Song Liue12f03d2017-12-06 14:45:15 -08001825error:
1826 free_trace_kprobe(tk);
1827 return ERR_PTR(ret);
1828}
1829
1830void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1831{
1832 struct trace_kprobe *tk;
1833
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001834 tk = trace_kprobe_primary_from_call(event_call);
1835 if (unlikely(!tk))
1836 return;
Song Liue12f03d2017-12-06 14:45:15 -08001837
1838 if (trace_probe_is_enabled(&tk->tp)) {
1839 WARN_ON(1);
1840 return;
1841 }
1842
1843 __unregister_trace_kprobe(tk);
Jiri Olsa0fc8c352018-07-09 16:19:06 +02001844
Song Liue12f03d2017-12-06 14:45:15 -08001845 free_trace_kprobe(tk);
1846}
1847#endif /* CONFIG_PERF_EVENTS */
1848
Masami Hiramatsu970988e2019-05-22 17:32:35 +09001849static __init void enable_boot_kprobe_events(void)
1850{
1851 struct trace_array *tr = top_trace_array();
1852 struct trace_event_file *file;
1853 struct trace_kprobe *tk;
1854 struct dyn_event *pos;
1855
1856 mutex_lock(&event_mutex);
1857 for_each_trace_kprobe(tk, pos) {
1858 list_for_each_entry(file, &tr->events, list)
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001859 if (file->event_call == trace_probe_event_call(&tk->tp))
Masami Hiramatsu970988e2019-05-22 17:32:35 +09001860 trace_event_enable_disable(file, 1, 0);
1861 }
1862 mutex_unlock(&event_mutex);
1863}
1864
1865static __init void setup_boot_kprobe_events(void)
1866{
1867 char *p, *cmd = kprobe_boot_events_buf;
1868 int ret;
1869
1870 strreplace(kprobe_boot_events_buf, ',', ' ');
1871
1872 while (cmd && *cmd != '\0') {
1873 p = strchr(cmd, ';');
1874 if (p)
1875 *p++ = '\0';
1876
Masami Hiramatsud2622712021-02-01 13:48:11 -06001877 ret = create_or_delete_trace_kprobe(cmd);
Masami Hiramatsu970988e2019-05-22 17:32:35 +09001878 if (ret)
1879 pr_warn("Failed to add event(%d): %s\n", ret, cmd);
1880
1881 cmd = p;
1882 }
1883
1884 enable_boot_kprobe_events();
1885}
1886
Masami Hiramatsud8d4c6d2020-01-11 01:05:42 +09001887/*
Masami Hiramatsuba0fbfb2020-09-10 21:39:17 +09001888 * Register dynevent at core_initcall. This allows kernel to setup kprobe
1889 * events in postcore_initcall without tracefs.
Masami Hiramatsud8d4c6d2020-01-11 01:05:42 +09001890 */
1891static __init int init_kprobe_trace_early(void)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001892{
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09001893 int ret;
1894
1895 ret = dyn_event_register(&trace_kprobe_ops);
1896 if (ret)
1897 return ret;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001898
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001899 if (register_module_notifier(&trace_kprobe_module_nb))
Masami Hiramatsu61424312011-06-27 16:26:56 +09001900 return -EINVAL;
1901
Masami Hiramatsud8d4c6d2020-01-11 01:05:42 +09001902 return 0;
1903}
Masami Hiramatsuba0fbfb2020-09-10 21:39:17 +09001904core_initcall(init_kprobe_trace_early);
Masami Hiramatsud8d4c6d2020-01-11 01:05:42 +09001905
1906/* Make a tracefs interface for controlling probe points */
1907static __init int init_kprobe_trace(void)
1908{
Wei Yang22c36b12020-07-12 09:10:36 +08001909 int ret;
Masami Hiramatsud8d4c6d2020-01-11 01:05:42 +09001910 struct dentry *entry;
1911
Wei Yang22c36b12020-07-12 09:10:36 +08001912 ret = tracing_init_dentry();
1913 if (ret)
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001914 return 0;
1915
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -04001916 entry = tracefs_create_file("kprobe_events", TRACE_MODE_WRITE,
1917 NULL, NULL, &kprobe_events_ops);
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001918
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001919 /* Event list interface */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001920 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001921 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001922
1923 /* Profile interface */
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -04001924 entry = tracefs_create_file("kprobe_profile", TRACE_MODE_READ,
1925 NULL, NULL, &kprobe_profile_ops);
Masami Hiramatsucd7e7bd2009-08-13 16:35:42 -04001926
1927 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001928 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
Masami Hiramatsu970988e2019-05-22 17:32:35 +09001929
1930 setup_boot_kprobe_events();
1931
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001932 return 0;
1933}
1934fs_initcall(init_kprobe_trace);
1935
1936
1937#ifdef CONFIG_FTRACE_STARTUP_TEST
Arnd Bergmann26a346f2017-02-01 17:57:56 +01001938static __init struct trace_event_file *
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001939find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001940{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001941 struct trace_event_file *file;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001942
1943 list_for_each_entry(file, &tr->events, list)
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001944 if (file->event_call == trace_probe_event_call(&tk->tp))
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001945 return file;
1946
1947 return NULL;
1948}
1949
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001950/*
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001951 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
Oleg Nesterov3fe3d612013-06-20 19:38:09 +02001952 * stage, we can do this lockless.
1953 */
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001954static __init int kprobe_trace_self_tests_init(void)
1955{
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001956 int ret, warn = 0;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001957 int (*target)(int, int, int, int, int, int);
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001958 struct trace_kprobe *tk;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001959 struct trace_event_file *file;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001960
Yoshihiro YUNOMAE748ec3a2014-06-06 07:35:20 +09001961 if (tracing_is_disabled())
1962 return -ENODEV;
1963
Masami Hiramatsu60efe212020-12-08 17:54:09 +09001964 if (tracing_selftest_disabled)
Steven Rostedt (VMware)b6399cc2019-05-23 19:50:34 -04001965 return 0;
Steven Rostedt (VMware)b6399cc2019-05-23 19:50:34 -04001966
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001967 target = kprobe_trace_selftest_target;
1968
1969 pr_info("Testing kprobe tracing: ");
1970
Masami Hiramatsud2622712021-02-01 13:48:11 -06001971 ret = create_or_delete_trace_kprobe("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001972 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001973 pr_warn("error on probing function entry.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001974 warn++;
1975 } else {
1976 /* Enable trace point */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001977 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1978 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001979 pr_warn("error on getting new probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001980 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001981 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001982 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001983 if (WARN_ON_ONCE(file == NULL)) {
1984 pr_warn("error on getting probe file.\n");
1985 warn++;
1986 } else
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001987 enable_trace_kprobe(
1988 trace_probe_event_call(&tk->tp), file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001989 }
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001990 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04001991
Masami Hiramatsud2622712021-02-01 13:48:11 -06001992 ret = create_or_delete_trace_kprobe("r:testprobe2 kprobe_trace_selftest_target $retval");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001993 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09001994 pr_warn("error on probing function return.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05001995 warn++;
1996 } else {
1997 /* Enable trace point */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09001998 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1999 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002000 pr_warn("error on getting 2nd new probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002001 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002002 } else {
Namhyung Kimc31ffb32013-07-03 13:50:51 +09002003 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002004 if (WARN_ON_ONCE(file == NULL)) {
2005 pr_warn("error on getting probe file.\n");
2006 warn++;
2007 } else
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09002008 enable_trace_kprobe(
2009 trace_probe_event_call(&tk->tp), file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002010 }
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002011 }
2012
2013 if (warn)
2014 goto end;
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04002015
2016 ret = target(1, 2, 3, 4, 5, 6);
2017
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01002018 /*
2019 * Not expecting an error here, the check is only to prevent the
2020 * optimizer from removing the call to target() as otherwise there
2021 * are no side-effects and the call is never performed.
2022 */
2023 if (ret != 21)
2024 warn++;
2025
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09002026 /* Disable trace points before removing it */
Namhyung Kimc31ffb32013-07-03 13:50:51 +09002027 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
2028 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002029 pr_warn("error on getting test probe.\n");
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09002030 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002031 } else {
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01002032 if (trace_kprobe_nhit(tk) != 1) {
2033 pr_warn("incorrect number of testprobe hits\n");
2034 warn++;
2035 }
2036
Namhyung Kimc31ffb32013-07-03 13:50:51 +09002037 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002038 if (WARN_ON_ONCE(file == NULL)) {
2039 pr_warn("error on getting probe file.\n");
2040 warn++;
2041 } else
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09002042 disable_trace_kprobe(
2043 trace_probe_event_call(&tk->tp), file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002044 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09002045
Namhyung Kimc31ffb32013-07-03 13:50:51 +09002046 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
2047 if (WARN_ON_ONCE(tk == NULL)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002048 pr_warn("error on getting 2nd test probe.\n");
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09002049 warn++;
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002050 } else {
Marcin Nowakowskid4d7ccc2016-12-09 15:19:38 +01002051 if (trace_kprobe_nhit(tk) != 1) {
2052 pr_warn("incorrect number of testprobe2 hits\n");
2053 warn++;
2054 }
2055
Namhyung Kimc31ffb32013-07-03 13:50:51 +09002056 file = find_trace_probe_file(tk, top_trace_array());
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002057 if (WARN_ON_ONCE(file == NULL)) {
2058 pr_warn("error on getting probe file.\n");
2059 warn++;
2060 } else
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09002061 disable_trace_kprobe(
2062 trace_probe_event_call(&tk->tp), file);
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002063 }
Masami Hiramatsu02ca1522011-10-04 19:44:38 +09002064
Masami Hiramatsud2622712021-02-01 13:48:11 -06002065 ret = create_or_delete_trace_kprobe("-:testprobe");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002066 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002067 pr_warn("error on deleting a probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002068 warn++;
2069 }
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04002070
Masami Hiramatsud2622712021-02-01 13:48:11 -06002071 ret = create_or_delete_trace_kprobe("-:testprobe2");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002072 if (WARN_ON_ONCE(ret)) {
Masami Hiramatsu41a7dd42013-05-09 14:44:49 +09002073 pr_warn("error on deleting a probe.\n");
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002074 warn++;
2075 }
2076
2077end:
Masami Hiramatsu6212dd22018-11-05 18:02:36 +09002078 ret = dyn_events_release_all(&trace_kprobe_ops);
2079 if (WARN_ON_ONCE(ret)) {
2080 pr_warn("error on cleaning up probes.\n");
2081 warn++;
2082 }
Thomas Gleixner30e7d8942017-05-17 10:19:49 +02002083 /*
2084 * Wait for the optimizer work to finish. Otherwise it might fiddle
2085 * with probes in already freed __init text.
2086 */
2087 wait_for_kprobe_optimizer();
Masami Hiramatsu231e36f2010-01-14 00:12:12 -05002088 if (warn)
2089 pr_cont("NG: Some tests are failed. Please check them.\n");
2090 else
2091 pr_cont("OK\n");
Masami Hiramatsu413d37d2009-08-13 16:35:11 -04002092 return 0;
2093}
2094
2095late_initcall(kprobe_trace_self_tests_init);
2096
2097#endif