public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] xfs: fix race between healthmon unmount and read_iter
@ 2026-03-02 17:31 Darrick J. Wong
  2026-03-05  9:47 ` Carlos Maiolino
  0 siblings, 1 reply; 2+ messages in thread
From: Darrick J. Wong @ 2026-03-02 17:31 UTC (permalink / raw)
  To: Carlos Maiolino; +Cc: xfs, Christoph Hellwig

From: Darrick J. Wong <djwong@kernel.org>

xfs/1879 on one of my test VMs got stuck due to the xfs_io healthmon
subcommand sleeping in wait_event_interruptible at:

 xfs_healthmon_read_iter+0x558/0x5f8 [xfs]
 vfs_read+0x248/0x320
 ksys_read+0x78/0x120

Looking at xfs_healthmon_read_iter, in !O_NONBLOCK mode it will sleep
until the mount cookie == DETACHED_MOUNT_COOKIE, there are events
waiting to be formatted, or there are formatted events in the read
buffer that could be copied to userspace.

Poking into the running kernel, I see that there are zero events in the
list, the read buffer is empty, and the mount cookie is indeed in
DETACHED state.  IOWs, xfs_healthmon_has_eventdata should have returned
true, but instead we're asleep waiting for a wakeup.

I think what happened here is that xfs_healthmon_read_iter and
xfs_healthmon_unmount were racing with each other, and _read_iter lost
the race.  _unmount queued an unmount event, which woke up _read_iter.
It found, formatted, and copied the event out to userspace.  That
cleared out the pending event list and emptied the read buffer.  xfs_io
then called read() again, so _has_eventdata decided that we should sleep
on the empty event queue.

Next, _unmount called xfs_healthmon_detach, which set the mount cookie
to DETACHED.  Unfortunately, it didn't call wake_up_all on the hm, so
the wait_event_interruptible in the _read_iter thread remains asleep.
That's why the test stalled.

Fix this by moving the wake_up_all call to xfs_healthmon_detach.

Fixes: b3a289a2a9397b ("xfs: create event queuing, formatting, and discovery infrastructure")
Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
---
v2: add review tags
---
 fs/xfs/xfs_healthmon.c |   17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/fs/xfs/xfs_healthmon.c b/fs/xfs/xfs_healthmon.c
index 4a06d6632f65e2..26c325d34bd1ac 100644
--- a/fs/xfs/xfs_healthmon.c
+++ b/fs/xfs/xfs_healthmon.c
@@ -141,6 +141,16 @@ xfs_healthmon_detach(
 	hm->mount_cookie = DETACHED_MOUNT_COOKIE;
 	spin_unlock(&xfs_healthmon_lock);
 
+	/*
+	 * Wake up any readers that might remain.  This can happen if unmount
+	 * races with the healthmon fd owner entering ->read_iter, having
+	 * already emptied the event queue.
+	 *
+	 * In the ->release case there shouldn't be any readers because the
+	 * only users of the waiter are read and poll.
+	 */
+	wake_up_all(&hm->wait);
+
 	trace_xfs_healthmon_detach(hm);
 	xfs_healthmon_put(hm);
 }
@@ -1027,13 +1037,6 @@ xfs_healthmon_release(
 	 * process can create another health monitor file.
 	 */
 	xfs_healthmon_detach(hm);
-
-	/*
-	 * Wake up any readers that might be left.  There shouldn't be any
-	 * because the only users of the waiter are read and poll.
-	 */
-	wake_up_all(&hm->wait);
-
 	xfs_healthmon_put(hm);
 	return 0;
 }

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] xfs: fix race between healthmon unmount and read_iter
  2026-03-02 17:31 [PATCH v2] xfs: fix race between healthmon unmount and read_iter Darrick J. Wong
@ 2026-03-05  9:47 ` Carlos Maiolino
  0 siblings, 0 replies; 2+ messages in thread
From: Carlos Maiolino @ 2026-03-05  9:47 UTC (permalink / raw)
  To: Darrick J. Wong; +Cc: xfs, Christoph Hellwig

On Mon, 02 Mar 2026 09:31:58 -0800, Darrick J. Wong wrote:
> xfs/1879 on one of my test VMs got stuck due to the xfs_io healthmon
> subcommand sleeping in wait_event_interruptible at:
> 
>  xfs_healthmon_read_iter+0x558/0x5f8 [xfs]
>  vfs_read+0x248/0x320
>  ksys_read+0x78/0x120
> 
> [...]

Applied to for-next, thanks!

[1/1] xfs: fix race between healthmon unmount and read_iter
      commit: 0ca1a8331c0fa5e57844e003a5d667a15b1e002c

Best regards,
-- 
Carlos Maiolino <cem@kernel.org>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-03-05  9:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-02 17:31 [PATCH v2] xfs: fix race between healthmon unmount and read_iter Darrick J. Wong
2026-03-05  9:47 ` Carlos Maiolino

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox