From patchwork Wed Apr 26 16:19:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Bulekov X-Patchwork-Id: 13224746 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 74456C7618E for ; Wed, 26 Apr 2023 16:21:16 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1prhst-0000lZ-BG; Wed, 26 Apr 2023 12:20:47 -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 1prhsr-0000lM-LP for qemu-devel@nongnu.org; Wed, 26 Apr 2023 12:20:46 -0400 Received: from esa9.hc2706-39.iphmx.com ([216.71.140.197]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1prhsp-0005jY-1p for qemu-devel@nongnu.org; Wed, 26 Apr 2023 12:20:45 -0400 X-IronPort-RemoteIP: 209.85.160.69 X-IronPort-MID: 276516072 X-IronPort-Reputation: None X-IronPort-Listener: OutgoingMail X-IronPort-SenderGroup: RELAY_GSUITE X-IronPort-MailFlowPolicy: $RELAYED IronPort-Data: A9a23:qTNit63eR23Y0EnnGvbD5W9zkn2cJEfYwER7XKvMYLTBsI5bp2MBz mcbUTuAOPyKNDejKthyPITloBwG78OGnYUyTgZkqSg9HnlHl5H5CIXCJC8cHc8zwu4v7q5Dx 59DAjUVBJlsFhcwnj/0bv666yE6jfrSLlbFILasEjhrQgN5QzsWhxtmmuoo6qZlmtH8CA6W0 T/Ii5S31GSNhnglYwr414rZ8Ek05Kio5mtC1rADTasjUGH2xiF94K03ePnZw0vQGuF8AuO8T uDf+7C1lkuxE8AFU47Nfh7TKyXmc5aLVeS8oiM+t5uK23CukhcPPpMTb5LwX6v2ZwKhxLidw P0V3XC5pJxA0qfkwYzxWDEBe81y0DEvFBYq7hFTvOTKp3AqfUcAzN1fD1tpN4YJ6992AFBj3 6MnIWkzczSc0rfeLLKTEoGAh+wmJcjveYQW4zRuk2CfAvEhTpTOBa7N4Le03h9q3pEITauYP pRBL2Q+BPjDS0Qn1lM/AZYumuuyrnPiNTBUtTp5oIJtvDaOlVIpj+KF3Nz9VNW1Xc51t1+jr 2OY0EjLUi4Hb4CG1m/Qmp6rrqqV9c/hY6oLGbils/JnnlCX7moUDhIQSB28u/bRt6Klc9dWK khR/SN36KZspQqkSd7yWxD+q3mB1vIBZ+dt/yQBwFnl4sLpD8yxWjhsouJpADD+iPILeA== IronPort-HdrOrdr: A9a23:3xUEw6oGTluB/pLjnTkc2dkaV5qaeYIsimQD101hICG9Lfbo8P xGzc5rsSMc1gxhPU3I+erwQ5VoJkmsh6KdgLNhTotKOTOWwldAQ7sSkLcKqQeAJ8STzJ8g6U 5LSdkwNDSINykwsS+Y2njJLz9D+qj4zEnAv463pBcdND2Gc5sQlzuRYjzrdHGeLzM2dKbReq DsmfaujVKbCAMqh86Adzc4t/CpnbP2fGeMW3E77t0chTVmTwnD1Fc3KWn64v5PaUIG/V8PmV K15zAQy82YwoeGIjC17R6n03x28OGRg+eqX6S3+4IowmeFsHfkWG2pYdHy3gzc9omUmRoXrO U= X-Talos-CUID: 9a23:OjgR9Grc9YDY4c1Y9hMABqfmUfEmaCLXx2bBGk65OEtDF4KcEGGN3Lwxxg== X-Talos-MUID: 9a23:VayL2AZKhDsL7OBTmTbTnDpCHd5T6qXwJ2w/waob4JWdKnkl Received: from mail-oa1-f69.google.com ([209.85.160.69]) by ob1.hc2706-39.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 26 Apr 2023 12:20:26 -0400 Received: by mail-oa1-f69.google.com with SMTP id 586e51a60fabf-18efc77f853so4039999fac.0 for ; Wed, 26 Apr 2023 09:20:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bu.edu; s=s1gsbu; t=1682526025; x=1685118025; 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=GDaUfRlwrigB29cnZTwowZYKrhUvgoRjtB178BxImD3aUDIcYs3EuyO68Uq0y25upO k2li0UaEpaO6if7ZyKvUl7v2ZYhxRU/dZVuEOiSzjuIpA0AMixZV8SEdLWaBSmVcvDOp Xa0/alQeQvYaYe/OB/ATFWIfnSvMCZOMeH0/9MZwFWWmDlLas+7btm68PNVMkBonrrwL +/rmFg33VaNC+rcfZxj5SKkvz4nWL/aA5b5wEKXTg0E1qxTkS1dRAZGWP9DfLanmo+SP bm0ZyxgmFCkcBy0aH8/B+Qr3I8uxRSP0elIGMbsI60jPLUqjrXUXUTCVJPTB16/m9Xgk tHBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682526025; x=1685118025; 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=gOS7zH1nRLJDC+YlsItLnTv3me78RIONSPl62oGA90U2Z3REY6mmACrw7noAvUlLm3 wut2dNaozKVihI+N6qp18P0thL762+h698USb6QNDwhkgEtt3KyOss3qN0OPaJYtEazA eUM5DRDj/HZFZkDUEiUhZi+qSKtDpCuIZFSGhQTc0BnoU7FkSH6pJ8BdDvRdchTux1q6 /wffM/fa/lXWa4Rf+MWT+R0imqKH2jIkLp6+RQe29DhFWimLGXVlb7zyjCRnkPvpgANx ve75CSaPc08B+ziuSzPFRs+u16oYPvmdrBizhxRHV7R0RPJJi53vH7U+pzjhjcWY6sqv H1/g== X-Gm-Message-State: AC+VfDw9VD/Uasxsn7cGTNNDlR1J1v/WdVPosurYcj0TFy/tTTKsZ3xb 1+dQWg1uuSuWF1rkKxMKwGbxpiUBiAWMb/aPk37KhmFoqDMzOwqi2X+ia96aa3D5WYUoIvTXdEB hcfUHMMSkaKraR8i02QsZk9NLzYUtvQDxnbszHH/9Clc= X-Received: by 2002:ac8:5a07:0:b0:3ef:5c5a:8932 with SMTP id n7-20020ac85a07000000b003ef5c5a8932mr5237324qta.18.1682526004396; Wed, 26 Apr 2023 09:20:04 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ61Ms3bgDhhuMamyq9UBH85tt1gsvpdytLNXUUXNyUXZo3r6NfvfbwCAFHhAj16jFc3H4XiVw== X-Received: by 2002:ac8:5a07:0:b0:3ef:5c5a:8932 with SMTP id n7-20020ac85a07000000b003ef5c5a8932mr5237273qta.18.1682526004092; Wed, 26 Apr 2023 09:20:04 -0700 (PDT) Received: from mozz.bu.edu (mozz.bu.edu. [128.197.127.33]) by smtp.gmail.com with ESMTPSA id ge8-20020a05622a5c8800b003e3914c6839sm2662208qtb.43.2023.04.26.09.20.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Apr 2023 09:20:03 -0700 (PDT) From: Alexander Bulekov To: qemu-devel@nongnu.org Cc: Alexander Bulekov , Stefan Hajnoczi , =?utf-8?q?Philippe_Mathieu-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_P_=2E_Berra?= =?utf-8?q?ng=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 v9 2/8] async: Add an optional reentrancy guard to the BH API Date: Wed, 26 Apr 2023 12:19:45 -0400 Message-Id: <20230426161951.2948996-3-alxndr@bu.edu> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230426161951.2948996-1-alxndr@bu.edu> References: <20230426161951.2948996-1-alxndr@bu.edu> MIME-Version: 1.0 X-CES-GSUITE_AUTH: bf3aNvsZpxl8 Received-SPF: pass client-ip=216.71.140.197; envelope-from=alxndr@bu.edu; helo=esa9.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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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-iothreads.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 AioContext: * 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 guard * LEGACY qemu_aio_wait() - run an event loop iteration Since they implicitly work on the main loop they cannot be used in code that @@ -72,8 +73,14 @@ Instead, use the AioContext functions directly (see include/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 +The qemu_bh_new_guarded/aio_bh_new_guarded APIs accept a "MemReentrancyGuard" +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" + typedef struct BlockAIOCB BlockAIOCB; typedef void BlockCompletionFunc(void *opaque, int ret); @@ -323,9 +325,11 @@ void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMUBHFunc *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_guard); /** * aio_bh_new: Allocate a new bottom half structure @@ -334,7 +338,17 @@ QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, 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) /** * 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); /* internal interfaces */ +#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); 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; } -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 = g_new(QEMUBH, 1); 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; }; /* Called concurrently from any thread */ @@ -137,7 +138,7 @@ void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMUBHFunc *cb, } QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque, - const char *name) + const char *name, MemReentrancyGuard *reentrancy_guard) { QEMUBH *bh; bh = g_new(QEMUBH, 1); @@ -146,13 +147,28 @@ QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque, .cb = cb, .opaque = opaque, .name = name, + .reentrancy_guard = reentrancy_guard, }; return bh; } void aio_bh_call(QEMUBH *bh) { + bool last_engaged_in_io = false; + + if (bh->reentrancy_guard) { + last_engaged_in_io = 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 = true; + } + bh->cb(bh->opaque); + + if (bh->reentrancy_guard) { + bh->reentrancy_guard->engaged_in_io = last_engaged_in_io; + } } /* 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) /* Functions to operate on the main QEMU AioContext. */ -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) { - return aio_bh_new_full(qemu_aio_context, cb, opaque, name); + return aio_bh_new_full(qemu_aio_context, cb, opaque, name, + reentrancy_guard); } /* 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" # thread-pool.c thread_pool_submit(void *pool, void *req, void *opaque) "pool %p req %p opaque %p"