mirror of
https://github.com/torvalds/linux.git
synced 2026-09-13 15:40:03 +02:00
fs: annotate inode timestamp accessors
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: 4e40eff0b5 ("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>
Link: https://patch.msgid.link/20260708080232.2564807-1-pengyu@kylinos.cn
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
This commit is contained in:
parent
f797d7b64e
commit
c610d2d078
18
fs/inode.c
18
fs/inode.c
|
|
@ -2830,8 +2830,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);
|
||||
|
|
@ -2905,7 +2905,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) {
|
||||
|
|
@ -2917,7 +2917,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;
|
||||
}
|
||||
|
|
@ -2926,7 +2926,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 {
|
||||
|
|
@ -2941,7 +2941,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:
|
||||
|
|
@ -2974,7 +2974,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)
|
||||
|
|
@ -3000,7 +3000,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;
|
||||
}
|
||||
|
|
@ -3016,7 +3016,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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user