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 | */ |
Masami Hiramatsu | 7257634 | 2017-02-07 20:21:28 +0900 | [diff] [blame] | 8 | #define pr_fmt(fmt) "trace_kprobe: " fmt |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/uaccess.h> |
| 12 | #include <linux/uprobes.h> |
| 13 | #include <linux/namei.h> |
Andy Shevchenko | b2e902f | 2012-12-17 16:01:27 -0800 | [diff] [blame] | 14 | #include <linux/string.h> |
Ingo Molnar | b2d0910 | 2017-02-04 01:27:20 +0100 | [diff] [blame] | 15 | #include <linux/rculist.h> |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 16 | |
| 17 | #include "trace_probe.h" |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 18 | #include "trace_probe_tmpl.h" |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 19 | |
| 20 | #define UPROBE_EVENT_SYSTEM "uprobes" |
| 21 | |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 22 | struct uprobe_trace_entry_head { |
| 23 | struct trace_entry ent; |
| 24 | unsigned long vaddr[]; |
| 25 | }; |
| 26 | |
| 27 | #define SIZEOF_TRACE_ENTRY(is_return) \ |
| 28 | (sizeof(struct uprobe_trace_entry_head) + \ |
| 29 | sizeof(unsigned long) * (is_return ? 2 : 1)) |
| 30 | |
| 31 | #define DATAOF_TRACE_ENTRY(entry, is_return) \ |
| 32 | ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return)) |
| 33 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 34 | struct trace_uprobe_filter { |
| 35 | rwlock_t rwlock; |
| 36 | int nr_systemwide; |
| 37 | struct list_head perf_events; |
| 38 | }; |
| 39 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 40 | /* |
| 41 | * uprobe event core functions |
| 42 | */ |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 43 | struct trace_uprobe { |
| 44 | struct list_head list; |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 45 | struct trace_uprobe_filter filter; |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 46 | struct uprobe_consumer consumer; |
Song Liu | 0c92c7a | 2018-04-23 10:21:34 -0700 | [diff] [blame] | 47 | struct path path; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 48 | struct inode *inode; |
| 49 | char *filename; |
| 50 | unsigned long offset; |
Ravi Bangoria | 1cc3316 | 2018-08-20 10:12:47 +0530 | [diff] [blame] | 51 | unsigned long ref_ctr_offset; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 52 | unsigned long nhit; |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 53 | struct trace_probe tp; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 54 | }; |
| 55 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 56 | #define SIZEOF_TRACE_UPROBE(n) \ |
| 57 | (offsetof(struct trace_uprobe, tp.args) + \ |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 58 | (sizeof(struct probe_arg) * (n))) |
| 59 | |
| 60 | static int register_uprobe_event(struct trace_uprobe *tu); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 61 | static int unregister_uprobe_event(struct trace_uprobe *tu); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 62 | |
| 63 | static DEFINE_MUTEX(uprobe_lock); |
| 64 | static LIST_HEAD(uprobe_list); |
| 65 | |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 66 | struct uprobe_dispatch_data { |
| 67 | struct trace_uprobe *tu; |
| 68 | unsigned long bp_addr; |
| 69 | }; |
| 70 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 71 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs); |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 72 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
| 73 | unsigned long func, struct pt_regs *regs); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 74 | |
Namhyung Kim | 3fd996a | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 75 | #ifdef CONFIG_STACK_GROWSUP |
| 76 | static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) |
| 77 | { |
| 78 | return addr - (n * sizeof(long)); |
| 79 | } |
| 80 | #else |
| 81 | static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) |
| 82 | { |
| 83 | return addr + (n * sizeof(long)); |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n) |
| 88 | { |
| 89 | unsigned long ret; |
| 90 | unsigned long addr = user_stack_pointer(regs); |
| 91 | |
| 92 | addr = adjust_stack_addr(addr, n); |
| 93 | |
| 94 | if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret))) |
| 95 | return 0; |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Uprobes-specific fetch functions |
| 102 | */ |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 103 | static nokprobe_inline int |
Masami Hiramatsu | 9b960a3 | 2018-04-25 21:19:59 +0900 | [diff] [blame] | 104 | probe_mem_read(void *dest, void *src, size_t size) |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 105 | { |
| 106 | void __user *vaddr = (void __force __user *)src; |
Namhyung Kim | 3fd996a | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 107 | |
Masami Hiramatsu | f3f5893 | 2018-08-29 01:17:47 +0900 | [diff] [blame] | 108 | return copy_from_user(dest, vaddr, size) ? -EFAULT : 0; |
Namhyung Kim | 5baaa59 | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 109 | } |
Namhyung Kim | 5baaa59 | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 110 | /* |
| 111 | * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max |
| 112 | * length and relative data location. |
| 113 | */ |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 114 | static nokprobe_inline int |
| 115 | fetch_store_string(unsigned long addr, void *dest, void *base) |
Namhyung Kim | 5baaa59 | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 116 | { |
| 117 | long ret; |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 118 | u32 loc = *(u32 *)dest; |
| 119 | int maxlen = get_loc_len(loc); |
| 120 | u8 *dst = get_loc_data(dest, base); |
Namhyung Kim | 5baaa59 | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 121 | void __user *src = (void __force __user *) addr; |
| 122 | |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 123 | if (unlikely(!maxlen)) |
| 124 | return -ENOMEM; |
Namhyung Kim | 5baaa59 | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 125 | |
| 126 | ret = strncpy_from_user(dst, src, maxlen); |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 127 | if (ret >= 0) { |
| 128 | if (ret == maxlen) |
| 129 | dst[ret - 1] = '\0'; |
| 130 | *(u32 *)dest = make_data_loc(ret, (void *)dst - base); |
Namhyung Kim | 5baaa59 | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 131 | } |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 132 | |
| 133 | return ret; |
Namhyung Kim | 5baaa59 | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 134 | } |
| 135 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 136 | /* Return the length of string -- including null terminal byte */ |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 137 | static nokprobe_inline int |
| 138 | fetch_store_strlen(unsigned long addr) |
Namhyung Kim | 5baaa59 | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 139 | { |
| 140 | int len; |
| 141 | void __user *vaddr = (void __force __user *) addr; |
| 142 | |
| 143 | len = strnlen_user(vaddr, MAX_STRING_SIZE); |
| 144 | |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 145 | return (len > MAX_STRING_SIZE) ? 0 : len; |
Namhyung Kim | 5baaa59 | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 146 | } |
Namhyung Kim | 3fd996a | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 147 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 148 | static unsigned long translate_user_vaddr(unsigned long file_offset) |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 149 | { |
| 150 | unsigned long base_addr; |
| 151 | struct uprobe_dispatch_data *udd; |
| 152 | |
| 153 | udd = (void *) current->utask->vaddr; |
| 154 | |
| 155 | base_addr = udd->bp_addr - udd->tu->offset; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 156 | return base_addr + file_offset; |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 157 | } |
| 158 | |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 159 | /* Note that we don't verify it, since the code does not come from user space */ |
| 160 | static int |
| 161 | process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest, |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 162 | void *base) |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 163 | { |
| 164 | unsigned long val; |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 165 | |
| 166 | /* 1st stage: get value from context */ |
| 167 | switch (code->op) { |
| 168 | case FETCH_OP_REG: |
| 169 | val = regs_get_register(regs, code->param); |
| 170 | break; |
| 171 | case FETCH_OP_STACK: |
| 172 | val = get_user_stack_nth(regs, code->param); |
| 173 | break; |
| 174 | case FETCH_OP_STACKP: |
| 175 | val = user_stack_pointer(regs); |
| 176 | break; |
| 177 | case FETCH_OP_RETVAL: |
| 178 | val = regs_return_value(regs); |
| 179 | break; |
| 180 | case FETCH_OP_IMM: |
| 181 | val = code->immediate; |
| 182 | break; |
| 183 | case FETCH_OP_FOFFS: |
| 184 | val = translate_user_vaddr(code->immediate); |
| 185 | break; |
| 186 | default: |
| 187 | return -EILSEQ; |
| 188 | } |
| 189 | code++; |
| 190 | |
Masami Hiramatsu | 9b960a3 | 2018-04-25 21:19:59 +0900 | [diff] [blame] | 191 | return process_fetch_insn_bottom(code, val, dest, base); |
Masami Hiramatsu | 5330592 | 2018-04-25 21:18:03 +0900 | [diff] [blame] | 192 | } |
| 193 | NOKPROBE_SYMBOL(process_fetch_insn) |
| 194 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 195 | static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter) |
| 196 | { |
| 197 | rwlock_init(&filter->rwlock); |
| 198 | filter->nr_systemwide = 0; |
| 199 | INIT_LIST_HEAD(&filter->perf_events); |
| 200 | } |
| 201 | |
| 202 | static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter) |
| 203 | { |
| 204 | return !filter->nr_systemwide && list_empty(&filter->perf_events); |
| 205 | } |
| 206 | |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 207 | static inline bool is_ret_probe(struct trace_uprobe *tu) |
| 208 | { |
| 209 | return tu->consumer.ret_handler != NULL; |
| 210 | } |
| 211 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 212 | /* |
| 213 | * Allocate new trace_uprobe and initialize it (including uprobes). |
| 214 | */ |
| 215 | static struct trace_uprobe * |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 216 | 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] | 217 | { |
| 218 | struct trace_uprobe *tu; |
| 219 | |
| 220 | if (!event || !is_good_name(event)) |
| 221 | return ERR_PTR(-EINVAL); |
| 222 | |
| 223 | if (!group || !is_good_name(group)) |
| 224 | return ERR_PTR(-EINVAL); |
| 225 | |
| 226 | tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL); |
| 227 | if (!tu) |
| 228 | return ERR_PTR(-ENOMEM); |
| 229 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 230 | tu->tp.call.class = &tu->tp.class; |
| 231 | tu->tp.call.name = kstrdup(event, GFP_KERNEL); |
| 232 | if (!tu->tp.call.name) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 233 | goto error; |
| 234 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 235 | tu->tp.class.system = kstrdup(group, GFP_KERNEL); |
| 236 | if (!tu->tp.class.system) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 237 | goto error; |
| 238 | |
| 239 | INIT_LIST_HEAD(&tu->list); |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 240 | INIT_LIST_HEAD(&tu->tp.files); |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 241 | tu->consumer.handler = uprobe_dispatcher; |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 242 | if (is_ret) |
| 243 | tu->consumer.ret_handler = uretprobe_dispatcher; |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 244 | init_trace_uprobe_filter(&tu->filter); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 245 | return tu; |
| 246 | |
| 247 | error: |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 248 | kfree(tu->tp.call.name); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 249 | kfree(tu); |
| 250 | |
| 251 | return ERR_PTR(-ENOMEM); |
| 252 | } |
| 253 | |
| 254 | static void free_trace_uprobe(struct trace_uprobe *tu) |
| 255 | { |
| 256 | int i; |
| 257 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 258 | for (i = 0; i < tu->tp.nr_args; i++) |
| 259 | traceprobe_free_probe_arg(&tu->tp.args[i]); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 260 | |
Song Liu | 0c92c7a | 2018-04-23 10:21:34 -0700 | [diff] [blame] | 261 | path_put(&tu->path); |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 262 | kfree(tu->tp.call.class->system); |
| 263 | kfree(tu->tp.call.name); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 264 | kfree(tu->filename); |
| 265 | kfree(tu); |
| 266 | } |
| 267 | |
| 268 | static struct trace_uprobe *find_probe_event(const char *event, const char *group) |
| 269 | { |
| 270 | struct trace_uprobe *tu; |
| 271 | |
| 272 | list_for_each_entry(tu, &uprobe_list, list) |
Steven Rostedt (Red Hat) | 687fcc4 | 2015-05-13 14:20:14 -0400 | [diff] [blame] | 273 | if (strcmp(trace_event_name(&tu->tp.call), event) == 0 && |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 274 | strcmp(tu->tp.call.class->system, group) == 0) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 275 | return tu; |
| 276 | |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */ |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 281 | static int unregister_trace_uprobe(struct trace_uprobe *tu) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 282 | { |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 283 | int ret; |
| 284 | |
| 285 | ret = unregister_uprobe_event(tu); |
| 286 | if (ret) |
| 287 | return ret; |
| 288 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 289 | list_del(&tu->list); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 290 | free_trace_uprobe(tu); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 291 | return 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 292 | } |
| 293 | |
Ravi Bangoria | ccea872 | 2018-08-20 10:12:49 +0530 | [diff] [blame] | 294 | /* |
| 295 | * Uprobe with multiple reference counter is not allowed. i.e. |
| 296 | * If inode and offset matches, reference counter offset *must* |
| 297 | * match as well. Though, there is one exception: If user is |
| 298 | * replacing old trace_uprobe with new one(same group/event), |
| 299 | * then we allow same uprobe with new reference counter as far |
| 300 | * as the new one does not conflict with any other existing |
| 301 | * ones. |
| 302 | */ |
| 303 | static struct trace_uprobe *find_old_trace_uprobe(struct trace_uprobe *new) |
| 304 | { |
| 305 | struct trace_uprobe *tmp, *old = NULL; |
| 306 | struct inode *new_inode = d_real_inode(new->path.dentry); |
| 307 | |
| 308 | old = find_probe_event(trace_event_name(&new->tp.call), |
| 309 | new->tp.call.class->system); |
| 310 | |
| 311 | list_for_each_entry(tmp, &uprobe_list, list) { |
| 312 | if ((old ? old != tmp : true) && |
| 313 | new_inode == d_real_inode(tmp->path.dentry) && |
| 314 | new->offset == tmp->offset && |
| 315 | new->ref_ctr_offset != tmp->ref_ctr_offset) { |
| 316 | pr_warn("Reference counter offset mismatch."); |
| 317 | return ERR_PTR(-EINVAL); |
| 318 | } |
| 319 | } |
| 320 | return old; |
| 321 | } |
| 322 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 323 | /* Register a trace_uprobe and probe_event */ |
| 324 | static int register_trace_uprobe(struct trace_uprobe *tu) |
| 325 | { |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 326 | struct trace_uprobe *old_tu; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 327 | int ret; |
| 328 | |
| 329 | mutex_lock(&uprobe_lock); |
| 330 | |
| 331 | /* register as an event */ |
Ravi Bangoria | ccea872 | 2018-08-20 10:12:49 +0530 | [diff] [blame] | 332 | old_tu = find_old_trace_uprobe(tu); |
| 333 | if (IS_ERR(old_tu)) { |
| 334 | ret = PTR_ERR(old_tu); |
| 335 | goto end; |
| 336 | } |
| 337 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 338 | if (old_tu) { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 339 | /* delete old event */ |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 340 | ret = unregister_trace_uprobe(old_tu); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 341 | if (ret) |
| 342 | goto end; |
| 343 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 344 | |
| 345 | ret = register_uprobe_event(tu); |
| 346 | if (ret) { |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 347 | pr_warn("Failed to register probe event(%d)\n", ret); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 348 | goto end; |
| 349 | } |
| 350 | |
| 351 | list_add_tail(&tu->list, &uprobe_list); |
| 352 | |
| 353 | end: |
| 354 | mutex_unlock(&uprobe_lock); |
| 355 | |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Argument syntax: |
Namhyung Kim | 306cfe2 | 2013-07-03 16:44:46 +0900 | [diff] [blame] | 361 | * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS] |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 362 | * |
| 363 | * - Remove uprobe: -:[GRP/]EVENT |
| 364 | */ |
| 365 | static int create_trace_uprobe(int argc, char **argv) |
| 366 | { |
| 367 | struct trace_uprobe *tu; |
Ravi Bangoria | 1cc3316 | 2018-08-20 10:12:47 +0530 | [diff] [blame] | 368 | char *arg, *event, *group, *filename, *rctr, *rctr_end; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 369 | char buf[MAX_EVENT_NAME_LEN]; |
| 370 | struct path path; |
Ravi Bangoria | 1cc3316 | 2018-08-20 10:12:47 +0530 | [diff] [blame] | 371 | unsigned long offset, ref_ctr_offset; |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 372 | bool is_delete, is_return; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 373 | int i, ret; |
| 374 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 375 | ret = 0; |
| 376 | is_delete = false; |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 377 | is_return = false; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 378 | event = NULL; |
| 379 | group = NULL; |
Ravi Bangoria | 1cc3316 | 2018-08-20 10:12:47 +0530 | [diff] [blame] | 380 | ref_ctr_offset = 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 381 | |
| 382 | /* argc must be >= 1 */ |
| 383 | if (argv[0][0] == '-') |
| 384 | is_delete = true; |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 385 | else if (argv[0][0] == 'r') |
| 386 | is_return = true; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 387 | else if (argv[0][0] != 'p') { |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 388 | pr_info("Probe definition must be started with 'p', 'r' or '-'.\n"); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 389 | return -EINVAL; |
| 390 | } |
| 391 | |
| 392 | if (argv[0][1] == ':') { |
| 393 | event = &argv[0][2]; |
| 394 | arg = strchr(event, '/'); |
| 395 | |
| 396 | if (arg) { |
| 397 | group = event; |
| 398 | event = arg + 1; |
| 399 | event[-1] = '\0'; |
| 400 | |
| 401 | if (strlen(group) == 0) { |
| 402 | pr_info("Group name is not specified\n"); |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | } |
| 406 | if (strlen(event) == 0) { |
| 407 | pr_info("Event name is not specified\n"); |
| 408 | return -EINVAL; |
| 409 | } |
| 410 | } |
| 411 | if (!group) |
| 412 | group = UPROBE_EVENT_SYSTEM; |
| 413 | |
| 414 | if (is_delete) { |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 415 | int ret; |
| 416 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 417 | if (!event) { |
| 418 | pr_info("Delete command needs an event name.\n"); |
| 419 | return -EINVAL; |
| 420 | } |
| 421 | mutex_lock(&uprobe_lock); |
| 422 | tu = find_probe_event(event, group); |
| 423 | |
| 424 | if (!tu) { |
| 425 | mutex_unlock(&uprobe_lock); |
| 426 | pr_info("Event %s/%s doesn't exist.\n", group, event); |
| 427 | return -ENOENT; |
| 428 | } |
| 429 | /* delete an event */ |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 430 | ret = unregister_trace_uprobe(tu); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 431 | mutex_unlock(&uprobe_lock); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 432 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | if (argc < 2) { |
| 436 | pr_info("Probe point is not specified.\n"); |
| 437 | return -EINVAL; |
| 438 | } |
Kenny Yu | 6496bb7 | 2017-01-13 08:58:34 -0800 | [diff] [blame] | 439 | /* Find the last occurrence, in case the path contains ':' too. */ |
| 440 | arg = strrchr(argv[1], ':'); |
Song Liu | 0c92c7a | 2018-04-23 10:21:34 -0700 | [diff] [blame] | 441 | if (!arg) |
| 442 | return -EINVAL; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 443 | |
| 444 | *arg++ = '\0'; |
| 445 | filename = argv[1]; |
| 446 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); |
| 447 | if (ret) |
Song Liu | 0c92c7a | 2018-04-23 10:21:34 -0700 | [diff] [blame] | 448 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 449 | |
Song Liu | 0c92c7a | 2018-04-23 10:21:34 -0700 | [diff] [blame] | 450 | if (!d_is_reg(path.dentry)) { |
Jovi Zhang | d24d7db | 2012-07-18 18:16:44 +0800 | [diff] [blame] | 451 | ret = -EINVAL; |
| 452 | goto fail_address_parse; |
| 453 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 454 | |
Ravi Bangoria | 1cc3316 | 2018-08-20 10:12:47 +0530 | [diff] [blame] | 455 | /* Parse reference counter offset if specified. */ |
| 456 | rctr = strchr(arg, '('); |
| 457 | if (rctr) { |
| 458 | rctr_end = strchr(rctr, ')'); |
| 459 | if (rctr > rctr_end || *(rctr_end + 1) != 0) { |
| 460 | ret = -EINVAL; |
| 461 | pr_info("Invalid reference counter offset.\n"); |
| 462 | goto fail_address_parse; |
| 463 | } |
| 464 | |
| 465 | *rctr++ = '\0'; |
| 466 | *rctr_end = '\0'; |
| 467 | ret = kstrtoul(rctr, 0, &ref_ctr_offset); |
| 468 | if (ret) { |
| 469 | pr_info("Invalid reference counter offset.\n"); |
| 470 | goto fail_address_parse; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /* Parse uprobe offset. */ |
Oleg Nesterov | 84d7ed7 | 2013-01-27 18:20:45 +0100 | [diff] [blame] | 475 | ret = kstrtoul(arg, 0, &offset); |
| 476 | if (ret) |
| 477 | goto fail_address_parse; |
| 478 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 479 | argc -= 2; |
| 480 | argv += 2; |
| 481 | |
| 482 | /* setup a probe */ |
| 483 | if (!event) { |
Andy Shevchenko | b2e902f | 2012-12-17 16:01:27 -0800 | [diff] [blame] | 484 | char *tail; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 485 | char *ptr; |
| 486 | |
Andy Shevchenko | b2e902f | 2012-12-17 16:01:27 -0800 | [diff] [blame] | 487 | tail = kstrdup(kbasename(filename), GFP_KERNEL); |
| 488 | if (!tail) { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 489 | ret = -ENOMEM; |
| 490 | goto fail_address_parse; |
| 491 | } |
| 492 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 493 | ptr = strpbrk(tail, ".-_"); |
| 494 | if (ptr) |
| 495 | *ptr = '\0'; |
| 496 | |
| 497 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset); |
| 498 | event = buf; |
| 499 | kfree(tail); |
| 500 | } |
| 501 | |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 502 | tu = alloc_trace_uprobe(group, event, argc, is_return); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 503 | if (IS_ERR(tu)) { |
| 504 | pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu)); |
| 505 | ret = PTR_ERR(tu); |
| 506 | goto fail_address_parse; |
| 507 | } |
| 508 | tu->offset = offset; |
Ravi Bangoria | 1cc3316 | 2018-08-20 10:12:47 +0530 | [diff] [blame] | 509 | tu->ref_ctr_offset = ref_ctr_offset; |
Song Liu | 0c92c7a | 2018-04-23 10:21:34 -0700 | [diff] [blame] | 510 | tu->path = path; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 511 | tu->filename = kstrdup(filename, GFP_KERNEL); |
| 512 | |
| 513 | if (!tu->filename) { |
| 514 | pr_info("Failed to allocate filename.\n"); |
| 515 | ret = -ENOMEM; |
| 516 | goto error; |
| 517 | } |
| 518 | |
| 519 | /* parse arguments */ |
| 520 | ret = 0; |
| 521 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 522 | struct probe_arg *parg = &tu->tp.args[i]; |
| 523 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 524 | /* Increment count for freeing args in error case */ |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 525 | tu->tp.nr_args++; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 526 | |
| 527 | /* Parse argument name */ |
| 528 | arg = strchr(argv[i], '='); |
| 529 | if (arg) { |
| 530 | *arg++ = '\0'; |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 531 | parg->name = kstrdup(argv[i], GFP_KERNEL); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 532 | } else { |
| 533 | arg = argv[i]; |
| 534 | /* If argument name is omitted, set "argN" */ |
| 535 | snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1); |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 536 | parg->name = kstrdup(buf, GFP_KERNEL); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 537 | } |
| 538 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 539 | if (!parg->name) { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 540 | pr_info("Failed to allocate argument[%d] name.\n", i); |
| 541 | ret = -ENOMEM; |
| 542 | goto error; |
| 543 | } |
| 544 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 545 | if (!is_good_name(parg->name)) { |
| 546 | pr_info("Invalid argument[%d] name: %s\n", i, parg->name); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 547 | ret = -EINVAL; |
| 548 | goto error; |
| 549 | } |
| 550 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 551 | if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 552 | pr_info("Argument[%d] name '%s' conflicts with " |
| 553 | "another field.\n", i, argv[i]); |
| 554 | ret = -EINVAL; |
| 555 | goto error; |
| 556 | } |
| 557 | |
| 558 | /* Parse fetch argument */ |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 559 | ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg, |
Masami Hiramatsu | a1303af | 2018-04-25 21:21:26 +0900 | [diff] [blame] | 560 | is_return ? TPARG_FL_RETURN : 0); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 561 | if (ret) { |
| 562 | pr_info("Parse error at argument[%d]. (%d)\n", i, ret); |
| 563 | goto error; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | ret = register_trace_uprobe(tu); |
| 568 | if (ret) |
| 569 | goto error; |
| 570 | return 0; |
| 571 | |
| 572 | error: |
| 573 | free_trace_uprobe(tu); |
| 574 | return ret; |
| 575 | |
| 576 | fail_address_parse: |
Song Liu | 0c92c7a | 2018-04-23 10:21:34 -0700 | [diff] [blame] | 577 | path_put(&path); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 578 | |
Jovi Zhang | d24d7db | 2012-07-18 18:16:44 +0800 | [diff] [blame] | 579 | pr_info("Failed to parse address or file.\n"); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 580 | |
| 581 | return ret; |
| 582 | } |
| 583 | |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 584 | static int cleanup_all_probes(void) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 585 | { |
| 586 | struct trace_uprobe *tu; |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 587 | int ret = 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 588 | |
| 589 | mutex_lock(&uprobe_lock); |
| 590 | while (!list_empty(&uprobe_list)) { |
| 591 | tu = list_entry(uprobe_list.next, struct trace_uprobe, list); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 592 | ret = unregister_trace_uprobe(tu); |
| 593 | if (ret) |
| 594 | break; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 595 | } |
| 596 | mutex_unlock(&uprobe_lock); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 597 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* Probes listing interfaces */ |
| 601 | static void *probes_seq_start(struct seq_file *m, loff_t *pos) |
| 602 | { |
| 603 | mutex_lock(&uprobe_lock); |
| 604 | return seq_list_start(&uprobe_list, *pos); |
| 605 | } |
| 606 | |
| 607 | static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos) |
| 608 | { |
| 609 | return seq_list_next(v, &uprobe_list, pos); |
| 610 | } |
| 611 | |
| 612 | static void probes_seq_stop(struct seq_file *m, void *v) |
| 613 | { |
| 614 | mutex_unlock(&uprobe_lock); |
| 615 | } |
| 616 | |
| 617 | static int probes_seq_show(struct seq_file *m, void *v) |
| 618 | { |
| 619 | struct trace_uprobe *tu = v; |
Oleg Nesterov | 3ede82d | 2013-03-30 19:48:09 +0100 | [diff] [blame] | 620 | char c = is_ret_probe(tu) ? 'r' : 'p'; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 621 | int i; |
| 622 | |
Ravi Bangoria | a64b2c0 | 2018-03-15 13:57:56 +0530 | [diff] [blame] | 623 | seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system, |
| 624 | trace_event_name(&tu->tp.call), tu->filename, |
| 625 | (int)(sizeof(void *) * 2), tu->offset); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 626 | |
Ravi Bangoria | 1cc3316 | 2018-08-20 10:12:47 +0530 | [diff] [blame] | 627 | if (tu->ref_ctr_offset) |
| 628 | seq_printf(m, "(0x%lx)", tu->ref_ctr_offset); |
| 629 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 630 | for (i = 0; i < tu->tp.nr_args; i++) |
| 631 | 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] | 632 | |
Rasmus Villemoes | fa6f0cc | 2014-11-08 21:42:10 +0100 | [diff] [blame] | 633 | seq_putc(m, '\n'); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static const struct seq_operations probes_seq_op = { |
| 638 | .start = probes_seq_start, |
| 639 | .next = probes_seq_next, |
| 640 | .stop = probes_seq_stop, |
| 641 | .show = probes_seq_show |
| 642 | }; |
| 643 | |
| 644 | static int probes_open(struct inode *inode, struct file *file) |
| 645 | { |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 646 | int ret; |
| 647 | |
| 648 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { |
| 649 | ret = cleanup_all_probes(); |
| 650 | if (ret) |
| 651 | return ret; |
| 652 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 653 | |
| 654 | return seq_open(file, &probes_seq_op); |
| 655 | } |
| 656 | |
| 657 | static ssize_t probes_write(struct file *file, const char __user *buffer, |
| 658 | size_t count, loff_t *ppos) |
| 659 | { |
Tom Zanussi | 7e465ba | 2017-09-22 14:58:20 -0500 | [diff] [blame] | 660 | return trace_parse_run_command(file, buffer, count, ppos, create_trace_uprobe); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | static const struct file_operations uprobe_events_ops = { |
| 664 | .owner = THIS_MODULE, |
| 665 | .open = probes_open, |
| 666 | .read = seq_read, |
| 667 | .llseek = seq_lseek, |
| 668 | .release = seq_release, |
| 669 | .write = probes_write, |
| 670 | }; |
| 671 | |
| 672 | /* Probes profiling interfaces */ |
| 673 | static int probes_profile_seq_show(struct seq_file *m, void *v) |
| 674 | { |
| 675 | struct trace_uprobe *tu = v; |
| 676 | |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 677 | seq_printf(m, " %s %-44s %15lu\n", tu->filename, |
Steven Rostedt (Red Hat) | 687fcc4 | 2015-05-13 14:20:14 -0400 | [diff] [blame] | 678 | trace_event_name(&tu->tp.call), tu->nhit); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | static const struct seq_operations profile_seq_op = { |
| 683 | .start = probes_seq_start, |
| 684 | .next = probes_seq_next, |
| 685 | .stop = probes_seq_stop, |
| 686 | .show = probes_profile_seq_show |
| 687 | }; |
| 688 | |
| 689 | static int profile_open(struct inode *inode, struct file *file) |
| 690 | { |
| 691 | return seq_open(file, &profile_seq_op); |
| 692 | } |
| 693 | |
| 694 | static const struct file_operations uprobe_profile_ops = { |
| 695 | .owner = THIS_MODULE, |
| 696 | .open = profile_open, |
| 697 | .read = seq_read, |
| 698 | .llseek = seq_lseek, |
| 699 | .release = seq_release, |
| 700 | }; |
| 701 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame] | 702 | struct uprobe_cpu_buffer { |
| 703 | struct mutex mutex; |
| 704 | void *buf; |
| 705 | }; |
| 706 | static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer; |
| 707 | static int uprobe_buffer_refcnt; |
| 708 | |
| 709 | static int uprobe_buffer_init(void) |
| 710 | { |
| 711 | int cpu, err_cpu; |
| 712 | |
| 713 | uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer); |
| 714 | if (uprobe_cpu_buffer == NULL) |
| 715 | return -ENOMEM; |
| 716 | |
| 717 | for_each_possible_cpu(cpu) { |
| 718 | struct page *p = alloc_pages_node(cpu_to_node(cpu), |
| 719 | GFP_KERNEL, 0); |
| 720 | if (p == NULL) { |
| 721 | err_cpu = cpu; |
| 722 | goto err; |
| 723 | } |
| 724 | per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p); |
| 725 | mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex); |
| 726 | } |
| 727 | |
| 728 | return 0; |
| 729 | |
| 730 | err: |
| 731 | for_each_possible_cpu(cpu) { |
| 732 | if (cpu == err_cpu) |
| 733 | break; |
| 734 | free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf); |
| 735 | } |
| 736 | |
| 737 | free_percpu(uprobe_cpu_buffer); |
| 738 | return -ENOMEM; |
| 739 | } |
| 740 | |
| 741 | static int uprobe_buffer_enable(void) |
| 742 | { |
| 743 | int ret = 0; |
| 744 | |
| 745 | BUG_ON(!mutex_is_locked(&event_mutex)); |
| 746 | |
| 747 | if (uprobe_buffer_refcnt++ == 0) { |
| 748 | ret = uprobe_buffer_init(); |
| 749 | if (ret < 0) |
| 750 | uprobe_buffer_refcnt--; |
| 751 | } |
| 752 | |
| 753 | return ret; |
| 754 | } |
| 755 | |
| 756 | static void uprobe_buffer_disable(void) |
| 757 | { |
zhangwei(Jovi) | 6ea6215 | 2014-04-17 16:05:19 +0800 | [diff] [blame] | 758 | int cpu; |
| 759 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame] | 760 | BUG_ON(!mutex_is_locked(&event_mutex)); |
| 761 | |
| 762 | if (--uprobe_buffer_refcnt == 0) { |
zhangwei(Jovi) | 6ea6215 | 2014-04-17 16:05:19 +0800 | [diff] [blame] | 763 | for_each_possible_cpu(cpu) |
| 764 | free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, |
| 765 | cpu)->buf); |
| 766 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame] | 767 | free_percpu(uprobe_cpu_buffer); |
| 768 | uprobe_cpu_buffer = NULL; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | static struct uprobe_cpu_buffer *uprobe_buffer_get(void) |
| 773 | { |
| 774 | struct uprobe_cpu_buffer *ucb; |
| 775 | int cpu; |
| 776 | |
| 777 | cpu = raw_smp_processor_id(); |
| 778 | ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu); |
| 779 | |
| 780 | /* |
| 781 | * Use per-cpu buffers for fastest access, but we might migrate |
| 782 | * so the mutex makes sure we have sole access to it. |
| 783 | */ |
| 784 | mutex_lock(&ucb->mutex); |
| 785 | |
| 786 | return ucb; |
| 787 | } |
| 788 | |
| 789 | static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb) |
| 790 | { |
| 791 | mutex_unlock(&ucb->mutex); |
| 792 | } |
| 793 | |
Namhyung Kim | a43b970 | 2014-01-17 17:08:36 +0900 | [diff] [blame] | 794 | static void __uprobe_trace_func(struct trace_uprobe *tu, |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 795 | unsigned long func, struct pt_regs *regs, |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 796 | struct uprobe_cpu_buffer *ucb, int dsize, |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 797 | struct trace_event_file *trace_file) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 798 | { |
| 799 | struct uprobe_trace_entry_head *entry; |
| 800 | struct ring_buffer_event *event; |
| 801 | struct ring_buffer *buffer; |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 802 | void *data; |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 803 | int size, esize; |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 804 | struct trace_event_call *call = &tu->tp.call; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 805 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 806 | WARN_ON(call != trace_file->event_call); |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 807 | |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 808 | if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE)) |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 809 | return; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 810 | |
Steven Rostedt (Red Hat) | 09a5059 | 2015-05-13 15:21:25 -0400 | [diff] [blame] | 811 | if (trace_trigger_soft_disabled(trace_file)) |
Namhyung Kim | ca3b162 | 2014-01-17 17:08:39 +0900 | [diff] [blame] | 812 | return; |
| 813 | |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 814 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame] | 815 | size = esize + tu->tp.size + dsize; |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 816 | event = trace_event_buffer_lock_reserve(&buffer, trace_file, |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 817 | call->event.type, size, 0, 0); |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame] | 818 | if (!event) |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 819 | return; |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame] | 820 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 821 | entry = ring_buffer_event_data(event); |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 822 | if (is_ret_probe(tu)) { |
| 823 | entry->vaddr[0] = func; |
| 824 | entry->vaddr[1] = instruction_pointer(regs); |
| 825 | data = DATAOF_TRACE_ENTRY(entry, true); |
| 826 | } else { |
| 827 | entry->vaddr[0] = instruction_pointer(regs); |
| 828 | data = DATAOF_TRACE_ENTRY(entry, false); |
| 829 | } |
| 830 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame] | 831 | memcpy(data, ucb->buf, tu->tp.size + dsize); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 832 | |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 833 | event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0); |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 834 | } |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 835 | |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 836 | /* uprobe handler */ |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 837 | static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs, |
| 838 | struct uprobe_cpu_buffer *ucb, int dsize) |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 839 | { |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 840 | struct event_file_link *link; |
| 841 | |
| 842 | if (is_ret_probe(tu)) |
| 843 | return 0; |
| 844 | |
| 845 | rcu_read_lock(); |
| 846 | list_for_each_entry_rcu(link, &tu->tp.files, list) |
| 847 | __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file); |
| 848 | rcu_read_unlock(); |
| 849 | |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 850 | return 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 851 | } |
| 852 | |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 853 | static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func, |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 854 | struct pt_regs *regs, |
| 855 | struct uprobe_cpu_buffer *ucb, int dsize) |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 856 | { |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 857 | struct event_file_link *link; |
| 858 | |
| 859 | rcu_read_lock(); |
| 860 | list_for_each_entry_rcu(link, &tu->tp.files, list) |
| 861 | __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file); |
| 862 | rcu_read_unlock(); |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 863 | } |
| 864 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 865 | /* Event entry printers */ |
| 866 | static enum print_line_t |
| 867 | print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event) |
| 868 | { |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 869 | struct uprobe_trace_entry_head *entry; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 870 | struct trace_seq *s = &iter->seq; |
| 871 | struct trace_uprobe *tu; |
| 872 | u8 *data; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 873 | |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 874 | entry = (struct uprobe_trace_entry_head *)iter->ent; |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 875 | tu = container_of(event, struct trace_uprobe, tp.call.event); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 876 | |
Oleg Nesterov | 3ede82d | 2013-03-30 19:48:09 +0100 | [diff] [blame] | 877 | if (is_ret_probe(tu)) { |
Steven Rostedt (Red Hat) | 8579a10 | 2014-11-12 17:26:57 -0500 | [diff] [blame] | 878 | trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", |
Steven Rostedt (Red Hat) | 687fcc4 | 2015-05-13 14:20:14 -0400 | [diff] [blame] | 879 | trace_event_name(&tu->tp.call), |
Steven Rostedt (Red Hat) | 8579a10 | 2014-11-12 17:26:57 -0500 | [diff] [blame] | 880 | entry->vaddr[1], entry->vaddr[0]); |
Oleg Nesterov | 3ede82d | 2013-03-30 19:48:09 +0100 | [diff] [blame] | 881 | data = DATAOF_TRACE_ENTRY(entry, true); |
| 882 | } else { |
Steven Rostedt (Red Hat) | 8579a10 | 2014-11-12 17:26:57 -0500 | [diff] [blame] | 883 | trace_seq_printf(s, "%s: (0x%lx)", |
Steven Rostedt (Red Hat) | 687fcc4 | 2015-05-13 14:20:14 -0400 | [diff] [blame] | 884 | trace_event_name(&tu->tp.call), |
Steven Rostedt (Red Hat) | 8579a10 | 2014-11-12 17:26:57 -0500 | [diff] [blame] | 885 | entry->vaddr[0]); |
Oleg Nesterov | 3ede82d | 2013-03-30 19:48:09 +0100 | [diff] [blame] | 886 | data = DATAOF_TRACE_ENTRY(entry, false); |
| 887 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 888 | |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 889 | if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0) |
| 890 | goto out; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 891 | |
Steven Rostedt (Red Hat) | 8579a10 | 2014-11-12 17:26:57 -0500 | [diff] [blame] | 892 | trace_seq_putc(s, '\n'); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 893 | |
Steven Rostedt (Red Hat) | 8579a10 | 2014-11-12 17:26:57 -0500 | [diff] [blame] | 894 | out: |
| 895 | return trace_handle_return(s); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 896 | } |
| 897 | |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 898 | typedef bool (*filter_func_t)(struct uprobe_consumer *self, |
| 899 | enum uprobe_filter_ctx ctx, |
| 900 | struct mm_struct *mm); |
| 901 | |
| 902 | static int |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 903 | probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file, |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 904 | filter_func_t filter) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 905 | { |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 906 | bool enabled = trace_probe_is_enabled(&tu->tp); |
| 907 | struct event_file_link *link = NULL; |
| 908 | int ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 909 | |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 910 | if (file) { |
Oleg Nesterov | 4821254 | 2014-06-27 19:01:36 +0200 | [diff] [blame] | 911 | if (tu->tp.flags & TP_FLAG_PROFILE) |
| 912 | return -EINTR; |
| 913 | |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 914 | link = kmalloc(sizeof(*link), GFP_KERNEL); |
| 915 | if (!link) |
| 916 | return -ENOMEM; |
| 917 | |
| 918 | link->file = file; |
| 919 | list_add_tail_rcu(&link->list, &tu->tp.files); |
| 920 | |
| 921 | tu->tp.flags |= TP_FLAG_TRACE; |
Oleg Nesterov | 4821254 | 2014-06-27 19:01:36 +0200 | [diff] [blame] | 922 | } else { |
| 923 | if (tu->tp.flags & TP_FLAG_TRACE) |
| 924 | return -EINTR; |
| 925 | |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 926 | tu->tp.flags |= TP_FLAG_PROFILE; |
Oleg Nesterov | 4821254 | 2014-06-27 19:01:36 +0200 | [diff] [blame] | 927 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 928 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 929 | WARN_ON(!uprobe_filter_is_empty(&tu->filter)); |
| 930 | |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 931 | if (enabled) |
| 932 | return 0; |
| 933 | |
Oleg Nesterov | fb6bab6 | 2014-06-27 19:01:46 +0200 | [diff] [blame] | 934 | ret = uprobe_buffer_enable(); |
| 935 | if (ret) |
| 936 | goto err_flags; |
| 937 | |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 938 | tu->consumer.filter = filter; |
Song Liu | 0c92c7a | 2018-04-23 10:21:34 -0700 | [diff] [blame] | 939 | tu->inode = d_real_inode(tu->path.dentry); |
Ravi Bangoria | 1cc3316 | 2018-08-20 10:12:47 +0530 | [diff] [blame] | 940 | if (tu->ref_ctr_offset) { |
| 941 | ret = uprobe_register_refctr(tu->inode, tu->offset, |
| 942 | tu->ref_ctr_offset, &tu->consumer); |
| 943 | } else { |
| 944 | ret = uprobe_register(tu->inode, tu->offset, &tu->consumer); |
| 945 | } |
| 946 | |
Oleg Nesterov | fb6bab6 | 2014-06-27 19:01:46 +0200 | [diff] [blame] | 947 | if (ret) |
| 948 | goto err_buffer; |
Oleg Nesterov | 4161824 | 2013-01-27 18:36:24 +0100 | [diff] [blame] | 949 | |
Oleg Nesterov | fb6bab6 | 2014-06-27 19:01:46 +0200 | [diff] [blame] | 950 | return 0; |
| 951 | |
| 952 | err_buffer: |
| 953 | uprobe_buffer_disable(); |
| 954 | |
| 955 | err_flags: |
| 956 | if (file) { |
| 957 | list_del(&link->list); |
| 958 | kfree(link); |
| 959 | tu->tp.flags &= ~TP_FLAG_TRACE; |
| 960 | } else { |
| 961 | tu->tp.flags &= ~TP_FLAG_PROFILE; |
| 962 | } |
Oleg Nesterov | 4161824 | 2013-01-27 18:36:24 +0100 | [diff] [blame] | 963 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 964 | } |
| 965 | |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 966 | static void |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 967 | probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 968 | { |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 969 | if (!trace_probe_is_enabled(&tu->tp)) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 970 | return; |
| 971 | |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 972 | if (file) { |
| 973 | struct event_file_link *link; |
| 974 | |
| 975 | link = find_event_file_link(&tu->tp, file); |
| 976 | if (!link) |
| 977 | return; |
| 978 | |
| 979 | list_del_rcu(&link->list); |
| 980 | /* synchronize with u{,ret}probe_trace_func */ |
Steven Rostedt (VMware) | 016f8ff | 2018-08-09 15:37:59 -0400 | [diff] [blame] | 981 | synchronize_rcu(); |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 982 | kfree(link); |
| 983 | |
| 984 | if (!list_empty(&tu->tp.files)) |
| 985 | return; |
| 986 | } |
| 987 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 988 | WARN_ON(!uprobe_filter_is_empty(&tu->filter)); |
| 989 | |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 990 | uprobe_unregister(tu->inode, tu->offset, &tu->consumer); |
Song Liu | 0c92c7a | 2018-04-23 10:21:34 -0700 | [diff] [blame] | 991 | tu->inode = NULL; |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 992 | tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE; |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame] | 993 | |
| 994 | uprobe_buffer_disable(); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 995 | } |
| 996 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 997 | static int uprobe_event_define_fields(struct trace_event_call *event_call) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 998 | { |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 999 | int ret, size; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1000 | struct uprobe_trace_entry_head field; |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 1001 | struct trace_uprobe *tu = event_call->data; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1002 | |
Oleg Nesterov | 4d1298e | 2013-03-30 19:23:15 +0100 | [diff] [blame] | 1003 | if (is_ret_probe(tu)) { |
| 1004 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0); |
| 1005 | DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0); |
| 1006 | size = SIZEOF_TRACE_ENTRY(true); |
| 1007 | } else { |
| 1008 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0); |
| 1009 | size = SIZEOF_TRACE_ENTRY(false); |
| 1010 | } |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1011 | |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame] | 1012 | return traceprobe_define_arg_fields(event_call, size, &tu->tp); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1013 | } |
| 1014 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1015 | #ifdef CONFIG_PERF_EVENTS |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 1016 | static bool |
| 1017 | __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm) |
| 1018 | { |
| 1019 | struct perf_event *event; |
| 1020 | |
| 1021 | if (filter->nr_systemwide) |
| 1022 | return true; |
| 1023 | |
| 1024 | list_for_each_entry(event, &filter->perf_events, hw.tp_list) { |
Peter Zijlstra | 50f16a8 | 2015-03-05 22:10:19 +0100 | [diff] [blame] | 1025 | if (event->hw.target->mm == mm) |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 1026 | return true; |
| 1027 | } |
| 1028 | |
| 1029 | return false; |
| 1030 | } |
| 1031 | |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 1032 | static inline bool |
| 1033 | uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event) |
| 1034 | { |
Peter Zijlstra | 50f16a8 | 2015-03-05 22:10:19 +0100 | [diff] [blame] | 1035 | return __uprobe_perf_filter(&tu->filter, event->hw.target->mm); |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 1036 | } |
| 1037 | |
Oleg Nesterov | ce5f36a | 2014-04-24 13:26:01 +0200 | [diff] [blame] | 1038 | static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event) |
| 1039 | { |
| 1040 | bool done; |
| 1041 | |
| 1042 | write_lock(&tu->filter.rwlock); |
Peter Zijlstra | 50f16a8 | 2015-03-05 22:10:19 +0100 | [diff] [blame] | 1043 | if (event->hw.target) { |
Oleg Nesterov | ce5f36a | 2014-04-24 13:26:01 +0200 | [diff] [blame] | 1044 | list_del(&event->hw.tp_list); |
| 1045 | done = tu->filter.nr_systemwide || |
Peter Zijlstra | 50f16a8 | 2015-03-05 22:10:19 +0100 | [diff] [blame] | 1046 | (event->hw.target->flags & PF_EXITING) || |
Oleg Nesterov | ce5f36a | 2014-04-24 13:26:01 +0200 | [diff] [blame] | 1047 | uprobe_filter_event(tu, event); |
| 1048 | } else { |
| 1049 | tu->filter.nr_systemwide--; |
| 1050 | done = tu->filter.nr_systemwide; |
| 1051 | } |
| 1052 | write_unlock(&tu->filter.rwlock); |
| 1053 | |
| 1054 | if (!done) |
Oleg Nesterov | 927d687 | 2014-04-24 13:33:31 +0200 | [diff] [blame] | 1055 | return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false); |
Oleg Nesterov | ce5f36a | 2014-04-24 13:26:01 +0200 | [diff] [blame] | 1056 | |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 1060 | static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event) |
| 1061 | { |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 1062 | bool done; |
Oleg Nesterov | 927d687 | 2014-04-24 13:33:31 +0200 | [diff] [blame] | 1063 | int err; |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 1064 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 1065 | write_lock(&tu->filter.rwlock); |
Peter Zijlstra | 50f16a8 | 2015-03-05 22:10:19 +0100 | [diff] [blame] | 1066 | if (event->hw.target) { |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 1067 | /* |
| 1068 | * event->parent != NULL means copy_process(), we can avoid |
| 1069 | * uprobe_apply(). current->mm must be probed and we can rely |
| 1070 | * on dup_mmap() which preserves the already installed bp's. |
| 1071 | * |
| 1072 | * attr.enable_on_exec means that exec/mmap will install the |
| 1073 | * breakpoints we need. |
| 1074 | */ |
| 1075 | done = tu->filter.nr_systemwide || |
| 1076 | event->parent || event->attr.enable_on_exec || |
| 1077 | uprobe_filter_event(tu, event); |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 1078 | list_add(&event->hw.tp_list, &tu->filter.perf_events); |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 1079 | } else { |
| 1080 | done = tu->filter.nr_systemwide; |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 1081 | tu->filter.nr_systemwide++; |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 1082 | } |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 1083 | write_unlock(&tu->filter.rwlock); |
| 1084 | |
Oleg Nesterov | 927d687 | 2014-04-24 13:33:31 +0200 | [diff] [blame] | 1085 | err = 0; |
| 1086 | if (!done) { |
| 1087 | err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true); |
| 1088 | if (err) |
| 1089 | uprobe_perf_close(tu, event); |
| 1090 | } |
| 1091 | return err; |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 1092 | } |
| 1093 | |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 1094 | static bool uprobe_perf_filter(struct uprobe_consumer *uc, |
| 1095 | enum uprobe_filter_ctx ctx, struct mm_struct *mm) |
| 1096 | { |
| 1097 | struct trace_uprobe *tu; |
| 1098 | int ret; |
| 1099 | |
| 1100 | tu = container_of(uc, struct trace_uprobe, consumer); |
| 1101 | read_lock(&tu->filter.rwlock); |
| 1102 | ret = __uprobe_perf_filter(&tu->filter, mm); |
| 1103 | read_unlock(&tu->filter.rwlock); |
| 1104 | |
| 1105 | return ret; |
| 1106 | } |
| 1107 | |
Namhyung Kim | a43b970 | 2014-01-17 17:08:36 +0900 | [diff] [blame] | 1108 | static void __uprobe_perf_func(struct trace_uprobe *tu, |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1109 | unsigned long func, struct pt_regs *regs, |
| 1110 | struct uprobe_cpu_buffer *ucb, int dsize) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1111 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1112 | struct trace_event_call *call = &tu->tp.call; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1113 | struct uprobe_trace_entry_head *entry; |
| 1114 | struct hlist_head *head; |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 1115 | void *data; |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1116 | int size, esize; |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame] | 1117 | int rctx; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1118 | |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1119 | if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs)) |
Wang Nan | 04a22fa | 2015-07-01 02:13:50 +0000 | [diff] [blame] | 1120 | return; |
| 1121 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame] | 1122 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
| 1123 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame] | 1124 | size = esize + tu->tp.size + dsize; |
| 1125 | size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32); |
| 1126 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough")) |
| 1127 | return; |
| 1128 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1129 | preempt_disable(); |
Oleg Nesterov | 515619f | 2013-04-13 15:36:49 +0200 | [diff] [blame] | 1130 | head = this_cpu_ptr(call->perf_events); |
| 1131 | if (hlist_empty(head)) |
| 1132 | goto out; |
| 1133 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 1134 | entry = perf_trace_buf_alloc(size, NULL, &rctx); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1135 | if (!entry) |
| 1136 | goto out; |
| 1137 | |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 1138 | if (is_ret_probe(tu)) { |
| 1139 | entry->vaddr[0] = func; |
Oleg Nesterov | 32520b2 | 2013-04-10 16:25:49 +0200 | [diff] [blame] | 1140 | entry->vaddr[1] = instruction_pointer(regs); |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 1141 | data = DATAOF_TRACE_ENTRY(entry, true); |
| 1142 | } else { |
Oleg Nesterov | 32520b2 | 2013-04-10 16:25:49 +0200 | [diff] [blame] | 1143 | entry->vaddr[0] = instruction_pointer(regs); |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 1144 | data = DATAOF_TRACE_ENTRY(entry, false); |
| 1145 | } |
| 1146 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame] | 1147 | memcpy(data, ucb->buf, tu->tp.size + dsize); |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1148 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame] | 1149 | if (size - esize > tu->tp.size + dsize) { |
| 1150 | int len = tu->tp.size + dsize; |
| 1151 | |
| 1152 | memset(data + len, 0, size - esize - len); |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1153 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1154 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 1155 | perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, |
Peter Zijlstra | 8fd0fbb | 2017-10-11 09:45:29 +0200 | [diff] [blame] | 1156 | head, NULL); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1157 | out: |
| 1158 | preempt_enable(); |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | /* uprobe profile handler */ |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1162 | static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs, |
| 1163 | struct uprobe_cpu_buffer *ucb, int dsize) |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 1164 | { |
| 1165 | if (!uprobe_perf_filter(&tu->consumer, 0, current->mm)) |
| 1166 | return UPROBE_HANDLER_REMOVE; |
| 1167 | |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 1168 | if (!is_ret_probe(tu)) |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1169 | __uprobe_perf_func(tu, 0, regs, ucb, dsize); |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 1170 | return 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1171 | } |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 1172 | |
| 1173 | static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func, |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1174 | struct pt_regs *regs, |
| 1175 | struct uprobe_cpu_buffer *ucb, int dsize) |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 1176 | { |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1177 | __uprobe_perf_func(tu, func, regs, ucb, dsize); |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 1178 | } |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 1179 | |
| 1180 | int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type, |
| 1181 | const char **filename, u64 *probe_offset, |
| 1182 | bool perf_type_tracepoint) |
| 1183 | { |
| 1184 | const char *pevent = trace_event_name(event->tp_event); |
| 1185 | const char *group = event->tp_event->class->system; |
| 1186 | struct trace_uprobe *tu; |
| 1187 | |
| 1188 | if (perf_type_tracepoint) |
| 1189 | tu = find_probe_event(pevent, group); |
| 1190 | else |
| 1191 | tu = event->tp_event->data; |
| 1192 | if (!tu) |
| 1193 | return -EINVAL; |
| 1194 | |
| 1195 | *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE |
| 1196 | : BPF_FD_TYPE_UPROBE; |
| 1197 | *filename = tu->filename; |
| 1198 | *probe_offset = tu->offset; |
| 1199 | return 0; |
| 1200 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1201 | #endif /* CONFIG_PERF_EVENTS */ |
| 1202 | |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 1203 | static int |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 1204 | trace_uprobe_register(struct trace_event_call *event, enum trace_reg type, |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 1205 | void *data) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1206 | { |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 1207 | struct trace_uprobe *tu = event->data; |
Steven Rostedt (Red Hat) | 7f1d2f8 | 2015-05-05 10:09:53 -0400 | [diff] [blame] | 1208 | struct trace_event_file *file = data; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1209 | |
| 1210 | switch (type) { |
| 1211 | case TRACE_REG_REGISTER: |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 1212 | return probe_event_enable(tu, file, NULL); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1213 | |
| 1214 | case TRACE_REG_UNREGISTER: |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 1215 | probe_event_disable(tu, file); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1216 | return 0; |
| 1217 | |
| 1218 | #ifdef CONFIG_PERF_EVENTS |
| 1219 | case TRACE_REG_PERF_REGISTER: |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 1220 | return probe_event_enable(tu, NULL, uprobe_perf_filter); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1221 | |
| 1222 | case TRACE_REG_PERF_UNREGISTER: |
zhangwei(Jovi) | 70ed91c | 2014-01-17 17:08:38 +0900 | [diff] [blame] | 1223 | probe_event_disable(tu, NULL); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1224 | return 0; |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 1225 | |
| 1226 | case TRACE_REG_PERF_OPEN: |
| 1227 | return uprobe_perf_open(tu, data); |
| 1228 | |
| 1229 | case TRACE_REG_PERF_CLOSE: |
| 1230 | return uprobe_perf_close(tu, data); |
| 1231 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1232 | #endif |
| 1233 | default: |
| 1234 | return 0; |
| 1235 | } |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs) |
| 1240 | { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1241 | struct trace_uprobe *tu; |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 1242 | struct uprobe_dispatch_data udd; |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1243 | struct uprobe_cpu_buffer *ucb; |
| 1244 | int dsize, esize; |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 1245 | int ret = 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1246 | |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1247 | |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 1248 | tu = container_of(con, struct trace_uprobe, consumer); |
Oleg Nesterov | 1b47aef | 2013-01-31 19:55:27 +0100 | [diff] [blame] | 1249 | tu->nhit++; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1250 | |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 1251 | udd.tu = tu; |
| 1252 | udd.bp_addr = instruction_pointer(regs); |
| 1253 | |
| 1254 | current->utask->vaddr = (unsigned long) &udd; |
| 1255 | |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1256 | if (WARN_ON_ONCE(!uprobe_cpu_buffer)) |
| 1257 | return 0; |
| 1258 | |
| 1259 | dsize = __get_data_size(&tu->tp, regs); |
| 1260 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
| 1261 | |
| 1262 | ucb = uprobe_buffer_get(); |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 1263 | store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize); |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1264 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1265 | if (tu->tp.flags & TP_FLAG_TRACE) |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1266 | ret |= uprobe_trace_func(tu, regs, ucb, dsize); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1267 | |
| 1268 | #ifdef CONFIG_PERF_EVENTS |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1269 | if (tu->tp.flags & TP_FLAG_PROFILE) |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1270 | ret |= uprobe_perf_func(tu, regs, ucb, dsize); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1271 | #endif |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1272 | uprobe_buffer_put(ucb); |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 1273 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1274 | } |
| 1275 | |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 1276 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
| 1277 | unsigned long func, struct pt_regs *regs) |
| 1278 | { |
| 1279 | struct trace_uprobe *tu; |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 1280 | struct uprobe_dispatch_data udd; |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1281 | struct uprobe_cpu_buffer *ucb; |
| 1282 | int dsize, esize; |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 1283 | |
| 1284 | tu = container_of(con, struct trace_uprobe, consumer); |
| 1285 | |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 1286 | udd.tu = tu; |
| 1287 | udd.bp_addr = func; |
| 1288 | |
| 1289 | current->utask->vaddr = (unsigned long) &udd; |
| 1290 | |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1291 | if (WARN_ON_ONCE(!uprobe_cpu_buffer)) |
| 1292 | return 0; |
| 1293 | |
| 1294 | dsize = __get_data_size(&tu->tp, regs); |
| 1295 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
| 1296 | |
| 1297 | ucb = uprobe_buffer_get(); |
Masami Hiramatsu | 9178412 | 2018-04-25 21:19:01 +0900 | [diff] [blame] | 1298 | store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize); |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1299 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1300 | if (tu->tp.flags & TP_FLAG_TRACE) |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1301 | uretprobe_trace_func(tu, func, regs, ucb, dsize); |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 1302 | |
| 1303 | #ifdef CONFIG_PERF_EVENTS |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1304 | if (tu->tp.flags & TP_FLAG_PROFILE) |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1305 | uretprobe_perf_func(tu, func, regs, ucb, dsize); |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 1306 | #endif |
Namhyung Kim | dd9fa55 | 2014-01-17 17:08:37 +0900 | [diff] [blame] | 1307 | uprobe_buffer_put(ucb); |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 1308 | return 0; |
| 1309 | } |
| 1310 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1311 | static struct trace_event_functions uprobe_funcs = { |
| 1312 | .trace = print_uprobe_event |
| 1313 | }; |
| 1314 | |
Song Liu | 33ea4b2 | 2017-12-06 14:45:16 -0800 | [diff] [blame] | 1315 | static inline void init_trace_event_call(struct trace_uprobe *tu, |
| 1316 | struct trace_event_call *call) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1317 | { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1318 | INIT_LIST_HEAD(&call->class->fields); |
| 1319 | call->event.funcs = &uprobe_funcs; |
| 1320 | call->class->define_fields = uprobe_event_define_fields; |
| 1321 | |
Song Liu | 33ea4b2 | 2017-12-06 14:45:16 -0800 | [diff] [blame] | 1322 | call->flags = TRACE_EVENT_FL_UPROBE; |
| 1323 | call->class->reg = trace_uprobe_register; |
| 1324 | call->data = tu; |
| 1325 | } |
| 1326 | |
| 1327 | static int register_uprobe_event(struct trace_uprobe *tu) |
| 1328 | { |
| 1329 | struct trace_event_call *call = &tu->tp.call; |
| 1330 | int ret = 0; |
| 1331 | |
| 1332 | init_trace_event_call(tu, call); |
| 1333 | |
Masami Hiramatsu | 0a46c85 | 2018-04-25 21:19:30 +0900 | [diff] [blame] | 1334 | if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1335 | return -ENOMEM; |
| 1336 | |
Steven Rostedt (Red Hat) | 9023c93 | 2015-05-05 09:39:12 -0400 | [diff] [blame] | 1337 | ret = register_trace_event(&call->event); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1338 | if (!ret) { |
| 1339 | kfree(call->print_fmt); |
| 1340 | return -ENODEV; |
| 1341 | } |
Oleg Nesterov | ede392a | 2014-07-15 20:48:24 +0200 | [diff] [blame] | 1342 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1343 | ret = trace_add_event_call(call); |
| 1344 | |
| 1345 | if (ret) { |
Mathieu Desnoyers | de7b297 | 2014-04-08 17:26:21 -0400 | [diff] [blame] | 1346 | pr_info("Failed to register uprobe event: %s\n", |
Steven Rostedt (Red Hat) | 687fcc4 | 2015-05-13 14:20:14 -0400 | [diff] [blame] | 1347 | trace_event_name(call)); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1348 | kfree(call->print_fmt); |
Steven Rostedt (Red Hat) | 9023c93 | 2015-05-05 09:39:12 -0400 | [diff] [blame] | 1349 | unregister_trace_event(&call->event); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | return ret; |
| 1353 | } |
| 1354 | |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 1355 | static int unregister_uprobe_event(struct trace_uprobe *tu) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1356 | { |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 1357 | int ret; |
| 1358 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1359 | /* tu->event is unregistered in trace_remove_event_call() */ |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1360 | ret = trace_remove_event_call(&tu->tp.call); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 1361 | if (ret) |
| 1362 | return ret; |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1363 | kfree(tu->tp.call.print_fmt); |
| 1364 | tu->tp.call.print_fmt = NULL; |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 1365 | return 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1366 | } |
| 1367 | |
Song Liu | 33ea4b2 | 2017-12-06 14:45:16 -0800 | [diff] [blame] | 1368 | #ifdef CONFIG_PERF_EVENTS |
| 1369 | struct trace_event_call * |
Song Liu | a6ca88b | 2018-10-01 22:36:36 -0700 | [diff] [blame] | 1370 | create_local_trace_uprobe(char *name, unsigned long offs, |
| 1371 | unsigned long ref_ctr_offset, bool is_return) |
Song Liu | 33ea4b2 | 2017-12-06 14:45:16 -0800 | [diff] [blame] | 1372 | { |
| 1373 | struct trace_uprobe *tu; |
Song Liu | 33ea4b2 | 2017-12-06 14:45:16 -0800 | [diff] [blame] | 1374 | struct path path; |
| 1375 | int ret; |
| 1376 | |
| 1377 | ret = kern_path(name, LOOKUP_FOLLOW, &path); |
| 1378 | if (ret) |
| 1379 | return ERR_PTR(ret); |
| 1380 | |
Song Liu | 0c92c7a | 2018-04-23 10:21:34 -0700 | [diff] [blame] | 1381 | if (!d_is_reg(path.dentry)) { |
| 1382 | path_put(&path); |
Song Liu | 33ea4b2 | 2017-12-06 14:45:16 -0800 | [diff] [blame] | 1383 | return ERR_PTR(-EINVAL); |
| 1384 | } |
| 1385 | |
| 1386 | /* |
| 1387 | * local trace_kprobes are not added to probe_list, so they are never |
| 1388 | * searched in find_trace_kprobe(). Therefore, there is no concern of |
| 1389 | * duplicated name "DUMMY_EVENT" here. |
| 1390 | */ |
| 1391 | tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0, |
| 1392 | is_return); |
| 1393 | |
| 1394 | if (IS_ERR(tu)) { |
| 1395 | pr_info("Failed to allocate trace_uprobe.(%d)\n", |
| 1396 | (int)PTR_ERR(tu)); |
Song Liu | 0c92c7a | 2018-04-23 10:21:34 -0700 | [diff] [blame] | 1397 | path_put(&path); |
Song Liu | 33ea4b2 | 2017-12-06 14:45:16 -0800 | [diff] [blame] | 1398 | return ERR_CAST(tu); |
| 1399 | } |
| 1400 | |
| 1401 | tu->offset = offs; |
Song Liu | 0c92c7a | 2018-04-23 10:21:34 -0700 | [diff] [blame] | 1402 | tu->path = path; |
Song Liu | a6ca88b | 2018-10-01 22:36:36 -0700 | [diff] [blame] | 1403 | tu->ref_ctr_offset = ref_ctr_offset; |
Song Liu | 33ea4b2 | 2017-12-06 14:45:16 -0800 | [diff] [blame] | 1404 | tu->filename = kstrdup(name, GFP_KERNEL); |
| 1405 | init_trace_event_call(tu, &tu->tp.call); |
| 1406 | |
Masami Hiramatsu | 0a46c85 | 2018-04-25 21:19:30 +0900 | [diff] [blame] | 1407 | if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) { |
Song Liu | 33ea4b2 | 2017-12-06 14:45:16 -0800 | [diff] [blame] | 1408 | ret = -ENOMEM; |
| 1409 | goto error; |
| 1410 | } |
| 1411 | |
| 1412 | return &tu->tp.call; |
| 1413 | error: |
| 1414 | free_trace_uprobe(tu); |
| 1415 | return ERR_PTR(ret); |
| 1416 | } |
| 1417 | |
| 1418 | void destroy_local_trace_uprobe(struct trace_event_call *event_call) |
| 1419 | { |
| 1420 | struct trace_uprobe *tu; |
| 1421 | |
| 1422 | tu = container_of(event_call, struct trace_uprobe, tp.call); |
| 1423 | |
| 1424 | kfree(tu->tp.call.print_fmt); |
| 1425 | tu->tp.call.print_fmt = NULL; |
| 1426 | |
| 1427 | free_trace_uprobe(tu); |
| 1428 | } |
| 1429 | #endif /* CONFIG_PERF_EVENTS */ |
| 1430 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1431 | /* Make a trace interface for controling probe points */ |
| 1432 | static __init int init_uprobe_trace(void) |
| 1433 | { |
| 1434 | struct dentry *d_tracer; |
| 1435 | |
| 1436 | d_tracer = tracing_init_dentry(); |
Steven Rostedt (Red Hat) | 14a5ae4 | 2015-01-20 11:14:16 -0500 | [diff] [blame] | 1437 | if (IS_ERR(d_tracer)) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1438 | return 0; |
| 1439 | |
| 1440 | trace_create_file("uprobe_events", 0644, d_tracer, |
| 1441 | NULL, &uprobe_events_ops); |
| 1442 | /* Profile interface */ |
| 1443 | trace_create_file("uprobe_profile", 0444, d_tracer, |
| 1444 | NULL, &uprobe_profile_ops); |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | fs_initcall(init_uprobe_trace); |