From 3178ad6476c63ae5b3bd00761aa755438c7a8706 Mon Sep 17 00:00:00 2001 From: Jim Harris Date: Thu, 2 Nov 2017 16:54:51 -0700 Subject: [PATCH] env: return fd from spdk_pci_device_claim() This allows users of this interface to then close the fd when they want to release the claim. This prepares for calling spdk_pci_device_claim() in the nvme driver to cover not just the bdev_nvme driver but all of our nvme example and test applications as well. We'll want the fd returned so that we can properly close it during detach (including hotplug) use cases. Signed-off-by: Jim Harris Change-Id: I8b149cc4e778ba31c0e7045b858c8a1561b6b7af Reviewed-on: https://review.gerrithub.io/385523 Reviewed-by: Dariusz Stojaczyk Tested-by: SPDK Automated Test System Reviewed-by: Daniel Verkamp Reviewed-by: Ben Walker --- include/spdk/env.h | 14 ++++++++++++++ lib/bdev/nvme/bdev_nvme.c | 2 +- lib/copy/ioat/copy_engine_ioat.c | 2 +- lib/env_dpdk/pci.c | 2 +- test/lib/env/pci/pci_ut.c | 2 +- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/spdk/env.h b/include/spdk/env.h index 830825687e..921e32329d 100644 --- a/include/spdk/env.h +++ b/include/spdk/env.h @@ -345,6 +345,20 @@ struct spdk_pci_id spdk_pci_device_get_id(struct spdk_pci_device *dev); int spdk_pci_device_get_socket_id(struct spdk_pci_device *dev); int spdk_pci_device_get_serial_number(struct spdk_pci_device *dev, char *sn, size_t len); + +/** + * Claim a PCI device for exclusive SPDK userspace access. + * + * Uses F_SETLK on a shared memory file with the PCI address embedded in its name. + * As long as this file remains open with the lock acquired, other processes will + * not be able to successfully call this function on the same PCI device. + * + * \param pci_addr PCI address of the device to claim + * + * \return -1 if the device has already been claimed, an fd otherwise. This fd + * should be closed when the application no longer needs access to the + * PCI device (including when it is hot removed). + */ int spdk_pci_device_claim(const struct spdk_pci_addr *pci_addr); void spdk_pci_device_detach(struct spdk_pci_device *device); diff --git a/lib/bdev/nvme/bdev_nvme.c b/lib/bdev/nvme/bdev_nvme.c index 8d099ea5bf..d4fa82a541 100644 --- a/lib/bdev/nvme/bdev_nvme.c +++ b/lib/bdev/nvme/bdev_nvme.c @@ -742,7 +742,7 @@ probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid, return false; } - if (spdk_pci_device_claim(&pci_addr) != 0) { + if (spdk_pci_device_claim(&pci_addr) < 0) { return false; } } diff --git a/lib/copy/ioat/copy_engine_ioat.c b/lib/copy/ioat/copy_engine_ioat.c index e0d0e8aebb..0e85c9033b 100644 --- a/lib/copy/ioat/copy_engine_ioat.c +++ b/lib/copy/ioat/copy_engine_ioat.c @@ -248,7 +248,7 @@ probe_cb(void *cb_ctx, struct spdk_pci_device *pci_dev) } /* Claim the device in case conflict with other process */ - if (spdk_pci_device_claim(&pci_addr) != 0) { + if (spdk_pci_device_claim(&pci_addr) < 0) { return false; } diff --git a/lib/env_dpdk/pci.c b/lib/env_dpdk/pci.c index df2f289f8a..144bd77983 100644 --- a/lib/env_dpdk/pci.c +++ b/lib/env_dpdk/pci.c @@ -495,7 +495,7 @@ spdk_pci_device_claim(const struct spdk_pci_addr *pci_addr) *(int *)dev_map = (int)getpid(); munmap(dev_map, sizeof(int)); /* Keep dev_fd open to maintain the lock. */ - return 0; + return dev_fd; } #endif /* __linux__ */ diff --git a/test/lib/env/pci/pci_ut.c b/test/lib/env/pci/pci_ut.c index 48a98c4626..873f67aed4 100644 --- a/test/lib/env/pci/pci_ut.c +++ b/test/lib/env/pci/pci_ut.c @@ -51,7 +51,7 @@ pci_test(void) pci_addr.func = 1; rc = spdk_pci_device_claim(&pci_addr); - CU_ASSERT(rc == 0); + CU_ASSERT(rc >= 0); childPid = fork(); CU_ASSERT(childPid >= 0);