At one point last year, we were discussing rtld and the non-use of superpage mappings for the code sections of large libraries. What we found was that the way that rtld was loading the library effectively blocked the use of superpage mappings. Specifically, the non-use of superpage mappings was the result of two things coming together. First, rtld performs a 1-page mmap call to read the header and this leads to an arbitrary color setting on the vm object backing the shared library. Second, rtld reserves virtual address space for the shared library using an anonymous mmap, so it has no idea what color was assigned to the vm object. (Currently, we force the anonymous mapping to be superpage aligned if it is sufficiently large.) Unfortunately, the arbitrary color assignment to the object results in the allocation of physical memory that is not aligned with the selected virtual address range, so superpage mappings are impossible.
We discussed switching from the 1-page mmap call to a read call, but that has a problem. We only set a color on mapped vm objects. By the time that we mapped the shared library and set its color, the read call would have resulted in the allocation of a few individual pages at the start of the vm object, blocking the creation of a reservation and eventual superpage mapping on the first possible superpage of the code section. (It's worth keeping in mind here that libc on arm is one and a fraction superpages, so we would lose the ability to create that one possible superpage mapping.)
Alternatively, we could force the 1-page mmap call to be superpage aligned so that the color is set correctly. However, I wanted to see if there was a way to handle this case automatically.
To that end, I propose the attached patch. In essence, the key idea here is that named vm objects will always have their color set to zero before they are mapped. In general, I think that this makes sense from the standpoint of maximizing the creation of superpage mappings, not just as a solution for the rtld problem. In the case of rtld, the 1-page mmap will still occur at an arbitrary virtual address, but the allocated upon reference physical memory will nonetheless be superpage aligned and backed by a reservation.
With the recent change to the page clustering policy on read faults and this change, I now see some superpage mappings created for the code section of the giant shared library used by the JVM:
1281 0x801c00000 0x80269e000 r-x 2185 2375 2 1 CNS- vn /usr/local/openjdk7/jre/lib/amd64/server/libjvm.so
At the same time, I'm not seeing a huge increase in the number of reservations created and destroyed. So, I don't think that this change is too aggressive.