[PATCH] ice: parser: use array_size() for table allocation

Weimin Xiong posted 1 patch 1 week, 2 days ago
[PATCH] ice: parser: use array_size() for table allocation
Posted by Weimin Xiong 1 week, 2 days ago
Use array_size() when calculating the parser table allocation size so an
overflow in the firmware-provided item dimensions is detected before
allocation. Include the overflow helpers explicitly instead of relying on
an indirect include.

Signed-off-by: Weimin Xiong <xiongwm2026@163.com>
---
diff --git a/drivers/net/ethernet/intel/ice/ice_parser.c b/drivers/net/ethernet/intel/ice/ice_parser.c
index f8e69630f..c109d3c32 100644
--- a/drivers/net/ethernet/intel/ice/ice_parser.c
+++ b/drivers/net/ethernet/intel/ice/ice_parser.c
@@ -1,6 +1,8 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (C) 2024 Intel Corporation */
 
+#include <linux/overflow.h>
+
 #include "ice_common.h"
 
 struct ice_pkg_sect_hdr {
@@ -102,7 +104,7 @@ ice_parser_create_table(struct ice_hw *hw, u32 sect_type,
 	if (!seg)
 		return ERR_PTR(-EINVAL);
 
-	table = kzalloc(item_size * length, GFP_KERNEL);
+	table = kzalloc(array_size(item_size, length), GFP_KERNEL);
 	if (!table)
 		return ERR_PTR(-ENOMEM);
Re: [Intel-wired-lan] [PATCH] ice: parser: use array_size() for table allocation
Posted by Alexander Lobakin 5 days, 1 hour ago
From: Weimin Xiong <xiongwm2026@163.com>
Date: Thu, 16 Jul 2026 10:51:00 +0800

> Use array_size() when calculating the parser table allocation size so an
> overflow in the firmware-provided item dimensions is detected before
> allocation. Include the overflow helpers explicitly instead of relying on
> an indirect include.
> 
> Signed-off-by: Weimin Xiong <xiongwm2026@163.com>
> ---
> diff --git a/drivers/net/ethernet/intel/ice/ice_parser.c b/drivers/net/ethernet/intel/ice/ice_parser.c
> index f8e69630f..c109d3c32 100644
> --- a/drivers/net/ethernet/intel/ice/ice_parser.c
> +++ b/drivers/net/ethernet/intel/ice/ice_parser.c
> @@ -1,6 +1,8 @@
>  // SPDX-License-Identifier: GPL-2.0
>  /* Copyright (C) 2024 Intel Corporation */
>  
> +#include <linux/overflow.h>

This is redundant.

> +
>  #include "ice_common.h"
>  
>  struct ice_pkg_sect_hdr {
> @@ -102,7 +104,7 @@ ice_parser_create_table(struct ice_hw *hw, u32 sect_type,
>  	if (!seg)
>  		return ERR_PTR(-EINVAL);
>  
> -	table = kzalloc(item_size * length, GFP_KERNEL);
> +	table = kzalloc(array_size(item_size, length), GFP_KERNEL);

kcalloc(length, item_size) if you want to make it _proper_.

array_size() is used only when no array allocation helpers are
available, e.g. for dma_alloc_coherent(). For regular slab allocations,
we have a whole bunch of different array allocation helpers.

>  	if (!table)
>  		return ERR_PTR(-ENOMEM);

Thanks,
Olek
[PATCH v2] ice: parser: use kcalloc for table allocation
Posted by Weimin Xiong 4 days, 14 hours ago
From 376dfdfc788a853c79a87c4bfdd94dc1ea678c43 Mon Sep 17 00:00:00 2001
From: Weimin Xiong <xiongwm2026@163.com>
Date: Tue, 21 Jul 2026 09:46:46 +0800
Subject: [PATCH v2] ice: parser: use kcalloc for table allocation
To: intel-wired-lan@lists.osuosl.org
Cc: Alexander Lobakin <aleksander.lobakin@intel.com>,
	Tony Nguyen <anthony.l.nguyen@intel.com>,
	Przemek Kitszel <przemyslaw.kitszel@intel.com>,
	netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>
In-Reply-To: <28460003-47ee-4706-ad95-7fba87d36509@intel.com>
References: <20260716025100.118145-1-xiongwm2026@163.com>

Use kcalloc() when calculating the parser table allocation size so an
overflow in the firmware-provided item dimensions is detected before
allocation.

v2: Use kcalloc() instead of kzalloc(array_size()) as suggested by
    Alexander Lobakin <aleksander.lobakin@intel.com>

---

v1->v2:
  - Remove redundant include of <linux/overflow.h>
  - Use kcalloc() instead of kzalloc(array_size()) as suggested by
    Alexander Lobakin <aleksander.lobakin@intel.com>

Signed-off-by: Weimin Xiong <xiongwm2026@163.com>
---
 drivers/net/ethernet/intel/ice/ice_parser.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/intel/ice/ice_parser.c b/drivers/net/ethernet/intel/ice/ice_parser.c
index f8e69630f..f5ae6c071 100644
--- a/drivers/net/ethernet/intel/ice/ice_parser.c
+++ b/drivers/net/ethernet/intel/ice/ice_parser.c
@@ -102,7 +102,7 @@ ice_parser_create_table(struct ice_hw *hw, u32 sect_type,
 	if (!seg)
 		return ERR_PTR(-EINVAL);
 
-	table = kzalloc(item_size * length, GFP_KERNEL);
+	table = kcalloc(length, item_size, GFP_KERNEL);
 	if (!table)
 		return ERR_PTR(-ENOMEM);
 

base-commit: 95e24e90b55ce6d9d266ed6f20514f37c931c751
-- 
2.43.0

RE: [Intel-wired-lan] [PATCH v2] ice: parser: use kcalloc for table allocation
Posted by Loktionov, Aleksandr 2 days, 7 hours ago

> -----Original Message-----
> From: Intel-wired-lan <intel-wired-lan-bounces@osuosl.org> On Behalf
> Of Weimin Xiong
> Sent: Tuesday, July 21, 2026 3:58 AM
> To: intel-wired-lan@lists.osuosl.org
> Cc: Lobakin, Aleksander <aleksander.lobakin@intel.com>; Nguyen,
> Anthony L <anthony.l.nguyen@intel.com>; Kitszel, Przemyslaw
> <przemyslaw.kitszel@intel.com>; netdev@vger.kernel.org; linux-
> kernel@vger.kernel.org; andrew+netdev@lunn.ch; davem@davemloft.net;
> edumazet@google.com; kuba@kernel.org; pabeni@redhat.com
> Subject: [Intel-wired-lan] [PATCH v2] ice: parser: use kcalloc for
> table allocation
> 
> From 376dfdfc788a853c79a87c4bfdd94dc1ea678c43 Mon Sep 17 00:00:00 2001
> From: Weimin Xiong <xiongwm2026@163.com>
> Date: Tue, 21 Jul 2026 09:46:46 +0800
> Subject: [PATCH v2] ice: parser: use kcalloc for table allocation
> To: intel-wired-lan@lists.osuosl.org
> Cc: Alexander Lobakin <aleksander.lobakin@intel.com>,
> 	Tony Nguyen <anthony.l.nguyen@intel.com>,
> 	Przemek Kitszel <przemyslaw.kitszel@intel.com>,
> 	netdev@vger.kernel.org,
> 	linux-kernel@vger.kernel.org,
> 	Andrew Lunn <andrew+netdev@lunn.ch>,
> 	"David S. Miller" <davem@davemloft.net>,
> 	"Eric Dumazet" <edumazet@google.com>,
> 	Jakub Kicinski <kuba@kernel.org>,
> 	Paolo Abeni <pabeni@redhat.com>
> In-Reply-To: <28460003-47ee-4706-ad95-7fba87d36509@intel.com>
> References: <20260716025100.118145-1-xiongwm2026@163.com>
> 
> Use kcalloc() when calculating the parser table allocation size so an
> overflow in the firmware-provided item dimensions is detected before
> allocation.
> 
> v2: Use kcalloc() instead of kzalloc(array_size()) as suggested by
>     Alexander Lobakin <aleksander.lobakin@intel.com>
> 
> ---
> 
> v1->v2:
>   - Remove redundant include of <linux/overflow.h>
>   - Use kcalloc() instead of kzalloc(array_size()) as suggested by
>     Alexander Lobakin <aleksander.lobakin@intel.com>
> 
> Signed-off-by: Weimin Xiong <xiongwm2026@163.com>
> ---
>  drivers/net/ethernet/intel/ice/ice_parser.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/ethernet/intel/ice/ice_parser.c
> b/drivers/net/ethernet/intel/ice/ice_parser.c
> index f8e69630f..f5ae6c071 100644
> --- a/drivers/net/ethernet/intel/ice/ice_parser.c
> +++ b/drivers/net/ethernet/intel/ice/ice_parser.c
> @@ -102,7 +102,7 @@ ice_parser_create_table(struct ice_hw *hw, u32
> sect_type,
>  	if (!seg)
>  		return ERR_PTR(-EINVAL);
> 
> -	table = kzalloc(item_size * length, GFP_KERNEL);
> +	table = kcalloc(length, item_size, GFP_KERNEL);
>  	if (!table)
>  		return ERR_PTR(-ENOMEM);
> 
> 
> base-commit: 95e24e90b55ce6d9d266ed6f20514f37c931c751
> --
> 2.43.0

Reviewed-by: Aleksandr Loktionov <aleksandr.loktionov@intel.com>