Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [for-linus][PATCH 1/8] tracing: Fix crash on lseek of recursed_functions file in WRONLY mode
       [not found] <20261203141725.845506016@kernel.org>
@ 2026-12-03 14:17 ` Steven Rostedt
  2026-12-03 14:17 ` [for-linus][PATCH 2/8] tracing/filters: Fix glob matching on zero size string fields Steven Rostedt
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: Steven Rostedt @ 2026-12-03 14:17 UTC (permalink / raw)
  To: linux-kernel
  Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
	stable, sashiko-bot

From: Steven Rostedt <rostedt@goodmis.org>

The lseek method for the "recursed_functions" file only handles
seq_lseek() which is for when the file is opened for reading. If the file
is opened for WRONLY, it will cause a NULL pointer dereferenced:

Oops: general protection fault, probably for non-canonical address 0xdffffc0000000012: 0000 [#1] SMP KASAN PTI
 KASAN: null-ptr-deref in range [0x0000000000000090-0x0000000000000097]
 CPU: 5 UID: 0 PID: 1100 Comm: lseek Not tainted 7.4.0-rc4-test-00006-g4dca8a8ee893 #36 PREEMPT(lazy)
 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.17.0-debian-1.17.0-1 04/01/2014
 RIP: 0010:__mutex_lock+0x162/0x2550
 Code: 08 84 d2 0f 85 a1 1c 00 00 44 8b 0d 38 cb df 03 45 85 c9 75 29 48 b8 00 00 00 00 00 fc ff df 48 8d 7b 58 48 89 fa 48 c1 ea 03 <80> 3c 02 00 0f 85 a6 1c 00 00 48 3b 5b 58 0f 85 3f 0e 00 00 bf 01
 RSP: 0018:ffff8881211afd10 EFLAGS: 00010206
 RAX: dffffc0000000000 RBX: 0000000000000038 RCX: 8000000000000000
 RDX: 0000000000000012 RSI: ffffffff8588bdfb RDI: 0000000000000090
 RBP: ffff8881211afe70 R08: 0000000000000001 R09: 0000000000000000
 R10: ffff8881083f8000 R11: ffffed10247ebc54 R12: 0000000000000000
 R13: 0000000000000000 R14: dffffc0000000000 R15: 1ffff11024235fac
 FS:  00007fb27d236780(0000) GS:ffff8882b4581000(0000) knlGS:0000000000000000
 CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
 CR2: 000055fb6eb6e008 CR3: 000000011d26e006 CR4: 0000000000172ef0
 Call Trace:
  <TASK>
  ? seq_lseek+0x60/0x3d0
  ? kmem_cache_free+0x14b/0x610
  ? __pfx___mutex_lock+0x10/0x10
  ? do_sys_openat2+0xd8/0x180
  ? seq_lseek+0x60/0x3d0
  seq_lseek+0x60/0x3d0
  ? __pfx___x64_sys_openat+0x10/0x10
  ksys_lseek+0x3c/0xb0
  do_syscall_64+0xcd/0x6f0
  entry_SYSCALL_64_after_hwframe+0x76/0x7e
 RIP: 0033:0x7fb27d33fa17
 Code: 8b 05 fd 03 0e 00 64 c7 00 0d 00 00 00 eb b2 e8 ef 98 01 00 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 b8 08 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 01 c3 48 8b 15 c9 03 0e 00 f7 d8 64 89 02 48
 RSP: 002b:00007ffd486d0258 EFLAGS: 00000202 ORIG_RAX: 0000000000000008
 RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007fb27d33fa17
 RDX: 0000000000000002 RSI: 0000000000000000 RDI: 0000000000000003
 RBP: 00007ffd486d0280 R08: 0000000000000000 R09: 0000000000000000
 R10: 0000000000000000 R11: 0000000000000202 R12: 0000000000000002
 R13: 00007fb27d482000 R14: 00007ffd486d03b0 R15: 000055fb6eb6cdd8
  </TASK>
 Modules linked in:
 ---[ end trace 0000000000000000 ]---

A helper function "tracing_lseek()" already exists for this use case. Have
the file_operations for the recursed_functions file use that.

Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20261125162834.2df4c684@gandalf.local.home
Fixes: 773c16705058e ("ftrace: Add recording of functions that caused recursion")
Reported-by: sashiko-bot@kernel.org
Closes: https://sashiko.dev/?list=org.kernel.vger.linux-trace-kernel#/patchset/20260502174741.39636-1-yashsuthar983%40gmail.com
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
 kernel/trace/trace_recursion_record.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/trace/trace_recursion_record.c b/kernel/trace/trace_recursion_record.c
index 4f63bec052fc..0452c9c4c558 100644
--- a/kernel/trace/trace_recursion_record.c
+++ b/kernel/trace/trace_recursion_record.c
@@ -217,7 +217,7 @@ static const struct file_operations recursed_functions_fops = {
 	.open           = recursed_function_open,
 	.write		= recursed_function_write,
 	.read           = seq_read,
-	.llseek         = seq_lseek,
+	.llseek         = tracing_lseek,
 	.release        = recursed_function_release,
 };
 
-- 
2.53.0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [for-linus][PATCH 2/8] tracing/filters: Fix glob matching on zero size string fields
       [not found] <20261203141725.845506016@kernel.org>
  2026-12-03 14:17 ` [for-linus][PATCH 1/8] tracing: Fix crash on lseek of recursed_functions file in WRONLY mode Steven Rostedt
@ 2026-12-03 14:17 ` Steven Rostedt
  2026-12-03 14:17 ` [for-linus][PATCH 3/8] ring-buffer: Use irqsave for the reader lock in ring_buffer_poll_remote Steven Rostedt
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: Steven Rostedt @ 2026-12-03 14:17 UTC (permalink / raw)
  To: linux-kernel
  Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
	stable, David Carlier

From: David Carlier <devnexen@gmail.com>

The "buf" field of the ftrace print event is a char[] of size zero,
and filter_pred_string() passes that size as the length. The regex
callbacks take a zero length as a NUL-terminated string of unknown
length, but since regex_match_glob() was switched to glob_match_len()
it treats it as an empty string, so a glob never matches:

  # echo 'snapshot if buf ~ "st*r*"' > events/ftrace/print/trigger
  # echo start > trace_marker

Use glob_match() when the length is zero.

Cc: stable@vger.kernel.org
Fixes: 0a6070839b1e ("tracing: Prevent out-of-bounds read in glob matching")
Link: https://patch.msgid.link/20261126215220.1205315-1-devnexen@gmail.com
Signed-off-by: David Carlier <devnexen@gmail.com>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
 kernel/trace/trace_events_filter.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c
index b3ac2519a62d..8023c83e0414 100644
--- a/kernel/trace/trace_events_filter.c
+++ b/kernel/trace/trace_events_filter.c
@@ -1118,6 +1118,10 @@ static int regex_match_end_dynstr(char *str, struct regex *r, int len)
 
 static int regex_match_glob(char *str, struct regex *r, int len)
 {
+	/* Zero is reserved for a NUL-terminated string of unknown length. */
+	if (!len)
+		return glob_match(r->pattern, str);
+
 	return glob_match_len(r->pattern, str, len) ? 1 : 0;
 }
 
-- 
2.53.0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [for-linus][PATCH 3/8] ring-buffer: Use irqsave for the reader lock in ring_buffer_poll_remote
       [not found] <20261203141725.845506016@kernel.org>
  2026-12-03 14:17 ` [for-linus][PATCH 1/8] tracing: Fix crash on lseek of recursed_functions file in WRONLY mode Steven Rostedt
  2026-12-03 14:17 ` [for-linus][PATCH 2/8] tracing/filters: Fix glob matching on zero size string fields Steven Rostedt
@ 2026-12-03 14:17 ` Steven Rostedt
  2026-12-03 14:17 ` [for-linus][PATCH 4/8] ring-buffer: Avoid reader_lock deadlock during panic Steven Rostedt
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: Steven Rostedt @ 2026-12-03 14:17 UTC (permalink / raw)
  To: linux-kernel
  Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
	stable, Sashiko, Vincent Donnefort

From: Vincent Donnefort <vdonnefort@google.com>

Calling rb_wakeups with the reader lock held but interrupts enabled can
lead to a deadlock: the irq_work might run on the same CPU, but will
block when acquiring that very same reader spinlock.

First, rb_wakeups does not even need to be called under the reader lock.
Move the function outside of the reader lock scope.

Second, the reader lock must be acquired with IRQs disabled anyway. Use
the irqsave variant of the spinlock.

Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20261130152335.2304881-2-vdonnefort@google.com
Fixes: 2e67fabd8b77 ("ring-buffer: Introduce ring-buffer remotes")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260602171146.2238998-1-vdonnefort%40google.com # patch 8
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
 kernel/trace/ring_buffer.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 65dddfac5e04..ffc1d0f08f17 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -6929,14 +6929,19 @@ int ring_buffer_poll_remote(struct trace_buffer *buffer, int cpu)
 	struct ring_buffer_per_cpu *cpu_buffer;
 
 	if (cpu != RING_BUFFER_ALL_CPUS) {
+		bool wakeup;
+
 		if (!cpumask_test_cpu(cpu, buffer->cpumask))
 			return -EINVAL;
 
 		cpu_buffer = buffer->buffers[cpu];
 
-		guard(raw_spinlock)(&cpu_buffer->reader_lock);
-		rb_read_remote_meta_page(cpu_buffer);
-		if (rb_num_of_entries(cpu_buffer))
+		scoped_guard(raw_spinlock_irqsave, &cpu_buffer->reader_lock) {
+			rb_read_remote_meta_page(cpu_buffer);
+			wakeup = rb_num_of_entries(cpu_buffer);
+		}
+
+		if (wakeup)
 			rb_wakeups(buffer, cpu_buffer);
 
 		return 0;
@@ -6951,7 +6956,7 @@ int ring_buffer_poll_remote(struct trace_buffer *buffer, int cpu)
 	for_each_buffer_cpu(buffer, cpu) {
 		cpu_buffer = buffer->buffers[cpu];
 
-		guard(raw_spinlock)(&cpu_buffer->reader_lock);
+		guard(raw_spinlock_irqsave)(&cpu_buffer->reader_lock);
 		rb_read_remote_meta_page(cpu_buffer);
 	}
 
-- 
2.53.0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [for-linus][PATCH 4/8] ring-buffer: Avoid reader_lock deadlock during panic
       [not found] <20261203141725.845506016@kernel.org>
                   ` (2 preceding siblings ...)
  2026-12-03 14:17 ` [for-linus][PATCH 3/8] ring-buffer: Use irqsave for the reader lock in ring_buffer_poll_remote Steven Rostedt
@ 2026-12-03 14:17 ` Steven Rostedt
  2026-12-03 14:17 ` [for-linus][PATCH 5/8] tracing/histograms: Reject non-string fields for synthetic string fields Steven Rostedt
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: Steven Rostedt @ 2026-12-03 14:17 UTC (permalink / raw)
  To: linux-kernel
  Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
	stable, Sashiko, Vincent Donnefort

From: Vincent Donnefort <vdonnefort@google.com>

Although reader_lock is taken with IRQs disabled, panic shutdown can
stop CPUs with NMIs (most likely if the first attempt with maskable
interrupts failed). Those NMIs may stop a CPU while it holds the
reader_lock. Use trylock when panic_on_this_cpu() just like in_nmi().

Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20261130152335.2304881-3-vdonnefort@google.com
Fixes: 289a5a25c5d0 ("ring-buffer: Give NMIs a chance to lock the reader_lock")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/linux-trace-kernel/20260924151509.23AEE1F000FF@smtp.kernel.org/
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
 kernel/trace/ring_buffer.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index ffc1d0f08f17..bdd010fa5cf7 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -22,6 +22,7 @@
 #include <linux/kthread.h>	/* for self test */
 #include <linux/module.h>
 #include <linux/percpu.h>
+#include <linux/panic.h>
 #include <linux/mutex.h>
 #include <linux/delay.h>
 #include <linux/slab.h>
@@ -6324,7 +6325,7 @@ EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
 
 static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
 {
-	if (likely(!in_nmi())) {
+	if (likely(!in_nmi() && !panic_on_this_cpu())) {
 		raw_spin_lock(&cpu_buffer->reader_lock);
 		return true;
 	}
-- 
2.53.0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [for-linus][PATCH 5/8] tracing/histograms: Reject non-string fields for synthetic string fields
       [not found] <20261203141725.845506016@kernel.org>
                   ` (3 preceding siblings ...)
  2026-12-03 14:17 ` [for-linus][PATCH 4/8] ring-buffer: Avoid reader_lock deadlock during panic Steven Rostedt
@ 2026-12-03 14:17 ` Steven Rostedt
  2026-12-03 14:17 ` [for-linus][PATCH 6/8] tracing: Free all event filters after RCU synchronization Steven Rostedt
  2026-12-03 14:17 ` [for-linus][PATCH 8/8] tracing: Reject backup instance opens only after autoremove is queued Steven Rostedt
  6 siblings, 0 replies; 7+ messages in thread
From: Steven Rostedt @ 2026-12-03 14:17 UTC (permalink / raw)
  To: linux-kernel
  Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
	stable, Lee Jones

From: Lee Jones <lee@kernel.org>

In check_synth_field(), when a synthetic event field is a string
(field->is_string) and the histogram field does not match the "char["
dynamic string shortcut, validation falls through to the generic size
comparison (which also skips signedness checks for string fields).

Because a dynamic string synthetic field ("__data_loc char[]") has a
size of sizeof(u64), any 8-byte scalar histogram field, constant, or
pointer field (including FILTER_PTR_STRING fields that
create_hist_field() intentionally excludes from HIST_FIELD_FL_STRING)
passes the fallback check.  Its scalar value is then dereferenced as a
string pointer in get_field_size() and trace_string().  Likewise, an
8-byte scalar can match a char[8] static string synthetic field, or a
char[8] static string field can match an 8-byte scalar synthetic field
and leak a kernel pointer address.

Propagate HIST_FIELD_FL_STRING to variable references and field
variables (matching HIST_FIELD_FL_STACKTRACE, with variable aliases
already excluded from per-element string capture), and reject any
mismatch between HIST_FIELD_FL_STRING and field->is_string in
check_synth_field().

Cc: stable@vger.kernel.org
Fixes: b05e89ae7cf3 ("tracing: Accept different type for synthetic event fields")
Link: https://patch.msgid.link/20261201142918.1849179-2-lee@kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
 kernel/trace/trace_events_hist.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index d4702ad4e003..7f5ec9ff19e5 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -2138,7 +2138,7 @@ static int init_var_ref(struct hist_field *ref_field,
 	ref_field->is_signed = var_field->is_signed;
 	ref_field->flags |= var_field->flags &
 		(HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS |
-		 HIST_FIELD_FL_STACKTRACE);
+		 HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE);
 
 	if (system) {
 		ref_field->system = kstrdup(system, GFP_KERNEL);
@@ -3331,7 +3331,8 @@ static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
 		ret = PTR_ERR(var);
 		goto err;
 	}
-	var->flags |= val->flags & HIST_FIELD_FL_STACKTRACE;
+	var->flags |= val->flags &
+		(HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE);
 
 	field_var = kzalloc_obj(struct field_var);
 	if (!field_var) {
@@ -3951,13 +3952,15 @@ static int check_synth_field(struct synth_event *event,
 
 	field = event->fields[field_pos];
 
+	if (!!(hist_field->flags & HIST_FIELD_FL_STRING) != field->is_string)
+		return -EINVAL;
+
 	/*
 	 * A dynamic string synth field can accept static or
 	 * dynamic. A static string synth field can only accept a
 	 * same-sized static string, which is checked for later.
 	 */
-	if (strstr(hist_field->type, "char[") && field->is_string
-	    && field->is_dynamic)
+	if (field->is_string && field->is_dynamic)
 		return 0;
 
 	if (!!(hist_field->flags & HIST_FIELD_FL_STACKTRACE) != field->is_stack)
-- 
2.53.0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [for-linus][PATCH 6/8] tracing: Free all event filters after RCU synchronization
       [not found] <20261203141725.845506016@kernel.org>
                   ` (4 preceding siblings ...)
  2026-12-03 14:17 ` [for-linus][PATCH 5/8] tracing/histograms: Reject non-string fields for synthetic string fields Steven Rostedt
@ 2026-12-03 14:17 ` Steven Rostedt
  2026-12-03 14:17 ` [for-linus][PATCH 8/8] tracing: Reject backup instance opens only after autoremove is queued Steven Rostedt
  6 siblings, 0 replies; 7+ messages in thread
From: Steven Rostedt @ 2026-12-03 14:17 UTC (permalink / raw)
  To: linux-kernel
  Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
	stable, Antoine Tenart

From: Steven Rostedt <rostedt@goodmis.org>

The global functions free_event_filter() and ftrace_profile_free_filter()
do not wait the full tracepoint synchronization needed before freeing a
filter. That is, if a tracepoint was activated and disabled, there could
still be an in-flight reading of the tracepoint that accesses the filter.
If the filter is freed before that RCU synchronization, it could cause a
use-after-free bug.

Most of the filter freeing code uses a delay_free_filter() call or uses a
try_delay_free_filter() to wait for the synchronization period to finish
before freeing the filters.

The try_delay_free_filter() allocates a temporary list object to use to
hold the filters to attach to the RCU and work queue callbacks. If the
allocation fails, it simply calls the trace RCU synchronization needed in
the function (causing the caller to wait).

Since filters are only created when used there's no reason to create a
separate descriptor to hold the RCU and work queue fields. Just add them
to the struct event_filter structure and then use the filter itself to
add it to the RCU lists for freeing. This means that the
try_delay_free_filter() call will always succeed.

Instead of having a separate item and list head descriptor for the freeing
of the filters, the event_filter itself is used. For freeing a list of
filters, use the first one as the list_head and attach the rest to that
filter and send that first one to the RCU function callbacks.

The try_delay_free_filter() is replaced with a free_filter_init() that
will initialize the filter->list to be an empty list and send it through
the delay logic.

Now update the free_event_filter() and the ftrace_profile_free_filter()
functions to use the new free_filter_init() so that their filters wait the
necessary duration before being freed. This also removes a lot of code and
makes it simpler.

Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20261201155010.3f9d268d@gandalf.local.home
Fixes: a9d0aab5eb33 ("tracing: Fix regression of filter waiting a long time on RCU synchronization")
Reported-by: Antoine Tenart <atenart@kernel.org>
Tested-by: Antoine Tenart <atenart@kernel.org>
Link: https://lore.kernel.org/linux-trace-kernel/20261126132830.14992-1-atenart@kernel.org/
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
 kernel/trace/trace.h               |   5 +
 kernel/trace/trace_events_filter.c | 153 +++++++++--------------------
 2 files changed, 54 insertions(+), 104 deletions(-)

diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
index 32997b58a8a0..45971e4be77e 100644
--- a/kernel/trace/trace.h
+++ b/kernel/trace/trace.h
@@ -1588,6 +1588,11 @@ struct prog_entry;
 struct event_filter {
 	struct prog_entry __rcu	*prog;
 	char			*filter_string;
+	struct list_head	list;
+	union {
+		struct rcu_head		rcu;
+		struct rcu_work		rwork;
+	};
 };
 
 struct event_subsystem {
diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c
index 8023c83e0414..0666d12c2afd 100644
--- a/kernel/trace/trace_events_filter.c
+++ b/kernel/trace/trace_events_filter.c
@@ -1367,11 +1367,6 @@ static void __free_filter(struct event_filter *filter)
 	kfree(filter);
 }
 
-void free_event_filter(struct event_filter *filter)
-{
-	__free_filter(filter);
-}
-
 static inline void __remove_filter(struct trace_event_file *file)
 {
 	filter_disable(file);
@@ -1390,45 +1385,31 @@ static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
 	}
 }
 
-struct filter_list {
-	struct list_head	list;
-	struct event_filter	*filter;
-};
-
-struct filter_head {
-	struct list_head	list;
-	union {
-		struct rcu_head		rcu;
-		struct rcu_work		rwork;
-	};
-};
-
-static void free_filter_list(struct filter_head *filter_list)
+static void free_filter_list(struct event_filter *filter_list)
 {
-	struct filter_list *filter_item, *tmp;
+	struct event_filter *filter_item, *tmp;
 
 	list_for_each_entry_safe(filter_item, tmp, &filter_list->list, list) {
-		__free_filter(filter_item->filter);
 		list_del(&filter_item->list);
-		kfree(filter_item);
+		__free_filter(filter_item);
 	}
-	kfree(filter_list);
+	__free_filter(filter_list);
 }
 
 static void free_filter_list_work(struct work_struct *work)
 {
-	struct filter_head *filter_list;
+	struct event_filter *filter_list;
 
-	filter_list = container_of(to_rcu_work(work), struct filter_head, rwork);
+	filter_list = container_of(to_rcu_work(work), struct event_filter, rwork);
 	free_filter_list(filter_list);
 }
 
 static void free_filter_list_tasks(struct rcu_head *rhp)
 {
-	struct filter_head *filter_list = container_of(rhp, struct filter_head, rcu);
+	struct event_filter *filter = container_of(rhp, struct event_filter, rcu);
 
-	INIT_RCU_WORK(&filter_list->rwork, free_filter_list_work);
-	queue_rcu_work(system_dfl_wq, &filter_list->rwork);
+	INIT_RCU_WORK(&filter->rwork, free_filter_list_work);
+	queue_rcu_work(system_dfl_wq, &filter->rwork);
 }
 
 /*
@@ -1437,43 +1418,25 @@ static void free_filter_list_tasks(struct rcu_head *rhp)
  * Instead of waiting for it, simply call these via the call_rcu*()
  * variants.
  */
-static void delay_free_filter(struct filter_head *head)
+static void delay_free_filter(struct event_filter *filter)
 {
-	call_rcu_tasks_trace(&head->rcu, free_filter_list_tasks);
+	if (!filter)
+		return;
+	call_rcu_tasks_trace(&filter->rcu, free_filter_list_tasks);
 }
 
-static void try_delay_free_filter(struct event_filter *filter)
+static void free_filter_init(struct event_filter *filter)
 {
-	struct filter_head *head;
-	struct filter_list *item;
-
-	head = kmalloc_obj(*head);
-	if (!head)
-		goto free_now;
-
-	INIT_LIST_HEAD(&head->list);
-
-	item = kmalloc_obj(*item);
-	if (!item) {
-		kfree(head);
-		goto free_now;
-	}
-
-	item->filter = filter;
-	list_add_tail(&item->list, &head->list);
-	delay_free_filter(head);
-	return;
+	if (!filter)
+		return;
 
- free_now:
-	/* Make sure the filter is not being used */
-	tracepoint_synchronize_unregister();
-	__free_filter(filter);
+	INIT_LIST_HEAD(&filter->list);
+	delay_free_filter(filter);
 }
 
-static inline void __free_subsystem_filter(struct trace_event_file *file)
+void free_event_filter(struct event_filter *filter)
 {
-	__free_filter(event_filter(file));
-	file->filter = NULL;
+	free_filter_init(filter);
 }
 
 static inline void event_set_filter(struct trace_event_file *file,
@@ -1492,47 +1455,33 @@ static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
 					  struct event_filter *filter)
 {
 	struct trace_event_file *file;
-	struct filter_head *head;
-	struct filter_list *item;
-
-	head = kmalloc_obj(*head);
-	if (!head)
-		goto free_now;
+	struct event_filter *filter_list = NULL;
+	struct event_filter *efilter;
 
-	INIT_LIST_HEAD(&head->list);
+	if (filter) {
+		filter_list = filter;
+		INIT_LIST_HEAD(&filter->list);
+	}
 
 	list_for_each_entry(file, &tr->events, list) {
 		if (file->system != dir)
 			continue;
-		item = kmalloc_obj(*item);
-		if (!item)
-			goto free_now;
-		item->filter = event_filter(file);
-		list_add_tail(&item->list, &head->list);
-		event_clear_filter(file);
-	}
-
-	item = kmalloc_obj(*item);
-	if (!item)
-		goto free_now;
-
-	item->filter = filter;
-	list_add_tail(&item->list, &head->list);
 
-	delay_free_filter(head);
-	return;
- free_now:
-	tracepoint_synchronize_unregister();
+		efilter = event_filter(file);
+		if (!efilter)
+			continue;
 
-	if (head)
-		free_filter_list(head);
+		if (!filter_list) {
+			filter_list = efilter;
+			INIT_LIST_HEAD(&efilter->list);
+		} else {
+			list_add_tail(&efilter->list, &filter_list->list);
+		}
 
-	list_for_each_entry(file, &tr->events, list) {
-		if (file->system != dir || !file->filter)
-			continue;
-		__free_subsystem_filter(file);
+		event_clear_filter(file);
 	}
-	__free_filter(filter);
+
+	delay_free_filter(filter_list);
 }
 
 int filter_assign_type(const char *type)
@@ -2341,14 +2290,14 @@ static int process_system_preds(struct trace_subsystem_dir *dir,
 				char *filter_string)
 {
 	struct trace_event_file *file;
-	struct filter_list *filter_item;
 	struct event_filter *filter = NULL;
-	struct filter_head *filter_list;
+	struct event_filter *filter_list;
+	struct event_filter *efilter;
 	bool fail = true;
 	int ret = -ENOMEM;
 	int err;
 
-	filter_list = kmalloc_obj(*filter_list);
+	filter_list = kzalloc_obj(*filter_list);
 	if (!filter_list)
 		return -ENOMEM;
 
@@ -2377,18 +2326,16 @@ static int process_system_preds(struct trace_subsystem_dir *dir,
 			fail = false;
 		}
 
-		filter_item = kzalloc_obj(*filter_item);
-		if (!filter_item)
-			goto fail_mem;
-
-		list_add_tail(&filter_item->list, &filter_list->list);
 		/*
 		 * Regardless of if this returned an error, we still
 		 * replace the filter for the call.
 		 */
-		filter_item->filter = event_filter(file);
+		efilter = event_filter(file);
 		event_set_filter(file, filter);
 		filter = NULL;
+
+		if (efilter)
+			list_add_tail(&efilter->list, &filter_list->list);
 	}
 
 	if (fail)
@@ -2555,7 +2502,7 @@ int apply_event_filter(struct trace_event_file *file, char *filter_string)
 
 		event_clear_filter(file);
 
-		try_delay_free_filter(filter);
+		free_filter_init(filter);
 
 		return 0;
 	}
@@ -2578,9 +2525,7 @@ int apply_event_filter(struct trace_event_file *file, char *filter_string)
 			filter_disable(file);
 
 		event_set_filter(file, filter);
-
-		if (tmp)
-			try_delay_free_filter(tmp);
+		free_filter_init(tmp);
 	}
 
 	return err;
@@ -2630,7 +2575,7 @@ void ftrace_profile_free_filter(struct perf_event *event)
 	struct event_filter *filter = event->filter;
 
 	event->filter = NULL;
-	__free_filter(filter);
+	free_filter_init(filter);
 }
 
 struct function_filter_data {
@@ -3073,7 +3018,7 @@ static __init int ftrace_test_event_filter(void)
 
 		mutex_unlock(&event_mutex);
 
-		__free_filter(filter);
+		free_filter_init(filter);
 
 		if (test_pred_visited) {
 			printk(KERN_INFO
-- 
2.53.0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [for-linus][PATCH 8/8] tracing: Reject backup instance opens only after autoremove is queued
       [not found] <20261203141725.845506016@kernel.org>
                   ` (5 preceding siblings ...)
  2026-12-03 14:17 ` [for-linus][PATCH 6/8] tracing: Free all event filters after RCU synchronization Steven Rostedt
@ 2026-12-03 14:17 ` Steven Rostedt
  6 siblings, 0 replies; 7+ messages in thread
From: Steven Rostedt @ 2026-12-03 14:17 UTC (permalink / raw)
  To: linux-kernel
  Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
	stable

From: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>

Commit eca33fdab47b ("tracing: Remove the backup instance automatically
after read") sets tr->free_on_close in update_last_data() once all
previous-boot data has been read out or cleared, and queues
autoremove_work in __trace_array_put() when the last user reference is
dropped (tr->ref == 1). However, __trace_array_get() rejects new
references with -ENODEV as soon as tr->free_on_close is set, even while
existing file descriptors still hold tr->ref > 1 and autoremove has not
yet been queued.

This breaks userspace consumers that keep multiple file descriptors open
on the backup instance while finishing a dump. For example, a reader
that keeps per_cpu/cpu*/trace_pipe_raw open across CPUs and drains them
via read() triggers update_last_data_if_empty() on the final empty read,
setting tr->free_on_close while those pipe file descriptors are still
open. Any subsequent open of another file in the same instance before
closing the pipes -- such as reading per_cpu/cpu*/stats or opening trace
with O_WRONLY | O_TRUNC to clear the buffer -- then fails with -ENODEV.

Introduce a tr->autoremove_queued flag set when autoremove_work is
queued, and check it in __trace_array_get() instead of tr->free_on_close.
This allows opening files while existing references remain, while still
blocking new opens once autoremove is queued.

Cc: stable@vger.kernel.org
Fixes: eca33fdab47b ("tracing: Remove the backup instance automatically after read")
Link: https://patch.msgid.link/179622393332.9100.10906884385392450114.stgit@mhiramat.c.googlers.com
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
 kernel/trace/trace.c | 12 +++++++++---
 kernel/trace/trace.h |  4 ++++
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index fa65ef8d7fbe..f153ffedb9d2 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -585,8 +585,12 @@ static struct workqueue_struct *autoremove_wq;
 
 static void trace_array_kick_autoremove(struct trace_array *tr)
 {
-	if (autoremove_wq)
+	lockdep_assert_held(&trace_types_lock);
+
+	if (autoremove_wq && !tr->autoremove_queued) {
+		tr->autoremove_queued = true;
 		queue_work(autoremove_wq, &tr->autoremove_work);
+	}
 }
 
 static void trace_array_cancel_autoremove(struct trace_array *tr)
@@ -619,8 +623,10 @@ LIST_HEAD(ftrace_trace_arrays);
 
 static int __trace_array_get(struct trace_array *this_tr)
 {
-	/* When free_on_close is set, this is not available anymore. */
-	if (autoremove_wq && this_tr->free_on_close)
+	lockdep_assert_held(&trace_types_lock);
+
+	/* Once autoremove is queued, this is not available anymore. */
+	if (this_tr->autoremove_queued)
 		return -ENODEV;
 
 	this_tr->ref++;
diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
index 45971e4be77e..0211db62d6a1 100644
--- a/kernel/trace/trace.h
+++ b/kernel/trace/trace.h
@@ -462,8 +462,12 @@ struct trace_array {
 	/*
 	 * If the ring buffer is a read only backup instance, it will be
 	 * removed after dumping all data via pipe, because no readable data.
+	 * @free_on_close is set once the buffer is drained or cleared, and
+	 * @autoremove_queued is set when the last reference is released and
+	 * @autoremove_work is queued.
 	 */
 	bool free_on_close;
+	bool autoremove_queued;
 	struct work_struct	autoremove_work;
 };
 
-- 
2.53.0

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-03 14:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261203141725.845506016@kernel.org>
2026-12-03 14:17 ` [for-linus][PATCH 1/8] tracing: Fix crash on lseek of recursed_functions file in WRONLY mode Steven Rostedt
2026-12-03 14:17 ` [for-linus][PATCH 2/8] tracing/filters: Fix glob matching on zero size string fields Steven Rostedt
2026-12-03 14:17 ` [for-linus][PATCH 3/8] ring-buffer: Use irqsave for the reader lock in ring_buffer_poll_remote Steven Rostedt
2026-12-03 14:17 ` [for-linus][PATCH 4/8] ring-buffer: Avoid reader_lock deadlock during panic Steven Rostedt
2026-12-03 14:17 ` [for-linus][PATCH 5/8] tracing/histograms: Reject non-string fields for synthetic string fields Steven Rostedt
2026-12-03 14:17 ` [for-linus][PATCH 6/8] tracing: Free all event filters after RCU synchronization Steven Rostedt
2026-12-03 14:17 ` [for-linus][PATCH 8/8] tracing: Reject backup instance opens only after autoremove is queued Steven Rostedt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox