af_unix: split 'u->readlock' into two: 'iolock' and 'bindlock'
Subsystems (3)
PATCH 2/2 af_unix: split 'u->readlock' into two: 'iolock' and 'bindlock'
From: Linus Torvalds <[email protected]>
Date: Thu, 1 Sep 2016 14:43:53 -0700
Subject: [PATCH 2/2] af_unix: split 'u->readlock' into two: 'iolock' and 'bindlock'
Right now we use the 'readlock' both for protecting some of the af_unix IO path and for making the bind be single-threaded.
The two are independent, but using the same lock makes for a nasty deadlock due to ordering with regards to filesystem locking. The bind locking would want to nest outside the VSF pathname locking, but the IO locking wants to nest inside some of those same locks.
We tried to fix this earlier with commit c845acb324aa ("af_unix: Fix splice-bind deadlock") which moved the readlock inside the vfs locks, but that caused problems with overlayfs that will then call back into filesystem routines that take the lock in the wrong order anyway.
Splitting the locks means that we can go back to having the bind lock be the outermost lock, and we don't have any deadlocks with lock ordering.
---
This patch is really trivial, and I've tried to be careful and look at the locking, but somebody who really knows the AF_UNIX code should definitely take a second look.
Note that I did the revert (that re-introduces the original splice deadlock) first, because that made the whole series much easier to explain. Doing it in the other order made the revert nastier because this patch obviously touches the same code that the revert in 1/2 does.
So this way the series ends up being "go back to the original code with the original deadlock, and then fix that original deadlock by splitting the bind lock".
include/net/af_unix.h | 2 +- net/unix/af_unix.c | 45 +++++++++++++++++++++++---------------------- 2 files changed, 24 insertions(+), 23 deletions(-)
include/net/af_unix.h +1 -1
@@ -52,7 +52,7 @@ struct unix_sock {struct sock sk;struct unix_address *addr;struct path path; -struct mutex readlock; +struct mutex iolock, bindlock;struct sock *peer;struct list_head link;atomic_long_t inflight;
net/unix/af_unix.c +23 -22
165 lines not on this page · raw message
Signature · 1 line
2.10.0.rc0.2.g0a9fa47
Quoted 18 lines · Linus Torvalds
From: Linus Torvalds <[email protected]>
Date: Thu, 1 Sep 2016 14:43:53 -0700
Subject: [PATCH 2/2]af_unix: split 'u->readlock' into two: 'iolock' and 'bindlock'Right now we use the 'readlock' both for protecting some of the
af_unixIO path and for making the bind be single-threaded.The two are independent, but using the same lock makes for a nasty deadlock due to ordering with regards to filesystem locking. The bind locking would want to nest outside the VSF pathname locking, but the IO locking wants to nest inside some of those same locks.
We tried to fix this earlier with commit
c845acb324aa("af_unix: Fix splice-bind deadlock") which moved the readlock inside the vfs locks, but that caused problems with overlayfs that will then call back into filesystem routines that take the lock in the wrong order anyway.
Quoted 6 lines
Splitting the locks means that we can go back to having the bind lock be the outermost lock, and we don't have any deadlocks with lock ordering.
Acked-by Rainer Weikusat <[email protected]>Acked-by Al Viro <[email protected]>Signed-off-by Linus Torvalds <[email protected]>
Oh, this was missing a
who found the new deadlock.
There's now *another* lockdep deadlock report by him, but that one has nothing to do with networking.
(And neither of these deadlocks will actually deadlock the machine in practice, but you can trigger the lockdep reports with some odd splice patterns and overlayfs use)
Linus
David Miller
maintainerFrom: Linus Torvalds <[email protected]>
Date: Fri, 2 Sep 2016 11:17:18 -0700
Quoted 4 lines
Oh, this was missing a
Reported-and-tested-by CAI Qian <[email protected]>
Quoted 8 lines
who found the new deadlock.
There's now *another* lockdep deadlock report by him, but that one has nothing to do with networking.
(And neither of these deadlocks will actually deadlock the machine in practice, but you can trigger the lockdep reports with some odd splice patterns and overlayfs use)
I read over this and can't find any problems.
The main thing I was concerned about was an I/O path that really expects the socket's hash not to change for whatever reason, but even all of the unix_find_other() calls are done outside of the mutex already.
Quoted 5 lines · David Miller
The main thing I was concerned about was an I/O path that really expects the socket's hash not to change for whatever reason, but even all of the
unix_find_other()calls are done outside of the mutex already.
That's my read of it too. I did actually go through the effort on trying to follow all the locking in there yesterday, so while I would want an AF_UNIX person to double-check me, I think it's fine.
The real locking seem to be the u->lock spinlock (taken by unix_state_lock() and friends).
The bindlock is only about serializing the binders, which do things that can sleep (notably the mknod of the unix socket node in the filesystem).
The one thing I looked at and wasn't sure about was unix_stream_connect(), which creates a *new* unix domain socket and binds it to the old one without holding the bindlock. Note that it never did hold the lock, so that's not a new thing, but it worries me a bit.
I *think* it's ok, because I think this all happens before that new socket is actually reachable, so it cannot race against somebody else doing a bind. And my patch doesn't actually change anything in this area, so I'm not making it worse. But it was the one thing I reacted to when going through the locking there.
(Well, not the "one thing". The other thing I reacted to was the overall reaction that none of this is documented, and the locking was clearly not very clear).
Linus
Quoted 16 lines · David Miller
From: Linus Torvalds <[email protected]>
Date: Fri, 2 Sep 2016 11:17:18 -0700Quoted 4 lines
Oh, this was missing a
Reported-and-tested-by CAI Qian <[email protected]>Quoted 8 lines
who found the new deadlock.
There's now *another* lockdep deadlock report by him, but that one has nothing to do with networking.
(And neither of these deadlocks will actually deadlock the machine in practice, but you can trigger the lockdep reports with some odd splice patterns and overlayfs use)
Quoted 6 lines
I read over this and can't find any problems.
The main thing I was concerned about was an I/O path that really expects the socket's hash not to change for whatever reason, but even all of the
unix_find_other()calls are done outside of the mutex already.
Furthermore we don't allow rehashing, if the socket is "published" once, it can only be destroyed but can't change identity during its lifetime. IIRC this was another bug we had a while ago.
Quoted 30 lines · Linus Torvalds
From: Linus Torvalds <[email protected]>
Date: Thu, 1 Sep 2016 14:43:53 -0700
Subject: [PATCH 2/2]af_unix: split 'u->readlock' into two: 'iolock' and 'bindlock'Right now we use the 'readlock' both for protecting some of the
af_unixIO path and for making the bind be single-threaded.The two are independent, but using the same lock makes for a nasty deadlock due to ordering with regards to filesystem locking. The bind locking would want to nest outside the VSF pathname locking, but the IO locking wants to nest inside some of those same locks.
We tried to fix this earlier with commit
c845acb324aa("af_unix: Fix splice-bind deadlock") which moved the readlock inside the vfs locks, but that caused problems with overlayfs that will then call back into filesystem routines that take the lock in the wrong order anyway.Splitting the locks means that we can go back to having the bind lock be the outermost lock, and we don't have any deadlocks with lock ordering.
Acked-by Rainer Weikusat <[email protected]>Acked-by Al Viro <[email protected]>Signed-off-by Linus Torvalds <[email protected]>---
This patch is really trivial, and I've tried to be careful and look at the locking, but somebody who really knows the
AF_UNIXcode should definitely take a second look.
Quoted 8 lines
Note that I did the revert (that re-introduces the original splice deadlock) first, because that made the whole series much easier to explain. Doing it in the other order made the revert nastier because this patch obviously touches the same code that the revert in 1/2 does.
So this way the series ends up being "go back to the original code with the original deadlock, and then fix that original deadlock by splitting the bind lock".
From: Linus Torvalds <[email protected]>
Date: Fri, 2 Sep 2016 11:13:09 -0700 (PDT)
Quoted 18 lines
From: Linus Torvalds <[email protected]>
Date: Thu, 1 Sep 2016 14:43:53 -0700
Subject: [PATCH 2/2]af_unix: split 'u->readlock' into two: 'iolock' and 'bindlock'Right now we use the 'readlock' both for protecting some of the
af_unixIO path and for making the bind be single-threaded.The two are independent, but using the same lock makes for a nasty deadlock due to ordering with regards to filesystem locking. The bind locking would want to nest outside the VSF pathname locking, but the IO locking wants to nest inside some of those same locks.
We tried to fix this earlier with commit
c845acb324aa("af_unix: Fix splice-bind deadlock") which moved the readlock inside the vfs locks, but that caused problems with overlayfs that will then call back into filesystem routines that take the lock in the wrong order anyway.
Quoted 6 lines
Splitting the locks means that we can go back to having the bind lock be the outermost lock, and we don't have any deadlocks with lock ordering.
Acked-by Rainer Weikusat <[email protected]>Acked-by Al Viro <[email protected]>Signed-off-by Linus Torvalds <[email protected]>
Applied.