linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] locks: try to catch potential deadlock between file-private and classic locks from same process
@ 2014-03-04 19:10 Jeff Layton
  2014-03-04 19:19 ` Andy Lutomirski
  2014-03-04 19:35 ` J. Bruce Fields
  0 siblings, 2 replies; 18+ messages in thread
From: Jeff Layton @ 2014-03-04 19:10 UTC (permalink / raw)
  To: linux-fsdevel; +Cc: bfields, luto

My expectation is that programs shouldn't mix classic and file-private
locks, but Glenn Skinner pointed out to me that that may occur at times
even if the programmer isn't aware.

Suppose we have a program that uses file-private locks. That program
then links in a library that uses classic POSIX locks. If those locks
end up conflicting and one is using blocking locks, then the program
could end up deadlocked.

Try to catch this situation in posix_locks_deadlock by looking 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 occurs.

This check is not perfect. You could (in principle) have a threaded
process that is using classic locks in one thread and file-private locks
in another. That's not necessarily a deadlockable situation but this
check would cause an EDEADLK return in that case.

By the same token, you could also have a file-private lock that 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.

Signed-off-by: Jeff Layton <jlayton@redhat.com>
---
 fs/locks.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/fs/locks.c b/fs/locks.c
index 6fdf26a79cc8..19c0c5c24b93 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -790,7 +790,17 @@ static int posix_locks_deadlock(struct file_lock *caller_fl,
 	int i = 0;
 
 	/*
-	 * This deadlock detector can't reasonably detect deadlocks with
+	 * If one lock is file-private and the other one isn't, and these are
+	 * owned by the same process, then we may be in a situation where
+	 * a library is attempting to use a different locking flavor than the
+	 * original program.
+	 */
+	if (caller_fl->fl_pid == block_fl->fl_pid &&
+	    IS_FILE_PVT(caller_fl) != IS_FILE_PVT(block_fl))
+		return 1;
+
+	/*
+	 * This deadlock detector can't reasonably detect cyclic deadlocks with
 	 * FL_FILE_PVT locks, since they aren't owned by a process, per-se.
 	 */
 	if (IS_FILE_PVT(caller_fl))
-- 
1.8.5.3


^ permalink raw reply related	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2014-03-06 18:58 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-04 19:10 [PATCH] locks: try to catch potential deadlock between file-private and classic locks from same process Jeff Layton
2014-03-04 19:19 ` Andy Lutomirski
2014-03-04 19:35 ` J. Bruce Fields
2014-03-04 20:14   ` Jeff Layton
2014-03-04 20:19     ` Andy Lutomirski
2014-03-04 20:37       ` Jeff Layton
2014-03-04 20:40         ` J. Bruce Fields
2014-03-04 20:44           ` Jeff Layton
2014-03-04 20:52         ` Trond Myklebust
2014-03-04 21:14           ` Dr Fields James Bruce
2014-03-04 21:24             ` Jeff Layton
2014-03-04 22:42             ` Trond Myklebust
2014-03-04 22:56               ` Dr Fields James Bruce
2014-03-04 23:50                 ` Trond Myklebust
2014-03-06 18:41                   ` Dr Fields James Bruce
2014-03-06 18:58                     ` Trond Myklebust
2014-03-04 21:21           ` Jeff Layton
2014-03-04 20:35     ` J. Bruce Fields

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).