]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/5.0.4/btrfs-fix-deadlock-between-clone-dedupe-and-rename.patch
Linux 4.19.31
[thirdparty/kernel/stable-queue.git] / releases / 5.0.4 / btrfs-fix-deadlock-between-clone-dedupe-and-rename.patch
1 From 4ea748e1d2c9f8a27332b949e8210dbbf392987e Mon Sep 17 00:00:00 2001
2 From: Filipe Manana <fdmanana@suse.com>
3 Date: Tue, 26 Feb 2019 12:06:09 +0000
4 Subject: Btrfs: fix deadlock between clone/dedupe and rename
5
6 From: Filipe Manana <fdmanana@suse.com>
7
8 commit 4ea748e1d2c9f8a27332b949e8210dbbf392987e upstream.
9
10 Reflinking (clone/dedupe) and rename are operations that operate on two
11 inodes and therefore need to lock them in the same order to avoid ABBA
12 deadlocks. It happens that Btrfs' reflink implementation always locked
13 them in a different order from VFS's lock_two_nondirectories() helper,
14 which is used by the rename code in VFS, resulting in ABBA type deadlocks.
15
16 Btrfs' locking order:
17
18 static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
19 {
20 if (inode1 < inode2)
21 swap(inode1, inode2);
22
23 inode_lock_nested(inode1, I_MUTEX_PARENT);
24 inode_lock_nested(inode2, I_MUTEX_CHILD);
25 }
26
27 VFS's locking order:
28
29 void lock_two_nondirectories(struct inode *inode1, struct inode *inode2)
30 {
31 if (inode1 > inode2)
32 swap(inode1, inode2);
33
34 if (inode1 && !S_ISDIR(inode1->i_mode))
35 inode_lock(inode1);
36 if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1)
37 inode_lock_nested(inode2, I_MUTEX_NONDIR2);
38 }
39
40 Fix this by killing the btrfs helper function that does the double inode
41 locking and replace it with VFS's helper lock_two_nondirectories().
42
43 Reported-by: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
44 Fixes: 416161db9b63e3 ("btrfs: offline dedupe")
45 CC: stable@vger.kernel.org # 4.4+
46 Signed-off-by: Filipe Manana <fdmanana@suse.com>
47 Signed-off-by: David Sterba <dsterba@suse.com>
48 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
49
50 ---
51 fs/btrfs/ioctl.c | 21 +++------------------
52 1 file changed, 3 insertions(+), 18 deletions(-)
53
54 --- a/fs/btrfs/ioctl.c
55 +++ b/fs/btrfs/ioctl.c
56 @@ -3206,21 +3206,6 @@ out:
57 return ret;
58 }
59
60 -static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
61 -{
62 - inode_unlock(inode1);
63 - inode_unlock(inode2);
64 -}
65 -
66 -static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
67 -{
68 - if (inode1 < inode2)
69 - swap(inode1, inode2);
70 -
71 - inode_lock_nested(inode1, I_MUTEX_PARENT);
72 - inode_lock_nested(inode2, I_MUTEX_CHILD);
73 -}
74 -
75 static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
76 struct inode *inode2, u64 loff2, u64 len)
77 {
78 @@ -3989,7 +3974,7 @@ static int btrfs_remap_file_range_prep(s
79 if (same_inode)
80 inode_lock(inode_in);
81 else
82 - btrfs_double_inode_lock(inode_in, inode_out);
83 + lock_two_nondirectories(inode_in, inode_out);
84
85 /*
86 * Now that the inodes are locked, we need to start writeback ourselves
87 @@ -4039,7 +4024,7 @@ static int btrfs_remap_file_range_prep(s
88 if (same_inode)
89 inode_unlock(inode_in);
90 else
91 - btrfs_double_inode_unlock(inode_in, inode_out);
92 + unlock_two_nondirectories(inode_in, inode_out);
93
94 return ret;
95 }
96 @@ -4069,7 +4054,7 @@ loff_t btrfs_remap_file_range(struct fil
97 if (same_inode)
98 inode_unlock(src_inode);
99 else
100 - btrfs_double_inode_unlock(src_inode, dst_inode);
101 + unlock_two_nondirectories(src_inode, dst_inode);
102
103 return ret < 0 ? ret : len;
104 }