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

configure: Check for membarrier support #1704

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions check-deps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ CHECK_LIST += have_libdw
CHECK_LIST += have_libunwind
CHECK_LIST += have_libcapstone
CHECK_LIST += cc_has_minline_all_stringops
CHECK_LIST += have_membarrier

#
# This is needed for checking build dependency
Expand Down
4 changes: 4 additions & 0 deletions check-deps/Makefile.check
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,7 @@ endif
ifneq ($(wildcard $(objdir)/check-deps/cc_has_minline_all_stringops),)
LIB_CFLAGS += -minline-all-stringops
endif

ifneq ($(wildcard $(objdir)/check-deps/have_membarrier),)
LIB_CFLAGS += -DHAVE_MEMBARRIER
endif
10 changes: 10 additions & 0 deletions check-deps/__have_membarrier.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <linux/membarrier.h>
#include <sys/syscall.h>
#include <unistd.h>

int main()
{
syscall(__NR_membarrier, MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE, 0, 0);
syscall(__NR_membarrier, MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE, 0, 0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By using _PRIVATE_EXPEDITED_SYNC_CORE, can we delete the old copy right after the syscall?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, what do you mean by "the old copy"?

Here is an extract from the membarrier manual, for future reference:

MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE (since Linux 4.16)

In addition to providing the memory ordering guarantees described in MEMBARRIER_CMD_PRIVATE_EXPEDITED, upon return from system call the calling thread has a guarantee that all its running thread siblings have executed a core serializing instruction. This guarantee is provided only for threads in the same process as the calling thread.

The "expedited" commands complete faster than the non-expedited ones, they never block, but have the downside of causing extra overhead.

A process must register its intent to use the private expedited sync core command prior to using it.

MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE (since Linux 4.16)

Register the process's intent to use MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply. I think you want to use membarrier to update global state like in a RCU fashion. Something like below:

new_data = copy_data(shared_data);
add_some_data(new_data);

old_data = shared_data;
shared_data = new_data;  // WRITE_ONCE ?

membarrier();

free(old_data);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is somewhat true. We want to use membarriers to stop all running threads before entering patching/unpatching code as will be done in this other commit:
f07ab57

Note that this particular PR merely checks for availability of membarrier system call. The actual usage is left for PR #1747

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, I was just curious how it's gonna be used.

return 0;
}
3 changes: 3 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ usage() {
--without-capstone build without libcapstone (even if found on the system)
--without-perf build without perf event (even if available)
--without-schedule build without scheduler event (even if available)
--without-membarrier build without membarriers (even if available)

--arch=<ARCH> set target architecture (default: system default arch)
e.g. x86_64, aarch64, i386, or arm
Expand Down Expand Up @@ -198,6 +199,7 @@ for dep in $IGNORE; do
capstone) TARGET=have_libcapstone ;;
perf*) TARGET=perf_clockid ;;
sched*) TARGET=perf_context_switch;;
membarrier) TARGET=have_membarrier ;;
*) ;;
esac
if [ ! -z "$TARGET" ]; then
Expand Down Expand Up @@ -265,6 +267,7 @@ print_feature "perf_event" "perf_clockid" "perf (PMU) event support"
print_feature "schedule" "perf_context_switch" "scheduler event support"
print_feature "capstone" "have_libcapstone" "full dynamic tracing support"
print_feature "libunwind" "have_libunwind" "stacktrace support (optional for debugging)"
print_feature "membarrier" "have_membarrier" "membarrier support (requires Linux 4.16)"

cat >$output <<EOF
# this file is generated automatically
Expand Down