Skip to contentSkip to conversation

mmap(MAP_PRIVATE) question

DISCUSSION Gianni Tedesco 2 participants 3 messages Sep 3, 2003

lore
Share & tools
lore
#0 lore raw

Hi all,

Quoted 1 line

From the mmap(2) manpage it says:

MAP_PRIVATE
Create a private copy-on-write mapping. Stores to the region do not
affect the original file. It is unspecified whether changes made
to the file after the mmap call are visible in the mapped region.

What is linux behaivour in this area? I am guessing if the page is modified between the call to mmap() and fault-in then the modified copy is seen by the application? But what about modifications after the page is already in page-cache?

Thanks.

Signature · 1 line
Gianni Tedesco <[email protected]>
#1 lore raw
Quoted 9 lines · Gianni Tedesco

Hi all,

Quoted 1 line

From the mmap(2) manpage it says:

MAP_PRIVATE
Create a private copy-on-write mapping. Stores to the region do not
affect the original file. It is unspecified whether changes made
to the file after the mmap call are visible in the mapped region.

Quoted 4 lines

What is linux behaivour in this area? I am guessing if the page is modified between the call to mmap() and fault-in then the modified copy is seen by the application? But what about modifications after the page is already in page-cache?

The page cache page is mapped into the application just like a shared mapping, until the application writes to the mapped region and triggers the copy-on-write fault.

This means that if you write() to the page prior to the copy-on-write fault, you may see the changes in application memory straight away. If some other process writes to the page through a writable mapping, you may see the changes.

On the other hand you may not. On some of the architectures which Linux supports, the CPU's cache is not sufficiently coherent to guarantee that what is written with write(), or by another process, will be seen in this application's memory. Indeed, you might see a mixture of some of the written data and some of the data before it was written, with no particular guarantee of which bits of data or in what order.

-- Jamie

#2 lore raw
Quoted 3 lines · Jamie Lokier

The page cache page is mapped into the application just like a shared mapping, until the application writes to the mapped region and triggers the copy-on-write fault.

that clears it up somewhat :)

Quoted 7 lines

On the other hand you may not. On some of the architectures which Linux supports, the CPU's cache is not sufficiently coherent to guarantee that what is written with write(), or by another process, will be seen in this application's memory. Indeed, you might see a mixture of some of the written data and some of the data before it was written, with no particular guarantee of which bits of data or in what order.

Well, the thing I'm interested in is people overwriting parts of shared libraries (at least those opened with dlopen). In my tests, I am using mmap/msync to overwrite the library rodata section with MS_SYNC|MS_INVALIDATE. When I do this the running copy (using MAP_PRIVATE) appears unmodified, but no ETXTBSY error is given. What you are saying would seem to indicate that the running program *should* be modified.

Perhaps MS_INVALIDATE doesn't bother invalidating MAP_PRIVATE mappings? Or am I missing a trick here? :)

(this is on intel btw).

Signature · 1 line
Gianni Tedesco <[email protected]>