linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Baptiste Lepers <baptiste.lepers@gmail.com>
To: unlisted-recipients:; (no To-header on input)
Cc: "Paul E . McKenney" <paulmck@linux.ibm.com>,
	Baptiste Lepers <baptiste.lepers@gmail.com>,
	Chris Mason <clm@fb.com>, Josef Bacik <josef@toxicpanda.com>,
	David Sterba <dsterba@suse.com>,
	linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] btrfs: transaction: Fix misplaced barrier in btrfs_record_root_in_trans
Date: Mon,  6 Sep 2021 11:25:59 +1000	[thread overview]
Message-ID: <20210906012559.8605-1-baptiste.lepers@gmail.com> (raw)

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


             reply	other threads:[~2021-09-06  1:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-06  1:25 Baptiste Lepers [this message]
2021-09-06 12:27 ` [PATCH] btrfs: transaction: Fix misplaced barrier in btrfs_record_root_in_trans 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210906012559.8605-1-baptiste.lepers@gmail.com \
    --to=baptiste.lepers@gmail.com \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paulmck@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).