From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7F7C5C433EF for ; Wed, 9 Mar 2022 00:49:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EDD7D8D0002; Tue, 8 Mar 2022 19:49:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E8C0D8D0001; Tue, 8 Mar 2022 19:49:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D55788D0002; Tue, 8 Mar 2022 19:49:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id C3CA28D0001 for ; Tue, 8 Mar 2022 19:49:17 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 8DF2E8016D for ; Wed, 9 Mar 2022 00:49:17 +0000 (UTC) X-FDA: 79223013954.03.C70C031 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 0B69740012 for ; Wed, 9 Mar 2022 00:49:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646786956; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=t1eWWygYqRs8r9/W14PM2EEm1/2DPyShF2lrMpAUnZw=; b=DNhVT/fqUiiWh0SnvhpAwr0pcO9QAqElKxgELByJKKrYSOABfhCT7o71jNtKJ+Ko6A1Xiw fsjjPI6vgRzT5wHFQTVy3wt8IBSB/1iq0UCLl3vOT/3bcHFo14qSjs2fXaMmaYChPh09nG OpVxwW6UXE/BN5c26bBv7Hc8d4nxisc= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-137-E5kCM6r3OzGotQCLRK3XaQ-1; Tue, 08 Mar 2022 19:49:11 -0500 X-MC-Unique: E5kCM6r3OzGotQCLRK3XaQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 12103180FD72; Wed, 9 Mar 2022 00:49:09 +0000 (UTC) Received: from [10.22.11.135] (unknown [10.22.11.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id 220CD131FF; Wed, 9 Mar 2022 00:48:12 +0000 (UTC) Message-ID: <8dc145f2-7195-05a1-40c5-533301ffca8b@redhat.com> Date: Tue, 8 Mar 2022 19:48:11 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH v4] mm/oom_kill.c: futex: Don't OOM reap a process with a futex robust list Content-Language: en-US To: Nico Pache , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Rafael Aquini , Baoquan He , Christoph von Recklinghausen , Don Dutile , "Herton R . Krzesinski" , David Rientjes , Michal Hocko , Andrea Arcangeli , Andrew Morton , tglx@linutronix.de, mingo@redhat.com, dvhart@infradead.org, dave@stgolabs.net, andrealmeid@collabora.com, peterz@infradead.org, Joel Savitz References: <20220309002550.103786-1-npache@redhat.com> From: Waiman Long In-Reply-To: <20220309002550.103786-1-npache@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 0B69740012 X-Stat-Signature: 9tftdzy9puein5qzpowmwfceyr4rcxs4 Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="DNhVT/fq"; spf=none (imf01.hostedemail.com: domain of longman@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-HE-Tag: 1646786956-618146 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 > Cc: Waiman Long > Cc: Baoquan He > Cc: Christoph von Recklinghausen > Cc: Don Dutile > Cc: Herton R. Krzesinski > Cc: David Rientjes > Cc: Michal Hocko > Cc: Andrea Arcangeli > Cc: Andrew Morton > Cc: > Cc: > Cc: > Cc: > Cc: > Cc: > Co-developed-by: Joel Savitz > Signed-off-by: Joel Savitz > Signed-off-by: Nico Pache > --- > 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