From nobody Tue May 14 19:51:04 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1645698062814639.0452105971161; Thu, 24 Feb 2022 02:21:02 -0800 (PST) Received: from localhost ([::1]:58486 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nNBF7-0004Dx-S1 for importer@patchew.org; Thu, 24 Feb 2022 05:21:01 -0500 Received: from eggs.gnu.org ([209.51.188.92]:45736) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nNB84-0007HJ-Bv for qemu-devel@nongnu.org; Thu, 24 Feb 2022 05:13:44 -0500 Received: from [2a00:1450:4864:20::42b] (port=41632 helo=mail-wr1-x42b.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nNB80-0008BF-AZ for qemu-devel@nongnu.org; Thu, 24 Feb 2022 05:13:43 -0500 Received: by mail-wr1-x42b.google.com with SMTP id x15so2144923wrg.8 for ; Thu, 24 Feb 2022 02:13:36 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id y12sm2667688wrl.81.2022.02.24.02.13.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 02:13:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lDwTA/7iM7KovdEVdTm8TUVtlltYAGebQy7UbB1w5os=; b=bdI+Az9QiXjzZZ7w9l3kC9w+Trbou85V4TehmO2hTF3DsSrNPWzSMM7NKID00vhcjJ 3BHK8fhDi4jE7LTWhXkIERlvw1KekEtU/yxS9a7uaxjDUR60R9Pq75i/TVmwjxflW+bR uedvd7H+g5RkoUHSA8ORdtzWPPp+fyKjyrJBBKdsOk6OTiTdiK5sC5dxyIpqpNSM87rw 4TTxuwlNSO+pKZ71Gl9DrE8JuJ7uV6M04hkio/ROzSLKGJGuWAssBw7n8ZS8kXi9oAG1 +b5fd+yAynnMXun0QqHXzUyxJ0q2XfYAdsaC8qdLLWu1Q3xvdc2p8gepPicHA2Cs2mNH sETQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lDwTA/7iM7KovdEVdTm8TUVtlltYAGebQy7UbB1w5os=; b=Etis6tzthniJfEECuJ3QKp5UGbX3cEgAwmwyKQJRfCerB4yr4dO+DIKSKj1sVL5Dfc aJEjla7jjJ5ytsLU5ZsJOnrpgFJPkepWvzN7iGdnOuW+Tco7QY/IS3O7E2Xqz2qkyorH Ej+5HoyHXHRaFviHa1sxDfSK/3nEMxcBvgqYyXneiKA6Pe9bWnZRlNSQK+9cqpbCHfL+ OErbKkA95dVSNbdDq1BqTrJoXnUpPA09GFUQnUMzd9cB7rwSJfvDOLu9BsY/mThHyqdu N4FdWyNKFbTjI5g640K7eE+PC1gWKEA479F0CbfKwJ3O4rwVTAGj2fk5xGgi/N1k7IXa emdw== X-Gm-Message-State: AOAM533B2/fuqgUg8TLBb+IjADLxs9ZCZQNGsMpbDfxrP7ue/+IRH8P5 8oNqGaF840Sy8xxjQtTbdRIPPL3kftG/0w== X-Google-Smtp-Source: ABdhPJx0jgKyFB3qrf5+vIb3puPIJUKlisjhG+8aSHVFhK41JUcUYuRIIF9FvUPDvVC6jO06SLRAEA== X-Received: by 2002:a5d:44ce:0:b0:1ed:d3dc:47e with SMTP id z14-20020a5d44ce000000b001edd3dc047emr1634893wrr.7.1645697615940; Thu, 24 Feb 2022 02:13:35 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PATCH v2 1/2] ui/cocoa.m: Fix updateUIInfo threading issues Date: Thu, 24 Feb 2022 10:13:29 +0000 Message-Id: <20220224101330.967429-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220224101330.967429-1-peter.maydell@linaro.org> References: <20220224101330.967429-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::42b (failed) 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=2a00:1450:4864:20::42b; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x42b.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=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: , Cc: Gerd Hoffmann , Akihiko Odaki Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1645698064729100001 Content-Type: text/plain; charset="utf-8" The updateUIInfo method makes Cocoa API calls. It also calls back into QEMU functions like dpy_set_ui_info(). To do this safely, we need to follow two rules: * Cocoa API calls are made on the Cocoa UI thread * When calling back into QEMU we must hold the iothread lock Fix the places where we got this wrong, by taking the iothread lock while executing updateUIInfo, and moving the call in cocoa_switch() inside the dispatch_async block. Some of the Cocoa UI methods which call updateUIInfo are invoked as part of the initial application startup, while we're still doing the little cross-thread dance described in the comment just above call_qemu_main(). This meant they were calling back into the QEMU UI layer before we'd actually finished initializing our display and registered the DisplayChangeListener, which isn't really valid. Once updateUIInfo takes the iothread lock, we no longer get away with this, because during this startup phase the iothread lock is held by the QEMU main-loop thread which is waiting for us to finish our display initialization. So we must suppress updateUIInfo until applicationDidFinishLaunching allows the QEMU main-loop thread to continue. Signed-off-by: Peter Maydell Reviewed-by: Akihiko Odaki Tested-by: Akihiko Odaki --- v1->v2: * name method updateUIInfoLocked, to match existing handleEventLocked * don't call updateUIInfo in cocoa_display_init() -- this happens indirectly as a result of register_displaychangelistener() --- ui/cocoa.m | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index a8f1cdaf926..5ed1495552a 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -522,8 +522,9 @@ QemuCocoaView *cocoaView; } } =20 -- (void) updateUIInfo +- (void) updateUIInfoLocked { + /* Must be called with the iothread lock, i.e. via updateUIInfo */ NSSize frameSize; QemuUIInfo info; =20 @@ -554,6 +555,25 @@ QemuCocoaView *cocoaView; dpy_set_ui_info(dcl.con, &info, TRUE); } =20 +- (void) updateUIInfo +{ + if (!allow_events) { + /* + * Don't try to tell QEMU about UI information in the application + * startup phase -- we haven't yet registered dcl with the QEMU UI + * layer, and also trying to take the iothread lock would deadlock. + * When cocoa_display_init() does register the dcl, the UI layer + * will call cocoa_switch(), which will call updateUIInfo, so + * we don't lose any information here. + */ + return; + } + + with_iothread_lock(^{ + [self updateUIInfoLocked]; + }); +} + - (void)viewDidMoveToWindow { [self updateUIInfo]; @@ -1985,8 +2005,6 @@ static void cocoa_switch(DisplayChangeListener *dcl, =20 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n"); =20 - [cocoaView updateUIInfo]; - // The DisplaySurface will be freed as soon as this callback returns. // We take a reference to the underlying pixman image here so it does // not disappear from under our feet; the switchSurface method will @@ -1994,6 +2012,7 @@ static void cocoa_switch(DisplayChangeListener *dcl, pixman_image_ref(image); =20 dispatch_async(dispatch_get_main_queue(), ^{ + [cocoaView updateUIInfo]; [cocoaView switchSurface:image]; }); [pool release]; --=20 2.25.1 From nobody Tue May 14 19:51:04 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 164569787614698.25926024031457; Thu, 24 Feb 2022 02:17:56 -0800 (PST) Received: from localhost ([::1]:53692 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nNBC6-0000x6-GD for importer@patchew.org; Thu, 24 Feb 2022 05:17:54 -0500 Received: from eggs.gnu.org ([209.51.188.92]:45728) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nNB82-0007Gq-Nb for qemu-devel@nongnu.org; Thu, 24 Feb 2022 05:13:42 -0500 Received: from [2a00:1450:4864:20::436] (port=38694 helo=mail-wr1-x436.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nNB80-0008BK-Ac for qemu-devel@nongnu.org; Thu, 24 Feb 2022 05:13:41 -0500 Received: by mail-wr1-x436.google.com with SMTP id v21so2161993wrv.5 for ; Thu, 24 Feb 2022 02:13:37 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id y12sm2667688wrl.81.2022.02.24.02.13.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 02:13:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HDY8iSmjWZtODpYAMTqlTmfmqbL61NXt9ThdYmBOFvo=; b=EnOeXBLEBo7VztDRL/DdhJaj2OsgLRFzpYhg3Teh1JRa4djuWXQMBkc+JAVrENnsap 2v1kIpCAXmExRFVf9g0CCPwiONI6VaLuElhAwHQIJ9R1a4G1jQI5EmJ8iYGcjgUIQhDF ZQLGSDI+BkoaByg+z0njBeieQje2GsPx+AfTpJk3871EKczmhDxn2fTYGvX9/cJXKP4F 0qZxEzZM5DXgflRmzMExSctGVoKHubEPa4MmjSzK97vM64pDzfTBNwRNQB/aL5aWzm1g kYR3k91VE1rQrRvrdaRA+6EOnGJSh0oZaSIBM9atOFo0jvF3UhxPU10xpbH5ujhmPoGd Kptw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HDY8iSmjWZtODpYAMTqlTmfmqbL61NXt9ThdYmBOFvo=; b=zJ+MG9xvuuJrEqKeO680HephSRZ1y6FRTTdYnhkjio1WqaZG6VX89V9KsrYgi6BYFr 7SIb4K9T/0KhcZ2+d12Nv3P0TZGDR9Q1j81xqRFa7+DgjepzuY68L58B/pB+RV2MqADu sWfNLCCLd/MPjBzqq1JqodkVOZDglYiS8MNBOJVWmC46jZtazF8lC00HgyPR1KM67c4d pFneg2f983JfD++10SRP0qGOyoWnxtVOhzf6AKhoTsYf4xWExruFT1/CdMh5fMrZuuZJ E1NW2Wae9gge/hkelJx8bB3ExhwFjwAJlSdeOlNaiJJfOLiNwTwhAOhYMofVRwmLqyWw 12yw== X-Gm-Message-State: AOAM533kko21rSG9d2M+QqMqRtGmLoNi5xZYkarw7R2B1TLg/csOOTQ+ wL9tY3/iV8+CQ2zJDgRRW5n4+OGmmCT8WQ== X-Google-Smtp-Source: ABdhPJy9pUFck6oz0tzSFVqtc3SC6tyEgDn+yXshIO4qpsetjMMRwoM7h8njwdbVBLtwk1+DXCd3Nw== X-Received: by 2002:adf:fcca:0:b0:1ed:a0ee:f7a1 with SMTP id f10-20020adffcca000000b001eda0eef7a1mr1631138wrs.492.1645697616826; Thu, 24 Feb 2022 02:13:36 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PATCH v2 2/2] ui/cocoa.m: Remove unnecessary NSAutoreleasePools Date: Thu, 24 Feb 2022 10:13:30 +0000 Message-Id: <20220224101330.967429-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220224101330.967429-1-peter.maydell@linaro.org> References: <20220224101330.967429-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::436 (failed) 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=2a00:1450:4864:20::436; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x436.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=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: , Cc: Gerd Hoffmann , Akihiko Odaki Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1645697878268100003 Content-Type: text/plain; charset="utf-8" In commit 6e657e64cdc478 in 2013 we added some autorelease pools to deal with complaints from macOS when we made calls into Cocoa from threads that didn't have automatically created autorelease pools. Later on, macOS got stricter about forbidding cross-thread Cocoa calls, and in commit 5588840ff77800e839d8 we restructured the code to avoid them. This left the autorelease pool creation in several functions without any purpose; delete it. We still need the pool in cocoa_refresh() for the clipboard related code which is called directly there. Signed-off-by: Peter Maydell Reviewed-by: Akihiko Odaki Tested-by: Akihiko Odaki --- ui/cocoa.m | 6 ------ 1 file changed, 6 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index 5ed1495552a..b6e70e9134d 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -1976,8 +1976,6 @@ int main (int argc, char **argv) { static void cocoa_update(DisplayChangeListener *dcl, int x, int y, int w, int h) { - NSAutoreleasePool * pool =3D [[NSAutoreleasePool alloc] init]; - COCOA_DEBUG("qemu_cocoa: cocoa_update\n"); =20 dispatch_async(dispatch_get_main_queue(), ^{ @@ -1993,14 +1991,11 @@ static void cocoa_update(DisplayChangeListener *dcl, } [cocoaView setNeedsDisplayInRect:rect]; }); - - [pool release]; } =20 static void cocoa_switch(DisplayChangeListener *dcl, DisplaySurface *surface) { - NSAutoreleasePool * pool =3D [[NSAutoreleasePool alloc] init]; pixman_image_t *image =3D surface->image; =20 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n"); @@ -2015,7 +2010,6 @@ static void cocoa_switch(DisplayChangeListener *dcl, [cocoaView updateUIInfo]; [cocoaView switchSurface:image]; }); - [pool release]; } =20 static void cocoa_refresh(DisplayChangeListener *dcl) --=20 2.25.1