virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: Asias He <asias@redhat.com>
Cc: kvm@vger.kernel.org, "Michael S. Tsirkin" <mst@redhat.com>,
	virtualization@lists.linux-foundation.org,
	target-devel@vger.kernel.org, Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [PATCH 5/5] tcm_vhost: Add hotplug/hotunplug support
Date: Thu, 7 Mar 2013 13:53:05 +0100	[thread overview]
Message-ID: <20130307125305.GA27175@stefanha-thinkpad.redhat.com> (raw)
In-Reply-To: <20130307094726.GD19603@hj.localdomain>

On Thu, Mar 07, 2013 at 05:47:26PM +0800, Asias He wrote:
> On Thu, Mar 07, 2013 at 09:58:04AM +0100, Stefan Hajnoczi wrote:
> > On Thu, Mar 07, 2013 at 08:26:20AM +0800, Asias He wrote:
> > > On Wed, Mar 06, 2013 at 10:21:09AM +0100, Stefan Hajnoczi wrote:
> > > > On Wed, Mar 06, 2013 at 02:16:30PM +0800, Asias He wrote:
> > > > > +static struct tcm_vhost_evt *tcm_vhost_allocate_evt(struct vhost_scsi *vs,
> > > > > +	u32 event, u32 reason)
> > > > > +{
> > > > > +	struct tcm_vhost_evt *evt;
> > > > > +
> > > > > +	if (atomic_read(&vs->vs_events_nr) > VHOST_SCSI_MAX_EVENT)
> > > > > +		return NULL;
> > > > > +
> > > > > +	evt = kzalloc(sizeof(*evt), GFP_KERNEL);
> > > > > +
> > > > > +	if (evt) {
> > > > > +		atomic_inc(&vs->vs_events_nr);
> > > > 
> > > > This looks suspicious: checking vs_events_nr > VHOST_SCSI_MAX_EVENT
> > > > first and then incrementing later isn't atomic!
> > > 
> > > This does not matter. (1) and (2) are okay. In case (3), the other side
> > > can only decrease the number of event, the limit will not be exceeded.
> > > 
> > > (1) 
> > >    		   atomic_dec()
> > >    atomic_read() 
> > >    atomic_inc()
> > > (2)
> > >    atomic_read() 
> > >    atomic_inc()
> > >    		   atomic_dec()
> > > 
> > > (3)
> > >    atomic_read() 
> > >    		   atomic_dec()
> > >    atomic_inc()
> > 
> > The cases you listed are fine but I'm actually concerned about
> > tcm_vhost_allocate_evt() racing with itself.  There are 3 callers and
> > I'm not sure which lock prevents them from executing at the same time.
> 
> No lock to prevent it. But what is the racing of executing
> tcm_vhost_allocate_evt() at the same time?

atomic_read() <= VHOST_SCSI_MAX_EVENT
                                       atomic_read() <= VHOST_SCSI_MAX_EVENT
atomic_inc()
                                       atomic_inc()

Now vs->vs_events_nr == VHOST_SCSI_MAX_EVENT + 1 which the if statement
was supposed to prevent.

> > > > > +static int tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
> > > > > +{
> > > > > +	struct vhost_scsi *vs = tpg->vhost_scsi;
> > > > > +
> > > > > +	mutex_lock(&tpg->tv_tpg_mutex);
> > > > > +	vs = tpg->vhost_scsi;
> > > > > +	mutex_unlock(&tpg->tv_tpg_mutex);
> > > > > +	if (!vs)
> > > > > +		return -EOPNOTSUPP;
> > > > > +
> > > > > +	if (!tcm_vhost_check_feature(vs, VIRTIO_SCSI_F_HOTPLUG))
> > > > > +		return -EOPNOTSUPP;
> > > > > +
> > > > > +	return tcm_vhost_send_evt(vs, tpg, lun,
> > > > > +			VIRTIO_SCSI_T_TRANSPORT_RESET,
> > > > > +			VIRTIO_SCSI_EVT_RESET_REMOVED);
> > > > > +}
> > > > 
> > > > tcm_vhost_hotplug() and tcm_vhost_hotunplug() are the same function
> > > > except for VIRTIO_SCSI_EVT_RESET_RESCAN vs
> > > > VIRTIO_SCSI_EVT_RESET_REMOVED.  That can be passed in as an argument and
> > > > the code duplication can be eliminated.
> > > 
> > > I thought about this also. We can have a tcm_vhost_do_hotplug() helper.
> > > 
> > >    tcm_vhost_do_hotplug(tpg, lun, plug)
> > >    
> > >    tcm_vhost_hotplug() {
> > >    	tcm_vhost_do_hotplug(tpg, lun, true)
> > >    }
> > >    
> > >    tcm_vhost_hotunplug() {
> > >    	tcm_vhost_do_hotplug(tpg, lun, false)
> > >    }
> > > 
> > > The reason I did not do that is I do not like the true/false argument
> > > but anyway this could remove duplication. I will do it.
> > 
> > true/false makes the calling code hard to read, I suggest passing in
> > VIRTIO_SCSI_EVT_RESET_RESCAN or VIRTIO_SCSI_EVT_RESET_REMOVED as the
> > argument.
> 
> Yes. However, I think passing VIRTIO_SCSI_EVT_RESET_* is even worse.
> 
> 1) Having VIRTIO_SCSI_EVT_RESET_RESCAN or VIRTIO_SCSI_EVT_RESET_REMOVED
> around VIRTIO_SCSI_T_TRANSPORT_RESET would be nicer.
> 
> 2) tcm_vhost_do_hotplug(tpg, lun, VIRTIO_SCSI_EVT_RESET_*)
> doest not make much sense. What the hell is VIRTIO_SCSI_EVT_RESET_* when
> you do hotplug or hotunplug. In contrast, if we have
> tcm_vhost_do_hotplug(tpg, lun, plug), plug means doing hotplug or
> hotunplug.

The VIRTIO_SCSI_EVT_RESET_REMOVED constant is pretty clear ("removed"
means unplug).  The VIRTIO_SCSI_EVT_RESET_RESCAN is less clear, but this
code is in drivers/vhost/tcm_vhost.c so you can expect the reader to
know the device specification :).

Anyway, it's not the end of the world if you leave the duplicated code
in, use a boolean parameter, or use the virtio event constant.

Stefan

  reply	other threads:[~2013-03-07 12:53 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-06  6:16 [PATCH 0/5] tcm_vhost hotplug/hotunplug support and locking fix Asias He
2013-03-06  6:16 ` [PATCH 1/5] tcm_vhost: Add missed lock in vhost_scsi_clear_endpoint() Asias He
2013-03-06  6:16 ` [PATCH 2/5] tcm_vhost: Introduce tcm_vhost_check_feature() Asias He
2013-03-06  9:06   ` Stefan Hajnoczi
2013-03-07  0:35     ` Asias He
2013-03-06  6:16 ` [PATCH 3/5] tcm_vhost: Introduce tcm_vhost_check_endpoint() Asias He
2013-03-06  6:16 ` [PATCH 4/5] tcm_vhost: Fix vs->vs_endpoint checking in vhost_scsi_handle_vq() Asias He
2013-03-06  6:16 ` [PATCH 5/5] tcm_vhost: Add hotplug/hotunplug support Asias He
     [not found] ` <1362550590-3534-6-git-send-email-asias@redhat.com>
2013-03-06  9:21   ` Stefan Hajnoczi
2013-03-07  0:26     ` Asias He
2013-03-07  8:58       ` Stefan Hajnoczi
2013-03-07  9:47         ` Asias He
2013-03-07 12:53           ` Stefan Hajnoczi [this message]
2013-03-08  2:11             ` Asias He

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130307125305.GA27175@stefanha-thinkpad.redhat.com \
    --to=stefanha@redhat.com \
    --cc=asias@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=target-devel@vger.kernel.org \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).