[PATCH] ubifs: vmalloc(array_size()) -> vmalloc_array()

Qianfeng Rong posted 1 patch 1 month, 2 weeks ago
fs/ubifs/lpt.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
[PATCH] ubifs: vmalloc(array_size()) -> vmalloc_array()
Posted by Qianfeng Rong 1 month, 2 weeks ago
Remove array_size() calls and replace vmalloc() with vmalloc_array() in
ubifs_create_dflt_lpt()/lpt_init_rd()/lpt_init_wr(). vmalloc_array() is
optimized better, resulting in less instructions being used [1].

[1]: https://lore.kernel.org/lkml/abc66ec5-85a4-47e1-9759-2f60ab111971@vivo.com/

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 fs/ubifs/lpt.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/fs/ubifs/lpt.c b/fs/ubifs/lpt.c
index 441d0beca4cf..dde0aa3287f4 100644
--- a/fs/ubifs/lpt.c
+++ b/fs/ubifs/lpt.c
@@ -628,8 +628,8 @@ int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first,
 	pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_KERNEL);
 	nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_KERNEL);
 	buf = vmalloc(c->leb_size);
-	ltab = vmalloc(array_size(sizeof(struct ubifs_lpt_lprops),
-				  c->lpt_lebs));
+	ltab = vmalloc_array(c->lpt_lebs,
+			     sizeof(struct ubifs_lpt_lprops));
 	if (!pnode || !nnode || !buf || !ltab || !lsave) {
 		err = -ENOMEM;
 		goto out;
@@ -1777,8 +1777,8 @@ static int lpt_init_rd(struct ubifs_info *c)
 {
 	int err, i;
 
-	c->ltab = vmalloc(array_size(sizeof(struct ubifs_lpt_lprops),
-				     c->lpt_lebs));
+	c->ltab = vmalloc_array(c->lpt_lebs,
+				sizeof(struct ubifs_lpt_lprops));
 	if (!c->ltab)
 		return -ENOMEM;
 
@@ -1846,8 +1846,8 @@ static int lpt_init_wr(struct ubifs_info *c)
 {
 	int err, i;
 
-	c->ltab_cmt = vmalloc(array_size(sizeof(struct ubifs_lpt_lprops),
-					 c->lpt_lebs));
+	c->ltab_cmt = vmalloc_array(c->lpt_lebs,
+				    sizeof(struct ubifs_lpt_lprops));
 	if (!c->ltab_cmt)
 		return -ENOMEM;
 
-- 
2.34.1
Re: [PATCH] ubifs: vmalloc(array_size()) -> vmalloc_array()
Posted by Zhihao Cheng 1 month, 2 weeks ago
在 2025/8/17 16:12, Qianfeng Rong 写道:
> Remove array_size() calls and replace vmalloc() with vmalloc_array() in
> ubifs_create_dflt_lpt()/lpt_init_rd()/lpt_init_wr(). vmalloc_array() is
> optimized better, resulting in less instructions being used [1].
> 
> [1]: https://lore.kernel.org/lkml/abc66ec5-85a4-47e1-9759-2f60ab111971@vivo.com/
> 
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> ---
>   fs/ubifs/lpt.c | 12 ++++++------
>   1 file changed, 6 insertions(+), 6 deletions(-)

Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
> 
> diff --git a/fs/ubifs/lpt.c b/fs/ubifs/lpt.c
> index 441d0beca4cf..dde0aa3287f4 100644
> --- a/fs/ubifs/lpt.c
> +++ b/fs/ubifs/lpt.c
> @@ -628,8 +628,8 @@ int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first,
>   	pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_KERNEL);
>   	nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_KERNEL);
>   	buf = vmalloc(c->leb_size);
> -	ltab = vmalloc(array_size(sizeof(struct ubifs_lpt_lprops),
> -				  c->lpt_lebs));
> +	ltab = vmalloc_array(c->lpt_lebs,
> +			     sizeof(struct ubifs_lpt_lprops));
>   	if (!pnode || !nnode || !buf || !ltab || !lsave) {
>   		err = -ENOMEM;
>   		goto out;
> @@ -1777,8 +1777,8 @@ static int lpt_init_rd(struct ubifs_info *c)
>   {
>   	int err, i;
>   
> -	c->ltab = vmalloc(array_size(sizeof(struct ubifs_lpt_lprops),
> -				     c->lpt_lebs));
> +	c->ltab = vmalloc_array(c->lpt_lebs,
> +				sizeof(struct ubifs_lpt_lprops));
>   	if (!c->ltab)
>   		return -ENOMEM;
>   
> @@ -1846,8 +1846,8 @@ static int lpt_init_wr(struct ubifs_info *c)
>   {
>   	int err, i;
>   
> -	c->ltab_cmt = vmalloc(array_size(sizeof(struct ubifs_lpt_lprops),
> -					 c->lpt_lebs));
> +	c->ltab_cmt = vmalloc_array(c->lpt_lebs,
> +				    sizeof(struct ubifs_lpt_lprops));
>   	if (!c->ltab_cmt)
>   		return -ENOMEM;
>   
> 

Re: [PATCH] ubifs: vmalloc(array_size()) -> vmalloc_array()
Posted by Richard Weinberger 1 month, 2 weeks ago
On Sun, Aug 17, 2025 at 10:12 AM Qianfeng Rong <rongqianfeng@vivo.com> wrote:
>
> Remove array_size() calls and replace vmalloc() with vmalloc_array() in
> ubifs_create_dflt_lpt()/lpt_init_rd()/lpt_init_wr(). vmalloc_array() is
> optimized better, resulting in less instructions being used [1].
>
> [1]: https://lore.kernel.org/lkml/abc66ec5-85a4-47e1-9759-2f60ab111971@vivo.com/
>
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> ---
>  fs/ubifs/lpt.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)

Makes sense. Do you want me to carry this through the ubifs tree or do you have
a distinct tree with all your vmalloc_array() patches?

-- 
Thanks,
//richard
Re: [PATCH] ubifs: vmalloc(array_size()) -> vmalloc_array()
Posted by Qianfeng Rong 1 month, 2 weeks ago
在 2025/8/18 21:49, Richard Weinberger 写道:
> [You don't often get email from richard.weinberger@gmail.com. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
>
> On Sun, Aug 17, 2025 at 10:12 AM Qianfeng Rong <rongqianfeng@vivo.com> wrote:
>> Remove array_size() calls and replace vmalloc() with vmalloc_array() in
>> ubifs_create_dflt_lpt()/lpt_init_rd()/lpt_init_wr(). vmalloc_array() is
>> optimized better, resulting in less instructions being used [1].
>>
>> [1]: https://lore.kernel.org/lkml/abc66ec5-85a4-47e1-9759-2f60ab111971@vivo.com/
>>
>> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
>> ---
>>   fs/ubifs/lpt.c | 12 ++++++------
>>   1 file changed, 6 insertions(+), 6 deletions(-)
> Makes sense. Do you want me to carry this through the ubifs tree or do you have
> a distinct tree with all your vmalloc_array() patches?

Please carry this through the ubifs tree.  Thanks for handling it!

>
> --
> Thanks,
> //richard
Best regards,
Qianfeng