Received: by 10.192.165.156 with SMTP id m28csp1595694imm; Wed, 18 Apr 2018 12:15:57 -0700 (PDT) X-Google-Smtp-Source: AIpwx48vAOmcPCS6hKAlmf+Nn3jR+slWb8PHxe8cdjXBzlavmvK7foZyVRo4BLUARLyt3ZnhsKYT X-Received: by 2002:a17:902:7844:: with SMTP id e4-v6mr3153111pln.296.1524078957086; Wed, 18 Apr 2018 12:15:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524078957; cv=none; d=google.com; s=arc-20160816; b=raqcrxb/CnRucW/WzhaxN03PHzhcLNckq9EA4m0YK3ERCO0GAYj9+HlE6eUnIxjbk3 jxw/7LyH4OoMkpt6ssMr5tCR6hvn3yQj3PA8fA4A29mWD5kE/1CiS5PKFrib5hcMPiUo udV/V4Wp3Ofek7qwEJ1tVjlSF5FEVKuZDEvQkwErKLMsUT5Yln11+V3JjG4H5Xk2uWcc HiYx9hqLYYNrXXXtO5kp7UNUvRC6whah7V+Ak3vbfc5fVAQ9PUrKt0+e1qvRfoNOGhkd ULEAnFFlqfA8MW7pI3ty1H8RseTg/Qwssw/hZFvtrg9ZlglYZ+po7MYCZkuQYwVTY3Sq JWTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=XQ+vO1NIr2/Z3InhIBHjM4N6otfpCkmZp/ZYPh+VyWI=; b=lO8wtjpoM6bO7Zof4xPO0oG9AWnga9BBj981L4LDzsEIb+iIntSmRUgmCqo1rgnHqI 4dnxAxHudlbNBtfshfJFxqhR3qD/XGiGpdVEXAjK+dzNr1psiamiJdoTFZWH9Rvhp9F9 EP7i6To3SoqPOMTAJQx/XbkAeBNkiEgEKgr7ak3ML/xGWb1R9xmoDUoilI+4LNfQpFp1 EhnrkJJemLVKP3foMsAp6dsAb4g30QGDiYYjM+a59yRIt52mUk0gKwPeBHX1XZNUn4Fr uklrGd4iCtik9qSVUEAMxnD8hFX5AMVv575HNTggisMkdQ1EJLO8WvlnOkSeSWIXyadK 1f+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=kyqvEkw7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i2-v6si430146pll.186.2018.04.18.12.15.42; Wed, 18 Apr 2018 12:15:57 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=kyqvEkw7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1752262AbeDRTOc (ORCPT + 99 others); Wed, 18 Apr 2018 15:14:32 -0400 Received: from mail-pl0-f41.google.com ([209.85.160.41]:33871 "EHLO mail-pl0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751256AbeDRTOb (ORCPT ); Wed, 18 Apr 2018 15:14:31 -0400 Received: by mail-pl0-f41.google.com with SMTP id z12-v6so1691254plo.1 for ; Wed, 18 Apr 2018 12:14:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=XQ+vO1NIr2/Z3InhIBHjM4N6otfpCkmZp/ZYPh+VyWI=; b=kyqvEkw7UPIPWdU7kURPk+z7c5Mk2Nut6B3s3WaQZD+6nN08XaXVte1aN+DSY8T9eJ /5a25vOJrXYKNl/DOATS7J3Yfd+QIYnMMGVKBXZde5a1zULxUTYOWg9EwsU6Xc704gsJ jt5f+mP5vS+4F6RVxPwL7NcH142yl4X2/LAqR83QggImGa4v2IoEpEGQWb7wIs3fPTcP B6rUAh/1ci1n3++zJOkaNh0p5l7BI3SkXVaVvRfqwtgwyh2BcaHe/pj2bU+HmHKpwAWQ EtOl/GRBqXwMX48UYXVaLv5eHQYFgrmKQwtnLh76xi91gnkgp6zist6xxxfVWmbw84Kv EWsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=XQ+vO1NIr2/Z3InhIBHjM4N6otfpCkmZp/ZYPh+VyWI=; b=JFMinQ3g3RedtV8D77/iaxuNx62435SG4Ql6BBIleFgPS4IViTFKP3GeL/0RHA/2nf ZYkzIGC0ZouyxzyDbREWGdti96/lxK0SLIK/86gvrMkAUfOXZXuBbwVhQ9Iq2dALtW+t KgqcJhtE3VMj3Lmq3tNSLACWhqIzfRQFjDtkA3ASa+4PT1udQV5Sfbk70jQVN9WB2wNR uNeeko1g5pd1b2MCkZLr2reF09s0KmBqiWH+nkngheKrtGzAJEfTTjh3tSxgD5YANgct zAZ9Hqyc6JI7ye/r1iqFvWc7MwBl48Ylcy1WlfHKkmCyTd9kvFvwf1fraYWUbvBhi2nV Fz+A== X-Gm-Message-State: ALQs6tBIXwEXT0TBlp94QLEYj2+l0O6+122z4zGRHOfw3xUe1F5qeeJ8 +iw/6WwflCfCs8DTQWk+7CSTzg== X-Received: by 2002:a17:902:7709:: with SMTP id n9-v6mr3205440pll.385.1524078870734; Wed, 18 Apr 2018 12:14:30 -0700 (PDT) Received: from [2620:15c:17:3:3a5:23a7:5e32:4598] ([2620:15c:17:3:3a5:23a7:5e32:4598]) by smtp.gmail.com with ESMTPSA id s17sm5981210pfk.105.2018.04.18.12.14.29 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 18 Apr 2018 12:14:29 -0700 (PDT) Date: Wed, 18 Apr 2018 12:14:29 -0700 (PDT) From: David Rientjes X-X-Sender: rientjes@chino.kir.corp.google.com To: Michal Hocko cc: Andrew Morton , Tetsuo Handa , Andrea Arcangeli , Roman Gushchin , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [patch v2] mm, oom: fix concurrent munlock and oom reaper unmap In-Reply-To: <20180418075051.GO17484@dhcp22.suse.cz> Message-ID: References: <201804180057.w3I0vieV034949@www262.sakura.ne.jp> <20180418075051.GO17484@dhcp22.suse.cz> User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 18 Apr 2018, Michal Hocko wrote: > > Since exit_mmap() is done without the protection of mm->mmap_sem, it is > > possible for the oom reaper to concurrently operate on an mm until > > MMF_OOM_SKIP is set. > > > > This allows munlock_vma_pages_all() to concurrently run while the oom > > reaper is operating on a vma. Since munlock_vma_pages_range() depends on > > clearing VM_LOCKED from vm_flags before actually doing the munlock to > > determine if any other vmas are locking the same memory, the check for > > VM_LOCKED in the oom reaper is racy. > > > > This is especially noticeable on architectures such as powerpc where > > clearing a huge pmd requires serialize_against_pte_lookup(). If the pmd > > is zapped by the oom reaper during follow_page_mask() after the check for > > pmd_none() is bypassed, this ends up deferencing a NULL ptl. > > > > Fix this by reusing MMF_UNSTABLE to specify that an mm should not be > > reaped. This prevents the concurrent munlock_vma_pages_range() and > > unmap_page_range(). The oom reaper will simply not operate on an mm that > > has the bit set and leave the unmapping to exit_mmap(). > > This will further complicate the protocol and actually theoretically > restores the oom lockup issues because the oom reaper doesn't set > MMF_OOM_SKIP when racing with exit_mmap so we fully rely that nothing > blocks there... So the resulting code is more fragile and tricky. > exit_mmap() does not block before set_bit(MMF_OOM_SKIP) once it is entered. > Can we try a simpler way and get back to what I was suggesting before > [1] and simply not play tricks with > down_write(&mm->mmap_sem); > up_write(&mm->mmap_sem); > > and use the write lock in exit_mmap for oom_victims? > > Andrea wanted to make this more clever but this is the second fallout > which could have been prevented. The patch would be smaller and the > locking protocol easier > > [1] http://lkml.kernel.org/r/20170727065023.GB20970@dhcp22.suse.cz > exit_mmap() doesn't need to protect munlock, unmap, or freeing pgtables with mm->mmap_sem; the issue is that you need to start holding it in this case before munlock and then until at least the end of free_pgtables(). Anything in between also needlessly holds it so could introduce weird lockdep issues that only trigger for oom victims, i.e. they could be very rare on some configs. I don't necessarily like holding a mutex over functions where it's actually not needed, not only as a general principle but also because the oom reaper can now infer that reaping isn't possible just because it can't do down_read() and isn't aware the thread is actually in exit_mmap() needlessly holding it. I like how the oom reaper currently retries on failing to grab mm->mmap_sem and then backs out because it's assumed it can't make forward progress. Adding additional complication for situations where mm->mmap_sem is contended (and munlock to free_pgtables() can take a long time for certain processes) to check if it's actually already in exit_mmap() would seem more complicated than this. The patch is simply using MMF_UNSTABLE rather than MMF_OOM_SKIP to serialize exit_mmap() with the oom reaper and doing it before anything interesting in exit_mmap() because without it the munlock can trivially race with unmap_page_range() and cause a NULL pointer or #GP on a pmd or pte. The way Andrea implemented it is fine, we simply have revealed a race between munlock_vma_pages_all() and unmap_page_range() that needs it to do set_bit(); down_write(); up_write(); earlier.