From: Hemanth Selam <hemanth.selam@gmail.com>
To: joro@8bytes.org
Cc: suravee.suthikulpanit@amd.com, vasant.hegde@amd.com,
will@kernel.org, robin.murphy@arm.com, jgg@nvidia.com,
iommu@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH] iommu/amd: Fix ineffective error check in nested domain allocation
Date: Tue, 25 Aug 2026 15:35:54 +0530 [thread overview]
Message-ID: <20260825100554.2787577-1-hemanth.selam@gmail.com> (raw)
amd_iommu_pdom_id_alloc() returns an int: a domain ID on success, or the
negative errno from ida_alloc_range() when the ID space is exhausted or
memory is short. amd_iommu_alloc_domain_nested() stores that return value
in gdom_info->hdom_id, which is a u32, and only then tests it:
gdom_info->hdom_id = amd_iommu_pdom_id_alloc();
if (gdom_info->hdom_id <= 0) {
The assignment discards the sign, so -ENOSPC becomes 0xffffffe4 and the
test never fires. The nested domain is then set up with a host domain ID
that was never allocated, instead of the allocation failing with -ENOSPC.
Keep the value in an int, test it there, and store it only once it is
known to be valid, which is what the other amd_iommu_pdom_id_alloc()
callers already do.
Fixes: 757d2b1fdf5b ("iommu/amd: Introduce gDomID-to-hDomID Mapping and handle parent domain invalidation")
Signed-off-by: Hemanth Selam <hemanth.selam@gmail.com>
---
Verified by driving amd_iommu_alloc_domain_nested() directly from a debugfs
hook, since the normal entry point needs iommufd to request a nested HWPT on
real hardware. No error injection was used: the harness allocates domain IDs
until ida_alloc_range() genuinely runs out, then calls the function with a
zeroed guest DTE, which is what validate_gdte_nested() accepts.
Without this patch:
NESTEDTEST: first alloc, IDs available -> success, hdom_id=1 (0x00000001)
NESTEDTEST: second alloc, same gDomID (reuse) -> success, hdom_id=1 (0x00000001)
NESTEDTEST: exhausted the ID space after 65534 IDs, allocator returns -28
NESTEDTEST: third alloc, IDs exhausted -> success, hdom_id=4294967268 (0xffffffe4)
With this patch:
NESTEDTEST: first alloc, IDs available -> success, hdom_id=1 (0x00000001)
NESTEDTEST: second alloc, same gDomID (reuse) -> success, hdom_id=1 (0x00000001)
NESTEDTEST: exhausted the ID space after 65534 IDs, allocator returns -28
NESTEDTEST: third alloc, IDs exhausted -> error -28
The successful and the reuse paths are unchanged; only the exhausted case
differs, and the result was the same in three runs of each kernel. Built with
CONFIG_AMD_IOMMU_IOMMUFD=y; sparse and checkpatch are clean.
drivers/iommu/amd/nested.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/drivers/iommu/amd/nested.c b/drivers/iommu/amd/nested.c
index 63b53b29e029..f1c7987fc585 100644
--- a/drivers/iommu/amd/nested.c
+++ b/drivers/iommu/amd/nested.c
@@ -96,7 +96,7 @@ struct iommu_domain *
amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
const struct iommu_user_data *user_data)
{
- int ret;
+ int ret, hdom_id;
unsigned long irqflags;
struct nested_domain *ndom;
struct guest_domain_mapping_info *gdom_info;
@@ -161,8 +161,8 @@ amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
}
/* The gDomID does not exist. We allocate new hdom_id */
- gdom_info->hdom_id = amd_iommu_pdom_id_alloc();
- if (gdom_info->hdom_id <= 0) {
+ hdom_id = amd_iommu_pdom_id_alloc();
+ if (hdom_id <= 0) {
__xa_cmpxchg(&aviommu->gdomid_array,
ndom->gdom_id, gdom_info, NULL, GFP_ATOMIC);
xa_unlock_irqrestore(&aviommu->gdomid_array, irqflags);
@@ -170,6 +170,7 @@ amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
goto out_err_gdom_info;
}
+ gdom_info->hdom_id = hdom_id;
ndom->gdom_info = gdom_info;
refcount_set(&gdom_info->users, 1);
--
2.43.7
next reply other threads:[~2026-08-25 10:06 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-25 10:05 Hemanth Selam [this message]
2026-08-25 11:14 ` [PATCH] iommu/amd: Fix ineffective error check in nested domain allocation Vasant Hegde
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260825100554.2787577-1-hemanth.selam@gmail.com \
--to=hemanth.selam@gmail.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@nvidia.com \
--cc=joro@8bytes.org \
--cc=linux-kernel@vger.kernel.org \
--cc=robin.murphy@arm.com \
--cc=suravee.suthikulpanit@amd.com \
--cc=vasant.hegde@amd.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox