From mboxrd@z Thu Jan 1 00:00:00 1970 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 05AA5C433F5 for ; Thu, 24 Feb 2022 10:20:58 +0000 (UTC) Received: from localhost ([::1]:58486 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nNBF3-0004Dx-1h for qemu-devel@archiver.kernel.org; Thu, 24 Feb 2022 05:20:57 -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) 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) 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) 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: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::42b (failed) 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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 --- 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; } } -- (void) updateUIInfo +- (void) updateUIInfoLocked { + /* Must be called with the iothread lock, i.e. via updateUIInfo */ NSSize frameSize; QemuUIInfo info; @@ -554,6 +555,25 @@ QemuCocoaView *cocoaView; dpy_set_ui_info(dcl.con, &info, TRUE); } +- (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, COCOA_DEBUG("qemu_cocoa: cocoa_switch\n"); - [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); dispatch_async(dispatch_get_main_queue(), ^{ + [cocoaView updateUIInfo]; [cocoaView switchSurface:image]; }); [pool release]; -- 2.25.1