tracing fixes for v7.3:

- Fix error output of boot instance creation failure
 
   Currently if a boot instance creation fails, instead of printing out the
   name of the instance that failed, it prints "(null)". That is because it
   prints "cur_str" that had already been processed by strsep(). Print the
   saved name instead.
 
   While at it, print the error code of the failure.
 
 - Fix use-after-free for same named historgrams
 
   Histograms can be named so that they can be used in multiple events. But
   if the named histogram has a variable attached, the second event that uses
   the named histogram which duplicates it and needs to free the original
   after duplication leaves the old variable in place and still visible. If
   another histogram uses than variable, it will use the stale one which will
   try to reference the freed duplicate histogram and crash the kernel.
 
   Free the duplicate variables along with the duplicated histogram data.
 
 - Check return value of kthread_run() in event self test
 
   The events self tests uses a kthread for testing but does not check if it
   succeeded in creating a kthread. If the kthread creation were to fail, the
   code will still try to call kthread_stop() on the error returned.
 
 - Fix race between reading trace_pipe and updating subbuffer size
 
   If a user is reading the trace_pipe file at the same time they update the
   ring buffer sub-buffer size, can cause the trace_pipe read to read stale
   data. Add trace_access_lock() around updating the ring buffer sub-buffer
   size.
 
 - Fix eventfs_inode on failure path in creation of the events directory
 
   In the creation of the "events" directory, if after allocating the
   eventfs_inode a failure is detected, it calls cleanup_ei() which calls
   free_ei(). The free_ei() will test if eventfs_inode being freed has no
   children. It is a bug if it does. But on the failure case of the creation
   of the "events" directory, the children lists have not yet been
   initialized and the free will trigger a warning because list_empty() on an
   uninitialized list returns false.
 
   Move the initialization into init_ei() where it makes more sense and makes
   sure that a created eventfs_inode has its lists initialized upon creation.
 
 - Check return value of kthread_run() in ftrace direct sample code
 
   The sample code that shows how to use the ftrace direct calls does not
   test the return of kthread_run() to see if it succeeds. Return a failure
   if the kthread_run() doesn't succeed.
 
 - Clear user events state on fork in case of alloc failure
 
   On fork, the child gets a pointer to the parent's user events state. It
   makes a copy of it then updates the child's pointer to it. But if the
   allocation fails, the duplication function leaves the child with a pointer
   to its parent's descriptor. When the child cleans up its data, it will free
   the parent's descriptor while the parent is still using it.
 
   In the duplication function, set the child's user_event_mm to NULL before
   testing if the allocation succeeded, and when it exits it will not free
   the parent's descriptor.
 
 - Fix retry exhaustion in simple ring buffer reader swap
 
   simple_ring_buffer_swap_reader_page() starts with retry set to 8 and
   post-decrements it only after a failed link replacement. On the final
   attempt, a successful replacement leaves retry at zero, while a failed
   replacement leaves it at -1.
 
   But the check for success expects the retry value to be non-zero and exits
   with an error on zero. This is the opposite result. Fix it.
 
 - Fail nicely when the remote swap_reader_page() returns an error
 
   Currently, if the swap_reader_page() of a remote buffer fails, it triggers
   a WARN_ON_ONCE() and continues normally. Instead, have it exit with an
   error and a pr_warn() print instead of a full WARNING.
 -----BEGIN PGP SIGNATURE-----
 
 iIoEABYKADIWIQRRSw7ePDh/lE+zeZMp5XQQmuv6qgUCapOC3hQccm9zdGVkdEBn
 b29kbWlzLm9yZwAKCRAp5XQQmuv6qvjkAQCGVuyK980rwiBnfenWLpeB3QjfHA8B
 mV0mJSlGWm1t1gEA9WWzMGbp+OHeRV2xyA+xW7OS1S58VO9OIGrzXCGqbAM=
 =TrF5
 -----END PGP SIGNATURE-----

Merge tag 'trace-v7.3-2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace

Pull tracing fixes from Steven Rostedt:

 - Fix error output of boot instance creation failure

   Currently if a boot instance creation fails, instead of printing out
   the name of the instance that failed, it prints "(null)". That is
   because it prints "cur_str" that had already been processed by
   strsep(). Print the saved name instead.

   While at it, print the error code of the failure.

 - Fix use-after-free for same named historgrams

   Histograms can be named so that they can be used in multiple events.
   But if the named histogram has a variable attached, the second event
   that uses the named histogram which duplicates it and needs to free
   the original after duplication leaves the old variable in place and
   still visible. If another histogram uses than variable, it will use
   the stale one which will try to reference the freed duplicate
   histogram and crash the kernel.

   Free the duplicate variables along with the duplicated histogram
   data.

 - Check return value of kthread_run() in event self test

   The events self tests uses a kthread for testing but does not check
   if it succeeded in creating a kthread. If the kthread creation were
   to fail, the code will still try to call kthread_stop() on the error
   returned.

 - Fix race between reading trace_pipe and updating subbuffer size

   If a user is reading the trace_pipe file at the same time they update
   the ring buffer sub-buffer size, can cause the trace_pipe read to
   read stale data. Add trace_access_lock() around updating the ring
   buffer sub-buffer size.

 - Fix eventfs_inode on failure path in creation of the events directory

   In the creation of the "events" directory, if after allocating the
   eventfs_inode a failure is detected, it calls cleanup_ei() which
   calls free_ei(). The free_ei() will test if eventfs_inode being freed
   has no children. It is a bug if it does. But on the failure case of
   the creation of the "events" directory, the children lists have not
   yet been initialized and the free will trigger a warning because
   list_empty() on an uninitialized list returns false.

   Move the initialization into init_ei() where it makes more sense and
   makes sure that a created eventfs_inode has its lists initialized
   upon creation.

 - Check return value of kthread_run() in ftrace direct sample code

   The sample code that shows how to use the ftrace direct calls does
   not test the return of kthread_run() to see if it succeeds. Return a
   failure if the kthread_run() doesn't succeed.

 - Clear user events state on fork in case of alloc failure

   On fork, the child gets a pointer to the parent's user events state.
   It makes a copy of it then updates the child's pointer to it. But if
   the allocation fails, the duplication function leaves the child with
   a pointer to its parent's descriptor. When the child cleans up its
   data, it will free the parent's descriptor while the parent is still
   using it.

   In the duplication function, set the child's user_event_mm to NULL
   before testing if the allocation succeeded, and when it exits it will
   not free the parent's descriptor.

 - Fix retry exhaustion in simple ring buffer reader swap

   simple_ring_buffer_swap_reader_page() starts with retry set to 8 and
   post-decrements it only after a failed link replacement. On the final
   attempt, a successful replacement leaves retry at zero, while a
   failed replacement leaves it at -1.

   But the check for success expects the retry value to be non-zero and
   exits with an error on zero. This is the opposite result. Fix it.

 - Fail nicely when the remote swap_reader_page() returns an error

   Currently, if the swap_reader_page() of a remote buffer fails, it
   triggers a WARN_ON_ONCE() and continues normally. Instead, have it
   exit with an error and a pr_warn() print instead of a full WARNING.

* tag 'trace-v7.3-2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace:
  ring-buffer: Stop remote reader update when page swap fails
  tracing: Fix retry exhaustion in simple ring buffer reader swap
  tracing/user_events: Clear copied tracing state before fork duplication
  samples/ftrace: Fix kthread_stop() on ERR_PTR in ftrace-direct-multi-modify
  samples/ftrace: Fix kthread_stop() on ERR_PTR in ftrace-direct-modify
  eventfs: Initialize ei->children and ei->list in init_ei()
  tracing: Fix use-after-free in trace_pipe read on sub-buffer order change
  tracing: Fix crash passing ERR_PTR to kthread_stop()
  tracing: Fix use-after-free with same-name named triggers
  tracing: Fix logged instance name on creation failure
This commit is contained in:
Linus Torvalds 2026-08-30 09:22:00 -07:00
commit 034dd340b0
9 changed files with 40 additions and 17 deletions

View File

@ -438,6 +438,8 @@ static inline struct eventfs_inode *init_ei(struct eventfs_inode *ei, const char
if (!ei->name)
return NULL;
kref_init(&ei->kref);
INIT_LIST_HEAD(&ei->children);
INIT_LIST_HEAD(&ei->list);
return ei;
}
@ -729,8 +731,6 @@ struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode
ei->entries = entries;
ei->nr_entries = size;
ei->data = data;
INIT_LIST_HEAD(&ei->children);
INIT_LIST_HEAD(&ei->list);
scoped_guard(mutex, &eventfs_mutex) {
if (!parent->is_freed)
@ -802,9 +802,6 @@ struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry
ei->attr.uid = uid;
ei->attr.gid = gid;
INIT_LIST_HEAD(&ei->children);
INIT_LIST_HEAD(&ei->list);
ti = get_tracefs(inode);
ti->flags |= TRACEFS_EVENT_INODE;
ti->private = ei;

View File

@ -5805,8 +5805,11 @@ __rb_get_reader_page_from_remote(struct ring_buffer_per_cpu *cpu_buffer)
prev_reader = cpu_buffer->subbuf_ids[cpu_buffer->meta_page->reader.id];
WARN_ON_ONCE(cpu_buffer->remote->swap_reader_page(cpu_buffer->cpu,
cpu_buffer->remote->priv));
if (cpu_buffer->remote->swap_reader_page(cpu_buffer->cpu,
cpu_buffer->remote->priv)) {
pr_warn_ratelimited("Remote reader page swap failed\n");
return NULL;
}
/* nr_pages doesn't include the reader page */
if (WARN_ON_ONCE(cpu_buffer->meta_page->reader.id > cpu_buffer->nr_pages))
return NULL;

View File

@ -160,8 +160,8 @@ int simple_ring_buffer_swap_reader_page(struct simple_rb_per_cpu *cpu_buffer)
overrun = cpu_buffer->meta->overrun;
} while (!simple_bpage_unset_head_link(last, reader, SIMPLE_RB_LINK_NORMAL) && retry--);
if (!retry)
return -EINVAL;
if (retry < 0)
return -EBUSY;
cpu_buffer->head_page = simple_bpage_from_link(reader->link.next);
cpu_buffer->head_page->link.prev = &reader->link;

View File

@ -8216,6 +8216,8 @@ buffer_subbuf_size_write(struct file *filp, const char __user *ubuf,
/* Do not allow tracing while changing the order of the ring buffer */
tracing_stop_tr(tr);
trace_access_lock(RING_BUFFER_ALL_CPUS);
old_order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
if (old_order == order)
goto out;
@ -8255,6 +8257,7 @@ buffer_subbuf_size_write(struct file *filp, const char __user *ubuf,
#endif
(*ppos)++;
out:
trace_access_unlock(RING_BUFFER_ALL_CPUS);
if (ret)
cnt = ret;
tracing_start_tr(tr);
@ -9728,7 +9731,8 @@ __init static void enable_instances(void)
tr = trace_array_create_systems(name, NULL, addr, size);
if (IS_ERR(tr)) {
pr_warn("Tracing: Failed to create instance buffer %s\n", curr_str);
pr_warn("Tracing: Failed to create instance buffer '%s' (%ld)\n", name,
PTR_ERR(tr));
continue;
}

View File

@ -5019,6 +5019,8 @@ static __init void event_test_stuff(void)
struct task_struct *test_thread;
test_thread = kthread_run(event_test_thread, NULL, "test-events");
if (WARN_ON(IS_ERR(test_thread)))
return;
msleep(1);
kthread_stop(test_thread);
}

View File

@ -6661,8 +6661,10 @@ static int hist_register_trigger(char *glob,
tracing_set_filter_buffering(file->tr, true);
}
if (named_data)
if (named_data) {
remove_hist_vars(hist_data);
destroy_hist_data(hist_data);
}
out:
return ret;
}

View File

@ -868,6 +868,9 @@ void user_event_mm_dup(struct task_struct *t, struct user_event_mm *old_mm)
struct user_event_mm *mm = user_event_mm_alloc(t);
struct user_event_enabler *enabler;
/* On failure, do not free parent's copy */
t->user_event_mm = NULL;
if (!mm)
return;

View File

@ -320,9 +320,15 @@ static int __init ftrace_direct_init(void)
ftrace_set_filter_ip(&direct, (unsigned long) my_ip, 0, 0);
ret = register_ftrace_direct(&direct, my_tramp);
if (!ret)
simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
return ret;
if (ret)
return ret;
simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
if (IS_ERR(simple_tsk)) {
unregister_ftrace_direct(&direct, my_tramp, true);
return PTR_ERR(simple_tsk);
}
return 0;
}
static void __exit ftrace_direct_exit(void)

View File

@ -364,9 +364,15 @@ static int __init ftrace_direct_multi_init(void)
ret = register_ftrace_direct(&direct, my_tramp);
if (!ret)
simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
return ret;
if (ret)
return ret;
simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
if (IS_ERR(simple_tsk)) {
unregister_ftrace_direct(&direct, my_tramp, true);
return PTR_ERR(simple_tsk);
}
return 0;
}
static void __exit ftrace_direct_multi_exit(void)