From patchwork Thu Oct 6 13:02:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alberto Garcia X-Patchwork-Id: 9364785 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DAC336075E for ; Thu, 6 Oct 2016 13:19:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CCC2D28FD8 for ; Thu, 6 Oct 2016 13:19:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C18DC28FF0; Thu, 6 Oct 2016 13:19:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 492C428FD8 for ; Thu, 6 Oct 2016 13:19:52 +0000 (UTC) Received: from localhost ([::1]:56052 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bs8aV-0002pb-Ac for patchwork-qemu-devel@patchwork.kernel.org; Thu, 06 Oct 2016 09:19:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33249) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bs8Kf-0005jf-U2 for qemu-devel@nongnu.org; Thu, 06 Oct 2016 09:03:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bs8KZ-00018s-VK for qemu-devel@nongnu.org; Thu, 06 Oct 2016 09:03:29 -0400 Received: from smtp3.mundo-r.com ([212.51.32.191]:63646 helo=smtp4.mundo-r.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bs8KZ-00018C-Nx; Thu, 06 Oct 2016 09:03:23 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2A7AgCgSvZX/5tjdVtdHQEFAQsBgz0BAQEBAR6BU40ylV0BAQUBgRaPcIIxgg+CC4YgAoF/OBQBAgEBAQEBAQFeJ4RiAgQnUhA/EjwbGYhSAb5hATGFcoJPh2OFEwWPLopRj3uPdEmQLR42PwUHghmCRG+IQAEBAQ X-IPAS-Result: A2A7AgCgSvZX/5tjdVtdHQEFAQsBgz0BAQEBAR6BU40ylV0BAQUBgRaPcIIxgg+CC4YgAoF/OBQBAgEBAQEBAQFeJ4RiAgQnUhA/EjwbGYhSAb5hATGFcoJPh2OFEwWPLopRj3uPdEmQLR42PwUHghmCRG+IQAEBAQ X-IronPort-AV: E=Sophos;i="5.31,454,1473112800"; d="scan'208";a="195926146" Received: from fanzine.igalia.com ([91.117.99.155]) by smtp4.mundo-r.com with ESMTP; 06 Oct 2016 15:03:19 +0200 Received: from a88-114-146-170.elisa-laajakaista.fi ([88.114.146.170] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim) id 1bs8KU-0001gH-VG; Thu, 06 Oct 2016 15:03:19 +0200 Received: from berto by perseus.local with local (Exim 4.87) (envelope-from ) id 1bs8KH-0007XT-6z; Thu, 06 Oct 2016 16:03:05 +0300 From: Alberto Garcia To: qemu-devel@nongnu.org Date: Thu, 6 Oct 2016 16:02:44 +0300 Message-Id: <950ab25638c15b988aa336474ab21f6020028828.1475757437.git.berto@igalia.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 212.51.32.191 Subject: [Qemu-devel] [PATCH v10 01/16] block: Pause all jobs during bdrv_reopen_multiple() 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Markus Armbruster , Max Reitz , Stefan Hajnoczi Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP When a BlockDriverState is about to be reopened it can trigger certain operations that need to write to disk. During this process a different block job can be woken up. If that block job completes and also needs to call bdrv_reopen() it can happen that it needs to do it on the same BlockDriverState that is still in the process of being reopened. This can have fatal consequences, like in this example: 1) Block job A starts and sleeps after a while. 2) Block job B starts and tries to reopen node1 (a qcow2 file). 3) Reopening node1 means flushing and replacing its qcow2 cache. 4) While the qcow2 cache is being flushed, job A wakes up. 5) Job A completes and reopens node1, replacing its cache. 6) Job B resumes, but the cache that was being flushed no longer exists. This patch pauses all block jobs during bdrv_reopen_multiple(), so that step 4 can never happen and the operation is safe. Note that this scenario can only happen if both bdrv_reopen() calls are made by block jobs on the same backing chain. Otherwise there's no chance that the same BlockDriverState appears in both reopen queues. Signed-off-by: Alberto Garcia --- block.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/block.c b/block.c index bb1f1ec..c80b528 100644 --- a/block.c +++ b/block.c @@ -2087,9 +2087,19 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) int ret = -1; BlockReopenQueueEntry *bs_entry, *next; Error *local_err = NULL; + BlockJob *job = NULL; assert(bs_queue != NULL); + /* Pause all block jobs */ + while ((job = block_job_next(job))) { + AioContext *aio_context = blk_get_aio_context(job->blk); + + aio_context_acquire(aio_context); + block_job_pause(job); + aio_context_release(aio_context); + } + bdrv_drain_all(); QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { @@ -2120,6 +2130,17 @@ cleanup: g_free(bs_entry); } g_free(bs_queue); + + /* Resume all block jobs */ + job = NULL; + while ((job = block_job_next(job))) { + AioContext *aio_context = blk_get_aio_context(job->blk); + + aio_context_acquire(aio_context); + block_job_resume(job); + aio_context_release(aio_context); + } + return ret; }