Linux swap files don't seem to update their modification time when you swap

November 9, 2022

I recently wrote about how we're moving away from swap partitions to swap files on our Linux servers, since swap files work fine (and are what Ubuntu defaults to) and swap partitions are slightly more work to set up. For reasons that will be covered in a later entry, today I looked at the modification time (and 'ctime', the inode change time) for those swap files. Somewhat to my surprise, all of them had the modification time of when the system was installed, which in many cases was a fair while ago.

It might be possible that some of these systems have never used their swap space since they were installed. But on other ones I know for a fact that they've used swap since then, and so if swap file IO was similar to regular Unix disk IO, their file modification times would have changed. Obviously, in Linux this is not the case; when the kernel writes things out to a swap file, it doesn't update the file's modification time. It just stuffs the blocks onto the disk (in our case, onto the software RAID).

Not updating the swap file's modification time is a sensible decision that reduces both IO and complexity. Updating file inode times is a known source of unnecessary IO (updating the access time is frequently disabled or significantly limited), and it would add extra work to the 'swap pages out' path to mark the relevant inode for update as well (it might also require taking internal kernel locks to modify the inode's in-memory data). This is clearly a special kernel path, since even using mmap() updates the file modification time (although I believe not necessarily to a completely predictable time).

(And of course there's no 'swap partition modification time', so it's not as if you're losing information by moving from swap partitions to swap files. In both cases you don't get any information on the most recent time the swap area has been written to.)

PS: I suspect that not updating modification time isn't considered part of the swapfile 'API', to such an extent as there is one, so this might change in the future. And if for some reason your swapfile is on NFS or another remote filesystem, the server may well update the file's modification time even though your client kernel hasn't asked it to. But swapping over NFS is probably not a thing any more, and I don't know if Linux even supports it.

Written on 09 November 2022.
« Some thoughts on organizations running their own Fediverse instance
The problem of (Unix) swapfiles and server backups »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Wed Nov 9 22:33:27 2022
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.