Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp1540534ioo; Sun, 22 May 2022 18:21:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyhCydoJ+U1nEkyLOY9RpP/3C2dLWZnxrc3/fyCi3btQMyY1NlW5+7SsEqzx/wFyvY/1RQE X-Received: by 2002:a63:7247:0:b0:3c1:a8ab:fc6 with SMTP id c7-20020a637247000000b003c1a8ab0fc6mr18554225pgn.602.1653268914334; Sun, 22 May 2022 18:21:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653268914; cv=none; d=google.com; s=arc-20160816; b=g9hUNkN/KTAEOyFqozT0ru4v9AHDiGAJQU4qKISQ/5UC3oaEtgQtgvn+tJXqx5JtIj dpisaF/MczM5lHO1xVPYRTpQhh2/2RebYN8P/IJlMXg4j4umtdMgXj/bpbRgUR6f7fWI kwJN1M7ZrlcMWMjoy6D8jizblQsopJfZkYLpGaw1MGZaSmCt6ls3a69MvVI/dZsvU8ys pWWyM/da4nALmOdwWYCCwf1DDvQorNMuno7CXg2VqK9s0lklQ0PGBaNGrujbbC22tJS3 ASKQXiOq9GpECM3EnMnmU1pPBgL52ycFYOZsq7PdsTzOq9fo7/MOYyRO3C9Jd5F3uLHr LpHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=qqIlZXHciqSCzGv9K20w6MWTF8E6ifVRlg9P+8G2gKQ=; b=V1vI3iY7rSr/jO+v89XyTixXsAQLV6kMoVR419EVoDwoDuD1Uu3ireEM5Q20FJE7eU 9boBVphKQz5cbDaCduB8O3AO6MKgQA1SGhFuwjmMbCk5t+airl0loGk348h4NjeSqoLU 1txn31s24dDLCcF4TeiM9PpvMqZ7hbw4nCc2IsQRnonqJFuWuXImoXBZl3xfiaV7PTs1 g5CKExltG+yEiDoNYgmYBA9xQUIxag2iN7UIHIX9I8KNYJIMNvGATpEGLudlJj4ZOToS rJbbfp4yjqsboiOmUtmRFRMG3yiV2Cl+LA7gbVSx/o2c1PgHONSDq2kwYfnXKIsyB+Nv w2ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=oo+1ocxB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q1-20020a17090a304100b001c6acc3b2b3si12257650pjl.23.2022.05.22.18.21.43; Sun, 22 May 2022 18:21:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=oo+1ocxB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351177AbiETPzg (ORCPT + 99 others); Fri, 20 May 2022 11:55:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244131AbiETPzd (ORCPT ); Fri, 20 May 2022 11:55:33 -0400 Received: from mail-yb1-xb32.google.com (mail-yb1-xb32.google.com [IPv6:2607:f8b0:4864:20::b32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3844B179955 for ; Fri, 20 May 2022 08:55:32 -0700 (PDT) Received: by mail-yb1-xb32.google.com with SMTP id i11so14892198ybq.9 for ; Fri, 20 May 2022 08:55:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=qqIlZXHciqSCzGv9K20w6MWTF8E6ifVRlg9P+8G2gKQ=; b=oo+1ocxByaJe982/04okSYCPxdX1W9blLgrX8Fa21vxgElGzE6B/yMLLTUByoo+WPH mBz3f2Ei8URAy3xNduTdzO3blo8zIiPHHdcd5gUVscNDg8M+gjUK8wyHJ+nC1xMC7vD3 pHj+bux8qQeKOobC8z6FBxyc7L1yqqMSwvSKQ2uONAGqa6qMzMh1X8ru9lq06mItoKR7 hCR3d4ZqrzHIfEzYws3kzrcYj0HlTKNkVJXXwcvW4zvbKqxF6DrgANf8ykl3p80CDf6a IhcXnywNslbNPvPRJftRgZDNh/+P7FHemqI9Q4aXoYdjrAMc/0cFzN3mwZuLSxF6bp/O KxFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=qqIlZXHciqSCzGv9K20w6MWTF8E6ifVRlg9P+8G2gKQ=; b=YkJLiPCh1854bH8U3GRQ2PntrRo1TFTtCTCbiW9xDabINmqL8eqjx+XhTOd/Dew3VB Dpk5z+d08HuW7hAP4mohek25Truk5x3eBLWWtuginWTx8/hjMVMtx+FcLHqXe4RAKfPs NxhDW2s1YVz/wjnHt4gD/34q9m5pTj7cO+2edaby0AuT3BErp+O8t7CMcJcIXKeKWXrm mzW1PBBHSNP86ztKYBM5Pxh40IIUYYD90sUcqz7pmYrnacxg+ejlESCQyzgDIdL4YyvV vPxQ9dldJ047zUbhXpb4ApZ0owqsRNrBDrAA4T1dgeAcKy/+k0hrg3Ry0MWOZJSmiNFw wvvA== X-Gm-Message-State: AOAM533gSem/jCFvkT4wuHmwRCLRUMmxTRWKgJhfwUa67gGcYGUAYO8C JXK1KwYDtaUEtfX44BCPyqv41gZ/3cejDZChWGLHiQ== X-Received: by 2002:a25:1c0b:0:b0:64d:6b11:6a32 with SMTP id c11-20020a251c0b000000b0064d6b116a32mr10536038ybc.441.1653062131138; Fri, 20 May 2022 08:55:31 -0700 (PDT) MIME-Version: 1.0 References: <20220516075619.1277152-1-surenb@google.com> <20220519202149.3ywynqhbxlzp6uyn@revolver> In-Reply-To: From: Suren Baghdasaryan Date: Fri, 20 May 2022 08:55:20 -0700 Message-ID: Subject: Re: [PATCH v2 1/2] mm: drop oom code from exit_mmap To: Michal Hocko Cc: Liam Howlett , "akpm@linux-foundation.org" , "rientjes@google.com" , "willy@infradead.org" , "hannes@cmpxchg.org" , "guro@fb.com" , "minchan@kernel.org" , "kirill@shutemov.name" , "aarcange@redhat.com" , "brauner@kernel.org" , "hch@infradead.org" , "oleg@redhat.com" , "david@redhat.com" , "jannh@google.com" , "shakeelb@google.com" , "peterx@redhat.com" , "jhubbard@nvidia.com" , "shuah@kernel.org" , "linux-kernel@vger.kernel.org" , "linux-mm@kvack.org" , "kernel-team@android.com" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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 On Fri, May 20, 2022 at 12:21 AM Michal Hocko wrote: > > On Thu 19-05-22 14:33:03, Suren Baghdasaryan wrote: > > On Thu, May 19, 2022 at 1:22 PM Liam Howlett wrote: > [...] > > > arch_exit_mmap() was called under the write lock before, is it safe to > > > call it under the read lock? > > > > Ah, good catch. I missed at least one call chain which I believe would > > require arch_exit_mmap() to be called under write lock: > > > > arch_exit_mmap > > ldt_arch_exit_mmap > > free_ldt_pgtables > > free_pgd_range > > Why would be this a problem? This is LDT mapped into page tables but as > far as I know oom_reaper cannot really ever see that range because it is > not really reachable from any VMA. Ah, thanks! I didn't realize these page tables are not reachable from VMAs. The only other call that I'm not sure is ok without mmap write lock is xen_hvm_exit_mmap: arch_exit_mmap paravirt_arch_exit_mmap xen_hvm_exit_mmap I'll look closer today but if someone can confirm it's safe then my current patch should be fine as is. Thanks, Suren. > > > I'll need to check whether arch_exit_mmap() has to be called before > > unmap_vmas(). If not, we could move it further down when we hold the > > write lock. > > Andrew, please remove this patchset from your tree for now until I fix this. > > > > > > > > > > > > > vma = mm->mmap; > > > > if (!vma) { > > > > /* Can happen if dup_mmap() received an OOM */ > > > > - mmap_write_unlock(mm); > > > > + mmap_read_unlock(mm); > > > > return; > > > > } > > > > > > > > @@ -3138,6 +3121,16 @@ void exit_mmap(struct mm_struct *mm) > > > > /* update_hiwater_rss(mm) here? but nobody should be looking */ > > > > /* Use -1 here to ensure all VMAs in the mm are unmapped */ > > > > unmap_vmas(&tlb, vma, 0, -1); > > > > + mmap_read_unlock(mm); > > > > + > > > > + /* > > > > + * Set MMF_OOM_SKIP to hide this task from the oom killer/reaper > > > > + * because the memory has been already freed. Do not bother checking > > > > + * mm_is_oom_victim because setting a bit unconditionally is cheaper. > > > > + */ > > > > + set_bit(MMF_OOM_SKIP, &mm->flags); > > > > + > > > > + mmap_write_lock(mm); > > > > > > Is there a race here? We had a VMA but after the read lock was dropped, > > > could the oom killer cause the VMA to be invalidated? > > Nope, the oom killer itself doesn't do much beyond sending SIGKILL and > scheduling the victim for the oom_reaper. dup_mmap is holding exclusive > mmap_lock throughout the whole process. > > > > I don't think so > > > but the comment above about dup_mmap() receiving an OOM makes me > > > question it. The code before kept the write lock from when the VMA was > > > found until the end of the mm edits - and it had the check for !vma > > > within the block itself. We are also hiding it from the oom killer > > > outside the read lock so it is possible for oom to find it in that > > > window, right? > > The oom killer's victim selection doesn't really depend on the > mmap_lock. If there is a race and MMF_OOM_SKIP is not set yet then it > will consider the task and very likely bail out anyway because the > address space has already been unampped so oom_badness() would consider > this task boring. > > oom_reaper on the other hand would just try to unmap in parallel but > that is fine regardless of MMF_OOM_SKIP. Seeing the flag would allow to > bail out early rather than just trying to unmap something that is no > longer there. The only problem for the oom_reaper is to see page tables > of the address space disappearing from udner its feet. That is excluded > by the the exlusive lock and as Suren mentions mm->mmap == NULL check > if the exit_mmap wins the race. > -- > Michal Hocko > SUSE Labs