Skip to contentSkip to conversation

Btrfs: fix crash due to not cleaning up tree log block's dirty bits

PATCH In review Liu Bo 2 participants 2 messages Jan 25, 2018 – Jan 26, 2018 Btrfs file system

lore
Share & tools
lore

Liu Bo

author
#0 lore raw

PATCH 1/1 Btrfs: fix crash due to not cleaning up tree log block's dirty bits

In cases that the whole fs flips into readonly status due to failures in critical sections, then log tree's blocks are still dirty, and this leads to a crash during umount time, the crash is about use-after-free,

umount
-> close_ctree

    -> stop workers
    -> iput(btree_inode)
       -> iput_final
          -> write_inode_now
	     -> ...
	       -> queue job on stop'd workers
cc <[email protected]> v3.12+
Fixes 681ae50917df ("Btrfs: cleanup reserved space when freeing tree log on error")
---
 fs/btrfs/tree-log.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/btrfs/tree-log.c +9 -0
index 7bf9b31..22046ed 100644
@@ -2494,6 +2494,9 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
 					clean_tree_block(fs_info, next);
 					btrfs_wait_tree_block_writeback(next);
 					btrfs_tree_unlock(next);
+				} else {
+					if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
+						clear_extent_buffer_dirty(next);
 				}
 
 				WARN_ON(root_owner !=
@@ -2574,6 +2577,9 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
 					clean_tree_block(fs_info, next);
 					btrfs_wait_tree_block_writeback(next);
 					btrfs_tree_unlock(next);
+				} else {
+					if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
+						clear_extent_buffer_dirty(next);
 				}
 
 				WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
@@ -2652,6 +2658,9 @@ static int walk_log_tree(struct btrfs_trans_handle *trans,
 				clean_tree_block(fs_info, next);
 				btrfs_wait_tree_block_writeback(next);
 				btrfs_tree_unlock(next);
+			} else {
+				if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
+					clear_extent_buffer_dirty(next);
 			}
 
 			WARN_ON(log->root_key.objectid !=
Signature · 1 line
2.9.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Liu Bo

In cases that the whole fs flips into readonly status due to failures in critical sections, then log tree's blocks are still dirty, and this leads to a crash during umount time, the crash is about use-after-free,

umount
-> close_ctree

    -> stop workers
    -> iput(btree_inode)
       -> iput_final
          -> write_inode_now
	     -> ...
	       -> queue job on stop'd workers
Quoted 3 lines
cc <[email protected]> v3.12+
Fixes 681ae50917df ("Btrfs: cleanup reserved space when freeing tree log on error")

Thanks,

Josef