public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: cygwin@cygwin.com
Subject: Re: Atomic mmap replacement
Date: Mon, 19 Feb 2018 09:01:00 -0000	[thread overview]
Message-ID: <20180219090042.GC3417@calimero.vinschen.de> (raw)
In-Reply-To: <66bf4f86-4618-b9a3-3e33-2c240b9204d0@cornell.edu>

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

On Feb 17 22:37, Ken Brown wrote:
> Some code in emacs wants to reserve a chunk of address space with a big
> PROT_NONE anonymous mapping, and then carve it up into separate mappings
> associated to segments of a file.  This fails on Cygwin.  Here's a test case
> that illustrates the problem:
> 
> $ truncate -s 64k foo
> 
> $ cat mmap_test.c
> #include <stdio.h>
> #include <stdlib.h>
> #include <fcntl.h>
> #include <sys/mman.h>
> 
> const size_t page_size = 64 * 1024;
> 
> int
> main ()
> {
>   void *mem = mmap (NULL, 2 * page_size,
>                     PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
>   if (mem == MAP_FAILED)
>     {
>       perror ("mmap");
>       exit (1);
>     }
>   int fd = open ("foo", O_RDONLY);
>   void *res = mmap (mem, page_size, PROT_READ | PROT_WRITE,
>                     MAP_PRIVATE | MAP_FIXED, fd, 0);
>   if (res == MAP_FAILED)
>     {
>       perror ("mmap");
>       exit (2);
>     }
> }
> 
> $ gcc mmap_test.c
> 
> $ ./a
> mmap: Invalid argument
> 
> $ echo $?
> 2
> 
> Is this a bug, or is it simply a limitation of Cygwin's mmap?  If the
> latter, is there a simple workaround?

Several limitations in the Windows kernel disallow this:

- It doesn't allow to unmap parts of a map, only the entire map as a
  whole.
  
  Cygwin has a workaround: If you unmap parts of a map it just keeps
  track of this and sets the protection of the affected pages to
  PAGE_NOACCESS.  In case of anonymous mappings, it even recycles them
  potentially for other mappings.

- It also disallows to re-map any allocated or mapped mamory for another
  purpose.

So this part of the POSIX specs for mmap:

  "The mapping established by mmap() shall replace any previous mappings
   for those whole pages containing any part of the address space of the
   process starting at pa and continuing for len bytes"

can't be implemented with Windows means.

The only workaround possible would be to handle this *exact* scenario as
a special case in Cygwin's mmap:  If the new mapping falls in the middle
of an existing mapping and if the original mapping was an anonymous
mapping with PROT_NONE page protection, then

- unmap the old mapping
- remap the unaffected parts as separate anonymous mapping
- map the affected parts for the requested file mapping

This is pretty complicated and I'm not hot on implementing it.  If it's
really required we can take a look of course.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2018-02-19  9:01 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-18  3:37 Ken Brown
2018-02-19  9:01 ` Corinna Vinschen [this message]
2018-02-19 13:22   ` Ken Brown
2018-02-19 17:19     ` Corinna Vinschen
2018-02-19 22:33       ` Ken Brown
2018-02-20 10:31         ` Corinna Vinschen
2018-02-20  1:04       ` John Hood
2018-02-20 10:47         ` Corinna Vinschen

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=20180219090042.GC3417@calimero.vinschen.de \
    --to=corinna-cygwin@cygwin.com \
    --cc=cygwin@cygwin.com \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).