Skip to contentSkip to conversation

iommu: Fix nested pci_dev_reset_iommu_prepare/done()

PATCH Superseded Nicolin Chen 3 participants 8 messages Mar 19 – Apr 1 IOMMU

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iommu: Fix nested pci_dev_reset_iommu_prepare/done()

Shuai found that cxl_reset_bus_function() calls pci_reset_bus_function() internally while both are calling pci_dev_reset_iommu_prepare/done().

As pci_dev_reset_iommu_prepare() doesn't support re-entry, the inner call will trigger a WARN_ON and return -EBUSY, resulting in failing the entire device reset.

On the other hand, removing the outer calls in the PCI callers is unsafe. As pointed out by Kevin, device-specific quirks like reset_hinic_vf_dev() execute custom firmware waits after their inner pcie_flr() completes. If the IOMMU protection relies solely on the inner reset, the IOMMU will be unblocked prematurely while the device is still resetting.

Instead, fix this by making pci_dev_reset_iommu_prepare/done() reentrant.

Introduce a 'reset_cnt' in struct iommu_group. Safely increment/decrement the reference counter in the nested calls, ensuring the IOMMU domains are only restored when the outermost reset finally completes.

Fixes c279e83953d9 ("iommu: Introduce pci_dev_reset_iommu_prepare/done()")

---
Changelog
v2:
* Fix in the helpers by allowing re-entry
v1:
https://lore.kernel.org/all/[email protected]/

 drivers/iommu/iommu.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/iommu/iommu.c +12 -3
index 35db51780954..16155097b27c 100644
@@ -68,6 +68,7 @@ struct iommu_group {
 	struct iommu_domain *resetting_domain;
 	struct iommu_domain *domain;
 	struct list_head entry;
+	unsigned int reset_cnt;
 	unsigned int owner_cnt;
 	void *owner;
 };
@@ -3961,9 +3962,10 @@ int pci_dev_reset_iommu_prepare(struct pci_dev *pdev)
 
 	guard(mutex)(&group->mutex);
 
-	/* Re-entry is not allowed */
-	if (WARN_ON(group->resetting_domain))
-		return -EBUSY;
+	if (group->resetting_domain) {
+		group->reset_cnt++;
+		return 0;
+	}
 
 	ret = __iommu_group_alloc_blocking_domain(group);
 	if (ret)
@@ -3988,6 +3990,7 @@ int pci_dev_reset_iommu_prepare(struct pci_dev *pdev)
 				       pasid_array_entry_to_domain(entry));
 
 	group->resetting_domain = group->blocking_domain;
+	group->reset_cnt = 1;
 	return ret;
 }
 EXPORT_SYMBOL_GPL(pci_dev_reset_iommu_prepare);
@@ -4021,6 +4024,12 @@ void pci_dev_reset_iommu_done(struct pci_dev *pdev)
 	if (!group->resetting_domain)
 		return;
 
+	/* Unbalanced done() calls that would underflow the counter */
+	if (WARN_ON(group->reset_cnt == 0))
+		return;
+	if (--group->reset_cnt > 0)
+		return;
+
 	/* pci_dev_reset_iommu_prepare() was not successfully called */
 	if (WARN_ON(!group->blocking_domain))
 		return;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 40 lines · Nicolin Chen

Shuai found that cxl_reset_bus_function() calls pci_reset_bus_function() internally while both are calling pci_dev_reset_iommu_prepare/done().

As pci_dev_reset_iommu_prepare() doesn't support re-entry, the inner call will trigger a WARN_ON and return -EBUSY, resulting in failing the entire device reset.

On the other hand, removing the outer calls in the PCI callers is unsafe. As pointed out by Kevin, device-specific quirks like reset_hinic_vf_dev() execute custom firmware waits after their inner pcie_flr() completes. If the IOMMU protection relies solely on the inner reset, the IOMMU will be unblocked prematurely while the device is still resetting.

Instead, fix this by making pci_dev_reset_iommu_prepare/done() reentrant.

Introduce a 'reset_cnt' in struct iommu_group. Safely increment/decrement the reference counter in the nested calls, ensuring the IOMMU domains are only restored when the outermost reset finally completes.

Fixes c279e83953d9 ("iommu: Introduce pci_dev_reset_iommu_prepare/done()")

---
Changelog
v2:
* Fix in the helpers by allowing re-entry
v1:
https://lore.kernel.org/all/[email protected]/

  drivers/iommu/iommu.c | 15 ++++++++++++---
  1 file changed, 12 insertions(+), 3 deletions(-)
drivers/iommu/iommu.c +0 -0
index 35db51780954..16155097b27c 100644

drivers/iommu/iommu.c struct iommu_group { · line 71

Quoted 4 lines
drivers/iommu/iommu.c +1 -0
@@ -68,6 +68,7 @@ struct iommu_group {
  	struct iommu_domain *resetting_domain;
  	struct iommu_domain *domain;
  	struct list_head entry;
+	unsigned int reset_cnt;

Nit: consider renaming reset_cnt to reset_depth or reset_nesting to better convey that this tracks nesting level, not a count of total resets.

Quoted 6 lines
drivers/iommu/iommu.c +0 -0
  	unsigned int owner_cnt;
  	void *owner;
  };
@@ -3961,9 +3962,10 @@ int pci_dev_reset_iommu_prepare(struct pci_dev *pdev)
 
  	guard(mutex)(&group->mutex);

drivers/iommu/iommu.c int pci_dev_reset_iommu_prepare(struct pci_dev *pdev) · line 3970

Quoted 10 lines
drivers/iommu/iommu.c +4 -3
 
-	/* Re-entry is not allowed */
-	if (WARN_ON(group->resetting_domain))
-		return -EBUSY;
+	if (group->resetting_domain) {
+		group->reset_cnt++;
+		return 0;
+	}
 
  	ret = __iommu_group_alloc_blocking_domain(group);

pci_dev_reset_iommu_prepare/done() have NO singleton group check. They operate on the specific pdev passed in, but use group-wide state (resetting_domain, reset_cnt) to track the reset lifecycle.

Interestingly, the broken_worker in patch 3 of the ATC timeout series DOES have an explicit singleton check:

  if (list_is_singular(&group->devices)) {
      /* Note: only support group with a single device */

This reveals an implicit assumption: the entire prepare/done mechanism works correctly only for singleton groups. For multi-device groups:

- prepare() only detaches the specific pdev, leaving other devices in the group still attached to the original domain
- The group-wide resetting_domain/reset_cnt state can be corrupted by concurrent resets on different devices (as discussed above)

If prepare/done is truly meant only for singleton groups, it should enforce this explicitly:

if (!list_is_singular(&group->devices))

      return -EOPNOTSUPP;

If it's meant to support multi-device groups, then the per-device vs group-wide state mismatch needs to be resolved — either by making the state per-device, or by detaching/restoring all devices in the group together.

Thanks.
Shuai

#2 lore raw
Quoted 26 lines · Shuai Xue

drivers/iommu/iommu.c int pci_dev_reset_iommu_prepare(struct pci_dev *pdev) · line 3967

Quoted 10 lines · Nicolin Chen
drivers/iommu/iommu.c +4 -3
@@ -3961,9 +3962,10 @@ int pci_dev_reset_iommu_prepare(struct pci_dev *pdev)
  	guard(mutex)(&group->mutex);
-	/* Re-entry is not allowed */
-	if (WARN_ON(group->resetting_domain))
-		return -EBUSY;
+	if (group->resetting_domain) {
+		group->reset_cnt++;
+		return 0;
+	}
  	ret = __iommu_group_alloc_blocking_domain(group);

pci_dev_reset_iommu_prepare/done() have NO singleton group check. They operate on the specific pdev passed in, but use group-wide state (resetting_domain, reset_cnt) to track the reset lifecycle.

Interestingly, the broken_worker in patch 3 of the ATC timeout series DOES have an explicit singleton check:

  if (list_is_singular(&group->devices)) {
      /* Note: only support group with a single device */

This reveals an implicit assumption: the entire prepare/done mechanism works correctly only for singleton groups. For multi-device groups:

Quoted 5 lines

- prepare() only detaches the specific pdev, leaving other devices in the group still attached to the original domain
- The group-wide resetting_domain/reset_cnt state can be corrupted by concurrent resets on different devices (as discussed above)

That's a phenomenal catch!

I think we shall have a reset_ndevs in the group and a reset_depth in the gdev.

Quoted 5 lines

If prepare/done is truly meant only for singleton groups, it should enforce this explicitly:

if (!list_is_singular(&group->devices))

      return -EOPNOTSUPP;

-EOPNOTSUPP will fail ail the PCI reset caller functions, though we could change them to ignore -EOPNOTSUPP or we could probably do:

	if (!list_is_singular(&group->devices))
		return 0;

But I feel this would be too heavy for a bug fix.

Quoted 4 lines

If it's meant to support multi-device groups, then the per-device vs group-wide state mismatch needs to be resolved — either by making the state per-device, or by detaching/restoring all devices in the group together.

Per-gdev should work, IMHO, at least for this patch.

For iommu_report_device_broken(), I am thinking we could still try a per-gdev WQ, by changing the mutex-protected gdev list to an RCU one.

Thanks
Nicolin

#3 lore raw
Quoted 33 lines

From: Nicolin Chen <[email protected]>
Sent: Friday, March 20, 2026 5:35 AM

Quoted 2 lines · Shuai Xue

Review int pci_dev_reset_iommu_prepare(struct

Quoted 1 line · Nicolin Chen
(quoted diff) +0 -0
@@ -3961,9 +3962,10 @@ int pci_dev_reset_iommu_prepare(struct

pci_dev *pdev)

Quoted 26 lines

drivers/iommu/iommu.c int pci_dev_reset_iommu_prepare(struct pci_dev *pdev) · line 3970

Quoted 9 lines
drivers/iommu/iommu.c +4 -3
  	guard(mutex)(&group->mutex);
-	/* Re-entry is not allowed */
-	if (WARN_ON(group->resetting_domain))
-		return -EBUSY;
+	if (group->resetting_domain) {
+		group->reset_cnt++;
+		return 0;
+	}
  	ret = __iommu_group_alloc_blocking_domain(group);

pci_dev_reset_iommu_prepare/done() have NO singleton group check. They operate on the specific pdev passed in, but use group-wide state (resetting_domain, reset_cnt) to track the reset lifecycle.

Interestingly, the broken_worker in patch 3 of the ATC timeout series DOES have an explicit singleton check:

  if (list_is_singular(&group->devices)) {
      /* Note: only support group with a single device */

This reveals an implicit assumption: the entire prepare/done mechanism works correctly only for singleton groups. For multi-device groups:

- prepare() only detaches the specific pdev, leaving other devices in the group still attached to the original domain

no, attach is per-group. all devices are changed together.

Quoted 9 lines
Quoted 3 lines

- The group-wide resetting_domain/reset_cnt state can be corrupted by concurrent resets on different devices (as discussed above)

That's a phenomenal catch!

I think we shall have a reset_ndevs in the group and a reset_depth in the gdev.

I didn't get how the state might be corrupted.

If there are two devices in a group both being reset, you only want to switch back to the original domain after both devices have finished reset.

so a reset_cnt is sufficient to cover both scenarios:

- nested prepare/done in a single device resetting path
- concurrent prepare/done in multiple devices resetting paths

v4 is way too complex. Probably it's required in the following ATS timeout series (which I haven't thought about carefully). but for the fix here seems a simple logic like v2 does is ok?

#4 lore raw
Quoted 27 lines · Kevin
Quoted 27 lines

From: Nicolin Chen <[email protected]>
Sent: Friday, March 20, 2026 5:35 AM

Quoted 2 lines · Shuai Xue

Review int pci_dev_reset_iommu_prepare(struct

Quoted 1 line · Nicolin Chen
(quoted diff) +0 -0
@@ -3961,9 +3962,10 @@ int pci_dev_reset_iommu_prepare(struct

pci_dev *pdev)

Quoted 20 lines

drivers/iommu/iommu.c int pci_dev_reset_iommu_prepare(struct pci_dev *pdev) · line 3970

Quoted 9 lines
drivers/iommu/iommu.c +4 -3
  	guard(mutex)(&group->mutex);
-	/* Re-entry is not allowed */
-	if (WARN_ON(group->resetting_domain))
-		return -EBUSY;
+	if (group->resetting_domain) {
+		group->reset_cnt++;
+		return 0;
+	}
  	ret = __iommu_group_alloc_blocking_domain(group);

pci_dev_reset_iommu_prepare/done() have NO singleton group check. They operate on the specific pdev passed in, but use group-wide state (resetting_domain, reset_cnt) to track the reset lifecycle.

Interestingly, the broken_worker in patch 3 of the ATC timeout series DOES have an explicit singleton check:

  if (list_is_singular(&group->devices)) {
      /* Note: only support group with a single device */
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

This reveals an implicit assumption: the entire prepare/done mechanism works correctly only for singleton groups. For multi-device groups:

- prepare() only detaches the specific pdev, leaving other devices in the group still attached to the original domain

no, attach is per-group. all devices are changed together.

I think prepare() should use the per-device ops:

    ops->attach_dev (via __iommu_attach_device)
    ops->set_dev_pasid

right?

And it is intended to limit to the resetting device only. Other devices in the group can still stay in the attached domain, but they cannot do new attachment (-EBUSY) because that's per-group.

Quoted 12 lines
Quoted 9 lines
Quoted 3 lines

- The group-wide resetting_domain/reset_cnt state can be corrupted by concurrent resets on different devices (as discussed above)

That's a phenomenal catch!

I think we shall have a reset_ndevs in the group and a reset_depth in the gdev.

I didn't get how the state might be corrupted.

Quoted 8 lines

If there are two devices in a group both being reset, you only want to switch back to the original domain after both devices have finished reset.

so a reset_cnt is sufficient to cover both scenarios:

- nested prepare/done in a single device resetting path
- concurrent prepare/done in multiple devices resetting paths

Since prepare() is per-device, the per-group reset_cnt wouldn't be sufficient if one device is resetting while the other isn't.

iommu_driver_get_domain_for_dev() for example would return the blocking_domain for both devices, but physically only the first device is attached to blocking_domain.

Quoted 3 lines

v4 is way too complex. Probably it's required in the following ATS timeout series (which I haven't thought about carefully). but for the fix here seems a simple logic like v2 does is ok?

I think v4 is the way to go, unless we fundamentally limit this API (and the followup ATS timeout series) to singleton groups.

FWIW, I have the ATS timeout series updated (waiting for some finalization of this base patch):
https://github.com/nicolinc/iommufd/commits/smmuv3_atc_timeout-v3/
The new iommu_report_device_broken() is changed to per-gdev.

Thanks
Nicolin

#5 lore raw
Quoted 38 lines

From: Nicolin Chen <[email protected]>
Sent: Saturday, March 28, 2026 5:09 AM

Quoted 34 lines · Kevin
Quoted 33 lines

From: Nicolin Chen <[email protected]>
Sent: Friday, March 20, 2026 5:35 AM

Quoted 2 lines · Shuai Xue

Review int pci_dev_reset_iommu_prepare(struct

Quoted 1 line · Nicolin Chen
(quoted diff) +0 -0
@@ -3961,9 +3962,10 @@ int pci_dev_reset_iommu_prepare(struct

pci_dev *pdev)

Quoted 26 lines

drivers/iommu/iommu.c int pci_dev_reset_iommu_prepare(struct pci_dev *pdev) · line 3970

Quoted 9 lines
drivers/iommu/iommu.c +4 -3
  	guard(mutex)(&group->mutex);
-	/* Re-entry is not allowed */
-	if (WARN_ON(group->resetting_domain))
-		return -EBUSY;
+	if (group->resetting_domain) {
+		group->reset_cnt++;
+		return 0;
+	}
  	ret = __iommu_group_alloc_blocking_domain(group);

pci_dev_reset_iommu_prepare/done() have NO singleton group check. They operate on the specific pdev passed in, but use group-wide state (resetting_domain, reset_cnt) to track the reset lifecycle.

Interestingly, the broken_worker in patch 3 of the ATC timeout series DOES have an explicit singleton check:

  if (list_is_singular(&group->devices)) {
      /* Note: only support group with a single device */

This reveals an implicit assumption: the entire prepare/done mechanism works correctly only for singleton groups. For multi-device groups:

- prepare() only detaches the specific pdev, leaving other devices in the group still attached to the original domain

Quoted 6 lines
Quoted 1 line

no, attach is per-group. all devices are changed together.

I think prepare() should use the per-device ops:

    ops->attach_dev (via __iommu_attach_device)
    ops->set_dev_pasid

right?

it's per-device op but the condition of calling it is per-group:

        if (group->domain != group->blocking_domain) {
                ret = __iommu_attach_device(group->blocking_domain, &pdev->dev,
                                            group->domain);
                if (ret)
                        return ret;
        }
Quoted 4 lines

And it is intended to limit to the resetting device only. Other devices in the group can still stay in the attached domain, but they cannot do new attachment (-EBUSY) because that's per-group.

how could other devices stay in the attached domain?

Quoted 22 lines
Quoted 13 lines
Quoted 10 lines
Quoted 4 lines

- The group-wide resetting_domain/reset_cnt state can be corrupted by concurrent resets on different devices (as discussed above)

That's a phenomenal catch!

I think we shall have a reset_ndevs in the group and a reset_depth in the gdev.

I didn't get how the state might be corrupted.

Quoted 8 lines

If there are two devices in a group both being reset, you only want to switch back to the original domain after both devices have finished reset.

so a reset_cnt is sufficient to cover both scenarios:

- nested prepare/done in a single device resetting path
- concurrent prepare/done in multiple devices resetting paths

Quoted 6 lines

Since prepare() is per-device, the per-group reset_cnt wouldn't be sufficient if one device is resetting while the other isn't.

iommu_driver_get_domain_for_dev() for example would return the blocking_domain for both devices, but physically only the first device is attached to blocking_domain.

what do you mean by 'physically'?

Quoted 7 lines
Quoted 4 lines

v4 is way too complex. Probably it's required in the following ATS timeout series (which I haven't thought about carefully). but for the fix here seems a simple logic like v2 does is ok?

I think v4 is the way to go, unless we fundamentally limit this API (and the followup ATS timeout series) to singleton groups.

currently we don't have any per-device resetting logic. As a rc fix I didn't see the point of making it complex.

Quoted 6 lines

FWIW, I have the ATS timeout series updated (waiting for some finalization of this base patch):
https://github.com/nicolinc/iommufd/commits/smmuv3_atc_timeout-v3/
The new iommu_report_device_broken() is changed to per-gdev.

#6 lore raw
Quoted 10 lines · Kevin
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

- prepare() only detaches the specific pdev, leaving other devices in the group still attached to the original domain

no, attach is per-group. all devices are changed together.

I think prepare() should use the per-device ops:

    ops->attach_dev (via __iommu_attach_device)
    ops->set_dev_pasid

right?

Quoted 8 lines

it's per-device op but the condition of calling it is per-group:

        if (group->domain != group->blocking_domain) {
                ret = __iommu_attach_device(group->blocking_domain, &pdev->dev,
                                            group->domain);
                if (ret)
                        return ret;
        }

If the group->domain is not blocking_domain, this device will move away from group->domain to blocking_domain, while other device (in the same group) are staying in group->domain. No?

Quoted 5 lines
Quoted 3 lines

And it is intended to limit to the resetting device only. Other devices in the group can still stay in the attached domain, but they cannot do new attachment (-EBUSY) because that's per-group.

how could other devices stay in the attached domain?

We call __iommu_attach_device only on one gdev, not looping the entire group, right?

Quoted 8 lines
Quoted 6 lines

Since prepare() is per-device, the per-group reset_cnt wouldn't be sufficient if one device is resetting while the other isn't.

iommu_driver_get_domain_for_dev() for example would return the blocking_domain for both devices, but physically only the first device is attached to blocking_domain.

what do you mean by 'physically'?

I mean SMMU driver will program STE to ask SMMU HW to reflect the change "physically". But it won't program the other STEs.

Nicolin

#7 lore raw
Quoted 24 lines

From: Nicolin Chen <[email protected]>
Sent: Tuesday, March 31, 2026 8:23 PM

Quoted 12 lines · Kevin
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

- prepare() only detaches the specific pdev, leaving other devices in the group still attached to the original domain

no, attach is per-group. all devices are changed together.

I think prepare() should use the per-device ops:

    ops->attach_dev (via __iommu_attach_device)
    ops->set_dev_pasid

right?

it's per-device op but the condition of calling it is per-group:

Quoted 7 lines
        if (group->domain != group->blocking_domain) {
                ret = __iommu_attach_device(group->blocking_domain, &pdev-
dev,
                                            group->domain);
                if (ret)
                        return ret;
        }
Quoted 3 lines

If the group->domain is not blocking_domain, this device will move away from group->domain to blocking_domain, while other device (in the same group) are staying in group->domain. No?

yeah, obviously I was wrong here. will review v4.