Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp1615924rwb; Fri, 28 Jul 2023 12:05:33 -0700 (PDT) X-Google-Smtp-Source: APBJJlHEHEjs0lAqKZypM32dTeR5ulJ9ndXX9RVt6emZBrSGSerPY1ancB1jd1sIPogBP+cSuZrW X-Received: by 2002:a17:902:dac2:b0:1b6:69dc:44d2 with SMTP id q2-20020a170902dac200b001b669dc44d2mr3476989plx.51.1690571132779; Fri, 28 Jul 2023 12:05:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690571132; cv=none; d=google.com; s=arc-20160816; b=oQiNqCgMLG46Y8pyp+XNAWeGeaeIxJsK0BSAWgLcLNq7w0sg8aa/B7FROGoYvOk/88 UZ44vLqm3zk0IJNmL3NXaqA3Vty/UQcO+MMmRvaTtkfpJOO0JjIhZvCaX4fCoAvKJWm5 +GLy5yWlf32YvA1N8EsSwJu5Fo01cCnpxvKtypVH56AFZPpBiJZi/kD0ea7Cb5luO2w8 3FeEmlpxFxsF8vH9eTDiWlVr1szF60wctFz5IZnzL3tUY2uak8ryC++S+B0Q8l2OB6te m6cNXqixhvR16ZA1l06Ka6VPgu5Npk2wHyY8PNLesO8RFakXMclDsg8zzZMrBR8SGPBE XzwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=1dYShVwsENxk7xxDTpCO8R/rabM82hH/yR6sPiB4nP8=; fh=8Q6dXBZNCfF8ATLpSadrphp1wvHeqQ8CWbXbuuHDE0k=; b=gPLZHRydNUhWpMy+U0eEOnUCANcMEiNECCFhjjdUZMMEoTkizZfMfuxgrUbV1PXHTG inaOVsjQPtD42hVS8oa72fSbva/2qW4961YrcwdZYSm1isdH532uZYqa9g5XY2YjnXQs VP4PaDKwUyDPsB9hks/ILUP0U/cjYx2uLocAySLMJrfDBDT04gYWDjZhO39EpZdaEvhu nkkDxxgrkSQMkqVg59wNFYQ/E8D9xbdW3Tgq1JKWFaE93r/XRGYBX3B+pzIlXkAUt/xS NDDRCo+VdM0mYlup68jXXplVAFgWYSp4Jkj80QF5BJvA4R7LG6AKF0KhnLltNNQeTmju H5+A== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jc9-20020a17090325c900b001b8bab3d5dasi2786060plb.108.2023.07.28.12.05.18; Fri, 28 Jul 2023 12:05:32 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231690AbjG1Rv4 (ORCPT + 99 others); Fri, 28 Jul 2023 13:51:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231896AbjG1Rvl (ORCPT ); Fri, 28 Jul 2023 13:51:41 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 99C2235BF for ; Fri, 28 Jul 2023 10:51:38 -0700 (PDT) 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-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no 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 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