[PATCH] tracing: check field variable references before removing histograms

From: Kyuwon Cho

Date: Fri Dec 04 2026 - 00:12:56 EST


A field variable creates an auxiliary source histogram owned by its target
histogram. Another histogram can reference a variable in that auxiliary
histogram. Removing the target checks only references to the target's own
variables, then proceeds even though removal of the auxiliary histogram is
busy. Unregistering the target's field variable histogram consequently
warns and leaves a stale variable registration:

WARNING: kernel/trace/trace_events_hist.c:6379 at
unregister_field_var_hists
RIP: 0010:unregister_field_var_hists
(kernel/trace/trace_events_hist.c:6379)
Call Trace:
event_hist_trigger_free (kernel/trace/trace_events_hist.c:6400)
event_hist_trigger_parse (kernel/trace/trace_events_hist.c:6961)
trigger_process_regex (kernel/trace/trace_events_trigger.c:388)
event_trigger_write (kernel/trace/trace_events_trigger.c:418)

Keep the auxiliary histogram pointer with each field variable histogram.
Before single-trigger or file-wide removal, check its variables for
external references while excluding the owning target's expected reference.

Fixes: 02205a6752f2 ("tracing: Add support for 'field variables'")
Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
---
This issue was found by an LLM-guided test generator used in our research prototype.
A reproducer is available on request.

kernel/trace/trace_events_hist.c | 38 ++++++++++++++++++++++++++------
1 file changed, 31 insertions(+), 7 deletions(-)

diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index 7f5ec9ff19e5..b581909bad98 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -558,6 +558,7 @@ struct field_var {

struct field_var_hist {
struct trace_event_file *file;
+ struct hist_trigger_data *hist_data;
char *cmd;
};

@@ -966,6 +967,7 @@ static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
* find_any_var_ref - Check if there is a reference to a given trigger variable
* @hist_data: The hist trigger
* @var_idx: The trigger variable identifier
+ * @exclude: An additional trigger to ignore
*
* Check to see whether the given variable is currently referenced by
* any other trigger.
@@ -977,14 +979,16 @@ static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
* Return: The VAR_REF field referencing the variable if so, NULL if not
*/
static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
- unsigned int var_idx)
+ unsigned int var_idx,
+ struct hist_trigger_data *exclude)
{
struct trace_array *tr = hist_data->event_file->tr;
struct hist_field *found = NULL;
struct hist_var_data *var_data;

list_for_each_entry(var_data, &tr->hist_vars, list) {
- if (var_data->hist_data == hist_data)
+ if (var_data->hist_data == hist_data ||
+ var_data->hist_data == exclude)
continue;
found = find_var_ref(var_data->hist_data, hist_data, var_idx);
if (found)
@@ -997,6 +1001,7 @@ static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
/**
* check_var_refs - Check if there is a reference to any of trigger's variables
* @hist_data: The hist trigger
+ * @exclude: An additional trigger to ignore
*
* A trigger can define one or more variables. If any one of them is
* currently referenced by any other trigger, this function will
@@ -1007,7 +1012,8 @@ static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
*
* Return: True if there is a reference to any of trigger's variables
*/
-static bool check_var_refs(struct hist_trigger_data *hist_data)
+static bool check_var_refs(struct hist_trigger_data *hist_data,
+ struct hist_trigger_data *exclude)
{
struct hist_field *field;
bool found = false;
@@ -1016,7 +1022,7 @@ static bool check_var_refs(struct hist_trigger_data *hist_data)
for_each_hist_field(i, hist_data) {
field = hist_data->fields[i];
if (field && field->flags & HIST_FIELD_FL_VAR) {
- if (find_any_var_ref(hist_data, field->var.idx)) {
+ if (find_any_var_ref(hist_data, field->var.idx, exclude)) {
found = true;
break;
}
@@ -1026,6 +1032,21 @@ static bool check_var_refs(struct hist_trigger_data *hist_data)
return found;
}

+static bool check_field_var_refs(struct hist_trigger_data *hist_data)
+{
+ unsigned int i;
+
+ for (i = 0; i < hist_data->n_field_var_hists; i++) {
+ if (!hist_data->field_var_hists[i]->hist_data)
+ continue;
+ if (check_var_refs(hist_data->field_var_hists[i]->hist_data,
+ hist_data))
+ return true;
+ }
+
+ return false;
+}
+
static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
{
struct trace_array *tr = hist_data->event_file->tr;
@@ -1114,7 +1135,7 @@ static void remove_hist_vars(struct hist_trigger_data *hist_data)
if (!var_data)
return;

- if (WARN_ON(check_var_refs(hist_data)))
+ if (WARN_ON(check_var_refs(hist_data, NULL)))
return;

list_del(&var_data->list);
@@ -3160,6 +3181,7 @@ create_field_var_hist(struct hist_trigger_data *target_hist_data,
hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
return ERR_PTR(-EINVAL);
}
+ var_hist->hist_data = event_var->var.hist_data;

return event_var;
}
@@ -6760,7 +6782,8 @@ static bool hist_trigger_check_refs(struct event_trigger_data *data,
if (!hist_trigger_match(data, test, named_data, false))
continue;
hist_data = test->private_data;
- if (check_var_refs(hist_data))
+ if (check_var_refs(hist_data, NULL) ||
+ check_field_var_refs(hist_data))
return true;
break;
}
@@ -6823,7 +6846,8 @@ static bool hist_file_check_refs(struct trace_event_file *file)
list_for_each_entry(test, &file->triggers, list) {
if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
hist_data = test->private_data;
- if (check_var_refs(hist_data))
+ if (check_var_refs(hist_data, NULL) ||
+ check_field_var_refs(hist_data))
return true;
}
}
--
2.43.0