Skip to contentSkip to conversation

Why don't shared anonymous mappings work?

DISCUSSION Colin Plumb 3 participants 3 messages Jan 13, 1999 – Jan 19, 1999 Memory management

lore
Share & tools
lore
#0 lore raw

Um, I just thought of another problem with shared anonymous pages. It's similar to the zero-page issue you raised, but it's no longer a single special case.

Copy-on-write and shared mappings. Let's say that process 1 has a COW copy of page X. Then the page is shared (via mmap /proc/1/mem or some such) with process 2. Now process A writes to the page.

While copying the page, we have to update B's pte to point to the new copy. But we have no data structure to keep track of the sharing structure.

It's a fairly simple structure, really, since a given physical page maps (via COW) to one or more separate logical pages, which are in turn each mapped into one or more memory maps. Becasue of the "one or more", you can hope to integrate it into another structure, but ugh. For n copies, you need n-1 non-null pointers. That's a lot of null pointers when n has its usual value of 1.

One possible fix is to consider multiple mappings of a logical page to be a write for the purposes of COW copying. That way, each physical page is *either* COW-mapped to multiple logical pages, each present in *one* mmap, or corresponds to one logical page which is present in one or more maps. This reduces the tree down to one level, and a type bit in the page structure will do.

It *is* possible to link PTE entries together in a singly-linked list where a pointer to another PTE is distinguishable from a pointer to a disk block or a valid PTE. I have thought of using this to update more PTEs when a page is swapped in, as the swapper-in would traverse the list to find the page at the end, swap it in if necessary, and copy the mapping to all the entries it traversed.

Come to think of it, this *could* be used for zero-mapped pages. Make it a circularly linked list. (You could even distinguish circular and non-circular lists if you need both.) Then when the page is accessed, allocate it and copy the pointer to all the other PTEs on the list.

Do you have any other ideas?

Signature · 4 lines
	-Colin
--
This is a majordomo managed list.  To unsubscribe, send a message with
the body 'unsubscribe linux-mm me@address' to: [email protected]
#1 lore raw

Hi,

Quoted 7 lines · Colin Plumb

Um, I just thought of another problem with shared anonymous pages. It's similar to the zero-page issue you raised, but it's no longer a single special case.

Copy-on-write and shared mappings. Let's say that process 1 has a COW copy of page X. Then the page is shared (via mmap /proc/1/mem or some such) with process 2. Now process A writes to the page.

Invalid argument. This is *precisely* why mmap of /proc/X/mem is broken. We don't need to implement reasonable semantics for that case, because there _are_ no reasonable semantics for a page which can be both MAP_PRIVATE and MAP_SHARED in the same process.

--Stephen
--
This is a majordomo managed list. To unsubscribe, send a message with the body 'unsubscribe linux-mm me@address' to: [email protected]

#2 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"ST" == Stephen C Tweedie <[email protected]> writes:

ST> Hi,
ST> On Wed, 13 Jan 1999 14:31:41 -0700 (MST), Colin Plumb <[email protected]>

Quoted 7 lines · ST> said:
Quoted 7 lines

Um, I just thought of another problem with shared anonymous pages. It's similar to the zero-page issue you raised, but it's no longer a single special case.

Copy-on-write and shared mappings. Let's say that process 1 has a COW copy of page X. Then the page is shared (via mmap /proc/1/mem or some such) with process 2. Now process A writes to the page.

ST> Invalid argument. This is *precisely* why mmap of /proc/X/mem is ST> broken. We don't need to implement reasonable semantics for that case, ST> because there _are_ no reasonable semantics for a page which can be both ST> MAP_PRIVATE and MAP_SHARED in the same process.

Thank you for stomping on this, and my apologies a while ago for bringing it up.

Dosemu keeps coming to my mind. For 2.3 we need a better version of shared memory for dosemu to use. shm is fine but it's not flexible enough.

For multiple MAP_PRIVATE & MAP_SHARED mappings, the most we can
theoretically allow is:
o If a page is updated, we need to update at most the page table entry, the write came from.
o If a write did not come from a page table entry we need to update no page table entries.
o During a mapping we need to update at most one old pte per page, and old pte's that are updated must be in the same mm.

With those guidelines the best we can allow for /proc/self/mem is to promote the page into a shared anonymous mapping, or fail.

Anything else would break the guidelines above. Which are what we need if we want to avoid reverse page table entries, which is reasonable.

Eric
--
This is a majordomo managed list. To unsubscribe, send a message with the body 'unsubscribe linux-mm me@address' to: [email protected]