iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
From: Jean-Philippe Brucker <jean-philippe@linaro.org>
To: Will Deacon <will@kernel.org>
Cc: Bjorn Helgaas <bhelgaas@google.com>,
	Robin Murphy <robin.murphy@arm.com>,
	iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 5/7] iommu/arm-smmu-v3: Allow building as a module
Date: Mon, 4 Nov 2019 20:15:24 +0100	[thread overview]
Message-ID: <20191104191524.GA2786242@lophozonia> (raw)
In-Reply-To: <20191031154247.GB28061@willie-the-truck>

[-- Attachment #1: Type: text/plain, Size: 2603 bytes --]

On Thu, Oct 31, 2019 at 03:42:47PM +0000, Will Deacon wrote:
> > Sorry for the stupid question, but what prevents the iommu module from
> > being unloaded when there are active users? There are no symbol
> > dependencies to endpoint device drivers, because the interface is only
> > exposed through the iommu-api, right? Is some sort of manual module
> > reference counting needed?
> 
> Generally, I think unloading the IOMMU driver module while there are
> active users is a pretty bad idea, much like unbinding the driver via
> /sys in the same situation would also be fairly daft. However, I *think*
> the code in __device_release_driver() tries to deal with this by
> iterating over the active consumers and ->remove()ing them first.

> I'm without hardware access at the moment, so I haven't been able to
> test this myself. We could nobble the module_exit() hook, but there's
> still the "force unload" option depending on the .config.

Shame that we can't completely prevent module unloading, because handling
rmmod cleanly is tricky.

On module unload we also need to tidy up the bus->iommu_ops installed by
bus_set_iommu(), and remove the IOMMU groups (and probably other leaks I
missed). I have a solution for the bus->iommu_ops, which is simply adding
a bus_unset_iommu() counterpart with a refcount, but it doesn't deal with
the IOMMU groups cleanly. If there are multiple IOMMU instances managing
one bus, then we should only remove the IOMMU groups belonging to the
instance that is being removed.

I'll think about this more, but the simple solution is attached if you
want to test. It at least works with a single IOMMU now:

$ modprobe virtio-iommu
[   25.180965] virtio_iommu virtio0: input address: 64 bits
[   25.181437] virtio_iommu virtio0: page mask: 0xfffffffffffff000
[   25.214493] virtio-pci 0000:00:03.0: Adding to iommu group 0
[   25.233252] virtio-pci 0000:00:03.0: enabling device (0000 -> 0003)
[   25.334810] e1000e 0000:00:02.0: Adding to iommu group 1
[   25.348997] e1000e 0000:00:02.0: enabling device (0000 -> 0002)
... net test etc

$ rmmod virtio-iommu
[   34.084816] e1000e: eth1 NIC Link is Down
[   34.212152] pci 0000:00:02.0: Removing from iommu group 1
[   34.250558] pci 0000:00:03.0: Removing from iommu group 0
[   34.261570] virtio_iommu virtio0: device removed

$ modprobe virtio-iommu
[   34.828982] virtio_iommu virtio0: input address: 64 bits
[   34.829442] virtio_iommu virtio0: page mask: 0xfffffffffffff000
[   34.844576] virtio-pci 0000:00:03.0: Adding to iommu group 0
[   34.916449] e1000e 0000:00:02.0: Adding to iommu group 1

Thanks,
Jean

[-- Attachment #2: 0001-iommu-Add-bus_unset_iommu.patch --]
[-- Type: text/plain, Size: 5399 bytes --]

From 5437fcaabe1d4671e2dc5b90b7898c0bf698111b Mon Sep 17 00:00:00 2001
From: Jean-Philippe Brucker <jean-philippe@linaro.org>
Date: Mon, 4 Nov 2019 15:52:36 +0100
Subject: [PATCH] iommu: Add bus_unset_iommu()

Let modular IOMMU drivers undo bus_set_iommu(). Keep track of bus
registrations with a list and refcount, and remove the iommu_ops from
the bus when there are no IOMMU providers anymore.

Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
 drivers/iommu/iommu.c | 101 ++++++++++++++++++++++++++++++++++--------
 include/linux/iommu.h |   1 +
 2 files changed, 84 insertions(+), 18 deletions(-)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 393a5376d7c6..f9bac5633f2a 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -31,6 +31,9 @@ static unsigned int iommu_def_domain_type __read_mostly;
 static bool iommu_dma_strict __read_mostly = true;
 static u32 iommu_cmd_line __read_mostly;
 
+static DEFINE_MUTEX(iommu_bus_notifiers_lock);
+static LIST_HEAD(iommu_bus_notifiers);
+
 struct iommu_group {
 	struct kobject kobj;
 	struct kobject *devices_kobj;
@@ -58,6 +61,14 @@ struct iommu_group_attribute {
 			 const char *buf, size_t count);
 };
 
+struct iommu_bus_notifier {
+	struct notifier_block	nb;
+	const struct iommu_ops	*ops;
+	struct bus_type		*bus;
+	struct list_head	list;
+	refcount_t		refs;
+};
+
 static const char * const iommu_group_resv_type_string[] = {
 	[IOMMU_RESV_DIRECT]			= "direct",
 	[IOMMU_RESV_DIRECT_RELAXABLE]		= "direct-relaxable",
@@ -1494,15 +1505,29 @@ static int iommu_bus_notifier(struct notifier_block *nb,
 static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
 {
 	int err;
-	struct notifier_block *nb;
+	struct iommu_bus_notifier *iommu_notifier;
 
-	nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
-	if (!nb)
-		return -ENOMEM;
+	list_for_each_entry(iommu_notifier, &iommu_bus_notifiers, list) {
+		if (iommu_notifier->ops == ops && iommu_notifier->bus == bus) {
+			refcount_inc(&iommu_notifier->refs);
+			return 0;
+		}
+	}
+
+	bus->iommu_ops = ops;
+
+	iommu_notifier = kzalloc(sizeof(*iommu_notifier), GFP_KERNEL);
+	if (!iommu_notifier) {
+		err = -ENOMEM;
+		goto out_clear;
+	}
 
-	nb->notifier_call = iommu_bus_notifier;
+	iommu_notifier->ops = ops;
+	iommu_notifier->bus = bus;
+	iommu_notifier->nb.notifier_call = iommu_bus_notifier;
+	refcount_set(&iommu_notifier->refs, 1);
 
-	err = bus_register_notifier(bus, nb);
+	err = bus_register_notifier(bus, &iommu_notifier->nb);
 	if (err)
 		goto out_free;
 
@@ -1510,20 +1535,47 @@ static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
 	if (err)
 		goto out_err;
 
-
+	list_add(&iommu_notifier->list, &iommu_bus_notifiers);
 	return 0;
 
 out_err:
 	/* Clean up */
 	bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
-	bus_unregister_notifier(bus, nb);
-
+	bus_unregister_notifier(bus, &iommu_notifier->nb);
 out_free:
-	kfree(nb);
+	kfree(iommu_notifier);
+out_clear:
+	bus->iommu_ops = NULL;
 
 	return err;
 }
 
+static int iommu_bus_remove(struct bus_type *bus, const struct iommu_ops *ops)
+{
+	struct iommu_bus_notifier *tmp;
+	struct iommu_bus_notifier *iommu_notifier = NULL;
+
+	list_for_each_entry(tmp, &iommu_bus_notifiers, list) {
+		if (tmp->ops == ops && tmp->bus == bus) {
+			iommu_notifier = tmp;
+			break;
+		}
+	}
+
+	if (!iommu_notifier)
+		return -ESRCH;
+
+	if (!refcount_dec_and_test(&iommu_notifier->refs))
+		return 0;
+
+	list_del(&iommu_notifier->list);
+	bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
+	bus_unregister_notifier(bus, &iommu_notifier->nb);
+	kfree(iommu_notifier);
+	bus->iommu_ops = NULL;
+	return 0;
+}
+
 /**
  * bus_set_iommu - set iommu-callbacks for the bus
  * @bus: bus.
@@ -1541,20 +1593,33 @@ int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
 {
 	int err;
 
-	if (bus->iommu_ops != NULL)
-		return -EBUSY;
-
-	bus->iommu_ops = ops;
-
 	/* Do IOMMU specific setup for this bus-type */
-	err = iommu_bus_init(bus, ops);
-	if (err)
-		bus->iommu_ops = NULL;
+	mutex_lock(&iommu_bus_notifiers_lock);
+	if (bus->iommu_ops != NULL && bus->iommu_ops != ops)
+		err = -EBUSY;
+	else
+		err = iommu_bus_init(bus, ops);
+	mutex_unlock(&iommu_bus_notifiers_lock);
 
 	return err;
 }
 EXPORT_SYMBOL_GPL(bus_set_iommu);
 
+int bus_unset_iommu(struct bus_type *bus, const struct iommu_ops *ops)
+{
+	int err;
+
+	mutex_lock(&iommu_bus_notifiers_lock);
+	if (bus->iommu_ops != ops)
+		err = -EINVAL;
+	else
+		err = iommu_bus_remove(bus, ops);
+	mutex_unlock(&iommu_bus_notifiers_lock);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(bus_unset_iommu);
+
 bool iommu_present(struct bus_type *bus)
 {
 	return bus->iommu_ops != NULL;
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index 29bac5345563..15c9115e31ff 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -408,6 +408,7 @@ static inline void iommu_iotlb_gather_init(struct iommu_iotlb_gather *gather)
 #define IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER	6 /* Post Driver unbind */
 
 extern int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops);
+extern int bus_unset_iommu(struct bus_type *bus, const struct iommu_ops *ops);
 extern bool iommu_present(struct bus_type *bus);
 extern bool iommu_capable(struct bus_type *bus, enum iommu_cap cap);
 extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus);
-- 
2.23.0


[-- Attachment #3: Type: text/plain, Size: 156 bytes --]

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

  reply	other threads:[~2019-11-04 19:15 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-30 14:51 [PATCH 0/7] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
2019-10-30 14:51 ` [PATCH 1/7] drivers/iommu: Export core IOMMU API symbols to permit modular drivers Will Deacon
2019-10-30 14:51 ` [PATCH 2/7] iommu/of: Request ACS from the PCI core when configuring IOMMU linkage Will Deacon
2019-10-30 14:51 ` [PATCH 3/7] PCI: Export pci_ats_disabled() as a GPL symbol to modules Will Deacon
2019-10-30 20:30   ` Bjorn Helgaas
2019-10-30 14:51 ` [PATCH 4/7] Revert "iommu/arm-smmu: Make arm-smmu-v3 explicitly non-modular" Will Deacon
2019-10-30 14:51 ` [PATCH 5/7] iommu/arm-smmu-v3: Allow building as a module Will Deacon
2019-10-30 19:31   ` Joerg Roedel
2019-10-31 15:42     ` Will Deacon
2019-11-04 19:15       ` Jean-Philippe Brucker [this message]
2019-11-08 14:54         ` Will Deacon
2019-11-05 12:15       ` Joerg Roedel
2019-11-08 11:03         ` Will Deacon
2019-10-30 14:51 ` [PATCH 6/7] Revert "iommu/arm-smmu: Make arm-smmu explicitly non-modular" Will Deacon
2019-10-30 23:09   ` Jordan Crouse
2019-10-31 12:03     ` Will Deacon
2019-10-31 15:32       ` Jordan Crouse
2019-10-30 14:51 ` [PATCH 7/7] iommu/arm-smmu: Allow building as a module Will Deacon
2019-10-30 15:22   ` Rob Herring
2019-10-30 15:26     ` Will Deacon
2019-10-30 15:33     ` Robin Murphy
2019-11-04 19:34   ` Isaac J. Manjarres
2019-11-07 12:48     ` Will Deacon
2019-10-30 15:35 ` [PATCH 0/7] iommu: Permit modular builds of ARM SMMU[v3] drivers Robin Murphy
2019-10-30 15:54   ` Will Deacon
2019-10-31  0:57     ` Saravana Kannan via iommu
2019-10-31 19:37       ` Jean-Philippe Brucker
2019-10-31 23:34         ` Saravana Kannan via iommu
2019-11-01 10:27           ` John Garry
2019-11-01 21:13             ` Saravana Kannan via iommu
2019-11-04 12:16               ` John Garry
2019-11-04 13:29                 ` Robin Murphy
2019-11-07  6:11                   ` Saravana Kannan via iommu
2019-11-07  9:13                     ` Jean-Philippe Brucker
2019-11-07  6:02                 ` Saravana Kannan via iommu
2019-11-01 11:41           ` Jean-Philippe Brucker
2019-11-01 12:28             ` Lorenzo Pieralisi
2019-11-01 21:26               ` Saravana Kannan via iommu
2019-11-04 11:43                 ` Lorenzo Pieralisi
2019-11-07  5:55                   ` Saravana Kannan via iommu
2019-11-01 17:21         ` Will Deacon
2019-11-04  7:54           ` Jean-Philippe Brucker
2019-11-07  6:16       ` Saravana Kannan via iommu

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=20191104191524.GA2786242@lophozonia \
    --to=jean-philippe@linaro.org \
    --cc=bhelgaas@google.com \
    --cc=iommu@lists.linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robin.murphy@arm.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;
as well as URLs for NNTP newsgroup(s).