[patch 02/32] genirq/msi: Add mutex for MSI list protection

From: Thomas Gleixner
Date: Fri Nov 26 2021 - 20:29:13 EST


For upcoming runtime extensions of MSI-X interrupts it's required to
protect the MSI descriptor list. Add a mutex to struct msi_device_data and
provide lock/unlock functions.

Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
---
include/linux/msi.h | 6 ++++++
kernel/irq/msi.c | 25 +++++++++++++++++++++++++
2 files changed, 31 insertions(+)

--- a/include/linux/msi.h
+++ b/include/linux/msi.h
@@ -3,6 +3,7 @@
#define LINUX_MSI_H

#include <linux/spinlock.h>
+#include <linux/mutex.h>
#include <linux/list.h>
#include <linux/bits.h>
#include <asm/msi.h>
@@ -146,6 +147,7 @@ struct msi_desc {
* @attrs: Pointer to the sysfs attribute group
* @platform_data: Platform-MSI specific data
* @list: List of MSI descriptors associated to the device
+ * @mutex: Mutex protecting the MSI list
*/
struct msi_device_data {
raw_spinlock_t lock;
@@ -153,6 +155,7 @@ struct msi_device_data {
const struct attribute_group **attrs;
struct platform_msi_priv_data *platform_data;
struct list_head list;
+ struct mutex mutex;
};

int msi_setup_device_data(struct device *dev);
@@ -187,6 +190,9 @@ static inline unsigned int msi_get_virq(
return ret < 0 ? 0 : ret;
}

+void msi_lock_descs(struct device *dev);
+void msi_unlock_descs(struct device *dev);
+
/* Helpers to hide struct msi_desc implementation details */
#define msi_desc_to_dev(desc) ((desc)->dev)
#define dev_to_msi_list(dev) (&(dev)->msi.data->list)
--- a/kernel/irq/msi.c
+++ b/kernel/irq/msi.c
@@ -116,12 +116,37 @@ int msi_setup_device_data(struct device

raw_spin_lock_init(&md->lock);
INIT_LIST_HEAD(&md->list);
+ mutex_init(&md->mutex);
dev->msi.data = md;
devres_add(dev, md);
return 0;
}

/**
+ * msi_lock_descs - Lock the MSI descriptor storage of a device
+ * @dev: Device to operate on
+ */
+void msi_lock_descs(struct device *dev)
+{
+ if (WARN_ON_ONCE(!dev->msi.data))
+ return;
+ mutex_lock(&dev->msi.data->mutex);
+}
+EXPORT_SYMBOL_GPL(msi_lock_descs);
+
+/**
+ * msi_unlock_descs - Unlock the MSI descriptor storage of a device
+ * @dev: Device to operate on
+ */
+void msi_unlock_descs(struct device *dev)
+{
+ if (WARN_ON_ONCE(!dev->msi.data))
+ return;
+ mutex_unlock(&dev->msi.data->mutex);
+}
+EXPORT_SYMBOL_GPL(msi_unlock_descs);
+
+/**
* __msi_get_virq - Return Linux interrupt number of a MSI interrupt
* @dev: Device to operate on
* @index: MSI interrupt index to look for (0-based)