From nobody Sat May 30 20:11:38 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1776788012; cv=none; d=zohomail.com; s=zohoarc; b=RFc9ai2lFPI2vXzuhhHo6XQGNraDslCv1SWTzrylI2EjAI+5K0xhSz7De54mFIljcAZTS9yA+nbDNWzOJZD99GL6gGMPv35ISrhDXKKAFp+nGnnUHQf3nSFodOn9GsEDPva+lIc+XnZC42Pi7JifbTVzFeOlLjc0+uyWRSY5Eqc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776788012; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=jo+ubSRf8Apw+IWgJ8yH3iKGU+qjmtNGJAAVsB8BsNg=; b=K/0d8VBI7FgOYZ8zPSWf6kmwjdxGS2M64p/FQb/4Hjezv6kjkn/WcjiF/7YRtwGUjgVnWUa+xsyfzqptJdATTneh6DnyShxAGidyf79UXxdlSq0KOhxaghsuzPlplr0kHeaBl1Yc7mm58RhHXcY6hWKie7KLY67x1AHYPnhtdVs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 177678801212275.24476080737509; Tue, 21 Apr 2026 09:13:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wFDi0-0001MS-ST; Tue, 21 Apr 2026 12:12:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDhy-0001Km-21 for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDht-0000Eg-2A for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:17 -0400 Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-610-wArqpyo3OeKZeu5asFJLMg-1; Tue, 21 Apr 2026 12:12:05 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E737C1800366; Tue, 21 Apr 2026 16:12:03 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.44.49.173]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EDBA9180045E; Tue, 21 Apr 2026 16:12:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1776787931; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jo+ubSRf8Apw+IWgJ8yH3iKGU+qjmtNGJAAVsB8BsNg=; b=TqPoyZi1hgCpxA1AAZmzBaBF1PxntSO2uO3xHUhpLQWWukJuzZ+D1msHrT02Ph96Xya4me YZpVDRfKLryzHbxZjag1ByLQnrQcjzYVmzUOS5yWCjaVBH5coy+88XaN+SnOgH+4BR15mJ kYYKmDgSKaqVQiSLDovdCnDbAD/vIdg= X-MC-Unique: wArqpyo3OeKZeu5asFJLMg-1 X-Mimecast-MFC-AGG-ID: wArqpyo3OeKZeu5asFJLMg_1776787924 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, jsnow@redhat.com, qemu-devel@nongnu.org, qemu-stable@nongnu.org Subject: [PATCH 1/7] blkdebug: Add 'delay-ns' option Date: Tue, 21 Apr 2026 18:11:26 +0200 Message-ID: <20260421161132.99878-2-kwolf@redhat.com> In-Reply-To: <20260421161132.99878-1-kwolf@redhat.com> References: <20260421161132.99878-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_PASS=-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.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1776788013494158500 Content-Type: text/plain; charset="utf-8" Sometimes reproducing a problem for debugging involves slow I/O, so let's add something to blkdebug to make I/O slow when we need it. This can be used either together with an error so that the request fails after the delay, or with errno=3D0, which allows the request to succeed after the delay. Signed-off-by: Kevin Wolf --- qapi/block-core.json | 4 ++++ block/blkdebug.c | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index 508b081ac16..0efd51787b4 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3919,6 +3919,9 @@ # # @errno: error identifier (errno) to be returned; defaults to EIO # +# @delay-ns: request delay before completion in nanoseconds +# (default: 0, since: 11.1) +# # @sector: specifies the sector index which has to be affected in # order to actually trigger the event; defaults to "any sector" # @@ -3934,6 +3937,7 @@ '*state': 'int', '*iotype': 'BlkdebugIOType', '*errno': 'int', + '*delay-ns': 'int', '*sector': 'int', '*once': 'bool', '*immediately': 'bool' } } diff --git a/block/blkdebug.c b/block/blkdebug.c index 8a4a8cb85ea..b07c67611a5 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -95,6 +95,7 @@ typedef struct BlkdebugRule { int immediately; int once; int64_t offset; + int64_t delay_ns; } inject; struct { int new_state; @@ -144,6 +145,10 @@ static QemuOptsList inject_error_opts =3D { .name =3D "immediately", .type =3D QEMU_OPT_BOOL, }, + { + .name =3D "delay-ns", + .type =3D QEMU_OPT_NUMBER, + }, { /* end of list */ } }, }; @@ -216,6 +221,8 @@ static int add_rule(void *opaque, QemuOpts *opts, Error= **errp) rule->options.inject.once =3D qemu_opt_get_bool(opts, "once", 0); rule->options.inject.immediately =3D qemu_opt_get_bool(opts, "immediately", 0); + rule->options.inject.delay_ns =3D + qemu_opt_get_number(opts, "delay-ns", 0); sector =3D qemu_opt_get_number(opts, "sector", -1); rule->options.inject.offset =3D sector =3D=3D -1 ? -1 : sector * BDRV_SECTOR_SIZE; @@ -594,6 +601,7 @@ static int coroutine_fn rule_check(BlockDriverState *bs= , uint64_t offset, BlkdebugRule *rule =3D NULL; int error; bool immediately; + int64_t delay_ns; =20 qemu_mutex_lock(&s->lock); QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { @@ -608,13 +616,14 @@ static int coroutine_fn rule_check(BlockDriverState *= bs, uint64_t offset, } } =20 - if (!rule || !rule->options.inject.error) { + if (!rule) { qemu_mutex_unlock(&s->lock); return 0; } =20 immediately =3D rule->options.inject.immediately; error =3D rule->options.inject.error; + delay_ns =3D rule->options.inject.delay_ns; =20 if (rule->options.inject.once) { QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next); @@ -622,6 +631,10 @@ static int coroutine_fn rule_check(BlockDriverState *b= s, uint64_t offset, } =20 qemu_mutex_unlock(&s->lock); + + if (delay_ns) { + qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, delay_ns); + } if (!immediately) { aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_sel= f()); qemu_coroutine_yield(); --=20 2.53.0 From nobody Sat May 30 20:11:38 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1776788002; cv=none; d=zohomail.com; s=zohoarc; b=OotzFupOj13UqWKltlh/CmkarPxRsm2a8N3lrvIhlUn1NsFb0Br/EdYF6N0OO0KzblhvD2Bki1iabHI0zMCA0vI1MMKEKqNTAjcrZd698hXe0vSLbp39ZFUW4fer2dU0KOmFv8RF39k9ZolsOsMKr6KqYP6HvwAalT1/Kkl+bfs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776788002; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=HaurCmmcFcdQqODCgdgVOtbs7i0XHZRM1ttP+Dd/wVI=; b=KxGTRCPJZ6iILlsxRA9hQst2cSpV6Givsin9IPVNPwCG3eatKKFDClyRiqAWj1aqvFPpjQOh7MpQ5TB2PZUXP4brUhHjtHkwfAbi8jDnS4ffckFuLguQyDVj2LbDRMB/MrKNDayV/lnxHtXKkMSuhNPnpupvBOVqMh3zWNhXnH8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1776788002649629.5246217841164; Tue, 21 Apr 2026 09:13:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wFDi3-0001P4-IK; Tue, 21 Apr 2026 12:12:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDi0-0001LL-3a for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:20 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDhu-0000FF-Va for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:19 -0400 Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-126-pWNiQMyoNfeU1BPgLa0LSA-1; Tue, 21 Apr 2026 12:12:07 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2DF961955F0B; Tue, 21 Apr 2026 16:12:06 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.44.49.173]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 59AAC180045E; Tue, 21 Apr 2026 16:12:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1776787933; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HaurCmmcFcdQqODCgdgVOtbs7i0XHZRM1ttP+Dd/wVI=; b=LPzFuJe4NvC0sYrkIkICyd48gBASD4agI+aYSpbtfcMDtJRnZ7HLn9CbFRbJjh3oSRkxV6 H/8vzVWLy6gQBC8X5AMvh/pZr3gGT+b/NwM4te3FhrqN4mAF2n4kAncxApQiLhwDpros9p d5yfSQVGearvBt6uyj6Gn6YWRpCPZR0= X-MC-Unique: pWNiQMyoNfeU1BPgLa0LSA-1 X-Mimecast-MFC-AGG-ID: pWNiQMyoNfeU1BPgLa0LSA_1776787926 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, jsnow@redhat.com, qemu-devel@nongnu.org, qemu-stable@nongnu.org Subject: [PATCH 2/7] block: Add blk_co_start/end_request() and BDRV_REQ_NO_QUEUE Date: Tue, 21 Apr 2026 18:11:27 +0200 Message-ID: <20260421161132.99878-3-kwolf@redhat.com> In-Reply-To: <20260421161132.99878-1-kwolf@redhat.com> References: <20260421161132.99878-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_PASS=-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.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1776788003467158500 Content-Type: text/plain; charset="utf-8" If a device uses blk_inc/dec_in_flight() in order to build macro operations that involve multiple requests for the block layer and that need to be completed as a unit before the BlockBackend can be considered drained, it sets the stage for a deadlock: When a drain is requested, the inner request at the BlockBackend level will be queued in blk_wait_while_drained() and wait until the drained section ends, but at the same time, drain_begin can only return if the whole macro operation at the device level has completed. Introduce a new interface to allow implementing the logic correctly: Instead of queueing individual requests, blk_co_start_request() calls blk_wait_while_drained() once at the beginning. The individual requests must then set BDRV_REQ_NO_QUEUE to avoid being queued and running into the deadlock; being wrapped in blk_co_start/end_request() makes sure that drain_begin waits for them and they don't sneak in when the BlockBackend is supposed to already be quiescent. Signed-off-by: Kevin Wolf --- include/block/block-common.h | 11 ++++++++- include/system/block-backend-io.h | 2 ++ block/block-backend.c | 38 +++++++++++++++++++++++-------- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/include/block/block-common.h b/include/block/block-common.h index c8c626daeaa..895ea175413 100644 --- a/include/block/block-common.h +++ b/include/block/block-common.h @@ -215,8 +215,17 @@ typedef enum { */ BDRV_REQ_NO_WAIT =3D 0x400, =20 + /* + * Used between blk_co_start_request() and blk_end_request() to avoid + * that the request waits in a drained BlockBackend until the drained + * section ends. Waiting would cause a deadlock because drain waits for + * blk_end_request() to be called, but the request never completes + * because it waits for the drain to end. + */ + BDRV_REQ_NO_QUEUE =3D 0x800, + /* Mask of valid flags */ - BDRV_REQ_MASK =3D 0x7ff, + BDRV_REQ_MASK =3D 0xfff, } BdrvRequestFlags; =20 #define BDRV_O_NO_SHARE 0x0001 /* don't share permissions */ diff --git a/include/system/block-backend-io.h b/include/system/block-backe= nd-io.h index 6d5ac476fc0..0248c1c36e2 100644 --- a/include/system/block-backend-io.h +++ b/include/system/block-backend-io.h @@ -71,6 +71,8 @@ BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned lon= g int req, void *buf, =20 void blk_inc_in_flight(BlockBackend *blk); void blk_dec_in_flight(BlockBackend *blk); +void coroutine_fn blk_co_start_request(BlockBackend *blk); +void blk_end_request(BlockBackend *blk); =20 bool coroutine_fn GRAPH_RDLOCK blk_co_is_inserted(BlockBackend *blk); bool co_wrapper_mixed_bdrv_rdlock blk_is_inserted(BlockBackend *blk); diff --git a/block/block-backend.c b/block/block-backend.c index 99446571201..ee00440e28d 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -82,6 +82,7 @@ struct BlockBackend { QemuMutex queued_requests_lock; /* protects queued_requests */ CoQueue queued_requests; bool disable_request_queuing; /* atomic */ + int start_request_count; /* atomic */ =20 VMChangeStateEntry *vmsh; bool force_allow_inactivate; @@ -1306,10 +1307,16 @@ bool blk_in_drain(BlockBackend *blk) } =20 /* To be called between exactly one pair of blk_inc/dec_in_flight() */ -static void coroutine_fn blk_wait_while_drained(BlockBackend *blk) +static void coroutine_fn blk_wait_while_drained(BlockBackend *blk, + BdrvRequestFlags flags) { assert(blk->in_flight > 0); =20 + if (flags & BDRV_REQ_NO_QUEUE) { + assert(qatomic_read(&blk->start_request_count)); + return; + } + if (qatomic_read(&blk->quiesce_counter) && !qatomic_read(&blk->disable_request_queuing)) { /* @@ -1335,7 +1342,7 @@ blk_co_do_preadv_part(BlockBackend *blk, int64_t offs= et, int64_t bytes, BlockDriverState *bs; IO_CODE(); =20 - blk_wait_while_drained(blk); + blk_wait_while_drained(blk, flags); GRAPH_RDLOCK_GUARD(); =20 /* Call blk_bs() only after waiting, the graph may have changed */ @@ -1410,7 +1417,7 @@ blk_co_do_pwritev_part(BlockBackend *blk, int64_t off= set, int64_t bytes, BlockDriverState *bs; IO_CODE(); =20 - blk_wait_while_drained(blk); + blk_wait_while_drained(blk, flags); GRAPH_RDLOCK_GUARD(); =20 /* Call blk_bs() only after waiting, the graph may have changed */ @@ -1523,6 +1530,19 @@ void blk_dec_in_flight(BlockBackend *blk) aio_wait_kick(); } =20 +void coroutine_fn blk_co_start_request(BlockBackend *blk) +{ + blk_inc_in_flight(blk); + blk_wait_while_drained(blk, 0); + qatomic_inc(&blk->start_request_count); +} + +void blk_end_request(BlockBackend *blk) +{ + qatomic_dec(&blk->start_request_count); + blk_dec_in_flight(blk); +} + static void error_callback_bh(void *opaque) { struct BlockBackendAIOCB *acb =3D opaque; @@ -1741,7 +1761,7 @@ blk_co_do_ioctl(BlockBackend *blk, unsigned long int = req, void *buf) { IO_CODE(); =20 - blk_wait_while_drained(blk); + blk_wait_while_drained(blk, 0); GRAPH_RDLOCK_GUARD(); =20 if (!blk_co_is_available(blk)) { @@ -1788,7 +1808,7 @@ blk_co_do_pdiscard(BlockBackend *blk, int64_t offset,= int64_t bytes) int ret; IO_CODE(); =20 - blk_wait_while_drained(blk); + blk_wait_while_drained(blk, 0); GRAPH_RDLOCK_GUARD(); =20 ret =3D blk_check_byte_request(blk, offset, bytes); @@ -1834,7 +1854,7 @@ int coroutine_fn blk_co_pdiscard(BlockBackend *blk, i= nt64_t offset, static int coroutine_fn blk_co_do_flush(BlockBackend *blk) { IO_CODE(); - blk_wait_while_drained(blk); + blk_wait_while_drained(blk, 0); GRAPH_RDLOCK_GUARD(); =20 if (!blk_co_is_available(blk)) { @@ -2009,7 +2029,7 @@ int coroutine_fn blk_co_zone_report(BlockBackend *blk= , int64_t offset, IO_CODE(); =20 blk_inc_in_flight(blk); /* increase before waiting */ - blk_wait_while_drained(blk); + blk_wait_while_drained(blk, 0); GRAPH_RDLOCK_GUARD(); if (!blk_is_available(blk)) { blk_dec_in_flight(blk); @@ -2034,7 +2054,7 @@ int coroutine_fn blk_co_zone_mgmt(BlockBackend *blk, = BlockZoneOp op, IO_CODE(); =20 blk_inc_in_flight(blk); - blk_wait_while_drained(blk); + blk_wait_while_drained(blk, 0); GRAPH_RDLOCK_GUARD(); =20 ret =3D blk_check_byte_request(blk, offset, len); @@ -2058,7 +2078,7 @@ int coroutine_fn blk_co_zone_append(BlockBackend *blk= , int64_t *offset, IO_CODE(); =20 blk_inc_in_flight(blk); - blk_wait_while_drained(blk); + blk_wait_while_drained(blk, flags); GRAPH_RDLOCK_GUARD(); if (!blk_is_available(blk)) { blk_dec_in_flight(blk); --=20 2.53.0 From nobody Sat May 30 20:11:38 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1776788026; cv=none; d=zohomail.com; s=zohoarc; b=JqNDCTffjl0cheW5gpfUl7pL7Vx3mOERurUQxjeRQfKlh6GM2Ako/KML2AepK6i1NLMnTcPTEnQMFES8RaBM9J9zJm3WCOYFkWB4iQFLiqeP3v5RHTz3ddrSWiqQ5ARHDwfMqjx/qstjoSALJ2KJlrRtxbAO5bwqcS3pUBGkioc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776788026; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=WhqC91BLULK2bubCupahOYep/yxmCAr4UDM1WsqQ1NI=; b=aA5W443LSH+OwsLW3a9xGmMA/VUDcBcein5tmPC4whjn7qt67StmNDCcMLXYpeLxUohQdZdczwB3+M2oVCenDoSscZFkl44WVTHT506IM3jHtC0IOCm8k9xW+uEyd7jqFtgRVg+m6Zop4rkgTecqED3mNv/z9Uco+oGpOystliE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1776788026591309.84758614143607; Tue, 21 Apr 2026 09:13:46 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wFDiE-0001SU-C3; Tue, 21 Apr 2026 12:12:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDi3-0001PT-Nd for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDhz-0000In-S6 for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:23 -0400 Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-307-xx__dSBGOTqP3JBQmVcE7Q-1; Tue, 21 Apr 2026 12:12:09 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 63DBA19560B7; Tue, 21 Apr 2026 16:12:08 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.44.49.173]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9651D1800660; Tue, 21 Apr 2026 16:12:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1776787938; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WhqC91BLULK2bubCupahOYep/yxmCAr4UDM1WsqQ1NI=; b=Ak03Jnnz2H0HfUHsuyHeepkQkN0noJe6qEh6AB9mh8iPgCgBjHoktho56OkDaVz2nqGG2t tukukMxIN2VNhBLAK7w/u0gkwTMQiMrSu7Ll4eMVmHWlMTMAMeWy/iK9Gy0l2WJDCeJ30d cAb75nqLgLbTmEIVPhOjGoQ2/eb8rlU= X-MC-Unique: xx__dSBGOTqP3JBQmVcE7Q-1 X-Mimecast-MFC-AGG-ID: xx__dSBGOTqP3JBQmVcE7Q_1776787928 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, jsnow@redhat.com, qemu-devel@nongnu.org, qemu-stable@nongnu.org Subject: [PATCH 3/7] block: Add flags parameter to blk_*_pdiscard() Date: Tue, 21 Apr 2026 18:11:28 +0200 Message-ID: <20260421161132.99878-4-kwolf@redhat.com> In-Reply-To: <20260421161132.99878-1-kwolf@redhat.com> References: <20260421161132.99878-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-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.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1776788029283154100 Content-Type: text/plain; charset="utf-8" All existing callers pass 0, but we need a way to pass BDRV_REQ_NO_QUEUE for discard requests. Signed-off-by: Kevin Wolf --- include/system/block-backend-io.h | 4 ++-- block/block-backend.c | 11 ++++++----- block/export/virtio-blk-handler.c | 2 +- block/mirror.c | 4 ++-- nbd/server.c | 2 +- qemu-io-cmds.c | 2 +- tests/unit/test-block-iothread.c | 4 ++-- 7 files changed, 15 insertions(+), 14 deletions(-) diff --git a/include/system/block-backend-io.h b/include/system/block-backe= nd-io.h index 0248c1c36e2..fd84723d9d0 100644 --- a/include/system/block-backend-io.h +++ b/include/system/block-backend-io.h @@ -218,9 +218,9 @@ int co_wrapper_mixed blk_zone_append(BlockBackend *blk,= int64_t *offset, BdrvRequestFlags flags); =20 int co_wrapper_mixed blk_pdiscard(BlockBackend *blk, int64_t offset, - int64_t bytes); + int64_t bytes, BdrvRequestFlags flags); int coroutine_fn blk_co_pdiscard(BlockBackend *blk, int64_t offset, - int64_t bytes); + int64_t bytes, BdrvRequestFlags flags); =20 int co_wrapper_mixed blk_flush(BlockBackend *blk); int coroutine_fn blk_co_flush(BlockBackend *blk); diff --git a/block/block-backend.c b/block/block-backend.c index ee00440e28d..37ba7e9fc40 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -1803,12 +1803,13 @@ BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsign= ed long int req, void *buf, =20 /* To be called between exactly one pair of blk_inc/dec_in_flight() */ static int coroutine_fn -blk_co_do_pdiscard(BlockBackend *blk, int64_t offset, int64_t bytes) +blk_co_do_pdiscard(BlockBackend *blk, int64_t offset, int64_t bytes, + BdrvRequestFlags flags) { int ret; IO_CODE(); =20 - blk_wait_while_drained(blk, 0); + blk_wait_while_drained(blk, flags); GRAPH_RDLOCK_GUARD(); =20 ret =3D blk_check_byte_request(blk, offset, bytes); @@ -1824,7 +1825,7 @@ static void coroutine_fn blk_aio_pdiscard_entry(void = *opaque) BlkAioEmAIOCB *acb =3D opaque; BlkRwCo *rwco =3D &acb->rwco; =20 - rwco->ret =3D blk_co_do_pdiscard(rwco->blk, rwco->offset, acb->bytes); + rwco->ret =3D blk_co_do_pdiscard(rwco->blk, rwco->offset, acb->bytes, = 0); blk_aio_complete(acb); } =20 @@ -1838,13 +1839,13 @@ BlockAIOCB *blk_aio_pdiscard(BlockBackend *blk, } =20 int coroutine_fn blk_co_pdiscard(BlockBackend *blk, int64_t offset, - int64_t bytes) + int64_t bytes, BdrvRequestFlags flags) { int ret; IO_OR_GS_CODE(); =20 blk_inc_in_flight(blk); - ret =3D blk_co_do_pdiscard(blk, offset, bytes); + ret =3D blk_co_do_pdiscard(blk, offset, bytes, flags); blk_dec_in_flight(blk); =20 return ret; diff --git a/block/export/virtio-blk-handler.c b/block/export/virtio-blk-ha= ndler.c index 3dd6c43af1a..eaa6fc19067 100644 --- a/block/export/virtio-blk-handler.c +++ b/block/export/virtio-blk-handler.c @@ -122,7 +122,7 @@ virtio_blk_discard_write_zeroes(VirtioBlkHandler *handl= er, struct iovec *iov, } =20 if (blk_co_pdiscard(blk, sector << VIRTIO_BLK_SECTOR_BITS, - bytes) =3D=3D 0) { + bytes, 0) =3D=3D 0) { return VIRTIO_BLK_S_OK; } } diff --git a/block/mirror.c b/block/mirror.c index 2fcded9e93d..089856f4a84 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -454,7 +454,7 @@ static void coroutine_fn mirror_co_discard(void *opaque) *op->bytes_handled =3D op->bytes; op->is_in_flight =3D true; =20 - ret =3D blk_co_pdiscard(op->s->target, op->offset, op->bytes); + ret =3D blk_co_pdiscard(op->s->target, op->offset, op->bytes, 0); mirror_write_complete(op, ret); } =20 @@ -1532,7 +1532,7 @@ do_sync_target_write(MirrorBlockJob *job, MirrorMetho= d method, zero_bitmap_end - zero_bitmap_offset); } assert(!qiov); - ret =3D blk_co_pdiscard(job->target, offset, bytes); + ret =3D blk_co_pdiscard(job->target, offset, bytes, 0); break; =20 default: diff --git a/nbd/server.c b/nbd/server.c index 620097c58ca..78ec9844097 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -2990,7 +2990,7 @@ static coroutine_fn int nbd_handle_request(NBDClient = *client, "flush failed", errp); =20 case NBD_CMD_TRIM: - ret =3D blk_co_pdiscard(exp->common.blk, request->from, request->l= en); + ret =3D blk_co_pdiscard(exp->common.blk, request->from, request->l= en, 0); if (ret >=3D 0 && request->flags & NBD_CMD_FLAG_FUA) { ret =3D blk_co_flush(exp->common.blk); } diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c index 13e03301624..f6d077908f2 100644 --- a/qemu-io-cmds.c +++ b/qemu-io-cmds.c @@ -2201,7 +2201,7 @@ static int discard_f(BlockBackend *blk, int argc, cha= r **argv) } =20 clock_gettime(CLOCK_MONOTONIC, &t1); - ret =3D blk_pdiscard(blk, offset, bytes); + ret =3D blk_pdiscard(blk, offset, bytes, 0); clock_gettime(CLOCK_MONOTONIC, &t2); =20 if (ret < 0) { diff --git a/tests/unit/test-block-iothread.c b/tests/unit/test-block-iothr= ead.c index e26b3be5939..5273ff235a2 100644 --- a/tests/unit/test-block-iothread.c +++ b/tests/unit/test-block-iothread.c @@ -270,11 +270,11 @@ static void test_sync_op_blk_pdiscard(BlockBackend *b= lk) int ret; =20 /* Early success: UNMAP not supported */ - ret =3D blk_pdiscard(blk, 0, 512); + ret =3D blk_pdiscard(blk, 0, 512, 0); g_assert_cmpint(ret, =3D=3D, 0); =20 /* Early error: Negative offset */ - ret =3D blk_pdiscard(blk, -2, 512); + ret =3D blk_pdiscard(blk, -2, 512, 0); g_assert_cmpint(ret, =3D=3D, -EIO); } =20 --=20 2.53.0 From nobody Sat May 30 20:11:38 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1776788027; cv=none; d=zohomail.com; s=zohoarc; b=i0uN++DAy0JK9BzZGr3D1pYPpauVepVN2hGIV3MO+QvsfYjjJBxJ+aAr+1wbS8zoadJz9W9z2t/dpBPICLXja7hu9r3pDJCRMNQ+4jfV5Ye2/G01jt+540vONu55zLUq58ZlXOwSrvNMywhBoeIWFrPR6SwxnC4jxkuMkyY5Ehg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776788027; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=vj54Ak3FmGNSQPY81ZQ81Ux00FAoPnunMSojWp63cjA=; b=Bsz1vYskHfaYFyilYo9v1sRkCAPva+/Usvuoi1TpcYxxQjqrTaVSo3e9TV/eKA4MmaFtKyutpUA35OqK/82KWGIe9gNCKzRher1JstAQO1LLspEmaeocxIqO53qCJZ4DsdnZU+GgvZ2PjFyJ6fb6eLKlLwE7RZF2i4l/F1H+x4Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1776788026941605.1567069962218; Tue, 21 Apr 2026 09:13:46 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wFDi1-0001My-GV; Tue, 21 Apr 2026 12:12:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDi0-0001LM-3c for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:20 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDhv-0000FI-0t for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:19 -0400 Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-154-M394sVNXPwG4yesRc778kQ-1; Tue, 21 Apr 2026 12:12:11 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 983041955F05; Tue, 21 Apr 2026 16:12:10 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.44.49.173]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id CBA171800351; Tue, 21 Apr 2026 16:12:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1776787934; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vj54Ak3FmGNSQPY81ZQ81Ux00FAoPnunMSojWp63cjA=; b=NnPokfQXTTPD+iCc7D+UpPYn27bgBcn0T6N/ZorZZZ2dJktVY+vRiRQ9w99VNxZto45NjA C3TOM/CTkf/YjlHs7zUuDKcjpcsSMrrfMMVmA1/qeo35cIm0H+vttkXWzmFKjB3Yk6PPsk SILdDyswch2rh8xRgZGxHQuV7Tdeh/0= X-MC-Unique: M394sVNXPwG4yesRc778kQ-1 X-Mimecast-MFC-AGG-ID: M394sVNXPwG4yesRc778kQ_1776787930 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, jsnow@redhat.com, qemu-devel@nongnu.org, qemu-stable@nongnu.org Subject: [PATCH 4/7] ide: Minimal fix for deadlock between TRIM and drain Date: Tue, 21 Apr 2026 18:11:29 +0200 Message-ID: <20260421161132.99878-5-kwolf@redhat.com> In-Reply-To: <20260421161132.99878-1-kwolf@redhat.com> References: <20260421161132.99878-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_PASS=-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.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1776788029523158500 Content-Type: text/plain; charset="utf-8" The implementation of TRIM in IDE can chain multiple discard requests and uses blk_inc/dec_in_flight() to make sure that the whole TRIM operation has completed when the device needs to be quiescent (e.g. for the drain when performing an IDE reset, it would be bad if an IDE request like TRIM were still in flight). The problem is that each drain request calls blk_wait_while_drained() and when draining, it waits until the drained section ends. At the same time, drain_begin can only return if the whole TRIM operation has completed. This is a classic deadlock. Use blk_co_start/end_request() and BDRV_REQ_NO_QUEUE to avoid the problem. This requires moving the TRIM state machine to a coroutine. This commit does the minimal conversion so that we do have a coroutine that works for the fix, but it still looks much like a callback-based implementation. This will be cleaned up in the next patch. Cc: qemu-stable@nongnu.org Fixes: 7e5cdb345f77 ('ide: Increment BB in-flight counter for TRIM BH') Buglink: https://redhat.atlassian.net/browse/RHEL-121686 Signed-off-by: Kevin Wolf --- hw/ide/core.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/hw/ide/core.c b/hw/ide/core.c index 7a15d6cac9b..48359c934c1 100644 --- a/hw/ide/core.c +++ b/hw/ide/core.c @@ -420,7 +420,6 @@ typedef struct TrimAIOCB { QEMUBH *bh; int ret; QEMUIOVector *qiov; - BlockAIOCB *aiocb; int i, j; } TrimAIOCB; =20 @@ -433,11 +432,6 @@ static void trim_aio_cancel(BlockAIOCB *acb) iocb->i =3D (iocb->qiov->iov[iocb->j].iov_len / 8) - 1; =20 iocb->ret =3D -ECANCELED; - - if (iocb->aiocb) { - blk_aio_cancel_async(iocb->aiocb); - iocb->aiocb =3D NULL; - } } =20 static const AIOCBInfo trim_aiocb_info =3D { @@ -456,15 +450,20 @@ static void ide_trim_bh_cb(void *opaque) iocb->bh =3D NULL; qemu_aio_unref(iocb); =20 - /* Paired with an increment in ide_issue_trim() */ - blk_dec_in_flight(blk); + /* Paired with blk_co_start_request in ide_trim_co_entry() */ + blk_end_request(blk); } =20 -static void ide_issue_trim_cb(void *opaque, int ret) +static void coroutine_fn ide_trim_co_entry(void *opaque) { TrimAIOCB *iocb =3D opaque; IDEState *s =3D iocb->s; + int ret =3D 0; + + /* Paired with blk_end_request in ide_trim_bh_cb() */ + blk_co_start_request(s->blk); =20 +loop: if (iocb->i >=3D 0) { if (ret >=3D 0) { block_acct_done(blk_get_stats(s->blk), &s->acct); @@ -499,11 +498,11 @@ static void ide_issue_trim_cb(void *opaque, int ret) count << BDRV_SECTOR_BITS, BLOCK_ACCT_UNM= AP); =20 /* Got an entry! Submit and exit. */ - iocb->aiocb =3D blk_aio_pdiscard(s->blk, - sector << BDRV_SECTOR_BITS, - count << BDRV_SECTOR_BITS, - ide_issue_trim_cb, opaque); - return; + ret =3D blk_co_pdiscard(s->blk, + sector << BDRV_SECTOR_BITS, + count << BDRV_SECTOR_BITS, + BDRV_REQ_NO_QUEUE); + goto loop; } =20 iocb->j++; @@ -514,7 +513,6 @@ static void ide_issue_trim_cb(void *opaque, int ret) } =20 done: - iocb->aiocb =3D NULL; if (iocb->bh) { replay_bh_schedule_event(iocb->bh); } @@ -527,9 +525,7 @@ BlockAIOCB *ide_issue_trim( IDEState *s =3D opaque; IDEDevice *dev =3D s->unit ? s->bus->slave : s->bus->master; TrimAIOCB *iocb; - - /* Paired with a decrement in ide_trim_bh_cb() */ - blk_inc_in_flight(s->blk); + Coroutine *co; =20 iocb =3D blk_aio_get(&trim_aiocb_info, s->blk, cb, cb_opaque); iocb->s =3D s; @@ -539,7 +535,10 @@ BlockAIOCB *ide_issue_trim( iocb->qiov =3D qiov; iocb->i =3D -1; iocb->j =3D 0; - ide_issue_trim_cb(iocb, 0); + + co =3D qemu_coroutine_create(ide_trim_co_entry, iocb); + aio_co_enter(qemu_get_current_aio_context(), co); + return &iocb->common; } =20 --=20 2.53.0 From nobody Sat May 30 20:11:38 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1776788024; cv=none; d=zohomail.com; s=zohoarc; b=L7qgLOeb2EB83zPlyfjKjwqnJR7Qm1Z9Nn+rvWMIpSAEfeJtmVb9HHLAR+FzMiT9Gvb0/XW5uwfhaSkSEDsSU+Y5LZkdplCuCrimnRCRiZPUTfRX2xiVcmh8zd/X6+xCBfgiq82tCRCh5neuuVv+35LfX/2gv9mj2poRjShMoFY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776788024; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=A4dsw4jNX6Z6/lLNoJG/cG5lUqCzWJZiyxEpvX7UznA=; b=V6s73GU/PmXd16LDpOKPje1ZcAP0jVB2kTm0pK6tRt77DQUwQbZs6+J/A9tp9BYljOj1q+mB/nkH2OKumEQT1QQAD06I85SCFGG5UTggO6nguOo9chPIWpBWs6UP9s/ltm3e8cUp8Qll8Ywn6LntMxT2xHLDaHMNx0kYxP2vnwQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1776788024555921.6299374856225; Tue, 21 Apr 2026 09:13:44 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wFDiV-0001a0-9D; Tue, 21 Apr 2026 12:12:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDi4-0001PZ-80 for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDi1-0000Jn-5a for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:23 -0400 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-424-owI0DaL8O9yGDJy5QFwhYA-1; Tue, 21 Apr 2026 12:12:15 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CD9301800473; Tue, 21 Apr 2026 16:12:12 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.44.49.173]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0A0211800351; Tue, 21 Apr 2026 16:12:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1776787940; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=A4dsw4jNX6Z6/lLNoJG/cG5lUqCzWJZiyxEpvX7UznA=; b=Ff+Fik/0YGPrpTxaqOJFnb8nrbjrvwx0fHcapBWoZwnd3mYknjIYFA/G+fiMokDu5l4FbG irkwdLkNS4cVGYE1kGE1d1mcmwgatm8htETLrM6ax8+aTeeoSo/xwD5L4h/dT+vrqXox89 vUXLMehYO6jhyUbLJqsgC6d1A+QIKAA= X-MC-Unique: owI0DaL8O9yGDJy5QFwhYA-1 X-Mimecast-MFC-AGG-ID: owI0DaL8O9yGDJy5QFwhYA_1776787934 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, jsnow@redhat.com, qemu-devel@nongnu.org, qemu-stable@nongnu.org Subject: [PATCH 5/7] ide: Clean up ide_trim_co_entry() to be idiomatic coroutine code Date: Tue, 21 Apr 2026 18:11:30 +0200 Message-ID: <20260421161132.99878-6-kwolf@redhat.com> In-Reply-To: <20260421161132.99878-1-kwolf@redhat.com> References: <20260421161132.99878-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_PASS=-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.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1776788027194154100 Content-Type: text/plain; charset="utf-8" The previous commit did a minimal conversion of the callback based state machine for TRIM to a coroutine in order to fix a bug. Refactor it to actually look like normal coroutine based code, which improves its readability. Signed-off-by: Kevin Wolf --- hw/ide/core.c | 87 +++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 48 deletions(-) diff --git a/hw/ide/core.c b/hw/ide/core.c index 48359c934c1..f78b00220b8 100644 --- a/hw/ide/core.c +++ b/hw/ide/core.c @@ -420,18 +420,15 @@ typedef struct TrimAIOCB { QEMUBH *bh; int ret; QEMUIOVector *qiov; - int i, j; + bool canceled; } TrimAIOCB; =20 static void trim_aio_cancel(BlockAIOCB *acb) { TrimAIOCB *iocb =3D container_of(acb, TrimAIOCB, common); =20 - /* Exit the loop so ide_issue_trim_cb will not continue */ - iocb->j =3D iocb->qiov->niov - 1; - iocb->i =3D (iocb->qiov->iov[iocb->j].iov_len / 8) - 1; - - iocb->ret =3D -ECANCELED; + /* Exit the loop so ide_trim_co_entry will not continue */ + iocb->canceled =3D true; } =20 static const AIOCBInfo trim_aiocb_info =3D { @@ -458,60 +455,55 @@ static void coroutine_fn ide_trim_co_entry(void *opaq= ue) { TrimAIOCB *iocb =3D opaque; IDEState *s =3D iocb->s; - int ret =3D 0; + int i, j; + int ret; =20 /* Paired with blk_end_request in ide_trim_bh_cb() */ blk_co_start_request(s->blk); =20 -loop: - if (iocb->i >=3D 0) { - if (ret >=3D 0) { - block_acct_done(blk_get_stats(s->blk), &s->acct); - } else { - block_acct_failed(blk_get_stats(s->blk), &s->acct); - } - } + for (j =3D 0; j < iocb->qiov->niov; j++) { + for (i =3D 0; i < iocb->qiov->iov[j].iov_len / 8; i++) { + uint64_t *buffer =3D iocb->qiov->iov[j].iov_base; =20 - if (ret >=3D 0) { - while (iocb->j < iocb->qiov->niov) { - int j =3D iocb->j; - while (++iocb->i < iocb->qiov->iov[j].iov_len / 8) { - int i =3D iocb->i; - uint64_t *buffer =3D iocb->qiov->iov[j].iov_base; + /* 6-byte LBA + 2-byte range per entry */ + uint64_t entry =3D le64_to_cpu(buffer[i]); + uint64_t sector =3D entry & 0x0000ffffffffffffULL; + uint16_t count =3D entry >> 48; =20 - /* 6-byte LBA + 2-byte range per entry */ - uint64_t entry =3D le64_to_cpu(buffer[i]); - uint64_t sector =3D entry & 0x0000ffffffffffffULL; - uint16_t count =3D entry >> 48; + if (count =3D=3D 0) { + continue; + } =20 - if (count =3D=3D 0) { - continue; - } + if (iocb->canceled) { + iocb->ret =3D -ECANCELED; + goto done; + } =20 - if (!ide_sect_range_ok(s, sector, count)) { - block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_U= NMAP); - iocb->ret =3D -EINVAL; - goto done; - } + if (!ide_sect_range_ok(s, sector, count)) { + block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_UNMAP= ); + iocb->ret =3D -EINVAL; + goto done; + } =20 - block_acct_start(blk_get_stats(s->blk), &s->acct, - count << BDRV_SECTOR_BITS, BLOCK_ACCT_UNM= AP); + block_acct_start(blk_get_stats(s->blk), &s->acct, + count << BDRV_SECTOR_BITS, BLOCK_ACCT_UNMAP); =20 - /* Got an entry! Submit and exit. */ - ret =3D blk_co_pdiscard(s->blk, - sector << BDRV_SECTOR_BITS, - count << BDRV_SECTOR_BITS, - BDRV_REQ_NO_QUEUE); - goto loop; + /* Got an entry! Submit and exit. */ + ret =3D blk_co_pdiscard(s->blk, + sector << BDRV_SECTOR_BITS, + count << BDRV_SECTOR_BITS, + BDRV_REQ_NO_QUEUE); + if (ret >=3D 0) { + block_acct_done(blk_get_stats(s->blk), &s->acct); + } else { + iocb->ret =3D ret; + block_acct_failed(blk_get_stats(s->blk), &s->acct); + goto done; } - - iocb->j++; - iocb->i =3D -1; } - } else { - iocb->ret =3D ret; } =20 + iocb->ret =3D 0; done: if (iocb->bh) { replay_bh_schedule_event(iocb->bh); @@ -533,8 +525,7 @@ BlockAIOCB *ide_issue_trim( &DEVICE(dev)->mem_reentrancy_guard); iocb->ret =3D 0; iocb->qiov =3D qiov; - iocb->i =3D -1; - iocb->j =3D 0; + iocb->canceled =3D false; =20 co =3D qemu_coroutine_create(ide_trim_co_entry, iocb); aio_co_enter(qemu_get_current_aio_context(), co); --=20 2.53.0 From nobody Sat May 30 20:11:38 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1776788026; cv=none; d=zohomail.com; s=zohoarc; b=ZRSJkzyl0LalOwv2UDAYS3vhB4De46kELJ8/ARwJiL4qV2mNjiAzGTQbLVVfabUwelLw+ZKAiLsni1HjfY3tMrH1tojP8HeXQdydsIOzTa9aoyJFn3aonKOi1bzp7O4SM/uXh6mSy9cWSzVJ2BclTY4uMJVlQ851O/TOAjwGvdQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776788026; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=mo+0Coln0FK96aKgSWdEDvcPO1Jn4n93jIREd0aBy5Y=; b=UuP83yf2PaiVUIPgpyK9BR0xmFtQMOyqhhwgDlCl6BVbVhWScshjqfOv+2QGOlvHDR6xT3HJMbzKrByXmgA8vDJISYjXwantcUbfVAdhTdlI8eG1nOSRRNcALkgMdFSpHE0lBfJq5Rf7yMELw4BKqWWml1FBnCjELJ7BDn7/UVc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1776788026291910.0316119862612; Tue, 21 Apr 2026 09:13:46 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wFDiK-0001VQ-Tj; Tue, 21 Apr 2026 12:12:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDi5-0001QQ-82 for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:26 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDi2-0000KW-I1 for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:24 -0400 Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-209-cg502Py8Nmq9YH2O1fJSLg-1; Tue, 21 Apr 2026 12:12:16 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0BEAA1800352; Tue, 21 Apr 2026 16:12:15 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.44.49.173]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 401A91800668; Tue, 21 Apr 2026 16:12:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1776787941; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mo+0Coln0FK96aKgSWdEDvcPO1Jn4n93jIREd0aBy5Y=; b=gS1v2tlp7+5pKdGh+AsNyzK83ZYxxRASnfAFOkz+e843yZ2WDbqLdrQqa6uR1P8E99aMWj pYWPQ3rrcLuSHGYhHdifXCSkQoh5eGr+6RrhupoOoj7NtFU7CwIZsLhhoA88iAMpKwnuTV jvDxtC+G+Yyt+sF/eI+3ECi211zvvV0= X-MC-Unique: cg502Py8Nmq9YH2O1fJSLg-1 X-Mimecast-MFC-AGG-ID: cg502Py8Nmq9YH2O1fJSLg_1776787935 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, jsnow@redhat.com, qemu-devel@nongnu.org, qemu-stable@nongnu.org Subject: [PATCH 6/7] ide-test: Factor out wait_dma_completion() Date: Tue, 21 Apr 2026 18:11:31 +0200 Message-ID: <20260421161132.99878-7-kwolf@redhat.com> In-Reply-To: <20260421161132.99878-1-kwolf@redhat.com> References: <20260421161132.99878-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-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.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1776788029234154100 Content-Type: text/plain; charset="utf-8" Signed-off-by: Kevin Wolf --- tests/qtest/ide-test.c | 48 +++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/tests/qtest/ide-test.c b/tests/qtest/ide-test.c index ceee444a9ec..c6dcb2c0745 100644 --- a/tests/qtest/ide-test.c +++ b/tests/qtest/ide-test.c @@ -200,6 +200,34 @@ static uint64_t trim_range_le(uint64_t sector, uint16_= t count) return cpu_to_le64(((uint64_t)count << 48) + sector); } =20 +static uint8_t wait_dma_completion(QTestState *qts, QPCIDevice *dev, + QPCIBar bmdma_bar, QPCIBar ide_bar) +{ + uint8_t status; + + /* Wait for the DMA transfer to complete */ + do { + status =3D qpci_io_readb(dev, bmdma_bar, bmreg_status); + } while ((status & (BM_STS_ACTIVE | BM_STS_INTR)) =3D=3D BM_STS_ACTIVE= ); + + g_assert_cmpint(qtest_get_irq(qts, IDE_PRIMARY_IRQ), =3D=3D, + !!(status & BM_STS_INTR)); + + /* Check IDE status code */ + assert_bit_set(qpci_io_readb(dev, ide_bar, reg_status), DRDY); + assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), BSY | DRQ); + + /* Reading the status register clears the IRQ */ + g_assert(!qtest_get_irq(qts, IDE_PRIMARY_IRQ)); + + /* Stop DMA transfer if still active */ + if (status & BM_STS_ACTIVE) { + qpci_io_writeb(dev, bmdma_bar, bmreg_cmd, 0); + } + + return status; +} + static int send_dma_request(QTestState *qts, int cmd, uint64_t sector, int nb_sectors, PrdtEntry *prdt, int prdt_entr= ies, void(*post_exec)(QPCIDevice *dev, QPCIBar ide_= bar, @@ -280,25 +308,7 @@ static int send_dma_request(QTestState *qts, int cmd, = uint64_t sector, qpci_io_writeb(dev, bmdma_bar, bmreg_cmd, 0); } =20 - /* Wait for the DMA transfer to complete */ - do { - status =3D qpci_io_readb(dev, bmdma_bar, bmreg_status); - } while ((status & (BM_STS_ACTIVE | BM_STS_INTR)) =3D=3D BM_STS_ACTIVE= ); - - g_assert_cmpint(qtest_get_irq(qts, IDE_PRIMARY_IRQ), =3D=3D, - !!(status & BM_STS_INTR)); - - /* Check IDE status code */ - assert_bit_set(qpci_io_readb(dev, ide_bar, reg_status), DRDY); - assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), BSY | DRQ); - - /* Reading the status register clears the IRQ */ - g_assert(!qtest_get_irq(qts, IDE_PRIMARY_IRQ)); - - /* Stop DMA transfer if still active */ - if (status & BM_STS_ACTIVE) { - qpci_io_writeb(dev, bmdma_bar, bmreg_cmd, 0); - } + status =3D wait_dma_completion(qts, dev, bmdma_bar, ide_bar); =20 free_pci_device(dev); =20 --=20 2.53.0 From nobody Sat May 30 20:11:38 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1776788011; cv=none; d=zohomail.com; s=zohoarc; b=H7sEoZ/gGgkx5uHu6QCdx6zd5JsIYB+fg2P4wNwk4kGkHetJ9Y+idc7TIuBgF0kTFvj2v2hiEdxAqls0adbrBlW1kUjXcdga4yL5rOUqLVoHS0SsbcSZ5VsPZTfYR9B0QjoF99KU9vfiId4jZWkrv2Itu98WD3qNxp8DQFbuBgg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776788011; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=dKypboLdCRAggBQbHJnEX0z4rr1+fj30DugdG4Qnd/w=; b=I6fhmzcC5uUXRbUriUMNIQo653POr7y15UOmPddvI+1c3dZJhxD9lJCdmlxWcrXcB4VxiZRayJbhQLOEdTKVIK2ekTbIfCkL3l+gDmyFt47kkKDEzN7/3YSef2sP0sMDH6Wfmuq+CXx8fdsir1roMT71wsmbShsmh2gtFe2hN5Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1776788011601318.37049583798785; Tue, 21 Apr 2026 09:13:31 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wFDiW-0001c0-Dr; Tue, 21 Apr 2026 12:12:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDi5-0001QN-26 for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:26 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wFDi2-0000KQ-Cc for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:12:24 -0400 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-470-SQ7jkUo9PACq2rGa0FJTqg-1; Tue, 21 Apr 2026 12:12:18 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4152C1800605; Tue, 21 Apr 2026 16:12:17 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.44.49.173]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 743561800351; Tue, 21 Apr 2026 16:12:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1776787941; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dKypboLdCRAggBQbHJnEX0z4rr1+fj30DugdG4Qnd/w=; b=AD2FbuEghgNWvMTEjgcd+6gBc5sFSKYktNzhwKh/+yXiRzsBPb4dZon+Kf4eIQnhEO0WIi yfP7F4YSlu0gwxNKX5VHW9ADlL7SwS4scil/z+XkaH6D6yijottxCo+di//+S8CaHXzLQk 9OfTHL6WOYk4KJRp58YtMoZkz6sllEA= X-MC-Unique: SQ7jkUo9PACq2rGa0FJTqg-1 X-Mimecast-MFC-AGG-ID: SQ7jkUo9PACq2rGa0FJTqg_1776787937 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, jsnow@redhat.com, qemu-devel@nongnu.org, qemu-stable@nongnu.org Subject: [PATCH 7/7] ide-test: Test reset during TRIM Date: Tue, 21 Apr 2026 18:11:32 +0200 Message-ID: <20260421161132.99878-8-kwolf@redhat.com> In-Reply-To: <20260421161132.99878-1-kwolf@redhat.com> References: <20260421161132.99878-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_PASS=-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.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1776788013027154100 Content-Type: text/plain; charset="utf-8" This is a regression test for the bug fixed in the previous commits, a deadlock between the drain issued by an IDE reset and the TRIM state machine. Signed-off-by: Kevin Wolf --- tests/qtest/ide-test.c | 95 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 8 deletions(-) diff --git a/tests/qtest/ide-test.c b/tests/qtest/ide-test.c index c6dcb2c0745..721e78170bc 100644 --- a/tests/qtest/ide-test.c +++ b/tests/qtest/ide-test.c @@ -41,8 +41,11 @@ #define IDE_PCI_FUNC 1 =20 #define IDE_BASE 0x1f0 +#define IDE_BASE2 0x3f6 #define IDE_PRIMARY_IRQ 14 =20 +#define IDE_CTRL_RESET 0x04 + #define ATAPI_BLOCK_SIZE 2048 =20 /* How many bytes to receive via ATAPI PIO at one time. @@ -99,6 +102,7 @@ enum { =20 CMDF_ABORT =3D 0x100, CMDF_NO_BM =3D 0x200, + CMDF_NO_WAIT =3D 0x400, }; =20 enum { @@ -228,21 +232,21 @@ static uint8_t wait_dma_completion(QTestState *qts, Q= PCIDevice *dev, return status; } =20 -static int send_dma_request(QTestState *qts, int cmd, uint64_t sector, - int nb_sectors, PrdtEntry *prdt, int prdt_entr= ies, - void(*post_exec)(QPCIDevice *dev, QPCIBar ide_= bar, - uint64_t sector, int nb_secto= rs)) +static int send_dma_request_dev(QTestState *qts, QPCIDevice *dev, + QPCIBar bmdma_bar, QPCIBar ide_bar, int cm= d, + uint64_t sector, int nb_sectors, + PrdtEntry *prdt, int prdt_entries, + void(*post_exec)(QPCIDevice *dev, + QPCIBar ide_bar, + uint64_t sector, + int nb_sectors)) { - QPCIDevice *dev; - QPCIBar bmdma_bar, ide_bar; uintptr_t guest_prdt; size_t len; bool from_dev; uint8_t status; int flags; =20 - dev =3D get_pci_device(qts, &bmdma_bar, &ide_bar); - flags =3D cmd & ~0xff; cmd &=3D 0xff; =20 @@ -308,8 +312,28 @@ static int send_dma_request(QTestState *qts, int cmd, = uint64_t sector, qpci_io_writeb(dev, bmdma_bar, bmreg_cmd, 0); } =20 + if (flags & CMDF_NO_WAIT) { + return 0; + } + status =3D wait_dma_completion(qts, dev, bmdma_bar, ide_bar); =20 + return status; +} + +static int send_dma_request(QTestState *qts, int cmd, uint64_t sector, + int nb_sectors, PrdtEntry *prdt, int prdt_entr= ies, + void(*post_exec)(QPCIDevice *dev, QPCIBar ide_= bar, + uint64_t sector, int nb_secto= rs)) +{ + QPCIDevice *dev; + QPCIBar bmdma_bar, ide_bar; + uint8_t status; + + dev =3D get_pci_device(qts, &bmdma_bar, &ide_bar); + status =3D send_dma_request_dev(qts, dev, bmdma_bar, ide_bar, + cmd, sector, nb_sectors, prdt, prdt_entr= ies, + post_exec); free_pci_device(dev); =20 return status; @@ -457,6 +481,60 @@ static void test_bmdma_trim(void) test_bmdma_teardown(qts); } =20 +static void test_bmdma_trim_reset(void) +{ + QTestState *qts; + QPCIDevice *dev; + QPCIBar bmdma_bar, ide_bar, ide_bar2; + uint8_t status; + const uint64_t trim_range[] =3D { + trim_range_le(0, 2), + trim_range_le(6, 8), + }; + size_t len =3D 512; + uint8_t *buf; + uintptr_t guest_buf; + PrdtEntry prdt[1]; + + qts =3D ide_test_start( + "-blockdev file,filename=3D%s,node-name=3Dimg " + "-blockdev blkdebug,image=3Dimg,node-name=3Ddbg,discard=3Dunmap," + "inject-error.0.event=3Dnone,inject-error.0.iotype=3Ddiscard," + "inject-error.0.errno=3D0,inject-error.0.delay-ns=3D1000000 " + "-device ide-hd,drive=3Ddbg,bus=3Dide.0", + tmp_path[0]); + qtest_irq_intercept_in(qts, "ioapic"); + + guest_buf =3D guest_alloc(&guest_malloc, len); + prdt[0].addr =3D cpu_to_le32(guest_buf), + prdt[0].size =3D cpu_to_le32(len | PRDT_EOT), + + dev =3D get_pci_device(qts, &bmdma_bar, &ide_bar); + ide_bar2 =3D qpci_legacy_iomap(dev, IDE_BASE2); + + buf =3D g_malloc(len); + + /* TRIM request with two segments */ + *((uint64_t *)buf) =3D trim_range[0]; + *((uint64_t *)buf + 1) =3D trim_range[1]; + + qtest_memwrite(qts, guest_buf, buf, 2 * sizeof(uint64_t)); + + send_dma_request_dev(qts, dev, bmdma_bar, ide_bar, CMD_DSM | CMDF_NO_W= AIT, 0, 1, prdt, + ARRAY_SIZE(prdt), NULL); + + /* Reset the device while the first segment is in flight */ + qpci_io_writeb(dev, ide_bar2, 0, IDE_CTRL_RESET); + + status =3D wait_dma_completion(qts, dev, bmdma_bar, ide_bar); + g_assert_cmphex(status, =3D=3D, BM_STS_INTR); + assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); + + free_pci_device(dev); + g_free(buf); + test_bmdma_teardown(qts); +} + /* * This test is developed according to the Programming Interface for * Bus Master IDE Controller (Revision 1.0 5/16/94) @@ -1138,6 +1216,7 @@ int main(int argc, char **argv) =20 qtest_add_func("/ide/bmdma/simple_rw", test_bmdma_simple_rw); qtest_add_func("/ide/bmdma/trim", test_bmdma_trim); + qtest_add_func("/ide/bmdma/trim_reset", test_bmdma_trim_reset); qtest_add_func("/ide/bmdma/various_prdts", test_bmdma_various_prdts); qtest_add_func("/ide/bmdma/no_busmaster", test_bmdma_no_busmaster); =20 --=20 2.53.0