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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 2045CC25B0E for ; Tue, 16 Aug 2022 16:45:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=bLYKLJqSx7HtxY7UCOxfM3QMZtcsqlZbil4UbtpDqwU=; b=1t78UUjqavnbvf +skLXNp0ET/Ld2fFIKJAz8foKZUp/Jz1wCfSdgFUkcU9/5oZOU5FHRDpsFsMnQhdnNnDsrA/Mx/Kj Ef//kj7gKOrOXW6TN4ORyNxar/RzCtU9+g4NPxpFeHMVxXyf6uuxRMarKkbm62nlZGcbFIHCZ/zVy 7SIPX/9Wro5bEtYQt/zbXzrtmpmwN09FM2RCYHBXE7MKtuEnnwBl4sUZ2wtYnelFe2ih4oy8bC5zH cC0r/2Bvn/fGruItYgWQ2cQ12I0n7NI/nVR1GqdtsAsqdMuZdFa9o78sQ725doWnsu0XXujHUmQsW 622hFtUKzV9QHmky9dig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oNzfW-004mWF-Lx; Tue, 16 Aug 2022 16:43:54 +0000 Received: from mail-qt1-x833.google.com ([2607:f8b0:4864:20::833]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oNzfT-004mTj-26 for linux-arm-kernel@lists.infradead.org; Tue, 16 Aug 2022 16:43:52 +0000 Received: by mail-qt1-x833.google.com with SMTP id h22so8549046qtu.2 for ; Tue, 16 Aug 2022 09:43:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc; bh=mNYcXw+6OCOLZbXQYQLHUSCGDuExOybfRt9j3BD47FU=; b=aljcIgx1vNs0ATxiQdCFDhTpfP9ZFju0rrLyUBnoV3bDPwfudWuoUYpC2HNOOxcpOW 5JJgy89zTmFNB9LOwjHKS3u5SSM9lH1RIu717MOEHaHRG+4VN4V7qZNHea275SPDcIJu tsZ/I64X+JBB/62fLVT0ABJ0yPYkZ1krvAwQgxHhKkw9ifT5fgN/7Va3ieSOvXfeoxb0 elYiC6vjxEBSGElh8nSOhJ28bPGL49cf4xUF4uk0IaTrIFJZB6wVtzCr16M6549jPu9c 3gW8jTj0VE5UqnDmloR3GDbd5h85fCLloxNxe6v2q/bIjyuKf2RxzEb1IwrlD4f5svW6 F6yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc; bh=mNYcXw+6OCOLZbXQYQLHUSCGDuExOybfRt9j3BD47FU=; b=tvtziZeKfXjnht+ZPrLdwa7vEKRz4FTuVoX0HO6khWpO1LX0WUxrrBzqHYBmeZCwee FjoZPNltKOJStF93rOQjpsX7SLExO3e97qcsGnIpflHX5jnawM3TAeXRZI/NNTS/detf nxJON9O9qIMguUlywSE2wtFDtC7b04zEUEn39kXr8xUI3Kgv4UL6bn025natYLc+YyDi f2dxd8L7H41skn5lNrWDczySvyCwfhsBS5vgPX2kOtR8GlDVKhmMHpQgdfrqB3PhT46D Js6dq6C7p6EbnOy84SwjE0dFpN5y0UyNd+1y8Hd8DGHT2Ap9i59kS0d3EsfAJntsFc24 F/pw== X-Gm-Message-State: ACgBeo2yx2tEBYX7Apy6gFLfNO6c8+RUOqEmoZM9ItNwk3ofuXte11AF EVUAu5cdZT+ib4PRU/NXCqc= X-Google-Smtp-Source: AA6agR61gcjcPiOJhGjgqszCvfHKGTe6Zc79v/Ow7PCU6yxsmHts/AB5J3hc5C/5MLW2gkNnxwixzw== X-Received: by 2002:ac8:5853:0:b0:343:7b95:96ff with SMTP id h19-20020ac85853000000b003437b9596ffmr16743248qth.386.1660668227607; Tue, 16 Aug 2022 09:43:47 -0700 (PDT) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id bp19-20020a05622a1b9300b00342fdc4004fsm10370308qtb.52.2022.08.16.09.43.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Aug 2022 09:43:47 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailauth.nyi.internal (Postfix) with ESMTP id ABA4A27C0071; Tue, 16 Aug 2022 12:43:46 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Tue, 16 Aug 2022 12:43:46 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrvdehgedguddthecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhephedugfduffffteeutddvheeuveelvdfhleelieevtdeguefhgeeuveei udffiedvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedt ieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfh higihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 16 Aug 2022 12:43:44 -0400 (EDT) Date: Tue, 16 Aug 2022 09:43:29 -0700 From: Boqun Feng To: Hector Martin Cc: Will Deacon , Linus Torvalds , Herbert Xu , Tejun Heo , peterz@infradead.org, jirislaby@kernel.org, maz@kernel.org, mark.rutland@arm.com, catalin.marinas@arm.com, oneukum@suse.com, roman.penyaev@profitbricks.com, asahi@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH] workqueue: Fix memory ordering race in queue_work*() Message-ID: References: <20220816134156.GB11202@willie-the-truck> <74559da4-5cd4-7cc4-0303-ab5f6a8b92ae@marcan.st> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <74559da4-5cd4-7cc4-0303-ab5f6a8b92ae@marcan.st> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220816_094351_182455_246F9298 X-CRM114-Status: GOOD ( 26.44 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, Aug 17, 2022 at 01:22:09AM +0900, Hector Martin wrote: > On 16/08/2022 23.55, Boqun Feng wrote: > > On Tue, Aug 16, 2022 at 02:41:57PM +0100, Will Deacon wrote: > >> It's worth noting that with the spinlock-based implementation (i.e. > >> prior to e986a0d6cb36) then we would have the same problem on > >> architectures that implement spinlocks with acquire/release semantics; > >> accesses from outside of the critical section can drift in and reorder > >> with each other there, so the conversion looked legitimate to me in > >> isolation and I vaguely remember going through callers looking for > >> potential issues. Alas, I obviously missed this case. > >> > > > > I just to want to mention that although spinlock-based atomic bitops > > don't provide the full barrier in test_and_set_bit(), but they don't > > have the problem spotted by Hector, because test_and_set_bit() and > > clear_bit() sync with each other via locks: > > > > test_and_set_bit(): > > lock(..); > > old = *p; // mask is already set by other test_and_set_bit() > > *p = old | mask; > > unlock(...); > > clear_bit(): > > lock(..); > > *p ~= mask; > > unlock(..); > > > > so "having a full barrier before test_and_set_bit()" may not be the > > exact thing we need here, as long as a test_and_set_bit() can sync with > > a clear_bit() uncondiontally, then the world is safe. For example, we > > can make test_and_set_bit() RELEASE, and clear_bit() ACQUIRE on ARM64: > > > > test_and_set_bit(): > > atomic_long_fetch_or_release(..); // pair with clear_bit() > > // guarantee everything is > > // observed. > > clear_bit(): > > atomic_long_fetch_andnot_acquire(..); > > > > , maybe that's somewhat cheaper than a full barrier implementation. > > > > Thoughts? Just to find the exact ordering requirement for bitops. > > It's worth pointing out that the workqueue code does *not* pair > test_and_set_bit() with clear_bit(). It does an atomic_long_set() > instead (and then there are explicit barriers around it, which are > expected to pair with the implicit barrier in test_and_set_bit()). If we > define test_and_set_bit() to only sync with clear_bit() and not > necessarily be a true barrier, that breaks the usage of the workqueue code. > Ah, I miss that, but that means the old spinlock-based atomics are totally broken unless spinlock means full barriers on these archs. But still, if we define test_and_set_bit() as RELEASE atomic instead of a full barrier + atomic, it should work for workqueue, right? Do we actually need extra ordering here? WRITE_ONCE(*x, 1); // A test_and_set_bit(..); // a full barrier will order A & B WRITE_ONCE(*y, 1); // B That's something I want to figure out. Regards, Boqun > - Hector _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel