From patchwork Tue Apr 19 14:20:41 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liang Li X-Patchwork-Id: 8880831 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 191609F36E for ; Tue, 19 Apr 2016 14:30:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D0CAC202AE for ; Tue, 19 Apr 2016 14:30:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5CD5F201B4 for ; Tue, 19 Apr 2016 14:30:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754645AbcDSOaK (ORCPT ); Tue, 19 Apr 2016 10:30:10 -0400 Received: from mga14.intel.com ([192.55.52.115]:36776 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754216AbcDSOaJ (ORCPT ); Tue, 19 Apr 2016 10:30:09 -0400 Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP; 19 Apr 2016 07:30:02 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.24,506,1455004800"; d="scan'208";a="87992160" Received: from ll.sh.intel.com (HELO localhost) ([10.239.13.27]) by fmsmga004.fm.intel.com with ESMTP; 19 Apr 2016 07:29:58 -0700 From: Liang Li To: mst@redhat.com, quintela@redhat.com, amit.shah@redhat.com, pbonzini@redhat.com, lcapitulino@redhat.com Cc: armbru@redhat.com, peter.maydell@linaro.org, rth@twiddle.net, ehabkost@redhat.com, james.hogan@imgtec.com, aurelien@aurel32.net, leon.alrae@imgtec.com, agraf@suse.de, borntraeger@de.ibm.com, cornelia.huck@de.ibm.com, qemu-devel@nongnu.org, kvm@vger.kernel.org, Liang Li Subject: [PATCH QEMU 3/5] virtio-balloon: Add a new feature to balloon device Date: Tue, 19 Apr 2016 22:20:41 +0800 Message-Id: <1461075643-3668-4-git-send-email-liang.z.li@intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1461075643-3668-1-git-send-email-liang.z.li@intel.com> References: <1461075643-3668-1-git-send-email-liang.z.li@intel.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Extend the virtio balloon device to support a new feature, this new feature can help to get guest's free pages information, which can be used for live migration optimzation. Signed-off-by: Liang Li --- balloon.c | 29 +++++++- hw/virtio/virtio-balloon.c | 92 ++++++++++++++++++++++++- include/hw/virtio/virtio-balloon.h | 30 +++++++- include/standard-headers/linux/virtio_balloon.h | 1 + include/sysemu/balloon.h | 12 +++- 5 files changed, 159 insertions(+), 5 deletions(-) diff --git a/balloon.c b/balloon.c index f2ef50c..346e215 100644 --- a/balloon.c +++ b/balloon.c @@ -36,6 +36,7 @@ static QEMUBalloonEvent *balloon_event_fn; static QEMUBalloonStatus *balloon_stat_fn; +static QEMUBalloonGetFreePage *balloon_get_free_page_fn; static void *balloon_opaque; static bool balloon_inhibited; @@ -65,9 +66,12 @@ static bool have_balloon(Error **errp) } int qemu_add_balloon_handler(QEMUBalloonEvent *event_func, - QEMUBalloonStatus *stat_func, void *opaque) + QEMUBalloonStatus *stat_func, + QEMUBalloonGetFreePage *get_free_page_func, + void *opaque) { - if (balloon_event_fn || balloon_stat_fn || balloon_opaque) { + if (balloon_event_fn || balloon_stat_fn || balloon_get_free_page_fn + || balloon_opaque) { /* We're already registered one balloon handler. How many can * a guest really have? */ @@ -75,6 +79,7 @@ int qemu_add_balloon_handler(QEMUBalloonEvent *event_func, } balloon_event_fn = event_func; balloon_stat_fn = stat_func; + balloon_get_free_page_fn = get_free_page_func; balloon_opaque = opaque; return 0; } @@ -86,6 +91,7 @@ void qemu_remove_balloon_handler(void *opaque) } balloon_event_fn = NULL; balloon_stat_fn = NULL; + balloon_get_free_page_fn = NULL; balloon_opaque = NULL; } @@ -116,3 +122,22 @@ void qmp_balloon(int64_t target, Error **errp) trace_balloon_event(balloon_opaque, target); balloon_event_fn(balloon_opaque, target); } + +bool balloon_free_pages_support(void) +{ + return balloon_get_free_page_fn ? true : false; +} + +FreePageStatus balloon_get_free_pages(unsigned long *bitmap, + unsigned long len, int drop_cache) +{ + if (!balloon_get_free_page_fn) { + return FREE_PAGE_UNSUPPORT; + } + + if (!bitmap) { + return FREE_PAGE_INVALID_PARAM; + } + + return balloon_get_free_page_fn(balloon_opaque, bitmap, len, drop_cache); +} diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c index 9dbe681..0abf375 100644 --- a/hw/virtio/virtio-balloon.c +++ b/hw/virtio/virtio-balloon.c @@ -78,6 +78,12 @@ static bool balloon_stats_supported(const VirtIOBalloon *s) return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ); } +static bool balloon_free_pages_supported(const VirtIOBalloon *s) +{ + VirtIODevice *vdev = VIRTIO_DEVICE(s); + return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_GET_FREE_PAGE); +} + static bool balloon_stats_enabled(const VirtIOBalloon *s) { return s->stats_poll_interval > 0; @@ -304,6 +310,38 @@ out: } } +static void virtio_balloon_get_free_pages(VirtIODevice *vdev, VirtQueue *vq) +{ + VirtIOBalloon *s = VIRTIO_BALLOON(vdev); + VirtQueueElement *elem; + size_t offset = 0; + uint64_t bitmap_bytes = 0; + + elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); + if (!elem) { + s->req_status = REQ_ERROR; + return; + } + + s->free_page_vq_elem = elem; + + if (!elem->out_num) { + return; + } + + iov_to_buf(elem->out_sg, elem->out_num, offset, + &bitmap_bytes, sizeof(uint64_t)); + + if (s->bmap_len < bitmap_bytes) { + s->req_status = REQ_INVALID_PARAM; + return; + } + offset += sizeof(uint64_t); + iov_to_buf(elem->out_sg, elem->out_num, offset, + s->free_page_bmap, bitmap_bytes); + s->req_status = REQ_DONE; +} + static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data) { VirtIOBalloon *dev = VIRTIO_BALLOON(vdev); @@ -373,6 +411,7 @@ static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f, VirtIOBalloon *dev = VIRTIO_BALLOON(vdev); f |= dev->host_features; virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ); + virtio_add_feature(&f, VIRTIO_BALLOON_F_GET_FREE_PAGE); return f; } @@ -383,6 +422,48 @@ static void virtio_balloon_stat(void *opaque, BalloonInfo *info) VIRTIO_BALLOON_PFN_SHIFT); } +static FreePageStatus virtio_balloon_free_pages(void *opaque, + unsigned long *bitmap, + unsigned long bmap_len, + int drop_cache_ctl) +{ + VirtIOBalloon *s = opaque; + VirtIODevice *vdev = VIRTIO_DEVICE(s); + VirtQueueElement *elem = s->free_page_vq_elem; + int len; + + if (!balloon_free_pages_supported(s)) { + return FREE_PAGE_UNSUPPORT; + } + + if (s->req_status == REQ_INIT) { + s->free_page_bmap = bitmap; + if (elem == NULL || !elem->in_num) { + elem = virtqueue_pop(s->fvq, sizeof(VirtQueueElement)); + if (!elem) { + return FREE_PAGE_ERROR; + } + s->free_page_vq_elem = elem; + } + s->free_page_req.param = drop_cache_ctl; + s->bmap_len = bmap_len; + len = iov_from_buf(elem->in_sg, elem->in_num, 0, &s->free_page_req, + sizeof(s->free_page_req)); + virtqueue_push(s->fvq, elem, len); + virtio_notify(vdev, s->fvq); + g_free(s->free_page_vq_elem); + s->free_page_vq_elem = NULL; + s->req_status = REQ_ON_GOING; + return FREE_PAGE_REQ; + } else if (s->req_status == REQ_ON_GOING) { + return FREE_PAGE_WAITING_FOR_RSP; + } else if (s->req_status == REQ_DONE) { + s->req_status = REQ_INIT; + } + + return FREE_PAGE_READY; +} + static void virtio_balloon_to_target(void *opaque, ram_addr_t target) { VirtIOBalloon *dev = VIRTIO_BALLOON(opaque); @@ -444,7 +525,8 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp) sizeof(struct virtio_balloon_config)); ret = qemu_add_balloon_handler(virtio_balloon_to_target, - virtio_balloon_stat, s); + virtio_balloon_stat, + virtio_balloon_free_pages, s); if (ret < 0) { error_setg(errp, "Only one balloon device is supported"); @@ -455,8 +537,10 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp) s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output); s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output); s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats); + s->fvq = virtio_add_queue(vdev, 128, virtio_balloon_get_free_pages); reset_stats(s); + s->req_status = REQ_INIT; register_savevm(dev, "virtio-balloon", -1, 1, virtio_balloon_save, virtio_balloon_load, s); @@ -481,6 +565,12 @@ static void virtio_balloon_device_reset(VirtIODevice *vdev) g_free(s->stats_vq_elem); s->stats_vq_elem = NULL; } + + if (s->free_page_vq_elem != NULL) { + g_free(s->free_page_vq_elem); + s->free_page_vq_elem = NULL; + } + s->req_status = REQ_INIT; } static void virtio_balloon_instance_init(Object *obj) diff --git a/include/hw/virtio/virtio-balloon.h b/include/hw/virtio/virtio-balloon.h index 35f62ac..fbed81c 100644 --- a/include/hw/virtio/virtio-balloon.h +++ b/include/hw/virtio/virtio-balloon.h @@ -23,6 +23,27 @@ #define VIRTIO_BALLOON(obj) \ OBJECT_CHECK(VirtIOBalloon, (obj), TYPE_VIRTIO_BALLOON) +typedef enum { + REQ_INIT, + REQ_ON_GOING, + REQ_DONE, + REQ_ERROR, + REQ_INVALID_PARAM, +} VirtIoReqStatus; + +typedef enum { + FREE_PAGE_REQ, + FREE_PAGE_WAITING_FOR_RSP, + FREE_PAGE_READY, + FREE_PAGE_INVALID_PARAM, + FREE_PAGE_UNSUPPORT, + FREE_PAGE_ERROR, +} FreePageStatus; + +typedef struct GetFreePageReq { + uint64_t param; +} GetFreePageReq; + typedef struct virtio_balloon_stat VirtIOBalloonStat; typedef struct virtio_balloon_stat_modern { @@ -33,16 +54,23 @@ typedef struct virtio_balloon_stat_modern { typedef struct VirtIOBalloon { VirtIODevice parent_obj; - VirtQueue *ivq, *dvq, *svq; + VirtQueue *ivq, *dvq, *svq, *fvq; uint32_t num_pages; uint32_t actual; uint64_t stats[VIRTIO_BALLOON_S_NR]; VirtQueueElement *stats_vq_elem; + VirtQueueElement *free_page_vq_elem; size_t stats_vq_offset; QEMUTimer *stats_timer; int64_t stats_last_update; int64_t stats_poll_interval; uint32_t host_features; + uint64_t *free_page_bmap; + uint64_t bmap_len; + uint64_t free_pages; + uint64_t cache_pages; + GetFreePageReq free_page_req; + VirtIoReqStatus req_status; } VirtIOBalloon; #endif diff --git a/include/standard-headers/linux/virtio_balloon.h b/include/standard-headers/linux/virtio_balloon.h index 9d06ccd..09211b0 100644 --- a/include/standard-headers/linux/virtio_balloon.h +++ b/include/standard-headers/linux/virtio_balloon.h @@ -34,6 +34,7 @@ #define VIRTIO_BALLOON_F_MUST_TELL_HOST 0 /* Tell before reclaiming pages */ #define VIRTIO_BALLOON_F_STATS_VQ 1 /* Memory Stats virtqueue */ #define VIRTIO_BALLOON_F_DEFLATE_ON_OOM 2 /* Deflate balloon on OOM */ +#define VIRTIO_BALLOON_F_GET_FREE_PAGE 3 /* Get the free page bitmap */ /* Size of a PFN in the balloon interface. */ #define VIRTIO_BALLOON_PFN_SHIFT 12 diff --git a/include/sysemu/balloon.h b/include/sysemu/balloon.h index 3f976b4..81a1b9f 100644 --- a/include/sysemu/balloon.h +++ b/include/sysemu/balloon.h @@ -15,14 +15,24 @@ #define _QEMU_BALLOON_H #include "qapi-types.h" +#include "hw/virtio/virtio-balloon.h" typedef void (QEMUBalloonEvent)(void *opaque, ram_addr_t target); typedef void (QEMUBalloonStatus)(void *opaque, BalloonInfo *info); +typedef FreePageStatus (QEMUBalloonGetFreePage)(void *opaque, + unsigned long *bitmap, + unsigned long len, + int drop_cache_ctl); int qemu_add_balloon_handler(QEMUBalloonEvent *event_func, - QEMUBalloonStatus *stat_func, void *opaque); + QEMUBalloonStatus *stat_func, + QEMUBalloonGetFreePage *get_free_page_func, + void *opaque); void qemu_remove_balloon_handler(void *opaque); bool qemu_balloon_is_inhibited(void); void qemu_balloon_inhibit(bool state); +bool balloon_free_pages_support(void); +FreePageStatus balloon_get_free_pages(unsigned long *bitmap, + unsigned long len, int drop_cache_ctl); #endif