2022-02-06 12:49:22

by Tom Zanussi

[permalink] [raw]
Subject: [PATCH v8 0/4] tracing: Add and use event_command parsing func helpers

With more event commands being implemented, it's been pointed out that
it would make sense to clean up the existing ones and make it easier
to implement new ones without copying a lot of boilerplate. The main
culprit here is the event_command.func() callback - the rest of the
event_command infrastructure has default implementations that work for
most implementations. The func() callback is a little different in
that every new command needs to customize parsing to some extent.

This patchset attempts to help clean that up and make it easier for
new users to deal with.

v8: Patches 1-4 of v7 were merged and so don't appear here.

Added new patch 'tracing: Remove logic for registering multiple
event triggers at a time' because the code doesn't ever register
multiple triggers and it just complicates the code. Doing this
also removes the unreg() call along with just about everything
else from event_trigger_register() so is no longer a policy
violation. Also added a new function event_trigger_unregister()
to match event_trigger_register() and clean up the code even more.

Moved 'tracing: Remove redundant trigger_ops params' so it doesn't
depend on the following patches.

Added 'tracing: Separate hist state updates from hist
registration' to remove the confusing logic concerning
pause/cont/clear and which caused problems in v7.

v7: Missed a fixup in patch 6/6 to a data->ops->print() in the debug
code since I forgot to build with CONFIG_HIST_TRIGGERS_DEBUG.

Patches 1-5 remain the same.

v6: Fixed warning for an uninitialized trigger_ops local in
event_trigger_parse() pointed out by Steve and the kernel test
robot. This was done by removing the need for the local by
removing the redundant trigger_ops params in [PATCH v6 3/6]
tracing: Remove ops param from event_command reg()/unreg()
callbacks.

Also added a follow-on patch to remove all the other instances of
redundant trigger_ops in [PATCH v6 6/6] tracing: Remove redundant
trigger_ops params

v5: Changed the parsing helper function example components param and
filter to reflect only the param and filter components, and added
param_and_filter which now appears in the functions.

Removed the check for a NULL *param following the strsep
separating the param from the filter in
event_trigger_separate_filter() because it will never be true due
to the previous check for NULL param_and_filter previously.

Changed the param name from trigger to param and updated the
function documentation in event_trigger_parse_num().

v4: Added two patches changing the names of event_command.func() and
event_trigger_ops.func() to make them reflect their functions.

Added back missing kfree(trigger_data) in event_trigger_callback().

Changed char *param to const char *param in
event_trigger_check_remove() and event_trigger_empty_param().

Changed event_trigger_separate_filter() to use separate param and
filter outparams, and changed the name of the param inparam to
param_and_filter to better reflect its contents and avoid the
clash with new param outparam. Changed all parse()
implementations to use this new scheme.

Fixed some typos and added more extensive comments with examples
explaining various things that were mentioned as causing confusion
and just in general tried to clarify things with respect to the
callbacks and parameters.

v3: broke up event_trigger_check() into smaller functions instead of
parameterizing it, and added function documentation.

v2: removed unused event_trigger_remove(). No change in functionality.


The following changes since commit f125ef075cd648a7794aa0cc61a188b1c40c8f94:

tracing: Remove size restriction on synthetic event cmd error logging (2022-01-27 19:15:51 -0500)

are available in the Git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/zanussi/linux-trace.git ftrace/cleanup-hist-func-v8

Tom Zanussi (4):
tracing: Remove logic for registering multiple event triggers at a
time
tracing: Remove redundant trigger_ops params
tracing: Have existing event_command.parse() implementations use
helpers
tracing: Separate hist state updates from hist registration

kernel/trace/trace.h | 26 ++-
kernel/trace/trace_eprobe.c | 10 +-
kernel/trace/trace_events_hist.c | 166 ++++++++--------
kernel/trace/trace_events_trigger.c | 292 +++++++++-------------------
4 files changed, 193 insertions(+), 301 deletions(-)

--
2.17.1



2022-02-07 16:48:50

by Tom Zanussi

[permalink] [raw]
Subject: [PATCH v8 1/4] tracing: Remove logic for registering multiple event triggers at a time

Code for registering triggers assumes it's possible to register more
than one trigger at a time. In fact, it's unimplemented and there
doesn't seem to be a reason to do that.

Remove the n_registered param from event_trigger_register() and fix up
callers.

Doing so simplifies the logic in event_trigger_register to the point
that it just becomes a wrapper calling event_command.reg().

It also removes the problematic call to event_command.unreg() in case
of failure. A new function, event_trigger_unregister() is also added
for callers to call themselves.

The changes to trace_events_hist.c simply allow compilation; a
separate patch follows which updates the hist triggers to work
correctly with the new changes.

Signed-off-by: Tom Zanussi <[email protected]>
---
kernel/trace/trace.h | 9 +--
kernel/trace/trace_events_hist.c | 17 ++---
kernel/trace/trace_events_trigger.c | 96 ++++++++++-------------------
3 files changed, 45 insertions(+), 77 deletions(-)

diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
index 0f5e22238cd2..835f1e3d9924 100644
--- a/kernel/trace/trace.h
+++ b/kernel/trace/trace.h
@@ -1630,10 +1630,11 @@ extern void event_trigger_reset_filter(struct event_command *cmd_ops,
extern int event_trigger_register(struct event_command *cmd_ops,
struct trace_event_file *file,
char *glob,
- char *cmd,
- char *trigger,
- struct event_trigger_data *trigger_data,
- int *n_registered);
+ struct event_trigger_data *trigger_data);
+extern void event_trigger_unregister(struct event_command *cmd_ops,
+ struct trace_event_file *file,
+ char *glob,
+ struct event_trigger_data *trigger_data);

/**
* struct event_trigger_ops - callbacks for trace event triggers
diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index be4a001a607f..8df815bc0ac5 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -6284,7 +6284,7 @@ static int event_hist_trigger_parse(struct event_command *cmd_ops,
goto out_free;
}

- cmd_ops->unreg(glob+1, trigger_data, file);
+ event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
se_name = trace_event_name(file->event_call);
se = find_synth_event(se_name);
if (se)
@@ -6293,13 +6293,10 @@ static int event_hist_trigger_parse(struct event_command *cmd_ops,
goto out_free;
}

- ret = cmd_ops->reg(glob, trigger_data, file);
- /*
- * The above returns on success the # of triggers registered,
- * but if it didn't register any it returns zero. Consider no
- * triggers registered a failure too.
- */
- if (!ret) {
+ ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
+ if (ret)
+ goto out_free;
+ if (ret == 0) {
if (!(attrs->pause || attrs->cont || attrs->clear))
ret = -ENOENT;
goto out_free;
@@ -6328,15 +6325,13 @@ static int event_hist_trigger_parse(struct event_command *cmd_ops,
se = find_synth_event(se_name);
if (se)
se->ref++;
- /* Just return zero, not the number of registered triggers */
- ret = 0;
out:
if (ret == 0)
hist_err_clear();

return ret;
out_unreg:
- cmd_ops->unreg(glob+1, trigger_data, file);
+ event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
out_free:
if (cmd_ops->set_filter)
cmd_ops->set_filter(NULL, trigger_data, NULL);
diff --git a/kernel/trace/trace_events_trigger.c b/kernel/trace/trace_events_trigger.c
index d00fee705f9c..0ab86b5449d7 100644
--- a/kernel/trace/trace_events_trigger.c
+++ b/kernel/trace/trace_events_trigger.c
@@ -573,13 +573,12 @@ static int register_trigger(char *glob,
}

list_add_rcu(&data->list, &file->triggers);
- ret++;

update_cond_flag(file);
- if (trace_event_trigger_enable_disable(file, 1) < 0) {
+ ret = trace_event_trigger_enable_disable(file, 1);
+ if (ret < 0) {
list_del_rcu(&data->list);
update_cond_flag(file);
- ret--;
}
out:
return ret;
@@ -913,48 +912,37 @@ void event_trigger_reset_filter(struct event_command *cmd_ops,
* @cmd_ops: The event_command operations for the trigger
* @file: The event file for the trigger's event
* @glob: The trigger command string, with optional remove(!) operator
- * @cmd: The cmd string
- * @param: The param string
* @trigger_data: The trigger_data for the trigger
- * @n_registered: optional outparam, the number of triggers registered
*
* Register an event trigger. The @cmd_ops are used to call the
- * cmd_ops->reg() function which actually does the registration. The
- * cmd_ops->reg() function returns the number of triggers registered,
- * which is assigned to n_registered, if n_registered is non-NULL.
+ * cmd_ops->reg() function which actually does the registration.
*
* Return: 0 on success, errno otherwise
*/
int event_trigger_register(struct event_command *cmd_ops,
struct trace_event_file *file,
char *glob,
- char *cmd,
- char *param,
- struct event_trigger_data *trigger_data,
- int *n_registered)
+ struct event_trigger_data *trigger_data)
{
- int ret;
-
- if (n_registered)
- *n_registered = 0;
-
- ret = cmd_ops->reg(glob, trigger_data, file);
- /*
- * The above returns on success the # of functions enabled,
- * but if it didn't find any functions it returns zero.
- * Consider no functions a failure too.
- */
- if (!ret) {
- cmd_ops->unreg(glob, trigger_data, file);
- ret = -ENOENT;
- } else if (ret > 0) {
- if (n_registered)
- *n_registered = ret;
- /* Just return zero, not the number of enabled functions */
- ret = 0;
- }
+ return cmd_ops->reg(glob, trigger_data, file);
+}

- return ret;
+/**
+ * event_trigger_unregister - unregister an event trigger
+ * @cmd_ops: The event_command operations for the trigger
+ * @file: The event file for the trigger's event
+ * @glob: The trigger command string, with optional remove(!) operator
+ * @trigger_data: The trigger_data for the trigger
+ *
+ * Unregister an event trigger. The @cmd_ops are used to call the
+ * cmd_ops->unreg() function which actually does the unregistration.
+ */
+void event_trigger_unregister(struct event_command *cmd_ops,
+ struct trace_event_file *file,
+ char *glob,
+ struct event_trigger_data *trigger_data)
+{
+ cmd_ops->unreg(glob, trigger_data, file);
}

/*
@@ -1013,7 +1001,7 @@ event_trigger_parse(struct event_command *cmd_ops,
INIT_LIST_HEAD(&trigger_data->named_list);

if (glob[0] == '!') {
- cmd_ops->unreg(glob+1, trigger_data, file);
+ event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
kfree(trigger_data);
ret = 0;
goto out;
@@ -1048,17 +1036,10 @@ event_trigger_parse(struct event_command *cmd_ops,
out_reg:
/* Up the trigger_data count to make sure reg doesn't free it on failure */
event_trigger_init(trigger_ops, trigger_data);
- ret = cmd_ops->reg(glob, trigger_data, file);
- /*
- * The above returns on success the # of functions enabled,
- * but if it didn't find any functions it returns zero.
- * Consider no functions a failure too.
- */
- if (!ret) {
- cmd_ops->unreg(glob, trigger_data, file);
- ret = -ENOENT;
- } else if (ret > 0)
- ret = 0;
+
+ ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
+ if (ret)
+ goto out_free;

/* Down the counter of trigger_data or free it if not used anymore */
event_trigger_free(trigger_ops, trigger_data);
@@ -1795,7 +1776,7 @@ int event_enable_trigger_parse(struct event_command *cmd_ops,
trigger_data->private_data = enable_data;

if (glob[0] == '!') {
- cmd_ops->unreg(glob+1, trigger_data, file);
+ event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
kfree(trigger_data);
kfree(enable_data);
ret = 0;
@@ -1842,19 +1823,11 @@ int event_enable_trigger_parse(struct event_command *cmd_ops,
ret = trace_event_enable_disable(event_enable_file, 1, 1);
if (ret < 0)
goto out_put;
- ret = cmd_ops->reg(glob, trigger_data, file);
- /*
- * The above returns on success the # of functions enabled,
- * but if it didn't find any functions it returns zero.
- * Consider no functions a failure too.
- */
- if (!ret) {
- ret = -ENOENT;
- goto out_disable;
- } else if (ret < 0)
+
+ ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
+ if (ret)
goto out_disable;
- /* Just return zero, not the number of enabled functions */
- ret = 0;
+
event_trigger_free(trigger_ops, trigger_data);
out:
return ret;
@@ -1900,13 +1873,12 @@ int event_enable_register_trigger(char *glob,
}

list_add_rcu(&data->list, &file->triggers);
- ret++;

update_cond_flag(file);
- if (trace_event_trigger_enable_disable(file, 1) < 0) {
+ ret = trace_event_trigger_enable_disable(file, 1);
+ if (ret < 0) {
list_del_rcu(&data->list);
update_cond_flag(file);
- ret--;
}
out:
return ret;
--
2.17.1


2022-02-08 22:29:52

by Tom Zanussi

[permalink] [raw]
Subject: [PATCH v8 4/4] tracing: Separate hist state updates from hist registration

hist_register_trigger() handles both new hist registration as well as
existing hist registration through event_command.reg().

Adding a new function, existing_hist_update_only(), that checks and
updates existing histograms and exits after doing so allows the
confusing logic in event_hist_trigger_parse() to be simplified.

Signed-off-by: Tom Zanussi <[email protected]>
---
kernel/trace/trace_events_hist.c | 66 +++++++++++++++++++++++---------
1 file changed, 48 insertions(+), 18 deletions(-)

diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index 28604e17bc73..bc52b03be11a 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -5926,6 +5926,48 @@ static bool hist_trigger_match(struct event_trigger_data *data,
return true;
}

+static bool existing_hist_update_only(char *glob,
+ struct event_trigger_data *data,
+ struct trace_event_file *file)
+{
+ struct hist_trigger_data *hist_data = data->private_data;
+ struct event_trigger_data *test, *named_data = NULL;
+ bool updated = false;
+
+ if (!hist_data->attrs->pause && !hist_data->attrs->cont &&
+ !hist_data->attrs->clear)
+ goto out;
+
+ if (hist_data->attrs->name) {
+ named_data = find_named_trigger(hist_data->attrs->name);
+ if (named_data) {
+ if (!hist_trigger_match(data, named_data, named_data,
+ true))
+ goto out;
+ }
+ }
+
+ if (hist_data->attrs->name && !named_data)
+ goto out;
+
+ list_for_each_entry(test, &file->triggers, list) {
+ if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
+ if (!hist_trigger_match(data, test, named_data, false))
+ continue;
+ if (hist_data->attrs->pause)
+ test->paused = true;
+ else if (hist_data->attrs->cont)
+ test->paused = false;
+ else if (hist_data->attrs->clear)
+ hist_clear(test);
+ updated = true;
+ goto out;
+ }
+ }
+ out:
+ return updated;
+}
+
static int hist_register_trigger(char *glob,
struct event_trigger_data *data,
struct trace_event_file *file)
@@ -5954,19 +5996,11 @@ static int hist_register_trigger(char *glob,

list_for_each_entry(test, &file->triggers, list) {
if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
- if (!hist_trigger_match(data, test, named_data, false))
- continue;
- if (hist_data->attrs->pause)
- test->paused = true;
- else if (hist_data->attrs->cont)
- test->paused = false;
- else if (hist_data->attrs->clear)
- hist_clear(test);
- else {
+ if (hist_trigger_match(data, test, named_data, false)) {
hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
ret = -EEXIST;
+ goto out;
}
- goto out;
}
}
new:
@@ -6005,8 +6039,6 @@ static int hist_register_trigger(char *glob,

if (named_data)
destroy_hist_data(hist_data);
-
- ret++;
out:
return ret;
}
@@ -6274,14 +6306,12 @@ static int event_hist_trigger_parse(struct event_command *cmd_ops,
goto out_free;
}

- ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
- if (ret)
+ if (existing_hist_update_only(glob, trigger_data, file))
goto out_free;
- if (ret == 0) {
- if (!(attrs->pause || attrs->cont || attrs->clear))
- ret = -ENOENT;
+
+ ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
+ if (ret < 0)
goto out_free;
- }

if (get_named_trigger_data(trigger_data))
goto enable;
--
2.17.1