From patchwork Fri Feb 10 01:28:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 9565781 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0FEC760572 for ; Fri, 10 Feb 2017 01:29:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 00EF228532 for ; Fri, 10 Feb 2017 01:29:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EA0542853B; Fri, 10 Feb 2017 01:29:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3703E28532 for ; Fri, 10 Feb 2017 01:29:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751429AbdBJB30 (ORCPT ); Thu, 9 Feb 2017 20:29:26 -0500 Received: from esa1.hgst.iphmx.com ([68.232.141.245]:36294 "EHLO esa1.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751368AbdBJB3Z (ORCPT ); Thu, 9 Feb 2017 20:29:25 -0500 X-IronPort-AV: E=Sophos;i="5.33,348,1477929600"; d="scan'208";a="82349477" Received: from unknown (HELO milsmgep14.sandisk.com) ([63.163.107.225]) by ob1.hgst.iphmx.com with ESMTP; 10 Feb 2017 09:28:59 +0800 Received: from MILHUBIP03.sdcorp.global.sandisk.com (Unknown_Domain [10.201.67.162]) (using TLS with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by (Symantec Messaging Gateway) with SMTP id 71.64.18895.B571D985; Thu, 9 Feb 2017 17:28:59 -0800 (PST) Received: from milsmgip11.sandisk.com (10.177.9.6) by MILHUBIP03.sdcorp.global.sandisk.com (10.177.9.96) with Microsoft SMTP Server id 14.3.319.2; Thu, 9 Feb 2017 17:28:55 -0800 X-AuditID: 0ac94371-0fadb980000049cf-08-589d175b72d9 Received: from exp-402881.sdcorp.global.sandisk.com ( [10.177.9.6]) by (Symantec Messaging Gateway) with SMTP id 15.9F.11415.7571D985; Thu, 9 Feb 2017 17:28:55 -0800 (PST) From: Bart Van Assche To: "Nicholas A . Bellinger" CC: , Bart Van Assche , Christoph Hellwig , Sagi Grimberg , Andy Grover Subject: [PATCH v5 10/22] target: Simplify session shutdown code Date: Thu, 9 Feb 2017 17:28:40 -0800 Message-ID: <20170210012852.15735-11-bart.vanassche@sandisk.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170210012852.15735-1-bart.vanassche@sandisk.com> References: <20170210012852.15735-1-bart.vanassche@sandisk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrALMWRmVeSWpSXmKPExsXCddJ5kW60+NwIg6UnRC32z3rGZLFy9VEm i7bVZxgtbrQ9YLNoXfqWyYHV4/72I0weu282sHk8m36YyeP9vqtsHp83yQWwRnHZpKTmZJal FunbJXBl/JjawlLQ6Fxx4+0DpgbGKxZdjJwcEgImEg9e3mLpYuTiEBJYyiTxrGUBK4SzlVHi fesqZpiqVb3HoKo2MkpMuDaZFSTBJmAk8e39TBYQWwTIXt2zjAnEZhbYxSjx65MPiC0s4CDR 2vMdbBCLgIrEw7WXGEFsXqD4pA09LBAL5CV2tV0Em8kJFH81+zqQzQG0zF5ixy4DkL0SApNY JbqfX2OD6BWUODnzCQvELgmJgy9egM0XElCXOLlkPtMERqFZSMpmISlbwMi0ilEsNzOnODc9 tcDQRK84MS8lszhbLzk/dxMjJNwLdzC+vu19iFGAg1GJh3dC1ZwIIdbEsuLK3EOMEhzMSiK8 9/nmRgjxpiRWVqUW5ccXleakFh9ilOZgURLnzZKdGiEkkJ5YkpqdmlqQWgSTZeLglGpgnOcd KrdJ/t+TneovhHpbxDa9VHGr/zz/dfvCjZNY2p+UZxof2sR+ZqONSEyuii3HTAErmSW5XOn7 F2yYfH4i68N1T6pCF3+3FniZIXBw+c0egYnHex7dUvu6dcfTVWd3pJSv9g3b7DQz+MqO/V1V L9TdF878sDAg0jfig8n9X+lzz3BY3W2sXKHEUpyRaKjFXFScCAAt6NRBcwIAAA== X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrPJMWRmVeSWpSXmKPExsXCtZGTTTdcfG6EwYpWGYv9s54xWRz82cZo sXL1USaLttVnGC1utD1gs2hd+pbJgc3j/vYjTB67bzaweTybfpjJ4/2+q2we09acZ/L4vEku gC2KyyYlNSezLLVI3y6BK+PH1BaWgkbnihtvHzA1MF6x6GLk5JAQMJFY1XuMpYuRi0NIYD2j xNXFZ1lBEmwCRhLf3s9kAbFFgOzVPcuYQGxmgT2MEgsXKoHYwgIOEq0935lBbBYBVYnrX1aw g9i8QPGHm/cwQiyQl9jVdhFsJidQ/NXs60A2B9Aye4kduwwmMHIvYGRYxSiWm5lTnJueWWBo qFecmJeSWZytl5yfu4kREiSROxifTjQ/xMjEwSnVwHh4mbhJYWXOgdPvnf68Xdqr1KD84KTk kUv7Ayu2eh3vOcnZuexkwY+Uc0f3ta2a9Hfmwp4liU/fLpdQUf28Zv/lmPgTucunrzV/+/3I RxOlX0++yuQ+K/258N6aX78LWjlev5yxu/HCuUw5zkXH/EUld7bZbWFdlGrA8nQJa4NGwrUs k4BlcoWdSizFGYmGWsxFxYkARKzPP8IBAAA= MIME-Version: 1.0 Sender: target-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: target-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Target drivers must call target_sess_cmd_list_set_waiting() and target_wait_for_sess_cmds() before freeing a session. Instead of setting a flag in each pending command from the former function and waiting in the latter function on a per-command completion, only set a per-session flag in the former function and wait on a per-session completion in the latter function. Note: this patch changes the behavior from aborting outstanding commands back to waiting for command completion. This makes the session shutdown code simpler. This change is safe because once a SCSI initiator system has submitted a command a target system is always allowed to execute it to completion. See also commit 0f4a943168f3 ("target: Fix remote-port TMR ABORT + se_cmd fabric stop"). This patch is based on the following two patches: * Bart Van Assche, target: Simplify session shutdown code, February 19, 2015 (https://github.com/bvanassche/linux/commit/8df5463d7d7619f2f1b70cfe5172eaef0aa52815). * Christoph Hellwig, target: Rework session shutdown code, December 7, 2015 (http://thread.gmane.org/gmane.linux.scsi.target.devel/10695). Signed-off-by: Bart Van Assche Reviewed-by: Hannes Reinecke Cc: Christoph Hellwig Cc: Sagi Grimberg Cc: Andy Grover --- drivers/target/target_core_tmr.c | 5 +- drivers/target/target_core_transport.c | 94 ++++++++++------------------------ include/target/target_core_base.h | 3 +- 3 files changed, 30 insertions(+), 72 deletions(-) diff --git a/drivers/target/target_core_tmr.c b/drivers/target/target_core_tmr.c index 1d32088414b0..7b8e2a33bdff 100644 --- a/drivers/target/target_core_tmr.c +++ b/drivers/target/target_core_tmr.c @@ -114,7 +114,7 @@ static bool __target_check_io_state(struct se_cmd *se_cmd, spin_unlock(&se_cmd->t_state_lock); return false; } - if (sess->sess_tearing_down || se_cmd->cmd_wait_set) { + if (sess->sess_tearing_down) { pr_debug("Attempted to abort io tag: %llu already shutdown," " skipping\n", se_cmd->tag); spin_unlock(&se_cmd->t_state_lock); @@ -167,7 +167,6 @@ void core_tmr_abort_task( continue; se_cmd->send_abort_response = false; - list_del_init(&se_cmd->se_cmd_list); spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); while (!wait_for_completion_timeout(&se_cmd->complete, 180 * HZ)) @@ -240,7 +239,7 @@ static void core_tmr_drain_tmr_list( spin_unlock(&sess->sess_cmd_lock); continue; } - if (sess->sess_tearing_down || cmd->cmd_wait_set) { + if (sess->sess_tearing_down) { spin_unlock(&cmd->t_state_lock); spin_unlock(&sess->sess_cmd_lock); continue; diff --git a/drivers/target/target_core_transport.c b/drivers/target/target_core_transport.c index dd30a8c098aa..0eaf69db4d33 100644 --- a/drivers/target/target_core_transport.c +++ b/drivers/target/target_core_transport.c @@ -237,8 +237,8 @@ struct se_session *transport_init_session(enum target_prot_op sup_prot_ops) INIT_LIST_HEAD(&se_sess->sess_list); INIT_LIST_HEAD(&se_sess->sess_acl_list); INIT_LIST_HEAD(&se_sess->sess_cmd_list); - INIT_LIST_HEAD(&se_sess->sess_wait_list); spin_lock_init(&se_sess->sess_cmd_lock); + init_waitqueue_head(&se_sess->cmd_list_wq); se_sess->sup_prot_ops = sup_prot_ops; return se_sess; @@ -2541,7 +2541,12 @@ int transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks) if (cmd->se_lun) transport_lun_remove_cmd(cmd); } - if (aborted) { + /* + * Since the iSCSI and iSER targets driver assume that a SCSI command + * can be freed once transport_generic_free_cmd() returns, wait here + * for cmd->complete. + */ + if (cmd->transport_state & CMD_T_ABORTED) { pr_debug("Detected CMD_T_ABORTED for ITT: %llu\n", cmd->tag); wait_for_completion(&cmd->complete); } @@ -2603,25 +2608,16 @@ static void target_release_cmd_kref(struct kref *kref) struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref); struct se_session *se_sess = se_cmd->se_sess; unsigned long flags; - bool fabric_stop; WARN_ON_ONCE(atomic_read(&se_cmd->tgt_ref.refcount) != 0); if (se_sess) { spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); - - spin_lock(&se_cmd->t_state_lock); - fabric_stop = (se_cmd->transport_state & CMD_T_FABRIC_STOP) && - (se_cmd->transport_state & CMD_T_ABORTED); - spin_unlock(&se_cmd->t_state_lock); - - if (se_cmd->cmd_wait_set || fabric_stop) { - list_del_init(&se_cmd->se_cmd_list); - spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); - target_free_cmd_mem(se_cmd); - return; + if (likely(!list_empty(&se_cmd->se_cmd_list))) { + list_del(&se_cmd->se_cmd_list); + if (list_empty(&se_sess->sess_cmd_list)) + wake_up(&se_sess->cmd_list_wq); } - list_del_init(&se_cmd->se_cmd_list); spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); } @@ -2739,71 +2735,35 @@ void target_show_cmd(const char *ctxt, struct se_cmd *cmd) */ void target_sess_cmd_list_set_waiting(struct se_session *se_sess) { - struct se_cmd *se_cmd, *tmp_cmd; unsigned long flags; - int rc; spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); - if (se_sess->sess_tearing_down) { - spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); - return; - } se_sess->sess_tearing_down = 1; - list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list); - - list_for_each_entry_safe(se_cmd, tmp_cmd, - &se_sess->sess_wait_list, se_cmd_list) { - rc = kref_get_unless_zero(&se_cmd->cmd_kref); - if (rc) { - se_cmd->cmd_wait_set = 1; - spin_lock(&se_cmd->t_state_lock); - se_cmd->transport_state |= CMD_T_FABRIC_STOP; - spin_unlock(&se_cmd->t_state_lock); - } else - list_del_init(&se_cmd->se_cmd_list); - } - spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); } EXPORT_SYMBOL(target_sess_cmd_list_set_waiting); -/* target_wait_for_sess_cmds - Wait for outstanding descriptors +/** + * target_wait_for_sess_cmds - Wait for outstanding commands * @se_sess: session to wait for active I/O */ void target_wait_for_sess_cmds(struct se_session *se_sess) { - struct se_cmd *se_cmd, *tmp_cmd; - unsigned long flags; - bool tas; - - list_for_each_entry_safe(se_cmd, tmp_cmd, - &se_sess->sess_wait_list, se_cmd_list) { - pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:" - " %d\n", se_cmd, se_cmd->t_state, - se_cmd->se_tfo->get_cmd_state(se_cmd)); - - spin_lock_irqsave(&se_cmd->t_state_lock, flags); - tas = (se_cmd->transport_state & CMD_T_TAS); - spin_unlock_irqrestore(&se_cmd->t_state_lock, flags); - - if (!__target_put_sess_cmd(se_cmd)) { - if (tas) - target_put_sess_cmd(se_cmd); - } - - wait_for_completion(&se_cmd->complete); - pr_debug("Finished waiting for se_cmd %p t_state %d fabric state: %d\n", - se_cmd, se_cmd->t_state, - se_cmd->se_tfo->get_cmd_state(se_cmd)); - - WARN_ON_ONCE(!se_cmd->se_sess); - target_put_sess_cmd(se_cmd); - } - - spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); - WARN_ON(!list_empty(&se_sess->sess_cmd_list)); - spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); + struct se_cmd *cmd; + int ret; + WARN_ON_ONCE(!se_sess->sess_tearing_down); + + spin_lock_irq(&se_sess->sess_cmd_lock); + do { + ret = wait_event_interruptible_lock_irq_timeout( + se_sess->cmd_list_wq, + list_empty(&se_sess->sess_cmd_list), + se_sess->sess_cmd_lock, 180 * HZ); + list_for_each_entry(cmd, &se_sess->sess_cmd_list, se_cmd_list) + target_show_cmd(__func__, cmd); + } while (ret <= 0); + spin_unlock_irq(&se_sess->sess_cmd_lock); } EXPORT_SYMBOL(target_wait_for_sess_cmds); diff --git a/include/target/target_core_base.h b/include/target/target_core_base.h index 5f35d92387d5..d7c0e7ecc601 100644 --- a/include/target/target_core_base.h +++ b/include/target/target_core_base.h @@ -440,7 +440,6 @@ struct se_cmd { u8 scsi_asc; u8 scsi_ascq; u16 scsi_sense_length; - unsigned cmd_wait_set:1; unsigned unknown_data_length:1; bool state_active:1; bool send_abort_response:1; @@ -603,8 +602,8 @@ struct se_session { struct list_head sess_list; struct list_head sess_acl_list; struct list_head sess_cmd_list; - struct list_head sess_wait_list; spinlock_t sess_cmd_lock; + wait_queue_head_t cmd_list_wq; void *sess_cmd_map; struct percpu_ida sess_tag_pool; };