Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp22113053ybl; Mon, 6 Jan 2020 18:33:16 -0800 (PST) X-Google-Smtp-Source: APXvYqx+w0tHl6kD1fiV+V3yTpBT6797y/wYraSCCiE+toNVg/lc8jKkaG6DDJ14s38UU5tfWl2L X-Received: by 2002:aca:c08b:: with SMTP id q133mr6257734oif.46.1578364396756; Mon, 06 Jan 2020 18:33:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578364396; cv=none; d=google.com; s=arc-20160816; b=mGuebnylZdvBAe8Di1kljHDBuWQrPE+aKT+w2AfthZY9OZ3oOB51B9JOaYCqlmLPru 64W2ZbWAayfGltS2n9WZaOFgHcD6wqGGYktqHxb2H73AWtTDodlriUkLwzv1W2h+XcQ/ kpDp9WuIcR3wCaYEcYkdkpBcvMlvCcEOq2rvi2EMJN9XRAHiO90PP/bKjhkkn/P4QyH/ Qz3ITkNfAOGw3DKemMw2RhZtw/0Bg3Oy7CTUE4zNGXiqlVSu0IqJW2RNi/WeIJ4xdp4i fofCzuXI0G+z7GMgWv9Q4eYAcUVqpgXQ6lv7JKzFeRuzaqG+9sVnYg+401Z+ImThnPyn j4NQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:reply-to:message-id :subject:cc:to:from:date; bh=h7q6y6kMyGilbayUpmvQ/IN+4i2+jAoSrkZGkC3U4PY=; b=pHcUv0JpqcRVa6kRt9FXd+W7bE0GHcQ3IJ7/hOcyQsleT8byj+keI0Il3QqfuaPq3Z 2omWt7ZKS5iRavCwlPDivm3IpY2da2xU9gWCgK8v81TV3QHMfndy88yaG6SQNI9XKzKq P3y7/bB5+CZ/W3e242Xu9FkV4jzO2Ad40TmBKhe7vlgFXWN6Gq7DGijdFYoRdTCvWV4d b6F+zU8SufboMxWCGHYSxGWowDtvBg+SauYh2SxpEYhDZcJITkYifZTFStu1jYizi1Mi g57jIi0K4AGrzJJRrMULpxPPJbF1oQ4zqmHAlsjPxpgbnz+K9BPuKn+NIRCYrJrcnKmJ pGBg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b1si30339750oiy.9.2020.01.06.18.33.03; Mon, 06 Jan 2020 18:33:16 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727481AbgAGCcT (ORCPT + 99 others); Mon, 6 Jan 2020 21:32:19 -0500 Received: from mga18.intel.com ([134.134.136.126]:61480 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727377AbgAGCcT (ORCPT ); Mon, 6 Jan 2020 21:32:19 -0500 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Jan 2020 18:32:18 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,404,1571727600"; d="scan'208";a="422327324" Received: from richard.sh.intel.com (HELO localhost) ([10.239.159.54]) by fmsmga006.fm.intel.com with ESMTP; 06 Jan 2020 18:32:17 -0800 Date: Tue, 7 Jan 2020 10:32:22 +0800 From: Wei Yang To: Konstantin Khlebnikov Cc: linux-mm@kvack.org, Andrew Morton , linux-kernel@vger.kernel.org, Wei Yang , "Kirill A. Shutemov" Subject: Re: [PATCH] mm/rmap: fix reusing mergeable anon_vma as parent when fork Message-ID: <20200107023221.GC15341@richard> Reply-To: Wei Yang References: <157830736034.8148.7070851958306750616.stgit@buzz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <157830736034.8148.7070851958306750616.stgit@buzz> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 06, 2020 at 01:42:40PM +0300, Konstantin Khlebnikov wrote: >This fixes couple misconceptions in commit 4e4a9eb92133 ("mm/rmap.c: reuse >mergeable anon_vma as parent when fork"). > >First problem caused by initialization order in dup_mmap(): vma->vm_prev >is set after calling anon_vma_fork(). Thus in anon_vma_fork() it points to >previous VMA in parent mm. This is fixed by rearrangement in dup_mmap(). > You are right, I missed this point. >If in parent VMAs: SRC1 SRC2 .. SRCn share anon-vma ANON0, then after fork >before all patches in child process related VMAs: DST1 DST2 .. DSTn will >use different anon-vmas: ANON1 ANON2 .. ANONn. Before this patch only DST1 >will fork new ANON1 and following DST2 .. DSTn will share parent's ANON0. >With this patch DST1 will create new ANON1 and DST2 .. DSTn will share it. > >Also this patch moves sharing logic out of anon_vma_clone() into more >specific anon_vma_fork() because this supposed to work only at fork(). >Function anon_vma_clone() is more generic is also used at splitting VMAs. > >Second problem is hidden behind first one: assumption "Parent has vm_prev, >which implies we have vm_prev" is wrong if first VMA in parent mm has set >flag VM_DONTCOPY. Luckily prev->anon_vma doesn't dereference NULL pointer >because in current code 'prev' actually is same as 'pprev'. To avoid that >this patch just checks pointer and compares vm_start to verify relation >between previous VMAs in parent and child. > Correct here too. >Signed-off-by: Konstantin Khlebnikov >Fixes: 4e4a9eb92133 ("mm/rmap.c: reuse mergeable anon_vma as parent when fork") >--- > kernel/fork.c | 4 ++-- > mm/rmap.c | 25 ++++++++++++------------- > 2 files changed, 14 insertions(+), 15 deletions(-) > >diff --git a/kernel/fork.c b/kernel/fork.c >index 2508a4f238a3..04ee5e243f65 100644 >--- a/kernel/fork.c >+++ b/kernel/fork.c >@@ -548,6 +548,8 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, > if (retval) > goto fail_nomem_policy; > tmp->vm_mm = mm; >+ tmp->vm_prev = prev; /* anon_vma_fork use this */ >+ tmp->vm_next = NULL; How about pass prev to anon_vma_fork()? So that we limit all the difference in anon_vma_fork(). > 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)) > 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); >diff --git a/mm/rmap.c b/mm/rmap.c >index b3e381919835..77b3aa38d5c2 100644 >--- a/mm/rmap.c >+++ b/mm/rmap.c >@@ -269,19 +269,6 @@ int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src) > { > struct anon_vma_chain *avc, *pavc; > struct anon_vma *root = NULL; >- struct vm_area_struct *prev = dst->vm_prev, *pprev = src->vm_prev; >- >- /* >- * If parent share anon_vma with its vm_prev, keep this sharing in in >- * child. >- * >- * 1. Parent has vm_prev, which implies we have vm_prev. >- * 2. Parent and its vm_prev have the same anon_vma. >- */ >- if (!dst->anon_vma && src->anon_vma && >- pprev && pprev->anon_vma == src->anon_vma) >- dst->anon_vma = prev->anon_vma; >- > > list_for_each_entry_reverse(pavc, &src->anon_vma_chain, same_vma) { > struct anon_vma *anon_vma; >@@ -334,6 +321,7 @@ int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src) > */ > int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) > { >+ struct vm_area_struct *prev = vma->vm_prev, *pprev = pvma->vm_prev; > struct anon_vma_chain *avc; > struct anon_vma *anon_vma; > int error; >@@ -345,6 +333,17 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) > /* Drop inherited anon_vma, we'll reuse existing or allocate new. */ > vma->anon_vma = NULL; > >+ /* >+ * If parent shares anon_vma with its vm_prev, keep this sharing. >+ * >+ * Previous VMA could be missing or not match previuos in parent >+ * if VM_DONTCOPY is set: compare vm_start to avoid this case. >+ */ >+ if (pvma->anon_vma && pprev && prev && >+ pprev->anon_vma == pvma->anon_vma && >+ pprev->vm_start == prev->vm_start) >+ vma->anon_vma = prev->anon_vma; >+ > /* > * First, attach the new VMA to the parent VMA's anon_vmas, > * so rmap can find non-COWed pages in child processes. -- Wei Yang Help you, Help me