Skip to contentSkip to conversation

nfp: flower: Added pointer check and continue

PATCH In review Denis Arefev 3 participants 3 messages Nov 18, 2022 – Nov 22, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 nfp: flower: Added pointer check and continue

Return value of a function 'kmalloc_array' is dereferenced at lag_conf.c:347 without checking for null,
but it is usually checked for this function.

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

Fixes bb9a8d031140 ("nfp: flower: monitor and offload LAG groups")
---
 drivers/net/ethernet/netronome/nfp/flower/lag_conf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/ethernet/netronome/nfp/flower/lag_conf.c +5 -1
index 63907aeb3884..1aaec4cb9f55 100644
@@ -276,7 +276,7 @@ static void nfp_fl_lag_do_work(struct work_struct *work)
 
 	mutex_lock(&lag->lock);
 	list_for_each_entry_safe(entry, storage, &lag->group_list, list) {
-		struct net_device *iter_netdev, **acti_netdevs;
+		struct net_device *iter_netdev, **acti_netdevs = NULL;
 		struct nfp_flower_repr_priv *repr_priv;
 		int active_count = 0, slaves = 0;
 		struct nfp_repr *repr;
@@ -308,6 +308,10 @@ static void nfp_fl_lag_do_work(struct work_struct *work)
 
 		acti_netdevs = kmalloc_array(entry->slave_cnt,
 					     sizeof(*acti_netdevs), GFP_KERNEL);
+		if (!acti_netdevs) {
+			schedule_delayed_work(&lag->work, NFP_FL_LAG_DELAY);
+			continue;
+		}
 
 		/* Include sanity check in the loop. It may be that a bond has
 		 * changed between processing the last notification and the
Signature · 1 line
2.25.1
#1 lore raw
Quoted 16 lines · Denis Arefev

Return value of a function 'kmalloc_array' is dereferenced at lag_conf.c:347 without checking for null,
but it is usually checked for this function.

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

Fixes bb9a8d031140 ("nfp: flower: monitor and offload LAG groups")
---
 drivers/net/ethernet/netronome/nfp/flower/lag_conf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/ethernet/netronome/nfp/flower/lag_conf.c +0 -0
index 63907aeb3884..1aaec4cb9f55 100644

drivers/net/ethernet/netronome/nfp/flower/lag_conf.c static void nfp_fl_lag_do_work(struct work_struct *work) · line 279

Quoted 5 lines
drivers/net/ethernet/netronome/nfp/flower/lag_conf.c +1 -1
@@ -276,7 +276,7 @@ static void nfp_fl_lag_do_work(struct work_struct *work)
 
 	mutex_lock(&lag->lock);
 	list_for_each_entry_safe(entry, storage, &lag->group_list, list) {
-		struct net_device *iter_netdev, **acti_netdevs;
+		struct net_device *iter_netdev, **acti_netdevs = NULL;

Small nit, otherwise looks fine.

I think there is no need to set it to NULL here as it is always set to return value from kmalloc_array().

Thanks

Quoted 17 lines
drivers/net/ethernet/netronome/nfp/flower/lag_conf.c +4 -0
 		struct nfp_flower_repr_priv *repr_priv;
 		int active_count = 0, slaves = 0;
 		struct nfp_repr *repr;
@@ -308,6 +308,10 @@ static void nfp_fl_lag_do_work(struct work_struct *work)
 
 		acti_netdevs = kmalloc_array(entry->slave_cnt,
 					     sizeof(*acti_netdevs), GFP_KERNEL);
+		if (!acti_netdevs) {
+			schedule_delayed_work(&lag->work, NFP_FL_LAG_DELAY);
+			continue;
+		}
 
 		/* Include sanity check in the loop. It may be that a bond has
 		 * changed between processing the last notification and the
Signature · 1 line
2.25.1
#2 lore raw
In reply to Denis Arefev
Quoted 10 lines · Denis Arefev

Return value of a function 'kmalloc_array' is dereferenced at lag_conf.c:347 without checking for null,
but it is usually checked for this function.

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

Fixes bb9a8d031140 ("nfp: flower: monitor and offload LAG groups")
---
 drivers/net/ethernet/netronome/nfp/flower/lag_conf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

<...>

drivers/net/ethernet/netronome/nfp/flower/lag_conf.c static void nfp_fl_lag_do_work(struct work_struct *work) · line 313

Quoted 5 lines
drivers/net/ethernet/netronome/nfp/flower/lag_conf.c +3 -0
 		acti_netdevs = kmalloc_array(entry->slave_cnt,
 					     sizeof(*acti_netdevs), GFP_KERNEL);
+		if (!acti_netdevs) {
+			schedule_delayed_work(&lag->work, NFP_FL_LAG_DELAY);
+			continue;

It is usually not good idea to continue after memory allocation fails. So or add __GFP_NOWARN with a comment why it is safe to continue or bail out from this loop.

Thanks

Quoted 7 lines
drivers/net/ethernet/netronome/nfp/flower/lag_conf.c +1 -0
+		}
 
 		/* Include sanity check in the loop. It may be that a bond has
 		 * changed between processing the last notification and the
Signature · 1 line
2.25.1