From nobody Mon Apr 29 00:49:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620637357; cv=none; d=zohomail.com; s=zohoarc; b=Pmlv9NvJrHdBQKZIBny01j+wsHYM4WqaJmxkIEZvHqxQyOg5ZTAn6sU+88gqhedhltkm7tbunmu0WfaPkRETkC3mmkN7E6Le1IyQmhp10nFHYgqbq5mUqGm8Ski6HdgGnyMAJdUGI6JiKL7JImAYft1apzK+evM4yaAACRDEqjk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620637357; h=Content-Type: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=71j0q2fNtlHCJ9PQYUaCJ1Q3f0+hrP1j+qWjuLpkOUc=; b=c7vtV5SM4CADo8wT/5TMiMLIEayl+IXjCiokZ+Q9FhOsyJ6AYapnI9WT334GE0ei64bSD0OmzaWMCBi2G2P1We2AhVgpXfy6uuS07m68gJRpb1I1MdU9CzSMa06K6ZM6rTyXh8FqO1HDW2VbvVu7F+VZkIW3h/PHbpxHRTAxWho= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1620637357158880.501686413612; Mon, 10 May 2021 02:02:37 -0700 (PDT) Received: from localhost ([::1]:51914 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lg1oB-0006SI-KF for importer@patchew.org; Mon, 10 May 2021 05:02:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55976) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lg1lc-0003B2-CJ for qemu-devel@nongnu.org; Mon, 10 May 2021 04:59:56 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:35339) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lg1la-0005da-Sc for qemu-devel@nongnu.org; Mon, 10 May 2021 04:59:56 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-564-_-JSVOsHMaOQd1RyDFzDuw-1; Mon, 10 May 2021 04:59:52 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 488A8801107; Mon, 10 May 2021 08:59:51 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-113-184.ams2.redhat.com [10.36.113.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id 27D876267D; Mon, 10 May 2021 08:59:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620637194; 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=71j0q2fNtlHCJ9PQYUaCJ1Q3f0+hrP1j+qWjuLpkOUc=; b=CoWJRVnXCS5/G+L30olcacE6xDuoSZ3kQmja/nCCW3U6vFmnCxup/y4CGvBFscxiQyst0R 7olBpoooml5Xq9rHVnXayNlvII7hJq+yIq0rsoczuIToRYNetK93FE4l6xbqmFVCRfJTei pBO8p0GRZ1peluUGT++nys9/u17k9PM= X-MC-Unique: _-JSVOsHMaOQd1RyDFzDuw-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH 1/6] ratelimit: treat zero speed as unlimited Date: Mon, 10 May 2021 10:59:36 +0200 Message-Id: <20210510085941.22769-2-eesposit@redhat.com> In-Reply-To: <20210510085941.22769-1-eesposit@redhat.com> References: <20210510085941.22769-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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 Content-Transfer-Encoding: quoted-printable 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: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.699, 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_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Paolo Bonzini Both users of RateLimit, block-copy.c and blockjob.c, treat a speed of zero as unlimited, while RateLimit treats it as "as slow as possible". The latter is nicer from the code point of view but pretty useless, so disable rate limiting if a speed of zero is provided. Signed-off-by: Paolo Bonzini Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Vladimir Sementsov-Ogievskiy --- include/qemu/ratelimit.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/qemu/ratelimit.h b/include/qemu/ratelimit.h index 003ea6d5a3..48bf59e857 100644 --- a/include/qemu/ratelimit.h +++ b/include/qemu/ratelimit.h @@ -43,7 +43,11 @@ static inline int64_t ratelimit_calculate_delay(RateLimi= t *limit, uint64_t n) double delay_slices; =20 QEMU_LOCK_GUARD(&limit->lock); - assert(limit->slice_quota && limit->slice_ns); + if (!limit->slice_quota) { + /* Throttling disabled. */ + return 0; + } + assert(limit->slice_ns); =20 if (limit->slice_end_time < now) { /* Previous, possibly extended, time slice finished; reset the @@ -83,7 +87,11 @@ static inline void ratelimit_set_speed(RateLimit *limit,= uint64_t speed, { QEMU_LOCK_GUARD(&limit->lock); limit->slice_ns =3D slice_ns; - limit->slice_quota =3D MAX(((double)speed * slice_ns) / 1000000000ULL,= 1); + if (speed =3D=3D 0) { + limit->slice_quota =3D 0; + } else { + limit->slice_quota =3D MAX(((double)speed * slice_ns) / 1000000000= ULL, 1); + } } =20 #endif --=20 2.30.2 From nobody Mon Apr 29 00:49:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620637407; cv=none; d=zohomail.com; s=zohoarc; b=nP3J7db2vI65snX4yL8ug+v8tn/yXF9TH5iaODt4QmXbs8lWbQMIg89pLf2AB1v/I7epudRDfNEfi3OXy3B0ljQ5PSly0SqQYFHYmOPSXg4Re80WSROOQkoKdcnAiYX6aW0D70K/BcysEBOn1vKzqG9qjuftDlgFW+SrcHf2i0I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620637407; h=Content-Type: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=ZJ/AB8Vi0nkp+ugLaGTjDja6Nl2eYXX5P1QMpx7KlnA=; b=GAHW3Y4aRJTTz+a0jEBhRE+dX1PXKRU5oYTGJWsxSKeNuzoV2xl8QVpUGpvOqZPbuedWHxNBQD9p4z/900h0Vkx9F4Oo4drfn4BonIIX0l3RPgCpjrq3TCxASWh05M2wks4rsYL9de2cMbUpjZm/8r3pZnfaD/i5aWjmVQ3wx0s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1620637406765559.063837691993; Mon, 10 May 2021 02:03:26 -0700 (PDT) Received: from localhost ([::1]:52870 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lg1oz-00075A-0X for importer@patchew.org; Mon, 10 May 2021 05:03:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56264) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lg1m6-000487-TJ for qemu-devel@nongnu.org; Mon, 10 May 2021 05:00:28 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:41842) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lg1lw-0005vH-UT for qemu-devel@nongnu.org; Mon, 10 May 2021 05:00:26 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-517-j2KoLWpfN2Ky78JT8fYBjA-1; Mon, 10 May 2021 04:59:58 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C80CA195D560; Mon, 10 May 2021 08:59:53 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-113-184.ams2.redhat.com [10.36.113.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id A810962923; Mon, 10 May 2021 08:59:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620637216; 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=ZJ/AB8Vi0nkp+ugLaGTjDja6Nl2eYXX5P1QMpx7KlnA=; b=SU1UlpnhzeBDjVv/+H1HmCK4tFpjB24l1eD+4BkAz2IObBFm9BcCpCtQjkXxk+eQekrYI4 9LEAg1/d4/OsFtW2rBK6M0WN1+dSR114nFdcFdw5QyKld+RIJdMpmnqx7ii45i9NnEcJHh Peh5vw6EjyTndI25SYlgyHATCQnM3vw= X-MC-Unique: j2KoLWpfN2Ky78JT8fYBjA-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH 2/6] block-copy: let ratelimit handle a speed of 0 Date: Mon, 10 May 2021 10:59:37 +0200 Message-Id: <20210510085941.22769-3-eesposit@redhat.com> In-Reply-To: <20210510085941.22769-1-eesposit@redhat.com> References: <20210510085941.22769-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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 Content-Transfer-Encoding: quoted-printable 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=216.205.24.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.699, 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_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Paolo Bonzini Signed-off-by: Paolo Bonzini Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Vladimir Sementsov-Ogievskiy --- block/block-copy.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/block/block-copy.c b/block/block-copy.c index c2e5090412..7e9467d48a 100644 --- a/block/block-copy.c +++ b/block/block-copy.c @@ -113,7 +113,6 @@ typedef struct BlockCopyState { =20 SharedResource *mem; =20 - uint64_t speed; RateLimit rate_limit; } BlockCopyState; =20 @@ -619,23 +618,19 @@ block_copy_dirty_clusters(BlockCopyCallState *call_st= ate) } task->zeroes =3D ret & BDRV_BLOCK_ZERO; =20 - if (s->speed) { - if (!call_state->ignore_ratelimit) { - uint64_t ns =3D ratelimit_calculate_delay(&s->rate_limit, = 0); - if (ns > 0) { - block_copy_task_end(task, -EAGAIN); - g_free(task); - qemu_co_sleep_ns_wakeable(&call_state->sleep, - QEMU_CLOCK_REALTIME, ns); - continue; - } + if (!call_state->ignore_ratelimit) { + uint64_t ns =3D ratelimit_calculate_delay(&s->rate_limit, 0); + if (ns > 0) { + block_copy_task_end(task, -EAGAIN); + g_free(task); + qemu_co_sleep_ns_wakeable(&call_state->sleep, + QEMU_CLOCK_REALTIME, ns); + continue; } - - ratelimit_calculate_delay(&s->rate_limit, task->bytes); } =20 + ratelimit_calculate_delay(&s->rate_limit, task->bytes); trace_block_copy_process(s, task->offset); - co_get_from_shres(s->mem, task->bytes); =20 offset =3D task_end(task); @@ -825,10 +820,7 @@ void block_copy_set_skip_unallocated(BlockCopyState *s= , bool skip) =20 void block_copy_set_speed(BlockCopyState *s, uint64_t speed) { - s->speed =3D speed; - if (speed > 0) { - ratelimit_set_speed(&s->rate_limit, speed, BLOCK_COPY_SLICE_TIME); - } + ratelimit_set_speed(&s->rate_limit, speed, BLOCK_COPY_SLICE_TIME); =20 /* * Note: it's good to kick all call states from here, but it should be= done --=20 2.30.2 From nobody Mon Apr 29 00:49:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620637526; cv=none; d=zohomail.com; s=zohoarc; b=hVzJnYAZrHx4BFhUiShn8BzkLCpPo0RGqezVxCX/DhDDBIhVSq4gnrSuTc+89g7BXNHC8osQgTlSW+7y+uHt1AOs//8Hcx7vKCi/WpXU6M7g6E/X2v/0fRPyE6+6/UNk2P7hOVgIr06ckS2gfO2NUQFWYzFBeDLW0XlfsMUyEHg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620637526; h=Content-Type: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=WacJFVkbfLKPGda97mgasN/KKrj662I+VeKllJ5ILvQ=; b=aHISC1MWJmLRQV40QlSrCMYoFiLPYQOTD9nt5Lj79GcrR+SGVEPhYipTF4NoZfRFL0YsFGyNF6/tTAixX6awINzlmLlSmIGgJQdrgxlB2+uWgYv7QBFlg+fqsvNOW57iVb9clVzkCFhojfxIF4EUoQwr2kHr80akaBmImuHNCWQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1620637526662819.4177965062042; Mon, 10 May 2021 02:05:26 -0700 (PDT) Received: from localhost ([::1]:59498 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lg1qv-00035E-L6 for importer@patchew.org; Mon, 10 May 2021 05:05:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56012) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lg1li-0003Rf-Jp for qemu-devel@nongnu.org; Mon, 10 May 2021 05:00:02 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:51293) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lg1lg-0005hn-3u for qemu-devel@nongnu.org; Mon, 10 May 2021 05:00:02 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-501--4orAuLWOFuM3jEYZjpn0Q-1; Mon, 10 May 2021 04:59:57 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4DD99107ACCA; Mon, 10 May 2021 08:59:56 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-113-184.ams2.redhat.com [10.36.113.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id 23FD062923; Mon, 10 May 2021 08:59:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620637199; 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=WacJFVkbfLKPGda97mgasN/KKrj662I+VeKllJ5ILvQ=; b=ghd07kBEa1bGzyWx1djVUcc+EoQpZqTpnxllWrns3Yw5ZvqSAr8YbsvYYfA/b9k3haMAJx TeTufUoQX169mMFHvlE4TpobecAJi4a4drlIHJjuox124GRm0Au09ko2mfp8A4dc7t1FZ8 /G4Lvf3Fzzj5i4V4sax0Bp3Mbh7ZbHs= X-MC-Unique: -4orAuLWOFuM3jEYZjpn0Q-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH 3/6] blockjob: let ratelimit handle a speed of 0 Date: Mon, 10 May 2021 10:59:38 +0200 Message-Id: <20210510085941.22769-4-eesposit@redhat.com> In-Reply-To: <20210510085941.22769-1-eesposit@redhat.com> References: <20210510085941.22769-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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 Content-Transfer-Encoding: quoted-printable 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=216.205.24.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.699, 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_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Paolo Bonzini Signed-off-by: Paolo Bonzini Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Vladimir Sementsov-Ogievskiy --- blockjob.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/blockjob.c b/blockjob.c index dc1d9e0e46..046c1bcd66 100644 --- a/blockjob.c +++ b/blockjob.c @@ -300,10 +300,6 @@ bool block_job_set_speed(BlockJob *job, int64_t speed,= Error **errp) =20 int64_t block_job_ratelimit_get_delay(BlockJob *job, uint64_t n) { - if (!job->speed) { - return 0; - } - return ratelimit_calculate_delay(&job->limit, n); } =20 @@ -473,11 +469,9 @@ void *block_job_create(const char *job_id, const Block= JobDriver *driver, blk_set_allow_aio_context_change(blk, true); =20 /* Only set speed when necessary to avoid NotSupported error */ - if (speed !=3D 0) { - if (!block_job_set_speed(job, speed, errp)) { - job_early_fail(&job->job); - return NULL; - } + if (!block_job_set_speed(job, speed, errp)) { + job_early_fail(&job->job); + return NULL; } =20 return job; --=20 2.30.2 From nobody Mon Apr 29 00:49:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620637431; cv=none; d=zohomail.com; s=zohoarc; b=gNaHMviLR5V1Mci+peGf70AslYqJF0jsNf9EjVWAIEcs06NgRBaFBdUAU8Q9Akbm/m5UqWlgap1nk227Gk6a8k5bZQ1/BHvUrDPOhwd90VDKXBaL7oJR0q8myuihw7YkzAgZc+WB7ynsFaTP2urNbU6ANC4KrgulPeMQHYp9EQM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620637431; h=Content-Type: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=I5A3PWmvRr078VtKbQJIPm7JEvimpSh2QCeF5BIEr6Y=; b=fzr+e094K+arx6md8uXGbNUeMGMns+NtQntXdxVewQG6zwS0fRyDIMMB3TWXMI2ZISFuoQTJkwb66Ke7khdSVde+8Xojbp1khrtwSRfEvb2QEBkX+qHWHpXe2pcM+w4VdPX+w+opgXOGHD2l0VuZm/l7UnhMGvkvX+xt/5jlutA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1620637431197299.5929698251492; Mon, 10 May 2021 02:03:51 -0700 (PDT) Received: from localhost ([::1]:53868 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lg1pN-0007lN-7F for importer@patchew.org; Mon, 10 May 2021 05:03:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56070) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lg1lo-0003iG-8I for qemu-devel@nongnu.org; Mon, 10 May 2021 05:00:08 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:30797) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lg1ll-0005nH-G8 for qemu-devel@nongnu.org; Mon, 10 May 2021 05:00:07 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-481-d2Z0R5AUPcWEvtcqby3b0w-1; Mon, 10 May 2021 04:59:59 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D590B800D62; Mon, 10 May 2021 08:59:58 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-113-184.ams2.redhat.com [10.36.113.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id 99C726267D; Mon, 10 May 2021 08:59:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620637204; 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=I5A3PWmvRr078VtKbQJIPm7JEvimpSh2QCeF5BIEr6Y=; b=GbohMu0lc1XtWovV3pO9tpA+hGiVIdI90X46L/Qd5hp5NRHV2XOOzlxdsTAPGdL/2tA5Go 0qNmkwNFVUX2pRsHIbvh70aco7zHXHBluFeH3d+9J0LFYlsMonP5WawJYtCpG7kIR0H9JH Gtv2y45EMQUxnh8XFpG11p9bLY4koUM= X-MC-Unique: d2Z0R5AUPcWEvtcqby3b0w-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH 4/6] progressmeter: protect with a mutex Date: Mon, 10 May 2021 10:59:39 +0200 Message-Id: <20210510085941.22769-5-eesposit@redhat.com> In-Reply-To: <20210510085941.22769-1-eesposit@redhat.com> References: <20210510085941.22769-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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 Content-Transfer-Encoding: quoted-printable 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=216.205.24.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.699, 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_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Progressmeter is protected by the AioContext mutex, which is taken by the block jobs and their caller (like blockdev). We would like to remove the dependency of block layer code on the AioContext mutex, since most drivers and the core I/O code are already not relying on it. The simplest thing to do is to add a mutex to be able to provide an accurate snapshot of the progress values to the caller. Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Stefan Hajnoczi --- blockjob.c | 33 +++++++++++++++++++++++++-------- include/qemu/progress_meter.h | 31 +++++++++++++++++++++++++++++++ job-qmp.c | 8 ++++++-- job.c | 3 +++ qemu-img.c | 9 ++++++--- 5 files changed, 71 insertions(+), 13 deletions(-) diff --git a/blockjob.c b/blockjob.c index 046c1bcd66..98bb12f015 100644 --- a/blockjob.c +++ b/blockjob.c @@ -306,18 +306,23 @@ int64_t block_job_ratelimit_get_delay(BlockJob *job, = uint64_t n) BlockJobInfo *block_job_query(BlockJob *job, Error **errp) { BlockJobInfo *info; + uint64_t progress_current, progress_total; =20 if (block_job_is_internal(job)) { error_setg(errp, "Cannot query QEMU internal jobs"); return NULL; } + + progress_get_snapshot(&job->job.progress, &progress_current, + &progress_total); + info =3D g_new0(BlockJobInfo, 1); info->type =3D g_strdup(job_type_str(&job->job)); info->device =3D g_strdup(job->job.id); info->busy =3D qatomic_read(&job->job.busy); info->paused =3D job->job.pause_count > 0; - info->offset =3D job->job.progress.current; - info->len =3D job->job.progress.total; + info->offset =3D progress_current; + info->len =3D progress_total; info->speed =3D job->speed; info->io_status =3D job->iostatus; info->ready =3D job_is_ready(&job->job), @@ -344,15 +349,19 @@ static void block_job_iostatus_set_err(BlockJob *job,= int error) static void block_job_event_cancelled(Notifier *n, void *opaque) { BlockJob *job =3D opaque; + uint64_t progress_current, progress_total; =20 if (block_job_is_internal(job)) { return; } =20 + progress_get_snapshot(&job->job.progress, &progress_current, + &progress_total); + qapi_event_send_block_job_cancelled(job_type(&job->job), job->job.id, - job->job.progress.total, - job->job.progress.current, + progress_total, + progress_current, job->speed); } =20 @@ -360,6 +369,7 @@ static void block_job_event_completed(Notifier *n, void= *opaque) { BlockJob *job =3D opaque; const char *msg =3D NULL; + uint64_t progress_current, progress_total; =20 if (block_job_is_internal(job)) { return; @@ -369,10 +379,13 @@ static void block_job_event_completed(Notifier *n, vo= id *opaque) msg =3D error_get_pretty(job->job.err); } =20 + progress_get_snapshot(&job->job.progress, &progress_current, + &progress_total); + qapi_event_send_block_job_completed(job_type(&job->job), job->job.id, - job->job.progress.total, - job->job.progress.current, + progress_total, + progress_current, job->speed, !!msg, msg); @@ -393,15 +406,19 @@ static void block_job_event_pending(Notifier *n, void= *opaque) static void block_job_event_ready(Notifier *n, void *opaque) { BlockJob *job =3D opaque; + uint64_t progress_current, progress_total; =20 if (block_job_is_internal(job)) { return; } =20 + progress_get_snapshot(&job->job.progress, &progress_current, + &progress_total); + qapi_event_send_block_job_ready(job_type(&job->job), job->job.id, - job->job.progress.total, - job->job.progress.current, + progress_total, + progress_current, job->speed); } =20 diff --git a/include/qemu/progress_meter.h b/include/qemu/progress_meter.h index 9a23ff071c..81f79770d2 100644 --- a/include/qemu/progress_meter.h +++ b/include/qemu/progress_meter.h @@ -27,6 +27,8 @@ #ifndef QEMU_PROGRESS_METER_H #define QEMU_PROGRESS_METER_H =20 +#include "qemu/lockable.h" + typedef struct ProgressMeter { /** * Current progress. The unit is arbitrary as long as the ratio between @@ -37,21 +39,50 @@ typedef struct ProgressMeter { =20 /** Estimated current value at the completion of the process */ uint64_t total; + + QemuMutex lock; } ProgressMeter; =20 +static inline void progress_init(ProgressMeter *pm) +{ + qemu_mutex_init(&pm->lock); +} + +static inline void progress_destroy(ProgressMeter *pm) +{ + qemu_mutex_destroy(&pm->lock); +} + +static inline void progress_get_snapshot(ProgressMeter *pm, + uint64_t *current, uint64_t *tota= l) +{ + QEMU_LOCK_GUARD(&pm->lock); + + if (current) { + *current =3D pm->current; + } + + if (total) { + *total =3D pm->total; + } +} + static inline void progress_work_done(ProgressMeter *pm, uint64_t done) { + QEMU_LOCK_GUARD(&pm->lock); pm->current +=3D done; } =20 static inline void progress_set_remaining(ProgressMeter *pm, uint64_t rema= ining) { + QEMU_LOCK_GUARD(&pm->lock); pm->total =3D pm->current + remaining; } =20 static inline void progress_increase_remaining(ProgressMeter *pm, uint64_t delta) { + QEMU_LOCK_GUARD(&pm->lock); pm->total +=3D delta; } =20 diff --git a/job-qmp.c b/job-qmp.c index 34c4da094f..829a28aa70 100644 --- a/job-qmp.c +++ b/job-qmp.c @@ -144,16 +144,20 @@ void qmp_job_dismiss(const char *id, Error **errp) static JobInfo *job_query_single(Job *job, Error **errp) { JobInfo *info; + uint64_t progress_current; + uint64_t progress_total; =20 assert(!job_is_internal(job)); + progress_get_snapshot(&job->progress, &progress_current, + &progress_total); =20 info =3D g_new(JobInfo, 1); *info =3D (JobInfo) { .id =3D g_strdup(job->id), .type =3D job_type(job), .status =3D job->status, - .current_progress =3D job->progress.current, - .total_progress =3D job->progress.total, + .current_progress =3D progress_current, + .total_progress =3D progress_total, .has_error =3D !!job->err, .error =3D job->err ? \ g_strdup(error_get_pretty(job->err)) : NULL, diff --git a/job.c b/job.c index 4aff13d95a..00b0560052 100644 --- a/job.c +++ b/job.c @@ -339,6 +339,8 @@ void *job_create(const char *job_id, const JobDriver *d= river, JobTxn *txn, job->cb =3D cb; job->opaque =3D opaque; =20 + progress_init(&job->progress); + notifier_list_init(&job->on_finalize_cancelled); notifier_list_init(&job->on_finalize_completed); notifier_list_init(&job->on_pending); @@ -382,6 +384,7 @@ void job_unref(Job *job) =20 QLIST_REMOVE(job, job_list); =20 + progress_destroy(&job->progress); error_free(job->err); g_free(job->id); g_free(job); diff --git a/qemu-img.c b/qemu-img.c index a5993682aa..7956a89965 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -900,6 +900,7 @@ static void common_block_job_cb(void *opaque, int ret) =20 static void run_block_job(BlockJob *job, Error **errp) { + uint64_t progress_current, progress_total; AioContext *aio_context =3D blk_get_aio_context(job->blk); int ret =3D 0; =20 @@ -908,9 +909,11 @@ static void run_block_job(BlockJob *job, Error **errp) do { float progress =3D 0.0f; aio_poll(aio_context, true); - if (job->job.progress.total) { - progress =3D (float)job->job.progress.current / - job->job.progress.total * 100.f; + + progress_get_snapshot(&job->job.progress, &progress_current, + &progress_total); + if (progress_total) { + progress =3D (float)progress_current / progress_total * 100.f; } qemu_progress_print(progress, 0); } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); --=20 2.30.2 From nobody Mon Apr 29 00:49:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620637692; cv=none; d=zohomail.com; s=zohoarc; b=PAkPrpa0RiNov0+ZtItAIuB/o3hHWO9PHwikCkjAnNAtAxWkjhnB2FPUphCxby0Uva2KqEQdYz0gaZVs1RoCZqlGpnxhj2nLlWnn9NsLHxzUR3TggKd/219QjdvumcJ0kpEOuH8l4d/CBFAzBIVkgOr27Hu3f0bpJ9JPl9qHFko= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620637692; h=Content-Type: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=T6kPlDFfZY6SAUMlloEcSFKJbzHvBXzvBVlEynO4IUQ=; b=NPjrrp0Esbo+hdw2ddZnrgEdafmxEncYmji7RuwxdWQw/Qgy/g3KRsmvkjOSWTaiHlH2BAJkOlje7TSON/QqwrmJmuafDxcm7aDw8U94MVhOvhVylGV7AVg5keuU8EI3TUALyEPS0NlAK6XOfXWs4BmplltW6vI1Sr+OGbiRXXI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1620637692803690.7659922325842; Mon, 10 May 2021 02:08:12 -0700 (PDT) Received: from localhost ([::1]:37464 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lg1tb-0007Iw-OZ for importer@patchew.org; Mon, 10 May 2021 05:08:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56308) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lg1mG-0004CN-Jr for qemu-devel@nongnu.org; Mon, 10 May 2021 05:00:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:52342) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lg1mF-00068x-3c for qemu-devel@nongnu.org; Mon, 10 May 2021 05:00:36 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-141-dnpzblSOPEKQ2W4AVhWGNg-1; Mon, 10 May 2021 05:00:02 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5200B107ACE3; Mon, 10 May 2021 09:00:01 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-113-184.ams2.redhat.com [10.36.113.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2CF91627DC; Mon, 10 May 2021 08:59:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620637234; 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=T6kPlDFfZY6SAUMlloEcSFKJbzHvBXzvBVlEynO4IUQ=; b=R67uKwNAY+rhzkSpd6QVWZlQw6AHM7caH29PjNWykcXz0PoZt/F58guT2jMQdPQDisdGI/ mX6ajGo/an4yltyG9iF0yqTJtsEMIaMxvJ2tSIM4achOnImNNlluNJsc4jLVRdKTU5ap7y y0uj7smTFRbiyf6sQUbinZfMOupVPtc= X-MC-Unique: dnpzblSOPEKQ2W4AVhWGNg-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH 5/6] co-shared-resource: protect with a mutex Date: Mon, 10 May 2021 10:59:40 +0200 Message-Id: <20210510085941.22769-6-eesposit@redhat.com> In-Reply-To: <20210510085941.22769-1-eesposit@redhat.com> References: <20210510085941.22769-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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 Content-Transfer-Encoding: quoted-printable 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=216.205.24.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.699, 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_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" co-shared-resource is currently not thread-safe, as also reported in co-shared-resource.h. Add a QemuMutex because co_try_get_from_shres can also be invoked from non-coroutine context. Signed-off-by: Emanuele Giuseppe Esposito --- util/qemu-co-shared-resource.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/util/qemu-co-shared-resource.c b/util/qemu-co-shared-resource.c index 1c83cd9d29..c455d02a1e 100644 --- a/util/qemu-co-shared-resource.c +++ b/util/qemu-co-shared-resource.c @@ -32,6 +32,7 @@ struct SharedResource { uint64_t available; =20 CoQueue queue; + QemuMutex lock; }; =20 SharedResource *shres_create(uint64_t total) @@ -40,17 +41,23 @@ SharedResource *shres_create(uint64_t total) =20 s->total =3D s->available =3D total; qemu_co_queue_init(&s->queue); + qemu_mutex_init(&s->lock); =20 return s; } =20 void shres_destroy(SharedResource *s) { - assert(s->available =3D=3D s->total); + WITH_QEMU_LOCK_GUARD(&s->lock) { + assert(s->available =3D=3D s->total); + } + + qemu_mutex_destroy(&s->lock); g_free(s); } =20 -bool co_try_get_from_shres(SharedResource *s, uint64_t n) +/* Called with lock held */ +static bool co_try_get_from_shres_unlocked(SharedResource *s, uint64_t n) { if (s->available >=3D n) { s->available -=3D n; @@ -60,16 +67,27 @@ bool co_try_get_from_shres(SharedResource *s, uint64_t = n) return false; } =20 +bool co_try_get_from_shres(SharedResource *s, uint64_t n) +{ + bool res; + QEMU_LOCK_GUARD(&s->lock); + res =3D co_try_get_from_shres_unlocked(s, n); + + return res; +} + void coroutine_fn co_get_from_shres(SharedResource *s, uint64_t n) { + QEMU_LOCK_GUARD(&s->lock); assert(n <=3D s->total); - while (!co_try_get_from_shres(s, n)) { - qemu_co_queue_wait(&s->queue, NULL); + while (!co_try_get_from_shres_unlocked(s, n)) { + qemu_co_queue_wait(&s->queue, &s->lock); } } =20 void coroutine_fn co_put_to_shres(SharedResource *s, uint64_t n) { + QEMU_LOCK_GUARD(&s->lock); assert(s->total - s->available >=3D n); s->available +=3D n; qemu_co_queue_restart_all(&s->queue); --=20 2.30.2 From nobody Mon Apr 29 00:49:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620637525; cv=none; d=zohomail.com; s=zohoarc; b=LWdHb8Sh+oZhu44NoWADCLeWx6xMxrVQBDnGveJvymIHCqd3nxw2sCKQJgYmf6n9uaxQ8Snx+SM2ZwXeXRCZj72WqKMRxUL1fCfnW1QYwTzd7tpJhBPXeleEjiJ+saPAkWtHb40rTemnAQmV1+JwdDJNo42+L8tlqd+5pks+1GQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620637525; h=Content-Type: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=2pB1gUVEJLGXCbwQ9saUHJhCgpt3dW+ZVZpBbS0d+GM=; b=C6SmB0YCDoyP6J7SMkPLPDDeRaSPGKMZ4/67WOdVENVpFXGjPkrpQSezMkQr/ZAKC7aPeiQBTkXFqVB2dkrTZ1DFlEq1LC1/fQRNbRMbH9Jdx7qAJPxK44u/EQqfDPtfgoew4mneyi4tALa7GKif3xICINuPThzUoLxy4KbD4wU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1620637525623934.8853541220235; Mon, 10 May 2021 02:05:25 -0700 (PDT) Received: from localhost ([::1]:59438 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lg1qu-000336-HV for importer@patchew.org; Mon, 10 May 2021 05:05:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56192) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lg1lz-00044T-69 for qemu-devel@nongnu.org; Mon, 10 May 2021 05:00:19 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:47865) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lg1lx-0005wC-7v for qemu-devel@nongnu.org; Mon, 10 May 2021 05:00:18 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-560-oNoXE0F4N0qdB6fVND2J1w-1; Mon, 10 May 2021 05:00:15 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 208478189DA; Mon, 10 May 2021 09:00:14 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-113-184.ams2.redhat.com [10.36.113.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id B03BB62499; Mon, 10 May 2021 09:00:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620637216; 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=2pB1gUVEJLGXCbwQ9saUHJhCgpt3dW+ZVZpBbS0d+GM=; b=PE16iaOt0zHj3r/aqGiAMqw9f9N62tFwqhTOrAB2y7DXWhQZWxEjvN8Yyvh7c+r1Lv/GuU sAabb14IdU38FrvOt6d38ExBehnVYIYYq0Fk3nUtGZ4zIyM0mBtEuq9Bj4yKdn5ark2tq3 03heqT8PbF+jZAan3AsSg5yEFbmX/nA= X-MC-Unique: oNoXE0F4N0qdB6fVND2J1w-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH 6/6] aiopool: protect with a mutex Date: Mon, 10 May 2021 10:59:41 +0200 Message-Id: <20210510085941.22769-7-eesposit@redhat.com> In-Reply-To: <20210510085941.22769-1-eesposit@redhat.com> References: <20210510085941.22769-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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 Content-Transfer-Encoding: quoted-printable 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: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.699, 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_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Divide the fields in AioTaskPool in IN and Status, and introduce a CoQueue instead of .wait to take care of suspending and resuming the calling coroutine, and a lock to protect the busy_tasks counter accesses and the AioTask .ret field. Signed-off-by: Emanuele Giuseppe Esposito --- block/aio_task.c | 63 ++++++++++++++++++++++++---------------- include/block/aio_task.h | 2 +- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/block/aio_task.c b/block/aio_task.c index 88989fa248..7ac6b5dd72 100644 --- a/block/aio_task.c +++ b/block/aio_task.c @@ -27,62 +27,70 @@ #include "block/aio_task.h" =20 struct AioTaskPool { - Coroutine *main_co; - int status; + /* IN: just set in aio_task_pool_new and never modified */ int max_busy_tasks; + + /* Status: either atomic or protected by the lock */ + int status; int busy_tasks; - bool waiting; + CoQueue queue; + CoMutex lock; }; =20 static void coroutine_fn aio_task_co(void *opaque) { + int ret; AioTask *task =3D opaque; AioTaskPool *pool =3D task->pool; =20 - assert(pool->busy_tasks < pool->max_busy_tasks); - pool->busy_tasks++; + WITH_QEMU_LOCK_GUARD(&pool->lock) { + assert(pool->busy_tasks < pool->max_busy_tasks); + pool->busy_tasks++; =20 - task->ret =3D task->func(task); + ret =3D task->func(task); + task->ret =3D ret; =20 - pool->busy_tasks--; + pool->busy_tasks--; + } =20 - if (task->ret < 0 && pool->status =3D=3D 0) { - pool->status =3D task->ret; + if (ret < 0) { + qatomic_cmpxchg(&pool->status, 0, ret); } =20 g_free(task); =20 - if (pool->waiting) { - pool->waiting =3D false; - aio_co_wake(pool->main_co); - } + qemu_co_queue_next(&pool->queue); } =20 -void coroutine_fn aio_task_pool_wait_one(AioTaskPool *pool) +/* Called with lock held */ +static void coroutine_fn aio_task_pool_wait_one_unlocked(AioTaskPool *pool) { assert(pool->busy_tasks > 0); - assert(qemu_coroutine_self() =3D=3D pool->main_co); - - pool->waiting =3D true; - qemu_coroutine_yield(); - - assert(!pool->waiting); + qemu_co_queue_wait(&pool->queue, &pool->lock); assert(pool->busy_tasks < pool->max_busy_tasks); } =20 +void coroutine_fn aio_task_pool_wait_one(AioTaskPool *pool) +{ + QEMU_LOCK_GUARD(&pool->lock); + aio_task_pool_wait_one_unlocked(pool); +} + void coroutine_fn aio_task_pool_wait_slot(AioTaskPool *pool) { + QEMU_LOCK_GUARD(&pool->lock); if (pool->busy_tasks < pool->max_busy_tasks) { return; } =20 - aio_task_pool_wait_one(pool); + aio_task_pool_wait_one_unlocked(pool); } =20 void coroutine_fn aio_task_pool_wait_all(AioTaskPool *pool) { + QEMU_LOCK_GUARD(&pool->lock); while (pool->busy_tasks > 0) { - aio_task_pool_wait_one(pool); + aio_task_pool_wait_one_unlocked(pool); } } =20 @@ -98,8 +106,8 @@ AioTaskPool *coroutine_fn aio_task_pool_new(int max_busy= _tasks) { AioTaskPool *pool =3D g_new0(AioTaskPool, 1); =20 - pool->main_co =3D qemu_coroutine_self(); pool->max_busy_tasks =3D max_busy_tasks; + qemu_co_queue_init(&pool->queue); =20 return pool; } @@ -115,10 +123,15 @@ int aio_task_pool_status(AioTaskPool *pool) return 0; /* Sugar for lazy allocation of aio pool */ } =20 - return pool->status; + return qatomic_read(&pool->status); } =20 bool aio_task_pool_empty(AioTaskPool *pool) { - return pool->busy_tasks =3D=3D 0; + int tasks; + + qemu_co_mutex_lock(&pool->lock); + tasks =3D pool->busy_tasks; + qemu_co_mutex_unlock(&pool->lock); + return tasks =3D=3D 0; } diff --git a/include/block/aio_task.h b/include/block/aio_task.h index 50bc1e1817..b22a4310aa 100644 --- a/include/block/aio_task.h +++ b/include/block/aio_task.h @@ -33,7 +33,7 @@ typedef int coroutine_fn (*AioTaskFunc)(AioTask *task); struct AioTask { AioTaskPool *pool; AioTaskFunc func; - int ret; + int ret; /* atomic */ }; =20 AioTaskPool *coroutine_fn aio_task_pool_new(int max_busy_tasks); --=20 2.30.2