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 91C64C433EF for ; Fri, 8 Apr 2022 03:24:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E50C36B0071; Thu, 7 Apr 2022 23:24:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DD9226B0072; Thu, 7 Apr 2022 23:24:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2B786B0074; Thu, 7 Apr 2022 23:24:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0221.hostedemail.com [216.40.44.221]) by kanga.kvack.org (Postfix) with ESMTP id ABF506B0071 for ; Thu, 7 Apr 2022 23:24:07 -0400 (EDT) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 548E2AE795 for ; Fri, 8 Apr 2022 03:24:07 +0000 (UTC) X-FDA: 79332268134.29.9555B2E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf11.hostedemail.com (Postfix) with ESMTP id D0C5C40002 for ; Fri, 8 Apr 2022 03:24:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649388246; 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=VwbMtpr/w594vzWsDxQkYyAj5QvpUxkx+48AUijQtdE=; b=a3ZgcH/KYd7iiBMZBnX3lgERenBlCji1dogf+6/vKsojPgP30NWR/dx2XV+RI3VzJKLNGj RgX43fra/6Vfb/WUXUy9sorCRfqUMNIB/GWOvmhJn+vmdUT9B/4cRK5BjwEetHNO9ZlmLG DDNRBvvuvn4DVkbDRIsemGoitXRKxwY= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-379-oWAA_Q2XMEW-wGhd3NY6gA-1; Thu, 07 Apr 2022 23:24:05 -0400 X-MC-Unique: oWAA_Q2XMEW-wGhd3NY6gA-1 Received: by mail-qk1-f197.google.com with SMTP id bp31-20020a05620a459f00b00699fabcc554so2824749qkb.12 for ; Thu, 07 Apr 2022 20:24:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=VwbMtpr/w594vzWsDxQkYyAj5QvpUxkx+48AUijQtdE=; b=icYv47YMCgLeln24iNFdMnDCKmrSKfma/or+d6pa/8RuC5O42DZnP1q1wUqpfAPFp2 ptwyFItsyVJMAkYu/zGCAtAVJ+/z2SADNx/IZUssWxo8LNvWz9yFzKft7ojBfGV2KbxH 9kiLXSJibHLHPV+7sYaSi3m5vRRUV0JXg0fxxWNVzsHX6yEYO0tY7vYjMm9Ksa9EuGxe Cn9S0Es/+EzET+uEVMmrfLuPThmUnco112vHcNbVieK4tpgR423AfV9Bs2YTbPtveTXr DEXHx6JPqoXq5wP8JxWDssr80NgEfnCtEgdFJoM+YZnilbp7g2YzUxbtsHgpj7WXoeD3 jB5Q== X-Gm-Message-State: AOAM533RlYwjnSJQGZKIxw1qVHnEQTirDM6pzDDXY7xGSvBOfFZaOCzM 20DrcWxWbOA8Si9QSXn+Xwasj7bYzZsFPLU/vIUpDo1c5Thab7k75CsWQhiFS4bInKn9jGwpZ6Z /dUret/FOs8U= X-Received: by 2002:a05:6214:508e:b0:441:24df:4b with SMTP id kk14-20020a056214508e00b0044124df004bmr14503257qvb.34.1649388244742; Thu, 07 Apr 2022 20:24:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzVAjMqFbyhfr+rJ24QD/v7Ras7QI1ICbZFTBoVen3rOLcMgbrRA2TID8i8n5O8xVUBtgs16w== X-Received: by 2002:a05:6214:508e:b0:441:24df:4b with SMTP id kk14-20020a056214508e00b0044124df004bmr14503240qvb.34.1649388244423; Thu, 07 Apr 2022 20:24:04 -0700 (PDT) Received: from [192.168.0.188] ([24.48.139.231]) by smtp.gmail.com with ESMTPSA id n8-20020ac85a08000000b002e06aa02021sm17819102qta.49.2022.04.07.20.24.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 07 Apr 2022 20:24:03 -0700 (PDT) Message-ID: <925163cf-d44c-de39-7fb9-9ff7769d36a8@redhat.com> Date: Thu, 7 Apr 2022 23:24:02 -0400 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 v7] oom_kill.c: futex: Don't OOM reap the VMA containing the robust_list_head To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Rafael Aquini , Waiman Long , Baoquan He , Christoph von Recklinghausen , Don Dutile , "Herton R . Krzesinski" , David Rientjes , Michal Hocko , Andrea Arcangeli , Davidlohr Bueso , Thomas Gleixner , Peter Zijlstra , Ingo Molnar , Joel Savitz , Darren Hart , stable@kernel.org References: <20220408030137.3693195-1-npache@redhat.com> <20220407201250.b4ebaae0cb327cad7b2eb3cf@linux-foundation.org> From: Nico Pache In-Reply-To: <20220407201250.b4ebaae0cb327cad7b2eb3cf@linux-foundation.org> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Stat-Signature: rp9z33snqc6pnm43buttckmyxi84z4co Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="a3ZgcH/K"; spf=none (imf11.hostedemail.com: domain of npache@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=npache@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: D0C5C40002 X-HE-Tag: 1649388246-940787 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 4/7/22 23:12, Andrew Morton wrote: > On Thu, 7 Apr 2022 23:01:37 -0400 Nico Pache wrote: > >> 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 >> >> ... >> >> --- 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 *robust_list); >> >> long oom_badness(struct task_struct *p, >> unsigned long totalpages); >> diff --git a/mm/mmap.c b/mm/mmap.c >> index 3aa839f81e63..d5af1b83cbb2 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; >> + >> +#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); > > uninitialized var warning when CONFIG_FUTEX=n? > >> + >> set_bit(MMF_OOM_SKIP, &mm->flags); >> } >> >> --- 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 *robust_list) > > Well, this is no longer necessarily a robust_list*. It's just an > address to skip and the name should reflect that? ok fair point. I left it as robust list to be more clean what is being skipped and renamed the head variable to skip_vma so its more verbose. But I dont have that strong of an opinion on it so I'll change it real quick. > >> { >> struct vm_area_struct *vma; >> + unsigned long skip_vma = (unsigned long) robust_list; >> 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; >> >> 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); > > unintialized var when CONFIG_FUTEX=n? > >> 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; >> >> 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)) > > again Good catch. Ill fix those real quick. > >> ret = -EAGAIN; >> mmap_read_unlock(mm); >