From patchwork Fri Feb 10 01:28:33 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: 9565765 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 5FB4860572 for ; Fri, 10 Feb 2017 01:29:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5172928532 for ; Fri, 10 Feb 2017 01:29:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4651E28548; Fri, 10 Feb 2017 01:29:10 +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 BC31028532 for ; Fri, 10 Feb 2017 01:29:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751290AbdBJB3H (ORCPT ); Thu, 9 Feb 2017 20:29:07 -0500 Received: from esa1.hgst.iphmx.com ([68.232.141.245]:18628 "EHLO esa1.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751362AbdBJB3F (ORCPT ); Thu, 9 Feb 2017 20:29:05 -0500 X-IronPort-AV: E=Sophos;i="5.33,348,1477929600"; d="scan'208";a="82349474" Received: from unknown (HELO milsmgep15.sandisk.com) ([63.163.107.21]) by ob1.hgst.iphmx.com with ESMTP; 10 Feb 2017 09:28:59 +0800 Received: from MILHUBIP04.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 8E.87.57638.B571D985; Thu, 9 Feb 2017 17:28:59 -0800 (PST) Received: from milsmgip11.sandisk.com (10.177.9.6) by MILHUBIP04.sdcorp.global.sandisk.com (10.177.9.97) with Microsoft SMTP Server id 14.3.319.2; Thu, 9 Feb 2017 17:28:55 -0800 X-AuditID: 0ac94369-26dee9800001e126-f9-589d175b8666 Received: from exp-402881.sdcorp.global.sandisk.com ( [10.177.9.6]) by (Symantec Messaging Gateway) with SMTP id 22.9F.11415.7571D985; Thu, 9 Feb 2017 17:28:55 -0800 (PST) From: Bart Van Assche To: "Nicholas A . Bellinger" CC: , Bart Van Assche , Hannes Reinecke , Christoph Hellwig , Andy Grover , David Disseldorp Subject: [PATCH v5 03/22] target: Introduce a target driver reference count per command Date: Thu, 9 Feb 2017 17:28:33 -0800 Message-ID: <20170210012852.15735-4-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+NgFnrJLMWRmVeSWpSXmKPExsXCddJ5kW60+NwIg+OLhC32z3rGZPH1/3QW iwVv9rJZrFx9lMmibfUZRovWpW+ZHNg87m8/wuSx+2YDm8f7fVfZPNZvucrisfl0tcfnTXIB bFFcNimpOZllqUX6dglcGc1r5zEWLNKs2HrTrYGxSbmLkZNDQsBEom36MaYuRi4OIYGlTBIX bn9hhnC2MkrM+/uYDaZq1/IWqMRGRoktdxYwgSTYBIwkvr2fyQJiiwDZq3uWgY1iFrjNKLHm 8WqwbmGBcInrG1+CNbAIqEhcXz8ZLM4rYC9xbOcfqA3yErvaLrKC2JwCDhKvZl8HsjmAttlL 7NhlADJTQmARq8TFi0fZIXoFJU7OfAK2mFlAQuLgixfMILaQgLrEySXzmSYwCs1CUjYLSdkC RqZVjGK5mTnFuempBYamesWJeSmZxdl6yfm5mxghEZC5g/HuE+9DjAIcjEo8vC9q5kQIsSaW FVfmHmKU4GBWEuG9zzc3Qog3JbGyKrUoP76oNCe1+BCjNAeLkjjvOZmpEUIC6YklqdmpqQWp RTBZJg5OqQbGQHYLg0U/ijct1FlzuzB61vGTdmkPfHg9nx+Y7Ze6gVFOw0deqn/OVKsfL6Iz ap5/rfi87LP3c2dO389/3jZXCxvZzdIWvpi2Quu9toWgvNqXb4V+JlwaLX1uCoubpy/ivMaY sqkjK8Rp/cXIaJ7dU+N+ysxdotLtlqtuGLE058KLue6XL7QqsRRnJBpqMRcVJwIAbjHy43wC AAA= X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrOJMWRmVeSWpSXmKPExsXCtZGTTTdcfG6Ewd+vvBb7Zz1jsjj4s43R 4uv/6SwWC97sZbNYufook0Xb6jOMFq1L3zI5sHvc336EyWP3zQY2j/f7rrJ5TFtznslj/Zar LB6bT1d7fN4kF8AexWWTkpqTWZZapG+XwJXRvHYeY8EizYqtN90aGJuUuxg5OSQETCR2LW9h 7mLk4hASWM8o8afhPxtIgk3ASOLb+5ksILYIkL26ZxkTSBGzwF1GiTPdn8CKhAXCJa5vfAmU 4OBgEVCVOLa1HCTMK2AvcW7NZzaIBfISu9ousoLYnAIOEq9mX2cFKRcCqtmxy2ACI/cCRoZV jGK5mTnFuemZBYaGesWJeSmZxdl6yfm5mxghQRO5g/HpRPNDjEwcnFINjOKBM6e/mFL2PGvV esesKzo8rHwBtpO7D796yZ7vFXZLacq9V4qfHNY+exezaE8fe5ZJ6Xsn5d/CJlqfk5XWX5mw 6tv7fSfSD7nNPNi79pHWx3rOyyLF1494T3g08apemJdud3CJfnLE39hZccz9joynt/XPUn/X aHCKv/VTq8e/a8eXnFOfcFuJpTgj0VCLuag4EQAVzq68ygEAAA== 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 This reference count keeps track of the number of references held by a target driver on a command. se_cmd.complete is set once the target driver has released all references it held on a command. Signed-off-by: Bart Van Assche Cc: Hannes Reinecke Cc: Christoph Hellwig Cc: Andy Grover Cc: David Disseldorp --- drivers/target/target_core_internal.h | 1 + drivers/target/target_core_tmr.c | 6 +++--- drivers/target/target_core_transport.c | 27 ++++++++++++++++++++++++--- include/target/target_core_base.h | 2 ++ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/drivers/target/target_core_internal.h b/drivers/target/target_core_internal.h index 9ab7090f7c83..48de996ad28d 100644 --- a/drivers/target/target_core_internal.h +++ b/drivers/target/target_core_internal.h @@ -146,6 +146,7 @@ int transport_dump_vpd_assoc(struct t10_vpd *, unsigned char *, int); int transport_dump_vpd_ident_type(struct t10_vpd *, unsigned char *, int); int transport_dump_vpd_ident(struct t10_vpd *, unsigned char *, int); void transport_clear_lun_ref(struct se_lun *); +int __target_put_sess_cmd(struct se_cmd *se_cmd); void transport_send_task_abort(struct se_cmd *); sense_reason_t target_cmd_size_check(struct se_cmd *cmd, unsigned int size); void target_qf_do_work(struct work_struct *work); diff --git a/drivers/target/target_core_tmr.c b/drivers/target/target_core_tmr.c index 3330775aa47a..ce6ab8cd7663 100644 --- a/drivers/target/target_core_tmr.c +++ b/drivers/target/target_core_tmr.c @@ -192,7 +192,7 @@ void core_tmr_abort_task( transport_wait_for_tasks(se_cmd); transport_cmd_finish_abort(se_cmd, true); - target_put_sess_cmd(se_cmd); + __target_put_sess_cmd(se_cmd); printk("ABORT_TASK: Sending TMR_FUNCTION_COMPLETE for" " ref_tag: %llu\n", ref_tag); @@ -292,7 +292,7 @@ static void core_tmr_drain_tmr_list( transport_wait_for_tasks(cmd); transport_cmd_finish_abort(cmd, 1); - target_put_sess_cmd(cmd); + __target_put_sess_cmd(cmd); } } @@ -391,7 +391,7 @@ static void core_tmr_drain_state_list( transport_wait_for_tasks(cmd); core_tmr_handle_tas_abort(cmd, tas); - target_put_sess_cmd(cmd); + __target_put_sess_cmd(cmd); } } diff --git a/drivers/target/target_core_transport.c b/drivers/target/target_core_transport.c index 4e233c0f5f68..c4ea12966fad 100644 --- a/drivers/target/target_core_transport.c +++ b/drivers/target/target_core_transport.c @@ -1223,6 +1223,8 @@ void transport_init_se_cmd( init_completion(&cmd->cmd_wait_comp); spin_lock_init(&cmd->t_state_lock); kref_init(&cmd->cmd_kref); + kref_init(&cmd->tgt_ref); + init_completion(&cmd->complete); cmd->se_tfo = tfo; cmd->se_sess = se_sess; @@ -2559,6 +2561,7 @@ int target_get_sess_cmd(struct se_cmd *se_cmd, bool ack_kref) if (!kref_get_unless_zero(&se_cmd->cmd_kref)) return -EINVAL; + kref_get(&se_cmd->tgt_ref); se_cmd->se_cmd_flags |= SCF_ACK_KREF; } @@ -2595,6 +2598,8 @@ static void target_release_cmd_kref(struct kref *kref) 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); @@ -2618,16 +2623,32 @@ static void target_release_cmd_kref(struct kref *kref) se_cmd->se_tfo->release_cmd(se_cmd); } +static void target_release_tgt_ref(struct kref *kref) +{ + struct se_cmd *cmd = container_of(kref, typeof(*cmd), tgt_ref); + + complete(&cmd->complete); +} + +/** + * __target_put_sess_cmd - drop a reference obtained by kref_get_unless_zero() + */ +int __target_put_sess_cmd(struct se_cmd *se_cmd) +{ + return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref); +} + /** * target_put_sess_cmd - decrease the command reference count - * @se_cmd: command to drop a reference from + * @se_cmd: Drop a reference obtained by target_get_sess_cmd(). * * Returns 1 if and only if this target_put_sess_cmd() call caused the * refcount to drop to zero. Returns zero otherwise. */ int target_put_sess_cmd(struct se_cmd *se_cmd) { - return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref); + kref_put(&se_cmd->tgt_ref, target_release_tgt_ref); + return __target_put_sess_cmd(se_cmd); } EXPORT_SYMBOL(target_put_sess_cmd); @@ -2685,7 +2706,7 @@ void target_wait_for_sess_cmds(struct se_session *se_sess) 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 (!__target_put_sess_cmd(se_cmd)) { if (tas) target_put_sess_cmd(se_cmd); } diff --git a/include/target/target_core_base.h b/include/target/target_core_base.h index 47aa40334d04..2633e6ff7eee 100644 --- a/include/target/target_core_base.h +++ b/include/target/target_core_base.h @@ -493,6 +493,8 @@ struct se_cmd { #define CMD_T_FABRIC_STOP (1 << 11) spinlock_t t_state_lock; struct kref cmd_kref; + struct kref tgt_ref; + struct completion complete; struct completion t_transport_stop_comp; struct work_struct work;