[PATCH] PM: hibernate: Use vmalloc_array() and vcalloc() to improve code

Qianfeng Rong posted 1 patch 1 month, 2 weeks ago
kernel/power/swap.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
[PATCH] PM: hibernate: Use vmalloc_array() and vcalloc() to improve code
Posted by Qianfeng Rong 1 month, 2 weeks ago
Remove array_size() calls and replace vmalloc() and vzalloc() with
vmalloc_array() and vcalloc() respectively to simplify the code in
save_compressed_image() and load_compressed_image().  vmalloc_array()
is also optimized better, resulting in less instructions being used,
and vmalloc_array() handling overflow is more concise [1].

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

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 kernel/power/swap.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/power/swap.c b/kernel/power/swap.c
index ad13c461b657..0beff7eeaaba 100644
--- a/kernel/power/swap.c
+++ b/kernel/power/swap.c
@@ -712,7 +712,7 @@ static int save_compressed_image(struct swap_map_handle *handle,
 		goto out_clean;
 	}
 
-	data = vzalloc(array_size(nr_threads, sizeof(*data)));
+	data = vcalloc(nr_threads, sizeof(*data));
 	if (!data) {
 		pr_err("Failed to allocate %s data\n", hib_comp_algo);
 		ret = -ENOMEM;
@@ -1225,14 +1225,14 @@ static int load_compressed_image(struct swap_map_handle *handle,
 	nr_threads = num_online_cpus() - 1;
 	nr_threads = clamp_val(nr_threads, 1, CMP_THREADS);
 
-	page = vmalloc(array_size(CMP_MAX_RD_PAGES, sizeof(*page)));
+	page = vmalloc_array(CMP_MAX_RD_PAGES, sizeof(*page));
 	if (!page) {
 		pr_err("Failed to allocate %s page\n", hib_comp_algo);
 		ret = -ENOMEM;
 		goto out_clean;
 	}
 
-	data = vzalloc(array_size(nr_threads, sizeof(*data)));
+	data = vcalloc(nr_threads, sizeof(*data));
 	if (!data) {
 		pr_err("Failed to allocate %s data\n", hib_comp_algo);
 		ret = -ENOMEM;
-- 
2.34.1
Re: [PATCH] PM: hibernate: Use vmalloc_array() and vcalloc() to improve code
Posted by Rafael J. Wysocki 1 month, 1 week ago
On Sun, Aug 17, 2025 at 10:36 AM Qianfeng Rong <rongqianfeng@vivo.com> wrote:
>
> Remove array_size() calls and replace vmalloc() and vzalloc() with
> vmalloc_array() and vcalloc() respectively to simplify the code in
> save_compressed_image() and load_compressed_image().  vmalloc_array()
> is also optimized better, resulting in less instructions being used,
> and vmalloc_array() handling overflow is more concise [1].
>
> [1]: https://lore.kernel.org/lkml/abc66ec5-85a4-47e1-9759-2f60ab111971@vivo.com/
>
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> ---
>  kernel/power/swap.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/power/swap.c b/kernel/power/swap.c
> index ad13c461b657..0beff7eeaaba 100644
> --- a/kernel/power/swap.c
> +++ b/kernel/power/swap.c
> @@ -712,7 +712,7 @@ static int save_compressed_image(struct swap_map_handle *handle,
>                 goto out_clean;
>         }
>
> -       data = vzalloc(array_size(nr_threads, sizeof(*data)));
> +       data = vcalloc(nr_threads, sizeof(*data));
>         if (!data) {
>                 pr_err("Failed to allocate %s data\n", hib_comp_algo);
>                 ret = -ENOMEM;
> @@ -1225,14 +1225,14 @@ static int load_compressed_image(struct swap_map_handle *handle,
>         nr_threads = num_online_cpus() - 1;
>         nr_threads = clamp_val(nr_threads, 1, CMP_THREADS);
>
> -       page = vmalloc(array_size(CMP_MAX_RD_PAGES, sizeof(*page)));
> +       page = vmalloc_array(CMP_MAX_RD_PAGES, sizeof(*page));
>         if (!page) {
>                 pr_err("Failed to allocate %s page\n", hib_comp_algo);
>                 ret = -ENOMEM;
>                 goto out_clean;
>         }
>
> -       data = vzalloc(array_size(nr_threads, sizeof(*data)));
> +       data = vcalloc(nr_threads, sizeof(*data));
>         if (!data) {
>                 pr_err("Failed to allocate %s data\n", hib_comp_algo);
>                 ret = -ENOMEM;
> --

Applied as 6.18 material with some edits in the changelog, thanks!