Skip to contentSkip to conversation

Doubts about mmap(2)

DISCUSSION Alex Colomar 2 participants 3 messages May 18, 2023 – May 19, 2023 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

Hi!

I believe mmap(2) is confusing (see the emboldened parts):

DESCRIPTION
mmap() creates a new mapping in the virtual address space of the calling process. **The starting address for the new mapping is specified in addr.** The length argument specifies the length of the mapping (which must be greater than 0).

If addr is NULL, then the kernel chooses the (page‐aligned) ad‐ dress at which to create the mapping; this is the most portable method of creating a new mapping. **If addr is not NULL, then the kernel takes it as a hint about where to place the mapping; on Linux, the kernel will pick a nearby page boundary** (but al‐
ways above or equal to the value specified by
/proc/sys/vm/mmap_min_addr) and attempt to create the mapping
there. If another mapping already exists there, the kernel
picks a new address that may or may not depend on the hint. The address of the new mapping is returned as the result of the call.

The first emboldened sentence contradicts the second. It's not the starting address, but just a hint to find a nearby (>=) starting address. How about saying this?:

A hint for the starting address for the new mapping is specified in addr.

Cheers,
Alex

Signature · 2 lines
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5
#1 lore raw
Quoted 23 lines · Alex Colomar

Hi!

I believe mmap(2) is confusing (see the emboldened parts):

DESCRIPTION
       mmap()  creates  a  new mapping in the virtual address space of the calling process.  **The starting address for the new  mapping is specified in addr.**  The length argument specifies the length of the mapping (which must be greater than 0).

       If addr is NULL, then the kernel chooses the (page‐aligned) ad‐ dress at which to create the mapping; this is the most portable method  of  creating  a new mapping.  **If addr is not NULL, then the kernel takes it as a hint about where to place the mapping; on Linux, the kernel will pick a nearby page boundary** (but  al‐ ways    above    or   equal   to   the   value   specified   by /proc/sys/vm/mmap_min_addr) and attempt to create  the  mapping there.   If  another  mapping  already exists there, the kernel picks a new address that may or may not  depend  on  the  hint. The address of the new mapping is returned as the result of the call.

Quoted 2 lines

The first emboldened sentence contradicts the second.  It's not the starting address, but just a hint to find a nearby (>=) starting

I was a bit wrong there. It's >= mmap_min_addr, not >= hint. But the suggestion below holds.

Quoted 8 lines

address.  How about saying this?:

    A hint for the starting address for the new mapping is specified in addr.

Cheers,
Alex

Signature · 2 lines
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5
#2 lore raw
Quoted 24 lines · Alex Colomar
Quoted 23 lines · Alex Colomar

Hi!

I believe mmap(2) is confusing (see the emboldened parts):

DESCRIPTION
       mmap()  creates  a  new mapping in the virtual address space of the calling process.  **The starting address for the new  mapping is specified in addr.**  The length argument specifies the length of the mapping (which must be greater than 0).

       If addr is NULL, then the kernel chooses the (page‐aligned) ad‐ dress at which to create the mapping; this is the most portable method  of  creating  a new mapping.  **If addr is not NULL, then the kernel takes it as a hint about where to place the mapping; on Linux, the kernel will pick a nearby page boundary** (but  al‐ ways    above    or   equal   to   the   value   specified   by /proc/sys/vm/mmap_min_addr) and attempt to create  the  mapping there.   If  another  mapping  already exists there, the kernel picks a new address that may or may not  depend  on  the  hint. The address of the new mapping is returned as the result of the call.

Quoted 11 lines
Quoted 2 lines

The first emboldened sentence contradicts the second.  It's not the starting address, but just a hint to find a nearby (>=) starting

I was a bit wrong there. It's >= mmap_min_addr, not >= hint. But the suggestion below holds.

Quoted 5 lines

address.  How about saying this?:

    A hint for the starting address for the new mapping is specified in addr.

I think describing the default behavior first is a good idea. If a user needs an exact address they can read further for MAP_FIXED/_NOREPLACE (and the potential footguns involved).

- Oskari