From patchwork Fri Jan 28 11:50:13 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: 1585638 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=KCawuHv7; 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 4JlbcR2bFhz9t56 for ; Fri, 28 Jan 2022 23:00:07 +1100 (AEDT) Received: from localhost ([::1]:46516 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDPvB-00084D-3C for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:00:05 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60364) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPm8-0004WH-2U for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:50:45 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:57513) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPm5-0003Nu-Lj for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:50:43 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370640; 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=qxcJppvwnYVfQdCihPNsBkAfFi8EBTfnO0Mo6JSswdc=; b=KCawuHv75oggyJpLlHQ61/ktCYhJGirhmRYUQt+pBc6CvtQuk7YR4OfmDbnUXkbMTl3nf5 xM+Dcue3iMOgTJaXfFT3fD4kFJsMErxBy3hfZxKv6BjpnJse+USuwto8YLKep4xB6Lj5sh oJKvgTuL1EXMHVNxbnvDesO2HrGwHNI= 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-448-J8omojhQMtC1cnyHRANLXQ-1; Fri, 28 Jan 2022 06:50:37 -0500 X-MC-Unique: J8omojhQMtC1cnyHRANLXQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 61CFD1006AA6; Fri, 28 Jan 2022 11:50:36 +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 43FE122DE7; Fri, 28 Jan 2022 11:50:34 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 01/19] job.c: make job_mutex and job_lock/unlock() public Date: Fri, 28 Jan 2022 06:50:13 -0500 Message-Id: <20220128115031.4061565-2-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" job mutex will be used to protect the job struct elements and list, replacing AioContext locks. Right now use a shared lock for all jobs, in order to keep things simple. Once the AioContext lock is gone, we can introduce per-job locks. To simplify the switch from aiocontext to job lock, introduce *nop* lock/unlock functions and macros. We want to always call job_lock/unlock outside the AioContext locks, and not vice-versa, otherwise we might get a deadlock. This is not straightforward to do, and that's why we start with nop functions. Once everything is protected by job_lock/unlock, we can change the nop into an actual mutex and remove the aiocontext lock. Since job_mutex is already being used, add static real_job_{lock/unlock} for the existing usage. Signed-off-by: Emanuele Giuseppe Esposito --- include/qemu/job.h | 24 ++++++++++++++++++++++++ job.c | 35 +++++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/include/qemu/job.h b/include/qemu/job.h index 915ceff425..8d0d370dda 100644 --- a/include/qemu/job.h +++ b/include/qemu/job.h @@ -312,6 +312,30 @@ typedef enum JobCreateFlags { JOB_MANUAL_DISMISS = 0x04, } JobCreateFlags; +extern QemuMutex job_mutex; + +#define JOB_LOCK_GUARD() /* QEMU_LOCK_GUARD(&job_mutex) */ + +#define WITH_JOB_LOCK_GUARD() /* WITH_QEMU_LOCK_GUARD(&job_mutex) */ + +/** + * job_lock: + * + * Take the mutex protecting the list of jobs and their status. + * Most functions called by the monitor need to call job_lock + * and job_unlock manually. On the other hand, function called + * by the block jobs themselves and by the block layer will take the + * lock for you. + */ +void job_lock(void); + +/** + * job_unlock: + * + * Release the mutex protecting the list of jobs and their status. + */ +void job_unlock(void); + /** * Allocate and return a new job transaction. Jobs can be added to the * transaction using job_txn_add_job(). diff --git a/job.c b/job.c index cf0dc9325a..f5e5b04e8c 100644 --- a/job.c +++ b/job.c @@ -32,6 +32,12 @@ #include "trace/trace-root.h" #include "qapi/qapi-events-job.h" +/* + * job_mutex protects the jobs list, but also makes the + * struct job fields thread-safe. + */ +QemuMutex job_mutex; + static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs); /* Job State Transition Table */ @@ -74,17 +80,22 @@ struct JobTxn { int refcnt; }; -/* Right now, this mutex is only needed to synchronize accesses to job->busy - * and job->sleep_timer, such as concurrent calls to job_do_yield and - * job_enter. */ -static QemuMutex job_mutex; +void job_lock(void) +{ + /* nop */ +} + +void job_unlock(void) +{ + /* nop */ +} -static void job_lock(void) +static void real_job_lock(void) { qemu_mutex_lock(&job_mutex); } -static void job_unlock(void) +static void real_job_unlock(void) { qemu_mutex_unlock(&job_mutex); } @@ -450,21 +461,21 @@ void job_enter_cond(Job *job, bool(*fn)(Job *job)) return; } - job_lock(); + real_job_lock(); if (job->busy) { - job_unlock(); + real_job_unlock(); return; } if (fn && !fn(job)) { - job_unlock(); + real_job_unlock(); return; } assert(!job->deferred_to_main_loop); timer_del(&job->sleep_timer); job->busy = true; - job_unlock(); + real_job_unlock(); aio_co_enter(job->aio_context, job->co); } @@ -481,13 +492,13 @@ void job_enter(Job *job) * called explicitly. */ static void coroutine_fn job_do_yield(Job *job, uint64_t ns) { - job_lock(); + real_job_lock(); if (ns != -1) { timer_mod(&job->sleep_timer, ns); } job->busy = false; job_event_idle(job); - job_unlock(); + real_job_unlock(); qemu_coroutine_yield(); /* Set by job_enter_cond() before re-entering the coroutine. */ From patchwork Fri Jan 28 11:50:14 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: 1585636 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=ZV6IX8XF; 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 4JlbWT24Xmz9t5H for ; Fri, 28 Jan 2022 22:55:49 +1100 (AEDT) Received: from localhost ([::1]:38432 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDPr1-0002ZB-2W for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 06:55:47 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60396) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPm9-0004WL-0E for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:50:45 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:29282) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPm6-0003O7-Ri for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:50:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370641; 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=RT2MwcJ1XMATqhJ+/wLnwovAkUHRy8yXLe3wz1foGR8=; b=ZV6IX8XFxFdJm8AXMiiddG5LPKOLsX7zqUuMX01/hOjZa4a1qQnCubYTCYrMxwKCL+Issi DOy6Ew9d6XW/RJ86buw35dqWsQsZCJzeU7su5y/+1ZviwtrnKdoJV2dTx9S7hhg2sldMey nq7a0OnyO+acvb0QWGqPw6F/LZPvJ0o= 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-412-q6_x8tv-NYKBNV7ZQCyQiQ-1; Fri, 28 Jan 2022 06:50:38 -0500 X-MC-Unique: q6_x8tv-NYKBNV7ZQCyQiQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 86D648144E1; Fri, 28 Jan 2022 11:50:37 +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 7B88A1FD88; Fri, 28 Jan 2022 11:50:36 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 02/19] job.h: categorize fields in struct Job Date: Fri, 28 Jan 2022 06:50:14 -0500 Message-Id: <20220128115031.4061565-3-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" Categorize the fields in struct Job to understand which ones need to be protected by the job mutex and which don't. Signed-off-by: Emanuele Giuseppe Esposito --- include/qemu/job.h | 59 ++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/include/qemu/job.h b/include/qemu/job.h index 8d0d370dda..a36ef921c9 100644 --- a/include/qemu/job.h +++ b/include/qemu/job.h @@ -40,27 +40,50 @@ typedef struct JobTxn JobTxn; * Long-running operation. */ typedef struct Job { + + /* Fields set at initialization (job_create), and never modified */ + /** The ID of the job. May be NULL for internal jobs. */ char *id; - /** The type of this job. */ + /** + * The type of this job. + * All callbacks are called with job_mutex *not* held. + */ const JobDriver *driver; - /** Reference count of the block job */ - int refcnt; - - /** Current state; See @JobStatus for details. */ - JobStatus status; - - /** AioContext to run the job coroutine in */ - AioContext *aio_context; - /** * The coroutine that executes the job. If not NULL, it is reentered when * busy is false and the job is cancelled. + * Initialized in job_start() */ Coroutine *co; + /** True if this job should automatically finalize itself */ + bool auto_finalize; + + /** True if this job should automatically dismiss itself */ + bool auto_dismiss; + + /** The completion function that will be called when the job completes. */ + BlockCompletionFunc *cb; + + /** The opaque value that is passed to the completion function. */ + void *opaque; + + /* ProgressMeter API is thread-safe */ + ProgressMeter progress; + + + /** AioContext to run the job coroutine in */ + AioContext *aio_context; + + /** Reference count of the block job */ + int refcnt; + + /** Current state; See @JobStatus for details. */ + JobStatus status; + /** * Timer that is used by @job_sleep_ns. Accessed under job_mutex (in * job.c). @@ -112,14 +135,6 @@ typedef struct Job { /** Set to true when the job has deferred work to the main loop. */ bool deferred_to_main_loop; - /** True if this job should automatically finalize itself */ - bool auto_finalize; - - /** True if this job should automatically dismiss itself */ - bool auto_dismiss; - - ProgressMeter progress; - /** * Return code from @run and/or @prepare callback(s). * Not final until the job has reached the CONCLUDED status. @@ -134,12 +149,6 @@ typedef struct Job { */ Error *err; - /** The completion function that will be called when the job completes. */ - BlockCompletionFunc *cb; - - /** The opaque value that is passed to the completion function. */ - void *opaque; - /** Notifiers called when a cancelled job is finalised */ NotifierList on_finalize_cancelled; @@ -167,6 +176,7 @@ typedef struct Job { /** * Callbacks and other information about a Job driver. + * All callbacks are invoked with job_mutex *not* held. */ struct JobDriver { @@ -481,7 +491,6 @@ void job_yield(Job *job); */ void coroutine_fn job_sleep_ns(Job *job, int64_t ns); - /** Returns the JobType of a given Job. */ JobType job_type(const Job *job); From patchwork Fri Jan 28 11:50:15 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: 1585650 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=JI+2RAGH; 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 4Jlbt328vGz9t56 for ; Fri, 28 Jan 2022 23:11:53 +1100 (AEDT) Received: from localhost ([::1]:55182 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDQ6Y-0006X2-IO for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:11:50 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60410) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPm9-0004WN-8O for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:50:45 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:22025) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPm7-0003OL-5N for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:50:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370642; 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=SqoxSw1h5BeFcY2tzotqevjRb8efoOUpor1OkE2Ew3s=; b=JI+2RAGHYYllzyfxBn812+TgDBpIsJsgCsKeQDpQegDFUN1IBBjGrToz9dtHOLA3G9aZX+ p/Z79kuPgJg6lw24x6DToqdSmQqKqnxFWWLzKD7zXIeNng1IfXr8hqFBfNMTOi4YktcUOV Q1VkTYfm2Opo2bAKzOqAvre2YGXUdrY= 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-628-azbzvunkPd-4BGoY22QqzA-1; Fri, 28 Jan 2022 06:50:39 -0500 X-MC-Unique: azbzvunkPd-4BGoY22QqzA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9648A1006AA0; Fri, 28 Jan 2022 11:50:38 +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 A083722DE3; Fri, 28 Jan 2022 11:50:37 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 03/19] job.c: make job_event_* functions static Date: Fri, 28 Jan 2022 06:50:15 -0500 Message-Id: <20220128115031.4061565-4-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" job_event_* functions can all be static, as they are not used outside job.c. Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Stefan Hajnoczi --- include/qemu/job.h | 6 ------ job.c | 12 ++++++++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/qemu/job.h b/include/qemu/job.h index a36ef921c9..6f22a20d58 100644 --- a/include/qemu/job.h +++ b/include/qemu/job.h @@ -438,12 +438,6 @@ void job_progress_set_remaining(Job *job, uint64_t remaining); */ void job_progress_increase_remaining(Job *job, uint64_t delta); -/** To be called when a cancelled job is finalised. */ -void job_event_cancelled(Job *job); - -/** To be called when a successfully completed job is finalised. */ -void job_event_completed(Job *job); - /** * Conditionally enter the job coroutine if the job is ready to run, not * already busy and fn() returns true. fn() is called while under the job_lock diff --git a/job.c b/job.c index f5e5b04e8c..e97b159396 100644 --- a/job.c +++ b/job.c @@ -427,12 +427,20 @@ void job_progress_increase_remaining(Job *job, uint64_t delta) progress_increase_remaining(&job->progress, delta); } -void job_event_cancelled(Job *job) +/** + * To be called when a cancelled job is finalised. + * Called with job_mutex held. + */ +static void job_event_cancelled(Job *job) { notifier_list_notify(&job->on_finalize_cancelled, job); } -void job_event_completed(Job *job) +/** + * To be called when a successfully completed job is finalised. + * Called with job_mutex held. + */ +static void job_event_completed(Job *job) { notifier_list_notify(&job->on_finalize_completed, job); } From patchwork Fri Jan 28 11:50:16 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: 1585633 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=aXs57xE2; 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 4JlbSP2b7Pz9t6g for ; Fri, 28 Jan 2022 22:53:09 +1100 (AEDT) Received: from localhost ([::1]:58154 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDPoR-0005LI-3q for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 06:53:07 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60472) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmC-0004YL-Hf for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:50:49 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:29763) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPm8-0003Oq-Pl for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:50:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370644; 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=wSNdqnXVltImj1xnDIfilmZEvtW+JONldwLSI+K6V7M=; b=aXs57xE24BsDKYcF3F1NN8EbzM78fDrVJHgv6r6u/k9A4M/zSNdwDuHRwmZCgD09pijMHm y1mZc6llRSXVTLw95EYDaWKahDctFgkmBp8casoXFaP9WAH5kKJEE7091FRtDB/D2RTVSl KgzUzoncZ5P6ZMu2kHHSz+07xqGW9OA= 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-117-BivP5EwbNzK_wa0Pl0Aayg-1; Fri, 28 Jan 2022 06:50:41 -0500 X-MC-Unique: BivP5EwbNzK_wa0Pl0Aayg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B722E1083F69; Fri, 28 Jan 2022 11:50:39 +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 B11A71F478; Fri, 28 Jan 2022 11:50:38 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 04/19] job.c: move inner aiocontext lock in callbacks Date: Fri, 28 Jan 2022 06:50:16 -0500 Message-Id: <20220128115031.4061565-5-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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.129.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" Instead of having the lock in job_tnx_apply, move it inside in the callback. This will be helpful for next commits, when we introduce job_lock/unlock pairs. job_transition_to_pending() and job_needs_finalize() do not need to be protected by the aiocontext lock. No functional change intended. Signed-off-by: Emanuele Giuseppe Esposito --- job.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/job.c b/job.c index e97b159396..2b88cef19d 100644 --- a/job.c +++ b/job.c @@ -149,7 +149,6 @@ static void job_txn_del_job(Job *job) static int job_txn_apply(Job *job, int fn(Job *)) { - AioContext *inner_ctx; Job *other_job, *next; JobTxn *txn = job->txn; int rc = 0; @@ -164,10 +163,7 @@ static int job_txn_apply(Job *job, int fn(Job *)) aio_context_release(job->aio_context); QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) { - inner_ctx = other_job->aio_context; - aio_context_acquire(inner_ctx); rc = fn(other_job); - aio_context_release(inner_ctx); if (rc) { break; } @@ -719,11 +715,15 @@ static void job_clean(Job *job) static int job_finalize_single(Job *job) { + AioContext *ctx = job->aio_context; + assert(job_is_completed(job)); /* Ensure abort is called for late-transactional failures */ job_update_rc(job); + aio_context_acquire(ctx); + if (!job->ret) { job_commit(job); } else { @@ -731,6 +731,8 @@ static int job_finalize_single(Job *job) } job_clean(job); + aio_context_release(ctx); + if (job->cb) { job->cb(job->opaque, job->ret); } @@ -835,8 +837,8 @@ static void job_completed_txn_abort(Job *job) assert(job_cancel_requested(other_job)); job_finish_sync(other_job, NULL, NULL); } - job_finalize_single(other_job); aio_context_release(ctx); + job_finalize_single(other_job); } /* @@ -851,11 +853,16 @@ static void job_completed_txn_abort(Job *job) static int job_prepare(Job *job) { + AioContext *ctx = job->aio_context; assert(qemu_in_main_thread()); + if (job->ret == 0 && job->driver->prepare) { + aio_context_acquire(ctx); job->ret = job->driver->prepare(job); + aio_context_release(ctx); job_update_rc(job); } + return job->ret; } From patchwork Fri Jan 28 11:50:17 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: 1585656 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=hXoBOSK8; 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 4Jlc0t4hgjz9t56 for ; Fri, 28 Jan 2022 23:17:49 +1100 (AEDT) Received: from localhost ([::1]:35576 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDQCH-0004li-8o for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:17:45 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60496) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmF-0004Yl-0f for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:50:51 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:31467) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmA-0003PR-AT for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:50:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370645; 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=2GFPYF3rd7vEl2S21B2p7zNKVat/lv/1EGml81GNS7s=; b=hXoBOSK8T2Wk5tbzQ5mc5t96/b7lpySBVs6vRdWYrvy69Dyw+6X3CIvlTny0VIzNaSy+Ka RtFCYQ0uAm12Hx9ZrAt6Kuly/RMuS7lc4GOuNrJZ4qRA60eZ8yd7vmIFZ1o43UzJ7DxO8C IZhtKNArI4DXsUNnV5ePG7K65BMM0+g= 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-484-1I0gxOsgMeqEK3F5C7zQLA-1; Fri, 28 Jan 2022 06:50:42 -0500 X-MC-Unique: 1I0gxOsgMeqEK3F5C7zQLA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C78C518460E6; Fri, 28 Jan 2022 11:50:40 +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 D08EF22DE5; Fri, 28 Jan 2022 11:50:39 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 05/19] aio-wait.h: introduce AIO_WAIT_WHILE_UNLOCKED Date: Fri, 28 Jan 2022 06:50:17 -0500 Message-Id: <20220128115031.4061565-6-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" Same as AIO_WAIT_WHILE macro, but if we are in the Main loop do not release and then acquire ctx_ 's aiocontext. Once all Aiocontext locks go away, this macro will replace AIO_WAIT_WHILE. Signed-off-by: Emanuele Giuseppe Esposito --- include/block/aio-wait.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/include/block/aio-wait.h b/include/block/aio-wait.h index b39eefb38d..ff27fe4eab 100644 --- a/include/block/aio-wait.h +++ b/include/block/aio-wait.h @@ -59,10 +59,11 @@ typedef struct { extern AioWait global_aio_wait; /** - * AIO_WAIT_WHILE: + * _AIO_WAIT_WHILE: * @ctx: the aio context, or NULL if multiple aio contexts (for which the * caller does not hold a lock) are involved in the polling condition. * @cond: wait while this conditional expression is true + * @unlock: whether to unlock and then lock again @ctx * * Wait while a condition is true. Use this to implement synchronous * operations that require event loop activity. @@ -75,7 +76,7 @@ extern AioWait global_aio_wait; * wait on conditions between two IOThreads since that could lead to deadlock, * go via the main loop instead. */ -#define AIO_WAIT_WHILE(ctx, cond) ({ \ +#define _AIO_WAIT_WHILE(ctx, cond, unlock) ({ \ bool waited_ = false; \ AioWait *wait_ = &global_aio_wait; \ AioContext *ctx_ = (ctx); \ @@ -90,11 +91,11 @@ extern AioWait global_aio_wait; assert(qemu_get_current_aio_context() == \ qemu_get_aio_context()); \ while ((cond)) { \ - if (ctx_) { \ + if (unlock && ctx_) { \ aio_context_release(ctx_); \ } \ aio_poll(qemu_get_aio_context(), true); \ - if (ctx_) { \ + if (unlock && ctx_) { \ aio_context_acquire(ctx_); \ } \ waited_ = true; \ @@ -103,6 +104,12 @@ extern AioWait global_aio_wait; qatomic_dec(&wait_->num_waiters); \ waited_; }) +#define AIO_WAIT_WHILE(ctx, cond) \ + _AIO_WAIT_WHILE(ctx, cond, true) + +#define AIO_WAIT_WHILE_UNLOCKED(ctx, cond) \ + _AIO_WAIT_WHILE(ctx, cond, false) + /** * aio_wait_kick: * Wake up the main thread if it is waiting on AIO_WAIT_WHILE(). During From patchwork Fri Jan 28 11:50:18 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: 1585657 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=L4gt0Tpv; 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 4Jlc1W4bJ2z9t56 for ; Fri, 28 Jan 2022 23:18:23 +1100 (AEDT) Received: from localhost ([::1]:37502 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDQCr-00065b-5o for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:18:21 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60860) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmW-0004dw-3u for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:08 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:35789) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmQ-0003QZ-13 for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370651; 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=42k9Q+BSGe1An4SoOJQbGA3ba7tKXbuGjZHnsMR2VFQ=; b=L4gt0Tpv+3Fv5VQBMQrlytryZoPlitbvzjhj21EW0+uwfmi8bNgOa64JrvsQ9mATDP2+IP YzB9Ql30NKPI+F5kCMKQYros+lQelnDddwCc2L6Mr+/cf2UTrNb9wm5Wy3fUMGOWOQKBEM 33HsevbcHg/lBkNdaIzWXREG4lm1tF0= 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-252-rL7nv7lDNdCSf4TEDCSNDw-1; Fri, 28 Jan 2022 06:50:43 -0500 X-MC-Unique: rL7nv7lDNdCSf4TEDCSNDw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D6E212F26; Fri, 28 Jan 2022 11:50:41 +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 E0CB622DE4; Fri, 28 Jan 2022 11:50:40 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 06/19] jobs: remove aiocontext locks since the functions are under BQL Date: Fri, 28 Jan 2022 06:50:18 -0500 Message-Id: <20220128115031.4061565-7-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" In preparation to the job_lock/unlock patch, remove these aiocontext locks. The main reason these two locks are removed here is because they are inside a loop iterating on the jobs list. Once the job_lock is added, it will have to protect the whole loop, wrapping also the aiocontext acquire/release. We don't want this, as job_lock must be taken inside the AioContext lock, and taking it outside would cause deadlocks. Signed-off-by: Emanuele Giuseppe Esposito --- blockdev.c | 4 ---- job-qmp.c | 4 ---- 2 files changed, 8 deletions(-) diff --git a/blockdev.c b/blockdev.c index 800e95ba54..45a24fee8a 100644 --- a/blockdev.c +++ b/blockdev.c @@ -3713,15 +3713,11 @@ BlockJobInfoList *qmp_query_block_jobs(Error **errp) for (job = block_job_next(NULL); job; job = block_job_next(job)) { BlockJobInfo *value; - AioContext *aio_context; if (block_job_is_internal(job)) { continue; } - aio_context = block_job_get_aio_context(job); - aio_context_acquire(aio_context); value = block_job_query(job, errp); - aio_context_release(aio_context); if (!value) { qapi_free_BlockJobInfoList(head); return NULL; diff --git a/job-qmp.c b/job-qmp.c index 829a28aa70..a6774aaaa5 100644 --- a/job-qmp.c +++ b/job-qmp.c @@ -173,15 +173,11 @@ JobInfoList *qmp_query_jobs(Error **errp) for (job = job_next(NULL); job; job = job_next(job)) { JobInfo *value; - AioContext *aio_context; if (job_is_internal(job)) { continue; } - aio_context = job->aio_context; - aio_context_acquire(aio_context); value = job_query_single(job, errp); - aio_context_release(aio_context); if (!value) { qapi_free_JobInfoList(head); return NULL; From patchwork Fri Jan 28 11:50:19 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: 1585635 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=KrtIv8xT; 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 4JlbW83jKJz9t5H for ; Fri, 28 Jan 2022 22:55:32 +1100 (AEDT) Received: from localhost ([::1]:37068 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDPqk-0001ej-As for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 06:55:30 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60510) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmI-0004Zj-Pp for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:50:58 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:30619) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmC-0003Pi-52 for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:50:50 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370647; 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=kCQoDTdCqX47voMvMQELdUXUIJy2lxwMFhhXZJgUkOA=; b=KrtIv8xTe8q5clgIU7o/ad0c/JuqHNoQq+Us2AzDF8+EHxArlCZbOD9aO4GcYQVnFc7r/3 7N6zCrkCIe4qkBx877vIV/zQWFmgVcGCOZMPwPYyCdGklxr5C1tim4QaNR30P/4FK+u0yY uAKAB0kQnjWxWXDiKIbfAVCP2Dff3gQ= 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-303-SmH5xgVvPPyaehwXFbfuEA-1; Fri, 28 Jan 2022 06:50:44 -0500 X-MC-Unique: SmH5xgVvPPyaehwXFbfuEA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EAA482F4A; Fri, 28 Jan 2022 11:50:42 +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 F2A6522DE3; Fri, 28 Jan 2022 11:50:41 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 07/19] jobs: protect jobs with job_lock/unlock Date: Fri, 28 Jan 2022 06:50:19 -0500 Message-Id: <20220128115031.4061565-8-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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.129.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" Introduce the job locking mechanism through the whole job API, following the comments and requirements of job-monitor (assume lock is held) and job-driver (lock is not held). job_{lock/unlock} is independent from real_job_{lock/unlock}. Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*. Signed-off-by: Emanuele Giuseppe Esposito --- block.c | 18 +++++--- block/replication.c | 8 +++- blockdev.c | 17 +++++-- blockjob.c | 60 +++++++++++++++++------- job-qmp.c | 2 + job.c | 110 ++++++++++++++++++++++++++++++++------------ monitor/qmp-cmds.c | 6 ++- qemu-img.c | 41 ++++++++++------- 8 files changed, 185 insertions(+), 77 deletions(-) diff --git a/block.c b/block.c index afa18653d1..5e1ab8d692 100644 --- a/block.c +++ b/block.c @@ -4972,7 +4972,9 @@ static void bdrv_close(BlockDriverState *bs) void bdrv_close_all(void) { - assert(job_next(NULL) == NULL); + WITH_JOB_LOCK_GUARD() { + assert(job_next(NULL) == NULL); + } assert(qemu_in_main_thread()); /* Drop references from requests still in flight, such as canceled block @@ -6149,13 +6151,15 @@ XDbgBlockGraph *bdrv_get_xdbg_block_graph(Error **errp) } } - for (job = block_job_next(NULL); job; job = block_job_next(job)) { - GSList *el; + WITH_JOB_LOCK_GUARD() { + for (job = block_job_next(NULL); job; job = block_job_next(job)) { + GSList *el; - xdbg_graph_add_node(gr, job, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_JOB, - job->job.id); - for (el = job->nodes; el; el = el->next) { - xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data); + xdbg_graph_add_node(gr, job, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_JOB, + job->job.id); + for (el = job->nodes; el; el = el->next) { + xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data); + } } } diff --git a/block/replication.c b/block/replication.c index 55c8f894aa..a03b28726e 100644 --- a/block/replication.c +++ b/block/replication.c @@ -149,7 +149,9 @@ static void replication_close(BlockDriverState *bs) if (s->stage == BLOCK_REPLICATION_FAILOVER) { commit_job = &s->commit_job->job; assert(commit_job->aio_context == qemu_get_current_aio_context()); - job_cancel_sync(commit_job, false); + WITH_JOB_LOCK_GUARD() { + job_cancel_sync(commit_job, false); + } } if (s->mode == REPLICATION_MODE_SECONDARY) { @@ -726,7 +728,9 @@ static void replication_stop(ReplicationState *rs, bool failover, Error **errp) * disk, secondary disk in backup_job_completed(). */ if (s->backup_job) { - job_cancel_sync(&s->backup_job->job, true); + WITH_JOB_LOCK_GUARD() { + job_cancel_sync(&s->backup_job->job, true); + } } if (!failover) { diff --git a/blockdev.c b/blockdev.c index 45a24fee8a..2e522dd7fe 100644 --- a/blockdev.c +++ b/blockdev.c @@ -150,6 +150,8 @@ void blockdev_mark_auto_del(BlockBackend *blk) return; } + JOB_LOCK_GUARD(); + for (job = block_job_next(NULL); job; job = block_job_next(job)) { if (block_job_has_bdrv(job, blk_bs(blk))) { AioContext *aio_context = job->job.aio_context; @@ -1838,7 +1840,9 @@ static void drive_backup_abort(BlkActionState *common) aio_context = bdrv_get_aio_context(state->bs); aio_context_acquire(aio_context); - job_cancel_sync(&state->job->job, true); + WITH_JOB_LOCK_GUARD() { + job_cancel_sync(&state->job->job, true); + } aio_context_release(aio_context); } @@ -1939,7 +1943,9 @@ static void blockdev_backup_abort(BlkActionState *common) aio_context = bdrv_get_aio_context(state->bs); aio_context_acquire(aio_context); - job_cancel_sync(&state->job->job, true); + WITH_JOB_LOCK_GUARD() { + job_cancel_sync(&state->job->job, true); + } aio_context_release(aio_context); } @@ -2388,7 +2394,10 @@ exit: if (!has_props) { qapi_free_TransactionProperties(props); } - job_txn_unref(block_job_txn); + + WITH_JOB_LOCK_GUARD() { + job_txn_unref(block_job_txn); + } } BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node, @@ -3711,6 +3720,8 @@ BlockJobInfoList *qmp_query_block_jobs(Error **errp) BlockJobInfoList *head = NULL, **tail = &head; BlockJob *job; + JOB_LOCK_GUARD(); + for (job = block_job_next(NULL); job; job = block_job_next(job)) { BlockJobInfo *value; diff --git a/blockjob.c b/blockjob.c index cce8146cb6..9fd1b6972f 100644 --- a/blockjob.c +++ b/blockjob.c @@ -102,7 +102,9 @@ static char *child_job_get_parent_desc(BdrvChild *c) static void child_job_drained_begin(BdrvChild *c) { BlockJob *job = c->opaque; - job_pause(&job->job); + WITH_JOB_LOCK_GUARD() { + job_pause(&job->job); + } } static bool child_job_drained_poll(BdrvChild *c) @@ -114,8 +116,10 @@ static bool child_job_drained_poll(BdrvChild *c) /* 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 * being reentered, so no job driver code will run before they pause. */ - if (!job->busy || job_is_completed(job)) { - return false; + WITH_JOB_LOCK_GUARD() { + if (!job->busy || job_is_completed(job)) { + return false; + } } /* Otherwise, assume that it isn't fully stopped yet, but allow the job to @@ -130,7 +134,9 @@ static bool child_job_drained_poll(BdrvChild *c) static void child_job_drained_end(BdrvChild *c, int *drained_end_counter) { BlockJob *job = c->opaque; - job_resume(&job->job); + WITH_JOB_LOCK_GUARD() { + job_resume(&job->job); + } } static bool child_job_can_set_aio_ctx(BdrvChild *c, AioContext *ctx, @@ -252,7 +258,13 @@ int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs, static void block_job_on_idle(Notifier *n, void *opaque) { + /* + * we can't kick with job_mutex held, but we also want + * to protect the notifier list. + */ + job_unlock(); aio_wait_kick(); + job_lock(); } bool block_job_is_internal(BlockJob *job) @@ -292,7 +304,9 @@ bool block_job_set_speed(BlockJob *job, int64_t speed, Error **errp) job->speed = speed; if (drv->set_speed) { + job_unlock(); drv->set_speed(job, speed); + job_lock(); } if (speed && speed <= old_speed) { @@ -469,13 +483,15 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver, job->ready_notifier.notify = block_job_event_ready; job->idle_notifier.notify = block_job_on_idle; - notifier_list_add(&job->job.on_finalize_cancelled, - &job->finalize_cancelled_notifier); - notifier_list_add(&job->job.on_finalize_completed, - &job->finalize_completed_notifier); - notifier_list_add(&job->job.on_pending, &job->pending_notifier); - notifier_list_add(&job->job.on_ready, &job->ready_notifier); - notifier_list_add(&job->job.on_idle, &job->idle_notifier); + WITH_JOB_LOCK_GUARD() { + notifier_list_add(&job->job.on_finalize_cancelled, + &job->finalize_cancelled_notifier); + notifier_list_add(&job->job.on_finalize_completed, + &job->finalize_completed_notifier); + notifier_list_add(&job->job.on_pending, &job->pending_notifier); + notifier_list_add(&job->job.on_ready, &job->ready_notifier); + notifier_list_add(&job->job.on_idle, &job->idle_notifier); + } error_setg(&job->blocker, "block device is in use by block job: %s", job_type_str(&job->job)); @@ -487,7 +503,10 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver, bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker); - if (!block_job_set_speed(job, speed, errp)) { + WITH_JOB_LOCK_GUARD() { + ret = block_job_set_speed(job, speed, errp); + } + if (!ret) { goto fail; } @@ -512,7 +531,9 @@ void block_job_user_resume(Job *job) { BlockJob *bjob = container_of(job, BlockJob, job); assert(qemu_in_main_thread()); - block_job_iostatus_reset(bjob); + WITH_JOB_LOCK_GUARD() { + block_job_iostatus_reset(bjob); + } } BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err, @@ -545,10 +566,15 @@ BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err, action); } if (action == BLOCK_ERROR_ACTION_STOP) { - if (!job->job.user_paused) { - job_pause(&job->job); - /* make the pause user visible, which will be resumed from QMP. */ - job->job.user_paused = true; + WITH_JOB_LOCK_GUARD() { + if (!job->job.user_paused) { + job_pause(&job->job); + /* + * make the pause user visible, which will be + * resumed from QMP. + */ + job->job.user_paused = true; + } } block_job_iostatus_set_err(job, error); } diff --git a/job-qmp.c b/job-qmp.c index a6774aaaa5..06f970f6cf 100644 --- a/job-qmp.c +++ b/job-qmp.c @@ -171,6 +171,8 @@ JobInfoList *qmp_query_jobs(Error **errp) JobInfoList *head = NULL, **tail = &head; Job *job; + JOB_LOCK_GUARD(); + for (job = job_next(NULL); job; job = job_next(job)) { JobInfo *value; diff --git a/job.c b/job.c index 2b88cef19d..4947a58dcb 100644 --- a/job.c +++ b/job.c @@ -322,6 +322,8 @@ void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn, { Job *job; + JOB_LOCK_GUARD(); + if (job_id) { if (flags & JOB_INTERNAL) { error_setg(errp, "Cannot specify job ID for internal job"); @@ -396,7 +398,9 @@ void job_unref(Job *job) assert(!job->txn); if (job->driver->free) { + job_unlock(); job->driver->free(job); + job_lock(); } QLIST_REMOVE(job, job_list); @@ -485,6 +489,7 @@ void job_enter_cond(Job *job, bool(*fn)(Job *job)) void job_enter(Job *job) { + JOB_LOCK_GUARD(); job_enter_cond(job, NULL); } @@ -503,7 +508,9 @@ static void coroutine_fn job_do_yield(Job *job, uint64_t ns) job->busy = false; job_event_idle(job); real_job_unlock(); + job_unlock(); qemu_coroutine_yield(); + job_lock(); /* Set by job_enter_cond() before re-entering the coroutine. */ assert(job->busy); @@ -513,15 +520,20 @@ void coroutine_fn job_pause_point(Job *job) { assert(job && job_started(job)); + job_lock(); if (!job_should_pause(job)) { + job_unlock(); return; } if (job_is_cancelled(job)) { + job_unlock(); return; } if (job->driver->pause) { + job_unlock(); job->driver->pause(job); + job_lock(); } if (job_should_pause(job) && !job_is_cancelled(job)) { @@ -534,6 +546,7 @@ void coroutine_fn job_pause_point(Job *job) job->paused = false; job_state_transition(job, status); } + job_unlock(); if (job->driver->resume) { job->driver->resume(job); @@ -542,15 +555,17 @@ void coroutine_fn job_pause_point(Job *job) void job_yield(Job *job) { - assert(job->busy); + WITH_JOB_LOCK_GUARD() { + assert(job->busy); - /* Check cancellation *before* setting busy = false, too! */ - if (job_is_cancelled(job)) { - return; - } + /* Check cancellation *before* setting busy = false, too! */ + if (job_is_cancelled(job)) { + return; + } - if (!job_should_pause(job)) { - job_do_yield(job, -1); + if (!job_should_pause(job)) { + job_do_yield(job, -1); + } } job_pause_point(job); @@ -558,21 +573,23 @@ void job_yield(Job *job) void coroutine_fn job_sleep_ns(Job *job, int64_t ns) { - assert(job->busy); + WITH_JOB_LOCK_GUARD() { + assert(job->busy); - /* Check cancellation *before* setting busy = false, too! */ - if (job_is_cancelled(job)) { - return; - } + /* Check cancellation *before* setting busy = false, too! */ + if (job_is_cancelled(job)) { + return; + } - if (!job_should_pause(job)) { - job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns); + if (!job_should_pause(job)) { + job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns); + } } job_pause_point(job); } -/* Assumes the block_job_mutex is held */ +/* Assumes the job_mutex is held */ static bool job_timer_not_pending(Job *job) { return !timer_pending(&job->sleep_timer); @@ -582,7 +599,7 @@ void job_pause(Job *job) { job->pause_count++; if (!job->paused) { - job_enter(job); + job_enter_cond(job, NULL); } } @@ -628,7 +645,9 @@ void job_user_resume(Job *job, Error **errp) return; } if (job->driver->user_resume) { + job_unlock(); job->driver->user_resume(job); + job_lock(); } job->user_paused = false; job_resume(job); @@ -692,7 +711,9 @@ static void job_commit(Job *job) assert(!job->ret); assert(qemu_in_main_thread()); if (job->driver->commit) { + job_unlock(); job->driver->commit(job); + job_lock(); } } @@ -701,7 +722,9 @@ static void job_abort(Job *job) assert(job->ret); assert(qemu_in_main_thread()); if (job->driver->abort) { + job_unlock(); job->driver->abort(job); + job_lock(); } } @@ -709,12 +732,15 @@ static void job_clean(Job *job) { assert(qemu_in_main_thread()); if (job->driver->clean) { + job_unlock(); job->driver->clean(job); + job_lock(); } } static int job_finalize_single(Job *job) { + int job_ret; AioContext *ctx = job->aio_context; assert(job_is_completed(job)); @@ -734,7 +760,10 @@ static int job_finalize_single(Job *job) aio_context_release(ctx); if (job->cb) { - job->cb(job->opaque, job->ret); + job_ret = job->ret; + job_unlock(); + job->cb(job->opaque, job_ret); + job_lock(); } /* Emit events only if we actually started */ @@ -755,7 +784,9 @@ static void job_cancel_async(Job *job, bool force) { assert(qemu_in_main_thread()); if (job->driver->cancel) { + job_unlock(); force = job->driver->cancel(job, force); + job_lock(); } else { /* No .cancel() means the job will behave as if force-cancelled */ force = true; @@ -764,7 +795,9 @@ static void job_cancel_async(Job *job, bool force) if (job->user_paused) { /* Do not call job_enter here, the caller will handle it. */ if (job->driver->user_resume) { + job_unlock(); job->driver->user_resume(job); + job_lock(); } job->user_paused = false; assert(job->pause_count > 0); @@ -853,13 +886,17 @@ static void job_completed_txn_abort(Job *job) static int job_prepare(Job *job) { + int ret; AioContext *ctx = job->aio_context; assert(qemu_in_main_thread()); if (job->ret == 0 && job->driver->prepare) { + job_unlock(); aio_context_acquire(ctx); - job->ret = job->driver->prepare(job); + ret = job->driver->prepare(job); aio_context_release(ctx); + job_lock(); + job->ret = ret; job_update_rc(job); } @@ -905,6 +942,7 @@ static int job_transition_to_pending(Job *job) void job_transition_to_ready(Job *job) { + JOB_LOCK_GUARD(); job_state_transition(job, JOB_STATUS_READY); job_event_ready(job); } @@ -954,6 +992,7 @@ static void job_exit(void *opaque) Job *job = (Job *)opaque; AioContext *ctx; + JOB_LOCK_GUARD(); job_ref(job); aio_context_acquire(job->aio_context); @@ -984,12 +1023,16 @@ static void job_exit(void *opaque) static void coroutine_fn job_co_entry(void *opaque) { Job *job = opaque; + int ret; assert(job && job->driver && job->driver->run); job_pause_point(job); - job->ret = job->driver->run(job, &job->err); - job->deferred_to_main_loop = true; - job->busy = true; + ret = job->driver->run(job, &job->err); + WITH_JOB_LOCK_GUARD() { + job->ret = ret; + job->deferred_to_main_loop = true; + job->busy = true; + } aio_bh_schedule_oneshot(qemu_get_aio_context(), job_exit, job); } @@ -1005,16 +1048,20 @@ static int job_pre_run(Job *job) void job_start(Job *job) { - assert(job && !job_started(job) && job->paused && - job->driver && job->driver->run); - job->co = qemu_coroutine_create(job_co_entry, job); + WITH_JOB_LOCK_GUARD() { + assert(job && !job_started(job) && job->paused && + job->driver && job->driver->run); + job->co = qemu_coroutine_create(job_co_entry, job); + } if (job_pre_run(job)) { return; } - job->pause_count--; - job->busy = true; - job->paused = false; - job_state_transition(job, JOB_STATUS_RUNNING); + WITH_JOB_LOCK_GUARD() { + job->pause_count--; + job->busy = true; + job->paused = false; + job_state_transition(job, JOB_STATUS_RUNNING); + } aio_co_enter(job->aio_context, job->co); } @@ -1042,7 +1089,7 @@ void job_cancel(Job *job, bool force) job_completed_txn_abort(job); } } else { - job_enter(job); + job_enter_cond(job, NULL); } } @@ -1084,6 +1131,7 @@ void job_cancel_sync_all(void) Job *job; AioContext *aio_context; + JOB_LOCK_GUARD(); while ((job = job_next(NULL))) { aio_context = job->aio_context; aio_context_acquire(aio_context); @@ -1111,7 +1159,9 @@ void job_complete(Job *job, Error **errp) return; } + job_unlock(); job->driver->complete(job, errp); + job_lock(); } int job_finish_sync(Job *job, void (*finish)(Job *, Error **errp), Error **errp) @@ -1130,8 +1180,10 @@ int job_finish_sync(Job *job, void (*finish)(Job *, Error **errp), Error **errp) return -EBUSY; } + job_unlock(); AIO_WAIT_WHILE(job->aio_context, (job_enter(job), !job_is_completed(job))); + job_lock(); ret = (job_is_cancelled(job) && job->ret == 0) ? -ECANCELED : job->ret; job_unref(job); diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c index f97bf7ca77..f935fa4ad5 100644 --- a/monitor/qmp-cmds.c +++ b/monitor/qmp-cmds.c @@ -134,8 +134,10 @@ void qmp_cont(Error **errp) blk_iostatus_reset(blk); } - for (job = block_job_next(NULL); job; job = block_job_next(job)) { - block_job_iostatus_reset(job); + WITH_JOB_LOCK_GUARD() { + for (job = block_job_next(NULL); job; job = block_job_next(job)) { + block_job_iostatus_reset(job); + } } /* Continuing after completed migration. Images have been inactivated to diff --git a/qemu-img.c b/qemu-img.c index 6fe2466032..a805f6fe7a 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -906,25 +906,30 @@ static void run_block_job(BlockJob *job, Error **errp) int ret = 0; aio_context_acquire(aio_context); - job_ref(&job->job); - do { - float progress = 0.0f; - aio_poll(aio_context, true); + WITH_JOB_LOCK_GUARD() { + job_ref(&job->job); + do { + float progress = 0.0f; + job_unlock(); + aio_poll(aio_context, true); + + progress_get_snapshot(&job->job.progress, &progress_current, + &progress_total); + if (progress_total) { + progress = (float)progress_current / progress_total * 100.f; + } + qemu_progress_print(progress, 0); + job_lock(); + } while (!job_is_ready(&job->job) && + !job_is_completed(&job->job)); - progress_get_snapshot(&job->job.progress, &progress_current, - &progress_total); - if (progress_total) { - progress = (float)progress_current / progress_total * 100.f; + if (!job_is_completed(&job->job)) { + ret = job_complete_sync(&job->job, errp); + } else { + ret = job->job.ret; } - qemu_progress_print(progress, 0); - } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); - - if (!job_is_completed(&job->job)) { - ret = job_complete_sync(&job->job, errp); - } else { - ret = job->job.ret; + job_unref(&job->job); } - job_unref(&job->job); aio_context_release(aio_context); /* publish completion progress only when success */ @@ -1077,7 +1082,9 @@ static int img_commit(int argc, char **argv) bdrv_ref(bs); } - job = block_job_get("commit"); + WITH_JOB_LOCK_GUARD() { + job = block_job_get("commit"); + } assert(job); run_block_job(job, &local_err); if (local_err) { From patchwork Fri Jan 28 11:50:20 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: 1585637 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=O+pCiA+w; 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 4JlbWd4b3zz9t5H for ; Fri, 28 Jan 2022 22:55:57 +1100 (AEDT) Received: from localhost ([::1]:39296 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDPr9-0003Fc-FN for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 06:55:55 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60660) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmQ-0004ac-94 for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:05 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:34649) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmK-0003RB-GA for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:01 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370653; 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=aphXLxgTJOPr15I2ZBMTkc/OIrSbqVG/Sb/p/DJmIGM=; b=O+pCiA+wTbDFDqMQo/AqhPjoh3aswyNPIdtHnA2URPQdVPlK/BAZhAIv4IJZ8wmQCB9gZr fioPs/w1lZRJWTSR2y0Eic5bW/hNUemiYQHG84N+o5jr30EnLNTmpbUpjHpUTt+MPL8SDR upaJstspF/dQyOUsOJyi7xcuGfTFo3k= 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-355-k2ioY-ueMXuumICJ1VHH5Q-1; Fri, 28 Jan 2022 06:50:45 -0500 X-MC-Unique: k2ioY-ueMXuumICJ1VHH5Q-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 07B661083F62; Fri, 28 Jan 2022 11:50:44 +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 10CB922DE4; Fri, 28 Jan 2022 11:50:43 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 08/19] jobs: add job lock in find_* functions Date: Fri, 28 Jan 2022 06:50:20 -0500 Message-Id: <20220128115031.4061565-9-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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.129.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" Both blockdev.c and job-qmp.c have TOC/TOU conditions, because they first search for the job and then perform an action on it. Therefore, we need to do the search + action under the same job mutex critical section. Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*. Signed-off-by: Emanuele Giuseppe Esposito --- blockdev.c | 14 +++++++++++++- job-qmp.c | 13 ++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/blockdev.c b/blockdev.c index 2e522dd7fe..7a9e9b4a13 100644 --- a/blockdev.c +++ b/blockdev.c @@ -3311,7 +3311,10 @@ out: aio_context_release(aio_context); } -/* Get a block job using its ID and acquire its AioContext */ +/* + * Get a block job using its ID and acquire its AioContext. + * Returns with job_lock held on success. + */ static BlockJob *find_block_job(const char *id, AioContext **aio_context, Error **errp) { @@ -3320,12 +3323,14 @@ static BlockJob *find_block_job(const char *id, AioContext **aio_context, assert(id != NULL); *aio_context = NULL; + job_lock(); job = block_job_get(id); if (!job) { error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, "Block job '%s' not found", id); + job_unlock(); return NULL; } @@ -3346,6 +3351,7 @@ void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) block_job_set_speed(job, speed, errp); aio_context_release(aio_context); + job_unlock(); } void qmp_block_job_cancel(const char *device, @@ -3372,6 +3378,7 @@ void qmp_block_job_cancel(const char *device, job_user_cancel(&job->job, force, errp); out: aio_context_release(aio_context); + job_unlock(); } void qmp_block_job_pause(const char *device, Error **errp) @@ -3386,6 +3393,7 @@ void qmp_block_job_pause(const char *device, Error **errp) trace_qmp_block_job_pause(job); job_user_pause(&job->job, errp); aio_context_release(aio_context); + job_unlock(); } void qmp_block_job_resume(const char *device, Error **errp) @@ -3400,6 +3408,7 @@ void qmp_block_job_resume(const char *device, Error **errp) trace_qmp_block_job_resume(job); job_user_resume(&job->job, errp); aio_context_release(aio_context); + job_unlock(); } void qmp_block_job_complete(const char *device, Error **errp) @@ -3414,6 +3423,7 @@ void qmp_block_job_complete(const char *device, Error **errp) trace_qmp_block_job_complete(job); job_complete(&job->job, errp); aio_context_release(aio_context); + job_unlock(); } void qmp_block_job_finalize(const char *id, Error **errp) @@ -3437,6 +3447,7 @@ void qmp_block_job_finalize(const char *id, Error **errp) aio_context = block_job_get_aio_context(job); job_unref(&job->job); aio_context_release(aio_context); + job_unlock(); } void qmp_block_job_dismiss(const char *id, Error **errp) @@ -3453,6 +3464,7 @@ void qmp_block_job_dismiss(const char *id, Error **errp) job = &bjob->job; job_dismiss(&job, errp); aio_context_release(aio_context); + job_unlock(); } void qmp_change_backing_file(const char *device, diff --git a/job-qmp.c b/job-qmp.c index 06f970f6cf..a08cd7dd36 100644 --- a/job-qmp.c +++ b/job-qmp.c @@ -29,16 +29,21 @@ #include "qapi/error.h" #include "trace/trace-root.h" -/* Get a job using its ID and acquire its AioContext */ +/* + * Get a block job using its ID and acquire its AioContext. + * Returns with job_lock held on success. + */ static Job *find_job(const char *id, AioContext **aio_context, Error **errp) { Job *job; *aio_context = NULL; + job_lock(); job = job_get(id); if (!job) { error_setg(errp, "Job not found"); + job_unlock(); return NULL; } @@ -60,6 +65,7 @@ void qmp_job_cancel(const char *id, Error **errp) trace_qmp_job_cancel(job); job_user_cancel(job, true, errp); aio_context_release(aio_context); + job_unlock(); } void qmp_job_pause(const char *id, Error **errp) @@ -74,6 +80,7 @@ void qmp_job_pause(const char *id, Error **errp) trace_qmp_job_pause(job); job_user_pause(job, errp); aio_context_release(aio_context); + job_unlock(); } void qmp_job_resume(const char *id, Error **errp) @@ -88,6 +95,7 @@ void qmp_job_resume(const char *id, Error **errp) trace_qmp_job_resume(job); job_user_resume(job, errp); aio_context_release(aio_context); + job_unlock(); } void qmp_job_complete(const char *id, Error **errp) @@ -102,6 +110,7 @@ void qmp_job_complete(const char *id, Error **errp) trace_qmp_job_complete(job); job_complete(job, errp); aio_context_release(aio_context); + job_unlock(); } void qmp_job_finalize(const char *id, Error **errp) @@ -125,6 +134,7 @@ void qmp_job_finalize(const char *id, Error **errp) aio_context = job->aio_context; job_unref(job); aio_context_release(aio_context); + job_unlock(); } void qmp_job_dismiss(const char *id, Error **errp) @@ -139,6 +149,7 @@ void qmp_job_dismiss(const char *id, Error **errp) trace_qmp_job_dismiss(job); job_dismiss(&job, errp); aio_context_release(aio_context); + job_unlock(); } static JobInfo *job_query_single(Job *job, Error **errp) From patchwork Fri Jan 28 11:50:21 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: 1585661 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=hNW9mJ6L; 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 4Jlc870y83z9t56 for ; Fri, 28 Jan 2022 23:24:07 +1100 (AEDT) Received: from localhost ([::1]:46288 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDQIO-00049B-Tw for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:24:04 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60970) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmg-0004oT-4N for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:19 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:52990) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmQ-0003Q9-3Y for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370649; 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=1gs9lmUZp0dbBkOUoMHttxO4egPGY9bZ3dvP/cPAblo=; b=hNW9mJ6LkMY2dMr1dl548oGBkRLkFe5Vx0XEVLckmEcAYu3eoUUmkf7oNj4SK7OGeURENq cpOm0uzJ8WhjSFZefZRRQf3Bz/yF2JESmwZf/1+ykKfurgq/SRMD8COL8YlFdOpD1foODn VToJ6ixWWinKX2tR6V+ThCJ9qUMmuZA= 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-352-1usWpNRhPFe4eFx3VxGTlA-1; Fri, 28 Jan 2022 06:50:46 -0500 X-MC-Unique: 1usWpNRhPFe4eFx3VxGTlA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 19F131006AA0; Fri, 28 Jan 2022 11:50:45 +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 2263D22DE3; Fri, 28 Jan 2022 11:50:44 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 09/19] jobs: use job locks also in the unit tests Date: Fri, 28 Jan 2022 06:50:21 -0500 Message-Id: <20220128115031.4061565-10-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" Add missing job synchronization in the unit tests, with explicit locks. Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*. Signed-off-by: Emanuele Giuseppe Esposito --- tests/unit/test-bdrv-drain.c | 76 ++++++++++++++++--------- tests/unit/test-block-iothread.c | 8 ++- tests/unit/test-blockjob-txn.c | 32 +++++++---- tests/unit/test-blockjob.c | 96 ++++++++++++++++++++++++-------- 4 files changed, 148 insertions(+), 64 deletions(-) diff --git a/tests/unit/test-bdrv-drain.c b/tests/unit/test-bdrv-drain.c index 36be84ae55..181458eecb 100644 --- a/tests/unit/test-bdrv-drain.c +++ b/tests/unit/test-bdrv-drain.c @@ -943,61 +943,83 @@ static void test_blockjob_common_drain_node(enum drain_type drain_type, } } - g_assert_cmpint(job->job.pause_count, ==, 0); - g_assert_false(job->job.paused); - g_assert_true(tjob->running); - g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */ + WITH_JOB_LOCK_GUARD() { + g_assert_cmpint(job->job.pause_count, ==, 0); + g_assert_false(job->job.paused); + g_assert_true(tjob->running); + g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */ + } do_drain_begin_unlocked(drain_type, drain_bs); - if (drain_type == BDRV_DRAIN_ALL) { - /* bdrv_drain_all() drains both src and target */ - g_assert_cmpint(job->job.pause_count, ==, 2); - } else { - g_assert_cmpint(job->job.pause_count, ==, 1); + WITH_JOB_LOCK_GUARD() { + if (drain_type == BDRV_DRAIN_ALL) { + /* bdrv_drain_all() drains both src and target */ + g_assert_cmpint(job->job.pause_count, ==, 2); + } else { + g_assert_cmpint(job->job.pause_count, ==, 1); + } + g_assert_true(job->job.paused); + g_assert_false(job->job.busy); /* The job is paused */ } - g_assert_true(job->job.paused); - g_assert_false(job->job.busy); /* The job is paused */ do_drain_end_unlocked(drain_type, drain_bs); if (use_iothread) { - /* paused is reset in the I/O thread, wait for it */ + /* + * Here we are waiting for the paused status to change, + * so don't bother protecting the read every time. + * + * paused is reset in the I/O thread, wait for it + */ while (job->job.paused) { aio_poll(qemu_get_aio_context(), false); } } - g_assert_cmpint(job->job.pause_count, ==, 0); - g_assert_false(job->job.paused); - g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */ + WITH_JOB_LOCK_GUARD() { + g_assert_cmpint(job->job.pause_count, ==, 0); + g_assert_false(job->job.paused); + g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */ + } do_drain_begin_unlocked(drain_type, target); - if (drain_type == BDRV_DRAIN_ALL) { - /* bdrv_drain_all() drains both src and target */ - g_assert_cmpint(job->job.pause_count, ==, 2); - } else { - g_assert_cmpint(job->job.pause_count, ==, 1); + WITH_JOB_LOCK_GUARD() { + if (drain_type == BDRV_DRAIN_ALL) { + /* bdrv_drain_all() drains both src and target */ + g_assert_cmpint(job->job.pause_count, ==, 2); + } else { + g_assert_cmpint(job->job.pause_count, ==, 1); + } + g_assert_true(job->job.paused); + g_assert_false(job->job.busy); /* The job is paused */ } - g_assert_true(job->job.paused); - g_assert_false(job->job.busy); /* The job is paused */ do_drain_end_unlocked(drain_type, target); if (use_iothread) { - /* paused is reset in the I/O thread, wait for it */ + /* + * Here we are waiting for the paused status to change, + * so don't bother protecting the read every time. + * + * paused is reset in the I/O thread, wait for it + */ while (job->job.paused) { aio_poll(qemu_get_aio_context(), false); } } - g_assert_cmpint(job->job.pause_count, ==, 0); - g_assert_false(job->job.paused); - g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */ + WITH_JOB_LOCK_GUARD() { + g_assert_cmpint(job->job.pause_count, ==, 0); + g_assert_false(job->job.paused); + g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */ + } aio_context_acquire(ctx); - ret = job_complete_sync(&job->job, &error_abort); + WITH_JOB_LOCK_GUARD() { + ret = job_complete_sync(&job->job, &error_abort); + } g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO)); if (use_iothread) { diff --git a/tests/unit/test-block-iothread.c b/tests/unit/test-block-iothread.c index 94718c9319..9866262f79 100644 --- a/tests/unit/test-block-iothread.c +++ b/tests/unit/test-block-iothread.c @@ -456,7 +456,9 @@ static void test_attach_blockjob(void) } aio_context_acquire(ctx); - job_complete_sync(&tjob->common.job, &error_abort); + WITH_JOB_LOCK_GUARD() { + job_complete_sync(&tjob->common.job, &error_abort); + } blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort); aio_context_release(ctx); @@ -630,7 +632,9 @@ static void test_propagate_mirror(void) BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT, false, "filter_node", MIRROR_COPY_MODE_BACKGROUND, &error_abort); - job = job_get("job0"); + WITH_JOB_LOCK_GUARD() { + job = job_get("job0"); + } filter = bdrv_find_node("filter_node"); /* Change the AioContext of src */ diff --git a/tests/unit/test-blockjob-txn.c b/tests/unit/test-blockjob-txn.c index c69028b450..0355e54001 100644 --- a/tests/unit/test-blockjob-txn.c +++ b/tests/unit/test-blockjob-txn.c @@ -116,8 +116,10 @@ static void test_single_job(int expected) job = test_block_job_start(1, true, expected, &result, txn); job_start(&job->job); - if (expected == -ECANCELED) { - job_cancel(&job->job, false); + WITH_JOB_LOCK_GUARD() { + if (expected == -ECANCELED) { + job_cancel(&job->job, false); + } } while (result == -EINPROGRESS) { @@ -125,7 +127,9 @@ static void test_single_job(int expected) } g_assert_cmpint(result, ==, expected); - job_txn_unref(txn); + WITH_JOB_LOCK_GUARD() { + job_txn_unref(txn); + } } static void test_single_job_success(void) @@ -160,13 +164,15 @@ static void test_pair_jobs(int expected1, int expected2) /* Release our reference now to trigger as many nice * use-after-free bugs as possible. */ - job_txn_unref(txn); + WITH_JOB_LOCK_GUARD() { + job_txn_unref(txn); - if (expected1 == -ECANCELED) { - job_cancel(&job1->job, false); - } - if (expected2 == -ECANCELED) { - job_cancel(&job2->job, false); + if (expected1 == -ECANCELED) { + job_cancel(&job1->job, false); + } + if (expected2 == -ECANCELED) { + job_cancel(&job2->job, false); + } } while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) { @@ -219,7 +225,9 @@ static void test_pair_jobs_fail_cancel_race(void) job_start(&job1->job); job_start(&job2->job); - job_cancel(&job1->job, false); + WITH_JOB_LOCK_GUARD() { + job_cancel(&job1->job, false); + } /* Now make job2 finish before the main loop kicks jobs. This simulates * the race between a pending kick and another job completing. @@ -234,7 +242,9 @@ static void test_pair_jobs_fail_cancel_race(void) g_assert_cmpint(result1, ==, -ECANCELED); g_assert_cmpint(result2, ==, -ECANCELED); - job_txn_unref(txn); + WITH_JOB_LOCK_GUARD() { + job_txn_unref(txn); + } } int main(int argc, char **argv) diff --git a/tests/unit/test-blockjob.c b/tests/unit/test-blockjob.c index 4c9e1bf1e5..ab7958dad5 100644 --- a/tests/unit/test-blockjob.c +++ b/tests/unit/test-blockjob.c @@ -211,8 +211,11 @@ static CancelJob *create_common(Job **pjob) bjob = mk_job(blk, "Steve", &test_cancel_driver, true, JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS); job = &bjob->job; - job_ref(job); - assert(job->status == JOB_STATUS_CREATED); + WITH_JOB_LOCK_GUARD() { + job_ref(job); + assert(job->status == JOB_STATUS_CREATED); + } + s = container_of(bjob, CancelJob, common); s->blk = blk; @@ -230,13 +233,15 @@ static void cancel_common(CancelJob *s) ctx = job->job.aio_context; aio_context_acquire(ctx); - job_cancel_sync(&job->job, true); - if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) { - Job *dummy = &job->job; - job_dismiss(&dummy, &error_abort); + WITH_JOB_LOCK_GUARD() { + job_cancel_sync(&job->job, true); + if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) { + Job *dummy = &job->job; + job_dismiss(&dummy, &error_abort); + } + assert(job->job.status == JOB_STATUS_NULL); + job_unref(&job->job); } - assert(job->job.status == JOB_STATUS_NULL); - job_unref(&job->job); destroy_blk(blk); aio_context_release(ctx); @@ -251,6 +256,10 @@ static void test_cancel_created(void) cancel_common(s); } +/* + * This test always runs in the main loop, so there is no + * need to protect job->status. + */ static void test_cancel_running(void) { Job *job; @@ -264,6 +273,10 @@ static void test_cancel_running(void) cancel_common(s); } +/* + * This test always runs in the main loop, so there is no + * need to protect job->status. + */ static void test_cancel_paused(void) { Job *job; @@ -274,13 +287,19 @@ static void test_cancel_paused(void) job_start(job); assert(job->status == JOB_STATUS_RUNNING); - job_user_pause(job, &error_abort); + WITH_JOB_LOCK_GUARD() { + job_user_pause(job, &error_abort); + } job_enter(job); assert(job->status == JOB_STATUS_PAUSED); cancel_common(s); } +/* + * This test always runs in the main loop, so there is no + * need to protect job->status. + */ static void test_cancel_ready(void) { Job *job; @@ -298,6 +317,10 @@ static void test_cancel_ready(void) cancel_common(s); } +/* + * This test always runs in the main loop, so there is no + * need to protect job->status. + */ static void test_cancel_standby(void) { Job *job; @@ -312,13 +335,19 @@ static void test_cancel_standby(void) job_enter(job); assert(job->status == JOB_STATUS_READY); - job_user_pause(job, &error_abort); + WITH_JOB_LOCK_GUARD() { + job_user_pause(job, &error_abort); + } job_enter(job); assert(job->status == JOB_STATUS_STANDBY); cancel_common(s); } +/* + * This test always runs in the main loop, so there is no + * need to protect job->status. + */ static void test_cancel_pending(void) { Job *job; @@ -333,7 +362,9 @@ static void test_cancel_pending(void) job_enter(job); assert(job->status == JOB_STATUS_READY); - job_complete(job, &error_abort); + WITH_JOB_LOCK_GUARD() { + job_complete(job, &error_abort); + } job_enter(job); while (!job->deferred_to_main_loop) { aio_poll(qemu_get_aio_context(), true); @@ -345,6 +376,10 @@ static void test_cancel_pending(void) cancel_common(s); } +/* + * This test always runs in the main loop, so there is no + * need to protect job->status. + */ static void test_cancel_concluded(void) { Job *job; @@ -359,7 +394,9 @@ static void test_cancel_concluded(void) job_enter(job); assert(job->status == JOB_STATUS_READY); - job_complete(job, &error_abort); + WITH_JOB_LOCK_GUARD() { + job_complete(job, &error_abort); + } job_enter(job); while (!job->deferred_to_main_loop) { aio_poll(qemu_get_aio_context(), true); @@ -369,7 +406,9 @@ static void test_cancel_concluded(void) assert(job->status == JOB_STATUS_PENDING); aio_context_acquire(job->aio_context); - job_finalize(job, &error_abort); + WITH_JOB_LOCK_GUARD() { + job_finalize(job, &error_abort); + } aio_context_release(job->aio_context); assert(job->status == JOB_STATUS_CONCLUDED); @@ -459,36 +498,45 @@ static void test_complete_in_standby(void) bjob = mk_job(blk, "job", &test_yielding_driver, true, JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS); job = &bjob->job; + /* Job did not start, so status is safe to read*/ assert(job->status == JOB_STATUS_CREATED); /* Wait for the job to become READY */ job_start(job); aio_context_acquire(ctx); + /* + * Here we are waiting for the status to change, so don't bother + * protecting the read every time. + */ AIO_WAIT_WHILE(ctx, job->status != JOB_STATUS_READY); aio_context_release(ctx); /* Begin the drained section, pausing the job */ bdrv_drain_all_begin(); - assert(job->status == JOB_STATUS_STANDBY); + WITH_JOB_LOCK_GUARD() { + assert(job->status == JOB_STATUS_STANDBY); + } /* Lock the IO thread to prevent the job from being run */ aio_context_acquire(ctx); /* This will schedule the job to resume it */ bdrv_drain_all_end(); - /* But the job cannot run, so it will remain on standby */ - assert(job->status == JOB_STATUS_STANDBY); + WITH_JOB_LOCK_GUARD() { + /* But the job cannot run, so it will remain on standby */ + assert(job->status == JOB_STATUS_STANDBY); - /* Even though the job is on standby, this should work */ - job_complete(job, &error_abort); + /* Even though the job is on standby, this should work */ + job_complete(job, &error_abort); - /* The test is done now, clean up. */ - job_finish_sync(job, NULL, &error_abort); - assert(job->status == JOB_STATUS_PENDING); + /* The test is done now, clean up. */ + job_finish_sync(job, NULL, &error_abort); + assert(job->status == JOB_STATUS_PENDING); - job_finalize(job, &error_abort); - assert(job->status == JOB_STATUS_CONCLUDED); + job_finalize(job, &error_abort); + assert(job->status == JOB_STATUS_CONCLUDED); - job_dismiss(&job, &error_abort); + job_dismiss(&job, &error_abort); + } destroy_blk(blk); aio_context_release(ctx); From patchwork Fri Jan 28 11:50:22 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: 1585655 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=WVmKRnPG; 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 4Jlbzm3qy8z9t6g for ; Fri, 28 Jan 2022 23:16:52 +1100 (AEDT) Received: from localhost ([::1]:33060 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDQBN-0002tv-MB for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:16:49 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60958) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmf-0004oN-Ce for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:17 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:46031) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmQ-0003Qu-22 for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370652; 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=BxWbrBscuEborYKlajU59WgGXWtOb1DLaAUD7JEp8TM=; b=WVmKRnPG4/f9CErwfrpXZhfaWxttxEDYQLbs5c0e8eojBunGWxkwn2hz6l6t/niBPG2RjV kN+emfKI5+34oFOwhRCYGN8Bif9BJzI5pKtfb76KibO0ypWeLojWeH93jqGW01sL33Mm1S Y+3qMcdw+3b8hp+n0eB7fEpWrAPe1H0= 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-657-fksTIeC6OB-rF5LtI5eK8g-1; Fri, 28 Jan 2022 06:50:47 -0500 X-MC-Unique: fksTIeC6OB-rF5LtI5eK8g-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3BEA11006AA7; Fri, 28 Jan 2022 11:50:46 +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 35BDE1FD88; Fri, 28 Jan 2022 11:50:45 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 10/19] job.h: define locked functions Date: Fri, 28 Jan 2022 06:50:22 -0500 Message-Id: <20220128115031.4061565-11-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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.129.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" These functions assume that the job lock is held by the caller, to avoid TOC/TOU conditions. Therefore, their name must end with _locked. Introduce also additional helpers that define _locked functions (useful when the job_mutex is globally applied). Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*. Signed-off-by: Emanuele Giuseppe Esposito --- block.c | 5 +- block/backup.c | 4 +- block/replication.c | 4 +- blockdev.c | 44 +++++---- blockjob.c | 28 +++--- include/block/blockjob.h | 23 +++-- include/qemu/job.h | 153 +++++++++++++++++++--------- job-qmp.c | 26 ++--- job.c | 164 ++++++++++++++++++------------- monitor/qmp-cmds.c | 3 +- qemu-img.c | 14 +-- tests/unit/test-bdrv-drain.c | 2 +- tests/unit/test-block-iothread.c | 4 +- tests/unit/test-blockjob-txn.c | 14 +-- tests/unit/test-blockjob.c | 30 +++--- 15 files changed, 306 insertions(+), 212 deletions(-) diff --git a/block.c b/block.c index 5e1ab8d692..ab801e776d 100644 --- a/block.c +++ b/block.c @@ -4973,7 +4973,7 @@ static void bdrv_close(BlockDriverState *bs) void bdrv_close_all(void) { WITH_JOB_LOCK_GUARD() { - assert(job_next(NULL) == NULL); + assert(job_next_locked(NULL) == NULL); } assert(qemu_in_main_thread()); @@ -6152,7 +6152,8 @@ XDbgBlockGraph *bdrv_get_xdbg_block_graph(Error **errp) } WITH_JOB_LOCK_GUARD() { - for (job = block_job_next(NULL); job; job = block_job_next(job)) { + for (job = block_job_next_locked(NULL); job; + job = block_job_next_locked(job)) { GSList *el; xdbg_graph_add_node(gr, job, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_JOB, diff --git a/block/backup.c b/block/backup.c index c53041772c..c29f3c1ae8 100644 --- a/block/backup.c +++ b/block/backup.c @@ -316,8 +316,8 @@ static void coroutine_fn backup_set_speed(BlockJob *job, int64_t speed) BackupBlockJob *s = container_of(job, BackupBlockJob, common); /* - * block_job_set_speed() is called first from block_job_create(), when we - * don't yet have s->bcs. + * block_job_set_speed_locked() is called first from block_job_create(), + * when we don't yet have s->bcs. */ if (s->bcs) { block_copy_set_speed(s->bcs, speed); diff --git a/block/replication.c b/block/replication.c index a03b28726e..50ea778937 100644 --- a/block/replication.c +++ b/block/replication.c @@ -150,7 +150,7 @@ static void replication_close(BlockDriverState *bs) commit_job = &s->commit_job->job; assert(commit_job->aio_context == qemu_get_current_aio_context()); WITH_JOB_LOCK_GUARD() { - job_cancel_sync(commit_job, false); + job_cancel_sync_locked(commit_job, false); } } @@ -729,7 +729,7 @@ static void replication_stop(ReplicationState *rs, bool failover, Error **errp) */ if (s->backup_job) { WITH_JOB_LOCK_GUARD() { - job_cancel_sync(&s->backup_job->job, true); + job_cancel_sync_locked(&s->backup_job->job, true); } } diff --git a/blockdev.c b/blockdev.c index 7a9e9b4a13..d6693593b0 100644 --- a/blockdev.c +++ b/blockdev.c @@ -152,12 +152,13 @@ void blockdev_mark_auto_del(BlockBackend *blk) JOB_LOCK_GUARD(); - for (job = block_job_next(NULL); job; job = block_job_next(job)) { + for (job = block_job_next_locked(NULL); job; + job = block_job_next_locked(job)) { if (block_job_has_bdrv(job, blk_bs(blk))) { AioContext *aio_context = job->job.aio_context; aio_context_acquire(aio_context); - job_cancel(&job->job, false); + job_cancel_locked(&job->job, false); aio_context_release(aio_context); } @@ -1841,7 +1842,7 @@ static void drive_backup_abort(BlkActionState *common) aio_context_acquire(aio_context); WITH_JOB_LOCK_GUARD() { - job_cancel_sync(&state->job->job, true); + job_cancel_sync_locked(&state->job->job, true); } aio_context_release(aio_context); @@ -1944,7 +1945,7 @@ static void blockdev_backup_abort(BlkActionState *common) aio_context_acquire(aio_context); WITH_JOB_LOCK_GUARD() { - job_cancel_sync(&state->job->job, true); + job_cancel_sync_locked(&state->job->job, true); } aio_context_release(aio_context); @@ -2396,7 +2397,7 @@ exit: } WITH_JOB_LOCK_GUARD() { - job_txn_unref(block_job_txn); + job_txn_unref_locked(block_job_txn); } } @@ -3325,7 +3326,7 @@ static BlockJob *find_block_job(const char *id, AioContext **aio_context, *aio_context = NULL; job_lock(); - job = block_job_get(id); + job = block_job_get_locked(id); if (!job) { error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, @@ -3349,7 +3350,7 @@ void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) return; } - block_job_set_speed(job, speed, errp); + block_job_set_speed_locked(job, speed, errp); aio_context_release(aio_context); job_unlock(); } @@ -3368,14 +3369,14 @@ void qmp_block_job_cancel(const char *device, force = false; } - if (job_user_paused(&job->job) && !force) { + if (job_user_paused_locked(&job->job) && !force) { error_setg(errp, "The block job for device '%s' is currently paused", device); goto out; } trace_qmp_block_job_cancel(job); - job_user_cancel(&job->job, force, errp); + job_user_cancel_locked(&job->job, force, errp); out: aio_context_release(aio_context); job_unlock(); @@ -3391,7 +3392,7 @@ void qmp_block_job_pause(const char *device, Error **errp) } trace_qmp_block_job_pause(job); - job_user_pause(&job->job, errp); + job_user_pause_locked(&job->job, errp); aio_context_release(aio_context); job_unlock(); } @@ -3406,7 +3407,7 @@ void qmp_block_job_resume(const char *device, Error **errp) } trace_qmp_block_job_resume(job); - job_user_resume(&job->job, errp); + job_user_resume_locked(&job->job, errp); aio_context_release(aio_context); job_unlock(); } @@ -3421,7 +3422,7 @@ void qmp_block_job_complete(const char *device, Error **errp) } trace_qmp_block_job_complete(job); - job_complete(&job->job, errp); + job_complete_locked(&job->job, errp); aio_context_release(aio_context); job_unlock(); } @@ -3436,16 +3437,16 @@ void qmp_block_job_finalize(const char *id, Error **errp) } trace_qmp_block_job_finalize(job); - job_ref(&job->job); - job_finalize(&job->job, errp); + job_ref_locked(&job->job); + job_finalize_locked(&job->job, errp); /* - * Job's context might have changed via job_finalize (and job_txn_apply - * automatically acquires the new one), so make sure we release the correct - * one. + * Job's context might have changed via job_finalize_locked + * (and job_txn_apply automatically acquires the new one), + * so make sure we release the correct one. */ aio_context = block_job_get_aio_context(job); - job_unref(&job->job); + job_unref_locked(&job->job); aio_context_release(aio_context); job_unlock(); } @@ -3462,7 +3463,7 @@ void qmp_block_job_dismiss(const char *id, Error **errp) trace_qmp_block_job_dismiss(bjob); job = &bjob->job; - job_dismiss(&job, errp); + job_dismiss_locked(&job, errp); aio_context_release(aio_context); job_unlock(); } @@ -3734,13 +3735,14 @@ BlockJobInfoList *qmp_query_block_jobs(Error **errp) JOB_LOCK_GUARD(); - for (job = block_job_next(NULL); job; job = block_job_next(job)) { + for (job = block_job_next_locked(NULL); job; + job = block_job_next_locked(job)) { BlockJobInfo *value; if (block_job_is_internal(job)) { continue; } - value = block_job_query(job, errp); + value = block_job_query_locked(job, errp); if (!value) { qapi_free_BlockJobInfoList(head); return NULL; diff --git a/blockjob.c b/blockjob.c index 9fd1b6972f..c370bd0b5e 100644 --- a/blockjob.c +++ b/blockjob.c @@ -59,21 +59,21 @@ static bool is_block_job(Job *job) job_type(job) == JOB_TYPE_STREAM; } -BlockJob *block_job_next(BlockJob *bjob) +BlockJob *block_job_next_locked(BlockJob *bjob) { Job *job = bjob ? &bjob->job : NULL; assert(qemu_in_main_thread()); do { - job = job_next(job); + job = job_next_locked(job); } while (job && !is_block_job(job)); return job ? container_of(job, BlockJob, job) : NULL; } -BlockJob *block_job_get(const char *id) +BlockJob *block_job_get_locked(const char *id) { - Job *job = job_get(id); + Job *job = job_get_locked(id); assert(qemu_in_main_thread()); if (job && is_block_job(job)) { @@ -103,7 +103,7 @@ static void child_job_drained_begin(BdrvChild *c) { BlockJob *job = c->opaque; WITH_JOB_LOCK_GUARD() { - job_pause(&job->job); + job_pause_locked(&job->job); } } @@ -117,7 +117,7 @@ static bool child_job_drained_poll(BdrvChild *c) * with !job->busy are either already paused or have a pause point after * being reentered, so no job driver code will run before they pause. */ WITH_JOB_LOCK_GUARD() { - if (!job->busy || job_is_completed(job)) { + if (!job->busy || job_is_completed_locked(job)) { return false; } } @@ -135,7 +135,7 @@ static void child_job_drained_end(BdrvChild *c, int *drained_end_counter) { BlockJob *job = c->opaque; WITH_JOB_LOCK_GUARD() { - job_resume(&job->job); + job_resume_locked(&job->job); } } @@ -283,14 +283,14 @@ static bool job_timer_pending(Job *job) return timer_pending(&job->sleep_timer); } -bool block_job_set_speed(BlockJob *job, int64_t speed, Error **errp) +bool block_job_set_speed_locked(BlockJob *job, int64_t speed, Error **errp) { const BlockJobDriver *drv = block_job_driver(job); int64_t old_speed = job->speed; assert(qemu_in_main_thread()); - if (job_apply_verb(&job->job, JOB_VERB_SET_SPEED, errp) < 0) { + if (job_apply_verb_locked(&job->job, JOB_VERB_SET_SPEED, errp) < 0) { return false; } if (speed < 0) { @@ -314,7 +314,7 @@ bool block_job_set_speed(BlockJob *job, int64_t speed, Error **errp) } /* kick only if a timer is pending */ - job_enter_cond(&job->job, job_timer_pending); + job_enter_cond_locked(&job->job, job_timer_pending); return true; } @@ -324,7 +324,7 @@ int64_t block_job_ratelimit_get_delay(BlockJob *job, uint64_t n) return ratelimit_calculate_delay(&job->limit, n); } -BlockJobInfo *block_job_query(BlockJob *job, Error **errp) +BlockJobInfo *block_job_query_locked(BlockJob *job, Error **errp) { BlockJobInfo *info; uint64_t progress_current, progress_total; @@ -348,7 +348,7 @@ BlockJobInfo *block_job_query(BlockJob *job, Error **errp) info->len = progress_total; info->speed = job->speed; info->io_status = job->iostatus; - info->ready = job_is_ready(&job->job), + info->ready = job_is_ready_locked(&job->job), info->status = job->job.status; info->auto_finalize = job->job.auto_finalize; info->auto_dismiss = job->job.auto_dismiss; @@ -504,7 +504,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver, bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker); WITH_JOB_LOCK_GUARD() { - ret = block_job_set_speed(job, speed, errp); + ret = block_job_set_speed_locked(job, speed, errp); } if (!ret) { goto fail; @@ -568,7 +568,7 @@ BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err, if (action == BLOCK_ERROR_ACTION_STOP) { WITH_JOB_LOCK_GUARD() { if (!job->job.user_paused) { - job_pause(&job->job); + job_pause_locked(&job->job); /* * make the pause user visible, which will be * resumed from QMP. diff --git a/include/block/blockjob.h b/include/block/blockjob.h index 2373dfeb07..cabfa52d5b 100644 --- a/include/block/blockjob.h +++ b/include/block/blockjob.h @@ -46,7 +46,7 @@ typedef struct BlockJob { /** Status that is published by the query-block-jobs QMP API */ BlockDeviceIoStatus iostatus; - /** Speed that was set with @block_job_set_speed. */ + /** Speed that was set with @block_job_set_speed_locked. */ int64_t speed; /** Rate limiting data structure for implementing @speed. */ @@ -82,25 +82,27 @@ typedef struct BlockJob { */ /** - * block_job_next: + * block_job_next_locked: * @job: A block job, or %NULL. * * Get the next element from the list of block jobs after @job, or the * first one if @job is %NULL. * * Returns the requested job, or %NULL if there are no more jobs left. + * Called with job_mutex lock held. */ -BlockJob *block_job_next(BlockJob *job); +BlockJob *block_job_next_locked(BlockJob *job); /** - * block_job_get: + * block_job_get_locked: * @id: The id of the block job. * * Get the block job identified by @id (which must not be %NULL). * * Returns the requested job, or %NULL if it doesn't exist. + * Called with job_mutex lock held. */ -BlockJob *block_job_get(const char *id); +BlockJob *block_job_get_locked(const char *id); /** * block_job_add_bdrv: @@ -135,23 +137,26 @@ void block_job_remove_all_bdrv(BlockJob *job); bool block_job_has_bdrv(BlockJob *job, BlockDriverState *bs); /** - * block_job_set_speed: + * block_job_set_speed_locked: * @job: The job to set the speed for. * @speed: The new value * @errp: Error object. * * Set a rate-limiting parameter for the job; the actual meaning may * vary depending on the job type. + * + * Called with job_mutex lock held. May temporarly release the lock. */ -bool block_job_set_speed(BlockJob *job, int64_t speed, Error **errp); +bool block_job_set_speed_locked(BlockJob *job, int64_t speed, Error **errp); /** - * block_job_query: + * block_job_query_locked: * @job: The job to get information about. * * Return information about a job. + * Called with job_mutex lock held. */ -BlockJobInfo *block_job_query(BlockJob *job, Error **errp); +BlockJobInfo *block_job_query_locked(BlockJob *job, Error **errp); /** * block_job_iostatus_reset: diff --git a/include/qemu/job.h b/include/qemu/job.h index 6f22a20d58..e13c976ba5 100644 --- a/include/qemu/job.h +++ b/include/qemu/job.h @@ -348,7 +348,7 @@ void job_unlock(void); /** * Allocate and return a new job transaction. Jobs can be added to the - * transaction using job_txn_add_job(). + * transaction using job_txn_add_job_locked(). * * The transaction is automatically freed when the last job completes or is * cancelled. @@ -360,22 +360,25 @@ void job_unlock(void); JobTxn *job_txn_new(void); /** - * Release a reference that was previously acquired with job_txn_add_job or - * job_txn_new. If it's the last reference to the object, it will be freed. + * Release a reference that was previously acquired with + * job_txn_add_job_locked or job_txn_new. + * If it's the last reference to the object, it will be freed. */ -void job_txn_unref(JobTxn *txn); +void job_txn_unref_locked(JobTxn *txn); /** * @txn: The transaction (may be NULL) * @job: Job to add to the transaction * * Add @job to the transaction. The @job must not already be in a transaction. - * The caller must call either job_txn_unref() or job_completed() to release - * the reference that is automatically grabbed here. + * The caller must call either job_txn_unref_locked() or job_completed() + * to release the reference that is automatically grabbed here. * * If @txn is NULL, the function does nothing. + * + * Called between job_lock and job_unlock. */ -void job_txn_add_job(JobTxn *txn, Job *job); +void job_txn_add_job_locked(JobTxn *txn, Job *job); /** * Create a new long-running job and return it. @@ -394,16 +397,20 @@ void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn, void *opaque, Error **errp); /** - * Add a reference to Job refcnt, it will be decreased with job_unref, and then - * be freed if it comes to be the last reference. + * Add a reference to Job refcnt, it will be decreased with job_unref_locked, + * and then be freed if it comes to be the last reference. + * + * Called between job_lock and job_unlock. */ -void job_ref(Job *job); +void job_ref_locked(Job *job); /** - * Release a reference that was previously acquired with job_ref() or + * Release a reference that was previously acquired with job_ref_locked() or * job_create(). If it's the last reference to the object, it will be freed. + * + * Called between job_lock and job_unlock, but might release it temporarly. */ -void job_unref(Job *job); +void job_unref_locked(Job *job); /** * @job: The job that has made progress @@ -442,8 +449,10 @@ void job_progress_increase_remaining(Job *job, uint64_t delta); * Conditionally enter the job coroutine if the job is ready to run, not * already busy and fn() returns true. fn() is called while under the job_lock * critical section. + * + * Called between job_lock and job_unlock, but might release it temporarly. */ -void job_enter_cond(Job *job, bool(*fn)(Job *job)); +void job_enter_cond_locked(Job *job, bool(*fn)(Job *job)); /** * @job: A job that has not yet been started. @@ -463,8 +472,9 @@ void job_enter(Job *job); /** * @job: The job that is ready to pause. * - * Pause now if job_pause() has been called. Jobs that perform lots of I/O - * must call this between requests so that the job can be paused. + * Pause now if job_pause_locked() has been called. + * Jobs that perform lots of I/O must call this between + * requests so that the job can be paused. */ void coroutine_fn job_pause_point(Job *job); @@ -503,79 +513,117 @@ bool job_is_cancelled(Job *job); */ bool job_cancel_requested(Job *job); -/** Returns whether the job is in a completed state. */ -bool job_is_completed(Job *job); +/** + * Returns whether the job is in a completed state. + * Called between job_lock and job_unlock. + */ +bool job_is_completed_locked(Job *job); -/** Returns whether the job is ready to be completed. */ +/** + * Returns whether the job is ready to be completed. + * Called with job_mutex *not* held. + */ bool job_is_ready(Job *job); +/** Same as job_is_ready(), but assumes job_lock is held. */ +bool job_is_ready_locked(Job *job); + /** * Request @job to pause at the next pause point. Must be paired with - * job_resume(). If the job is supposed to be resumed by user action, call - * job_user_pause() instead. + * job_resume_locked(). If the job is supposed to be resumed by user action, + * call job_user_pause_locked() instead. + * + * Called between job_lock and job_unlock. */ -void job_pause(Job *job); +void job_pause_locked(Job *job); -/** Resumes a @job paused with job_pause. */ -void job_resume(Job *job); +/** + * Resumes a @job paused with job_pause_locked. + * Called between job_lock and job_unlock. + */ +void job_resume_locked(Job *job); /** * Asynchronously pause the specified @job. - * Do not allow a resume until a matching call to job_user_resume. + * Do not allow a resume until a matching call to job_user_resume_locked. + * + * Called between job_lock and job_unlock. */ -void job_user_pause(Job *job, Error **errp); +void job_user_pause_locked(Job *job, Error **errp); -/** Returns true if the job is user-paused. */ -bool job_user_paused(Job *job); +/** + * Returns true if the job is user-paused. + * Called between job_lock and job_unlock. + */ +bool job_user_paused_locked(Job *job); /** * Resume the specified @job. - * Must be paired with a preceding job_user_pause. + * Must be paired with a preceding job_user_pause_locked. + * + * Called between job_lock and job_unlock, but might release it temporarly. */ -void job_user_resume(Job *job, Error **errp); +void job_user_resume_locked(Job *job, Error **errp); /** * Get the next element from the list of block jobs after @job, or the * first one if @job is %NULL. * * Returns the requested job, or %NULL if there are no more jobs left. + * + * Called between job_lock and job_unlock. */ -Job *job_next(Job *job); +Job *job_next_locked(Job *job); /** * Get the job identified by @id (which must not be %NULL). * * Returns the requested job, or %NULL if it doesn't exist. + * + * Called between job_lock and job_unlock. */ -Job *job_get(const char *id); +Job *job_get_locked(const char *id); /** * Check whether the verb @verb can be applied to @job in its current state. * Returns 0 if the verb can be applied; otherwise errp is set and -EPERM * returned. + * + * Called between job_lock and job_unlock. */ -int job_apply_verb(Job *job, JobVerb verb, Error **errp); +int job_apply_verb_locked(Job *job, JobVerb verb, Error **errp); /** The @job could not be started, free it. */ void job_early_fail(Job *job); +/** Same as job_early_fail(), but assumes job_lock is held. */ +void job_early_fail_locked(Job *job); + /** Moves the @job from RUNNING to READY */ void job_transition_to_ready(Job *job); -/** Asynchronously complete the specified @job. */ -void job_complete(Job *job, Error **errp); +/** + * Asynchronously complete the specified @job. + * Called between job_lock and job_unlock, but it releases the lock temporarly. + */ +void job_complete_locked(Job *job, Error **errp); /** * Asynchronously cancel the specified @job. If @force is true, the job should * be cancelled immediately without waiting for a consistent state. + * + * Called between job_lock and job_unlock. */ -void job_cancel(Job *job, bool force); +void job_cancel_locked(Job *job, bool force); /** - * Cancels the specified job like job_cancel(), but may refuse to do so if the - * operation isn't meaningful in the current state of the job. + * Cancels the specified job like job_cancel_locked(), + * but may refuse to do so if the operation isn't meaningful + * in the current state of the job. + * + * Called between job_lock and job_unlock. */ -void job_user_cancel(Job *job, bool force, Error **errp); +void job_user_cancel_locked(Job *job, bool force, Error **errp); /** * Synchronously cancel the @job. The completion callback is called @@ -588,14 +636,20 @@ void job_user_cancel(Job *job, bool force, Error **errp); * * Callers must hold the AioContext lock of job->aio_context. */ -int job_cancel_sync(Job *job, bool force); +int job_cancel_sync_locked(Job *job, bool force); -/** Synchronously force-cancels all jobs using job_cancel_sync(). */ +/** + * Synchronously force-cancels all jobs using job_cancel_sync_locked(). + * + * Called with job_lock *not* held, unlike most other APIs consumed + * by the monitor! This is primarly to avoid adding unnecessary lock-unlock + * patterns in the caller. + */ void job_cancel_sync_all(void); /** * @job: The job to be completed. - * @errp: Error object which may be set by job_complete(); this is not + * @errp: Error object which may be set by job_complete_locked(); this is not * necessarily set on every error, the job return value has to be * checked as well. * @@ -606,8 +660,10 @@ void job_cancel_sync_all(void); * Returns the return value from the job. * * Callers must hold the AioContext lock of job->aio_context. + * + * Called between job_lock and job_unlock. */ -int job_complete_sync(Job *job, Error **errp); +int job_complete_sync_locked(Job *job, Error **errp); /** * For a @job that has finished its work and is pending awaiting explicit @@ -616,14 +672,18 @@ int job_complete_sync(Job *job, Error **errp); * FIXME: Make the below statement universally true: * For jobs that support the manual workflow mode, all graph changes that occur * as a result will occur after this command and before a successful reply. + * + * Called between job_lock and job_unlock. */ -void job_finalize(Job *job, Error **errp); +void job_finalize_locked(Job *job, Error **errp); /** * Remove the concluded @job from the query list and resets the passed pointer * to %NULL. Returns an error if the job is not actually concluded. + * + * Called between job_lock and job_unlock. */ -void job_dismiss(Job **job, Error **errp); +void job_dismiss_locked(Job **job, Error **errp); /** * Synchronously finishes the given @job. If @finish is given, it is called to @@ -633,7 +693,10 @@ void job_dismiss(Job **job, Error **errp); * cancelled before completing, and -errno in other error cases. * * Callers must hold the AioContext lock of job->aio_context. + * + * Called between job_lock and job_unlock. */ -int job_finish_sync(Job *job, void (*finish)(Job *, Error **errp), Error **errp); +int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp), + Error **errp); #endif diff --git a/job-qmp.c b/job-qmp.c index a08cd7dd36..615e056fc4 100644 --- a/job-qmp.c +++ b/job-qmp.c @@ -40,7 +40,7 @@ static Job *find_job(const char *id, AioContext **aio_context, Error **errp) *aio_context = NULL; job_lock(); - job = job_get(id); + job = job_get_locked(id); if (!job) { error_setg(errp, "Job not found"); job_unlock(); @@ -63,7 +63,7 @@ void qmp_job_cancel(const char *id, Error **errp) } trace_qmp_job_cancel(job); - job_user_cancel(job, true, errp); + job_user_cancel_locked(job, true, errp); aio_context_release(aio_context); job_unlock(); } @@ -78,7 +78,7 @@ void qmp_job_pause(const char *id, Error **errp) } trace_qmp_job_pause(job); - job_user_pause(job, errp); + job_user_pause_locked(job, errp); aio_context_release(aio_context); job_unlock(); } @@ -93,7 +93,7 @@ void qmp_job_resume(const char *id, Error **errp) } trace_qmp_job_resume(job); - job_user_resume(job, errp); + job_user_resume_locked(job, errp); aio_context_release(aio_context); job_unlock(); } @@ -108,7 +108,7 @@ void qmp_job_complete(const char *id, Error **errp) } trace_qmp_job_complete(job); - job_complete(job, errp); + job_complete_locked(job, errp); aio_context_release(aio_context); job_unlock(); } @@ -123,16 +123,16 @@ void qmp_job_finalize(const char *id, Error **errp) } trace_qmp_job_finalize(job); - job_ref(job); - job_finalize(job, errp); + job_ref_locked(job); + job_finalize_locked(job, errp); /* - * Job's context might have changed via job_finalize (and job_txn_apply - * automatically acquires the new one), so make sure we release the correct - * one. + * Job's context might have changed via job_finalize_locked + * (and job_txn_apply automatically acquires the new one), + * so make sure we release the correct one. */ aio_context = job->aio_context; - job_unref(job); + job_unref_locked(job); aio_context_release(aio_context); job_unlock(); } @@ -147,7 +147,7 @@ void qmp_job_dismiss(const char *id, Error **errp) } trace_qmp_job_dismiss(job); - job_dismiss(&job, errp); + job_dismiss_locked(&job, errp); aio_context_release(aio_context); job_unlock(); } @@ -184,7 +184,7 @@ JobInfoList *qmp_query_jobs(Error **errp) JOB_LOCK_GUARD(); - for (job = job_next(NULL); job; job = job_next(job)) { + for (job = job_next_locked(NULL); job; job = job_next_locked(job)) { JobInfo *value; if (job_is_internal(job)) { diff --git a/job.c b/job.c index 4947a58dcb..b169bd7280 100644 --- a/job.c +++ b/job.c @@ -118,14 +118,14 @@ static void job_txn_ref(JobTxn *txn) txn->refcnt++; } -void job_txn_unref(JobTxn *txn) +void job_txn_unref_locked(JobTxn *txn) { if (txn && --txn->refcnt == 0) { g_free(txn); } } -void job_txn_add_job(JobTxn *txn, Job *job) +void job_txn_add_job_locked(JobTxn *txn, Job *job) { if (!txn) { return; @@ -142,7 +142,7 @@ static void job_txn_del_job(Job *job) { if (job->txn) { QLIST_REMOVE(job, txn_list); - job_txn_unref(job->txn); + job_txn_unref_locked(job->txn); job->txn = NULL; } } @@ -159,7 +159,7 @@ static int job_txn_apply(Job *job, int fn(Job *)) * we need to release it here to avoid holding the lock twice - which would * break AIO_WAIT_WHILE from within fn. */ - job_ref(job); + job_ref_locked(job); aio_context_release(job->aio_context); QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) { @@ -174,7 +174,7 @@ static int job_txn_apply(Job *job, int fn(Job *)) * can't use a local variable to cache it. */ aio_context_acquire(job->aio_context); - job_unref(job); + job_unref_locked(job); return rc; } @@ -198,7 +198,7 @@ static void job_state_transition(Job *job, JobStatus s1) } } -int job_apply_verb(Job *job, JobVerb verb, Error **errp) +int job_apply_verb_locked(Job *job, JobVerb verb, Error **errp) { JobStatus s0 = job->status; assert(verb >= 0 && verb < JOB_VERB__MAX); @@ -234,7 +234,7 @@ bool job_cancel_requested(Job *job) return job->cancelled; } -bool job_is_ready(Job *job) +bool job_is_ready_locked(Job *job) { switch (job->status) { case JOB_STATUS_UNDEFINED: @@ -256,7 +256,13 @@ bool job_is_ready(Job *job) return false; } -bool job_is_completed(Job *job) +bool job_is_ready(Job *job) +{ + JOB_LOCK_GUARD(); + return job_is_ready_locked(job); +} + +bool job_is_completed_locked(Job *job) { switch (job->status) { case JOB_STATUS_UNDEFINED: @@ -278,6 +284,13 @@ bool job_is_completed(Job *job) return false; } +/* Called with job_mutex lock *not* held */ +static bool job_is_completed(Job *job) +{ + JOB_LOCK_GUARD(); + return job_is_completed_locked(job); +} + static bool job_started(Job *job) { return job->co; @@ -288,7 +301,7 @@ static bool job_should_pause(Job *job) return job->pause_count > 0; } -Job *job_next(Job *job) +Job *job_next_locked(Job *job) { if (!job) { return QLIST_FIRST(&jobs); @@ -296,7 +309,7 @@ Job *job_next(Job *job) return QLIST_NEXT(job, job_list); } -Job *job_get(const char *id) +Job *job_get_locked(const char *id) { Job *job; @@ -333,7 +346,7 @@ void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn, error_setg(errp, "Invalid job ID '%s'", job_id); return NULL; } - if (job_get(job_id)) { + if (job_get_locked(job_id)) { error_setg(errp, "Job ID '%s' already in use", job_id); return NULL; } @@ -374,21 +387,21 @@ void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn, * consolidating the job management logic */ if (!txn) { txn = job_txn_new(); - job_txn_add_job(txn, job); - job_txn_unref(txn); + job_txn_add_job_locked(txn, job); + job_txn_unref_locked(txn); } else { - job_txn_add_job(txn, job); + job_txn_add_job_locked(txn, job); } return job; } -void job_ref(Job *job) +void job_ref_locked(Job *job) { ++job->refcnt; } -void job_unref(Job *job) +void job_unref_locked(Job *job) { assert(qemu_in_main_thread()); @@ -460,7 +473,7 @@ static void job_event_idle(Job *job) notifier_list_notify(&job->on_idle, job); } -void job_enter_cond(Job *job, bool(*fn)(Job *job)) +void job_enter_cond_locked(Job *job, bool(*fn)(Job *job)) { if (!job_started(job)) { return; @@ -490,7 +503,7 @@ void job_enter_cond(Job *job, bool(*fn)(Job *job)) void job_enter(Job *job) { JOB_LOCK_GUARD(); - job_enter_cond(job, NULL); + job_enter_cond_locked(job, NULL); } /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds. @@ -512,7 +525,7 @@ static void coroutine_fn job_do_yield(Job *job, uint64_t ns) qemu_coroutine_yield(); job_lock(); - /* Set by job_enter_cond() before re-entering the coroutine. */ + /* Set by job_enter_cond_locked() before re-entering the coroutine. */ assert(job->busy); } @@ -595,15 +608,15 @@ static bool job_timer_not_pending(Job *job) return !timer_pending(&job->sleep_timer); } -void job_pause(Job *job) +void job_pause_locked(Job *job) { job->pause_count++; if (!job->paused) { - job_enter_cond(job, NULL); + job_enter_cond_locked(job, NULL); } } -void job_resume(Job *job) +void job_resume_locked(Job *job) { assert(job->pause_count > 0); job->pause_count--; @@ -612,12 +625,12 @@ void job_resume(Job *job) } /* kick only if no timer is pending */ - job_enter_cond(job, job_timer_not_pending); + job_enter_cond_locked(job, job_timer_not_pending); } -void job_user_pause(Job *job, Error **errp) +void job_user_pause_locked(Job *job, Error **errp) { - if (job_apply_verb(job, JOB_VERB_PAUSE, errp)) { + if (job_apply_verb_locked(job, JOB_VERB_PAUSE, errp)) { return; } if (job->user_paused) { @@ -625,15 +638,15 @@ void job_user_pause(Job *job, Error **errp) return; } job->user_paused = true; - job_pause(job); + job_pause_locked(job); } -bool job_user_paused(Job *job) +bool job_user_paused_locked(Job *job) { return job->user_paused; } -void job_user_resume(Job *job, Error **errp) +void job_user_resume_locked(Job *job, Error **errp) { assert(job); assert(qemu_in_main_thread()); @@ -641,7 +654,7 @@ void job_user_resume(Job *job, Error **errp) error_setg(errp, "Can't resume a job that was not paused"); return; } - if (job_apply_verb(job, JOB_VERB_RESUME, errp)) { + if (job_apply_verb_locked(job, JOB_VERB_RESUME, errp)) { return; } if (job->driver->user_resume) { @@ -650,7 +663,7 @@ void job_user_resume(Job *job, Error **errp) job_lock(); } job->user_paused = false; - job_resume(job); + job_resume_locked(job); } static void job_do_dismiss(Job *job) @@ -663,15 +676,15 @@ static void job_do_dismiss(Job *job) job_txn_del_job(job); job_state_transition(job, JOB_STATUS_NULL); - job_unref(job); + job_unref_locked(job); } -void job_dismiss(Job **jobptr, Error **errp) +void job_dismiss_locked(Job **jobptr, Error **errp) { Job *job = *jobptr; /* similarly to _complete, this is QMP-interface only. */ assert(job->id); - if (job_apply_verb(job, JOB_VERB_DISMISS, errp)) { + if (job_apply_verb_locked(job, JOB_VERB_DISMISS, errp)) { return; } @@ -679,12 +692,18 @@ void job_dismiss(Job **jobptr, Error **errp) *jobptr = NULL; } -void job_early_fail(Job *job) +void job_early_fail_locked(Job *job) { assert(job->status == JOB_STATUS_CREATED); job_do_dismiss(job); } +void job_early_fail(Job *job) +{ + JOB_LOCK_GUARD(); + job_early_fail_locked(job); +} + static void job_conclude(Job *job) { job_state_transition(job, JOB_STATUS_CONCLUDED); @@ -743,7 +762,7 @@ static int job_finalize_single(Job *job) int job_ret; AioContext *ctx = job->aio_context; - assert(job_is_completed(job)); + assert(job_is_completed_locked(job)); /* Ensure abort is called for late-transactional failures */ job_update_rc(job); @@ -839,7 +858,7 @@ static void job_completed_txn_abort(Job *job) * calls of AIO_WAIT_WHILE(), which could deadlock otherwise. * Note that the job's AioContext may change when it is finalized. */ - job_ref(job); + job_ref_locked(job); aio_context_release(job->aio_context); /* Other jobs are effectively cancelled by us, set the status for @@ -866,22 +885,22 @@ static void job_completed_txn_abort(Job *job) */ ctx = other_job->aio_context; aio_context_acquire(ctx); - if (!job_is_completed(other_job)) { + if (!job_is_completed_locked(other_job)) { assert(job_cancel_requested(other_job)); - job_finish_sync(other_job, NULL, NULL); + job_finish_sync_locked(other_job, NULL, NULL); } aio_context_release(ctx); job_finalize_single(other_job); } /* - * Use job_ref()/job_unref() so we can read the AioContext here - * even if the job went away during job_finalize_single(). + * Use job_ref_locked()/job_unref_locked() so we can read the AioContext + * here even if the job went away during job_finalize_single(). */ aio_context_acquire(job->aio_context); - job_unref(job); + job_unref_locked(job); - job_txn_unref(txn); + job_txn_unref_locked(txn); } static int job_prepare(Job *job) @@ -922,10 +941,10 @@ static void job_do_finalize(Job *job) } } -void job_finalize(Job *job, Error **errp) +void job_finalize_locked(Job *job, Error **errp) { assert(job && job->id); - if (job_apply_verb(job, JOB_VERB_FINALIZE, errp)) { + if (job_apply_verb_locked(job, JOB_VERB_FINALIZE, errp)) { return; } job_do_finalize(job); @@ -959,7 +978,7 @@ static void job_completed_txn_success(Job *job) * txn. */ QLIST_FOREACH(other_job, &txn->jobs, txn_list) { - if (!job_is_completed(other_job)) { + if (!job_is_completed_locked(other_job)) { return; } assert(other_job->ret == 0); @@ -975,7 +994,7 @@ static void job_completed_txn_success(Job *job) static void job_completed(Job *job) { - assert(job && job->txn && !job_is_completed(job)); + assert(job && job->txn && !job_is_completed_locked(job)); job_update_rc(job); trace_job_completed(job, job->ret); @@ -993,7 +1012,7 @@ static void job_exit(void *opaque) AioContext *ctx; JOB_LOCK_GUARD(); - job_ref(job); + job_ref_locked(job); aio_context_acquire(job->aio_context); /* This is a lie, we're not quiescent, but still doing the completion @@ -1012,7 +1031,7 @@ static void job_exit(void *opaque) * the job underneath us. */ ctx = job->aio_context; - job_unref(job); + job_unref_locked(job); aio_context_release(ctx); } @@ -1065,7 +1084,7 @@ void job_start(Job *job) aio_co_enter(job->aio_context, job->co); } -void job_cancel(Job *job, bool force) +void job_cancel_locked(Job *job, bool force) { if (job->status == JOB_STATUS_CONCLUDED) { job_do_dismiss(job); @@ -1089,24 +1108,26 @@ void job_cancel(Job *job, bool force) job_completed_txn_abort(job); } } else { - job_enter_cond(job, NULL); + job_enter_cond_locked(job, NULL); } } -void job_user_cancel(Job *job, bool force, Error **errp) +void job_user_cancel_locked(Job *job, bool force, Error **errp) { - if (job_apply_verb(job, JOB_VERB_CANCEL, errp)) { + if (job_apply_verb_locked(job, JOB_VERB_CANCEL, errp)) { return; } - job_cancel(job, force); + job_cancel_locked(job, force); } -/* A wrapper around job_cancel() taking an Error ** parameter so it may be - * used with job_finish_sync() without the need for (rather nasty) function - * pointer casts there. */ +/* + * A wrapper around job_cancel_locked() taking an Error ** parameter so + * it may be used with job_finish_sync_locked() without the + * need for (rather nasty) function pointer casts there. + */ static void job_cancel_err(Job *job, Error **errp) { - job_cancel(job, false); + job_cancel_locked(job, false); } /** @@ -1114,15 +1135,15 @@ static void job_cancel_err(Job *job, Error **errp) */ static void job_force_cancel_err(Job *job, Error **errp) { - job_cancel(job, true); + job_cancel_locked(job, true); } -int job_cancel_sync(Job *job, bool force) +int job_cancel_sync_locked(Job *job, bool force) { if (force) { - return job_finish_sync(job, &job_force_cancel_err, NULL); + return job_finish_sync_locked(job, &job_force_cancel_err, NULL); } else { - return job_finish_sync(job, &job_cancel_err, NULL); + return job_finish_sync_locked(job, &job_cancel_err, NULL); } } @@ -1132,25 +1153,25 @@ void job_cancel_sync_all(void) AioContext *aio_context; JOB_LOCK_GUARD(); - while ((job = job_next(NULL))) { + while ((job = job_next_locked(NULL))) { aio_context = job->aio_context; aio_context_acquire(aio_context); - job_cancel_sync(job, true); + job_cancel_sync_locked(job, true); aio_context_release(aio_context); } } -int job_complete_sync(Job *job, Error **errp) +int job_complete_sync_locked(Job *job, Error **errp) { - return job_finish_sync(job, job_complete, errp); + return job_finish_sync_locked(job, job_complete_locked, errp); } -void job_complete(Job *job, Error **errp) +void job_complete_locked(Job *job, Error **errp) { /* Should not be reachable via external interface for internal jobs */ assert(job->id); assert(qemu_in_main_thread()); - if (job_apply_verb(job, JOB_VERB_COMPLETE, errp)) { + if (job_apply_verb_locked(job, JOB_VERB_COMPLETE, errp)) { return; } if (job_cancel_requested(job) || !job->driver->complete) { @@ -1164,19 +1185,20 @@ void job_complete(Job *job, Error **errp) job_lock(); } -int job_finish_sync(Job *job, void (*finish)(Job *, Error **errp), Error **errp) +int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp), + Error **errp) { Error *local_err = NULL; int ret; - job_ref(job); + job_ref_locked(job); if (finish) { finish(job, &local_err); } if (local_err) { error_propagate(errp, local_err); - job_unref(job); + job_unref_locked(job); return -EBUSY; } @@ -1186,6 +1208,6 @@ int job_finish_sync(Job *job, void (*finish)(Job *, Error **errp), Error **errp) job_lock(); ret = (job_is_cancelled(job) && job->ret == 0) ? -ECANCELED : job->ret; - job_unref(job); + job_unref_locked(job); return ret; } diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c index f935fa4ad5..59f6bdd98c 100644 --- a/monitor/qmp-cmds.c +++ b/monitor/qmp-cmds.c @@ -135,7 +135,8 @@ void qmp_cont(Error **errp) } WITH_JOB_LOCK_GUARD() { - for (job = block_job_next(NULL); job; job = block_job_next(job)) { + for (job = block_job_next_locked(NULL); job; + job = block_job_next_locked(job)) { block_job_iostatus_reset(job); } } diff --git a/qemu-img.c b/qemu-img.c index a805f6fe7a..deb5421137 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -907,7 +907,7 @@ static void run_block_job(BlockJob *job, Error **errp) aio_context_acquire(aio_context); WITH_JOB_LOCK_GUARD() { - job_ref(&job->job); + job_ref_locked(&job->job); do { float progress = 0.0f; job_unlock(); @@ -920,15 +920,15 @@ static void run_block_job(BlockJob *job, Error **errp) } qemu_progress_print(progress, 0); job_lock(); - } while (!job_is_ready(&job->job) && - !job_is_completed(&job->job)); + } while (!job_is_ready_locked(&job->job) && + !job_is_completed_locked(&job->job)); - if (!job_is_completed(&job->job)) { - ret = job_complete_sync(&job->job, errp); + if (!job_is_completed_locked(&job->job)) { + ret = job_complete_sync_locked(&job->job, errp); } else { ret = job->job.ret; } - job_unref(&job->job); + job_unref_locked(&job->job); } aio_context_release(aio_context); @@ -1083,7 +1083,7 @@ static int img_commit(int argc, char **argv) } WITH_JOB_LOCK_GUARD() { - job = block_job_get("commit"); + job = block_job_get_locked("commit"); } assert(job); run_block_job(job, &local_err); diff --git a/tests/unit/test-bdrv-drain.c b/tests/unit/test-bdrv-drain.c index 181458eecb..0db056ea63 100644 --- a/tests/unit/test-bdrv-drain.c +++ b/tests/unit/test-bdrv-drain.c @@ -1018,7 +1018,7 @@ static void test_blockjob_common_drain_node(enum drain_type drain_type, aio_context_acquire(ctx); WITH_JOB_LOCK_GUARD() { - ret = job_complete_sync(&job->job, &error_abort); + ret = job_complete_sync_locked(&job->job, &error_abort); } g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO)); diff --git a/tests/unit/test-block-iothread.c b/tests/unit/test-block-iothread.c index 9866262f79..89e7f0fffb 100644 --- a/tests/unit/test-block-iothread.c +++ b/tests/unit/test-block-iothread.c @@ -457,7 +457,7 @@ static void test_attach_blockjob(void) aio_context_acquire(ctx); WITH_JOB_LOCK_GUARD() { - job_complete_sync(&tjob->common.job, &error_abort); + job_complete_sync_locked(&tjob->common.job, &error_abort); } blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort); aio_context_release(ctx); @@ -633,7 +633,7 @@ static void test_propagate_mirror(void) false, "filter_node", MIRROR_COPY_MODE_BACKGROUND, &error_abort); WITH_JOB_LOCK_GUARD() { - job = job_get("job0"); + job = job_get_locked("job0"); } filter = bdrv_find_node("filter_node"); diff --git a/tests/unit/test-blockjob-txn.c b/tests/unit/test-blockjob-txn.c index 0355e54001..8dc1eaefc8 100644 --- a/tests/unit/test-blockjob-txn.c +++ b/tests/unit/test-blockjob-txn.c @@ -118,7 +118,7 @@ static void test_single_job(int expected) WITH_JOB_LOCK_GUARD() { if (expected == -ECANCELED) { - job_cancel(&job->job, false); + job_cancel_locked(&job->job, false); } } @@ -128,7 +128,7 @@ static void test_single_job(int expected) g_assert_cmpint(result, ==, expected); WITH_JOB_LOCK_GUARD() { - job_txn_unref(txn); + job_txn_unref_locked(txn); } } @@ -165,13 +165,13 @@ static void test_pair_jobs(int expected1, int expected2) * use-after-free bugs as possible. */ WITH_JOB_LOCK_GUARD() { - job_txn_unref(txn); + job_txn_unref_locked(txn); if (expected1 == -ECANCELED) { - job_cancel(&job1->job, false); + job_cancel_locked(&job1->job, false); } if (expected2 == -ECANCELED) { - job_cancel(&job2->job, false); + job_cancel_locked(&job2->job, false); } } @@ -226,7 +226,7 @@ static void test_pair_jobs_fail_cancel_race(void) job_start(&job2->job); WITH_JOB_LOCK_GUARD() { - job_cancel(&job1->job, false); + job_cancel_locked(&job1->job, false); } /* Now make job2 finish before the main loop kicks jobs. This simulates @@ -243,7 +243,7 @@ static void test_pair_jobs_fail_cancel_race(void) g_assert_cmpint(result2, ==, -ECANCELED); WITH_JOB_LOCK_GUARD() { - job_txn_unref(txn); + job_txn_unref_locked(txn); } } diff --git a/tests/unit/test-blockjob.c b/tests/unit/test-blockjob.c index ab7958dad5..8280b1e0c9 100644 --- a/tests/unit/test-blockjob.c +++ b/tests/unit/test-blockjob.c @@ -212,7 +212,7 @@ static CancelJob *create_common(Job **pjob) JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS); job = &bjob->job; WITH_JOB_LOCK_GUARD() { - job_ref(job); + job_ref_locked(job); assert(job->status == JOB_STATUS_CREATED); } @@ -234,13 +234,13 @@ static void cancel_common(CancelJob *s) aio_context_acquire(ctx); WITH_JOB_LOCK_GUARD() { - job_cancel_sync(&job->job, true); + job_cancel_sync_locked(&job->job, true); if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) { Job *dummy = &job->job; - job_dismiss(&dummy, &error_abort); + job_dismiss_locked(&dummy, &error_abort); } assert(job->job.status == JOB_STATUS_NULL); - job_unref(&job->job); + job_unref_locked(&job->job); } destroy_blk(blk); @@ -288,7 +288,7 @@ static void test_cancel_paused(void) assert(job->status == JOB_STATUS_RUNNING); WITH_JOB_LOCK_GUARD() { - job_user_pause(job, &error_abort); + job_user_pause_locked(job, &error_abort); } job_enter(job); assert(job->status == JOB_STATUS_PAUSED); @@ -336,7 +336,7 @@ static void test_cancel_standby(void) assert(job->status == JOB_STATUS_READY); WITH_JOB_LOCK_GUARD() { - job_user_pause(job, &error_abort); + job_user_pause_locked(job, &error_abort); } job_enter(job); assert(job->status == JOB_STATUS_STANDBY); @@ -363,7 +363,7 @@ static void test_cancel_pending(void) assert(job->status == JOB_STATUS_READY); WITH_JOB_LOCK_GUARD() { - job_complete(job, &error_abort); + job_complete_locked(job, &error_abort); } job_enter(job); while (!job->deferred_to_main_loop) { @@ -395,7 +395,7 @@ static void test_cancel_concluded(void) assert(job->status == JOB_STATUS_READY); WITH_JOB_LOCK_GUARD() { - job_complete(job, &error_abort); + job_complete_locked(job, &error_abort); } job_enter(job); while (!job->deferred_to_main_loop) { @@ -407,7 +407,7 @@ static void test_cancel_concluded(void) aio_context_acquire(job->aio_context); WITH_JOB_LOCK_GUARD() { - job_finalize(job, &error_abort); + job_finalize_locked(job, &error_abort); } aio_context_release(job->aio_context); assert(job->status == JOB_STATUS_CONCLUDED); @@ -456,7 +456,7 @@ static const BlockJobDriver test_yielding_driver = { }; /* - * Test that job_complete() works even on jobs that are in a paused + * Test that job_complete_locked() works even on jobs that are in a paused * state (i.e., STANDBY). * * To do this, run YieldingJob in an IO thread, get it into the READY @@ -464,7 +464,7 @@ static const BlockJobDriver test_yielding_driver = { * acquire the context so the job will not be entered and will thus * remain on STANDBY. * - * job_complete() should still work without error. + * job_complete_locked() should still work without error. * * Note that on the QMP interface, it is impossible to lock an IO * thread before a drained section ends. In practice, the @@ -526,16 +526,16 @@ static void test_complete_in_standby(void) assert(job->status == JOB_STATUS_STANDBY); /* Even though the job is on standby, this should work */ - job_complete(job, &error_abort); + job_complete_locked(job, &error_abort); /* The test is done now, clean up. */ - job_finish_sync(job, NULL, &error_abort); + job_finish_sync_locked(job, NULL, &error_abort); assert(job->status == JOB_STATUS_PENDING); - job_finalize(job, &error_abort); + job_finalize_locked(job, &error_abort); assert(job->status == JOB_STATUS_CONCLUDED); - job_dismiss(&job, &error_abort); + job_dismiss_locked(&job, &error_abort); } destroy_blk(blk); From patchwork Fri Jan 28 11:50:23 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: 1585641 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=Q7cb/Uit; 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 4JlbgR16N4z9t56 for ; Fri, 28 Jan 2022 23:02:41 +1100 (AEDT) Received: from localhost ([::1]:48604 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDPxe-00013U-KH for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:02:38 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60808) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmU-0004b6-S1 for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:07 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:53081) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmO-0003Ql-71 for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370651; 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=Z8LmR5UB0sxldZrzxM2ie6cp7lMgOr3I6o5e+HjgF6I=; b=Q7cb/Uit1hcedFQdFCPB6KXzapjKfbVmVttqLWhQo03UvBFmTqqs1pHUjH5FH2iToKd0F0 arCsN8cu5uBA8Jrg2d4M5vV7By8Zo56B4KmEyQMKeJUUKVW24CZU/0wRFuIVRV3ia79CYz vKqn0S/Hz10giBaST+I8hPoDXehXr2s= 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-501-6dkdK4KfPXG3JclXicQ3Zg-1; Fri, 28 Jan 2022 06:50:48 -0500 X-MC-Unique: 6dkdK4KfPXG3JclXicQ3Zg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4E54B1006AA0; Fri, 28 Jan 2022 11:50:47 +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 57D5222DE3; Fri, 28 Jan 2022 11:50:46 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 11/19] jobs: document all static functions and add _locked() suffix Date: Fri, 28 Jan 2022 06:50:23 -0500 Message-Id: <20220128115031.4061565-12-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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.129.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" Now that we added the job_lock/unlock pairs, we can also rename all static functions in job.c that are called with the job mutex held as _locked(), and add a little comment on top. No functional change intended. Signed-off-by: Emanuele Giuseppe Esposito --- blockjob.c | 29 +++-- include/block/blockjob.h | 6 +- job.c | 247 ++++++++++++++++++++++----------------- monitor/qmp-cmds.c | 2 +- 4 files changed, 165 insertions(+), 119 deletions(-) diff --git a/blockjob.c b/blockjob.c index c370bd0b5e..f1a30833bd 100644 --- a/blockjob.c +++ b/blockjob.c @@ -256,7 +256,8 @@ int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs, return 0; } -static void block_job_on_idle(Notifier *n, void *opaque) +/* Called with job_mutex lock held. */ +static void block_job_on_idle_locked(Notifier *n, void *opaque) { /* * we can't kick with job_mutex held, but we also want @@ -369,7 +370,8 @@ static void block_job_iostatus_set_err(BlockJob *job, int error) } } -static void block_job_event_cancelled(Notifier *n, void *opaque) +/* Called with job_mutex lock held. */ +static void block_job_event_cancelled_locked(Notifier *n, void *opaque) { BlockJob *job = opaque; uint64_t progress_current, progress_total; @@ -388,7 +390,8 @@ static void block_job_event_cancelled(Notifier *n, void *opaque) job->speed); } -static void block_job_event_completed(Notifier *n, void *opaque) +/* Called with job_mutex lock held. */ +static void block_job_event_completed_locked(Notifier *n, void *opaque) { BlockJob *job = opaque; const char *msg = NULL; @@ -414,7 +417,8 @@ static void block_job_event_completed(Notifier *n, void *opaque) msg); } -static void block_job_event_pending(Notifier *n, void *opaque) +/* Called with job_mutex lock held. */ +static void block_job_event_pending_locked(Notifier *n, void *opaque) { BlockJob *job = opaque; @@ -426,7 +430,8 @@ static void block_job_event_pending(Notifier *n, void *opaque) job->job.id); } -static void block_job_event_ready(Notifier *n, void *opaque) +/* Called with job_mutex lock held. */ +static void block_job_event_ready_locked(Notifier *n, void *opaque) { BlockJob *job = opaque; uint64_t progress_current, progress_total; @@ -477,11 +482,11 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver, ratelimit_init(&job->limit); - job->finalize_cancelled_notifier.notify = block_job_event_cancelled; - job->finalize_completed_notifier.notify = block_job_event_completed; - job->pending_notifier.notify = block_job_event_pending; - job->ready_notifier.notify = block_job_event_ready; - job->idle_notifier.notify = block_job_on_idle; + job->finalize_cancelled_notifier.notify = block_job_event_cancelled_locked; + job->finalize_completed_notifier.notify = block_job_event_completed_locked; + job->pending_notifier.notify = block_job_event_pending_locked; + job->ready_notifier.notify = block_job_event_ready_locked; + job->idle_notifier.notify = block_job_on_idle_locked; WITH_JOB_LOCK_GUARD() { notifier_list_add(&job->job.on_finalize_cancelled, @@ -517,7 +522,7 @@ fail: return NULL; } -void block_job_iostatus_reset(BlockJob *job) +void block_job_iostatus_reset_locked(BlockJob *job) { assert(qemu_in_main_thread()); if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { @@ -532,7 +537,7 @@ void block_job_user_resume(Job *job) BlockJob *bjob = container_of(job, BlockJob, job); assert(qemu_in_main_thread()); WITH_JOB_LOCK_GUARD() { - block_job_iostatus_reset(bjob); + block_job_iostatus_reset_locked(bjob); } } diff --git a/include/block/blockjob.h b/include/block/blockjob.h index cabfa52d5b..a9a6cf2b84 100644 --- a/include/block/blockjob.h +++ b/include/block/blockjob.h @@ -159,13 +159,15 @@ bool block_job_set_speed_locked(BlockJob *job, int64_t speed, Error **errp); BlockJobInfo *block_job_query_locked(BlockJob *job, Error **errp); /** - * block_job_iostatus_reset: + * block_job_iostatus_reset_locked: * @job: The job whose I/O status should be reset. * * Reset I/O status on @job and on BlockDriverState objects it uses, * other than job->blk. + * + * Called with job_mutex lock held. */ -void block_job_iostatus_reset(BlockJob *job); +void block_job_iostatus_reset_locked(BlockJob *job); /* Common functions that are neither I/O nor Global State */ diff --git a/job.c b/job.c index b169bd7280..2f2b4892e0 100644 --- a/job.c +++ b/job.c @@ -38,6 +38,7 @@ */ QemuMutex job_mutex; +/* Protected by job_mutex */ static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs); /* Job State Transition Table */ @@ -113,7 +114,8 @@ JobTxn *job_txn_new(void) return txn; } -static void job_txn_ref(JobTxn *txn) +/* Called with job_mutex held. */ +static void job_txn_ref_locked(JobTxn *txn) { txn->refcnt++; } @@ -135,10 +137,11 @@ void job_txn_add_job_locked(JobTxn *txn, Job *job) job->txn = txn; QLIST_INSERT_HEAD(&txn->jobs, job, txn_list); - job_txn_ref(txn); + job_txn_ref_locked(txn); } -static void job_txn_del_job(Job *job) +/* Called with job_mutex held. */ +static void job_txn_del_job_locked(Job *job) { if (job->txn) { QLIST_REMOVE(job, txn_list); @@ -147,17 +150,18 @@ static void job_txn_del_job(Job *job) } } -static int job_txn_apply(Job *job, int fn(Job *)) +/* Called with job_mutex held. */ +static int job_txn_apply_locked(Job *job, int fn(Job *)) { Job *other_job, *next; JobTxn *txn = job->txn; int rc = 0; /* - * Similar to job_completed_txn_abort, we take each job's lock before - * applying fn, but since we assume that outer_ctx is held by the caller, - * we need to release it here to avoid holding the lock twice - which would - * break AIO_WAIT_WHILE from within fn. + * Similar to job_completed_txn_abort_locked, we take each job's lock + * before applying fn, but since we assume that outer_ctx is held by the + * caller, we need to release it here to avoid holding the lock + * twice - which would break AIO_WAIT_WHILE from within fn. */ job_ref_locked(job); aio_context_release(job->aio_context); @@ -183,7 +187,8 @@ bool job_is_internal(Job *job) return (job->id == NULL); } -static void job_state_transition(Job *job, JobStatus s1) +/* Called with job_mutex held. */ +static void job_state_transition_locked(Job *job, JobStatus s1) { JobStatus s0 = job->status; assert(s1 >= 0 && s1 < JOB_STATUS__MAX); @@ -296,7 +301,8 @@ static bool job_started(Job *job) return job->co; } -static bool job_should_pause(Job *job) +/* Called with job_mutex held. */ +static bool job_should_pause_locked(Job *job) { return job->pause_count > 0; } @@ -322,6 +328,7 @@ Job *job_get_locked(const char *id) return NULL; } +/* Called with job_mutex *not* held. */ static void job_sleep_timer_cb(void *opaque) { Job *job = opaque; @@ -376,7 +383,7 @@ void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn, notifier_list_init(&job->on_ready); notifier_list_init(&job->on_idle); - job_state_transition(job, JOB_STATUS_CREATED); + job_state_transition_locked(job, JOB_STATUS_CREATED); aio_timer_init(qemu_get_aio_context(), &job->sleep_timer, QEMU_CLOCK_REALTIME, SCALE_NS, job_sleep_timer_cb, job); @@ -444,7 +451,7 @@ void job_progress_increase_remaining(Job *job, uint64_t delta) * To be called when a cancelled job is finalised. * Called with job_mutex held. */ -static void job_event_cancelled(Job *job) +static void job_event_cancelled_locked(Job *job) { notifier_list_notify(&job->on_finalize_cancelled, job); } @@ -453,22 +460,25 @@ static void job_event_cancelled(Job *job) * To be called when a successfully completed job is finalised. * Called with job_mutex held. */ -static void job_event_completed(Job *job) +static void job_event_completed_locked(Job *job) { notifier_list_notify(&job->on_finalize_completed, job); } -static void job_event_pending(Job *job) +/* Called with job_mutex held. */ +static void job_event_pending_locked(Job *job) { notifier_list_notify(&job->on_pending, job); } -static void job_event_ready(Job *job) +/* Called with job_mutex held. */ +static void job_event_ready_locked(Job *job) { notifier_list_notify(&job->on_ready, job); } -static void job_event_idle(Job *job) +/* Called with job_mutex held. */ +static void job_event_idle_locked(Job *job) { notifier_list_notify(&job->on_idle, job); } @@ -511,15 +521,18 @@ void job_enter(Job *job) * is allowed and cancels the timer. * * If @ns is (uint64_t) -1, no timer is scheduled and job_enter() must be - * called explicitly. */ -static void coroutine_fn job_do_yield(Job *job, uint64_t ns) + * called explicitly. + * + * Called with job_mutex held, but releases it temporarly. + */ +static void coroutine_fn job_do_yield_locked(Job *job, uint64_t ns) { real_job_lock(); if (ns != -1) { timer_mod(&job->sleep_timer, ns); } job->busy = false; - job_event_idle(job); + job_event_idle_locked(job); real_job_unlock(); job_unlock(); qemu_coroutine_yield(); @@ -534,7 +547,7 @@ void coroutine_fn job_pause_point(Job *job) assert(job && job_started(job)); job_lock(); - if (!job_should_pause(job)) { + if (!job_should_pause_locked(job)) { job_unlock(); return; } @@ -549,15 +562,15 @@ void coroutine_fn job_pause_point(Job *job) job_lock(); } - if (job_should_pause(job) && !job_is_cancelled(job)) { + if (job_should_pause_locked(job) && !job_is_cancelled_locked(job)) { JobStatus status = job->status; - job_state_transition(job, status == JOB_STATUS_READY + job_state_transition_locked(job, status == JOB_STATUS_READY ? JOB_STATUS_STANDBY : JOB_STATUS_PAUSED); job->paused = true; - job_do_yield(job, -1); + job_do_yield_locked(job, -1); job->paused = false; - job_state_transition(job, status); + job_state_transition_locked(job, status); } job_unlock(); @@ -576,8 +589,8 @@ void job_yield(Job *job) return; } - if (!job_should_pause(job)) { - job_do_yield(job, -1); + if (!job_should_pause_locked(job)) { + job_do_yield_locked(job, -1); } } @@ -594,8 +607,9 @@ void coroutine_fn job_sleep_ns(Job *job, int64_t ns) return; } - if (!job_should_pause(job)) { - job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns); + if (!job_should_pause_locked(job)) { + job_do_yield_locked(job, + qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns); } } @@ -666,16 +680,17 @@ void job_user_resume_locked(Job *job, Error **errp) job_resume_locked(job); } -static void job_do_dismiss(Job *job) +/* Called with job_mutex held. */ +static void job_do_dismiss_locked(Job *job) { assert(job); job->busy = false; job->paused = false; job->deferred_to_main_loop = true; - job_txn_del_job(job); + job_txn_del_job_locked(job); - job_state_transition(job, JOB_STATUS_NULL); + job_state_transition_locked(job, JOB_STATUS_NULL); job_unref_locked(job); } @@ -688,14 +703,14 @@ void job_dismiss_locked(Job **jobptr, Error **errp) return; } - job_do_dismiss(job); + job_do_dismiss_locked(job); *jobptr = NULL; } void job_early_fail_locked(Job *job) { assert(job->status == JOB_STATUS_CREATED); - job_do_dismiss(job); + job_do_dismiss_locked(job); } void job_early_fail(Job *job) @@ -704,15 +719,17 @@ void job_early_fail(Job *job) job_early_fail_locked(job); } -static void job_conclude(Job *job) +/* Called with job_mutex held. */ +static void job_conclude_locked(Job *job) { - job_state_transition(job, JOB_STATUS_CONCLUDED); + job_state_transition_locked(job, JOB_STATUS_CONCLUDED); if (job->auto_dismiss || !job_started(job)) { - job_do_dismiss(job); + job_do_dismiss_locked(job); } } -static void job_update_rc(Job *job) +/* Called with job_mutex held. */ +static void job_update_rc_locked(Job *job) { if (!job->ret && job_is_cancelled(job)) { job->ret = -ECANCELED; @@ -721,11 +738,12 @@ static void job_update_rc(Job *job) if (!job->err) { error_setg(&job->err, "%s", strerror(-job->ret)); } - job_state_transition(job, JOB_STATUS_ABORTING); + job_state_transition_locked(job, JOB_STATUS_ABORTING); } } -static void job_commit(Job *job) +/* Called with job_mutex held, but releases it temporarly */ +static void job_commit_locked(Job *job) { assert(!job->ret); assert(qemu_in_main_thread()); @@ -736,7 +754,8 @@ static void job_commit(Job *job) } } -static void job_abort(Job *job) +/* Called with job_mutex held, but releases it temporarly */ +static void job_abort_locked(Job *job) { assert(job->ret); assert(qemu_in_main_thread()); @@ -747,7 +766,8 @@ static void job_abort(Job *job) } } -static void job_clean(Job *job) +/* Called with job_mutex held, but releases it temporarly */ +static void job_clean_locked(Job *job) { assert(qemu_in_main_thread()); if (job->driver->clean) { @@ -757,7 +777,8 @@ static void job_clean(Job *job) } } -static int job_finalize_single(Job *job) +/* Called with job_mutex held, but releases it temporarly. */ +static int job_finalize_single_locked(Job *job) { int job_ret; AioContext *ctx = job->aio_context; @@ -765,16 +786,16 @@ static int job_finalize_single(Job *job) assert(job_is_completed_locked(job)); /* Ensure abort is called for late-transactional failures */ - job_update_rc(job); + job_update_rc_locked(job); aio_context_acquire(ctx); if (!job->ret) { - job_commit(job); + job_commit_locked(job); } else { - job_abort(job); + job_abort_locked(job); } - job_clean(job); + job_clean_locked(job); aio_context_release(ctx); @@ -787,19 +808,20 @@ static int job_finalize_single(Job *job) /* Emit events only if we actually started */ if (job_started(job)) { - if (job_is_cancelled(job)) { - job_event_cancelled(job); + if (job_is_cancelled_locked(job)) { + job_event_cancelled_locked(job); } else { - job_event_completed(job); + job_event_completed_locked(job); } } - job_txn_del_job(job); - job_conclude(job); + job_txn_del_job_locked(job); + job_conclude_locked(job); return 0; } -static void job_cancel_async(Job *job, bool force) +/* Called with job_mutex held, but releases it temporarly. */ +static void job_cancel_async_locked(Job *job, bool force) { assert(qemu_in_main_thread()); if (job->driver->cancel) { @@ -837,7 +859,8 @@ static void job_cancel_async(Job *job, bool force) } } -static void job_completed_txn_abort(Job *job) +/* Called with job_mutex held. */ +static void job_completed_txn_abort_locked(Job *job) { AioContext *ctx; JobTxn *txn = job->txn; @@ -850,12 +873,12 @@ static void job_completed_txn_abort(Job *job) return; } txn->aborting = true; - job_txn_ref(txn); + job_txn_ref_locked(txn); /* * We can only hold the single job's AioContext lock while calling - * job_finalize_single() because the finalization callbacks can involve - * calls of AIO_WAIT_WHILE(), which could deadlock otherwise. + * job_finalize_single_locked() because the finalization callbacks can + * involve calls of AIO_WAIT_WHILE(), which could deadlock otherwise. * Note that the job's AioContext may change when it is finalized. */ job_ref_locked(job); @@ -870,10 +893,10 @@ static void job_completed_txn_abort(Job *job) aio_context_acquire(ctx); /* * This is a transaction: If one job failed, no result will matter. - * Therefore, pass force=true to terminate all other jobs as quickly - * as possible. + * Therefore, pass force=true to terminate all other jobs as + * quickly as possible. */ - job_cancel_async(other_job, true); + job_cancel_async_locked(other_job, true); aio_context_release(ctx); } } @@ -890,12 +913,12 @@ static void job_completed_txn_abort(Job *job) job_finish_sync_locked(other_job, NULL, NULL); } aio_context_release(ctx); - job_finalize_single(other_job); + job_finalize_single_locked(other_job); } /* * Use job_ref_locked()/job_unref_locked() so we can read the AioContext - * here even if the job went away during job_finalize_single(). + * here even if the job went away during job_finalize_single_locked(). */ aio_context_acquire(job->aio_context); job_unref_locked(job); @@ -903,7 +926,8 @@ static void job_completed_txn_abort(Job *job) job_txn_unref_locked(txn); } -static int job_prepare(Job *job) +/* Called with job_mutex held, but releases it temporarly. */ +static int job_prepare_locked(Job *job) { int ret; AioContext *ctx = job->aio_context; @@ -916,28 +940,30 @@ static int job_prepare(Job *job) aio_context_release(ctx); job_lock(); job->ret = ret; - job_update_rc(job); + job_update_rc_locked(job); } return job->ret; } -static int job_needs_finalize(Job *job) +/* Called with job_mutex held. */ +static int job_needs_finalize_locked(Job *job) { return !job->auto_finalize; } -static void job_do_finalize(Job *job) +/* Called with job_mutex held. */ +static void job_do_finalize_locked(Job *job) { int rc; assert(job && job->txn); /* prepare the transaction to complete */ - rc = job_txn_apply(job, job_prepare); + rc = job_txn_apply_locked(job, job_prepare_locked); if (rc) { - job_completed_txn_abort(job); + job_completed_txn_abort_locked(job); } else { - job_txn_apply(job, job_finalize_single); + job_txn_apply_locked(job, job_finalize_single_locked); } } @@ -947,14 +973,15 @@ void job_finalize_locked(Job *job, Error **errp) if (job_apply_verb_locked(job, JOB_VERB_FINALIZE, errp)) { return; } - job_do_finalize(job); + job_do_finalize_locked(job); } -static int job_transition_to_pending(Job *job) +/* Called with job_mutex held. */ +static int job_transition_to_pending_locked(Job *job) { - job_state_transition(job, JOB_STATUS_PENDING); + job_state_transition_locked(job, JOB_STATUS_PENDING); if (!job->auto_finalize) { - job_event_pending(job); + job_event_pending_locked(job); } return 0; } @@ -962,16 +989,17 @@ static int job_transition_to_pending(Job *job) void job_transition_to_ready(Job *job) { JOB_LOCK_GUARD(); - job_state_transition(job, JOB_STATUS_READY); - job_event_ready(job); + job_state_transition_locked(job, JOB_STATUS_READY); + job_event_ready_locked(job); } -static void job_completed_txn_success(Job *job) +/* Called with job_mutex held. */ +static void job_completed_txn_success_locked(Job *job) { JobTxn *txn = job->txn; Job *other_job; - job_state_transition(job, JOB_STATUS_WAITING); + job_state_transition_locked(job, JOB_STATUS_WAITING); /* * Successful completion, see if there are other running jobs in this @@ -984,28 +1012,32 @@ static void job_completed_txn_success(Job *job) assert(other_job->ret == 0); } - job_txn_apply(job, job_transition_to_pending); + job_txn_apply_locked(job, job_transition_to_pending_locked); /* If no jobs need manual finalization, automatically do so */ - if (job_txn_apply(job, job_needs_finalize) == 0) { - job_do_finalize(job); + if (job_txn_apply_locked(job, job_needs_finalize_locked) == 0) { + job_do_finalize_locked(job); } } -static void job_completed(Job *job) +/* Called with job_mutex held. */ +static void job_completed_locked(Job *job) { assert(job && job->txn && !job_is_completed_locked(job)); - job_update_rc(job); + job_update_rc_locked(job); trace_job_completed(job, job->ret); if (job->ret) { - job_completed_txn_abort(job); + job_completed_txn_abort_locked(job); } else { - job_completed_txn_success(job); + job_completed_txn_success_locked(job); } } -/** Useful only as a type shim for aio_bh_schedule_oneshot. */ +/** + * Useful only as a type shim for aio_bh_schedule_oneshot. + * Called with job_mutex *not* held. + */ static void job_exit(void *opaque) { Job *job = (Job *)opaque; @@ -1020,15 +1052,15 @@ static void job_exit(void *opaque) * drain block nodes, and if .drained_poll still returned true, we would * deadlock. */ job->busy = false; - job_event_idle(job); + job_event_idle_locked(job); - job_completed(job); + job_completed_locked(job); /* - * Note that calling job_completed can move the job to a different - * aio_context, so we cannot cache from above. job_txn_apply takes care of - * acquiring the new lock, and we ref/unref to avoid job_completed freeing - * the job underneath us. + * Note that calling job_completed_locked can move the job to a different + * aio_context, so we cannot cache from above. job_txn_apply_locked takes + * care of acquiring the new lock, and we ref/unref to avoid + * job_completed_locked freeing the job underneath us. */ ctx = job->aio_context; job_unref_locked(job); @@ -1038,6 +1070,8 @@ static void job_exit(void *opaque) /** * All jobs must allow a pause point before entering their job proper. This * ensures that jobs can be paused prior to being started, then resumed later. + * + * Called with job_mutex *not* held. */ static void coroutine_fn job_co_entry(void *opaque) { @@ -1055,6 +1089,7 @@ static void coroutine_fn job_co_entry(void *opaque) aio_bh_schedule_oneshot(qemu_get_aio_context(), job_exit, job); } +/* Called with job_mutex *not* held. */ static int job_pre_run(Job *job) { assert(qemu_in_main_thread()); @@ -1079,7 +1114,7 @@ void job_start(Job *job) job->pause_count--; job->busy = true; job->paused = false; - job_state_transition(job, JOB_STATUS_RUNNING); + job_state_transition_locked(job, JOB_STATUS_RUNNING); } aio_co_enter(job->aio_context, job->co); } @@ -1087,25 +1122,25 @@ void job_start(Job *job) void job_cancel_locked(Job *job, bool force) { if (job->status == JOB_STATUS_CONCLUDED) { - job_do_dismiss(job); + job_do_dismiss_locked(job); return; } - job_cancel_async(job, force); + job_cancel_async_locked(job, force); if (!job_started(job)) { - job_completed(job); + job_completed_locked(job); } else if (job->deferred_to_main_loop) { /* - * job_cancel_async() ignores soft-cancel requests for jobs + * job_cancel_async_locked() ignores soft-cancel requests for jobs * that are already done (i.e. deferred to the main loop). We * have to check again whether the job is really cancelled. * (job_cancel_requested() and job_is_cancelled() are equivalent - * here, because job_cancel_async() will make soft-cancel + * here, because job_cancel_async_locked() will make soft-cancel * requests no-ops when deferred_to_main_loop is true. We * choose to call job_is_cancelled() to show that we invoke - * job_completed_txn_abort() only for force-cancelled jobs.) + * job_completed_txn_abort_locked() only for force-cancelled jobs.) */ - if (job_is_cancelled(job)) { - job_completed_txn_abort(job); + if (job_is_cancelled_locked(job)) { + job_completed_txn_abort_locked(job); } } else { job_enter_cond_locked(job, NULL); @@ -1124,16 +1159,20 @@ void job_user_cancel_locked(Job *job, bool force, Error **errp) * A wrapper around job_cancel_locked() taking an Error ** parameter so * it may be used with job_finish_sync_locked() without the * need for (rather nasty) function pointer casts there. + * + * Called with job_mutex held. */ -static void job_cancel_err(Job *job, Error **errp) +static void job_cancel_err_locked(Job *job, Error **errp) { job_cancel_locked(job, false); } /** - * Same as job_cancel_err(), but force-cancel. + * Same as job_cancel_err_locked(), but force-cancel. + * + * Called with job_mutex held. */ -static void job_force_cancel_err(Job *job, Error **errp) +static void job_force_cancel_err_locked(Job *job, Error **errp) { job_cancel_locked(job, true); } @@ -1141,9 +1180,9 @@ static void job_force_cancel_err(Job *job, Error **errp) int job_cancel_sync_locked(Job *job, bool force) { if (force) { - return job_finish_sync_locked(job, &job_force_cancel_err, NULL); + return job_finish_sync_locked(job, &job_force_cancel_err_locked, NULL); } else { - return job_finish_sync_locked(job, &job_cancel_err, NULL); + return job_finish_sync_locked(job, &job_cancel_err_locked, NULL); } } diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c index 59f6bdd98c..58c96078f2 100644 --- a/monitor/qmp-cmds.c +++ b/monitor/qmp-cmds.c @@ -137,7 +137,7 @@ void qmp_cont(Error **errp) WITH_JOB_LOCK_GUARD() { for (job = block_job_next_locked(NULL); job; job = block_job_next_locked(job)) { - block_job_iostatus_reset(job); + block_job_iostatus_reset_locked(job); } } From patchwork Fri Jan 28 11:50:24 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: 1585647 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=KnYw531o; 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 4JlblM3fVnz9t56 for ; Fri, 28 Jan 2022 23:06:07 +1100 (AEDT) Received: from localhost ([::1]:49820 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDQ0w-00023N-Ee for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:06:02 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60662) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmQ-0004ad-94 for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:05 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:35170) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmJ-0003QS-Tp for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:50:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370650; 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=bpK6YwXXmaGiaJSMzMZgz5JuC7LtKXmaevRPXpWssQk=; b=KnYw531oPimpghdeeIkI6syjBg6TrtgfHS67j1v+bV+ipOlXIjTHq4JddDKyjqNVopKCEI JzFmJLa1RMCYzsBJQipNPkOxm08kESKhu4oGxSXSKER5kzg1fjlEUckM1NHBNLkF/g/vbf TDHQxaM0lajHVkoOBBpTQ6fAfXtf5q0= 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-453-Nmr0YNLqOrqFwg4JJXKgsw-1; Fri, 28 Jan 2022 06:50:49 -0500 X-MC-Unique: Nmr0YNLqOrqFwg4JJXKgsw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5FA742F4D; Fri, 28 Jan 2022 11:50:48 +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 694E622DE3; Fri, 28 Jan 2022 11:50:47 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 12/19] job.h: define unlocked functions Date: Fri, 28 Jan 2022 06:50:24 -0500 Message-Id: <20220128115031.4061565-13-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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.129.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" All these functions assume that the lock is not held, and acquire it internally. These functions will be useful when job_lock is globally applied, as they will allow callers to access the job struct fields without worrying about the job lock. Update also the comments in blockjob.c (and move them in job.c). Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*. Signed-off-by: Emanuele Giuseppe Esposito --- blockjob.c | 20 ----------------- include/qemu/job.h | 38 ++++++++++++++++++++++++++++--- job.c | 56 +++++++++++++++++++++++++++++++++++----------- 3 files changed, 78 insertions(+), 36 deletions(-) diff --git a/blockjob.c b/blockjob.c index f1a30833bd..a6c94b6b64 100644 --- a/blockjob.c +++ b/blockjob.c @@ -36,21 +36,6 @@ #include "qemu/main-loop.h" #include "qemu/timer.h" -/* - * The block job API is composed of two categories of functions. - * - * The first includes functions used by the monitor. The monitor is - * peculiar in that it accesses the block job list with block_job_get, and - * therefore needs consistency across block_job_get and the actual operation - * (e.g. block_job_set_speed). The consistency is achieved with - * aio_context_acquire/release. These functions are declared in blockjob.h. - * - * The second includes functions used by the block job drivers and sometimes - * by the core block layer. These do not care about locking, because the - * whole coroutine runs under the AioContext lock, and are declared in - * blockjob_int.h. - */ - static bool is_block_job(Job *job) { return job_type(job) == JOB_TYPE_BACKUP || @@ -451,11 +436,6 @@ static void block_job_event_ready_locked(Notifier *n, void *opaque) } -/* - * API for block job drivers and the block layer. These functions are - * declared in blockjob_int.h. - */ - void *block_job_create(const char *job_id, const BlockJobDriver *driver, JobTxn *txn, BlockDriverState *bs, uint64_t perm, uint64_t shared_perm, int64_t speed, int flags, diff --git a/include/qemu/job.h b/include/qemu/job.h index e13c976ba5..fc57472e84 100644 --- a/include/qemu/job.h +++ b/include/qemu/job.h @@ -382,6 +382,7 @@ void job_txn_add_job_locked(JobTxn *txn, Job *job); /** * Create a new long-running job and return it. + * Called with job_mutex *not* held. * * @job_id: The id of the newly-created job, or %NULL for internal jobs * @driver: The class object for the newly-created job. @@ -417,6 +418,8 @@ void job_unref_locked(Job *job); * @done: How much progress the job made since the last call * * Updates the progress counter of the job. + * + * Progress API is thread safe. */ void job_progress_update(Job *job, uint64_t done); @@ -427,6 +430,8 @@ void job_progress_update(Job *job, uint64_t done); * * Sets the expected end value of the progress counter of a job so that a * completion percentage can be calculated when the progress is updated. + * + * Progress API is thread safe. */ void job_progress_set_remaining(Job *job, uint64_t remaining); @@ -442,6 +447,8 @@ void job_progress_set_remaining(Job *job, uint64_t remaining); * length before, and job_progress_update() afterwards. * (So the operation acts as a parenthesis in regards to the main job * operation running in background.) + * + * Progress API is thread safe. */ void job_progress_increase_remaining(Job *job, uint64_t delta); @@ -459,13 +466,17 @@ void job_enter_cond_locked(Job *job, bool(*fn)(Job *job)); * * Begins execution of a job. * Takes ownership of one reference to the job object. + * + * Called with job_mutex *not* held. */ void job_start(Job *job); /** * @job: The job to enter. + * Called with job_mutex *not* held. * * Continue the specified job by entering the coroutine. + * Called with job_mutex lock *not* held. */ void job_enter(Job *job); @@ -475,6 +486,9 @@ void job_enter(Job *job); * Pause now if job_pause_locked() has been called. * Jobs that perform lots of I/O must call this between * requests so that the job can be paused. + * + * Called with job_mutex *not* held (we don't want the coroutine + * to yield with the lock held!). */ void coroutine_fn job_pause_point(Job *job); @@ -482,6 +496,8 @@ void coroutine_fn job_pause_point(Job *job); * @job: The job that calls the function. * * Yield the job coroutine. + * Called with job_mutex *not* held (we don't want the coroutine + * to yield with the lock held!). */ void job_yield(Job *job); @@ -492,6 +508,9 @@ void job_yield(Job *job); * Put the job to sleep (assuming that it wasn't canceled) for @ns * %QEMU_CLOCK_REALTIME nanoseconds. Canceling the job will immediately * interrupt the wait. + * + * Called with job_mutex *not* held (we don't want the coroutine + * to yield with the lock held!). */ void coroutine_fn job_sleep_ns(Job *job, int64_t ns); @@ -504,12 +523,19 @@ const char *job_type_str(const Job *job); /** Returns true if the job should not be visible to the management layer. */ bool job_is_internal(Job *job); -/** Returns whether the job is being cancelled. */ +/** + * Returns whether the job is being cancelled. + * Called with job_mutex *not* held. + */ bool job_is_cancelled(Job *job); +/** Just like job_is_cancelled, but called between job_lock and job_unlock */ +bool job_is_cancelled_locked(Job *job); + /** * Returns whether the job is scheduled for cancellation (at an * indefinite point). + * Called with job_mutex *not* held. */ bool job_cancel_requested(Job *job); @@ -593,13 +619,19 @@ Job *job_get_locked(const char *id); */ int job_apply_verb_locked(Job *job, JobVerb verb, Error **errp); -/** The @job could not be started, free it. */ +/** + * The @job could not be started, free it. + * Called with job_mutex *not* held. + */ void job_early_fail(Job *job); /** Same as job_early_fail(), but assumes job_lock is held. */ void job_early_fail_locked(Job *job); -/** Moves the @job from RUNNING to READY */ +/** + * Moves the @job from RUNNING to READY. + * Called with job_mutex *not* held. + */ void job_transition_to_ready(Job *job); /** diff --git a/job.c b/job.c index 2f2b4892e0..468a387390 100644 --- a/job.c +++ b/job.c @@ -32,6 +32,22 @@ #include "trace/trace-root.h" #include "qapi/qapi-events-job.h" +/* + * The job API is composed of two categories of functions. + * + * The first includes functions used by the monitor. The monitor is + * peculiar in that it accesses the block job list with job_get, and + * therefore needs consistency across job_get and the actual operation + * (e.g. job_user_cancel). To achieve this consistency, the caller + * calls job_lock/job_unlock itself around the whole operation. + * These functions are declared in job-monitor.h. + * + * + * The second includes functions used by the block job drivers and sometimes + * by the core block layer. These delegate the locking to the callee instead, + * and are declared in job-driver.h. + */ + /* * job_mutex protects the jobs list, but also makes the * struct job fields thread-safe. @@ -227,18 +243,31 @@ const char *job_type_str(const Job *job) return JobType_str(job_type(job)); } -bool job_is_cancelled(Job *job) +bool job_is_cancelled_locked(Job *job) { /* force_cancel may be true only if cancelled is true, too */ assert(job->cancelled || !job->force_cancel); return job->force_cancel; } -bool job_cancel_requested(Job *job) +bool job_is_cancelled(Job *job) +{ + JOB_LOCK_GUARD(); + return job_is_cancelled_locked(job); +} + +/* Called with job_mutex held. */ +static bool job_cancel_requested_locked(Job *job) { return job->cancelled; } +bool job_cancel_requested(Job *job) +{ + JOB_LOCK_GUARD(); + return job_cancel_requested_locked(job); +} + bool job_is_ready_locked(Job *job) { switch (job->status) { @@ -551,7 +580,7 @@ void coroutine_fn job_pause_point(Job *job) job_unlock(); return; } - if (job_is_cancelled(job)) { + if (job_is_cancelled_locked(job)) { job_unlock(); return; } @@ -585,7 +614,7 @@ void job_yield(Job *job) assert(job->busy); /* Check cancellation *before* setting busy = false, too! */ - if (job_is_cancelled(job)) { + if (job_is_cancelled_locked(job)) { return; } @@ -603,7 +632,7 @@ void coroutine_fn job_sleep_ns(Job *job, int64_t ns) assert(job->busy); /* Check cancellation *before* setting busy = false, too! */ - if (job_is_cancelled(job)) { + if (job_is_cancelled_locked(job)) { return; } @@ -731,7 +760,7 @@ static void job_conclude_locked(Job *job) /* Called with job_mutex held. */ static void job_update_rc_locked(Job *job) { - if (!job->ret && job_is_cancelled(job)) { + if (!job->ret && job_is_cancelled_locked(job)) { job->ret = -ECANCELED; } if (job->ret) { @@ -909,7 +938,7 @@ static void job_completed_txn_abort_locked(Job *job) ctx = other_job->aio_context; aio_context_acquire(ctx); if (!job_is_completed_locked(other_job)) { - assert(job_cancel_requested(other_job)); + assert(job_cancel_requested_locked(other_job)); job_finish_sync_locked(other_job, NULL, NULL); } aio_context_release(ctx); @@ -1133,10 +1162,10 @@ void job_cancel_locked(Job *job, bool force) * job_cancel_async_locked() ignores soft-cancel requests for jobs * that are already done (i.e. deferred to the main loop). We * have to check again whether the job is really cancelled. - * (job_cancel_requested() and job_is_cancelled() are equivalent - * here, because job_cancel_async_locked() will make soft-cancel - * requests no-ops when deferred_to_main_loop is true. We - * choose to call job_is_cancelled() to show that we invoke + * (job_cancel_requested_locked() and job_is_cancelled_locked() are + * equivalent here, because job_cancel_async_locked() will make + * soft-cancel requests no-ops when deferred_to_main_loop is true. We + * choose to call job_is_cancelled_locked() to show that we invoke * job_completed_txn_abort_locked() only for force-cancelled jobs.) */ if (job_is_cancelled_locked(job)) { @@ -1213,7 +1242,7 @@ void job_complete_locked(Job *job, Error **errp) if (job_apply_verb_locked(job, JOB_VERB_COMPLETE, errp)) { return; } - if (job_cancel_requested(job) || !job->driver->complete) { + if (job_cancel_requested_locked(job) || !job->driver->complete) { error_setg(errp, "The active block job '%s' cannot be completed", job->id); return; @@ -1246,7 +1275,8 @@ int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp), (job_enter(job), !job_is_completed(job))); job_lock(); - ret = (job_is_cancelled(job) && job->ret == 0) ? -ECANCELED : job->ret; + ret = (job_is_cancelled_locked(job) && job->ret == 0) ? + -ECANCELED : job->ret; job_unref_locked(job); return ret; } From patchwork Fri Jan 28 11:50:25 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: 1585639 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=Hz04Nlqd; 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 4Jlbcf43vlz9t56 for ; Fri, 28 Jan 2022 23:00:18 +1100 (AEDT) Received: from localhost ([::1]:46678 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDPvM-0008DC-3q for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:00:16 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60732) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmS-0004af-GX for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:05 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:45101) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmO-0003RF-4z for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370653; 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=UN+o0pKDhQYko5tFHyLJseO205L+a1CvcIZ8PWaZ9Og=; b=Hz04Nlqd2Ee5DX7Lh2/ydGKF0qWHzGeYSx29NyecduaExaVVydaRvidfIHiFYfVNRUkiZV qVQ/exzghiuYwS80zDqvVm7b6CELtYitYVxmpldTRwz0v3t0xElDKYOJWgUetGrLRnpAFS V0E23iBWF3q96F4qqd5xltTdX3MkYVA= 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-490-1vLHofZzMJm1qeIunOrgKw-1; Fri, 28 Jan 2022 06:50:50 -0500 X-MC-Unique: 1vLHofZzMJm1qeIunOrgKw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7883F1083F6C; Fri, 28 Jan 2022 11:50:49 +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 7A70D22DE4; Fri, 28 Jan 2022 11:50:48 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 13/19] block/mirror.c: use of job helpers in drivers to avoid TOC/TOU Date: Fri, 28 Jan 2022 06:50:25 -0500 Message-Id: <20220128115031.4061565-14-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" Once job lock is used and aiocontext is removed, mirror has to perform job operations under the same critical section, using the helpers prepared in previous commit. Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*. Signed-off-by: Emanuele Giuseppe Esposito --- block/mirror.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/block/mirror.c b/block/mirror.c index bfc3ac1f77..0a3eb712e6 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -653,9 +653,13 @@ static int mirror_exit_common(Job *job) BlockDriverState *target_bs; BlockDriverState *mirror_top_bs; Error *local_err = NULL; - bool abort = job->ret < 0; + bool abort; int ret = 0; + WITH_JOB_LOCK_GUARD() { + abort = job->ret < 0; + } + if (s->prepared) { return 0; } @@ -1151,8 +1155,10 @@ static void mirror_complete(Job *job, Error **errp) s->should_complete = true; /* If the job is paused, it will be re-entered when it is resumed */ - if (!job->paused) { - job_enter(job); + WITH_JOB_LOCK_GUARD() { + if (!job->paused) { + job_enter_cond_locked(job, NULL); + } } } @@ -1172,8 +1178,11 @@ static bool mirror_drained_poll(BlockJob *job) * from one of our own drain sections, to avoid a deadlock waiting for * ourselves. */ - if (!s->common.job.paused && !job_is_cancelled(&job->job) && !s->in_drain) { - return true; + WITH_JOB_LOCK_GUARD() { + if (!s->common.job.paused && !job_is_cancelled_locked(&job->job) + && !s->in_drain) { + return true; + } } return !!s->in_flight; From patchwork Fri Jan 28 11:50:26 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: 1585658 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=H9/bzNAg; 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 4Jlc2L720Mz9t56 for ; Fri, 28 Jan 2022 23:19:06 +1100 (AEDT) Received: from localhost ([::1]:38828 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDQDY-0006yp-II for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:19:04 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60796) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmU-0004b1-Nv for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:07 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:23597) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmP-0003SL-VZ for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370657; 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=oTC6hAOkNVf4r3kj2csR/6UgeLJRKD1qOcAFfv0Ui8g=; b=H9/bzNAg+JuYvzZBKXPu514392pXmqnIqUKQqkXcfRUMmCKNISn6+3CpswCHtH+VClgTkc 4eTcwgMwz+VnugdML4JY1aGwjeWHn3gg+t3LbmdSbSTjJxhOBExEMe3EPKmLElV+DfsIOh AseA8bRCgIeO91hJBf/IdUGaiWM3L94= 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-494-WTa3g2JiPOSpA1Gt4GidLQ-1; Fri, 28 Jan 2022 06:50:51 -0500 X-MC-Unique: WTa3g2JiPOSpA1Gt4GidLQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7FFB71083F60; Fri, 28 Jan 2022 11:50:50 +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 8A66A1F478; Fri, 28 Jan 2022 11:50:49 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 14/19] blockjob: block_job_get_aio_context is a GS function Date: Fri, 28 Jan 2022 06:50:26 -0500 Message-Id: <20220128115031.4061565-15-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" Move block_job_get_aio_context under the GS category in blockjob.h, as it is always called with the BQL held. This is done also to simplify the coming job->aiocontext protection. For more infos about the GS API, see include/block/block-global-state.h Signed-off-by: Emanuele Giuseppe Esposito --- blockjob.c | 1 + include/block/blockjob.h | 15 ++++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/blockjob.c b/blockjob.c index a6c94b6b64..04d868f020 100644 --- a/blockjob.c +++ b/blockjob.c @@ -568,5 +568,6 @@ BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err, AioContext *block_job_get_aio_context(BlockJob *job) { + assert(qemu_in_main_thread()); return job->job.aio_context; } diff --git a/include/block/blockjob.h b/include/block/blockjob.h index a9a6cf2b84..376cd95877 100644 --- a/include/block/blockjob.h +++ b/include/block/blockjob.h @@ -169,6 +169,14 @@ BlockJobInfo *block_job_query_locked(BlockJob *job, Error **errp); */ void block_job_iostatus_reset_locked(BlockJob *job); +/* + * block_job_get_aio_context: + * + * Returns aio context associated with a block job. + */ +AioContext *block_job_get_aio_context(BlockJob *job); + + /* Common functions that are neither I/O nor Global State */ /** @@ -186,11 +194,4 @@ bool block_job_is_internal(BlockJob *job); */ const BlockJobDriver *block_job_driver(BlockJob *job); -/* - * block_job_get_aio_context: - * - * Returns aio context associated with a block job. - */ -AioContext *block_job_get_aio_context(BlockJob *job); - #endif From patchwork Fri Jan 28 11:50:27 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: 1585662 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=N7MADZXr; 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 4JlcDy3b0wz9t56 for ; Fri, 28 Jan 2022 23:28:17 +1100 (AEDT) Received: from localhost ([::1]:53630 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDQMP-0001F4-Fj for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:28:13 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60810) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmU-0004b7-Rq for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:07 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:40346) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmP-0003Rk-Uw for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370656; 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=wX+3AwW6iYj+pDk0+pG3oFLfarjTAcAJI2uFQ6auxic=; b=N7MADZXrqNmSnCzWQnMgDqMFSqz2mwHfAo+tQthmIPwTUSROnr1s6YPYqmiH5BK7xVofzr 5nXJaGEwd5wH/bh7MZX79ES61RBS2XDVCVwG7sGQ2mYTFypXtBXD4TmtyjOhCq8SEWyjF8 Ny1BHn3BWy2s3s80Silw8CWdVkihlcU= 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-516-7LBxFUP-OxatV0jfMjNJUw-1; Fri, 28 Jan 2022 06:50:52 -0500 X-MC-Unique: 7LBxFUP-OxatV0jfMjNJUw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 901338144E0; Fri, 28 Jan 2022 11:50:51 +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 996B322DE3; Fri, 28 Jan 2022 11:50:50 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 15/19] commit and mirror: create new nodes using bdrv_get_aio_context, and not the job aiocontext Date: Fri, 28 Jan 2022 06:50:27 -0500 Message-Id: <20220128115031.4061565-16-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" We are always using the given bs AioContext, so there is no need to take the job ones (which is identical anyways). This also reduces the point we need to check when protecting job.aio_context field. Signed-off-by: Emanuele Giuseppe Esposito --- block/commit.c | 4 ++-- block/mirror.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/block/commit.c b/block/commit.c index 2883a3ba52..3ea460fe4a 100644 --- a/block/commit.c +++ b/block/commit.c @@ -369,7 +369,7 @@ void commit_start(const char *job_id, BlockDriverState *bs, goto fail; } - s->base = blk_new(s->common.job.aio_context, + s->base = blk_new(bdrv_get_aio_context(bs), base_perms, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED); @@ -381,7 +381,7 @@ void commit_start(const char *job_id, BlockDriverState *bs, s->base_bs = base; /* Required permissions are already taken with block_job_add_bdrv() */ - s->top = blk_new(s->common.job.aio_context, 0, BLK_PERM_ALL); + s->top = blk_new(bdrv_get_aio_context(bs), 0, BLK_PERM_ALL); ret = blk_insert_bs(s->top, top, errp); if (ret < 0) { goto fail; diff --git a/block/mirror.c b/block/mirror.c index 0a3eb712e6..28d137407c 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -1727,7 +1727,7 @@ static BlockJob *mirror_start_job( goto fail; } - s->target = blk_new(s->common.job.aio_context, + s->target = blk_new(bdrv_get_aio_context(bs), target_perms, target_shared_perms); ret = blk_insert_bs(s->target, target, errp); if (ret < 0) { From patchwork Fri Jan 28 11:50:28 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: 1585660 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=SFH/mswA; 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 4Jlc7X48GQz9t56 for ; Fri, 28 Jan 2022 23:23:36 +1100 (AEDT) Received: from localhost ([::1]:44912 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDQHs-0002w3-Iz for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:23:33 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60812) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmU-0004b8-Ri for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:07 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:36471) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmP-0003S9-W9 for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370657; 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=GbkEuR1C6NYQz0JZJ4AH1ROAHc6gyehm+OZm5zwf6uA=; b=SFH/mswAKyMT1SBWhQdpFfDkOQGouE0YKQJ/mPyeyDFy+d7AYGMfGeXvbA4yWbsanNt/iA F+zlrLq7TKWLHtoXffDpjPh4YfvkZngDUDA9CFa8NpZSaW8CQQvu+crjohfv5ZImY2xCNA C4J0RgJlglKvw428FIyEK9XaaBJ491A= 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-399-BtQNS9GzNA2f9Du-LE4Ofw-1; Fri, 28 Jan 2022 06:50:54 -0500 X-MC-Unique: BtQNS9GzNA2f9Du-LE4Ofw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 912A61083F60; Fri, 28 Jan 2022 11:50:52 +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 AA02E22DE7; Fri, 28 Jan 2022 11:50:51 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 16/19] job: detect change of aiocontext within job coroutine Date: Fri, 28 Jan 2022 06:50:28 -0500 Message-Id: <20220128115031.4061565-17-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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.129.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Markus Armbruster , 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" From: Paolo Bonzini We want to make sure access of job->aio_context is always done under either BQL or job_mutex. The problem is that using aio_co_enter(job->aiocontext, job->co) in job_start and job_enter_cond makes the coroutine immediately resume, so we can't hold the job lock. And caching it is not safe either, as it might change. job_start is under BQL, so it can freely read job->aiocontext, but job_enter_cond is not. In order to fix this, use aio_co_wake(): the advantage is that it won't use job->aiocontext, but the main disadvantage is that it won't be able to detect a change of job AioContext. Calling bdrv_try_set_aio_context() will issue the following calls (simplified): * in terms of bdrv callbacks: .drained_begin -> .set_aio_context -> .drained_end * in terms of child_job functions: child_job_drained_begin -> child_job_set_aio_context -> child_job_drained_end * in terms of job functions: job_pause_locked -> job_set_aio_context -> job_resume_locked We can see that after setting the new aio_context, job_resume_locked calls again job_enter_cond, which then invokes aio_co_wake(). But while job->aiocontext has been set in job_set_aio_context, job->co->ctx has not changed, so the coroutine would be entering in the wrong aiocontext. Using aio_co_schedule in job_resume_locked() might seem as a valid alternative, but the problem is that the bh resuming the coroutine is not scheduled immediately, and if in the meanwhile another bdrv_try_set_aio_context() is run (see test_propagate_mirror() in test-block-iothread.c), we would have the first schedule in the wrong aiocontext, and the second set of drains won't even manage to schedule the coroutine, as job->busy would still be true from the previous job_resume_locked(). The solution is to stick with aio_co_wake(), but then detect every time the coroutine resumes back from yielding if job->aio_context has changed. If so, we can reschedule it to the new context. Check for the aiocontext change in job_do_yield_locked because: 1) aio_co_reschedule_self requires to be in the running coroutine 2) since child_job_set_aio_context allows changing the aiocontext only while the job is paused, this is the exact place where the coroutine resumes, before running JobDriver's code. As a small improvement, we don't need aio_co_enter() in job_start(), we can simplify it with qemu_coroutine_enter(). If the aio_context where it is running is wrong, job_do_yield_locked() will automatically reschedule the coroutine where it belongs, once the first job_pause_point is triggered in job_co_entry. Signed-off-by: Paolo Bonzini --- job.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/job.c b/job.c index 468a387390..bce76b21d8 100644 --- a/job.c +++ b/job.c @@ -532,11 +532,12 @@ void job_enter_cond_locked(Job *job, bool(*fn)(Job *job)) return; } - assert(!job->deferred_to_main_loop); timer_del(&job->sleep_timer); job->busy = true; real_job_unlock(); - aio_co_enter(job->aio_context, job->co); + job_unlock(); + aio_co_wake(job->co); + job_lock(); } void job_enter(Job *job) @@ -556,6 +557,8 @@ void job_enter(Job *job) */ static void coroutine_fn job_do_yield_locked(Job *job, uint64_t ns) { + AioContext *next_aio_context; + real_job_lock(); if (ns != -1) { timer_mod(&job->sleep_timer, ns); @@ -567,6 +570,20 @@ static void coroutine_fn job_do_yield_locked(Job *job, uint64_t ns) qemu_coroutine_yield(); job_lock(); + next_aio_context = job->aio_context; + /* + * Coroutine has resumed, but in the meanwhile the job AioContext + * might have changed via bdrv_try_set_aio_context(), so we need to move + * the coroutine too in the new aiocontext. + */ + while (qemu_get_current_aio_context() != next_aio_context) { + job_unlock(); + aio_co_reschedule_self(next_aio_context); + job_lock(); + next_aio_context = job->aio_context; + } + + /* Set by job_enter_cond_locked() before re-entering the coroutine. */ assert(job->busy); } @@ -666,7 +683,6 @@ void job_resume_locked(Job *job) if (job->pause_count) { return; } - /* kick only if no timer is pending */ job_enter_cond_locked(job, job_timer_not_pending); } @@ -1145,7 +1161,7 @@ void job_start(Job *job) job->paused = false; job_state_transition_locked(job, JOB_STATUS_RUNNING); } - aio_co_enter(job->aio_context, job->co); + qemu_coroutine_enter(job->co); } void job_cancel_locked(Job *job, bool force) From patchwork Fri Jan 28 11:50:29 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: 1585654 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=Qh7YgJKD; 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 4Jlbwm1p6fz9sRR for ; Fri, 28 Jan 2022 23:14:16 +1100 (AEDT) Received: from localhost ([::1]:58546 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDQ8r-0000Rg-OE for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:14:14 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60802) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmU-0004b3-QQ for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:07 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:46375) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmP-0003Rp-Vh for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:05 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370656; 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=+/YSnuKmiHOeC92swLxPOEUPIFkBnw/qLJ7GY6AQ7NM=; b=Qh7YgJKD7HeoqvrWIiUIBcURpbV1iRSsZe9peRjNnVlF5Ptx8HovYA3OEdZ3sGk3fXzisN cNU9mhReU/X272gQbEVz7gcGbJi+GXWmmRD4YpNEiGePh/F6JSuwrkggC/lhiAf0HhSy98 3k7DcJ1juSvoS96ZFSOyRUPJq9pSdGc= 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-25-MxMJUQe_NPGz4uiREUresw-1; Fri, 28 Jan 2022 06:50:55 -0500 X-MC-Unique: MxMJUQe_NPGz4uiREUresw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A30902F44; Fri, 28 Jan 2022 11:50:53 +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 AB5C41F478; Fri, 28 Jan 2022 11:50:52 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 17/19] jobs: protect job.aio_context with BQL and job_mutex Date: Fri, 28 Jan 2022 06:50:29 -0500 Message-Id: <20220128115031.4061565-18-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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.129.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" In order to make it thread safe, implement a "fake rwlock", where we allow reads under BQL *or* job_mutex held, but writes only under BQL *and* job_mutex. The only write we have is in child_job_set_aio_ctx, which always happens under drain (so the job is paused). For this reason, introduce job_set_aio_context and make sure that the context is set under BQL, job_mutex and drain. Also make sure all other places where the aiocontext is read are protected. Suggested-by: Paolo Bonzini Signed-off-by: Emanuele Giuseppe Esposito --- block/replication.c | 2 +- blockjob.c | 3 ++- include/qemu/job.h | 19 ++++++++++++++++++- job.c | 12 ++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/block/replication.c b/block/replication.c index 50ea778937..68018948b9 100644 --- a/block/replication.c +++ b/block/replication.c @@ -148,8 +148,8 @@ static void replication_close(BlockDriverState *bs) } if (s->stage == BLOCK_REPLICATION_FAILOVER) { commit_job = &s->commit_job->job; - assert(commit_job->aio_context == qemu_get_current_aio_context()); WITH_JOB_LOCK_GUARD() { + assert(commit_job->aio_context == qemu_get_current_aio_context()); job_cancel_sync_locked(commit_job, false); } } diff --git a/blockjob.c b/blockjob.c index 04d868f020..afb0e9ad5e 100644 --- a/blockjob.c +++ b/blockjob.c @@ -154,12 +154,13 @@ static void child_job_set_aio_ctx(BdrvChild *c, AioContext *ctx, bdrv_set_aio_context_ignore(sibling->bs, ctx, ignore); } - job->job.aio_context = ctx; + job_set_aio_context(&job->job, ctx); } static AioContext *child_job_get_parent_aio_context(BdrvChild *c) { BlockJob *job = c->opaque; + assert(qemu_in_main_thread()); return job->job.aio_context; } diff --git a/include/qemu/job.h b/include/qemu/job.h index fc57472e84..f335bac46c 100644 --- a/include/qemu/job.h +++ b/include/qemu/job.h @@ -75,7 +75,12 @@ typedef struct Job { ProgressMeter progress; - /** AioContext to run the job coroutine in */ + /** + * AioContext to run the job coroutine in. + * This field can be read when holding either the BQL (so we are in + * the main loop) or the job_mutex. + * Instead, it can be only written when we hold *both* BQL and job_mutex. + */ AioContext *aio_context; /** Reference count of the block job */ @@ -731,4 +736,16 @@ void job_dismiss_locked(Job **job, Error **errp); int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp), Error **errp); +/** + * Sets the @job->aio_context. + * Called with job_mutex *not* held. + * + * This function must run in the main thread to protect against + * concurrent read in job_finish_sync_locked(), + * takes the job_mutex lock to protect against the read in + * job_do_yield_locked(), and must be called when the coroutine + * is quiescent. + */ +void job_set_aio_context(Job *job, AioContext *ctx); + #endif diff --git a/job.c b/job.c index bce76b21d8..c4e0641bd4 100644 --- a/job.c +++ b/job.c @@ -357,6 +357,17 @@ Job *job_get_locked(const char *id) return NULL; } +void job_set_aio_context(Job *job, AioContext *ctx) +{ + /* protect against read in job_finish_sync_locked */ + assert(qemu_in_main_thread()); + /* protect against read in job_do_yield_locked */ + JOB_LOCK_GUARD(); + /* ensure the coroutine is quiescent while the AioContext is changed */ + assert(job->pause_count > 0); + job->aio_context = ctx; +} + /* Called with job_mutex *not* held. */ static void job_sleep_timer_cb(void *opaque) { @@ -1274,6 +1285,7 @@ int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp), { Error *local_err = NULL; int ret; + assert(qemu_in_main_thread()); job_ref_locked(job); From patchwork Fri Jan 28 11:50:30 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: 1585652 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=Pnt7+eaL; 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 4Jlbtb5H9dz9t56 for ; Fri, 28 Jan 2022 23:12:23 +1100 (AEDT) Received: from localhost ([::1]:56866 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDQ73-0007iP-Gm for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:12:21 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60842) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmV-0004c6-La for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:07 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:50407) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmP-0003SU-Vz for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370659; 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=dm7rJV9A7Q/y8dyzMuyYfUFqsk3VtpZVVHnICQncXNY=; b=Pnt7+eaLqQWhFsfrIqPzaeKWB99HAD3LVYqoR3l8IprI/Qt4/BulxFRUg4hHXUCQriSjCu YAZJ2VQsp0s3D3Ox3I5MlfLI4fzQeW0VIkQhmUWX9xLI4xs7ufNH7RFhe0K5sWJibOBawk yI38EqPuK13sw3hYbfVI0jEAr3wTpTI= 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-209-eCThsTTFPey1T2rlBYkoEQ-1; Fri, 28 Jan 2022 06:50:56 -0500 X-MC-Unique: eCThsTTFPey1T2rlBYkoEQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B36301006AAA; Fri, 28 Jan 2022 11:50:54 +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 BDF5722DE3; Fri, 28 Jan 2022 11:50:53 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 18/19] job.c: enable job lock/unlock and remove Aiocontext locks Date: Fri, 28 Jan 2022 06:50:30 -0500 Message-Id: <20220128115031.4061565-19-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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.129.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" Change the job_{lock/unlock} and macros to use job_mutex. Now that they are not nop anymore, remove the aiocontext to avoid deadlocks. Therefore: - when possible, remove completely the aiocontext lock/unlock pair - if it is used by some other functions too, reduce the locking section as much as possible, leaving the job API outside. There is only one JobDriver callback, ->free() that assumes that the aiocontext lock is held (because it calls bdrv_unref), so for now keep that under aiocontext lock. Also remove real_job_{lock/unlock}, as they are replaced by the public functions. Signed-off-by: Emanuele Giuseppe Esposito --- blockdev.c | 65 ++++----------------------- include/qemu/job.h | 22 ++++----- job-qmp.c | 41 ++++------------- job.c | 76 +++----------------------------- tests/unit/test-bdrv-drain.c | 4 +- tests/unit/test-block-iothread.c | 2 +- tests/unit/test-blockjob.c | 13 ++---- 7 files changed, 38 insertions(+), 185 deletions(-) diff --git a/blockdev.c b/blockdev.c index d6693593b0..203e5ff770 100644 --- a/blockdev.c +++ b/blockdev.c @@ -155,12 +155,7 @@ void blockdev_mark_auto_del(BlockBackend *blk) for (job = block_job_next_locked(NULL); job; job = block_job_next_locked(job)) { if (block_job_has_bdrv(job, blk_bs(blk))) { - AioContext *aio_context = job->job.aio_context; - aio_context_acquire(aio_context); - job_cancel_locked(&job->job, false); - - aio_context_release(aio_context); } } @@ -1836,16 +1831,9 @@ static void drive_backup_abort(BlkActionState *common) DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); if (state->job) { - AioContext *aio_context; - - aio_context = bdrv_get_aio_context(state->bs); - aio_context_acquire(aio_context); - WITH_JOB_LOCK_GUARD() { job_cancel_sync_locked(&state->job->job, true); } - - aio_context_release(aio_context); } } @@ -1939,16 +1927,9 @@ static void blockdev_backup_abort(BlkActionState *common) BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); if (state->job) { - AioContext *aio_context; - - aio_context = bdrv_get_aio_context(state->bs); - aio_context_acquire(aio_context); - WITH_JOB_LOCK_GUARD() { job_cancel_sync_locked(&state->job->job, true); } - - aio_context_release(aio_context); } } @@ -3312,18 +3293,13 @@ out: aio_context_release(aio_context); } -/* - * Get a block job using its ID and acquire its AioContext. - * Returns with job_lock held on success. - */ -static BlockJob *find_block_job(const char *id, AioContext **aio_context, - Error **errp) +/* Get a block job using its ID. Returns with job_lock held on success */ +static BlockJob *find_block_job(const char *id, Error **errp) { BlockJob *job; assert(id != NULL); - *aio_context = NULL; job_lock(); job = block_job_get_locked(id); @@ -3335,31 +3311,25 @@ static BlockJob *find_block_job(const char *id, AioContext **aio_context, return NULL; } - *aio_context = block_job_get_aio_context(job); - aio_context_acquire(*aio_context); - return job; } void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) { - AioContext *aio_context; - BlockJob *job = find_block_job(device, &aio_context, errp); + BlockJob *job = find_block_job(device, errp); if (!job) { return; } block_job_set_speed_locked(job, speed, errp); - aio_context_release(aio_context); job_unlock(); } void qmp_block_job_cancel(const char *device, bool has_force, bool force, Error **errp) { - AioContext *aio_context; - BlockJob *job = find_block_job(device, &aio_context, errp); + BlockJob *job = find_block_job(device, errp); if (!job) { return; @@ -3378,14 +3348,12 @@ void qmp_block_job_cancel(const char *device, trace_qmp_block_job_cancel(job); job_user_cancel_locked(&job->job, force, errp); out: - aio_context_release(aio_context); job_unlock(); } void qmp_block_job_pause(const char *device, Error **errp) { - AioContext *aio_context; - BlockJob *job = find_block_job(device, &aio_context, errp); + BlockJob *job = find_block_job(device, errp); if (!job) { return; @@ -3393,14 +3361,12 @@ void qmp_block_job_pause(const char *device, Error **errp) trace_qmp_block_job_pause(job); job_user_pause_locked(&job->job, errp); - aio_context_release(aio_context); job_unlock(); } void qmp_block_job_resume(const char *device, Error **errp) { - AioContext *aio_context; - BlockJob *job = find_block_job(device, &aio_context, errp); + BlockJob *job = find_block_job(device, errp); if (!job) { return; @@ -3408,14 +3374,12 @@ void qmp_block_job_resume(const char *device, Error **errp) trace_qmp_block_job_resume(job); job_user_resume_locked(&job->job, errp); - aio_context_release(aio_context); job_unlock(); } void qmp_block_job_complete(const char *device, Error **errp) { - AioContext *aio_context; - BlockJob *job = find_block_job(device, &aio_context, errp); + BlockJob *job = find_block_job(device, errp); if (!job) { return; @@ -3423,14 +3387,12 @@ void qmp_block_job_complete(const char *device, Error **errp) trace_qmp_block_job_complete(job); job_complete_locked(&job->job, errp); - aio_context_release(aio_context); job_unlock(); } void qmp_block_job_finalize(const char *id, Error **errp) { - AioContext *aio_context; - BlockJob *job = find_block_job(id, &aio_context, errp); + BlockJob *job = find_block_job(id, errp); if (!job) { return; @@ -3440,21 +3402,13 @@ void qmp_block_job_finalize(const char *id, Error **errp) job_ref_locked(&job->job); job_finalize_locked(&job->job, errp); - /* - * Job's context might have changed via job_finalize_locked - * (and job_txn_apply automatically acquires the new one), - * so make sure we release the correct one. - */ - aio_context = block_job_get_aio_context(job); job_unref_locked(&job->job); - aio_context_release(aio_context); job_unlock(); } void qmp_block_job_dismiss(const char *id, Error **errp) { - AioContext *aio_context; - BlockJob *bjob = find_block_job(id, &aio_context, errp); + BlockJob *bjob = find_block_job(id, errp); Job *job; if (!bjob) { @@ -3464,7 +3418,6 @@ void qmp_block_job_dismiss(const char *id, Error **errp) trace_qmp_block_job_dismiss(bjob); job = &bjob->job; job_dismiss_locked(&job, errp); - aio_context_release(aio_context); job_unlock(); } diff --git a/include/qemu/job.h b/include/qemu/job.h index f335bac46c..7f9e6ee13e 100644 --- a/include/qemu/job.h +++ b/include/qemu/job.h @@ -75,11 +75,14 @@ typedef struct Job { ProgressMeter progress; + /** Protected by job_mutex */ + /** * AioContext to run the job coroutine in. - * This field can be read when holding either the BQL (so we are in - * the main loop) or the job_mutex. - * Instead, it can be only written when we hold *both* BQL and job_mutex. + * The job Aiocontext can be read when holding *either* + * the BQL (so we are in the main loop) or the job_mutex. + * Instead, it can only be written when we hold *both* BQL + * and the job_mutex. */ AioContext *aio_context; @@ -104,7 +107,7 @@ typedef struct Job { /** * Set to false by the job while the coroutine has yielded and may be * re-entered by job_enter(). There may still be I/O or event loop activity - * pending. Accessed under block_job_mutex (in blockjob.c). + * pending. Accessed under job_mutex. * * When the job is deferred to the main loop, busy is true as long as the * bottom half is still pending. @@ -329,9 +332,9 @@ typedef enum JobCreateFlags { extern QemuMutex job_mutex; -#define JOB_LOCK_GUARD() /* QEMU_LOCK_GUARD(&job_mutex) */ +#define JOB_LOCK_GUARD() QEMU_LOCK_GUARD(&job_mutex) -#define WITH_JOB_LOCK_GUARD() /* WITH_QEMU_LOCK_GUARD(&job_mutex) */ +#define WITH_JOB_LOCK_GUARD() WITH_QEMU_LOCK_GUARD(&job_mutex) /** * job_lock: @@ -670,8 +673,6 @@ void job_user_cancel_locked(Job *job, bool force, Error **errp); * * Returns the return value from the job if the job actually completed * during the call, or -ECANCELED if it was canceled. - * - * Callers must hold the AioContext lock of job->aio_context. */ int job_cancel_sync_locked(Job *job, bool force); @@ -695,9 +696,6 @@ void job_cancel_sync_all(void); * function). * * Returns the return value from the job. - * - * Callers must hold the AioContext lock of job->aio_context. - * * Called between job_lock and job_unlock. */ int job_complete_sync_locked(Job *job, Error **errp); @@ -729,8 +727,6 @@ void job_dismiss_locked(Job **job, Error **errp); * Returns 0 if the job is successfully completed, -ECANCELED if the job was * cancelled before completing, and -errno in other error cases. * - * Callers must hold the AioContext lock of job->aio_context. - * * Called between job_lock and job_unlock. */ int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp), diff --git a/job-qmp.c b/job-qmp.c index 615e056fc4..858b3a28f5 100644 --- a/job-qmp.c +++ b/job-qmp.c @@ -29,15 +29,11 @@ #include "qapi/error.h" #include "trace/trace-root.h" -/* - * Get a block job using its ID and acquire its AioContext. - * Returns with job_lock held on success. - */ -static Job *find_job(const char *id, AioContext **aio_context, Error **errp) +/* Get a job using its ID. Returns with job_lock held on success. */ +static Job *find_job(const char *id, Error **errp) { Job *job; - *aio_context = NULL; job_lock(); job = job_get_locked(id); @@ -47,16 +43,12 @@ static Job *find_job(const char *id, AioContext **aio_context, Error **errp) return NULL; } - *aio_context = job->aio_context; - aio_context_acquire(*aio_context); - return job; } void qmp_job_cancel(const char *id, Error **errp) { - AioContext *aio_context; - Job *job = find_job(id, &aio_context, errp); + Job *job = find_job(id, errp); if (!job) { return; @@ -64,14 +56,12 @@ void qmp_job_cancel(const char *id, Error **errp) trace_qmp_job_cancel(job); job_user_cancel_locked(job, true, errp); - aio_context_release(aio_context); job_unlock(); } void qmp_job_pause(const char *id, Error **errp) { - AioContext *aio_context; - Job *job = find_job(id, &aio_context, errp); + Job *job = find_job(id, errp); if (!job) { return; @@ -79,14 +69,12 @@ void qmp_job_pause(const char *id, Error **errp) trace_qmp_job_pause(job); job_user_pause_locked(job, errp); - aio_context_release(aio_context); job_unlock(); } void qmp_job_resume(const char *id, Error **errp) { - AioContext *aio_context; - Job *job = find_job(id, &aio_context, errp); + Job *job = find_job(id, errp); if (!job) { return; @@ -94,14 +82,12 @@ void qmp_job_resume(const char *id, Error **errp) trace_qmp_job_resume(job); job_user_resume_locked(job, errp); - aio_context_release(aio_context); job_unlock(); } void qmp_job_complete(const char *id, Error **errp) { - AioContext *aio_context; - Job *job = find_job(id, &aio_context, errp); + Job *job = find_job(id, errp); if (!job) { return; @@ -109,14 +95,12 @@ void qmp_job_complete(const char *id, Error **errp) trace_qmp_job_complete(job); job_complete_locked(job, errp); - aio_context_release(aio_context); job_unlock(); } void qmp_job_finalize(const char *id, Error **errp) { - AioContext *aio_context; - Job *job = find_job(id, &aio_context, errp); + Job *job = find_job(id, errp); if (!job) { return; @@ -126,21 +110,13 @@ void qmp_job_finalize(const char *id, Error **errp) job_ref_locked(job); job_finalize_locked(job, errp); - /* - * Job's context might have changed via job_finalize_locked - * (and job_txn_apply automatically acquires the new one), - * so make sure we release the correct one. - */ - aio_context = job->aio_context; job_unref_locked(job); - aio_context_release(aio_context); job_unlock(); } void qmp_job_dismiss(const char *id, Error **errp) { - AioContext *aio_context; - Job *job = find_job(id, &aio_context, errp); + Job *job = find_job(id, errp); if (!job) { return; @@ -148,7 +124,6 @@ void qmp_job_dismiss(const char *id, Error **errp) trace_qmp_job_dismiss(job); job_dismiss_locked(&job, errp); - aio_context_release(aio_context); job_unlock(); } diff --git a/job.c b/job.c index c4e0641bd4..be59fe7fd3 100644 --- a/job.c +++ b/job.c @@ -98,21 +98,11 @@ struct JobTxn { }; void job_lock(void) -{ - /* nop */ -} - -void job_unlock(void) -{ - /* nop */ -} - -static void real_job_lock(void) { qemu_mutex_lock(&job_mutex); } -static void real_job_unlock(void) +void job_unlock(void) { qemu_mutex_unlock(&job_mutex); } @@ -180,7 +170,6 @@ static int job_txn_apply_locked(Job *job, int fn(Job *)) * twice - which would break AIO_WAIT_WHILE from within fn. */ job_ref_locked(job); - aio_context_release(job->aio_context); QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) { rc = fn(other_job); @@ -189,11 +178,6 @@ static int job_txn_apply_locked(Job *job, int fn(Job *)) } } - /* - * Note that job->aio_context might have been changed by calling fn, so we - * can't use a local variable to cache it. - */ - aio_context_acquire(job->aio_context); job_unref_locked(job); return rc; } @@ -459,7 +443,10 @@ void job_unref_locked(Job *job) if (job->driver->free) { job_unlock(); + /* FIXME: aiocontext lock is required because cb calls blk_unref */ + aio_context_acquire(job->aio_context); job->driver->free(job); + aio_context_release(job->aio_context); job_lock(); } @@ -532,20 +519,16 @@ void job_enter_cond_locked(Job *job, bool(*fn)(Job *job)) return; } - real_job_lock(); if (job->busy) { - real_job_unlock(); return; } if (fn && !fn(job)) { - real_job_unlock(); return; } timer_del(&job->sleep_timer); job->busy = true; - real_job_unlock(); job_unlock(); aio_co_wake(job->co); job_lock(); @@ -570,13 +553,11 @@ static void coroutine_fn job_do_yield_locked(Job *job, uint64_t ns) { AioContext *next_aio_context; - real_job_lock(); if (ns != -1) { timer_mod(&job->sleep_timer, ns); } job->busy = false; job_event_idle_locked(job); - real_job_unlock(); job_unlock(); qemu_coroutine_yield(); job_lock(); @@ -918,7 +899,6 @@ static void job_cancel_async_locked(Job *job, bool force) /* Called with job_mutex held. */ static void job_completed_txn_abort_locked(Job *job) { - AioContext *ctx; JobTxn *txn = job->txn; Job *other_job; @@ -931,54 +911,28 @@ static void job_completed_txn_abort_locked(Job *job) txn->aborting = true; job_txn_ref_locked(txn); - /* - * We can only hold the single job's AioContext lock while calling - * job_finalize_single_locked() because the finalization callbacks can - * involve calls of AIO_WAIT_WHILE(), which could deadlock otherwise. - * Note that the job's AioContext may change when it is finalized. - */ - job_ref_locked(job); - aio_context_release(job->aio_context); - /* Other jobs are effectively cancelled by us, set the status for * them; this job, however, may or may not be cancelled, depending * on the caller, so leave it. */ QLIST_FOREACH(other_job, &txn->jobs, txn_list) { if (other_job != job) { - ctx = other_job->aio_context; - aio_context_acquire(ctx); /* * This is a transaction: If one job failed, no result will matter. * Therefore, pass force=true to terminate all other jobs as * quickly as possible. */ job_cancel_async_locked(other_job, true); - aio_context_release(ctx); } } while (!QLIST_EMPTY(&txn->jobs)) { other_job = QLIST_FIRST(&txn->jobs); - /* - * The job's AioContext may change, so store it in @ctx so we - * release the same context that we have acquired before. - */ - ctx = other_job->aio_context; - aio_context_acquire(ctx); if (!job_is_completed_locked(other_job)) { assert(job_cancel_requested_locked(other_job)); job_finish_sync_locked(other_job, NULL, NULL); } - aio_context_release(ctx); job_finalize_single_locked(other_job); } - /* - * Use job_ref_locked()/job_unref_locked() so we can read the AioContext - * here even if the job went away during job_finalize_single_locked(). - */ - aio_context_acquire(job->aio_context); - job_unref_locked(job); - job_txn_unref_locked(txn); } @@ -1097,11 +1051,7 @@ static void job_completed_locked(Job *job) static void job_exit(void *opaque) { Job *job = (Job *)opaque; - AioContext *ctx; - JOB_LOCK_GUARD(); - job_ref_locked(job); - aio_context_acquire(job->aio_context); /* This is a lie, we're not quiescent, but still doing the completion * callbacks. However, completion callbacks tend to involve operations that @@ -1111,16 +1061,6 @@ static void job_exit(void *opaque) job_event_idle_locked(job); job_completed_locked(job); - - /* - * Note that calling job_completed_locked can move the job to a different - * aio_context, so we cannot cache from above. job_txn_apply_locked takes - * care of acquiring the new lock, and we ref/unref to avoid - * job_completed_locked freeing the job underneath us. - */ - ctx = job->aio_context; - job_unref_locked(job); - aio_context_release(ctx); } /** @@ -1245,14 +1185,10 @@ int job_cancel_sync_locked(Job *job, bool force) void job_cancel_sync_all(void) { Job *job; - AioContext *aio_context; JOB_LOCK_GUARD(); while ((job = job_next_locked(NULL))) { - aio_context = job->aio_context; - aio_context_acquire(aio_context); job_cancel_sync_locked(job, true); - aio_context_release(aio_context); } } @@ -1299,8 +1235,8 @@ int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp), } job_unlock(); - AIO_WAIT_WHILE(job->aio_context, - (job_enter(job), !job_is_completed(job))); + AIO_WAIT_WHILE_UNLOCKED(job->aio_context, + (job_enter(job), !job_is_completed(job))); job_lock(); ret = (job_is_cancelled_locked(job) && job->ret == 0) ? diff --git a/tests/unit/test-bdrv-drain.c b/tests/unit/test-bdrv-drain.c index 0db056ea63..4924ceb562 100644 --- a/tests/unit/test-bdrv-drain.c +++ b/tests/unit/test-bdrv-drain.c @@ -930,9 +930,9 @@ static void test_blockjob_common_drain_node(enum drain_type drain_type, tjob->prepare_ret = -EIO; break; } + aio_context_release(ctx); job_start(&job->job); - aio_context_release(ctx); if (use_iothread) { /* job_co_entry() is run in the I/O thread, wait for the actual job @@ -1016,12 +1016,12 @@ static void test_blockjob_common_drain_node(enum drain_type drain_type, g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */ } - aio_context_acquire(ctx); WITH_JOB_LOCK_GUARD() { ret = job_complete_sync_locked(&job->job, &error_abort); } g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO)); + aio_context_acquire(ctx); if (use_iothread) { blk_set_aio_context(blk_src, qemu_get_aio_context(), &error_abort); assert(blk_get_aio_context(blk_target) == qemu_get_aio_context()); diff --git a/tests/unit/test-block-iothread.c b/tests/unit/test-block-iothread.c index 89e7f0fffb..9d7c8be00f 100644 --- a/tests/unit/test-block-iothread.c +++ b/tests/unit/test-block-iothread.c @@ -455,10 +455,10 @@ static void test_attach_blockjob(void) aio_poll(qemu_get_aio_context(), false); } - aio_context_acquire(ctx); WITH_JOB_LOCK_GUARD() { job_complete_sync_locked(&tjob->common.job, &error_abort); } + aio_context_acquire(ctx); blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort); aio_context_release(ctx); diff --git a/tests/unit/test-blockjob.c b/tests/unit/test-blockjob.c index 8280b1e0c9..d6fc52f80a 100644 --- a/tests/unit/test-blockjob.c +++ b/tests/unit/test-blockjob.c @@ -228,10 +228,6 @@ static void cancel_common(CancelJob *s) BlockJob *job = &s->common; BlockBackend *blk = s->blk; JobStatus sts = job->job.status; - AioContext *ctx; - - ctx = job->job.aio_context; - aio_context_acquire(ctx); WITH_JOB_LOCK_GUARD() { job_cancel_sync_locked(&job->job, true); @@ -244,7 +240,6 @@ static void cancel_common(CancelJob *s) } destroy_blk(blk); - aio_context_release(ctx); } static void test_cancel_created(void) @@ -405,11 +400,9 @@ static void test_cancel_concluded(void) aio_poll(qemu_get_aio_context(), true); assert(job->status == JOB_STATUS_PENDING); - aio_context_acquire(job->aio_context); WITH_JOB_LOCK_GUARD() { job_finalize_locked(job, &error_abort); } - aio_context_release(job->aio_context); assert(job->status == JOB_STATUS_CONCLUDED); cancel_common(s); @@ -503,13 +496,11 @@ static void test_complete_in_standby(void) /* Wait for the job to become READY */ job_start(job); - aio_context_acquire(ctx); /* * Here we are waiting for the status to change, so don't bother * protecting the read every time. */ - AIO_WAIT_WHILE(ctx, job->status != JOB_STATUS_READY); - aio_context_release(ctx); + AIO_WAIT_WHILE_UNLOCKED(ctx, job->status != JOB_STATUS_READY); /* Begin the drained section, pausing the job */ bdrv_drain_all_begin(); @@ -529,6 +520,7 @@ static void test_complete_in_standby(void) job_complete_locked(job, &error_abort); /* The test is done now, clean up. */ + aio_context_release(ctx); job_finish_sync_locked(job, NULL, &error_abort); assert(job->status == JOB_STATUS_PENDING); @@ -538,6 +530,7 @@ static void test_complete_in_standby(void) job_dismiss_locked(&job, &error_abort); } + aio_context_acquire(ctx); destroy_blk(blk); aio_context_release(ctx); iothread_join(iothread); From patchwork Fri Jan 28 11:50:31 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: 1585676 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=Hze42rgq; 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 4Jlchp6DGBz9t56 for ; Fri, 28 Jan 2022 23:48:57 +1100 (AEDT) Received: from localhost ([::1]:36462 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nDQgI-000155-5G for incoming@patchwork.ozlabs.org; Fri, 28 Jan 2022 07:48:47 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60878) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmW-0004g6-PD for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:09 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:50996) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nDPmQ-0003Sp-2G for qemu-devel@nongnu.org; Fri, 28 Jan 2022 06:51:08 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643370660; 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=pi2sh+AgeO3EvJDWiCygbpS7HupSLZzAr/YQChAyZLc=; b=Hze42rgqbUXin+YCv9WqyDu6nCiGeU+1COgOjwBNG0bcyea/3CZFVynDYJb/Lmeopr43lJ 0YoDJsnSuPEznJwKWQHBLKWZ5TOoNEO0BWGQ8uKJ+BbUV9S+q6ZCPqRadBA6sajIjVHRno +rdTGWS4cywKY5UIiQkWGx4Wmo8tBJk= 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-588-c7lK8aYtNBqGXvY-a448tQ-1; Fri, 28 Jan 2022 06:50:57 -0500 X-MC-Unique: c7lK8aYtNBqGXvY-a448tQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CC77583DEA6; Fri, 28 Jan 2022 11:50:55 +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 CDB601FD88; Fri, 28 Jan 2022 11:50:54 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v4 19/19] block_job_query: remove atomic read Date: Fri, 28 Jan 2022 06:50:31 -0500 Message-Id: <20220128115031.4061565-20-eesposit@redhat.com> In-Reply-To: <20220128115031.4061565-1-eesposit@redhat.com> References: <20220128115031.4061565-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, 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_H2=-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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Emanuele Giuseppe Esposito , Markus Armbruster , 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" Not sure what the atomic here was supposed to do, since job.busy is protected by the job lock. Since the whole function is called under job_mutex, just remove the atomic. Signed-off-by: Emanuele Giuseppe Esposito --- blockjob.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockjob.c b/blockjob.c index afb0e9ad5e..e7ed4f545d 100644 --- a/blockjob.c +++ b/blockjob.c @@ -329,7 +329,7 @@ BlockJobInfo *block_job_query_locked(BlockJob *job, Error **errp) info = g_new0(BlockJobInfo, 1); info->type = g_strdup(job_type_str(&job->job)); info->device = g_strdup(job->job.id); - info->busy = qatomic_read(&job->job.busy); + info->busy = job->job.busy; info->paused = job->job.pause_count > 0; info->offset = progress_current; info->len = progress_total;