Skip to contentSkip to conversation

Remove unnecessary null free checks

PATCH In review Stephen Hemminger 3 participants 8 messages Mar 23, 2023

lore
Share & tools
lore
#0 lore raw

cover Remove unnecessary null free checks

These were produced by using coccinelle script.

  $ ./devtools/cocci.sh ./devtools/cocci/nullfree.cocci

Stephen Hemminger (4):
mldev: remove unnecessary null free checks ml/cnkx: remove unnecessary null checks
graph: remove unnicessary null checks examples/fips_validation: remove unneeded null checks

 app/test-mldev/test_device_ops.c       |  6 ++----
 app/test-mldev/test_inference_common.c | 24 ++++++++----------------
 app/test-mldev/test_model_ops.c        |  6 ++----
 drivers/ml/cnxk/cn10k_ml_dev.c         |  9 +++------
 drivers/ml/cnxk/cn10k_ml_ops.c         | 12 ++++--------
 examples/fips_validation/main.c        |  6 ++----
 lib/graph/graph_pcap.c                 |  3 +--
 lib/mldev/rte_mldev.c                  |  3 +--
 8 files changed, 23 insertions(+), 46 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/4 mldev: remove unnecessary null free checks

These are places where mldev is doing unnecessary checks for calling free() style functions.

---
 app/test-mldev/test_device_ops.c       |  6 ++----
 app/test-mldev/test_inference_common.c | 24 ++++++++----------------
 app/test-mldev/test_model_ops.c        |  6 ++----
 lib/mldev/rte_mldev.c                  |  3 +--
 4 files changed, 13 insertions(+), 26 deletions(-)
app/test-mldev/test_device_ops.c +2 -4
index be0c5f0780d9..81b3ca18f36b 100644
@@ -70,8 +70,7 @@ test_device_setup(struct ml_test *test, struct ml_options *opt)
 	return 0;
 
 error:
-	if (test_device != NULL)
-		rte_free(test_device);
+	rte_free(test_device);
 
 	return ret;
 }
@@ -84,8 +83,7 @@ test_device_destroy(struct ml_test *test, struct ml_options *opt)
 	RTE_SET_USED(opt);
 
 	t = ml_test_priv(test);
-	if (t != NULL)
-		rte_free(t);
+	rte_free(t);
 }
 
 static int
app/test-mldev/test_inference_common.c +8 -16
index eba37c50e96c..af831fc1bf4c 100644
@@ -492,8 +492,7 @@ test_inference_setup(struct ml_test *test, struct ml_options *opt)
 	return 0;
 
 error:
-	if (test_inference != NULL)
-		rte_free(test_inference);
+	rte_free(test_inference);
 
 	return ret;
 }
@@ -506,8 +505,7 @@ test_inference_destroy(struct ml_test *test, struct ml_options *opt)
 	RTE_SET_USED(opt);
 
 	t = ml_test_priv(test);
-	if (t != NULL)
-		rte_free(t);
+	rte_free(t);
 }
 
 int
@@ -748,8 +746,7 @@ ml_inference_iomem_destroy(struct ml_test *test, struct ml_options *opt, uint16_
 	/* destroy io pool */
 	sprintf(mp_name, "ml_io_pool_%d", fid);
 	mp = rte_mempool_lookup(mp_name);
-	if (mp != NULL)
-		rte_mempool_free(mp);
+	rte_mempool_free(mp);
 }
 
 int
@@ -776,8 +773,7 @@ ml_inference_mem_destroy(struct ml_test *test, struct ml_options *opt)
 	RTE_SET_USED(opt);
 
 	/* release op pool */
-	if (t->op_pool != NULL)
-		rte_mempool_free(t->op_pool);
+	rte_mempool_free(t->op_pool);
 }
 
 static bool
@@ -1088,11 +1084,9 @@ ml_inference_stats_get(struct ml_test *test, struct ml_options *opt)
 	print_line(80);
 
 	/* release buffers */
-	if (t->xstats_map)
-		rte_free(t->xstats_map);
+	rte_free(t->xstats_map);
 
-	if (t->xstats_values)
-		rte_free(t->xstats_values);
+	rte_free(t->xstats_values);
 
 	/* print end-to-end stats */
 	freq = rte_get_tsc_hz();
@@ -1129,11 +1123,9 @@ ml_inference_stats_get(struct ml_test *test, struct ml_options *opt)
 	return 0;
 
 error:
-	if (t->xstats_map)
-		rte_free(t->xstats_map);
+	rte_free(t->xstats_map);
 
-	if (t->xstats_values)
-		rte_free(t->xstats_values);
+	rte_free(t->xstats_values);
 
 	return ret;
 }
app/test-mldev/test_model_ops.c +2 -4
index 0202b31190f8..5bbbcb1a6c0b 100644
@@ -93,8 +93,7 @@ test_model_ops_setup(struct ml_test *test, struct ml_options *opt)
 	return 0;
 
 error:
-	if (test_model_ops != NULL)
-		rte_free(test_model_ops);
+	rte_free(test_model_ops);
 
 	return ret;
 }
@@ -107,8 +106,7 @@ test_model_ops_destroy(struct ml_test *test, struct ml_options *opt)
 	RTE_SET_USED(opt);
 
 	t = ml_test_priv(test);
-	if (t != NULL)
-		rte_free(t);
+	rte_free(t);
 }
 
 static int
lib/mldev/rte_mldev.c +1 -2
index 50ebeb1bfe8d..f5904a0d4a90 100644
@@ -836,8 +836,7 @@ rte_ml_op_pool_create(const char *name, unsigned int nb_elts, unsigned int cache
 void
 rte_ml_op_pool_free(struct rte_mempool *mempool)
 {
-	if (mempool != NULL)
-		rte_mempool_free(mempool);
+	rte_mempool_free(mempool);
 }
 
 uint16_t
Signature · 1 line
2.39.2
Acked-by on v1 1/4
#2 lore raw

RE: [EXT] [PATCH 1/4] mldev: remove unnecessary null free checks

Quoted 9 lines

-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: 23 March 2023 06:34
To: [email protected]
Cc: Stephen Hemminger <[email protected]>; Srikanth Yalavarthi <[email protected]>; Srikanth Yalavarthi <[email protected]>
Subject: [EXT] [PATCH 1/4] mldev: remove unnecessary null free checks

Quoted 7 lines

External Email

----------------------------------------------------------------------
These are places where mldev is doing unnecessary checks for calling free() style functions.

#3 lore raw

PATCH 2/4 ml/cnkx: remove unnecessary null checks

In reply to Stephen Hemminger

These are places where null check is not necessary before calling free.

---
 drivers/ml/cnxk/cn10k_ml_dev.c |  9 +++------
 drivers/ml/cnxk/cn10k_ml_ops.c | 12 ++++--------
 2 files changed, 7 insertions(+), 14 deletions(-)
drivers/ml/cnxk/cn10k_ml_dev.c +3 -6
index bba3c9022e6b..983138a7f237 100644
@@ -319,8 +319,7 @@ cn10k_mldev_parse_devargs(struct rte_devargs *devargs, struct cn10k_ml_dev *mlde
 	plt_info("ML: %s = %d", CN10K_ML_OCM_PAGE_SIZE, mldev->ocm_page_size);
 
 exit:
-	if (kvlist)
-		rte_kvargs_free(kvlist);
+	rte_kvargs_free(kvlist);
 
 	return ret;
 }
@@ -795,8 +794,7 @@ cn10k_ml_fw_load(struct cn10k_ml_dev *mldev)
 	mz = plt_memzone_reserve_aligned(FW_MEMZONE_NAME, mz_size, 0, ML_CN10K_ALIGN_SIZE);
 	if (mz == NULL) {
 		plt_err("plt_memzone_reserve failed : %s", FW_MEMZONE_NAME);
-		if (fw_buffer != NULL)
-			free(fw_buffer);
+		free(fw_buffer);
 		return -ENOMEM;
 	}
 	fw->req = mz->addr;
@@ -813,8 +811,7 @@ cn10k_ml_fw_load(struct cn10k_ml_dev *mldev)
 	if (roc_env_is_emulator() || roc_env_is_hw()) {
 		fw->data = PLT_PTR_ADD(mz->addr, sizeof(struct cn10k_ml_req));
 		ret = cn10k_ml_fw_load_cn10ka(fw, fw_buffer, fw_size);
-		if (fw_buffer != NULL)
-			free(fw_buffer);
+		free(fw_buffer);
 	} else if (roc_env_is_asim()) {
 		fw->data = NULL;
 		ret = cn10k_ml_fw_load_asim(fw);
drivers/ml/cnxk/cn10k_ml_ops.c +4 -8
index 4df2ca0e8c26..b5eaa24e831c 100644
@@ -845,11 +845,9 @@ cn10k_ml_dev_configure(struct rte_ml_dev *dev, const struct rte_ml_dev_config *c
 	return 0;
 
 error:
-	if (dev->data->queue_pairs != NULL)
-		rte_free(dev->data->queue_pairs);
+	rte_free(dev->data->queue_pairs);
 
-	if (dev->data->models != NULL)
-		rte_free(dev->data->models);
+	rte_free(dev->data->models);
 
 	return ret;
 }
@@ -887,8 +885,7 @@ cn10k_ml_dev_close(struct rte_ml_dev *dev)
 		}
 	}
 
-	if (dev->data->models)
-		rte_free(dev->data->models);
+	rte_free(dev->data->models);
 
 	/* Destroy all queue pairs */
 	for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) {
@@ -900,8 +897,7 @@ cn10k_ml_dev_close(struct rte_ml_dev *dev)
 		}
 	}
 
-	if (dev->data->queue_pairs)
-		rte_free(dev->data->queue_pairs);
+	rte_free(dev->data->queue_pairs);
 
 	/* Unload firmware */
 	cn10k_ml_fw_unload(mldev);
Signature · 1 line
2.39.2
Acked-by on v1 2/4
#4 lore raw

RE: [EXT] [PATCH 2/4] ml/cnkx: remove unnecessary null checks

Quoted 9 lines

-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: 23 March 2023 06:34
To: [email protected]
Cc: Stephen Hemminger <[email protected]>; Srikanth Yalavarthi <[email protected]>; Srikanth Yalavarthi <[email protected]>
Subject: [EXT] [PATCH 2/4] ml/cnkx: remove unnecessary null checks

Quoted 6 lines

External Email

----------------------------------------------------------------------
These are places where null check is not necessary before calling free.

#5 lore raw

PATCH 3/4 graph: remove unnicessary null checks

In reply to Stephen Hemminger

Checking for null before calling rte_mempool_free is unnecessary.

---
 lib/graph/graph_pcap.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
lib/graph/graph_pcap.c +1 -2
index 9cbd1b8fdb6c..6c433300290b 100644
@@ -40,8 +40,7 @@ void
 graph_pcap_exit(struct rte_graph *graph)
 {
 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
-		if (pkt_mp)
-			rte_mempool_free(pkt_mp);
+		rte_mempool_free(pkt_mp);
 
 	if (pcapng_fd) {
 		rte_pcapng_close(pcapng_fd);
Signature · 1 line
2.39.2
#6 lore raw

PATCH 4/4 examples/fips_validation: remove unneeded null checks

In reply to Stephen Hemminger

No check for null pointer is necessary before calling free().

---
 examples/fips_validation/main.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
examples/fips_validation/main.c +2 -4
index 2c4353185edd..4c231fdb29cd 100644
@@ -2288,8 +2288,7 @@ fips_mct_sha_test(void)
 
 	max_outlen = md_blocks * vec.cipher_auth.digest.len;
 
-	if (vec.cipher_auth.digest.val)
-		free(vec.cipher_auth.digest.val);
+	free(vec.cipher_auth.digest.val);
 
 	vec.cipher_auth.digest.val = calloc(1, max_outlen);
 
@@ -2382,8 +2381,7 @@ fips_mct_shake_test(void)
 
 	max_outlen = vec.cipher_auth.digest.len;
 
-	if (vec.cipher_auth.digest.val)
-		free(vec.cipher_auth.digest.val);
+	free(vec.cipher_auth.digest.val);
 
 	vec.cipher_auth.digest.val = calloc(1, max_outlen);
 
Signature · 1 line
2.39.2
#7 lore raw
In reply to Stephen Hemminger

23/03/2023 02:04, Stephen Hemminger:

Quoted 8 lines

These were produced by using coccinelle script.

  $ ./devtools/cocci.sh ./devtools/cocci/nullfree.cocci

Stephen Hemminger (4):
mldev: remove unnecessary null free checks ml/cnkx: remove unnecessary null checks
graph: remove unnicessary null checks examples/fips_validation: remove unneeded null checks

Fixed typos in titles and applied, thanks.