From patchwork Mon Sep 17 21:35:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 10603483 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 825BD157B for ; Mon, 17 Sep 2018 22:16:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6A4162A22D for ; Mon, 17 Sep 2018 22:16:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5D6D82A358; Mon, 17 Sep 2018 22:16:22 +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=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 383C92A22D for ; Mon, 17 Sep 2018 22:16:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728594AbeIRDpg (ORCPT ); Mon, 17 Sep 2018 23:45:36 -0400 Received: from out002.mailprotect.be ([83.217.72.86]:36135 "EHLO out002.mailprotect.be" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728415AbeIRDpg (ORCPT ); Mon, 17 Sep 2018 23:45:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mailprotect.be; s=mail; h=Content-Transfer-Encoding:MIME-Version:References :In-Reply-To:Message-Id:Date:Subject:Cc:To:From:reply-to:sender:bcc: content-type; bh=PqlQN6g08nMAe59B/5L6wvT+TuBIcX5BodjCxzLiEqY=; b=kn0bBAIC8Hjh yfY65vSoFjJO5PWHek3KJ6g5pNXagZxzJi8mHEH5P3jPKNBjpUVegGbXGsPIqJtF9D9T/8lQkQ0hf StIXVcBDjLSBd4/RZz1NMmV4FlAAu065hvLDjRrNQTxHaUDbD6Gdp+q0W57aeSZZm9kuuaD5o110s Z3cn/B6vFfnZYxQne7KVvWzKiAr3l/o1BSKNhasGnz368u/09bFJeH3AGP32XQLW8XnW09gPMR58a 8r6sLx81BuJTAGdgR+xSFK7I7FKV7lyqSLJ2KxoptCaF+S8uFlhIW7GSv0LObyPno81iwXOMxFD+K KC4taFIGwK7FkvTKtc4s7w==; Received: from smtp-auth.mailprotect.be ([178.208.39.155]) by com-mpt-out002.mailprotect.be with esmtp (Exim 4.89) (envelope-from ) id 1g21C1-0002ii-2M; Mon, 17 Sep 2018 23:36:29 +0200 Received: from asus.hsd1.ca.comcast.net (c-174-62-111-89.hsd1.ca.comcast.net [174.62.111.89]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp-auth.mailprotect.be (Postfix) with ESMTPSA id 04820C0718; Mon, 17 Sep 2018 23:36:26 +0200 (CEST) From: Bart Van Assche To: "Martin K . Petersen" Cc: Christoph Hellwig , target-devel@vger.kernel.org, Bart Van Assche , Nicholas Bellinger , Mike Christie , Hannes Reinecke Subject: [PATCH 10/17] target/core: Make it possible to wait from more than one context for command completion Date: Mon, 17 Sep 2018 14:35:47 -0700 Message-Id: <20180917213554.987-11-bvanassche@acm.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180917213554.987-1-bvanassche@acm.org> References: <20180917213554.987-1-bvanassche@acm.org> MIME-Version: 1.0 X-Originating-IP: 178.208.39.155 X-SpamExperts-Domain: mailprotect.be X-SpamExperts-Username: 178.208.39.128/27 Authentication-Results: mailprotect.be; auth=pass smtp.auth=178.208.39.128/27@mailprotect.be X-SpamExperts-Outgoing-Class: ham X-SpamExperts-Out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cHzHNTu4W3OyTlA5uhTP73RN8y cSzpfj5sSQhb3cc4IkKXIhy+I6DM7/owTL8J95f1/+MXnOL6CZv+n8/wRksD+x9JjG1O+eyolGM4 l8rlxuGfhU+34hrNm42X2UZAYRVVEuFqpn0Wcxxec4eIhtK5Pi2eqhlDMrilbtDDvtO4rayUOwaR X-Report-Abuse-To: spam@com-mpt-mgt001.mailprotect.be 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 This patch does not change any functionality but makes the next patch easier to read. Signed-off-by: Bart Van Assche Cc: Nicholas Bellinger Cc: Mike Christie Cc: Christoph Hellwig Cc: Hannes Reinecke Signed-off-by: Bart Van Assche --- drivers/target/target_core_transport.c | 69 ++++++++++++++++++++++---- include/target/target_core_base.h | 2 +- 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/drivers/target/target_core_transport.c b/drivers/target/target_core_transport.c index 79fa79afcdc2..1fecd2bb53af 100644 --- a/drivers/target/target_core_transport.c +++ b/drivers/target/target_core_transport.c @@ -1331,7 +1331,7 @@ void transport_init_se_cmd( INIT_LIST_HEAD(&cmd->se_cmd_list); INIT_LIST_HEAD(&cmd->state_list); init_completion(&cmd->t_transport_stop_comp); - cmd->compl = NULL; + init_waitqueue_head(&cmd->release_wq); spin_lock_init(&cmd->t_state_lock); INIT_WORK(&cmd->work, NULL); kref_init(&cmd->cmd_kref); @@ -2649,6 +2649,47 @@ static void target_wait_free_cmd(struct se_cmd *cmd, bool *aborted, bool *tas) spin_unlock_irqrestore(&cmd->t_state_lock, flags); } +static void __target_put_cmd_and_wait(struct se_cmd *cmd, bool put) +{ + DEFINE_WAIT(wait); + long ret; + + prepare_to_wait(&cmd->release_wq, &wait, TASK_UNINTERRUPTIBLE); + if (put) + target_put_sess_cmd(cmd); + for (;;) { + if (list_empty_careful(&wait.entry)) + break; + set_current_state(TASK_UNINTERRUPTIBLE); + if (list_empty_careful(&wait.entry)) { + set_current_state(TASK_RUNNING); + break; + } + ret = schedule_timeout(180 * HZ); + if (ret == 0) + target_show_cmd("still waiting for ", cmd); + } +} + +/* + * Call target_put_sess_cmd() and wait until target_release_cmd_kref(@cmd) has + * finished. + */ +static void target_put_cmd_and_wait(struct se_cmd *cmd) +{ + __target_put_cmd_and_wait(cmd, true); +} + +/* + * Wait until target_release_cmd_kref(@cmd) has finished. Calling this function + * is only safe if the release function does not free the memory allocated for + * @cmd. This is e.g. the case for the iSCSI target driver. + */ +static void target_wait_for_cmd(struct se_cmd *cmd) +{ + __target_put_cmd_and_wait(cmd, false); +} + /* * This function is called by frontend drivers after processing of a command * has finished. @@ -2672,7 +2713,6 @@ static void target_wait_free_cmd(struct se_cmd *cmd, bool *aborted, bool *tas) */ int transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks) { - DECLARE_COMPLETION_ONSTACK(compl); int ret = 0; bool aborted = false, tas = false; @@ -2691,14 +2731,17 @@ int transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks) if (cmd->se_lun) transport_lun_remove_cmd(cmd); } - if (aborted) - cmd->compl = &compl; - if (!aborted || tas) - ret = target_put_sess_cmd(cmd); if (aborted) { pr_debug("Detected CMD_T_ABORTED for ITT: %llu\n", cmd->tag); - wait_for_completion(&compl); + if (tas) { + target_put_cmd_and_wait(cmd); + } else { + WARN_ON_ONCE(!wait_for_tasks); + target_wait_for_cmd(cmd); + } ret = 1; + } else { + ret = target_put_sess_cmd(cmd); } return ret; } @@ -2759,9 +2802,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; - struct completion *compl = se_cmd->compl; + struct wait_queue_head release_wq; unsigned long flags; + init_waitqueue_head(&release_wq); + /* + * No locking is required since we are in the release function and + * adding to release_wq is only allowed while holding a reference. + */ + list_splice_init(&se_cmd->release_wq.head, &release_wq.head); + if (se_sess) { spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); list_del_init(&se_cmd->se_cmd_list); @@ -2770,8 +2820,7 @@ static void target_release_cmd_kref(struct kref *kref) target_free_cmd_mem(se_cmd); se_cmd->se_tfo->release_cmd(se_cmd); - if (compl) - complete(compl); + wake_up_all(&release_wq); percpu_ref_put(&se_sess->cmd_count); } diff --git a/include/target/target_core_base.h b/include/target/target_core_base.h index d084479fbe18..d93ec20710c2 100644 --- a/include/target/target_core_base.h +++ b/include/target/target_core_base.h @@ -475,7 +475,7 @@ struct se_cmd { struct se_session *se_sess; struct se_tmr_req *se_tmr_req; struct list_head se_cmd_list; - struct completion *compl; + struct wait_queue_head release_wq; const struct target_core_fabric_ops *se_tfo; sense_reason_t (*execute_cmd)(struct se_cmd *); sense_reason_t (*transport_complete_callback)(struct se_cmd *, bool, int *);