Blame SOURCES/libdb-cbd-race.patch

7809d0
From 4ae2eb88fadc256ddf9862b2e72ed216ddbb919d Mon Sep 17 00:00:00 2001
7809d0
From: michael brey <michael.brey@oracle.com>
7809d0
Date: Tue, 20 May 2014 14:49:44 +0200
7809d0
Subject: [PATCH] Fix a CDB race
7809d0
7809d0
Report and reproducer here:
7809d0
https://community.oracle.com/thread/3514381
7809d0
7809d0
From: michael brey <michael.brey@oracle.com>
7809d0
To: Lubomir Rintel <lkundrak@v3.sk>
7809d0
Subject: Re: BDB crash
7809d0
Date: Tue, 13 May 2014 09:07:45 -0600 (05/13/2014 05:07:45 PM)
7809d0
Message-id: <53723541.7040203@oracle.com>
7809d0
7809d0
  attached are patches for each release.  the 5.3.28 patch will apply on
7809d0
top of 5.3.21.
7809d0
7809d0
thanks
7809d0
mike
7809d0
7809d0
RHBZ: #1099509
7809d0
---
7809d0
 src/env/env_failchk.c | 24 ++++++++++++++++++++++++
7809d0
 src/mutex/mut_tas.c   | 18 +++++++++++++++++-
7809d0
 2 files changed, 41 insertions(+), 1 deletion(-)
7809d0
7809d0
diff --git a/src/env/env_failchk.c b/src/env/env_failchk.c
7809d0
index 05752f0..b09df96 100644
7809d0
--- a/src/env/env_failchk.c
7809d0
+++ b/src/env/env_failchk.c
7809d0
@@ -312,6 +312,7 @@ __env_in_api(env)
7809d0
 	REGINFO *infop;
7809d0
 	THREAD_INFO *thread;
7809d0
 	u_int32_t i;
7809d0
+	pid_t pid;
7809d0
 	int unpin, ret;
7809d0
 
7809d0
 	if ((htab = env->thr_hashtab) == NULL)
7809d0
@@ -325,6 +326,7 @@ __env_in_api(env)
7809d0
 
7809d0
 	for (i = 0; i < env->thr_nbucket; i++)
7809d0
 		SH_TAILQ_FOREACH(ip, &htab[i], dbth_links, __db_thread_info) {
7809d0
+			pid = ip->dbth_pid;
7809d0
 			if (ip->dbth_state == THREAD_SLOT_NOT_IN_USE ||
7809d0
 			    (ip->dbth_state == THREAD_OUT &&
7809d0
 			    thread->thr_count <  thread->thr_max))
7809d0
@@ -341,6 +343,28 @@ __env_in_api(env)
7809d0
 				ip->dbth_state = THREAD_SLOT_NOT_IN_USE;
7809d0
 				continue;
7809d0
 			}
7809d0
+			/*
7809d0
+			 * The above tests are not atomic, so it is possible that
7809d0
+			 * the process pointed by ip has changed during the tests.
7809d0
+			 * In particular, if the process pointed by ip when is_alive
7809d0
+			 * was executed terminated normally, a new process may reuse
7809d0
+			 * the same ip structure and change its dbth_state before the
7809d0
+			 * next two tests were performed. Therefore, we need to test
7809d0
+			 * here that all four tests above are done on the same process.
7809d0
+			 * If the process pointed by ip changed, all tests are invalid
7809d0
+			 * and can be ignored.
7809d0
+			 * Similarly, it's also possible for two processes racing to
7809d0
+			 * change the dbth_state of the same ip structure. For example,
7809d0
+			 * both process A and B reach the above test for the same
7809d0
+			 * terminated process C where C's dbth_state is THREAD_OUT.
7809d0
+			 * If A goes into the 'if' block and changes C's dbth_state to
7809d0
+			 * THREAD_SLOT_NOT_IN_USE before B checks the condition, B
7809d0
+			 * would incorrectly fail the test and run into this line.
7809d0
+			 * Therefore, we need to check C's dbth_state again and fail
7809d0
+			 * the db only if C's dbth_state is indeed THREAD_ACTIVE.
7809d0
+			 */
7809d0
+			if (ip->dbth_state != THREAD_ACTIVE || ip->dbth_pid != pid)
7809d0
+				continue;
7809d0
 			return (__db_failed(env, DB_STR("1507",
7809d0
 			    "Thread died in Berkeley DB library"),
7809d0
 			    ip->dbth_pid, ip->dbth_tid));
7809d0
diff --git a/src/mutex/mut_tas.c b/src/mutex/mut_tas.c
7809d0
index 0899d23..db95030 100644
7809d0
--- a/src/mutex/mut_tas.c
7809d0
+++ b/src/mutex/mut_tas.c
7809d0
@@ -151,10 +151,26 @@ loop:	/* Attempt to acquire the resource for N spins. */
7809d0
 			if (F_ISSET(dbenv, DB_ENV_FAILCHK) &&
7809d0
 			    ip == NULL && dbenv->is_alive(dbenv,
7809d0
 			    mutexp->pid, mutexp->tid, 0) == 0) {
7809d0
+				/*
7809d0
+				 * The process owing the mutex is "dead" now, but it may
7809d0
+				 * have already released the mutex. We need to check again
7809d0
+				 * by going back to the top of the loop
7809d0
+				 * if the mutex is still held by the "dead" process. We
7809d0
+				 * yield 10 us to increase the likelyhood of mutexp fields
7809d0
+				 * being up-to-date. Set spin so we spin one more time
7809d0
+				 * because no need to spin more if dead process owns mutex.
7809d0
+				 */                               
7809d0
+				if (nspins > 1) {
7809d0
+					nspins = 2;
7809d0
+					__os_yield(env, 0, 10);
7809d0
+					continue;
7809d0
+				}
7809d0
 				ret = __env_set_state(env, &ip, THREAD_VERIFY);
7809d0
 				if (ret != 0 ||
7809d0
-				    ip->dbth_state == THREAD_FAILCHK)
7809d0
+				    ip->dbth_state == THREAD_FAILCHK) {
7809d0
+					printf("mut_tas:172, pid: %d, flag: %d\n", mutexp->pid, mutexp->flags);
7809d0
 					return (DB_RUNRECOVERY);
7809d0
+				}
7809d0
 			}
7809d0
 			if (nowait)
7809d0
 				return (DB_LOCK_NOTGRANTED);
7809d0
-- 
7809d0
1.8.3.1
7809d0