From owner-freebsd-stable@FreeBSD.ORG Thu May 8 08:37:02 2008 Return-Path: Delivered-To: freebsd-stable@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 659341065670 for ; Thu, 8 May 2008 08:37:02 +0000 (UTC) (envelope-from dfr@rabson.org) Received: from itchy.rabson.org (unknown [IPv6:2002:50b1:e8f2:1::143]) by mx1.freebsd.org (Postfix) with ESMTP id 1E0548FC0A for ; Thu, 8 May 2008 08:37:02 +0000 (UTC) (envelope-from dfr@rabson.org) Received: from [IPv6:2001:470:909f:1:21b:63ff:feb8:5abc] (unknown [IPv6:2001:470:909f:1:21b:63ff:feb8:5abc]) by itchy.rabson.org (Postfix) with ESMTP id 721EA3F8F; Thu, 8 May 2008 09:37:00 +0100 (BST) Message-Id: <1B6FCF23-413B-452A-B66D-3CCD6257F7BD@rabson.org> From: Doug Rabson To: paul.koch@statseeker.com In-Reply-To: <200805081812.24692.paul.koch@statseeker.com> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v919.2) Date: Thu, 8 May 2008 09:37:00 +0100 References: <200805081812.24692.paul.koch@statseeker.com> X-Mailer: Apple Mail (2.919.2) Cc: freebsd-stable@freebsd.org Subject: Re: flock incorrectly detects deadlock on 7-stable and current X-BeenThere: freebsd-stable@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Production branch of FreeBSD source code List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 08 May 2008 08:37:02 -0000 On 8 May 2008, at 09:12, Paul Koch wrote: > Hi, > > We have been trying to track down a problem with one of our apps which > does a lot of flock(2) calls. flock returns errno 11 (Resource > deadlock avoided) under certain scenarios. Our app works fine on > 7-Release, but fails on 7-stable and -current. > > The problem appears to be when we have at least three processes doing > flock() on a file, and one is trying to upgrade a shared lock to an > exclusive lock but fails with a deadlock avoided. > > Attached is a simple flock() test program. > > a. Process 1 requests and gets a shared lock > b. Process 2 requests and blocks for an exclusive lock > c. Process 3 requests and gets a shared lock > d. Process 3 requests an upgrade to an exclusive lock but fails (errno > 11) > > If we change 'd' to > Process 3 requests unlock, then requests exclusive lock, it works. Could you possibly try this patch and tell me if it helps: ==== //depot/user/dfr/lockd/sys/kern/kern_lockf.c#57 - /tank/projects/ lockd/src/sys/kern/kern_lockf.c ==== @@ -1370,6 +1370,18 @@ } /* + * For flock type locks, we must first remove + * any shared locks that we hold before we sleep + * waiting for an exclusive lock. + */ + if ((lock->lf_flags & F_FLOCK) && + lock->lf_type == F_WRLCK) { + lock->lf_type = F_UNLCK; + lf_activate_lock(state, lock); + lock->lf_type = F_WRLCK; + } + + /* * We are blocked. Create edges to each blocking lock, * checking for deadlock using the owner graph. For * simplicity, we run deadlock detection for all @@ -1389,17 +1401,6 @@ } /* - * For flock type locks, we must first remove - * any shared locks that we hold before we sleep - * waiting for an exclusive lock. - */ - if ((lock->lf_flags & F_FLOCK) && - lock->lf_type == F_WRLCK) { - lock->lf_type = F_UNLCK; - lf_activate_lock(state, lock); - lock->lf_type = F_WRLCK; - } - /* * We have added edges to everything that blocks * us. Sleep until they all go away. */