From nobody Sun Feb 8 04:38:36 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1655385899; cv=none; d=zohomail.com; s=zohoarc; b=MgqllvU685TNzJm7qpud+BP+tG4g1F7r5D8lUGkbOkbO1g8LH10kHaZH4l5+swYFPpUzZOvm+NBCIC1nROxlQpouUloEW48WsFnrhM07PGVVMIUqX7b9LWzDowFgHv/gzU3k59/bxt+ED6/Bv7tPx6vOwWny4EwI7jGEqEP8JOs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655385899; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=dbdGNURLbLyvKL4YsERT1gmauqtRqZqFUH+yWodtyOY=; b=lOC9rlE44PgYzbgJn8FHNTtggOqt6KWpupRnjmp4K9UU9+CXm+5tjYmEpxE3x+lIarMpO7b7W7tjfiUKWXj0C44Ue9dCVkVV/0qd77hNwLCaYSflefCN360CWoksJZqHQvh/xreBgzdk12kbCcHLjivF/69sTzaftvisBoOIKKo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 165538589909721.24426679368389; Thu, 16 Jun 2022 06:24:59 -0700 (PDT) Received: from localhost ([::1]:53446 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o1pUX-0006AA-Q7 for importer@patchew.org; Thu, 16 Jun 2022 09:24:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51646) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o1pOb-0005Dl-Oq for qemu-devel@nongnu.org; Thu, 16 Jun 2022 09:18:49 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:50579) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o1pOX-0007Xs-UN for qemu-devel@nongnu.org; Thu, 16 Jun 2022 09:18:49 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-330-4oBoLHMMN9W6hkamZ_cLJg-1; Thu, 16 Jun 2022 09:18:40 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 08126185A7BA; Thu, 16 Jun 2022 13:18: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 A959540334E; Thu, 16 Jun 2022 13:18:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655385523; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dbdGNURLbLyvKL4YsERT1gmauqtRqZqFUH+yWodtyOY=; b=Uu7x+/khNbbhMrmRQ+/2MdkeF0upJr3NdRtccnd0wGFZ1jDHow870j1CYpL68W274fwYSl 9uRsnpo/j0QTSHtm71rR+76/LbwPm9cFULZ5yjKKG6pEfEgbxNjIGGSP8RAFtZEPe7DKC7 CPjYdt5TIyNA8bCmGw++a7t6NGLMUOo= X-MC-Unique: 4oBoLHMMN9W6hkamZ_cLJg-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Cc: Kevin Wolf , Hanna Reitz , Paolo Bonzini , John Snow , Vladimir Sementsov-Ogievskiy , Wen Congyang , Xie Changlong , Markus Armbruster , Stefan Hajnoczi , Fam Zheng , qemu-devel@nongnu.org, Emanuele Giuseppe Esposito Subject: [PATCH v7 07/18] jobs: add job lock in find_* functions Date: Thu, 16 Jun 2022 09:18:24 -0400 Message-Id: <20220616131835.2004262-8-eesposit@redhat.com> In-Reply-To: <20220616131835.2004262-1-eesposit@redhat.com> References: <20220616131835.2004262-1-eesposit@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1655385899404100002 Content-Type: text/plain; charset="utf-8" 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 | 46 +++++++++++++++++++++++++++++++++++----------- job-qmp.c | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/blockdev.c b/blockdev.c index b1099e678c..6f83783f10 100644 --- a/blockdev.c +++ b/blockdev.c @@ -3311,9 +3311,13 @@ out: aio_context_release(aio_context); } =20 -/* Get a block job using its ID and acquire its AioContext */ -static BlockJob *find_block_job(const char *id, AioContext **aio_context, - Error **errp) +/* + * Get a block job using its ID and acquire its AioContext. + * Called with job_mutex held. + */ +static BlockJob *find_block_job_locked(const char *id, + AioContext **aio_context, + Error **errp) { BlockJob *job; =20 @@ -3322,7 +3326,6 @@ static BlockJob *find_block_job(const char *id, AioCo= ntext **aio_context, *aio_context =3D NULL; =20 job =3D block_job_get(id); - if (!job) { error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, "Block job '%s' not found", id); @@ -3338,7 +3341,10 @@ static BlockJob *find_block_job(const char *id, AioC= ontext **aio_context, void qmp_block_job_set_speed(const char *device, int64_t speed, Error **er= rp) { AioContext *aio_context; - BlockJob *job =3D find_block_job(device, &aio_context, errp); + BlockJob *job; + + JOB_LOCK_GUARD(); + job =3D find_block_job_locked(device, &aio_context, errp); =20 if (!job) { return; @@ -3352,7 +3358,10 @@ void qmp_block_job_cancel(const char *device, bool has_force, bool force, Error **errp) { AioContext *aio_context; - BlockJob *job =3D find_block_job(device, &aio_context, errp); + BlockJob *job; + + JOB_LOCK_GUARD(); + job =3D find_block_job_locked(device, &aio_context, errp); =20 if (!job) { return; @@ -3377,7 +3386,10 @@ out: void qmp_block_job_pause(const char *device, Error **errp) { AioContext *aio_context; - BlockJob *job =3D find_block_job(device, &aio_context, errp); + BlockJob *job; + + JOB_LOCK_GUARD(); + job =3D find_block_job_locked(device, &aio_context, errp); =20 if (!job) { return; @@ -3391,7 +3403,10 @@ void qmp_block_job_pause(const char *device, Error *= *errp) void qmp_block_job_resume(const char *device, Error **errp) { AioContext *aio_context; - BlockJob *job =3D find_block_job(device, &aio_context, errp); + BlockJob *job; + + JOB_LOCK_GUARD(); + job =3D find_block_job_locked(device, &aio_context, errp); =20 if (!job) { return; @@ -3405,7 +3420,10 @@ void qmp_block_job_resume(const char *device, Error = **errp) void qmp_block_job_complete(const char *device, Error **errp) { AioContext *aio_context; - BlockJob *job =3D find_block_job(device, &aio_context, errp); + BlockJob *job; + + JOB_LOCK_GUARD(); + job =3D find_block_job_locked(device, &aio_context, errp); =20 if (!job) { return; @@ -3419,7 +3437,10 @@ void qmp_block_job_complete(const char *device, Erro= r **errp) void qmp_block_job_finalize(const char *id, Error **errp) { AioContext *aio_context; - BlockJob *job =3D find_block_job(id, &aio_context, errp); + BlockJob *job; + + JOB_LOCK_GUARD(); + job =3D find_block_job_locked(id, &aio_context, errp); =20 if (!job) { return; @@ -3442,9 +3463,12 @@ void qmp_block_job_finalize(const char *id, Error **= errp) void qmp_block_job_dismiss(const char *id, Error **errp) { AioContext *aio_context; - BlockJob *bjob =3D find_block_job(id, &aio_context, errp); + BlockJob *bjob; Job *job; =20 + JOB_LOCK_GUARD(); + bjob =3D find_block_job_locked(id, &aio_context, errp); + if (!bjob) { return; } diff --git a/job-qmp.c b/job-qmp.c index 270df1eb7e..58ca9b6632 100644 --- a/job-qmp.c +++ b/job-qmp.c @@ -29,8 +29,11 @@ #include "qapi/error.h" #include "trace/trace-root.h" =20 -/* Get a job using its ID and acquire its AioContext */ -static Job *find_job(const char *id, AioContext **aio_context, Error **err= p) +/* + * Get a block job using its ID and acquire its AioContext. + * Called with job_mutex held. + */ +static Job *find_job_locked(const char *id, AioContext **aio_context, Erro= r **errp) { Job *job; =20 @@ -51,7 +54,10 @@ static Job *find_job(const char *id, AioContext **aio_co= ntext, Error **errp) void qmp_job_cancel(const char *id, Error **errp) { AioContext *aio_context; - Job *job =3D find_job(id, &aio_context, errp); + Job *job; + + JOB_LOCK_GUARD(); + job =3D find_job_locked(id, &aio_context, errp); =20 if (!job) { return; @@ -65,7 +71,10 @@ void qmp_job_cancel(const char *id, Error **errp) void qmp_job_pause(const char *id, Error **errp) { AioContext *aio_context; - Job *job =3D find_job(id, &aio_context, errp); + Job *job; + + JOB_LOCK_GUARD(); + job =3D find_job_locked(id, &aio_context, errp); =20 if (!job) { return; @@ -79,7 +88,10 @@ void qmp_job_pause(const char *id, Error **errp) void qmp_job_resume(const char *id, Error **errp) { AioContext *aio_context; - Job *job =3D find_job(id, &aio_context, errp); + Job *job; + + JOB_LOCK_GUARD(); + job =3D find_job_locked(id, &aio_context, errp); =20 if (!job) { return; @@ -93,7 +105,10 @@ void qmp_job_resume(const char *id, Error **errp) void qmp_job_complete(const char *id, Error **errp) { AioContext *aio_context; - Job *job =3D find_job(id, &aio_context, errp); + Job *job; + + JOB_LOCK_GUARD(); + job =3D find_job_locked(id, &aio_context, errp); =20 if (!job) { return; @@ -107,7 +122,10 @@ void qmp_job_complete(const char *id, Error **errp) void qmp_job_finalize(const char *id, Error **errp) { AioContext *aio_context; - Job *job =3D find_job(id, &aio_context, errp); + Job *job; + + JOB_LOCK_GUARD(); + job =3D find_job_locked(id, &aio_context, errp); =20 if (!job) { return; @@ -130,7 +148,10 @@ void qmp_job_finalize(const char *id, Error **errp) void qmp_job_dismiss(const char *id, Error **errp) { AioContext *aio_context; - Job *job =3D find_job(id, &aio_context, errp); + Job *job; + + JOB_LOCK_GUARD(); + job =3D find_job_locked(id, &aio_context, errp); =20 if (!job) { return; --=20 2.31.1