Skip to contentSkip to conversation

vfio_iommu_type1: increase the validity check of function parameters

PATCH In review Li kunyu 2 participants 2 messages Dec 23, 2022

Subsystems (2)
lore
Share & tools
lore

Li kunyu

author
#0 lore raw

PATCH 1/1 vfio_iommu_type1: increase the validity check of function parameters

Added validity check for count variable, return if count variable does not meet the execution condition (do not execute mutex_lock and mutex_unlock function).

---
 drivers/vfio/vfio_iommu_type1.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/vfio/vfio_iommu_type1.c +3 -0
index 23c24fe98c00..9bdf96d932e4 100644
@@ -3137,6 +3137,9 @@ static int vfio_iommu_type1_dma_rw(void *iommu_data, dma_addr_t user_iova,
 	int ret = 0;
 	size_t done;
 
+	if (count <= 0)
+		return ret;
+
 	mutex_lock(&iommu->lock);
 	while (count > 0) {
 		ret = vfio_iommu_type1_dma_rw_chunk(iommu, user_iova, data,
Signature · 1 line
2.18.2
#1 lore raw
Quoted 14 lines · On Fri, 23 Dec 2022 15:24:18 +0800 Li kunyu <[email protected]> wrote:

Added validity check for count variable, return if count variable does not meet the execution condition (do not execute mutex_lock and mutex_unlock function).

---
 drivers/vfio/vfio_iommu_type1.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/vfio/vfio_iommu_type1.c +0 -0
index 23c24fe98c00..9bdf96d932e4 100644

drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_dma_rw(void *iommu_data, dma_addr_t user_iova, · line 3145

Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +3 -0
@@ -3137,6 +3137,9 @@ static int vfio_iommu_type1_dma_rw(void *iommu_data, dma_addr_t user_iova,
 	int ret = 0;
 	size_t done;
 
+	if (count <= 0)
+		return ret;
+
 	mutex_lock(&iommu->lock);
 	while (count > 0) {
 		ret = vfio_iommu_type1_dma_rw_chunk(iommu, user_iova, data,

This is only optimizing a case that shouldn't exist, the return value is the same. Callers should be smart enough not to call the function with such values. As an internal API, we assume reasonable behavior by the caller. Thanks,

Alex