Skip to contentSkip to conversation

xfs: don't call into blockgc scan with freeze protection

PATCH In review Brian Foster 3 participants 6 messages Feb 18, 2021 – Feb 22, 2021 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: don't call into blockgc scan with freeze protection

fstest xfs/167 produced a lockdep splat that complained about a nested transaction acquiring freeze protection during an eofblocks scan. Drop freeze protection around the block reclaim scan in the transaction allocation code to avoid this problem.

---
 fs/xfs/xfs_trans.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/xfs/xfs_trans.c +14 -5
index 44f72c09c203..c32c62d3b77a 100644
@@ -261,6 +261,7 @@ xfs_trans_alloc(
 {
 	struct xfs_trans	*tp;
 	int			error;
+	bool			retried = false;
 
 	/*
 	 * Allocate the handle before we do our freeze accounting and setting up
@@ -288,19 +289,27 @@ xfs_trans_alloc(
 	INIT_LIST_HEAD(&tp->t_dfops);
 	tp->t_firstblock = NULLFSBLOCK;
 
+retry:
 	error = xfs_trans_reserve(tp, resp, blocks, rtextents);
-	if (error == -ENOSPC) {
+	if (error == -ENOSPC && !retried) {
 		/*
 		 * We weren't able to reserve enough space for the transaction.
 		 * Flush the other speculative space allocations to free space.
 		 * Do not perform a synchronous scan because callers can hold
 		 * other locks.
 		 */
+		retried = true;
+		if (!(flags & XFS_TRANS_NO_WRITECOUNT))
+			sb_end_intwrite(mp->m_super);
 		error = xfs_blockgc_free_space(mp, NULL);
-		if (!error)
-			error = xfs_trans_reserve(tp, resp, blocks, rtextents);
-	}
-	if (error) {
+		if (error) {
+			kmem_cache_free(xfs_trans_zone, tp);
+			return error;
+		}
+		if (!(flags & XFS_TRANS_NO_WRITECOUNT))
+			sb_start_intwrite(mp->m_super);
+		goto retry;
+	} else if (error) {
 		xfs_trans_cancel(tp);
 		return error;
 	}
Signature · 1 line
2.26.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Brian Foster

fstest xfs/167 produced a lockdep splat that complained about a nested transaction acquiring freeze protection during an eofblocks scan. Drop freeze protection around the block reclaim scan in the transaction allocation code to avoid this problem.

I think it seems reasonable, though I really wish that other patchset to clean up all the "modify thread state when we start/end transactions" had landed.

--D

Quoted 52 lines
---
 fs/xfs/xfs_trans.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/xfs/xfs_trans.c +14 -5
index 44f72c09c203..c32c62d3b77a 100644
@@ -261,6 +261,7 @@ xfs_trans_alloc(
 {
 	struct xfs_trans	*tp;
 	int			error;
+	bool			retried = false;
 
 	/*
 	 * Allocate the handle before we do our freeze accounting and setting up
@@ -288,19 +289,27 @@ xfs_trans_alloc(
 	INIT_LIST_HEAD(&tp->t_dfops);
 	tp->t_firstblock = NULLFSBLOCK;
 
+retry:
 	error = xfs_trans_reserve(tp, resp, blocks, rtextents);
-	if (error == -ENOSPC) {
+	if (error == -ENOSPC && !retried) {
 		/*
 		 * We weren't able to reserve enough space for the transaction.
 		 * Flush the other speculative space allocations to free space.
 		 * Do not perform a synchronous scan because callers can hold
 		 * other locks.
 		 */
+		retried = true;
+		if (!(flags & XFS_TRANS_NO_WRITECOUNT))
+			sb_end_intwrite(mp->m_super);
 		error = xfs_blockgc_free_space(mp, NULL);
-		if (!error)
-			error = xfs_trans_reserve(tp, resp, blocks, rtextents);
-	}
-	if (error) {
+		if (error) {
+			kmem_cache_free(xfs_trans_zone, tp);
+			return error;
+		}
+		if (!(flags & XFS_TRANS_NO_WRITECOUNT))
+			sb_start_intwrite(mp->m_super);
+		goto retry;
+	} else if (error) {
 		xfs_trans_cancel(tp);
 		return error;
 	}
Signature · 1 line
2.26.2
#2 lore raw
Quoted 50 lines · Darrick J. Wong
Quoted 6 lines · Brian Foster

fstest xfs/167 produced a lockdep splat that complained about a nested transaction acquiring freeze protection during an eofblocks scan. Drop freeze protection around the block reclaim scan in the transaction allocation code to avoid this problem.

I think it seems reasonable, though I really wish that other patchset to clean up all the "modify thread state when we start/end transactions" had landed.

--D

Quoted 34 lines
---
 fs/xfs/xfs_trans.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/xfs/xfs_trans.c +6 -1
index 44f72c09c203..c32c62d3b77a 100644
@@ -261,6 +261,7 @@ xfs_trans_alloc(
 {
 	struct xfs_trans	*tp;
 	int			error;
+	bool			retried = false;
 
 	/*
 	 * Allocate the handle before we do our freeze accounting and setting up
@@ -288,19 +289,27 @@ xfs_trans_alloc(
 	INIT_LIST_HEAD(&tp->t_dfops);
 	tp->t_firstblock = NULLFSBLOCK;
 
+retry:
 	error = xfs_trans_reserve(tp, resp, blocks, rtextents);
-	if (error == -ENOSPC) {
+	if (error == -ENOSPC && !retried) {
 		/*
 		 * We weren't able to reserve enough space for the transaction.
 		 * Flush the other speculative space allocations to free space.
 		 * Do not perform a synchronous scan because callers can hold
 		 * other locks.
 		 */
+		retried = true;
+		if (!(flags & XFS_TRANS_NO_WRITECOUNT))
+			sb_end_intwrite(mp->m_super);
 		error = xfs_blockgc_free_space(mp, NULL);
Quoted 8 lines

fs/xfs/xfs_trans.c xfs_trans_alloc( · line 308

Quoted 8 lines
fs/xfs/xfs_trans.c +4 -4
-		if (!error)
-			error = xfs_trans_reserve(tp, resp, blocks, rtextents);
-	}
-	if (error) {
+		if (error) {
+			kmem_cache_free(xfs_trans_zone, tp);
+			return error;
+		}

This seems dangerous to me. If xfs_trans_reserve() adds anything to the transaction even if it fails, this will fail to free it. e.g. xfs_log_reserve() call allocate a ticket and attach it to the transaction and *then fail*. This code will now leak that ticket.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#3 lore raw
Quoted 8 lines · Dave Chinner
Quoted 7 lines · Darrick J. Wong
Quoted 6 lines · Brian Foster

fstest xfs/167 produced a lockdep splat that complained about a nested transaction acquiring freeze protection during an eofblocks scan. Drop freeze protection around the block reclaim scan in the transaction allocation code to avoid this problem.

...

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
 fs/xfs/xfs_trans.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/xfs/xfs_trans.c +0 -0
index 44f72c09c203..c32c62d3b77a 100644

...

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
fs/xfs/xfs_trans.c +6 -5
@@ -288,19 +289,27 @@ xfs_trans_alloc(
 	INIT_LIST_HEAD(&tp->t_dfops);
 	tp->t_firstblock = NULLFSBLOCK;
 
+retry:
 	error = xfs_trans_reserve(tp, resp, blocks, rtextents);
-	if (error == -ENOSPC) {
+	if (error == -ENOSPC && !retried) {
 		/*
 		 * We weren't able to reserve enough space for the transaction.
 		 * Flush the other speculative space allocations to free space.
 		 * Do not perform a synchronous scan because callers can hold
 		 * other locks.
 		 */
+		retried = true;
+		if (!(flags & XFS_TRANS_NO_WRITECOUNT))
+			sb_end_intwrite(mp->m_super);
 		error = xfs_blockgc_free_space(mp, NULL);
-		if (!error)
-			error = xfs_trans_reserve(tp, resp, blocks, rtextents);
-	}
-	if (error) {
+		if (error) {
Quoted 9 lines
Quoted 3 lines

fs/xfs/xfs_trans.c xfs_trans_alloc( · line 308

Quoted 3 lines
fs/xfs/xfs_trans.c +3 -0
+			kmem_cache_free(xfs_trans_zone, tp);
+			return error;
+		}

This seems dangerous to me. If xfs_trans_reserve() adds anything to the transaction even if it fails, this will fail to free it. e.g. xfs_log_reserve() call allocate a ticket and attach it to the transaction and *then fail*. This code will now leak that ticket.

xfs_trans_reserve() ungrants the log ticket (which frees it, at least in the allocation case) and disassociates from the transaction on error, so I don't see how this causes any problems.

Brian

Quoted 7 lines

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#4 lore raw
Quoted 51 lines · Brian Foster
Quoted 8 lines · Dave Chinner
Quoted 7 lines · Darrick J. Wong
Quoted 6 lines · Brian Foster

fstest xfs/167 produced a lockdep splat that complained about a nested transaction acquiring freeze protection during an eofblocks scan. Drop freeze protection around the block reclaim scan in the transaction allocation code to avoid this problem.

...

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
 fs/xfs/xfs_trans.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/xfs/xfs_trans.c +0 -0
index 44f72c09c203..c32c62d3b77a 100644

...

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
fs/xfs/xfs_trans.c +6 -5
@@ -288,19 +289,27 @@ xfs_trans_alloc(
 	INIT_LIST_HEAD(&tp->t_dfops);
 	tp->t_firstblock = NULLFSBLOCK;
 
+retry:
 	error = xfs_trans_reserve(tp, resp, blocks, rtextents);
-	if (error == -ENOSPC) {
+	if (error == -ENOSPC && !retried) {
 		/*
 		 * We weren't able to reserve enough space for the transaction.
 		 * Flush the other speculative space allocations to free space.
 		 * Do not perform a synchronous scan because callers can hold
 		 * other locks.
 		 */
+		retried = true;
+		if (!(flags & XFS_TRANS_NO_WRITECOUNT))
+			sb_end_intwrite(mp->m_super);
 		error = xfs_blockgc_free_space(mp, NULL);
-		if (!error)
-			error = xfs_trans_reserve(tp, resp, blocks, rtextents);
-	}
-	if (error) {
+		if (error) {
Quoted 9 lines
Quoted 3 lines

fs/xfs/xfs_trans.c xfs_trans_alloc( · line 308

Quoted 3 lines
fs/xfs/xfs_trans.c +3 -0
+			kmem_cache_free(xfs_trans_zone, tp);
+			return error;
+		}

This seems dangerous to me. If xfs_trans_reserve() adds anything to the transaction even if it fails, this will fail to free it. e.g. xfs_log_reserve() call allocate a ticket and attach it to the transaction and *then fail*. This code will now leak that ticket.

Quoted 3 lines

xfs_trans_reserve() ungrants the log ticket (which frees it, at least in the allocation case) and disassociates from the transaction on error, so I don't see how this causes any problems.

It ungrants the log ticket when it jumps to "undo_log" on error. When xfs_log_reserve() fails, it jumps to "undo_blocks" and doesn't ungrant the ticket. Hence potentially leaving an allocated ticket attached to the transaction on error. xfs_trans_cancel() handles this just fine, just freeing the transaction doesn't.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#5 lore raw
Quoted 56 lines · Dave Chinner
Quoted 51 lines · Brian Foster
Quoted 8 lines · Dave Chinner
Quoted 7 lines · Darrick J. Wong
Quoted 6 lines · Brian Foster

fstest xfs/167 produced a lockdep splat that complained about a nested transaction acquiring freeze protection during an eofblocks scan. Drop freeze protection around the block reclaim scan in the transaction allocation code to avoid this problem.

...

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
 fs/xfs/xfs_trans.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/xfs/xfs_trans.c +0 -0
index 44f72c09c203..c32c62d3b77a 100644

...

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
fs/xfs/xfs_trans.c +6 -5
@@ -288,19 +289,27 @@ xfs_trans_alloc(
 	INIT_LIST_HEAD(&tp->t_dfops);
 	tp->t_firstblock = NULLFSBLOCK;
 
+retry:
 	error = xfs_trans_reserve(tp, resp, blocks, rtextents);
-	if (error == -ENOSPC) {
+	if (error == -ENOSPC && !retried) {
 		/*
 		 * We weren't able to reserve enough space for the transaction.
 		 * Flush the other speculative space allocations to free space.
 		 * Do not perform a synchronous scan because callers can hold
 		 * other locks.
 		 */
+		retried = true;
+		if (!(flags & XFS_TRANS_NO_WRITECOUNT))
+			sb_end_intwrite(mp->m_super);
 		error = xfs_blockgc_free_space(mp, NULL);
-		if (!error)
-			error = xfs_trans_reserve(tp, resp, blocks, rtextents);
-	}
-	if (error) {
+		if (error) {
Quoted 9 lines
Quoted 3 lines

fs/xfs/xfs_trans.c xfs_trans_alloc( · line 308

Quoted 3 lines
fs/xfs/xfs_trans.c +3 -0
+			kmem_cache_free(xfs_trans_zone, tp);
+			return error;
+		}

This seems dangerous to me. If xfs_trans_reserve() adds anything to the transaction even if it fails, this will fail to free it. e.g. xfs_log_reserve() call allocate a ticket and attach it to the transaction and *then fail*. This code will now leak that ticket.

Quoted 3 lines

xfs_trans_reserve() ungrants the log ticket (which frees it, at least in the allocation case) and disassociates from the transaction on error, so I don't see how this causes any problems.

Quoted 6 lines

It ungrants the log ticket when it jumps to "undo_log" on error. When xfs_log_reserve() fails, it jumps to "undo_blocks" and doesn't ungrant the ticket. Hence potentially leaving an allocated ticket attached to the transaction on error. xfs_trans_cancel() handles this just fine, just freeing the transaction doesn't.

Ah, my mistake. Must have misread the label...

Brian

Quoted 7 lines

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]