From patchwork Wed Feb 15 00:25:55 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: 9573125 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 37A15600F6 for ; Wed, 15 Feb 2017 00:27:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2AEAF2842B for ; Wed, 15 Feb 2017 00:27:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1F7C42843C; Wed, 15 Feb 2017 00:27:31 +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 E926E2842B for ; Wed, 15 Feb 2017 00:27:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750905AbdBOA13 (ORCPT ); Tue, 14 Feb 2017 19:27:29 -0500 Received: from esa2.hgst.iphmx.com ([68.232.143.124]:50689 "EHLO esa2.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751338AbdBOA10 (ORCPT ); Tue, 14 Feb 2017 19:27:26 -0500 X-IronPort-AV: E=Sophos;i="5.35,163,1483977600"; d="scan'208";a="81774853" Received: from unknown (HELO milsmgep15.sandisk.com) ([63.163.107.21]) by ob1.hgst.iphmx.com with ESMTP; 15 Feb 2017 08:43:11 +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 09.E4.57638.B20A3A85; Tue, 14 Feb 2017 16:26:19 -0800 (PST) Received: from milsmgip11.sandisk.com (10.177.8.100) by MILHUBIP03.sdcorp.global.sandisk.com (10.177.9.96) with Microsoft SMTP Server id 14.3.319.2; Tue, 14 Feb 2017 16:26:15 -0800 X-AuditID: 0ac94369-26dee9800001e126-6e-58a3a02bc9dc Received: from exp-402881.sdcorp.global.sandisk.com ( [10.177.9.6]) by (Symantec Messaging Gateway) with SMTP id 2D.7D.11415.720A3A85; Tue, 14 Feb 2017 16:26:15 -0800 (PST) From: Bart Van Assche To: "Nicholas A . Bellinger" CC: , Bart Van Assche , Hannes Reinecke , Christoph Hellwig , Andy Grover , David Disseldorp Subject: [PATCH v6 16/33] target: Introduce a target driver reference count per command Date: Tue, 14 Feb 2017 16:25:55 -0800 Message-ID: <20170215002612.14566-17-bart.vanassche@sandisk.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170215002612.14566-1-bart.vanassche@sandisk.com> References: <20170215002612.14566-1-bart.vanassche@sandisk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrBLMWRmVeSWpSXmKPExsXCddJ5ka72gsURBhs2y1vsn/WMyeLr/+ks Fgve7GWzWLn6KJNF2+ozjBatS98yObB53N9+hMlj980GNo/3+66yeazfcpXFY/Ppao/Pm+QC 2KK4bFJSczLLUov07RK4MvoXNjAXLNKseH+ojbGBsUm5i5GTQ0LARGLrtiPsILaQwFImiYO7 MroYuYDsHYwSb5ZcZIIpOvDiOhNEYiOjxKonCxlBEmwCRhLf3s9kAbFFgOzVPcvAipgFbjNK rHm8mg0kISwQLnF3YzNQEQcHi4CqxMp7WiBhXgEHifdbD7BBLJCX2NV2kRXE5gSKz9hxEqxc SMBe4tEZa5CREgLzWCWmTdzPCtErKHFy5hOwvcwCEhIHX7xghvhAXeLkkvlMExiFZiEpm4Wk bAEj0ypGsdzMnOLc9NQCQ1O94sS8lMzibL3k/NxNjJDwz9zBePeJ9yFGAQ5GJR7eE7KLI4RY E8uKK3MPMUpwMCuJ8LLVA4V4UxIrq1KL8uOLSnNSiw8xSnOwKInznpOZGiEkkJ5YkpqdmlqQ WgSTZeLglGpgXGgff+RAutjTz7HNZoWuleHsippfpzmGX0pk2fk2XlH/Qty0RfYzixaq7qjU ajUWOvXaj/u69S3lxXe3b5u+Z3v//X3bizdcYzC7qJdyveSHePfqwzti7v60/bVAu5K9tWX/ Irb4pTveyn1lTL2tflFbVHatV4CkjtfN76FlyumRG5fHnl9UrMRSnJFoqMVcVJwIAF4ntYd7 AgAA X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrOJMWRmVeSWpSXmKPExsXCtZGTTVd9weIIg3+/FSz2z3rGZHHwZxuj xdf/01ksFrzZy2axcvVRJou21WcYLVqXvmVyYPe4v/0Ik8fumw1sHu/3XWXzmLbmPJPH+i1X WTw2n672+LxJLoA9issmJTUnsyy1SN8ugSujf2EDc8EizYr3h9oYGxiblLsYOTkkBEwkDry4 ztTFyMUhJLCeUeJu9wdmkASbgJHEt/czWUBsESB7dc8ysCJmgbuMEme6P7GBJIQFwiXubmwG KuLgYBFQlVh5TwvE5BVwkFiwswpivrzErraLrCA2J1B4xo6TYNVCAvYSj85YT2DkXsDIsIpR LDczpzg3PbPA0FCvODEvJbM4Wy85P3cTIyRoIncwPp1ofoiRiYNTqoHxShuP9uSinlBblaMG hf9PfviwZ/2n31UPl3nOf7h/9nX7rRfmBi3/GHE6ISdJ7qjv0hy+fe6M/w4zr1/2PJ1dl1OF O793G9+Ebs1rp9cu9oiriE+9lLB56zHvA1KavTcEfQJdyuJ02tL43n4P0X1RsC1kgdKaFwGH Ba5PP5J3Pl39hBLfnvJDSizFGYmGWsxFxYkA/40SWcoBAAA= 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 cf32df0e65ba..882db8c2c2e9 100644 --- a/drivers/target/target_core_internal.h +++ b/drivers/target/target_core_internal.h @@ -148,6 +148,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 5c671fa67339..0b7c4a66102c 100644 --- a/drivers/target/target_core_tmr.c +++ b/drivers/target/target_core_tmr.c @@ -200,7 +200,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); @@ -295,7 +295,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); } } @@ -394,7 +394,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 ab1c493a9a16..0cf2151b6a21 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 c847f520b0e7..9309522d50c3 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;