Re: [PATCHv2] mm: Fix warning in move_normal_pmd()

From: Kirill A. Shutemov
Date: Wed Jul 15 2020 - 18:22:26 EST


On Wed, Jul 15, 2020 at 02:43:00PM -0700, Linus Torvalds wrote:
> On Wed, Jul 15, 2020 at 2:31 PM Linus Torvalds
> <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
> >
> > Naresh - don't test that version. The bugs Joel found just make the
> > math wrong, so it won't work.
>
> Here's a new version with the thing that Joel and Kirill both noticed
> hopefully fixed.
>
> But I probably screwed it up again. I guess I should test it, but I
> don't have any really relevant environment (the plain mremap() case
> should have shown the obvious bugs, though, so that's just an excuse
> for my laziness)

Sorry, but the patch is broken.

It overcopies entires in some cases. It doesn't handles correctly if you
try to move PTEs from the middle of VMA.

The test case below rightly sigfaults without the patch when trying access
DST_ADDR[0], but not with the patch. It creates PTE entry at DST_ADDR that
doesn't belong to any VMA. :/

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>

#define SRC_ADDR ((char *)(4UL << 30))
#define DST_ADDR ((char *)(5UL << 30))


int main(void)
{
mmap(SRC_ADDR, 2UL << 20, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
madvise(SRC_ADDR, 2UL << 20, MADV_NOHUGEPAGE);
memset(SRC_ADDR, 0x33, 2UL << 20);

mremap(SRC_ADDR + 4096, 4096, 4096, MREMAP_MAYMOVE | MREMAP_FIXED,
DST_ADDR + 4096);
printf("0: %#x 4096: %#x\n", DST_ADDR[0], DST_ADDR[4096]);
return 0;
}

--
Kirill A. Shutemov