trace_dynevent.c 13 KB

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