Skip to content

Commit 2254a73

Browse files
Darrick J. WongDave Chinner
authored andcommitted
xfs: fix xfs_inodegc_stop racing with mod_delayed_work
syzbot reported this warning from the faux inodegc shrinker that tries to kick off inodegc work: ------------[ cut here ]------------ WARNING: CPU: 1 PID: 102 at kernel/workqueue.c:1445 __queue_work+0xd44/0x1120 kernel/workqueue.c:1444 RIP: 0010:__queue_work+0xd44/0x1120 kernel/workqueue.c:1444 Call Trace: __queue_delayed_work+0x1c8/0x270 kernel/workqueue.c:1672 mod_delayed_work_on+0xe1/0x220 kernel/workqueue.c:1746 xfs_inodegc_shrinker_scan fs/xfs/xfs_icache.c:2212 [inline] xfs_inodegc_shrinker_scan+0x250/0x4f0 fs/xfs/xfs_icache.c:2191 do_shrink_slab+0x428/0xaa0 mm/vmscan.c:853 shrink_slab+0x175/0x660 mm/vmscan.c:1013 shrink_one+0x502/0x810 mm/vmscan.c:5343 shrink_many mm/vmscan.c:5394 [inline] lru_gen_shrink_node mm/vmscan.c:5511 [inline] shrink_node+0x2064/0x35f0 mm/vmscan.c:6459 kswapd_shrink_node mm/vmscan.c:7262 [inline] balance_pgdat+0xa02/0x1ac0 mm/vmscan.c:7452 kswapd+0x677/0xd60 mm/vmscan.c:7712 kthread+0x2e8/0x3a0 kernel/kthread.c:376 ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:308 This warning corresponds to this code in __queue_work: /* * For a draining wq, only works from the same workqueue are * allowed. The __WQ_DESTROYING helps to spot the issue that * queues a new work item to a wq after destroy_workqueue(wq). */ if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && WARN_ON_ONCE(!is_chained_work(wq)))) return; For this to trip, we must have a thread draining the inodedgc workqueue and a second thread trying to queue inodegc work to that workqueue. This can happen if freezing or a ro remount race with reclaim poking our faux inodegc shrinker and another thread dropping an unlinked O_RDONLY file: Thread 0 Thread 1 Thread 2 xfs_inodegc_stop xfs_inodegc_shrinker_scan xfs_is_inodegc_enabled <yes, will continue> xfs_clear_inodegc_enabled xfs_inodegc_queue_all <list empty, do not queue inodegc worker> xfs_inodegc_queue <add to list> xfs_is_inodegc_enabled <no, returns> drain_workqueue <set WQ_DRAINING> llist_empty <no, will queue list> mod_delayed_work_on(..., 0) __queue_work <sees WQ_DRAINING, kaboom> In other words, everything between the access to inodegc_enabled state and the decision to poke the inodegc workqueue requires some kind of coordination to avoid the WQ_DRAINING state. We could perhaps introduce a lock here, but we could also try to eliminate WQ_DRAINING from the picture. We could replace the drain_workqueue call with a loop that flushes the workqueue and queues workers as long as there is at least one inode present in the per-cpu inodegc llists. We've disabled inodegc at this point, so we know that the number of queued inodes will eventually hit zero as long as xfs_inodegc_start cannot reactivate the workers. There are four callers of xfs_inodegc_start. Three of them come from the VFS with s_umount held: filesystem thawing, failed filesystem freezing, and the rw remount transition. The fourth caller is mounting rw (no remount or freezing possible). There are three callers ofs xfs_inodegc_stop. One is unmounting (no remount or thaw possible). Two of them come from the VFS with s_umount held: fs freezing and ro remount transition. Hence, it is correct to replace the drain_workqueue call with a loop that drains the inodegc llists. Fixes: 6191cf3 ("xfs: flush inodegc workqueue tasks before cancel") Signed-off-by: Darrick J. Wong <[email protected]> Reviewed-by: Dave Chinner <[email protected]> Signed-off-by: Dave Chinner <[email protected]>
1 parent 2d5f38a commit 2254a73

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

fs/xfs/xfs_icache.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,18 +435,23 @@ xfs_iget_check_free_state(
435435
}
436436

437437
/* Make all pending inactivation work start immediately. */
438-
static void
438+
static bool
439439
xfs_inodegc_queue_all(
440440
struct xfs_mount *mp)
441441
{
442442
struct xfs_inodegc *gc;
443443
int cpu;
444+
bool ret = false;
444445

445446
for_each_online_cpu(cpu) {
446447
gc = per_cpu_ptr(mp->m_inodegc, cpu);
447-
if (!llist_empty(&gc->list))
448+
if (!llist_empty(&gc->list)) {
448449
mod_delayed_work_on(cpu, mp->m_inodegc_wq, &gc->work, 0);
450+
ret = true;
451+
}
449452
}
453+
454+
return ret;
450455
}
451456

452457
/*
@@ -1911,24 +1916,41 @@ xfs_inodegc_flush(
19111916

19121917
/*
19131918
* Flush all the pending work and then disable the inode inactivation background
1914-
* workers and wait for them to stop.
1919+
* workers and wait for them to stop. Caller must hold sb->s_umount to
1920+
* coordinate changes in the inodegc_enabled state.
19151921
*/
19161922
void
19171923
xfs_inodegc_stop(
19181924
struct xfs_mount *mp)
19191925
{
1926+
bool rerun;
1927+
19201928
if (!xfs_clear_inodegc_enabled(mp))
19211929
return;
19221930

1931+
/*
1932+
* Drain all pending inodegc work, including inodes that could be
1933+
* queued by racing xfs_inodegc_queue or xfs_inodegc_shrinker_scan
1934+
* threads that sample the inodegc state just prior to us clearing it.
1935+
* The inodegc flag state prevents new threads from queuing more
1936+
* inodes, so we queue pending work items and flush the workqueue until
1937+
* all inodegc lists are empty. IOWs, we cannot use drain_workqueue
1938+
* here because it does not allow other unserialized mechanisms to
1939+
* reschedule inodegc work while this draining is in progress.
1940+
*/
19231941
xfs_inodegc_queue_all(mp);
1924-
drain_workqueue(mp->m_inodegc_wq);
1942+
do {
1943+
flush_workqueue(mp->m_inodegc_wq);
1944+
rerun = xfs_inodegc_queue_all(mp);
1945+
} while (rerun);
19251946

19261947
trace_xfs_inodegc_stop(mp, __return_address);
19271948
}
19281949

19291950
/*
19301951
* Enable the inode inactivation background workers and schedule deferred inode
1931-
* inactivation work if there is any.
1952+
* inactivation work if there is any. Caller must hold sb->s_umount to
1953+
* coordinate changes in the inodegc_enabled state.
19321954
*/
19331955
void
19341956
xfs_inodegc_start(

0 commit comments

Comments
 (0)