From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796047219; b=Rp6xkvGcslxh5+5UcMK9+hpHloB/USDEyxSJUlb2C+lVT9pBwoOX90Z6ygsJR4VhdHSb HYbGdfklrpN/DKFh6SlArzlQvWDDG/wUCqlO0qHwW9EiOQ38Nzby5CWKz2nrgrf1bOXsp 5HbcSmMbd6xQ+ba4vXk9hin8ckbIONB8n7gboUYyQ8lPU2k96I4ldeUQVlGcKAE4uKcJV jTAk8FxnnbDVY9EYewEEJq6vqWJ7wcMwkOYNe9UeY9UngbL3LS1UEV9PitcMsyTsAX3tp uQuepixx8PEmIvrcXvf50f6+teKW1xxI6UK3MCUxCwQwJdG+q83j0fuYI6Bu/xV2/6g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796047219; h=Received:DKIM-Signature:Received:Message-ID:User-Agent:Date:From:To: Cc:Subject:References:MIME-Version:Content-Type; bh=1ICSaVI9eOvaEYxibllJjAgLM1GMN5nFMzQPyIWZXig=; b=bjw61iNdZXQZe0Y32M12gogTrQy1yqocJiJG4yN5wqh47guNdntwompgoq/ti9R3O1Tj /239SbexgKwz7lF+2oJaMU7xUqY1BiJLIa94cr2+90podH5X8afP0anFbmirbaP0GQBiq QLsd9+CvFqy90uXUFN3zgisQ1AU5YcViNnjAO7ZLDloL9ox0UZjIE4xXfcFUqW5ZyQP+t rwuxP91Q8SbgcGaS2k1gNc4FtXa9qvWCB3/4rVQlVeUwiw+tiG4wxFB7v3eUgWST6ecD9 GYdIUbjC9BCLPD4X3NlVY4CzHEYaQH1slM0KTyGsOQBCFvr4/AhOv55FLJGmVWz8LuQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=eTFgRh8g; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B79E1606A687 for ; Mon, 30 Nov 2026 14:00:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=eTFgRh8g Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5695E1F00929; Mon, 30 Nov 2026 14:00:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796047219; bh=1ICSaVI9eOvaEYxibllJjAgLM1GMN5nFMzQPyIWZXig=; h=Date:From:To:Cc:Subject:References; b=eTFgRh8gnj/2QR5USYKTauRqBFI+TU/U9LOL9GdJVoARHiH3drpS/5heQ5UTWDc5t +do3Agy8SG/V6pMem29wssrf6FMdfFKCtJx6LdUB01szyxD2BtuM8w/7nTlS9cdxHd o+bqblmkBz+/iAruAyloCZuEhmRq6mKj6T3yvL4IAJzJ0VFTAuctmUCQqzqED/z9+8 5rfnikF6QmT9bG/CelE6zmKnWSDGna0RzoFVP0p53OcRCU/qdOHmAubhbzjpdilO/e 90MZZ9Ib1P35Mjdcr2Yz15UmQaGmsf15F0QC8rtI3KHeenBeQTh+/tubDtE2PgiUNs AOqSO/BUYhyYg== Received: from rostedt by gandalf with local (Exim 4.100.1) (envelope-from ) id 1xY1vW-00000009IcX-2tTT; Mon, 30 Nov 2026 09:00:18 -0500 Message-ID: <20261130140018.491904199@kernel.org> User-Agent: quilt/0.69 Date: Mon, 30 Nov 2026 08:59:53 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mathieu Desnoyers , Beau Belgrave , Jeff Barnes Subject: [for-next][PATCH 4/5] tracing/user_events: Share event state with existing threads References: <20261130135949.755499997@kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 From: Jeff Barnes Threads created before the first user-events registration share an mm_struct but can each retain a NULL user_event_mm pointer. After one thread registers an event and publishes a user_event_mm for the shared address space, a pre-existing sibling can otherwise allocate a second wrapper with an independent enabler list. When duplicate-enabler lookup runs for a task with no user_event_mm, look up the wrapper already associated with current->mm and attach the task to it. The group reg_mutex serializes registration paths, while user_event_mms_lock protects the global lookup and interlocks attachment with the final task-count decrement and list removal. Increment tasks before publishing the wrapper through current->user_event_mm so exit or exec will account for the attachment. Hold user_event_mms_lock only for lookup, reference accounting, and task attachment. Release it before acquiring event_mutex to inspect the shared enabler list. user_event_mm_attach() cannot be reused for this operation because it publishes a newly allocated wrapper on the global list rather than attaching a task to an already-published wrapper. Link: https://patch.msgid.link/20261006164720.3940272-3-jeffbarnes@linux.microsoft.com Reviewed-by: Beau Belgrave Signed-off-by: Jeff Barnes Signed-off-by: Steven Rostedt --- kernel/trace/trace_events_user.c | 44 +++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_user.c index 28e07fb1386b..a238e3bbcdae 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -760,6 +760,20 @@ static struct user_event_mm *user_event_mm_alloc(struct task_struct *t) return user_mm; } +static struct user_event_mm * +user_event_mm_find_locked(struct mm_struct *mm) +{ + struct user_event_mm *user_mm; + + lockdep_assert_held(&user_event_mms_lock); + + list_for_each_entry(user_mm, &user_event_mms, mms_link) + if (user_mm->mm == mm) + return user_mm; + + return NULL; +} + static void user_event_mm_attach(struct user_event_mm *user_mm, struct task_struct *t) { unsigned long flags; @@ -823,14 +837,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); @@ -900,11 +918,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); + } + if (!user_mm) return false; @@ -912,8 +944,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; } -- 2.53.0