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

Use fallocate on truncate_file #106

Closed
wants to merge 1 commit into from

Conversation

xonatius
Copy link

@xonatius xonatius commented Dec 6, 2019

On linux platform, boost interprocess shared memory uses shared memory files allocated in /dev/shm which is tmpfs. Upon creating a file [1], boost tried to resize it to a requested length [2] through a ftruncate call, which changes the size but does not actually guarantee that the memory will be allocated. As result, when accessing the memory linux kernel will try to allocate it and if the host is under memory pressure, the allocation attempt may fail and application will receive a SIGBUS and crash.

This change introduces posix_fallocate call, which guarantees memory to be allocated or otherwise the call will fail. This behavior is easier to handle than recovering after SIGBUS. The fallocate is supported for tmpfs, which is used for /dev/shm since linux 3.5 [3]. For older versions, which do not support fallocate syscall, the glibc will emulate the behavior.

[1] https://github.com/boostorg/interprocess/blob/boost-1.68.0/include/boost/interprocess/detail/managed_open_or_create_impl.hpp#L368

[2] https://github.com/boostorg/interprocess/blob/boost-1.68.0/include/boost/interprocess/detail/managed_open_or_create_impl.hpp#L403

[3] https://elixir.bootlin.com/linux/v3.5/source/mm/shmem.c#L2721

On linux platform, boost interprocess shared memory uses sharder memory
files allocated in /dev/shm which is tmpfs. Upon creating a file [1],
boost tried to resize it to a requested length [2], through a ftruncate
call, whichchanges the size, but does not actually guarantee that the
memory will be allocated. As result, when accessing the memory linux
kernel will try to allocate it and if the host is under memory pressure
the allocation attempt may fail and application will receive a SIGBUS
and crash.

This change introduces poxis_fallocate call, which guarantees memory to
be allocated or otherwise the call will fail. This behavior is easier to
handle than recovering after SIGBUS. The fallocate is supported for
tmpfs, which is used for /dev/shm since linux 3.5 [3]. For older
versions, which do not support fallocate syscall, the glibc will emulate
the behavior.

[1] https://github.com/boostorg/interprocess/blob/boost-1.68.0/include/boost/interprocess/detail/managed_open_or_create_impl.hpp#L368

[2] https://github.com/boostorg/interprocess/blob/boost-1.68.0/include/boost/interprocess/detail/managed_open_or_create_impl.hpp#L403

[3] https://elixir.bootlin.com/linux/v3.5/source/mm/shmem.c#L2721
@roehling
Copy link

roehling commented Dec 7, 2020

This will probably fix #114

@igaztanaga igaztanaga closed this in 87379be Feb 3, 2021
@igaztanaga
Copy link
Member

Many thanks! I've applied a patch based on your pull request.

@xonatius xonatius deleted the fallocate branch February 3, 2021 19:54
ktf added a commit to alisw/boost that referenced this pull request Nov 21, 2023
Previously, ftruncate usage did not actually enforce the memory was
actually available. Switching to posix_fallocate (see
boostorg/interprocess#106) does so, breaking a
few tests and possibly other parts of our stack.

Interim solution until we cleanup things or decide on a different
strategy.
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

Successfully merging this pull request may close these issues.

3 participants