Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp13862pxb; Tue, 12 Jan 2021 18:30:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJw8uLQctcWhjAZ6tFkPP48Um+GabrgMsN11Ts+aHsArO8uXwcvTs42+j/DJ4ViiD7x/lPcN X-Received: by 2002:a17:907:b01:: with SMTP id h1mr1214403ejl.450.1610505029621; Tue, 12 Jan 2021 18:30:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610505029; cv=none; d=google.com; s=arc-20160816; b=MgJ7/U4kMMqjDb/p+xNqHBERvLjjulGQ1IirR+ZQFMI2G+zEdn9WYuK3IpN1OEIipN A792JZy2bIDIben4OoJFWVDI04ZfG91rDeVf1ngcTH+vqWfJ49GkdZRLwLJdi/GSqV44 7GBPe9MwFOiY5aEKiSGUddri7mCRcIrb/J3xA4QHX4TsDjUpF9Kk/zl6MyHHjK2oylW6 2e9wZCDPuVXm98BcKEka9UNouqpOmWw1ZPem3IYO9Tmmavuwb8ei2vP3WnEoAZ3jls4k 2dn4zuBGGFw8comKN+MKXdWWX7OdBR7ymxA3Ctb8dYWzyrbDOq4qU7SPMW1NonqUuW6z weSw== 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=m+OlJPIb+Y7HL3mVvnn3aPJy9H6Uy5divcbzK7j1Vac=; b=FCIPeb8DlsTMEvkN7KGaJDG9FscXr6gNTwLhIVX1fC4jjjfjb05hl58tNHvvCjqbdZ 5Jrn6pvNG8Lhm7Fz1CgUa9Gi1VJUdtJ22lfYvXXi3AgEQwBZ1Yh6J29wHzYTFr5ZNgGa 7vJCxkag1s36JkOUZH2xgXXsDD7gW8SXuzDV/PPKC8cmUp+tvjUeNhN6O244GvdZYlE4 P/5Jmp6kSYNaNm/zUox+V8E6VuWXhnxQBHeDJoIyBWtSvz6hsS1pkMVL1qRdNDkhxHpf P2BFSJ0rBRvC2vaM/rRDl0ZCy8BEJ3XL5x8l0kfW2qpZXePv2C7mRDaykM2cZCJqYJNa c14w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="ijpBBn3/"; 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 dj28si286945edb.352.2021.01.12.18.30.06; Tue, 12 Jan 2021 18:30:29 -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="ijpBBn3/"; 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 S2406932AbhALT5N (ORCPT + 99 others); Tue, 12 Jan 2021 14:57:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406911AbhALT5M (ORCPT ); Tue, 12 Jan 2021 14:57:12 -0500 Received: from mail-io1-xd36.google.com (mail-io1-xd36.google.com [IPv6:2607:f8b0:4864:20::d36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2522C061794 for ; Tue, 12 Jan 2021 11:56:31 -0800 (PST) Received: by mail-io1-xd36.google.com with SMTP id e22so6666285iom.5 for ; Tue, 12 Jan 2021 11:56:31 -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=m+OlJPIb+Y7HL3mVvnn3aPJy9H6Uy5divcbzK7j1Vac=; b=ijpBBn3/eYfeKjIklKWS1c5NBUUWU/glzhzuVqSiYrNeGFj/33i6sMGT6ABu6v8y30 6ClaiIxIJqZhO4gWRmAEMM8ki53XQp0vzKenQLOwDafAPdfAUk+Vbnq4B3TNJENciWDT MmDQ7CXTXbXr2Um9vBiU+rIbTYtlacGzKba6j2rW5bJsOWWQ2hs8mBkgg7y72tigagm/ Bm2iym+5uqEeisSbob3R540G7m4/iv7PmuZahZWdNM49malb01s1pvyViEfA+RFHZsW3 GsxZJQUp46Ei8tm92rtxLpIVxvAEsDXGuDmPDaYhrBj7dyFy+1+i7IyWsnz3R56nGBGS Uj2g== 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=m+OlJPIb+Y7HL3mVvnn3aPJy9H6Uy5divcbzK7j1Vac=; b=EiNd6iatbtb2aEUwrwC1Wme+uhCjE1UioBxF8f0rnMLzffco7oYDT3PVJ4oKqe+1My QpGugcP3+lbwcU5CKD7hwLLx20ejqex15z5MnyNwWH3xag40r4T1W10HvJ4bRIr8/HYN dpzUJXPHbybO0+xtg1m8wAn+MSB8J8m433ntTU/tj4ixYOWOg25zutmBhWKMOunyZw5T Mr8XokF9jWtBaj2h155ENbwaK7jaWXJN6bnHweu3WOd40GI2W0+Q3Dv9JJSLwyttzPn7 bLczOWZwNDrKu0CdJxph+YrMQ8eyF2U2OidB4eyG5FgayNV2+gFA9mVyBKslF5hAdqkp qfMg== X-Gm-Message-State: AOAM530eZlSDzH1Bh+JUNkhJn/lPKNlhrAU4V4Nf0kAAjGvNAQvIli71 PCSjANYoMkgoMxwenRMcl5+cFg== X-Received: by 2002:a92:dc8e:: with SMTP id c14mr757390iln.54.1610481391209; Tue, 12 Jan 2021 11:56:31 -0800 (PST) Received: from google.com ([2620:15c:183:200:7220:84ff:fe09:2d90]) by smtp.gmail.com with ESMTPSA id n16sm3147956ilj.19.2021.01.12.11.56.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jan 2021 11:56:30 -0800 (PST) Date: Tue, 12 Jan 2021 12:56:25 -0700 From: Yu Zhao To: Nadav Amit Cc: Laurent Dufour , Peter Zijlstra , Vinayak Menon , Linus Torvalds , Andy Lutomirski , Peter Xu , Andrea Arcangeli , linux-mm , lkml , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , stable , Minchan Kim , Will Deacon , surenb@google.com Subject: Re: [PATCH] mm/userfaultfd: fix memory corruption due to writeprotect Message-ID: References: <20201221223041.GL6640@xz-x1> <20210105153727.GK3040@hirez.programming.kicks-ass.net> <0201238b-e716-2a3c-e9ea-d5294ff77525@linux.vnet.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 12, 2021 at 11:15:43AM -0800, Nadav Amit wrote: > > On Jan 12, 2021, at 11:02 AM, Laurent Dufour wrote: > > > > Le 12/01/2021 ? 17:57, Peter Zijlstra a ?crit : > >> On Tue, Jan 12, 2021 at 04:47:17PM +0100, Laurent Dufour wrote: > >>> Le 12/01/2021 ? 12:43, Vinayak Menon a ?crit : > >>>> 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/ > >> 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. > > > > 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. > > > > Shared anonymous mapping, are also not yet handled in a speculative way (vm_ops is not null). > > > >>>> 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. > > > > That's a good point, I need to double check on that side. > > > >>> 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? > > > > 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). I don't want to discourage you but I don't think this would end up well. PPC doesn't necessarily follow one-page-struct-per-table rule, and I've run into problems with this before while trying to do something similar. I'd recommend per-vma and per-category (unmapping, clearing writable and clearing dirty) tracking, which only rely on arch-independent data structures, i.e., vm_area_struct and mm_struct. > It would still require to do the copying while holding the PTL though. IMO, this is unacceptable. Most archs don't support per-table PTL, and even x86_64 can be configured to use per-mm PTL. What if we want to support a larger page size in the feature? It seems to me the only way to solve the problem with self-explanatory code and without performance impact is to check mm_tlb_flush_pending and the writable bit (and two other cases I mentioned above) at the same time. Of course, this requires a lot of effort to audit the existing uses, clean them up and properly wrap them up with new primitives, BUG_ON all invalid cases and document the exact workflow to prevent misuses. I've mentioned the following before -- it only demonstrates the rough idea. diff --git a/mm/memory.c b/mm/memory.c index 5e9ca612d7d7..af38c5ee327e 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4403,8 +4403,11 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf) goto unlock; } if (vmf->flags & FAULT_FLAG_WRITE) { - if (!pte_write(entry)) + if (!pte_write(entry)) { + if (mm_tlb_flush_pending(vmf->vma->vm_mm)) + flush_tlb_page(vmf->vma, vmf->address); return do_wp_page(vmf); + } entry = pte_mkdirty(entry); } entry = pte_mkyoung(entry);