X-Recipient: archive-cygwin@delorie.com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:date:from:to:subject:message-id:reply-to
	:references:mime-version:content-type:in-reply-to; q=dns; s=
	default; b=BVMk9BNP5hgSgoLmCRMSQ9pyGMVM1V3xgjaez0QF9NrLMAanS5OOb
	6z0PhDjXju5osFlQF1fGgAJPvPsbavXZ+JDqqGlQ/4seRGc4uEGPOJtlr7c/ds3K
	VAYVukMi8CwW1z1KKnCWNX/+uyn8IgL0UExOnNqrEOZ/nds6AiAjSs=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:date:from:to:subject:message-id:reply-to
	:references:mime-version:content-type:in-reply-to; s=default;
	 bh=Rln31+CktzNaDhM9IuaUpX/uUcM=; b=b7EJWbBxkE04pEocaR/nzR4OpeeX
	qohBvPBARK2/dBdxb5y0CMjcm3j8kVKE4jesZ2Xp1iTrInOZ5i7hd8EImCPATYF5
	z+j5cxDSdVVKFkTnTou9ffE7hW1OzBw+vU/qATbNRFy+RZ02lztU04w8gHHDEZwQ
	FSdzi9Cj6H2+kRE=
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.3.1
Date: Wed, 24 Apr 2013 14:50:18 +0200
From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: cygwin@cygwin.com
Subject: Re: mmap(MAP_FIXED) vs mprotect
Message-ID: <20130424125018.GA21193@calimero.vinschen.de>
Reply-To: cygwin@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
References: <5177CA05.3010500@cs.utoronto.ca>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
In-Reply-To: <5177CA05.3010500@cs.utoronto.ca>
User-Agent: Mutt/1.5.21 (2010-09-15)

On Apr 24 08:03, Ryan Johnson wrote:
> Hi all,
> 
> I'm trying to port a linux program that uses mmap to implement a
> growable array; the ideas is to mmap(PROT_NONE, MAP_NORESERVE) a
> chunk of address space (corresponding to the maximum array size) and
> then call mmap(PROT_READ|PROT_WRITE, MAP_FIXED) to allocate actual
> memory in the "blank" region. This works well in Linux but fails
> with EINVAL in cygwin.
> 
> My code aligns all sizes up to 2MB boundaries, so it's not a 64kB
> boundary problem. My code reports the failing call as:
> >22 Invalid argument addr=0xffdb0000, sz=2097152
> 
> A peek in /proc/self/maps confirms that the address is correct:
> >FFDB0000-FFFB0000 ===p 00000000 0000:0000 0
> 
> Oddly, trying to map in blank pages in with mprotect succeeds on
> cygwin but fails with ENOMEM on linux...
> 
> Am I missing something here, or is this just a place where different
> behavior between the two platforms is a fact of life? Which version
> is the posixly "correct" way to reserve a chunk of address space and
> later back it with actual memory?

There is no POSIXly correct way to do that since MAP_NORESERVE is a
non-POSIX extension in Linux as well as in Cygwin.

The general idea of MAP_NORESERVE is to make sure that we get as much
memory as requested, but to use only as much memory as is required.
On Linux MAP_NORESERVE only performs bookkeeping but doesn't change
the state of the memory, so a later mmap works.  On Cygwin MAP_NORESERVE
uses the Windows way of handling this requirements, so in contrast to
what the name of the option suggests, Cygwin actually *reserves* space
but does not *commit* it.  Cygwin's mmap can't handle this, but you can
commit pages by using mprotect or by simply peeking or poking into
the address space.  This raises a SEGV, and the exception handling code
will then commit the page you peeked or poked.

Having said that, we *could* also change mmap to handle this scenario
gracefully as well.  http://cygwin.com/acronyms/#PTC.


Corinna

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

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

