From nobody Sat Feb 7 08:53:40 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520427327916678.8456848572308; Wed, 7 Mar 2018 04:55:27 -0800 (PST) Received: from localhost ([::1]:33041 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1etYbP-0006B7-47 for importer@patchew.org; Wed, 07 Mar 2018 07:55:27 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54710) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1etYZN-0004bt-HJ for qemu-devel@nongnu.org; Wed, 07 Mar 2018 07:53:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1etYZK-0000W7-3l for qemu-devel@nongnu.org; Wed, 07 Mar 2018 07:53:21 -0500 Received: from mga14.intel.com ([192.55.52.115]:58183) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1etYZJ-0000VH-QK for qemu-devel@nongnu.org; Wed, 07 Mar 2018 07:53:18 -0500 Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Mar 2018 04:53:17 -0800 Received: from devel-ww.sh.intel.com ([10.239.48.110]) by orsmga006.jf.intel.com with ESMTP; 07 Mar 2018 04:53:15 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,435,1515484800"; d="scan'208";a="23657126" From: Wei Wang To: qemu-devel@nongnu.org, virtio-dev@lists.oasis-open.org, mst@redhat.com, quintela@redhat.com, dgilbert@redhat.com Date: Wed, 7 Mar 2018 20:34:22 +0800 Message-Id: <1520426065-40265-2-git-send-email-wei.w.wang@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1520426065-40265-1-git-send-email-wei.w.wang@intel.com> References: <1520426065-40265-1-git-send-email-wei.w.wang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.115 Subject: [Qemu-devel] [PATCH v4 1/4] bitmap: bitmap_count_one_with_offset X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.zhang.wz@gmail.com, quan.xu0@gmail.com, liliang.opensource@gmail.com, wei.w.wang@intel.com, pbonzini@redhat.com, nilal@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Count the number of 1s in a bitmap starting from an offset. Signed-off-by: Wei Wang CC: Dr. David Alan Gilbert CC: Juan Quintela CC: Michael S. Tsirkin Reviewed-by: Dr. David Alan Gilbert --- include/qemu/bitmap.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/qemu/bitmap.h b/include/qemu/bitmap.h index 509eedd..e3f31f1 100644 --- a/include/qemu/bitmap.h +++ b/include/qemu/bitmap.h @@ -228,6 +228,19 @@ static inline long bitmap_count_one(const unsigned lon= g *bitmap, long nbits) } } =20 +static inline long bitmap_count_one_with_offset(const unsigned long *bitma= p, + long offset, long nbits) +{ + long aligned_offset =3D QEMU_ALIGN_DOWN(offset, BITS_PER_LONG); + long redundant_bits =3D offset - aligned_offset; + long bits_to_count =3D nbits + redundant_bits; + const unsigned long *bitmap_start =3D bitmap + + aligned_offset / BITS_PER_LONG; + + return bitmap_count_one(bitmap_start, bits_to_count) - + bitmap_count_one(bitmap_start, redundant_bits); +} + void bitmap_set(unsigned long *map, long i, long len); void bitmap_set_atomic(unsigned long *map, long i, long len); void bitmap_clear(unsigned long *map, long start, long nr); --=20 1.8.3.1 From nobody Sat Feb 7 08:53:40 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520427864035763.6195940528726; Wed, 7 Mar 2018 05:04:24 -0800 (PST) Received: from localhost ([::1]:33136 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1etYjz-0005or-0h for importer@patchew.org; Wed, 07 Mar 2018 08:04:19 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54714) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1etYZN-0004c2-I0 for qemu-devel@nongnu.org; Wed, 07 Mar 2018 07:53:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1etYZM-0000Wx-BZ for qemu-devel@nongnu.org; Wed, 07 Mar 2018 07:53:21 -0500 Received: from mga14.intel.com ([192.55.52.115]:58183) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1etYZM-0000VH-0w for qemu-devel@nongnu.org; Wed, 07 Mar 2018 07:53:20 -0500 Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Mar 2018 04:53:19 -0800 Received: from devel-ww.sh.intel.com ([10.239.48.110]) by orsmga006.jf.intel.com with ESMTP; 07 Mar 2018 04:53:17 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,435,1515484800"; d="scan'208";a="23657131" From: Wei Wang To: qemu-devel@nongnu.org, virtio-dev@lists.oasis-open.org, mst@redhat.com, quintela@redhat.com, dgilbert@redhat.com Date: Wed, 7 Mar 2018 20:34:23 +0800 Message-Id: <1520426065-40265-3-git-send-email-wei.w.wang@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1520426065-40265-1-git-send-email-wei.w.wang@intel.com> References: <1520426065-40265-1-git-send-email-wei.w.wang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.115 Subject: [Qemu-devel] [PATCH v4 2/4] migration: API to clear bits of guest free pages from the dirty bitmap X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.zhang.wz@gmail.com, quan.xu0@gmail.com, liliang.opensource@gmail.com, wei.w.wang@intel.com, pbonzini@redhat.com, nilal@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds an API to clear bits corresponding to guest free pages from the dirty bitmap. Spilt the free page block if it crosses the QEMU RAMBlock boundary. Signed-off-by: Wei Wang CC: Dr. David Alan Gilbert CC: Juan Quintela CC: Michael S. Tsirkin --- include/migration/misc.h | 2 ++ migration/ram.c | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/migration/misc.h b/include/migration/misc.h index 77fd4f5..fae1acf 100644 --- a/include/migration/misc.h +++ b/include/migration/misc.h @@ -14,11 +14,13 @@ #ifndef MIGRATION_MISC_H #define MIGRATION_MISC_H =20 +#include "exec/cpu-common.h" #include "qemu/notify.h" =20 /* migration/ram.c */ =20 void ram_mig_init(void); +void qemu_guest_free_page_hint(void *addr, size_t len); =20 /* migration/block.c */ =20 diff --git a/migration/ram.c b/migration/ram.c index 5e33e5c..e172798 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -2189,6 +2189,27 @@ static int ram_init_all(RAMState **rsp) return 0; } =20 +void qemu_guest_free_page_hint(void *addr, size_t len) +{ + RAMBlock *block; + ram_addr_t offset; + size_t used_len, start, npages; + + for (used_len =3D len; len > 0; len -=3D used_len) { + block =3D qemu_ram_block_from_host(addr, false, &offset); + if (unlikely(offset + len > block->used_length)) { + used_len =3D block->used_length - offset; + addr +=3D used_len; + } + + start =3D offset >> TARGET_PAGE_BITS; + npages =3D used_len >> TARGET_PAGE_BITS; + ram_state->migration_dirty_pages -=3D + bitmap_count_one_with_offset(block->bmap, start, npa= ges); + bitmap_clear(block->bmap, start, npages); + } +} + /* * Each of ram_save_setup, ram_save_iterate and ram_save_complete has * long-running RCU critical section. When rcu-reclaims in the code --=20 1.8.3.1 From nobody Sat Feb 7 08:53:40 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520427991311376.2543856443041; Wed, 7 Mar 2018 05:06:31 -0800 (PST) Received: from localhost ([::1]:33269 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1etYm1-00087S-Cp for importer@patchew.org; Wed, 07 Mar 2018 08:06:25 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54750) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1etYZW-0004yA-EJ for qemu-devel@nongnu.org; Wed, 07 Mar 2018 07:53:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1etYZP-0000Xw-04 for qemu-devel@nongnu.org; Wed, 07 Mar 2018 07:53:30 -0500 Received: from mga14.intel.com ([192.55.52.115]:58183) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1etYZO-0000VH-J7 for qemu-devel@nongnu.org; Wed, 07 Mar 2018 07:53:22 -0500 Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Mar 2018 04:53:21 -0800 Received: from devel-ww.sh.intel.com ([10.239.48.110]) by orsmga006.jf.intel.com with ESMTP; 07 Mar 2018 04:53:19 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,435,1515484800"; d="scan'208";a="23657139" From: Wei Wang To: qemu-devel@nongnu.org, virtio-dev@lists.oasis-open.org, mst@redhat.com, quintela@redhat.com, dgilbert@redhat.com Date: Wed, 7 Mar 2018 20:34:24 +0800 Message-Id: <1520426065-40265-4-git-send-email-wei.w.wang@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1520426065-40265-1-git-send-email-wei.w.wang@intel.com> References: <1520426065-40265-1-git-send-email-wei.w.wang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.115 Subject: [Qemu-devel] [PATCH v4 3/4] virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.zhang.wz@gmail.com, quan.xu0@gmail.com, liliang.opensource@gmail.com, wei.w.wang@intel.com, pbonzini@redhat.com, nilal@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The new feature enables the virtio-balloon device to receive hints of guest free pages from the free page vq. balloon_free_page_start - start guest free page hint reporting. balloon_free_page_stop - stop guest free page hint reporting. 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 call is made. Guest reporting must be disabled before the migration dirty bitmap is synchronized. Signed-off-by: Wei Wang Signed-off-by: Liang Li CC: Michael S. Tsirkin CC: Dr. David Alan Gilbert CC: Juan Quintela --- balloon.c | 49 +++++-- hw/virtio/virtio-balloon.c | 183 ++++++++++++++++++++= +--- include/hw/virtio/virtio-balloon.h | 15 +- include/standard-headers/linux/virtio_balloon.h | 7 + include/sysemu/balloon.h | 15 +- 5 files changed, 240 insertions(+), 29 deletions(-) diff --git a/balloon.c b/balloon.c index d8dd6fe..b0b0749 100644 --- a/balloon.c +++ b/balloon.c @@ -36,6 +36,9 @@ =20 static QEMUBalloonEvent *balloon_event_fn; static QEMUBalloonStatus *balloon_stat_fn; +static QEMUBalloonFreePageSupport *balloon_free_page_support_fn; +static QEMUBalloonFreePageStart *balloon_free_page_start_fn; +static QEMUBalloonFreePageStop *balloon_free_page_stop_fn; static void *balloon_opaque; static bool balloon_inhibited; =20 @@ -64,19 +67,42 @@ static bool have_balloon(Error **errp) return true; } =20 -int qemu_add_balloon_handler(QEMUBalloonEvent *event_func, - QEMUBalloonStatus *stat_func, void *opaque) +bool balloon_free_page_support(void) { - if (balloon_event_fn || balloon_stat_fn || balloon_opaque) { - /* We're already registered one balloon handler. How many can - * a guest really have? - */ - return -1; + return balloon_free_page_support_fn && + balloon_free_page_support_fn(balloon_opaque); +} + +void balloon_free_page_start(void) +{ + balloon_free_page_start_fn(balloon_opaque); +} + +void balloon_free_page_stop(void) +{ + balloon_free_page_stop_fn(balloon_opaque); +} + +void qemu_add_balloon_handler(QEMUBalloonEvent *event_fn, + QEMUBalloonStatus *stat_fn, + QEMUBalloonFreePageSupport *free_page_suppor= t_fn, + QEMUBalloonFreePageStart *free_page_start_fn, + QEMUBalloonFreePageStop *free_page_stop_fn, + void *opaque) +{ + if (balloon_event_fn || balloon_stat_fn || balloon_free_page_support_f= n || + balloon_free_page_start_fn || balloon_free_page_stop_fn || + balloon_opaque) { + /* We already registered one balloon handler. */ + return; } - balloon_event_fn =3D event_func; - balloon_stat_fn =3D stat_func; + + balloon_event_fn =3D event_fn; + balloon_stat_fn =3D stat_fn; + balloon_free_page_support_fn =3D free_page_support_fn; + balloon_free_page_start_fn =3D free_page_start_fn; + balloon_free_page_stop_fn =3D free_page_stop_fn; balloon_opaque =3D opaque; - return 0; } =20 void qemu_remove_balloon_handler(void *opaque) @@ -86,6 +112,9 @@ void qemu_remove_balloon_handler(void *opaque) } balloon_event_fn =3D NULL; balloon_stat_fn =3D NULL; + balloon_free_page_support_fn =3D NULL; + balloon_free_page_start_fn =3D NULL; + balloon_free_page_stop_fn =3D NULL; balloon_opaque =3D NULL; } =20 diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c index 4822449..48ed2ec 100644 --- a/hw/virtio/virtio-balloon.c +++ b/hw/virtio/virtio-balloon.c @@ -31,6 +31,7 @@ =20 #include "hw/virtio/virtio-bus.h" #include "hw/virtio/virtio-access.h" +#include "migration/misc.h" =20 #define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT) =20 @@ -308,6 +309,111 @@ out: } } =20 +static void *virtio_balloon_poll_free_page_hints(void *opaque) +{ + VirtQueueElement *elem; + VirtIOBalloon *dev =3D opaque; + VirtQueue *vq =3D dev->free_page_vq; + uint32_t id; + size_t size; + + /* + * Poll the vq till the status changed to STOP. This happens when + * the guest finishes reporting hints or the migration thread actively + * stops the reporting. + */ + while (dev->free_page_report_status < FREE_PAGE_REPORT_S_STOP) { + elem =3D virtqueue_pop(vq, sizeof(VirtQueueElement)); + if (!elem) { + continue; + } + + if (elem->out_num) { + size =3D iov_to_buf(elem->out_sg, elem->out_num, 0, &id, sizeo= f(id)); + virtqueue_push(vq, elem, size); + g_free(elem); + if (unlikely(size !=3D sizeof(id))) { + warn_report("%s: received an incorrect cmd id", __func__); + break; + } + if (id =3D=3D dev->free_page_report_cmd_id) { + dev->free_page_report_status =3D FREE_PAGE_REPORT_S_START; + } else if (dev->free_page_report_status =3D=3D + FREE_PAGE_REPORT_S_START) { + /* + * Stop the optimization only when it has started. This av= oids + * obsolete stop sign for the previous command. + */ + dev->free_page_report_status =3D FREE_PAGE_REPORT_S_STOP; + break; + } + } + + if (elem->in_num) { + if (dev->free_page_report_status =3D=3D FREE_PAGE_REPORT_S_STA= RT && + !dev->poison_val) { + qemu_guest_free_page_hint(elem->in_sg[0].iov_base, + elem->in_sg[0].iov_len); + } + virtqueue_push(vq, elem, 0); + g_free(elem); + } + } + return NULL; +} + +static bool virtio_balloon_free_page_support(void *opaque) +{ + VirtIOBalloon *s =3D opaque; + VirtIODevice *vdev =3D VIRTIO_DEVICE(s); + + return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT); +} + +static void virtio_balloon_free_page_start(void *opaque) +{ + VirtIOBalloon *s =3D opaque; + VirtIODevice *vdev =3D VIRTIO_DEVICE(s); + + if (unlikely(s->free_page_report_cmd_id =3D=3D UINT_MAX)) { + s->free_page_report_cmd_id =3D + VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN; + } else { + s->free_page_report_cmd_id++; + } + + s->free_page_report_status =3D FREE_PAGE_REPORT_S_REQUESTED; + virtio_notify_config(vdev); + qemu_thread_create(&s->free_page_thread, "free_page_optimization_threa= d", + virtio_balloon_poll_free_page_hints, s, + QEMU_THREAD_JOINABLE); +} + +static void virtio_balloon_free_page_stop(void *opaque) +{ + VirtIOBalloon *s =3D opaque; + VirtIODevice *vdev =3D VIRTIO_DEVICE(s); + + switch (s->free_page_report_status) { + case FREE_PAGE_REPORT_S_REQUESTED: + case FREE_PAGE_REPORT_S_START: + /* + * The guest hasn't done the reporting, so host sends a notificati= on + * to the guest to actively stop the reporting before joining the + * optimization thread. + */ + s->free_page_report_status =3D FREE_PAGE_REPORT_S_STOP; + virtio_notify_config(vdev); + case FREE_PAGE_REPORT_S_STOP: + /* The guest has stopped the reporting. Join the optimization thre= ad */ + qemu_thread_join(&s->free_page_thread); + s->free_page_report_status =3D FREE_PAGE_REPORT_S_EXIT; + case FREE_PAGE_REPORT_S_EXIT: + /* The optimization thread has gone. No actions needded so far. */ + break; + } +} + static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_= data) { VirtIOBalloon *dev =3D VIRTIO_BALLOON(vdev); @@ -315,6 +421,15 @@ static void virtio_balloon_get_config(VirtIODevice *vd= ev, uint8_t *config_data) =20 config.num_pages =3D cpu_to_le32(dev->num_pages); config.actual =3D cpu_to_le32(dev->actual); + config.poison_val =3D cpu_to_le32(dev->poison_val); + + if (dev->free_page_report_status >=3D FREE_PAGE_REPORT_S_STOP) { + config.free_page_report_cmd_id =3D + cpu_to_le32(VIRTIO_BALLOON_FREE_PAGE_REPORT_STOP_ID= ); + } else { + config.free_page_report_cmd_id =3D + cpu_to_le32(dev->free_page_report_cmd_id); + } =20 trace_virtio_balloon_get_config(config.num_pages, config.actual); memcpy(config_data, &config, sizeof(struct virtio_balloon_config)); @@ -368,6 +483,7 @@ static void virtio_balloon_set_config(VirtIODevice *vde= v, ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SH= IFT), &error_abort); } + dev->poison_val =3D le32_to_cpu(config.poison_val); trace_virtio_balloon_set_config(dev->actual, oldactual); } =20 @@ -377,6 +493,11 @@ static uint64_t virtio_balloon_get_features(VirtIODevi= ce *vdev, uint64_t f, VirtIOBalloon *dev =3D VIRTIO_BALLOON(vdev); f |=3D dev->host_features; virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ); + + if (dev->host_features & 1ULL << VIRTIO_BALLOON_F_FREE_PAGE_HINT) { + virtio_add_feature(&f, VIRTIO_BALLOON_F_PAGE_POISON); + } + return f; } =20 @@ -413,6 +534,18 @@ static int virtio_balloon_post_load_device(void *opaqu= e, int version_id) return 0; } =20 +static const VMStateDescription vmstate_virtio_balloon_free_page_report = =3D { + .name =3D "virtio-balloon-device/free-page-report", + .version_id =3D 1, + .minimum_version_id =3D 1, + .needed =3D virtio_balloon_free_page_support, + .fields =3D (VMStateField[]) { + VMSTATE_UINT32(free_page_report_cmd_id, VirtIOBalloon), + VMSTATE_UINT32(poison_val, VirtIOBalloon), + VMSTATE_END_OF_LIST() + } +}; + static const VMStateDescription vmstate_virtio_balloon_device =3D { .name =3D "virtio-balloon-device", .version_id =3D 1, @@ -423,30 +556,30 @@ static const VMStateDescription vmstate_virtio_balloo= n_device =3D { VMSTATE_UINT32(actual, VirtIOBalloon), VMSTATE_END_OF_LIST() }, + .subsections =3D (const VMStateDescription * []) { + &vmstate_virtio_balloon_free_page_report, + NULL + } }; =20 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp) { VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); VirtIOBalloon *s =3D VIRTIO_BALLOON(dev); - int ret; =20 virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON, sizeof(struct virtio_balloon_config)); =20 - ret =3D qemu_add_balloon_handler(virtio_balloon_to_target, - virtio_balloon_stat, s); - - if (ret < 0) { - error_setg(errp, "Only one balloon device is supported"); - virtio_cleanup(vdev); - return; - } - s->ivq =3D virtio_add_queue(vdev, 128, virtio_balloon_handle_output); s->dvq =3D virtio_add_queue(vdev, 128, virtio_balloon_handle_output); s->svq =3D virtio_add_queue(vdev, 128, virtio_balloon_receive_stats); - + if (virtio_has_feature(s->host_features, + VIRTIO_BALLOON_F_FREE_PAGE_HINT)) { + s->free_page_vq =3D virtio_add_queue(vdev, VIRTQUEUE_MAX_SIZE, NUL= L); + s->free_page_report_status =3D FREE_PAGE_REPORT_S_EXIT; + s->free_page_report_cmd_id =3D + VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN - 1; + } reset_stats(s); } =20 @@ -475,11 +608,27 @@ static void virtio_balloon_set_status(VirtIODevice *v= dev, uint8_t status) { VirtIOBalloon *s =3D VIRTIO_BALLOON(vdev); =20 - if (!s->stats_vq_elem && vdev->vm_running && - (status & VIRTIO_CONFIG_S_DRIVER_OK) && virtqueue_rewind(s->svq, 1= )) { - /* poll stats queue for the element we have discarded when the VM - * was stopped */ - virtio_balloon_receive_stats(vdev, s->svq); + if (status & VIRTIO_CONFIG_S_DRIVER_OK) { + if (!s->stats_vq_elem && vdev->vm_running && + virtqueue_rewind(s->svq, 1)) { + /* + * Poll stats queue for the element we have discarded when the= VM + * was stopped. + */ + virtio_balloon_receive_stats(vdev, s->svq); + } + + if (virtio_balloon_free_page_support(s)) { + qemu_add_balloon_handler(virtio_balloon_to_target, + virtio_balloon_stat, + virtio_balloon_free_page_support, + virtio_balloon_free_page_start, + virtio_balloon_free_page_stop, + s); + } else { + qemu_add_balloon_handler(virtio_balloon_to_target, + virtio_balloon_stat, NULL, NULL, NULL= , s); + } } } =20 @@ -509,6 +658,8 @@ static const VMStateDescription vmstate_virtio_balloon = =3D { static Property virtio_balloon_properties[] =3D { DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon, host_features, VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false), + DEFINE_PROP_BIT("free-page-hint", VirtIOBalloon, host_features, + VIRTIO_BALLOON_F_FREE_PAGE_HINT, false), DEFINE_PROP_END_OF_LIST(), }; =20 diff --git a/include/hw/virtio/virtio-balloon.h b/include/hw/virtio/virtio-= balloon.h index 1ea13bd..12fde2f 100644 --- a/include/hw/virtio/virtio-balloon.h +++ b/include/hw/virtio/virtio-balloon.h @@ -23,6 +23,8 @@ #define VIRTIO_BALLOON(obj) \ OBJECT_CHECK(VirtIOBalloon, (obj), TYPE_VIRTIO_BALLOON) =20 +#define VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN 0x80000000 + typedef struct virtio_balloon_stat VirtIOBalloonStat; =20 typedef struct virtio_balloon_stat_modern { @@ -31,15 +33,26 @@ typedef struct virtio_balloon_stat_modern { uint64_t val; } VirtIOBalloonStatModern; =20 +enum virtio_balloon_free_page_report_status { + FREE_PAGE_REPORT_S_REQUESTED, + FREE_PAGE_REPORT_S_START, + FREE_PAGE_REPORT_S_STOP, + FREE_PAGE_REPORT_S_EXIT, +}; + typedef struct VirtIOBalloon { VirtIODevice parent_obj; - VirtQueue *ivq, *dvq, *svq; + VirtQueue *ivq, *dvq, *svq, *free_page_vq; + uint32_t free_page_report_status; uint32_t num_pages; uint32_t actual; + uint32_t free_page_report_cmd_id; + uint32_t poison_val; uint64_t stats[VIRTIO_BALLOON_S_NR]; VirtQueueElement *stats_vq_elem; size_t stats_vq_offset; QEMUTimer *stats_timer; + QemuThread free_page_thread; int64_t stats_last_update; int64_t stats_poll_interval; uint32_t host_features; diff --git a/include/standard-headers/linux/virtio_balloon.h b/include/stan= dard-headers/linux/virtio_balloon.h index 7b0a41b..f89e80f 100644 --- a/include/standard-headers/linux/virtio_balloon.h +++ b/include/standard-headers/linux/virtio_balloon.h @@ -34,15 +34,22 @@ #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_FREE_PAGE_HINT 3 /* VQ to report free pages */ +#define VIRTIO_BALLOON_F_PAGE_POISON 4 /* Guest is using page poisoning */ =20 /* Size of a PFN in the balloon interface. */ #define VIRTIO_BALLOON_PFN_SHIFT 12 =20 +#define VIRTIO_BALLOON_FREE_PAGE_REPORT_STOP_ID 0 struct virtio_balloon_config { /* Number of pages host wants Guest to give up. */ uint32_t num_pages; /* Number of pages we've actually got in balloon. */ uint32_t actual; + /* Free page report command id, readonly by guest */ + uint32_t free_page_report_cmd_id; + /* Stores PAGE_POISON if page poisoning is in use */ + uint32_t poison_val; }; =20 #define VIRTIO_BALLOON_S_SWAP_IN 0 /* Amount of memory swapped in */ diff --git a/include/sysemu/balloon.h b/include/sysemu/balloon.h index af49e19..16a2aae 100644 --- a/include/sysemu/balloon.h +++ b/include/sysemu/balloon.h @@ -18,11 +18,22 @@ =20 typedef void (QEMUBalloonEvent)(void *opaque, ram_addr_t target); typedef void (QEMUBalloonStatus)(void *opaque, BalloonInfo *info); +typedef bool (QEMUBalloonFreePageSupport)(void *opaque); +typedef void (QEMUBalloonFreePageStart)(void *opaque); +typedef void (QEMUBalloonFreePageStop)(void *opaque); =20 -int qemu_add_balloon_handler(QEMUBalloonEvent *event_func, - QEMUBalloonStatus *stat_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_page_support(void); +void balloon_free_page_start(void); +void balloon_free_page_stop(void); + +void qemu_add_balloon_handler(QEMUBalloonEvent *event_fn, + QEMUBalloonStatus *stat_fn, + QEMUBalloonFreePageSupport *free_page_suppor= t_fn, + QEMUBalloonFreePageStart *free_page_start_fn, + QEMUBalloonFreePageStop *free_page_stop_fn, + void *opaque); =20 #endif --=20 1.8.3.1 From nobody Sat Feb 7 08:53:40 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520427484520631.1215464581771; Wed, 7 Mar 2018 04:58:04 -0800 (PST) Received: from localhost ([::1]:33058 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1etYdv-0000Et-P1 for importer@patchew.org; Wed, 07 Mar 2018 07:58:03 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54739) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1etYZS-0004t5-3l for qemu-devel@nongnu.org; Wed, 07 Mar 2018 07:53:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1etYZR-0000YQ-5i for qemu-devel@nongnu.org; Wed, 07 Mar 2018 07:53:26 -0500 Received: from mga14.intel.com ([192.55.52.115]:58183) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1etYZQ-0000VH-MQ for qemu-devel@nongnu.org; Wed, 07 Mar 2018 07:53:24 -0500 Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Mar 2018 04:53:24 -0800 Received: from devel-ww.sh.intel.com ([10.239.48.110]) by orsmga006.jf.intel.com with ESMTP; 07 Mar 2018 04:53:22 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,435,1515484800"; d="scan'208";a="23657153" From: Wei Wang To: qemu-devel@nongnu.org, virtio-dev@lists.oasis-open.org, mst@redhat.com, quintela@redhat.com, dgilbert@redhat.com Date: Wed, 7 Mar 2018 20:34:25 +0800 Message-Id: <1520426065-40265-5-git-send-email-wei.w.wang@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1520426065-40265-1-git-send-email-wei.w.wang@intel.com> References: <1520426065-40265-1-git-send-email-wei.w.wang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.115 Subject: [Qemu-devel] [PATCH v4 4/4] migration: use the free page hint feature from balloon X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.zhang.wz@gmail.com, quan.xu0@gmail.com, liliang.opensource@gmail.com, wei.w.wang@intel.com, pbonzini@redhat.com, nilal@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Start the free page optimization after the migration bitmap is synchronized. This can't be used in the stop© phase since the guest is paused. Make sure the guest reporting has stopped before synchronizing the migration dirty bitmap. Currently, the optimization is added to precopy only. Signed-off-by: Wei Wang CC: Dr. David Alan Gilbert CC: Juan Quintela CC: Michael S. Tsirkin --- migration/ram.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/migration/ram.c b/migration/ram.c index e172798..7b4c9b1 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -51,6 +51,8 @@ #include "qemu/rcu_queue.h" #include "migration/colo.h" #include "migration/block.h" +#include "sysemu/balloon.h" +#include "sysemu/sysemu.h" =20 /***********************************************************/ /* ram save/restore */ @@ -208,6 +210,8 @@ struct RAMState { uint32_t last_version; /* We are in the first round */ bool ram_bulk_stage; + /* The free pages optimization feature is supported */ + bool free_page_support; /* How many times we have dirty too many pages */ int dirty_rate_high_cnt; /* these variables are used for bitmap sync */ @@ -775,7 +779,7 @@ unsigned long migration_bitmap_find_dirty(RAMState *rs,= RAMBlock *rb, unsigned long *bitmap =3D rb->bmap; unsigned long next; =20 - if (rs->ram_bulk_stage && start > 0) { + if (rs->ram_bulk_stage && start > 0 && !rs->free_page_support) { next =3D start + 1; } else { next =3D find_next_bit(bitmap, size, start); @@ -833,6 +837,10 @@ static void migration_bitmap_sync(RAMState *rs) int64_t end_time; uint64_t bytes_xfer_now; =20 + if (rs->free_page_support) { + balloon_free_page_stop(); + } + ram_counters.dirty_sync_count++; =20 if (!rs->time_last_bitmap_sync) { @@ -899,6 +907,10 @@ static void migration_bitmap_sync(RAMState *rs) if (migrate_use_events()) { qapi_event_send_migration_pass(ram_counters.dirty_sync_count, NULL= ); } + + if (rs->free_page_support && runstate_is_running()) { + balloon_free_page_start(); + } } =20 /** @@ -1656,6 +1668,8 @@ static void ram_state_reset(RAMState *rs) rs->last_page =3D 0; rs->last_version =3D ram_list.version; rs->ram_bulk_stage =3D true; + rs->free_page_support =3D balloon_free_page_support() & + !migration_in_postcopy(); } =20 #define MAX_WAIT 50 /* ms, half buffered_file limit */ @@ -2330,6 +2344,9 @@ static int ram_save_iterate(QEMUFile *f, void *opaque) =20 ret =3D qemu_file_get_error(f); if (ret < 0) { + if (rs->free_page_support) { + balloon_free_page_stop(); + } return ret; } =20 --=20 1.8.3.1