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