<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Sep 5, 2019 at 2:16 AM Rui Ueyama <<a href="mailto:ruiu@google.com">ruiu@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">On Wed, Sep 4, 2019 at 2:59 AM Vic (Chun-Ju) Yang via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><pre style="white-space:pre-wrap;color:rgb(0,0,0)">On 30/08/2019 13:27, David Chisnall via llvm-dev wrote:<br></pre><pre style="white-space:pre-wrap;color:rgb(0,0,0)">> On 28/08/2019 18:58, Vic (Chun-Ju) Yang via llvm-dev wrote:
> ><i> This is an RFC for moving RELRO segment. Currently, lld orders ELF 
</i>> ><i> sections in the following order: R, RX, RWX, RW, and RW contains RELRO. 
</i>> ><i> At run time, after RELRO is write-protected, we'd have VMAs in the order 
</i>> ><i> of: R, RX, RWX, R (RELRO), RW. I'd like to propose that we move RELRO to 
</i>> ><i> be immediately after the read-only sections, so that the order of VMAs 
</i>> ><i> become: R, R (RELRO), RX, RWX, RW, and the dynamic linker would have the 
</i>> ><i> option to merge the two read-only VMAs to reduce bookkeeping costs.
</i>> 
> I am not convinced by this change.  With current hardware, to make any 
> mapping more efficient, you need both the virtual to physical 
> translation and the permissions to be the same.

> Anything that is writeable at any point will be a CoW mapping that, when 
> written, will be replaced by a different page.  Anything that is not 
> ever writeable will be the same physical pages.  This means that the old 
> order is (S for shared, P for private):

> S S P P

> The new order is:

> S P S P P

> This means that the translation for the shared part is *definitely* not 
> contiguous.  Modern architectures currently (though not necessarily 
> indefinitely) conflate protection and translation and so both versions 
> require the same number of page table and TLB entries.

> This; however, is true only when you think about single-level 
> translation.  When you consider nested paging in a VM, things get more 
> complex because the translation is a two-stage lookup and the protection 
> is based on the intersection of the permissions at each level.

> The hypervisor will typically try to use superpages for the second-level 
> translation and so both of the shared pages have a high probability of 
> hitting in the same PTE for the second-level translation.  The same is 
> true for the RW and RELRO segments, because they will be allocated at 
> the same time and any OS that does transparent superpage promotion (I 
> think Linux does now?  FreeBSD has for almost a decade) will therefore 
> try to allocate contiguous physical memory for the mappings if possible.

> I would expect your scheme to translate to more memory traffic from 
> page-table walks in any virtualised environment and I don't see (given 
> that you have increased address space fragmentation) where you are 
> seeing a saving.  With RELRO as part of RW, the kernel is free to split 
> and recombine adjacent VM objects, with the new layout it is not able to 
> combine adjacent objects because they are backed by different storage.

> David</pre><pre style="white-space:pre-wrap;color:rgb(0,0,0)">Indeed I did not think about this case. Thanks for pointing this out! I agree that with superpages this can result in worse performance and memory usage. Perhaps we can consider putting this change behind a build time flag? As much as I'd like to avoid adding flags, it seems to me from this thread that there are some real world cases that benefit from this change and some that suffer</pre></div></blockquote><div>If "build time" in the above sentence means a build-time configuration of the linker (i.e. changing a linker default setting when lld is configured and built), we don't have that kind of configuration in lld at all, and that is (I believe) considered a good thing. As long as two lld binaries are of the same version, they behave exactly the same however they were built on any OS. So, if we need to make it configurable, we should add it as a linker flag.</div></div></div></blockquote><div>Yes, a linker flag is what I meant. (i.e. same lld binary) </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div><br></div><div>If the proposed new layout works better than the current one on a non-virtualized environment and behave poorly on a virtualized environment, that is a tricky situation. We usually run the exact same OS and applications on both environments, so we have to choose one. Perhaps, we should first verify that the performance degradation on a VM is not hypothetical but real?</div></div></div></blockquote><div>Agreed. I'll see if I can figure out how to verify this. If anyone has any pointers on how I can get a VM to use a huge page, that'd be most welcomed.</div><div><br></div><div>At the same time, I'd also like to point out that there are cases where a shared library is only used in a non-virtualized environment and never in a virtualized environment. For example, Android has different build targets for real devices vs virtual devices, and if we ended up with the linker flag, one can choose to enable it on real devices and keep the current behavior on virtual devices if the performance of virtual devices is of concern.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><pre style="white-space:pre-wrap;color:rgb(0,0,0)">Vic</pre></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Sep 3, 2019 at 10:40 AM Vic (Chun-Ju) Yang <<a href="mailto:victoryang@google.com" target="_blank">victoryang@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Aug 30, 2019 at 4:54 AM Fāng-ruì Sòng <<a href="mailto:maskray@google.com" target="_blank">maskray@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">> > Old: R RX RW(RELRO) RW<br>> > New: R(R+RELRO) RX RW;      R includes the traditional R part and the<br>> > RELRO part<br>> > Runtime (before relocation resolving): RW RX RW<br>> > Runtime (after relocation resolving): R RX RW<br>> ><br>> I actually see two ways of implementing this, and yes what you mentioned<br>> here is one of them:<br>>   1. Move RELRO to before RX, and merge it with R segment. This is what you<br>> said above.<br>>   2. Move RELRO to before RX, but keep it as a separate segment. This is<br>> what I implemented in my test.<br>> As I mentioned in my reply to Peter, option 1 would allow existing<br>> implementations to take advantage of this without any change. While I think<br>> this optimization is well worth it, if we go with option 1, the dynamic<br>> linkers won't have a choice to keep RO separate if they want to for<br>> whatever reason (e.g. less VM commit, finer granularity in VM maps, not<br>> wanting to have RO as writable even if for a short while.) So there's a<br>> trade-off to be made here (or an option to be added, even though we all<br>> want to avoid that if we can.)<br><br>Then you probably meant:<br><br>Old: R RX RW(RELRO) RW<br>New: R | RW(RELRO) RX RW<br>Runtime (before relocation resolving): R RW RX RW<br>Runtime (after relocation resolving): R R RX RW   ; the two R cannot be merged<br><br>| means a maxpagesize alignment. I am not sure whether you are going to add it<br>because I still do not understand where the saving comes from. </div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><br>If the alignment is added, the R and RW maps can get contiguous<br>(non-overlapping) p_offset ranges. However, the RW map is private dirty,<br>it cannot be merged with adjacent maps so I am not clear how it can save kernel memory.</div></div></blockquote><div><br></div><div>My understanding (and my test result shows so) is that two VMAs can be merged even when one of them contains dirty pages. As far as I can tell from reading vma_merge() in mm/mmap.c in Linux kernel, there's nothing preventing merging consecutively mmaped regions in that case. That said, we may not care about this case too much if we decide that this change should be put behind a flag, because in that case, I think we can just go with option 1.</div><div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><br></div><div dir="ltr">If the alignment is not added, the two maps will get overlapping p_offset ranges.<br><br>> My test showed an overall ~1MB decrease in kernel slab memory usage on<br>> vm_area_struct, with about 150 processes running. For this to work, I had<br>> to modify the dynamic linker:<br><br>Can you elaborate how this decreases the kernel slab memory usage on<br>vm_area_struct?  References to source code are very welcomed :) This is<br>contrary to my intuition because the second R is private dirty.  The number of<br>VMAs do not decrease.<br></div></div></blockquote><div>In mm/mprotect.c, merging is done in mprotect_fixup(), which calls vma_merge() to do the actual work. In the same function you can also see VM_ACCOUNT flag is set for writable VMA, which is why I had to modify the dynamic linker to make R section temporarily writable for it to be mergeable with RELRO (they need to have the same flags to be merged.) Again, IMO all these somewhat indirect manipulations of VMAs were because I was hoping to give the dynamic linker an option to choose whether to take advantage of this or not. If for any reason, we put this behind a build time flag, there's no reason to jump through these hoops instead of just going with option 1.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><br>>   1. The dynamic linker needs to make the read-only VMA briefly writable in<br>> order for it to have the same VM flags with the RELRO VMA so that they can<br>> be merged. Specifically VM_ACCOUNT is set when a VMA is made writable.<br><br>Same question. I hope you can give a bit more details.<br><br>> > How to layout the segments if --no-rosegment is specified?<br>> > Runtime (before relocation resolving): RX RW   ;      some people may be<br>> > concered with writable stuff (relocated part) being made executable<br>> Indeed I think weakening in the security aspect may be a problem if we are<br>> to merge RELRO into RX. Keeping the old layout would be more<br>> preferable IMHO.<br><br>This means the new layout conflicts with --no-rosegment.<br>In Driver.cpp, there should be a "... cannot be used together" error.<br><br>> > Another problem is that in the default -z relro -z lazy (-z now not<br>> > specified) layout, .got and .got.plt will be separated by potentially huge<br>> > code sections (e.g. .text). I'm still thinking what problems this layout<br>> > change may bring.<br>> ><br>> Not sure if this is the same issue as what you mentioned here, but I also<br>> see a comment in lld/ELF/Writer.cpp about how .rodata and .eh_frame should<br>> be as close to .text as possible due to fear of relocation overflow. If we<br>> go with option 2 above, the distance would have to be made larger. With<br>> option 1, we may still have some leeway in how to order sections within the<br>> merged RELRO segment.<br><br>For huge executables (>2G or 3G), it may cause relocation overflows<br>between .text and .rodata if other large sections like .dynsym and .dynstr are<br>placed in between.<br><br>I do not worry too much about overflows potentially caused by moving<br>PT_GNU_RELRO around.  PT_GNU_RELRO is usually less than 10% of the size of the<br>RX PT_LOAD.<br></div></div></blockquote><div>That's good to know! </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><br>> This would be a somewhat tedious change (especially the part about having<br>> to update all the unit tests), but the benefit is pretty good, especially<br>> considering the kernel slab memory is not swappable/evictable. Please let<br>> me know your thoughts!<br><br>Definitely! I have prototyped this and find ~260 tests will need address changing..</div></div>
</blockquote></div></div>
</blockquote></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>
</blockquote></div></div>