Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp3042158pxb; Tue, 12 Jan 2021 05:05:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJw9ngHO9u4v+UHeH0NBSHaEBEBU8eVI5vKAXkR/GTLNYLtSXmexSgQZbfWaJFxbRgjaHEb1 X-Received: by 2002:a05:6402:3192:: with SMTP id di18mr3250929edb.332.1610456718836; Tue, 12 Jan 2021 05:05:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610456718; cv=none; d=google.com; s=arc-20160816; b=YdBkJTPb789XfIVoGLtDIj+Ul2BnqIBu8vovV7pXYNd9ZZSQyjVcewmRcbo8j/yQ01 v9QYQ5jRZhR4xDqpJakuVScDu9/BhEgSoBPstAHMyNGiRE0E0hSm8o0mHvQzQflCUTfC OUmY/F7FS0i4v5wld8QbPGbL2stwceVwNUnZvEbxdu5lDllVTpYIrPOoPZsNDgJkarQK 9hSRb4q9tqtX/MvVQMCksfK/uJ+bPlhxVultMoeLgurX3YI3ZLvp5ZzzCqb/YPeVOPs/ PwNWlNwzaAqqSyUfYJ4YEzlSV2BocJC7RIRRDEWvpcs8vH+RLIouHQ5mhof37kJZDh8e fHow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dmarc-filter:sender:dkim-signature; bh=pDD5+cOOV+kq8coSojkQ9ER1nl8CFfyd8Ps/ttb8Gl0=; b=grPvIGr3kqBJZMvfxIJbhZduxm7w2sD1d3oK3Gt+OoWO95wskP5tYK1H0C253glGyJ wIYAUdwkOa8i/SS+I7WXQUI91ba+hZe63Qv0DvpwwGn4z8xeWhgZLGJTOL89T6yWMYAh eG+aDD83QEgeClYomw/qQW3uBL7eQ92x+3BbkO+TOrBDbTQuxvxkcBZSWV15ty90mvsC aAjwRARM9C+ln+V25mXv62ktJuhqkRP5eRYaZ9QKOG+FthxZHzXqaF6kELQk1f91lMiE JbYJTNC1XdEVy2145uRYF7kdVavjzrQtRUvlEC2sCITNEY20javjaz77AdlAJGThZMyA cPyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=DAlNq3m5; 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 g5si1364057edj.194.2021.01.12.05.04.48; Tue, 12 Jan 2021 05:05:18 -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=@mg.codeaurora.org header.s=smtp header.b=DAlNq3m5; 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 S1730703AbhALLpA (ORCPT + 99 others); Tue, 12 Jan 2021 06:45:00 -0500 Received: from m43-15.mailgun.net ([69.72.43.15]:26809 "EHLO m43-15.mailgun.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728431AbhALLo7 (ORCPT ); Tue, 12 Jan 2021 06:44:59 -0500 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1610451880; h=Content-Transfer-Encoding: Content-Type: In-Reply-To: MIME-Version: Date: Message-ID: From: References: Cc: To: Subject: Sender; bh=pDD5+cOOV+kq8coSojkQ9ER1nl8CFfyd8Ps/ttb8Gl0=; b=DAlNq3m56kueriJv1+piarBt32NMb0qo9vAvNgZvAxRkxCVsvgtafkVsBjFnc1JoXza8c0io AihOuAb+GwCYOJcOwGF7rOPbIz65GWtch1MyozIklAXGrkVwDMyaKWKCUA1EVhy0IEk/IPJr E5iiTESWLSxmBTFTaAtg9eW8uiA= X-Mailgun-Sending-Ip: 69.72.43.15 X-Mailgun-Sid: WyI0MWYwYSIsICJsaW51eC1rZXJuZWxAdmdlci5rZXJuZWwub3JnIiwgImJlOWU0YSJd Received: from smtp.codeaurora.org (ec2-35-166-182-171.us-west-2.compute.amazonaws.com [35.166.182.171]) by smtp-out-n09.prod.us-east-1.postgun.com with SMTP id 5ffd8b888fb3cda82f163e7b (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Tue, 12 Jan 2021 11:44:08 GMT Sender: vinmenon=codeaurora.org@mg.codeaurora.org Received: by smtp.codeaurora.org (Postfix, from userid 1001) id 78D49C43465; Tue, 12 Jan 2021 11:44:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-caf-mail-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=ALL_TRUSTED,BAYES_00, NICE_REPLY_A,SPF_FAIL autolearn=no autolearn_force=no version=3.4.0 Received: from [192.168.1.4] (unknown [183.83.147.228]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: vinmenon) by smtp.codeaurora.org (Postfix) with ESMTPSA id 454D0C433ED; Tue, 12 Jan 2021 11:43:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 454D0C433ED Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; spf=fail smtp.mailfrom=vinmenon@codeaurora.org Subject: Re: [PATCH] mm/userfaultfd: fix memory corruption due to writeprotect To: Peter Zijlstra , Linus Torvalds Cc: Andy Lutomirski , Peter Xu , Nadav Amit , Yu Zhao , Andrea Arcangeli , linux-mm , lkml , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , stable , Minchan Kim , Will Deacon , ldufour@linux.vnet.ibm.com, surenb@google.com References: <9E301C7C-882A-4E0F-8D6D-1170E792065A@gmail.com> <1FCC8F93-FF29-44D3-A73A-DF943D056680@gmail.com> <20201221223041.GL6640@xz-x1> <20210105153727.GK3040@hirez.programming.kicks-ass.net> From: Vinayak Menon Message-ID: Date: Tue, 12 Jan 2021 17:13:57 +0530 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: <20210105153727.GK3040@hirez.programming.kicks-ass.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-GB Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 1/5/2021 9:07 PM, Peter Zijlstra wrote: > On Mon, Dec 21, 2020 at 08:16:11PM -0800, Linus Torvalds wrote: > >> So I think the basic rule is that "if you hold mmap_sem for writing, >> you're always safe". And that really should be considered the >> "default" locking. >> >> ANY time you make a modification to the VM layer, you should basically >> always treat it as a write operation, and get the mmap_sem for >> writing. >> >> Yeah, yeah, that's a bit simplified, and it ignores various special >> cases (and the hardware page table walkers that obviously take no >> locks at all), but if you hold the mmap_sem for writing you won't >> really race with anything else - not page faults, and not other >> "modify this VM". >> To a first approximation, everybody that changes the VM should take >> the mmap_sem for writing, and the readers should just be just about >> page fault handling (and I count GUP as "page fault handling" too - >> it's kind of the same "look up page" rather than "modify vm" kind of >> operation). >> >> And there are just a _lot_ more page faults than there are things that >> modify the page tables and the vma's. >> >> So having that mental model of "lookup of pages in a VM take mmap_semn >> for reading, any modification of the VM uses it for writing" makes >> sense both from a performance angle and a logical standpoint. It's the >> correct model. >> And it's worth noting that COW is still "lookup of pages", even though >> it might modify the page tables in the process. The same way lookup >> can modify the page tables to mark things accessed or dirty. >> >> So COW is still a lookup operation, in ways that "change the >> writabiility of this range" very much is not. COW is "lookup for >> write", and the magic we do to copy to make that write valid is still >> all about the lookup of the page. > (your other email clarified this point; the COW needs to copy while > holding the PTL and we need TLBI under PTL if we're to change this) > >> Which brings up another mental mistake I saw earlier in this thread: >> you should not think "mmap_sem is for vma, and the page table lock is >> for the page table changes". >> >> mmap_sem is the primary lock for any modifications to the VM layout, >> whether it be in the vma's or in the page tables. >> >> Now, the page table lock does exist _in_addition_to_ the mmap_sem, but >> it is partly because >> >> (a) we have things that historically walked the page tables _without_ >> walking the vma's (notably the virtual memory scanning) >> >> (b) we do allow concurrent page faults, so we then need a lower-level >> lock to serialize the parallelism we _do_ have. > And I'm thinking the speculative page fault series steps right into all > this, it fundamentally avoids mmap_sem and entirely relies on the PTL. > > Which opens it up to exactly these races explored here. > > The range lock approach does not suffer this, but I'm still worried > about the actual performance of that thing. Some thoughts on why there may not be an issue with speculative page fault. Adding Laurent as well. Possibility of race against other PTE modifiers 1) Fork - We have seen a case of SPF racing with fork marking PTEs RO and that is described and fixed here https://lore.kernel.org/patchwork/patch/1062672/ 2) mprotect - change_protection in mprotect which does the deferred flush is marked under vm_write_begin/vm_write_end, thus SPF bails out on faults on those VMAs. 3) userfaultfd - mwriteprotect_range is not protected unlike in (2) above. But SPF does not take UFFD faults. 4) hugetlb - hugetlb_change_protection - called from mprotect and covered by (2) above. 5) Concurrent faults - SPF does not handle all faults. Only anon page faults. Of which do_anonymous_page and do_swap_page are NONE/NON-PRESENT->PRESENT transitions without tlb flush. And I hope do_wp_page with RO->RW is fine as well. I could not see a case where speculative path cannot see a PTE update done via a fault on another CPU. -- QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation