Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4534743pxu; Mon, 21 Dec 2020 15:25:59 -0800 (PST) X-Google-Smtp-Source: ABdhPJxlCuq97r2dhfS0/hMGXkC5uLQz9xDk44+BetACzv2lDVBnOj8NoUqPwCannmV215PHVZ4R X-Received: by 2002:a17:907:a96:: with SMTP id by22mr16915810ejc.171.1608593159073; Mon, 21 Dec 2020 15:25:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608593159; cv=none; d=google.com; s=arc-20160816; b=cZpVy97ykAwF1kVX8+I+fmtANtOlDir6D6albyfC3mqB19kuXWTQWfuIyWbI4iyJ1/ DkNAuniqpKBcJLNyOpTlUCFKNPv9wRlEm/HJmTuAv0hSxHhq6HcCxNo4sPNlDiKtUFyZ AqeNU2rUF0aXw2dcfLoAoZDwAoLOWExRsi/pgHYmZQs9dc8EKuNqoaPXA/UjofJRpzbt PKkmzhQvtp4muAxXuVBY/F/amg63DEfuMErLXKuq/9vRH051Cq5IZ745IVgvDdw80PIz UnrMcO8qHkcsWW5LaUfb7y10SORZvjx9i0BzZmh/MXDqPHcGbS0L58pBSJTL4Tss+x06 4+Jg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=buZJFKyPYBzgbeXC66/N/0dTpo1YFcNm0CccjQ3HclY=; b=enL8fROAMsWsAmiBdU4NkAh6goQvdBkXfRgbP3hYqfKggZIWqRBx0Gt8cBSNwJ9EHB z5MW1xreR2r3ojI++0XI1NBUE2VkFc2sjWV+wxArv7qSWRb0G+gmbZuWou7pBnDPe1rw 6yUdIWA/pqeCm/O07wi+xcsWpaeTRnIS8vhpV+q03CnJX9L8wmuo9o0gD1BpILnouz/z hU7STsGSaqHe3fndIn34+jkC21woCff56bFwCkfn7x/XzLnu5ItD4Vt5bzhzMo0gXeKw 2x6K1xZ6MlhJRDlt+z3iXwFNdaUVzAtkxx5l+XaPeNR0fp2wVUhgPAU5npinhDw76eMh LGZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=BaAudlum; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ga29si1356692ejc.421.2020.12.21.15.25.36; Mon, 21 Dec 2020 15:25:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=BaAudlum; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726095AbgLUXXh (ORCPT + 99 others); Mon, 21 Dec 2020 18:23:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725844AbgLUXXg (ORCPT ); Mon, 21 Dec 2020 18:23:36 -0500 Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [IPv6:2a00:1450:4864:20::132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D579C0613D3 for ; Mon, 21 Dec 2020 15:22:56 -0800 (PST) Received: by mail-lf1-x132.google.com with SMTP id m25so27589928lfc.11 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=j9fIoi4Cq7vaq4DE1CuBxn4xhYNzJcRGlUUkQ017ang8QwheqgtJHVfB64Xythz+IS uemNPlnrmt3InZgkYey7/Hg2kUJM5lINnv78jx0U+IJyGKWRDElp7ScBl/SXeVX+J5PJ UvWxW1z2K27qKAaa5fiZIWKkK6K+Exd1hVTNj2iQtkZa+sHbMLM7EjOUpuircr6r/r9a CbiiJKvdadjC4bn1AM5ZNpyZKfGfrpE6s8GRo83SMcEBnugFiEisUEdYdl+r/a51tbtO tIYvX4WYSSZS5MXgBir9iW0kfX/9MxHWJFmP2LwZujOkuwNM14No8XtyUw9HrE0y841e FqEw== X-Gm-Message-State: AOAM533tMQqgVBmc4WqDHV7OkIjOtQ0rYC8GSDNow8hH4A6GZkcrGQ1S akt1LeI6efOqbVcadCmrI6nfV29H2qhwtw== X-Received: by 2002:a19:4cf:: with SMTP id 198mr7425260lfe.136.1608592974659; Mon, 21 Dec 2020 15:22:54 -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 j20sm2488836ljc.47.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 y19so27543858lfa.13 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" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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