Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pari default stack size results in huge physical memory commit after fork on Cygwin #22633

Closed
embray opened this issue Mar 17, 2017 · 35 comments
Closed

Comments

@embray
Copy link
Contributor

embray commented Mar 17, 2017

It turns out #21582 causes big problems for fork on Cygwin. This is due to an implementation detail of how Cygwin handles copy-on-write private mappings when forking. A fact that might, unfortunately, be difficult to avoid. The problem with this on Cygwin is that, while Windows does not commit pages to physical memory until they are accessed, they do become committed upon any access (even reads, when they haven't already been written to).

So when a process with a private mmap is forked, it loops over all pages in the mmap'd region and copies them into the child's copy of the mmap'd region. This results in committing physical memory on both the parent and the child, even for pages that haven't been written to yet.

TL;DR, if there's a huge private|anonymous mmap in a process, and that process is forked, it will commit the full size of the mmap to memory in both the parent and child processes.

This is a big problem in Sage since we set a very large default stack size for Pari. This was not a problem prior to #21582, since Pari used the MAP_NORESERVE flag which circumvents this issue (only dirty pages need to be copied). The changes in #21582 make sense for Linux, but for Cygwin the opposite is true. Different but unfortunate implementation details on both platforms.

The best way forward is to use MAP_NORESERVE anyway, which helps Cygwin and won't hurt other platforms.

Upstream bug: https://pari.math.u-bordeaux.fr/cgi-bin/bugreport.cgi?bug=1912

Upstream: Fixed upstream, but not in a stable release.

CC: @jdemeyer

Component: porting: Cygwin

Keywords: days85 windows cygwin pari mmap

Author: Erik Bray, Jeroen Demeyer

Branch/Commit: 343f241

Reviewer: Erik Bray, Jeroen Demeyer

Issue created by migration from https://trac.sagemath.org/ticket/22633

@embray embray added this to the sage-8.0 milestone Mar 17, 2017
@embray
Copy link
Contributor Author

embray commented Mar 17, 2017

comment:1

The additional patch I've attached seems to fix this, but I would like to do more testing to see if any more work is needed.

This definitely fixes the immediate problem of memory being eaten up after fork(), but I don't know if there are any other implications for these changes on Cygwin (I suspect the rest is fine, however).


New commits:

48b7621Add patch to Pari (on top of prot_none_1.patch) that restores use of MAP_NORESERVE on Cygwin

@embray
Copy link
Contributor Author

embray commented Mar 17, 2017

Branch: u/embray/cygwin/pari-mmap

@embray
Copy link
Contributor Author

embray commented Mar 17, 2017

Author: Erik Bray

@embray
Copy link
Contributor Author

embray commented Mar 17, 2017

Commit: 48b7621

@embray
Copy link
Contributor Author

embray commented Mar 17, 2017

Upstream: Not yet reported upstream; Will do shortly.

@embray
Copy link
Contributor Author

embray commented Mar 17, 2017

comment:2

Per jdemeyer it might be fine to simplify this by including MAP_NORESERVE on all platforms that support it. Now that PROT_NONE is used I think the MAP_NORESERVE should be harmless on Linux (though I'd like to know a way to check that...)

@jdemeyer
Copy link

comment:3

Replying to @embray:

Now that PROT_NONE is used I think the MAP_NORESERVE should be harmless on Linux (though I'd like to know a way to check that...)

If you have a patch ready, I can test it.

@jdemeyer
Copy link

comment:4

Do you want me to report this upstream?

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 21, 2017

Changed commit from 48b7621 to a724b2a

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 21, 2017

Branch pushed to git repo; I updated commit sha1. New commits:

a724b2aUpdate the patch to always apply MAP_NORESERVE if it's defined (not just on Cygwin)

@embray
Copy link
Contributor Author

embray commented Mar 21, 2017

comment:6

I just updated the patch with your suggestion. I'll report it now.

@embray
Copy link
Contributor Author

embray commented Mar 21, 2017

Changed upstream from Not yet reported upstream; Will do shortly. to Reported upstream. No feedback yet.

@jdemeyer

This comment has been minimized.

@jdemeyer
Copy link

comment:9

Can you check if it suffices to add MAP_NORESERVE only to the PROT_NONE allocations? That would be more in line with the intent of the current code. See also my answer to the PARI bug.

@jdemeyer
Copy link

comment:10

Beware of #22675: we might need to synchronize these tickets.

@embray
Copy link
Contributor Author

embray commented Mar 23, 2017

comment:11

I can update these after #22675.

@embray
Copy link
Contributor Author

embray commented Mar 23, 2017

comment:12

Hm, for some reason, while I got Bill's initial reply to my report I didn't get your response in my e-mail. Maybe I need to explicitly subscribe to updates on the bug...

Yeah, I can try making that change. I don't see what difference it would make--what harm does it have on the other allocations? But if you think it will help I can try it. It should be fine.

@jdemeyer
Copy link

jdemeyer commented Apr 3, 2017

comment:13

Replying to @jdemeyer:

Can you check if it suffices to add MAP_NORESERVE only to the PROT_NONE allocations? That would be more in line with the intent of the current code. See also my answer to the PARI bug.

ping

@embray
Copy link
Contributor Author

embray commented Apr 3, 2017

comment:14

Right. Working on it.

@embray
Copy link
Contributor Author

embray commented Apr 3, 2017

comment:15

A minimal example in C seems to indicate that this would not work. Perhaps it's a limitation of Cygwin and/or Windows, but calling mmap(..., PROT_NONE, MAP_NORESERVE) at the address of a previous mmap(NULL, ..., PROT_READ|PROT_WRITE, ...) without MAP_NORESERVE exhibits the same bug on fork.

@jdemeyer
Copy link

jdemeyer commented Apr 3, 2017

comment:16

Thanks for testing.

I will push a new patch here, hang on.

@jdemeyer
Copy link

jdemeyer commented Apr 3, 2017

Changed branch from u/embray/cygwin/pari-mmap to u/jdemeyer/cygwin/pari-mmap

@jdemeyer
Copy link

jdemeyer commented Apr 3, 2017

comment:18

Does this work for you?


New commits:

795e3f1mmap() PARI stack with MAP_NORESERVE (for Cygwin)

@jdemeyer
Copy link

jdemeyer commented Apr 3, 2017

Changed commit from a724b2a to 795e3f1

@jdemeyer
Copy link

jdemeyer commented Apr 4, 2017

comment:19

Hang on, I will add a check for the return value of mmap().

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 4, 2017

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

343f241mmap() PARI stack with MAP_NORESERVE (for Cygwin)

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 4, 2017

Changed commit from 795e3f1 to 343f241

@jdemeyer
Copy link

jdemeyer commented Apr 4, 2017

comment:22

Does it work on Cygwin?

@embray
Copy link
Contributor Author

embray commented Apr 4, 2017

comment:23

It looks like the explicit munmap before remapping works. I don't exactly see what advantage this has over my original patch but this is fine too.

@jdemeyer
Copy link

jdemeyer commented Apr 5, 2017

Changed upstream from Reported upstream. No feedback yet. to Fixed upstream, but not in a stable release.

@jdemeyer
Copy link

jdemeyer commented Apr 5, 2017

comment:24

Accepted by upstream.

@vbraun
Copy link
Member

vbraun commented Apr 5, 2017

comment:25

Reviewer name missing...

@jdemeyer
Copy link

jdemeyer commented Apr 6, 2017

Changed author from Erik Bray to Erik Bray, Jeroen Demeyer

@jdemeyer
Copy link

jdemeyer commented Apr 6, 2017

Reviewer: Erik Bray, Jeroen Demeyer

@vbraun
Copy link
Member

vbraun commented Apr 7, 2017

Changed branch from u/jdemeyer/cygwin/pari-mmap to 343f241

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants