Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp6263527pxu; Wed, 23 Dec 2020 19:36:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJyzBYRvNILvtLAtwiDxLPPoLB8g6vYGrR8iKXzR67OiDEG5OWPSZsT/4eahnxZseH3OPg09 X-Received: by 2002:aa7:cac2:: with SMTP id l2mr27026030edt.141.1608780997398; Wed, 23 Dec 2020 19:36:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608780997; cv=none; d=google.com; s=arc-20160816; b=M0I20et63mRjXvvQOhi5qV1T1MOvhNj9CRXufn/QVUPt+EIX/WMtF/FZt5d1F1vGm+ 5H0ocrl7GPhrBymr/JOIFHTjGgke9bAnHARPsrQPsI6GCVJAlm/vWPmzncuphhS8af0d yJuzc2RZyDe5pe57yVoFttIS+mwkk8QFdCWBvwVmtPbT1Yapwe6srzUJSrMQFSHxgtrc nedtqWnQO366goK3+2y8eGs4kj0vviOb2py01NvmtIlNbslAe/xxT6I2FiVzg00vASWT D3FNvqAwjuK3Qu50A39bjsqavXq+C8miTFRn6AhesTfkWknZpCK17s24F6iVL6oevQso WsQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=c5fNsg2UwF5UocytYwq0XQNimIsx8M/0x2kcxXnzw6s=; b=YP7mrVDSH3rgDXCJc/nwmHu//DY/g5NlbuQLn7eC/jQ3vtpCTtNmeI99HusybUJ3xW fOUuTKd3hk6GVFo73fqNcIFN4nXvKXlubkeIKsK3Yk9bzeI2919KUYFGG0EmOArruESh pmjjz/ofGC/Ix7Qr/aX0s0T8tEy6lJrDM4uYS7E5x/5rsnVHh+BPFu3LWsXmTo4tWbu5 NeDeTjJ+uzoFSwm7zlMHAm1eeYOQT1SBbkKlWDkpfemnB21US1zNvQ171pxce+jUCDs1 2/4lD78D8jrOXl+XxDBpLckJ165B74gEqalKzpF+yDdr5nItKyBYj7mksDSbHlJts13I EIaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=PgNEDPhR; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q6si12174051ejb.731.2020.12.23.19.36.15; Wed, 23 Dec 2020 19:36:37 -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=@google.com header.s=20161025 header.b=PgNEDPhR; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728932AbgLXDew (ORCPT + 99 others); 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-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. >