mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] iommu/iova: Add NULL check in iova_magazine_free()
@ 2026-03-04  8:06 Lynn Liu
  2026-03-04 18:41 ` Robin Murphy
  0 siblings, 1 reply; 3+ messages in thread
From: Lynn Liu @ 2026-03-04  8:06 UTC (permalink / raw)
  To: joro, will, robin.murphy; +Cc: iommu, linux-kernel, Lynn Liu

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.

The crash can be observed when fault injection triggers a failure in
iova_magazine_alloc():

pc : _compound_head include/linux/page-flags.h:251 [inline]
pc : virt_to_folio include/linux/mm.h:1395 [inline]
pc : virt_to_slab mm/slab.h:215 [inline]
pc : kmem_cache_free+0x90/0x5c4 mm/slub.c:4769
...
Call trace:
 trace_kmem_cache_free include/trace/events/kmem.h:114 [inline]
 kmem_cache_free+0x90/0x5c4 mm/slub.c:4768
 iova_magazine_free drivers/iommu/iova.c:623 [inline]
 free_iova_rcaches+0x114/0x344 drivers/iommu/iova.c:894
 iova_domain_init_rcaches drivers/iommu/iova.c:762 [inline]
 iova_domain_init_rcaches+0x3d0/0x528 drivers/iommu/iova.c:717

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

Fixes: 84e6f56be9c6 ("iommu/iova: use named kmem_cache for iova magazines")
Signed-off-by: Lynn Liu <liulynn@google.com>
---
v1: https://lore.kernel.org/linux-iommu/20260214080919.390846-1-liulynn@google.com/T/#u
Changelog in v1 -> v2:
 - Update author name to full name.
 - Add Fixes tag.
 - Include the Call trace from the kernel crash.

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

diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c
index 18f839721813..e026be5e068b 100644
--- a/drivers/iommu/iova.c
+++ b/drivers/iommu/iova.c
@@ -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
-- 
2.53.0.473.g4a7958ca14-goog


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

end of thread, other threads:[~2026-03-09 18:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-04  8:06 [PATCH v2] iommu/iova: Add NULL check in iova_magazine_free() Lynn Liu
2026-03-04 18:41 ` Robin Murphy
2026-03-09 18:41   ` [PATCH v3] " Lynn Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®