The connection uniqueness check is performed before a new connection
is added to the target list. This patch protects both actions by a
mutex such that simultaneous writes from two different threads into the
"add_target" variable do not result in duplicate connections.

Signed-off-by: Bart Van Assche <[email protected]>
Cc: Sagi Grimberg <[email protected]>
Cc: Sebastian Riemer <[email protected]>
Cc: Roland Dreier <[email protected]>
---
 drivers/infiniband/ulp/srp/ib_srp.c | 12 +++++++++---
 drivers/infiniband/ulp/srp/ib_srp.h |  1 +
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/infiniband/ulp/srp/ib_srp.c 
b/drivers/infiniband/ulp/srp/ib_srp.c
index b3ca4d6..7d9413e 100644
--- a/drivers/infiniband/ulp/srp/ib_srp.c
+++ b/drivers/infiniband/ulp/srp/ib_srp.c
@@ -2617,6 +2617,8 @@ static ssize_t srp_create_target(struct device *dev,
        target->tl_retry_count  = 7;
        target->queue_size      = SRP_DEFAULT_QUEUE_SIZE;
 
+       mutex_lock(&host->add_target_mutex);
+
        ret = srp_parse_options(buf, target);
        if (ret)
                goto err;
@@ -2683,7 +2685,11 @@ static ssize_t srp_create_target(struct device *dev,
                     be64_to_cpu(target->service_id),
                     target->path.sgid.raw, target->path.dgid.raw);
 
-       return count;
+       ret = count;
+
+out:
+       mutex_unlock(&host->add_target_mutex);
+       return ret;
 
 err_disconnect:
        srp_disconnect_target(target);
@@ -2699,8 +2705,7 @@ err_free_mem:
 
 err:
        scsi_host_put(target_host);
-
-       return ret;
+       goto out;
 }
 
 static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
@@ -2736,6 +2741,7 @@ static struct srp_host *srp_add_port(struct srp_device 
*device, u8 port)
        INIT_LIST_HEAD(&host->target_list);
        spin_lock_init(&host->target_lock);
        init_completion(&host->released);
+       mutex_init(&host->add_target_mutex);
        host->srp_dev = device;
        host->port = port;
 
diff --git a/drivers/infiniband/ulp/srp/ib_srp.h 
b/drivers/infiniband/ulp/srp/ib_srp.h
index 5756810..aad27b7 100644
--- a/drivers/infiniband/ulp/srp/ib_srp.h
+++ b/drivers/infiniband/ulp/srp/ib_srp.h
@@ -105,6 +105,7 @@ struct srp_host {
        spinlock_t              target_lock;
        struct completion       released;
        struct list_head        list;
+       struct mutex            add_target_mutex;
 };
 
 struct srp_request {
-- 
1.8.4.5

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to