From patchwork Thu Nov 10 20:31:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13039316 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 180FDC433FE for ; Thu, 10 Nov 2022 20:31:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 931D06B007B; Thu, 10 Nov 2022 15:31:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E2318E0002; Thu, 10 Nov 2022 15:31:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 782FD8E0001; Thu, 10 Nov 2022 15:31:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 6ABA66B007B for ; Thu, 10 Nov 2022 15:31:40 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 319371C717E for ; Thu, 10 Nov 2022 20:31:40 +0000 (UTC) X-FDA: 80118678360.05.8ED1B9B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf21.hostedemail.com (Postfix) with ESMTP id 4D5111C000B for ; Thu, 10 Nov 2022 20:31:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668112297; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ff4uIKDwEIX1/MsTGENOmYM+Xv3Uc0BmHEC53Fnq3vY=; b=fO5ncn0BdyuBNXtQzyxtq9VlbhBGAKcszrRDQTo/jkWRwUjWIFdAx2zVmQco//k/pFiUFu LDYJQG4yQbPqtZMI1VVEeA5GT1dpvgq6yp9Qj7JbL+lcJ92xcKXvCkr/pEA7q+rFzxclKY CWYl0HpgpF0rwOkcb0LBjDEkuMdgkvg= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-59-lEp5MI__OP694sj2nPc8YQ-1; Thu, 10 Nov 2022 15:31:36 -0500 X-MC-Unique: lEp5MI__OP694sj2nPc8YQ-1 Received: by mail-qt1-f197.google.com with SMTP id w27-20020a05622a191b00b003a56c0e1cd0so2247011qtc.4 for ; Thu, 10 Nov 2022 12:31:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ff4uIKDwEIX1/MsTGENOmYM+Xv3Uc0BmHEC53Fnq3vY=; b=CC5IGtpqTHPodQWFTOH9L5D8zdEwXxUs9azq3feSaGNnJHnVUQVXrn2EX0fJnjKdbZ ExVKriGVXBXcDREbaPBDumqbE8SoghC0DCMnM7Ek6F4XyVGrheMyKJaB7gsAMolhauo9 zjEhQOXkzmc/mClGKWaS1G/JviQ4TcIAUyH1dD0BIjHsUssNbeeG6KucchzpOnKApYbO Gqrw0uV5qXt0miv3L4qXyZ8UpGL/wvocAGGm5eMCgbWdaV+e6gt+jSGybbPGzom5SMU2 cRoprT0P6vnisWgRudEmgkXx5Z29OUGAR/OW6mauj/xZG0N7evxtdGLiAKu4TodfhbYv hDRw== X-Gm-Message-State: ACrzQf3u50mXuW/rW05rs6EakcuhFA8H1iZWvF+0Qbqk+8adkhq0zvxX ZWuP3Y1WtMV1GWYTNQLCb0V8HuRPuJe5AKyr4D4DcPcdZaWoKihGpMQxpaK6ZPvddwnvT2b23mO oZv/srfipMUQ= X-Received: by 2002:a05:620a:2455:b0:6fa:3fbf:6b51 with SMTP id h21-20020a05620a245500b006fa3fbf6b51mr1747390qkn.519.1668112296205; Thu, 10 Nov 2022 12:31:36 -0800 (PST) X-Google-Smtp-Source: AMsMyM48Dbsd61+EMKMLnYyrMapQFxdZHIJkOTIgyuDxqxIk8mfk0DBVx1tPHeFMdbbAkCTVyomt5g== X-Received: by 2002:a05:620a:2455:b0:6fa:3fbf:6b51 with SMTP id h21-20020a05620a245500b006fa3fbf6b51mr1747373qkn.519.1668112295954; Thu, 10 Nov 2022 12:31:35 -0800 (PST) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id i1-20020ac860c1000000b00399edda03dfsm123588qtm.67.2022.11.10.12.31.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 12:31:35 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: peterx@redhat.com, Andrea Arcangeli , Axel Rasmussen , Ives van Hoorne , Nadav Amit , Andrew Morton , Mike Rapoport , stable@vger.kernel.org Subject: [PATCH v2 1/2] mm/migrate: Fix read-only page got writable when recover pte Date: Thu, 10 Nov 2022 15:31:31 -0500 Message-Id: <20221110203132.1498183-2-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221110203132.1498183-1-peterx@redhat.com> References: <20221110203132.1498183-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668112298; a=rsa-sha256; cv=none; b=RoJPdm3HMnkK6Z7FrGLXE8/ENd/iWUIalE5L9qugEkyvlqNZfAgEAu9O8L2rZeUL/odSQ6 J1RA4BTh3jqSHInENmITygV4A9udnJgXyI/QOqIXl9CsTROrHQ7DMcYWOzpc/D3Iv/WW19 bgaAtViPU98BNlswXwu0PluRLb44eWo= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=fO5ncn0B; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf21.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668112298; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Ff4uIKDwEIX1/MsTGENOmYM+Xv3Uc0BmHEC53Fnq3vY=; b=G6sNATsTlmCkPwE7Ku4iW0zLTeVPPef51HYkR8o15q+Kl4aRFzQVxarz9nieOQAWZnymj8 HBJLbfL+BbRlwbM1F9sf1A7S3AshN1Ah0nBwhfet5iZ/n/KJVd+nXG9lPlxUDPqaiFGmev 8IfmnTeoS7hxMEekn64q7SOwsK4+kB8= Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=fO5ncn0B; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf21.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com X-Rspam-User: X-Stat-Signature: 7ax1xna53h1u1mm4eizzd1b8ys5ta84r X-Rspamd-Queue-Id: 4D5111C000B X-Rspamd-Server: rspam05 X-HE-Tag: 1668112298-901502 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: 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 sympton 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. I didn't verify, but I think it'll be the same for mprotect()ed pages and after migration we can miss the sigbus instead. 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. To satisfy the need on the backport, I'm attaching the Fixes tag to the uffd-wp shmem support. Since no one had issue with mprotect, so I assume that's also the kernel version we should start to backport for stable, and we shouldn't need to worry before that. Cc: Andrea Arcangeli Cc: stable@vger.kernel.org Fixes: b1f9e876862d ("mm/uffd: enable write protection for shmem & hugetlbfs") Reported-by: Ives van Hoorne Signed-off-by: Peter Xu Reviewed-by: Alistair Popple --- mm/migrate.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/migrate.c b/mm/migrate.c index dff333593a8a..8b6351c08c78 100644 --- 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)); pte = pte_mkuffd_wp(pte); + } if (folio_test_anon(folio) && !is_readable_migration_entry(entry)) rmap_flags |= RMAP_EXCLUSIVE;