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=-3.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,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 EBE63C433E0 for ; Tue, 5 Jan 2021 09:00:23 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7D84D2255F for ; Tue, 5 Jan 2021 09:00:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7D84D2255F Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C9B888D006B; Tue, 5 Jan 2021 04:00:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C4C328D0036; Tue, 5 Jan 2021 04:00:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B61428D006B; Tue, 5 Jan 2021 04:00:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id A0CD38D0036 for ; Tue, 5 Jan 2021 04:00:22 -0500 (EST) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 54D6B181AC9C6 for ; Tue, 5 Jan 2021 09:00:22 +0000 (UTC) X-FDA: 77671125084.26.look91_1401acc274d7 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin26.hostedemail.com (Postfix) with ESMTP id 400891804B669 for ; Tue, 5 Jan 2021 09:00:22 +0000 (UTC) X-HE-Tag: look91_1401acc274d7 X-Filterd-Recvd-Size: 5026 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf50.hostedemail.com (Postfix) with ESMTP for ; Tue, 5 Jan 2021 09:00:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description; bh=zkGwQVpT36gCI4ARYhJdLse+sYnJDQpho9D+RW3CCPM=; b=BVW+TM2mvKNW1RESBNOXWft0Pg 9H3XdzAjO68uKY3mRqtw8+LDVgJ/qGl94Aw/ZY0QKKCyncZ/yg13G9OOwJpGD93a6qoEb4h+0NPjK TsujGi532VRiW94THx0KaicmsBy5GOUEf/FjnO9CAlnxT3iMd1h/e+ma/UDjzbQJJExRPY+saJ4Hy dbfoygpjwCz9wgjLRL1T26Sa9kLUOC13GJtRrG7aDjM5irv4W6mb/FPHV0DifcS9QrZoPZf5uBwza El8swWSWu1wdaBS+6Eu1V+AqPGikkRmNQArslrVNRchFrIT/Tgklh8NHMcDavmyHYZbDCuHA33xAo FDq/I7RA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1kwiB8-0013NO-Lt; Tue, 05 Jan 2021 08:59:26 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 42181305815; Tue, 5 Jan 2021 09:58:57 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 2BB4B20CBF47D; Tue, 5 Jan 2021 09:58:57 +0100 (CET) Date: Tue, 5 Jan 2021 09:58:57 +0100 From: Peter Zijlstra To: Andrea Arcangeli Cc: Nadav Amit , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Nadav Amit , Yu Zhao , Andy Lutomirski , Peter Xu , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , Minchan Kim , Will Deacon , Mel Gorman Subject: Re: [RFC PATCH v2 1/2] mm/userfaultfd: fix memory corruption due to writeprotect Message-ID: <20210105085857.GE3040@hirez.programming.kicks-ass.net> References: <20201225092529.3228466-1-namit@vmware.com> <20201225092529.3228466-2-namit@vmware.com> <20210104122227.GL3021@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: Content-Transfer-Encoding: quoted-printable 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, Jan 04, 2021 at 02:24:38PM -0500, Andrea Arcangeli wrote: > On Mon, Jan 04, 2021 at 01:22:27PM +0100, Peter Zijlstra wrote: > > On Fri, Dec 25, 2020 at 01:25:28AM -0800, Nadav Amit wrote: > >=20 > > > The scenario that happens in selftests/vm/userfaultfd is as follows= : > > >=20 > > > cpu0 cpu1 cpu2 > > > ---- ---- ---- > > > [ Writable PTE > > > cached in TLB ] > > > userfaultfd_writeprotect() > > > [ write-*unprotect* ] > > > mwriteprotect_range() > > > mmap_read_lock() > > > change_protection() > > >=20 > > > change_protection_range() > > > ... > > > change_pte_range() > > > [ *clear* =E2=80=9Cwrite=E2=80=9D-bit ] > > > [ defer TLB flushes ] > > > [ page-fault ] > > > ... > > > wp_page_copy() > > > cow_user_page() > > > [ copy page ] > > > [ write to old > > > page ] > > > ... > > > set_pte_at_notify() > >=20 > > Yuck! > >=20 >=20 > Note, the above was posted before we figured out the details so it > wasn't showing the real deferred tlb flush that caused problems (the > one showed on the left causes zero issues). >=20 > The problematic one not pictured is the one of the wrprotect that has > to be running in another CPU which is also isn't picture above. More > accurate traces are posted later in the thread. Lets assume CPU0 does a read-lock, W -> RO with deferred flush. > > Isn't this all rather similar to the problem that resulted in the > > tlb_flush_pending mess? > >=20 > > I still think that's all fundamentally buggered, the much saner solut= ion > > (IMO) would've been to make things wait for the pending flush, instea= d >=20 > How do intend you wait in PT lock while the writer also has to take PT > lock repeatedly before it can do wake_up_var? >=20 > If you release the PT lock before calling wait_tlb_flush_pending it > all falls apart again. I suppose you can check for pending, if found, release lock, wait for 0, and re-take the fault? > This I guess explains why a local pte/hugepmd smp local invlpg is the > only working solution for this issue, similarly to how it's done in rma= p. In that case a local invalidate on CPU1 simply doesn't help anything. CPU1 needs to do a global invalidate or wait for the in-progress one to complete, such that CPU2 is sure to not have a W entry left before CPU1 goes and copies the page.