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=-2.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 C83E5C433DB for ; Tue, 12 Jan 2021 19:15:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 57091230F9 for ; Tue, 12 Jan 2021 19:15:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 57091230F9 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5ACD36B00A9; Tue, 12 Jan 2021 14:15:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 55D296B00AA; Tue, 12 Jan 2021 14:15:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 44CF16B00AB; Tue, 12 Jan 2021 14:15:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0009.hostedemail.com [216.40.44.9]) by kanga.kvack.org (Postfix) with ESMTP id 31BD26B00A9 for ; Tue, 12 Jan 2021 14:15:49 -0500 (EST) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id F0CCF8248068 for ; Tue, 12 Jan 2021 19:15:48 +0000 (UTC) X-FDA: 77698077618.03.yard26_1e0277127517 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin03.hostedemail.com (Postfix) with ESMTP id CF5C228A4EA for ; Tue, 12 Jan 2021 19:15:48 +0000 (UTC) X-HE-Tag: yard26_1e0277127517 X-Filterd-Recvd-Size: 7447 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by imf50.hostedemail.com (Postfix) with ESMTP for ; Tue, 12 Jan 2021 19:15:48 +0000 (UTC) Received: by mail-pj1-f53.google.com with SMTP id v1so2223292pjr.2 for ; Tue, 12 Jan 2021 11:15:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=0tRaNGrEjZ7VVI8rFnwR+cCsJ6NqUdiRrcFX266r1hA=; b=nZvzMKx81n2G28HYoQ/+aIE94yv+8MT80iS8KL/p+cZogpTMtg7LEaE6IaLKjN5npw P45Xye1qNiJqZXvZIHqwdvJl4tls1lzIkP271lsxVIWqNfaZ2m7ZUVPzR9nwLs6Yeqn+ wUfy7GVR6z4Ljy52m2pZHBUbVZ+1OqAjgvsYGJDuBJVPP9vep2XnhENByI5DyI6iRPRQ qs6Iu8ulYLuLYuh7u+13QGzHWBDvIHilrTtOdDCZfYHXFfuPuhEd9HcJpEyY46uY/E57 NBOWxhWnB911wOM1z9imVOIf0uGy/3ORlYD0wwFDlfTxQGYW1ex4FmAN0MDzn/kQLzOC IlFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=0tRaNGrEjZ7VVI8rFnwR+cCsJ6NqUdiRrcFX266r1hA=; b=gAT0JRn/+Q4vxnL4/PFwLgGx24lP9vpDr5czTmrn+klPNo0JAUbNWTmofaUfbFusTE IWI9XMmbu81AMcvk2mcu4P/lktMGLm21C97nwNF/9QbGRs7/VQXbpIu8o1s5O/Na/Ncb 6orEbzONNAMYv0vU5Qa++1050loJMPxgwpSRkCnR+RZfAePtgskGCxIOXMD04XJQ5Hev rN5rYoyHDQwFh7kiB8s5v/w/0H6Aaj9lXXFtf9Xqppfwc2iW7svoAGTjqF2Xjc1Evzd4 UKxmST8YxcBNghxB5E92REXy6aGV3fOc/yXRTi/X2U2jkPKZ5sDu/CopSydqqd3YAL9I B9tg== X-Gm-Message-State: AOAM530tx0JGA6rq8LMRe4nRY5S/WFc54pPeP5FUrJeccc+bY1H+a2YE 6HGKOriwMcn0FBzINuctNHs= X-Google-Smtp-Source: ABdhPJzTSMc3aE7AVU9T1WA+JTz7FKFYlJFbcpHt9QckdzpjexzJv1H5KVXZm87aONq1oi7Qirw0nA== X-Received: by 2002:a17:90b:3892:: with SMTP id mu18mr602891pjb.143.1610478947197; Tue, 12 Jan 2021 11:15:47 -0800 (PST) Received: from [192.168.88.245] (c-24-6-216-183.hsd1.ca.comcast.net. [24.6.216.183]) by smtp.gmail.com with ESMTPSA id iw4sm4194789pjb.55.2021.01.12.11.15.44 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Jan 2021 11:15:46 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) Subject: Re: [PATCH] mm/userfaultfd: fix memory corruption due to writeprotect From: Nadav Amit In-Reply-To: Date: Tue, 12 Jan 2021 11:15:43 -0800 Cc: Peter Zijlstra , Vinayak Menon , Linus Torvalds , Andy Lutomirski , Peter Xu , Yu Zhao , Andrea Arcangeli , linux-mm , lkml , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , stable , Minchan Kim , Will Deacon , surenb@google.com Content-Transfer-Encoding: quoted-printable Message-Id: References: <1FCC8F93-FF29-44D3-A73A-DF943D056680@gmail.com> <20201221223041.GL6640@xz-x1> <20210105153727.GK3040@hirez.programming.kicks-ass.net> <0201238b-e716-2a3c-e9ea-d5294ff77525@linux.vnet.ibm.com> To: Laurent Dufour X-Mailer: Apple Mail (2.3608.120.23.2.4) 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 Jan 12, 2021, at 11:02 AM, Laurent Dufour = wrote: >=20 > Le 12/01/2021 =C3=A0 17:57, Peter Zijlstra a =C3=A9crit : >> On Tue, Jan 12, 2021 at 04:47:17PM +0100, Laurent Dufour wrote: >>> Le 12/01/2021 =C3=A0 12:43, Vinayak Menon a =C3=A9crit : >>>> Possibility of race against other PTE modifiers >>>>=20 >>>> 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/ >> Right, that's exactly the kind of thing I was worried about. >>>> 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. >> Sure, mprotect also changes vm_flags, so it really needs that anyway. >>>> 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. >> What happened to shared/file-backed stuff? ISTR I had that working. >=20 > File-backed mappings are not processed in a speculative way, there = were options to manage some of them depending on the underlying file = system but that's still not done. >=20 > Shared anonymous mapping, are also not yet handled in a speculative = way (vm_ops is not null). >=20 >>>> 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. >> The tricky one is demotion, specifically write to non-write. >>>> I could not see a case where speculative path cannot see a PTE = update done via >>>> a fault on another CPU. >> One you didn't mention is the NUMA balancing scanning crud; although = I >> think that's fine, loosing a PTE update there is harmless. But I've = not >> thought overly hard on it. >=20 > That's a good point, I need to double check on that side. >=20 >>> You explained it fine. Indeed SPF is handling deferred TLB = invalidation by >>> marking the VMA through vm_write_begin/end(), as for the fork case = you >>> mentioned. Once the PTL is held, and the VMA's seqcount is checked, = the PTE >>> values read are valid. >> That should indeed work, but are we really sure we covered them all? >> Should we invest in better TLBI APIs to make sure we can't get this >> wrong? >=20 > That may be a good option to identify deferred TLB invalidation but = I've no clue on what this API would look like. I will send an RFC soon for per-table deferred TLB flushes tracking. The basic idea is to save a generation in the page-struct that tracks when deferred PTE change took place, and track whenever a TLB flush completed. In addition, other users - such as mprotect - would use the tlb_gather interface. Unfortunately, due to limited space in page-struct this would only be possible for 64-bit (and my implementation is only for x86-64). It would still require to do the copying while holding the PTL though.