Skip to contentSkip to conversation

libxfs: fix crash on second attempt to initialize library

PATCH In review Darrick J. Wong 2 participants 2 messages Oct 5, 2021 – Oct 15, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxfs: fix crash on second attempt to initialize library

From: Darrick J. Wong <[email protected]>

xfs_repair crashes when it tries to initialize the libxfs library but the initialization fails because the fs is already mounted:

# xfs_repair /dev/sdd
xfs_repair: /dev/sdd contains a mounted filesystem
xfs_repair: urcu.c:553: urcu_memb_register_thread: Assertion `!URCU_TLS(rcu_reader).registered' failed.
Aborted

This is because libxfs_init() registers the main thread with liburcu, but doesn't unregister the thread if libxfs library initialization fails. When repair sets more dangerous options and tries again, the second initialization attempt causes liburcu to abort. Fix this by unregistering the thread with liburcu if libxfs initialization fails.

Observed by running xfs/284.

Fixes e4da1b16 ("xfsprogs: introduce liburcu support")
---
 libxfs/init.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libxfs/init.c +3 -1
index d0753ce5..14911596 100644
@@ -407,8 +407,10 @@ libxfs_init(libxfs_init_t *a)
 		unlink(rtpath);
 	if (fd >= 0)
 		close(fd);
-	if (!rval)
+	if (!rval) {
 		libxfs_close_devices(a);
+		rcu_unregister_thread();
+	}
 
 	return rval;
 }
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

xfs_repair crashes when it tries to initialize the libxfs library but the initialization fails because the fs is already mounted:

Sorry for missing this; looks right to me. For some reason I did not hit it in testing, old library version I guess.

Quoted 37 lines
# xfs_repair /dev/sdd
xfs_repair: /dev/sdd contains a mounted filesystem
xfs_repair: urcu.c:553: urcu_memb_register_thread: Assertion `!URCU_TLS(rcu_reader).registered' failed.
Aborted

This is because libxfs_init() registers the main thread with liburcu, but doesn't unregister the thread if libxfs library initialization fails. When repair sets more dangerous options and tries again, the second initialization attempt causes liburcu to abort. Fix this by unregistering the thread with liburcu if libxfs initialization fails.

Observed by running xfs/284.

Fixes e4da1b16 ("xfsprogs: introduce liburcu support")
---
  libxfs/init.c |    4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
libxfs/init.c +3 -1
index d0753ce5..14911596 100644
@@ -407,8 +407,10 @@ libxfs_init(libxfs_init_t *a)
  		unlink(rtpath);
  	if (fd >= 0)
  		close(fd);
-	if (!rval)
+	if (!rval) {
  		libxfs_close_devices(a);
+		rcu_unregister_thread();
+	}
 
  	return rval;
  }