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 0F1C2C001DE for ; Fri, 28 Jul 2023 17:51:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B0E96B0075; Fri, 28 Jul 2023 13:51:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 761B06B0078; Fri, 28 Jul 2023 13:51:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6777F8D0001; Fri, 28 Jul 2023 13:51:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 58E4E6B0075 for ; Fri, 28 Jul 2023 13:51:41 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A2B29B2298 for ; Fri, 28 Jul 2023 17:51:40 +0000 (UTC) X-FDA: 81061763160.17.B03BAA7 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by imf05.hostedemail.com (Postfix) with SMTP id D139D10000C for ; Fri, 28 Jul 2023 17:51:38 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; spf=pass (imf05.hostedemail.com: domain of stern+64c0f95a@netrider.rowland.org designates 192.131.102.5 as permitted sender) smtp.mailfrom=stern+64c0f95a@netrider.rowland.org; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=harvard.edu (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690566699; 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; bh=1dYShVwsENxk7xxDTpCO8R/rabM82hH/yR6sPiB4nP8=; b=ji9uSwFEKzhphPG8XQtvMeLC12UdmB+RYczMwNNRqBU3nWwRckl4TUfbV7If//62Zh03MN 4JDlNqwQtxWPFA51ffQQzZWuS84Ypk4xEJmXjDV1cBNHSJMLBCP00cX0XavWMFcSEZu0da wdlM7dS9Vt7icpvY4tBpn27yTCOGLoU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690566699; a=rsa-sha256; cv=none; b=Xwx9zO0XR0qcr5VYFkz0EbkAXeVHbx0e4mllnzI03Z3MnaUlMMlJWEo8Vn4cm4cPSRGxwC X9fMo4EdvTjmMytU+IrvYD1Wj+LinjcJtBa8We/0FVYkXCN92VQ9GI9hxr7RiCAMXjCMpc wxtytGrHnT3ODF/eo0uBDxDtmhOSmzs= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; spf=pass (imf05.hostedemail.com: domain of stern+64c0f95a@netrider.rowland.org designates 192.131.102.5 as permitted sender) smtp.mailfrom=stern+64c0f95a@netrider.rowland.org; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=harvard.edu (policy=none) Received: (qmail 45940 invoked by uid 1000); 28 Jul 2023 13:51:37 -0400 Date: Fri, 28 Jul 2023 13:51:37 -0400 From: Alan Stern To: Joel Fernandes Cc: Will Deacon , Jann Horn , paulmck@kernel.org, Andrew Morton , Linus Torvalds , Peter Zijlstra , Suren Baghdasaryan , Matthew Wilcox , linux-kernel@vger.kernel.org, linux-mm@kvack.org, 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: <9fd99405-a3ff-4ab7-b6b7-e74849f1d334@rowland.harvard.edu> References: <20230728124412.GA21303@willie-the-truck> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspamd-Queue-Id: D139D10000C X-Rspam-User: X-Stat-Signature: onzesgdwgefodtaqxqybcz7spyyx3mwx X-Rspamd-Server: rspam03 X-HE-Tag: 1690566698-510906 X-HE-Meta: U2FsdGVkX1/9mAWlUGU+Ld9XSav+K2zp2E4OiOwg4lefCf10CnykZ1ufuv6uts/u2bL9Gl1Xk4fHANlWtOCHZVZvboN/i4+lKX/eF8Nqc3nQtyeOksnCNfKgSCVqDOhELlwEypFgm563UslGmmGTmWWhTa01FnsNOtb3xxQ7rf5bYovUOcTOs8Rbr6yYqMahnXnd6jHooWv0C/MmJGac5/qplrkt/S1bXOvdNuvz9XJlY0VtI0ZI8nNXm/kSWpAZYY0YTrf/wtifcHdrAJgoepMrfEJTxe2/etDYDLrZVoV+WaTlbkk/jIl/SC7gHozz+0xsHay/5qg2LHLUN2WM+hRz92Z7r8iXNrL4zXaxIX5LDrEKmlwdVXwF28jHHipgKoT7eyrmVICST7kP/oC3BP/dlbhTrszlZnz5vsNsP81GajTGILIQ+93WJjFaOAoHI9oXXAh9VZDT8uoQszIPmToD6yfXCBtJKQfXSWjKDjvpgGRQ+R2K3G82JvdJf3w6xFoYzL1deuKxPW5zKUlyv4MIyruW12uDDlt+YrJWoIa6koGEUXTbbuZwfmhekvvtIWe8ZX06QHi5BVj8GQFEsbaw1KNxMEcaVUewYQF3RcCtiy8m6cMCJZpDZAchUoegm33BPrYEo+OcCaaknl4G7YNMCDGYVicXbuaeaAZhUU+jV/aqYr4vmR1URnBIGafNRtP5L54gkqsyobMaVQ1gejZheGWDGuYztDXjM7aoCz8LDv1oHwk98WflDSDmiLGUxGp2RV9l/j2MsX8JBTGcCDByZqWfS5fMJgg26iid4xwX37rH+QbYaJhnb0OCupw9OUlK6h8RKKbP8nGDedFhhL/keSmdcPg5mJiDwXDSsk8hhD6BH+KbWGauW1BIaF8r5doNVHKV+aXQ/X16o8agVPng4um2le4NhUzc7HPPouIpErM0t7F+lvycCnf3PW2pVMDt2Yeo+iCChPgm4kr zT0BOqTB n3wHvq+axea1vLu9RzH6a8IkUOuQm8Fz+Byo+05mybztLTgfKDMMC2MWYl0Wb0Hx9PUy2su0H68BFwQ4aP1N66kkAabRGsAewDeB+p7q9jWbElGuXS8MfEhaaugAJ/IOLsNfZGXXFg9Ujm2u6qtdWP8thrrWuVx1LHHE107XmNGUCubl0UPPDBQbvWXMJbrG1Bb59I3QkBpeRnEqMVU9TAV9TXAj3qp90pofW7AhZJQwrXxszGAoG7K0nUX7dVVSeWocx5GUsSbBVKOTLNFaUKbDC1MZns72iZ2GaqejTURfEdh4ujx79jQTa3KBxCfN909TCqY0cU3T6mCsYw7ymqxZ1bK3ho8f9bR5lpO4NQpS2xTAQ5qqA3EJqCohdEUEPa2ThoydJzNnKE1mq3tJY6YEfGytc/1abioXTXeVyuY9KBwDovp/PL1z4vbe09cc+AKk2X0IsrXS7+oQLYB8tKrd3YE/PYpiyRyljHk9JMGQgCNE= 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 Fri, Jul 28, 2023 at 01:35:43PM -0400, Joel Fernandes wrote: > On Fri, Jul 28, 2023 at 8:44 AM Will Deacon wrote: > > > > 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. > > If I can be a bit brave enough to say -- that appears to be a compiler > bug to me. It seems that the compiler in such an instance violates the > "Sequential Consistency Per Variable" rule? I mean if it can't even > keep SCPV true for a same memory-location load (plain or not) for a > sequence of code, how can it expect the hardware to. It's not a compiler bug. In this example, some other thread performs a write that changes vma->anon_vma from NULL to non-NULL. This write races with the plain reads, and compilers are not required to obey the "Sequential Consistency Per Variable" rule (or indeed, any rule) when there is a data race. Alan Stern