[PATCH 2/2] fs: don't update the atime if existing atime is newer than "now"

From: Jeff Layton
Date: Thu Sep 07 2023 - 13:53:10 EST


It's possible for the atime to be updated with a fine-grained timestamp
and then later get an update that uses a coarse-grained timestamp which
makes the atime appear to go backward.

Fix this by only updating the atime if "now" is later than the current
value.

Reported-by: kernel test robot <oliver.sang@xxxxxxxxx>
Closes: https://lore.kernel.org/oe-lkp/202309071017.a64aca5e-oliver.sang@xxxxxxxxx
Signed-off-by: Jeff Layton <jlayton@xxxxxxxxxx>
---
fs/inode.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/inode.c b/fs/inode.c
index 54237f4242ff..cf4726b7f4b5 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -1905,7 +1905,7 @@ int inode_update_timestamps(struct inode *inode, int flags)
}

if (flags & S_ATIME) {
- if (!timespec64_equal(&now, &inode->i_atime)) {
+ if (timespec64_compare(&inode->i_atime, &now) < 0) {
inode->i_atime = now;
updated |= S_ATIME;
}
@@ -1991,7 +1991,7 @@ bool atime_needs_update(const struct path *path, struct inode *inode)
if (!relatime_need_update(mnt, inode, now))
return false;

- if (timespec64_equal(&inode->i_atime, &now))
+ if (timespec64_compare(&inode->i_atime, &now) >= 0)
return false;

return true;

--
2.41.0