Skip to content

Commit 05ae7cf

Browse files
jankaragregkh
authored andcommitted
writeback: Fix sync livelock due to b_dirty_time processing
commit f9cae92 upstream. When we are processing writeback for sync(2), move_expired_inodes() didn't set any inode expiry value (older_than_this). This can result in writeback never completing if there's steady stream of inodes added to b_dirty_time list as writeback rechecks dirty lists after each writeback round whether there's more work to be done. Fix the problem by using sync(2) start time is inode expiry value when processing b_dirty_time list similarly as for ordinarily dirtied inodes. This requires some refactoring of older_than_this handling which simplifies the code noticeably as a bonus. Fixes: 0ae45f6 ("vfs: add support for a lazytime mount option") CC: stable@vger.kernel.org Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d74c235 commit 05ae7cf

2 files changed

Lines changed: 23 additions & 34 deletions

File tree

‎fs/fs-writeback.c‎

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
struct wb_writeback_work {
4343
long nr_pages;
4444
struct super_block *sb;
45-
unsigned long *older_than_this;
4645
enum writeback_sync_modes sync_mode;
4746
unsigned int tagged_writepages:1;
4847
unsigned int for_kupdate:1;
@@ -1234,33 +1233,23 @@ static bool inode_dirtied_after(struct inode *inode, unsigned long t)
12341233
#define EXPIRE_DIRTY_ATIME 0x0001
12351234

12361235
/*
1237-
* Move expired (dirtied before work->older_than_this) dirty inodes from
1236+
* Move expired (dirtied before dirtied_before) dirty inodes from
12381237
* @delaying_queue to @dispatch_queue.
12391238
*/
12401239
static int move_expired_inodes(struct list_head *delaying_queue,
12411240
struct list_head *dispatch_queue,
1242-
int flags,
1243-
struct wb_writeback_work *work)
1241+
int flags, unsigned long dirtied_before)
12441242
{
1245-
unsigned long *older_than_this = NULL;
1246-
unsigned long expire_time;
12471243
LIST_HEAD(tmp);
12481244
struct list_head *pos, *node;
12491245
struct super_block *sb = NULL;
12501246
struct inode *inode;
12511247
int do_sb_sort = 0;
12521248
int moved = 0;
12531249

1254-
if ((flags & EXPIRE_DIRTY_ATIME) == 0)
1255-
older_than_this = work->older_than_this;
1256-
else if (!work->for_sync) {
1257-
expire_time = jiffies - (dirtytime_expire_interval * HZ);
1258-
older_than_this = &expire_time;
1259-
}
12601250
while (!list_empty(delaying_queue)) {
12611251
inode = wb_inode(delaying_queue->prev);
1262-
if (older_than_this &&
1263-
inode_dirtied_after(inode, *older_than_this))
1252+
if (inode_dirtied_after(inode, dirtied_before))
12641253
break;
12651254
list_move(&inode->i_io_list, &tmp);
12661255
moved++;
@@ -1306,18 +1295,22 @@ static int move_expired_inodes(struct list_head *delaying_queue,
13061295
* |
13071296
* +--> dequeue for IO
13081297
*/
1309-
static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
1298+
static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work,
1299+
unsigned long dirtied_before)
13101300
{
13111301
int moved;
1302+
unsigned long time_expire_jif = dirtied_before;
13121303

13131304
assert_spin_locked(&wb->list_lock);
13141305
list_splice_init(&wb->b_more_io, &wb->b_io);
1315-
moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work);
1306+
moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, dirtied_before);
1307+
if (!work->for_sync)
1308+
time_expire_jif = jiffies - dirtytime_expire_interval * HZ;
13161309
moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io,
1317-
EXPIRE_DIRTY_ATIME, work);
1310+
EXPIRE_DIRTY_ATIME, time_expire_jif);
13181311
if (moved)
13191312
wb_io_lists_populated(wb);
1320-
trace_writeback_queue_io(wb, work, moved);
1313+
trace_writeback_queue_io(wb, work, dirtied_before, moved);
13211314
}
13221315

13231316
static int write_inode(struct inode *inode, struct writeback_control *wbc)
@@ -1829,7 +1822,7 @@ static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
18291822
blk_start_plug(&plug);
18301823
spin_lock(&wb->list_lock);
18311824
if (list_empty(&wb->b_io))
1832-
queue_io(wb, &work);
1825+
queue_io(wb, &work, jiffies);
18331826
__writeback_inodes_wb(wb, &work);
18341827
spin_unlock(&wb->list_lock);
18351828
blk_finish_plug(&plug);
@@ -1849,22 +1842,19 @@ static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
18491842
* takes longer than a dirty_writeback_interval interval, then leave a
18501843
* one-second gap.
18511844
*
1852-
* older_than_this takes precedence over nr_to_write. So we'll only write back
1845+
* dirtied_before takes precedence over nr_to_write. So we'll only write back
18531846
* all dirty pages if they are all attached to "old" mappings.
18541847
*/
18551848
static long wb_writeback(struct bdi_writeback *wb,
18561849
struct wb_writeback_work *work)
18571850
{
18581851
unsigned long wb_start = jiffies;
18591852
long nr_pages = work->nr_pages;
1860-
unsigned long oldest_jif;
1853+
unsigned long dirtied_before = jiffies;
18611854
struct inode *inode;
18621855
long progress;
18631856
struct blk_plug plug;
18641857

1865-
oldest_jif = jiffies;
1866-
work->older_than_this = &oldest_jif;
1867-
18681858
blk_start_plug(&plug);
18691859
spin_lock(&wb->list_lock);
18701860
for (;;) {
@@ -1898,14 +1888,14 @@ static long wb_writeback(struct bdi_writeback *wb,
18981888
* safe.
18991889
*/
19001890
if (work->for_kupdate) {
1901-
oldest_jif = jiffies -
1891+
dirtied_before = jiffies -
19021892
msecs_to_jiffies(dirty_expire_interval * 10);
19031893
} else if (work->for_background)
1904-
oldest_jif = jiffies;
1894+
dirtied_before = jiffies;
19051895

19061896
trace_writeback_start(wb, work);
19071897
if (list_empty(&wb->b_io))
1908-
queue_io(wb, work);
1898+
queue_io(wb, work, dirtied_before);
19091899
if (work->sb)
19101900
progress = writeback_sb_inodes(work->sb, wb, work);
19111901
else

‎include/trace/events/writeback.h‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,9 @@ DEFINE_WBC_EVENT(wbc_writepage);
498498
TRACE_EVENT(writeback_queue_io,
499499
TP_PROTO(struct bdi_writeback *wb,
500500
struct wb_writeback_work *work,
501+
unsigned long dirtied_before,
501502
int moved),
502-
TP_ARGS(wb, work, moved),
503+
TP_ARGS(wb, work, dirtied_before, moved),
503504
TP_STRUCT__entry(
504505
__array(char, name, 32)
505506
__field(unsigned long, older)
@@ -509,19 +510,17 @@ TRACE_EVENT(writeback_queue_io,
509510
__field(ino_t, cgroup_ino)
510511
),
511512
TP_fast_assign(
512-
unsigned long *older_than_this = work->older_than_this;
513513
strscpy_pad(__entry->name, bdi_dev_name(wb->bdi), 32);
514-
__entry->older = older_than_this ? *older_than_this : 0;
515-
__entry->age = older_than_this ?
516-
(jiffies - *older_than_this) * 1000 / HZ : -1;
514+
__entry->older = dirtied_before;
515+
__entry->age = (jiffies - dirtied_before) * 1000 / HZ;
517516
__entry->moved = moved;
518517
__entry->reason = work->reason;
519518
__entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
520519
),
521520
TP_printk("bdi %s: older=%lu age=%ld enqueue=%d reason=%s cgroup_ino=%lu",
522521
__entry->name,
523-
__entry->older, /* older_than_this in jiffies */
524-
__entry->age, /* older_than_this in relative milliseconds */
522+
__entry->older, /* dirtied_before in jiffies */
523+
__entry->age, /* dirtied_before in relative milliseconds */
525524
__entry->moved,
526525
__print_symbolic(__entry->reason, WB_WORK_REASON),
527526
(unsigned long)__entry->cgroup_ino

0 commit comments

Comments
 (0)