qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Gonglei <arei.gonglei@huawei.com>
To: Stefan Hajnoczi <stefanha@redhat.com>
Cc: pbonzini@redhat.com, qemu-devel@nongnu.org, qemu-block@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v3] virtio-blk: trivial code optimization
Date: Tue, 10 Nov 2015 14:35:19 +0800	[thread overview]
Message-ID: <56419027.8090509@huawei.com> (raw)
In-Reply-To: <20151109135722.GB27221@stefanha-x1.localdomain>

On 2015/11/9 21:57, Stefan Hajnoczi wrote:
> On Mon, Nov 09, 2015 at 05:03:30PM +0800, arei.gonglei@huawei.com wrote:
>> From: Gonglei <arei.gonglei@huawei.com>
>>
>> 1. avoid possible superflous checking
>> 2. make code more robustness
>>
>> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
>> Reviewed-by: Fam Zheng <famz@redhat.com>
>> ---
>> v3: change the third condition too [Paolo]
>>     add Fam's R-by
>> ---
>>  hw/block/virtio-blk.c | 27 +++++++++------------------
>>  1 file changed, 9 insertions(+), 18 deletions(-)
>>
>> diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
>> index 093e475..9124358 100644
>> --- a/hw/block/virtio-blk.c
>> +++ b/hw/block/virtio-blk.c
>> @@ -404,24 +404,15 @@ void virtio_blk_submit_multireq(BlockBackend *blk, MultiReqBuffer *mrb)
>>      for (i = 0; i < mrb->num_reqs; i++) {
>>          VirtIOBlockReq *req = mrb->reqs[i];
>>          if (num_reqs > 0) {
>> -            bool merge = true;
>> -
>> -            /* merge would exceed maximum number of IOVs */
>> -            if (niov + req->qiov.niov > IOV_MAX) {
>> -                merge = false;
>> -            }
>> -
>> -            /* merge would exceed maximum transfer length of backend device */
>> -            if (req->qiov.size / BDRV_SECTOR_SIZE + nb_sectors > max_xfer_len) {
>> -                merge = false;
>> -            }
>> -
>> -            /* requests are not sequential */
>> -            if (sector_num + nb_sectors != req->sector_num) {
>> -                merge = false;
>> -            }
>> -
>> -            if (!merge) {
>> +            /*
>> +             * NOTE: We cannot merge the requests in below situations:
>> +             * 1. requests are not sequential
>> +             * 2. merge would exceed maximum number of IOVs
>> +             * 3. merge would exceed maximum transfer length of backend device
>> +             */
>> +            if (sector_num + nb_sectors != req->sector_num ||
>> +                niov > IOV_MAX - req->qiov.niov ||
>> +                nb_sectors > max_xfer_len - req->qiov.size / BDRV_SECTOR_SIZE) {
> 
> nb_sectors - int
> max_xfer_len - int
> req->qiov.size - size_t
> BDRV_SECTOR_SIZE - unsigned long long
> 
> Therefore this expression is an int > unsigned long long comparison.
> 
Sorry, I'm confused.
max_xfer_len is int,
"req->qiov.size / BDRV_SECTOR_SIZE" is  unsigned long long,
so, "max_xfer_len - req->qiov.size / BDRV_SECTOR_SIZE" will be int,

and nb_sectors is int too, so this comparison is right. Am I wrong?

> req->qiov.size can be larger than max_xfer_len so this comparison
> suffers from underflow.  Please check that req->qiov.size <=
> max_xfer_len first.
> 

Regards,
-Gonglei

  reply	other threads:[~2015-11-10  6:36 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-09  9:03 [Qemu-devel] [PATCH v3] virtio-blk: trivial code optimization arei.gonglei
2015-11-09 13:28 ` Paolo Bonzini
2015-11-09 13:57 ` Stefan Hajnoczi
2015-11-10  6:35   ` Gonglei [this message]
2015-11-10  9:51     ` Paolo Bonzini
2015-11-10  9:56     ` Stefan Hajnoczi
2015-11-11  1:53       ` Gonglei

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=56419027.8090509@huawei.com \
    --to=arei.gonglei@huawei.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.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).