ext4: fast commit: snapshot inode state for FC log
Subsystems (2)
v5 cover ext4: fast commit: snapshot inode state for FC log
Hi,
(This RFC v5 series is based on linux-next tag next-20260106 plus the prerequisite patch "ext4: fast commit: make s_fc_lock reclaim-safe":
https://lore.kernel.org/all/[email protected]/)
Zhang Yi in RFC v3 review pointed out that postponing lockdep assertions only masks the issue, and that sleeping in ext4_fc_track_inode() while holding i_data_sem can form a real ABBA deadlock if the fast commit writer also needs i_data_sem while the inode is in FC_COMMITTING.
Zhang Yi suggested two possible directions to address the root cause:
1. "Ha, the solution seems to have already been listed in the TODOs in fast_commit.c.
Change ext4_fc_commit() to lookup logical to physical mapping using extent status tree. This would get rid of the need to call ext4_fc_track_inode() before acquiring i_data_sem. To do that we would need to ensure that modified extents from the extent status tree are not evicted from memory."
2. "Alternatively, recording the mapped range of tracking might also be feasible."
This series implements a hybrid way: it implements approach 2 by snapshotting inode image and mapped ranges at commit time, and consuming only snapshots during log writing.
Approach 2 still needs a mapping source while building the snapshot (logical-to-physical and unwritten/hole semantics). Calling ext4_map_blocks() there would take i_data_sem and can block inside the jbd2_journal_lock_updates() window, which risks deadlocks or unbounded stalls. So the snapshot path uses approach 1's extent status lookups as a best-effort mapping source to avoid ext4_map_blocks().
I did not fully implement approach 1 (making extent status lookups authoritative by preventing reclaim of needed entries) because that would need additional pinning/integration under memory pressure and a larger correctness surface. Instead, the extent status tree is treated as a cache and the snapshot path falls back to full commit on cache misses or unstable mappings (e.g. delayed allocation).
Lock inversion / deadlock model (before):
CPU0 (metadata update) CPU1 (fast commit)
-------------------- -----------------
... hold i_data_sem (A) mutex_lock(s_fc_lock) (B)
ext4_fc_track_inode() ext4_fc_write_inode_data()
mutex_lock(s_fc_lock) (B) ext4_map_blocks()
wait FC_COMMITTING (sleep) down_read(i_data_sem) (A)
This creates i_data_sem (A) -> s_fc_lock (B) on update paths, and s_fc_lock (B) -> i_data_sem (A) on commit paths. Once CPU0 sleeps while holding (A), CPU1 can block on (A) while holding (B), completing the ABBA cycle.
New model (this series):
CPU0 (metadata update) CPU1 (fast commit)
-------------------- -----------------
... maybe hold i_data_sem (A) jbd2_journal_lock_updates()
ext4_fc_track_*() snapshot inode + ranges (no map_blocks)
mutex_lock(s_fc_lock) (B) jbd2_journal_unlock_updates()
if FC_COMMITTING: set FC_REQUEUE s_fc_lock (B)
no sleep write FC log from snapshots only
cleanup: clear COMMITTING, requeue if set
The commit path no longer takes i_data_sem while holding s_fc_lock, and tracking no longer sleeps waiting for FC_COMMITTING. If an inode is updated during a fast commit, EXT4_STATE_FC_REQUEUE records that fact and the inode is moved to FC_Q_STAGING for the next commit.
The only remaining FC_COMMITTING waiter is ext4_fc_del(), which drops s_fc_lock before sleeping.
This series snapshots the on-disk inode and tracked data ranges while journal updates are locked and existing handles are drained. The log writing phase then serializes only snapshots, so it no longer needs to call ext4_map_blocks() and take i_data_sem under s_fc_lock. This is done in two steps: patch 1 drops ext4_map_blocks() from log writing by introducing commit-time snapshots, and patch 5 drops ext4_map_blocks() from the snapshot path by using the extent status cache. The snapshot also records whether a mapped extent is unwritten, so the ADD_RANGE records (and replay) preserve unwritten semantics.
Snapshotting runs under jbd2_journal_lock_updates(). Since a cache miss in ext4_get_inode_loc() can start synchronous inode table I/O and stall handle starts for milliseconds, patch 1 uses ext4_get_inode_loc_noio() and falls back to full commit if the inode table block is not present or not uptodate.
ext4_fc_track_inode() also stops waiting for FC_COMMITTING. Updates during an ongoing fast commit are marked with EXT4_STATE_FC_REQUEUE and are replayed in the next fast commit, while ext4_fc_del() waits for FC_COMMITTING so an inode cannot be removed while the commit thread is still using it.
The extent status tree is a cache, not an authoritative source, so the snapshot path falls back to full commit on cache misses or unstable mappings (e.g. delayed allocation). This includes cases where extent status entries are not present (or have been reclaimed) under memory pressure. The snapshot path does not try to rebuild mappings by calling ext4_map_blocks(); instead it simply marks the transaction fast commit ineligible.
To keep the updates-locked window bounded, the snapshot path caps the number of snapshotted inodes and ranges per fast commit (currently 1024 inodes and 2048 ranges) and falls back to full commit when the cap is exceeded. The series also handles the journal inode i_data_sem lockdep false positive via subclassing; journal inode mapping may still take i_data_sem even when data inode mapping is avoided.
Patch 6 adds the ext4_fc_lock_updates tracepoint to quantify the updates-locked window and snapshot fallback reasons. Patch 7 extends /proc/fs/ext4/<sb_id>/fc_info with best-effort snapshot counters. If the /proc interface is undesirable, I can drop patch 7 and keep the tracepoint only, or drop even both.
Testing and measurement were done on a QEMU/KVM guest with virtio-pmem + dax (ext4 -O fast_commit, mounted dax,noatime). The workload does python3 500x {4K write + fsync}, fallocate 256M, and python3 500x {creat + fsync(dir)}. Over 3 cold boots, ext4_fc_lock_updates reported locked_ns p50 2.88-2.92 us, p99 <= 6.71 us, and max <= 102.71 us, with snap_err always 0. Under stress-ng memory pressure (stress-ng --vm 4 --vm-bytes 75% --timeout 60s), locked_ns p50 2.94 us, p99 <= 4.97 us, and max <= 20.07 us. The fc_info snapshot failure counters stayed at 0.
These hold times are in the low microseconds range, and the caps keep the worst case bounded.
Comments and guidance are very welcome. Please let me know if there are any concerns about correctness, corner cases, or better approaches.
RFC v4 -> RFC v5:
- Patch 6: Make ext4_fc_lock_updates snap_err human readable via TRACE_DEFINE_ENUM() + __print_symbolic(), using a single TRACE_SNAP_ERR mapping while keeping the enum values stable for tooling.
RFC v3 -> RFC v4:
- Replace lockdep_assert movement with removing the wait in ext4_fc_track_inode() and using EXT4_STATE_FC_REQUEUE to capture updates during an ongoing fast commit.
- Replace dropping s_fc_lock around log writing with commit-time snapshots of inode image and mapped ranges (recording the mapped range of tracking as suggested by Zhang Yi) so log writing consumes only snapshots.
- Avoid inode table I/O under jbd2_journal_lock_updates() via ext4_get_inode_loc_noio() and fallback to full commit on cache misses.
- Use the extent status cache for snapshot mappings and fall back to full commit on cache misses or unstable mappings (e.g. delayed allocation).
- Add tracepoint and /proc snapshot stats to quantify the updates-locked window and snapshot fallback reasons.
RFC v2 -> RFC v3:
- rebase on top of
https://lore.kernel.org/linux-ext4/[email protected]/T/#u
RFC v1 -> RFC v2:
- patch 1: move comments to correct place
- patch 2: add it to patchset.
- add missing RFC prefix
RFC v1: https://lore.kernel.org/linux-ext4/[email protected]/T/#u
RFC v2: https://lore.kernel.org/linux-ext4/[email protected]/T/#t
RFC v3: https://lore.kernel.org/linux-ext4/[email protected]/
RFC v4: https://lore.kernel.org/all/[email protected]/t/#m9a6c8f2391c6dc67471e918a0577b130e7633e49
Thanks,
Li Chen (7):
ext4: fast commit: snapshot inode state before writing log
ext4: lockdep: handle i_data_sem subclassing for special inodes
ext4: fast commit: avoid waiting for FC_COMMITTING
ext4: fast commit: avoid self-deadlock in inode snapshotting
ext4: fast commit: avoid i_data_sem by dropping ext4_map_blocks() in
snapshots
ext4: fast commit: add lock_updates tracepoint
ext4: fast commit: export snapshot stats in fc_info
fs/ext4/ext4.h | 73 +++- fs/ext4/fast_commit.c | 703 +++++++++++++++++++++++++++++------- fs/ext4/inode.c | 51 +++ fs/ext4/super.c | 9 + include/trace/events/ext4.h | 61 ++++ 5 files changed, 763 insertions(+), 134 deletions(-)
Signature · 1 line
2.53.0
RFC v5 1/7 ext4: fast commit: snapshot inode state before writing log
Fast commit writes inode metadata and data range updates after unlocking journal updates. New handles can start at that point, so the log writing path must not look at live inode state.
Add a commit-time per-inode snapshot and populate it while journal updates are locked and existing handles are drained. Store the snapshot behind ext4_inode_info->i_fc_snap so ext4_inode_info only grows by one pointer. The snapshot contains a copy of the on-disk inode plus the data range records needed for fast commit TLVs.
Snapshotting runs under jbd2_journal_lock_updates(). Avoid triggering I/O there by using ext4_get_inode_loc_noio() and falling back to full commit if the inode table block is not present or not uptodate.
Log writing then only serializes the snapshot, so it no longer needs to call ext4_map_blocks() and take i_data_sem under s_fc_lock. The snapshot is installed and freed under s_fc_lock and is released from fast commit cleanup and inode eviction.
--- fs/ext4/ext4.h | 22 ++- fs/ext4/fast_commit.c | 330 +++++++++++++++++++++++++++++++++++------- fs/ext4/inode.c | 51 +++++++ 3 files changed, 351 insertions(+), 52 deletions(-)
fs/ext4/ext4.h +20 -2
@@ -1033,6 +1033,7 @@ enum { I_DATA_SEM_EA }; +struct ext4_fc_inode_snap ;/* * fourth extended file system inode data in memory
@@ -1089,6 +1090,22 @@ struct ext4_inode_info {/* End of lblk range that needs to be committed in this fast commit */ ext4_lblk_t i_fc_lblk_len; +/* +* Commit-time fast commit snapshots. +* +* i_fc_snap is installed and freed under sbi->s_fc_lock. The fast +* commit log writing path reads the snapshot under sbi->s_fc_lock while +* serializing fast commit TLVs. +* +* The snapshot lifetime is bounded by EXT4_STATE_FC_COMMITTING and the +* corresponding cleanup / eviction paths. +* +* i_fc_snap points to per-inode snapshot data for fast commit: +* - a raw inode snapshot for EXT4_FC_TAG_INODE +* - data range records for EXT4_FC_TAG_{ADD,DEL}_RANGE +*/ +struct ext4_fc_inode_snap *i_fc_snap; +spinlock_t i_raw_lock;/* protects updates to the raw inode */ /* Fast commit wait queue for this inode */
@@ -3093,8 +3110,9 @@ extern int ext4_file_getattr(struct mnt_idmap *, const struct path *, struct kstat *, u32, unsigned int);extern void ext4_dirty_inode (struct inode *,int );extern int ext4_change_inode_journal_flag (struct inode *,int ); -extern int ext4_get_inode_loc (struct inode *,struct ext4_iloc *); -extern int ext4_get_fc_inode_loc (struct super_block *sb,unsigned long ino, +int ext4_get_inode_loc (struct inode *inode,struct ext4_iloc *iloc); +int ext4_get_inode_loc_noio (struct inode *inode,struct ext4_iloc *iloc); +int ext4_get_fc_inode_loc (struct super_block *sb,unsigned long ino,struct ext4_iloc *iloc);extern int ext4_inode_attach_jinode (struct inode *inode);extern int ext4_can_truncate (struct inode *inode);
fs/ext4/fast_commit.c +280 -50
466 lines not on this page · raw message
fs/ext4/inode.c +51 -0
57 lines not on this page · raw message
Signature · 1 line
2.53.0
RFC v5 2/7 ext4: lockdep: handle i_data_sem subclassing for special inodes
In reply to Li ChenFast commit can hold s_fc_lock while writing journal blocks. Mapping the journal inode can take its i_data_sem. Normal inode update paths can take a data inode i_data_sem and then s_fc_lock, which makes lockdep report a circular dependency.
lockdep treats all i_data_sem instances as one lock class and cannot distinguish the journal inode i_data_sem from a regular inode i_data_sem. The journal inode is not tracked by fast commit and no FC waiters ever depend on it, so this is not a real ABBA deadlock. Assign the journal inode a dedicated i_data_sem lockdep subclass to avoid the false positive.
Inode cache objects can be recycled, so also reset i_data_sem to I_DATA_SEM_NORMAL when allocating an ext4 inode. Otherwise a new inode may inherit an old subclass (journal/quota/ea) and trigger lockdep warnings.
--- fs/ext4/ext4.h | 4 +++- fs/ext4/super.c | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-)
fs/ext4/ext4.h +3 -1
@@ -1025,12 +1025,14 @@ do { \* than the first * I_DATA_SEM_QUOTA - Used for quota inodes only * I_DATA_SEM_EA - Used for ea_inodes only +* I_DATA_SEM_JOURNAL - Used for journal inode only */ enum {I_DATA_SEM_NORMAL =0 ,I_DATA_SEM_OTHER ,I_DATA_SEM_QUOTA , -I_DATA_SEM_EA +I_DATA_SEM_EA , +I_DATA_SEM_JOURNAL };struct ext4_fc_inode_snap ;
fs/ext4/super.c +8 -0
@@ -1423,6 +1423,9 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)INIT_WORK (&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);ext4_fc_init_inode (&ei->vfs_inode);spin_lock_init (&ei->i_fc_lock); +#ifdef CONFIG_LOCKDEP +lockdep_set_subclass (&ei->i_data_sem,I_DATA_SEM_NORMAL ); +#endif return &ei->vfs_inode; }
@@ -5863,6 +5866,11 @@ static struct inode *ext4_get_journal_inode(struct super_block *sb,return ERR_PTR (-EFSCORRUPTED ); } +#ifdef CONFIG_LOCKDEP +lockdep_set_subclass (&EXT4_I (journal_inode)->i_data_sem, +I_DATA_SEM_JOURNAL ); +#endif +ext4_debug ("Journal inode found at %p: %lld bytes\n" , journal_inode, journal_inode->i_size);return journal_inode;
Signature · 1 line
2.53.0
RFC v5 3/7 ext4: fast commit: avoid waiting for FC_COMMITTING
In reply to Li Chenext4_fc_track_inode() can be called while holding i_data_sem (e.g. fallocate). Waiting for EXT4_STATE_FC_COMMITTING in that case risks an ABBA deadlock: i_data_sem -> wait(FC_COMMITTING) vs FC_COMMITTING -> wait(i_data_sem) in the commit task.
Now that fast commit snapshots inode state at commit time, updates during log writing do not need to block. Drop the wait and lockdep assertion in ext4_fc_track_inode(), and make ext4_fc_del() wait for FC_COMMITTING so an inode cannot be removed while the commit thread is still using it.
When an inode is modified during a fast commit, mark it with EXT4_STATE_FC_REQUEUE so cleanup keeps it queued for the next fast commit. This is needed because jbd2_fc_end_commit() invokes the cleanup callback with tid == 0, so tid-based requeue logic would requeue every inode.
Testing: tracepoint ext4:ext4_fc_commit_stop with two fsyncs in the same transaction. nblks is the number of journal blocks written for that fast commit. Before this change, the second fsync still wrote almost the same fast commit log (nblks 10->9), because tid == 0 in jbd2_fc_end_commit() caused the tid-based requeue logic to keep all inodes queued. After this change, only inodes modified during the commit are requeued, and the second fsync wrote a nearly empty fast commit (nblks 10->1).
--- fs/ext4/ext4.h | 1 + fs/ext4/fast_commit.c | 111 ++++++++++++++++++++---------------------- 2 files changed, 53 insertions(+), 59 deletions(-)
fs/ext4/ext4.h +1 -0
@@ -2004,6 +2004,7 @@ enum {EXT4_STATE_FC_COMMITTING ,/* Fast commit ongoing */ EXT4_STATE_FC_FLUSHING_DATA ,/* Fast commit flushing data */ EXT4_STATE_ORPHAN_FILE ,/* Inode orphaned in orphan file */ + EXT4_STATE_FC_REQUEUE, /* Inode modified during fast commit */ };#define EXT4_INODE_BIT_FNS (name, field, offset) \
fs/ext4/fast_commit.c +52 -59
183 lines not on this page · raw message
Signature · 1 line
2.53.0
RFC v5 4/7 ext4: fast commit: avoid self-deadlock in inode snapshotting
In reply to Li Chenext4_fc_snapshot_inodes() used igrab()/iput() to pin inodes while building commit-time snapshots. With ext4_fc_del() waiting for EXT4_STATE_FC_COMMITTING, iput() can trigger ext4_clear_inode()->ext4_fc_del() in the commit thread and deadlock waiting for the fast commit to finish.
Avoid taking extra references. Collect inode pointers under s_fc_lock and rely on EXT4_STATE_FC_COMMITTING to pin inodes until ext4_fc_cleanup() clears the bit.
Also set EXT4_STATE_FC_COMMITTING for create-only inodes referenced from the dentry update queue, and wake up waiters when ext4_fc_cleanup() clears the bit.
--- fs/ext4/fast_commit.c | 47 ++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 12 deletions(-)
fs/ext4/fast_commit.c +35 -12
@@ -1210,13 +1210,12 @@ static int ext4_fc_snapshot_inodes(journal_t *journal) alloc_ctx =ext4_fc_lock (sb);list_for_each_entry (iter, &sbi->s_fc_q[FC_Q_MAIN ], i_fc_list) { - inodes[i] =igrab (&iter->vfs_inode); -if (inodes[i]) - i++; + inodes[i++] = &iter->vfs_inode; }list_for_each_entry (fc_dentry, &sbi->s_fc_dentry_q[FC_Q_MAIN ], fcd_list) {struct ext4_inode_info *ei; +struct inode *inode;if (fc_dentry->fcd_op !=EXT4_FC_TAG_CREAT )continue ;
@@ -1226,12 +1225,20 @@ static int ext4_fc_snapshot_inodes(journal_t *journal)/* See the comment in ext4_fc_commit_dentry_updates(). */ ei =list_first_entry (&fc_dentry->fcd_dilist, struct ext4_inode_info, i_fc_dilist); + inode = &ei->vfs_inode;if (!list_empty (&ei->i_fc_list))continue ; - inodes[i] =igrab (&ei->vfs_inode); -if (inodes[i]) - i++; +/* +* Create-only inodes may only be referenced via fcd_dilist and +* not appear on s_fc_q[MAIN]. They may hit the last iput while +* we are snapshotting, but inode eviction calls ext4_fc_del(), +* which waits for FC_COMMITTING to clear. Mark them FC_COMMITTING +* so the inode stays pinned and the snapshot stays valid until +* ext4_fc_cleanup(). +*/ +ext4_set_inode_state (inode,EXT4_STATE_FC_COMMITTING ); + inodes[i++] = inode; }ext4_fc_unlock (sb, alloc_ctx);
@@ -1241,10 +1248,6 @@ static int ext4_fc_snapshot_inodes(journal_t *journal)break ; } -for (nr_inodes =0 ; nr_inodes < i; nr_inodes++) { -if (inodes[nr_inodes]) -iput (inodes[nr_inodes]); - }kvfree (inodes);return ret; }
@@ -1312,8 +1315,9 @@ static int ext4_fc_perform_commit(journal_t *journal)jbd2_journal_lock_updates (journal);/* * The journal is now locked. No more handles can start and all the -* previous handles are now drained. We now mark the inodes on the -* commit queue as being committed. +* previous handles are now drained. Snapshotting happens in this +* window so log writing can consume only stable snapshots without +* doing logical-to-physical mapping. */ alloc_ctx =ext4_fc_lock (sb);list_for_each_entry (iter, &sbi->s_fc_q[FC_Q_MAIN ], i_fc_list) {
@@ -1565,6 +1569,25 @@ static void ext4_fc_cleanup(journal_t *journal, int full, tid_t tid) struct ext4_inode_info, i_fc_dilist);ext4_fc_free_inode_snap (&ei->vfs_inode); +spin_lock (&ei->i_fc_lock); +ext4_clear_inode_state (&ei->vfs_inode, +EXT4_STATE_FC_REQUEUE ); +ext4_clear_inode_state (&ei->vfs_inode, +EXT4_STATE_FC_COMMITTING ); +spin_unlock (&ei->i_fc_lock); +/* +* Make sure clearing of EXT4_STATE_FC_COMMITTING is +* visible before we send the wakeup. Pairs with implicit +* barrier in prepare_to_wait() in ext4_fc_del(). +*/ +smp_mb (); +#if (BITS_PER_LONG <64 ) +wake_up_bit (&ei->i_state_flags, +EXT4_STATE_FC_COMMITTING ); +#else +wake_up_bit (&ei->i_flags, +EXT4_STATE_FC_COMMITTING ); +#endif }list_del_init (&fc_dentry->fcd_dilist);
Signature · 1 line
2.53.0
RFC v5 5/7 ext4: fast commit: avoid i_data_sem by dropping ext4_map_blocks() in snapshots
In reply to Li ChenCommit-time snapshots run under jbd2_journal_lock_updates(), so the work done there must stay bounded.
The snapshot path still used ext4_map_blocks() to build data ranges. This can take i_data_sem and pulls the mapping code into the snapshot logic. Build inode data range snapshots from the extent status tree instead.
The extent status tree is a cache, not an authoritative source. If the needed information is missing or unstable (e.g. delayed allocation), treat the transaction as fast commit ineligible and fall back to full commit.
Also cap the number of inodes and ranges snapshotted per fast commit and allocate range records from a dedicated slab cache. The inode pointer array is allocated outside the updates-locked window.
Testing: QEMU/KVM guest, virtio-pmem + dax, ext4 -O fast_commit, mounted dax,noatime. Ran python3 500x {4K write + fsync}, fallocate 256M, and python3 500x {creat + fsync(dir)} without lockdep splats or errors.
--- fs/ext4/fast_commit.c | 253 +++++++++++++++++++++++++++++------------- 1 file changed, 177 insertions(+), 76 deletions(-)
fs/ext4/fast_commit.c +177 -76
@@ -183,6 +183,15 @@#include <trace/events/ext4.h> static struct kmem_cache *ext4_fc_dentry_cachep; +static struct kmem_cache *ext4_fc_range_cachep; + +/* +* Avoid spending unbounded time/memory snapshotting highly fragmented files +* under jbd2_journal_lock_updates(). If we exceed this limit, fall back to +* full commit. +*/ +#define EXT4_FC_SNAPSHOT_MAX_INODES 1024 +#define EXT4_FC_SNAPSHOT_MAX_RANGES 2048static void ext4_end_buffer_io_sync (struct buffer_head *bh,int uptodate) {
@@ -954,7 +963,7 @@ static void ext4_fc_free_ranges(struct list_head *head)list_for_each_entry_safe (range, range_n, head, list) {list_del (&range->list); -kfree (range); +kmem_cache_free (ext4_fc_range_cachep, range); } }
@@ -972,16 +981,19 @@ static void ext4_fc_free_inode_snap(struct inode *inode) }static int ext4_fc_snapshot_inode_data (struct inode *inode, -struct list_head *ranges) +struct list_head *ranges, +unsigned int nr_ranges_total, +unsigned int *nr_rangesp) {struct ext4_inode_info *ei =EXT4_I (inode); +unsigned int nr_ranges =0 ;ext4_lblk_t start_lblk, end_lblk, cur_lblk; -struct ext4_map_blocks map; -int ret;spin_lock (&ei->i_fc_lock);if (ei->i_fc_lblk_len ==0 ) {spin_unlock (&ei->i_fc_lock); +if (nr_rangesp) + *nr_rangesp =0 ;return 0 ; } start_lblk = ei->i_fc_lblk_start;
@@ -994,61 +1006,78 @@ static int ext4_fc_snapshot_inode_data(struct inode *inode, start_lblk, end_lblk, inode->i_ino);while (cur_lblk <= end_lblk) { +struct extent_status es;struct ext4_fc_range *range; +ext4_lblk_t len; - map.m_lblk = cur_lblk; - map.m_len = end_lblk - cur_lblk +1 ; - ret =ext4_map_blocks (NULL , inode, &map, -EXT4_GET_BLOCKS_IO_SUBMIT | -EXT4_EX_NOCACHE ); -if (ret <0 ) -return -ECANCELED ; +if (!ext4_es_lookup_extent (inode, cur_lblk,NULL , &es,NULL )) +return -EAGAIN ; + +if (ext4_es_is_delayed (&es)) +return -EAGAIN ; -if (map.m_len ==0 ) { + len = es.es_len - (cur_lblk - es.es_lblk); +if (len > end_lblk - cur_lblk +1 ) + len = end_lblk - cur_lblk +1 ; +if (len ==0 ) { cur_lblk++;continue ; } - range =kmalloc (sizeof (*range),GFP_NOFS ); +if (nr_ranges_total + nr_ranges >=EXT4_FC_SNAPSHOT_MAX_RANGES ) +return -E2BIG ; + + range =kmem_cache_alloc (ext4_fc_range_cachep,GFP_NOFS );if (!range)return -ENOMEM ; + nr_ranges++; - range->lblk = map.m_lblk; - range->len = map.m_len; + range->lblk = cur_lblk; + range->len = len; range->pblk =0 ; range->unwritten = false; -if (ret ==0 ) { +if (ext4_es_is_hole (&es)) { range->tag =EXT4_FC_TAG_DEL_RANGE ; - }else { -unsigned int max = (map.m_flags &EXT4_MAP_UNWRITTEN ) ? -EXT_UNWRITTEN_MAX_LEN :EXT_INIT_MAX_LEN ; - -/* Limit the number of blocks in one extent */ - map.m_len =min (max, map.m_len); + }else if (ext4_es_is_written (&es) || +ext4_es_is_unwritten (&es)) { +unsigned int max; range->tag =EXT4_FC_TAG_ADD_RANGE ; - range->len = map.m_len; - range->pblk = map.m_pblk; - range->unwritten = !!(map.m_flags &EXT4_MAP_UNWRITTEN ); + range->pblk =ext4_es_pblock (&es) + + (cur_lblk - es.es_lblk); + range->unwritten =ext4_es_is_unwritten (&es); + + max = range->unwritten ?EXT_UNWRITTEN_MAX_LEN : +EXT_INIT_MAX_LEN ; +if (range->len > max) + range->len = max; + }else { +kmem_cache_free (ext4_fc_range_cachep, range); +return -EAGAIN ; }INIT_LIST_HEAD (&range->list);list_add_tail (&range->list, ranges); - cur_lblk += map.m_len; + cur_lblk += range->len; } +if (nr_rangesp) + *nr_rangesp = nr_ranges;return 0 ; } -static int ext4_fc_snapshot_inode (struct inode *inode) +static int ext4_fc_snapshot_inode (struct inode *inode, +unsigned int nr_ranges_total, +unsigned int *nr_rangesp) {struct ext4_inode_info *ei =EXT4_I (inode);struct ext4_fc_inode_snap *snap;int inode_len =EXT4_GOOD_OLD_INODE_SIZE ;struct ext4_iloc iloc;LIST_HEAD (ranges); +unsigned int nr_ranges =0 ;int ret;int alloc_ctx;
@@ -1072,7 +1101,8 @@ static int ext4_fc_snapshot_inode(struct inode *inode)memcpy (snap->inode_buf, (u8 *)ext4_raw_inode (&iloc), inode_len);brelse (iloc.bh); - ret =ext4_fc_snapshot_inode_data (inode, &ranges); + ret =ext4_fc_snapshot_inode_data (inode, &ranges, nr_ranges_total, + &nr_ranges);if (ret) {kfree (snap);ext4_fc_free_ranges (&ranges);
@@ -1085,10 +1115,11 @@ static int ext4_fc_snapshot_inode(struct inode *inode)list_splice_tail_init (&ranges, &snap->data_list);ext4_fc_unlock (inode->i_sb, alloc_ctx); +if (nr_rangesp) + *nr_rangesp = nr_ranges;return 0 ; } -/* Flushes data of all the inodes in the commit queue. */ static int ext4_fc_flush_data (journal_t *journal) {
@@ -1167,49 +1198,32 @@ static int ext4_fc_commit_dentry_updates(journal_t *journal, u32 *crc)return 0 ; } -static int ext4_fc_snapshot_inodes (journal_t *journal) +static int ext4_fc_alloc_snapshot_inodes (struct super_block *sb, +struct inode ***inodesp, +unsigned int *nr_inodesp); + +static int ext4_fc_snapshot_inodes (journal_t *journal,struct inode **inodes, +unsigned int inodes_size) {struct super_block *sb = journal->j_private;struct ext4_sb_info *sbi =EXT4_SB (sb);struct ext4_inode_info *iter;struct ext4_fc_dentry_update *fc_dentry; -struct inode **inodes; -unsigned int nr_inodes =0 ;unsigned int i =0 ; +unsigned int idx; +unsigned int nr_ranges =0 ;int ret =0 ;int alloc_ctx; - alloc_ctx =ext4_fc_lock (sb); -list_for_each_entry (iter, &sbi->s_fc_q[FC_Q_MAIN ], i_fc_list) - nr_inodes++; - -list_for_each_entry (fc_dentry, &sbi->s_fc_dentry_q[FC_Q_MAIN ], fcd_list) { -struct ext4_inode_info *ei; - -if (fc_dentry->fcd_op !=EXT4_FC_TAG_CREAT ) -continue ; -if (list_empty (&fc_dentry->fcd_dilist)) -continue ; - -/* See the comment in ext4_fc_commit_dentry_updates(). */ - ei =list_first_entry (&fc_dentry->fcd_dilist, - struct ext4_inode_info, i_fc_dilist); -if (!list_empty (&ei->i_fc_list)) -continue ; - - nr_inodes++; - } -ext4_fc_unlock (sb, alloc_ctx); - -if (!nr_inodes) +if (!inodes_size)return 0 ; - inodes =kvcalloc (nr_inodes,sizeof (*inodes),GFP_NOFS ); -if (!inodes) -return -ENOMEM ; - alloc_ctx =ext4_fc_lock (sb);list_for_each_entry (iter, &sbi->s_fc_q[FC_Q_MAIN ], i_fc_list) { +if (i >= inodes_size) { + ret = -E2BIG ; + goto unlock; + } inodes[i++] = &iter->vfs_inode; }
@@ -1229,6 +1243,10 @@ static int ext4_fc_snapshot_inodes(journal_t *journal)if (!list_empty (&ei->i_fc_list))continue ; +if (i >= inodes_size) { + ret = -E2BIG ; + goto unlock; + }/* * Create-only inodes may only be referenced via fcd_dilist and * not appear on s_fc_q[MAIN]. They may hit the last iput while
@@ -1240,15 +1258,22 @@ static int ext4_fc_snapshot_inodes(journal_t *journal)ext4_set_inode_state (inode,EXT4_STATE_FC_COMMITTING ); inodes[i++] = inode; } +unlock:ext4_fc_unlock (sb, alloc_ctx); -for (nr_inodes =0 ; nr_inodes < i; nr_inodes++) { - ret =ext4_fc_snapshot_inode (inodes[nr_inodes]); +if (ret) +return ret; + +for (idx =0 ; idx < i; idx++) { +unsigned int inode_ranges =0 ; + + ret =ext4_fc_snapshot_inode (inodes[idx], nr_ranges, + &inode_ranges);if (ret)break ; + nr_ranges += inode_ranges; } -kvfree (inodes);return ret; }
@@ -1259,6 +1284,8 @@ static int ext4_fc_perform_commit(journal_t *journal)struct ext4_inode_info *iter;struct ext4_fc_head head;struct inode *inode; +struct inode **inodes; +unsigned int inodes_size;struct blk_plug plug;int ret =0 ;u32 crc =0 ;
@@ -1311,6 +1338,10 @@ static int ext4_fc_perform_commit(journal_t *journal)return ret; + ret =ext4_fc_alloc_snapshot_inodes (sb, &inodes, &inodes_size); +if (ret) +return ret; +/* Step 4: Mark all inodes as being committed. */ jbd2_journal_lock_updates (journal);/*
@@ -1326,8 +1357,9 @@ static int ext4_fc_perform_commit(journal_t *journal) }ext4_fc_unlock (sb, alloc_ctx); - ret =ext4_fc_snapshot_inodes (journal); + ret =ext4_fc_snapshot_inodes (journal, inodes, inodes_size);jbd2_journal_unlock_updates (journal); +kvfree (inodes);if (ret)return ret;
@@ -1383,6 +1415,64 @@ static int ext4_fc_perform_commit(journal_t *journal)return ret; } +static unsigned int ext4_fc_count_snapshot_inodes (struct super_block *sb) +{ +struct ext4_sb_info *sbi =EXT4_SB (sb); +struct ext4_inode_info *iter; +struct ext4_fc_dentry_update *fc_dentry; +unsigned int nr_inodes =0 ; +int alloc_ctx; + + alloc_ctx =ext4_fc_lock (sb); +list_for_each_entry (iter, &sbi->s_fc_q[FC_Q_MAIN ], i_fc_list) + nr_inodes++; + +list_for_each_entry (fc_dentry, &sbi->s_fc_dentry_q[FC_Q_MAIN ], fcd_list) { +struct ext4_inode_info *ei; + +if (fc_dentry->fcd_op !=EXT4_FC_TAG_CREAT ) +continue ; +if (list_empty (&fc_dentry->fcd_dilist)) +continue ; + +/* See the comment in ext4_fc_commit_dentry_updates(). */ + ei =list_first_entry (&fc_dentry->fcd_dilist, + struct ext4_inode_info, i_fc_dilist); +if (!list_empty (&ei->i_fc_list)) +continue ; + + nr_inodes++; + } +ext4_fc_unlock (sb, alloc_ctx); + +return nr_inodes; +} + +static int ext4_fc_alloc_snapshot_inodes (struct super_block *sb, +struct inode ***inodesp, +unsigned int *nr_inodesp) +{ +unsigned int nr_inodes =ext4_fc_count_snapshot_inodes (sb); +struct inode **inodes; + + *inodesp =NULL ; + *nr_inodesp =0 ; + +if (!nr_inodes) +return 0 ; + +if (nr_inodes >EXT4_FC_SNAPSHOT_MAX_INODES ) +return -E2BIG ; + + inodes =kvcalloc (nr_inodes,sizeof (*inodes),GFP_NOFS ); +if (!inodes) +return -ENOMEM ; + + *inodesp = inodes; + *nr_inodesp = nr_inodes; +return 0 ; +} +static void ext4_fc_update_stats (struct super_block *sb,int status,u64 commit_time,int nblks,tid_t commit_tid) {
@@ -1475,7 +1565,10 @@ int ext4_fc_commit(journal_t *journal, tid_t commit_tid) fc_bufs_before = (sbi->s_fc_bytes + bsize -1 ) / bsize; ret =ext4_fc_perform_commit (journal);if (ret <0 ) { - status =EXT4_FC_STATUS_FAILED ; +if (ret == -EAGAIN || ret == -E2BIG || ret == -ECANCELED ) + status =EXT4_FC_STATUS_INELIGIBLE ; +else + status =EXT4_FC_STATUS_FAILED ; goto fallback; } nblks = (sbi->s_fc_bytes + bsize -1 ) / bsize - fc_bufs_before;
@@ -1559,34 +1652,35 @@ static void ext4_fc_cleanup(journal_t *journal, int full, tid_t tid)while (!list_empty (&sbi->s_fc_dentry_q[FC_Q_MAIN ])) { fc_dentry =list_first_entry (&sbi->s_fc_dentry_q[FC_Q_MAIN ], - struct ext4_fc_dentry_update, - fcd_list); + struct ext4_fc_dentry_update, + fcd_list);list_del_init (&fc_dentry->fcd_list);if (fc_dentry->fcd_op ==EXT4_FC_TAG_CREAT && - !list_empty (&fc_dentry->fcd_dilist)) { + !list_empty (&fc_dentry->fcd_dilist)) {/* See the comment in ext4_fc_commit_dentry_updates(). */ ei =list_first_entry (&fc_dentry->fcd_dilist, - struct ext4_inode_info, - i_fc_dilist); + struct ext4_inode_info, + i_fc_dilist);ext4_fc_free_inode_snap (&ei->vfs_inode);spin_lock (&ei->i_fc_lock);ext4_clear_inode_state (&ei->vfs_inode, -EXT4_STATE_FC_REQUEUE ); +EXT4_STATE_FC_REQUEUE );ext4_clear_inode_state (&ei->vfs_inode, -EXT4_STATE_FC_COMMITTING ); +EXT4_STATE_FC_COMMITTING );spin_unlock (&ei->i_fc_lock);/* * Make sure clearing of EXT4_STATE_FC_COMMITTING is -* visible before we send the wakeup. Pairs with implicit -* barrier in prepare_to_wait() in ext4_fc_del(). +* visible before we send the wakeup. Pairs with +* implicit barrier in prepare_to_wait() in +* ext4_fc_del(). */ smp_mb ();#if (BITS_PER_LONG <64 )wake_up_bit (&ei->i_state_flags, -EXT4_STATE_FC_COMMITTING ); +EXT4_STATE_FC_COMMITTING );#else wake_up_bit (&ei->i_flags, -EXT4_STATE_FC_COMMITTING ); +EXT4_STATE_FC_COMMITTING );#endif }list_del_init (&fc_dentry->fcd_dilist);
@@ -2582,13 +2676,20 @@ int __init ext4_fc_init_dentry_cache(void) ext4_fc_dentry_cachep =KMEM_CACHE (ext4_fc_dentry_update,SLAB_RECLAIM_ACCOUNT ); -if (ext4_fc_dentry_cachep ==NULL ) +if (!ext4_fc_dentry_cachep)return -ENOMEM ; + ext4_fc_range_cachep =KMEM_CACHE (ext4_fc_range,SLAB_RECLAIM_ACCOUNT ); +if (!ext4_fc_range_cachep) { +kmem_cache_destroy (ext4_fc_dentry_cachep); +return -ENOMEM ; + } +return 0 ; }void ext4_fc_destroy_dentry_cache (void ) { +kmem_cache_destroy (ext4_fc_range_cachep);kmem_cache_destroy (ext4_fc_dentry_cachep); }
Signature · 1 line
2.53.0
RFC v5 6/7 ext4: fast commit: add lock_updates tracepoint
In reply to Li ChenCommit-time fast commit snapshots run under jbd2_journal_lock_updates(), so it is useful to quantify the time spent with updates locked and to understand why snapshotting can fail.
Add a new tracepoint, ext4_fc_lock_updates, reporting the time spent in the updates-locked window along with the number of snapshotted inodes and ranges. Record the first snapshot failure reason in a stable snap_err field for tooling.
--- fs/ext4/ext4.h | 15 ++++++++ fs/ext4/fast_commit.c | 71 +++++++++++++++++++++++++++++-------- include/trace/events/ext4.h | 61 +++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 15 deletions(-)
fs/ext4/ext4.h +15 -0
@@ -1037,6 +1037,21 @@ enum {struct ext4_fc_inode_snap ; +/* +* Snapshot failure reasons for ext4_fc_lock_updates tracepoint. +* Keep these stable for tooling. +*/ +enum ext4_fc_snap_err { +EXT4_FC_SNAP_ERR_NONE =0 , +EXT4_FC_SNAP_ERR_ES_MISS =1 , +EXT4_FC_SNAP_ERR_ES_DELAYED =2 , +EXT4_FC_SNAP_ERR_ES_OTHER =3 , +EXT4_FC_SNAP_ERR_INODES_CAP =4 , +EXT4_FC_SNAP_ERR_RANGES_CAP =5 , +EXT4_FC_SNAP_ERR_NOMEM =6 , +EXT4_FC_SNAP_ERR_INODE_LOC =7 , +}; +/* * fourth extended file system inode data in memory */
fs/ext4/fast_commit.c +56 -15
203 lines not on this page · raw message
include/trace/events/ext4.h +61 -0
73 lines not on this page · raw message
Signature · 1 line
2.53.0
Steven Rostedt
maintainerRe: [RFC v5 6/7] ext4: fast commit: add lock_updates tracepoint
Quoted 27 lines · On Tue, 17 Mar 2026 16:46:21 +0800 Li Chen <[email protected]> wrote:
Commit-time fast commit snapshots run under
jbd2_journal_lock_updates(), so it is useful to quantify the time spent with updates locked and to understand why snapshotting can fail.Add a new tracepoint,
ext4_fc_lock_updates, reporting the time spent in the updates-locked window along with the number of snapshotted inodes and ranges. Record the first snapshot failure reason in a stablesnap_errfield for tooling.Signed-off-by Li Chen <[email protected]>--- fs/ext4/ext4.h | 15 ++++++++ fs/ext4/fast_commit.c | 71 +++++++++++++++++++++++++++++-------- include/trace/events/ext4.h | 61 +++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 15 deletions(-)fs/ext4/ext4.h +3
-0@@ -1037,6 +1037,21 @@ enum {struct ext4_fc_inode_snap ; +/* +* Snapshot failure reasons for ext4_fc_lock_updates tracepoint. +* Keep these stable for tooling.
fs/ext4/ext4.h enum { · line 1052
Quoted 10 lines
fs/ext4/ext4.h +10
-0+*/ +enum ext4_fc_snap_err { +EXT4_FC_SNAP_ERR_NONE =0 , +EXT4_FC_SNAP_ERR_ES_MISS =1 , +EXT4_FC_SNAP_ERR_ES_DELAYED =2 , +EXT4_FC_SNAP_ERR_ES_OTHER =3 , +EXT4_FC_SNAP_ERR_INODES_CAP =4 , +EXT4_FC_SNAP_ERR_RANGES_CAP =5 , +EXT4_FC_SNAP_ERR_NOMEM =6 , +EXT4_FC_SNAP_ERR_INODE_LOC =7 ,
You don't need to explicitly state the assignments, the enum will increment them without them.
Quoted 207 lines
fs/ext4/ext4.h +2
-0+}; +/* * fourth extended file system inode data in memory */ fs/ext4/fast_commit.c +50
-13@@ -193,6 +193,12 @@ static struct kmem_cache *ext4_fc_range_cachep;#define EXT4_FC_SNAPSHOT_MAX_INODES 1024#define EXT4_FC_SNAPSHOT_MAX_RANGES 2048 +static inline void ext4_fc_set_snap_err (int *snap_err,int err) +{ +if (snap_err && *snap_err ==EXT4_FC_SNAP_ERR_NONE ) + *snap_err = err; +} +static void ext4_end_buffer_io_sync (struct buffer_head *bh,int uptodate) {BUFFER_TRACE (bh,"" );@@ -983,11 +989,12 @@ static void ext4_fc_free_inode_snap(struct inode *inode)static int ext4_fc_snapshot_inode_data (struct inode *inode,struct list_head *ranges,unsigned int nr_ranges_total, -unsigned int *nr_rangesp) +unsigned int *nr_rangesp, +int *snap_err) {struct ext4_inode_info *ei =EXT4_I (inode); -unsigned int nr_ranges =0 ;ext4_lblk_t start_lblk, end_lblk, cur_lblk; +unsigned int nr_ranges =0 ;spin_lock (&ei->i_fc_lock);if (ei->i_fc_lblk_len ==0 ) {@@ -1010,11 +1017,16 @@ static int ext4_fc_snapshot_inode_data(struct inode *inode,struct ext4_fc_range *range;ext4_lblk_t len; -if (!ext4_es_lookup_extent (inode, cur_lblk,NULL , &es,NULL )) +if (!ext4_es_lookup_extent (inode, cur_lblk,NULL , &es,NULL )) { +ext4_fc_set_snap_err (snap_err,EXT4_FC_SNAP_ERR_ES_MISS );return -EAGAIN ; + } -if (ext4_es_is_delayed (&es)) +if (ext4_es_is_delayed (&es)) { +ext4_fc_set_snap_err (snap_err, +EXT4_FC_SNAP_ERR_ES_DELAYED );return -EAGAIN ; + } len = es.es_len - (cur_lblk - es.es_lblk);if (len > end_lblk - cur_lblk +1 )@@ -1024,12 +1036,17 @@ static int ext4_fc_snapshot_inode_data(struct inode *inode,continue ; } -if (nr_ranges_total + nr_ranges >=EXT4_FC_SNAPSHOT_MAX_RANGES ) +if (nr_ranges_total + nr_ranges >=EXT4_FC_SNAPSHOT_MAX_RANGES ) { +ext4_fc_set_snap_err (snap_err, +EXT4_FC_SNAP_ERR_RANGES_CAP );return -E2BIG ; + } range =kmem_cache_alloc (ext4_fc_range_cachep,GFP_NOFS ); -if (!range) +if (!range) { +ext4_fc_set_snap_err (snap_err,EXT4_FC_SNAP_ERR_NOMEM );return -ENOMEM ; + } nr_ranges++; range->lblk = cur_lblk;@@ -1054,6 +1071,7 @@ static int ext4_fc_snapshot_inode_data(struct inode *inode, range->len = max; }else {kmem_cache_free (ext4_fc_range_cachep, range); +ext4_fc_set_snap_err (snap_err,EXT4_FC_SNAP_ERR_ES_OTHER );return -EAGAIN ; }@@ -1070,7 +1088,7 @@ static int ext4_fc_snapshot_inode_data(struct inode *inode,static int ext4_fc_snapshot_inode (struct inode *inode,unsigned int nr_ranges_total, -unsigned int *nr_rangesp) +unsigned int *nr_rangesp,int *snap_err) {struct ext4_inode_info *ei =EXT4_I (inode);struct ext4_fc_inode_snap *snap;@@ -1082,8 +1100,10 @@ static int ext4_fc_snapshot_inode(struct inode *inode,int alloc_ctx; ret =ext4_get_inode_loc_noio (inode, &iloc); -if (ret) +if (ret) { +ext4_fc_set_snap_err (snap_err,EXT4_FC_SNAP_ERR_INODE_LOC );return ret; + }if (ext4_test_inode_flag (inode,EXT4_INODE_INLINE_DATA )) inode_len =EXT4_INODE_SIZE (inode->i_sb);@@ -1092,6 +1112,7 @@ static int ext4_fc_snapshot_inode(struct inode *inode, snap =kmalloc (struct_size (snap, inode_buf, inode_len),GFP_NOFS );if (!snap) { +ext4_fc_set_snap_err (snap_err,EXT4_FC_SNAP_ERR_NOMEM );brelse (iloc.bh);return -ENOMEM ; }@@ -1102,7 +1123,7 @@ static int ext4_fc_snapshot_inode(struct inode *inode,brelse (iloc.bh); ret =ext4_fc_snapshot_inode_data (inode, &ranges, nr_ranges_total, - &nr_ranges); + &nr_ranges, snap_err);if (ret) {kfree (snap);ext4_fc_free_ranges (&ranges);@@ -1203,7 +1224,10 @@ static int ext4_fc_alloc_snapshot_inodes(struct super_block *sb, unsigned int *nr_inodesp);static int ext4_fc_snapshot_inodes (journal_t *journal,struct inode **inodes, -unsigned int inodes_size) +unsigned int inodes_size, +unsigned int *nr_inodesp, +unsigned int *nr_rangesp, +int *snap_err) {struct super_block *sb = journal->j_private;struct ext4_sb_info *sbi =EXT4_SB (sb);@@ -1221,6 +1245,8 @@ static int ext4_fc_snapshot_inodes(journal_t *journal, struct inode **inodes, alloc_ctx =ext4_fc_lock (sb);list_for_each_entry (iter, &sbi->s_fc_q[FC_Q_MAIN ], i_fc_list) {if (i >= inodes_size) { +ext4_fc_set_snap_err (snap_err, +EXT4_FC_SNAP_ERR_INODES_CAP ); ret = -E2BIG ; goto unlock; }@@ -1244,6 +1270,8 @@ static int ext4_fc_snapshot_inodes(journal_t *journal, struct inode **inodes,continue ;if (i >= inodes_size) { +ext4_fc_set_snap_err (snap_err, +EXT4_FC_SNAP_ERR_INODES_CAP ); ret = -E2BIG ; goto unlock; }@@ -1268,16 +1296,20 @@ static int ext4_fc_snapshot_inodes(journal_t *journal, struct inode **inodes,unsigned int inode_ranges =0 ; ret =ext4_fc_snapshot_inode (inodes[idx], nr_ranges, - &inode_ranges); + &inode_ranges, snap_err);if (ret)break ; nr_ranges += inode_ranges; } +if (nr_inodesp) + *nr_inodesp = i; +if (nr_rangesp) + *nr_rangesp = nr_ranges;return ret; } -static int ext4_fc_perform_commit (journal_t *journal) +static int ext4_fc_perform_commit (journal_t *journal,tid_t commit_tid) {struct super_block *sb = journal->j_private;struct ext4_sb_info *sbi =EXT4_SB (sb);@@ -1286,10 +1318,15 @@ static int ext4_fc_perform_commit(journal_t *journal)struct inode *inode;struct inode **inodes;unsigned int inodes_size; +unsigned int snap_inodes =0 ; +unsigned int snap_ranges =0 ; +int snap_err =EXT4_FC_SNAP_ERR_NONE ;struct blk_plug plug;int ret =0 ;u32 crc =0 ;int alloc_ctx; +ktime_t lock_start; +u64 locked_ns;/* * Step 1: Mark all inodes on s_fc_q[MAIN] with @@ -1337,13 +1374,13 @@ static int ext4_fc_perform_commit(journal_t *journal)if (ret)return ret; - ret =ext4_fc_alloc_snapshot_inodes (sb, &inodes, &inodes_size);if (ret)return ret;/* Step 4: Mark all inodes as being committed. */ jbd2_journal_lock_updates (journal); + lock_start =ktime_get ();/* * The journal is now locked. No more handles can start and all the * previous handles are now drained. Snapshotting happens in this
fs/ext4/fast_commit.c static int ext4_fc_perform_commit(journal_t *journal) · line 1400
Quoted 8 lines
fs/ext4/fast_commit.c +3
-1@@ -1357,8 +1394,12 @@ static int ext4_fc_perform_commit(journal_t *journal) }ext4_fc_unlock (sb, alloc_ctx); - ret =ext4_fc_snapshot_inodes (journal, inodes, inodes_size); + ret =ext4_fc_snapshot_inodes (journal, inodes, inodes_size, + &snap_inodes, &snap_ranges, &snap_err);jbd2_journal_unlock_updates (journal); + locked_ns =ktime_to_ns (ktime_sub (ktime_get (), lock_start));
If locked_ns is only used for the tracepoint, it should either be calculated in the tracepoint, or add:
if (trace_ext4_fc_lock_updates_enabled()) {
locked_ns = ktime_to_ns(ktime_sub(ktime_get(), lock_start));fs/ext4/fast_commit.c static int ext4_fc_perform_commit(journal_t *journal) · line 1402
Quoted 2 lines
fs/ext4/fast_commit.c +2
-0+trace_ext4_fc_lock_updates (sb, commit_tid, locked_ns, snap_inodes, + snap_ranges, ret, snap_err);
}
Note, we are going to also add a code to call the tracepoint directly, to remove the double static_branch.
https://lore.kernel.org/all/[email protected]/
But that code is still being worked on so you don't need to worry about it at the moment.
-- Steve
Quoted 91 lines
fs/ext4/fast_commit.c +1
-1kvfree (inodes);if (ret)return ret;@@ -1563,7 +1604,7 @@ int ext4_fc_commit(journal_t *journal, tid_t commit_tid) journal_ioprio =EXT4_DEF_JOURNAL_IOPRIO ;set_task_ioprio (current, journal_ioprio); fc_bufs_before = (sbi->s_fc_bytes + bsize -1 ) / bsize; - ret =ext4_fc_perform_commit (journal); + ret =ext4_fc_perform_commit (journal, commit_tid);if (ret <0 ) {if (ret == -EAGAIN || ret == -E2BIG || ret == -ECANCELED ) status =EXT4_FC_STATUS_INELIGIBLE ;include/trace/events/ext4.h +61
-0@@ -104,6 +104,26 @@ TRACE_DEFINE_ENUM(EXT4_FC_REASON_INODE_JOURNAL_DATA);TRACE_DEFINE_ENUM (EXT4_FC_REASON_ENCRYPTED_FILENAME );TRACE_DEFINE_ENUM (EXT4_FC_REASON_MAX ); +#undef EM +#undef EMe +#define EM (a) TRACE_DEFINE_ENUM(EXT4_FC_SNAP_ERR_##a); +#define EMe (a) TRACE_DEFINE_ENUM(EXT4_FC_SNAP_ERR_##a); + +#define TRACE_SNAP_ERR \ + EM(NONE) \ + EM(ES_MISS) \ + EM(ES_DELAYED) \ + EM(ES_OTHER) \ + EM(INODES_CAP) \ + EM(RANGES_CAP) \ + EM(NOMEM) \ + EMe(INODE_LOC) + +TRACE_SNAP_ERR + +#undef EM +#undef EMe +#define show_fc_reason (reason) \ __print_symbolic(reason, \ { EXT4_FC_REASON_XATTR, "XATTR"}, \@@ -2812,6 +2832,47 @@ TRACE_EVENT(ext4_fc_commit_stop, __entry->num_fc_ineligible, __entry->nblks_agg, __entry->tid) ); +#define EM (a) { EXT4_FC_SNAP_ERR_##a, #a }, +#define EMe (a) { EXT4_FC_SNAP_ERR_##a, #a } + +TRACE_EVENT (ext4_fc_lock_updates, + TP_PROTO(struct super_block *sb, tid_t commit_tid, u64 locked_ns, + unsigned int nr_inodes, unsigned int nr_ranges, int err, + int snap_err), + +TP_ARGS (sb, commit_tid, locked_ns, nr_inodes, nr_ranges, err, snap_err), + +TP_STRUCT__entry (/* entry */ + __field(dev_t, dev) + __field(tid_t, tid) + __field(u64, locked_ns) + __field(unsigned int, nr_inodes) + __field(unsigned int, nr_ranges) + __field(int, err) +__field (int, snap_err) + ), + +TP_fast_assign (/* assign */ + __entry->dev = sb->s_dev; + __entry->tid = commit_tid; + __entry->locked_ns = locked_ns; + __entry->nr_inodes = nr_inodes; + __entry->nr_ranges = nr_ranges; + __entry->err = err; + __entry->snap_err = snap_err; + ), + +TP_printk ("dev %d,%d tid %u locked_ns %llu nr_inodes %u nr_ranges %u err %d snap_err %s" , +MAJOR (__entry->dev),MINOR (__entry->dev), __entry->tid, + __entry->locked_ns, __entry->nr_inodes, __entry->nr_ranges, + __entry->err,__print_symbolic (__entry->snap_err, +TRACE_SNAP_ERR )) +); + +#undef EM +#undef EMe +#undef TRACE_SNAP_ERR +#define FC_REASON_NAME_STAT (reason) \ show_fc_reason(reason), \ __entry->fc_ineligible_rc[reason]
Re: [RFC v5 6/7] ext4: fast commit: add lock_updates tracepoint
Hi Steven,
Thank you for your review, and I apologize for my delayed response.
---- On Wed, 18 Mar 2026 00:21:29 +0800 Steven Rostedt <[email protected]> wrote ---
Quoted 27 lines
Quoted 24 lines · On Tue, 17 Mar 2026 16:46:21 +0800 Li Chen <[email protected]> wrote:
Commit-time fast commit snapshots run under
jbd2_journal_lock_updates(), so it is useful to quantify the time spent with updates locked and to understand why snapshotting can fail.Add a new tracepoint,
ext4_fc_lock_updates, reporting the time spent in the updates-locked window along with the number of snapshotted inodes and ranges. Record the first snapshot failure reason in a stablesnap_errfield for tooling.Signed-off-by Li Chen <[email protected]>--- fs/ext4/ext4.h | 15 ++++++++ fs/ext4/fast_commit.c | 71 +++++++++++++++++++++++++++++-------- include/trace/events/ext4.h | 61 +++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 15 deletions(-)fs/ext4/ext4.h +0
-0@@ -1037,6 +1037,21 @@ enum {struct ext4_fc_inode_snap ;
Quoted 16 lines
fs/ext4/ext4.h enum { · line 1052
Quoted 13 lines
fs/ext4/ext4.h +13
-0+/* +* Snapshot failure reasons for ext4_fc_lock_updates tracepoint. +* Keep these stable for tooling. +*/ +enum ext4_fc_snap_err { +EXT4_FC_SNAP_ERR_NONE =0 , +EXT4_FC_SNAP_ERR_ES_MISS =1 , +EXT4_FC_SNAP_ERR_ES_DELAYED =2 , +EXT4_FC_SNAP_ERR_ES_OTHER =3 , +EXT4_FC_SNAP_ERR_INODES_CAP =4 , +EXT4_FC_SNAP_ERR_RANGES_CAP =5 , +EXT4_FC_SNAP_ERR_NOMEM =6 , +EXT4_FC_SNAP_ERR_INODE_LOC =7 ,You don't need to explicitly state the assignments, the enum will increment them without them.
Agree.
Quoted 219 lines
Quoted 207 lines
fs/ext4/ext4.h +2
-0+}; +/* * fourth extended file system inode data in memory */ fs/ext4/fast_commit.c +50
-13@@ -193,6 +193,12 @@ static struct kmem_cache *ext4_fc_range_cachep;#define EXT4_FC_SNAPSHOT_MAX_INODES 1024#define EXT4_FC_SNAPSHOT_MAX_RANGES 2048 +static inline void ext4_fc_set_snap_err (int *snap_err,int err) +{ +if (snap_err && *snap_err ==EXT4_FC_SNAP_ERR_NONE ) + *snap_err = err; +} +static void ext4_end_buffer_io_sync (struct buffer_head *bh,int uptodate) {BUFFER_TRACE (bh,"" );@@ -983,11 +989,12 @@ static void ext4_fc_free_inode_snap(struct inode *inode)static int ext4_fc_snapshot_inode_data (struct inode *inode,struct list_head *ranges,unsigned int nr_ranges_total, -unsigned int *nr_rangesp) +unsigned int *nr_rangesp, +int *snap_err) {struct ext4_inode_info *ei =EXT4_I (inode); -unsigned int nr_ranges =0 ;ext4_lblk_t start_lblk, end_lblk, cur_lblk; +unsigned int nr_ranges =0 ;spin_lock (&ei->i_fc_lock);if (ei->i_fc_lblk_len ==0 ) {@@ -1010,11 +1017,16 @@ static int ext4_fc_snapshot_inode_data(struct inode *inode,struct ext4_fc_range *range;ext4_lblk_t len; -if (!ext4_es_lookup_extent (inode, cur_lblk,NULL , &es,NULL )) +if (!ext4_es_lookup_extent (inode, cur_lblk,NULL , &es,NULL )) { +ext4_fc_set_snap_err (snap_err,EXT4_FC_SNAP_ERR_ES_MISS );return -EAGAIN ; + } -if (ext4_es_is_delayed (&es)) +if (ext4_es_is_delayed (&es)) { +ext4_fc_set_snap_err (snap_err, +EXT4_FC_SNAP_ERR_ES_DELAYED );return -EAGAIN ; + } len = es.es_len - (cur_lblk - es.es_lblk);if (len > end_lblk - cur_lblk +1 )@@ -1024,12 +1036,17 @@ static int ext4_fc_snapshot_inode_data(struct inode *inode,continue ; } -if (nr_ranges_total + nr_ranges >=EXT4_FC_SNAPSHOT_MAX_RANGES ) +if (nr_ranges_total + nr_ranges >=EXT4_FC_SNAPSHOT_MAX_RANGES ) { +ext4_fc_set_snap_err (snap_err, +EXT4_FC_SNAP_ERR_RANGES_CAP );return -E2BIG ; + } range =kmem_cache_alloc (ext4_fc_range_cachep,GFP_NOFS ); -if (!range) +if (!range) { +ext4_fc_set_snap_err (snap_err,EXT4_FC_SNAP_ERR_NOMEM );return -ENOMEM ; + } nr_ranges++; range->lblk = cur_lblk;@@ -1054,6 +1071,7 @@ static int ext4_fc_snapshot_inode_data(struct inode *inode, range->len = max; }else {kmem_cache_free (ext4_fc_range_cachep, range); +ext4_fc_set_snap_err (snap_err,EXT4_FC_SNAP_ERR_ES_OTHER );return -EAGAIN ; }@@ -1070,7 +1088,7 @@ static int ext4_fc_snapshot_inode_data(struct inode *inode,static int ext4_fc_snapshot_inode (struct inode *inode,unsigned int nr_ranges_total, -unsigned int *nr_rangesp) +unsigned int *nr_rangesp,int *snap_err) {struct ext4_inode_info *ei =EXT4_I (inode);struct ext4_fc_inode_snap *snap;@@ -1082,8 +1100,10 @@ static int ext4_fc_snapshot_inode(struct inode *inode,int alloc_ctx; ret =ext4_get_inode_loc_noio (inode, &iloc); -if (ret) +if (ret) { +ext4_fc_set_snap_err (snap_err,EXT4_FC_SNAP_ERR_INODE_LOC );return ret; + }if (ext4_test_inode_flag (inode,EXT4_INODE_INLINE_DATA )) inode_len =EXT4_INODE_SIZE (inode->i_sb);@@ -1092,6 +1112,7 @@ static int ext4_fc_snapshot_inode(struct inode *inode, snap =kmalloc (struct_size (snap, inode_buf, inode_len),GFP_NOFS );if (!snap) { +ext4_fc_set_snap_err (snap_err,EXT4_FC_SNAP_ERR_NOMEM );brelse (iloc.bh);return -ENOMEM ; }@@ -1102,7 +1123,7 @@ static int ext4_fc_snapshot_inode(struct inode *inode,brelse (iloc.bh); ret =ext4_fc_snapshot_inode_data (inode, &ranges, nr_ranges_total, - &nr_ranges); + &nr_ranges, snap_err);if (ret) {kfree (snap);ext4_fc_free_ranges (&ranges);@@ -1203,7 +1224,10 @@ static int ext4_fc_alloc_snapshot_inodes(struct super_block *sb, unsigned int *nr_inodesp);static int ext4_fc_snapshot_inodes (journal_t *journal,struct inode **inodes, -unsigned int inodes_size) +unsigned int inodes_size, +unsigned int *nr_inodesp, +unsigned int *nr_rangesp, +int *snap_err) {struct super_block *sb = journal->j_private;struct ext4_sb_info *sbi =EXT4_SB (sb);@@ -1221,6 +1245,8 @@ static int ext4_fc_snapshot_inodes(journal_t *journal, struct inode **inodes, alloc_ctx =ext4_fc_lock (sb);list_for_each_entry (iter, &sbi->s_fc_q[FC_Q_MAIN ], i_fc_list) {if (i >= inodes_size) { +ext4_fc_set_snap_err (snap_err, +EXT4_FC_SNAP_ERR_INODES_CAP ); ret = -E2BIG ; goto unlock; }@@ -1244,6 +1270,8 @@ static int ext4_fc_snapshot_inodes(journal_t *journal, struct inode **inodes,continue ;if (i >= inodes_size) { +ext4_fc_set_snap_err (snap_err, +EXT4_FC_SNAP_ERR_INODES_CAP ); ret = -E2BIG ; goto unlock; }@@ -1268,16 +1296,20 @@ static int ext4_fc_snapshot_inodes(journal_t *journal, struct inode **inodes,unsigned int inode_ranges =0 ; ret =ext4_fc_snapshot_inode (inodes[idx], nr_ranges, - &inode_ranges); + &inode_ranges, snap_err);if (ret)break ; nr_ranges += inode_ranges; } +if (nr_inodesp) + *nr_inodesp = i; +if (nr_rangesp) + *nr_rangesp = nr_ranges;return ret; } -static int ext4_fc_perform_commit (journal_t *journal) +static int ext4_fc_perform_commit (journal_t *journal,tid_t commit_tid) {struct super_block *sb = journal->j_private;struct ext4_sb_info *sbi =EXT4_SB (sb);@@ -1286,10 +1318,15 @@ static int ext4_fc_perform_commit(journal_t *journal)struct inode *inode;struct inode **inodes;unsigned int inodes_size; +unsigned int snap_inodes =0 ; +unsigned int snap_ranges =0 ; +int snap_err =EXT4_FC_SNAP_ERR_NONE ;struct blk_plug plug;int ret =0 ;u32 crc =0 ;int alloc_ctx; +ktime_t lock_start; +u64 locked_ns;/* * Step 1: Mark all inodes on s_fc_q[MAIN] with @@ -1337,13 +1374,13 @@ static int ext4_fc_perform_commit(journal_t *journal)if (ret)return ret; - ret =ext4_fc_alloc_snapshot_inodes (sb, &inodes, &inodes_size);if (ret)return ret;/* Step 4: Mark all inodes as being committed. */ jbd2_journal_lock_updates (journal); + lock_start =ktime_get ();/* * The journal is now locked. No more handles can start and all the * previous handles are now drained. Snapshotting happens in this fs/ext4/fast_commit.c static int ext4_fc_perform_commit(journal_t *journal) · line 1400
Quoted 8 lines
fs/ext4/fast_commit.c +3
-1@@ -1357,8 +1394,12 @@ static int ext4_fc_perform_commit(journal_t *journal) }ext4_fc_unlock (sb, alloc_ctx); - ret =ext4_fc_snapshot_inodes (journal, inodes, inodes_size); + ret =ext4_fc_snapshot_inodes (journal, inodes, inodes_size, + &snap_inodes, &snap_ranges, &snap_err);jbd2_journal_unlock_updates (journal); + locked_ns =ktime_to_ns (ktime_sub (ktime_get (), lock_start));If
locked_nsis only used for the tracepoint, it should either be calculated in the tracepoint, or add:
Quoted 2 lines
if (trace_ext4_fc_lock_updates_enabled()) {
locked_ns = ktime_to_ns(ktime_sub(ktime_get(), lock_start));Good catch!
Quoted 5 lines
fs/ext4/fast_commit.c static int ext4_fc_perform_commit(journal_t *journal) · line 1402
Quoted 2 lines
fs/ext4/fast_commit.c +2
-0+trace_ext4_fc_lock_updates (sb, commit_tid, locked_ns, snap_inodes, + snap_ranges, ret, snap_err);}
Quoted 7 lines
Note, we are going to also add a code to call the tracepoint directly, to remove the double
static_branch.https://lore.kernel.org/all/[email protected]/But that code is still being worked on so you don't need to worry about it at the moment.
Thank you! I will monitor this patch set and incorporate it into future versions if possible.
Quoted 84 lines
Quoted 84 lines
fs/ext4/fast_commit.c +1
-1kvfree (inodes);if (ret)return ret;@@ -1563,7 +1604,7 @@ int ext4_fc_commit(journal_t *journal, tid_t commit_tid) journal_ioprio =EXT4_DEF_JOURNAL_IOPRIO ;set_task_ioprio (current, journal_ioprio); fc_bufs_before = (sbi->s_fc_bytes + bsize -1 ) / bsize; - ret =ext4_fc_perform_commit (journal); + ret =ext4_fc_perform_commit (journal, commit_tid);if (ret <0 ) {if (ret == -EAGAIN || ret == -E2BIG || ret == -ECANCELED ) status =EXT4_FC_STATUS_INELIGIBLE ;include/trace/events/ext4.h +56
-0@@ -104,6 +104,26 @@ TRACE_DEFINE_ENUM(EXT4_FC_REASON_INODE_JOURNAL_DATA);TRACE_DEFINE_ENUM (EXT4_FC_REASON_ENCRYPTED_FILENAME );TRACE_DEFINE_ENUM (EXT4_FC_REASON_MAX ); +#undef EM +#undef EMe +#define EM (a) TRACE_DEFINE_ENUM(EXT4_FC_SNAP_ERR_##a); +#define EMe (a) TRACE_DEFINE_ENUM(EXT4_FC_SNAP_ERR_##a); + +#define TRACE_SNAP_ERR \ + EM(NONE) \ + EM(ES_MISS) \ + EM(ES_DELAYED) \ + EM(ES_OTHER) \ + EM(INODES_CAP) \ + EM(RANGES_CAP) \ + EM(NOMEM) \ + EMe(INODE_LOC) + +TRACE_SNAP_ERR + +#undef EM +#undef EMe +#define show_fc_reason (reason) \ __print_symbolic(reason, \ { EXT4_FC_REASON_XATTR, "XATTR"}, \@@ -2812,6 +2832,47 @@ TRACE_EVENT(ext4_fc_commit_stop, __entry->num_fc_ineligible, __entry->nblks_agg, __entry->tid) ); +#define EM (a) { EXT4_FC_SNAP_ERR_##a, #a }, +#define EMe (a) { EXT4_FC_SNAP_ERR_##a, #a } + +TRACE_EVENT (ext4_fc_lock_updates, + TP_PROTO(struct super_block *sb, tid_t commit_tid, u64 locked_ns, + unsigned int nr_inodes, unsigned int nr_ranges, int err, + int snap_err), + +TP_ARGS (sb, commit_tid, locked_ns, nr_inodes, nr_ranges, err, snap_err), + +TP_STRUCT__entry (/* entry */ + __field(dev_t, dev) + __field(tid_t, tid) + __field(u64, locked_ns) + __field(unsigned int, nr_inodes) + __field(unsigned int, nr_ranges) + __field(int, err) +__field (int, snap_err) + ), + +TP_fast_assign (/* assign */ + __entry->dev = sb->s_dev; + __entry->tid = commit_tid; + __entry->locked_ns = locked_ns; + __entry->nr_inodes = nr_inodes; + __entry->nr_ranges = nr_ranges; + __entry->err = err; + __entry->snap_err = snap_err; + ), + +TP_printk ("dev %d,%d tid %u locked_ns %llu nr_inodes %u nr_ranges %u err %d snap_err %s" , +MAJOR (__entry->dev),MINOR (__entry->dev), __entry->tid, + __entry->locked_ns, __entry->nr_inodes, __entry->nr_ranges, + __entry->err,__print_symbolic (__entry->snap_err, +TRACE_SNAP_ERR )) +);
Quoted 10 lines
include/trace/events/ext4.h TRACE_EVENT(ext4_fc_commit_stop, · line 2878
Quoted 8 lines
include/trace/events/ext4.h +5
-0+ +#undef EM +#undef EMe +#undef TRACE_SNAP_ERR +#define FC_REASON_NAME_STAT (reason) \ show_fc_reason(reason), \ __entry->fc_ineligible_rc[reason]
Regards,
Li
RFC v5 7/7 ext4: fast commit: export snapshot stats in fc_info
In reply to Li ChenSnapshot-based fast commit can fall back when the commit-time snapshot cannot be built (e.g. extent status cache misses). It is useful to quantify the updates-locked window and to see why snapshotting failed.
Add best-effort snapshot counters to the ext4 superblock and extend /proc/fs/ext4/<sb_id>/fc_info to report the number of snapshotted inodes and ranges, snapshot failure reasons, and the average/max time spent with journal updates locked.
--- fs/ext4/ext4.h | 31 ++++++++++++++++++++++ fs/ext4/fast_commit.c | 61 ++++++++++++++++++++++++++++++++++++++++--- fs/ext4/super.c | 1 + 3 files changed, 89 insertions(+), 4 deletions(-)
fs/ext4/ext4.h +31 -0
@@ -1566,6 +1566,36 @@ struct ext4_orphan_info {* file blocks */ }; +/* +* Ext4 fast commit snapshot statistics. +* +* These are best-effort counters intended for debugging / performance +* introspection; they are not exact under concurrent updates. +*/ +struct ext4_fc_snap_stats { +u64 lock_updates_ns_total; +u64 lock_updates_ns_max; +u64 lock_updates_samples; + +u64 snap_inodes; +u64 snap_ranges; + +u64 snap_fail_es_miss; +u64 snap_fail_es_delayed; +u64 snap_fail_es_other; + +u64 snap_fail_inodes_cap; +u64 snap_fail_ranges_cap; +u64 snap_fail_nomem; +u64 snap_fail_inode_loc; + +/* +* Missing inode snapshots during log writing should never happen. +* Keep this counter to help catch unexpected regressions. +*/ +u64 snap_fail_no_snap; +}; +/* * fourth extended-fs super-block data in memory */
@@ -1837,6 +1867,7 @@ struct ext4_sb_info {struct mutex s_fc_lock;struct buffer_head *s_fc_bh;struct ext4_fc_stats s_fc_stats; +struct ext4_fc_snap_stats s_fc_snap_stats;tid_t s_fc_ineligible_tid;#ifdef CONFIG_EXT4_DEBUG int s_fc_debug_max_replay;
fs/ext4/fast_commit.c +57 -4
186 lines not on this page · raw message
fs/ext4/super.c +1 -0
@@ -4500,6 +4500,7 @@ static void ext4_fast_commit_init(struct super_block *sb) sbi->s_fc_ineligible_tid =0 ;mutex_init (&sbi->s_fc_lock);memset (&sbi->s_fc_stats,0 ,sizeof (sbi->s_fc_stats)); +memset (&sbi->s_fc_snap_stats,0 ,sizeof (sbi->s_fc_snap_stats)); sbi->s_fc_replay_state.fc_regions =NULL ; sbi->s_fc_replay_state.fc_regions_size =0 ; sbi->s_fc_replay_state.fc_regions_used =0 ;
Signature · 1 line
2.53.0