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

From: Kyuwon Cho

Date: Sat Dec 05 2026 - 03:21:00 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.

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 complete removal set: the target
and all of its auxiliary histograms. Remove the auxiliary histograms in
reverse creation order so sibling dependencies disappear first.

Fixes: 02205a6752f2 ("tracing: Add support for 'field variables'")
Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
---
Changes in v2:
- Exclude the target and all of its auxiliary histograms from reference
checks, then tear the auxiliary set down in reverse order. This addresses
the sibling-auxiliary case in the Sashiko AI review:
https://lore.kernel.org/all/sashiko-outbox-249148@xxxxxxxxxx/

v1: https://lore.kernel.org/all/179634936693.931432.2335780990204797817@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 | 61 ++++++++++++++++++++++++++------
1 file changed, 51 insertions(+), 10 deletions(-)

diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index 7f5ec9ff19e5..a5fc58736a48 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;
};

@@ -962,10 +963,27 @@ static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
return NULL;
}

+static bool field_var_hist_is_owned_by(struct hist_trigger_data *hist_data,
+ struct hist_trigger_data *owner)
+{
+ unsigned int i;
+
+ if (!owner)
+ return false;
+
+ for (i = 0; i < owner->n_field_var_hists; i++) {
+ if (owner->field_var_hists[i]->hist_data == hist_data)
+ return true;
+ }
+
+ return false;
+}
+
/**
* 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: A target trigger whose complete removal set should be ignored
*
* Check to see whether the given variable is currently referenced by
* any other trigger.
@@ -977,14 +995,17 @@ 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 ||
+ field_var_hist_is_owned_by(var_data->hist_data, exclude))
continue;
found = find_var_ref(var_data->hist_data, hist_data, var_idx);
if (found)
@@ -997,6 +1018,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 +1029,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 +1039,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 +1049,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 +1152,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 +3198,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;
}
@@ -6374,9 +6413,9 @@ static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
char *cmd;
int ret;

- for (i = 0; i < hist_data->n_field_var_hists; i++) {
- file = hist_data->field_var_hists[i]->file;
- cmd = hist_data->field_var_hists[i]->cmd;
+ for (i = hist_data->n_field_var_hists; i > 0; i--) {
+ file = hist_data->field_var_hists[i - 1]->file;
+ cmd = hist_data->field_var_hists[i - 1]->cmd;
ret = event_hist_trigger_parse(&trigger_hist_cmd, file,
"!hist", "hist", cmd);
WARN_ON_ONCE(ret < 0);
@@ -6760,7 +6799,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 +6863,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;
}
}