From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: Kiryl Shutsemau <kas@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
"Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Jann Horn <jannh@google.com>, Pedro Falcato <pfalcato@suse.de>,
Brian Geffon <bgeffon@google.com>,
Minchan Kim <minchan@kernel.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH 1/2] mm/mremap: fix locked_vm leak from MREMAP_DONTUNMAP self-merge
Date: Mon, 28 Sep 2026 11:47:20 +0100 [thread overview]
Message-ID: <arpFclevdjhgJGtL@gremlin> (raw)
In-Reply-To: <arpEHIwTy8QxUjI2@thinkstation>
On Mon, Sep 28, 2026 at 11:45:21AM +0100, Kiryl Shutsemau wrote:
> On Sun, Sep 20, 2026 at 03:13:10PM +0100, Lorenzo Stoakes (ARM) wrote:
> > The MREMAP_DONTUNMAP feature is highly unusual in that it permits mremap()
> > operations that keep the original VMA in place.
> >
> > Historically this has led to a lot of bugs where non-obvious interactions
> > occur between existing mremap() operations and the original VMA.
> >
> > Fix another of these - self-merge.
> >
> > Self-merge occurs when a VMA is moved in front of or behind itself and the
> > attributes of the VMA permit such a merge.
> >
> > Practically this can only happen for unfaulted anonymous VMAs due to the
> > page offset equality requirement for merge:
> >
> > |------------|
> > | |
> > | v
> > |...........||-----------||...........|
> > | || unfaulted || |
> > |...........||-----------||...........|
> > ^ |
> > | |
> > |------------|
> >
> > This becomes problematic if the VMA is configured by the user to
> > mlock-on-fault, i.e. the VMA_LOCKED_BIT, VMA_LOCKONFAULT_BIT VMA flags are
> > set.
> >
> > MREMAP_DONTUNMAP clears mlock flags for the source VMA and maintains them
> > for the destination VMA.
> >
> > Self-merge makes this impossible (there is only one VMA) and incorrectly
> > clears the destination VMA's mlock flags.
> >
> > This causes a leak in mm->locked_vm as clearing this flag does not
> > decrement the counter and the VMA no longer has VMA_LOCKED_BIT set so it
> > is not decremented on unmap.
> >
> > Resolve this by simply disallowing a self-merge in this case - the source
> > and destination VMAs are kept distinct and then are able to have distinct
> > mlock() flags.
> >
> > Update dontunmap_complete() to make the now-redundant self-merge check a
> > VM_WARN_ON_ONCE() instead to guard against future regressions.
> >
> > Also update the VMA userland tests to reflect the change.
> >
> > Fixes: e346b3813067 ("mm/mremap: add MREMAP_DONTUNMAP to mremap()")
> > Cc: <stable@vger.kernel.org>
> > Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
>
> Acked-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
Thanks!
>
> One thing: mlock semantics of MREMAP_DONTUNMAP are not documented in the
> man pages, only in the commit that added the flag. It can be surprising
> that the source gets munlocked. I am not even sure it is the right thing
> to do.
It's unavoidable as the source no longer has anything mapped (page tables moved)
and it's not correct for it to be marked as mlock()'d in that case.
Ack on man page update, I'll add to my TODO!
>
> --
> Kiryl Shutsemau / Kirill A. Shutemov
--
Cheers, Lorenzo
next prev parent reply other threads:[~2026-09-28 10:47 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-20 14:13 [PATCH 0/2] mm/mremap: fix two issues with MREMAP_DONTUNMAP Lorenzo Stoakes (ARM)
2026-09-20 14:13 ` [PATCH 1/2] mm/mremap: fix locked_vm leak from MREMAP_DONTUNMAP self-merge Lorenzo Stoakes (ARM)
2026-09-27 14:53 ` Jose A. Perez de Azpillaga
2026-09-28 10:45 ` Kiryl Shutsemau
2026-09-28 10:47 ` Lorenzo Stoakes (ARM) [this message]
2026-09-28 11:00 ` Pedro Falcato
2026-09-20 14:13 ` [PATCH 2/2] mm/mremap: fix locked_vm leak by splitting VMA for MREMAP_DONTUNMAP Lorenzo Stoakes (ARM)
2026-09-27 15:06 ` Jose A. Perez de Azpillaga
2026-09-28 11:01 ` Kiryl Shutsemau
2026-09-28 11:08 ` Pedro Falcato
2026-09-26 20:53 ` [PATCH 0/2] mm/mremap: fix two issues with MREMAP_DONTUNMAP Andrew Morton
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=arpFclevdjhgJGtL@gremlin \
--to=ljs@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=bgeffon@google.com \
--cc=jannh@google.com \
--cc=kas@kernel.org \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=minchan@kernel.org \
--cc=pfalcato@suse.de \
--cc=stable@vger.kernel.org \
--cc=vbabka@kernel.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
all inboxes | Powered by JetHome®