Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp1657234rwb; Wed, 26 Jul 2023 16:37:12 -0700 (PDT) X-Google-Smtp-Source: APBJJlFJtVbV389krZ/GFfZQJSslJbd7H0FoXgCEjPCNo8fpfbjpMiVts2fL1KLwl2yOSGA9qZcL X-Received: by 2002:a05:6a20:9388:b0:123:4ffe:4018 with SMTP id x8-20020a056a20938800b001234ffe4018mr4357418pzh.60.1690414632020; Wed, 26 Jul 2023 16:37:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690414632; cv=none; d=google.com; s=arc-20160816; b=fudUMoEs0kbm3AMVIK3exWyClMwWCKpGtY+wZpymKXIs+a7A9kDhpivTUn46E7rUl+ lWZacZzvTYmKiSVCoVVee5/o/24+OVSV7XcRgaHYEttxCqhNJaxhl/MRWdoV5uL4lQd2 jqeTyfp1GVjp8WL8hgnXVJ8GHUar8TQeGftwD48TGoMkxWrb3zRRr3B+TZX3hjLOOrM9 OU0c5ylfInwKsazTm4LFXcSJyYrtZUWnuPT+nl1mJzdBzjld1BWP4ybpNrVfLenunxg+ uYpn2ZD4TdOtapHcZvLMYJ0KXatVtgJzt2MTNDEmwjavgkqzO30X9giSQxfy+JGIcMuk IXvQ== 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=PJtV/Tp2I0/XX1+DqrcQUfTlHkzuDOXZR9nwTj4nk1Q=; fh=jAYDazXGyYBScavga8Wb1eTQR2u94918bNzoMhRShks=; b=SDJ053X/PcdOJ6ULhqDQeKUf6gr+pgINrRuKQIZH5d+e4scVrA0pdy6Tmf1ugZa82g ApyjNKU7b8jFhlRk4sUZKURG5aZugdgAp78IfyA5pWHMxqp/Wlac7ySOO/tHMnq/PwT+ P2PAKXXpb9u2YjAgejZd5SeF40haM4JT94Ugznt7bPGKy9k+AhaagHvTWY+V69xFKRP2 fwvghjsMNOzdjoCIcOl1DnWv3zvN6dWFTUBTTVJEjJVsCHa+UQc9NK1p/JESbLLxRw/4 HDOHZutV9PHwKJYguVDlde3a2H2rjdsJjDnI5GpZwkI1iirF2Sr5aPzf4xj2WuBg/zQ3 ygOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KwAO6H00; 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 b75-20020a63344e000000b00563deb65f94si101978pga.192.2023.07.26.16.37.00; Wed, 26 Jul 2023 16:37:11 -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=KwAO6H00; 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 S230480AbjGZXTE (ORCPT + 99 others); Wed, 26 Jul 2023 19:19:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229896AbjGZXTD (ORCPT ); Wed, 26 Jul 2023 19:19:03 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44298110 for ; Wed, 26 Jul 2023 16:19:02 -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 D76A461BAF for ; Wed, 26 Jul 2023 23:19:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3CBE5C433C8; Wed, 26 Jul 2023 23:19:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690413541; bh=OyzZD5MHFQ71H/7riF0ScWcazkzij12xVCuT2YfRhl0=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=KwAO6H00AZupIs+wOw57k4SWypFeb4kiLtPomsQBCR8dp2v6ehfiCEWYqQdg/AmH6 49ifx71i+yGverkc41Ppyyv5imfJi8Zkw2Fhb3HvQLCCnpGY+4UpVHVcFFznJN2c7C TAgQSvYPhzj9kqdOxU8fdIZ/mhFQOtg67ZUqps9sPzuINfheocfcUJtmbV/0D5X5TZ XH9jGEBN7S4PQwd92OEINDKEwHleXv4DjNcwtCUxlNNeCPLC9PMBShRzAZICGk/cWg wU2GB+/5u3jo1MYNyKjCkjBedi58jSpdiHR6Mx7u/g17kPzgr1KMVLsMaFrg+5JCG5 VcL9Hwff2A0/A== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id B5E83CE0AD7; Wed, 26 Jul 2023 16:19:00 -0700 (PDT) Date: Wed, 26 Jul 2023 16:19:00 -0700 From: "Paul E. McKenney" To: Jann Horn Cc: Andrew Morton , Linus Torvalds , Peter Zijlstra , Suren Baghdasaryan , Matthew Wilcox , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alan Stern , Andrea Parri , Will Deacon , 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: <31df93bd-4862-432c-8135-5595ffd2bd43@paulmck-laptop> Reply-To: paulmck@kernel.org References: <20230726214103.3261108-1-jannh@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230726214103.3261108-1-jannh@google.com> X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 Wed, Jul 26, 2023 at 11:41:01PM +0200, Jann Horn wrote: > Hi! > > Patch 1 here is a straightforward fix for a race in per-VMA locking code > that can lead to use-after-free; I hope we can get this one into > mainline and stable quickly. > > Patch 2 is a fix for what I believe is a longstanding memory ordering > issue in how vma->anon_vma is used across the MM subsystem; I expect > that this one will have to go through a few iterations of review and > potentially rewrites, because memory ordering is tricky. > (If someone else wants to take over patch 2, I would be very happy.) > > These patches don't really belong together all that much, I'm just > sending them as a series because they'd otherwise conflict. > > I am CCing: > > - Suren because patch 1 touches his code > - Matthew Wilcox because he is also currently working on per-VMA > locking stuff > - all the maintainers/reviewers for the Kernel Memory Consistency Model > so they can help figure out the READ_ONCE() vs smp_load_acquire() > thing READ_ONCE() has weaker ordering properties than smp_load_acquire(). For example, given a pointer gp: p = whichever(gp); a = 1; r1 = p->b; if ((uintptr_t)p & 0x1) WRITE_ONCE(b, 1); WRITE_ONCE(c, 1); Leaving aside the "&" needed by smp_load_acquire(), if "whichever" is "READ_ONCE", then the load from p->b and the WRITE_ONCE() to "b" are ordered after the load from gp (the former due to an address dependency and the latter due to a (fragile) control dependency). The compiler is within its rights to reorder the store to "a" to precede the load from gp. The compiler is forbidden from reordering the store to "c" wtih the load from gp (because both are volatile accesses), but the CPU is completely within its rights to do this reordering. But if "whichever" is "smp_load_acquire()", all four of the subsequent memory accesses are ordered after the load from gp. Similarly, for WRITE_ONCE() and smp_store_release(): p = READ_ONCE(gp); r1 = READ_ONCE(gi); r2 = READ_ONCE(gj); a = 1; WRITE_ONCE(b, 1); if (r1 & 0x1) whichever(p->q, r2); Again leaving aside the "&" needed by smp_store_release(), if "whichever" is WRITE_ONCE(), then the load from gp, the load from gi, and the load from gj are all ordered before the store to p->q (by address dependency, control dependency, and data dependency, respectively). The store to "a" can be reordered with the store to p->q by the compiler. The store to "b" cannot be reordered with the store to p->q by the compiler (again, both are volatile), but the CPU is free to reorder them, especially when whichever() is implemented as a conditional store. But if "whichever" is "smp_store_release()", all five of the earlier memory accesses are ordered before the store to p->q. Does that help, or am I missing the point of your question? Thanx, Paul > - people involved in the previous discussion on the security list > > > Jann Horn (2): > mm: lock_vma_under_rcu() must check vma->anon_vma under vma lock > mm: Fix anon_vma memory ordering > > include/linux/rmap.h | 15 ++++++++++++++- > mm/huge_memory.c | 4 +++- > mm/khugepaged.c | 2 +- > mm/ksm.c | 16 +++++++++++----- > mm/memory.c | 32 ++++++++++++++++++++------------ > mm/mmap.c | 13 ++++++++++--- > mm/rmap.c | 6 ++++-- > mm/swapfile.c | 3 ++- > 8 files changed, 65 insertions(+), 26 deletions(-) > > > base-commit: 20ea1e7d13c1b544fe67c4a8dc3943bb1ab33e6f > -- > 2.41.0.487.g6d72f3e995-goog >