Commit 9f541801 authored by Dave Chinner's avatar Dave Chinner Committed by Al Viro

xfs: concurrent readdir hangs on data buffer locks

There's a three-process deadlock involving shared/exclusive barriers
and inverted lock orders in the directory readdir implementation.
It's a pre-existing problem with lock ordering, exposed by the
VFS parallelisation code.

process 1               process 2               process 3
---------               ---------               ---------
readdir
iolock(shared)
  get_leaf_dents
    iterate entries
       ilock(shared)
       map, lock and read buffer
       iunlock(shared)
       process entries in buffer
       .....
                                                readdir
                                                iolock(shared)
                                                  get_leaf_dents
                                                    iterate entries
                                                      ilock(shared)
                                                      map, lock buffer
                                                      <blocks>
                        finish ->iterate_shared
                        file_accessed()
                          ->update_time
                            start transaction
                            ilock(excl)
                            <blocks>
        .....
        finishes processing buffer
        get next buffer
          ilock(shared)
          <blocks>

And that's the deadlock.

Fix this by dropping the current buffer lock in process 1 before
trying to map the next buffer. This means we keep the lock order of
ilock -> buffer lock intact and hence will allow process 3 to make
progress and drop it's ilock(shared) once it is done.
Reported-by: default avatarXiong Zhou <xzhou@redhat.com>
Signed-off-by: default avatarDave Chinner <dchinner@redhat.com>
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent fe742fd4
...@@ -273,10 +273,11 @@ xfs_dir2_leaf_readbuf( ...@@ -273,10 +273,11 @@ xfs_dir2_leaf_readbuf(
size_t bufsize, size_t bufsize,
struct xfs_dir2_leaf_map_info *mip, struct xfs_dir2_leaf_map_info *mip,
xfs_dir2_off_t *curoff, xfs_dir2_off_t *curoff,
struct xfs_buf **bpp) struct xfs_buf **bpp,
bool trim_map)
{ {
struct xfs_inode *dp = args->dp; struct xfs_inode *dp = args->dp;
struct xfs_buf *bp = *bpp; struct xfs_buf *bp = NULL;
struct xfs_bmbt_irec *map = mip->map; struct xfs_bmbt_irec *map = mip->map;
struct blk_plug plug; struct blk_plug plug;
int error = 0; int error = 0;
...@@ -286,13 +287,10 @@ xfs_dir2_leaf_readbuf( ...@@ -286,13 +287,10 @@ xfs_dir2_leaf_readbuf(
struct xfs_da_geometry *geo = args->geo; struct xfs_da_geometry *geo = args->geo;
/* /*
* If we have a buffer, we need to release it and * If the caller just finished processing a buffer, it will tell us
* take it out of the mapping. * we need to trim that block out of the mapping now it is done.
*/ */
if (trim_map) {
if (bp) {
xfs_trans_brelse(NULL, bp);
bp = NULL;
mip->map_blocks -= geo->fsbcount; mip->map_blocks -= geo->fsbcount;
/* /*
* Loop to get rid of the extents for the * Loop to get rid of the extents for the
...@@ -533,10 +531,17 @@ xfs_dir2_leaf_getdents( ...@@ -533,10 +531,17 @@ xfs_dir2_leaf_getdents(
*/ */
if (!bp || ptr >= (char *)bp->b_addr + geo->blksize) { if (!bp || ptr >= (char *)bp->b_addr + geo->blksize) {
int lock_mode; int lock_mode;
bool trim_map = false;
if (bp) {
xfs_trans_brelse(NULL, bp);
bp = NULL;
trim_map = true;
}
lock_mode = xfs_ilock_data_map_shared(dp); lock_mode = xfs_ilock_data_map_shared(dp);
error = xfs_dir2_leaf_readbuf(args, bufsize, map_info, error = xfs_dir2_leaf_readbuf(args, bufsize, map_info,
&curoff, &bp); &curoff, &bp, trim_map);
xfs_iunlock(dp, lock_mode); xfs_iunlock(dp, lock_mode);
if (error || !map_info->map_valid) if (error || !map_info->map_valid)
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment