trace_dynevent.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generic dynamic event control interface
  4. *
  5. * Copyright (C) 2018 Masami Hiramatsu <[email protected]>
  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. static 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. static int create_dyn_event(const char *raw_command)
  110. {
  111. struct dyn_event_operations *ops;
  112. int ret = -ENODEV;
  113. if (raw_command[0] == '-' || raw_command[0] == '!')
  114. return dyn_event_release(raw_command, NULL);
  115. mutex_lock(&dyn_event_ops_mutex);
  116. list_for_each_entry(ops, &dyn_event_ops_list, list) {
  117. ret = ops->create(raw_command);
  118. if (!ret || ret != -ECANCELED)
  119. break;
  120. }
  121. mutex_unlock(&dyn_event_ops_mutex);
  122. if (ret == -ECANCELED)
  123. ret = -EINVAL;
  124. return ret;
  125. }
  126. /* Protected by event_mutex */
  127. LIST_HEAD(dyn_event_list);
  128. void *dyn_event_seq_start(struct seq_file *m, loff_t *pos)
  129. {
  130. mutex_lock(&event_mutex);
  131. return seq_list_start(&dyn_event_list, *pos);
  132. }
  133. void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos)
  134. {
  135. return seq_list_next(v, &dyn_event_list, pos);
  136. }
  137. void dyn_event_seq_stop(struct seq_file *m, void *v)
  138. {
  139. mutex_unlock(&event_mutex);
  140. }
  141. static int dyn_event_seq_show(struct seq_file *m, void *v)
  142. {
  143. struct dyn_event *ev = v;
  144. if (ev && ev->ops)
  145. return ev->ops->show(m, ev);
  146. return 0;
  147. }
  148. static const struct seq_operations dyn_event_seq_op = {
  149. .start = dyn_event_seq_start,
  150. .next = dyn_event_seq_next,
  151. .stop = dyn_event_seq_stop,
  152. .show = dyn_event_seq_show
  153. };
  154. /*
  155. * dyn_events_release_all - Release all specific events
  156. * @type: the dyn_event_operations * which filters releasing events
  157. *
  158. * This releases all events which ->ops matches @type. If @type is NULL,
  159. * all events are released.
  160. * Return -EBUSY if any of them are in use, and return other errors when
  161. * it failed to free the given event. Except for -EBUSY, event releasing
  162. * process will be aborted at that point and there may be some other
  163. * releasable events on the list.
  164. */
  165. int dyn_events_release_all(struct dyn_event_operations *type)
  166. {
  167. struct dyn_event *ev, *tmp;
  168. int ret = 0;
  169. mutex_lock(&event_mutex);
  170. for_each_dyn_event(ev) {
  171. if (type && ev->ops != type)
  172. continue;
  173. if (ev->ops->is_busy(ev)) {
  174. ret = -EBUSY;
  175. goto out;
  176. }
  177. }
  178. for_each_dyn_event_safe(ev, tmp) {
  179. if (type && ev->ops != type)
  180. continue;
  181. ret = ev->ops->free(ev);
  182. if (ret)
  183. break;
  184. }
  185. out:
  186. tracing_reset_all_online_cpus();
  187. mutex_unlock(&event_mutex);
  188. return ret;
  189. }
  190. static int dyn_event_open(struct inode *inode, struct file *file)
  191. {
  192. int ret;
  193. ret = tracing_check_open_get_tr(NULL);
  194. if (ret)
  195. return ret;
  196. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  197. ret = dyn_events_release_all(NULL);
  198. if (ret < 0)
  199. return ret;
  200. }
  201. return seq_open(file, &dyn_event_seq_op);
  202. }
  203. static ssize_t dyn_event_write(struct file *file, const char __user *buffer,
  204. size_t count, loff_t *ppos)
  205. {
  206. return trace_parse_run_command(file, buffer, count, ppos,
  207. create_dyn_event);
  208. }
  209. static const struct file_operations dynamic_events_ops = {
  210. .owner = THIS_MODULE,
  211. .open = dyn_event_open,
  212. .read = seq_read,
  213. .llseek = seq_lseek,
  214. .release = seq_release,
  215. .write = dyn_event_write,
  216. };
  217. /* Make a tracefs interface for controlling dynamic events */
  218. static __init int init_dynamic_event(void)
  219. {
  220. int ret;
  221. ret = tracing_init_dentry();
  222. if (ret)
  223. return 0;
  224. trace_create_file("dynamic_events", TRACE_MODE_WRITE, NULL,
  225. NULL, &dynamic_events_ops);
  226. return 0;
  227. }
  228. fs_initcall(init_dynamic_event);
  229. /**
  230. * dynevent_arg_add - Add an arg to a dynevent_cmd
  231. * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
  232. * @arg: The argument to append to the current cmd
  233. * @check_arg: An (optional) pointer to a function checking arg sanity
  234. *
  235. * Append an argument to a dynevent_cmd. The argument string will be
  236. * appended to the current cmd string, followed by a separator, if
  237. * applicable. Before the argument is added, the @check_arg function,
  238. * if present, will be used to check the sanity of the current arg
  239. * string.
  240. *
  241. * The cmd string and separator should be set using the
  242. * dynevent_arg_init() before any arguments are added using this
  243. * function.
  244. *
  245. * Return: 0 if successful, error otherwise.
  246. */
  247. int dynevent_arg_add(struct dynevent_cmd *cmd,
  248. struct dynevent_arg *arg,
  249. dynevent_check_arg_fn_t check_arg)
  250. {
  251. int ret = 0;
  252. if (check_arg) {
  253. ret = check_arg(arg);
  254. if (ret)
  255. return ret;
  256. }
  257. ret = seq_buf_printf(&cmd->seq, " %s%c", arg->str, arg->separator);
  258. if (ret) {
  259. pr_err("String is too long: %s%c\n", arg->str, arg->separator);
  260. return -E2BIG;
  261. }
  262. return ret;
  263. }
  264. /**
  265. * dynevent_arg_pair_add - Add an arg pair to a dynevent_cmd
  266. * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
  267. * @arg_pair: The argument pair to append to the current cmd
  268. * @check_arg: An (optional) pointer to a function checking arg sanity
  269. *
  270. * Append an argument pair to a dynevent_cmd. An argument pair
  271. * consists of a left-hand-side argument and a right-hand-side
  272. * argument separated by an operator, which can be whitespace, all
  273. * followed by a separator, if applicable. This can be used to add
  274. * arguments of the form 'type variable_name;' or 'x+y'.
  275. *
  276. * The lhs argument string will be appended to the current cmd string,
  277. * followed by an operator, if applicable, followed by the rhs string,
  278. * followed finally by a separator, if applicable. Before the
  279. * argument is added, the @check_arg function, if present, will be
  280. * used to check the sanity of the current arg strings.
  281. *
  282. * The cmd strings, operator, and separator should be set using the
  283. * dynevent_arg_pair_init() before any arguments are added using this
  284. * function.
  285. *
  286. * Return: 0 if successful, error otherwise.
  287. */
  288. int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
  289. struct dynevent_arg_pair *arg_pair,
  290. dynevent_check_arg_fn_t check_arg)
  291. {
  292. int ret = 0;
  293. if (check_arg) {
  294. ret = check_arg(arg_pair);
  295. if (ret)
  296. return ret;
  297. }
  298. ret = seq_buf_printf(&cmd->seq, " %s%c%s%c", arg_pair->lhs,
  299. arg_pair->operator, arg_pair->rhs,
  300. arg_pair->separator);
  301. if (ret) {
  302. pr_err("field string is too long: %s%c%s%c\n", arg_pair->lhs,
  303. arg_pair->operator, arg_pair->rhs,
  304. arg_pair->separator);
  305. return -E2BIG;
  306. }
  307. return ret;
  308. }
  309. /**
  310. * dynevent_str_add - Add a string to a dynevent_cmd
  311. * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
  312. * @str: The string to append to the current cmd
  313. *
  314. * Append a string to a dynevent_cmd. The string will be appended to
  315. * the current cmd string as-is, with nothing prepended or appended.
  316. *
  317. * Return: 0 if successful, error otherwise.
  318. */
  319. int dynevent_str_add(struct dynevent_cmd *cmd, const char *str)
  320. {
  321. int ret = 0;
  322. ret = seq_buf_puts(&cmd->seq, str);
  323. if (ret) {
  324. pr_err("String is too long: %s\n", str);
  325. return -E2BIG;
  326. }
  327. return ret;
  328. }
  329. /**
  330. * dynevent_cmd_init - Initialize a dynevent_cmd object
  331. * @cmd: A pointer to the dynevent_cmd struct representing the cmd
  332. * @buf: A pointer to the buffer to generate the command into
  333. * @maxlen: The length of the buffer the command will be generated into
  334. * @type: The type of the cmd, checked against further operations
  335. * @run_command: The type-specific function that will actually run the command
  336. *
  337. * Initialize a dynevent_cmd. A dynevent_cmd is used to build up and
  338. * run dynamic event creation commands, such as commands for creating
  339. * synthetic and kprobe events. Before calling any of the functions
  340. * used to build the command, a dynevent_cmd object should be
  341. * instantiated and initialized using this function.
  342. *
  343. * The initialization sets things up by saving a pointer to the
  344. * user-supplied buffer and its length via the @buf and @maxlen
  345. * params, and by saving the cmd-specific @type and @run_command
  346. * params which are used to check subsequent dynevent_cmd operations
  347. * and actually run the command when complete.
  348. */
  349. void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen,
  350. enum dynevent_type type,
  351. dynevent_create_fn_t run_command)
  352. {
  353. memset(cmd, '\0', sizeof(*cmd));
  354. seq_buf_init(&cmd->seq, buf, maxlen);
  355. cmd->type = type;
  356. cmd->run_command = run_command;
  357. }
  358. /**
  359. * dynevent_arg_init - Initialize a dynevent_arg object
  360. * @arg: A pointer to the dynevent_arg struct representing the arg
  361. * @separator: An (optional) separator, appended after adding the arg
  362. *
  363. * Initialize a dynevent_arg object. A dynevent_arg represents an
  364. * object used to append single arguments to the current command
  365. * string. After the arg string is successfully appended to the
  366. * command string, the optional @separator is appended. If no
  367. * separator was specified when initializing the arg, a space will be
  368. * appended.
  369. */
  370. void dynevent_arg_init(struct dynevent_arg *arg,
  371. char separator)
  372. {
  373. memset(arg, '\0', sizeof(*arg));
  374. if (!separator)
  375. separator = ' ';
  376. arg->separator = separator;
  377. }
  378. /**
  379. * dynevent_arg_pair_init - Initialize a dynevent_arg_pair object
  380. * @arg_pair: A pointer to the dynevent_arg_pair struct representing the arg
  381. * @operator: An (optional) operator, appended after adding the first arg
  382. * @separator: An (optional) separator, appended after adding the second arg
  383. *
  384. * Initialize a dynevent_arg_pair object. A dynevent_arg_pair
  385. * represents an object used to append argument pairs such as 'type
  386. * variable_name;' or 'x+y' to the current command string. An
  387. * argument pair consists of a left-hand-side argument and a
  388. * right-hand-side argument separated by an operator, which can be
  389. * whitespace, all followed by a separator, if applicable. After the
  390. * first arg string is successfully appended to the command string,
  391. * the optional @operator is appended, followed by the second arg and
  392. * optional @separator. If no separator was specified when
  393. * initializing the arg, a space will be appended.
  394. */
  395. void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
  396. char operator, char separator)
  397. {
  398. memset(arg_pair, '\0', sizeof(*arg_pair));
  399. if (!operator)
  400. operator = ' ';
  401. arg_pair->operator = operator;
  402. if (!separator)
  403. separator = ' ';
  404. arg_pair->separator = separator;
  405. }
  406. /**
  407. * dynevent_create - Create the dynamic event contained in dynevent_cmd
  408. * @cmd: The dynevent_cmd object containing the dynamic event creation command
  409. *
  410. * Once a dynevent_cmd object has been successfully built up via the
  411. * dynevent_cmd_init(), dynevent_arg_add() and dynevent_arg_pair_add()
  412. * functions, this function runs the final command to actually create
  413. * the event.
  414. *
  415. * Return: 0 if the event was successfully created, error otherwise.
  416. */
  417. int dynevent_create(struct dynevent_cmd *cmd)
  418. {
  419. return cmd->run_command(cmd);
  420. }
  421. EXPORT_SYMBOL_GPL(dynevent_create);