Skip to contentSkip to conversation

iommu/arm-smmu-v3: Free MSIs in case of ENOMEM

PATCH Applied Aleksandr Aprelkov 4 participants 6 messages Apr 3, 2024 – Apr 19, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iommu/arm-smmu-v3: Free MSIs in case of ENOMEM

If devm_add_action() returns ENOMEM, then MSIs allocated but not freed on teardown.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 166bdbd23161 ("iommu/arm-smmu: Add support for MSI on SMMUv3")

---
v2: Use appropriate function for registration failure as Jonathan Cameron <[email protected]> suggested.

 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -1
index 41f93c3ab160..8800af041e5f 100644
@@ -3402,7 +3402,9 @@ static void arm_smmu_setup_msis(struct arm_smmu_device *smmu)
 	smmu->priq.q.irq = msi_get_virq(dev, PRIQ_MSI_INDEX);
 
 	/* Add callback to free MSIs on teardown */
-	devm_add_action(dev, arm_smmu_free_msis, dev);
+	ret = devm_add_action_or_reset(dev, arm_smmu_free_msis, dev);
+	if (ret)
+		dev_warn(dev, "failed to add free MSIs callback - falling back to wired irqs\n");
 }
 
 static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)
Signature · 1 line
2.34.1
#1 lore raw

Hi Aleksandr,

Quoted 19 lines · Aleksandr Aprelkov

If devm_add_action() returns ENOMEM, then MSIs allocated but not freed on teardown.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 166bdbd23161 ("iommu/arm-smmu: Add support for MSI on SMMUv3")

---
v2: Use appropriate function for registration failure as Jonathan Cameron <[email protected]> suggested.

 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
index 41f93c3ab160..8800af041e5f 100644

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_setup_msis(struct arm_smmu_device *smmu) · line 3407

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -1
@@ -3402,7 +3402,9 @@ static void arm_smmu_setup_msis(struct arm_smmu_device *smmu)
 	smmu->priq.q.irq = msi_get_virq(dev, PRIQ_MSI_INDEX);
 
 	/* Add callback to free MSIs on teardown */
-	devm_add_action(dev, arm_smmu_free_msis, dev);
+	ret = devm_add_action_or_reset(dev, arm_smmu_free_msis, dev);
+	if (ret)
+		dev_warn(dev, "failed to add free MSIs callback - falling back to wired irqs\n");

I am not sure that is the right fix, as allowing the driver to probe without MSIs, seems worse than leaking MSI memory.

IMHO, we can just add something like:

    dev_err(smmu->dev, “Can’t allocate devm action, MSIs are never freed! !\n”) ;

Also, we can’t unconditionally fallback to wired irqs if MSI exists, according to the user manual:
An implementation must support one of, or optionally both of, wired interrupts and MSIs
...
The discovery of support for wired interrupts is IMPLEMENTATION DEFINED.

We can add some logic, to check dt/acpi irqs and to choose to fallback or not based on that, but, if we get -ENOMEM, (especially early at probe) something really went wrong, so I am not sure it’s worth the complexity.

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
 }
 
 static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)
Signature · 1 line
2.34.1

Thanks,
Mostafa

reviewer
#2 lore raw
Quoted 33 lines · Mostafa Saleh

Hi Aleksandr,

Quoted 19 lines · Aleksandr Aprelkov

If devm_add_action() returns ENOMEM, then MSIs allocated but not freed on teardown.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 166bdbd23161 ("iommu/arm-smmu: Add support for MSI on SMMUv3")

---
v2: Use appropriate function for registration failure as Jonathan Cameron <[email protected]> suggested.

  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
index 41f93c3ab160..8800af041e5f 100644

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_setup_msis(struct arm_smmu_device *smmu) · line 3407

Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -1
@@ -3402,7 +3402,9 @@ static void arm_smmu_setup_msis(struct arm_smmu_device *smmu)
  	smmu->priq.q.irq = msi_get_virq(dev, PRIQ_MSI_INDEX);
 
  	/* Add callback to free MSIs on teardown */
-	devm_add_action(dev, arm_smmu_free_msis, dev);
+	ret = devm_add_action_or_reset(dev, arm_smmu_free_msis, dev);
+	if (ret)
+		dev_warn(dev, "failed to add free MSIs callback - falling back to wired irqs\n");

I am not sure that is the right fix, as allowing the driver to probe without MSIs, seems worse than leaking MSI memory.

Quoted 2 lines

IMHO, we can just add something like:

     dev_err(smmu->dev, “Can’t allocate devm action, MSIs are never freed! !\n”) ;

Honestly I don't think this matters. If we ever really did fail to allocate 16 bytes, SLUB would already be screaming and spewing stacktraces, and the system is dead already.

Quoted 11 lines

Also, we can’t unconditionally fallback to wired irqs if MSI exists, according to the user manual:
An implementation must support one of, or optionally both of, wired interrupts and MSIs
...
The discovery of support for wired interrupts is IMPLEMENTATION DEFINED.

We can add some logic, to check dt/acpi irqs and to choose to fallback or not based on that, but, if we get -ENOMEM, (especially early at probe) something really went wrong, so I am not sure it’s worth the complexity.

That logic already exists in arm_smmu_setup_unique_irqs() - the messages here are in the sense of "we're giving up on MSIs and falling back to trying whatever wired IRQs we may or may not have." The critical point is that we're not using MSIs for some potentially actionable reason, i.e. if the user does expect the system to be MSI-capable, then it could be an indication of perhaps a wrong or missing msi-parent, for which they may pursue a firmware fix. In other cases it's normal and expected not to use MSIs though (e.g. the system just doesn't have an ITS), so we don't want to be *too* noisy about it.

Thanks,
Robin.

Quoted 9 lines
Quoted 7 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
  }
 
  static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)
Signature · 1 line
2.34.1

Thanks,
Mostafa

#3 lore raw

Hi Robin,

Quoted 52 lines · Robin Murphy
Quoted 32 lines · Mostafa Saleh

Hi Aleksandr,

Quoted 19 lines · Aleksandr Aprelkov

If devm_add_action() returns ENOMEM, then MSIs allocated but not freed on teardown.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 166bdbd23161 ("iommu/arm-smmu: Add support for MSI on SMMUv3")

---
v2: Use appropriate function for registration failure as Jonathan Cameron <[email protected]> suggested.

  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
index 41f93c3ab160..8800af041e5f 100644

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_setup_msis(struct arm_smmu_device *smmu) · line 3406

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -1
@@ -3402,7 +3402,9 @@ static void arm_smmu_setup_msis(struct arm_smmu_device *smmu)
  	smmu->priq.q.irq = msi_get_virq(dev, PRIQ_MSI_INDEX);
  	/* Add callback to free MSIs on teardown */
-	devm_add_action(dev, arm_smmu_free_msis, dev);
+	ret = devm_add_action_or_reset(dev, arm_smmu_free_msis, dev);
+	if (ret)
+		dev_warn(dev, "failed to add free MSIs callback - falling back to wired irqs\n");

I am not sure that is the right fix, as allowing the driver to probe without MSIs, seems worse than leaking MSI memory.

Quoted 2 lines

IMHO, we can just add something like:

     dev_err(smmu->dev, “Can’t allocate devm action, MSIs are never freed! !\n”) ;

Honestly I don't think this matters. If we ever really did fail to allocate 16 bytes, SLUB would already be screaming and spewing stacktraces, and the system is dead already.

Quoted 11 lines

Also, we can’t unconditionally fallback to wired irqs if MSI exists, according to the user manual:
An implementation must support one of, or optionally both of, wired interrupts and MSIs
...
The discovery of support for wired interrupts is IMPLEMENTATION DEFINED.

We can add some logic, to check dt/acpi irqs and to choose to fallback or not based on that, but, if we get -ENOMEM, (especially early at probe) something really went wrong, so I am not sure it’s worth the complexity.

Quoted 9 lines

That logic already exists in arm_smmu_setup_unique_irqs() - the messages here are in the sense of "we're giving up on MSIs and falling back to trying whatever wired IRQs we may or may not have." The critical point is that we're not using MSIs for some potentially actionable reason, i.e. if the user does expect the system to be MSI-capable, then it could be an indication of perhaps a wrong or missing msi-parent, for which they may pursue a firmware fix. In other cases it's normal and expected not to use MSIs though (e.g. the system just doesn't have an ITS), so we don't want to be *too* noisy about it.

The case I am worried about in this patch, is for systems with MSIs only.
With this patch, that means, we fallback to wired irqs which don't exist, so the driver will probe with no interrupts at all, which in my opinion worse than leaking the memory.

Thanks,
Mostafa

Quoted 12 lines

Thanks,
Robin.

Quoted 8 lines
Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
  }
  static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)
Signature · 1 line
2.34.1

Thanks,
Mostafa

reviewer
#4 lore raw
Quoted 65 lines · Mostafa Saleh

Hi Robin,

Quoted 52 lines · Robin Murphy
Quoted 32 lines · Mostafa Saleh

Hi Aleksandr,

Quoted 19 lines · Aleksandr Aprelkov

If devm_add_action() returns ENOMEM, then MSIs allocated but not freed on teardown.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 166bdbd23161 ("iommu/arm-smmu: Add support for MSI on SMMUv3")

---
v2: Use appropriate function for registration failure as Jonathan Cameron <[email protected]> suggested.

   drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 4 +++-
   1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
index 41f93c3ab160..8800af041e5f 100644

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_setup_msis(struct arm_smmu_device *smmu) · line 3406

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -1
@@ -3402,7 +3402,9 @@ static void arm_smmu_setup_msis(struct arm_smmu_device *smmu)
   	smmu->priq.q.irq = msi_get_virq(dev, PRIQ_MSI_INDEX);
   	/* Add callback to free MSIs on teardown */
-	devm_add_action(dev, arm_smmu_free_msis, dev);
+	ret = devm_add_action_or_reset(dev, arm_smmu_free_msis, dev);
+	if (ret)
+		dev_warn(dev, "failed to add free MSIs callback - falling back to wired irqs\n");

I am not sure that is the right fix, as allowing the driver to probe without MSIs, seems worse than leaking MSI memory.

Quoted 2 lines

IMHO, we can just add something like:

      dev_err(smmu->dev, “Can’t allocate devm action, MSIs are never freed! !\n”) ;

Honestly I don't think this matters. If we ever really did fail to allocate 16 bytes, SLUB would already be screaming and spewing stacktraces, and the system is dead already.

Quoted 11 lines

Also, we can’t unconditionally fallback to wired irqs if MSI exists, according to the user manual:
An implementation must support one of, or optionally both of, wired interrupts and MSIs
...
The discovery of support for wired interrupts is IMPLEMENTATION DEFINED.

We can add some logic, to check dt/acpi irqs and to choose to fallback or not based on that, but, if we get -ENOMEM, (especially early at probe) something really went wrong, so I am not sure it’s worth the complexity.

Quoted 9 lines

That logic already exists in arm_smmu_setup_unique_irqs() - the messages here are in the sense of "we're giving up on MSIs and falling back to trying whatever wired IRQs we may or may not have." The critical point is that we're not using MSIs for some potentially actionable reason, i.e. if the user does expect the system to be MSI-capable, then it could be an indication of perhaps a wrong or missing msi-parent, for which they may pursue a firmware fix. In other cases it's normal and expected not to use MSIs though (e.g. the system just doesn't have an ITS), so we don't want to be *too* noisy about it.

Quoted 5 lines

The case I am worried about in this patch, is for systems with MSIs only.
With this patch, that means, we fallback to wired irqs which don't exist, so the driver will probe with no interrupts at all, which in my opinion worse than leaking the memory.

True, the logic looks a bit off at first glance - I was halfway through writing a reply to that effect - but then if you look past the reality that this is all academic since it's never really going to happen anyway, if we *did* fail to allocate 16 bytes here, there's an incredibly high chance that immediately proceeding into iommu_device_sysfs_add() is also going to result in another (larger) allocation failure which ends up aborting the whole probe anyway. Plus the chance of subsequently being able to allocate any domains/pagetables/etc. for any meaningful IOMMU usage would seem slim.

Honestly I'd be inclined to do nothing more than add the _or_reset to shut the static checkers up, and not waste code and data on a useless message for a theoretical condition at all.

Cheers,
Robin.

maintainer Applied
#5 lore raw
In reply to Aleksandr Aprelkov
Quoted 6 lines · Aleksandr Aprelkov

If devm_add_action() returns ENOMEM, then MSIs allocated but not freed on teardown.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Applied to will (for-joerg/arm-smmu/updates), thanks!

[1/1] iommu/arm-smmu-v3: Free MSIs in case of ENOMEM

      https://git.kernel.org/will/c/80fea979dd9d

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev