iommu: Add iommu_init/deinit_device() paired functions
authorJason Gunthorpe <jgg@nvidia.com>
Tue, 6 Jun 2023 00:59:43 +0000 (21:59 -0300)
committerJoerg Roedel <jroedel@suse.de>
Fri, 14 Jul 2023 14:14:14 +0000 (16:14 +0200)
commitaa0958570f24f562422afa41fefd1b3a1fe0f6d0
treeb51a0ffee6be58c50ec4c2d4a4ba1bbc35140ab3
parentdf15d76dcacac2126a4d20ba06c9d7e4b18bad8e
iommu: Add iommu_init/deinit_device() paired functions

Move the driver init and destruction code into two logically paired
functions.

There is a subtle ordering dependency in how the group's domains are
freed, the current code does the kobject_put() on the group which will
hopefully trigger the free of the domains before the module_put() that
protects the domain->ops.

Reorganize this to be explicit and documented. The domains are cleaned up
by iommu_deinit_device() if it is the last device to be deinit'd from the
group.  This must be done in a specific order - after
ops->release_device() and before the module_put(). Make it very clear and
obvious by putting the order directly in one function.

Leave WARN_ON's in case the refcounting gets messed up somehow.

This also moves the module_put() and dev_iommu_free() under the
group->mutex to keep the code simple.

Building paired functions like this helps ensure that error cleanup flows
in __iommu_probe_device() are correct because they share the same code
that handles the normal flow. These details become relavent as following
patches add more error unwind into __iommu_probe_device(), and ultimately
a following series adds fine-grained locking to __iommu_probe_device().

Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/5-v3-328044aa278c+45e49-iommu_probe_jgg@nvidia.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
drivers/iommu/iommu.c