Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>, bpf@vger.kernel.org
Cc: daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com,
	memxor@gmail.com, oleg@redhat.com, peterz@infradead.org,
	mhiramat@kernel.org, jolsa@kernel.org, akpm@linux-foundation.org,
	liam@infradead.org, ljs@kernel.org, surenb@google.com,
	david@kernel.org, vbabka@kernel.org, jannh@google.com,
	hughd@google.com, kees@kernel.org, mingo@redhat.com,
	tglx@kernel.org, x86@kernel.org,
	linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org,
	linux-perf-users@vger.kernel.org
Subject: Re: [PATCH bpf-next 7/9] fork: Let the new task point to its mm before uprobes can find it
Date: Thu, 22 Oct 2026 02:23:45 +0000	[thread overview]
Message-ID: <sashiko-outbox-180866@kernel.org> (raw)
In-Reply-To: <20261022015712.1297331-8-alexei.starovoitov@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] fork: clear p->mm under task_lock() before mmput() in bad_fork_cleanup_mm
--

commit ec1e952207350f8b07e6038358df3fabd0496801
Author: Alexei Starovoitov <ast@kernel.org>

fork: Let the new task point to its mm before uprobes can find it

This commit sets tsk->mm under task_lock() at the end of dup_mmap() before
uprobe_end_dup_mmap() releases dup_mmap_sem, so that uprobe filters on
inherited perf events can match the child's mm. It also clears tsk->mm under
task_lock() if dup_mm() fails after dup_mmap() succeeds.

> diff --git a/kernel/fork.c b/kernel/fork.c
> index 2e125623cdbc3..a1042e370bdb8 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -1537,7 +1537,7 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,
>  	if (!mm_init(mm, tsk))
>  		goto fail_nomem;
>  
> -	err = dup_mmap(mm, oldmm);
> +	err = dup_mmap(tsk, mm, oldmm);
>  	if (err)
>  		goto free_pt;
>  
> @@ -1550,6 +1550,13 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,
>  	return mm;
>  
>  free_pt:
> +	/*
> +	 * dup_mmap() has set it if it succeeded. get_task_mm() must not find
> +	 * it after the mmput() below.
> +	 */
> +	task_lock(tsk);
> +	tsk->mm = NULL;
> +	task_unlock(tsk);
>  	/* don't put binfmt in mmput, we haven't got module yet */
>  	mm->binfmt = NULL;
>  	mm_init_owner(mm, NULL);

[Severity: High]
dup_mm() clears tsk->mm under task_lock(tsk) before calling mmput(mm) so
that get_task_mm() cannot find a freed mm when dup_mm() fails. However,
what happens if copy_process() fails after copy_mm() has already succeeded?

In kernel/fork.c:copy_process(), perf_event_init_task() runs before
copy_mm() and links inherited perf events (with event->hw.target = p) onto
parent_event->child_list and uprobe filter->perf_events:

kernel/fork.c:copy_process() {
	...
	retval = perf_event_init_task(p, clone_flags);
	if (retval)
		goto bad_fork_sched_cancel_fork;
	...
	retval = copy_mm(clone_flags, p);
	if (retval)
		goto bad_fork_cleanup_signal;

If a later step in copy_process() fails (for example, copy_namespaces(),
copy_io(), copy_thread(), alloc_pid(), cgroup_can_fork(), or
fatal_signal_pending(current) after waiting in dup_userfaultfd_complete()),
execution jumps to bad_fork_cleanup_mm:

bad_fork_cleanup_mm:
	sched_cache_fork_cleanup(p);
	if (p->mm) {
		mm_clear_owner(p->mm, p);
		mmput(p->mm);
	}
	...
bad_fork_cleanup_security:
	security_task_free(p);
bad_fork_cleanup_audit:
	audit_free(p);
bad_fork_cleanup_perf:
	perf_event_free_task(p);

Here mmput(p->mm) drops the reference and frees the mm, but p->mm is left
non-null while teardown continues through exit_fs(p), exit_files(p), and
security_task_free(p) before perf_event_free_task(p) detaches the inherited
events.

During this window, can a concurrent caller of get_task_mm(p) (such as
perf_event_set_addr_filter() -> perf_event_for_each_child() ->
perf_event_addr_filters_apply() in kernel/events/core.c) or bpf helpers like
bpf_find_vma() and bpf_iter_task_vma_new() in kernel/bpf/task_iter.c (for
instance from security_task_free(p)) observe the non-null p->mm under
task_lock(p) and call mmget() on the already-freed mm_struct?

Similarly, when perf_event_free_task(p) -> uprobe_perf_close() runs (or a
concurrent uprobe_perf_filter() runs), trace_uprobe_filter_remove() and
__uprobe_perf_filter() in kernel/trace/trace_uprobe.c read
event->hw.target->mm and compare that dangling pointer against active
mm_structs.

Should bad_fork_cleanup_mm in copy_process() also clear p->mm under
task_lock(p) before calling mmput()?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261022015712.1297331-1-alexei.starovoitov@gmail.com?part=7

  reply	other threads:[~2026-10-22  2:23 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-22  1:57 [PATCH bpf-next 0/9] uprobes: Don't stall fork() when mmap_lock is busy Alexei Starovoitov
2026-10-22  1:57 ` [PATCH bpf-next 1/9] mm: Bring back mmap_write_trylock() Alexei Starovoitov
2026-10-22  2:02   ` sashiko-bot
2026-10-22  1:57 ` [PATCH bpf-next 2/9] uprobes: Don't run exit_mmap() under dup_mmap_sem Alexei Starovoitov
2026-10-22  2:04   ` sashiko-bot
2026-10-22  1:57 ` [PATCH bpf-next 3/9] uprobes/x86: Finish an unoptimization which failed half-way Alexei Starovoitov
2026-10-22  2:06   ` sashiko-bot
2026-10-22  2:57   ` Gregory Price (Meta)
2026-10-22  1:57 ` [PATCH bpf-next 4/9] uprobes/x86: Don't WARN if an optimized probe can't be written Alexei Starovoitov
2026-10-22  2:03   ` sashiko-bot
2026-10-22  2:37   ` bot+bpf-ci
2026-10-22 17:56   ` Jann Horn
2026-10-22 19:33     ` Liam R. Howlett
2026-10-22  1:57 ` [PATCH bpf-next 5/9] uprobes: Don't sleep on mmap_lock under dup_mmap_sem Alexei Starovoitov
2026-10-22  2:16   ` sashiko-bot
2026-10-22  2:37   ` bot+bpf-ci
2026-10-22 23:11   ` Andrii Nakryiko
2026-10-22  1:57 ` [PATCH bpf-next 6/9] uprobes: Don't sleep on mmap_lock with dup_mmap_sem held in dup_mmap() Alexei Starovoitov
2026-10-22  2:15   ` sashiko-bot
2026-10-22 18:42   ` Jann Horn
2026-10-22  1:57 ` [PATCH bpf-next 7/9] fork: Let the new task point to its mm before uprobes can find it Alexei Starovoitov
2026-10-22  2:23   ` sashiko-bot [this message]
2026-10-22  1:57 ` [PATCH bpf-next 8/9] uprobes: Don't leak the breakpoints if the unregistering task is killed Alexei Starovoitov
2026-10-22  2:20   ` sashiko-bot
2026-10-22  1:57 ` [PATCH bpf-next 9/9] selftests/bpf: Test uprobes and fork() with busy mmap_lock Alexei Starovoitov
2026-10-22  2:10   ` sashiko-bot
2026-10-22  2:22     ` Alexei Starovoitov
2026-10-22  2:37   ` bot+bpf-ci

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-180866@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=david@kernel.org \
    --cc=eddyz87@gmail.com \
    --cc=hughd@google.com \
    --cc=jannh@google.com \
    --cc=jolsa@kernel.org \
    --cc=kees@kernel.org \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=memxor@gmail.com \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=surenb@google.com \
    --cc=tglx@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox