From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) (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 198083EFFD7 for ; Tue, 25 Aug 2026 10:06:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787652379; cv=none; b=Jv1e3ATKPy67tLIOlVxqfxpzKlB5PGF1+TCpwnmXcrvg/WnJV1YMeZPsHZbjB575rX2BKQZzc7UxHtL9FsYggcmi83J4kAzKHskI21EW7Eb/yJ5870dUnANGLtifFpeKdW3E2G+2x0C5YNYYniCO8k4ygFXA7qDix4RzB01Yr7A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787652379; c=relaxed/simple; bh=kC0aTN0K7u9RCDOE2X6e4KofmqQ39JXeOz9FhSIz23Y=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=R35zLxY69CbkxdMwRHWQLpa8kumlwRBEglajKp29OFZ8WT3c50K750MJbBFLvFXTT3y1TwQidoWBX6CwoZD5m3NTiYpdNkviOAgdTme17UnDX6FA+DPVNC6acHFvOpyDoICWTnqlQ1dmtfHnMGg/lyscSsoe7KtvggVzl7x37Cs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=BpU5xTQH; arc=none smtp.client-ip=209.85.167.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BpU5xTQH" Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-4ab8cb2a6e4so4089734b6e.0 for ; Tue, 25 Aug 2026 03:06:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787652377; x=1788257177; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=ITNgIlTwsBrNBLrYNkaCeP0gJOrZpKRql3oj7U2AlDY=; b=BpU5xTQHzyeaoK2Pq37v16hnf0Q6LTvUHBdS+FblRGFLjhXmg3+owhfUOqWqFGqY2H suhn8fvDYsWpmVi9uYiW7QLMQQ0Xd4lmvdQSgKBH7jSQPJJiWhDoFSV0HHcEdgmGrmZB YVJPdBX4KO7nSQ6XIUi1Klhldjn9DdZ3T09mBxOXZcYoQu3bK/xZm5y5UdmysKE+Vg/a eUtBRrECQd8JVuBGx3PKM8OeJnb78Fs+oDh23mCF+OVMnxtqNWOOg2JQjwXWmZZb3aWo I3LyadA2Ek1R7DsbY+O036KpZUoM8D68QUObVGLxdpGX1Dnh+SMrAiHIfDd89Ck14Gi+ S4tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787652377; x=1788257177; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ITNgIlTwsBrNBLrYNkaCeP0gJOrZpKRql3oj7U2AlDY=; b=Z10z39QOxCMge4GP9Yj1wX9cuNuBsA0gRP3ZMJLzvimJwUJJA9ocjhW/j6bPRJHNLR KRoNBKsxQtM383ZjUAc81cUEHzQezlfZND9kIfsTq8zfAFApaPaV4obDnS+WOlg7714n BvlnmdZtzme/V8knEN72CCeHjO24q0IgKCSyxpp3ySqUkrlK2BOfuqNgue2bu9uvhbP6 1I0ww4gHQcb6hPvn3V6Dx4gyUGNOk3ELqWh0UV+2JyHYSNa8qZAXutqDq+Epv6irFDwL j5J5Lq+CdZMuyeO4X85oc5E0OEipR4d/U4eOGRsLXDiop+gj66zQ/OcjO6gEuvRs+Thw oUFw== X-Forwarded-Encrypted: i=1; AHgh+RqL/o0jBh32xxHlYwaGbLs3EZLoWkcyKNOR7xpsYkBk+SxfraRwbXLB9GQAutrRPMfDig648Mddi9oHM4Y=@vger.kernel.org X-Gm-Message-State: AFuF++nAPZHXvEEVupNHOc3kHbDuwZWFkh0sjg5P06uIkPFowxAJLaqV aFk/l93PZMZ8AY9QExNC66Go7ZQXkwPfCC4JBM9WGD86eSdoWBO62a+j X-Gm-Gg: AR+sD13R9CLo6aRnBPgqsqGc3W03ohyaT+CA5UysMFJcyW1PZwX27oux+qDL/xPCcZ5 jigw6/zCFR6v5O/Yhx0gvsJftKYhFub8QdmXOF8qgDf9s8LlFYoAQtJIyi1/tJ1gcW3ToZcg1Ej a/0Fotc7c3c7US0aBTbNth2vAIKkJkUvs/qVzJmuJ6CuVWGPBO5z72GGqBbPo3Y0oL7btSwO1js YvNwF4XJBtsRdhFwEGuQqJldmYjgjbbmXCMFF+6pxpJXQ+niTPBmTDloFTKz/dTrln8yXpZ8TKv ldMpunCHgyILNmWdtfhEmsLbO30hMUCPtnRyish6g/kP3rjbHxkxNnn1SPWyx33tY3XJt1B6yPi jFJ53yCxB391/P8b5ZZXrSTxnH9hhZ1XugzKDU3lR1h1CugQIgxxpRs6nsKOzJlDtp/brUv+OHW vOwLmxlW5q2gEz91FsXZr8AKb+RNxxP8VO5Hcsj+y1RGmLXAqm5HSIIc2AWjAILWIKonAJI+H/l kDOYHkNj1c= X-Received: by 2002:a05:6808:1201:b0:4a3:3108:866a with SMTP id 5614622812f47-4b2ef480315mr28341239b6e.17.1787652376848; Tue, 25 Aug 2026 03:06:16 -0700 (PDT) Received: from volcano9f8e-host.amd.com ([165.204.217.251]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-14186183f31sm29502922c88.13.2026.08.25.03.06.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Aug 2026 03:06:16 -0700 (PDT) From: Hemanth Selam 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 Message-ID: <20260825100554.2787577-1-hemanth.selam@gmail.com> X-Mailer: git-send-email 2.43.7 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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