linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Venkatesh Srinivas <venkateshs@google.com>
To: linux-scsi@vger.kernel.org
Cc: tom.leiming@gmail.com, Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [PATCH] virtio-scsi: replace target spinlock with seqcount
Date: Tue, 27 May 2014 09:50:31 -0700	[thread overview]
Message-ID: <20140527165031.GA21827@google.com> (raw)

Hi,

I think this patch has a small race involving just two commands:

1. The first command to a target is in virtscsi_pick_vq(), after
    atomic_inc_return(&tgt->tgt_lock)) but before write_seqcount_begin()
2. A second command to the same target enters virtscsi_pick_vq(). It
    will hit the same atomic inc, take the upper branch of the
    conditional, and read out a stale (or NULL) tgt->req_vq.

Specifically:

@@ -508,19 +507,33 @@ static struct virtio_scsi_vq *virtscsi_pick_vq(struct virtio_scsi *vscsi,
  	unsigned long flags;
  	u32 queue_num;
  
-	spin_lock_irqsave(&tgt->tgt_lock, flags);
+	local_irq_save(flags);
+	if (atomic_inc_return(&tgt->reqs) > 1) {
+		unsigned long seq;
+
+		do {
+			seq = read_seqcount_begin(&tgt->tgt_seq);
+			vq = tgt->req_vq;
+		} while (read_seqcount_retry(&tgt->tgt_seq, seq));
+	} else {

A second virtscsi_pick_vq() here will read a stale or NULL tgt->req_vq.

+		/* no writes can be concurrent because of atomic_t */
+		write_seqcount_begin(&tgt->tgt_seq);
+
+		/* keep previous req_vq if there is reader found */
+		if (unlikely(atomic_read(&tgt->reqs) > 1)) {
+			vq = tgt->req_vq;
+			goto unlock;
+		}
  
-	if (atomic_inc_return(&tgt->reqs) > 1)
-		vq = tgt->req_vq;
-	else {
  		queue_num = smp_processor_id();
  		while (unlikely(queue_num >= vscsi->num_queues))
  			queue_num -= vscsi->num_queues;
-
  		tgt->req_vq = vq = &vscsi->req_vqs[queue_num];
+ unlock:
+		write_seqcount_end(&tgt->tgt_seq);
  	}
+	local_irq_restore(flags);
  
-	spin_unlock_irqrestore(&tgt->tgt_lock, flags);
  	return vq;

Thanks,
-- vs;

             reply	other threads:[~2014-05-27 16:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-27 16:50 Venkatesh Srinivas [this message]
2014-05-27 16:57 ` [PATCH] virtio-scsi: replace target spinlock with seqcount Paolo Bonzini
2014-05-28  1:48   ` Ming Lei
2014-05-28  8:52     ` Paolo Bonzini
  -- strict thread matches above, loose matches on Subject: below --
2014-05-23 13:28 Ming Lei
2014-05-23 13:59 ` Paolo Bonzini

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=20140527165031.GA21827@google.com \
    --to=venkateshs@google.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=tom.leiming@gmail.com \
    /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).