Fix possible hangs and race conditions during login
v2 cover Fix possible hangs and race conditions during login
PATCH 1 fixes a possible hang when during a login operation the initiator stops sending data to the target and the login_work doesn't get scheduled.
PATCH 3 fixes a bug in iscsi_target_locate_portal(), in the error code path the login semaphore may end up being released even if no one called down() against it, this may allow multiple threads to access the semaphore-protected code.
v2: the login timeout of 15 seconds now is relative to the entire
login operation and not per-PDU.
Maurizio Lombardi (3):
target: iscsi: fix hang in the iSCSI login code
target: iscsi: remove unused transport_timer
target: iscsi: prevent login threads from racing between each other
drivers/target/iscsi/iscsi_target.c | 2 - drivers/target/iscsi/iscsi_target_login.c | 63 ++------------------ drivers/target/iscsi/iscsi_target_nego.c | 72 +++++++++++++---------- drivers/target/iscsi/iscsi_target_util.c | 51 ++++++++++++++++ drivers/target/iscsi/iscsi_target_util.h | 4 ++ include/target/iscsi/iscsi_target_core.h | 7 ++- 6 files changed, 104 insertions(+), 95 deletions(-)
Signature · 1 line
2.31.1
PATCH v2 1/3 target: iscsi: fix hang in the iSCSI login code
If the initiator suddenly stops sending data during a login while keeping the TCP connection open, the login_work won't be scheduled and will never release the login semaphore;
concurrent login operations will therefore get stuck and fail.
The bug is due to the inability of the login timeout code to properly handle this particular case.
Fix the problem by replacing the old per-NP login timer with a new per-connection timer.
The timer is started when an initiator connects to the target; if it expires, it sends a SIGINT signal to the thread pointed at by the conn->login_kworker pointer.
conn->login_kworker is set by calling the iscsit_set_login_timer_kworker() helper, initially it will point to the np thread;
When the login operation's control is in the process
of being passed from the NP-thread to login_work, the conn->login_worker pointer is set to NULL.
Finally, login_kworker will be changed to point to the worker thread executing the login_work job.
If conn->login_kworker is NULL when the timer expires, it means that the login operation hasn't been completed yet but login_work isn't running, in this case the timer will mark the login process as failed and will schedule login_work so the latter will be
forced to free the resources it holds.
--- drivers/target/iscsi/iscsi_target.c | 2 - drivers/target/iscsi/iscsi_target_login.c | 63 ++------------------- drivers/target/iscsi/iscsi_target_nego.c | 68 ++++++++++++----------- drivers/target/iscsi/iscsi_target_util.c | 51 +++++++++++++++++ drivers/target/iscsi/iscsi_target_util.h | 4 ++ include/target/iscsi/iscsi_target_core.h | 6 +- 6 files changed, 101 insertions(+), 93 deletions(-)
drivers/target/iscsi/iscsi_target.c +0 -2
@@ -363,8 +363,6 @@ struct iscsi_np *iscsit_add_np(init_completion (&np->np_restart_comp);INIT_LIST_HEAD (&np->np_list); -timer_setup (&np->np_login_timer, iscsi_handle_login_thread_timeout,0 ); - ret =iscsi_target_setup_login_socket (np, sockaddr);if (ret !=0 ) {kfree (np);
drivers/target/iscsi/iscsi_target_login.c +5 -58
103 lines not on this page · raw message
drivers/target/iscsi/iscsi_target_nego.c +37 -31
120 lines not on this page · raw message
drivers/target/iscsi/iscsi_target_util.c +51 -0
57 lines not on this page · raw message
drivers/target/iscsi/iscsi_target_util.h +4 -0
@@ -56,6 +56,10 @@ extern void iscsit_handle_nopin_timeout(struct timer_list *t);extern void __iscsit_start_nopin_timer (struct iscsit_conn *);extern void iscsit_start_nopin_timer (struct iscsit_conn *);extern void iscsit_stop_nopin_timer (struct iscsit_conn *); +extern void iscsit_login_timeout (struct timer_list *t); +extern void iscsit_start_login_timer (struct iscsit_conn *,struct task_struct *kthr); +extern void iscsit_stop_login_timer (struct iscsit_conn *); +extern int iscsit_set_login_timer_kworker (struct iscsit_conn *,struct task_struct *kthr);extern int iscsit_send_tx_data (struct iscsit_cmd *,struct iscsit_conn *,int );extern int iscsit_fe_sendpage_sg (struct iscsit_cmd *,struct iscsit_conn *);extern int iscsit_tx_login_rsp (struct iscsit_conn *,u8 ,u8 );
include/target/iscsi/iscsi_target_core.h +4 -2
@@ -562,12 +562,14 @@ struct iscsit_conn {#define LOGIN_FLAGS_READ_ACTIVE 2#define LOGIN_FLAGS_WRITE_ACTIVE 3#define LOGIN_FLAGS_CLOSED 4 +#define LOGIN_FLAGS_WORKER_RUNNING 5unsigned long login_flags;struct delayed_work login_work;struct iscsi_login *login;struct timer_list nopin_timer;struct timer_list nopin_response_timer;struct timer_list transport_timer; +struct timer_list login_timer;struct task_struct *login_kworker;/* Spinlock used for add/deleting cmd's from conn_cmd_list */ spinlock_t cmd_lock;
@@ -576,6 +578,8 @@ struct iscsit_conn {spinlock_t nopin_timer_lock;spinlock_t response_queue_lock;spinlock_t state_lock; +spinlock_t login_timer_lock; +spinlock_t login_worker_lock;/* libcrypto RX and TX contexts for crc32c */ struct ahash_request *conn_rx_hash;struct ahash_request *conn_tx_hash;
@@ -791,7 +795,6 @@ struct iscsi_np {enum np_thread_state_table np_thread_state;bool enabled;atomic_t np_reset_count; -enum iscsi_timer_flags_table np_login_timer_flags;u32 np_exports;enum np_flags_table np_flags;spinlock_t np_thread_lock;
@@ -799,7 +802,6 @@ struct iscsi_np {struct socket *np_socket;struct sockaddr_storage np_sockaddr;struct task_struct *np_thread; -struct timer_list np_login_timer;void *np_context;struct iscsit_transport *np_transport;struct list_head np_list;
Signature · 1 line
2.31.1
Re: [PATCH V2 1/3] target: iscsi: fix hang in the iSCSI login code
Quoted 7 lines · Maurizio Lombardi
static void
iscsi_target_do_login_rx(struct work_struct *work){drivers/target/iscsi/iscsi_target_nego.c +0
-0struct iscsit_conn *conn = container_of(work,@@ -562,12 +543,15 @@ static void iscsi_target_do_login_rx(struct work_struct *work)struct iscsi_np *np = login->np;struct iscsi_portal_group *tpg = conn->tpg;struct iscsi_tpg_np *tpg_np = conn->tpg_np;
drivers/target/iscsi/iscsi_target_nego.c static void iscsi_target_do_login_rx(struct work_struct *work) · line 554
Quoted 10 lines
drivers/target/iscsi/iscsi_target_nego.c +4
-1-struct conn_timeout timeout;int rc, zero_tsih = login->zero_tsih;bool state;pr_debug ("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n" , conn, current->comm, current->pid); + +spin_lock (&conn->login_worker_lock); +set_bit (LOGIN_FLAGS_WORKER_RUNNING , &conn->login_flags); +spin_unlock (&conn->login_worker_lock);
I think the locking bh use is reversed. You are in thread context here so you need to do spin_lock/unlock_bh to prevent a softirq on the same cpu from interrupting us and running your timeout function which would try to grab the login_worker_lock.
iscsit_login_timeout is run from softirq context so you wouldn't need to do the _bh calls from it.
Quoted 30 lines
drivers/target/iscsi/iscsi_target_nego.c +6
-9/* * If iscsi_target_do_login_rx() has been invoked by ->sk_data_ready() * before initial PDU processing in iscsi_target_start_negotiation() @@ -597,19 +581,16 @@ static void iscsi_target_do_login_rx(struct work_struct *work) goto err; } - conn->login_kworker = current;allow_signal (SIGINT ); - - timeout.conn = conn; -timer_setup_on_stack (&timeout.timer, iscsi_target_login_timeout,0 ); -mod_timer (&timeout.timer, jiffies +TA_LOGIN_TIMEOUT *HZ ); -pr_debug ("Starting login timer for %s/%d\n" , current->comm, current->pid); + rc =iscsit_set_login_timer_kworker (conn, current); +if (rc <0 ) { +/* The login timer has already expired */ +pr_debug ("iscsi_target_do_login_rx, login failed\n" ); + goto err; + } rc = conn->conn_transport->iscsit_get_login_rx (conn, login); -del_timer_sync (&timeout.timer); -destroy_timer_on_stack (&timeout.timer);flush_signals (current); - conn->login_kworker =NULL ;if (rc <0 ) goto err;
drivers/target/iscsi/iscsi_target_nego.c static void iscsi_target_do_login_rx(struct work_struct *work) · line 634
Quoted 8 lines
drivers/target/iscsi/iscsi_target_nego.c +5
-0@@ -646,7 +627,16 @@ static void iscsi_target_do_login_rx(struct work_struct *work)if (iscsi_target_sk_check_and_clear (conn,LOGIN_FLAGS_WRITE_ACTIVE )) goto err; + +/* Set the login timer thread pointer to NULL to prevent the +* login process from getting stuck if the initiator +* stops sending data. +*/
Use the more common comment style we have in the target code (there was some other cases below):
/*
* Set the...
Quoted 17 lines
drivers/target/iscsi/iscsi_target_nego.c +5
-0+ rc =iscsit_set_login_timer_kworker (conn,NULL ); +if (rc <0 ) + goto err; }else if (rc ==1 ) { +iscsit_stop_login_timer (conn);cancel_delayed_work (&conn->login_work);iscsi_target_nego_release (conn);iscsi_post_login_handler (np, conn, zero_tsih);@@ -656,6 +646,7 @@ static void iscsi_target_do_login_rx(struct work_struct *work) err:iscsi_target_restore_sock_callbacks (conn); +iscsit_stop_login_timer (conn);cancel_delayed_work (&conn->login_work);iscsi_target_login_drop (conn, login);iscsit_deaccess_np (np, tpg, tpg_np);
drivers/target/iscsi/iscsi_target_nego.c int iscsi_target_start_negotiation( · line 1363
Quoted 7 lines
drivers/target/iscsi/iscsi_target_nego.c +2
-2@@ -1368,14 +1359,29 @@ int iscsi_target_start_negotiation(* and perform connection cleanup now. */ ret =iscsi_target_do_login (conn, login); -if (!ret &&iscsi_target_sk_check_and_clear (conn,LOGIN_FLAGS_INITIAL_PDU )) - ret =-1 ; +if (!ret) { +spin_lock (&conn->login_worker_lock);
Same locking comment.
drivers/target/iscsi/iscsi_target_nego.c int iscsi_target_start_negotiation( · line 1368
Quoted 5 lines
drivers/target/iscsi/iscsi_target_nego.c +5
-0+ +if (iscsi_target_sk_check_and_clear (conn,LOGIN_FLAGS_INITIAL_PDU )) + ret =-1 ; +else if (!test_bit (LOGIN_FLAGS_WORKER_RUNNING , &conn->login_flags)) { +if (iscsit_set_login_timer_kworker (conn,NULL ) <0 ) {
I think this will deadlock because iscsit_set_login_timer_kworker tries to take the login_worker_lock we took above.
Re: [PATCH V2 1/3] target: iscsi: fix hang in the iSCSI login code
Quoted 12 lines · Mike Christie
Quoted 11 lines · Maurizio Lombardi
static void
iscsi_target_do_login_rx(struct work_struct *work){drivers/target/iscsi/iscsi_target_nego.c +0
-1struct iscsit_conn *conn = container_of(work,@@ -562,12 +543,15 @@ static void iscsi_target_do_login_rx(struct work_struct *work)struct iscsi_np *np = login->np;struct iscsi_portal_group *tpg = conn->tpg;struct iscsi_tpg_np *tpg_np = conn->tpg_np; -struct conn_timeout timeout;int rc, zero_tsih = login->zero_tsih;bool state;
Quoted 9 lines
drivers/target/iscsi/iscsi_target_nego.c static void iscsi_target_do_login_rx(struct work_struct *work) · line 554
Quoted 6 lines
drivers/target/iscsi/iscsi_target_nego.c +4
-0pr_debug ("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n" , conn, current->comm, current->pid); + +spin_lock (&conn->login_worker_lock); +set_bit (LOGIN_FLAGS_WORKER_RUNNING , &conn->login_flags); +spin_unlock (&conn->login_worker_lock);I think the locking bh use is reversed.
Ignore the locking comment like here and below. I see there are 2 locks.
Quoted 8 lines
drivers/target/iscsi/iscsi_target_nego.c int iscsi_target_start_negotiation( · line 1368
Quoted 5 lines
drivers/target/iscsi/iscsi_target_nego.c +5
-0+ +if (iscsi_target_sk_check_and_clear (conn,LOGIN_FLAGS_INITIAL_PDU )) + ret =-1 ; +else if (!test_bit (LOGIN_FLAGS_WORKER_RUNNING , &conn->login_flags)) { +if (iscsit_set_login_timer_kworker (conn,NULL ) <0 ) {I think this will deadlock because
iscsit_set_login_timer_kworkertries to take thelogin_worker_lockwe took above.
PATCH v2 2/3 target: iscsi: remove unused transport_timer
In reply to Maurizio Lombardi--- include/target/iscsi/iscsi_target_core.h | 1 - 1 file changed, 1 deletion(-)
include/target/iscsi/iscsi_target_core.h +0 -1
@@ -568,7 +568,6 @@ struct iscsit_conn {struct iscsi_login *login;struct timer_list nopin_timer;struct timer_list nopin_response_timer; -struct timer_list transport_timer;struct timer_list login_timer;struct task_struct *login_kworker;/* Spinlock used for add/deleting cmd's from conn_cmd_list */
Signature · 1 line
2.31.1
Re: [PATCH V2 2/3] target: iscsi: remove unused transport_timer
Quoted 10 lines · Maurizio Lombardi
Signed-off-by Maurizio Lombardi <[email protected]>--- include/target/iscsi/iscsi_target_core.h | 1 - 1 file changed, 1 deletion(-)include/target/iscsi/iscsi_target_core.h +0
-0
include/target/iscsi/iscsi_target_core.h struct iscsit_conn { · line 573
Quoted 7 lines
include/target/iscsi/iscsi_target_core.h +0
-1@@ -568,7 +568,6 @@ struct iscsit_conn {struct iscsi_login *login;struct timer_list nopin_timer;struct timer_list nopin_response_timer; -struct timer_list transport_timer;struct timer_list login_timer;struct task_struct *login_kworker;/* Spinlock used for add/deleting cmd's from conn_cmd_list */
PATCH v2 3/3 target: iscsi: prevent login threads from racing between each other
In reply to Maurizio LombardiThe tpg->np_login_sem is a semaphore that is used to serialize the login process when multiple login threads run concurrently against the same target portal group.
The iscsi_target_locate_portal() function finds the tpg, calls iscsit_access_np() against the np_login_sem semaphore and saves the tpg pointer in conn->tpg;
If iscsi_target_locate_portal() fails, the caller will check for the conn->tpg pointer and, if it's not NULL, then it will assume that iscsi_target_locate_portal() called iscsit_access_np() on the semaphore.
Make sure that conn->tpg gets initialized only if iscsit_access_np() was successful, otherwise iscsit_deaccess_np() may end up being called against a semaphore we never took, allowing more than one thread to access the same tpg.
--- drivers/target/iscsi/iscsi_target_nego.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/target/iscsi/iscsi_target_nego.c +3 -1
@@ -1121,6 +1121,7 @@ int iscsi_target_locate_portal(iscsi_target_set_sock_callbacks (conn); login->np = np; + conn->tpg =NULL ; login_req = (struct iscsi_login_req *) login->req; payload_length =ntoh24 (login_req->dlength);
@@ -1188,7 +1189,6 @@ int iscsi_target_locate_portal(*/ sessiontype =strncmp (s_buf,DISCOVERY ,9 );if (!sessiontype) { - conn->tpg = iscsit_global->discovery_tpg;if (!login->leading_connection) goto get_target;
@@ -1205,9 +1205,11 @@ int iscsi_target_locate_portal(* Serialize access across the discovery struct iscsi_portal_group to * process login attempt. */ + conn->tpg = iscsit_global->discovery_tpg;if (iscsit_access_np (np, conn->tpg) <0 ) {iscsit_tx_login_rsp (conn,ISCSI_STATUS_CLS_TARGET_ERR ,ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE ); + conn->tpg =NULL ; ret =-1 ; goto out; }
Signature · 1 line
2.31.1
Re: [PATCH V2 3/3] target: iscsi: prevent login threads from racing between each other
Quoted 13 lines · Maurizio Lombardi
The
tpg->np_login_semis a semaphore that is used to serialize the login process when multiple login threads run concurrently against the same target portal group.The
iscsi_target_locate_portal()function finds the tpg, callsiscsit_access_np()against thenp_login_semsemaphore and saves the tpg pointer inconn->tpg;If
iscsi_target_locate_portal()fails, the caller will check for theconn->tpgpointer and, if it's not NULL, then it will assume thatiscsi_target_locate_portal()callediscsit_access_np()on the semaphore.
Quoted 6 lines
Make sure that
conn->tpggets initialized only ifiscsit_access_np()was successful, otherwiseiscsit_deaccess_np()may end up being called against a semaphore we never took, allowing more than one thread to access the same tpg.Signed-off-by Maurizio Lombardi <[email protected]>