In preparation for modularizing io-pgtable formats, add support
for reference counting the io-pgtable format modules to ensure
that the modules are not unloaded while they are in use.

Signed-off-by: Isaac J. Manjarres <isa...@codeaurora.org>
---
 drivers/iommu/io-pgtable-arm-v7s.c |  1 +
 drivers/iommu/io-pgtable-arm.c     |  5 +++++
 drivers/iommu/io-pgtable.c         | 12 ++++++++++--
 include/linux/io-pgtable.h         |  2 ++
 4 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/io-pgtable-arm-v7s.c 
b/drivers/iommu/io-pgtable-arm-v7s.c
index 080881b..7e81135 100644
--- a/drivers/iommu/io-pgtable-arm-v7s.c
+++ b/drivers/iommu/io-pgtable-arm-v7s.c
@@ -839,6 +839,7 @@ static struct io_pgtable *arm_v7s_alloc_pgtable(struct 
io_pgtable_cfg *cfg,
 static struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns = {
        .alloc  = arm_v7s_alloc_pgtable,
        .free   = arm_v7s_free_pgtable,
+       .owner  = THIS_MODULE,
 };
 
 #ifdef CONFIG_IOMMU_IO_PGTABLE_ARMV7S_SELFTEST
diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c
index e1f8d54..8ed52a0 100644
--- a/drivers/iommu/io-pgtable-arm.c
+++ b/drivers/iommu/io-pgtable-arm.c
@@ -1054,26 +1054,31 @@ static struct arm_lpae_io_pgtable_init_fns 
arm_lpae_init_fns_table[] = {
                .fmt            = ARM_32_LPAE_S1,
                .init_fns.alloc = arm_32_lpae_alloc_pgtable_s1,
                .init_fns.free  = arm_lpae_free_pgtable,
+               .init_fns.owner = THIS_MODULE,
        },
        {
                .fmt            = ARM_32_LPAE_S2,
                .init_fns.alloc = arm_32_lpae_alloc_pgtable_s2,
                .init_fns.free  = arm_lpae_free_pgtable,
+               .init_fns.owner = THIS_MODULE,
        },
        {
                .fmt            = ARM_64_LPAE_S1,
                .init_fns.alloc = arm_64_lpae_alloc_pgtable_s1,
                .init_fns.free  = arm_lpae_free_pgtable,
+               .init_fns.owner = THIS_MODULE,
        },
        {
                .fmt            = ARM_64_LPAE_S2,
                .init_fns.alloc = arm_64_lpae_alloc_pgtable_s2,
                .init_fns.free  = arm_lpae_free_pgtable,
+               .init_fns.owner = THIS_MODULE,
        },
        {
                .fmt            = ARM_MALI_LPAE,
                .init_fns.alloc = arm_mali_lpae_alloc_pgtable,
                .init_fns.free  = arm_lpae_free_pgtable,
+               .init_fns.owner = THIS_MODULE,
        },
 };
 
diff --git a/drivers/iommu/io-pgtable.c b/drivers/iommu/io-pgtable.c
index 95e872d..9792e25 100644
--- a/drivers/iommu/io-pgtable.c
+++ b/drivers/iommu/io-pgtable.c
@@ -10,6 +10,7 @@
 #include <linux/bug.h>
 #include <linux/io-pgtable.h>
 #include <linux/kernel.h>
+#include <linux/module.h>
 #include <linux/types.h>
 
 static struct io_pgtable_init_fns *io_pgtable_init_table[IO_PGTABLE_NUM_FMTS];
@@ -28,9 +29,14 @@ struct io_pgtable_ops *alloc_io_pgtable_ops(enum 
io_pgtable_fmt fmt,
        if (!fns)
                return NULL;
 
+       if (!try_module_get(fns->owner))
+               return NULL;
+
        iop = fns->alloc(cfg, cookie);
-       if (!iop)
+       if (!iop) {
+               module_put(fns->owner);
                return NULL;
+       }
 
        iop->fmt        = fmt;
        iop->cookie     = cookie;
@@ -55,8 +61,10 @@ void free_io_pgtable_ops(struct io_pgtable_ops *ops)
        iop = io_pgtable_ops_to_pgtable(ops);
        io_pgtable_tlb_flush_all(iop);
        fns = io_pgtable_init_table[iop->fmt];
-       if (fns)
+       if (fns) {
                fns->free(iop);
+               module_put(fns->owner);
+       }
 }
 EXPORT_SYMBOL_GPL(free_io_pgtable_ops);
 
diff --git a/include/linux/io-pgtable.h b/include/linux/io-pgtable.h
index a460ae1..bdf0a01 100644
--- a/include/linux/io-pgtable.h
+++ b/include/linux/io-pgtable.h
@@ -168,10 +168,12 @@ struct io_pgtable_ops {
  *
  * @alloc: Allocate a set of page tables described by cfg.
  * @free:  Free the page tables associated with iop.
+ * @owner: Driver module providing these ops.
  */
 struct io_pgtable_init_fns {
        struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie);
        void (*free)(struct io_pgtable *iop);
+       struct module *owner;
 };
 
 /**
-- 
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project

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

Reply via email to