pci: introduce a global hotplug lock

Despite the scary commit title, this patch just unifies
per-driver mutexes into a single pci mutex.

On each hotplug we modify some DPDK global resources,
which per-driver locks aren't sufficient for. If
multiple threads try to attach devices at the same time,
then we'll likely have a data race. DPDK hotplug APIs
don't provide any kind of thread safety on their own.

Change-Id: I89cca9fea04ecf576ec5854c662bae1d3712b3fb
Signed-off-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-on: https://review.gerrithub.io/433864
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Darek Stojaczyk 2018-11-18 02:48:56 +01:00
parent 9cf7d886af
commit d808a62db5
5 changed files with 8 additions and 10 deletions

View File

@ -73,7 +73,6 @@ struct spdk_pci_enum_ctx {
struct rte_pci_driver driver; struct rte_pci_driver driver;
spdk_pci_enum_cb cb_fn; spdk_pci_enum_cb cb_fn;
void *cb_arg; void *cb_arg;
pthread_mutex_t mtx;
bool is_registered; bool is_registered;
}; };

View File

@ -40,6 +40,8 @@
#define PCI_CFG_SIZE 256 #define PCI_CFG_SIZE 256
#define PCI_EXT_CAP_ID_SN 0x03 #define PCI_EXT_CAP_ID_SN 0x03
static pthread_mutex_t g_pci_mutex = PTHREAD_MUTEX_INITIALIZER;
int int
spdk_pci_device_init(struct rte_pci_driver *driver, spdk_pci_device_init(struct rte_pci_driver *driver,
struct rte_pci_device *device) struct rte_pci_device *device)
@ -122,7 +124,7 @@ spdk_pci_device_attach(struct spdk_pci_enum_ctx *ctx,
addr.function = pci_address->func; addr.function = pci_address->func;
#endif #endif
pthread_mutex_lock(&ctx->mtx); pthread_mutex_lock(&g_pci_mutex);
if (!ctx->is_registered) { if (!ctx->is_registered) {
ctx->is_registered = true; ctx->is_registered = true;
@ -147,13 +149,13 @@ spdk_pci_device_attach(struct spdk_pci_enum_ctx *ctx,
#endif #endif
ctx->cb_arg = NULL; ctx->cb_arg = NULL;
ctx->cb_fn = NULL; ctx->cb_fn = NULL;
pthread_mutex_unlock(&ctx->mtx); pthread_mutex_unlock(&g_pci_mutex);
return -1; return -1;
} }
ctx->cb_arg = NULL; ctx->cb_arg = NULL;
ctx->cb_fn = NULL; ctx->cb_fn = NULL;
pthread_mutex_unlock(&ctx->mtx); pthread_mutex_unlock(&g_pci_mutex);
return 0; return 0;
} }
@ -167,7 +169,7 @@ spdk_pci_enumerate(struct spdk_pci_enum_ctx *ctx,
spdk_pci_enum_cb enum_cb, spdk_pci_enum_cb enum_cb,
void *enum_ctx) void *enum_ctx)
{ {
pthread_mutex_lock(&ctx->mtx); pthread_mutex_lock(&g_pci_mutex);
if (!ctx->is_registered) { if (!ctx->is_registered) {
ctx->is_registered = true; ctx->is_registered = true;
@ -190,13 +192,13 @@ spdk_pci_enumerate(struct spdk_pci_enum_ctx *ctx,
#endif #endif
ctx->cb_arg = NULL; ctx->cb_arg = NULL;
ctx->cb_fn = NULL; ctx->cb_fn = NULL;
pthread_mutex_unlock(&ctx->mtx); pthread_mutex_unlock(&g_pci_mutex);
return -1; return -1;
} }
ctx->cb_arg = NULL; ctx->cb_arg = NULL;
ctx->cb_fn = NULL; ctx->cb_fn = NULL;
pthread_mutex_unlock(&ctx->mtx); pthread_mutex_unlock(&g_pci_mutex);
return 0; return 0;
} }

View File

@ -105,7 +105,6 @@ static struct spdk_pci_enum_ctx g_ioat_pci_drv = {
.cb_fn = NULL, .cb_fn = NULL,
.cb_arg = NULL, .cb_arg = NULL,
.mtx = PTHREAD_MUTEX_INITIALIZER,
.is_registered = false, .is_registered = false,
}; };

View File

@ -71,7 +71,6 @@ static struct spdk_pci_enum_ctx g_nvme_pci_drv = {
.cb_fn = NULL, .cb_fn = NULL,
.cb_arg = NULL, .cb_arg = NULL,
.mtx = PTHREAD_MUTEX_INITIALIZER,
.is_registered = false, .is_registered = false,
}; };

View File

@ -62,7 +62,6 @@ static struct spdk_pci_enum_ctx g_virtio_pci_drv = {
.cb_fn = NULL, .cb_fn = NULL,
.cb_arg = NULL, .cb_arg = NULL,
.mtx = PTHREAD_MUTEX_INITIALIZER,
.is_registered = false, .is_registered = false,
}; };