From: Daniel De Graaf Subject: Re: [PATCH, v2] xenstore: Fix deadlock in xs_read_watch Date: Mon, 30 Aug 2010 09:36:38 -0400 Message-ID: <4C7BB3E6.1080406@tycho.nsa.gov> References: <4C76D9C3.5020600@tycho.nsa.gov> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------050308090908030500010006" Return-path: In-Reply-To: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Stefano Stabellini Cc: xen-devel List-Id: xen-devel@lists.xenproject.org This is a multi-part message in MIME format. --------------050308090908030500010006 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On 08/30/2010 09:16 AM, Stefano Stabellini wrote: > On Thu, 26 Aug 2010, Daniel De Graaf wrote: >> When read_message returns -1 while a read is pending, an attempt is made >> to lock h->request_mutex which is already locked by the reader. Change >> the read thread to only exit on thread cancellation, where >> read_thr_exists will return 0. >> >> Signed-off-by: Daniel De Graaf >> >> diff -r 6bebaf40e925 tools/xenstore/xs.c >> --- a/tools/xenstore/xs.c Tue Jul 20 13:42:17 2010 +0100 >> +++ b/tools/xenstore/xs.c Thu Aug 26 17:08:55 2010 -0400 >> @@ -271,6 +271,7 @@ >> { >> #ifdef USE_PTHREAD >> if (h->read_thr_exists) { >> + h->read_thr_exists = 0; >> pthread_cancel(h->read_thr); >> pthread_join(h->read_thr, NULL); >> } >> @@ -667,7 +668,7 @@ >> mutex_lock(&h->watch_mutex); >> >> /* Wait on the condition variable for a watch to fire. */ >> - while (list_empty(&h->watch_list) && read_thread_exists(h)) >> + while (list_empty(&h->watch_list) && (!read_from_thread || read_thread_exists(h))) >> condvar_wait(&h->watch_condvar, &h->watch_mutex, h); >> if (!read_thread_exists(h)) { >> mutex_unlock(&h->watch_mutex); > > read_from_thread is not declared in this function > Sorry about that, looks like I picked the wrong patch to send. Corrected patch attached. --------------050308090908030500010006 Content-Type: text/plain; name="deadlock-fix.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="deadlock-fix.patch" diff -r 3c4c3d48a835 tools/xenstore/xs.c --- a/tools/xenstore/xs.c Thu Aug 26 11:16:56 2010 +0100 +++ b/tools/xenstore/xs.c Mon Aug 30 09:31:27 2010 -0400 @@ -271,6 +271,7 @@ { #ifdef USE_PTHREAD if (h->read_thr_exists) { + h->read_thr_exists = 0; pthread_cancel(h->read_thr); pthread_join(h->read_thr, NULL); } @@ -666,9 +667,15 @@ mutex_lock(&h->watch_mutex); +#ifdef USE_PTHREAD /* Wait on the condition variable for a watch to fire. */ while (list_empty(&h->watch_list) && read_thread_exists(h)) condvar_wait(&h->watch_condvar, &h->watch_mutex, h); +#else + while (list_empty(&h->watch_list)) + read_message(h); +#endif + if (!read_thread_exists(h)) { mutex_unlock(&h->watch_mutex); errno = EINVAL; @@ -966,13 +973,8 @@ { struct xs_handle *h = arg; - while (read_message(h) != -1) - continue; - - /* Kick anyone waiting for a reply */ - pthread_mutex_lock(&h->request_mutex); - h->read_thr_exists = 0; - pthread_mutex_unlock(&h->request_mutex); + while (h->read_thr_exists) + read_message(h); pthread_mutex_lock(&h->reply_mutex); pthread_cond_signal(&h->reply_condvar); --------------050308090908030500010006 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --------------050308090908030500010006--