Received: by 2002:ac8:45c5:0:b0:405:464a:c27a with SMTP id e5csp1273077qto; Thu, 27 Jul 2023 09:30:51 -0700 (PDT) X-Google-Smtp-Source: APBJJlGs/P1WtJPq5bKnOAuYXLq343OjYG3hzWwNQ7b7Q2p81nCi6VgVMJ7dSjFTrB/2nQGw7Jk5 X-Received: by 2002:a05:6a20:12ca:b0:13b:1d41:e91c with SMTP id v10-20020a056a2012ca00b0013b1d41e91cmr3944391pzg.5.1690475451423; Thu, 27 Jul 2023 09:30:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690475451; cv=none; d=google.com; s=arc-20160816; b=1GyPWFdsHf1WDFzyRZERtaXGeT/Fj4wOHHblDH3s3iUYJ9V3eSwQCKld8uG6/3RobA 12UFQaIh5R10aKVIn1FNHPivT9DaO8uDeaqXKzQbP1SFrflX/ULPIudHSKBL8JfM+8wC G1sP+M+a5K9W07B94QTaYTVdbkNk1WMdbJk7blAYziDQoyknM1Z3+1enV6PxeUfe1FpH 0BKtf4FKo2N271U/cxMcTXyJacneThiJvo588FOFvay8SyqfbHoVpmhaOXHbbmrELQQY wgkfaQorcQlHj7lQqv5YTPiZ/rceGdKhqDswOS/Da3hazs6FyqIitzaaChHvxEDX5YtQ rlng== 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:reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=nN/Y1RPMNWn9SFc67O4hFoJv0L9d2T9vxJbl/Xr3EjU=; fh=tGdRXbKP8cBoJC3hM/ijyJKtQDC4qvxv3DpxqNoQFNQ=; b=Td5wIYSIOvwD5f2b/gN5CfsEZT57m1FNE0SX6yKtYk+pcjkO8YmAfXjKLd/OyRqYFO tueO09kRLt6IDECT/bDdYnnCaXCrE+j5cTF41SrJs7Tm0w9PcOeZO76w5GF2zh1YNKYk oZpBNf8iMCon8tpg25UMBPgbx/Z5QG6PceODu0oSQTJQySFdAOp0WPFp8JsVTkiTm6iX GU3X9a1C3f5Bn7l8OquSA3zKnyGHdu7YkwvlWYngkXX4tR/frWm2Hvt2WQO/K4OCdSy1 6E1UvSSBdy8HRPockRTyieC808P4GgPqM4dl8iYzEZ+ZQ8/EIy+fGcDLeStXL/phzGjy hOWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PtppGcCL; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e64-20020a636943000000b0055c81b17502si1471989pgc.273.2023.07.27.09.30.38; Thu, 27 Jul 2023 09:30:51 -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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PtppGcCL; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232217AbjG0QQw (ORCPT + 99 others); Thu, 27 Jul 2023 12:16:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231407AbjG0QQu (ORCPT ); Thu, 27 Jul 2023 12:16:50 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B16392728 for ; Thu, 27 Jul 2023 09:16:49 -0700 (PDT) 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 46EBD61EC6 for ; Thu, 27 Jul 2023 16:16:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A5E7BC433C7; Thu, 27 Jul 2023 16:16:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690474608; bh=5PFG1z3pRVnypkDZVwniaTTH6jGfFJ0ZkRrga6GBt08=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=PtppGcCLFWUUf/6l3hEDtuAkOADoiNfKlY62WB1ZnNOvywfUUG5BZJnncsG79USvK UUGrzGYRRaN1pNqGFXRhvmnLIWHMTxTUi9qpKP/y1k4MDU4E41d/BKeS1qVL3qbj0i BPbIIxdIFir6zajtxBo7tNR5xLZwW89ei5AWiLw25I3UyW8RCcWyHOVu1bzThYPaoM N4uVtmnoGAawjdLIb/+cCm1ajwCHGw3ZIu9qUMXrVCaakKb1q1cgxt4jzjZYIFgAgs WH4AuqlD2gz8UVv4kXhbzcA3O23JW35MwXfnJY5IKtOx+ATVT8wF1KTUEWQofBocGa Pt3q8sSKAVGOQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 48A5DCE0B66; Thu, 27 Jul 2023 09:16:48 -0700 (PDT) Date: Thu, 27 Jul 2023 09:16:48 -0700 From: "Paul E. McKenney" To: Alan Stern Cc: Will Deacon , Jann Horn , 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: Reply-To: paulmck@kernel.org References: <20230726214103.3261108-1-jannh@google.com> <31df93bd-4862-432c-8135-5595ffd2bd43@paulmck-laptop> <20230727145747.GB19940@willie-the-truck> <13dc448b-712e-41ce-b74b-b95a55f3e740@rowland.harvard.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <13dc448b-712e-41ce-b74b-b95a55f3e740@rowland.harvard.edu> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 11:44:02AM -0400, Alan Stern wrote: > 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. Agreed. In contrast, control dependencies require quite a bit more care and feeding, and are usually best avoided. But even with the normal RCU address/data dependencies, it is possible to get in trouble. For but one example, comparing a pointer obtained from rcu_dereference() to the address of a static structure is a good way to break your address dependency. (Just yesterday evening I talked to someone who had spent quite a bit of time chasing one of these down, so yes, this is quite real.) > > > 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. Agreed. Furthermore, it is more important to comment plain C-language accesses to shared variables than to comment the likes of READ_ONCE(). "OK, tell me again exactly why you think the compiler cannot mess you up here?" Thanx, Paul