From nobody Sat May 18 12:47:53 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1626715734059796.9709995006401; Mon, 19 Jul 2021 10:28:54 -0700 (PDT) Received: from localhost ([::1]:60968 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m5X4W-0007pE-Sz for importer@patchew.org; Mon, 19 Jul 2021 13:28:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45220) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5X2s-00054M-Mu for qemu-devel@nongnu.org; Mon, 19 Jul 2021 13:27:10 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:29400) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5X2p-0003Tp-RT for qemu-devel@nongnu.org; Mon, 19 Jul 2021 13:27:10 -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-525-Ty7W4-zROwC44WisyReEIw-1; Mon, 19 Jul 2021 13:27:04 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D3CE38015C6; Mon, 19 Jul 2021 17:27:02 +0000 (UTC) Received: from localhost (ovpn-112-59.ams2.redhat.com [10.36.112.59]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7B3F95D9F0; Mon, 19 Jul 2021 17:27:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626715625; 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=3PPD8etV+1Pe46UlCqV7iYaCKgpbDuzXH3bZmcMxirA=; b=Pbmf9teBkjo6YS6dTlZ+20kDeqEVzz6WBSvvVv8jEbJqwcIjBpCZ1d1TsNNmyH51ScpKT+ v8SNg0P/YUxtbriGUI5R/xe+EGdYObX9qXsRKR2YxWw8rF1zq8F52hPp2SxwDCYjCTUsEc 3IpN8VYOTRHFJZobg72ixZbiJmPUs0Y= X-MC-Unique: Ty7W4-zROwC44WisyReEIw-1 From: Max Reitz To: qemu-block@nongnu.org Subject: [PULL 1/6] blkdebug: refactor removal of a suspended request Date: Mon, 19 Jul 2021 19:26:53 +0200 Message-Id: <20210719172658.715442-2-mreitz@redhat.com> In-Reply-To: <20210719172658.715442-1-mreitz@redhat.com> References: <20210719172658.715442-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mreitz@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=mreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) DKIMWL_WL_HIGH=-1.469, 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 , Peter Maydell , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1626715734349100003 Content-Type: text/plain; charset="utf-8" From: Emanuele Giuseppe Esposito Extract to a separate function. Do not rely on FOREACH_SAFE, which is only "safe" if the *current* node is removed---not if another node is removed. Instead, just walk the entire list from the beginning when asked to resume all suspended requests with a given tag. Co-developed-by: Paolo Bonzini Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake Message-Id: <20210614082931.24925-2-eesposit@redhat.com> Signed-off-by: Max Reitz --- block/blkdebug.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/block/blkdebug.c b/block/blkdebug.c index 2c0b9b0ee8..5ccbfcab42 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -793,7 +793,6 @@ static void suspend_request(BlockDriverState *bs, Blkde= bugRule *rule) printf("blkdebug: Resuming request '%s'\n", r.tag); } =20 - QLIST_REMOVE(&r, next); g_free(r.tag); } =20 @@ -869,25 +868,40 @@ static int blkdebug_debug_breakpoint(BlockDriverState= *bs, const char *event, return 0; } =20 -static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag) +static int resume_req_by_tag(BDRVBlkdebugState *s, const char *tag, bool a= ll) { - BDRVBlkdebugState *s =3D bs->opaque; - BlkdebugSuspendedReq *r, *next; + BlkdebugSuspendedReq *r; =20 - QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) { +retry: + /* + * No need for _SAFE, since a different coroutine can remove another n= ode + * (not the current one) in this list, and when the current one is rem= oved + * the iteration starts back from beginning anyways. + */ + QLIST_FOREACH(r, &s->suspended_reqs, next) { if (!strcmp(r->tag, tag)) { + QLIST_REMOVE(r, next); qemu_coroutine_enter(r->co); + if (all) { + goto retry; + } return 0; } } return -ENOENT; } =20 +static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag) +{ + BDRVBlkdebugState *s =3D bs->opaque; + + return resume_req_by_tag(s, tag, false); +} + static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs, const char *tag) { BDRVBlkdebugState *s =3D bs->opaque; - BlkdebugSuspendedReq *r, *r_next; BlkdebugRule *rule, *next; int i, ret =3D -ENOENT; =20 @@ -900,11 +914,8 @@ static int blkdebug_debug_remove_breakpoint(BlockDrive= rState *bs, } } } - QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) { - if (!strcmp(r->tag, tag)) { - qemu_coroutine_enter(r->co); - ret =3D 0; - } + if (resume_req_by_tag(s, tag, true) =3D=3D 0) { + ret =3D 0; } return ret; } --=20 2.31.1 From nobody Sat May 18 12:47:53 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1626715885081218.39457660782193; Mon, 19 Jul 2021 10:31:25 -0700 (PDT) Received: from localhost ([::1]:40656 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m5X6x-0004kZ-VF for importer@patchew.org; Mon, 19 Jul 2021 13:31:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45258) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5X2v-00056N-KQ for qemu-devel@nongnu.org; Mon, 19 Jul 2021 13:27:14 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:48688) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5X2r-0003Vh-Mo for qemu-devel@nongnu.org; Mon, 19 Jul 2021 13:27:13 -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-473-cIhsWD9rPKe9lSnfQ969hg-1; Mon, 19 Jul 2021 13:27:06 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2BD4610066F7; Mon, 19 Jul 2021 17:27:05 +0000 (UTC) Received: from localhost (ovpn-112-59.ams2.redhat.com [10.36.112.59]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B389D60877; Mon, 19 Jul 2021 17:27:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626715628; 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=E4UIohc1Wi2K/DZs9OmuX0EAzg8fqMb42f9LXAizuxY=; b=SvlpDQybdlpK3v5QqCfHlrswSThkUbjXHBf4CUoeVYnZBNDIE1YCqI4ycdmWLPGvVbjEy+ LonRaFOAQHVarcc6V/fhObTOhTeXDhBFb/hu4tSfKEn8HGCU1qDwPq+Y40wZmmUhRc1r0i IvS9DRe9TuNKhkNWvh5GRcNWmENzcKw= X-MC-Unique: cIhsWD9rPKe9lSnfQ969hg-1 From: Max Reitz To: qemu-block@nongnu.org Subject: [PULL 2/6] blkdebug: move post-resume handling to resume_req_by_tag Date: Mon, 19 Jul 2021 19:26:54 +0200 Message-Id: <20210719172658.715442-3-mreitz@redhat.com> In-Reply-To: <20210719172658.715442-1-mreitz@redhat.com> References: <20210719172658.715442-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mreitz@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=mreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) DKIMWL_WL_HIGH=-1.469, 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 , Peter Maydell , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1626715885986100001 Content-Type: text/plain; charset="utf-8" From: Emanuele Giuseppe Esposito We want to move qemu_coroutine_yield() after the loop on rules, because QLIST_FOREACH_SAFE is wrong if the rule list is modified while the coroutine has yielded. Therefore move the suspended request to the heap and clean it up from the remove side. All that is left is for blkdebug_debug_event to handle the yielding. Co-developed-by: Paolo Bonzini Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210614082931.24925-3-eesposit@redhat.com> Signed-off-by: Max Reitz --- block/blkdebug.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/block/blkdebug.c b/block/blkdebug.c index 5ccbfcab42..e8fdf7b056 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -775,25 +775,20 @@ static void blkdebug_close(BlockDriverState *bs) static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule) { BDRVBlkdebugState *s =3D bs->opaque; - BlkdebugSuspendedReq r; + BlkdebugSuspendedReq *r; =20 - r =3D (BlkdebugSuspendedReq) { - .co =3D qemu_coroutine_self(), - .tag =3D g_strdup(rule->options.suspend.tag), - }; + r =3D g_new(BlkdebugSuspendedReq, 1); + + r->co =3D qemu_coroutine_self(); + r->tag =3D g_strdup(rule->options.suspend.tag); =20 remove_rule(rule); - QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next); + QLIST_INSERT_HEAD(&s->suspended_reqs, r, next); =20 if (!qtest_enabled()) { - printf("blkdebug: Suspended request '%s'\n", r.tag); + printf("blkdebug: Suspended request '%s'\n", r->tag); } qemu_coroutine_yield(); - if (!qtest_enabled()) { - printf("blkdebug: Resuming request '%s'\n", r.tag); - } - - g_free(r.tag); } =20 static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule, @@ -880,8 +875,18 @@ retry: */ QLIST_FOREACH(r, &s->suspended_reqs, next) { if (!strcmp(r->tag, tag)) { + Coroutine *co =3D r->co; + + if (!qtest_enabled()) { + printf("blkdebug: Resuming request '%s'\n", r->tag); + } + QLIST_REMOVE(r, next); - qemu_coroutine_enter(r->co); + g_free(r->tag); + g_free(r); + + qemu_coroutine_enter(co); + if (all) { goto retry; } --=20 2.31.1 From nobody Sat May 18 12:47:53 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1626715751561490.00512436397344; Mon, 19 Jul 2021 10:29:11 -0700 (PDT) Received: from localhost ([::1]:34098 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m5X4o-0000HF-IY for importer@patchew.org; Mon, 19 Jul 2021 13:29:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45254) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5X2u-00055m-Th for qemu-devel@nongnu.org; Mon, 19 Jul 2021 13:27:13 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:43022) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5X2s-0003X2-Op for qemu-devel@nongnu.org; Mon, 19 Jul 2021 13:27:12 -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-96-tGWmIzCcN9yqwvfEz7mpMw-1; Mon, 19 Jul 2021 13:27:08 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 72C7E18414A6; Mon, 19 Jul 2021 17:27:07 +0000 (UTC) Received: from localhost (ovpn-112-59.ams2.redhat.com [10.36.112.59]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1726F5D9F0; Mon, 19 Jul 2021 17:27:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626715630; 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=voy99wCLLRqAgmEkMf0czvY8DsvmFu4vYyQEjZFNspg=; b=BlqQ8rc9PN1CwFhjkrOj3W8kurEImVIDAor9+DqsUEdTVnE8ErpDq5cDtCgUGn4BYtuXf+ QwduCn5ZvhBKHRMw08bE8LQOyMPKnOTniLRMT6BPf4NwCzLEaLptcfK1lvsDg1MU0LuT79 0ar20U5uBqijcfba6aYHlLNfwiyR0Wc= X-MC-Unique: tGWmIzCcN9yqwvfEz7mpMw-1 From: Max Reitz To: qemu-block@nongnu.org Subject: [PULL 3/6] blkdebug: track all actions Date: Mon, 19 Jul 2021 19:26:55 +0200 Message-Id: <20210719172658.715442-4-mreitz@redhat.com> In-Reply-To: <20210719172658.715442-1-mreitz@redhat.com> References: <20210719172658.715442-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mreitz@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=mreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.469, 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 , Peter Maydell , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1626715752739100001 Content-Type: text/plain; charset="utf-8" From: Emanuele Giuseppe Esposito Add a counter for each action that a rule can trigger. This is mainly used to keep track of how many coroutine_yield() we need to perform after processing all rules in the list. Co-developed-by: Paolo Bonzini Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210614082931.24925-4-eesposit@redhat.com> Signed-off-by: Max Reitz --- block/blkdebug.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/block/blkdebug.c b/block/blkdebug.c index e8fdf7b056..6bdeb2c7b3 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -74,6 +74,7 @@ enum { ACTION_INJECT_ERROR, ACTION_SET_STATE, ACTION_SUSPEND, + ACTION__MAX, }; =20 typedef struct BlkdebugRule { @@ -791,22 +792,22 @@ static void suspend_request(BlockDriverState *bs, Blk= debugRule *rule) qemu_coroutine_yield(); } =20 -static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule, - bool injected) +static void process_rule(BlockDriverState *bs, struct BlkdebugRule *rule, + int *action_count) { BDRVBlkdebugState *s =3D bs->opaque; =20 /* Only process rules for the current state */ if (rule->state && rule->state !=3D s->state) { - return injected; + return; } =20 /* Take the action */ + action_count[rule->action]++; switch (rule->action) { case ACTION_INJECT_ERROR: - if (!injected) { + if (action_count[ACTION_INJECT_ERROR] =3D=3D 1) { QSIMPLEQ_INIT(&s->active_rules); - injected =3D true; } QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next); break; @@ -819,21 +820,19 @@ static bool process_rule(BlockDriverState *bs, struct= BlkdebugRule *rule, suspend_request(bs, rule); break; } - return injected; } =20 static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event) { BDRVBlkdebugState *s =3D bs->opaque; struct BlkdebugRule *rule, *next; - bool injected; + int actions_count[ACTION__MAX] =3D { 0 }; =20 assert((int)event >=3D 0 && event < BLKDBG__MAX); =20 - injected =3D false; s->new_state =3D s->state; QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) { - injected =3D process_rule(bs, rule, injected); + process_rule(bs, rule, actions_count); } s->state =3D s->new_state; } --=20 2.31.1 From nobody Sat May 18 12:47:53 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1626715886303347.98977406433164; Mon, 19 Jul 2021 10:31:26 -0700 (PDT) Received: from localhost ([::1]:40736 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m5X6z-0004nN-8P for importer@patchew.org; Mon, 19 Jul 2021 13:31:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45316) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5X2z-0005C3-ER for qemu-devel@nongnu.org; Mon, 19 Jul 2021 13:27:19 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:24739) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5X2x-0003bU-8Y for qemu-devel@nongnu.org; Mon, 19 Jul 2021 13:27:16 -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-179-aNMUEk2iMKG2YXbpVPsU9g-1; Mon, 19 Jul 2021 13:27:11 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CF9AD804141; Mon, 19 Jul 2021 17:27:09 +0000 (UTC) Received: from localhost (ovpn-112-59.ams2.redhat.com [10.36.112.59]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 452CB61093; Mon, 19 Jul 2021 17:27:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626715634; 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=vnwJfIIpdzYND3bS03e3cI7ygpHvu0x9lIHcH4CR5RA=; b=jKDfRPhQQQl6RpWhxFDFqmZaA3YKKEFJaoHJdshwNLre6HKPWl8DZPgIHuPjIXPEX2hHv9 kkOgSW03joWTqdu/oQWapMhwimSJKlaZdS7Lc9WD6/jStCAjxMuTNZ7NF/h8WXERbOvMwz f1WDYGkCBWVyHA/I0r+grAy0TCg6+IA= X-MC-Unique: aNMUEk2iMKG2YXbpVPsU9g-1 From: Max Reitz To: qemu-block@nongnu.org Subject: [PULL 4/6] blkdebug: do not suspend in the middle of QLIST_FOREACH_SAFE Date: Mon, 19 Jul 2021 19:26:56 +0200 Message-Id: <20210719172658.715442-5-mreitz@redhat.com> In-Reply-To: <20210719172658.715442-1-mreitz@redhat.com> References: <20210719172658.715442-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mreitz@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=mreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.469, 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 , Peter Maydell , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1626715888063100001 Content-Type: text/plain; charset="utf-8" From: Emanuele Giuseppe Esposito That would be unsafe in case a rule other than the current one is removed while the coroutine has yielded. Keep FOREACH_SAFE because suspend_request deletes the current rule. After this patch, *all* matching rules are deleted before suspending the coroutine, rather than just one. This doesn't affect the existing testcases. Use actions_count to see how many yield to issue. Co-developed-by: Paolo Bonzini Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210614082931.24925-5-eesposit@redhat.com> Signed-off-by: Max Reitz --- block/blkdebug.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/block/blkdebug.c b/block/blkdebug.c index 6bdeb2c7b3..dd82131d1e 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -789,7 +789,6 @@ static void suspend_request(BlockDriverState *bs, Blkde= bugRule *rule) if (!qtest_enabled()) { printf("blkdebug: Suspended request '%s'\n", r->tag); } - qemu_coroutine_yield(); } =20 static void process_rule(BlockDriverState *bs, struct BlkdebugRule *rule, @@ -834,6 +833,12 @@ static void blkdebug_debug_event(BlockDriverState *bs,= BlkdebugEvent event) QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) { process_rule(bs, rule, actions_count); } + + while (actions_count[ACTION_SUSPEND] > 0) { + qemu_coroutine_yield(); + actions_count[ACTION_SUSPEND]--; + } + s->state =3D s->new_state; } =20 --=20 2.31.1 From nobody Sat May 18 12:47:53 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1626715746366877.1251840893312; Mon, 19 Jul 2021 10:29:06 -0700 (PDT) Received: from localhost ([::1]:33612 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m5X4j-0008PR-BS for importer@patchew.org; Mon, 19 Jul 2021 13:29:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45320) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5X2z-0005C5-JY for qemu-devel@nongnu.org; Mon, 19 Jul 2021 13:27:19 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:56346) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5X2x-0003bZ-AN for qemu-devel@nongnu.org; Mon, 19 Jul 2021 13:27: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-547-vkuk34nKPrmNp3Q2dVLAfQ-1; Mon, 19 Jul 2021 13:27:13 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 25667804140; Mon, 19 Jul 2021 17:27:12 +0000 (UTC) Received: from localhost (ovpn-112-59.ams2.redhat.com [10.36.112.59]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B35F260938; Mon, 19 Jul 2021 17:27:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626715634; 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=giRroj1+EzsSlLW9sdsjxfGSVcEZLdtIfZvGnWmyihM=; b=PJb5OqpAnG+goVQhuoaaHHJHOQiv9U1BWY57JbyzTOD405r8EjS7ULLiI9MXKdIMrxzNzD YwOlKDw3bOWIAiV8LhF1jzxpoFtQtwghnPNY0YcFAPiSbR5pGEFbC+y3MYrCnGZpnyGomT 82r2GoW3HJvorSwe4EJIUfapHhyesMw= X-MC-Unique: vkuk34nKPrmNp3Q2dVLAfQ-1 From: Max Reitz To: qemu-block@nongnu.org Subject: [PULL 5/6] block/blkdebug: remove new_state field and instead use a local variable Date: Mon, 19 Jul 2021 19:26:57 +0200 Message-Id: <20210719172658.715442-6-mreitz@redhat.com> In-Reply-To: <20210719172658.715442-1-mreitz@redhat.com> References: <20210719172658.715442-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mreitz@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=mreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) DKIMWL_WL_HIGH=-1.469, 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 , Peter Maydell , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1626715748122100003 Content-Type: text/plain; charset="utf-8" From: Emanuele Giuseppe Esposito There seems to be no benefit in using a field. Replace it with a local variable, and move the state update before the yields. The state update has do be done before the yields because now using a local variable does not allow the new updated state to be visible by the other yields. Signed-off-by: Emanuele Giuseppe Esposito Message-Id: <20210614082931.24925-6-eesposit@redhat.com> Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Max Reitz --- block/blkdebug.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/block/blkdebug.c b/block/blkdebug.c index dd82131d1e..b47c3fd97c 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -40,7 +40,6 @@ =20 typedef struct BDRVBlkdebugState { int state; - int new_state; uint64_t align; uint64_t max_transfer; uint64_t opt_write_zero; @@ -792,7 +791,7 @@ static void suspend_request(BlockDriverState *bs, Blkde= bugRule *rule) } =20 static void process_rule(BlockDriverState *bs, struct BlkdebugRule *rule, - int *action_count) + int *action_count, int *new_state) { BDRVBlkdebugState *s =3D bs->opaque; =20 @@ -812,7 +811,7 @@ static void process_rule(BlockDriverState *bs, struct B= lkdebugRule *rule, break; =20 case ACTION_SET_STATE: - s->new_state =3D rule->options.set_state.new_state; + *new_state =3D rule->options.set_state.new_state; break; =20 case ACTION_SUSPEND: @@ -825,21 +824,21 @@ static void blkdebug_debug_event(BlockDriverState *bs= , BlkdebugEvent event) { BDRVBlkdebugState *s =3D bs->opaque; struct BlkdebugRule *rule, *next; + int new_state; int actions_count[ACTION__MAX] =3D { 0 }; =20 assert((int)event >=3D 0 && event < BLKDBG__MAX); =20 - s->new_state =3D s->state; + new_state =3D s->state; QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) { - process_rule(bs, rule, actions_count); + process_rule(bs, rule, actions_count, &new_state); } + s->state =3D new_state; =20 while (actions_count[ACTION_SUSPEND] > 0) { qemu_coroutine_yield(); actions_count[ACTION_SUSPEND]--; } - - s->state =3D s->new_state; } =20 static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *eve= nt, --=20 2.31.1 From nobody Sat May 18 12:47:53 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1626715978242530.0012036470862; Mon, 19 Jul 2021 10:32:58 -0700 (PDT) Received: from localhost ([::1]:45020 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m5X8T-0007gL-0I for importer@patchew.org; Mon, 19 Jul 2021 13:32:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45388) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5X33-0005H3-Pb for qemu-devel@nongnu.org; Mon, 19 Jul 2021 13:27:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:38808) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m5X31-0003er-U4 for qemu-devel@nongnu.org; Mon, 19 Jul 2021 13:27:21 -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-258-WhH4k3MzNeqgErPwYRxZKA-1; Mon, 19 Jul 2021 13:27: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 A9BD91084F4B; Mon, 19 Jul 2021 17:27:14 +0000 (UTC) Received: from localhost (ovpn-112-59.ams2.redhat.com [10.36.112.59]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EE6DD19C44; Mon, 19 Jul 2021 17:27:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626715639; 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=Pmc8fK8KoNKCoLW2UsEY+/ylkpVsysQtUC9Fe+pl91g=; b=AukPSKSbieEutJYMybaktwS/L0xZ57dH1/Rn4Qek4X1X1bj8LBBFQ+RAXHj60OEV7JDNJx 1gAPDRYmpw7/5OhJm+t/m6+5m2+QTz9MGG5Qy1o8zmH++zmCuxu7oThEWH3mdMwKOrPs7D VJ61bAzalyLAGzpUo3eVG8oOlwKM53E= X-MC-Unique: WhH4k3MzNeqgErPwYRxZKA-1 From: Max Reitz To: qemu-block@nongnu.org Subject: [PULL 6/6] blkdebug: protect rules and suspended_reqs with a lock Date: Mon, 19 Jul 2021 19:26:58 +0200 Message-Id: <20210719172658.715442-7-mreitz@redhat.com> In-Reply-To: <20210719172658.715442-1-mreitz@redhat.com> References: <20210719172658.715442-1-mreitz@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=mreitz@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=mreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.469, 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 , Peter Maydell , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1626715979761100001 Content-Type: text/plain; charset="utf-8" From: Emanuele Giuseppe Esposito First, categorize the structure fields to identify what needs to be protected and what doesn't. We essentially need to protect only .state, and the 3 lists in BDRVBlkdebugState. Then, add the lock and mark the functions accordingly. Co-developed-by: Paolo Bonzini Signed-off-by: Emanuele Giuseppe Esposito Message-Id: <20210614082931.24925-7-eesposit@redhat.com> Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Max Reitz --- block/blkdebug.c | 49 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/block/blkdebug.c b/block/blkdebug.c index b47c3fd97c..8b67554bec 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -38,24 +38,27 @@ #include "qapi/qobject-input-visitor.h" #include "sysemu/qtest.h" =20 +/* All APIs are thread-safe */ + typedef struct BDRVBlkdebugState { - int state; + /* IN: initialized in blkdebug_open() and never changed */ uint64_t align; uint64_t max_transfer; uint64_t opt_write_zero; uint64_t max_write_zero; uint64_t opt_discard; uint64_t max_discard; - + char *config_file; /* For blkdebug_refresh_filename() */ + /* initialized in blkdebug_parse_perms() */ uint64_t take_child_perms; uint64_t unshare_child_perms; =20 - /* For blkdebug_refresh_filename() */ - char *config_file; - + /* State. Protected by lock */ + int state; QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX]; QSIMPLEQ_HEAD(, BlkdebugRule) active_rules; QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs; + QemuMutex lock; } BDRVBlkdebugState; =20 typedef struct BlkdebugAIOCB { @@ -64,8 +67,11 @@ typedef struct BlkdebugAIOCB { } BlkdebugAIOCB; =20 typedef struct BlkdebugSuspendedReq { + /* IN: initialized in suspend_request() */ Coroutine *co; char *tag; + + /* List entry protected BDRVBlkdebugState's lock */ QLIST_ENTRY(BlkdebugSuspendedReq) next; } BlkdebugSuspendedReq; =20 @@ -77,6 +83,7 @@ enum { }; =20 typedef struct BlkdebugRule { + /* IN: initialized in add_rule() or blkdebug_debug_breakpoint() */ BlkdebugEvent event; int action; int state; @@ -95,6 +102,8 @@ typedef struct BlkdebugRule { char *tag; } suspend; } options; + + /* List entries protected BDRVBlkdebugState's lock */ QLIST_ENTRY(BlkdebugRule) next; QSIMPLEQ_ENTRY(BlkdebugRule) active_next; } BlkdebugRule; @@ -244,11 +253,14 @@ static int add_rule(void *opaque, QemuOpts *opts, Err= or **errp) }; =20 /* Add the rule */ + qemu_mutex_lock(&s->lock); QLIST_INSERT_HEAD(&s->rules[event], rule, next); + qemu_mutex_unlock(&s->lock); =20 return 0; } =20 +/* Called with lock held or from .bdrv_close */ static void remove_rule(BlkdebugRule *rule) { switch (rule->action) { @@ -467,6 +479,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *o= ptions, int flags, int ret; uint64_t align; =20 + qemu_mutex_init(&s->lock); opts =3D qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); if (!qemu_opts_absorb_qdict(opts, options, errp)) { ret =3D -EINVAL; @@ -567,6 +580,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *o= ptions, int flags, ret =3D 0; out: if (ret < 0) { + qemu_mutex_destroy(&s->lock); g_free(s->config_file); } qemu_opts_del(opts); @@ -581,6 +595,7 @@ static int rule_check(BlockDriverState *bs, uint64_t of= fset, uint64_t bytes, int error; bool immediately; =20 + qemu_mutex_lock(&s->lock); QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { uint64_t inject_offset =3D rule->options.inject.offset; =20 @@ -594,6 +609,7 @@ static int rule_check(BlockDriverState *bs, uint64_t of= fset, uint64_t bytes, } =20 if (!rule || !rule->options.inject.error) { + qemu_mutex_unlock(&s->lock); return 0; } =20 @@ -605,6 +621,7 @@ static int rule_check(BlockDriverState *bs, uint64_t of= fset, uint64_t bytes, remove_rule(rule); } =20 + qemu_mutex_unlock(&s->lock); if (!immediately) { aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_sel= f()); qemu_coroutine_yield(); @@ -770,8 +787,10 @@ static void blkdebug_close(BlockDriverState *bs) } =20 g_free(s->config_file); + qemu_mutex_destroy(&s->lock); } =20 +/* Called with lock held. */ static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule) { BDRVBlkdebugState *s =3D bs->opaque; @@ -790,6 +809,7 @@ static void suspend_request(BlockDriverState *bs, Blkde= bugRule *rule) } } =20 +/* Called with lock held. */ static void process_rule(BlockDriverState *bs, struct BlkdebugRule *rule, int *action_count, int *new_state) { @@ -829,11 +849,13 @@ static void blkdebug_debug_event(BlockDriverState *bs= , BlkdebugEvent event) =20 assert((int)event >=3D 0 && event < BLKDBG__MAX); =20 - new_state =3D s->state; - QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) { - process_rule(bs, rule, actions_count, &new_state); + WITH_QEMU_LOCK_GUARD(&s->lock) { + new_state =3D s->state; + QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) { + process_rule(bs, rule, actions_count, &new_state); + } + s->state =3D new_state; } - s->state =3D new_state; =20 while (actions_count[ACTION_SUSPEND] > 0) { qemu_coroutine_yield(); @@ -861,11 +883,14 @@ static int blkdebug_debug_breakpoint(BlockDriverState= *bs, const char *event, .options.suspend.tag =3D g_strdup(tag), }; =20 + qemu_mutex_lock(&s->lock); QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next); + qemu_mutex_unlock(&s->lock); =20 return 0; } =20 +/* Called with lock held. May temporarily release lock. */ static int resume_req_by_tag(BDRVBlkdebugState *s, const char *tag, bool a= ll) { BlkdebugSuspendedReq *r; @@ -888,7 +913,9 @@ retry: g_free(r->tag); g_free(r); =20 + qemu_mutex_unlock(&s->lock); qemu_coroutine_enter(co); + qemu_mutex_lock(&s->lock); =20 if (all) { goto retry; @@ -902,7 +929,7 @@ retry: static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag) { BDRVBlkdebugState *s =3D bs->opaque; - + QEMU_LOCK_GUARD(&s->lock); return resume_req_by_tag(s, tag, false); } =20 @@ -913,6 +940,7 @@ static int blkdebug_debug_remove_breakpoint(BlockDriver= State *bs, BlkdebugRule *rule, *next; int i, ret =3D -ENOENT; =20 + QEMU_LOCK_GUARD(&s->lock); for (i =3D 0; i < BLKDBG__MAX; i++) { QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) { if (rule->action =3D=3D ACTION_SUSPEND && @@ -933,6 +961,7 @@ static bool blkdebug_debug_is_suspended(BlockDriverStat= e *bs, const char *tag) BDRVBlkdebugState *s =3D bs->opaque; BlkdebugSuspendedReq *r; =20 + QEMU_LOCK_GUARD(&s->lock); QLIST_FOREACH(r, &s->suspended_reqs, next) { if (!strcmp(r->tag, tag)) { return true; --=20 2.31.1