Skip to contentSkip to conversation

xfs: Fix a deadlock in xfs_log_commit_cil() code path

PATCH In review Chandra Seetharaman 2 participants 5 messages Jul 15, 2013 – Jul 18, 2013 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: Fix a deadlock in xfs_log_commit_cil() code path

While testing and rearranging my pquota/gquota code, I stumbled on a xfs_shutdown() during a mount. But the mount just hung.

I debugged and found that there is a deadlock involving &log->l_cilp->xc_ctx_lock.

It is in a code path where &log->l_cilp->xc_ctx_lock is first acquired in read mode and some levels down the same semaphore is being acquired in write mode causing a deadlock.

This is the stack:
xfs_log_commit_cil -> acquires &log->l_cilp->xc_ctx_lock in read mode
xlog_print_tic_res
xfs_force_shutdown
xfs_log_force_umount
xlog_cil_force
xlog_cil_force_lsn
xlog_cil_push_foreground
xlog_cil_push - tries to acquire same semaphore in write mode

This patch fixes the deadlock by not calling xfs_force_shutdown() while holding the semaphore, instead calling it after dropping teh semaphore.

Thanks to Dave for suggesting this solution.

---
 fs/xfs/xfs_log.c      |    6 +++---
 fs/xfs/xfs_log_cil.c  |   10 ++++++----
 fs/xfs/xfs_log_priv.h |    2 +-
 fs/xfs/xfs_trans.c    |    2 +-
 4 files changed, 11 insertions(+), 9 deletions(-)
fs/xfs/xfs_log.c +3 -3
index d852a2b..b9fa2da 100644
@@ -1837,7 +1837,7 @@ xlog_state_finish_copy(
  * print out info relating to regions written which consume
  * the reservation
  */
-void
+int
 xlog_print_tic_res(
 	struct xfs_mount	*mp,
 	struct xlog_ticket	*ticket)
@@ -1941,7 +1941,7 @@ xlog_print_tic_res(
 
 	xfs_alert_tag(mp, XFS_PTAG_LOGRES,
 		"xlog_write: reservation ran out. Need to up reservation");
-	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
+	return EFSCORRUPTED;
 }
 
 /*
@@ -2215,7 +2215,7 @@ xlog_write(
 		ticket->t_curr_res -= sizeof(xlog_op_header_t);
 
 	if (ticket->t_curr_res < 0)
-		xlog_print_tic_res(log->l_mp, ticket);
+		return xlog_print_tic_res(log->l_mp, ticket);
 
 	index = 0;
 	lv = log_vector;
fs/xfs/xfs_log_cil.c +6 -4
index 02b9cf3..93ba7bd 100644
@@ -730,10 +730,6 @@ xfs_log_commit_cil(
 	/* xlog_cil_insert_items() destroys log_vector list */
 	xlog_cil_insert_items(log, log_vector, tp->t_ticket);
 
-	/* check we didn't blow the reservation */
-	if (tp->t_ticket->t_curr_res < 0)
-		xlog_print_tic_res(log->l_mp, tp->t_ticket);
-
 	/* attach the transaction to the CIL if it has any busy extents */
 	if (!list_empty(&tp->t_busy)) {
 		spin_lock(&log->l_cilp->xc_cil_lock);
@@ -742,6 +738,12 @@ xfs_log_commit_cil(
 		spin_unlock(&log->l_cilp->xc_cil_lock);
 	}
 
+	/* check we didn't blow the reservation */
+	if (tp->t_ticket->t_curr_res < 0) {
+		up_read(&log->l_cilp->xc_ctx_lock);
+		return xlog_print_tic_res(log->l_mp, tp->t_ticket);
+	}
+
 	tp->t_commit_lsn = *commit_lsn;
 	xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
 	xfs_trans_unreserve_and_mod_sb(tp);
fs/xfs/xfs_log_priv.h +1 -1
index b9ea262..4f2fa6d 100644
@@ -576,7 +576,7 @@ xlog_write_adv_cnt(void **ptr, int *len, int *off, size_t bytes)
 	*off += bytes;
 }
 
-void	xlog_print_tic_res(struct xfs_mount *mp, struct xlog_ticket *ticket);
+int	xlog_print_tic_res(struct xfs_mount *mp, struct xlog_ticket *ticket);
 int
 xlog_write(
 	struct xlog		*log,
fs/xfs/xfs_trans.c +1 -1
index 35a2299..d96022f 100644
@@ -1547,7 +1547,7 @@ xfs_trans_commit(
 	xfs_trans_apply_dquot_deltas(tp);
 
 	error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
-	if (error == ENOMEM) {
+	if (error) {
 		xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
 		error = XFS_ERROR(EIO);
 		goto out_unreserve;

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#1 lore raw
Quoted 48 lines · Chandra Seetharaman

While testing and rearranging my pquota/gquota code, I stumbled on a xfs_shutdown() during a mount. But the mount just hung.

I debugged and found that there is a deadlock involving &log->l_cilp->xc_ctx_lock.

It is in a code path where &log->l_cilp->xc_ctx_lock is first acquired in read mode and some levels down the same semaphore is being acquired in write mode causing a deadlock.

This is the stack:
xfs_log_commit_cil -> acquires &log->l_cilp->xc_ctx_lock in read mode
xlog_print_tic_res
xfs_force_shutdown
xfs_log_force_umount
xlog_cil_force
xlog_cil_force_lsn
xlog_cil_push_foreground
xlog_cil_push - tries to acquire same semaphore in write mode

This patch fixes the deadlock by not calling xfs_force_shutdown() while holding the semaphore, instead calling it after dropping teh semaphore.

Thanks to Dave for suggesting this solution.

---
 fs/xfs/xfs_log.c      |    6 +++---
 fs/xfs/xfs_log_cil.c  |   10 ++++++----
 fs/xfs/xfs_log_priv.h |    2 +-
 fs/xfs/xfs_trans.c    |    2 +-
 4 files changed, 11 insertions(+), 9 deletions(-)
fs/xfs/xfs_log.c +1 -1
index d852a2b..b9fa2da 100644
@@ -1837,7 +1837,7 @@ xlog_state_finish_copy(
  * print out info relating to regions written which consume
  * the reservation
  */
-void
+int
 xlog_print_tic_res(
 	struct xfs_mount	*mp,
 	struct xlog_ticket	*ticket)

fs/xfs/xfs_log.c xlog_print_tic_res( · line 1944

Quoted 5 lines
fs/xfs/xfs_log.c +1 -1
@@ -1941,7 +1941,7 @@ xlog_print_tic_res(
 
 	xfs_alert_tag(mp, XFS_PTAG_LOGRES,
 		"xlog_write: reservation ran out. Need to up reservation");
-	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
+	return EFSCORRUPTED;

Note the "SHUTDOWN_CORRUPT_INCORE" reason given here....

fs/xfs/xfs_trans.c xfs_trans_commit( · line 1551

Quoted 11 lines
fs/xfs/xfs_trans.c +1 -1
index 35a2299..d96022f 100644
@@ -1547,7 +1547,7 @@ xfs_trans_commit(
 	xfs_trans_apply_dquot_deltas(tp);
 
 	error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
-	if (error == ENOMEM) {
+	if (error) {
 		xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);

Which is different to the reason given here. The shutdown reason should be maintained for this particular error....

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#2 lore raw
Quoted 64 lines · Dave Chinner
Quoted 48 lines · Chandra Seetharaman

While testing and rearranging my pquota/gquota code, I stumbled on a xfs_shutdown() during a mount. But the mount just hung.

I debugged and found that there is a deadlock involving &log->l_cilp->xc_ctx_lock.

It is in a code path where &log->l_cilp->xc_ctx_lock is first acquired in read mode and some levels down the same semaphore is being acquired in write mode causing a deadlock.

This is the stack:
xfs_log_commit_cil -> acquires &log->l_cilp->xc_ctx_lock in read mode
xlog_print_tic_res
xfs_force_shutdown
xfs_log_force_umount
xlog_cil_force
xlog_cil_force_lsn
xlog_cil_push_foreground
xlog_cil_push - tries to acquire same semaphore in write mode

This patch fixes the deadlock by not calling xfs_force_shutdown() while holding the semaphore, instead calling it after dropping teh semaphore.

Thanks to Dave for suggesting this solution.

---
 fs/xfs/xfs_log.c      |    6 +++---
 fs/xfs/xfs_log_cil.c  |   10 ++++++----
 fs/xfs/xfs_log_priv.h |    2 +-
 fs/xfs/xfs_trans.c    |    2 +-
 4 files changed, 11 insertions(+), 9 deletions(-)
fs/xfs/xfs_log.c +1 -1
index d852a2b..b9fa2da 100644
@@ -1837,7 +1837,7 @@ xlog_state_finish_copy(
  * print out info relating to regions written which consume
  * the reservation
  */
-void
+int
 xlog_print_tic_res(
 	struct xfs_mount	*mp,
 	struct xlog_ticket	*ticket)

fs/xfs/xfs_log.c xlog_print_tic_res( · line 1944

Quoted 5 lines
fs/xfs/xfs_log.c +1 -1
@@ -1941,7 +1941,7 @@ xlog_print_tic_res(
 
 	xfs_alert_tag(mp, XFS_PTAG_LOGRES,
 		"xlog_write: reservation ran out. Need to up reservation");
-	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
+	return EFSCORRUPTED;

Note the "SHUTDOWN_CORRUPT_INCORE" reason given here....

Quoted 7 lines
fs/xfs/xfs_trans.c +0 -0
index 35a2299..d96022f 100644
@@ -1547,7 +1547,7 @@ xfs_trans_commit(
 	xfs_trans_apply_dquot_deltas(tp);
 
Quoted 7 lines

fs/xfs/xfs_trans.c xfs_trans_commit( · line 1551

Quoted 4 lines
fs/xfs/xfs_trans.c +1 -1
 	error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
-	if (error == ENOMEM) {
+	if (error) {
 		xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);

Which is different to the reason given here. The shutdown reason should be maintained for this particular error....

I see.

Is it ok if the error reason is not propagated to the xlog_write() code path ?

Quoted 4 lines

Cheers,

Dave.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#3 lore raw
Quoted 73 lines · Chandra Seetharaman
Quoted 64 lines · Dave Chinner
Quoted 48 lines · Chandra Seetharaman

While testing and rearranging my pquota/gquota code, I stumbled on a xfs_shutdown() during a mount. But the mount just hung.

I debugged and found that there is a deadlock involving &log->l_cilp->xc_ctx_lock.

It is in a code path where &log->l_cilp->xc_ctx_lock is first acquired in read mode and some levels down the same semaphore is being acquired in write mode causing a deadlock.

This is the stack:
xfs_log_commit_cil -> acquires &log->l_cilp->xc_ctx_lock in read mode
xlog_print_tic_res
xfs_force_shutdown
xfs_log_force_umount
xlog_cil_force
xlog_cil_force_lsn
xlog_cil_push_foreground
xlog_cil_push - tries to acquire same semaphore in write mode

This patch fixes the deadlock by not calling xfs_force_shutdown() while holding the semaphore, instead calling it after dropping teh semaphore.

Thanks to Dave for suggesting this solution.

---
 fs/xfs/xfs_log.c      |    6 +++---
 fs/xfs/xfs_log_cil.c  |   10 ++++++----
 fs/xfs/xfs_log_priv.h |    2 +-
 fs/xfs/xfs_trans.c    |    2 +-
 4 files changed, 11 insertions(+), 9 deletions(-)
fs/xfs/xfs_log.c +1 -1
index d852a2b..b9fa2da 100644
@@ -1837,7 +1837,7 @@ xlog_state_finish_copy(
  * print out info relating to regions written which consume
  * the reservation
  */
-void
+int
 xlog_print_tic_res(
 	struct xfs_mount	*mp,
 	struct xlog_ticket	*ticket)

fs/xfs/xfs_log.c xlog_print_tic_res( · line 1944

Quoted 5 lines
fs/xfs/xfs_log.c +1 -1
@@ -1941,7 +1941,7 @@ xlog_print_tic_res(
 
 	xfs_alert_tag(mp, XFS_PTAG_LOGRES,
 		"xlog_write: reservation ran out. Need to up reservation");
-	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
+	return EFSCORRUPTED;

Note the "SHUTDOWN_CORRUPT_INCORE" reason given here....

Quoted 7 lines
fs/xfs/xfs_trans.c +0 -0
index 35a2299..d96022f 100644
@@ -1547,7 +1547,7 @@ xfs_trans_commit(
 	xfs_trans_apply_dquot_deltas(tp);
 
Quoted 7 lines

fs/xfs/xfs_trans.c xfs_trans_commit( · line 1551

Quoted 4 lines
fs/xfs/xfs_trans.c +1 -1
 	error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
-	if (error == ENOMEM) {
+	if (error) {
 		xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);

Which is different to the reason given here. The shutdown reason should be maintained for this particular error....

Quoted 1 line

I see.

What I mean is that the code in xfs_trans_commit() should do something like:

	if (error) {
		int reason = SHUTDOWN_LOG_IO_ERROR;
		if (error == EFSCORRUPTED)
			reason = SHUTDOWN_CORRUPT_INCORE;
		xfs_force_shutdown(mp, reason);
		....
	}
Quoted 3 lines

Is it ok if the error reason is not propagated to the xlog_write() code path ?

No - if we get a transaction overflow, we need to trigger a shutdown. That means the error needs to be caught by the xlog_write() path an the filesystem shut down.

Looking at it more deeply, you could probably just change the shutdown in xlog_print_tic_res() to use SHUTDOWN_LOG_IO_ERROR and the problem is solved as the shutdown won't try to force the log. i.e. this whole problem will go away with that one line fix...

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#4 lore raw
Quoted 91 lines · Dave Chinner
Quoted 73 lines · Chandra Seetharaman
Quoted 64 lines · Dave Chinner
Quoted 48 lines · Chandra Seetharaman

While testing and rearranging my pquota/gquota code, I stumbled on a xfs_shutdown() during a mount. But the mount just hung.

I debugged and found that there is a deadlock involving &log->l_cilp->xc_ctx_lock.

It is in a code path where &log->l_cilp->xc_ctx_lock is first acquired in read mode and some levels down the same semaphore is being acquired in write mode causing a deadlock.

This is the stack:
xfs_log_commit_cil -> acquires &log->l_cilp->xc_ctx_lock in read mode
xlog_print_tic_res
xfs_force_shutdown
xfs_log_force_umount
xlog_cil_force
xlog_cil_force_lsn
xlog_cil_push_foreground
xlog_cil_push - tries to acquire same semaphore in write mode

This patch fixes the deadlock by not calling xfs_force_shutdown() while holding the semaphore, instead calling it after dropping teh semaphore.

Thanks to Dave for suggesting this solution.

---
 fs/xfs/xfs_log.c      |    6 +++---
 fs/xfs/xfs_log_cil.c  |   10 ++++++----
 fs/xfs/xfs_log_priv.h |    2 +-
 fs/xfs/xfs_trans.c    |    2 +-
 4 files changed, 11 insertions(+), 9 deletions(-)
fs/xfs/xfs_log.c +1 -1
index d852a2b..b9fa2da 100644
@@ -1837,7 +1837,7 @@ xlog_state_finish_copy(
  * print out info relating to regions written which consume
  * the reservation
  */
-void
+int
 xlog_print_tic_res(
 	struct xfs_mount	*mp,
 	struct xlog_ticket	*ticket)

fs/xfs/xfs_log.c xlog_print_tic_res( · line 1944

Quoted 5 lines
fs/xfs/xfs_log.c +1 -1
@@ -1941,7 +1941,7 @@ xlog_print_tic_res(
 
 	xfs_alert_tag(mp, XFS_PTAG_LOGRES,
 		"xlog_write: reservation ran out. Need to up reservation");
-	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
+	return EFSCORRUPTED;

Note the "SHUTDOWN_CORRUPT_INCORE" reason given here....

Quoted 7 lines
fs/xfs/xfs_trans.c +0 -0
index 35a2299..d96022f 100644
@@ -1547,7 +1547,7 @@ xfs_trans_commit(
 	xfs_trans_apply_dquot_deltas(tp);
 
Quoted 7 lines

fs/xfs/xfs_trans.c xfs_trans_commit( · line 1551

Quoted 4 lines
fs/xfs/xfs_trans.c +1 -1
 	error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
-	if (error == ENOMEM) {
+	if (error) {
 		xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);

Which is different to the reason given here. The shutdown reason should be maintained for this particular error....

Quoted 1 line

I see.

What I mean is that the code in xfs_trans_commit() should do something like:

	if (error) {
		int reason = SHUTDOWN_LOG_IO_ERROR;
		if (error == EFSCORRUPTED)
			reason = SHUTDOWN_CORRUPT_INCORE;
		xfs_force_shutdown(mp, reason);
		....
	}
Quoted 3 lines

Is it ok if the error reason is not propagated to the xlog_write() code path ?

Quoted 8 lines

No - if we get a transaction overflow, we need to trigger a shutdown. That means the error needs to be caught by the xlog_write() path an the filesystem shut down.

Looking at it more deeply, you could probably just change the shutdown in xlog_print_tic_res() to use SHUTDOWN_LOG_IO_ERROR and the problem is solved as the shutdown won't try to force the log. i.e. this whole problem will go away with that one line fix...

I am confused.

In the previous response you mentioned that we have to propagate the reason as-is in xfs_trans_commit() path. But, the new suggestion you are making will change the behavior of all paths and they will not enter xfs_log_force_umount().

Besides, IIUC, XFS_MOUNT_FS_SHUTDOWN is set only in xfs_log_force_umount(), so the very first time we enter xlog_print_tic_res(), even with SHUTDOWN_LOG_IO_ERROR we will call xfs_log_force_umount() when can lead to the deadlock we are trying to avoid.

Quoted 4 lines

Cheers,

Dave.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs