fserror_unmount() drops the bias on s_pending_errors and waits for the count to reach zero in wait_var_event() while the umount holds s_umount exclusively. The worker that finishes the last pending error wakes it up in fserror_pending_dec() through refcount_dec_and_test() and wake_up_var(). wake_up_var() requires a full barrier between the store that makes the condition true and its waitqueue_active() check. refcount_dec_and_test() isn't fully ordered though. It provides release ordering and acquire ordering on success which orders the load of the decrement but not its store against the later load of the waitqueue. So the worker can see an empty waitqueue while the umount, already queued, still reads a count of one. The umount then sleeps forever with s_umount held and sync(2) and everything else that wants that superblock hangs behind it. LKMM allows this and so do the herd7 models for arm64 (LDADDL; DMB ISHLD; LDR) and POWER. x86 is safe because the locked xadd is a full barrier. With smp_mb() before wake_up_var() all three forbid it. The litmus test (LKMM: Sometimes; with smp_mb() before the read of wq in P1: Never). P0 is fserror_unmount() dropping the bias and queueing in wait_var_event(), P1 is the last fserror_pending_dec(): { cnt=2; wq=0; state=0; } P0(atomic_t *cnt, int *wq, int *state, spinlock_t *wql) { int r0, r1, r2; r0 = atomic_fetch_sub_release(1, cnt); if (r0 == 2) { r1 = atomic_read(cnt); if (r1 == 1) { spin_lock(wql); WRITE_ONCE(*wq, 1); smp_store_mb(*state, 2); spin_unlock(wql); r2 = atomic_read(cnt); } } } P1(atomic_t *cnt, int *wq, int *woken, spinlock_t *wql) { int r3, r4; r3 = atomic_fetch_sub_release(1, cnt); if (r3 == 1) { smp_rmb(); r4 = READ_ONCE(*wq); if (r4 != 0) { spin_lock(wql); WRITE_ONCE(*woken, 1); spin_unlock(wql); } } } exists (0:r0=2 /\ 0:r1=1 /\ 0:r2=1 /\ 1:r3=1 /\ 1:r4=0) Not reproducible on x86 hardware. The consequence can be shown there by emulating the reordering in fserror_worker(), debug only: sample waitqueue_active() before the decrement, sleep, then decrement and only wake if the sample saw a waiter: iput(event->inode); + if (debug_reorder_ms) { + struct super_block *dsb = event->sb; + bool waiter; + + mempool_free(event, &fserror_events_pool); + waiter = waitqueue_active(__var_waitqueue(&dsb->s_pending_errors)); + msleep(debug_reorder_ms); + if (refcount_dec_and_test(&dsb->s_pending_errors) && waiter) + wake_up_var(&dsb->s_pending_errors); + return; + } fserror_free_event(event); # boot with fserror.debug_reorder_ms=5000 mount -t ext4 /dev/vda /mnt xfs_io -x -c "shutdown" /mnt # EXT4_IOC_SHUTDOWN with NOLOGFLUSH umount /mnt & # hangs forever sync & # hangs behind s_umount INFO: task umount:108 blocked for more than 30 seconds. fserror_unmount+0x20d/0x2c0 generic_shutdown_super+0xac/0x530 kill_block_super+0x3b/0xa0 ext4_kill_sb+0x6c/0xc0 INFO: task sync:111 blocked on an rw-semaphore likely owned by task umount:108 Fixes: 21945e6cb516 ("fs: report filesystem and file I/O errors to fsnotify") Cc: stable@vger.kernel.org Signed-off-by: Christian Brauner (Amutable) --- fs/fserror.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/fs/fserror.c b/fs/fserror.c index 1e4d11fd9562..d8a800580012 100644 --- a/fs/fserror.c +++ b/fs/fserror.c @@ -35,8 +35,15 @@ void fserror_unmount(struct super_block *sb) static inline void fserror_pending_dec(struct super_block *sb) { - if (refcount_dec_and_test(&sb->s_pending_errors)) + if (refcount_dec_and_test(&sb->s_pending_errors)) { + /* + * refcount_dec_and_test() isn't fully ordered but wake_up_var() + * needs a full barrier between the store and its waitqueue check. + * Pairs with the barrier in prepare_to_wait_event(). + */ + smp_mb(); wake_up_var(&sb->s_pending_errors); + } } static inline void fserror_free_event(struct fserror_event *event) -- 2.53.0