From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86C6B606A31C for ; Mon, 30 Nov 2026 16:36:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=hds0SmUN Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xY4ML-0006Y9-4c; Mon, 30 Nov 2026 11:36:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xY4MH-0006XY-Eu for qemu-arm@nongnu.org; Mon, 30 Nov 2026 11:36:06 -0500 Received: from mail-wr1-x431.google.com ([2a00:1450:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xY4MF-00012c-D2 for qemu-arm@nongnu.org; Mon, 30 Nov 2026 11:36:05 -0500 Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-491f34b4e0fso4774869f8f.2 for ; Mon, 30 Nov 2026 08:36:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796056561; x=1796661361; darn=nongnu.org; h=content-type: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:content-type; bh=ujrf0fsK47RPfneseYdmyetJcPAN718a9Wwc7kwdQ0Q=; b=hds0SmUN7Yf4Y5dLQ8PPlzpKAdewxziFCXbyd+D6zqweH/ObEOWQOXl2zNiNAHQNHI XPNfHSCKqTRMGRipgmWoxfyhY1Ra2pKhCO8Q+BmWms7rrwaGfMdsm69rBnvIwdxe3G8O cc4K0gkALAe0+0W7lnB/7nAecRp+C8Av31Q0Okv8beHhUkgo/gPoxRKHwZcmesL4sxSo uu2ek39e1PvwxOsaq7eqWCWej3oX2CbolBfjEYEvopFnUhJeleG8t+59er4cWX65ExQw ISGH4CTcZtTda5qo2Al8KMgZ3Kp0tgIEWVzPIZZWgiWqFOnu3w0eTdu6ju92GmVCEeGa S3KQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796056561; x=1796661361; h=content-type:content-transfer-encoding:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ujrf0fsK47RPfneseYdmyetJcPAN718a9Wwc7kwdQ0Q=; b=WSyIVl0/iqq1AhKPe84uRFVyEmsQ8/+tQgj5KNCgaepV83yGm9b87Y9fIpkhxZnLtD IVbyLIK490tVZe1AqztRMtF449yCtLyfrzbW4c3MFjOFN02a2xnpbga+B6tEKssVtje2 VMyOpeTq5oXOTPfxMkaXZjOIcpVQ6HhaGKTTByRpjvkViuoxlpOsVMOu73G38uz8rHkh NYfvUCGo2Lq0q1DrGW7nsS40t1IfSz/9FC7/PZfzn0Htifhn2SZdSM/RmFWN00PQZGKB CHiaxl7YNb+GxyybG5S4fwBVfEYssTBH+1B4moh+2mLIwTT8Iu2pM5enbByStfcnFM/S Hhrw== X-Forwarded-Encrypted: i=1; AL+hErHVM4T0w/0puzd1g5/Qc26ylHAB7vCypNq+9XiT3D5gLfia7ZzYdJmAqjDxGAGJ82FMR4yQfph15w==@nongnu.org X-Gm-Message-State: APuU4Z8juM5NaadPyINQtaZc1vjSJaRKHdvSTyH+iPHb5lREnFoWNBPL zYMWCYQlWDi5RwUbfkd9GownhfJJ1WJ3BG4tFG5YhPwiNm1gM4awVe0P X-Gm-Gg: AZFdTZc99Z6f/2oo7CkghW48UhgzXP2qGRFH9TcZPnylBywbA5cqi0zZTts4C6DqRuz P5mvfXHIWvME2hQlO6gAi0/wV/mmlqRedcqkjQhcWSEKGRuTgnsbsCvsoAiLGoB+xRMIuamOdsO 9xq3OgX14WoxaRarpcfU1pelSzGKcx0siCD+CYwA+ryWVEWN3HxWz2icIoVBj58eQAJU8zTZOEd /9kvi7M/mQwgogPTCNIfpFtWgtDoEVb6QU/1DYnlS7+GrIt521mxRNqaL3Jf2RrIQPu474LB9c8 x8TepabykzCFPQVsGIC9C+MtMPkCRES9ehWMWWkHyVniyrQmBboyJOyElhtRrRmqYAJ53RPWZDs CaxhiTLFlQLos5o1vhsLUUjKoBtrfqippFN0KWvcm78OOqNxI4g3T26Nn86P75OWI0jiLNwHEfk HOkU2pBfqeKr54Sp4EZRuwLve7FMePWXe8wyTHLqkXmEVmzpJTyPk1+gjh4nGkYD87Zc2qVE10j wk1/3hjmYOOw34WhDbkcEu4svC/ierBQhNtNyoJfookwko/pASd+u/pMr6m+kZ53P5o95zgAvqB u+cBq6G/gnv15ZbLXMVO1VfEhiKn8e1J8QX4sML1eoTECJLOABfVFkW5NYFpp6cI+27m0kHyATl FMPQMYpg1mu9A X-Received: by 2002:a05:6000:4203:b0:4a7:557:f96 with SMTP id ffacd0b85a97d-4b10dc5e3ccmr53014472f8f.21.1796056561143; Mon, 30 Nov 2026 08:36:01 -0800 (PST) Received: from noys4.localnet (ip-178-026-066-148.vkd85.pools.vodafone-ip.de. [178.26.66.148]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-4ab6a67f204sm82000855f8f.21.2026.11.30.08.36.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 08:36:00 -0800 (PST) From: Frank Mehnert To: Alex =?UTF-8?B?QmVubsOpZQ==?= , Peter Maydell Cc: qemu-devel@nongnu.org, qemu-arm@nongnu.org, Richard Henderson Subject: Re: [PATCH for-11.2 v2 1/2] target/arm: set the event register on a failed store-exclusive Date: Mon, 30 Nov 2026 17:35:59 +0100 Message-ID: In-Reply-To: References: <20261126143258.1056688-1-frank.mehnert@kernkonzept.com> <87zeuq48pt.fsf@draig.linaro.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Received-SPF: pass client-ip=2a00:1450:4864:20::431; envelope-from=frank.mehnert@gmail.com; helo=mail-wr1-x431.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-arm@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-arm-bounces+qemu-arm=archiver.kernel.org@nongnu.org X-Spam-Status: No, score=-17.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Montag, 30. November 2026 17:23 Peter Maydell wrote: > On Mon, 30 Nov 2026 at 15:53, Alex Benn=C3=A9e w= rote: > > > > Peter Maydell writes: > > > > > On Mon, 30 Nov 2026 at 13:56, Alex Benn=C3=A9e wrote: > > >> > > >> Peter Maydell writes: > > >> > > >> > On Thu, 26 Nov 2026 at 14:33, Frank Mehnert wrote: > > >> >> > > >> >> Since WFE may sleep on A-profile, a WFE executed outside an exclu= sive > > >> >> region halts the vCPU until an event arrives. Lock code commonly > > >> >> branches back to WFE after a failed store-exclusive, for example: > > >> >> > > >> >> 1: wfe > > >> >> ldaxr w1, [x0] > > >> >> cbnz w1, 1b > > >> >> stxr w2, w3, [x0] > > >> >> cbnz w2, 1b > > >> >> > > >> >> The store-exclusive clears exclusive_addr, so the following WFE s= leeps. > > >> >> On hardware, the store by another PE that made the store-exclusiv= e fail > > >> >> cleared the global monitor of this PE, and clearing the global mo= nitor > > >> >> sets the Event Register (Arm ARM D1.6.1, R_XRZRK). The WFE then > > >> >> completes immediately. QEMU does not model this, so with interrup= ts > > >> >> masked and no event stream the vCPU sleeps until another PE execu= tes > > >> >> SEV, which such lock code never does. > > >> >> > > >> >> Set the Event Register when a store-exclusive fails, in both the > > >> >> AArch64 and the AArch32 translator. A failure for another reason = causes > > >> >> a spurious wakeup, which the architecture permits. > > >> >> > > >> >> Fixes: 01b223f895a7 ("target/arm: enable WFE sleeping for A-profi= le") > > >> >> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4938 > > >> >> Reviewed-by: Alex Benn=C3=A9e > > >> >> Signed-off-by: Frank Mehnert > > >> >> --- > > >> >> target/arm/tcg/translate-a64.c | 7 ++++++- > > >> >> target/arm/tcg/translate.c | 7 ++++++- > > >> >> 2 files changed, 12 insertions(+), 2 deletions(-) > > >> >> > > >> >> diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/tran= slate-a64.c > > >> >> index 49472f7d97..45f3b3df03 100644 > > >> >> --- a/target/arm/tcg/translate-a64.c > > >> >> +++ b/target/arm/tcg/translate-a64.c > > >> >> @@ -3531,10 +3531,15 @@ static void gen_store_exclusive(DisasCont= ext *s, int rd, int rt, int rt2, > > >> >> tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive= _val); > > >> >> } > > >> >> tcg_gen_mov_i64(cpu_reg(s, rd), tmp); > > >> >> - tcg_gen_br(done_label); > > >> >> + tcg_gen_brcondi_i64(TCG_COND_EQ, tmp, 0, done_label); > > >> >> > > >> >> gen_set_label(fail_label); > > >> >> tcg_gen_movi_i64(cpu_reg(s, rd), 1); > > >> >> + /* > > >> >> + * A failing store-exclusive means the global monitor was cl= eared, which > > >> >> + * generates a WFE wakeup event. > > >> >> + */ > > >> > > > >> > We might add "Architecturally the wakeup event should have happene= d when > > >> > the store by the other CPU happened, but we do it here." > > >> > > > >> >> + gen_event_reg(); > > >> >> gen_set_label(done_label); > > >> >> tcg_gen_movi_i64(cpu_exclusive_addr, -1); > > >> > > > >> > The architecture permits the global monitor to be cleared when a > > >> > StoreExcl() succeeds as well as when it fails. Given that gen_even= t_reg() > > >> > is just a byte-store, we could also unconditionally do it after the > > >> > done_label, to avoid the branching around. Not sure which is > > >> > preferable. > > >> > > >> Isn't the global monitor subtly different from the per-vcpu event > > >> register in this case as we are modelling the effect of it being cle= ared > > >> without properly modelling the actual global monitor itself? Or does= our > > >> messing with cpu_exclusive_addr count? > > > > > > We're quite a long way out of spec on our exclusives handling, so > > > anything we're doing here is in the realm of "try to make the common > > > cases continue to work". The interaction here is that the event regis= ter > > > spec defines that the CPU's event register is cleared when the global > > > monitor for that CPU goes from "exclusive" to "open". Since we don't > > > really model the global monitor we're trying to patch things up by > > > doing it after the fact for at least some cases where we notice that > > > that transition would have happened. There will still be cases > > > where we should have set the event register and don't, we just hope > > > they don't matter in practice... > > > > > >> However making the store occur always still passes the test case so I > > >> don't have any objection if this is easier. > > > > > > Given that you're allowed to set the event register for any > > > IMPDEF reason, setting it more often is always safe. The tradeoff > > > I think is between "every store exclusive takes an extra conditional > > > branch" and "WFE spuriously wakes up more often". As I say, I don't > > > know which side of that we should prefer. > > > > WFE doesn't wake up here, the event register is local to the PE which is > > currently executing a StoreExcl - it just skips the first attempt to > > sleep after the StoreExcl. >=20 > Yes, which is WFE waking up spuriously (it could/should have gone to > sleep but immediately returned instead). >=20 > > Only SEV broadcasts causes the other PE's to wake up. >=20 > Other PEs doing stores can also cause other PEs to wake up: >=20 > * the state for CPU X in a global monitor goes from "Exclusive > Access" to "Open Access" if CPU Y stores to the marked address > (i.e. to an address that CPU X has done a LoadExcl on) > [fig B2-5 in B2.12.2.1.1 in rev M.d of the Arm ARM, specifically > the "Store(Marked_address,!n)" transition] > * clearing the global monitor for CPU X sets the event > register for CPU X > [R_XRZRK, section D1.7.1] > * this event is a WFE wakeup event for CPU X > [R_KBMFC, section D1.7.1.4] >=20 > So architecturally a sequence like "ldrex from addr; wfe" is supposed to > be wakeable by another CPU doing a plain store to addr. >=20 > Actually that makes me wonder; in the quoted lock sequence in the > commit message / Arm ARM K11.3.4.1: >=20 > sevl // make sure first iteration wfe doesn't sleep > 1: wfe > ldaxr w1, [x0] > cbnz w1, 1b > stxr w2, w3, [x0] > cbnz w2, 1b >=20 > and its corresponding lock-release: > stlr wzr, [x1] >=20 > aren't we missing this case? >=20 > This patch sets the event register when the stxr fails, which > means we won't deadlock when we go round the loop from the > second cbnz. But there's also a branch back up to the WFE for > the "ldaxr load was not 0" case (i.e. where some other CPU already > holds the lock). Architecturally the thing that wakes us up from > that WFE is the fact that the ldaxr means our global monitor > state is "Exclusive Access" and the other CPU doing the STLR > into the lock address will push us out to Open Access, setting > the event register and waking up the WFE. That case is already covered by HELPER(wfe): after the ldaxr, exclusive_addr is still set when the cbnz branches back, and WFE returns immediately while the CPU is in an exclusive region (the check added in 01b223f895a7). So the waiter spins instead of sleeping, but it does not deadlock. Only the path from the failed stxr reaches WFE with exclusive_addr =3D=3D -1, which is what this patch addresses. Regarding setting the event register unconditionally after done_label: I agree. The event register is local to the PE, so after a successful store-exclusive only the next WFE of this PE returns once without sleeping, and lock loops typically start with SEVL anyway. I can send a v3, unless you prefer to keep the conditional version. Thanks, =46rank