From patchwork Wed Feb 8 22:24:47 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: 9563665 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 82E146020F for ; Wed, 8 Feb 2017 22:28:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 569D5284F2 for ; Wed, 8 Feb 2017 22:28:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4B8472852B; Wed, 8 Feb 2017 22:28:13 +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 B96862852C for ; Wed, 8 Feb 2017 22:28:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751457AbdBHW2H (ORCPT ); Wed, 8 Feb 2017 17:28:07 -0500 Received: from esa2.hgst.iphmx.com ([68.232.143.124]:63845 "EHLO esa2.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751785AbdBHW16 (ORCPT ); Wed, 8 Feb 2017 17:27:58 -0500 X-IronPort-AV: E=Sophos;i="5.35,348,1483977600"; d="scan'208";a="76618202" Received: from unknown (HELO milsmgep14.sandisk.com) ([63.163.107.225]) by ob1.hgst.iphmx.com with ESMTP; 09 Feb 2017 06:28:22 +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 36.72.18895.CCA9B985; Wed, 8 Feb 2017 14:25:16 -0800 (PST) Received: from milsmgip12.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; Wed, 8 Feb 2017 14:25:11 -0800 X-AuditID: 0ac94371-0fadb980000049cf-fc-589b9acc314d Received: from exp-402881.sdcorp.global.sandisk.com ( [10.177.9.6]) by (Symantec Messaging Gateway) with SMTP id 23.42.18148.7CA9B985; Wed, 8 Feb 2017 14:25:11 -0800 (PST) From: Bart Van Assche To: Bart Van Assche CC: , Hannes Reinecke , Christoph Hellwig , Andy Grover , David Disseldorp Subject: [PATCH v4 17/37] target: Introduce a target driver reference count per command Date: Wed, 8 Feb 2017 14:24:47 -0800 Message-ID: <20170208222507.25715-18-bart.vanassche@sandisk.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170208222507.25715-1-bart.vanassche@sandisk.com> References: <20170208222507.25715-1-bart.vanassche@sandisk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrPLMWRmVeSWpSXmKPExsXCddJ5ke6ZWbMjDNp38Vrsn/WMyeLr/+ks Fgve7GWzWLn6KJNF69K3TA6sHrtvNrB5vN93lc1j/ZarLB6bT1d7fN4kF8AaxWWTkpqTWZZa pG+XwJWx4sws1oJOrYplc46xNTBuVu5i5OSQEDCRePLpK1sXIxeHkMBSJolN/9uYIJytjBKP ek8BZTjAqrrfREHENzJKzJ3QzgbSzSZgJPHt/UwWEFtEwEDid+8FRhCbWWA1o8T1h5IgtrBA uETP12fMIDaLgIrEuubNYDavgIPEoYkH2SCukJfY1XaRFcTmBIpPePkMbI6QgL3EqwNfGEEW SwjMYpV4P+EwVLOgxMmZT1gglklIHHzxghmiQV3i5JL5TBMYhWYhKZuFpGwBI9MqRrHczJzi 3PTUAkMTveLEvJTM4my95PzcTYyQYC/cwfj6tvchRgEORiUe3gvWsyOEWBPLiitzDzFKcDAr ifCmTAMK8aYkVlalFuXHF5XmpBYfYpTmYFES582SnRohJJCeWJKanZpakFoEk2Xi4JRqYDRv NbF7xp+zrmDaH/mAtoytV7f2pvm7O6ucnBVnKy+gsCTMY+I0a66MPhXXhWWLGTjThA1mXOWZ qPpmae5sPcFTEXFb24znb47W+lB2IX0VV+zCZefrPr8++caA7WDQ78DpnaVfNcsKJcR2W4q8 rl/IzvTG/oSg839t56Y1/HnpR/MyVnEvUWIpzkg01GIuKk4EAJ3DU/xyAgAA X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrHJMWRmVeSWpSXmKPExsXCtZGTTff4rNkRBp87tCz2z3rGZHHwZxuj xdf/01ksFrzZy2axcvVRJovWpW+ZHNg8dt9sYPN4v+8qm8e0NeeZPNZvucrisfl0tcfnTXIB bFFcNimpOZllqUX6dglcGSvOzGIt6NSqWDbnGFsD42blLkYODgkBE4nuN1FdjFwcQgLrGSVe HNzG1MXIycEmYCTx7f1MFhBbRMBA4nfvBUaQImaBtYwSe2bMYgVJCAuES/R8fcYMMohFQFVi 5tUKkDCvgIPEpRsTmEFsCQF5iV1tF8HKOYHiE14+YwSxhQTsJV4d+MI4gZF7ASPDKkax3Myc 4tz0zAJDI73ixLyUzOJsveT83E2MkBCJ2sF4faL5IUYmDk6pBkb7L+oqUq8VKso/hPUIrHfV OnLzfrP9W27xwP3H6qfoTxfoTYrY46Rycv5FyXmbdBb+N5f7H1S+f9a3/fe6O+ZuNjVI5Jt1 fYPDzdUN/+3mtJxMXhrAIiBjzLlwU4Xv1JUrHn/6zTt9w7XzYa5ni5Zu3OFgqaq6WV37FyPj T3/DuFuzNrewdUxWYinOSDTUYi4qTgQAo7xpBsEBAAA= 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 | 8 ++++---- drivers/target/target_core_transport.c | 24 +++++++++++++++++++++--- include/target/target_core_base.h | 2 ++ 4 files changed, 28 insertions(+), 7 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 367799b4dde1..161c8b8482db 100644 --- a/drivers/target/target_core_tmr.c +++ b/drivers/target/target_core_tmr.c @@ -188,7 +188,7 @@ void core_tmr_abort_task( if (!tmr->tmr_dev && transport_lookup_tmr_lun(tmr->task_cmd, se_cmd->orig_fe_lun) < 0) { - target_put_sess_cmd(se_cmd); + __target_put_sess_cmd(se_cmd); continue; } @@ -199,7 +199,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); @@ -299,7 +299,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); } } @@ -398,7 +398,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 8f2169e92771..533b584c2806 100644 --- a/drivers/target/target_core_transport.c +++ b/drivers/target/target_core_transport.c @@ -1217,6 +1217,7 @@ void transport_init_se_cmd( init_completion(&cmd->cmd_wait_comp); spin_lock_init(&cmd->t_state_lock); kref_init(&cmd->cmd_kref); + atomic_set(&cmd->tgt_ref, 1); cmd->transport_state = CMD_T_DEV_ACTIVE; cmd->se_tfo = tfo; @@ -2554,6 +2555,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; + atomic_inc(&se_cmd->tgt_ref); se_cmd->se_cmd_flags |= SCF_ACK_KREF; } @@ -2590,6 +2592,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) != 0); + if (se_sess) { spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); @@ -2614,15 +2618,29 @@ static void target_release_cmd_kref(struct kref *kref) } /** + * __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); + long ref = atomic_dec_return(&se_cmd->tgt_ref); + + WARN_ON_ONCE(ref < 0); + if (ref == 0) + complete(&se_cmd->complete); + + return __target_put_sess_cmd(se_cmd); } EXPORT_SYMBOL(target_put_sess_cmd); @@ -2680,7 +2698,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 fb87f0e5d0d6..c84e5eecb94d 100644 --- a/include/target/target_core_base.h +++ b/include/target/target_core_base.h @@ -495,7 +495,9 @@ struct se_cmd { #define CMD_T_FABRIC_STOP (1 << 11) spinlock_t t_state_lock; struct kref cmd_kref; + atomic_t tgt_ref; struct completion t_transport_stop_comp; + struct completion complete; struct work_struct work;