[PATCH v8 09/10] iommu/ioasid: Add notifier for status change

From: Jacob Pan
Date: Mon Dec 16 2019 - 15:11:50 EST


IOASIDs are system resources that can be shared by multiple drivers or
subsystems. When status of an IOASID changes at runtime, there is need
to notify all current users such that proper actions can be taken.

For example, an IOASID can be used by IOMMU subsystem for guest SVM as
well as KVM. When the guest is terminating unexpectedly, both KVM and
IOMMU need to perform clean up action before the IOASID is reclaimed.

This patch adds a per IOASID notifier that can be registered by
interesting parties.

Signed-off-by: Jacob Pan <jacob.jun.pan@xxxxxxxxxxxxxxx>
---
drivers/iommu/ioasid.c | 43 +++++++++++++++++++++++++++++++++++++++++++
include/linux/ioasid.h | 20 ++++++++++++++++++++
2 files changed, 63 insertions(+)

diff --git a/drivers/iommu/ioasid.c b/drivers/iommu/ioasid.c
index 0f8dd377aada..53a2ab287f7d 100644
--- a/drivers/iommu/ioasid.c
+++ b/drivers/iommu/ioasid.c
@@ -15,6 +15,7 @@ struct ioasid_data {
struct ioasid_set *set;
void *private;
struct rcu_head rcu;
+ struct atomic_notifier_head notifications;
};

/*
@@ -314,6 +315,7 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max,

data->set = set;
data->private = private;
+ ATOMIC_INIT_NOTIFIER_HEAD(&data->notifications);

/*
* Custom allocator needs allocator data to perform platform specific
@@ -360,6 +362,9 @@ void ioasid_free(ioasid_t ioasid)
goto exit_unlock;
}

+ /* Notify all users that this IOASID is being freed */
+ atomic_notifier_call_chain(&ioasid_data->notifications, IOASID_FREE,
+ &ioasid);
active_allocator->ops->free(ioasid, active_allocator->ops->pdata);
/* Custom allocator needs additional steps to free the xa element */
if (active_allocator->flags & IOASID_ALLOCATOR_CUSTOM) {
@@ -416,6 +421,44 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid,
}
EXPORT_SYMBOL_GPL(ioasid_find);

+int ioasid_add_notifier(ioasid_t ioasid, struct notifier_block *nb)
+{
+ struct ioasid_allocator_data *idata;
+ struct ioasid_data *data;
+ int ret = 0;
+
+ rcu_read_lock();
+ idata = rcu_dereference(active_allocator);
+ data = xa_load(&idata->xa, ioasid);
+ if (!data) {
+ ret = -ENOENT;
+ goto unlock;
+ }
+ ret = atomic_notifier_chain_register(&data->notifications, nb);
+unlock:
+ rcu_read_unlock();
+ return ret;
+}
+EXPORT_SYMBOL_GPL(ioasid_add_notifier);
+
+void ioasid_remove_notifier(ioasid_t ioasid, struct notifier_block *nb)
+{
+ struct ioasid_allocator_data *idata;
+ struct ioasid_data *data;
+
+ rcu_read_lock();
+ idata = rcu_dereference(active_allocator);
+ data = xa_load(&idata->xa, ioasid);
+ rcu_read_unlock();
+ if (!data) {
+ pr_err("IOASID %d not found\n", ioasid);
+ return;
+ }
+ /* Unregister can sleep, called outside RCU critical section. */
+ atomic_notifier_chain_unregister(&data->notifications, nb);
+}
+EXPORT_SYMBOL_GPL(ioasid_remove_notifier);
+
MODULE_AUTHOR("Jean-Philippe Brucker <jean-philippe.brucker@xxxxxxx>");
MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@xxxxxxxxxxxxxxx>");
MODULE_DESCRIPTION("IO Address Space ID (IOASID) allocator");
diff --git a/include/linux/ioasid.h b/include/linux/ioasid.h
index 6f000d7a0ddc..4517c4be4088 100644
--- a/include/linux/ioasid.h
+++ b/include/linux/ioasid.h
@@ -4,6 +4,7 @@

#include <linux/types.h>
#include <linux/errno.h>
+#include <linux/notifier.h>

#define INVALID_IOASID ((ioasid_t)-1)
typedef unsigned int ioasid_t;
@@ -29,6 +30,12 @@ struct ioasid_allocator_ops {
void *pdata;
};

+/* Notification data when IOASID status changed */
+enum ioasid_notify_val {
+ IOASID_FREE = 1,
+ IOASID_SUSPEND,
+};
+
#define DECLARE_IOASID_SET(name) struct ioasid_set name = { 0 }

#if IS_ENABLED(CONFIG_IOASID)
@@ -40,6 +47,8 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid,
int ioasid_register_allocator(struct ioasid_allocator_ops *allocator);
void ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator);
int ioasid_set_data(ioasid_t ioasid, void *data);
+int ioasid_add_notifier(ioasid_t ioasid, struct notifier_block *nb);
+void ioasid_remove_notifier(ioasid_t ioasid, struct notifier_block *nb);

#else /* !CONFIG_IOASID */
static inline ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min,
@@ -58,6 +67,17 @@ static inline void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid,
return NULL;
}

+static inline int ioasid_add_notifier(ioasid_t ioasid,
+ struct notifier_block *nb)
+{
+ return -ENOTSUPP;
+}
+
+static inline void ioasid_remove_notifier(ioasid_t ioasid,
+ struct notifier_block *nb)
+{
+}
+
static inline int ioasid_register_allocator(struct ioasid_allocator_ops *allocator)
{
return -ENOTSUPP;
--
2.7.4