Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp1322882ioo; Sun, 22 May 2022 10:38:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRuIyf/oDt4mNz5YE98eYArRtzOpRQmWhjAszwg0OWgwR/zB7gJ808QCE5FAJPUU4PKCqP X-Received: by 2002:a65:428c:0:b0:3db:822e:2163 with SMTP id j12-20020a65428c000000b003db822e2163mr16898951pgp.466.1653241107037; Sun, 22 May 2022 10:38:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653241107; cv=none; d=google.com; s=arc-20160816; b=l0tEpFdGlVn7UdXp0VfYjoJr7qiBvx7jQAj/mOozz4vueqrPBiqZi2W3a85SDouc3Z dNHEBUiHiqraiwHGWWaNCe9ku5oQ1hEWO72JxZwpPHwF+UJgnfVYxYVHiruNoPi/T68o 6VfzLcvI8/ExdURMvy0wH56tN94M0NkpCgjjggOuk3pJv86zXb8IlbUceOq0N5iPHGI0 CqZjEmI3q8bKeeZuj5ARp+ZZvLwCRq0H9rnrrOfBcbeKZmlC7422FKXYOAK5xL/FyyRA KQ+l5s2Gqt2JjFJWjmqZYL8AGPLLBoethrnoBMORkOPPNe8MLQFzAvKrTaQ39Y+57JIi eRpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=TBN5hB0/WPFsCGSSaSWI79+b/MCuCRipw2TznjIJXMM=; b=Xj8SfrEyz9E6g7Jb41FSzFnO6q3PnsbfRHvcxplqmcp13d7BQwNCGdFGLS7MyzVCht snxY4oF6IzeqC12TspFVETTGxHXEfAxepur3ASjDfN79s+EDgRCRxCic9iVlcVTpfm0u zcnVyvhsrCwdO2V3J+xFk8je322Ci7VD6F3zaKfIJssV2NShRrIDg2Jgvai0qohBQyEg u69kUTPohBt5Sv4Q6UwCr0JJ+w19/t1IQpvqYe+gWQnVFw0C5jjCT9EPKnNtUsTfuzwD 17f9w89QWriLCXdOS+NW9rUXEjXiXtWRuJoBg9C9oL83aIwrP8Jo+5jM6Kby3mErxevS CFQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=UAPf2Wrw; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a24-20020a631a58000000b003f64a4eea39si7401031pgm.360.2022.05.22.10.38.14; Sun, 22 May 2022 10:38:27 -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=@suse.com header.s=susede1 header.b=UAPf2Wrw; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346335AbiETHVt (ORCPT + 99 others); Fri, 20 May 2022 03:21:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346276AbiETHVr (ORCPT ); Fri, 20 May 2022 03:21:47 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EE0699680 for ; Fri, 20 May 2022 00:21:44 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 17B021F975; Fri, 20 May 2022 07:21:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1653031303; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=TBN5hB0/WPFsCGSSaSWI79+b/MCuCRipw2TznjIJXMM=; b=UAPf2WrwRzaKaTOqud9B3Zxza6x0ksyxYqr57T0xLu4aZEgB+f2r3BsBe4mb1sofkwwOXh MUB1lYGzME/Ar3aV4GRM9p8wJJyAc4PEV19TOAdIX5vzb6l0vGkoqq3PJf+sWb/LQuWH+N uxCYlMHLQIyzCDF0TTs+0psO3Ofj5o0= Received: from suse.cz (unknown [10.100.201.86]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 9681D2C141; Fri, 20 May 2022 07:21:42 +0000 (UTC) Date: Fri, 20 May 2022 09:21:42 +0200 From: Michal Hocko To: Suren Baghdasaryan 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" Subject: Re: [PATCH v2 1/2] mm: drop oom code from exit_mmap Message-ID: References: <20220516075619.1277152-1-surenb@google.com> <20220519202149.3ywynqhbxlzp6uyn@revolver> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 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. > 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