[PATCH] Fix mutexes on FreeBSD 11

Volker Lendecke vl at samba.org
Tue Nov 22 10:01:59 UTC 2016


Hi!

The attached patchset makes tdbtorture -m work for me on FreeBSD 11.
It did survive a private autobuild for me. Mutexes are always really
tricky, so I would appreciate a thorough review.

Thanks, Volker
-------------- next part --------------
>From 85a5b20ba6395b9fb33ccfa721504cb320a59570 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Mon, 21 Nov 2016 20:56:55 +0100
Subject: [PATCH 1/3] tdb: NULL out tdb->mutexes in tdb_mutex_munmap

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/tdb/common/mutex.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/lib/tdb/common/mutex.c b/lib/tdb/common/mutex.c
index 280dec1..3420d21 100644
--- a/lib/tdb/common/mutex.c
+++ b/lib/tdb/common/mutex.c
@@ -636,13 +636,20 @@ int tdb_mutex_mmap(struct tdb_context *tdb)
 int tdb_mutex_munmap(struct tdb_context *tdb)
 {
 	size_t len;
+	int ret;
 
 	len = tdb_mutex_size(tdb);
 	if (len == 0) {
 		return 0;
 	}
 
-	return munmap(tdb->mutexes, len);
+	ret = munmap(tdb->mutexes, len);
+	if (ret == -1) {
+		return -1;
+	}
+	tdb->mutexes = NULL;
+
+	return 0;
 }
 
 static bool tdb_mutex_locking_cached;
-- 
2.1.4


>From 899a013a1fcda4a4575e8900cb4a1a2d50808bf6 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Mon, 21 Nov 2016 20:58:08 +0100
Subject: [PATCH 2/3] tdb: Only mmap the mutex area if not alread mmap'ed

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/tdb/common/mutex.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/lib/tdb/common/mutex.c b/lib/tdb/common/mutex.c
index 3420d21..3df9f5b 100644
--- a/lib/tdb/common/mutex.c
+++ b/lib/tdb/common/mutex.c
@@ -623,6 +623,10 @@ int tdb_mutex_mmap(struct tdb_context *tdb)
 		return 0;
 	}
 
+	if (tdb->mutexes != NULL) {
+		return 0;
+	}
+
 	ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FILE,
 		   tdb->fd, 0);
 	if (ptr == MAP_FAILED) {
-- 
2.1.4


>From 990815df76be2130e85e02832a354ea106c81353 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Mon, 21 Nov 2016 21:00:01 +0100
Subject: [PATCH 3/3] tdb: Fix mutexes on FreeBSD

susv4 on mmap has the following snippet:

> The state of synchronization objects such as mutexes, semaphores,
> barriers, and conditional variables placed in shared memory mapped
> with MAP_SHARED becomes undefined when the last region in any process
> containing the synchronization object is unmapped.

This means we can't keep the mutex mmap area unmapped at any point
in time.

Thanks a lot to Konstantin Belousov <kostikbel at gmail.com> for insights and the
hint to the mmap definition. See

https://lists.freebsd.org/pipermail/freebsd-hackers/2016-November/050173.html

and followups.

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/tdb/common/mutex.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/lib/tdb/common/mutex.c b/lib/tdb/common/mutex.c
index 3df9f5b..b387c50 100644
--- a/lib/tdb/common/mutex.c
+++ b/lib/tdb/common/mutex.c
@@ -569,7 +569,7 @@ int tdb_mutex_init(struct tdb_context *tdb)
 
 	ret = pthread_mutexattr_init(&ma);
 	if (ret != 0) {
-		goto fail_munmap;
+		goto fail_nodestroy;
 	}
 	ret = pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK);
 	if (ret != 0) {
@@ -602,9 +602,7 @@ int tdb_mutex_init(struct tdb_context *tdb)
 	ret = 0;
 fail:
 	pthread_mutexattr_destroy(&ma);
-fail_munmap:
-	tdb_mutex_munmap(tdb);
-
+fail_nodestroy:
 	if (ret == 0) {
 		return 0;
 	}
-- 
2.1.4



More information about the samba-technical mailing list