Skip to content

Commit 7ef3ff2

Browse files
konistorvalds
authored andcommitted
nilfs2: fix deadlock of segment constructor over I_SYNC flag
Nilfs2 eventually hangs in a stress test with fsstress program. This issue was caused by the following deadlock over I_SYNC flag between nilfs_segctor_thread() and writeback_sb_inodes(): nilfs_segctor_thread() nilfs_segctor_thread_construct() nilfs_segctor_unlock() nilfs_dispose_list() iput() iput_final() evict() inode_wait_for_writeback() * wait for I_SYNC flag writeback_sb_inodes() * set I_SYNC flag on inode->i_state __writeback_single_inode() do_writepages() nilfs_writepages() nilfs_construct_dsync_segment() nilfs_segctor_sync() * wait for completion of segment constructor inode_sync_complete() * clear I_SYNC flag after __writeback_single_inode() completed writeback_sb_inodes() calls do_writepages() for dirty inodes after setting I_SYNC flag on inode->i_state. do_writepages() in turn calls nilfs_writepages(), which can run segment constructor and wait for its completion. On the other hand, segment constructor calls iput(), which can call evict() and wait for the I_SYNC flag on inode_wait_for_writeback(). Since segment constructor doesn't know when I_SYNC will be set, it cannot know whether iput() will block or not unless inode->i_nlink has a non-zero count. We can prevent evict() from being called in iput() by implementing sop->drop_inode(), but it's not preferable to leave inodes with i_nlink == 0 for long periods because it even defers file truncation and inode deallocation. So, this instead resolves the deadlock by calling iput() asynchronously with a workqueue for inodes with i_nlink == 0. Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp> Cc: Al Viro <viro@zeniv.linux.org.uk> Tested-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 81cca6f commit 7ef3ff2

3 files changed

Lines changed: 44 additions & 7 deletions

File tree

‎fs/nilfs2/nilfs.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ enum {
141141
* @ti_save: Backup of journal_info field of task_struct
142142
* @ti_flags: Flags
143143
* @ti_count: Nest level
144-
* @ti_garbage: List of inode to be put when releasing semaphore
145144
*/
146145
struct nilfs_transaction_info {
147146
u32 ti_magic;
@@ -150,7 +149,6 @@ struct nilfs_transaction_info {
150149
one of other filesystems has a bug. */
151150
unsigned short ti_flags;
152151
unsigned short ti_count;
153-
struct list_head ti_garbage;
154152
};
155153

156154
/* ti_magic */

‎fs/nilfs2/segment.c‎

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ static void nilfs_transaction_lock(struct super_block *sb,
305305
ti->ti_count = 0;
306306
ti->ti_save = cur_ti;
307307
ti->ti_magic = NILFS_TI_MAGIC;
308-
INIT_LIST_HEAD(&ti->ti_garbage);
309308
current->journal_info = ti;
310309

311310
for (;;) {
@@ -332,8 +331,6 @@ static void nilfs_transaction_unlock(struct super_block *sb)
332331

333332
up_write(&nilfs->ns_segctor_sem);
334333
current->journal_info = ti->ti_save;
335-
if (!list_empty(&ti->ti_garbage))
336-
nilfs_dispose_list(nilfs, &ti->ti_garbage, 0);
337334
}
338335

339336
static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci,
@@ -746,6 +743,15 @@ static void nilfs_dispose_list(struct the_nilfs *nilfs,
746743
}
747744
}
748745

746+
static void nilfs_iput_work_func(struct work_struct *work)
747+
{
748+
struct nilfs_sc_info *sci = container_of(work, struct nilfs_sc_info,
749+
sc_iput_work);
750+
struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
751+
752+
nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 0);
753+
}
754+
749755
static int nilfs_test_metadata_dirty(struct the_nilfs *nilfs,
750756
struct nilfs_root *root)
751757
{
@@ -1900,8 +1906,8 @@ static int nilfs_segctor_collect_dirty_files(struct nilfs_sc_info *sci,
19001906
static void nilfs_segctor_drop_written_files(struct nilfs_sc_info *sci,
19011907
struct the_nilfs *nilfs)
19021908
{
1903-
struct nilfs_transaction_info *ti = current->journal_info;
19041909
struct nilfs_inode_info *ii, *n;
1910+
int defer_iput = false;
19051911

19061912
spin_lock(&nilfs->ns_inode_lock);
19071913
list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
@@ -1912,9 +1918,24 @@ static void nilfs_segctor_drop_written_files(struct nilfs_sc_info *sci,
19121918
clear_bit(NILFS_I_BUSY, &ii->i_state);
19131919
brelse(ii->i_bh);
19141920
ii->i_bh = NULL;
1915-
list_move_tail(&ii->i_dirty, &ti->ti_garbage);
1921+
list_del_init(&ii->i_dirty);
1922+
if (!ii->vfs_inode.i_nlink) {
1923+
/*
1924+
* Defer calling iput() to avoid a deadlock
1925+
* over I_SYNC flag for inodes with i_nlink == 0
1926+
*/
1927+
list_add_tail(&ii->i_dirty, &sci->sc_iput_queue);
1928+
defer_iput = true;
1929+
} else {
1930+
spin_unlock(&nilfs->ns_inode_lock);
1931+
iput(&ii->vfs_inode);
1932+
spin_lock(&nilfs->ns_inode_lock);
1933+
}
19161934
}
19171935
spin_unlock(&nilfs->ns_inode_lock);
1936+
1937+
if (defer_iput)
1938+
schedule_work(&sci->sc_iput_work);
19181939
}
19191940

19201941
/*
@@ -2583,6 +2604,8 @@ static struct nilfs_sc_info *nilfs_segctor_new(struct super_block *sb,
25832604
INIT_LIST_HEAD(&sci->sc_segbufs);
25842605
INIT_LIST_HEAD(&sci->sc_write_logs);
25852606
INIT_LIST_HEAD(&sci->sc_gc_inodes);
2607+
INIT_LIST_HEAD(&sci->sc_iput_queue);
2608+
INIT_WORK(&sci->sc_iput_work, nilfs_iput_work_func);
25862609
init_timer(&sci->sc_timer);
25872610

25882611
sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
@@ -2609,6 +2632,8 @@ static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
26092632
ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
26102633
nilfs_transaction_unlock(sci->sc_super);
26112634

2635+
flush_work(&sci->sc_iput_work);
2636+
26122637
} while (ret && retrycount-- > 0);
26132638
}
26142639

@@ -2633,6 +2658,9 @@ static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
26332658
|| sci->sc_seq_request != sci->sc_seq_done);
26342659
spin_unlock(&sci->sc_state_lock);
26352660

2661+
if (flush_work(&sci->sc_iput_work))
2662+
flag = true;
2663+
26362664
if (flag || !nilfs_segctor_confirm(sci))
26372665
nilfs_segctor_write_out(sci);
26382666

@@ -2642,6 +2670,12 @@ static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
26422670
nilfs_dispose_list(nilfs, &sci->sc_dirty_files, 1);
26432671
}
26442672

2673+
if (!list_empty(&sci->sc_iput_queue)) {
2674+
nilfs_warning(sci->sc_super, __func__,
2675+
"iput queue is not empty\n");
2676+
nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 1);
2677+
}
2678+
26452679
WARN_ON(!list_empty(&sci->sc_segbufs));
26462680
WARN_ON(!list_empty(&sci->sc_write_logs));
26472681

‎fs/nilfs2/segment.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/types.h>
2727
#include <linux/fs.h>
2828
#include <linux/buffer_head.h>
29+
#include <linux/workqueue.h>
2930
#include <linux/nilfs2_fs.h>
3031
#include "nilfs.h"
3132

@@ -92,6 +93,8 @@ struct nilfs_segsum_pointer {
9293
* @sc_nblk_inc: Block count of current generation
9394
* @sc_dirty_files: List of files to be written
9495
* @sc_gc_inodes: List of GC inodes having blocks to be written
96+
* @sc_iput_queue: list of inodes for which iput should be done
97+
* @sc_iput_work: work struct to defer iput call
9598
* @sc_freesegs: array of segment numbers to be freed
9699
* @sc_nfreesegs: number of segments on @sc_freesegs
97100
* @sc_dsync_inode: inode whose data pages are written for a sync operation
@@ -135,6 +138,8 @@ struct nilfs_sc_info {
135138

136139
struct list_head sc_dirty_files;
137140
struct list_head sc_gc_inodes;
141+
struct list_head sc_iput_queue;
142+
struct work_struct sc_iput_work;
138143

139144
__u64 *sc_freesegs;
140145
size_t sc_nfreesegs;

0 commit comments

Comments
 (0)