From: Andrew Morton <akpm@linux-foundation.org>
To: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Cc: "Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>, Jann Horn <jannh@google.com>,
Pedro Falcato <pfalcato@suse.de>,
Li Xinhai <lixinhai.lxh@gmail.com>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
syzbot+f12658786a4153df5113@syzkaller.appspotmail.com,
stable@vger.kernel.org
Subject: Re: [PATCH] mm/mremap: reset unfaulted VMA page offset for MREMAP_DONTUNMAP
Date: Wed, 26 Aug 2026 19:26:42 -0700 [thread overview]
Message-ID: <20260826192642.980c3aba921bf20cef425591@linux-foundation.org> (raw)
In-Reply-To: <20260825-fix-mremap-dontunmap-pgoff-v1-1-39a40b2c98b3@kernel.org>
On Tue, 25 Aug 2026 08:55:26 +0100 "Lorenzo Stoakes (ARM)" <ljs@kernel.org> wrote:
> Uniquely an mremap() invocation using the MREMAP_DONTUNMAP flag can reset
> a faulted VMA into an unfaulted one.
>
> It does so after the page tables have been moved to the copied VMA with
> MREMAP_DONTUNMAP leaving the old VMA in place which is naturally unfaulted
> as the page tables it had are no longer present.
>
> However, in doing so, it violates the invariant that the anonymous page
> offset of an unfaulted VMA is vma->vm_start >> PAGE_SHIFT.
>
> This is because a VMA may have been faulted in, mremap()'d (causing a delta
> between its page offset and vma->vm_start >> PAGE_SHIFT), and then
> mremap()'d again with MREMAP_DONTUNMAP resulting in the unfaulting.
>
> This condition is a violation of a fundamental assumption in mm, but now
> also triggers an assert in assert_sane_pgoff() which explicitly checks for
> this condition.
>
> Correct it by resetting the VMA's page offset at the point of completing
> the MREMAP_DONTUNMAP operation.
Thanks. I'll park this in mm-new until mm.git is all merged up
(simplifying my life..)
Unrelatedly, Sashiko thinks we're messing up locked_vm accounting with
MREMAP_DONTUNMAP on a locked VMA.
https://sashiko.dev/#/patchset/20260825-fix-mremap-dontunmap-pgoff-v1-1-39a40b2c98b3@kernel.org
I had Sashiko write code to demonstrate this but am too lazy to test it
on a current kernel. If someone could oblige?
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
/* Read VMLck (in kB) from /proc/self/status */
static long get_vmlck_kb(void) {
FILE *f = fopen("/proc/self/status", "r");
if (!f) {
perror("fopen /proc/self/status");
return -1;
}
char line[256];
long vmlck = -1;
while (fgets(line, sizeof(line), f)) {
if (strncmp(line, "VMLck:", 6) == 0) {
sscanf(line + 6, "%ld", &vmlck);
break;
}
}
fclose(f);
return vmlck;
}
int main(void) {
size_t size = 4096 * 10; // 40 kB
long initial_vmlck, post_mlock, post_mremap, post_munmap;
initial_vmlck = get_vmlck_kb();
printf("[1] Initial VMLck: %ld kB\n", initial_vmlck);
/* 1. Allocate initial VMA */
void *addr = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (addr == MAP_FAILED) {
perror("mmap initial");
return 1;
}
/* 2. Lock the VMA (increments mm->locked_vm) */
if (mlock(addr, size) != 0) {
perror("mlock");
return 1;
}
post_mlock = get_vmlck_kb();
printf("[2] Post-mlock VMLck: %ld kB (+%ld kB)\n",
post_mlock, post_mlock - initial_vmlck);
/* 3. mremap with MREMAP_DONTUNMAP
* move_vma() increments mm->locked_vm for the destination VMA,
* while dontunmap_complete() clears VMA_LOCKED_MASK on source VMA
* without decrementing mm->locked_vm.
*/
void *new_addr = mremap(addr, size, size,
MREMAP_MAYMOVE | MREMAP_DONTUNMAP, NULL);
if (new_addr == MAP_FAILED) {
perror("mremap MREMAP_DONTUNMAP");
return 1;
}
post_mremap = get_vmlck_kb();
printf("[3] Post-mremap VMLck: %ld kB (+%ld kB from initial)\n",
post_mremap, post_mremap - initial_vmlck);
/* 4. Unmap source VMA
* Since VMA_LOCKED_BIT was cleared on source VMA,
* munmap fails to decrement mm->locked_vm for this region.
*/
munmap(addr, size);
post_munmap = get_vmlck_kb();
printf("[4] Post-munmap source: %ld kB\n", post_munmap);
/* 5. Clean up destination VMA */
munmap(new_addr, size);
long final_vmlck = get_vmlck_kb();
printf("[5] Final VMLck: %ld kB\n", final_vmlck);
/* Evaluation */
printf("\n--- Result ---\n");
if (final_vmlck > initial_vmlck) {
printf("BUG DEMONSTRATED: Leaked %ld kB in mm->locked_vm counter.\n",
final_vmlck - initial_vmlck);
} else {
printf("NO LEAK: mm->locked_vm returned to initial state.\n");
}
return 0;
}
next prev parent reply other threads:[~2026-08-27 2:26 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-25 7:55 [PATCH] mm/mremap: reset unfaulted VMA page offset for MREMAP_DONTUNMAP Lorenzo Stoakes (ARM)
2026-08-26 15:15 ` Kunwu Chan
2026-08-26 15:27 ` Lorenzo Stoakes (ARM)
2026-08-27 2:26 ` Andrew Morton [this message]
2026-08-27 6:26 ` Kunwu Chan
2026-08-27 8:32 ` Vlastimil Babka (SUSE)
2026-08-27 8:36 ` Lorenzo Stoakes (ARM)
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=20260826192642.980c3aba921bf20cef425591@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=jannh@google.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lixinhai.lxh@gmail.com \
--cc=ljs@kernel.org \
--cc=pfalcato@suse.de \
--cc=stable@vger.kernel.org \
--cc=syzbot+f12658786a4153df5113@syzkaller.appspotmail.com \
--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