ext4: Prevent memory reclaim from re-entering the filesystem and deadlocking
v2 cover ext4: Prevent memory reclaim from re-entering the filesystem and deadlocking
This series aims to prevent ext4_kvmalloc() from re-entering the filesystem and deadlocking. Although __vmalloc() doesn't support GFP_NOFS[1], all of the user of ext4_kvmalloc are using GFP_NOFS (e.g. fs/ext4/resize.c::add_new_gdb), causing the memory reclaim to re-enter the filesystem.
To fix this issue, use memalloc_nfs_save/restore() that get __vmalloc() to drop __GFP_FS.
[1] linux-tree/Documentation/core-api/gfp-mask-fs-io.rst
Changes since v1:
- Add Patch 1 to delete ext4_kvzvalloc() that is not used anywhere.
- Add Patch 2 to rename ext4_kvmalloc() to ext4_kvmalloc_nofs() and drop its flags argument since all the users of ext4_kvmalloc() are using GFP_NOFS
Thanks,
Naoto Kobayashi (3):
ext4: Delete ext4_kvzvalloc()
ext4: Rename ext4_kvmalloc() to ext4_kvmalloc_nofs() and drop its
flags argument
ext4: Prevent ext4_kvmalloc_nofs() from re-entering the filesystem and
deadlocking
fs/ext4/ext4.h | 3 +-- fs/ext4/resize.c | 10 ++++------ fs/ext4/super.c | 21 +++++++-------------- fs/ext4/xattr.c | 2 +- 4 files changed, 13 insertions(+), 23 deletions(-)
--
2.16.6
PATCH v2 1/3 ext4: Delete ext4_kvzvalloc()
Since we're not using ext4_kvzalloc(), delete this function.
--- fs/ext4/ext4.h | 1 - fs/ext4/super.c | 10 ---------- 2 files changed, 11 deletions(-)
fs/ext4/ext4.h +0 -1
@@ -2678,7 +2678,6 @@ extern int ext4_seq_options_show(struct seq_file *seq, void *offset);extern int ext4_calculate_overhead (struct super_block *sb);extern void ext4_superblock_csum_set (struct super_block *sb);extern void *ext4_kvmalloc (size_t size,gfp_t flags); -extern void *ext4_kvzalloc (size_t size,gfp_t flags);extern int ext4_alloc_flex_bg_array (struct super_block *sb,ext4_group_t ngroup); extern const char *ext4_decode_error(struct super_block *sb, int errno,
fs/ext4/super.c +0 -10
@@ -214,16 +214,6 @@ void *ext4_kvmalloc(size_t size, gfp_t flags)return ret; } -void *ext4_kvzalloc (size_t size,gfp_t flags) -{ -void *ret; - - ret =kzalloc (size, flags | __GFP_NOWARN); -if (!ret) - ret =__vmalloc (size, flags | __GFP_ZERO,PAGE_KERNEL ); -return ret; -} -ext4_fsblk_t ext4_block_bitmap (struct super_block *sb,struct ext4_group_desc *bg) {
--
2.16.6
Re: [PATCH v2 1/3] ext4: Delete ext4_kvzvalloc()
Quoted 3 lines · Naoto Kobayashi
Since we're not using
ext4_kvzalloc(), delete this function.Signed-off-by Naoto Kobayashi <[email protected]>
Looks good to me. You can add:
Honza
Quoted 41 lines
--- fs/ext4/ext4.h | 1 - fs/ext4/super.c | 10 ---------- 2 files changed, 11 deletions(-)fs/ext4/ext4.h +0
-1@@ -2678,7 +2678,6 @@ extern int ext4_seq_options_show(struct seq_file *seq, void *offset);extern int ext4_calculate_overhead (struct super_block *sb);extern void ext4_superblock_csum_set (struct super_block *sb);extern void *ext4_kvmalloc (size_t size,gfp_t flags); -extern void *ext4_kvzalloc (size_t size,gfp_t flags);extern int ext4_alloc_flex_bg_array (struct super_block *sb,ext4_group_t ngroup); extern const char *ext4_decode_error(struct super_block *sb, int errno,fs/ext4/super.c +0
-11@@ -214,16 +214,6 @@ void *ext4_kvmalloc(size_t size, gfp_t flags)return ret; } -void *ext4_kvzalloc (size_t size,gfp_t flags) -{ -void *ret; - - ret =kzalloc (size, flags | __GFP_NOWARN); -if (!ret) - ret =__vmalloc (size, flags | __GFP_ZERO,PAGE_KERNEL ); -return ret; -} -ext4_fsblk_t ext4_block_bitmap (struct super_block *sb,struct ext4_group_desc *bg) { --2.16.6
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH v2 1/3] ext4: Delete ext4_kvzvalloc()
In reply to Naoto KobayashiQuoted 3 lines · Naoto Kobayashi
Since we're not using
ext4_kvzalloc(), delete this function.Signed-off-by Naoto Kobayashi <[email protected]>
Thanks, applied.
- Ted
PATCH v2 2/3 ext4: Rename ext4_kvmalloc() to ext4_kvmalloc_nofs() and drop its flags argument
In reply to Naoto KobayashiRename ext4_kvmalloc() to ext4_kvmalloc_nofs() and drop its flags argument, because ext4_kvmalloc() callers must be under GFP_NOFS (otherwise, they should use generic kvmalloc() helper function).
--- fs/ext4/ext4.h | 2 +- fs/ext4/resize.c | 10 ++++------ fs/ext4/super.c | 6 +++--- fs/ext4/xattr.c | 2 +- 4 files changed, 9 insertions(+), 11 deletions(-)
fs/ext4/ext4.h +1 -1
@@ -2677,7 +2677,7 @@ extern struct buffer_head *ext4_sb_bread(struct super_block *sb,extern int ext4_seq_options_show (struct seq_file *seq,void *offset);extern int ext4_calculate_overhead (struct super_block *sb);extern void ext4_superblock_csum_set (struct super_block *sb); -extern void *ext4_kvmalloc (size_t size,gfp_t flags); +extern void *ext4_kvmalloc_nofs (size_t size);extern int ext4_alloc_flex_bg_array (struct super_block *sb,ext4_group_t ngroup); extern const char *ext4_decode_error(struct super_block *sb, int errno,
fs/ext4/resize.c +4 -6
@@ -824,9 +824,8 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,if (unlikely (err)) goto errout; - n_group_desc =ext4_kvmalloc ((gdb_num +1 ) * -sizeof (struct buffer_head *), -GFP_NOFS ); + n_group_desc =ext4_kvmalloc_nofs ((gdb_num +1 ) * +sizeof (struct buffer_head *));if (!n_group_desc) { err = -ENOMEM ; ext4_warning(sb, "not enough memory for %lu groups",
@@ -900,9 +899,8 @@ static int add_new_gdb_meta_bg(struct super_block *sb, gdb_bh =ext4_sb_bread (sb, gdblock,0 );if (IS_ERR (gdb_bh))return PTR_ERR (gdb_bh); - n_group_desc =ext4_kvmalloc ((gdb_num +1 ) * -sizeof (struct buffer_head *), -GFP_NOFS ); + n_group_desc =ext4_kvmalloc_nofs ((gdb_num +1 ) * +sizeof (struct buffer_head *));if (!n_group_desc) {brelse (gdb_bh); err = -ENOMEM ;
fs/ext4/super.c +3 -3
@@ -204,13 +204,13 @@ void ext4_superblock_csum_set(struct super_block *sb) es->s_checksum =ext4_superblock_csum (sb, es); } -void *ext4_kvmalloc (size_t size,gfp_t flags) +void *ext4_kvmalloc_nofs (size_t size) {void *ret; - ret =kmalloc (size, flags | __GFP_NOWARN); + ret =kmalloc (size,GFP_NOFS | __GFP_NOWARN);if (!ret) - ret =__vmalloc (size, flags,PAGE_KERNEL ); + ret =__vmalloc (size,GFP_NOFS ,PAGE_KERNEL );return ret; }
fs/ext4/xattr.c +1 -1
@@ -1456,7 +1456,7 @@ ext4_xattr_inode_cache_find(struct inode *inode, const void *value,if (!ce)return NULL ; - ea_data =ext4_kvmalloc (value_len,GFP_NOFS ); + ea_data =ext4_kvmalloc_nofs (value_len);if (!ea_data) {mb_cache_entry_put (ea_inode_cache, ce);return NULL ;
--
2.16.6
Jan Kara
reviewerRe: [PATCH v2 2/3] ext4: Rename ext4_kvmalloc() to ext4_kvmalloc_nofs() and drop its flags argument
Quoted 6 lines · Naoto Kobayashi
Rename
ext4_kvmalloc()toext4_kvmalloc_nofs()and drop its flags argument, becauseext4_kvmalloc()callers must be underGFP_NOFS(otherwise, they should use generickvmalloc()helper function).Signed-off-by Naoto Kobayashi <[email protected]>
Thanks for the patch but I don't think this or the following patch is actually needed. Did you ever see the deadlock with reclaim you mention in the initial message with a recent kernel? The reason is that in all three call sites of ext4_kvmalloc() in ext4 we have a transaction started (which is the reason for GFP_NOFS there after all) but since commit 81378da64de "jbd2: mark the transaction context with the scope GFP_NOFS context" the transaction machinery takes care of updating reclaim context as needed... So I'd be almost inclined to just drop 'flags' argument from ext4_kvmalloc() instead and if we ever create a callsite for which current memalloc_nofs machinery won't be enough, I'd rather extend that than randomly sprinkle GFP_NOFS flags in the code.
Honza
Quoted 85 lines
--- fs/ext4/ext4.h | 2 +- fs/ext4/resize.c | 10 ++++------ fs/ext4/super.c | 6 +++--- fs/ext4/xattr.c | 2 +- 4 files changed, 9 insertions(+), 11 deletions(-)fs/ext4/ext4.h +1
-1@@ -2677,7 +2677,7 @@ extern struct buffer_head *ext4_sb_bread(struct super_block *sb,extern int ext4_seq_options_show (struct seq_file *seq,void *offset);extern int ext4_calculate_overhead (struct super_block *sb);extern void ext4_superblock_csum_set (struct super_block *sb); -extern void *ext4_kvmalloc (size_t size,gfp_t flags); +extern void *ext4_kvmalloc_nofs (size_t size);extern int ext4_alloc_flex_bg_array (struct super_block *sb,ext4_group_t ngroup); extern const char *ext4_decode_error(struct super_block *sb, int errno,fs/ext4/resize.c +4
-6@@ -824,9 +824,8 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,if (unlikely (err)) goto errout; - n_group_desc =ext4_kvmalloc ((gdb_num +1 ) * -sizeof (struct buffer_head *), -GFP_NOFS ); + n_group_desc =ext4_kvmalloc_nofs ((gdb_num +1 ) * +sizeof (struct buffer_head *));if (!n_group_desc) { err = -ENOMEM ; ext4_warning(sb, "not enough memory for %lu groups",@@ -900,9 +899,8 @@ static int add_new_gdb_meta_bg(struct super_block *sb, gdb_bh =ext4_sb_bread (sb, gdblock,0 );if (IS_ERR (gdb_bh))return PTR_ERR (gdb_bh); - n_group_desc =ext4_kvmalloc ((gdb_num +1 ) * -sizeof (struct buffer_head *), -GFP_NOFS ); + n_group_desc =ext4_kvmalloc_nofs ((gdb_num +1 ) * +sizeof (struct buffer_head *));if (!n_group_desc) {brelse (gdb_bh); err = -ENOMEM ;fs/ext4/super.c +3
-3@@ -204,13 +204,13 @@ void ext4_superblock_csum_set(struct super_block *sb) es->s_checksum =ext4_superblock_csum (sb, es); } -void *ext4_kvmalloc (size_t size,gfp_t flags) +void *ext4_kvmalloc_nofs (size_t size) {void *ret; - ret =kmalloc (size, flags | __GFP_NOWARN); + ret =kmalloc (size,GFP_NOFS | __GFP_NOWARN);if (!ret) - ret =__vmalloc (size, flags,PAGE_KERNEL ); + ret =__vmalloc (size,GFP_NOFS ,PAGE_KERNEL );return ret; }fs/ext4/xattr.c +1
-2@@ -1456,7 +1456,7 @@ ext4_xattr_inode_cache_find(struct inode *inode, const void *value,if (!ce)return NULL ; - ea_data =ext4_kvmalloc (value_len,GFP_NOFS ); + ea_data =ext4_kvmalloc_nofs (value_len);if (!ea_data) {mb_cache_entry_put (ea_inode_cache, ce);return NULL ; --2.16.6
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH v2 2/3] ext4: Rename ext4_kvmalloc() to ext4_kvmalloc_nofs() and drop its flags argument
In reply to Naoto KobayashiQuoted 6 lines · Naoto Kobayashi
Rename
ext4_kvmalloc()toext4_kvmalloc_nofs()and drop its flags argument, becauseext4_kvmalloc()callers must be underGFP_NOFS(otherwise, they should use generickvmalloc()helper function).Signed-off-by Naoto Kobayashi <[email protected]>
Thanks, applied.
- Ted
Jan Kara
reviewerRe: [PATCH v2 2/3] ext4: Rename ext4_kvmalloc() to ext4_kvmalloc_nofs() and drop its flags argument
Quoted 9 lines · Theodore Y. Ts'o
Quoted 6 lines · Naoto Kobayashi
Rename
ext4_kvmalloc()toext4_kvmalloc_nofs()and drop its flags argument, becauseext4_kvmalloc()callers must be underGFP_NOFS(otherwise, they should use generickvmalloc()helper function).Signed-off-by Naoto Kobayashi <[email protected]>Thanks, applied.
Ted, I don't think this patch is needed at all - see my email [1]. Sadly Naoto didn't reply to my question whether he really saw any deadlock / lockdep splat or whether it was just a theoretical concern he had...
Honza
[1] https://lore.kernel.org/linux-ext4/[email protected]
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Theodore Y. Ts'o
maintainerRe: [PATCH v2 2/3] ext4: Rename ext4_kvmalloc() to ext4_kvmalloc_nofs() and drop its flags argument
Quoted 4 lines · Jan Kara
Ted, I don't think this patch is needed at all - see my email [1]. Sadly Naoto didn't reply to my question whether he really saw any deadlock / lockdep splat or whether it was just a theoretical concern he had...
Thanks, good point. So what we should do instead is just drop ext4_kvmalloc() entirely.
- Ted
Theodore Ts'o
maintainerPATCH 1/1 ext4: drop ext4_kvmalloc()
As Jan pointed out[1], as of commit 81378da64de ("jbd2: mark the transaction context with the scope GFP_NOFS context") we use memalloc_nofs_{save,restore}() while a jbd2 handle is active. So ext4_kvmalloc() so we can call allocate using GFP_NOFS is no longer necessary.
[1] https://lore.kernel.org/r/[email protected]
--- fs/ext4/ext4.h | 1 - fs/ext4/resize.c | 10 ++++------ fs/ext4/super.c | 10 ---------- fs/ext4/xattr.c | 2 +- 4 files changed, 5 insertions(+), 18 deletions(-)
fs/ext4/ext4.h +0 -1
@@ -2740,7 +2740,6 @@ extern struct buffer_head *ext4_sb_bread(struct super_block *sb,extern int ext4_seq_options_show (struct seq_file *seq,void *offset);extern int ext4_calculate_overhead (struct super_block *sb);extern void ext4_superblock_csum_set (struct super_block *sb); -extern void *ext4_kvmalloc (size_t size,gfp_t flags);extern int ext4_alloc_flex_bg_array (struct super_block *sb,ext4_group_t ngroup); extern const char *ext4_decode_error(struct super_block *sb, int errno,
fs/ext4/resize.c +4 -6
@@ -824,9 +824,8 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,if (unlikely (err)) goto errout; - n_group_desc =ext4_kvmalloc ((gdb_num +1 ) * -sizeof (struct buffer_head *), -GFP_NOFS ); + n_group_desc =kvmalloc ((gdb_num +1 ) *sizeof (struct buffer_head *), +GFP_KERNEL );if (!n_group_desc) { err = -ENOMEM ; ext4_warning(sb, "not enough memory for %lu groups",
@@ -900,9 +899,8 @@ static int add_new_gdb_meta_bg(struct super_block *sb, gdb_bh =ext4_sb_bread (sb, gdblock,0 );if (IS_ERR (gdb_bh))return PTR_ERR (gdb_bh); - n_group_desc =ext4_kvmalloc ((gdb_num +1 ) * -sizeof (struct buffer_head *), -GFP_NOFS ); + n_group_desc =kvmalloc ((gdb_num +1 ) *sizeof (struct buffer_head *), +GFP_KERNEL );if (!n_group_desc) {brelse (gdb_bh); err = -ENOMEM ;
fs/ext4/super.c +0 -10
@@ -204,16 +204,6 @@ void ext4_superblock_csum_set(struct super_block *sb) es->s_checksum =ext4_superblock_csum (sb, es); } -void *ext4_kvmalloc (size_t size,gfp_t flags) -{ -void *ret; - - ret =kmalloc (size, flags | __GFP_NOWARN); -if (!ret) - ret =__vmalloc (size, flags,PAGE_KERNEL ); -return ret; -} -ext4_fsblk_t ext4_block_bitmap (struct super_block *sb,struct ext4_group_desc *bg) {
fs/ext4/xattr.c +1 -1
@@ -1456,7 +1456,7 @@ ext4_xattr_inode_cache_find(struct inode *inode, const void *value,if (!ce)return NULL ; - ea_data =ext4_kvmalloc (value_len,GFP_NOFS ); + ea_data =kvmalloc (value_len,GFP_KERNEL );if (!ea_data) {mb_cache_entry_put (ea_inode_cache, ce);return NULL ;
Signature · 1 line
2.24.1
Re: [PATCH] ext4: drop ext4_kvmalloc()
Quoted 7 lines · Theodore Ts'o
As Jan pointed out[1], as of commit
81378da64de("jbd2: mark the transaction context with the scopeGFP_NOFScontext") we use memalloc_nofs_{save,restore}() while a jbd2 handle is active. Soext4_kvmalloc()so we can call allocate usingGFP_NOFSis no longer necessary.
Your signed-off-by is missing but otherwise the patch looks good to me. You can add:
Honza
Quoted 84 lines
--- fs/ext4/ext4.h | 1 - fs/ext4/resize.c | 10 ++++------ fs/ext4/super.c | 10 ---------- fs/ext4/xattr.c | 2 +- 4 files changed, 5 insertions(+), 18 deletions(-)fs/ext4/ext4.h +0
-1@@ -2740,7 +2740,6 @@ extern struct buffer_head *ext4_sb_bread(struct super_block *sb,extern int ext4_seq_options_show (struct seq_file *seq,void *offset);extern int ext4_calculate_overhead (struct super_block *sb);extern void ext4_superblock_csum_set (struct super_block *sb); -extern void *ext4_kvmalloc (size_t size,gfp_t flags);extern int ext4_alloc_flex_bg_array (struct super_block *sb,ext4_group_t ngroup); extern const char *ext4_decode_error(struct super_block *sb, int errno,fs/ext4/resize.c +4
-6@@ -824,9 +824,8 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,if (unlikely (err)) goto errout; - n_group_desc =ext4_kvmalloc ((gdb_num +1 ) * -sizeof (struct buffer_head *), -GFP_NOFS ); + n_group_desc =kvmalloc ((gdb_num +1 ) *sizeof (struct buffer_head *), +GFP_KERNEL );if (!n_group_desc) { err = -ENOMEM ; ext4_warning(sb, "not enough memory for %lu groups",@@ -900,9 +899,8 @@ static int add_new_gdb_meta_bg(struct super_block *sb, gdb_bh =ext4_sb_bread (sb, gdblock,0 );if (IS_ERR (gdb_bh))return PTR_ERR (gdb_bh); - n_group_desc =ext4_kvmalloc ((gdb_num +1 ) * -sizeof (struct buffer_head *), -GFP_NOFS ); + n_group_desc =kvmalloc ((gdb_num +1 ) *sizeof (struct buffer_head *), +GFP_KERNEL );if (!n_group_desc) {brelse (gdb_bh); err = -ENOMEM ;fs/ext4/super.c +0
-10@@ -204,16 +204,6 @@ void ext4_superblock_csum_set(struct super_block *sb) es->s_checksum =ext4_superblock_csum (sb, es); } -void *ext4_kvmalloc (size_t size,gfp_t flags) -{ -void *ret; - - ret =kmalloc (size, flags | __GFP_NOWARN); -if (!ret) - ret =__vmalloc (size, flags,PAGE_KERNEL ); -return ret; -} -ext4_fsblk_t ext4_block_bitmap (struct super_block *sb,struct ext4_group_desc *bg) {fs/ext4/xattr.c +1
-1@@ -1456,7 +1456,7 @@ ext4_xattr_inode_cache_find(struct inode *inode, const void *value,if (!ce)return NULL ; - ea_data =ext4_kvmalloc (value_len,GFP_NOFS ); + ea_data =kvmalloc (value_len,GFP_KERNEL );if (!ea_data) {mb_cache_entry_put (ea_inode_cache, ce);return NULL ;Signature · 1 line
2.24.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH] ext4: drop ext4_kvmalloc()
Quoted 7 lines · Jan Kara
Quoted 6 lines · Theodore Ts'o
As Jan pointed out[1], as of commit
81378da64de("jbd2: mark the transaction context with the scopeGFP_NOFScontext") we use memalloc_nofs_{save,restore}() while a jbd2 handle is active. Soext4_kvmalloc()so we can call allocate usingGFP_NOFSis no longer necessary.
Quoted 6 lines
Quoted 1 line
Your signed-off-by is missing but otherwise the patch looks good to me. You can add:
Reviewed-by Jan Kara <[email protected]>
Thanks, applied with my signed-off-by and a Link: trailer.
- Ted
PATCH v2 3/3 ext4: Prevent ext4_kvmalloc_nofs() from re-entering the filesystem and deadlocking
In reply to Naoto KobayashiEven if __vmalloc() receives GFP_NOFS, this function allocates data pages and auxiliary structures (e.g. pagetables) with __GFP_FS[1]. To prevent memory reclaim from re-entering the filesystem here and potentially deadlocking, use memalloc_nofs_save() that gets __vmalloc() to drop __GFP_FS.
[1] linux-tree/Documentation/core-api/gfp-mask-fs-io.rst
--- fs/ext4/super.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
fs/ext4/super.c +6 -3
@@ -43,6 +43,7 @@#include <linux/uaccess.h> #include <linux/iversion.h> #include <linux/unicode.h> +#include <linux/sched/mm.h> #include <linux/kthread.h> #include <linux/freezer.h>
@@ -206,11 +207,13 @@ void ext4_superblock_csum_set(struct super_block *sb)void *ext4_kvmalloc_nofs (size_t size) { +unsigned int nofs_flag;void *ret; - ret =kmalloc (size,GFP_NOFS | __GFP_NOWARN); -if (!ret) - ret =__vmalloc (size,GFP_NOFS ,PAGE_KERNEL ); +/* kvmalloc() does not support GFP_NOFS */ + nofs_flag =memalloc_nofs_save (); + ret =kvmalloc (size,GFP_KERNEL ); +memalloc_nofs_restore (nofs_flag);return ret; }
--
2.16.6