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

Too many Xbyak::Allocator::alloc call produces mprotect failure #13

Closed
Constellation opened this issue Oct 10, 2014 · 6 comments
Closed

Comments

@Constellation
Copy link
Contributor

Hi, thank you for your great library.

I found that sometimes mprotect fails it's modification to the page attribues.
For example, in this test, we observe it several times. https://travis-ci.org/Constellation/iv/jobs/37578037.
In this test, the test script requires xbyak's code generation huge times. (16 ** 4 = 65536 times)

After investigating this problem, I've found that mprotect with valid parameters (address and size are valid) fails with ENOMEM, and it seems that this is because the map count becomes zero (Maybe I think).
ref:

Since xbyak's dynamic memory allocator calls mprotect, but doesn't call munmap, map count doesn't released.
To prevent it, I suggest the following plan.

  • Allocate memory with mmap instead of posix_memalign
  • Manage the allocated size with the address
  • Free the allocated memory with munmap

What do you think about it?

Constellation added a commit to Constellation/iv that referenced this issue Oct 10, 2014
This is monkey patching allocator. free/alloc should be extended to take the
size parameter of the memory to be processed.

ref: herumi/xbyak#13
@herumi
Copy link
Owner

herumi commented Oct 10, 2014

そんな制限があったのですね。mprotectがmmapした領域をmunmapできたらいいのですがちょっと調べてみます。posix_memalignの代わりにmmapは互換性を確保できるか気になりますね。

@herumi
Copy link
Owner

herumi commented Oct 11, 2014

posix_memalignした領域をmprotectした場合は同時に33000個ぐらいのインスタンスを生成するとエラーになるけどmmapした領域に対しては同時にもっと作れるのですね。
mummapがsizeを必要とするのでXbyakMMapAllocatorのようにちょっと余計なmempryを持たないといけないのか。うーむ。
posix_memalignからmmapを使うように変更した場合のデメリットは何かあるでしょうか。システムコールの呼び出し回数が増える?
普通は数万個もインスタンスを作ることはないことを考えるとConstellationさんが作ってくださったようなXbyakMMapAllocatorを提供するのが一番楽ですかね。

@herumi
Copy link
Owner

herumi commented Oct 13, 2014

I created MmapAllocator branch.
https://github.com/herumi/xbyak/tree/mmap_allocator
You can use MmapAllocator if XBYAK_USE_MMAP_ALLOCATOR is defined.
Could you try it?

@Constellation
Copy link
Contributor Author

Thank you! Looks good to me.
Nits.
0fc4b9b#commitcomment-8144440

@herumi
Copy link
Owner

herumi commented Oct 14, 2014

Thank you for your very kind message.

@herumi herumi closed this as completed Oct 14, 2014
@Constellation
Copy link
Contributor Author

Thank you for your quick response!!

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

No branches or pull requests

2 participants