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 X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 68629C432C0 for ; Sun, 1 Dec 2019 01:50:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2406E206F0 for ; Sun, 1 Dec 2019 01:50:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="dmIY41gK" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2406E206F0 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C89976B029A; Sat, 30 Nov 2019 20:50:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C3B6F6B029C; Sat, 30 Nov 2019 20:50:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B50736B029D; Sat, 30 Nov 2019 20:50:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0214.hostedemail.com [216.40.44.214]) by kanga.kvack.org (Postfix) with ESMTP id 9FFBA6B029A for ; Sat, 30 Nov 2019 20:50:58 -0500 (EST) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with SMTP id 68DBC824999B for ; Sun, 1 Dec 2019 01:50:58 +0000 (UTC) X-FDA: 76214894196.10.dock18_873737fd534c X-HE-Tag: dock18_873737fd534c X-Filterd-Recvd-Size: 5226 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf45.hostedemail.com (Postfix) with ESMTP for ; Sun, 1 Dec 2019 01:50:57 +0000 (UTC) Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D59B6206E1; Sun, 1 Dec 2019 01:50:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1575165057; bh=1ps3QRFuKzUBB60qg0tSnlO8FbdgG53ScwlY77rMywA=; h=Date:From:To:Subject:From; b=dmIY41gKhIYvL6bwLxCqvBNuPPGtqg14BHdMyANK/LHnl0XwPLaLZqvj9ZeIh1Dhn yBj3qKjmA4jWewWzOgcu5CZKtC5MvrfBhDfIbxSnJGMvXSAjTHXrFmFqxsZrW97z40 GcZphKg0IT9VqCGb3HoPmOp97EavZ6qh7CSDpAtA= Date: Sat, 30 Nov 2019 17:50:56 -0800 From: akpm@linux-foundation.org To: akpm@linux-foundation.org, cai@lca.pw, jglisse@redhat.com, khlebnikov@yandex-team.ru, kirill.shutemov@linux.intel.com, linux-mm@kvack.org, mike.kravetz@oracle.com, mm-commits@vger.kernel.org, richardw.yang@linux.intel.com, riel@surriel.com, shakeelb@google.com, torvalds@linux-foundation.org Subject: [patch 034/158] mm/rmap.c: don't reuse anon_vma if we just want a copy Message-ID: <20191201015056.UHNvLWuJ8%akpm@linux-foundation.org> User-Agent: s-nail v14.8.16 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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: =46rom: Wei Yang Subject: mm/rmap.c: don't reuse anon_vma if we just want a copy Before commit 7a3ef208e662 ("mm: prevent endless growth of anon_vma hierarchy"), anon_vma_clone() doesn't change dst->anon_vma. While after this commit, anon_vma_clone() will try to reuse an exist one on forking. But this commit go a little bit further for the case not forking.=20 anon_vma_clone() is called from __vma_split(), __split_vma(), copy_vma() and anon_vma_fork(). For the first three places, the purpose here is get a copy of src and we don't expect to touch dst->anon_vma even it is NULL.= =20 While after that commit, it is possible to reuse an anon_vma when dst->anon_vma is NULL. This is not we intend to have. This patch stops reuse of anon_vma for non-fork cases. Link: http://lkml.kernel.org/r/20191011072256.16275-1-richardw.yang@linux.i= ntel.com Fixes: 7a3ef208e662 ("mm: prevent endless growth of anon_vma hierarchy") Signed-off-by: Wei Yang Acked-by: Konstantin Khlebnikov Cc: Kirill A. Shutemov Cc: "J=C3=A9r=C3=B4me Glisse" Cc: Mike Kravetz Cc: Rik van Riel Cc: Qian Cai Cc: Shakeel Butt Signed-off-by: Andrew Morton --- mm/rmap.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) --- a/mm/rmap.c~mm-rmapc-dont-reuse-anon_vma-if-we-just-want-a-copy +++ a/mm/rmap.c @@ -251,13 +251,19 @@ static inline void unlock_anon_vma_root( * Attach the anon_vmas from src to dst. * Returns 0 on success, -ENOMEM on failure. * - * If dst->anon_vma is NULL this function tries to find and reuse existing - * anon_vma which has no vmas and only one child anon_vma. This prevents - * degradation of anon_vma hierarchy to endless linear chain in case of - * constantly forking task. On the other hand, an anon_vma with more than = one - * child isn't reused even if there was no alive vma, thus rmap walker has= a - * good chance of avoiding scanning the whole hierarchy when it searches w= here - * page is mapped. + * anon_vma_clone() is called by __vma_split(), __split_vma(), copy_vma() = and + * anon_vma_fork(). The first three want an exact copy of src, while the l= ast + * one, anon_vma_fork(), may try to reuse an existing anon_vma to prevent + * endless growth of anon_vma. Since dst->anon_vma is set to NULL before c= all, + * we can identify this case by checking (!dst->anon_vma && src->anon_vma). + * + * If (!dst->anon_vma && src->anon_vma) is true, this function tries to fi= nd + * and reuse existing anon_vma which has no vmas and only one child anon_v= ma. + * This prevents degradation of anon_vma hierarchy to endless linear chain= in + * case of constantly forking task. On the other hand, an anon_vma with mo= re + * than one child isn't reused even if there was no alive vma, thus rmap + * walker has a good chance of avoiding scanning the whole hierarchy when = it + * searches where page is mapped. */ int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src) { @@ -287,8 +293,8 @@ int anon_vma_clone(struct vm_area_struct * will always reuse it. Root anon_vma is never reused: * it has self-parent reference and at least one child. */ - if (!dst->anon_vma && anon_vma !=3D src->anon_vma && - anon_vma->degree < 2) + if (!dst->anon_vma && src->anon_vma && + anon_vma !=3D src->anon_vma && anon_vma->degree < 2) dst->anon_vma =3D anon_vma; } if (dst->anon_vma) _