Skip to content

Commit 5afced3

Browse files
committed
writeback: Avoid skipping inode writeback
Inode's i_io_list list head is used to attach inode to several different lists - wb->{b_dirty, b_dirty_time, b_io, b_more_io}. When flush worker prepares a list of inodes to writeback e.g. for sync(2), it moves inodes to b_io list. Thus it is critical for sync(2) data integrity guarantees that inode is not requeued to any other writeback list when inode is queued for processing by flush worker. That's the reason why writeback_single_inode() does not touch i_io_list (unless the inode is completely clean) and why __mark_inode_dirty() does not touch i_io_list if I_SYNC flag is set. However there are two flaws in the current logic: 1) When inode has only I_DIRTY_TIME set but it is already queued in b_io list due to sync(2), concurrent __mark_inode_dirty(inode, I_DIRTY_SYNC) can still move inode back to b_dirty list resulting in skipping writeback of inode time stamps during sync(2). 2) When inode is on b_dirty_time list and writeback_single_inode() races with __mark_inode_dirty() like: writeback_single_inode() __mark_inode_dirty(inode, I_DIRTY_PAGES) inode->i_state |= I_SYNC __writeback_single_inode() inode->i_state |= I_DIRTY_PAGES; if (inode->i_state & I_SYNC) bail if (!(inode->i_state & I_DIRTY_ALL)) - not true so nothing done We end up with I_DIRTY_PAGES inode on b_dirty_time list and thus standard background writeback will not writeback this inode leading to possible dirty throttling stalls etc. (thanks to Martijn Coenen for this analysis). Fix these problems by tracking whether inode is queued in b_io or b_more_io lists in a new I_SYNC_QUEUED flag. When this flag is set, we know flush worker has queued inode and we should not touch i_io_list. On the other hand we also know that once flush worker is done with the inode it will requeue the inode to appropriate dirty list. When I_SYNC_QUEUED is not set, __mark_inode_dirty() can (and must) move inode to appropriate dirty list. Reported-by: Martijn Coenen <maco@android.com> Reviewed-by: Martijn Coenen <maco@android.com> Tested-by: Martijn Coenen <maco@android.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Fixes: 0ae45f6 ("vfs: add support for a lazytime mount option") CC: stable@vger.kernel.org Signed-off-by: Jan Kara <jack@suse.cz>
1 parent b35250c commit 5afced3

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

‎fs/fs-writeback.c‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ static void inode_io_list_del_locked(struct inode *inode,
146146
assert_spin_locked(&wb->list_lock);
147147
assert_spin_locked(&inode->i_lock);
148148

149+
inode->i_state &= ~I_SYNC_QUEUED;
149150
list_del_init(&inode->i_io_list);
150151
wb_io_lists_depopulated(wb);
151152
}
@@ -1187,6 +1188,7 @@ static void redirty_tail_locked(struct inode *inode, struct bdi_writeback *wb)
11871188
inode->dirtied_when = jiffies;
11881189
}
11891190
inode_io_list_move_locked(inode, wb, &wb->b_dirty);
1191+
inode->i_state &= ~I_SYNC_QUEUED;
11901192
}
11911193

11921194
static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
@@ -1262,8 +1264,11 @@ static int move_expired_inodes(struct list_head *delaying_queue,
12621264
break;
12631265
list_move(&inode->i_io_list, &tmp);
12641266
moved++;
1267+
spin_lock(&inode->i_lock);
12651268
if (flags & EXPIRE_DIRTY_ATIME)
1266-
set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state);
1269+
inode->i_state |= I_DIRTY_TIME_EXPIRED;
1270+
inode->i_state |= I_SYNC_QUEUED;
1271+
spin_unlock(&inode->i_lock);
12671272
if (sb_is_blkdev_sb(inode->i_sb))
12681273
continue;
12691274
if (sb && sb != inode->i_sb)
@@ -1438,6 +1443,7 @@ static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
14381443
} else if (inode->i_state & I_DIRTY_TIME) {
14391444
inode->dirtied_when = jiffies;
14401445
inode_io_list_move_locked(inode, wb, &wb->b_dirty_time);
1446+
inode->i_state &= ~I_SYNC_QUEUED;
14411447
} else {
14421448
/* The inode is clean. Remove from writeback lists. */
14431449
inode_io_list_del_locked(inode, wb);
@@ -2301,11 +2307,12 @@ void __mark_inode_dirty(struct inode *inode, int flags)
23012307
inode->i_state |= flags;
23022308

23032309
/*
2304-
* If the inode is being synced, just update its dirty state.
2305-
* The unlocker will place the inode on the appropriate
2306-
* superblock list, based upon its state.
2310+
* If the inode is queued for writeback by flush worker, just
2311+
* update its dirty state. Once the flush worker is done with
2312+
* the inode it will place it on the appropriate superblock
2313+
* list, based upon its state.
23072314
*/
2308-
if (inode->i_state & I_SYNC)
2315+
if (inode->i_state & I_SYNC_QUEUED)
23092316
goto out_unlock_inode;
23102317

23112318
/*

‎include/linux/fs.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,10 @@ static inline void kiocb_clone(struct kiocb *kiocb, struct kiocb *kiocb_src,
21572157
*
21582158
* I_DONTCACHE Evict inode as soon as it is not used anymore.
21592159
*
2160+
* I_SYNC_QUEUED Inode is queued in b_io or b_more_io writeback lists.
2161+
* Used to detect that mark_inode_dirty() should not move
2162+
* inode between dirty lists.
2163+
*
21602164
* Q: What is the difference between I_WILL_FREE and I_FREEING?
21612165
*/
21622166
#define I_DIRTY_SYNC (1 << 0)
@@ -2174,12 +2178,12 @@ static inline void kiocb_clone(struct kiocb *kiocb, struct kiocb *kiocb_src,
21742178
#define I_DIO_WAKEUP (1 << __I_DIO_WAKEUP)
21752179
#define I_LINKABLE (1 << 10)
21762180
#define I_DIRTY_TIME (1 << 11)
2177-
#define __I_DIRTY_TIME_EXPIRED 12
2178-
#define I_DIRTY_TIME_EXPIRED (1 << __I_DIRTY_TIME_EXPIRED)
2181+
#define I_DIRTY_TIME_EXPIRED (1 << 12)
21792182
#define I_WB_SWITCH (1 << 13)
21802183
#define I_OVL_INUSE (1 << 14)
21812184
#define I_CREATING (1 << 15)
21822185
#define I_DONTCACHE (1 << 16)
2186+
#define I_SYNC_QUEUED (1 << 17)
21832187

21842188
#define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
21852189
#define I_DIRTY (I_DIRTY_INODE | I_DIRTY_PAGES)

0 commit comments

Comments
 (0)