DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH 1/1] ml/cnxk: fix multiple coverity issues
@ 2023-03-15 13:54 Srikanth Yalavarthi
  2023-03-16  9:33 ` [PATCH v2 " Srikanth Yalavarthi
  2023-03-16 21:28 ` [PATCH v3 0/8] Fixes to ml/cnxk driver Srikanth Yalavarthi
  0 siblings, 2 replies; 15+ messages in thread
From: Srikanth Yalavarthi @ 2023-03-15 13:54 UTC (permalink / raw)
  To: Srikanth Yalavarthi, Prince Takkar; +Cc: dev, sshankarnara

Added checks for null pointers. Removed logically dead
code. Fix division or modulo by zero. Fix evaludation
order violation issues.

Coverity issue: 383658 383659, 383664, 383665
Fixes: 817e3e55bb18 ("ml/cnxk: support simulator environment")
Fixes: da3325131d71 ("ml/cnxk: find OCM mask and page slots for a model")
Fixes: b7d0650ebce0 ("ml/cnxk: reserve and free OCM pages")
Fixes: 4ff4ab8e1a20 ("ml/cnxk: support extended statistics")

Signed-off-by: Srikanth Yalavarthi <syalavarthi@marvell.com>
---
 drivers/ml/cnxk/cn10k_ml_dev.c |  4 ++--
 drivers/ml/cnxk/cn10k_ml_ocm.c | 33 +++++++--------------------------
 drivers/ml/cnxk/cn10k_ml_ops.c |  3 ++-
 3 files changed, 11 insertions(+), 29 deletions(-)

diff --git a/drivers/ml/cnxk/cn10k_ml_dev.c b/drivers/ml/cnxk/cn10k_ml_dev.c
index 6f9a1015a6..bba3c9022e 100644
--- a/drivers/ml/cnxk/cn10k_ml_dev.c
+++ b/drivers/ml/cnxk/cn10k_ml_dev.c
@@ -779,8 +779,8 @@ cn10k_ml_fw_load(struct cn10k_ml_dev *mldev)
 	if (roc_env_is_emulator() || roc_env_is_hw()) {
 		/* Read firmware image to a buffer */
 		ret = rte_firmware_read(fw->path, &fw_buffer, &fw_size);
-		if (ret < 0) {
-			plt_err("Can't read firmware data: %s\n", fw->path);
+		if ((ret < 0) || (fw_buffer == NULL)) {
+			plt_err("Unable to read firmware data: %s\n", fw->path);
 			return ret;
 		}
 
diff --git a/drivers/ml/cnxk/cn10k_ml_ocm.c b/drivers/ml/cnxk/cn10k_ml_ocm.c
index d8d2c71a3c..f0f76b83b3 100644
--- a/drivers/ml/cnxk/cn10k_ml_ocm.c
+++ b/drivers/ml/cnxk/cn10k_ml_ocm.c
@@ -224,7 +224,6 @@ cn10k_ml_ocm_tilemask_find(struct rte_ml_dev *dev, uint8_t num_tiles, uint16_t w
 	uint16_t scratch_page_start;
 	int used_last_wb_page_max;
 	uint16_t scratch_page_end;
-	uint8_t search_start_tile;
 	uint8_t search_end_tile;
 	uint8_t *local_ocm_mask;
 	int wb_page_start_curr;
@@ -235,7 +234,6 @@ cn10k_ml_ocm_tilemask_find(struct rte_ml_dev *dev, uint8_t num_tiles, uint16_t w
 	uint16_t word_id;
 	uint8_t tile_idx;
 	int max_slot_sz;
-	int start_tile;
 	int page_id;
 
 	mldev = dev->data->dev_private;
@@ -250,28 +248,14 @@ cn10k_ml_ocm_tilemask_find(struct rte_ml_dev *dev, uint8_t num_tiles, uint16_t w
 	wb_page_start = -1;
 	used_scratch_pages_max = 0;
 	used_last_wb_page_max = -1;
-	start_tile = -1;
 	max_slot_sz_curr = 0;
 	max_slot_sz = 0;
 	tile_idx = 0;
 
-	if ((start_tile != -1) && (start_tile % num_tiles != 0)) {
-		plt_err("Invalid start_tile, %d", start_tile);
-		return -1;
-	}
-
-	if (start_tile < 0) {
-		search_start_tile = 0;
-		search_end_tile = ocm->num_tiles - num_tiles;
-	} else {
-		search_start_tile = start_tile;
-		search_end_tile = start_tile;
-	}
-
-	/* nibbles + prefix '0x' */
+	search_end_tile = ocm->num_tiles - num_tiles;
 	local_ocm_mask = rte_zmalloc("local_ocm_mask", mldev->ocm.mask_words, RTE_CACHE_LINE_SIZE);
+	tile_start = 0;
 
-	tile_start = search_start_tile;
 start_search:
 	used_scratch_pages_max = 0;
 	used_last_wb_page_max = -1;
@@ -423,10 +407,8 @@ cn10k_ml_ocm_free_pages(struct rte_ml_dev *dev, uint16_t model_id)
 	wb_page_end = wb_page_start + model->model_mem_map.wb_pages - 1;
 	for (tile_id = model->addr.tile_start; tile_id <= model->addr.tile_end; tile_id++) {
 		for (page_id = wb_page_start; page_id <= wb_page_end; page_id++) {
-			ocm->tile_ocm_info[tile_id].ocm_mask[page_id / OCM_MAP_WORD_SIZE] =
-				CLEAR_BIT(ocm->tile_ocm_info[tile_id]
-						  .ocm_mask[page_id / OCM_MAP_WORD_SIZE],
-					  page_id % OCM_MAP_WORD_SIZE);
+			CLEAR_BIT(ocm->tile_ocm_info[tile_id].ocm_mask[page_id / OCM_MAP_WORD_SIZE],
+				  page_id % OCM_MAP_WORD_SIZE);
 		}
 
 		/* Update last_wb_page size */
@@ -452,10 +434,9 @@ cn10k_ml_ocm_free_pages(struct rte_ml_dev *dev, uint16_t model_id)
 			prev_start = ocm->num_pages - ocm->tile_ocm_info[tile_id].scratch_pages;
 			curr_start = ocm->num_pages - scratch_resize_pages;
 			for (page_id = prev_start; page_id < curr_start; page_id++) {
-				ocm->tile_ocm_info[tile_id].ocm_mask[page_id / OCM_MAP_WORD_SIZE] =
-					CLEAR_BIT(ocm->tile_ocm_info[tile_id]
-							  .ocm_mask[page_id / OCM_MAP_WORD_SIZE],
-						  page_id % OCM_MAP_WORD_SIZE);
+				CLEAR_BIT(ocm->tile_ocm_info[tile_id]
+						  .ocm_mask[page_id / OCM_MAP_WORD_SIZE],
+					  page_id % OCM_MAP_WORD_SIZE);
 			}
 			ocm->tile_ocm_info[tile_id].scratch_pages = scratch_resize_pages;
 		}
diff --git a/drivers/ml/cnxk/cn10k_ml_ops.c b/drivers/ml/cnxk/cn10k_ml_ops.c
index 7d5eb97668..bf9409ad10 100644
--- a/drivers/ml/cnxk/cn10k_ml_ops.c
+++ b/drivers/ml/cnxk/cn10k_ml_ops.c
@@ -444,7 +444,8 @@ cn10k_ml_prep_fp_job_descriptor(struct rte_ml_dev *dev, struct cn10k_ml_req *req
 			count += model->burst_stats[qp_id].dequeued_count -                        \
 				 model->burst_stats[qp_id].str##_reset_count;                      \
 		}                                                                                  \
-		value = value / count;                                                             \
+		if (count != 0)                                                                    \
+			value = value / count;                                                     \
 	} while (0)
 
 #define ML_MIN_FOREACH_QP(dev, model, qp_id, str, value, count)                                    \
-- 
2.17.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v2 1/1] ml/cnxk: fix multiple coverity issues
  2023-03-15 13:54 [PATCH 1/1] ml/cnxk: fix multiple coverity issues Srikanth Yalavarthi
@ 2023-03-16  9:33 ` Srikanth Yalavarthi
  2023-03-16 17:00   ` Thomas Monjalon
  2023-03-16 21:28 ` [PATCH v3 0/8] Fixes to ml/cnxk driver Srikanth Yalavarthi
  1 sibling, 1 reply; 15+ messages in thread
From: Srikanth Yalavarthi @ 2023-03-16  9:33 UTC (permalink / raw)
  To: Srikanth Yalavarthi, Prince Takkar, Shivah Shankar S; +Cc: dev, aprabhu

Added checks for null pointers. Removed logically dead code.
Fix division or modulo by zero. Fix evaluation order violation
issues. Fix potential memory leak in xstats function.

Coverity issue: 383658 383659, 383664, 383665
Fixes: 817e3e55bb18 ("ml/cnxk: support simulator environment")
Fixes: da3325131d71 ("ml/cnxk: find OCM mask and page slots for a model")
Fixes: b7d0650ebce0 ("ml/cnxk: reserve and free OCM pages")
Fixes: 515e3608c741 ("ml/cnxk: configure OCM page size")
Fixes: 4ff4ab8e1a20 ("ml/cnxk: support extended statistics")

Signed-off-by: Srikanth Yalavarthi <syalavarthi@marvell.com>
---

v2:
* additional null pointer checks
* xstats memory leak fix

 drivers/ml/cnxk/cn10k_ml_dev.c |  4 ++--
 drivers/ml/cnxk/cn10k_ml_ocm.c | 39 ++++++++++++----------------------
 drivers/ml/cnxk/cn10k_ml_ops.c | 17 +++++++++++++--
 3 files changed, 31 insertions(+), 29 deletions(-)

diff --git a/drivers/ml/cnxk/cn10k_ml_dev.c b/drivers/ml/cnxk/cn10k_ml_dev.c
index 6f9a1015a6..bba3c9022e 100644
--- a/drivers/ml/cnxk/cn10k_ml_dev.c
+++ b/drivers/ml/cnxk/cn10k_ml_dev.c
@@ -779,8 +779,8 @@ cn10k_ml_fw_load(struct cn10k_ml_dev *mldev)
 	if (roc_env_is_emulator() || roc_env_is_hw()) {
 		/* Read firmware image to a buffer */
 		ret = rte_firmware_read(fw->path, &fw_buffer, &fw_size);
-		if (ret < 0) {
-			plt_err("Can't read firmware data: %s\n", fw->path);
+		if ((ret < 0) || (fw_buffer == NULL)) {
+			plt_err("Unable to read firmware data: %s\n", fw->path);
 			return ret;
 		}
 
diff --git a/drivers/ml/cnxk/cn10k_ml_ocm.c b/drivers/ml/cnxk/cn10k_ml_ocm.c
index d8d2c71a3c..edc1e5dd4b 100644
--- a/drivers/ml/cnxk/cn10k_ml_ocm.c
+++ b/drivers/ml/cnxk/cn10k_ml_ocm.c
@@ -224,7 +224,6 @@ cn10k_ml_ocm_tilemask_find(struct rte_ml_dev *dev, uint8_t num_tiles, uint16_t w
 	uint16_t scratch_page_start;
 	int used_last_wb_page_max;
 	uint16_t scratch_page_end;
-	uint8_t search_start_tile;
 	uint8_t search_end_tile;
 	uint8_t *local_ocm_mask;
 	int wb_page_start_curr;
@@ -235,7 +234,6 @@ cn10k_ml_ocm_tilemask_find(struct rte_ml_dev *dev, uint8_t num_tiles, uint16_t w
 	uint16_t word_id;
 	uint8_t tile_idx;
 	int max_slot_sz;
-	int start_tile;
 	int page_id;
 
 	mldev = dev->data->dev_private;
@@ -250,28 +248,18 @@ cn10k_ml_ocm_tilemask_find(struct rte_ml_dev *dev, uint8_t num_tiles, uint16_t w
 	wb_page_start = -1;
 	used_scratch_pages_max = 0;
 	used_last_wb_page_max = -1;
-	start_tile = -1;
 	max_slot_sz_curr = 0;
 	max_slot_sz = 0;
 	tile_idx = 0;
+	tile_start = 0;
+	search_end_tile = ocm->num_tiles - num_tiles;
 
-	if ((start_tile != -1) && (start_tile % num_tiles != 0)) {
-		plt_err("Invalid start_tile, %d", start_tile);
+	local_ocm_mask = rte_zmalloc("local_ocm_mask", mldev->ocm.mask_words, RTE_CACHE_LINE_SIZE);
+	if (local_ocm_mask == NULL) {
+		plt_err("Unable to allocate memory for local_ocm_mask");
 		return -1;
 	}
 
-	if (start_tile < 0) {
-		search_start_tile = 0;
-		search_end_tile = ocm->num_tiles - num_tiles;
-	} else {
-		search_start_tile = start_tile;
-		search_end_tile = start_tile;
-	}
-
-	/* nibbles + prefix '0x' */
-	local_ocm_mask = rte_zmalloc("local_ocm_mask", mldev->ocm.mask_words, RTE_CACHE_LINE_SIZE);
-
-	tile_start = search_start_tile;
 start_search:
 	used_scratch_pages_max = 0;
 	used_last_wb_page_max = -1;
@@ -423,10 +411,8 @@ cn10k_ml_ocm_free_pages(struct rte_ml_dev *dev, uint16_t model_id)
 	wb_page_end = wb_page_start + model->model_mem_map.wb_pages - 1;
 	for (tile_id = model->addr.tile_start; tile_id <= model->addr.tile_end; tile_id++) {
 		for (page_id = wb_page_start; page_id <= wb_page_end; page_id++) {
-			ocm->tile_ocm_info[tile_id].ocm_mask[page_id / OCM_MAP_WORD_SIZE] =
-				CLEAR_BIT(ocm->tile_ocm_info[tile_id]
-						  .ocm_mask[page_id / OCM_MAP_WORD_SIZE],
-					  page_id % OCM_MAP_WORD_SIZE);
+			CLEAR_BIT(ocm->tile_ocm_info[tile_id].ocm_mask[page_id / OCM_MAP_WORD_SIZE],
+				  page_id % OCM_MAP_WORD_SIZE);
 		}
 
 		/* Update last_wb_page size */
@@ -452,10 +438,9 @@ cn10k_ml_ocm_free_pages(struct rte_ml_dev *dev, uint16_t model_id)
 			prev_start = ocm->num_pages - ocm->tile_ocm_info[tile_id].scratch_pages;
 			curr_start = ocm->num_pages - scratch_resize_pages;
 			for (page_id = prev_start; page_id < curr_start; page_id++) {
-				ocm->tile_ocm_info[tile_id].ocm_mask[page_id / OCM_MAP_WORD_SIZE] =
-					CLEAR_BIT(ocm->tile_ocm_info[tile_id]
-							  .ocm_mask[page_id / OCM_MAP_WORD_SIZE],
-						  page_id % OCM_MAP_WORD_SIZE);
+				CLEAR_BIT(ocm->tile_ocm_info[tile_id]
+						  .ocm_mask[page_id / OCM_MAP_WORD_SIZE],
+					  page_id % OCM_MAP_WORD_SIZE);
 			}
 			ocm->tile_ocm_info[tile_id].scratch_pages = scratch_resize_pages;
 		}
@@ -497,6 +482,10 @@ cn10k_ml_ocm_print(struct rte_ml_dev *dev, FILE *fp)
 
 	/* nibbles + prefix '0x' */
 	str = rte_zmalloc("ocm_mask_str", mldev->ocm.num_pages / 4 + 2, RTE_CACHE_LINE_SIZE);
+	if (str == NULL) {
+		plt_err("Unable to allocate memory for ocm_mask_str");
+		return;
+	}
 
 	fprintf(fp, "OCM State:\n");
 	for (tile_id = 0; tile_id < ocm->num_tiles; tile_id++) {
diff --git a/drivers/ml/cnxk/cn10k_ml_ops.c b/drivers/ml/cnxk/cn10k_ml_ops.c
index 7d5eb97668..4df2ca0e8c 100644
--- a/drivers/ml/cnxk/cn10k_ml_ops.c
+++ b/drivers/ml/cnxk/cn10k_ml_ops.c
@@ -444,7 +444,8 @@ cn10k_ml_prep_fp_job_descriptor(struct rte_ml_dev *dev, struct cn10k_ml_req *req
 			count += model->burst_stats[qp_id].dequeued_count -                        \
 				 model->burst_stats[qp_id].str##_reset_count;                      \
 		}                                                                                  \
-		value = value / count;                                                             \
+		if (count != 0)                                                                    \
+			value = value / count;                                                     \
 	} while (0)
 
 #define ML_MIN_FOREACH_QP(dev, model, qp_id, str, value, count)                                    \
@@ -788,6 +789,11 @@ cn10k_ml_dev_configure(struct rte_ml_dev *dev, const struct rte_ml_dev_config *c
 	/* Allocate memory for ocm_mask */
 	ocm->ocm_mask =
 		rte_zmalloc("ocm_mask", ocm->mask_words * ocm->num_tiles, RTE_CACHE_LINE_SIZE);
+	if (ocm->ocm_mask == NULL) {
+		plt_err("Unable to allocate memory for OCM mask");
+		ret = -ENOMEM;
+		goto error;
+	}
 
 	for (tile_id = 0; tile_id < ocm->num_tiles; tile_id++) {
 		ocm->tile_ocm_info[tile_id].ocm_mask = ocm->ocm_mask + tile_id * ocm->mask_words;
@@ -1106,6 +1112,11 @@ cn10k_ml_dev_xstats_by_name_get(struct rte_ml_dev *dev, const char *name, uint16
 	num_xstats = PLT_DIM(cn10k_ml_model_xstats_table) * mldev->nb_models_loaded;
 	xstats_map = rte_zmalloc("cn10k_ml_xstats_map",
 				 sizeof(struct rte_ml_dev_xstats_map) * num_xstats, 0);
+	if (xstats_map == NULL) {
+		plt_err("Unable to allocate memory for cn10k_ml_xstats_map");
+		return -ENOMEM;
+	}
+
 	cn10k_ml_dev_xstats_names_get(dev, xstats_map, num_xstats);
 
 	cn10k_ml_dev_info_get(dev, &dev_info);
@@ -1117,8 +1128,10 @@ cn10k_ml_dev_xstats_by_name_get(struct rte_ml_dev *dev, const char *name, uint16
 		}
 	}
 
-	if (id == PLT_DIM(cn10k_ml_model_xstats_table) * dev_info.max_models)
+	if (id == PLT_DIM(cn10k_ml_model_xstats_table) * dev_info.max_models) {
+		rte_free(xstats_map);
 		return -EINVAL;
+	}
 
 	model_id = id / PLT_DIM(cn10k_ml_model_xstats_table);
 	type = id % PLT_DIM(cn10k_ml_model_xstats_table);
-- 
2.17.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 1/1] ml/cnxk: fix multiple coverity issues
  2023-03-16  9:33 ` [PATCH v2 " Srikanth Yalavarthi
@ 2023-03-16 17:00   ` Thomas Monjalon
  2023-03-16 17:02     ` [EXT] " Srikanth Yalavarthi
  0 siblings, 1 reply; 15+ messages in thread
From: Thomas Monjalon @ 2023-03-16 17:00 UTC (permalink / raw)
  To: Srikanth Yalavarthi; +Cc: Prince Takkar, Shivah Shankar S, dev, aprabhu

16/03/2023 10:33, Srikanth Yalavarthi:
> Added checks for null pointers. Removed logically dead code.
> Fix division or modulo by zero. Fix evaluation order violation
> issues. Fix potential memory leak in xstats function.

I think it would be better to split this patch a little,
so we can easily see what are the reasons for the changes.




^ permalink raw reply	[flat|nested] 15+ messages in thread

* RE: [EXT] Re: [PATCH v2 1/1] ml/cnxk: fix multiple coverity issues
  2023-03-16 17:00   ` Thomas Monjalon
@ 2023-03-16 17:02     ` Srikanth Yalavarthi
  2023-03-16 17:07       ` Thomas Monjalon
  0 siblings, 1 reply; 15+ messages in thread
From: Srikanth Yalavarthi @ 2023-03-16 17:02 UTC (permalink / raw)
  To: Thomas Monjalon
  Cc: Prince Takkar, Shivah Shankar Shankar Narayan Rao, dev,
	Anup Prabhu, Srikanth Yalavarthi

> -----Original Message-----
> From: Thomas Monjalon <thomas@monjalon.net>
> Sent: 16 March 2023 22:30
> To: Srikanth Yalavarthi <syalavarthi@marvell.com>
> Cc: Prince Takkar <ptakkar@marvell.com>; Shivah Shankar Shankar Narayan
> Rao <sshankarnara@marvell.com>; dev@dpdk.org; Anup Prabhu
> <aprabhu@marvell.com>
> Subject: [EXT] Re: [PATCH v2 1/1] ml/cnxk: fix multiple coverity issues
> 
> External Email
> 
> ----------------------------------------------------------------------
> 16/03/2023 10:33, Srikanth Yalavarthi:
> > Added checks for null pointers. Removed logically dead code.
> > Fix division or modulo by zero. Fix evaluation order violation issues.
> > Fix potential memory leak in xstats function.
> 
> I think it would be better to split this patch a little, so we can easily see what
> are the reasons for the changes.

Separate patch for each Coverity issue?

> 
> 


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [EXT] Re: [PATCH v2 1/1] ml/cnxk: fix multiple coverity issues
  2023-03-16 17:02     ` [EXT] " Srikanth Yalavarthi
@ 2023-03-16 17:07       ` Thomas Monjalon
  0 siblings, 0 replies; 15+ messages in thread
From: Thomas Monjalon @ 2023-03-16 17:07 UTC (permalink / raw)
  To: Srikanth Yalavarthi
  Cc: Prince Takkar, Shivah Shankar Shankar Narayan Rao, dev,
	Anup Prabhu, Srikanth Yalavarthi

16/03/2023 18:02, Srikanth Yalavarthi:
> > -----Original Message-----
> > From: Thomas Monjalon <thomas@monjalon.net>
> > Sent: 16 March 2023 22:30
> > To: Srikanth Yalavarthi <syalavarthi@marvell.com>
> > Cc: Prince Takkar <ptakkar@marvell.com>; Shivah Shankar Shankar Narayan
> > Rao <sshankarnara@marvell.com>; dev@dpdk.org; Anup Prabhu
> > <aprabhu@marvell.com>
> > Subject: [EXT] Re: [PATCH v2 1/1] ml/cnxk: fix multiple coverity issues
> > 
> > External Email
> > 
> > ----------------------------------------------------------------------
> > 16/03/2023 10:33, Srikanth Yalavarthi:
> > > Added checks for null pointers. Removed logically dead code.
> > > Fix division or modulo by zero. Fix evaluation order violation issues.
> > > Fix potential memory leak in xstats function.
> > 
> > I think it would be better to split this patch a little, so we can easily see what
> > are the reasons for the changes.
> 
> Separate patch for each Coverity issue?

Maybe yes.
Or try to group similar causes.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v3 0/8] Fixes to ml/cnxk driver
  2023-03-15 13:54 [PATCH 1/1] ml/cnxk: fix multiple coverity issues Srikanth Yalavarthi
  2023-03-16  9:33 ` [PATCH v2 " Srikanth Yalavarthi
@ 2023-03-16 21:28 ` Srikanth Yalavarthi
  2023-03-16 21:28   ` [PATCH v3 1/8] ml/cnxk: fix evaluation order violation issues Srikanth Yalavarthi
                     ` (8 more replies)
  1 sibling, 9 replies; 15+ messages in thread
From: Srikanth Yalavarthi @ 2023-03-16 21:28 UTC (permalink / raw)
  Cc: dev, syalavarthi, sshankarnara, aprabhu, ptakkar, pshukla

This is a series of fixes for ml/cnxk diver. Changes include fixes
for coverity scan issues, klocwork issues and additional fixes.

v3:
* split fixes into multiple patches
* add code improvement changes

v2:
* additional null pointer checks
* xstats memory leak fix

v1:
* initial consolidated patch

Srikanth Yalavarthi (8):
  ml/cnxk: fix evaluation order violation issues
  ml/cnxk: fix potential division by zero
  ml/cnxk: add pointer check after memory allocation
  ml/cnxk: remove logically dead code
  ml/cnxk: fix potential memory leak in xstats
  ml/cnxk: check for null pointer before dereference
  ml/cnxk: avoid variable name reuse in a function
  ml/cnxk: reduce levels of nested variables access

 drivers/ml/cnxk/cn10k_ml_dev.c   |  4 +--
 drivers/ml/cnxk/cn10k_ml_model.c | 48 +++++++++++++-------------
 drivers/ml/cnxk/cn10k_ml_ocm.c   | 59 +++++++++++++-------------------
 drivers/ml/cnxk/cn10k_ml_ops.c   | 17 +++++++--
 4 files changed, 65 insertions(+), 63 deletions(-)

--
2.17.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v3 1/8] ml/cnxk: fix evaluation order violation issues
  2023-03-16 21:28 ` [PATCH v3 0/8] Fixes to ml/cnxk driver Srikanth Yalavarthi
@ 2023-03-16 21:28   ` Srikanth Yalavarthi
  2023-03-16 21:28   ` [PATCH v3 2/8] ml/cnxk: fix potential division by zero Srikanth Yalavarthi
                     ` (7 subsequent siblings)
  8 siblings, 0 replies; 15+ messages in thread
From: Srikanth Yalavarthi @ 2023-03-16 21:28 UTC (permalink / raw)
  To: Srikanth Yalavarthi, Prince Takkar; +Cc: dev, sshankarnara, aprabhu, pshukla

Fix evaluation order violation issues reported by coverity
tool. ocm_mask variable is being written twice with the
same value.

Coverity issue: 383659
Fixes: b7d0650ebce0 ("ml/cnxk: reserve and free OCM pages")

Signed-off-by: Srikanth Yalavarthi <syalavarthi@marvell.com>
---
 drivers/ml/cnxk/cn10k_ml_ocm.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/drivers/ml/cnxk/cn10k_ml_ocm.c b/drivers/ml/cnxk/cn10k_ml_ocm.c
index d8d2c71a3c..1ea45b5b60 100644
--- a/drivers/ml/cnxk/cn10k_ml_ocm.c
+++ b/drivers/ml/cnxk/cn10k_ml_ocm.c
@@ -423,10 +423,8 @@ cn10k_ml_ocm_free_pages(struct rte_ml_dev *dev, uint16_t model_id)
 	wb_page_end = wb_page_start + model->model_mem_map.wb_pages - 1;
 	for (tile_id = model->addr.tile_start; tile_id <= model->addr.tile_end; tile_id++) {
 		for (page_id = wb_page_start; page_id <= wb_page_end; page_id++) {
-			ocm->tile_ocm_info[tile_id].ocm_mask[page_id / OCM_MAP_WORD_SIZE] =
-				CLEAR_BIT(ocm->tile_ocm_info[tile_id]
-						  .ocm_mask[page_id / OCM_MAP_WORD_SIZE],
-					  page_id % OCM_MAP_WORD_SIZE);
+			CLEAR_BIT(ocm->tile_ocm_info[tile_id].ocm_mask[page_id / OCM_MAP_WORD_SIZE],
+				  page_id % OCM_MAP_WORD_SIZE);
 		}
 
 		/* Update last_wb_page size */
@@ -452,10 +450,9 @@ cn10k_ml_ocm_free_pages(struct rte_ml_dev *dev, uint16_t model_id)
 			prev_start = ocm->num_pages - ocm->tile_ocm_info[tile_id].scratch_pages;
 			curr_start = ocm->num_pages - scratch_resize_pages;
 			for (page_id = prev_start; page_id < curr_start; page_id++) {
-				ocm->tile_ocm_info[tile_id].ocm_mask[page_id / OCM_MAP_WORD_SIZE] =
-					CLEAR_BIT(ocm->tile_ocm_info[tile_id]
-							  .ocm_mask[page_id / OCM_MAP_WORD_SIZE],
-						  page_id % OCM_MAP_WORD_SIZE);
+				CLEAR_BIT(ocm->tile_ocm_info[tile_id]
+						  .ocm_mask[page_id / OCM_MAP_WORD_SIZE],
+					  page_id % OCM_MAP_WORD_SIZE);
 			}
 			ocm->tile_ocm_info[tile_id].scratch_pages = scratch_resize_pages;
 		}
-- 
2.17.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v3 2/8] ml/cnxk: fix potential division by zero
  2023-03-16 21:28 ` [PATCH v3 0/8] Fixes to ml/cnxk driver Srikanth Yalavarthi
  2023-03-16 21:28   ` [PATCH v3 1/8] ml/cnxk: fix evaluation order violation issues Srikanth Yalavarthi
@ 2023-03-16 21:28   ` Srikanth Yalavarthi
  2023-03-16 21:28   ` [PATCH v3 3/8] ml/cnxk: add pointer check after memory allocation Srikanth Yalavarthi
                     ` (6 subsequent siblings)
  8 siblings, 0 replies; 15+ messages in thread
From: Srikanth Yalavarthi @ 2023-03-16 21:28 UTC (permalink / raw)
  To: Srikanth Yalavarthi, Prince Takkar; +Cc: dev, sshankarnara, aprabhu, pshukla

Fix division or modulo by zero issue reported by coverity. Added
a check to count, before updating average value of a stat.

Coverity issue: 383658
Fixes: 4ff4ab8e1a20 ("ml/cnxk: support extended statistics")

Signed-off-by: Srikanth Yalavarthi <syalavarthi@marvell.com>
---
 drivers/ml/cnxk/cn10k_ml_ops.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/ml/cnxk/cn10k_ml_ops.c b/drivers/ml/cnxk/cn10k_ml_ops.c
index 7d5eb97668..bf9409ad10 100644
--- a/drivers/ml/cnxk/cn10k_ml_ops.c
+++ b/drivers/ml/cnxk/cn10k_ml_ops.c
@@ -444,7 +444,8 @@ cn10k_ml_prep_fp_job_descriptor(struct rte_ml_dev *dev, struct cn10k_ml_req *req
 			count += model->burst_stats[qp_id].dequeued_count -                        \
 				 model->burst_stats[qp_id].str##_reset_count;                      \
 		}                                                                                  \
-		value = value / count;                                                             \
+		if (count != 0)                                                                    \
+			value = value / count;                                                     \
 	} while (0)
 
 #define ML_MIN_FOREACH_QP(dev, model, qp_id, str, value, count)                                    \
-- 
2.17.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v3 3/8] ml/cnxk: add pointer check after memory allocation
  2023-03-16 21:28 ` [PATCH v3 0/8] Fixes to ml/cnxk driver Srikanth Yalavarthi
  2023-03-16 21:28   ` [PATCH v3 1/8] ml/cnxk: fix evaluation order violation issues Srikanth Yalavarthi
  2023-03-16 21:28   ` [PATCH v3 2/8] ml/cnxk: fix potential division by zero Srikanth Yalavarthi
@ 2023-03-16 21:28   ` Srikanth Yalavarthi
  2023-03-16 21:29   ` [PATCH v3 4/8] ml/cnxk: remove logically dead code Srikanth Yalavarthi
                     ` (5 subsequent siblings)
  8 siblings, 0 replies; 15+ messages in thread
From: Srikanth Yalavarthi @ 2023-03-16 21:28 UTC (permalink / raw)
  To: Srikanth Yalavarthi, Prince Takkar, Shivah Shankar S
  Cc: dev, aprabhu, pshukla

Added checks for null pointers after memory allocation. Issues
were reported by klocwork static analysis tool.

Fixes: 515e3608c741 ("ml/cnxk: configure OCM page size")
Fixes: 4ff4ab8e1a20 ("ml/cnxk: support extended statistics")

Signed-off-by: Srikanth Yalavarthi <syalavarthi@marvell.com>
---
 drivers/ml/cnxk/cn10k_ml_ocm.c | 10 +++++++++-
 drivers/ml/cnxk/cn10k_ml_ops.c | 10 ++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/drivers/ml/cnxk/cn10k_ml_ocm.c b/drivers/ml/cnxk/cn10k_ml_ocm.c
index 1ea45b5b60..b9211b50a8 100644
--- a/drivers/ml/cnxk/cn10k_ml_ocm.c
+++ b/drivers/ml/cnxk/cn10k_ml_ocm.c
@@ -268,8 +268,12 @@ cn10k_ml_ocm_tilemask_find(struct rte_ml_dev *dev, uint8_t num_tiles, uint16_t w
 		search_end_tile = start_tile;
 	}
 
-	/* nibbles + prefix '0x' */
+	/* allocate for local ocm mask */
 	local_ocm_mask = rte_zmalloc("local_ocm_mask", mldev->ocm.mask_words, RTE_CACHE_LINE_SIZE);
+	if (local_ocm_mask == NULL) {
+		plt_err("Unable to allocate memory for local_ocm_mask");
+		return -1;
+	}
 
 	tile_start = search_start_tile;
 start_search:
@@ -494,6 +498,10 @@ cn10k_ml_ocm_print(struct rte_ml_dev *dev, FILE *fp)
 
 	/* nibbles + prefix '0x' */
 	str = rte_zmalloc("ocm_mask_str", mldev->ocm.num_pages / 4 + 2, RTE_CACHE_LINE_SIZE);
+	if (str == NULL) {
+		plt_err("Unable to allocate memory for ocm_mask_str");
+		return;
+	}
 
 	fprintf(fp, "OCM State:\n");
 	for (tile_id = 0; tile_id < ocm->num_tiles; tile_id++) {
diff --git a/drivers/ml/cnxk/cn10k_ml_ops.c b/drivers/ml/cnxk/cn10k_ml_ops.c
index bf9409ad10..8d31276e8c 100644
--- a/drivers/ml/cnxk/cn10k_ml_ops.c
+++ b/drivers/ml/cnxk/cn10k_ml_ops.c
@@ -789,6 +789,11 @@ cn10k_ml_dev_configure(struct rte_ml_dev *dev, const struct rte_ml_dev_config *c
 	/* Allocate memory for ocm_mask */
 	ocm->ocm_mask =
 		rte_zmalloc("ocm_mask", ocm->mask_words * ocm->num_tiles, RTE_CACHE_LINE_SIZE);
+	if (ocm->ocm_mask == NULL) {
+		plt_err("Unable to allocate memory for OCM mask");
+		ret = -ENOMEM;
+		goto error;
+	}
 
 	for (tile_id = 0; tile_id < ocm->num_tiles; tile_id++) {
 		ocm->tile_ocm_info[tile_id].ocm_mask = ocm->ocm_mask + tile_id * ocm->mask_words;
@@ -1107,6 +1112,11 @@ cn10k_ml_dev_xstats_by_name_get(struct rte_ml_dev *dev, const char *name, uint16
 	num_xstats = PLT_DIM(cn10k_ml_model_xstats_table) * mldev->nb_models_loaded;
 	xstats_map = rte_zmalloc("cn10k_ml_xstats_map",
 				 sizeof(struct rte_ml_dev_xstats_map) * num_xstats, 0);
+	if (xstats_map == NULL) {
+		plt_err("Unable to allocate memory for cn10k_ml_xstats_map");
+		return -ENOMEM;
+	}
+
 	cn10k_ml_dev_xstats_names_get(dev, xstats_map, num_xstats);
 
 	cn10k_ml_dev_info_get(dev, &dev_info);
-- 
2.17.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v3 4/8] ml/cnxk: remove logically dead code
  2023-03-16 21:28 ` [PATCH v3 0/8] Fixes to ml/cnxk driver Srikanth Yalavarthi
                     ` (2 preceding siblings ...)
  2023-03-16 21:28   ` [PATCH v3 3/8] ml/cnxk: add pointer check after memory allocation Srikanth Yalavarthi
@ 2023-03-16 21:29   ` Srikanth Yalavarthi
  2023-03-16 21:29   ` [PATCH v3 5/8] ml/cnxk: fix potential memory leak in xstats Srikanth Yalavarthi
                     ` (4 subsequent siblings)
  8 siblings, 0 replies; 15+ messages in thread
From: Srikanth Yalavarthi @ 2023-03-16 21:29 UTC (permalink / raw)
  To: Srikanth Yalavarthi, Prince Takkar; +Cc: dev, sshankarnara, aprabhu, pshukla

Remove logically dead code. Issue reported by coverity scan.

Coverity issue: 383664
Fixes: da3325131d71 ("ml/cnxk: find OCM mask and page slots for a model")

Signed-off-by: Srikanth Yalavarthi <syalavarthi@marvell.com>
---
 drivers/ml/cnxk/cn10k_ml_ocm.c | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)

diff --git a/drivers/ml/cnxk/cn10k_ml_ocm.c b/drivers/ml/cnxk/cn10k_ml_ocm.c
index b9211b50a8..2367f40a1d 100644
--- a/drivers/ml/cnxk/cn10k_ml_ocm.c
+++ b/drivers/ml/cnxk/cn10k_ml_ocm.c
@@ -224,7 +224,6 @@ cn10k_ml_ocm_tilemask_find(struct rte_ml_dev *dev, uint8_t num_tiles, uint16_t w
 	uint16_t scratch_page_start;
 	int used_last_wb_page_max;
 	uint16_t scratch_page_end;
-	uint8_t search_start_tile;
 	uint8_t search_end_tile;
 	uint8_t *local_ocm_mask;
 	int wb_page_start_curr;
@@ -235,14 +234,13 @@ cn10k_ml_ocm_tilemask_find(struct rte_ml_dev *dev, uint8_t num_tiles, uint16_t w
 	uint16_t word_id;
 	uint8_t tile_idx;
 	int max_slot_sz;
-	int start_tile;
 	int page_id;
 
 	mldev = dev->data->dev_private;
 	ocm = &mldev->ocm;
 
 	if (num_tiles > ML_CN10K_OCM_NUMTILES) {
-		plt_err("Invalid num_tiles = %u (> ML_CN10K_OCM_NUMTILES)", num_tiles);
+		plt_err("Invalid num_tiles = %u (> %u)", num_tiles, ML_CN10K_OCM_NUMTILES);
 		return -1;
 	}
 
@@ -250,23 +248,11 @@ cn10k_ml_ocm_tilemask_find(struct rte_ml_dev *dev, uint8_t num_tiles, uint16_t w
 	wb_page_start = -1;
 	used_scratch_pages_max = 0;
 	used_last_wb_page_max = -1;
-	start_tile = -1;
 	max_slot_sz_curr = 0;
 	max_slot_sz = 0;
 	tile_idx = 0;
-
-	if ((start_tile != -1) && (start_tile % num_tiles != 0)) {
-		plt_err("Invalid start_tile, %d", start_tile);
-		return -1;
-	}
-
-	if (start_tile < 0) {
-		search_start_tile = 0;
-		search_end_tile = ocm->num_tiles - num_tiles;
-	} else {
-		search_start_tile = start_tile;
-		search_end_tile = start_tile;
-	}
+	tile_start = 0;
+	search_end_tile = ocm->num_tiles - num_tiles;
 
 	/* allocate for local ocm mask */
 	local_ocm_mask = rte_zmalloc("local_ocm_mask", mldev->ocm.mask_words, RTE_CACHE_LINE_SIZE);
@@ -275,7 +261,6 @@ cn10k_ml_ocm_tilemask_find(struct rte_ml_dev *dev, uint8_t num_tiles, uint16_t w
 		return -1;
 	}
 
-	tile_start = search_start_tile;
 start_search:
 	used_scratch_pages_max = 0;
 	used_last_wb_page_max = -1;
-- 
2.17.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v3 5/8] ml/cnxk: fix potential memory leak in xstats
  2023-03-16 21:28 ` [PATCH v3 0/8] Fixes to ml/cnxk driver Srikanth Yalavarthi
                     ` (3 preceding siblings ...)
  2023-03-16 21:29   ` [PATCH v3 4/8] ml/cnxk: remove logically dead code Srikanth Yalavarthi
@ 2023-03-16 21:29   ` Srikanth Yalavarthi
  2023-03-16 21:29   ` [PATCH v3 6/8] ml/cnxk: check for null pointer before dereference Srikanth Yalavarthi
                     ` (3 subsequent siblings)
  8 siblings, 0 replies; 15+ messages in thread
From: Srikanth Yalavarthi @ 2023-03-16 21:29 UTC (permalink / raw)
  To: Srikanth Yalavarthi, Prince Takkar; +Cc: dev, sshankarnara, aprabhu, pshukla

Local buffer xstats_map is not released, when the xstats name
passed by the user application is not valid. This patch releases
the memory in such cases.

Fixes: 4ff4ab8e1a20 ("ml/cnxk: support extended statistics")

Signed-off-by: Srikanth Yalavarthi <syalavarthi@marvell.com>
---
 drivers/ml/cnxk/cn10k_ml_ops.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/ml/cnxk/cn10k_ml_ops.c b/drivers/ml/cnxk/cn10k_ml_ops.c
index 8d31276e8c..4df2ca0e8c 100644
--- a/drivers/ml/cnxk/cn10k_ml_ops.c
+++ b/drivers/ml/cnxk/cn10k_ml_ops.c
@@ -1128,8 +1128,10 @@ cn10k_ml_dev_xstats_by_name_get(struct rte_ml_dev *dev, const char *name, uint16
 		}
 	}
 
-	if (id == PLT_DIM(cn10k_ml_model_xstats_table) * dev_info.max_models)
+	if (id == PLT_DIM(cn10k_ml_model_xstats_table) * dev_info.max_models) {
+		rte_free(xstats_map);
 		return -EINVAL;
+	}
 
 	model_id = id / PLT_DIM(cn10k_ml_model_xstats_table);
 	type = id % PLT_DIM(cn10k_ml_model_xstats_table);
-- 
2.17.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v3 6/8] ml/cnxk: check for null pointer before dereference
  2023-03-16 21:28 ` [PATCH v3 0/8] Fixes to ml/cnxk driver Srikanth Yalavarthi
                     ` (4 preceding siblings ...)
  2023-03-16 21:29   ` [PATCH v3 5/8] ml/cnxk: fix potential memory leak in xstats Srikanth Yalavarthi
@ 2023-03-16 21:29   ` Srikanth Yalavarthi
  2023-03-16 21:29   ` [PATCH v3 7/8] ml/cnxk: avoid variable name reuse in a function Srikanth Yalavarthi
                     ` (2 subsequent siblings)
  8 siblings, 0 replies; 15+ messages in thread
From: Srikanth Yalavarthi @ 2023-03-16 21:29 UTC (permalink / raw)
  To: Srikanth Yalavarthi, Prince Takkar; +Cc: dev, sshankarnara, aprabhu, pshukla

Fix deference before null check. Issue reported by coverity scan.

Coverity issue: 383665
Fixes: 817e3e55bb18 ("ml/cnxk: support simulator environment")

Signed-off-by: Srikanth Yalavarthi <syalavarthi@marvell.com>
---
 drivers/ml/cnxk/cn10k_ml_dev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/ml/cnxk/cn10k_ml_dev.c b/drivers/ml/cnxk/cn10k_ml_dev.c
index 6f9a1015a6..bba3c9022e 100644
--- a/drivers/ml/cnxk/cn10k_ml_dev.c
+++ b/drivers/ml/cnxk/cn10k_ml_dev.c
@@ -779,8 +779,8 @@ cn10k_ml_fw_load(struct cn10k_ml_dev *mldev)
 	if (roc_env_is_emulator() || roc_env_is_hw()) {
 		/* Read firmware image to a buffer */
 		ret = rte_firmware_read(fw->path, &fw_buffer, &fw_size);
-		if (ret < 0) {
-			plt_err("Can't read firmware data: %s\n", fw->path);
+		if ((ret < 0) || (fw_buffer == NULL)) {
+			plt_err("Unable to read firmware data: %s\n", fw->path);
 			return ret;
 		}
 
-- 
2.17.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v3 7/8] ml/cnxk: avoid variable name reuse in a function
  2023-03-16 21:28 ` [PATCH v3 0/8] Fixes to ml/cnxk driver Srikanth Yalavarthi
                     ` (5 preceding siblings ...)
  2023-03-16 21:29   ` [PATCH v3 6/8] ml/cnxk: check for null pointer before dereference Srikanth Yalavarthi
@ 2023-03-16 21:29   ` Srikanth Yalavarthi
  2023-03-16 21:29   ` [PATCH v3 8/8] ml/cnxk: reduce levels of nested variables access Srikanth Yalavarthi
  2023-03-19 19:01   ` [PATCH v3 0/8] Fixes to ml/cnxk driver Thomas Monjalon
  8 siblings, 0 replies; 15+ messages in thread
From: Srikanth Yalavarthi @ 2023-03-16 21:29 UTC (permalink / raw)
  To: Srikanth Yalavarthi, Prince Takkar; +Cc: dev, sshankarnara, aprabhu, pshukla

Avoid reusing variable name in different scopes with in same
function. Move variable declarations to beginning of function.

Fixes: b7d0650ebce0 ("ml/cnxk: reserve and free OCM pages")

Signed-off-by: Srikanth Yalavarthi <syalavarthi@marvell.com>
---
 drivers/ml/cnxk/cn10k_ml_ocm.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/drivers/ml/cnxk/cn10k_ml_ocm.c b/drivers/ml/cnxk/cn10k_ml_ocm.c
index 2367f40a1d..93505c9c09 100644
--- a/drivers/ml/cnxk/cn10k_ml_ocm.c
+++ b/drivers/ml/cnxk/cn10k_ml_ocm.c
@@ -390,6 +390,7 @@ cn10k_ml_ocm_reserve_pages(struct rte_ml_dev *dev, uint16_t model_id, uint64_t t
 void
 cn10k_ml_ocm_free_pages(struct rte_ml_dev *dev, uint16_t model_id)
 {
+	struct cn10k_ml_model *local_model;
 	struct cn10k_ml_model *model;
 	struct cn10k_ml_dev *mldev;
 	struct cn10k_ml_ocm *ocm;
@@ -420,17 +421,15 @@ cn10k_ml_ocm_free_pages(struct rte_ml_dev *dev, uint16_t model_id)
 		if (wb_page_end == ocm->tile_ocm_info[tile_id].last_wb_page)
 			ocm->tile_ocm_info[tile_id].last_wb_page = wb_page_start - 1;
 
-		/* Update scratch page size and clear extra bits */
-		scratch_resize_pages = 0;
 		/* Get max scratch pages required, excluding the current model */
+		scratch_resize_pages = 0;
 		for (i = 0; i < dev->data->nb_models; i++) {
-			struct cn10k_ml_model *model = dev->data->models[i];
-
-			if ((i != model_id) && (model != NULL)) {
-				if (IS_BIT_SET(model->model_mem_map.tilemask, tile_id))
-					scratch_resize_pages =
-						PLT_MAX((int)model->model_mem_map.scratch_pages,
-							scratch_resize_pages);
+			local_model = dev->data->models[i];
+			if ((i != model_id) && (local_model != NULL)) {
+				if (IS_BIT_SET(local_model->model_mem_map.tilemask, tile_id))
+					scratch_resize_pages = PLT_MAX(
+						(int)local_model->model_mem_map.scratch_pages,
+						scratch_resize_pages);
 			}
 		}
 
-- 
2.17.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v3 8/8] ml/cnxk: reduce levels of nested variables access
  2023-03-16 21:28 ` [PATCH v3 0/8] Fixes to ml/cnxk driver Srikanth Yalavarthi
                     ` (6 preceding siblings ...)
  2023-03-16 21:29   ` [PATCH v3 7/8] ml/cnxk: avoid variable name reuse in a function Srikanth Yalavarthi
@ 2023-03-16 21:29   ` Srikanth Yalavarthi
  2023-03-19 19:01   ` [PATCH v3 0/8] Fixes to ml/cnxk driver Thomas Monjalon
  8 siblings, 0 replies; 15+ messages in thread
From: Srikanth Yalavarthi @ 2023-03-16 21:29 UTC (permalink / raw)
  To: Srikanth Yalavarthi, Prince Takkar; +Cc: dev, sshankarnara, aprabhu, pshukla

Reduce the number of levels to access nested structure
variables. Use available variables or add new local
pointer variables for access to keep the code uniform.

Fixes: 298b2af4267f ("ml/cnxk: add internal structures for derived info")
Fixes: 0b9c0768ce2b ("ml/cnxk: support model query")

Signed-off-by: Srikanth Yalavarthi <syalavarthi@marvell.com>
---
 drivers/ml/cnxk/cn10k_ml_model.c | 48 ++++++++++++++++----------------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/drivers/ml/cnxk/cn10k_ml_model.c b/drivers/ml/cnxk/cn10k_ml_model.c
index ceffde8459..2ded05c5dc 100644
--- a/drivers/ml/cnxk/cn10k_ml_model.c
+++ b/drivers/ml/cnxk/cn10k_ml_model.c
@@ -272,8 +272,8 @@ cn10k_ml_model_addr_update(struct cn10k_ml_model *model, uint8_t *buffer, uint8_
 	addr->total_input_sz_q = 0;
 	for (i = 0; i < metadata->model.num_input; i++) {
 		addr->input[i].nb_elements =
-			model->metadata.input[i].shape.w * model->metadata.input[i].shape.x *
-			model->metadata.input[i].shape.y * model->metadata.input[i].shape.z;
+			metadata->input[i].shape.w * metadata->input[i].shape.x *
+			metadata->input[i].shape.y * metadata->input[i].shape.z;
 		addr->input[i].sz_d = addr->input[i].nb_elements *
 				      rte_ml_io_type_size_get(metadata->input[i].input_type);
 		addr->input[i].sz_q = addr->input[i].nb_elements *
@@ -360,52 +360,52 @@ cn10k_ml_model_ocm_pages_count(struct cn10k_ml_dev *mldev, uint16_t model_id, ui
 void
 cn10k_ml_model_info_set(struct rte_ml_dev *dev, struct cn10k_ml_model *model)
 {
+	struct cn10k_ml_model_metadata *metadata;
 	struct rte_ml_model_info *info;
 	struct rte_ml_io_info *output;
 	struct rte_ml_io_info *input;
 	uint8_t i;
 
+	metadata = &model->metadata;
 	info = PLT_PTR_CAST(model->info);
 	input = PLT_PTR_ADD(info, sizeof(struct rte_ml_model_info));
-	output =
-		PLT_PTR_ADD(input, model->metadata.model.num_input * sizeof(struct rte_ml_io_info));
+	output = PLT_PTR_ADD(input, metadata->model.num_input * sizeof(struct rte_ml_io_info));
 
 	/* Set model info */
 	memset(info, 0, sizeof(struct rte_ml_model_info));
-	rte_memcpy(info->name, model->metadata.model.name, MRVL_ML_MODEL_NAME_LEN);
-	snprintf(info->version, RTE_ML_STR_MAX, "%u.%u.%u.%u", model->metadata.model.version[0],
-		 model->metadata.model.version[1], model->metadata.model.version[2],
-		 model->metadata.model.version[3]);
+	rte_memcpy(info->name, metadata->model.name, MRVL_ML_MODEL_NAME_LEN);
+	snprintf(info->version, RTE_ML_STR_MAX, "%u.%u.%u.%u", metadata->model.version[0],
+		 metadata->model.version[1], metadata->model.version[2],
+		 metadata->model.version[3]);
 	info->model_id = model->model_id;
 	info->device_id = dev->data->dev_id;
 	info->batch_size = model->batch_size;
-	info->nb_inputs = model->metadata.model.num_input;
+	info->nb_inputs = metadata->model.num_input;
 	info->input_info = input;
-	info->nb_outputs = model->metadata.model.num_output;
+	info->nb_outputs = metadata->model.num_output;
 	info->output_info = output;
-	info->wb_size = model->metadata.weights_bias.file_size;
+	info->wb_size = metadata->weights_bias.file_size;
 
 	/* Set input info */
 	for (i = 0; i < info->nb_inputs; i++) {
-		rte_memcpy(input[i].name, model->metadata.input[i].input_name,
-			   MRVL_ML_INPUT_NAME_LEN);
-		input[i].dtype = model->metadata.input[i].input_type;
-		input[i].qtype = model->metadata.input[i].model_input_type;
-		input[i].shape.format = model->metadata.input[i].shape.format;
-		input[i].shape.w = model->metadata.input[i].shape.w;
-		input[i].shape.x = model->metadata.input[i].shape.x;
-		input[i].shape.y = model->metadata.input[i].shape.y;
-		input[i].shape.z = model->metadata.input[i].shape.z;
+		rte_memcpy(input[i].name, metadata->input[i].input_name, MRVL_ML_INPUT_NAME_LEN);
+		input[i].dtype = metadata->input[i].input_type;
+		input[i].qtype = metadata->input[i].model_input_type;
+		input[i].shape.format = metadata->input[i].shape.format;
+		input[i].shape.w = metadata->input[i].shape.w;
+		input[i].shape.x = metadata->input[i].shape.x;
+		input[i].shape.y = metadata->input[i].shape.y;
+		input[i].shape.z = metadata->input[i].shape.z;
 	}
 
 	/* Set output info */
 	for (i = 0; i < info->nb_outputs; i++) {
-		rte_memcpy(output[i].name, model->metadata.output[i].output_name,
+		rte_memcpy(output[i].name, metadata->output[i].output_name,
 			   MRVL_ML_OUTPUT_NAME_LEN);
-		output[i].dtype = model->metadata.output[i].output_type;
-		output[i].qtype = model->metadata.output[i].model_output_type;
+		output[i].dtype = metadata->output[i].output_type;
+		output[i].qtype = metadata->output[i].model_output_type;
 		output[i].shape.format = RTE_ML_IO_FORMAT_1D;
-		output[i].shape.w = model->metadata.output[i].size;
+		output[i].shape.w = metadata->output[i].size;
 		output[i].shape.x = 1;
 		output[i].shape.y = 1;
 		output[i].shape.z = 1;
-- 
2.17.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v3 0/8] Fixes to ml/cnxk driver
  2023-03-16 21:28 ` [PATCH v3 0/8] Fixes to ml/cnxk driver Srikanth Yalavarthi
                     ` (7 preceding siblings ...)
  2023-03-16 21:29   ` [PATCH v3 8/8] ml/cnxk: reduce levels of nested variables access Srikanth Yalavarthi
@ 2023-03-19 19:01   ` Thomas Monjalon
  8 siblings, 0 replies; 15+ messages in thread
From: Thomas Monjalon @ 2023-03-19 19:01 UTC (permalink / raw)
  To: Srikanth Yalavarthi
  Cc: dev, syalavarthi, sshankarnara, aprabhu, ptakkar, pshukla

> Srikanth Yalavarthi (8):
>   ml/cnxk: fix evaluation order violation issues
>   ml/cnxk: fix potential division by zero
>   ml/cnxk: add pointer check after memory allocation
>   ml/cnxk: remove logically dead code
>   ml/cnxk: fix potential memory leak in xstats
>   ml/cnxk: check for null pointer before dereference
>   ml/cnxk: avoid variable name reuse in a function
>   ml/cnxk: reduce levels of nested variables access

Applied, thanks.




^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2023-03-19 19:01 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-15 13:54 [PATCH 1/1] ml/cnxk: fix multiple coverity issues Srikanth Yalavarthi
2023-03-16  9:33 ` [PATCH v2 " Srikanth Yalavarthi
2023-03-16 17:00   ` Thomas Monjalon
2023-03-16 17:02     ` [EXT] " Srikanth Yalavarthi
2023-03-16 17:07       ` Thomas Monjalon
2023-03-16 21:28 ` [PATCH v3 0/8] Fixes to ml/cnxk driver Srikanth Yalavarthi
2023-03-16 21:28   ` [PATCH v3 1/8] ml/cnxk: fix evaluation order violation issues Srikanth Yalavarthi
2023-03-16 21:28   ` [PATCH v3 2/8] ml/cnxk: fix potential division by zero Srikanth Yalavarthi
2023-03-16 21:28   ` [PATCH v3 3/8] ml/cnxk: add pointer check after memory allocation Srikanth Yalavarthi
2023-03-16 21:29   ` [PATCH v3 4/8] ml/cnxk: remove logically dead code Srikanth Yalavarthi
2023-03-16 21:29   ` [PATCH v3 5/8] ml/cnxk: fix potential memory leak in xstats Srikanth Yalavarthi
2023-03-16 21:29   ` [PATCH v3 6/8] ml/cnxk: check for null pointer before dereference Srikanth Yalavarthi
2023-03-16 21:29   ` [PATCH v3 7/8] ml/cnxk: avoid variable name reuse in a function Srikanth Yalavarthi
2023-03-16 21:29   ` [PATCH v3 8/8] ml/cnxk: reduce levels of nested variables access Srikanth Yalavarthi
2023-03-19 19:01   ` [PATCH v3 0/8] Fixes to ml/cnxk driver Thomas Monjalon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).