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 X-Spam-Level: X-Spam-Status: No, score=-5.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52EF1C433DB for ; Mon, 21 Dec 2020 23:22:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C9E4222A83 for ; Mon, 21 Dec 2020 23:22:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C9E4222A83 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 11A2F6B0036; Mon, 21 Dec 2020 18:22:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0F2F56B005C; Mon, 21 Dec 2020 18:22:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 02DCB6B0071; Mon, 21 Dec 2020 18:22:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0218.hostedemail.com [216.40.44.218]) by kanga.kvack.org (Postfix) with ESMTP id E23116B0036 for ; Mon, 21 Dec 2020 18:22:57 -0500 (EST) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id B310D3640 for ; Mon, 21 Dec 2020 23:22:57 +0000 (UTC) X-FDA: 77618866794.10.paper58_290b9912745b Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin10.hostedemail.com (Postfix) with ESMTP id 8F5A216A0D1 for ; Mon, 21 Dec 2020 23:22:57 +0000 (UTC) X-HE-Tag: paper58_290b9912745b X-Filterd-Recvd-Size: 5898 Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) by imf16.hostedemail.com (Postfix) with ESMTP for ; Mon, 21 Dec 2020 23:22:56 +0000 (UTC) Received: by mail-lf1-f41.google.com with SMTP id s26so27617635lfc.8 for ; Mon, 21 Dec 2020 15:22:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=buZJFKyPYBzgbeXC66/N/0dTpo1YFcNm0CccjQ3HclY=; b=BaAudlumzhLJFyMaOvrwQsrDy2D4LVnE2wm1c82U854eO+hECKkarKXbDK2eQaxVB+ FUFFWTdEOS/xy8UHt0PWSrAWdiRa763LeATnu6tLnSSMu9m0wgZkjpHp47KRSyeS3zOC K5sYkTtO4S/K/zSR0sI9++ObqZ1CbYGik3uQo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=buZJFKyPYBzgbeXC66/N/0dTpo1YFcNm0CccjQ3HclY=; b=gRYuYFkl1RYlMLK/STLIn8ZFORtEnslGYwiZ5uayPd13Hr5B9evFmQnmYqGFGmHAA2 0p65w0dxlcaaeW3SrQhRJQY7Ny6kgE3Yr84gmXsKCxrHCiTPEjbI2gbS5YbhmDrci64a bzeKvSsMwTUhd3oyRrbxO6fSILGkGAgllnWuiBCcBXqQpD8Wtvvs8EBtWcKM6rulM05J A9dA/sK1Qi5k99kUjhBvzAEZ9rfdiPu3oUK5DgPHAlk4TqixFTBgE9ANm71LyEG0nlFD 6uI8fRRG9p7c/NcsfRSBaALey2/sNa0EICAwlf4xG0wL+6D6bTX6YzGnc/3Q01O7EXcB /edg== X-Gm-Message-State: AOAM532XIE0zpPa7gcxNbhXA79m1+FNBktHDL1V4jKfEhI6PPjwMR8Rv Np2LSomR9jZ2kq+8gtdN2RDlWMAqNE9ezg== X-Google-Smtp-Source: ABdhPJwpZXKopOJ2fb0Sltj545fnTBJoh+LrcGisQUlyPfphL7alHyt0oUSs4NZDJRw2DKDPRC0APQ== X-Received: by 2002:a2e:8ec7:: with SMTP id e7mr8115285ljl.249.1608592975196; Mon, 21 Dec 2020 15:22:55 -0800 (PST) Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com. [209.85.167.54]) by smtp.gmail.com with ESMTPSA id i11sm2475557ljb.19.2020.12.21.15.22.53 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 21 Dec 2020 15:22:53 -0800 (PST) Received: by mail-lf1-f54.google.com with SMTP id h22so18139244lfu.2 for ; Mon, 21 Dec 2020 15:22:53 -0800 (PST) X-Received: by 2002:a19:7d85:: with SMTP id y127mr7803717lfc.253.1608592973153; Mon, 21 Dec 2020 15:22:53 -0800 (PST) MIME-Version: 1.0 References: <20201221172711.GE6640@xz-x1> <76B4F49B-ED61-47EA-9BE4-7F17A26B610D@gmail.com> <9E301C7C-882A-4E0F-8D6D-1170E792065A@gmail.com> <1FCC8F93-FF29-44D3-A73A-DF943D056680@gmail.com> <20201221223041.GL6640@xz-x1> In-Reply-To: <20201221223041.GL6640@xz-x1> From: Linus Torvalds Date: Mon, 21 Dec 2020 15:22:37 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] mm/userfaultfd: fix memory corruption due to writeprotect To: Peter Xu Cc: Nadav Amit , Yu Zhao , Andrea Arcangeli , linux-mm , lkml , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , stable , Minchan Kim , Andy Lutomirski , Will Deacon , Peter Zijlstra Content-Type: text/plain; charset="UTF-8" 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 Mon, Dec 21, 2020 at 2:30 PM Peter Xu wrote: > > AFAIU mprotect() is the only one who modifies the pte using the mmap write > lock. NUMA balancing is also using read mmap lock when changing pte > protections, while my understanding is mprotect() used write lock only because > it manipulates the address space itself (aka. vma layout) rather than modifying > the ptes, so it needs to. So it's ok to change the pte holding only the PTE lock, if it's a *one*way* conversion. That doesn't break the "re-check the PTE contents" model (which predates _all_ of the rest: NUMA, userfaultfd, everything - it's pretty much the original model for our page table operations, and goes back to the dark ages even before SMP and the existence of a page table lock). So for example, a COW will always create a different pte (not just because the page number itself changes - you could imagine a page getting re-used and changing back - but because it's always a RO->RW transition). So two COW operations cannot "undo" each other and fool us into thinking nothing changed. Anything that changes RW->RO - like fork(), for example - needs to take the mmap_lock. NUMA balancing should be ok wrt COW, because it doesn't do that RW->RO thing, it uses the present bit. I think that you are right that NUMA balancing itself might cause other issues, because it can cause that "pte changed and then came back" (for numa protectoipn and then a numa fault) all with just the mmap lock for reading. However, even that shouldn't matter for COW, because the write protect bit is the one that proptects the *contents* of the page, so even if NUMA balancing caused that "load original PTE, then re-check later" to succeed (despite the PTE actually changing in the middle), the _contents_ of the page cannot have changed, so COW is ok. NUMA balancing won't be making a read-only page temporarily writable. Linus