mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* MREMAP_DONTUNMAP corrupts initial mapping
@ 2023-03-30 12:38 stsp
  2023-03-30 14:45 ` stsp
  0 siblings, 1 reply; 4+ messages in thread
From: stsp @ 2023-03-30 12:38 UTC (permalink / raw)
  To: Linux Kernel Mailing List; +Cc: linux-mm

[-- Attachment #1: Type: text/plain, Size: 320 bytes --]

Hello.

Attached is a small test-case that
demonstrates the problem.
The problem happens if you change
some data in a file-backed private
mapping and then use mremap on
it with MREMAP_DONTUNMAP flag.
The result is:
- destination copy is valid
- source copy restored from the original file

So the 2 copies do not match.

[-- Attachment #2: mretst.c --]
[-- Type: text/x-csrc, Size: 838 bytes --]

#define _GNU_SOURCE
#include <sys/mman.h>
#include <assert.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif

int main(void)
{
    const char *file = "mretst.c";
    char *addr, *addr2;
    int fd;
    char c;

    fd = open (file, O_RDONLY);
    if (fd == -1)
        return EXIT_FAILURE;
    addr = mmap (NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
    close(fd);
    if (addr == MAP_FAILED)
        return EXIT_FAILURE;
    c = addr[0];
    assert(addr[0]);
    addr[0] = 0;
    assert(!addr[0]);
    addr2 = mremap (addr, PAGE_SIZE, PAGE_SIZE,
                    MREMAP_MAYMOVE | MREMAP_DONTUNMAP);
    assert(!addr2[0]);
    if (addr[0] == c)
        printf("Test FAILED\n");
    else
        printf("Test PASSED\n");
    return 0;
}

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-03-31  7:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-30 12:38 MREMAP_DONTUNMAP corrupts initial mapping stsp
2023-03-30 14:45 ` stsp
2023-03-30 18:42   ` Lorenzo Stoakes
2023-03-31  6:53     ` stsp

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®