* [PATCH] btrfs: transaction: Fix misplaced barrier in btrfs_record_root_in_trans @ 2021-09-06 1:25 Baptiste Lepers 2021-09-06 12:27 ` David Sterba 2021-09-16 9:30 ` Filipe Manana 0 siblings, 2 replies; 5+ messages in thread From: Baptiste Lepers @ 2021-09-06 1:25 UTC (permalink / raw) Cc: Paul E . McKenney, Baptiste Lepers, Chris Mason, Josef Bacik, David Sterba, linux-btrfs, linux-kernel 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. Fixes: 7585717f304f5 ("Btrfs: fix relocation races") Signed-off-by: Baptiste Lepers <baptiste.lepers@gmail.com> --- fs/btrfs/transaction.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c index 14b9fdc8aaa9..a609222e6704 100644 --- a/fs/btrfs/transaction.c +++ b/fs/btrfs/transaction.c @@ -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; -- 2.17.1 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] btrfs: transaction: Fix misplaced barrier in btrfs_record_root_in_trans 2021-09-06 1:25 [PATCH] btrfs: transaction: Fix misplaced barrier in btrfs_record_root_in_trans Baptiste Lepers @ 2021-09-06 12:27 ` David Sterba [not found] ` <CABdVr8Rfd3jXvaa_GYzSqpqUs3Fy7AVHou5z8vHXBhn-YenZfg@mail.gmail.com> 2021-09-16 9:30 ` Filipe Manana 1 sibling, 1 reply; 5+ messages in thread From: David Sterba @ 2021-09-06 12:27 UTC (permalink / raw) To: Baptiste Lepers Cc: Paul E . McKenney, Chris Mason, Josef Bacik, David Sterba, linux-btrfs, linux-kernel On Mon, Sep 06, 2021 at 11:25:59AM +1000, Baptiste Lepers wrote: > 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. > > Fixes: 7585717f304f5 ("Btrfs: fix relocation races") > Signed-off-by: Baptiste Lepers <baptiste.lepers@gmail.com> > --- > fs/btrfs/transaction.c | 7 ++++--- > 1 file changed, 4 insertions(+), 3 deletions(-) > > diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c > index 14b9fdc8aaa9..a609222e6704 100644 > --- a/fs/btrfs/transaction.c > +++ b/fs/btrfs/transaction.c > @@ -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)) Aren't the smp_rmb barriers supposed to be used before the condition? ^ permalink raw reply [flat|nested] 5+ messages in thread
[parent not found: <CABdVr8Rfd3jXvaa_GYzSqpqUs3Fy7AVHou5z8vHXBhn-YenZfg@mail.gmail.com>]
* Re: [PATCH] btrfs: transaction: Fix misplaced barrier in btrfs_record_root_in_trans [not found] ` <CABdVr8Rfd3jXvaa_GYzSqpqUs3Fy7AVHou5z8vHXBhn-YenZfg@mail.gmail.com> @ 2021-09-07 0:44 ` Baptiste Lepers 2021-09-16 3:45 ` Baptiste Lepers 0 siblings, 1 reply; 5+ messages in thread From: Baptiste Lepers @ 2021-09-07 0:44 UTC (permalink / raw) To: dsterba, Baptiste Lepers, Paul E . McKenney, Chris Mason, Josef Bacik, David Sterba, linux-btrfs, linux-kernel 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? :) On Tue, Sep 7, 2021 at 10:43 AM Baptiste Lepers <baptiste.lepers@gmail.com> wrote: > > > > On Mon, Sep 6, 2021 at 10:27 PM David Sterba <dsterba@suse.cz> wrote: >> >> On Mon, Sep 06, 2021 at 11:25:59AM +1000, Baptiste Lepers wrote: >> > 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. >> > >> > Fixes: 7585717f304f5 ("Btrfs: fix relocation races") >> > Signed-off-by: Baptiste Lepers <baptiste.lepers@gmail.com> >> > --- >> > fs/btrfs/transaction.c | 7 ++++--- >> > 1 file changed, 4 insertions(+), 3 deletions(-) >> > >> > diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c >> > index 14b9fdc8aaa9..a609222e6704 100644 >> > --- a/fs/btrfs/transaction.c >> > +++ b/fs/btrfs/transaction.c >> > @@ -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)) >> >> Aren't the smp_rmb barriers 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? :) ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] btrfs: transaction: Fix misplaced barrier in btrfs_record_root_in_trans 2021-09-07 0:44 ` Baptiste Lepers @ 2021-09-16 3:45 ` Baptiste Lepers 0 siblings, 0 replies; 5+ messages in thread From: Baptiste Lepers @ 2021-09-16 3:45 UTC (permalink / raw) To: dsterba, Baptiste Lepers, Paul E . McKenney, Chris Mason, Josef Bacik, David Sterba, linux-btrfs, linux-kernel Just curious about the status of this patch. :) Let me know if you need further information. Thanks! On Tue, Sep 7, 2021 at 10:44 AM Baptiste Lepers <baptiste.lepers@gmail.com> wrote: > > 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? :) > > > On Tue, Sep 7, 2021 at 10:43 AM Baptiste Lepers > <baptiste.lepers@gmail.com> wrote: > > > > > > > > On Mon, Sep 6, 2021 at 10:27 PM David Sterba <dsterba@suse.cz> wrote: > >> > >> On Mon, Sep 06, 2021 at 11:25:59AM +1000, Baptiste Lepers wrote: > >> > 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. > >> > > >> > Fixes: 7585717f304f5 ("Btrfs: fix relocation races") > >> > Signed-off-by: Baptiste Lepers <baptiste.lepers@gmail.com> > >> > --- > >> > fs/btrfs/transaction.c | 7 ++++--- > >> > 1 file changed, 4 insertions(+), 3 deletions(-) > >> > > >> > diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c > >> > index 14b9fdc8aaa9..a609222e6704 100644 > >> > --- a/fs/btrfs/transaction.c > >> > +++ b/fs/btrfs/transaction.c > >> > @@ -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)) > >> > >> Aren't the smp_rmb barriers 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? :) ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] btrfs: transaction: Fix misplaced barrier in btrfs_record_root_in_trans 2021-09-06 1:25 [PATCH] btrfs: transaction: Fix misplaced barrier in btrfs_record_root_in_trans Baptiste Lepers 2021-09-06 12:27 ` David Sterba @ 2021-09-16 9:30 ` Filipe Manana 1 sibling, 0 replies; 5+ messages in thread From: Filipe Manana @ 2021-09-16 9:30 UTC (permalink / raw) To: Baptiste Lepers Cc: Paul E . McKenney, Chris Mason, Josef Bacik, David Sterba, linux-btrfs, Linux Kernel Mailing List On Mon, Sep 6, 2021 at 2:38 AM Baptiste Lepers <baptiste.lepers@gmail.com> wrote: > > 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. > > Fixes: 7585717f304f5 ("Btrfs: fix relocation races") > Signed-off-by: Baptiste Lepers <baptiste.lepers@gmail.com> > --- > fs/btrfs/transaction.c | 7 ++++--- > 1 file changed, 4 insertions(+), 3 deletions(-) > > diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c > index 14b9fdc8aaa9..a609222e6704 100644 > --- a/fs/btrfs/transaction.c > +++ b/fs/btrfs/transaction.c > @@ -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; last_trans should be u64, as root->last_trans is a u64. Other than that it looks good to me. Thanks. > > 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; > > -- > 2.17.1 > -- Filipe David Manana, “Whether you think you can, or you think you can't — you're right.” ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2021-09-16 9:31 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2021-09-06 1:25 [PATCH] btrfs: transaction: Fix misplaced barrier in btrfs_record_root_in_trans Baptiste Lepers 2021-09-06 12:27 ` David Sterba [not found] ` <CABdVr8Rfd3jXvaa_GYzSqpqUs3Fy7AVHou5z8vHXBhn-YenZfg@mail.gmail.com> 2021-09-07 0:44 ` Baptiste Lepers 2021-09-16 3:45 ` Baptiste Lepers 2021-09-16 9:30 ` Filipe Manana
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).