• Miao Xie's avatar
    Btrfs: fix deadlock on page lock when doing auto-defragment · 600a45e1
    Miao Xie authored
    When I ran xfstests circularly on a auto-defragment btrfs, the deadlock
    happened.
    
    Steps to reproduce:
    [tty0]
     # export MOUNT_OPTIONS="-o autodefrag"
     # export TEST_DEV=<partition1>
     # export TEST_DIR=<mountpoint1>
     # export SCRATCH_DEV=<partition2>
     # export SCRATCH_MNT=<mountpoint2>
     # while [ 1 ]
     > do
     > ./check 091 127 263
     > sleep 1
     > done
    [tty1]
     # while [ 1 ]
     > do
     > echo 3 > /proc/sys/vm/drop_caches
     > done
    
    Several hours later, the test processes will hang on, and the deadlock will
    happen on page lock.
    
    The reason is that:
      Auto defrag task		Flush thread			Test task
    				btrfs_writepages()
    				  add ordered extent
    				  (including page 1, 2)
    				  set page 1 writeback
    				  set page 2 writeback
    				endio_fn()
    				  end page 2 writeback
    								release page 2
    lock page 1
    alloc and lock page 2
    page 2 is not uptodate
      btrfs_readpage()
        start ordered extent()
        btrfs_writepages()
          try  to lock page 1
    
    so deadlock happens.
    
    Fix this bug by unlocking the page which is in writeback, and re-locking it
    after the writeback end.
    Signed-off-by: default avatarMiao Xie <miax@cn.fujitsu.com>
    600a45e1
ioctl.c 78.9 KB