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 51FD9C76196 for ; Mon, 3 Apr 2023 19:50:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B400F6B0072; Mon, 3 Apr 2023 15:50:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AEFFD6B0074; Mon, 3 Apr 2023 15:50:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9DF246B0075; Mon, 3 Apr 2023 15:50:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 903196B0072 for ; Mon, 3 Apr 2023 15:50:05 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 617051607A2 for ; Mon, 3 Apr 2023 19:50:05 +0000 (UTC) X-FDA: 80641120770.04.921AA59 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf22.hostedemail.com (Postfix) with ESMTP id EC85FC001B for ; Mon, 3 Apr 2023 19:50:01 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=X60NF2vi; dmarc=none; spf=none (imf22.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680551402; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HGctQB0MsRBtisRk2++z/+GWlNTueM1Ui8YvixFhQlY=; b=YnGTKJdZfpIfI2xy4f3e9lZrlS9614dVt3HvTZaBZ18rZ+etIU2zQkw/Hvh/nLlaIzbjKL x61m2MwUiT13VwwW2vxcQuswuedxZBV10ZSJ9cyV7r41t2cAKHSfBeYkhYzQtguHaUJFnG khkFG9qApXmddScfPIfaDT2Pw+LiRpE= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=X60NF2vi; dmarc=none; spf=none (imf22.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680551402; a=rsa-sha256; cv=none; b=IlMvt3ACpKO1tnBXtVQrZU1OWJCOTiy69l52Xb4BcC6Ce90LXSB/Q7K8l+A1g//5m+II8Z zIK1SXlgL22UE/3CoelhpWtmycpp5L+Xf16lxdvhoaYUbz72pWYmqwGy6IMYE1uIhCkhsX AspFt9RkiT1QTN8rkSESDqzsInmJ/ms= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=HGctQB0MsRBtisRk2++z/+GWlNTueM1Ui8YvixFhQlY=; b=X60NF2viHjNcrKZmmtR7mlOIcZ 1Y36Pe07ymKNMV3a8raWrlb4MRrCcrdjoAvIbIQjODociXLvioH6LYHzwVlwfWsvJq+Coq+a/0z/B cslo4SZh7ylCbnShU+ddLnjAk0nBSLoWs32ZKkGIAbvH2yttKqc7lXMcYSOWSd/dkFhurl+0xKNX0 2gzCwU/YVPspZTaLjZf6xB7foALpxqVWfVraXkGPwiT1Nn/4ptB2CYzNFQb9qpeeVUrymrRKh9Tt0 2WSTo+eQKqG+msljYC8OzkjMwNhCnm+5sQ4ESwLx/A81NxHyIciWE+1QzEz+kzcR0GJ0bZUGMIsmV QcRYx6yg==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pjQB8-00ES6a-Ln; Mon, 03 Apr 2023 19:49:22 +0000 Date: Mon, 3 Apr 2023 20:49:22 +0100 From: Matthew Wilcox To: Suren Baghdasaryan Cc: akpm@linux-foundation.org, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mgorman@techsingularity.net, dave@stgolabs.net, liam.howlett@oracle.com, peterz@infradead.org, ldufour@linux.ibm.com, paulmck@kernel.org, mingo@redhat.com, will@kernel.org, luto@kernel.org, songliubraving@fb.com, peterx@redhat.com, david@redhat.com, dhowells@redhat.com, hughd@google.com, bigeasy@linutronix.de, kent.overstreet@linux.dev, punit.agrawal@bytedance.com, lstoakes@gmail.com, peterjung1337@gmail.com, rientjes@google.com, chriscli@google.com, axelrasmussen@google.com, joelaf@google.com, minchan@google.com, rppt@kernel.org, jannh@google.com, shakeelb@google.com, tatashin@google.com, edumazet@google.com, gthelen@google.com, gurua@google.com, arjunroy@google.com, soheil@google.com, leewalsh@google.com, posk@google.com, michalechner92@googlemail.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH v3 26/35] mm: fall back to mmap_lock if vma->anon_vma is not yet set Message-ID: References: <20230216051750.3125598-1-surenb@google.com> <20230216051750.3125598-27-surenb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: EC85FC001B X-Stat-Signature: rjesb7puc8of7y7et4oypg5q7gq7qxyq X-HE-Tag: 1680551401-366593 X-HE-Meta: U2FsdGVkX193UecGrscf4JvJuVWQ7U0bpaGK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ekp9VBuS6pNh5kNWdBsQjIcVZfXaA7GG7pwd3MK5S1PXgsQ9S3Fl55VsRpwebkIWKM0W94Xt5r08+uGFyhtXlHt/5RNVvWPJWNwLvaNK/I0b8pXoqD8/a1vFKIi0p6ysd+dUCi3iV82yQbRlPMjdEdVxYW9m7kLy3Z+as 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 Fri, Feb 17, 2023 at 08:10:35AM -0800, Suren Baghdasaryan wrote: > On Fri, Feb 17, 2023 at 8:05 AM Matthew Wilcox wrote: > > > > On Thu, Feb 16, 2023 at 06:14:59PM -0800, Suren Baghdasaryan wrote: > > > On Thu, Feb 16, 2023 at 11:43 AM Suren Baghdasaryan wrote: > > > > > > > > On Thu, Feb 16, 2023 at 7:44 AM Matthew Wilcox wrote: > > > > > > > > > > On Wed, Feb 15, 2023 at 09:17:41PM -0800, Suren Baghdasaryan wrote: > > > > > > When vma->anon_vma is not set, page fault handler will set it by either > > > > > > reusing anon_vma of an adjacent VMA if VMAs are compatible or by > > > > > > allocating a new one. find_mergeable_anon_vma() walks VMA tree to find > > > > > > a compatible adjacent VMA and that requires not only the faulting VMA > > > > > > to be stable but also the tree structure and other VMAs inside that tree. > > > > > > Therefore locking just the faulting VMA is not enough for this search. > > > > > > Fall back to taking mmap_lock when vma->anon_vma is not set. This > > > > > > situation happens only on the first page fault and should not affect > > > > > > overall performance. > > > > > > > > > > I think I asked this before, but don't remember getting an aswer. > > > > > Why do we defer setting anon_vma to the first fault? Why don't we > > > > > set it up at mmap time? > > > > > > > > Yeah, I remember that conversation Matthew and I could not find the > > > > definitive answer at the time. I'll look into that again or maybe > > > > someone can answer it here. > > > > > > After looking into it again I'm still under the impression that > > > vma->anon_vma is populated lazily (during the first page fault rather > > > than at mmap time) to avoid doing extra work for areas which are never > > > faulted. Though I might be missing some important detail here. > > > > How often does userspace call mmap() and then _never_ fault on it? > > I appreciate that userspace might mmap() gigabytes of address space and > > then only end up using a small amount of it, so populating it lazily > > makes sense. But creating a region and never faulting on it? The only > > use-case I can think of is loading shared libraries: > > > > openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 > > (...) > > mmap(NULL, 1970000, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f0ce612e000 > > mmap(0x7f0ce6154000, 1396736, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x26000) = 0x7f0ce6154000 > > mmap(0x7f0ce62a9000, 339968, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x17b000) = 0x7f0ce62a9000 > > mmap(0x7f0ce62fc000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1ce000) = 0x7f0ce62fc000 > > mmap(0x7f0ce6302000, 53072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f0ce6302000 > > > > but that's a file-backed VMA, not an anon VMA. > > Might the case of dup_mmap() while forking be the reason why a VMA in > the child process might be never used while parent uses it (or visa > versa)? Again, I'm not sure this is the reason but I can find no other > good explanation. I found an explanation! Well, a partial one. If we MAP_PRIVATE a file mapping (like, er those ones up there) and only take read faults on it, we can postpone allocation of the anon_vma indefinitely. But once we take a write fault in that VMA, we need to allocate an anon_vma for it so that we can track the anonymous pages that have been allocated to satisfy the copy-on-write (see do_cow_fault()). However, I think in that caase, we could probably skip the find_mergeable_anon_vma() step. We don't today; we check whether a->vm_file == b->vm_file in anon_vma_compatible, but I wonder if that triggers often.