btrfs: transaction: Fix misplaced barrier in btrfs_record_root_in_trans
PATCH 1/1 btrfs: transaction: Fix misplaced barrier in btrfs_record_root_in_trans
Per comment, record_root_in_trans orders the writes of the root->state and root->last_trans:
set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
smp_wmb();
root->last_trans = trans->transid;But the barrier that enforces the order on the read side is misplaced:
smp_rmb(); <-- misplaced
if (root->last_trans == trans->transid &&
<-- missing barrier here -->
!test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))This patches fixes the ordering and wraps the racy accesses with READ_ONCE and WRITE_ONCE calls to avoid load/store tearing.
--- fs/btrfs/transaction.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
fs/btrfs/transaction.c +4 -3
@@ -437,7 +437,7 @@ static int record_root_in_trans(struct btrfs_trans_handle *trans, (unsigned long )root->root_key.objectid, BTRFS_ROOT_TRANS_TAG);spin_unlock (&fs_info->fs_roots_radix_lock); - root->last_trans = trans->transid; +WRITE_ONCE (root->last_trans, trans->transid);/* this is pretty tricky. We don't want to * take the relocation lock in btrfs_record_root_in_trans
@@ -489,7 +489,7 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, struct btrfs_root *root) {struct btrfs_fs_info *fs_info = root->fs_info; -int ret; +int ret, last_trans;if (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state))return 0 ;
@@ -498,8 +498,9 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,* see record_root_in_trans for comments about IN_TRANS_SETUP usage * and barriers */ + last_trans =READ_ONCE (root->last_trans);smp_rmb (); -if (root->last_trans == trans->transid && +if (last_trans == trans->transid && !test_bit (BTRFS_ROOT_IN_TRANS_SETUP , &root->state))return 0 ;
Signature · 1 line
2.17.1
Quoted 44 lines · Baptiste Lepers
Per comment,
record_root_in_transorders the writes of theroot->stateandroot->last_trans:set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); smp_wmb(); root->last_trans = trans->transid;But the barrier that enforces the order on the read side is misplaced:
smp_rmb(); <-- misplaced if (root->last_trans == trans->transid && <-- missing barrier here --> !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))This patches fixes the ordering and wraps the racy accesses with
READ_ONCEandWRITE_ONCEcalls to avoid load/store tearing.Fixes 7585717f304f5 ("Btrfs: fix relocation races")Signed-off-by Baptiste Lepers <[email protected]>--- fs/btrfs/transaction.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)fs/btrfs/transaction.c +2
-2@@ -437,7 +437,7 @@ static int record_root_in_trans(struct btrfs_trans_handle *trans, (unsigned long )root->root_key.objectid, BTRFS_ROOT_TRANS_TAG);spin_unlock (&fs_info->fs_roots_radix_lock); - root->last_trans = trans->transid; +WRITE_ONCE (root->last_trans, trans->transid);/* this is pretty tricky. We don't want to * take the relocation lock in btrfs_record_root_in_trans @@ -489,7 +489,7 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, struct btrfs_root *root) {struct btrfs_fs_info *fs_info = root->fs_info; -int ret; +int ret, last_trans;if (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state))return 0 ;
fs/btrfs/transaction.c int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, · line 504
Quoted 8 lines
fs/btrfs/transaction.c +2
-1@@ -498,8 +498,9 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,* see record_root_in_trans for comments about IN_TRANS_SETUP usage * and barriers */ + last_trans =READ_ONCE (root->last_trans);smp_rmb (); -if (root->last_trans == trans->transid && +if (last_trans == trans->transid && !test_bit (BTRFS_ROOT_IN_TRANS_SETUP , &root->state))
Aren't the smp_rmb barriers supposed to be used before the condition?
A message that is not in the archive (sent off-list or not yet received): CABdVr8Rfd3jXvaa_GYzSqpqUs3Fy7AVHou5z8vHXBhn-YenZfg@mail.gmail.com
No, they need to be between the reads to have an effect. See
https://www.kernel.org/doc/Documentation/memory-barriers.txt §SMP BARRIER PAIRING ("When dealing with CPU-CPU interactions..."). You will see that the barriers are always between the ordered reads and not before.
I think that Paul, the barrier guru, can confirm that the barrier was misplaced in the original code? :)
Quoted 65 lines · Baptiste Lepers
Quoted 43 lines · David Sterba
Quoted 41 lines · Baptiste Lepers
Per comment,
record_root_in_transorders the writes of theroot->stateandroot->last_trans:set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); smp_wmb(); root->last_trans = trans->transid;But the barrier that enforces the order on the read side is misplaced:
smp_rmb(); <-- misplaced if (root->last_trans == trans->transid && <-- missing barrier here --> !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))This patches fixes the ordering and wraps the racy accesses with
READ_ONCEandWRITE_ONCEcalls to avoid load/store tearing.Fixes 7585717f304f5 ("Btrfs: fix relocation races")Signed-off-by Baptiste Lepers <[email protected]>--- fs/btrfs/transaction.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)fs/btrfs/transaction.c +2
-2@@ -437,7 +437,7 @@ static int record_root_in_trans(struct btrfs_trans_handle *trans, (unsigned long )root->root_key.objectid, BTRFS_ROOT_TRANS_TAG);spin_unlock (&fs_info->fs_roots_radix_lock); - root->last_trans = trans->transid; +WRITE_ONCE (root->last_trans, trans->transid);/* this is pretty tricky. We don't want to * take the relocation lock in btrfs_record_root_in_trans @@ -489,7 +489,7 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, struct btrfs_root *root) {struct btrfs_fs_info *fs_info = root->fs_info; -int ret; +int ret, last_trans;Quoted 13 lines
fs/btrfs/transaction.c int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, · line 504
Quoted 11 lines
fs/btrfs/transaction.c +2
-1if (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state))return 0 ;@@ -498,8 +498,9 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,* see record_root_in_trans for comments about IN_TRANS_SETUP usage * and barriers */ + last_trans =READ_ONCE (root->last_trans);smp_rmb (); -if (root->last_trans == trans->transid && +if (last_trans == trans->transid && !test_bit (BTRFS_ROOT_IN_TRANS_SETUP , &root->state))Aren't the
smp_rmbbarriers supposed to be used before the condition?No, they need to be between the reads to have an effect. See https://www.kernel.org/doc/Documentation/memory-barriers.txt §SMP BARRIER PAIRING ("When dealing with CPU-CPU interactions..."). You will see that the barriers are always between the ordered reads and not before.
I think that Paul, the barrier guru, can confirm that the barrier was misplaced in the original code? :)
Just curious about the status of this patch. :) Let me know if you need further information.
Thanks!
Quoted 78 lines · Baptiste Lepers
No, they need to be between the reads to have an effect. See
https://www.kernel.org/doc/Documentation/memory-barriers.txt §SMP BARRIER PAIRING ("When dealing with CPU-CPU interactions..."). You will see that the barriers are always between the ordered reads and not before.I think that Paul, the barrier guru, can confirm that the barrier was misplaced in the original code? :)
Quoted 65 lines · Baptiste Lepers
Quoted 43 lines · David Sterba
Quoted 41 lines · Baptiste Lepers
Per comment,
record_root_in_transorders the writes of theroot->stateandroot->last_trans:set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); smp_wmb(); root->last_trans = trans->transid;But the barrier that enforces the order on the read side is misplaced:
smp_rmb(); <-- misplaced if (root->last_trans == trans->transid && <-- missing barrier here --> !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))This patches fixes the ordering and wraps the racy accesses with
READ_ONCEandWRITE_ONCEcalls to avoid load/store tearing.Fixes 7585717f304f5 ("Btrfs: fix relocation races")Signed-off-by Baptiste Lepers <[email protected]>--- fs/btrfs/transaction.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)fs/btrfs/transaction.c +2
-2@@ -437,7 +437,7 @@ static int record_root_in_trans(struct btrfs_trans_handle *trans, (unsigned long )root->root_key.objectid, BTRFS_ROOT_TRANS_TAG);spin_unlock (&fs_info->fs_roots_radix_lock); - root->last_trans = trans->transid; +WRITE_ONCE (root->last_trans, trans->transid);/* this is pretty tricky. We don't want to * take the relocation lock in btrfs_record_root_in_trans @@ -489,7 +489,7 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, struct btrfs_root *root) {struct btrfs_fs_info *fs_info = root->fs_info; -int ret; +int ret, last_trans;Quoted 13 lines
fs/btrfs/transaction.c int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, · line 504
Quoted 11 lines
fs/btrfs/transaction.c +2
-1if (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state))return 0 ;@@ -498,8 +498,9 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,* see record_root_in_trans for comments about IN_TRANS_SETUP usage * and barriers */ + last_trans =READ_ONCE (root->last_trans);smp_rmb (); -if (root->last_trans == trans->transid && +if (last_trans == trans->transid && !test_bit (BTRFS_ROOT_IN_TRANS_SETUP , &root->state))Aren't the
smp_rmbbarriers supposed to be used before the condition?No, they need to be between the reads to have an effect. See https://www.kernel.org/doc/Documentation/memory-barriers.txt §SMP BARRIER PAIRING ("When dealing with CPU-CPU interactions..."). You will see that the barriers are always between the ordered reads and not before.
I think that Paul, the barrier guru, can confirm that the barrier was misplaced in the original code? :)
Quoted 36 lines · Baptiste Lepers
Per comment,
record_root_in_transorders the writes of theroot->stateandroot->last_trans:set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); smp_wmb(); root->last_trans = trans->transid;But the barrier that enforces the order on the read side is misplaced:
smp_rmb(); <-- misplaced if (root->last_trans == trans->transid && <-- missing barrier here --> !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))This patches fixes the ordering and wraps the racy accesses with
READ_ONCEandWRITE_ONCEcalls to avoid load/store tearing.Fixes 7585717f304f5 ("Btrfs: fix relocation races")Signed-off-by Baptiste Lepers <[email protected]>--- fs/btrfs/transaction.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)fs/btrfs/transaction.c +1
-1@@ -437,7 +437,7 @@ static int record_root_in_trans(struct btrfs_trans_handle *trans, (unsigned long )root->root_key.objectid, BTRFS_ROOT_TRANS_TAG);spin_unlock (&fs_info->fs_roots_radix_lock); - root->last_trans = trans->transid; +WRITE_ONCE (root->last_trans, trans->transid);/* this is pretty tricky. We don't want to * take the relocation lock in btrfs_record_root_in_trans
fs/btrfs/transaction.c int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, · line 492
Quoted 5 lines
fs/btrfs/transaction.c +1
-1@@ -489,7 +489,7 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, struct btrfs_root *root) {struct btrfs_fs_info *fs_info = root->fs_info; -int ret; +int ret, last_trans;
last_trans should be u64, as root->last_trans is a u64.
Other than that it looks good to me.
Thanks.
Quoted 17 lines
fs/btrfs/transaction.c +2
-2if (!test_bit (BTRFS_ROOT_SHAREABLE , &root->state))return 0 ;@@ -498,8 +498,9 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,* see record_root_in_trans for comments about IN_TRANS_SETUP usage * and barriers */ + last_trans =READ_ONCE (root->last_trans);smp_rmb (); -if (root->last_trans == trans->transid && +if (last_trans == trans->transid && !test_bit (BTRFS_ROOT_IN_TRANS_SETUP , &root->state))return 0 ; --2.17.1
Signature · 3 lines
Filipe David Manana, “Whether you think you can, or you think you can't — you're right.”