mirror of
https://github.com/Dasharo/linux.git
synced 2026-03-06 15:25:10 -08:00
Merge patch series "timekeeping/fs: multigrain timestamp redux"
Jeff Layton <jlayton@kernel.org> says: The VFS has always used coarse-grained timestamps when updating the ctime and mtime after a change. This has the benefit of allowing filesystems to optimize away a lot metadata updates, down to around 1 per jiffy, even when a file is under heavy writes. Unfortunately, this has always been an issue when we're exporting via NFSv3, which relies on timestamps to validate caches. A lot of changes can happen in a jiffy, so timestamps aren't sufficient to help the client decide when to invalidate the cache. Even with NFSv4, a lot of exported filesystems don't properly support a change attribute and are subject to the same problems with timestamp granularity. Other applications have similar issues with timestamps (e.g backup applications). If we were to always use fine-grained timestamps, that would improve the situation, but that becomes rather expensive, as the underlying filesystem would have to log a lot more metadata updates. What we need is a way to only use fine-grained timestamps when they are being actively queried. Use the (unused) top bit in inode->i_ctime_nsec as a flag that indicates whether the current timestamps have been queried via stat() or the like. When it's set, we allow the kernel to use a fine-grained timestamp iff it's necessary to make the ctime show a different value. This solves the problem of being able to distinguish the timestamp between updates, but introduces a new problem: it's now possible for a file being changed to get a fine-grained timestamp. A file that is altered just a bit later can then get a coarse-grained one that appears older than the earlier fine-grained time. This violates timestamp ordering guarantees. To remedy this, keep a global monotonic atomic64_t value that acts as a timestamp floor. When we go to stamp a file, we first get the latter of the current floor value and the current coarse-grained time. If the inode ctime hasn't been queried then we just attempt to stamp it with that value. If it has been queried, then first see whether the current coarse time is later than the existing ctime. If it is, then we accept that value. If it isn't, then we get a fine-grained time and try to swap that into the global floor. Whether that succeeds or fails, we take the resulting floor time, convert it to realtime and try to swap that into the ctime. We take the result of the ctime swap whether it succeeds or fails, since either is just as valid. Filesystems can opt into this by setting the FS_MGTIME fstype flag. Others should be unaffected (other than being subject to the same floor value as multigrain filesystems). * patches from https://lore.kernel.org/r/20241002-mgtime-v10-0-d1c4717f5284@kernel.org: tmpfs: add support for multigrain timestamps btrfs: convert to multigrain timestamps ext4: switch to multigrain timestamps xfs: switch to multigrain timestamps Documentation: add a new file documenting multigrain timestamps fs: add percpu counters for significant multigrain timestamp events fs: tracepoints around multigrain timestamp events fs: handle delegated timestamps in setattr_copy_mgtime fs: have setattr_copy handle multigrain timestamps appropriately fs: add infrastructure for multigrain timestamps Link: https://lore.kernel.org/r/20241002-mgtime-v10-0-d1c4717f5284@kernel.org Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
+264
-26
@@ -21,7 +21,12 @@
|
||||
#include <linux/list_lru.h>
|
||||
#include <linux/iversion.h>
|
||||
#include <linux/rw_hint.h>
|
||||
#include <linux/seq_file.h>
|
||||
#include <linux/debugfs.h>
|
||||
#include <trace/events/writeback.h>
|
||||
#define CREATE_TRACE_POINTS
|
||||
#include <trace/events/timestamp.h>
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
/*
|
||||
@@ -98,6 +103,70 @@ long get_nr_dirty_inodes(void)
|
||||
return nr_dirty > 0 ? nr_dirty : 0;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_DEBUG_FS
|
||||
static DEFINE_PER_CPU(long, mg_ctime_updates);
|
||||
static DEFINE_PER_CPU(long, mg_fine_stamps);
|
||||
static DEFINE_PER_CPU(long, mg_ctime_swaps);
|
||||
|
||||
static unsigned long get_mg_ctime_updates(void)
|
||||
{
|
||||
unsigned long sum = 0;
|
||||
int i;
|
||||
|
||||
for_each_possible_cpu(i)
|
||||
sum += data_race(per_cpu(mg_ctime_updates, i));
|
||||
return sum;
|
||||
}
|
||||
|
||||
static unsigned long get_mg_fine_stamps(void)
|
||||
{
|
||||
unsigned long sum = 0;
|
||||
int i;
|
||||
|
||||
for_each_possible_cpu(i)
|
||||
sum += data_race(per_cpu(mg_fine_stamps, i));
|
||||
return sum;
|
||||
}
|
||||
|
||||
static unsigned long get_mg_ctime_swaps(void)
|
||||
{
|
||||
unsigned long sum = 0;
|
||||
int i;
|
||||
|
||||
for_each_possible_cpu(i)
|
||||
sum += data_race(per_cpu(mg_ctime_swaps, i));
|
||||
return sum;
|
||||
}
|
||||
|
||||
#define mgtime_counter_inc(__var) this_cpu_inc(__var)
|
||||
|
||||
static int mgts_show(struct seq_file *s, void *p)
|
||||
{
|
||||
unsigned long ctime_updates = get_mg_ctime_updates();
|
||||
unsigned long ctime_swaps = get_mg_ctime_swaps();
|
||||
unsigned long fine_stamps = get_mg_fine_stamps();
|
||||
unsigned long floor_swaps = timekeeping_get_mg_floor_swaps();
|
||||
|
||||
seq_printf(s, "%lu %lu %lu %lu\n",
|
||||
ctime_updates, ctime_swaps, fine_stamps, floor_swaps);
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEFINE_SHOW_ATTRIBUTE(mgts);
|
||||
|
||||
static int __init mg_debugfs_init(void)
|
||||
{
|
||||
debugfs_create_file("multigrain_timestamps", S_IFREG | S_IRUGO, NULL, NULL, &mgts_fops);
|
||||
return 0;
|
||||
}
|
||||
late_initcall(mg_debugfs_init);
|
||||
|
||||
#else /* ! CONFIG_DEBUG_FS */
|
||||
|
||||
#define mgtime_counter_inc(__var) do { } while (0)
|
||||
|
||||
#endif /* CONFIG_DEBUG_FS */
|
||||
|
||||
/*
|
||||
* Handle nr_inode sysctl
|
||||
*/
|
||||
@@ -2209,19 +2278,58 @@ int file_remove_privs(struct file *file)
|
||||
}
|
||||
EXPORT_SYMBOL(file_remove_privs);
|
||||
|
||||
/**
|
||||
* current_time - Return FS time (possibly fine-grained)
|
||||
* @inode: inode.
|
||||
*
|
||||
* Return the current time truncated to the time granularity supported by
|
||||
* the fs, as suitable for a ctime/mtime change. If the ctime is flagged
|
||||
* as having been QUERIED, get a fine-grained timestamp, but don't update
|
||||
* the floor.
|
||||
*
|
||||
* For a multigrain inode, this is effectively an estimate of the timestamp
|
||||
* that a file would receive. An actual update must go through
|
||||
* inode_set_ctime_current().
|
||||
*/
|
||||
struct timespec64 current_time(struct inode *inode)
|
||||
{
|
||||
struct timespec64 now;
|
||||
u32 cns;
|
||||
|
||||
ktime_get_coarse_real_ts64_mg(&now);
|
||||
|
||||
if (!is_mgtime(inode))
|
||||
goto out;
|
||||
|
||||
/* If nothing has queried it, then coarse time is fine */
|
||||
cns = smp_load_acquire(&inode->i_ctime_nsec);
|
||||
if (cns & I_CTIME_QUERIED) {
|
||||
/*
|
||||
* If there is no apparent change, then get a fine-grained
|
||||
* timestamp.
|
||||
*/
|
||||
if (now.tv_nsec == (cns & ~I_CTIME_QUERIED))
|
||||
ktime_get_real_ts64(&now);
|
||||
}
|
||||
out:
|
||||
return timestamp_truncate(now, inode);
|
||||
}
|
||||
EXPORT_SYMBOL(current_time);
|
||||
|
||||
static int inode_needs_update_time(struct inode *inode)
|
||||
{
|
||||
struct timespec64 now, ts;
|
||||
int sync_it = 0;
|
||||
struct timespec64 now = current_time(inode);
|
||||
struct timespec64 ts;
|
||||
|
||||
/* First try to exhaust all avenues to not sync */
|
||||
if (IS_NOCMTIME(inode))
|
||||
return 0;
|
||||
|
||||
now = current_time(inode);
|
||||
|
||||
ts = inode_get_mtime(inode);
|
||||
if (!timespec64_equal(&ts, &now))
|
||||
sync_it = S_MTIME;
|
||||
sync_it |= S_MTIME;
|
||||
|
||||
ts = inode_get_ctime(inode);
|
||||
if (!timespec64_equal(&ts, &now))
|
||||
@@ -2598,6 +2706,16 @@ void inode_nohighmem(struct inode *inode)
|
||||
}
|
||||
EXPORT_SYMBOL(inode_nohighmem);
|
||||
|
||||
struct timespec64 inode_set_ctime_to_ts(struct inode *inode, struct timespec64 ts)
|
||||
{
|
||||
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;
|
||||
return ts;
|
||||
}
|
||||
EXPORT_SYMBOL(inode_set_ctime_to_ts);
|
||||
|
||||
/**
|
||||
* timestamp_truncate - Truncate timespec to a granularity
|
||||
* @t: Timespec
|
||||
@@ -2629,41 +2747,161 @@ struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode)
|
||||
}
|
||||
EXPORT_SYMBOL(timestamp_truncate);
|
||||
|
||||
/**
|
||||
* current_time - Return FS time
|
||||
* @inode: inode.
|
||||
*
|
||||
* Return the current time truncated to the time granularity supported by
|
||||
* the fs.
|
||||
*
|
||||
* Note that inode and inode->sb cannot be NULL.
|
||||
* Otherwise, the function warns and returns time without truncation.
|
||||
*/
|
||||
struct timespec64 current_time(struct inode *inode)
|
||||
{
|
||||
struct timespec64 now;
|
||||
|
||||
ktime_get_coarse_real_ts64(&now);
|
||||
return timestamp_truncate(now, inode);
|
||||
}
|
||||
EXPORT_SYMBOL(current_time);
|
||||
|
||||
/**
|
||||
* inode_set_ctime_current - set the ctime to current_time
|
||||
* @inode: inode
|
||||
*
|
||||
* Set the inode->i_ctime to the current value for the inode. Returns
|
||||
* the current value that was assigned to i_ctime.
|
||||
* Set the inode's ctime to the current value for the inode. Returns the
|
||||
* current value that was assigned. If this is not a multigrain inode, then we
|
||||
* set it to the later of the coarse time and floor value.
|
||||
*
|
||||
* If it is multigrain, then we first see if the coarse-grained timestamp is
|
||||
* distinct from what is already there. If so, then use that. Otherwise, get a
|
||||
* fine-grained timestamp.
|
||||
*
|
||||
* After that, try to swap the new value into i_ctime_nsec. Accept the
|
||||
* resulting ctime, regardless of the outcome of the swap. If it has
|
||||
* already been replaced, then that timestamp is later than the earlier
|
||||
* unacceptable one, and is thus acceptable.
|
||||
*/
|
||||
struct timespec64 inode_set_ctime_current(struct inode *inode)
|
||||
{
|
||||
struct timespec64 now = current_time(inode);
|
||||
struct timespec64 now;
|
||||
u32 cns, cur;
|
||||
|
||||
inode_set_ctime_to_ts(inode, now);
|
||||
ktime_get_coarse_real_ts64_mg(&now);
|
||||
now = timestamp_truncate(now, inode);
|
||||
|
||||
/* Just return that if this is not a multigrain fs */
|
||||
if (!is_mgtime(inode)) {
|
||||
inode_set_ctime_to_ts(inode, now);
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*
|
||||
* A fine-grained time is only needed if someone has queried
|
||||
* for timestamps, and the current coarse grained time isn't
|
||||
* later than what's already there.
|
||||
*/
|
||||
cns = smp_load_acquire(&inode->i_ctime_nsec);
|
||||
if (cns & I_CTIME_QUERIED) {
|
||||
struct timespec64 ctime = { .tv_sec = inode->i_ctime_sec,
|
||||
.tv_nsec = cns & ~I_CTIME_QUERIED };
|
||||
|
||||
if (timespec64_compare(&now, &ctime) <= 0) {
|
||||
ktime_get_real_ts64_mg(&now);
|
||||
now = timestamp_truncate(now, inode);
|
||||
mgtime_counter_inc(mg_fine_stamps);
|
||||
}
|
||||
}
|
||||
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) {
|
||||
trace_ctime_xchg_skip(inode, &now);
|
||||
goto out;
|
||||
}
|
||||
cur = cns;
|
||||
retry:
|
||||
/* 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;
|
||||
trace_ctime_ns_xchg(inode, cns, now.tv_nsec, cur);
|
||||
mgtime_counter_inc(mg_ctime_swaps);
|
||||
} else {
|
||||
/*
|
||||
* Was the change due to someone marking the old ctime QUERIED?
|
||||
* If so then retry the swap. This can only happen once since
|
||||
* the only way to clear I_CTIME_QUERIED is to stamp the inode
|
||||
* with a new ctime.
|
||||
*/
|
||||
if (!(cns & I_CTIME_QUERIED) && (cns | I_CTIME_QUERIED) == cur) {
|
||||
cns = cur;
|
||||
goto retry;
|
||||
}
|
||||
/* Otherwise, keep the existing ctime */
|
||||
now.tv_sec = inode->i_ctime_sec;
|
||||
now.tv_nsec = cur & ~I_CTIME_QUERIED;
|
||||
}
|
||||
out:
|
||||
return now;
|
||||
}
|
||||
EXPORT_SYMBOL(inode_set_ctime_current);
|
||||
|
||||
/**
|
||||
* inode_set_ctime_deleg - try to update the ctime on a delegated inode
|
||||
* @inode: inode to update
|
||||
* @update: timespec64 to set the ctime
|
||||
*
|
||||
* Attempt to atomically update the ctime on behalf of a delegation holder.
|
||||
*
|
||||
* The nfs server can call back the holder of a delegation to get updated
|
||||
* inode attributes, including the mtime. When updating the mtime, update
|
||||
* the ctime to a value at least equal to that.
|
||||
*
|
||||
* This can race with concurrent updates to the inode, in which
|
||||
* case the update is skipped.
|
||||
*
|
||||
* Note that this works even when multigrain timestamps are not enabled,
|
||||
* so it is used in either case.
|
||||
*/
|
||||
struct timespec64 inode_set_ctime_deleg(struct inode *inode, struct timespec64 update)
|
||||
{
|
||||
struct timespec64 now, cur_ts;
|
||||
u32 cur, old;
|
||||
|
||||
/* 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;
|
||||
|
||||
/* If the update is older than the existing value, skip it. */
|
||||
if (timespec64_compare(&update, &cur_ts) <= 0)
|
||||
return cur_ts;
|
||||
|
||||
ktime_get_coarse_real_ts64_mg(&now);
|
||||
|
||||
/* Clamp the update to "now" if it's in the future */
|
||||
if (timespec64_compare(&update, &now) > 0)
|
||||
update = now;
|
||||
|
||||
update = timestamp_truncate(update, inode);
|
||||
|
||||
/* No need to update if the values are already the same */
|
||||
if (timespec64_equal(&update, &cur_ts))
|
||||
return cur_ts;
|
||||
|
||||
/*
|
||||
* Try to swap the nsec value into place. If it fails, that means
|
||||
* it raced with an update due to a write or similar activity. That
|
||||
* stamp takes precedence, so just skip the update.
|
||||
*/
|
||||
retry:
|
||||
old = cur;
|
||||
if (try_cmpxchg(&inode->i_ctime_nsec, &cur, update.tv_nsec)) {
|
||||
inode->i_ctime_sec = update.tv_sec;
|
||||
mgtime_counter_inc(mg_ctime_swaps);
|
||||
return update;
|
||||
}
|
||||
|
||||
/*
|
||||
* Was the change due to another task marking the old ctime QUERIED?
|
||||
*
|
||||
* If so, then retry the swap. This can only happen once since
|
||||
* the only way to clear I_CTIME_QUERIED is to stamp the inode
|
||||
* with a new ctime.
|
||||
*/
|
||||
if (!(old & I_CTIME_QUERIED) && (cur == (old | I_CTIME_QUERIED)))
|
||||
goto retry;
|
||||
|
||||
/* Otherwise, it was a new timestamp. */
|
||||
cur_ts.tv_sec = inode->i_ctime_sec;
|
||||
cur_ts.tv_nsec = cur & ~I_CTIME_QUERIED;
|
||||
return cur_ts;
|
||||
}
|
||||
EXPORT_SYMBOL(inode_set_ctime_deleg);
|
||||
|
||||
/**
|
||||
* in_group_or_capable - check whether caller is CAP_FSETID privileged
|
||||
* @idmap: idmap of the mount @inode was found from
|
||||
|
||||
Reference in New Issue
Block a user