From nobody Fri May 10 10:21:55 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=bytedance.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16263542763591007.4482939458862; Thu, 15 Jul 2021 06:04:36 -0700 (PDT) Received: from localhost ([::1]:54712 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m412Z-00060p-8l for importer@patchew.org; Thu, 15 Jul 2021 09:04:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37322) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m410e-00043v-ES for qemu-devel@nongnu.org; Thu, 15 Jul 2021 09:02:36 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]:45873) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m410Y-0001IJ-4C for qemu-devel@nongnu.org; Thu, 15 Jul 2021 09:02:33 -0400 Received: by mail-pj1-x1031.google.com with SMTP id b8-20020a17090a4888b02901725eedd346so4115229pjh.4 for ; Thu, 15 Jul 2021 06:02:28 -0700 (PDT) Received: from C02D383UML85.bytedance.net ([139.177.225.235]) by smtp.gmail.com with ESMTPSA id l6sm6673651pff.74.2021.07.15.06.02.18 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Jul 2021 06:02:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=P0UPJEWy7KCGGXec/MbDU7xIgvNTe5rdeqVh83pd+CE=; b=gbx1mYQ/rZH1HEjQ+dPJdQjxwUOlPw+M8HgUH1bRgIiGNIJf+odRALsAbfziNlyCWp sshvwQK5aIWnj0aRjiq1S1QR4AHcklRU0WCoXjl7rMfe+pv61oKJZIzkMbaSUDQ76nWE FMTYzxtY6c4qVvKvxhPpAS+ExSGjDSGGEq7Tcyfb+e4W9fOuf5YaI94nM0oxn6CBpw8T 6ZSxi5ZlW2cgRfU4lZp0/lbeeGEND2c86Rym6PnUT6lYikuEC0l0ZSWxMMe4XXcdMvjt d8RKw8bFeD1KKhxmVVN//vkei2WIwcK8JlTCv4An8k555TGmzvEn0mpOJ8bcIUY7P45s 6OFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=P0UPJEWy7KCGGXec/MbDU7xIgvNTe5rdeqVh83pd+CE=; b=io/59x5Yt+h5IKseZ+T9RgDMJYAfu2pTWeRohAPqPgllly7IGtu/kvr/H+3EHNwLAI GRD3iDV6s2VQbhoRgBOZTMC0mTwFrQ6ph/1TSrCHHbB6QeeH1RjlyBsqitXS/adK4/Rw UvYB3zxJnSpCgQZvRvprooB0fbcooVOZIutPpWSOm++rhLZcHEA5B2WsN/kuk9P1zovC cqZSMCd9Kfy7g8jNug23sBKovP76hi3OM6ZorMZ38YnyZZ0B28ENsRkPlg/aEP3wWuT4 rxzgT+XOMp9VoHgUAYksSybo4Ua++P3NgJ6ZZfisJakOIYFkVVr92glFB1UK7HiJPY0N PfMA== X-Gm-Message-State: AOAM533PhIQjp+fYlT8q/UJdI2SERmLVxx/OKO1AP0ypnk/PLOucdQ6g buLxzy8fvlvDj+UPF5pH91qLcQ== X-Google-Smtp-Source: ABdhPJx11RLzBYwOxpGSq9j7RC7MnpJXcwTtdY9FCtXPcq2VcKrX9XD0z2zaqr2Z1iNXOrNSfB1ahw== X-Received: by 2002:a17:90a:a6a:: with SMTP id o97mr10232992pjo.179.1626354147069; Thu, 15 Jul 2021 06:02:27 -0700 (PDT) From: Zhiyong Ye To: qemu-block@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH] block: Fix deadlock in bdrv_set_aio_context_ignore() Date: Thu, 15 Jul 2021 21:02:05 +0800 Message-Id: <20210715130205.36912-1-yezhiyong@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) 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=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=yezhiyong@bytedance.com; helo=mail-pj1-x1031.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Zhiyong Ye , pizhenwei@bytedance.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1626354308820100001 Content-Type: text/plain; charset="utf-8" When bdrv_set_aio_context_ignore() is called in the main loop to change the AioContext onto the IO thread, the bdrv_drain_invoke_entry() never gets to run and the IO thread hangs at co_schedule_bh_cb(). This is because the AioContext is occupied by the main thread after being attached to the IO thread, and the main thread poll in bdrv_drained_end() waiting for the IO request to be drained, but the IO thread cannot acquire the AioContext, which leads to deadlock. Just like below: <------> [Switching to thread 1 (Thread 0x7fd810bbef40 (LWP 533312))] (gdb) bt ... 3 0x00005601f6ea93aa in fdmon_poll_wait at ../util/fdmon-poll.c:80 4 0x00005601f6e81a1c in aio_poll at ../util/aio-posix.c:607 5 0x00005601f6dcde87 in bdrv_drained_end at ../block/io.c:496 6 0x00005601f6d798cd in bdrv_set_aio_context_ignore at ../block.c:6502 7 0x00005601f6d7996c in bdrv_set_aio_context_ignore at ../block.c:6472 8 0x00005601f6d79cb8 in bdrv_child_try_set_aio_context at ../block.c:6587 9 0x00005601f6da86f2 in blk_do_set_aio_context at ../block/block-backend.c= :2026 10 0x00005601f6daa96d in blk_set_aio_context at ../block/block-backend.c:20= 47 11 0x00005601f6c71883 in virtio_scsi_hotplug at ../hw/scsi/virtio-scsi.c:831 ... [Switching to thread 4 (Thread 0x7fd8092e7700 (LWP 533315))] (gdb) bt ... 4 0x00005601f6eab6a8 in qemu_mutex_lock_impl at ../util/qemu-thread-posix.= c:79 5 0x00005601f6e7ce88 in co_schedule_bh_cb at ../util/async.c:489 6 0x00005601f6e7c404 in aio_bh_poll at ../util/async.c:164 7 0x00005601f6e81a46 in aio_poll at ../util/aio-posix.c:659 8 0x00005601f6d5ccf3 in iothread_run at ../iothread.c:73 9 0x00005601f6eab512 in qemu_thread_start at ../util/qemu-thread-posix.c:5= 21 10 0x00007fd80d7b84a4 in start_thread at pthread_create.c:456 11 0x00007fd80d4fad0f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clon= e.S:97 (gdb) f 4 4 0x00005601f6eab6a8 in qemu_mutex_lock_impl at ../util/qemu-thread-posix.= c:79 (gdb) p *mutex $2 =3D {lock =3D {__data =3D {__lock =3D 2, __count =3D 1, __owner =3D 5333= 12, __nusers =3D 1, __kind =3D 1, __spins =3D 0, __elision =3D 0, __list =3D {__prev =3D = 0x0, __next =3D 0x0}}, __size =3D "\002\000\000\000\001\000\000\000@#\b\000\001\000\000\000\= 001", '\000' , __align =3D 4294967298}, initialized =3D t= rue} <------> Therefore, we should never poll anywhere in bdrv_set_aio_context_ignore() when acquiring the new context. In fact, commit e037c09c has also already elaborated on why we can't poll at bdrv_do_drained_end(). Signed-off-by: Zhiyong Ye --- block.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/block.c b/block.c index be083f389e..ebbea72d64 100644 --- a/block.c +++ b/block.c @@ -6846,6 +6846,7 @@ void bdrv_set_aio_context_ignore(BlockDriverState *bs, GSList *parents_to_process =3D NULL; GSList *entry; BdrvChild *child, *parent; + int drained_end_counter =3D 0; =20 g_assert(qemu_get_current_aio_context() =3D=3D qemu_get_aio_context()); =20 @@ -6907,7 +6908,7 @@ void bdrv_set_aio_context_ignore(BlockDriverState *bs, aio_context_release(old_context); } =20 - bdrv_drained_end(bs); + bdrv_drained_end_no_poll(bs, &drained_end_counter); =20 if (qemu_get_aio_context() !=3D old_context) { aio_context_acquire(old_context); @@ -6915,6 +6916,7 @@ void bdrv_set_aio_context_ignore(BlockDriverState *bs, if (qemu_get_aio_context() !=3D new_context) { aio_context_release(new_context); } + BDRV_POLL_WHILE(bs, qatomic_read(&drained_end_counter) > 0); } =20 static bool bdrv_parent_can_set_aio_context(BdrvChild *c, AioContext *ctx, --=20 2.11.0