Skip to content

Commit

Permalink
[PATCH] tee: link_pipe() must be careful when dropping one of the pip…
Browse files Browse the repository at this point in the history
…e locks

We need to ensure that we only drop a lock that is ordered last, to avoid
ABBA deadlocks with competing processes.

Signed-off-by: Jens Axboe <axboe@suse.de>
  • Loading branch information
Jens Axboe committed Apr 19, 2006
1 parent c4f895c commit 2a27250
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions fs/splice.c
Original file line number Diff line number Diff line change
Expand Up @@ -1012,14 +1012,17 @@ static int link_pipe(struct pipe_inode_info *ipipe,
size_t len, unsigned int flags)
{
struct pipe_buffer *ibuf, *obuf;
int ret = 0, do_wakeup = 0, i;
int ret, do_wakeup, i, ipipe_first;

ret = do_wakeup = ipipe_first = 0;

/*
* Potential ABBA deadlock, work around it by ordering lock
* grabbing by inode address. Otherwise two different processes
* could deadlock (one doing tee from A -> B, the other from B -> A).
*/
if (ipipe->inode < opipe->inode) {
ipipe_first = 1;
mutex_lock(&ipipe->inode->i_mutex);
mutex_lock(&opipe->inode->i_mutex);
} else {
Expand Down Expand Up @@ -1068,9 +1071,11 @@ static int link_pipe(struct pipe_inode_info *ipipe,

/*
* We have input available, but no output room.
* If we already copied data, return that.
* If we already copied data, return that. If we
* need to drop the opipe lock, it must be ordered
* last to avoid deadlocks.
*/
if (flags & SPLICE_F_NONBLOCK) {
if ((flags & SPLICE_F_NONBLOCK) || !ipipe_first) {
if (!ret)
ret = -EAGAIN;
break;
Expand Down Expand Up @@ -1104,7 +1109,12 @@ static int link_pipe(struct pipe_inode_info *ipipe,
if (ret)
break;
}
if (flags & SPLICE_F_NONBLOCK) {
/*
* pipe_wait() drops the ipipe mutex. To avoid deadlocks
* with another process, we can only safely do that if
* the ipipe lock is ordered last.
*/
if ((flags & SPLICE_F_NONBLOCK) || ipipe_first) {
if (!ret)
ret = -EAGAIN;
break;
Expand Down

0 comments on commit 2a27250

Please sign in to comment.