From nobody Sat May 4 14:41:09 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=bu.edu ARC-Seal: i=1; a=rsa-sha256; t=1675570151; cv=none; d=zohomail.com; s=zohoarc; b=J6kM6QGxoWAxkewcH6ZkYUuvNYrUVJHAZOpi4PYWwPVxvqMAvhcPzWN6T7RAeMBspyBbHtzFYFRjd9iu2wnFNTlVv6EqmXnfyl5tSx7BpVzJmXKpgkG4MchtyOPkpV3roMulOgV+cy9vCaCnVKX2Pix0LoAaiVG+DatHIyysRgE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675570151; 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=8vZ3mp35XAlxGvDT4NGszzpubWskmGO1xwXSkj57qxo=; b=EcnrXm6ufcgo0NIolyhuqQQhXxdg205PffLPlfAeARnsktg8NQ5YTXoN6iXCPzrMz6fa+TxaI8oWRKRM1u3306+ophAb9N4fJ1eNo4pk3tJqOhgnFtOVEdO3bXQXjEg8o6N1SBi66E1bD9rqstbHP7bCeH5zus2uqOB8PckjlKc= 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+importer2=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 1675570151362820.117047121353; Sat, 4 Feb 2023 20:09:11 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pOWK7-0000KM-GH; Sat, 04 Feb 2023 23:08:15 -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 1pOWK5-0000Jj-FW for qemu-devel@nongnu.org; Sat, 04 Feb 2023 23:08:13 -0500 Received: from esa8.hc2706-39.iphmx.com ([216.71.140.196]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pOWK3-0002jb-1C for qemu-devel@nongnu.org; Sat, 04 Feb 2023 23:08:12 -0500 Received: from mail-qk1-f200.google.com ([209.85.222.200]) by ob1.hc2706-39.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 04 Feb 2023 23:08:06 -0500 Received: by mail-qk1-f200.google.com with SMTP id v7-20020a05620a0f0700b006faffce43b2so5848018qkl.9 for ; Sat, 04 Feb 2023 20:08:06 -0800 (PST) Received: from mozz.bu.edu (mozz.bu.edu. [128.197.127.33]) by smtp.gmail.com with ESMTPSA id w9-20020ac87189000000b003b9b41a32b7sm4619282qto.81.2023.02.04.20.08.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Feb 2023 20:08:05 -0800 (PST) X-IronPort-RemoteIP: 209.85.222.200 X-IronPort-MID: 254465733 X-IronPort-Reputation: None X-IronPort-Listener: OutgoingMail X-IronPort-SenderGroup: RELAY_GSUITE X-IronPort-MailFlowPolicy: $RELAYED IronPort-Data: A9a23:d6ZY16Lh4oOS4/c1FE+Rb5clxSXFcZb7ZxGr2PjKsXjdYENS0TVRz 2JKCjjXM6yIazD9LdF/bYvnoRkB75XTx4A2HAZorCE8RH908vbIVI+TRqvS04J+DSFhoGZPt Zh2hgzodZhsJpPkjk7xdOOn9Twkj/rgqoPUUIbsIjp2SRJvVBAvgBdin/9RqoNziJ2yDhjlV ena+qUzA3f4nW8rWo4ow/jb8kg35qyu4GhwUmEWPpingnePzxH5M7pCfcldH1OgKqFIE+izQ fr0zb3R1gs1KD90V7tJOp6iGqE7aue60Tqm0xK6aID76vR2nRHe545gXBYqhea7vB3S9zx54 I0lWZVd0m7FNIWV8AgWe0Aw/y2TocSqUVIISJSymZX78qHIT5fj6+t1Klg1GqI2xr00BmJvr cceDToIcynW0opawJrjIgVtrsEqLc2uJYBG/386lWmfAvEhTpTOBa7N4Le03h9q3pEITauYP ZBDL2A3NHwsYDUWUrsTIJs6jOGknFH1bntVpE/9Sa8fuTWPkFIsj+O2WDbTUoSWQ+4EpXSSm lzh8TzUI0AoLN6e+RPQpxpAgceKx0sXQrk6DbC967tmjUOewkQVDxsZU0b9puO24nNSQPpaI k0QvzMt9O08rhTzCNb6WBK8rTiPuRt0t8dsLtDWITqlksL8izt1zEBdJtKdQLTKbPMLeAE= IronPort-HdrOrdr: A9a23:ViXBL6//pcVJX08ldoluk+DoI+orL9Y04lQ7vn2ZLiY4TiX4ra +TdZEgviMc5wx+ZJhNo7G90cu7MBHhHO9OgbX5VI3KNGOKhILPFvAG0WKI+UyCJ8SRzJ866Y 5QN4R4Fd3sHRxboK/BkXCF+g8bsb26GXaT9IDj80s= 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=8vZ3mp35XAlxGvDT4NGszzpubWskmGO1xwXSkj57qxo=; b=kSDwDHw22fnzch1rPETXXOSFwdDJ/2YrGhS/Lxls8PB9tq0afK7MRITWtl8Jzwg2GJ AI9W1Km6P/Fc0Wl/FIGnO8krrhOJYS1FOVm3IbrTee9ocvl8vxvFObjiTdYGDDpqZhvr EM2xuRChcm5aqgkeY7gxK8kF7AnW57sobf+hPgtyQ/lG/58zLfiImeDqJb10CtNmAbfi laVdjVlVo1V2cqltXYYaVZ4T6UuEDb1nlhiHdxjTo5zNa8+gnTa566O4Fxib8fBoPmkX 4lELa6IevEGAL8btDiS6+p3qx6FyHoFGFkx4nWNDf4HFHN3ndiG9KbjCxg1e7VrjGGJb E85Q== 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=8vZ3mp35XAlxGvDT4NGszzpubWskmGO1xwXSkj57qxo=; b=to1k/w4IH8WrIAArDGF9G3/guMjG2+Hznjm8gRT/Mn7W32iHDeh9eIADvHis+02at4 zl55PpAvcQDec6q7iBCdd9rUGih0LJTrBQL2al+iovROvwxqTuPBUFFRIBRt5etIOPbi zr7ktfB16m7SKCcNqbGBf2IspH8+JxZZCWPh7P18e1nWWHvLPOOTPs/CibiVJfnNNWqx zZCTHOPLGgkVIDj57m9MkUDKoHCNdIjDZbfST5jWbv1hyhzYNESXIGoyG7/a3XQDN6aa BqPsEQqQ1X+B7XFN03JEm/PWHdTZZxKumX6LzQkZZF8uQVZzJo+F1ALA2WEVvpvWCeei DWTw== X-Gm-Message-State: AO0yUKUR4mANXM8fnxbQy7prMrxXs/uPvq2UPYE/YS9TQT8VzMsD3Sr2 pNN0Z8qTlXyeKWppT9eNGHOuN6XANqKR9rhkia6n2CWkQuiL1fL/WYJfrgaQTh8KRCPNOMd0cXx nvBGBBNQDAm0y9JZFhL1Mi8SHTaWjag== X-Received: by 2002:a05:622a:100e:b0:3b0:11a1:3140 with SMTP id d14-20020a05622a100e00b003b011a13140mr25772731qte.43.1675570086069; Sat, 04 Feb 2023 20:08:06 -0800 (PST) X-Google-Smtp-Source: AK7set+MGatfdHWgwCghVkgsI9RQYZfB9HNfJbkm6DIpDMoIACVb2192XjJRfXGzeSUWo69xv1hUng== X-Received: by 2002:a05:622a:100e:b0:3b0:11a1:3140 with SMTP id d14-20020a05622a100e00b003b011a13140mr25772708qte.43.1675570085703; Sat, 04 Feb 2023 20:08:05 -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 Subject: [PATCH v6 1/4] memory: prevent dma-reentracy issues Date: Sat, 4 Feb 2023 23:07:34 -0500 Message-Id: <20230205040737.3567731-2-alxndr@bu.edu> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230205040737.3567731-1-alxndr@bu.edu> References: <20230205040737.3567731-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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.71.140.196; envelope-from=alxndr@bu.edu; helo=esa8.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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bu.edu) X-ZM-MESSAGEID: 1675570152355100005 Content-Type: text/plain; charset="utf-8" Add a flag to the DeviceState, when a device is engaged in PIO/MMIO/DMA. This flag is set/checked prior to calling a device's MemoryRegion handlers, and set when device code initiates DMA. The purpose of this flag is to prevent two types of DMA-based reentrancy issues: 1.) mmio -> dma -> mmio case 2.) bh -> dma write -> mmio case These issues have led to problems such as stack-exhaustion and use-after-frees. Summary of the problem from Peter Maydell: https://lore.kernel.org/qemu-devel/CAFEAcA_23vc7hE3iaM-JVA6W38LK4hJoWae5Kck= nhPRD5fPBZA@mail.gmail.com Resolves: https://gitlab.com/qemu-project/qemu/-/issues/62 Resolves: https://gitlab.com/qemu-project/qemu/-/issues/540 Resolves: https://gitlab.com/qemu-project/qemu/-/issues/541 Resolves: https://gitlab.com/qemu-project/qemu/-/issues/556 Resolves: https://gitlab.com/qemu-project/qemu/-/issues/557 Resolves: https://gitlab.com/qemu-project/qemu/-/issues/827 Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1282 Reviewed-by: Darren Kenny Reviewed-by: Stefan Hajnoczi Signed-off-by: Alexander Bulekov Acked-by: Peter Xu Acked-by: Michael S. Tsirkin --- include/hw/qdev-core.h | 7 +++++++ softmmu/memory.c | 17 +++++++++++++++++ softmmu/trace-events | 1 + 3 files changed, 25 insertions(+) diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 35fddb19a6..8858195262 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -162,6 +162,10 @@ struct NamedClockList { QLIST_ENTRY(NamedClockList) node; }; =20 +typedef struct { + bool engaged_in_io; +} MemReentrancyGuard; + /** * DeviceState: * @realized: Indicates whether the device has been fully constructed. @@ -194,6 +198,9 @@ struct DeviceState { int alias_required_for_version; ResettableState reset; GSList *unplug_blockers; + + /* Is the device currently in mmio/pio/dma? Used to prevent re-entranc= y */ + MemReentrancyGuard mem_reentrancy_guard; }; =20 struct DeviceListener { diff --git a/softmmu/memory.c b/softmmu/memory.c index 9d64efca26..eefeeae317 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -533,6 +533,7 @@ static MemTxResult access_with_adjusted_size(hwaddr add= r, uint64_t access_mask; unsigned access_size; unsigned i; + DeviceState *dev =3D NULL; MemTxResult r =3D MEMTX_OK; =20 if (!access_size_min) { @@ -542,6 +543,19 @@ static MemTxResult access_with_adjusted_size(hwaddr ad= dr, access_size_max =3D 4; } =20 + /* Do not allow more than one simultanous access to a device's IO Regi= ons */ + if (mr->owner && + !mr->ram_device && !mr->ram && !mr->rom_device && !mr->readonly) { + dev =3D (DeviceState *) object_dynamic_cast(mr->owner, TYPE_DEVICE= ); + if (dev) { + if (dev->mem_reentrancy_guard.engaged_in_io) { + trace_memory_region_reentrant_io(get_cpu_index(), mr, addr= , size); + return MEMTX_ERROR; + } + dev->mem_reentrancy_guard.engaged_in_io =3D true; + } + } + /* FIXME: support unaligned access? */ access_size =3D MAX(MIN(size, access_size_max), access_size_min); access_mask =3D MAKE_64BIT_MASK(0, access_size * 8); @@ -556,6 +570,9 @@ static MemTxResult access_with_adjusted_size(hwaddr add= r, access_mask, attrs); } } + if (dev) { + dev->mem_reentrancy_guard.engaged_in_io =3D false; + } return r; } =20 diff --git a/softmmu/trace-events b/softmmu/trace-events index 22606dc27b..62d04ea9a7 100644 --- a/softmmu/trace-events +++ b/softmmu/trace-events @@ -13,6 +13,7 @@ memory_region_ops_read(int cpu_index, void *mr, uint64_t = addr, uint64_t value, u memory_region_ops_write(int cpu_index, void *mr, uint64_t addr, uint64_t v= alue, unsigned size, const char *name) "cpu %d mr %p addr 0x%"PRIx64" value= 0x%"PRIx64" size %u name '%s'" memory_region_subpage_read(int cpu_index, void *mr, uint64_t offset, uint6= 4_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64= " size %u" memory_region_subpage_write(int cpu_index, void *mr, uint64_t offset, uint= 64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx6= 4" size %u" +memory_region_reentrant_io(int cpu_index, void *mr, uint64_t offset, unsig= ned size) "cpu %d mr %p offset 0x%"PRIx64" size %u" memory_region_ram_device_read(int cpu_index, void *mr, uint64_t addr, uint= 64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64"= size %u" memory_region_ram_device_write(int cpu_index, void *mr, uint64_t addr, uin= t64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64= " size %u" memory_region_sync_dirty(const char *mr, const char *listener, int global)= "mr '%s' listener '%s' synced (global=3D%d)" --=20 2.39.0 From nobody Sat May 4 14:41:09 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=bu.edu ARC-Seal: i=1; a=rsa-sha256; t=1675570155; cv=none; d=zohomail.com; s=zohoarc; b=emDqGDguQdexLifbWhiVum44ilQRytxexn2ipcU0Ka0Bzm2duWjzIYH9rNxiC0kCZZS2CKHYHHKzqRPaR+eVN6uurNJuiBkWo9fCJrMw4nkg1nyG9njQFnQl0Mjjzoy72qKOZIUor++wgF4ACpNU/8te1csOmF2Zda4AYOge3zM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675570155; 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=1ZIvTLvldoHILUtKZt0L98ElAkvpg/WKYDgA0YNYX6o=; b=SGIi8NqvtKs8/TqOKta5IHnSiSy7MhahQRL9rG0z+Gp2eS7OdmOQ7Z8mBzc5yyQy3DDvGiyuNF4CSZSLaDXTCqcCnEQ3EVng6Ir77COFnTz6JONseG67sgLokMRAfDBz3kNHbtFfSnM0UWzKPOosU8ydpimQgSud44OHobQVHyk= 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+importer2=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 1675570155202423.1351587544509; Sat, 4 Feb 2023 20:09:15 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pOWKL-0000N2-8K; Sat, 04 Feb 2023 23:08:29 -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 1pOWKJ-0000Ma-DA for qemu-devel@nongnu.org; Sat, 04 Feb 2023 23:08:27 -0500 Received: from esa3.hc2706-39.iphmx.com ([68.232.154.118]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pOWKG-0002k3-EM for qemu-devel@nongnu.org; Sat, 04 Feb 2023 23:08:27 -0500 Received: from mail-qk1-f198.google.com ([209.85.222.198]) by ob1.hc2706-39.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 04 Feb 2023 23:08:22 -0500 Received: by mail-qk1-f198.google.com with SMTP id h13-20020a05620a244d00b006fb713618b8so5926812qkn.0 for ; Sat, 04 Feb 2023 20:08:22 -0800 (PST) Received: from mozz.bu.edu (mozz.bu.edu. [128.197.127.33]) by smtp.gmail.com with ESMTPSA id g7-20020a05620a40c700b007208a81e11esm4278052qko.41.2023.02.04.20.08.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Feb 2023 20:08:20 -0800 (PST) X-IronPort-RemoteIP: 209.85.222.198 X-IronPort-MID: 256220934 X-IronPort-Reputation: None X-IronPort-Listener: OutgoingMail X-IronPort-SenderGroup: RELAY_GSUITE X-IronPort-MailFlowPolicy: $RELAYED IronPort-Data: A9a23:VWYCRqshZMAe63mF3s1Mr9yyMefnVOhcMUV32f8akzHdYApBsoF/q tZmKW6COfjYM2Kke9h3OY7i8B5X6MKEndRhHAVrqC4yEioQ9ZOVVN+UEBzMMnLJJKUvbq7GA +byyDXkBJppJpMJjk71atANlVEliefTAOK5ULSfUsxIbVcMYD87jh5+kPIOjIdtgNyoayuAo tq3qMDEULOf82cc3lk8tuTS93uDgNyo4GlD5gZmOagQ1LPjvyJ94Kw3dPnZw0TQH9E88t6SH 47r0Ly/92XFyBYhYvvNfmHTKxBirhb6ZGBiu1IPM0SQqkEqSh8ai87XAMEhhXJ/0F1lqTzQJ OJl7vRcQS9xVkHFdX90vxNwSkmSNoUfkFPLzOTWXWV+ACQqflO1q8iCAn3aMqUV8NRqPFxi1 MApB2sCTEjem+Gx5JG0H7wEasQLdKEHPasas3BkiCjaVLMoGMGTBarN4tBc0XE7gcUm8fT2P ZJIL2oyKk6aOVsWawd/5JEWxY9EglH2dy1epEi9r7dx7mTOpOB0+OKwboCPIo3RGa25mG62l FzH2kf4BygQH/3E+x6nqmudi8v2yHaTtIU6UefQGuRRqESew3FWBBAIWF+Tp/6/hUijHdVFJ CQpFjEGqKEz8AmyTYC4UUTp8DiLuRkTX9cWGOo/gO2Q9pfpD8+iLjBsZlZ8hBYO7afamRRCO oe1ou7U IronPort-HdrOrdr: A9a23:StTHU68GKdqaUYwE1wRuk+BHI+orL9Y04lQ7vn2ZKCYlCPBw8v rFoB1173/JYUkqKRMdcLy7SdG9qBDnm6Kdg7NhW4uKZwXvsmqvMJxe9oPpwTH6cheOk9K1vJ 0IG8ND4bbLfCFHZKjBkXKFOudl6NOM9ryumKP4yWpsQgFhY7xh6QA8NxaDHiRNNWp77FkCeK Z0JPArm9NtQxsqR/X+IX8ZX+fCrdXXlJTge3c9dnoawTjLqjW06LH7GRCE3hEYSXdlx6sk+2 SAswv34MyYwpaGI9bntlPu0w== 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=1ZIvTLvldoHILUtKZt0L98ElAkvpg/WKYDgA0YNYX6o=; b=N0r07ZAqT8SMLUEdHwI695G3TGUFQ7qDDu2WbrDvUxKoQ7dqqbjZQlZxKD12ueIxGd PfywUmKkrarGG7mm0liN70kZVQQw9OyuuDT/j7Rhof8cls382udCrELDz3IcY6enuUrO dGtRvZHYBcIW1fA5h6zMCRMMjLb4t8CpJorZOlawQT3ra265T4RTs5CQrtT13chwN7es 8+C2IVRKk/XTDM2G6aSWcq2/DQt/ZsPz8rF/UTLDQUPD6DVg53rzrQOQ43gixZN1BCAx A2HS4XlFhWDMIfGZSiwWHaI60L6JYKPWi9/nTOVS8hiLPMaVEElPmNdmaLN4s9MlRlgM yKyg== 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=1ZIvTLvldoHILUtKZt0L98ElAkvpg/WKYDgA0YNYX6o=; b=IkP++/15VPHTQa2ottFD9FDJpYcauHBtNp1tZIbsaBwwzMVdiYklTeeJ8ThXZ45L+o TYmnV1NVVDcUyJtbNLidu070G7+QjXLXcdzvs2+6ZGz+8D0J13TjFzSRv9UCMp8SlzB3 5C6ktsb4QgwKAyYlCUQTbRblJybotf/WyxzZhQg65PDSo6ODuFbRVrCsp5suD5TbpgZQ nGZPtnej0KdHcJiDFNBLvH2HEQGk3mVbjEP1DuFXcPNvnUf6e9XQd8PudNTQdL/XQX0y Xu6AsxZdYNlBXdrlB0EJ1RIbBd3mVH/4sqOLm3L4PAkkJuaYXi2b0ZlC0QWZhPZ1p1Br 0hGg== X-Gm-Message-State: AO0yUKWJB8pMDRhe/JufZR1L3s6fJYbojkTjyb7D4jCfrAQ2hDbMFLTh q/M6l73FE3ainAMmvKmW5jHrBrTcgH/SIaGS+qHRYAqHdLcqtzJmgBDA0pgbCJ0BKrT/Z74Mpz0 Jf/FUqi5QQ3EopG4eFN7HjRlC9V6JKQ== X-Received: by 2002:ac8:5cd3:0:b0:3b9:bb27:e116 with SMTP id s19-20020ac85cd3000000b003b9bb27e116mr27904908qta.1.1675570101552; Sat, 04 Feb 2023 20:08:21 -0800 (PST) X-Google-Smtp-Source: AK7set9HGK5v2UAMD8CBhIRvxHrG14cuB/74A7YNt2hJeu3xk29rYGeudYhoSx2MJ/iKOg7DDlpcXA== X-Received: by 2002:ac8:5cd3:0:b0:3b9:bb27:e116 with SMTP id s19-20020ac85cd3000000b003b9bb27e116mr27904870qta.1.1675570101244; Sat, 04 Feb 2023 20:08:21 -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 v6 2/4] async: Add an optional reentrancy guard to the BH API Date: Sat, 4 Feb 2023 23:07:35 -0500 Message-Id: <20230205040737.3567731-3-alxndr@bu.edu> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230205040737.3567731-1-alxndr@bu.edu> References: <20230205040737.3567731-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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=68.232.154.118; envelope-from=alxndr@bu.edu; helo=esa3.hc2706-39.iphmx.com X-Spam_score_int: -33 X-Spam_score: -3.4 X-Spam_bar: --- X-Spam_report: (-3.4 / 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, RCVD_IN_DNSWL_MED=-2.3, 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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bu.edu) X-ZM-MESSAGEID: 1675570156392100003 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 Acked-by: Michael S. Tsirkin Reviewed-by: Stefan Hajnoczi --- 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 8fba6a3584..3e3bdb9352 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); @@ -331,9 +333,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 @@ -342,7 +346,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 0657b75397..fbebfd7897 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 From nobody Sat May 4 14:41:09 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=bu.edu ARC-Seal: i=1; a=rsa-sha256; t=1675570149; cv=none; d=zohomail.com; s=zohoarc; b=GtrmSo4JC5TK2fTra3/3L6XelGN7c1zTyHQtq+VosPnMNfO21JNZ4tCGlng/NvTuWnWkPH2LPVq72PQc/fjiGIoJVW2vgiKMBFfZJolrtVB32ccHjf7g7pOB1Wq3MlTyyelU9qwXIRM9IKkCLjd6r6hxhevSupjbQLFAeFWx6y0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675570149; 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=xaj0P5NfDOj0aUUjOgS+QNvT8LrWmHCX1h2HgFRSCw8=; b=DDai7KYJGzCWiGXhSyYOaoNY8qa7K3vFWyFqwI/IHsYMTGmuxGwEiuRlun/nUyT6CF2ddtwOwhWQ8GyZQdl1N4IUAqGGKlbvYtQQVyKKAUKTR6AVIL4bZ5zaX1uGJ14Qfgb1enU+4YaokHLwD0hKh6m0wkqP+3yWINApxjC1rv0= 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+importer2=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 167557014982461.39089639822157; Sat, 4 Feb 2023 20:09:09 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pOWKj-0000Qp-L9; Sat, 04 Feb 2023 23:08:54 -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 1pOWKg-0000Om-79 for qemu-devel@nongnu.org; Sat, 04 Feb 2023 23:08:50 -0500 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 1pOWKd-0002lX-G9 for qemu-devel@nongnu.org; Sat, 04 Feb 2023 23:08:49 -0500 Received: from mail-il1-f198.google.com ([209.85.166.198]) by ob1.hc2706-39.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 04 Feb 2023 23:08:46 -0500 Received: by mail-il1-f198.google.com with SMTP id i23-20020a056e021d1700b003111192e89aso5898490ila.10 for ; Sat, 04 Feb 2023 20:08:46 -0800 (PST) Received: from mozz.bu.edu (mozz.bu.edu. [128.197.127.33]) by smtp.gmail.com with ESMTPSA id o2-20020a05620a0d4200b00731c30ac2e8sm1387104qkl.74.2023.02.04.20.08.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Feb 2023 20:08:33 -0800 (PST) X-IronPort-RemoteIP: 209.85.166.198 X-IronPort-MID: 256812067 X-IronPort-Reputation: None X-IronPort-Listener: OutgoingMail X-IronPort-SenderGroup: RELAY_GSUITE X-IronPort-MailFlowPolicy: $RELAYED IronPort-Data: A9a23:Pq0FJavVxtLTU+QPETg14oO4c+fnVOhcMUV32f8akzHdYApBsoF/q tZmKWqBPaqLY2unc4siYI3kpksOsMCGm9RnS1Zo/CljFyga9ZOVVN+UEBzMMnLJJKUvbq7GA +byyDXkBJppJpMJjk71atANlVEliefTAOK5ULSfUsxIbVcMYD87jh5+kPIOjIdtgNyoayuAo tq3qMDEULOf82cc3lk8tuTS93uDgNyo4GlD5gZmOagQ1LPjvyJ94Kw3dPnZw0TQH9E88t6SH 47r0Ly/92XFyBYhYvvNfmHTKxBirhb6ZGBiu1IPM0SQqkEqSh8ai87XAMEhhXJ/0F1lqTzQJ OJl7vRcQS9xVkHFdX90vxNwSkmSNoUfkFPLzOTWXWV+ACQqflO1q8iCAn3aMqU09flFX2981 8UoNRELTR2klfyZ0rKkH7wEasQLdKEHPasas3BkiDDdVLMoGMGTBarN4tBc0XE7gcUm8fT2P ZJIL2oyKk2ePVsWawd/5JEWxY9EglH2dy1epEi9r7dx7mTOpOB0+OKwboqMIITUGq25mG7B+ GTaon/HLS0mE/KV2Tioy3SKlP7myHaTtIU6UefQGuRRqESew3FWBBAIWF+Tp/6/hUijHdVFJ CQpFjEGqKEz8AmqSoC4UUTp8DiLuRkTX9cWGOo/gO2Q9pfpD8+iLjBsZlZ8hBYO7qfamRRCO oe1ou7U IronPort-HdrOrdr: A9a23:Zjp6wKtJi4lsHS8A7qulpuGu7skDpdV00zEX/kB9WHVpm62j5q eTdZEguyMc5wxxZJgfo6H6BEDtexPhHP1OkO0s1NWZLWvbUQKTRekI0WKI+UyEJ8SRzJ8/6U 8ZGZIOceEZCjBB4PoTt2GDYqwdKMvuysyVbfS39QYRcShaL5xt6x59AhuWe3cGPDWvFfICfq Z1/KJ8yAZJAB4sH6KGOkU= 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=xaj0P5NfDOj0aUUjOgS+QNvT8LrWmHCX1h2HgFRSCw8=; b=iaWD72LH/CAletQXbBr1KVCUHAxY1DGpPgSf59OI6axHIFaR8cNzb4KT1K9Y1Rw+KY FUJN7JaA5bcjVfKKnLphqFdxmhq8OoWCFuePnD1scVMmm2TP3v4szAtu040z9HFlAxJi VNIwgKVbQH/OIlRmKU//2V1HocqPUolJgu6fR9K6jTmHDzaanXLNTup2zIUJT1KeT+ej mbsGdElbfHEF3YoWKgf4q+Jhn2fPgQNeEdPyi6vhD3BSaHbGutRcet1GaHW+c/P5oyv6 3y7Hk48hgs+jMrHY6maTWbVqocREgyVJDcQ0gia5nm/1VllpGxE6L4oDqlB0CB68FgGU bAUA== 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=xaj0P5NfDOj0aUUjOgS+QNvT8LrWmHCX1h2HgFRSCw8=; b=e5sq0SFnSyDTcykfGBvRCvEYOfmEmiOl9gnPxHzDz2vhao2CX4QyDSk35r9riPowuD Dh4uZbFMBcMU8yivKfel2+pWw/1P/iWWeA3TurM6GMpuS6aOcIi2K58omkkIwu5c06PK uzqnra4ZRMeIT3bDOM7lU3aoHjCxw+B6tHqHNzNujsiPErmRrEcJmqiX4NfeGhr2/kYq 2n2sTsQ6GW671NO/O4UBcQ3zOtCx2KcOh4lSvpWQM6KiH1aeGgb+w0e9PJ/ZyG04nlMd hoRULP/7Ga1wQVtS/yqAz9SFPf6pgyShse+GtldkLNxZpundsYC17qYsiaRTizjMf0I+ Enbg== X-Gm-Message-State: AO0yUKVwroY2pbklUu5/LwpUZskqNAEu45nSiMhnZZBeJNz/JtHWmyKR jIMHwpFJoGiS9f9G2Nr4+QSjdQ2Q+sH7Hb/+IpGAQU7i4d2j+YUX2gDeMGBMgJ3mZZ2vk/HxxEj H6a1qZtB7qA4xUdJMlly9k0VSWL4bPg== X-Received: by 2002:ac8:7f4e:0:b0:3b8:6c8e:4f8d with SMTP id g14-20020ac87f4e000000b003b86c8e4f8dmr29735341qtk.68.1675570114446; Sat, 04 Feb 2023 20:08:34 -0800 (PST) X-Google-Smtp-Source: AK7set9mVHm/Tk+DydRlTWDKhXZMECv1AAgq7duHEV6Bv94/D3oL7YUPLYNrpnvegLZrmMkGMcNnvg== X-Received: by 2002:ac8:7f4e:0:b0:3b8:6c8e:4f8d with SMTP id g14-20020ac87f4e000000b003b86c8e4f8dmr29735322qtk.68.1675570114193; Sat, 04 Feb 2023 20:08:34 -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 Subject: [PATCH v6 3/4] checkpatch: add qemu_bh_new/aio_bh_new checks Date: Sat, 4 Feb 2023 23:07:36 -0500 Message-Id: <20230205040737.3567731-4-alxndr@bu.edu> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230205040737.3567731-1-alxndr@bu.edu> References: <20230205040737.3567731-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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.71.140.197; envelope-from=alxndr@bu.edu; helo=esa9.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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bu.edu) X-ZM-MESSAGEID: 1675570150286100001 Content-Type: text/plain; charset="utf-8" Advise authors to use the _guarded versions of the APIs, instead. Reviewed-by: Darren Kenny Signed-off-by: Alexander Bulekov Acked-by: Michael S. Tsirkin Reviewed-by: Stefan Hajnoczi --- scripts/checkpatch.pl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 6ecabfb2b5..fbb71c70f8 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -2865,6 +2865,14 @@ sub process { if ($line =3D~ /\bsignal\s*\(/ && !($line =3D~ /SIG_(?:IGN|DFL)/)) { ERROR("use sigaction to establish signal handlers; signal is not portab= le\n" . $herecurr); } +# recommend qemu_bh_new_guarded instead of qemu_bh_new + if ($realfile =3D~ /.*\/hw\/.*/ && $line =3D~ /\bqemu_bh_new\s*\(/= ) { + ERROR("use qemu_bh_new_guarded() instead of qemu_bh_new() to avoid reen= trancy problems\n" . $herecurr); + } +# recommend aio_bh_new_guarded instead of aio_bh_new + if ($realfile =3D~ /.*\/hw\/.*/ && $line =3D~ /\baio_bh_new\s*\(/)= { + ERROR("use aio_bh_new_guarded() instead of aio_bh_new() to avoid reentr= ancy problems\n" . $herecurr); + } # check for module_init(), use category-specific init macros explicitly pl= ease if ($line =3D~ /^module_init\s*\(/) { ERROR("please use block_init(), type_init() etc. instead of module_init= ()\n" . $herecurr); --=20 2.39.0 From nobody Sat May 4 14:41:09 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=bu.edu ARC-Seal: i=1; a=rsa-sha256; t=1675570202; cv=none; d=zohomail.com; s=zohoarc; b=F4zXZJe+UxI2EI/8cLXlZf70WoAnWgls14EFI2ZmFOvIvKT8Y8Va4Gb7RF3IVTrJYQrgczf56loDvMgU34A4xSxe+ynQDlMCKMBT+wSRuQc7KdOHszHiAwO7yMqEbqVaL9x79fUQoz7Jt1xJjkRGnPsH/Qd0yrimDI3N/fKSnoc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675570202; 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=IOvqN3WejMEJLJDUyDO4/PFLr/b6LzRjKv9Yr9nPTE8=; b=GZaQxNyaidPtZaHiVHDG3vA9A4HYabMDRcmj/UUEfS9VUYub/dGgdS1EdHq2w3ZyTjkVQaryfVV2MI0MBGdqX0Y6ko3PsIExaIHuS6lO+tZh0OQaC3JjbqvdwpSxIKAOEGxKp7UxyjyXNawD6mvlE30/c3CV0107manRR2IDfqI= 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+importer2=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 1675570202587419.10682476386967; Sat, 4 Feb 2023 20:10:02 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pOWKy-0000p9-Dv; Sat, 04 Feb 2023 23:09:08 -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 1pOWKu-0000jf-B5 for qemu-devel@nongnu.org; Sat, 04 Feb 2023 23:09:05 -0500 Received: from esa3.hc2706-39.iphmx.com ([68.232.154.118]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pOWKr-0002nR-ET for qemu-devel@nongnu.org; Sat, 04 Feb 2023 23:09:04 -0500 Received: from mail-vk1-f198.google.com ([209.85.221.198]) by ob1.hc2706-39.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 04 Feb 2023 23:08:59 -0500 Received: by mail-vk1-f198.google.com with SMTP id r136-20020a1fc18e000000b003eab7deb229so3788270vkf.11 for ; Sat, 04 Feb 2023 20:08:59 -0800 (PST) Received: from mozz.bu.edu (mozz.bu.edu. [128.197.127.33]) by smtp.gmail.com with ESMTPSA id x11-20020a05620a448b00b0072c01a3b6aasm5031450qkp.100.2023.02.04.20.08.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Feb 2023 20:08:47 -0800 (PST) X-IronPort-RemoteIP: 209.85.221.198 X-IronPort-MID: 256220954 X-IronPort-Reputation: None X-IronPort-Listener: OutgoingMail X-IronPort-SenderGroup: RELAY_GSUITE X-IronPort-MailFlowPolicy: $RELAYED IronPort-Data: A9a23:DrxcFKLVH8SsCow2FE+Rb5clxSXFcZb7ZxGr2PjKsXjdYENS0D1Vz jdJD2yDb6mLYWfxKosnb9u28EgAvZLWz9IxTgdorCE8RH908vbIVI+TRqvS04J+DSFhoGZPt Zh2hgzodZhsJpPkjk7xdOOn9Twkj/rgqoPUUIbsIjp2SRJvVBAvgBdin/9RqoNziJ2yDhjlV ena+qUzA3f4nW8rWo4ow/jb8kg35qyu4GhwUmEWPpingnePzxH5M7pCfcldH1OgKqFIE+izQ fr0zb3R1gs1KD90V7tJOp6iGqE7aue60Tqm0xK6aID76vR2nRHe545gXBYqhea7vB3S9zx54 I0lWZVd0m7FNIWV8AgWe0Aw/y2TocSqUVIISJSymZX78qHIT5fj69tBFm0pAocdxvxyJ04e3 9oGED8fRw/W0opawJrjIgVtrsEqLc2uIoBG/385nGGfAvEhTpTOBa7N4Le03h9q3pEITauYP ZBAL2M+PHwsYDUWUrsTIJs6jOGknFH1bntVpE/9Sa8fuTWNllYpgeWxWDbTUuygf/hlnkakn F3P70L8KS8abNuu7jXQpxpAgceKx0sXQrk6DbC967tmjUOewkQVDxsZU0b9puO24nNSQPpaI k0QvzQt9O08rR37CNb6WBK8rTiPuRt0t8dsLtDWITqlksL8izt1zEBdJtKdQLTKbPMLeAE= IronPort-HdrOrdr: A9a23:jroBpapAQq6Ejw5hH57Kqb8aV5r9eYIsimQD101hICG9vPbo8/ xG+85rqSMc7Qx6ZJhOo6HnBEDtewK/yXcx2/hrAV7AZniahILXFvAa0WKK+VSJcFycygce79 YbT0EXMr3N5DNB/KHHCWeDYrMd6ejC2oyTwcnl81dRYTdDV5xAhj0JdTpz0XcbeOCFP/cE/V aniPav3wDQAUj/p/7VZ0U4Yw== 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=IOvqN3WejMEJLJDUyDO4/PFLr/b6LzRjKv9Yr9nPTE8=; b=aN6T2mk2T6GtehZIfi/g96OZW+dXClvobi5ThTY61Ag1KE2jshi2nyCYulicTs1iDT vPfV3Ty2lKYS0a1P8VJR99ACPU6Kzpl1Yi2SJOrEv0WVN+UgRPBtioNaBN1de5PYM7h3 zzSuYRm/5OSX1PdIRm33dp5QzGsFyhdDq6sEHweuKg0CVEYHl1CslAR62OqLWsQj/yxq ahEo2iEiWHLO4exDq6MWdHTqYfb1Xac9nOdTG59KMh42ud2HKQE6uxtZwc2WKDsJCWp7 dqUxgpkx0Sy/t9YhA3KX+ftYjWTtHDA0+OiMibGrKFS1chTiZkLmfdlChsJgD3QSsPXT UTYw== 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=IOvqN3WejMEJLJDUyDO4/PFLr/b6LzRjKv9Yr9nPTE8=; b=EEFo5u6f9K2nTg239iKAvGzQBWDo52A2MZmoLCHvaNcuelIm9d02PQuDSoECbLSMed Yi0LIQd33prtZgpiTIWicA6DrmuCp7lFcHR5I87TRkE1qEOLDsLzANPwSWGHu6GRQEuf ukMQfpViccqzu9+98yMHF08dC2zuwIRM6tEH+vETeR9iOwql0sqWttBuJ9se4N0ZEFpq WapP1rTAuuZiWLxJzRShOlp29zICkUrY5ZUwHTawNzFUotg931X9ypcYOAu40cVBOChT XOr1jSocn2HSPf4ePYyZHA78NKwyOfJ/6GtZd62eUedVvwu+RVc/Ac3+3y+I8UUupJz9 4Xxw== X-Gm-Message-State: AO0yUKX9cVt6+H0HuW8R7udULBoC1E9OHJRNx23wvAjnWceovfXMoHKh enBVd8fl25dfIqPXzY7KXBGAQBKXoTiy45TqCKny6LmQ3Fx/xorrsHktWaslwJ2+eHoqebvHMxq JFSS9d1iud6TjucjMClZRQK/PYLQ4dw== X-Received: by 2002:ac8:7770:0:b0:3b9:bc8c:c20c with SMTP id h16-20020ac87770000000b003b9bc8cc20cmr11909904qtu.23.1675570127837; Sat, 04 Feb 2023 20:08:47 -0800 (PST) X-Google-Smtp-Source: AK7set/xx0BTLv3UNnqT9/Q54Sxp1Rx3BtxUlZ/n9BQOGfQx1yKHQ1RB/enyaIyXA11Ar0ZAtj7Kcw== X-Received: by 2002:ac8:7770:0:b0:3b9:bc8c:c20c with SMTP id h16-20020ac87770000000b003b9bc8cc20cmr11909853qtu.23.1675570127403; Sat, 04 Feb 2023 20:08:47 -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 , Stefano Stabellini , Anthony Perard , Paul Durrant , Kevin Wolf , Hanna Reitz , Amit Shah , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , John Snow , Peter Maydell , Mark Cave-Ayland , Keith Busch , Klaus Jensen , Fam Zheng , Dmitry Fleytman , "Gonglei (Arei)" , xen-devel@lists.xenproject.org (open list:X86 Xen CPUs), qemu-block@nongnu.org (open list:virtio-blk), qemu-arm@nongnu.org (open list:i.MX31 (kzm)), qemu-ppc@nongnu.org (open list:Old World (g3beige)) Subject: [PATCH v6 4/4] hw: replace most qemu_bh_new calls with qemu_bh_new_guarded Date: Sat, 4 Feb 2023 23:07:37 -0500 Message-Id: <20230205040737.3567731-5-alxndr@bu.edu> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230205040737.3567731-1-alxndr@bu.edu> References: <20230205040737.3567731-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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=68.232.154.118; envelope-from=alxndr@bu.edu; helo=esa3.hc2706-39.iphmx.com X-Spam_score_int: -33 X-Spam_score: -3.4 X-Spam_bar: --- X-Spam_report: (-3.4 / 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, RCVD_IN_DNSWL_MED=-2.3, 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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bu.edu) X-ZM-MESSAGEID: 1675570204491100003 Content-Type: text/plain; charset="utf-8" This protects devices from bh->mmio reentrancy issues. Reviewed-by: Darren Kenny Reviewed-by: Stefan Hajnoczi Signed-off-by: Alexander Bulekov Acked-by: Michael S. Tsirkin Reviewed-by: Michael S. Tsirkin Reviewed-by: Paul Durrant --- hw/9pfs/xen-9p-backend.c | 4 +++- hw/block/dataplane/virtio-blk.c | 3 ++- hw/block/dataplane/xen-block.c | 5 +++-- hw/char/virtio-serial-bus.c | 3 ++- hw/display/qxl.c | 9 ++++++--- hw/display/virtio-gpu.c | 6 ++++-- hw/ide/ahci.c | 3 ++- hw/ide/core.c | 3 ++- hw/misc/imx_rngc.c | 6 ++++-- hw/misc/macio/mac_dbdma.c | 2 +- hw/net/virtio-net.c | 3 ++- hw/nvme/ctrl.c | 6 ++++-- hw/scsi/mptsas.c | 3 ++- hw/scsi/scsi-bus.c | 3 ++- hw/scsi/vmw_pvscsi.c | 3 ++- hw/usb/dev-uas.c | 3 ++- hw/usb/hcd-dwc2.c | 3 ++- hw/usb/hcd-ehci.c | 3 ++- hw/usb/hcd-uhci.c | 2 +- hw/usb/host-libusb.c | 6 ++++-- hw/usb/redirect.c | 6 ++++-- hw/usb/xen-usb.c | 3 ++- hw/virtio/virtio-balloon.c | 5 +++-- hw/virtio/virtio-crypto.c | 3 ++- 24 files changed, 63 insertions(+), 33 deletions(-) diff --git a/hw/9pfs/xen-9p-backend.c b/hw/9pfs/xen-9p-backend.c index 65c4979c3c..f077c1b255 100644 --- a/hw/9pfs/xen-9p-backend.c +++ b/hw/9pfs/xen-9p-backend.c @@ -441,7 +441,9 @@ static int xen_9pfs_connect(struct XenLegacyDevice *xen= dev) xen_9pdev->rings[i].ring.out =3D xen_9pdev->rings[i].data + XEN_FLEX_RING_SIZE(ring_order); =20 - xen_9pdev->rings[i].bh =3D qemu_bh_new(xen_9pfs_bh, &xen_9pdev->ri= ngs[i]); + xen_9pdev->rings[i].bh =3D qemu_bh_new_guarded(xen_9pfs_bh, + &xen_9pdev->rings[i], + &DEVICE(xen_9pdev)->m= em_reentrancy_guard); xen_9pdev->rings[i].out_cons =3D 0; xen_9pdev->rings[i].out_size =3D 0; xen_9pdev->rings[i].inprogress =3D false; diff --git a/hw/block/dataplane/virtio-blk.c b/hw/block/dataplane/virtio-bl= k.c index b28d81737e..a6202997ee 100644 --- a/hw/block/dataplane/virtio-blk.c +++ b/hw/block/dataplane/virtio-blk.c @@ -127,7 +127,8 @@ bool virtio_blk_data_plane_create(VirtIODevice *vdev, V= irtIOBlkConf *conf, } else { s->ctx =3D qemu_get_aio_context(); } - s->bh =3D aio_bh_new(s->ctx, notify_guest_bh, s); + s->bh =3D aio_bh_new_guarded(s->ctx, notify_guest_bh, s, + &DEVICE(vdev)->mem_reentrancy_guard); s->batch_notify_vqs =3D bitmap_new(conf->num_queues); =20 *dataplane =3D s; diff --git a/hw/block/dataplane/xen-block.c b/hw/block/dataplane/xen-block.c index 2785b9e849..e31806b317 100644 --- a/hw/block/dataplane/xen-block.c +++ b/hw/block/dataplane/xen-block.c @@ -632,8 +632,9 @@ XenBlockDataPlane *xen_block_dataplane_create(XenDevice= *xendev, } else { dataplane->ctx =3D qemu_get_aio_context(); } - dataplane->bh =3D aio_bh_new(dataplane->ctx, xen_block_dataplane_bh, - dataplane); + dataplane->bh =3D aio_bh_new_guarded(dataplane->ctx, xen_block_datapla= ne_bh, + dataplane, + &DEVICE(xendev)->mem_reentrancy_gua= rd); =20 return dataplane; } diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c index 7d4601cb5d..dd619f0731 100644 --- a/hw/char/virtio-serial-bus.c +++ b/hw/char/virtio-serial-bus.c @@ -985,7 +985,8 @@ static void virtser_port_device_realize(DeviceState *de= v, Error **errp) return; } =20 - port->bh =3D qemu_bh_new(flush_queued_data_bh, port); + port->bh =3D qemu_bh_new_guarded(flush_queued_data_bh, port, + &dev->mem_reentrancy_guard); port->elem =3D NULL; } =20 diff --git a/hw/display/qxl.c b/hw/display/qxl.c index ec712d3ca2..c0460c4ef1 100644 --- a/hw/display/qxl.c +++ b/hw/display/qxl.c @@ -2201,11 +2201,14 @@ static void qxl_realize_common(PCIQXLDevice *qxl, E= rror **errp) =20 qemu_add_vm_change_state_handler(qxl_vm_change_state_handler, qxl); =20 - qxl->update_irq =3D qemu_bh_new(qxl_update_irq_bh, qxl); + qxl->update_irq =3D qemu_bh_new_guarded(qxl_update_irq_bh, qxl, + &DEVICE(qxl)->mem_reentrancy_gua= rd); qxl_reset_state(qxl); =20 - qxl->update_area_bh =3D qemu_bh_new(qxl_render_update_area_bh, qxl); - qxl->ssd.cursor_bh =3D qemu_bh_new(qemu_spice_cursor_refresh_bh, &qxl-= >ssd); + qxl->update_area_bh =3D qemu_bh_new_guarded(qxl_render_update_area_bh,= qxl, + &DEVICE(qxl)->mem_reentrancy= _guard); + qxl->ssd.cursor_bh =3D qemu_bh_new_guarded(qemu_spice_cursor_refresh_b= h, &qxl->ssd, + &DEVICE(qxl)->mem_reentrancy_= guard); } =20 static void qxl_realize_primary(PCIDevice *dev, Error **errp) diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c index 5e15c79b94..66ac9b6cc5 100644 --- a/hw/display/virtio-gpu.c +++ b/hw/display/virtio-gpu.c @@ -1339,8 +1339,10 @@ void virtio_gpu_device_realize(DeviceState *qdev, Er= ror **errp) =20 g->ctrl_vq =3D virtio_get_queue(vdev, 0); g->cursor_vq =3D virtio_get_queue(vdev, 1); - g->ctrl_bh =3D qemu_bh_new(virtio_gpu_ctrl_bh, g); - g->cursor_bh =3D qemu_bh_new(virtio_gpu_cursor_bh, g); + g->ctrl_bh =3D qemu_bh_new_guarded(virtio_gpu_ctrl_bh, g, + &qdev->mem_reentrancy_guard); + g->cursor_bh =3D qemu_bh_new_guarded(virtio_gpu_cursor_bh, g, + &qdev->mem_reentrancy_guard); QTAILQ_INIT(&g->reslist); QTAILQ_INIT(&g->cmdq); QTAILQ_INIT(&g->fenceq); diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c index 7ce001cacd..37091150cb 100644 --- a/hw/ide/ahci.c +++ b/hw/ide/ahci.c @@ -1508,7 +1508,8 @@ static void ahci_cmd_done(const IDEDMA *dma) ahci_write_fis_d2h(ad); =20 if (ad->port_regs.cmd_issue && !ad->check_bh) { - ad->check_bh =3D qemu_bh_new(ahci_check_cmd_bh, ad); + ad->check_bh =3D qemu_bh_new_guarded(ahci_check_cmd_bh, ad, + &DEVICE(ad)->mem_reentrancy_gua= rd); qemu_bh_schedule(ad->check_bh); } } diff --git a/hw/ide/core.c b/hw/ide/core.c index 5d1039378f..8c8d1a8ec2 100644 --- a/hw/ide/core.c +++ b/hw/ide/core.c @@ -519,7 +519,8 @@ BlockAIOCB *ide_issue_trim( =20 iocb =3D blk_aio_get(&trim_aiocb_info, s->blk, cb, cb_opaque); iocb->s =3D s; - iocb->bh =3D qemu_bh_new(ide_trim_bh_cb, iocb); + iocb->bh =3D qemu_bh_new_guarded(ide_trim_bh_cb, iocb, + &DEVICE(s)->mem_reentrancy_guard); iocb->ret =3D 0; iocb->qiov =3D qiov; iocb->i =3D -1; diff --git a/hw/misc/imx_rngc.c b/hw/misc/imx_rngc.c index 632c03779c..082c6980ad 100644 --- a/hw/misc/imx_rngc.c +++ b/hw/misc/imx_rngc.c @@ -228,8 +228,10 @@ static void imx_rngc_realize(DeviceState *dev, Error *= *errp) sysbus_init_mmio(sbd, &s->iomem); =20 sysbus_init_irq(sbd, &s->irq); - s->self_test_bh =3D qemu_bh_new(imx_rngc_self_test, s); - s->seed_bh =3D qemu_bh_new(imx_rngc_seed, s); + s->self_test_bh =3D qemu_bh_new_guarded(imx_rngc_self_test, s, + &dev->mem_reentrancy_guard); + s->seed_bh =3D qemu_bh_new_guarded(imx_rngc_seed, s, + &dev->mem_reentrancy_guard); } =20 static void imx_rngc_reset(DeviceState *dev) diff --git a/hw/misc/macio/mac_dbdma.c b/hw/misc/macio/mac_dbdma.c index 43bb1f56ba..80a789f32b 100644 --- a/hw/misc/macio/mac_dbdma.c +++ b/hw/misc/macio/mac_dbdma.c @@ -914,7 +914,7 @@ static void mac_dbdma_realize(DeviceState *dev, Error *= *errp) { DBDMAState *s =3D MAC_DBDMA(dev); =20 - s->bh =3D qemu_bh_new(DBDMA_run_bh, s); + s->bh =3D qemu_bh_new_guarded(DBDMA_run_bh, s, &dev->mem_reentrancy_gu= ard); } =20 static void mac_dbdma_class_init(ObjectClass *oc, void *data) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 3ae909041a..a170c724de 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -2885,7 +2885,8 @@ static void virtio_net_add_queue(VirtIONet *n, int in= dex) n->vqs[index].tx_vq =3D virtio_add_queue(vdev, n->net_conf.tx_queue_size, virtio_net_handle_tx_bh); - n->vqs[index].tx_bh =3D qemu_bh_new(virtio_net_tx_bh, &n->vqs[inde= x]); + n->vqs[index].tx_bh =3D qemu_bh_new_guarded(virtio_net_tx_bh, &n->= vqs[index], + &DEVICE(vdev)->mem_reent= rancy_guard); } =20 n->vqs[index].tx_waiting =3D 0; diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c index f25cc2c235..dcb250e772 100644 --- a/hw/nvme/ctrl.c +++ b/hw/nvme/ctrl.c @@ -4318,7 +4318,8 @@ static void nvme_init_sq(NvmeSQueue *sq, NvmeCtrl *n,= uint64_t dma_addr, QTAILQ_INSERT_TAIL(&(sq->req_list), &sq->io_req[i], entry); } =20 - sq->bh =3D qemu_bh_new(nvme_process_sq, sq); + sq->bh =3D qemu_bh_new_guarded(nvme_process_sq, sq, + &DEVICE(sq->ctrl)->mem_reentrancy_guard); =20 if (n->dbbuf_enabled) { sq->db_addr =3D n->dbbuf_dbs + (sqid << 3); @@ -4708,7 +4709,8 @@ static void nvme_init_cq(NvmeCQueue *cq, NvmeCtrl *n,= uint64_t dma_addr, } } n->cq[cqid] =3D cq; - cq->bh =3D qemu_bh_new(nvme_post_cqes, cq); + cq->bh =3D qemu_bh_new_guarded(nvme_post_cqes, cq, + &DEVICE(cq->ctrl)->mem_reentrancy_guard); } =20 static uint16_t nvme_create_cq(NvmeCtrl *n, NvmeRequest *req) diff --git a/hw/scsi/mptsas.c b/hw/scsi/mptsas.c index c485da792c..3de288b454 100644 --- a/hw/scsi/mptsas.c +++ b/hw/scsi/mptsas.c @@ -1322,7 +1322,8 @@ static void mptsas_scsi_realize(PCIDevice *dev, Error= **errp) } s->max_devices =3D MPTSAS_NUM_PORTS; =20 - s->request_bh =3D qemu_bh_new(mptsas_fetch_requests, s); + s->request_bh =3D qemu_bh_new_guarded(mptsas_fetch_requests, s, + &DEVICE(dev)->mem_reentrancy_guard= ); =20 scsi_bus_init(&s->bus, sizeof(s->bus), &dev->qdev, &mptsas_scsi_info); } diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index ceceafb2cd..e5c9f7a53d 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -193,7 +193,8 @@ static void scsi_dma_restart_cb(void *opaque, bool runn= ing, RunState state) AioContext *ctx =3D blk_get_aio_context(s->conf.blk); /* The reference is dropped in scsi_dma_restart_bh.*/ object_ref(OBJECT(s)); - s->bh =3D aio_bh_new(ctx, scsi_dma_restart_bh, s); + s->bh =3D aio_bh_new_guarded(ctx, scsi_dma_restart_bh, s, + &DEVICE(s)->mem_reentrancy_guard); qemu_bh_schedule(s->bh); } } diff --git a/hw/scsi/vmw_pvscsi.c b/hw/scsi/vmw_pvscsi.c index fa76696855..4de34536e9 100644 --- a/hw/scsi/vmw_pvscsi.c +++ b/hw/scsi/vmw_pvscsi.c @@ -1184,7 +1184,8 @@ pvscsi_realizefn(PCIDevice *pci_dev, Error **errp) pcie_endpoint_cap_init(pci_dev, PVSCSI_EXP_EP_OFFSET); } =20 - s->completion_worker =3D qemu_bh_new(pvscsi_process_completion_queue, = s); + s->completion_worker =3D qemu_bh_new_guarded(pvscsi_process_completion= _queue, s, + &DEVICE(pci_dev)->mem_reent= rancy_guard); =20 scsi_bus_init(&s->bus, sizeof(s->bus), DEVICE(pci_dev), &pvscsi_scsi_i= nfo); /* override default SCSI bus hotplug-handler, with pvscsi's one */ diff --git a/hw/usb/dev-uas.c b/hw/usb/dev-uas.c index 88f99c05d5..f013ded91e 100644 --- a/hw/usb/dev-uas.c +++ b/hw/usb/dev-uas.c @@ -937,7 +937,8 @@ static void usb_uas_realize(USBDevice *dev, Error **err= p) =20 QTAILQ_INIT(&uas->results); QTAILQ_INIT(&uas->requests); - uas->status_bh =3D qemu_bh_new(usb_uas_send_status_bh, uas); + uas->status_bh =3D qemu_bh_new_guarded(usb_uas_send_status_bh, uas, + &d->mem_reentrancy_guard); =20 dev->flags |=3D (1 << USB_DEV_FLAG_IS_SCSI_STORAGE); scsi_bus_init(&uas->bus, sizeof(uas->bus), DEVICE(dev), &usb_uas_scsi_= info); diff --git a/hw/usb/hcd-dwc2.c b/hw/usb/hcd-dwc2.c index 8755e9cbb0..a0c4e782b2 100644 --- a/hw/usb/hcd-dwc2.c +++ b/hw/usb/hcd-dwc2.c @@ -1364,7 +1364,8 @@ static void dwc2_realize(DeviceState *dev, Error **er= rp) s->fi =3D USB_FRMINTVL - 1; s->eof_timer =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, dwc2_frame_boundary,= s); s->frame_timer =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, dwc2_work_timer, s= ); - s->async_bh =3D qemu_bh_new(dwc2_work_bh, s); + s->async_bh =3D qemu_bh_new_guarded(dwc2_work_bh, s, + &dev->mem_reentrancy_guard); =20 sysbus_init_irq(sbd, &s->irq); } diff --git a/hw/usb/hcd-ehci.c b/hw/usb/hcd-ehci.c index d4da8dcb8d..c930c60921 100644 --- a/hw/usb/hcd-ehci.c +++ b/hw/usb/hcd-ehci.c @@ -2533,7 +2533,8 @@ void usb_ehci_realize(EHCIState *s, DeviceState *dev,= Error **errp) } =20 s->frame_timer =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, ehci_work_timer, s= ); - s->async_bh =3D qemu_bh_new(ehci_work_bh, s); + s->async_bh =3D qemu_bh_new_guarded(ehci_work_bh, s, + &dev->mem_reentrancy_guard); s->device =3D dev; =20 s->vmstate =3D qemu_add_vm_change_state_handler(usb_ehci_vm_state_chan= ge, s); diff --git a/hw/usb/hcd-uhci.c b/hw/usb/hcd-uhci.c index 30ae0104bb..bdc891f57a 100644 --- a/hw/usb/hcd-uhci.c +++ b/hw/usb/hcd-uhci.c @@ -1193,7 +1193,7 @@ void usb_uhci_common_realize(PCIDevice *dev, Error **= errp) USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL); } } - s->bh =3D qemu_bh_new(uhci_bh, s); + s->bh =3D qemu_bh_new_guarded(uhci_bh, s, &DEVICE(dev)->mem_reentrancy= _guard); s->frame_timer =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, uhci_frame_timer, = s); s->num_ports_vmstate =3D NB_PORTS; QTAILQ_INIT(&s->queues); diff --git a/hw/usb/host-libusb.c b/hw/usb/host-libusb.c index 176868d345..f500db85ab 100644 --- a/hw/usb/host-libusb.c +++ b/hw/usb/host-libusb.c @@ -1141,7 +1141,8 @@ static void usb_host_nodev_bh(void *opaque) static void usb_host_nodev(USBHostDevice *s) { if (!s->bh_nodev) { - s->bh_nodev =3D qemu_bh_new(usb_host_nodev_bh, s); + s->bh_nodev =3D qemu_bh_new_guarded(usb_host_nodev_bh, s, + &DEVICE(s)->mem_reentrancy_guard= ); } qemu_bh_schedule(s->bh_nodev); } @@ -1739,7 +1740,8 @@ static int usb_host_post_load(void *opaque, int versi= on_id) USBHostDevice *dev =3D opaque; =20 if (!dev->bh_postld) { - dev->bh_postld =3D qemu_bh_new(usb_host_post_load_bh, dev); + dev->bh_postld =3D qemu_bh_new_guarded(usb_host_post_load_bh, dev, + &DEVICE(dev)->mem_reentrancy_= guard); } qemu_bh_schedule(dev->bh_postld); dev->bh_postld_pending =3D true; diff --git a/hw/usb/redirect.c b/hw/usb/redirect.c index fd7df599bc..39fbaaab16 100644 --- a/hw/usb/redirect.c +++ b/hw/usb/redirect.c @@ -1441,8 +1441,10 @@ static void usbredir_realize(USBDevice *udev, Error = **errp) } } =20 - dev->chardev_close_bh =3D qemu_bh_new(usbredir_chardev_close_bh, dev); - dev->device_reject_bh =3D qemu_bh_new(usbredir_device_reject_bh, dev); + dev->chardev_close_bh =3D qemu_bh_new_guarded(usbredir_chardev_close_b= h, dev, + &DEVICE(dev)->mem_reentran= cy_guard); + dev->device_reject_bh =3D qemu_bh_new_guarded(usbredir_device_reject_b= h, dev, + &DEVICE(dev)->mem_reentran= cy_guard); dev->attach_timer =3D timer_new_ms(QEMU_CLOCK_VIRTUAL, usbredir_do_att= ach, dev); =20 packet_id_queue_init(&dev->cancelled, dev, "cancelled"); diff --git a/hw/usb/xen-usb.c b/hw/usb/xen-usb.c index 0f7369e7ed..dec91294ad 100644 --- a/hw/usb/xen-usb.c +++ b/hw/usb/xen-usb.c @@ -1021,7 +1021,8 @@ static void usbback_alloc(struct XenLegacyDevice *xen= dev) =20 QTAILQ_INIT(&usbif->req_free_q); QSIMPLEQ_INIT(&usbif->hotplug_q); - usbif->bh =3D qemu_bh_new(usbback_bh, usbif); + usbif->bh =3D qemu_bh_new_guarded(usbback_bh, usbif, + &DEVICE(xendev)->mem_reentrancy_guard); } =20 static int usbback_free(struct XenLegacyDevice *xendev) diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c index 746f07c4d2..309cebacc6 100644 --- a/hw/virtio/virtio-balloon.c +++ b/hw/virtio/virtio-balloon.c @@ -908,8 +908,9 @@ static void virtio_balloon_device_realize(DeviceState *= dev, Error **errp) precopy_add_notifier(&s->free_page_hint_notify); =20 object_ref(OBJECT(s->iothread)); - s->free_page_bh =3D aio_bh_new(iothread_get_aio_context(s->iothrea= d), - virtio_ballloon_get_free_page_hints, = s); + s->free_page_bh =3D aio_bh_new_guarded(iothread_get_aio_context(s-= >iothread), + virtio_ballloon_get_free_page= _hints, s, + &DEVICE(s)->mem_reentrancy_gu= ard); } =20 if (virtio_has_feature(s->host_features, VIRTIO_BALLOON_F_REPORTING)) { diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c index 516425e26a..4c95f1096e 100644 --- a/hw/virtio/virtio-crypto.c +++ b/hw/virtio/virtio-crypto.c @@ -1050,7 +1050,8 @@ static void virtio_crypto_device_realize(DeviceState = *dev, Error **errp) vcrypto->vqs[i].dataq =3D virtio_add_queue(vdev, 1024, virtio_crypto_handle_dataq_b= h); vcrypto->vqs[i].dataq_bh =3D - qemu_bh_new(virtio_crypto_dataq_bh, &vcrypto->vqs[i]); + qemu_bh_new_guarded(virtio_crypto_dataq_bh, &vcrypto->vqs= [i], + &dev->mem_reentrancy_guard); vcrypto->vqs[i].vcrypto =3D vcrypto; } =20 --=20 2.39.0