From: Paolo Bonzini <pbonzini@redhat.com>
To: Ming Lei <tom.leiming@gmail.com>
Cc: Venkatesh Srinivas <venkateshs@google.com>,
Linux SCSI List <linux-scsi@vger.kernel.org>
Subject: Re: [PATCH] virtio-scsi: replace target spinlock with seqcount
Date: Wed, 28 May 2014 10:52:24 +0200 [thread overview]
Message-ID: <5385A3C8.7090305@redhat.com> (raw)
In-Reply-To: <CACVXFVN8qaJVkipoKmvepfm0nAoeV0hGagzc=o=3DqVymDt+Xw@mail.gmail.com>
Il 28/05/2014 03:48, Ming Lei ha scritto:
> On Wed, May 28, 2014 at 12:57 AM, Paolo Bonzini <pbonzini@redhat.com> wrote:
>> Il 27/05/2014 18:50, Venkatesh Srinivas ha scritto:
>>
>>> 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()
>
> Good catch, thanks Venkatesh
>
> And it doesn't happen in my test, so looks it won't be easy to trigger, :-)
No, it's basically impossible because the race window is very small and
the first command (INQUIRY or REPORT LUNS) is likely to be synchronous
anyway. But it's there anyway.
>>> 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.
>>
>>
>> The NULL case is true, indeed I was going to send a patch to initialize
>> tgt->req_vq but I have not tested it.
>>
>> diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c
>> index fc054935eb1f..f0b4cdbfceb0 100644
>> --- a/drivers/scsi/virtio_scsi.c
>> +++ b/drivers/scsi/virtio_scsi.c
>> @@ -618,14 +631,17 @@ static int virtscsi_abort(struct scsi_cmnd *sc)
>>
>> static int virtscsi_target_alloc(struct scsi_target *starget)
>> {
>> + struct Scsi_Host *sh = dev_to_shost(starget->dev.parent);
>> + struct virtio_scsi *vscsi = shost_priv(sh);
>> +
>> struct virtio_scsi_target_state *tgt =
>> kmalloc(sizeof(*tgt), GFP_KERNEL);
>> if (!tgt)
>> return -ENOMEM;
>>
>>
>> seqcount_init(&tgt->tgt_seq);
>> atomic_set(&tgt->reqs, 0);
>> - tgt->req_vq = NULL;
>> + tgt->req_vq = &vscsi->req_vqs[0];
>>
>> starget->hostdata = tgt;
>> return 0;
>
> Paolo, do you need me to integrate this one into the patch? or
> you will make it as one standalone?
I will integrate it myself after testing it.
Paolo
next prev parent reply other threads:[~2014-05-28 8:52 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-27 16:50 [PATCH] virtio-scsi: replace target spinlock with seqcount Venkatesh Srinivas
2014-05-27 16:57 ` Paolo Bonzini
2014-05-28 1:48 ` Ming Lei
2014-05-28 8:52 ` Paolo Bonzini [this message]
-- 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=5385A3C8.7090305@redhat.com \
--to=pbonzini@redhat.com \
--cc=linux-scsi@vger.kernel.org \
--cc=tom.leiming@gmail.com \
--cc=venkateshs@google.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).