From: Qu Wenruo <quwenruo.btrfs@gmx.com>
To: Hao-ran Zheng <zhenghaoran@buaa.edu.cn>,
clm@fb.com, josef@toxicpanda.com, dsterba@suse.com,
linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: baijiaju1990@gmail.com, 21371365@buaa.edu.cn
Subject: Re: [PATCH] btrfs: Fix data race in log_conflicting_inodes
Date: Fri, 1 Nov 2024 15:46:03 +1030 [thread overview]
Message-ID: <4ae5d9ef-a49c-43d2-be71-d178d525edb1@gmx.com> (raw)
In-Reply-To: <20241101035133.925251-1-zhenghaoran@buaa.edu.cn>
在 2024/11/1 14:21, Hao-ran Zheng 写道:
> The Data Race occurs when the `log_conflicting_inodes()` function is
> executed in different threads at the same time. When one thread assigns
> a value to `ctx->logging_conflict_inodes` while another thread performs
> an `if(ctx->logging_conflict_inodes)` judgment or modifies it at the
> same time, a data contention problem may arise.
>
> Further, an atomicity violation may also occur here. Consider the
> following case, when a thread A `if(ctx->logging_conflict_inodes)`
> passes the judgment, the execution switches to another thread B, at
> which time the value of `ctx->logging_conflict_inodes` has not yet
> been assigned true, which would result in multiple threads executing
> `log_conflicting_inodes()`.
>
> To address this issue, it is recommended to add locks to protect
> `logging_conflict_inodes` in the `btrfs_log_ctx` structure, and lock
> protection during assignment and judgment. This modification ensures
> that the value of `ctx->logging_conflict_inodes` does not change during
> the validation process, thereby maintaining its integrity.
>
> Signed-off-by: Hao-ran Zheng <zhenghaoran@buaa.edu.cn>
> ---
> fs/btrfs/tree-log.c | 7 +++++++
> fs/btrfs/tree-log.h | 1 +
> 2 files changed, 8 insertions(+)
>
> diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
> index 9637c7cdc0cf..9cdbf280ca9a 100644
> --- a/fs/btrfs/tree-log.c
> +++ b/fs/btrfs/tree-log.c
> @@ -2854,6 +2854,7 @@ void btrfs_init_log_ctx(struct btrfs_log_ctx *ctx, struct btrfs_inode *inode)
> INIT_LIST_HEAD(&ctx->conflict_inodes);
> ctx->num_conflict_inodes = 0;
> ctx->logging_conflict_inodes = false;
> + spin_lock_init(&ctx->logging_conflict_inodes_lock);
> ctx->scratch_eb = NULL;
> }
>
> @@ -5779,16 +5780,20 @@ static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
> struct btrfs_log_ctx *ctx)
> {
> int ret = 0;
> + unsigned long logging_conflict_inodes_flags;
>
> /*
> * Conflicting inodes are logged by the first call to btrfs_log_inode(),
> * otherwise we could have unbounded recursion of btrfs_log_inode()
> * calls. This check guarantees we can have only 1 level of recursion.
> */
> + spin_lock_irqsave(&ctx->conflict_inodes_lock, logging_conflict_inodes_flags);
> if (ctx->logging_conflict_inodes)
> + spin_unlock_irqrestore(&ctx->conflict_inodes_lock, logging_conflict_inodes_flags);
Not an expert on the log tree, but in the above case, the only thing the
spinlock is protecting is a bool.
This looks overkilled to me.
Yes, several booleans can be stored in to a single byte, which can cause
problems.
But in that case, why not changing those booleans into a unsigned long
and use test_bit()/set_bit()/clear_bit() so that the bit operation will
be atomic and no need for the extra spinlock.
Although I haven't check the other boolean usage, but at least for this
@logging_conflict_inodes variable, it looks like atomic bit operation is
safe.
Thanks,
Qu
> return 0;
>
> ctx->logging_conflict_inodes = true;
> + spin_unlock_irqrestore(&ctx->conflict_inodes_lock, logging_conflict_inodes_flags);
>
> /*
> * New conflicting inodes may be found and added to the list while we
> @@ -5869,7 +5874,9 @@ static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
> break;
> }
>
> + spin_lock_irqsave(&ctx->conflict_inodes_lock, logging_conflict_inodes_flags);
> ctx->logging_conflict_inodes = false;
> + spin_unlock_irqrestore(&ctx->conflict_inodes_lock, logging_conflict_inodes_flags);
> if (ret)
> free_conflicting_inodes(ctx);
>
> diff --git a/fs/btrfs/tree-log.h b/fs/btrfs/tree-log.h
> index dc313e6bb2fa..0f862d0c80f2 100644
> --- a/fs/btrfs/tree-log.h
> +++ b/fs/btrfs/tree-log.h
> @@ -44,6 +44,7 @@ struct btrfs_log_ctx {
> struct list_head conflict_inodes;
> int num_conflict_inodes;
> bool logging_conflict_inodes;
> + spinlock_t logging_conflict_inodes_lock;
> /*
> * Used for fsyncs that need to copy items from the subvolume tree to
> * the log tree (full sync flag set or copy everything flag set) to
next prev parent reply other threads:[~2024-11-01 5:16 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-01 3:51 [PATCH] btrfs: Fix data race in log_conflicting_inodes Hao-ran Zheng
2024-11-01 5:16 ` Qu Wenruo [this message]
2024-11-01 8:44 ` Filipe Manana
2024-11-02 6:26 ` kernel test robot
2024-11-02 6:47 ` kernel test robot
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=4ae5d9ef-a49c-43d2-be71-d178d525edb1@gmx.com \
--to=quwenruo.btrfs@gmx.com \
--cc=21371365@buaa.edu.cn \
--cc=baijiaju1990@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=zhenghaoran@buaa.edu.cn \
/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