Blame SOURCES/libdb-cbd-race.patch

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