From nobody Tue Feb 10 09:27:45 2026 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=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1611865017; cv=none; d=zohomail.com; s=zohoarc; b=b3R6F0THhzGlU2dwEl6nY7NK4kNVFPbwR+bAcThHIgjpVcsyyjJdX/274Yl3/HEpLHWScRIhmGui70+NC0HumBWm0lZuRGwE4ke3g8gY0LMO9SVcJdxoNTJ/a2NDc4KYXZg38pyw5DAE5a0pwQgHzOrgpqPmWIk04x+fwqyPxqM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1611865017; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=KRyR0vXaSOGmYq/LWT3lr62EA7h3B5scd73hSVpIApU=; b=NPG8WuwQRIG6tftpAlAath2TggnbMwVDL202zu8gMiALAACe3xiFQHXjctML0umOHIfEQgdbPOHVBmBZDDV7DBExetJhTpsY4YGE7zUo2IHKu8vl/k0fiiRK7k7PMfW0sXNUWoON7hSwtsWk/ykqtwzhJ6uR0xKeumq5tsN0FSo= ARC-Authentication-Results: i=1; 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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1611865017604882.1064384149364; Thu, 28 Jan 2021 12:16:57 -0800 (PST) Received: from localhost ([::1]:32796 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l5Dip-0006Ji-D3 for importer@patchew.org; Thu, 28 Jan 2021 15:16:55 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58288) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l5Dgb-0004U8-FC; Thu, 28 Jan 2021 15:14:37 -0500 Received: from forwardcorp1p.mail.yandex.net ([2a02:6b8:0:1472:2741:0:8b6:217]:45074) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l5DgU-0002kA-TK; Thu, 28 Jan 2021 15:14:37 -0500 Received: from vla1-fdfb804fb3f3.qloud-c.yandex.net (vla1-fdfb804fb3f3.qloud-c.yandex.net [IPv6:2a02:6b8:c0d:3199:0:640:fdfb:804f]) by forwardcorp1p.mail.yandex.net (Yandex) with ESMTP id 8DAEF2E1AC0; Thu, 28 Jan 2021 23:14:25 +0300 (MSK) Received: from vla1-81430ab5870b.qloud-c.yandex.net (vla1-81430ab5870b.qloud-c.yandex.net [2a02:6b8:c0d:35a1:0:640:8143:ab5]) by vla1-fdfb804fb3f3.qloud-c.yandex.net (mxbackcorp/Yandex) with ESMTP id f265UETGZn-EPwSqL8c; Thu, 28 Jan 2021 23:14:25 +0300 Received: from dynamic-vpn.dhcp.yndx.net (dynamic-vpn.dhcp.yndx.net [2a02:6b8:b081:420::1:f]) by vla1-81430ab5870b.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id fjMENJuJUJ-EOmWAkcj; Thu, 28 Jan 2021 23:14:25 +0300 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1611864865; bh=KRyR0vXaSOGmYq/LWT3lr62EA7h3B5scd73hSVpIApU=; h=In-Reply-To:Message-Id:References:Date:Subject:To:From:Cc; b=10EXSasudhHnARESPuQZz8xJPZI6RtVtT6D9zphEJnx8I2GRZhUitGr9deJ1FYWSU mWArIehEfp80W3MH9HRmfbKzeYe+ktRTxD30GxzefoiHjSZJkdPYp0l384rCyiJ4gO xypZcLD5yEE9y54mBet8j0BO+TTi3OGVr8fX0GUA= Authentication-Results: vla1-fdfb804fb3f3.qloud-c.yandex.net; dkim=pass header.i=@yandex-team.ru From: Roman Kagan To: qemu-devel@nongnu.org Subject: [PATCH 2/3] block/nbd: only enter connection coroutine if it's present Date: Thu, 28 Jan 2021 23:14:17 +0300 Message-Id: <20210128201418.607640-3-rvkagan@yandex-team.ru> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210128201418.607640-1-rvkagan@yandex-team.ru> References: <20210128201418.607640-1-rvkagan@yandex-team.ru> 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=2a02:6b8:0:1472:2741:0:8b6:217; envelope-from=rvkagan@yandex-team.ru; helo=forwardcorp1p.mail.yandex.net 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, 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: Kevin Wolf , Vladimir Sementsov-Ogievskiy , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" When an NBD block driver state is moved from one aio_context to another (e.g. when doing a drain in a migration thread), nbd_client_attach_aio_context_bh is executed that enters the connection coroutine. However, the assumption that ->connection_co is always present here appears incorrect: the connection may have encountered an error other than -EIO in the underlying transport, and thus may have decided to quit rather than keep trying to reconnect, and therefore it may have terminated the connection coroutine. As a result an attempt to reassign the client in this state (NBD_CLIENT_QUIT) to a different aio_context leads to a null pointer dereference: at /build/qemu-6MF7tq/qemu-5.0.1/util/qemu-coroutine.c:109 opaque=3D0x561805ed4c00) at /build/qemu-6MF7tq/qemu-5.0.1/block/nbd.c:1= 64 at /build/qemu-6MF7tq/qemu-5.0.1/util/aio-wait.c:55 at /build/qemu-6MF7tq/qemu-5.0.1/util/async.c:136 at /build/qemu-6MF7tq/qemu-5.0.1/util/async.c:164 blocking=3Dblocking@entry=3Dtrue) at /build/qemu-6MF7tq/qemu-5.0.1/util/aio-posix.c:650 cb=3D, opaque=3D) at /build/qemu-6MF7tq/qemu-5.0.1/util/aio-wait.c:71 bs=3D0x561805ed4c00) at /build/qemu-6MF7tq/qemu-5.0.1/block.c:6172 new_context=3Dnew_context@entry=3D0x5618056c7580, ignore=3Dignore@entry=3D0x7f60e1e63780) at /build/qemu-6MF7tq/qemu-5.0.1/block.c:6237 bs=3Dbs@entry=3D0x561805ed4c00, ctx=3D0x5618056c7580, ignore_child=3D, errp=3D) at /build/qemu-6MF7tq/qemu-5.0.1/block.c:6332 new_context=3D0x5618056c7580, update_root_node=3Dupdate_root_node@entry= =3Dtrue, errp=3Derrp@entry=3D0x0) at /build/qemu-6MF7tq/qemu-5.0.1/block/block-backend.c:1989 new_context=3D, errp=3Derrp@entry=3D0x0) at /build/qemu-6MF7tq/qemu-5.0.1/block/block-backend.c:2010 at /build/qemu-6MF7tq/qemu-5.0.1/hw/block/dataplane/virtio-blk.c:292 at /build/qemu-6MF7tq/qemu-5.0.1/hw/virtio/virtio-bus.c:245 running=3D0, state=3D) at /build/qemu-6MF7tq/qemu-5.0.1/hw/virtio/virtio.c:3220 state=3Dstate@entry=3DRUN_STATE_FINISH_MIGRATE) at /build/qemu-6MF7tq/qemu-5.0.1/softmmu/vl.c:1275 send_stop=3D) at /build/qemu-6MF7tq/qemu-5.0.1/cpus.c:10= 32 at /build/qemu-6MF7tq/qemu-5.0.1/migration/migration.c:2914 at /build/qemu-6MF7tq/qemu-5.0.1/migration/migration.c:3275 at /build/qemu-6MF7tq/qemu-5.0.1/migration/migration.c:3439 at /build/qemu-6MF7tq/qemu-5.0.1/util/qemu-thread-posix.c:519 from /lib/x86_64-linux-gnu/libpthread.so.0 Fix it by checking that the connection coroutine is non-null before trying to enter it. If it is null, no entering is needed, as the connection is probably going down anyway. Signed-off-by: Roman Kagan Reviewed-by: Vladimir Sementsov-Ogievskiy --- block/nbd.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index bcd6641e90..b3cbbeb4b0 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -250,13 +250,15 @@ static void nbd_client_attach_aio_context_bh(void *op= aque) BlockDriverState *bs =3D opaque; BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; =20 - /* - * The node is still drained, so we know the coroutine has yielded in - * nbd_read_eof(), the only place where bs->in_flight can reach 0, or = it is - * entered for the first time. Both places are safe for entering the - * coroutine. - */ - qemu_aio_coroutine_enter(bs->aio_context, s->connection_co); + if (s->connection_co) { + /* + * The node is still drained, so we know the coroutine has yielded= in + * nbd_read_eof(), the only place where bs->in_flight can reach 0,= or + * it is entered for the first time. Both places are safe for ente= ring + * the coroutine. + */ + qemu_aio_coroutine_enter(bs->aio_context, s->connection_co); + } bdrv_dec_in_flight(bs); } =20 --=20 2.29.2