gperftools: memfs sysallocator can't share fd across forking (was: SIGBUS error in child after parent performs allocation on Hugepages)
I am facing an error where I receive SIGBUS error in the child process. The flow of things happening here are as follows -
- Fork()
- Child Process performs some allocation and goes to sleep.
- Parent process which was sleeping till now performs some allocations and goes to sleep again.
- Child Process wakes up and tries accessing the memory it allocated earlier -------> SIGBUS received.
Steps to reproduce -
g++ tcm.cpp -o tcm -L/usr/local/lib -ltcmalloc_debug
TCMALLOC_MEMFS_DISABLE_FALLBACK=true TCMALLOC_MEMFS_MAP_PRIVATE=true TCMALLOC_MEMFS_MALLOC_PATH=/dev/hugepages/tcmalloc ./tcm
I am using MAP_PRIVATE on Hugepage setup. My understanding is that this should have ensured that parent and child have their own separate mapping and we shouldn’t get into SIGBUS. But somehow after the parent process does its allocations the mappings of child process get corrupted and we receive SIGBUS. I am using gperftools master. Is this a known issue ? tcm.cpp.txt
Output Received:
Parent Process: 2202509 Child Process: 2202513 Memory Allocated by Child from 0x7fbd0b801000:0x7fbd0bc01000 malloc_hook section is missing, thus InHookCaller is broken! Memory Allocated by Child from 0x7fbd0b201000:0x7fbd0b601000 Memory Allocated by Child from 0x7fbd0ac01000:0x7fbd0b001000 Memory Allocated by Child from 0x7fbd0a601000:0x7fbd0aa01000 Memory Allocated by Parent from 0x7fbd0b801000:0x7fbd0bc01000 About to memset on addrress: 0x7fbd0b801000 Memset done on address: 0x7fbd0b801000 About to memset on addrress: 0x7fbd0b201000 Invoking SIGBUS handler in context of 2202513 Signal number: 7 Error number: 0 Signal code: 2 Sending process ID: 186650624 Sending user ID: 32701 Fault address: 0x7fbd0b201000 Exit value or signal: 0 Band event: 140449912197120 Timer overrun count: 32701 POSIX timer ID: 0xb201000 User CPU time consumed: 0 System CPU time consumed: 0 Signal value: (nil) POSIX.1b signal: 0 POSIX.1b signal pointer: (nil) System call number: 0 ARCH dependent data: (nil) Parent Process Exit malloc_hook section is missing, thus InHookCaller is broken!
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 18 (10 by maintainers)
Assigned. But do note, that we don’t normally do the bureaucracy thingy around assignments etc. Just do the thing and if its right we’ll merge and be thankful.
Regarding the usecase, for historic reasons, we do have a parent process which is not doing much but forks the child which does all the allocations, so dropping MAP_PRIVATE would cause lot of legacy headaches.
Given the tmpfs usecases that you cited, it seems atfork is the only agreeable option.
No, some Google workloads do use (sibling of) memfs allocator over tmpfs, for complicated reasons.
And in general, I don’t see memfs as hugetlbfs and Linux-only. It can be useful for all kinds of memories exposed as a filesystem.
As for your comment that your workloads run with MAP_PRIVATE, I am curious for more details. As far as I can see, it is only useful across forks, but those forks are broken as noted here.
Looks like there are only 2 decent alternatives:
Ah, okay. Here is what happens. After fork, both child and parent have same FD for hugetlbfs file. As part of allocating memory, we do ftruncate, with intention to extend the file. But after child has allocated it’s pages, the file has grown and then parent actually shrinks it when it allocates it’s memory. So than child gets SIGBUS as per normal behavior.
This code has ~never had actual working support for MAP_PRIVATE and “sharing” hugetlbfs heap file descriptor.
From what I can see, we actually don’t have to do ftruncate as mmap-ing “past” end of file usually works. And does seem to work on my fairly up-to-date Linux kernel for hugetlbfs (maybe it didn’t way back when this code was written).
Now I have to comment about the homework comment I already made above. So in the perfect world, I can see you following up from my SIGBUS comment above and reducing your test program until you get to something. Maybe not final conclusion but to much tighter test case. Please don’t take this comment as some sort of offense, but rather as advise on how to become better engineer. Good luck.
P.S. here is my test program that I ended up building to narrow things down:
Here is what happens. You’ve set up your malloc to use hugepages. So that memory was allocated (and you say you have 0 free hugepages left). Then you fork, which means entire address space is logically duplicated. Then as you touch the memory, hugetlbfs needs to allocate new pages (because of duplication). And it runs out of pages to give. So it gives you SIGBUS.
SIGBUS is how hugetlbfs reacts to failures to allocate a huge page. So just either don’t fork or reserve more memory for huge pages.