From nobody Wed May 15 19:49:11 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=1621331368; cv=none; d=zohomail.com; s=zohoarc; b=f1EXKOa+mAChXZ287MCIudEx/Pi/2X/8sCn+c9E9pN/E/PP9sQMNthiWoXSHf7BY/UuI+2vrbc5U3ZN3mu81rZ6T7MIdFwZXcN7/I6dS/efSprlW+I4rnj5ToNI1jXO5u5PVYgAWPRa7x6Qn3Mqtm0exSdV/rziS2ra+HPZg+l0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621331368; 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=hh1v6TxpBzWMlyWoHnoITWi2rX0mQzlShi927/beNPc=; b=UgYKLkndrUZUZ/mIzF4n0To0MIFGJA3lLSu33PaQ+7PoBeWJP2b+Bm9Yb5ZOSEz2A+v+2q2lByIGqmcj0f3c4+YUBn2ymBnyOgd5I9QzfQh78CKdvPWQEwVWrqe9G9AqpH8S347gPL5n9dPko6MdbaRqaHdDUd22fcdZglrp1oM= 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 1621331368454328.98477678087613; Tue, 18 May 2021 02:49:28 -0700 (PDT) Received: from localhost ([::1]:38342 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liwLu-0004DT-FP for importer@patchew.org; Tue, 18 May 2021 05:49:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35710) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liwE1-0002PB-Gz for qemu-devel@nongnu.org; Tue, 18 May 2021 05:41:17 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:26988) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liwDx-0007vY-K9 for qemu-devel@nongnu.org; Tue, 18 May 2021 05:41:17 -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-377-PDhcFIjyPx-dmdD8ZvOvow-1; Tue, 18 May 2021 05:41:10 -0400 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 90C321013721; Tue, 18 May 2021 09:41:09 +0000 (UTC) Received: from localhost.localdomain (ovpn-114-236.ams2.redhat.com [10.36.114.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4953B17A9B; Tue, 18 May 2021 09:41:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621330872; 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=hh1v6TxpBzWMlyWoHnoITWi2rX0mQzlShi927/beNPc=; b=QCXvCQe8wB0fb+JGMSRB/S+gyMjsJniyh2y5eRyJJWNxUgPNYUwSwC5FGGweuxYHBF4XtY k1Ow9NCM9cqsDLlHFVg2iWBKjQXxFBhM20T/5423KIv1MOajVdwJuoZeWDSPCQClOXghHz t8NZf6+xbt2L+ir3A5F+pbLSRQwpwGE= X-MC-Unique: PDhcFIjyPx-dmdD8ZvOvow-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v2 1/5] ratelimit: treat zero speed as unlimited Date: Tue, 18 May 2021 11:40:54 +0200 Message-Id: <20210518094058.25952-2-eesposit@redhat.com> In-Reply-To: <20210518094058.25952-1-eesposit@redhat.com> References: <20210518094058.25952-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 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: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, 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.001, RCVD_IN_MSPIKE_WL=0.001, 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 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. Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Paolo Bonzini Signed-off-by: Emanuele Giuseppe Esposito --- 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 Wed May 15 19:49:11 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=1621331566; cv=none; d=zohomail.com; s=zohoarc; b=W6rpOMmaDHHopC5UHa/5hH6Vhie8ozV06x2w8ZuD5dWQjwiuMQ0sRuW0LqpLwOoJXVQ7vsdcf5o0dE1x26/BkI0T9GD1+FmZyFZKX+7HFUEE1TZxFvWXrt+TOzXkL276m8sBicsDMHwdRfJ68GEefA4xP/bfic2zFZdfsF8qpxs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621331566; 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=Ih/0W6Xu4Toe2fAAkV25ROCl/3DmBdCjCVPEFvtgvCI=; b=JbarBbPEqkKTc75G/gDt1Mkm9YjhIEsc7wpZT95zVKceibWn+HNDCHSIBpdLzya5Fdqa5hwI3A/iB2pRJ7TYyRcIxvlzfm0ec5n/fatCMFmSW75ky8jxVYkAW58qmRxW2eoaJ6qldOZdqj3A4jD4xMAoDYlpG34hDzW1HuKuYn4= 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 1621331566006281.57215768651986; Tue, 18 May 2021 02:52:46 -0700 (PDT) Received: from localhost ([::1]:50630 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liwP6-0004LE-Vz for importer@patchew.org; Tue, 18 May 2021 05:52:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35720) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liwE3-0002VF-2z for qemu-devel@nongnu.org; Tue, 18 May 2021 05:41:19 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:47300) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liwE0-0007xZ-2E for qemu-devel@nongnu.org; Tue, 18 May 2021 05:41: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-503-vmvD4QCENPWVpKG4CD48CQ-1; Tue, 18 May 2021 05:41:13 -0400 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 3C0C1107ACE3; Tue, 18 May 2021 09:41:12 +0000 (UTC) Received: from localhost.localdomain (ovpn-114-236.ams2.redhat.com [10.36.114.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id E1B7D2ED60; Tue, 18 May 2021 09:41:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621330875; 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=Ih/0W6Xu4Toe2fAAkV25ROCl/3DmBdCjCVPEFvtgvCI=; b=cXWTB/TQqZOGFyjkzLfuRDNnO06cgg1eTpCtML2Sqgs5KIs3+GkaxpIcm4xSqiynvkO1FZ jnPiEVHbXHDIFmjC1hb6z7/Z75/MIVbvnNX+8DYTV89xfc7p1AUjnaa+OQ8Ll8ZlmC0ySZ 5JTdXy8jbDdx3TaXsuSxAOB3L0EW61U= X-MC-Unique: vmvD4QCENPWVpKG4CD48CQ-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v2 2/5] block-copy: let ratelimit handle a speed of 0 Date: Tue, 18 May 2021 11:40:55 +0200 Message-Id: <20210518094058.25952-3-eesposit@redhat.com> In-Reply-To: <20210518094058.25952-1-eesposit@redhat.com> References: <20210518094058.25952-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 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: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, 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.001, RCVD_IN_MSPIKE_WL=0.001, 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 Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Paolo Bonzini Signed-off-by: Emanuele Giuseppe Esposito --- block/block-copy.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/block/block-copy.c b/block/block-copy.c index c2e5090412..37c8e8504b 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,21 +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); =20 co_get_from_shres(s->mem, task->bytes); @@ -825,10 +822,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 Wed May 15 19:49:11 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=1621331905; cv=none; d=zohomail.com; s=zohoarc; b=lcdw388FxcTFFtXoAnxjoNOMcjFOEy0Oua1SIDgYjKPbjie2ylROMCks7pkK1O8eIo/pMfxvDR8LwI0DWyN0acu5Cimlka4V1tsa8PEyGrBBoK2i5tJH/8unO4G/jdZy1maf9fj+JqF7YcbIhb4ZuOew/WuM+hrgI8ydL/pg0/8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621331905; 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=CFDnO1vlXZk8m9pr0EPJZR/pNpnGcIQXuCLDnP9TTqg=; b=RQSJGOSbESmaZ+kvOqRF4cC5mTljmf0K3bJUM3OcSSH2yxV0q2FEsbwLrQAWfQM6CjSUyOGnspHL51+rEgihm05i/07SbF+gBPHC5tKb5XBp+cUGGc4d3ATX3TLy886usAcwqb6xnaLXP8x3xljx+jpn0t+yWisOv45RNJiA4D0= 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 1621331905743349.09595485483567; Tue, 18 May 2021 02:58:25 -0700 (PDT) Received: from localhost ([::1]:42916 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liwUa-0001Tn-HD for importer@patchew.org; Tue, 18 May 2021 05:58:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35744) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liwE4-0002bp-Lk for qemu-devel@nongnu.org; Tue, 18 May 2021 05:41:20 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:53370) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liwE2-0007zD-T3 for qemu-devel@nongnu.org; Tue, 18 May 2021 05:41:20 -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-152-oIFYk7aVPzmuHjKBKg6e_Q-1; Tue, 18 May 2021 05:41:15 -0400 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 DCB7C6414C; Tue, 18 May 2021 09:41:14 +0000 (UTC) Received: from localhost.localdomain (ovpn-114-236.ams2.redhat.com [10.36.114.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id 991CB17A9B; Tue, 18 May 2021 09:41:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621330878; 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=CFDnO1vlXZk8m9pr0EPJZR/pNpnGcIQXuCLDnP9TTqg=; b=iVozlirge61bmao6wufC8XcDDAHwiwWD10qtudrWdUrI4Kqlo2zOdscjIF9Qd5s3JQizr1 mJgFodftpMFbdBN4Vg1lnm/B6GoB86dSYDHLfczmOAJGMLpfNYOrt85r0/EjfrFiOgazE/ FDT7EBLV3DMtu/o/txC+kByObAe23HY= X-MC-Unique: oIFYk7aVPzmuHjKBKg6e_Q-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v2 3/5] blockjob: let ratelimit handle a speed of 0 Date: Tue, 18 May 2021 11:40:56 +0200 Message-Id: <20210518094058.25952-4-eesposit@redhat.com> In-Reply-To: <20210518094058.25952-1-eesposit@redhat.com> References: <20210518094058.25952-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 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: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, 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.001, RCVD_IN_MSPIKE_WL=0.001, 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 Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Paolo Bonzini Signed-off-by: Emanuele Giuseppe Esposito --- blockjob.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/blockjob.c b/blockjob.c index dc1d9e0e46..22e5bb9b1f 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 @@ -472,12 +468,9 @@ void *block_job_create(const char *job_id, const Block= JobDriver *driver, blk_set_disable_request_queuing(blk, true); 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 Wed May 15 19:49:11 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=1621331827; cv=none; d=zohomail.com; s=zohoarc; b=EK+D+/+mT39z1fFyYF1f2so9kUAUl0gBtv6TUhHk/+QxwroNKtqUWDSLr0FCI9s3/LnpjURIaWZXSjgj+YGv76XyIG7GWTibIfJY6Moy7/t/Be1TWtJGFSUaeVAs7u7L8JCuU9iJvg93HG+K/BlAvJf0EiNmW1MQbIetYUBLWHU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621331827; 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=a9axNQ1N7IcJKKH8L4qtlkhwZwrH8Sg1M9KEpCzuV5U=; b=iGo5x2yg+yz7gs3Sb3vxIAdhnOwLhnWKAVG9hlyaCF02vWbLqaXF/W4dYwP8DTT9xazOFmNwvC2KcfUCSoARXmH1vvTxaL6r1eOI20p3FMfnZWSzkSwyz0+dL+ATqWZMuBzl2pMYF6Jn8lq58x4TOrOCAk7MQ1FsxUr2Jg/53ag= 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 162133182742342.047862518595025; Tue, 18 May 2021 02:57:07 -0700 (PDT) Received: from localhost ([::1]:38256 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liwTK-0006mW-C1 for importer@patchew.org; Tue, 18 May 2021 05:57:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35844) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liwEA-000303-Ov for qemu-devel@nongnu.org; Tue, 18 May 2021 05:41:26 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:37034) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liwE8-00084D-AI for qemu-devel@nongnu.org; Tue, 18 May 2021 05:41: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-472-NzQmQx1KMRmTwB39LNcohw-1; Tue, 18 May 2021 05:41:18 -0400 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 DB83981840B; Tue, 18 May 2021 09:41:17 +0000 (UTC) Received: from localhost.localdomain (ovpn-114-236.ams2.redhat.com [10.36.114.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id 46C0817A9B; Tue, 18 May 2021 09:41:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621330883; 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=a9axNQ1N7IcJKKH8L4qtlkhwZwrH8Sg1M9KEpCzuV5U=; b=W9ZwIwBAZNZu+kDOivocrBJXE76tulG8SbqjwN0G6+wDOwTWshmU/0PAKYPmx+2H7q+fVE +CUumwvdYd70uSwBiIEPp0PEQwiOxv0dqFH8uAdtAKxtvH/KKfirGAWLCy6oNn6NGMXnEy VI4msC7F38mA6nCwfdvN8iuVI95Dh0Y= X-MC-Unique: NzQmQx1KMRmTwB39LNcohw-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v2 4/5] progressmeter: protect with a mutex Date: Tue, 18 May 2021 11:40:57 +0200 Message-Id: <20210518094058.25952-5-eesposit@redhat.com> In-Reply-To: <20210518094058.25952-1-eesposit@redhat.com> References: <20210518094058.25952-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 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: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, 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.001, RCVD_IN_MSPIKE_WL=0.001, 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. Create a new C file to implement the ProgressMeter API, but keep the struct as public, to avoid forcing allocation on the heap. Also add a mutex to be able to provide an accurate snapshot of the progress values to the caller. Reviewed-by: Stefan Hajnoczi Signed-off-by: Emanuele Giuseppe Esposito --- block/meson.build | 1 + block/progress_meter.c | 64 +++++++++++++++++++++++++++++++++++ blockjob.c | 33 +++++++++++++----- include/qemu/progress_meter.h | 34 +++++++++++-------- job-qmp.c | 8 +++-- job.c | 3 ++ qemu-img.c | 9 +++-- 7 files changed, 124 insertions(+), 28 deletions(-) create mode 100644 block/progress_meter.c diff --git a/block/meson.build b/block/meson.build index d21990ec95..90efd21ecf 100644 --- a/block/meson.build +++ b/block/meson.build @@ -13,6 +13,7 @@ block_ss.add(files( 'commit.c', 'copy-on-read.c', 'preallocate.c', + 'progress_meter.c', 'create.c', 'crypto.c', 'dirty-bitmap.c', diff --git a/block/progress_meter.c b/block/progress_meter.c new file mode 100644 index 0000000000..aa2e60248c --- /dev/null +++ b/block/progress_meter.c @@ -0,0 +1,64 @@ +/* + * Helper functionality for some process progress tracking. + * + * Copyright (c) 2011 IBM Corp. + * Copyright (c) 2012, 2018 Red Hat, Inc. + * Copyright (c) 2020 Virtuozzo International GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ +#include "qemu/osdep.h" +#include "qemu/progress_meter.h" + +void progress_init(ProgressMeter *pm) +{ + qemu_mutex_init(&pm->lock); +} + +void progress_destroy(ProgressMeter *pm) +{ + qemu_mutex_destroy(&pm->lock); +} + +void progress_get_snapshot(ProgressMeter *pm, uint64_t *current, + uint64_t *total) +{ + QEMU_LOCK_GUARD(&pm->lock); + + *current =3D pm->current; + *total =3D pm->total; +} + +void progress_work_done(ProgressMeter *pm, uint64_t done) +{ + QEMU_LOCK_GUARD(&pm->lock); + pm->current +=3D done; +} + +void progress_set_remaining(ProgressMeter *pm, uint64_t remaining) +{ + QEMU_LOCK_GUARD(&pm->lock); + pm->total =3D pm->current + remaining; +} + +void progress_increase_remaining(ProgressMeter *pm, uint64_t delta) +{ + QEMU_LOCK_GUARD(&pm->lock); + pm->total +=3D delta; +} diff --git a/blockjob.c b/blockjob.c index 22e5bb9b1f..4bad1408cb 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..dadf822bbf 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,22 +39,24 @@ typedef struct ProgressMeter { =20 /** Estimated current value at the completion of the process */ uint64_t total; + + QemuMutex lock; /* protects concurrent access to above fields */ } ProgressMeter; =20 -static inline void progress_work_done(ProgressMeter *pm, uint64_t done) -{ - pm->current +=3D done; -} - -static inline void progress_set_remaining(ProgressMeter *pm, uint64_t rema= ining) -{ - pm->total =3D pm->current + remaining; -} - -static inline void progress_increase_remaining(ProgressMeter *pm, - uint64_t delta) -{ - pm->total +=3D delta; -} +void progress_init(ProgressMeter *pm); +void progress_destroy(ProgressMeter *pm); + +/* Get a snapshot of internal current and total values */ +void progress_get_snapshot(ProgressMeter *pm, uint64_t *current, + uint64_t *total); + +/* Increases the amount of work done so far by @done */ +void progress_work_done(ProgressMeter *pm, uint64_t done); + +/* Sets how much work has to be done to complete to @remaining */ +void progress_set_remaining(ProgressMeter *pm, uint64_t remaining); + +/* Increases the total work to do by @delta */ +void progress_increase_remaining(ProgressMeter *pm, uint64_t delta); =20 #endif /* QEMU_PROGRESS_METER_H */ 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 Wed May 15 19:49:11 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=1621331682; cv=none; d=zohomail.com; s=zohoarc; b=Kl8F4t8f6iqJzDxnm0/4n+EnE8i4XlUAvbMXsuEaS2Ppusi056iq4GNwzgIg4zkltqQIw6orQV31+cVYCc/PYf44Rd+6JGsq49YI9p6OdfqQqoDNQkMsVw3TQkaAUyhekXn17NTDWWENENLh9HoFhHYSiba1r19SjjanW5Y7EA4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621331682; 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=o29XndGUfRwY0P3peJ9TK4DE+Rh+I26/FbueKykjyy8=; b=W4dZs6gFjaBy6JUngCMl1K14HxZ4PCb4PYkmEC/3oRmhkhSPdwS8JdgsAkXcXOhJe9GVAiWCbBZIzazSJidE5aelb/UP538Nj44I+6UIjC1dAxpTnK0vYPhriBDJVl54ljbmBx4zDienHD+p6W+FL5FxiMlg59yImzZ02WSmNBc= 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 1621331682902190.44227817602814; Tue, 18 May 2021 02:54:42 -0700 (PDT) Received: from localhost ([::1]:58858 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liwQz-0001Xo-Tz for importer@patchew.org; Tue, 18 May 2021 05:54:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35834) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liwE9-0002x8-Tx for qemu-devel@nongnu.org; Tue, 18 May 2021 05:41:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:55884) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liwE8-000846-4k for qemu-devel@nongnu.org; Tue, 18 May 2021 05:41:25 -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-497-d7yA4pt3PviZvUvUeGGipw-1; Tue, 18 May 2021 05:41:21 -0400 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 BC138101371E; Tue, 18 May 2021 09:41:20 +0000 (UTC) Received: from localhost.localdomain (ovpn-114-236.ams2.redhat.com [10.36.114.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3F7742ED6F; Tue, 18 May 2021 09:41:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621330883; 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=o29XndGUfRwY0P3peJ9TK4DE+Rh+I26/FbueKykjyy8=; b=HhT6Oxk/nyKJYmhTnTfx/FJbZtMthkHZ2HhC3ALM8UzwkVYLoQ+FDbm8VZI/Dmj18tUH5m +CMiNINyf4pfOkQG2AqWQgQF6kcvm8HvJ8pk1REsS+RWivSEOcezDvPFePDZ4j7oJ2ZYPL vdTY8fCu+Bcdryms/EUDwQUrb3GPGR8= X-MC-Unique: d7yA4pt3PviZvUvUeGGipw-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v2 5/5] co-shared-resource: protect with a mutex Date: Tue, 18 May 2021 11:40:58 +0200 Message-Id: <20210518094058.25952-6-eesposit@redhat.com> In-Reply-To: <20210518094058.25952-1-eesposit@redhat.com> References: <20210518094058.25952-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 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: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, 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.001, RCVD_IN_MSPIKE_WL=0.001, 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" 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 Reviewed-by: Vladimir Sementsov-Ogievskiy --- include/qemu/co-shared-resource.h | 4 +--- util/qemu-co-shared-resource.c | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/include/qemu/co-shared-resource.h b/include/qemu/co-shared-res= ource.h index 4e4503004c..78ca5850f8 100644 --- a/include/qemu/co-shared-resource.h +++ b/include/qemu/co-shared-resource.h @@ -26,15 +26,13 @@ #ifndef QEMU_CO_SHARED_RESOURCE_H #define QEMU_CO_SHARED_RESOURCE_H =20 - +/* Accesses to co-shared-resource API are thread-safe */ typedef struct SharedResource SharedResource; =20 /* * Create SharedResource structure * * @total: total amount of some resource to be shared between clients - * - * Note: this API is not thread-safe. */ SharedResource *shres_create(uint64_t total); =20 diff --git a/util/qemu-co-shared-resource.c b/util/qemu-co-shared-resource.c index 1c83cd9d29..bb875a86be 100644 --- a/util/qemu-co-shared-resource.c +++ b/util/qemu-co-shared-resource.c @@ -28,10 +28,13 @@ #include "qemu/co-shared-resource.h" =20 struct SharedResource { - uint64_t total; - uint64_t available; + uint64_t total; /* Set in shres_create() and not changed anymore */ =20 + /* State fields protected by lock */ + uint64_t available; CoQueue queue; + + QemuMutex lock; }; =20 SharedResource *shres_create(uint64_t total) @@ -40,6 +43,7 @@ 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; } @@ -47,10 +51,12 @@ SharedResource *shres_create(uint64_t total) void shres_destroy(SharedResource *s) { 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_locked(SharedResource *s, uint64_t n) { if (s->available >=3D n) { s->available -=3D n; @@ -60,16 +66,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_locked(s, n); + + return res; +} + void coroutine_fn co_get_from_shres(SharedResource *s, uint64_t n) { assert(n <=3D s->total); - while (!co_try_get_from_shres(s, n)) { - qemu_co_queue_wait(&s->queue, NULL); + QEMU_LOCK_GUARD(&s->lock); + while (!co_try_get_from_shres_locked(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