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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 883DCC4321E for ; Thu, 1 Dec 2022 15:28:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 078C86B0071; Thu, 1 Dec 2022 10:28:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0295D6B0075; Thu, 1 Dec 2022 10:28:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E0C0D6B0078; Thu, 1 Dec 2022 10:28:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id CCF206B0071 for ; Thu, 1 Dec 2022 10:28:44 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A2C7F12120D for ; Thu, 1 Dec 2022 15:28:44 +0000 (UTC) X-FDA: 80194119768.07.43B4A97 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id D7C891C000E for ; Thu, 1 Dec 2022 15:28:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669908523; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=4JN6Y0JKwNo2MyPJkzuMcb+wstjq2QWXvJdh/1a52mc=; b=EGzMNTZ1r8T81rvjjldlqjuVYiFG66To6ShrL0K8uYf2yI2HlA3dKH8p15jmMFpeMRmsQx y5lyZCxgH4Rpgnb3cR0HMxWceuPGYxQ6wq80/d0nFm66qfsOFVs4d0brualT0lAd38i5cK hYL9pZDH/7Q4mpQ9QY33nOn0o7UxLfg= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-478-vGh57l_0O7O39kGgZbv8Gg-1; Thu, 01 Dec 2022 10:28:42 -0500 X-MC-Unique: vGh57l_0O7O39kGgZbv8Gg-1 Received: by mail-qv1-f72.google.com with SMTP id p17-20020a05621415d100b004c6a488c925so5240356qvz.2 for ; Thu, 01 Dec 2022 07:28:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=BdDC1WAtZV0XTmkt9D4E/axQjlEA5HE6l2HGiOiq2ps=; b=RkwjwdYS3aoXFcLR6tGePID4Q7G0tRMhjl9MZkOwmr9XoKtQjNAnsFwFSn4hdmCkez +zZ1msygmg+mQLBW5qZ4Bsam/w76/Vi6v7gTS6EXPmrLnIURw/Hoj4v/HAps31UlT9Bn H6S9cTonNgcxmcTBp4kZl6SD22UuxThspl8fxUDxlyBHPL/sr5Lw3sv/SoO/jYCpyYa5 cMVvMNLmyk7/Bzfz+GVsdPVL1n5ZPQMfmjtgP57tly+etYuy0YZY89a/QwOXA10NHSD6 HR33cA5z8s5SqfZegPnj4apISTO4+knwflN5GMHF3uTl9v5fwbUV/l97GS7ALwunIO0L mw7g== X-Gm-Message-State: ANoB5pnDCXafyPSdn3GJlNAsY48hsbWJdgB+TySs/1JN7QNIQkpz8RCk rBgeGKbiLRvgiUiiVwPaG06Ud+mGFBC2IWlmrNYFWdKC3xykBsAOUjI09t8SDSUjlGIwgeD45EA 8/a/NgdPiNMw= X-Received: by 2002:ae9:e415:0:b0:6f3:e5c8:ddde with SMTP id q21-20020ae9e415000000b006f3e5c8dddemr42575927qkc.80.1669908512371; Thu, 01 Dec 2022 07:28:32 -0800 (PST) X-Google-Smtp-Source: AA0mqf7SDmgvGwuxBNpX3bmNkH7gq7tVsmOyoad4tTaFFGWQ0CEzE5W7pZEcGNLGqCcuZqnkFFN+XA== X-Received: by 2002:ae9:e415:0:b0:6f3:e5c8:ddde with SMTP id q21-20020ae9e415000000b006f3e5c8dddemr42575895qkc.80.1669908512067; Thu, 01 Dec 2022 07:28:32 -0800 (PST) Received: from x1n (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id h13-20020a05620a244d00b006fba44843a5sm3670991qkn.52.2022.12.01.07.28.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Dec 2022 07:28:31 -0800 (PST) Date: Thu, 1 Dec 2022 10:28:30 -0500 From: Peter Xu To: Andrew Morton Cc: David Hildenbrand , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Mike Rapoport , Nadav Amit , Andrea Arcangeli , Ives van Hoorne , Axel Rasmussen , Alistair Popple , stable@vger.kernel.org Subject: Re: [PATCH v3 1/2] mm/migrate: Fix read-only page got writable when recover pte Message-ID: References: <20221114000447.1681003-1-peterx@redhat.com> <20221114000447.1681003-2-peterx@redhat.com> <5ddf1310-b49f-6e66-a22a-6de361602558@redhat.com> <20221130142425.6a7fdfa3e5954f3c305a77ee@linux-foundation.org> MIME-Version: 1.0 In-Reply-To: <20221130142425.6a7fdfa3e5954f3c305a77ee@linux-foundation.org> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="CjNznIJt6mVQmyak" Content-Disposition: inline ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EGzMNTZ1; spf=pass (imf18.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669908524; a=rsa-sha256; cv=none; b=yD4Qyi43IFbDnAs7TydaSDZJkpNzA8Ompt7EzRJvTUW/6ItsgZWe86aseho2szTRo4Y7te cthFz67kdTtqEjSAKXcSOy4EyuuZsKh9ag1MNyqlxYEXqPRKZpd7S/xAYtwkBGnKR6+rVA 51xmtRGzAodPNacd77Be/5ssBZojt2A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669908524; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4JN6Y0JKwNo2MyPJkzuMcb+wstjq2QWXvJdh/1a52mc=; b=GLozxlMfboOINrnCHJDFJ/XRsG7NY/olziu2o3yRHTHxJMmQIzIVNBzlA1FkW0VfWIXeEh SuuQRmCDHgBIdFddqv/Ne1qTXJl7j8PVj79EVYJEoO0q/jsQwLleK4+lsMbdL/NTibY+oz SaNLwZYJaIITU1e282y6TBoVNcX/HSQ= Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EGzMNTZ1; spf=pass (imf18.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam01 X-Stat-Signature: kc394sqg9w9kk9bu1pgfp6aqfcad15a5 X-Rspamd-Queue-Id: D7C891C000E X-Rspam-User: X-HE-Tag: 1669908523-768184 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: --CjNznIJt6mVQmyak Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Hi, Andrew, On Wed, Nov 30, 2022 at 02:24:25PM -0800, Andrew Morton wrote: > On Tue, 15 Nov 2022 19:17:43 +0100 David Hildenbrand wrote: > > > On 14.11.22 01:04, Peter Xu wrote: > > > Ives van Hoorne from codesandbox.io reported an issue regarding possible > > > data loss of uffd-wp when applied to memfds on heavily loaded systems. The > > > symptom is some read page got data mismatch from the snapshot child VMs. > > > > > > Here I can also reproduce with a Rust reproducer that was provided by Ives > > > that keeps taking snapshot of a 256MB VM, on a 32G system when I initiate > > > 80 instances I can trigger the issues in ten minutes. > > > > > > It turns out that we got some pages write-through even if uffd-wp is > > > applied to the pte. > > > > > > The problem is, when removing migration entries, we didn't really worry > > > about write bit as long as we know it's not a write migration entry. That > > > may not be true, for some memory types (e.g. writable shmem) mk_pte can > > > return a pte with write bit set, then to recover the migration entry to its > > > original state we need to explicit wr-protect the pte or it'll has the > > > write bit set if it's a read migration entry. For uffd it can cause > > > write-through. > > > > > > The relevant code on uffd was introduced in the anon support, which is > > > commit f45ec5ff16a7 ("userfaultfd: wp: support swap and page migration", > > > 2020-04-07). However anon shouldn't suffer from this problem because anon > > > should already have the write bit cleared always, so that may not be a > > > proper Fixes target, while I'm adding the Fixes to be uffd shmem support. > > > > > > > ... > > > > > --- a/mm/migrate.c > > > +++ b/mm/migrate.c > > > @@ -213,8 +213,14 @@ static bool remove_migration_pte(struct folio *folio, > > > pte = pte_mkdirty(pte); > > > if (is_writable_migration_entry(entry)) > > > pte = maybe_mkwrite(pte, vma); > > > - else if (pte_swp_uffd_wp(*pvmw.pte)) > > > + else > > > + /* NOTE: mk_pte can have write bit set */ > > > + pte = pte_wrprotect(pte); > > > + > > > + if (pte_swp_uffd_wp(*pvmw.pte)) { > > > + WARN_ON_ONCE(pte_write(pte)); > > Will this warnnig trigger in the scenario you and Ives have discovered? If without the above newly added wr-protect, yes. This is the case where we found we got write bit set even if uffd-wp bit is also set, hence allows the write to go through even if marked protected. > > > > pte = pte_mkuffd_wp(pte); > > > + } > > > > > > if (folio_test_anon(folio) && !is_readable_migration_entry(entry)) > > > rmap_flags |= RMAP_EXCLUSIVE; > > > > As raised, I don't agree to this generic non-uffd-wp change without > > further, clear justification. > > Pater, can you please work this further? I didn't reply here because I have already replied with the question in previous version with a few attempts. Quotting myself: https://lore.kernel.org/all/Y3KgYeMTdTM0FN5W@x1n/ The thing is recovering the pte into its original form is the safest approach to me, so I think we need justification on why it's always safe to set the write bit. I've also got another longer email trying to explain why I think it's the other way round to be justfied, rather than justifying removal of the write bit for a read migration entry, here: https://lore.kernel.org/all/Y3O5bCXSbvKJrjRL@x1n/ > > > I won't nack it, but I won't ack it either. > > I wouldn't mind seeing a little code comment which explains why we're > doing this. I've got one more fixup to the same patch attached, with enriched comments on why we need wr-protect for read migration entries. Please have a look to see whether that helps, thanks. -- Peter Xu --CjNznIJt6mVQmyak Content-Type: text/plain; charset=utf-8 Content-Disposition: attachment; filename="0001-fixup-mm-migrate-fix-read-only-page-got-writable-whe.patch" >From d68c98047ce54c62f3454997a55f23ff6fb317cd Mon Sep 17 00:00:00 2001 From: Peter Xu Date: Thu, 1 Dec 2022 10:19:22 -0500 Subject: [PATCH] fixup! mm/migrate: fix read-only page got writable when recover pte Content-type: text/plain Signed-off-by: Peter Xu --- mm/migrate.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/migrate.c b/mm/migrate.c index c13c828d34f3..d14f1f3ab073 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -214,7 +214,14 @@ static bool remove_migration_pte(struct folio *folio, if (is_writable_migration_entry(entry)) pte = maybe_mkwrite(pte, vma); else - /* NOTE: mk_pte can have write bit set */ + /* + * NOTE: mk_pte() can have write bit set per memory + * type (e.g. shmem), or pte_mkdirty() per archs + * (e.g., sparc64). If this is a read migration + * entry, we need to make sure when we recover the + * pte from migration entry to present entry the + * write bit is cleared. + */ pte = pte_wrprotect(pte); if (pte_swp_uffd_wp(*pvmw.pte)) { -- 2.37.3 --CjNznIJt6mVQmyak--