Received: by 2002:ac8:45c5:0:b0:405:464a:c27a with SMTP id e5csp1263530qto; Thu, 27 Jul 2023 09:14:26 -0700 (PDT) X-Google-Smtp-Source: APBJJlFrX4gM3pMjpOFD89trzHRHhVB1Bxj1v7c8kU8+IftcdDbusMHT25hGx399O01AIvcz08WC X-Received: by 2002:a17:902:988e:b0:1b9:e9f1:91e0 with SMTP id s14-20020a170902988e00b001b9e9f191e0mr3917399plp.41.1690474465643; Thu, 27 Jul 2023 09:14:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690474465; cv=none; d=google.com; s=arc-20160816; b=EQQ3qG2kA6V8nZQcXVBOr9LkVAkX/A0Oz3fCcrVIOixSRGQBhJl8zKEfLgKHlaEz8s JqMVpbAxUUbplzwYD19fd9SLKRhJPBGxoijU1POXSoBaCXB9y87Ab7YTdEe5th6eqolI lyoLCtpHAtVnwB1u9YcGm+w7jr1haeGK452+3nLzYa+crGldjxn4siuZNdOMwVMLXxeS eM5YkF2qx3DXFp+8ctgyhVUJ5kLVktWRKoq+yMuRYtrAwq3mw+zTtsP/Q9wJdYnUTRON Nr/HwLHegKj1Is51gSFGO8wab0eqOsNEgP0yaRiCy9Cel2AlG4ucLH8CjKyt0oW/O7uC 7tCQ== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=UFRj272CuwjkZGW2q7rl0YIToVJlgjtglg3bGRF1LKA=; fh=rds0Kpj6W9mCI7LMt0blbFGSFWxS8st43pDopD1x1PU=; b=zQWp4NHKeGxBi0TCI4YEbZUUuTlRh2fQlNqlK+cM/zOm+3MiOrk8SRHUXSlCBtIk7A e+/02tvEt+2xJ6OkQorw4aaOYXPWxbDDr9SQuTFARru/yvrz3vPjFBEveVo6Y9/yFf45 4bk/PATIK5CNO086/Nj1pAwboATDaQXeQCFkfv0o+beGA2mNJG+OPVb/6qBNY3+U6vPJ a63bPzhSK9ZBaF/re3NJbFCPlAZbLT+hS7N7/+6cyv24mwqLZEskoluG5EQFXiiXpCtz Tgl29rvWmOHgnEsbvgf0ZO23ckNLBf798riHdsJc1P2B/9vVDN7uMN5clqCXefYnZ6B9 mkvg== 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 k2-20020a170902c40200b001b86e3129e2si1584075plk.9.2023.07.27.09.14.12; Thu, 27 Jul 2023 09:14:25 -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 S233462AbjG0PoF (ORCPT + 99 others); Thu, 27 Jul 2023 11:44:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232992AbjG0PoE (ORCPT ); Thu, 27 Jul 2023 11:44:04 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 509152122 for ; Thu, 27 Jul 2023 08:44:03 -0700 (PDT) Received: (qmail 1994132 invoked by uid 1000); 27 Jul 2023 11:44:02 -0400 Date: Thu, 27 Jul 2023 11:44:02 -0400 From: Alan Stern To: Will Deacon 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, Andrea Parri , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , Akira Yokosawa , Daniel Lustig , Joel Fernandes Subject: Re: [PATCH 0/2] fix vma->anon_vma check for per-VMA locking; fix anon_vma memory ordering Message-ID: <13dc448b-712e-41ce-b74b-b95a55f3e740@rowland.harvard.edu> References: <20230726214103.3261108-1-jannh@google.com> <31df93bd-4862-432c-8135-5595ffd2bd43@paulmck-laptop> <20230727145747.GB19940@willie-the-truck> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230727145747.GB19940@willie-the-truck> 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 Thu, Jul 27, 2023 at 03:57:47PM +0100, Will Deacon wrote: > On Thu, Jul 27, 2023 at 04:39:34PM +0200, Jann Horn wrote: > > Assume that we are holding some kind of lock that ensures that the > > only possible concurrent update to "vma->anon_vma" is that it changes > > from a NULL pointer to a non-NULL pointer (using smp_store_release()). > > > > > > 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; > > } This reads a little oddly, perhaps because it's a fragment from a larger piece of code. Still, if I were doing something like this, I'd write it as: struct anon_vma *a; a = READ_ONCE(vma->anon_vma); if (a != NULL) { struct anon_vma *root = a->root; ... thus eliminating the possibility of confusion from multiple reads of the same address. In this situation, the ordering of the two reads is guaranteed by the address dependency. And people shouldn't worry too much about using that sort of ordering; RCU relies on it critically, all the time. > > 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. Indeed, that's the sort of thing that can happen when plain accesses are involved in a race. Alan Stern