From nobody Mon Apr 13 21:01:10 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6A8AA389107 for ; Wed, 4 Mar 2026 08:07:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772611644; cv=none; b=dJHQ9ln1Z7bR+N9wgkXmNqWb5BxhjPM8Tk5FIJxW3jtF5rQoc2/j9EJzNxKSBikF7+Yco5NVE1j4divR6gBwTmQjSHxkPWwFaL1IWl3k11LY4H4v7Qullo4WE8EuboL4sv6PsZ5HsuX8IRuigYItlXTbW4tjVL39E7zVCfJSCNQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772611644; c=relaxed/simple; bh=ECZkvFYUed+381xaLK5V5ceV1IEVrr/yAiQkDAC7vwc=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=Sxds7oULsmgMH5VZLqChkKL6CpUXW8xgd4LgqCVlZhQPgwDX9KFhcm9e/mlfU+7C9v6T/Mjj/L6iEqnBFCWBSdBnqY9xTSWqSmUrjmk/Mvtb0YGjqqaxPUKkKPPVielrehs9PKYxgmvdvF5VUh5S02Wgyd1+s/ZTRXDTSnnA6do= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--liulynn.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=lA8vTFgf; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--liulynn.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lA8vTFgf" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2ae47b3adacso31427535ad.3 for ; Wed, 04 Mar 2026 00:07:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772611642; x=1773216442; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=49qpq9KbDR/+8tqM5T7B4wKYI1YtV2DTAFLCoKtW5+0=; b=lA8vTFgfmoCnPAqheJV2WWklSU9MC0ZzMkOaxpVoh4EZuObBHHwoSGE1UAvBrQYMo/ wGO0x+VjfZ+2Rtc0wyNFGgo6kue76pXHVKbm32zbdVgK7dFR9NXrzST2pyGtEd+Lmh37 pGJTT8R+BGT8nhjoFqxPKgn3jQAxeB1Z0llUCvpf6QPlbGp06BfSN7X58bN97T8bjsxA emPAeWIujab2vQfe2rYSrMJa46U521HKZbOHz8G3/ZipOcwPWpl/wnoYmzgxcgQRHif2 +dNqiLKSs/yTCqYcDRSFPu3/kCNwdjgJTJhszZOh73iLyQHQmIsRqMmdbY212/pZ0xsB G4Zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772611642; x=1773216442; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=49qpq9KbDR/+8tqM5T7B4wKYI1YtV2DTAFLCoKtW5+0=; b=sDB3gXj8qiqAAWUfzZv+4BpB0UkRuZzfzP1MTtTwiiJ9JL6IjmHPp11DLvzr2QASWh JDeuKmOJBiCNoymsANg25l2mJpUo2uJ7XB8ZHmbCw+fFQsS4bYsPnDUhZdDqqtZyjMj5 G7YUowM2RoZKG7TieE39VtOhS0o4/Bb8qCRJtcYTDmzlH7haEv3oRY3r+nVAlO1dfB/H 0sPl3IpqzXxAokSyZpmGI8cstiEk5Fh+yVIdPZT1IRgKmcUIPsc4JTxmK6hSuyvgIvsV F02XjcTMyPj0nM3+q4bpNFxBv3iAsK1Wl5oSx+YiyOpDFkpT/5szp1Nfc6GHRasMkvwr RXsA== X-Forwarded-Encrypted: i=1; AJvYcCUqM5pxyoQAKxXgpt54HE+6AfNFzYOQ8KFYgWpTvoJr7kDaPsCy8X8KAqHAEldkYh5BZpFyQqLkS6SGFbM=@vger.kernel.org X-Gm-Message-State: AOJu0Yw+Uoq2h5F+bG0xuA0AoDD096V75vQgCtqWJDUwbjfTQQaTKN6w Wu/Gb1Rs1OEaCWs5bOFc6c4HDVQMa0nYFxzwpuwSbY6rSUj+oKl8lzp+lcn1ES7HwLN1SBYmtkg jS+rkQuTOEg== X-Received: from pldd2.prod.google.com ([2002:a17:902:c182:b0:2ad:8161:bc65]) (user=liulynn job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:db0f:b0:2a9:622e:fd3e with SMTP id d9443c01a7336-2ae6aa04cecmr13694195ad.4.1772611642026; Wed, 04 Mar 2026 00:07:22 -0800 (PST) Date: Wed, 4 Mar 2026 08:06:36 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260304080636.167426-1-liulynn@google.com> Subject: [PATCH v2] iommu/iova: Add NULL check in iova_magazine_free() From: Lynn Liu To: joro@8bytes.org, will@kernel.org, robin.murphy@arm.com Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Lynn Liu Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- v1: https://lore.kernel.org/linux-iommu/20260214080919.390846-1-liulynn@goo= gle.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) =20 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); } =20 static void --=20 2.53.0.473.g4a7958ca14-goog