ext4: enable scoped NOFS when starting a handle in nojournal mode
Subsystems (2)
Theodore Ts'o
maintainerPATCH 1/1 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 +----- include/linux/jbd2.h | 1 + 3 files changed, 24 insertions(+), 19 deletions(-)
fs/ext4/ext4_jbd2.c +22 -14
@@ -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
@@ -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)
include/linux/jbd2.h +1 -0
@@ -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
Quoted 9 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_NOFSflags while a jbd2 handle is active.This will require that we actually allocate a real handle, but with an
h_invalidflag set, so there is a place to put the saved memalloc context.
If you wanted to avoid an allocation for each nojournal handle on 64-bit systems, you could put a magic number in the high 32 bits of the handle pointer (which is unlikely to conflict with real memory addresses), and the PF_* flags in the low 32 bits.
While the memory savings would be minimal (32 bytes for minimum kmalloc), it would avoid a potential new allocation failure path for that mode.
include/linux/jbd2.h struct jbd2_journal_handle · line 518
Quoted 11 lines
include/linux/jbd2.h +1
-0@@ -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;
Not sure if it makes much difference, but should h_line_no be aligned on a 16-bit offset and h_type aligned on an 8-bit offset by adding an "unsigned int h_unused:4" field before h_type, or reorder them so the 1-bit fields are at the end? That would allow the integer fields to be accessed directly rather than also having to shift them.
Cheers, Andreas
Theodore Tso
maintainerQuoted 4 lines · Andreas Dilger
If you wanted to avoid an allocation for each nojournal handle on 64-bit systems, you could put a magic number in the high 32 bits of the handle pointer (which is unlikely to conflict with real memory addresses), and the PF_* flags in the low 32 bits.
That's an optimization we could do later, if we think the complexity overhead is worth it.
Quoted 5 lines
Quoted 5 lines
include/linux/jbd2.h +1
-0@@ -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;
Quoted 8 lines
include/linux/jbd2.h struct jbd2_journal_handle · line 518
Quoted 2 lines
include/linux/jbd2.h +0
-0unsigned int h_type: 8; unsigned int h_line_no: 16;Not sure if it makes much difference, but should
h_line_nobe aligned on a 16-bit offset andh_typealigned on an 8-bit offset by adding an "unsigned int h_unused:4" field beforeh_type, or reorder them so the 1-bit fields are at the end? That would allow the integer fields to be accessed directly rather than also having to shift them.
Both gcc and clang support the extension of supporting bitfields on char and shorts, so we can do this:
- 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;
- Ted