From patchwork Tue Jun 14 18:17:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 9176443 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 ED07B6021C for ; Tue, 14 Jun 2016 18:26:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DB6A0265B9 for ; Tue, 14 Jun 2016 18:26:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CE7E82818B; Tue, 14 Jun 2016 18:26:14 +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 63A6B265B9 for ; Tue, 14 Jun 2016 18:26:14 +0000 (UTC) Received: from localhost ([::1]:37378 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bCt2T-0000pa-JM for patchwork-qemu-devel@patchwork.kernel.org; Tue, 14 Jun 2016 14:26:13 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33181) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bCstu-00016F-3F for qemu-devel@nongnu.org; Tue, 14 Jun 2016 14:17:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bCstn-0002kV-Rf for qemu-devel@nongnu.org; Tue, 14 Jun 2016 14:17:21 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60370) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bCstn-0002kR-K2 for qemu-devel@nongnu.org; Tue, 14 Jun 2016 14:17:15 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3ED1A8E3E4; Tue, 14 Jun 2016 18:17:15 +0000 (UTC) Received: from localhost (ovpn-112-68.ams2.redhat.com [10.36.112.68]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u5EIHDrJ013924; Tue, 14 Jun 2016 14:17:14 -0400 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Date: Tue, 14 Jun 2016 19:17:05 +0100 Message-Id: <1465928228-1184-3-git-send-email-stefanha@redhat.com> In-Reply-To: <1465928228-1184-1-git-send-email-stefanha@redhat.com> References: <1465928228-1184-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 14 Jun 2016 18:17:15 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 2/5] blockjob: add pause points 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 , Fam Zheng , Stefan Hajnoczi , Jeff Cody , mreitz@redhat.com, jjherne@linux.vnet.ibm.com, Paolo Bonzini Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Block jobs are coroutines that usually perform I/O but sometimes also sleep or yield. Currently only sleeping or yielded block jobs can be paused. This means jobs that do not sleep or yield (using block_job_yield()) are unaffected by block_job_pause(). Add block_job_pause_point() so that block jobs can mark quiescent points that are suitable for pausing. This solves the problem that it can take a block job a long time to pause if it is performing a long series of I/O operations. Transitioning to paused state involves a .pause()/.resume() callback. These callbacks are used to ensure that I/O and event loop activity has ceased while the job is at a pause point. Note that this patch introduces a stricter pause state than previously. The job->busy flag was incorrectly documented as a quiescent state without I/O pending. This is violated by any job that has I/O pending across sleep or block_job_yield(), like the mirror block job. Signed-off-by: Stefan Hajnoczi Reviewed-by: Fam Zheng --- blockjob.c | 44 ++++++++++++++++++++++++++++++++++++-------- include/block/blockjob.h | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 11 deletions(-) diff --git a/blockjob.c b/blockjob.c index 463bccf..1a383d1 100644 --- a/blockjob.c +++ b/blockjob.c @@ -247,6 +247,30 @@ void block_job_complete(BlockJob *job, Error **errp) job->driver->complete(job, errp); } +void block_job_pause_point(BlockJob *job) +{ + if (!block_job_is_paused(job)) { + return; + } + if (block_job_is_cancelled(job)) { + return; + } + + if (job->driver->pause) { + job->driver->pause(job); + } + + job->paused = true; + job->busy = false; + qemu_coroutine_yield(); /* wait for block_job_resume() */ + job->busy = true; + job->paused = false; + + if (job->driver->resume) { + job->driver->resume(job); + } +} + void block_job_pause(BlockJob *job) { job->pause_count++; @@ -360,13 +384,13 @@ void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns) return; } - job->busy = false; - if (block_job_is_paused(job)) { - qemu_coroutine_yield(); - } else { + if (!block_job_is_paused(job)) { + job->busy = false; co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns); + job->busy = true; } - job->busy = true; + + block_job_pause_point(job); } void block_job_yield(BlockJob *job) @@ -378,9 +402,13 @@ void block_job_yield(BlockJob *job) return; } - job->busy = false; - qemu_coroutine_yield(); - job->busy = true; + if (!block_job_is_paused(job)) { + job->busy = false; + qemu_coroutine_yield(); + job->busy = true; + } + + block_job_pause_point(job); } BlockJobInfo *block_job_query(BlockJob *job) diff --git a/include/block/blockjob.h b/include/block/blockjob.h index 00ac418..154c48b 100644 --- a/include/block/blockjob.h +++ b/include/block/blockjob.h @@ -70,6 +70,20 @@ typedef struct BlockJobDriver { * never both. */ void (*abort)(BlockJob *job); + + /** + * If the callback is not NULL, it will be invoked when the job transitions + * into the paused state. Paused jobs must not perform any asynchronous + * I/O or event loop activity. This callback is used to quiesce jobs. + */ + void (*pause)(BlockJob *job); + + /** + * If the callback is not NULL, it will be invoked when the job transitions + * out of the paused state. Any asynchronous I/O or event loop activity + * should be restarted from this callback. + */ + void (*resume)(BlockJob *job); } BlockJobDriver; /** @@ -119,13 +133,19 @@ struct BlockJob { bool user_paused; /** - * Set to false by the job while it is in a quiescent state, where - * no I/O is pending and the job has yielded on any condition - * that is not detected by #aio_poll, such as a timer. + * Set to false by the job while the coroutine has yielded and may be + * re-entered by block_job_enter(). There may still be I/O or event loop + * activity pending. */ bool busy; /** + * Set to true by the job while it is in a quiescent state, where + * no I/O or event loop activity is pending. + */ + bool paused; + + /** * Set to true when the job is ready to be completed. */ bool ready; @@ -299,6 +319,15 @@ bool block_job_is_cancelled(BlockJob *job); BlockJobInfo *block_job_query(BlockJob *job); /** + * block_job_pause_point: + * @job: The job that is ready to pause. + * + * Pause now if block_job_pause() has been called. Block jobs that perform + * lots of I/O must call this between requests so that the job can be paused. + */ +void coroutine_fn block_job_pause_point(BlockJob *job); + +/** * block_job_pause: * @job: The job to be paused. *