Blame SOURCES/libdb-cbd-race.patch

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