Repository navigation
Closed
Labels
Milestone
Description
In commit: c2f970c pages.c introduces a conditionally compiled os_overcommits_proc() function that opens a /proc file to query the overcommit policy. The trouble is that this is done holding the malloc init lock. If you have an LD_PRELOAD library in place, or anything that needs to init (and expects to be able to use malloc) in order to hook the open() system call, you will end up with a recursive acquire of the malloc init lock.
Please consider acquiring the lock after querying the commit policy via /proc.
#ifdef JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY
static bool
os_overcommits_proc(void)
{
int fd;
char buf[1];
ssize_t nread;
fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY);
if (fd == -1)
return (false); /* Error. */
nread = read(fd, &buf, sizeof(buf));
if (nread < 1)
return (false); /* Error. */
/*
* /proc/sys/vm/overcommit_memory meanings:
* 0: Heuristic overcommit.
* 1: Always overcommit.
* 2: Never overcommit.
*/
return (buf[0] == '0' || buf[0] == '1');
}
#endifExample:
__lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
135 ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S: No such file or directory.
(gdb) where
#0 __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
#1 0x00007ffff7727f3e in __GI___pthread_mutex_lock (mutex=mutex@entry=0x1680680 ) at ../nptl/pthread_mutex_lock.c:135
#2 0x0000000000f86940 in je_malloc_mutex_lock (tsdn=0x0, mutex=0x1680680 ) at include/jemalloc/internal/mutex.h:97
#3 malloc_init_hard () at src/jemalloc.c:1315
#4 0x0000000000f8a60d in malloc_init () at src/jemalloc.c:291
#5 ialloc_body (slow_path=true, usize=, tsdn=, zero=true, size=) at src/jemalloc.c:1413
#6 calloc (num=num@entry=1, size=size@entry=32) at src/jemalloc.c:1648
#7 0x00007ffff751b627 in _dlerror_run (operate=operate@entry=0x7ffff751b020 , args=args@entry=0x7fffffffe660) at dlerror.c:141
#8 0x00007ffff751b088 in __dlsym (handle=handle@entry=0xffffffffffffffff, name=name@entry=0x7ffff7bbbd05 "socket") at dlsym.c:70
#9 0x00007ffff7b4b877 in citp_find_all_sys_calls () at ../../../../../src/include/onload/declare_syscalls.h.tmpl:40
#10 citp_syscall_init () at ../../../../../src/lib/transport/unix/sys.c:103
#11 0x00007ffff7b4b24c in citp_do_init (max_init_level=max_init_level@entry=2) at ../../../../../src/lib/transport/unix/startup.c:650
#12 0x00007ffff7b516a2 in onload_open (pathname=pathname@entry=0x1257458 "/proc/sys/vm/overcommit_memory", flags=flags@entry=0)
at ../../../../../src/lib/transport/unix/sockcall_intercept.c:2092
#13 0x0000000000fad5fa in open (__oflag=0, __path=0x1257458 "/proc/sys/vm/overcommit_memory") at /usr/include/x86_64-linux-gnu/bits/fcntl2.h:53
#14 os_overcommits_proc () at src/pages.c:217
#15 je_pages_boot () at src/pages.c:245
#16 0x0000000000f86852 in malloc_init_hard_a0_locked () at src/jemalloc.c:1194
#17 0x0000000000f869b4 in malloc_init_hard () at src/jemalloc.c:1322
#18 0x0000000000f8a60d in malloc_init () at src/jemalloc.c:291
#19 ialloc_body (slow_path=true, usize=, tsdn=, zero=true, size=) at src/jemalloc.c:1413
#20 calloc (num=num@entry=1, size=size@entry=32) at src/jemalloc.c:1648
#21 0x00007ffff751b627 in _dlerror_run (operate=operate@entry=0x7ffff751b020 , args=args@entry=0x7fffffffe840) at dlerror.c:141
#22 0x00007ffff751b088 in __dlsym (handle=handle@entry=0xffffffffffffffff, name=name@entry=0x7ffff7bbbd05 "socket") at dlsym.c:70
#23 0x00007ffff7b4b877 in citp_find_all_sys_calls () at ../../../../../src/include/onload/declare_syscalls.h.tmpl:40
#24 citp_syscall_init () at ../../../../../src/lib/transport/unix/sys.c:103
#25 0x00007ffff7b4b24c in citp_do_init (max_init_level=max_init_level@entry=10) at ../../../../../src/lib/transport/unix/startup.c:650
#26 0x00007ffff7b4b3de in _init () at ../../../../../src/lib/transport/unix/startup.c:666
#27 0x00007ffff7de74aa in call_init (l=0x7ffff7ff7620, argc=argc@entry=1, argv=argv@entry=0x7fffffffe948, env=env@entry=0x7fffffffe958) at dl-init.c:58
#28 0x00007ffff7de75fb in call_init (env=0x7fffffffe958, argv=0x7fffffffe948, argc=1, l=) at dl-init.c:30
#29 _dl_init (main_map=0x7ffff7ffe168, argc=1, argv=0x7fffffffe948, env=0x7fffffffe958) at dl-init.c:120
#30 0x00007ffff7dd7cfa in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#31 0x0000000000000001 in ?? ()
#32 0x00007fffffffeb7d in ?? ()
#33 0x0000000000000000 in ?? ()
Activity
[-]Deadlock when using LD_PRELOAD libraries that hook the open system call[/-][+]Deadlock when using LD_PRELOAD libraries that hook the open() system call[/+]kspinka commentedon Aug 12, 2016
Indexing tags: futex, hang, preload.
Move pages_boot() call outside of init_lock; fixes jemalloc#443
Whissi commentedon Aug 23, 2016
@kspinka: I tried your patch however jemalloc test suite doesn't pass... no error but test suite hangs (the test suite runs in the same sandbox where I experienced the problem first). So it doesn't looked fixed for me. Last test lines:
kspinka commentedon Aug 24, 2016
@Whissi, just to be sure we're dealing with the same issue, try building without my patch, but with JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY undefined to omit the section of code I referenced above.
Whissi commentedon Aug 24, 2016
I patched configure.ac like
and re-generated configure. Now jemalloc-4.2.1 compiles and passes test suite. If
JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORYis defined it doesn't pass.So yes, I think we are dealing with the same issue, not?
vsarunas commentedon Aug 30, 2016
We have solved the same Onload issue by using syscall() instead of open() to get overcommit_memory policy.
Whissi commentedon Aug 30, 2016
@vsarunas: Who is "we"? A downstream project (a software project which uses jemalloc)? Is the source available? Can you link the commit which added the workaround for you?
vsarunas commentedon Aug 30, 2016
Whissi commentedon Aug 30, 2016
Confirmed, thanks for sharing! With
jemalloc-4.2.1 passes its own test suite and jemalloc consumsers like rsyslog or percona-server no longer show the problem reported in #430 when running using sandbox.
jasone commentedon Sep 7, 2016
I see this as a bootstrapping issue in the code that hooks
open(), rather than something that the memory allocator should accommodate. As mentioned elsewhere, I've seen this same issue for hookedmmap(). It's a constant struggle to keep jemalloc bootstrapping operational without general support for reentrant allocation, but thus far the extra cost of a general mechanism does not appear to be warranted.jasone commentedon Oct 28, 2016
@kspinka convinced me to reopen this issue. I'm planning to integrate a patch based on the patch @Whissi pasted (needs an explanatory comment, as well as a conditional
#include <sys/syscall.h>). The broader problem of supporting other libraries hooking arbitrary syscalls (especiallymmap()) remains something I have no enthusiasm for.sjnewbury commentedon Oct 29, 2016
The patch on here is sufficient for the portage sandbox, but sandboxes are in general becoming increasingly important, a general solution is probably warranted.
Use syscall(2) rather than {open,read,close}(2) during boot.
leelamv commentedon Feb 25, 2017
I could workaround this problem by calling _dl_sym (libc private function) instead of dlsym().
So, why can't we use "dlsym" ?
libdl's dlsym() allocate memory for its internal purposes(actually, for dlerror support). Allocation using malloc/calloc interfaces can put us into deadlock when trying to resolve the libc functions.
Use of "_dl_sym" and other details:
a) "_dl_sym" is unpublished libc function which is functionally equivalent to libdl's dlsym.
b) Internally, dlsym calls libc's "_dl_sym" but with below benefits implemented in the libdl layer.
c) It has a third argument which is return address of the caller.
d) libraries using GLIBC_PRIVATE will run into rpm packaging issues if you don't relax those checks.
When is it okay to relax on the benefits and use _dl_sym.
a) Your library should be loaded as a LD_PRELOAD or linked as one of the first libraries.
b) These are known/published functions which are expected to be found.