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