Blame SOURCES/libdb-5.3.21-mutex_leak.patch

994491
diff -U 5 -r db-5.3.21.old/src/dbinc_auto/int_def.in db-5.3.21/src/dbinc_auto/int_def.in
994491
--- db-5.3.21.old/src/dbinc_auto/int_def.in	2012-05-12 01:57:53.000000000 +0800
994491
+++ db-5.3.21/src/dbinc_auto/int_def.in	2016-10-25 22:40:58.000000000 +0800
994491
@@ -1371,10 +1371,11 @@
994491
 #define	__memp_failchk __memp_failchk@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_bhwrite __memp_bhwrite@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_pgread __memp_pgread@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_pg __memp_pg@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_bhfree __memp_bhfree@DB_VERSION_UNIQUE_NAME@
994491
+#define	__memp_bh_clear_dirty __memp_bh_clear_dirty@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_fget_pp __memp_fget_pp@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_fget __memp_fget@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_fcreate_pp __memp_fcreate_pp@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_fcreate __memp_fcreate@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_set_clear_len __memp_set_clear_len@DB_VERSION_UNIQUE_NAME@
994491
@@ -1395,10 +1396,11 @@
994491
 #define	__memp_fopen __memp_fopen@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_fclose_pp __memp_fclose_pp@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_fclose __memp_fclose@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_mf_discard __memp_mf_discard@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_inmemlist __memp_inmemlist@DB_VERSION_UNIQUE_NAME@
994491
+#define	__memp_mf_mark_dead __memp_mf_mark_dead@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_fput_pp __memp_fput_pp@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_fput __memp_fput@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_unpin_buffers __memp_unpin_buffers@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_dirty __memp_dirty@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_shared __memp_shared@DB_VERSION_UNIQUE_NAME@
994491
@@ -1453,10 +1455,11 @@
994491
 #define	__memp_fsync_pp __memp_fsync_pp@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_fsync __memp_fsync@DB_VERSION_UNIQUE_NAME@
994491
 #define	__mp_xxx_fh __mp_xxx_fh@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_sync_int __memp_sync_int@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_mf_sync __memp_mf_sync@DB_VERSION_UNIQUE_NAME@
994491
+#define	__memp_purge_dead_files __memp_purge_dead_files@DB_VERSION_UNIQUE_NAME@
994491
 #define	__memp_trickle_pp __memp_trickle_pp@DB_VERSION_UNIQUE_NAME@
994491
 #define	__mutex_alloc __mutex_alloc@DB_VERSION_UNIQUE_NAME@
994491
 #define	__mutex_alloc_int __mutex_alloc_int@DB_VERSION_UNIQUE_NAME@
994491
 #define	__mutex_free __mutex_free@DB_VERSION_UNIQUE_NAME@
994491
 #define	__mutex_free_int __mutex_free_int@DB_VERSION_UNIQUE_NAME@
994491
diff -U 5 -r db-5.3.21.old/src/dbinc_auto/mp_ext.h db-5.3.21/src/dbinc_auto/mp_ext.h
994491
--- db-5.3.21.old/src/dbinc_auto/mp_ext.h	2012-05-12 01:57:53.000000000 +0800
994491
+++ db-5.3.21/src/dbinc_auto/mp_ext.h	2016-10-25 22:40:58.000000000 +0800
994491
@@ -14,10 +14,11 @@
994491
 int __memp_failchk __P((ENV *));
994491
 int __memp_bhwrite __P((DB_MPOOL *, DB_MPOOL_HASH *, MPOOLFILE *, BH *, int));
994491
 int __memp_pgread __P((DB_MPOOLFILE *, BH *, int));
994491
 int __memp_pg __P((DB_MPOOLFILE *, db_pgno_t, void *, int));
994491
 int __memp_bhfree __P((DB_MPOOL *, REGINFO *, MPOOLFILE *, DB_MPOOL_HASH *, BH *, u_int32_t));
994491
+void __memp_bh_clear_dirty __P((ENV*, DB_MPOOL_HASH *, BH *));
994491
 int __memp_fget_pp __P((DB_MPOOLFILE *, db_pgno_t *, DB_TXN *, u_int32_t, void *));
994491
 int __memp_fget __P((DB_MPOOLFILE *, db_pgno_t *, DB_THREAD_INFO *, DB_TXN *, u_int32_t, void *));
994491
 int __memp_fcreate_pp __P((DB_ENV *, DB_MPOOLFILE **, u_int32_t));
994491
 int __memp_fcreate __P((ENV *, DB_MPOOLFILE **));
994491
 int __memp_set_clear_len __P((DB_MPOOLFILE *, u_int32_t));
994491
@@ -38,10 +39,11 @@
994491
 int __memp_fopen __P((DB_MPOOLFILE *, MPOOLFILE *, const char *, const char **, u_int32_t, int, size_t));
994491
 int __memp_fclose_pp __P((DB_MPOOLFILE *, u_int32_t));
994491
 int __memp_fclose __P((DB_MPOOLFILE *, u_int32_t));
994491
 int __memp_mf_discard __P((DB_MPOOL *, MPOOLFILE *, int));
994491
 int __memp_inmemlist __P((ENV *, char ***, int *));
994491
+void __memp_mf_mark_dead __P((DB_MPOOL *, MPOOLFILE *, int*));
994491
 int __memp_fput_pp __P((DB_MPOOLFILE *, void *, DB_CACHE_PRIORITY, u_int32_t));
994491
 int __memp_fput __P((DB_MPOOLFILE *, DB_THREAD_INFO *, void *, DB_CACHE_PRIORITY));
994491
 int __memp_unpin_buffers __P((ENV *, DB_THREAD_INFO *));
994491
 int __memp_dirty __P((DB_MPOOLFILE *, void *, DB_THREAD_INFO *, DB_TXN *, DB_CACHE_PRIORITY, u_int32_t));
994491
 int __memp_shared __P((DB_MPOOLFILE *, void *));
994491
@@ -96,10 +98,11 @@
994491
 int __memp_fsync_pp __P((DB_MPOOLFILE *));
994491
 int __memp_fsync __P((DB_MPOOLFILE *));
994491
 int __mp_xxx_fh __P((DB_MPOOLFILE *, DB_FH **));
994491
 int __memp_sync_int __P((ENV *, DB_MPOOLFILE *, u_int32_t, u_int32_t, u_int32_t *, int *));
994491
 int __memp_mf_sync __P((DB_MPOOL *, MPOOLFILE *, int));
994491
+int __memp_purge_dead_files __P((ENV *));
994491
 int __memp_trickle_pp __P((DB_ENV *, int, int *));
994491
 
994491
 #if defined(__cplusplus)
994491
 }
994491
 #endif
994491
diff -U 5 -r db-5.3.21.old/src/mp/mp_bh.c db-5.3.21/src/mp/mp_bh.c
994491
--- db-5.3.21.old/src/mp/mp_bh.c	2012-05-12 01:57:53.000000000 +0800
994491
+++ db-5.3.21/src/mp/mp_bh.c	2016-10-25 17:09:35.000000000 +0800
994491
@@ -472,15 +472,12 @@
994491
 	 * a shared latch.
994491
 	 */
994491
 	if (F_ISSET(bhp, BH_DIRTY | BH_TRASH)) {
994491
 		MUTEX_LOCK(env, hp->mtx_hash);
994491
 		DB_ASSERT(env, !SH_CHAIN_HASNEXT(bhp, vc));
994491
-		if (ret == 0 && F_ISSET(bhp, BH_DIRTY)) {
994491
-			F_CLR(bhp, BH_DIRTY | BH_DIRTY_CREATE);
994491
-			DB_ASSERT(env, atomic_read(&hp->hash_page_dirty) > 0);
994491
-			atomic_dec(env, &hp->hash_page_dirty);
994491
-		}
994491
+		if (ret == 0)
994491
+			__memp_bh_clear_dirty(env, hp, bhp);
994491
 
994491
 		/* put the page back if necessary. */
994491
 		if ((ret != 0 || BH_REFCOUNT(bhp) > 1) &&
994491
 		    F_ISSET(bhp, BH_TRASH)) {
994491
 			ret = __memp_pg(dbmfp, bhp->pgno, bhp->buf, 1);
994491
@@ -686,5 +683,31 @@
994491
 	} else
994491
 		MUTEX_UNLOCK(env, mfp->mutex);
994491
 
994491
 	return (ret);
994491
 }
994491
+
994491
+/*
994491
+ * __memp_bh_clear_dirty --
994491
+ *	Clear the dirty flag of of a buffer. Calls on the same buffer must be
994491
+ *	serialized to get the accounting correct. This can be achieved by
994491
+ *	acquiring an exclusive lock on the buffer, a shared lock on the
994491
+ *	buffer plus an exclusive lock on the hash bucket, or some other
994491
+ *	mechanism that guarantees single-thread access to the entire region
994491
+ *	(e.g. during __memp_region_bhfree()).
994491
+ *
994491
+ * PUBLIC: void __memp_bh_clear_dirty __P((ENV*, DB_MPOOL_HASH *, BH *));
994491
+ */
994491
+void
994491
+__memp_bh_clear_dirty(env, hp, bhp)
994491
+	ENV *env;
994491
+	DB_MPOOL_HASH *hp;
994491
+	BH *bhp;
994491
+{
994491
+	COMPQUIET(env, env);
994491
+	if (F_ISSET(bhp, BH_DIRTY)) {
994491
+		F_CLR(bhp, BH_DIRTY | BH_DIRTY_CREATE);
994491
+		DB_ASSERT(env, atomic_read(&hp->hash_page_dirty) > 0);
994491
+		(void)atomic_dec(env, &hp->hash_page_dirty);
994491
+	}
994491
+}
994491
+
994491
diff -U 5 -r db-5.3.21.old/src/mp/mp_fget.c db-5.3.21/src/mp/mp_fget.c
994491
--- db-5.3.21.old/src/mp/mp_fget.c	2012-05-12 01:57:53.000000000 +0800
994491
+++ db-5.3.21/src/mp/mp_fget.c	2016-10-25 17:11:08.000000000 +0800
994491
@@ -437,16 +437,11 @@
994491
 		 * complain and get out.
994491
 		 */
994491
 		if (flags == DB_MPOOL_FREE) {
994491
 freebuf:		MUTEX_LOCK(env, hp->mtx_hash);
994491
 			h_locked = 1;
994491
-			if (F_ISSET(bhp, BH_DIRTY)) {
994491
-				F_CLR(bhp, BH_DIRTY | BH_DIRTY_CREATE);
994491
-				DB_ASSERT(env,
994491
-				   atomic_read(&hp->hash_page_dirty) > 0);
994491
-				atomic_dec(env, &hp->hash_page_dirty);
994491
-			}
994491
+			__memp_bh_clear_dirty(env, hp, bhp);
994491
 
994491
 			/*
994491
 			 * If the buffer we found is already freed, we're done.
994491
 			 * If the ref count is not 1 then someone may be
994491
 			 * peeking at the buffer.  We cannot free it until they
994491
diff -U 5 -r db-5.3.21.old/src/mp/mp_fopen.c db-5.3.21/src/mp/mp_fopen.c
994491
--- db-5.3.21.old/src/mp/mp_fopen.c	2012-05-12 01:57:53.000000000 +0800
994491
+++ db-5.3.21/src/mp/mp_fopen.c	2016-10-25 22:31:05.000000000 +0800
994491
@@ -12,10 +12,11 @@
994491
 #include "dbinc/log.h"
994491
 #include "dbinc/mp.h"
994491
 #include "dbinc/db_page.h"
994491
 #include "dbinc/hash.h"
994491
 
994491
+static int __memp_count_dead_mutex __P((DB_MPOOL *, u_int32_t *));
994491
 static int __memp_mpf_alloc __P((DB_MPOOL *,
994491
     DB_MPOOLFILE *, const char *, u_int32_t, u_int32_t, MPOOLFILE **));
994491
 static int __memp_mpf_find __P((ENV *,
994491
     DB_MPOOLFILE *, DB_MPOOL_HASH *, const char *, u_int32_t, MPOOLFILE **));
994491
 
994491
@@ -709,11 +710,15 @@
994491
 		 * We should be able to set mfp to NULL and break out of the
994491
 		 * loop, but I like the idea of checking all the entries.
994491
 		 */
994491
 		if (LF_ISSET(DB_TRUNCATE)) {
994491
 			MUTEX_LOCK(env, mfp->mutex);
994491
-			mfp->deadfile = 1;
994491
+			/*
994491
+			 * We cannot purge dead files here, because the caller
994491
+			 * is holding the mutex of the hash bucket of mfp.
994491
+			 */
994491
+			__memp_mf_mark_dead(dbmp, mfp, NULL);
994491
 			MUTEX_UNLOCK(env, mfp->mutex);
994491
 			continue;
994491
 		}
994491
 
994491
 		/*
994491
@@ -907,14 +912,15 @@
994491
 	DB_MPOOL *dbmp;
994491
 	ENV *env;
994491
 	MPOOLFILE *mfp;
994491
 	char *rpath;
994491
 	u_int32_t ref;
994491
-	int deleted, ret, t_ret;
994491
+	int deleted, purge_dead, ret, t_ret;
994491
 
994491
 	env = dbmfp->env;
994491
 	dbmp = env->mp_handle;
994491
+	purge_dead = 0;
994491
 	ret = 0;
994491
 
994491
 	/*
994491
 	 * Remove the DB_MPOOLFILE from the process' list.
994491
 	 *
994491
@@ -1004,11 +1010,11 @@
994491
 	}
994491
 	DB_ASSERT(env, mfp->neutral_cnt < mfp->mpf_cnt);
994491
 	if (--mfp->mpf_cnt == 0 || LF_ISSET(DB_MPOOL_DISCARD)) {
994491
 		if (LF_ISSET(DB_MPOOL_DISCARD) ||
994491
 		    F_ISSET(mfp, MP_TEMP) || mfp->unlink_on_close) {
994491
-			mfp->deadfile = 1;
994491
+			__memp_mf_mark_dead(dbmp, mfp, &purge_dead);
994491
 		}
994491
 		if (mfp->unlink_on_close) {
994491
 			if ((t_ret = __db_appname(dbmp->env, DB_APP_DATA,
994491
 			    R_ADDR(dbmp->reginfo, mfp->path_off), NULL,
994491
 			    &rpath)) != 0 && ret == 0)
994491
@@ -1037,10 +1043,12 @@
994491
 			deleted = 1;
994491
 		}
994491
 	}
994491
 	if (!deleted && !LF_ISSET(DB_MPOOL_NOLOCK))
994491
 		MUTEX_UNLOCK(env, mfp->mutex);
994491
+	if (purge_dead)
994491
+		(void)__memp_purge_dead_files(env);
994491
 
994491
 done:	/* Discard the DB_MPOOLFILE structure. */
994491
 	if (dbmfp->pgcookie != NULL) {
994491
 		__os_free(env, dbmfp->pgcookie->data);
994491
 		__os_free(env, dbmfp->pgcookie);
994491
@@ -1091,11 +1099,11 @@
994491
 	/*
994491
 	 * We have to release the MPOOLFILE mutex before acquiring the region
994491
 	 * mutex so we don't deadlock.  Make sure nobody ever looks at this
994491
 	 * structure again.
994491
 	 */
994491
-	mfp->deadfile = 1;
994491
+	__memp_mf_mark_dead(dbmp, mfp, NULL);
994491
 
994491
 	/* Discard the mutex we're holding and return it too the pool. */
994491
 	MUTEX_UNLOCK(env, mfp->mutex);
994491
 	if ((t_ret = __mutex_free(env, &mfp->mutex)) != 0 && ret == 0)
994491
 		ret = t_ret;
994491
@@ -1216,5 +1224,106 @@
994491
 	/* Make sure we don't return any garbage. */
994491
 	*cntp = 0;
994491
 	*namesp = NULL;
994491
 	return (ret);
994491
 }
994491
+
994491
+/*
994491
+ * __memp_mf_mark_dead --
994491
+ *	Mark an MPOOLFILE as dead because its contents are no longer necessary.
994491
+ *	This happens when removing, truncation, or closing an unnamed in-memory
994491
+ *	database. Return, in the purgep parameter, whether the caller should
994491
+ *	call __memp_purge_dead_files() after the lock on mfp is released. The
994491
+ *	caller must hold an exclusive lock on the mfp handle.
994491
+ *
994491
+ * PUBLIC: void __memp_mf_mark_dead __P((DB_MPOOL *, MPOOLFILE *, int*));
994491
+ */
994491
+void
994491
+__memp_mf_mark_dead(dbmp, mfp, purgep)
994491
+	DB_MPOOL *dbmp;	
994491
+	MPOOLFILE *mfp;
994491
+	int *purgep;
994491
+{
994491
+	ENV *env;
994491
+#ifdef HAVE_MUTEX_SUPPORT
994491
+	REGINFO *infop;
994491
+	DB_MUTEXREGION *mtxregion;
994491
+	u_int32_t mutex_max, mutex_inuse, dead_mutex;
994491
+#endif
994491
+
994491
+	if (purgep != NULL)
994491
+		*purgep = 0;
994491
+
994491
+	env = dbmp->env;
994491
+
994491
+#ifdef HAVE_MUTEX_SUPPORT
994491
+	MUTEX_REQUIRED(env, mfp->mutex);
994491
+
994491
+	if (MUTEX_ON(env) && mfp->deadfile == 0) {
994491
+		infop = &env->mutex_handle->reginfo;
994491
+		mtxregion = infop->primary;
994491
+
994491
+		mutex_inuse = mtxregion->stat.st_mutex_inuse;
994491
+		if ((mutex_max = env->dbenv->mutex_max) == 0)
994491
+			mutex_max = infop->rp->max / mtxregion->mutex_size;
994491
+
994491
+		/*
994491
+		 * Purging dead pages requires a full scan of the entire cache
994491
+		 * buffer, so it is a slow operation. We only want to do it
994491
+		 * when it is necessary and provides enough benefits. Below is
994491
+		 * a simple heuristic that determines when to purge all dead
994491
+		 * pages.
994491
+		 */
994491
+		if (purgep != NULL && mutex_inuse > mutex_max - 200) {
994491
+			/*
994491
+			 * If the mutex region is almost full and there are
994491
+			 * many mutexes held by dead files, purge dead files.
994491
+			 */
994491
+			(void)__memp_count_dead_mutex(dbmp, &dead_mutex);
994491
+			dead_mutex += mfp->block_cnt + 1;
994491
+
994491
+			if (dead_mutex > mutex_inuse / 20)
994491
+				*purgep = 1;
994491
+		}
994491
+	}
994491
+#endif
994491
+
994491
+	mfp->deadfile = 1;
994491
+}
994491
+
994491
+/*
994491
+ * __memp_count_dead_mutex --
994491
+ *	Estimate the number of mutexes held by dead files.
994491
+ */
994491
+static int
994491
+__memp_count_dead_mutex(dbmp, dead_mutex)
994491
+	DB_MPOOL *dbmp;
994491
+	u_int32_t *dead_mutex;
994491
+{
994491
+	ENV *env;
994491
+	DB_MPOOL_HASH *hp;
994491
+	MPOOL *mp;
994491
+	MPOOLFILE *mfp;
994491
+	u_int32_t mutex_per_file;
994491
+	int busy, i;
994491
+
994491
+	env = dbmp->env;
994491
+	*dead_mutex = 0;
994491
+	mutex_per_file = 1;
994491
+#ifndef HAVE_ATOMICFILEREAD
994491
+	mutex_per_file = 2;
994491
+#endif
994491
+	mp = dbmp->reginfo[0].primary;
994491
+	hp = R_ADDR(dbmp->reginfo, mp->ftab);
994491
+	for (i = 0; i < MPOOL_FILE_BUCKETS; i++, hp++) {
994491
+		busy = MUTEX_TRYLOCK(env, hp->mtx_hash);
994491
+		if (busy)
994491
+			continue;
994491
+		SH_TAILQ_FOREACH(mfp, &hp->hash_bucket, q, __mpoolfile) {
994491
+			if (mfp->deadfile)
994491
+				*dead_mutex += mfp->block_cnt + mutex_per_file;
994491
+		}
994491
+		MUTEX_UNLOCK(env, hp->mtx_hash);
994491
+	}
994491
+
994491
+	return (0);
994491
+}
994491
diff -U 5 -r db-5.3.21.old/src/mp/mp_method.c db-5.3.21/src/mp/mp_method.c
994491
--- db-5.3.21.old/src/mp/mp_method.c	2012-05-12 01:57:53.000000000 +0800
994491
+++ db-5.3.21/src/mp/mp_method.c	2016-10-25 17:22:23.000000000 +0800
994491
@@ -638,11 +638,11 @@
994491
 	DB_MPOOL_HASH *hp, *nhp;
994491
 	MPOOL *mp;
994491
 	MPOOLFILE *mfp;
994491
 	roff_t newname_off;
994491
 	u_int32_t bucket;
994491
-	int locked, ret;
994491
+	int locked, purge_dead, ret;
994491
 	size_t nlen;
994491
 	void *p;
994491
 
994491
 #undef	op_is_remove
994491
 #define	op_is_remove	(newname == NULL)
994491
@@ -655,10 +655,11 @@
994491
 	dbmp = NULL;
994491
 	mfp = NULL;
994491
 	nhp = NULL;
994491
 	p = NULL;
994491
 	locked = ret = 0;
994491
+	purge_dead = 0;
994491
 
994491
 	if (!MPOOL_ON(env))
994491
 		goto fsop;
994491
 
994491
 	dbmp = env->mp_handle;
994491
@@ -747,11 +748,11 @@
994491
 		 * they do not get reclaimed as long as they exist.  Since we
994491
 		 * are now deleting the database, we need to dec that count.
994491
 		 */
994491
 		if (mfp->no_backing_file)
994491
 			mfp->mpf_cnt--;
994491
-		mfp->deadfile = 1;
994491
+		__memp_mf_mark_dead(dbmp, mfp, &purge_dead);
994491
 		MUTEX_UNLOCK(env, mfp->mutex);
994491
 	} else {
994491
 		/*
994491
 		 * Else, it's a rename.  We've allocated memory for the new
994491
 		 * name.  Swap it with the old one.  If it's in memory we
994491
@@ -806,10 +807,16 @@
994491
 	if (locked == 1) {
994491
 		MUTEX_UNLOCK(env, hp->mtx_hash);
994491
 		if (nhp != NULL && nhp != hp)
994491
 			MUTEX_UNLOCK(env, nhp->mtx_hash);
994491
 	}
994491
+	/* 
994491
+	 * __memp_purge_dead_files() must be called when the hash bucket is
994491
+	 * unlocked.
994491
+	 */
994491
+	if (purge_dead)
994491
+		(void)__memp_purge_dead_files(env);
994491
 	return (ret);
994491
 }
994491
 
994491
 /*
994491
  * __memp_ftruncate __
994491
diff -U 5 -r db-5.3.21.old/src/mp/mp_sync.c db-5.3.21/src/mp/mp_sync.c
994491
--- db-5.3.21.old/src/mp/mp_sync.c	2012-05-12 01:57:53.000000000 +0800
994491
+++ db-5.3.21/src/mp/mp_sync.c	2016-10-25 17:26:58.000000000 +0800
994491
@@ -24,10 +24,11 @@
994491
 static int __bhcmp __P((const void *, const void *));
994491
 static int __memp_close_flush_files __P((ENV *, int));
994491
 static int __memp_sync_files __P((ENV *));
994491
 static int __memp_sync_file __P((ENV *,
994491
 		MPOOLFILE *, void *, u_int32_t *, u_int32_t));
994491
+static inline void __update_err_ret(int, int*);
994491
 
994491
 /*
994491
  * __memp_walk_files --
994491
  * PUBLIC: int __memp_walk_files __P((ENV *, MPOOL *,
994491
  * PUBLIC:	int (*) __P((ENV *, MPOOLFILE *, void *,
994491
@@ -961,5 +962,125 @@
994491
 		return (-1);
994491
 	if (bhp1->track_pgno > bhp2->track_pgno)
994491
 		return (1);
994491
 	return (0);
994491
 }
994491
+
994491
+/*
994491
+ * __memp_purge_dead_files --
994491
+ *	Remove all dead files and their buffers from the mpool. The caller
994491
+ *	cannot hold any lock on the dead MPOOLFILE handles, their buffers
994491
+ *	or their hash buckets.
994491
+ *
994491
+ * PUBLIC: int __memp_purge_dead_files __P((ENV *));
994491
+ */
994491
+int
994491
+__memp_purge_dead_files(env)
994491
+	ENV *env;
994491
+{
994491
+	BH *bhp;
994491
+	DB_MPOOL *dbmp;
994491
+	DB_MPOOL_HASH *hp, *hp_end;
994491
+	REGINFO *infop;
994491
+	MPOOL *c_mp, *mp;
994491
+	MPOOLFILE *mfp;
994491
+	u_int32_t i_cache;
994491
+	int ret, t_ret, h_lock;
994491
+
994491
+	if (!MPOOL_ON(env))
994491
+		return (0);
994491
+
994491
+	dbmp = env->mp_handle;
994491
+	mp = dbmp->reginfo[0].primary;
994491
+	ret = t_ret = h_lock = 0;
994491
+
994491
+	/*
994491
+	 * Walk each cache's list of buffers and free all buffers whose
994491
+	 * MPOOLFILE is marked as dead.
994491
+	 */
994491
+	for (i_cache = 0; i_cache < mp->nreg; i_cache++) {
994491
+		infop = &dbmp->reginfo[i_cache]; 
994491
+		c_mp = infop->primary;
994491
+
994491
+		hp = R_ADDR(infop, c_mp->htab);
994491
+		hp_end = &hp[c_mp->htab_buckets];
994491
+		for (; hp < hp_end; hp++) {
994491
+			/* Skip empty buckets. */
994491
+			if (SH_TAILQ_FIRST(&hp->hash_bucket, __bh) == NULL)
994491
+				continue;
994491
+
994491
+			/* 
994491
+			 * Search for a dead buffer. Other places that call
994491
+			 * __memp_bhfree() acquire the buffer lock before the
994491
+			 * hash bucket lock. Even though we acquire the two
994491
+			 * locks in reverse order, we cannot deadlock here
994491
+			 * because we don't block waiting for the locks.
994491
+			 */
994491
+			t_ret = MUTEX_TRYLOCK(env, hp->mtx_hash);
994491
+			if (t_ret != 0) {
994491
+				__update_err_ret(t_ret, &ret;;
994491
+				continue;
994491
+			}
994491
+			h_lock = 1;
994491
+			SH_TAILQ_FOREACH(bhp, &hp->hash_bucket, hq, __bh) {
994491
+				/* Skip buffers that are being used. */
994491
+				if (BH_REFCOUNT(bhp) > 0)
994491
+					continue;
994491
+
994491
+				mfp = R_ADDR(dbmp->reginfo, bhp->mf_offset);
994491
+				if (!mfp->deadfile)
994491
+					continue;
994491
+
994491
+				/* Found a dead buffer. Prepare to free it. */
994491
+				t_ret = MUTEX_TRYLOCK(env, bhp->mtx_buf);
994491
+				if (t_ret != 0) {
994491
+					__update_err_ret(t_ret, &ret;;
994491
+					continue;
994491
+				}
994491
+
994491
+				DB_ASSERT(env, (!F_ISSET(bhp, BH_EXCLUSIVE) &&
994491
+				    BH_REFCOUNT(bhp) == 0));
994491
+				F_SET(bhp, BH_EXCLUSIVE);
994491
+				(void)atomic_inc(env, &bhp->ref);
994491
+
994491
+				__memp_bh_clear_dirty(env, hp, bhp);
994491
+
994491
+				/*
994491
+				 * Free the buffer. The buffer and hash bucket
994491
+				 * are unlocked by __memp_bhfree.
994491
+				 */
994491
+				if ((t_ret = __memp_bhfree(dbmp, infop, mfp,
994491
+				    hp, bhp, BH_FREE_FREEMEM)) == 0)
994491
+					/*
994491
+					 * Decrement hp, so the next turn will
994491
+					 * search the same bucket again.
994491
+					 */
994491
+					hp--;
994491
+				else
994491
+					__update_err_ret(t_ret, &ret;;
994491
+
994491
+				/*
994491
+				 * The hash bucket is unlocked, we need to
994491
+				 * start over again.
994491
+				 */
994491
+				h_lock = 0;
994491
+				break;
994491
+			}
994491
+
994491
+			if (h_lock) {
994491
+				MUTEX_UNLOCK(env, hp->mtx_hash);
994491
+				h_lock = 0;
994491
+			}
994491
+		}
994491
+	}
994491
+
994491
+	return (ret);
994491
+}
994491
+
994491
+static inline void
994491
+__update_err_ret(t_ret, retp)
994491
+	int t_ret;
994491
+	int *retp;
994491
+{
994491
+	if (t_ret != 0 && t_ret != DB_LOCK_NOTGRANTED && *retp == 0)
994491
+		*retp = t_ret;
994491
+}
994491
diff -U 5 -r db-5.3.21.old/src/mp/mp_trickle.c db-5.3.21/src/mp/mp_trickle.c
994491
--- db-5.3.21.old/src/mp/mp_trickle.c	2012-05-12 01:57:53.000000000 +0800
994491
+++ db-5.3.21/src/mp/mp_trickle.c	2016-10-25 17:27:57.000000000 +0800
994491
@@ -65,10 +65,14 @@
994491
 	    "DB_ENV->memp_trickle: %d: percent must be between 1 and 100",
994491
 		    "%d"), pct);
994491
 		return (EINVAL);
994491
 	}
994491
 
994491
+	/* First we purge all dead files and their buffers. */
994491
+	if ((ret = __memp_purge_dead_files(env)) != 0)
994491
+		return (ret);
994491
+
994491
 	/*
994491
 	 * Loop through the caches counting total/dirty buffers.
994491
 	 *
994491
 	 * XXX
994491
 	 * Using hash_page_dirty is our only choice at the moment, but it's not
994491
diff -U 5 -r db-5.3.21.old/src/mutex/mut_region.c db-5.3.21/src/mutex/mut_region.c
994491
--- db-5.3.21.old/src/mutex/mut_region.c	2012-05-12 01:57:54.000000000 +0800
994491
+++ db-5.3.21/src/mutex/mut_region.c	2016-10-25 17:34:22.000000000 +0800
994491
@@ -15,11 +15,11 @@
994491
 #include "dbinc/txn.h"
994491
 
994491
 static db_size_t __mutex_align_size __P((ENV *));
994491
 static int __mutex_region_init __P((ENV *, DB_MUTEXMGR *));
994491
 static size_t __mutex_region_size __P((ENV *));
994491
-static size_t __mutex_region_max __P((ENV *));
994491
+static size_t __mutex_region_max __P((ENV *, u_int32_t));
994491
 
994491
 /*
994491
  * __mutex_open --
994491
  *	Open a mutex region.
994491
  *
994491
@@ -32,11 +32,11 @@
994491
 {
994491
 	DB_ENV *dbenv;
994491
 	DB_MUTEXMGR *mtxmgr;
994491
 	DB_MUTEXREGION *mtxregion;
994491
 	size_t size;
994491
-	u_int32_t cpu_count;
994491
+	u_int32_t cpu_count, mutex_needed;
994491
 	int ret;
994491
 #ifndef HAVE_ATOMIC_SUPPORT
994491
 	u_int i;
994491
 #endif
994491
 
994491
@@ -59,23 +59,24 @@
994491
 		    cpu_count : cpu_count * MUTEX_SPINS_PER_PROCESSOR)) != 0)
994491
 			return (ret);
994491
 	}
994491
 
994491
 	/*
994491
-	 * If the user didn't set an absolute value on the number of mutexes
994491
-	 * we'll need, figure it out.  We're conservative in our allocation,
994491
-	 * we need mutexes for DB handles, group-commit queues and other things
994491
-	 * applications allocate at run-time.  The application may have kicked
994491
-	 * up our count to allocate its own mutexes, add that in.
994491
+	 * Figure out the number of mutexes we'll need.  We're conservative in
994491
+	 * our allocation, we need mutexes for DB handles, group-commit queues
994491
+	 * and other things applications allocate at run-time.  The application
994491
+	 * may have kicked up our count to allocate its own mutexes, add that
994491
+	 * in.
994491
 	 */
994491
+	mutex_needed =
994491
+	    __lock_region_mutex_count(env) +
994491
+	    __log_region_mutex_count(env) +
994491
+	    __memp_region_mutex_count(env) +
994491
+	    __txn_region_mutex_count(env);
994491
 	if (dbenv->mutex_cnt == 0 &&
994491
 	    F_ISSET(env, ENV_PRIVATE | ENV_THREAD) != ENV_PRIVATE)
994491
-		dbenv->mutex_cnt =
994491
-		    __lock_region_mutex_count(env) +
994491
-		    __log_region_mutex_count(env) +
994491
-		    __memp_region_mutex_count(env) +
994491
-		    __txn_region_mutex_count(env);
994491
+		dbenv->mutex_cnt = mutex_needed;
994491
 
994491
 	if (dbenv->mutex_max != 0 && dbenv->mutex_cnt > dbenv->mutex_max)
994491
 		dbenv->mutex_cnt = dbenv->mutex_max;
994491
 
994491
 	/* Create/initialize the mutex manager structure. */
994491
@@ -88,12 +89,12 @@
994491
 	mtxmgr->reginfo.id = INVALID_REGION_ID;
994491
 	mtxmgr->reginfo.flags = REGION_JOIN_OK;
994491
 	size = __mutex_region_size(env);
994491
 	if (create_ok)
994491
 		F_SET(&mtxmgr->reginfo, REGION_CREATE_OK);
994491
-	if ((ret = __env_region_attach(env,
994491
-	    &mtxmgr->reginfo, size, size + __mutex_region_max(env))) != 0)
994491
+	if ((ret = __env_region_attach(env, &mtxmgr->reginfo,
994491
+	    size, size + __mutex_region_max(env, mutex_needed))) != 0)
994491
 		goto err;
994491
 
994491
 	/* If we created the region, initialize it. */
994491
 	if (F_ISSET(&mtxmgr->reginfo, REGION_CREATE))
994491
 		if ((ret = __mutex_region_init(env, mtxmgr)) != 0)
994491
@@ -350,44 +351,62 @@
994491
 
994491
 	dbenv = env->dbenv;
994491
 
994491
 	s = sizeof(DB_MUTEXMGR) + 1024;
994491
 
994491
-	/* We discard one mutex for the OOB slot. */
994491
+	/* 
994491
+	 * We discard one mutex for the OOB slot. Make sure mutex_cnt doesn't
994491
+	 * overflow.
994491
+	 */
994491
 	s += __env_alloc_size(
994491
-	    (dbenv->mutex_cnt + 1) *__mutex_align_size(env));
994491
+	    (dbenv->mutex_cnt + (dbenv->mutex_cnt == UINT32_MAX ? 0 : 1)) *
994491
+	    __mutex_align_size(env));
994491
 
994491
 	return (s);
994491
 }
994491
 
994491
 /*
994491
  * __mutex_region_max --
994491
  *	 Return the amount of space needed to reach the maximum size.
994491
  */
994491
 static size_t
994491
-__mutex_region_max(env)
994491
+__mutex_region_max(env, mutex_needed)
994491
 	ENV *env;
994491
+	u_int32_t mutex_needed;
994491
 {
994491
 	DB_ENV *dbenv;
994491
-	u_int32_t max;
994491
+	u_int32_t max, mutex_cnt;
994491
 
994491
 	dbenv = env->dbenv;
994491
+	mutex_cnt = dbenv->mutex_cnt;
994491
 
994491
-	if ((max = dbenv->mutex_max) == 0) {
994491
+	/*
994491
+	 * We want to limit the region size to accommodate at most UINT32_MAX
994491
+	 * mutexes. If mutex_cnt is UINT32_MAX, no more space is allowed.
994491
+	 */
994491
+	if ((max = dbenv->mutex_max) == 0 && mutex_cnt != UINT32_MAX)
994491
 		if (F_ISSET(env, ENV_PRIVATE | ENV_THREAD) == ENV_PRIVATE)
994491
-			max = dbenv->mutex_inc + 1;
994491
-		else
994491
+			if (dbenv->mutex_inc + 1 < UINT32_MAX - mutex_cnt)
994491
+				max = dbenv->mutex_inc + 1 + mutex_cnt;
994491
+			else
994491
+				max = UINT32_MAX;
994491
+		else {
994491
 			max = __lock_region_mutex_max(env) +
994491
 			    __txn_region_mutex_max(env) +
994491
 			    __log_region_mutex_max(env) +
994491
 			    dbenv->mutex_inc + 100;
994491
-	} else if (max <= dbenv->mutex_cnt)
994491
+			if (max < UINT32_MAX - mutex_needed)
994491
+				max += mutex_needed;
994491
+			else
994491
+				max = UINT32_MAX;
994491
+		}
994491
+
994491
+	if (max <= mutex_cnt)
994491
 		return (0);
994491
 	else
994491
-		max -= dbenv->mutex_cnt;
994491
-
994491
-	return ( __env_alloc_size(max * __mutex_align_size(env)));
994491
+		return (__env_alloc_size(
994491
+		    (max - mutex_cnt) * __mutex_align_size(env)));
994491
 }
994491
 
994491
 #ifdef	HAVE_MUTEX_SYSTEM_RESOURCES
994491
 /*
994491
  * __mutex_resource_return
994491