You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
static TLS errors from jemalloc 5.0.0 built on CentOS 6 #937
I help maintain packages on conda-forge which has become fairly popular in the Python community. We recently added jemalloc 5.0.0 to the package manager, built on CentOS 6 with devtoolset-2 from this base Docker image (glibc 2.12 I think)
Thread local storage in shared libraries will, by default, require a malloc on first use of the thread-local data by a thread. This can trigger some reentrancy scenarios, and even if you don't, it's easy to accidentally mix malloc-from-jemalloc and malloc-from-glibc. We also don't try to reclaim global data structures on shutdown, so dlopen-ing and dlclose-ing libjemalloc.so repeatedly will leak.
To avoid the reentrancy scenarios, we mark our TLS so that its storage gets allocated with new threads. This works fine for LD_PRELOAD'd (or whatever) shared libraries, but has some problems with dlopen'd ones (where, obviously, the loader doesn't know that it will need that space allocated in advance). glibc tries to save some extra space in advance for any dlopen that happens to come along, but not a ton. jemalloc 5.0 increased the amount of TLS it uses, so it plausibly pushed itself over the limit and can no longer fit in glibc's spare capacity.
We could potentially add a config option to move our TLS out of line, but given the pitfalls here I'm pretty nervous. Could you describe a little more why people want to be able to dlopen libjemalloc.so?
(also, in any case, a bugfix 5.0.1 release will be coming out relatively soon that may be worth waiting for before increasing the blast radius of the 5.0 change).
Just to outline our usage of jemalloc in Apache Arrow: We don't explicitly dlopen the library but simply link dynamically against it. It is never unloaded explicitly, usage is exactly like any other shared library. We also explicitly use the jemalloc mallocx API thus LD_PRELOADing would not suffice for our usage.
@davidtgoldblatt sorry to be confusing, we have a shared library libarrow.so that is linked to libjemalloc.so, and the TLS error occurs in the dynamic loader
Could you deploy a jemalloc built with custom config options? We'd want to support this by moving the TLS to be backed by the system malloc, which we'd have to have a config flag for.
In fact, I think ideally we'd only let someone do this when a custom prefix is turned on, to flash a big neon warning sign saying "malloc here does not mean malloc elsewhere". Would that badly break something for you?
Why not just always delegate to the platform allocator (e.g. posix_memalign)? If all you're getting from linking against jemalloc is a perf boost, you can LD_PRELOAD it instead.
The two bullet points you mention are in some tension, because people linking against libjemalloc.so may (quite reasonably) expect to be able to free() memory they mallocx(), and will hit a nasty surprise.
We explicitly call rallocx to get 64byte-aligned memory reallocations. This is not available in the POSIX API and thus we see a great benefit of directly using the jemalloc API instead of simply the POSIX one.
Would the strategy of introduce a special configure flag that moves jemalloc's TLS to be allocated by glibc malloc, but requiring this to be used only with --with-jemalloc-prefix work?
This wouldn't let users use the *allocx functions unprefixed, but, because of the symbol collision issues involved, I think that's more of a feature than a bug.
Yes, I think in this case we would use a prefix that is unique to our library and statically link these symbols to avoid conflicts with other applications that may expect to use libjemalloc.so
I put up a hypothetical change we could make in davidtgoldblatt@dbd61d6 . This would let you switch at build time to a dlsym-able libjemalloc.so by passing a --enable-general-dynamic-tls configure flag.
I don't love this, for a two reasons:
I don't know how package maintainers would deal with it (this way always breaks for people who depend on jemalloc without dlsym, and the other way always breaks for people who depend on it with dlsym).
I don't think there's a good way of stopping pointers from leaking between the two malloc implementations running in the single process. This will blow up in a confusing way in an end-user's face at some point. Then they'll come angrily yell at you, or, worse, me.
If you're hitting problems with the limited static TLS size, could you try out your build with this fix to ensure you don't hit any issues?
I think if this doesn't work, we'd have to add a config option that lets you go back to storing TLS out of line and taking a pointer indirection hit on every operation. I'd really like to avoid something like that, which will hurt common case performance (since I assume that's the only version of the library that would get installed in that case).
I tested -mtls-dialect=gnu2 and removing the __attribute__((tls_model("initial-exec"))) and it worked fine both being loaded dynamically and on start. For reference, here's a readelf --all <lib>> | grep -i tls on each:
Original install:
L (link order), O (extra OS processing required), G (group), T (TLS),
TLS 0x000000000006a870 0x000000000026a870 0x000000000026a870
0x000000000000001e (FLAGS) STATIC_TLS
00000026f0f8 001d00000007 R_X86_64_JUMP_SLO 0000000000000000 __tls_get_addr@GLIBC_2.3 + 0
29: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __tls_get_addr@GLIBC_2.3 (9)
With tls-dialect:
L (link order), O (extra OS processing required), G (group), T (TLS),
TLS 0x0000000000071870 0x0000000000271870 0x0000000000271870
0x000000006ffffef6 (TLSDESC_PLT) 0x5990
0x000000006ffffef7 (TLSDESC_GOT) 0x275ff0
000000276220 000000000024 R_X86_64_TLSDESC 0
504: 0000000000000000 6104 TLS LOCAL DEFAULT 18 je_tsd_tls
I'm not sure how to test the performance in both situations.
Activity
davidtgoldblatt commentedon Jun 27, 2017
Trying to dlopen jemalloc is bad news bears.
Thread local storage in shared libraries will, by default, require a malloc on first use of the thread-local data by a thread. This can trigger some reentrancy scenarios, and even if you don't, it's easy to accidentally mix malloc-from-jemalloc and malloc-from-glibc. We also don't try to reclaim global data structures on shutdown, so dlopen-ing and dlclose-ing libjemalloc.so repeatedly will leak.
To avoid the reentrancy scenarios, we mark our TLS so that its storage gets allocated with new threads. This works fine for LD_PRELOAD'd (or whatever) shared libraries, but has some problems with dlopen'd ones (where, obviously, the loader doesn't know that it will need that space allocated in advance). glibc tries to save some extra space in advance for any dlopen that happens to come along, but not a ton. jemalloc 5.0 increased the amount of TLS it uses, so it plausibly pushed itself over the limit and can no longer fit in glibc's spare capacity.
We could potentially add a config option to move our TLS out of line, but given the pitfalls here I'm pretty nervous. Could you describe a little more why people want to be able to dlopen libjemalloc.so?
davidtgoldblatt commentedon Jun 27, 2017
(also, in any case, a bugfix 5.0.1 release will be coming out relatively soon that may be worth waiting for before increasing the blast radius of the 5.0 change).
xhochy commentedon Jun 27, 2017
Just to outline our usage of jemalloc in Apache Arrow: We don't explicitly
dlopenthe library but simply link dynamically against it. It is never unloaded explicitly, usage is exactly like any other shared library. We also explicitly use the jemallocmallocxAPI thus LD_PRELOADing would not suffice for our usage.wesm commentedon Jun 27, 2017
@davidtgoldblatt sorry to be confusing, we have a shared library
libarrow.sothat is linked tolibjemalloc.so, and the TLS error occurs in the dynamic loaderdavidtgoldblatt commentedon Jun 27, 2017
Could you deploy a jemalloc built with custom config options? We'd want to support this by moving the TLS to be backed by the system malloc, which we'd have to have a config flag for.
In fact, I think ideally we'd only let someone do this when a custom prefix is turned on, to flash a big neon warning sign saying "malloc here does not mean malloc elsewhere". Would that badly break something for you?
wesm commentedon Jun 27, 2017
Our usage of jemalloc is fairly limited (https://github.com/apache/arrow/blob/master/cpp/src/arrow/memory_pool.cc#L54), so as long as we can
libjemalloc.soto link tothen I think we should be OK.
davidtgoldblatt commentedon Jun 27, 2017
Why not just always delegate to the platform allocator (e.g. posix_memalign)? If all you're getting from linking against jemalloc is a perf boost, you can LD_PRELOAD it instead.
The two bullet points you mention are in some tension, because people linking against libjemalloc.so may (quite reasonably) expect to be able to free() memory they mallocx(), and will hit a nasty surprise.
xhochy commentedon Jun 28, 2017
We explicitly call
rallocxto get 64byte-aligned memory reallocations. This is not available in the POSIX API and thus we see a great benefit of directly using the jemalloc API instead of simply the POSIX one.davidtgoldblatt commentedon Jun 28, 2017
Would the strategy of introduce a special
configureflag that moves jemalloc's TLS to be allocated by glibc malloc, but requiring this to be used only with--with-jemalloc-prefixwork?This wouldn't let users use the *allocx functions unprefixed, but, because of the symbol collision issues involved, I think that's more of a feature than a bug.
xhochy commentedon Jun 28, 2017
I think that this should work for us for the moment until we can raise the minimal glibc requirement.
wesm commentedon Jun 28, 2017
Yes, I think in this case we would use a prefix that is unique to our library and statically link these symbols to avoid conflicts with other applications that may expect to use
libjemalloc.soKenMacD commentedon Jul 5, 2017
I'm running in to this same issue with
rocksdbon Arch when trying to load that library usingpython-rocksdb.davidtgoldblatt commentedon Jul 11, 2017
I put up a hypothetical change we could make in davidtgoldblatt@dbd61d6 . This would let you switch at build time to a dlsym-able libjemalloc.so by passing a --enable-general-dynamic-tls configure flag.
I don't love this, for a two reasons:
If you're hitting problems with the limited static TLS size, could you try out your build with this fix to ensure you don't hit any issues?
I think if this doesn't work, we'd have to add a config option that lets you go back to storing TLS out of line and taking a pointer indirection hit on every operation. I'd really like to avoid something like that, which will hurt common case performance (since I assume that's the only version of the library that would get installed in that case).
KenMacD commentedon Jul 11, 2017
I agree that solution doesn't sound ideal. If I was packaging
jemalocandrocksdbI really wouldn't know what option to use.Would using
-mtls-dialect=gnu2be likely to work? https://bugs.freedesktop.org/show_bug.cgi?id=35268#c15KenMacD commentedon Jul 11, 2017
I tested
-mtls-dialect=gnu2and removing the__attribute__((tls_model("initial-exec")))and it worked fine both being loaded dynamically and on start. For reference, here's areadelf --all <lib>> | grep -i tlson each:Original install:
With
tls-dialect:I'm not sure how to test the performance in both situations.
17 remaining items
Add patch to workaround static TLS block issue
MB-33042: Add --disable-initial-exec-tls flag to jemalloc compilation
Add disable_initial_exec_tls feature
Add 3rdparty/jemalloc
leezu commentedon Dec 19, 2019
@interwq you asked at #303 (comment) why
--disable-initial-exec-tlsis needed. It's essentially the same use-case as described by Apache ArrowThe use I'm referring to is with
libmxnet.so, which links against thelibjemalloc.so.libmxnet.soisdlopened by Python causing jemalloc to crash:Add 3rdparty/jemalloc
Add 3rdparty/jemalloc
Add 3rdparty/jemalloc
Add 3rdparty/jemalloc
Add 3rdparty/jemalloc
Add 3rdparty/jemalloc
Add 3rdparty/jemalloc
Only link to jemalloc in fdbserver
gnu: jemalloc: --disable-initial-exec-tls
component/jemalloc: upgrade to version 5.3.0