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