From nobody Tue Feb 10 23:13:16 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=bu.edu ARC-Seal: i=1; a=rsa-sha256; t=1682629914; cv=none; d=zohomail.com; s=zohoarc; b=LLPNQFrq5fmJVDh57JcIVH5wegkm5UkUNkLsmXJ35hnK07/A/yl47hQ5h0AtQemRot0a2HcHDfcAzuB0Dubl4ZpvEw/7zfn/fNbHBqyHj0LibG1ucRrr07PkSrLTMuARken2Lf6i/1Bu27iEF18Mmy5aA0ZDdXmZ0VJoSCVwsBw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1682629914; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=lp25phZ45HRqtDee0Q+1ws/xkoM4Gpnqec2ldOouCHM=; b=XNbKWZFcyEXrAddVpQHWUFeu85I6uMWUji5CiXTvSwZnQk3jrgXgX4KU1CifgWqtrIT31Y7ylv8a/WcvKEGZenxk1RsoVyxcg4aqPPPl/r2XFl8KqIbsy+sZp32wxcib1DhSNroM1MeG1pqPecG7dLYeoKoiUUfb39AsKxVHXao= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1682629914904933.3876715328383; Thu, 27 Apr 2023 14:11:54 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ps8tH-0005vU-3S; Thu, 27 Apr 2023 17:10:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ps8tE-0005v1-Pm for qemu-devel@nongnu.org; Thu, 27 Apr 2023 17:10:56 -0400 Received: from esa13.hc2706-39.iphmx.com ([216.71.137.83]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ps8tC-0005O0-Oo for qemu-devel@nongnu.org; Thu, 27 Apr 2023 17:10:56 -0400 Received: from mail-oi1-f200.google.com ([209.85.167.200]) by ob1.hc2706-39.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 27 Apr 2023 17:10:53 -0400 Received: by mail-oi1-f200.google.com with SMTP id 5614622812f47-38c5845e8e5so6153812b6e.0 for ; Thu, 27 Apr 2023 14:10:52 -0700 (PDT) Received: from mozz.bu.edu (mozz.bu.edu. [128.197.127.33]) by smtp.gmail.com with ESMTPSA id j14-20020ac84c8e000000b003ef3df76ec7sm6483969qtv.93.2023.04.27.14.10.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Apr 2023 14:10:31 -0700 (PDT) X-IronPort-RemoteIP: 209.85.167.200 X-IronPort-MID: 275213208 X-IronPort-Reputation: None X-IronPort-Listener: OutgoingMail X-IronPort-SenderGroup: RELAY_GSUITE X-IronPort-MailFlowPolicy: $RELAYED IronPort-Data: A9a23:QZJlTKkfDfSSZXf88vFrUAjo5gw/JERdPkR7XQ2eYbSJt1+Wr1Gzt xJLXT+EO/3eZDamedglOYXioR8HsZbcmNE2TQVorClhRS4T+ZvOCOrCEkqhZCn6wu8v7a5EA 2fyTvGacajYm1eF/k/F3oDJ9CU6jufQAOKnUoYoAwgpLSd8UiAtlBl/rOAwh49skLCRDhiE/ Nj/uKUzAnf8s9JPGj9SuvPrRC9H5qyo42tE5ANmPpingXeF/5UrJMNHTU2OByagKmVkNrbSb /rOyri/4lTY838FYj9yuu+mGqGiaue60Tmm0xK6aYD76vRxjnVaPpIACRYpQRw/ZwNlPjxG4 I4lWZSYEW/FN0BX8QgXe0Aw/ypWZMWq9FJbSJQWXAP6I0DuKhPRL/tS4E4eYpIcxsBHE1p3s v1ECRpRaCHZ2uWZ3+fuIgVsrpxLwMjDOYoevjRnzGicA6x+GdbMRKLF4dIe1zA17ixMNayGN oxJNHw2MkuGOUUXUrsUIMtWcOOAj33vdTFCgFiI46c7/gA/ySQrj+W3aYGPIoziqcN9t2+0+ lnX0DvDGwAVMtO/6D66wi60ibqa9c/8cMdIfFGizdZzjViOg2AeFhASfV28p/a/lwi5Qd03F qAP0i8nrKx3803yC9enB0X+r3mDsRoRHdFXFoXW9T2w90Yd2C7BbkBsc9KLQIdOWBMeLdDy6 mK0og== IronPort-HdrOrdr: A9a23:2fzk6q6BEtdGHyIilwPXwBXXdLJyesId70hD6qkRc20yTiX8ra qTdZsgpHvJYVoqKRMdcLO7Scq9qBHnm6KdiLN5VduftWHd01eAFoFt6ovkzXnMHSn39uk178 ZdWpJ/BtrxClY/t8r+7AOkFc1I+rm6GcuT9IDjJhlWPGRXQpAlxAd/BwadHAlXaWB9aqbRRK Dw2vZ6 X-Talos-CUID: =?us-ascii?q?9a23=3A92UwyGjYnLG96EGblPy/5MjewTJuLWLZ8FHgMXS?= =?us-ascii?q?BTmc1aZfJT27KpaRLjJ87?= X-Talos-MUID: 9a23:5JPmtAmbCS9h6yXSo00vdnpLHes02LWBV3oUgJoAtOmlMRNAZy+S2WE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bu.edu; s=s1gsbu; t=1682629852; x=1685221852; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lp25phZ45HRqtDee0Q+1ws/xkoM4Gpnqec2ldOouCHM=; b=D1BAdHcOKXPCxaoq2nLAWDtGiXB9XlEGYzXFrNK3lBgS4yDo9cRkgXmV6/yjKyVdMj e0diDehi2nMCc6m3JQ+3HF/KgnsIhyX8tCSY8AyVRKcS7vHCXzaPP5xdeYT8ISWZLuay iMjYM+CBEo/l5YwiXTqEu+Lg9Pb8GCbuePE9BiBIC/jQO9/w+cl8TDPtQC87UbIMTyTW JOBAJCFBjVx++tHzrq287RrZk0S0zTRoQQM45YU4znx4P9C6D8WXNnkgmE3y/Hky3XLf S8P6gCJbp+El7HyCEEM0rKVibggHaq4jMBQ4rz62lGaT9d7SJfVj6Xhj9/pM981kBQR+ XDdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682629852; x=1685221852; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lp25phZ45HRqtDee0Q+1ws/xkoM4Gpnqec2ldOouCHM=; b=Zy+EjaB32HmMNaLLV5cvoZNwc+JLSfPt6UyQR+6h2PgLMzkF08qlbqV05ji/iu7Nx6 f9p2mrbtI8H7LIR88vJ/L3dplkx5qAfIuwHozC/G/7BBGAhjgOKRLEAfze0M0tvcL/8W w4EkfQK6Xgenu0wQQmNhIGcQmihsCUtGmMqkrublxVxezvoU0Jy7P3jtTtn8EpsyBgSi 1dQmEVtzvzGtBinc2ulYEuHf/zP7OPVqysYyGKazTVeOQJwU9hozf+w4DT3Dtn1QWjO6 Y5tvbtTK8R0/irgN813K1hvWF8FDcoRsqSUEhz7cy9ruDpVmeuSGdf9MYvvC/DXVYctW VSWw== X-Gm-Message-State: AC+VfDyNakKsjwqI8JYtHPzvolEpWxCe14y/PfnEfdZaEsYot5z44aMA wbTUkYIToVDGNYWJOHE261aLbQfu1lRnOs8NQi0vqFgmVut5+tDZ9fRr7KphoUz/SpYajg9+FDz w570faZ2yOe2NDVDf9v94Dnvh0wzden2w3lJsobX+IO8= X-Received: by 2002:a05:6214:c88:b0:5f4:4e3d:2bde with SMTP id r8-20020a0562140c8800b005f44e3d2bdemr3329536qvr.48.1682629831789; Thu, 27 Apr 2023 14:10:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4jb2+k0W+QOxztuMbRyAZjbakFzGUw4xXgSPdtKhPEIPchovU1lxd5ffeCdz8rSaOEXnDiHg== X-Received: by 2002:a05:6214:c88:b0:5f4:4e3d:2bde with SMTP id r8-20020a0562140c8800b005f44e3d2bdemr3329482qvr.48.1682629831455; Thu, 27 Apr 2023 14:10:31 -0700 (PDT) From: Alexander Bulekov To: qemu-devel@nongnu.org Cc: Alexander Bulekov , Stefan Hajnoczi , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Mauro Matteo Cascella , Peter Xu , Jason Wang , David Hildenbrand , Gerd Hoffmann , Thomas Huth , Laurent Vivier , Bandan Das , "Edgar E . Iglesias" , Darren Kenny , Bin Meng , Paolo Bonzini , "Michael S . Tsirkin" , Marcel Apfelbaum , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Eduardo Habkost , Jon Maloy , Siqi Chen , Michael Tokarev , Fam Zheng , Kevin Wolf , Hanna Reitz , qemu-block@nongnu.org (open list:Block I/O path) Subject: [PATCH v10 2/8] async: Add an optional reentrancy guard to the BH API Date: Thu, 27 Apr 2023 17:10:07 -0400 Message-Id: <20230427211013.2994127-3-alxndr@bu.edu> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230427211013.2994127-1-alxndr@bu.edu> References: <20230427211013.2994127-1-alxndr@bu.edu> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CES-GSUITE_AUTH: bf3aNvsZpxl8 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.71.137.83; envelope-from=alxndr@bu.edu; helo=esa13.hc2706-39.iphmx.com X-Spam_score_int: -7 X-Spam_score: -0.8 X-Spam_bar: / X-Spam_report: (-0.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HK_RANDOM_ENVFROM=0.266, HK_RANDOM_FROM=0.999, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bu.edu) X-ZM-MESSAGEID: 1682629916174100005 Content-Type: text/plain; charset="utf-8" Devices can pass their MemoryReentrancyGuard (from their DeviceState), when creating new BHes. Then, the async API will toggle the guard before/after calling the BH call-back. This prevents bh->mmio reentrancy issues. Reviewed-by: Darren Kenny Signed-off-by: Alexander Bulekov --- docs/devel/multiple-iothreads.txt | 7 +++++++ include/block/aio.h | 18 ++++++++++++++++-- include/qemu/main-loop.h | 7 +++++-- tests/unit/ptimer-test-stubs.c | 3 ++- util/async.c | 18 +++++++++++++++++- util/main-loop.c | 5 +++-- util/trace-events | 1 + 7 files changed, 51 insertions(+), 8 deletions(-) diff --git a/docs/devel/multiple-iothreads.txt b/docs/devel/multiple-iothre= ads.txt index 343120f2ef..a3e949f6b3 100644 --- a/docs/devel/multiple-iothreads.txt +++ b/docs/devel/multiple-iothreads.txt @@ -61,6 +61,7 @@ There are several old APIs that use the main loop AioCont= ext: * LEGACY qemu_aio_set_event_notifier() - monitor an event notifier * LEGACY timer_new_ms() - create a timer * LEGACY qemu_bh_new() - create a BH + * LEGACY qemu_bh_new_guarded() - create a BH with a device re-entrancy gu= ard * LEGACY qemu_aio_wait() - run an event loop iteration =20 Since they implicitly work on the main loop they cannot be used in code th= at @@ -72,8 +73,14 @@ Instead, use the AioContext functions directly (see incl= ude/block/aio.h): * aio_set_event_notifier() - monitor an event notifier * aio_timer_new() - create a timer * aio_bh_new() - create a BH + * aio_bh_new_guarded() - create a BH with a device re-entrancy guard * aio_poll() - run an event loop iteration =20 +The qemu_bh_new_guarded/aio_bh_new_guarded APIs accept a "MemReentrancyGua= rd" +argument, which is used to check for and prevent re-entrancy problems. For +BHs associated with devices, the reentrancy-guard is contained in the +corresponding DeviceState and named "mem_reentrancy_guard". + The AioContext can be obtained from the IOThread using iothread_get_aio_context() or for the main loop using qemu_get_aio_context= (). Code that takes an AioContext argument works both in IOThreads or the main diff --git a/include/block/aio.h b/include/block/aio.h index e267d918fd..89bbc536f9 100644 --- a/include/block/aio.h +++ b/include/block/aio.h @@ -23,6 +23,8 @@ #include "qemu/thread.h" #include "qemu/timer.h" #include "block/graph-lock.h" +#include "hw/qdev-core.h" + =20 typedef struct BlockAIOCB BlockAIOCB; typedef void BlockCompletionFunc(void *opaque, int ret); @@ -323,9 +325,11 @@ void aio_bh_schedule_oneshot_full(AioContext *ctx, QEM= UBHFunc *cb, void *opaque, * is opaque and must be allocated prior to its use. * * @name: A human-readable identifier for debugging purposes. + * @reentrancy_guard: A guard set when entering a cb to prevent + * device-reentrancy issues */ QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque, - const char *name); + const char *name, MemReentrancyGuard *reentrancy_g= uard); =20 /** * aio_bh_new: Allocate a new bottom half structure @@ -334,7 +338,17 @@ QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *c= b, void *opaque, * string. */ #define aio_bh_new(ctx, cb, opaque) \ - aio_bh_new_full((ctx), (cb), (opaque), (stringify(cb))) + aio_bh_new_full((ctx), (cb), (opaque), (stringify(cb)), NULL) + +/** + * aio_bh_new_guarded: Allocate a new bottom half structure with a + * reentrancy_guard + * + * A convenience wrapper for aio_bh_new_full() that uses the cb as the name + * string. + */ +#define aio_bh_new_guarded(ctx, cb, opaque, guard) \ + aio_bh_new_full((ctx), (cb), (opaque), (stringify(cb)), guard) =20 /** * aio_notify: Force processing of pending events. diff --git a/include/qemu/main-loop.h b/include/qemu/main-loop.h index b3e54e00bc..68e70e61aa 100644 --- a/include/qemu/main-loop.h +++ b/include/qemu/main-loop.h @@ -387,9 +387,12 @@ void qemu_cond_timedwait_iothread(QemuCond *cond, int = ms); =20 /* internal interfaces */ =20 +#define qemu_bh_new_guarded(cb, opaque, guard) \ + qemu_bh_new_full((cb), (opaque), (stringify(cb)), guard) #define qemu_bh_new(cb, opaque) \ - qemu_bh_new_full((cb), (opaque), (stringify(cb))) -QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name); + qemu_bh_new_full((cb), (opaque), (stringify(cb)), NULL) +QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name, + MemReentrancyGuard *reentrancy_guard); void qemu_bh_schedule_idle(QEMUBH *bh); =20 enum { diff --git a/tests/unit/ptimer-test-stubs.c b/tests/unit/ptimer-test-stubs.c index f2bfcede93..8c9407c560 100644 --- a/tests/unit/ptimer-test-stubs.c +++ b/tests/unit/ptimer-test-stubs.c @@ -107,7 +107,8 @@ int64_t qemu_clock_deadline_ns_all(QEMUClockType type, = int attr_mask) return deadline; } =20 -QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name) +QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name, + MemReentrancyGuard *reentrancy_guard) { QEMUBH *bh =3D g_new(QEMUBH, 1); =20 diff --git a/util/async.c b/util/async.c index 21016a1ac7..a9b528c370 100644 --- a/util/async.c +++ b/util/async.c @@ -65,6 +65,7 @@ struct QEMUBH { void *opaque; QSLIST_ENTRY(QEMUBH) next; unsigned flags; + MemReentrancyGuard *reentrancy_guard; }; =20 /* Called concurrently from any thread */ @@ -137,7 +138,7 @@ void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMU= BHFunc *cb, } =20 QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque, - const char *name) + const char *name, MemReentrancyGuard *reentrancy_g= uard) { QEMUBH *bh; bh =3D g_new(QEMUBH, 1); @@ -146,13 +147,28 @@ QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *= cb, void *opaque, .cb =3D cb, .opaque =3D opaque, .name =3D name, + .reentrancy_guard =3D reentrancy_guard, }; return bh; } =20 void aio_bh_call(QEMUBH *bh) { + bool last_engaged_in_io =3D false; + + if (bh->reentrancy_guard) { + last_engaged_in_io =3D bh->reentrancy_guard->engaged_in_io; + if (bh->reentrancy_guard->engaged_in_io) { + trace_reentrant_aio(bh->ctx, bh->name); + } + bh->reentrancy_guard->engaged_in_io =3D true; + } + bh->cb(bh->opaque); + + if (bh->reentrancy_guard) { + bh->reentrancy_guard->engaged_in_io =3D last_engaged_in_io; + } } =20 /* Multiple occurrences of aio_bh_poll cannot be called concurrently. */ diff --git a/util/main-loop.c b/util/main-loop.c index e180c85145..4d76261010 100644 --- a/util/main-loop.c +++ b/util/main-loop.c @@ -605,9 +605,10 @@ void main_loop_wait(int nonblocking) =20 /* Functions to operate on the main QEMU AioContext. */ =20 -QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name) +QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name, M= emReentrancyGuard *reentrancy_guard) { - return aio_bh_new_full(qemu_aio_context, cb, opaque, name); + return aio_bh_new_full(qemu_aio_context, cb, opaque, name, + reentrancy_guard); } =20 /* diff --git a/util/trace-events b/util/trace-events index 16f78d8fe5..3f7e766683 100644 --- a/util/trace-events +++ b/util/trace-events @@ -11,6 +11,7 @@ poll_remove(void *ctx, void *node, int fd) "ctx %p node %= p fd %d" # async.c aio_co_schedule(void *ctx, void *co) "ctx %p co %p" aio_co_schedule_bh_cb(void *ctx, void *co) "ctx %p co %p" +reentrant_aio(void *ctx, const char *name) "ctx %p name %s" =20 # thread-pool.c thread_pool_submit(void *pool, void *req, void *opaque) "pool %p req %p op= aque %p" --=20 2.39.0