Re: [PATCH v2 2/3] tracing/user_events: Share event state with existing threads
From: Steven Rostedt
Date: Mon Nov 23 2026 - 16:56:51 EST
On Tue, 6 Oct 2026 12:47:19 -0400
Jeff Barnes <jeffbarnes@xxxxxxxxxxxxxxxxxxx> wrote:
> static void user_event_mm_attach(struct user_event_mm *user_mm, struct task_struct *t)
> {
> unsigned long flags;
> @@ -817,14 +831,18 @@ void user_event_mm_remove(struct task_struct *t)
> might_sleep();
>
> mm = t->user_event_mm;
> +
> + spin_lock_irqsave(&user_event_mms_lock, flags);
> +
> t->user_event_mm = NULL;
>
> - /* Clone will increment the tasks, only remove if last clone */
> - if (!refcount_dec_and_test(&mm->tasks))
> + /* Clones and attached tasks increment this count. */
> + if (!refcount_dec_and_test(&mm->tasks)) {
> + spin_unlock_irqrestore(&user_event_mms_lock, flags);
> return;
> + }
>
> - /* Remove the mm from the list, so it can no longer be enabled */
> - spin_lock_irqsave(&user_event_mms_lock, flags);
> + /* Prevent new tasks from attaching after the last-task transition. */
> list_del_rcu(&mm->mms_link);
> spin_unlock_irqrestore(&user_event_mms_lock, flags);
Actually the above can be written better as:
@@ -832,26 +832,22 @@ static void delayed_user_event_mm_put(struct work_struct *work)
void user_event_mm_remove(struct task_struct *t)
{
struct user_event_mm *mm;
- unsigned long flags;
might_sleep();
mm = t->user_event_mm;
- spin_lock_irqsave(&user_event_mms_lock, flags);
+ scoped_guard(spinlock_irqsave, &user_event_mms_lock) {
+ t->user_event_mm = NULL;
- t->user_event_mm = NULL;
+ /* Clones and attached tasks increment this count. */
+ if (!refcount_dec_and_test(&mm->tasks))
+ return;
- /* Clones and attached tasks increment this count. */
- if (!refcount_dec_and_test(&mm->tasks)) {
- spin_unlock_irqrestore(&user_event_mms_lock, flags);
- return;
+ /* Prevent new tasks from attaching after the last-task transition. */
+ list_del_rcu(&mm->mms_link);
}
- /* Prevent new tasks from attaching after the last-task transition. */
- list_del_rcu(&mm->mms_link);
- spin_unlock_irqrestore(&user_event_mms_lock, flags);
-
/*
* We need to wait for currently occurring writes to stop within
* the mm. This is required since exit_mm() snaps the current rss
>
> @@ -894,11 +912,25 @@ static bool
> current_user_event_enabler_exists(struct user_event_group *group,
> unsigned long uaddr, unsigned char bit)
> {
> - struct user_event_mm *user_mm = current_user_event_mm();
> + struct user_event_mm *user_mm = current->user_event_mm;
> + unsigned long flags;
> bool exists;
>
> lockdep_assert_held(&group->reg_mutex);
>
> + if (!user_mm) {
> + spin_lock_irqsave(&user_event_mms_lock, flags);
> +
> + user_mm = user_event_mm_find_locked(current->mm);
> +
> + if (user_mm) {
> + refcount_inc(&user_mm->tasks);
> + current->user_event_mm = user_mm;
> + }
> +
> + spin_unlock_irqrestore(&user_event_mms_lock, flags);
> + }
and this with:
@@ -919,22 +915,17 @@ current_user_event_enabler_exists(struct user_event_group *group,
unsigned long uaddr, unsigned char bit)
{
struct user_event_mm *user_mm = current->user_event_mm;
- unsigned long flags;
bool exists;
lockdep_assert_held(&group->reg_mutex);
if (!user_mm) {
- spin_lock_irqsave(&user_event_mms_lock, flags);
-
+ guard(spinlock_irqsave)(&user_event_mms_lock);
user_mm = user_event_mm_find_locked(current->mm);
-
if (user_mm) {
refcount_inc(&user_mm->tasks);
current->user_event_mm = user_mm;
}
-
- spin_unlock_irqrestore(&user_event_mms_lock, flags);
}
if (!user_mm)
-- Steve
> +
> if (!user_mm)
> return false;
>
> @@ -906,8 +938,6 @@ current_user_event_enabler_exists(struct user_event_group *group,
> exists = user_event_enabler_exists(user_mm, uaddr, bit);
> mutex_unlock(&event_mutex);
>
> - user_event_mm_put(user_mm);
> -
> return exists;
> }
>