Blame SOURCES/libdb-cbd-race.patch

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