Skip to contentSkip to conversation

ext4: scoped NOFS for the handle in nojournal mode

PATCH In review Theodore Ts'o 3 participants 5 messages Jul 21 – Sep 16

Subsystems (2)
lore
Share & tools
lore

Theodore Ts'o

author maintainer
#0 lore raw

cover ext4: scoped NOFS for the handle in nojournal mode

This patch series makes it safe to remove the GFP_NOFS flag when an active is handle and slightly optimized the data layout of the jbd2 handle.

Theodore Ts'o (2):
ext4: enable scoped NOFS when starting a handle in nojournal mode
jbd2: align h_type and h_line_no in the handle structure on byte
boundaries

 fs/ext4/ext4_jbd2.c  | 36 ++++++++++++++++++++++--------------
 fs/ext4/ext4_jbd2.h  |  6 +-----
 fs/jbd2/journal.c    |  1 +
 include/linux/jbd2.h | 11 ++++++-----
 4 files changed, 30 insertions(+), 24 deletions(-)

Changes in v3:
* Export ext4_handle_cache to fix a build break when ext4 is compiled as module
* Add an optimization in how h_type and h_line_no is laid out in the jbd2_journal_handle structure.
Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
* Remove an unused variable in ext4_get_nojournal()
* Add a safety check in case ext4_handle_valid() is passed a NULL pointer, even though the fucntion is documented that the caller must not be called with a NULL handle.
Link to v1: https://lore.kernel.org/r/[email protected]

Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/2 ext4: enable scoped NOFS when starting a handle in nojournal mode

The jbd2 layer enables NOFS mode using memalloc_nofs_{save,restore}() while a handle is active. We need to do the same in nojournal mode so that it is safe to remove GFP_NOFS flags while a jbd2 handle is active.

This will require that we actually allocate a real handle, but with an h_invalid flag set, so there is a place to put the saved memalloc context.

---
 fs/ext4/ext4_jbd2.c  | 36 ++++++++++++++++++++++--------------
 fs/ext4/ext4_jbd2.h  |  6 +-----
 fs/jbd2/journal.c    |  1 +
 include/linux/jbd2.h |  1 +
 4 files changed, 25 insertions(+), 19 deletions(-)
fs/ext4/ext4_jbd2.c +22 -14
index 9a8c225f2753..b4dacd1a89e7 100644
@@ -33,14 +33,22 @@ int ext4_inode_journal_mode(struct inode *inode)
 static handle_t *ext4_get_nojournal(void)
 {
 	handle_t *handle = current->journal_info;
-	unsigned long ref_cnt = (unsigned long)handle;
 
-	BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
-
-	ref_cnt++;
-	handle = (handle_t *)ref_cnt;
-
-	current->journal_info = handle;
+	BUG_ON(handle && !handle->h_invalid);
+
+	if (!handle) {
+		handle = jbd2_alloc_handle(GFP_NOFS);
+		if (!handle)
+			return ERR_PTR(-ENOMEM);
+		handle->h_invalid = 1;
+		/*
+		 * This is done by start_this_handle() if journalling
+		 * is enabled.
+		 */
+		handle->saved_alloc_context = memalloc_nofs_save();
+		current->journal_info = handle;
+	}
+	handle->h_ref++;
 	return handle;
 }
 
@@ -48,14 +56,14 @@ static handle_t *ext4_get_nojournal(void)
 /* Decrement the non-pointer handle value */
 static void ext4_put_nojournal(handle_t *handle)
 {
-	unsigned long ref_cnt = (unsigned long)handle;
+	BUG_ON(handle->h_ref == 0);
 
-	BUG_ON(ref_cnt == 0);
-
-	ref_cnt--;
-	handle = (handle_t *)ref_cnt;
-
-	current->journal_info = handle;
+	handle->h_ref--;
+	if (handle->h_ref == 0) {
+		memalloc_nofs_restore(handle->saved_alloc_context);
+		jbd2_free_handle(handle);
+		current->journal_info = NULL;
+	}
 }
 
 /*
fs/ext4/ext4_jbd2.h +1 -5
index 63d17c5201b5..2fbf48b3dfe2 100644
@@ -182,15 +182,11 @@ handle_t *__ext4_journal_start_sb(struct inode *inode, struct super_block *sb,
 				  int rsv_blocks, int revoke_creds);
 int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle);
 
-#define EXT4_NOJOURNAL_MAX_REF_COUNT ((unsigned long) 4096)
-
 /* Note:  Do not use this for NULL handles.  This is only to determine if
  * a properly allocated handle is using a journal or not. */
 static inline int ext4_handle_valid(handle_t *handle)
 {
-	if ((unsigned long)handle < EXT4_NOJOURNAL_MAX_REF_COUNT)
-		return 0;
-	return 1;
+	return (handle && !handle->h_invalid);
 }
 
 static inline void ext4_handle_sync(handle_t *handle)
fs/jbd2/journal.c +1 -0
index 4fdf089500f6..f0eab6151f48 100644
@@ -94,6 +94,7 @@ EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
 EXPORT_SYMBOL(jbd2_inode_cache);
+EXPORT_SYMBOL(jbd2_handle_cache);
 
 #ifdef CONFIG_JBD2_DEBUG
 void __jbd2_debug(int level, const char *file, const char *func,
include/linux/jbd2.h +1 -0
index b68561187e90..7348fdadc810 100644
@@ -513,6 +513,7 @@ struct jbd2_journal_handle
 	unsigned int	h_sync:		1;
 	unsigned int	h_reserved:	1;
 	unsigned int	h_aborted:	1;
+	unsigned int	h_invalid:	1;
 	unsigned int	h_type:		8;
 	unsigned int	h_line_no:	16;
 
Signature · 1 line
2.53.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] ext4: enable scoped NOFS when starting a handle in nojournal mode

Quoted 11 lines · Theodore Ts'o

The jbd2 layer enables NOFS mode using memalloc_nofs_{save,restore}() while a handle is active. We need to do the same in nojournal mode so that it is safe to remove GFP_NOFS flags while a jbd2 handle is active.

This will require that we actually allocate a real handle, but with an h_invalid flag set, so there is a place to put the saved memalloc context.

Reviewed-by Andreas Dilger <[email protected] <mailto:[email protected]>>

Cheers, Andreas

#3 lore raw

Re: [PATCH 1/2] ext4: enable scoped NOFS when starting a handle in nojournal mode

In reply to Theodore Ts'o
Quoted 11 lines · Theodore Ts'o

The jbd2 layer enables NOFS mode using memalloc_nofs_{save,restore}() while a handle is active. We need to do the same in nojournal mode so that it is safe to remove GFP_NOFS flags while a jbd2 handle is active.

This will require that we actually allocate a real handle, but with an h_invalid flag set, so there is a place to put the saved memalloc context.

---

...

include/linux/jbd2.h struct jbd2_journal_handle · line 518

Quoted 11 lines
include/linux/jbd2.h +1 -0
index b68561187e90..7348fdadc810 100644
@@ -513,6 +513,7 @@ struct jbd2_journal_handle
  	unsigned int	h_sync:		1;
  	unsigned int	h_reserved:	1;
  	unsigned int	h_aborted:	1;
+	unsigned int	h_invalid:	1;
  	unsigned int	h_type:		8;
  	unsigned int	h_line_no:	16;

Hi!

FYI, this seems to cause a warning during "make htmldocs" now:

 WARNING: .../include/linux/jbd2.h:523 struct member 'h_invalid' not 

described in 'jbd2_journal_handle'
Could you maybe add a line to the kerneldoc comment?

Thanks,
Thomas

author maintainer
#4 lore raw

PATCH 2/2 jbd2: align h_type and h_line_no in the handle structure on byte boundaries

In reply to Theodore Ts'o

This makes starting handles a little more efficient, since it avoids requiring bitshifts when setting or getting the h_type and h_line_no fields in the jbd2_journal_handle structure.

---
 include/linux/jbd2.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
include/linux/jbd2.h +6 -6
index 7348fdadc810..1b42fe47c26b 100644
@@ -510,12 +510,12 @@ struct jbd2_journal_handle
 	int			h_err;
 
 	/* Flags [no locking] */
-	unsigned int	h_sync:		1;
-	unsigned int	h_reserved:	1;
-	unsigned int	h_aborted:	1;
-	unsigned int	h_invalid:	1;
-	unsigned int	h_type:		8;
-	unsigned int	h_line_no:	16;
+	unsigned char	h_sync:		1;
+	unsigned char	h_reserved:	1;
+	unsigned char	h_aborted:	1;
+	unsigned char	h_invalid:	1;
+	unsigned char	h_type;
+	unsigned short	h_line_no;
 
 	unsigned long		h_start_jiffies;
 	unsigned int		h_requested_credits;
Signature · 1 line
2.53.0