Linux IOMMU Development
 help / color / mirror / Atom feed
From: Pranjal Shrivastava <praan@google.com>
To: Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
	 Robin Murphy <robin.murphy@arm.com>,
	Jason Gunthorpe <jgg@ziepe.ca>
Cc: Nicolin Chen <nicolinc@nvidia.com>,
	Mostafa Saleh <smostafa@google.com>,
	 Daniel Mentz <danielmentz@google.com>,
	iommu@lists.linux.dev,  Pranjal Shrivastava <praan@google.com>
Subject: [RFC PATCH v2 08/10] iommu/arm-smmu-v3: Avoid suspend when user owns DMA
Date: Fri, 18 Apr 2025 23:34:07 +0000	[thread overview]
Message-ID: <20250418233409.3926715-9-praan@google.com> (raw)
In-Reply-To: <20250418233409.3926715-1-praan@google.com>

Suspending the smmu is unsafe if any attached devices belong
to iommu groups claimed by user-space (via VFIO etc.), as a DMA may
be ongoing or initiated at any time. The smmu must remain powered ON to
handle potential DMA requests originating from the user-space context.

Introduce a helper `arm_smmu_suspend_is_safe()` using the previously
added `insecure_attachments` list. If the check fails (list is not empty),
defer the suspend ensuring the SMMU remains active while it's potentially
needed by user-space for DMA.

Signed-off-by: Pranjal Shrivastava <praan@google.com>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 18 ++++++++++++++++++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h |  2 +-
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
index 2b51665d5ca6..7099c0cbf5fe 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -5040,11 +5040,29 @@ static void arm_smmu_device_shutdown(struct platform_device *pdev)
 	arm_smmu_device_disable(smmu);
 }
 
+static bool arm_smmu_suspend_is_safe(struct arm_smmu_device *smmu)
+{
+	unsigned long flags;
+	bool is_empty;
+
+	spin_lock_irqsave(&smmu->attach_lock, flags);
+	is_empty = list_empty(&smmu->insecure_attachments);
+	spin_unlock_irqrestore(&smmu->attach_lock, flags);
+
+	return is_empty;
+}
+
 static int __maybe_unused arm_smmu_runtime_suspend(struct device *dev)
 {
 	struct arm_smmu_device *smmu = dev_get_drvdata(dev);
 	int ret;
 
+	/* Refuse to suspend if user-space controls DMA */
+	if (!arm_smmu_suspend_is_safe(smmu)) {
+		dev_err(smmu->dev, "Suspend deferred due to unsafe DMA owners\n");
+		return -EAGAIN;
+	}
+
 	/*
 	 * Since suspend is invoked when all clients have been suspended,
 	 * we don't expect more cmds or events to be added to the queues.
diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
index 5ab60ae7ff91..28fddc07ab49 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
@@ -796,7 +796,7 @@ struct arm_smmu_device {
 	struct rb_root			streams;
 	struct mutex			streams_mutex;
 
-	/* Insecure Attach handles */
+	/* Insecure attach handles */
 	struct list_head		insecure_attachments;
 	/* Lock for the list */
 	spinlock_t			attach_lock;
-- 
2.49.0.805.g082f7c87e0-goog


  parent reply	other threads:[~2025-04-18 23:35 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-18 23:33 [RFC PATCH v2 00/10] iommu/arm-smmu-v3: Implement Runtime/System Sleep ops Pranjal Shrivastava
2025-04-18 23:34 ` [RFC PATCH v2 01/10] iommu/arm-smmu-v3: Refactor arm_smmu_setup_irqs Pranjal Shrivastava
2025-05-02 19:14   ` Nicolin Chen
2025-04-18 23:34 ` [RFC PATCH v2 02/10] iommu/arm-smmu-v3: Add a helper to drain all queues Pranjal Shrivastava
2025-05-02 19:32   ` Nicolin Chen
2025-05-05 15:14     ` Pranjal Shrivastava
2025-05-04 20:28   ` Daniel Mentz
2025-05-05 15:10     ` Pranjal Shrivastava
2025-04-18 23:34 ` [RFC PATCH v2 03/10] iommu/tegra241-cmdqv: Add a helper to drain VCMDQs Pranjal Shrivastava
2025-04-23  6:34   ` Nicolin Chen
2025-04-18 23:34 ` [RFC PATCH v2 04/10] iommu/arm-smmu-v3: Cache and restore MSI config Pranjal Shrivastava
2025-05-02 19:43   ` Nicolin Chen
2025-05-05 15:16     ` Pranjal Shrivastava
2025-04-18 23:34 ` [RFC PATCH v2 05/10] iommu/arm-smmu-v3: Implement pm_runtime & system sleep ops Pranjal Shrivastava
2025-05-04 20:29   ` Daniel Mentz
2025-05-05 15:22     ` Pranjal Shrivastava
2025-04-18 23:34 ` [RFC PATCH v2 06/10] iommu: Add a helper to check for user ownership Pranjal Shrivastava
2025-04-18 23:34 ` [RFC PATCH v2 07/10] iommu/arm-smmu-v3: Track masters with user-owned groups Pranjal Shrivastava
2025-04-18 23:34 ` Pranjal Shrivastava [this message]
2025-05-04 20:28   ` [RFC PATCH v2 08/10] iommu/arm-smmu-v3: Avoid suspend when user owns DMA Daniel Mentz
2025-05-05 15:22     ` Pranjal Shrivastava
2025-04-18 23:34 ` [RFC PATCH v2 09/10] iommu/arm-smmu-v3: Enable pm_runtime and setup devlinks Pranjal Shrivastava
2025-04-18 23:34 ` [RFC PATCH v2 10/10] iommu/arm-smmu-v3: Invoke pm_runtime before hw access Pranjal Shrivastava
2025-05-04 20:29   ` Daniel Mentz
2025-05-05 16:10     ` Pranjal Shrivastava

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=20250418233409.3926715-9-praan@google.com \
    --to=praan@google.com \
    --cc=danielmentz@google.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=joro@8bytes.org \
    --cc=nicolinc@nvidia.com \
    --cc=robin.murphy@arm.com \
    --cc=smostafa@google.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