linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Waiman Long <longman@redhat.com>
To: Nico Pache <npache@redhat.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Cc: Rafael Aquini <aquini@redhat.com>, Baoquan He <bhe@redhat.com>,
	Christoph von Recklinghausen <crecklin@redhat.com>,
	Don Dutile <ddutile@redhat.com>,
	"Herton R . Krzesinski" <herton@redhat.com>,
	David Rientjes <rientjes@google.com>,
	Michal Hocko <mhocko@suse.com>,
	Andrea Arcangeli <aarcange@redhat.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	tglx@linutronix.de, mingo@redhat.com, dvhart@infradead.org,
	dave@stgolabs.net, andrealmeid@collabora.com,
	peterz@infradead.org, Joel Savitz <jsavitz@redhat.com>
Subject: Re: [PATCH v4] mm/oom_kill.c: futex: Don't OOM reap a process with a futex robust list
Date: Tue, 8 Mar 2022 19:48:11 -0500	[thread overview]
Message-ID: <8dc145f2-7195-05a1-40c5-533301ffca8b@redhat.com> (raw)
In-Reply-To: <20220309002550.103786-1-npache@redhat.com>

On 3/8/22 19:25, Nico Pache wrote:
> The pthread struct is allocated on PRIVATE|ANONYMOUS memory [1] which can
> be targeted by the oom reaper. This mapping is also used to store the futex
> robust list; the kernel does not keep a copy of the robust list and instead
> references a userspace address to maintain the robustness during a process
> death. A race can occur between exit_mm and the oom reaper that allows
> the oom reaper to clear the memory of the futex robust list before the
> exit path has handled the futex death.
>
> Prevent the OOM reaper from concurrently reaping the mappings if the dying
> process contains a robust_list. If the dying task_struct does not contain
> a pointer in tsk->robust_list, we can assume there was either never one
> setup for this task struct, or futex_cleanup has properly handled the
> futex death and we can safely reap this memory.
>
> Reproducer: https://gitlab.com/jsavitz/oom_futex_reproducer
>
> [1] https://elixir.bootlin.com/glibc/latest/source/nptl/allocatestack.c#L370
>
> Fixes: 212925802454 ("mm: oom: let oom_reap_task and exit_mmap run concurrently")
> Cc: Rafael Aquini <aquini@redhat.com>
> Cc: Waiman Long <longman@redhat.com>
> Cc: Baoquan He <bhe@redhat.com>
> Cc: Christoph von Recklinghausen <crecklin@redhat.com>
> Cc: Don Dutile <ddutile@redhat.com>
> Cc: Herton R. Krzesinski <herton@redhat.com>
> Cc: David Rientjes <rientjes@google.com>
> Cc: Michal Hocko <mhocko@suse.com>
> Cc: Andrea Arcangeli <aarcange@redhat.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: <tglx@linutronix.de>
> Cc: <mingo@redhat.com>
> Cc: <dvhart@infradead.org>
> Cc: <dave@stgolabs.net>
> Cc: <andrealmeid@collabora.com>
> Cc: <peterz@infradead.org>
> Co-developed-by: Joel Savitz <jsavitz@redhat.com>
> Signed-off-by: Joel Savitz <jsavitz@redhat.com>
> Signed-off-by: Nico Pache <npache@redhat.com>
> ---
>   mm/oom_kill.c | 19 +++++++++++++++++++
>   1 file changed, 19 insertions(+)
>
> diff --git a/mm/oom_kill.c b/mm/oom_kill.c
> index 989f35a2bbb1..37af902494d8 100644
> --- a/mm/oom_kill.c
> +++ b/mm/oom_kill.c
> @@ -587,6 +587,25 @@ static bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm)
>   		goto out_unlock;
>   	}
>   
> +	/* Don't reap a process holding a robust_list as the pthread
> +	 * struct is allocated in userspace using PRIVATE | ANONYMOUS
> +	 * memory which when reaped before futex_cleanup() can leave
> +	 * the waiting process stuck.
> +	 */
> +#ifdef CONFIG_FUTEX
> +	bool robust = false;
> +
> +	robust = tsk->robust_list != NULL;
> +#ifdef CONFIG_COMPAT
> +	robust |= tsk->compat_robust_list != NULL;
> +#endif
> +	if (robust) {
> +		trace_skip_task_reaping(tsk->pid);
> +		pr_info("oom_reaper: skipping task as it contains a robust list");
> +		goto out_finish;
> +	}
> +#endif
> +
>   	trace_start_task_reaping(tsk->pid);
>   
>   	/* failed to reap part of the address space. Try again later */

I believe it will be easier to read if you define a helper function like

#ifdef CONFIG_FUTEX
static inline bool has_robust_list(struct task_struct *tsk)
{
         bool robust = !!tsk->robust_list;

#ifdef CONFIG_COMPAT
         robust |= !!tsk->compat_robust_list
#endif
         return robust;
}
#else
static inline bool has_robust_list(struct task_struct *tsk)
{
         return false;
}
#endif

Then you don't need any #if/endif in oom_reap_task_mm().

Cheers,
Longman


  reply	other threads:[~2022-03-09  1:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-09  0:25 [PATCH v4] mm/oom_kill.c: futex: Don't OOM reap a process with a futex robust list Nico Pache
2022-03-09  0:48 ` Waiman Long [this message]
2022-03-09 13:09 ` Michal Hocko
2022-03-09 21:34   ` Nico Pache
2022-03-10  8:46     ` Michal Hocko

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=8dc145f2-7195-05a1-40c5-533301ffca8b@redhat.com \
    --to=longman@redhat.com \
    --cc=aarcange@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=andrealmeid@collabora.com \
    --cc=aquini@redhat.com \
    --cc=bhe@redhat.com \
    --cc=crecklin@redhat.com \
    --cc=dave@stgolabs.net \
    --cc=ddutile@redhat.com \
    --cc=dvhart@infradead.org \
    --cc=herton@redhat.com \
    --cc=jsavitz@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.com \
    --cc=mingo@redhat.com \
    --cc=npache@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rientjes@google.com \
    --cc=tglx@linutronix.de \
    /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;
as well as URLs for NNTP newsgroup(s).