Commit 6ca738d60c563d5c6cf6253ee4b8e76fa77b2b9e

Authored by Derek Basehore
Committed by Linus Torvalds
1 parent a0c32761e7

backing_dev: fix hung task on sync

bdi_wakeup_thread_delayed() used the mod_delayed_work() function to
schedule work to writeback dirty inodes.  The problem with this is that
it can delay work that is scheduled for immediate execution, such as the
work from sync_inodes_sb().  This can happen since mod_delayed_work()
can now steal work from a work_queue.  This fixes the problem by using
queue_delayed_work() instead.  This is a regression caused by commit
839a8e8660b6 ("writeback: replace custom worker pool implementation with
unbound workqueue").

The reason that this causes a problem is that laptop-mode will change
the delay, dirty_writeback_centisecs, to 60000 (10 minutes) by default.
In the case that bdi_wakeup_thread_delayed() races with
sync_inodes_sb(), sync will be stopped for 10 minutes and trigger a hung
task.  Even if dirty_writeback_centisecs is not long enough to cause a
hung task, we still don't want to delay sync for that long.

We fix the problem by using queue_delayed_work() when we want to
schedule writeback sometime in future.  This function doesn't change the
timer if it is already armed.

For the same reason, we also change bdi_writeback_workfn() to
immediately queue the work again in the case that the work_list is not
empty.  The same problem can happen if the sync work is run on the
rescue worker.

[jack@suse.cz: update changelog, add comment, use bdi_wakeup_thread_delayed()]
Signed-off-by: Derek Basehore <dbasehore@chromium.org>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Alexander Viro <viro@zento.linux.org.uk>
Reviewed-by: Tejun Heo <tj@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Darrick J. Wong" <darrick.wong@oracle.com>
Cc: Derek Basehore <dbasehore@chromium.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: Benson Leung <bleung@chromium.org>
Cc: Sonny Rao <sonnyrao@chromium.org>
Cc: Luigi Semenzato <semenzato@chromium.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Dave Chinner <david@fromorbit.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 2 changed files with 8 additions and 5 deletions Side-by-side Diff

... ... @@ -1039,10 +1039,10 @@
1039 1039 trace_writeback_pages_written(pages_written);
1040 1040 }
1041 1041  
1042   - if (!list_empty(&bdi->work_list) ||
1043   - (wb_has_dirty_io(wb) && dirty_writeback_interval))
1044   - queue_delayed_work(bdi_wq, &wb->dwork,
1045   - msecs_to_jiffies(dirty_writeback_interval * 10));
  1042 + if (!list_empty(&bdi->work_list))
  1043 + mod_delayed_work(bdi_wq, &wb->dwork, 0);
  1044 + else if (wb_has_dirty_io(wb) && dirty_writeback_interval)
  1045 + bdi_wakeup_thread_delayed(bdi);
1046 1046  
1047 1047 current->flags &= ~PF_SWAPWRITE;
1048 1048 }
... ... @@ -288,13 +288,16 @@
288 288 * Note, we wouldn't bother setting up the timer, but this function is on the
289 289 * fast-path (used by '__mark_inode_dirty()'), so we save few context switches
290 290 * by delaying the wake-up.
  291 + *
  292 + * We have to be careful not to postpone flush work if it is scheduled for
  293 + * earlier. Thus we use queue_delayed_work().
291 294 */
292 295 void bdi_wakeup_thread_delayed(struct backing_dev_info *bdi)
293 296 {
294 297 unsigned long timeout;
295 298  
296 299 timeout = msecs_to_jiffies(dirty_writeback_interval * 10);
297   - mod_delayed_work(bdi_wq, &bdi->wb.dwork, timeout);
  300 + queue_delayed_work(bdi_wq, &bdi->wb.dwork, timeout);
298 301 }
299 302  
300 303 /*