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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id ADFD9C0015E for ; Fri, 28 Jul 2023 12:44:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C714E8D0001; Fri, 28 Jul 2023 08:44:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C21EB6B0074; Fri, 28 Jul 2023 08:44:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AE9618D0001; Fri, 28 Jul 2023 08:44:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id A18416B0071 for ; Fri, 28 Jul 2023 08:44:27 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 56F1DC11DA for ; Fri, 28 Jul 2023 12:44:27 +0000 (UTC) X-FDA: 81060988974.29.901588A Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf30.hostedemail.com (Postfix) with ESMTP id 930A080012 for ; Fri, 28 Jul 2023 12:44:24 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=AtmW0C+s; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf30.hostedemail.com: domain of will@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=will@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690548264; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=RBlyYmBmKa3NdWJklkUnB6TgyxhnkiE6bHXxvMms53M=; b=qvTzVMA8D7V3/U38OeilhkGXUWiRxCa4nkjob1AozrCZUY7j5w7k/fGnJveAw5OgqI0f53 Q7gF4po+inN4lY04FxJacdkQ5jdR4TSoIEucCeHMIzboW/BJacR549ngPtUZItWd4fwcOU YM2ZNa8YCgQlFLXePeaGk2WoFVLNTUI= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=AtmW0C+s; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf30.hostedemail.com: domain of will@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=will@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690548264; a=rsa-sha256; cv=none; b=eoE8mqO8dmeuVcFMQ+eyDqz6xQglbGgcSy8HFWcD4jl7fTCJo0fpeoPXpsPY24cbHSvw8G ZB+KLN+Oc2YZuQAHlaJMdyulyvF1osJ8Xw0GqxQ7NbzM8lqx5NFdnEe3DULZNzp388PqhD dYQ2PwbX3RnYMgSK3FcBVVJOF1sFohE= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9BF2162129; Fri, 28 Jul 2023 12:44:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B50F4C433C7; Fri, 28 Jul 2023 12:44:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690548260; bh=v8YrvVoPuUgOyELF1X35nwd/jvp6+wC1gtdJPz4y+Qs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=AtmW0C+sKb5EwCUN9lzSQxju06CENccgDK7vn+k/9oE8TEYvKtwlqIC4kcEhvTbZY UFYAJgHp8WwFvGxUctY7UOWhV1ZKQc4mJQvI2LA3Ul8bhd33KedOMJI7pOiHNyWOv+ UIUh9zD8gj0THO7auxLeWbHPK5Yer3ye1dsb+anJq8Raa9/o7YxrF2ZjO2cAD0viZf f28H4YZzJVwmpduYHcM99AHY7Wp2NIsozV+frM3EgTIdipIJafDLi6kKUALefSPhR+ lHzWdEEASTnL1GCcANUwomvqy1MttSue9OIhVgkzguWncL0rv3s9+4CISCgTJ1rGCq oXcf1DCZk+Pkg== Date: Fri, 28 Jul 2023 13:44:13 +0100 From: Will Deacon To: Joel Fernandes Cc: Jann Horn , paulmck@kernel.org, Andrew Morton , Linus Torvalds , Peter Zijlstra , Suren Baghdasaryan , Matthew Wilcox , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alan Stern , Andrea Parri , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , Akira Yokosawa , Daniel Lustig Subject: Re: [PATCH 0/2] fix vma->anon_vma check for per-VMA locking; fix anon_vma memory ordering Message-ID: <20230728124412.GA21303@willie-the-truck> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) X-Rspamd-Queue-Id: 930A080012 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: jw75umz8uou9sjienwbb7wtjojac9pkk X-HE-Tag: 1690548264-249354 X-HE-Meta: U2FsdGVkX1877sw7s06yApGDhq9eNbzvk0N4XEVcQSBclkrHhTkJLnGUJra6l+c9VyBBB+d45q5z39VNkza+VaoouAkJOY2QpysvAImQDcY8x2SQI4CJt3rgwZ6HGaPHb11XgEXercCb5afysi3CMuGy/VyrE+tL1aGgq2gX2CubHHndnAbNznIX8WRhZDBd8zDwzRpmJX/HwV4E1rof2uwQRMX0o/r4FLtS9tUyRWHMwPdZw0h7AJ+zR1oAmUvP8O6FRFp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/xmYVBnfI74xxvaYrTwshFtYzQC52YMjva5e5F4Q8xhZgkK6pgKUfJWv8PU7NGC5oHTxpPCC5HTbfOfBk1upeYHo+htc8XnuceOoAdov9hpeBg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Jul 27, 2023 at 12:34:44PM -0400, Joel Fernandes wrote: > > On Jul 27, 2023, at 10:57 AM, Will Deacon wrote: > > On Thu, Jul 27, 2023 at 04:39:34PM +0200, Jann Horn wrote: > >> if (READ_ONCE(vma->anon_vma) != NULL) { > >> // we now know that vma->anon_vma cannot change anymore > >> > >> // access the same memory location again with a plain load > >> struct anon_vma *a = vma->anon_vma; > >> > >> // this needs to be address-dependency-ordered against one of > >> // the loads from vma->anon_vma > >> struct anon_vma *root = a->root; > >> } > >> > >> > >> Is this fine? If it is not fine just because the compiler might > >> reorder the plain load of vma->anon_vma before the READ_ONCE() load, > >> would it be fine after adding a barrier() directly after the > >> READ_ONCE()? > > > > I'm _very_ wary of mixing READ_ONCE() and plain loads to the same variable, > > as I've run into cases where you have sequences such as: > > > > // Assume *ptr is initially 0 and somebody else writes it to 1 > > // concurrently > > > > foo = *ptr; > > bar = READ_ONCE(*ptr); > > baz = *ptr; > > > > and you can get foo == baz == 0 but bar == 1 because the compiler only > > ends up reading from memory twice. > > > > That was the root cause behind f069faba6887 ("arm64: mm: Use READ_ONCE > > when dereferencing pointer to pte table"), which was very unpleasant to > > debug. > > Will, Unless I am missing something fundamental, this case is different though. > This case does not care about fewer reads. As long as the first read is volatile, the subsequent loads (even plain) > should work fine, no? > I am not seeing how the compiler can screw that up, so please do enlighten :). I guess the thing I'm worried about is if there is some previous read of 'vma->anon_vma' which didn't use READ_ONCE() and the compiler kept the result around in a register. In that case, 'a' could be NULL, even if the READ_ONCE(vma->anon_vma) returned non-NULL. The crux of the issue is that the compiler can break read-after-read ordering if you don't use READ_ONCE() consistently. Sadly, judging by the other part of the thread from Nadav, it's fiddly to fix this without wrecking the codegen. Will