[4/4] dmadev: clean code for verify parameter

Message ID 20240905064638.17980-5-haijie1@huawei.com (mailing list archive)
State New
Delegated to: David Marchand
Headers
Series clean code for dmadev and hns3 |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/intel-Functional success Functional PASS
ci/iol-marvell-Functional success Functional Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS

Commit Message

Jie Hai Sept. 5, 2024, 6:46 a.m. UTC
From: Chengwen Feng <fengchengwen@huawei.com>

Make sure to verify the 'dev_id' parameter before using them.
Note: there is no actual problem with the current code, but it is
recommended to clean it up.

Cc: stable@dpdk.org

Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
 lib/dmadev/rte_dmadev.c | 30 ++++++++++++++++++++----------
 1 file changed, 20 insertions(+), 10 deletions(-)
  

Patch

diff --git a/lib/dmadev/rte_dmadev.c b/lib/dmadev/rte_dmadev.c
index 60c3d8ebf68e..4ba3b9380c5f 100644
--- a/lib/dmadev/rte_dmadev.c
+++ b/lib/dmadev/rte_dmadev.c
@@ -436,11 +436,12 @@  rte_dma_count_avail(void)
 int
 rte_dma_info_get(int16_t dev_id, struct rte_dma_info *dev_info)
 {
-	const struct rte_dma_dev *dev = &rte_dma_devices[dev_id];
+	const struct rte_dma_dev *dev;
 	int ret;
 
 	if (!rte_dma_is_valid(dev_id) || dev_info == NULL)
 		return -EINVAL;
+	dev = &rte_dma_devices[dev_id];
 
 	if (*dev->dev_ops->dev_info_get == NULL)
 		return -ENOTSUP;
@@ -462,12 +463,13 @@  rte_dma_info_get(int16_t dev_id, struct rte_dma_info *dev_info)
 int
 rte_dma_configure(int16_t dev_id, const struct rte_dma_conf *dev_conf)
 {
-	struct rte_dma_dev *dev = &rte_dma_devices[dev_id];
 	struct rte_dma_info dev_info;
+	struct rte_dma_dev *dev;
 	int ret;
 
 	if (!rte_dma_is_valid(dev_id) || dev_conf == NULL)
 		return -EINVAL;
+	dev = &rte_dma_devices[dev_id];
 
 	if (dev->data->dev_started != 0) {
 		RTE_DMA_LOG(ERR,
@@ -513,11 +515,12 @@  rte_dma_configure(int16_t dev_id, const struct rte_dma_conf *dev_conf)
 int
 rte_dma_start(int16_t dev_id)
 {
-	struct rte_dma_dev *dev = &rte_dma_devices[dev_id];
+	struct rte_dma_dev *dev;
 	int ret;
 
 	if (!rte_dma_is_valid(dev_id))
 		return -EINVAL;
+	dev = &rte_dma_devices[dev_id];
 
 	if (dev->data->dev_conf.nb_vchans == 0) {
 		RTE_DMA_LOG(ERR, "Device %d must be configured first", dev_id);
@@ -545,11 +548,12 @@  rte_dma_start(int16_t dev_id)
 int
 rte_dma_stop(int16_t dev_id)
 {
-	struct rte_dma_dev *dev = &rte_dma_devices[dev_id];
+	struct rte_dma_dev *dev;
 	int ret;
 
 	if (!rte_dma_is_valid(dev_id))
 		return -EINVAL;
+	dev = &rte_dma_devices[dev_id];
 
 	if (dev->data->dev_started == 0) {
 		RTE_DMA_LOG(WARNING, "Device %d already stopped", dev_id);
@@ -572,11 +576,12 @@  rte_dma_stop(int16_t dev_id)
 int
 rte_dma_close(int16_t dev_id)
 {
-	struct rte_dma_dev *dev = &rte_dma_devices[dev_id];
+	struct rte_dma_dev *dev;
 	int ret;
 
 	if (!rte_dma_is_valid(dev_id))
 		return -EINVAL;
+	dev = &rte_dma_devices[dev_id];
 
 	/* Device must be stopped before it can be closed */
 	if (dev->data->dev_started == 1) {
@@ -600,13 +605,14 @@  int
 rte_dma_vchan_setup(int16_t dev_id, uint16_t vchan,
 		    const struct rte_dma_vchan_conf *conf)
 {
-	struct rte_dma_dev *dev = &rte_dma_devices[dev_id];
 	struct rte_dma_info dev_info;
 	bool src_is_dev, dst_is_dev;
+	struct rte_dma_dev *dev;
 	int ret;
 
 	if (!rte_dma_is_valid(dev_id) || conf == NULL)
 		return -EINVAL;
+	dev = &rte_dma_devices[dev_id];
 
 	if (dev->data->dev_started != 0) {
 		RTE_DMA_LOG(ERR,
@@ -693,10 +699,11 @@  rte_dma_vchan_setup(int16_t dev_id, uint16_t vchan,
 int
 rte_dma_stats_get(int16_t dev_id, uint16_t vchan, struct rte_dma_stats *stats)
 {
-	const struct rte_dma_dev *dev = &rte_dma_devices[dev_id];
+	const struct rte_dma_dev *dev;
 
 	if (!rte_dma_is_valid(dev_id) || stats == NULL)
 		return -EINVAL;
+	dev = &rte_dma_devices[dev_id];
 
 	if (vchan >= dev->data->dev_conf.nb_vchans &&
 	    vchan != RTE_DMA_ALL_VCHAN) {
@@ -715,11 +722,12 @@  rte_dma_stats_get(int16_t dev_id, uint16_t vchan, struct rte_dma_stats *stats)
 int
 rte_dma_stats_reset(int16_t dev_id, uint16_t vchan)
 {
-	struct rte_dma_dev *dev = &rte_dma_devices[dev_id];
+	struct rte_dma_dev *dev;
 	int ret;
 
 	if (!rte_dma_is_valid(dev_id))
 		return -EINVAL;
+	dev = &rte_dma_devices[dev_id];
 
 	if (vchan >= dev->data->dev_conf.nb_vchans &&
 	    vchan != RTE_DMA_ALL_VCHAN) {
@@ -739,10 +747,11 @@  rte_dma_stats_reset(int16_t dev_id, uint16_t vchan)
 int
 rte_dma_vchan_status(int16_t dev_id, uint16_t vchan, enum rte_dma_vchan_status *status)
 {
-	struct rte_dma_dev *dev = &rte_dma_devices[dev_id];
+	struct rte_dma_dev *dev;
 
 	if (!rte_dma_is_valid(dev_id) || status == NULL)
 		return -EINVAL;
+	dev = &rte_dma_devices[dev_id];
 
 	if (vchan >= dev->data->dev_conf.nb_vchans) {
 		RTE_DMA_LOG(ERR, "Device %u vchan %u out of range", dev_id, vchan);
@@ -804,12 +813,13 @@  dma_dump_capability(FILE *f, uint64_t dev_capa)
 int
 rte_dma_dump(int16_t dev_id, FILE *f)
 {
-	const struct rte_dma_dev *dev = &rte_dma_devices[dev_id];
+	const struct rte_dma_dev *dev;
 	struct rte_dma_info dev_info;
 	int ret;
 
 	if (!rte_dma_is_valid(dev_id) || f == NULL)
 		return -EINVAL;
+	dev = &rte_dma_devices[dev_id];
 
 	ret = rte_dma_info_get(dev_id, &dev_info);
 	if (ret != 0) {