From owner-freebsd-hackers@freebsd.org Thu Jul 16 14:13:29 2015 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 624EA9A1390 for ; Thu, 16 Jul 2015 14:13:29 +0000 (UTC) (envelope-from pjalaber@gmail.com) Received: from mail-qg0-x22d.google.com (mail-qg0-x22d.google.com [IPv6:2607:f8b0:400d:c04::22d]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 20C261E61; Thu, 16 Jul 2015 14:13:29 +0000 (UTC) (envelope-from pjalaber@gmail.com) Received: by qgy5 with SMTP id 5so33052206qgy.3; Thu, 16 Jul 2015 07:13:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=EZHR0JTRGeDg1NKa02WC+HlGCLrpwKh1hD4hqIYfHHc=; b=P8ImltVXsHLM8BG5+Iwltim7TL70PnlYdX1L+DIpnW6wK5fwaKRy5/+CZHm/rBdfy0 MQpQyrKbFA9/nJzgslSNNUx0FU9m1ZZuS3fImi6bNBUoeL1i9Tmy8weNQXa9KxHxuVkG nx6ips1RewuuIIkAbNKqIkDXnBmE5FJ9iqSRrYzV9DA/DNBEL+LN8Qs5wqm3tsPyBYtb mtj/Ewczkl23vsWYlVByAkeVjwWQf3pXXE45PhQH45Kk9Dllp7kRPa/SdzIbo+qei/PW SdajBcg3/Uu/9jdKojIznJb55Isf5aV7S7OD/NIJKE/UXzOM80Inv7Oucf/nGBU8hKuA aMmA== MIME-Version: 1.0 X-Received: by 10.140.146.83 with SMTP id 80mr11067267qhs.76.1437056007987; Thu, 16 Jul 2015 07:13:27 -0700 (PDT) Received: by 10.140.92.247 with HTTP; Thu, 16 Jul 2015 07:13:27 -0700 (PDT) In-Reply-To: References: Date: Thu, 16 Jul 2015 16:13:27 +0200 Message-ID: Subject: Re: adaptive rwlock deadlock From: Philippe Jalaber To: freebsd-hackers@freebsd.org Cc: jhb@freebsd.org, attilio@freebsd.org Content-Type: text/plain; charset=UTF-8 X-Content-Filtered-By: Mailman/MimeDel 2.1.20 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Jul 2015 14:13:29 -0000 2015-07-07 12:10 GMT+02:00 Philippe Jalaber : > Hi, > > I am facing a strange problem using the network stack and adaptive rwlocks > running Freebsd 9.3. > Basically I can reproduce the problem with 3 threads: > > 1) thread 1 has taken the rwlock of structure inpcb in exclusive mode in > tcp_input.c. This thread also runs my own code and repeatedly takes a > rwlock (called g_rwlock) in shared mode and releases it, until a shared > object is marked not "busy" any more: > > rwlock(inp_lock); > .... > do { // thread is active waiting in the loop > rlock(g_rwlock); > o = find(); > if ( o == NULL ) > break; > busy = o.busy; > if (o != NULL && busy) > runlock(g_rwlock); > } while ( busy ); > > if ( o != NULL ) > { > // do something with o > .... > } > runlock(g_rwlock); > .... > > 2) thread 2 wants to set the shared object as "ready". So it tries to take > g_rwlock in exclusive mode and is blocked in _rw_wlock_hard@kern_rwlock.c:815 > "turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE)" because thread 1 has > already taken it in shared mode: > > wlock(g_rwlock); > o = find(); > if ( o != NULL ) > o.busy = 1; > wunlock(g_rwlock); > > // o is busy so work on it without any lock > .... > > wlock(g_rwlock); // thread is blocked here > o.busy = 0; > maybe_delete(o); > wunlock(g_rwlock); > > 3) thread 3 spins on the same inpcb rwlock than thread 1 in > _rw_wlock_hard@kern_rwlock.c:721 "while ((struct > thread*)RW_OWNER(rw->rw_lock) == owner && TD_IS_RUNNING(owner)) " > > > My target machine has two cpus. > Thread 1 is pinned to cpu 0. > Thread 2 and Thread 3 are pinned to cpu 1. > Thread 1 and Thread 2 have a priority of 28. > Thread 3 has a priority of 127 > > Now what seems to happen is that when thread 1 calls runlock(g_rwlock), it > calls turnstile_broadcast@kern_rwlock.c:650, but thread 2 never regains > control because thread 3 is spinning on the inpcb rwlock. Also the > condition TD_IS_RUNNING(owner) is always true because thread 1 is active > waiting in a loop. So the 3 threads deadlock. > Note that if I compile the kernel without adaptive rwlocks it works > without any problem. > A workaround is to add a call to "sched_relinquish(curthread)" in thread 1 > in the loop just after the call to runlock. > > I am also wondering about the code in _rw_runlock after > "turnstile_broadcast(ts, queue)". Isn't the flag RW_LOCK_WRITE_WAITERS > definitely lost if the other thread which is blocked in turnstile_wait > never regains control ? > > Thank you for your time, > Regards, > Philippe > > the sched_relinquish workaround does not seem to work every time. one possible solution (which seems to work) is to rlock/runlock in thread 1, and if the busy flag is set, then take the lock in exclusive mode, like this: shared_count = 0; rwlock(inp_lock); .... do { // thread is active waiting in the loop if ( shared_count == 0 ) rlock(g_rwlock); else wlock(g_rwlock); o = find(); if ( o == NULL ) break; busy = o.busy; if (o != NULL && busy) { if ( shared_count == 0 ) runlock(g_rwlock); else wunlock(g_rwlock); shared_count++; } } while ( busy ); if ( o != NULL ) { // do something with o .... } if ( shared_count == 0 ) runlock(g_rwlock); else wunlock(g_rwlock); with this code, deadlock does not happen anymore but I don't really see why. Any idea ? Thanks, Philippe