[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230731120813.123247-2-abhijit.gangurde@amd.com>
Date: Mon, 31 Jul 2023 17:38:10 +0530
From: Abhijit Gangurde <abhijit.gangurde@....com>
To: <gregkh@...uxfoundation.org>, <masahiroy@...nel.org>,
<linux-kernel@...r.kernel.org>
CC: <michal.simek@....com>, <git@....com>, <nikhil.agarwal@....com>,
<Nipun.Gupta@....com>, Abhijit Gangurde <abhijit.gangurde@....com>
Subject: [PATCH v2 1/4] cdx: Introduce lock to protect controller ops and controller list
Add a mutex lock to prevent race between controller ops initiated by
the bus subsystem and the controller registration/unregistration.
Signed-off-by: Abhijit Gangurde <abhijit.gangurde@....com>
---
drivers/cdx/cdx.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/drivers/cdx/cdx.c b/drivers/cdx/cdx.c
index d2cad4c670a0..66797c8fe400 100644
--- a/drivers/cdx/cdx.c
+++ b/drivers/cdx/cdx.c
@@ -72,6 +72,8 @@
/* CDX controllers registered with the CDX bus */
static DEFINE_XARRAY_ALLOC(cdx_controllers);
+/* Lock to protect controller ops and controller list */
+static DEFINE_MUTEX(cdx_controller_lock);
/**
* cdx_dev_reset - Reset a CDX device
@@ -393,6 +395,8 @@ static ssize_t rescan_store(const struct bus_type *bus,
if (!val)
return -EINVAL;
+ mutex_lock(&cdx_controller_lock);
+
/* Unregister all the devices on the bus */
cdx_unregister_devices(&cdx_bus_type);
@@ -405,6 +409,8 @@ static ssize_t rescan_store(const struct bus_type *bus,
dev_err(cdx->dev, "cdx bus scanning failed\n");
}
+ mutex_unlock(&cdx_controller_lock);
+
return count;
}
static BUS_ATTR_WO(rescan);
@@ -520,9 +526,12 @@ int cdx_register_controller(struct cdx_controller *cdx)
{
int ret;
+ mutex_lock(&cdx_controller_lock);
+
ret = xa_alloc(&cdx_controllers, &cdx->id, cdx,
XA_LIMIT(0, MAX_CDX_CONTROLLERS - 1), GFP_KERNEL);
if (ret) {
+ mutex_unlock(&cdx_controller_lock);
dev_err(cdx->dev,
"No free index available. Maximum controllers already registered\n");
cdx->id = (u8)MAX_CDX_CONTROLLERS;
@@ -531,6 +540,7 @@ int cdx_register_controller(struct cdx_controller *cdx)
/* Scan all the devices */
cdx->ops->scan(cdx);
+ mutex_unlock(&cdx_controller_lock);
return 0;
}
@@ -541,8 +551,12 @@ void cdx_unregister_controller(struct cdx_controller *cdx)
if (cdx->id >= MAX_CDX_CONTROLLERS)
return;
+ mutex_lock(&cdx_controller_lock);
+
device_for_each_child(cdx->dev, NULL, cdx_unregister_device);
xa_erase(&cdx_controllers, cdx->id);
+
+ mutex_unlock(&cdx_controller_lock);
}
EXPORT_SYMBOL_GPL(cdx_unregister_controller);
--
2.25.1
Powered by blists - more mailing lists