Commit d808a62d authored by Darek Stojaczyk's avatar Darek Stojaczyk
Browse files

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: default avatarDarek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-on: https://review.gerrithub.io/433864


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
parent 9cf7d886
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -73,7 +73,6 @@ struct spdk_pci_enum_ctx {
	struct rte_pci_driver	driver;
	spdk_pci_enum_cb	cb_fn;
	void			*cb_arg;
	pthread_mutex_t		mtx;
	bool			is_registered;
};

+8 −6
Original line number Diff line number Diff line
@@ -40,6 +40,8 @@
#define PCI_CFG_SIZE		256
#define PCI_EXT_CAP_ID_SN	0x03

static pthread_mutex_t g_pci_mutex = PTHREAD_MUTEX_INITIALIZER;

int
spdk_pci_device_init(struct rte_pci_driver *driver,
		     struct rte_pci_device *device)
@@ -122,7 +124,7 @@ spdk_pci_device_attach(struct spdk_pci_enum_ctx *ctx,
	addr.function = pci_address->func;
#endif

	pthread_mutex_lock(&ctx->mtx);
	pthread_mutex_lock(&g_pci_mutex);

	if (!ctx->is_registered) {
		ctx->is_registered = true;
@@ -147,13 +149,13 @@ spdk_pci_device_attach(struct spdk_pci_enum_ctx *ctx,
#endif
		ctx->cb_arg = NULL;
		ctx->cb_fn = NULL;
		pthread_mutex_unlock(&ctx->mtx);
		pthread_mutex_unlock(&g_pci_mutex);
		return -1;
	}

	ctx->cb_arg = NULL;
	ctx->cb_fn = NULL;
	pthread_mutex_unlock(&ctx->mtx);
	pthread_mutex_unlock(&g_pci_mutex);

	return 0;
}
@@ -167,7 +169,7 @@ spdk_pci_enumerate(struct spdk_pci_enum_ctx *ctx,
		   spdk_pci_enum_cb enum_cb,
		   void *enum_ctx)
{
	pthread_mutex_lock(&ctx->mtx);
	pthread_mutex_lock(&g_pci_mutex);

	if (!ctx->is_registered) {
		ctx->is_registered = true;
@@ -190,13 +192,13 @@ spdk_pci_enumerate(struct spdk_pci_enum_ctx *ctx,
#endif
		ctx->cb_arg = NULL;
		ctx->cb_fn = NULL;
		pthread_mutex_unlock(&ctx->mtx);
		pthread_mutex_unlock(&g_pci_mutex);
		return -1;
	}

	ctx->cb_arg = NULL;
	ctx->cb_fn = NULL;
	pthread_mutex_unlock(&ctx->mtx);
	pthread_mutex_unlock(&g_pci_mutex);

	return 0;
}
+0 −1
Original line number Diff line number Diff line
@@ -105,7 +105,6 @@ static struct spdk_pci_enum_ctx g_ioat_pci_drv = {

	.cb_fn = NULL,
	.cb_arg = NULL,
	.mtx = PTHREAD_MUTEX_INITIALIZER,
	.is_registered = false,
};

+0 −1
Original line number Diff line number Diff line
@@ -71,7 +71,6 @@ static struct spdk_pci_enum_ctx g_nvme_pci_drv = {

	.cb_fn = NULL,
	.cb_arg = NULL,
	.mtx = PTHREAD_MUTEX_INITIALIZER,
	.is_registered = false,
};

+0 −1
Original line number Diff line number Diff line
@@ -62,7 +62,6 @@ static struct spdk_pci_enum_ctx g_virtio_pci_drv = {

	.cb_fn = NULL,
	.cb_arg = NULL,
	.mtx = PTHREAD_MUTEX_INITIALIZER,
	.is_registered = false,
};