From patchwork Thu Feb 24 10:13:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 545577 Delivered-To: patch@linaro.org Received: by 2002:ac0:d8d1:0:0:0:0:0 with SMTP id b17csp55036imj; Thu, 24 Feb 2022 02:20:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJxT+dj15ev8ni7/v47ae4OpXyXOsvadtMU60ZrStUGkFU9iqEU1kodybJxx8dHdDVLzjZZ9 X-Received: by 2002:a05:6214:2a4b:b0:432:6487:9c54 with SMTP id jf11-20020a0562142a4b00b0043264879c54mr1324953qvb.98.1645698021409; Thu, 24 Feb 2022 02:20:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645698021; cv=none; d=google.com; s=arc-20160816; b=g8qShIziTV03JfbQTHie4glNSvOZ8HtLlX4x+X/uiuzOybuRHDbvoXjhtl4jhC1vLY 3t4zBmeyHHm4Fr304NOniDp52jpWZnJcmFFjVam4N6uOA8ldI3LafYQhFndy76B+e121 1iiXJ9y7y9FaEeeUqQFiOgmGw5Sr81Z1LXdCfMg1qQ+ZvApK/lPdBRXqPcRkVPINeHzA YEIPxhi4JTPTKdFgudnjc3rHb7mcem7yatrl+V1RJX4AQTtmPHedfLKfG4ZU5TCocaos jWHscTXRFC2L4th726TEh45KLTY6LuDvtQpZu77g4/1UDvx47s6mNVT1xN5MVgrRJNmL L/hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=lDwTA/7iM7KovdEVdTm8TUVtlltYAGebQy7UbB1w5os=; b=XXnfrjWYj3YZw5FBUnfTdt6GKyIKCyfDH8Jv6fgnAOh9BpmsPzJYZ35r6vZi1YiapZ JfRTO7LMWbKO7QNEtgpnDauh//MX/zN8jJFcqx97eCOmIEQrvceUeUvo8X95Tv6mDdXn /HYi9/p8swLzsjvZsPzZKFVJmJfWUmN7wPyIv8waP7JHcop61Yo/4SN5KqUQXXKP/e7v 9wIlNntQKoNR1X1BhY9EwQz0PEb3XCYN14tiS8qFk3aC7ZQmvRNJFV6ufnt7vP/R39KU 32H+AOdCoBtfdA2uEsJB+gOo8H8+JomP9s9l0J6k41UIV7GNQzV2FKrjSZEKOUuRjzsg xaPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=bdI+Az9Q; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id v6si984708qtx.643.2022.02.24.02.20.21 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 24 Feb 2022 02:20:21 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=bdI+Az9Q; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:56058 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nNBES-0002Y9-VR for patch@linaro.org; Thu, 24 Feb 2022 05:20:20 -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-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 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+patch=linaro.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];