linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: "Liam R. Howlett" <Liam.Howlett@Oracle.com>
To: Vlastimil Babka <vbabka@suse.cz>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	maple-tree@lists.infradead.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, Jann Horn <jannh@google.com>,
	Lorenzo Stoakes <lstoakes@gmail.com>,
	Suren Baghdasaryan <surenb@google.com>,
	Matthew Wilcox <willy@infradead.org>,
	stable@vger.kernel.org
Subject: Re: [PATCH 2/3] mmap: Fix error paths with dup_anon_vma()
Date: Fri, 29 Sep 2023 10:52:48 -0400	[thread overview]
Message-ID: <20230929145248.uzrxislyblgxh2j3@revolver> (raw)
In-Reply-To: <437896e6-c54e-4c5a-f1af-46d91ea6f155@suse.cz>

* Vlastimil Babka <vbabka@suse.cz> [230929 06:08]:
> On 9/27/23 18:07, Liam R. Howlett wrote:
> > When the calling function fails after the dup_anon_vma(), the
> > duplication of the anon_vma is not being undone.  Add the necessary
> > unlink_anon_vma() call to the error paths that are missing them.
> > 
> > This issue showed up during inspection of the error path in vma_merge()
> > for an unrelated vma iterator issue.
> > 
> > Users may experience increased memory usage, which may be problematic as
> > the failure would likely be caused by a low memory situation.
> > 
> > Fixes: d4af56c5c7c6 ("mm: start tracking VMAs with maple tree")
> > Cc: stable@vger.kernel.org
> > Cc: Jann Horn <jannh@google.com>
> > Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
> > ---
> >  mm/mmap.c | 20 ++++++++++++++------
> >  1 file changed, 14 insertions(+), 6 deletions(-)
> > 
> > diff --git a/mm/mmap.c b/mm/mmap.c
> > index b5bc4ca9bdc4..2f0ee489db8a 100644
> > --- a/mm/mmap.c
> > +++ b/mm/mmap.c
> > @@ -587,7 +587,7 @@ static inline void vma_complete(struct vma_prepare *vp,
> >   * Returns: 0 on success.
> >   */
> >  static inline int dup_anon_vma(struct vm_area_struct *dst,
> > -			       struct vm_area_struct *src)
> > +		struct vm_area_struct *src, struct vm_area_struct **dup)
> >  {
> >  	/*
> >  	 * Easily overlooked: when mprotect shifts the boundary, make sure the
> > @@ -597,6 +597,7 @@ static inline int dup_anon_vma(struct vm_area_struct *dst,
> >  	if (src->anon_vma && !dst->anon_vma) {
> >  		vma_assert_write_locked(dst);
> >  		dst->anon_vma = src->anon_vma;
> > +		*dup = dst;
> >  		return anon_vma_clone(dst, src);
> 
> So the code is simpler that way and seems current callers are fine, but
> shouldn't we rather only assign *dup if the clone succeeds?

Fair point.  I'll address this in v3.

> 
> >  	}
> >  
> > @@ -624,6 +625,7 @@ int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma,
> >  	       unsigned long start, unsigned long end, pgoff_t pgoff,
> >  	       struct vm_area_struct *next)
> >  {
> > +	struct vm_area_struct *anon_dup = NULL;
> >  	bool remove_next = false;
> >  	struct vma_prepare vp;
> >  
> > @@ -633,7 +635,7 @@ int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma,
> >  
> >  		remove_next = true;
> >  		vma_start_write(next);
> > -		ret = dup_anon_vma(vma, next);
> > +		ret = dup_anon_vma(vma, next, &anon_dup);
> >  		if (ret)
> >  			return ret;
> >  	}
> > @@ -661,6 +663,8 @@ int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma,
> >  	return 0;
> >  
> >  nomem:
> > +	if (anon_dup)
> > +		unlink_anon_vmas(anon_dup);
> >  	return -ENOMEM;
> >  }
> >  
> > @@ -860,6 +864,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm,
> >  {
> >  	struct vm_area_struct *curr, *next, *res;
> >  	struct vm_area_struct *vma, *adjust, *remove, *remove2;
> > +	struct vm_area_struct *anon_dup = NULL;
> >  	struct vma_prepare vp;
> >  	pgoff_t vma_pgoff;
> >  	int err = 0;
> > @@ -927,18 +932,18 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm,
> >  		vma_start_write(next);
> >  		remove = next;				/* case 1 */
> >  		vma_end = next->vm_end;
> > -		err = dup_anon_vma(prev, next);
> > +		err = dup_anon_vma(prev, next, &anon_dup);
> >  		if (curr) {				/* case 6 */
> >  			vma_start_write(curr);
> >  			remove = curr;
> >  			remove2 = next;
> >  			if (!next->anon_vma)
> > -				err = dup_anon_vma(prev, curr);
> > +				err = dup_anon_vma(prev, curr, &anon_dup);
> >  		}
> >  	} else if (merge_prev) {			/* case 2 */
> >  		if (curr) {
> >  			vma_start_write(curr);
> > -			err = dup_anon_vma(prev, curr);
> > +			err = dup_anon_vma(prev, curr, &anon_dup);
> >  			if (end == curr->vm_end) {	/* case 7 */
> >  				remove = curr;
> >  			} else {			/* case 5 */
> > @@ -954,7 +959,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm,
> >  			vma_end = addr;
> >  			adjust = next;
> >  			adj_start = -(prev->vm_end - addr);
> > -			err = dup_anon_vma(next, prev);
> > +			err = dup_anon_vma(next, prev, &anon_dup);
> >  		} else {
> >  			/*
> >  			 * Note that cases 3 and 8 are the ONLY ones where prev
> > @@ -1018,6 +1023,9 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm,
> >  	return res;
> >  
> >  prealloc_fail:
> > +	if (anon_dup)
> > +		unlink_anon_vmas(anon_dup);
> > +
> >  anon_vma_fail:
> >  	if (merge_prev)
> >  		vma_next(vmi);
> 


  reply	other threads:[~2023-09-29 14:53 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-27 16:07 [PATCH 0/3] Fixes for vma_merge() error path Liam R. Howlett
2023-09-27 16:07 ` [PATCH 1/3] mmap: Fix vma_iterator in error path of vma_merge() Liam R. Howlett
2023-09-27 17:14   ` Andrew Morton
2023-09-27 17:26     ` Liam R. Howlett
2023-09-28 17:06       ` Liam R. Howlett
2023-09-27 20:06   ` Matthew Wilcox
2023-09-27 22:42     ` Liam R. Howlett
2023-09-29  9:52   ` Vlastimil Babka
2023-09-29 14:41     ` Liam R. Howlett
2023-09-27 16:07 ` [PATCH 2/3] mmap: Fix error paths with dup_anon_vma() Liam R. Howlett
2023-09-29 10:07   ` Vlastimil Babka
2023-09-29 14:52     ` Liam R. Howlett [this message]
2023-09-27 16:07 ` [PATCH 3/3] mmap: Add clarifying comment to vma_merge() code Liam R. Howlett

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230929145248.uzrxislyblgxh2j3@revolver \
    --to=liam.howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=jannh@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lstoakes@gmail.com \
    --cc=maple-tree@lists.infradead.org \
    --cc=stable@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=vbabka@suse.cz \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox