qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>, wei.w.wang@intel.com
Cc: QEMU Developers <qemu-devel@nongnu.org>,
	Juan Quintela <quintela@redhat.com>, Peter Xu <peterx@redhat.com>,
	Marcel Apfelbaum <marcel.apfelbaum@gmail.com>,
	yury-kotov@yandex-team.ru, Zhang Chen <chen.zhang@intel.com>,
	Markus Armbruster <armbru@redhat.com>
Subject: Re: [Qemu-devel] [PULL 19/22] virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
Date: Fri, 8 Mar 2019 18:30:53 +0000	[thread overview]
Message-ID: <20190308183053.GI2834@work-vm> (raw)
In-Reply-To: <CAFEAcA-mJdymqYBp-xrUbrXQ_fZ+mTZ90DG0nQs3v40HVxETzA@mail.gmail.com>

* Peter Maydell (peter.maydell@linaro.org) wrote:
> On Wed, 6 Mar 2019 at 11:55, Dr. David Alan Gilbert (git)
> <dgilbert@redhat.com> wrote:
> >
> > From: Wei Wang <wei.w.wang@intel.com>


Wei: Can you look at this....

> > The new feature enables the virtio-balloon device to receive hints of
> > guest free pages from the free page vq.
> >
> > A notifier is registered to the migration precopy notifier chain. The
> > notifier calls free_page_start after the migration thread syncs the dirty
> > bitmap, so that the free page optimization starts to clear bits of free
> > pages from the bitmap. It calls the free_page_stop before the migration
> > thread syncs the bitmap, which is the end of the current round of ram
> > save. The free_page_stop is also called to stop the optimization in the
> > case when there is an error occurred in the process of ram saving.
> >
> > Note: balloon will report pages which were free at the time of this call.
> > As the reporting happens asynchronously, dirty bit logging must be
> > enabled before this free_page_start call is made. Guest reporting must be
> > disabled before the migration dirty bitmap is synchronized.
> >
> > Signed-off-by: Wei Wang <wei.w.wang@intel.com>
> > CC: Michael S. Tsirkin <mst@redhat.com>
> > CC: Dr. David Alan Gilbert <dgilbert@redhat.com>
> > CC: Juan Quintela <quintela@redhat.com>
> > CC: Peter Xu <peterx@redhat.com>
> > Message-Id: <1544516693-5395-8-git-send-email-wei.w.wang@intel.com>
> > Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
> > Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> >   dgilbert: Dropped kernel header update, fixed up CMD_ID_* name change
> > ---
> 
> Hi -- Coverity points out a use-after-free here (CID 1399412):
> 
> > +static bool get_free_page_hints(VirtIOBalloon *dev)
> > +{
> > +    VirtQueueElement *elem;
> > +    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
> > +    VirtQueue *vq = dev->free_page_vq;
> > +
> > +    while (dev->block_iothread) {
> > +        qemu_cond_wait(&dev->free_page_cond, &dev->free_page_lock);
> > +    }
> > +
> > +    elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
> > +    if (!elem) {
> > +        return false;
> > +    }
> > +
> > +    if (elem->out_num) {
> > +        uint32_t id;
> > +        size_t size = iov_to_buf(elem->out_sg, elem->out_num, 0,
> > +                                 &id, sizeof(id));
> > +        virtqueue_push(vq, elem, size);
> > +        g_free(elem);
> 
> Here we free elem...
> 
> > +
> > +        virtio_tswap32s(vdev, &id);
> > +        if (unlikely(size != sizeof(id))) {
> > +            virtio_error(vdev, "received an incorrect cmd id");
> > +            return false;
> > +        }
> > +        if (id == dev->free_page_report_cmd_id) {
> > +            dev->free_page_report_status = FREE_PAGE_REPORT_S_START;
> > +        } else {
> > +            /*
> > +             * Stop the optimization only when it has started. This
> > +             * avoids a stale stop sign for the previous command.
> > +             */
> > +            if (dev->free_page_report_status == FREE_PAGE_REPORT_S_START) {
> > +                dev->free_page_report_status = FREE_PAGE_REPORT_S_STOP;
> > +            }
> > +        }
> > +    }
> > +
> > +    if (elem->in_num) {
> 
> ...but we can fall through here and try to dereference elem...
> 
> > +        if (dev->free_page_report_status == FREE_PAGE_REPORT_S_START) {
> > +            qemu_guest_free_page_hint(elem->in_sg[0].iov_base,
> > +                                      elem->in_sg[0].iov_len);
> > +        }
> > +        virtqueue_push(vq, elem, 1);
> > +        g_free(elem);
> 
> ...and then free it again.

OK, so the question here is:
  Is it allowed to have both in and out in the same queue element; if
it's not then we need to error the device.
  If it is allowed then we need to fix up the out_num case.

Dave

> > +    }
> > +
> > +    return true;
> > +}
> 
> thanks
> -- PMM
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK

  reply	other threads:[~2019-03-08 18:31 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-06 11:42 [Qemu-devel] [PULL 00/22] migration queue Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 01/22] migration: Fix cancel state Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 02/22] migration/rdma: Fix qemu_rdma_cleanup null check Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 03/22] migration: Cleanup during exit Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 04/22] migration/rdma: clang compilation fix Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 05/22] exec: Change RAMBlockIterFunc definition Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 06/22] migration: Introduce ignore-shared capability Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 07/22] migration: Add an ability to ignore shared RAM blocks Dr. David Alan Gilbert (git)
2019-03-08 17:12   ` Peter Maydell
2019-03-08 18:43     ` Dr. David Alan Gilbert
2019-03-06 11:42 ` [Qemu-devel] [PULL 08/22] tests/migration-test: Add a test for ignore-shared capability Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 09/22] migration: Add capabilities validation Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 10/22] tests: Add migration xbzrle test Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 11/22] migration: Create socket-address parameter Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 12/22] tests: Add basic migration precopy tcp test Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 13/22] bitmap: fix bitmap_count_one Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 14/22] bitmap: bitmap_count_one_with_offset Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 15/22] migration: use bitmap_mutex in migration_bitmap_clear_dirty Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 16/22] migration: API to clear bits of guest free pages from the dirty bitmap Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 17/22] migration/ram.c: add a notifier chain for precopy Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 18/22] migration/ram.c: add the free page optimization enable flag Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 19/22] virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT Dr. David Alan Gilbert (git)
2019-03-08 17:14   ` Peter Maydell
2019-03-08 18:30     ` Dr. David Alan Gilbert [this message]
2019-03-09 11:22       ` Wei Wang
2019-03-06 11:42 ` [Qemu-devel] [PULL 20/22] Migration/colo.c: Fix double close bug when occur COLO failover Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 21/22] Migration/colo.c: Make COLO node running after failover Dr. David Alan Gilbert (git)
2019-03-06 11:42 ` [Qemu-devel] [PULL 22/22] qapi/migration.json: Remove a variable that doesn't exist in example Dr. David Alan Gilbert (git)
2019-03-06 12:06 ` [Qemu-devel] [PULL 00/22] migration queue no-reply
2019-03-06 16:23 ` Peter Maydell

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=20190308183053.GI2834@work-vm \
    --to=dgilbert@redhat.com \
    --cc=armbru@redhat.com \
    --cc=chen.zhang@intel.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=peter.maydell@linaro.org \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=wei.w.wang@intel.com \
    --cc=yury-kotov@yandex-team.ru \
    /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).