Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp70059ybl; Tue, 7 Jan 2020 02:21:11 -0800 (PST) X-Google-Smtp-Source: APXvYqzKMBpnFT1YawrwFGZMcY6g2+SAr4h7aRN/drwiWBJYI6Fe3BBQ4ddCZrnufwCw8s02QUL1 X-Received: by 2002:a05:6830:2057:: with SMTP id f23mr121563143otp.110.1578392471664; Tue, 07 Jan 2020 02:21:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578392471; cv=none; d=google.com; s=arc-20160816; b=DKCGVL1EVMZs/nh8iKF+7h4qgVaL88fI/fFZek1Jib+MUeoT5tN0ViG+DItkUg9y45 HpCKTnmqSlfPNi0U6uISs4jZBs/YF7sWXk5pybac/LjcA3di+DDMchAPvTllrLiKwvlI DOvsUIk7gpjo5ZCqiN0ZzcCruwwMu0ELp6eCsNuWoxF7tGoUySdj/T4qlb2v456qYfFY Qi1ORkT7vhYejjqCUFc1Q/8vNGGGoqHy2xutnX+2jknPFo1OPJx9kBwkqyeOJypBk7jx 8ybh77sROYdJ9UizhY9SKJBBRys14GFLOsieo5gtEds1Nk1gOqzMSDM5EVcgDEOzXNya 21Rw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:dkim-signature; bh=tHNiuDWCSNjv1Gat00JoTMFTmnv9MeRMDid0TscrI1I=; b=p/cDsOFbKmM4MZYEkX9fWuo3gziEVZ8Fd+ERoMdX+T9FunaHjnpJQlYrZyE3Vb648t 2BvcXXREzjs/h/LMGjoSyIEE4Z8VYeryJjaOyILo39gjyO1mKX6mOh+P8wXhR0pVFFLj +i9bs1Psfr0kDFS9U5uINJepz5/SeXuWVMSjoquGGxgBkNOKZehfqjt/KVyRWHlj3Gus wvPNIW9ergP8KIqWVXGZWObcBbXPPyh0KUmcEPqnMla87VSBI52xs6DooQfN4UP7l4D8 UFIUC4yRNUEca3OZSDyRknQhTx9b9jRZT/PfyRpTEp3/7hAa0KDu/1S8pa8ZznmEz5oD RDuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@yandex-team.ru header.s=default header.b=hPnA0eLx; 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=NONE sp=NONE dis=NONE) header.from=yandex-team.ru Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w196si31863153oia.135.2020.01.07.02.20.59; Tue, 07 Jan 2020 02:21:11 -0800 (PST) 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=@yandex-team.ru header.s=default header.b=hPnA0eLx; 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=NONE sp=NONE dis=NONE) header.from=yandex-team.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727837AbgAGKUE (ORCPT + 99 others); Tue, 7 Jan 2020 05:20:04 -0500 Received: from forwardcorp1j.mail.yandex.net ([5.45.199.163]:36950 "EHLO forwardcorp1j.mail.yandex.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727084AbgAGKUC (ORCPT ); Tue, 7 Jan 2020 05:20:02 -0500 Received: from mxbackcorp2j.mail.yandex.net (mxbackcorp2j.mail.yandex.net [IPv6:2a02:6b8:0:1619::119]) by forwardcorp1j.mail.yandex.net (Yandex) with ESMTP id 894F32E1479; Tue, 7 Jan 2020 13:19:59 +0300 (MSK) Received: from myt5-70c90f7d6d7d.qloud-c.yandex.net (myt5-70c90f7d6d7d.qloud-c.yandex.net [2a02:6b8:c12:3e2c:0:640:70c9:f7d]) by mxbackcorp2j.mail.yandex.net (mxbackcorp/Yandex) with ESMTP id wR5HWf8AHd-JwPGY61O; Tue, 07 Jan 2020 13:19:59 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1578392399; bh=tHNiuDWCSNjv1Gat00JoTMFTmnv9MeRMDid0TscrI1I=; h=In-Reply-To:Message-ID:References:Date:To:From:Subject:Cc; b=hPnA0eLx475dWXpU6BH9CFAYZAu825ICzNUrgeWh7VJcPFLFT2BdzyIDeG4b4/jhr l0BOvHosrIuuGPGMWMDRLcStODDetoPUODo4zA2P2ZEPjVwWz6t8/lRfl8MAH1hOll lqR9H7AU1SYVHmxjvW1ZhSApH5XfHfN90S9ky2h0= Authentication-Results: mxbackcorp2j.mail.yandex.net; dkim=pass header.i=@yandex-team.ru Received: from unknown (unknown [2a02:6b8:b080:6407::1:5]) by myt5-70c90f7d6d7d.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id 5ah5C5r85j-JwVGiL5m; Tue, 07 Jan 2020 13:19:58 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client certificate not present) Subject: [PATCH v2 2/2] kernel/fork: set VMA's mm/prev/next right after vm_area_dup in dup_mmap From: Konstantin Khlebnikov To: linux-mm@kvack.org, Andrew Morton , linux-kernel@vger.kernel.org, Wei Yang Cc: Rik van Riel , Li Xinhai , "Kirill A. Shutemov" Date: Tue, 07 Jan 2020 13:19:58 +0300 Message-ID: <157839239842.694.2288178566540902852.stgit@buzz> In-Reply-To: <157839239609.694.10268055713935919822.stgit@buzz> References: <157839239609.694.10268055713935919822.stgit@buzz> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Function vm_area_dup() makes exact copy of structure. It's better to stop referencing parent structures because various helpers use these pointers. For example if VM_WIPEONFORK is set then anon_vma_prepare() will try to merge anon_vma with previous and next VMA. Poking parent VMAs and sharing their anon_vma is safe for now but is not expected behavior. Note: this will break commit 4e4a9eb92133 ("mm/rmap.c: reuse mergeable anon_vma as parent when fork") without related fix because it contains several flaws hidden by current initialization sequence in dup_mmap(). Signed-off-by: Konstantin Khlebnikov --- kernel/fork.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/fork.c b/kernel/fork.c index c33626993831..784c9ae56aa9 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -544,10 +544,12 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, tmp = vm_area_dup(mpnt); if (!tmp) goto fail_nomem; + tmp->vm_mm = mm; + tmp->vm_prev = prev; + tmp->vm_next = NULL; retval = vma_dup_policy(mpnt, tmp); if (retval) goto fail_nomem_policy; - tmp->vm_mm = mm; retval = dup_userfaultfd(tmp, &uf); if (retval) goto fail_nomem_anon_vma_fork; @@ -559,7 +561,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, } else if (anon_vma_fork(tmp, mpnt, prev)) goto fail_nomem_anon_vma_fork; tmp->vm_flags &= ~(VM_LOCKED | VM_LOCKONFAULT); - tmp->vm_next = tmp->vm_prev = NULL; file = tmp->vm_file; if (file) { struct inode *inode = file_inode(file); @@ -592,7 +593,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, */ *pprev = tmp; pprev = &tmp->vm_next; - tmp->vm_prev = prev; prev = tmp; __vma_link_rb(mm, tmp, rb_link, rb_parent);