@@ -298,6 +379,129 @@ static void io_mm_detach_locked(struct iommu_bond *bond)
kfree(bond);
}
+static int iommu_signal_mm_exit(struct iommu_bond *bond)
+{
+ struct device *dev = bond->dev;
+ struct io_mm *io_mm = bond->io_mm;
+ struct iommu_sva_param *param = dev->iommu_param->sva_param;
+
+ /*
+ * We can't hold the device's param_lock. If we did and the device
+ * driver used a global lock around io_mm, we would risk getting the
+ * following deadlock:
+ *
+ * exit_mm() | Shutdown SVA
+ * mutex_lock(param->lock) | mutex_lock(glob lock)
+ * param->mm_exit() | sva_device_shutdown()
+ * mutex_lock(glob lock) | mutex_lock(param->lock)
+ *
+ * Fortunately unbind() waits for us to finish, and sva_device_shutdown
+ * requires that any bond is removed, so we can safely access mm_exit
+ * and drvdata without taking any lock.
+ */
+ if (!param || !param->mm_exit)
+ return 0;
+
+ return param->mm_exit(dev, io_mm->pasid, bond->drvdata);
+}
+
+/* Called when the mm exits. Can race with unbind(). */
+static void iommu_notifier_release(struct mmu_notifier *mn, struct mm_struct *mm)
+{
+ struct iommu_bond *bond, *next;
+ struct io_mm *io_mm = container_of(mn, struct io_mm, notifier);
+
+ /*
+ * If the mm is exiting then devices are still bound to the io_mm.
+ * A few things need to be done before it is safe to release:
+ *
+ * - As the mmu notifier doesn't hold any reference to the io_mm when
+ * calling ->release(), try to take a reference.
+ * - Tell the device driver to stop using this PASID.
+ * - Clear the PASID table and invalidate TLBs.
+ * - Drop all references to this io_mm by freeing the bonds.
+ */
+ spin_lock(&iommu_sva_lock);
+ if (!io_mm_get_locked(io_mm)) {
+ /* Someone's already taking care of it. */
+ spin_unlock(&iommu_sva_lock);
+ return;
+ }
+
+ list_for_each_entry_safe(bond, next, &io_mm->devices, mm_head) {
+ /*
+ * Release the lock to let the handler sleep. We need to be
+ * careful about concurrent modifications to the list and to the
+ * bond. Tell unbind() not to free the bond until we're done.
+ */
+ bond->mm_exit_active = true;
+ spin_unlock(&iommu_sva_lock);
+
+ if (iommu_signal_mm_exit(bond))
+ dev_WARN(bond->dev, "possible leak of PASID %u",
+ io_mm->pasid);
+
+ spin_lock(&iommu_sva_lock);
+ next = list_next_entry(bond, mm_head);
+
+ /* If someone is waiting, let them delete the bond now */
+ bond->mm_exit_active = false;
+ wake_up_all(&bond->mm_exit_wq);
+
+ /* Otherwise, do it ourselves */
+ io_mm_detach_locked(bond, false);
+ }
+ spin_unlock(&iommu_sva_lock);
+
+ /*
+ * We're now reasonably certain that no more fault is being handled for
+ * this io_mm, since we just flushed them all out of the fault queue.
+ * Release the last reference to free the io_mm.
+ */
+ io_mm_put(io_mm);
+}
+
+static void iommu_notifier_invalidate_range(struct mmu_notifier *mn,
+ struct mm_struct *mm,
+ unsigned long start,
+ unsigned long end)
+{
+ struct iommu_bond *bond;
+ struct io_mm *io_mm = container_of(mn, struct io_mm, notifier);
+
+ spin_lock(&iommu_sva_lock);
+ list_for_each_entry(bond, &io_mm->devices, mm_head) {
+ struct iommu_domain *domain = bond->domain;
+
+ domain->ops->mm_invalidate(domain, bond->dev, io_mm, start,
+ end - start);
+ }
+ spin_unlock(&iommu_sva_lock);
+}
+
+static int iommu_notifier_clear_flush_young(struct mmu_notifier *mn,
+ struct mm_struct *mm,
+ unsigned long start,
+ unsigned long end)
+{
+ iommu_notifier_invalidate_range(mn, mm, start, end);
+ return 0;
+}
+
+static void iommu_notifier_change_pte(struct mmu_notifier *mn,
+ struct mm_struct *mm,
+ unsigned long address, pte_t pte)
+{
+ iommu_notifier_invalidate_range(mn, mm, address, address + PAGE_SIZE);
+}
+
+static struct mmu_notifier_ops iommu_mmu_notifier = {
+ .release = iommu_notifier_release,
+ .clear_flush_young = iommu_notifier_clear_flush_young,
+ .change_pte = iommu_notifier_change_pte,
+ .invalidate_range = iommu_notifier_invalidate_range,
+};
+
/**
* iommu_sva_device_init() - Initialize Shared Virtual Addressing for a device
* @dev: the device@@ -320,6 +524,12 @@ static void io_mm_detach_locked(struct iommu_bond *bond)
* The handler gets an opaque pointer corresponding to the drvdata passed as
* argument of bind().
*
+ * The @mm_exit handler is allowed to sleep. Be careful about the locks taken in
+ * @mm_exit, because they might lead to deadlocks if they are also held when
+ * dropping references to the mm. Consider the following call chain:
+ * mutex_lock(A); mmput(mm) -> exit_mm() -> @mm_exit() -> mutex_lock(A)
+ * Using mmput_async() prevents this scenario.
+ *
* The device should not be performing any DMA while this function is running,
* otherwise the behavior is undefined.
*
@@ -484,15 +694,16 @@ int __iommu_sva_unbind_device(struct device *dev, int pasid)
if (!param || WARN_ON(!domain))
return -EINVAL;
- spin_lock(&iommu_sva_lock);
+ /* spin_lock_irq matches the one in wait_event_lock_irq */
+ spin_lock_irq(&iommu_sva_lock);
list_for_each_entry(bond, ¶m->mm_list, dev_head) {
if (bond->io_mm->pasid == pasid) {
- io_mm_detach_locked(bond);
+ io_mm_detach_locked(bond, true);
ret = 0;
break;
}
}
- spin_unlock(&iommu_sva_lock);
+ spin_unlock_irq(&iommu_sva_lock);
return ret;
}@@ -503,18 +714,25 @@ EXPORT_SYMBOL_GPL(__iommu_sva_unbind_device);
* @dev: the device
*
* When detaching @device from a domain, IOMMU drivers should use this helper.
+ * This function may sleep while waiting for bonds to be released.
*/
void __iommu_sva_unbind_dev_all(struct device *dev)
{
struct iommu_sva_param *param;
struct iommu_bond *bond, *next;
+ /*
+ * io_mm_detach_locked might wait, so we shouldn't call it with the dev
+ * param lock held. It's fine to read sva_param outside the lock because
+ * it can only be freed by iommu_sva_device_shutdown when there are no
+ * more bonds in the list.
+ */
param = dev->iommu_param->sva_param;
if (param) {
- spin_lock(&iommu_sva_lock);
+ spin_lock_irq(&iommu_sva_lock);
list_for_each_entry_safe(bond, next, ¶m->mm_list, dev_head)
- io_mm_detach_locked(bond);
- spin_unlock(&iommu_sva_lock);
+ io_mm_detach_locked(bond, true);
+ spin_unlock_irq(&iommu_sva_lock);
}
}
EXPORT_SYMBOL_GPL(__iommu_sva_unbind_dev_all);diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index 439c8fffd836..caa6f79785b9 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -24,6 +24,7 @@
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/err.h>
+#include <linux/mmu_notifier.h>
#include <linux/of.h>
#include <uapi/linux/iommu.h>
@@ -111,10 +112,15 @@ struct io_mm {
unsigned long flags;
struct list_head devices;
struct kref kref;
+#if defined(CONFIG_MMU_NOTIFIER)
+ struct mmu_notifier notifier;
+#endif
struct mm_struct *mm;
/* Release callback for this mm */
void (*release)(struct io_mm *io_mm);
+ /* For postponed release */
+ struct rcu_head rcu;
};
enum iommu_cap {@@ -249,6 +255,7 @@ struct iommu_sva_param {
* @mm_attach: attach io_mm to a device. Install PASID entry if necessary
* @mm_detach: detach io_mm from a device. Remove PASID entry and
* flush associated TLB entries.
+ * @mm_invalidate: Invalidate a range of mappings for an mm
* @map: map a physically contiguous memory region to an iommu domain
* @unmap: unmap a physically contiguous memory region from an iommu domain
* @map_sg: map a scatter-gather list of physically contiguous memory chunks@@ -298,6 +305,9 @@ struct iommu_ops {
struct io_mm *io_mm, bool attach_domain);
void (*mm_detach)(struct iommu_domain *domain, struct device *dev,
struct io_mm *io_mm, bool detach_domain);
+ void (*mm_invalidate)(struct iommu_domain *domain, struct device *dev,
+ struct io_mm *io_mm, unsigned long vaddr,
+ size_t size);
int (*map)(struct iommu_domain *domain, unsigned long iova,
phys_addr_t paddr, size_t size, int prot);
size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,