From patchwork Tue Mar 1 14:21:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 1599419 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=NGGn/aBk; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4K7KHM3nqbz9sFy for ; Wed, 2 Mar 2022 01:23:43 +1100 (AEDT) Received: from localhost ([::1]:45690 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nP3Ph-0002Iw-8K for incoming@patchwork.ozlabs.org; Tue, 01 Mar 2022 09:23:41 -0500 Received: from eggs.gnu.org ([209.51.188.92]:40272) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nP3NY-0000SM-1F for qemu-devel@nongnu.org; Tue, 01 Mar 2022 09:21:28 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:58036) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nP3NW-00074e-DI for qemu-devel@nongnu.org; Tue, 01 Mar 2022 09:21:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646144485; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=g5f+rQLg9LhPIHUAod7Xj2CQqVzU5134kG9U/8ZVgRw=; b=NGGn/aBkDx92aWnvVjRl81rFvSbTE11bOxyTISXFpMRkpsTSuiqu8yY9eDqvq97IET4R0+ BsmYguA9QLCAbCeG4z48MD28s0+QUldssIDE5As1230K4h3UMmk5MotSrsRaiYJJ1c2PsN fKpKTMHfRWAccwfC9rjTrWZkY8zFf9c= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-526-Sg7mSfJyOrW48NWaxYCc-A-1; Tue, 01 Mar 2022 09:21:23 -0500 X-MC-Unique: Sg7mSfJyOrW48NWaxYCc-A-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0751F801DDB; Tue, 1 Mar 2022 14:21:22 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 42DED866CD; Tue, 1 Mar 2022 14:21:21 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [RFC PATCH 4/5] child_job_drained_poll: override polling condition only when in home thread Date: Tue, 1 Mar 2022 09:21:12 -0500 Message-Id: <20220301142113.163174-5-eesposit@redhat.com> In-Reply-To: <20220301142113.163174-1-eesposit@redhat.com> References: <20220301142113.163174-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eesposit@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Received-SPF: pass client-ip=170.10.133.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , Kevin Wolf , Vladimir Sementsov-Ogievskiy , Emanuele Giuseppe Esposito , qemu-devel@nongnu.org, Hanna Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" drv->drained_poll() is only implemented in mirror, and allows it to drain from the coroutine. The mirror implementation uses in_drain flag to recognize when it is draining from coroutine, and consequently avoid deadlocking (wait the poll condition in child_job_drained_poll to wait for itself). The problem is that this flag is dangerous, because it breaks bdrv_drained_begin() invariants: once drained_begin ends, all jobs, in_flight requests, and anything running in the iothread are blocked. This can be broken in such way: iothread(mirror): s->in_drain = true; // mirror.c:1112 main loop: bdrv_drained_begin(mirror_bs); /* * drained_begin wait for bdrv_drain_poll_top_level() condition, * that translates in child_job_drained_poll() for jobs, but * mirror implements drv->drained_poll() so it returns * !!in_flight_requests, which his 0 (assertion in mirror.c:1105). */ main loop: thinks iothread is stopped and is modifying the graph... iothread(mirror): *continues*, as nothing is stopping it iothread(mirror): bdrv_drained_begin(bs); /* draining reads the graph while it is modified!! */ main loop: done modifying the graph... In order to fix this, we can simply allow drv->drained_poll() to be called only by the iothread, and not the main loop. We distinguish it by using in_aio_context_home_thread(), that returns false if @ctx is not the same as the thread that runs it. Co-Developed-by: Paolo Bonzini Signed-off-by: Emanuele Giuseppe Esposito --- blockjob.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/blockjob.c b/blockjob.c index 10815a89fe..e132d9587e 100644 --- a/blockjob.c +++ b/blockjob.c @@ -107,6 +107,7 @@ static bool child_job_drained_poll(BdrvChild *c) BlockJob *bjob = c->opaque; Job *job = &bjob->job; const BlockJobDriver *drv = block_job_driver(bjob); + AioContext *ctx = block_job_get_aio_context(bjob); /* An inactive or completed job doesn't have any pending requests. Jobs * with !job->busy are either already paused or have a pause point after @@ -117,7 +118,7 @@ static bool child_job_drained_poll(BdrvChild *c) /* Otherwise, assume that it isn't fully stopped yet, but allow the job to * override this assumption. */ - if (drv->drained_poll) { + if (in_aio_context_home_thread(ctx) && drv->drained_poll) { return drv->drained_poll(bjob); } else { return true;