From: Jeff Layton Subject: Re: [PATCH] locks: try to catch potential deadlock between file-private and classic locks from same process Date: Tue, 4 Mar 2014 16:24:55 -0500 Message-ID: <20140304162455.6475b7a6@tlielax.poochiereds.net> References: <1393960249-18961-1-git-send-email-jlayton@redhat.com> <20140304193551.GG12805@fieldses.org> <20140304151451.07530a98@tlielax.poochiereds.net> <20140304153723.088db7cd@tlielax.poochiereds.net> <849B7F2D-A1C2-4E65-AE6A-0CCFA92990F0@primarydata.com> <20140304211443.GJ12805@fieldses.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Trond Myklebust , Andy Lutomirski , Linux FS Devel To: Dr Fields James Bruce Return-path: Received: from mx1.redhat.com ([209.132.183.28]:51693 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755709AbaCDVZA convert rfc822-to-8bit (ORCPT ); Tue, 4 Mar 2014 16:25:00 -0500 In-Reply-To: <20140304211443.GJ12805@fieldses.org> Sender: linux-fsdevel-owner@vger.kernel.org List-ID: On Tue, 4 Mar 2014 16:14:43 -0500 Dr Fields James Bruce wrote: > On Tue, Mar 04, 2014 at 03:52:47PM -0500, Trond Myklebust wrote: > >=20 > > On Mar 4, 2014, at 15:37, Jeff Layton wrote: > >=20 > > > On Tue, 4 Mar 2014 12:19:44 -0800 > > > Andy Lutomirski wrote: > > >=20 > > >> On Tue, Mar 4, 2014 at 12:14 PM, Jeff Layton wrote: > > >>> On Tue, 4 Mar 2014 14:35:51 -0500 > > >>> "J. Bruce Fields" wrote: > > >>>=20 > > >>>> On Tue, Mar 04, 2014 at 02:10:49PM -0500, Jeff Layton wrote: > > >>>>> My expectation is that programs shouldn't mix classic and fil= e-private > > >>>>> locks, but Glenn Skinner pointed out to me that that may occu= r at times > > >>>>> even if the programmer isn't aware. > > >>>>>=20 > > >>>>> Suppose we have a program that uses file-private locks. That = program > > >>>>> then links in a library that uses classic POSIX locks. If tho= se locks > > >>>>> end up conflicting and one is using blocking locks, then the = program > > >>>>> could end up deadlocked. > > >>>>>=20 > > >>>>> Try to catch this situation in posix_locks_deadlock by lookin= g for the > > >>>>> case where the blocking lock was set by the same process but = has a > > >>>>> different type, and have the kernel return EDEADLK if that oc= curs. > > >>>>>=20 > > >>>>> This check is not perfect. You could (in principle) have a th= readed > > >>>>> process that is using classic locks in one thread and file-pr= ivate locks > > >>>>> in another. That's not necessarily a deadlockable situation b= ut this > > >>>>> check would cause an EDEADLK return in that case. > > >>>>>=20 > > >>>>> By the same token, you could also have a file-private lock th= at was > > >>>>> inherited across a fork(). If the inheriting process ends up = blocking on > > >>>>> that while trying to set a classic POSIX lock then this check= would miss > > >>>>> it and the program would deadlock. > > >>>>=20 > > >>>> If the caller's not prepared for the library to use classic po= six locks, > > >>>> then it's not going to know how to recover from this EDEADLCK = either, is > > >>>> it? > > >>>>=20 > > >>>=20 > > >>> Well, callers should be aware of that if we take this change. T= he > > >>> semantics aren't yet set in stone... > > >>>=20 > > >>>> I guess I don't understand how this helps anyone. > > >>>>=20 > > >>>> Has it ever made sense for a library function and its caller t= o both use > > >>>> classic posix locking on the same file without any coordinatio= n? > > >>>>=20 > > >>>=20 > > >>> Not really, but that doesn't mean that it isn't done... ;) > > >>>=20 > > >>>> Besides the first-close problem there's the problem that locks= merge, so > > >>>> for example you can't hold your own lock across a call to a fu= nction > > >>>> that grabs and drops a lock on the same file. > > >>>>=20 > > >>>=20 > > >>> It depends, but you're basically correct... > > >>>=20 > > >>> It's likely that if the above situation occurred with a program= using > > >>> classic locks, then those locks were silently lost at times. It= 's also > > >>> plausible that when it occurs that no one is aware of it due to= the way > > >>> POSIX locks work. > > >>>=20 > > >>> If the program switched to using file-private locks and the lib= rary > > >>> stays using classic locks (or vice versa), you then potentially= trade > > >>> that silent loss of locks for a deadlock (since classic and > > >>> file-private locks always conflict). > > >>>=20 > > >>> So, the idea would be to try to catch that situation explicitly= and > > >>> return a hard error instead of deadlocking. Unfortunately, it's= a > > >>> little tough to do that in all cases so all this does is try to= catch a > > >>> subset of them. > > >>>=20 > > >>> Will it be helpful in the long run? I'm not sure. It seems unli= kely to > > >>> harm legit use cases though, and might catch some problematic > > >>> situations. I can drop this if that's the consensus however. > > >>=20 > > >> I don't think I like it except in the case where there are no th= reads > > >> (number of tasks sharing the fd table is 1) and where the struct= file > > >> only has one fd. Otherwise I think it can have false positives.= Or > > >> am I missing something? > > >>=20 > > >=20 > > > The only case where I think this would hit a false positive is if= you > > > have a threaded program that's doing something weird like having = one > > > thread that's setting classic POSIX locks on a file, and one thre= ad > > > that isn't. Once you hit a conflict between the two, you'd get ba= ck > > > EDEADLK on one of them, even though that situation might not actu= ally > > > be a deadlock. > > >=20 > > > That doesn't really seem like a real-world use-case though, so I'= m > > > generally OK with that potential false-positive. > > >=20 > >=20 > > How do these locks interact with locks_mandatory_area(), and mandat= ory locking in general? Unless I missed something, it looks to me as if= there is a nasty potential for a self-DOS if you set a file-private lo= ck on a file with the mandatory lock bits set and the filesystem is mou= nted =E2=80=98-omand'. >=20 > Good point: if I understand it right, in the mandatory locking case, > before doing a read or write we first check if we'd be able to apply = a > classic posix lock. And that lock will always conflict with a > file-private lock. >=20 > I think we should just not worry about it and see if anyone complains= =2E > File-private locks are a new feature and I don't see that we're under > any obligation to support the combination of file-private locks and > mandatory locking. >=20 > Mandatory locking is already buggy (because of the race between check= ing > for locks and performing the IO). If we get no complaints about this > file-private behavior then that's more evidence we could use to justi= fy > just ripping it out completely some day.... >=20 > But if we really want to be helpful to (possibly nonexistant?) users = of > mandatory locking, maybe we could allow locks_mandatory_area to try > *both* a file-private and a classic lock and to succeed if either one > succeeds?? >=20 Yeah, I think that's what we'll have to do. In principle it shouldn't be too hard to do, but I'll have look at the most efficient way to handle it. --=20 Jeff Layton -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel= " in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html