blob: 3a7c73c40007ff6c779d807529a740d777cb8e05 [file] [log] [blame]
Steven Rostedt (VMware)bcea3f92018-08-16 11:23:53 -04001// SPDX-License-Identifier: GPL-2.0
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05302/*
3 * uprobes-based tracing events
4 *
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05305 * Copyright (C) IBM Corporation, 2010-2012
6 * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
7 */
Masami Hiramatsu72576342017-02-07 20:21:28 +09008#define pr_fmt(fmt) "trace_kprobe: " fmt
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05309
10#include <linux/module.h>
11#include <linux/uaccess.h>
12#include <linux/uprobes.h>
13#include <linux/namei.h>
Andy Shevchenkob2e902f2012-12-17 16:01:27 -080014#include <linux/string.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010015#include <linux/rculist.h>
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053016
17#include "trace_probe.h"
18
19#define UPROBE_EVENT_SYSTEM "uprobes"
20
Oleg Nesterov457d1772013-03-29 18:26:51 +010021struct uprobe_trace_entry_head {
22 struct trace_entry ent;
23 unsigned long vaddr[];
24};
25
26#define SIZEOF_TRACE_ENTRY(is_return) \
27 (sizeof(struct uprobe_trace_entry_head) + \
28 sizeof(unsigned long) * (is_return ? 2 : 1))
29
30#define DATAOF_TRACE_ENTRY(entry, is_return) \
31 ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
32
Oleg Nesterov736288b2013-02-03 20:58:35 +010033struct trace_uprobe_filter {
34 rwlock_t rwlock;
35 int nr_systemwide;
36 struct list_head perf_events;
37};
38
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053039/*
40 * uprobe event core functions
41 */
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053042struct trace_uprobe {
43 struct list_head list;
Oleg Nesterov736288b2013-02-03 20:58:35 +010044 struct trace_uprobe_filter filter;
Oleg Nesterova932b732013-01-31 19:47:23 +010045 struct uprobe_consumer consumer;
Song Liu0c92c7a2018-04-23 10:21:34 -070046 struct path path;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053047 struct inode *inode;
48 char *filename;
49 unsigned long offset;
Ravi Bangoria1cc33162018-08-20 10:12:47 +053050 unsigned long ref_ctr_offset;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053051 unsigned long nhit;
Namhyung Kim14577c32013-07-03 15:42:53 +090052 struct trace_probe tp;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053053};
54
Namhyung Kim14577c32013-07-03 15:42:53 +090055#define SIZEOF_TRACE_UPROBE(n) \
56 (offsetof(struct trace_uprobe, tp.args) + \
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053057 (sizeof(struct probe_arg) * (n)))
58
59static int register_uprobe_event(struct trace_uprobe *tu);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -040060static int unregister_uprobe_event(struct trace_uprobe *tu);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053061
62static DEFINE_MUTEX(uprobe_lock);
63static LIST_HEAD(uprobe_list);
64
Namhyung Kimb7e0bf32013-11-25 13:42:47 +090065struct uprobe_dispatch_data {
66 struct trace_uprobe *tu;
67 unsigned long bp_addr;
68};
69
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053070static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +010071static int uretprobe_dispatcher(struct uprobe_consumer *con,
72 unsigned long func, struct pt_regs *regs);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +053073
Namhyung Kim3fd996a2013-11-26 15:21:04 +090074#ifdef CONFIG_STACK_GROWSUP
75static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
76{
77 return addr - (n * sizeof(long));
78}
79#else
80static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
81{
82 return addr + (n * sizeof(long));
83}
84#endif
85
86static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
87{
88 unsigned long ret;
89 unsigned long addr = user_stack_pointer(regs);
90
91 addr = adjust_stack_addr(addr, n);
92
93 if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
94 return 0;
95
96 return ret;
97}
98
99/*
100 * Uprobes-specific fetch functions
101 */
102#define DEFINE_FETCH_stack(type) \
Masami Hiramatsufbc19632014-04-17 17:18:00 +0900103static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
104 void *offset, void *dest) \
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900105{ \
106 *(type *)dest = (type)get_user_stack_nth(regs, \
107 ((unsigned long)offset)); \
108}
109DEFINE_BASIC_FETCH_FUNCS(stack)
110/* No string on the stack entry */
111#define fetch_stack_string NULL
112#define fetch_stack_string_size NULL
113
Namhyung Kim5baaa592013-11-26 15:21:04 +0900114#define DEFINE_FETCH_memory(type) \
Masami Hiramatsufbc19632014-04-17 17:18:00 +0900115static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
116 void *addr, void *dest) \
Namhyung Kim5baaa592013-11-26 15:21:04 +0900117{ \
118 type retval; \
119 void __user *vaddr = (void __force __user *) addr; \
120 \
121 if (copy_from_user(&retval, vaddr, sizeof(type))) \
122 *(type *)dest = 0; \
123 else \
124 *(type *) dest = retval; \
125}
126DEFINE_BASIC_FETCH_FUNCS(memory)
127/*
128 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
129 * length and relative data location.
130 */
Masami Hiramatsufbc19632014-04-17 17:18:00 +0900131static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
132 void *addr, void *dest)
Namhyung Kim5baaa592013-11-26 15:21:04 +0900133{
134 long ret;
135 u32 rloc = *(u32 *)dest;
136 int maxlen = get_rloc_len(rloc);
137 u8 *dst = get_rloc_data(dest);
138 void __user *src = (void __force __user *) addr;
139
140 if (!maxlen)
141 return;
142
143 ret = strncpy_from_user(dst, src, maxlen);
Masami Hiramatsu50268a32018-04-10 21:20:08 +0900144 if (ret == maxlen)
145 dst[--ret] = '\0';
Namhyung Kim5baaa592013-11-26 15:21:04 +0900146
147 if (ret < 0) { /* Failed to fetch string */
148 ((u8 *)get_rloc_data(dest))[0] = '\0';
149 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(rloc));
150 } else {
151 *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(rloc));
152 }
153}
154
Masami Hiramatsufbc19632014-04-17 17:18:00 +0900155static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
156 void *addr, void *dest)
Namhyung Kim5baaa592013-11-26 15:21:04 +0900157{
158 int len;
159 void __user *vaddr = (void __force __user *) addr;
160
161 len = strnlen_user(vaddr, MAX_STRING_SIZE);
162
163 if (len == 0 || len > MAX_STRING_SIZE) /* Failed to check length */
164 *(u32 *)dest = 0;
165 else
166 *(u32 *)dest = len;
167}
Namhyung Kim3fd996a2013-11-26 15:21:04 +0900168
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900169static unsigned long translate_user_vaddr(void *file_offset)
170{
171 unsigned long base_addr;
172 struct uprobe_dispatch_data *udd;
173
174 udd = (void *) current->utask->vaddr;
175
176 base_addr = udd->bp_addr - udd->tu->offset;
177 return base_addr + (unsigned long)file_offset;
178}
179
180#define DEFINE_FETCH_file_offset(type) \
Masami Hiramatsufbc19632014-04-17 17:18:00 +0900181static void FETCH_FUNC_NAME(file_offset, type)(struct pt_regs *regs, \
182 void *offset, void *dest)\
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900183{ \
184 void *vaddr = (void *)translate_user_vaddr(offset); \
185 \
186 FETCH_FUNC_NAME(memory, type)(regs, vaddr, dest); \
187}
188DEFINE_BASIC_FETCH_FUNCS(file_offset)
189DEFINE_FETCH_file_offset(string)
190DEFINE_FETCH_file_offset(string_size)
191
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900192/* Fetch type information table */
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100193static const struct fetch_type uprobes_fetch_type_table[] = {
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900194 /* Special types */
195 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
196 sizeof(u32), 1, "__data_loc char[]"),
197 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
198 string_size, sizeof(u32), 0, "u32"),
199 /* Basic types */
200 ASSIGN_FETCH_TYPE(u8, u8, 0),
201 ASSIGN_FETCH_TYPE(u16, u16, 0),
202 ASSIGN_FETCH_TYPE(u32, u32, 0),
203 ASSIGN_FETCH_TYPE(u64, u64, 0),
204 ASSIGN_FETCH_TYPE(s8, u8, 1),
205 ASSIGN_FETCH_TYPE(s16, u16, 1),
206 ASSIGN_FETCH_TYPE(s32, u32, 1),
207 ASSIGN_FETCH_TYPE(s64, u64, 1),
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +0900208 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
209 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
210 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
211 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900212
213 ASSIGN_FETCH_TYPE_END
214};
215
Oleg Nesterov736288b2013-02-03 20:58:35 +0100216static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
217{
218 rwlock_init(&filter->rwlock);
219 filter->nr_systemwide = 0;
220 INIT_LIST_HEAD(&filter->perf_events);
221}
222
223static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
224{
225 return !filter->nr_systemwide && list_empty(&filter->perf_events);
226}
227
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100228static inline bool is_ret_probe(struct trace_uprobe *tu)
229{
230 return tu->consumer.ret_handler != NULL;
231}
232
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530233/*
234 * Allocate new trace_uprobe and initialize it (including uprobes).
235 */
236static struct trace_uprobe *
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100237alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530238{
239 struct trace_uprobe *tu;
240
241 if (!event || !is_good_name(event))
242 return ERR_PTR(-EINVAL);
243
244 if (!group || !is_good_name(group))
245 return ERR_PTR(-EINVAL);
246
247 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
248 if (!tu)
249 return ERR_PTR(-ENOMEM);
250
Namhyung Kim14577c32013-07-03 15:42:53 +0900251 tu->tp.call.class = &tu->tp.class;
252 tu->tp.call.name = kstrdup(event, GFP_KERNEL);
253 if (!tu->tp.call.name)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530254 goto error;
255
Namhyung Kim14577c32013-07-03 15:42:53 +0900256 tu->tp.class.system = kstrdup(group, GFP_KERNEL);
257 if (!tu->tp.class.system)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530258 goto error;
259
260 INIT_LIST_HEAD(&tu->list);
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900261 INIT_LIST_HEAD(&tu->tp.files);
Oleg Nesterova932b732013-01-31 19:47:23 +0100262 tu->consumer.handler = uprobe_dispatcher;
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100263 if (is_ret)
264 tu->consumer.ret_handler = uretprobe_dispatcher;
Oleg Nesterov736288b2013-02-03 20:58:35 +0100265 init_trace_uprobe_filter(&tu->filter);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530266 return tu;
267
268error:
Namhyung Kim14577c32013-07-03 15:42:53 +0900269 kfree(tu->tp.call.name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530270 kfree(tu);
271
272 return ERR_PTR(-ENOMEM);
273}
274
275static void free_trace_uprobe(struct trace_uprobe *tu)
276{
277 int i;
278
Namhyung Kim14577c32013-07-03 15:42:53 +0900279 for (i = 0; i < tu->tp.nr_args; i++)
280 traceprobe_free_probe_arg(&tu->tp.args[i]);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530281
Song Liu0c92c7a2018-04-23 10:21:34 -0700282 path_put(&tu->path);
Namhyung Kim14577c32013-07-03 15:42:53 +0900283 kfree(tu->tp.call.class->system);
284 kfree(tu->tp.call.name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530285 kfree(tu->filename);
286 kfree(tu);
287}
288
289static struct trace_uprobe *find_probe_event(const char *event, const char *group)
290{
291 struct trace_uprobe *tu;
292
293 list_for_each_entry(tu, &uprobe_list, list)
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400294 if (strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
Namhyung Kim14577c32013-07-03 15:42:53 +0900295 strcmp(tu->tp.call.class->system, group) == 0)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530296 return tu;
297
298 return NULL;
299}
300
301/* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400302static int unregister_trace_uprobe(struct trace_uprobe *tu)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530303{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400304 int ret;
305
306 ret = unregister_uprobe_event(tu);
307 if (ret)
308 return ret;
309
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530310 list_del(&tu->list);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530311 free_trace_uprobe(tu);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400312 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530313}
314
Ravi Bangoriaccea8722018-08-20 10:12:49 +0530315/*
316 * Uprobe with multiple reference counter is not allowed. i.e.
317 * If inode and offset matches, reference counter offset *must*
318 * match as well. Though, there is one exception: If user is
319 * replacing old trace_uprobe with new one(same group/event),
320 * then we allow same uprobe with new reference counter as far
321 * as the new one does not conflict with any other existing
322 * ones.
323 */
324static struct trace_uprobe *find_old_trace_uprobe(struct trace_uprobe *new)
325{
326 struct trace_uprobe *tmp, *old = NULL;
327 struct inode *new_inode = d_real_inode(new->path.dentry);
328
329 old = find_probe_event(trace_event_name(&new->tp.call),
330 new->tp.call.class->system);
331
332 list_for_each_entry(tmp, &uprobe_list, list) {
333 if ((old ? old != tmp : true) &&
334 new_inode == d_real_inode(tmp->path.dentry) &&
335 new->offset == tmp->offset &&
336 new->ref_ctr_offset != tmp->ref_ctr_offset) {
337 pr_warn("Reference counter offset mismatch.");
338 return ERR_PTR(-EINVAL);
339 }
340 }
341 return old;
342}
343
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530344/* Register a trace_uprobe and probe_event */
345static int register_trace_uprobe(struct trace_uprobe *tu)
346{
Namhyung Kim14577c32013-07-03 15:42:53 +0900347 struct trace_uprobe *old_tu;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530348 int ret;
349
350 mutex_lock(&uprobe_lock);
351
352 /* register as an event */
Ravi Bangoriaccea8722018-08-20 10:12:49 +0530353 old_tu = find_old_trace_uprobe(tu);
354 if (IS_ERR(old_tu)) {
355 ret = PTR_ERR(old_tu);
356 goto end;
357 }
358
Namhyung Kim14577c32013-07-03 15:42:53 +0900359 if (old_tu) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530360 /* delete old event */
Namhyung Kim14577c32013-07-03 15:42:53 +0900361 ret = unregister_trace_uprobe(old_tu);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400362 if (ret)
363 goto end;
364 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530365
366 ret = register_uprobe_event(tu);
367 if (ret) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700368 pr_warn("Failed to register probe event(%d)\n", ret);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530369 goto end;
370 }
371
372 list_add_tail(&tu->list, &uprobe_list);
373
374end:
375 mutex_unlock(&uprobe_lock);
376
377 return ret;
378}
379
380/*
381 * Argument syntax:
Namhyung Kim306cfe22013-07-03 16:44:46 +0900382 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530383 *
384 * - Remove uprobe: -:[GRP/]EVENT
385 */
386static int create_trace_uprobe(int argc, char **argv)
387{
388 struct trace_uprobe *tu;
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530389 char *arg, *event, *group, *filename, *rctr, *rctr_end;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530390 char buf[MAX_EVENT_NAME_LEN];
391 struct path path;
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530392 unsigned long offset, ref_ctr_offset;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100393 bool is_delete, is_return;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530394 int i, ret;
395
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530396 ret = 0;
397 is_delete = false;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100398 is_return = false;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530399 event = NULL;
400 group = NULL;
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530401 ref_ctr_offset = 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530402
403 /* argc must be >= 1 */
404 if (argv[0][0] == '-')
405 is_delete = true;
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100406 else if (argv[0][0] == 'r')
407 is_return = true;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530408 else if (argv[0][0] != 'p') {
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100409 pr_info("Probe definition must be started with 'p', 'r' or '-'.\n");
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530410 return -EINVAL;
411 }
412
413 if (argv[0][1] == ':') {
414 event = &argv[0][2];
415 arg = strchr(event, '/');
416
417 if (arg) {
418 group = event;
419 event = arg + 1;
420 event[-1] = '\0';
421
422 if (strlen(group) == 0) {
423 pr_info("Group name is not specified\n");
424 return -EINVAL;
425 }
426 }
427 if (strlen(event) == 0) {
428 pr_info("Event name is not specified\n");
429 return -EINVAL;
430 }
431 }
432 if (!group)
433 group = UPROBE_EVENT_SYSTEM;
434
435 if (is_delete) {
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400436 int ret;
437
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530438 if (!event) {
439 pr_info("Delete command needs an event name.\n");
440 return -EINVAL;
441 }
442 mutex_lock(&uprobe_lock);
443 tu = find_probe_event(event, group);
444
445 if (!tu) {
446 mutex_unlock(&uprobe_lock);
447 pr_info("Event %s/%s doesn't exist.\n", group, event);
448 return -ENOENT;
449 }
450 /* delete an event */
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400451 ret = unregister_trace_uprobe(tu);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530452 mutex_unlock(&uprobe_lock);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400453 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530454 }
455
456 if (argc < 2) {
457 pr_info("Probe point is not specified.\n");
458 return -EINVAL;
459 }
Kenny Yu6496bb72017-01-13 08:58:34 -0800460 /* Find the last occurrence, in case the path contains ':' too. */
461 arg = strrchr(argv[1], ':');
Song Liu0c92c7a2018-04-23 10:21:34 -0700462 if (!arg)
463 return -EINVAL;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530464
465 *arg++ = '\0';
466 filename = argv[1];
467 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
468 if (ret)
Song Liu0c92c7a2018-04-23 10:21:34 -0700469 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530470
Song Liu0c92c7a2018-04-23 10:21:34 -0700471 if (!d_is_reg(path.dentry)) {
Jovi Zhangd24d7db2012-07-18 18:16:44 +0800472 ret = -EINVAL;
473 goto fail_address_parse;
474 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530475
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530476 /* Parse reference counter offset if specified. */
477 rctr = strchr(arg, '(');
478 if (rctr) {
479 rctr_end = strchr(rctr, ')');
480 if (rctr > rctr_end || *(rctr_end + 1) != 0) {
481 ret = -EINVAL;
482 pr_info("Invalid reference counter offset.\n");
483 goto fail_address_parse;
484 }
485
486 *rctr++ = '\0';
487 *rctr_end = '\0';
488 ret = kstrtoul(rctr, 0, &ref_ctr_offset);
489 if (ret) {
490 pr_info("Invalid reference counter offset.\n");
491 goto fail_address_parse;
492 }
493 }
494
495 /* Parse uprobe offset. */
Oleg Nesterov84d7ed72013-01-27 18:20:45 +0100496 ret = kstrtoul(arg, 0, &offset);
497 if (ret)
498 goto fail_address_parse;
499
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530500 argc -= 2;
501 argv += 2;
502
503 /* setup a probe */
504 if (!event) {
Andy Shevchenkob2e902f2012-12-17 16:01:27 -0800505 char *tail;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530506 char *ptr;
507
Andy Shevchenkob2e902f2012-12-17 16:01:27 -0800508 tail = kstrdup(kbasename(filename), GFP_KERNEL);
509 if (!tail) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530510 ret = -ENOMEM;
511 goto fail_address_parse;
512 }
513
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530514 ptr = strpbrk(tail, ".-_");
515 if (ptr)
516 *ptr = '\0';
517
518 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
519 event = buf;
520 kfree(tail);
521 }
522
Oleg Nesterov4ee5a522013-03-30 20:28:15 +0100523 tu = alloc_trace_uprobe(group, event, argc, is_return);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530524 if (IS_ERR(tu)) {
525 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
526 ret = PTR_ERR(tu);
527 goto fail_address_parse;
528 }
529 tu->offset = offset;
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530530 tu->ref_ctr_offset = ref_ctr_offset;
Song Liu0c92c7a2018-04-23 10:21:34 -0700531 tu->path = path;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530532 tu->filename = kstrdup(filename, GFP_KERNEL);
533
534 if (!tu->filename) {
535 pr_info("Failed to allocate filename.\n");
536 ret = -ENOMEM;
537 goto error;
538 }
539
540 /* parse arguments */
541 ret = 0;
542 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
Namhyung Kim14577c32013-07-03 15:42:53 +0900543 struct probe_arg *parg = &tu->tp.args[i];
544
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530545 /* Increment count for freeing args in error case */
Namhyung Kim14577c32013-07-03 15:42:53 +0900546 tu->tp.nr_args++;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530547
548 /* Parse argument name */
549 arg = strchr(argv[i], '=');
550 if (arg) {
551 *arg++ = '\0';
Namhyung Kim14577c32013-07-03 15:42:53 +0900552 parg->name = kstrdup(argv[i], GFP_KERNEL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530553 } else {
554 arg = argv[i];
555 /* If argument name is omitted, set "argN" */
556 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
Namhyung Kim14577c32013-07-03 15:42:53 +0900557 parg->name = kstrdup(buf, GFP_KERNEL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530558 }
559
Namhyung Kim14577c32013-07-03 15:42:53 +0900560 if (!parg->name) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530561 pr_info("Failed to allocate argument[%d] name.\n", i);
562 ret = -ENOMEM;
563 goto error;
564 }
565
Namhyung Kim14577c32013-07-03 15:42:53 +0900566 if (!is_good_name(parg->name)) {
567 pr_info("Invalid argument[%d] name: %s\n", i, parg->name);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530568 ret = -EINVAL;
569 goto error;
570 }
571
Namhyung Kim14577c32013-07-03 15:42:53 +0900572 if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) {
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530573 pr_info("Argument[%d] name '%s' conflicts with "
574 "another field.\n", i, argv[i]);
575 ret = -EINVAL;
576 goto error;
577 }
578
579 /* Parse fetch argument */
Namhyung Kim14577c32013-07-03 15:42:53 +0900580 ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg,
Stephen Rothwelld9a16d32015-03-12 16:58:34 +1100581 is_return, false,
582 uprobes_fetch_type_table);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530583 if (ret) {
584 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
585 goto error;
586 }
587 }
588
589 ret = register_trace_uprobe(tu);
590 if (ret)
591 goto error;
592 return 0;
593
594error:
595 free_trace_uprobe(tu);
596 return ret;
597
598fail_address_parse:
Song Liu0c92c7a2018-04-23 10:21:34 -0700599 path_put(&path);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530600
Jovi Zhangd24d7db2012-07-18 18:16:44 +0800601 pr_info("Failed to parse address or file.\n");
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530602
603 return ret;
604}
605
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400606static int cleanup_all_probes(void)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530607{
608 struct trace_uprobe *tu;
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400609 int ret = 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530610
611 mutex_lock(&uprobe_lock);
612 while (!list_empty(&uprobe_list)) {
613 tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400614 ret = unregister_trace_uprobe(tu);
615 if (ret)
616 break;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530617 }
618 mutex_unlock(&uprobe_lock);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400619 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530620}
621
622/* Probes listing interfaces */
623static void *probes_seq_start(struct seq_file *m, loff_t *pos)
624{
625 mutex_lock(&uprobe_lock);
626 return seq_list_start(&uprobe_list, *pos);
627}
628
629static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
630{
631 return seq_list_next(v, &uprobe_list, pos);
632}
633
634static void probes_seq_stop(struct seq_file *m, void *v)
635{
636 mutex_unlock(&uprobe_lock);
637}
638
639static int probes_seq_show(struct seq_file *m, void *v)
640{
641 struct trace_uprobe *tu = v;
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100642 char c = is_ret_probe(tu) ? 'r' : 'p';
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530643 int i;
644
Ravi Bangoriaa64b2c02018-03-15 13:57:56 +0530645 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system,
646 trace_event_name(&tu->tp.call), tu->filename,
647 (int)(sizeof(void *) * 2), tu->offset);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530648
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530649 if (tu->ref_ctr_offset)
650 seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
651
Namhyung Kim14577c32013-07-03 15:42:53 +0900652 for (i = 0; i < tu->tp.nr_args; i++)
653 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530654
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100655 seq_putc(m, '\n');
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530656 return 0;
657}
658
659static const struct seq_operations probes_seq_op = {
660 .start = probes_seq_start,
661 .next = probes_seq_next,
662 .stop = probes_seq_stop,
663 .show = probes_seq_show
664};
665
666static int probes_open(struct inode *inode, struct file *file)
667{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -0400668 int ret;
669
670 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
671 ret = cleanup_all_probes();
672 if (ret)
673 return ret;
674 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530675
676 return seq_open(file, &probes_seq_op);
677}
678
679static ssize_t probes_write(struct file *file, const char __user *buffer,
680 size_t count, loff_t *ppos)
681{
Tom Zanussi7e465ba2017-09-22 14:58:20 -0500682 return trace_parse_run_command(file, buffer, count, ppos, create_trace_uprobe);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530683}
684
685static const struct file_operations uprobe_events_ops = {
686 .owner = THIS_MODULE,
687 .open = probes_open,
688 .read = seq_read,
689 .llseek = seq_lseek,
690 .release = seq_release,
691 .write = probes_write,
692};
693
694/* Probes profiling interfaces */
695static int probes_profile_seq_show(struct seq_file *m, void *v)
696{
697 struct trace_uprobe *tu = v;
698
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400699 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400700 trace_event_name(&tu->tp.call), tu->nhit);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530701 return 0;
702}
703
704static const struct seq_operations profile_seq_op = {
705 .start = probes_seq_start,
706 .next = probes_seq_next,
707 .stop = probes_seq_stop,
708 .show = probes_profile_seq_show
709};
710
711static int profile_open(struct inode *inode, struct file *file)
712{
713 return seq_open(file, &profile_seq_op);
714}
715
716static const struct file_operations uprobe_profile_ops = {
717 .owner = THIS_MODULE,
718 .open = profile_open,
719 .read = seq_read,
720 .llseek = seq_lseek,
721 .release = seq_release,
722};
723
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900724struct uprobe_cpu_buffer {
725 struct mutex mutex;
726 void *buf;
727};
728static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
729static int uprobe_buffer_refcnt;
730
731static int uprobe_buffer_init(void)
732{
733 int cpu, err_cpu;
734
735 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
736 if (uprobe_cpu_buffer == NULL)
737 return -ENOMEM;
738
739 for_each_possible_cpu(cpu) {
740 struct page *p = alloc_pages_node(cpu_to_node(cpu),
741 GFP_KERNEL, 0);
742 if (p == NULL) {
743 err_cpu = cpu;
744 goto err;
745 }
746 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
747 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
748 }
749
750 return 0;
751
752err:
753 for_each_possible_cpu(cpu) {
754 if (cpu == err_cpu)
755 break;
756 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
757 }
758
759 free_percpu(uprobe_cpu_buffer);
760 return -ENOMEM;
761}
762
763static int uprobe_buffer_enable(void)
764{
765 int ret = 0;
766
767 BUG_ON(!mutex_is_locked(&event_mutex));
768
769 if (uprobe_buffer_refcnt++ == 0) {
770 ret = uprobe_buffer_init();
771 if (ret < 0)
772 uprobe_buffer_refcnt--;
773 }
774
775 return ret;
776}
777
778static void uprobe_buffer_disable(void)
779{
zhangwei(Jovi)6ea62152014-04-17 16:05:19 +0800780 int cpu;
781
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900782 BUG_ON(!mutex_is_locked(&event_mutex));
783
784 if (--uprobe_buffer_refcnt == 0) {
zhangwei(Jovi)6ea62152014-04-17 16:05:19 +0800785 for_each_possible_cpu(cpu)
786 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
787 cpu)->buf);
788
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900789 free_percpu(uprobe_cpu_buffer);
790 uprobe_cpu_buffer = NULL;
791 }
792}
793
794static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
795{
796 struct uprobe_cpu_buffer *ucb;
797 int cpu;
798
799 cpu = raw_smp_processor_id();
800 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
801
802 /*
803 * Use per-cpu buffers for fastest access, but we might migrate
804 * so the mutex makes sure we have sole access to it.
805 */
806 mutex_lock(&ucb->mutex);
807
808 return ucb;
809}
810
811static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
812{
813 mutex_unlock(&ucb->mutex);
814}
815
Namhyung Kima43b9702014-01-17 17:08:36 +0900816static void __uprobe_trace_func(struct trace_uprobe *tu,
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900817 unsigned long func, struct pt_regs *regs,
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900818 struct uprobe_cpu_buffer *ucb, int dsize,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400819 struct trace_event_file *trace_file)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530820{
821 struct uprobe_trace_entry_head *entry;
822 struct ring_buffer_event *event;
823 struct ring_buffer *buffer;
Oleg Nesterov457d1772013-03-29 18:26:51 +0100824 void *data;
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900825 int size, esize;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400826 struct trace_event_call *call = &tu->tp.call;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530827
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400828 WARN_ON(call != trace_file->event_call);
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900829
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900830 if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
Oleg Nesterova51cc602013-03-30 18:02:12 +0100831 return;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530832
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -0400833 if (trace_trigger_soft_disabled(trace_file))
Namhyung Kimca3b1622014-01-17 17:08:39 +0900834 return;
835
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900836 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900837 size = esize + tu->tp.size + dsize;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400838 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900839 call->event.type, size, 0, 0);
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900840 if (!event)
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900841 return;
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900842
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530843 entry = ring_buffer_event_data(event);
Oleg Nesterov393a7362013-03-30 18:46:22 +0100844 if (is_ret_probe(tu)) {
845 entry->vaddr[0] = func;
846 entry->vaddr[1] = instruction_pointer(regs);
847 data = DATAOF_TRACE_ENTRY(entry, true);
848 } else {
849 entry->vaddr[0] = instruction_pointer(regs);
850 data = DATAOF_TRACE_ENTRY(entry, false);
851 }
852
Namhyung Kimdcad1a22013-07-03 16:40:28 +0900853 memcpy(data, ucb->buf, tu->tp.size + dsize);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530854
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400855 event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
Oleg Nesterova51cc602013-03-30 18:02:12 +0100856}
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100857
Oleg Nesterova51cc602013-03-30 18:02:12 +0100858/* uprobe handler */
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900859static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
860 struct uprobe_cpu_buffer *ucb, int dsize)
Oleg Nesterova51cc602013-03-30 18:02:12 +0100861{
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900862 struct event_file_link *link;
863
864 if (is_ret_probe(tu))
865 return 0;
866
867 rcu_read_lock();
868 list_for_each_entry_rcu(link, &tu->tp.files, list)
869 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
870 rcu_read_unlock();
871
Oleg Nesterovf42d24a2013-02-04 17:48:34 +0100872 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530873}
874
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100875static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
Namhyung Kimdd9fa552014-01-17 17:08:37 +0900876 struct pt_regs *regs,
877 struct uprobe_cpu_buffer *ucb, int dsize)
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100878{
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900879 struct event_file_link *link;
880
881 rcu_read_lock();
882 list_for_each_entry_rcu(link, &tu->tp.files, list)
883 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
884 rcu_read_unlock();
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +0100885}
886
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530887/* Event entry printers */
888static enum print_line_t
889print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
890{
Oleg Nesterov457d1772013-03-29 18:26:51 +0100891 struct uprobe_trace_entry_head *entry;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530892 struct trace_seq *s = &iter->seq;
893 struct trace_uprobe *tu;
894 u8 *data;
895 int i;
896
Oleg Nesterov457d1772013-03-29 18:26:51 +0100897 entry = (struct uprobe_trace_entry_head *)iter->ent;
Namhyung Kim14577c32013-07-03 15:42:53 +0900898 tu = container_of(event, struct trace_uprobe, tp.call.event);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530899
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100900 if (is_ret_probe(tu)) {
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500901 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400902 trace_event_name(&tu->tp.call),
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500903 entry->vaddr[1], entry->vaddr[0]);
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100904 data = DATAOF_TRACE_ENTRY(entry, true);
905 } else {
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500906 trace_seq_printf(s, "%s: (0x%lx)",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400907 trace_event_name(&tu->tp.call),
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500908 entry->vaddr[0]);
Oleg Nesterov3ede82d2013-03-30 19:48:09 +0100909 data = DATAOF_TRACE_ENTRY(entry, false);
910 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530911
Namhyung Kim14577c32013-07-03 15:42:53 +0900912 for (i = 0; i < tu->tp.nr_args; i++) {
913 struct probe_arg *parg = &tu->tp.args[i];
914
915 if (!parg->type->print(s, parg->name, data + parg->offset, entry))
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500916 goto out;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530917 }
918
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500919 trace_seq_putc(s, '\n');
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530920
Steven Rostedt (Red Hat)8579a102014-11-12 17:26:57 -0500921 out:
922 return trace_handle_return(s);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530923}
924
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100925typedef bool (*filter_func_t)(struct uprobe_consumer *self,
926 enum uprobe_filter_ctx ctx,
927 struct mm_struct *mm);
928
929static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400930probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900931 filter_func_t filter)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530932{
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900933 bool enabled = trace_probe_is_enabled(&tu->tp);
934 struct event_file_link *link = NULL;
935 int ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530936
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900937 if (file) {
Oleg Nesterov48212542014-06-27 19:01:36 +0200938 if (tu->tp.flags & TP_FLAG_PROFILE)
939 return -EINTR;
940
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900941 link = kmalloc(sizeof(*link), GFP_KERNEL);
942 if (!link)
943 return -ENOMEM;
944
945 link->file = file;
946 list_add_tail_rcu(&link->list, &tu->tp.files);
947
948 tu->tp.flags |= TP_FLAG_TRACE;
Oleg Nesterov48212542014-06-27 19:01:36 +0200949 } else {
950 if (tu->tp.flags & TP_FLAG_TRACE)
951 return -EINTR;
952
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900953 tu->tp.flags |= TP_FLAG_PROFILE;
Oleg Nesterov48212542014-06-27 19:01:36 +0200954 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530955
Oleg Nesterov736288b2013-02-03 20:58:35 +0100956 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
957
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900958 if (enabled)
959 return 0;
960
Oleg Nesterovfb6bab62014-06-27 19:01:46 +0200961 ret = uprobe_buffer_enable();
962 if (ret)
963 goto err_flags;
964
Oleg Nesterov31ba3342013-02-04 17:11:58 +0100965 tu->consumer.filter = filter;
Song Liu0c92c7a2018-04-23 10:21:34 -0700966 tu->inode = d_real_inode(tu->path.dentry);
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530967 if (tu->ref_ctr_offset) {
968 ret = uprobe_register_refctr(tu->inode, tu->offset,
969 tu->ref_ctr_offset, &tu->consumer);
970 } else {
971 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
972 }
973
Oleg Nesterovfb6bab62014-06-27 19:01:46 +0200974 if (ret)
975 goto err_buffer;
Oleg Nesterov41618242013-01-27 18:36:24 +0100976
Oleg Nesterovfb6bab62014-06-27 19:01:46 +0200977 return 0;
978
979 err_buffer:
980 uprobe_buffer_disable();
981
982 err_flags:
983 if (file) {
984 list_del(&link->list);
985 kfree(link);
986 tu->tp.flags &= ~TP_FLAG_TRACE;
987 } else {
988 tu->tp.flags &= ~TP_FLAG_PROFILE;
989 }
Oleg Nesterov41618242013-01-27 18:36:24 +0100990 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530991}
992
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900993static void
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400994probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530995{
Namhyung Kim14577c32013-07-03 15:42:53 +0900996 if (!trace_probe_is_enabled(&tu->tp))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +0530997 return;
998
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +0900999 if (file) {
1000 struct event_file_link *link;
1001
1002 link = find_event_file_link(&tu->tp, file);
1003 if (!link)
1004 return;
1005
1006 list_del_rcu(&link->list);
1007 /* synchronize with u{,ret}probe_trace_func */
Steven Rostedt (VMware)016f8ff2018-08-09 15:37:59 -04001008 synchronize_rcu();
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001009 kfree(link);
1010
1011 if (!list_empty(&tu->tp.files))
1012 return;
1013 }
1014
Oleg Nesterov736288b2013-02-03 20:58:35 +01001015 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
1016
Oleg Nesterova932b732013-01-31 19:47:23 +01001017 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
Song Liu0c92c7a2018-04-23 10:21:34 -07001018 tu->inode = NULL;
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001019 tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001020
1021 uprobe_buffer_disable();
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301022}
1023
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001024static int uprobe_event_define_fields(struct trace_event_call *event_call)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301025{
Oleg Nesterov457d1772013-03-29 18:26:51 +01001026 int ret, i, size;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301027 struct uprobe_trace_entry_head field;
Oleg Nesterov457d1772013-03-29 18:26:51 +01001028 struct trace_uprobe *tu = event_call->data;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301029
Oleg Nesterov4d1298e2013-03-30 19:23:15 +01001030 if (is_ret_probe(tu)) {
1031 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
1032 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
1033 size = SIZEOF_TRACE_ENTRY(true);
1034 } else {
1035 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
1036 size = SIZEOF_TRACE_ENTRY(false);
1037 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301038 /* Set argument names as fields */
Namhyung Kim14577c32013-07-03 15:42:53 +09001039 for (i = 0; i < tu->tp.nr_args; i++) {
1040 struct probe_arg *parg = &tu->tp.args[i];
1041
1042 ret = trace_define_field(event_call, parg->type->fmttype,
1043 parg->name, size + parg->offset,
1044 parg->type->size, parg->type->is_signed,
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301045 FILTER_OTHER);
1046
1047 if (ret)
1048 return ret;
1049 }
1050 return 0;
1051}
1052
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301053#ifdef CONFIG_PERF_EVENTS
Oleg Nesterov31ba3342013-02-04 17:11:58 +01001054static bool
1055__uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
1056{
1057 struct perf_event *event;
1058
1059 if (filter->nr_systemwide)
1060 return true;
1061
1062 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
Peter Zijlstra50f16a82015-03-05 22:10:19 +01001063 if (event->hw.target->mm == mm)
Oleg Nesterov31ba3342013-02-04 17:11:58 +01001064 return true;
1065 }
1066
1067 return false;
1068}
1069
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001070static inline bool
1071uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1072{
Peter Zijlstra50f16a82015-03-05 22:10:19 +01001073 return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001074}
1075
Oleg Nesterovce5f36a2014-04-24 13:26:01 +02001076static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1077{
1078 bool done;
1079
1080 write_lock(&tu->filter.rwlock);
Peter Zijlstra50f16a82015-03-05 22:10:19 +01001081 if (event->hw.target) {
Oleg Nesterovce5f36a2014-04-24 13:26:01 +02001082 list_del(&event->hw.tp_list);
1083 done = tu->filter.nr_systemwide ||
Peter Zijlstra50f16a82015-03-05 22:10:19 +01001084 (event->hw.target->flags & PF_EXITING) ||
Oleg Nesterovce5f36a2014-04-24 13:26:01 +02001085 uprobe_filter_event(tu, event);
1086 } else {
1087 tu->filter.nr_systemwide--;
1088 done = tu->filter.nr_systemwide;
1089 }
1090 write_unlock(&tu->filter.rwlock);
1091
1092 if (!done)
Oleg Nesterov927d6872014-04-24 13:33:31 +02001093 return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
Oleg Nesterovce5f36a2014-04-24 13:26:01 +02001094
1095 return 0;
1096}
1097
Oleg Nesterov736288b2013-02-03 20:58:35 +01001098static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1099{
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001100 bool done;
Oleg Nesterov927d6872014-04-24 13:33:31 +02001101 int err;
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001102
Oleg Nesterov736288b2013-02-03 20:58:35 +01001103 write_lock(&tu->filter.rwlock);
Peter Zijlstra50f16a82015-03-05 22:10:19 +01001104 if (event->hw.target) {
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001105 /*
1106 * event->parent != NULL means copy_process(), we can avoid
1107 * uprobe_apply(). current->mm must be probed and we can rely
1108 * on dup_mmap() which preserves the already installed bp's.
1109 *
1110 * attr.enable_on_exec means that exec/mmap will install the
1111 * breakpoints we need.
1112 */
1113 done = tu->filter.nr_systemwide ||
1114 event->parent || event->attr.enable_on_exec ||
1115 uprobe_filter_event(tu, event);
Oleg Nesterov736288b2013-02-03 20:58:35 +01001116 list_add(&event->hw.tp_list, &tu->filter.perf_events);
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001117 } else {
1118 done = tu->filter.nr_systemwide;
Oleg Nesterov736288b2013-02-03 20:58:35 +01001119 tu->filter.nr_systemwide++;
Oleg Nesterovb2fe8ba2013-02-04 19:05:43 +01001120 }
Oleg Nesterov736288b2013-02-03 20:58:35 +01001121 write_unlock(&tu->filter.rwlock);
1122
Oleg Nesterov927d6872014-04-24 13:33:31 +02001123 err = 0;
1124 if (!done) {
1125 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1126 if (err)
1127 uprobe_perf_close(tu, event);
1128 }
1129 return err;
Oleg Nesterov736288b2013-02-03 20:58:35 +01001130}
1131
Oleg Nesterov31ba3342013-02-04 17:11:58 +01001132static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1133 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1134{
1135 struct trace_uprobe *tu;
1136 int ret;
1137
1138 tu = container_of(uc, struct trace_uprobe, consumer);
1139 read_lock(&tu->filter.rwlock);
1140 ret = __uprobe_perf_filter(&tu->filter, mm);
1141 read_unlock(&tu->filter.rwlock);
1142
1143 return ret;
1144}
1145
Namhyung Kima43b9702014-01-17 17:08:36 +09001146static void __uprobe_perf_func(struct trace_uprobe *tu,
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001147 unsigned long func, struct pt_regs *regs,
1148 struct uprobe_cpu_buffer *ucb, int dsize)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301149{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001150 struct trace_event_call *call = &tu->tp.call;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301151 struct uprobe_trace_entry_head *entry;
1152 struct hlist_head *head;
Oleg Nesterov457d1772013-03-29 18:26:51 +01001153 void *data;
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001154 int size, esize;
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001155 int rctx;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301156
Yonghong Songe87c6bc2017-10-23 23:53:08 -07001157 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
Wang Nan04a22fa2015-07-01 02:13:50 +00001158 return;
1159
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001160 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1161
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001162 size = esize + tu->tp.size + dsize;
1163 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1164 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1165 return;
1166
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301167 preempt_disable();
Oleg Nesterov515619f2013-04-13 15:36:49 +02001168 head = this_cpu_ptr(call->perf_events);
1169 if (hlist_empty(head))
1170 goto out;
1171
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001172 entry = perf_trace_buf_alloc(size, NULL, &rctx);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301173 if (!entry)
1174 goto out;
1175
Oleg Nesterov393a7362013-03-30 18:46:22 +01001176 if (is_ret_probe(tu)) {
1177 entry->vaddr[0] = func;
Oleg Nesterov32520b22013-04-10 16:25:49 +02001178 entry->vaddr[1] = instruction_pointer(regs);
Oleg Nesterov393a7362013-03-30 18:46:22 +01001179 data = DATAOF_TRACE_ENTRY(entry, true);
1180 } else {
Oleg Nesterov32520b22013-04-10 16:25:49 +02001181 entry->vaddr[0] = instruction_pointer(regs);
Oleg Nesterov393a7362013-03-30 18:46:22 +01001182 data = DATAOF_TRACE_ENTRY(entry, false);
1183 }
1184
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001185 memcpy(data, ucb->buf, tu->tp.size + dsize);
Namhyung Kim14577c32013-07-03 15:42:53 +09001186
Namhyung Kimdcad1a22013-07-03 16:40:28 +09001187 if (size - esize > tu->tp.size + dsize) {
1188 int len = tu->tp.size + dsize;
1189
1190 memset(data + len, 0, size - esize - len);
Namhyung Kim14577c32013-07-03 15:42:53 +09001191 }
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301192
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07001193 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02001194 head, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301195 out:
1196 preempt_enable();
Oleg Nesterova51cc602013-03-30 18:02:12 +01001197}
1198
1199/* uprobe profile handler */
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001200static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1201 struct uprobe_cpu_buffer *ucb, int dsize)
Oleg Nesterova51cc602013-03-30 18:02:12 +01001202{
1203 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1204 return UPROBE_HANDLER_REMOVE;
1205
Oleg Nesterov393a7362013-03-30 18:46:22 +01001206 if (!is_ret_probe(tu))
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001207 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
Oleg Nesterovf42d24a2013-02-04 17:48:34 +01001208 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301209}
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001210
1211static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001212 struct pt_regs *regs,
1213 struct uprobe_cpu_buffer *ucb, int dsize)
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001214{
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001215 __uprobe_perf_func(tu, func, regs, ucb, dsize);
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001216}
Yonghong Song41bdc4b2018-05-24 11:21:09 -07001217
1218int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1219 const char **filename, u64 *probe_offset,
1220 bool perf_type_tracepoint)
1221{
1222 const char *pevent = trace_event_name(event->tp_event);
1223 const char *group = event->tp_event->class->system;
1224 struct trace_uprobe *tu;
1225
1226 if (perf_type_tracepoint)
1227 tu = find_probe_event(pevent, group);
1228 else
1229 tu = event->tp_event->data;
1230 if (!tu)
1231 return -EINVAL;
1232
1233 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1234 : BPF_FD_TYPE_UPROBE;
1235 *filename = tu->filename;
1236 *probe_offset = tu->offset;
1237 return 0;
1238}
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301239#endif /* CONFIG_PERF_EVENTS */
1240
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001241static int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001242trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001243 void *data)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301244{
Oleg Nesterov457d1772013-03-29 18:26:51 +01001245 struct trace_uprobe *tu = event->data;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001246 struct trace_event_file *file = data;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301247
1248 switch (type) {
1249 case TRACE_REG_REGISTER:
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001250 return probe_event_enable(tu, file, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301251
1252 case TRACE_REG_UNREGISTER:
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001253 probe_event_disable(tu, file);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301254 return 0;
1255
1256#ifdef CONFIG_PERF_EVENTS
1257 case TRACE_REG_PERF_REGISTER:
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001258 return probe_event_enable(tu, NULL, uprobe_perf_filter);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301259
1260 case TRACE_REG_PERF_UNREGISTER:
zhangwei(Jovi)70ed91c2014-01-17 17:08:38 +09001261 probe_event_disable(tu, NULL);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301262 return 0;
Oleg Nesterov736288b2013-02-03 20:58:35 +01001263
1264 case TRACE_REG_PERF_OPEN:
1265 return uprobe_perf_open(tu, data);
1266
1267 case TRACE_REG_PERF_CLOSE:
1268 return uprobe_perf_close(tu, data);
1269
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301270#endif
1271 default:
1272 return 0;
1273 }
1274 return 0;
1275}
1276
1277static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1278{
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301279 struct trace_uprobe *tu;
Namhyung Kimb7e0bf32013-11-25 13:42:47 +09001280 struct uprobe_dispatch_data udd;
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001281 struct uprobe_cpu_buffer *ucb;
1282 int dsize, esize;
Oleg Nesterovf42d24a2013-02-04 17:48:34 +01001283 int ret = 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301284
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001285
Oleg Nesterova932b732013-01-31 19:47:23 +01001286 tu = container_of(con, struct trace_uprobe, consumer);
Oleg Nesterov1b47aef2013-01-31 19:55:27 +01001287 tu->nhit++;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301288
Namhyung Kimb7e0bf32013-11-25 13:42:47 +09001289 udd.tu = tu;
1290 udd.bp_addr = instruction_pointer(regs);
1291
1292 current->utask->vaddr = (unsigned long) &udd;
1293
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001294 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1295 return 0;
1296
1297 dsize = __get_data_size(&tu->tp, regs);
1298 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1299
1300 ucb = uprobe_buffer_get();
1301 store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
1302
Namhyung Kim14577c32013-07-03 15:42:53 +09001303 if (tu->tp.flags & TP_FLAG_TRACE)
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001304 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301305
1306#ifdef CONFIG_PERF_EVENTS
Namhyung Kim14577c32013-07-03 15:42:53 +09001307 if (tu->tp.flags & TP_FLAG_PROFILE)
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001308 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301309#endif
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001310 uprobe_buffer_put(ucb);
Oleg Nesterovf42d24a2013-02-04 17:48:34 +01001311 return ret;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301312}
1313
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001314static int uretprobe_dispatcher(struct uprobe_consumer *con,
1315 unsigned long func, struct pt_regs *regs)
1316{
1317 struct trace_uprobe *tu;
Namhyung Kimb7e0bf32013-11-25 13:42:47 +09001318 struct uprobe_dispatch_data udd;
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001319 struct uprobe_cpu_buffer *ucb;
1320 int dsize, esize;
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001321
1322 tu = container_of(con, struct trace_uprobe, consumer);
1323
Namhyung Kimb7e0bf32013-11-25 13:42:47 +09001324 udd.tu = tu;
1325 udd.bp_addr = func;
1326
1327 current->utask->vaddr = (unsigned long) &udd;
1328
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001329 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1330 return 0;
1331
1332 dsize = __get_data_size(&tu->tp, regs);
1333 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1334
1335 ucb = uprobe_buffer_get();
1336 store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
1337
Namhyung Kim14577c32013-07-03 15:42:53 +09001338 if (tu->tp.flags & TP_FLAG_TRACE)
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001339 uretprobe_trace_func(tu, func, regs, ucb, dsize);
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001340
1341#ifdef CONFIG_PERF_EVENTS
Namhyung Kim14577c32013-07-03 15:42:53 +09001342 if (tu->tp.flags & TP_FLAG_PROFILE)
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001343 uretprobe_perf_func(tu, func, regs, ucb, dsize);
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001344#endif
Namhyung Kimdd9fa552014-01-17 17:08:37 +09001345 uprobe_buffer_put(ucb);
Oleg Nesterovc1ae5c72013-03-30 18:25:23 +01001346 return 0;
1347}
1348
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301349static struct trace_event_functions uprobe_funcs = {
1350 .trace = print_uprobe_event
1351};
1352
Song Liu33ea4b22017-12-06 14:45:16 -08001353static inline void init_trace_event_call(struct trace_uprobe *tu,
1354 struct trace_event_call *call)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301355{
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301356 INIT_LIST_HEAD(&call->class->fields);
1357 call->event.funcs = &uprobe_funcs;
1358 call->class->define_fields = uprobe_event_define_fields;
1359
Song Liu33ea4b22017-12-06 14:45:16 -08001360 call->flags = TRACE_EVENT_FL_UPROBE;
1361 call->class->reg = trace_uprobe_register;
1362 call->data = tu;
1363}
1364
1365static int register_uprobe_event(struct trace_uprobe *tu)
1366{
1367 struct trace_event_call *call = &tu->tp.call;
1368 int ret = 0;
1369
1370 init_trace_event_call(tu, call);
1371
Namhyung Kim5bf652a2013-07-03 16:09:02 +09001372 if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301373 return -ENOMEM;
1374
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001375 ret = register_trace_event(&call->event);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301376 if (!ret) {
1377 kfree(call->print_fmt);
1378 return -ENODEV;
1379 }
Oleg Nesterovede392a2014-07-15 20:48:24 +02001380
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301381 ret = trace_add_event_call(call);
1382
1383 if (ret) {
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001384 pr_info("Failed to register uprobe event: %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001385 trace_event_name(call));
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301386 kfree(call->print_fmt);
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001387 unregister_trace_event(&call->event);
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301388 }
1389
1390 return ret;
1391}
1392
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001393static int unregister_uprobe_event(struct trace_uprobe *tu)
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301394{
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001395 int ret;
1396
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301397 /* tu->event is unregistered in trace_remove_event_call() */
Namhyung Kim14577c32013-07-03 15:42:53 +09001398 ret = trace_remove_event_call(&tu->tp.call);
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001399 if (ret)
1400 return ret;
Namhyung Kim14577c32013-07-03 15:42:53 +09001401 kfree(tu->tp.call.print_fmt);
1402 tu->tp.call.print_fmt = NULL;
Steven Rostedt (Red Hat)c6c24012013-07-03 23:33:51 -04001403 return 0;
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301404}
1405
Song Liu33ea4b22017-12-06 14:45:16 -08001406#ifdef CONFIG_PERF_EVENTS
1407struct trace_event_call *
1408create_local_trace_uprobe(char *name, unsigned long offs, bool is_return)
1409{
1410 struct trace_uprobe *tu;
Song Liu33ea4b22017-12-06 14:45:16 -08001411 struct path path;
1412 int ret;
1413
1414 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1415 if (ret)
1416 return ERR_PTR(ret);
1417
Song Liu0c92c7a2018-04-23 10:21:34 -07001418 if (!d_is_reg(path.dentry)) {
1419 path_put(&path);
Song Liu33ea4b22017-12-06 14:45:16 -08001420 return ERR_PTR(-EINVAL);
1421 }
1422
1423 /*
1424 * local trace_kprobes are not added to probe_list, so they are never
1425 * searched in find_trace_kprobe(). Therefore, there is no concern of
1426 * duplicated name "DUMMY_EVENT" here.
1427 */
1428 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1429 is_return);
1430
1431 if (IS_ERR(tu)) {
1432 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1433 (int)PTR_ERR(tu));
Song Liu0c92c7a2018-04-23 10:21:34 -07001434 path_put(&path);
Song Liu33ea4b22017-12-06 14:45:16 -08001435 return ERR_CAST(tu);
1436 }
1437
1438 tu->offset = offs;
Song Liu0c92c7a2018-04-23 10:21:34 -07001439 tu->path = path;
Song Liu33ea4b22017-12-06 14:45:16 -08001440 tu->filename = kstrdup(name, GFP_KERNEL);
1441 init_trace_event_call(tu, &tu->tp.call);
1442
1443 if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1444 ret = -ENOMEM;
1445 goto error;
1446 }
1447
1448 return &tu->tp.call;
1449error:
1450 free_trace_uprobe(tu);
1451 return ERR_PTR(ret);
1452}
1453
1454void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1455{
1456 struct trace_uprobe *tu;
1457
1458 tu = container_of(event_call, struct trace_uprobe, tp.call);
1459
1460 kfree(tu->tp.call.print_fmt);
1461 tu->tp.call.print_fmt = NULL;
1462
1463 free_trace_uprobe(tu);
1464}
1465#endif /* CONFIG_PERF_EVENTS */
1466
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301467/* Make a trace interface for controling probe points */
1468static __init int init_uprobe_trace(void)
1469{
1470 struct dentry *d_tracer;
1471
1472 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05001473 if (IS_ERR(d_tracer))
Srikar Dronamrajuf3f096c2012-04-11 16:00:43 +05301474 return 0;
1475
1476 trace_create_file("uprobe_events", 0644, d_tracer,
1477 NULL, &uprobe_events_ops);
1478 /* Profile interface */
1479 trace_create_file("uprobe_profile", 0444, d_tracer,
1480 NULL, &uprobe_profile_ops);
1481 return 0;
1482}
1483
1484fs_initcall(init_uprobe_trace);