From nobody Mon Feb 9 10:53:37 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zoho.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 1486136398574123.8042821980905; Fri, 3 Feb 2017 07:39:58 -0800 (PST) Received: from localhost ([::1]:35343 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZfxr-0000pa-54 for importer@patchew.org; Fri, 03 Feb 2017 10:39:55 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33745) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZfvu-0008H7-Kp for qemu-devel@nongnu.org; Fri, 03 Feb 2017 10:37:56 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZfvr-0002Yd-9q for qemu-devel@nongnu.org; Fri, 03 Feb 2017 10:37:54 -0500 Received: from mail-db5eur01on0093.outbound.protection.outlook.com ([104.47.2.93]:17933 helo=EUR01-DB5-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZfvq-0002W0-DQ for qemu-devel@nongnu.org; Fri, 03 Feb 2017 10:37:51 -0500 Received: from pavelb-Z68P-DS3.sw.ru (195.214.232.6) by VI1PR0802MB2559.eurprd08.prod.outlook.com (10.172.255.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.874.12; Fri, 3 Feb 2017 15:23:39 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=FxGB00PJTfIlYGMST5M3lvS+EKS5M81GVBiL5CPW8qA=; b=dmVExLZZ/Am8nb60BrbOJtCBrh//NKAhqUOYmF2j6pnTQw5KucHnHW4jfuA22poD1cuaW2Y4Ka+4wJAg1aDHtx4jTC3zSXDh47F7hp78z7DmJDut100sdy/ble+dWNhgh+suQKFSRD+H/uJDwmyUfBfevC/EmtXQe2Fo3noIOR4= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=pbutsykin@virtuozzo.com; From: Pavel Butsykin To: Date: Fri, 3 Feb 2017 18:23:20 +0300 Message-ID: <20170203152321.19739-3-pbutsykin@virtuozzo.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170203152321.19739-1-pbutsykin@virtuozzo.com> References: <20170203152321.19739-1-pbutsykin@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: VI1PR0901CA0084.eurprd09.prod.outlook.com (10.167.203.180) To VI1PR0802MB2559.eurprd08.prod.outlook.com (10.172.255.137) X-MS-Office365-Filtering-Correlation-Id: 7b0094e3-194d-48fe-afa4-08d44c48a1fd X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:VI1PR0802MB2559; X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2559; 3:JiKGvvSKAaiCxn+G0M+/Ln1T4cobLa+Zji/YGg0D9LG0R0cV0X/Rm7CVXckJW4aqC9ZrXHTIz8RLXaC/W8siRKQWWEqfHQlHPVP8sJwpRK7bRWi0ZOdbRjIYEIjRxOz6chzdizR7feJDKFZ81v4v2wXCo55mOTAL99a3opbGMnRp9mNjQ7yRqffhb+EU1sehDJP8OzBlNQs/myD555566cQn/9gNtp1ORGr3g5Pg0Ic5hsboqhgO66e8YWMLxV1lodaji6a6zbn47jGhper/2g==; 25:qgVtWaRV9eM9f5NZiCopY10ya6xYl+eWsC46gxltiHGc5uy4SRpRMyGiTw1cM/DfNDSBR1ZD0isFGkRh3HqMeA1RkhuQw7TR/gy11DGdOa2c/YZUcMGEps0+QKvF52OU6a71uCit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-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046)(6041248)(20161123562025)(20161123555025)(20161123564025)(20161123560025)(20161123558025)(6072148); SRVR:VI1PR0802MB2559; BCL:0; PCL:0; RULEID:; SRVR:VI1PR0802MB2559; X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2559; 4:jB7txotbUbzVAMKtz/Bab7zJl7IzMrov98uyoVVsg1KNNMW7lUTZvyVU7NxntxZmyLvlqnxAcE9c3BfFeO4WzQrPZKsvVIgT+00tqd5cNuSWPLnmHNkyCwQj2Vdm7/LyWgN3Ce84ofrhl+bQVUtWbwDgoP6ZoWbF9PG4aZxVeWP161szVhjNBFw+1P770vWXsh1yA4XUkW7XeVlJv42q1m6aT6H21qjWT+LRHB6k4wVG3fFYpCirwP70bUPPqZWTjCLBuTa5SlyI0ZH9WTDmaQ2ND3UOR6ju9fQHJkNyGFObNWKS+TL1VGkP9P5BUwdXaT7Ck7Q23QtAMjoShYdXLdUgPUXZAhMtPlsNQTWa+dG92gnp4OltWgct+HpiXH6IElj+d3rIx6TLEePi71iARps/7HCfz1PIShd2sjcy66lBw1guSyOE2+koq5gUW9JTLlw32Hce4D8oqJ3M6/uHc+mcFgVmv/93JEJgPDRf7jgz13F9oNTmsQ2oBHk+gN4xqomkEzq2TuwkfAeVosnW4Hv25F9nyO/ZUQgcPMGBnQYn029k8zKcz7QZft2XYCOjlrBlv7LtDJ3C9x07GPVGtweEcNr1bwOcPJaeOxA42to= X-Forefront-PRVS: 02070414A1 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6009001)(7916002)(39450400003)(189002)(199003)(6116002)(48376002)(6486002)(2351001)(189998001)(68736007)(6506006)(38730400001)(50226002)(25786008)(54906002)(36756003)(53416004)(7736002)(106356001)(305945005)(8676002)(105586002)(101416001)(5003940100001)(53936002)(69596002)(42186005)(76176999)(575784001)(50986999)(107886002)(81166006)(86362001)(2950100002)(4326007)(110136003)(81156014)(6666003)(2906002)(6916009)(5660300001)(33646002)(50466002)(92566002)(66066001)(97736004)(6512007)(1076002)(3846002)(4001430100002)(47776003); DIR:OUT; SFP:1102; SCL:1; SRVR:VI1PR0802MB2559; H:pavelb-Z68P-DS3.sw.ru; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; 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; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; VI1PR0802MB2559; 23:fyKqRoNh/WMeiGUtUDDhty1xb+D202OSCTbDEpW?= =?us-ascii?Q?LXesvbxu6e5VfXLjylEMf6q4Lei3MR9YgK4XZLofbVt67KNJXPJk2iR715el?= =?us-ascii?Q?uoudXr3bKZLv9UUgHAyempet6LFFUr1ypjdBte+WhRrCS3tgNlHOX0dp/ZcX?= =?us-ascii?Q?HPOkE4Xa408KHjG3DN2mO5nD/2vDS4RJkeVnOdh/htPn0zAKjq6feUogLWaW?= =?us-ascii?Q?WddwP1KMfgim8gJ9V0UCW84vtm7B9axAoZItHONMCr8kjB8NeWvPHd/JDd7o?= =?us-ascii?Q?dIIA3oBcq9lehXcPaKvTLfuLZwuGT/zW7q6ZcyPJzX+HMTTXj4FHb5mnAxnT?= =?us-ascii?Q?E5jLxOhsXGy4kM4nAVZ/mnxYUpkYjucQvvvJht5tPF33e7AaRofelKvhcyL3?= =?us-ascii?Q?QMqOnGDA920XfA9mTQJbpC3hc4REO8XY4KvqACOIjbQxZRpM7oc2a/3ppcG8?= =?us-ascii?Q?ANtSAGZVCRD7UO4rkfVGDUGPWaEsqFVa67E/eANK9Jrek/CHVLKEL4Ew15aQ?= =?us-ascii?Q?/Nsgvza7Eudtm/QhTkEostqa8rfrYQsYxXJirIo1zfx8zak369r4DU8W0mff?= =?us-ascii?Q?GX20Y2H1A4uzJY8NoZs4+sG/ANFZi1pRNgRciaHGeD2rgWKn7tJyH5Hg9exH?= =?us-ascii?Q?SWi3fmP2vmEaGyDjcqaRedsry65uUzfGGti3O4wdgesSjujufDQheGY4TDqM?= =?us-ascii?Q?sGX4vEbZ+t++bMXp4XUJ4CptIVoMZk1brQOYue891KE1xL9OPTkwPLqJhfoR?= =?us-ascii?Q?6RwuDqBI51vgJMo6Uu5YlmBWBpzLC2iWEqzb33cJI9Xv2SAEnBhRE7Qx7qA+?= =?us-ascii?Q?e6IWEu4t1U5lH+NldW1vPofQxq3lPFgnIMLWvoxN7hHDX4JNf9LjlfYfmYaD?= =?us-ascii?Q?FIHtJXdN8CTtWQgeFHLnZfQWirJtSPnCTMTc4bF78717K8MrC3jpv9v1mBcC?= =?us-ascii?Q?KoYtiLhRUznkgouRDnFTx2BjGPeFVCUlaN6tJQ3KVuWYW1lsbqneUYznxPDn?= =?us-ascii?Q?2eFqoMP1276+1AgYpHMENof2Oj787tUSyRpkjtTFeYyHve8JjtT62K1es7Bj?= =?us-ascii?Q?RC24xVoxPXhy8BsDDKJka+uKUMqIrQA//9D7b9nN6MBd3vf9EiS9N1L0hyNS?= =?us-ascii?Q?JZefTkXBqom8tIs4GPlzT6vwZ62ir+yMD4rmxd7YMmnmvMTxY9h8hwmHTbHm?= =?us-ascii?Q?tPeWSnZ23krIiurI+Nv2dQRkqJY8OSjeZeSdmi3hmzQDxjpn38XFCAaxMn5a?= =?us-ascii?Q?sR5IsN+FF/EICnKQ4k7rE1J0HDbazwwlxrOxoSgQi?= X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2559; 6:izTAROhfs49yogbWrr11FmliA//bN5PCRgBsjvDlMT03ya4HzpNzSqEyK1pLgne/4pGy44GRDmeinYBkf0qVFb4zsyy/6lkkSYLoQZJ9vMt3PvVzKU6INCTBAQ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q0Pw3Bh4F2nSQfUj23sEi2VNHhH5o4Kf+l7N95V6G8LR8cG9EKn3s7HA2VUkiF3qebDKpMntJxvi+7Z9Ro8Yis8AlJsnb5w==; 20:nJT4dZIE9Vjt5pXYdPa/je7n2f+c2g3iQSBA1G6Bxg2vxo4RlkCJS9hV7+F8EkOQOMriffaCW6XxfWWTURWR+DvdOUzPRWKowi5Tz9nMrp01GdE6S29odFFFo7uGoiO144AP7FHbPV4666mXkCPWeXkGMwVkWhZuM0W42u+XV4s= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Feb 2017 15:23:39.9801 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0802MB2559 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.2.93 Subject: [Qemu-devel] [PATCH v2 2/3] add 'release-ram' migrate capability 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: pbutsykin@virtuozzo.com, quintela@redhat.com, armbru@redhat.com, amit.shah@redhat.com, den@openvz.org 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 Content-Type: text/plain; charset="utf-8" This feature frees the migrated memory on the source during postcopy-ram migration. In the second step of postcopy-ram migration when the source vm is put on pause we can free unnecessary memory. It will allow, in particula= r, to start relaxing the memory stress on the source host in a load-balancing scenario. Signed-off-by: Pavel Butsykin Reviewed-by: Dr. David Alan Gilbert --- include/migration/migration.h | 1 + include/migration/qemu-file.h | 3 ++- migration/migration.c | 9 +++++++ migration/qemu-file.c | 59 ++++++++++++++++++++++++++++++++++++++-= ---- migration/ram.c | 22 +++++++++++++++- qapi-schema.json | 5 +++- 6 files changed, 89 insertions(+), 10 deletions(-) diff --git a/include/migration/migration.h b/include/migration/migration.h index bd399fc0df..401fbe1f77 100644 --- a/include/migration/migration.h +++ b/include/migration/migration.h @@ -307,6 +307,7 @@ int migrate_add_blocker(Error *reason, Error **errp); */ void migrate_del_blocker(Error *reason); =20 +bool migrate_release_ram(void); bool migrate_postcopy_ram(void); bool migrate_zero_blocks(void); =20 diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h index abedd466c9..0cd648a733 100644 --- a/include/migration/qemu-file.h +++ b/include/migration/qemu-file.h @@ -132,7 +132,8 @@ void qemu_put_byte(QEMUFile *f, int v); * put_buffer without copying the buffer. * The buffer should be available till it is sent asynchronously. */ -void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size); +void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size, + bool may_free); bool qemu_file_mode_is_not_valid(const char *mode); bool qemu_file_is_writable(QEMUFile *f); =20 diff --git a/migration/migration.c b/migration/migration.c index 1ae68be0c7..8d5a5f8a6e 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -1302,6 +1302,15 @@ void qmp_migrate_set_downtime(double value, Error **= errp) qmp_migrate_set_parameters(&p, errp); } =20 +bool migrate_release_ram(void) +{ + MigrationState *s; + + s =3D migrate_get_current(); + + return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM]; +} + bool migrate_postcopy_ram(void) { MigrationState *s; diff --git a/migration/qemu-file.c b/migration/qemu-file.c index e9fae31158..82dbef3c86 100644 --- a/migration/qemu-file.c +++ b/migration/qemu-file.c @@ -49,6 +49,7 @@ struct QEMUFile { int buf_size; /* 0 when writing */ uint8_t buf[IO_BUF_SIZE]; =20 + DECLARE_BITMAP(may_free, MAX_IOV_SIZE); struct iovec iov[MAX_IOV_SIZE]; unsigned int iovcnt; =20 @@ -132,6 +133,41 @@ bool qemu_file_is_writable(QEMUFile *f) return f->ops->writev_buffer; } =20 +static void qemu_iovec_release_ram(QEMUFile *f) +{ + struct iovec iov; + unsigned long idx; + + /* Find and release all the contiguous memory ranges marked as may_fre= e. */ + idx =3D find_next_bit(f->may_free, f->iovcnt, 0); + if (idx >=3D f->iovcnt) { + return; + } + iov =3D f->iov[idx]; + + /* The madvise() in the loop is called for iov within a continuous ran= ge and + * then reinitialize the iov. And in the end, madvise() is called for = the + * last iov. + */ + while ((idx =3D find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->i= ovcnt) { + /* check for adjacent buffer and coalesce them */ + if (iov.iov_base + iov.iov_len =3D=3D f->iov[idx].iov_base) { + iov.iov_len +=3D f->iov[idx].iov_len; + continue; + } + if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < = 0) { + error_report("migrate: madvise DONTNEED failed %p %ld: %s", + iov.iov_base, iov.iov_len, strerror(errno)); + } + iov =3D f->iov[idx]; + } + if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) { + error_report("migrate: madvise DONTNEED failed %p %ld: %s", + iov.iov_base, iov.iov_len, strerror(errno)); + } + memset(f->may_free, 0, sizeof(f->may_free)); +} + /** * Flushes QEMUFile buffer * @@ -151,6 +187,8 @@ void qemu_fflush(QEMUFile *f) if (f->iovcnt > 0) { expect =3D iov_size(f->iov, f->iovcnt); ret =3D f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos= ); + + qemu_iovec_release_ram(f); } =20 if (ret >=3D 0) { @@ -304,13 +342,19 @@ int qemu_fclose(QEMUFile *f) return ret; } =20 -static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size) +static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, + bool may_free) { /* check for adjacent buffer and coalesce them */ if (f->iovcnt > 0 && buf =3D=3D f->iov[f->iovcnt - 1].iov_base + - f->iov[f->iovcnt - 1].iov_len) { + f->iov[f->iovcnt - 1].iov_len && + may_free =3D=3D test_bit(f->iovcnt - 1, f->may_free)) + { f->iov[f->iovcnt - 1].iov_len +=3D size; } else { + if (may_free) { + set_bit(f->iovcnt, f->may_free); + } f->iov[f->iovcnt].iov_base =3D (uint8_t *)buf; f->iov[f->iovcnt++].iov_len =3D size; } @@ -320,14 +364,15 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *= buf, size_t size) } } =20 -void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size) +void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size, + bool may_free) { if (f->last_error) { return; } =20 f->bytes_xfer +=3D size; - add_to_iovec(f, buf, size); + add_to_iovec(f, buf, size, may_free); } =20 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) @@ -345,7 +390,7 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, s= ize_t size) } memcpy(f->buf + f->buf_index, buf, l); f->bytes_xfer +=3D l; - add_to_iovec(f, f->buf + f->buf_index, l); + add_to_iovec(f, f->buf + f->buf_index, l, false); f->buf_index +=3D l; if (f->buf_index =3D=3D IO_BUF_SIZE) { qemu_fflush(f); @@ -366,7 +411,7 @@ void qemu_put_byte(QEMUFile *f, int v) =20 f->buf[f->buf_index] =3D v; f->bytes_xfer++; - add_to_iovec(f, f->buf + f->buf_index, 1); + add_to_iovec(f, f->buf + f->buf_index, 1, false); f->buf_index++; if (f->buf_index =3D=3D IO_BUF_SIZE) { qemu_fflush(f); @@ -647,7 +692,7 @@ ssize_t qemu_put_compression_data(QEMUFile *f, const ui= nt8_t *p, size_t size, } qemu_put_be32(f, blen); if (f->ops->writev_buffer) { - add_to_iovec(f, f->buf + f->buf_index, blen); + add_to_iovec(f, f->buf + f->buf_index, blen, false); } f->buf_index +=3D blen; if (f->buf_index =3D=3D IO_BUF_SIZE) { diff --git a/migration/ram.c b/migration/ram.c index d866b6518b..5a43f716d1 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -726,6 +726,16 @@ static int save_zero_page(QEMUFile *f, RAMBlock *block= , ram_addr_t offset, return pages; } =20 +static void ram_release_pages(MigrationState *ms, const char *block_name, + uint64_t offset, int pages) +{ + if (!migrate_release_ram() || !migration_in_postcopy(ms)) { + return; + } + + ram_discard_range(NULL, block_name, offset, pages << TARGET_PAGE_BITS); +} + /** * ram_save_page: Send the given page to the stream * @@ -786,6 +796,7 @@ static int ram_save_page(MigrationState *ms, QEMUFile *= f, PageSearchStatus *pss, * page would be stale */ xbzrle_cache_zero_page(current_addr); + ram_release_pages(ms, block->idstr, pss->offset, pages); } else if (!ram_bulk_stage && !migration_in_postcopy(ms) && migrate_use_xbzrle()) { pages =3D save_xbzrle_page(f, &p, current_addr, block, @@ -804,7 +815,9 @@ static int ram_save_page(MigrationState *ms, QEMUFile *= f, PageSearchStatus *pss, *bytes_transferred +=3D save_page_header(f, block, offset | RAM_SAVE_FLAG_PAGE= ); if (send_async) { - qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE); + qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE, + migrate_release_ram() & + migration_in_postcopy(ms)); } else { qemu_put_buffer(f, p, TARGET_PAGE_SIZE); } @@ -834,6 +847,8 @@ static int do_compress_ram_page(QEMUFile *f, RAMBlock *= block, error_report("compressed data failed!"); } else { bytes_sent +=3D blen; + ram_release_pages(migrate_get_current(), block->idstr, + offset & TARGET_PAGE_MASK, 1); } =20 return bytes_sent; @@ -973,12 +988,17 @@ static int ram_save_compressed_page(MigrationState *m= s, QEMUFile *f, error_report("compressed data failed!"); } } + if (pages > 0) { + ram_release_pages(ms, block->idstr, pss->offset, pages); + } } else { offset |=3D RAM_SAVE_FLAG_CONTINUE; pages =3D save_zero_page(f, block, offset, p, bytes_transferre= d); if (pages =3D=3D -1) { pages =3D compress_page_with_multi_thread(f, block, offset, bytes_transferred); + } else { + ram_release_pages(ms, block->idstr, pss->offset, pages); } } } diff --git a/qapi-schema.json b/qapi-schema.json index 82fabc6e24..e58228d083 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -865,11 +865,14 @@ # side, this process is called COarse-Grain LOck Stepping (COLO) for # Non-stop Service. (since 2.8) # +# @release-ram: if enabled, qemu will free the migrated ram pages on the s= ource +# during postcopy-ram migration. (since 2.9) +# # Since: 1.2 ## { 'enum': 'MigrationCapability', 'data': ['xbzrle', 'rdma-pin-all', 'auto-converge', 'zero-blocks', - 'compress', 'events', 'postcopy-ram', 'x-colo'] } + 'compress', 'events', 'postcopy-ram', 'x-colo', 'release-ram'] } =20 ## # @MigrationCapabilityStatus: --=20 2.11.0