From: David Sterba <dsterba@suse.cz>
To: Cen Zhang <zzzccc427@gmail.com>
Cc: clm@fb.com, dsterba@suse.com, linux-btrfs@vger.kernel.org,
linux-kernel@vger.kernel.org, baijiaju1990@gmail.com
Subject: Re: [PATCH] btrfs: annotate lockless read of defrag_bytes in should_nocow()
Date: Tue, 21 Apr 2026 05:33:18 +0200 [thread overview]
Message-ID: <20260421033318.GE12792@twin.jikos.cz> (raw)
In-Reply-To: <20260401022153.743576-1-zzzccc427@gmail.com>
On Wed, Apr 01, 2026 at 10:21:53AM +0800, Cen Zhang wrote:
> should_nocow() reads inode->defrag_bytes without holding inode->lock,
> while btrfs_set_delalloc_extent() and btrfs_clear_delalloc_extent()
> update it under that spinlock.
>
> This is a data race. The read is a quick check used to decide whether
> to fall back to COW for a NOCOW inode: if defrag_bytes is non-zero and
> the range is tagged EXTENT_DEFRAG, we force COW so that defragmentation
> can rewrite the extent. Reading a stale value is harmless because:
>
> - A missed increment may skip COW once, but the defrag pass will
> redo the extent later.
> - A stale non-zero may force an unnecessary COW, which is a minor
> efficiency loss, not a correctness issue.
>
> On 64-bit platforms an aligned u64 load is naturally atomic so tearing
> cannot happen. On 32-bit platforms u64 may tear, but we only test for
> zero vs non-zero, so the heuristic stays correct regardless.
>
> Add READ_ONCE() to prevent the compiler from caching or splitting the
> load and to document the intentional lock-free pattern.
As it's explained it's ok to do the unlocked read but of that that the
dara_race() is more convenient, we do not really need the READ_ONCE
semantics.
next prev parent reply other threads:[~2026-04-21 3:33 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-01 2:21 Cen Zhang
2026-04-21 3:33 ` David Sterba [this message]
2026-05-25 13:13 ` David Sterba
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=20260421033318.GE12792@twin.jikos.cz \
--to=dsterba@suse.cz \
--cc=baijiaju1990@gmail.com \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=zzzccc427@gmail.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
all inboxes | Powered by JetHome®