Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 590273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:30:40+00:00 2026-05-13T15:30:40+00:00

The following C code illustrates a problem I’m seeing on Linux 2.6.30.5-43.fc11.x86_64: #include <sys/types.h>

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:30:40+00:00Added an answer on May 13, 2026 at 3:30 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code fragment does nothing, but illustrates the problem. It was extracted from
The problem I have is illustrated in the following code. #include <iostream> #define X
I am struggling with a Scala implicit conversion problem. The following code snippet illustrates
The following diagram illustrates a problem I have encountered creating a Manhattan diagram: Overlapping
My problem can be illustrated in the following example code, which sets up a
Here is some sample code that illustrates the problem. It compiles and runs correctly,
I'm having a problem with __sync_fetch_and_and incorrectly performing. I wrote the following code to
I have the following problem illustrated by the pseudo code below (might not make
Following code iterates through many data-rows, calcs some score per row and then sorts
following code doesn't work with input: 2 7 add Elly 0888424242 add Elly 0883666666

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.