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

Incorrect range calculation in mcode_alloc() #282

Closed
akopytov opened this issue Feb 23, 2017 · 3 comments
Closed

Incorrect range calculation in mcode_alloc() #282

akopytov opened this issue Feb 23, 2017 · 3 comments

Comments

@akopytov
Copy link

akopytov commented Feb 23, 2017

The random address selection loop in mcode_alloc() unnecessarily reduces the available allocation range by 2 times:

    /* Next try probing pseudo-random addresses. */
    do {
      hint = (0x78fb ^ LJ_PRNG_BITS(J, 15)) << 16;  /* 64K aligned. */
    } while (!(hint + sz < range));
    hint = target + hint - (range>>1);

Two problems here:

  • we probably want LJ_PRNG_BITS(J, LJ_TARGET_JUMPRANGE-16) there, otherwise we may be wasting cycles on architectures with LJ_TARGET_JUMPRANGE < 31
  • to get a block within the [target - range; target + range) range (since range is already half the available jump range) we want to check hint + sz against range * 2, then subtract range.

This is likely a minor issue for x86, where the available jump range is big. This becomes a big problem for architectures where the available range is already small (e.g. ARM64 with the +-128 MB range) and there are many parallel threads, amplifying contention on mmap() that I reported earlier in the mailing list.

@akopytov
Copy link
Author

Updated the report to remove the first part -- that code in mcode_alloc() is actually correct.

@akopytov
Copy link
Author

There are other issues in mcode_alloc() contributing to mmap() contention. I'm going to report the separately.

akopytov added a commit to akopytov/LuaJIT that referenced this issue Feb 25, 2017
Since 'range' in mcode_alloc() is calculated based on
LJ_TARGET_JUMPRANGE-1, i.e. already half the available jump range, don't
divide it by 2 again for randomized allocations.

Also fix the number of bits argument to LJ_PRNG_BITS() to not generate
excessive bits on architectures with LJ_TARGET_JUMPRANGE < 31. That
wouldn't play well with the 0x78b constant being XORed with the
generated random number apparently to improve PRNG properties, so that
part has been removed. Improving PRNG will be addressed separately.
@MikePall
Copy link
Member

MikePall commented Mar 8, 2017

Applied. Thanks!

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

2 participants