Skip to content

Commit 169ebd9

Browse files
jankaraFengguang Wu
authored andcommitted
writeback: Avoid iput() from flusher thread
Doing iput() from flusher thread (writeback_sb_inodes()) can create problems because iput() can do a lot of work - for example truncate the inode if it's the last iput on unlinked file. Some filesystems depend on flusher thread progressing (e.g. because they need to flush delay allocated blocks to reduce allocation uncertainty) and so flusher thread doing truncate creates interesting dependencies and possibilities for deadlocks. We get rid of iput() in flusher thread by using the fact that I_SYNC inode flag effectively pins the inode in memory. So if we take care to either hold i_lock or have I_SYNC set, we can get away without taking inode reference in writeback_sb_inodes(). As a side effect of these changes, we also fix possible use-after-free in wb_writeback() because inode_wait_for_writeback() call could try to reacquire i_lock on the inode that was already free. Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Fengguang Wu <fengguang.wu@intel.com>
1 parent dbd5768 commit 169ebd9

4 files changed

Lines changed: 65 additions & 23 deletions

File tree

‎fs/fs-writeback.c‎

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,12 @@ static int write_inode(struct inode *inode, struct writeback_control *wbc)
326326
}
327327

328328
/*
329-
* Wait for writeback on an inode to complete.
329+
* Wait for writeback on an inode to complete. Called with i_lock held.
330+
* Caller must make sure inode cannot go away when we drop i_lock.
330331
*/
331-
static void inode_wait_for_writeback(struct inode *inode)
332+
static void __inode_wait_for_writeback(struct inode *inode)
333+
__releases(inode->i_lock)
334+
__acquires(inode->i_lock)
332335
{
333336
DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
334337
wait_queue_head_t *wqh;
@@ -341,6 +344,36 @@ static void inode_wait_for_writeback(struct inode *inode)
341344
}
342345
}
343346

347+
/*
348+
* Wait for writeback on an inode to complete. Caller must have inode pinned.
349+
*/
350+
void inode_wait_for_writeback(struct inode *inode)
351+
{
352+
spin_lock(&inode->i_lock);
353+
__inode_wait_for_writeback(inode);
354+
spin_unlock(&inode->i_lock);
355+
}
356+
357+
/*
358+
* Sleep until I_SYNC is cleared. This function must be called with i_lock
359+
* held and drops it. It is aimed for callers not holding any inode reference
360+
* so once i_lock is dropped, inode can go away.
361+
*/
362+
static void inode_sleep_on_writeback(struct inode *inode)
363+
__releases(inode->i_lock)
364+
{
365+
DEFINE_WAIT(wait);
366+
wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
367+
int sleep;
368+
369+
prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
370+
sleep = inode->i_state & I_SYNC;
371+
spin_unlock(&inode->i_lock);
372+
if (sleep)
373+
schedule();
374+
finish_wait(wqh, &wait);
375+
}
376+
344377
/*
345378
* Find proper writeback list for the inode depending on its current state and
346379
* possibly also change of its state while we were doing writeback. Here we
@@ -479,9 +512,11 @@ writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
479512
if (wbc->sync_mode != WB_SYNC_ALL)
480513
goto out;
481514
/*
482-
* It's a data-integrity sync. We must wait.
515+
* It's a data-integrity sync. We must wait. Since callers hold
516+
* inode reference or inode has I_WILL_FREE set, it cannot go
517+
* away under us.
483518
*/
484-
inode_wait_for_writeback(inode);
519+
__inode_wait_for_writeback(inode);
485520
}
486521
WARN_ON(inode->i_state & I_SYNC);
487522
/*
@@ -620,20 +655,28 @@ static long writeback_sb_inodes(struct super_block *sb,
620655
}
621656
spin_unlock(&wb->list_lock);
622657

623-
__iget(inode);
624658
/*
625659
* We already requeued the inode if it had I_SYNC set and we
626660
* are doing WB_SYNC_NONE writeback. So this catches only the
627661
* WB_SYNC_ALL case.
628662
*/
629-
if (inode->i_state & I_SYNC)
630-
inode_wait_for_writeback(inode);
663+
if (inode->i_state & I_SYNC) {
664+
/* Wait for I_SYNC. This function drops i_lock... */
665+
inode_sleep_on_writeback(inode);
666+
/* Inode may be gone, start again */
667+
continue;
668+
}
631669
inode->i_state |= I_SYNC;
632670
spin_unlock(&inode->i_lock);
671+
633672
write_chunk = writeback_chunk_size(wb->bdi, work);
634673
wbc.nr_to_write = write_chunk;
635674
wbc.pages_skipped = 0;
636675

676+
/*
677+
* We use I_SYNC to pin the inode in memory. While it is set
678+
* evict_inode() will wait so the inode cannot be freed.
679+
*/
637680
__writeback_single_inode(inode, wb, &wbc);
638681

639682
work->nr_pages -= write_chunk - wbc.nr_to_write;
@@ -645,10 +688,7 @@ static long writeback_sb_inodes(struct super_block *sb,
645688
requeue_inode(inode, wb, &wbc);
646689
inode_sync_complete(inode);
647690
spin_unlock(&inode->i_lock);
648-
spin_unlock(&wb->list_lock);
649-
iput(inode);
650-
cond_resched();
651-
spin_lock(&wb->list_lock);
691+
cond_resched_lock(&wb->list_lock);
652692
/*
653693
* bail out to wb_writeback() often enough to check
654694
* background threshold and other termination conditions.
@@ -843,8 +883,8 @@ static long wb_writeback(struct bdi_writeback *wb,
843883
inode = wb_inode(wb->b_more_io.prev);
844884
spin_lock(&inode->i_lock);
845885
spin_unlock(&wb->list_lock);
846-
inode_wait_for_writeback(inode);
847-
spin_unlock(&inode->i_lock);
886+
/* This function drops i_lock... */
887+
inode_sleep_on_writeback(inode);
848888
spin_lock(&wb->list_lock);
849889
}
850890
}

‎fs/inode.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,13 @@ static void evict(struct inode *inode)
530530

531531
inode_sb_list_del(inode);
532532

533-
inode_sync_wait(inode);
533+
/*
534+
* Wait for flusher thread to be done with the inode so that filesystem
535+
* does not start destroying it while writeback is still running. Since
536+
* the inode has I_FREEING set, flusher thread won't start new work on
537+
* the inode. We just have to wait for running writeback to finish.
538+
*/
539+
inode_wait_for_writeback(inode);
534540

535541
if (op->evict_inode) {
536542
op->evict_inode(inode);

‎include/linux/fs.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,9 +1753,10 @@ struct super_operations {
17531753
* anew. Other functions will just ignore such inodes,
17541754
* if appropriate. I_NEW is used for waiting.
17551755
*
1756-
* I_SYNC Synchonized write of dirty inode data. The bits is
1757-
* set during data writeback, and cleared with a wakeup
1758-
* on the bit address once it is done.
1756+
* I_SYNC Writeback of inode is running. The bit is set during
1757+
* data writeback, and cleared with a wakeup on the bit
1758+
* address once it is done. The bit is also used to pin
1759+
* the inode in memory for flusher thread.
17591760
*
17601761
* I_REFERENCED Marks the inode as recently references on the LRU list.
17611762
*

‎include/linux/writeback.h‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,14 @@ long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
9595
enum wb_reason reason);
9696
long wb_do_writeback(struct bdi_writeback *wb, int force_wait);
9797
void wakeup_flusher_threads(long nr_pages, enum wb_reason reason);
98+
void inode_wait_for_writeback(struct inode *inode);
9899

99100
/* writeback.h requires fs.h; it, too, is not included from here. */
100101
static inline void wait_on_inode(struct inode *inode)
101102
{
102103
might_sleep();
103104
wait_on_bit(&inode->i_state, __I_NEW, inode_wait, TASK_UNINTERRUPTIBLE);
104105
}
105-
static inline void inode_sync_wait(struct inode *inode)
106-
{
107-
might_sleep();
108-
wait_on_bit(&inode->i_state, __I_SYNC, inode_wait,
109-
TASK_UNINTERRUPTIBLE);
110-
}
111106

112107

113108
/*

0 commit comments

Comments
 (0)