Skip to contentSkip to conversation

iommufd: viommu: free memory allocated by kvcalloc() using kvfree()

PATCH Applied Akhilesh Patil 7 participants 8 messages Aug 10, 2025 – Aug 18, 2025

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

PATCH 1/1 iommufd: viommu: free memory allocated by kvcalloc() using kvfree()

Use kvfree() instead of kfree() to free pages allocated by kvcalloc() in iommufs_hw_queue_alloc_phys() to fix potential memory corruption. Ensure the memory is properly freed, as kvcalloc may internally use vmalloc or kmalloc depending on available memory in the system.

Fixes 2238ddc2b056 ("iommufd/viommu: Add IOMMUFD_CMD_HW_QUEUE_ALLOC ioctl")
---
 drivers/iommu/iommufd/viommu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/iommufd/viommu.c +2 -2
index 2ca5809b238b..462b457ffd0c 100644
@@ -339,7 +339,7 @@ iommufd_hw_queue_alloc_phys(struct iommu_hw_queue_alloc *cmd,
 	}
 
 	*base_pa = (page_to_pfn(pages[0]) << PAGE_SHIFT) + offset;
-	kfree(pages);
+	kvfree(pages);
 	return access;
 
 out_unpin:
@@ -349,7 +349,7 @@ iommufd_hw_queue_alloc_phys(struct iommu_hw_queue_alloc *cmd,
 out_destroy:
 	iommufd_access_destroy_internal(viommu->ictx, access);
 out_free:
-	kfree(pages);
+	kvfree(pages);
 	return ERR_PTR(rc);
 }
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Akhilesh Patil

Use kvfree() instead of kfree() to free pages allocated by kvcalloc() in iommufs_hw_queue_alloc_phys() to fix potential memory corruption. Ensure the memory is properly freed, as kvcalloc may internally use vmalloc or kmalloc depending on available memory in the system.

Fixes 2238ddc2b056 ("iommufd/viommu: Add IOMMUFD_CMD_HW_QUEUE_ALLOC ioctl")

Thanks!

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Akhilesh Patil
Quoted 11 lines

From: Akhilesh Patil <[email protected]>
Sent: Sunday, August 10, 2025 9:34 PM

Use kvfree() instead of kfree() to free pages allocated by kvcalloc() in iommufs_hw_queue_alloc_phys() to fix potential memory corruption. Ensure the memory is properly freed, as kvcalloc may internally use vmalloc or kmalloc depending on available memory in the system.

Fixes 2238ddc2b056 ("iommufd/viommu: Add

IOMMUFD_CMD_HW_QUEUE_ALLOC ioctl")

Reviewed-by on v1 1/1
#3 lore raw
In reply to Akhilesh Patil
Quoted 7 lines · Akhilesh Patil

Use kvfree() instead of kfree() to free pages allocated by kvcalloc() in iommufs_hw_queue_alloc_phys() to fix potential memory corruption. Ensure the memory is properly freed, as kvcalloc may internally use vmalloc or kmalloc depending on available memory in the system.

Fixes 2238ddc2b056 ("iommufd/viommu: Add IOMMUFD_CMD_HW_QUEUE_ALLOC ioctl")

Thanks,
Praan

#4 lore raw
In reply to Akhilesh Patil
Quoted 4 lines

Use kvfree() instead of kfree() to free pages allocated by kvcalloc() in iommufs_hw_queue_alloc_phys() to fix potential memory corruption. Ensure the memory is properly freed, as kvcalloc may internally use vmalloc or kmalloc depending on available memory in the system.

Will another bit of background information become helpful for an improved change description?

Test command example:
Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> /usr/bin/spatch -D report scripts/coccinelle/api/kfree_mismatch.cocci drivers/iommu/iommufd/viommu.c …

Regards,
Markus

#5 lore raw
Quoted 7 lines · Markus Elfring
Quoted 4 lines

Use kvfree() instead of kfree() to free pages allocated by kvcalloc() in iommufs_hw_queue_alloc_phys() to fix potential memory corruption. Ensure the memory is properly freed, as kvcalloc may internally use vmalloc or kmalloc depending on available memory in the system.

Will another bit of background information become helpful for an improved change description?

No it will not. Please stop pointlessly nitpicking. A thing that is allocated with kv*alloc() must be freed with kvfree(); that alone is all that needs to be said to explain and justify any patch fixing this particular bug.

Thanks,
Robin.

#6 lore raw
Quoted 4 lines
Quoted 2 lines

Will another bit of background information become helpful for an improved change description?

No it will not. Please stop pointlessly nitpicking. …

Do you care for help and further influence of automatic source code analysis tools?

Regards,
Markus

maintainer Applied
#7 lore raw
In reply to Akhilesh Patil
Quoted 10 lines · Akhilesh Patil

Use kvfree() instead of kfree() to free pages allocated by kvcalloc() in iommufs_hw_queue_alloc_phys() to fix potential memory corruption. Ensure the memory is properly freed, as kvcalloc may internally use vmalloc or kmalloc depending on available memory in the system.

Fixes 2238ddc2b056 ("iommufd/viommu: Add IOMMUFD_CMD_HW_QUEUE_ALLOC ioctl")
---
 drivers/iommu/iommufd/viommu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Applied to for-rc

Thanks,
Jason