From nobody Wed Feb 11 01:36: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=1674710822; cv=none; d=zohomail.com; s=zohoarc; b=OKa8fdcBdH9Xl02/oKWSdnvfmdWYL0u4V1araM/Dd3KxYYddMsG8tj8rUyesnxkYlx77pXG4xBC3XV4P+mQjbTaTARp/fMUf6e349J4NEtL2NcIntvBVVYo9I5Hw03yXQUg74bQ4LFIOG3w+9KKPl0p5zwLvAJIdb1udEzIUaAo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1674710822; 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=Ix/LQlL7SnPYOBEKRvRlhs6b0+t/hamvfERWUcZMY5A=; b=aaJaka0+kBjZ/CIHlz5vLIhWRzhe5BXCVvZGDw67VzqIlGo2x+fhxioxfxRBVWBmGGnUEognze4uSWH+bbYrGGaYCCCprIQIWC9YWN4DX3aiExczxN/pVZpvNxly7kBWR/oXu2FelmcODCaDNvposgimpZCzN1hLjjUTWuG6wZE= 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 1674710822947437.64159311552476; Wed, 25 Jan 2023 21:27:02 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pKumG-0007L9-2j; Thu, 26 Jan 2023 00:26:24 -0500 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 1pKumC-0007KB-9H for qemu-devel@nongnu.org; Thu, 26 Jan 2023 00:26:20 -0500 Received: from esa5.hc2706-39.iphmx.com ([216.71.137.63]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pKum8-0000qd-Tg for qemu-devel@nongnu.org; Thu, 26 Jan 2023 00:26:19 -0500 Received: from mail-qv1-f70.google.com ([209.85.219.70]) by ob1.hc2706-39.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 26 Jan 2023 00:26:13 -0500 Received: by mail-qv1-f70.google.com with SMTP id e5-20020a056214110500b0053547681552so519217qvs.8 for ; Wed, 25 Jan 2023 21:26:13 -0800 (PST) Received: from mozz.bu.edu (mozz.bu.edu. [128.197.127.33]) by smtp.gmail.com with ESMTPSA id h185-20020a376cc2000000b007069375e0f4sm326169qkc.122.2023.01.25.21.26.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Jan 2023 21:26:11 -0800 (PST) X-IronPort-RemoteIP: 209.85.219.70 X-IronPort-MID: 265809644 X-IronPort-Reputation: None X-IronPort-Listener: OutgoingMail X-IronPort-SenderGroup: RELAY_GSUITE X-IronPort-MailFlowPolicy: $RELAYED IronPort-Data: A9a23:8MlBUqCMy09oBRVW/5Thw5YqxClBgxIJ4kV8jS/XYbTApGgh32ZRy 2NOW2DVa/6DZ2X9fN0nbY609BkFusXdndQxTANkpHpgcSl2pJueD7x1DKtR0wB+jCHnZBg6h ynLQoCYdKjYdleF+lH2dOCk9SMnvU2xbuKUIPbePSxsThNTRi4kiBZy88Y0mYctitWia++3k YqaT/b3ZRn0hFaYDkpOs/jY8Eo14qyr0N8llgdWic5j7Qe2e0Y9Ucp3yZGZdxPQXoRSF+imc OfPpJnRErTxpkpF5nuNy94XQ2VTKlLgFVHmZkl+AsBOtiN/Shkaic7XAha9hXB/0F1ll/gpo DlEWAfZpQ0BZ8Ugk8xEO/VU/r0X0QSrN9YrLFDm2fF/wXEqfFPsyfBiDRpnE7QD48JLWFp/2 sIlFjskO0Xra+KemNpXS8Fpj8UnadD1ZcYR5SkmwjbeAvIrB5vERs0m5/cChGZ21p0IR6+PI ZVBAdZsRE2ojxlnM1MHDp4ktO21wHTzblW0rXrP+/dnuzmNkFAZPL7FHsvPVc6maeBukE+Kl GKf+mfiWT4iK4nKodaC2jf27gPVpgviVYcPUbG16PNuqFuUwGMVFVsRT1TTnBWioku3WtYaM lJNvyRx9e4980ukStS7VBq9yJKZgiMhtxNrO7VSwGmwJmD8sm512kBsouZ9VeEb IronPort-HdrOrdr: A9a23:acJyBKi3Tigylsux0opwhozJWHBQXgwji2hC6mlwRA09TyVXrb HLoB19726JtN91YhsdcL+7Sc+9qB/nhPxICMwqTMyftWrdyRaVxf9ZnPLfKlTbckWUh41gPO VbAtJD4bXLbWSS5vyKhzVQfexQpeWvweSDqd2b4U1QbTxHXYld0iYRMHflLqS0fmV77FgCea Z0KvAom9PZQwVuUi1zPBZlY9T+ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bu.edu; s=s1gsbu; 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=Ix/LQlL7SnPYOBEKRvRlhs6b0+t/hamvfERWUcZMY5A=; b=PX5a9lchwqPi7cL+tnI+1mKLflgjVO3fKmwzbbz0ZBn2fY+528tyg/34ioMUPMW4VT 9oyM2VlUhYNsua0n4s2RELQQmd/qcNLljQZCuOW/SXvkcEveh73iHNTSnXon8k8Ec44N PTaw0cIFRnMNrFPr70nPVAIcJizXK9ENR59O13sBGv0IWWJXfGW3QVK9lnTAKDdYZCZ5 KT5OUvykGUBoTiA0eaxS2zsBn2nS9Uafc4WH8Nz5GgcZvmgH6rjm4xjLojwFK5jmRt14 YRa/akJA6QfAkxPN3YEhGdiIKevKFDPG61Z3/NP8UfUEe9z68dHyWpjuGRgbnLnjMlSL a7rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=Ix/LQlL7SnPYOBEKRvRlhs6b0+t/hamvfERWUcZMY5A=; b=hdAQ4wQM5P6hrseX4EIGcp0+K6cORwogoUlfWpwHXU5tz+FmmG1M6S097KieXDmumn dNmQYvp9kNFFceaRK41MCtw7oFv8oLOEN79sqg97czKlvLNpybIxqcyfBfppFRoBVEYK nFYHmu8Ry7jA4+q2+p9ofZhpypaYZb8E5glANZat4jWn6Xfte4oNus+uD4usgmEU5cIp C7umLXWtA1AFy5t3Q4pusNULBuKAzH8peUkptH4t7owNPVBwhJAzhu9MdvfnEcvgxbTd JwAIjozBMenru0yO/xiPAI3Z5lkhJKHw3antpDnoDB15vhiCwTaft48wX+vEfVqRx9Ky nlWA== X-Gm-Message-State: AFqh2krqUjTZC0E4FuVNPrYtoJMeWKV+C2c91BVxxljF73BHnh3YvD1S zFqMtJzm14gURwfRDZm329Hzce9fgfgS1K+2G64K39qhJDTyBGB8ffiaesUkeFCT6+MaxspBUzu pWGNEvTkIg11kpof0NhqSPjaVh9kRrg== X-Received: by 2002:a05:6214:3185:b0:532:32b7:b7e4 with SMTP id lb5-20020a056214318500b0053232b7b7e4mr46745543qvb.29.1674710773014; Wed, 25 Jan 2023 21:26:13 -0800 (PST) X-Google-Smtp-Source: AMrXdXtFf9qcWoInG1SmTJspY0x7lq8bRIWqmQVAN0nrHHWd36zkt74i8hjneSgCXXvkTZ2RM1+SyQ== X-Received: by 2002:a05:6214:3185:b0:532:32b7:b7e4 with SMTP id lb5-20020a056214318500b0053232b7b7e4mr46745501qvb.29.1674710772722; Wed, 25 Jan 2023 21:26:12 -0800 (PST) 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 , Fam Zheng , Kevin Wolf , Hanna Reitz , qemu-block@nongnu.org (open list:Block I/O path) Subject: [PATCH v5 2/4] async: Add an optional reentrancy guard to the BH API Date: Thu, 26 Jan 2023 00:25:56 -0500 Message-Id: <20230126052558.572634-3-alxndr@bu.edu> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230126052558.572634-1-alxndr@bu.edu> References: <20230126052558.572634-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.63; envelope-from=alxndr@bu.edu; helo=esa5.hc2706-39.iphmx.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.1 / 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.001, HK_RANDOM_FROM=0.999, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 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: 1674710825267100003 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. 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 0f65a3cc9e..94d661ff7e 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); @@ -332,9 +334,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 @@ -343,7 +347,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 c25f390696..84d1ce57f0 100644 --- a/include/qemu/main-loop.h +++ b/include/qemu/main-loop.h @@ -389,9 +389,12 @@ void qemu_cond_timedwait_iothread(QemuCond *cond, int = ms); =20 void qemu_fd_register(int fd); =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 f5e75a96b6..24d5413f9d 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 14d63b3091..dae77c0d7e 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 */ @@ -133,7 +134,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); @@ -142,13 +143,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 58f776a8c9..07d2e2040a 100644 --- a/util/main-loop.c +++ b/util/main-loop.c @@ -617,9 +617,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 c8f53d7d9f..dc3b1eb3bf 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