diff mbox

[v4,17/37] target: Introduce a target driver reference count per command

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

Commit Message

Bart Van Assche Feb. 8, 2017, 10:24 p.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       |  8 ++++----
 drivers/target/target_core_transport.c | 24 +++++++++++++++++++++---
 include/target/target_core_base.h      |  2 ++
 4 files changed, 28 insertions(+), 7 deletions(-)

Comments

Hannes Reinecke Feb. 9, 2017, 7:06 a.m. UTC | #1
On 02/08/2017 11:24 PM, Bart Van Assche wrote:
> 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       |  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;
>  
> 
What a curious construct.
Why not a normal kref here?
Using an atomic here would make sense if you need to bail out if both
counters would be independent.
But as it stands the 'tgt_kref' _has_ to be '0' before the final call to
__target_put_sess_cmd() (otherwise you'll end up with a hanging
wait_for_completion).
Which means that the lifetime of 'tgt_kref' is bounded by the lifetime
of se_cmd, and we can just add a second kref there.

Cheers,

Hannes
Nicholas A. Bellinger Feb. 9, 2017, 10:07 a.m. UTC | #2
On Wed, 2017-02-08 at 14:24 -0800, Bart Van Assche wrote:
> 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>
> ---

I'm strongly opposed to adding another reference count and struct
completion to every se_cmd, and using a special version of
__target_put_sess_cmd() sprinkled all over the place.

We've already got a se_cmd->cmd_kref, se_cmd->t_transport_stop_comp used
for se_cmd quiesce (CMD_T_STOP) during CMD_T_ABORT and
transport_generic_free_cmd() shutdown, and se_cmd->cmd_wait_set for
drivers doing active I/O shutdown via target_sess_cmd_list_set_waiting.

Adding a new reference count, a third struct completion, and fast path
complete_all() for everyone (not just CMD_T_ABORT) just to get rid of
se_cmd->cmd_wait_set doesn't simplify anything.

So that said, dropping this patch until you can figure out how to use
the single existing se_cmd->cmd_kref.

>  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;
>  


--
To unsubscribe from this list: send the line "unsubscribe target-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Bart Van Assche Feb. 9, 2017, 5:06 p.m. UTC | #3
On Thu, 2017-02-09 at 08:06 +0100, Hannes Reinecke wrote:
> On 02/08/2017 11:24 PM, Bart Van Assche wrote:
> > 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       |  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;
> >  
> > 
> 
> What a curious construct.
> Why not a normal kref here?
> Using an atomic here would make sense if you need to bail out if both
> counters would be independent.
> But as it stands the 'tgt_kref' _has_ to be '0' before the final call to
> __target_put_sess_cmd() (otherwise you'll end up with a hanging
> wait_for_completion).
> Which means that the lifetime of 'tgt_kref' is bounded by the lifetime
> of se_cmd, and we can just add a second kref there.

Although that will result in more code, I will change the atomic_t into a
kref_t.

Bart.--
To unsubscribe from this list: send the line "unsubscribe target-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Bart Van Assche Feb. 9, 2017, 7:03 p.m. UTC | #4
On Thu, 2017-02-09 at 02:07 -0800, Nicholas A. Bellinger wrote:
> I'm strongly opposed to adding another reference count and struct
> completion to every se_cmd, and using a special version of
> __target_put_sess_cmd() sprinkled all over the place.
> 
> We've already got a se_cmd->cmd_kref, se_cmd->t_transport_stop_comp used
> for se_cmd quiesce (CMD_T_STOP) during CMD_T_ABORT and
> transport_generic_free_cmd() shutdown, and se_cmd->cmd_wait_set for
> drivers doing active I/O shutdown via target_sess_cmd_list_set_waiting.
> 
> Adding a new reference count, a third struct completion, and fast path
> complete_all() for everyone (not just CMD_T_ABORT) just to get rid of
> se_cmd->cmd_wait_set doesn't simplify anything.
> 
> So that said, dropping this patch until you can figure out how to use
> the single existing se_cmd->cmd_kref.

It is very ugly that there are multiple direct calls of .release_cmd() in
the target code. That is just not acceptable. The only way to address
this is by introducing a second reference count in struct se_cmd. We need
one reference count to keep track of the lifetime of struct se_cmd and a
second reference count to keep track of the number of references held by
the target driver.

Regarding t_transport_stop_comp: my plan is to remove that completion later
on.

Bart.--
To unsubscribe from this list: send the line "unsubscribe target-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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;