tracing: More synthetic event error fixes
PATCH v6 1/6 tracing/dynevent: Delegate parsing to create function
In reply to Tom ZanussiFrom: Masami Hiramatsu <[email protected]>
Delegate command parsing to each create function so that the command syntax can be customized.
This requires changes to the kprobe/uprobe/synthetic event handling, which are also included here.
--- kernel/trace/trace.c | 23 ++---------- kernel/trace/trace.h | 3 +- kernel/trace/trace_dynevent.c | 35 +++++++++++------- kernel/trace/trace_dynevent.h | 4 +-- kernel/trace/trace_events_synth.c | 60 +++++++++++++++++++++++-------- kernel/trace/trace_kprobe.c | 33 +++++++++-------- kernel/trace/trace_probe.c | 17 +++++++++ kernel/trace/trace_probe.h | 1 + kernel/trace/trace_uprobe.c | 17 +++++---- 9 files changed, 120 insertions(+), 73 deletions(-)
kernel/trace/trace.c +2 -21
@@ -9426,30 +9426,11 @@ void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) }EXPORT_SYMBOL_GPL (ftrace_dump); -int trace_run_command (const char *buf,int (*createfn)(int ,char **)) -{ -char **argv; -int argc, ret; - - argc =0 ; - ret =0 ; - argv =argv_split (GFP_KERNEL , buf, &argc); -if (!argv) -return -ENOMEM ; - -if (argc) - ret =createfn (argc, argv); - -argv_free (argv); - -return ret; -} -#define WRITE_BUFSIZE 4096ssize_t trace_parse_run_command (struct file *file,const char __user *buffer,size_t count,loff_t *ppos, -int (*createfn)(int ,char **)) +int (*createfn)(const char *)) {char *kbuf, *buf, *tmp;int ret =0 ;
@@ -9497,7 +9478,7 @@ ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,if (tmp) *tmp ='\0' ; - ret =trace_run_command (buf, createfn); + ret =createfn (buf);if (ret) goto out; buf += size;
kernel/trace/trace.h +1 -2
@@ -1830,10 +1830,9 @@ extern int tracing_set_cpumask(struct trace_array *tr,#define MAX_EVENT_NAME_LEN 64 -extern int trace_run_command (const char *buf,int (*createfn)(int ,char **));extern ssize_t trace_parse_run_command (struct file *file,const char __user *buffer,size_t count,loff_t *ppos, -int (*createfn)(int ,char **)); +int (*createfn)(const char *));extern unsigned int err_pos (char *cmd,const char *str); extern void tracing_log_err(struct trace_array *tr,
kernel/trace/trace_dynevent.c +22 -13
@@ -31,23 +31,31 @@ int dyn_event_register(struct dyn_event_operations *ops)return 0 ; } -int dyn_event_release (int argc,char **argv,struct dyn_event_operations *type) +int dyn_event_release (const char *raw_command,struct dyn_event_operations *type) {struct dyn_event *pos, *n;char *system =NULL , *event, *p; -int ret = -ENOENT ; +int argc, ret = -ENOENT ; +char **argv; + + argv =argv_split (GFP_KERNEL , raw_command, &argc); +if (!argv) +return -ENOMEM ;if (argv[0 ][0 ] =='-' ) { -if (argv[0 ][1 ] !=':' ) -return -EINVAL ; +if (argv[0 ][1 ] !=':' ) { + ret = -EINVAL ; + goto out; + } event = &argv[0 ][2 ]; }else { event =strchr (argv[0 ],':' ); -if (!event) -return -EINVAL ; +if (!event) { + ret = -EINVAL ; + goto out; + } event++; } - argc--; argv++; p =strchr (event,'/' );if (p) {
@@ -63,7 +71,7 @@ int dyn_event_release(int argc, char **argv, struct dyn_event_operations *type)if (type && type != pos->ops)continue ;if (!pos->ops->match (system, event, - argc, (const char **)argv, pos)) + argc -1 , (const char **)argv +1 , pos))continue ; ret = pos->ops->free (pos);
@@ -71,21 +79,22 @@ int dyn_event_release(int argc, char **argv, struct dyn_event_operations *type)break ; }mutex_unlock (&event_mutex); - +out: +argv_free (argv);return ret; } -static int create_dyn_event (int argc,char **argv) +static int create_dyn_event (const char *raw_command) {struct dyn_event_operations *ops;int ret = -ENODEV ; -if (argv[0 ][0 ] =='-' || argv[0 ][0 ] =='!' ) -return dyn_event_release (argc, argv,NULL ); +if (raw_command[0 ] =='-' || raw_command[0 ] =='!' ) +return dyn_event_release (raw_command,NULL );mutex_lock (&dyn_event_ops_mutex);list_for_each_entry (ops, &dyn_event_ops_list, list) { - ret = ops->create (argc, (const char **)argv); + ret = ops->create (raw_command);if (!ret || ret != -ECANCELED )break ; }
kernel/trace/trace_dynevent.h +2 -2
@@ -39,7 +39,7 @@ struct dyn_event;*/ struct dyn_event_operations {struct list_head list; -int (*create)(int argc,const char *argv[]); +int (*create)(const char *raw_command);int (*show)(struct seq_file *m,struct dyn_event *ev);bool (*is_busy)(struct dyn_event *ev);int (*free)(struct dyn_event *ev);
@@ -97,7 +97,7 @@ void *dyn_event_seq_start(struct seq_file *m, loff_t *pos);void *dyn_event_seq_next (struct seq_file *m,void *v,loff_t *pos);void dyn_event_seq_stop (struct seq_file *m,void *v);int dyn_events_release_all (struct dyn_event_operations *type); -int dyn_event_release (int argc,char **argv,struct dyn_event_operations *type); +int dyn_event_release (const char *raw_command,struct dyn_event_operations *type);/* * for_each_dyn_event - iterate over the dyn_event list
kernel/trace/trace_events_synth.c +46 -14
101 lines not on this page · raw message
kernel/trace/trace_kprobe.c +18 -15
@@ -35,7 +35,7 @@ static int __init set_kprobe_boot_events(char *str) }__setup ("kprobe_event=" , set_kprobe_boot_events); -static int trace_kprobe_create (int argc,const char **argv); +static int trace_kprobe_create (const char *raw_command);static int trace_kprobe_show (struct seq_file *m,struct dyn_event *ev);static int trace_kprobe_release (struct dyn_event *ev);static bool trace_kprobe_is_busy (struct dyn_event *ev);
@@ -711,7 +711,7 @@ static inline void sanitize_event_name(char *name) *name ='_' ; } -static int trace_kprobe_create (int argc,const char *argv[]) +static int __trace_kprobe_create (int argc,const char *argv[]) {/* * Argument syntax:
@@ -908,20 +908,25 @@ static int trace_kprobe_create(int argc, const char *argv[]) goto out; } -static int create_or_delete_trace_kprobe (int argc,char **argv) +static int trace_kprobe_create (const char *raw_command) +{ +return trace_probe_create (raw_command, __trace_kprobe_create); +} + +static int create_or_delete_trace_kprobe (const char *raw_command) {int ret; -if (argv[0 ][0 ] =='-' ) -return dyn_event_release (argc, argv, &trace_kprobe_ops); +if (raw_command[0 ] =='-' ) +return dyn_event_release (raw_command, &trace_kprobe_ops); - ret =trace_kprobe_create (argc, (const char **)argv); + ret =trace_kprobe_create (raw_command);return ret == -ECANCELED ? -EINVAL : ret; }static int trace_kprobe_run_command (struct dynevent_cmd *cmd) { -return trace_run_command (cmd->seq.buffer, create_or_delete_trace_kprobe); +return create_or_delete_trace_kprobe (cmd->seq.buffer); }/**
@@ -1082,7 +1087,7 @@ int kprobe_event_delete(const char *name)snprintf (buf,MAX_EVENT_NAME_LEN ,"-:%s" , name); -return trace_run_command (buf, create_or_delete_trace_kprobe); +return create_or_delete_trace_kprobe (buf); }EXPORT_SYMBOL_GPL (kprobe_event_delete);
@@ -1885,7 +1890,7 @@ static __init void setup_boot_kprobe_events(void)if (p) *p++ ='\0' ; - ret =trace_run_command (cmd, create_or_delete_trace_kprobe); + ret =create_or_delete_trace_kprobe (cmd);if (ret)pr_warn ("Failed to add event(%d): %s\n" , ret, cmd);
@@ -1979,8 +1984,7 @@ static __init int kprobe_trace_self_tests_init(void)pr_info ("Testing kprobe tracing: " ); - ret =trace_run_command ("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)" , - create_or_delete_trace_kprobe); + ret =create_or_delete_trace_kprobe ("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)" );if (WARN_ON_ONCE (ret)) {pr_warn ("error on probing function entry.\n" ); warn++;
@@ -2001,8 +2005,7 @@ static __init int kprobe_trace_self_tests_init(void) } } - ret =trace_run_command ("r:testprobe2 kprobe_trace_selftest_target $retval" , - create_or_delete_trace_kprobe); + ret =create_or_delete_trace_kprobe ("r:testprobe2 kprobe_trace_selftest_target $retval" );if (WARN_ON_ONCE (ret)) {pr_warn ("error on probing function return.\n" ); warn++;
@@ -2075,13 +2078,13 @@ static __init int kprobe_trace_self_tests_init(void) trace_probe_event_call(&tk->tp), file); } - ret =trace_run_command ("-:testprobe" , create_or_delete_trace_kprobe); + ret =create_or_delete_trace_kprobe ("-:testprobe" );if (WARN_ON_ONCE (ret)) {pr_warn ("error on deleting a probe.\n" ); warn++; } - ret =trace_run_command ("-:testprobe2" , create_or_delete_trace_kprobe); + ret =create_or_delete_trace_kprobe ("-:testprobe2" );if (WARN_ON_ONCE (ret)) {pr_warn ("error on deleting a probe.\n" ); warn++;
kernel/trace/trace_probe.c +17 -0
@@ -1134,3 +1134,20 @@ bool trace_probe_match_command_args(struct trace_probe *tp, }return true; } + +int trace_probe_create (const char *raw_command,int (*createfn)(int ,const char **)) +{ +int argc =0 , ret =0 ; +char **argv; + + argv =argv_split (GFP_KERNEL , raw_command, &argc); +if (!argv) +return -ENOMEM ; + +if (argc) + ret =createfn (argc, (const char **)argv); + +argv_free (argv); + +return ret; +}
kernel/trace/trace_probe.h +1 -0
@@ -341,6 +341,7 @@ struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,int trace_probe_compare_arg_type (struct trace_probe *a,struct trace_probe *b);bool trace_probe_match_command_args (struct trace_probe *tp,int argc,const char **argv); +int trace_probe_create (const char *raw_command,int (*createfn)(int ,const char **));#define trace_probe_for_each_link (pos, tp) \ list_for_each_entry(pos, &(tp)->event->files, list)
kernel/trace/trace_uprobe.c +11 -6
@@ -34,7 +34,7 @@ struct uprobe_trace_entry_head {#define DATAOF_TRACE_ENTRY (entry, is_return) \ ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return)) -static int trace_uprobe_create (int argc,const char **argv); +static int trace_uprobe_create (const char *raw_command);static int trace_uprobe_show (struct seq_file *m,struct dyn_event *ev);static int trace_uprobe_release (struct dyn_event *ev);static bool trace_uprobe_is_busy (struct dyn_event *ev);
@@ -530,7 +530,7 @@ static int register_trace_uprobe(struct trace_uprobe *tu)* Argument syntax: * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET[%return][(REF)] [FETCHARGS] */ -static int trace_uprobe_create (int argc,const char **argv) +static int __trace_uprobe_create (int argc,const char **argv) {struct trace_uprobe *tu;const char *event =NULL , *group =UPROBE_EVENT_SYSTEM ;
@@ -716,14 +716,19 @@ static int trace_uprobe_create(int argc, const char **argv)return ret; } -static int create_or_delete_trace_uprobe (int argc,char **argv) +int trace_uprobe_create (const char *raw_command) +{ +return trace_probe_create (raw_command, __trace_uprobe_create); +} + +static int create_or_delete_trace_uprobe (const char *raw_command) {int ret; -if (argv[0 ][0 ] =='-' ) -return dyn_event_release (argc, argv, &trace_uprobe_ops); +if (raw_command[0 ] =='-' ) +return dyn_event_release (raw_command, &trace_uprobe_ops); - ret =trace_uprobe_create (argc, (const char **)argv); + ret =trace_uprobe_create (raw_command);return ret == -ECANCELED ? -EINVAL : ret; }
Signature · 1 line
2.17.1
PATCH v6 5/6 selftests/ftrace: Update synthetic event syntax errors
In reply to Tom ZanussiSome of the synthetic event errors and positions have changed in the code - update those and add several more tests.
Also add a runtime check to ensure that the kernel supports dynamic strings in synthetic events, which these tests require.
event syntax errors)
--- .../trigger-synthetic_event_syntax_errors.tc | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-)
tools/testing/selftests/ftrace/test.d/trigger/inter-event/trigger-synthetic_event_syntax_errors.tc +27 -8
@@ -1,19 +1,38 @@ #!/bin/sh # SPDX-License-Identifier: GPL-2.0 # description: event trigger - test synthetic_events syntax parser errors -# requires: synthetic_events error_log +# requires: synthetic_events error_log "char name[]' >> synthetic_events":README check_error() { # command-with-error-pos-by-^ ftrace_errlog_check 'synthetic_events' "$1" 'synthetic_events' } +check_dyn_error() { # command-with-error-pos-by-^ + ftrace_errlog_check 'synthetic_events' "$1" 'dynamic_events' +} + check_error 'myevent ^chr arg' # INVALID_TYPE -check_error 'myevent ^char str[];; int v' # INVALID_TYPE -check_error 'myevent char ^str]; int v' # INVALID_NAME -check_error 'myevent char ^str;[]' # INVALID_NAME -check_error 'myevent ^char str[; int v' # INVALID_TYPE -check_error '^mye;vent char str[]' # BAD_NAME -check_error 'myevent char str[]; ^int' # INVALID_FIELD -check_error '^myevent' # INCOMPLETE_CMD +check_error 'myevent ^unsigned arg' # INCOMPLETE_TYPE + +check_error 'myevent char ^str]; int v' # BAD_NAME +check_error '^mye-vent char str[]' # BAD_NAME +check_error 'myevent char ^st-r[]' # BAD_NAME + +check_error 'myevent char str;^[]' # INVALID_FIELD +check_error 'myevent char str; ^int' # INVALID_FIELD + +check_error 'myevent char ^str[; int v' # INVALID_ARRAY_SPEC +check_error 'myevent char ^str[kdjdk]' # INVALID_ARRAY_SPEC +check_error 'myevent char ^str[257]' # INVALID_ARRAY_SPEC + +check_error '^mye;vent char str[]' # INVALID_CMD +check_error '^myevent ; char str[]' # INVALID_CMD +check_error '^myevent; char str[]' # INVALID_CMD +check_error '^myevent ;char str[]' # INVALID_CMD +check_error '^; char str[]' # INVALID_CMD +check_error '^;myevent char str[]' # INVALID_CMD +check_error '^myevent' # INVALID_CMD + +check_dyn_error '^s:junk/myevent char str[' # INVALID_DYN_CMD exit 0
Signature · 1 line
2.17.1
PATCH v6 6/6 selftests/ftrace: Add '!event' synthetic event syntax check
In reply to Tom ZanussiAdd a check confirming that '!event' alone will remove a synthetic event.
--- .../trigger/inter-event/trigger-synthetic-event-syntax.tc | 4 ++++ 1 file changed, 4 insertions(+)
tools/testing/selftests/ftrace/test.d/trigger/inter-event/trigger-synthetic-event-syntax.tc +4 -0
@@ -32,6 +32,10 @@ grep "myevent[[:space:]]u64 var1" synthetic_events # it is not possible to add same name event ! echo "myevent u64 var2" >> synthetic_events +# make sure !synthetic event doesn't require a field +echo "!myevent" >> synthetic_events +echo "myevent u64 var1" >> synthetic_events + # Non-append open will cleanup all events and add new one echo "myevent u64 var2" > synthetic_events
Signature · 1 line
2.17.1
PATCH v6 4/6 tracing: Add a backward-compatibility check for synthetic event creation
In reply to Tom ZanussiThe synthetic event parsing rework now requires semicolons between synthetic event fields. That requirement breaks existing users who might already have used the old synthetic event command format, so this adds an inner loop that can parse more than one field, if present, between semicolons. For each field, parse_synth_field() checks in which version that field was introduced, using check_field_version(). The caller, __create_synth_event() can then use that version information to determine whether or not to enforce the requirement on the command as a whole.
In the future, if/when new features are added, the requirement will be that any field/string containing the new feature must use semicolons, and the check_field_version() check can then check for those and enforce it. Using a version number allows this scheme to be extended if necessary.
--- kernel/trace/trace_events_synth.c | 68 ++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 19 deletions(-)
kernel/trace/trace_events_synth.c +49 -19
@@ -580,11 +580,18 @@ static void free_synth_field(struct synth_field *field)kfree (field); } -static struct synth_field *parse_synth_field (int argc,char **argv) +static int check_field_version (const char *prefix,const char *field_type, +const char *field_name) +{ +return 1 ; +} + +static struct synth_field *parse_synth_field (int argc,char **argv, +int *consumed,int *field_version) {const char *prefix =NULL , *field_type = argv[0 ], *field_name, *array; -int len, consumed, ret = -ENOMEM ;struct synth_field *field; +int len, ret = -ENOMEM ;struct seq_buf s;ssize_t size;
@@ -596,15 +603,10 @@ static struct synth_field *parse_synth_field(int argc, char **argv) prefix ="unsigned " ; field_type = argv[1 ]; field_name = argv[2 ]; - consumed =3 ; + *consumed +=3 ; }else { field_name = argv[1 ]; - consumed =2 ; - } - -if (consumed < argc) { -synth_err (SYNTH_ERR_INVALID_FIELD ,errpos (field_type)); -return ERR_PTR (-EINVAL ); + *consumed +=2 ; }if (!field_name) {
@@ -612,6 +614,8 @@ static struct synth_field *parse_synth_field(int argc, char **argv)return ERR_PTR (-EINVAL ); } + *field_version =check_field_version (prefix, field_type, field_name); + field =kzalloc (sizeof (*field),GFP_KERNEL );if (!field)return ERR_PTR (-ENOMEM );
@@ -1167,6 +1171,7 @@ static int __create_synth_event(const char *name, const char *raw_fields) {char **argv, *field_str, *tmp_fields, *saved_fields =NULL ;struct synth_field *field, *fields[SYNTH_FIELDS_MAX ]; +int consumed, cmd_version =1 , n_fields_this_loop;int i, argc, n_fields =0 , ret =0 ;struct synth_event *event =NULL ;
@@ -1214,21 +1219,46 @@ static int __create_synth_event(const char *name, const char *raw_fields)if (!argc)continue ; - field =parse_synth_field (argc, argv); -if (IS_ERR (field)) { -argv_free (argv); - ret =PTR_ERR (field); - goto err; - } + n_fields_this_loop =0 ; + consumed =0 ; +while (argc > consumed) { +int field_version; + + field =parse_synth_field (argc - consumed, + argv + consumed, &consumed, + &field_version); +if (IS_ERR (field)) { +argv_free (argv); + ret =PTR_ERR (field); + goto err; + } -argv_free (argv); +if (field_version > cmd_version) + cmd_version = field_version; + +if (cmd_version >1 && n_fields_this_loop >=1 ) { +synth_err (SYNTH_ERR_INVALID_CMD ,errpos (field_str)); + ret = -EINVAL ; + goto err; + } + + fields[n_fields++] = field; +if (n_fields ==SYNTH_FIELDS_MAX ) { +synth_err (SYNTH_ERR_TOO_MANY_FIELDS ,0 ); + ret = -EINVAL ; + goto err; + } + + n_fields_this_loop++; + } - fields[n_fields++] = field; -if (n_fields ==SYNTH_FIELDS_MAX ) { -synth_err (SYNTH_ERR_TOO_MANY_FIELDS ,0 ); +if (consumed < argc) { +synth_err (SYNTH_ERR_INVALID_CMD ,0 ); ret = -EINVAL ; goto err; } + +argv_free (argv); }if (n_fields ==0 ) {
Signature · 1 line
2.17.1
PATCH v6 2/6 tracing: Rework synthetic event command parsing
In reply to Tom ZanussiNow that command parsing has been delegated to the create functions and we're no longer constrained by argv_split(), we can modify the synthetic event command parser to better match the higher-level structure of the synthetic event commands, which is basically an event name followed by a set of semicolon-separated fields.
Since we're also now passed the raw command, we can also save it directly and can get rid of save_cmdstr().
--- kernel/trace/trace_events_synth.c | 198 +++++++++++++++--------------- 1 file changed, 98 insertions(+), 100 deletions(-)
kernel/trace/trace_events_synth.c +98 -100
@@ -48,7 +48,7 @@ static int errpos(const char *str)return err_pos (last_cmd, str); } -static void last_cmd_set (char *str) +static void last_cmd_set (const char *str) {if (!str)return ;
@@ -579,18 +579,14 @@ static void free_synth_field(struct synth_field *field)kfree (field); } -static struct synth_field *parse_synth_field (int argc,const char **argv, -int *consumed) +static struct synth_field *parse_synth_field (int argc,char **argv) { -struct synth_field *field;const char *prefix =NULL , *field_type = argv[0 ], *field_name, *array; -int len, ret = -ENOMEM ; +int len, consumed, ret = -ENOMEM ; +struct synth_field *field;struct seq_buf s;ssize_t size; -if (field_type[0 ] ==';' ) - field_type++; -if (!strcmp (field_type,"unsigned" )) {if (argc <3 ) {synth_err (SYNTH_ERR_INCOMPLETE_TYPE ,errpos (field_type));
@@ -599,10 +595,20 @@ static struct synth_field *parse_synth_field(int argc, const char **argv, prefix ="unsigned " ; field_type = argv[1 ]; field_name = argv[2 ]; - *consumed =3 ; + consumed =3 ; }else { field_name = argv[1 ]; - *consumed =2 ; + consumed =2 ; + } + +if (consumed < argc) { +synth_err (SYNTH_ERR_INVALID_FIELD ,errpos (field_type)); +return ERR_PTR (-EINVAL ); + } + +if (!field_name) { +synth_err (SYNTH_ERR_INVALID_FIELD ,errpos (field_type)); +return ERR_PTR (-EINVAL ); } field =kzalloc (sizeof (*field),GFP_KERNEL );
@@ -613,8 +619,6 @@ static struct synth_field *parse_synth_field(int argc, const char **argv, array =strchr (field_name,'[' );if (array) len -=strlen (array); -else if (field_name[len -1 ] ==';' ) - len--; field->name =kmemdup_nul (field_name, len,GFP_KERNEL );if (!field->name)
@@ -626,8 +630,6 @@ static struct synth_field *parse_synth_field(int argc, const char **argv, goto free; } -if (field_type[0 ] ==';' ) - field_type++; len =strlen (field_type) +1 ;if (array)
@@ -644,11 +646,8 @@ static struct synth_field *parse_synth_field(int argc, const char **argv,if (prefix)seq_buf_puts (&s, prefix);seq_buf_puts (&s, field_type); -if (array) { +if (array)seq_buf_puts (&s, array); -if (s.buffer[s.len -1 ] ==';' ) - s.len--; - }if (WARN_ON_ONCE (!seq_buf_buffer_left (&s))) goto free;
@@ -1160,46 +1159,12 @@ int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name, }EXPORT_SYMBOL_GPL (synth_event_gen_cmd_array_start); -static int save_cmdstr (int argc,const char *name,const char **argv) -{ -struct seq_buf s; -char *buf; -int i; - - buf =kzalloc (MAX_DYNEVENT_CMD_LEN ,GFP_KERNEL ); -if (!buf) -return -ENOMEM ; - -seq_buf_init (&s, buf,MAX_DYNEVENT_CMD_LEN ); - -seq_buf_puts (&s, name); - -for (i =0 ; i < argc; i++) { -seq_buf_putc (&s,' ' ); -seq_buf_puts (&s, argv[i]); - } - -if (!seq_buf_buffer_left (&s)) { -synth_err (SYNTH_ERR_CMD_TOO_LONG ,0 ); -kfree (buf); -return -EINVAL ; - } - buf[s.len] =0 ; -last_cmd_set (buf); - -kfree (buf); -return 0 ; -} - -static int __create_synth_event (int argc,const char *name,const char **argv) +static int __create_synth_event (const char *name,const char *raw_fields) { +char **argv, *field_str, *tmp_fields, *saved_fields =NULL ;struct synth_field *field, *fields[SYNTH_FIELDS_MAX ]; +int i, argc, n_fields =0 , ret =0 ;struct synth_event *event =NULL ; -int i, consumed =0 , n_fields =0 , ret =0 ; - - ret =save_cmdstr (argc, name, argv); -if (ret) -return ret;/* * Argument syntax:
@@ -1208,13 +1173,14 @@ static int __create_synth_event(int argc, const char *name, const char **argv)* where 'field' = type field_name */ -if (name[0 ] =='\0' || argc <1 ) { +mutex_lock (&event_mutex); + +if (name[0 ] =='\0' ) {synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); -return -EINVAL ; + ret = -EINVAL ; + goto out; } -mutex_lock (&event_mutex); -if (!is_good_name (name)) {synth_err (SYNTH_ERR_BAD_NAME ,errpos (name)); ret = -EINVAL ;
@@ -1228,26 +1194,41 @@ static int __create_synth_event(int argc, const char *name, const char **argv) goto out; } -for (i =0 ; i < argc -1 ; i++) { -if (strcmp (argv[i],";" ) ==0 ) -continue ; -if (n_fields ==SYNTH_FIELDS_MAX ) { -synth_err (SYNTH_ERR_TOO_MANY_FIELDS ,0 ); - ret = -EINVAL ; + tmp_fields = saved_fields =kstrdup (raw_fields,GFP_KERNEL ); +if (!tmp_fields) { + ret = -ENOMEM ; + goto out; + } + +while ((field_str =strsep (&tmp_fields,";" )) !=NULL ) { + argv =argv_split (GFP_KERNEL , field_str, &argc); +if (!argv) { + ret = -ENOMEM ; goto err; } - field =parse_synth_field (argc - i, &argv[i], &consumed); +if (!argc) +continue ; + + field =parse_synth_field (argc, argv);if (IS_ERR (field)) { +argv_free (argv); ret =PTR_ERR (field); goto err; } + +argv_free (argv); + fields[n_fields++] = field; - i += consumed -1 ; +if (n_fields ==SYNTH_FIELDS_MAX ) { +synth_err (SYNTH_ERR_TOO_MANY_FIELDS ,0 ); + ret = -EINVAL ; + goto err; + } } -if (i < argc &&strcmp (argv[i],";" ) !=0 ) { -synth_err (SYNTH_ERR_INVALID_FIELD ,errpos (argv[i])); +if (n_fields ==0 ) { +synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); ret = -EINVAL ; goto err; }
@@ -1266,6 +1247,8 @@ static int __create_synth_event(int argc, const char *name, const char **argv) out:mutex_unlock (&event_mutex); +kfree (saved_fields); +return ret; err:for (i =0 ; i < n_fields; i++)
@@ -1385,29 +1368,38 @@ EXPORT_SYMBOL_GPL(synth_event_delete);static int create_or_delete_synth_event (const char *raw_command) { -char **argv, *name =NULL ; -int argc =0 , ret =0 ; +char *name =NULL , *fields, *p; +int ret =0 ; - argv =argv_split (GFP_KERNEL , raw_command, &argc); -if (!argv) -return -ENOMEM ; + raw_command =skip_spaces (raw_command); +if (raw_command[0 ] =='\0' ) +return ret; + +last_cmd_set (raw_command); -if (!argc) + p =strpbrk (raw_command," \t" ); +if (!p && raw_command[0 ] !='!' ) { +synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); + ret = -EINVAL ; goto free; + } - name = argv[0 ]; + name =kmemdup_nul (raw_command, p ? p - raw_command :strlen (raw_command),GFP_KERNEL ); +if (!name) +return -ENOMEM ; -/* trace_run_command() ensures argc != 0 */ if (name[0 ] =='!' ) { ret =synth_event_delete (name +1 ); goto free; } - ret =__create_synth_event (argc -1 , name, (const char **)argv +1 ); + fields =skip_spaces (p); + + ret =__create_synth_event (name, fields); free: -argv_free (argv); +kfree (name); -return ret == -ECANCELED ? -EINVAL : ret; +return ret; }static int synth_event_run_command (struct dynevent_cmd *cmd)
@@ -1953,39 +1945,45 @@ EXPORT_SYMBOL_GPL(synth_event_trace_end);static int create_synth_event (const char *raw_command) { -char **argv, *name; -int len, argc =0 , ret =0 ; +char *fields, *p; +const char *name; +int len, ret =0 ; - argv =argv_split (GFP_KERNEL , raw_command, &argc); -if (!argv) { - ret = -ENOMEM ; + raw_command =skip_spaces (raw_command); +if (raw_command[0 ] =='\0' )return ret; - } -if (!argc) - goto free; +last_cmd_set (raw_command); - name = argv[0 ]; + p =strpbrk (raw_command," \t" ); +if (!p) +return -EINVAL ; -if (name[0 ] !='s' || name[1 ] !=':' ) { - ret = -ECANCELED ; - goto free; - } + fields =skip_spaces (p); + + name = raw_command; + +if (name[0 ] !='s' || name[1 ] !=':' ) +return -ECANCELED ; name +=2 ;/* This interface accepts group name prefix */ if (strchr (name,'/' )) { len =str_has_prefix (name,SYNTH_SYSTEM "/" ); -if (len ==0 ) { - ret = -EINVAL ; - goto free; - } +if (len ==0 ) +return -EINVAL ; name += len; } - ret =__create_synth_event (argc -1 , name, (const char **)argv +1 ); -free: -argv_free (argv); + len = name - raw_command; + + name =kmemdup_nul (raw_command + len, p - raw_command - len,GFP_KERNEL ); +if (!name) +return -ENOMEM ; + + ret =__create_synth_event (name, fields); + +kfree (name);return ret; }
Signature · 1 line
2.17.1
v6 cover tracing: More synthetic event error fixes
Hi,
This is v6 of the synthetic event error fix patchset. This version removes the semicolon-adding pass and instead adds an inner loop as suggested by Masami. A different mechanism adding per-version field checks was also added in place of the previous whole-string audit.
Also, moved the parse_synth_field() error message back into patch 2/6 (tracing: Rework synthetic event command parsing) and fixed the problem with the !name problem as also noted by Masami, and added a new patch (selftests/ftrace: Add '!event' synthetic event syntax check) to check for that and prevent future changes from breaking it again.
Tom
v5 text:
Hi,
This is v5 of the sythetic event error fix patchset. This version is the same as v4 but with a few variable-initialization fixes flagged by Dan Carpenter and the kernel test robot.
Tom
v4 text:
Hi,
This is v4 of the sythetic event error fix patchset. As suggested by Steve, I added a new pass that adds semicolons to 'old' commands that may be missing them, in order to maintain backward compatibility. All commands are handled by the new and improved parsing code, but commands missing the semicolons have them added before processing and are therefore still valid. At some point in the future, as new features are added and we can require any command containing them to require semicolons, this pass can be completely skipped by detecting those features in the currently empty audit_old_buffer() hook.
Also, as a result, the patch adding semicolons to the selftests is no longer necessary (selftests/ftrace: Add synthetic event field separators) and has been dropped in this series.
Tom
v3 text:
Hi,
This is v3 of the sythetic event error fix patchset. As suggested by Masami, I split the 'tracing/dynevent: Delegate parsing to create function' into two - one containing just the interface changes and the second with the synthetic event parsing changes the first enabled.
I also replaced a couple argv_split() with strpbrk() as suggested by Masami, along with removing the no-longer-used consume lines and another line that tested ECANCELED that was no longer needed.
Also, removed a test case that was no longer needed since the commands are now stripped of whitespace first.
Thanks, Masami, for the suggestions.
Tom
v2 text:
This is v2 of the previous sythetic event error fix patchset.
This version drops the original ([PATCH 1/4] tracing: Make trace_*_run_command() more flexible) and (tracing: Use new trace_run_command() options) patches and replaces them with Masami's patch (tracing/dynevent: Delegate parsing to create function) [1]. The new version adds in all the synthetic event changes needed to compile and use the new interface.
A new patch was also added (selftests/ftrace: Add synthetic event field separators) that fixes more invalid synthetic event syntax I found while testing.
I also added some more new checks to the synthetic event sytax error testcase.
As before, I didn't see any problems running the entire ftrace testsuite or the test modules that also use the things that were touched here.
[1] https://lore.kernel.org/lkml/[email protected]/
Thanks,
Tom
v1 text:
Hi,
This patchset addresses the synthetic event error anomalies reported by Masami in the last patchset [1].
It turns out that most of the problems boil down to clunky separator parsing; adding a couple new abilities to trace_run_command() and then adapting the existing users seemed to me the best way to fix these things, and also gets rid of some code.
Also, to make things easier for error display, I changed these to preserve the original command string and pass it through the callback instead of rebuilding it for error display.
I added some new error strings and removed unused ones as well, and added a bunch of new test cases to the synthetic parser error test case.
I didn't see any problems running the entire ftrace testsuite or the test modules that also use the things that were touched here.
Thanks,
Tom
[1] https://lore.kernel.org/lkml/[email protected]/
The following changes since commit f6a694665f132cbf6e2222dd2f173dc35330a8aa:
tracing: Offload eval map updates to a work queue (2020-12-15 09:29:14 -0500)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/zanussi/linux-trace.git ftrace/synth-fixes-v6
Masami Hiramatsu (1):
tracing/dynevent: Delegate parsing to create function
Tom Zanussi (5):
tracing: Rework synthetic event command parsing
tracing: Update synth command errors
tracing: Add a backward-compatibility check for synthetic event
creation
selftests/ftrace: Update synthetic event syntax errors selftests/ftrace: Add '!event' synthetic event syntax check
kernel/trace/trace.c | 23 +- kernel/trace/trace.h | 3 +- kernel/trace/trace_dynevent.c | 35 ++- kernel/trace/trace_dynevent.h | 4 +- kernel/trace/trace_events_synth.c | 294 ++++++++++++------ kernel/trace/trace_kprobe.c | 33 +- kernel/trace/trace_probe.c | 17 + kernel/trace/trace_probe.h | 1 + kernel/trace/trace_uprobe.c | 17 +- .../trigger-synthetic-event-syntax.tc | 4 + .../trigger-synthetic_event_syntax_errors.tc | 35 ++- 11 files changed, 309 insertions(+), 157 deletions(-)
Signature · 1 line
2.17.1
PATCH v6 3/6 tracing: Update synth command errors
Since array types are handled differently, errors referencing them also need to be handled differently. Add and use a new INVALID_ARRAY_SPEC error. Also add INVALID_CMD and INVALID_DYN_CMD to catch and display the correct form for badly-formed commands, which can also be used in place of CMD_INCOMPLETE, which is removed, and remove CMD_TOO_LONG, since it's no longer used.
--- kernel/trace/trace_events_synth.c | 72 +++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 9 deletions(-)
kernel/trace/trace_events_synth.c +63 -9
@@ -23,13 +23,14 @@#undef ERRORS#define ERRORS \ C(BAD_NAME, "Illegal name"), \ - C(CMD_INCOMPLETE, "Incomplete command"), \ + C(INVALID_CMD, "Command must be of the form: <name> field[;field] ..."),\ + C(INVALID_DYN_CMD, "Command must be of the form: s or -:[synthetic/]<name> field[;field] ..."),\ C(EVENT_EXISTS, "Event already exists"), \ C(TOO_MANY_FIELDS, "Too many fields"), \ C(INCOMPLETE_TYPE, "Incomplete type"), \ C(INVALID_TYPE, "Invalid type"), \ - C(INVALID_FIELD, "Invalid field"), \ - C(CMD_TOO_LONG, "Command too long"), + C(INVALID_FIELD, "Invalid field"), \ + C(INVALID_ARRAY_SPEC, "Invalid array specification"),#undef C#define C (a, b) SYNTH_ERR_##a
@@ -655,7 +656,10 @@ static struct synth_field *parse_synth_field(int argc, char **argv) size =synth_field_size (field->type);if (size <0 ) { -synth_err (SYNTH_ERR_INVALID_TYPE ,errpos (field_type)); +if (array) +synth_err (SYNTH_ERR_INVALID_ARRAY_SPEC ,errpos (field_name)); +else +synth_err (SYNTH_ERR_INVALID_TYPE ,errpos (field_type)); ret = -EINVAL ; goto free; }else if (size ==0 ) {
@@ -1176,7 +1180,7 @@ static int __create_synth_event(const char *name, const char *raw_fields)mutex_lock (&event_mutex);if (name[0 ] =='\0' ) { -synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); +synth_err (SYNTH_ERR_INVALID_CMD ,0 ); ret = -EINVAL ; goto out; }
@@ -1228,7 +1232,7 @@ static int __create_synth_event(const char *name, const char *raw_fields) }if (n_fields ==0 ) { -synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); +synth_err (SYNTH_ERR_INVALID_CMD ,0 ); ret = -EINVAL ; goto err; }
@@ -1366,6 +1370,40 @@ int synth_event_delete(const char *event_name) }EXPORT_SYMBOL_GPL (synth_event_delete); +static int check_command (const char *raw_command) +{ +char **argv =NULL , *cmd, *saved_cmd, *name_and_field; +int argc, ret =0 ; + + cmd = saved_cmd =kstrdup (raw_command,GFP_KERNEL ); +if (!cmd) +return -ENOMEM ; + + name_and_field =strsep (&cmd,";" ); +if (!name_and_field) { + ret = -EINVAL ; + goto free; + } + +if (name_and_field[0 ] =='!' ) + goto free; + + argv =argv_split (GFP_KERNEL , name_and_field, &argc); +if (!argv) { + ret = -ENOMEM ; + goto free; + } + +if (argc <3 ) + ret = -EINVAL ; +free: +kfree (saved_cmd); +if (argv) +argv_free (argv); + +return ret; +} +static int create_or_delete_synth_event (const char *raw_command) {char *name =NULL , *fields, *p;
@@ -1377,9 +1415,15 @@ static int create_or_delete_synth_event(const char *raw_command)last_cmd_set (raw_command); + ret =check_command (raw_command); +if (ret) { +synth_err (SYNTH_ERR_INVALID_CMD ,0 ); +return ret; + } + p =strpbrk (raw_command," \t" );if (!p && raw_command[0 ] !='!' ) { -synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); +synth_err (SYNTH_ERR_INVALID_CMD ,0 ); ret = -EINVAL ; goto free; }
@@ -1956,8 +2000,10 @@ static int create_synth_event(const char *raw_command)last_cmd_set (raw_command); p =strpbrk (raw_command," \t" ); -if (!p) +if (!p) { +synth_err (SYNTH_ERR_INVALID_CMD ,0 );return -EINVAL ; + } fields =skip_spaces (p);
@@ -1970,13 +2016,21 @@ static int create_synth_event(const char *raw_command)/* This interface accepts group name prefix */ if (strchr (name,'/' )) { len =str_has_prefix (name,SYNTH_SYSTEM "/" ); -if (len ==0 ) +if (len ==0 ) { +synth_err (SYNTH_ERR_INVALID_DYN_CMD ,0 );return -EINVAL ; + } name += len; } len = name - raw_command; + ret =check_command (raw_command + len); +if (ret) { +synth_err (SYNTH_ERR_INVALID_CMD ,0 ); +return ret; + } + name =kmemdup_nul (raw_command + len, p - raw_command - len,GFP_KERNEL );if (!name)return -ENOMEM ;
Signature · 1 line
2.17.1
Re: [PATCH v6 2/6] tracing: Rework synthetic event command parsing
In reply to Tom ZanussiQuoted 9 lines · On Thu, 21 Jan 2021 11:01:05 -0600 Tom Zanussi <[email protected]> wrote:
Now that command parsing has been delegated to the create functions and we're no longer constrained by
argv_split(), we can modify the synthetic event command parser to better match the higher-level structure of the synthetic event commands, which is basically an event name followed by a set of semicolon-separated fields.Since we're also now passed the raw command, we can also save it directly and can get rid of
save_cmdstr().
This looks good to me.
Thank you,
Quoted 343 lines
Signed-off-by Tom Zanussi <[email protected]>--- kernel/trace/trace_events_synth.c | 198 +++++++++++++++--------------- 1 file changed, 98 insertions(+), 100 deletions(-)kernel/trace/trace_events_synth.c +98
-100@@ -48,7 +48,7 @@ static int errpos(const char *str)return err_pos (last_cmd, str); } -static void last_cmd_set (char *str) +static void last_cmd_set (const char *str) {if (!str)return ;@@ -579,18 +579,14 @@ static void free_synth_field(struct synth_field *field)kfree (field); } -static struct synth_field *parse_synth_field (int argc,const char **argv, -int *consumed) +static struct synth_field *parse_synth_field (int argc,char **argv) { -struct synth_field *field;const char *prefix =NULL , *field_type = argv[0 ], *field_name, *array; -int len, ret = -ENOMEM ; +int len, consumed, ret = -ENOMEM ; +struct synth_field *field;struct seq_buf s;ssize_t size; -if (field_type[0 ] ==';' ) - field_type++; -if (!strcmp (field_type,"unsigned" )) {if (argc <3 ) {synth_err (SYNTH_ERR_INCOMPLETE_TYPE ,errpos (field_type));@@ -599,10 +595,20 @@ static struct synth_field *parse_synth_field(int argc, const char **argv, prefix ="unsigned " ; field_type = argv[1 ]; field_name = argv[2 ]; - *consumed =3 ; + consumed =3 ; }else { field_name = argv[1 ]; - *consumed =2 ; + consumed =2 ; + } + +if (consumed < argc) { +synth_err (SYNTH_ERR_INVALID_FIELD ,errpos (field_type)); +return ERR_PTR (-EINVAL ); + } + +if (!field_name) { +synth_err (SYNTH_ERR_INVALID_FIELD ,errpos (field_type)); +return ERR_PTR (-EINVAL ); } field =kzalloc (sizeof (*field),GFP_KERNEL );@@ -613,8 +619,6 @@ static struct synth_field *parse_synth_field(int argc, const char **argv, array =strchr (field_name,'[' );if (array) len -=strlen (array); -else if (field_name[len -1 ] ==';' ) - len--; field->name =kmemdup_nul (field_name, len,GFP_KERNEL );if (!field->name)@@ -626,8 +630,6 @@ static struct synth_field *parse_synth_field(int argc, const char **argv, goto free; } -if (field_type[0 ] ==';' ) - field_type++; len =strlen (field_type) +1 ;if (array)@@ -644,11 +646,8 @@ static struct synth_field *parse_synth_field(int argc, const char **argv,if (prefix)seq_buf_puts (&s, prefix);seq_buf_puts (&s, field_type); -if (array) { +if (array)seq_buf_puts (&s, array); -if (s.buffer[s.len -1 ] ==';' ) - s.len--; - }if (WARN_ON_ONCE (!seq_buf_buffer_left (&s))) goto free;@@ -1160,46 +1159,12 @@ int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name, }EXPORT_SYMBOL_GPL (synth_event_gen_cmd_array_start); -static int save_cmdstr (int argc,const char *name,const char **argv) -{ -struct seq_buf s; -char *buf; -int i; - - buf =kzalloc (MAX_DYNEVENT_CMD_LEN ,GFP_KERNEL ); -if (!buf) -return -ENOMEM ; - -seq_buf_init (&s, buf,MAX_DYNEVENT_CMD_LEN ); - -seq_buf_puts (&s, name); - -for (i =0 ; i < argc; i++) { -seq_buf_putc (&s,' ' ); -seq_buf_puts (&s, argv[i]); - } - -if (!seq_buf_buffer_left (&s)) { -synth_err (SYNTH_ERR_CMD_TOO_LONG ,0 ); -kfree (buf); -return -EINVAL ; - } - buf[s.len] =0 ; -last_cmd_set (buf); - -kfree (buf); -return 0 ; -} - -static int __create_synth_event (int argc,const char *name,const char **argv) +static int __create_synth_event (const char *name,const char *raw_fields) { +char **argv, *field_str, *tmp_fields, *saved_fields =NULL ;struct synth_field *field, *fields[SYNTH_FIELDS_MAX ]; +int i, argc, n_fields =0 , ret =0 ;struct synth_event *event =NULL ; -int i, consumed =0 , n_fields =0 , ret =0 ; - - ret =save_cmdstr (argc, name, argv); -if (ret) -return ret;/* * Argument syntax: @@ -1208,13 +1173,14 @@ static int __create_synth_event(int argc, const char *name, const char **argv)* where 'field' = type field_name */ -if (name[0 ] =='\0' || argc <1 ) { +mutex_lock (&event_mutex); + +if (name[0 ] =='\0' ) {synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); -return -EINVAL ; + ret = -EINVAL ; + goto out; } -mutex_lock (&event_mutex); -if (!is_good_name (name)) {synth_err (SYNTH_ERR_BAD_NAME ,errpos (name)); ret = -EINVAL ;@@ -1228,26 +1194,41 @@ static int __create_synth_event(int argc, const char *name, const char **argv) goto out; } -for (i =0 ; i < argc -1 ; i++) { -if (strcmp (argv[i],";" ) ==0 ) -continue ; -if (n_fields ==SYNTH_FIELDS_MAX ) { -synth_err (SYNTH_ERR_TOO_MANY_FIELDS ,0 ); - ret = -EINVAL ; + tmp_fields = saved_fields =kstrdup (raw_fields,GFP_KERNEL ); +if (!tmp_fields) { + ret = -ENOMEM ; + goto out; + } + +while ((field_str =strsep (&tmp_fields,";" )) !=NULL ) { + argv =argv_split (GFP_KERNEL , field_str, &argc); +if (!argv) { + ret = -ENOMEM ; goto err; } - field =parse_synth_field (argc - i, &argv[i], &consumed); +if (!argc) +continue ; + + field =parse_synth_field (argc, argv);if (IS_ERR (field)) { +argv_free (argv); ret =PTR_ERR (field); goto err; } + +argv_free (argv); + fields[n_fields++] = field; - i += consumed -1 ; +if (n_fields ==SYNTH_FIELDS_MAX ) { +synth_err (SYNTH_ERR_TOO_MANY_FIELDS ,0 ); + ret = -EINVAL ; + goto err; + } } -if (i < argc &&strcmp (argv[i],";" ) !=0 ) { -synth_err (SYNTH_ERR_INVALID_FIELD ,errpos (argv[i])); +if (n_fields ==0 ) { +synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); ret = -EINVAL ; goto err; }@@ -1266,6 +1247,8 @@ static int __create_synth_event(int argc, const char *name, const char **argv) out:mutex_unlock (&event_mutex); +kfree (saved_fields); +return ret; err:for (i =0 ; i < n_fields; i++)@@ -1385,29 +1368,38 @@ EXPORT_SYMBOL_GPL(synth_event_delete);static int create_or_delete_synth_event (const char *raw_command) { -char **argv, *name =NULL ; -int argc =0 , ret =0 ; +char *name =NULL , *fields, *p; +int ret =0 ; - argv =argv_split (GFP_KERNEL , raw_command, &argc); -if (!argv) -return -ENOMEM ; + raw_command =skip_spaces (raw_command); +if (raw_command[0 ] =='\0' ) +return ret; + +last_cmd_set (raw_command); -if (!argc) + p =strpbrk (raw_command," \t" ); +if (!p && raw_command[0 ] !='!' ) { +synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); + ret = -EINVAL ; goto free; + } - name = argv[0 ]; + name =kmemdup_nul (raw_command, p ? p - raw_command :strlen (raw_command),GFP_KERNEL ); +if (!name) +return -ENOMEM ; -/* trace_run_command() ensures argc != 0 */ if (name[0 ] =='!' ) { ret =synth_event_delete (name +1 ); goto free; } - ret =__create_synth_event (argc -1 , name, (const char **)argv +1 ); + fields =skip_spaces (p); + + ret =__create_synth_event (name, fields); free: -argv_free (argv); +kfree (name); -return ret == -ECANCELED ? -EINVAL : ret; +return ret; }static int synth_event_run_command (struct dynevent_cmd *cmd)@@ -1953,39 +1945,45 @@ EXPORT_SYMBOL_GPL(synth_event_trace_end);static int create_synth_event (const char *raw_command) { -char **argv, *name; -int len, argc =0 , ret =0 ; +char *fields, *p; +const char *name; +int len, ret =0 ; - argv =argv_split (GFP_KERNEL , raw_command, &argc); -if (!argv) { - ret = -ENOMEM ; + raw_command =skip_spaces (raw_command); +if (raw_command[0 ] =='\0' )return ret; - } -if (!argc) - goto free; +last_cmd_set (raw_command); - name = argv[0 ]; + p =strpbrk (raw_command," \t" ); +if (!p) +return -EINVAL ; -if (name[0 ] !='s' || name[1 ] !=':' ) { - ret = -ECANCELED ; - goto free; - } + fields =skip_spaces (p); + + name = raw_command; + +if (name[0 ] !='s' || name[1 ] !=':' ) +return -ECANCELED ; name +=2 ;/* This interface accepts group name prefix */ if (strchr (name,'/' )) { len =str_has_prefix (name,SYNTH_SYSTEM "/" ); -if (len ==0 ) { - ret = -EINVAL ; - goto free; - } +if (len ==0 ) +return -EINVAL ; name += len; } - ret =__create_synth_event (argc -1 , name, (const char **)argv +1 ); -free: -argv_free (argv); + len = name - raw_command; + + name =kmemdup_nul (raw_command + len, p - raw_command - len,GFP_KERNEL ); +if (!name) +return -ENOMEM ; + + ret =__create_synth_event (name, fields); + +kfree (name);return ret; }Signature · 1 line
2.17.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
Masami Hiramatsu
maintainerRe: [PATCH v6 3/6] tracing: Update synth command errors
In reply to Tom ZanussiHi Tom,
Quoted 6 lines · On Thu, 21 Jan 2021 11:01:06 -0600 Tom Zanussi <[email protected]> wrote:
Since array types are handled differently, errors referencing them also need to be handled differently. Add and use a new
INVALID_ARRAY_SPECerror. Also addINVALID_CMDandINVALID_DYN_CMDto catch and display the correct form for badly-formed commands, which can also be used in place ofCMD_INCOMPLETE, which is removed, and removeCMD_TOO_LONG, since it's no longer used.
OK, so this will add new errors for precise error logging.
Quoted 85 lines
Signed-off-by Tom Zanussi <[email protected]>--- kernel/trace/trace_events_synth.c | 72 +++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 9 deletions(-)kernel/trace/trace_events_synth.c +33
-6@@ -23,13 +23,14 @@#undef ERRORS#define ERRORS \ C(BAD_NAME, "Illegal name"), \ - C(CMD_INCOMPLETE, "Incomplete command"), \ + C(INVALID_CMD, "Command must be of the form: <name> field[;field] ..."),\ + C(INVALID_DYN_CMD, "Command must be of the form: s or -:[synthetic/]<name> field[;field] ..."),\ C(EVENT_EXISTS, "Event already exists"), \ C(TOO_MANY_FIELDS, "Too many fields"), \ C(INCOMPLETE_TYPE, "Incomplete type"), \ C(INVALID_TYPE, "Invalid type"), \ - C(INVALID_FIELD, "Invalid field"), \ - C(CMD_TOO_LONG, "Command too long"), + C(INVALID_FIELD, "Invalid field"), \ + C(INVALID_ARRAY_SPEC, "Invalid array specification"),#undef C#define C (a, b) SYNTH_ERR_##a@@ -655,7 +656,10 @@ static struct synth_field *parse_synth_field(int argc, char **argv) size =synth_field_size (field->type);if (size <0 ) { -synth_err (SYNTH_ERR_INVALID_TYPE ,errpos (field_type)); +if (array) +synth_err (SYNTH_ERR_INVALID_ARRAY_SPEC ,errpos (field_name)); +else +synth_err (SYNTH_ERR_INVALID_TYPE ,errpos (field_type)); ret = -EINVAL ; goto free; }else if (size ==0 ) {@@ -1176,7 +1180,7 @@ static int __create_synth_event(const char *name, const char *raw_fields)mutex_lock (&event_mutex);if (name[0 ] =='\0' ) { -synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); +synth_err (SYNTH_ERR_INVALID_CMD ,0 ); ret = -EINVAL ; goto out; }@@ -1228,7 +1232,7 @@ static int __create_synth_event(const char *name, const char *raw_fields) }if (n_fields ==0 ) { -synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); +synth_err (SYNTH_ERR_INVALID_CMD ,0 ); ret = -EINVAL ; goto err; }@@ -1366,6 +1370,40 @@ int synth_event_delete(const char *event_name) }EXPORT_SYMBOL_GPL (synth_event_delete); +static int check_command (const char *raw_command) +{ +char **argv =NULL , *cmd, *saved_cmd, *name_and_field; +int argc, ret =0 ; + + cmd = saved_cmd =kstrdup (raw_command,GFP_KERNEL ); +if (!cmd) +return -ENOMEM ; + + name_and_field =strsep (&cmd,";" ); +if (!name_and_field) { + ret = -EINVAL ; + goto free; + } + +if (name_and_field[0 ] =='!' ) + goto free; + + argv =argv_split (GFP_KERNEL , name_and_field, &argc); +if (!argv) { + ret = -ENOMEM ; + goto free; + }
kernel/trace/trace_events_synth.c int synth_event_delete(const char *event_name) · line 1405
Quoted 10 lines
kernel/trace/trace_events_synth.c +10
-0+ +if (argc <3 ) + ret = -EINVAL ; +free: +kfree (saved_cmd); +if (argv) +argv_free (argv); + +return ret; +}
But I'm not sure why this (yet another parser) is needed. What you are expecting for this check_command()? Could you tell me some examples?
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>
Steven Rostedt
maintainerRe: [PATCH v6 2/6] tracing: Rework synthetic event command parsing
In reply to Tom Zanussikernel/trace/trace_events_synth.c static int __create_synth_event(int argc, const char *name, const char **argv) · line 1176
Quoted 6 lines · On Thu, 21 Jan 2021 11:01:05 -0600 Tom Zanussi <[email protected]> wrote:
kernel/trace/trace_events_synth.c +1
-1@@ -1208,13 +1173,14 @@ static int __create_synth_event(int argc, const char *name, const char **argv)* where 'field' = type field_name */ -if (name[0 ] =='\0' || argc <1 ) { +mutex_lock (&event_mutex);
I'm curious, why is the event_mutex taken here? I'm guessing it is first needed for the find_synth_event() call, in which case, it can be moved after the is_good_name() check. I don't see why the goto out is required here or for the is_good_name() check.
-- Steve
Quoted 13 lines
kernel/trace/trace_events_synth.c +4
-3+ +if (name[0 ] =='\0' ) {synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); -return -EINVAL ; + ret = -EINVAL ; + goto out; } -mutex_lock (&event_mutex); -if (!is_good_name (name)) {synth_err (SYNTH_ERR_BAD_NAME ,errpos (name)); ret = -EINVAL ;
Steven Rostedt
maintainerRe: [PATCH v6 3/6] tracing: Update synth command errors
In reply to Tom ZanussiQuoted 16 lines · On Thu, 21 Jan 2021 11:01:06 -0600 Tom Zanussi <[email protected]> wrote:
kernel/trace/trace_events_synth.c +16
-0+static int check_command (const char *raw_command) +{ +char **argv =NULL , *cmd, *saved_cmd, *name_and_field; +int argc, ret =0 ; + + cmd = saved_cmd =kstrdup (raw_command,GFP_KERNEL ); +if (!cmd) +return -ENOMEM ; + + name_and_field =strsep (&cmd,";" ); +if (!name_and_field) { + ret = -EINVAL ; + goto free; + } + +if (name_and_field[0 ] =='!' )
kernel/trace/trace_events_synth.c int synth_event_delete(const char *event_name) · line 1398
Quoted 10 lines
kernel/trace/trace_events_synth.c +10
-0+ goto free; + + argv =argv_split (GFP_KERNEL , name_and_field, &argc); +if (!argv) { + ret = -ENOMEM ; + goto free; + } + +if (argc <3 ) + ret = -EINVAL ;
Nit, you can simply put here:
argv_free(argv);
Hmm, is it used at all? If not, just move it after its allocation check.
kernel/trace/trace_events_synth.c int synth_event_delete(const char *event_name) · line 1402
Quoted 5 lines
kernel/trace/trace_events_synth.c +4
-0+free: +kfree (saved_cmd); +if (argv) +argv_free (argv);
And remove the above, as argv is always NULL when "goto free" is used.
-- Steve
Quoted 4 lines
kernel/trace/trace_events_synth.c +4
-0+ +return ret; +} +
Steven Rostedt
maintainerRe: [PATCH v6 4/6] tracing: Add a backward-compatibility check for synthetic event creation
In reply to Tom Zanussikernel/trace/trace_events_synth.c static void free_synth_field(struct synth_field *field) · line 585
Quoted 10 lines · On Thu, 21 Jan 2021 11:01:07 -0600 Tom Zanussi <[email protected]> wrote:
kernel/trace/trace_events_synth.c +3
-1@@ -580,11 +580,18 @@ static void free_synth_field(struct synth_field *field)kfree (field); } -static struct synth_field *parse_synth_field (int argc,char **argv) +static int check_field_version (const char *prefix,const char *field_type, +const char *field_name) +{
This needs a comment:
/*
* For backward compatibility, the old format did not require
* semicolons, and not to break user space, that old format must
* still work. If a new feature is added, then the format that uses
* the new feature will be required to have semicolons, as nothing
* that uses the old format would be using the new, yet to be
* created, feature. When a new feature is added, this will detect
* it, and return a number greater than 1, and require the format
* to use semicolons.
*/
Or something to that effect.
Quoted 66 lines
kernel/trace/trace_events_synth.c +19
-14+return 1 ; +} + +static struct synth_field *parse_synth_field (int argc,char **argv, +int *consumed,int *field_version) {const char *prefix =NULL , *field_type = argv[0 ], *field_name, *array; -int len, consumed, ret = -ENOMEM ;struct synth_field *field; +int len, ret = -ENOMEM ;struct seq_buf s;ssize_t size;@@ -596,15 +603,10 @@ static struct synth_field *parse_synth_field(int argc, char **argv) prefix ="unsigned " ; field_type = argv[1 ]; field_name = argv[2 ]; - consumed =3 ; + *consumed +=3 ; }else { field_name = argv[1 ]; - consumed =2 ; - } - -if (consumed < argc) { -synth_err (SYNTH_ERR_INVALID_FIELD ,errpos (field_type)); -return ERR_PTR (-EINVAL ); + *consumed +=2 ; }if (!field_name) {@@ -612,6 +614,8 @@ static struct synth_field *parse_synth_field(int argc, char **argv)return ERR_PTR (-EINVAL ); } + *field_version =check_field_version (prefix, field_type, field_name); + field =kzalloc (sizeof (*field),GFP_KERNEL );if (!field)return ERR_PTR (-ENOMEM );@@ -1167,6 +1171,7 @@ static int __create_synth_event(const char *name, const char *raw_fields) {char **argv, *field_str, *tmp_fields, *saved_fields =NULL ;struct synth_field *field, *fields[SYNTH_FIELDS_MAX ]; +int consumed, cmd_version =1 , n_fields_this_loop;int i, argc, n_fields =0 , ret =0 ;struct synth_event *event =NULL ;@@ -1214,21 +1219,46 @@ static int __create_synth_event(const char *name, const char *raw_fields)if (!argc)continue ; - field =parse_synth_field (argc, argv); -if (IS_ERR (field)) { -argv_free (argv); - ret =PTR_ERR (field); - goto err; - } + n_fields_this_loop =0 ; + consumed =0 ; +while (argc > consumed) { +int field_version; + + field =parse_synth_field (argc - consumed, + argv + consumed, &consumed, + &field_version);
kernel/trace/trace_events_synth.c static int __create_synth_event(const char *name, const char *raw_fields) · line 1238
Quoted 10 lines
kernel/trace/trace_events_synth.c +8
-1+if (IS_ERR (field)) { +argv_free (argv); + ret =PTR_ERR (field); + goto err; + } -argv_free (argv); +if (field_version > cmd_version) + cmd_version = field_version; +
There needs to be some comments here to explain what the versioning means.
-- Steve
Quoted 29 lines
kernel/trace/trace_events_synth.c +19
-3+if (cmd_version >1 && n_fields_this_loop >=1 ) { +synth_err (SYNTH_ERR_INVALID_CMD ,errpos (field_str)); + ret = -EINVAL ; + goto err; + } + + fields[n_fields++] = field; +if (n_fields ==SYNTH_FIELDS_MAX ) { +synth_err (SYNTH_ERR_TOO_MANY_FIELDS ,0 ); + ret = -EINVAL ; + goto err; + } + + n_fields_this_loop++; + } - fields[n_fields++] = field; -if (n_fields ==SYNTH_FIELDS_MAX ) { -synth_err (SYNTH_ERR_TOO_MANY_FIELDS ,0 ); +if (consumed < argc) { +synth_err (SYNTH_ERR_INVALID_CMD ,0 ); ret = -EINVAL ; goto err; } + +argv_free (argv); }if (n_fields ==0 ) {
Re: [PATCH v6 2/6] tracing: Rework synthetic event command parsing
In reply to Masami HiramatsuQuoted 14 lines · Masami Hiramatsu
Quoted 10 lines · On Thu, 21 Jan 2021 11:01:05 -0600 Tom Zanussi <[email protected]> wrote:
Now that command parsing has been delegated to the create functions and we're no longer constrained by
argv_split(), we can modify the synthetic event command parser to better match the higher-level structure of the synthetic event commands, which is basically an event
name followed by a set of semicolon-separated fields.Since we're also now passed the raw command, we can also save it directly and can get rid of
save_cmdstr().
Quoted 6 lines
This looks good to me.
Reviewed-by Masami Hiramatsu <[email protected]>Thank you,
Thanks, Masami.
Tom
Quoted 368 lines
Quoted 366 lines
Signed-off-by Tom Zanussi <[email protected]>--- kernel/trace/trace_events_synth.c | 198 +++++++++++++++----------- ---- 1 file changed, 98 insertions(+), 100 deletions(-)kernel/trace/trace_events_synth.c +4
-8@@ -48,7 +48,7 @@ static int errpos(const char *str)return err_pos (last_cmd, str); } -static void last_cmd_set (char *str) +static void last_cmd_set (const char *str) {if (!str)return ;@@ -579,18 +579,14 @@ static void free_synth_field(struct synth_field *field)kfree (field ); } -static struct synth_field *parse_synth_field (int argc,const char **argv, - int *consumed) +static struct synth_field *parse_synth_field(int argc, char **argv) { -struct synth_field *field;const char *prefix =NULL , *field_type = argv[0 ], *field_name, *array; -int len, ret = -ENOMEM ; +int len, consumed, ret = -ENOMEM ; +struct synth_field *field;struct seq_buf s;ssize_t size; -if (field_type[0 ] ==';' ) - field_type++; -if (!strcmp (field_type,"unsigned" )) {if (argc <3 ) { synth_err(SYNTH_ERR_INCOMPLETE_TYPE,
errpos(field_type));kernel/trace/trace_events_synth.c +12
-2@@ -599,10 +595,20 @@ static struct synth_field *parse_synth_field(int argc, const char **argv, prefix = "unsigned "; field_type = argv[1 ]; field_name = argv[2 ]; - *consumed =3 ; + consumed =3 ; }else { field_name = argv[1 ]; - *consumed =2 ; + consumed =2 ; + } + +if (consumed < argc) { +synth_err (SYNTH_ERR_INVALID_FIELD ,errpos (field_type)); +return ERR_PTR (-EINVAL ); + } + +if (!field_name) { +synth_err (SYNTH_ERR_INVALID_FIELD ,errpos (field_type)); +return ERR_PTR (-EINVAL ); }kernel/trace/trace_events_synth.c +0
-2field =kzalloc (sizeof (*field),GFP_KERNEL );@@ -613,8 +619,6 @@ static struct synth_field *parse_synth_field(int argc, const char **argv, array = strchr(field_name, '[');if (array) len -=strlen (array); -else if (field_name[len -1 ] ==';' ) - len--; field->name =kmemdup_nul (field_name, len,GFP_KERNEL );kernel/trace/trace_events_synth.c +0
-2if (!field->name)@@ -626,8 +630,6 @@ static struct synth_field *parse_synth_field(int argc, const char **argv, goto free; } - if (field_type[0] == ';') - field_type++; len = strlen(field_type) + 1;kernel/trace/trace_events_synth.c +9
-45if (array)@@ -644,11 +646,8 @@ static struct synth_field *parse_synth_field(int argc, const char **argv, if (prefix) seq_buf_puts(&s, prefix); seq_buf_puts(&s, field_type); -if (array) { +if (array)seq_buf_puts (&s, array); -if (s.buffer[s.len -1 ] ==';' ) - s.len--; - }if (WARN_ON_ONCE (!seq_buf_buffer_left (&s))) goto free;@@ -1160,46 +1159,12 @@ int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name, }EXPORT_SYMBOL_GPL (synth_event_gen_cmd_array_start ); -static int save_cmdstr (int argc,const char *name,const char **argv) -{ -struct seq_buf s; -char *buf; -int i; - - buf =kzalloc (MAX_DYNEVENT_CMD_LEN ,GFP_KERNEL ); -if (!buf) -return -ENOMEM ; - -seq_buf_init (&s, buf,MAX_DYNEVENT_CMD_LEN ); - -seq_buf_puts (&s, name); - -for (i =0 ; i < argc; i++) { -seq_buf_putc (&s,' ' ); -seq_buf_puts (&s, argv[i]); - } - -if (!seq_buf_buffer_left (&s)) { -synth_err (SYNTH_ERR_CMD_TOO_LONG ,0 ); -kfree (buf); -return -EINVAL ; - } - buf[s.len] =0 ; -last_cmd_set (buf); - -kfree (buf); -return 0 ; -} - -static int __create_synth_event (int argc,const char *name,const char **argv) +static int __create_synth_event (const char *name,const char *raw_fields) { +char **argv, *field_str, *tmp_fields, *saved_fields =NULL ;struct synth_field *field, *fields[SYNTH_FIELDS_MAX ]; +int i, argc, n_fields =0 , ret =0 ;struct synth_event *event =NULL ; -int i, consumed =0 , n_fields =0 , ret =0 ; - - ret =save_cmdstr (argc, name, argv); -if (ret) -return ret;/* * Argument syntax: @@ -1208,13 +1173,14 @@ static int __create_synth_event(int argc,const char *name, const char **argv) * where 'field' = type field_name */ -if (name[0 ] =='\0' || argc <1 ) { +mutex_lock (&event_mutex); + +if (name[0 ] =='\0' ) {synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); -return -EINVAL ; + ret = -EINVAL ; + goto out; } -mutex_lock (&event_mutex); -if (!is_good_name (name)) {synth_err (SYNTH_ERR_BAD_NAME ,errpos (name));kernel/trace/trace_events_synth.c +25
-10ret = -EINVAL ;@@ -1228,26 +1194,41 @@ static int __create_synth_event(int argc, const char *name, const char **argv) goto out; } -for (i =0 ; i < argc -1 ; i++) { -if (strcmp (argv[i],";" ) ==0 ) -continue ; -if (n_fields ==SYNTH_FIELDS_MAX ) { -synth_err (SYNTH_ERR_TOO_MANY_FIELDS ,0 ); - ret = -EINVAL ; + tmp_fields = saved_fields =kstrdup (raw_fields,GFP_KERNEL ); +if (!tmp_fields) { + ret = -ENOMEM ; + goto out; + } + +while ((field_str =strsep (&tmp_fields,";" )) !=NULL ) { + argv =argv_split (GFP_KERNEL , field_str, &argc); +if (!argv) { + ret = -ENOMEM ; goto err; } - field =parse_synth_field (argc - i, &argv[i], &consumed); +if (!argc) +continue ; + + field =parse_synth_field (argc, argv);if (IS_ERR (field)) { +argv_free (argv); ret =PTR_ERR (field); goto err; } + +argv_free (argv); + fields[n_fields++] = field; - i += consumed -1 ; +if (n_fields ==SYNTH_FIELDS_MAX ) { +synth_err (SYNTH_ERR_TOO_MANY_FIELDS ,0 ); + ret = -EINVAL ; + goto err; + } } -if (i < argc &&strcmp (argv[i],";" ) !=0 ) { -synth_err (SYNTH_ERR_INVALID_FIELD ,errpos (argv[i])); +if (n_fields ==0 ) { +synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); ret = -EINVAL ;kernel/trace/trace_events_synth.c +2
-0goto err; }@@ -1266,6 +1247,8 @@ static int __create_synth_event(int argc, const char *name, const char **argv) out: mutex_unlock(&event_mutex); +kfree (saved_fields); +return ret; err:kernel/trace/trace_events_synth.c +48
-31for (i =0 ; i < n_fields; i++)@@ -1385,29 +1368,38 @@ EXPORT_SYMBOL_GPL(synth_event_delete);static int create_or_delete_synth_event (const char *raw_command) { -char **argv, *name =NULL ; -int argc =0 , ret =0 ; +char *name =NULL , *fields, *p; +int ret =0 ; - argv =argv_split (GFP_KERNEL , raw_command, &argc); -if (!argv) -return -ENOMEM ; + raw_command =skip_spaces (raw_command); +if (raw_command[0 ] =='\0' ) +return ret; + +last_cmd_set (raw_command); -if (!argc) + p =strpbrk (raw_command," \t" ); +if (!p && raw_command[0 ] !='!' ) { +synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); + ret = -EINVAL ; goto free; + } - name = argv[0 ]; + name =kmemdup_nul (raw_command, p ? p - raw_command :strlen (raw_command),GFP_KERNEL ); +if (!name) +return -ENOMEM ; -/* trace_run_command() ensures argc != 0 */ if (name[0 ] =='!' ) { ret =synth_event_delete (name +1 ); goto free; } - ret =__create_synth_event (argc -1 , name, (const char **)argv + 1); + fields =skip_spaces (p); + + ret =__create_synth_event (name, fields); free: - argv_free(argv); +kfree (name); -return ret == -ECANCELED ? -EINVAL : ret; +return ret; }static int synth_event_run_command (struct dynevent_cmd *cmd)@@ -1953,39 +1945,45 @@ EXPORT_SYMBOL_GPL(synth_event_trace_end);static int create_synth_event (const char *raw_command) { -char **argv, *name; -int len, argc =0 , ret =0 ; +char *fields, *p; +const char *name; +int len, ret =0 ; - argv =argv_split (GFP_KERNEL , raw_command, &argc); -if (!argv) { - ret = -ENOMEM ; + raw_command =skip_spaces (raw_command); +if (raw_command[0 ] =='\0' )return ret; - } -if (!argc) - goto free; +last_cmd_set (raw_command); - name = argv[0 ]; + p =strpbrk (raw_command," \t" ); +if (!p) +return -EINVAL ; -if (name[0 ] !='s' || name[1 ] !=':' ) { - ret = -ECANCELED ; - goto free; - } + fields =skip_spaces (p); + + name = raw_command; + +if (name[0 ] !='s' || name[1 ] !=':' ) +return -ECANCELED ; name +=2 ;/* This interface accepts group name prefix */ if (strchr (name,'/' )) { len =str_has_prefix (name,SYNTH_SYSTEM "/" ); -if (len ==0 ) { - ret = -EINVAL ; - goto free; - } +if (len ==0 ) +return -EINVAL ; name += len; } - ret =__create_synth_event (argc -1 , name, (const char **)argv + 1); -free: - argv_free(argv); + len = name - raw_command; + + name =kmemdup_nul (raw_command + len, p - raw_command - len,GFP_KERNEL ); +if (!name) +return -ENOMEM ; + + ret =__create_synth_event (name, fields); + +kfree (name);return ret; }Signature · 1 line
2.17.1
Re: [PATCH v6 3/6] tracing: Update synth command errors
In reply to Masami HiramatsuQuoted 116 lines · Masami Hiramatsu
Hi Tom,
Quoted 7 lines · On Thu, 21 Jan 2021 11:01:06 -0600 Tom Zanussi <[email protected]> wrote:
Since array types are handled differently, errors referencing them also need to be handled differently. Add and use a new
INVALID_ARRAY_SPECerror. Also addINVALID_CMDandINVALID_DYN_CMDto
catch and display the correct form for badly-formed commands, which can also be used in place ofCMD_INCOMPLETE, which is removed, and removeCMD_TOO_LONG, since it's no longer used.OK, so this will add new errors for precise error logging.
Quoted 101 lines
Signed-off-by Tom Zanussi <[email protected]>--- kernel/trace/trace_events_synth.c | 72 +++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 9 deletions(-)kernel/trace/trace_events_synth.c +8
-5@@ -23,13 +23,14 @@#undef ERRORS#define ERRORS \ C(BAD_NAME, "Illegal name"), \ - C(CMD_INCOMPLETE, "Incomplete command"), \ + C(INVALID_CMD, "Command must be of the form: <name> field[;field] ..."),\ + C(INVALID_DYN_CMD, "Command must be of the form: s or -:[synthetic/]<name> field[;field] ..."),\C (EVENT_EXISTS ,"Event already exists" ), \C (TOO_MANY_FIELDS ,"Too many fields" ), \C (INCOMPLETE_TYPE ,"Incomplete type" ), \C (INVALID_TYPE ,"Invalid type" ), \ - C(INVALID_FIELD, "Invalid field"), \ - C(CMD_TOO_LONG, "Command too long"), +C (INVALID_FIELD ,"Invalid field" ), \ +C (INVALID_ARRAY_SPEC ,"Invalid array specification" ), #undef C #define C(a, b) SYNTH_ERR_##a@@ -655,7 +656,10 @@ static struct synth_field *parse_synth_field(int argc, char **argv) size = synth_field_size(field->type);if (size <0 ) { -synth_err (SYNTH_ERR_INVALID_TYPE ,errpos (field_type)); +if (array) +synth_err (SYNTH_ERR_INVALID_ARRAY_SPEC ,errpos (field_name)); +else +synth_err (SYNTH_ERR_INVALID_TYPE ,errpos (field_type));kernel/trace/trace_events_synth.c +1
-1ret = -EINVAL ; goto free; }else if (size ==0 ) {@@ -1176,7 +1180,7 @@ static int __create_synth_event(const char *name, const char *raw_fields)mutex_lock (&event_mutex);if (name[0 ] =='\0' ) { -synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); +synth_err (SYNTH_ERR_INVALID_CMD ,0 ); ret = -EINVAL ; goto out;kernel/trace/trace_events_synth.c +1
-1}@@ -1228,7 +1232,7 @@ static int __create_synth_event(const char *name, const char *raw_fields) }if (n_fields ==0 ) { -synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); +synth_err (SYNTH_ERR_INVALID_CMD ,0 ); ret = -EINVAL ; goto err;kernel/trace/trace_events_synth.c +29
-0}@@ -1366,6 +1370,40 @@ int synth_event_delete(const char *event_name) }EXPORT_SYMBOL_GPL (synth_event_delete); +static int check_command (const char *raw_command) +{ +char **argv =NULL , *cmd, *saved_cmd, *name_and_field; +int argc, ret =0 ; + + cmd = saved_cmd =kstrdup (raw_command,GFP_KERNEL ); +if (!cmd) +return -ENOMEM ; + + name_and_field =strsep (&cmd,";" ); +if (!name_and_field) { + ret = -EINVAL ; + goto free; + } + +if (name_and_field[0 ] =='!' ) + goto free; + + argv =argv_split (GFP_KERNEL , name_and_field, &argc); +if (!argv) { + ret = -ENOMEM ; + goto free; + } + +if (argc <3 ) + ret = -EINVAL ; +free: +kfree (saved_cmd); +if (argv)
Quoted 9 lines
kernel/trace/trace_events_synth.c int synth_event_delete(const char *event_name) · line 1405
Quoted 4 lines
kernel/trace/trace_events_synth.c +4
-0+argv_free (argv); + +return ret; +}But I'm not sure why this (yet another parser) is needed. What you are expecting
for thischeck_command()? Could you tell me some examples?
This is just a common function to check we even have the possibility of a valid command. It will catch things right off the top like
# echo 'myevent' >> synthetic_events
or
# echo 'myevent int' >> synthetic events
You're right, it's confusing to have it in this patch, since it really belongs in the rework patch - I'll move it there.
Thanks,
Tom
Quoted 4 lines
Thank you,
Re: [PATCH v6 2/6] tracing: Rework synthetic event command parsing
In reply to Steven RostedtHi Steve,
Quoted 8 lines · Steven Rostedt
Quoted 5 lines · On Thu, 21 Jan 2021 11:01:05 -0600 Tom Zanussi <[email protected]> wrote:
kernel/trace/trace_events_synth.c +0
-0@@ -1208,13 +1173,14 @@ static int __create_synth_event(int argc,const char *name, const char **argv) * where 'field' = type field_name */
Quoted 11 lines
kernel/trace/trace_events_synth.c static int __create_synth_event(int argc, const char *name, const char **argv) · line 1176
Quoted 2 lines
kernel/trace/trace_events_synth.c +1
-1-if (name[0 ] =='\0' || argc <1 ) { +mutex_lock (&event_mutex);I'm curious, why is the
event_mutextaken here? I'm guessing it is first
needed for thefind_synth_event()call, in which case, it can be moved
after theis_good_name()check. I don't see why the goto out is required
here or for theis_good_name()check.
Yes, it's for the find_synth_event() call, and yes, it should come after the is_good_name() check. I'll move it and fix up the goto changes as a result.
Thanks,
Tom
Quoted 15 lines
-- Steve
Quoted 13 lines
kernel/trace/trace_events_synth.c +4
-3+ +if (name[0 ] =='\0' ) {synth_err (SYNTH_ERR_CMD_INCOMPLETE ,0 ); -return -EINVAL ; + ret = -EINVAL ; + goto out; } -mutex_lock (&event_mutex); -if (!is_good_name (name)) {synth_err (SYNTH_ERR_BAD_NAME ,errpos (name));ret =
-EINVAL;
Re: [PATCH v6 4/6] tracing: Add a backward-compatibility check for synthetic event creation
In reply to Steven RostedtHi Steve,
Quoted 31 lines · Steven Rostedt
kernel/trace/trace_events_synth.c static void free_synth_field(struct · line 588
Quoted 13 lines · On Thu, 21 Jan 2021 11:01:07 -0600 Tom Zanussi <[email protected]> wrote:
kernel/trace/trace_events_synth.c +3
-1@@ -580,11 +580,18 @@ static void free_synth_field(struct synth_field *field)kfree (field ); } -static struct synth_field *parse_synth_field (int argc,char **argv) +static int check_field_version (const char *prefix,const char *field_type, +const char *field_name) +{This needs a comment:
/*
* For backward compatibility, the old format did not require
* semicolons, and not to break user space, that old format
must
* still work. If a new feature is added, then the format that
uses
* the new feature will be required to have semicolons, as
nothing
* that uses the old format would be using the new, yet to be
* created, feature. When a new feature is added, this will
detect
* it, and return a number greater than 1, and require the
format
Quoted 5 lines
* to use semicolons. */Or something to that effect.
OK, will add better comments in the code about how this is meant to work.
Thanks,
Tom
Quoted 126 lines
Quoted 75 lines
kernel/trace/trace_events_synth.c +5
-0+return 1 ; +} + +static struct synth_field *parse_synth_field (int argc,char **argv, +int *consumed,int *field_version) { const char *prefix = NULL, *field_type = argv[0], *field_name,*array;
kernel/trace/trace_events_synth.c +3
-8-int len, consumed, ret = -ENOMEM ;struct synth_field *field; +int len, ret = -ENOMEM ;struct seq_buf s;ssize_t size;@@ -596,15 +603,10 @@ static struct synth_field *parse_synth_field(int argc, char **argv) prefix = "unsigned "; field_type = argv[1 ]; field_name = argv[2 ]; - consumed =3 ; + *consumed +=3 ; }else { field_name = argv[1 ]; - consumed =2 ; - } - -if (consumed < argc) { -synth_err (SYNTH_ERR_INVALID_FIELD ,errpos (field_type)); -return ERR_PTR (-EINVAL ); + *consumed +=2 ; }kernel/trace/trace_events_synth.c +2
-0if (!field_name) {@@ -612,6 +614,8 @@ static struct synth_field *parse_synth_field(int argc, char **argv)return ERR_PTR (-EINVAL ); } + *field_version =check_field_version (prefix, field_type, field_name); + field =kzalloc (sizeof (*field),GFP_KERNEL );kernel/trace/trace_events_synth.c +9
-6if (!field)return ERR_PTR (-ENOMEM );@@ -1167,6 +1171,7 @@ static int __create_synth_event(const char *name, const char *raw_fields) {char **argv, *field_str, *tmp_fields, *saved_fields =NULL ;struct synth_field *field, *fields[SYNTH_FIELDS_MAX ]; +int consumed, cmd_version =1 , n_fields_this_loop;int i, argc, n_fields =0 , ret =0 ;struct synth_event *event =NULL ;@@ -1214,21 +1219,46 @@ static int __create_synth_event(const char *name, const char *raw_fields)if (!argc)continue ; - field =parse_synth_field (argc, argv); -if (IS_ERR (field)) { -argv_free (argv); - ret =PTR_ERR (field); - goto err; - } + n_fields_this_loop =0 ; + consumed =0 ; +while (argc > consumed) { +int field_version; + + field =parse_synth_field (argc - consumed, + argv + consumed, &consumed, + &field_version);kernel/trace/trace_events_synth.c static int __create_synth_event(const char · line 1240
Quoted 10 lines
kernel/trace/trace_events_synth.c +8
-1+if (IS_ERR (field)) { +argv_free (argv); + ret =PTR_ERR (field); + goto err; + } -argv_free (argv); +if (field_version > cmd_version) + cmd_version = field_version; +There needs to be some comments here to explain what the versioning means.
-- Steve
Quoted 32 lines
kernel/trace/trace_events_synth.c +19
-3+if (cmd_version >1 && n_fields_this_loop >=1 ) { +synth_err (SYNTH_ERR_INVALID_CMD ,errpos (field_str)); + ret = -EINVAL ; + goto err; + } + + fields[n_fields++] = field; +if (n_fields ==SYNTH_FIELDS_MAX ) { +synth_err (SYNTH_ERR_TOO_MANY_FIELDS ,0 ); + ret = -EINVAL ; + goto err; + } + + n_fields_this_loop++; + } - fields[n_fields++] = field; -if (n_fields ==SYNTH_FIELDS_MAX ) { -synth_err (SYNTH_ERR_TOO_MANY_FIELDS ,0 ); +if (consumed < argc) { +synth_err (SYNTH_ERR_INVALID_CMD ,0 ); ret = -EINVAL ; goto err; } + +argv_free (argv); }if (n_fields ==0 ) {