From patchwork Sun Dec 1 01:50:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 11268245 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 327A2112B for ; Sun, 1 Dec 2019 01:51:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E9C83206F0 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 E9C83206F0 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) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C3B6F6B029C; Sat, 30 Nov 2019 20:50:58 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org 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-Original-To: linux-mm@kvack.org 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-Spam-Summary: 2,0,0,7885f43804138749,d41d8cd98f00b204,akpm@linux-foundation.org,:akpm@linux-foundation.org:cai@lca.pw:jglisse@redhat.com:khlebnikov@yandex-team.ru:kirill.shutemov@linux.intel.com::mike.kravetz@oracle.com:mm-commits@vger.kernel.org:richardw.yang@linux.intel.com:riel@surriel.com:shakeelb@google.com:torvalds@linux-foundation.org,RULES_HIT:41:152:355:379:800:960:967:973:988:989:1260:1263:1277:1311:1313:1314:1345:1381:1431:1437:1513:1515:1516:1518:1521:1535:1543:1593:1594:1605:1711:1730:1747:1777:1792:1969:2195:2198:2199:2200:2393:2525:2553:2559:2563:2682:2685:2689:2693:2859:2899:2902:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4250:4321:5007:6119:6261:6653:6737:7576:7903:8599:8660:8666:9025:9207:9545:10004:10400:10913:11026:11658:11914:12043:12048:12296:12297:12438:12517:12519:12555:12679:12783:12986:13148:13151:13228:13230:14096:14097:14181:14721: 14849:14 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 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: From: 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. 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. 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.intel.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érôme 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 where - * 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 last + * 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 call, + * 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 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 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 != src->anon_vma && - anon_vma->degree < 2) + if (!dst->anon_vma && src->anon_vma && + anon_vma != src->anon_vma && anon_vma->degree < 2) dst->anon_vma = anon_vma; } if (dst->anon_vma)