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=-13.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL 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 16581C433E0 for ; Thu, 24 Dec 2020 03:35:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CF15022A99 for ; Thu, 24 Dec 2020 03:35:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728969AbgLXDew (ORCPT ); Wed, 23 Dec 2020 22:34:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728710AbgLXDew (ORCPT ); Wed, 23 Dec 2020 22:34:52 -0500 Received: from mail-il1-x133.google.com (mail-il1-x133.google.com [IPv6:2607:f8b0:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62B8CC06179C for ; Wed, 23 Dec 2020 19:34:06 -0800 (PST) Received: by mail-il1-x133.google.com with SMTP id q5so922445ilc.10 for ; Wed, 23 Dec 2020 19:34:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=c5fNsg2UwF5UocytYwq0XQNimIsx8M/0x2kcxXnzw6s=; b=PgNEDPhRRco6MUm+b35ScL9j7jTW2b7WyA5+rPGX4xjQrF6SUP/6+x/45YJPtKsHtG +pw2Aig8V5nQJ2pMAor1ZD3H9qsk2OPqg+BJ6NRonx+8A5Ph3TLfa+3BEvoH+W65zwHs 52a2XnIgl0IVSgJZPRgMOO/G/7vAYwyW1rBkk2Lb4b/aJuqeHYZ05wrVsjliA/IcAfoC mJ1cmIuuNuZ0I1Jx9dM1iGTfOwJU4ubp6ZkIf1Q48WGSVqulGu+na8E+JuA2Plv4CrFD MTVZlotsa8kip62TOCTXmoqLGNdIL65SxXUH1gTe9J7QfqcxmGdBBVDoXUU8FIcr45Ns sysA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=c5fNsg2UwF5UocytYwq0XQNimIsx8M/0x2kcxXnzw6s=; b=rAPrwPjdxcJWP26LH1XUK85/Ec6WLj39mlSluidyNAwZ9fSmirGTFJRdRHq3+22VL2 kcctn7urUhLuHMlVYg42Fa8T85iiK02yIofNTw566ANNQktsZQGrb8aKw9hS39SFnrLN GMmSIFWIEkEjp7yZLboLkxojqp9z8kAJ3GEGxpAjEJWU3MPrxvTQC3B9wGtZYql6xpSR 9D9Q07406qzQiHJxPqVFlanRjKnDLkEU9wxrGi5eeNur6QOhY6l7xUjMVB2hFI14TlON eLmL+gZKMHzLM4v3rpAn/4XS0QOzJDeUbMSWncP0W0FTTHJcPpthPJCB2S/GJyHXF4QN y+CA== X-Gm-Message-State: AOAM5334+Mnckoqpqu+qH8HMdlhhdMScrfYLXvr2NIElrbzACyXpAYtb hWvTKoAd9HZsiiU6fUN86csTew== X-Google-Smtp-Source: ABdhPJwlSb2HJ2fXreVypNQMIQPBK3ZPIqIThkVchlUswu3+TbmFlW0XsYozNSC0nOOqS5dkRor2sg== X-Received: by 2002:a92:ca91:: with SMTP id t17mr27317300ilo.67.1608780845418; Wed, 23 Dec 2020 19:34:05 -0800 (PST) Received: from google.com ([2620:15c:183:200:7220:84ff:fe09:2d90]) by smtp.gmail.com with ESMTPSA id e9sm18039843ils.14.2020.12.23.19.34.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Dec 2020 19:34:04 -0800 (PST) Date: Wed, 23 Dec 2020 20:34:00 -0700 From: Yu Zhao To: Nadav Amit Cc: Andrea Arcangeli , Andy Lutomirski , Andy Lutomirski , Linus Torvalds , Peter Xu , linux-mm , lkml , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , stable , Minchan Kim , Will Deacon , Peter Zijlstra Subject: Re: [PATCH] mm/userfaultfd: fix memory corruption due to writeprotect Message-ID: References: <3A6A1049-24C6-4B2D-8C59-21B549F742B4@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <3A6A1049-24C6-4B2D-8C59-21B549F742B4@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 23, 2020 at 07:09:10PM -0800, Nadav Amit wrote: > > On Dec 23, 2020, at 6:00 PM, Andrea Arcangeli wrote: > > > > On Wed, Dec 23, 2020 at 05:21:43PM -0800, Andy Lutomirski wrote: > >> I don’t love this as a long term fix. AFAICT we can have mm_tlb_flush_pending set for quite a while — mprotect seems like it can wait in IO while splitting a huge page, for example. That gives us a window in which every write fault turns into a TLB flush. > > > > mprotect can't run concurrently with a page fault in the first place. > > > > One other near zero cost improvement easy to add if this would be "if > > (vma->vm_flags & (VM_SOFTDIRTY|VM_UFFD_WP))" and it could be made > > conditional to the two config options too. > > > > Still I don't mind doing it in some other way, uffd-wp has much easier > > time doing it in another way in fact. > > > > Whatever performs better is fine, but queuing up pending invalidate > > ranges don't look very attractive since it'd be a fixed cost that we'd > > always have to pay even when there's no fault (and there can't be any > > fault at least for mprotect). > > I think there are other cases in which Andy’s concern is relevant > (MADV_PAGEOUT). That patch only demonstrate a rough idea and I should have been elaborate: if we ever decide to go that direction, we only need to worry about "jumping through hoops", because the final patch (set) I have in mind would not only have the build time optimization Andrea suggested but also include runtime optimizations like skipping do_swap_page() path and (!PageAnon() || page_mapcount > 1). Rest assured, the performance impact on do_wp_page() from occasionally an additional TLB flush on top of a page copy is negligible. > Perhaps holding some small bitmap based on part of the deferred flushed > pages (e.g., bits 12-17 of the address or some other kind of a single > hash-function bloom-filter) would be more performant to avoid (most) > unnecessary TLB flushes. It will be cleared before a TLB flush and set while > holding the PTL. > > Checking if a flush is needed, under the PTL, would require a single memory > access (although potentially cache miss). It will however require one atomic > operation for each page-table whose PTEs’ flushes are deferred - in contrast > to the current scheme which requires two atomic operations for the *entire* > operation. >