btrfs: avoid load/store tearing races when checking if an inode was logged
authorFilipe Manana <fdmanana@suse.com>
Wed, 6 Aug 2025 11:11:32 +0000 (12:11 +0100)
committerDavid Sterba <dsterba@suse.com>
Thu, 21 Aug 2025 22:58:55 +0000 (00:58 +0200)
commit986bf6ed44dff7fbae7b43a0882757ee7f5ba21b
tree8d8c929e81980ea58276a6deefd120ad442a8b5f
parent59a0dd4ab98970086fd096281b1606c506ff2698
btrfs: avoid load/store tearing races when checking if an inode was logged

At inode_logged() we do a couple lockless checks for ->logged_trans, and
these are generally safe except the second one in case we get a load or
store tearing due to a concurrent call updating ->logged_trans (either at
btrfs_log_inode() or later at inode_logged()).

In the first case it's safe to compare to the current transaction ID since
once ->logged_trans is set the current transaction, we never set it to a
lower value.

In the second case, where we check if it's greater than zero, we are prone
to load/store tearing races, since we can have a concurrent task updating
to the current transaction ID with store tearing for example, instead of
updating with a single 64 bits write, to update with two 32 bits writes or
four 16 bits writes. In that case the reading side at inode_logged() could
see a positive value that does not match the current transaction and then
return a false negative.

Fix this by doing the second check while holding the inode's spinlock, add
some comments about it too. Also add the data_race() annotation to the
first check to avoid any reports from KCSAN (or similar tools) and comment
about it.

Fixes: 0f8ce49821de ("btrfs: avoid inode logging during rename and link when possible")
Reviewed-by: Boris Burkov <boris@bur.io>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/tree-log.c