Skip to contentSkip to conversation

iommu/iova: Add NULL check in iova_magazine_free()

PATCH Applied Lynn Liu 2 participants 2 messages Feb 14 – Mar 17

Subsystems (2)

v1 of 3 Applied Latest is v3 →

lore
Share & tools
lore

Lynn Liu

author
#0 lore raw

PATCH 1/1 iommu/iova: Add NULL check in iova_magazine_free()

From: lynn <[email protected]>

When iova_domain_init_rcaches() fails to allocate an iova_magazine during the initialization of per-cpu rcaches, it jumps to out_err and calls free_iova_rcaches() for cleanup.

In free_iova_rcaches(), the code iterates through all possible CPUs to free both cpu_rcache->loaded and cpu_rcache->prev. However, if the original allocation failed mid-way through the CPU loop, the pointers for the remaining CPUs remain NULL.

Since kmem_cache_free() does not explicitly handle NULL pointers like kfree() does, passing these NULL pointers leads to a kernel paging request fault.

Add a NULL check in iova_magazine_free() to safely handle partially initialized rcaches in error paths.

---
 drivers/iommu/iova.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/iova.c +2 -1
index 18f839721813..e026be5e068b 100644
@@ -611,7 +611,8 @@ static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
 
 static void iova_magazine_free(struct iova_magazine *mag)
 {
-	kmem_cache_free(iova_magazine_cache, mag);
+	if (mag)
+		kmem_cache_free(iova_magazine_cache, mag);
 }
 
 static void
Signature · 1 line
2.53.0.273.g2a3d683680-goog
maintainer Applied
#1 lore raw
Quoted 15 lines · Lynn Liu

From: lynn <[email protected]>

When iova_domain_init_rcaches() fails to allocate an iova_magazine during the initialization of per-cpu rcaches, it jumps to out_err and calls free_iova_rcaches() for cleanup.

In free_iova_rcaches(), the code iterates through all possible CPUs to free both cpu_rcache->loaded and cpu_rcache->prev. However, if the original allocation failed mid-way through the CPU loop, the pointers for the remaining CPUs remain NULL.

Since kmem_cache_free() does not explicitly handle NULL pointers like kfree() does, passing these NULL pointers leads to a kernel paging request fault.

Quoted 7 lines

Add a NULL check in iova_magazine_free() to safely handle partially initialized rcaches in error paths.

---
 drivers/iommu/iova.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Applied, thanks.