diff mbox

[v6,16/33] target: Introduce a target driver reference count per command

Message ID 20170215002612.14566-17-bart.vanassche@sandisk.com (mailing list archive)
State Superseded
Headers show

Commit Message

Bart Van Assche Feb. 15, 2017, 12:25 a.m. UTC
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 <bart.vanassche@sandisk.com>
Cc: Hannes Reinecke <hare@suse.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Andy Grover <agrover@redhat.com>
Cc: David Disseldorp <ddiss@suse.de>
---
 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 mbox

Patch

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;