1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Generic dynamic event control interface 4 * 5 * Copyright (C) 2018 Masami Hiramatsu <mhiramat@kernel.org> 6 */ 7 8 #include <linux/debugfs.h> 9 #include <linux/kernel.h> 10 #include <linux/list.h> 11 #include <linux/mm.h> 12 #include <linux/mutex.h> 13 #include <linux/tracefs.h> 14 15 #include "trace.h" 16 #include "trace_output.h" /* for trace_event_sem */ 17 #include "trace_dynevent.h" 18 19 DEFINE_MUTEX(dyn_event_ops_mutex); 20 static LIST_HEAD(dyn_event_ops_list); 21 22 bool trace_event_dyn_try_get_ref(struct trace_event_call *dyn_call) 23 { 24 struct trace_event_call *call; 25 bool ret = false; 26 27 if (WARN_ON_ONCE(!(dyn_call->flags & TRACE_EVENT_FL_DYNAMIC))) 28 return false; 29 30 down_read(&trace_event_sem); 31 list_for_each_entry(call, &ftrace_events, list) { 32 if (call == dyn_call) { 33 atomic_inc(&dyn_call->refcnt); 34 ret = true; 35 } 36 } 37 up_read(&trace_event_sem); 38 return ret; 39 } 40 41 void trace_event_dyn_put_ref(struct trace_event_call *call) 42 { 43 if (WARN_ON_ONCE(!(call->flags & TRACE_EVENT_FL_DYNAMIC))) 44 return; 45 46 if (WARN_ON_ONCE(atomic_read(&call->refcnt) <= 0)) { 47 atomic_set(&call->refcnt, 0); 48 return; 49 } 50 51 atomic_dec(&call->refcnt); 52 } 53 54 bool trace_event_dyn_busy(struct trace_event_call *call) 55 { 56 return atomic_read(&call->refcnt) != 0; 57 } 58 59 int dyn_event_register(struct dyn_event_operations *ops) 60 { 61 if (!ops || !ops->create || !ops->show || !ops->is_busy || 62 !ops->free || !ops->match) 63 return -EINVAL; 64 65 INIT_LIST_HEAD(&ops->list); 66 mutex_lock(&dyn_event_ops_mutex); 67 list_add_tail(&ops->list, &dyn_event_ops_list); 68 mutex_unlock(&dyn_event_ops_mutex); 69 return 0; 70 } 71 72 int dyn_event_release(const char *raw_command, struct dyn_event_operations *type) 73 { 74 struct dyn_event *pos, *n; 75 char *system = NULL, *event, *p; 76 int argc, ret = -ENOENT; 77 char **argv __free(argv_free) = argv_split(GFP_KERNEL, raw_command, &argc); 78 79 if (!argv) 80 return -ENOMEM; 81 82 if (argv[0][0] == '-') { 83 if (argv[0][1] != ':') 84 return -EINVAL; 85 event = &argv[0][2]; 86 } else { 87 event = strchr(argv[0], ':'); 88 if (!event) 89 return -EINVAL; 90 event++; 91 } 92 93 p = strchr(event, '/'); 94 if (p) { 95 system = event; 96 event = p + 1; 97 *p = '\0'; 98 } 99 if (!system && event[0] == '\0') 100 return -EINVAL; 101 102 mutex_lock(&event_mutex); 103 for_each_dyn_event_safe(pos, n) { 104 if (type && type != pos->ops) 105 continue; 106 if (!pos->ops->match(system, event, 107 argc - 1, (const char **)argv + 1, pos)) 108 continue; 109 110 ret = pos->ops->free(pos); 111 if (ret) 112 break; 113 } 114 tracing_reset_all_online_cpus(); 115 mutex_unlock(&event_mutex); 116 return ret; 117 } 118 119 /* 120 * Locked version of event creation. The event creation must be protected by 121 * dyn_event_ops_mutex because of protecting trace_probe_log. 122 */ 123 int dyn_event_create(const char *raw_command, struct dyn_event_operations *type) 124 { 125 int ret; 126 127 mutex_lock(&dyn_event_ops_mutex); 128 ret = type->create(raw_command); 129 mutex_unlock(&dyn_event_ops_mutex); 130 return ret; 131 } 132 133 static int create_dyn_event(const char *raw_command) 134 { 135 struct dyn_event_operations *ops; 136 int ret = -ENODEV; 137 138 if (raw_command[0] == '-' || raw_command[0] == '!') 139 return dyn_event_release(raw_command, NULL); 140 141 mutex_lock(&dyn_event_ops_mutex); 142 list_for_each_entry(ops, &dyn_event_ops_list, list) { 143 ret = ops->create(raw_command); 144 if (!ret || ret != -ECANCELED) 145 break; 146 } 147 mutex_unlock(&dyn_event_ops_mutex); 148 if (ret == -ECANCELED) 149 ret = -EINVAL; 150 151 return ret; 152 } 153 154 /* Protected by event_mutex */ 155 LIST_HEAD(dyn_event_list); 156 157 void *dyn_event_seq_start(struct seq_file *m, loff_t *pos) 158 { 159 mutex_lock(&event_mutex); 160 return seq_list_start(&dyn_event_list, *pos); 161 } 162 163 void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos) 164 { 165 return seq_list_next(v, &dyn_event_list, pos); 166 } 167 168 void dyn_event_seq_stop(struct seq_file *m, void *v) 169 { 170 mutex_unlock(&event_mutex); 171 } 172 173 static int dyn_event_seq_show(struct seq_file *m, void *v) 174 { 175 struct dyn_event *ev = v; 176 177 if (ev && ev->ops) 178 return ev->ops->show(m, ev); 179 180 return 0; 181 } 182 183 static const struct seq_operations dyn_event_seq_op = { 184 .start = dyn_event_seq_start, 185 .next = dyn_event_seq_next, 186 .stop = dyn_event_seq_stop, 187 .show = dyn_event_seq_show 188 }; 189 190 /* 191 * dyn_events_release_all - Release all specific events 192 * @type: the dyn_event_operations * which filters releasing events 193 * 194 * This releases all events which ->ops matches @type. If @type is NULL, 195 * all events are released. 196 * Return -EBUSY if any of them are in use, and return other errors when 197 * it failed to free the given event. Except for -EBUSY, event releasing 198 * process will be aborted at that point and there may be some other 199 * releasable events on the list. 200 */ 201 int dyn_events_release_all(struct dyn_event_operations *type) 202 { 203 struct dyn_event *ev, *tmp; 204 int ret = 0; 205 206 mutex_lock(&event_mutex); 207 for_each_dyn_event(ev) { 208 if (type && ev->ops != type) 209 continue; 210 if (ev->ops->is_busy(ev)) { 211 ret = -EBUSY; 212 goto out; 213 } 214 } 215 for_each_dyn_event_safe(ev, tmp) { 216 if (type && ev->ops != type) 217 continue; 218 ret = ev->ops->free(ev); 219 if (ret) 220 break; 221 } 222 out: 223 tracing_reset_all_online_cpus(); 224 mutex_unlock(&event_mutex); 225 226 return ret; 227 } 228 229 static int dyn_event_open(struct inode *inode, struct file *file) 230 { 231 int ret; 232 233 ret = tracing_check_open_get_tr(NULL); 234 if (ret) 235 return ret; 236 237 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { 238 ret = dyn_events_release_all(NULL); 239 if (ret < 0) 240 return ret; 241 } 242 243 return seq_open(file, &dyn_event_seq_op); 244 } 245 246 static ssize_t dyn_event_write(struct file *file, const char __user *buffer, 247 size_t count, loff_t *ppos) 248 { 249 return trace_parse_run_command(file, buffer, count, ppos, 250 create_dyn_event); 251 } 252 253 static const struct file_operations dynamic_events_ops = { 254 .owner = THIS_MODULE, 255 .open = dyn_event_open, 256 .read = seq_read, 257 .llseek = seq_lseek, 258 .release = seq_release, 259 .write = dyn_event_write, 260 }; 261 262 /* Make a tracefs interface for controlling dynamic events */ 263 static __init int init_dynamic_event(void) 264 { 265 int ret; 266 267 ret = tracing_init_dentry(); 268 if (ret) 269 return 0; 270 271 trace_create_file("dynamic_events", TRACE_MODE_WRITE, NULL, 272 NULL, &dynamic_events_ops); 273 274 return 0; 275 } 276 fs_initcall(init_dynamic_event); 277 278 /** 279 * dynevent_arg_add - Add an arg to a dynevent_cmd 280 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd 281 * @arg: The argument to append to the current cmd 282 * @check_arg: An (optional) pointer to a function checking arg sanity 283 * 284 * Append an argument to a dynevent_cmd. The argument string will be 285 * appended to the current cmd string, followed by a separator, if 286 * applicable. Before the argument is added, the @check_arg function, 287 * if present, will be used to check the sanity of the current arg 288 * string. 289 * 290 * The cmd string and separator should be set using the 291 * dynevent_arg_init() before any arguments are added using this 292 * function. 293 * 294 * Return: 0 if successful, error otherwise. 295 */ 296 int dynevent_arg_add(struct dynevent_cmd *cmd, 297 struct dynevent_arg *arg, 298 dynevent_check_arg_fn_t check_arg) 299 { 300 int ret = 0; 301 302 if (check_arg) { 303 ret = check_arg(arg); 304 if (ret) 305 return ret; 306 } 307 308 ret = seq_buf_printf(&cmd->seq, " %s%c", arg->str, arg->separator); 309 if (ret) { 310 pr_err("String is too long: %s%c\n", arg->str, arg->separator); 311 return -E2BIG; 312 } 313 314 return ret; 315 } 316 317 /** 318 * dynevent_arg_pair_add - Add an arg pair to a dynevent_cmd 319 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd 320 * @arg_pair: The argument pair to append to the current cmd 321 * @check_arg: An (optional) pointer to a function checking arg sanity 322 * 323 * Append an argument pair to a dynevent_cmd. An argument pair 324 * consists of a left-hand-side argument and a right-hand-side 325 * argument separated by an operator, which can be whitespace, all 326 * followed by a separator, if applicable. This can be used to add 327 * arguments of the form 'type variable_name;' or 'x+y'. 328 * 329 * The lhs argument string will be appended to the current cmd string, 330 * followed by an operator, if applicable, followed by the rhs string, 331 * followed finally by a separator, if applicable. Before the 332 * argument is added, the @check_arg function, if present, will be 333 * used to check the sanity of the current arg strings. 334 * 335 * The cmd strings, operator, and separator should be set using the 336 * dynevent_arg_pair_init() before any arguments are added using this 337 * function. 338 * 339 * Return: 0 if successful, error otherwise. 340 */ 341 int dynevent_arg_pair_add(struct dynevent_cmd *cmd, 342 struct dynevent_arg_pair *arg_pair, 343 dynevent_check_arg_fn_t check_arg) 344 { 345 int ret = 0; 346 347 if (check_arg) { 348 ret = check_arg(arg_pair); 349 if (ret) 350 return ret; 351 } 352 353 ret = seq_buf_printf(&cmd->seq, " %s%c%s%c", arg_pair->lhs, 354 arg_pair->operator, arg_pair->rhs, 355 arg_pair->separator); 356 if (ret) { 357 pr_err("field string is too long: %s%c%s%c\n", arg_pair->lhs, 358 arg_pair->operator, arg_pair->rhs, 359 arg_pair->separator); 360 return -E2BIG; 361 } 362 363 return ret; 364 } 365 366 /** 367 * dynevent_str_add - Add a string to a dynevent_cmd 368 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd 369 * @str: The string to append to the current cmd 370 * 371 * Append a string to a dynevent_cmd. The string will be appended to 372 * the current cmd string as-is, with nothing prepended or appended. 373 * 374 * Return: 0 if successful, error otherwise. 375 */ 376 int dynevent_str_add(struct dynevent_cmd *cmd, const char *str) 377 { 378 int ret = 0; 379 380 ret = seq_buf_puts(&cmd->seq, str); 381 if (ret) { 382 pr_err("String is too long: %s\n", str); 383 return -E2BIG; 384 } 385 386 return ret; 387 } 388 389 /** 390 * dynevent_cmd_init - Initialize a dynevent_cmd object 391 * @cmd: A pointer to the dynevent_cmd struct representing the cmd 392 * @buf: A pointer to the buffer to generate the command into 393 * @maxlen: The length of the buffer the command will be generated into 394 * @type: The type of the cmd, checked against further operations 395 * @run_command: The type-specific function that will actually run the command 396 * 397 * Initialize a dynevent_cmd. A dynevent_cmd is used to build up and 398 * run dynamic event creation commands, such as commands for creating 399 * synthetic and kprobe events. Before calling any of the functions 400 * used to build the command, a dynevent_cmd object should be 401 * instantiated and initialized using this function. 402 * 403 * The initialization sets things up by saving a pointer to the 404 * user-supplied buffer and its length via the @buf and @maxlen 405 * params, and by saving the cmd-specific @type and @run_command 406 * params which are used to check subsequent dynevent_cmd operations 407 * and actually run the command when complete. 408 */ 409 void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen, 410 enum dynevent_type type, 411 dynevent_create_fn_t run_command) 412 { 413 memset(cmd, '\0', sizeof(*cmd)); 414 415 seq_buf_init(&cmd->seq, buf, maxlen); 416 cmd->type = type; 417 cmd->run_command = run_command; 418 } 419 420 /** 421 * dynevent_arg_init - Initialize a dynevent_arg object 422 * @arg: A pointer to the dynevent_arg struct representing the arg 423 * @separator: An (optional) separator, appended after adding the arg 424 * 425 * Initialize a dynevent_arg object. A dynevent_arg represents an 426 * object used to append single arguments to the current command 427 * string. After the arg string is successfully appended to the 428 * command string, the optional @separator is appended. If no 429 * separator was specified when initializing the arg, a space will be 430 * appended. 431 */ 432 void dynevent_arg_init(struct dynevent_arg *arg, 433 char separator) 434 { 435 memset(arg, '\0', sizeof(*arg)); 436 437 if (!separator) 438 separator = ' '; 439 arg->separator = separator; 440 } 441 442 /** 443 * dynevent_arg_pair_init - Initialize a dynevent_arg_pair object 444 * @arg_pair: A pointer to the dynevent_arg_pair struct representing the arg 445 * @operator: An (optional) operator, appended after adding the first arg 446 * @separator: An (optional) separator, appended after adding the second arg 447 * 448 * Initialize a dynevent_arg_pair object. A dynevent_arg_pair 449 * represents an object used to append argument pairs such as 'type 450 * variable_name;' or 'x+y' to the current command string. An 451 * argument pair consists of a left-hand-side argument and a 452 * right-hand-side argument separated by an operator, which can be 453 * whitespace, all followed by a separator, if applicable. After the 454 * first arg string is successfully appended to the command string, 455 * the optional @operator is appended, followed by the second arg and 456 * optional @separator. If no separator was specified when 457 * initializing the arg, a space will be appended. 458 */ 459 void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair, 460 char operator, char separator) 461 { 462 memset(arg_pair, '\0', sizeof(*arg_pair)); 463 464 if (!operator) 465 operator = ' '; 466 arg_pair->operator = operator; 467 468 if (!separator) 469 separator = ' '; 470 arg_pair->separator = separator; 471 } 472 473 /** 474 * dynevent_create - Create the dynamic event contained in dynevent_cmd 475 * @cmd: The dynevent_cmd object containing the dynamic event creation command 476 * 477 * Once a dynevent_cmd object has been successfully built up via the 478 * dynevent_cmd_init(), dynevent_arg_add() and dynevent_arg_pair_add() 479 * functions, this function runs the final command to actually create 480 * the event. 481 * 482 * Return: 0 if the event was successfully created, error otherwise. 483 */ 484 int dynevent_create(struct dynevent_cmd *cmd) 485 { 486 return cmd->run_command(cmd); 487 } 488 EXPORT_SYMBOL_GPL(dynevent_create); 489