From owner-freebsd-current@FreeBSD.ORG Fri Feb 6 11:26:03 2004 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4CC1116A4CE for ; Fri, 6 Feb 2004 11:26:03 -0800 (PST) Received: from mail3.speakeasy.net (mail3.speakeasy.net [216.254.0.203]) by mx1.FreeBSD.org (Postfix) with ESMTP id 149EF43D5D for ; Fri, 6 Feb 2004 11:25:27 -0800 (PST) (envelope-from jhb@FreeBSD.org) Received: (qmail 5894 invoked from network); 6 Feb 2004 19:24:50 -0000 Received: from dsl027-160-063.atl1.dsl.speakeasy.net (HELO server.baldwin.cx) ([216.27.160.63]) (envelope-sender ) encrypted SMTP for ; 6 Feb 2004 19:24:50 -0000 Received: from 10.50.40.205 (gw1.twc.weather.com [216.133.140.1]) by server.baldwin.cx (8.12.10/8.12.10) with ESMTP id i16JONMC019640; Fri, 6 Feb 2004 14:24:45 -0500 (EST) (envelope-from jhb@FreeBSD.org) From: John Baldwin To: popsong old Date: Fri, 6 Feb 2004 13:49:43 -0500 User-Agent: KMail/1.5.4 References: <20040206020616.12702.qmail@web60606.mail.yahoo.com> In-Reply-To: <20040206020616.12702.qmail@web60606.mail.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200402061349.43213.jhb@FreeBSD.org> X-Spam-Checker-Version: SpamAssassin 2.55 (1.174.2.19-2003-05-19-exp) cc: freebsd-current@freebsd.org Subject: Re: three locks and lock order reversal? X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 Feb 2004 19:26:03 -0000 On Thursday 05 February 2004 09:06 pm, popsong old wrote: > > If one thread does A then B, another thread does B then C, and a third > > thread does C then A you can deadlock if each thread gets the first lock > > and blocks on the second lock. Thread 1 wants B and holds A, thread 2 > > holds B and wants C, and thread 3 wants A and holds C. Thread 3 will not > > giveup C until it gets A. Thread 1 holds A and won't give it up until it > > gets B. Thread 2 holds B and won't give it up until it gets C which is > > held by thread 3. Hence, deadlock. > > > > -- > > John Baldwin <>< http://www.FreeBSD.org/~jhb/ > > "Power Users Use the Power to Serve" = http://www.FreeBSD.org > > Thank you for the explanation! I only thought of two threads. So this bring > me another question: if the machine only have 2 CPUs, does it mean that > this kind of LOR is safe, provided there is no other sleep lock between A > and B, B and C, C and A? Default mutexes block instead of spin when they are contested, so you can have a deadlock on a UP machine like so: Thread 1 locks A and is preempted by an interrupt. Thread 2 gets to run before thread 1, locks B, and is preempted by an interrupt. Thread 3 runs next, locks C, and blocks on A. It lends its priority to thread 1 which then runs next and blocks on B. Thread 2 then gets max(thread1, thread3)'s priority and blocks on C. Now you are deadlocked on a UP machine. -- John Baldwin <>< http://www.FreeBSD.org/~jhb/ "Power Users Use the Power to Serve" = http://www.FreeBSD.org