return 0;
 }
 
+/**
+ * iscsi_alloc_mgmt_task - allocate and setup a mgmt task.
+ * @conn: iscsi conn that the task will be sent on.
+ * @hdr: iscsi pdu that will be sent.
+ * @data: buffer for data segment if needed.
+ * @data_size: length of data in bytes.
+ */
 static struct iscsi_task *
-__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
+iscsi_alloc_mgmt_task(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
                      char *data, uint32_t data_size)
 {
        struct iscsi_session *session = conn->session;
-       struct iscsi_host *ihost = shost_priv(session->host);
        uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
        struct iscsi_task *task;
        itt_t itt;
                                                   task->conn->session->age);
        }
 
-       if (unlikely(READ_ONCE(conn->ping_task) == INVALID_SCSI_TASK))
-               WRITE_ONCE(conn->ping_task, task);
+       return task;
+
+free_task:
+       iscsi_put_task(task);
+       return NULL;
+}
+
+/**
+ * iscsi_send_mgmt_task - Send task created with iscsi_alloc_mgmt_task.
+ * @task: iscsi task to send.
+ *
+ * On failure this returns a non-zero error code, and the driver must free
+ * the task with iscsi_put_task;
+ */
+static int iscsi_send_mgmt_task(struct iscsi_task *task)
+{
+       struct iscsi_conn *conn = task->conn;
+       struct iscsi_session *session = conn->session;
+       struct iscsi_host *ihost = shost_priv(conn->session->host);
+       int rc = 0;
 
        if (!ihost->workq) {
-               if (iscsi_prep_mgmt_task(conn, task))
-                       goto free_task;
+               rc = iscsi_prep_mgmt_task(conn, task);
+               if (rc)
+                       return rc;
 
-               if (session->tt->xmit_task(task))
-                       goto free_task;
+               rc = session->tt->xmit_task(task);
+               if (rc)
+                       return rc;
        } else {
                list_add_tail(&task->running, &conn->mgmtqueue);
                iscsi_conn_queue_xmit(conn);
        }
 
-       return task;
+       return 0;
+}
 
-free_task:
-       /* regular RX path uses back_lock */
-       spin_lock(&session->back_lock);
-       __iscsi_put_task(task);
-       spin_unlock(&session->back_lock);
-       return NULL;
+static int __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
+                                char *data, uint32_t data_size)
+{
+       struct iscsi_task *task;
+       int rc;
+
+       task = iscsi_alloc_mgmt_task(conn, hdr, data, data_size);
+       if (!task)
+               return -ENOMEM;
+
+       rc = iscsi_send_mgmt_task(task);
+       if (rc)
+               iscsi_put_task(task);
+       return rc;
 }
 
 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
        int err = 0;
 
        spin_lock_bh(&session->frwd_lock);
-       if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
+       if (__iscsi_conn_send_pdu(conn, hdr, data, data_size))
                err = -EPERM;
        spin_unlock_bh(&session->frwd_lock);
        return err;
        if (!rhdr) {
                if (READ_ONCE(conn->ping_task))
                        return -EINVAL;
-               WRITE_ONCE(conn->ping_task, INVALID_SCSI_TASK);
        }
 
        memset(&hdr, 0, sizeof(struct iscsi_nopout));
        } else
                hdr.ttt = RESERVED_ITT;
 
-       task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
-       if (!task) {
+       task = iscsi_alloc_mgmt_task(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
+       if (!task)
+               return -ENOMEM;
+
+       if (!rhdr)
+               WRITE_ONCE(conn->ping_task, task);
+
+       if (iscsi_send_mgmt_task(task)) {
                if (!rhdr)
                        WRITE_ONCE(conn->ping_task, NULL);
+               iscsi_put_task(task);
+
                iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
                return -EIO;
        } else if (!rhdr) {
        __must_hold(&session->frwd_lock)
 {
        struct iscsi_session *session = conn->session;
-       struct iscsi_task *task;
 
-       task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
-                                     NULL, 0);
-       if (!task) {
+       if (__iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0)) {
                spin_unlock_bh(&session->frwd_lock);
                iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
                iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);