views:

29

answers:

1

The following C code illustrates a problem I'm seeing on Linux 2.6.30.5-43.fc11.x86_64:

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main() {
    char buf[1024];
    void *base;
    int fd;
    size_t pagesz = sysconf(_SC_PAGE_SIZE);

    fd = open("<some file, at least 4*pagesz in length>", O_RDONLY);
    if (fd < 0) {
       perror("open");
       return 1;
    }

    base = mmap(0, 4*pagesz, PROT_READ, MAP_SHARED, fd, 0);
    if (base < 0) {
        perror("mmap");
        close(fd);
        return 1;
    }

    memcpy(buf, (char*)base + 2*pagesz, 1024);

    if (remap_file_pages(base, pagesz, 0, 2, 0) < 0) {
        perror("remap_file_pages");
        munmap(base, 4*pagesz);
        close(fd);
        return 1;
    }

    printf("%d\n", memcmp(buf, base, 1024));

    munmap(base, 4*pagesz);
    close(fd);
    return 0;
}

This always fails with remap_file_pages() returning -1 and errno set to EINVAL. Looking at the kernel source I can see all the conditions in remap_file_pages() where it might fail but none of them seem to apply to my example. What's going on?

+2  A: 

It's caused by the file being opened O_RDONLY. If you change the open mode to O_RDWR, it works (even if the mmap() still specifies just PROT_READ).

This code in do_mmap_pgoff is the root cause - it only marks the vma as VM_SHARED if the file was opened for writing:

vm_flags |= VM_SHARED | VM_MAYSHARE;
if (!(file->f_mode & FMODE_WRITE))
    vm_flags &= ~(VM_MAYWRITE | VM_SHARED);

So in remap_file_pages(), you fail on the first check:

if (!vma || !(vma->vm_flags & VM_SHARED))
    goto out;
caf
That works perfectly, thank you.
Neil Gall