* [PATCH] drivers/hv: Use kmalloc_array() instead of kmalloc()
@ 2025-11-04 12:16 Rahul Kumar
2025-11-07 6:52 ` Wei Liu
0 siblings, 1 reply; 2+ messages in thread
From: Rahul Kumar @ 2025-11-04 12:16 UTC (permalink / raw)
To: kys, haiyangz, wei.liu, decui
Cc: linux-hyperv, linux-kernel, linux-kernel-mentees, skhan,
rk0006818
Documentation/process/deprecated.rst recommends against the use of
kmalloc with dynamic size calculations due to the risk of overflow and
smaller allocation being made than the caller was expecting.
Replace kmalloc() with kmalloc_array() in hv_common.c to make the
intended allocation size clearer and avoid potential overflow issues.
The number of pages (pgcount) is bounded, so overflow is not a
practical concern here. However, using kmalloc_array() better reflects
the intent to allocate an array and improves consistency with other
allocations.
No functional change intended.
Signed-off-by: Rahul Kumar <rk0006818@gmail.com>
---
drivers/hv/hv_common.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/hv/hv_common.c b/drivers/hv/hv_common.c
index e109a620c83f..68689beb383c 100644
--- a/drivers/hv/hv_common.c
+++ b/drivers/hv/hv_common.c
@@ -487,7 +487,7 @@ int hv_common_cpu_init(unsigned int cpu)
* online and then taken offline
*/
if (!*inputarg) {
- mem = kmalloc(pgcount * HV_HYP_PAGE_SIZE, flags);
+ mem = kmalloc_array(pgcount, HV_HYP_PAGE_SIZE, flags);
if (!mem)
return -ENOMEM;
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] drivers/hv: Use kmalloc_array() instead of kmalloc()
2025-11-04 12:16 [PATCH] drivers/hv: Use kmalloc_array() instead of kmalloc() Rahul Kumar
@ 2025-11-07 6:52 ` Wei Liu
0 siblings, 0 replies; 2+ messages in thread
From: Wei Liu @ 2025-11-07 6:52 UTC (permalink / raw)
To: Rahul Kumar
Cc: kys, haiyangz, wei.liu, decui, linux-hyperv, linux-kernel,
linux-kernel-mentees, skhan
On Tue, Nov 04, 2025 at 05:46:18PM +0530, Rahul Kumar wrote:
> Documentation/process/deprecated.rst recommends against the use of
> kmalloc with dynamic size calculations due to the risk of overflow and
> smaller allocation being made than the caller was expecting.
>
> Replace kmalloc() with kmalloc_array() in hv_common.c to make the
> intended allocation size clearer and avoid potential overflow issues.
>
> The number of pages (pgcount) is bounded, so overflow is not a
> practical concern here. However, using kmalloc_array() better reflects
> the intent to allocate an array and improves consistency with other
> allocations.
>
> No functional change intended.
>
> Signed-off-by: Rahul Kumar <rk0006818@gmail.com>
Applied to hyperv-next. Thanks.
> ---
> drivers/hv/hv_common.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/hv/hv_common.c b/drivers/hv/hv_common.c
> index e109a620c83f..68689beb383c 100644
> --- a/drivers/hv/hv_common.c
> +++ b/drivers/hv/hv_common.c
> @@ -487,7 +487,7 @@ int hv_common_cpu_init(unsigned int cpu)
> * online and then taken offline
> */
> if (!*inputarg) {
> - mem = kmalloc(pgcount * HV_HYP_PAGE_SIZE, flags);
> + mem = kmalloc_array(pgcount, HV_HYP_PAGE_SIZE, flags);
> if (!mem)
> return -ENOMEM;
>
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-11-07 6:52 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-04 12:16 [PATCH] drivers/hv: Use kmalloc_array() instead of kmalloc() Rahul Kumar
2025-11-07 6:52 ` Wei Liu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox