[PATCH v2 2/7] iommu/io-pgtable: Add refcounting for io-pgtable format modules

From: Isaac J. Manjarres
Date: Mon Dec 21 2020 - 19:46:28 EST


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 <isaacm@xxxxxxxxxxxxxx>
---
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 89aad2f..a5cb755a 100644
--- a/drivers/iommu/io-pgtable-arm-v7s.c
+++ b/drivers/iommu/io-pgtable-arm-v7s.c
@@ -840,6 +840,7 @@ static struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns = {
.fmt = ARM_V7S,
.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 ff0ea2f..e8b1e34 100644
--- a/drivers/iommu/io-pgtable-arm.c
+++ b/drivers/iommu/io-pgtable-arm.c
@@ -1049,26 +1049,31 @@ static struct io_pgtable_init_fns io_pgtable_arm_lpae_init_fns[] = {
.fmt = ARM_32_LPAE_S1,
.alloc = arm_32_lpae_alloc_pgtable_s1,
.free = arm_lpae_free_pgtable,
+ .owner = THIS_MODULE,
},
{
.fmt = ARM_32_LPAE_S2,
.alloc = arm_32_lpae_alloc_pgtable_s2,
.free = arm_lpae_free_pgtable,
+ .owner = THIS_MODULE,
},
{
.fmt = ARM_64_LPAE_S1,
.alloc = arm_64_lpae_alloc_pgtable_s1,
.free = arm_lpae_free_pgtable,
+ .owner = THIS_MODULE,
},
{
.fmt = ARM_64_LPAE_S2,
.alloc = arm_64_lpae_alloc_pgtable_s2,
.free = arm_lpae_free_pgtable,
+ .owner = THIS_MODULE,
},
{
.fmt = ARM_MALI_LPAE,
.alloc = arm_mali_lpae_alloc_pgtable,
.free = arm_lpae_free_pgtable,
+ .owner = THIS_MODULE,
},
};

diff --git a/drivers/iommu/io-pgtable.c b/drivers/iommu/io-pgtable.c
index 2c6eb2e..cc83542 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/rwlock.h>
#include <linux/slab.h>
#include <linux/types.h>
@@ -52,9 +53,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;
@@ -79,8 +85,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_get_init_fns(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 45b367ce..a03b262 100644
--- a/include/linux/io-pgtable.h
+++ b/include/linux/io-pgtable.h
@@ -169,11 +169,13 @@ struct io_pgtable_ops {
* @fmt: The page table format.
* @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 {
enum io_pgtable_fmt fmt;
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