]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
btrfs: add and use helpers for reading and writing last_trans_committed
authorFilipe Manana <fdmanana@suse.com>
Wed, 4 Oct 2023 10:38:51 +0000 (11:38 +0100)
committerDavid Sterba <dsterba@suse.com>
Thu, 12 Oct 2023 14:44:17 +0000 (16:44 +0200)
Currently the last_trans_committed field of struct btrfs_fs_info is
modified and read without any locking or other protection. For example
early in the fsync path, skip_inode_logging() is called which reads
fs_info->last_trans_committed, but at the same time we can have a
transaction commit completing and updating that field.

In the case of an fsync this is harmless and any data race should be
rare and at most cause an unnecessary logging of an inode.

To avoid data race warnings from tools like KCSAN and other issues such
as load and store tearing (amongst others, see [1]), create helpers to
access the last_trans_committed field of struct btrfs_fs_info using
READ_ONCE() and WRITE_ONCE(), and use these helpers everywhere.

[1] https://lwn.net/Articles/793253/

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/disk-io.c
fs/btrfs/file.c
fs/btrfs/fs.h
fs/btrfs/ioctl.c
fs/btrfs/scrub.c
fs/btrfs/super.c
fs/btrfs/transaction.c
fs/btrfs/tree-checker.c

index c84d32951b2670bb8465abcefc1d3e32b41886bf..401ea09ae4b8ce284c1b07a1d24347159f9821dc 100644 (file)
@@ -244,6 +244,7 @@ blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio)
        struct extent_buffer *eb = bbio->private;
        struct btrfs_fs_info *fs_info = eb->fs_info;
        u64 found_start = btrfs_header_bytenr(eb);
+       u64 last_trans;
        u8 result[BTRFS_CSUM_SIZE];
        int ret;
 
@@ -281,12 +282,12 @@ blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio)
         * Also check the generation, the eb reached here must be newer than
         * last committed. Or something seriously wrong happened.
         */
-       if (unlikely(btrfs_header_generation(eb) <= fs_info->last_trans_committed)) {
+       last_trans = btrfs_get_last_trans_committed(fs_info);
+       if (unlikely(btrfs_header_generation(eb) <= last_trans)) {
                ret = -EUCLEAN;
                btrfs_err(fs_info,
                        "block=%llu bad generation, have %llu expect > %llu",
-                         eb->start, btrfs_header_generation(eb),
-                         fs_info->last_trans_committed);
+                         eb->start, btrfs_header_generation(eb), last_trans);
                goto error;
        }
        write_extent_buffer(eb, result, 0, fs_info->csum_size);
@@ -2653,7 +2654,7 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
 
                /* All successful */
                fs_info->generation = btrfs_header_generation(tree_root->node);
-               fs_info->last_trans_committed = fs_info->generation;
+               btrfs_set_last_trans_committed(fs_info, fs_info->generation);
                fs_info->last_reloc_trans = 0;
 
                /* Always begin writing backup roots after the one being used */
index 723f0c70452e57d346aa0d8c6ad240edf427b321..92e6f224bff979d3ed78b367dd51aa415f5eb144 100644 (file)
@@ -1760,7 +1760,7 @@ static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx)
         * and for a fast fsync we don't wait for that, we only wait for the
         * writeback to complete.
         */
-       if (inode->last_trans <= fs_info->last_trans_committed &&
+       if (inode->last_trans <= btrfs_get_last_trans_committed(fs_info) &&
            (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) ||
             list_empty(&ctx->ordered_extents)))
                return true;
index d04b729cbdf3353970e066b5b02a8fd118b9a82c..318df6f9d9cb6922b2dc342448a69ad1583c4cc7 100644 (file)
@@ -423,6 +423,10 @@ struct btrfs_fs_info {
         * Should always be updated using btrfs_set_fs_generation().
         */
        u64 generation;
+       /*
+        * Always use btrfs_get_last_trans_committed() and
+        * btrfs_set_last_trans_committed() to read and update this field.
+        */
        u64 last_trans_committed;
        /*
         * Generation of the last transaction used for block group relocation
@@ -833,6 +837,16 @@ static inline void btrfs_set_fs_generation(struct btrfs_fs_info *fs_info, u64 ge
        WRITE_ONCE(fs_info->generation, gen);
 }
 
+static inline u64 btrfs_get_last_trans_committed(const struct btrfs_fs_info *fs_info)
+{
+       return READ_ONCE(fs_info->last_trans_committed);
+}
+
+static inline void btrfs_set_last_trans_committed(struct btrfs_fs_info *fs_info, u64 gen)
+{
+       WRITE_ONCE(fs_info->last_trans_committed, gen);
+}
+
 static inline void btrfs_set_last_root_drop_gen(struct btrfs_fs_info *fs_info,
                                                u64 gen)
 {
index 7ab21283fae80a342bd99504f0a2d41f249811e1..ab4ddbebeb16a2c9dfca2432903a0f58af87b4a1 100644 (file)
@@ -3131,7 +3131,7 @@ static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
                        return PTR_ERR(trans);
 
                /* No running transaction, don't bother */
-               transid = root->fs_info->last_trans_committed;
+               transid = btrfs_get_last_trans_committed(root->fs_info);
                goto out;
        }
        transid = trans->transid;
index 2ac574bde350c9a26c9babefc3ca5f9b264182d4..9ce5be21b0360107d1a2416f06fa0cbfd1710030 100644 (file)
@@ -2787,7 +2787,7 @@ static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
        if (scrub_dev->fs_devices != fs_info->fs_devices)
                gen = scrub_dev->generation;
        else
-               gen = fs_info->last_trans_committed;
+               gen = btrfs_get_last_trans_committed(fs_info);
 
        for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
                bytenr = btrfs_sb_offset(i);
index 55d308cd5de4f1702b9bd7b8f2cbb41ab0efe49e..6ecf78d09694342f41857f83bbfd7177efeac616 100644 (file)
@@ -2217,6 +2217,7 @@ static int check_dev_super(struct btrfs_device *dev)
 {
        struct btrfs_fs_info *fs_info = dev->fs_info;
        struct btrfs_super_block *sb;
+       u64 last_trans;
        u16 csum_type;
        int ret = 0;
 
@@ -2252,10 +2253,10 @@ static int check_dev_super(struct btrfs_device *dev)
        if (ret < 0)
                goto out;
 
-       if (btrfs_super_generation(sb) != fs_info->last_trans_committed) {
+       last_trans = btrfs_get_last_trans_committed(fs_info);
+       if (btrfs_super_generation(sb) != last_trans) {
                btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
-                       btrfs_super_generation(sb),
-                       fs_info->last_trans_committed);
+                         btrfs_super_generation(sb), last_trans);
                ret = -EUCLEAN;
                goto out;
        }
index f5db3a483f40544500a0f2a9d54f4e6096cefff6..9694a3ca17390914a9e7103356541a9f360aa0c1 100644 (file)
@@ -980,7 +980,7 @@ int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
        int ret = 0;
 
        if (transid) {
-               if (transid <= fs_info->last_trans_committed)
+               if (transid <= btrfs_get_last_trans_committed(fs_info))
                        goto out;
 
                /* find specified transaction */
@@ -1004,7 +1004,7 @@ int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
                 * raced with btrfs_commit_transaction
                 */
                if (!cur_trans) {
-                       if (transid > fs_info->last_trans_committed)
+                       if (transid > btrfs_get_last_trans_committed(fs_info))
                                ret = -EINVAL;
                        goto out;
                }
@@ -2587,7 +2587,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
        if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
                btrfs_clear_space_info_full(fs_info);
 
-       fs_info->last_trans_committed = cur_trans->transid;
+       btrfs_set_last_trans_committed(fs_info, cur_trans->transid);
        /*
         * We needn't acquire the lock here because there is no other task
         * which can change it.
index 1f2c389b0bfa37c8dcdc4c08e1e6b056d9e81510..a416cbea75d1ff4f009284dfbfc42650da493e45 100644 (file)
@@ -2051,7 +2051,7 @@ int btrfs_verify_level_key(struct extent_buffer *eb, int level,
         * So we only checks tree blocks which is read from disk, whose
         * generation <= fs_info->last_trans_committed.
         */
-       if (btrfs_header_generation(eb) > fs_info->last_trans_committed)
+       if (btrfs_header_generation(eb) > btrfs_get_last_trans_committed(fs_info))
                return 0;
 
        /* We have @first_key, so this @eb must have at least one item */