This is an archive of the discontinued LLVM Phabricator instance.

Return memory to OS right after free (not in the async thread).
ClosedPublic

Authored by alekseyshl on Nov 22 2016, 2:31 PM.

Details

Summary

In order to avoid starting a separate thread to return unused memory to
the system (the thread interferes with process startup on Android,
Zygota waits for all threads to exit before fork, but this thread never
exits), try to return it right after free.

Event Timeline

alekseyshl updated this revision to Diff 78951.Nov 22 2016, 2:31 PM
alekseyshl retitled this revision from to Return memory to OS right after free (not in the async thread)..
alekseyshl updated this object.
alekseyshl added a reviewer: eugenis.
alekseyshl added a subscriber: llvm-commits.
eugenis added inline comments.Nov 22 2016, 3:06 PM
lib/asan/asan_allocator.cc
296

This function is only used in asan_activation.cc, but you are not adding the flag to asan_activation_flags.inc...
Please add the flag and test it (see TestCases/Linux/activation-options.cc).

lib/sanitizer_common/sanitizer_allocator_primary64.h
87

I think this can be memory_order_relaxed.

486–487

Wait, is this not always negative / underflown?
region->n_freed grows monotonically.

498

For general sanity, check that now_ns is larger than last_release_at_ns.

lib/sanitizer_common/sanitizer_flag_parser.h
71 ↗(On Diff #78951)

Just make the flag signed int, and the special "never" value - (-1).

alekseyshl updated this revision to Diff 79010.Nov 22 2016, 5:41 PM
alekseyshl marked 5 inline comments as done.
  • Address comments

PTAL

lib/sanitizer_common/sanitizer_allocator_primary64.h
486–487

Good catch!

eugenis added inline comments.Nov 22 2016, 6:07 PM
lib/asan/asan_activation.cc
116

Deactivation is meant to reduce memory footprint. It should keep the current value of release_to_os.

lib/sanitizer_common/sanitizer_allocator_primary64.h
498

I meant a non-fatal check, in case the time wraps. After a closer look, perhaps it can not wrap, but it may jump around (and even back). Let's just remove the check.

alekseyshl updated this revision to Diff 79113.Nov 23 2016, 9:45 AM
alekseyshl marked 2 inline comments as done.
  • Comments addressed
filcab added a subscriber: filcab.Nov 23 2016, 11:37 AM
filcab added inline comments.
lib/asan/asan_allocator.h
40

(Unimportant) Nit: Why have a signed number?

lib/sanitizer_common/sanitizer_common.h
841

It's not "the magic value" right now, since any negative value will accomplish this.
I don't care that much about the signed/unsigned above, but this comment should (if we keep s32 as the type) mention the same as the help message: That any negative value means "never".

lib/sanitizer_common/sanitizer_flags.inc
125

Please mention that this only affects a 64-bit allocator.

alekseyshl updated this revision to Diff 79148.Nov 23 2016, 2:10 PM
alekseyshl marked 2 inline comments as done.
  • Addressing comments
lib/asan/asan_allocator.h
40

It seems that using negative values to represent "do not release memory" state looks a bit saner and safer than a magic value, hence signed.

alekseyshl updated this revision to Diff 79410.Nov 28 2016, 9:02 AM
  • Use the actual page size instead of the hardcoded one.
eugenis added inline comments.Nov 28 2016, 9:28 AM
lib/sanitizer_common/sanitizer_allocator_primary64.h
470

I don't think this can be a CHECK any longer. The caller does not guarantee page size granularity.

alekseyshl added inline comments.Nov 28 2016, 4:13 PM
lib/sanitizer_common/sanitizer_allocator_primary64.h
470

Why so? It is called from the loop in which we collect chunks until we get more than a page size.

eugenis accepted this revision.Nov 28 2016, 4:17 PM
eugenis edited edge metadata.

LGTM

lib/sanitizer_common/sanitizer_allocator_primary64.h
470

sorry, I've misread the code

This revision is now accepted and ready to land.Nov 28 2016, 4:17 PM
This revision was automatically updated to reflect the committed changes.