Re: [PATCH net-next v2] ice: Adjust over allocation of memory in ice_sched_add_root_node() and ice_sched_add_node()

From: Przemek Kitszel
Date: Mon Jul 08 2024 - 06:17:57 EST


On 7/6/24 16:05, Aleksandr Mishin wrote:
In ice_sched_add_root_node() and ice_sched_add_node() there are calls to
devm_kcalloc() in order to allocate memory for array of pointers to
'ice_sched_node' structure. But in this calls there are 'sizeof(*root)'
instead of 'sizeof(root)' and 'sizeof(*node)' instead of 'sizeof(node)'.
So memory is allocated for structures instead pointers. This lead to
significant over allocation of memory.

Adjust over allocation of memory by correcting devm_kcalloc() parameters.

Last three sentences are not correct.
Better commit message would be also more concise.


Found by Linux Verification Center (linuxtesting.org) with SVACE.

Suggested-by: Simon Horman <horms@xxxxxxxxxx>
Signed-off-by: Aleksandr Mishin <amishin@xxxxxxxxxx>
---
v2:
- Update comment, remove 'Fixes' tag and change the tree from 'net' to
'net-next' as suggested by Simon
(https://lore.kernel.org/all/20240706095258.GB1481495@xxxxxxxxxx/)
v1: https://lore.kernel.org/all/20240705163620.12429-1-amishin@xxxxxxxxxx/

drivers/net/ethernet/intel/ice/ice_sched.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/net/ethernet/intel/ice/ice_sched.c b/drivers/net/ethernet/intel/ice/ice_sched.c
index ecf8f5d60292..d8b6054f3436 100644
--- a/drivers/net/ethernet/intel/ice/ice_sched.c
+++ b/drivers/net/ethernet/intel/ice/ice_sched.c
@@ -28,9 +28,8 @@ ice_sched_add_root_node(struct ice_port_info *pi,
if (!root)
return -ENOMEM;
- /* coverity[suspicious_sizeof] */

good to clear that, thanks

root->children = devm_kcalloc(ice_hw_to_dev(hw), hw->max_children[0],
- sizeof(*root), GFP_KERNEL);
+ sizeof(root), GFP_KERNEL);

Your change makes code to use the correct type as sizeof() argument,
however, I would like to also make it the correct entity, so:
sizeof(*root->children) // == sizeof(root->children[0])

For the reference
562│ struct ice_sched_node {
...
565│ struct ice_sched_node **children;


if (!root->children) {
devm_kfree(ice_hw_to_dev(hw), root);
return -ENOMEM;
@@ -186,10 +185,9 @@ ice_sched_add_node(struct ice_port_info *pi, u8 layer,
if (!node)
return -ENOMEM;
if (hw->max_children[layer]) {
- /* coverity[suspicious_sizeof] */
node->children = devm_kcalloc(ice_hw_to_dev(hw),
hw->max_children[layer],
- sizeof(*node), GFP_KERNEL);
+ sizeof(node), GFP_KERNEL);

ditto

if (!node->children) {
devm_kfree(ice_hw_to_dev(hw), node);
return -ENOMEM;