[PATCH] Fix mutexes on FreeBSD 11

Volker Lendecke vl at samba.org
Wed Nov 23 14:57:27 UTC 2016


On Tue, Nov 22, 2016 at 12:03:41PM +0100, Volker Lendecke wrote:
> On Tue, Nov 22, 2016 at 11:01:59AM +0100, Volker Lendecke wrote:
> > 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.
> 
> Hmm. Patch faulty. New patch pending.

Attached. Review appreciated!

Thanks, Volker
-------------- next part --------------
>From de50b3cadd27b5a54526a6d909ef7405afe0c87b 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 52f867a3b6df9f8aea59c147c01c3b826ac65ec6 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 0ed471d43c5546faf28d61e0580095d203f8ac9d 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.

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

diff --git a/lib/tdb/common/mutex.c b/lib/tdb/common/mutex.c
index 3df9f5b..cac3916 100644
--- a/lib/tdb/common/mutex.c
+++ b/lib/tdb/common/mutex.c
@@ -603,12 +603,13 @@ int tdb_mutex_init(struct tdb_context *tdb)
 fail:
 	pthread_mutexattr_destroy(&ma);
 fail_munmap:
-	tdb_mutex_munmap(tdb);
 
 	if (ret == 0) {
 		return 0;
 	}
 
+	tdb_mutex_munmap(tdb);
+
 	errno = ret;
 	return -1;
 }
-- 
2.1.4



More information about the samba-technical mailing list