Skip to content

Commit fe0a8a9

Browse files
gonzoleemanmartinkpetersen
authored andcommitted
scsi: libiscsi: Fix NOP race condition
iSCSI NOPs are sometimes "lost", mistakenly sent to the user-land iscsid daemon instead of handled in the kernel, as they should be, resulting in a message from the daemon like: iscsid: Got nop in, but kernel supports nop handling. This can occur because of the new forward- and back-locks, and the fact that an iSCSI NOP response can occur before processing of the NOP send is complete. This can result in "conn->ping_task" being NULL in iscsi_nop_out_rsp(), when the pointer is actually in the process of being set. To work around this, we add a new state to the "ping_task" pointer. In addition to NULL (not assigned) and a pointer (assigned), we add the state "being set", which is signaled with an INVALID pointer (using "-1"). Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Mike Christie <[email protected]> Signed-off-by: Lee Duncan <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent 2e6f11a commit fe0a8a9

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

drivers/scsi/libiscsi.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ static void iscsi_complete_task(struct iscsi_task *task, int state)
533533
if (conn->task == task)
534534
conn->task = NULL;
535535

536-
if (conn->ping_task == task)
537-
conn->ping_task = NULL;
536+
if (READ_ONCE(conn->ping_task) == task)
537+
WRITE_ONCE(conn->ping_task, NULL);
538538

539539
/* release get from queueing */
540540
__iscsi_put_task(task);
@@ -738,6 +738,9 @@ __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
738738
task->conn->session->age);
739739
}
740740

741+
if (unlikely(READ_ONCE(conn->ping_task) == INVALID_SCSI_TASK))
742+
WRITE_ONCE(conn->ping_task, task);
743+
741744
if (!ihost->workq) {
742745
if (iscsi_prep_mgmt_task(conn, task))
743746
goto free_task;
@@ -941,8 +944,11 @@ static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
941944
struct iscsi_nopout hdr;
942945
struct iscsi_task *task;
943946

944-
if (!rhdr && conn->ping_task)
945-
return -EINVAL;
947+
if (!rhdr) {
948+
if (READ_ONCE(conn->ping_task))
949+
return -EINVAL;
950+
WRITE_ONCE(conn->ping_task, INVALID_SCSI_TASK);
951+
}
946952

947953
memset(&hdr, 0, sizeof(struct iscsi_nopout));
948954
hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
@@ -957,11 +963,12 @@ static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
957963

958964
task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
959965
if (!task) {
966+
if (!rhdr)
967+
WRITE_ONCE(conn->ping_task, NULL);
960968
iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
961969
return -EIO;
962970
} else if (!rhdr) {
963971
/* only track our nops */
964-
conn->ping_task = task;
965972
conn->last_ping = jiffies;
966973
}
967974

@@ -984,7 +991,7 @@ static int iscsi_nop_out_rsp(struct iscsi_task *task,
984991
struct iscsi_conn *conn = task->conn;
985992
int rc = 0;
986993

987-
if (conn->ping_task != task) {
994+
if (READ_ONCE(conn->ping_task) != task) {
988995
/*
989996
* If this is not in response to one of our
990997
* nops then it must be from userspace.
@@ -1923,7 +1930,7 @@ static void iscsi_start_tx(struct iscsi_conn *conn)
19231930
*/
19241931
static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
19251932
{
1926-
if (conn->ping_task &&
1933+
if (READ_ONCE(conn->ping_task) &&
19271934
time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
19281935
(conn->ping_timeout * HZ), jiffies))
19291936
return 1;
@@ -2058,7 +2065,7 @@ enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
20582065
* Checking the transport already or nop from a cmd timeout still
20592066
* running
20602067
*/
2061-
if (conn->ping_task) {
2068+
if (READ_ONCE(conn->ping_task)) {
20622069
task->have_checked_conn = true;
20632070
rc = BLK_EH_RESET_TIMER;
20642071
goto done;

include/scsi/libiscsi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ struct iscsi_task {
132132
void *dd_data; /* driver/transport data */
133133
};
134134

135+
/* invalid scsi_task pointer */
136+
#define INVALID_SCSI_TASK (struct iscsi_task *)-1l
137+
135138
static inline int iscsi_task_has_unsol_data(struct iscsi_task *task)
136139
{
137140
return task->unsol_r2t.data_length > task->unsol_r2t.sent;

0 commit comments

Comments
 (0)