Received: by 2002:a05:6512:2355:0:0:0:0 with SMTP id p21csp79630lfu; Thu, 7 Apr 2022 21:03:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxPUSkYR0BChq1PWKmSmIlYIXYLWSYBs90oSdqTcX6LlxR7YZVEG5x5kFp3JEnHaBJH9opg X-Received: by 2002:a17:902:e5c6:b0:155:ceb9:3710 with SMTP id u6-20020a170902e5c600b00155ceb93710mr17750635plf.59.1649390627012; Thu, 07 Apr 2022 21:03:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649390627; cv=none; d=google.com; s=arc-20160816; b=nOv+6huveU5pLNGUBI++2xIG/WwBx0mXoCGNT+tEBvk3yzgvu1YKz6VygARRgzuziB U3zjmqXboQK8QUlV+6hHI/7fSyXNa7N/JLW5P0hh5zjjjPVtDDgzYSRC9K/esaJCO2I/ d2FYqpP4pbnaRrPAmwLLdNvQNpFgogXlGLPkbDBcjyDy3W6mKVXvYgH2+7TiQlaJrtis v1JW0vraTi3jv2NXp+s6bDbbokwJcEfu4C38UDK6wgxNxZLtAollu1ivLXfEcqJXjngf XIEKApQT1Vp5rVmMniO9peWfJqi2Gwa0tbbQmFQ2hCCYWhHX2UlcRKThOlr8qp65MhoM ARWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=htZcce+yZyKi4W+j/VoSEvcM7NhbQlWyaP75s9V8ebY=; b=lRuMyH4W1c4BINZ3gxumlev/H7lkoCfpcDw2x4ufnG/jY5hM96seLj9hkbxwSNRLgZ dR4fHucsK+79fRRVb1jIhD3JEnq+l8tJW3KwDjrZl+Yg0ZT1+l0hfXSmAgbKr+Lr1u5G SwmibVIKMoKgjujOrFEoW5U1bTo4ej9tEP3rFrj4T+RC0FU7cEOHU4siZVhsRzDxRtVr zWuPx+O9OB8YtJ8axEEnHCobAbF+LBTbxHlIALYBUXz+FYfPGV76UODDS1PE8/taPJgH X2im1BHVUTRUafCMjTS/Cb/LBQAvVO9IlWsNAMs4744lwCJ/IolpnlRwaUzyDekIUqiX 9r6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bA04wgq+; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id s9-20020a056a0008c900b004fe215ae62bsi24312pfu.67.2022.04.07.21.03.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 21:03:46 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bA04wgq+; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7FBF524A125; Thu, 7 Apr 2022 20:28:48 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231274AbiDHDao (ORCPT + 99 others); Thu, 7 Apr 2022 23:30:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229895AbiDHDam (ORCPT ); Thu, 7 Apr 2022 23:30:42 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 855CE2438D3 for ; Thu, 7 Apr 2022 20:28:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649388517; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=htZcce+yZyKi4W+j/VoSEvcM7NhbQlWyaP75s9V8ebY=; b=bA04wgq+RBE4xQ2Dw8VJkmiwJs1o9A7Gln4JHakbnb8edchnR4jZaOS9Hjn4rOUj+/XoBY VAHvZSxAXKfQe2BfjKOA80PbOIovMHdKhu1vfunzbXHnDfA1h83OgqL3eN6AfUqhdA67gl H7JzKL/JEdIavSDpWlZQSKBF9XId3kU= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-556-fZbqNhLDOxyepwNcMr-3Mg-1; Thu, 07 Apr 2022 23:28:33 -0400 X-MC-Unique: fZbqNhLDOxyepwNcMr-3Mg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1A720380352D; Fri, 8 Apr 2022 03:28:32 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.22.19.176]) by smtp.corp.redhat.com (Postfix) with ESMTP id D94F843E78D; Fri, 8 Apr 2022 03:28:21 +0000 (UTC) From: Nico Pache To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Rafael Aquini , Waiman Long , Baoquan He , Christoph von Recklinghausen , Don Dutile , "Herton R . Krzesinski" , David Rientjes , Michal Hocko , Andrea Arcangeli , Andrew Morton , Davidlohr Bueso , Thomas Gleixner , Peter Zijlstra , Ingo Molnar , Joel Savitz , Darren Hart , stable@kernel.org Subject: [PATCH v8] oom_kill.c: futex: Don't OOM reap the VMA containing the robust_list_head Date: Thu, 7 Apr 2022 23:28:09 -0400 Message-Id: <20220408032809.3696798-1-npache@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The pthread struct is allocated on PRIVATE|ANONYMOUS memory [1] which can be targeted by the oom reaper. This mapping is used to store the futex robust list head; 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 free the memory of the futex robust list before the exit path has handled the futex death: CPU1 CPU2 ------------------------------------------------------------------------ page_fault do_exit "signal" wake_oom_reaper oom_reaper oom_reap_task_mm (invalidates mm) exit_mm exit_mm_release futex_exit_release futex_cleanup exit_robust_list get_user (EFAULT- can't access memory) If the get_user EFAULT's, the kernel will be unable to recover the waiters on the robust_list, leaving userspace mutexes hung indefinitely. Use the robust_list address stored in the kernel to skip the VMA that holds it, allowing a successful futex_cleanup. Theoretically a failure can still occur if there are locks mapped as PRIVATE|ANON; however, the robust futexes are a best-effort approach. This patch only strengthens that best-effort. The following case can still fail: robust head (skipped) -> private lock (reaped) -> shared lock (skipped) 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: Davidlohr Bueso Cc: Thomas Gleixner Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Joel Savitz Cc: Darren Hart Cc: stable@kernel.org Co-developed-by: Joel Savitz Signed-off-by: Joel Savitz Signed-off-by: Nico Pache --- include/linux/oom.h | 2 +- mm/mmap.c | 8 +++++++- mm/oom_kill.c | 31 ++++++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/include/linux/oom.h b/include/linux/oom.h index 2db9a1432511..46355ff97abb 100644 --- a/include/linux/oom.h +++ b/include/linux/oom.h @@ -106,7 +106,7 @@ static inline vm_fault_t check_stable_address_space(struct mm_struct *mm) return 0; } -bool __oom_reap_task_mm(struct mm_struct *mm); +bool __oom_reap_task_mm(struct mm_struct *mm, void *skip_area); long oom_badness(struct task_struct *p, unsigned long totalpages); diff --git a/mm/mmap.c b/mm/mmap.c index 3aa839f81e63..6dee7ea60a13 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -3109,6 +3109,11 @@ void exit_mmap(struct mm_struct *mm) struct mmu_gather tlb; struct vm_area_struct *vma; unsigned long nr_accounted = 0; + void *robust_list = NULL; + +#ifdef CONFIG_FUTEX + robust_list = current->robust_list; +#endif /* mm's last user has gone, and its about to be pulled down */ mmu_notifier_release(mm); @@ -3126,7 +3131,8 @@ void exit_mmap(struct mm_struct *mm) * to mmu_notifier_release(mm) ensures mmu notifier callbacks in * __oom_reap_task_mm() will not block. */ - (void)__oom_reap_task_mm(mm); + (void)__oom_reap_task_mm(mm, robust_list); + set_bit(MMF_OOM_SKIP, &mm->flags); } diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 7ec38194f8e1..2a7443a64909 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -509,9 +509,10 @@ static DECLARE_WAIT_QUEUE_HEAD(oom_reaper_wait); static struct task_struct *oom_reaper_list; static DEFINE_SPINLOCK(oom_reaper_lock); -bool __oom_reap_task_mm(struct mm_struct *mm) +bool __oom_reap_task_mm(struct mm_struct *mm, void *skip_area) { struct vm_area_struct *vma; + unsigned long skip_vma = (unsigned long) skip_area; bool ret = true; /* @@ -526,6 +527,20 @@ bool __oom_reap_task_mm(struct mm_struct *mm) if (vma->vm_flags & (VM_HUGETLB|VM_PFNMAP)) continue; +#ifdef CONFIG_FUTEX + /* + * The OOM reaper runs concurrently with do_exit. + * The robust_list_head is stored in userspace and is required + * by the exit path to recover the robust futex waiters. + * Skip the VMA that contains the robust_list to allow for + * proper cleanup. + */ + if (vma->vm_start <= skip_vma && vma->vm_end > skip_vma) { + pr_info("oom_reaper: skipping vma, contains robust_list"); + continue; + } +#endif + /* * Only anonymous pages have a good chance to be dropped * without additional steps which we cannot afford as we @@ -567,6 +582,7 @@ bool __oom_reap_task_mm(struct mm_struct *mm) static bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm) { bool ret = true; + void *robust_list = NULL; if (!mmap_read_trylock(mm)) { trace_skip_task_reaping(tsk->pid); @@ -586,8 +602,11 @@ static bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm) trace_start_task_reaping(tsk->pid); +#ifdef CONFIG_FUTEX + robust_list = tsk->robust_list; +#endif /* failed to reap part of the address space. Try again later */ - ret = __oom_reap_task_mm(mm); + ret = __oom_reap_task_mm(mm, robust_list); if (!ret) goto out_finish; @@ -1149,6 +1168,7 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) unsigned int f_flags; bool reap = false; long ret = 0; + void *robust_list = NULL; if (flags) return -EINVAL; @@ -1186,11 +1206,16 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags) ret = -EINTR; goto drop_mm; } + +#ifdef CONFIG_FUTEX + robust_list = p->robust_list; +#endif /* * Check MMF_OOM_SKIP again under mmap_read_lock protection to ensure * possible change in exit_mmap is seen */ - if (!test_bit(MMF_OOM_SKIP, &mm->flags) && !__oom_reap_task_mm(mm)) + if (!test_bit(MMF_OOM_SKIP, &mm->flags) && + !__oom_reap_task_mm(mm, robust_list)) ret = -EAGAIN; mmap_read_unlock(mm); -- 2.35.1