exfat: use kvmalloc_array/kvfree instead of kmalloc_array/kfree
Subsystems (2)
PATCH 1/1 exfat: use kvmalloc_array/kvfree instead of kmalloc_array/kfree
The call stack shown below is a scenario in the Linux 4.19 kernel. Allocating memory failed where exfat fs use kmalloc_array due to system memory fragmentation, while the u-disk was inserted without recognition.
Devices such as u-disk using the exfat file system are pluggable and may be insert into the system at any time.
However, long-term running systems cannot guarantee the continuity of physical memory. Therefore, it's necessary to address this issue.
Binder:2632_6: page allocation failure: order:4, mode:0x6040c0(GFP_KERNEL|__GFP_COMP), nodemask=(null)
Call trace: [242178.097582] dump_backtrace+0x0/0x4 [242178.097589] dump_stack+0xf4/0x134 [242178.097598] warn_alloc+0xd8/0x144 [242178.097603] __alloc_pages_nodemask+0x1364/0x1384 [242178.097608] kmalloc_order+0x2c/0x510 [242178.097612] kmalloc_order_trace+0x40/0x16c [242178.097618] __kmalloc+0x360/0x408 [242178.097624] load_alloc_bitmap+0x160/0x284 [242178.097628] exfat_fill_super+0xa3c/0xe7c [242178.097635] mount_bdev+0x2e8/0x3a0 [242178.097638] exfat_fs_mount+0x40/0x50 [242178.097643] mount_fs+0x138/0x2e8 [242178.097649] vfs_kern_mount+0x90/0x270 [242178.097655] do_mount+0x798/0x173c [242178.097659] ksys_mount+0x114/0x1ac [242178.097665] __arm64_sys_mount+0x24/0x34 [242178.097671] el0_svc_common+0xb8/0x1b8 [242178.097676] el0_svc_handler+0x74/0x90 [242178.097681] el0_svc+0x8/0x340
By analyzing the exfat code,we found that continuous physical memory is not required here,so kvmalloc_array is used can solve this problem.
--- fs/exfat/balloc.c | 4 ++-- fs/exfat/dir.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
fs/exfat/balloc.c +2 -2
@@ -69,7 +69,7 @@ static int exfat_allocate_bitmap(struct super_block *sb, } sbi->map_sectors = ((need_map_size -1 ) >> (sb->s_blocksize_bits)) +1 ; - sbi->vol_amap =kmalloc_array (sbi->map_sectors, + sbi->vol_amap =kvmalloc_array (sbi->map_sectors,sizeof (struct buffer_head *),GFP_KERNEL );if (!sbi->vol_amap)return -ENOMEM ;
@@ -84,7 +84,7 @@ static int exfat_allocate_bitmap(struct super_block *sb,while (j < i)brelse (sbi->vol_amap[j++]); -kfree (sbi->vol_amap); +kvfree (sbi->vol_amap); sbi->vol_amap =NULL ;return -EIO ; }
fs/exfat/dir.c +2 -2
@@ -649,7 +649,7 @@ int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync)brelse (es->bh[i]);if (IS_DYNAMIC_ES (es)) -kfree (es->bh); +kvfree (es->bh);return err; }
@@ -888,7 +888,7 @@ int exfat_get_dentry_set(struct exfat_entry_set_cache *es, num_bh =EXFAT_B_TO_BLK_ROUND_UP (off + num_entries *DENTRY_SIZE , sb);if (num_bh >ARRAY_SIZE (es->__bh)) { - es->bh =kmalloc_array (num_bh,sizeof (*es->bh),GFP_KERNEL ); + es->bh =kvmalloc_array (num_bh,sizeof (*es->bh),GFP_KERNEL );if (!es->bh) {brelse (bh);return -ENOMEM ;
Signature · 1 line
2.17.1
Namjae Jeon
maintainer2023-07-05 18:15 GMT+09:00, gaoming <[email protected]>:
Quoted 78 lines
The call stack shown below is a scenario in the Linux 4.19 kernel. Allocating memory failed where exfat fs use
kmalloc_arraydue to system memory fragmentation, while the u-disk was inserted without recognition.
Devices such as u-disk using the exfat file system are pluggable and may be insert into the system at any time.
However, long-term running systems cannot guarantee the continuity of physical memory. Therefore, it's necessary to address this issue.Binder:2632_6: page allocation failure: order:4, mode:0x6040c0(GFP_KERNEL|__GFP_COMP), nodemask=(null)
Call trace: [242178.097582] dump_backtrace+0x0/0x4 [242178.097589] dump_stack+0xf4/0x134 [242178.097598] warn_alloc+0xd8/0x144 [242178.097603] __alloc_pages_nodemask+0x1364/0x1384 [242178.097608] kmalloc_order+0x2c/0x510 [242178.097612] kmalloc_order_trace+0x40/0x16c [242178.097618] __kmalloc+0x360/0x408 [242178.097624] load_alloc_bitmap+0x160/0x284 [242178.097628] exfat_fill_super+0xa3c/0xe7c [242178.097635] mount_bdev+0x2e8/0x3a0 [242178.097638] exfat_fs_mount+0x40/0x50 [242178.097643] mount_fs+0x138/0x2e8 [242178.097649] vfs_kern_mount+0x90/0x270 [242178.097655] do_mount+0x798/0x173c [242178.097659] ksys_mount+0x114/0x1ac [242178.097665] __arm64_sys_mount+0x24/0x34 [242178.097671] el0_svc_common+0xb8/0x1b8 [242178.097676] el0_svc_handler+0x74/0x90 [242178.097681] el0_svc+0x8/0x340By analyzing the exfat code,we found that continuous physical memory is not required here,so
kvmalloc_arrayis used can solve this problem.Signed-off-by gaoming <[email protected]>--- fs/exfat/balloc.c | 4 ++-- fs/exfat/dir.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)fs/exfat/balloc.c +2
-2@@ -69,7 +69,7 @@ static int exfat_allocate_bitmap(struct super_block *sb, } sbi->map_sectors = ((need_map_size -1 ) >> (sb->s_blocksize_bits)) +1 ; - sbi->vol_amap =kmalloc_array (sbi->map_sectors, + sbi->vol_amap =kvmalloc_array (sbi->map_sectors,sizeof (struct buffer_head *),GFP_KERNEL );if (!sbi->vol_amap)return -ENOMEM ;@@ -84,7 +84,7 @@ static int exfat_allocate_bitmap(struct super_block *sb,while (j < i)brelse (sbi->vol_amap[j++]); -kfree (sbi->vol_amap); +kvfree (sbi->vol_amap); sbi->vol_amap =NULL ;return -EIO ; }fs/exfat/dir.c +1
-1@@ -649,7 +649,7 @@ int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync)brelse (es->bh[i]);if (IS_DYNAMIC_ES (es)) -kfree (es->bh); +kvfree (es->bh);return err;}
fs/exfat/dir.c int exfat_get_dentry_set(struct exfat_entry_set_cache · line 892
Quoted 6 lines
fs/exfat/dir.c +1
-1@@ -888,7 +888,7 @@ int exfat_get_dentry_set(struct exfat_entry_set_cache *es, num_bh =EXFAT_B_TO_BLK_ROUND_UP (off + num_entries *DENTRY_SIZE , sb);if (num_bh >ARRAY_SIZE (es->__bh)) { - es->bh =kmalloc_array (num_bh,sizeof (*es->bh),GFP_KERNEL ); + es->bh =kvmalloc_array (num_bh,sizeof (*es->bh),GFP_KERNEL );
Could you please elaborate why you change this to kvmalloc_array also?
Thanks.
Quoted 7 lines
fs/exfat/dir.c +0
-0if (!es->bh) {brelse (bh);return
-ENOMEM;
--
2.17.1
exfat_get_dentry_set could be called after the u-disk have been inserted, through exfat_find, __exfat_write_inode functions.
This could happen at any time, which scenario can not guarantee the continuity of physical memory.
This bugfix will enhance the robustness of exfat.
Thanks.
-----邮件原件-----
发件人: Namjae Jeon <[email protected]>
发送时间: 2023年7月7日 7:10
收件人: gaoming <[email protected]>
抄送: Sungjong Seo <[email protected]>; open list:EXFAT FILE SYSTEM <[email protected]>; open list <[email protected]>; fengbaopeng <[email protected]>; gaoxu <[email protected]>; wangfei 00014658 <[email protected]>; shenchen 00013118 <[email protected]> 主题: Re: [PATCH] exfat: use kvmalloc_array/kvfree instead of kmalloc_array/kfree
2023-07-05 18:15 GMT+09:00, gaoming <[email protected]>:
Quoted 71 lines
The call stack shown below is a scenario in the Linux 4.19 kernel. Allocating memory failed where exfat fs use
kmalloc_arraydue to system memory fragmentation, while the u-disk was inserted without recognition.
Devices such as u-disk using the exfat file system are pluggable and may be insert into the system at any time.
However, long-term running systems cannot guarantee the continuity of physical memory. Therefore, it's necessary to address this issue.Binder:2632_6: page allocation failure: order:4, mode:0x6040c0(GFP_KERNEL|__GFP_COMP), nodemask=(null) Call trace:
[242178.097582] dump_backtrace+0x0/0x4 [242178.097589] dump_stack+0xf4/0x134 [242178.097598] warn_alloc+0xd8/0x144 [242178.097603] __alloc_pages_nodemask+0x1364/0x1384 [242178.097608] kmalloc_order+0x2c/0x510 [242178.097612] kmalloc_order_trace+0x40/0x16c [242178.097618] __kmalloc+0x360/0x408 [242178.097624] load_alloc_bitmap+0x160/0x284 [242178.097628] exfat_fill_super+0xa3c/0xe7c [242178.097635] mount_bdev+0x2e8/0x3a0 [242178.097638] exfat_fs_mount+0x40/0x50 [242178.097643] mount_fs+0x138/0x2e8 [242178.097649] vfs_kern_mount+0x90/0x270 [242178.097655] do_mount+0x798/0x173c [242178.097659] ksys_mount+0x114/0x1ac [242178.097665] __arm64_sys_mount+0x24/0x34 [242178.097671] el0_svc_common+0xb8/0x1b8 [242178.097676] el0_svc_handler+0x74/0x90 [242178.097681] el0_svc+0x8/0x340By analyzing the exfat code,we found that continuous physical memory is not required here,so
kvmalloc_arrayis used can solve this problem.Signed-off-by gaoming <[email protected]>--- fs/exfat/balloc.c | 4 ++-- fs/exfat/dir.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)fs/exfat/balloc.c index renamed from fs/exfat/balloc.c +0
-0
9f42f25fab92..a183558cb7a0100644fs/exfat/balloc.c +2
-2@@ -69,7 +69,7 @@ static int exfat_allocate_bitmap(struct super_block *sb, } sbi->map_sectors = ((need_map_size -1 ) >> (sb->s_blocksize_bits)) +1 ; - sbi->vol_amap =kmalloc_array (sbi->map_sectors, + sbi->vol_amap =kvmalloc_array (sbi->map_sectors,sizeof (struct buffer_head *),GFP_KERNEL );if (!sbi->vol_amap)return -ENOMEM ;@@ -84,7 +84,7 @@ static int exfat_allocate_bitmap(struct super_block *sb,while (j < i)brelse (sbi->vol_amap[j++]); -kfree (sbi->vol_amap); +kvfree (sbi->vol_amap); sbi->vol_amap =NULL ;return -EIO ; }fs/exfat/dir.c index renamed from fs/exfat/dir.c +0
-0
957574180a5e..5cbb78d0a2a2100644fs/exfat/dir.c +1
-1@@ -649,7 +649,7 @@ int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync) brelse(es->bh[i]);if (IS_DYNAMIC_ES (es)) -kfree (es->bh); +kvfree (es->bh);return err;}
fs/exfat/dir.c int exfat_get_dentry_set(struct · line 892
Quoted 6 lines
fs/exfat/dir.c +1
-1@@ -888,7 +888,7 @@ int exfat_get_dentry_set(structexfat_entry_set_cache *es, num_bh =EXFAT_B_TO_BLK_ROUND_UP (off + num_entries *DENTRY_SIZE , sb);if (num_bh >ARRAY_SIZE (es->__bh)) { - es->bh =kmalloc_array (num_bh,sizeof (*es->bh),GFP_KERNEL ); + es->bh =kvmalloc_array (num_bh,sizeof (*es->bh),GFP_KERNEL );
Could you please elaborate why you change this to kvmalloc_array also?
Thanks.
Quoted 7 lines
fs/exfat/dir.c +0
-0if (!es->bh) {brelse (bh);return
-ENOMEM;
--
2.17.1
Namjae Jeon
maintainer2023-07-07 11:27 GMT+09:00, gaoming <[email protected]>:
Quoted 5 lines
exfat_get_dentry_setcould be called after the u-disk have been inserted, throughexfat_find,__exfat_write_inodefunctions.
This could happen at any time, which scenario can not guarantee the continuity of physical memory.
This bugfix will enhance the robustness of exfat.
I'm sorry, but I understood that it was changed even though you didn't find any particular problem. I think there will not be issues like allocation-bitmap allocation failures. I will delete this code and apply it. If you don't agree, please explain how much memory is allocated here.
Thanks.
Quoted 105 lines
Thanks.
-----邮件原件-----
发件人: Namjae Jeon <[email protected]>
发送时间: 2023年7月7日 7:10
收件人: gaoming <[email protected]>
抄送: Sungjong Seo <[email protected]>; open list:EXFAT FILE SYSTEM <[email protected]>; open list <[email protected]>; fengbaopeng <[email protected]>; gaoxu <[email protected]>; wangfei 00014658 <[email protected]>; shenchen 00013118 <[email protected]>
主题: Re: [PATCH] exfat: use kvmalloc_array/kvfree instead of kmalloc_array/kfree2023-07-05 18:15 GMT+09:00, gaoming <[email protected]>:
Quoted 73 lines
The call stack shown below is a scenario in the Linux 4.19 kernel. Allocating memory failed where exfat fs use
kmalloc_arraydue to system memory fragmentation, while the u-disk was inserted without recognition.
Devices such as u-disk using the exfat file system are pluggable and may be insert into the system at any time.
However, long-term running systems cannot guarantee the continuity of physical memory. Therefore, it's necessary to address this issue.Binder:2632_6: page allocation failure: order:4, mode:0x6040c0(GFP_KERNEL|__GFP_COMP), nodemask=(null) Call trace:
[242178.097582] dump_backtrace+0x0/0x4 [242178.097589] dump_stack+0xf4/0x134 [242178.097598] warn_alloc+0xd8/0x144 [242178.097603] __alloc_pages_nodemask+0x1364/0x1384 [242178.097608] kmalloc_order+0x2c/0x510 [242178.097612] kmalloc_order_trace+0x40/0x16c [242178.097618] __kmalloc+0x360/0x408 [242178.097624] load_alloc_bitmap+0x160/0x284 [242178.097628] exfat_fill_super+0xa3c/0xe7c [242178.097635] mount_bdev+0x2e8/0x3a0 [242178.097638] exfat_fs_mount+0x40/0x50 [242178.097643] mount_fs+0x138/0x2e8 [242178.097649] vfs_kern_mount+0x90/0x270 [242178.097655] do_mount+0x798/0x173c [242178.097659] ksys_mount+0x114/0x1ac [242178.097665] __arm64_sys_mount+0x24/0x34 [242178.097671] el0_svc_common+0xb8/0x1b8 [242178.097676] el0_svc_handler+0x74/0x90 [242178.097681] el0_svc+0x8/0x340By analyzing the exfat code,we found that continuous physical memory is not required here,so
kvmalloc_arrayis used can solve this problem.Signed-off-by gaoming <[email protected]>--- fs/exfat/balloc.c | 4 ++-- fs/exfat/dir.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)fs/exfat/balloc.c index renamed from fs/exfat/balloc.c +0
-0
9f42f25fab92..a183558cb7a0100644fs/exfat/balloc.c +1
-1@@ -69,7 +69,7 @@ static int exfat_allocate_bitmap(struct super_block *sb, } sbi->map_sectors = ((need_map_size -1 ) >> (sb->s_blocksize_bits)) +1 ; - sbi->vol_amap =kmalloc_array (sbi->map_sectors, + sbi->vol_amap =kvmalloc_array (sbi->map_sectors,sizeof (struct buffer_head *),GFP_KERNEL );if (!sbi->vol_amap)fs/exfat/balloc.c +1
-1return -ENOMEM ;@@ -84,7 +84,7 @@ static int exfat_allocate_bitmap(struct super_block *sb, while (j < i)brelse (sbi->vol_amap[j++]); -kfree (sbi->vol_amap); +kvfree (sbi->vol_amap); sbi->vol_amap =NULL ;return -EIO ;fs/exfat/balloc.c +0
-0}fs/exfat/dir.c index renamed from fs/exfat/dir.c +0
-0
957574180a5e..5cbb78d0a2a2100644fs/exfat/dir.c +1
-1@@ -649,7 +649,7 @@ int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync) brelse(es->bh[i]);if (IS_DYNAMIC_ES (es)) -kfree (es->bh); +kvfree (es->bh);return err;}
fs/exfat/dir.c int exfat_get_dentry_set(struct · line 892
Quoted 6 lines
fs/exfat/dir.c +1
-1@@ -888,7 +888,7 @@ int exfat_get_dentry_set(structexfat_entry_set_cache *es, num_bh =EXFAT_B_TO_BLK_ROUND_UP (off + num_entries *DENTRY_SIZE , sb);if (num_bh >ARRAY_SIZE (es->__bh)) { - es->bh =kmalloc_array (num_bh,sizeof (*es->bh),GFP_KERNEL ); + es->bh =kvmalloc_array (num_bh,sizeof (*es->bh),GFP_KERNEL );Could you please elaborate why you change this to
kvmalloc_arrayalso?Thanks.
Quoted 7 lines
fs/exfat/dir.c +0
-0if (!es->bh) {brelse (bh);return
-ENOMEM;
--
2.17.1
Ok. Just delete exfat_get_dentry_set modification.
We have checked the mem allocation size really not exceeds one page.
Thanks.
-----邮件原件-----
发件人: Namjae Jeon <[email protected]>
发送时间: 2023年7月7日 11:46
收件人: gaoming <[email protected]>
抄送: Sungjong Seo <[email protected]>; open list:EXFAT FILE SYSTEM <[email protected]>; open list <[email protected]>; fengbaopeng <[email protected]>; gaoxu <[email protected]>; wangfei 00014658 <[email protected]>; shenchen 00013118 <[email protected]> 主题: Re: 答复: [PATCH] exfat: use kvmalloc_array/kvfree instead of kmalloc_array/kfree
2023-07-07 11:27 GMT+09:00, gaoming <[email protected]>:
Quoted 5 lines
exfat_get_dentry_setcould be called after the u-disk have been inserted, throughexfat_find,__exfat_write_inodefunctions. This could happen at any time, which scenario can not guarantee the continuity of physical memory.
This bugfix will enhance the robustness of exfat.
I'm sorry, but I understood that it was changed even though you didn't find any particular problem. I think there will not be issues like allocation-bitmap allocation failures. I will delete this code and apply it. If you don't agree, please explain how much memory is allocated here. Thanks.
Quoted 106 lines
Thanks.
-----邮件原件-----
发件人: Namjae Jeon <[email protected]>
发送时间: 2023年7月7日 7:10
收件人: gaoming <[email protected]>
抄送: Sungjong Seo <[email protected]>; open list:EXFAT FILE SYSTEM <[email protected]>; open list <[email protected]>; fengbaopeng <[email protected]>; gaoxu <[email protected]>; wangfei
00014658 <[email protected]>; shenchen 00013118 <[email protected]>
主题: Re: [PATCH] exfat: use kvmalloc_array/kvfree instead of kmalloc_array/kfree2023-07-05 18:15 GMT+09:00, gaoming <[email protected]>:
Quoted 73 lines
The call stack shown below is a scenario in the Linux 4.19 kernel. Allocating memory failed where exfat fs use
kmalloc_arraydue to system memory fragmentation, while the u-disk was inserted without recognition.
Devices such as u-disk using the exfat file system are pluggable and may be insert into the system at any time.
However, long-term running systems cannot guarantee the continuity of physical memory. Therefore, it's necessary to address this issue.Binder:2632_6: page allocation failure: order:4, mode:0x6040c0(GFP_KERNEL|__GFP_COMP), nodemask=(null) Call trace:
[242178.097582] dump_backtrace+0x0/0x4 [242178.097589] dump_stack+0xf4/0x134 [242178.097598] warn_alloc+0xd8/0x144 [242178.097603] __alloc_pages_nodemask+0x1364/0x1384 [242178.097608] kmalloc_order+0x2c/0x510 [242178.097612] kmalloc_order_trace+0x40/0x16c [242178.097618] __kmalloc+0x360/0x408 [242178.097624] load_alloc_bitmap+0x160/0x284 [242178.097628] exfat_fill_super+0xa3c/0xe7c [242178.097635] mount_bdev+0x2e8/0x3a0 [242178.097638] exfat_fs_mount+0x40/0x50 [242178.097643] mount_fs+0x138/0x2e8 [242178.097649] vfs_kern_mount+0x90/0x270 [242178.097655] do_mount+0x798/0x173c [242178.097659] ksys_mount+0x114/0x1ac [242178.097665] __arm64_sys_mount+0x24/0x34 [242178.097671] el0_svc_common+0xb8/0x1b8 [242178.097676] el0_svc_handler+0x74/0x90 [242178.097681] el0_svc+0x8/0x340By analyzing the exfat code,we found that continuous physical memory is not required here,so
kvmalloc_arrayis used can solve this problem.Signed-off-by gaoming <[email protected]>--- fs/exfat/balloc.c | 4 ++-- fs/exfat/dir.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)fs/exfat/balloc.c index renamed from fs/exfat/balloc.c +0
-0
9f42f25fab92..a183558cb7a0100644fs/exfat/balloc.c +1
-1@@ -69,7 +69,7 @@ static int exfat_allocate_bitmap(struct super_block *sb, } sbi->map_sectors = ((need_map_size -1 ) >> (sb->s_blocksize_bits)) +1 ; - sbi->vol_amap =kmalloc_array (sbi->map_sectors, + sbi->vol_amap =kvmalloc_array (sbi->map_sectors,sizeof (struct buffer_head *),GFP_KERNEL );if (!sbi->vol_amap)fs/exfat/balloc.c +1
-1return -ENOMEM ;@@ -84,7 +84,7 @@ static int exfat_allocate_bitmap(struct super_block *sb, while (j < i)brelse (sbi->vol_amap[j++]); -kfree (sbi->vol_amap); +kvfree (sbi->vol_amap); sbi->vol_amap =NULL ;return -EIO ;fs/exfat/balloc.c +0
-0}fs/exfat/dir.c index renamed from fs/exfat/dir.c +0
-0
957574180a5e..5cbb78d0a2a2100644fs/exfat/dir.c +1
-1@@ -649,7 +649,7 @@ int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync) brelse(es->bh[i]);if (IS_DYNAMIC_ES (es)) -kfree (es->bh); +kvfree (es->bh);return err;}
fs/exfat/dir.c int exfat_get_dentry_set(struct · line 892
Quoted 6 lines
fs/exfat/dir.c +1
-1@@ -888,7 +888,7 @@ int exfat_get_dentry_set(structexfat_entry_set_cache *es, num_bh =EXFAT_B_TO_BLK_ROUND_UP (off + num_entries *DENTRY_SIZE , sb);if (num_bh >ARRAY_SIZE (es->__bh)) { - es->bh =kmalloc_array (num_bh,sizeof (*es->bh),GFP_KERNEL ); + es->bh =kvmalloc_array (num_bh,sizeof (*es->bh),GFP_KERNEL );Could you please elaborate why you change this to
kvmalloc_arrayalso?Thanks.
Quoted 7 lines
fs/exfat/dir.c +0
-0if (!es->bh) {brelse (bh);return
-ENOMEM;
--
2.17.1