Commit aaf25593 authored by Tejun Heo's avatar Tejun Heo Committed by Jens Axboe

writeback, cgroup: fix use of the wrong bdi_writeback which mismatches the inode

When cgroup writeback is in use, there can be multiple wb's
(bdi_writeback's) per bdi and an inode may switch among them
dynamically.  In a couple places, the wrong wb was used leading to
performing operations on the wrong list under the wrong lock
corrupting the io lists.

* writeback_single_inode() was taking @wb parameter and used it to
  remove the inode from io lists if it becomes clean after writeback.
  The callers of this function were always passing in the root wb
  regardless of the actual wb that the inode was associated with,
  which could also change while writeback is in progress.

  Fix it by dropping the @wb parameter and using
  inode_to_wb_and_lock_list() to determine and lock the associated wb.

* After writeback_sb_inodes() writes out an inode, it re-locks @wb and
  inode to remove it from or move it to the right io list.  It assumes
  that the inode is still associated with @wb; however, the inode may
  have switched to another wb while writeback was in progress.

  Fix it by using inode_to_wb_and_lock_list() to determine and lock
  the associated wb after writeback is complete.  As the function
  requires the original @wb->list_lock locked for the next iteration,
  in the unlikely case where the inode has changed association, switch
  the locks.

Kudos to Tahsin for pinpointing these subtle breakages.
Signed-off-by: default avatarTejun Heo <>
Fixes: d10c8095 ("writeback: implement foreign cgroup inode bdi_writeback switching")
Link: default avatarTahsin Erdogan <>
Tested-by: default avatarTahsin Erdogan <>
Cc: # v4.2+
Signed-off-by: default avatarJens Axboe <>
parent 614a4e37
......@@ -1339,10 +1339,10 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
* we go e.g. from filesystem. Flusher thread uses __writeback_single_inode()
* and does more profound writeback list handling in writeback_sb_inodes().
static int
writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
struct writeback_control *wbc)
static int writeback_single_inode(struct inode *inode,
struct writeback_control *wbc)
struct bdi_writeback *wb;
int ret = 0;
......@@ -1380,7 +1380,8 @@ writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
ret = __writeback_single_inode(inode, wbc);
wb = inode_to_wb_and_lock_list(inode);
* If inode is clean, remove it from writeback lists. Otherwise don't
......@@ -1455,6 +1456,7 @@ static long writeback_sb_inodes(struct super_block *sb,
while (!list_empty(&wb->b_io)) {
struct inode *inode = wb_inode(wb->b_io.prev);
struct bdi_writeback *tmp_wb;
if (inode->i_sb != sb) {
if (work->sb) {
......@@ -1545,15 +1547,23 @@ static long writeback_sb_inodes(struct super_block *sb,
* Requeue @inode if still dirty. Be careful as @inode may
* have been switched to another wb in the meantime.
tmp_wb = inode_to_wb_and_lock_list(inode);
if (!(inode->i_state & I_DIRTY_ALL))
requeue_inode(inode, wb, &wbc);
requeue_inode(inode, tmp_wb, &wbc);
if (unlikely(tmp_wb != wb)) {
* bail out to wb_writeback() often enough to check
* background threshold and other termination conditions.
......@@ -2340,7 +2350,6 @@ EXPORT_SYMBOL(sync_inodes_sb);
int write_inode_now(struct inode *inode, int sync)
struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
struct writeback_control wbc = {
.nr_to_write = LONG_MAX,
.sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
......@@ -2352,7 +2361,7 @@ int write_inode_now(struct inode *inode, int sync)
wbc.nr_to_write = 0;
return writeback_single_inode(inode, wb, &wbc);
return writeback_single_inode(inode, &wbc);
......@@ -2369,7 +2378,7 @@ EXPORT_SYMBOL(write_inode_now);
int sync_inode(struct inode *inode, struct writeback_control *wbc)
return writeback_single_inode(inode, &inode_to_bdi(inode)->wb, wbc);
return writeback_single_inode(inode, wbc);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment