Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2669983rwb; Mon, 15 Aug 2022 09:16:25 -0700 (PDT) X-Google-Smtp-Source: AA6agR43CvVfqaDqUvTyyPgriNL2h6PakoYHQjVroYfl6gFVIlE1srdvQ7hLqGsHkccpaLCrOysO X-Received: by 2002:a17:902:e94e:b0:16d:12b6:b9fe with SMTP id b14-20020a170902e94e00b0016d12b6b9femr17212053pll.152.1660580184847; Mon, 15 Aug 2022 09:16:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660580184; cv=none; d=google.com; s=arc-20160816; b=dbGhGKvoYMM9ncJg+9cuqi7P7h1nzBwGXV1hjGx0gjtYCNDncpOC4tBfK7P34nCdvl EGCPUN0i9j9rO030ECCWcsnwWzRQB2+ZsbXalebOoU1vCWwl4yfKpx/njGQg7xTeXS3E UxkgRF9O2pcE3Jd3QI66UmZTEvzUoIQU7AILjOGbfHWJcpcFsec/KDwpwPn482OQzdHZ S110Khqy7p0b7FIJSDpwb+wc8E+E65K5pShbBzDr420FWhk7ih9LDPu5YKGdmO/KU4HO ZLCYVXio+706CkrExadNrefxmEBIRs7ifYc/9dGhIqR7PNMdUC4+lb8wmyShN9cJmqVm NCUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=P7pDkblFG8LhvzbukiqSCBk2rXRDDjpR4fcFBGBkSJ4=; b=ZuG+xATzNQu+PhzhRD+J14GUMk1YogQlmRyq4t8FcNZTkMgqTRLUQL7ZUKf5KFyUuU sym4iPz+GM24iajmgK9kWBthdwPrloyT8dADi+HjwZ4SZF35zZ9Z0G9h2sgkZRu7aUYg Xr0ccQVCFaBFbbg5qGMestvB1mC795rRNIL8PMBaUjfUg1WheHFG5cYnHCIhjfCxca1G hCM5a5xgrDl1e52JntFf1VxjUTtXvD6iwnMh8cWMC/R7nWvy+zl4AOqx6yBH8U5bzRno ZOa3ueqzIwsganZ4zUzhk0S9PdgAf8VtJJ1J5cWYlZIjItwtPZpEj9diN3FoBCoEX4Wc Ipog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@marcan.st header.s=default header.b=bGKYDqKd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=marcan.st Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 19-20020a630213000000b004293d32dcb2si3368339pgc.215.2022.08.15.09.16.12; Mon, 15 Aug 2022 09:16:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@marcan.st header.s=default header.b=bGKYDqKd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=marcan.st Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232036AbiHOQB2 (ORCPT + 99 others); Mon, 15 Aug 2022 12:01:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231228AbiHOQB1 (ORCPT ); Mon, 15 Aug 2022 12:01:27 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72014641D for ; Mon, 15 Aug 2022 09:01:24 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) (Authenticated sender: marcan@marcan.st) by mail.marcansoft.com (Postfix) with ESMTPSA id 89988447DB; Mon, 15 Aug 2022 16:01:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=marcan.st; s=default; t=1660579282; bh=VmF3FTu694LbmWoV17slTSTf/K4ZQcEVB4/OX7NK47I=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=bGKYDqKdaH6yX2rSOAnTSkSAl0XPV99z3QFH/NDw9sQ8GcNSGibpbnkKFCMMAViTg aDe6DwfITSXxLBzuPOOQjsi03lahDVtiGQNVe5Z1nSCC6mcG8cVLgDgUrtAAFQD/wu +uZl3+8jLU8odv8WViQEVVdyy2WK4c6+hJ8/C9SOf1KaoZjOb2ecNO6ulpnHu789rT gOoI5BG0FflnDtWkFGxiZ+gv2S6je1cV2mSULhnT3xswW57IrEeaKCuetMaO/H+3xE yWpJbxEvPhQoDho8kE49i0cR6BGtaJtLSb62iejoiAkThw8vHlC0kTnKjkhs/6xgoU hKxO0ZktFUAqg== Message-ID: <63cd54a8-3c48-d1b9-406a-c521bd02ee4a@marcan.st> Date: Tue, 16 Aug 2022 01:01:17 +0900 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: Debugging a TTY race condition on M1 (memory ordering dragons) Content-Language: es-ES To: Will Deacon Cc: Linux ARM , Greg KH , jirislaby@kernel.org, Marc Zyngier , Mark Rutland , Peter Zijlstra , Boqun Feng , Catalin Marinas , Asahi Linux , Oliver Neukum , LKML References: <6c089268-4f2c-9fdf-7bcb-107b611fbc21@marcan.st> <20220815134711.GA10374@willie-the-truck> From: Hector Martin In-Reply-To: <20220815134711.GA10374@willie-the-truck> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org (Resend, because I still can't use mail clients properly it seems...) On 15/08/2022 22.47, Will Deacon wrote: > As I mentioned in the thread you linked to, the architecture was undergoing > review in this area. I should've followed back up, but in the end it was > tightened retrospectively to provide the behaviour you wanted. This was > achieved by augmenting the barrier-ordered-before relation with: > > * RW1 is a memory write effect W1 and is generated by an atomic instruction > with both Acquire and Release semantics. > > You can see this in the latest Arm ARM. > > However, test_and_set_bit() is unordered on failure (i.e. when the bit is > unchanged) and uses READ_ONCE() as a quick check before the RmW. See the > "ORDERING" section of Documentation/atomic_bitops.txt. Argh, I'd completely missed that early exit (and had stumbled on an unofficial doc that said it was always ordered, which confused me). Indeed, getting rid of the early exit it fixes the problem. > I think you're missing the "shortcut" in test_and_set_bit(): > > if (READ_ONCE(*p) & mask) > return 1; > > old = arch_atomic_long_fetch_or(mask, (atomic_long_t *)p); > > so if the bit is already set (which I think is the 'ret == false' case) > then you've only got a control dependency here and we elide writing to > B. Completely missed it. Ouch. > >> >> CPU#2: >> DMB ISHST >> STR B > > Missing DMB ISH here from the smp_mb()? Yup, my apologies, that was a brain fart while writing the email. I did have it in the litmus tests (and they indeed completely fail without it). > If that non-atomic store is hitting the same variable, then it cannot land > in the middle of the atomic RmW. The architecture says: > > | The atomic instructions perform atomic read and write operations on a memory > | location such that the architecture guarantees that no modification of that > | memory location by another observer can occur between the read and the write > | defined by that instruction. > > and the .cat file used by herd has a separate constraint for this (see the > "empty rmw & (fre; coe) as atomic" line). Ha, I was using G.a from Jan 2021 (back when I started working on this M1 stuff), and it looks like that wording was added as an issue after that (D17572) :-) > There's never anything obvious when you're working with this sort of stuff, > but my suggestion is that we work towards a litmus tests that we both agree > represents the code and then take it from there. At the moment there's an > awful lof of information, but you can see from my comments that I'm not > up to speed with you yet! I think you nailed it with the early exit, I'd completely missed that. I think I can fairly confidently say that's the issue now. As for the litmus test, indeed with the revised definitions of the memory model / ARM my concerns no longer apply, hence why I couldn't reproduce them (and the hardware, thankfully, seems to agree here). Workqueues are broken. Yay! I'll send a patch. - Hector