Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp6023643pxu; Wed, 23 Dec 2020 11:14:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJw09Kowxj+J9V7EmhPHcU+k3w86ETBOXJz5tMBAL2h8xZxljVi+mWMbwo97oqPgO3Ppu2/r X-Received: by 2002:a17:906:9613:: with SMTP id s19mr25441905ejx.351.1608750862276; Wed, 23 Dec 2020 11:14:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608750862; cv=none; d=google.com; s=arc-20160816; b=vcS1RGM+QiIVganrBeMVH1M6gJ8YBn3njC/xNdjUJoWtjv0kdRgz7leRD9s+bYPCwF 7L1u4BskWEiHcAZG8uSflBq5ik6CLGIDo/gJ6OFnGdTYMbkf08mr1KpGQNuj6oM6+KM0 DMKJlx/k2oiQln9+1DXhv2H6tWVrfpS9HPky+wICyT2lvMbVlZYgKpbjf3b25YkDFdPL f5iwhrP8DxBGheLHjIT4MqQodku22SwEcMqzj26u7ln2EH98Q7YRn6qJuQtL/uGZ2qWk KOwUBVDjWncOVAY2Zmqqoofs+sxkCXWHQ8rqseHig78IPltWqJ7Wo/cuVRzd/U+Dcpge o/5g== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=qb0tc7BydwYkZNXccd9OZJoWvelQk+CP47NzXiB2qoM=; b=Ok2O83Y6+IhNQuwwMWm3VKooq87wBb5JFVlmcZ12pUfbtDK9NvQGwwjk4VpJHeh88s CfydXoDxaZPJtg0rZ2XPSK5XGi6bn6zh6TYgY6chMIzx1tb31mXthhXIcCVA/xm4A4JY Dn0AEyZYC68ZCz7iiaAE8AHQdch+PTWYTEMXNQPwM0UoRnzWDni6JAe3SyyYN3ndhNK4 FB44ivOVhZeCiI/JNkO3NZmTe1fCkAQXXGi8IZB1zERwLX7q2FySsBRFIS0OGYiEnZHy i6cYpRXj9m7r30zk7gPnLZ+WtaRUchTmq0ctqTyy2HpN9t7MbPuD50zCBOk051S4KQqp /O+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=krLppYrJ; 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 o21si14341591edi.210.2020.12.23.11.13.57; Wed, 23 Dec 2020 11:14:22 -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=krLppYrJ; 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 S1728011AbgLWTNK (ORCPT + 99 others); Wed, 23 Dec 2020 14:13:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727198AbgLWTNK (ORCPT ); Wed, 23 Dec 2020 14:13:10 -0500 Received: from mail-io1-xd2b.google.com (mail-io1-xd2b.google.com [IPv6:2607:f8b0:4864:20::d2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF0A8C06179C for ; Wed, 23 Dec 2020 11:12:29 -0800 (PST) Received: by mail-io1-xd2b.google.com with SMTP id z5so126990iob.11 for ; Wed, 23 Dec 2020 11:12:29 -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:in-reply-to; bh=qb0tc7BydwYkZNXccd9OZJoWvelQk+CP47NzXiB2qoM=; b=krLppYrJggRrdIffbbHEWXkW8VcHhFvGxZPH9PGQmhQtxckNSvhsxahbfh/LDcIuEi yAxOBPQVTuyhiFnU5zIRuPySMrwU6A889Qh9hAUs3F8YoqksmsA9+fTf0YIY26ppK8RL mJAj4KuTmWxGhTMxHzT0amyp+WYQahh7/M1t8MQY1cGVpCtCz2A2p0t9Im0ACNhZi8Do E78Js58M5RxDCaPlIt4yE//4aE33cDHdswhXj26VGoZg4KIv+j78rQ78/SfznD++KNeY jz7kzsa8CFWpelOuUaeXnDhjtcocQvQ3WOLdaJXxtfDiLjmf2fTduAB3ETpLZINGOtrf Da2g== 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:in-reply-to; bh=qb0tc7BydwYkZNXccd9OZJoWvelQk+CP47NzXiB2qoM=; b=JWhdEw8ZOZuM9hg30ju+g+NkgAxAwiTzAQai6JLAZiAuA8dacyKfEoPqlqLyXt05Ll tx4LhMKiC3MTlC9SIHTpTVG9giRKMU6rdw2OmwT5OWpYy6xxknu5ycnlsNXPf0bB9NOS JhUNYiXxd5dJ8Ro6xOouWYscHvLQQjOGtz8hNstrWVMEuxmDa6ln5G3yy8xUfWbGdcdX JsqMGe41qdUz3mKaHSLdZBoO6EPzwQUYLRYjMtpF0Qd5MHdNA5l04VTDy9e+kora7ADh ug+fclgaes9gtARKBQHjFt7uMqGbma2Q5zwWa+VGf6iNTypn4qIVKr0baNkYNX7o7S2e 8QvQ== X-Gm-Message-State: AOAM532/JaL+JKbX+AdvvgZJFN+VAd+g5HWGihCfP4CEijNRq/xhL1gX Xl84wkNHe52WLudgwb3MrbgEfQ== X-Received: by 2002:a02:a498:: with SMTP id d24mr23961590jam.4.1608750748680; Wed, 23 Dec 2020 11:12:28 -0800 (PST) Received: from google.com ([2620:15c:183:200:7220:84ff:fe09:2d90]) by smtp.gmail.com with ESMTPSA id m8sm25523711ioh.16.2020.12.23.11.12.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Dec 2020 11:12:28 -0800 (PST) Date: Wed, 23 Dec 2020 12:12:23 -0700 From: Yu Zhao To: Peter Xu Cc: Linus Torvalds , Andrea Arcangeli , Andy Lutomirski , Nadav Amit , 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: <20201223162416.GD6404@xz-x1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201223162416.GD6404@xz-x1> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 23, 2020 at 11:24:16AM -0500, Peter Xu wrote: > On Wed, Dec 23, 2020 at 03:06:30AM -0700, Yu Zhao wrote: > > On Wed, Dec 23, 2020 at 01:44:42AM -0800, Linus Torvalds wrote: > > > On Tue, Dec 22, 2020 at 4:01 PM Linus Torvalds > > > wrote: > > > > > > > > The more I look at the mprotect code, the less I like it. We seem to > > > > be much better about the TLB flushes in other places (looking at > > > > mremap, for example). The mprotect code seems to be very laissez-faire > > > > about the TLB flushing. > > > > > > No, this doesn't help. > > > > > > > Does adding a TLB flush to before that > > > > > > > > pte_unmap_unlock(pte - 1, ptl); > > > > > > > > fix things for you? > > > > > > It really doesn't fix it. Exactly because - as pointed out earlier - > > > the actual page *copy* happens outside the pte lock. > > > > I appreciate all the pointers. It seems to me it does. > > > > > So what can happen is: > > > > > > - CPU 1 holds the page table lock, while doing the write protect. It > > > has cleared the writable bit, but hasn't flushed the TLB's yet > > > > > > - CPU 2 did *not* have the TLB entry, sees the new read-only state, > > > takes a COW page fault, and reads the PTE from memory (into > > > vmf->orig_pte) > > > > In handle_pte_fault(), we lock page table and check pte_write(), so > > we either see a RW pte before CPU 1 runs or a RO one with no stale tlb > > entries after CPU 1 runs, assume CPU 1 flushes tlb while holding the > > same page table lock (not mmap_lock). > > I think this is not against Linus's example - where cpu2 does not have tlb > cached so it sees RO while cpu3 does have tlb cached so cpu3 can still modify > it. So IMHO there's no problem here. None of the CPUs has stale entries when CPU 2 sees a RO PTE. We are assuming that TLB flush will be done on CPU 1 while it's still holding page table lock. CPU 2 (re)locks page table and (re)checks the PTE under question when it decides if copy is necessary. If it sees a RO PTE, it means the flush has been done on all CPUs, therefore it fixes the problem. > But I do think in step 2 here we overlooked _PAGE_UFFD_WP bit. Note that if > it's uffd-wp wr-protection it's always applied along with removing of the write > bit in change_pte_range(): > > if (uffd_wp) { > ptent = pte_wrprotect(ptent); > ptent = pte_mkuffd_wp(ptent); > } > > So instead of being handled as COW page do_wp_page() will always trap > userfaultfd-wp first, hence no chance to race with COW. > > COW could only trigger after another uffd-wp-resolve ioctl which could remove > the _PAGE_UFFD_WP bit, but with Andrea's theory unprotect will only happen > after all wr-protect completes, which guarantees that when reaching the COW > path the tlb must has been flushed anyways. Then no one should be modifying > the page anymore even without pgtable lock in COW path. > > So IIUC what Linus proposed on "flushing tlb within pgtable lock" seems to > work, but it just may cause more tlb flush than Andrea's proposal especially > when the protection range is large (it's common to have a huge protection range > for e.g. VM live snapshotting, where we'll protect all guest rw ram). > > My understanding of current issue is that either we can take Andrea's proposal > (although I think the group rwsem may not be extremely better than a per-mm > rwsem, which I don't know... at least not worst than that?), or we can also go > the other way (also as Andrea mentioned) so that when wr-protect: > > - for <=2M range (pmd or less), we take read rwsem, but flush tlb within > pgtable lock > > - for >2M range, we take write rwsem directly but flush tlb once > > Thanks, > > > > > > - CPU 2 correctly decides it needs to be a COW, and copies the page contents > > > > > > - CPU 3 *does* have a stale TLB (because TLB invalidation hasn't > > > happened yet), and writes to that page in users apce > > > > > > - CPU 1 now does the TLB invalidate, and releases the page table lock > > > > > > - CPU 2 gets the page table lock, sees that its PTE matches > > > vmf->orig_pte, and switches it to be that writable copy of the page. > > > > > > where the copy happened before CPU 3 had stopped writing to the page. > > > > > > So the pte lock doesn't actually matter, unless we actually do the > > > page copy inside of it (on CPU2), in addition to doing the TLB flush > > > inside of it (on CPU1). > > > > > > mprotect() is actually safe for two independent reasons: (a) it does > > > the mmap_sem for writing (so mprotect can't race with the COW logic at > > > all), and (b) it changes the vma permissions so turning something > > > read-only actually disables COW anyway, since it won't be a COW, it > > > will be a SIGSEGV. > > > > > > So mprotect() is irrelevant, other than the fact that it shares some > > > code with that "turn it read-only in the page tables". > > > > > > fork() is a much closer operation, in that it actually triggers that > > > COW behavior, but fork() takes the mmap_sem for writing, so it avoids > > > this too. > > > > > > So it's really just userfaultfd and that kind of ilk that is relevant > > > here, I think. But that "you need to flush the TLB before releasing > > > the page table lock" was not true (well, it's true in other > > > circumstances - just not *here*), and is not part of the solution. > > > > > > Or rather, if it's part of the solution here, it would have to be > > > matched with that "page copy needs to be done under the page table > > > lock too". > > > > > > Linus > > > > > > > -- > Peter Xu >