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

[OpenMP] runtime might try to take the bootstrap lock twice and deadlock #86684

Closed
jprotze opened this issue Mar 26, 2024 · 5 comments · Fixed by #88539
Closed

[OpenMP] runtime might try to take the bootstrap lock twice and deadlock #86684

jprotze opened this issue Mar 26, 2024 · 5 comments · Fixed by #88539
Assignees
Labels
openmp:libomp OpenMP host runtime openmp

Comments

@jprotze
Copy link
Collaborator

jprotze commented Mar 26, 2024

This was initially reported here (including stacktraces): https://stackoverflow.com/questions/78183545/does-compiling-imagick-with-openmp-enabled-in-freebsd-13-2-cause-sched-yield

If __kmp_register_library_startup detects that another instance of the library is present, __kmp_is_address_mapped is eventually called. It calles __kmp_str_format, which uses kmpc_alloc to allocate memory for the string. This function calls __kmp_entry_thread to access the thread-local memory pool, which seems a bad idea during initialization. This macro internally calls __kmp_get_global_thread_id_reg which set the bootstrap look at the beginning (before calling __kmp_register_library_startup).

@jprotze jprotze added openmp openmp:libomp OpenMP host runtime labels Mar 26, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 26, 2024

@llvm/issue-subscribers-openmp

Author: Joachim (jprotze)

This was initially reported here (including stacktraces): https://stackoverflow.com/questions/78183545/does-compiling-imagick-with-openmp-enabled-in-freebsd-13-2-cause-sched-yield

If __kmp_register_library_startup detects that another instance of the library is present, __kmp_is_address_mapped is eventually called. It calles __kmp_str_format, which uses kmpc_alloc to allocate memory for the string. This function calls __kmp_entry_thread to access the thread-local memory pool, which seems a bad idea during initialization. This macro internally calls __kmp_get_global_thread_id_reg which set the bootstrap look at the beginning (before calling __kmp_register_library_startup).

@shiltian shiltian changed the title OpenMP runtime might try to take the bootstrap lock twice and deadlock [OpenMP] runtime might try to take the bootstrap lock twice and deadlock Mar 27, 2024
@caliguian
Copy link

It would be fantastic if someone (more knowledgeable than I) could look into this. We've been manually killing the stuck processes every day for quite some time now, which is not a good time. The processes get locked with an infinite sched_yield() call that maxes out the CPU until we see it and manually kill it.

@jprotze
Copy link
Collaborator Author

jprotze commented Apr 10, 2024

@jpeyton52 can you look into this issue?

jpeyton52 added a commit to jpeyton52/llvm-project that referenced this issue Apr 12, 2024
This was initially reported here (including stacktraces):
https://stackoverflow.com/questions/78183545/does-compiling-
imagick-with-openmp-enabled-in-freebsd-13-2-cause-sched-yield

If __kmp_register_library_startup detects that another instance of the
library is present, __kmp_is_address_mapped is eventually called. It
calls __kmp_str_format, which uses kmpc_alloc to allocate memory for
the string. This function calls __kmp_entry_thread to access the
thread-local memory pool, which seems a bad idea during initialization.
This macro internally calls __kmp_get_global_thread_id_reg which set the
bootstrap look at the beginning (before calling
__kmp_register_library_startup).

The fix is to use KMP_INTERNAL_MALLOC/FREE instead of kmpc_malloc/free.
KMP_INTERNAL_MALLOC and KMP_INTERNAL_FREE do not use any bootstrap lock.
They just translate to malloc()/free().

Fixes: llvm#86684
@jpeyton52
Copy link
Contributor

@caliguian, it may help to manually delete /tmp/__KMP_REGISTERED_LIB_* files as well if they exist. This is especially true if you begin seeing a message like the following after using the fixed library (from PR #88539):

This means that multiple copies of the OpenMP runtime have been linked into the program.
That is dangerous, since it can degrade performance or cause incorrect results. The best thing
to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding
static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented
workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow 
the program to continue to execute, but that may cause crashes or silently produce incorrect results. 

@jpeyton52 jpeyton52 self-assigned this Apr 12, 2024
jpeyton52 added a commit that referenced this issue Apr 12, 2024
This was initially reported here (including stacktraces):
https://stackoverflow.com/questions/78183545/does-compiling-imagick-with-openmp-enabled-in-freebsd-13-2-cause-sched-yield

If `__kmp_register_library_startup()` detects that another instance of
the library is present, `__kmp_is_address_mapped()` is eventually
called. which uses `kmpc_alloc()` to allocate memory. This function
calls `__kmp_entry_thread()` to access the thread-local memory pool,
which is a bad idea during initialization. This macro internally calls
`__kmp_get_global_thread_id_reg()` which sets the bootstrap lock at the
beginning (before calling `__kmp_register_library_startup()`).

The fix is to use `KMP_INTERNAL_MALLOC()`/`KMP_INTERNAL_FREE()` instead
of `kmpc_malloc()`/`kmpc_free()`. `KMP_INTERNAL_MALLOC` and
`KMP_INTERNAL_FREE` do not use any bootstrap locks. They just translate
to `malloc()`/`free()` and are meant to be used during library
initialization before other library-specific allocators have been
initialized.

Fixes: #86684
@caliguian
Copy link

@jpeyton52 Thank you!

bazuzi pushed a commit to bazuzi/llvm-project that referenced this issue Apr 15, 2024
This was initially reported here (including stacktraces):
https://stackoverflow.com/questions/78183545/does-compiling-imagick-with-openmp-enabled-in-freebsd-13-2-cause-sched-yield

If `__kmp_register_library_startup()` detects that another instance of
the library is present, `__kmp_is_address_mapped()` is eventually
called. which uses `kmpc_alloc()` to allocate memory. This function
calls `__kmp_entry_thread()` to access the thread-local memory pool,
which is a bad idea during initialization. This macro internally calls
`__kmp_get_global_thread_id_reg()` which sets the bootstrap lock at the
beginning (before calling `__kmp_register_library_startup()`).

The fix is to use `KMP_INTERNAL_MALLOC()`/`KMP_INTERNAL_FREE()` instead
of `kmpc_malloc()`/`kmpc_free()`. `KMP_INTERNAL_MALLOC` and
`KMP_INTERNAL_FREE` do not use any bootstrap locks. They just translate
to `malloc()`/`free()` and are meant to be used during library
initialization before other library-specific allocators have been
initialized.

Fixes: llvm#86684
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
openmp:libomp OpenMP host runtime openmp
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants