From: Jeff Layton <jlayton@kernel.org>
To: Yu Peng <pengyu@kylinos.cn>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
syzbot+8b3bd9f8a06658479d4a@syzkaller.appspotmail.com
Subject: Re: [PATCH] fs: annotate inode timestamp accessors
Date: Wed, 08 Jul 2026 07:15:54 -0400 [thread overview]
Message-ID: <67fb179754d4d69a9342ca86ce89d659c7704895.camel@kernel.org> (raw)
In-Reply-To: <20260708080232.2564807-1-pengyu@kylinos.cn>
On Wed, 2026-07-08 at 16:02 +0800, Yu Peng wrote:
> syzbot reported a KCSAN race between fill_mg_cmtime() and
> inode_set_ctime_to_ts() on inode->i_ctime_{sec,nsec}.
>
> stat/getattr can sample inode timestamps while update paths store new
> values concurrently, so KCSAN can report benign races on these fields.
>
> Annotate the timestamp accessors with READ_ONCE()/WRITE_ONCE(), and use
> the ctime accessor for the remaining ctime loads. This avoids the KCSAN
> reports without changing timestamp semantics.
>
> Fixes: 4e40eff0b573 ("fs: add infrastructure for multigrain timestamps")
> Reported-by: syzbot+8b3bd9f8a06658479d4a@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=8b3bd9f8a06658479d4a
> Signed-off-by: Yu Peng <pengyu@kylinos.cn>
> ---
> fs/inode.c | 18 +++++++++---------
> fs/stat.c | 2 +-
> include/linux/fs.h | 20 ++++++++++----------
> 3 files changed, 20 insertions(+), 20 deletions(-)
>
> diff --git a/fs/inode.c b/fs/inode.c
> index 31c5b9ee3a81..95e981b4e19c 100644
> --- a/fs/inode.c
> +++ b/fs/inode.c
> @@ -2833,8 +2833,8 @@ struct timespec64 inode_set_ctime_to_ts(struct inode *inode, struct timespec64 t
> {
> trace_inode_set_ctime_to_ts(inode, &ts);
> set_normalized_timespec64(&ts, ts.tv_sec, ts.tv_nsec);
> - inode->i_ctime_sec = ts.tv_sec;
> - inode->i_ctime_nsec = ts.tv_nsec;
> + WRITE_ONCE(inode->i_ctime_sec, ts.tv_sec);
> + WRITE_ONCE(inode->i_ctime_nsec, ts.tv_nsec);
> return ts;
> }
> EXPORT_SYMBOL(inode_set_ctime_to_ts);
> @@ -2908,7 +2908,7 @@ struct timespec64 inode_set_ctime_current(struct inode *inode)
> */
> cns = smp_load_acquire(&inode->i_ctime_nsec);
> if (cns & I_CTIME_QUERIED) {
> - struct timespec64 ctime = { .tv_sec = inode->i_ctime_sec,
> + struct timespec64 ctime = { .tv_sec = inode_get_ctime_sec(inode),
> .tv_nsec = cns & ~I_CTIME_QUERIED };
>
> if (timespec64_compare(&now, &ctime) <= 0) {
> @@ -2920,7 +2920,7 @@ struct timespec64 inode_set_ctime_current(struct inode *inode)
> mgtime_counter_inc(mg_ctime_updates);
>
> /* No need to cmpxchg if it's exactly the same */
> - if (cns == now.tv_nsec && inode->i_ctime_sec == now.tv_sec) {
> + if (cns == now.tv_nsec && inode_get_ctime_sec(inode) == now.tv_sec) {
> trace_ctime_xchg_skip(inode, &now);
> goto out;
> }
> @@ -2929,7 +2929,7 @@ struct timespec64 inode_set_ctime_current(struct inode *inode)
> /* Try to swap the nsec value into place. */
> if (try_cmpxchg(&inode->i_ctime_nsec, &cur, now.tv_nsec)) {
> /* If swap occurred, then we're (mostly) done */
> - inode->i_ctime_sec = now.tv_sec;
> + WRITE_ONCE(inode->i_ctime_sec, now.tv_sec);
> trace_ctime_ns_xchg(inode, cns, now.tv_nsec, cur);
> mgtime_counter_inc(mg_ctime_swaps);
> } else {
> @@ -2944,7 +2944,7 @@ struct timespec64 inode_set_ctime_current(struct inode *inode)
> goto retry;
> }
> /* Otherwise, keep the existing ctime */
> - now.tv_sec = inode->i_ctime_sec;
> + now.tv_sec = inode_get_ctime_sec(inode);
> now.tv_nsec = cur & ~I_CTIME_QUERIED;
> }
> out:
> @@ -2977,7 +2977,7 @@ struct timespec64 inode_set_ctime_deleg(struct inode *inode, struct timespec64 u
> /* pairs with try_cmpxchg below */
> cur = smp_load_acquire(&inode->i_ctime_nsec);
> cur_ts.tv_nsec = cur & ~I_CTIME_QUERIED;
> - cur_ts.tv_sec = inode->i_ctime_sec;
> + cur_ts.tv_sec = inode_get_ctime_sec(inode);
>
> /* If the update is older than the existing value, skip it. */
> if (timespec64_compare(&update, &cur_ts) <= 0)
> @@ -3003,7 +3003,7 @@ struct timespec64 inode_set_ctime_deleg(struct inode *inode, struct timespec64 u
> retry:
> old = cur;
> if (try_cmpxchg(&inode->i_ctime_nsec, &cur, update.tv_nsec)) {
> - inode->i_ctime_sec = update.tv_sec;
> + WRITE_ONCE(inode->i_ctime_sec, update.tv_sec);
> mgtime_counter_inc(mg_ctime_swaps);
> return update;
> }
> @@ -3019,7 +3019,7 @@ struct timespec64 inode_set_ctime_deleg(struct inode *inode, struct timespec64 u
> goto retry;
>
> /* Otherwise, it was a new timestamp. */
> - cur_ts.tv_sec = inode->i_ctime_sec;
> + cur_ts.tv_sec = inode_get_ctime_sec(inode);
> cur_ts.tv_nsec = cur & ~I_CTIME_QUERIED;
> return cur_ts;
> }
> diff --git a/fs/stat.c b/fs/stat.c
> index 89909746bed1..c461c3054234 100644
> --- a/fs/stat.c
> +++ b/fs/stat.c
> @@ -53,7 +53,7 @@ void fill_mg_cmtime(struct kstat *stat, u32 request_mask, struct inode *inode)
> }
>
> stat->mtime = inode_get_mtime(inode);
> - stat->ctime.tv_sec = inode->i_ctime_sec;
> + stat->ctime.tv_sec = inode_get_ctime_sec(inode);
> stat->ctime.tv_nsec = (u32)atomic_read(pcn);
> if (!(stat->ctime.tv_nsec & I_CTIME_QUERIED))
> stat->ctime.tv_nsec = ((u32)atomic_fetch_or(I_CTIME_QUERIED, pcn));
> diff --git a/include/linux/fs.h b/include/linux/fs.h
> index d10897b3a1e3..e5b97e324db1 100644
> --- a/include/linux/fs.h
> +++ b/include/linux/fs.h
> @@ -1598,12 +1598,12 @@ struct timespec64 inode_set_ctime_deleg(struct inode *inode,
>
> static inline time64_t inode_get_atime_sec(const struct inode *inode)
> {
> - return inode->i_atime_sec;
> + return READ_ONCE(inode->i_atime_sec);
> }
>
> static inline long inode_get_atime_nsec(const struct inode *inode)
> {
> - return inode->i_atime_nsec;
> + return READ_ONCE(inode->i_atime_nsec);
> }
>
> static inline struct timespec64 inode_get_atime(const struct inode *inode)
> @@ -1617,8 +1617,8 @@ static inline struct timespec64 inode_get_atime(const struct inode *inode)
> static inline struct timespec64 inode_set_atime_to_ts(struct inode *inode,
> struct timespec64 ts)
> {
> - inode->i_atime_sec = ts.tv_sec;
> - inode->i_atime_nsec = ts.tv_nsec;
> + WRITE_ONCE(inode->i_atime_sec, ts.tv_sec);
> + WRITE_ONCE(inode->i_atime_nsec, ts.tv_nsec);
> return ts;
> }
>
> @@ -1633,12 +1633,12 @@ static inline struct timespec64 inode_set_atime(struct inode *inode,
>
> static inline time64_t inode_get_mtime_sec(const struct inode *inode)
> {
> - return inode->i_mtime_sec;
> + return READ_ONCE(inode->i_mtime_sec);
> }
>
> static inline long inode_get_mtime_nsec(const struct inode *inode)
> {
> - return inode->i_mtime_nsec;
> + return READ_ONCE(inode->i_mtime_nsec);
> }
>
> static inline struct timespec64 inode_get_mtime(const struct inode *inode)
> @@ -1651,8 +1651,8 @@ static inline struct timespec64 inode_get_mtime(const struct inode *inode)
> static inline struct timespec64 inode_set_mtime_to_ts(struct inode *inode,
> struct timespec64 ts)
> {
> - inode->i_mtime_sec = ts.tv_sec;
> - inode->i_mtime_nsec = ts.tv_nsec;
> + WRITE_ONCE(inode->i_mtime_sec, ts.tv_sec);
> + WRITE_ONCE(inode->i_mtime_nsec, ts.tv_nsec);
> return ts;
> }
>
> @@ -1677,12 +1677,12 @@ static inline struct timespec64 inode_set_mtime(struct inode *inode,
>
> static inline time64_t inode_get_ctime_sec(const struct inode *inode)
> {
> - return inode->i_ctime_sec;
> + return READ_ONCE(inode->i_ctime_sec);
> }
>
> static inline long inode_get_ctime_nsec(const struct inode *inode)
> {
> - return inode->i_ctime_nsec & ~I_CTIME_QUERIED;
> + return READ_ONCE(inode->i_ctime_nsec) & ~I_CTIME_QUERIED;
> }
>
> static inline struct timespec64 inode_get_ctime(const struct inode *inode)
Nice catch. We could also do this with data_race(), I suppose, which
would allow the compiler to do a bit more optimization, but I don't
think it'll make any significant difference to the generated code.
Reviewed-by: Jeff Layton <jlayton@kernel.org>
prev parent reply other threads:[~2026-07-08 11:15 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-08 8:02 [PATCH] fs: annotate inode timestamp accessors Yu Peng
2026-07-08 11:15 ` Jeff Layton [this message]
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=67fb179754d4d69a9342ca86ce89d659c7704895.camel@kernel.org \
--to=jlayton@kernel.org \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pengyu@kylinos.cn \
--cc=syzbot+8b3bd9f8a06658479d4a@syzkaller.appspotmail.com \
--cc=viro@zeniv.linux.org.uk \
/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