From nobody Mon Nov 23 04:44:10 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=openvz.org ARC-Seal: i=1; a=rsa-sha256; t=1794335081; cv=none; d=zohomail.com; s=zohoarc; b=S1+f1OEyY2gnVAw4jmUUvl2nGEOPRMm+St1AyysyBpWF+ycMwgo+y111/TxWjrhhueqTfOiFPOTvT790bgH6d+nPbZPqHz0DzgTmejH6ck2krxwEf1RxqpM0ZsoiCUyPA9znuj4PWkksciVncPL3ardbR+hwRuxI0DFVYRlsg5A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794335081; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=km+hBAzvnvfDPCwEyUqAgklvaoh4AgeGGZMYyeks7FA=; b=kyU9UrESvbJ53FuVpYGAMQCCPRnkLBvWCTtcPxP4acxXjcYFXfKhJMbH11idSWIQ3/D5i1DN1gRXE3QMhzw80ubE5jZVRhABch5rdk9Jka5ZXce4OuBxNBmGC3+wQx+NTLFEZMQLkDcT6M/wcPPDc1kzyX11viVIXtSlF/FbVxI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1794335080765479.89576859863587; Tue, 10 Nov 2026 10:24:40 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xQqW3-00027Q-4S; Tue, 10 Nov 2026 13:24:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xQqW0-000265-Sa for qemu-devel@nongnu.org; Tue, 10 Nov 2026 13:24:16 -0500 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xQqVy-0002UN-B0 for qemu-devel@nongnu.org; Tue, 10 Nov 2026 13:24:16 -0500 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-4a5177d78c7so19554125e9.2 for ; Tue, 10 Nov 2026 10:24:13 -0800 (PST) Received: from athena.sw.ru ([2a06:5b06:b600:300:4f78:6441:d0f1:22af]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4ba08e354b0sm2219345e9.1.2026.11.10.10.24.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Nov 2026 10:24:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz.org; s=google; t=1794335052; x=1794939852; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=km+hBAzvnvfDPCwEyUqAgklvaoh4AgeGGZMYyeks7FA=; b=J8jBHPHTnnn9r5J/0DshCnSPIIUg2456bLTNyI+ek2CDvZmRR1njZymbJ36YJOb8NY aDeiw6kALP89KrzzP902fTAHAR03U5zpXJCzU3gsO4et0sYhnP1hBYInn7QtD5ZRH4u+ VrtrrX1pJtQ7hp38vbhonsO3qosSoBH8lRhp24VixYkQyZrp6X0IiOWq0MfSlPeJ/KRm QUJkJ48pZi4O6psCw0ZyeEITTr+czX5cPyrhH/tqKD6liY0GSvkJTJkpKsRSjWqLdb07 7dzj/9ATgL5ld0tvslpS7Vr3MsC6aL81aMojwOa53yMbAEnr9DpRgir/dQsggUoIX32s X3pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794335052; x=1794939852; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=km+hBAzvnvfDPCwEyUqAgklvaoh4AgeGGZMYyeks7FA=; b=lIco+73+wN+taIFilXgAjtpKLNklJ+poTdkR7+NuMYpP3SnFITh41NS+D5QnyJee/j svQ97JEHANJgKVPYg5rxNESAvsG6O0HLdyM5ppMUhuUB6SHWEq+O15pxh/EQ+YqLipCT bhSVPXHfrUVWwgVCMYy32yljxn4WqxdclfIkGMhnUwyfl3Gr87A+j2mzyw4T2Nz3mFvS HopdNmwXWKIg0Hg/hVkShLf62vK6PiOICrtSugP1iKfVXLJT8BD0loIAwrRlQEoZVq7i 6+hh8nPrNiQI9ibsbydbukL7gV+BRFSLo/CXZWcDOqnEbPDT/vjkDu8SsydWmec5QyO2 ERaw== X-Gm-Message-State: APuU4Z/g3TBJ5JvjNJcz90PQ7kQamrNFJlbPaw/63OeK5ZDSuJdRM3eE 5Txo+VgIdezaC8wR+NSwhq2nESrz05WF55v9HxvgrGib5k6CguttdH4JC+2rBS4wk1M/4DIaUty m8NnD X-Gm-Gg: AYBFou24M3eOt2FibPC7/xoP8x6nDoTyRjJGPM9AzYbHSFho8E+EuUQYlpK8+UzAhfp 3u/zDMyXF//lVboVO+98XDZfrz7NlepABHnpMAWJopnmGM1eSI7KOpnpgPzvjp/1ZsJwiDxp+7e SmlNMsG2sNJMZo+n+qarlc6TANZUdxrq6Wf+yWnjAnx14AugJOADepHi6OtoGObpYwDEsQLlndT Jo5suyz1ZmhSldbrTeXzvJ7w3rTsmCkFqVPPV0nM15QquwS+jLc1FRXAcW3YtdidlwtS15K0RmI PbwUh3aaH/qe/QaH1G34KwsZf0cG0iFnOy0tkphwmq+H8t6mIVFMjnUsLPm0jFoF2IsMpPKToGA pg9Gx0YxDr7An3OOexIkjKOEwiCOBHo2+oMzZKtZLUaKlnVyTqmbqdiz6ZnAg16O0z6kg1eUdCn FlNG7scAoTBgrxVdvJo9NH0F/6EEZYUJz06OYHX2D1A2U96yVNU8PuJMaKsWqOWaYJ83E7djNsX Je4heSlJcNAF19MJ/jgNcBz/n/VWZ5rfC2NWhrwc/50hiRiUH+MC3yWHclli/Nh4al61WeFq6eF a8Y/CxlzIhdwlS2ipHelRx0iLA== X-Received: by 2002:a05:600c:468f:b0:4a3:12fb:1ee7 with SMTP id 5b1f17b1804b1-4ba0234cd58mr61857645e9.25.1794335051883; Tue, 10 Nov 2026 10:24:11 -0800 (PST) From: "Denis V. Lunev" To: qemu-devel@nongnu.org Cc: den@openvz.org, Vladimir Sementsov-Ogievskiy , Eric Blake , John Snow , Peter Xu , Fabiano Rosas , qemu-block@nongnu.org Subject: [PATCH] migration/block-dirty-bitmap: don't hold the load lock across stream reads Date: Tue, 10 Nov 2026 19:24:08 +0100 Message-ID: <20261110155113.2721134-1-den@openvz.org> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::330; envelope-from=den@openvz.org; helo=mail-wm1-x330.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @openvz.org) X-ZM-MESSAGEID: 1794335083306158500 Content-Type: text/plain; charset="utf-8" From: Denis V. Lunev dirty_bitmap_load() takes dbm_state.load.lock for each chunk and holds it while it reads the chunk from the migration stream. In precopy the load runs in the incoming migration coroutine, which yields in qemu_fill_buffer() while it waits for data, still holding the lock. A quit on the destination at that point never completes: migration_shutdown() calls dirty_bitmap_mig_cancel_incoming(), which takes the same lock in the same main thread. In postcopy the load runs in the listen thread, and the main thread takes the lock in dirty_bitmap_mig_before_vm_start(), called from loadvm_postcopy_handle_run_bh(), and in migration_shutdown(). The main loop then waits until the source sends the next chunk. Read each chunk into a DBMLoadChunk first and take the lock only to resolve the names and apply the chunk. The lock protects the bitmaps list and the resolved node and bitmap, which the main thread uses. The flags and alias strings read from the stream are only touched by the code doing the load. Signed-off-by: Denis V. Lunev Cc: Vladimir Sementsov-Ogievskiy Cc: Eric Blake Cc: John Snow Cc: Peter Xu Cc: Fabiano Rosas Cc: qemu-block@nongnu.org Reviewed-by: Eric Blake --- migration/block-dirty-bitmap.c | 222 +++++++++++++++++++-------------- 1 file changed, 130 insertions(+), 92 deletions(-) diff --git a/migration/block-dirty-bitmap.c b/migration/block-dirty-bitmap.c index 35623379eb..14e5a984e4 100644 --- a/migration/block-dirty-bitmap.c +++ b/migration/block-dirty-bitmap.c @@ -796,12 +796,22 @@ static void dirty_bitmap_state_pending(void *opaque, = MigPendingData *data, data->postcopy_bytes +=3D pending; } =20 +/* Chunk payload, read from the stream without holding DBMLoadState.lock */ +typedef struct DBMLoadChunk { + uint32_t granularity; + uint8_t start_flags; + uint64_t first_byte; + uint64_t nr_bytes; + uint64_t buf_size; + uint8_t *buf; +} DBMLoadChunk; + /* First occurrence of this bitmap. It should be created if doesn't exist = */ -static int dirty_bitmap_load_start(QEMUFile *f, DBMLoadState *s) +static int dirty_bitmap_load_start(DBMLoadState *s, DBMLoadChunk *c) { Error *local_err =3D NULL; - uint32_t granularity =3D qemu_get_be32(f); - uint8_t flags =3D qemu_get_byte(f); + uint32_t granularity =3D c->granularity; + uint8_t flags =3D c->start_flags; LoadBitmapState *b; bool persistent; =20 @@ -959,7 +969,7 @@ void dirty_bitmap_mig_cancel_incoming(void) qemu_mutex_unlock(&s->lock); } =20 -static void dirty_bitmap_load_complete(QEMUFile *f, DBMLoadState *s) +static void dirty_bitmap_load_complete(DBMLoadState *s) { GSList *item; trace_dirty_bitmap_load_complete(); @@ -990,71 +1000,43 @@ static void dirty_bitmap_load_complete(QEMUFile *f, = DBMLoadState *s) } } =20 -static int dirty_bitmap_load_bits(QEMUFile *f, DBMLoadState *s) +static int dirty_bitmap_load_bits(DBMLoadState *s, DBMLoadChunk *c) { - uint64_t first_byte =3D qemu_get_be64(f) << BDRV_SECTOR_BITS; - uint64_t nr_bytes =3D (uint64_t)qemu_get_be32(f) << BDRV_SECTOR_BITS; - trace_dirty_bitmap_load_bits_enter(first_byte >> BDRV_SECTOR_BITS, - nr_bytes >> BDRV_SECTOR_BITS); + uint64_t needed_size; + + trace_dirty_bitmap_load_bits_enter(c->first_byte >> BDRV_SECTOR_BITS, + c->nr_bytes >> BDRV_SECTOR_BITS); =20 if (s->flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) { trace_dirty_bitmap_load_bits_zeroes(); if (!s->cancelled) { - bdrv_dirty_bitmap_deserialize_zeroes(s->bitmap, first_byte, - nr_bytes, false); - } - } else { - size_t ret; - g_autofree uint8_t *buf =3D NULL; - uint64_t buf_size =3D qemu_get_be64(f); - uint64_t needed_size; - - /* - * The actual check for buf_size is done a bit later. We can't do = it in - * cancelled mode as we don't have the bitmap to check the constra= ints - * (so, we allocate a buffer and read prior to the check). On the = other - * hand, we shouldn't blindly g_malloc the number from the stream. - * Actually one chunk should not be larger than CHUNK_SIZE. Let's = allow - * a bit larger (which means that bitmap migration will fail anywa= y and - * the whole migration will most probably fail soon due to broken - * stream). - */ - if (buf_size > 10 * CHUNK_SIZE) { - error_report("Bitmap migration stream buffer allocation reques= t " - "is too large"); - return -EIO; - } - - buf =3D g_malloc(buf_size); - ret =3D qemu_get_buffer(f, buf, buf_size); - if (ret !=3D buf_size) { - error_report("Failed to read bitmap bits"); - return -EIO; - } - - if (s->cancelled) { - return 0; + bdrv_dirty_bitmap_deserialize_zeroes(s->bitmap, c->first_byte, + c->nr_bytes, false); } + return 0; + } =20 - needed_size =3D bdrv_dirty_bitmap_serialization_size(s->bitmap, - first_byte, - nr_bytes); - - if (needed_size > buf_size || - buf_size > QEMU_ALIGN_UP(needed_size, 4 * sizeof(long)) - /* Here used same alignment as in send_bitmap_bits */ - ) { - error_report("Migrated bitmap granularity doesn't " - "match the destination bitmap '%s' granularity", - bdrv_dirty_bitmap_name(s->bitmap)); - cancel_incoming_locked(s); - return 0; - } + if (s->cancelled) { + return 0; + } =20 - bdrv_dirty_bitmap_deserialize_part(s->bitmap, buf, first_byte, nr_= bytes, - false); + needed_size =3D bdrv_dirty_bitmap_serialization_size(s->bitmap, + c->first_byte, + c->nr_bytes); + + if (needed_size > c->buf_size || + c->buf_size > QEMU_ALIGN_UP(needed_size, 4 * sizeof(long)) + /* Here used same alignment as in send_bitmap_bits */ + ) { + error_report("Migrated bitmap granularity doesn't " + "match the destination bitmap '%s' granularity", + bdrv_dirty_bitmap_name(s->bitmap)); + cancel_incoming_locked(s); + return 0; } =20 + bdrv_dirty_bitmap_deserialize_part(s->bitmap, c->buf, c->first_byte, + c->nr_bytes, false); return 0; } =20 @@ -1105,23 +1087,73 @@ static BdrvDirtyBitmap *dirty_bitmap_load_find_bitm= ap(DBMLoadState *s) return NULL; } =20 -static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s, - GHashTable *alias_map) +static int dirty_bitmap_read_chunk(QEMUFile *f, DBMLoadState *s, + DBMLoadChunk *c) +{ + s->flags =3D qemu_get_bitmap_flags(f); + trace_dirty_bitmap_load_header(s->flags); + + if ((s->flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) && + !qemu_get_counted_string(f, s->node_alias)) { + error_report("Unable to read node alias string"); + return -EINVAL; + } + + if ((s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) && + !qemu_get_counted_string(f, s->bitmap_alias)) { + error_report("Unable to read bitmap alias string"); + return -EINVAL; + } + + if (s->flags & DIRTY_BITMAP_MIG_FLAG_START) { + c->granularity =3D qemu_get_be32(f); + c->start_flags =3D qemu_get_byte(f); + } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_COMPLETE) { + /* no payload */ + } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITS) { + c->first_byte =3D qemu_get_be64(f) << BDRV_SECTOR_BITS; + c->nr_bytes =3D (uint64_t)qemu_get_be32(f) << BDRV_SECTOR_BITS; + + if (!(s->flags & DIRTY_BITMAP_MIG_FLAG_ZEROES)) { + c->buf_size =3D qemu_get_be64(f); + + /* + * The actual check for buf_size is done when the chunk is + * applied. We can't do it in cancelled mode as we don't have = the + * bitmap to check the constraints (so, we allocate a buffer a= nd + * read prior to the check). On the other hand, we shouldn't + * blindly g_malloc the number from the stream. Actually one c= hunk + * should not be larger than CHUNK_SIZE. Let's allow a bit lar= ger + * (which means that bitmap migration will fail anyway and the + * whole migration will most probably fail soon due to broken + * stream). + */ + if (c->buf_size > 10 * CHUNK_SIZE) { + error_report("Bitmap migration stream buffer allocation " + "request is too large"); + return -EIO; + } + + c->buf =3D g_malloc(c->buf_size); + if (qemu_get_buffer(f, c->buf, c->buf_size) !=3D c->buf_size) { + error_report("Failed to read bitmap bits"); + return -EIO; + } + } + } + + return qemu_file_get_error(f); +} + +static void dirty_bitmap_load_header(DBMLoadState *s, GHashTable *alias_ma= p) { GHashTable *bitmap_alias_map =3D NULL; Error *local_err =3D NULL; bool nothing; - s->flags =3D qemu_get_bitmap_flags(f); - trace_dirty_bitmap_load_header(s->flags); =20 nothing =3D s->flags =3D=3D (s->flags & DIRTY_BITMAP_MIG_FLAG_EOS); =20 if (s->flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) { - if (!qemu_get_counted_string(f, s->node_alias)) { - error_report("Unable to read node alias string"); - return -EINVAL; - } - if (!s->cancelled) { if (alias_map) { const AliasMapInnerNode *amin; @@ -1165,11 +1197,6 @@ static int dirty_bitmap_load_header(QEMUFile *f, DBM= LoadState *s, if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) { const char *bitmap_name; =20 - if (!qemu_get_counted_string(f, s->bitmap_alias)) { - error_report("Unable to read bitmap alias string"); - return -EINVAL; - } - bitmap_name =3D s->bitmap_alias; if (!s->cancelled && bitmap_alias_map) { BitmapMigrationBitmapAlias *bmap_inner; @@ -1206,7 +1233,23 @@ static int dirty_bitmap_load_header(QEMUFile *f, DBM= LoadState *s, error_report("Error: block device name is not set"); cancel_incoming_locked(s); } +} =20 +static int dirty_bitmap_apply_chunk(DBMLoadState *s, DBMLoadChunk *c, + GHashTable *alias_map) +{ + dirty_bitmap_load_header(s, alias_map); + + if (s->flags & DIRTY_BITMAP_MIG_FLAG_START) { + return dirty_bitmap_load_start(s, c); + } + if (s->flags & DIRTY_BITMAP_MIG_FLAG_COMPLETE) { + dirty_bitmap_load_complete(s); + return 0; + } + if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITS) { + return dirty_bitmap_load_bits(s, c); + } return 0; } =20 @@ -1240,28 +1283,23 @@ static int dirty_bitmap_load(QEMUFile *f, void *opa= que, int version_id) } =20 do { - QEMU_LOCK_GUARD(&s->lock); - - ret =3D dirty_bitmap_load_header(f, s, alias_map); - if (ret < 0) { - cancel_incoming_locked(s); - goto fail; - } - - if (s->flags & DIRTY_BITMAP_MIG_FLAG_START) { - ret =3D dirty_bitmap_load_start(f, s); - } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_COMPLETE) { - dirty_bitmap_load_complete(f, s); - } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITS) { - ret =3D dirty_bitmap_load_bits(f, s); - } + DBMLoadChunk c =3D {}; =20 - if (!ret) { - ret =3D qemu_file_get_error(f); + /* + * dirty_bitmap_mig_before_vm_start() takes the lock from the main + * thread, so never hold it while waiting for the stream. + */ + ret =3D dirty_bitmap_read_chunk(f, s, &c); + WITH_QEMU_LOCK_GUARD(&s->lock) { + if (!ret) { + ret =3D dirty_bitmap_apply_chunk(s, &c, alias_map); + } + if (ret) { + cancel_incoming_locked(s); + } } - + g_free(c.buf); if (ret) { - cancel_incoming_locked(s); goto fail; } } while (!(s->flags & DIRTY_BITMAP_MIG_FLAG_EOS)); base-commit: 723622bedb0b423de7d1c61ebbe6e7a71934949c --=20 2.53.0