From: sashiko-bot@kernel.org
To: "Samiullah Khawaja" <skhawaja@google.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v8 13/18] iommu: Restore and reattach preserved domains to devices
Date: Mon, 23 Nov 2026 05:18:33 +0000 [thread overview]
Message-ID: <sashiko-outbox-228711@kernel.org> (raw)
In-Reply-To: <20261123044949.3610702-14-skhawaja@google.com>
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] iommu: inconsistent group domain state in
iommu_setup_default_domain()
--
commit c2881b6423bf649bce482244af7dd51d680e43e3
Author: Samiullah Khawaja <skhawaja@google.com>
iommu: Restore and reattach preserved domains to devices
This commit restores preserved IOMMU domains via the iommupt restore()
operation during default domain setup and reattaches them to preserved
devices and their IOMMU groups.
> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> index f8837ab1b526d..c60af471ec32f 100644
> --- a/drivers/iommu/iommu.c
> +++ b/drivers/iommu/iommu.c
[ ... ]
> @@ -3161,6 +3223,66 @@ int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids)
> }
> EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
>
> +static struct device *__iommu_group_restored_device(struct iommu_group *group)
> +{
> + struct group_device *gdev;
> +
> + for_each_group_device(group, gdev) {
> + if (dev_is_pci(gdev->dev) && dev_iommu_restored_state(gdev->dev))
> + return gdev->dev;
> + }
> +
> + return NULL;
> +}
> +
> +static int __iommu_device_restore_domain(struct device *dev)
> +{
> + struct iommu_group *group = dev->iommu_group;
> + struct iommu_device_ser *device_ser;
> + struct iommu_domain *domain;
> + int ret;
> +
> + lockdep_assert_held(&group->mutex);
> + if (group->domain)
> + return -EBUSY;
> +
> + device_ser = dev_iommu_restored_state(dev);
> + if (!device_ser)
> + return -ENOENT;
> +
> + /*
> + * Ownership of groups with preserved devices is set during boot. These
> + * will be reclaimed later by the entity (iommufd) that preserved them.
> + * Claim ownership first so that even if the reservation or the domain
> + * restoration fails, no driver can bind to the device using
> + * default_domain.
> + */
> + WARN_ON(group->owner);
> + group->owner = device_ser;
> + group->owner_cnt = 1;
> +
> + /* A restored device must be the only device in its group */
> + ret = __iommu_group_reserve_liveupdate(group);
> + if (ret)
> + return ret;
> +
> + ret = __iommu_group_alloc_blocking_domain(group);
> + if (ret)
> + return ret;
> +
> + /*
> + * If attaching the restored domain fails below, the domain is not
> + * leaked; it remains cached in domain_ser->restored_domain and will be
> + * reused if the device is rescanned.
> + */
> + domain = iommu_restore_domain(dev, device_ser);
> + if (IS_ERR(domain))
> + return PTR_ERR(domain);
> +
> + /* The restored domain is attached with the restored device. */
> + return __iommu_group_set_domain(group, domain);
> +}
[ ... ]
> @@ -3219,6 +3342,18 @@ static int iommu_setup_default_domain(struct iommu_group *group,
>
> /* We must set default_domain early for __iommu_device_set_domain */
> group->default_domain = dom;
> +
> + /*
> + * Preserved devices need to be attached to the restored domain. If the
> + * restore fails, the group stays owned, so the default_domain is never
> + * attached or used.
> + */
> + dev = __iommu_group_restored_device(group);
> + if (dev) {
> + WARN_ON(__iommu_device_restore_domain(dev));
> + goto out_free_old;
> + }
[Severity: Medium]
Can iommu_setup_default_domain() leave group->default_domain set while
group->domain is NULL when __iommu_device_restore_domain() fails?
If __iommu_device_restore_domain() fails (for example, when
__iommu_group_reserve_liveupdate() returns -EOPNOTSUPP because a sibling
device was added to group->devices during bus_iommu_probe(), or when
blocking domain allocation, domain restoration, or domain attachment
fails), iommu_setup_default_domain() jumps to out_free_old and returns 0
with group->default_domain = dom and group->domain = NULL.
That breaks the invariant checked in __iommu_probe_device():
drivers/iommu/iommu.c:__iommu_probe_device() {
...
list_add_tail(&gdev->list, &group->devices);
WARN_ON(group->default_domain && !group->domain);
...
}
If the group contains another device (or one is added while
liveupdate_reserved is false) and the restored device is then removed,
__iommu_group_remove_restored_device() clears group->owner and
group->owner_cnt without attaching group->default_domain, and
iommu_deinit_device() keeps group->default_domain because group->devices
is non-empty. A driver can then bind to the remaining device via
iommu_device_use_default_domain() while group->domain is still NULL and
default_domain was never attached in hardware, and rescanning the removed
device hits WARN_ON(group->default_domain && !group->domain) in
__iommu_probe_device() without setting up a domain.
Also, if a non-restored sibling device in the same group is probed first
with group_list == NULL (setting group->default_domain and group->domain)
and the restored device is probed second, __iommu_probe_device() takes the
if (group->domain) branch and attaches group->default_domain to the
restored device instead of restoring its preserved domain.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123044949.3610702-1-skhawaja@google.com?part=13
next prev parent reply other threads:[~2026-11-23 5:18 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 4:49 [PATCH v8 00/18] iommu: Add live update state preservation Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 01/18] liveupdate: Export symbols needed by modules Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 02/18] iommu: Implement IOMMU Live update FLB callbacks Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 03/18] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 04/18] iommupt: Implement preserve/unpreserve/restore callbacks Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 05/18] iommu: Implement IOMMU domain preservation Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 06/18] iommu: Implement device and IOMMU HW preservation Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 07/18] iommu/vt-d: Implement device and iommu preserve/unpreserve ops Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 08/18] iommu/vt-d: Clear unpreserved context entries during shutdown Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 09/18] iommu: Add APIs to get iommu and device restored state Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 10/18] iommu/vt-d: Combine disable_dmar_iommu() and free_dmar_iommu() Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 11/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 12/18] iommu/vt-d: Preserve PASID table of preserved device Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 13/18] iommu: Restore and reattach preserved domains to devices Samiullah Khawaja
2026-11-23 5:18 ` sashiko-bot [this message]
2026-11-23 4:49 ` [PATCH v8 14/18] iommu/vt-d: Handle reattach of the restored domain Samiullah Khawaja
2026-11-23 5:03 ` sashiko-bot
2026-11-23 4:49 ` [PATCH v8 15/18] iommufd: Implement ioctl to mark HWPT for preservation Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 16/18] iommufd: Persist iommu hardware pagetables for live update Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 17/18] iommufd: Add APIs to preserve/unpreserve a vfio cdev Samiullah Khawaja
2026-11-23 4:49 ` [PATCH v8 18/18] iommufd/selftest: Add test to verify iommufd preservation Samiullah Khawaja
2026-11-23 5:11 ` sashiko-bot
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=sashiko-outbox-228711@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=skhawaja@google.com \
/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