Commit | Line | Data |
---|---|---|
bcea3f96 | 1 | // SPDX-License-Identifier: GPL-2.0 |
f3f096cf SD |
2 | /* |
3 | * uprobes-based tracing events | |
4 | * | |
f3f096cf SD |
5 | * Copyright (C) IBM Corporation, 2010-2012 |
6 | * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com> | |
7 | */ | |
ea6eb5e7 | 8 | #define pr_fmt(fmt) "trace_uprobe: " fmt |
f3f096cf | 9 | |
aef2feda | 10 | #include <linux/bpf-cgroup.h> |
17911ff3 | 11 | #include <linux/security.h> |
0597c49c | 12 | #include <linux/ctype.h> |
f3f096cf SD |
13 | #include <linux/module.h> |
14 | #include <linux/uaccess.h> | |
15 | #include <linux/uprobes.h> | |
16 | #include <linux/namei.h> | |
b2e902f0 | 17 | #include <linux/string.h> |
b2d09103 | 18 | #include <linux/rculist.h> |
8c7dcb84 | 19 | #include <linux/filter.h> |
f3f096cf | 20 | |
0597c49c | 21 | #include "trace_dynevent.h" |
f3f096cf | 22 | #include "trace_probe.h" |
53305928 | 23 | #include "trace_probe_tmpl.h" |
f3f096cf SD |
24 | |
25 | #define UPROBE_EVENT_SYSTEM "uprobes" | |
26 | ||
457d1772 ON |
27 | struct uprobe_trace_entry_head { |
28 | struct trace_entry ent; | |
29 | unsigned long vaddr[]; | |
30 | }; | |
31 | ||
32 | #define SIZEOF_TRACE_ENTRY(is_return) \ | |
33 | (sizeof(struct uprobe_trace_entry_head) + \ | |
34 | sizeof(unsigned long) * (is_return ? 2 : 1)) | |
35 | ||
36 | #define DATAOF_TRACE_ENTRY(entry, is_return) \ | |
37 | ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return)) | |
38 | ||
d262271d | 39 | static int trace_uprobe_create(const char *raw_command); |
0597c49c MH |
40 | static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev); |
41 | static int trace_uprobe_release(struct dyn_event *ev); | |
42 | static bool trace_uprobe_is_busy(struct dyn_event *ev); | |
43 | static bool trace_uprobe_match(const char *system, const char *event, | |
30199137 | 44 | int argc, const char **argv, struct dyn_event *ev); |
0597c49c MH |
45 | |
46 | static struct dyn_event_operations trace_uprobe_ops = { | |
47 | .create = trace_uprobe_create, | |
48 | .show = trace_uprobe_show, | |
49 | .is_busy = trace_uprobe_is_busy, | |
50 | .free = trace_uprobe_release, | |
51 | .match = trace_uprobe_match, | |
52 | }; | |
53 | ||
f3f096cf SD |
54 | /* |
55 | * uprobe event core functions | |
56 | */ | |
f3f096cf | 57 | struct trace_uprobe { |
0597c49c | 58 | struct dyn_event devent; |
a932b738 | 59 | struct uprobe_consumer consumer; |
0c92c7a3 | 60 | struct path path; |
f3f096cf SD |
61 | struct inode *inode; |
62 | char *filename; | |
63 | unsigned long offset; | |
1cc33161 | 64 | unsigned long ref_ctr_offset; |
f3f096cf | 65 | unsigned long nhit; |
14577c39 | 66 | struct trace_probe tp; |
f3f096cf SD |
67 | }; |
68 | ||
0597c49c MH |
69 | static bool is_trace_uprobe(struct dyn_event *ev) |
70 | { | |
71 | return ev->ops == &trace_uprobe_ops; | |
72 | } | |
73 | ||
74 | static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev) | |
75 | { | |
76 | return container_of(ev, struct trace_uprobe, devent); | |
77 | } | |
78 | ||
79 | /** | |
80 | * for_each_trace_uprobe - iterate over the trace_uprobe list | |
81 | * @pos: the struct trace_uprobe * for each entry | |
82 | * @dpos: the struct dyn_event * to use as a loop cursor | |
83 | */ | |
84 | #define for_each_trace_uprobe(pos, dpos) \ | |
85 | for_each_dyn_event(dpos) \ | |
86 | if (is_trace_uprobe(dpos) && (pos = to_trace_uprobe(dpos))) | |
87 | ||
f3f096cf | 88 | static int register_uprobe_event(struct trace_uprobe *tu); |
c6c2401d | 89 | static int unregister_uprobe_event(struct trace_uprobe *tu); |
f3f096cf | 90 | |
f3f096cf | 91 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs); |
c1ae5c75 ON |
92 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
93 | unsigned long func, struct pt_regs *regs); | |
f3f096cf | 94 | |
3fd996a2 NK |
95 | #ifdef CONFIG_STACK_GROWSUP |
96 | static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) | |
97 | { | |
98 | return addr - (n * sizeof(long)); | |
99 | } | |
100 | #else | |
101 | static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) | |
102 | { | |
103 | return addr + (n * sizeof(long)); | |
104 | } | |
105 | #endif | |
106 | ||
107 | static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n) | |
108 | { | |
109 | unsigned long ret; | |
110 | unsigned long addr = user_stack_pointer(regs); | |
111 | ||
112 | addr = adjust_stack_addr(addr, n); | |
113 | ||
114 | if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret))) | |
115 | return 0; | |
116 | ||
117 | return ret; | |
118 | } | |
119 | ||
120 | /* | |
121 | * Uprobes-specific fetch functions | |
122 | */ | |
53305928 | 123 | static nokprobe_inline int |
9b960a38 | 124 | probe_mem_read(void *dest, void *src, size_t size) |
53305928 MH |
125 | { |
126 | void __user *vaddr = (void __force __user *)src; | |
127 | ||
f3f58935 | 128 | return copy_from_user(dest, vaddr, size) ? -EFAULT : 0; |
5baaa59e | 129 | } |
e65f7ae7 MH |
130 | |
131 | static nokprobe_inline int | |
132 | probe_mem_read_user(void *dest, void *src, size_t size) | |
133 | { | |
134 | return probe_mem_read(dest, src, size); | |
135 | } | |
136 | ||
5baaa59e NK |
137 | /* |
138 | * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max | |
139 | * length and relative data location. | |
140 | */ | |
9178412d MH |
141 | static nokprobe_inline int |
142 | fetch_store_string(unsigned long addr, void *dest, void *base) | |
5baaa59e NK |
143 | { |
144 | long ret; | |
9178412d MH |
145 | u32 loc = *(u32 *)dest; |
146 | int maxlen = get_loc_len(loc); | |
147 | u8 *dst = get_loc_data(dest, base); | |
5baaa59e NK |
148 | void __user *src = (void __force __user *) addr; |
149 | ||
9178412d MH |
150 | if (unlikely(!maxlen)) |
151 | return -ENOMEM; | |
5baaa59e | 152 | |
4dd537ac MH |
153 | if (addr == FETCH_TOKEN_COMM) |
154 | ret = strlcpy(dst, current->comm, maxlen); | |
155 | else | |
156 | ret = strncpy_from_user(dst, src, maxlen); | |
9178412d MH |
157 | if (ret >= 0) { |
158 | if (ret == maxlen) | |
159 | dst[ret - 1] = '\0'; | |
0722069a AZ |
160 | else |
161 | /* | |
162 | * Include the terminating null byte. In this case it | |
163 | * was copied by strncpy_from_user but not accounted | |
164 | * for in ret. | |
165 | */ | |
166 | ret++; | |
9178412d | 167 | *(u32 *)dest = make_data_loc(ret, (void *)dst - base); |
797311bc MHG |
168 | } else |
169 | *(u32 *)dest = make_data_loc(0, (void *)dst - base); | |
9178412d MH |
170 | |
171 | return ret; | |
5baaa59e NK |
172 | } |
173 | ||
88903c46 MH |
174 | static nokprobe_inline int |
175 | fetch_store_string_user(unsigned long addr, void *dest, void *base) | |
176 | { | |
177 | return fetch_store_string(addr, dest, base); | |
178 | } | |
179 | ||
53305928 | 180 | /* Return the length of string -- including null terminal byte */ |
9178412d MH |
181 | static nokprobe_inline int |
182 | fetch_store_strlen(unsigned long addr) | |
5baaa59e NK |
183 | { |
184 | int len; | |
185 | void __user *vaddr = (void __force __user *) addr; | |
186 | ||
4dd537ac MH |
187 | if (addr == FETCH_TOKEN_COMM) |
188 | len = strlen(current->comm) + 1; | |
189 | else | |
190 | len = strnlen_user(vaddr, MAX_STRING_SIZE); | |
5baaa59e | 191 | |
9178412d | 192 | return (len > MAX_STRING_SIZE) ? 0 : len; |
5baaa59e | 193 | } |
3fd996a2 | 194 | |
88903c46 MH |
195 | static nokprobe_inline int |
196 | fetch_store_strlen_user(unsigned long addr) | |
197 | { | |
198 | return fetch_store_strlen(addr); | |
199 | } | |
200 | ||
53305928 | 201 | static unsigned long translate_user_vaddr(unsigned long file_offset) |
b7e0bf34 NK |
202 | { |
203 | unsigned long base_addr; | |
204 | struct uprobe_dispatch_data *udd; | |
205 | ||
206 | udd = (void *) current->utask->vaddr; | |
207 | ||
208 | base_addr = udd->bp_addr - udd->tu->offset; | |
53305928 | 209 | return base_addr + file_offset; |
b7e0bf34 | 210 | } |
b7e0bf34 | 211 | |
53305928 MH |
212 | /* Note that we don't verify it, since the code does not come from user space */ |
213 | static int | |
8565a45d | 214 | process_fetch_insn(struct fetch_insn *code, void *rec, void *dest, |
9178412d | 215 | void *base) |
53305928 | 216 | { |
8565a45d | 217 | struct pt_regs *regs = rec; |
53305928 | 218 | unsigned long val; |
bd78acc8 | 219 | int ret; |
53305928 MH |
220 | |
221 | /* 1st stage: get value from context */ | |
222 | switch (code->op) { | |
223 | case FETCH_OP_REG: | |
224 | val = regs_get_register(regs, code->param); | |
225 | break; | |
226 | case FETCH_OP_STACK: | |
227 | val = get_user_stack_nth(regs, code->param); | |
228 | break; | |
229 | case FETCH_OP_STACKP: | |
230 | val = user_stack_pointer(regs); | |
231 | break; | |
232 | case FETCH_OP_RETVAL: | |
233 | val = regs_return_value(regs); | |
234 | break; | |
4dd537ac MH |
235 | case FETCH_OP_COMM: |
236 | val = FETCH_TOKEN_COMM; | |
237 | break; | |
53305928 MH |
238 | case FETCH_OP_FOFFS: |
239 | val = translate_user_vaddr(code->immediate); | |
240 | break; | |
241 | default: | |
bd78acc8 SC |
242 | ret = process_common_fetch_insn(code, &val); |
243 | if (ret < 0) | |
244 | return ret; | |
53305928 MH |
245 | } |
246 | code++; | |
247 | ||
9b960a38 | 248 | return process_fetch_insn_bottom(code, val, dest, base); |
53305928 MH |
249 | } |
250 | NOKPROBE_SYMBOL(process_fetch_insn) | |
251 | ||
736288ba ON |
252 | static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter) |
253 | { | |
254 | rwlock_init(&filter->rwlock); | |
255 | filter->nr_systemwide = 0; | |
256 | INIT_LIST_HEAD(&filter->perf_events); | |
257 | } | |
258 | ||
259 | static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter) | |
260 | { | |
261 | return !filter->nr_systemwide && list_empty(&filter->perf_events); | |
262 | } | |
263 | ||
c1ae5c75 ON |
264 | static inline bool is_ret_probe(struct trace_uprobe *tu) |
265 | { | |
266 | return tu->consumer.ret_handler != NULL; | |
267 | } | |
268 | ||
0597c49c MH |
269 | static bool trace_uprobe_is_busy(struct dyn_event *ev) |
270 | { | |
271 | struct trace_uprobe *tu = to_trace_uprobe(ev); | |
272 | ||
273 | return trace_probe_is_enabled(&tu->tp); | |
274 | } | |
275 | ||
ab10d69e MH |
276 | static bool trace_uprobe_match_command_head(struct trace_uprobe *tu, |
277 | int argc, const char **argv) | |
278 | { | |
279 | char buf[MAX_ARGSTR_LEN + 1]; | |
280 | int len; | |
281 | ||
282 | if (!argc) | |
283 | return true; | |
284 | ||
285 | len = strlen(tu->filename); | |
286 | if (strncmp(tu->filename, argv[0], len) || argv[0][len] != ':') | |
287 | return false; | |
288 | ||
289 | if (tu->ref_ctr_offset == 0) | |
290 | snprintf(buf, sizeof(buf), "0x%0*lx", | |
291 | (int)(sizeof(void *) * 2), tu->offset); | |
292 | else | |
293 | snprintf(buf, sizeof(buf), "0x%0*lx(0x%lx)", | |
294 | (int)(sizeof(void *) * 2), tu->offset, | |
295 | tu->ref_ctr_offset); | |
296 | if (strcmp(buf, &argv[0][len + 1])) | |
297 | return false; | |
298 | ||
299 | argc--; argv++; | |
300 | ||
301 | return trace_probe_match_command_args(&tu->tp, argc, argv); | |
302 | } | |
303 | ||
0597c49c | 304 | static bool trace_uprobe_match(const char *system, const char *event, |
30199137 | 305 | int argc, const char **argv, struct dyn_event *ev) |
0597c49c MH |
306 | { |
307 | struct trace_uprobe *tu = to_trace_uprobe(ev); | |
308 | ||
95c104c3 LY |
309 | return (event[0] == '\0' || |
310 | strcmp(trace_probe_name(&tu->tp), event) == 0) && | |
ab10d69e MH |
311 | (!system || strcmp(trace_probe_group_name(&tu->tp), system) == 0) && |
312 | trace_uprobe_match_command_head(tu, argc, argv); | |
0597c49c MH |
313 | } |
314 | ||
60d53e2c MH |
315 | static nokprobe_inline struct trace_uprobe * |
316 | trace_uprobe_primary_from_call(struct trace_event_call *call) | |
317 | { | |
318 | struct trace_probe *tp; | |
319 | ||
320 | tp = trace_probe_primary_from_call(call); | |
321 | if (WARN_ON_ONCE(!tp)) | |
322 | return NULL; | |
323 | ||
324 | return container_of(tp, struct trace_uprobe, tp); | |
325 | } | |
326 | ||
f3f096cf SD |
327 | /* |
328 | * Allocate new trace_uprobe and initialize it (including uprobes). | |
329 | */ | |
330 | static struct trace_uprobe * | |
c1ae5c75 | 331 | alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret) |
f3f096cf SD |
332 | { |
333 | struct trace_uprobe *tu; | |
455b2899 | 334 | int ret; |
f3f096cf | 335 | |
845cbf3e | 336 | tu = kzalloc(struct_size(tu, tp.args, nargs), GFP_KERNEL); |
f3f096cf SD |
337 | if (!tu) |
338 | return ERR_PTR(-ENOMEM); | |
339 | ||
b61387cb | 340 | ret = trace_probe_init(&tu->tp, event, group, true); |
455b2899 | 341 | if (ret < 0) |
f3f096cf SD |
342 | goto error; |
343 | ||
0597c49c | 344 | dyn_event_init(&tu->devent, &trace_uprobe_ops); |
a932b738 | 345 | tu->consumer.handler = uprobe_dispatcher; |
c1ae5c75 ON |
346 | if (is_ret) |
347 | tu->consumer.ret_handler = uretprobe_dispatcher; | |
b61387cb | 348 | init_trace_uprobe_filter(tu->tp.event->filter); |
f3f096cf SD |
349 | return tu; |
350 | ||
351 | error: | |
f3f096cf SD |
352 | kfree(tu); |
353 | ||
455b2899 | 354 | return ERR_PTR(ret); |
f3f096cf SD |
355 | } |
356 | ||
357 | static void free_trace_uprobe(struct trace_uprobe *tu) | |
358 | { | |
0597c49c MH |
359 | if (!tu) |
360 | return; | |
361 | ||
0c92c7a3 | 362 | path_put(&tu->path); |
455b2899 | 363 | trace_probe_cleanup(&tu->tp); |
f3f096cf SD |
364 | kfree(tu->filename); |
365 | kfree(tu); | |
366 | } | |
367 | ||
368 | static struct trace_uprobe *find_probe_event(const char *event, const char *group) | |
369 | { | |
0597c49c | 370 | struct dyn_event *pos; |
f3f096cf SD |
371 | struct trace_uprobe *tu; |
372 | ||
0597c49c | 373 | for_each_trace_uprobe(tu, pos) |
b55ce203 MH |
374 | if (strcmp(trace_probe_name(&tu->tp), event) == 0 && |
375 | strcmp(trace_probe_group_name(&tu->tp), group) == 0) | |
f3f096cf SD |
376 | return tu; |
377 | ||
378 | return NULL; | |
379 | } | |
380 | ||
0597c49c | 381 | /* Unregister a trace_uprobe and probe_event */ |
c6c2401d | 382 | static int unregister_trace_uprobe(struct trace_uprobe *tu) |
f3f096cf | 383 | { |
c6c2401d SRRH |
384 | int ret; |
385 | ||
41af3cf5 MH |
386 | if (trace_probe_has_sibling(&tu->tp)) |
387 | goto unreg; | |
388 | ||
1d18538e SRV |
389 | /* If there's a reference to the dynamic event */ |
390 | if (trace_event_dyn_busy(trace_probe_event_call(&tu->tp))) | |
391 | return -EBUSY; | |
392 | ||
c6c2401d SRRH |
393 | ret = unregister_uprobe_event(tu); |
394 | if (ret) | |
395 | return ret; | |
396 | ||
41af3cf5 | 397 | unreg: |
0597c49c | 398 | dyn_event_remove(&tu->devent); |
41af3cf5 | 399 | trace_probe_unlink(&tu->tp); |
f3f096cf | 400 | free_trace_uprobe(tu); |
c6c2401d | 401 | return 0; |
f3f096cf SD |
402 | } |
403 | ||
fe60b0ce MH |
404 | static bool trace_uprobe_has_same_uprobe(struct trace_uprobe *orig, |
405 | struct trace_uprobe *comp) | |
406 | { | |
407 | struct trace_probe_event *tpe = orig->tp.event; | |
fe60b0ce MH |
408 | struct inode *comp_inode = d_real_inode(comp->path.dentry); |
409 | int i; | |
410 | ||
e161c6bf | 411 | list_for_each_entry(orig, &tpe->probes, tp.list) { |
fe60b0ce MH |
412 | if (comp_inode != d_real_inode(orig->path.dentry) || |
413 | comp->offset != orig->offset) | |
414 | continue; | |
415 | ||
416 | /* | |
417 | * trace_probe_compare_arg_type() ensured that nr_args and | |
418 | * each argument name and type are same. Let's compare comm. | |
419 | */ | |
420 | for (i = 0; i < orig->tp.nr_args; i++) { | |
421 | if (strcmp(orig->tp.args[i].comm, | |
422 | comp->tp.args[i].comm)) | |
f8d7ab2b | 423 | break; |
fe60b0ce MH |
424 | } |
425 | ||
f8d7ab2b SD |
426 | if (i == orig->tp.nr_args) |
427 | return true; | |
fe60b0ce MH |
428 | } |
429 | ||
430 | return false; | |
431 | } | |
432 | ||
41af3cf5 MH |
433 | static int append_trace_uprobe(struct trace_uprobe *tu, struct trace_uprobe *to) |
434 | { | |
435 | int ret; | |
436 | ||
fe60b0ce MH |
437 | ret = trace_probe_compare_arg_type(&tu->tp, &to->tp); |
438 | if (ret) { | |
439 | /* Note that argument starts index = 2 */ | |
440 | trace_probe_log_set_index(ret + 1); | |
441 | trace_probe_log_err(0, DIFF_ARG_TYPE); | |
442 | return -EEXIST; | |
443 | } | |
444 | if (trace_uprobe_has_same_uprobe(to, tu)) { | |
445 | trace_probe_log_set_index(0); | |
446 | trace_probe_log_err(0, SAME_PROBE); | |
447 | return -EEXIST; | |
448 | } | |
449 | ||
41af3cf5 MH |
450 | /* Append to existing event */ |
451 | ret = trace_probe_append(&tu->tp, &to->tp); | |
452 | if (!ret) | |
8b0e6c74 | 453 | dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp)); |
41af3cf5 MH |
454 | |
455 | return ret; | |
456 | } | |
457 | ||
ccea8727 RB |
458 | /* |
459 | * Uprobe with multiple reference counter is not allowed. i.e. | |
460 | * If inode and offset matches, reference counter offset *must* | |
461 | * match as well. Though, there is one exception: If user is | |
462 | * replacing old trace_uprobe with new one(same group/event), | |
463 | * then we allow same uprobe with new reference counter as far | |
464 | * as the new one does not conflict with any other existing | |
465 | * ones. | |
466 | */ | |
41af3cf5 | 467 | static int validate_ref_ctr_offset(struct trace_uprobe *new) |
ccea8727 | 468 | { |
0597c49c | 469 | struct dyn_event *pos; |
41af3cf5 | 470 | struct trace_uprobe *tmp; |
ccea8727 RB |
471 | struct inode *new_inode = d_real_inode(new->path.dentry); |
472 | ||
0597c49c | 473 | for_each_trace_uprobe(tmp, pos) { |
41af3cf5 | 474 | if (new_inode == d_real_inode(tmp->path.dentry) && |
ccea8727 RB |
475 | new->offset == tmp->offset && |
476 | new->ref_ctr_offset != tmp->ref_ctr_offset) { | |
477 | pr_warn("Reference counter offset mismatch."); | |
41af3cf5 | 478 | return -EINVAL; |
ccea8727 RB |
479 | } |
480 | } | |
41af3cf5 | 481 | return 0; |
ccea8727 RB |
482 | } |
483 | ||
f3f096cf SD |
484 | /* Register a trace_uprobe and probe_event */ |
485 | static int register_trace_uprobe(struct trace_uprobe *tu) | |
486 | { | |
14577c39 | 487 | struct trace_uprobe *old_tu; |
f3f096cf SD |
488 | int ret; |
489 | ||
0597c49c | 490 | mutex_lock(&event_mutex); |
f3f096cf | 491 | |
41af3cf5 MH |
492 | ret = validate_ref_ctr_offset(tu); |
493 | if (ret) | |
ccea8727 | 494 | goto end; |
ccea8727 | 495 | |
41af3cf5 MH |
496 | /* register as an event */ |
497 | old_tu = find_probe_event(trace_probe_name(&tu->tp), | |
498 | trace_probe_group_name(&tu->tp)); | |
14577c39 | 499 | if (old_tu) { |
41af3cf5 MH |
500 | if (is_ret_probe(tu) != is_ret_probe(old_tu)) { |
501 | trace_probe_log_set_index(0); | |
502 | trace_probe_log_err(0, DIFF_PROBE_TYPE); | |
503 | ret = -EEXIST; | |
504 | } else { | |
fe60b0ce | 505 | ret = append_trace_uprobe(tu, old_tu); |
41af3cf5 MH |
506 | } |
507 | goto end; | |
c6c2401d | 508 | } |
f3f096cf SD |
509 | |
510 | ret = register_uprobe_event(tu); | |
511 | if (ret) { | |
8e242060 MH |
512 | if (ret == -EEXIST) { |
513 | trace_probe_log_set_index(0); | |
514 | trace_probe_log_err(0, EVENT_EXIST); | |
515 | } else | |
516 | pr_warn("Failed to register probe event(%d)\n", ret); | |
f3f096cf SD |
517 | goto end; |
518 | } | |
519 | ||
8b0e6c74 | 520 | dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp)); |
f3f096cf SD |
521 | |
522 | end: | |
0597c49c | 523 | mutex_unlock(&event_mutex); |
f3f096cf SD |
524 | |
525 | return ret; | |
526 | } | |
527 | ||
528 | /* | |
529 | * Argument syntax: | |
95c104c3 | 530 | * - Add uprobe: p|r[:[GRP/][EVENT]] PATH:OFFSET[%return][(REF)] [FETCHARGS] |
f3f096cf | 531 | */ |
d262271d | 532 | static int __trace_uprobe_create(int argc, const char **argv) |
f3f096cf SD |
533 | { |
534 | struct trace_uprobe *tu; | |
0597c49c MH |
535 | const char *event = NULL, *group = UPROBE_EVENT_SYSTEM; |
536 | char *arg, *filename, *rctr, *rctr_end, *tmp; | |
f3f096cf | 537 | char buf[MAX_EVENT_NAME_LEN]; |
95c104c3 | 538 | char gbuf[MAX_EVENT_NAME_LEN]; |
007517a0 | 539 | enum probe_print_type ptype; |
f3f096cf | 540 | struct path path; |
1cc33161 | 541 | unsigned long offset, ref_ctr_offset; |
0597c49c | 542 | bool is_return = false; |
f3f096cf SD |
543 | int i, ret; |
544 | ||
1cc33161 | 545 | ref_ctr_offset = 0; |
f3f096cf | 546 | |
f01098c7 ET |
547 | switch (argv[0][0]) { |
548 | case 'r': | |
4ee5a52e | 549 | is_return = true; |
f01098c7 ET |
550 | break; |
551 | case 'p': | |
552 | break; | |
553 | default: | |
554 | return -ECANCELED; | |
555 | } | |
556 | ||
557 | if (argc < 2) | |
0597c49c | 558 | return -ECANCELED; |
f3f096cf | 559 | |
0597c49c | 560 | if (argv[0][1] == ':') |
f3f096cf | 561 | event = &argv[0][2]; |
f3f096cf | 562 | |
0597c49c MH |
563 | if (!strchr(argv[1], '/')) |
564 | return -ECANCELED; | |
f3f096cf | 565 | |
0597c49c MH |
566 | filename = kstrdup(argv[1], GFP_KERNEL); |
567 | if (!filename) | |
568 | return -ENOMEM; | |
f3f096cf | 569 | |
6496bb72 | 570 | /* Find the last occurrence, in case the path contains ':' too. */ |
0597c49c MH |
571 | arg = strrchr(filename, ':'); |
572 | if (!arg || !isdigit(arg[1])) { | |
573 | kfree(filename); | |
574 | return -ECANCELED; | |
575 | } | |
f3f096cf | 576 | |
ab105a4f MH |
577 | trace_probe_log_init("trace_uprobe", argc, argv); |
578 | trace_probe_log_set_index(1); /* filename is the 2nd argument */ | |
579 | ||
f3f096cf | 580 | *arg++ = '\0'; |
f3f096cf | 581 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); |
0597c49c | 582 | if (ret) { |
ab105a4f | 583 | trace_probe_log_err(0, FILE_NOT_FOUND); |
0597c49c | 584 | kfree(filename); |
ab105a4f | 585 | trace_probe_log_clear(); |
0c92c7a3 | 586 | return ret; |
0597c49c | 587 | } |
0c92c7a3 | 588 | if (!d_is_reg(path.dentry)) { |
ab105a4f | 589 | trace_probe_log_err(0, NO_REGULAR_FILE); |
d24d7dbf JZ |
590 | ret = -EINVAL; |
591 | goto fail_address_parse; | |
592 | } | |
f3f096cf | 593 | |
1cc33161 RB |
594 | /* Parse reference counter offset if specified. */ |
595 | rctr = strchr(arg, '('); | |
596 | if (rctr) { | |
597 | rctr_end = strchr(rctr, ')'); | |
ab105a4f MH |
598 | if (!rctr_end) { |
599 | ret = -EINVAL; | |
600 | rctr_end = rctr + strlen(rctr); | |
601 | trace_probe_log_err(rctr_end - filename, | |
602 | REFCNT_OPEN_BRACE); | |
603 | goto fail_address_parse; | |
604 | } else if (rctr_end[1] != '\0') { | |
1cc33161 | 605 | ret = -EINVAL; |
ab105a4f MH |
606 | trace_probe_log_err(rctr_end + 1 - filename, |
607 | BAD_REFCNT_SUFFIX); | |
1cc33161 RB |
608 | goto fail_address_parse; |
609 | } | |
610 | ||
611 | *rctr++ = '\0'; | |
612 | *rctr_end = '\0'; | |
613 | ret = kstrtoul(rctr, 0, &ref_ctr_offset); | |
614 | if (ret) { | |
ab105a4f | 615 | trace_probe_log_err(rctr - filename, BAD_REFCNT); |
1cc33161 RB |
616 | goto fail_address_parse; |
617 | } | |
618 | } | |
619 | ||
3dd3aae3 MH |
620 | /* Check if there is %return suffix */ |
621 | tmp = strchr(arg, '%'); | |
622 | if (tmp) { | |
623 | if (!strcmp(tmp, "%return")) { | |
624 | *tmp = '\0'; | |
625 | is_return = true; | |
626 | } else { | |
627 | trace_probe_log_err(tmp - filename, BAD_ADDR_SUFFIX); | |
628 | ret = -EINVAL; | |
629 | goto fail_address_parse; | |
630 | } | |
631 | } | |
632 | ||
1cc33161 | 633 | /* Parse uprobe offset. */ |
84d7ed79 | 634 | ret = kstrtoul(arg, 0, &offset); |
ab105a4f MH |
635 | if (ret) { |
636 | trace_probe_log_err(arg - filename, BAD_UPROBE_OFFS); | |
84d7ed79 | 637 | goto fail_address_parse; |
ab105a4f | 638 | } |
f3f096cf SD |
639 | |
640 | /* setup a probe */ | |
ab105a4f | 641 | trace_probe_log_set_index(0); |
0597c49c | 642 | if (event) { |
95c104c3 | 643 | ret = traceprobe_parse_event_name(&event, &group, gbuf, |
ab105a4f | 644 | event - argv[0]); |
0597c49c MH |
645 | if (ret) |
646 | goto fail_address_parse; | |
95c104c3 LY |
647 | } |
648 | ||
649 | if (!event) { | |
b2e902f0 | 650 | char *tail; |
f3f096cf SD |
651 | char *ptr; |
652 | ||
b2e902f0 AS |
653 | tail = kstrdup(kbasename(filename), GFP_KERNEL); |
654 | if (!tail) { | |
f3f096cf SD |
655 | ret = -ENOMEM; |
656 | goto fail_address_parse; | |
657 | } | |
658 | ||
f3f096cf SD |
659 | ptr = strpbrk(tail, ".-_"); |
660 | if (ptr) | |
661 | *ptr = '\0'; | |
662 | ||
663 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset); | |
664 | event = buf; | |
665 | kfree(tail); | |
666 | } | |
667 | ||
ab105a4f MH |
668 | argc -= 2; |
669 | argv += 2; | |
670 | ||
4ee5a52e | 671 | tu = alloc_trace_uprobe(group, event, argc, is_return); |
f3f096cf | 672 | if (IS_ERR(tu)) { |
f3f096cf | 673 | ret = PTR_ERR(tu); |
a039480e MH |
674 | /* This must return -ENOMEM otherwise there is a bug */ |
675 | WARN_ON_ONCE(ret != -ENOMEM); | |
f3f096cf SD |
676 | goto fail_address_parse; |
677 | } | |
678 | tu->offset = offset; | |
1cc33161 | 679 | tu->ref_ctr_offset = ref_ctr_offset; |
0c92c7a3 | 680 | tu->path = path; |
0597c49c | 681 | tu->filename = filename; |
f3f096cf SD |
682 | |
683 | /* parse arguments */ | |
f3f096cf | 684 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { |
1b8b0cd7 MHG |
685 | struct traceprobe_parse_context ctx = { |
686 | .flags = (is_return ? TPARG_FL_RETURN : 0) | TPARG_FL_USER, | |
687 | }; | |
688 | ||
ab105a4f | 689 | trace_probe_log_set_index(i + 2); |
1b8b0cd7 | 690 | ret = traceprobe_parse_probe_arg(&tu->tp, i, argv[i], &ctx); |
d00bbea9 | 691 | if (ret) |
f3f096cf | 692 | goto error; |
f3f096cf SD |
693 | } |
694 | ||
007517a0 SRV |
695 | ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL; |
696 | ret = traceprobe_set_print_fmt(&tu->tp, ptype); | |
b4d4b96b MH |
697 | if (ret < 0) |
698 | goto error; | |
699 | ||
f3f096cf | 700 | ret = register_trace_uprobe(tu); |
ab105a4f MH |
701 | if (!ret) |
702 | goto out; | |
f3f096cf SD |
703 | |
704 | error: | |
705 | free_trace_uprobe(tu); | |
ab105a4f MH |
706 | out: |
707 | trace_probe_log_clear(); | |
f3f096cf SD |
708 | return ret; |
709 | ||
710 | fail_address_parse: | |
ab105a4f | 711 | trace_probe_log_clear(); |
0c92c7a3 | 712 | path_put(&path); |
0597c49c | 713 | kfree(filename); |
f3f096cf | 714 | |
f3f096cf SD |
715 | return ret; |
716 | } | |
717 | ||
d262271d MH |
718 | int trace_uprobe_create(const char *raw_command) |
719 | { | |
720 | return trace_probe_create(raw_command, __trace_uprobe_create); | |
721 | } | |
722 | ||
723 | static int create_or_delete_trace_uprobe(const char *raw_command) | |
f3f096cf | 724 | { |
0597c49c | 725 | int ret; |
f3f096cf | 726 | |
d262271d MH |
727 | if (raw_command[0] == '-') |
728 | return dyn_event_release(raw_command, &trace_uprobe_ops); | |
f3f096cf | 729 | |
d262271d | 730 | ret = trace_uprobe_create(raw_command); |
0597c49c | 731 | return ret == -ECANCELED ? -EINVAL : ret; |
f3f096cf SD |
732 | } |
733 | ||
0597c49c | 734 | static int trace_uprobe_release(struct dyn_event *ev) |
f3f096cf | 735 | { |
0597c49c | 736 | struct trace_uprobe *tu = to_trace_uprobe(ev); |
f3f096cf | 737 | |
0597c49c | 738 | return unregister_trace_uprobe(tu); |
f3f096cf SD |
739 | } |
740 | ||
0597c49c MH |
741 | /* Probes listing interfaces */ |
742 | static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev) | |
f3f096cf | 743 | { |
0597c49c | 744 | struct trace_uprobe *tu = to_trace_uprobe(ev); |
3ede82dd | 745 | char c = is_ret_probe(tu) ? 'r' : 'p'; |
f3f096cf SD |
746 | int i; |
747 | ||
b55ce203 MH |
748 | seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, trace_probe_group_name(&tu->tp), |
749 | trace_probe_name(&tu->tp), tu->filename, | |
a64b2c01 | 750 | (int)(sizeof(void *) * 2), tu->offset); |
f3f096cf | 751 | |
1cc33161 RB |
752 | if (tu->ref_ctr_offset) |
753 | seq_printf(m, "(0x%lx)", tu->ref_ctr_offset); | |
754 | ||
14577c39 NK |
755 | for (i = 0; i < tu->tp.nr_args; i++) |
756 | seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm); | |
f3f096cf | 757 | |
fa6f0cc7 | 758 | seq_putc(m, '\n'); |
f3f096cf SD |
759 | return 0; |
760 | } | |
761 | ||
0597c49c MH |
762 | static int probes_seq_show(struct seq_file *m, void *v) |
763 | { | |
764 | struct dyn_event *ev = v; | |
765 | ||
766 | if (!is_trace_uprobe(ev)) | |
767 | return 0; | |
768 | ||
769 | return trace_uprobe_show(m, ev); | |
770 | } | |
771 | ||
f3f096cf | 772 | static const struct seq_operations probes_seq_op = { |
0597c49c MH |
773 | .start = dyn_event_seq_start, |
774 | .next = dyn_event_seq_next, | |
775 | .stop = dyn_event_seq_stop, | |
776 | .show = probes_seq_show | |
f3f096cf SD |
777 | }; |
778 | ||
779 | static int probes_open(struct inode *inode, struct file *file) | |
780 | { | |
c6c2401d SRRH |
781 | int ret; |
782 | ||
17911ff3 SRV |
783 | ret = security_locked_down(LOCKDOWN_TRACEFS); |
784 | if (ret) | |
785 | return ret; | |
786 | ||
c6c2401d | 787 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { |
0597c49c | 788 | ret = dyn_events_release_all(&trace_uprobe_ops); |
c6c2401d SRRH |
789 | if (ret) |
790 | return ret; | |
791 | } | |
f3f096cf SD |
792 | |
793 | return seq_open(file, &probes_seq_op); | |
794 | } | |
795 | ||
796 | static ssize_t probes_write(struct file *file, const char __user *buffer, | |
797 | size_t count, loff_t *ppos) | |
798 | { | |
0597c49c MH |
799 | return trace_parse_run_command(file, buffer, count, ppos, |
800 | create_or_delete_trace_uprobe); | |
f3f096cf SD |
801 | } |
802 | ||
803 | static const struct file_operations uprobe_events_ops = { | |
804 | .owner = THIS_MODULE, | |
805 | .open = probes_open, | |
806 | .read = seq_read, | |
807 | .llseek = seq_lseek, | |
808 | .release = seq_release, | |
809 | .write = probes_write, | |
810 | }; | |
811 | ||
812 | /* Probes profiling interfaces */ | |
813 | static int probes_profile_seq_show(struct seq_file *m, void *v) | |
814 | { | |
0597c49c MH |
815 | struct dyn_event *ev = v; |
816 | struct trace_uprobe *tu; | |
817 | ||
818 | if (!is_trace_uprobe(ev)) | |
819 | return 0; | |
f3f096cf | 820 | |
0597c49c | 821 | tu = to_trace_uprobe(ev); |
de7b2973 | 822 | seq_printf(m, " %s %-44s %15lu\n", tu->filename, |
b55ce203 | 823 | trace_probe_name(&tu->tp), tu->nhit); |
f3f096cf SD |
824 | return 0; |
825 | } | |
826 | ||
827 | static const struct seq_operations profile_seq_op = { | |
0597c49c MH |
828 | .start = dyn_event_seq_start, |
829 | .next = dyn_event_seq_next, | |
830 | .stop = dyn_event_seq_stop, | |
f3f096cf SD |
831 | .show = probes_profile_seq_show |
832 | }; | |
833 | ||
834 | static int profile_open(struct inode *inode, struct file *file) | |
835 | { | |
17911ff3 SRV |
836 | int ret; |
837 | ||
838 | ret = security_locked_down(LOCKDOWN_TRACEFS); | |
839 | if (ret) | |
840 | return ret; | |
841 | ||
f3f096cf SD |
842 | return seq_open(file, &profile_seq_op); |
843 | } | |
844 | ||
845 | static const struct file_operations uprobe_profile_ops = { | |
846 | .owner = THIS_MODULE, | |
847 | .open = profile_open, | |
848 | .read = seq_read, | |
849 | .llseek = seq_lseek, | |
850 | .release = seq_release, | |
851 | }; | |
852 | ||
dcad1a20 NK |
853 | struct uprobe_cpu_buffer { |
854 | struct mutex mutex; | |
855 | void *buf; | |
856 | }; | |
857 | static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer; | |
858 | static int uprobe_buffer_refcnt; | |
859 | ||
860 | static int uprobe_buffer_init(void) | |
861 | { | |
862 | int cpu, err_cpu; | |
863 | ||
864 | uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer); | |
865 | if (uprobe_cpu_buffer == NULL) | |
866 | return -ENOMEM; | |
867 | ||
868 | for_each_possible_cpu(cpu) { | |
869 | struct page *p = alloc_pages_node(cpu_to_node(cpu), | |
870 | GFP_KERNEL, 0); | |
871 | if (p == NULL) { | |
872 | err_cpu = cpu; | |
873 | goto err; | |
874 | } | |
875 | per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p); | |
876 | mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex); | |
877 | } | |
878 | ||
879 | return 0; | |
880 | ||
881 | err: | |
882 | for_each_possible_cpu(cpu) { | |
883 | if (cpu == err_cpu) | |
884 | break; | |
885 | free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf); | |
886 | } | |
887 | ||
888 | free_percpu(uprobe_cpu_buffer); | |
889 | return -ENOMEM; | |
890 | } | |
891 | ||
892 | static int uprobe_buffer_enable(void) | |
893 | { | |
894 | int ret = 0; | |
895 | ||
896 | BUG_ON(!mutex_is_locked(&event_mutex)); | |
897 | ||
898 | if (uprobe_buffer_refcnt++ == 0) { | |
899 | ret = uprobe_buffer_init(); | |
900 | if (ret < 0) | |
901 | uprobe_buffer_refcnt--; | |
902 | } | |
903 | ||
904 | return ret; | |
905 | } | |
906 | ||
907 | static void uprobe_buffer_disable(void) | |
908 | { | |
6ea6215f J |
909 | int cpu; |
910 | ||
dcad1a20 NK |
911 | BUG_ON(!mutex_is_locked(&event_mutex)); |
912 | ||
913 | if (--uprobe_buffer_refcnt == 0) { | |
6ea6215f J |
914 | for_each_possible_cpu(cpu) |
915 | free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, | |
916 | cpu)->buf); | |
917 | ||
dcad1a20 NK |
918 | free_percpu(uprobe_cpu_buffer); |
919 | uprobe_cpu_buffer = NULL; | |
920 | } | |
921 | } | |
922 | ||
923 | static struct uprobe_cpu_buffer *uprobe_buffer_get(void) | |
924 | { | |
925 | struct uprobe_cpu_buffer *ucb; | |
926 | int cpu; | |
927 | ||
928 | cpu = raw_smp_processor_id(); | |
929 | ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu); | |
930 | ||
931 | /* | |
932 | * Use per-cpu buffers for fastest access, but we might migrate | |
933 | * so the mutex makes sure we have sole access to it. | |
934 | */ | |
935 | mutex_lock(&ucb->mutex); | |
936 | ||
937 | return ucb; | |
938 | } | |
939 | ||
940 | static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb) | |
941 | { | |
942 | mutex_unlock(&ucb->mutex); | |
943 | } | |
944 | ||
a43b9704 | 945 | static void __uprobe_trace_func(struct trace_uprobe *tu, |
dd9fa555 | 946 | unsigned long func, struct pt_regs *regs, |
70ed91c6 | 947 | struct uprobe_cpu_buffer *ucb, int dsize, |
7f1d2f82 | 948 | struct trace_event_file *trace_file) |
f3f096cf SD |
949 | { |
950 | struct uprobe_trace_entry_head *entry; | |
b7d5eb26 | 951 | struct trace_event_buffer fbuffer; |
457d1772 | 952 | void *data; |
dd9fa555 | 953 | int size, esize; |
e3dc9f89 | 954 | struct trace_event_call *call = trace_probe_event_call(&tu->tp); |
f3f096cf | 955 | |
7f1d2f82 | 956 | WARN_ON(call != trace_file->event_call); |
70ed91c6 | 957 | |
dd9fa555 | 958 | if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE)) |
dcad1a20 NK |
959 | return; |
960 | ||
09a5059a | 961 | if (trace_trigger_soft_disabled(trace_file)) |
ca3b1620 NK |
962 | return; |
963 | ||
dd9fa555 | 964 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
dcad1a20 | 965 | size = esize + tu->tp.size + dsize; |
b7d5eb26 SRV |
966 | entry = trace_event_buffer_reserve(&fbuffer, trace_file, size); |
967 | if (!entry) | |
dd9fa555 | 968 | return; |
f3f096cf | 969 | |
393a736c ON |
970 | if (is_ret_probe(tu)) { |
971 | entry->vaddr[0] = func; | |
972 | entry->vaddr[1] = instruction_pointer(regs); | |
973 | data = DATAOF_TRACE_ENTRY(entry, true); | |
974 | } else { | |
975 | entry->vaddr[0] = instruction_pointer(regs); | |
976 | data = DATAOF_TRACE_ENTRY(entry, false); | |
977 | } | |
978 | ||
dcad1a20 | 979 | memcpy(data, ucb->buf, tu->tp.size + dsize); |
f3f096cf | 980 | |
b7d5eb26 | 981 | trace_event_buffer_commit(&fbuffer); |
a51cc604 | 982 | } |
f42d24a1 | 983 | |
a51cc604 | 984 | /* uprobe handler */ |
dd9fa555 NK |
985 | static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs, |
986 | struct uprobe_cpu_buffer *ucb, int dsize) | |
a51cc604 | 987 | { |
70ed91c6 J |
988 | struct event_file_link *link; |
989 | ||
990 | if (is_ret_probe(tu)) | |
991 | return 0; | |
992 | ||
993 | rcu_read_lock(); | |
b5f935ee | 994 | trace_probe_for_each_link_rcu(link, &tu->tp) |
70ed91c6 J |
995 | __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file); |
996 | rcu_read_unlock(); | |
997 | ||
f42d24a1 | 998 | return 0; |
f3f096cf SD |
999 | } |
1000 | ||
c1ae5c75 | 1001 | static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func, |
dd9fa555 NK |
1002 | struct pt_regs *regs, |
1003 | struct uprobe_cpu_buffer *ucb, int dsize) | |
c1ae5c75 | 1004 | { |
70ed91c6 J |
1005 | struct event_file_link *link; |
1006 | ||
1007 | rcu_read_lock(); | |
b5f935ee | 1008 | trace_probe_for_each_link_rcu(link, &tu->tp) |
70ed91c6 J |
1009 | __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file); |
1010 | rcu_read_unlock(); | |
c1ae5c75 ON |
1011 | } |
1012 | ||
f3f096cf SD |
1013 | /* Event entry printers */ |
1014 | static enum print_line_t | |
1015 | print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event) | |
1016 | { | |
457d1772 | 1017 | struct uprobe_trace_entry_head *entry; |
f3f096cf SD |
1018 | struct trace_seq *s = &iter->seq; |
1019 | struct trace_uprobe *tu; | |
1020 | u8 *data; | |
f3f096cf | 1021 | |
457d1772 | 1022 | entry = (struct uprobe_trace_entry_head *)iter->ent; |
60d53e2c MH |
1023 | tu = trace_uprobe_primary_from_call( |
1024 | container_of(event, struct trace_event_call, event)); | |
1025 | if (unlikely(!tu)) | |
1026 | goto out; | |
f3f096cf | 1027 | |
3ede82dd | 1028 | if (is_ret_probe(tu)) { |
8579a107 | 1029 | trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", |
b55ce203 | 1030 | trace_probe_name(&tu->tp), |
8579a107 | 1031 | entry->vaddr[1], entry->vaddr[0]); |
3ede82dd ON |
1032 | data = DATAOF_TRACE_ENTRY(entry, true); |
1033 | } else { | |
8579a107 | 1034 | trace_seq_printf(s, "%s: (0x%lx)", |
b55ce203 | 1035 | trace_probe_name(&tu->tp), |
8579a107 | 1036 | entry->vaddr[0]); |
3ede82dd ON |
1037 | data = DATAOF_TRACE_ENTRY(entry, false); |
1038 | } | |
f3f096cf | 1039 | |
196b6389 | 1040 | if (trace_probe_print_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0) |
56de7630 | 1041 | goto out; |
f3f096cf | 1042 | |
8579a107 | 1043 | trace_seq_putc(s, '\n'); |
f3f096cf | 1044 | |
8579a107 SRRH |
1045 | out: |
1046 | return trace_handle_return(s); | |
f3f096cf SD |
1047 | } |
1048 | ||
31ba3348 ON |
1049 | typedef bool (*filter_func_t)(struct uprobe_consumer *self, |
1050 | enum uprobe_filter_ctx ctx, | |
1051 | struct mm_struct *mm); | |
1052 | ||
60d53e2c | 1053 | static int trace_uprobe_enable(struct trace_uprobe *tu, filter_func_t filter) |
f3f096cf | 1054 | { |
70ed91c6 J |
1055 | int ret; |
1056 | ||
60d53e2c MH |
1057 | tu->consumer.filter = filter; |
1058 | tu->inode = d_real_inode(tu->path.dentry); | |
1059 | ||
1060 | if (tu->ref_ctr_offset) | |
1061 | ret = uprobe_register_refctr(tu->inode, tu->offset, | |
1062 | tu->ref_ctr_offset, &tu->consumer); | |
1063 | else | |
1064 | ret = uprobe_register(tu->inode, tu->offset, &tu->consumer); | |
1065 | ||
1066 | if (ret) | |
1067 | tu->inode = NULL; | |
1068 | ||
1069 | return ret; | |
1070 | } | |
1071 | ||
1072 | static void __probe_event_disable(struct trace_probe *tp) | |
1073 | { | |
60d53e2c MH |
1074 | struct trace_uprobe *tu; |
1075 | ||
99c9a923 | 1076 | tu = container_of(tp, struct trace_uprobe, tp); |
b61387cb | 1077 | WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter)); |
99c9a923 | 1078 | |
e161c6bf | 1079 | list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { |
60d53e2c MH |
1080 | if (!tu->inode) |
1081 | continue; | |
1082 | ||
60d53e2c MH |
1083 | uprobe_unregister(tu->inode, tu->offset, &tu->consumer); |
1084 | tu->inode = NULL; | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | static int probe_event_enable(struct trace_event_call *call, | |
1089 | struct trace_event_file *file, filter_func_t filter) | |
1090 | { | |
e161c6bf | 1091 | struct trace_probe *tp; |
60d53e2c MH |
1092 | struct trace_uprobe *tu; |
1093 | bool enabled; | |
1094 | int ret; | |
1095 | ||
1096 | tp = trace_probe_primary_from_call(call); | |
1097 | if (WARN_ON_ONCE(!tp)) | |
1098 | return -ENODEV; | |
1099 | enabled = trace_probe_is_enabled(tp); | |
1100 | ||
1101 | /* This may also change "enabled" state */ | |
70ed91c6 | 1102 | if (file) { |
60d53e2c | 1103 | if (trace_probe_test_flag(tp, TP_FLAG_PROFILE)) |
48212542 ON |
1104 | return -EINTR; |
1105 | ||
60d53e2c | 1106 | ret = trace_probe_add_file(tp, file); |
b5f935ee MH |
1107 | if (ret < 0) |
1108 | return ret; | |
48212542 | 1109 | } else { |
60d53e2c | 1110 | if (trace_probe_test_flag(tp, TP_FLAG_TRACE)) |
48212542 ON |
1111 | return -EINTR; |
1112 | ||
60d53e2c | 1113 | trace_probe_set_flag(tp, TP_FLAG_PROFILE); |
48212542 | 1114 | } |
f3f096cf | 1115 | |
60d53e2c | 1116 | tu = container_of(tp, struct trace_uprobe, tp); |
b61387cb | 1117 | WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter)); |
736288ba | 1118 | |
70ed91c6 J |
1119 | if (enabled) |
1120 | return 0; | |
1121 | ||
fb6bab6a ON |
1122 | ret = uprobe_buffer_enable(); |
1123 | if (ret) | |
1124 | goto err_flags; | |
1125 | ||
e161c6bf | 1126 | list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { |
60d53e2c MH |
1127 | ret = trace_uprobe_enable(tu, filter); |
1128 | if (ret) { | |
1129 | __probe_event_disable(tp); | |
1130 | goto err_buffer; | |
1131 | } | |
1cc33161 RB |
1132 | } |
1133 | ||
fb6bab6a ON |
1134 | return 0; |
1135 | ||
1136 | err_buffer: | |
1137 | uprobe_buffer_disable(); | |
f3f096cf | 1138 | |
fb6bab6a | 1139 | err_flags: |
b5f935ee | 1140 | if (file) |
60d53e2c | 1141 | trace_probe_remove_file(tp, file); |
b5f935ee | 1142 | else |
60d53e2c | 1143 | trace_probe_clear_flag(tp, TP_FLAG_PROFILE); |
b5f935ee | 1144 | |
4161824f | 1145 | return ret; |
f3f096cf SD |
1146 | } |
1147 | ||
60d53e2c MH |
1148 | static void probe_event_disable(struct trace_event_call *call, |
1149 | struct trace_event_file *file) | |
f3f096cf | 1150 | { |
60d53e2c MH |
1151 | struct trace_probe *tp; |
1152 | ||
1153 | tp = trace_probe_primary_from_call(call); | |
1154 | if (WARN_ON_ONCE(!tp)) | |
1155 | return; | |
1156 | ||
1157 | if (!trace_probe_is_enabled(tp)) | |
f3f096cf SD |
1158 | return; |
1159 | ||
70ed91c6 | 1160 | if (file) { |
60d53e2c | 1161 | if (trace_probe_remove_file(tp, file) < 0) |
70ed91c6 J |
1162 | return; |
1163 | ||
60d53e2c | 1164 | if (trace_probe_is_enabled(tp)) |
70ed91c6 | 1165 | return; |
b5f935ee | 1166 | } else |
60d53e2c | 1167 | trace_probe_clear_flag(tp, TP_FLAG_PROFILE); |
dcad1a20 | 1168 | |
60d53e2c | 1169 | __probe_event_disable(tp); |
dcad1a20 | 1170 | uprobe_buffer_disable(); |
f3f096cf SD |
1171 | } |
1172 | ||
2425bcb9 | 1173 | static int uprobe_event_define_fields(struct trace_event_call *event_call) |
f3f096cf | 1174 | { |
eeb07b06 | 1175 | int ret, size; |
f3f096cf | 1176 | struct uprobe_trace_entry_head field; |
60d53e2c MH |
1177 | struct trace_uprobe *tu; |
1178 | ||
1179 | tu = trace_uprobe_primary_from_call(event_call); | |
1180 | if (unlikely(!tu)) | |
1181 | return -ENODEV; | |
f3f096cf | 1182 | |
4d1298e2 ON |
1183 | if (is_ret_probe(tu)) { |
1184 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0); | |
1185 | DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0); | |
1186 | size = SIZEOF_TRACE_ENTRY(true); | |
1187 | } else { | |
1188 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0); | |
1189 | size = SIZEOF_TRACE_ENTRY(false); | |
1190 | } | |
f3f096cf | 1191 | |
eeb07b06 | 1192 | return traceprobe_define_arg_fields(event_call, size, &tu->tp); |
f3f096cf SD |
1193 | } |
1194 | ||
f3f096cf | 1195 | #ifdef CONFIG_PERF_EVENTS |
31ba3348 ON |
1196 | static bool |
1197 | __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm) | |
1198 | { | |
1199 | struct perf_event *event; | |
1200 | ||
1201 | if (filter->nr_systemwide) | |
1202 | return true; | |
1203 | ||
1204 | list_for_each_entry(event, &filter->perf_events, hw.tp_list) { | |
50f16a8b | 1205 | if (event->hw.target->mm == mm) |
31ba3348 ON |
1206 | return true; |
1207 | } | |
1208 | ||
1209 | return false; | |
1210 | } | |
1211 | ||
b2fe8ba6 | 1212 | static inline bool |
99c9a923 MH |
1213 | trace_uprobe_filter_event(struct trace_uprobe_filter *filter, |
1214 | struct perf_event *event) | |
b2fe8ba6 | 1215 | { |
99c9a923 | 1216 | return __uprobe_perf_filter(filter, event->hw.target->mm); |
b2fe8ba6 ON |
1217 | } |
1218 | ||
99c9a923 MH |
1219 | static bool trace_uprobe_filter_remove(struct trace_uprobe_filter *filter, |
1220 | struct perf_event *event) | |
736288ba | 1221 | { |
b2fe8ba6 ON |
1222 | bool done; |
1223 | ||
99c9a923 | 1224 | write_lock(&filter->rwlock); |
50f16a8b | 1225 | if (event->hw.target) { |
ce5f36a5 | 1226 | list_del(&event->hw.tp_list); |
99c9a923 | 1227 | done = filter->nr_systemwide || |
50f16a8b | 1228 | (event->hw.target->flags & PF_EXITING) || |
99c9a923 | 1229 | trace_uprobe_filter_event(filter, event); |
b2fe8ba6 | 1230 | } else { |
99c9a923 MH |
1231 | filter->nr_systemwide--; |
1232 | done = filter->nr_systemwide; | |
b2fe8ba6 | 1233 | } |
99c9a923 | 1234 | write_unlock(&filter->rwlock); |
31ba3348 | 1235 | |
99c9a923 | 1236 | return done; |
736288ba ON |
1237 | } |
1238 | ||
99c9a923 MH |
1239 | /* This returns true if the filter always covers target mm */ |
1240 | static bool trace_uprobe_filter_add(struct trace_uprobe_filter *filter, | |
1241 | struct perf_event *event) | |
736288ba | 1242 | { |
b2fe8ba6 ON |
1243 | bool done; |
1244 | ||
99c9a923 | 1245 | write_lock(&filter->rwlock); |
50f16a8b | 1246 | if (event->hw.target) { |
ce5f36a5 ON |
1247 | /* |
1248 | * event->parent != NULL means copy_process(), we can avoid | |
1249 | * uprobe_apply(). current->mm must be probed and we can rely | |
1250 | * on dup_mmap() which preserves the already installed bp's. | |
1251 | * | |
1252 | * attr.enable_on_exec means that exec/mmap will install the | |
1253 | * breakpoints we need. | |
1254 | */ | |
99c9a923 | 1255 | done = filter->nr_systemwide || |
ce5f36a5 | 1256 | event->parent || event->attr.enable_on_exec || |
99c9a923 MH |
1257 | trace_uprobe_filter_event(filter, event); |
1258 | list_add(&event->hw.tp_list, &filter->perf_events); | |
b2fe8ba6 | 1259 | } else { |
99c9a923 MH |
1260 | done = filter->nr_systemwide; |
1261 | filter->nr_systemwide++; | |
b2fe8ba6 | 1262 | } |
99c9a923 | 1263 | write_unlock(&filter->rwlock); |
736288ba | 1264 | |
99c9a923 | 1265 | return done; |
736288ba ON |
1266 | } |
1267 | ||
99c9a923 MH |
1268 | static int uprobe_perf_close(struct trace_event_call *call, |
1269 | struct perf_event *event) | |
60d53e2c | 1270 | { |
e161c6bf | 1271 | struct trace_probe *tp; |
60d53e2c MH |
1272 | struct trace_uprobe *tu; |
1273 | int ret = 0; | |
1274 | ||
1275 | tp = trace_probe_primary_from_call(call); | |
1276 | if (WARN_ON_ONCE(!tp)) | |
1277 | return -ENODEV; | |
1278 | ||
99c9a923 | 1279 | tu = container_of(tp, struct trace_uprobe, tp); |
b61387cb | 1280 | if (trace_uprobe_filter_remove(tu->tp.event->filter, event)) |
99c9a923 MH |
1281 | return 0; |
1282 | ||
e161c6bf | 1283 | list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { |
99c9a923 | 1284 | ret = uprobe_apply(tu->inode, tu->offset, &tu->consumer, false); |
60d53e2c MH |
1285 | if (ret) |
1286 | break; | |
1287 | } | |
1288 | ||
1289 | return ret; | |
1290 | } | |
99c9a923 MH |
1291 | |
1292 | static int uprobe_perf_open(struct trace_event_call *call, | |
1293 | struct perf_event *event) | |
1294 | { | |
e161c6bf | 1295 | struct trace_probe *tp; |
99c9a923 MH |
1296 | struct trace_uprobe *tu; |
1297 | int err = 0; | |
1298 | ||
1299 | tp = trace_probe_primary_from_call(call); | |
1300 | if (WARN_ON_ONCE(!tp)) | |
1301 | return -ENODEV; | |
1302 | ||
1303 | tu = container_of(tp, struct trace_uprobe, tp); | |
b61387cb | 1304 | if (trace_uprobe_filter_add(tu->tp.event->filter, event)) |
99c9a923 MH |
1305 | return 0; |
1306 | ||
e161c6bf | 1307 | list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { |
99c9a923 MH |
1308 | err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true); |
1309 | if (err) { | |
1310 | uprobe_perf_close(call, event); | |
1311 | break; | |
1312 | } | |
1313 | } | |
1314 | ||
1315 | return err; | |
1316 | } | |
1317 | ||
31ba3348 ON |
1318 | static bool uprobe_perf_filter(struct uprobe_consumer *uc, |
1319 | enum uprobe_filter_ctx ctx, struct mm_struct *mm) | |
1320 | { | |
99c9a923 | 1321 | struct trace_uprobe_filter *filter; |
31ba3348 ON |
1322 | struct trace_uprobe *tu; |
1323 | int ret; | |
1324 | ||
1325 | tu = container_of(uc, struct trace_uprobe, consumer); | |
b61387cb | 1326 | filter = tu->tp.event->filter; |
99c9a923 MH |
1327 | |
1328 | read_lock(&filter->rwlock); | |
1329 | ret = __uprobe_perf_filter(filter, mm); | |
1330 | read_unlock(&filter->rwlock); | |
31ba3348 ON |
1331 | |
1332 | return ret; | |
1333 | } | |
1334 | ||
a43b9704 | 1335 | static void __uprobe_perf_func(struct trace_uprobe *tu, |
dd9fa555 NK |
1336 | unsigned long func, struct pt_regs *regs, |
1337 | struct uprobe_cpu_buffer *ucb, int dsize) | |
f3f096cf | 1338 | { |
e3dc9f89 | 1339 | struct trace_event_call *call = trace_probe_event_call(&tu->tp); |
f3f096cf SD |
1340 | struct uprobe_trace_entry_head *entry; |
1341 | struct hlist_head *head; | |
457d1772 | 1342 | void *data; |
dd9fa555 | 1343 | int size, esize; |
dcad1a20 NK |
1344 | int rctx; |
1345 | ||
aca80dd9 | 1346 | #ifdef CONFIG_BPF_EVENTS |
70ed0706 AS |
1347 | if (bpf_prog_array_valid(call)) { |
1348 | u32 ret; | |
1349 | ||
a3c485a5 | 1350 | ret = bpf_prog_run_array_uprobe(call->prog_array, regs, bpf_prog_run); |
70ed0706 AS |
1351 | if (!ret) |
1352 | return; | |
1353 | } | |
aca80dd9 | 1354 | #endif /* CONFIG_BPF_EVENTS */ |
04a22fae | 1355 | |
dcad1a20 | 1356 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
f3f096cf | 1357 | |
dcad1a20 NK |
1358 | size = esize + tu->tp.size + dsize; |
1359 | size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32); | |
1360 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough")) | |
1361 | return; | |
1362 | ||
f3f096cf | 1363 | preempt_disable(); |
515619f2 ON |
1364 | head = this_cpu_ptr(call->perf_events); |
1365 | if (hlist_empty(head)) | |
1366 | goto out; | |
1367 | ||
1e1dcd93 | 1368 | entry = perf_trace_buf_alloc(size, NULL, &rctx); |
f3f096cf SD |
1369 | if (!entry) |
1370 | goto out; | |
1371 | ||
393a736c ON |
1372 | if (is_ret_probe(tu)) { |
1373 | entry->vaddr[0] = func; | |
32520b2c | 1374 | entry->vaddr[1] = instruction_pointer(regs); |
393a736c ON |
1375 | data = DATAOF_TRACE_ENTRY(entry, true); |
1376 | } else { | |
32520b2c | 1377 | entry->vaddr[0] = instruction_pointer(regs); |
393a736c ON |
1378 | data = DATAOF_TRACE_ENTRY(entry, false); |
1379 | } | |
1380 | ||
dcad1a20 NK |
1381 | memcpy(data, ucb->buf, tu->tp.size + dsize); |
1382 | ||
1383 | if (size - esize > tu->tp.size + dsize) { | |
1384 | int len = tu->tp.size + dsize; | |
14577c39 | 1385 | |
dcad1a20 | 1386 | memset(data + len, 0, size - esize - len); |
14577c39 | 1387 | } |
f3f096cf | 1388 | |
1e1dcd93 | 1389 | perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, |
8fd0fbbe | 1390 | head, NULL); |
f3f096cf SD |
1391 | out: |
1392 | preempt_enable(); | |
a51cc604 ON |
1393 | } |
1394 | ||
1395 | /* uprobe profile handler */ | |
dd9fa555 NK |
1396 | static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs, |
1397 | struct uprobe_cpu_buffer *ucb, int dsize) | |
a51cc604 ON |
1398 | { |
1399 | if (!uprobe_perf_filter(&tu->consumer, 0, current->mm)) | |
1400 | return UPROBE_HANDLER_REMOVE; | |
1401 | ||
393a736c | 1402 | if (!is_ret_probe(tu)) |
dd9fa555 | 1403 | __uprobe_perf_func(tu, 0, regs, ucb, dsize); |
f42d24a1 | 1404 | return 0; |
f3f096cf | 1405 | } |
c1ae5c75 ON |
1406 | |
1407 | static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func, | |
dd9fa555 NK |
1408 | struct pt_regs *regs, |
1409 | struct uprobe_cpu_buffer *ucb, int dsize) | |
c1ae5c75 | 1410 | { |
dd9fa555 | 1411 | __uprobe_perf_func(tu, func, regs, ucb, dsize); |
c1ae5c75 | 1412 | } |
41bdc4b4 YS |
1413 | |
1414 | int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type, | |
1415 | const char **filename, u64 *probe_offset, | |
5125e757 | 1416 | u64 *probe_addr, bool perf_type_tracepoint) |
41bdc4b4 YS |
1417 | { |
1418 | const char *pevent = trace_event_name(event->tp_event); | |
1419 | const char *group = event->tp_event->class->system; | |
1420 | struct trace_uprobe *tu; | |
1421 | ||
1422 | if (perf_type_tracepoint) | |
1423 | tu = find_probe_event(pevent, group); | |
1424 | else | |
22d5bd68 | 1425 | tu = trace_uprobe_primary_from_call(event->tp_event); |
41bdc4b4 YS |
1426 | if (!tu) |
1427 | return -EINVAL; | |
1428 | ||
1429 | *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE | |
1430 | : BPF_FD_TYPE_UPROBE; | |
1431 | *filename = tu->filename; | |
1432 | *probe_offset = tu->offset; | |
5125e757 | 1433 | *probe_addr = 0; |
41bdc4b4 YS |
1434 | return 0; |
1435 | } | |
f3f096cf SD |
1436 | #endif /* CONFIG_PERF_EVENTS */ |
1437 | ||
70ed91c6 | 1438 | static int |
2425bcb9 | 1439 | trace_uprobe_register(struct trace_event_call *event, enum trace_reg type, |
70ed91c6 | 1440 | void *data) |
f3f096cf | 1441 | { |
7f1d2f82 | 1442 | struct trace_event_file *file = data; |
f3f096cf SD |
1443 | |
1444 | switch (type) { | |
1445 | case TRACE_REG_REGISTER: | |
60d53e2c | 1446 | return probe_event_enable(event, file, NULL); |
f3f096cf SD |
1447 | |
1448 | case TRACE_REG_UNREGISTER: | |
60d53e2c | 1449 | probe_event_disable(event, file); |
f3f096cf SD |
1450 | return 0; |
1451 | ||
1452 | #ifdef CONFIG_PERF_EVENTS | |
1453 | case TRACE_REG_PERF_REGISTER: | |
60d53e2c | 1454 | return probe_event_enable(event, NULL, uprobe_perf_filter); |
f3f096cf SD |
1455 | |
1456 | case TRACE_REG_PERF_UNREGISTER: | |
60d53e2c | 1457 | probe_event_disable(event, NULL); |
f3f096cf | 1458 | return 0; |
736288ba ON |
1459 | |
1460 | case TRACE_REG_PERF_OPEN: | |
99c9a923 | 1461 | return uprobe_perf_open(event, data); |
736288ba ON |
1462 | |
1463 | case TRACE_REG_PERF_CLOSE: | |
99c9a923 | 1464 | return uprobe_perf_close(event, data); |
736288ba | 1465 | |
f3f096cf SD |
1466 | #endif |
1467 | default: | |
1468 | return 0; | |
1469 | } | |
f3f096cf SD |
1470 | } |
1471 | ||
1472 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs) | |
1473 | { | |
f3f096cf | 1474 | struct trace_uprobe *tu; |
b7e0bf34 | 1475 | struct uprobe_dispatch_data udd; |
dd9fa555 NK |
1476 | struct uprobe_cpu_buffer *ucb; |
1477 | int dsize, esize; | |
f42d24a1 | 1478 | int ret = 0; |
f3f096cf | 1479 | |
dd9fa555 | 1480 | |
a932b738 | 1481 | tu = container_of(con, struct trace_uprobe, consumer); |
1b47aefd | 1482 | tu->nhit++; |
f3f096cf | 1483 | |
b7e0bf34 NK |
1484 | udd.tu = tu; |
1485 | udd.bp_addr = instruction_pointer(regs); | |
1486 | ||
1487 | current->utask->vaddr = (unsigned long) &udd; | |
1488 | ||
dd9fa555 NK |
1489 | if (WARN_ON_ONCE(!uprobe_cpu_buffer)) |
1490 | return 0; | |
1491 | ||
1492 | dsize = __get_data_size(&tu->tp, regs); | |
1493 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); | |
1494 | ||
1495 | ucb = uprobe_buffer_get(); | |
9178412d | 1496 | store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize); |
dd9fa555 | 1497 | |
747774d6 | 1498 | if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE)) |
dd9fa555 | 1499 | ret |= uprobe_trace_func(tu, regs, ucb, dsize); |
f3f096cf SD |
1500 | |
1501 | #ifdef CONFIG_PERF_EVENTS | |
747774d6 | 1502 | if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE)) |
dd9fa555 | 1503 | ret |= uprobe_perf_func(tu, regs, ucb, dsize); |
f3f096cf | 1504 | #endif |
dd9fa555 | 1505 | uprobe_buffer_put(ucb); |
f42d24a1 | 1506 | return ret; |
f3f096cf SD |
1507 | } |
1508 | ||
c1ae5c75 ON |
1509 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
1510 | unsigned long func, struct pt_regs *regs) | |
1511 | { | |
1512 | struct trace_uprobe *tu; | |
b7e0bf34 | 1513 | struct uprobe_dispatch_data udd; |
dd9fa555 NK |
1514 | struct uprobe_cpu_buffer *ucb; |
1515 | int dsize, esize; | |
c1ae5c75 ON |
1516 | |
1517 | tu = container_of(con, struct trace_uprobe, consumer); | |
1518 | ||
b7e0bf34 NK |
1519 | udd.tu = tu; |
1520 | udd.bp_addr = func; | |
1521 | ||
1522 | current->utask->vaddr = (unsigned long) &udd; | |
1523 | ||
dd9fa555 NK |
1524 | if (WARN_ON_ONCE(!uprobe_cpu_buffer)) |
1525 | return 0; | |
1526 | ||
1527 | dsize = __get_data_size(&tu->tp, regs); | |
1528 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); | |
1529 | ||
1530 | ucb = uprobe_buffer_get(); | |
9178412d | 1531 | store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize); |
dd9fa555 | 1532 | |
747774d6 | 1533 | if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE)) |
dd9fa555 | 1534 | uretprobe_trace_func(tu, func, regs, ucb, dsize); |
c1ae5c75 ON |
1535 | |
1536 | #ifdef CONFIG_PERF_EVENTS | |
747774d6 | 1537 | if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE)) |
dd9fa555 | 1538 | uretprobe_perf_func(tu, func, regs, ucb, dsize); |
c1ae5c75 | 1539 | #endif |
dd9fa555 | 1540 | uprobe_buffer_put(ucb); |
c1ae5c75 ON |
1541 | return 0; |
1542 | } | |
1543 | ||
f3f096cf SD |
1544 | static struct trace_event_functions uprobe_funcs = { |
1545 | .trace = print_uprobe_event | |
1546 | }; | |
1547 | ||
04ae87a5 PZ |
1548 | static struct trace_event_fields uprobe_fields_array[] = { |
1549 | { .type = TRACE_FUNCTION_TYPE, | |
1550 | .define_fields = uprobe_event_define_fields }, | |
1551 | {} | |
1552 | }; | |
1553 | ||
e3dc9f89 | 1554 | static inline void init_trace_event_call(struct trace_uprobe *tu) |
f3f096cf | 1555 | { |
e3dc9f89 | 1556 | struct trace_event_call *call = trace_probe_event_call(&tu->tp); |
f3f096cf | 1557 | call->event.funcs = &uprobe_funcs; |
04ae87a5 | 1558 | call->class->fields_array = uprobe_fields_array; |
f3f096cf | 1559 | |
9fd2e48b | 1560 | call->flags = TRACE_EVENT_FL_UPROBE | TRACE_EVENT_FL_CAP_ANY; |
33ea4b24 | 1561 | call->class->reg = trace_uprobe_register; |
33ea4b24 SL |
1562 | } |
1563 | ||
1564 | static int register_uprobe_event(struct trace_uprobe *tu) | |
1565 | { | |
e3dc9f89 | 1566 | init_trace_event_call(tu); |
f3f096cf | 1567 | |
46e5376d | 1568 | return trace_probe_register_event_call(&tu->tp); |
f3f096cf SD |
1569 | } |
1570 | ||
c6c2401d | 1571 | static int unregister_uprobe_event(struct trace_uprobe *tu) |
f3f096cf | 1572 | { |
46e5376d | 1573 | return trace_probe_unregister_event_call(&tu->tp); |
f3f096cf SD |
1574 | } |
1575 | ||
33ea4b24 SL |
1576 | #ifdef CONFIG_PERF_EVENTS |
1577 | struct trace_event_call * | |
a6ca88b2 SL |
1578 | create_local_trace_uprobe(char *name, unsigned long offs, |
1579 | unsigned long ref_ctr_offset, bool is_return) | |
33ea4b24 | 1580 | { |
007517a0 | 1581 | enum probe_print_type ptype; |
33ea4b24 | 1582 | struct trace_uprobe *tu; |
33ea4b24 SL |
1583 | struct path path; |
1584 | int ret; | |
1585 | ||
1586 | ret = kern_path(name, LOOKUP_FOLLOW, &path); | |
1587 | if (ret) | |
1588 | return ERR_PTR(ret); | |
1589 | ||
0c92c7a3 SL |
1590 | if (!d_is_reg(path.dentry)) { |
1591 | path_put(&path); | |
33ea4b24 SL |
1592 | return ERR_PTR(-EINVAL); |
1593 | } | |
1594 | ||
1595 | /* | |
0597c49c | 1596 | * local trace_kprobes are not added to dyn_event, so they are never |
33ea4b24 SL |
1597 | * searched in find_trace_kprobe(). Therefore, there is no concern of |
1598 | * duplicated name "DUMMY_EVENT" here. | |
1599 | */ | |
1600 | tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0, | |
1601 | is_return); | |
1602 | ||
1603 | if (IS_ERR(tu)) { | |
1604 | pr_info("Failed to allocate trace_uprobe.(%d)\n", | |
1605 | (int)PTR_ERR(tu)); | |
0c92c7a3 | 1606 | path_put(&path); |
33ea4b24 SL |
1607 | return ERR_CAST(tu); |
1608 | } | |
1609 | ||
1610 | tu->offset = offs; | |
0c92c7a3 | 1611 | tu->path = path; |
a6ca88b2 | 1612 | tu->ref_ctr_offset = ref_ctr_offset; |
33ea4b24 | 1613 | tu->filename = kstrdup(name, GFP_KERNEL); |
8c722424 XW |
1614 | if (!tu->filename) { |
1615 | ret = -ENOMEM; | |
1616 | goto error; | |
1617 | } | |
1618 | ||
e3dc9f89 | 1619 | init_trace_event_call(tu); |
33ea4b24 | 1620 | |
007517a0 SRV |
1621 | ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL; |
1622 | if (traceprobe_set_print_fmt(&tu->tp, ptype) < 0) { | |
33ea4b24 SL |
1623 | ret = -ENOMEM; |
1624 | goto error; | |
1625 | } | |
1626 | ||
e3dc9f89 | 1627 | return trace_probe_event_call(&tu->tp); |
33ea4b24 SL |
1628 | error: |
1629 | free_trace_uprobe(tu); | |
1630 | return ERR_PTR(ret); | |
1631 | } | |
1632 | ||
1633 | void destroy_local_trace_uprobe(struct trace_event_call *event_call) | |
1634 | { | |
1635 | struct trace_uprobe *tu; | |
1636 | ||
60d53e2c | 1637 | tu = trace_uprobe_primary_from_call(event_call); |
33ea4b24 | 1638 | |
33ea4b24 SL |
1639 | free_trace_uprobe(tu); |
1640 | } | |
1641 | #endif /* CONFIG_PERF_EVENTS */ | |
1642 | ||
39bcdd6a | 1643 | /* Make a trace interface for controlling probe points */ |
f3f096cf SD |
1644 | static __init int init_uprobe_trace(void) |
1645 | { | |
0597c49c MH |
1646 | int ret; |
1647 | ||
1648 | ret = dyn_event_register(&trace_uprobe_ops); | |
1649 | if (ret) | |
1650 | return ret; | |
f3f096cf | 1651 | |
22c36b18 WY |
1652 | ret = tracing_init_dentry(); |
1653 | if (ret) | |
f3f096cf SD |
1654 | return 0; |
1655 | ||
21ccc9cd | 1656 | trace_create_file("uprobe_events", TRACE_MODE_WRITE, NULL, |
f3f096cf SD |
1657 | NULL, &uprobe_events_ops); |
1658 | /* Profile interface */ | |
21ccc9cd | 1659 | trace_create_file("uprobe_profile", TRACE_MODE_READ, NULL, |
f3f096cf SD |
1660 | NULL, &uprobe_profile_ops); |
1661 | return 0; | |
1662 | } | |
1663 | ||
1664 | fs_initcall(init_uprobe_trace); |