Received: by 2002:ac2:464d:0:0:0:0:0 with SMTP id s13csp3279658lfo; Mon, 23 May 2022 00:33:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx4UqWUZEeNbsVERw8sRe0rMFvAXKf2Db4KvTVR+3o0du4PvMaDIbU+zxXKHk5oq9VFjbsL X-Received: by 2002:a17:90b:3e8a:b0:1e0:51fa:516b with SMTP id rj10-20020a17090b3e8a00b001e051fa516bmr3215413pjb.151.1653291239727; Mon, 23 May 2022 00:33:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653291239; cv=none; d=google.com; s=arc-20160816; b=ljQq6swGa6qjwXw+bx2eR8Z+fShojdURIoohhFDp0M7Lsxq1yc3qgA0oUHE8v59L8Z ylYBLoq+st68eyPmXiXg1O74UkYRgWqfQl2wo1uclaav+5U3OmGCmc/VZceSItimYOzs ZGv7KqQbIlzmAjw1+NsUdWYFSd0kqqoT7pW2HjI6uhoQNxbeoH5FsZe3Croy/I6+IPEG QMGiG+Fh8dsytlirQhZ7cIA0SRA/MQ1SteRLOrC3QLFAplmbYHOlIxsjXH0JVVzg4oNe a52O2laKp7/xazrxa+U8sxTdFiI+uvrfCbFj9jTgqvi4Z5866xNHYEpQAD0JCKjVQeDm kQ0A== 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=ONhZlVBI4HpQAKHE0J0FctRsQVKq+x2f4PkT65aBxxg=; b=Foi8D1QrEchU2pgqo63droy/fiYkVNJBwJSFrwGZmPw02+hbSF3RGv8HSL/amMboks CnEUYA0mqhZ8JXoWNtaOwL6RgFinvFh1YhtGPWX0zqLlWNZdDc5qOkHrsLFzPqy9P1ws GJy4WkS1l0eClSsU/koxWnAyTfFkmjHGQfrqKj4875RBQ3KnNFrEbZ0PNQ6dPitX85Ip MWFKTPjtby1g+rpjAi1zuSIkwfBlmZJXzZTlulCi7FM70ytb0nWkg6Ka8SqhuIu+gpR2 kLxiXd6e1cKh4XjIg+xbkkW1gTiyGmykyD+kFr8eI7WF2ctES97lMML9PH7fI4Ut7o4I 6ahw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=dmedB56p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id 73-20020a62164c000000b00518b91753b3si202883pfw.208.2022.05.23.00.33.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 00:33:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=dmedB56p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 99EB3193202; Sun, 22 May 2022 23:44:42 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351509AbiETQRo (ORCPT + 99 others); Fri, 20 May 2022 12:17:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351469AbiETQRl (ORCPT ); Fri, 20 May 2022 12:17:41 -0400 Received: from mail-yw1-x1131.google.com (mail-yw1-x1131.google.com [IPv6:2607:f8b0:4864:20::1131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F91B6D878 for ; Fri, 20 May 2022 09:17:38 -0700 (PDT) Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-2ec42eae76bso92099027b3.10 for ; Fri, 20 May 2022 09:17:38 -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=ONhZlVBI4HpQAKHE0J0FctRsQVKq+x2f4PkT65aBxxg=; b=dmedB56pvgqE7vaxmJFTfngnHOclw61WJgI459OreAQ/EWhACPq1JJTl+tBexbZd3/ clCCW9wARLONeh60N3DHvB3Y1BQSNmngLUMTLBYkUFbVhQ6Zz3TAdZGwd7Tln+nw7vlY EKu+sLeAsRK/llsvB/Yx1bKeOwOhQEFTd1IoLVIkMHpkjORtv2JjeJV7qfRNvyIkyGHN JRCl3PyDvndsxDY2oDtf4qdb8KvcbW1aDVsv71H918s88KQnwhUnG1Jape1XL5qZ/FaG q93yLWhQIvyuE5w/PihuBY6NuvMO0Dj6/jwlAWaDYA9KwYqGc88G1WBjRWJcaX1aRoMs NGEw== 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=ONhZlVBI4HpQAKHE0J0FctRsQVKq+x2f4PkT65aBxxg=; b=hCYOMRftGm8/mFjcyo4wMTqSTUJ8iIzsH/QUHpP1sMKgc06Fy+MSbQjH62xSn6oiGp hJ86GeuaWTZcxB68CgrbemFS0z7XL+ABQgR16IP8nVFXYq9KX6k27FxQCb5Z/dmGHzRA MvwkjRkV+sOWRpjGhJoY5HitF/c03rEMK7uDYKt8G1nCjFLV8y+ZgADCUEdmO1pQVR6y jubEE0V1ZG0rwUg7DJ3I780+HIriooxfo42It5Jo3qmCU1CDci7bhbAMIQs4PYbo2Zsz mwHHzrOsFy5IHtjddpMvk3AoibGDQKe5eH2HqLzqnZsLObQLbteZEpbp+vfwc/m+S+Yl 7AfA== X-Gm-Message-State: AOAM532MVaA0lc7sh+k5ni4fK2Qd5MpOKDGvLZpNncuJwt0VK23XjkE6 1uYyDdQUkK2nPRKbVxyFWV6MpzmaG7jA03IEPZGcwQ== X-Received: by 2002:a81:557:0:b0:2ff:a20c:4bfc with SMTP id 84-20020a810557000000b002ffa20c4bfcmr969269ywf.293.1653063457607; Fri, 20 May 2022 09:17:37 -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 09:17:26 -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=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL 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 On Fri, May 20, 2022 at 8:55 AM Suren Baghdasaryan wrote: > > 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. My conclusion is that it's safe to call arch_exit_mmap without exclusive mmap lock since the only possible competition is from OOM-killer/process_mrelease which operate on mm->mmap and none of the arch_exit_mmap implementations use mm->mmap. Andrew, sorry for going back and forth. I think the patch is fine as is and can be integrated. Thanks! > 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