Skip to contentSkip to conversation

jbd2: preserve original nofs flag during journal restart

PATCH Superseded Tahsin Erdogan 4 participants 10 messages May 17, 2017 – May 20, 2017 Journalling layer for block devices (JBD2)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 jbd2: preserve original nofs flag during journal restart

When a transaction starts, start_this_handle() saves current PF_MEMALLOC_NOFS value so that it can be restored at journal stop time. Journal restart is a special case that calls start_this_handle() without stopping the transaction. start_this_handle() isn't aware that the original value is already stored so it overwrites it with current value.

For instance, a call sequence like below leaves PF_MEMALLOC_NOFS flag set at the end:

jbd2_journal_start()
jbd2__journal_restart()
jbd2_journal_stop()

Make jbd2__journal_restart() restore the original value before calling start_this_handle().

Fixes 81378da64de6 ("jbd2: mark the transaction context with the scope GFP_NOFS context")
---
 fs/jbd2/transaction.c | 1 +
 1 file changed, 1 insertion(+)
fs/jbd2/transaction.c +1 -0
index 9ee4832b6f8b..dfd6afebdfeb 100644
@@ -680,6 +680,7 @@ int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
 
 	rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
 	handle->h_buffer_credits = nblocks;
+	memalloc_nofs_restore(handle->saved_alloc_context);
 	ret = start_this_handle(journal, handle, gfp_mask);
 	return ret;
 }
Signature · 1 line
2.13.0.303.g4ebf302169-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Tahsin Erdogan

When a transaction starts, start_this_handle() saves current PF_MEMALLOC_NOFS value so that it can be restored at journal stop time. Journal restart is a special case that calls start_this_handle() without stopping the transaction. start_this_handle() isn't aware that the original value is already stored so it overwrites it with current value.

For instance, a call sequence like below leaves PF_MEMALLOC_NOFS flag set at the end:

jbd2_journal_start()
jbd2__journal_restart()
jbd2_journal_stop()

Quoted 5 lines

Make jbd2__journal_restart() restore the original value before calling start_this_handle().

Fixes 81378da64de6 ("jbd2: mark the transaction context with the scope GFP_NOFS context")

Thanks for catching this! The patch looks good. You can add:

Honza

Quoted 20 lines
---
 fs/jbd2/transaction.c | 1 +
 1 file changed, 1 insertion(+)
fs/jbd2/transaction.c +1 -0
index 9ee4832b6f8b..dfd6afebdfeb 100644
@@ -680,6 +680,7 @@ int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
 
 	rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
 	handle->h_buffer_credits = nblocks;
+	memalloc_nofs_restore(handle->saved_alloc_context);
 	ret = start_this_handle(journal, handle, gfp_mask);
 	return ret;
 }
Signature · 1 line
2.13.0.303.g4ebf302169-goog
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#2 lore raw
In reply to Tahsin Erdogan
Quoted 27 lines · Tahsin Erdogan

When a transaction starts, start_this_handle() saves current PF_MEMALLOC_NOFS value so that it can be restored at journal stop time. Journal restart is a special case that calls start_this_handle() without stopping the transaction. start_this_handle() isn't aware that the original value is already stored so it overwrites it with current value.

For instance, a call sequence like below leaves PF_MEMALLOC_NOFS flag set at the end:

jbd2_journal_start()
jbd2__journal_restart()
jbd2_journal_stop()

Make jbd2__journal_restart() restore the original value before calling start_this_handle().

Fixes 81378da64de6 ("jbd2: mark the transaction context with the scope GFP_NOFS context")
---
 fs/jbd2/transaction.c | 1 +
 1 file changed, 1 insertion(+)
fs/jbd2/transaction.c +0 -0
index 9ee4832b6f8b..dfd6afebdfeb 100644

fs/jbd2/transaction.c int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask) · line 686

Quoted 7 lines
fs/jbd2/transaction.c +1 -0
@@ -680,6 +680,7 @@ int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
 
 	rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
 	handle->h_buffer_credits = nblocks;
+	memalloc_nofs_restore(handle->saved_alloc_context);
 	ret = start_this_handle(journal, handle, gfp_mask);
 	return ret;
 }

I remember Jack has mentioned something about nested transaction back then when reviewing the patch. But I cannot remember or find a pointer to that email. I have a vague recollection that there is a reference counting for those transactions.

Anyway, Is this patch really correct? So let's say we are in the transaction context already and then you disable the scope NOFS protection, start_this_handle will allocate before it calls memalloc_nofs_save and that would recurse to the filesystem. If anything wouldn't it be better to simply call memalloc_nofs_save only if we start a new transaction? I thought we were doing that already but the code is so convoluted I have hard time to wrap my head around it.

Signature · 2 lines
Michal Hocko
SUSE Labs
#3 lore raw
Quoted 41 lines · Michal Hocko
Quoted 27 lines · Tahsin Erdogan

When a transaction starts, start_this_handle() saves current PF_MEMALLOC_NOFS value so that it can be restored at journal stop time. Journal restart is a special case that calls start_this_handle() without stopping the transaction. start_this_handle() isn't aware that the original value is already stored so it overwrites it with current value.

For instance, a call sequence like below leaves PF_MEMALLOC_NOFS flag set at the end:

jbd2_journal_start()
jbd2__journal_restart()
jbd2_journal_stop()

Make jbd2__journal_restart() restore the original value before calling start_this_handle().

Fixes 81378da64de6 ("jbd2: mark the transaction context with the scope GFP_NOFS context")
---
 fs/jbd2/transaction.c | 1 +
 1 file changed, 1 insertion(+)
fs/jbd2/transaction.c +0 -0
index 9ee4832b6f8b..dfd6afebdfeb 100644

fs/jbd2/transaction.c int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask) · line 686

Quoted 7 lines
fs/jbd2/transaction.c +1 -0
@@ -680,6 +680,7 @@ int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
 
 	rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
 	handle->h_buffer_credits = nblocks;
+	memalloc_nofs_restore(handle->saved_alloc_context);
 	ret = start_this_handle(journal, handle, gfp_mask);
 	return ret;
 }

I remember Jack has mentioned something about nested transaction back then when reviewing the patch. But I cannot remember or find a pointer to that email. I have a vague recollection that there is a reference counting for those transactions.

Quoted 7 lines

Anyway, Is this patch really correct? So let's say we are in the transaction context already and then you disable the scope NOFS protection, start_this_handle will allocate before it calls memalloc_nofs_save and that would recurse to the filesystem. If anything wouldn't it be better to simply call memalloc_nofs_save only if we start a new transaction? I thought we were doing that already but the code is so convoluted I have hard time to wrap my head around it.

I was thinking about his as well but the fact is jbd2__journal_restart() actually does equivalent of jbd2_journal_stop() for the handle above the place where memalloc_nofs_restore() was added so in this sense we really miss memalloc_nofs_restore() there... So what Tahsin did makes sense.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw
Quoted 50 lines · Jan Kara
Quoted 41 lines · Michal Hocko
Quoted 27 lines · Tahsin Erdogan

When a transaction starts, start_this_handle() saves current PF_MEMALLOC_NOFS value so that it can be restored at journal stop time. Journal restart is a special case that calls start_this_handle() without stopping the transaction. start_this_handle() isn't aware that the original value is already stored so it overwrites it with current value.

For instance, a call sequence like below leaves PF_MEMALLOC_NOFS flag set at the end:

jbd2_journal_start()
jbd2__journal_restart()
jbd2_journal_stop()

Make jbd2__journal_restart() restore the original value before calling start_this_handle().

Fixes 81378da64de6 ("jbd2: mark the transaction context with the scope GFP_NOFS context")
---
 fs/jbd2/transaction.c | 1 +
 1 file changed, 1 insertion(+)
fs/jbd2/transaction.c +0 -0
index 9ee4832b6f8b..dfd6afebdfeb 100644

fs/jbd2/transaction.c int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask) · line 686

Quoted 7 lines
fs/jbd2/transaction.c +1 -0
@@ -680,6 +680,7 @@ int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
 
 	rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
 	handle->h_buffer_credits = nblocks;
+	memalloc_nofs_restore(handle->saved_alloc_context);
 	ret = start_this_handle(journal, handle, gfp_mask);
 	return ret;
 }

I remember Jack has mentioned something about nested transaction back then when reviewing the patch. But I cannot remember or find a pointer to that email. I have a vague recollection that there is a reference counting for those transactions.

Quoted 7 lines

Anyway, Is this patch really correct? So let's say we are in the transaction context already and then you disable the scope NOFS protection, start_this_handle will allocate before it calls memalloc_nofs_save and that would recurse to the filesystem. If anything wouldn't it be better to simply call memalloc_nofs_save only if we start a new transaction? I thought we were doing that already but the code is so convoluted I have hard time to wrap my head around it.

Quoted 4 lines

I was thinking about his as well but the fact is jbd2__journal_restart() actually does equivalent of jbd2_journal_stop() for the handle above the place where memalloc_nofs_restore() was added so in this sense we really miss memalloc_nofs_restore() there... So what Tahsin did makes sense.

OK, I will trust you ;). This deserves a comment then, I believe.

Thanks!

Signature · 2 lines
Michal Hocko
SUSE Labs
#5 lore raw

PATCH v2 1/1 jbd2: preserve original nofs flag during journal restart

When a transaction starts, start_this_handle() saves current PF_MEMALLOC_NOFS value so that it can be restored at journal stop time. Journal restart is a special case that calls start_this_handle() without stopping the transaction. start_this_handle() isn't aware that the original value is already stored so it overwrites it with current value.

For instance, a call sequence like below leaves PF_MEMALLOC_NOFS flag set at the end:

jbd2_journal_start()
jbd2__journal_restart()
jbd2_journal_stop()

Make jbd2__journal_restart() restore the original value before calling start_this_handle().

Fixes 81378da64de6 ("jbd2: mark the transaction context with the scope GFP_NOFS context")

---
v2: added comment to jbd2__journal_restart()

 fs/jbd2/transaction.c | 2 ++
 1 file changed, 2 insertions(+)
fs/jbd2/transaction.c +2 -0
index 9ee4832b6f8b..05c0323493fb 100644
@@ -680,6 +680,8 @@ int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
 
 	rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
 	handle->h_buffer_credits = nblocks;
+	/* Restore original nofs flag as jbd2_journal_stop() would do. */
+	memalloc_nofs_restore(handle->saved_alloc_context);
 	ret = start_this_handle(journal, handle, gfp_mask);
 	return ret;
 }
Signature · 1 line
2.13.0.303.g4ebf302169-goog
#6 lore raw

On Wed 17-05-17 09:16:29, Tahsin Erdogan wrote:
[...]

fs/jbd2/transaction.c int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask) · line 683

Quoted 9 lines
fs/jbd2/transaction.c +1 -0
index 9ee4832b6f8b..05c0323493fb 100644
@@ -680,6 +680,8 @@ int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
 
 	rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
 	handle->h_buffer_credits = nblocks;
+	/* Restore original nofs flag as jbd2_journal_stop() would do. */

I was thinking something more specific like
/*
* Restore the original nofs context because the journal restart
* is basically the same thing as journal stop and start.
* start_this_handle will start a new nofs context. */

Quoted 7 lines
fs/jbd2/transaction.c +1 -0
+	memalloc_nofs_restore(handle->saved_alloc_context);
 	ret = start_this_handle(journal, handle, gfp_mask);
 	return ret;
 }
Signature · 1 line
2.13.0.303.g4ebf302169-goog
Signature · 2 lines
Michal Hocko
SUSE Labs
#7 lore raw
Quoted 8 lines · Michal Hocko

On Wed 17-05-17 09:16:29, Tahsin Erdogan wrote:
[...]

Quoted 6 lines
fs/jbd2/transaction.c +0 -0
index 9ee4832b6f8b..05c0323493fb 100644
@@ -680,6 +680,8 @@ int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
 
Quoted 10 lines

fs/jbd2/transaction.c int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask) · line 683

Quoted 3 lines
fs/jbd2/transaction.c +1 -0
 	rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
 	handle->h_buffer_credits = nblocks;
+	/* Restore original nofs flag as jbd2_journal_stop() would do. */

I was thinking something more specific like
/*
* Restore the original nofs context because the journal restart
* is basically the same thing as journal stop and start.
* start_this_handle will start a new nofs context. */

Yeah, this is much better. Tahsin, please use Michal's version. Thanks!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#8 lore raw

PATCH v3 1/1 jbd2: preserve original nofs flag during journal restart

When a transaction starts, start_this_handle() saves current PF_MEMALLOC_NOFS value so that it can be restored at journal stop time. Journal restart is a special case that calls start_this_handle() without stopping the transaction. start_this_handle() isn't aware that the original value is already stored so it overwrites it with current value.

For instance, a call sequence like below leaves PF_MEMALLOC_NOFS flag set at the end:

jbd2_journal_start()
jbd2__journal_restart()
jbd2_journal_stop()

Make jbd2__journal_restart() restore the original value before calling start_this_handle().

Fixes 81378da64de6 ("jbd2: mark the transaction context with the scope GFP_NOFS context")

---
v3: Use Michal's suggested comment in jbd2__journal_restart()
v2: added comment to jbd2__journal_restart()

 fs/jbd2/transaction.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/jbd2/transaction.c +6 -0
index 9ee4832b6f8b..2d30a6da7013 100644
@@ -680,6 +680,12 @@ int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
 
 	rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
 	handle->h_buffer_credits = nblocks;
+	/*
+	 * Restore the original nofs context because the journal restart
+	 * is basically the same thing as journal stop and start.
+	 * start_this_handle will start a new nofs context.
+	 */
+	memalloc_nofs_restore(handle->saved_alloc_context);
 	ret = start_this_handle(journal, handle, gfp_mask);
 	return ret;
 }
Signature · 1 line
2.13.0.303.g4ebf302169-goog
maintainer Applied
#9 lore raw
Quoted 13 lines · Tahsin Erdogan

When a transaction starts, start_this_handle() saves current PF_MEMALLOC_NOFS value so that it can be restored at journal stop time. Journal restart is a special case that calls start_this_handle() without stopping the transaction. start_this_handle() isn't aware that the original value is already stored so it overwrites it with current value.

For instance, a call sequence like below leaves PF_MEMALLOC_NOFS flag set at the end:

jbd2_journal_start()
jbd2__journal_restart()
jbd2_journal_stop()

Quoted 6 lines

Make jbd2__journal_restart() restore the original value before calling start_this_handle().

Fixes 81378da64de6 ("jbd2: mark the transaction context with the scope GFP_NOFS context")

Thanks, applied.

- Ted