Skip to content
This repository was archived by the owner on May 28, 2021. It is now read-only.

Commit 52245f0

Browse files
josefbacikgregkh
authored andcommitted
btrfs: fix potential deadlock in the search ioctl
[ Upstream commit a48b73eca4ceb9b8a4b97f290a065335dbcd8a04 ] With the conversion of the tree locks to rwsem I got the following lockdep splat: ====================================================== WARNING: possible circular locking dependency detected 5.8.0-rc7-00165-g04ec4da5f45f-dirty #922 Not tainted ------------------------------------------------------ compsize/11122 is trying to acquire lock: ffff889fabca8768 (&mm->mmap_lock#2){++++}-{3:3}, at: __might_fault+0x3e/0x90 but task is already holding lock: ffff889fe720fe40 (btrfs-fs-00){++++}-{3:3}, at: __btrfs_tree_read_lock+0x39/0x180 which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #2 (btrfs-fs-00){++++}-{3:3}: down_write_nested+0x3b/0x70 __btrfs_tree_lock+0x24/0x120 btrfs_search_slot+0x756/0x990 btrfs_lookup_inode+0x3a/0xb4 __btrfs_update_delayed_inode+0x93/0x270 btrfs_async_run_delayed_root+0x168/0x230 btrfs_work_helper+0xd4/0x570 process_one_work+0x2ad/0x5f0 worker_thread+0x3a/0x3d0 kthread+0x133/0x150 ret_from_fork+0x1f/0x30 -> #1 (&delayed_node->mutex){+.+.}-{3:3}: __mutex_lock+0x9f/0x930 btrfs_delayed_update_inode+0x50/0x440 btrfs_update_inode+0x8a/0xf0 btrfs_dirty_inode+0x5b/0xd0 touch_atime+0xa1/0xd0 btrfs_file_mmap+0x3f/0x60 mmap_region+0x3a4/0x640 do_mmap+0x376/0x580 vm_mmap_pgoff+0xd5/0x120 ksys_mmap_pgoff+0x193/0x230 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9 -> #0 (&mm->mmap_lock#2){++++}-{3:3}: __lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 __might_fault+0x68/0x90 _copy_to_user+0x1e/0x80 copy_to_sk.isra.32+0x121/0x300 search_ioctl+0x106/0x200 btrfs_ioctl_tree_search_v2+0x7b/0xf0 btrfs_ioctl+0x106f/0x30a0 ksys_ioctl+0x83/0xc0 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9 other info that might help us debug this: Chain exists of: &mm->mmap_lock#2 --> &delayed_node->mutex --> btrfs-fs-00 Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(btrfs-fs-00); lock(&delayed_node->mutex); lock(btrfs-fs-00); lock(&mm->mmap_lock#2); *** DEADLOCK *** 1 lock held by compsize/11122: #0: ffff889fe720fe40 (btrfs-fs-00){++++}-{3:3}, at: __btrfs_tree_read_lock+0x39/0x180 stack backtrace: CPU: 17 PID: 11122 Comm: compsize Kdump: loaded Not tainted 5.8.0-rc7-00165-g04ec4da5f45f-dirty #922 Hardware name: Quanta Tioga Pass Single Side 01-0030993006/Tioga Pass Single Side, BIOS F08_3A18 12/20/2018 Call Trace: dump_stack+0x78/0xa0 check_noncircular+0x165/0x180 __lock_acquire+0x1272/0x2310 lock_acquire+0x9e/0x360 ? __might_fault+0x3e/0x90 ? find_held_lock+0x72/0x90 __might_fault+0x68/0x90 ? __might_fault+0x3e/0x90 _copy_to_user+0x1e/0x80 copy_to_sk.isra.32+0x121/0x300 ? btrfs_search_forward+0x2a6/0x360 search_ioctl+0x106/0x200 btrfs_ioctl_tree_search_v2+0x7b/0xf0 btrfs_ioctl+0x106f/0x30a0 ? __do_sys_newfstat+0x5a/0x70 ? ksys_ioctl+0x83/0xc0 ksys_ioctl+0x83/0xc0 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x50/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xa9 The problem is we're doing a copy_to_user() while holding tree locks, which can deadlock if we have to do a page fault for the copy_to_user(). This exists even without my locking changes, so it needs to be fixed. Rework the search ioctl to do the pre-fault and then copy_to_user_nofault for the copying. CC: [email protected] # 4.4+ Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: Josef Bacik <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 6faf75b commit 52245f0

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

fs/btrfs/extent_io.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -5488,9 +5488,9 @@ void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
54885488
}
54895489
}
54905490

5491-
int read_extent_buffer_to_user(const struct extent_buffer *eb,
5492-
void __user *dstv,
5493-
unsigned long start, unsigned long len)
5491+
int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb,
5492+
void __user *dstv,
5493+
unsigned long start, unsigned long len)
54945494
{
54955495
size_t cur;
54965496
size_t offset;
@@ -5511,7 +5511,7 @@ int read_extent_buffer_to_user(const struct extent_buffer *eb,
55115511

55125512
cur = min(len, (PAGE_SIZE - offset));
55135513
kaddr = page_address(page);
5514-
if (copy_to_user(dst, kaddr + offset, cur)) {
5514+
if (probe_user_write(dst, kaddr + offset, cur)) {
55155515
ret = -EFAULT;
55165516
break;
55175517
}

fs/btrfs/extent_io.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,9 @@ int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
401401
void read_extent_buffer(const struct extent_buffer *eb, void *dst,
402402
unsigned long start,
403403
unsigned long len);
404-
int read_extent_buffer_to_user(const struct extent_buffer *eb,
405-
void __user *dst, unsigned long start,
406-
unsigned long len);
404+
int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb,
405+
void __user *dst, unsigned long start,
406+
unsigned long len);
407407
void write_extent_buffer(struct extent_buffer *eb, const void *src,
408408
unsigned long start, unsigned long len);
409409
void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,

fs/btrfs/ioctl.c

+20-7
Original file line numberDiff line numberDiff line change
@@ -2041,20 +2041,29 @@ static noinline int copy_to_sk(struct btrfs_path *path,
20412041
sh.len = item_len;
20422042
sh.transid = found_transid;
20432043

2044-
/* copy search result header */
2045-
if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2046-
ret = -EFAULT;
2044+
/*
2045+
* Copy search result header. If we fault then loop again so we
2046+
* can fault in the pages and -EFAULT there if there's a
2047+
* problem. Otherwise we'll fault and then copy the buffer in
2048+
* properly this next time through
2049+
*/
2050+
if (probe_user_write(ubuf + *sk_offset, &sh, sizeof(sh))) {
2051+
ret = 0;
20472052
goto out;
20482053
}
20492054

20502055
*sk_offset += sizeof(sh);
20512056

20522057
if (item_len) {
20532058
char __user *up = ubuf + *sk_offset;
2054-
/* copy the item */
2055-
if (read_extent_buffer_to_user(leaf, up,
2056-
item_off, item_len)) {
2057-
ret = -EFAULT;
2059+
/*
2060+
* Copy the item, same behavior as above, but reset the
2061+
* * sk_offset so we copy the full thing again.
2062+
*/
2063+
if (read_extent_buffer_to_user_nofault(leaf, up,
2064+
item_off, item_len)) {
2065+
ret = 0;
2066+
*sk_offset -= sizeof(sh);
20582067
goto out;
20592068
}
20602069

@@ -2142,6 +2151,10 @@ static noinline int search_ioctl(struct inode *inode,
21422151
key.offset = sk->min_offset;
21432152

21442153
while (1) {
2154+
ret = fault_in_pages_writeable(ubuf, *buf_size - sk_offset);
2155+
if (ret)
2156+
break;
2157+
21452158
ret = btrfs_search_forward(root, &key, path, sk->min_transid);
21462159
if (ret != 0) {
21472160
if (ret > 0)

0 commit comments

Comments
 (0)